]> granicus.if.org Git - clang/commitdiff
Make Diagnostic reference-counted, which is simpler than juggling
authorDouglas Gregor <dgregor@apple.com>
Mon, 5 Apr 2010 23:52:57 +0000 (23:52 +0000)
committerDouglas Gregor <dgregor@apple.com>
Mon, 5 Apr 2010 23:52:57 +0000 (23:52 +0000)
maybe-ownership vs. ownership.

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

include/clang/Basic/Diagnostic.h
include/clang/Frontend/ASTUnit.h
include/clang/Frontend/CompilerInstance.h
lib/Frontend/ASTMerge.cpp
lib/Frontend/ASTUnit.cpp
lib/Frontend/CompilerInstance.cpp
lib/Frontend/FrontendAction.cpp
tools/CIndex/CIndex.cpp
tools/CIndex/CIndexCodeCompletion.cpp

index 643868506d8f41676c47595b1725855e594479f4..57dd6967fc5163b0857fa0271da9c036a20a0d5b 100644 (file)
@@ -15,6 +15,7 @@
 #define LLVM_CLANG_DIAGNOSTIC_H
 
 #include "clang/Basic/SourceLocation.h"
+#include "llvm/ADT/IntrusiveRefCntPtr.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/type_traits.h"
 #include <string>
@@ -150,7 +151,7 @@ public:
 /// problems and issues.  It massages the diagnostics (e.g. handling things like
 /// "report warnings as errors" and passes them off to the DiagnosticClient for
 /// reporting to the user.
