Skip to content

Commit 63ed15b

Browse files
CoTinkerhstk30-hw
authored andcommitted
[X86_64] fix empty structure vaarg in c++
SizeInBytes of empty structure is 0 in C, while 1 in C++. And empty structure argument of the function is ignored in X86_64 backend.As a result, the value of variable arguments in C++ is incorrect.
1 parent 4d46721 commit 63ed15b

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

clang/lib/CodeGen/Targets/X86.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2989,6 +2989,11 @@ static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF,
29892989
// an 8 byte boundary.
29902990

29912991
uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
2992+
2993+
if (isEmptyRecord(CGF.getContext(), Ty, true)) {
2994+
SizeInBytes = 0;
2995+
}
2996+
29922997
llvm::Value *Offset =
29932998
llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
29942999
overflow_arg_area = CGF.Builder.CreateGEP(CGF.Int8Ty, overflow_arg_area,

clang/test/CodeGen/X86/x86_64-vaarg.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %clang -xc++ -target x86_64-linux-gnu -emit-llvm -S -o - %s | FileCheck %s
2+
3+
struct Empty {};
4+
5+
struct Empty emptyvar;
6+
7+
void take_args(int a, ...) {
8+
// CHECK: %overflow_arg_area = load ptr, ptr %overflow_arg_area_p, align 8
9+
// CHECK-NEXT: %overflow_arg_area.next = getelementptr i8, ptr %overflow_arg_area, i32 0
10+
// CHECK-NEXT: store ptr %overflow_arg_area.next, ptr %overflow_arg_area_p, align 8
11+
__builtin_va_list l;
12+
__builtin_va_start(l, a);
13+
emptyvar = __builtin_va_arg(l, struct Empty);
14+
__builtin_va_end(l);
15+
}

0 commit comments

Comments
 (0)