[cfe-commits] r140743 - in /cfe/trunk: include/clang/Basic/ include/clang/Frontend/ include/clang/Rewrite/ lib/ARCMigrate/ lib/Frontend/ lib/Rewrite/

Douglas Gregor dgregor at apple.com
Wed Sep 28 17:38:02 PDT 2011


Author: dgregor
Date: Wed Sep 28 19:38:00 2011
New Revision: 140743

URL: http://llvm.org/viewvc/llvm-project?rev=140743&view=rev
Log:
Introduce a pure virtual clone() method to DiagnosticConsumer, so that
we have the ability to create a new, distict diagnostic consumer when
we go off and build a module. This avoids the currently horribleness
where the same diagnostic consumer sees diagnostics for multiple
translation units (and multiple SourceManagers!) causing all sorts of havok.

Modified:
    cfe/trunk/include/clang/Basic/Diagnostic.h
    cfe/trunk/include/clang/Frontend/ChainedDiagnosticConsumer.h
    cfe/trunk/include/clang/Frontend/CompilerInstance.h
    cfe/trunk/include/clang/Frontend/LogDiagnosticPrinter.h
    cfe/trunk/include/clang/Frontend/TextDiagnosticBuffer.h
    cfe/trunk/include/clang/Frontend/TextDiagnosticPrinter.h
    cfe/trunk/include/clang/Frontend/VerifyDiagnosticConsumer.h
    cfe/trunk/include/clang/Rewrite/FixItRewriter.h
    cfe/trunk/include/clang/Rewrite/Rewriter.h
    cfe/trunk/lib/ARCMigrate/ARCMT.cpp
    cfe/trunk/lib/Frontend/ASTUnit.cpp
    cfe/trunk/lib/Frontend/CompilerInstance.cpp
    cfe/trunk/lib/Frontend/LogDiagnosticPrinter.cpp
    cfe/trunk/lib/Frontend/TextDiagnosticBuffer.cpp
    cfe/trunk/lib/Frontend/TextDiagnosticPrinter.cpp
    cfe/trunk/lib/Frontend/VerifyDiagnosticConsumer.cpp
    cfe/trunk/lib/Rewrite/FixItRewriter.cpp
    cfe/trunk/lib/Rewrite/HTMLRewrite.cpp

Modified: cfe/trunk/include/clang/Basic/Diagnostic.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/Diagnostic.h?rev=140743&r1=140742&r2=140743&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/Diagnostic.h (original)
+++ cfe/trunk/include/clang/Basic/Diagnostic.h Wed Sep 28 19:38:00 2011
@@ -1072,6 +1072,22 @@
   /// and errors.
   virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
                                 const Diagnostic &Info);
+  
+  /// \brief Clone the diagnostic consumer, producing an equivalent consumer
+  /// that can be used in a different context.
+  virtual DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const = 0;
+};
+
+/// IgnoringDiagConsumer - This is a diagnostic client that just ignores all
+/// diags.
+class IgnoringDiagConsumer : public DiagnosticConsumer {
+  void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
+                        const Diagnostic &Info) {
+    // Just ignore it.
+  }
+  DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
+    return new IgnoringDiagConsumer();
+  }
 };
 
 }  // end namespace clang

Modified: cfe/trunk/include/clang/Frontend/ChainedDiagnosticConsumer.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/ChainedDiagnosticConsumer.h?rev=140743&r1=140742&r2=140743&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/ChainedDiagnosticConsumer.h (original)
+++ cfe/trunk/include/clang/Frontend/ChainedDiagnosticConsumer.h Wed Sep 28 19:38:00 2011
@@ -54,6 +54,12 @@
     Primary->HandleDiagnostic(DiagLevel, Info);
     Secondary->HandleDiagnostic(DiagLevel, Info);
   }
+  
+  DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
+    return new ChainedDiagnosticConsumer(Primary->clone(Diags), 
+                                         Secondary->clone(Diags));
+  }
+
 };
 
 } // end namspace clang

Modified: cfe/trunk/include/clang/Frontend/CompilerInstance.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/CompilerInstance.h?rev=140743&r1=140742&r2=140743&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/CompilerInstance.h (original)
+++ cfe/trunk/include/clang/Frontend/CompilerInstance.h Wed Sep 28 19:38:00 2011
@@ -460,9 +460,13 @@
   ///
   /// \param ShouldOwnClient If Client is non-NULL, specifies whether 
   /// the diagnostic object should take ownership of the client.
+  ///
+  /// \param ShouldCloneClient If Client is non-NULL, specifies whether that
+  /// client should be cloned.
   void createDiagnostics(int Argc, const char* const *Argv,
                          DiagnosticConsumer *Client = 0,
-                         bool ShouldOwnClient = true);
+                         bool ShouldOwnClient = true,
+                         bool ShouldCloneClient = true);
 
   /// Create a DiagnosticsEngine object with a the TextDiagnosticPrinter.
   ///
