Skip to content

Commit 95ce40b

Browse files
committed
[clang] Fix-it hint for ++this -> ++*this when deref is modifiable
Resolves #93066
1 parent 1430405 commit 95ce40b

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8777,6 +8777,9 @@ def err_typecheck_incomplete_type_not_modifiable_lvalue : Error<
87778777
def err_typecheck_lvalue_casts_not_supported : Error<
87788778
"assignment to cast is illegal, lvalue casts are not supported">;
87798779

8780+
def note_typecheck_expression_not_modifiable_lvalue : Note<
8781+
"dereference the pointer to modify">;
8782+
87808783
def err_typecheck_duplicate_vector_components_not_mlvalue : Error<
87818784
"vector is not assignable (contains duplicate components)">;
87828785
def err_block_decl_ref_not_modifiable_lvalue : Error<

clang/lib/Sema/SemaExpr.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13587,10 +13587,22 @@ static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) {
1358713587
SourceRange Assign;
1358813588
if (Loc != OrigLoc)
1358913589
Assign = SourceRange(OrigLoc, OrigLoc);
13590-
if (NeedType)
13590+
if (NeedType) {
1359113591
S.Diag(Loc, DiagID) << E->getType() << E->getSourceRange() << Assign;
13592-
else
13592+
} else {
13593+
ExprResult Deref;
13594+
unsigned FixitDiagID = 0;
13595+
{
13596+
Sema::TentativeAnalysisScope Trap(S);
13597+
Deref = S.ActOnUnaryOp(S.getCurScope(), Loc, tok::star, E);
13598+
}
1359313599
S.Diag(Loc, DiagID) << E->getSourceRange() << Assign;
13600+
if (Deref.isUsable() &&
13601+
Deref.get()->isModifiableLvalue(S.Context, &Loc) == Expr::MLV_Valid) {
13602+
FixitDiagID = diag::note_typecheck_expression_not_modifiable_lvalue;
13603+
S.Diag(Loc, FixitDiagID) << E->getSourceRange() << Assign;
13604+
}
13605+
}
1359413606
return true;
1359513607
}
1359613608

clang/test/Sema/debug-93066.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify %s
2+
3+
struct S {
4+
void f() {
5+
++this;
6+
// expected-error@-1 {{expression is not assignable}}
7+
// expected-note@-2 {{dereference the pointer to modify}}
8+
}
9+
10+
void g() const {
11+
++this;
12+
// expected-error@-1 {{expression is not assignable}}
13+
}
14+
};

0 commit comments

Comments
 (0)