Persist the TextDiagnostic object across multiple diagnostics as long as
the SourceManager doesn't change, and the source files don't change.
This greatly simplifies the interfaces and interactions. The lifetime of
the TextDiagnostic object forms the 'session' over which we attempt to
condense and deduplicate information in diagnostics.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@142104 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Frontend/TextDiagnosticPrinter.cpp b/lib/Frontend/TextDiagnosticPrinter.cpp
index cf28264..b5d0bcb 100644
--- a/lib/Frontend/TextDiagnosticPrinter.cpp
+++ b/lib/Frontend/TextDiagnosticPrinter.cpp
@@ -27,7 +27,7 @@
 TextDiagnosticPrinter::TextDiagnosticPrinter(raw_ostream &os,
                                              const DiagnosticOptions &diags,
                                              bool _OwnsOutputStream)
-  : OS(os), LangOpts(0), DiagOpts(&diags), LastLevel(),
+  : OS(os), LangOpts(0), DiagOpts(&diags), SM(0),
     OwnsOutputStream(_OwnsOutputStream) {
 }
 
@@ -36,6 +36,16 @@
     delete &OS;
 }
 
+void TextDiagnosticPrinter::BeginSourceFile(const LangOptions &LO,
+                                            const Preprocessor *PP) {
+  LangOpts = &LO;
+}
+
+void TextDiagnosticPrinter::EndSourceFile() {
+  LangOpts = 0;
+  TextDiag.reset(0);
+}
+
 /// \brief Print the diagnostic name to a raw_ostream.
 ///
 /// This prints the diagnostic name to a raw_ostream if it has one. It formats
@@ -158,23 +168,18 @@
   assert(DiagOpts && "Unexpected diagnostic without options set");
   assert(Info.hasSourceManager() &&
          "Unexpected diagnostic with no source manager");
-  const SourceManager &SM = Info.getSourceManager();
-  TextDiagnostic TextDiag(OS, SM, *LangOpts, *DiagOpts,
-                          LastLoc, LastIncludeLoc, LastLevel);
 
-  TextDiag.emitDiagnostic(Info.getLocation(), Level, DiagMessageStream.str(),
-                          Info.getRanges(),
-                          llvm::makeArrayRef(Info.getFixItHints(),
-                                             Info.getNumFixItHints()));
+  // Rebuild the TextDiagnostic utility if missing or the source manager has
+  // changed.
+  if (!TextDiag || SM != &Info.getSourceManager()) {
+    SM = &Info.getSourceManager();
+    TextDiag.reset(new TextDiagnostic(OS, *SM, *LangOpts, *DiagOpts));
+  }
 
-  // Cache the LastLoc from the TextDiagnostic printing.
-  // FIXME: Rather than this, we should persist a TextDiagnostic object across
-  // diagnostics until the SourceManager changes. That will allow the
-  // TextDiagnostic object to form a 'session' of output where we can
-  // reasonably collapse redundant information.
-  LastLoc = FullSourceLoc(TextDiag.getLastLoc(), SM);
-  LastIncludeLoc = FullSourceLoc(TextDiag.getLastIncludeLoc(), SM);
-  LastLevel = TextDiag.getLastLevel();
+  TextDiag->emitDiagnostic(Info.getLocation(), Level, DiagMessageStream.str(),
+                           Info.getRanges(),
+                           llvm::makeArrayRef(Info.getFixItHints(),
+                                              Info.getNumFixItHints()));
 
   OS.flush();
 }