@@ -492,6 +496,7 @@
                     const char* const *Argv,
                     DiagnosticConsumer *Client = 0,
                     bool ShouldOwnClient = true,
+                    bool ShouldCloneClient = true,
                     const CodeGenOptions *CodeGenOpts = 0);
 
   /// Create the file manager and replace any existing one with it.

Modified: cfe/trunk/include/clang/Frontend/LogDiagnosticPrinter.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/LogDiagnosticPrinter.h?rev=140743&r1=140742&r2=140743&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/LogDiagnosticPrinter.h (original)
+++ cfe/trunk/include/clang/Frontend/LogDiagnosticPrinter.h Wed Sep 28 19:38:00 2011
@@ -70,6 +70,8 @@
 
   virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
                                 const Diagnostic &Info);
+  
+  DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const;
 };
 
 } // end namespace clang

Modified: cfe/trunk/include/clang/Frontend/TextDiagnosticBuffer.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/TextDiagnosticBuffer.h?rev=140743&r1=140742&r2=140743&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/TextDiagnosticBuffer.h (original)
+++ cfe/trunk/include/clang/Frontend/TextDiagnosticBuffer.h Wed Sep 28 19:38:00 2011
@@ -45,6 +45,8 @@
   /// FlushDiagnostics - Flush the buffered diagnostics to an given
   /// diagnostic engine.
   void FlushDiagnostics(DiagnosticsEngine &Diags) const;
+  
+  virtual DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const;
 };
 
 } // end namspace clang

Modified: cfe/trunk/include/clang/Frontend/TextDiagnosticPrinter.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/TextDiagnosticPrinter.h?rev=140743&r1=140742&r2=140743&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/TextDiagnosticPrinter.h (original)
+++ cfe/trunk/include/clang/Frontend/TextDiagnosticPrinter.h Wed Sep 28 19:38:00 2011
@@ -59,6 +59,8 @@
   virtual void HandleDiagnostic(DiagnosticsEngine::Level Level,
                                 const Diagnostic &Info);
 
+  DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const;
+
 private:
   void EmitDiagnosticLoc(DiagnosticsEngine::Level Level,
                          const Diagnostic &Info,

Modified: cfe/trunk/include/clang/Frontend/VerifyDiagnosticConsumer.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/VerifyDiagnosticConsumer.h?rev=140743&r1=140742&r2=140743&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/VerifyDiagnosticConsumer.h (original)
+++ cfe/trunk/include/clang/Frontend/VerifyDiagnosticConsumer.h Wed Sep 28 19:38:00 2011
@@ -88,6 +88,8 @@
 
   virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
                                 const Diagnostic &Info);
+  
+  virtual DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const;
 };
 
 } // end namspace clang

Modified: cfe/trunk/include/clang/Rewrite/FixItRewriter.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Rewrite/FixItRewriter.h?rev=140743&r1=140742&r2=140743&view=diff
==============================================================================
--- cfe/trunk/include/clang/Rewrite/FixItRewriter.h (original)
+++ cfe/trunk/include/clang/Rewrite/FixItRewriter.h Wed Sep 28 19:38:00 2011
@@ -97,6 +97,8 @@
 
   /// \brief Emit a diagnostic via the adapted diagnostic client.
   void Diag(SourceLocation Loc, unsigned DiagID);
+  
+  DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const;
 };
 
 }

Modified: cfe/trunk/include/clang/Rewrite/Rewriter.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Rewrite/Rewriter.h?rev=140743&r1=140742&r2=140743&view=diff
==============================================================================
--- cfe/trunk/include/clang/Rewrite/Rewriter.h (original)
+++ cfe/trunk/include/clang/Rewrite/Rewriter.h Wed Sep 28 19:38:00 2011
@@ -154,8 +154,8 @@
     SourceMgr = &SM;
     LangOpts = &LO;
   }
-  SourceManager &getSourceMgr() { return *SourceMgr; }
-  const LangOptions &getLangOpts() { return *LangOpts; }
+  SourceManager &getSourceMgr() const { return *SourceMgr; }
+  const LangOptions &getLangOpts() const { return *LangOpts; }
 
   /// isRewritable - Return true if this location is a raw file location, which
   /// is rewritable.  Locations from macros, etc are not rewritable.

Modified: cfe/trunk/lib/ARCMigrate/ARCMT.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ARCMigrate/ARCMT.cpp?rev=140743&r1=140742&r2=140743&view=diff
==============================================================================
--- cfe/trunk/lib/ARCMigrate/ARCMT.cpp (original)
+++ cfe/trunk/lib/ARCMigrate/ARCMT.cpp Wed Sep 28 19:38:00 2011
@@ -93,7 +93,7 @@
   CapturedDiagList &CapturedDiags;
 public:
   CaptureDiagnosticConsumer(DiagnosticsEngine &diags,
-                          CapturedDiagList &capturedDiags)
+                           CapturedDiagList &capturedDiags)
     : Diags(diags), CapturedDiags(capturedDiags) { }
 
   virtual void HandleDiagnostic(DiagnosticsEngine::Level level,
@@ -107,6 +107,12 @@
     // Non-ARC warnings are ignored.
     Diags.setLastDiagnosticIgnored();
   }
+  
+  DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
+    // Just drop any diagnostics that come from cloned consumers; they'll
+    // have different source managers anyway.
+    return new IgnoringDiagConsumer();
+  }
 };
 
 } // end anonymous namespace

Modified: cfe/trunk/lib/Frontend/ASTUnit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/ASTUnit.cpp?rev=140743&r1=140742&r2=140743&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/ASTUnit.cpp (original)
+++ cfe/trunk/lib/Frontend/ASTUnit.cpp Wed Sep 28 19:38:00 2011
@@ -458,6 +458,12 @@
   
   virtual void HandleDiagnostic(DiagnosticsEngine::Level Level,
                                 const Diagnostic &Info);
+  
+  DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
+    // Just drop any diagnostics that come from cloned consumers; they'll
+    // have different source managers anyway.
+    return new IgnoringDiagConsumer();
+  }
 };
 
 /// \brief RAII object that optionally captures diagnostics, if

Modified: cfe/trunk/lib/Frontend/CompilerInstance.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInstance.cpp?rev=140743&r1=140742&r2=140743&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/CompilerInstance.cpp (original)
+++ cfe/trunk/lib/Frontend/CompilerInstance.cpp Wed Sep 28 19:38:00 2011
@@ -141,9 +141,11 @@
 
 void CompilerInstance::createDiagnostics(int Argc, const char* const *Argv,
                                          DiagnosticConsumer *Client,
-                                         bool ShouldOwnClient) {
+                                         bool ShouldOwnClient,
+                                         bool ShouldCloneClient) {
   Diagnostics = createDiagnostics(getDiagnosticOpts(), Argc, Argv, Client,
-                                  ShouldOwnClient, &getCodeGenOpts());
+                                  ShouldOwnClient, ShouldCloneClient,
+                                  &getCodeGenOpts());
 }
 
 llvm::IntrusiveRefCntPtr<DiagnosticsEngine> 
@@ -151,6 +153,7 @@
                                     int Argc, const char* const *Argv,
                                     DiagnosticConsumer *Client,
                                     bool ShouldOwnClient,
+                                    bool ShouldCloneClient,
                                     const CodeGenOptions *CodeGenOpts) {
   llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
   llvm::IntrusiveRefCntPtr<DiagnosticsEngine>
@@ -158,9 +161,12 @@
 
   // Create the diagnostic client for reporting errors or for
   // implementing -verify.
-  if (Client)
-    Diags->setClient(Client, ShouldOwnClient);
-  else
+  if (Client) {
+    if (ShouldCloneClient)
+      Diags->setClient(Client->clone(*Diags), ShouldOwnClient);
+    else
+      Diags->setClient(Client, ShouldOwnClient);
+  } else
     Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts));
 
   // Chain in -verify checker, if requested.
@@ -691,7 +697,8 @@
   Instance.setInvocation(&*Invocation);
   Instance.createDiagnostics(/*argc=*/0, /*argv=*/0, 
                              &ImportingInstance.getDiagnosticClient(),
-                             /*ShouldOwnClient=*/false);
+                             /*ShouldOwnClient=*/true,
+                             /*ShouldCloneClient=*/true);
 
   // Construct a module-generating action.
   GeneratePCHAction CreateModuleAction(true);
@@ -699,13 +706,6 @@
   // Execute the action to actually build the module in-place.
   // FIXME: Need to synchronize when multiple processes do this.
   Instance.ExecuteAction(CreateModuleAction);
-  
-  // Tell the diagnostic client that it's (re-)starting to process a source
-  // file.
-  // FIXME: This is a hack. We probably want to clone the diagnostic client.
-  ImportingInstance.getDiagnosticClient()
-    .BeginSourceFile(ImportingInstance.getLangOpts(),
-                     &ImportingInstance.getPreprocessor());
 } 
 
 ModuleKey CompilerInstance::loadModule(SourceLocation ImportLoc, 

Modified: cfe/trunk/lib/Frontend/LogDiagnosticPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/LogDiagnosticPrinter.cpp?rev=140743&r1=140742&r2=140743&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/LogDiagnosticPrinter.cpp (original)
+++ cfe/trunk/lib/Frontend/LogDiagnosticPrinter.cpp Wed Sep 28 19:38:00 2011
@@ -169,3 +169,9 @@
   // Record the diagnostic entry.
   Entries.push_back(DE);
 }
