[cfe-commits] r94714 - in /cfe/trunk: include/clang-c/Index.h include/clang/Basic/DiagnosticFrontendKinds.td tools/CIndex/CIndex.cpp tools/CIndex/CIndexCodeCompletion.cpp tools/CIndex/CIndexer.h tools/c-index-test/c-index-test.c

Douglas Gregor dgregor at apple.com
Wed Jan 27 16:56:43 PST 2010


Author: dgregor
Date: Wed Jan 27 18:56:43 2010
New Revision: 94714

URL: http://llvm.org/viewvc/llvm-project?rev=94714&view=rev
Log:
Switch the remaining diagnostic printing in CIndex over to the
diagnostic callback mechanism, so all diagnostics now go through that
callback. Also, eliminate the displayDiagnostics flag to
clang_createIndex(), since it is no longer necessary: the client
determines whether to display diagnostics or not.


Modified:
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/include/clang/Basic/DiagnosticFrontendKinds.td
    cfe/trunk/tools/CIndex/CIndex.cpp
    cfe/trunk/tools/CIndex/CIndexCodeCompletion.cpp
    cfe/trunk/tools/CIndex/CIndexer.h
    cfe/trunk/tools/c-index-test/c-index-test.c

Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=94714&r1=94713&r2=94714&view=diff

==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Wed Jan 27 18:56:43 2010
@@ -143,13 +143,10 @@
  * header that was used by the translation unit. If zero, all declarations
  * will be enumerated.
  *
- * - displayDiagnostics: when non-zero, diagnostics will be output. If zero,
- * diagnostics will be ignored.
- *
  * Here is an example:
  *
- *   // excludeDeclsFromPCH = 1, displayDiagnostics = 1
- *   Idx = clang_createIndex(1, 1);
+ *   // excludeDeclsFromPCH = 1
+ *   Idx = clang_createIndex(1);
  *
  *   // IndexTest.pch was produced with the following command:
  *   // "clang -x c IndexTest.h -emit-ast -o IndexTest.pch"
@@ -173,8 +170,7 @@
  * -include-pch) allows 'excludeDeclsFromPCH' to remove redundant callbacks
  * (which gives the indexer the same performance benefit as the compiler).
  */
-CINDEX_LINKAGE CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
-                          int displayDiagnostics);
+CINDEX_LINKAGE CXIndex clang_createIndex(int excludeDeclarationsFromPCH);
 CINDEX_LINKAGE void clang_disposeIndex(CXIndex index);
 
 /**
@@ -1593,6 +1589,13 @@
  * Note that the column should point just after the syntactic construct that
  * initiated code completion, and not in the middle of a lexical token.
  *
+ * \param diag_callback callback function that will receive any diagnostics
+ * emitted while processing this source file. If NULL, diagnostics will be
+ * suppressed.
+ *
+ * \param diag_client_data client data that will be passed to the diagnostic
+ * callback function.
+ *
  * \returns if successful, a new CXCodeCompleteResults structure
  * containing code-completion results, which should eventually be
  * freed with \c clang_disposeCodeCompleteResults(). If code
@@ -1607,7 +1610,9 @@
                                           struct CXUnsavedFile *unsaved_files,
                                           const char *complete_filename,
                                           unsigned complete_line,
-                                          unsigned complete_column);
+                                          unsigned complete_column,
+                                          CXDiagnosticCallback diag_callback,
+                                          CXClientData diag_client_data);
 
 /**
  * \brief Free the given set of code-completion results.

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

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticFrontendKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticFrontendKinds.td Wed Jan 27 18:56:43 2010
@@ -80,6 +80,8 @@
 def warn_fixit_no_changes : Note<
     "FIX-IT detected errors it could not fix; no output will be generated">;
 
+def err_fe_clang : Error<"error invoking%s: %s">, DefaultFatal;
+
 // PCH reader
 def err_relocatable_without_without_isysroot : Error<
     "must specify system root with -isysroot when building a relocatable "

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

==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndex.cpp Wed Jan 27 18:56:43 2010
@@ -18,9 +18,11 @@
 #include "CIndexDiagnostic.h"
 
 #include "clang/Basic/Version.h"
+
 #include "clang/AST/DeclVisitor.h"
 #include "clang/AST/StmtVisitor.h"
 #include "clang/AST/TypeLocVisitor.h"
+#include "clang/Frontend/FrontendDiagnostic.h"
 #include "clang/Lex/Lexer.h"
 #include "clang/Lex/Preprocessor.h"
 #include "llvm/Support/MemoryBuffer.h"
@@ -893,13 +895,10 @@
 }
 
 extern "C" {
-CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
-                          int displayDiagnostics) {
+CXIndex clang_createIndex(int excludeDeclarationsFromPCH) {
   CIndexer *CIdxr = new CIndexer();
   if (excludeDeclarationsFromPCH)
     CIdxr->setOnlyLocalDecls();
-  if (displayDiagnostics)
-    CIdxr->setDisplayDiagnostics();
   return CIdxr;
 }
 
@@ -1054,20 +1053,23 @@
   std::string ErrMsg;
   const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL };
   llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
-      /* redirects */ !CXXIdx->getDisplayDiagnostics() ? &Redirects[0] : NULL,
+      /* redirects */ &Redirects[0],
       /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
 
