Kirill Bobyrev | 8e35f1e | 2018-08-14 16:03:32 +0000 | [diff] [blame] | 1 | //===--- CodeComplete.cpp ----------------------------------------*- C++-*-===// |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
Kirill Bobyrev | 8e35f1e | 2018-08-14 16:03:32 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 9 | // |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 10 | // Code completion has several moving parts: |
| 11 | // - AST-based completions are provided using the completion hooks in Sema. |
| 12 | // - external completions are retrieved from the index (using hints from Sema) |
| 13 | // - the two sources overlap, and must be merged and overloads bundled |
| 14 | // - results must be scored and ranked (see Quality.h) before rendering |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 15 | // |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 16 | // Signature help works in a similar way as code completion, but it is simpler: |
| 17 | // it's purely AST-based, and there are few candidates. |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 18 | // |
Kirill Bobyrev | 8e35f1e | 2018-08-14 16:03:32 +0000 | [diff] [blame] | 19 | //===----------------------------------------------------------------------===// |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 20 | |
| 21 | #include "CodeComplete.h" |
Eric Liu | 7ad1696 | 2018-06-22 10:46:59 +0000 | [diff] [blame] | 22 | #include "AST.h" |
Eric Liu | b1d7542 | 2018-10-02 10:43:55 +0000 | [diff] [blame] | 23 | #include "ClangdUnit.h" |
Eric Liu | 63696e1 | 2017-12-20 17:24:31 +0000 | [diff] [blame] | 24 | #include "CodeCompletionStrings.h" |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 25 | #include "Compiler.h" |
Kadir Cetinkaya | 2f84d91 | 2018-08-08 08:59:29 +0000 | [diff] [blame] | 26 | #include "Diagnostics.h" |
Ilya Biryukov | 647da3e | 2018-11-26 15:38:01 +0000 | [diff] [blame] | 27 | #include "ExpectedTypes.h" |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 28 | #include "FileDistance.h" |
Sam McCall | 84652cc | 2018-01-12 16:16:09 +0000 | [diff] [blame] | 29 | #include "FuzzyMatch.h" |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 30 | #include "Headers.h" |
Eric Liu | 6f648df | 2017-12-19 16:50:37 +0000 | [diff] [blame] | 31 | #include "Logger.h" |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 32 | #include "Quality.h" |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 33 | #include "SourceCode.h" |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 34 | #include "TUScheduler.h" |
Sam McCall | 2b78016 | 2018-01-30 17:20:54 +0000 | [diff] [blame] | 35 | #include "Trace.h" |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 36 | #include "URI.h" |
Eric Liu | 6f648df | 2017-12-19 16:50:37 +0000 | [diff] [blame] | 37 | #include "index/Index.h" |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 38 | #include "clang/AST/Decl.h" |
| 39 | #include "clang/AST/DeclBase.h" |
Ilya Biryukov | c22d344 | 2018-05-16 12:32:49 +0000 | [diff] [blame] | 40 | #include "clang/Basic/LangOptions.h" |
Kadir Cetinkaya | a9c9d00 | 2018-08-13 08:23:01 +0000 | [diff] [blame] | 41 | #include "clang/Basic/SourceLocation.h" |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 42 | #include "clang/Format/Format.h" |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 43 | #include "clang/Frontend/CompilerInstance.h" |
| 44 | #include "clang/Frontend/FrontendActions.h" |
Sam McCall | ebef812 | 2018-09-14 12:36:06 +0000 | [diff] [blame] | 45 | #include "clang/Lex/PreprocessorOptions.h" |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 46 | #include "clang/Sema/CodeCompleteConsumer.h" |
| 47 | #include "clang/Sema/Sema.h" |
Eric Liu | 670c147 | 2018-09-27 18:46:00 +0000 | [diff] [blame] | 48 | #include "llvm/ADT/ArrayRef.h" |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 49 | #include "llvm/ADT/None.h" |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 50 | #include "llvm/ADT/Optional.h" |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 51 | #include "llvm/ADT/SmallVector.h" |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 52 | #include "llvm/Support/Error.h" |
Haojian Wu | ba28e9a | 2018-01-10 14:44:34 +0000 | [diff] [blame] | 53 | #include "llvm/Support/Format.h" |
Eric Liu | bc25ef7 | 2018-07-05 08:29:33 +0000 | [diff] [blame] | 54 | #include "llvm/Support/FormatVariadic.h" |
Sam McCall | 2161ec7 | 2018-07-05 06:20:41 +0000 | [diff] [blame] | 55 | #include "llvm/Support/ScopedPrinter.h" |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 56 | #include <algorithm> |
| 57 | #include <iterator> |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 58 | |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 59 | // We log detailed candidate here if you run with -debug-only=codecomplete. |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 60 | #define DEBUG_TYPE "CodeComplete" |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 61 | |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 62 | namespace clang { |
| 63 | namespace clangd { |
| 64 | namespace { |
| 65 | |
Eric Liu | 6f648df | 2017-12-19 16:50:37 +0000 | [diff] [blame] | 66 | CompletionItemKind toCompletionItemKind(index::SymbolKind Kind) { |
| 67 | using SK = index::SymbolKind; |
| 68 | switch (Kind) { |
| 69 | case SK::Unknown: |
| 70 | return CompletionItemKind::Missing; |
| 71 | case SK::Module: |
| 72 | case SK::Namespace: |
| 73 | case SK::NamespaceAlias: |
| 74 | return CompletionItemKind::Module; |
| 75 | case SK::Macro: |
| 76 | return CompletionItemKind::Text; |
| 77 | case SK::Enum: |
| 78 | return CompletionItemKind::Enum; |
| 79 | // FIXME(ioeric): use LSP struct instead of class when it is suppoted in the |
| 80 | // protocol. |
| 81 | case SK::Struct: |
| 82 | case SK::Class: |
| 83 | case SK::Protocol: |
| 84 | case SK::Extension: |
| 85 | case SK::Union: |
| 86 | return CompletionItemKind::Class; |
| 87 | // FIXME(ioeric): figure out whether reference is the right type for aliases. |
| 88 | case SK::TypeAlias: |
| 89 | case SK::Using: |
| 90 | return CompletionItemKind::Reference; |
| 91 | case SK::Function: |
| 92 | // FIXME(ioeric): this should probably be an operator. This should be fixed |
| 93 | // when `Operator` is support type in the protocol. |
| 94 | case SK::ConversionFunction: |
| 95 | return CompletionItemKind::Function; |
| 96 | case SK::Variable: |
| 97 | case SK::Parameter: |
| 98 | return CompletionItemKind::Variable; |
| 99 | case SK::Field: |
| 100 | return CompletionItemKind::Field; |
| 101 | // FIXME(ioeric): use LSP enum constant when it is supported in the protocol. |
| 102 | case SK::EnumConstant: |
| 103 | return CompletionItemKind::Value; |
| 104 | case SK::InstanceMethod: |
| 105 | case SK::ClassMethod: |
| 106 | case SK::StaticMethod: |
| 107 | case SK::Destructor: |
| 108 | return CompletionItemKind::Method; |
| 109 | case SK::InstanceProperty: |
| 110 | case SK::ClassProperty: |
| 111 | case SK::StaticProperty: |
| 112 | return CompletionItemKind::Property; |
| 113 | case SK::Constructor: |
| 114 | return CompletionItemKind::Constructor; |
| 115 | } |
| 116 | llvm_unreachable("Unhandled clang::index::SymbolKind."); |
| 117 | } |
| 118 | |
Sam McCall | 8330589 | 2018-06-08 21:17:19 +0000 | [diff] [blame] | 119 | CompletionItemKind |
| 120 | toCompletionItemKind(CodeCompletionResult::ResultKind ResKind, |
Sam McCall | 4c077f9 | 2018-09-18 09:08:28 +0000 | [diff] [blame] | 121 | const NamedDecl *Decl, |
| 122 | CodeCompletionContext::Kind CtxKind) { |
Sam McCall | 8330589 | 2018-06-08 21:17:19 +0000 | [diff] [blame] | 123 | if (Decl) |
| 124 | return toCompletionItemKind(index::getSymbolInfo(Decl).Kind); |
Sam McCall | 4c077f9 | 2018-09-18 09:08:28 +0000 | [diff] [blame] | 125 | if (CtxKind == CodeCompletionContext::CCC_IncludedFile) |
| 126 | return CompletionItemKind::File; |
Sam McCall | 8330589 | 2018-06-08 21:17:19 +0000 | [diff] [blame] | 127 | switch (ResKind) { |
| 128 | case CodeCompletionResult::RK_Declaration: |
| 129 | llvm_unreachable("RK_Declaration without Decl"); |
| 130 | case CodeCompletionResult::RK_Keyword: |
| 131 | return CompletionItemKind::Keyword; |
| 132 | case CodeCompletionResult::RK_Macro: |
| 133 | return CompletionItemKind::Text; // unfortunately, there's no 'Macro' |
| 134 | // completion items in LSP. |
| 135 | case CodeCompletionResult::RK_Pattern: |
| 136 | return CompletionItemKind::Snippet; |
| 137 | } |
| 138 | llvm_unreachable("Unhandled CodeCompletionResult::ResultKind."); |
| 139 | } |
| 140 | |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 141 | /// Get the optional chunk as a string. This function is possibly recursive. |
| 142 | /// |
| 143 | /// The parameter info for each parameter is appended to the Parameters. |
Kadir Cetinkaya | e486e37 | 2018-08-13 08:40:05 +0000 | [diff] [blame] | 144 | std::string getOptionalParameters(const CodeCompletionString &CCS, |
| 145 | std::vector<ParameterInformation> &Parameters, |
| 146 | SignatureQualitySignals &Signal) { |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 147 | std::string Result; |
| 148 | for (const auto &Chunk : CCS) { |
| 149 | switch (Chunk.Kind) { |
| 150 | case CodeCompletionString::CK_Optional: |
| 151 | assert(Chunk.Optional && |
| 152 | "Expected the optional code completion string to be non-null."); |
Kadir Cetinkaya | e486e37 | 2018-08-13 08:40:05 +0000 | [diff] [blame] | 153 | Result += getOptionalParameters(*Chunk.Optional, Parameters, Signal); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 154 | break; |
| 155 | case CodeCompletionString::CK_VerticalSpace: |
| 156 | break; |
| 157 | case CodeCompletionString::CK_Placeholder: |
| 158 | // A string that acts as a placeholder for, e.g., a function call |
| 159 | // argument. |
| 160 | // Intentional fallthrough here. |
| 161 | case CodeCompletionString::CK_CurrentParameter: { |
| 162 | // A piece of text that describes the parameter that corresponds to |
| 163 | // the code-completion location within a function call, message send, |
| 164 | // macro invocation, etc. |
| 165 | Result += Chunk.Text; |
| 166 | ParameterInformation Info; |
| 167 | Info.label = Chunk.Text; |
| 168 | Parameters.push_back(std::move(Info)); |
Kadir Cetinkaya | e486e37 | 2018-08-13 08:40:05 +0000 | [diff] [blame] | 169 | Signal.ContainsActiveParameter = true; |
| 170 | Signal.NumberOfOptionalParameters++; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 171 | break; |
| 172 | } |
| 173 | default: |
| 174 | Result += Chunk.Text; |
| 175 | break; |
| 176 | } |
| 177 | } |
| 178 | return Result; |
| 179 | } |
| 180 | |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 181 | /// Creates a `HeaderFile` from \p Header which can be either a URI or a literal |
| 182 | /// include. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 183 | static llvm::Expected<HeaderFile> toHeaderFile(llvm::StringRef Header, |
| 184 | llvm::StringRef HintPath) { |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 185 | if (isLiteralInclude(Header)) |
| 186 | return HeaderFile{Header.str(), /*Verbatim=*/true}; |
| 187 | auto U = URI::parse(Header); |
| 188 | if (!U) |
| 189 | return U.takeError(); |
| 190 | |
| 191 | auto IncludePath = URI::includeSpelling(*U); |
| 192 | if (!IncludePath) |
| 193 | return IncludePath.takeError(); |
| 194 | if (!IncludePath->empty()) |
| 195 | return HeaderFile{std::move(*IncludePath), /*Verbatim=*/true}; |
| 196 | |
| 197 | auto Resolved = URI::resolve(*U, HintPath); |
| 198 | if (!Resolved) |
| 199 | return Resolved.takeError(); |
| 200 | return HeaderFile{std::move(*Resolved), /*Verbatim=*/false}; |
| 201 | } |
| 202 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 203 | /// A code completion result, in clang-native form. |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 204 | /// It may be promoted to a CompletionItem if it's among the top-ranked results. |
| 205 | struct CompletionCandidate { |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 206 | llvm::StringRef Name; // Used for filtering and sorting. |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 207 | // We may have a result from Sema, from the index, or both. |
| 208 | const CodeCompletionResult *SemaResult = nullptr; |
| 209 | const Symbol *IndexResult = nullptr; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 210 | llvm::SmallVector<llvm::StringRef, 1> RankedIncludeHeaders; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 211 | |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 212 | // Returns a token identifying the overload set this is part of. |
| 213 | // 0 indicates it's not part of any overload set. |
| 214 | size_t overloadSet() const { |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 215 | llvm::SmallString<256> Scratch; |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 216 | if (IndexResult) { |
| 217 | switch (IndexResult->SymInfo.Kind) { |
| 218 | case index::SymbolKind::ClassMethod: |
| 219 | case index::SymbolKind::InstanceMethod: |
| 220 | case index::SymbolKind::StaticMethod: |
Fangrui Song | 12e4ee7 | 2018-11-02 05:59:29 +0000 | [diff] [blame] | 221 | #ifndef NDEBUG |
| 222 | llvm_unreachable("Don't expect members from index in code completion"); |
| 223 | #else |
Fangrui Song | a3ed05b | 2018-11-02 04:23:50 +0000 | [diff] [blame] | 224 | LLVM_FALLTHROUGH; |
Fangrui Song | 12e4ee7 | 2018-11-02 05:59:29 +0000 | [diff] [blame] | 225 | #endif |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 226 | case index::SymbolKind::Function: |
| 227 | // We can't group overloads together that need different #includes. |
| 228 | // This could break #include insertion. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 229 | return llvm::hash_combine( |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 230 | (IndexResult->Scope + IndexResult->Name).toStringRef(Scratch), |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 231 | headerToInsertIfAllowed().getValueOr("")); |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 232 | default: |
| 233 | return 0; |
| 234 | } |
| 235 | } |
| 236 | assert(SemaResult); |
| 237 | // We need to make sure we're consistent with the IndexResult case! |
| 238 | const NamedDecl *D = SemaResult->Declaration; |
| 239 | if (!D || !D->isFunctionOrFunctionTemplate()) |
| 240 | return 0; |
| 241 | { |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 242 | llvm::raw_svector_ostream OS(Scratch); |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 243 | D->printQualifiedName(OS); |
| 244 | } |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 245 | return llvm::hash_combine(Scratch, |
| 246 | headerToInsertIfAllowed().getValueOr("")); |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 249 | // The best header to include if include insertion is allowed. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 250 | llvm::Optional<llvm::StringRef> headerToInsertIfAllowed() const { |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 251 | if (RankedIncludeHeaders.empty()) |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 252 | return None; |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 253 | if (SemaResult && SemaResult->Declaration) { |
| 254 | // Avoid inserting new #include if the declaration is found in the current |
| 255 | // file e.g. the symbol is forward declared. |
| 256 | auto &SM = SemaResult->Declaration->getASTContext().getSourceManager(); |
| 257 | for (const Decl *RD : SemaResult->Declaration->redecls()) |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 258 | if (SM.isInMainFile(SM.getExpansionLoc(RD->getBeginLoc()))) |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 259 | return None; |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 260 | } |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 261 | return RankedIncludeHeaders[0]; |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 264 | using Bundle = llvm::SmallVector<CompletionCandidate, 4>; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 265 | }; |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 266 | using ScoredBundle = |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 267 | std::pair<CompletionCandidate::Bundle, CodeCompletion::Scores>; |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 268 | struct ScoredBundleGreater { |
| 269 | bool operator()(const ScoredBundle &L, const ScoredBundle &R) { |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 270 | if (L.second.Total != R.second.Total) |
| 271 | return L.second.Total > R.second.Total; |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 272 | return L.first.front().Name < |
| 273 | R.first.front().Name; // Earlier name is better. |
| 274 | } |
| 275 | }; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 276 | |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 277 | // Assembles a code completion out of a bundle of >=1 completion candidates. |
| 278 | // Many of the expensive strings are only computed at this point, once we know |
| 279 | // the candidate bundle is going to be returned. |
| 280 | // |
| 281 | // Many fields are the same for all candidates in a bundle (e.g. name), and are |
| 282 | // computed from the first candidate, in the constructor. |
| 283 | // Others vary per candidate, so add() must be called for remaining candidates. |
| 284 | struct CodeCompletionBuilder { |
| 285 | CodeCompletionBuilder(ASTContext &ASTCtx, const CompletionCandidate &C, |
| 286 | CodeCompletionString *SemaCCS, |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 287 | llvm::ArrayRef<std::string> QueryScopes, |
| 288 | const IncludeInserter &Includes, |
| 289 | llvm::StringRef FileName, |
Sam McCall | 4c077f9 | 2018-09-18 09:08:28 +0000 | [diff] [blame] | 290 | CodeCompletionContext::Kind ContextKind, |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 291 | const CodeCompleteOptions &Opts) |
Kadir Cetinkaya | 516fcda | 2018-08-23 12:19:39 +0000 | [diff] [blame] | 292 | : ASTCtx(ASTCtx), ExtractDocumentation(Opts.IncludeComments), |
| 293 | EnableFunctionArgSnippets(Opts.EnableFunctionArgSnippets) { |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 294 | add(C, SemaCCS); |
| 295 | if (C.SemaResult) { |
Sam McCall | 4e5742a | 2018-07-06 11:50:49 +0000 | [diff] [blame] | 296 | Completion.Origin |= SymbolOrigin::AST; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 297 | Completion.Name = llvm::StringRef(SemaCCS->getTypedText()); |
Eric Liu | f433c2d | 2018-07-18 15:31:14 +0000 | [diff] [blame] | 298 | if (Completion.Scope.empty()) { |
| 299 | if ((C.SemaResult->Kind == CodeCompletionResult::RK_Declaration) || |
| 300 | (C.SemaResult->Kind == CodeCompletionResult::RK_Pattern)) |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 301 | if (const auto *D = C.SemaResult->getDeclaration()) |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 302 | if (const auto *ND = dyn_cast<NamedDecl>(D)) |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 303 | Completion.Scope = |
| 304 | splitQualifiedName(printQualifiedName(*ND)).first; |
Eric Liu | f433c2d | 2018-07-18 15:31:14 +0000 | [diff] [blame] | 305 | } |
Sam McCall | 4c077f9 | 2018-09-18 09:08:28 +0000 | [diff] [blame] | 306 | Completion.Kind = toCompletionItemKind( |
| 307 | C.SemaResult->Kind, C.SemaResult->Declaration, ContextKind); |
Kadir Cetinkaya | 0ed5d29 | 2018-09-27 14:21:07 +0000 | [diff] [blame] | 308 | // Sema could provide more info on whether the completion was a file or |
| 309 | // folder. |
| 310 | if (Completion.Kind == CompletionItemKind::File && |
| 311 | Completion.Name.back() == '/') |
| 312 | Completion.Kind = CompletionItemKind::Folder; |
Kadir Cetinkaya | 2f84d91 | 2018-08-08 08:59:29 +0000 | [diff] [blame] | 313 | for (const auto &FixIt : C.SemaResult->FixIts) { |
| 314 | Completion.FixIts.push_back( |
| 315 | toTextEdit(FixIt, ASTCtx.getSourceManager(), ASTCtx.getLangOpts())); |
| 316 | } |
Kirill Bobyrev | 4a5ff88 | 2018-10-07 14:49:41 +0000 | [diff] [blame] | 317 | llvm::sort(Completion.FixIts, [](const TextEdit &X, const TextEdit &Y) { |
| 318 | return std::tie(X.range.start.line, X.range.start.character) < |
| 319 | std::tie(Y.range.start.line, Y.range.start.character); |
| 320 | }); |
Eric Liu | 6df6600 | 2018-09-06 18:52:26 +0000 | [diff] [blame] | 321 | Completion.Deprecated |= |
| 322 | (C.SemaResult->Availability == CXAvailability_Deprecated); |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 323 | } |
| 324 | if (C.IndexResult) { |
Sam McCall | 4e5742a | 2018-07-06 11:50:49 +0000 | [diff] [blame] | 325 | Completion.Origin |= C.IndexResult->Origin; |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 326 | if (Completion.Scope.empty()) |
| 327 | Completion.Scope = C.IndexResult->Scope; |
| 328 | if (Completion.Kind == CompletionItemKind::Missing) |
| 329 | Completion.Kind = toCompletionItemKind(C.IndexResult->SymInfo.Kind); |
| 330 | if (Completion.Name.empty()) |
| 331 | Completion.Name = C.IndexResult->Name; |
Eric Liu | 670c147 | 2018-09-27 18:46:00 +0000 | [diff] [blame] | 332 | // If the completion was visible to Sema, no qualifier is needed. This |
| 333 | // avoids unneeded qualifiers in cases like with `using ns::X`. |
| 334 | if (Completion.RequiredQualifier.empty() && !C.SemaResult) { |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 335 | llvm::StringRef ShortestQualifier = C.IndexResult->Scope; |
| 336 | for (llvm::StringRef Scope : QueryScopes) { |
| 337 | llvm::StringRef Qualifier = C.IndexResult->Scope; |
Eric Liu | 670c147 | 2018-09-27 18:46:00 +0000 | [diff] [blame] | 338 | if (Qualifier.consume_front(Scope) && |
| 339 | Qualifier.size() < ShortestQualifier.size()) |
| 340 | ShortestQualifier = Qualifier; |
| 341 | } |
| 342 | Completion.RequiredQualifier = ShortestQualifier; |
| 343 | } |
Eric Liu | 6df6600 | 2018-09-06 18:52:26 +0000 | [diff] [blame] | 344 | Completion.Deprecated |= (C.IndexResult->Flags & Symbol::Deprecated); |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 345 | } |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 346 | |
| 347 | // Turn absolute path into a literal string that can be #included. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 348 | auto Inserted = [&](llvm::StringRef Header) |
| 349 | -> llvm::Expected<std::pair<std::string, bool>> { |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 350 | auto ResolvedDeclaring = |
| 351 | toHeaderFile(C.IndexResult->CanonicalDeclaration.FileURI, FileName); |
| 352 | if (!ResolvedDeclaring) |
| 353 | return ResolvedDeclaring.takeError(); |
| 354 | auto ResolvedInserted = toHeaderFile(Header, FileName); |
| 355 | if (!ResolvedInserted) |
| 356 | return ResolvedInserted.takeError(); |
| 357 | return std::make_pair( |
| 358 | Includes.calculateIncludePath(*ResolvedDeclaring, *ResolvedInserted), |
| 359 | Includes.shouldInsertInclude(*ResolvedDeclaring, *ResolvedInserted)); |
| 360 | }; |
| 361 | bool ShouldInsert = C.headerToInsertIfAllowed().hasValue(); |
| 362 | // Calculate include paths and edits for all possible headers. |
| 363 | for (const auto &Inc : C.RankedIncludeHeaders) { |
| 364 | if (auto ToInclude = Inserted(Inc)) { |
| 365 | CodeCompletion::IncludeCandidate Include; |
| 366 | Include.Header = ToInclude->first; |
| 367 | if (ToInclude->second && ShouldInsert) |
| 368 | Include.Insertion = Includes.insert(ToInclude->first); |
| 369 | Completion.Includes.push_back(std::move(Include)); |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 370 | } else |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 371 | log("Failed to generate include insertion edits for adding header " |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 372 | "(FileURI='{0}', IncludeHeader='{1}') into {2}", |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 373 | C.IndexResult->CanonicalDeclaration.FileURI, Inc, FileName); |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 374 | } |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 375 | // Prefer includes that do not need edits (i.e. already exist). |
| 376 | std::stable_partition(Completion.Includes.begin(), |
| 377 | Completion.Includes.end(), |
| 378 | [](const CodeCompletion::IncludeCandidate &I) { |
| 379 | return !I.Insertion.hasValue(); |
| 380 | }); |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | void add(const CompletionCandidate &C, CodeCompletionString *SemaCCS) { |
| 384 | assert(bool(C.SemaResult) == bool(SemaCCS)); |
| 385 | Bundled.emplace_back(); |
| 386 | BundledEntry &S = Bundled.back(); |
| 387 | if (C.SemaResult) { |
| 388 | getSignature(*SemaCCS, &S.Signature, &S.SnippetSuffix, |
| 389 | &Completion.RequiredQualifier); |
| 390 | S.ReturnType = getReturnType(*SemaCCS); |
| 391 | } else if (C.IndexResult) { |
| 392 | S.Signature = C.IndexResult->Signature; |
| 393 | S.SnippetSuffix = C.IndexResult->CompletionSnippetSuffix; |
Sam McCall | 2e5700f | 2018-08-31 13:55:01 +0000 | [diff] [blame] | 394 | S.ReturnType = C.IndexResult->ReturnType; |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 395 | } |
| 396 | if (ExtractDocumentation && Completion.Documentation.empty()) { |
Sam McCall | 2e5700f | 2018-08-31 13:55:01 +0000 | [diff] [blame] | 397 | if (C.IndexResult) |
| 398 | Completion.Documentation = C.IndexResult->Documentation; |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 399 | else if (C.SemaResult) |
| 400 | Completion.Documentation = getDocComment(ASTCtx, *C.SemaResult, |
| 401 | /*CommentsFromHeader=*/false); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | CodeCompletion build() { |
| 406 | Completion.ReturnType = summarizeReturnType(); |
| 407 | Completion.Signature = summarizeSignature(); |
| 408 | Completion.SnippetSuffix = summarizeSnippet(); |
| 409 | Completion.BundleSize = Bundled.size(); |
| 410 | return std::move(Completion); |
| 411 | } |
| 412 | |
| 413 | private: |
| 414 | struct BundledEntry { |
| 415 | std::string SnippetSuffix; |
| 416 | std::string Signature; |
| 417 | std::string ReturnType; |
| 418 | }; |
| 419 | |
| 420 | // If all BundledEntrys have the same value for a property, return it. |
| 421 | template <std::string BundledEntry::*Member> |
| 422 | const std::string *onlyValue() const { |
| 423 | auto B = Bundled.begin(), E = Bundled.end(); |
| 424 | for (auto I = B + 1; I != E; ++I) |
| 425 | if (I->*Member != B->*Member) |
| 426 | return nullptr; |
| 427 | return &(B->*Member); |
| 428 | } |
| 429 | |
Kadir Cetinkaya | f8b85a3 | 2018-08-23 13:14:50 +0000 | [diff] [blame] | 430 | template <bool BundledEntry::*Member> const bool *onlyValue() const { |
| 431 | auto B = Bundled.begin(), E = Bundled.end(); |
| 432 | for (auto I = B + 1; I != E; ++I) |
| 433 | if (I->*Member != B->*Member) |
| 434 | return nullptr; |
| 435 | return &(B->*Member); |
| 436 | } |
| 437 | |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 438 | std::string summarizeReturnType() const { |
| 439 | if (auto *RT = onlyValue<&BundledEntry::ReturnType>()) |
| 440 | return *RT; |
| 441 | return ""; |
| 442 | } |
| 443 | |
| 444 | std::string summarizeSnippet() const { |
Kadir Cetinkaya | 516fcda | 2018-08-23 12:19:39 +0000 | [diff] [blame] | 445 | auto *Snippet = onlyValue<&BundledEntry::SnippetSuffix>(); |
| 446 | if (!Snippet) |
| 447 | // All bundles are function calls. |
Ilya Biryukov | 4f98470 | 2018-09-26 05:45:31 +0000 | [diff] [blame] | 448 | // FIXME(ibiryukov): sometimes add template arguments to a snippet, e.g. |
| 449 | // we need to complete 'forward<$1>($0)'. |
Kadir Cetinkaya | 516fcda | 2018-08-23 12:19:39 +0000 | [diff] [blame] | 450 | return "($0)"; |
Ilya Biryukov | 4f98470 | 2018-09-26 05:45:31 +0000 | [diff] [blame] | 451 | if (EnableFunctionArgSnippets) |
| 452 | return *Snippet; |
| 453 | |
| 454 | // Replace argument snippets with a simplified pattern. |
| 455 | if (Snippet->empty()) |
| 456 | return ""; |
| 457 | if (Completion.Kind == CompletionItemKind::Function || |
| 458 | Completion.Kind == CompletionItemKind::Method) { |
| 459 | // Functions snippets can be of 2 types: |
| 460 | // - containing only function arguments, e.g. |
| 461 | // foo(${1:int p1}, ${2:int p2}); |
| 462 | // We transform this pattern to '($0)' or '()'. |
| 463 | // - template arguments and function arguments, e.g. |
| 464 | // foo<${1:class}>(${2:int p1}). |
| 465 | // We transform this pattern to '<$1>()$0' or '<$0>()'. |
| 466 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 467 | bool EmptyArgs = llvm::StringRef(*Snippet).endswith("()"); |
Ilya Biryukov | 4f98470 | 2018-09-26 05:45:31 +0000 | [diff] [blame] | 468 | if (Snippet->front() == '<') |
| 469 | return EmptyArgs ? "<$1>()$0" : "<$1>($0)"; |
| 470 | if (Snippet->front() == '(') |
| 471 | return EmptyArgs ? "()" : "($0)"; |
| 472 | return *Snippet; // Not an arg snippet? |
| 473 | } |
| 474 | if (Completion.Kind == CompletionItemKind::Reference || |
| 475 | Completion.Kind == CompletionItemKind::Class) { |
| 476 | if (Snippet->front() != '<') |
| 477 | return *Snippet; // Not an arg snippet? |
| 478 | |
| 479 | // Classes and template using aliases can only have template arguments, |
| 480 | // e.g. Foo<${1:class}>. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 481 | if (llvm::StringRef(*Snippet).endswith("<>")) |
Ilya Biryukov | 4f98470 | 2018-09-26 05:45:31 +0000 | [diff] [blame] | 482 | return "<>"; // can happen with defaulted template arguments. |
| 483 | return "<$0>"; |
| 484 | } |
Kadir Cetinkaya | 516fcda | 2018-08-23 12:19:39 +0000 | [diff] [blame] | 485 | return *Snippet; |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | std::string summarizeSignature() const { |
| 489 | if (auto *Signature = onlyValue<&BundledEntry::Signature>()) |
| 490 | return *Signature; |
| 491 | // All bundles are function calls. |
| 492 | return "(…)"; |
| 493 | } |
| 494 | |
| 495 | ASTContext &ASTCtx; |
| 496 | CodeCompletion Completion; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 497 | llvm::SmallVector<BundledEntry, 1> Bundled; |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 498 | bool ExtractDocumentation; |
Kadir Cetinkaya | 516fcda | 2018-08-23 12:19:39 +0000 | [diff] [blame] | 499 | bool EnableFunctionArgSnippets; |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 500 | }; |
| 501 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 502 | // Determine the symbol ID for a Sema code completion result, if possible. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 503 | llvm::Optional<SymbolID> getSymbolID(const CodeCompletionResult &R, |
| 504 | const SourceManager &SM) { |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 505 | switch (R.Kind) { |
| 506 | case CodeCompletionResult::RK_Declaration: |
| 507 | case CodeCompletionResult::RK_Pattern: { |
Haojian Wu | c6ddb46 | 2018-08-07 08:57:52 +0000 | [diff] [blame] | 508 | return clang::clangd::getSymbolID(R.Declaration); |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 509 | } |
| 510 | case CodeCompletionResult::RK_Macro: |
Eric Liu | d25f121 | 2018-09-06 09:59:37 +0000 | [diff] [blame] | 511 | return clang::clangd::getSymbolID(*R.Macro, R.MacroDefInfo, SM); |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 512 | case CodeCompletionResult::RK_Keyword: |
| 513 | return None; |
| 514 | } |
| 515 | llvm_unreachable("unknown CodeCompletionResult kind"); |
| 516 | } |
| 517 | |
Haojian Wu | 061c73e | 2018-01-23 11:37:26 +0000 | [diff] [blame] | 518 | // Scopes of the paritial identifier we're trying to complete. |
| 519 | // It is used when we query the index for more completion results. |
Eric Liu | 6f648df | 2017-12-19 16:50:37 +0000 | [diff] [blame] | 520 | struct SpecifiedScope { |
Haojian Wu | 061c73e | 2018-01-23 11:37:26 +0000 | [diff] [blame] | 521 | // The scopes we should look in, determined by Sema. |
| 522 | // |
| 523 | // If the qualifier was fully resolved, we look for completions in these |
| 524 | // scopes; if there is an unresolved part of the qualifier, it should be |
| 525 | // resolved within these scopes. |
| 526 | // |
| 527 | // Examples of qualified completion: |
| 528 | // |
| 529 | // "::vec" => {""} |
| 530 | // "using namespace std; ::vec^" => {"", "std::"} |
| 531 | // "namespace ns {using namespace std;} ns::^" => {"ns::", "std::"} |
| 532 | // "std::vec^" => {""} // "std" unresolved |
| 533 | // |
| 534 | // Examples of unqualified completion: |
| 535 | // |
| 536 | // "vec^" => {""} |
| 537 | // "using namespace std; vec^" => {"", "std::"} |
| 538 | // "using namespace std; namespace ns { vec^ }" => {"ns::", "std::", ""} |
| 539 | // |
| 540 | // "" for global namespace, "ns::" for normal namespace. |
| 541 | std::vector<std::string> AccessibleScopes; |
| 542 | // The full scope qualifier as typed by the user (without the leading "::"). |
| 543 | // Set if the qualifier is not fully resolved by Sema. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 544 | llvm::Optional<std::string> UnresolvedQualifier; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 545 | |
Eric Liu | abbd713 | 2018-11-06 11:17:40 +0000 | [diff] [blame] | 546 | // Construct scopes being queried in indexes. The results are deduplicated. |
Haojian Wu | 061c73e | 2018-01-23 11:37:26 +0000 | [diff] [blame] | 547 | // This method format the scopes to match the index request representation. |
| 548 | std::vector<std::string> scopesForIndexQuery() { |
Eric Liu | abbd713 | 2018-11-06 11:17:40 +0000 | [diff] [blame] | 549 | std::set<std::string> Results; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 550 | for (llvm::StringRef AS : AccessibleScopes) |
Eric Liu | abbd713 | 2018-11-06 11:17:40 +0000 | [diff] [blame] | 551 | Results.insert( |
| 552 | ((UnresolvedQualifier ? *UnresolvedQualifier : "") + AS).str()); |
| 553 | return {Results.begin(), Results.end()}; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 554 | } |
Eric Liu | 6f648df | 2017-12-19 16:50:37 +0000 | [diff] [blame] | 555 | }; |
| 556 | |
Eric Liu | 670c147 | 2018-09-27 18:46:00 +0000 | [diff] [blame] | 557 | // Get all scopes that will be queried in indexes and whether symbols from |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 558 | // any scope is allowed. The first scope in the list is the preferred scope |
| 559 | // (e.g. enclosing namespace). |
Eric Liu | 670c147 | 2018-09-27 18:46:00 +0000 | [diff] [blame] | 560 | std::pair<std::vector<std::string>, bool> |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 561 | getQueryScopes(CodeCompletionContext &CCContext, const Sema &CCSema, |
Eric Liu | 670c147 | 2018-09-27 18:46:00 +0000 | [diff] [blame] | 562 | const CodeCompleteOptions &Opts) { |
Kirill Bobyrev | 5a267ed | 2018-05-29 11:50:51 +0000 | [diff] [blame] | 563 | auto GetAllAccessibleScopes = [](CodeCompletionContext &CCContext) { |
Haojian Wu | 061c73e | 2018-01-23 11:37:26 +0000 | [diff] [blame] | 564 | SpecifiedScope Info; |
Kirill Bobyrev | 5a267ed | 2018-05-29 11:50:51 +0000 | [diff] [blame] | 565 | for (auto *Context : CCContext.getVisitedContexts()) { |
Haojian Wu | 061c73e | 2018-01-23 11:37:26 +0000 | [diff] [blame] | 566 | if (isa<TranslationUnitDecl>(Context)) |
| 567 | Info.AccessibleScopes.push_back(""); // global namespace |
Mikael Holmen | fa41add | 2018-10-18 06:00:39 +0000 | [diff] [blame] | 568 | else if (isa<NamespaceDecl>(Context)) |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 569 | Info.AccessibleScopes.push_back(printNamespaceScope(*Context)); |
Haojian Wu | 061c73e | 2018-01-23 11:37:26 +0000 | [diff] [blame] | 570 | } |
| 571 | return Info; |
| 572 | }; |
| 573 | |
| 574 | auto SS = CCContext.getCXXScopeSpecifier(); |
| 575 | |
| 576 | // Unqualified completion (e.g. "vec^"). |
| 577 | if (!SS) { |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 578 | std::vector<std::string> Scopes; |
| 579 | std::string EnclosingScope = printNamespaceScope(*CCSema.CurContext); |
| 580 | Scopes.push_back(EnclosingScope); |
| 581 | for (auto &S : GetAllAccessibleScopes(CCContext).scopesForIndexQuery()) { |
| 582 | if (EnclosingScope != S) |
| 583 | Scopes.push_back(std::move(S)); |
| 584 | } |
Eric Liu | 670c147 | 2018-09-27 18:46:00 +0000 | [diff] [blame] | 585 | // Allow AllScopes completion only for there is no explicit scope qualifier. |
| 586 | return {Scopes, Opts.AllScopes}; |
Haojian Wu | 061c73e | 2018-01-23 11:37:26 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | // Qualified completion ("std::vec^"), we have two cases depending on whether |
| 590 | // the qualifier can be resolved by Sema. |
| 591 | if ((*SS)->isValid()) { // Resolved qualifier. |
Eric Liu | 670c147 | 2018-09-27 18:46:00 +0000 | [diff] [blame] | 592 | return {GetAllAccessibleScopes(CCContext).scopesForIndexQuery(), false}; |
Haojian Wu | 061c73e | 2018-01-23 11:37:26 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | // Unresolved qualifier. |
| 596 | // FIXME: When Sema can resolve part of a scope chain (e.g. |
| 597 | // "known::unknown::id"), we should expand the known part ("known::") rather |
| 598 | // than treating the whole thing as unknown. |
| 599 | SpecifiedScope Info; |
| 600 | Info.AccessibleScopes.push_back(""); // global namespace |
| 601 | |
| 602 | Info.UnresolvedQualifier = |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 603 | Lexer::getSourceText(CharSourceRange::getCharRange((*SS)->getRange()), |
| 604 | CCSema.SourceMgr, clang::LangOptions()) |
Kirill Bobyrev | 5a267ed | 2018-05-29 11:50:51 +0000 | [diff] [blame] | 605 | .ltrim("::"); |
Haojian Wu | 061c73e | 2018-01-23 11:37:26 +0000 | [diff] [blame] | 606 | // Sema excludes the trailing "::". |
| 607 | if (!Info.UnresolvedQualifier->empty()) |
| 608 | *Info.UnresolvedQualifier += "::"; |
| 609 | |
Eric Liu | 670c147 | 2018-09-27 18:46:00 +0000 | [diff] [blame] | 610 | return {Info.scopesForIndexQuery(), false}; |
Haojian Wu | 061c73e | 2018-01-23 11:37:26 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Eric Liu | 42abe41 | 2018-05-24 11:20:19 +0000 | [diff] [blame] | 613 | // Should we perform index-based completion in a context of the specified kind? |
| 614 | // FIXME: consider allowing completion, but restricting the result types. |
| 615 | bool contextAllowsIndex(enum CodeCompletionContext::Kind K) { |
| 616 | switch (K) { |
| 617 | case CodeCompletionContext::CCC_TopLevel: |
| 618 | case CodeCompletionContext::CCC_ObjCInterface: |
| 619 | case CodeCompletionContext::CCC_ObjCImplementation: |
| 620 | case CodeCompletionContext::CCC_ObjCIvarList: |
| 621 | case CodeCompletionContext::CCC_ClassStructUnion: |
| 622 | case CodeCompletionContext::CCC_Statement: |
| 623 | case CodeCompletionContext::CCC_Expression: |
| 624 | case CodeCompletionContext::CCC_ObjCMessageReceiver: |
| 625 | case CodeCompletionContext::CCC_EnumTag: |
| 626 | case CodeCompletionContext::CCC_UnionTag: |
| 627 | case CodeCompletionContext::CCC_ClassOrStructTag: |
| 628 | case CodeCompletionContext::CCC_ObjCProtocolName: |
| 629 | case CodeCompletionContext::CCC_Namespace: |
| 630 | case CodeCompletionContext::CCC_Type: |
Eric Liu | 42abe41 | 2018-05-24 11:20:19 +0000 | [diff] [blame] | 631 | case CodeCompletionContext::CCC_ParenthesizedExpression: |
| 632 | case CodeCompletionContext::CCC_ObjCInterfaceName: |
| 633 | case CodeCompletionContext::CCC_ObjCCategoryName: |
Kadir Cetinkaya | b915790 | 2018-10-24 15:24:29 +0000 | [diff] [blame] | 634 | case CodeCompletionContext::CCC_Symbol: |
| 635 | case CodeCompletionContext::CCC_SymbolOrNewName: |
Eric Liu | 42abe41 | 2018-05-24 11:20:19 +0000 | [diff] [blame] | 636 | return true; |
Eric Liu | 42abe41 | 2018-05-24 11:20:19 +0000 | [diff] [blame] | 637 | case CodeCompletionContext::CCC_OtherWithMacros: |
| 638 | case CodeCompletionContext::CCC_DotMemberAccess: |
| 639 | case CodeCompletionContext::CCC_ArrowMemberAccess: |
| 640 | case CodeCompletionContext::CCC_ObjCPropertyAccess: |
| 641 | case CodeCompletionContext::CCC_MacroName: |
| 642 | case CodeCompletionContext::CCC_MacroNameUse: |
| 643 | case CodeCompletionContext::CCC_PreprocessorExpression: |
| 644 | case CodeCompletionContext::CCC_PreprocessorDirective: |
Eric Liu | 42abe41 | 2018-05-24 11:20:19 +0000 | [diff] [blame] | 645 | case CodeCompletionContext::CCC_SelectorName: |
| 646 | case CodeCompletionContext::CCC_TypeQualifiers: |
| 647 | case CodeCompletionContext::CCC_ObjCInstanceMessage: |
| 648 | case CodeCompletionContext::CCC_ObjCClassMessage: |
Sam McCall | 4c077f9 | 2018-09-18 09:08:28 +0000 | [diff] [blame] | 649 | case CodeCompletionContext::CCC_IncludedFile: |
Kadir Cetinkaya | b915790 | 2018-10-24 15:24:29 +0000 | [diff] [blame] | 650 | // FIXME: Provide identifier based completions for the following contexts: |
| 651 | case CodeCompletionContext::CCC_Other: // Be conservative. |
| 652 | case CodeCompletionContext::CCC_NaturalLanguage: |
Eric Liu | 42abe41 | 2018-05-24 11:20:19 +0000 | [diff] [blame] | 653 | case CodeCompletionContext::CCC_Recovery: |
Kadir Cetinkaya | b915790 | 2018-10-24 15:24:29 +0000 | [diff] [blame] | 654 | case CodeCompletionContext::CCC_NewName: |
Eric Liu | 42abe41 | 2018-05-24 11:20:19 +0000 | [diff] [blame] | 655 | return false; |
| 656 | } |
| 657 | llvm_unreachable("unknown code completion context"); |
| 658 | } |
| 659 | |
Eric Liu | b1317fa | 2018-11-30 11:12:40 +0000 | [diff] [blame] | 660 | static bool isInjectedClass(const NamedDecl &D) { |
| 661 | if (auto *R = dyn_cast_or_null<RecordDecl>(&D)) |
| 662 | if (R->isInjectedClassName()) |
| 663 | return true; |
| 664 | return false; |
| 665 | } |
| 666 | |
Sam McCall | 4caa851 | 2018-06-07 12:49:17 +0000 | [diff] [blame] | 667 | // Some member calls are blacklisted because they're so rarely useful. |
| 668 | static bool isBlacklistedMember(const NamedDecl &D) { |
| 669 | // Destructor completion is rarely useful, and works inconsistently. |
| 670 | // (s.^ completes ~string, but s.~st^ is an error). |
| 671 | if (D.getKind() == Decl::CXXDestructor) |
| 672 | return true; |
| 673 | // Injected name may be useful for A::foo(), but who writes A::A::foo()? |
Eric Liu | b1317fa | 2018-11-30 11:12:40 +0000 | [diff] [blame] | 674 | if (isInjectedClass(D)) |
| 675 | return true; |
Sam McCall | 4caa851 | 2018-06-07 12:49:17 +0000 | [diff] [blame] | 676 | // Explicit calls to operators are also rare. |
| 677 | auto NameKind = D.getDeclName().getNameKind(); |
| 678 | if (NameKind == DeclarationName::CXXOperatorName || |
| 679 | NameKind == DeclarationName::CXXLiteralOperatorName || |
| 680 | NameKind == DeclarationName::CXXConversionFunctionName) |
| 681 | return true; |
| 682 | return false; |
| 683 | } |
| 684 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 685 | // The CompletionRecorder captures Sema code-complete output, including context. |
| 686 | // It filters out ignored results (but doesn't apply fuzzy-filtering yet). |
| 687 | // It doesn't do scoring or conversion to CompletionItem yet, as we want to |
| 688 | // merge with index results first. |
Ilya Biryukov | ddf6a33 | 2018-03-02 12:28:27 +0000 | [diff] [blame] | 689 | // Generally the fields and methods of this object should only be used from |
| 690 | // within the callback. |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 691 | struct CompletionRecorder : public CodeCompleteConsumer { |
Ilya Biryukov | ddf6a33 | 2018-03-02 12:28:27 +0000 | [diff] [blame] | 692 | CompletionRecorder(const CodeCompleteOptions &Opts, |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 693 | llvm::unique_function<void()> ResultsCallback) |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 694 | : CodeCompleteConsumer(Opts.getClangCompleteOpts(), |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 695 | /*OutputIsBinary=*/false), |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 696 | CCContext(CodeCompletionContext::CCC_Other), Opts(Opts), |
| 697 | CCAllocator(std::make_shared<GlobalCodeCompletionAllocator>()), |
Ilya Biryukov | ddf6a33 | 2018-03-02 12:28:27 +0000 | [diff] [blame] | 698 | CCTUInfo(CCAllocator), ResultsCallback(std::move(ResultsCallback)) { |
| 699 | assert(this->ResultsCallback); |
| 700 | } |
| 701 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 702 | std::vector<CodeCompletionResult> Results; |
| 703 | CodeCompletionContext CCContext; |
| 704 | Sema *CCSema = nullptr; // Sema that created the results. |
| 705 | // FIXME: Sema is scary. Can we store ASTContext and Preprocessor, instead? |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 706 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 707 | void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context, |
| 708 | CodeCompletionResult *InResults, |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 709 | unsigned NumResults) override final { |
Eric Liu | 485074f | 2018-07-11 13:15:31 +0000 | [diff] [blame] | 710 | // Results from recovery mode are generally useless, and the callback after |
| 711 | // recovery (if any) is usually more interesting. To make sure we handle the |
| 712 | // future callback from sema, we just ignore all callbacks in recovery mode, |
| 713 | // as taking only results from recovery mode results in poor completion |
| 714 | // results. |
| 715 | // FIXME: in case there is no future sema completion callback after the |
| 716 | // recovery mode, we might still want to provide some results (e.g. trivial |
| 717 | // identifier-based completion). |
| 718 | if (Context.getKind() == CodeCompletionContext::CCC_Recovery) { |
| 719 | log("Code complete: Ignoring sema code complete callback with Recovery " |
| 720 | "context."); |
| 721 | return; |
| 722 | } |
Eric Liu | 42abe41 | 2018-05-24 11:20:19 +0000 | [diff] [blame] | 723 | // If a callback is called without any sema result and the context does not |
| 724 | // support index-based completion, we simply skip it to give way to |
| 725 | // potential future callbacks with results. |
| 726 | if (NumResults == 0 && !contextAllowsIndex(Context.getKind())) |
| 727 | return; |
Ilya Biryukov | 94da7bd | 2018-03-16 15:23:44 +0000 | [diff] [blame] | 728 | if (CCSema) { |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 729 | log("Multiple code complete callbacks (parser backtracked?). " |
Ilya Biryukov | 94da7bd | 2018-03-16 15:23:44 +0000 | [diff] [blame] | 730 | "Dropping results from context {0}, keeping results from {1}.", |
Eric Liu | 42abe41 | 2018-05-24 11:20:19 +0000 | [diff] [blame] | 731 | getCompletionKindString(Context.getKind()), |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 732 | getCompletionKindString(this->CCContext.getKind())); |
Ilya Biryukov | 94da7bd | 2018-03-16 15:23:44 +0000 | [diff] [blame] | 733 | return; |
| 734 | } |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 735 | // Record the completion context. |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 736 | CCSema = &S; |
| 737 | CCContext = Context; |
Eric Liu | 6f648df | 2017-12-19 16:50:37 +0000 | [diff] [blame] | 738 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 739 | // Retain the results we might want. |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 740 | for (unsigned I = 0; I < NumResults; ++I) { |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 741 | auto &Result = InResults[I]; |
Sam McCall | e8437cb | 2018-10-24 13:51:44 +0000 | [diff] [blame] | 742 | // Class members that are shadowed by subclasses are usually noise. |
| 743 | if (Result.Hidden && Result.Declaration && |
| 744 | Result.Declaration->isCXXClassMember()) |
Ilya Biryukov | f60bf34 | 2018-01-10 13:51:09 +0000 | [diff] [blame] | 745 | continue; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 746 | if (!Opts.IncludeIneligibleResults && |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 747 | (Result.Availability == CXAvailability_NotAvailable || |
| 748 | Result.Availability == CXAvailability_NotAccessible)) |
| 749 | continue; |
Sam McCall | 4caa851 | 2018-06-07 12:49:17 +0000 | [diff] [blame] | 750 | if (Result.Declaration && |
| 751 | !Context.getBaseType().isNull() // is this a member-access context? |
| 752 | && isBlacklistedMember(*Result.Declaration)) |
Sam McCall | d2a9592 | 2018-01-22 21:05:00 +0000 | [diff] [blame] | 753 | continue; |
Eric Liu | b1317fa | 2018-11-30 11:12:40 +0000 | [diff] [blame] | 754 | // Skip injected class name when no class scope is not explicitly set. |
| 755 | // E.g. show injected A::A in `using A::A^` but not in "A^". |
| 756 | if (Result.Declaration && !Context.getCXXScopeSpecifier().hasValue() && |
| 757 | isInjectedClass(*Result.Declaration)) |
| 758 | continue; |
Ilya Biryukov | 53d6d93 | 2018-03-06 16:45:21 +0000 | [diff] [blame] | 759 | // We choose to never append '::' to completion results in clangd. |
| 760 | Result.StartsNestedNameSpecifier = false; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 761 | Results.push_back(Result); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 762 | } |
Ilya Biryukov | ddf6a33 | 2018-03-02 12:28:27 +0000 | [diff] [blame] | 763 | ResultsCallback(); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 766 | CodeCompletionAllocator &getAllocator() override { return *CCAllocator; } |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 767 | CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; } |
| 768 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 769 | // Returns the filtering/sorting name for Result, which must be from Results. |
| 770 | // Returned string is owned by this recorder (or the AST). |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 771 | llvm::StringRef getName(const CodeCompletionResult &Result) { |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 772 | switch (Result.Kind) { |
| 773 | case CodeCompletionResult::RK_Declaration: |
| 774 | if (auto *ID = Result.Declaration->getIdentifier()) |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 775 | return ID->getName(); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 776 | break; |
| 777 | case CodeCompletionResult::RK_Keyword: |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 778 | return Result.Keyword; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 779 | case CodeCompletionResult::RK_Macro: |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 780 | return Result.Macro->getName(); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 781 | case CodeCompletionResult::RK_Pattern: |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 782 | return Result.Pattern->getTypedText(); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 783 | } |
Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 784 | auto *CCS = codeCompletionString(Result); |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 785 | return CCS->getTypedText(); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 788 | // Build a CodeCompletion string for R, which must be from Results. |
| 789 | // The CCS will be owned by this recorder. |
Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 790 | CodeCompletionString *codeCompletionString(const CodeCompletionResult &R) { |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 791 | // CodeCompletionResult doesn't seem to be const-correct. We own it, anyway. |
| 792 | return const_cast<CodeCompletionResult &>(R).CreateCodeCompletionString( |
Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 793 | *CCSema, CCContext, *CCAllocator, CCTUInfo, |
| 794 | /*IncludeBriefComments=*/false); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 797 | private: |
| 798 | CodeCompleteOptions Opts; |
| 799 | std::shared_ptr<GlobalCodeCompletionAllocator> CCAllocator; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 800 | CodeCompletionTUInfo CCTUInfo; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 801 | llvm::unique_function<void()> ResultsCallback; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 802 | }; |
| 803 | |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 804 | struct ScoredSignature { |
| 805 | // When set, requires documentation to be requested from the index with this |
| 806 | // ID. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 807 | llvm::Optional<SymbolID> IDForDoc; |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 808 | SignatureInformation Signature; |
| 809 | SignatureQualitySignals Quality; |
| 810 | }; |
Kadir Cetinkaya | e486e37 | 2018-08-13 08:40:05 +0000 | [diff] [blame] | 811 | |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 812 | class SignatureHelpCollector final : public CodeCompleteConsumer { |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 813 | public: |
| 814 | SignatureHelpCollector(const clang::CodeCompleteOptions &CodeCompleteOpts, |
Sam McCall | 046557b | 2018-09-03 16:37:59 +0000 | [diff] [blame] | 815 | const SymbolIndex *Index, SignatureHelp &SigHelp) |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 816 | : CodeCompleteConsumer(CodeCompleteOpts, |
| 817 | /*OutputIsBinary=*/false), |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 818 | SigHelp(SigHelp), |
| 819 | Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()), |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 820 | CCTUInfo(Allocator), Index(Index) {} |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 821 | |
| 822 | void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, |
| 823 | OverloadCandidate *Candidates, |
Ilya Biryukov | 43c292c | 2018-08-30 13:14:31 +0000 | [diff] [blame] | 824 | unsigned NumCandidates, |
| 825 | SourceLocation OpenParLoc) override { |
| 826 | assert(!OpenParLoc.isInvalid()); |
| 827 | SourceManager &SrcMgr = S.getSourceManager(); |
| 828 | OpenParLoc = SrcMgr.getFileLoc(OpenParLoc); |
| 829 | if (SrcMgr.isInMainFile(OpenParLoc)) |
| 830 | SigHelp.argListStart = sourceLocToPosition(SrcMgr, OpenParLoc); |
| 831 | else |
| 832 | elog("Location oustide main file in signature help: {0}", |
| 833 | OpenParLoc.printToString(SrcMgr)); |
| 834 | |
Kadir Cetinkaya | e486e37 | 2018-08-13 08:40:05 +0000 | [diff] [blame] | 835 | std::vector<ScoredSignature> ScoredSignatures; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 836 | SigHelp.signatures.reserve(NumCandidates); |
Kadir Cetinkaya | e486e37 | 2018-08-13 08:40:05 +0000 | [diff] [blame] | 837 | ScoredSignatures.reserve(NumCandidates); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 838 | // FIXME(rwols): How can we determine the "active overload candidate"? |
| 839 | // Right now the overloaded candidates seem to be provided in a "best fit" |
| 840 | // order, so I'm not too worried about this. |
| 841 | SigHelp.activeSignature = 0; |
| 842 | assert(CurrentArg <= (unsigned)std::numeric_limits<int>::max() && |
| 843 | "too many arguments"); |
| 844 | SigHelp.activeParameter = static_cast<int>(CurrentArg); |
| 845 | for (unsigned I = 0; I < NumCandidates; ++I) { |
Ilya Biryukov | 8fd44bb | 2018-08-14 09:36:32 +0000 | [diff] [blame] | 846 | OverloadCandidate Candidate = Candidates[I]; |
| 847 | // We want to avoid showing instantiated signatures, because they may be |
| 848 | // long in some cases (e.g. when 'T' is substituted with 'std::string', we |
| 849 | // would get 'std::basic_string<char>'). |
| 850 | if (auto *Func = Candidate.getFunction()) { |
| 851 | if (auto *Pattern = Func->getTemplateInstantiationPattern()) |
| 852 | Candidate = OverloadCandidate(Pattern); |
| 853 | } |
| 854 | |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 855 | const auto *CCS = Candidate.CreateSignatureString( |
| 856 | CurrentArg, S, *Allocator, CCTUInfo, true); |
| 857 | assert(CCS && "Expected the CodeCompletionString to be non-null"); |
Kadir Cetinkaya | e486e37 | 2018-08-13 08:40:05 +0000 | [diff] [blame] | 858 | ScoredSignatures.push_back(processOverloadCandidate( |
Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 859 | Candidate, *CCS, |
Ilya Biryukov | 5f4a351 | 2018-08-17 09:29:38 +0000 | [diff] [blame] | 860 | Candidate.getFunction() |
| 861 | ? getDeclComment(S.getASTContext(), *Candidate.getFunction()) |
| 862 | : "")); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 863 | } |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 864 | |
| 865 | // Sema does not load the docs from the preamble, so we need to fetch extra |
| 866 | // docs from the index instead. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 867 | llvm::DenseMap<SymbolID, std::string> FetchedDocs; |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 868 | if (Index) { |
| 869 | LookupRequest IndexRequest; |
| 870 | for (const auto &S : ScoredSignatures) { |
| 871 | if (!S.IDForDoc) |
| 872 | continue; |
| 873 | IndexRequest.IDs.insert(*S.IDForDoc); |
| 874 | } |
| 875 | Index->lookup(IndexRequest, [&](const Symbol &S) { |
Sam McCall | 2e5700f | 2018-08-31 13:55:01 +0000 | [diff] [blame] | 876 | if (!S.Documentation.empty()) |
| 877 | FetchedDocs[S.ID] = S.Documentation; |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 878 | }); |
| 879 | log("SigHelp: requested docs for {0} symbols from the index, got {1} " |
| 880 | "symbols with non-empty docs in the response", |
| 881 | IndexRequest.IDs.size(), FetchedDocs.size()); |
| 882 | } |
| 883 | |
Ilya Biryukov | 22fa465 | 2019-01-03 13:28:05 +0000 | [diff] [blame] | 884 | llvm::sort(ScoredSignatures, [](const ScoredSignature &L, |
| 885 | const ScoredSignature &R) { |
| 886 | // Ordering follows: |
| 887 | // - Less number of parameters is better. |
| 888 | // - Function is better than FunctionType which is better than |
| 889 | // Function Template. |
| 890 | // - High score is better. |
| 891 | // - Shorter signature is better. |
| 892 | // - Alphebatically smaller is better. |
| 893 | if (L.Quality.NumberOfParameters != R.Quality.NumberOfParameters) |
| 894 | return L.Quality.NumberOfParameters < R.Quality.NumberOfParameters; |
| 895 | if (L.Quality.NumberOfOptionalParameters != |
| 896 | R.Quality.NumberOfOptionalParameters) |
| 897 | return L.Quality.NumberOfOptionalParameters < |
| 898 | R.Quality.NumberOfOptionalParameters; |
| 899 | if (L.Quality.Kind != R.Quality.Kind) { |
| 900 | using OC = CodeCompleteConsumer::OverloadCandidate; |
| 901 | switch (L.Quality.Kind) { |
| 902 | case OC::CK_Function: |
| 903 | return true; |
| 904 | case OC::CK_FunctionType: |
| 905 | return R.Quality.Kind != OC::CK_Function; |
| 906 | case OC::CK_FunctionTemplate: |
| 907 | return false; |
| 908 | } |
| 909 | llvm_unreachable("Unknown overload candidate type."); |
| 910 | } |
| 911 | if (L.Signature.label.size() != R.Signature.label.size()) |
| 912 | return L.Signature.label.size() < R.Signature.label.size(); |
| 913 | return L.Signature.label < R.Signature.label; |
| 914 | }); |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 915 | |
| 916 | for (auto &SS : ScoredSignatures) { |
| 917 | auto IndexDocIt = |
| 918 | SS.IDForDoc ? FetchedDocs.find(*SS.IDForDoc) : FetchedDocs.end(); |
| 919 | if (IndexDocIt != FetchedDocs.end()) |
| 920 | SS.Signature.documentation = IndexDocIt->second; |
| 921 | |
| 922 | SigHelp.signatures.push_back(std::move(SS.Signature)); |
| 923 | } |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | GlobalCodeCompletionAllocator &getAllocator() override { return *Allocator; } |
| 927 | |
| 928 | CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; } |
| 929 | |
| 930 | private: |
Eric Liu | 63696e1 | 2017-12-20 17:24:31 +0000 | [diff] [blame] | 931 | // FIXME(ioeric): consider moving CodeCompletionString logic here to |
| 932 | // CompletionString.h. |
Kadir Cetinkaya | e486e37 | 2018-08-13 08:40:05 +0000 | [diff] [blame] | 933 | ScoredSignature processOverloadCandidate(const OverloadCandidate &Candidate, |
| 934 | const CodeCompletionString &CCS, |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 935 | llvm::StringRef DocComment) const { |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 936 | SignatureInformation Signature; |
Kadir Cetinkaya | e486e37 | 2018-08-13 08:40:05 +0000 | [diff] [blame] | 937 | SignatureQualitySignals Signal; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 938 | const char *ReturnType = nullptr; |
| 939 | |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 940 | Signature.documentation = formatDocumentation(CCS, DocComment); |
Kadir Cetinkaya | e486e37 | 2018-08-13 08:40:05 +0000 | [diff] [blame] | 941 | Signal.Kind = Candidate.getKind(); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 942 | |
| 943 | for (const auto &Chunk : CCS) { |
| 944 | switch (Chunk.Kind) { |
| 945 | case CodeCompletionString::CK_ResultType: |
| 946 | // A piece of text that describes the type of an entity or, |
| 947 | // for functions and methods, the return type. |
| 948 | assert(!ReturnType && "Unexpected CK_ResultType"); |
| 949 | ReturnType = Chunk.Text; |
| 950 | break; |
| 951 | case CodeCompletionString::CK_Placeholder: |
| 952 | // A string that acts as a placeholder for, e.g., a function call |
| 953 | // argument. |
| 954 | // Intentional fallthrough here. |
| 955 | case CodeCompletionString::CK_CurrentParameter: { |
| 956 | // A piece of text that describes the parameter that corresponds to |
| 957 | // the code-completion location within a function call, message send, |
| 958 | // macro invocation, etc. |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 959 | Signature.label += Chunk.Text; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 960 | ParameterInformation Info; |
| 961 | Info.label = Chunk.Text; |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 962 | Signature.parameters.push_back(std::move(Info)); |
Kadir Cetinkaya | e486e37 | 2018-08-13 08:40:05 +0000 | [diff] [blame] | 963 | Signal.NumberOfParameters++; |
| 964 | Signal.ContainsActiveParameter = true; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 965 | break; |
| 966 | } |
| 967 | case CodeCompletionString::CK_Optional: { |
| 968 | // The rest of the parameters are defaulted/optional. |
| 969 | assert(Chunk.Optional && |
| 970 | "Expected the optional code completion string to be non-null."); |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 971 | Signature.label += getOptionalParameters(*Chunk.Optional, |
| 972 | Signature.parameters, Signal); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 973 | break; |
| 974 | } |
| 975 | case CodeCompletionString::CK_VerticalSpace: |
| 976 | break; |
| 977 | default: |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 978 | Signature.label += Chunk.Text; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 979 | break; |
| 980 | } |
| 981 | } |
| 982 | if (ReturnType) { |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 983 | Signature.label += " -> "; |
| 984 | Signature.label += ReturnType; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 985 | } |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 986 | dlog("Signal for {0}: {1}", Signature, Signal); |
| 987 | ScoredSignature Result; |
| 988 | Result.Signature = std::move(Signature); |
| 989 | Result.Quality = Signal; |
| 990 | Result.IDForDoc = |
| 991 | Result.Signature.documentation.empty() && Candidate.getFunction() |
| 992 | ? clangd::getSymbolID(Candidate.getFunction()) |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 993 | : None; |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 994 | return Result; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | SignatureHelp &SigHelp; |
| 998 | std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator; |
| 999 | CodeCompletionTUInfo CCTUInfo; |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 1000 | const SymbolIndex *Index; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1001 | }; // SignatureHelpCollector |
| 1002 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1003 | struct SemaCompleteInput { |
| 1004 | PathRef FileName; |
| 1005 | const tooling::CompileCommand &Command; |
Eric Liu | b1d7542 | 2018-10-02 10:43:55 +0000 | [diff] [blame] | 1006 | const PreambleData *Preamble; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 1007 | llvm::StringRef Contents; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1008 | Position Pos; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 1009 | llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1010 | std::shared_ptr<PCHContainerOperations> PCHs; |
| 1011 | }; |
| 1012 | |
| 1013 | // Invokes Sema code completion on a file. |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1014 | // If \p Includes is set, it will be updated based on the compiler invocation. |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 1015 | bool semaCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer, |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1016 | const clang::CodeCompleteOptions &Options, |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 1017 | const SemaCompleteInput &Input, |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1018 | IncludeStructure *Includes = nullptr) { |
Ilya Biryukov | ddf6a33 | 2018-03-02 12:28:27 +0000 | [diff] [blame] | 1019 | trace::Span Tracer("Sema completion"); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1020 | std::vector<const char *> ArgStrs; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1021 | for (const auto &S : Input.Command.CommandLine) |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1022 | ArgStrs.push_back(S.c_str()); |
| 1023 | |
Ilya Biryukov | a9cf311 | 2018-02-13 17:15:06 +0000 | [diff] [blame] | 1024 | if (Input.VFS->setCurrentWorkingDirectory(Input.Command.Directory)) { |
| 1025 | log("Couldn't set working directory"); |
| 1026 | // We run parsing anyway, our lit-tests rely on results for non-existing |
| 1027 | // working dirs. |
| 1028 | } |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1029 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 1030 | llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = Input.VFS; |
Eric Liu | b1d7542 | 2018-10-02 10:43:55 +0000 | [diff] [blame] | 1031 | if (Input.Preamble && Input.Preamble->StatCache) |
| 1032 | VFS = Input.Preamble->StatCache->getConsumingFS(std::move(VFS)); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1033 | IgnoreDiagnostics DummyDiagsConsumer; |
| 1034 | auto CI = createInvocationFromCommandLine( |
| 1035 | ArgStrs, |
| 1036 | CompilerInstance::createDiagnostics(new DiagnosticOptions, |
| 1037 | &DummyDiagsConsumer, false), |
Eric Liu | b1d7542 | 2018-10-02 10:43:55 +0000 | [diff] [blame] | 1038 | VFS); |
Ilya Biryukov | b6ad25c | 2018-02-09 13:51:57 +0000 | [diff] [blame] | 1039 | if (!CI) { |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 1040 | elog("Couldn't create CompilerInvocation"); |
Ilya Biryukov | b6ad25c | 2018-02-09 13:51:57 +0000 | [diff] [blame] | 1041 | return false; |
| 1042 | } |
Ilya Biryukov | 981a35d | 2018-05-28 12:11:37 +0000 | [diff] [blame] | 1043 | auto &FrontendOpts = CI->getFrontendOpts(); |
| 1044 | FrontendOpts.DisableFree = false; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1045 | FrontendOpts.SkipFunctionBodies = true; |
Ilya Biryukov | 981a35d | 2018-05-28 12:11:37 +0000 | [diff] [blame] | 1046 | CI->getLangOpts()->CommentOpts.ParseAllComments = true; |
| 1047 | // Disable typo correction in Sema. |
| 1048 | CI->getLangOpts()->SpellChecking = false; |
| 1049 | // Setup code completion. |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1050 | FrontendOpts.CodeCompleteOpts = Options; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1051 | FrontendOpts.CodeCompletionAt.FileName = Input.FileName; |
Sam McCall | a4962cc | 2018-04-27 11:59:28 +0000 | [diff] [blame] | 1052 | auto Offset = positionToOffset(Input.Contents, Input.Pos); |
| 1053 | if (!Offset) { |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 1054 | elog("Code completion position was invalid {0}", Offset.takeError()); |
Sam McCall | a4962cc | 2018-04-27 11:59:28 +0000 | [diff] [blame] | 1055 | return false; |
| 1056 | } |
| 1057 | std::tie(FrontendOpts.CodeCompletionAt.Line, |
| 1058 | FrontendOpts.CodeCompletionAt.Column) = |
| 1059 | offsetToClangLineColumn(Input.Contents, *Offset); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1060 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 1061 | std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer = |
| 1062 | llvm::MemoryBuffer::getMemBufferCopy(Input.Contents, Input.FileName); |
Ilya Biryukov | 981a35d | 2018-05-28 12:11:37 +0000 | [diff] [blame] | 1063 | // The diagnostic options must be set before creating a CompilerInstance. |
| 1064 | CI->getDiagnosticOpts().IgnoreWarnings = true; |
| 1065 | // We reuse the preamble whether it's valid or not. This is a |
| 1066 | // correctness/performance tradeoff: building without a preamble is slow, and |
| 1067 | // completion is latency-sensitive. |
Sam McCall | ebef812 | 2018-09-14 12:36:06 +0000 | [diff] [blame] | 1068 | // However, if we're completing *inside* the preamble section of the draft, |
| 1069 | // overriding the preamble will break sema completion. Fortunately we can just |
| 1070 | // skip all includes in this case; these completions are really simple. |
| 1071 | bool CompletingInPreamble = |
| 1072 | ComputePreambleBounds(*CI->getLangOpts(), ContentsBuffer.get(), 0).Size > |
| 1073 | *Offset; |
Ilya Biryukov | 981a35d | 2018-05-28 12:11:37 +0000 | [diff] [blame] | 1074 | // NOTE: we must call BeginSourceFile after prepareCompilerInstance. Otherwise |
| 1075 | // the remapped buffers do not get freed. |
| 1076 | auto Clang = prepareCompilerInstance( |
Eric Liu | b1d7542 | 2018-10-02 10:43:55 +0000 | [diff] [blame] | 1077 | std::move(CI), |
| 1078 | (Input.Preamble && !CompletingInPreamble) ? &Input.Preamble->Preamble |
| 1079 | : nullptr, |
| 1080 | std::move(ContentsBuffer), std::move(Input.PCHs), std::move(VFS), |
Sam McCall | ebef812 | 2018-09-14 12:36:06 +0000 | [diff] [blame] | 1081 | DummyDiagsConsumer); |
| 1082 | Clang->getPreprocessorOpts().SingleFileParseMode = CompletingInPreamble; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1083 | Clang->setCodeCompletionConsumer(Consumer.release()); |
| 1084 | |
| 1085 | SyntaxOnlyAction Action; |
| 1086 | if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0])) { |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 1087 | log("BeginSourceFile() failed when running codeComplete for {0}", |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 1088 | Input.FileName); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1089 | return false; |
| 1090 | } |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1091 | if (Includes) |
| 1092 | Clang->getPreprocessor().addPPCallbacks( |
| 1093 | collectIncludeStructureCallback(Clang->getSourceManager(), Includes)); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1094 | if (!Action.Execute()) { |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 1095 | log("Execute() failed when running codeComplete for {0}", Input.FileName); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1096 | return false; |
| 1097 | } |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1098 | Action.EndSourceFile(); |
| 1099 | |
| 1100 | return true; |
| 1101 | } |
| 1102 | |
Ilya Biryukov | a907ba4 | 2018-05-14 10:50:04 +0000 | [diff] [blame] | 1103 | // Should we allow index completions in the specified context? |
| 1104 | bool allowIndex(CodeCompletionContext &CC) { |
| 1105 | if (!contextAllowsIndex(CC.getKind())) |
| 1106 | return false; |
| 1107 | // We also avoid ClassName::bar (but allow namespace::bar). |
| 1108 | auto Scope = CC.getCXXScopeSpecifier(); |
| 1109 | if (!Scope) |
| 1110 | return true; |
| 1111 | NestedNameSpecifier *NameSpec = (*Scope)->getScopeRep(); |
| 1112 | if (!NameSpec) |
| 1113 | return true; |
| 1114 | // We only query the index when qualifier is a namespace. |
| 1115 | // If it's a class, we rely solely on sema completions. |
| 1116 | switch (NameSpec->getKind()) { |
| 1117 | case NestedNameSpecifier::Global: |
| 1118 | case NestedNameSpecifier::Namespace: |
| 1119 | case NestedNameSpecifier::NamespaceAlias: |
| 1120 | return true; |
| 1121 | case NestedNameSpecifier::Super: |
| 1122 | case NestedNameSpecifier::TypeSpec: |
| 1123 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1124 | // Unresolved inside a template. |
| 1125 | case NestedNameSpecifier::Identifier: |
| 1126 | return false; |
| 1127 | } |
Ilya Biryukov | a6556e2 | 2018-05-14 11:47:30 +0000 | [diff] [blame] | 1128 | llvm_unreachable("invalid NestedNameSpecifier kind"); |
Ilya Biryukov | a907ba4 | 2018-05-14 10:50:04 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1131 | std::future<SymbolSlab> startAsyncFuzzyFind(const SymbolIndex &Index, |
| 1132 | const FuzzyFindRequest &Req) { |
| 1133 | return runAsync<SymbolSlab>([&Index, Req]() { |
| 1134 | trace::Span Tracer("Async fuzzyFind"); |
| 1135 | SymbolSlab::Builder Syms; |
| 1136 | Index.fuzzyFind(Req, [&Syms](const Symbol &Sym) { Syms.insert(Sym); }); |
| 1137 | return std::move(Syms).build(); |
| 1138 | }); |
| 1139 | } |
| 1140 | |
| 1141 | // Creates a `FuzzyFindRequest` based on the cached index request from the |
| 1142 | // last completion, if any, and the speculated completion filter text in the |
| 1143 | // source code. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 1144 | llvm::Optional<FuzzyFindRequest> |
| 1145 | speculativeFuzzyFindRequestForCompletion(FuzzyFindRequest CachedReq, |
| 1146 | PathRef File, llvm::StringRef Content, |
| 1147 | Position Pos) { |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1148 | auto Filter = speculateCompletionFilter(Content, Pos); |
| 1149 | if (!Filter) { |
| 1150 | elog("Failed to speculate filter text for code completion at Pos " |
| 1151 | "{0}:{1}: {2}", |
| 1152 | Pos.line, Pos.character, Filter.takeError()); |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 1153 | return None; |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1154 | } |
| 1155 | CachedReq.Query = *Filter; |
| 1156 | return CachedReq; |
| 1157 | } |
| 1158 | |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 1159 | // Returns the most popular include header for \p Sym. If two headers are |
| 1160 | // equally popular, prefer the shorter one. Returns empty string if \p Sym has |
| 1161 | // no include header. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 1162 | llvm::SmallVector<llvm::StringRef, 1> getRankedIncludes(const Symbol &Sym) { |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 1163 | auto Includes = Sym.IncludeHeaders; |
| 1164 | // Sort in descending order by reference count and header length. |
Kirill Bobyrev | 4a5ff88 | 2018-10-07 14:49:41 +0000 | [diff] [blame] | 1165 | llvm::sort(Includes, [](const Symbol::IncludeHeaderWithReferences &LHS, |
| 1166 | const Symbol::IncludeHeaderWithReferences &RHS) { |
| 1167 | if (LHS.References == RHS.References) |
| 1168 | return LHS.IncludeHeader.size() < RHS.IncludeHeader.size(); |
| 1169 | return LHS.References > RHS.References; |
| 1170 | }); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 1171 | llvm::SmallVector<llvm::StringRef, 1> Headers; |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 1172 | for (const auto &Include : Includes) |
| 1173 | Headers.push_back(Include.IncludeHeader); |
| 1174 | return Headers; |
| 1175 | } |
| 1176 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1177 | // Runs Sema-based (AST) and Index-based completion, returns merged results. |
| 1178 | // |
| 1179 | // There are a few tricky considerations: |
| 1180 | // - the AST provides information needed for the index query (e.g. which |
| 1181 | // namespaces to search in). So Sema must start first. |
| 1182 | // - we only want to return the top results (Opts.Limit). |
| 1183 | // Building CompletionItems for everything else is wasteful, so we want to |
| 1184 | // preserve the "native" format until we're done with scoring. |
| 1185 | // - the data underlying Sema completion items is owned by the AST and various |
| 1186 | // other arenas, which must stay alive for us to build CompletionItems. |
| 1187 | // - we may get duplicate results from Sema and the Index, we need to merge. |
| 1188 | // |
Ilya Biryukov | ddf6a33 | 2018-03-02 12:28:27 +0000 | [diff] [blame] | 1189 | // So we start Sema completion first, and do all our work in its callback. |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1190 | // We use the Sema context information to query the index. |
| 1191 | // Then we merge the two result sets, producing items that are Sema/Index/Both. |
| 1192 | // These items are scored, and the top N are synthesized into the LSP response. |
| 1193 | // Finally, we can clean up the data structures created by Sema completion. |
| 1194 | // |
| 1195 | // Main collaborators are: |
| 1196 | // - semaCodeComplete sets up the compiler machinery to run code completion. |
| 1197 | // - CompletionRecorder captures Sema completion results, including context. |
| 1198 | // - SymbolIndex (Opts.Index) provides index completion results as Symbols |
| 1199 | // - CompletionCandidates are the result of merging Sema and Index results. |
| 1200 | // Each candidate points to an underlying CodeCompletionResult (Sema), a |
| 1201 | // Symbol (Index), or both. It computes the result quality score. |
| 1202 | // CompletionCandidate also does conversion to CompletionItem (at the end). |
| 1203 | // - FuzzyMatcher scores how the candidate matches the partial identifier. |
| 1204 | // This score is combined with the result quality score for the final score. |
| 1205 | // - TopN determines the results with the best score. |
| 1206 | class CodeCompleteFlow { |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 1207 | PathRef FileName; |
Ilya Biryukov | 22fa465 | 2019-01-03 13:28:05 +0000 | [diff] [blame] | 1208 | IncludeStructure Includes; // Complete once the compiler runs. |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1209 | SpeculativeFuzzyFind *SpecFuzzyFind; // Can be nullptr. |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1210 | const CodeCompleteOptions &Opts; |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1211 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1212 | // Sema takes ownership of Recorder. Recorder is valid until Sema cleanup. |
Ilya Biryukov | ddf6a33 | 2018-03-02 12:28:27 +0000 | [diff] [blame] | 1213 | CompletionRecorder *Recorder = nullptr; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1214 | int NSema = 0, NIndex = 0, NBoth = 0; // Counters for logging. |
Ilya Biryukov | 22fa465 | 2019-01-03 13:28:05 +0000 | [diff] [blame] | 1215 | bool Incomplete = false; // Would more be available with a higher limit? |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 1216 | llvm::Optional<FuzzyMatcher> Filter; // Initialized once Sema runs. |
Eric Liu | 670c147 | 2018-09-27 18:46:00 +0000 | [diff] [blame] | 1217 | std::vector<std::string> QueryScopes; // Initialized once Sema runs. |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 1218 | // Initialized once QueryScopes is initialized, if there are scopes. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 1219 | llvm::Optional<ScopeDistance> ScopeProximity; |
Ilya Biryukov | 647da3e | 2018-11-26 15:38:01 +0000 | [diff] [blame] | 1220 | llvm::Optional<OpaqueType> PreferredType; // Initialized once Sema runs. |
Eric Liu | 670c147 | 2018-09-27 18:46:00 +0000 | [diff] [blame] | 1221 | // Whether to query symbols from any scope. Initialized once Sema runs. |
| 1222 | bool AllScopes = false; |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1223 | // Include-insertion and proximity scoring rely on the include structure. |
| 1224 | // This is available after Sema has run. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 1225 | llvm::Optional<IncludeInserter> Inserter; // Available during runWithSema. |
| 1226 | llvm::Optional<URIDistance> FileProximity; // Initialized once Sema runs. |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1227 | /// Speculative request based on the cached request and the filter text before |
| 1228 | /// the cursor. |
| 1229 | /// Initialized right before sema run. This is only set if `SpecFuzzyFind` is |
| 1230 | /// set and contains a cached request. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 1231 | llvm::Optional<FuzzyFindRequest> SpecReq; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1232 | |
| 1233 | public: |
| 1234 | // A CodeCompleteFlow object is only useful for calling run() exactly once. |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1235 | CodeCompleteFlow(PathRef FileName, const IncludeStructure &Includes, |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1236 | SpeculativeFuzzyFind *SpecFuzzyFind, |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1237 | const CodeCompleteOptions &Opts) |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1238 | : FileName(FileName), Includes(Includes), SpecFuzzyFind(SpecFuzzyFind), |
| 1239 | Opts(Opts) {} |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1240 | |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1241 | CodeCompleteResult run(const SemaCompleteInput &SemaCCInput) && { |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 1242 | trace::Span Tracer("CodeCompleteFlow"); |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1243 | if (Opts.Index && SpecFuzzyFind && SpecFuzzyFind->CachedReq.hasValue()) { |
| 1244 | assert(!SpecFuzzyFind->Result.valid()); |
| 1245 | if ((SpecReq = speculativeFuzzyFindRequestForCompletion( |
| 1246 | *SpecFuzzyFind->CachedReq, SemaCCInput.FileName, |
| 1247 | SemaCCInput.Contents, SemaCCInput.Pos))) |
| 1248 | SpecFuzzyFind->Result = startAsyncFuzzyFind(*Opts.Index, *SpecReq); |
| 1249 | } |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 1250 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1251 | // We run Sema code completion first. It builds an AST and calculates: |
Ilya Biryukov | ddf6a33 | 2018-03-02 12:28:27 +0000 | [diff] [blame] | 1252 | // - completion results based on the AST. |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1253 | // - partial identifier and context. We need these for the index query. |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1254 | CodeCompleteResult Output; |
Ilya Biryukov | ddf6a33 | 2018-03-02 12:28:27 +0000 | [diff] [blame] | 1255 | auto RecorderOwner = llvm::make_unique<CompletionRecorder>(Opts, [&]() { |
| 1256 | assert(Recorder && "Recorder is not set"); |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1257 | auto Style = |
Eric Liu | 9338a88 | 2018-07-03 14:51:23 +0000 | [diff] [blame] | 1258 | format::getStyle(format::DefaultFormatStyle, SemaCCInput.FileName, |
| 1259 | format::DefaultFallbackStyle, SemaCCInput.Contents, |
| 1260 | SemaCCInput.VFS.get()); |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1261 | if (!Style) { |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 1262 | log("getStyle() failed for file {0}: {1}. Fallback is LLVM style.", |
| 1263 | SemaCCInput.FileName, Style.takeError()); |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1264 | Style = format::getLLVMStyle(); |
| 1265 | } |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 1266 | // If preprocessor was run, inclusions from preprocessor callback should |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1267 | // already be added to Includes. |
| 1268 | Inserter.emplace( |
| 1269 | SemaCCInput.FileName, SemaCCInput.Contents, *Style, |
| 1270 | SemaCCInput.Command.Directory, |
| 1271 | Recorder->CCSema->getPreprocessor().getHeaderSearchInfo()); |
| 1272 | for (const auto &Inc : Includes.MainFileIncludes) |
| 1273 | Inserter->addExisting(Inc); |
| 1274 | |
| 1275 | // Most of the cost of file proximity is in initializing the FileDistance |
| 1276 | // structures based on the observed includes, once per query. Conceptually |
| 1277 | // that happens here (though the per-URI-scheme initialization is lazy). |
| 1278 | // The per-result proximity scoring is (amortized) very cheap. |
| 1279 | FileDistanceOptions ProxOpts{}; // Use defaults. |
| 1280 | const auto &SM = Recorder->CCSema->getSourceManager(); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 1281 | llvm::StringMap<SourceParams> ProxSources; |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1282 | for (auto &Entry : Includes.includeDepth( |
| 1283 | SM.getFileEntryForID(SM.getMainFileID())->getName())) { |
| 1284 | auto &Source = ProxSources[Entry.getKey()]; |
| 1285 | Source.Cost = Entry.getValue() * ProxOpts.IncludeCost; |
| 1286 | // Symbols near our transitive includes are good, but only consider |
| 1287 | // things in the same directory or below it. Otherwise there can be |
| 1288 | // many false positives. |
| 1289 | if (Entry.getValue() > 0) |
| 1290 | Source.MaxUpTraversals = 1; |
| 1291 | } |
| 1292 | FileProximity.emplace(ProxSources, ProxOpts); |
| 1293 | |
Ilya Biryukov | ddf6a33 | 2018-03-02 12:28:27 +0000 | [diff] [blame] | 1294 | Output = runWithSema(); |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1295 | Inserter.reset(); // Make sure this doesn't out-live Clang. |
Ilya Biryukov | ddf6a33 | 2018-03-02 12:28:27 +0000 | [diff] [blame] | 1296 | SPAN_ATTACH(Tracer, "sema_completion_kind", |
| 1297 | getCompletionKindString(Recorder->CCContext.getKind())); |
Ilya Biryukov | 647da3e | 2018-11-26 15:38:01 +0000 | [diff] [blame] | 1298 | log("Code complete: sema context {0}, query scopes [{1}] (AnyScope={2}), " |
| 1299 | "expected type {3}", |
Eric Liu | bc25ef7 | 2018-07-05 08:29:33 +0000 | [diff] [blame] | 1300 | getCompletionKindString(Recorder->CCContext.getKind()), |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 1301 | llvm::join(QueryScopes.begin(), QueryScopes.end(), ","), AllScopes, |
Ilya Biryukov | 647da3e | 2018-11-26 15:38:01 +0000 | [diff] [blame] | 1302 | PreferredType ? Recorder->CCContext.getPreferredType().getAsString() |
| 1303 | : "<none>"); |
Ilya Biryukov | ddf6a33 | 2018-03-02 12:28:27 +0000 | [diff] [blame] | 1304 | }); |
| 1305 | |
| 1306 | Recorder = RecorderOwner.get(); |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1307 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 1308 | semaCodeComplete(std::move(RecorderOwner), Opts.getClangCompleteOpts(), |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 1309 | SemaCCInput, &Includes); |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1310 | |
Sam McCall | 2b78016 | 2018-01-30 17:20:54 +0000 | [diff] [blame] | 1311 | SPAN_ATTACH(Tracer, "sema_results", NSema); |
| 1312 | SPAN_ATTACH(Tracer, "index_results", NIndex); |
| 1313 | SPAN_ATTACH(Tracer, "merged_results", NBoth); |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 1314 | SPAN_ATTACH(Tracer, "returned_results", int64_t(Output.Completions.size())); |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1315 | SPAN_ATTACH(Tracer, "incomplete", Output.HasMore); |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 1316 | log("Code complete: {0} results from Sema, {1} from Index, " |
| 1317 | "{2} matched, {3} returned{4}.", |
| 1318 | NSema, NIndex, NBoth, Output.Completions.size(), |
| 1319 | Output.HasMore ? " (incomplete)" : ""); |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1320 | assert(!Opts.Limit || Output.Completions.size() <= Opts.Limit); |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1321 | // We don't assert that isIncomplete means we hit a limit. |
| 1322 | // Indexes may choose to impose their own limits even if we don't have one. |
| 1323 | return Output; |
| 1324 | } |
| 1325 | |
| 1326 | private: |
| 1327 | // This is called by run() once Sema code completion is done, but before the |
| 1328 | // Sema data structures are torn down. It does all the real work. |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1329 | CodeCompleteResult runWithSema() { |
Kadir Cetinkaya | a9c9d00 | 2018-08-13 08:23:01 +0000 | [diff] [blame] | 1330 | const auto &CodeCompletionRange = CharSourceRange::getCharRange( |
| 1331 | Recorder->CCSema->getPreprocessor().getCodeCompletionTokenRange()); |
| 1332 | Range TextEditRange; |
| 1333 | // When we are getting completions with an empty identifier, for example |
| 1334 | // std::vector<int> asdf; |
| 1335 | // asdf.^; |
| 1336 | // Then the range will be invalid and we will be doing insertion, use |
| 1337 | // current cursor position in such cases as range. |
| 1338 | if (CodeCompletionRange.isValid()) { |
| 1339 | TextEditRange = halfOpenToRange(Recorder->CCSema->getSourceManager(), |
| 1340 | CodeCompletionRange); |
| 1341 | } else { |
| 1342 | const auto &Pos = sourceLocToPosition( |
| 1343 | Recorder->CCSema->getSourceManager(), |
| 1344 | Recorder->CCSema->getPreprocessor().getCodeCompletionLoc()); |
| 1345 | TextEditRange.start = TextEditRange.end = Pos; |
| 1346 | } |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1347 | Filter = FuzzyMatcher( |
Ilya Biryukov | ddf6a33 | 2018-03-02 12:28:27 +0000 | [diff] [blame] | 1348 | Recorder->CCSema->getPreprocessor().getCodeCompletionFilter()); |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 1349 | std::tie(QueryScopes, AllScopes) = |
| 1350 | getQueryScopes(Recorder->CCContext, *Recorder->CCSema, Opts); |
| 1351 | if (!QueryScopes.empty()) |
| 1352 | ScopeProximity.emplace(QueryScopes); |
Ilya Biryukov | 647da3e | 2018-11-26 15:38:01 +0000 | [diff] [blame] | 1353 | PreferredType = |
| 1354 | OpaqueType::fromType(Recorder->CCSema->getASTContext(), |
| 1355 | Recorder->CCContext.getPreferredType()); |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1356 | // Sema provides the needed context to query the index. |
| 1357 | // FIXME: in addition to querying for extra/overlapping symbols, we should |
| 1358 | // explicitly request symbols corresponding to Sema results. |
| 1359 | // We can use their signals even if the index can't suggest them. |
| 1360 | // We must copy index results to preserve them, but there are at most Limit. |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 1361 | auto IndexResults = (Opts.Index && allowIndex(Recorder->CCContext)) |
| 1362 | ? queryIndex() |
| 1363 | : SymbolSlab(); |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1364 | trace::Span Tracer("Populate CodeCompleteResult"); |
Kadir Cetinkaya | b15b8dc | 2018-10-02 09:42:17 +0000 | [diff] [blame] | 1365 | // Merge Sema and Index results, score them, and pick the winners. |
| 1366 | auto Top = mergeResults(Recorder->Results, IndexResults); |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1367 | CodeCompleteResult Output; |
Kadir Cetinkaya | f8b85a3 | 2018-08-23 13:14:50 +0000 | [diff] [blame] | 1368 | |
| 1369 | // Convert the results to final form, assembling the expensive strings. |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1370 | for (auto &C : Top) { |
| 1371 | Output.Completions.push_back(toCodeCompletion(C.first)); |
| 1372 | Output.Completions.back().Score = C.second; |
Kadir Cetinkaya | a9c9d00 | 2018-08-13 08:23:01 +0000 | [diff] [blame] | 1373 | Output.Completions.back().CompletionTokenRange = TextEditRange; |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1374 | } |
| 1375 | Output.HasMore = Incomplete; |
Eric Liu | 5d2a807 | 2018-07-23 10:56:37 +0000 | [diff] [blame] | 1376 | Output.Context = Recorder->CCContext.getKind(); |
Kadir Cetinkaya | f8b85a3 | 2018-08-23 13:14:50 +0000 | [diff] [blame] | 1377 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1378 | return Output; |
| 1379 | } |
| 1380 | |
| 1381 | SymbolSlab queryIndex() { |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 1382 | trace::Span Tracer("Query index"); |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 1383 | SPAN_ATTACH(Tracer, "limit", int64_t(Opts.Limit)); |
Sam McCall | 2b78016 | 2018-01-30 17:20:54 +0000 | [diff] [blame] | 1384 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1385 | // Build the query. |
| 1386 | FuzzyFindRequest Req; |
Haojian Wu | 48b4865 | 2018-01-25 09:20:09 +0000 | [diff] [blame] | 1387 | if (Opts.Limit) |
Kirill Bobyrev | e6dd080 | 2018-09-13 14:27:03 +0000 | [diff] [blame] | 1388 | Req.Limit = Opts.Limit; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1389 | Req.Query = Filter->pattern(); |
Marc-Andre Laperle | 945b5a3 | 2018-06-05 14:01:40 +0000 | [diff] [blame] | 1390 | Req.RestrictForCodeCompletion = true; |
Eric Liu | bc25ef7 | 2018-07-05 08:29:33 +0000 | [diff] [blame] | 1391 | Req.Scopes = QueryScopes; |
Eric Liu | 670c147 | 2018-09-27 18:46:00 +0000 | [diff] [blame] | 1392 | Req.AnyScope = AllScopes; |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1393 | // FIXME: we should send multiple weighted paths here. |
Eric Liu | 6de95ec | 2018-06-12 08:48:20 +0000 | [diff] [blame] | 1394 | Req.ProximityPaths.push_back(FileName); |
Kirill Bobyrev | 09f00dc | 2018-09-10 11:51:05 +0000 | [diff] [blame] | 1395 | vlog("Code complete: fuzzyFind({0:2})", toJSON(Req)); |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1396 | |
| 1397 | if (SpecFuzzyFind) |
| 1398 | SpecFuzzyFind->NewReq = Req; |
| 1399 | if (SpecFuzzyFind && SpecFuzzyFind->Result.valid() && (*SpecReq == Req)) { |
| 1400 | vlog("Code complete: speculative fuzzy request matches the actual index " |
| 1401 | "request. Waiting for the speculative index results."); |
| 1402 | SPAN_ATTACH(Tracer, "Speculative results", true); |
| 1403 | |
| 1404 | trace::Span WaitSpec("Wait speculative results"); |
| 1405 | return SpecFuzzyFind->Result.get(); |
| 1406 | } |
| 1407 | |
| 1408 | SPAN_ATTACH(Tracer, "Speculative results", false); |
| 1409 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1410 | // Run the query against the index. |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1411 | SymbolSlab::Builder ResultsBuilder; |
Sam McCall | ab8e393 | 2018-02-19 13:04:41 +0000 | [diff] [blame] | 1412 | if (Opts.Index->fuzzyFind( |
| 1413 | Req, [&](const Symbol &Sym) { ResultsBuilder.insert(Sym); })) |
| 1414 | Incomplete = true; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1415 | return std::move(ResultsBuilder).build(); |
| 1416 | } |
| 1417 | |
Kadir Cetinkaya | b15b8dc | 2018-10-02 09:42:17 +0000 | [diff] [blame] | 1418 | // Merges Sema and Index results where possible, to form CompletionCandidates. |
| 1419 | // Groups overloads if desired, to form CompletionCandidate::Bundles. The |
| 1420 | // bundles are scored and top results are returned, best to worst. |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1421 | std::vector<ScoredBundle> |
Kadir Cetinkaya | f8b85a3 | 2018-08-23 13:14:50 +0000 | [diff] [blame] | 1422 | mergeResults(const std::vector<CodeCompletionResult> &SemaResults, |
Kadir Cetinkaya | b15b8dc | 2018-10-02 09:42:17 +0000 | [diff] [blame] | 1423 | const SymbolSlab &IndexResults) { |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 1424 | trace::Span Tracer("Merge and score results"); |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1425 | std::vector<CompletionCandidate::Bundle> Bundles; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 1426 | llvm::DenseMap<size_t, size_t> BundleLookup; |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1427 | auto AddToBundles = [&](const CodeCompletionResult *SemaResult, |
Kadir Cetinkaya | b15b8dc | 2018-10-02 09:42:17 +0000 | [diff] [blame] | 1428 | const Symbol *IndexResult) { |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1429 | CompletionCandidate C; |
| 1430 | C.SemaResult = SemaResult; |
| 1431 | C.IndexResult = IndexResult; |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 1432 | if (C.IndexResult) |
| 1433 | C.RankedIncludeHeaders = getRankedIncludes(*C.IndexResult); |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1434 | C.Name = IndexResult ? IndexResult->Name : Recorder->getName(*SemaResult); |
| 1435 | if (auto OverloadSet = Opts.BundleOverloads ? C.overloadSet() : 0) { |
| 1436 | auto Ret = BundleLookup.try_emplace(OverloadSet, Bundles.size()); |
| 1437 | if (Ret.second) |
| 1438 | Bundles.emplace_back(); |
| 1439 | Bundles[Ret.first->second].push_back(std::move(C)); |
| 1440 | } else { |
| 1441 | Bundles.emplace_back(); |
| 1442 | Bundles.back().push_back(std::move(C)); |
| 1443 | } |
| 1444 | }; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 1445 | llvm::DenseSet<const Symbol *> UsedIndexResults; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1446 | auto CorrespondingIndexResult = |
| 1447 | [&](const CodeCompletionResult &SemaResult) -> const Symbol * { |
Eric Liu | d25f121 | 2018-09-06 09:59:37 +0000 | [diff] [blame] | 1448 | if (auto SymID = |
| 1449 | getSymbolID(SemaResult, Recorder->CCSema->getSourceManager())) { |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1450 | auto I = IndexResults.find(*SymID); |
| 1451 | if (I != IndexResults.end()) { |
| 1452 | UsedIndexResults.insert(&*I); |
| 1453 | return &*I; |
| 1454 | } |
| 1455 | } |
| 1456 | return nullptr; |
| 1457 | }; |
| 1458 | // Emit all Sema results, merging them with Index results if possible. |
Ilya Biryukov | ddf6a33 | 2018-03-02 12:28:27 +0000 | [diff] [blame] | 1459 | for (auto &SemaResult : Recorder->Results) |
Kadir Cetinkaya | b15b8dc | 2018-10-02 09:42:17 +0000 | [diff] [blame] | 1460 | AddToBundles(&SemaResult, CorrespondingIndexResult(SemaResult)); |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1461 | // Now emit any Index-only results. |
| 1462 | for (const auto &IndexResult : IndexResults) { |
| 1463 | if (UsedIndexResults.count(&IndexResult)) |
| 1464 | continue; |
Kadir Cetinkaya | b15b8dc | 2018-10-02 09:42:17 +0000 | [diff] [blame] | 1465 | AddToBundles(/*SemaResult=*/nullptr, &IndexResult); |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1466 | } |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1467 | // We only keep the best N results at any time, in "native" format. |
| 1468 | TopN<ScoredBundle, ScoredBundleGreater> Top( |
| 1469 | Opts.Limit == 0 ? std::numeric_limits<size_t>::max() : Opts.Limit); |
| 1470 | for (auto &Bundle : Bundles) |
| 1471 | addCandidate(Top, std::move(Bundle)); |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1472 | return std::move(Top).items(); |
| 1473 | } |
| 1474 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 1475 | llvm::Optional<float> fuzzyScore(const CompletionCandidate &C) { |
Sam McCall | 80ad707 | 2018-06-08 13:32:25 +0000 | [diff] [blame] | 1476 | // Macros can be very spammy, so we only support prefix completion. |
| 1477 | // We won't end up with underfull index results, as macros are sema-only. |
| 1478 | if (C.SemaResult && C.SemaResult->Kind == CodeCompletionResult::RK_Macro && |
| 1479 | !C.Name.startswith_lower(Filter->pattern())) |
| 1480 | return None; |
| 1481 | return Filter->match(C.Name); |
| 1482 | } |
| 1483 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1484 | // Scores a candidate and adds it to the TopN structure. |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1485 | void addCandidate(TopN<ScoredBundle, ScoredBundleGreater> &Candidates, |
| 1486 | CompletionCandidate::Bundle Bundle) { |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 1487 | SymbolQualitySignals Quality; |
| 1488 | SymbolRelevanceSignals Relevance; |
Eric Liu | 5d2a807 | 2018-07-23 10:56:37 +0000 | [diff] [blame] | 1489 | Relevance.Context = Recorder->CCContext.getKind(); |
Sam McCall | d9b54f0 | 2018-06-05 16:30:25 +0000 | [diff] [blame] | 1490 | Relevance.Query = SymbolRelevanceSignals::CodeComplete; |
Sam McCall | f84dd02 | 2018-07-05 08:26:53 +0000 | [diff] [blame] | 1491 | Relevance.FileProximityMatch = FileProximity.getPointer(); |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 1492 | if (ScopeProximity) |
| 1493 | Relevance.ScopeProximityMatch = ScopeProximity.getPointer(); |
Ilya Biryukov | 647da3e | 2018-11-26 15:38:01 +0000 | [diff] [blame] | 1494 | if (PreferredType) |
| 1495 | Relevance.HadContextType = true; |
Eric Liu | 670c147 | 2018-09-27 18:46:00 +0000 | [diff] [blame] | 1496 | |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1497 | auto &First = Bundle.front(); |
| 1498 | if (auto FuzzyScore = fuzzyScore(First)) |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 1499 | Relevance.NameMatch = *FuzzyScore; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1500 | else |
| 1501 | return; |
Sam McCall | 2161ec7 | 2018-07-05 06:20:41 +0000 | [diff] [blame] | 1502 | SymbolOrigin Origin = SymbolOrigin::Unknown; |
Sam McCall | 4e5742a | 2018-07-06 11:50:49 +0000 | [diff] [blame] | 1503 | bool FromIndex = false; |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1504 | for (const auto &Candidate : Bundle) { |
| 1505 | if (Candidate.IndexResult) { |
| 1506 | Quality.merge(*Candidate.IndexResult); |
| 1507 | Relevance.merge(*Candidate.IndexResult); |
Sam McCall | 4e5742a | 2018-07-06 11:50:49 +0000 | [diff] [blame] | 1508 | Origin |= Candidate.IndexResult->Origin; |
| 1509 | FromIndex = true; |
Ilya Biryukov | 647da3e | 2018-11-26 15:38:01 +0000 | [diff] [blame] | 1510 | if (!Candidate.IndexResult->Type.empty()) |
| 1511 | Relevance.HadSymbolType |= true; |
| 1512 | if (PreferredType && |
| 1513 | PreferredType->raw() == Candidate.IndexResult->Type) { |
| 1514 | Relevance.TypeMatchesPreferred = true; |
| 1515 | } |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1516 | } |
| 1517 | if (Candidate.SemaResult) { |
| 1518 | Quality.merge(*Candidate.SemaResult); |
| 1519 | Relevance.merge(*Candidate.SemaResult); |
Ilya Biryukov | 647da3e | 2018-11-26 15:38:01 +0000 | [diff] [blame] | 1520 | if (PreferredType) { |
| 1521 | if (auto CompletionType = OpaqueType::fromCompletionResult( |
| 1522 | Recorder->CCSema->getASTContext(), *Candidate.SemaResult)) { |
| 1523 | Relevance.HadSymbolType |= true; |
| 1524 | if (PreferredType == CompletionType) |
| 1525 | Relevance.TypeMatchesPreferred = true; |
| 1526 | } |
| 1527 | } |
Sam McCall | 4e5742a | 2018-07-06 11:50:49 +0000 | [diff] [blame] | 1528 | Origin |= SymbolOrigin::AST; |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1529 | } |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1532 | CodeCompletion::Scores Scores; |
| 1533 | Scores.Quality = Quality.evaluate(); |
| 1534 | Scores.Relevance = Relevance.evaluate(); |
| 1535 | Scores.Total = evaluateSymbolAndRelevance(Scores.Quality, Scores.Relevance); |
| 1536 | // NameMatch is in fact a multiplier on total score, so rescoring is sound. |
| 1537 | Scores.ExcludingName = Relevance.NameMatch |
| 1538 | ? Scores.Total / Relevance.NameMatch |
| 1539 | : Scores.Quality; |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 1540 | |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 1541 | dlog("CodeComplete: {0} ({1}) = {2}\n{3}{4}\n", First.Name, |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 1542 | llvm::to_string(Origin), Scores.Total, llvm::to_string(Quality), |
| 1543 | llvm::to_string(Relevance)); |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1544 | |
Sam McCall | 2161ec7 | 2018-07-05 06:20:41 +0000 | [diff] [blame] | 1545 | NSema += bool(Origin & SymbolOrigin::AST); |
Sam McCall | 4e5742a | 2018-07-06 11:50:49 +0000 | [diff] [blame] | 1546 | NIndex += FromIndex; |
| 1547 | NBoth += bool(Origin & SymbolOrigin::AST) && FromIndex; |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1548 | if (Candidates.push({std::move(Bundle), Scores})) |
Sam McCall | ab8e393 | 2018-02-19 13:04:41 +0000 | [diff] [blame] | 1549 | Incomplete = true; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1552 | CodeCompletion toCodeCompletion(const CompletionCandidate::Bundle &Bundle) { |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 1553 | llvm::Optional<CodeCompletionBuilder> Builder; |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1554 | for (const auto &Item : Bundle) { |
| 1555 | CodeCompletionString *SemaCCS = |
| 1556 | Item.SemaResult ? Recorder->codeCompletionString(*Item.SemaResult) |
| 1557 | : nullptr; |
| 1558 | if (!Builder) |
| 1559 | Builder.emplace(Recorder->CCSema->getASTContext(), Item, SemaCCS, |
Eric Liu | 670c147 | 2018-09-27 18:46:00 +0000 | [diff] [blame] | 1560 | QueryScopes, *Inserter, FileName, |
| 1561 | Recorder->CCContext.getKind(), Opts); |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1562 | else |
| 1563 | Builder->add(Item, SemaCCS); |
Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 1564 | } |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1565 | return Builder->build(); |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 1566 | } |
| 1567 | }; |
| 1568 | |
Haojian Wu | 9ff5001 | 2019-01-03 15:36:18 +0000 | [diff] [blame] | 1569 | } // namespace |
| 1570 | |
| 1571 | clang::CodeCompleteOptions CodeCompleteOptions::getClangCompleteOpts() const { |
| 1572 | clang::CodeCompleteOptions Result; |
| 1573 | Result.IncludeCodePatterns = EnableSnippets && IncludeCodePatterns; |
| 1574 | Result.IncludeMacros = IncludeMacros; |
| 1575 | Result.IncludeGlobals = true; |
| 1576 | // We choose to include full comments and not do doxygen parsing in |
| 1577 | // completion. |
| 1578 | // FIXME: ideally, we should support doxygen in some form, e.g. do markdown |
| 1579 | // formatting of the comments. |
| 1580 | Result.IncludeBriefComments = false; |
| 1581 | |
| 1582 | // When an is used, Sema is responsible for completing the main file, |
| 1583 | // the index can provide results from the preamble. |
| 1584 | // Tell Sema not to deserialize the preamble to look for results. |
| 1585 | Result.LoadExternal = !Index; |
| 1586 | Result.IncludeFixIts = IncludeFixIts; |
| 1587 | |
| 1588 | return Result; |
| 1589 | } |
| 1590 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 1591 | llvm::Expected<llvm::StringRef> |
| 1592 | speculateCompletionFilter(llvm::StringRef Content, Position Pos) { |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1593 | auto Offset = positionToOffset(Content, Pos); |
| 1594 | if (!Offset) |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 1595 | return llvm::make_error<llvm::StringError>( |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1596 | "Failed to convert position to offset in content.", |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 1597 | llvm::inconvertibleErrorCode()); |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1598 | if (*Offset == 0) |
| 1599 | return ""; |
| 1600 | |
| 1601 | // Start from the character before the cursor. |
| 1602 | int St = *Offset - 1; |
| 1603 | // FIXME(ioeric): consider UTF characters? |
| 1604 | auto IsValidIdentifierChar = [](char c) { |
| 1605 | return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || |
| 1606 | (c >= '0' && c <= '9') || (c == '_')); |
| 1607 | }; |
| 1608 | size_t Len = 0; |
| 1609 | for (; (St >= 0) && IsValidIdentifierChar(Content[St]); --St, ++Len) { |
| 1610 | } |
| 1611 | if (Len > 0) |
| 1612 | St++; // Shift to the first valid character. |
| 1613 | return Content.substr(St, Len); |
| 1614 | } |
| 1615 | |
| 1616 | CodeCompleteResult |
| 1617 | codeComplete(PathRef FileName, const tooling::CompileCommand &Command, |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 1618 | const PreambleData *Preamble, llvm::StringRef Contents, |
| 1619 | Position Pos, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1620 | std::shared_ptr<PCHContainerOperations> PCHs, |
| 1621 | CodeCompleteOptions Opts, SpeculativeFuzzyFind *SpecFuzzyFind) { |
Eric Liu | b1d7542 | 2018-10-02 10:43:55 +0000 | [diff] [blame] | 1622 | return CodeCompleteFlow(FileName, |
| 1623 | Preamble ? Preamble->Includes : IncludeStructure(), |
| 1624 | SpecFuzzyFind, Opts) |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1625 | .run({FileName, Command, Preamble, Contents, Pos, VFS, PCHs}); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 1628 | SignatureHelp signatureHelp(PathRef FileName, |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 1629 | const tooling::CompileCommand &Command, |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 1630 | const PreambleData *Preamble, |
| 1631 | llvm::StringRef Contents, Position Pos, |
| 1632 | llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 1633 | std::shared_ptr<PCHContainerOperations> PCHs, |
Sam McCall | 046557b | 2018-09-03 16:37:59 +0000 | [diff] [blame] | 1634 | const SymbolIndex *Index) { |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1635 | SignatureHelp Result; |
| 1636 | clang::CodeCompleteOptions Options; |
| 1637 | Options.IncludeGlobals = false; |
| 1638 | Options.IncludeMacros = false; |
| 1639 | Options.IncludeCodePatterns = false; |
Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 1640 | Options.IncludeBriefComments = false; |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1641 | IncludeStructure PreambleInclusions; // Unused for signatureHelp |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 1642 | semaCodeComplete( |
| 1643 | llvm::make_unique<SignatureHelpCollector>(Options, Index, Result), |
| 1644 | Options, |
| 1645 | {FileName, Command, Preamble, Contents, Pos, std::move(VFS), |
| 1646 | std::move(PCHs)}); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1647 | return Result; |
| 1648 | } |
| 1649 | |
Marc-Andre Laperle | 945b5a3 | 2018-06-05 14:01:40 +0000 | [diff] [blame] | 1650 | bool isIndexedForCodeCompletion(const NamedDecl &ND, ASTContext &ASTCtx) { |
Haojian Wu | dd61ccb | 2018-12-03 12:53:19 +0000 | [diff] [blame] | 1651 | auto InTopLevelScope = [](const NamedDecl &ND) { |
| 1652 | switch (ND.getDeclContext()->getDeclKind()) { |
| 1653 | case Decl::TranslationUnit: |
| 1654 | case Decl::Namespace: |
| 1655 | case Decl::LinkageSpec: |
| 1656 | return true; |
| 1657 | default: |
| 1658 | break; |
| 1659 | }; |
| 1660 | return false; |
| 1661 | }; |
| 1662 | if (InTopLevelScope(ND)) |
| 1663 | return true; |
| 1664 | |
| 1665 | if (const auto *EnumDecl = dyn_cast<clang::EnumDecl>(ND.getDeclContext())) |
| 1666 | return InTopLevelScope(*EnumDecl) && !EnumDecl->isScoped(); |
| 1667 | |
| 1668 | return false; |
Marc-Andre Laperle | 945b5a3 | 2018-06-05 14:01:40 +0000 | [diff] [blame] | 1669 | } |
| 1670 | |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1671 | CompletionItem CodeCompletion::render(const CodeCompleteOptions &Opts) const { |
| 1672 | CompletionItem LSP; |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 1673 | const auto *InsertInclude = Includes.empty() ? nullptr : &Includes[0]; |
| 1674 | LSP.label = ((InsertInclude && InsertInclude->Insertion) |
| 1675 | ? Opts.IncludeIndicator.Insert |
| 1676 | : Opts.IncludeIndicator.NoInsert) + |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 1677 | (Opts.ShowOrigins ? "[" + llvm::to_string(Origin) + "]" : "") + |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1678 | RequiredQualifier + Name + Signature; |
Sam McCall | 2161ec7 | 2018-07-05 06:20:41 +0000 | [diff] [blame] | 1679 | |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1680 | LSP.kind = Kind; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 1681 | LSP.detail = BundleSize > 1 ? llvm::formatv("[{0} overloads]", BundleSize) |
| 1682 | : ReturnType; |
Eric Liu | 6df6600 | 2018-09-06 18:52:26 +0000 | [diff] [blame] | 1683 | LSP.deprecated = Deprecated; |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 1684 | if (InsertInclude) |
| 1685 | LSP.detail += "\n" + InsertInclude->Header; |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1686 | LSP.documentation = Documentation; |
| 1687 | LSP.sortText = sortText(Score.Total, Name); |
| 1688 | LSP.filterText = Name; |
Kadir Cetinkaya | a9c9d00 | 2018-08-13 08:23:01 +0000 | [diff] [blame] | 1689 | LSP.textEdit = {CompletionTokenRange, RequiredQualifier + Name}; |
Fangrui Song | 445bdd1 | 2018-09-05 08:01:37 +0000 | [diff] [blame] | 1690 | // Merge continuous additionalTextEdits into main edit. The main motivation |
Kadir Cetinkaya | a9c9d00 | 2018-08-13 08:23:01 +0000 | [diff] [blame] | 1691 | // behind this is to help LSP clients, it seems most of them are confused when |
| 1692 | // they are provided with additionalTextEdits that are consecutive to main |
| 1693 | // edit. |
| 1694 | // Note that we store additional text edits from back to front in a line. That |
| 1695 | // is mainly to help LSP clients again, so that changes do not effect each |
| 1696 | // other. |
| 1697 | for (const auto &FixIt : FixIts) { |
| 1698 | if (IsRangeConsecutive(FixIt.range, LSP.textEdit->range)) { |
| 1699 | LSP.textEdit->newText = FixIt.newText + LSP.textEdit->newText; |
| 1700 | LSP.textEdit->range.start = FixIt.range.start; |
| 1701 | } else { |
| 1702 | LSP.additionalTextEdits.push_back(FixIt); |
| 1703 | } |
| 1704 | } |
Kadir Cetinkaya | 516fcda | 2018-08-23 12:19:39 +0000 | [diff] [blame] | 1705 | if (Opts.EnableSnippets) |
| 1706 | LSP.textEdit->newText += SnippetSuffix; |
Kadir Cetinkaya | 6c9f15c | 2018-08-17 15:42:54 +0000 | [diff] [blame] | 1707 | |
Kadir Cetinkaya | a9c9d00 | 2018-08-13 08:23:01 +0000 | [diff] [blame] | 1708 | // FIXME(kadircet): Do not even fill insertText after making sure textEdit is |
| 1709 | // compatible with most of the editors. |
| 1710 | LSP.insertText = LSP.textEdit->newText; |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1711 | LSP.insertTextFormat = Opts.EnableSnippets ? InsertTextFormat::Snippet |
| 1712 | : InsertTextFormat::PlainText; |
Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 1713 | if (InsertInclude && InsertInclude->Insertion) |
| 1714 | LSP.additionalTextEdits.push_back(*InsertInclude->Insertion); |
Eric Liu | 6df6600 | 2018-09-06 18:52:26 +0000 | [diff] [blame] | 1715 | |
Sam McCall | 27c979a | 2018-06-29 14:47:57 +0000 | [diff] [blame] | 1716 | return LSP; |
| 1717 | } |
| 1718 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 1719 | llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const CodeCompletion &C) { |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1720 | // For now just lean on CompletionItem. |
| 1721 | return OS << C.render(CodeCompleteOptions()); |
| 1722 | } |
| 1723 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 1724 | llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, |
| 1725 | const CodeCompleteResult &R) { |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1726 | OS << "CodeCompleteResult: " << R.Completions.size() << (R.HasMore ? "+" : "") |
Eric Liu | 5d2a807 | 2018-07-23 10:56:37 +0000 | [diff] [blame] | 1727 | << " (" << getCompletionKindString(R.Context) << ")" |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1728 | << " items:\n"; |
| 1729 | for (const auto &C : R.Completions) |
| 1730 | OS << C << "\n"; |
| 1731 | return OS; |
| 1732 | } |
| 1733 | |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 1734 | } // namespace clangd |
| 1735 | } // namespace clang |