-class Diagnostic {
+class Diagnostic : public llvm::RefCountedBase<Diagnostic> {
 public:
   /// Level - The level of the diagnostic, after it has been through mapping.
   enum Level {
index 7fc3bf9b1e39894d81d846a5371f751625040e9b..9252358f42a63c1878f83e9b6ec26926cd5a0a72 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "clang/Lex/PreprocessingRecord.h"
 #include "clang/Basic/SourceManager.h"
+#include "llvm/ADT/IntrusiveRefCntPtr.h"
 #include "llvm/ADT/OwningPtr.h"
 #include "clang/Basic/FileManager.h"
 #include "clang/Index/ASTLocation.h"
@@ -52,7 +53,7 @@ public:
   typedef std::map<FileID, std::vector<PreprocessedEntity *> > 
     PreprocessedEntitiesByFileMap;
 private:
-  llvm::MaybeOwningPtr<Diagnostic>  Diagnostics;
+  llvm::IntrusiveRefCntPtr<Diagnostic> Diagnostics;
   llvm::OwningPtr<FileManager>      FileMgr;
   llvm::OwningPtr<SourceManager>    SourceMgr;
   llvm::OwningPtr<HeaderSearch>     HeaderInfo;
@@ -214,7 +215,7 @@ public:
   ///
   /// \returns - The initialized ASTUnit or null if the PCH failed to load.
   static ASTUnit *LoadFromPCHFile(const std::string &Filename,
-                                  llvm::MaybeOwningPtr<Diagnostic> Diags,
+                                  llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
                                   bool OnlyLocalDecls = false,
                                   RemappedFile *RemappedFiles = 0,
                                   unsigned NumRemappedFiles = 0,
@@ -232,7 +233,7 @@ public:
   // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
   // shouldn't need to specify them at construction time.
   static ASTUnit *LoadFromCompilerInvocation(CompilerInvocation *CI,
-                                       llvm::MaybeOwningPtr<Diagnostic> Diags,
+                                     llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
                                              bool OnlyLocalDecls = false,
                                              bool CaptureDiagnostics = false);
 
@@ -252,7 +253,7 @@ public:
   // shouldn't need to specify them at construction time.
   static ASTUnit *LoadFromCommandLine(const char **ArgBegin,
                                       const char **ArgEnd,
-                                      llvm::MaybeOwningPtr<Diagnostic> Diags,
+                                    llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
                                       llvm::StringRef ResourceFilesPath,
                                       bool OnlyLocalDecls = false,
                                       RemappedFile *RemappedFiles = 0,
@@ -260,24 +261,6 @@ public:
                                       bool CaptureDiagnostics = false);
 };
 
-/// \brief Return an potentially-owning pointer for the given diagnostic engine
-/// that owns the pointer.
-inline llvm::MaybeOwningPtr<Diagnostic> OwnedDiag(Diagnostic &Diags) {
-  return llvm::MaybeOwningPtr<Diagnostic>(&Diags, true);
-}
-
-/// \brief Return a potentially-owning pointer for the given diagnostic engine
-/// that does not own the pointer.
-inline llvm::MaybeOwningPtr<Diagnostic> UnownedDiag(Diagnostic &Diags) {
-  return llvm::MaybeOwningPtr<Diagnostic>(&Diags, false);
-}
-
-/// \brief Return an potentially-owning pointer that indicates that the
-/// default diagnostic engine should be used.
-inline llvm::MaybeOwningPtr<Diagnostic> DefaultDiag() {
-  return llvm::MaybeOwningPtr<Diagnostic>();
-}
-  
 } // namespace clang
 
 #endif
index 3444b640f01d3c501cea612ff19ab0cdb8ca3780..36720c9d1462f4e7130915131acac45fe37a7977 100644 (file)
@@ -11,6 +11,7 @@
 #define LLVM_CLANG_FRONTEND_COMPILERINSTANCE_H_
 
 #include "clang/Frontend/CompilerInvocation.h"
+#include "llvm/ADT/IntrusiveRefCntPtr.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/OwningPtr.h"
 #include <cassert>
@@ -63,7 +64,7 @@ class CompilerInstance {
   llvm::OwningPtr<CompilerInvocation> Invocation;
 
   /// The diagnostics engine instance.
-  llvm::OwningPtr<Diagnostic> Diagnostics;
+  llvm::IntrusiveRefCntPtr<Diagnostic> Diagnostics;
 
   /// The diagnostics client instance.
   llvm::OwningPtr<DiagnosticClient> DiagClient;
@@ -255,10 +256,6 @@ public:
     return *Diagnostics;
   }
 
-  /// takeDiagnostics - Remove the current diagnostics engine and give ownership
-  /// to the caller.
-  Diagnostic *takeDiagnostics() { return Diagnostics.take(); }
-
   /// setDiagnostics - Replace the current diagnostics engine; the compiler
   /// instance takes ownership of \arg Value.
   void setDiagnostics(Diagnostic *Value);
@@ -469,8 +466,8 @@ public:
   /// must extend past that of the diagnostic engine.
   ///
   /// \return The new object on success, or null on failure.
-  static Diagnostic *createDiagnostics(const DiagnosticOptions &Opts,
-                                       int Argc, char **Argv);
+  static llvm::IntrusiveRefCntPtr<Diagnostic> 
+  createDiagnostics(const DiagnosticOptions &Opts, int Argc, char **Argv);
 
   /// Create the file manager and replace any existing one with it.
   void createFileManager();
index 24658615a9aff37a6a4c3670ce6029fca78ca07c..b0faf0ae86b1a8e87dfc68ba9e0b0b736c6ff744 100644 (file)
@@ -12,6 +12,7 @@
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/ASTDiagnostic.h"
 #include "clang/AST/ASTImporter.h"
+#include "clang/Basic/Diagnostic.h"
 
 using namespace clang;
 
@@ -36,10 +37,9 @@ void ASTMergeAction::ExecuteAction() {
                                          CI.getASTContext().getLangOptions());
   CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
                                        &CI.getASTContext());
+  llvm::IntrusiveRefCntPtr<Diagnostic> Diags(&CI.getDiagnostics());
   for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
-    ASTUnit *Unit = ASTUnit::LoadFromPCHFile(ASTFiles[I], 
-                                             UnownedDiag(CI.getDiagnostics()),
-                                             false);
+    ASTUnit *Unit = ASTUnit::LoadFromPCHFile(ASTFiles[I], Diags, false);
     if (!Unit)
       continue;
 
index 2a76d6f37bc9ef1109121d3ecad8ccc45edfd430..427bd6a9b8035188f5c011cda6c37a83649ca7b5 100644 (file)
@@ -141,24 +141,22 @@ const std::string &ASTUnit::getPCHFileName() {
 }
 
 ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
-                                  llvm::MaybeOwningPtr<Diagnostic> Diags,
+                                  llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
                                   bool OnlyLocalDecls,
                                   RemappedFile *RemappedFiles,
                                   unsigned NumRemappedFiles,
                                   bool CaptureDiagnostics) {
   llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true));
   
