llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.38k stars 12.15k forks source link

Macro expansion of # pragma directive differs from GCC. #112152

Open mrolle45 opened 1 month ago

mrolle45 commented 1 month ago

Clang expands macros in the remainder of a # pragma, but gcc does not.

This is important because the pragma will be interpreted by a downstream compiler (if clang is used as a front end), or written to a .i file with the -E command line switch. In either case, there is no way for macros to be expanded afterward. Hence, clang and gcc will produce different results.

Macro expansion is neither required nor prohibited in the C or C++ standard, so it would fall into the category of implementation-defined behavior.

clang is advertised as a complete replacement for gcc. Therefore, in order to keep true to this statement, clang SHOULD NOT expand macros.

I used gcc version 10 to test this. If gcc should change this behavior in a later version (which I consider unlikely), then clang should make a similar change, and provide a command line option to specify which behavior the user wants to use.

pinskia commented 1 month ago

If gcc should change this behavior in a later version (which I consider unlikely),

So for GCC it depends on which pragma really.

See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=18638, which shows GCC is doing the wrong thing for some pragma's even (to be compatible with Oracle/Solaris).

See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=37023 for which it was a bug where macros were not expanded (openmp for an example are required by the open mp standard) (fixed by https://gcc.gnu.org/legacy-ml/gcc-patches/2008-01/msg01145.html ).

This is not cut and dry as you think.

llvmbot commented 1 month ago

@llvm/issue-subscribers-clang-frontend

Author: None (mrolle45)

Clang expands macros in the remainder of a # pragma, but gcc does not. This is important because the pragma will be interpreted by a downstream compiler (if clang is used as a front end), or written to a .i file with the -E command line switch. In either case, there is no way for macros to be expanded afterward. Hence, clang and gcc will produce different results. Macro expansion is neither required nor prohibited in the C or C++ standard, so it would fall into the category of implementation-defined behavior. clang is advertised as a complete replacement for gcc. Therefore, in order to keep true to this statement, clang SHOULD NOT expand macros. I used gcc version 10 to test this. If gcc should change this behavior in a later version (which I consider unlikely), then clang should make a similar change, and provide a command line option to specify which behavior the user wants to use.