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

Steve Naroff snaroff at apple.com
Thu Oct 15 13:04:39 PDT 2009


Author: snaroff
Date: Thu Oct 15 15:04:39 2009
New Revision: 84198

URL: http://llvm.org/viewvc/llvm-project?rev=84198&view=rev
Log:
Implement <rdar://problem/7303432> [Clang/Index] In-memory-style AST generation API (initial API implementation).

Added clang_createTranslationUnitFromSourceFile().
Changed clang_createIndex() to lookup the location of clang (using dladdr).

Modified:
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/tools/CIndex/CIndex.cpp
    cfe/trunk/tools/CIndex/CIndex.exports

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

==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Thu Oct 15 15:04:39 2009
@@ -109,6 +109,12 @@
 CXTranslationUnit clang_createTranslationUnit(
   CXIndex, const char *ast_filename
 );
+CXTranslationUnit clang_createTranslationUnitFromSourceFile(
+  CXIndex CIdx, 
+  const char *source_filename,
+  int num_clang_command_line_args, 
+  const char **clang_command_line_args
+);
 void clang_disposeTranslationUnit(CXTranslationUnit);
 
 /*

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

==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndex.cpp Thu Oct 15 15:04:39 2009
@@ -23,6 +23,9 @@
 #include "clang/Basic/SourceManager.h"
 #include "clang/Frontend/ASTUnit.h"
 #include <cstdio>
+#include <dlfcn.h>
+#include "llvm/System/Path.h"
+
 using namespace clang;
 using namespace idx;
 
@@ -241,9 +244,27 @@
 
 extern "C" {
 
+static const char *clangPath;
+
 CXIndex clang_createIndex() 
 {
   // FIXME: Program is leaked.
+  
+  // Find the location where this library lives (libCIndex.dylib).
+  // We do the lookup here to avoid poking dladdr too many times.
+  // This silly cast below avoids a C++ warning.
+  Dl_info info;
+  if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0)
+    assert(0 && "Call to dladdr() failed");
+
+  llvm::sys::Path CIndexPath(info.dli_fname);
+  std::string CIndexDir = CIndexPath.getDirname();
+  
+  // We now have the CIndex directory, locate clang relative to it.
+  std::string ClangPath = CIndexDir + "/../bin/clang";
+  
+  clangPath = ClangPath.c_str();
+  
   return new Indexer(*new Program());
 }
 
@@ -266,6 +287,47 @@
                                   CXXIdx->getFileManager(), &ErrMsg);
 }
 
+CXTranslationUnit clang_createTranslationUnitFromSourceFile(
+  CXIndex CIdx, 
+  const char *source_filename,
+  int num_command_line_args, const char **command_line_args) 
+{
+  // Generate a temporary name for the AST file.
+  char astTmpFile[L_tmpnam];
+
+  // Build up the arguments for involking clang.
+  const char * argv[ARG_MAX];
+  int argc = 0;
+  argv[argc++] = clangPath;
+  argv[argc++] = "-emit-ast";
+  argv[argc++] = source_filename;
+  argv[argc++] = "-o";
+  argv[argc++] = tmpnam(astTmpFile);
+  for (int i = num_command_line_args; i < num_command_line_args; i++)
+    argv[argc++] = command_line_args[i];
+  argv[argc] = 0;
+  
+  // Generate the AST file in a separate process.
+  pid_t child_pid = fork();
+  if (child_pid == 0) { // Child process
+  
+    // Execute the command, passing the appropriate arguments.
+    execv(argv[0], (char *const *)argv);
+    
+    // If execv returns, it failed.
+    assert(0 && "execv() failed");
+  } else { // This is run by the parent.
+     int child_status;
+     pid_t tpid;
+     do { // Wait for the child to terminate.
+       tpid = wait(&child_status);
+     } while (tpid != child_pid);
+     
+     // Finally, we create the translation unit from the ast file.
+     return clang_createTranslationUnit(CIdx, astTmpFile);
+  }
+}
+
 void clang_disposeTranslationUnit(
   CXTranslationUnit CTUnit) 
 {

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

==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.exports (original)
+++ cfe/trunk/tools/CIndex/CIndex.exports Thu Oct 15 15:04:39 2009
@@ -18,6 +18,7 @@
 _clang_loadDeclaration
 _clang_loadTranslationUnit
 _clang_createTranslationUnit
+_clang_createTranslationUnitFromSourceFile
 _clang_disposeTranslationUnit
 _clang_isDeclaration
 _clang_isReference





More information about the cfe-commits mailing list