From: Aaron Ballman Date: Sat, 3 Jan 2015 17:00:12 +0000 (+0000) Subject: Volatile reads are side-effecting operations, but in the general case of access throu... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=108b48c8b205f420c1a03c022144e280f77f3894;p=clang Volatile reads are side-effecting operations, but in the general case of access through a volatile-qualified type, we're not certain of the underlying object's side-effects on access. Treat volatile accesses as "maybe" instead of "definite" side effects for the purposes of warning on evaluations in an unevaluated context. No longer diagnose on idiomatic code like: int * volatile v; (void)sizeof(*v); git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@225116 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp index 818b60ca50..21e668f68f 100644 --- a/lib/AST/Expr.cpp +++ b/lib/AST/Expr.cpp @@ -3021,6 +3021,13 @@ bool Expr::HasSideEffects(const ASTContext &Ctx, case CXXReinterpretCastExprClass: case CXXConstCastExprClass: case CXXFunctionalCastExprClass: { + // While volatile reads are side-effecting in both C and C++, we treat them + // as having possible (not definite) side-effects. This allows idiomatic + // code to behave without warning, such as sizeof(*v) for a volatile- + // qualified pointer. + if (!IncludePossibleEffects) + break; + const CastExpr *CE = cast(this); if (CE->getCastKind() == CK_LValueToRValue && CE->getSubExpr()->getType().isVolatileQualified()) diff --git a/test/SemaCXX/warn-unused-value.cpp b/test/SemaCXX/warn-unused-value.cpp index 3be2844aba..efabd50630 100644 --- a/test/SemaCXX/warn-unused-value.cpp +++ b/test/SemaCXX/warn-unused-value.cpp @@ -90,9 +90,10 @@ void f() { Bad b; (void)typeid(b.f()); // expected-warning {{expression with side effects will be evaluated despite being used as an operand to 'typeid'}} - // A dereference of a volatile pointer is a side effecting operation, despite - // it being a reasonable operation. + // A dereference of a volatile pointer is a side effecting operation, however + // since it is idiomatic code, and the alternatives induce higher maintenance + // costs, it is allowed. int * volatile x; - (void)sizeof(*x); // expected-warning {{expression with side effects has no effect in an unevaluated context}} + (void)sizeof(*x); // Ok } }