[cfe-commits] r90379 - in /cfe/trunk: examples/wpa/clang-wpa.cpp include/clang/Basic/DiagnosticFrontendKinds.td include/clang/Frontend/ASTUnit.h lib/Frontend/ASTUnit.cpp lib/Frontend/FrontendAction.cpp tools/CIndex/CIndex.cpp tools/index-test/index-test.cpp

Daniel Dunbar daniel at zuster.org
Wed Dec 2 17:45:44 PST 2009


Author: ddunbar
Date: Wed Dec  2 19:45:44 2009
New Revision: 90379

URL: http://llvm.org/viewvc/llvm-project?rev=90379&view=rev
Log:
Fix ASTUnit to allows require a (persistent) Diagnostic object be provided; propogate and simplify.

Modified:
    cfe/trunk/examples/wpa/clang-wpa.cpp
    cfe/trunk/include/clang/Basic/DiagnosticFrontendKinds.td
    cfe/trunk/include/clang/Frontend/ASTUnit.h
    cfe/trunk/lib/Frontend/ASTUnit.cpp
    cfe/trunk/lib/Frontend/FrontendAction.cpp
    cfe/trunk/tools/CIndex/CIndex.cpp
    cfe/trunk/tools/index-test/index-test.cpp

Modified: cfe/trunk/examples/wpa/clang-wpa.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/examples/wpa/clang-wpa.cpp?rev=90379&r1=90378&r2=90379&view=diff

==============================================================================
--- cfe/trunk/examples/wpa/clang-wpa.cpp (original)
+++ cfe/trunk/examples/wpa/clang-wpa.cpp Wed Dec  2 19:45:44 2009
@@ -16,7 +16,7 @@
 #include "clang/Frontend/ASTUnit.h"
 #include "clang/Basic/FileManager.h"
 #include "clang/Basic/SourceManager.h"
-#include "clang/Frontend/TextDiagnosticBuffer.h"
+#include "clang/Frontend/CompilerInstance.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/raw_ostream.h"
 using namespace clang;
@@ -33,20 +33,14 @@
   if (InputFilenames.empty())
     return 0;
 
-  TextDiagnosticBuffer DiagClient;
+  llvm::OwningPtr<Diagnostic> Diags(
+    CompilerInstance::createDiagnostics(DiagnosticOptions(), argc, argv));
 
   for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) {
     const std::string &InFile = InputFilenames[i];
-
-    std::string ErrMsg;
-    llvm::OwningPtr<ASTUnit> AST;
-
-    AST.reset(ASTUnit::LoadFromPCHFile(InFile, &ErrMsg, &DiagClient));
-
-    if (!AST) {
-      llvm::errs() << "[" << InFile << "] error: " << ErrMsg << '\n';
+    llvm::OwningPtr<ASTUnit> AST(ASTUnit::LoadFromPCHFile(InFile, *Diags));
+    if (!AST)
       return 1;
-    }
 
     ASTUnits.push_back(AST.take());
   }

Modified: cfe/trunk/include/clang/Basic/DiagnosticFrontendKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticFrontendKinds.td?rev=90379&r1=90378&r2=90379&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticFrontendKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticFrontendKinds.td Wed Dec  2 19:45:44 2009
@@ -37,6 +37,8 @@
     "could not remap file '%0' to the contents of file '%1'">, DefaultFatal;
 def err_fe_remap_missing_from_file : Error<
     "could not remap from missing file '%0'">, DefaultFatal;
+def err_fe_unable_to_load_pch : Error<
+    "unable to load PCH file">;
 
 def err_verify_bogus_characters : Error<
     "bogus characters before '{{' in expected string">;

Modified: cfe/trunk/include/clang/Frontend/ASTUnit.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/ASTUnit.h?rev=90379&r1=90378&r2=90379&view=diff

==============================================================================
--- cfe/trunk/include/clang/Frontend/ASTUnit.h (original)
+++ cfe/trunk/include/clang/Frontend/ASTUnit.h Wed Dec  2 19:45:44 2009
@@ -16,7 +16,6 @@
 
 #include "clang/Basic/SourceManager.h"
 #include "llvm/ADT/OwningPtr.h"
-#include "clang/Frontend/TextDiagnosticBuffer.h"
 #include "clang/Basic/FileManager.h"
 #include "clang/Index/ASTLocation.h"
 #include <string>
@@ -32,14 +31,12 @@
 class Preprocessor;
 class SourceManager;
 class TargetInfo;
-class TextDiagnosticBuffer;
 
 using namespace idx;
 
 /// \brief Utility class for loading a ASTContext from a PCH file.
 ///
 class ASTUnit {
-  Diagnostic Diags;
   FileManager FileMgr;
 
   SourceManager                     SourceMgr;
@@ -67,7 +64,7 @@
   ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT
 
 public:
-  ASTUnit(bool MainFileIsAST, DiagnosticClient *diagClient = NULL);
+  ASTUnit(bool MainFileIsAST);
   ~ASTUnit();
 
   bool isMainFileAST() const { return MainFileIsAST; }
@@ -81,9 +78,6 @@
   const ASTContext &getASTContext() const { return *Ctx.get(); }
         ASTContext &getASTContext()       { return *Ctx.get(); }
 
-  const Diagnostic &getDiagnostic() const { return Diags; }
-        Diagnostic &getDiagnostic()       { return Diags; }
-
   const FileManager &getFileManager() const { return FileMgr; }
         FileManager &getFileManager()       { return FileMgr; }
   
@@ -101,17 +95,12 @@
   ///
   /// \param Filename - The PCH file to load.
   ///
-  /// \param DiagClient - The diagnostics client to use.  Specify NULL
-  /// to use a default client that emits warnings/errors to standard error.
-  /// The ASTUnit objects takes ownership of this object.
-  ///
-  /// \param ErrMsg - Error message to report if the PCH file could not be
-  /// loaded.
+  /// \param Diags - The diagnostics engine to use for reporting errors; its
+  /// lifetime is expected to extend past that of the returned ASTUnit.
   ///
   /// \returns - The initialized ASTUnit or null if the PCH failed to load.
   static ASTUnit *LoadFromPCHFile(const std::string &Filename,
-                                  std::string *ErrMsg = 0,
-                                  DiagnosticClient *DiagClient = NULL,
+                                  Diagnostic &Diags,
                                   bool OnlyLocalDecls = false,
                                   bool UseBumpAllocator = false);
 
@@ -121,7 +110,8 @@
   /// \param CI - The compiler invocation to use; it must have exactly one input
   /// source file.
   ///
-  /// \param Diags - The diagnostics engine to use for reporting errors.
+  /// \param Diags - The diagnostics engine to use for reporting errors; its
+  /// lifetime is expected to extend past that of the returned ASTUnit.
   //
   // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
   // shouldn't need to specify them at construction time.
@@ -136,7 +126,8 @@
   ///
   /// \param ArgEnd - The end of the argument vector.
   ///
-  /// \param Diags - The diagnostics engine to use for reporting errors.
+  /// \param Diags - The diagnostics engine to use for reporting errors; its
+  /// lifetime is expected to extend past that of the returned ASTUnit.
   ///
   /// \param Argv0 - The program path (from argv[0]), for finding the builtin
   /// compiler path.

Modified: cfe/trunk/lib/Frontend/ASTUnit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/ASTUnit.cpp?rev=90379&r1=90378&r2=90379&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/ASTUnit.cpp (original)
+++ cfe/trunk/lib/Frontend/ASTUnit.cpp Wed Dec  2 19:45:44 2009
@@ -34,18 +34,12 @@
 #include "llvm/System/Path.h"
 using namespace clang;
 
-ASTUnit::ASTUnit(bool _MainFileIsAST,
-                 DiagnosticClient *diagClient)
-  : tempFile(false), MainFileIsAST(_MainFileIsAST)
-{
-  Diags.setClient(diagClient ? diagClient : new TextDiagnosticBuffer());
+ASTUnit::ASTUnit(bool _MainFileIsAST)
+  : tempFile(false), MainFileIsAST(_MainFileIsAST) {
 }
 ASTUnit::~ASTUnit() {
   if (tempFile)
     llvm::sys::Path(getPCHFileName()).eraseFromDisk();
-
-  //  The ASTUnit object owns the DiagnosticClient.
-  delete Diags.getClient();
 }
 
 namespace {
@@ -107,11 +101,10 @@
 }
 
 ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
-                                  std::string *ErrMsg,
-                                  DiagnosticClient *diagClient,
+                                  Diagnostic &Diags,
                                   bool OnlyLocalDecls,
                                   bool UseBumpAllocator) {
-  llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true, diagClient));
+  llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true));
   AST->OnlyLocalDecls = OnlyLocalDecls;
   AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager()));
 
@@ -127,7 +120,7 @@
   llvm::OwningPtr<ExternalASTSource> Source;
 
   Reader.reset(new PCHReader(AST->getSourceManager(), AST->getFileManager(),
-                             AST->Diags));
+                             Diags));
   Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple,
                                            Predefines, Counter));
 
@@ -137,8 +130,7 @@
 
   case PCHReader::Failure:
   case PCHReader::IgnorePCH:
-    if (ErrMsg)
-      *ErrMsg = "Could not load PCH file";
+    Diags.Report(diag::err_fe_unable_to_load_pch);
     return NULL;
   }
 
@@ -154,8 +146,8 @@
   TargetOpts.CPU = "";
   TargetOpts.Features.clear();
   TargetOpts.Triple = TargetTriple;
-  AST->Target.reset(TargetInfo::CreateTargetInfo(AST->Diags, TargetOpts));
-  AST->PP.reset(new Preprocessor(AST->Diags, LangInfo, *AST->Target.get(),
+  AST->Target.reset(TargetInfo::CreateTargetInfo(Diags, TargetOpts));
+  AST->PP.reset(new Preprocessor(Diags, LangInfo, *AST->Target.get(),
                                  AST->getSourceManager(), HeaderInfo));
   Preprocessor &PP = *AST->PP.get();
 
@@ -231,8 +223,6 @@
          "FIXME: AST inputs not yet supported here!");
 
   // Create the AST unit.
-  //
-  // FIXME: Use the provided diagnostic client.
   AST.reset(new ASTUnit(false));
 
   AST->OnlyLocalDecls = OnlyLocalDecls;

Modified: cfe/trunk/lib/Frontend/FrontendAction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/FrontendAction.cpp?rev=90379&r1=90378&r2=90379&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/FrontendAction.cpp (original)
+++ cfe/trunk/lib/Frontend/FrontendAction.cpp Wed Dec  2 19:45:44 2009
@@ -46,11 +46,9 @@
     assert(hasASTSupport() && "This action does not have AST support!");
 
     std::string Error;
-    ASTUnit *AST = ASTUnit::LoadFromPCHFile(Filename, &Error);
-    if (!AST) {
-      CI.getDiagnostics().Report(diag::err_fe_invalid_ast_file) << Error;
+    ASTUnit *AST = ASTUnit::LoadFromPCHFile(Filename, CI.getDiagnostics());
+    if (!AST)
       goto failure;
-    }
 
     setCurrentFile(Filename, AST);
 

Modified: cfe/trunk/tools/CIndex/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndex.cpp?rev=90379&r1=90378&r2=90379&view=diff

==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndex.cpp Wed Dec  2 19:45:44 2009
@@ -23,6 +23,7 @@
 #include "clang/Basic/FileManager.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Frontend/ASTUnit.h"
+#include "clang/Frontend/CompilerInstance.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/Config/config.h"
 #include "llvm/Support/Compiler.h"
@@ -291,10 +292,24 @@
 };
 
 class CIndexer : public Indexer {
+  IgnoreDiagnosticsClient IgnoreDiagClient;
+  llvm::OwningPtr<Diagnostic> TextDiags;
+  Diagnostic IgnoreDiags;
+  bool UseExternalASTGeneration;
+  bool OnlyLocalDecls;
+  bool DisplayDiagnostics;
+
+  llvm::sys::Path ClangPath;
+
 public:
   explicit CIndexer(Program *prog) : Indexer(*prog),
+                                     IgnoreDiags(&IgnoreDiagClient),
+                                     UseExternalASTGeneration(false),
                                      OnlyLocalDecls(false),
-                                     DisplayDiagnostics(false) {}
+                                     DisplayDiagnostics(false) {
+    TextDiags.reset(
+      CompilerInstance::createDiagnostics(DiagnosticOptions(), 0, 0));
+  }
 
   virtual ~CIndexer() { delete &getProgram(); }
 
@@ -304,18 +319,17 @@
   bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
   void setOnlyLocalDecls(bool Local = true) { OnlyLocalDecls = Local; }
 
+  bool getDisplayDiagnostics() const { return DisplayDiagnostics; }
   void setDisplayDiagnostics(bool Display = true) {
     DisplayDiagnostics = Display;
   }
-  bool getDisplayDiagnostics() const { return DisplayDiagnostics; }
+
+  Diagnostic &getDiags() {
+    return DisplayDiagnostics ? *TextDiags : IgnoreDiags;
+  }
 
   /// \brief Get the path of the clang binary.
   const llvm::sys::Path& getClangPath();
-private:
-  bool OnlyLocalDecls;
-  bool DisplayDiagnostics;
-
-  llvm::sys::Path ClangPath;
 };
 
 const llvm::sys::Path& CIndexer::getClangPath() {
@@ -457,20 +471,10 @@
                                               const char *ast_filename) {
   assert(CIdx && "Passed null CXIndex");
   CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
-  std::string astName(ast_filename);
-  std::string ErrMsg;
-
-  CXTranslationUnit TU =
-    ASTUnit::LoadFromPCHFile(astName, &ErrMsg,
-                             CXXIdx->getDisplayDiagnostics() ?
-                             NULL : new IgnoreDiagnosticsClient(),
-                             CXXIdx->getOnlyLocalDecls(),
-                             /* UseBumpAllocator = */ true);
-
-  if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty())
-    llvm::errs() << "clang_createTranslationUnit: " << ErrMsg  << '\n';
 
-  return TU;
+  return ASTUnit::LoadFromPCHFile(ast_filename, CXXIdx->getDiags(),
+                                  CXXIdx->getOnlyLocalDecls(),
+                                  /* UseBumpAllocator = */ true);
 }
 
 CXTranslationUnit

Modified: cfe/trunk/tools/index-test/index-test.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/index-test/index-test.cpp?rev=90379&r1=90378&r2=90379&view=diff

==============================================================================
--- cfe/trunk/tools/index-test/index-test.cpp (original)
+++ cfe/trunk/tools/index-test/index-test.cpp Wed Dec  2 19:45:44 2009
@@ -238,7 +238,6 @@
   Indexer Idxer(Prog);
   llvm::SmallVector<TUnit*, 4> TUnits;
 
-  TextDiagnosticPrinter DiagClient(llvm::errs(), DiagnosticOptions(), false);
   llvm::OwningPtr<Diagnostic> Diags(
     CompilerInstance::createDiagnostics(DiagnosticOptions(), argc, argv));
 
@@ -248,21 +247,13 @@
 
   for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) {
     const std::string &InFile = InputFilenames[i];
-
-    std::string ErrMsg;
     llvm::OwningPtr<ASTUnit> AST;
-
-    if (ASTFromSource) {
+    if (ASTFromSource)
       AST.reset(CreateFromSource(InFile, *Diags, argv[0]));
-      if (!AST || Diags->getNumErrors())
-        ErrMsg = "unable to create AST";
-    } else
-      AST.reset(ASTUnit::LoadFromPCHFile(InFile, &ErrMsg));
-
-    if (!AST) {
-      llvm::errs() << "[" << InFile << "] Error: " << ErrMsg << '\n';
+    else
+      AST.reset(ASTUnit::LoadFromPCHFile(InFile, *Diags));
+    if (!AST)
       return 1;
-    }
 
     TUnit *TU = new TUnit(AST.take(), InFile);
     TUnits.push_back(TU);





More information about the cfe-commits mailing list