27#include "llvm/Support/Debug.h"
28#define DEBUG_TYPE "REPLCC"
49 CCTUInfo(CCAllocator), Results(Results), CC(CC) {}
56 unsigned NumResults)
final;
63 std::shared_ptr<GlobalCodeCompletionAllocator> CCAllocator;
65 std::vector<std::string> &Results;
83 std::vector<std::string> &
Results)
91 if (PreferredType.isNull()) {
96 if (
auto *VD = dyn_cast<VarDecl>(
Result.Declaration)) {
97 auto ArgumentType = VD->getType();
98 if (PreferredType->isReferenceType()) {
102 S.CompareReferenceRelationship(
SourceLocation(), RT, ArgumentType,
104 switch (RefRelationship) {
107 Results.push_back(VD->getName().str());
112 }
else if (S.Context.hasSameType(ArgumentType, PreferredType)) {
113 Results.push_back(VD->getName().str());
121 auto Prefix = S.getPreprocessor().getCodeCompletionFilter();
126 if (StringRef(
Result.Keyword).starts_with(Prefix))
142 std::vector<std::string> &
Results)
145 auto *
ID =
Result.Declaration->getIdentifier();
148 if (!isa<CXXMethodDecl>(
Result.Declaration))
150 const auto *Fun = cast<CXXMethodDecl>(
Result.Declaration);
151 if (Fun->getParent()->getCanonicalDecl() ==
153 LLVM_DEBUG(llvm::dbgs() <<
"[In HandleCodeCompleteDOT] Name : "
154 <<
ID->getName() <<
"\n");
166 auto Prefix = S.getPreprocessor().getCodeCompletionFilter();
169 std::unique_ptr<CompletionContextHandler> CCH;
173 switch (Context.getKind()) {
181 for (
unsigned I = 0; I < NumResults; I++) {
182 auto &
Result = InResults[I];
188 if (!
Result.Declaration->getDeclName().isIdentifier() ||
189 !
Result.Declaration->getName().starts_with(Prefix)) {
192 CCH->handleDeclaration(
Result);
195 CCH->handleKeyword(
Result);
201 CCH->handlePattern(
Result);
206 std::sort(Results.begin(), Results.end());
214 : ParentCI(ParentCI) {}
225 std::unique_ptr<ASTImporter> Importer;
264 : ChildTUDeclCtxt(ChildASTCtxt.getTranslationUnitDecl()),
265 ParentASTCtxt(ParentASTCtxt),
266 ParentTUDeclCtxt(ParentASTCtxt.getTranslationUnitDecl()) {
268 new ASTImporter(ChildASTCtxt, ChildFM, ParentASTCtxt, ParentFM,
270 Importer.reset(importer);
278 auto ParentDeclName =
282 ParentTUDeclCtxt->
lookup(ParentDeclName);
284 if (!lookup_result.
empty()) {
292 assert(ChildDeclContext && ChildDeclContext == ChildTUDeclCtxt &&
293 "No child decl context!");
298 for (
auto *DeclCtxt = ParentTUDeclCtxt; DeclCtxt !=
nullptr;
300 for (
auto &IDeclContext : DeclCtxt->decls()) {
301 if (!llvm::isa<NamedDecl>(IDeclContext))
306 auto DeclOrErr = Importer->Import(
Decl);
314 llvm::consumeError(DeclOrErr.takeError());
318 if (!llvm::isa<NamedDecl>(*DeclOrErr))
321 NamedDecl *importedNamedDecl = llvm::cast<NamedDecl>(*DeclOrErr);
327 if (!llvm::isa<CXXRecordDecl>(importedNamedDecl))
330 auto *
Record = llvm::cast<CXXRecordDecl>(importedNamedDecl);
332 if (
auto Err = Importer->ImportDefinition(
Decl)) {
334 consumeError(std::move(Err));
338 Record->setHasLoadedFieldsFromExternalStorage(
true);
339 LLVM_DEBUG(llvm::dbgs()
340 <<
"\nCXXRecrod : " <<
Record->getName() <<
" size(methods): "
341 << std::distance(
Record->method_begin(),
Record->method_end())
342 <<
" has def?: " <<
Record->hasDefinition()
344 << std::distance(
Record->getDefinition()->method_begin(),
345 Record->getDefinition()->method_end())
347 for (
auto *Meth :
Record->methods())
356 llvm::StringRef Content,
unsigned Line,
359 std::vector<std::string> &CCResults) {
371 auto Act = std::make_unique<IncrementalSyntaxOnlyAction>(ParentCI);
372 std::unique_ptr<llvm::MemoryBuffer> MB =
379 AU->setOwnsRemappedFileBuffers(
false);
382 std::make_shared<clang::PCHContainerOperations>(), *diag,
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
llvm::MachO::Record Record
static QualType getPointeeType(const MemRegion *R)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
TranslationUnitDecl * getTranslationUnitDecl() const
void setExternalSource(IntrusiveRefCntPtr< ExternalASTSource > Source)
Attach an external AST source to the AST context.
Imports selected nodes from one AST context into another context, merging AST nodes where appropriate...
static ASTUnit * LoadFromCompilerInvocationAction(std::shared_ptr< CompilerInvocation > CI, std::shared_ptr< PCHContainerOperations > PCHContainerOps, IntrusiveRefCntPtr< DiagnosticsEngine > Diags, FrontendAction *Action=nullptr, ASTUnit *Unit=nullptr, bool Persistent=true, StringRef ResourceFilesPath=StringRef(), bool OnlyLocalDecls=false, CaptureDiagsKind CaptureDiagnostics=CaptureDiagsKind::None, unsigned PrecompilePreambleAfterNParses=0, bool CacheCodeCompletionResults=false, bool UserFilesAreVolatile=false, std::unique_ptr< ASTUnit > *ErrAST=nullptr)
Create an ASTUnit from a source file, via a CompilerInvocation object, by invoking the optionally pro...
CXXRecordDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
Abstract interface for a consumer of code-completion information.
Options controlling the behavior of code completion.
unsigned IncludeCodePatterns
Show code patterns in code completion results.
unsigned IncludeMacros
Show macros in code completion results.
unsigned IncludeBriefComments
Show brief documentation comments in code completion results.
unsigned IncludeGlobals
Show top-level decls in code completion results.
An allocator used specifically for the purpose of code completion.
The context in which code completion occurred, so that the code-completion consumer can process the r...
@ CCC_DotMemberAccess
Code completion occurred on the right-hand side of a member access expression using the dot operator.
QualType getBaseType() const
Retrieve the type of the base object in a member-access expression.
QualType getPreferredType() const
Retrieve the type that this expression would prefer to have, e.g., if the expression is a variable in...
Captures a result of code completion.
@ RK_Pattern
Refers to a precomputed pattern.
@ RK_Declaration
Refers to a declaration.
@ RK_Macro
Refers to a macro.
@ RK_Keyword
Refers to a keyword or symbol.
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
FileManager & getFileManager() const
Return the current file manager to the caller.
IntrusiveRefCntPtr< DiagnosticsEngine > getDiagnosticsPtr() const
ASTContext & getASTContext() const
FrontendOptions & getFrontendOpts()
std::shared_ptr< CompilerInvocation > getInvocationPtr()
LangOptions & getLangOpts()
SourceManager & getSourceManager() const
Return the current source manager.
The class CompletionContextHandler contains four interfaces, each of which handles one type of comple...
CompletionContextHandler(Sema &S, CodeCompletionContext CCC, std::vector< std::string > &Results)
std::vector< std::string > & Results
virtual void handleMacro(const CodeCompletionResult &Result)
Converts a Macro completion result to a completion string, and then stores it in Results.
virtual void handleDeclaration(const CodeCompletionResult &Result)
Converts a Declaration completion result to a completion string, and then stores it in Results.
virtual void handlePattern(const CodeCompletionResult &Result)
Converts a Pattern completion result to a completion string, and then stores it in Results.
virtual void handleKeyword(const CodeCompletionResult &Result)
Converts a Keyword completion result to a completion string, and then stores it in Results.
virtual ~CompletionContextHandler()=default
CodeCompletionContext CCC
The results of name lookup within a DeclContext.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
void setHasExternalVisibleStorage(bool ES=true) const
State whether this DeclContext has external storage for declarations visible in this context.
lookup_result lookup(DeclarationName Name) const
lookup - Find the declarations (if any) with the given Name in this context.
bool hasExternalVisibleStorage() const
Whether this DeclContext has external storage containing additional declarations that are visible in ...
lookups_range lookups() const
void setHasExternalLexicalStorage(bool ES=true) const
State whether this DeclContext has external storage for declarations lexically in this context.
Decl - This represents one declaration (or definition), e.g.
Decl * getPreviousDecl()
Retrieve the previous declaration that declares the same entity as this declaration,...
The name of a declaration.
Options for controlling the compiler diagnostics engine.
void handleDeclaration(const CodeCompletionResult &Result) override
Converts a Declaration completion result to a completion string, and then stores it in Results.
DotMemberAccessHandler(Sema &S, CodeCompletionContext CCC, std::vector< std::string > &Results)
void handleKeyword(const CodeCompletionResult &Result) override
Converts a Keyword completion result to a completion string, and then stores it in Results.
Abstract interface for external sources of AST nodes.
static DeclContextLookupResult SetExternalVisibleDeclsForName(const DeclContext *DC, DeclarationName Name, ArrayRef< NamedDecl * > Decls)
ExternalSource(ASTContext &ChildASTCtxt, FileManager &ChildFM, ASTContext &ParentASTCtxt, FileManager &ParentFM)
void completeVisibleDeclsMap(const clang::DeclContext *childDeclContext) override
Ensures that the table of all visible declarations inside this context is up to date.
bool FindExternalVisibleDeclsByName(const DeclContext *DC, DeclarationName Name) override
Find all declarations with the given name in the given context, and add them to the context by callin...
Implements support for file system lookup, file system caching, and directory search management.
CompilerInstance & getCompilerInstance() const
virtual void ExecuteAction()=0
Callback to run the program action, using the initialized compiler instance.
SmallVector< FrontendInputFile, 0 > Inputs
The input files and their types.
Allocator for a cached set of global code completions.
Implements an efficient mapping from strings to IdentifierInfo nodes.
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
IncrementalSyntaxOnlyAction(const CompilerInstance *ParentCI)
void ExecuteAction() override
Implement the ExecuteAction interface by running Sema on the already-initialized AST consumer.
This represents a decl that may have a name.
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
A (possibly-)qualified type.
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Base for LValueReferenceType and RValueReferenceType.
ReplCompletionConsumer(std::vector< std::string > &Results, ReplCodeCompleter &CC)
void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context, CodeCompletionResult *InResults, unsigned NumResults) final
Process the finalized code-completion results.
CodeCompletionAllocator & getAllocator() override
Retrieve the allocator that will be used to allocate code completion strings.
CodeCompletionTUInfo & getCodeCompletionTUInfo() override
Sema - This implements semantic analysis and AST building for C.
ReferenceCompareResult
ReferenceCompareResult - Expresses the result of comparing two types (cv1 T1 and cv2 T2) to determine...
@ Ref_Incompatible
Ref_Incompatible - The two types are incompatible, so direct reference binding is not possible.
@ Ref_Compatible
Ref_Compatible - The two types are reference-compatible.
@ Ref_Related
Ref_Related - The two types are reference-related, which means that their unqualified forms (T1 and T...
Encodes a location in the source.
The top declaration context.
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
const T * castAs() const
Member-template castAs<specific type>.
The JSON file list parser is used to communicate input to InstallAPI.
@ Result
The result type of a method or function.
clang::CodeCompleteOptions getClangCompleteOpts()
const std::string CodeCompletionFileName
void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, unsigned Line, unsigned Col, const CompilerInstance *ParentCI, std::vector< std::string > &CCResults)
ReferenceConversions
The conversions that would be performed on an lvalue of type T2 when binding a reference of type T1 t...