1 //===--- CodeCompleteConsumer.cpp - Code Completion Interface ---*- C++ -*-===//
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 CodeCompleteConsumer class.
11 //
12 //===----------------------------------------------------------------------===//
13 #include "clang/Sema/CodeCompleteConsumer.h"
14 #include "clang-c/Index.h"
15 #include "clang/AST/DeclCXX.h"
16 #include "clang/AST/DeclObjC.h"
17 #include "clang/AST/DeclTemplate.h"
18 #include "clang/Sema/Scope.h"
19 #include "clang/Sema/Sema.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include <algorithm>
25 #include <cstring>
26 #include <functional>
27 
28 using namespace clang;
29 
30 //===----------------------------------------------------------------------===//
31 // Code completion context implementation
32 //===----------------------------------------------------------------------===//
33 
wantConstructorResults() const34 bool CodeCompletionContext::wantConstructorResults() const {
35   switch (Kind) {
36   case CCC_Recovery:
37   case CCC_Statement:
38   case CCC_Expression:
39   case CCC_ObjCMessageReceiver:
40   case CCC_ParenthesizedExpression:
41     return true;
42 
43   case CCC_TopLevel:
44   case CCC_ObjCInterface:
45   case CCC_ObjCImplementation:
46   case CCC_ObjCIvarList:
47   case CCC_ClassStructUnion:
48   case CCC_DotMemberAccess:
49   case CCC_ArrowMemberAccess:
50   case CCC_ObjCPropertyAccess:
51   case CCC_EnumTag:
52   case CCC_UnionTag:
53   case CCC_ClassOrStructTag:
54   case CCC_ObjCProtocolName:
55   case CCC_Namespace:
56   case CCC_Type:
57   case CCC_Name:
58   case CCC_PotentiallyQualifiedName:
59   case CCC_MacroName:
60   case CCC_MacroNameUse:
61   case CCC_PreprocessorExpression:
62   case CCC_PreprocessorDirective:
63   case CCC_NaturalLanguage:
64   case CCC_SelectorName:
65   case CCC_TypeQualifiers:
66   case CCC_Other:
67   case CCC_OtherWithMacros:
68   case CCC_ObjCInstanceMessage:
69   case CCC_ObjCClassMessage:
70   case CCC_ObjCInterfaceName:
71   case CCC_ObjCCategoryName:
72     return false;
73   }
74 
75   llvm_unreachable("Invalid CodeCompletionContext::Kind!");
76 }
77 
78 //===----------------------------------------------------------------------===//
79 // Code completion string implementation
80 //===----------------------------------------------------------------------===//
Chunk(ChunkKind Kind,const char * Text)81 CodeCompletionString::Chunk::Chunk(ChunkKind Kind, const char *Text)
82   : Kind(Kind), Text("")
83 {
84   switch (Kind) {
85   case CK_TypedText:
86   case CK_Text:
87   case CK_Placeholder:
88   case CK_Informative:
89   case CK_ResultType:
90   case CK_CurrentParameter:
91     this->Text = Text;
92     break;
93 
94   case CK_Optional:
95     llvm_unreachable("Optional strings cannot be created from text");
96 
97   case CK_LeftParen:
98     this->Text = "(";
99     break;
100 
101   case CK_RightParen:
102     this->Text = ")";
103     break;
104 
105   case CK_LeftBracket:
106     this->Text = "[";
107     break;
108 
109   case CK_RightBracket:
110     this->Text = "]";
111     break;
112 
113   case CK_LeftBrace:
114     this->Text = "{";
115     break;
116 
117   case CK_RightBrace:
118     this->Text = "}";
119     break;
120 
121   case CK_LeftAngle:
122     this->Text = "<";
123     break;
124 
125   case CK_RightAngle:
126     this->Text = ">";
127     break;
128 
129   case CK_Comma:
130     this->Text = ", ";
131     break;
132 
133   case CK_Colon:
134     this->Text = ":";
135     break;
136 
137   case CK_SemiColon:
138     this->Text = ";";
139     break;
140 
141   case CK_Equal:
142     this->Text = " = ";
143     break;
144 
145   case CK_HorizontalSpace:
146     this->Text = " ";
147     break;
148 
149   case CK_VerticalSpace:
150     this->Text = "\n";
151     break;
152   }
153 }
154 
155 CodeCompletionString::Chunk
CreateText(const char * Text)156 CodeCompletionString::Chunk::CreateText(const char *Text) {
157   return Chunk(CK_Text, Text);
158 }
159 
160 CodeCompletionString::Chunk
CreateOptional(CodeCompletionString * Optional)161 CodeCompletionString::Chunk::CreateOptional(CodeCompletionString *Optional) {
162   Chunk Result;
163   Result.Kind = CK_Optional;
164   Result.Optional = Optional;
165   return Result;
166 }
167 
168 CodeCompletionString::Chunk
CreatePlaceholder(const char * Placeholder)169 CodeCompletionString::Chunk::CreatePlaceholder(const char *Placeholder) {
170   return Chunk(CK_Placeholder, Placeholder);
171 }
172 
173 CodeCompletionString::Chunk
CreateInformative(const char * Informative)174 CodeCompletionString::Chunk::CreateInformative(const char *Informative) {
175   return Chunk(CK_Informative, Informative);
176 }
177 
178 CodeCompletionString::Chunk
CreateResultType(const char * ResultType)179 CodeCompletionString::Chunk::CreateResultType(const char *ResultType) {
180   return Chunk(CK_ResultType, ResultType);
181 }
182 
183 CodeCompletionString::Chunk
CreateCurrentParameter(const char * CurrentParameter)184 CodeCompletionString::Chunk::CreateCurrentParameter(
185                                                 const char *CurrentParameter) {
186   return Chunk(CK_CurrentParameter, CurrentParameter);
187 }
188 
CodeCompletionString(const Chunk * Chunks,unsigned NumChunks,unsigned Priority,CXAvailabilityKind Availability,const char ** Annotations,unsigned NumAnnotations,StringRef ParentName,const char * BriefComment)189 CodeCompletionString::CodeCompletionString(const Chunk *Chunks,
190                                            unsigned NumChunks,
191                                            unsigned Priority,
192                                            CXAvailabilityKind Availability,
193                                            const char **Annotations,
194                                            unsigned NumAnnotations,
195                                            StringRef ParentName,
196                                            const char *BriefComment)
197   : NumChunks(NumChunks), NumAnnotations(NumAnnotations),
198     Priority(Priority), Availability(Availability),
199     ParentName(ParentName), BriefComment(BriefComment)
200 {
201   assert(NumChunks <= 0xffff);
202   assert(NumAnnotations <= 0xffff);
203 
204   Chunk *StoredChunks = reinterpret_cast<Chunk *>(this + 1);
205   for (unsigned I = 0; I != NumChunks; ++I)
206     StoredChunks[I] = Chunks[I];
207 
208   const char **StoredAnnotations = reinterpret_cast<const char **>(StoredChunks + NumChunks);
209   for (unsigned I = 0; I != NumAnnotations; ++I)
210     StoredAnnotations[I] = Annotations[I];
211 }
212 
getAnnotationCount() const213 unsigned CodeCompletionString::getAnnotationCount() const {
214   return NumAnnotations;
215 }
216 
getAnnotation(unsigned AnnotationNr) const217 const char *CodeCompletionString::getAnnotation(unsigned AnnotationNr) const {
218   if (AnnotationNr < NumAnnotations)
219     return reinterpret_cast<const char * const*>(end())[AnnotationNr];
220   else
221     return nullptr;
222 }
223 
224 
getAsString() const225 std::string CodeCompletionString::getAsString() const {
226   std::string Result;
227   llvm::raw_string_ostream OS(Result);
228 
229   for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) {
230     switch (C->Kind) {
231     case CK_Optional: OS << "{#" << C->Optional->getAsString() << "#}"; break;
232     case CK_Placeholder: OS << "<#" << C->Text << "#>"; break;
233 
234     case CK_Informative:
235     case CK_ResultType:
236       OS << "[#" << C->Text << "#]";
237       break;
238 
239     case CK_CurrentParameter: OS << "<#" << C->Text << "#>"; break;
240     default: OS << C->Text; break;
241     }
242   }
243   return OS.str();
244 }
245 
getTypedText() const246 const char *CodeCompletionString::getTypedText() const {
247   for (iterator C = begin(), CEnd = end(); C != CEnd; ++C)
248     if (C->Kind == CK_TypedText)
249       return C->Text;
250 
251   return nullptr;
252 }
253 
CopyString(const Twine & String)254 const char *CodeCompletionAllocator::CopyString(const Twine &String) {
255   SmallString<128> Data;
256   StringRef Ref = String.toStringRef(Data);
257   // FIXME: It would be more efficient to teach Twine to tell us its size and
258   // then add a routine there to fill in an allocated char* with the contents
259   // of the string.
260   char *Mem = (char *)Allocate(Ref.size() + 1, 1);
261   std::copy(Ref.begin(), Ref.end(), Mem);
262   Mem[Ref.size()] = 0;
263   return Mem;
264 }
265 
getParentName(const DeclContext * DC)266 StringRef CodeCompletionTUInfo::getParentName(const DeclContext *DC) {
267   const NamedDecl *ND = dyn_cast<NamedDecl>(DC);
268   if (!ND)
269     return StringRef();
270 
271   // Check whether we've already cached the parent name.
272   StringRef &CachedParentName = ParentNames[DC];
273   if (!CachedParentName.empty())
274     return CachedParentName;
275 
276   // If we already processed this DeclContext and assigned empty to it, the
277   // data pointer will be non-null.
278   if (CachedParentName.data() != nullptr)
279     return StringRef();
280 
281   // Find the interesting names.
282   SmallVector<const DeclContext *, 2> Contexts;
283   while (DC && !DC->isFunctionOrMethod()) {
284     if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC)) {
285       if (ND->getIdentifier())
286         Contexts.push_back(DC);
287     }
288 
289     DC = DC->getParent();
290   }
291 
292   {
293     SmallString<128> S;
294     llvm::raw_svector_ostream OS(S);
295     bool First = true;
296     for (unsigned I = Contexts.size(); I != 0; --I) {
297       if (First)
298         First = false;
299       else {
300         OS << "::";
301       }
302 
303       const DeclContext *CurDC = Contexts[I-1];
304       if (const ObjCCategoryImplDecl *CatImpl = dyn_cast<ObjCCategoryImplDecl>(CurDC))
305         CurDC = CatImpl->getCategoryDecl();
306 
307       if (const ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CurDC)) {
308         const ObjCInterfaceDecl *Interface = Cat->getClassInterface();
309         if (!Interface) {
310           // Assign an empty StringRef but with non-null data to distinguish
311           // between empty because we didn't process the DeclContext yet.
312           CachedParentName = StringRef((const char *)~0U, 0);
313           return StringRef();
314         }
315 
316         OS << Interface->getName() << '(' << Cat->getName() << ')';
317       } else {
318         OS << cast<NamedDecl>(CurDC)->getName();
319       }
320     }
321 
322     CachedParentName = AllocatorRef->CopyString(OS.str());
323   }
324 
325   return CachedParentName;
326 }
327 
TakeString()328 CodeCompletionString *CodeCompletionBuilder::TakeString() {
329   void *Mem = getAllocator().Allocate(
330                   sizeof(CodeCompletionString) + sizeof(Chunk) * Chunks.size()
331                                     + sizeof(const char *) * Annotations.size(),
332                                  llvm::alignOf<CodeCompletionString>());
333   CodeCompletionString *Result
334     = new (Mem) CodeCompletionString(Chunks.data(), Chunks.size(),
335                                      Priority, Availability,
336                                      Annotations.data(), Annotations.size(),
337                                      ParentName, BriefComment);
338   Chunks.clear();
339   return Result;
340 }
341 
AddTypedTextChunk(const char * Text)342 void CodeCompletionBuilder::AddTypedTextChunk(const char *Text) {
343   Chunks.push_back(Chunk(CodeCompletionString::CK_TypedText, Text));
344 }
345 
AddTextChunk(const char * Text)346 void CodeCompletionBuilder::AddTextChunk(const char *Text) {
347   Chunks.push_back(Chunk::CreateText(Text));
348 }
349 
AddOptionalChunk(CodeCompletionString * Optional)350 void CodeCompletionBuilder::AddOptionalChunk(CodeCompletionString *Optional) {
351   Chunks.push_back(Chunk::CreateOptional(Optional));
352 }
353 
AddPlaceholderChunk(const char * Placeholder)354 void CodeCompletionBuilder::AddPlaceholderChunk(const char *Placeholder) {
355   Chunks.push_back(Chunk::CreatePlaceholder(Placeholder));
356 }
357 
AddInformativeChunk(const char * Text)358 void CodeCompletionBuilder::AddInformativeChunk(const char *Text) {
359   Chunks.push_back(Chunk::CreateInformative(Text));
360 }
361 
AddResultTypeChunk(const char * ResultType)362 void CodeCompletionBuilder::AddResultTypeChunk(const char *ResultType) {
363   Chunks.push_back(Chunk::CreateResultType(ResultType));
364 }
365 
366 void
AddCurrentParameterChunk(const char * CurrentParameter)367 CodeCompletionBuilder::AddCurrentParameterChunk(const char *CurrentParameter) {
368   Chunks.push_back(Chunk::CreateCurrentParameter(CurrentParameter));
369 }
370 
AddChunk(CodeCompletionString::ChunkKind CK,const char * Text)371 void CodeCompletionBuilder::AddChunk(CodeCompletionString::ChunkKind CK,
372                                      const char *Text) {
373   Chunks.push_back(Chunk(CK, Text));
374 }
375 
addParentContext(const DeclContext * DC)376 void CodeCompletionBuilder::addParentContext(const DeclContext *DC) {
377   if (DC->isTranslationUnit()) {
378     return;
379   }
380 
381   if (DC->isFunctionOrMethod())
382     return;
383 
384   const NamedDecl *ND = dyn_cast<NamedDecl>(DC);
385   if (!ND)
386     return;
387 
388   ParentName = getCodeCompletionTUInfo().getParentName(DC);
389 }
390 
addBriefComment(StringRef Comment)391 void CodeCompletionBuilder::addBriefComment(StringRef Comment) {
392   BriefComment = Allocator.CopyString(Comment);
393 }
394 
395 //===----------------------------------------------------------------------===//
396 // Code completion overload candidate implementation
397 //===----------------------------------------------------------------------===//
398 FunctionDecl *
getFunction() const399 CodeCompleteConsumer::OverloadCandidate::getFunction() const {
400   if (getKind() == CK_Function)
401     return Function;
402   else if (getKind() == CK_FunctionTemplate)
403     return FunctionTemplate->getTemplatedDecl();
404   else
405     return nullptr;
406 }
407 
408 const FunctionType *
getFunctionType() const409 CodeCompleteConsumer::OverloadCandidate::getFunctionType() const {
410   switch (Kind) {
411   case CK_Function:
412     return Function->getType()->getAs<FunctionType>();
413 
414   case CK_FunctionTemplate:
415     return FunctionTemplate->getTemplatedDecl()->getType()
416              ->getAs<FunctionType>();
417 
418   case CK_FunctionType:
419     return Type;
420   }
421 
422   llvm_unreachable("Invalid CandidateKind!");
423 }
424 
425 //===----------------------------------------------------------------------===//
426 // Code completion consumer implementation
427 //===----------------------------------------------------------------------===//
428 
~CodeCompleteConsumer()429 CodeCompleteConsumer::~CodeCompleteConsumer() { }
430 
431 void
ProcessCodeCompleteResults(Sema & SemaRef,CodeCompletionContext Context,CodeCompletionResult * Results,unsigned NumResults)432 PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &SemaRef,
433                                                  CodeCompletionContext Context,
434                                                  CodeCompletionResult *Results,
435                                                          unsigned NumResults) {
436   std::stable_sort(Results, Results + NumResults);
437 
438   // Print the results.
439   for (unsigned I = 0; I != NumResults; ++I) {
440     OS << "COMPLETION: ";
441     switch (Results[I].Kind) {
442     case CodeCompletionResult::RK_Declaration:
443       OS << *Results[I].Declaration;
444       if (Results[I].Hidden)
445         OS << " (Hidden)";
446       if (CodeCompletionString *CCS
447             = Results[I].CreateCodeCompletionString(SemaRef, getAllocator(),
448                                                     CCTUInfo,
449                                                     includeBriefComments())) {
450         OS << " : " << CCS->getAsString();
451         if (const char *BriefComment = CCS->getBriefComment())
452           OS << " : " << BriefComment;
453       }
454 
455       OS << '\n';
456       break;
457 
458     case CodeCompletionResult::RK_Keyword:
459       OS << Results[I].Keyword << '\n';
460       break;
461 
462     case CodeCompletionResult::RK_Macro: {
463       OS << Results[I].Macro->getName();
464       if (CodeCompletionString *CCS
465             = Results[I].CreateCodeCompletionString(SemaRef, getAllocator(),
466                                                     CCTUInfo,
467                                                     includeBriefComments())) {
468         OS << " : " << CCS->getAsString();
469       }
470       OS << '\n';
471       break;
472     }
473 
474     case CodeCompletionResult::RK_Pattern: {
475       OS << "Pattern : "
476          << Results[I].Pattern->getAsString() << '\n';
477       break;
478     }
479     }
480   }
481 }
482 
483 // This function is used solely to preserve the former presentation of overloads
484 // by "clang -cc1 -code-completion-at", since CodeCompletionString::getAsString
485 // needs to be improved for printing the newer and more detailed overload
486 // chunks.
getOverloadAsString(const CodeCompletionString & CCS)487 static std::string getOverloadAsString(const CodeCompletionString &CCS) {
488   std::string Result;
489   llvm::raw_string_ostream OS(Result);
490 
491   for (auto &C : CCS) {
492     switch (C.Kind) {
493     case CodeCompletionString::CK_Informative:
494     case CodeCompletionString::CK_ResultType:
495       OS << "[#" << C.Text << "#]";
496       break;
497 
498     case CodeCompletionString::CK_CurrentParameter:
499       OS << "<#" << C.Text << "#>";
500       break;
501 
502     default: OS << C.Text; break;
503     }
504   }
505   return OS.str();
506 }
507 
508 void
ProcessOverloadCandidates(Sema & SemaRef,unsigned CurrentArg,OverloadCandidate * Candidates,unsigned NumCandidates)509 PrintingCodeCompleteConsumer::ProcessOverloadCandidates(Sema &SemaRef,
510                                                         unsigned CurrentArg,
511                                               OverloadCandidate *Candidates,
512                                                      unsigned NumCandidates) {
513   for (unsigned I = 0; I != NumCandidates; ++I) {
514     if (CodeCompletionString *CCS
515           = Candidates[I].CreateSignatureString(CurrentArg, SemaRef,
516                                                 getAllocator(), CCTUInfo,
517                                                 includeBriefComments())) {
518       OS << "OVERLOAD: " << getOverloadAsString(*CCS) << "\n";
519     }
520   }
521 }
522 
523 /// \brief Retrieve the effective availability of the given declaration.
getDeclAvailability(const Decl * D)524 static AvailabilityResult getDeclAvailability(const Decl *D) {
525   AvailabilityResult AR = D->getAvailability();
526   if (isa<EnumConstantDecl>(D))
527     AR = std::max(AR, cast<Decl>(D->getDeclContext())->getAvailability());
528   return AR;
529 }
530 
computeCursorKindAndAvailability(bool Accessible)531 void CodeCompletionResult::computeCursorKindAndAvailability(bool Accessible) {
532   switch (Kind) {
533   case RK_Pattern:
534     if (!Declaration) {
535       // Do nothing: Patterns can come with cursor kinds!
536       break;
537     }
538     // Fall through
539 
540   case RK_Declaration: {
541     // Set the availability based on attributes.
542     switch (getDeclAvailability(Declaration)) {
543     case AR_Available:
544     case AR_NotYetIntroduced:
545       Availability = CXAvailability_Available;
546       break;
547 
548     case AR_Deprecated:
549       Availability = CXAvailability_Deprecated;
550       break;
551 
552     case AR_Unavailable:
553       Availability = CXAvailability_NotAvailable;
554       break;
555     }
556 
557     if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(Declaration))
558       if (Function->isDeleted())
559         Availability = CXAvailability_NotAvailable;
560 
561     CursorKind = getCursorKindForDecl(Declaration);
562     if (CursorKind == CXCursor_UnexposedDecl) {
563       // FIXME: Forward declarations of Objective-C classes and protocols
564       // are not directly exposed, but we want code completion to treat them
565       // like a definition.
566       if (isa<ObjCInterfaceDecl>(Declaration))
567         CursorKind = CXCursor_ObjCInterfaceDecl;
568       else if (isa<ObjCProtocolDecl>(Declaration))
569         CursorKind = CXCursor_ObjCProtocolDecl;
570       else
571         CursorKind = CXCursor_NotImplemented;
572     }
573     break;
574   }
575 
576   case RK_Macro:
577   case RK_Keyword:
578     llvm_unreachable("Macro and keyword kinds are handled by the constructors");
579   }
580 
581   if (!Accessible)
582     Availability = CXAvailability_NotAccessible;
583 }
584 
585 /// \brief Retrieve the name that should be used to order a result.
586 ///
587 /// If the name needs to be constructed as a string, that string will be
588 /// saved into Saved and the returned StringRef will refer to it.
getOrderedName(const CodeCompletionResult & R,std::string & Saved)589 static StringRef getOrderedName(const CodeCompletionResult &R,
590                                     std::string &Saved) {
591   switch (R.Kind) {
592     case CodeCompletionResult::RK_Keyword:
593       return R.Keyword;
594 
595     case CodeCompletionResult::RK_Pattern:
596       return R.Pattern->getTypedText();
597 
598     case CodeCompletionResult::RK_Macro:
599       return R.Macro->getName();
600 
601     case CodeCompletionResult::RK_Declaration:
602       // Handle declarations below.
603       break;
604   }
605 
606   DeclarationName Name = R.Declaration->getDeclName();
607 
608   // If the name is a simple identifier (by far the common case), or a
609   // zero-argument selector, just return a reference to that identifier.
610   if (IdentifierInfo *Id = Name.getAsIdentifierInfo())
611     return Id->getName();
612   if (Name.isObjCZeroArgSelector())
613     if (IdentifierInfo *Id
614         = Name.getObjCSelector().getIdentifierInfoForSlot(0))
615       return Id->getName();
616 
617   Saved = Name.getAsString();
618   return Saved;
619 }
620 
operator <(const CodeCompletionResult & X,const CodeCompletionResult & Y)621 bool clang::operator<(const CodeCompletionResult &X,
622                       const CodeCompletionResult &Y) {
623   std::string XSaved, YSaved;
624   StringRef XStr = getOrderedName(X, XSaved);
625   StringRef YStr = getOrderedName(Y, YSaved);
626   int cmp = XStr.compare_lower(YStr);
627   if (cmp)
628     return cmp < 0;
629 
630   // If case-insensitive comparison fails, try case-sensitive comparison.
631   cmp = XStr.compare(YStr);
632   if (cmp)
633     return cmp < 0;
634 
635   return false;
636 }
637