} 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.
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(); }
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;
};
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)
// 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);
}
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;
// 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.
// 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;
}
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)
{