Skip to content

Commit 874f511

Browse files
authored
[clang] Introduce SemaCodeCompletion (#92311)
This patch continues previous efforts to split `Sema` up, this time covering code completion. Context can be found in #84184. Dropping `Code` prefix from function names in `SemaCodeCompletion` would make sense, but I think this PR has enough changes already. As usual, formatting changes are done as a separate commit. Hopefully this helps with the review.
1 parent e368675 commit 874f511

17 files changed

+1203
-1022
lines changed

clang/include/clang/Parse/Parser.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "clang/Lex/CodeCompletionHandler.h"
1919
#include "clang/Lex/Preprocessor.h"
2020
#include "clang/Sema/Sema.h"
21+
#include "clang/Sema/SemaCodeCompletion.h"
2122
#include "clang/Sema/SemaObjC.h"
2223
#include "clang/Sema/SemaOpenMP.h"
2324
#include "llvm/ADT/SmallVector.h"
@@ -2997,7 +2998,8 @@ class Parser : public CodeCompletionHandler {
29972998

29982999
IdentifierInfo *TryParseCXX11AttributeIdentifier(
29993000
SourceLocation &Loc,
3000-
Sema::AttributeCompletion Completion = Sema::AttributeCompletion::None,
3001+
SemaCodeCompletion::AttributeCompletion Completion =
3002+
SemaCodeCompletion::AttributeCompletion::None,
30013003
const IdentifierInfo *EnclosingScope = nullptr);
30023004

30033005
void MaybeParseHLSLAnnotations(Declarator &D,

clang/include/clang/Sema/Sema.h

Lines changed: 14 additions & 211 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ class Preprocessor;
167167
class PseudoDestructorTypeStorage;
168168
class PseudoObjectExpr;
169169
class QualType;
170+
class SemaCodeCompletion;
170171
class SemaCUDA;
171172
class SemaHLSL;
172173
class SemaObjC;
@@ -481,9 +482,8 @@ class Sema final : public SemaBase {
481482
// 29. C++ Variadic Templates (SemaTemplateVariadic.cpp)
482483
// 30. Constraints and Concepts (SemaConcept.cpp)
483484
// 31. Types (SemaType.cpp)
484-
// 32. Code Completion (SemaCodeComplete.cpp)
485-
// 33. FixIt Helpers (SemaFixItUtils.cpp)
486-
// 34. Name Lookup for RISC-V Vector Intrinsic (SemaRISCVVectorLookup.cpp)
485+
// 32. FixIt Helpers (SemaFixItUtils.cpp)
486+
// 33. Name Lookup for RISC-V Vector Intrinsic (SemaRISCVVectorLookup.cpp)
487487

488488
/// \name Semantic Analysis
489489
/// Implementations are in Sema.cpp
@@ -984,6 +984,11 @@ class Sema final : public SemaBase {
984984
/// CurContext - This is the current declaration context of parsing.
985985
DeclContext *CurContext;
986986

987+
SemaCodeCompletion &CodeCompletion() {
988+
assert(CodeCompletionPtr);
989+
return *CodeCompletionPtr;
990+
}
991+
987992
SemaCUDA &CUDA() {
988993
assert(CUDAPtr);
989994
return *CUDAPtr;
@@ -1044,6 +1049,7 @@ class Sema final : public SemaBase {
10441049

10451050
mutable IdentifierInfo *Ident_super;
10461051

1052+
std::unique_ptr<SemaCodeCompletion> CodeCompletionPtr;
10471053
std::unique_ptr<SemaCUDA> CUDAPtr;
10481054
std::unique_ptr<SemaHLSL> HLSLPtr;
10491055
std::unique_ptr<SemaObjC> ObjCPtr;
@@ -4801,6 +4807,11 @@ class Sema final : public SemaBase {
48014807
DelayedDiagnostics.popUndelayed(state);
48024808
}
48034809

4810+
ValueDecl *tryLookupCtorInitMemberDecl(CXXRecordDecl *ClassDecl,
4811+
CXXScopeSpec &SS,
4812+
ParsedType TemplateTypeTy,
4813+
IdentifierInfo *MemberOrBase);
4814+
48044815
private:
48054816
void setupImplicitSpecialMemberType(CXXMethodDecl *SpecialMem,
48064817
QualType ResultTy,
@@ -4811,11 +4822,6 @@ class Sema final : public SemaBase {
48114822
// of a ComparisonCategoryType enumerator.
48124823
llvm::SmallBitVector FullyCheckedComparisonCategories;
48134824

4814-
ValueDecl *tryLookupCtorInitMemberDecl(CXXRecordDecl *ClassDecl,
4815-
CXXScopeSpec &SS,
4816-
ParsedType TemplateTypeTy,
4817-
IdentifierInfo *MemberOrBase);
4818-
48194825
/// Check if there is a field shadowing.
48204826
void CheckShadowInheritedFields(const SourceLocation &Loc,
48214827
DeclarationName FieldName,
@@ -11664,209 +11670,6 @@ class Sema final : public SemaBase {
1166411670
//
1166511671
//
1166611672

11667-
/// \name Code Completion
11668-
/// Implementations are in SemaCodeComplete.cpp
11669-
///@{
11670-
11671-
public:
11672-
/// Code-completion consumer.
11673-
CodeCompleteConsumer *CodeCompleter;
11674-
11675-
/// Describes the context in which code completion occurs.
11676-
enum ParserCompletionContext {
11677-
/// Code completion occurs at top-level or namespace context.
11678-
PCC_Namespace,
11679-
/// Code completion occurs within a class, struct, or union.
11680-
PCC_Class,
11681-
/// Code completion occurs within an Objective-C interface, protocol,
11682-
/// or category.
11683-
PCC_ObjCInterface,
11684-
/// Code completion occurs within an Objective-C implementation or
11685-
/// category implementation
11686-
PCC_ObjCImplementation,
11687-
/// Code completion occurs within the list of instance variables
11688-
/// in an Objective-C interface, protocol, category, or implementation.
11689-
PCC_ObjCInstanceVariableList,
11690-
/// Code completion occurs following one or more template
11691-
/// headers.
11692-
PCC_Template,
11693-
/// Code completion occurs following one or more template
11694-
/// headers within a class.
11695-
PCC_MemberTemplate,
11696-
/// Code completion occurs within an expression.
11697-
PCC_Expression,
11698-
/// Code completion occurs within a statement, which may
11699-
/// also be an expression or a declaration.
11700-
PCC_Statement,
11701-
/// Code completion occurs at the beginning of the
11702-
/// initialization statement (or expression) in a for loop.
11703-
PCC_ForInit,
11704-
/// Code completion occurs within the condition of an if,
11705-
/// while, switch, or for statement.
11706-
PCC_Condition,
11707-
/// Code completion occurs within the body of a function on a
11708-
/// recovery path, where we do not have a specific handle on our position
11709-
/// in the grammar.
11710-
PCC_RecoveryInFunction,
11711-
/// Code completion occurs where only a type is permitted.
11712-
PCC_Type,
11713-
/// Code completion occurs in a parenthesized expression, which
11714-
/// might also be a type cast.
11715-
PCC_ParenthesizedExpression,
11716-
/// Code completion occurs within a sequence of declaration
11717-
/// specifiers within a function, method, or block.
11718-
PCC_LocalDeclarationSpecifiers,
11719-
/// Code completion occurs at top-level in a REPL session
11720-
PCC_TopLevelOrExpression,
11721-
};
11722-
11723-
void CodeCompleteModuleImport(SourceLocation ImportLoc, ModuleIdPath Path);
11724-
void CodeCompleteOrdinaryName(Scope *S,
11725-
ParserCompletionContext CompletionContext);
11726-
void CodeCompleteDeclSpec(Scope *S, DeclSpec &DS, bool AllowNonIdentifiers,
11727-
bool AllowNestedNameSpecifiers);
11728-
11729-
struct CodeCompleteExpressionData;
11730-
void CodeCompleteExpression(Scope *S, const CodeCompleteExpressionData &Data);
11731-
void CodeCompleteExpression(Scope *S, QualType PreferredType,
11732-
bool IsParenthesized = false);
11733-
void CodeCompleteMemberReferenceExpr(Scope *S, Expr *Base, Expr *OtherOpBase,
11734-
SourceLocation OpLoc, bool IsArrow,
11735-
bool IsBaseExprStatement,
11736-
QualType PreferredType);
11737-
void CodeCompletePostfixExpression(Scope *S, ExprResult LHS,
11738-
QualType PreferredType);
11739-
void CodeCompleteTag(Scope *S, unsigned TagSpec);
11740-
void CodeCompleteTypeQualifiers(DeclSpec &DS);
11741-
void CodeCompleteFunctionQualifiers(DeclSpec &DS, Declarator &D,
11742-
const VirtSpecifiers *VS = nullptr);
11743-
void CodeCompleteBracketDeclarator(Scope *S);
11744-
void CodeCompleteCase(Scope *S);
11745-
enum class AttributeCompletion {
11746-
Attribute,
11747-
Scope,
11748-
None,
11749-
};
11750-
void CodeCompleteAttribute(
11751-
AttributeCommonInfo::Syntax Syntax,
11752-
AttributeCompletion Completion = AttributeCompletion::Attribute,
11753-
const IdentifierInfo *Scope = nullptr);
11754-
/// Determines the preferred type of the current function argument, by
11755-
/// examining the signatures of all possible overloads.
11756-
/// Returns null if unknown or ambiguous, or if code completion is off.
11757-
///
11758-
/// If the code completion point has been reached, also reports the function
11759-
/// signatures that were considered.
11760-
///
11761-
/// FIXME: rename to GuessCallArgumentType to reduce confusion.
11762-
QualType ProduceCallSignatureHelp(Expr *Fn, ArrayRef<Expr *> Args,
11763-
SourceLocation OpenParLoc);
11764-
QualType ProduceConstructorSignatureHelp(QualType Type, SourceLocation Loc,
11765-
ArrayRef<Expr *> Args,
11766-
SourceLocation OpenParLoc,
11767-
bool Braced);
11768-
QualType ProduceCtorInitMemberSignatureHelp(
11769-
Decl *ConstructorDecl, CXXScopeSpec SS, ParsedType TemplateTypeTy,
11770-
ArrayRef<Expr *> ArgExprs, IdentifierInfo *II, SourceLocation OpenParLoc,
11771-
bool Braced);
11772-
QualType ProduceTemplateArgumentSignatureHelp(
11773-
TemplateTy, ArrayRef<ParsedTemplateArgument>, SourceLocation LAngleLoc);
11774-
void CodeCompleteInitializer(Scope *S, Decl *D);
11775-
/// Trigger code completion for a record of \p BaseType. \p InitExprs are
11776-
/// expressions in the initializer list seen so far and \p D is the current
11777-
/// Designation being parsed.
11778-
void CodeCompleteDesignator(const QualType BaseType,
11779-
llvm::ArrayRef<Expr *> InitExprs,
11780-
const Designation &D);
11781-
void CodeCompleteAfterIf(Scope *S, bool IsBracedThen);
11782-
11783-
void CodeCompleteQualifiedId(Scope *S, CXXScopeSpec &SS, bool EnteringContext,
11784-
bool IsUsingDeclaration, QualType BaseType,
11785-
QualType PreferredType);
11786-
void CodeCompleteUsing(Scope *S);
11787-
void CodeCompleteUsingDirective(Scope *S);
11788-
void CodeCompleteNamespaceDecl(Scope *S);
11789-
void CodeCompleteNamespaceAliasDecl(Scope *S);
11790-
void CodeCompleteOperatorName(Scope *S);
11791-
void CodeCompleteConstructorInitializer(
11792-
Decl *Constructor, ArrayRef<CXXCtorInitializer *> Initializers);
11793-
11794-
void CodeCompleteLambdaIntroducer(Scope *S, LambdaIntroducer &Intro,
11795-
bool AfterAmpersand);
11796-
void CodeCompleteAfterFunctionEquals(Declarator &D);
11797-
11798-
void CodeCompleteObjCAtDirective(Scope *S);
11799-
void CodeCompleteObjCAtVisibility(Scope *S);
11800-
void CodeCompleteObjCAtStatement(Scope *S);
11801-
void CodeCompleteObjCAtExpression(Scope *S);
11802-
void CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS);
11803-
void CodeCompleteObjCPropertyGetter(Scope *S);
11804-
void CodeCompleteObjCPropertySetter(Scope *S);
11805-
void CodeCompleteObjCPassingType(Scope *S, ObjCDeclSpec &DS,
11806-
bool IsParameter);
11807-
void CodeCompleteObjCMessageReceiver(Scope *S);
11808-
void CodeCompleteObjCSuperMessage(Scope *S, SourceLocation SuperLoc,
11809-
ArrayRef<const IdentifierInfo *> SelIdents,
11810-
bool AtArgumentExpression);
11811-
void CodeCompleteObjCClassMessage(Scope *S, ParsedType Receiver,
11812-
ArrayRef<const IdentifierInfo *> SelIdents,
11813-
bool AtArgumentExpression,
11814-
bool IsSuper = false);
11815-
void CodeCompleteObjCInstanceMessage(
11816-
Scope *S, Expr *Receiver, ArrayRef<const IdentifierInfo *> SelIdents,
11817-
bool AtArgumentExpression, ObjCInterfaceDecl *Super = nullptr);
11818-
void CodeCompleteObjCForCollection(Scope *S, DeclGroupPtrTy IterationVar);
11819-
void CodeCompleteObjCSelector(Scope *S,
11820-
ArrayRef<const IdentifierInfo *> SelIdents);
11821-
void
11822-
CodeCompleteObjCProtocolReferences(ArrayRef<IdentifierLocPair> Protocols);
11823-
void CodeCompleteObjCProtocolDecl(Scope *S);
11824-
void CodeCompleteObjCInterfaceDecl(Scope *S);
11825-
void CodeCompleteObjCClassForwardDecl(Scope *S);
11826-
void CodeCompleteObjCSuperclass(Scope *S, IdentifierInfo *ClassName,
11827-
SourceLocation ClassNameLoc);
11828-
void CodeCompleteObjCImplementationDecl(Scope *S);
11829-
void CodeCompleteObjCInterfaceCategory(Scope *S, IdentifierInfo *ClassName,
11830-
SourceLocation ClassNameLoc);
11831-
void CodeCompleteObjCImplementationCategory(Scope *S,
11832-
IdentifierInfo *ClassName,
11833-
SourceLocation ClassNameLoc);
11834-
void CodeCompleteObjCPropertyDefinition(Scope *S);
11835-
void CodeCompleteObjCPropertySynthesizeIvar(Scope *S,
11836-
IdentifierInfo *PropertyName);
11837-
void CodeCompleteObjCMethodDecl(Scope *S,
11838-
std::optional<bool> IsInstanceMethod,
11839-
ParsedType ReturnType);
11840-
void CodeCompleteObjCMethodDeclSelector(
11841-
Scope *S, bool IsInstanceMethod, bool AtParameterName,
11842-
ParsedType ReturnType, ArrayRef<const IdentifierInfo *> SelIdents);
11843-
void CodeCompleteObjCClassPropertyRefExpr(Scope *S,
11844-
const IdentifierInfo &ClassName,
11845-
SourceLocation ClassNameLoc,
11846-
bool IsBaseExprStatement);
11847-
void CodeCompletePreprocessorDirective(bool InConditional);
11848-
void CodeCompleteInPreprocessorConditionalExclusion(Scope *S);
11849-
void CodeCompletePreprocessorMacroName(bool IsDefinition);
11850-
void CodeCompletePreprocessorExpression();
11851-
void CodeCompletePreprocessorMacroArgument(Scope *S, IdentifierInfo *Macro,
11852-
MacroInfo *MacroInfo,
11853-
unsigned Argument);
11854-
void CodeCompleteIncludedFile(llvm::StringRef Dir, bool IsAngled);
11855-
void CodeCompleteNaturalLanguage();
11856-
void CodeCompleteAvailabilityPlatformName();
11857-
void
11858-
GatherGlobalCodeCompletions(CodeCompletionAllocator &Allocator,
11859-
CodeCompletionTUInfo &CCTUInfo,
11860-
SmallVectorImpl<CodeCompletionResult> &Results);
11861-
11862-
///@}
11863-
11864-
//
11865-
//
11866-
// -------------------------------------------------------------------------
11867-
//
11868-
//
11869-
1187011673
/// \name FixIt Helpers
1187111674
/// Implementations are in SemaFixItUtils.cpp
1187211675
///@{

0 commit comments

Comments
 (0)