Skip to content

Commit a92693d

Browse files
committed
[CodeCompletion] Don't track preferred types if code completion is disabled.
Some of this work isn't quite trivial. (As requested in D96058) Differential Revision: https://reviews.llvm.org/D98459
1 parent 1310c68 commit a92693d

File tree

5 files changed

+34
-17
lines changed

5 files changed

+34
-17
lines changed

clang/include/clang/Parse/Parser.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -941,8 +941,8 @@ class Parser : public CodeCompletionHandler {
941941
bool isActive;
942942

943943
public:
944-
explicit TentativeParsingAction(Parser& p) : P(p) {
945-
PrevPreferredType = P.PreferredType;
944+
explicit TentativeParsingAction(Parser &p)
945+
: P(p), PrevPreferredType(P.PreferredType) {
946946
PrevTok = P.Tok;
947947
PrevTentativelyDeclaredIdentifierCount =
948948
P.TentativelyDeclaredIdentifiers.size();

clang/include/clang/Sema/Sema.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -286,14 +286,13 @@ class FileNullabilityMap {
286286
}
287287
};
288288

289-
/// Keeps track of expected type during expression parsing. The type is tied to
290-
/// a particular token, all functions that update or consume the type take a
291-
/// start location of the token they are looking at as a parameter. This allows
292-
/// to avoid updating the type on hot paths in the parser.
289+
/// Tracks expected type during expression parsing, for use in code completion.
290+
/// The type is tied to a particular token, all functions that update or consume
291+
/// the type take a start location of the token they are looking at as a
292+
/// parameter. This avoids updating the type on hot paths in the parser.
293293
class PreferredTypeBuilder {
294294
public:
295-
PreferredTypeBuilder() = default;
296-
explicit PreferredTypeBuilder(QualType Type) : Type(Type) {}
295+
PreferredTypeBuilder(bool Enabled) : Enabled(Enabled) {}
297296

298297
void enterCondition(Sema &S, SourceLocation Tok);
299298
void enterReturn(Sema &S, SourceLocation Tok);
@@ -320,7 +319,7 @@ class PreferredTypeBuilder {
320319
void enterTypeCast(SourceLocation Tok, QualType CastType);
321320

322321
QualType get(SourceLocation Tok) const {
323-
if (Tok != ExpectedLoc)
322+
if (!Enabled || Tok != ExpectedLoc)
324323
return QualType();
325324
if (!Type.isNull())
326325
return Type;
@@ -330,6 +329,7 @@ class PreferredTypeBuilder {
330329
}
331330

332331
private:
332+
bool Enabled;
333333
/// Start position of a token for which we store expected type.
334334
SourceLocation ExpectedLoc;
335335
/// Expected type for a token starting at ExpectedLoc.

clang/lib/Parse/ParseInit.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,6 @@ static void CheckArrayDesignatorSyntax(Parser &P, SourceLocation Loc,
160160
/// \p CodeCompleteCB is called with Designation parsed so far.
161161
ExprResult Parser::ParseInitializerWithPotentialDesignator(
162162
DesignatorCompletionInfo DesignatorCompletion) {
163-
if (!getPreprocessor().isCodeCompletionEnabled())
164-
DesignatorCompletion.PreferredBaseType = QualType(); // skip field lookup
165-
166163
// If this is the old-style GNU extension:
167164
// designation ::= identifier ':'
168165
// Handle it as a field designator. Otherwise, this must be the start of a

clang/lib/Parse/Parser.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ IdentifierInfo *Parser::getSEHExceptKeyword() {
4949
}
5050

5151
Parser::Parser(Preprocessor &pp, Sema &actions, bool skipFunctionBodies)
52-
: PP(pp), Actions(actions), Diags(PP.getDiagnostics()),
53-
GreaterThanIsOperator(true), ColonIsSacred(false),
54-
InMessageExpression(false), TemplateParameterDepth(0),
55-
ParsingInObjCContainer(false) {
52+
: PP(pp), PreferredType(pp.isCodeCompletionEnabled()), Actions(actions),
53+
Diags(PP.getDiagnostics()), GreaterThanIsOperator(true),
54+
ColonIsSacred(false), InMessageExpression(false),
55+
TemplateParameterDepth(0), ParsingInObjCContainer(false) {
5656
SkipFunctionBodies = pp.isCodeCompletionEnabled() || skipFunctionBodies;
5757
Tok.startToken();
5858
Tok.setKind(tok::eof);

clang/lib/Sema/SemaCodeComplete.cpp

+21-1
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,8 @@ class ResultBuilder {
381381
} // namespace
382382

383383
void PreferredTypeBuilder::enterReturn(Sema &S, SourceLocation Tok) {
384+
if (!Enabled)
385+
return;
384386
if (isa<BlockDecl>(S.CurContext)) {
385387
if (sema::BlockScopeInfo *BSI = S.getCurBlock()) {
386388
ComputeType = nullptr;
@@ -399,6 +401,8 @@ void PreferredTypeBuilder::enterReturn(Sema &S, SourceLocation Tok) {
399401
}
400402

401403
void PreferredTypeBuilder::enterVariableInit(SourceLocation Tok, Decl *D) {
404+
if (!Enabled)
405+
return;
402406
auto *VD = llvm::dyn_cast_or_null<ValueDecl>(D);
403407
ComputeType = nullptr;
404408
Type = VD ? VD->getType() : QualType();
@@ -410,20 +414,26 @@ static QualType getDesignatedType(QualType BaseType, const Designation &Desig);
410414
void PreferredTypeBuilder::enterDesignatedInitializer(SourceLocation Tok,
411415
QualType BaseType,
412416
const Designation &D) {
417+
if (!Enabled)
418+
return;
413419
ComputeType = nullptr;
414420
Type = getDesignatedType(BaseType, D);
415421
ExpectedLoc = Tok;
416422
}
417423

418424
void PreferredTypeBuilder::enterFunctionArgument(
419425
SourceLocation Tok, llvm::function_ref<QualType()> ComputeType) {
426+
if (!Enabled)
427+
return;
420428
this->ComputeType = ComputeType;
421429
Type = QualType();
422430
ExpectedLoc = Tok;
423431
}
424432

425433
void PreferredTypeBuilder::enterParenExpr(SourceLocation Tok,
426434
SourceLocation LParLoc) {
435+
if (!Enabled)
436+
return;
427437
// expected type for parenthesized expression does not change.
428438
if (ExpectedLoc == LParLoc)
429439
ExpectedLoc = Tok;
@@ -541,14 +551,16 @@ static QualType getPreferredTypeOfUnaryArg(Sema &S, QualType ContextType,
541551

542552
void PreferredTypeBuilder::enterBinary(Sema &S, SourceLocation Tok, Expr *LHS,
543553
tok::TokenKind Op) {
554+
if (!Enabled)
555+
return;
544556
ComputeType = nullptr;
545557
Type = getPreferredTypeOfBinaryRHS(S, LHS, Op);
546558
ExpectedLoc = Tok;
547559
}
548560

549561
void PreferredTypeBuilder::enterMemAccess(Sema &S, SourceLocation Tok,
550562
Expr *Base) {
551-
if (!Base)
563+
if (!Enabled || !Base)
552564
return;
553565
// Do we have expected type for Base?
554566
if (ExpectedLoc != Base->getBeginLoc())
@@ -561,26 +573,34 @@ void PreferredTypeBuilder::enterMemAccess(Sema &S, SourceLocation Tok,
561573
void PreferredTypeBuilder::enterUnary(Sema &S, SourceLocation Tok,
562574
tok::TokenKind OpKind,
563575
SourceLocation OpLoc) {
576+
if (!Enabled)
577+
return;
564578
ComputeType = nullptr;
565579
Type = getPreferredTypeOfUnaryArg(S, this->get(OpLoc), OpKind);
566580
ExpectedLoc = Tok;
567581
}
568582

569583
void PreferredTypeBuilder::enterSubscript(Sema &S, SourceLocation Tok,
570584
Expr *LHS) {
585+
if (!Enabled)
586+
return;
571587
ComputeType = nullptr;
572588
Type = S.getASTContext().IntTy;
573589
ExpectedLoc = Tok;
574590
}
575591

576592
void PreferredTypeBuilder::enterTypeCast(SourceLocation Tok,
577593
QualType CastType) {
594+
if (!Enabled)
595+
return;
578596
ComputeType = nullptr;
579597
Type = !CastType.isNull() ? CastType.getCanonicalType() : QualType();
580598
ExpectedLoc = Tok;
581599
}
582600

583601
void PreferredTypeBuilder::enterCondition(Sema &S, SourceLocation Tok) {
602+
if (!Enabled)
603+
return;
584604
ComputeType = nullptr;
585605
Type = S.getASTContext().BoolTy;
586606
ExpectedLoc = Tok;

0 commit comments

Comments
 (0)