[all-commits] [llvm/llvm-project] bdf69f: [Clang] Fix an unused-but-set-variable warning wit...

yonghong-song via All-commits all-commits at lists.llvm.org
Mon Mar 21 14:59:24 PDT 2022


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: bdf69f63df2cf4c9e3cd1af5cfcd503bc8e2869b
      https://github.com/llvm/llvm-project/commit/bdf69f63df2cf4c9e3cd1af5cfcd503bc8e2869b
  Author: Yonghong Song <yhs at fb.com>
  Date:   2022-03-21 (Mon, 21 Mar 2022)

  Changed paths:
    M clang/lib/Sema/SemaExprCXX.cpp
    M clang/test/Sema/warn-unused-but-set-variables.c

  Log Message:
  -----------
  [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




More information about the All-commits mailing list