[patch] Let FrontendAction::BeginSourceFile() failure cleanup code reuse EndSourceFile() cleanup code

Nico Weber thakis at chromium.org
Wed Apr 23 16:00:33 PDT 2014


Hi,

I noticed that the end of FrontendAction::BeginSourceFile() contains some
code that's partially copied from FrontendAction::EndSourceFile(). This
patch moves the shared code into a separate function.

I believe this doesn't change behavior, but I'm not very familiar with this
code.

Nico
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140423/4f602086/attachment.html>
-------------- next part --------------
Index: include/clang/Frontend/FrontendAction.h
===================================================================
--- include/clang/Frontend/FrontendAction.h	(revision 207012)
+++ include/clang/Frontend/FrontendAction.h	(working copy)
@@ -44,6 +44,8 @@
   ASTConsumer* CreateWrappedASTConsumer(CompilerInstance &CI,
                                         StringRef InFile);
 
+  void CleanUpSourceFile(bool HasBegunSourceFile, bool Failure);
+
 protected:
   /// @name Implementation Action Interface
   /// @{
Index: lib/Frontend/FrontendAction.cpp
===================================================================
--- lib/Frontend/FrontendAction.cpp	(revision 207012)
+++ lib/Frontend/FrontendAction.cpp	(working copy)
@@ -363,18 +363,7 @@
   // If we failed, reset state since the client will not end up calling the
   // matching EndSourceFile().
   failure:
-  if (isCurrentFileAST()) {
-    CI.setASTContext(0);
-    CI.setPreprocessor(0);
-    CI.setSourceManager(0);
-    CI.setFileManager(0);
-  }
-
-  if (HasBegunSourceFile)
-    CI.getDiagnosticClient().EndSourceFile();
-  CI.clearOutputFiles(/*EraseFiles=*/true);
-  setCurrentInput(FrontendInputFile());
-  setCompilerInstance(0);
+  CleanUpSourceFile(HasBegunSourceFile, /*Failure=*/true);
   return false;
 }
 
@@ -400,13 +389,19 @@
 }
 
 void FrontendAction::EndSourceFile() {
+  CleanUpSourceFile(/*HasBegunSourceFile=*/true, /*Failure=*/false);
+}
+
+void FrontendAction::CleanUpSourceFile(bool HasBegunSourceFile, bool Failure) {
   CompilerInstance &CI = getCompilerInstance();
 
   // Inform the diagnostic client we are done with this source file.
-  CI.getDiagnosticClient().EndSourceFile();
+  if (HasBegunSourceFile)
+    CI.getDiagnosticClient().EndSourceFile();
 
   // Finalize the action.
-  EndSourceFileAction();
+  if (!Failure)
+    EndSourceFileAction();
 
   // Release the consumer and the AST, in that order since the consumer may
   // perform actions in its destructor which require the context.
@@ -427,10 +422,10 @@
   }
 
   // Inform the preprocessor we are done.
-  if (CI.hasPreprocessor())
+  if (!Failure && CI.hasPreprocessor())
     CI.getPreprocessor().EndSourceFile();
 
-  if (CI.getFrontendOpts().ShowStats) {
+  if (!Failure && CI.getFrontendOpts().ShowStats) {
     llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
     CI.getPreprocessor().PrintStats();
     CI.getPreprocessor().getIdentifierTable().PrintStats();
@@ -441,7 +436,7 @@
 
   // Cleanup the output streams, and erase the output files if instructed by the
   // FrontendAction.
-  CI.clearOutputFiles(/*EraseFiles=*/shouldEraseOutputFiles());
+  CI.clearOutputFiles(/*EraseFiles=*/shouldEraseOutputFiles() || Failure);
 
   if (isCurrentFileAST()) {
     CI.takeSema();


More information about the cfe-commits mailing list