Skip to content

#undef fails to undefine commandline preprocessor directives #57954

@ryao

Description

@ryao

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"invalidResolved as invalid, i.e. not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions