]> granicus.if.org Git - clang/commitdiff
[clang-scan-deps] Dependency directives source minimizer: handle #pragma once
authorAlex Lorenz <arphaman@gmail.com>
Thu, 18 Jul 2019 22:33:14 +0000 (22:33 +0000)
committerAlex Lorenz <arphaman@gmail.com>
Thu, 18 Jul 2019 22:33:14 +0000 (22:33 +0000)
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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@366509 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Lex/DependencyDirectivesSourceMinimizer.h
lib/Lex/DependencyDirectivesSourceMinimizer.cpp
unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp

index 41641078afe4333201ec389cc4dc69263f2c6fcd..39ea39600222b4deabaa5376101149c995a313c1 100644 (file)
@@ -38,6 +38,7 @@ enum TokenKind {
   pp_undef,
   pp_import,
   pp_pragma_import,
+  pp_pragma_once,
   pp_include_next,
   pp_if,
   pp_ifdef,
index cfc37c5d3c62bd910df78a6799b8b97c4f712e53..2e8c5f3a51a08fda50caa4a517eee0eb21c8a1e1 100644 (file)
@@ -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;
   }
index cfa3ec91e03df25aa68fa3ebf328fbd40fc977ac..38a7a27e8aa5e0f2a02ff65ad04f1840af922e1b 100644 (file)
@@ -544,4 +544,28 @@ int z = 128'78;
   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);
+
+  Source = R"(// comment
+    #pragma once extra tokens
+    // another comment
+    #include <test.h>
+    )";
+  ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
+  EXPECT_STREQ("#pragma once\n#include <test.h>\n", Out.data());
+}
+
 } // end anonymous namespace