+
+DiagnosticConsumer *
+LogDiagnosticPrinter::clone(DiagnosticsEngine &Diags) const {
+  return new LogDiagnosticPrinter(OS, *DiagOpts, /*OwnsOutputStream=*/false);
+}
+

Modified: cfe/trunk/lib/Frontend/TextDiagnosticBuffer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/TextDiagnosticBuffer.cpp?rev=140743&r1=140742&r2=140743&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/TextDiagnosticBuffer.cpp (original)
+++ cfe/trunk/lib/Frontend/TextDiagnosticBuffer.cpp Wed Sep 28 19:38:00 2011
@@ -54,3 +54,7 @@
     Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Note,
                  it->second.c_str()));
 }
+
+DiagnosticConsumer *TextDiagnosticBuffer::clone(DiagnosticsEngine &) const {
+  return new TextDiagnosticBuffer();
+}

Modified: cfe/trunk/lib/Frontend/TextDiagnosticPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/TextDiagnosticPrinter.cpp?rev=140743&r1=140742&r2=140743&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/TextDiagnosticPrinter.cpp (original)
+++ cfe/trunk/lib/Frontend/TextDiagnosticPrinter.cpp Wed Sep 28 19:38:00 2011
@@ -1264,3 +1264,8 @@
 
   OS.flush();
 }
+
+DiagnosticConsumer *
+TextDiagnosticPrinter::clone(DiagnosticsEngine &Diags) const {
+  return new TextDiagnosticPrinter(OS, *DiagOpts, /*OwnsOutputStream=*/false);
+}

Modified: cfe/trunk/lib/Frontend/VerifyDiagnosticConsumer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/VerifyDiagnosticConsumer.cpp?rev=140743&r1=140742&r2=140743&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/VerifyDiagnosticConsumer.cpp (original)
+++ cfe/trunk/lib/Frontend/VerifyDiagnosticConsumer.cpp Wed Sep 28 19:38:00 2011
@@ -525,6 +525,14 @@
   Buffer.reset(new TextDiagnosticBuffer());
 }
 
+DiagnosticConsumer *
+VerifyDiagnosticConsumer::clone(DiagnosticsEngine &Diags) const {
+  if (!Diags.getClient())
+    Diags.setClient(PrimaryClient->clone(Diags));
+  
+  return new VerifyDiagnosticConsumer(Diags);
+}
+
 Directive* Directive::Create(bool RegexKind, const SourceLocation &Location,
                              const std::string &Text, unsigned Count) {
   if (RegexKind)

Modified: cfe/trunk/lib/Rewrite/FixItRewriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Rewrite/FixItRewriter.cpp?rev=140743&r1=140742&r2=140743&view=diff
==============================================================================
--- cfe/trunk/lib/Rewrite/FixItRewriter.cpp (original)
+++ cfe/trunk/lib/Rewrite/FixItRewriter.cpp Wed Sep 28 19:38:00 2011
@@ -156,4 +156,9 @@
   Diags.setClient(this);
 }
 
+DiagnosticConsumer *FixItRewriter::clone(DiagnosticsEngine &Diags) const {
+  return new FixItRewriter(Diags, Diags.getSourceManager(), 
+                           Rewrite.getLangOpts(), FixItOpts);
+}
+
 FixItOptions::~FixItOptions() {}

Modified: cfe/trunk/lib/Rewrite/HTMLRewrite.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Rewrite/HTMLRewrite.cpp?rev=140743&r1=140742&r2=140743&view=diff
==============================================================================
--- cfe/trunk/lib/Rewrite/HTMLRewrite.cpp (original)
+++ cfe/trunk/lib/Rewrite/HTMLRewrite.cpp Wed Sep 28 19:38:00 2011
@@ -440,17 +440,6 @@
   }
 }
 
-namespace {
-/// IgnoringDiagConsumer - This is a diagnostic client that just ignores all
-/// diags.
-class IgnoringDiagConsumer : public DiagnosticConsumer {
-  void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
-                        const Diagnostic &Info) {
-    // Just ignore it.
-  }
-};
-}
-
 /// HighlightMacros - This uses the macro table state from the end of the
 /// file, to re-expand macros and insert (into the HTML) information about the
 /// macro expansions.  This won't be perfectly perfect, but it will be





More information about the cfe-commits mailing list