<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/57954>57954</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            #undef fails to undefine commandline preprocessor directives
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          ryao
      </td>
    </tr>
</table>

<pre>
    In OpenZFS, we define preprocessor directives to provide API depreciation warnings. These are set in an automake file like this:

```
AM_CPPFLAGS_NOCHECK += -D"strncpy(...)=__attribute__((deprecated(\"strncpy(3) is deprecated. Use strlcpy(3) instead!\"))) strncpy(__VA_ARGS__)"
```

That is actually the latest (proposed) deprecation warning. Due to portability issues, we initially wanted to provide an exception to the proposed deprecation policy in one of our test cases by doing `#undef`. This worked on GCC, but failed to work with Clang, where the `#undef` was ignored.

I made a minimal test case based on the depreciation warning:

```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#undef strncpy

int main()
{
        char buf[256];
        char hello[] = "hello world\n";

        strncpy(buf, hello, sizeof(buf));

        printf("%s", buf);

        return 0;
}
```


`gcc -D"strncpy(...)=__attribute__((deprecated(\"strncpy(3) is deprecated. Use strlcpy(3) instead\!\"))) strncpy(__VA_ARGS__)" hello.c` works fine.

`clang -D"strncpy(...)=__attribute__((deprecated(\"strncpy(3) is deprecated. Use strlcpy(3) instead\!\"))) strncpy(__VA_ARGS__)" hello.c` breaks.

This was discovered in openzfs/zfs#13876. We have since changed the code to avoid the need to bypass the deprecation warning, but I am filing this because I feel it is a bug.

This issue was detected by the FreeBSD system clang on FreeBSD stable/13 amd64 in our continuous integration infrastructure. It was reproduced on my local Gentoo Linux machine that uses Clang version 14.0.6.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzVVstu4zYU_Rp5czGCLdlOtPDCscdp0GknaKYt0I1BUVcWG5o0SCoZz9f3k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