static void MaybeDecrementCount(
Expr *E, llvm::DenseMap<const VarDecl *, int> &RefsMinusAssignments) {
DeclRefExpr *LHS = nullptr;
+ bool IsCompoundAssign = false;
if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
if (BO->getLHS()->getType()->isDependentType() ||
BO->getRHS()->getType()->isDependentType()) {
return;
} else if (!BO->isAssignmentOp())
return;
+ else
+ IsCompoundAssign = BO->isCompoundAssignmentOp();
LHS = dyn_cast<DeclRefExpr>(BO->getLHS());
} else if (CXXOperatorCallExpr *COCE = dyn_cast<CXXOperatorCallExpr>(E)) {
if (COCE->getOperator() != OO_Equal)
VarDecl *VD = dyn_cast<VarDecl>(LHS->getDecl());
if (!VD)
return;
+ // Don't decrement RefsMinusAssignments if volatile variable with compound
+ // assignment (+=, ...) to avoid potential unused-but-set-variable warning.
+ if (IsCompoundAssign && VD->getType().isVolatileQualified())
+ return;
auto iter = RefsMinusAssignments.find(VD);
if (iter == RefsMinusAssignments.end())
return;
int a;
w = (a = 0);
+ int j = 0; // expected-warning{{variable 'j' set but not used}}
+ for (int i = 0; i < 1000; i++)
+ j += 1;
+
// Following gcc, warn for a volatile variable.
volatile int b; // expected-warning{{variable 'b' set but not used}}
b = 0;
+ // volatile variable k is used, no warning.
+ volatile int k = 0;
+ for (int i = 0; i < 1000; i++)
+ k += 1;
+
+ // typedef of volatile type, no warning.
+ typedef volatile int volint;
+ volint l = 0;
+ l += 1;
+
int x;
x = 0;
return x;