-  if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) {
-    llvm::errs() << "clang_createTranslationUnitFromSourceFile: " << ErrMsg
-                 << '\n' << "Arguments: \n";
+  if (!ErrMsg.empty()) {
+    std::string AllArgs;
     for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
-         I!=E; ++I) {
+         I != E; ++I) {
+      AllArgs += ' ';
       if (*I)
-        llvm::errs() << ' ' << *I << '\n';
+        AllArgs += *I;
     }
-    llvm::errs() << '\n';
+    
+    Diags->Report(diag::err_fe_clang) << AllArgs << ErrMsg;
   }
 
+  // FIXME: Parse the (redirected) standard error to emit diagnostics.
+
   ASTUnit *ATU = ASTUnit::LoadFromPCHFile(astTmpFile, *Diags,
                                           CXXIdx->getOnlyLocalDecls(),
                                           /* UseBumpAllocator = */ true,

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

==============================================================================
--- cfe/trunk/tools/CIndex/CIndexCodeCompletion.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndexCodeCompletion.cpp Wed Jan 27 18:56:43 2010
@@ -13,6 +13,8 @@
 //===----------------------------------------------------------------------===//
 
 #include "CIndexer.h"
+#include "CIndexDiagnostic.h"
+#include "clang/Frontend/FrontendDiagnostic.h"
 #include "clang/Sema/CodeCompleteConsumer.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/Support/MemoryBuffer.h"
@@ -186,10 +188,19 @@
                                           struct CXUnsavedFile *unsaved_files,
                                           const char *complete_filename,
                                           unsigned complete_line,
-                                          unsigned complete_column) {
+                                          unsigned complete_column,
+                                          CXDiagnosticCallback diag_callback,
+                                          CXClientData diag_client_data) {
   // The indexer, which is mainly used to determine where diagnostics go.
   CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
 
+  // Configure the diagnostics.
+  DiagnosticOptions DiagOpts;
+  llvm::OwningPtr<Diagnostic> Diags;
+  Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0));
+  CIndexDiagnosticClient DiagClient(diag_callback, diag_client_data);
+  Diags->setClient(&DiagClient);
+  
   // The set of temporary files that we've built.
   std::vector<llvm::sys::Path> TemporaryFiles;
 
@@ -272,15 +283,16 @@
                                      /* secondsToWait */ 0,
                                      /* memoryLimits */ 0, &ErrMsg);
 
-  if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) {
-    llvm::errs() << "clang_codeComplete: " << ErrMsg
-                 << '\n' << "Arguments: \n";
+  if (!ErrMsg.empty()) {
+    std::string AllArgs;
     for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
-         I!=E; ++I) {
+         I != E; ++I) {
+      AllArgs += ' ';
       if (*I)
-        llvm::errs() << ' ' << *I << '\n';
+        AllArgs += *I;
     }
-    llvm::errs() << '\n';
+    
+    Diags->Report(diag::err_fe_clang) << AllArgs << ErrMsg;
   }
 
   // Parse the resulting source file to find code-completion results.
