From 5a43021ac491bf091494167127772a20d9a9bb48 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Wed, 21 Jul 2010 18:52:53 +0000 Subject: [PATCH] Introduce a new C API function, clang_parseTranslationUnit(), which will eventually replace clang_createTranslationUnitFromSourceFile(). The only addition in clang_parseTranslationUnit() is a set of flags that can control how the translation unit is loaded. More interesting flags will be coming. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@109027 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang-c/Index.h | 77 ++++++++++++++++++++++++++ tools/libclang/CIndex.cpp | 20 ++++++- tools/libclang/libclang.darwin.exports | 1 + tools/libclang/libclang.exports | 1 + 4 files changed, 97 insertions(+), 2 deletions(-) diff --git a/include/clang-c/Index.h b/include/clang-c/Index.h index 08178e61e6..467a9826eb 100644 --- a/include/clang-c/Index.h +++ b/include/clang-c/Index.h @@ -634,6 +634,83 @@ CINDEX_LINKAGE CXTranslationUnit clang_createTranslationUnitFromSourceFile( CINDEX_LINKAGE CXTranslationUnit clang_createTranslationUnit(CXIndex, const char *ast_filename); + /** + * \brief Flags that control the creation of translation units. + * + * The enumerators in this enumeration type are meant to be bitwise + * ORed together to specify which options should be used when + * constructing the translation unit. + */ +enum CXTranslationUnit_Flags { + /** + * \brief Used to indicate that no special translation-unit options are + * needed. + */ + CXTranslationUnit_None = 0x0, + + /** + * \brief Used to indicate that the parser should construct a "detailed" + * preprocessing record, including all macro definitions and instantiations. + * + * Constructing a detailed preprocessing record requires more memory + * and time to parse, since the information contained in the record + * is usually not retained. However, it can be useful for + * applications that require more detailed information about the + * behavior of the preprocessor. + */ + CXTranslationUnit_DetailedPreprocessingRecord = 0x01 +}; + +/** + * \brief Parse the given source file and the translation unit corresponding + * to that file. + * + * This routine is the main entry point for the Clang C API, providing the + * ability to parse a source file into a translation unit that can then be + * queried by other functions in the API. This routine accepts a set of + * command-line arguments so that the compilation can be configured in the same + * way that the compiler is configured on the command line. + * + * \param CIdx The index object with which the translation unit will be + * associated. + * + * \param source_filename The name of the source file to load, or NULL if the + * source file is included in \p clang_command_line_args. + * + * \param command_line_args The command-line arguments that would be + * passed to the \c clang executable if it were being invoked out-of-process. + * These command-line options will be parsed and will affect how the translation + * unit is parsed. Note that the following options are ignored: '-c', + * '-emit-ast', '-fsyntex-only' (which is the default), and '-o '. + * + * \param num_command_line_args The number of command-line arguments in + * \p command_line_args. + * + * \param unsaved_files the files that have not yet been saved to disk + * but may be required for code completion, including the contents of + * those files. The contents and name of these files (as specified by + * CXUnsavedFile) are copied when necessary, so the client only needs to + * guarantee their validity until the call to this function returns. + * + * \param num_unsaved_files the number of unsaved file entries in \p + * unsaved_files. + * + * \param options A bitmask of options that affects how the translation unit + * is managed but not its compilation. This should be a bitwise OR of the + * CXTranslationUnit_XXX flags. + * + * \returns A new translation unit describing the parsed code and containing + * any diagnostics produced by the compiler. If there is a failure from which + * the compiler cannot recover, returns NULL. + */ +CINDEX_LINKAGE CXTranslationUnit clang_parseTranslationUnit(CXIndex CIdx, + const char *source_filename, + const char **command_line_args, + int num_command_line_args, + struct CXUnsavedFile *unsaved_files, + unsigned num_unsaved_files, + unsigned options); + /** * \brief Destroy the specified CXTranslationUnit object. */ diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp index efc61a0645..47c6d3ca3f 100644 --- a/tools/libclang/CIndex.cpp +++ b/tools/libclang/CIndex.cpp @@ -1157,6 +1157,19 @@ clang_createTranslationUnitFromSourceFile(CXIndex CIdx, const char **command_line_args, unsigned num_unsaved_files, struct CXUnsavedFile *unsaved_files) { + return clang_parseTranslationUnit(CIdx, source_filename, + command_line_args, num_command_line_args, + unsaved_files, num_unsaved_files, + CXTranslationUnit_DetailedPreprocessingRecord); +} + +CXTranslationUnit clang_parseTranslationUnit(CXIndex CIdx, + const char *source_filename, + const char **command_line_args, + int num_command_line_args, + struct CXUnsavedFile *unsaved_files, + unsigned num_unsaved_files, + unsigned options) { if (!CIdx) return 0; @@ -1195,8 +1208,11 @@ clang_createTranslationUnitFromSourceFile(CXIndex CIdx, Args.insert(Args.end(), command_line_args, command_line_args + num_command_line_args); - Args.push_back("-Xclang"); - Args.push_back("-detailed-preprocessing-record"); + + if (options & CXTranslationUnit_DetailedPreprocessingRecord) { + Args.push_back("-Xclang"); + Args.push_back("-detailed-preprocessing-record"); + } unsigned NumErrors = Diags->getNumErrors(); #ifdef USE_CRASHTRACER diff --git a/tools/libclang/libclang.darwin.exports b/tools/libclang/libclang.darwin.exports index e09a6a00af..e51620c786 100644 --- a/tools/libclang/libclang.darwin.exports +++ b/tools/libclang/libclang.darwin.exports @@ -86,6 +86,7 @@ _clang_isReference _clang_isStatement _clang_isTranslationUnit _clang_isUnexposed +_clang_parseTranslationUnit _clang_reparseTranslationUnit _clang_setUseExternalASTGeneration _clang_tokenize diff --git a/tools/libclang/libclang.exports b/tools/libclang/libclang.exports index 4b6ddd1f5c..3d3e7138c8 100644 --- a/tools/libclang/libclang.exports +++ b/tools/libclang/libclang.exports @@ -86,6 +86,7 @@ clang_isReference clang_isStatement clang_isTranslationUnit clang_isUnexposed +clang_parseTranslationUnit clang_reparseTranslationUnit clang_setUseExternalASTGeneration clang_tokenize -- 2.40.0