diff options
| author | Roman Lebedev <lebedev.ri@gmail.com> | 2018-04-07 10:39:21 +0000 |
|---|---|---|
| committer | Roman Lebedev <lebedev.ri@gmail.com> | 2018-04-07 10:39:21 +0000 |
| commit | 61061d69eacfb546283f33cba053c29bb1c1ef7e (patch) | |
| tree | 671a06ad0b749490526e6b057db7c7d772d57d90 /clang/lib/Sema/SemaExpr.cpp | |
| parent | 41922f1a6d153e4754bde2ef3a3662d578dfdc69 (diff) | |
| download | bcm5719-llvm-61061d69eacfb546283f33cba053c29bb1c1ef7e.tar.gz bcm5719-llvm-61061d69eacfb546283f33cba053c29bb1c1ef7e.zip | |
[Sema] Extend -Wself-assign and -Wself-assign-field to warn on overloaded self-assignment (classes)
Summary:
This has just bit me, so i though it would be nice to avoid that next time :)
Motivational case:
https://godbolt.org/g/cq9UNk
Basically, it's likely to happen if you don't like shadowing issues,
and use `-Wshadow` and friends. And it won't be diagnosed by clang.
The reason is, these self-assign diagnostics only work for builtin assignment
operators. Which makes sense, one could have a very special operator=,
that does something unusual in case of self-assignment,
so it may make sense to not warn on that.
But while it may be intentional in some cases, it may be a bug in other cases,
so it would be really great to have some diagnostic about it...
Reviewers: aaron.ballman, rsmith, rtrieu, nikola, rjmccall, dblaikie
Reviewed By: rjmccall
Subscribers: EricWF, lebedev.ri, thakis, Quuxplusone, cfe-commits
Differential Revision: https://reviews.llvm.org/D44883
llvm-svn: 329493
Diffstat (limited to 'clang/lib/Sema/SemaExpr.cpp')
| -rw-r--r-- | clang/lib/Sema/SemaExpr.cpp | 48 |
1 files changed, 43 insertions, 5 deletions
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 79f422c0026..71cbd7f7776 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -10701,12 +10701,34 @@ static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { static void CheckIdentityFieldAssignment(Expr *LHSExpr, Expr *RHSExpr, SourceLocation Loc, Sema &Sema) { + if (Sema.inTemplateInstantiation()) + return; + if (Sema.isUnevaluatedContext()) + return; + if (Loc.isInvalid() || Loc.isMacroID()) + return; + if (LHSExpr->getExprLoc().isMacroID() || RHSExpr->getExprLoc().isMacroID()) + return; + // C / C++ fields MemberExpr *ML = dyn_cast<MemberExpr>(LHSExpr); MemberExpr *MR = dyn_cast<MemberExpr>(RHSExpr); - if (ML && MR && ML->getMemberDecl() == MR->getMemberDecl()) { - if (isa<CXXThisExpr>(ML->getBase()) && isa<CXXThisExpr>(MR->getBase())) - Sema.Diag(Loc, diag::warn_identity_field_assign) << 0; + if (ML && MR) { + if (!(isa<CXXThisExpr>(ML->getBase()) && isa<CXXThisExpr>(MR->getBase()))) + return; + const ValueDecl *LHSDecl = + cast<ValueDecl>(ML->getMemberDecl()->getCanonicalDecl()); + const ValueDecl *RHSDecl = + cast<ValueDecl>(MR->getMemberDecl()->getCanonicalDecl()); + if (LHSDecl != RHSDecl) + return; + if (LHSDecl->getType().isVolatileQualified()) + return; + if (const ReferenceType *RefTy = LHSDecl->getType()->getAs<ReferenceType>()) + if (RefTy->getPointeeType().isVolatileQualified()) + return; + + Sema.Diag(Loc, diag::warn_identity_field_assign) << 0; } // Objective-C instance variables @@ -11460,12 +11482,13 @@ static inline UnaryOperatorKind ConvertTokenKindToUnaryOpcode( } /// DiagnoseSelfAssignment - Emits a warning if a value is assigned to itself. -/// This warning is only emitted for builtin assignment operations. It is also -/// suppressed in the event of macro expansions. +/// This warning suppressed in the event of macro expansions. static void DiagnoseSelfAssignment(Sema &S, Expr *LHSExpr, Expr *RHSExpr, SourceLocation OpLoc) { if (S.inTemplateInstantiation()) return; + if (S.isUnevaluatedContext()) + return; if (OpLoc.isInvalid() || OpLoc.isMacroID()) return; LHSExpr = LHSExpr->IgnoreParenImpCasts(); @@ -12080,6 +12103,21 @@ ExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, static ExprResult BuildOverloadedBinOp(Sema &S, Scope *Sc, SourceLocation OpLoc, BinaryOperatorKind Opc, Expr *LHS, Expr *RHS) { + switch (Opc) { + case BO_Assign: + case BO_DivAssign: + case BO_RemAssign: + case BO_SubAssign: + case BO_AndAssign: + case BO_OrAssign: + case BO_XorAssign: + DiagnoseSelfAssignment(S, LHS, RHS, OpLoc); + CheckIdentityFieldAssignment(LHS, RHS, OpLoc, S); + break; + default: + break; + } + // Find all of the overloaded operators visible from this // point. We perform both an operator-name lookup from the local // scope and an argument-dependent lookup based on the types of |

