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