Debian Codegraph @

clang/test/CodeGenCXX/catch-nullptr-and-nonzero-offset-in-offsetof-idiom.cpp

raw
1 // NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py
2 // RUN: %clang_cc1 -x c++ -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s
3 // RUN: %clang_cc1 -x c++ -fsanitize=pointer-overflow -fno-sanitize-recover=pointer-overflow -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s
4
5 #include <stdint.h>
6
7 struct S {
8 int x, y;
9 };
10
11 // CHECK-LABEL: @_Z23get_offset_of_y_naivelyv(
12 // CHECK-NEXT: entry:
13 // CHECK-NEXT: ret i64 ptrtoint (ptr getelementptr ([[STRUCT_S:%.*]], ptr null, i32 0, i32 1) to i64)
14 //
15 uintptr_t get_offset_of_y_naively() {
16 return ((uintptr_t)(&(((S *)nullptr)->y)));
17 }
18
19 struct Empty {};
20
21 struct T {
22 int a;
23 S s;
24 [[no_unique_address]] Empty e1;
25 int b;
26 [[no_unique_address]] Empty e2;
27 };
28
29 // CHECK-LABEL: @_Z30get_offset_of_y_naively_nestedv(
30 // CHECK-NEXT: entry:
31 // CHECK-NEXT: ret i64 ptrtoint (ptr getelementptr ([[STRUCT_S:%.*]], ptr getelementptr ([[STRUCT_T:%.*]], ptr null, i32 0, i32 1), i32 0, i32 1) to i64)
32 //
33 uintptr_t get_offset_of_y_naively_nested() {
34 return ((uintptr_t)(&(((T *)nullptr)->s.y)));
35 }
36
37 // CHECK-LABEL: @_Z42get_offset_of_y_naively_nested_with_parensv(
38 // CHECK-NEXT: entry:
39 // CHECK-NEXT: ret i64 ptrtoint (ptr getelementptr ([[STRUCT_S:%.*]], ptr getelementptr ([[STRUCT_T:%.*]], ptr null, i32 0, i32 1), i32 0, i32 1) to i64)
40 //
41 uintptr_t get_offset_of_y_naively_nested_with_parens() {
42 return ((uintptr_t)(&((((T *)nullptr)->s).y)));
43 }
44
45 // CHECK-LABEL: @_Z26get_offset_of_zero_storagev(
46 // CHECK-NEXT: entry:
47 // CHECK-NEXT: ret i64 ptrtoint (ptr getelementptr (i8, ptr null, i64 16) to i64)
48 //
49 uintptr_t get_offset_of_zero_storage() {
50 return ((uintptr_t)(&(((T *)nullptr)->e2)));
51 }
52
53 namespace std { typedef decltype(__nullptr) nullptr_t; }
54 // CHECK-LABEL: @_Z29get_offset_of_y_integral_zerov(
55 // CHECK-NEXT: entry:
56 // CHECK-NEXT: ret i64 ptrtoint (ptr getelementptr ([[STRUCT_S:%.*]], ptr null, i32 0, i32 1) to i64)
57 //
58 uintptr_t get_offset_of_y_integral_zero() {
59 return ((uintptr_t)(&(((S *)0)->y)));
60 }
61
62 // CHECK-LABEL: @_Z37get_offset_of_y_integral_zero_voidptrv(
63 // CHECK-NEXT: entry:
64 // CHECK-NEXT: ret i64 ptrtoint (ptr getelementptr ([[STRUCT_S:%.*]], ptr null, i32 0, i32 1) to i64)
65 //
66 uintptr_t get_offset_of_y_integral_zero_voidptr() {
67 return ((uintptr_t)(&(((S *)(void*)0)->y)));
68 }
69
70 // CHECK-LABEL: @_Z25get_offset_of_y_nullptr_tv(
71 // CHECK-NEXT: entry:
72 // CHECK-NEXT: ret i64 ptrtoint (ptr getelementptr ([[STRUCT_S:%.*]], ptr null, i32 0, i32 1) to i64)
73 //
74 uintptr_t get_offset_of_y_nullptr_t() {
75 return ((uintptr_t)(&(((S *)std::nullptr_t{})->y)));
76 }
77
78 // CHECK-LABEL: @_Z32get_offset_of_y_nullptr_constantv(
79 // CHECK-NEXT: entry:
80 // CHECK-NEXT: [[NULL:%.*]] = alloca ptr, align 8
81 // CHECK-NEXT: store ptr null, ptr [[NULL]], align 8
82 // CHECK-NEXT: ret i64 ptrtoint (ptr getelementptr inbounds nuw ([[STRUCT_S:%.*]], ptr null, i32 0, i32 1) to i64)
83 //
84 uintptr_t get_offset_of_y_nullptr_constant() {
85 constexpr void *null = nullptr;
86 return ((uintptr_t)(&(((S *)null)->y)));
87 }
88
89 // CHECK-LABEL: @_Z27get_offset_of_y_via_builtinv(
90 // CHECK-NEXT: entry:
91 // CHECK-NEXT: ret i64 4
92 //
93 uintptr_t get_offset_of_y_via_builtin() {
94 return __builtin_offsetof(S, y);
95 }