Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use regular NULL as initializer for atomic function pointer in C #49563

Closed
mstorsjo opened this issue May 4, 2021 · 4 comments
Closed
Labels
bugzilla Issues migrated from bugzilla c11 clang:frontend Language frontend issues, e.g. anything involving "Sema" confirmed Verified by a second party rejects-valid

Comments

@mstorsjo
Copy link
Member

mstorsjo commented May 4, 2021

Bugzilla Link 50219
Version trunk
OS All
CC @dwblaikie,@DougGregor,@zygoloid

Extended Description

$ cat test-atomic.c
typedef void (fp)(void);
_Atomic fp ptr1 = ((void(
)(void))0);
_Atomic fp ptr2 = ((void*)0);
$ gcc -c test-atomic.c -Wall -Wextra
$ clang -c test-atomic.c
test-atomic.c:3:19: error: initializer element is not a compile-time constant
_Atomic fp ptr2 = ((void*)0);
^~~~~~~~~~
1 error generated.

When initiailizing an _Atomic function pointer, which is a pointer to a function, clang errors out when initializing it to the common NULL define (which often expands to ((void*)0) in C). Using 0 cast to the right function pointer type works.

GCC accepts the construct without warnings.

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 11, 2021
@AaronBallman AaronBallman added clang:frontend Language frontend issues, e.g. anything involving "Sema" rejects-valid confirmed Verified by a second party labels May 5, 2022
@llvmbot
Copy link
Collaborator

llvmbot commented May 5, 2022

@llvm/issue-subscribers-clang-frontend

@AaronBallman
Copy link
Collaborator

I can confirm that this is an issue. I looked into it briefly and the trouble is that when we go to check the assignment constraints, we first try the constraints on the non-atomic type and then tell the caller it was an atomic conversion. However, the non-atomic assignment creates an implicit bitcast that the constant expression evaluator thinks causes the initialization to be non-constant.

@gnaggnoyil
Copy link

gnaggnoyil commented Sep 23, 2022

Not sure if it's the same issue, but Clang C frontend emits a warning when the code tries to assign NULL to an _Atomic function pointer with -pedantic flags on.

The code:

#include <stddef.h>

int main(void){
    _Atomic(int (*)(char)) a = NULL;
    (void)a;
    return 0;
}

Compile with clang -std=c11 -pedantic:

source.c:4:28: warning: initializing '_Atomic(int (*)(char))' with an expression of type 'void *' converts between void pointer and function pointer [-Wpedantic]
    _Atomic(int (*)(char)) a = NULL;
                           ^   ~~~~

@AaronBallman
Copy link
Collaborator

Potential fix: https://reviews.llvm.org/D148730

Not sure if it's the same issue, but Clang C frontend emits a warning when the code tries to assign NULL to an _Atomic function pointer with -pedantic flags on.

I believe this is the same issue.

raisinware pushed a commit to raisinware/llvm-project that referenced this issue Jun 28, 2023
…er constant

The relevant language rule from C11 is 6.5.16.1p1: "the left operand is
an atomic, qualified, or unqualified pointer, and the right is a null
pointer constant; or". We correctly handled qualified or unqualified
pointer types, but failed to handle atomic-qualified pointer types. Now
we look through the atomic qualification before testing the constraint
requirements.

Fixes llvm#49563
Differential Revision: https://reviews.llvm.org/D148730

Signed-off-by: raisinware <102261580+raisinware@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla c11 clang:frontend Language frontend issues, e.g. anything involving "Sema" confirmed Verified by a second party rejects-valid
Projects
None yet
Development

No branches or pull requests

4 participants