<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Variable is incorrectly marked as "set but not used" when used with the __cleanup__() attribute"
   href="https://bugs.llvm.org/show_bug.cgi?id=51865">51865</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Variable is incorrectly marked as "set but not used" when used with the __cleanup__() attribute
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Frontend
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>frantisek@sumsal.cz
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Hello,

Yesterday I came across a possible issue in clang-13 when trying to rebuild[0]
the systemd codebase with one of the nightly[1] LLVM builds. After minimizing
the reproducer, the issue stems from a fact that clang incorrectly marks a
variable as "set but not used" if the variable is used only from the
__cleanup__() function:

Reproducer:
#include <stdio.h>

static void my_cleanup(char **test) {
    if (test)
        printf("my_cleanup: %s\n", *test);
}

int main(void) {
    __attribute__((__cleanup__(my_cleanup))) char *x = NULL;

    puts("Hello");
    x = "test";
    puts("World");

    return 0;
}

Expected behavior (clang-12):
# clang --version
clang version 12.0.1 (Fedora 12.0.1-1.fc34)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /bin
# clang -Wall -Wextra -Werror -o test test.c

Unexpected behavior (clang-13):
# clang-13 --version
Ubuntu clang version
13.0.0-++20210914062619+f6b09e394a5f-1~exp1~20210914043438.103
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
# clang-13 -Wall -Wextra -Werror -o test test.c
test.c:9:52: error: variable 'x' set but not used
[-Werror,-Wunused-but-set-variable]
    __attribute__((__cleanup__(my_cleanup))) char *x = NULL;
                                                   ^
1 error generated.


[0] <a href="https://github.com/systemd/systemd/pull/20738">https://github.com/systemd/systemd/pull/20738</a>
[1] <a href="https://apt.llvm.org/">https://apt.llvm.org/</a></pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>