@@ -319,6 +331,8 @@
     Results->Buffer = F;
   }
 
+  // FIXME: Parse the (redirected) standard error to emit diagnostics.
+  
   for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i)
     TemporaryFiles[i].eraseFromDisk();
 

Modified: cfe/trunk/tools/CIndex/CIndexer.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndexer.h?rev=94714&r1=94713&r2=94714&view=diff

==============================================================================
--- cfe/trunk/tools/CIndex/CIndexer.h (original)
+++ cfe/trunk/tools/CIndex/CIndexer.h Wed Jan 27 18:56:43 2010
@@ -27,14 +27,11 @@
 class CIndexer {
   bool UseExternalASTGeneration;
   bool OnlyLocalDecls;
-  bool DisplayDiagnostics;
   
   llvm::sys::Path ClangPath;
   
 public:
-  CIndexer() 
-    : UseExternalASTGeneration(false), OnlyLocalDecls(false), 
-      DisplayDiagnostics(false) { }
+  CIndexer() : UseExternalASTGeneration(false), OnlyLocalDecls(false) { }
   
   /// \brief Whether we only want to see "local" declarations (that did not
   /// come from a previous precompiled header). If false, we want to see all
@@ -42,11 +39,6 @@
   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 getUseExternalASTGeneration() const { return UseExternalASTGeneration; }
   void setUseExternalASTGeneration(bool Value) {
     UseExternalASTGeneration = Value;

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=94714&r1=94713&r2=94714&view=diff

==============================================================================
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)
+++ cfe/trunk/tools/c-index-test/c-index-test.c Wed Jan 27 18:56:43 2010
@@ -411,8 +411,7 @@
   CXIndex Idx;
   CXTranslationUnit TU;
   Idx = clang_createIndex(/* excludeDeclsFromPCH */ 
-                          !strcmp(filter, "local") ? 1 : 0, 
-                          /* displayDiagnostics */ 1);
+                          !strcmp(filter, "local") ? 1 : 0);
   
   if (!CreateTranslationUnit(Idx, file, &TU))
     return 1;
@@ -432,8 +431,7 @@
   int result;
   
   Idx = clang_createIndex(/* excludeDeclsFromPCH */
-                          !strcmp(filter, "local") ? 1 : 0,
-                          /* displayDiagnostics */ 1);
+                          !strcmp(filter, "local") ? 1 : 0);
 
   if (UseExternalASTs && strlen(UseExternalASTs))
     clang_setUseExternalASTGeneration(Idx, 1);
@@ -487,8 +485,7 @@
   unsigned start_line, start_col, last_line, last_col;
   size_t i;
   
-  if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
-                                /* displayDiagnostics */ 1))) {
+  if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1))) {
     fprintf(stderr, "Could not create Index\n");
     return 1;
   }
@@ -700,12 +697,14 @@
   if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
     return -1;
 
-  CIdx = clang_createIndex(0, 0);
+  CIdx = clang_createIndex(0);
   results = clang_codeComplete(CIdx, 
                                argv[argc - 1], argc - num_unsaved_files - 3, 
                                argv + num_unsaved_files + 2, 
                                num_unsaved_files, unsaved_files,
-                               filename, line, column);
+                               filename, line, column,
+                               PrintDiagnosticCallback, stderr);
+
   if (results) {
     unsigned i, n = results->NumResults;
     for (i = 0; i != n; ++i)
@@ -757,7 +756,7 @@
                            &num_unsaved_files))
     return -1;
   
-  CIdx = clang_createIndex(0, 1);
+  CIdx = clang_createIndex(0);
   TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
                                   argc - num_unsaved_files - 2 - NumLocations,
                                    argv + num_unsaved_files + 1 + NumLocations,
@@ -816,7 +815,7 @@
   if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
     return -1;
 
-  CIdx = clang_createIndex(0, 0);
+  CIdx = clang_createIndex(0);
   TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
                                                  argc - num_unsaved_files - 3,
                                                  argv + num_unsaved_files + 2,





More information about the cfe-commits mailing list