[cfe-commits] r84539 - in /cfe/trunk: include/clang/Frontend/ASTUnit.h lib/Frontend/ASTUnit.cpp tools/CIndex/CIndex.cpp

Ted Kremenek kremenek at apple.com
Mon Oct 19 14:44:57 PDT 2009


Author: kremenek
Date: Mon Oct 19 16:44:57 2009
New Revision: 84539

URL: http://llvm.org/viewvc/llvm-project?rev=84539&view=rev
Log:
The constructor for ASTUnit now takes a DiagnosticClient*, allowing uses of ASTUnit to specify
alternate DiagnosticClients. To match this API, ASTUnit::LoadFromPCHFile() now takes a corresponding
DiagnosticClient* argument as well. The DiagnosticClient object is destroyed when the ASTUnit object
is destroyed.

The CIndex library now uses this API to create a 'IgnoreDiagnosticsClient' that simply silences
diagnostics when using the clang_createTranslationUnitFromSourceFile() function. This fixes
<rdar://problem/7312058>. This API can change in the future as we add more flexibility for clients.

Modified:
    cfe/trunk/include/clang/Frontend/ASTUnit.h
    cfe/trunk/lib/Frontend/ASTUnit.cpp
    cfe/trunk/tools/CIndex/CIndex.cpp

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

==============================================================================
--- cfe/trunk/include/clang/Frontend/ASTUnit.h (original)
+++ cfe/trunk/include/clang/Frontend/ASTUnit.h Mon Oct 19 16:44:57 2009
@@ -35,7 +35,6 @@
 /// \brief Utility class for loading a ASTContext from a PCH file.
 ///
 class ASTUnit {
-  TextDiagnosticBuffer DiagClient;
   Diagnostic Diags;
   FileManager FileMgr;
 
@@ -56,6 +55,7 @@
   ASTUnit();
 
 public:
+  ASTUnit(DiagnosticClient *diagClient = NULL);
   ~ASTUnit();
 
   const SourceManager &getSourceManager() const { return SourceMgr; }
@@ -84,7 +84,9 @@
   ///
   /// \param Filename - The PCH file to load.
   ///
-  /// \param Diags - The Diagnostic implementation to use.
+  /// \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 FileMgr - The FileManager to use.
   ///
@@ -94,6 +96,7 @@
   /// \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,
                                   bool OnlyLocalDecls = false,
                                   bool UseBumpAllocator = false);
 };

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

==============================================================================
--- cfe/trunk/lib/Frontend/ASTUnit.cpp (original)
+++ cfe/trunk/lib/Frontend/ASTUnit.cpp Mon Oct 19 16:44:57 2009
@@ -25,12 +25,15 @@
 
 using namespace clang;
 
-ASTUnit::ASTUnit() : tempFile(false) {
-  Diags.setClient(&DiagClient);
+ASTUnit::ASTUnit(DiagnosticClient *diagClient) : tempFile(false) {      
+  Diags.setClient(diagClient ? diagClient : new TextDiagnosticBuffer());
 }
 ASTUnit::~ASTUnit() { 
   if (tempFile)
     llvm::sys::Path(getPCHFileName()).eraseFromDisk();
+  
+  //  The ASTUnit object owns the DiagnosticClient.
+  delete Diags.getClient();
 }
 
 namespace {
@@ -92,9 +95,10 @@
 
 ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
                                   std::string *ErrMsg,
+                                  DiagnosticClient *diagClient,
                                   bool OnlyLocalDecls,
                                   bool UseBumpAllocator) {
-  llvm::OwningPtr<ASTUnit> AST(new ASTUnit());
+  llvm::OwningPtr<ASTUnit> AST(new ASTUnit(diagClient));
   AST->OnlyLocalDecls = OnlyLocalDecls;
   AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager()));
 
@@ -109,7 +113,8 @@
   llvm::OwningPtr<PCHReader> Reader;
   llvm::OwningPtr<ExternalASTSource> Source;
 
-  Reader.reset(new PCHReader(AST->getSourceManager(), AST->getFileManager(), AST->Diags));
+  Reader.reset(new PCHReader(AST->getSourceManager(), AST->getFileManager(),
+                             AST->Diags));
   Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple,
                                            Predefines, Counter));
 

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

==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndex.cpp Mon Oct 19 16:44:57 2009
@@ -23,9 +23,11 @@
 #include "clang/Basic/SourceManager.h"
 #include "clang/Frontend/ASTUnit.h"
 #include "llvm/Config/config.h"
+#include "llvm/Support/Compiler.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/System/Path.h"
 #include "llvm/System/Program.h"
+
 #include <cstdio>
 #include <vector>
 
@@ -40,7 +42,6 @@
 using namespace idx;
 
 namespace {
-
 static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE) 
 {
   NamedDecl *D = DRE->getDecl();
@@ -89,6 +90,14 @@
   }
 };
 #endif
+  
+/// IgnoreDiagnosticsClient - A DiagnosticsClient that just ignores emitted
+/// warnings and errors.
+class VISIBILITY_HIDDEN IgnoreDiagnosticsClient : public DiagnosticClient {
+public:
+  virtual ~IgnoreDiagnosticsClient() {}
+  virtual void HandleDiagnostic(Diagnostic::Level, const DiagnosticInfo &) {}  
+};
 
 // Translation Unit Visitor.
 class TUVisitor : public DeclVisitor<TUVisitor> {
@@ -349,6 +358,7 @@
   std::string ErrMsg;
   
   return ASTUnit::LoadFromPCHFile(astName, &ErrMsg,
+                                  new IgnoreDiagnosticsClient(),
                                   CXXIdx->getOnlyLocalDecls(),
                                   /* UseBumpAllocator = */ true);
 }





More information about the cfe-commits mailing list