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