]> granicus.if.org Git - clang/commitdiff
Volatile reads are side-effecting operations, but in the general case of access throu...
authorAaron Ballman <aaron@aaronballman.com>
Sat, 3 Jan 2015 17:00:12 +0000 (17:00 +0000)
committerAaron Ballman <aaron@aaronballman.com>
Sat, 3 Jan 2015 17:00:12 +0000 (17:00 +0000)
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

lib/AST/Expr.cpp
test/SemaCXX/warn-unused-value.cpp

index 818b60ca50bde9137393e6c0628391e22b6ed35e..21e668f68ffd6c080f797104e972c8c1d3c21b21 100644 (file)
@@ -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<CastExpr>(this);
     if (CE->getCastKind() == CK_LValueToRValue &&
         CE->getSubExpr()->getType().isVolatileQualified())
index 3be2844aba7724c9d63e0e2b0685128328047437..efabd506306814358e1ad79b9e6ee274397a5294 100644 (file)
@@ -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
 }
 }