clang  9.0.0svn
CodeCompleteConsumer.cpp
Go to the documentation of this file.
1 //===- CodeCompleteConsumer.cpp - Code Completion Interface ---------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the CodeCompleteConsumer class.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #include "clang-c/Index.h"
15 #include "clang/AST/Decl.h"
16 #include "clang/AST/DeclBase.h"
17 #include "clang/AST/DeclObjC.h"
18 #include "clang/AST/DeclTemplate.h"
20 #include "clang/AST/Type.h"
22 #include "clang/Lex/Preprocessor.h"
23 #include "clang/Sema/Sema.h"
24 #include "llvm/ADT/SmallString.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/ADT/StringRef.h"
27 #include "llvm/ADT/Twine.h"
28 #include "llvm/Support/Casting.h"
29 #include "llvm/Support/Compiler.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/FormatVariadic.h"
32 #include "llvm/Support/raw_ostream.h"
33 #include <algorithm>
34 #include <cassert>
35 #include <cstdint>
36 #include <string>
37 
38 using namespace clang;
39 
40 //===----------------------------------------------------------------------===//
41 // Code completion context implementation
42 //===----------------------------------------------------------------------===//
43 
45  switch (CCKind) {
46  case CCC_Recovery:
47  case CCC_Statement:
48  case CCC_Expression:
51  case CCC_Symbol:
53  return true;
54 
55  case CCC_TopLevel:
56  case CCC_ObjCInterface:
58  case CCC_ObjCIvarList:
63  case CCC_EnumTag:
64  case CCC_UnionTag:
67  case CCC_Namespace:
68  case CCC_Type:
69  case CCC_NewName:
70  case CCC_MacroName:
71  case CCC_MacroNameUse:
75  case CCC_SelectorName:
76  case CCC_TypeQualifiers:
77  case CCC_Other:
83  case CCC_IncludedFile:
84  return false;
85  }
86 
87  llvm_unreachable("Invalid CodeCompletionContext::Kind!");
88 }
89 
91  using CCKind = CodeCompletionContext::Kind;
92  switch (Kind) {
93  case CCKind::CCC_Other:
94  return "Other";
95  case CCKind::CCC_OtherWithMacros:
96  return "OtherWithMacros";
97  case CCKind::CCC_TopLevel:
98  return "TopLevel";
99  case CCKind::CCC_ObjCInterface:
100  return "ObjCInterface";
101  case CCKind::CCC_ObjCImplementation:
102  return "ObjCImplementation";
103  case CCKind::CCC_ObjCIvarList:
104  return "ObjCIvarList";
105  case CCKind::CCC_ClassStructUnion:
106  return "ClassStructUnion";
107  case CCKind::CCC_Statement:
108  return "Statement";
109  case CCKind::CCC_Expression:
110  return "Expression";
111  case CCKind::CCC_ObjCMessageReceiver:
112  return "ObjCMessageReceiver";
113  case CCKind::CCC_DotMemberAccess:
114  return "DotMemberAccess";
115  case CCKind::CCC_ArrowMemberAccess:
116  return "ArrowMemberAccess";
117  case CCKind::CCC_ObjCPropertyAccess:
118  return "ObjCPropertyAccess";
119  case CCKind::CCC_EnumTag:
120  return "EnumTag";
121  case CCKind::CCC_UnionTag:
122  return "UnionTag";
123  case CCKind::CCC_ClassOrStructTag:
124  return "ClassOrStructTag";
125  case CCKind::CCC_ObjCProtocolName:
126  return "ObjCProtocolName";
127  case CCKind::CCC_Namespace:
128  return "Namespace";
129  case CCKind::CCC_Type:
130  return "Type";
131  case CCKind::CCC_NewName:
132  return "NewName";
133  case CCKind::CCC_Symbol:
134  return "Symbol";
135  case CCKind::CCC_SymbolOrNewName:
136  return "SymbolOrNewName";
137  case CCKind::CCC_MacroName:
138  return "MacroName";
139  case CCKind::CCC_MacroNameUse:
140  return "MacroNameUse";
141  case CCKind::CCC_PreprocessorExpression:
142  return "PreprocessorExpression";
143  case CCKind::CCC_PreprocessorDirective:
144  return "PreprocessorDirective";
145  case CCKind::CCC_NaturalLanguage:
146  return "NaturalLanguage";
147  case CCKind::CCC_SelectorName:
148  return "SelectorName";
149  case CCKind::CCC_TypeQualifiers:
150  return "TypeQualifiers";
151  case CCKind::CCC_ParenthesizedExpression:
152  return "ParenthesizedExpression";
153  case CCKind::CCC_ObjCInstanceMessage:
154  return "ObjCInstanceMessage";
155  case CCKind::CCC_ObjCClassMessage:
156  return "ObjCClassMessage";
157  case CCKind::CCC_ObjCInterfaceName:
158  return "ObjCInterfaceName";
159  case CCKind::CCC_ObjCCategoryName:
160  return "ObjCCategoryName";
161  case CCKind::CCC_IncludedFile:
162  return "IncludedFile";
163  case CCKind::CCC_Recovery:
164  return "Recovery";
165  }
166  llvm_unreachable("Invalid CodeCompletionContext::Kind!");
167 }
168 
169 //===----------------------------------------------------------------------===//
170 // Code completion string implementation
171 //===----------------------------------------------------------------------===//
172 
174  : Kind(Kind), Text("") {
175  switch (Kind) {
176  case CK_TypedText:
177  case CK_Text:
178  case CK_Placeholder:
179  case CK_Informative:
180  case CK_ResultType:
181  case CK_CurrentParameter:
182  this->Text = Text;
183  break;
184 
185  case CK_Optional:
186  llvm_unreachable("Optional strings cannot be created from text");
187 
188  case CK_LeftParen:
189  this->Text = "(";
190  break;
191 
192  case CK_RightParen:
193  this->Text = ")";
194  break;
195 
196  case CK_LeftBracket:
197  this->Text = "[";
198  break;
199 
200  case CK_RightBracket:
201  this->Text = "]";
202  break;
203 
204  case CK_LeftBrace:
205  this->Text = "{";
206  break;
207 
208  case CK_RightBrace:
209  this->Text = "}";
210  break;
211 
212  case CK_LeftAngle:
213  this->Text = "<";
214  break;
215 
216  case CK_RightAngle:
217  this->Text = ">";
218  break;
219 
220  case CK_Comma:
221  this->Text = ", ";
222  break;
223 
224  case CK_Colon:
225  this->Text = ":";
226  break;
227 
228  case CK_SemiColon:
229  this->Text = ";";
230  break;
231 
232  case CK_Equal:
233  this->Text = " = ";
234  break;
235 
236  case CK_HorizontalSpace:
237  this->Text = " ";
238  break;
239 
240  case CK_VerticalSpace:
241  this->Text = "\n";
242  break;
243  }
244 }
245 
248  return Chunk(CK_Text, Text);
249 }
250 
253  Chunk Result;
254  Result.Kind = CK_Optional;
255  Result.Optional = Optional;
256  return Result;
257 }
258 
261  return Chunk(CK_Placeholder, Placeholder);
262 }
263 
266  return Chunk(CK_Informative, Informative);
267 }
268 
271  return Chunk(CK_ResultType, ResultType);
272 }
273 
275  const char *CurrentParameter) {
276  return Chunk(CK_CurrentParameter, CurrentParameter);
277 }
278 
279 CodeCompletionString::CodeCompletionString(
280  const Chunk *Chunks, unsigned NumChunks, unsigned Priority,
281  CXAvailabilityKind Availability, const char **Annotations,
282  unsigned NumAnnotations, StringRef ParentName, const char *BriefComment)
283  : NumChunks(NumChunks), NumAnnotations(NumAnnotations), Priority(Priority),
284  Availability(Availability), ParentName(ParentName),
285  BriefComment(BriefComment) {
286  assert(NumChunks <= 0xffff);
287  assert(NumAnnotations <= 0xffff);
288 
289  Chunk *StoredChunks = reinterpret_cast<Chunk *>(this + 1);
290  for (unsigned I = 0; I != NumChunks; ++I)
291  StoredChunks[I] = Chunks[I];
292 
293  const char **StoredAnnotations =
294  reinterpret_cast<const char **>(StoredChunks + NumChunks);
295  for (unsigned I = 0; I != NumAnnotations; ++I)
296  StoredAnnotations[I] = Annotations[I];
297 }
298 
300  return NumAnnotations;
301 }
302 
303 const char *CodeCompletionString::getAnnotation(unsigned AnnotationNr) const {
304  if (AnnotationNr < NumAnnotations)
305  return reinterpret_cast<const char *const *>(end())[AnnotationNr];
306  else
307  return nullptr;
308 }
309 
311  std::string Result;
312  llvm::raw_string_ostream OS(Result);
313 
314  for (const Chunk &C : *this) {
315  switch (C.Kind) {
316  case CK_Optional:
317  OS << "{#" << C.Optional->getAsString() << "#}";
318  break;
319  case CK_Placeholder:
320  OS << "<#" << C.Text << "#>";
321  break;
322  case CK_Informative:
323  case CK_ResultType:
324  OS << "[#" << C.Text << "#]";
325  break;
326  case CK_CurrentParameter:
327  OS << "<#" << C.Text << "#>";
328  break;
329  default:
330  OS << C.Text;
331  break;
332  }
333  }
334  return OS.str();
335 }
336 
338  for (const Chunk &C : *this)
339  if (C.Kind == CK_TypedText)
340  return C.Text;
341 
342  return nullptr;
343 }
344 
345 const char *CodeCompletionAllocator::CopyString(const Twine &String) {
346  SmallString<128> Data;
347  StringRef Ref = String.toStringRef(Data);
348  // FIXME: It would be more efficient to teach Twine to tell us its size and
349  // then add a routine there to fill in an allocated char* with the contents
350  // of the string.
351  char *Mem = (char *)Allocate(Ref.size() + 1, 1);
352  std::copy(Ref.begin(), Ref.end(), Mem);
353  Mem[Ref.size()] = 0;
354  return Mem;
355 }
356 
358  const NamedDecl *ND = dyn_cast<NamedDecl>(DC);
359  if (!ND)
360  return {};
361 
362  // Check whether we've already cached the parent name.
363  StringRef &CachedParentName = ParentNames[DC];
364  if (!CachedParentName.empty())
365  return CachedParentName;
366 
367  // If we already processed this DeclContext and assigned empty to it, the
368  // data pointer will be non-null.
369  if (CachedParentName.data() != nullptr)
370  return {};
371 
372  // Find the interesting names.
374  while (DC && !DC->isFunctionOrMethod()) {
375  if (const auto *ND = dyn_cast<NamedDecl>(DC)) {
376  if (ND->getIdentifier())
377  Contexts.push_back(DC);
378  }
379 
380  DC = DC->getParent();
381  }
382 
383  {
385  llvm::raw_svector_ostream OS(S);
386  bool First = true;
387  for (unsigned I = Contexts.size(); I != 0; --I) {
388  if (First)
389  First = false;
390  else {
391  OS << "::";
392  }
393 
394  const DeclContext *CurDC = Contexts[I - 1];
395  if (const auto *CatImpl = dyn_cast<ObjCCategoryImplDecl>(CurDC))
396  CurDC = CatImpl->getCategoryDecl();
397 
398  if (const auto *Cat = dyn_cast<ObjCCategoryDecl>(CurDC)) {
399  const ObjCInterfaceDecl *Interface = Cat->getClassInterface();
400  if (!Interface) {
401  // Assign an empty StringRef but with non-null data to distinguish
402  // between empty because we didn't process the DeclContext yet.
403  CachedParentName = StringRef((const char *)(uintptr_t)~0U, 0);
404  return {};
405  }
406 
407  OS << Interface->getName() << '(' << Cat->getName() << ')';
408  } else {
409  OS << cast<NamedDecl>(CurDC)->getName();
410  }
411  }
412 
413  CachedParentName = AllocatorRef->CopyString(OS.str());
414  }
415 
416  return CachedParentName;
417 }
418 
420  void *Mem = getAllocator().Allocate(
421  sizeof(CodeCompletionString) + sizeof(Chunk) * Chunks.size() +
422  sizeof(const char *) * Annotations.size(),
423  alignof(CodeCompletionString));
425  Chunks.data(), Chunks.size(), Priority, Availability, Annotations.data(),
426  Annotations.size(), ParentName, BriefComment);
427  Chunks.clear();
428  return Result;
429 }
430 
432  Chunks.push_back(Chunk(CodeCompletionString::CK_TypedText, Text));
433 }
434 
436  Chunks.push_back(Chunk::CreateText(Text));
437 }
438 
440  Chunks.push_back(Chunk::CreateOptional(Optional));
441 }
442 
443 void CodeCompletionBuilder::AddPlaceholderChunk(const char *Placeholder) {
444  Chunks.push_back(Chunk::CreatePlaceholder(Placeholder));
445 }
446 
448  Chunks.push_back(Chunk::CreateInformative(Text));
449 }
450 
451 void CodeCompletionBuilder::AddResultTypeChunk(const char *ResultType) {
452  Chunks.push_back(Chunk::CreateResultType(ResultType));
453 }
454 
456  const char *CurrentParameter) {
457  Chunks.push_back(Chunk::CreateCurrentParameter(CurrentParameter));
458 }
459 
461  const char *Text) {
462  Chunks.push_back(Chunk(CK, Text));
463 }
464 
466  if (DC->isTranslationUnit())
467  return;
468 
469  if (DC->isFunctionOrMethod())
470  return;
471 
472  const NamedDecl *ND = dyn_cast<NamedDecl>(DC);
473  if (!ND)
474  return;
475 
476  ParentName = getCodeCompletionTUInfo().getParentName(DC);
477 }
478 
479 void CodeCompletionBuilder::addBriefComment(StringRef Comment) {
480  BriefComment = Allocator.CopyString(Comment);
481 }
482 
483 //===----------------------------------------------------------------------===//
484 // Code completion overload candidate implementation
485 //===----------------------------------------------------------------------===//
487  if (getKind() == CK_Function)
488  return Function;
489  else if (getKind() == CK_FunctionTemplate)
490  return FunctionTemplate->getTemplatedDecl();
491  else
492  return nullptr;
493 }
494 
495 const FunctionType *
497  switch (Kind) {
498  case CK_Function:
499  return Function->getType()->getAs<FunctionType>();
500 
501  case CK_FunctionTemplate:
502  return FunctionTemplate->getTemplatedDecl()
503  ->getType()
504  ->getAs<FunctionType>();
505 
506  case CK_FunctionType:
507  return Type;
508  }
509 
510  llvm_unreachable("Invalid CandidateKind!");
511 }
512 
513 //===----------------------------------------------------------------------===//
514 // Code completion consumer implementation
515 //===----------------------------------------------------------------------===//
516 
518 
520  StringRef Filter, CodeCompletionResult Result) {
521  switch (Result.Kind) {
523  return !(Result.Declaration->getIdentifier() &&
524  Result.Declaration->getIdentifier()->getName().startswith(Filter));
526  return !StringRef(Result.Keyword).startswith(Filter);
528  return !Result.Macro->getName().startswith(Filter);
530  return !(Result.Pattern->getTypedText() &&
531  StringRef(Result.Pattern->getTypedText()).startswith(Filter));
532  }
533  llvm_unreachable("Unknown code completion result Kind.");
534 }
535 
537  Sema &SemaRef, CodeCompletionContext Context, CodeCompletionResult *Results,
538  unsigned NumResults) {
539  std::stable_sort(Results, Results + NumResults);
540 
541  if (!Context.getPreferredType().isNull())
542  OS << "PREFERRED-TYPE: " << Context.getPreferredType().getAsString()
543  << "\n";
544 
545  StringRef Filter = SemaRef.getPreprocessor().getCodeCompletionFilter();
546  // Print the completions.
547  for (unsigned I = 0; I != NumResults; ++I) {
548  if (!Filter.empty() && isResultFilteredOut(Filter, Results[I]))
549  continue;
550  OS << "COMPLETION: ";
551  switch (Results[I].Kind) {
553  OS << *Results[I].Declaration;
554  {
555  std::vector<std::string> Tags;
556  if (Results[I].Hidden)
557  Tags.push_back("Hidden");
558  if (Results[I].InBaseClass)
559  Tags.push_back("InBase");
560  if (Results[I].Availability ==
562  Tags.push_back("Inaccessible");
563  if (!Tags.empty())
564  OS << " (" << llvm::join(Tags, ",") << ")";
565  }
566  if (CodeCompletionString *CCS = Results[I].CreateCodeCompletionString(
567  SemaRef, Context, getAllocator(), CCTUInfo,
568  includeBriefComments())) {
569  OS << " : " << CCS->getAsString();
570  if (const char *BriefComment = CCS->getBriefComment())
571  OS << " : " << BriefComment;
572  }
573  for (const FixItHint &FixIt : Results[I].FixIts) {
574  const SourceLocation BLoc = FixIt.RemoveRange.getBegin();
575  const SourceLocation ELoc = FixIt.RemoveRange.getEnd();
576 
577  SourceManager &SM = SemaRef.SourceMgr;
578  std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(BLoc);
579  std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(ELoc);
580  // Adjust for token ranges.
581  if (FixIt.RemoveRange.isTokenRange())
582  EInfo.second += Lexer::MeasureTokenLength(ELoc, SM, SemaRef.LangOpts);
583 
584  OS << " (requires fix-it:"
585  << " {" << SM.getLineNumber(BInfo.first, BInfo.second) << ':'
586  << SM.getColumnNumber(BInfo.first, BInfo.second) << '-'
587  << SM.getLineNumber(EInfo.first, EInfo.second) << ':'
588  << SM.getColumnNumber(EInfo.first, EInfo.second) << "}"
589  << " to \"" << FixIt.CodeToInsert << "\")";
590  }
591  OS << '\n';
592  break;
593 
595  OS << Results[I].Keyword << '\n';
596  break;
597 
599  OS << Results[I].Macro->getName();
600  if (CodeCompletionString *CCS = Results[I].CreateCodeCompletionString(
601  SemaRef, Context, getAllocator(), CCTUInfo,
602  includeBriefComments())) {
603  OS << " : " << CCS->getAsString();
604  }
605  OS << '\n';
606  break;
607 
609  OS << "Pattern : " << Results[I].Pattern->getAsString() << '\n';
610  break;
611  }
612  }
613 }
614 
615 // This function is used solely to preserve the former presentation of overloads
616 // by "clang -cc1 -code-completion-at", since CodeCompletionString::getAsString
617 // needs to be improved for printing the newer and more detailed overload
618 // chunks.
619 static std::string getOverloadAsString(const CodeCompletionString &CCS) {
620  std::string Result;
621  llvm::raw_string_ostream OS(Result);
622 
623  for (auto &C : CCS) {
624  switch (C.Kind) {
627  OS << "[#" << C.Text << "#]";
628  break;
629 
631  OS << "<#" << C.Text << "#>";
632  break;
633 
634  // FIXME: We can also print optional parameters of an overload.
636  break;
637 
638  default:
639  OS << C.Text;
640  break;
641  }
642  }
643  return OS.str();
644 }
645 
647  Sema &SemaRef, unsigned CurrentArg, OverloadCandidate *Candidates,
648  unsigned NumCandidates, SourceLocation OpenParLoc) {
649  OS << "OPENING_PAREN_LOC: ";
650  OpenParLoc.print(OS, SemaRef.getSourceManager());
651  OS << "\n";
652 
653  for (unsigned I = 0; I != NumCandidates; ++I) {
654  if (CodeCompletionString *CCS = Candidates[I].CreateSignatureString(
655  CurrentArg, SemaRef, getAllocator(), CCTUInfo,
656  includeBriefComments())) {
657  OS << "OVERLOAD: " << getOverloadAsString(*CCS) << "\n";
658  }
659  }
660 }
661 
662 /// Retrieve the effective availability of the given declaration.
665  if (isa<EnumConstantDecl>(D))
666  AR = std::max(AR, cast<Decl>(D->getDeclContext())->getAvailability());
667  return AR;
668 }
669 
670 void CodeCompletionResult::computeCursorKindAndAvailability(bool Accessible) {
671  switch (Kind) {
672  case RK_Pattern:
673  if (!Declaration) {
674  // Do nothing: Patterns can come with cursor kinds!
675  break;
676  }
677  LLVM_FALLTHROUGH;
678 
679  case RK_Declaration: {
680  // Set the availability based on attributes.
681  switch (getDeclAvailability(Declaration)) {
682  case AR_Available:
683  case AR_NotYetIntroduced:
684  Availability = CXAvailability_Available;
685  break;
686 
687  case AR_Deprecated:
688  Availability = CXAvailability_Deprecated;
689  break;
690 
691  case AR_Unavailable:
692  Availability = CXAvailability_NotAvailable;
693  break;
694  }
695 
696  if (const auto *Function = dyn_cast<FunctionDecl>(Declaration))
697  if (Function->isDeleted())
698  Availability = CXAvailability_NotAvailable;
699 
700  CursorKind = getCursorKindForDecl(Declaration);
701  if (CursorKind == CXCursor_UnexposedDecl) {
702  // FIXME: Forward declarations of Objective-C classes and protocols
703  // are not directly exposed, but we want code completion to treat them
704  // like a definition.
705  if (isa<ObjCInterfaceDecl>(Declaration))
706  CursorKind = CXCursor_ObjCInterfaceDecl;
707  else if (isa<ObjCProtocolDecl>(Declaration))
708  CursorKind = CXCursor_ObjCProtocolDecl;
709  else
710  CursorKind = CXCursor_NotImplemented;
711  }
712  break;
713  }
714 
715  case RK_Macro:
716  case RK_Keyword:
717  llvm_unreachable("Macro and keyword kinds are handled by the constructors");
718  }
719 
720  if (!Accessible)
721  Availability = CXAvailability_NotAccessible;
722 }
723 
724 /// Retrieve the name that should be used to order a result.
725 ///
726 /// If the name needs to be constructed as a string, that string will be
727 /// saved into Saved and the returned StringRef will refer to it.
728 StringRef CodeCompletionResult::getOrderedName(std::string &Saved) const {
729  switch (Kind) {
730  case RK_Keyword:
731  return Keyword;
732  case RK_Pattern:
733  return Pattern->getTypedText();
734  case RK_Macro:
735  return Macro->getName();
736  case RK_Declaration:
737  // Handle declarations below.
738  break;
739  }
740 
741  DeclarationName Name = Declaration->getDeclName();
742 
743  // If the name is a simple identifier (by far the common case), or a
744  // zero-argument selector, just return a reference to that identifier.
745  if (IdentifierInfo *Id = Name.getAsIdentifierInfo())
746  return Id->getName();
747  if (Name.isObjCZeroArgSelector())
749  return Id->getName();
750 
751  Saved = Name.getAsString();
752  return Saved;
753 }
754 
756  const CodeCompletionResult &Y) {
757  std::string XSaved, YSaved;
758  StringRef XStr = X.getOrderedName(XSaved);
759  StringRef YStr = Y.getOrderedName(YSaved);
760  int cmp = XStr.compare_lower(YStr);
761  if (cmp)
762  return cmp < 0;
763 
764  // If case-insensitive comparison fails, try case-sensitive comparison.
765  return XStr.compare(YStr) < 0;
766 }
One piece of the code completion string.
An unknown context, in which we are recovering from a parsing error and don&#39;t know which completions ...
A code completion string that is entirely optional.
CXAvailabilityKind
Describes the availability of a particular entity, which indicates whether the use of this entity wil...
Definition: Index.h:131
Represents a function declaration or definition.
Definition: Decl.h:1743
ResultKind Kind
The kind of result stored here.
StringRef getParentName(const DeclContext *DC)
Code completion for a selector, as in an @selector expression.
Code completion occurred where an existing name(such as type, function or variable) is expected...
const FunctionType * getFunctionType() const
Retrieve the function type of the entity, regardless of how the function is stored.
void AddTextChunk(const char *Text)
Add a new text chunk.
Code completion where an Objective-C class message is expected.
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition: Type.h:3367
FunctionDecl * getFunction() const
Retrieve the function overload candidate or the templated function declaration for a function templat...
C Language Family Type Representation.
Code completion within a type-qualifier list.
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:88
void AddChunk(CodeCompletionString::ChunkKind CK, const char *Text="")
Add a new chunk.
Selector getObjCSelector() const
Get the Objective-C selector stored in this declaration name.
Defines the C++ template declaration subclasses.
unsigned getColumnNumber(FileID FID, unsigned FilePos, bool *Invalid=nullptr) const
Return the column # for the specified file position.
static Chunk CreateText(const char *Text)
Create a new text chunk.
The base class of the type hierarchy.
Definition: Type.h:1414
An unspecified code-completion context.
static Chunk CreateOptional(CodeCompletionString *Optional)
Create a new optional chunk.
Code completion occurred where an Objective-C message receiver is expected.
unsigned getAnnotationCount() const
Retrieve the number of annotations for this code completion result.
Code completion occurred on the right-hand side of a member access expression using the arrow operato...
ChunkKind
The different kinds of "chunks" that can occur within a code completion string.
Code completion occurred after the "enum" keyword, to indicate an enumeration name.
const T * getAs() const
Member-template getAs<specific type>&#39;.
Definition: Type.h:6803
A piece of text that describes the type of an entity or, for functions and methods, the return type.
const char * Keyword
When Kind == RK_Keyword, the string representing the keyword or symbol&#39;s spelling.
static Chunk CreateResultType(const char *ResultType)
Create a new result type chunk.
void AddOptionalChunk(CodeCompletionString *Optional)
Add a new optional chunk.
Code completion occurred within the instance variable list of an Objective-C interface, implementation, or category implementation.
void AddTypedTextChunk(const char *Text)
Add a new typed-text chunk.
The entity is not available; any use of it will be an error.
Definition: Index.h:144
Parse and apply any fixits to the source.
std::string getName(ArrayRef< StringRef > Parts) const
Get the platform-specific name separator.
Code completion occurred where both a new name and an existing symbol is permissible.
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition: Decl.h:269
One of these records is kept for each identifier that is lexed.
CodeCompletionString * TakeString()
Take the resulting completion string.
void print(raw_ostream &OS, const SourceManager &SM) const
static Chunk CreateInformative(const char *Informative)
Create a new informative chunk.
A "string" used to describe how code completion can be performed for an entity.
An Objective-C @protocol declaration.
Definition: Index.h:1752
void AddResultTypeChunk(const char *ResultType)
Add a new result-type chunk.
static Chunk CreatePlaceholder(const char *Placeholder)
Create a new placeholder chunk.
CodeCompletionString * Pattern
When Kind == RK_Pattern, the code-completion string that describes the completion text to insert...
Code completion occurred where a preprocessor directive is expected.
Code completion occurred within an Objective-C implementation or category implementation.
Code completion occurred where a namespace or namespace alias is expected.
static Chunk CreateCurrentParameter(const char *CurrentParameter)
Create a new current-parameter chunk.
Represents an ObjC class declaration.
Definition: DeclObjC.h:1171
The piece of text that the user is expected to type to match the code-completion string, typically a keyword or the name of a declarator or macro.
A comma separator (&#39;,&#39;).
bool wantConstructorResults() const
Determines whether we want C++ constructors as results within this context.
const LangOptions & LangOpts
Definition: Sema.h:372
AvailabilityResult
Captures the result of checking the availability of a declaration.
Definition: DeclBase.h:74
A right parenthesis (&#39;)&#39;).
Code completion where an Objective-C category name is expected.
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:328
Code completion occurred within a "top-level" completion context, e.g., at namespace or global scope...
AvailabilityResult getAvailability(std::string *Message=nullptr, VersionTuple EnclosingVersion=VersionTuple(), StringRef *RealizedPlatform=nullptr) const
Determine the availability of the given declaration.
Definition: DeclBase.cpp:573
static std::string getOverloadAsString(const CodeCompletionString &CCS)
Code completion occurred where a protocol name is expected.
CXCursorKind getCursorKindForDecl(const Decl *D)
Determine the libclang cursor kind associated with the given declaration.
QualType getPreferredType() const
Retrieve the type that this expression would prefer to have, e.g., if the expression is a variable in...
llvm::StringRef getCompletionKindString(CodeCompletionContext::Kind Kind)
Get string representation of Kind, useful for for debugging.
int Id
Definition: ASTDiff.cpp:190
std::string getAsString() const
Retrieve a string representation of the code completion string, which is mainly useful for debugging...
static unsigned MeasureTokenLength(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
MeasureTokenLength - Relex the token at the specified location and return its length in bytes in the ...
Definition: Lexer.cpp:435
const char * Text
The text string associated with a CK_Text, CK_Placeholder, CK_Informative, or CK_Comma chunk...
std::string getAsString() const
Retrieve the human-readable string for this name.
Defines the clang::Preprocessor interface.
const NamedDecl * Declaration
When Kind == RK_Declaration or RK_Pattern, the declaration we are referring to.
DeclContext * getDeclContext()
Definition: DeclBase.h:431
IdentifierInfo * getAsIdentifierInfo() const
Retrieve the IdentifierInfo * stored in this declaration name, or null if this declaration name isn&#39;t...
Captures a result of code completion.
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
bool isFunctionOrMethod() const
Definition: DeclBase.h:1815
Code completion in a parenthesized expression, which means that we may also have types here in C and ...
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition: DeclBase.h:1763
Code completion occurred in a context where natural language is expected, e.g., a comment or string l...
Preprocessor & getPreprocessor() const
Definition: Sema.h:1282
A piece of text that should be placed in the buffer, e.g., parentheses or a comma in a function call...
The result type of a method or function.
bool isNull() const
Return true if this QualType doesn&#39;t point to a type yet.
Definition: Type.h:704
const char * CopyString(const Twine &String)
Copy the given string into this allocator.
const SourceManager & SM
Definition: Format.cpp:1568
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
Definition: opencl-c.h:89
const char * getTypedText() const
Returns the text in the TypedText chunk.
CodeCompletionString * Optional
The code completion string associated with a CK_Optional chunk.
The context in which code completion occurred, so that the code-completion consumer can process the r...
const IdentifierInfo * Macro
When Kind == RK_Macro, the identifier that refers to a macro.
Code completion occurred within a class, struct, or union.
llvm::cl::opt< std::string > Filter
Kind
IdentifierInfo * getIdentifierInfoForSlot(unsigned argIndex) const
Retrieve the identifier at a given position in the selector.
Encodes a location in the source.
Code completion occurred where a new name is expected.
const char * getAnnotation(unsigned AnnotationNr) const
Retrieve the annotation string specified by AnnotationNr.
Code completion where the name of an Objective-C class is expected.
Code completion occurred within an Objective-C interface, protocol, or category interface.
unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid=nullptr) const
Given a SourceLocation, return the spelling line number for the position indicated.
The entity is available.
Definition: Index.h:135
void addParentContext(const DeclContext *DC)
Add the parent context information to this code completion.
bool operator<(DeclarationName LHS, DeclarationName RHS)
Ordering on two declaration names.
The entity is available, but not accessible; any use of it will be an error.
Definition: Index.h:149
ChunkKind Kind
The kind of data stored in this piece of the code completion string.
void addBriefComment(StringRef Comment)
StringRef getName() const
Return the actual identifier string.
Dataflow Directional Tag Classes.
Code completion occurred where an macro is being defined.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1264
A piece of text that describes something about the result but should not be inserted into the buffer...
Code completion occurred after the "struct" or "class" keyword, to indicate a struct or class name...
static AvailabilityResult getDeclAvailability(const Decl *D)
Retrieve the effective availability of the given declaration.
void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context, CodeCompletionResult *Results, unsigned NumResults) override
Prints the finalized code-completion results.
Code completion occurred after the "union" keyword, to indicate a union name.
Code completion occurred where a macro name is expected (without any arguments, in the case of a func...
Code completion where an Objective-C instance message is expected.
The name of a declaration.
static std::string getAsString(SplitQualType split, const PrintingPolicy &Policy)
Definition: Type.h:972
StringRef getCodeCompletionFilter()
Get the code completion token for filtering purposes.
Refers to a precomputed pattern.
Code completion occurred where a statement (or declaration) is expected in a function, method, or block.
Code completion occurred on the right-hand side of an Objective-C property access expression...
void AddInformativeChunk(const char *Text)
Add a new informative chunk.
void AddCurrentParameterChunk(const char *CurrentParameter)
Add a new current-parameter chunk.
A declaration whose specific kind is not exposed via this interface.
Definition: Index.h:1725
A piece of text that describes the parameter that corresponds to the code-completion location within ...
A string that acts as a placeholder for, e.g., a function call argument.
unsigned getAvailability() const
Retrieve the availability of this code completion result.
A left parenthesis (&#39;(&#39;).
X
Add a minimal nested name specifier fixit hint to allow lookup of a tag name from an outer enclosing ...
Definition: SemaDecl.cpp:14095
Code completion occurred within a preprocessor expression.
Code completion occurred where an expression is expected.
Code completion inside the filename part of a #include directive.
An unspecified code-completion context where we should also add macro completions.
A left angle bracket (&#39;<&#39;).
void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, OverloadCandidate *Candidates, unsigned NumCandidates, SourceLocation OpenParLoc) override
A right angle bracket (&#39;>&#39;).
SourceManager & getSourceManager() const
Definition: Sema.h:1281
Vertical whitespace (&#39;\n&#39; or &#39;\r\n&#39;, depending on the platform).
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition: Decl.h:275
__DEVICE__ int max(int __a, int __b)
static Decl::Kind getKind(const Decl *D)
Definition: DeclBase.cpp:942
StringRef getOrderedName(std::string &Saved) const
Retrieve the name that should be used to order a result.
Code completion occurred on the right-hand side of a member access expression using the dot operator...
void AddPlaceholderChunk(const char *Placeholder)
Add a new placeholder chunk.
Code completion occurred where a type name is expected.
Horizontal whitespace (&#39; &#39;).
SourceManager & SourceMgr
Definition: Sema.h:377
Annotates a diagnostic with some code that should be inserted, removed, or replaced to fix the proble...
Definition: Diagnostic.h:65
StringRef Text
Definition: Format.cpp:1708
bool isResultFilteredOut(StringRef Filter, CodeCompletionResult Results) override
The entity is available, but has been deprecated (and its use is not recommended).
Definition: Index.h:140
This represents a decl that may have a name.
Definition: Decl.h:248
bool isTranslationUnit() const
Definition: DeclBase.h:1838
An Objective-C @interface.
Definition: Index.h:1748
virtual ~CodeCompleteConsumer()
Deregisters and destroys this code-completion consumer.
bool isObjCZeroArgSelector() const
This class handles loading and caching of source files into memory.
std::pair< FileID, unsigned > getDecomposedLoc(SourceLocation Loc) const
Decompose the specified location into a raw FileID + Offset pair.