From cee235cdf0b8047761ffac598c4c3a32ab7411a2 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Thu, 5 Aug 2010 09:09:23 +0000 Subject: [PATCH] Give clang_codeCompleteAt() an "options" parameter, and add a new flags enumeration + default-generating function that allows code-completion to be customized via the libclang API. Plus, turn on spell-checking when performing code completion. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@110319 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang-c/Index.h | 36 ++++++++++++++++++++++++- include/clang/Frontend/ASTUnit.h | 16 ++++++++--- lib/Frontend/ASTUnit.cpp | 14 +++++++--- tools/c-index-test/c-index-test.c | 3 ++- tools/libclang/CIndexCodeCompletion.cpp | 12 +++++++-- tools/libclang/libclang.darwin.exports | 1 + tools/libclang/libclang.exports | 1 + 7 files changed, 72 insertions(+), 11 deletions(-) diff --git a/include/clang-c/Index.h b/include/clang-c/Index.h index 9e8623a3d8..bbc8fbaf96 100644 --- a/include/clang-c/Index.h +++ b/include/clang-c/Index.h @@ -2078,6 +2078,33 @@ CXCodeCompleteResults *clang_codeComplete(CXIndex CIdx, unsigned complete_line, unsigned complete_column); +/** + * \brief Flags that can be passed to \c clang_codeCompleteAt() to + * modify its behavior. + * + * The enumerators in this enumeration can be bitwise-OR'd together to + * provide multiple options to \c clang_codeCompleteAt(). + */ +enum CXCodeComplete_Flags { + /** + * \brief Whether to include macros within the set of code + * completions returned. + */ + CXCodeComplete_IncludeMacros = 0x01, + + /** + * \brief Whether to include code patterns for language constructs + * within the set of code completions, e.g., for loops. + */ + CXCodeComplete_IncludeCodePatterns = 0x02 +}; + +/** + * \brief Returns a default set of code-completion options that can be + * passed to\c clang_codeCompleteAt(). + */ +CINDEX_LINKAGE unsigned clang_defaultCodeCompleteOptions(void); + /** * \brief Perform code completion at a given location in a translation unit. * @@ -2135,6 +2162,12 @@ CXCodeCompleteResults *clang_codeComplete(CXIndex CIdx, * \param num_unsaved_files The number of unsaved file entries in \p * unsaved_files. * + * \param options Extra options that control the behavior of code + * completion, expressed as a bitwise OR of the enumerators of the + * CXCodeComplete_Flags enumeration. The + * \c clang_defaultCodeCompleteOptions() function returns a default set + * of code-completion options. + * * \returns If successful, a new \c CXCodeCompleteResults structure * containing code-completion results, which should eventually be * freed with \c clang_disposeCodeCompleteResults(). If code @@ -2146,7 +2179,8 @@ CXCodeCompleteResults *clang_codeCompleteAt(CXTranslationUnit TU, unsigned complete_line, unsigned complete_column, struct CXUnsavedFile *unsaved_files, - unsigned num_unsaved_files); + unsigned num_unsaved_files, + unsigned options); /** * \brief Free the given set of code-completion results. diff --git a/include/clang/Frontend/ASTUnit.h b/include/clang/Frontend/ASTUnit.h index 0068f90c90..e8d4ccef02 100644 --- a/include/clang/Frontend/ASTUnit.h +++ b/include/clang/Frontend/ASTUnit.h @@ -389,16 +389,24 @@ public: /// \brief Perform code completion at the given file, line, and /// column within this translation unit. /// - /// \brief File The file in which code completion will occur. - /// \brief Line The line at which code completion will occur. - /// \brief Column The column at which code completion will occur. - /// \brief Consumer The consumer that will receive code-completion results. + /// \param File The file in which code completion will occur. + /// + /// \param Line The line at which code completion will occur. + /// + /// \param Column The column at which code completion will occur. + /// + /// \param IncludeMacros Whether to include macros in the code-completion + /// results. + /// + /// \param IncludeCodePatterns Whether to include code patterns (such as a + /// for loop) in the code-completion results. /// /// FIXME: The Diag, LangOpts, SourceMgr, FileMgr, and /// StoredDiagnostics parameters are all disgusting hacks. They will /// go away. void CodeComplete(llvm::StringRef File, unsigned Line, unsigned Column, RemappedFile *RemappedFiles, unsigned NumRemappedFiles, + bool IncludeMacros, bool IncludeCodePatterns, CodeCompleteConsumer &Consumer, Diagnostic &Diag, LangOptions &LangOpts, SourceManager &SourceMgr, FileManager &FileMgr, diff --git a/lib/Frontend/ASTUnit.cpp b/lib/Frontend/ASTUnit.cpp index 2d1708ccc6..7c15d82d3b 100644 --- a/lib/Frontend/ASTUnit.cpp +++ b/lib/Frontend/ASTUnit.cpp @@ -1154,6 +1154,8 @@ bool ASTUnit::Reparse(RemappedFile *RemappedFiles, unsigned NumRemappedFiles) { void ASTUnit::CodeComplete(llvm::StringRef File, unsigned Line, unsigned Column, RemappedFile *RemappedFiles, unsigned NumRemappedFiles, + bool IncludeMacros, + bool IncludeCodePatterns, CodeCompleteConsumer &Consumer, Diagnostic &Diag, LangOptions &LangOpts, SourceManager &SourceMgr, FileManager &FileMgr, @@ -1164,13 +1166,18 @@ void ASTUnit::CodeComplete(llvm::StringRef File, unsigned Line, unsigned Column, CompilerInvocation CCInvocation(*Invocation); FrontendOptions &FrontendOpts = CCInvocation.getFrontendOpts(); PreprocessorOptions &PreprocessorOpts = CCInvocation.getPreprocessorOpts(); - - FrontendOpts.ShowMacrosInCodeCompletion = 1; - FrontendOpts.ShowCodePatternsInCodeCompletion = 1; + + FrontendOpts.ShowMacrosInCodeCompletion = IncludeMacros; + FrontendOpts.ShowCodePatternsInCodeCompletion = IncludeCodePatterns; FrontendOpts.CodeCompletionAt.FileName = File; FrontendOpts.CodeCompletionAt.Line = Line; FrontendOpts.CodeCompletionAt.Column = Column; + // Turn on spell-checking when performing code completion. It leads + // to better results. + unsigned SpellChecking = CCInvocation.getLangOpts().SpellChecking; + CCInvocation.getLangOpts().SpellChecking = 1; + // Set the language options appropriately. LangOpts = CCInvocation.getLangOpts(); @@ -1235,4 +1242,5 @@ void ASTUnit::CodeComplete(llvm::StringRef File, unsigned Line, unsigned Column, Clang.takeInvocation(); Clang.takeDiagnosticClient(); Clang.takeCodeCompletionConsumer(); + CCInvocation.getLangOpts().SpellChecking = SpellChecking; } diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c index 8146213225..1530aa8697 100644 --- a/tools/c-index-test/c-index-test.c +++ b/tools/c-index-test/c-index-test.c @@ -896,7 +896,8 @@ int perform_code_completion(int argc, const char **argv, int timing_only) { num_unsaved_files, getDefaultParsingOptions()); results = clang_codeCompleteAt(TU, filename, line, column, - unsaved_files, num_unsaved_files); + unsaved_files, num_unsaved_files, + clang_defaultCodeCompleteOptions()); } else results = clang_codeComplete(CIdx, argv[argc - 1], argc - num_unsaved_files - 3, diff --git a/tools/libclang/CIndexCodeCompletion.cpp b/tools/libclang/CIndexCodeCompletion.cpp index 2aa644b757..4ab6b9bd91 100644 --- a/tools/libclang/CIndexCodeCompletion.cpp +++ b/tools/libclang/CIndexCodeCompletion.cpp @@ -569,7 +569,8 @@ CXCodeCompleteResults *clang_codeCompleteAt(CXTranslationUnit TU, unsigned complete_line, unsigned complete_column, struct CXUnsavedFile *unsaved_files, - unsigned num_unsaved_files) { + unsigned num_unsaved_files, + unsigned options) { #ifdef UDP_CODE_COMPLETION_LOGGER #ifdef UDP_CODE_COMPLETION_LOGGER_PORT const llvm::TimeRecord &StartTime = llvm::TimeRecord::getCurrentTime(); @@ -611,7 +612,10 @@ CXCodeCompleteResults *clang_codeCompleteAt(CXTranslationUnit TU, // Perform completion. AST->CodeComplete(complete_filename, complete_line, complete_column, - RemappedFiles.data(), RemappedFiles.size(), Capture, + RemappedFiles.data(), RemappedFiles.size(), + (options & CXCodeComplete_IncludeMacros), + (options & CXCodeComplete_IncludeCodePatterns), + Capture, *Results->Diag, Results->LangOpts, Results->SourceMgr, Results->FileMgr, Results->Diagnostics); @@ -692,6 +696,10 @@ CXCodeCompleteResults *clang_codeCompleteAt(CXTranslationUnit TU, return Results; } +unsigned clang_defaultCodeCompleteOptions(void) { + return CXCodeComplete_IncludeMacros; +} + void clang_disposeCodeCompleteResults(CXCodeCompleteResults *ResultsIn) { if (!ResultsIn) return; diff --git a/tools/libclang/libclang.darwin.exports b/tools/libclang/libclang.darwin.exports index b971ed3838..8ebdf9916e 100644 --- a/tools/libclang/libclang.darwin.exports +++ b/tools/libclang/libclang.darwin.exports @@ -13,6 +13,7 @@ _clang_constructUSR_ObjCProtocol _clang_createIndex _clang_createTranslationUnit _clang_createTranslationUnitFromSourceFile +_clang_defaultCodeCompleteOptions _clang_defaultDiagnosticDisplayOptions _clang_disposeCodeCompleteResults _clang_disposeDiagnostic diff --git a/tools/libclang/libclang.exports b/tools/libclang/libclang.exports index b5533fcbcf..793b042808 100644 --- a/tools/libclang/libclang.exports +++ b/tools/libclang/libclang.exports @@ -13,6 +13,7 @@ clang_constructUSR_ObjCProtocol clang_createIndex clang_createTranslationUnit clang_createTranslationUnitFromSourceFile +clang_defaultCodeCompleteOptions clang_defaultDiagnosticDisplayOptions clang_disposeCodeCompleteResults clang_disposeDiagnostic -- 2.40.0