From 4cd905bdc1a84b8f9424a031c56e3d99249082f9 Mon Sep 17 00:00:00 2001 From: Alex Lorenz Date: Thu, 18 Jul 2019 22:33:14 +0000 Subject: [PATCH] [clang-scan-deps] Dependency directives source minimizer: handle #pragma once We should re-emit `#pragma once` to ensure the preprocessor will still honor it when running on minimized sources. Differential Revision: https://reviews.llvm.org/D64945 llvm-svn: 366509 --- .../Lex/DependencyDirectivesSourceMinimizer.h | 1 + .../Lex/DependencyDirectivesSourceMinimizer.cpp | 16 ++++++++++++++- .../DependencyDirectivesSourceMinimizerTest.cpp | 24 ++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/clang/include/clang/Lex/DependencyDirectivesSourceMinimizer.h b/clang/include/clang/Lex/DependencyDirectivesSourceMinimizer.h index 4164107..39ea396 100644 --- a/clang/include/clang/Lex/DependencyDirectivesSourceMinimizer.h +++ b/clang/include/clang/Lex/DependencyDirectivesSourceMinimizer.h @@ -38,6 +38,7 @@ enum TokenKind { pp_undef, pp_import, pp_pragma_import, + pp_pragma_once, pp_include_next, pp_if, pp_ifdef, diff --git a/clang/lib/Lex/DependencyDirectivesSourceMinimizer.cpp b/clang/lib/Lex/DependencyDirectivesSourceMinimizer.cpp index cfc37c5..2e8c5f3 100644 --- a/clang/lib/Lex/DependencyDirectivesSourceMinimizer.cpp +++ b/clang/lib/Lex/DependencyDirectivesSourceMinimizer.cpp @@ -612,7 +612,21 @@ bool Minimizer::lexDefine(const char *&First, const char *const End) { 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; } diff --git a/clang/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp b/clang/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp index cfa3ec9..38a7a27 100644 --- a/clang/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp +++ b/clang/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp @@ -544,4 +544,28 @@ int z = 128'78; EXPECT_STREQ("#include \n", Out.data()); } +TEST(MinimizeSourceToDependencyDirectivesTest, PragmaOnce) { + SmallVector Out; + SmallVector Tokens; + + StringRef Source = R"(// comment +#pragma once +// another comment +#include +)"; + ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out, Tokens)); + EXPECT_STREQ("#pragma once\n#include \n", Out.data()); + ASSERT_EQ(Tokens.size(), 3u); + EXPECT_EQ(Tokens[0].K, + minimize_source_to_dependency_directives::pp_pragma_once); + + Source = R"(// comment + #pragma once extra tokens + // another comment + #include + )"; + ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out)); + EXPECT_STREQ("#pragma once\n#include \n", Out.data()); +} + } // end anonymous namespace -- 2.7.4