clang  3.7.0
CodeCompleteConsumer.h
Go to the documentation of this file.
1 //===---- CodeCompleteConsumer.h - 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 defines the CodeCompleteConsumer class.
11 //
12 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_SEMA_CODECOMPLETECONSUMER_H
14 #define LLVM_CLANG_SEMA_CODECOMPLETECONSUMER_H
15 
16 #include "clang-c/Index.h"
18 #include "clang/AST/Type.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/Support/Allocator.h"
24 #include <string>
25 
26 namespace clang {
27 
28 class Decl;
29 
30 /// \brief Default priority values for code-completion results based
31 /// on their kind.
32 enum {
33  /// \brief Priority for the next initialization in a constructor initializer
34  /// list.
36  /// \brief Priority for an enumeration constant inside a switch whose
37  /// condition is of the enumeration type.
39  /// \brief Priority for a send-to-super completion.
41  /// \brief Priority for a declaration that is in the local scope.
43  /// \brief Priority for a member declaration found from the current
44  /// method or member function.
46  /// \brief Priority for a language keyword (that isn't any of the other
47  /// categories).
49  /// \brief Priority for a code pattern.
51  /// \brief Priority for a non-type declaration.
53  /// \brief Priority for a type.
55  /// \brief Priority for a constant value (e.g., enumerator).
57  /// \brief Priority for a preprocessor macro.
58  CCP_Macro = 70,
59  /// \brief Priority for a nested-name-specifier.
61  /// \brief Priority for a result that isn't likely to be what the user wants,
62  /// but is included for completeness.
64 
65  /// \brief Priority for the Objective-C "_cmd" implicit parameter.
67 };
68 
69 /// \brief Priority value deltas that are added to code-completion results
70 /// based on the context of the result.
71 enum {
72  /// \brief The result is in a base class.
74  /// \brief The result is a C++ non-static member function whose qualifiers
75  /// exactly match the object type on which the member function can be called.
77  /// \brief The selector of the given message exactly matches the selector
78  /// of the current method, which might imply that some kind of delegation
79  /// is occurring.
81 
82  /// \brief Adjustment to the "bool" type in Objective-C, where the typedef
83  /// "BOOL" is preferred.
85 
86  /// \brief Adjustment for KVC code pattern priorities when it doesn't look
87  /// like the
89 
90  /// \brief An Objective-C method being used as a property.
92 };
93 
94 /// \brief Priority value factors by which we will divide or multiply the
95 /// priority of a code-completion result.
96 enum {
97  /// \brief Divide by this factor when a code-completion result's type exactly
98  /// matches the type we expect.
100  /// \brief Divide by this factor when a code-completion result's type is
101  /// similar to the type we expect (e.g., both arithmetic types, both
102  /// Objective-C object pointer types).
104 };
105 
106 /// \brief A simplified classification of types used when determining
107 /// "similar" types for code completion.
118 };
119 
120 /// \brief Determine the simplified type class of the given canonical type.
122 
123 /// \brief Determine the type that this declaration will have if it is used
124 /// as a type or in an expression.
125 QualType getDeclUsageType(ASTContext &C, const NamedDecl *ND);
126 
127 /// \brief Determine the priority to be given to a macro code completion result
128 /// with the given name.
129 ///
130 /// \param MacroName The name of the macro.
131 ///
132 /// \param LangOpts Options describing the current language dialect.
133 ///
134 /// \param PreferredTypeIsPointer Whether the preferred type for the context
135 /// of this macro is a pointer type.
136 unsigned getMacroUsagePriority(StringRef MacroName,
137  const LangOptions &LangOpts,
138  bool PreferredTypeIsPointer = false);
139 
140 /// \brief Determine the libclang cursor kind associated with the given
141 /// declaration.
142 CXCursorKind getCursorKindForDecl(const Decl *D);
143 
144 class FunctionDecl;
145 class FunctionType;
146 class FunctionTemplateDecl;
147 class IdentifierInfo;
148 class NamedDecl;
149 class NestedNameSpecifier;
150 class Sema;
151 
152 /// \brief The context in which code completion occurred, so that the
153 /// code-completion consumer can process the results accordingly.
155 public:
156  enum Kind {
157  /// \brief An unspecified code-completion context.
159  /// \brief An unspecified code-completion context where we should also add
160  /// macro completions.
162  /// \brief Code completion occurred within a "top-level" completion context,
163  /// e.g., at namespace or global scope.
165  /// \brief Code completion occurred within an Objective-C interface,
166  /// protocol, or category interface.
168  /// \brief Code completion occurred within an Objective-C implementation
169  /// or category implementation.
171  /// \brief Code completion occurred within the instance variable list of
172  /// an Objective-C interface, implementation, or category implementation.
174  /// \brief Code completion occurred within a class, struct, or union.
176  /// \brief Code completion occurred where a statement (or declaration) is
177  /// expected in a function, method, or block.
179  /// \brief Code completion occurred where an expression is expected.
181  /// \brief Code completion occurred where an Objective-C message receiver
182  /// is expected.
184  /// \brief Code completion occurred on the right-hand side of a member
185  /// access expression using the dot operator.
186  ///
187  /// The results of this completion are the members of the type being
188  /// accessed. The type itself is available via
189  /// \c CodeCompletionContext::getType().
191  /// \brief Code completion occurred on the right-hand side of a member
192  /// access expression using the arrow operator.
193  ///
194  /// The results of this completion are the members of the type being
195  /// accessed. The type itself is available via
196  /// \c CodeCompletionContext::getType().
198  /// \brief Code completion occurred on the right-hand side of an Objective-C
199  /// property access expression.
200  ///
201  /// The results of this completion are the members of the type being
202  /// accessed. The type itself is available via
203  /// \c CodeCompletionContext::getType().
205  /// \brief Code completion occurred after the "enum" keyword, to indicate
206  /// an enumeration name.
208  /// \brief Code completion occurred after the "union" keyword, to indicate
209  /// a union name.
211  /// \brief Code completion occurred after the "struct" or "class" keyword,
212  /// to indicate a struct or class name.
214  /// \brief Code completion occurred where a protocol name is expected.
216  /// \brief Code completion occurred where a namespace or namespace alias
217  /// is expected.
219  /// \brief Code completion occurred where a type name is expected.
221  /// \brief Code completion occurred where a new name is expected.
223  /// \brief Code completion occurred where a new name is expected and a
224  /// qualified name is permissible.
226  /// \brief Code completion occurred where an macro is being defined.
228  /// \brief Code completion occurred where a macro name is expected
229  /// (without any arguments, in the case of a function-like macro).
231  /// \brief Code completion occurred within a preprocessor expression.
233  /// \brief Code completion occurred where a preprocessor directive is
234  /// expected.
236  /// \brief Code completion occurred in a context where natural language is
237  /// expected, e.g., a comment or string literal.
238  ///
239  /// This context usually implies that no completions should be added,
240  /// unless they come from an appropriate natural-language dictionary.
242  /// \brief Code completion for a selector, as in an \@selector expression.
244  /// \brief Code completion within a type-qualifier list.
246  /// \brief Code completion in a parenthesized expression, which means that
247  /// we may also have types here in C and Objective-C (as well as in C++).
249  /// \brief Code completion where an Objective-C instance message is
250  /// expected.
252  /// \brief Code completion where an Objective-C class message is expected.
254  /// \brief Code completion where the name of an Objective-C class is
255  /// expected.
257  /// \brief Code completion where an Objective-C category name is expected.
259  /// \brief An unknown context, in which we are recovering from a parsing
260  /// error and don't know which completions we should give.
262  };
263 
264 private:
265  enum Kind Kind;
266 
267  /// \brief The type that would prefer to see at this point (e.g., the type
268  /// of an initializer or function parameter).
269  QualType PreferredType;
270 
271  /// \brief The type of the base object in a member access expression.
272  QualType BaseType;
273 
274  /// \brief The identifiers for Objective-C selector parts.
275  ArrayRef<IdentifierInfo *> SelIdents;
276 
277 public:
278  /// \brief Construct a new code-completion context of the given kind.
279  CodeCompletionContext(enum Kind Kind) : Kind(Kind), SelIdents(None) { }
280 
281  /// \brief Construct a new code-completion context of the given kind.
283  ArrayRef<IdentifierInfo *> SelIdents = None)
284  : Kind(Kind),
285  SelIdents(SelIdents) {
286  if (Kind == CCC_DotMemberAccess || Kind == CCC_ArrowMemberAccess ||
287  Kind == CCC_ObjCPropertyAccess || Kind == CCC_ObjCClassMessage ||
288  Kind == CCC_ObjCInstanceMessage)
289  BaseType = T;
290  else
291  PreferredType = T;
292  }
293 
294  /// \brief Retrieve the kind of code-completion context.
295  enum Kind getKind() const { return Kind; }
296 
297  /// \brief Retrieve the type that this expression would prefer to have, e.g.,
298  /// if the expression is a variable initializer or a function argument, the
299  /// type of the corresponding variable or function parameter.
300  QualType getPreferredType() const { return PreferredType; }
301 
302  /// \brief Retrieve the type of the base object in a member-access
303  /// expression.
304  QualType getBaseType() const { return BaseType; }
305 
306  /// \brief Retrieve the Objective-C selector identifiers.
307  ArrayRef<IdentifierInfo *> getSelIdents() const { return SelIdents; }
308 
309  /// \brief Determines whether we want C++ constructors as results within this
310  /// context.
311  bool wantConstructorResults() const;
312 };
313 
314 
315 /// \brief A "string" used to describe how code completion can
316 /// be performed for an entity.
317 ///
318 /// A code completion string typically shows how a particular entity can be
319 /// used. For example, the code completion string for a function would show
320 /// the syntax to call it, including the parentheses, placeholders for the
321 /// arguments, etc.
323 public:
324  /// \brief The different kinds of "chunks" that can occur within a code
325  /// completion string.
326  enum ChunkKind {
327  /// \brief The piece of text that the user is expected to type to
328  /// match the code-completion string, typically a keyword or the name of a
329  /// declarator or macro.
331  /// \brief A piece of text that should be placed in the buffer, e.g.,
332  /// parentheses or a comma in a function call.
334  /// \brief A code completion string that is entirely optional. For example,
335  /// an optional code completion string that describes the default arguments
336  /// in a function call.
338  /// \brief A string that acts as a placeholder for, e.g., a function
339  /// call argument.
341  /// \brief A piece of text that describes something about the result but
342  /// should not be inserted into the buffer.
344  /// \brief A piece of text that describes the type of an entity or, for
345  /// functions and methods, the return type.
347  /// \brief A piece of text that describes the parameter that corresponds
348  /// to the code-completion location within a function call, message send,
349  /// macro invocation, etc.
351  /// \brief A left parenthesis ('(').
353  /// \brief A right parenthesis (')').
355  /// \brief A left bracket ('[').
357  /// \brief A right bracket (']').
359  /// \brief A left brace ('{').
361  /// \brief A right brace ('}').
363  /// \brief A left angle bracket ('<').
365  /// \brief A right angle bracket ('>').
367  /// \brief A comma separator (',').
369  /// \brief A colon (':').
371  /// \brief A semicolon (';').
373  /// \brief An '=' sign.
375  /// \brief Horizontal whitespace (' ').
377  /// \brief Vertical whitespace ('\\n' or '\\r\\n', depending on the
378  /// platform).
380  };
381 
382  /// \brief One piece of the code completion string.
383  struct Chunk {
384  /// \brief The kind of data stored in this piece of the code completion
385  /// string.
387 
388  union {
389  /// \brief The text string associated with a CK_Text, CK_Placeholder,
390  /// CK_Informative, or CK_Comma chunk.
391  /// The string is owned by the chunk and will be deallocated
392  /// (with delete[]) when the chunk is destroyed.
393  const char *Text;
394 
395  /// \brief The code completion string associated with a CK_Optional chunk.
396  /// The optional code completion string is owned by the chunk, and will
397  /// be deallocated (with delete) when the chunk is destroyed.
399  };
400 
401  Chunk() : Kind(CK_Text), Text(nullptr) { }
402 
403  explicit Chunk(ChunkKind Kind, const char *Text = "");
404 
405  /// \brief Create a new text chunk.
406  static Chunk CreateText(const char *Text);
407 
408  /// \brief Create a new optional chunk.
410 
411  /// \brief Create a new placeholder chunk.
412  static Chunk CreatePlaceholder(const char *Placeholder);
413 
414  /// \brief Create a new informative chunk.
415  static Chunk CreateInformative(const char *Informative);
416 
417  /// \brief Create a new result type chunk.
418  static Chunk CreateResultType(const char *ResultType);
419 
420  /// \brief Create a new current-parameter chunk.
421  static Chunk CreateCurrentParameter(const char *CurrentParameter);
422  };
423 
424 private:
425  /// \brief The number of chunks stored in this string.
426  unsigned NumChunks : 16;
427 
428  /// \brief The number of annotations for this code-completion result.
429  unsigned NumAnnotations : 16;
430 
431  /// \brief The priority of this code-completion string.
432  unsigned Priority : 16;
433 
434  /// \brief The availability of this code-completion result.
435  unsigned Availability : 2;
436 
437  /// \brief The name of the parent context.
438  StringRef ParentName;
439 
440  /// \brief A brief documentation comment attached to the declaration of
441  /// entity being completed by this result.
442  const char *BriefComment;
443 
444  CodeCompletionString(const CodeCompletionString &) = delete;
445  void operator=(const CodeCompletionString &) = delete;
446 
447  CodeCompletionString(const Chunk *Chunks, unsigned NumChunks,
448  unsigned Priority, CXAvailabilityKind Availability,
449  const char **Annotations, unsigned NumAnnotations,
450  StringRef ParentName,
451  const char *BriefComment);
452  ~CodeCompletionString() = default;
453 
454  friend class CodeCompletionBuilder;
455  friend class CodeCompletionResult;
456 
457 public:
458  typedef const Chunk *iterator;
459  iterator begin() const { return reinterpret_cast<const Chunk *>(this + 1); }
460  iterator end() const { return begin() + NumChunks; }
461  bool empty() const { return NumChunks == 0; }
462  unsigned size() const { return NumChunks; }
463 
464  const Chunk &operator[](unsigned I) const {
465  assert(I < size() && "Chunk index out-of-range");
466  return begin()[I];
467  }
468 
469  /// \brief Returns the text in the TypedText chunk.
470  const char *getTypedText() const;
471 
472  /// \brief Retrieve the priority of this code completion result.
473  unsigned getPriority() const { return Priority; }
474 
475  /// \brief Retrieve the availability of this code completion result.
476  unsigned getAvailability() const { return Availability; }
477 
478  /// \brief Retrieve the number of annotations for this code completion result.
479  unsigned getAnnotationCount() const;
480 
481  /// \brief Retrieve the annotation string specified by \c AnnotationNr.
482  const char *getAnnotation(unsigned AnnotationNr) const;
483 
484  /// \brief Retrieve the name of the parent context.
485  StringRef getParentContextName() const {
486  return ParentName;
487  }
488 
489  const char *getBriefComment() const {
490  return BriefComment;
491  }
492 
493  /// \brief Retrieve a string representation of the code completion string,
494  /// which is mainly useful for debugging.
495  std::string getAsString() const;
496 };
497 
498 /// \brief An allocator used specifically for the purpose of code completion.
499 class CodeCompletionAllocator : public llvm::BumpPtrAllocator {
500 public:
501  /// \brief Copy the given string into this allocator.
502  const char *CopyString(const Twine &String);
503 };
504 
505 /// \brief Allocator for a cached set of global code completions.
507  : public CodeCompletionAllocator,
508  public RefCountedBase<GlobalCodeCompletionAllocator>
509 {
510 
511 };
512 
514  llvm::DenseMap<const DeclContext *, StringRef> ParentNames;
516 
517 public:
520  : AllocatorRef(Allocator) { }
521 
523  return AllocatorRef;
524  }
526  assert(AllocatorRef);
527  return *AllocatorRef;
528  }
529 
530  StringRef getParentName(const DeclContext *DC);
531 };
532 
533 } // end namespace clang
534 
535 namespace llvm {
536  template <> struct isPodLike<clang::CodeCompletionString::Chunk> {
537  static const bool value = true;
538  };
539 }
540 
541 namespace clang {
542 
543 /// \brief A builder class used to construct new code-completion strings.
545 public:
547 
548 private:
549  CodeCompletionAllocator &Allocator;
550  CodeCompletionTUInfo &CCTUInfo;
551  unsigned Priority;
552  CXAvailabilityKind Availability;
553  StringRef ParentName;
554  const char *BriefComment;
555 
556  /// \brief The chunks stored in this string.
557  SmallVector<Chunk, 4> Chunks;
558 
559  SmallVector<const char *, 2> Annotations;
560 
561 public:
563  CodeCompletionTUInfo &CCTUInfo)
564  : Allocator(Allocator), CCTUInfo(CCTUInfo),
565  Priority(0), Availability(CXAvailability_Available),
566  BriefComment(nullptr) { }
567 
569  CodeCompletionTUInfo &CCTUInfo,
570  unsigned Priority, CXAvailabilityKind Availability)
571  : Allocator(Allocator), CCTUInfo(CCTUInfo),
572  Priority(Priority), Availability(Availability),
573  BriefComment(nullptr) { }
574 
575  /// \brief Retrieve the allocator into which the code completion
576  /// strings should be allocated.
577  CodeCompletionAllocator &getAllocator() const { return Allocator; }
578 
579  CodeCompletionTUInfo &getCodeCompletionTUInfo() const { return CCTUInfo; }
580 
581  /// \brief Take the resulting completion string.
582  ///
583  /// This operation can only be performed once.
585 
586  /// \brief Add a new typed-text chunk.
587  void AddTypedTextChunk(const char *Text);
588 
589  /// \brief Add a new text chunk.
590  void AddTextChunk(const char *Text);
591 
592  /// \brief Add a new optional chunk.
594 
595  /// \brief Add a new placeholder chunk.
596  void AddPlaceholderChunk(const char *Placeholder);
597 
598  /// \brief Add a new informative chunk.
599  void AddInformativeChunk(const char *Text);
600 
601  /// \brief Add a new result-type chunk.
602  void AddResultTypeChunk(const char *ResultType);
603 
604  /// \brief Add a new current-parameter chunk.
605  void AddCurrentParameterChunk(const char *CurrentParameter);
606 
607  /// \brief Add a new chunk.
608  void AddChunk(CodeCompletionString::ChunkKind CK, const char *Text = "");
609 
610  void AddAnnotation(const char *A) { Annotations.push_back(A); }
611 
612  /// \brief Add the parent context information to this code completion.
613  void addParentContext(const DeclContext *DC);
614 
615  const char *getBriefComment() const { return BriefComment; }
616  void addBriefComment(StringRef Comment);
617 
618  StringRef getParentName() const { return ParentName; }
619 };
620 
621 /// \brief Captures a result of code completion.
623 public:
624  /// \brief Describes the kind of result generated.
625  enum ResultKind {
626  RK_Declaration = 0, ///< Refers to a declaration
627  RK_Keyword, ///< Refers to a keyword or symbol.
628  RK_Macro, ///< Refers to a macro
629  RK_Pattern ///< Refers to a precomputed pattern.
630  };
631 
632  /// \brief When Kind == RK_Declaration or RK_Pattern, the declaration we are
633  /// referring to. In the latter case, the declaration might be NULL.
635 
636  union {
637  /// \brief When Kind == RK_Keyword, the string representing the keyword
638  /// or symbol's spelling.
639  const char *Keyword;
640 
641  /// \brief When Kind == RK_Pattern, the code-completion string that
642  /// describes the completion text to insert.
644 
645  /// \brief When Kind == RK_Macro, the identifier that refers to a macro.
647  };
648 
649  /// \brief The priority of this particular code-completion result.
650  unsigned Priority;
651 
652  /// \brief Specifies which parameter (of a function, Objective-C method,
653  /// macro, etc.) we should start with when formatting the result.
654  unsigned StartParameter;
655 
656  /// \brief The kind of result stored here.
658 
659  /// \brief The cursor kind that describes this result.
661 
662  /// \brief The availability of this result.
664 
665  /// \brief Whether this result is hidden by another name.
666  bool Hidden : 1;
667 
668  /// \brief Whether this result was found via lookup into a base class.
670 
671  /// \brief Whether this declaration is the beginning of a
672  /// nested-name-specifier and, therefore, should be followed by '::'.
674 
675  /// \brief Whether all parameters (of a function, Objective-C
676  /// method, etc.) should be considered "informative".
678 
679  /// \brief Whether we're completing a declaration of the given entity,
680  /// rather than a use of that entity.
681  bool DeclaringEntity : 1;
682 
683  /// \brief If the result should have a nested-name-specifier, this is it.
684  /// When \c QualifierIsInformative, the nested-name-specifier is
685  /// informative rather than required.
687 
688  /// \brief Build a result that refers to a declaration.
690  unsigned Priority,
691  NestedNameSpecifier *Qualifier = nullptr,
692  bool QualifierIsInformative = false,
693  bool Accessible = true)
694  : Declaration(Declaration), Priority(Priority),
700  computeCursorKindAndAvailability(Accessible);
701  }
702 
703  /// \brief Build a result that refers to a keyword or symbol.
705  : Declaration(nullptr), Keyword(Keyword), Priority(Priority),
710  Qualifier(nullptr) {}
711 
712  /// \brief Build a result that refers to a macro.
714  unsigned Priority = CCP_Macro)
715  : Declaration(nullptr), Macro(Macro), Priority(Priority), StartParameter(0),
720  Qualifier(nullptr) {}
721 
722  /// \brief Build a result that refers to a pattern.
724  unsigned Priority = CCP_CodePattern,
727  const NamedDecl *D = nullptr)
728  : Declaration(D), Pattern(Pattern), Priority(Priority), StartParameter(0),
732  DeclaringEntity(false), Qualifier(nullptr)
733  {
734  }
735 
736  /// \brief Build a result that refers to a pattern with an associated
737  /// declaration.
739  unsigned Priority)
740  : Declaration(D), Pattern(Pattern), Priority(Priority), StartParameter(0),
744  Qualifier(nullptr) {
745  computeCursorKindAndAvailability();
746  }
747 
748  /// \brief Retrieve the declaration stored in this result.
749  const NamedDecl *getDeclaration() const {
750  assert(Kind == RK_Declaration && "Not a declaration result");
751  return Declaration;
752  }
753 
754  /// \brief Retrieve the keyword stored in this result.
755  const char *getKeyword() const {
756  assert(Kind == RK_Keyword && "Not a keyword result");
757  return Keyword;
758  }
759 
760  /// \brief Create a new code-completion string that describes how to insert
761  /// this result into a program.
762  ///
763  /// \param S The semantic analysis that created the result.
764  ///
765  /// \param Allocator The allocator that will be used to allocate the
766  /// string itself.
768  const CodeCompletionContext &CCContext,
770  CodeCompletionTUInfo &CCTUInfo,
771  bool IncludeBriefComments);
773  Preprocessor &PP,
774  const CodeCompletionContext &CCContext,
776  CodeCompletionTUInfo &CCTUInfo,
777  bool IncludeBriefComments);
778 
779 private:
780  void computeCursorKindAndAvailability(bool Accessible = true);
781 };
782 
783 bool operator<(const CodeCompletionResult &X, const CodeCompletionResult &Y);
784 
785 inline bool operator>(const CodeCompletionResult &X,
786  const CodeCompletionResult &Y) {
787  return Y < X;
788 }
789 
790 inline bool operator<=(const CodeCompletionResult &X,
791  const CodeCompletionResult &Y) {
792  return !(Y < X);
793 }
794 
795 inline bool operator>=(const CodeCompletionResult &X,
796  const CodeCompletionResult &Y) {
797  return !(X < Y);
798 }
799 
800 
801 raw_ostream &operator<<(raw_ostream &OS,
802  const CodeCompletionString &CCS);
803 
804 /// \brief Abstract interface for a consumer of code-completion
805 /// information.
807 protected:
809 
810  /// \brief Whether the output format for the code-completion consumer is
811  /// binary.
813 
814 public:
816  public:
817  /// \brief Describes the type of overload candidate.
819  /// \brief The candidate is a function declaration.
821  /// \brief The candidate is a function template.
823  /// \brief The "candidate" is actually a variable, expression, or block
824  /// for which we only have a function prototype.
826  };
827 
828  private:
829  /// \brief The kind of overload candidate.
831 
832  union {
833  /// \brief The function overload candidate, available when
834  /// Kind == CK_Function.
836 
837  /// \brief The function template overload candidate, available when
838  /// Kind == CK_FunctionTemplate.
840 
841  /// \brief The function type that describes the entity being called,
842  /// when Kind == CK_FunctionType.
844  };
845 
846  public:
848  : Kind(CK_Function), Function(Function) { }
849 
851  : Kind(CK_FunctionTemplate), FunctionTemplate(FunctionTemplateDecl) { }
852 
854  : Kind(CK_FunctionType), Type(Type) { }
855 
856  /// \brief Determine the kind of overload candidate.
857  CandidateKind getKind() const { return Kind; }
858 
859  /// \brief Retrieve the function overload candidate or the templated
860  /// function declaration for a function template.
861  FunctionDecl *getFunction() const;
862 
863  /// \brief Retrieve the function template overload candidate.
865  assert(getKind() == CK_FunctionTemplate && "Not a function template");
866  return FunctionTemplate;
867  }
868 
869  /// \brief Retrieve the function type of the entity, regardless of how the
870  /// function is stored.
871  const FunctionType *getFunctionType() const;
872 
873  /// \brief Create a new code-completion string that describes the function
874  /// signature of this overload candidate.
875  CodeCompletionString *CreateSignatureString(unsigned CurrentArg,
876  Sema &S,
878  CodeCompletionTUInfo &CCTUInfo,
879  bool IncludeBriefComments) const;
880  };
881 
883  bool OutputIsBinary)
884  : CodeCompleteOpts(CodeCompleteOpts), OutputIsBinary(OutputIsBinary)
885  { }
886 
887  /// \brief Whether the code-completion consumer wants to see macros.
888  bool includeMacros() const {
890  }
891 
892  /// \brief Whether the code-completion consumer wants to see code patterns.
893  bool includeCodePatterns() const {
895  }
896 
897  /// \brief Whether to include global (top-level) declaration results.
898  bool includeGlobals() const {
900  }
901 
902  /// \brief Whether to include brief documentation comments within the set of
903  /// code completions returned.
904  bool includeBriefComments() const {
906  }
907 
908  /// \brief Determine whether the output of this consumer is binary.
909  bool isOutputBinary() const { return OutputIsBinary; }
910 
911  /// \brief Deregisters and destroys this code-completion consumer.
912  virtual ~CodeCompleteConsumer();
913 
914  /// \name Code-completion callbacks
915  //@{
916  /// \brief Process the finalized code-completion results.
919  CodeCompletionResult *Results,
920  unsigned NumResults) { }
921 
922  /// \param S the semantic-analyzer object for which code-completion is being
923  /// done.
924  ///
925  /// \param CurrentArg the index of the current argument.
926  ///
927  /// \param Candidates an array of overload candidates.
928  ///
929  /// \param NumCandidates the number of overload candidates
930  virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
931  OverloadCandidate *Candidates,
932  unsigned NumCandidates) { }
933  //@}
934 
935  /// \brief Retrieve the allocator that will be used to allocate
936  /// code completion strings.
937  virtual CodeCompletionAllocator &getAllocator() = 0;
938 
940 };
941 
942 /// \brief A simple code-completion consumer that prints the results it
943 /// receives in a simple format.
945  /// \brief The raw output stream.
946  raw_ostream &OS;
947 
948  CodeCompletionTUInfo CCTUInfo;
949 
950 public:
951  /// \brief Create a new printing code-completion consumer that prints its
952  /// results to the given raw output stream.
954  raw_ostream &OS)
955  : CodeCompleteConsumer(CodeCompleteOpts, false), OS(OS),
956  CCTUInfo(new GlobalCodeCompletionAllocator) {}
957 
958  /// \brief Prints the finalized code-completion results.
960  CodeCompletionResult *Results,
961  unsigned NumResults) override;
962 
963  void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
964  OverloadCandidate *Candidates,
965  unsigned NumCandidates) override;
966 
968  return CCTUInfo.getAllocator();
969  }
970 
971  CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
972 };
973 
974 } // end namespace clang
975 
976 #endif // LLVM_CLANG_SEMA_CODECOMPLETECONSUMER_H
One piece of the code completion string.
An unknown context, in which we are recovering from a parsing error and don't know which completions ...
A code completion string that is entirely optional. For example, an optional code completion string t...
CXAvailabilityKind
Describes the availability of a particular entity, which indicates whether the use of this entity wil...
Definition: Index.h:125
ResultKind Kind
The kind of result stored here.
FunctionDecl * getFunction() const
Retrieve the function overload candidate or the templated function declaration for a function templat...
CodeCompletionResult(const IdentifierInfo *Macro, unsigned Priority=CCP_Macro)
Build a result that refers to a macro.
const FunctionType * Type
The function type that describes the entity being called, when Kind == CK_FunctionType.
CXCursorKind CursorKind
The cursor kind that describes this result.
CodeCompletionString * CreateSignatureString(unsigned CurrentArg, Sema &S, CodeCompletionAllocator &Allocator, CodeCompletionTUInfo &CCTUInfo, bool IncludeBriefComments) const
Create a new code-completion string that describes the function signature of this overload candidate...
CodeCompletionContext(enum Kind Kind, QualType T, ArrayRef< IdentifierInfo * > SelIdents=None)
Construct a new code-completion context of the given kind.
bool includeGlobals() const
Whether to include global (top-level) declaration results.
CodeCompletionString * CreateCodeCompletionString(Sema &S, const CodeCompletionContext &CCContext, CodeCompletionAllocator &Allocator, CodeCompletionTUInfo &CCTUInfo, bool IncludeBriefComments)
Create a new code-completion string that describes how to insert this result into a program...
StringRef getParentName(const DeclContext *DC)
Priority for the Objective-C "_cmd" implicit parameter.
Code completion for a selector, as in an @selector expression.
bool includeMacros() const
Whether the code-completion consumer wants to see macros.
void AddTextChunk(const char *Text)
Add a new text chunk.
unsigned getMacroUsagePriority(StringRef MacroName, const LangOptions &LangOpts, bool PreferredTypeIsPointer=false)
Determine the priority to be given to a macro code completion result with the given name...
Code completion where an Objective-C class message is expected.
FunctionTemplateDecl * getFunctionTemplate() const
Retrieve the function template overload candidate.
Code completion within a type-qualifier list.
void AddChunk(CodeCompletionString::ChunkKind CK, const char *Text="")
Add a new chunk.
CandidateKind
Describes the type of overload candidate.
const DiagnosticBuilder & operator<<(const DiagnosticBuilder &DB, const Attr *At)
Definition: Attr.h:154
static Chunk CreateText(const char *Text)
Create a new text chunk.
An unspecified code-completion context.
Allocator for a cached set of global code completions.
static Chunk CreateOptional(CodeCompletionString *Optional)
Create a new optional chunk.
Code completion occurred where an Objective-C message receiver is expected.
unsigned Priority
The priority of this particular code-completion result.
Priority for a member declaration found from the current method or member function.
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.
unsigned getAvailability() const
Retrieve the availability of this code completion result.
A piece of text that describes the type of an entity or, for functions and methods, the return type.
bool StartsNestedNameSpecifier
Whether this declaration is the beginning of a nested-name-specifier and, therefore, should be followed by '::'.
const char * Keyword
When Kind == RK_Keyword, the string representing the keyword or symbol's spelling.
static Chunk CreateResultType(const char *ResultType)
Create a new result type chunk.
void AddOptionalChunk(CodeCompletionString *Optional)
Add a new optional chunk.
Priority for a constant value (e.g., enumerator).
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.
StringRef getParentContextName() const
Retrieve the name of the parent context.
bool operator<=(DeclarationName LHS, DeclarationName RHS)
CodeCompletionBuilder(CodeCompletionAllocator &Allocator, CodeCompletionTUInfo &CCTUInfo, unsigned Priority, CXAvailabilityKind Availability)
const FunctionType * getFunctionType() const
Retrieve the function type of the entity, regardless of how the function is stored.
CodeCompletionString * TakeString()
Take the resulting completion string.
OverloadCandidate(FunctionTemplateDecl *FunctionTemplateDecl)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:89
static Chunk CreateInformative(const char *Informative)
Create a new informative chunk.
unsigned getAnnotationCount() const
Retrieve the number of annotations for this code completion result.
A "string" used to describe how code completion can be performed for an entity.
The selector of the given message exactly matches the selector of the current method, which might imply that some kind of delegation is occurring.
void AddResultTypeChunk(const char *ResultType)
Add a new result-type chunk.
bool operator>(DeclarationName LHS, DeclarationName RHS)
CodeCompletionTUInfo(IntrusiveRefCntPtr< GlobalCodeCompletionAllocator > Allocator)
static Chunk CreatePlaceholder(const char *Placeholder)
Create a new placeholder chunk.
An allocator used specifically for the purpose of code completion.
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.
virtual CodeCompletionTUInfo & getCodeCompletionTUInfo()=0
Code completion occurred within an Objective-C implementation or category implementation.
bool QualifierIsInformative
Whether this result was found via lookup into a base class.
FunctionTemplateDecl * FunctionTemplate
The function template overload candidate, available when Kind == CK_FunctionTemplate.
bool wantConstructorResults() const
Determines whether we want C++ constructors as results within this context.
The "candidate" is actually a variable, expression, or block for which we only have a function protot...
QualType getDeclUsageType(ASTContext &C, const NamedDecl *ND)
Determine the type that this declaration will have if it is used as a type or in an expression...
Code completion occurred where a namespace or namespace alias is expected.
static Chunk CreateCurrentParameter(const char *CurrentParameter)
Create a new current-parameter chunk.
CodeCompletionTUInfo & getCodeCompletionTUInfo() const
const char * getKeyword() const
Retrieve the keyword stored in this result.
QualType getBaseType() const
Retrieve the type of the base object in a member-access expression.
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.
bool includeBriefComments() const
Whether to include brief documentation comments within the set of code completions returned...
QualType getPreferredType() const
Retrieve the type that this expression would prefer to have, e.g., if the expression is a variable in...
llvm::SpecificBumpPtrAllocator< FormatToken > Allocator
Definition: Format.cpp:1211
CXCursorKind
Describes the kind of entity that a cursor refers to.
Definition: Index.h:1525
Code completion where an Objective-C category name is expected.
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:258
Code completion occurred within a "top-level" completion context, e.g., at namespace or global scope...
Divide by this factor when a code-completion result's type exactly matches the type we expect...
CodeCompletionTUInfo & getCodeCompletionTUInfo() override
const char * getTypedText() const
Returns the text in the TypedText chunk.
ASTContext * Context
CodeCompletionAllocator & getAllocator() const
Code completion occurred where a protocol name is expected.
CXCursorKind getCursorKindForDecl(const Decl *D)
Determine the libclang cursor kind associated with the given declaration.
A simple code-completion consumer that prints the results it receives in a simple format...
const NamedDecl * getDeclaration() const
Retrieve the declaration stored in this result.
CXAvailabilityKind Availability
The availability of this result.
Code completion occurred where a new name is expected.
unsigned IncludeCodePatterns
Show code patterns in code completion results.
const char * Text
The text string associated with a CK_Text, CK_Placeholder, CK_Informative, or CK_Comma chunk...
virtual void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context, CodeCompletionResult *Results, unsigned NumResults)
Process the finalized code-completion results.
bool includeCodePatterns() const
Whether the code-completion consumer wants to see code patterns.
The result is in a base class.
const NamedDecl * Declaration
When Kind == RK_Declaration or RK_Pattern, the declaration we are referring to. In the latter case...
unsigned getPriority() const
Retrieve the priority of this code completion result.
ArrayRef< IdentifierInfo * > getSelIdents() const
Retrieve the Objective-C selector identifiers.
Captures a result of code completion.
Code completion occurred where a new name is expected and a qualified name is permissible.
Priority for a result that isn't likely to be what the user wants, but is included for completeness...
Code completion in a parenthesized expression, which means that we may also have types here in C and ...
Code completion occurred in a context where natural language is expected, e.g., a comment or string l...
FunctionDecl * Function
The function overload candidate, available when Kind == CK_Function.
IntrusiveRefCntPtr< GlobalCodeCompletionAllocator > getAllocatorRef() const
A piece of text that should be placed in the buffer, e.g., parentheses or a comma in a function call...
Priority for the next initialization in a constructor initializer list.
const char * CopyString(const Twine &String)
Copy the given string into this allocator.
const char * getBriefComment() const
unsigned StartParameter
Specifies which parameter (of a function, Objective-C method, macro, etc.) we should start with when ...
CandidateKind getKind() const
Determine the kind of overload candidate.
CodeCompletionString * Optional
The code completion string associated with a CK_Optional chunk. The optional code completion string i...
Options controlling the behavior of code completion.
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.
PrintingCodeCompleteConsumer(const CodeCompleteOptions &CodeCompleteOpts, raw_ostream &OS)
Create a new printing code-completion consumer that prints its results to the given raw output stream...
Code completion occurred within a class, struct, or union.
#define false
Definition: stdbool.h:33
Kind
OverloadCandidate - A single candidate in an overload set (C++ 13.3).
Definition: Overload.h:586
bool isOutputBinary() const
Determine whether the output of this consumer is binary.
Code completion where the name of an Objective-C class is expected.
The result is a C++ non-static member function whose qualifiers exactly match the object type on whic...
Code completion occurred within an Objective-C interface, protocol, or category interface.
CodeCompleteConsumer(const CodeCompleteOptions &CodeCompleteOpts, bool OutputIsBinary)
virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, OverloadCandidate *Candidates, unsigned NumCandidates)
Represents a C++ nested name specifier, such as "\::std::vector<int>::".
CanQual< Type > CanQualType
Represents a canonical, potentially-qualified type.
The entity is available.
Definition: Index.h:129
Priority for a non-type declaration.
SimplifiedTypeClass getSimplifiedTypeClass(CanQualType T)
Determine the simplified type class of the given canonical type.
bool operator>=(DeclarationName LHS, DeclarationName RHS)
void addParentContext(const DeclContext *DC)
Add the parent context information to this code completion.
bool operator<(DeclarationName LHS, DeclarationName RHS)
ChunkKind Kind
The kind of data stored in this piece of the code completion string.
const char * getBriefComment() const
void addBriefComment(StringRef Comment)
Priority for a nested-name-specifier.
Priority for a declaration that is in the local scope.
Abstract interface for a consumer of code-completion information.
unsigned IncludeGlobals
Show top-level decls in code completion results.
Code completion occurred where an macro is being defined.
Adjustment for KVC code pattern priorities when it doesn't look like the.
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...
void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context, CodeCompletionResult *Results, unsigned NumResults) override
Prints the finalized code-completion results.
CodeCompletionResult(CodeCompletionString *Pattern, unsigned Priority=CCP_CodePattern, CXCursorKind CursorKind=CXCursor_NotImplemented, CXAvailabilityKind Availability=CXAvailability_Available, const NamedDecl *D=nullptr)
Build a result that refers to a pattern.
CodeCompletionResult(const NamedDecl *Declaration, unsigned Priority, NestedNameSpecifier *Qualifier=nullptr, bool QualifierIsInformative=false, bool Accessible=true)
Build a result that refers to a declaration.
NestedNameSpecifier * Qualifier
If the result should have a nested-name-specifier, this is it. When QualifierIsInformative, the nested-name-specifier is informative rather than required.
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...
std::string getAsString() const
Retrieve a string representation of the code completion string, which is mainly useful for debugging...
A builder class used to construct new code-completion strings.
Code completion where an Objective-C instance message is expected.
Priority for a send-to-super completion.
Priority for a type.
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...
unsigned IncludeBriefComments
Show brief documentation comments in code completion results.
const CodeCompleteOptions CodeCompleteOpts
void AddInformativeChunk(const char *Text)
Add a new informative chunk.
CodeCompletionResult(const char *Keyword, unsigned Priority=CCP_Keyword)
Build a result that refers to a keyword or symbol.
void AddCurrentParameterChunk(const char *CurrentParameter)
Add a new current-parameter chunk.
enum Kind getKind() const
Retrieve the kind of code-completion context.
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.
Priority for a language keyword (that isn't any of the other categories).
CodeCompletionResult(CodeCompletionString *Pattern, NamedDecl *D, unsigned Priority)
Build a result that refers to a pattern with an associated declaration.
An Objective-C method being used as a property.
bool Hidden
Whether this result is hidden by another name.
Adjustment to the "bool" type in Objective-C, where the typedef "BOOL" is preferred.
X
Definition: SemaDecl.cpp:11429
Code completion occurred within a preprocessor expression.
Code completion occurred where an expression is expected.
virtual CodeCompletionAllocator & getAllocator()=0
Retrieve the allocator that will be used to allocate code completion strings.
const Chunk & operator[](unsigned I) const
bool AllParametersAreInformative
Whether all parameters (of a function, Objective-C method, etc.) should be considered "informative"...
An unspecified code-completion context where we should also add macro completions.
CodeCompletionAllocator & getAllocator() override
Retrieve the allocator that will be used to allocate code completion strings.
unsigned IncludeMacros
Show macros in code completion results.
const char * getAnnotation(unsigned AnnotationNr) const
Retrieve the annotation string specified by AnnotationNr.
bool OutputIsBinary
Whether the output format for the code-completion consumer is binary.
ResultKind
Describes the kind of result generated.
CodeCompletionString::Chunk Chunk
Vertical whitespace ('\n' or '\r\n', depending on the platform).
CodeCompletionContext(enum Kind Kind)
Construct a new code-completion context of the given kind.
void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, OverloadCandidate *Candidates, unsigned NumCandidates) override
CodeCompletionAllocator & getAllocator() const
Retrieve the allocator into which the code completion strings should be allocated.
Code completion occurred on the right-hand side of a member access expression using the dot operator...
SimplifiedTypeClass
A simplified classification of types used when determining "similar" types for code completion...
void AddPlaceholderChunk(const char *Placeholder)
Add a new placeholder chunk.
Code completion occurred where a type name is expected.
bool DeclaringEntity
Whether we're completing a declaration of the given entity, rather than a use of that entity...
CodeCompletionBuilder(CodeCompletionAllocator &Allocator, CodeCompletionTUInfo &CCTUInfo)
virtual ~CodeCompleteConsumer()
Deregisters and destroys this code-completion consumer.
Declaration of a template function.
Definition: DeclTemplate.h:821
Divide by this factor when a code-completion result's type is similar to the type we expect (e...
Priority for a code pattern.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:96
Priority for a preprocessor macro.
Priority for an enumeration constant inside a switch whose condition is of the enumeration type...