[cfe-commits] r84634 - in /cfe/trunk: include/clang-c/Index.h tools/CIndex/CIndex.cpp tools/CIndex/CIndex.exports tools/c-index-test/c-index-test.c

Steve Naroff snaroff at apple.com
Tue Oct 20 07:46:24 PDT 2009


Author: snaroff
Date: Tue Oct 20 09:46:24 2009
New Revision: 84634

URL: http://llvm.org/viewvc/llvm-project?rev=84634&view=rev
Log:

- Extend clang_createIndex() to support PCH and diagnostic 'filtering'. This seems cleaner to me without sacrificing much flexibility.

- Remove clang_wantOnlyLocalDeclarations().

- Remove 'displayDiagnostics' arguments to clang_createTranslationUnitFromSourceFile() and clang_createTranslationUnit().

- Have clang_createTranslationUnitFromSourceFile() strip the '-o <outfile>' command line arguments if they exist. Document this semantic in the header. Also verify we have a valid ASTUnit before telling it to 'unlinkTemporaryFile()'.


Modified:
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/tools/CIndex/CIndex.cpp
    cfe/trunk/tools/CIndex/CIndex.exports
    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=84634&r1=84633&r2=84634&view=diff

==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Tue Oct 20 09:46:24 2009
@@ -99,46 +99,52 @@
 } CXCursor;  
 
 /* A unique token for looking up "visible" CXDecls from a CXTranslationUnit. */
-typedef void *CXEntity;     
+typedef void *CXEntity;
 
-CXIndex clang_createIndex();
+/**  
+ * \brief clang_createIndex() provides a shared context for creating
+ * translation units. It provides two options:
+ *
+ * - excludeDeclarationsFromPCH: When non-zero, allows enumeration of "local"
+ * declarations (when loading any new translation units). A "local" declaration
+ * is one that belongs in the translation unit itself and not in a precompiled 
+ * 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.
+ */
+CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
+                          int displayDiagnostics);
 void clang_disposeIndex(CXIndex);
 
 const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit);
 
+/* 
+ * \brief Create a translation unit from an AST file (-emit-ast).
+ */
 CXTranslationUnit clang_createTranslationUnit(
-  CXIndex, const char *ast_filename,
-  int displayDiagnostics
+  CXIndex, const char *ast_filename
 );
-
 /**
  * \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.
+ * \brief Return the CXTranslationUnit for a given source file and the provided
+ * command line arguments one would pass to the compiler.
+ *
+ * Note: If provided, this routine will strip the '-o <outputfile>' command 
+ * line arguments.
  */
 CXTranslationUnit clang_createTranslationUnitFromSourceFile(
   CXIndex CIdx, 
   const char *source_filename,
   int num_clang_command_line_args, 
-  const char **clang_command_line_args,
-  int displayDiagnostics
+  const char **clang_command_line_args
 );
 
-/**
- * \brief Indicate to Clang that it should only enumerate "local" declarations
- * when loading any new translation units.
- *
- * A "local" declaration is one that belongs in the translation unit itself and
- * not in a precompiled header that was used by the translation unit.
- *
- * FIXME: Remove this hook.
- */
-void clang_wantOnlyLocalDeclarations(CXIndex);
-  
 /*
    Usage: clang_loadTranslationUnit(). Will load the toplevel declarations
    within a translation unit, issuing a 'callback' for each one.

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

==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndex.cpp Tue Oct 20 09:46:24 2009
@@ -282,7 +282,9 @@
 
 class CIndexer : public Indexer {
 public:  
-  explicit CIndexer(Program *prog) : Indexer(*prog), OnlyLocalDecls(false) {}
+  explicit CIndexer(Program *prog) : Indexer(*prog), 
+                                     OnlyLocalDecls(false), 
+                                     DisplayDiagnostics(false) {}
 
   virtual ~CIndexer() { delete &getProgram(); }
 
@@ -292,10 +294,17 @@
   bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
   void setOnlyLocalDecls(bool Local = true) { OnlyLocalDecls = Local; }
 
+  void setDisplayDiagnostics(bool Display = true) { 
+    DisplayDiagnostics = Display;
+  }
+  bool getDisplayDiagnostics() const { return DisplayDiagnostics; }
+  
   /// \brief Get the path of the clang binary.
   const llvm::sys::Path& getClangPath();
 private:
   bool OnlyLocalDecls;
+  bool DisplayDiagnostics;
+  
   llvm::sys::Path ClangPath;
 };
 
@@ -337,9 +346,15 @@
 
 extern "C" {
 
-CXIndex clang_createIndex() 
+CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
+                          int displayDiagnostics) 
 {  
-  return new CIndexer(new Program());
+  CIndexer *CIdxr = new CIndexer(new Program());
+  if (excludeDeclarationsFromPCH)
+    CIdxr->setOnlyLocalDecls();
+  if (displayDiagnostics)
+    CIdxr->setDisplayDiagnostics();
+  return CIdxr;
 }
 
 void clang_disposeIndex(CXIndex CIdx)
@@ -350,16 +365,16 @@
 
 // FIXME: need to pass back error info.
 CXTranslationUnit clang_createTranslationUnit(
-  CXIndex CIdx, const char *ast_filename, int displayDiagnostics) 
+  CXIndex CIdx, const char *ast_filename) 
 {
   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, diagClient,
+  return ASTUnit::LoadFromPCHFile(astName, &ErrMsg, 
+                                  CXXIdx->getDisplayDiagnostics() ? 
+                                    NULL : new IgnoreDiagnosticsClient(),
                                   CXXIdx->getOnlyLocalDecls(),
                                   /* UseBumpAllocator = */ true);
 }
