42 lines · cpp
1// RUN: %clangxx_tysan -O0 %s -c -o %t.o2// RUN: %clangxx_tysan -O0 %s -DPMAIN -c -o %tm.o3// RUN: %clangxx_tysan -O0 %t.o %tm.o -o %t4// RUN: %run %t >%t.out 2>&15// RUN: FileCheck %s < %t.out6 7#include <iostream>8 9// This test demonstrates that the types from anonymous namespaces are10// different in different translation units (while the char* type is the same).11 12namespace {13struct X {14 X(int i, int j) : a(i), b(j) {}15 int a;16 int b;17};18} // namespace19 20#ifdef PMAIN21void foo(void *context, int i);22char fbyte(void *context);23 24int main() {25 X x(5, 6);26 foo((void *)&x, 8);27 std::cout << "fbyte: " << fbyte((void *)&x) << "\n";28}29#else30void foo(void *context, int i) {31 X *x = (X *)context;32 x->b = i;33 // CHECK: ERROR: TypeSanitizer: type-aliasing-violation34 // CHECK: WRITE of size 4 at {{.*}} with type int (in (anonymous namespace)::X at offset 4) accesses an existing object of type int (in (anonymous namespace)::X at offset 4)35 // CHECK: {{#0 0x.* in foo\(void\*, int\) .*anon-ns.cpp:}}[[@LINE-3]]36}37 38char fbyte(void *context) { return *(char *)context; }39#endif40 41// CHECK-NOT: ERROR: TypeSanitizer: type-aliasing-violation42