]> granicus.if.org Git - clang/commitdiff
Flip the order the preprocessor and frontendaction are informed of the end of a file.
authorBenjamin Kramer <benny.kra@googlemail.com>
Thu, 7 Aug 2014 20:51:16 +0000 (20:51 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Thu, 7 Aug 2014 20:51:16 +0000 (20:51 +0000)
This allows using EndOfMainFile from a PPCallback to access data from the
action. The pattern of PPCallback referencing an action is common in clang-tidy.

Differential Revision: http://reviews.llvm.org/D4773

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

lib/Frontend/FrontendAction.cpp
unittests/Frontend/FrontendActionTest.cpp

index 791017924d69e26f0da9f02f46acca121b52a1e2..cf13d7b9ba8a9b41d7b919c225b0df7e3cb1701a 100644 (file)
@@ -432,6 +432,10 @@ void FrontendAction::EndSourceFile() {
   // Inform the diagnostic client we are done with this source file.
   CI.getDiagnosticClient().EndSourceFile();
 
+  // Inform the preprocessor we are done.
+  if (CI.hasPreprocessor())
+    CI.getPreprocessor().EndSourceFile();
+
   // Finalize the action.
   EndSourceFileAction();
 
@@ -453,10 +457,6 @@ void FrontendAction::EndSourceFile() {
     CI.setASTConsumer(nullptr);
   }
 
-  // Inform the preprocessor we are done.
-  if (CI.hasPreprocessor())
-    CI.getPreprocessor().EndSourceFile();
-
   if (CI.getFrontendOpts().ShowStats) {
     llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
     CI.getPreprocessor().PrintStats();
index e39d00f6af3de2a18ea57e27feab1277cf2b320d..44843eed1379c1bdd7d22429abb17d90b8937013 100644 (file)
@@ -100,4 +100,50 @@ TEST(ASTFrontendAction, IncrementalParsing) {
   EXPECT_EQ("x", test_action.decl_names[1]);
 }
 
+struct TestPPCallbacks : public PPCallbacks {
+  TestPPCallbacks() : SeenEnd(false) {}
+
+  void EndOfMainFile() override { SeenEnd = true; }
+
+  bool SeenEnd;
+};
+
+class TestPPCallbacksFrontendAction : public PreprocessorFrontendAction {
+  TestPPCallbacks *Callbacks;
+
+public:
+  TestPPCallbacksFrontendAction(TestPPCallbacks *C)
+      : Callbacks(C), SeenEnd(false) {}
+
+  void ExecuteAction() override {
+    Preprocessor &PP = getCompilerInstance().getPreprocessor();
+    PP.addPPCallbacks(Callbacks);
+    PP.EnterMainSourceFile();
+  }
+  void EndSourceFileAction() override { SeenEnd = Callbacks->SeenEnd; }
+
+  bool SeenEnd;
+};
+
+TEST(PreprocessorFrontendAction, EndSourceFile) {
+  CompilerInvocation *Invocation = new CompilerInvocation;
+  Invocation->getPreprocessorOpts().addRemappedFile(
+      "test.cc", MemoryBuffer::getMemBuffer("int main() { float x; }"));
+  Invocation->getFrontendOpts().Inputs.push_back(
+      FrontendInputFile("test.cc", IK_CXX));
+  Invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
+  Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
+  CompilerInstance Compiler;
+  Compiler.setInvocation(Invocation);
+  Compiler.createDiagnostics();
+
+  TestPPCallbacks *Callbacks = new TestPPCallbacks;
+  TestPPCallbacksFrontendAction TestAction(Callbacks);
+  ASSERT_FALSE(Callbacks->SeenEnd);
+  ASSERT_FALSE(TestAction.SeenEnd);
+  ASSERT_TRUE(Compiler.ExecuteAction(TestAction));
+  // Check that EndOfMainFile was called before EndSourceFileAction.
+  ASSERT_TRUE(TestAction.SeenEnd);
+}
+
 } // anonymous namespace