Skip to content

Macro expansion not performed on pragma arguments when using -save-temps #19903

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

Open
llvmbot opened this issue Apr 23, 2014 · 2 comments
Open
Labels
bugzilla Issues migrated from bugzilla clang Clang issues not falling into any other category

Comments

@llvmbot
Copy link
Member

llvmbot commented Apr 23, 2014

Bugzilla Link 19529
Version 3.7
OS Linux
Attachments C test case
Reporter LLVM Bugzilla Contributor
CC @rnk

Extended Description

Consider the following C code:

#define PACK_SZ 1
#pragma pack(PACK_SZ)
struct s1 {
char a;
int b;
};
struct s1 myst;


When compiling it without '-save-temps',
the pragma is correctly handled:

clang pragma_pack.c -O2 -S -emit-llvm
cat pragma_pack.ll
[...]
%struct.s1 = type <{ i8, i32 }>
@​myst = common global %struct.s1 zeroinitializer, align 1
[...]


When compiling it with '-save-temps', the macro
is not expanded and the pragma is then ignored:

clang pragma_pack.c -O2 -S -emit-llvm -save-temps
pragma_pack.c:4:14: warning: unknown action for '#pragma pack' - ignored
#pragma pack(PACK_SZ)
^
1 warning generated.
cat
[...]
%struct.s1 = type { i8, i32 }
@​myst = common global %struct.s1 zeroinitializer, align 4
[...]

The problem has been reproduced with other pragmas.
The problem is present in CLANG 3.4, but likely in other version as well.

@rnk
Copy link
Collaborator

rnk commented Apr 25, 2014

See also #18950 , which I "fixed" by always expanding macros when -fms-extensions is set, because that's what MSVC does.

The problem is that, depending on the pragma, macro expansion may or may not occur. Only the parser knows. And, when we're preprocessing, the parser isn't around to tell us what to expand and what not to. We need to change the layering that the lexer knows how to treat each pragma.

@rnk
Copy link
Collaborator

rnk commented Apr 25, 2014

Note that gcc does not expand macros here, which explains clang's current behavior:

$ cat t.cpp
#include <stdio.h>
#define PACK_SZ 1
#pragma pack(PACK_SZ)
struct s1 {
char a;
int b;
};
struct s1 myst;
int main() {
printf("%d\n", sizeof(myst));
}

$ g++ t.cpp -o t.exe
t.cpp:3:14: warning: unknown action ‘PACK_SZ’ for ‘#pragma pack’ - ignored [-Wpragmas]
#pragma pack(PACK_SZ)
^
$ ./t.exe
8

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 9, 2021
nirvedhmeshram added a commit that referenced this issue Dec 12, 2024
Missed commiting clang-fomrat in
[#19903](#119039)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla clang Clang issues not falling into any other category
Projects
None yet
Development

No branches or pull requests

2 participants