[Clang] Fix an unused-but-set-variable warning with volatile variable
For the following code, void test() { volatile int j = 0; for (int i = 0; i < 1000; i++) j += 1; return; } If compiled with clang -g -Wall -Werror -S -emit-llvm test.c we will see the following error: test.c:2:6: error: variable 'j' set but not used [-Werror,-Wunused-but-set-variable] volatile int j = 0; ^ This is not quite right since 'j' is indeed used due to '+=' operator. gcc doesn't emit error either in this case. Also if we change 'j += 1' to 'j++', the warning will disappear with latest clang. Note that clang will issue the warning if the volatile declaration involves only simple assignment (var = ...). To fix the issue, in function MaybeDecrementCount(), if the operator is a compound assignment (i.e., +=, -=, etc.) and the variable is volatile, the count for RefsMinusAssignments will be decremented, similar to 'j++' case. Differential Revision: https://reviews.llvm.org/D121715
Loading
Please sign in to comment