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