]> granicus.if.org Git - clang/commitdiff
Introduce a pure virtual clone() method to DiagnosticConsumer, so that
authorDouglas Gregor <dgregor@apple.com>
Thu, 29 Sep 2011 00:38:00 +0000 (00:38 +0000)
committerDouglas Gregor <dgregor@apple.com>
Thu, 29 Sep 2011 00:38:00 +0000 (00:38 +0000)
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.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@140743 91177308-0d34-0410-b5e6-96231b3b80d8

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

index a1a3d44d44a6bb015f757264474c598553e7d14a..d9b447947ea0f5b6dd551ee393478a9fad12b342 100644 (file)
@@ -1072,6 +1072,22 @@ public:
   /// 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
index 125a9b2bf747084a1d9effcbadb3f71c2ed11234..f20cf6fcf4f2fc7b199f4eaae40e1b38e59159d1 100644 (file)
@@ -54,6 +54,12 @@ public:
     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
index d44f507d02cc143b575ba08866db6477e652bafc..881774022d12b7be981f678d8c052182beee8c72 100644 (file)
@@ -460,9 +460,13 @@ public:
   ///
   /// \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 @@ public:
                     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.
index e12752b4f82b62ac0e0659970775af032f0a7919..4de15f2aed5ff8e2902882d99150a4e268557224 100644 (file)
@@ -70,6 +70,8 @@ public:
 
   virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
                                 const Diagnostic &Info);
+  
+  DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const;
 };
 
 } // end namespace clang
index 93ac299da30e475f324cd4ec64b673822811ecec..6f1c0e8aeaf7ec8c5ad8540ee58e83e952bc29e7 100644 (file)
@@ -45,6 +45,8 @@ public:
   /// FlushDiagnostics - Flush the buffered diagnostics to an given
   /// diagnostic engine.
   void FlushDiagnostics(DiagnosticsEngine &Diags) const;
+  
+  virtual DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const;
 };
 
 } // end namspace clang
index f3dec55f10723762025fa03a95a36ca9c704ea53..22fa18b050275317b349f88947e1407969de7d0c 100644 (file)
@@ -59,6 +59,8 @@ public:
   virtual void HandleDiagnostic(DiagnosticsEngine::Level Level,
                                 const Diagnostic &Info);
 
+  DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const;
+
 private:
   void EmitDiagnosticLoc(DiagnosticsEngine::Level Level,
                          const Diagnostic &Info,
index a74c80910955d326a62d474ea0d07545a5fa15a5..28dc9de03af0fc11d784a55caf4979e2cf918354 100644 (file)
@@ -88,6 +88,8 @@ public:
 
   virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
                                 const Diagnostic &Info);
+  
+  virtual DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const;
 };
 
 } // end namspace clang
index d98058134c9942517796e45cfbfa033b3b555043..bf7e7911c97719582e370634e4988b37d4bbdb70 100644 (file)
@@ -97,6 +97,8 @@ public:
 
   /// \brief Emit a diagnostic via the adapted diagnostic client.
   void Diag(SourceLocation Loc, unsigned DiagID);
+  
+  DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const;
 };
 
 }
index ad463790209ec50ce4216993d112587548993226..f1358a0c85340486e76b097f9fcd740ae1b5b356 100644 (file)
@@ -154,8 +154,8 @@ public:
     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.
index 05bc150487fdf3cdad88faa809aee04864a650b0..6e1b0e535bbbf0314e9950b614d6b39e5c044ee8 100644 (file)
@@ -93,7 +93,7 @@ class CaptureDiagnosticConsumer : public DiagnosticConsumer {
   CapturedDiagList &CapturedDiags;
 public:
   CaptureDiagnosticConsumer(DiagnosticsEngine &diags,
-                          CapturedDiagList &capturedDiags)
+                           CapturedDiagList &capturedDiags)
     : Diags(diags), CapturedDiags(capturedDiags) { }
 
   virtual void HandleDiagnostic(DiagnosticsEngine::Level level,
@@ -107,6 +107,12 @@ public:
     // 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
index 4e8226d65112dba6bffa91ef98aafee66084a135..e84f8bd6e29f0eaf2cb0c8652cf4b5e10eeb8d76 100644 (file)
@@ -458,6 +458,12 @@ public:
   
   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
index ce5496562b1225083e9fb5081266ebb3fd5f7c10..554ca352e1cc874ff796197fd5a2e4cb237099b8 100644 (file)
@@ -141,9 +141,11 @@ static void SetUpDiagnosticLog(const DiagnosticOptions &DiagOpts,
 
 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 @@ CompilerInstance::createDiagnostics(const DiagnosticOptions &Opts,
                                     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 @@ CompilerInstance::createDiagnostics(const DiagnosticOptions &Opts,
 
   // 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 @@ static void compileModule(CompilerInstance &ImportingInstance,
   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 @@ static void compileModule(CompilerInstance &ImportingInstance,
   // 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, 
index f295acfff325f242cf82ab4e6d436271a9d13d27..8b585be90ec431da9feccc6f22db167084d452ac 100644 (file)
@@ -169,3 +169,9 @@ void LogDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,
   // Record the diagnostic entry.
   Entries.push_back(DE);
 }
+
+DiagnosticConsumer *
+LogDiagnosticPrinter::clone(DiagnosticsEngine &Diags) const {
+  return new LogDiagnosticPrinter(OS, *DiagOpts, /*OwnsOutputStream=*/false);
+}
+
index cef46a27e8a47cf9289c3ba81ebb063615073101..f8ea9f1361ab1290a5bf7139823e6518ab156610 100644 (file)
@@ -54,3 +54,7 @@ void TextDiagnosticBuffer::FlushDiagnostics(DiagnosticsEngine &Diags) const {
     Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Note,
                  it->second.c_str()));
 }
+
+DiagnosticConsumer *TextDiagnosticBuffer::clone(DiagnosticsEngine &) const {
+  return new TextDiagnosticBuffer();
+}
index 64a5c1bc9cfc54c57d86e7c2161baabd7c291a06..378e673812dafe25c1048595a8e07355a7bdf282 100644 (file)
@@ -1264,3 +1264,8 @@ void TextDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,
 
   OS.flush();
 }
+
+DiagnosticConsumer *
+TextDiagnosticPrinter::clone(DiagnosticsEngine &Diags) const {
+  return new TextDiagnosticPrinter(OS, *DiagOpts, /*OwnsOutputStream=*/false);
+}
index 983dc8eebb4de937f5de55cdbd4ea5a6d49cec57..cf35c8edc310974e097fadc92083370985774ac1 100644 (file)
@@ -525,6 +525,14 @@ void VerifyDiagnosticConsumer::CheckDiagnostics() {
   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)
index d38179dabfcd68bb854842970b46f4ac9b890aae..632c0de0742a5db9801779d0fa8c0ffd12ebdee1 100644 (file)
@@ -156,4 +156,9 @@ void FixItRewriter::Diag(SourceLocation Loc, unsigned DiagID) {
   Diags.setClient(this);
 }
 
+DiagnosticConsumer *FixItRewriter::clone(DiagnosticsEngine &Diags) const {
+  return new FixItRewriter(Diags, Diags.getSourceManager(), 
+                           Rewrite.getLangOpts(), FixItOpts);
+}
+
 FixItOptions::~FixItOptions() {}
index dcf52f9e3cbd3ea76bd03ed7e7bc2c2098e35dec..ba39602d162b5d15099905789ec49df51a5f5359 100644 (file)
@@ -440,17 +440,6 @@ void html::SyntaxHighlight(Rewriter &R, FileID FID, const Preprocessor &PP) {
   }
 }
 
-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