/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/tools/libclang/CIndexCodeCompletion.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===- CIndexCodeCompletion.cpp - Code Completion API hooks ---------------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file implements the Clang-C Source Indexing library hooks for |
10 | | // code completion. |
11 | | // |
12 | | //===----------------------------------------------------------------------===// |
13 | | |
14 | | #include "CIndexDiagnostic.h" |
15 | | #include "CIndexer.h" |
16 | | #include "CLog.h" |
17 | | #include "CXCursor.h" |
18 | | #include "CXSourceLocation.h" |
19 | | #include "CXString.h" |
20 | | #include "CXTranslationUnit.h" |
21 | | #include "clang/AST/Decl.h" |
22 | | #include "clang/AST/DeclObjC.h" |
23 | | #include "clang/AST/Type.h" |
24 | | #include "clang/Basic/FileManager.h" |
25 | | #include "clang/Basic/SourceManager.h" |
26 | | #include "clang/Frontend/ASTUnit.h" |
27 | | #include "clang/Frontend/CompilerInstance.h" |
28 | | #include "clang/Frontend/FrontendActions.h" |
29 | | #include "clang/Sema/CodeCompleteConsumer.h" |
30 | | #include "clang/Sema/Sema.h" |
31 | | #include "llvm/ADT/SmallString.h" |
32 | | #include "llvm/ADT/StringExtras.h" |
33 | | #include "llvm/Support/CrashRecoveryContext.h" |
34 | | #include "llvm/Support/FileSystem.h" |
35 | | #include "llvm/Support/FormatVariadic.h" |
36 | | #include "llvm/Support/MemoryBuffer.h" |
37 | | #include "llvm/Support/Program.h" |
38 | | #include "llvm/Support/Timer.h" |
39 | | #include "llvm/Support/raw_ostream.h" |
40 | | #include <atomic> |
41 | | #include <cstdio> |
42 | | #include <cstdlib> |
43 | | #include <string> |
44 | | |
45 | | #ifdef UDP_CODE_COMPLETION_LOGGER |
46 | | #include "clang/Basic/Version.h" |
47 | | #include <arpa/inet.h> |
48 | | #include <sys/socket.h> |
49 | | #include <sys/types.h> |
50 | | #include <unistd.h> |
51 | | #endif |
52 | | |
53 | | using namespace clang; |
54 | | using namespace clang::cxindex; |
55 | | |
56 | | enum CXCompletionChunkKind |
57 | | clang_getCompletionChunkKind(CXCompletionString completion_string, |
58 | 157k | unsigned chunk_number) { |
59 | 157k | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
60 | 157k | if (!CCStr || chunk_number >= CCStr->size()) |
61 | 0 | return CXCompletionChunk_Text; |
62 | | |
63 | 157k | switch ((*CCStr)[chunk_number].Kind) { |
64 | 127k | case CodeCompletionString::CK_TypedText: |
65 | 127k | return CXCompletionChunk_TypedText; |
66 | 1.30k | case CodeCompletionString::CK_Text: |
67 | 1.30k | return CXCompletionChunk_Text; |
68 | 70 | case CodeCompletionString::CK_Optional: |
69 | 70 | return CXCompletionChunk_Optional; |
70 | 8.00k | case CodeCompletionString::CK_Placeholder: |
71 | 8.00k | return CXCompletionChunk_Placeholder; |
72 | 146 | case CodeCompletionString::CK_Informative: |
73 | 146 | return CXCompletionChunk_Informative; |
74 | 4.88k | case CodeCompletionString::CK_ResultType: |
75 | 4.88k | return CXCompletionChunk_ResultType; |
76 | 144 | case CodeCompletionString::CK_CurrentParameter: |
77 | 144 | return CXCompletionChunk_CurrentParameter; |
78 | 5.01k | case CodeCompletionString::CK_LeftParen: |
79 | 5.01k | return CXCompletionChunk_LeftParen; |
80 | 5.06k | case CodeCompletionString::CK_RightParen: |
81 | 5.06k | return CXCompletionChunk_RightParen; |
82 | 246 | case CodeCompletionString::CK_LeftBracket: |
83 | 246 | return CXCompletionChunk_LeftBracket; |
84 | 291 | case CodeCompletionString::CK_RightBracket: |
85 | 291 | return CXCompletionChunk_RightBracket; |
86 | 62 | case CodeCompletionString::CK_LeftBrace: |
87 | 62 | return CXCompletionChunk_LeftBrace; |
88 | 107 | case CodeCompletionString::CK_RightBrace: |
89 | 107 | return CXCompletionChunk_RightBrace; |
90 | 693 | case CodeCompletionString::CK_LeftAngle: |
91 | 693 | return CXCompletionChunk_LeftAngle; |
92 | 693 | case CodeCompletionString::CK_RightAngle: |
93 | 693 | return CXCompletionChunk_RightAngle; |
94 | 1.33k | case CodeCompletionString::CK_Comma: |
95 | 1.33k | return CXCompletionChunk_Comma; |
96 | 51 | case CodeCompletionString::CK_Colon: |
97 | 51 | return CXCompletionChunk_Colon; |
98 | 317 | case CodeCompletionString::CK_SemiColon: |
99 | 317 | return CXCompletionChunk_SemiColon; |
100 | 55 | case CodeCompletionString::CK_Equal: |
101 | 55 | return CXCompletionChunk_Equal; |
102 | 1.83k | case CodeCompletionString::CK_HorizontalSpace: |
103 | 1.83k | return CXCompletionChunk_HorizontalSpace; |
104 | 100 | case CodeCompletionString::CK_VerticalSpace: |
105 | 100 | return CXCompletionChunk_VerticalSpace; |
106 | 157k | } |
107 | | |
108 | 0 | llvm_unreachable("Invalid CompletionKind!"); |
109 | 0 | } |
110 | | |
111 | | CXString clang_getCompletionChunkText(CXCompletionString completion_string, |
112 | 157k | unsigned chunk_number) { |
113 | 157k | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
114 | 157k | if (!CCStr || chunk_number >= CCStr->size()) |
115 | 0 | return cxstring::createNull(); |
116 | | |
117 | 157k | switch ((*CCStr)[chunk_number].Kind) { |
118 | 127k | case CodeCompletionString::CK_TypedText: |
119 | 128k | case CodeCompletionString::CK_Text: |
120 | 136k | case CodeCompletionString::CK_Placeholder: |
121 | 136k | case CodeCompletionString::CK_CurrentParameter: |
122 | 137k | case CodeCompletionString::CK_Informative: |
123 | 142k | case CodeCompletionString::CK_LeftParen: |
124 | 147k | case CodeCompletionString::CK_RightParen: |
125 | 147k | case CodeCompletionString::CK_LeftBracket: |
126 | 147k | case CodeCompletionString::CK_RightBracket: |
127 | 147k | case CodeCompletionString::CK_LeftBrace: |
128 | 147k | case CodeCompletionString::CK_RightBrace: |
129 | 148k | case CodeCompletionString::CK_LeftAngle: |
130 | 149k | case CodeCompletionString::CK_RightAngle: |
131 | 150k | case CodeCompletionString::CK_Comma: |
132 | 155k | case CodeCompletionString::CK_ResultType: |
133 | 155k | case CodeCompletionString::CK_Colon: |
134 | 155k | case CodeCompletionString::CK_SemiColon: |
135 | 155k | case CodeCompletionString::CK_Equal: |
136 | 157k | case CodeCompletionString::CK_HorizontalSpace: |
137 | 157k | case CodeCompletionString::CK_VerticalSpace: |
138 | 157k | return cxstring::createRef((*CCStr)[chunk_number].Text); |
139 | | |
140 | 0 | case CodeCompletionString::CK_Optional: |
141 | | // Note: treated as an empty text block. |
142 | 0 | return cxstring::createEmpty(); |
143 | 157k | } |
144 | | |
145 | 0 | llvm_unreachable("Invalid CodeCompletionString Kind!"); |
146 | 0 | } |
147 | | |
148 | | |
149 | | CXCompletionString |
150 | | clang_getCompletionChunkCompletionString(CXCompletionString completion_string, |
151 | 70 | unsigned chunk_number) { |
152 | 70 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
153 | 70 | if (!CCStr || chunk_number >= CCStr->size()) |
154 | 0 | return nullptr; |
155 | | |
156 | 70 | switch ((*CCStr)[chunk_number].Kind) { |
157 | 0 | case CodeCompletionString::CK_TypedText: |
158 | 0 | case CodeCompletionString::CK_Text: |
159 | 0 | case CodeCompletionString::CK_Placeholder: |
160 | 0 | case CodeCompletionString::CK_CurrentParameter: |
161 | 0 | case CodeCompletionString::CK_Informative: |
162 | 0 | case CodeCompletionString::CK_LeftParen: |
163 | 0 | case CodeCompletionString::CK_RightParen: |
164 | 0 | case CodeCompletionString::CK_LeftBracket: |
165 | 0 | case CodeCompletionString::CK_RightBracket: |
166 | 0 | case CodeCompletionString::CK_LeftBrace: |
167 | 0 | case CodeCompletionString::CK_RightBrace: |
168 | 0 | case CodeCompletionString::CK_LeftAngle: |
169 | 0 | case CodeCompletionString::CK_RightAngle: |
170 | 0 | case CodeCompletionString::CK_Comma: |
171 | 0 | case CodeCompletionString::CK_ResultType: |
172 | 0 | case CodeCompletionString::CK_Colon: |
173 | 0 | case CodeCompletionString::CK_SemiColon: |
174 | 0 | case CodeCompletionString::CK_Equal: |
175 | 0 | case CodeCompletionString::CK_HorizontalSpace: |
176 | 0 | case CodeCompletionString::CK_VerticalSpace: |
177 | 0 | return nullptr; |
178 | | |
179 | 70 | case CodeCompletionString::CK_Optional: |
180 | | // Note: treated as an empty text block. |
181 | 70 | return (*CCStr)[chunk_number].Optional; |
182 | 70 | } |
183 | | |
184 | 0 | llvm_unreachable("Invalid CompletionKind!"); |
185 | 0 | } |
186 | | |
187 | 127k | unsigned clang_getNumCompletionChunks(CXCompletionString completion_string) { |
188 | 127k | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
189 | 127k | return CCStr? CCStr->size() : 00 ; |
190 | 127k | } |
191 | | |
192 | 127k | unsigned clang_getCompletionPriority(CXCompletionString completion_string) { |
193 | 127k | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
194 | 127k | return CCStr? CCStr->getPriority() : unsigned(CCP_Unlikely)0 ; |
195 | 127k | } |
196 | | |
197 | | enum CXAvailabilityKind |
198 | 127k | clang_getCompletionAvailability(CXCompletionString completion_string) { |
199 | 127k | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
200 | 127k | return CCStr? static_cast<CXAvailabilityKind>(CCStr->getAvailability()) |
201 | 127k | : CXAvailability_Available0 ; |
202 | 127k | } |
203 | | |
204 | | unsigned clang_getCompletionNumAnnotations(CXCompletionString completion_string) |
205 | 127k | { |
206 | 127k | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
207 | 127k | return CCStr ? CCStr->getAnnotationCount() : 00 ; |
208 | 127k | } |
209 | | |
210 | | CXString clang_getCompletionAnnotation(CXCompletionString completion_string, |
211 | 6 | unsigned annotation_number) { |
212 | 6 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
213 | 6 | return CCStr ? cxstring::createRef(CCStr->getAnnotation(annotation_number)) |
214 | 6 | : cxstring::createNull()0 ; |
215 | 6 | } |
216 | | |
217 | | CXString |
218 | | clang_getCompletionParent(CXCompletionString completion_string, |
219 | 127k | CXCursorKind *kind) { |
220 | 127k | if (kind) |
221 | 127k | *kind = CXCursor_NotImplemented; |
222 | | |
223 | 127k | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
224 | 127k | if (!CCStr) |
225 | 0 | return cxstring::createNull(); |
226 | | |
227 | 127k | return cxstring::createRef(CCStr->getParentContextName()); |
228 | 127k | } |
229 | | |
230 | | CXString |
231 | 127k | clang_getCompletionBriefComment(CXCompletionString completion_string) { |
232 | 127k | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
233 | | |
234 | 127k | if (!CCStr) |
235 | 0 | return cxstring::createNull(); |
236 | | |
237 | 127k | return cxstring::createRef(CCStr->getBriefComment()); |
238 | 127k | } |
239 | | |
240 | | namespace { |
241 | | |
242 | | /// The CXCodeCompleteResults structure we allocate internally; |
243 | | /// the client only sees the initial CXCodeCompleteResults structure. |
244 | | /// |
245 | | /// Normally, clients of CXString shouldn't care whether or not a CXString is |
246 | | /// managed by a pool or by explicitly malloc'ed memory. But |
247 | | /// AllocatedCXCodeCompleteResults outlives the CXTranslationUnit, so we can |
248 | | /// not rely on the StringPool in the TU. |
249 | | struct AllocatedCXCodeCompleteResults : public CXCodeCompleteResults { |
250 | | AllocatedCXCodeCompleteResults(IntrusiveRefCntPtr<FileManager> FileMgr); |
251 | | ~AllocatedCXCodeCompleteResults(); |
252 | | |
253 | | /// Diagnostics produced while performing code completion. |
254 | | SmallVector<StoredDiagnostic, 8> Diagnostics; |
255 | | |
256 | | /// Allocated API-exposed wrappters for Diagnostics. |
257 | | SmallVector<std::unique_ptr<CXStoredDiagnostic>, 8> DiagnosticsWrappers; |
258 | | |
259 | | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts; |
260 | | |
261 | | /// Diag object |
262 | | IntrusiveRefCntPtr<DiagnosticsEngine> Diag; |
263 | | |
264 | | /// Language options used to adjust source locations. |
265 | | LangOptions LangOpts; |
266 | | |
267 | | /// File manager, used for diagnostics. |
268 | | IntrusiveRefCntPtr<FileManager> FileMgr; |
269 | | |
270 | | /// Source manager, used for diagnostics. |
271 | | IntrusiveRefCntPtr<SourceManager> SourceMgr; |
272 | | |
273 | | /// Temporary buffers that will be deleted once we have finished with |
274 | | /// the code-completion results. |
275 | | SmallVector<const llvm::MemoryBuffer *, 1> TemporaryBuffers; |
276 | | |
277 | | /// Allocator used to store globally cached code-completion results. |
278 | | std::shared_ptr<clang::GlobalCodeCompletionAllocator> |
279 | | CachedCompletionAllocator; |
280 | | |
281 | | /// Allocator used to store code completion results. |
282 | | std::shared_ptr<clang::GlobalCodeCompletionAllocator> CodeCompletionAllocator; |
283 | | |
284 | | /// Context under which completion occurred. |
285 | | enum clang::CodeCompletionContext::Kind ContextKind; |
286 | | |
287 | | /// A bitfield representing the acceptable completions for the |
288 | | /// current context. |
289 | | unsigned long long Contexts; |
290 | | |
291 | | /// The kind of the container for the current context for completions. |
292 | | enum CXCursorKind ContainerKind; |
293 | | |
294 | | /// The USR of the container for the current context for completions. |
295 | | std::string ContainerUSR; |
296 | | |
297 | | /// a boolean value indicating whether there is complete information |
298 | | /// about the container |
299 | | unsigned ContainerIsIncomplete; |
300 | | |
301 | | /// A string containing the Objective-C selector entered thus far for a |
302 | | /// message send. |
303 | | std::string Selector; |
304 | | |
305 | | /// Vector of fix-its for each completion result that *must* be applied |
306 | | /// before that result for the corresponding completion item. |
307 | | std::vector<std::vector<FixItHint>> FixItsVector; |
308 | | }; |
309 | | |
310 | | } // end anonymous namespace |
311 | | |
312 | | unsigned clang_getCompletionNumFixIts(CXCodeCompleteResults *results, |
313 | 127k | unsigned completion_index) { |
314 | 127k | AllocatedCXCodeCompleteResults *allocated_results = (AllocatedCXCodeCompleteResults *)results; |
315 | | |
316 | 127k | if (!allocated_results || allocated_results->FixItsVector.size() <= completion_index) |
317 | 127k | return 0; |
318 | | |
319 | 24 | return static_cast<unsigned>(allocated_results->FixItsVector[completion_index].size()); |
320 | 127k | } |
321 | | |
322 | | CXString clang_getCompletionFixIt(CXCodeCompleteResults *results, |
323 | | unsigned completion_index, |
324 | | unsigned fixit_index, |
325 | 12 | CXSourceRange *replacement_range) { |
326 | 12 | AllocatedCXCodeCompleteResults *allocated_results = (AllocatedCXCodeCompleteResults *)results; |
327 | | |
328 | 12 | if (!allocated_results || allocated_results->FixItsVector.size() <= completion_index) { |
329 | 0 | if (replacement_range) |
330 | 0 | *replacement_range = clang_getNullRange(); |
331 | 0 | return cxstring::createNull(); |
332 | 0 | } |
333 | | |
334 | 12 | ArrayRef<FixItHint> FixIts = allocated_results->FixItsVector[completion_index]; |
335 | 12 | if (FixIts.size() <= fixit_index) { |
336 | 0 | if (replacement_range) |
337 | 0 | *replacement_range = clang_getNullRange(); |
338 | 0 | return cxstring::createNull(); |
339 | 0 | } |
340 | | |
341 | 12 | const FixItHint &FixIt = FixIts[fixit_index]; |
342 | 12 | if (replacement_range) { |
343 | 12 | *replacement_range = cxloc::translateSourceRange( |
344 | 12 | *allocated_results->SourceMgr, allocated_results->LangOpts, |
345 | 12 | FixIt.RemoveRange); |
346 | 12 | } |
347 | | |
348 | 12 | return cxstring::createRef(FixIt.CodeToInsert.c_str()); |
349 | 12 | } |
350 | | |
351 | | /// Tracks the number of code-completion result objects that are |
352 | | /// currently active. |
353 | | /// |
354 | | /// Used for debugging purposes only. |
355 | | static std::atomic<unsigned> CodeCompletionResultObjects; |
356 | | |
357 | | AllocatedCXCodeCompleteResults::AllocatedCXCodeCompleteResults( |
358 | | IntrusiveRefCntPtr<FileManager> FileMgr) |
359 | 721 | : CXCodeCompleteResults(), DiagOpts(new DiagnosticOptions), |
360 | 721 | Diag(new DiagnosticsEngine( |
361 | 721 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), &*DiagOpts)), |
362 | 721 | FileMgr(std::move(FileMgr)), |
363 | 721 | SourceMgr(new SourceManager(*Diag, *this->FileMgr)), |
364 | | CodeCompletionAllocator( |
365 | 721 | std::make_shared<clang::GlobalCodeCompletionAllocator>()), |
366 | 721 | Contexts(CXCompletionContext_Unknown), |
367 | 721 | ContainerKind(CXCursor_InvalidCode), ContainerIsIncomplete(1) { |
368 | 721 | if (getenv("LIBCLANG_OBJTRACKING")) |
369 | 0 | fprintf(stderr, "+++ %u completion results\n", |
370 | 0 | ++CodeCompletionResultObjects); |
371 | 721 | } |
372 | | |
373 | 718 | AllocatedCXCodeCompleteResults::~AllocatedCXCodeCompleteResults() { |
374 | 718 | delete [] Results; |
375 | | |
376 | 794 | for (unsigned I = 0, N = TemporaryBuffers.size(); I != N; ++I76 ) |
377 | 76 | delete TemporaryBuffers[I]; |
378 | | |
379 | 718 | if (getenv("LIBCLANG_OBJTRACKING")) |
380 | 0 | fprintf(stderr, "--- %u completion results\n", |
381 | 0 | --CodeCompletionResultObjects); |
382 | 718 | } |
383 | | |
384 | | static unsigned long long getContextsForContextKind( |
385 | | enum CodeCompletionContext::Kind kind, |
386 | 716 | Sema &S) { |
387 | 716 | unsigned long long contexts = 0; |
388 | 716 | switch (kind) { |
389 | 0 | case CodeCompletionContext::CCC_OtherWithMacros: { |
390 | | //We can allow macros here, but we don't know what else is permissible |
391 | | //So we'll say the only thing permissible are macros |
392 | 0 | contexts = CXCompletionContext_MacroName; |
393 | 0 | break; |
394 | 0 | } |
395 | 40 | case CodeCompletionContext::CCC_TopLevel: |
396 | 41 | case CodeCompletionContext::CCC_ObjCIvarList: |
397 | 44 | case CodeCompletionContext::CCC_ClassStructUnion: |
398 | 53 | case CodeCompletionContext::CCC_Type: { |
399 | 53 | contexts = CXCompletionContext_AnyType | |
400 | 53 | CXCompletionContext_ObjCInterface; |
401 | 53 | if (S.getLangOpts().CPlusPlus) { |
402 | 18 | contexts |= CXCompletionContext_EnumTag | |
403 | 18 | CXCompletionContext_UnionTag | |
404 | 18 | CXCompletionContext_StructTag | |
405 | 18 | CXCompletionContext_ClassTag | |
406 | 18 | CXCompletionContext_NestedNameSpecifier; |
407 | 18 | } |
408 | 53 | break; |
409 | 44 | } |
410 | 75 | case CodeCompletionContext::CCC_Statement: { |
411 | 75 | contexts = CXCompletionContext_AnyType | |
412 | 75 | CXCompletionContext_ObjCInterface | |
413 | 75 | CXCompletionContext_AnyValue; |
414 | 75 | if (S.getLangOpts().CPlusPlus) { |
415 | 31 | contexts |= CXCompletionContext_EnumTag | |
416 | 31 | CXCompletionContext_UnionTag | |
417 | 31 | CXCompletionContext_StructTag | |
418 | 31 | CXCompletionContext_ClassTag | |
419 | 31 | CXCompletionContext_NestedNameSpecifier; |
420 | 31 | } |
421 | 75 | break; |
422 | 44 | } |
423 | 186 | case CodeCompletionContext::CCC_Expression: { |
424 | 186 | contexts = CXCompletionContext_AnyValue; |
425 | 186 | if (S.getLangOpts().CPlusPlus) { |
426 | 106 | contexts |= CXCompletionContext_AnyType | |
427 | 106 | CXCompletionContext_ObjCInterface | |
428 | 106 | CXCompletionContext_EnumTag | |
429 | 106 | CXCompletionContext_UnionTag | |
430 | 106 | CXCompletionContext_StructTag | |
431 | 106 | CXCompletionContext_ClassTag | |
432 | 106 | CXCompletionContext_NestedNameSpecifier; |
433 | 106 | } |
434 | 186 | break; |
435 | 44 | } |
436 | 7 | case CodeCompletionContext::CCC_ObjCMessageReceiver: { |
437 | 7 | contexts = CXCompletionContext_ObjCObjectValue | |
438 | 7 | CXCompletionContext_ObjCSelectorValue | |
439 | 7 | CXCompletionContext_ObjCInterface; |
440 | 7 | if (S.getLangOpts().CPlusPlus) { |
441 | 2 | contexts |= CXCompletionContext_CXXClassTypeValue | |
442 | 2 | CXCompletionContext_AnyType | |
443 | 2 | CXCompletionContext_EnumTag | |
444 | 2 | CXCompletionContext_UnionTag | |
445 | 2 | CXCompletionContext_StructTag | |
446 | 2 | CXCompletionContext_ClassTag | |
447 | 2 | CXCompletionContext_NestedNameSpecifier; |
448 | 2 | } |
449 | 7 | break; |
450 | 44 | } |
451 | 22 | case CodeCompletionContext::CCC_DotMemberAccess: { |
452 | 22 | contexts = CXCompletionContext_DotMemberAccess; |
453 | 22 | break; |
454 | 44 | } |
455 | 24 | case CodeCompletionContext::CCC_ArrowMemberAccess: { |
456 | 24 | contexts = CXCompletionContext_ArrowMemberAccess; |
457 | 24 | break; |
458 | 44 | } |
459 | 24 | case CodeCompletionContext::CCC_ObjCPropertyAccess: { |
460 | 24 | contexts = CXCompletionContext_ObjCPropertyAccess; |
461 | 24 | break; |
462 | 44 | } |
463 | 0 | case CodeCompletionContext::CCC_EnumTag: { |
464 | 0 | contexts = CXCompletionContext_EnumTag | |
465 | 0 | CXCompletionContext_NestedNameSpecifier; |
466 | 0 | break; |
467 | 44 | } |
468 | 0 | case CodeCompletionContext::CCC_UnionTag: { |
469 | 0 | contexts = CXCompletionContext_UnionTag | |
470 | 0 | CXCompletionContext_NestedNameSpecifier; |
471 | 0 | break; |
472 | 44 | } |
473 | 6 | case CodeCompletionContext::CCC_ClassOrStructTag: { |
474 | 6 | contexts = CXCompletionContext_StructTag | |
475 | 6 | CXCompletionContext_ClassTag | |
476 | 6 | CXCompletionContext_NestedNameSpecifier; |
477 | 6 | break; |
478 | 44 | } |
479 | 9 | case CodeCompletionContext::CCC_ObjCProtocolName: { |
480 | 9 | contexts = CXCompletionContext_ObjCProtocol; |
481 | 9 | break; |
482 | 44 | } |
483 | 0 | case CodeCompletionContext::CCC_Namespace: { |
484 | 0 | contexts = CXCompletionContext_Namespace; |
485 | 0 | break; |
486 | 44 | } |
487 | 14 | case CodeCompletionContext::CCC_SymbolOrNewName: |
488 | 49 | case CodeCompletionContext::CCC_Symbol: { |
489 | 49 | contexts = CXCompletionContext_NestedNameSpecifier; |
490 | 49 | break; |
491 | 14 | } |
492 | 8 | case CodeCompletionContext::CCC_MacroNameUse: { |
493 | 8 | contexts = CXCompletionContext_MacroName; |
494 | 8 | break; |
495 | 14 | } |
496 | 45 | case CodeCompletionContext::CCC_NaturalLanguage: { |
497 | 45 | contexts = CXCompletionContext_NaturalLanguage; |
498 | 45 | break; |
499 | 14 | } |
500 | 0 | case CodeCompletionContext::CCC_IncludedFile: { |
501 | 0 | contexts = CXCompletionContext_IncludedFile; |
502 | 0 | break; |
503 | 14 | } |
504 | 2 | case CodeCompletionContext::CCC_SelectorName: { |
505 | 2 | contexts = CXCompletionContext_ObjCSelectorName; |
506 | 2 | break; |
507 | 14 | } |
508 | 13 | case CodeCompletionContext::CCC_ParenthesizedExpression: { |
509 | 13 | contexts = CXCompletionContext_AnyType | |
510 | 13 | CXCompletionContext_ObjCInterface | |
511 | 13 | CXCompletionContext_AnyValue; |
512 | 13 | if (S.getLangOpts().CPlusPlus) { |
513 | 0 | contexts |= CXCompletionContext_EnumTag | |
514 | 0 | CXCompletionContext_UnionTag | |
515 | 0 | CXCompletionContext_StructTag | |
516 | 0 | CXCompletionContext_ClassTag | |
517 | 0 | CXCompletionContext_NestedNameSpecifier; |
518 | 0 | } |
519 | 13 | break; |
520 | 14 | } |
521 | 50 | case CodeCompletionContext::CCC_ObjCInstanceMessage: { |
522 | 50 | contexts = CXCompletionContext_ObjCInstanceMessage; |
523 | 50 | break; |
524 | 14 | } |
525 | 22 | case CodeCompletionContext::CCC_ObjCClassMessage: { |
526 | 22 | contexts = CXCompletionContext_ObjCClassMessage; |
527 | 22 | break; |
528 | 14 | } |
529 | 8 | case CodeCompletionContext::CCC_ObjCInterfaceName: { |
530 | 8 | contexts = CXCompletionContext_ObjCInterface; |
531 | 8 | break; |
532 | 14 | } |
533 | 5 | case CodeCompletionContext::CCC_ObjCCategoryName: { |
534 | 5 | contexts = CXCompletionContext_ObjCCategory; |
535 | 5 | break; |
536 | 14 | } |
537 | 65 | case CodeCompletionContext::CCC_Other: |
538 | 66 | case CodeCompletionContext::CCC_ObjCInterface: |
539 | 70 | case CodeCompletionContext::CCC_ObjCImplementation: |
540 | 70 | case CodeCompletionContext::CCC_ObjCClassForwardDecl: |
541 | 87 | case CodeCompletionContext::CCC_NewName: |
542 | 87 | case CodeCompletionContext::CCC_MacroName: |
543 | 93 | case CodeCompletionContext::CCC_PreprocessorExpression: |
544 | 105 | case CodeCompletionContext::CCC_PreprocessorDirective: |
545 | 105 | case CodeCompletionContext::CCC_Attribute: |
546 | 105 | case CodeCompletionContext::CCC_TopLevelOrExpression: |
547 | 107 | case CodeCompletionContext::CCC_TypeQualifiers: { |
548 | | //Only Clang results should be accepted, so we'll set all of the other |
549 | | //context bits to 0 (i.e. the empty set) |
550 | 107 | contexts = CXCompletionContext_Unexposed; |
551 | 107 | break; |
552 | 105 | } |
553 | 1 | case CodeCompletionContext::CCC_Recovery: { |
554 | | //We don't know what the current context is, so we'll return unknown |
555 | | //This is the equivalent of setting all of the other context bits |
556 | 1 | contexts = CXCompletionContext_Unknown; |
557 | 1 | break; |
558 | 105 | } |
559 | 716 | } |
560 | 716 | return contexts; |
561 | 716 | } |
562 | | |
563 | | namespace { |
564 | | class CaptureCompletionResults : public CodeCompleteConsumer { |
565 | | AllocatedCXCodeCompleteResults &AllocatedResults; |
566 | | CodeCompletionTUInfo CCTUInfo; |
567 | | SmallVector<CXCompletionResult, 16> StoredResults; |
568 | | CXTranslationUnit *TU; |
569 | | public: |
570 | | CaptureCompletionResults(const CodeCompleteOptions &Opts, |
571 | | AllocatedCXCodeCompleteResults &Results, |
572 | | CXTranslationUnit *TranslationUnit) |
573 | 721 | : CodeCompleteConsumer(Opts), AllocatedResults(Results), |
574 | 721 | CCTUInfo(Results.CodeCompletionAllocator), TU(TranslationUnit) {} |
575 | 718 | ~CaptureCompletionResults() override { Finish(); } |
576 | | |
577 | | void ProcessCodeCompleteResults(Sema &S, |
578 | | CodeCompletionContext Context, |
579 | | CodeCompletionResult *Results, |
580 | 716 | unsigned NumResults) override { |
581 | 716 | StoredResults.reserve(StoredResults.size() + NumResults); |
582 | 716 | if (includeFixIts()) |
583 | 2 | AllocatedResults.FixItsVector.reserve(NumResults); |
584 | 177k | for (unsigned I = 0; I != NumResults; ++I176k ) { |
585 | 176k | CodeCompletionString *StoredCompletion |
586 | 176k | = Results[I].CreateCodeCompletionString(S, Context, getAllocator(), |
587 | 176k | getCodeCompletionTUInfo(), |
588 | 176k | includeBriefComments()); |
589 | | |
590 | 176k | CXCompletionResult R; |
591 | 176k | R.CursorKind = Results[I].CursorKind; |
592 | 176k | R.CompletionString = StoredCompletion; |
593 | 176k | StoredResults.push_back(R); |
594 | 176k | if (includeFixIts()) |
595 | 12 | AllocatedResults.FixItsVector.emplace_back(std::move(Results[I].FixIts)); |
596 | 176k | } |
597 | | |
598 | 716 | enum CodeCompletionContext::Kind contextKind = Context.getKind(); |
599 | | |
600 | 716 | AllocatedResults.ContextKind = contextKind; |
601 | 716 | AllocatedResults.Contexts = getContextsForContextKind(contextKind, S); |
602 | | |
603 | 716 | AllocatedResults.Selector = ""; |
604 | 716 | ArrayRef<IdentifierInfo *> SelIdents = Context.getSelIdents(); |
605 | 716 | for (ArrayRef<IdentifierInfo *>::iterator I = SelIdents.begin(), |
606 | 716 | E = SelIdents.end(); |
607 | 738 | I != E; ++I22 ) { |
608 | 22 | if (IdentifierInfo *selIdent = *I) |
609 | 22 | AllocatedResults.Selector += selIdent->getName(); |
610 | 22 | AllocatedResults.Selector += ":"; |
611 | 22 | } |
612 | | |
613 | 716 | QualType baseType = Context.getBaseType(); |
614 | 716 | NamedDecl *D = nullptr; |
615 | | |
616 | 716 | if (!baseType.isNull()) { |
617 | | // Get the declaration for a class/struct/union/enum type |
618 | 139 | if (const TagType *Tag = baseType->getAs<TagType>()) |
619 | 37 | D = Tag->getDecl(); |
620 | | // Get the @interface declaration for a (possibly-qualified) Objective-C |
621 | | // object pointer type, e.g., NSString* |
622 | 102 | else if (const ObjCObjectPointerType *ObjPtr = |
623 | 102 | baseType->getAs<ObjCObjectPointerType>()) |
624 | 80 | D = ObjPtr->getInterfaceDecl(); |
625 | | // Get the @interface declaration for an Objective-C object type |
626 | 22 | else if (const ObjCObjectType *Obj = baseType->getAs<ObjCObjectType>()) |
627 | 20 | D = Obj->getInterface(); |
628 | | // Get the class for a C++ injected-class-name |
629 | 2 | else if (const InjectedClassNameType *Injected = |
630 | 2 | baseType->getAs<InjectedClassNameType>()) |
631 | 0 | D = Injected->getDecl(); |
632 | 139 | } |
633 | | |
634 | 716 | if (D != nullptr) { |
635 | 129 | CXCursor cursor = cxcursor::MakeCXCursor(D, *TU); |
636 | | |
637 | 129 | AllocatedResults.ContainerKind = clang_getCursorKind(cursor); |
638 | | |
639 | 129 | CXString CursorUSR = clang_getCursorUSR(cursor); |
640 | 129 | AllocatedResults.ContainerUSR = clang_getCString(CursorUSR); |
641 | 129 | clang_disposeString(CursorUSR); |
642 | | |
643 | 129 | const Type *type = baseType.getTypePtrOrNull(); |
644 | 129 | if (type) { |
645 | 129 | AllocatedResults.ContainerIsIncomplete = type->isIncompleteType(); |
646 | 129 | } |
647 | 0 | else { |
648 | 0 | AllocatedResults.ContainerIsIncomplete = 1; |
649 | 0 | } |
650 | 129 | } |
651 | 587 | else { |
652 | 587 | AllocatedResults.ContainerKind = CXCursor_InvalidCode; |
653 | 587 | AllocatedResults.ContainerUSR.clear(); |
654 | 587 | AllocatedResults.ContainerIsIncomplete = 1; |
655 | 587 | } |
656 | 716 | } |
657 | | |
658 | | void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, |
659 | | OverloadCandidate *Candidates, |
660 | | unsigned NumCandidates, |
661 | | SourceLocation OpenParLoc, |
662 | 101 | bool Braced) override { |
663 | 101 | StoredResults.reserve(StoredResults.size() + NumCandidates); |
664 | 264 | for (unsigned I = 0; I != NumCandidates; ++I163 ) { |
665 | 163 | CodeCompletionString *StoredCompletion = |
666 | 163 | Candidates[I].CreateSignatureString(CurrentArg, S, getAllocator(), |
667 | 163 | getCodeCompletionTUInfo(), |
668 | 163 | includeBriefComments(), Braced); |
669 | | |
670 | 163 | CXCompletionResult R; |
671 | 163 | R.CursorKind = CXCursor_OverloadCandidate; |
672 | 163 | R.CompletionString = StoredCompletion; |
673 | 163 | StoredResults.push_back(R); |
674 | 163 | } |
675 | 101 | } |
676 | | |
677 | 179k | CodeCompletionAllocator &getAllocator() override { |
678 | 179k | return *AllocatedResults.CodeCompletionAllocator; |
679 | 179k | } |
680 | | |
681 | 179k | CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo;} |
682 | | |
683 | | private: |
684 | 718 | void Finish() { |
685 | 718 | AllocatedResults.Results = new CXCompletionResult [StoredResults.size()]; |
686 | 718 | AllocatedResults.NumResults = StoredResults.size(); |
687 | 718 | std::memcpy(AllocatedResults.Results, StoredResults.data(), |
688 | 718 | StoredResults.size() * sizeof(CXCompletionResult)); |
689 | 718 | StoredResults.clear(); |
690 | 718 | } |
691 | | }; |
692 | | } |
693 | | |
694 | | static CXCodeCompleteResults * |
695 | | clang_codeCompleteAt_Impl(CXTranslationUnit TU, const char *complete_filename, |
696 | | unsigned complete_line, unsigned complete_column, |
697 | | ArrayRef<CXUnsavedFile> unsaved_files, |
698 | 721 | unsigned options) { |
699 | 721 | bool IncludeBriefComments = options & CXCodeComplete_IncludeBriefComments; |
700 | 721 | bool SkipPreamble = options & CXCodeComplete_SkipPreamble; |
701 | 721 | bool IncludeFixIts = options & CXCodeComplete_IncludeCompletionsWithFixIts; |
702 | | |
703 | | #ifdef UDP_CODE_COMPLETION_LOGGER |
704 | | #ifdef UDP_CODE_COMPLETION_LOGGER_PORT |
705 | | const llvm::TimeRecord &StartTime = llvm::TimeRecord::getCurrentTime(); |
706 | | #endif |
707 | | #endif |
708 | 721 | bool EnableLogging = getenv("LIBCLANG_CODE_COMPLETION_LOGGING") != nullptr; |
709 | | |
710 | 721 | if (cxtu::isNotUsableTU(TU)) { |
711 | 0 | LOG_BAD_TU(TU); |
712 | 0 | return nullptr; |
713 | 0 | } |
714 | | |
715 | 721 | ASTUnit *AST = cxtu::getASTUnit(TU); |
716 | 721 | if (!AST) |
717 | 0 | return nullptr; |
718 | | |
719 | 721 | CIndexer *CXXIdx = TU->CIdx; |
720 | 721 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing)) |
721 | 0 | setThreadBackgroundPriority(); |
722 | | |
723 | 721 | ASTUnit::ConcurrencyCheck Check(*AST); |
724 | | |
725 | | // Perform the remapping of source files. |
726 | 721 | SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles; |
727 | | |
728 | 721 | for (auto &UF : unsaved_files) { |
729 | 4 | std::unique_ptr<llvm::MemoryBuffer> MB = |
730 | 4 | llvm::MemoryBuffer::getMemBufferCopy(getContents(UF), UF.Filename); |
731 | 4 | RemappedFiles.push_back(std::make_pair(UF.Filename, MB.release())); |
732 | 4 | } |
733 | | |
734 | 721 | if (EnableLogging) { |
735 | | // FIXME: Add logging. |
736 | 0 | } |
737 | | |
738 | | // Parse the resulting source file to find code-completion results. |
739 | 721 | AllocatedCXCodeCompleteResults *Results = new AllocatedCXCodeCompleteResults( |
740 | 721 | &AST->getFileManager()); |
741 | 721 | Results->Results = nullptr; |
742 | 721 | Results->NumResults = 0; |
743 | | |
744 | | // Create a code-completion consumer to capture the results. |
745 | 721 | CodeCompleteOptions Opts; |
746 | 721 | Opts.IncludeBriefComments = IncludeBriefComments; |
747 | 721 | Opts.LoadExternal = !SkipPreamble; |
748 | 721 | Opts.IncludeFixIts = IncludeFixIts; |
749 | 721 | CaptureCompletionResults Capture(Opts, *Results, &TU); |
750 | | |
751 | | // Perform completion. |
752 | 721 | std::vector<const char *> CArgs; |
753 | 721 | for (const auto &Arg : TU->Arguments) |
754 | 4.70k | CArgs.push_back(Arg.c_str()); |
755 | 721 | std::string CompletionInvocation = |
756 | 721 | llvm::formatv("-code-completion-at={0}:{1}:{2}", complete_filename, |
757 | 721 | complete_line, complete_column) |
758 | 721 | .str(); |
759 | 721 | LibclangInvocationReporter InvocationReporter( |
760 | 721 | *CXXIdx, LibclangInvocationReporter::OperationKind::CompletionOperation, |
761 | 721 | TU->ParsingOptions, CArgs, CompletionInvocation, unsaved_files); |
762 | 721 | AST->CodeComplete(complete_filename, complete_line, complete_column, |
763 | 721 | RemappedFiles, (options & CXCodeComplete_IncludeMacros), |
764 | 721 | (options & CXCodeComplete_IncludeCodePatterns), |
765 | 721 | IncludeBriefComments, Capture, |
766 | 721 | CXXIdx->getPCHContainerOperations(), *Results->Diag, |
767 | 721 | Results->LangOpts, *Results->SourceMgr, *Results->FileMgr, |
768 | 721 | Results->Diagnostics, Results->TemporaryBuffers); |
769 | | |
770 | 721 | Results->DiagnosticsWrappers.resize(Results->Diagnostics.size()); |
771 | | |
772 | | // Keep a reference to the allocator used for cached global completions, so |
773 | | // that we can be sure that the memory used by our code completion strings |
774 | | // doesn't get freed due to subsequent reparses (while the code completion |
775 | | // results are still active). |
776 | 721 | Results->CachedCompletionAllocator = AST->getCachedCompletionAllocator(); |
777 | | |
778 | | |
779 | | |
780 | | #ifdef UDP_CODE_COMPLETION_LOGGER |
781 | | #ifdef UDP_CODE_COMPLETION_LOGGER_PORT |
782 | | const llvm::TimeRecord &EndTime = llvm::TimeRecord::getCurrentTime(); |
783 | | SmallString<256> LogResult; |
784 | | llvm::raw_svector_ostream os(LogResult); |
785 | | |
786 | | // Figure out the language and whether or not it uses PCH. |
787 | | const char *lang = 0; |
788 | | bool usesPCH = false; |
789 | | |
790 | | for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); |
791 | | I != E; ++I) { |
792 | | if (*I == 0) |
793 | | continue; |
794 | | if (strcmp(*I, "-x") == 0) { |
795 | | if (I + 1 != E) { |
796 | | lang = *(++I); |
797 | | continue; |
798 | | } |
799 | | } |
800 | | else if (strcmp(*I, "-include") == 0) { |
801 | | if (I+1 != E) { |
802 | | const char *arg = *(++I); |
803 | | SmallString<512> pchName; |
804 | | { |
805 | | llvm::raw_svector_ostream os(pchName); |
806 | | os << arg << ".pth"; |
807 | | } |
808 | | pchName.push_back('\0'); |
809 | | llvm::sys::fs::file_status stat_results; |
810 | | if (!llvm::sys::fs::status(pchName, stat_results)) |
811 | | usesPCH = true; |
812 | | continue; |
813 | | } |
814 | | } |
815 | | } |
816 | | |
817 | | os << "{ "; |
818 | | os << "\"wall\": " << (EndTime.getWallTime() - StartTime.getWallTime()); |
819 | | os << ", \"numRes\": " << Results->NumResults; |
820 | | os << ", \"diags\": " << Results->Diagnostics.size(); |
821 | | os << ", \"pch\": " << (usesPCH ? "true" : "false"); |
822 | | os << ", \"lang\": \"" << (lang ? lang : "<unknown>") << '"'; |
823 | | const char *name = getlogin(); |
824 | | os << ", \"user\": \"" << (name ? name : "unknown") << '"'; |
825 | | os << ", \"clangVer\": \"" << getClangFullVersion() << '"'; |
826 | | os << " }"; |
827 | | |
828 | | StringRef res = os.str(); |
829 | | if (res.size() > 0) { |
830 | | do { |
831 | | // Setup the UDP socket. |
832 | | struct sockaddr_in servaddr; |
833 | | bzero(&servaddr, sizeof(servaddr)); |
834 | | servaddr.sin_family = AF_INET; |
835 | | servaddr.sin_port = htons(UDP_CODE_COMPLETION_LOGGER_PORT); |
836 | | if (inet_pton(AF_INET, UDP_CODE_COMPLETION_LOGGER, |
837 | | &servaddr.sin_addr) <= 0) |
838 | | break; |
839 | | |
840 | | int sockfd = socket(AF_INET, SOCK_DGRAM, 0); |
841 | | if (sockfd < 0) |
842 | | break; |
843 | | |
844 | | sendto(sockfd, res.data(), res.size(), 0, |
845 | | (struct sockaddr *)&servaddr, sizeof(servaddr)); |
846 | | close(sockfd); |
847 | | } |
848 | | while (false); |
849 | | } |
850 | | #endif |
851 | | #endif |
852 | 721 | return Results; |
853 | 721 | } |
854 | | |
855 | | CXCodeCompleteResults *clang_codeCompleteAt(CXTranslationUnit TU, |
856 | | const char *complete_filename, |
857 | | unsigned complete_line, |
858 | | unsigned complete_column, |
859 | | struct CXUnsavedFile *unsaved_files, |
860 | | unsigned num_unsaved_files, |
861 | 721 | unsigned options) { |
862 | 721 | LOG_FUNC_SECTION { |
863 | 0 | *Log << TU << ' ' |
864 | 0 | << complete_filename << ':' << complete_line << ':' << complete_column; |
865 | 0 | } |
866 | | |
867 | 721 | if (num_unsaved_files && !unsaved_files4 ) |
868 | 0 | return nullptr; |
869 | | |
870 | 721 | CXCodeCompleteResults *result; |
871 | 721 | auto CodeCompleteAtImpl = [=, &result]() { |
872 | 721 | result = clang_codeCompleteAt_Impl( |
873 | 721 | TU, complete_filename, complete_line, complete_column, |
874 | 721 | llvm::ArrayRef(unsaved_files, num_unsaved_files), options); |
875 | 721 | }; |
876 | | |
877 | 721 | llvm::CrashRecoveryContext CRC; |
878 | | |
879 | 721 | if (!RunSafely(CRC, CodeCompleteAtImpl)) { |
880 | 3 | fprintf(stderr, "libclang: crash detected in code completion\n"); |
881 | 3 | cxtu::getASTUnit(TU)->setUnsafeToFree(true); |
882 | 3 | return nullptr; |
883 | 718 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) |
884 | 0 | PrintLibclangResourceUsage(TU); |
885 | | |
886 | 718 | return result; |
887 | 721 | } |
888 | | |
889 | 537 | unsigned clang_defaultCodeCompleteOptions(void) { |
890 | 537 | return CXCodeComplete_IncludeMacros; |
891 | 537 | } |
892 | | |
893 | 718 | void clang_disposeCodeCompleteResults(CXCodeCompleteResults *ResultsIn) { |
894 | 718 | if (!ResultsIn) |
895 | 0 | return; |
896 | | |
897 | 718 | AllocatedCXCodeCompleteResults *Results |
898 | 718 | = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); |
899 | 718 | delete Results; |
900 | 718 | } |
901 | | |
902 | | unsigned |
903 | 534 | clang_codeCompleteGetNumDiagnostics(CXCodeCompleteResults *ResultsIn) { |
904 | 534 | AllocatedCXCodeCompleteResults *Results |
905 | 534 | = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); |
906 | 534 | if (!Results) |
907 | 0 | return 0; |
908 | | |
909 | 534 | return Results->Diagnostics.size(); |
910 | 534 | } |
911 | | |
912 | | CXDiagnostic |
913 | | clang_codeCompleteGetDiagnostic(CXCodeCompleteResults *ResultsIn, |
914 | 205 | unsigned Index) { |
915 | 205 | AllocatedCXCodeCompleteResults *Results |
916 | 205 | = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); |
917 | 205 | if (!Results || Index >= Results->Diagnostics.size()) |
918 | 0 | return nullptr; |
919 | | |
920 | 205 | CXStoredDiagnostic *Diag = Results->DiagnosticsWrappers[Index].get(); |
921 | 205 | if (!Diag) |
922 | 205 | Diag = (Results->DiagnosticsWrappers[Index] = |
923 | 205 | std::make_unique<CXStoredDiagnostic>( |
924 | 205 | Results->Diagnostics[Index], Results->LangOpts)) |
925 | 205 | .get(); |
926 | 205 | return Diag; |
927 | 205 | } |
928 | | |
929 | | unsigned long long |
930 | 534 | clang_codeCompleteGetContexts(CXCodeCompleteResults *ResultsIn) { |
931 | 534 | AllocatedCXCodeCompleteResults *Results |
932 | 534 | = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); |
933 | 534 | if (!Results) |
934 | 0 | return 0; |
935 | | |
936 | 534 | return Results->Contexts; |
937 | 534 | } |
938 | | |
939 | | enum CXCursorKind clang_codeCompleteGetContainerKind( |
940 | | CXCodeCompleteResults *ResultsIn, |
941 | 534 | unsigned *IsIncomplete) { |
942 | 534 | AllocatedCXCodeCompleteResults *Results = |
943 | 534 | static_cast<AllocatedCXCodeCompleteResults *>(ResultsIn); |
944 | 534 | if (!Results) |
945 | 0 | return CXCursor_InvalidCode; |
946 | | |
947 | 534 | if (IsIncomplete != nullptr) { |
948 | 534 | *IsIncomplete = Results->ContainerIsIncomplete; |
949 | 534 | } |
950 | | |
951 | 534 | return Results->ContainerKind; |
952 | 534 | } |
953 | | |
954 | 125 | CXString clang_codeCompleteGetContainerUSR(CXCodeCompleteResults *ResultsIn) { |
955 | 125 | AllocatedCXCodeCompleteResults *Results = |
956 | 125 | static_cast<AllocatedCXCodeCompleteResults *>(ResultsIn); |
957 | 125 | if (!Results) |
958 | 0 | return cxstring::createEmpty(); |
959 | | |
960 | 125 | return cxstring::createRef(Results->ContainerUSR.c_str()); |
961 | 125 | } |
962 | | |
963 | | |
964 | 534 | CXString clang_codeCompleteGetObjCSelector(CXCodeCompleteResults *ResultsIn) { |
965 | 534 | AllocatedCXCodeCompleteResults *Results = |
966 | 534 | static_cast<AllocatedCXCodeCompleteResults *>(ResultsIn); |
967 | 534 | if (!Results) |
968 | 0 | return cxstring::createEmpty(); |
969 | | |
970 | 534 | return cxstring::createDup(Results->Selector); |
971 | 534 | } |
972 | | |
973 | | /// Simple utility function that appends a \p New string to the given |
974 | | /// \p Old string, using the \p Buffer for storage. |
975 | | /// |
976 | | /// \param Old The string to which we are appending. This parameter will be |
977 | | /// updated to reflect the complete string. |
978 | | /// |
979 | | /// |
980 | | /// \param New The string to append to \p Old. |
981 | | /// |
982 | | /// \param Buffer A buffer that stores the actual, concatenated string. It will |
983 | | /// be used if the old string is already-non-empty. |
984 | | static void AppendToString(StringRef &Old, StringRef New, |
985 | 5.40M | SmallString<256> &Buffer) { |
986 | 5.40M | if (Old.empty()) { |
987 | 5.40M | Old = New; |
988 | 5.40M | return; |
989 | 5.40M | } |
990 | | |
991 | 2.09k | if (Buffer.empty()) |
992 | 1.94k | Buffer.append(Old.begin(), Old.end()); |
993 | 2.09k | Buffer.append(New.begin(), New.end()); |
994 | 2.09k | Old = Buffer.str(); |
995 | 2.09k | } |
996 | | |
997 | | /// Get the typed-text blocks from the given code-completion string |
998 | | /// and return them as a single string. |
999 | | /// |
1000 | | /// \param String The code-completion string whose typed-text blocks will be |
1001 | | /// concatenated. |
1002 | | /// |
1003 | | /// \param Buffer A buffer used for storage of the completed name. |
1004 | | static StringRef GetTypedName(CodeCompletionString *String, |
1005 | 5.41M | SmallString<256> &Buffer) { |
1006 | 5.41M | StringRef Result; |
1007 | 5.41M | for (CodeCompletionString::iterator C = String->begin(), CEnd = String->end(); |
1008 | 11.6M | C != CEnd; ++C6.22M ) { |
1009 | 6.22M | if (C->Kind == CodeCompletionString::CK_TypedText) |
1010 | 5.40M | AppendToString(Result, C->Text, Buffer); |
1011 | 6.22M | } |
1012 | | |
1013 | 5.41M | return Result; |
1014 | 5.41M | } |
1015 | | |
1016 | | namespace { |
1017 | | struct OrderCompletionResults { |
1018 | | bool operator()(const CXCompletionResult &XR, |
1019 | 2.70M | const CXCompletionResult &YR) const { |
1020 | 2.70M | CodeCompletionString *X |
1021 | 2.70M | = (CodeCompletionString *)XR.CompletionString; |
1022 | 2.70M | CodeCompletionString *Y |
1023 | 2.70M | = (CodeCompletionString *)YR.CompletionString; |
1024 | | |
1025 | 2.70M | SmallString<256> XBuffer; |
1026 | 2.70M | StringRef XText = GetTypedName(X, XBuffer); |
1027 | 2.70M | SmallString<256> YBuffer; |
1028 | 2.70M | StringRef YText = GetTypedName(Y, YBuffer); |
1029 | | |
1030 | 2.70M | if (XText.empty() || YText.empty()2.70M ) |
1031 | 5.81k | return !XText.empty(); |
1032 | | |
1033 | 2.70M | int result = XText.compare_insensitive(YText); |
1034 | 2.70M | if (result < 0) |
1035 | 2.41M | return true; |
1036 | 284k | if (result > 0) |
1037 | 279k | return false; |
1038 | | |
1039 | 5.70k | result = XText.compare(YText); |
1040 | 5.70k | return result < 0; |
1041 | 284k | } |
1042 | | }; |
1043 | | } |
1044 | | |
1045 | | void clang_sortCodeCompletionResults(CXCompletionResult *Results, |
1046 | 534 | unsigned NumResults) { |
1047 | 534 | std::stable_sort(Results, Results + NumResults, OrderCompletionResults()); |
1048 | 534 | } |