-  if (Diags.get())
-    AST->Diagnostics = Diags;
-  else {
+  if (!Diags.getPtr()) {
     // No diagnostics engine was provided, so create our own diagnostics object
     // with the default options.
     DiagnosticOptions DiagOpts;
-    AST->Diagnostics.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0),
-                           true);
+    Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0);
   }
   
   AST->OnlyLocalDecls = OnlyLocalDecls;
+  AST->Diagnostics = Diags;
   AST->FileMgr.reset(new FileManager);
   AST->SourceMgr.reset(new SourceManager(AST->getDiagnostics()));
   AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager()));
@@ -290,7 +288,7 @@ public:
 }
 
 ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
-                                         llvm::MaybeOwningPtr<Diagnostic> Diags,
+                                   llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
                                              bool OnlyLocalDecls,
                                              bool CaptureDiagnostics) {
   // Create the compiler instance to use for building the AST.
@@ -298,16 +296,16 @@ ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
   llvm::OwningPtr<ASTUnit> AST;
   llvm::OwningPtr<TopLevelDeclTrackerAction> Act;
 
-  if (!Diags.get()) {
+  if (!Diags.getPtr()) {
     // No diagnostics engine was provided, so create our own diagnostics object
     // with the default options.
     DiagnosticOptions DiagOpts;
-    Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0), true);
+    Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0);
   }
   
   Clang.setInvocation(CI);
 
-  Clang.setDiagnostics(Diags.get());
+  Clang.setDiagnostics(Diags.getPtr());
   Clang.setDiagnosticClient(Diags->getClient());
 
   // Create the target instance.
@@ -315,7 +313,6 @@ ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
                                                Clang.getTargetOpts()));
   if (!Clang.hasTarget()) {
     Clang.takeDiagnosticClient();
-    Clang.takeDiagnostics();
     return 0;
   }
 
@@ -370,7 +367,6 @@ ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
   Act->EndSourceFile();
 
   Clang.takeDiagnosticClient();
-  Clang.takeDiagnostics();
   Clang.takeInvocation();
 
   AST->Invocation.reset(Clang.takeInvocation());
@@ -380,23 +376,22 @@ error:
   Clang.takeSourceManager();
   Clang.takeFileManager();
   Clang.takeDiagnosticClient();
-  Clang.takeDiagnostics();
   return 0;
 }
 
 ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
                                       const char **ArgEnd,
-                                      llvm::MaybeOwningPtr<Diagnostic> Diags,
+                                    llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
                                       llvm::StringRef ResourceFilesPath,
                                       bool OnlyLocalDecls,
                                       RemappedFile *RemappedFiles,
                                       unsigned NumRemappedFiles,
                                       bool CaptureDiagnostics) {
-  if (!Diags.get()) {
+  if (!Diags.getPtr()) {
     // No diagnostics engine was provided, so create our own diagnostics object
     // with the default options.
     DiagnosticOptions DiagOpts;
-    Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0), true);
+    Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0);
   }
   
   llvm::SmallVector<const char *, 16> Args;
