clang 20.0.0git
CodeCompletion.cpp
Go to the documentation of this file.
1//===------ CodeCompletion.cpp - Code Completion for ClangRepl -------===//
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 classes which performs code completion at the REPL.
10//
11//===----------------------------------------------------------------------===//
12
26#include "clang/Sema/Sema.h"
27#include "llvm/Support/Debug.h"
28#define DEBUG_TYPE "REPLCC"
29
30namespace clang {
31
32const std::string CodeCompletionFileName = "input_line_[Completion]";
33
36 Opts.IncludeCodePatterns = true;
37 Opts.IncludeMacros = true;
38 Opts.IncludeGlobals = true;
39 Opts.IncludeBriefComments = true;
40 return Opts;
41}
42
44public:
45 ReplCompletionConsumer(std::vector<std::string> &Results,
48 CCAllocator(std::make_shared<GlobalCodeCompletionAllocator>()),
49 CCTUInfo(CCAllocator), Results(Results), CC(CC) {}
50
51 // The entry of handling code completion. When the function is called, we
52 // create a `Context`-based handler (see classes defined below) to handle each
53 // completion result.
55 CodeCompletionResult *InResults,
56 unsigned NumResults) final;
57
58 CodeCompletionAllocator &getAllocator() override { return *CCAllocator; }
59
60 CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
61
62private:
63 std::shared_ptr<GlobalCodeCompletionAllocator> CCAllocator;
64 CodeCompletionTUInfo CCTUInfo;
65 std::vector<std::string> &Results;
67};
68
69/// The class CompletionContextHandler contains four interfaces, each of
70/// which handles one type of completion result.
71/// Its derived classes are used to create concrete handlers based on
72/// \c CodeCompletionContext.
74protected:
76 std::vector<std::string> &Results;
77
78private:
79 Sema &S;
80
81public:
83 std::vector<std::string> &Results)
84 : CCC(CCC), Results(Results), S(S) {}
85
86 virtual ~CompletionContextHandler() = default;
87 /// Converts a Declaration completion result to a completion string, and then
88 /// stores it in Results.
90 auto PreferredType = CCC.getPreferredType();
91 if (PreferredType.isNull()) {
92 Results.push_back(Result.Declaration->getName().str());
93 return;
94 }
95
96 if (auto *VD = dyn_cast<VarDecl>(Result.Declaration)) {
97 auto ArgumentType = VD->getType();
98 if (PreferredType->isReferenceType()) {
99 QualType RT = PreferredType->castAs<ReferenceType>()->getPointeeType();
101 Sema::ReferenceCompareResult RefRelationship =
102 S.CompareReferenceRelationship(SourceLocation(), RT, ArgumentType,
103 &RefConv);
104 switch (RefRelationship) {
107 Results.push_back(VD->getName().str());
108 break;
110 break;
111 }
112 } else if (S.Context.hasSameType(ArgumentType, PreferredType)) {
113 Results.push_back(VD->getName().str());
114 }
115 }
116 }
117
118 /// Converts a Keyword completion result to a completion string, and then
119 /// stores it in Results.
121 auto Prefix = S.getPreprocessor().getCodeCompletionFilter();
122 // Add keyword to the completion results only if we are in a type-aware
123 // situation.
125 return;
126 if (StringRef(Result.Keyword).starts_with(Prefix))
127 Results.push_back(Result.Keyword);
128 }
129
130 /// Converts a Pattern completion result to a completion string, and then
131 /// stores it in Results.
133
134 /// Converts a Macro completion result to a completion string, and then stores
135 /// it in Results.
137};
138
140public:
142 std::vector<std::string> &Results)
145 auto *ID = Result.Declaration->getIdentifier();
146 if (!ID)
147 return;
148 if (!isa<CXXMethodDecl>(Result.Declaration))
149 return;
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");
155 Results.push_back(ID->getName().str());
156 }
157 }
158
160};
161
163 class Sema &S, CodeCompletionContext Context,
164 CodeCompletionResult *InResults, unsigned NumResults) {
165
166 auto Prefix = S.getPreprocessor().getCodeCompletionFilter();
167 CC.Prefix = Prefix;
168
169 std::unique_ptr<CompletionContextHandler> CCH;
170
171 // initialize fine-grained code completion handler based on the code
172 // completion context.
173 switch (Context.getKind()) {
175 CCH.reset(new DotMemberAccessHandler(S, Context, this->Results));
176 break;
177 default:
178 CCH.reset(new CompletionContextHandler(S, Context, this->Results));
179 };
180
181 for (unsigned I = 0; I < NumResults; I++) {
182 auto &Result = InResults[I];
183 switch (Result.Kind) {
185 if (Result.Hidden) {
186 break;
187 }
188 if (!Result.Declaration->getDeclName().isIdentifier() ||
189 !Result.Declaration->getName().starts_with(Prefix)) {
190 break;
191 }
192 CCH->handleDeclaration(Result);
193 break;
195 CCH->handleKeyword(Result);
196 break;
198 CCH->handleMacro(Result);
199 break;
201 CCH->handlePattern(Result);
202 break;
203 }
204 }
205
206 std::sort(Results.begin(), Results.end());
207}
208
210 const CompilerInstance *ParentCI;
211
212public:
214 : ParentCI(ParentCI) {}
215
216protected:
217 void ExecuteAction() override;
218};
219
221 TranslationUnitDecl *ChildTUDeclCtxt;
222 ASTContext &ParentASTCtxt;
223 TranslationUnitDecl *ParentTUDeclCtxt;
224
225 std::unique_ptr<ASTImporter> Importer;
226
227public:
228 ExternalSource(ASTContext &ChildASTCtxt, FileManager &ChildFM,
229 ASTContext &ParentASTCtxt, FileManager &ParentFM);
231 DeclarationName Name,
232 const DeclContext *OriginalDC) override;
233 void
234 completeVisibleDeclsMap(const clang::DeclContext *childDeclContext) override;
235};
236
237// This method is intended to set up `ExternalASTSource` to the running
238// compiler instance before the super `ExecuteAction` triggers parsing
241 ExternalSource *myExternalSource =
243 ParentCI->getASTContext(), ParentCI->getFileManager());
245 myExternalSource);
246 CI.getASTContext().setExternalSource(astContextExternalSource);
248 true);
249
250 // Load all external decls into current context. Under the hood, it calls
251 // ExternalSource::completeVisibleDeclsMap, which make all decls on the redecl
252 // chain visible.
253 //
254 // This is crucial to code completion on dot members, since a bound variable
255 // before "." would be otherwise treated out-of-scope.
256 //
257 // clang-repl> Foo f1;
258 // clang-repl> f1.<tab>
261}
262
264 ASTContext &ParentASTCtxt, FileManager &ParentFM)
265 : ChildTUDeclCtxt(ChildASTCtxt.getTranslationUnitDecl()),
266 ParentASTCtxt(ParentASTCtxt),
267 ParentTUDeclCtxt(ParentASTCtxt.getTranslationUnitDecl()) {
268 ASTImporter *importer =
269 new ASTImporter(ChildASTCtxt, ChildFM, ParentASTCtxt, ParentFM,
270 /*MinimalImport : ON*/ true);
271 Importer.reset(importer);
272}
273
275 const DeclContext *DC, DeclarationName Name,
276 const DeclContext *OriginalDC) {
277
278 IdentifierTable &ParentIdTable = ParentASTCtxt.Idents;
279
280 auto ParentDeclName =
281 DeclarationName(&(ParentIdTable.get(Name.getAsString())));
282
283 DeclContext::lookup_result lookup_result =
284 ParentTUDeclCtxt->lookup(ParentDeclName);
285
286 if (!lookup_result.empty()) {
287 return true;
288 }
289 return false;
290}
291
293 const DeclContext *ChildDeclContext) {
294 assert(ChildDeclContext && ChildDeclContext == ChildTUDeclCtxt &&
295 "No child decl context!");
296
297 if (!ChildDeclContext->hasExternalVisibleStorage())
298 return;
299
300 for (auto *DeclCtxt = ParentTUDeclCtxt; DeclCtxt != nullptr;
301 DeclCtxt = DeclCtxt->getPreviousDecl()) {
302 for (auto &IDeclContext : DeclCtxt->decls()) {
303 if (!llvm::isa<NamedDecl>(IDeclContext))
304 continue;
305
306 NamedDecl *Decl = llvm::cast<NamedDecl>(IDeclContext);
307
308 auto DeclOrErr = Importer->Import(Decl);
309 if (!DeclOrErr) {
310 // if an error happens, it usually means the decl has already been
311 // imported or the decl is a result of a failed import. But in our
312 // case, every import is fresh each time code completion is
313 // triggered. So Import usually doesn't fail. If it does, it just means
314 // the related decl can't be used in code completion and we can safely
315 // drop it.
316 llvm::consumeError(DeclOrErr.takeError());
317 continue;
318 }
319
320 if (!llvm::isa<NamedDecl>(*DeclOrErr))
321 continue;
322
323 NamedDecl *importedNamedDecl = llvm::cast<NamedDecl>(*DeclOrErr);
324
325 SetExternalVisibleDeclsForName(ChildDeclContext,
326 importedNamedDecl->getDeclName(),
327 importedNamedDecl);
328
329 if (!llvm::isa<CXXRecordDecl>(importedNamedDecl))
330 continue;
331
332 auto *Record = llvm::cast<CXXRecordDecl>(importedNamedDecl);
333
334 if (auto Err = Importer->ImportDefinition(Decl)) {
335 // the same as above
336 consumeError(std::move(Err));
337 continue;
338 }
339
340 Record->setHasLoadedFieldsFromExternalStorage(true);
341 LLVM_DEBUG(llvm::dbgs()
342 << "\nCXXRecrod : " << Record->getName() << " size(methods): "
343 << std::distance(Record->method_begin(), Record->method_end())
344 << " has def?: " << Record->hasDefinition()
345 << " # (methods): "
346 << std::distance(Record->getDefinition()->method_begin(),
347 Record->getDefinition()->method_end())
348 << "\n");
349 for (auto *Meth : Record->methods())
350 SetExternalVisibleDeclsForName(ChildDeclContext, Meth->getDeclName(),
351 Meth);
352 }
353 ChildDeclContext->setHasExternalLexicalStorage(false);
354 }
355}
356
358 llvm::StringRef Content, unsigned Line,
359 unsigned Col,
360 const CompilerInstance *ParentCI,
361 std::vector<std::string> &CCResults) {
362 auto DiagOpts = DiagnosticOptions();
363 auto consumer = ReplCompletionConsumer(CCResults, *this);
364
365 auto diag = InterpCI->getDiagnosticsPtr();
366 std::unique_ptr<ASTUnit> AU(ASTUnit::LoadFromCompilerInvocationAction(
367 InterpCI->getInvocationPtr(), std::make_shared<PCHContainerOperations>(),
368 diag));
373 auto Act = std::make_unique<IncrementalSyntaxOnlyAction>(ParentCI);
374 std::unique_ptr<llvm::MemoryBuffer> MB =
375 llvm::MemoryBuffer::getMemBufferCopy(Content, CodeCompletionFileName);
377
378 RemappedFiles.push_back(std::make_pair(CodeCompletionFileName, MB.get()));
379 // we don't want the AU destructor to release the memory buffer that MB
380 // owns twice, because MB handles its resource on its own.
381 AU->setOwnsRemappedFileBuffers(false);
382 AU->CodeComplete(CodeCompletionFileName, 1, Col, RemappedFiles, false, false,
383 false, consumer,
384 std::make_shared<clang::PCHContainerOperations>(), *diag,
385 InterpCI->getLangOpts(), AU->getSourceManager(),
386 AU->getFileManager(), sd, tb, std::move(Act));
387}
388
389} // namespace clang
static char ID
Definition: Arena.cpp:183
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
llvm::MachO::Record Record
Definition: MachO.h:31
static QualType getPointeeType(const MemRegion *R)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:188
TranslationUnitDecl * getTranslationUnitDecl() const
Definition: ASTContext.h:1141
IdentifierTable & Idents
Definition: ASTContext.h:680
void setExternalSource(IntrusiveRefCntPtr< ExternalASTSource > Source)
Attach an external AST source to the AST context.
Definition: ASTContext.cpp:990
Imports selected nodes from one AST context into another context, merging AST nodes where appropriate...
Definition: ASTImporter.h:62
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...
Definition: ASTUnit.cpp:1554
CXXRecordDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
Definition: DeclCXX.h:524
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_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()
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
The results of name lookup within a DeclContext.
Definition: DeclBase.h:1368
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1435
void setHasExternalVisibleStorage(bool ES=true) const
State whether this DeclContext has external storage for declarations visible in this context.
Definition: DeclBase.h:2693
lookup_result lookup(DeclarationName Name) const
lookup - Find the declarations (if any) with the given Name in this context.
Definition: DeclBase.cpp:1854
bool hasExternalVisibleStorage() const
Whether this DeclContext has external storage containing additional declarations that are visible in ...
Definition: DeclBase.h:2687
lookups_range lookups() const
Definition: DeclLookups.h:75
void setHasExternalLexicalStorage(bool ES=true) const
State whether this DeclContext has external storage for declarations lexically in this context.
Definition: DeclBase.h:2681
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
Decl * getPreviousDecl()
Retrieve the previous declaration that declares the same entity as this declaration,...
Definition: DeclBase.h:1050
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)
Definition: DeclBase.cpp:1609
ExternalSource(ASTContext &ChildASTCtxt, FileManager &ChildFM, ASTContext &ParentASTCtxt, FileManager &ParentFM)
bool FindExternalVisibleDeclsByName(const DeclContext *DC, DeclarationName Name, const DeclContext *OriginalDC) override
Find all declarations with the given name in the given context, and add them to the context by callin...
void completeVisibleDeclsMap(const clang::DeclContext *childDeclContext) override
Ensures that the table of all visible declarations inside this context is up to date.
Implements support for file system lookup, file system caching, and directory search management.
Definition: FileManager.h:53
CompilerInstance & getCompilerInstance() const
virtual void ExecuteAction()=0
Callback to run the program action, using the initialized compiler instance.
An input file for the front end.
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.
Definition: Decl.h:253
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Definition: Decl.h:319
A (possibly-)qualified type.
Definition: Type.h:929
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition: Type.h:996
Base for LValueReferenceType and RValueReferenceType.
Definition: Type.h:3439
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.
Definition: Sema.h:464
ReferenceCompareResult
ReferenceCompareResult - Expresses the result of comparing two types (cv1 T1 and cv2 T2) to determine...
Definition: Sema.h:10114
@ Ref_Incompatible
Ref_Incompatible - The two types are incompatible, so direct reference binding is not possible.
Definition: Sema.h:10117
@ Ref_Compatible
Ref_Compatible - The two types are reference-compatible.
Definition: Sema.h:10123
@ Ref_Related
Ref_Related - The two types are reference-related, which means that their unqualified forms (T1 and T...
Definition: Sema.h:10121
Encodes a location in the source.
The top declaration context.
Definition: Decl.h:84
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1916
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:8805
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...
Definition: Sema.h:10131