[cfe-commits] r142104 - in /cfe/trunk: include/clang/Frontend/TextDiagnostic.h include/clang/Frontend/TextDiagnosticPrinter.h lib/Frontend/TextDiagnostic.cpp lib/Frontend/TextDiagnosticPrinter.cpp

Chandler Carruth chandlerc at gmail.com
Sat Oct 15 19:57:39 PDT 2011


Author: chandlerc
Date: Sat Oct 15 21:57:39 2011
New Revision: 142104

URL: http://llvm.org/viewvc/llvm-project?rev=142104&view=rev
Log:
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.

Modified:
    cfe/trunk/include/clang/Frontend/TextDiagnostic.h
    cfe/trunk/include/clang/Frontend/TextDiagnosticPrinter.h
    cfe/trunk/lib/Frontend/TextDiagnostic.cpp
    cfe/trunk/lib/Frontend/TextDiagnosticPrinter.cpp

Modified: cfe/trunk/include/clang/Frontend/TextDiagnostic.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/TextDiagnostic.h?rev=142104&r1=142103&r2=142104&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/TextDiagnostic.h (original)
+++ cfe/trunk/include/clang/Frontend/TextDiagnostic.h Sat Oct 15 21:57:39 2011
@@ -70,20 +70,7 @@
   TextDiagnostic(raw_ostream &OS,
                  const SourceManager &SM,
                  const LangOptions &LangOpts,
-                 const DiagnosticOptions &DiagOpts,
-                 FullSourceLoc LastLoc = FullSourceLoc(),
-                 FullSourceLoc LastIncludeLoc = FullSourceLoc(),
-                 DiagnosticsEngine::Level LastLevel
-                   = DiagnosticsEngine::Level());
-
-  /// \brief Get the last diagnostic location emitted.
-  SourceLocation getLastLoc() const { return LastLoc; }
-
-  /// \brief Get the last emitted include stack location.
-  SourceLocation getLastIncludeLoc() const { return LastIncludeLoc; }
-
-  /// \brief Get the last diagnostic level.
-  DiagnosticsEngine::Level getLastLevel() const { return LastLevel; }
+                 const DiagnosticOptions &DiagOpts);
 
   void emitDiagnostic(SourceLocation Loc, DiagnosticsEngine::Level Level,
                       StringRef Message, ArrayRef<CharSourceRange> Ranges,

Modified: cfe/trunk/include/clang/Frontend/TextDiagnosticPrinter.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/TextDiagnosticPrinter.h?rev=142104&r1=142103&r2=142104&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/TextDiagnosticPrinter.h (original)
+++ cfe/trunk/include/clang/Frontend/TextDiagnosticPrinter.h Sat Oct 15 21:57:39 2011
@@ -16,25 +16,28 @@
 #define LLVM_CLANG_FRONTEND_TEXT_DIAGNOSTIC_PRINTER_H_
 
 #include "clang/Basic/Diagnostic.h"
-#include "clang/Basic/SourceLocation.h"
+#include "clang/Basic/LLVM.h"
+#include "llvm/ADT/OwningPtr.h"
 
 namespace clang {
 class DiagnosticOptions;
 class LangOptions;
+class TextDiagnostic;
 
 class TextDiagnosticPrinter : public DiagnosticConsumer {
   raw_ostream &OS;
   const LangOptions *LangOpts;
   const DiagnosticOptions *DiagOpts;
+  const SourceManager *SM;
 
-  FullSourceLoc LastLoc;
-  FullSourceLoc LastIncludeLoc;
-  DiagnosticsEngine::Level LastLevel;
-  unsigned OwnsOutputStream : 1;
+  /// \brief Handle to the currently active text diagnostic emitter.
+  llvm::OwningPtr<TextDiagnostic> TextDiag;
 
   /// A string to prefix to error messages.
   std::string Prefix;
 
+  unsigned OwnsOutputStream : 1;
+
 public:
   TextDiagnosticPrinter(raw_ostream &os, const DiagnosticOptions &diags,
                         bool OwnsOutputStream = false);
@@ -45,17 +48,9 @@
   /// used.
   void setPrefix(std::string Value) { Prefix = Value; }
 
-  void BeginSourceFile(const LangOptions &LO, const Preprocessor *PP) {
-    LangOpts = &LO;
-  }
-
-  void EndSourceFile() {
-    LangOpts = 0;
-  }
-
-  virtual void HandleDiagnostic(DiagnosticsEngine::Level Level,
-                                const Diagnostic &Info);
-
+  void BeginSourceFile(const LangOptions &LO, const Preprocessor *PP);
+  void EndSourceFile();
+  void HandleDiagnostic(DiagnosticsEngine::Level Level, const Diagnostic &Info);
   DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const;
 };
 

Modified: cfe/trunk/lib/Frontend/TextDiagnostic.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/TextDiagnostic.cpp?rev=142104&r1=142103&r2=142104&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/TextDiagnostic.cpp (original)
+++ cfe/trunk/lib/Frontend/TextDiagnostic.cpp Sat Oct 15 21:57:39 2011
@@ -392,17 +392,9 @@
 TextDiagnostic::TextDiagnostic(raw_ostream &OS,
                                const SourceManager &SM,
                                const LangOptions &LangOpts,
-                               const DiagnosticOptions &DiagOpts,
-                               FullSourceLoc LastLoc,
-                               FullSourceLoc LastIncludeLoc,
-                               DiagnosticsEngine::Level LastLevel)
-  : OS(OS), SM(SM), LangOpts(LangOpts), DiagOpts(DiagOpts),
-    LastLoc(LastLoc), LastIncludeLoc(LastIncludeLoc), LastLevel(LastLevel) {
-  if (LastLoc.isValid() && &SM != &LastLoc.getManager())
-    this->LastLoc = SourceLocation();
-  if (LastIncludeLoc.isValid() && &SM != &LastIncludeLoc.getManager())
-    this->LastIncludeLoc = SourceLocation();
-    }
+                               const DiagnosticOptions &DiagOpts)
+  : OS(OS), SM(SM), LangOpts(LangOpts), DiagOpts(DiagOpts), LastLevel() {
+}
 
 void TextDiagnostic::emitDiagnostic(SourceLocation Loc,
                                     DiagnosticsEngine::Level Level,

Modified: cfe/trunk/lib/Frontend/TextDiagnosticPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/TextDiagnosticPrinter.cpp?rev=142104&r1=142103&r2=142104&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/TextDiagnosticPrinter.cpp (original)
+++ cfe/trunk/lib/Frontend/TextDiagnosticPrinter.cpp Sat Oct 15 21:57:39 2011
@@ -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()));
-
-  // 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();
+
+  // 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));
+  }
+
+  TextDiag->emitDiagnostic(Info.getLocation(), Level, DiagMessageStream.str(),
+                           Info.getRanges(),
+                           llvm::makeArrayRef(Info.getFixItHints(),
+                                              Info.getNumFixItHints()));
 
   OS.flush();
 }





More information about the cfe-commits mailing list