index 879e9f681de98791f4cbb6d506c92cf4f7255cc8..1f915e3713d3cc0d8ea9ea14c5fff60dd049a8d1 100644 (file)
@@ -52,7 +52,7 @@ void CompilerInstance::setInvocation(CompilerInvocation *Value) {
 }
 
 void CompilerInstance::setDiagnostics(Diagnostic *Value) {
-  Diagnostics.reset(Value);
+  Diagnostics = Value;
 }
 
 void CompilerInstance::setDiagnosticClient(DiagnosticClient *Value) {
@@ -130,15 +130,16 @@ static void SetUpBuildDumpLog(const DiagnosticOptions &DiagOpts,
 }
 
 void CompilerInstance::createDiagnostics(int Argc, char **Argv) {
-  Diagnostics.reset(createDiagnostics(getDiagnosticOpts(), Argc, Argv));
+  Diagnostics = createDiagnostics(getDiagnosticOpts(), Argc, Argv);
 
   if (Diagnostics)
     DiagClient.reset(Diagnostics->getClient());
 }
 
-Diagnostic *CompilerInstance::createDiagnostics(const DiagnosticOptions &Opts,
-                                                int Argc, char **Argv) {
-  llvm::OwningPtr<Diagnostic> Diags(new Diagnostic());
+llvm::IntrusiveRefCntPtr<Diagnostic> 
+CompilerInstance::createDiagnostics(const DiagnosticOptions &Opts,
+                                    int Argc, char **Argv) {
+  llvm::IntrusiveRefCntPtr<Diagnostic> Diags(new Diagnostic());
 
   // Create the diagnostic client for reporting errors or for
   // implementing -verify.
@@ -152,7 +153,7 @@ Diagnostic *CompilerInstance::createDiagnostics(const DiagnosticOptions &Opts,
       DiagClient.reset(new TextDiagnosticPrinter(llvm::errs(), Opts));
       Diags->setClient(DiagClient.take());
       Diags->Report(diag::err_fe_stderr_binary);
-      return Diags.take();
+      return Diags;
     } else {
       DiagClient.reset(new BinaryDiagnosticSerializer(llvm::errs()));
     }
@@ -171,7 +172,7 @@ Diagnostic *CompilerInstance::createDiagnostics(const DiagnosticOptions &Opts,
   // Configure our handling of diagnostics.
   ProcessWarningOptions(*Diags, Opts);
 
-  return Diags.take();
+  return Diags;
 }
 
 // File Manager
index 464b589f183e6f4eebb7dcb09fcff433a844cc45..87fc1227b2f250b1d3109d85ed4b10b757df779d 100644 (file)
@@ -45,9 +45,9 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
            "Attempt to pass AST file to preprocessor only action!");
     assert(hasASTSupport() && "This action does not have AST support!");
 
+    llvm::IntrusiveRefCntPtr<Diagnostic> Diags(&CI.getDiagnostics());
     std::string Error;
-    ASTUnit *AST = ASTUnit::LoadFromPCHFile(Filename, 
-                                            UnownedDiag(CI.getDiagnostics()));
+    ASTUnit *AST = ASTUnit::LoadFromPCHFile(Filename, Diags);
     if (!AST)
       goto failure;
 
index 2b56bfb7d109d4a65fa29d39eea0ae63373936b0..01130e22773ddb6790ee198233f9b446b88286b5 100644 (file)
@@ -996,7 +996,8 @@ CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
 
   CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
 
-  return ASTUnit::LoadFromPCHFile(ast_filename, DefaultDiag(),
+  llvm::IntrusiveRefCntPtr<Diagnostic> Diags;
+  return ASTUnit::LoadFromPCHFile(ast_filename, Diags,
                                   CXXIdx->getOnlyLocalDecls(),
                                   0, 0, true);
 }
@@ -1015,8 +1016,8 @@ clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
 
   // Configure the diagnostics.
   DiagnosticOptions DiagOpts;
-  llvm::MaybeOwningPtr<Diagnostic> Diags;
-  Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0), true);
+  llvm::IntrusiveRefCntPtr<Diagnostic> Diags;
+  Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0);
 
   llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
   for (unsigned I = 0; I != num_unsaved_files; ++I) {
index 264e5064ddb46c3e72e9910ce27d483fda7d7c68..d11b8dfb50f79d36ac007a5d62efa5aced1a86a4 100644 (file)
@@ -231,8 +231,8 @@ CXCodeCompleteResults *clang_codeComplete(CXIndex CIdx,
 
   // Configure the diagnostics.
   DiagnosticOptions DiagOpts;
-  llvm::OwningPtr<Diagnostic> Diags;
-  Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0));
+  llvm::IntrusiveRefCntPtr<Diagnostic> Diags;
+  Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0);
   
   // The set of temporary files that we've built.
   std::vector<llvm::sys::Path> TemporaryFiles;