]> granicus.if.org Git - clang/commitdiff
Really fix <rdar://problem/7312058> by adding a 'displayDiagnostics' option to
authorTed Kremenek <kremenek@apple.com>
Mon, 19 Oct 2009 22:15:09 +0000 (22:15 +0000)
committerTed Kremenek <kremenek@apple.com>
Mon, 19 Oct 2009 22:15:09 +0000 (22:15 +0000)
clang_createTranslationUnit() and clang_createTranslationUnitFromSourceFile(). The user can now
specify if the diagnostics from Clang are printed to stderr or are silenced completely. We can
obviously evolve this API to be more general in the future.

Note: Added a FIXME since I wasn't certain what was the best way to redirect to something analogous
to '/dev/null' on Windows.

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

include/clang-c/Index.h
tools/CIndex/CIndex.cpp
tools/c-index-test/c-index-test.c

index b80cd47e01f14df2805fc0dab05c1bb8dbf6378b..b9a982ec0c70a313b77acb4e9f8cff0e2b2f6d21 100644 (file)
@@ -107,15 +107,26 @@ void clang_disposeIndex(CXIndex);
 const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit);
 
 CXTranslationUnit clang_createTranslationUnit(
-  CXIndex, const char *ast_filename
+  CXIndex, const char *ast_filename,
+  int displayDiagnostics
 );
+
+/**
+ * \brief Destroy the specified CXTranslationUnit object.
+ */ 
+void clang_disposeTranslationUnit(CXTranslationUnit);
+
+/**
+ * \brief Return the CXTranslationUnit for a given source file and the provided command line
+ *   arguments one would pass to the compiler.
+ */
 CXTranslationUnit clang_createTranslationUnitFromSourceFile(
   CXIndex CIdx, 
   const char *source_filename,
   int num_clang_command_line_args, 
-  const char **clang_command_line_args
+  const char **clang_command_line_args,
+  int displayDiagnostics
 );
-void clang_disposeTranslationUnit(CXTranslationUnit);
 
 /**
  * \brief Indicate to Clang that it should only enumerate "local" declarations
index 3f3fc8c3d329cc0cca709c23d0505477d5cc4c10..77e7589269f2264b7cd3d81f3c25f37b23414059 100644 (file)
@@ -350,15 +350,16 @@ void clang_disposeIndex(CXIndex CIdx)
 
 // FIXME: need to pass back error info.
 CXTranslationUnit clang_createTranslationUnit(
-  CXIndex CIdx, const char *ast_filename) 
+  CXIndex CIdx, const char *ast_filename, int displayDiagnostics
 {
   assert(CIdx && "Passed null CXIndex");
   CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
   std::string astName(ast_filename);
   std::string ErrMsg;
+  DiagnosticClient *diagClient = displayDiagnostics
+                                  ? NULL : new IgnoreDiagnosticsClient();
   
-  return ASTUnit::LoadFromPCHFile(astName, &ErrMsg,
-                                  new IgnoreDiagnosticsClient(),
+  return ASTUnit::LoadFromPCHFile(astName, &ErrMsg, diagClient,
                                   CXXIdx->getOnlyLocalDecls(),
                                   /* UseBumpAllocator = */ true);
 }
@@ -366,8 +367,8 @@ CXTranslationUnit clang_createTranslationUnit(
 CXTranslationUnit clang_createTranslationUnitFromSourceFile(
   CXIndex CIdx, 
   const char *source_filename,
-  int num_command_line_args, const char **command_line_args
-{
+  int num_command_line_args, const char **command_line_args,
+  int displayDiagnostics)  {
   // Build up the arguments for involing clang.
   llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
   std::vector<const char *> argv;
@@ -383,11 +384,21 @@ CXTranslationUnit clang_createTranslationUnitFromSourceFile(
   argv.push_back(NULL);
 
   // Generate the AST file in a separate process.
+#ifdef LLVM_ON_WIN32
+  llvm::sys::Path DevNull("/dev/null")
+  llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL };
+  llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0],
+                                     !displayDiagnostics ? Redirects : NULL);
+#else
+  // FIXME: I don't know what is the equivalent '/dev/null' redirect for
+  // Windows for this API.
   llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0]);
+#endif
 
   // Finally, we create the translation unit from the ast file.
   ASTUnit *ATU = static_cast<ASTUnit *>(
-                   clang_createTranslationUnit(CIdx, astTmpFile));
+                   clang_createTranslationUnit(CIdx, astTmpFile,
+                                               displayDiagnostics));
   ATU->unlinkTemporaryFile();
   return ATU;
 }
index fc84faac2bb7aec7bbe1d1ef1d93727294f91658..29cd1370e5a35ff053e0e244f9f0161fec90959a 100644 (file)
@@ -96,7 +96,7 @@ int main(int argc, char **argv) {
   if (!strcmp(argv[2], "local"))
     clang_wantOnlyLocalDeclarations(Idx);
 
-  TU = clang_createTranslationUnit(Idx, argv[1]);
+  TU = clang_createTranslationUnit(Idx, argv[1], /* displayDiagnostics= */ 1);
 
   if (!TU) {
     fprintf(stderr, "Unable to load translation unit!\n");