|
| 1 | +// Normally this file can't be compiled due to |
| 2 | +// nullptr cast in offset calculation |
| 3 | +// RUN: not %clang_cc1 -triple x86_64-pc-win32 -ast-dump %s -o - 2>&1 | FileCheck %s --check-prefix=AST-ORIG |
| 4 | +// Kernel variant works OK. |
| 5 | +// RUN: %clang_cc1 -triple x86_64-pc-win32 -fms-kernel -ast-dump %s -o - | FileCheck %s --check-prefix=AST-NEW |
| 6 | +// We use PseudoObjectExpr, so reconstruction of source from AST should work as expected |
| 7 | +// RUN: %clang_cc1 -triple x86_64-pc-win32 -fms-kernel -ast-print %s -o - | FileCheck %s --check-prefix=AST-PRINT |
| 8 | + |
| 9 | +// AST-ORIG: error: constexpr function never produces a constant expression [-Winvalid-constexpr] |
| 10 | +// AST-ORIG: FunctionDecl |
| 11 | +// AST-ORIG-NEXT: ParmVarDecl |
| 12 | +// AST-ORIG-NEXT: CompoundStmt |
| 13 | +// AST-ORIG-NEXT: ReturnStmt |
| 14 | +// AST-ORIG-NEXT: ParenExpr |
| 15 | +// AST-ORIG-NEXT: CStyleCastExpr |
| 16 | +// AST-ORIG-NEXT: ParenExpr |
| 17 | +// AST-ORIG-NEXT: BinaryOperator |
| 18 | +// AST-ORIG-NEXT: CStyleCastExpr |
| 19 | +// AST-ORIG-NEXT: ImplicitCastExpr |
| 20 | +// AST-ORIG-NEXT: ParenExpr |
| 21 | +// AST-ORIG-NEXT: DeclRefExpr |
| 22 | +// AST-ORIG-NEXT: CStyleCastExpr |
| 23 | +// AST-ORIG-NEXT: ParenExpr |
| 24 | +// AST-ORIG-NEXT: UnaryOperator |
| 25 | +// AST-ORIG-NEXT: MemberExpr |
| 26 | +// AST-ORIG-NEXT: ParenExpr |
| 27 | +// AST-ORIG-NEXT: CStyleCastExpr |
| 28 | +// AST-ORIG-NEXT: ImplicitCastExpr |
| 29 | +// AST-ORIG-NEXT: IntegerLiteral |
| 30 | + |
| 31 | +// AST-NEW: FunctionDecl |
| 32 | +// AST-NEW-NEXT: ParmVarDecl |
| 33 | +// AST-NEW-NEXT: CompoundStmt |
| 34 | +// AST-NEW-NEXT: ReturnStmt |
| 35 | +// AST-NEW-NEXT: ParenExpr |
| 36 | +// AST-NEW-NEXT: CStyleCastExpr |
| 37 | +// AST-NEW-NEXT: ParenExpr |
| 38 | +// AST-NEW-NEXT: BinaryOperator |
| 39 | +// AST-NEW-NEXT: CStyleCastExpr |
| 40 | +// AST-NEW-NEXT: ImplicitCastExpr |
| 41 | +// AST-NEW-NEXT: ParenExpr |
| 42 | +// AST-NEW-NEXT: DeclRefExpr |
| 43 | +// AST-NEW-NEXT: PseudoObjectExpr |
| 44 | +// AST-NEW-NEXT: CStyleCastExpr |
| 45 | +// AST-NEW-NEXT: ParenExpr |
| 46 | +// AST-NEW-NEXT: UnaryOperator |
| 47 | +// AST-NEW-NEXT: MemberExpr |
| 48 | +// AST-NEW-NEXT: ParenExpr |
| 49 | +// AST-NEW-NEXT: CStyleCastExpr |
| 50 | +// AST-NEW-NEXT: ImplicitCastExpr |
| 51 | +// AST-NEW-NEXT: IntegerLiteral |
| 52 | +// AST-NEW-NEXT: CStyleCastExpr |
| 53 | +// AST-NEW-NEXT: OffsetOfExpr |
| 54 | + |
| 55 | +// AST-PRINT: constexpr MyStruct *get_container_ub(int *p) { |
| 56 | +// AST-PRINT-NEXT: return ((MyStruct *)((PCHAR)(p) - (ULONG_PTR)(&((MyStruct *)0)->b))); |
| 57 | +// AST-PRINT-NEXT: } |
| 58 | + |
| 59 | +typedef char* PCHAR; |
| 60 | +typedef unsigned long long ULONG_PTR; |
| 61 | + |
| 62 | +#define CONTAINING_RECORD_UB(address, type, field) ((type *)( \ |
| 63 | + (PCHAR)(address) - (ULONG_PTR)(&((type *)0)->field))) |
| 64 | + |
| 65 | + |
| 66 | +struct MyStruct { int a; int b; int c; }; |
| 67 | + |
| 68 | +constexpr MyStruct* get_container_ub(int* p) { |
| 69 | + return CONTAINING_RECORD_UB(p, MyStruct, b); |
| 70 | +} |
| 71 | + |
0 commit comments