[clang] [clang] Fix-it hint for `++this` -> `++*this` when deref is modifiable (PR #94159)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Jun 6 07:08:51 PDT 2024
================
@@ -13587,10 +13602,47 @@ static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) {
SourceRange Assign;
if (Loc != OrigLoc)
Assign = SourceRange(OrigLoc, OrigLoc);
- if (NeedType)
+ if (NeedType) {
S.Diag(Loc, DiagID) << E->getType() << E->getSourceRange() << Assign;
- else
+ } else {
+ ExprResult Deref;
+ {
+ Sema::TentativeAnalysisScope Trap(S);
+ Deref = S.ActOnUnaryOp(S.getCurScope(), Loc, tok::star, E);
+ }
S.Diag(Loc, DiagID) << E->getSourceRange() << Assign;
+ if (Deref.isUsable() &&
+ Deref.get()->isModifiableLvalue(S.Context, &Loc) == Expr::MLV_Valid) {
+ std::string exprType;
+ switch (E->getStmtClass()) {
+ case Stmt::CStyleCastExprClass: {
+ const auto *CSC = cast<CStyleCastExpr>(E);
+ exprType = CSC->getType().getAsString();
+ break;
+ }
+ case Stmt::CXXConstCastExprClass: {
+ const auto *CXXCCE = cast<CXXConstCastExpr>(E);
+ exprType = CXXCCE->getTypeAsWritten().getAsString();
+ break;
+ }
+ case Stmt::CXXReinterpretCastExprClass: {
+ const auto *CXXRCE = cast<CXXReinterpretCastExpr>(E);
+ exprType = CXXRCE->getTypeAsWritten().getAsString();
+ break;
+ }
+ case Stmt::CXXThisExprClass: {
+ exprType = "this";
+ break;
+ }
+ default: {
+ }
+ }
+ S.Diag(Loc, diag::note_typecheck_expression_not_modifiable_lvalue)
+ << E->getSourceRange() << Assign
+ << FixItHint::CreateInsertion(E->getBeginLoc(),
+ "*(" + exprType + ")");
+ }
----------------
Sirraide wrote:
Just `*` if that is enough (which it should be in most cases), otherwise `*(` and `)`, yeah. That’s usually how we do fix-it hints if you look at some of the other diagnostics that have them.
https://github.com/llvm/llvm-project/pull/94159
More information about the cfe-commits
mailing list