arphaman created this revision. arphaman added reviewers: Bigcheese, dexonsmith. Herald added subscribers: tschuett, jkorous. Herald added a project: clang.
We should re-emit `#pragma once` to ensure the preprocessor will still honor when running on minimized sources. Repository: rC Clang https://reviews.llvm.org/D64945 Files: clang/include/clang/Lex/DependencyDirectivesSourceMinimizer.h clang/lib/Lex/DependencyDirectivesSourceMinimizer.cpp clang/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp Index: clang/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp =================================================================== --- clang/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp +++ clang/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp @@ -544,4 +544,20 @@ EXPECT_STREQ("#include <test.h>\n", Out.data()); } +TEST(MinimizeSourceToDependencyDirectivesTest, PragmaOnce) { + SmallVector<char, 128> Out; + SmallVector<Token, 4> Tokens; + + StringRef Source = R"(// comment +#pragma once +// another comment +#include <test.h> +)"; + ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out, Tokens)); + EXPECT_STREQ("#pragma once\n#include <test.h>\n", Out.data()); + ASSERT_EQ(Tokens.size(), 3u); + EXPECT_EQ(Tokens[0].K, + minimize_source_to_dependency_directives::pp_pragma_once); +} + } // end anonymous namespace Index: clang/lib/Lex/DependencyDirectivesSourceMinimizer.cpp =================================================================== --- clang/lib/Lex/DependencyDirectivesSourceMinimizer.cpp +++ clang/lib/Lex/DependencyDirectivesSourceMinimizer.cpp @@ -612,7 +612,21 @@ bool Minimizer::lexPragma(const char *&First, const char *const End) { // #pragma. - if (!isNextIdentifier("clang", First, End)) { + skipWhitespace(First, End); + if (First == End || !isIdentifierHead(*First)) + return false; + + IdInfo FoundId = lexIdentifier(First, End); + First = FoundId.Last; + if (FoundId.Name == "once") { + // #pragma once + skipLine(First, End); + makeToken(pp_pragma_once); + append("#pragma once\n"); + return false; + } + + if (FoundId.Name != "clang") { skipLine(First, End); return false; } Index: clang/include/clang/Lex/DependencyDirectivesSourceMinimizer.h =================================================================== --- clang/include/clang/Lex/DependencyDirectivesSourceMinimizer.h +++ clang/include/clang/Lex/DependencyDirectivesSourceMinimizer.h @@ -38,6 +38,7 @@ pp_undef, pp_import, pp_pragma_import, + pp_pragma_once, pp_include_next, pp_if, pp_ifdef,
Index: clang/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp =================================================================== --- clang/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp +++ clang/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp @@ -544,4 +544,20 @@ EXPECT_STREQ("#include <test.h>\n", Out.data()); } +TEST(MinimizeSourceToDependencyDirectivesTest, PragmaOnce) { + SmallVector<char, 128> Out; + SmallVector<Token, 4> Tokens; + + StringRef Source = R"(// comment +#pragma once +// another comment +#include <test.h> +)"; + ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out, Tokens)); + EXPECT_STREQ("#pragma once\n#include <test.h>\n", Out.data()); + ASSERT_EQ(Tokens.size(), 3u); + EXPECT_EQ(Tokens[0].K, + minimize_source_to_dependency_directives::pp_pragma_once); +} + } // end anonymous namespace Index: clang/lib/Lex/DependencyDirectivesSourceMinimizer.cpp =================================================================== --- clang/lib/Lex/DependencyDirectivesSourceMinimizer.cpp +++ clang/lib/Lex/DependencyDirectivesSourceMinimizer.cpp @@ -612,7 +612,21 @@ bool Minimizer::lexPragma(const char *&First, const char *const End) { // #pragma. - if (!isNextIdentifier("clang", First, End)) { + skipWhitespace(First, End); + if (First == End || !isIdentifierHead(*First)) + return false; + + IdInfo FoundId = lexIdentifier(First, End); + First = FoundId.Last; + if (FoundId.Name == "once") { + // #pragma once + skipLine(First, End); + makeToken(pp_pragma_once); + append("#pragma once\n"); + return false; + } + + if (FoundId.Name != "clang") { skipLine(First, End); return false; } Index: clang/include/clang/Lex/DependencyDirectivesSourceMinimizer.h =================================================================== --- clang/include/clang/Lex/DependencyDirectivesSourceMinimizer.h +++ clang/include/clang/Lex/DependencyDirectivesSourceMinimizer.h @@ -38,6 +38,7 @@ pp_undef, pp_import, pp_pragma_import, + pp_pragma_once, pp_include_next, pp_if, pp_ifdef,
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits