[cfe-commits] r150456 - in /cfe/trunk/tools: c-index-test/c-index-test.c libclang/CIndexDiagnostic.cpp libclang/CIndexDiagnostic.h

Ted Kremenek kremenek at apple.com
Mon Feb 13 18:46:03 PST 2012


Author: kremenek
Date: Mon Feb 13 20:46:03 2012
New Revision: 150456

URL: http://llvm.org/viewvc/llvm-project?rev=150456&view=rev
Log:
Implement new DiagnosticsRenderer that packages notes retrieved by clang_getDiagnosticSetFromTU() as
child diagnostics of primary diagnostics.  By using the DiagnosticRenderer, these Diagnostics now
match with those generated for serialized diagnostics.

Modified:
    cfe/trunk/tools/c-index-test/c-index-test.c
    cfe/trunk/tools/libclang/CIndexDiagnostic.cpp
    cfe/trunk/tools/libclang/CIndexDiagnostic.h

Modified: cfe/trunk/tools/c-index-test/c-index-test.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-index-test/c-index-test.c?rev=150456&r1=150455&r2=150456&view=diff
==============================================================================
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)
+++ cfe/trunk/tools/c-index-test/c-index-test.c Mon Feb 13 20:46:03 2012
@@ -418,13 +418,21 @@
   }
 }
 
-void PrintDiagnostics(CXTranslationUnit TU) {
-  int i, n = clang_getNumDiagnostics(TU);
-  for (i = 0; i != n; ++i) {
-    CXDiagnostic Diag = clang_getDiagnostic(TU, i);
+void PrintDiagnosticSet(CXDiagnosticSet Set) {
+  int i = 0, n = clang_getNumDiagnosticsInSet(Set);
+  for ( ; i != n ; ++i) {
+    CXDiagnostic Diag = clang_getDiagnosticInSet(Set, i);
+    CXDiagnosticSet ChildDiags = clang_getChildDiagnostics(Diag);
     PrintDiagnostic(Diag);
-    clang_disposeDiagnostic(Diag);
-  }
+    if (ChildDiags)
+      PrintDiagnosticSet(ChildDiags);
+  }  
+}
+
+void PrintDiagnostics(CXTranslationUnit TU) {
+  CXDiagnosticSet TUSet = clang_getDiagnosticSetFromTU(TU);
+  PrintDiagnosticSet(TUSet);
+  clang_disposeDiagnosticSet(TUSet);
 }
 
 void PrintMemoryUsage(CXTranslationUnit TU) {

Modified: cfe/trunk/tools/libclang/CIndexDiagnostic.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndexDiagnostic.cpp?rev=150456&r1=150455&r2=150456&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndexDiagnostic.cpp (original)
+++ cfe/trunk/tools/libclang/CIndexDiagnostic.cpp Mon Feb 13 20:46:03 2012
@@ -18,6 +18,8 @@
 
 #include "clang/Frontend/ASTUnit.h"
 #include "clang/Frontend/FrontendDiagnostic.h"
+#include "clang/Frontend/DiagnosticRenderer.h"
+#include "clang/Frontend/DiagnosticOptions.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/Twine.h"
 #include "llvm/Support/MemoryBuffer.h"
@@ -40,6 +42,109 @@
 
 CXDiagnosticImpl::~CXDiagnosticImpl() {}
 
+namespace {
+class CXDiagnosticCustomNoteImpl : public CXDiagnosticImpl {
+  CXString Message;
+  CXSourceLocation Loc;
+public:
+  CXDiagnosticCustomNoteImpl(StringRef Msg, CXSourceLocation L)
+    : CXDiagnosticImpl(CustomNoteDiagnosticKind),
+      Message(createCXString(Msg)), Loc(L) {}
+
+  virtual ~CXDiagnosticCustomNoteImpl() {
+    clang_disposeString(Message);
+  }
+  
+  CXDiagnosticSeverity getSeverity() const {
+    return CXDiagnostic_Note;
+  }
+  
+  CXSourceLocation getLocation() const {
+    return Loc;
+  }
+  
+  CXString getSpelling() const {
+    return Message;
+  }
+  
+  CXString getDiagnosticOption(CXString *Disable) const {
+    if (Disable)
+      *Disable = createCXString("", false);    
+    return createCXString("", false);
+  }
+  
+  unsigned getCategory() const { return 0; }
+  unsigned getNumRanges() const { return 0; }
+  CXSourceRange getRange(unsigned Range) const { return clang_getNullRange(); }
+  unsigned getNumFixIts() const { return 0; }
+  CXString getFixIt(unsigned FixIt, CXSourceRange *ReplacementRange) const {
+    if (ReplacementRange)
+      *ReplacementRange = clang_getNullRange();
+    return createCXString("", false);
+  }
+};    
+    
+class CXDiagnosticRenderer : public DiagnosticNoteRenderer {
+public:  
+  CXDiagnosticRenderer(const SourceManager &SM,
+                       const LangOptions &LangOpts,
+                       const DiagnosticOptions &DiagOpts,
+                       CXDiagnosticSetImpl *mainSet)
+  : DiagnosticNoteRenderer(SM, LangOpts, DiagOpts),
+    CurrentSet(mainSet), MainSet(mainSet) {}
+  
+  virtual ~CXDiagnosticRenderer() {}
+
+  virtual void beginDiagnostic(DiagOrStoredDiag D,
+                               DiagnosticsEngine::Level Level) {    
+    
+    const StoredDiagnostic *SD = D.dyn_cast<const StoredDiagnostic*>();
+    if (!SD)
+      return;
+    
+    if (Level != DiagnosticsEngine::Note)
+      CurrentSet = MainSet;
+    
+    CXStoredDiagnostic *CD = new CXStoredDiagnostic(*SD, LangOpts);
+    CurrentSet->appendDiagnostic(CD);
+    
+    if (Level != DiagnosticsEngine::Note)
+      CurrentSet = &CD->getChildDiagnostics();
+  }
+  
+  virtual void emitDiagnosticMessage(SourceLocation Loc, PresumedLoc PLoc,
+                                     DiagnosticsEngine::Level Level,
+                                     StringRef Message,
+                                     ArrayRef<CharSourceRange> Ranges,
+                                     DiagOrStoredDiag D) {
+    if (!D.isNull())
+      return;
+    
+    CXSourceLocation L = translateSourceLocation(SM, LangOpts, Loc);
+    CXDiagnosticImpl *CD = new CXDiagnosticCustomNoteImpl(Message, L);
+    CurrentSet->appendDiagnostic(CD);
+  }
+  
+  virtual void emitDiagnosticLoc(SourceLocation Loc, PresumedLoc PLoc,
+                                 DiagnosticsEngine::Level Level,
+                                 ArrayRef<CharSourceRange> Ranges) {}
+
+  virtual void emitCodeContext(SourceLocation Loc,
+                               DiagnosticsEngine::Level Level,
+                               SmallVectorImpl<CharSourceRange>& Ranges,
+                               ArrayRef<FixItHint> Hints) {};
+  
+  virtual void emitNote(SourceLocation Loc, StringRef Message) {
+    CXSourceLocation L = translateSourceLocation(SM, LangOpts, Loc);
+    CurrentSet->appendDiagnostic(new CXDiagnosticCustomNoteImpl(Message,
+                                                                L));
+  }
+
+  CXDiagnosticSetImpl *CurrentSet;
+  CXDiagnosticSetImpl *MainSet;
+};  
+}
+
 CXDiagnosticSetImpl *cxdiag::lazyCreateDiags(CXTranslationUnit TU,
                                              bool checkIfChanged) {
   ASTUnit *AU = static_cast<ASTUnit *>(TU->TUData);
@@ -75,12 +180,14 @@
   if (!TU->Diagnostics) {
     CXDiagnosticSetImpl *Set = new CXDiagnosticSetImpl();
     TU->Diagnostics = Set;
+    DiagnosticOptions DOpts;
+    CXDiagnosticRenderer Renderer(AU->getSourceManager(),
+                                  AU->getASTContext().getLangOptions(),
+                                  DOpts, Set);
     
     for (ASTUnit::stored_diag_iterator it = AU->stored_diag_begin(),
          ei = AU->stored_diag_end(); it != ei; ++it) {
-      CXStoredDiagnostic *D =
-        new CXStoredDiagnostic(*it, AU->getASTContext().getLangOptions());
-      Set->appendDiagnostic(D);
+      Renderer.emitStoredDiagnostic(*it);
     }
   }
   return static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics);

Modified: cfe/trunk/tools/libclang/CIndexDiagnostic.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndexDiagnostic.h?rev=150456&r1=150455&r2=150456&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndexDiagnostic.h (original)
+++ cfe/trunk/tools/libclang/CIndexDiagnostic.h Mon Feb 13 20:46:03 2012
@@ -54,7 +54,8 @@
 
 class CXDiagnosticImpl {
 public:
-  enum Kind { StoredDiagnosticKind, LoadedDiagnosticKind };
+  enum Kind { StoredDiagnosticKind, LoadedDiagnosticKind,
+              CustomNoteDiagnosticKind };
   
   virtual ~CXDiagnosticImpl();
   





More information about the cfe-commits mailing list