Skip to content

Commit 28c2bdf

Browse files
committed
[AST] Record SourceLocation for TypoExpr.
Reviewers: sammccall Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D81008
1 parent 8948eab commit 28c2bdf

File tree

5 files changed

+16
-13
lines changed

5 files changed

+16
-13
lines changed

clang/include/clang/AST/Expr.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6152,8 +6152,12 @@ class AtomicExpr : public Expr {
61526152
/// TypoExpr - Internal placeholder for expressions where typo correction
61536153
/// still needs to be performed and/or an error diagnostic emitted.
61546154
class TypoExpr : public Expr {
6155+
// The location for the typo name.
6156+
SourceLocation TypoLoc;
6157+
61556158
public:
6156-
TypoExpr(QualType T) : Expr(TypoExprClass, T, VK_LValue, OK_Ordinary) {
6159+
TypoExpr(QualType T, SourceLocation TypoLoc)
6160+
: Expr(TypoExprClass, T, VK_LValue, OK_Ordinary), TypoLoc(TypoLoc) {
61576161
assert(T->isDependentType() && "TypoExpr given a non-dependent type");
61586162
setDependence(ExprDependence::TypeValueInstantiation |
61596163
ExprDependence::Error);
@@ -6166,8 +6170,8 @@ class TypoExpr : public Expr {
61666170
return const_child_range(const_child_iterator(), const_child_iterator());
61676171
}
61686172

6169-
SourceLocation getBeginLoc() const LLVM_READONLY { return SourceLocation(); }
6170-
SourceLocation getEndLoc() const LLVM_READONLY { return SourceLocation(); }
6173+
SourceLocation getBeginLoc() const LLVM_READONLY { return TypoLoc; }
6174+
SourceLocation getEndLoc() const LLVM_READONLY { return TypoLoc; }
61716175

61726176
static bool classof(const Stmt *T) {
61736177
return T->getStmtClass() == TypoExprClass;

clang/include/clang/Sema/Sema.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3695,7 +3695,7 @@ class Sema final {
36953695
/// Creates a new TypoExpr AST node.
36963696
TypoExpr *createDelayedTypo(std::unique_ptr<TypoCorrectionConsumer> TCC,
36973697
TypoDiagnosticGenerator TDG,
3698-
TypoRecoveryCallback TRC);
3698+
TypoRecoveryCallback TRC, SourceLocation TypoLoc);
36993699

37003700
// The set of known/encountered (unique, canonicalized) NamespaceDecls.
37013701
//

clang/lib/Sema/SemaExprCXX.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8307,8 +8307,6 @@ ExprResult Sema::ActOnFinishFullExpr(Expr *FE, SourceLocation CC,
83078307
if (FullExpr.isInvalid()) {
83088308
// Typo-correction fails, we rebuild the broken AST with the typos degraded
83098309
// to RecoveryExpr.
8310-
// FIXME: we lose source locations for RecoveryExpr, as TypoExpr doesn't
8311-
// track source locations.
83128310
struct TyposReplace : TreeTransform<TyposReplace> {
83138311
TyposReplace(Sema &SemaRef) : TreeTransform(SemaRef) {}
83148312
ExprResult TransformTypoExpr(TypoExpr *E) {

clang/lib/Sema/SemaLookup.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5167,9 +5167,9 @@ TypoExpr *Sema::CorrectTypoDelayed(
51675167
IdentifierInfo *Typo = TypoName.getName().getAsIdentifierInfo();
51685168
if (!ExternalTypo && ED > 0 && Typo->getName().size() / ED < 3)
51695169
return nullptr;
5170-
51715170
ExprEvalContexts.back().NumTypos++;
5172-
return createDelayedTypo(std::move(Consumer), std::move(TDG), std::move(TRC));
5171+
return createDelayedTypo(std::move(Consumer), std::move(TDG), std::move(TRC),
5172+
TypoName.getLoc());
51735173
}
51745174

51755175
void TypoCorrection::addCorrectionDecl(NamedDecl *CDecl) {
@@ -5481,9 +5481,10 @@ void Sema::diagnoseTypo(const TypoCorrection &Correction,
54815481

54825482
TypoExpr *Sema::createDelayedTypo(std::unique_ptr<TypoCorrectionConsumer> TCC,
54835483
TypoDiagnosticGenerator TDG,
5484-
TypoRecoveryCallback TRC) {
5484+
TypoRecoveryCallback TRC,
5485+
SourceLocation TypoLoc) {
54855486
assert(TCC && "createDelayedTypo requires a valid TypoCorrectionConsumer");
5486-
auto TE = new (Context) TypoExpr(Context.DependentTy);
5487+
auto TE = new (Context) TypoExpr(Context.DependentTy, TypoLoc);
54875488
auto &State = DelayedTypos[TE];
54885489
State.Consumer = std::move(TCC);
54895490
State.DiagHandler = std::move(TDG);

clang/test/AST/ast-dump-recovery.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ int invalid_call = some_func(123);
1313
void test_invalid_call(int s) {
1414
// CHECK: CallExpr {{.*}} '<dependent type>' contains-errors
1515
// CHECK-NEXT: |-UnresolvedLookupExpr {{.*}} 'some_func'
16-
// CHECK-NEXT: |-RecoveryExpr {{.*}} <<invalid sloc>>
17-
// CHECK-NEXT: `-BinaryOperator {{.*}} <<invalid sloc>, col:28>
18-
// CHECK-NEXT: |-RecoveryExpr {{.*}} <<invalid sloc>>
16+
// CHECK-NEXT: |-RecoveryExpr {{.*}} <col:13>
17+
// CHECK-NEXT: `-BinaryOperator {{.*}}
18+
// CHECK-NEXT: |-RecoveryExpr {{.*}}
1919
// CHECK-NEXT: `-IntegerLiteral {{.*}} <col:28> 'int' 1
2020
some_func(undef1, undef2+1);
2121

0 commit comments

Comments
 (0)