From dbd8209b33e6c9f151e4913a9c095d64a95439c4 Mon Sep 17 00:00:00 2001
From: Daniel Dunbar <daniel@zuster.org>
Date: Tue, 23 Mar 2010 05:09:10 +0000
Subject: [PATCH] PPCallbacks: Add hook for reaching the end of the main file,
 and fix DependencyFile to not do work in its destructor.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@99257 91177308-0d34-0410-b5e6-96231b3b80d8
---
 include/clang/Lex/PPCallbacks.h  | 11 +++++++++++
 include/clang/Lex/Preprocessor.h |  4 ++++
 lib/Frontend/DependencyFile.cpp  |  9 +++++----
 lib/Frontend/FrontendAction.cpp  |  4 ++++
 lib/Lex/PPLexerChange.cpp        |  1 +
 lib/Lex/Preprocessor.cpp         |  5 +++++
 6 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/include/clang/Lex/PPCallbacks.h b/include/clang/Lex/PPCallbacks.h
index dd24fb7d7ba..e891e94d4cb 100644
--- a/include/clang/Lex/PPCallbacks.h
+++ b/include/clang/Lex/PPCallbacks.h
@@ -44,6 +44,12 @@ public:
                            SrcMgr::CharacteristicKind FileType) {
   }
 
+
+  /// EndOfMainFile - This callback is invoked when the end of the main file is
+  /// reach, no subsequent callbacks will be made.
+  virtual void EndOfMainFile() {
+  }
+
   /// Ident - This callback is invoked when a #ident or #sccs directive is read.
   ///
   virtual void Ident(SourceLocation Loc, const std::string &str) {
@@ -90,6 +96,11 @@ public:
     Second->FileChanged(Loc, Reason, FileType);
   }
 
+  virtual void EndOfMainFile() {
+    First->EndOfMainFile();
+    Second->EndOfMainFile();
+  }
+
   virtual void Ident(SourceLocation Loc, const std::string &str) {
     First->Ident(Loc, str);
     Second->Ident(Loc, str);
diff --git a/include/clang/Lex/Preprocessor.h b/include/clang/Lex/Preprocessor.h
index 23c118d1fc6..312a760e01d 100644
--- a/include/clang/Lex/Preprocessor.h
+++ b/include/clang/Lex/Preprocessor.h
@@ -368,6 +368,10 @@ public:
   /// which implicitly adds the builtin defines etc.
   bool EnterMainSourceFile();
 
+  /// EndSourceFile - Inform the preprocessor callbacks that processing is
+  /// complete.
+  void EndSourceFile();
+
   /// EnterSourceFile - Add a source file to the top of the include stack and
   /// start lexing tokens from it instead of the current buffer.  Return true
   /// and fill in ErrorStr with the error information on failure.
diff --git a/lib/Frontend/DependencyFile.cpp b/lib/Frontend/DependencyFile.cpp
index de2b056dc7c..14aee3559c9 100644
--- a/lib/Frontend/DependencyFile.cpp
+++ b/lib/Frontend/DependencyFile.cpp
@@ -48,14 +48,15 @@ public:
       IncludeSystemHeaders(Opts.IncludeSystemHeaders),
       PhonyTarget(Opts.UsePhonyTargets) {}
 
-  ~DependencyFileCallback() {
+  virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
+                           SrcMgr::CharacteristicKind FileType);
+
+  virtual void EndOfMainFile() {
     OutputDependencyFile();
     OS->flush();
     delete OS;
+    OS = 0;
   }
-
-  virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
-                           SrcMgr::CharacteristicKind FileType);
 };
 }
 
diff --git a/lib/Frontend/FrontendAction.cpp b/lib/Frontend/FrontendAction.cpp
index 66df7a61917..110612d03b5 100644
--- a/lib/Frontend/FrontendAction.cpp
+++ b/lib/Frontend/FrontendAction.cpp
@@ -169,6 +169,10 @@ void FrontendAction::EndSourceFile() {
       CI.setASTContext(0);
   }
 
+  // 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();
diff --git a/lib/Lex/PPLexerChange.cpp b/lib/Lex/PPLexerChange.cpp
index 6d1c132fc0a..335d3db627d 100644
--- a/lib/Lex/PPLexerChange.cpp
+++ b/lib/Lex/PPLexerChange.cpp
@@ -255,6 +255,7 @@ bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
       if (!I->second->isUsed())
         Diag(I->second->getDefinitionLoc(), diag::pp_macro_not_used);
   }
+
   return true;
 }
 
diff --git a/lib/Lex/Preprocessor.cpp b/lib/Lex/Preprocessor.cpp
index 9d59300d213..8b4b1ddf414 100644
--- a/lib/Lex/Preprocessor.cpp
+++ b/lib/Lex/Preprocessor.cpp
@@ -519,6 +519,11 @@ bool Preprocessor::EnterMainSourceFile() {
   return EnterSourceFile(FID, 0, ErrorStr);
 }
 
+void Preprocessor::EndSourceFile() {
+  // Notify the client that we reached the end of the source file.
+  if (Callbacks)
+    Callbacks->EndOfMainFile();
+}
 
 //===----------------------------------------------------------------------===//
 // Lexer Event Handling.
-- 
GitLab