@@ -367,8 +382,10 @@
 CXTranslationUnit clang_createTranslationUnitFromSourceFile(
   CXIndex CIdx, 
   const char *source_filename,
-  int num_command_line_args, const char **command_line_args,
-  int displayDiagnostics)  {
+  int num_command_line_args, const char **command_line_args)  {
+  assert(CIdx && "Passed null CXIndex");
+  CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
+
   // Build up the arguments for involing clang.
   llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
   std::vector<const char *> argv;
@@ -379,16 +396,22 @@
   // Generate a temporary name for the AST file.
   char astTmpFile[L_tmpnam];
   argv.push_back(tmpnam(astTmpFile));
-  for (int i = 0; i < num_command_line_args; i++)
-    argv.push_back(command_line_args[i]);
+  for (int i = 0; i < num_command_line_args; i++) {
+    if (command_line_args[i] && strcmp(command_line_args[i], "-o") != 0)
+      argv.push_back(command_line_args[i]);
+    else { 
+      if (++i < num_command_line_args) // Skip "-o"...
+        i++; // ...and the following argument as well.
+    }
+  }
   argv.push_back(NULL);
 
-  // Generate the AST file in a separate process.
 #ifndef LLVM_ON_WIN32
   llvm::sys::Path DevNull("/dev/null");
   const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL };
   llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], NULL,
-                                     !displayDiagnostics ? &Redirects[0] :NULL);
+                                     !CXXIdx->getDisplayDiagnostics() ? 
+                                       &Redirects[0] : NULL);
 #else
   // FIXME: I don't know what is the equivalent '/dev/null' redirect for
   // Windows for this API.
@@ -397,9 +420,9 @@
 
   // Finally, we create the translation unit from the ast file.
   ASTUnit *ATU = static_cast<ASTUnit *>(
-                   clang_createTranslationUnit(CIdx, astTmpFile,
-                                               displayDiagnostics));
-  ATU->unlinkTemporaryFile();
+                   clang_createTranslationUnit(CIdx, astTmpFile));
+  if (ATU)
+    ATU->unlinkTemporaryFile();
   return ATU;
 }
 
@@ -409,10 +432,6 @@
   assert(CTUnit && "Passed null CXTranslationUnit");
   delete static_cast<ASTUnit *>(CTUnit);
 }
-
-void clang_wantOnlyLocalDeclarations(CXIndex CIdx) {
-  static_cast<CIndexer *>(CIdx)->setOnlyLocalDecls(true);
-}
   
 const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit)
 {

Modified: cfe/trunk/tools/CIndex/CIndex.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndex.exports?rev=84634&r1=84633&r2=84634&view=diff

==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.exports (original)
+++ cfe/trunk/tools/CIndex/CIndex.exports Tue Oct 20 09:46:24 2009
@@ -28,4 +28,3 @@
 _clang_getCursorKindSpelling
 _clang_getDefinitionSpellingAndExtent
 _clang_getTranslationUnitSpelling
-_clang_wantOnlyLocalDeclarations

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=84634&r1=84633&r2=84634&view=diff

==============================================================================
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)
+++ cfe/trunk/tools/c-index-test/c-index-test.c Tue Oct 20 09:46:24 2009
@@ -91,12 +91,10 @@
   CXTranslationUnit TU;
   enum CXCursorKind K = CXCursor_NotImplemented;
   
-  Idx = clang_createIndex();
+  Idx = clang_createIndex(/* excludeDeclsFromPCH */ !strcmp(argv[2], "local") ? 1 : 0, 
+                          /* displayDiagnostics */ 1);
   
-  if (!strcmp(argv[2], "local"))
-    clang_wantOnlyLocalDeclarations(Idx);
-
-  TU = clang_createTranslationUnit(Idx, argv[1], /* displayDiagnostics= */ 1);
+  TU = clang_createTranslationUnit(Idx, argv[1]);
 
   if (!TU) {
     fprintf(stderr, "Unable to load translation unit!\n");





More information about the cfe-commits mailing list