[PATCH] Flip the order the preprocessor and frontendaction are informed of the end of a file.

Benjamin Kramer benny.kra at gmail.com
Thu Aug 7 14:00:25 PDT 2014


Closed by commit rL215145 (authored by d0k).

REPOSITORY
  rL LLVM

http://reviews.llvm.org/D4773

Files:
  cfe/trunk/lib/Frontend/FrontendAction.cpp
  cfe/trunk/unittests/Frontend/FrontendActionTest.cpp

Index: cfe/trunk/unittests/Frontend/FrontendActionTest.cpp
===================================================================
--- cfe/trunk/unittests/Frontend/FrontendActionTest.cpp
+++ cfe/trunk/unittests/Frontend/FrontendActionTest.cpp
@@ -100,4 +100,50 @@
   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
Index: cfe/trunk/lib/Frontend/FrontendAction.cpp
===================================================================
--- cfe/trunk/lib/Frontend/FrontendAction.cpp
+++ cfe/trunk/lib/Frontend/FrontendAction.cpp
@@ -432,6 +432,10 @@
   // 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 @@
     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();
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D4773.12290.patch
Type: text/x-patch
Size: 2766 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140807/1cb97bf7/attachment.bin>


More information about the cfe-commits mailing list