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

passing a pointer as an enumerated must be an error? #59858

Closed
jemarch opened this issue Jan 6, 2023 · 2 comments
Closed

passing a pointer as an enumerated must be an error? #59858

jemarch opened this issue Jan 6, 2023 · 2 comments
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer

Comments

@jemarch
Copy link

jemarch commented Jan 6, 2023

The program below:

$ cat enumcast.c
enum E {
  E_FOO = 0,
  E_BAR = 1,
};

int foo_enum (enum E e);

int bar_enum (enum E e) {
  return foo_enum ((void *) e);
}

int foo_int (int x);

int bar_int (int x) {
  return foo_int ((void *) x);
}

Triggers an error in GCC, but only a warning in Clang/LLVM.

GCC:

$ gcc -c enumcast.c -o enumcast.o
enumcast.c: In function ‘bar_enum’:
enumcast.c:10:20: error: incompatible type for argument 1 of ‘foo_enum’
   10 |   return foo_enum ((void *) e);
      |                    ^~~~~~~~~~
      |                    |
      |                    void *
enumcast.c:7:22: note: expected ‘enum E’ but argument is of type ‘void *’
    7 | int foo_enum (enum E e);
      |               ~~~~~~~^
enumcast.c: In function ‘bar_int’:
enumcast.c:16:19: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
   16 |   return foo_int ((void *) x);
      |                   ^
enumcast.c:16:19: warning: passing argument 1 of ‘foo_int’ makes integer from pointer without a cast [-Wint-conversion]
   16 |   return foo_int ((void *) x);
      |                   ^~~~~~~~~~
      |                   |
      |                   void *
enumcast.c:13:18: note: expected ‘int’ but argument is of type ‘void *’
   13 | int foo_int (int x);
      |              ~~~~^

Clang:

$ clang -c enumcast.c -o enumcast.o
enumcast.c:10:20: warning: incompatible pointer to integer conversion passing 'void *' to parameter of type 'enum E' [-Wint-conversion]
  return foo_enum ((void *) e);
                   ^~~~~~~~~~
enumcast.c:7:22: note: passing argument to parameter 'e' here
int foo_enum (enum E e);
                     ^
enumcast.c:16:19: warning: cast to 'void *' from smaller integer type 'int' [-Wint-to-void-pointer-cast]
  return foo_int ((void *) x);
                  ^~~~~~~~~~
enumcast.c:16:19: warning: incompatible pointer to integer conversion passing 'void *' to parameter of type 'int' [-Wint-conversion]
  return foo_int ((void *) x);
                  ^~~~~~~~~~
enumcast.c:13:18: note: passing argument to parameter 'x' here
int foo_int (int x);
                 ^
3 warnings generated.

It would be good to have the same behavior in both compilers.

Would clang consider to turn that warning into an error? It seems to me that it makes very little sense to pass pointers as enumerated values...

The related GCC bugzilla is https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107843

@EugeneZelenko EugeneZelenko added clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer and removed new issue labels Jan 6, 2023
@shafik
Copy link
Collaborator

shafik commented Jan 10, 2023

it looks like in clang trunk and clang 15 this is an error: https://godbolt.org/z/xz5aKsPha

@shafik shafik closed this as completed Jan 10, 2023
@jemarch
Copy link
Author

jemarch commented Jan 10, 2023

Ok, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer
Projects
None yet
Development

No branches or pull requests

3 participants