r366509 - [clang-scan-deps] Dependency directives source minimizer: handle #pragma once

Alex Lorenz via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 18 15:33:14 PDT 2019


Author: arphaman
Date: Thu Jul 18 15:33:14 2019
New Revision: 366509

URL: http://llvm.org/viewvc/llvm-project?rev=366509&view=rev
Log:
[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

Modified:
    cfe/trunk/include/clang/Lex/DependencyDirectivesSourceMinimizer.h
    cfe/trunk/lib/Lex/DependencyDirectivesSourceMinimizer.cpp
    cfe/trunk/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp

Modified: cfe/trunk/include/clang/Lex/DependencyDirectivesSourceMinimizer.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/DependencyDirectivesSourceMinimizer.h?rev=366509&r1=366508&r2=366509&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/DependencyDirectivesSourceMinimizer.h (original)
+++ cfe/trunk/include/clang/Lex/DependencyDirectivesSourceMinimizer.h Thu Jul 18 15:33:14 2019
@@ -38,6 +38,7 @@ enum TokenKind {
   pp_undef,
   pp_import,
   pp_pragma_import,
+  pp_pragma_once,
   pp_include_next,
   pp_if,
   pp_ifdef,

Modified: cfe/trunk/lib/Lex/DependencyDirectivesSourceMinimizer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/DependencyDirectivesSourceMinimizer.cpp?rev=366509&r1=366508&r2=366509&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/DependencyDirectivesSourceMinimizer.cpp (original)
+++ cfe/trunk/lib/Lex/DependencyDirectivesSourceMinimizer.cpp Thu Jul 18 15:33:14 2019
@@ -612,7 +612,21 @@ bool Minimizer::lexDefine(const char *&F
 
 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;
   }

Modified: cfe/trunk/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp?rev=366509&r1=366508&r2=366509&view=diff
==============================================================================
--- cfe/trunk/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp (original)
+++ cfe/trunk/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp Thu Jul 18 15:33:14 2019
@@ -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




More information about the cfe-commits mailing list