Skip to content

__builtin_constant_p incorrect with undefined variables #130649

@kees

Description

@kees

Given this situation:

// -O2 -Wall
extern void knowable(void);
extern void unknowable(void);

void test(void)
{
    char buf[8];

    // The end of "buf" is undefined. That shouldn't make
    // __builtin_constant_p() undefined nor true, though.
    // It should just be false.
    if (__builtin_constant_p(buf[sizeof(buf)-1])) {
        knowable();
    } else {
    	unknowable();
    }
}

Clang is incorrectly resolving __builtin_constant_p() to true (or undefined) instead of false. GCC correctly resolves it to false (and emits a warning about using buf uninitialized).

Isolated from ClangBuiltLinux: ClangBuiltLinux/linux#2073

Providing an initializer to buf makes this go away (in the sense that now it is correctly "knowable"). GCC flips over to knowable():

    char buf[8] = "";

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions