xref: /llvm-project/clang/test/CodeGenCXX/catch-nullptr-and-nonzero-offset-in-offsetof-idiom.cpp (revision 94473f4db6a6f5f12d7c4081455b5b596094eac5)
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 inbounds nuw ([[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 // CHECK-LABEL: @_Z27get_offset_of_y_via_builtinv(
20 // CHECK-NEXT:  entry:
21 // CHECK-NEXT:    ret i64 4
22 //
23 uintptr_t get_offset_of_y_via_builtin() {
24   return __builtin_offsetof(S, y);
25 }
26