From f92f6454efbd2b6cacbc563ffa131e165eba06a0 Mon Sep 17 00:00:00 2001 From: Eli Friedman Date: Thu, 24 May 2012 21:05:41 +0000 Subject: [PATCH] A minor tweak to the new volatile lvalue warning: don't warn on "(void)x", where "x" refers to a local variable. This should silence a useless warning in compiler-rt and other places. llvm-svn: 157414 --- clang/lib/AST/Expr.cpp | 47 ++++++++++++++++++++----------------------- clang/test/SemaCXX/unused.cpp | 2 ++ 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp index 729030b..111f338 100644 --- a/clang/lib/AST/Expr.cpp +++ b/clang/lib/AST/Expr.cpp @@ -1883,43 +1883,40 @@ bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc, return true; } case CStyleCastExprClass: { - // Ignore an explicit cast to void, as long as the operand isn't a + // Ignore an explicit cast to void unless the operand is a non-trivial // volatile lvalue. - const CStyleCastExpr *CE = cast(this); - if (CE->getCastKind() == CK_ToVoid) { - if (CE->getSubExpr()->isGLValue() && - CE->getSubExpr()->getType().isVolatileQualified()) - return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, - R1, R2, Ctx); - return false; - } - WarnE = this; - Loc = CE->getLParenLoc(); - R1 = CE->getSubExpr()->getSourceRange(); - return true; - } - case CXXFunctionalCastExprClass: { - // Ignore an explicit cast to void, as long as the operand isn't a - // volatile lvalue. - const CXXFunctionalCastExpr *CE = cast(this); + const CastExpr *CE = cast(this); if (CE->getCastKind() == CK_ToVoid) { if (CE->getSubExpr()->isGLValue() && - CE->getSubExpr()->getType().isVolatileQualified()) - return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, - R1, R2, Ctx); + CE->getSubExpr()->getType().isVolatileQualified()) { + const DeclRefExpr *DRE = + dyn_cast(CE->getSubExpr()->IgnoreParens()); + if (!(DRE && isa(DRE->getDecl()) && + cast(DRE->getDecl())->hasLocalStorage())) { + return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, + R1, R2, Ctx); + } + } return false; } - + // If this is a cast to a constructor conversion, check the operand. // Otherwise, the result of the cast is unused. if (CE->getCastKind() == CK_ConstructorConversion) return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx); + WarnE = this; - Loc = CE->getTypeBeginLoc(); - R1 = CE->getSubExpr()->getSourceRange(); + if (const CXXFunctionalCastExpr *CXXCE = + dyn_cast(this)) { + Loc = CXXCE->getTypeBeginLoc(); + R1 = CXXCE->getSubExpr()->getSourceRange(); + } else { + const CStyleCastExpr *CStyleCE = cast(this); + Loc = CStyleCE->getLParenLoc(); + R1 = CStyleCE->getSubExpr()->getSourceRange(); + } return true; } - case ImplicitCastExprClass: { const CastExpr *ICE = cast(this); diff --git a/clang/test/SemaCXX/unused.cpp b/clang/test/SemaCXX/unused.cpp index b330d6e..54898c8 100644 --- a/clang/test/SemaCXX/unused.cpp +++ b/clang/test/SemaCXX/unused.cpp @@ -30,5 +30,7 @@ namespace derefvolatile { void f(volatile char* x) { *x; // expected-warning {{expression result unused; assign into a variable to force a volatile load}} (void)*x; // expected-warning {{expression result unused; assign into a variable to force a volatile load}} + volatile char y = 10; + (void)y; // don't warn here, because it's a common pattern. } } -- 2.7.4