Unnamed: 0
int64
0
0
repo_id
stringlengths
5
186
file_path
stringlengths
15
223
content
stringlengths
1
32.8M
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/vla.cpp
// RUN: %clang_cc1 -verify %s // PR11925 int n; int (&f())[n]; // expected-error {{function declaration cannot have variably modified type}} namespace PR18581 { template<typename T> struct pod {}; template<typename T> struct error { typename T::error e; // expected-error {{cannot be used prior to '::'}} }; struct incomplete; // expected-note {{forward declaration}} void f(int n) { pod<int> a[n]; error<int> b[n]; // expected-note {{instantiation}} incomplete c[n]; // expected-error {{incomplete}} } } void pr23151(int (&)[*]) { // expected-error {{variable length array must be bound in function definition}} }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/type-convert-construct.cpp
// RUN: %clang_cc1 -fsyntax-only -verify %s void f() { float v1 = float(1); int v2 = typeof(int)(1,2); // expected-error {{excess elements in scalar initializer}} typedef int arr[]; int v3 = arr(); // expected-error {{array types cannot be value-initialized}} int v4 = int(); int v5 = int; // expected-error {{expected '(' for function-style cast or type construction}} typedef int T; int *p; bool v6 = T(0) == p; char *str; str = "a string"; // expected-warning{{conversion from string literal to 'char *' is deprecated}} wchar_t *wstr; wstr = L"a wide string"; // expected-warning{{conversion from string literal to 'wchar_t *' is deprecated}} }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/PR10447.cpp
// RUN: %clang_cc1 -verify %s // expected-no-diagnostics // PR12223 namespace test1 { namespace N { extern "C" void f_test1(struct S*); void g(S*); } namespace N { void f(struct S *s) { g(s); } } } // PR10447 namespace test2 { extern "C" { void f_test2(struct Bar*) { } test2::Bar *ptr; } }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/libstdcxx_explicit_init_list_hack.cpp
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify -Wsystem-headers %s // libstdc++4.6 in debug mode has explicit default constructors. // stlport has this for all containers. #ifdef BE_THE_HEADER #pragma clang system_header namespace std { namespace __debug { template <class T> class vector { public: explicit vector() {} // expected-warning{{should not be explicit}} }; } } #else #define BE_THE_HEADER #include __FILE__ struct { int a, b; std::__debug::vector<int> c; } e[] = { {1, 1} }; // expected-note{{used in initialization here}} #endif
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/warn-redundant-move.cpp
// RUN: %clang_cc1 -fsyntax-only -Wredundant-move -std=c++11 -verify %s // RUN: %clang_cc1 -fsyntax-only -Wredundant-move -std=c++11 -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s // RUN: %clang_cc1 -fsyntax-only -std=c++11 %s -ast-dump | FileCheck %s --check-prefix=CHECK-AST // definitions for std::move namespace std { inline namespace foo { template <class T> struct remove_reference { typedef T type; }; template <class T> struct remove_reference<T&> { typedef T type; }; template <class T> struct remove_reference<T&&> { typedef T type; }; template <class T> typename remove_reference<T>::type &&move(T &&t); } } // test1 and test2 should not warn until after implementation of DR1579. struct A {}; struct B : public A {}; A test1(B b1) { B b2; return b1; return b2; return std::move(b1); return std::move(b2); } struct C { C() {} C(A) {} }; C test2(A a1, B b1) { A a2; B b2; return a1; return a2; return b1; return b2; return std::move(a1); return std::move(a2); return std::move(b1); return std::move(b2); } // Copy of tests above with types changed to reference types. A test3(B& b1) { B& b2 = b1; return b1; return b2; return std::move(b1); return std::move(b2); } C test4(A& a1, B& b1) { A& a2 = a1; B& b2 = b1; return a1; return a2; return b1; return b2; return std::move(a1); return std::move(a2); return std::move(b1); return std::move(b2); } // PR23819, case 2 struct D {}; D test5(D d) { return d; // Verify the implicit move from the AST dump // CHECK-AST: ReturnStmt{{.*}}line:[[@LINE-2]] // CHECK-AST-NEXT: CXXConstructExpr{{.*}}struct D{{.*}}void (struct D &&) // CHECK-AST-NEXT: ImplicitCastExpr // CHECK-AST-NEXT: DeclRefExpr{{.*}}ParmVar{{.*}}'d' return std::move(d); // expected-warning@-1{{redundant move in return statement}} // expected-note@-2{{remove std::move call here}} // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:20}:"" // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:21-[[@LINE-4]]:22}:"" } namespace templates { struct A {}; struct B { B(A); }; // Warn once here since the type is not dependent. template <typename T> A test1(A a) { return std::move(a); // expected-warning@-1{{redundant move in return statement}} // expected-note@-2{{remove std::move call here}} // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:12-[[@LINE-3]]:22}:"" // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:23-[[@LINE-4]]:24}:"" } void run_test1() { test1<A>(A()); test1<B>(A()); } // T1 and T2 may not be the same, the warning may not always apply. template <typename T1, typename T2> T1 test2(T2 t) { return std::move(t); } void run_test2() { test2<A, A>(A()); test2<B, A>(A()); } }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/attr-cxx0x.cpp
// RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -verify -pedantic -std=c++11 %s int align_illegal alignas(3); //expected-error {{requested alignment is not a power of 2}} char align_big alignas(int); int align_small alignas(1); // expected-error {{requested alignment is less than minimum}} int align_multiple alignas(1) alignas(8) alignas(1); alignas(4) int align_before; struct align_member { int member alignas(8); int bitfield alignas(1) : 1; // expected-error {{}} }; void f(alignas(1) char c) { // expected-error {{'alignas' attribute cannot be applied to a function parameter}} alignas(1) register char k; // expected-error {{'alignas' attribute cannot be applied to a variable with 'register' storage class}} expected-warning {{deprecated}} try { } catch (alignas(4) int n) { // expected-error {{'alignas' attribute cannot be applied to a 'catch' variable}} } } template <unsigned A> struct alignas(A) align_class_template {}; template <typename... T> struct alignas(T...) align_class_temp_pack_type {}; template <unsigned... A> struct alignas(A...) align_class_temp_pack_expr {}; struct alignas(int...) alignas_expansion_no_packs {}; // expected-error {{pack expansion does not contain any unexpanded parameter packs}} template <typename... A> struct outer { template <typename... B> struct alignas(alignof(A) * alignof(B)...) inner {}; // expected-error@-1 {{pack expansion contains parameter packs 'A' and 'B' that have different lengths (1 vs. 2)}} }; outer<int>::inner<short, double> mismatched_packs; // expected-note {{in instantiation of}} typedef char align_typedef alignas(8); // expected-error {{'alignas' attribute only applies to variables, data members and tag types}} template<typename T> using align_alias_template = align_typedef alignas(8); // expected-error {{'alignas' attribute cannot be applied to types}} static_assert(alignof(align_big) == alignof(int), "k's alignment is wrong"); // expected-warning{{'alignof' applied to an expression is a GNU extension}} static_assert(alignof(align_small) == 1, "j's alignment is wrong"); // expected-warning{{'alignof' applied to an expression is a GNU extension}} static_assert(alignof(align_multiple) == 8, "l's alignment is wrong"); // expected-warning{{'alignof' applied to an expression is a GNU extension}} static_assert(alignof(align_member) == 8, "quuux's alignment is wrong"); static_assert(sizeof(align_member) == 8, "quuux's size is wrong"); static_assert(alignof(align_class_template<8>) == 8, "template's alignment is wrong"); static_assert(alignof(align_class_template<16>) == 16, "template's alignment is wrong"); static_assert(alignof(align_class_temp_pack_type<short, int, long>) == alignof(long), "template's alignment is wrong"); static_assert(alignof(align_class_temp_pack_expr<8, 16, 32>) == 32, "template's alignment is wrong"); static_assert(alignof(outer<int,char>::inner<double,short>) == alignof(int) * alignof(double), "template's alignment is wrong"); static_assert(alignof(int(int)) >= 1, "alignof(function) not positive"); // expected-error{{invalid application of 'alignof' to a function type}} [[__carries_dependency__]] // expected-warning{{unknown attribute '__carries_dependency__' ignored}} void func(void); alignas(4) auto PR19252 = 0;
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/vector-no-lax.cpp
// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -fno-lax-vector-conversions -verify %s typedef unsigned int __attribute__((vector_size (16))) vUInt32; typedef int __attribute__((vector_size (16))) vSInt32; vSInt32 foo (vUInt32 a) { vSInt32 b = { 0, 0, 0, 0 }; b += a; // expected-error{{can't convert between vector values}} return b; }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/dependent-auto.cpp
// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11 template<typename T> struct only { only(T); template<typename U> only(U) = delete; // expected-note {{here}} }; template<typename ...T> void f(T ...t) { auto x(t...); // expected-error {{is empty}} expected-error {{contains multiple expressions}} only<int> check = x; } void g() { f(); // expected-note {{here}} f(0); f(0, 1); // expected-note {{here}} } template<typename T> bool h(T t) { auto a = t; decltype(a) b; a = a + b; auto p = new auto(t); only<double*> test = p; // expected-error {{conversion function from 'char *' to 'only<double *>'}} return p; } bool b = h('x'); // expected-note {{here}} // PR 9276 - Make sure we check auto types deduce the same // in the case of a dependent initializer namespace PR9276 { template<typename T> void f() { auto i = T(), j = 0; // expected-error {{deduced as 'long' in declaration of 'i' and deduced as 'int' in declaration of 'j'}} } void g() { f<long>(); // expected-note {{here}} f<int>(); } } namespace NoRepeatedDiagnostic { template<typename T> void f() { auto a = 0, b = 0.0, c = T(); // expected-error {{deduced as 'int' in declaration of 'a' and deduced as 'double' in declaration of 'b'}} } // We've already diagnosed an issue. No extra diagnostics is needed for these. template void f<int>(); template void f<double>(); template void f<char>(); }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/decl-init-ref.cpp
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s -Wno-uninitialized struct A {}; struct BASE { operator A(); // expected-note {{candidate function}} }; struct BASE1 { operator A(); // expected-note {{candidate function}} }; class B : public BASE , public BASE1 { public: B(); } b; extern B f(); const int& ri = (void)0; // expected-error {{reference to type 'const int' could not bind to an rvalue of type 'void'}} int main() { const A& rca = f(); // expected-error {{reference initialization of type 'const A &' with initializer of type 'B' is ambiguous}} A& ra = f(); // expected-error {{non-const lvalue reference to type 'A' cannot bind to a temporary of type 'B'}} } struct PR6139 { A (&x)[1]; }; PR6139 x = {{A()}}; // expected-error{{non-const lvalue reference to type 'A [1]' cannot bind to an initializer list temporary}} struct PR6139b { A (&x)[1]; }; PR6139b y = {A()}; // expected-error{{non-const lvalue reference to type 'A [1]' cannot bind to a temporary of type 'A'}} namespace PR16502 { struct A { int &&temporary; int x, y; }; int f(); const A &c = { 10, ++c.temporary }; }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/gnu-flags.cpp
// RUN: %clang_cc1 -fsyntax-only -verify %s -DNONE -Wno-gnu // RUN: %clang_cc1 -fsyntax-only -verify %s -DALL -Wgnu // RUN: %clang_cc1 -fsyntax-only -verify %s -DALL -Wno-gnu \ // RUN: -Wgnu-anonymous-struct -Wredeclared-class-member \ // RUN: -Wgnu-flexible-array-union-member -Wgnu-folding-constant \ // RUN: -Wgnu-empty-struct // RUN: %clang_cc1 -fsyntax-only -verify %s -DNONE -Wgnu \ // RUN: -Wno-gnu-anonymous-struct -Wno-redeclared-class-member \ // RUN: -Wno-gnu-flexible-array-union-member -Wno-gnu-folding-constant \ // RUN: -Wno-gnu-empty-struct // Additional disabled tests: // %clang_cc1 -fsyntax-only -verify %s -DANONYMOUSSTRUCT -Wno-gnu -Wgnu-anonymous-struct // %clang_cc1 -fsyntax-only -verify %s -DREDECLAREDCLASSMEMBER -Wno-gnu -Wredeclared-class-member // %clang_cc1 -fsyntax-only -verify %s -DFLEXIBLEARRAYUNIONMEMBER -Wno-gnu -Wgnu-flexible-array-union-member // %clang_cc1 -fsyntax-only -verify %s -DFOLDINGCONSTANT -Wno-gnu -Wgnu-folding-constant // %clang_cc1 -fsyntax-only -verify %s -DEMPTYSTRUCT -Wno-gnu -Wgnu-empty-struct #if NONE // expected-no-diagnostics #endif #if ALL || ANONYMOUSSTRUCT // expected-warning@+5 {{anonymous structs are a GNU extension}} #endif struct as { int x; struct { int a; float b; }; }; #if ALL || REDECLAREDCLASSMEMBER // expected-note@+6 {{previous declaration is here}} // expected-warning@+6 {{class member cannot be redeclared}} #endif namespace rcm { class A { class X; class X; class X {}; }; } #if ALL || FLEXIBLEARRAYUNIONMEMBER // expected-warning@+6 {{flexible array member 'c1' in a union is a GNU extension}} #endif struct faum { int l; union { int c1[]; }; }; #if ALL || FOLDINGCONSTANT // expected-warning@+4 {{in-class initializer for static data member is not a constant expression; folding it to a constant is a GNU extension}} #endif struct fic { static const int B = int(0.75 * 1000 * 1000); }; #if ALL || EMPTYSTRUCT // expected-warning@+3 {{flexible array member 'a' in otherwise empty struct is a GNU extension}} #endif struct ofam {int a[];};
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/predefined-expr.cpp
// RUN: %clang_cc1 -x c++ -std=c++1y -fblocks -fsyntax-only -triple %itanium_abi_triple -verify %s // PR16946 // expected-no-diagnostics auto foo() { static_assert(sizeof(__func__) == 4, "foo"); static_assert(sizeof(__FUNCTION__) == 4, "foo"); static_assert(sizeof(__PRETTY_FUNCTION__) == 11, "auto foo()"); return 0; } auto bar() -> decltype(42) { static_assert(sizeof(__func__) == 4, "bar"); static_assert(sizeof(__FUNCTION__) == 4, "bar"); static_assert(sizeof(__PRETTY_FUNCTION__) == 10, "int bar()"); return 0; } // Within templates. template <typename T> int baz() { static_assert(sizeof(__func__) == 4, "baz"); static_assert(sizeof(__FUNCTION__) == 4, "baz"); static_assert(sizeof(__PRETTY_FUNCTION__) == 20, "int baz() [T = int]"); []() { static_assert(sizeof(__func__) == 11, "operator()"); static_assert(sizeof(__FUNCTION__) == 11, "operator()"); static_assert(sizeof(__PRETTY_FUNCTION__) == 50, "auto baz()::<anonymous class>::operator()() const"); return 0; } (); ^{ static_assert(sizeof(__func__) == 27, "___Z3bazIiEiv_block_invoke"); static_assert(sizeof(__FUNCTION__) == 27, "___Z3bazIiEiv_block_invoke"); static_assert(sizeof(__PRETTY_FUNCTION__) == 27, "___Z3bazIiEiv_block_invoke"); } (); #pragma clang __debug captured { static_assert(sizeof(__func__) == 4, "baz"); static_assert(sizeof(__FUNCTION__) == 4, "baz"); static_assert(sizeof(__PRETTY_FUNCTION__) == 20, "int baz() [T = int]"); } return 0; } int main() { static_assert(sizeof(__func__) == 5, "main"); static_assert(sizeof(__FUNCTION__) == 5, "main"); static_assert(sizeof(__PRETTY_FUNCTION__) == 11, "int main()"); []() { static_assert(sizeof(__func__) == 11, "operator()"); static_assert(sizeof(__FUNCTION__) == 11, "operator()"); static_assert(sizeof(__PRETTY_FUNCTION__) == 51, "auto main()::<anonymous class>::operator()() const"); return 0; } (); ^{ static_assert(sizeof(__func__) == 20, "__main_block_invoke"); static_assert(sizeof(__FUNCTION__) == 20, "__main_block_invoke"); static_assert(sizeof(__PRETTY_FUNCTION__) == 20, "__main_block_invoke"); } (); #pragma clang __debug captured { static_assert(sizeof(__func__) == 5, "main"); static_assert(sizeof(__FUNCTION__) == 5, "main"); static_assert(sizeof(__PRETTY_FUNCTION__) == 11, "int main()"); #pragma clang __debug captured { static_assert(sizeof(__func__) == 5, "main"); static_assert(sizeof(__FUNCTION__) == 5, "main"); static_assert(sizeof(__PRETTY_FUNCTION__) == 11, "int main()"); } } []() { #pragma clang __debug captured { static_assert(sizeof(__func__) == 11, "operator()"); static_assert(sizeof(__FUNCTION__) == 11, "operator()"); static_assert(sizeof(__PRETTY_FUNCTION__) == 51, "auto main()::<anonymous class>::operator()() const"); } } (); baz<int>(); return 0; }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/no-wchar.cpp
// RUN: %clang_cc1 -triple i386-pc-win32 -fsyntax-only -fno-wchar -verify %s wchar_t x; // expected-error {{unknown type name 'wchar_t'}} typedef unsigned short wchar_t; void foo(const wchar_t* x); void bar() { foo(L"wide string literal"); }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/attr-no-sanitize-address.cpp
// RUN: %clang_cc1 -fsyntax-only -verify %s #define NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address)) #if !__has_attribute(no_sanitize_address) #error "Should support no_sanitize_address" #endif void noanal_fun() NO_SANITIZE_ADDRESS; void noanal_fun_args() __attribute__((no_sanitize_address(1))); // \ // expected-error {{'no_sanitize_address' attribute takes no arguments}} int noanal_testfn(int y) NO_SANITIZE_ADDRESS; int noanal_testfn(int y) { int x NO_SANITIZE_ADDRESS = y; // \ // expected-error {{'no_sanitize_address' attribute only applies to functions}} return x; } int noanal_test_var NO_SANITIZE_ADDRESS; // \ // expected-error {{'no_sanitize_address' attribute only applies to functions}} class NoanalFoo { private: int test_field NO_SANITIZE_ADDRESS; // \ // expected-error {{'no_sanitize_address' attribute only applies to functions}} void test_method() NO_SANITIZE_ADDRESS; }; class NO_SANITIZE_ADDRESS NoanalTestClass { // \ // expected-error {{'no_sanitize_address' attribute only applies to functions}} }; void noanal_fun_params(int lvar NO_SANITIZE_ADDRESS); // \ // expected-error {{'no_sanitize_address' attribute only applies to functions}}
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/typeid.cpp
// RUN: %clang_cc1 -fsyntax-only -verify %s void f() { (void)typeid(int); // expected-error {{you need to include <typeinfo> before using the 'typeid' operator}} } namespace std { class type_info; } void g() { (void)typeid(int); } struct X; // expected-note 3{{forward declaration}} void g1(X &x) { (void)typeid(X); // expected-error{{'typeid' of incomplete type 'X'}} (void)typeid(X&); // expected-error{{'typeid' of incomplete type 'X'}} (void)typeid(x); // expected-error{{'typeid' of incomplete type 'X'}} } void h(int i) { char V[i]; typeid(V); // expected-error{{'typeid' of variably modified type 'char [i]'}} typeid(char [i]); // expected-error{{'typeid' of variably modified type 'char [i]'}} }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/warn-consumed-analysis.cpp
// RUN: %clang_cc1 -fsyntax-only -verify -Wconsumed -fcxx-exceptions -std=c++11 %s // TODO: Switch to using macros for the expected warnings. #define CALLABLE_WHEN(...) __attribute__ ((callable_when(__VA_ARGS__))) #define CONSUMABLE(state) __attribute__ ((consumable(state))) #define PARAM_TYPESTATE(state) __attribute__ ((param_typestate(state))) #define RETURN_TYPESTATE(state) __attribute__ ((return_typestate(state))) #define SET_TYPESTATE(state) __attribute__ ((set_typestate(state))) #define TEST_TYPESTATE(state) __attribute__ ((test_typestate(state))) typedef decltype(nullptr) nullptr_t; template <typename T> class CONSUMABLE(unconsumed) ConsumableClass { T var; public: ConsumableClass(); ConsumableClass(nullptr_t p) RETURN_TYPESTATE(consumed); ConsumableClass(T val) RETURN_TYPESTATE(unconsumed); ConsumableClass(ConsumableClass<T> &other); ConsumableClass(ConsumableClass<T> &&other); ConsumableClass<T>& operator=(ConsumableClass<T> &other); ConsumableClass<T>& operator=(ConsumableClass<T> &&other); ConsumableClass<T>& operator=(nullptr_t) SET_TYPESTATE(consumed); template <typename U> ConsumableClass<T>& operator=(ConsumableClass<U> &other); template <typename U> ConsumableClass<T>& operator=(ConsumableClass<U> &&other); void operator()(int a) SET_TYPESTATE(consumed); void operator*() const CALLABLE_WHEN("unconsumed"); void unconsumedCall() const CALLABLE_WHEN("unconsumed"); void callableWhenUnknown() const CALLABLE_WHEN("unconsumed", "unknown"); bool isValid() const TEST_TYPESTATE(unconsumed); operator bool() const TEST_TYPESTATE(unconsumed); bool operator!=(nullptr_t) const TEST_TYPESTATE(unconsumed); bool operator==(nullptr_t) const TEST_TYPESTATE(consumed); void constCall() const; void nonconstCall(); void consume() SET_TYPESTATE(consumed); void unconsume() SET_TYPESTATE(unconsumed); }; class CONSUMABLE(unconsumed) DestructorTester { public: DestructorTester(); DestructorTester(int); void operator*() CALLABLE_WHEN("unconsumed"); ~DestructorTester() CALLABLE_WHEN("consumed"); }; void baf0(const ConsumableClass<int> var); void baf1(const ConsumableClass<int> &var); void baf2(const ConsumableClass<int> *var); void baf3(ConsumableClass<int> var); void baf4(ConsumableClass<int> &var); void baf5(ConsumableClass<int> *var); void baf6(ConsumableClass<int> &&var); ConsumableClass<int> returnsUnconsumed() { return ConsumableClass<int>(); // expected-warning {{return value not in expected state; expected 'unconsumed', observed 'consumed'}} } ConsumableClass<int> returnsConsumed() RETURN_TYPESTATE(consumed); ConsumableClass<int> returnsConsumed() { return ConsumableClass<int>(); } ConsumableClass<int> returnsUnknown() RETURN_TYPESTATE(unknown); void testInitialization() { ConsumableClass<int> var0; ConsumableClass<int> var1 = ConsumableClass<int>(); ConsumableClass<int> var2(42); ConsumableClass<int> var3(var2); // copy constructor ConsumableClass<int> var4(var0); // copy consumed value *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'consumed' state}} *var1; // expected-warning {{invalid invocation of method 'operator*' on object 'var1' while it is in the 'consumed' state}} *var2; *var3; *var4; // expected-warning {{invalid invocation of method 'operator*' on object 'var4' while it is in the 'consumed' state}} var0 = ConsumableClass<int>(42); *var0; var0 = var1; *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'consumed' state}} if (var0.isValid()) { *var0; *var1; } else { *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'consumed' state}} } } void testDestruction() { DestructorTester D0(42), D1(42), D2; *D0; *D1; *D2; // expected-warning {{invalid invocation of method 'operator*' on object 'D2' while it is in the 'consumed' state}} D0.~DestructorTester(); // expected-warning {{invalid invocation of method '~DestructorTester' on object 'D0' while it is in the 'unconsumed' state}} return; // expected-warning {{invalid invocation of method '~DestructorTester' on object 'D0' while it is in the 'unconsumed' state}} \ expected-warning {{invalid invocation of method '~DestructorTester' on object 'D1' while it is in the 'unconsumed' state}} } void testTempValue() { *ConsumableClass<int>(); // expected-warning {{invalid invocation of method 'operator*' on a temporary object while it is in the 'consumed' state}} } void testSimpleRValueRefs() { ConsumableClass<int> var0; ConsumableClass<int> var1(42); *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'consumed' state}} *var1; var0 = static_cast<ConsumableClass<int>&&>(var1); *var0; *var1; // expected-warning {{invalid invocation of method 'operator*' on object 'var1' while it is in the 'consumed' state}} } void testIfStmt() { ConsumableClass<int> var; if (var.isValid()) { *var; } else { *var; // expected-warning {{invalid invocation of method 'operator*' on object 'var' while it is in the 'consumed' state}} } if (!var.isValid()) { *var; // expected-warning {{invalid invocation of method 'operator*' on object 'var' while it is in the 'consumed' state}} } else { *var; } if (var) { // Empty } else { *var; // expected-warning {{invalid invocation of method 'operator*' on object 'var' while it is in the 'consumed' state}} } if (var != nullptr) { // Empty } else { *var; // expected-warning {{invalid invocation of method 'operator*' on object 'var' while it is in the 'consumed' state}} } if (var == nullptr) { *var; // expected-warning {{invalid invocation of method 'operator*' on object 'var' while it is in the 'consumed' state}} } else { // Empty } } void testComplexConditionals0() { ConsumableClass<int> var0, var1, var2; if (var0 && var1) { *var0; *var1; } else { *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'consumed' state}} *var1; // expected-warning {{invalid invocation of method 'operator*' on object 'var1' while it is in the 'consumed' state}} } if (var0 || var1) { *var0; *var1; } else { *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'consumed' state}} *var1; // expected-warning {{invalid invocation of method 'operator*' on object 'var1' while it is in the 'consumed' state}} } if (var0 && !var1) { *var0; *var1; } else { *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'consumed' state}} *var1; // expected-warning {{invalid invocation of method 'operator*' on object 'var1' while it is in the 'consumed' state}} } if (var0 || !var1) { *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'consumed' state}} *var1; // expected-warning {{invalid invocation of method 'operator*' on object 'var1' while it is in the 'consumed' state}} } else { *var0; *var1; } if (!var0 && !var1) { *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'consumed' state}} *var1; // expected-warning {{invalid invocation of method 'operator*' on object 'var1' while it is in the 'consumed' state}} } else { *var0; *var1; } if (!var0 || !var1) { *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'consumed' state}} *var1; // expected-warning {{invalid invocation of method 'operator*' on object 'var1' while it is in the 'consumed' state}} } else { *var0; *var1; } if (!(var0 && var1)) { *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'consumed' state}} *var1; // expected-warning {{invalid invocation of method 'operator*' on object 'var1' while it is in the 'consumed' state}} } else { *var0; *var1; } if (!(var0 || var1)) { *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'consumed' state}} *var1; // expected-warning {{invalid invocation of method 'operator*' on object 'var1' while it is in the 'consumed' state}} } else { *var0; *var1; } if (var0 && var1 && var2) { *var0; *var1; *var2; } else { *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'consumed' state}} *var1; // expected-warning {{invalid invocation of method 'operator*' on object 'var1' while it is in the 'consumed' state}} *var2; // expected-warning {{invalid invocation of method 'operator*' on object 'var2' while it is in the 'consumed' state}} } #if 0 // FIXME: Get this test to pass. if (var0 || var1 || var2) { *var0; *var1; *var2; } else { *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'consumed' state}} *var1; // expected-warning {{invalid invocation of method 'operator*' on object 'var1' while it is in the 'consumed' state}} *var2; // expected-warning {{invalid invocation of method 'operator*' on object 'var2' while it is in the 'consumed' state}} } #endif } void testComplexConditionals1() { ConsumableClass<int> var0, var1, var2; // Coerce all variables into the unknown state. baf4(var0); baf4(var1); baf4(var2); if (var0 && var1) { *var0; *var1; } else { *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'unknown' state}} *var1; // expected-warning {{invalid invocation of method 'operator*' on object 'var1' while it is in the 'unknown' state}} } if (var0 || var1) { *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'unknown' state}} *var1; // expected-warning {{invalid invocation of method 'operator*' on object 'var1' while it is in the 'unknown' state}} } else { *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'consumed' state}} *var1; // expected-warning {{invalid invocation of method 'operator*' on object 'var1' while it is in the 'consumed' state}} } if (var0 && !var1) { *var0; *var1; // expected-warning {{invalid invocation of method 'operator*' on object 'var1' while it is in the 'consumed' state}} } else { *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'unknown' state}} *var1; // expected-warning {{invalid invocation of method 'operator*' on object 'var1' while it is in the 'unknown' state}} } if (var0 || !var1) { *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'unknown' state}} *var1; // expected-warning {{invalid invocation of method 'operator*' on object 'var1' while it is in the 'unknown' state}} } else { *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'consumed' state}} *var1; } if (!var0 && !var1) { *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'consumed' state}} *var1; // expected-warning {{invalid invocation of method 'operator*' on object 'var1' while it is in the 'consumed' state}} } else { *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'unknown' state}} *var1; // expected-warning {{invalid invocation of method 'operator*' on object 'var1' while it is in the 'unknown' state}} } if (!(var0 || var1)) { *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'consumed' state}} *var1; // expected-warning {{invalid invocation of method 'operator*' on object 'var1' while it is in the 'consumed' state}} } else { *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'unknown' state}} *var1; // expected-warning {{invalid invocation of method 'operator*' on object 'var1' while it is in the 'unknown' state}} } if (!var0 || !var1) { *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'unknown' state}} *var1; // expected-warning {{invalid invocation of method 'operator*' on object 'var1' while it is in the 'unknown' state}} } else { *var0; *var1; } if (!(var0 && var1)) { *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'unknown' state}} *var1; // expected-warning {{invalid invocation of method 'operator*' on object 'var1' while it is in the 'unknown' state}} } else { *var0; *var1; } if (var0 && var1 && var2) { *var0; *var1; *var2; } else { *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'unknown' state}} *var1; // expected-warning {{invalid invocation of method 'operator*' on object 'var1' while it is in the 'unknown' state}} *var2; // expected-warning {{invalid invocation of method 'operator*' on object 'var2' while it is in the 'unknown' state}} } #if 0 // FIXME: Get this test to pass. if (var0 || var1 || var2) { *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'unknown' state}} *var1; // expected-warning {{invalid invocation of method 'operator*' on object 'var1' while it is in the 'unknown' state}} *var2; // expected-warning {{invalid invocation of method 'operator*' on object 'var2' while it is in the 'unknown' state}} } else { *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'consumed' state}} *var1; // expected-warning {{invalid invocation of method 'operator*' on object 'var1' while it is in the 'consumed' state}} *var2; // expected-warning {{invalid invocation of method 'operator*' on object 'var2' while it is in the 'consumed' state}} } #endif } void testStateChangeInBranch() { ConsumableClass<int> var; // Make var enter the 'unknown' state. baf4(var); if (!var) { var = ConsumableClass<int>(42); } *var; } void testFunctionParam(ConsumableClass<int> param) { if (param.isValid()) { *param; } else { *param; } param = nullptr; *param; // expected-warning {{invocation of method 'operator*' on object 'param' while it is in the 'consumed' state}} } void testParamReturnTypestateCallee(bool cond, ConsumableClass<int> &Param RETURN_TYPESTATE(unconsumed)) { // expected-warning {{parameter 'Param' not in expected state when the function returns: expected 'unconsumed', observed 'consumed'}} if (cond) { Param.consume(); return; // expected-warning {{parameter 'Param' not in expected state when the function returns: expected 'unconsumed', observed 'consumed'}} } Param.consume(); } void testParamReturnTypestateCaller() { ConsumableClass<int> var; testParamReturnTypestateCallee(true, var); *var; } void testParamTypestateCallee(ConsumableClass<int> Param0 PARAM_TYPESTATE(consumed), ConsumableClass<int> &Param1 PARAM_TYPESTATE(consumed)) { *Param0; // expected-warning {{invalid invocation of method 'operator*' on object 'Param0' while it is in the 'consumed' state}} *Param1; // expected-warning {{invalid invocation of method 'operator*' on object 'Param1' while it is in the 'consumed' state}} } void testParamTypestateCaller() { ConsumableClass<int> Var0, Var1(42); testParamTypestateCallee(Var0, Var1); // expected-warning {{argument not in expected state; expected 'consumed', observed 'unconsumed'}} } void consumeFunc(ConsumableClass<int> P PARAM_TYPESTATE(unconsumed)); struct ParamTest { static void consumeFuncStatic(ConsumableClass<int> P PARAM_TYPESTATE(unconsumed)); void consumeFuncMeth(ConsumableClass<int> P PARAM_TYPESTATE(unconsumed)); void operator<<(ConsumableClass<int> P PARAM_TYPESTATE(unconsumed)); }; void operator>>(ParamTest& pt, ConsumableClass<int> P PARAM_TYPESTATE(unconsumed)); void testFunctionParams() { // Make sure we handle the different kinds of functions. ConsumableClass<int> P; consumeFunc(P); // expected-warning {{argument not in expected state; expected 'unconsumed', observed 'consumed'}} ParamTest::consumeFuncStatic(P); // expected-warning {{argument not in expected state; expected 'unconsumed', observed 'consumed'}} ParamTest pt; pt.consumeFuncMeth(P); // expected-warning {{argument not in expected state; expected 'unconsumed', observed 'consumed'}} pt << P; // expected-warning {{argument not in expected state; expected 'unconsumed', observed 'consumed'}} pt >> P; // expected-warning {{argument not in expected state; expected 'unconsumed', observed 'consumed'}} } void baf3(ConsumableClass<int> var) { *var; } void baf4(ConsumableClass<int> &var) { *var; // expected-warning {{invalid invocation of method 'operator*' on object 'var' while it is in the 'unknown' state}} } void baf6(ConsumableClass<int> &&var) { *var; } void testCallingConventions() { ConsumableClass<int> var(42); baf0(var); *var; baf1(var); *var; baf2(&var); *var; baf4(var); *var; // expected-warning {{invalid invocation of method 'operator*' on object 'var' while it is in the 'unknown' state}} var = ConsumableClass<int>(42); baf5(&var); *var; // expected-warning {{invalid invocation of method 'operator*' on object 'var' while it is in the 'unknown' state}} var = ConsumableClass<int>(42); baf6(static_cast<ConsumableClass<int>&&>(var)); *var; // expected-warning {{invalid invocation of method 'operator*' on object 'var' while it is in the 'consumed' state}} } void testConstAndNonConstMemberFunctions() { ConsumableClass<int> var(42); var.constCall(); *var; var.nonconstCall(); *var; } void testFunctionParam0(ConsumableClass<int> param) { *param; } void testFunctionParam1(ConsumableClass<int> &param) { *param; // expected-warning {{invalid invocation of method 'operator*' on object 'param' while it is in the 'unknown' state}} } void testReturnStates() { ConsumableClass<int> var; var = returnsUnconsumed(); *var; var = returnsConsumed(); *var; // expected-warning {{invalid invocation of method 'operator*' on object 'var' while it is in the 'consumed' state}} } void testCallableWhen() { ConsumableClass<int> var(42); *var; baf4(var); var.callableWhenUnknown(); } void testMoveAsignmentish() { ConsumableClass<int> var0; ConsumableClass<long> var1(42); *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'consumed' state}} *var1; var0 = static_cast<ConsumableClass<long>&&>(var1); *var0; *var1; // expected-warning {{invalid invocation of method 'operator*' on object 'var1' while it is in the 'consumed' state}} var1 = ConsumableClass<long>(42); var1 = nullptr; *var1; // expected-warning {{invalid invocation of method 'operator*' on object 'var1' while it is in the 'consumed' state}} } void testConditionalMerge() { ConsumableClass<int> var; if (var.isValid()) { // Empty } *var; // expected-warning {{invalid invocation of method 'operator*' on object 'var' while it is in the 'consumed' state}} if (var.isValid()) { // Empty } else { // Empty } *var; // expected-warning {{invalid invocation of method 'operator*' on object 'var' while it is in the 'consumed' state}} } void testSetTypestate() { ConsumableClass<int> var(42); *var; var.consume(); *var; // expected-warning {{invalid invocation of method 'operator*' on object 'var' while it is in the 'consumed' state}} var.unconsume(); *var; } void testConsumes0() { ConsumableClass<int> var(nullptr); *var; // expected-warning {{invalid invocation of method 'operator*' on object 'var' while it is in the 'consumed' state}} } void testConsumes1() { ConsumableClass<int> var(42); var.unconsumedCall(); var(6); var.unconsumedCall(); // expected-warning {{invalid invocation of method 'unconsumedCall' on object 'var' while it is in the 'consumed' state}} } void testUnreachableBlock() { ConsumableClass<int> var(42); if (var) { *var; } else { *var; } *var; } void testForLoop1() { ConsumableClass<int> var0, var1(42); for (int i = 0; i < 10; ++i) { // expected-warning {{state of variable 'var1' must match at the entry and exit of loop}} *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'consumed' state}} *var1; var1.consume(); *var1; // expected-warning {{invalid invocation of method 'operator*' on object 'var1' while it is in the 'consumed' state}} } *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'consumed' state}} } void testWhileLoop1() { int i = 10; ConsumableClass<int> var0, var1(42); while (i-- > 0) { // expected-warning {{state of variable 'var1' must match at the entry and exit of loop}} *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'consumed' state}} *var1; var1.consume(); *var1; // expected-warning {{invalid invocation of method 'operator*' on object 'var1' while it is in the 'consumed' state}} } *var0; // expected-warning {{invalid invocation of method 'operator*' on object 'var0' while it is in the 'consumed' state}} } // Tests if state information is correctly discarded for certain shapes of CFGs. void testSwitchGOTO(void) { int a; LABEL0: switch (a) case 0: goto LABEL0; goto LABEL0; } typedef const int*& IntegerPointerReference; void testIsRValueRefishAndCanonicalType(IntegerPointerReference a) {} namespace ContinueICETest { bool cond1(); bool cond2(); static void foo1() { while (cond1()) { if (cond2()) continue; } } static void foo2() { while (true) { if (false) continue; } } class runtime_error { public: virtual ~runtime_error(); }; void read(bool sf) { while (sf) { if(sf) throw runtime_error(); } } } // end namespace ContinueICETest namespace StatusUseCaseTests { class CONSUMABLE(unconsumed) __attribute__((consumable_auto_cast_state)) __attribute__((consumable_set_state_on_read)) Status { int code; public: static Status OK; Status() RETURN_TYPESTATE(consumed); Status(int c) RETURN_TYPESTATE(unconsumed); Status(const Status &other); Status(Status &&other); Status& operator=(const Status &other) CALLABLE_WHEN("unknown", "consumed"); Status& operator=(Status &&other) CALLABLE_WHEN("unknown", "consumed"); bool operator==(const Status &other) const SET_TYPESTATE(consumed); bool check() const SET_TYPESTATE(consumed); void ignore() const SET_TYPESTATE(consumed); // Status& markAsChecked() { return *this; } void clear() CALLABLE_WHEN("unknown", "consumed") SET_TYPESTATE(consumed); ~Status() CALLABLE_WHEN("unknown", "consumed"); operator bool() const; // Will not consume the object. }; bool cond(); Status doSomething(); void handleStatus(const Status& s RETURN_TYPESTATE(consumed)); void handleStatusRef(Status& s); void handleStatusPtr(Status* s); void handleStatusUnmarked(const Status& s); void log(const char* msg); void fail() __attribute__((noreturn)); void checkStat(const Status& s); void testSimpleTemporaries0() { doSomething(); // expected-warning {{invalid invocation of method '~Status' on a temporary object while it is in the 'unconsumed' state}} } void testSimpleTemporaries1() { doSomething().ignore(); } void testSimpleTemporaries2() { handleStatus(doSomething()); } void testSimpleTemporaries3() { Status s = doSomething(); } // expected-warning {{invalid invocation of method '~Status' on object 's' while it is in the 'unconsumed' state}} void testTemporariesWithControlFlow(bool a) { bool b = false || doSomething(); // expected-warning {{invalid invocation of method '~Status' on a temporary object while it is in the 'unconsumed' state}} } Status testSimpleTemporariesReturn0() { return doSomething(); } Status testSimpleTemporariesReturn1() { Status s = doSomething(); return s; } void testSimpleTemporaries4() { Status s = doSomething(); s.check(); } void testSimpleTemporaries5() { Status s = doSomething(); s.clear(); // expected-warning {{invalid invocation of method 'clear' on object 's' while it is in the 'unconsumed' state}} } void testSimpleTemporaries6() { Status s1 = doSomething(); handleStatus(s1); Status s2 = doSomething(); handleStatusRef(s2); Status s3 = doSomething(); handleStatusPtr(&s3); Status s4 = doSomething(); handleStatusUnmarked(s4); } void testSimpleTemporaries7() { Status s; s = doSomething(); } // expected-warning {{invalid invocation of method '~Status' on object 's' while it is in the 'unconsumed' state}} void testTemporariesWithConditionals0() { int a; Status s = doSomething(); if (cond()) a = 0; else a = 1; } // expected-warning {{invalid invocation of method '~Status' on object 's' while it is in the 'unconsumed' state}} void testTemporariesWithConditionals1() { int a; Status s = doSomething(); if (cond()) a = 0; else a = 1; s.ignore(); } void testTemporariesWithConditionals2() { int a; Status s = doSomething(); s.ignore(); if (cond()) a = 0; else a = 1; } void testTemporariesWithConditionals3() { Status s = doSomething(); if (cond()) { s.check(); } } void testTemporariesAndConstructors0() { Status s(doSomething()); // Test the copy constructor. s.check(); } void testTemporariesAndConstructors1F() { Status s1 = doSomething(); // Test the copy constructor. Status s2 = s1; } // expected-warning {{invalid invocation of method '~Status' on object 's2' while it is in the 'unconsumed' state}} void testTemporariesAndConstructors1S() { Status s1 = doSomething(); // Test the copy constructor. Status s2(s1); s2.check(); } void testTemporariesAndConstructors2F() { // Test the move constructor. Status s1 = doSomething(); Status s2 = static_cast<Status&&>(s1); } // expected-warning {{invalid invocation of method '~Status' on object 's2' while it is in the 'unconsumed' state}} void testTemporariesAndConstructors2S() { // Test the move constructor. Status s1 = doSomething(); Status s2 = static_cast<Status&&>(s1); s2.check(); } void testTemporariesAndOperators0F() { // Test the assignment operator. Status s1 = doSomething(); Status s2; s2 = s1; } // expected-warning {{invalid invocation of method '~Status' on object 's2' while it is in the 'unconsumed' state}} void testTemporariesAndOperators0S() { // Test the assignment operator. Status s1 = doSomething(); Status s2; s2 = s1; s2.check(); } void testTemporariesAndOperators1F() { // Test the move assignment operator. Status s1 = doSomething(); Status s2; s2 = static_cast<Status&&>(s1); } // expected-warning {{invalid invocation of method '~Status' on object 's2' while it is in the 'unconsumed' state}} void testTemporariesAndOperators1S() { // Test the move assignment operator. Status s1 = doSomething(); Status s2; s2 = static_cast<Status&&>(s1); s2.check(); } void testTemporariesAndOperators2() { Status s1 = doSomething(); Status s2 = doSomething(); s1 = s2; // expected-warning {{invalid invocation of method 'operator=' on object 's1' while it is in the 'unconsumed' state}} s1.check(); s2.check(); } Status testReturnAutocast() { Status s = doSomething(); s.check(); // consume s return s; // should autocast back to unconsumed } namespace TestParens { void test3() { checkStat((doSomething())); } void test4() { Status s = (doSomething()); s.check(); } void test5() { (doSomething()).check(); } void test6() { if ((doSomething()) == Status::OK) return; } } // end namespace TestParens } // end namespace InitializerAssertionFailTest namespace std { void move(); template<class T> void move(T&&); namespace __1 { void move(); template<class T> void move(T&&); } } namespace PR18260 { class X { public: void move(); } x; void test() { x.move(); std::move(); std::move(x); std::__1::move(); std::__1::move(x); } } // end namespace PR18260
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/pr9812.cpp
// RUN: %clang_cc1 -fsyntax-only -verify %s int test(int, char**) { bool signed; // expected-error {{'bool' cannot be signed or unsigned}} expected-warning {{declaration does not declare anything}} return 0; }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/overload-0x.cpp
// RUN: %clang_cc1 -fsyntax-only -std=c++11 -verify %s // RUN: %clang_cc1 -fsyntax-only -verify %s namespace test0 { struct A { // expected-note {{candidate function (the implicit copy assignment operator) not viable: 'this' argument has type 'const test0::A', but method is not marked const}} #if __cplusplus >= 201103L // expected-note@-2 {{candidate function (the implicit move assignment operator) not viable: 'this' argument has type 'const test0::A', but method is not marked const}} #endif A &operator=(void*); // expected-note {{candidate function not viable: 'this' argument has type 'const test0::A', but method is not marked const}} }; void test(const A &a) { a = "help"; // expected-error {{no viable overloaded '='}} } } namespace PR16314 { void f(char*); int &f(...); void x() { int &n = f("foo"); #if __cplusplus < 201103L // expected-warning@-2 {{conversion from string literal to 'char *' is deprecated}} // expected-error@-3 {{non-const lvalue reference to type 'int' cannot bind to a temporary of type 'void'}} #endif } } namespace warn_if_best { int f(char *); void f(double); void x() { int n = f("foo"); #if __cplusplus < 201103L // expected-warning@-2 {{conversion from string literal to 'char *' is deprecated}} #else // expected-warning@-4 {{ISO C++11 does not allow conversion from string literal to 'char *'}} #endif } } namespace userdefined_vs_illformed { struct X { X(const char *); }; void *f(char *p); // best for C++03 double f(X x); // best for C++11 void g() { double d = f("foo"); #if __cplusplus < 201103L // expected-warning@-2 {{conversion from string literal to 'char *' is deprecated}} // expected-error@-3 {{cannot initialize a variable of type 'double' with an rvalue of type 'void *'}} #endif } } namespace sfinae_test { int f(int, char*); template<int T> struct S { typedef int type; }; template<> struct S<sizeof(int)> { typedef void type; }; // C++11: SFINAE failure // C++03: ok template<typename T> int cxx11_ignored(T, typename S<sizeof(f(T(), "foo"))>::type *); #if __cplusplus < 201103L // expected-warning@-2 {{conversion from string literal to 'char *' is deprecated}} #else // expected-note@-4 {{candidate template ignored: substitution failure}} #endif // C++11: better than latter // C++03: worse than latter template<typename T> void g(T, ...); template<typename T> int g(T, typename S<sizeof(f(T(), "foo"))>::type *); #if __cplusplus < 201103L // expected-warning@-2 {{conversion from string literal to 'char *' is deprecated}} #endif int a = cxx11_ignored(0, 0); int b = g(0, 0); #if __cplusplus >= 201103L // expected-error@-3 {{no matching function for call to 'cxx11_ignored'}} // expected-error@-3 {{cannot initialize a variable of type 'int' with an rvalue of type 'void'}} #endif }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/constexpr-factorial.cpp
// RUN: %clang_cc1 -std=c++11 -fsyntax-only %s constexpr unsigned oddfac(unsigned n) { return n == 1 ? 1 : n * oddfac(n-2); } constexpr unsigned k = oddfac(123); using A = int[k % 256]; using A = int[43];
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/functional-cast.cpp
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s // REQUIRES: LP64 // ------------ not interpreted as C-style cast ------------ struct SimpleValueInit { int i; }; struct InitViaConstructor { InitViaConstructor(int i = 7); }; struct NoValueInit { // expected-note 2 {{candidate constructor (the implicit copy constructor)}} expected-note 2 {{candidate constructor (the implicit move constructor)}} NoValueInit(int i, int j); // expected-note 2 {{candidate constructor}} }; void test_cxx_functional_value_init() { (void)SimpleValueInit(); (void)InitViaConstructor(); (void)NoValueInit(); // expected-error{{no matching constructor for initialization}} } void test_cxx_function_cast_multi() { (void)NoValueInit(0, 0); (void)NoValueInit(0, 0, 0); // expected-error{{no matching constructor for initialization}} (void)int(1, 2); // expected-error{{excess elements in scalar initializer}} (void)int({}, 2); // expected-error{{excess elements in scalar initializer}} } // ------------------ everything else -------------------- struct A {}; // ----------- const_cast -------------- typedef char c; typedef c *cp; typedef cp *cpp; typedef cpp *cppp; typedef cppp &cpppr; typedef const cppp &cpppcr; typedef const char cc; typedef cc *ccp; typedef volatile ccp ccvp; typedef ccvp *ccvpp; typedef const volatile ccvpp ccvpcvp; typedef ccvpcvp *ccvpcvpp; typedef int iar[100]; typedef iar &iarr; typedef int (*f)(int); void t_cc() { ccvpcvpp var = 0; // Cast away deep consts and volatiles. char ***var2 = cppp(var); char ***const &var3 = var2; // Const reference to reference. char ***&var4 = cpppr(var3); // Drop reference. Intentionally without qualifier change. char *** var5 = cppp(var4); const int ar[100] = {0}; // Array decay. Intentionally without qualifier change. typedef int *intp; int *pi = intp(ar); f fp = 0; // Don't misidentify fn** as a function pointer. typedef f *fp_t; f *fpp = fp_t(&fp); int const A::* const A::*icapcap = 0; typedef int A::* A::*iapap_t; iapap_t iapap = iapap_t(icapcap); } // ----------- static_cast ------------- struct B : public A {}; // Single public base. struct C1 : public virtual B {}; // Single virtual base. struct C2 : public virtual B {}; struct D : public C1, public C2 {}; // Diamond struct E : private A {}; // Single private base. struct F : public C1 {}; // Single path to B with virtual. struct G1 : public B {}; struct G2 : public B {}; struct H : public G1, public G2 {}; // Ambiguous path to B. enum Enum { En1, En2 }; enum Onom { On1, On2 }; struct Co1 { operator int(); }; struct Co2 { Co2(int); }; struct Co3 { }; struct Co4 { Co4(Co3); operator Co3(); }; // Explicit implicits void t_529_2() { int i = 1; (void)float(i); double d = 1.0; (void)float(d); (void)int(d); (void)char(i); typedef unsigned long ulong; (void)ulong(i); (void)int(En1); (void)double(En1); typedef int &intr; (void)intr(i); typedef const int &cintr; (void)cintr(i); int ar[1]; typedef const int *cintp; (void)cintp(ar); typedef void (*pfvv)(); (void)pfvv(t_529_2); typedef void *voidp; (void)voidp(0); (void)voidp((int*)0); typedef volatile const void *vcvoidp; (void)vcvoidp((const int*)0); typedef A *Ap; (void)Ap((B*)0); typedef A &Ar; (void)Ar(*((B*)0)); typedef const B *cBp; (void)cBp((C1*)0); typedef B &Br; (void)Br(*((C1*)0)); (void)Ap((D*)0); typedef const A &cAr; (void)cAr(*((D*)0)); typedef int B::*Bmp; (void)Bmp((int A::*)0); typedef void (B::*Bmfp)(); (void)Bmfp((void (A::*)())0); (void)Ap((E*)0); // functional-style cast ignores access control (void)voidp((const int*)0); // const_cast appended (void)int(Co1()); (void)Co2(1); (void)Co3((Co4)(Co3())); // Bad code below //(void)(A*)((H*)0); // {{static_cast from 'struct H *' to 'struct A *' is not allowed}} } // Anything to void void t_529_4() { void(1); (void(t_529_4)); } // Static downcasts void t_529_5_8() { typedef B *Bp; (void)Bp((A*)0); typedef B &Br; (void)Br(*((A*)0)); typedef const G1 *cG1p; (void)cG1p((A*)0); typedef const G1 &cG1r; (void)cG1r(*((A*)0)); (void)Bp((const A*)0); // const_cast appended (void)Br(*((const A*)0)); // const_cast appended typedef E *Ep; (void)Ep((A*)0); // access control ignored typedef E &Er; (void)Er(*((A*)0)); // access control ignored // Bad code below typedef C1 *C1p; (void)C1p((A*)0); // expected-error {{cannot cast 'A *' to 'C1p' (aka 'C1 *') via virtual base 'B'}} typedef C1 &C1r; (void)C1r(*((A*)0)); // expected-error {{cannot cast 'A' to 'C1r' (aka 'C1 &') via virtual base 'B'}} typedef D *Dp; (void)Dp((A*)0); // expected-error {{cannot cast 'A *' to 'Dp' (aka 'D *') via virtual base 'B'}} typedef D &Dr; (void)Dr(*((A*)0)); // expected-error {{cannot cast 'A' to 'Dr' (aka 'D &') via virtual base 'B'}} typedef H *Hp; (void)Hp((A*)0); // expected-error {{ambiguous cast from base 'A' to derived 'H':\n struct A -> struct B -> struct G1 -> struct H\n struct A -> struct B -> struct G2 -> struct H}} typedef H &Hr; (void)Hr(*((A*)0)); // expected-error {{ambiguous cast from base 'A' to derived 'H':\n struct A -> struct B -> struct G1 -> struct H\n struct A -> struct B -> struct G2 -> struct H}} // TODO: Test DR427. This requires user-defined conversions, though. } // Enum conversions void t_529_7() { (void)Enum(1); (void)Enum(1.0); (void)Onom(En1); // Bad code below (void)Enum((int*)0); // expected-error {{functional-style cast from 'int *' to 'Enum' is not allowed}} } // Void pointer to object pointer void t_529_10() { typedef int *intp; (void)intp((void*)0); typedef const A *cAp; (void)cAp((void*)0); (void)intp((const void*)0); // const_cast appended } // Member pointer upcast. void t_529_9() { typedef int A::*Amp; (void)Amp((int B::*)0); // Bad code below (void)Amp((int H::*)0); // expected-error {{ambiguous conversion from pointer to member of derived class 'H' to pointer to member of base class 'A':}} (void)Amp((int F::*)0); // expected-error {{conversion from pointer to member of class 'F' to pointer to member of class 'A' via virtual base 'B' is not allowed}} } // -------- reinterpret_cast ----------- enum test { testval = 1 }; struct structure { int m; }; typedef void (*fnptr)(); // Test conversion between pointer and integral types, as in p3 and p4. void integral_conversion() { typedef void *voidp; void *vp = voidp(testval); long l = long(vp); typedef float *floatp; (void)floatp(l); fnptr fnp = fnptr(l); (void)char(fnp); // expected-error {{cast from pointer to smaller type 'char' loses information}} (void)long(fnp); } void pointer_conversion() { int *p1 = 0; typedef float *floatp; float *p2 = floatp(p1); typedef structure *structurep; structure *p3 = structurep(p2); typedef int **ppint; typedef ppint *pppint; ppint *deep = pppint(p3); typedef fnptr fnptrp; (void)fnptrp(deep); } void constness() { int ***const ipppc = 0; typedef int const *icp_t; int const *icp = icp_t(ipppc); typedef int *intp; (void)intp(icp); // const_cast appended typedef int const *const ** intcpcpp; intcpcpp icpcpp = intcpcpp(ipppc); // const_cast appended int *ip = intp(icpcpp); (void)icp_t(ip); typedef int const *const *const *intcpcpcp; (void)intcpcpcp(ipppc); } void fnptrs() { typedef int (*fnptr2)(int); fnptr fp = 0; (void)fnptr2(fp); typedef void *voidp; void *vp = voidp(fp); (void)fnptr(vp); } void refs() { long l = 0; typedef char &charr; char &c = charr(l); // Bad: from rvalue typedef int &intr; (void)intr(&c); // expected-error {{functional-style cast from rvalue to reference type 'intr' (aka 'int &')}} } void memptrs() { const int structure::*psi = 0; typedef const float structure::*structurecfmp; (void)structurecfmp(psi); typedef int structure::*structureimp; (void)structureimp(psi); // const_cast appended void (structure::*psf)() = 0; typedef int (structure::*structureimfp)(); (void)structureimfp(psf); typedef void (structure::*structurevmfp)(); (void)structurevmfp(psi); // expected-error-re {{functional-style cast from 'const int structure::*' to 'structurevmfp' (aka 'void (structure::*)(){{( __attribute__\(\(thiscall\)\))?}}') is not allowed}} (void)structureimp(psf); // expected-error-re {{functional-style cast from 'void (structure::*)(){{( __attribute__\(\(thiscall\)\))?}}' to 'structureimp' (aka 'int structure::*') is not allowed}} } // ---------------- misc ------------------ void crash_on_invalid_1() { typedef itn Typo; // expected-error {{unknown type name 'itn'}} (void)Typo(1); // used to crash typedef int &int_ref; (void)int_ref(); // expected-error {{reference to type 'int' requires an initializer}} }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/builtin_objc_msgSend.cpp
// RUN: %clang_cc1 %s -fsyntax-only -verify // expected-no-diagnostics // rdar://8686888 typedef struct objc_selector *SEL; typedef struct objc_object *id; extern "C" __attribute__((visibility("default"))) id objc_msgSend(id self, SEL op, ...) __attribute__((visibility("default"))); inline void TCFReleaseGC(void * object) { static SEL SEL_release; objc_msgSend((id)object, SEL_release); }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/warn-div-or-rem-by-zero.cpp
// RUN: %clang_cc1 -verify %s // RUN: %clang_cc1 -std=c++11 -verify %s // RUN: %clang_cc1 -std=c++1y -verify %s void div() { (void)(42 / 0); // expected-warning{{division by zero is undefined}} (void)(42 / false); // expected-warning{{division by zero is undefined}} (void)(42 / !1); // expected-warning{{division by zero is undefined}} (void)(42 / (1 - 1)); // expected-warning{{division by zero is undefined}} (void)(42 / !(1 + 1)); // expected-warning{{division by zero is undefined}} (void)(42 / (int)(0.0)); // expected-warning{{division by zero is undefined}} } void rem() { (void)(42 % 0); // expected-warning{{remainder by zero is undefined}} (void)(42 % false); // expected-warning{{remainder by zero is undefined}} (void)(42 % !1); // expected-warning{{remainder by zero is undefined}} (void)(42 % (1 - 1)); // expected-warning{{remainder by zero is undefined}} (void)(42 % !(1 + 1)); // expected-warning{{remainder by zero is undefined}} (void)(42 % (int)(0.0)); // expected-warning{{remainder by zero is undefined}} }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/integer-overflow.cpp
// RUN: %clang_cc1 %s -verify -fsyntax-only -std=gnu++98 typedef unsigned long long uint64_t; typedef unsigned long long uint32_t; uint64_t f0(uint64_t); uint64_t f1(uint64_t, uint32_t); uint64_t f2(uint64_t, ...); static const uint64_t overflow = 1 * 4608 * 1024 * 1024; // expected-warning {{overflow in expression; result is 536870912 with type 'int'}} uint64_t check_integer_overflows(int i) { //expected-note {{declared here}} // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} uint64_t overflow = 4608 * 1024 * 1024, // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} overflow2 = (uint64_t)(4608 * 1024 * 1024), // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} overflow3 = (uint64_t)(4608 * 1024 * 1024 * i), // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} overflow4 = (1ULL * ((4608) * ((1024) * (1024))) + 2ULL), // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} overflow5 = static_cast<uint64_t>(4608 * 1024 * 1024), // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}} multi_overflow = (uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)); // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} overflow += overflow2 = overflow3 = (uint64_t)(4608 * 1024 * 1024); // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} overflow += overflow2 = overflow3 = 4608 * 1024 * 1024; // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} overflow += overflow2 = overflow3 = static_cast<uint64_t>(4608 * 1024 * 1024); uint64_t not_overflow = 4608 * 1024 * 1024ULL; uint64_t not_overflow2 = (1ULL * ((uint64_t)(4608) * (1024 * 1024)) + 2ULL); // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}} overflow = 4608 * 1024 * 1024 ? 4608 * 1024 * 1024 : 0; // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} overflow = 0 ? 0 : 4608 * 1024 * 1024; // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} if (4608 * 1024 * 1024) return 0; // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} if ((uint64_t)(4608 * 1024 * 1024)) return 1; // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} if (static_cast<uint64_t>(4608 * 1024 * 1024)) return 1; // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} if ((uint64_t)(4608 * 1024 * 1024)) return 2; // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} if ((uint64_t)(4608 * 1024 * 1024 * i)) return 3; // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} if ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL)) return 4; // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}} if ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024))) return 5; switch (i) { // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} case 4608 * 1024 * 1024: return 6; // expected-warning@+1 {{overflow in expression; result is 537919488 with type 'int'}} case (uint64_t)(4609 * 1024 * 1024): return 7; // expected-warning@+1 {{overflow in expression; result is 537919488 with type 'int'}} case 1 + static_cast<uint64_t>(4609 * 1024 * 1024): return 7; // expected-error@+2 {{expression is not an integral constant expression}} // expected-note@+1 {{read of non-const variable 'i' is not allowed in a constant expression}} case ((uint64_t)(4608 * 1024 * 1024 * i)): return 8; // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} case ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL)): return 9; // expected-warning@+2 2{{overflow in expression; result is 536870912 with type 'int'}} // expected-warning@+1 {{overflow converting case value to switch condition type (288230376151711744 to 0)}} case ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024))): return 10; } // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} while (4608 * 1024 * 1024); // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} while ((uint64_t)(4608 * 1024 * 1024)); // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} while (static_cast<uint64_t>(4608 * 1024 * 1024)); // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} while ((uint64_t)(4608 * 1024 * 1024)); // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} while ((uint64_t)(4608 * 1024 * 1024 * i)); // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} while ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL)); // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}} while ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024))); // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} do { } while (4608 * 1024 * 1024); // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} do { } while ((uint64_t)(4608 * 1024 * 1024)); // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} do { } while (static_cast<uint64_t>(4608 * 1024 * 1024)); // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} do { } while ((uint64_t)(4608 * 1024 * 1024)); // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} do { } while ((uint64_t)(4608 * 1024 * 1024 * i)); // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} do { } while ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL)); // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}} do { } while ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024))); // expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}} // expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}} // expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}} for (uint64_t i = 4608 * 1024 * 1024; (uint64_t)(4608 * 1024 * 1024); i += (uint64_t)(4608 * 1024 * 1024 * i)); // expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}} // expected-warning@+3 2{{overflow in expression; result is 536870912 with type 'int'}} // expected-warning@+3 2{{overflow in expression; result is 536870912 with type 'int'}} for (uint64_t i = (1ULL * ((4608) * ((1024) * (1024))) + 2ULL); ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024))); i = ((4608 * 1024 * 1024) + ((uint64_t)(4608 * 1024 * 1024)))); // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} _Complex long long x = 4608 * 1024 * 1024; // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} (__real__ x) = 4608 * 1024 * 1024; // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} (__imag__ x) = 4608 * 1024 * 1024; // expected-warning@+4 {{overflow in expression; result is 536870912 with type 'int'}} // expected-warning@+3 {{array index 536870912 is past the end of the array (which contains 10 elements)}} // expected-note@+1 {{array 'a' declared here}} uint64_t a[10]; a[4608 * 1024 * 1024] = 1i; // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}} return ((4608 * 1024 * 1024) + ((uint64_t)(4608 * 1024 * 1024))); }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/inline.cpp
// RUN: %clang_cc1 -fsyntax-only -verify %s // Check that we don't allow illegal uses of inline // (checking C++-only constructs here) struct c {inline int a;}; // expected-error{{'inline' can only appear on functions}}
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/unused-with-error.cpp
// RUN: %clang_cc1 -fsyntax-only -Wunused -verify %s // Make sure 'unused' warnings are disabled when errors occurred. static void foo(int *X) { // expected-note {{candidate}} } void bar(const int *Y) { foo(Y); // expected-error {{no matching function for call}} }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/constexpr-steps.cpp
// RUN: %clang_cc1 -std=c++1y -fsyntax-only -verify %s -DMAX=1234 -fconstexpr-steps 1234 // RUN: %clang_cc1 -std=c++1y -fsyntax-only -verify %s -DMAX=10 -fconstexpr-steps 10 // RUN: %clang -std=c++1y -fsyntax-only -Xclang -verify %s -DMAX=12345 -fconstexpr-steps=12345 // This takes a total of n + 4 steps according to our current rules: // - One for the compound-statement that is the function body // - One for the 'for' statement // - One for the 'int k = 0;' statement // - One for each of the n evaluations of the compound-statement in the 'for' body // - One for the 'return' statemnet constexpr bool steps(int n) { for (int k = 0; k != n; ++k) {} return true; // expected-note {{step limit}} } static_assert(steps((MAX - 4)), ""); // ok static_assert(steps((MAX - 3)), ""); // expected-error {{constant}} expected-note{{call}}
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/complex-overload.cpp
// RUN: %clang_cc1 -fsyntax-only -verify %s char *foo(float); void test_foo_1(float fv, double dv, float _Complex fc, double _Complex dc) { char *cp1 = foo(fv); char *cp2 = foo(dv); // Note: GCC and EDG reject these two, but they are valid C99 conversions char *cp3 = foo(fc); char *cp4 = foo(dc); } int *foo(float _Complex); void test_foo_2(float fv, double dv, float _Complex fc, double _Complex dc) { char *cp1 = foo(fv); char *cp2 = foo(dv); int *ip = foo(fc); int *lp = foo(dc); } long *foo(double _Complex); void test_foo_3(float fv, double dv, float _Complex fc, double _Complex dc) { char *cp1 = foo(fv); char *cp2 = foo(dv); int *ip = foo(fc); long *lp = foo(dc); } char *promote_or_convert(double _Complex); // expected-note{{candidate function}} int *promote_or_convert(long double _Complex); // expected-note{{candidate function}} void test_promote_or_convert(float f, float _Complex fc) { char *cp = promote_or_convert(fc); int *ip2 = promote_or_convert(f); // expected-error{{call to 'promote_or_convert' is ambiguous}} } char *promote_or_convert2(float); int *promote_or_convert2(double _Complex); void test_promote_or_convert2(float _Complex fc) { int *cp = promote_or_convert2(fc); } char *promote_or_convert3(int _Complex); // expected-note {{candidate}} int *promote_or_convert3(long _Complex); // expected-note {{candidate}} void test_promote_or_convert3(short _Complex sc) { char *cp1 = promote_or_convert3(sc); char *cp2 = promote_or_convert3(1i); int *cp3 = promote_or_convert3(1il); int *cp4 = promote_or_convert3(1ill); // expected-error {{ambiguous}} } char &convert4(short _Complex); char &test_convert4 = convert4(1i);
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/ref-init-ambiguous.cpp
// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11 enum E2 { }; struct A { operator E2&(); // expected-note 3 {{candidate function}} }; struct B { operator E2&(); // expected-note 3 {{candidate function}} }; struct C : B, A { }; void test(C c) { const E2 &e2 = c; // expected-error {{reference initialization of type 'const E2 &' with initializer of type 'C' is ambiguous}} } void foo(const E2 &);// expected-note{{passing argument to parameter here}} const E2 & re(C c) { foo(c); // expected-error {{reference initialization of type 'const E2 &' with initializer of type 'C' is ambiguous}} return c; // expected-error {{reference initialization of type 'const E2 &' with initializer of type 'C' is ambiguous}} }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/cxx0x-return-init-list.cpp
// RUN: %clang_cc1 -fsyntax-only -verify %s // Test that a very basic variation of generalized initializer returns (that // required for libstdc++ 4.5) is supported in C++98. int test0(int i) { return { i }; // expected-warning{{generalized initializer lists are a C++11 extension}} expected-warning {{scalar}} } template<typename T, typename U> T test1(U u) { return { u }; // expected-warning{{generalized initializer lists are a C++11 extension}} } template int test1(char); template long test1(int);
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/MicrosoftSuper.cpp
// RUN: %clang_cc1 %s -fsyntax-only -fms-extensions -std=c++11 -verify struct Errors { using __super::foo; // expected-error {{'__super' cannot be used with a using declaration}} __super::XXX x; // expected-error {{invalid use of '__super', Errors has no base classes}} expected-error {{expected}} void foo() { // expected-note@+4 {{replace parentheses with an initializer to declare a variable}} // expected-warning@+3 {{empty parentheses interpreted as a function declaration}} // expected-error@+2 {{C++ requires a type specifier for all declarations}} // expected-error@+1 {{use of '__super' inside a lambda is unsupported}} auto lambda = []{ __super::foo(); }; } }; struct Base1 { void foo(int) {} static void static_foo() {} typedef int XXX; }; struct Derived : Base1 { __super::XXX x; typedef __super::XXX Type; enum E { X = sizeof(__super::XXX) }; void foo() { __super::foo(1); if (true) { __super::foo(1); } return __super::foo(1); } static void bar() { __super::static_foo(); } }; struct Outer { struct Inner : Base1 { static const int x = sizeof(__super::XXX); }; }; struct Base2 { void foo(char) {} }; struct MemberFunctionInMultipleBases : Base1, Base2 { void foo() { __super::foo('x'); } }; struct Base3 { void foo(int) {} void foo(char) {} }; struct OverloadedMemberFunction : Base3 { void foo() { __super::foo('x'); } }; struct PointerToMember : Base1 { template <void (Base1::*MP)(int)> struct Wrapper { static void bar() {} }; void baz(); }; void PointerToMember::baz() { Wrapper<&__super::foo>::bar(); } template <typename T> struct BaseTemplate { typedef int XXX; int foo() { return 0; } }; struct DerivedFromKnownSpecialization : BaseTemplate<int> { __super::XXX a; typedef __super::XXX b; void foo() { __super::XXX c; typedef __super::XXX d; __super::foo(); } }; template <typename T> struct DerivedFromDependentBase : BaseTemplate<T> { typename __super::XXX a; typedef typename __super::XXX b; __super::XXX c; // expected-error {{missing 'typename'}} typedef __super::XXX d; // expected-error {{missing 'typename'}} void foo() { typename __super::XXX e; typedef typename __super::XXX f; __super::XXX g; // expected-error {{missing 'typename'}} typedef __super::XXX h; // expected-error {{missing 'typename'}} int x = __super::foo(); } }; template <typename T> struct DerivedFromTemplateParameter : T { typename __super::XXX a; typedef typename __super::XXX b; __super::XXX c; // expected-error {{missing 'typename'}} typedef __super::XXX d; // expected-error {{missing 'typename'}} void foo() { typename __super::XXX e; typedef typename __super::XXX f; __super::XXX g; // expected-error {{missing 'typename'}} typedef __super::XXX h; // expected-error {{missing 'typename'}} __super::foo(1); } }; void instantiate() { DerivedFromDependentBase<int> d; d.foo(); DerivedFromTemplateParameter<Base1> t; t.foo(); } namespace { struct B { int a; }; template <class C> struct A : B { // Don't crash on dependent_type_var '->' '__super' void f() { int a = this->__super::a; } }; }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/decl-microsoft-call-conv.cpp
// RUN: %clang_cc1 -std=c++14 -triple i686-pc-win32 -fms-extensions -verify %s // RUN: %clang_cc1 -std=c++14 -triple i686-pc-mingw32 -verify %s // RUN: %clang_cc1 -std=c++14 -triple i686-pc-mingw32 -fms-extensions -verify %s typedef void void_fun_t(); typedef void __cdecl cdecl_fun_t(); // Pointers to free functions void free_func_default(); // expected-note 2 {{previous declaration is here}} void __cdecl free_func_cdecl(); // expected-note 2 {{previous declaration is here}} void __stdcall free_func_stdcall(); // expected-note 2 {{previous declaration is here}} void __fastcall free_func_fastcall(); // expected-note 2 {{previous declaration is here}} void __vectorcall free_func_vectorcall(); // expected-note 2 {{previous declaration is here}} void __cdecl free_func_default(); void __stdcall free_func_default(); // expected-error {{function declared 'stdcall' here was previously declared without calling convention}} void __fastcall free_func_default(); // expected-error {{function declared 'fastcall' here was previously declared without calling convention}} void free_func_cdecl(); void __stdcall free_func_cdecl(); // expected-error {{function declared 'stdcall' here was previously declared 'cdecl'}} void __fastcall free_func_cdecl(); // expected-error {{function declared 'fastcall' here was previously declared 'cdecl'}} void free_func_stdcall(); void __cdecl free_func_stdcall(); // expected-error {{function declared 'cdecl' here was previously declared 'stdcall'}} void __fastcall free_func_stdcall(); // expected-error {{function declared 'fastcall' here was previously declared 'stdcall'}} void __cdecl free_func_fastcall(); // expected-error {{function declared 'cdecl' here was previously declared 'fastcall'}} void __stdcall free_func_fastcall(); // expected-error {{function declared 'stdcall' here was previously declared 'fastcall'}} void free_func_fastcall(); void __cdecl free_func_vectorcall(); // expected-error {{function declared 'cdecl' here was previously declared 'vectorcall'}} void __stdcall free_func_vectorcall(); // expected-error {{function declared 'stdcall' here was previously declared 'vectorcall'}} void free_func_vectorcall(); // Overloaded functions may have different calling conventions void __fastcall free_func_default(int); void __cdecl free_func_default(int *); void __thiscall free_func_cdecl(char *); void __cdecl free_func_cdecl(double); typedef void void_fun_t(); typedef void __cdecl cdecl_fun_t(); // Pointers to member functions struct S { void member_default1(); // expected-note {{previous declaration is here}} void member_default2(); void __cdecl member_cdecl1(); void __cdecl member_cdecl2(); // expected-note {{previous declaration is here}} void __thiscall member_thiscall1(); void __thiscall member_thiscall2(); // expected-note {{previous declaration is here}} void __vectorcall member_vectorcall1(); void __vectorcall member_vectorcall2(); // expected-note {{previous declaration is here}} // Typedefs carrying the __cdecl convention are adjusted to __thiscall. void_fun_t member_typedef_default; // expected-note {{previous declaration is here}} cdecl_fun_t member_typedef_cdecl1; // expected-note {{previous declaration is here}} cdecl_fun_t __cdecl member_typedef_cdecl2; void_fun_t __stdcall member_typedef_stdcall; // Static member functions can't be __thiscall static void static_member_default1(); static void static_member_default2(); static void static_member_default3(); // expected-note {{previous declaration is here}} static void __cdecl static_member_cdecl1(); static void __cdecl static_member_cdecl2(); // expected-note {{previous declaration is here}} static void __stdcall static_member_stdcall1(); static void __stdcall static_member_stdcall2(); // Variadic functions can't be other than default or __cdecl void member_variadic_default(int x, ...); void __cdecl member_variadic_cdecl(int x, ...); static void static_member_variadic_default(int x, ...); static void __cdecl static_member_variadic_cdecl(int x, ...); }; void __cdecl S::member_default1() {} // expected-error {{function declared 'cdecl' here was previously declared without calling convention}} void __thiscall S::member_default2() {} void __cdecl S::member_typedef_default() {} // expected-error {{function declared 'cdecl' here was previously declared without calling convention}} void __cdecl S::member_typedef_cdecl1() {} // expected-error {{function declared 'cdecl' here was previously declared without calling convention}} void __cdecl S::member_typedef_cdecl2() {} void __stdcall S::member_typedef_stdcall() {} void S::member_cdecl1() {} void __thiscall S::member_cdecl2() {} // expected-error {{function declared 'thiscall' here was previously declared 'cdecl'}} void S::member_thiscall1() {} void __cdecl S::member_thiscall2() {} // expected-error {{function declared 'cdecl' here was previously declared 'thiscall'}} void S::member_vectorcall1() {} void __cdecl S::member_vectorcall2() {} // expected-error {{function declared 'cdecl' here was previously declared 'vectorcall'}} void S::static_member_default1() {} void __cdecl S::static_member_default2() {} void __stdcall S::static_member_default3() {} // expected-error {{function declared 'stdcall' here was previously declared without calling convention}} void S::static_member_cdecl1() {} void __stdcall S::static_member_cdecl2() {} // expected-error {{function declared 'stdcall' here was previously declared 'cdecl'}} void __cdecl S::member_variadic_default(int x, ...) { (void)x; } void S::member_variadic_cdecl(int x, ...) { (void)x; } void __cdecl S::static_member_variadic_default(int x, ...) { (void)x; } void S::static_member_variadic_cdecl(int x, ...) { (void)x; } // Declare a template using a calling convention. template <class CharT> inline int __cdecl mystrlen(const CharT *str) { int i; for (i = 0; str[i]; i++) { } return i; } extern int sse_strlen(const char *str); template <> inline int __cdecl mystrlen(const char *str) { return sse_strlen(str); } void use_tmpl(const char *str, const int *ints) { mystrlen(str); mystrlen(ints); } struct MixedCCStaticOverload { static void overloaded(int a); static void __stdcall overloaded(short a); }; void MixedCCStaticOverload::overloaded(int a) {} void MixedCCStaticOverload::overloaded(short a) {} // Friend function decls are cdecl by default, not thiscall. Friend method // decls should always be redeclarations, because the class cannot be // incomplete. struct FriendClass { void friend_method() {} }; void __stdcall friend_stdcall1() {} class MakeFriendDecls { int x; friend void FriendClass::friend_method(); friend void friend_default(); friend void friend_stdcall1(); friend void __stdcall friend_stdcall2(); friend void friend_stdcall3(); // expected-note {{previous declaration is here}} }; void friend_default() {} void __stdcall friend_stdcall3() {} // expected-error {{function declared 'stdcall' here was previously declared without calling convention}} void __stdcall friend_stdcall2() {} // Test functions with multiple attributes. void __attribute__((noreturn)) __stdcall __attribute__((regparm(1))) multi_attribute(int x); void multi_attribute(int x) { __builtin_unreachable(); } // expected-error@+3 {{vectorcall and cdecl attributes are not compatible}} // expected-error@+2 {{stdcall and cdecl attributes are not compatible}} // expected-error@+1 {{fastcall and cdecl attributes are not compatible}} void __cdecl __cdecl __stdcall __cdecl __fastcall __vectorcall multi_cc(int x); template <typename T> void __stdcall StdcallTemplate(T) {} template <> void StdcallTemplate<int>(int) {} template <> void __stdcall StdcallTemplate<short>(short) {} // FIXME: Note the template, not the implicit instantiation. // expected-error@+2 {{function declared 'cdecl' here was previously declared 'stdcall}} // expected-note@+1 {{previous declaration is here}} template <> void __cdecl StdcallTemplate<long>(long) {} struct ExactlyInt { template <typename T> static int cast_to_int(T) { return T::this_is_not_an_int(); } }; template <> inline int ExactlyInt::cast_to_int<int>(int x) { return x; } namespace test2 { class foo { template <typename T> void bar(T v); }; extern template void foo::bar(const void *); } namespace test3 { struct foo { typedef void bar(); }; bool zed(foo::bar *); void bah() {} void baz() { zed(bah); } } namespace test4 { class foo { template <typename T> static void bar(T v); }; extern template void foo::bar(const void *); } namespace test5 { template <class T> class valarray { void bar(); }; extern template void valarray<int>::bar(); } namespace test6 { struct foo { int bar(); }; typedef int bar_t(); void zed(bar_t foo::*) { } void baz() { zed(&foo::bar); } } namespace test7 { template <typename T> struct S { void f(T t) { t = 42; } }; template<> void S<void*>::f(void*); void g(S<void*> s, void* p) { s.f(p); } } namespace test8 { template <typename T> struct S { void f(T t) { // expected-note {{previous declaration is here}} t = 42; // expected-error {{assigning to 'void *' from incompatible type 'int'}} } }; template<> void __cdecl S<void*>::f(void*); // expected-error {{function declared 'cdecl' here was previously declared without calling convention}} void g(S<void*> s, void* p) { s.f(p); // expected-note {{in instantiation of member function 'test8::S<void *>::f' requested here}} } } namespace test9 { // Used to fail when we forgot to make lambda call operators use __thiscall. template <typename F> decltype(auto) deduce(F f) { return &decltype(f)::operator(); } template <typename C, typename R, typename A> decltype(auto) signaturehelper(R (C::*f)(A) const) { return R(); } void f() { auto l = [](int x) { return x * 2; }; decltype(signaturehelper(deduce(l))) p; } }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/function-redecl.cpp
// RUN: %clang_cc1 -fsyntax-only -verify %s int foo(int); namespace N { void f1() { void foo(int); // okay void bar(int); // expected-note 2{{previous declaration is here}} } void foo(int); // expected-note 2{{previous declaration is here}} void f2() { int foo(int); // expected-error {{functions that differ only in their return type cannot be overloaded}} int bar(int); // expected-error {{functions that differ only in their return type cannot be overloaded}} int baz(int); // expected-note {{previous declaration is here}} { int foo; int bar; int baz; { float foo(int); // expected-error {{functions that differ only in their return type cannot be overloaded}} float bar(int); // expected-error {{functions that differ only in their return type cannot be overloaded}} float baz(int); // expected-error {{functions that differ only in their return type cannot be overloaded}} } } } } class A { void typocorrection(); // expected-note {{'typocorrection' declared here}} }; void A::Notypocorrection() { // expected-error {{out-of-line definition of 'Notypocorrection' does not match any declaration in 'A'; did you mean 'typocorrection'}} } namespace test0 { void dummy() { void Bar(); // expected-note {{'Bar' declared here}} class A { friend void bar(); // expected-error {{no matching function 'bar' found in local scope; did you mean 'Bar'}} }; } } class B { void typocorrection(const int); // expected-note {{'typocorrection' declared here}} void typocorrection(double); }; void B::Notypocorrection(int) { // expected-error {{out-of-line definition of 'Notypocorrection' does not match any declaration in 'B'; did you mean 'typocorrection'}} } struct X { int f(); }; struct Y : public X {}; int Y::f() { return 3; } // expected-error {{out-of-line definition of 'f' does not match any declaration in 'Y'}} namespace test1 { struct Foo { class Inner { }; }; } class Bar { void f(test1::Foo::Inner foo) const; // expected-note {{member declaration does not match because it is const qualified}} }; using test1::Foo; void Bar::f(Foo::Inner foo) { // expected-error {{out-of-line definition of 'f' does not match any declaration in 'Bar'}} (void)foo; } class Crash { public: void GetCart(int count) const; }; // This out-of-line definition was fine... void Crash::cart(int count) const {} // expected-error {{out-of-line definition of 'cart' does not match any declaration in 'Crash'}} // ...while this one crashed clang void Crash::chart(int count) const {} // expected-error {{out-of-line definition of 'chart' does not match any declaration in 'Crash'}} class TestConst { public: int getit() const; // expected-note {{member declaration does not match because it is const qualified}} void setit(int); // expected-note {{member declaration does not match because it is not const qualified}} }; int TestConst::getit() { // expected-error {{out-of-line definition of 'getit' does not match any declaration in 'TestConst'}} return 1; } void TestConst::setit(int) const { // expected-error {{out-of-line definition of 'setit' does not match any declaration in 'TestConst'}} } struct J { int typo() const; }; int J::typo_() { return 3; } // expected-error {{out-of-line definition of 'typo_' does not match any declaration in 'J'}} // Ensure we correct the redecl of Foo::isGood to Bar::Foo::isGood and not // Foo::IsGood even though Foo::IsGood is technically a closer match since it // already has a body. Also make sure Foo::beEvil is corrected to Foo::BeEvil // since it is a closer match than Bar::Foo::beEvil and neither have a body. namespace redecl_typo { namespace Foo { bool IsGood() { return false; } void BeEvil(); // expected-note {{'BeEvil' declared here}} } namespace Bar { namespace Foo { bool isGood(); // expected-note {{'Bar::Foo::isGood' declared here}} void beEvil(); } } bool Foo::isGood() { // expected-error {{out-of-line definition of 'isGood' does not match any declaration in namespace 'redecl_typo::Foo'; did you mean 'Bar::Foo::isGood'?}} return true; } void Foo::beEvil() {} // expected-error {{out-of-line definition of 'beEvil' does not match any declaration in namespace 'redecl_typo::Foo'; did you mean 'BeEvil'?}} }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/warn-thread-safety-analysis.cpp
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wthread-safety -Wthread-safety-beta -Wno-thread-safety-negative -fcxx-exceptions %s // FIXME: should also run %clang_cc1 -fsyntax-only -verify -Wthread-safety -std=c++11 -Wc++98-compat %s // FIXME: should also run %clang_cc1 -fsyntax-only -verify -Wthread-safety %s #define LOCKABLE __attribute__((lockable)) #define SCOPED_LOCKABLE __attribute__((scoped_lockable)) #define GUARDED_BY(x) __attribute__((guarded_by(x))) #define GUARDED_VAR __attribute__((guarded_var)) #define PT_GUARDED_BY(x) __attribute__((pt_guarded_by(x))) #define PT_GUARDED_VAR __attribute__((pt_guarded_var)) #define ACQUIRED_AFTER(...) __attribute__((acquired_after(__VA_ARGS__))) #define ACQUIRED_BEFORE(...) __attribute__((acquired_before(__VA_ARGS__))) #define EXCLUSIVE_LOCK_FUNCTION(...) __attribute__((exclusive_lock_function(__VA_ARGS__))) #define SHARED_LOCK_FUNCTION(...) __attribute__((shared_lock_function(__VA_ARGS__))) #define ASSERT_EXCLUSIVE_LOCK(...) __attribute__((assert_exclusive_lock(__VA_ARGS__))) #define ASSERT_SHARED_LOCK(...) __attribute__((assert_shared_lock(__VA_ARGS__))) #define EXCLUSIVE_TRYLOCK_FUNCTION(...) __attribute__((exclusive_trylock_function(__VA_ARGS__))) #define SHARED_TRYLOCK_FUNCTION(...) __attribute__((shared_trylock_function(__VA_ARGS__))) #define UNLOCK_FUNCTION(...) __attribute__((unlock_function(__VA_ARGS__))) #define EXCLUSIVE_UNLOCK_FUNCTION(...) __attribute__((release_capability(__VA_ARGS__))) #define SHARED_UNLOCK_FUNCTION(...) __attribute__((release_shared_capability(__VA_ARGS__))) #define LOCK_RETURNED(x) __attribute__((lock_returned(x))) #define LOCKS_EXCLUDED(...) __attribute__((locks_excluded(__VA_ARGS__))) #define EXCLUSIVE_LOCKS_REQUIRED(...) __attribute__((exclusive_locks_required(__VA_ARGS__))) #define SHARED_LOCKS_REQUIRED(...) __attribute__((shared_locks_required(__VA_ARGS__))) #define NO_THREAD_SAFETY_ANALYSIS __attribute__((no_thread_safety_analysis)) class LOCKABLE Mutex { public: void Lock() __attribute__((exclusive_lock_function)); void ReaderLock() __attribute__((shared_lock_function)); void Unlock() __attribute__((unlock_function)); bool TryLock() __attribute__((exclusive_trylock_function(true))); bool ReaderTryLock() __attribute__((shared_trylock_function(true))); void LockWhen(const int &cond) __attribute__((exclusive_lock_function)); // for negative capabilities const Mutex& operator!() const { return *this; } void AssertHeld() ASSERT_EXCLUSIVE_LOCK(); void AssertReaderHeld() ASSERT_SHARED_LOCK(); }; class SCOPED_LOCKABLE MutexLock { public: MutexLock(Mutex *mu) EXCLUSIVE_LOCK_FUNCTION(mu); MutexLock(Mutex *mu, bool adopt) EXCLUSIVE_LOCKS_REQUIRED(mu); ~MutexLock() UNLOCK_FUNCTION(); }; class SCOPED_LOCKABLE ReaderMutexLock { public: ReaderMutexLock(Mutex *mu) SHARED_LOCK_FUNCTION(mu); ReaderMutexLock(Mutex *mu, bool adopt) SHARED_LOCKS_REQUIRED(mu); ~ReaderMutexLock() UNLOCK_FUNCTION(); }; class SCOPED_LOCKABLE ReleasableMutexLock { public: ReleasableMutexLock(Mutex *mu) EXCLUSIVE_LOCK_FUNCTION(mu); ~ReleasableMutexLock() UNLOCK_FUNCTION(); void Release() UNLOCK_FUNCTION(); }; class __attribute__((scoped_lockable)) DoubleMutexLock { public: DoubleMutexLock(Mutex *mu1, Mutex *mu2) __attribute__((exclusive_lock_function(mu1, mu2))); ~DoubleMutexLock() __attribute__((unlock_function)); }; // The universal lock, written "*", allows checking to be selectively turned // off for a particular piece of code. void beginNoWarnOnReads() SHARED_LOCK_FUNCTION("*"); void endNoWarnOnReads() UNLOCK_FUNCTION("*"); void beginNoWarnOnWrites() EXCLUSIVE_LOCK_FUNCTION("*"); void endNoWarnOnWrites() UNLOCK_FUNCTION("*"); // For testing handling of smart pointers. template<class T> class SmartPtr { public: SmartPtr(T* p) : ptr_(p) { } SmartPtr(const SmartPtr<T>& p) : ptr_(p.ptr_) { } ~SmartPtr(); T* get() const { return ptr_; } T* operator->() const { return ptr_; } T& operator*() const { return *ptr_; } T& operator[](int i) const { return ptr_[i]; } private: T* ptr_; }; // For testing destructor calls and cleanup. class MyString { public: MyString(const char* s); ~MyString(); }; // For testing operator overloading template <class K, class T> class MyMap { public: T& operator[](const K& k); }; // For testing handling of containers. template <class T> class MyContainer { public: MyContainer(); typedef T* iterator; typedef const T* const_iterator; T* begin(); T* end(); const T* cbegin(); const T* cend(); T& operator[](int i); const T& operator[](int i) const; private: T* ptr_; }; Mutex sls_mu; Mutex sls_mu2 __attribute__((acquired_after(sls_mu))); int sls_guard_var __attribute__((guarded_var)) = 0; int sls_guardby_var __attribute__((guarded_by(sls_mu))) = 0; bool getBool(); class MutexWrapper { public: Mutex mu; int x __attribute__((guarded_by(mu))); void MyLock() __attribute__((exclusive_lock_function(mu))); }; MutexWrapper sls_mw; void sls_fun_0() { sls_mw.mu.Lock(); sls_mw.x = 5; sls_mw.mu.Unlock(); } void sls_fun_2() { sls_mu.Lock(); int x = sls_guard_var; sls_mu.Unlock(); } void sls_fun_3() { sls_mu.Lock(); sls_guard_var = 2; sls_mu.Unlock(); } void sls_fun_4() { sls_mu2.Lock(); sls_guard_var = 2; sls_mu2.Unlock(); } void sls_fun_5() { sls_mu.Lock(); int x = sls_guardby_var; sls_mu.Unlock(); } void sls_fun_6() { sls_mu.Lock(); sls_guardby_var = 2; sls_mu.Unlock(); } void sls_fun_7() { sls_mu.Lock(); sls_mu2.Lock(); sls_mu2.Unlock(); sls_mu.Unlock(); } void sls_fun_8() { sls_mu.Lock(); if (getBool()) sls_mu.Unlock(); else sls_mu.Unlock(); } void sls_fun_9() { if (getBool()) sls_mu.Lock(); else sls_mu.Lock(); sls_mu.Unlock(); } void sls_fun_good_6() { if (getBool()) { sls_mu.Lock(); } else { if (getBool()) { getBool(); // EMPTY } else { getBool(); // EMPTY } sls_mu.Lock(); } sls_mu.Unlock(); } void sls_fun_good_7() { sls_mu.Lock(); while (getBool()) { sls_mu.Unlock(); if (getBool()) { if (getBool()) { sls_mu.Lock(); continue; } } sls_mu.Lock(); } sls_mu.Unlock(); } void sls_fun_good_8() { sls_mw.MyLock(); sls_mw.mu.Unlock(); } void sls_fun_bad_1() { sls_mu.Unlock(); // \ // expected-warning{{releasing mutex 'sls_mu' that was not held}} } void sls_fun_bad_2() { sls_mu.Lock(); sls_mu.Lock(); // \ // expected-warning{{acquiring mutex 'sls_mu' that is already held}} sls_mu.Unlock(); } void sls_fun_bad_3() { sls_mu.Lock(); // expected-note {{mutex acquired here}} } // expected-warning{{mutex 'sls_mu' is still held at the end of function}} void sls_fun_bad_4() { if (getBool()) sls_mu.Lock(); // expected-note{{mutex acquired here}} else sls_mu2.Lock(); // expected-note{{mutex acquired here}} } // expected-warning{{mutex 'sls_mu' is not held on every path through here}} \ // expected-warning{{mutex 'sls_mu2' is not held on every path through here}} void sls_fun_bad_5() { sls_mu.Lock(); // expected-note {{mutex acquired here}} if (getBool()) sls_mu.Unlock(); } // expected-warning{{mutex 'sls_mu' is not held on every path through here}} void sls_fun_bad_6() { if (getBool()) { sls_mu.Lock(); // expected-note {{mutex acquired here}} } else { if (getBool()) { getBool(); // EMPTY } else { getBool(); // EMPTY } } sls_mu.Unlock(); // \ expected-warning{{mutex 'sls_mu' is not held on every path through here}}\ expected-warning{{releasing mutex 'sls_mu' that was not held}} } void sls_fun_bad_7() { sls_mu.Lock(); while (getBool()) { sls_mu.Unlock(); if (getBool()) { if (getBool()) { continue; // \ expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}} } } sls_mu.Lock(); // expected-note {{mutex acquired here}} } sls_mu.Unlock(); } void sls_fun_bad_8() { sls_mu.Lock(); // expected-note{{mutex acquired here}} do { sls_mu.Unlock(); // expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}} } while (getBool()); } void sls_fun_bad_9() { do { sls_mu.Lock(); // \ // expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}} \ // expected-note{{mutex acquired here}} } while (getBool()); sls_mu.Unlock(); } void sls_fun_bad_10() { sls_mu.Lock(); // expected-note 2{{mutex acquired here}} while(getBool()) { // expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}} sls_mu.Unlock(); } } // expected-warning{{mutex 'sls_mu' is still held at the end of function}} void sls_fun_bad_11() { while (getBool()) { // \ expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}} sls_mu.Lock(); // expected-note {{mutex acquired here}} } sls_mu.Unlock(); // \ // expected-warning{{releasing mutex 'sls_mu' that was not held}} } void sls_fun_bad_12() { sls_mu.Lock(); // expected-note {{mutex acquired here}} while (getBool()) { sls_mu.Unlock(); if (getBool()) { if (getBool()) { break; // expected-warning{{mutex 'sls_mu' is not held on every path through here}} } } sls_mu.Lock(); } sls_mu.Unlock(); } //-----------------------------------------// // Handling lock expressions in attribute args // -------------------------------------------// Mutex aa_mu; class GlobalLocker { public: void globalLock() __attribute__((exclusive_lock_function(aa_mu))); void globalUnlock() __attribute__((unlock_function(aa_mu))); }; GlobalLocker glock; void aa_fun_1() { glock.globalLock(); glock.globalUnlock(); } void aa_fun_bad_1() { glock.globalUnlock(); // \ // expected-warning{{releasing mutex 'aa_mu' that was not held}} } void aa_fun_bad_2() { glock.globalLock(); glock.globalLock(); // \ // expected-warning{{acquiring mutex 'aa_mu' that is already held}} glock.globalUnlock(); } void aa_fun_bad_3() { glock.globalLock(); // expected-note{{mutex acquired here}} } // expected-warning{{mutex 'aa_mu' is still held at the end of function}} //--------------------------------------------------// // Regression tests for unusual method names //--------------------------------------------------// Mutex wmu; // Test diagnostics for other method names. class WeirdMethods { // FIXME: can't currently check inside constructors and destructors. WeirdMethods() { wmu.Lock(); // EXPECTED-NOTE {{mutex acquired here}} } // EXPECTED-WARNING {{mutex 'wmu' is still held at the end of function}} ~WeirdMethods() { wmu.Lock(); // EXPECTED-NOTE {{mutex acquired here}} } // EXPECTED-WARNING {{mutex 'wmu' is still held at the end of function}} void operator++() { wmu.Lock(); // expected-note {{mutex acquired here}} } // expected-warning {{mutex 'wmu' is still held at the end of function}} operator int*() { wmu.Lock(); // expected-note {{mutex acquired here}} return 0; } // expected-warning {{mutex 'wmu' is still held at the end of function}} }; //-----------------------------------------------// // Errors for guarded by or guarded var variables // ----------------------------------------------// int *pgb_gvar __attribute__((pt_guarded_var)); int *pgb_var __attribute__((pt_guarded_by(sls_mu))); class PGBFoo { public: int x; int *pgb_field __attribute__((guarded_by(sls_mu2))) __attribute__((pt_guarded_by(sls_mu))); void testFoo() { pgb_field = &x; // \ // expected-warning {{writing variable 'pgb_field' requires holding mutex 'sls_mu2' exclusively}} *pgb_field = x; // expected-warning {{reading variable 'pgb_field' requires holding mutex 'sls_mu2'}} \ // expected-warning {{writing the value pointed to by 'pgb_field' requires holding mutex 'sls_mu' exclusively}} x = *pgb_field; // expected-warning {{reading variable 'pgb_field' requires holding mutex 'sls_mu2'}} \ // expected-warning {{reading the value pointed to by 'pgb_field' requires holding mutex 'sls_mu'}} (*pgb_field)++; // expected-warning {{reading variable 'pgb_field' requires holding mutex 'sls_mu2'}} \ // expected-warning {{writing the value pointed to by 'pgb_field' requires holding mutex 'sls_mu' exclusively}} } }; class GBFoo { public: int gb_field __attribute__((guarded_by(sls_mu))); void testFoo() { gb_field = 0; // \ // expected-warning {{writing variable 'gb_field' requires holding mutex 'sls_mu' exclusively}} } void testNoAnal() __attribute__((no_thread_safety_analysis)) { gb_field = 0; } }; GBFoo GlobalGBFoo __attribute__((guarded_by(sls_mu))); void gb_fun_0() { sls_mu.Lock(); int x = *pgb_var; sls_mu.Unlock(); } void gb_fun_1() { sls_mu.Lock(); *pgb_var = 2; sls_mu.Unlock(); } void gb_fun_2() { int x; pgb_var = &x; } void gb_fun_3() { int *x = pgb_var; } void gb_bad_0() { sls_guard_var = 1; // \ // expected-warning{{writing variable 'sls_guard_var' requires holding any mutex exclusively}} } void gb_bad_1() { int x = sls_guard_var; // \ // expected-warning{{reading variable 'sls_guard_var' requires holding any mutex}} } void gb_bad_2() { sls_guardby_var = 1; // \ // expected-warning {{writing variable 'sls_guardby_var' requires holding mutex 'sls_mu' exclusively}} } void gb_bad_3() { int x = sls_guardby_var; // \ // expected-warning {{reading variable 'sls_guardby_var' requires holding mutex 'sls_mu'}} } void gb_bad_4() { *pgb_gvar = 1; // \ // expected-warning {{writing the value pointed to by 'pgb_gvar' requires holding any mutex exclusively}} } void gb_bad_5() { int x = *pgb_gvar; // \ // expected-warning {{reading the value pointed to by 'pgb_gvar' requires holding any mutex}} } void gb_bad_6() { *pgb_var = 1; // \ // expected-warning {{writing the value pointed to by 'pgb_var' requires holding mutex 'sls_mu' exclusively}} } void gb_bad_7() { int x = *pgb_var; // \ // expected-warning {{reading the value pointed to by 'pgb_var' requires holding mutex 'sls_mu'}} } void gb_bad_8() { GBFoo G; G.gb_field = 0; // \ // expected-warning {{writing variable 'gb_field' requires holding mutex 'sls_mu'}} } void gb_bad_9() { sls_guard_var++; // \ // expected-warning{{writing variable 'sls_guard_var' requires holding any mutex exclusively}} sls_guard_var--; // \ // expected-warning{{writing variable 'sls_guard_var' requires holding any mutex exclusively}} ++sls_guard_var; // \ // expected-warning{{writing variable 'sls_guard_var' requires holding any mutex exclusively}} --sls_guard_var;// \ // expected-warning{{writing variable 'sls_guard_var' requires holding any mutex exclusively}} } //-----------------------------------------------// // Warnings on variables with late parsed attributes // ----------------------------------------------// class LateFoo { public: int a __attribute__((guarded_by(mu))); int b; void foo() __attribute__((exclusive_locks_required(mu))) { } void test() { a = 0; // \ // expected-warning{{writing variable 'a' requires holding mutex 'mu' exclusively}} b = a; // \ // expected-warning {{reading variable 'a' requires holding mutex 'mu'}} c = 0; // \ // expected-warning {{writing variable 'c' requires holding mutex 'mu' exclusively}} } int c __attribute__((guarded_by(mu))); Mutex mu; }; class LateBar { public: int a_ __attribute__((guarded_by(mu1_))); int b_; int *q __attribute__((pt_guarded_by(mu))); Mutex mu1_; Mutex mu; LateFoo Foo; LateFoo Foo2; LateFoo *FooPointer; }; LateBar b1, *b3; void late_0() { LateFoo FooA; LateFoo FooB; FooA.mu.Lock(); FooA.a = 5; FooA.mu.Unlock(); } void late_1() { LateBar BarA; BarA.FooPointer->mu.Lock(); BarA.FooPointer->a = 2; BarA.FooPointer->mu.Unlock(); } void late_bad_0() { LateFoo fooA; LateFoo fooB; fooA.mu.Lock(); fooB.a = 5; // \ // expected-warning{{writing variable 'a' requires holding mutex 'fooB.mu' exclusively}} \ // expected-note{{found near match 'fooA.mu'}} fooA.mu.Unlock(); } void late_bad_1() { Mutex mu; mu.Lock(); b1.mu1_.Lock(); int res = b1.a_ + b3->b_; b3->b_ = *b1.q; // \ // expected-warning{{reading the value pointed to by 'q' requires holding mutex 'b1.mu'}} b1.mu1_.Unlock(); b1.b_ = res; mu.Unlock(); } void late_bad_2() { LateBar BarA; BarA.FooPointer->mu.Lock(); BarA.Foo.a = 2; // \ // expected-warning{{writing variable 'a' requires holding mutex 'BarA.Foo.mu' exclusively}} \ // expected-note{{found near match 'BarA.FooPointer->mu'}} BarA.FooPointer->mu.Unlock(); } void late_bad_3() { LateBar BarA; BarA.Foo.mu.Lock(); BarA.FooPointer->a = 2; // \ // expected-warning{{writing variable 'a' requires holding mutex 'BarA.FooPointer->mu' exclusively}} \ // expected-note{{found near match 'BarA.Foo.mu'}} BarA.Foo.mu.Unlock(); } void late_bad_4() { LateBar BarA; BarA.Foo.mu.Lock(); BarA.Foo2.a = 2; // \ // expected-warning{{writing variable 'a' requires holding mutex 'BarA.Foo2.mu' exclusively}} \ // expected-note{{found near match 'BarA.Foo.mu'}} BarA.Foo.mu.Unlock(); } //-----------------------------------------------// // Extra warnings for shared vs. exclusive locks // ----------------------------------------------// void shared_fun_0() { sls_mu.Lock(); do { sls_mu.Unlock(); sls_mu.Lock(); } while (getBool()); sls_mu.Unlock(); } void shared_fun_1() { sls_mu.ReaderLock(); // \ // expected-warning {{mutex 'sls_mu' is acquired exclusively and shared in the same scope}} do { sls_mu.Unlock(); sls_mu.Lock(); // \ // expected-note {{the other acquisition of mutex 'sls_mu' is here}} } while (getBool()); sls_mu.Unlock(); } void shared_fun_3() { if (getBool()) sls_mu.Lock(); else sls_mu.Lock(); *pgb_var = 1; sls_mu.Unlock(); } void shared_fun_4() { if (getBool()) sls_mu.ReaderLock(); else sls_mu.ReaderLock(); int x = sls_guardby_var; sls_mu.Unlock(); } void shared_fun_8() { if (getBool()) sls_mu.Lock(); // \ // expected-warning {{mutex 'sls_mu' is acquired exclusively and shared in the same scope}} else sls_mu.ReaderLock(); // \ // expected-note {{the other acquisition of mutex 'sls_mu' is here}} sls_mu.Unlock(); } void shared_bad_0() { sls_mu.Lock(); // \ // expected-warning {{mutex 'sls_mu' is acquired exclusively and shared in the same scope}} do { sls_mu.Unlock(); sls_mu.ReaderLock(); // \ // expected-note {{the other acquisition of mutex 'sls_mu' is here}} } while (getBool()); sls_mu.Unlock(); } void shared_bad_1() { if (getBool()) sls_mu.Lock(); // \ // expected-warning {{mutex 'sls_mu' is acquired exclusively and shared in the same scope}} else sls_mu.ReaderLock(); // \ // expected-note {{the other acquisition of mutex 'sls_mu' is here}} *pgb_var = 1; sls_mu.Unlock(); } void shared_bad_2() { if (getBool()) sls_mu.ReaderLock(); // \ // expected-warning {{mutex 'sls_mu' is acquired exclusively and shared in the same scope}} else sls_mu.Lock(); // \ // expected-note {{the other acquisition of mutex 'sls_mu' is here}} *pgb_var = 1; sls_mu.Unlock(); } // FIXME: Add support for functions (not only methods) class LRBar { public: void aa_elr_fun() __attribute__((exclusive_locks_required(aa_mu))); void aa_elr_fun_s() __attribute__((shared_locks_required(aa_mu))); void le_fun() __attribute__((locks_excluded(sls_mu))); }; class LRFoo { public: void test() __attribute__((exclusive_locks_required(sls_mu))); void testShared() __attribute__((shared_locks_required(sls_mu2))); }; void elr_fun() __attribute__((exclusive_locks_required(sls_mu))); void elr_fun() {} LRFoo MyLRFoo; LRBar Bar; void es_fun_0() { aa_mu.Lock(); Bar.aa_elr_fun(); aa_mu.Unlock(); } void es_fun_1() { aa_mu.Lock(); Bar.aa_elr_fun_s(); aa_mu.Unlock(); } void es_fun_2() { aa_mu.ReaderLock(); Bar.aa_elr_fun_s(); aa_mu.Unlock(); } void es_fun_3() { sls_mu.Lock(); MyLRFoo.test(); sls_mu.Unlock(); } void es_fun_4() { sls_mu2.Lock(); MyLRFoo.testShared(); sls_mu2.Unlock(); } void es_fun_5() { sls_mu2.ReaderLock(); MyLRFoo.testShared(); sls_mu2.Unlock(); } void es_fun_6() { Bar.le_fun(); } void es_fun_7() { sls_mu.Lock(); elr_fun(); sls_mu.Unlock(); } void es_fun_8() __attribute__((no_thread_safety_analysis)); void es_fun_8() { Bar.aa_elr_fun_s(); } void es_fun_9() __attribute__((shared_locks_required(aa_mu))); void es_fun_9() { Bar.aa_elr_fun_s(); } void es_fun_10() __attribute__((exclusive_locks_required(aa_mu))); void es_fun_10() { Bar.aa_elr_fun_s(); } void es_bad_0() { Bar.aa_elr_fun(); // \ // expected-warning {{calling function 'aa_elr_fun' requires holding mutex 'aa_mu' exclusively}} } void es_bad_1() { aa_mu.ReaderLock(); Bar.aa_elr_fun(); // \ // expected-warning {{calling function 'aa_elr_fun' requires holding mutex 'aa_mu' exclusively}} aa_mu.Unlock(); } void es_bad_2() { Bar.aa_elr_fun_s(); // \ // expected-warning {{calling function 'aa_elr_fun_s' requires holding mutex 'aa_mu'}} } void es_bad_3() { MyLRFoo.test(); // \ // expected-warning {{calling function 'test' requires holding mutex 'sls_mu' exclusively}} } void es_bad_4() { MyLRFoo.testShared(); // \ // expected-warning {{calling function 'testShared' requires holding mutex 'sls_mu2'}} } void es_bad_5() { sls_mu.ReaderLock(); MyLRFoo.test(); // \ // expected-warning {{calling function 'test' requires holding mutex 'sls_mu' exclusively}} sls_mu.Unlock(); } void es_bad_6() { sls_mu.Lock(); Bar.le_fun(); // \ // expected-warning {{cannot call function 'le_fun' while mutex 'sls_mu' is held}} sls_mu.Unlock(); } void es_bad_7() { sls_mu.ReaderLock(); Bar.le_fun(); // \ // expected-warning {{cannot call function 'le_fun' while mutex 'sls_mu' is held}} sls_mu.Unlock(); } //-----------------------------------------------// // Unparseable lock expressions // ----------------------------------------------// // FIXME -- derive new tests for unhandled expressions //----------------------------------------------------------------------------// // The following test cases are ported from the gcc thread safety implementation // They are each wrapped inside a namespace with the test number of the gcc test // // FIXME: add all the gcc tests, once this analysis passes them. //----------------------------------------------------------------------------// //-----------------------------------------// // Good testcases (no errors) //-----------------------------------------// namespace thread_annot_lock_20 { class Bar { public: static int func1() EXCLUSIVE_LOCKS_REQUIRED(mu1_); static int b_ GUARDED_BY(mu1_); static Mutex mu1_; static int a_ GUARDED_BY(mu1_); }; Bar b1; int Bar::func1() { int res = 5; if (a_ == 4) res = b_; return res; } } // end namespace thread_annot_lock_20 namespace thread_annot_lock_22 { // Test various usage of GUARDED_BY and PT_GUARDED_BY annotations, especially // uses in class definitions. Mutex mu; class Bar { public: int a_ GUARDED_BY(mu1_); int b_; int *q PT_GUARDED_BY(mu); Mutex mu1_ ACQUIRED_AFTER(mu); }; Bar b1, *b3; int *p GUARDED_BY(mu) PT_GUARDED_BY(mu); int res GUARDED_BY(mu) = 5; int func(int i) { int x; mu.Lock(); b1.mu1_.Lock(); res = b1.a_ + b3->b_; *p = i; b1.a_ = res + b3->b_; b3->b_ = *b1.q; b1.mu1_.Unlock(); b1.b_ = res; x = res; mu.Unlock(); return x; } } // end namespace thread_annot_lock_22 namespace thread_annot_lock_27_modified { // test lock annotations applied to function definitions // Modified: applied annotations only to function declarations Mutex mu1; Mutex mu2 ACQUIRED_AFTER(mu1); class Foo { public: int method1(int i) SHARED_LOCKS_REQUIRED(mu2) EXCLUSIVE_LOCKS_REQUIRED(mu1); }; int Foo::method1(int i) { return i; } int foo(int i) EXCLUSIVE_LOCKS_REQUIRED(mu2) SHARED_LOCKS_REQUIRED(mu1); int foo(int i) { return i; } static int bar(int i) EXCLUSIVE_LOCKS_REQUIRED(mu1); static int bar(int i) { return i; } void main() { Foo a; mu1.Lock(); mu2.Lock(); a.method1(1); foo(2); mu2.Unlock(); bar(3); mu1.Unlock(); } } // end namespace thread_annot_lock_27_modified namespace thread_annot_lock_38 { // Test the case where a template member function is annotated with lock // attributes in a non-template class. class Foo { public: void func1(int y) LOCKS_EXCLUDED(mu_); template <typename T> void func2(T x) LOCKS_EXCLUDED(mu_); private: Mutex mu_; }; Foo *foo; void main() { foo->func1(5); foo->func2(5); } } // end namespace thread_annot_lock_38 namespace thread_annot_lock_43 { // Tests lock canonicalization class Foo { public: Mutex *mu_; }; class FooBar { public: Foo *foo_; int GetA() EXCLUSIVE_LOCKS_REQUIRED(foo_->mu_) { return a_; } int a_ GUARDED_BY(foo_->mu_); }; FooBar *fb; void main() { int x; fb->foo_->mu_->Lock(); x = fb->GetA(); fb->foo_->mu_->Unlock(); } } // end namespace thread_annot_lock_43 namespace thread_annot_lock_49 { // Test the support for use of lock expression in the annotations class Foo { public: Mutex foo_mu_; }; class Bar { private: Foo *foo; Mutex bar_mu_ ACQUIRED_AFTER(foo->foo_mu_); public: void Test1() { foo->foo_mu_.Lock(); bar_mu_.Lock(); bar_mu_.Unlock(); foo->foo_mu_.Unlock(); } }; void main() { Bar bar; bar.Test1(); } } // end namespace thread_annot_lock_49 namespace thread_annot_lock_61_modified { // Modified to fix the compiler errors // Test the fix for a bug introduced by the support of pass-by-reference // paramters. struct Foo { Foo &operator<< (bool) {return *this;} }; Foo &getFoo(); struct Bar { Foo &func () {return getFoo();} }; struct Bas { void operator& (Foo &) {} }; void mumble() { Bas() & Bar().func() << "" << ""; Bas() & Bar().func() << ""; } } // end namespace thread_annot_lock_61_modified namespace thread_annot_lock_65 { // Test the fix for a bug in the support of allowing reader locks for // non-const, non-modifying overload functions. (We didn't handle the builtin // properly.) enum MyFlags { Zero, One, Two, Three, Four, Five, Six, Seven, Eight, Nine }; inline MyFlags operator|(MyFlags a, MyFlags b) { return MyFlags(static_cast<int>(a) | static_cast<int>(b)); } inline MyFlags& operator|=(MyFlags& a, MyFlags b) { return a = a | b; } } // end namespace thread_annot_lock_65 namespace thread_annot_lock_66_modified { // Modified: Moved annotation to function defn // Test annotations on out-of-line definitions of member functions where the // annotations refer to locks that are also data members in the class. Mutex mu; class Foo { public: int method1(int i) SHARED_LOCKS_REQUIRED(mu1, mu, mu2); int data GUARDED_BY(mu1); Mutex *mu1; Mutex *mu2; }; int Foo::method1(int i) { return data + i; } void main() { Foo a; a.mu2->Lock(); a.mu1->Lock(); mu.Lock(); a.method1(1); mu.Unlock(); a.mu1->Unlock(); a.mu2->Unlock(); } } // end namespace thread_annot_lock_66_modified namespace thread_annot_lock_68_modified { // Test a fix to a bug in the delayed name binding with nested template // instantiation. We use a stack to make sure a name is not resolved to an // inner context. template <typename T> class Bar { Mutex mu_; }; template <typename T> class Foo { public: void func(T x) { mu_.Lock(); count_ = x; mu_.Unlock(); } private: T count_ GUARDED_BY(mu_); Bar<T> bar_; Mutex mu_; }; void main() { Foo<int> *foo; foo->func(5); } } // end namespace thread_annot_lock_68_modified namespace thread_annot_lock_30_modified { // Test delay parsing of lock attribute arguments with nested classes. // Modified: trylocks replaced with exclusive_lock_fun int a = 0; class Bar { struct Foo; public: void MyLock() EXCLUSIVE_LOCK_FUNCTION(mu); int func() { MyLock(); // if (foo == 0) { // return 0; // } a = 5; mu.Unlock(); return 1; } class FooBar { int x; int y; }; private: Mutex mu; }; Bar *bar; void main() { bar->func(); } } // end namespace thread_annot_lock_30_modified namespace thread_annot_lock_47 { // Test the support for annotations on virtual functions. // This is a good test case. (i.e. There should be no warning emitted by the // compiler.) class Base { public: virtual void func1() EXCLUSIVE_LOCKS_REQUIRED(mu_); virtual void func2() LOCKS_EXCLUDED(mu_); Mutex mu_; }; class Child : public Base { public: virtual void func1() EXCLUSIVE_LOCKS_REQUIRED(mu_); virtual void func2() LOCKS_EXCLUDED(mu_); }; void main() { Child *c; Base *b = c; b->mu_.Lock(); b->func1(); b->mu_.Unlock(); b->func2(); c->mu_.Lock(); c->func1(); c->mu_.Unlock(); c->func2(); } } // end namespace thread_annot_lock_47 //-----------------------------------------// // Tests which produce errors //-----------------------------------------// namespace thread_annot_lock_13 { Mutex mu1; Mutex mu2; int g GUARDED_BY(mu1); int w GUARDED_BY(mu2); class Foo { public: void bar() LOCKS_EXCLUDED(mu_, mu1); int foo() SHARED_LOCKS_REQUIRED(mu_) EXCLUSIVE_LOCKS_REQUIRED(mu2); private: int a_ GUARDED_BY(mu_); public: Mutex mu_ ACQUIRED_AFTER(mu1); }; int Foo::foo() { int res; w = 5; res = a_ + 5; return res; } void Foo::bar() { int x; mu_.Lock(); x = foo(); // expected-warning {{calling function 'foo' requires holding mutex 'mu2' exclusively}} a_ = x + 1; mu_.Unlock(); if (x > 5) { mu1.Lock(); g = 2; mu1.Unlock(); } } void main() { Foo f1, *f2; f1.mu_.Lock(); f1.bar(); // expected-warning {{cannot call function 'bar' while mutex 'f1.mu_' is held}} mu2.Lock(); f1.foo(); mu2.Unlock(); f1.mu_.Unlock(); f2->mu_.Lock(); f2->bar(); // expected-warning {{cannot call function 'bar' while mutex 'f2->mu_' is held}} f2->mu_.Unlock(); mu2.Lock(); w = 2; mu2.Unlock(); } } // end namespace thread_annot_lock_13 namespace thread_annot_lock_18_modified { // Modified: Trylocks removed // Test the ability to distnguish between the same lock field of // different objects of a class. class Bar { public: bool MyLock() EXCLUSIVE_LOCK_FUNCTION(mu1_); void MyUnlock() UNLOCK_FUNCTION(mu1_); int a_ GUARDED_BY(mu1_); private: Mutex mu1_; }; Bar *b1, *b2; void func() { b1->MyLock(); b1->a_ = 5; b2->a_ = 3; // \ // expected-warning {{writing variable 'a_' requires holding mutex 'b2->mu1_' exclusively}} \ // expected-note {{found near match 'b1->mu1_'}} b2->MyLock(); b2->MyUnlock(); b1->MyUnlock(); } } // end namespace thread_annot_lock_18_modified namespace thread_annot_lock_21 { // Test various usage of GUARDED_BY and PT_GUARDED_BY annotations, especially // uses in class definitions. Mutex mu; class Bar { public: int a_ GUARDED_BY(mu1_); int b_; int *q PT_GUARDED_BY(mu); Mutex mu1_ ACQUIRED_AFTER(mu); }; Bar b1, *b3; int *p GUARDED_BY(mu) PT_GUARDED_BY(mu); int res GUARDED_BY(mu) = 5; int func(int i) { int x; b3->mu1_.Lock(); res = b1.a_ + b3->b_; // expected-warning {{reading variable 'a_' requires holding mutex 'b1.mu1_'}} \ // expected-warning {{writing variable 'res' requires holding mutex 'mu' exclusively}} \ // expected-note {{found near match 'b3->mu1_'}} *p = i; // expected-warning {{reading variable 'p' requires holding mutex 'mu'}} \ // expected-warning {{writing the value pointed to by 'p' requires holding mutex 'mu' exclusively}} b1.a_ = res + b3->b_; // expected-warning {{reading variable 'res' requires holding mutex 'mu'}} \ // expected-warning {{writing variable 'a_' requires holding mutex 'b1.mu1_' exclusively}} \ // expected-note {{found near match 'b3->mu1_'}} b3->b_ = *b1.q; // expected-warning {{reading the value pointed to by 'q' requires holding mutex 'mu'}} b3->mu1_.Unlock(); b1.b_ = res; // expected-warning {{reading variable 'res' requires holding mutex 'mu'}} x = res; // expected-warning {{reading variable 'res' requires holding mutex 'mu'}} return x; } } // end namespace thread_annot_lock_21 namespace thread_annot_lock_35_modified { // Test the analyzer's ability to distinguish the lock field of different // objects. class Foo { private: Mutex lock_; int a_ GUARDED_BY(lock_); public: void Func(Foo* child) LOCKS_EXCLUDED(lock_) { Foo *new_foo = new Foo; lock_.Lock(); child->Func(new_foo); // There shouldn't be any warning here as the // acquired lock is not in child. child->bar(7); // \ // expected-warning {{calling function 'bar' requires holding mutex 'child->lock_' exclusively}} \ // expected-note {{found near match 'lock_'}} child->a_ = 5; // \ // expected-warning {{writing variable 'a_' requires holding mutex 'child->lock_' exclusively}} \ // expected-note {{found near match 'lock_'}} lock_.Unlock(); } void bar(int y) EXCLUSIVE_LOCKS_REQUIRED(lock_) { a_ = y; } }; Foo *x; void main() { Foo *child = new Foo; x->Func(child); } } // end namespace thread_annot_lock_35_modified namespace thread_annot_lock_36_modified { // Modified to move the annotations to function defns. // Test the analyzer's ability to distinguish the lock field of different // objects class Foo { private: Mutex lock_; int a_ GUARDED_BY(lock_); public: void Func(Foo* child) LOCKS_EXCLUDED(lock_); void bar(int y) EXCLUSIVE_LOCKS_REQUIRED(lock_); }; void Foo::Func(Foo* child) { Foo *new_foo = new Foo; lock_.Lock(); child->lock_.Lock(); child->Func(new_foo); // expected-warning {{cannot call function 'Func' while mutex 'child->lock_' is held}} child->bar(7); child->a_ = 5; child->lock_.Unlock(); lock_.Unlock(); } void Foo::bar(int y) { a_ = y; } Foo *x; void main() { Foo *child = new Foo; x->Func(child); } } // end namespace thread_annot_lock_36_modified namespace thread_annot_lock_42 { // Test support of multiple lock attributes of the same kind on a decl. class Foo { private: Mutex mu1, mu2, mu3; int x GUARDED_BY(mu1) GUARDED_BY(mu2); int y GUARDED_BY(mu2); void f2() LOCKS_EXCLUDED(mu1) LOCKS_EXCLUDED(mu2) LOCKS_EXCLUDED(mu3) { mu2.Lock(); y = 2; mu2.Unlock(); } public: void f1() EXCLUSIVE_LOCKS_REQUIRED(mu2) EXCLUSIVE_LOCKS_REQUIRED(mu1) { x = 5; f2(); // expected-warning {{cannot call function 'f2' while mutex 'mu1' is held}} \ // expected-warning {{cannot call function 'f2' while mutex 'mu2' is held}} } }; Foo *foo; void func() { foo->f1(); // expected-warning {{calling function 'f1' requires holding mutex 'foo->mu2' exclusively}} \ // expected-warning {{calling function 'f1' requires holding mutex 'foo->mu1' exclusively}} } } // end namespace thread_annot_lock_42 namespace thread_annot_lock_46 { // Test the support for annotations on virtual functions. class Base { public: virtual void func1() EXCLUSIVE_LOCKS_REQUIRED(mu_); virtual void func2() LOCKS_EXCLUDED(mu_); Mutex mu_; }; class Child : public Base { public: virtual void func1() EXCLUSIVE_LOCKS_REQUIRED(mu_); virtual void func2() LOCKS_EXCLUDED(mu_); }; void main() { Child *c; Base *b = c; b->func1(); // expected-warning {{calling function 'func1' requires holding mutex 'b->mu_' exclusively}} b->mu_.Lock(); b->func2(); // expected-warning {{cannot call function 'func2' while mutex 'b->mu_' is held}} b->mu_.Unlock(); c->func1(); // expected-warning {{calling function 'func1' requires holding mutex 'c->mu_' exclusively}} c->mu_.Lock(); c->func2(); // expected-warning {{cannot call function 'func2' while mutex 'c->mu_' is held}} c->mu_.Unlock(); } } // end namespace thread_annot_lock_46 namespace thread_annot_lock_67_modified { // Modified: attributes on definitions moved to declarations // Test annotations on out-of-line definitions of member functions where the // annotations refer to locks that are also data members in the class. Mutex mu; Mutex mu3; class Foo { public: int method1(int i) SHARED_LOCKS_REQUIRED(mu1, mu, mu2, mu3); int data GUARDED_BY(mu1); Mutex *mu1; Mutex *mu2; }; int Foo::method1(int i) { return data + i; } void main() { Foo a; a.method1(1); // expected-warning {{calling function 'method1' requires holding mutex 'a.mu1'}} \ // expected-warning {{calling function 'method1' requires holding mutex 'mu'}} \ // expected-warning {{calling function 'method1' requires holding mutex 'a.mu2'}} \ // expected-warning {{calling function 'method1' requires holding mutex 'mu3'}} } } // end namespace thread_annot_lock_67_modified namespace substitution_test { class MyData { public: Mutex mu; void lockData() __attribute__((exclusive_lock_function(mu))); void unlockData() __attribute__((unlock_function(mu))); void doSomething() __attribute__((exclusive_locks_required(mu))) { } }; class DataLocker { public: void lockData (MyData *d) __attribute__((exclusive_lock_function(d->mu))); void unlockData(MyData *d) __attribute__((unlock_function(d->mu))); }; class Foo { public: void foo(MyData* d) __attribute__((exclusive_locks_required(d->mu))) { } void bar1(MyData* d) { d->lockData(); foo(d); d->unlockData(); } void bar2(MyData* d) { DataLocker dlr; dlr.lockData(d); foo(d); dlr.unlockData(d); } void bar3(MyData* d1, MyData* d2) { DataLocker dlr; dlr.lockData(d1); // expected-note {{mutex acquired here}} dlr.unlockData(d2); // \ // expected-warning {{releasing mutex 'd2->mu' that was not held}} } // expected-warning {{mutex 'd1->mu' is still held at the end of function}} void bar4(MyData* d1, MyData* d2) { DataLocker dlr; dlr.lockData(d1); foo(d2); // \ // expected-warning {{calling function 'foo' requires holding mutex 'd2->mu' exclusively}} \ // expected-note {{found near match 'd1->mu'}} dlr.unlockData(d1); } }; } // end namespace substituation_test namespace constructor_destructor_tests { Mutex fooMu; int myVar GUARDED_BY(fooMu); class Foo { public: Foo() __attribute__((exclusive_lock_function(fooMu))) { } ~Foo() __attribute__((unlock_function(fooMu))) { } }; void fooTest() { Foo foo; myVar = 0; } } namespace template_member_test { struct S { int n; }; struct T { Mutex m; S *s GUARDED_BY(this->m); }; Mutex m; struct U { union { int n; }; } *u GUARDED_BY(m); template<typename U> struct IndirectLock { int DoNaughtyThings(T *t) { u->n = 0; // expected-warning {{reading variable 'u' requires holding mutex 'm'}} return t->s->n; // expected-warning {{reading variable 's' requires holding mutex 't->m'}} } }; template struct IndirectLock<int>; // expected-note {{here}} struct V { void f(int); void f(double); Mutex m; V *p GUARDED_BY(this->m); }; template<typename U> struct W { V v; void f(U u) { v.p->f(u); // expected-warning {{reading variable 'p' requires holding mutex 'v.m'}} } }; template struct W<int>; // expected-note {{here}} } namespace test_scoped_lockable { struct TestScopedLockable { Mutex mu1; Mutex mu2; int a __attribute__((guarded_by(mu1))); int b __attribute__((guarded_by(mu2))); bool getBool(); void foo1() { MutexLock mulock(&mu1); a = 5; } void foo2() { ReaderMutexLock mulock1(&mu1); if (getBool()) { MutexLock mulock2a(&mu2); b = a + 1; } else { MutexLock mulock2b(&mu2); b = a + 2; } } void foo3() { MutexLock mulock_a(&mu1); MutexLock mulock_b(&mu1); // \ // expected-warning {{acquiring mutex 'mu1' that is already held}} } void foo4() { MutexLock mulock1(&mu1), mulock2(&mu2); a = b+1; b = a+1; } void foo5() { DoubleMutexLock mulock(&mu1, &mu2); a = b + 1; b = a + 1; } }; } // end namespace test_scoped_lockable namespace FunctionAttrTest { class Foo { public: Mutex mu_; int a GUARDED_BY(mu_); }; Foo fooObj; void foo() EXCLUSIVE_LOCKS_REQUIRED(fooObj.mu_); void bar() { foo(); // expected-warning {{calling function 'foo' requires holding mutex 'fooObj.mu_' exclusively}} fooObj.mu_.Lock(); foo(); fooObj.mu_.Unlock(); } }; // end namespace FunctionAttrTest namespace TryLockTest { struct TestTryLock { Mutex mu; int a GUARDED_BY(mu); bool cond; void foo1() { if (mu.TryLock()) { a = 1; mu.Unlock(); } } void foo2() { if (!mu.TryLock()) return; a = 2; mu.Unlock(); } void foo3() { bool b = mu.TryLock(); if (b) { a = 3; mu.Unlock(); } } void foo4() { bool b = mu.TryLock(); if (!b) return; a = 4; mu.Unlock(); } void foo5() { while (mu.TryLock()) { a = a + 1; mu.Unlock(); } } void foo6() { bool b = mu.TryLock(); b = !b; if (b) return; a = 6; mu.Unlock(); } void foo7() { bool b1 = mu.TryLock(); bool b2 = !b1; bool b3 = !b2; if (b3) { a = 7; mu.Unlock(); } } // Test use-def chains: join points void foo8() { bool b = mu.TryLock(); bool b2 = b; if (cond) b = true; if (b) { // b should be unknown at this point, because of the join point a = 8; // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}} } if (b2) { // b2 should be known at this point. a = 8; mu.Unlock(); } } // Test use-def-chains: back edges void foo9() { bool b = mu.TryLock(); for (int i = 0; i < 10; ++i); if (b) { // b is still known, because the loop doesn't alter it a = 9; mu.Unlock(); } } // Test use-def chains: back edges void foo10() { bool b = mu.TryLock(); while (cond) { if (b) { // b should be uknown at this point b/c of the loop a = 10; // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}} } b = !b; } } // Test merge of exclusive trylock void foo11() { if (cond) { if (!mu.TryLock()) return; } else { mu.Lock(); } a = 10; mu.Unlock(); } // Test merge of shared trylock void foo12() { if (cond) { if (!mu.ReaderTryLock()) return; } else { mu.ReaderLock(); } int i = a; mu.Unlock(); } }; // end TestTrylock } // end namespace TrylockTest namespace TestTemplateAttributeInstantiation { class Foo1 { public: Mutex mu_; int a GUARDED_BY(mu_); }; class Foo2 { public: int a GUARDED_BY(mu_); Mutex mu_; }; class Bar { public: // Test non-dependent expressions in attributes on template functions template <class T> void barND(Foo1 *foo, T *fooT) EXCLUSIVE_LOCKS_REQUIRED(foo->mu_) { foo->a = 0; } // Test dependent expressions in attributes on template functions template <class T> void barD(Foo1 *foo, T *fooT) EXCLUSIVE_LOCKS_REQUIRED(fooT->mu_) { fooT->a = 0; } }; template <class T> class BarT { public: Foo1 fooBase; T fooBaseT; // Test non-dependent expression in ordinary method on template class void barND() EXCLUSIVE_LOCKS_REQUIRED(fooBase.mu_) { fooBase.a = 0; } // Test dependent expressions in ordinary methods on template class void barD() EXCLUSIVE_LOCKS_REQUIRED(fooBaseT.mu_) { fooBaseT.a = 0; } // Test dependent expressions in template method in template class template <class T2> void barTD(T2 *fooT) EXCLUSIVE_LOCKS_REQUIRED(fooBaseT.mu_, fooT->mu_) { fooBaseT.a = 0; fooT->a = 0; } }; template <class T> class Cell { public: Mutex mu_; // Test dependent guarded_by T data GUARDED_BY(mu_); void fooEx() EXCLUSIVE_LOCKS_REQUIRED(mu_) { data = 0; } void foo() { mu_.Lock(); data = 0; mu_.Unlock(); } }; void test() { Bar b; BarT<Foo2> bt; Foo1 f1; Foo2 f2; f1.mu_.Lock(); f2.mu_.Lock(); bt.fooBase.mu_.Lock(); bt.fooBaseT.mu_.Lock(); b.barND(&f1, &f2); b.barD(&f1, &f2); bt.barND(); bt.barD(); bt.barTD(&f2); f1.mu_.Unlock(); bt.barTD(&f1); // \ // expected-warning {{calling function 'barTD' requires holding mutex 'f1.mu_' exclusively}} \ // expected-note {{found near match 'bt.fooBase.mu_'}} bt.fooBase.mu_.Unlock(); bt.fooBaseT.mu_.Unlock(); f2.mu_.Unlock(); Cell<int> cell; cell.data = 0; // \ // expected-warning {{writing variable 'data' requires holding mutex 'cell.mu_' exclusively}} cell.foo(); cell.mu_.Lock(); cell.fooEx(); cell.mu_.Unlock(); } template <class T> class CellDelayed { public: // Test dependent guarded_by T data GUARDED_BY(mu_); static T static_data GUARDED_BY(static_mu_); void fooEx(CellDelayed<T> *other) EXCLUSIVE_LOCKS_REQUIRED(mu_, other->mu_) { this->data = other->data; } template <class T2> void fooExT(CellDelayed<T2> *otherT) EXCLUSIVE_LOCKS_REQUIRED(mu_, otherT->mu_) { this->data = otherT->data; } void foo() { mu_.Lock(); data = 0; mu_.Unlock(); } Mutex mu_; static Mutex static_mu_; }; void testDelayed() { CellDelayed<int> celld; CellDelayed<int> celld2; celld.foo(); celld.mu_.Lock(); celld2.mu_.Lock(); celld.fooEx(&celld2); celld.fooExT(&celld2); celld2.mu_.Unlock(); celld.mu_.Unlock(); } }; // end namespace TestTemplateAttributeInstantiation namespace FunctionDeclDefTest { class Foo { public: Mutex mu_; int a GUARDED_BY(mu_); virtual void foo1(Foo *f_declared) EXCLUSIVE_LOCKS_REQUIRED(f_declared->mu_); }; // EXCLUSIVE_LOCKS_REQUIRED should be applied, and rewritten to f_defined->mu_ void Foo::foo1(Foo *f_defined) { f_defined->a = 0; }; void test() { Foo myfoo; myfoo.foo1(&myfoo); // \ // expected-warning {{calling function 'foo1' requires holding mutex 'myfoo.mu_' exclusively}} myfoo.mu_.Lock(); myfoo.foo1(&myfoo); myfoo.mu_.Unlock(); } }; namespace GoingNative { struct __attribute__((lockable)) mutex { void lock() __attribute__((exclusive_lock_function)); void unlock() __attribute__((unlock_function)); // ... }; bool foo(); bool bar(); mutex m; void test() { m.lock(); while (foo()) { m.unlock(); // ... if (bar()) { // ... if (foo()) continue; // expected-warning {{expecting mutex 'm' to be held at start of each loop}} //... } // ... m.lock(); // expected-note {{mutex acquired here}} } m.unlock(); } } namespace FunctionDefinitionTest { class Foo { public: void foo1(); void foo2(); void foo3(Foo *other); template<class T> void fooT1(const T& dummy1); template<class T> void fooT2(const T& dummy2) EXCLUSIVE_LOCKS_REQUIRED(mu_); Mutex mu_; int a GUARDED_BY(mu_); }; template<class T> class FooT { public: void foo(); Mutex mu_; T a GUARDED_BY(mu_); }; void Foo::foo1() NO_THREAD_SAFETY_ANALYSIS { a = 1; } void Foo::foo2() EXCLUSIVE_LOCKS_REQUIRED(mu_) { a = 2; } void Foo::foo3(Foo *other) EXCLUSIVE_LOCKS_REQUIRED(other->mu_) { other->a = 3; } template<class T> void Foo::fooT1(const T& dummy1) EXCLUSIVE_LOCKS_REQUIRED(mu_) { a = dummy1; } /* TODO -- uncomment with template instantiation of attributes. template<class T> void Foo::fooT2(const T& dummy2) { a = dummy2; } */ void fooF1(Foo *f) EXCLUSIVE_LOCKS_REQUIRED(f->mu_) { f->a = 1; } void fooF2(Foo *f); void fooF2(Foo *f) EXCLUSIVE_LOCKS_REQUIRED(f->mu_) { f->a = 2; } void fooF3(Foo *f) EXCLUSIVE_LOCKS_REQUIRED(f->mu_); void fooF3(Foo *f) { f->a = 3; } template<class T> void FooT<T>::foo() EXCLUSIVE_LOCKS_REQUIRED(mu_) { a = 0; } void test() { int dummy = 0; Foo myFoo; myFoo.foo2(); // \ // expected-warning {{calling function 'foo2' requires holding mutex 'myFoo.mu_' exclusively}} myFoo.foo3(&myFoo); // \ // expected-warning {{calling function 'foo3' requires holding mutex 'myFoo.mu_' exclusively}} myFoo.fooT1(dummy); // \ // expected-warning {{calling function 'fooT1' requires holding mutex 'myFoo.mu_' exclusively}} myFoo.fooT2(dummy); // \ // expected-warning {{calling function 'fooT2' requires holding mutex 'myFoo.mu_' exclusively}} fooF1(&myFoo); // \ // expected-warning {{calling function 'fooF1' requires holding mutex 'myFoo.mu_' exclusively}} fooF2(&myFoo); // \ // expected-warning {{calling function 'fooF2' requires holding mutex 'myFoo.mu_' exclusively}} fooF3(&myFoo); // \ // expected-warning {{calling function 'fooF3' requires holding mutex 'myFoo.mu_' exclusively}} myFoo.mu_.Lock(); myFoo.foo2(); myFoo.foo3(&myFoo); myFoo.fooT1(dummy); myFoo.fooT2(dummy); fooF1(&myFoo); fooF2(&myFoo); fooF3(&myFoo); myFoo.mu_.Unlock(); FooT<int> myFooT; myFooT.foo(); // \ // expected-warning {{calling function 'foo' requires holding mutex 'myFooT.mu_' exclusively}} } } // end namespace FunctionDefinitionTest namespace SelfLockingTest { class LOCKABLE MyLock { public: int foo GUARDED_BY(this); void lock() EXCLUSIVE_LOCK_FUNCTION(); void unlock() UNLOCK_FUNCTION(); void doSomething() { this->lock(); // allow 'this' as a lock expression foo = 0; doSomethingElse(); this->unlock(); } void doSomethingElse() EXCLUSIVE_LOCKS_REQUIRED(this) { foo = 1; }; void test() { foo = 2; // \ // expected-warning {{writing variable 'foo' requires holding mutex 'this' exclusively}} } }; class LOCKABLE MyLock2 { public: Mutex mu_; int foo GUARDED_BY(this); // don't check inside lock and unlock functions void lock() EXCLUSIVE_LOCK_FUNCTION() { mu_.Lock(); } void unlock() UNLOCK_FUNCTION() { mu_.Unlock(); } // don't check inside constructors and destructors MyLock2() { foo = 1; } ~MyLock2() { foo = 0; } }; } // end namespace SelfLockingTest namespace InvalidNonstatic { // Forward decl here causes bogus "invalid use of non-static data member" // on reference to mutex_ in guarded_by attribute. class Foo; class Foo { Mutex* mutex_; int foo __attribute__((guarded_by(mutex_))); }; } // end namespace InvalidNonStatic namespace NoReturnTest { bool condition(); void fatal() __attribute__((noreturn)); Mutex mu_; void test1() { MutexLock lock(&mu_); if (condition()) { fatal(); return; } } } // end namespace NoReturnTest namespace TestMultiDecl { class Foo { public: int GUARDED_BY(mu_) a; int GUARDED_BY(mu_) b, c; void foo() { a = 0; // \ // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} b = 0; // \ // expected-warning {{writing variable 'b' requires holding mutex 'mu_' exclusively}} c = 0; // \ // expected-warning {{writing variable 'c' requires holding mutex 'mu_' exclusively}} } private: Mutex mu_; }; } // end namespace TestMultiDecl namespace WarnNoDecl { class Foo { void foo(int a); __attribute__(( // \ // expected-warning {{declaration does not declare anything}} exclusive_locks_required(a))); // \ // expected-warning {{attribute exclusive_locks_required ignored}} }; } // end namespace WarnNoDecl namespace MoreLockExpressions { class Foo { public: Mutex mu_; int a GUARDED_BY(mu_); }; class Bar { public: int b; Foo* f; Foo& getFoo() { return *f; } Foo& getFoo2(int c) { return *f; } Foo& getFoo3(int c, int d) { return *f; } Foo& getFooey() { return *f; } }; Foo& getBarFoo(Bar &bar, int c) { return bar.getFoo2(c); } void test() { Foo foo; Foo *fooArray; Bar bar; int a; int b; int c; bar.getFoo().mu_.Lock(); bar.getFoo().a = 0; bar.getFoo().mu_.Unlock(); (bar.getFoo().mu_).Lock(); // test parenthesis bar.getFoo().a = 0; (bar.getFoo().mu_).Unlock(); bar.getFoo2(a).mu_.Lock(); bar.getFoo2(a).a = 0; bar.getFoo2(a).mu_.Unlock(); bar.getFoo3(a, b).mu_.Lock(); bar.getFoo3(a, b).a = 0; bar.getFoo3(a, b).mu_.Unlock(); getBarFoo(bar, a).mu_.Lock(); getBarFoo(bar, a).a = 0; getBarFoo(bar, a).mu_.Unlock(); bar.getFoo2(10).mu_.Lock(); bar.getFoo2(10).a = 0; bar.getFoo2(10).mu_.Unlock(); bar.getFoo2(a + 1).mu_.Lock(); bar.getFoo2(a + 1).a = 0; bar.getFoo2(a + 1).mu_.Unlock(); (a > 0 ? fooArray[1] : fooArray[b]).mu_.Lock(); (a > 0 ? fooArray[1] : fooArray[b]).a = 0; (a > 0 ? fooArray[1] : fooArray[b]).mu_.Unlock(); } void test2() { Foo *fooArray; Bar bar; int a; int b; int c; bar.getFoo().mu_.Lock(); bar.getFooey().a = 0; // \ // expected-warning {{writing variable 'a' requires holding mutex 'bar.getFooey().mu_' exclusively}} \ // expected-note {{found near match 'bar.getFoo().mu_'}} bar.getFoo().mu_.Unlock(); bar.getFoo2(a).mu_.Lock(); bar.getFoo2(b).a = 0; // \ // expected-warning {{writing variable 'a' requires holding mutex 'bar.getFoo2(b).mu_' exclusively}} \ // expected-note {{found near match 'bar.getFoo2(a).mu_'}} bar.getFoo2(a).mu_.Unlock(); bar.getFoo3(a, b).mu_.Lock(); bar.getFoo3(a, c).a = 0; // \ // expected-warning {{writing variable 'a' requires holding mutex 'bar.getFoo3(a, c).mu_' exclusively}} \ // expected-note {{found near match 'bar.getFoo3(a, b).mu_'}} bar.getFoo3(a, b).mu_.Unlock(); getBarFoo(bar, a).mu_.Lock(); getBarFoo(bar, b).a = 0; // \ // expected-warning {{writing variable 'a' requires holding mutex 'getBarFoo(bar, b).mu_' exclusively}} \ // expected-note {{found near match 'getBarFoo(bar, a).mu_'}} getBarFoo(bar, a).mu_.Unlock(); (a > 0 ? fooArray[1] : fooArray[b]).mu_.Lock(); (a > 0 ? fooArray[b] : fooArray[c]).a = 0; // \ // expected-warning {{writing variable 'a' requires holding mutex '((0 < a) ? fooArray[b] : fooArray[c]).mu_' exclusively}} \ // expected-note {{found near match '((0 < a) ? fooArray[1] : fooArray[b]).mu_'}} (a > 0 ? fooArray[1] : fooArray[b]).mu_.Unlock(); } } // end namespace MoreLockExpressions namespace TrylockJoinPoint { class Foo { Mutex mu; bool c; void foo() { if (c) { if (!mu.TryLock()) return; } else { mu.Lock(); } mu.Unlock(); } }; } // end namespace TrylockJoinPoint namespace LockReturned { class Foo { public: int a GUARDED_BY(mu_); void foo() EXCLUSIVE_LOCKS_REQUIRED(mu_); void foo2(Foo* f) EXCLUSIVE_LOCKS_REQUIRED(mu_, f->mu_); static void sfoo(Foo* f) EXCLUSIVE_LOCKS_REQUIRED(f->mu_); Mutex* getMu() LOCK_RETURNED(mu_); Mutex mu_; static Mutex* getMu(Foo* f) LOCK_RETURNED(f->mu_); }; // Calls getMu() directly to lock and unlock void test1(Foo* f1, Foo* f2) { f1->a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'f1->mu_' exclusively}} f1->foo(); // expected-warning {{calling function 'foo' requires holding mutex 'f1->mu_' exclusively}} f1->foo2(f2); // expected-warning {{calling function 'foo2' requires holding mutex 'f1->mu_' exclusively}} \ // expected-warning {{calling function 'foo2' requires holding mutex 'f2->mu_' exclusively}} Foo::sfoo(f1); // expected-warning {{calling function 'sfoo' requires holding mutex 'f1->mu_' exclusively}} f1->getMu()->Lock(); f1->a = 0; f1->foo(); f1->foo2(f2); // \ // expected-warning {{calling function 'foo2' requires holding mutex 'f2->mu_' exclusively}} \ // expected-note {{found near match 'f1->mu_'}} Foo::getMu(f2)->Lock(); f1->foo2(f2); Foo::getMu(f2)->Unlock(); Foo::sfoo(f1); f1->getMu()->Unlock(); } Mutex* getFooMu(Foo* f) LOCK_RETURNED(Foo::getMu(f)); class Bar : public Foo { public: int b GUARDED_BY(getMu()); void bar() EXCLUSIVE_LOCKS_REQUIRED(getMu()); void bar2(Bar* g) EXCLUSIVE_LOCKS_REQUIRED(getMu(this), g->getMu()); static void sbar(Bar* g) EXCLUSIVE_LOCKS_REQUIRED(g->getMu()); static void sbar2(Bar* g) EXCLUSIVE_LOCKS_REQUIRED(getFooMu(g)); }; // Use getMu() within other attributes. // This requires at lest levels of substitution, more in the case of void test2(Bar* b1, Bar* b2) { b1->b = 0; // expected-warning {{writing variable 'b' requires holding mutex 'b1->mu_' exclusively}} b1->bar(); // expected-warning {{calling function 'bar' requires holding mutex 'b1->mu_' exclusively}} b1->bar2(b2); // expected-warning {{calling function 'bar2' requires holding mutex 'b1->mu_' exclusively}} \ // expected-warning {{calling function 'bar2' requires holding mutex 'b2->mu_' exclusively}} Bar::sbar(b1); // expected-warning {{calling function 'sbar' requires holding mutex 'b1->mu_' exclusively}} Bar::sbar2(b1); // expected-warning {{calling function 'sbar2' requires holding mutex 'b1->mu_' exclusively}} b1->getMu()->Lock(); b1->b = 0; b1->bar(); b1->bar2(b2); // \ // expected-warning {{calling function 'bar2' requires holding mutex 'b2->mu_' exclusively}} \ // // expected-note {{found near match 'b1->mu_'}} b2->getMu()->Lock(); b1->bar2(b2); b2->getMu()->Unlock(); Bar::sbar(b1); Bar::sbar2(b1); b1->getMu()->Unlock(); } // Sanity check -- lock the mutex directly, but use attributes that call getMu() // Also lock the mutex using getFooMu, which calls a lock_returned function. void test3(Bar* b1, Bar* b2) { b1->mu_.Lock(); b1->b = 0; b1->bar(); getFooMu(b2)->Lock(); b1->bar2(b2); getFooMu(b2)->Unlock(); Bar::sbar(b1); Bar::sbar2(b1); b1->mu_.Unlock(); } } // end namespace LockReturned namespace ReleasableScopedLock { class Foo { Mutex mu_; bool c; int a GUARDED_BY(mu_); void test1(); void test2(); void test3(); void test4(); void test5(); }; void Foo::test1() { ReleasableMutexLock rlock(&mu_); rlock.Release(); } void Foo::test2() { ReleasableMutexLock rlock(&mu_); if (c) { // test join point -- held/not held during release rlock.Release(); } } void Foo::test3() { ReleasableMutexLock rlock(&mu_); a = 0; rlock.Release(); a = 1; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} } void Foo::test4() { ReleasableMutexLock rlock(&mu_); rlock.Release(); rlock.Release(); // expected-warning {{releasing mutex 'mu_' that was not held}} } void Foo::test5() { ReleasableMutexLock rlock(&mu_); if (c) { rlock.Release(); } // no warning on join point for managed lock. rlock.Release(); // expected-warning {{releasing mutex 'mu_' that was not held}} } } // end namespace ReleasableScopedLock namespace TrylockFunctionTest { class Foo { public: Mutex mu1_; Mutex mu2_; bool c; bool lockBoth() EXCLUSIVE_TRYLOCK_FUNCTION(true, mu1_, mu2_); }; bool Foo::lockBoth() { if (!mu1_.TryLock()) return false; mu2_.Lock(); if (!c) { mu1_.Unlock(); mu2_.Unlock(); return false; } return true; } } // end namespace TrylockFunctionTest namespace DoubleLockBug { class Foo { public: Mutex mu_; int a GUARDED_BY(mu_); void foo1() EXCLUSIVE_LOCKS_REQUIRED(mu_); int foo2() SHARED_LOCKS_REQUIRED(mu_); }; void Foo::foo1() EXCLUSIVE_LOCKS_REQUIRED(mu_) { a = 0; } int Foo::foo2() SHARED_LOCKS_REQUIRED(mu_) { return a; } } namespace UnlockBug { class Foo { public: Mutex mutex_; void foo1() EXCLUSIVE_LOCKS_REQUIRED(mutex_) { // expected-note {{mutex acquired here}} mutex_.Unlock(); } // expected-warning {{expecting mutex 'mutex_' to be held at the end of function}} void foo2() SHARED_LOCKS_REQUIRED(mutex_) { // expected-note {{mutex acquired here}} mutex_.Unlock(); } // expected-warning {{expecting mutex 'mutex_' to be held at the end of function}} }; } // end namespace UnlockBug namespace FoolishScopedLockableBug { class SCOPED_LOCKABLE WTF_ScopedLockable { public: WTF_ScopedLockable(Mutex* mu) EXCLUSIVE_LOCK_FUNCTION(mu); // have to call release() manually; ~WTF_ScopedLockable(); void release() UNLOCK_FUNCTION(); }; class Foo { Mutex mu_; int a GUARDED_BY(mu_); bool c; void doSomething(); void test1() { WTF_ScopedLockable wtf(&mu_); wtf.release(); } void test2() { WTF_ScopedLockable wtf(&mu_); // expected-note {{mutex acquired here}} } // expected-warning {{mutex 'mu_' is still held at the end of function}} void test3() { if (c) { WTF_ScopedLockable wtf(&mu_); wtf.release(); } } void test4() { if (c) { doSomething(); } else { WTF_ScopedLockable wtf(&mu_); wtf.release(); } } void test5() { if (c) { WTF_ScopedLockable wtf(&mu_); // expected-note {{mutex acquired here}} } } // expected-warning {{mutex 'mu_' is not held on every path through here}} void test6() { if (c) { doSomething(); } else { WTF_ScopedLockable wtf(&mu_); // expected-note {{mutex acquired here}} } } // expected-warning {{mutex 'mu_' is not held on every path through here}} }; } // end namespace FoolishScopedLockableBug namespace TemporaryCleanupExpr { class Foo { int a GUARDED_BY(getMutexPtr().get()); SmartPtr<Mutex> getMutexPtr(); void test(); }; void Foo::test() { { ReaderMutexLock lock(getMutexPtr().get()); int b = a; } int b = a; // expected-warning {{reading variable 'a' requires holding mutex 'getMutexPtr()'}} } } // end namespace TemporaryCleanupExpr namespace SmartPointerTests { class Foo { public: SmartPtr<Mutex> mu_; int a GUARDED_BY(mu_); int b GUARDED_BY(mu_.get()); int c GUARDED_BY(*mu_); void Lock() EXCLUSIVE_LOCK_FUNCTION(mu_); void Unlock() UNLOCK_FUNCTION(mu_); void test0(); void test1(); void test2(); void test3(); void test4(); void test5(); void test6(); void test7(); void test8(); }; void Foo::test0() { a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} b = 0; // expected-warning {{writing variable 'b' requires holding mutex 'mu_' exclusively}} c = 0; // expected-warning {{writing variable 'c' requires holding mutex 'mu_' exclusively}} } void Foo::test1() { mu_->Lock(); a = 0; b = 0; c = 0; mu_->Unlock(); } void Foo::test2() { (*mu_).Lock(); a = 0; b = 0; c = 0; (*mu_).Unlock(); } void Foo::test3() { mu_.get()->Lock(); a = 0; b = 0; c = 0; mu_.get()->Unlock(); } void Foo::test4() { MutexLock lock(mu_.get()); a = 0; b = 0; c = 0; } void Foo::test5() { MutexLock lock(&(*mu_)); a = 0; b = 0; c = 0; } void Foo::test6() { Lock(); a = 0; b = 0; c = 0; Unlock(); } void Foo::test7() { { Lock(); mu_->Unlock(); } { mu_->Lock(); Unlock(); } { mu_.get()->Lock(); mu_->Unlock(); } { mu_->Lock(); mu_.get()->Unlock(); } { mu_.get()->Lock(); (*mu_).Unlock(); } { (*mu_).Lock(); mu_->Unlock(); } } void Foo::test8() { mu_->Lock(); mu_.get()->Lock(); // expected-warning {{acquiring mutex 'mu_' that is already held}} (*mu_).Lock(); // expected-warning {{acquiring mutex 'mu_' that is already held}} mu_.get()->Unlock(); Unlock(); // expected-warning {{releasing mutex 'mu_' that was not held}} } class Bar { SmartPtr<Foo> foo; void test0(); void test1(); void test2(); void test3(); }; void Bar::test0() { foo->a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'foo->mu_' exclusively}} (*foo).b = 0; // expected-warning {{writing variable 'b' requires holding mutex 'foo->mu_' exclusively}} foo.get()->c = 0; // expected-warning {{writing variable 'c' requires holding mutex 'foo->mu_' exclusively}} } void Bar::test1() { foo->mu_->Lock(); foo->a = 0; (*foo).b = 0; foo.get()->c = 0; foo->mu_->Unlock(); } void Bar::test2() { (*foo).mu_->Lock(); foo->a = 0; (*foo).b = 0; foo.get()->c = 0; foo.get()->mu_->Unlock(); } void Bar::test3() { MutexLock lock(foo->mu_.get()); foo->a = 0; (*foo).b = 0; foo.get()->c = 0; } } // end namespace SmartPointerTests namespace DuplicateAttributeTest { class LOCKABLE Foo { public: Mutex mu1_; Mutex mu2_; Mutex mu3_; int a GUARDED_BY(mu1_); int b GUARDED_BY(mu2_); int c GUARDED_BY(mu3_); void lock() EXCLUSIVE_LOCK_FUNCTION(); void unlock() UNLOCK_FUNCTION(); void lock1() EXCLUSIVE_LOCK_FUNCTION(mu1_); void slock1() SHARED_LOCK_FUNCTION(mu1_); void lock3() EXCLUSIVE_LOCK_FUNCTION(mu1_, mu2_, mu3_); void locklots() EXCLUSIVE_LOCK_FUNCTION(mu1_) EXCLUSIVE_LOCK_FUNCTION(mu2_) EXCLUSIVE_LOCK_FUNCTION(mu1_, mu2_, mu3_); void unlock1() UNLOCK_FUNCTION(mu1_); void unlock3() UNLOCK_FUNCTION(mu1_, mu2_, mu3_); void unlocklots() UNLOCK_FUNCTION(mu1_) UNLOCK_FUNCTION(mu2_) UNLOCK_FUNCTION(mu1_, mu2_, mu3_); }; void Foo::lock() EXCLUSIVE_LOCK_FUNCTION() { } void Foo::unlock() UNLOCK_FUNCTION() { } void Foo::lock1() EXCLUSIVE_LOCK_FUNCTION(mu1_) { mu1_.Lock(); } void Foo::slock1() SHARED_LOCK_FUNCTION(mu1_) { mu1_.ReaderLock(); } void Foo::lock3() EXCLUSIVE_LOCK_FUNCTION(mu1_, mu2_, mu3_) { mu1_.Lock(); mu2_.Lock(); mu3_.Lock(); } void Foo::locklots() EXCLUSIVE_LOCK_FUNCTION(mu1_, mu2_) EXCLUSIVE_LOCK_FUNCTION(mu2_, mu3_) { mu1_.Lock(); mu2_.Lock(); mu3_.Lock(); } void Foo::unlock1() UNLOCK_FUNCTION(mu1_) { mu1_.Unlock(); } void Foo::unlock3() UNLOCK_FUNCTION(mu1_, mu2_, mu3_) { mu1_.Unlock(); mu2_.Unlock(); mu3_.Unlock(); } void Foo::unlocklots() UNLOCK_FUNCTION(mu1_, mu2_) UNLOCK_FUNCTION(mu2_, mu3_) { mu1_.Unlock(); mu2_.Unlock(); mu3_.Unlock(); } void test0() { Foo foo; foo.lock(); foo.unlock(); foo.lock(); foo.lock(); // expected-warning {{acquiring mutex 'foo' that is already held}} foo.unlock(); foo.unlock(); // expected-warning {{releasing mutex 'foo' that was not held}} } void test1() { Foo foo; foo.lock1(); foo.a = 0; foo.unlock1(); foo.lock1(); foo.lock1(); // expected-warning {{acquiring mutex 'foo.mu1_' that is already held}} foo.a = 0; foo.unlock1(); foo.unlock1(); // expected-warning {{releasing mutex 'foo.mu1_' that was not held}} } int test2() { Foo foo; foo.slock1(); int d1 = foo.a; foo.unlock1(); foo.slock1(); foo.slock1(); // expected-warning {{acquiring mutex 'foo.mu1_' that is already held}} int d2 = foo.a; foo.unlock1(); foo.unlock1(); // expected-warning {{releasing mutex 'foo.mu1_' that was not held}} return d1 + d2; } void test3() { Foo foo; foo.lock3(); foo.a = 0; foo.b = 0; foo.c = 0; foo.unlock3(); foo.lock3(); foo.lock3(); // \ // expected-warning {{acquiring mutex 'foo.mu1_' that is already held}} \ // expected-warning {{acquiring mutex 'foo.mu2_' that is already held}} \ // expected-warning {{acquiring mutex 'foo.mu3_' that is already held}} foo.a = 0; foo.b = 0; foo.c = 0; foo.unlock3(); foo.unlock3(); // \ // expected-warning {{releasing mutex 'foo.mu1_' that was not held}} \ // expected-warning {{releasing mutex 'foo.mu2_' that was not held}} \ // expected-warning {{releasing mutex 'foo.mu3_' that was not held}} } void testlots() { Foo foo; foo.locklots(); foo.a = 0; foo.b = 0; foo.c = 0; foo.unlocklots(); foo.locklots(); foo.locklots(); // \ // expected-warning {{acquiring mutex 'foo.mu1_' that is already held}} \ // expected-warning {{acquiring mutex 'foo.mu2_' that is already held}} \ // expected-warning {{acquiring mutex 'foo.mu3_' that is already held}} foo.a = 0; foo.b = 0; foo.c = 0; foo.unlocklots(); foo.unlocklots(); // \ // expected-warning {{releasing mutex 'foo.mu1_' that was not held}} \ // expected-warning {{releasing mutex 'foo.mu2_' that was not held}} \ // expected-warning {{releasing mutex 'foo.mu3_' that was not held}} } } // end namespace DuplicateAttributeTest namespace TryLockEqTest { class Foo { Mutex mu_; int a GUARDED_BY(mu_); bool c; int tryLockMutexI() EXCLUSIVE_TRYLOCK_FUNCTION(1, mu_); Mutex* tryLockMutexP() EXCLUSIVE_TRYLOCK_FUNCTION(1, mu_); void unlock() UNLOCK_FUNCTION(mu_); void test1(); void test2(); }; void Foo::test1() { if (tryLockMutexP() == 0) { a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} return; } a = 0; unlock(); if (tryLockMutexP() != 0) { a = 0; unlock(); } if (0 != tryLockMutexP()) { a = 0; unlock(); } if (!(tryLockMutexP() == 0)) { a = 0; unlock(); } if (tryLockMutexI() == 0) { a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} return; } a = 0; unlock(); if (0 == tryLockMutexI()) { a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} return; } a = 0; unlock(); if (tryLockMutexI() == 1) { a = 0; unlock(); } if (mu_.TryLock() == false) { a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} return; } a = 0; unlock(); if (mu_.TryLock() == true) { a = 0; unlock(); } else { a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} } #if __has_feature(cxx_nullptr) if (tryLockMutexP() == nullptr) { a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} return; } a = 0; unlock(); #endif } } // end namespace TryLockEqTest namespace ExistentialPatternMatching { class Graph { public: Mutex mu_; }; void LockAllGraphs() EXCLUSIVE_LOCK_FUNCTION(&Graph::mu_); void UnlockAllGraphs() UNLOCK_FUNCTION(&Graph::mu_); class Node { public: int a GUARDED_BY(&Graph::mu_); void foo() EXCLUSIVE_LOCKS_REQUIRED(&Graph::mu_) { a = 0; } void foo2() LOCKS_EXCLUDED(&Graph::mu_); }; void test() { Graph g1; Graph g2; Node n1; n1.a = 0; // expected-warning {{writing variable 'a' requires holding mutex '&ExistentialPatternMatching::Graph::mu_' exclusively}} n1.foo(); // expected-warning {{calling function 'foo' requires holding mutex '&ExistentialPatternMatching::Graph::mu_' exclusively}} n1.foo2(); g1.mu_.Lock(); n1.a = 0; n1.foo(); n1.foo2(); // expected-warning {{cannot call function 'foo2' while mutex '&ExistentialPatternMatching::Graph::mu_' is held}} g1.mu_.Unlock(); g2.mu_.Lock(); n1.a = 0; n1.foo(); n1.foo2(); // expected-warning {{cannot call function 'foo2' while mutex '&ExistentialPatternMatching::Graph::mu_' is held}} g2.mu_.Unlock(); LockAllGraphs(); n1.a = 0; n1.foo(); n1.foo2(); // expected-warning {{cannot call function 'foo2' while mutex '&ExistentialPatternMatching::Graph::mu_' is held}} UnlockAllGraphs(); LockAllGraphs(); g1.mu_.Unlock(); LockAllGraphs(); g2.mu_.Unlock(); LockAllGraphs(); g1.mu_.Lock(); // expected-warning {{acquiring mutex 'g1.mu_' that is already held}} g1.mu_.Unlock(); } } // end namespace ExistentialPatternMatching namespace StringIgnoreTest { class Foo { public: Mutex mu_; void lock() EXCLUSIVE_LOCK_FUNCTION(""); void unlock() UNLOCK_FUNCTION(""); void goober() EXCLUSIVE_LOCKS_REQUIRED(""); void roober() SHARED_LOCKS_REQUIRED(""); }; class Bar : public Foo { public: void bar(Foo* f) { f->unlock(); f->goober(); f->roober(); f->lock(); }; }; } // end namespace StringIgnoreTest namespace LockReturnedScopeFix { class Base { protected: struct Inner; bool c; const Mutex& getLock(const Inner* i); void lockInner (Inner* i) EXCLUSIVE_LOCK_FUNCTION(getLock(i)); void unlockInner(Inner* i) UNLOCK_FUNCTION(getLock(i)); void foo(Inner* i) EXCLUSIVE_LOCKS_REQUIRED(getLock(i)); void bar(Inner* i); }; struct Base::Inner { Mutex lock_; void doSomething() EXCLUSIVE_LOCKS_REQUIRED(lock_); }; const Mutex& Base::getLock(const Inner* i) LOCK_RETURNED(i->lock_) { return i->lock_; } void Base::foo(Inner* i) { i->doSomething(); } void Base::bar(Inner* i) { if (c) { i->lock_.Lock(); unlockInner(i); } else { lockInner(i); i->lock_.Unlock(); } } } // end namespace LockReturnedScopeFix namespace TrylockWithCleanups { struct Foo { Mutex mu_; int a GUARDED_BY(mu_); }; Foo* GetAndLockFoo(const MyString& s) EXCLUSIVE_TRYLOCK_FUNCTION(true, &Foo::mu_); static void test() { Foo* lt = GetAndLockFoo("foo"); if (!lt) return; int a = lt->a; lt->mu_.Unlock(); } } // end namespace TrylockWithCleanups namespace UniversalLock { class Foo { Mutex mu_; bool c; int a GUARDED_BY(mu_); void r_foo() SHARED_LOCKS_REQUIRED(mu_); void w_foo() EXCLUSIVE_LOCKS_REQUIRED(mu_); void test1() { int b; beginNoWarnOnReads(); b = a; r_foo(); endNoWarnOnReads(); beginNoWarnOnWrites(); a = 0; w_foo(); endNoWarnOnWrites(); } // don't warn on joins with universal lock void test2() { if (c) { beginNoWarnOnWrites(); } a = 0; // \ // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} endNoWarnOnWrites(); // \ // expected-warning {{releasing mutex '*' that was not held}} } // make sure the universal lock joins properly void test3() { if (c) { mu_.Lock(); beginNoWarnOnWrites(); } else { beginNoWarnOnWrites(); mu_.Lock(); } a = 0; endNoWarnOnWrites(); mu_.Unlock(); } // combine universal lock with other locks void test4() { beginNoWarnOnWrites(); mu_.Lock(); mu_.Unlock(); endNoWarnOnWrites(); mu_.Lock(); beginNoWarnOnWrites(); endNoWarnOnWrites(); mu_.Unlock(); mu_.Lock(); beginNoWarnOnWrites(); mu_.Unlock(); endNoWarnOnWrites(); } }; } // end namespace UniversalLock namespace TemplateLockReturned { template<class T> class BaseT { public: virtual void baseMethod() = 0; Mutex* get_mutex() LOCK_RETURNED(mutex_) { return &mutex_; } Mutex mutex_; int a GUARDED_BY(mutex_); }; class Derived : public BaseT<int> { public: void baseMethod() EXCLUSIVE_LOCKS_REQUIRED(get_mutex()) { a = 0; } }; } // end namespace TemplateLockReturned namespace ExprMatchingBugFix { class Foo { public: Mutex mu_; }; class Bar { public: bool c; Foo* foo; Bar(Foo* f) : foo(f) { } struct Nested { Foo* foo; Nested(Foo* f) : foo(f) { } void unlockFoo() UNLOCK_FUNCTION(&Foo::mu_); }; void test(); }; void Bar::test() { foo->mu_.Lock(); if (c) { Nested *n = new Nested(foo); n->unlockFoo(); } else { foo->mu_.Unlock(); } } }; // end namespace ExprMatchingBugfix namespace ComplexNameTest { class Foo { public: static Mutex mu_; Foo() EXCLUSIVE_LOCKS_REQUIRED(mu_) { } ~Foo() EXCLUSIVE_LOCKS_REQUIRED(mu_) { } int operator[](int i) EXCLUSIVE_LOCKS_REQUIRED(mu_) { return 0; } }; class Bar { public: static Mutex mu_; Bar() LOCKS_EXCLUDED(mu_) { } ~Bar() LOCKS_EXCLUDED(mu_) { } int operator[](int i) LOCKS_EXCLUDED(mu_) { return 0; } }; void test1() { Foo f; // expected-warning {{calling function 'Foo' requires holding mutex 'mu_' exclusively}} int a = f[0]; // expected-warning {{calling function 'operator[]' requires holding mutex 'mu_' exclusively}} } // expected-warning {{calling function '~Foo' requires holding mutex 'mu_' exclusively}} void test2() { Bar::mu_.Lock(); { Bar b; // expected-warning {{cannot call function 'Bar' while mutex 'mu_' is held}} int a = b[0]; // expected-warning {{cannot call function 'operator[]' while mutex 'mu_' is held}} } // expected-warning {{cannot call function '~Bar' while mutex 'mu_' is held}} Bar::mu_.Unlock(); } }; // end namespace ComplexNameTest namespace UnreachableExitTest { class FemmeFatale { public: FemmeFatale(); ~FemmeFatale() __attribute__((noreturn)); }; void exitNow() __attribute__((noreturn)); void exitDestruct(const MyString& ms) __attribute__((noreturn)); Mutex fatalmu_; void test1() EXCLUSIVE_LOCKS_REQUIRED(fatalmu_) { exitNow(); } void test2() EXCLUSIVE_LOCKS_REQUIRED(fatalmu_) { FemmeFatale femme; } bool c; void test3() EXCLUSIVE_LOCKS_REQUIRED(fatalmu_) { if (c) { exitNow(); } else { FemmeFatale femme; } } void test4() EXCLUSIVE_LOCKS_REQUIRED(fatalmu_) { exitDestruct("foo"); } } // end namespace UnreachableExitTest namespace VirtualMethodCanonicalizationTest { class Base { public: virtual Mutex* getMutex() = 0; }; class Base2 : public Base { public: Mutex* getMutex(); }; class Base3 : public Base2 { public: Mutex* getMutex(); }; class Derived : public Base3 { public: Mutex* getMutex(); // overrides Base::getMutex() }; void baseFun(Base *b) EXCLUSIVE_LOCKS_REQUIRED(b->getMutex()) { } void derivedFun(Derived *d) EXCLUSIVE_LOCKS_REQUIRED(d->getMutex()) { baseFun(d); } } // end namespace VirtualMethodCanonicalizationTest namespace TemplateFunctionParamRemapTest { template <class T> struct Cell { T dummy_; Mutex* mu_; }; class Foo { public: template <class T> void elr(Cell<T>* c) __attribute__((exclusive_locks_required(c->mu_))); void test(); }; template<class T> void Foo::elr(Cell<T>* c1) { } void Foo::test() { Cell<int> cell; elr(&cell); // \ // expected-warning {{calling function 'elr' requires holding mutex 'cell.mu_' exclusively}} } template<class T> void globalELR(Cell<T>* c) __attribute__((exclusive_locks_required(c->mu_))); template<class T> void globalELR(Cell<T>* c1) { } void globalTest() { Cell<int> cell; globalELR(&cell); // \ // expected-warning {{calling function 'globalELR' requires holding mutex 'cell.mu_' exclusively}} } template<class T> void globalELR2(Cell<T>* c) __attribute__((exclusive_locks_required(c->mu_))); // second declaration template<class T> void globalELR2(Cell<T>* c2); template<class T> void globalELR2(Cell<T>* c3) { } // re-declaration after definition template<class T> void globalELR2(Cell<T>* c4); void globalTest2() { Cell<int> cell; globalELR2(&cell); // \ // expected-warning {{calling function 'globalELR2' requires holding mutex 'cell.mu_' exclusively}} } template<class T> class FooT { public: void elr(Cell<T>* c) __attribute__((exclusive_locks_required(c->mu_))); }; template<class T> void FooT<T>::elr(Cell<T>* c1) { } void testFooT() { Cell<int> cell; FooT<int> foo; foo.elr(&cell); // \ // expected-warning {{calling function 'elr' requires holding mutex 'cell.mu_' exclusively}} } } // end namespace TemplateFunctionParamRemapTest namespace SelfConstructorTest { class SelfLock { public: SelfLock() EXCLUSIVE_LOCK_FUNCTION(mu_); ~SelfLock() UNLOCK_FUNCTION(mu_); void foo() EXCLUSIVE_LOCKS_REQUIRED(mu_); Mutex mu_; }; class LOCKABLE SelfLock2 { public: SelfLock2() EXCLUSIVE_LOCK_FUNCTION(); ~SelfLock2() UNLOCK_FUNCTION(); void foo() EXCLUSIVE_LOCKS_REQUIRED(this); }; void test() { SelfLock s; s.foo(); } void test2() { SelfLock2 s2; s2.foo(); } } // end namespace SelfConstructorTest namespace MultipleAttributeTest { class Foo { Mutex mu1_; Mutex mu2_; int a GUARDED_BY(mu1_); int b GUARDED_BY(mu2_); int c GUARDED_BY(mu1_) GUARDED_BY(mu2_); int* d PT_GUARDED_BY(mu1_) PT_GUARDED_BY(mu2_); void foo1() EXCLUSIVE_LOCKS_REQUIRED(mu1_) EXCLUSIVE_LOCKS_REQUIRED(mu2_); void foo2() SHARED_LOCKS_REQUIRED(mu1_) SHARED_LOCKS_REQUIRED(mu2_); void foo3() LOCKS_EXCLUDED(mu1_) LOCKS_EXCLUDED(mu2_); void lock() EXCLUSIVE_LOCK_FUNCTION(mu1_) EXCLUSIVE_LOCK_FUNCTION(mu2_); void readerlock() SHARED_LOCK_FUNCTION(mu1_) SHARED_LOCK_FUNCTION(mu2_); void unlock() UNLOCK_FUNCTION(mu1_) UNLOCK_FUNCTION(mu2_); bool trylock() EXCLUSIVE_TRYLOCK_FUNCTION(true, mu1_) EXCLUSIVE_TRYLOCK_FUNCTION(true, mu2_); bool readertrylock() SHARED_TRYLOCK_FUNCTION(true, mu1_) SHARED_TRYLOCK_FUNCTION(true, mu2_); void assertBoth() ASSERT_EXCLUSIVE_LOCK(mu1_) ASSERT_EXCLUSIVE_LOCK(mu2_); void assertShared() ASSERT_SHARED_LOCK(mu1_) ASSERT_SHARED_LOCK(mu2_); void test(); void testAssert(); void testAssertShared(); }; void Foo::foo1() { a = 1; b = 2; } void Foo::foo2() { int result = a + b; } void Foo::foo3() { } void Foo::lock() { mu1_.Lock(); mu2_.Lock(); } void Foo::readerlock() { mu1_.ReaderLock(); mu2_.ReaderLock(); } void Foo::unlock() { mu1_.Unlock(); mu2_.Unlock(); } bool Foo::trylock() { return true; } bool Foo::readertrylock() { return true; } void Foo::test() { mu1_.Lock(); foo1(); // expected-warning {{}} c = 0; // expected-warning {{}} *d = 0; // expected-warning {{}} mu1_.Unlock(); mu1_.ReaderLock(); foo2(); // expected-warning {{}} int x = c; // expected-warning {{}} int y = *d; // expected-warning {{}} mu1_.Unlock(); mu2_.Lock(); foo3(); // expected-warning {{}} mu2_.Unlock(); lock(); a = 0; b = 0; unlock(); readerlock(); int z = a + b; unlock(); if (trylock()) { a = 0; b = 0; unlock(); } if (readertrylock()) { int zz = a + b; unlock(); } } // Force duplication of attributes void Foo::assertBoth() { } void Foo::assertShared() { } void Foo::testAssert() { assertBoth(); a = 0; b = 0; } void Foo::testAssertShared() { assertShared(); int zz = a + b; } } // end namespace MultipleAttributeTest namespace GuardedNonPrimitiveTypeTest { class Data { public: Data(int i) : dat(i) { } int getValue() const { return dat; } void setValue(int i) { dat = i; } int operator[](int i) const { return dat; } int& operator[](int i) { return dat; } void operator()() { } private: int dat; }; class DataCell { public: DataCell(const Data& d) : dat(d) { } private: Data dat; }; void showDataCell(const DataCell& dc); class Foo { public: // method call tests void test() { data_.setValue(0); // FIXME -- should be writing \ // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}} int a = data_.getValue(); // \ // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}} datap1_->setValue(0); // FIXME -- should be writing \ // expected-warning {{reading variable 'datap1_' requires holding mutex 'mu_'}} a = datap1_->getValue(); // \ // expected-warning {{reading variable 'datap1_' requires holding mutex 'mu_'}} datap2_->setValue(0); // FIXME -- should be writing \ // expected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}} a = datap2_->getValue(); // \ // expected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}} (*datap2_).setValue(0); // FIXME -- should be writing \ // expected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}} a = (*datap2_).getValue(); // \ // expected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}} mu_.Lock(); data_.setValue(1); datap1_->setValue(1); datap2_->setValue(1); mu_.Unlock(); mu_.ReaderLock(); a = data_.getValue(); datap1_->setValue(0); // reads datap1_, writes *datap1_ a = datap1_->getValue(); a = datap2_->getValue(); mu_.Unlock(); } // operator tests void test2() { data_ = Data(1); // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}} *datap1_ = data_; // expected-warning {{reading variable 'datap1_' requires holding mutex 'mu_'}} \ // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}} *datap2_ = data_; // expected-warning {{writing the value pointed to by 'datap2_' requires holding mutex 'mu_' exclusively}} \ // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}} data_ = *datap1_; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}} \ // expected-warning {{reading variable 'datap1_' requires holding mutex 'mu_'}} data_ = *datap2_; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}} \ // expected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}} data_[0] = 0; // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}} (*datap2_)[0] = 0; // expected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}} data_(); // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}} } // const operator tests void test3() const { Data mydat(data_); // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}} //FIXME //showDataCell(data_); // xpected-warning {{reading variable 'data_' requires holding mutex 'mu_'}} //showDataCell(*datap2_); // xpected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}} int a = data_[0]; // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}} } private: Mutex mu_; Data data_ GUARDED_BY(mu_); Data* datap1_ GUARDED_BY(mu_); Data* datap2_ PT_GUARDED_BY(mu_); }; } // end namespace GuardedNonPrimitiveTypeTest namespace GuardedNonPrimitive_MemberAccess { class Cell { public: Cell(int i); void cellMethod(); int a; }; class Foo { public: int a; Cell c GUARDED_BY(cell_mu_); Cell* cp PT_GUARDED_BY(cell_mu_); void myMethod(); Mutex cell_mu_; }; class Bar { private: Mutex mu_; Foo foo GUARDED_BY(mu_); Foo* foop PT_GUARDED_BY(mu_); void test() { foo.myMethod(); // expected-warning {{reading variable 'foo' requires holding mutex 'mu_'}} int fa = foo.a; // expected-warning {{reading variable 'foo' requires holding mutex 'mu_'}} foo.a = fa; // expected-warning {{writing variable 'foo' requires holding mutex 'mu_' exclusively}} fa = foop->a; // expected-warning {{reading the value pointed to by 'foop' requires holding mutex 'mu_'}} foop->a = fa; // expected-warning {{writing the value pointed to by 'foop' requires holding mutex 'mu_' exclusively}} fa = (*foop).a; // expected-warning {{reading the value pointed to by 'foop' requires holding mutex 'mu_'}} (*foop).a = fa; // expected-warning {{writing the value pointed to by 'foop' requires holding mutex 'mu_' exclusively}} foo.c = Cell(0); // expected-warning {{writing variable 'foo' requires holding mutex 'mu_'}} \ // expected-warning {{writing variable 'c' requires holding mutex 'foo.cell_mu_' exclusively}} foo.c.cellMethod(); // expected-warning {{reading variable 'foo' requires holding mutex 'mu_'}} \ // expected-warning {{reading variable 'c' requires holding mutex 'foo.cell_mu_'}} foop->c = Cell(0); // expected-warning {{writing the value pointed to by 'foop' requires holding mutex 'mu_'}} \ // expected-warning {{writing variable 'c' requires holding mutex 'foop->cell_mu_' exclusively}} foop->c.cellMethod(); // expected-warning {{reading the value pointed to by 'foop' requires holding mutex 'mu_'}} \ // expected-warning {{reading variable 'c' requires holding mutex 'foop->cell_mu_'}} (*foop).c = Cell(0); // expected-warning {{writing the value pointed to by 'foop' requires holding mutex 'mu_'}} \ // expected-warning {{writing variable 'c' requires holding mutex 'foop->cell_mu_' exclusively}} (*foop).c.cellMethod(); // expected-warning {{reading the value pointed to by 'foop' requires holding mutex 'mu_'}} \ // expected-warning {{reading variable 'c' requires holding mutex 'foop->cell_mu_'}} }; }; } // namespace GuardedNonPrimitive_MemberAccess namespace TestThrowExpr { class Foo { Mutex mu_; bool hasError(); void test() { mu_.Lock(); if (hasError()) { throw "ugly"; } mu_.Unlock(); } }; } // end namespace TestThrowExpr namespace UnevaluatedContextTest { // parse attribute expressions in an unevaluated context. static inline Mutex* getMutex1(); static inline Mutex* getMutex2(); void bar() EXCLUSIVE_LOCKS_REQUIRED(getMutex1()); void bar2() EXCLUSIVE_LOCKS_REQUIRED(getMutex1(), getMutex2()); } // end namespace UnevaluatedContextTest namespace LockUnlockFunctionTest { // Check built-in lock functions class LOCKABLE MyLockable { public: void lock() EXCLUSIVE_LOCK_FUNCTION() { mu_.Lock(); } void readerLock() SHARED_LOCK_FUNCTION() { mu_.ReaderLock(); } void unlock() UNLOCK_FUNCTION() { mu_.Unlock(); } private: Mutex mu_; }; class Foo { public: // Correct lock/unlock functions void lock() EXCLUSIVE_LOCK_FUNCTION(mu_) { mu_.Lock(); } void readerLock() SHARED_LOCK_FUNCTION(mu_) { mu_.ReaderLock(); } void unlock() UNLOCK_FUNCTION(mu_) { mu_.Unlock(); } // Check failure to lock. void lockBad() EXCLUSIVE_LOCK_FUNCTION(mu_) { // expected-note {{mutex acquired here}} mu2_.Lock(); mu2_.Unlock(); } // expected-warning {{expecting mutex 'mu_' to be held at the end of function}} void readerLockBad() SHARED_LOCK_FUNCTION(mu_) { // expected-note {{mutex acquired here}} mu2_.Lock(); mu2_.Unlock(); } // expected-warning {{expecting mutex 'mu_' to be held at the end of function}} void unlockBad() UNLOCK_FUNCTION(mu_) { // expected-note {{mutex acquired here}} mu2_.Lock(); mu2_.Unlock(); } // expected-warning {{mutex 'mu_' is still held at the end of function}} // Check locking the wrong thing. void lockBad2() EXCLUSIVE_LOCK_FUNCTION(mu_) { // expected-note {{mutex acquired here}} mu2_.Lock(); // expected-note {{mutex acquired here}} } // expected-warning {{expecting mutex 'mu_' to be held at the end of function}} \ // expected-warning {{mutex 'mu2_' is still held at the end of function}} void readerLockBad2() SHARED_LOCK_FUNCTION(mu_) { // expected-note {{mutex acquired here}} mu2_.ReaderLock(); // expected-note {{mutex acquired here}} } // expected-warning {{expecting mutex 'mu_' to be held at the end of function}} \ // expected-warning {{mutex 'mu2_' is still held at the end of function}} void unlockBad2() UNLOCK_FUNCTION(mu_) { // expected-note {{mutex acquired here}} mu2_.Unlock(); // expected-warning {{releasing mutex 'mu2_' that was not held}} } // expected-warning {{mutex 'mu_' is still held at the end of function}} private: Mutex mu_; Mutex mu2_; }; } // end namespace LockUnlockFunctionTest namespace AssertHeldTest { class Foo { public: int c; int a GUARDED_BY(mu_); Mutex mu_; void test1() { mu_.AssertHeld(); int b = a; a = 0; } void test2() { mu_.AssertReaderHeld(); int b = a; a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} } void test3() { if (c) { mu_.AssertHeld(); } else { mu_.AssertHeld(); } int b = a; a = 0; } void test4() EXCLUSIVE_LOCKS_REQUIRED(mu_) { mu_.AssertHeld(); int b = a; a = 0; } void test5() UNLOCK_FUNCTION(mu_) { mu_.AssertHeld(); mu_.Unlock(); } void test6() { mu_.AssertHeld(); mu_.Unlock(); } // should this be a warning? void test7() { if (c) { mu_.AssertHeld(); } else { mu_.Lock(); } int b = a; a = 0; mu_.Unlock(); } void test8() { if (c) { mu_.Lock(); } else { mu_.AssertHeld(); } int b = a; a = 0; mu_.Unlock(); } void test9() { if (c) { mu_.AssertHeld(); } else { mu_.Lock(); // expected-note {{mutex acquired here}} } } // expected-warning {{mutex 'mu_' is still held at the end of function}} void test10() { if (c) { mu_.Lock(); // expected-note {{mutex acquired here}} } else { mu_.AssertHeld(); } } // expected-warning {{mutex 'mu_' is still held at the end of function}} void assertMu() ASSERT_EXCLUSIVE_LOCK(mu_); void test11() { assertMu(); int b = a; a = 0; } }; } // end namespace AssertHeldTest namespace LogicalConditionalTryLock { class Foo { public: Mutex mu; int a GUARDED_BY(mu); bool c; bool newc(); void test1() { if (c && mu.TryLock()) { a = 0; mu.Unlock(); } } void test2() { bool b = mu.TryLock(); if (c && b) { a = 0; mu.Unlock(); } } void test3() { if (c || !mu.TryLock()) return; a = 0; mu.Unlock(); } void test4() { while (c && mu.TryLock()) { a = 0; c = newc(); mu.Unlock(); } } void test5() { while (c) { if (newc() || !mu.TryLock()) break; a = 0; mu.Unlock(); } } void test6() { mu.Lock(); do { a = 0; mu.Unlock(); } while (newc() && mu.TryLock()); } void test7() { for (bool b = mu.TryLock(); c && b;) { a = 0; mu.Unlock(); } } void test8() { if (c && newc() && mu.TryLock()) { a = 0; mu.Unlock(); } } void test9() { if (!(c && newc() && mu.TryLock())) return; a = 0; mu.Unlock(); } void test10() { if (!(c || !mu.TryLock())) { a = 0; mu.Unlock(); } } }; } // end namespace LogicalConditionalTryLock namespace PtGuardedByTest { void doSomething(); class Cell { public: int a; }; // This mainly duplicates earlier tests, but just to make sure... class PtGuardedBySanityTest { Mutex mu1; Mutex mu2; int* a GUARDED_BY(mu1) PT_GUARDED_BY(mu2); Cell* c GUARDED_BY(mu1) PT_GUARDED_BY(mu2); int sa[10] GUARDED_BY(mu1); Cell sc[10] GUARDED_BY(mu1); void test1() { mu1.Lock(); if (a == 0) doSomething(); // OK, we don't dereference. a = 0; c = 0; if (sa[0] == 42) doSomething(); sa[0] = 57; if (sc[0].a == 42) doSomething(); sc[0].a = 57; mu1.Unlock(); } void test2() { mu1.ReaderLock(); if (*a == 0) doSomething(); // expected-warning {{reading the value pointed to by 'a' requires holding mutex 'mu2'}} *a = 0; // expected-warning {{writing the value pointed to by 'a' requires holding mutex 'mu2' exclusively}} if (c->a == 0) doSomething(); // expected-warning {{reading the value pointed to by 'c' requires holding mutex 'mu2'}} c->a = 0; // expected-warning {{writing the value pointed to by 'c' requires holding mutex 'mu2' exclusively}} if ((*c).a == 0) doSomething(); // expected-warning {{reading the value pointed to by 'c' requires holding mutex 'mu2'}} (*c).a = 0; // expected-warning {{writing the value pointed to by 'c' requires holding mutex 'mu2' exclusively}} if (a[0] == 42) doSomething(); // expected-warning {{reading the value pointed to by 'a' requires holding mutex 'mu2'}} a[0] = 57; // expected-warning {{writing the value pointed to by 'a' requires holding mutex 'mu2' exclusively}} if (c[0].a == 42) doSomething(); // expected-warning {{reading the value pointed to by 'c' requires holding mutex 'mu2'}} c[0].a = 57; // expected-warning {{writing the value pointed to by 'c' requires holding mutex 'mu2' exclusively}} mu1.Unlock(); } void test3() { mu2.Lock(); if (*a == 0) doSomething(); // expected-warning {{reading variable 'a' requires holding mutex 'mu1'}} *a = 0; // expected-warning {{reading variable 'a' requires holding mutex 'mu1'}} if (c->a == 0) doSomething(); // expected-warning {{reading variable 'c' requires holding mutex 'mu1'}} c->a = 0; // expected-warning {{reading variable 'c' requires holding mutex 'mu1'}} if ((*c).a == 0) doSomething(); // expected-warning {{reading variable 'c' requires holding mutex 'mu1'}} (*c).a = 0; // expected-warning {{reading variable 'c' requires holding mutex 'mu1'}} if (a[0] == 42) doSomething(); // expected-warning {{reading variable 'a' requires holding mutex 'mu1'}} a[0] = 57; // expected-warning {{reading variable 'a' requires holding mutex 'mu1'}} if (c[0].a == 42) doSomething(); // expected-warning {{reading variable 'c' requires holding mutex 'mu1'}} c[0].a = 57; // expected-warning {{reading variable 'c' requires holding mutex 'mu1'}} mu2.Unlock(); } void test4() { // Literal arrays if (sa[0] == 42) doSomething(); // expected-warning {{reading variable 'sa' requires holding mutex 'mu1'}} sa[0] = 57; // expected-warning {{writing variable 'sa' requires holding mutex 'mu1' exclusively}} if (sc[0].a == 42) doSomething(); // expected-warning {{reading variable 'sc' requires holding mutex 'mu1'}} sc[0].a = 57; // expected-warning {{writing variable 'sc' requires holding mutex 'mu1' exclusively}} if (*sa == 42) doSomething(); // expected-warning {{reading variable 'sa' requires holding mutex 'mu1'}} *sa = 57; // expected-warning {{writing variable 'sa' requires holding mutex 'mu1' exclusively}} if ((*sc).a == 42) doSomething(); // expected-warning {{reading variable 'sc' requires holding mutex 'mu1'}} (*sc).a = 57; // expected-warning {{writing variable 'sc' requires holding mutex 'mu1' exclusively}} if (sc->a == 42) doSomething(); // expected-warning {{reading variable 'sc' requires holding mutex 'mu1'}} sc->a = 57; // expected-warning {{writing variable 'sc' requires holding mutex 'mu1' exclusively}} } void test5() { mu1.ReaderLock(); // OK -- correct use. mu2.Lock(); if (*a == 0) doSomething(); *a = 0; if (c->a == 0) doSomething(); c->a = 0; if ((*c).a == 0) doSomething(); (*c).a = 0; mu2.Unlock(); mu1.Unlock(); } }; class SmartPtr_PtGuardedBy_Test { Mutex mu1; Mutex mu2; SmartPtr<int> sp GUARDED_BY(mu1) PT_GUARDED_BY(mu2); SmartPtr<Cell> sq GUARDED_BY(mu1) PT_GUARDED_BY(mu2); void test1() { mu1.ReaderLock(); mu2.Lock(); sp.get(); if (*sp == 0) doSomething(); *sp = 0; sq->a = 0; if (sp[0] == 0) doSomething(); sp[0] = 0; mu2.Unlock(); mu1.Unlock(); } void test2() { mu2.Lock(); sp.get(); // expected-warning {{reading variable 'sp' requires holding mutex 'mu1'}} if (*sp == 0) doSomething(); // expected-warning {{reading variable 'sp' requires holding mutex 'mu1'}} *sp = 0; // expected-warning {{reading variable 'sp' requires holding mutex 'mu1'}} sq->a = 0; // expected-warning {{reading variable 'sq' requires holding mutex 'mu1'}} if (sp[0] == 0) doSomething(); // expected-warning {{reading variable 'sp' requires holding mutex 'mu1'}} sp[0] = 0; // expected-warning {{reading variable 'sp' requires holding mutex 'mu1'}} if (sq[0].a == 0) doSomething(); // expected-warning {{reading variable 'sq' requires holding mutex 'mu1'}} sq[0].a = 0; // expected-warning {{reading variable 'sq' requires holding mutex 'mu1'}} mu2.Unlock(); } void test3() { mu1.Lock(); sp.get(); if (*sp == 0) doSomething(); // expected-warning {{reading the value pointed to by 'sp' requires holding mutex 'mu2'}} *sp = 0; // expected-warning {{reading the value pointed to by 'sp' requires holding mutex 'mu2'}} sq->a = 0; // expected-warning {{reading the value pointed to by 'sq' requires holding mutex 'mu2'}} if (sp[0] == 0) doSomething(); // expected-warning {{reading the value pointed to by 'sp' requires holding mutex 'mu2'}} sp[0] = 0; // expected-warning {{reading the value pointed to by 'sp' requires holding mutex 'mu2'}} if (sq[0].a == 0) doSomething(); // expected-warning {{reading the value pointed to by 'sq' requires holding mutex 'mu2'}} sq[0].a = 0; // expected-warning {{reading the value pointed to by 'sq' requires holding mutex 'mu2'}} mu1.Unlock(); } }; } // end namespace PtGuardedByTest namespace NonMemberCalleeICETest { class A { void Run() { (RunHelper)(); // expected-warning {{calling function 'RunHelper' requires holding mutex 'M' exclusively}} } void RunHelper() __attribute__((exclusive_locks_required(M))); Mutex M; }; } // end namespace NonMemberCalleeICETest namespace pt_guard_attribute_type { int i PT_GUARDED_BY(sls_mu); // expected-warning {{'pt_guarded_by' only applies to pointer types; type here is 'int'}} int j PT_GUARDED_VAR; // expected-warning {{'pt_guarded_var' only applies to pointer types; type here is 'int'}} void test() { int i PT_GUARDED_BY(sls_mu); // expected-warning {{'pt_guarded_by' attribute only applies to fields and global variables}} int j PT_GUARDED_VAR; // expected-warning {{'pt_guarded_var' attribute only applies to fields and global variables}} typedef int PT_GUARDED_BY(sls_mu) bad1; // expected-warning {{'pt_guarded_by' attribute only applies to fields and global variables}} typedef int PT_GUARDED_VAR bad2; // expected-warning {{'pt_guarded_var' attribute only applies to fields and global variables}} } } // end namespace pt_guard_attribute_type namespace ThreadAttributesOnLambdas { class Foo { Mutex mu_; void LockedFunction() EXCLUSIVE_LOCKS_REQUIRED(mu_); void test() { auto func1 = [this]() EXCLUSIVE_LOCKS_REQUIRED(mu_) { LockedFunction(); }; auto func2 = [this]() NO_THREAD_SAFETY_ANALYSIS { LockedFunction(); }; auto func3 = [this]() EXCLUSIVE_LOCK_FUNCTION(mu_) { mu_.Lock(); }; func1(); // expected-warning {{calling function 'operator()' requires holding mutex 'mu_' exclusively}} func2(); func3(); mu_.Unlock(); } }; } // end namespace ThreadAttributesOnLambdas namespace AttributeExpressionCornerCases { class Foo { int a GUARDED_BY(getMu()); Mutex* getMu() LOCK_RETURNED(""); Mutex* getUniv() LOCK_RETURNED("*"); void test1() { a = 0; } void test2() EXCLUSIVE_LOCKS_REQUIRED(getUniv()) { a = 0; } void foo(Mutex* mu) EXCLUSIVE_LOCKS_REQUIRED(mu); void test3() { foo(nullptr); } }; class MapTest { struct MuCell { Mutex* mu; }; MyMap<MyString, Mutex*> map; MyMap<MyString, MuCell> mapCell; int a GUARDED_BY(map["foo"]); int b GUARDED_BY(mapCell["foo"].mu); void test() { map["foo"]->Lock(); a = 0; map["foo"]->Unlock(); } void test2() { mapCell["foo"].mu->Lock(); b = 0; mapCell["foo"].mu->Unlock(); } }; class PreciseSmartPtr { SmartPtr<Mutex> mu; int val GUARDED_BY(mu); static bool compare(PreciseSmartPtr& a, PreciseSmartPtr &b) { a.mu->Lock(); bool result = (a.val == b.val); // expected-warning {{reading variable 'val' requires holding mutex 'b.mu'}} \ // expected-note {{found near match 'a.mu'}} a.mu->Unlock(); return result; } }; class SmartRedeclare { SmartPtr<Mutex> mu; int val GUARDED_BY(mu); void test() EXCLUSIVE_LOCKS_REQUIRED(mu); void test2() EXCLUSIVE_LOCKS_REQUIRED(mu.get()); void test3() EXCLUSIVE_LOCKS_REQUIRED(mu.get()); }; void SmartRedeclare::test() EXCLUSIVE_LOCKS_REQUIRED(mu.get()) { val = 0; } void SmartRedeclare::test2() EXCLUSIVE_LOCKS_REQUIRED(mu) { val = 0; } void SmartRedeclare::test3() { val = 0; } namespace CustomMutex { class LOCKABLE BaseMutex { }; class DerivedMutex : public BaseMutex { }; void customLock(const BaseMutex *m) EXCLUSIVE_LOCK_FUNCTION(m); void customUnlock(const BaseMutex *m) UNLOCK_FUNCTION(m); static struct DerivedMutex custMu; static void doSomethingRequiringLock() EXCLUSIVE_LOCKS_REQUIRED(custMu) { } void customTest() { customLock(reinterpret_cast<BaseMutex*>(&custMu)); // ignore casts doSomethingRequiringLock(); customUnlock(reinterpret_cast<BaseMutex*>(&custMu)); } } // end namespace CustomMutex } // end AttributeExpressionCornerCases namespace ScopedLockReturnedInvalid { class Opaque; Mutex* getMutex(Opaque* o) LOCK_RETURNED(""); void test(Opaque* o) { MutexLock lock(getMutex(o)); } } // end namespace ScopedLockReturnedInvalid namespace NegativeRequirements { class Bar { Mutex mu; int a GUARDED_BY(mu); public: void baz() EXCLUSIVE_LOCKS_REQUIRED(!mu) { mu.Lock(); a = 0; mu.Unlock(); } }; class Foo { Mutex mu; int a GUARDED_BY(mu); public: void foo() { mu.Lock(); // warning? needs !mu? baz(); // expected-warning {{cannot call function 'baz' while mutex 'mu' is held}} bar(); mu.Unlock(); } void bar() { bar2(); // expected-warning {{calling function 'bar2' requires holding '!mu'}} } void bar2() EXCLUSIVE_LOCKS_REQUIRED(!mu) { baz(); } void baz() EXCLUSIVE_LOCKS_REQUIRED(!mu) { mu.Lock(); a = 0; mu.Unlock(); } void test() { Bar b; b.baz(); // no warning -- in different class. } }; } // end namespace NegativeRequirements namespace NegativeThreadRoles { typedef int __attribute__((capability("role"))) ThreadRole; void acquire(ThreadRole R) __attribute__((exclusive_lock_function(R))) __attribute__((no_thread_safety_analysis)) {} void release(ThreadRole R) __attribute__((unlock_function(R))) __attribute__((no_thread_safety_analysis)) {} ThreadRole FlightControl, Logger; extern void enque_log_msg(const char *msg); void log_msg(const char *msg) { enque_log_msg(msg); } void dispatch_log(const char *msg) __attribute__((requires_capability(!FlightControl))) {} void dispatch_log2(const char *msg) __attribute__((requires_capability(Logger))) {} void flight_control_entry(void) __attribute__((requires_capability(FlightControl))) { dispatch_log("wrong"); /* expected-warning {{cannot call function 'dispatch_log' while mutex 'FlightControl' is held}} */ dispatch_log2("also wrong"); /* expected-warning {{calling function 'dispatch_log2' requires holding role 'Logger' exclusively}} */ } void spawn_fake_flight_control_thread(void) { acquire(FlightControl); flight_control_entry(); release(FlightControl); } extern const char *deque_log_msg(void) __attribute__((requires_capability(Logger))); void logger_entry(void) __attribute__((requires_capability(Logger))) { const char *msg; while ((msg = deque_log_msg())) { dispatch_log(msg); } } void spawn_fake_logger_thread(void) { acquire(Logger); logger_entry(); release(Logger); } int main(void) { spawn_fake_flight_control_thread(); spawn_fake_logger_thread(); for (;;) ; /* Pretend to dispatch things. */ return 0; } } // end namespace NegativeThreadRoles namespace AssertSharedExclusive { void doSomething(); class Foo { Mutex mu; int a GUARDED_BY(mu); void test() SHARED_LOCKS_REQUIRED(mu) { mu.AssertHeld(); if (a > 0) doSomething(); } }; } // end namespace AssertSharedExclusive namespace RangeBasedForAndReferences { class Foo { struct MyStruct { int a; }; Mutex mu; int a GUARDED_BY(mu); MyContainer<int> cntr GUARDED_BY(mu); MyStruct s GUARDED_BY(mu); int arr[10] GUARDED_BY(mu); void nonref_test() { int b = a; // expected-warning {{reading variable 'a' requires holding mutex 'mu'}} b = 0; // no warning } void auto_test() { auto b = a; // expected-warning {{reading variable 'a' requires holding mutex 'mu'}} b = 0; // no warning auto &c = a; // no warning c = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}} } void ref_test() { int &b = a; int &c = b; int &d = c; b = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}} c = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}} d = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}} MyStruct &rs = s; rs.a = 0; // expected-warning {{writing variable 's' requires holding mutex 'mu' exclusively}} int (&rarr)[10] = arr; rarr[2] = 0; // expected-warning {{writing variable 'arr' requires holding mutex 'mu' exclusively}} } void ptr_test() { int *b = &a; *b = 0; // no expected warning yet } void for_test() { int total = 0; for (int i : cntr) { // expected-warning2 {{reading variable 'cntr' requires holding mutex 'mu'}} total += i; } } }; } // end namespace RangeBasedForAndReferences namespace PassByRefTest { class Foo { public: Foo() : a(0), b(0) { } int a; int b; void operator+(const Foo& f); void operator[](const Foo& g); }; template<class T> T&& mymove(T& f); // test top-level functions void copy(Foo f); void write1(Foo& f); void write2(int a, Foo& f); void read1(const Foo& f); void read2(int a, const Foo& f); void destroy(Foo&& f); void operator/(const Foo& f, const Foo& g); void operator*(const Foo& f, const Foo& g); class Bar { public: Mutex mu; Foo foo GUARDED_BY(mu); Foo foo2 GUARDED_BY(mu); Foo* foop PT_GUARDED_BY(mu); SmartPtr<Foo> foosp PT_GUARDED_BY(mu); // test methods. void mwrite1(Foo& f); void mwrite2(int a, Foo& f); void mread1(const Foo& f); void mread2(int a, const Foo& f); // static methods static void smwrite1(Foo& f); static void smwrite2(int a, Foo& f); static void smread1(const Foo& f); static void smread2(int a, const Foo& f); void operator<<(const Foo& f); void test1() { copy(foo); // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}} write1(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} write2(10, foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} read1(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} read2(10, foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} destroy(mymove(foo)); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} mwrite1(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} mwrite2(10, foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} mread1(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} mread2(10, foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} smwrite1(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} smwrite2(10, foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} smread1(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} smread2(10, foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} foo + foo2; // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}} \ // expected-warning {{passing variable 'foo2' by reference requires holding mutex 'mu'}} foo / foo2; // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}} \ // expected-warning {{passing variable 'foo2' by reference requires holding mutex 'mu'}} foo * foo2; // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}} \ // expected-warning {{passing variable 'foo2' by reference requires holding mutex 'mu'}} foo[foo2]; // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}} \ // expected-warning {{passing variable 'foo2' by reference requires holding mutex 'mu'}} (*this) << foo; // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} copy(*foop); // expected-warning {{reading the value pointed to by 'foop' requires holding mutex 'mu'}} write1(*foop); // expected-warning {{passing the value that 'foop' points to by reference requires holding mutex 'mu'}} write2(10, *foop); // expected-warning {{passing the value that 'foop' points to by reference requires holding mutex 'mu'}} read1(*foop); // expected-warning {{passing the value that 'foop' points to by reference requires holding mutex 'mu'}} read2(10, *foop); // expected-warning {{passing the value that 'foop' points to by reference requires holding mutex 'mu'}} destroy(mymove(*foop)); // expected-warning {{passing the value that 'foop' points to by reference requires holding mutex 'mu'}} copy(*foosp); // expected-warning {{reading the value pointed to by 'foosp' requires holding mutex 'mu'}} write1(*foosp); // expected-warning {{reading the value pointed to by 'foosp' requires holding mutex 'mu'}} write2(10, *foosp); // expected-warning {{reading the value pointed to by 'foosp' requires holding mutex 'mu'}} read1(*foosp); // expected-warning {{reading the value pointed to by 'foosp' requires holding mutex 'mu'}} read2(10, *foosp); // expected-warning {{reading the value pointed to by 'foosp' requires holding mutex 'mu'}} destroy(mymove(*foosp)); // expected-warning {{reading the value pointed to by 'foosp' requires holding mutex 'mu'}} // TODO -- these require better smart pointer handling. copy(*foosp.get()); write1(*foosp.get()); write2(10, *foosp.get()); read1(*foosp.get()); read2(10, *foosp.get()); destroy(mymove(*foosp.get())); } }; } // end namespace PassByRefTest namespace AcquiredBeforeAfterText { class Foo { Mutex mu1 ACQUIRED_BEFORE(mu2, mu3); Mutex mu2; Mutex mu3; void test1() { mu1.Lock(); mu2.Lock(); mu3.Lock(); mu3.Unlock(); mu2.Unlock(); mu1.Unlock(); } void test2() { mu2.Lock(); mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu2'}} mu1.Unlock(); mu2.Unlock(); } void test3() { mu3.Lock(); mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu3'}} mu1.Unlock(); mu3.Unlock(); } void test4() EXCLUSIVE_LOCKS_REQUIRED(mu1) { mu2.Lock(); mu2.Unlock(); } void test5() EXCLUSIVE_LOCKS_REQUIRED(mu2) { mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu2'}} mu1.Unlock(); } void test6() EXCLUSIVE_LOCKS_REQUIRED(mu2) { mu1.AssertHeld(); } void test7() EXCLUSIVE_LOCKS_REQUIRED(mu1, mu2, mu3) { } void test8() EXCLUSIVE_LOCKS_REQUIRED(mu3, mu2, mu1) { } }; class Foo2 { Mutex mu1; Mutex mu2 ACQUIRED_AFTER(mu1); Mutex mu3 ACQUIRED_AFTER(mu1); void test1() { mu1.Lock(); mu2.Lock(); mu3.Lock(); mu3.Unlock(); mu2.Unlock(); mu1.Unlock(); } void test2() { mu2.Lock(); mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu2'}} mu1.Unlock(); mu2.Unlock(); } void test3() { mu3.Lock(); mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu3'}} mu1.Unlock(); mu3.Unlock(); } }; class Foo3 { Mutex mu1 ACQUIRED_BEFORE(mu2); Mutex mu2; Mutex mu3 ACQUIRED_AFTER(mu2) ACQUIRED_BEFORE(mu4); Mutex mu4; void test1() { mu1.Lock(); mu2.Lock(); mu3.Lock(); mu4.Lock(); mu4.Unlock(); mu3.Unlock(); mu2.Unlock(); mu1.Unlock(); } void test2() { mu4.Lock(); mu2.Lock(); // expected-warning {{mutex 'mu2' must be acquired before 'mu4'}} mu2.Unlock(); mu4.Unlock(); } void test3() { mu4.Lock(); mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu4'}} mu1.Unlock(); mu4.Unlock(); } void test4() { mu3.Lock(); mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu3'}} mu1.Unlock(); mu3.Unlock(); } }; // Test transitive DAG traversal with AFTER class Foo4 { Mutex mu1; Mutex mu2 ACQUIRED_AFTER(mu1); Mutex mu3 ACQUIRED_AFTER(mu1); Mutex mu4 ACQUIRED_AFTER(mu2, mu3); Mutex mu5 ACQUIRED_AFTER(mu4); Mutex mu6 ACQUIRED_AFTER(mu4); Mutex mu7 ACQUIRED_AFTER(mu5, mu6); Mutex mu8 ACQUIRED_AFTER(mu7); void test() { mu8.Lock(); mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu8'}} mu1.Unlock(); mu8.Unlock(); } }; // Test transitive DAG traversal with BEFORE class Foo5 { Mutex mu1 ACQUIRED_BEFORE(mu2, mu3); Mutex mu2 ACQUIRED_BEFORE(mu4); Mutex mu3 ACQUIRED_BEFORE(mu4); Mutex mu4 ACQUIRED_BEFORE(mu5, mu6); Mutex mu5 ACQUIRED_BEFORE(mu7); Mutex mu6 ACQUIRED_BEFORE(mu7); Mutex mu7 ACQUIRED_BEFORE(mu8); Mutex mu8; void test() { mu8.Lock(); mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu8'}} mu1.Unlock(); mu8.Unlock(); } }; class Foo6 { Mutex mu1 ACQUIRED_AFTER(mu3); // expected-warning {{Cycle in acquired_before/after dependencies, starting with 'mu1'}} Mutex mu2 ACQUIRED_AFTER(mu1); // expected-warning {{Cycle in acquired_before/after dependencies, starting with 'mu2'}} Mutex mu3 ACQUIRED_AFTER(mu2); // expected-warning {{Cycle in acquired_before/after dependencies, starting with 'mu3'}} Mutex mu_b ACQUIRED_BEFORE(mu_b); // expected-warning {{Cycle in acquired_before/after dependencies, starting with 'mu_b'}} Mutex mu_a ACQUIRED_AFTER(mu_a); // expected-warning {{Cycle in acquired_before/after dependencies, starting with 'mu_a'}} void test0() { mu_a.Lock(); mu_b.Lock(); mu_b.Unlock(); mu_a.Unlock(); } void test1a() { mu1.Lock(); mu1.Unlock(); } void test1b() { mu1.Lock(); mu_a.Lock(); mu_b.Lock(); mu_b.Unlock(); mu_a.Unlock(); mu1.Unlock(); } void test() { mu2.Lock(); mu2.Unlock(); } void test3() { mu3.Lock(); mu3.Unlock(); } }; } // end namespace AcquiredBeforeAfterTest namespace ScopedAdoptTest { class Foo { Mutex mu; int a GUARDED_BY(mu); int b; void test1() EXCLUSIVE_UNLOCK_FUNCTION(mu) { MutexLock slock(&mu, true); a = 0; } void test2() SHARED_UNLOCK_FUNCTION(mu) { ReaderMutexLock slock(&mu, true); b = a; } void test3() EXCLUSIVE_LOCKS_REQUIRED(mu) { // expected-note {{mutex acquired here}} MutexLock slock(&mu, true); a = 0; } // expected-warning {{expecting mutex 'mu' to be held at the end of function}} void test4() SHARED_LOCKS_REQUIRED(mu) { // expected-note {{mutex acquired here}} ReaderMutexLock slock(&mu, true); b = a; } // expected-warning {{expecting mutex 'mu' to be held at the end of function}} }; } // end namespace ScopedAdoptTest
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/vararg-class.cpp
// RUN: %clang_cc1 -verify -Wclass-varargs -std=c++98 %s // RUN: %clang_cc1 -verify -Wclass-varargs -std=c++11 %s struct A {}; struct B { ~B(); }; class C { char *c_str(); }; struct D { char *c_str(); }; struct E { E(); }; struct F { F(); char *c_str(); }; void v(...); void w(const char*, ...) __attribute__((format(printf, 1, 2))); void test(A a, B b, C c, D d, E e, F f) { v(a); // expected-warning-re {{passing object of class type 'A' through variadic function{{$}}}} v(b); // expected-error-re {{cannot pass object of non-{{POD|trivial}} type 'B' through variadic function; call will abort at runtime}} v(c); // expected-warning {{passing object of class type 'C' through variadic function; did you mean to call '.c_str()'?}} v(d); // expected-warning {{passing object of class type 'D' through variadic function; did you mean to call '.c_str()'?}} v(e); v(f); #if __cplusplus < 201103L // expected-error@-3 {{cannot pass object of non-POD type 'E' through variadic function; call will abort at runtime}} // expected-error@-3 {{cannot pass object of non-POD type 'F' through variadic function; call will abort at runtime}} #else // expected-warning-re@-6 {{passing object of class type 'E' through variadic function{{$}}}} // expected-warning@-6 {{passing object of class type 'F' through variadic function; did you mean to call '.c_str()'?}} #endif v(d.c_str()); v(f.c_str()); v(0); v('x'); w("%s", a); // expected-warning {{format specifies type 'char *' but the argument has type 'A'}} w("%s", b); // expected-error-re {{cannot pass non-{{POD|trivial}} object of type 'B' to variadic function; expected type from format string was 'char *'}} w("%s", c); // expected-warning {{format specifies type 'char *' but the argument has type 'C'}} w("%s", d); // expected-warning {{format specifies type 'char *' but the argument has type 'D'}} w("%s", e); w("%s", f); #if __cplusplus < 201103L // expected-error@-3 {{cannot pass non-POD object of type 'E' to variadic function; expected type from format string was 'char *'}} // expected-error@-3 {{cannot pass non-POD object of type 'F' to variadic function; expected type from format string was 'char *'}} // expected-note@-4 {{did you mean to call the c_str() method?}} #else // expected-warning@-7 {{format specifies type 'char *' but the argument has type 'E'}} // expected-warning@-7 {{format specifies type 'char *' but the argument has type 'F'}} #endif }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/cxx0x-initializer-aggregates.cpp
// RUN: %clang_cc1 -std=c++0x -fsyntax-only -verify %s struct one { char c[1]; }; struct two { char c[2]; }; namespace aggregate { struct S { int ar[2]; struct T { int i1; int i2; } t; struct U { int i1; } u[2]; struct V { int var[2]; } v; }; void bracing() { S s1 = { 1, 2, 3 ,4, 5, 6, 7, 8 }; S s2{ {1, 2}, {3, 4}, { {5}, {6} }, { {7, 8} } }; S s3{ 1, 2, 3, 4, 5, 6 }; S s4{ {1, 2}, {3, 4}, {5, 6}, { {7, 8} } }; S s5{ {1, 2}, {3, 4}, { {5}, {6} }, {7, 8} }; } void bracing_new() { new S{ {1, 2}, {3, 4}, { {5}, {6} }, { {7, 8} } }; new S{ 1, 2, 3, 4, 5, 6 }; new S{ {1, 2}, {3, 4}, {5, 6}, { {7, 8} } }; new S{ {1, 2}, {3, 4}, { {5}, {6} }, {7, 8} }; } void bracing_construct() { (void) S{ {1, 2}, {3, 4}, { {5}, {6} }, { {7, 8} } }; (void) S{ 1, 2, 3, 4, 5, 6 }; (void) S{ {1, 2}, {3, 4}, {5, 6}, { {7, 8} } }; (void) S{ {1, 2}, {3, 4}, { {5}, {6} }, {7, 8} }; } struct String { String(const char*); }; struct A { int m1; int m2; }; void function_call() { void takes_A(A); takes_A({1, 2}); } struct B { int m1; String m2; }; void overloaded_call() { one overloaded(A); two overloaded(B); static_assert(sizeof(overloaded({1, 2})) == sizeof(one), "bad overload"); static_assert(sizeof(overloaded({1, "two"})) == sizeof(two), "bad overload"); // String is not default-constructible static_assert(sizeof(overloaded({1})) == sizeof(one), "bad overload"); } struct C { int a[2]; C():a({1, 2}) { } }; // expected-error {{parenthesized initialization of a member array is a GNU extension}} } namespace array_explicit_conversion { typedef int test1[2]; typedef int test2[]; template<int x> struct A { int a[x]; }; // expected-error {{'a' declared as an array with a negative size}} typedef A<1> test3[]; typedef A<-1> test4[]; void f() { (void)test1{1}; (void)test2{1}; (void)test3{{{1}}}; (void)test4{{{1}}}; // expected-note {{in instantiation of template class 'array_explicit_conversion::A<-1>' requested here}} } } namespace sub_constructor { struct DefaultConstructor { // expected-note 2 {{not viable}} DefaultConstructor(); // expected-note {{not viable}} int x; }; struct NoDefaultConstructor1 { // expected-note 2 {{not viable}} NoDefaultConstructor1(int); // expected-note {{not viable}} int x; }; struct NoDefaultConstructor2 { // expected-note 4 {{not viable}} NoDefaultConstructor2(int,int); // expected-note 2 {{not viable}} int x; }; struct Aggr { DefaultConstructor a; NoDefaultConstructor1 b; NoDefaultConstructor2 c; }; Aggr ok1 { {}, {0} , {0,0} }; Aggr ok2 = { {}, {0} , {0,0} }; Aggr too_many { {0} , {0} , {0,0} }; // expected-error {{no matching constructor for initialization}} Aggr too_few { {} , {0} , {0} }; // expected-error {{no matching constructor for initialization}} Aggr invalid { {} , {&ok1} , {0,0} }; // expected-error {{no matching constructor for initialization}} NoDefaultConstructor2 array_ok[] = { {0,0} , {0,1} }; NoDefaultConstructor2 array_error[] = { {0,0} , {0} }; // expected-error {{no matching constructor for initialization}} } namespace multidimensional_array { void g(const int (&)[2][2]) {} void g(const int (&)[2][2][2]) = delete; void h() { g({{1,2},{3,4}}); } } namespace array_addressof { using T = int[5]; T *p = &T{1,2,3,4,5}; // expected-error {{taking the address of a temporary object of type 'T' (aka 'int [5]')}} }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/using-decl-pr4450.cpp
// RUN: %clang_cc1 -fsyntax-only -verify %s // expected-no-diagnostics namespace A { void g(); } namespace X { using A::g; } void h() { A::g(); X::g(); }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/microsoft-cxx0x.cpp
// RUN: %clang_cc1 %s -triple i686-pc-win32 -fsyntax-only -Wc++11-narrowing -Wmicrosoft -verify -fms-extensions -std=c++11 // RUN: %clang_cc1 %s -triple i686-pc-win32 -fsyntax-only -Wc++11-narrowing -Wmicrosoft -verify -fms-extensions -std=c++11 -fms-compatibility -DMS_COMPAT struct A { unsigned int a; }; int b = 3; A var = { b }; // expected-warning {{ cannot be narrowed }} expected-note {{insert an explicit cast to silence this issue}} namespace PR13433 { struct S; S make(); template<typename F> auto x(F f) -> decltype(f(make())); #ifndef MS_COMPAT // expected-error@-2{{calling 'make' with incomplete return type 'PR13433::S'}} // expected-note@-5{{'make' declared here}} // expected-note@-7{{forward declaration of 'PR13433::S'}} #endif }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/cast-lvalue-to-rvalue-reference.cpp
// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11 // expected-no-diagnostics struct S {}; int x; S&& y1 = (S&&)x; S&& y2 = reinterpret_cast<S&&>(x); S& z1 = (S&)x;
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/warn-unused-value.cpp
// RUN: %clang_cc1 -fsyntax-only -verify -Wunused-value %s // PR4806 namespace test0 { class Box { public: int i; volatile int j; }; void doit() { // pointer to volatile has side effect (thus no warning) Box* box = new Box; box->i; // expected-warning {{expression result unused}} box->j; // expected-warning {{expression result unused}} } } namespace test1 { struct Foo { int i; bool operator==(const Foo& rhs) { return i == rhs.i; } }; #define NOP(x) (x) void b(Foo f1, Foo f2) { NOP(f1 == f2); // expected-warning {{expression result unused}} } #undef NOP } namespace test2 { extern "C++" { namespace std { template<typename T> struct basic_string { struct X {}; void method() const { X* x; &x[0]; // expected-warning {{expression result unused}} } }; typedef basic_string<char> string; void func(const std::string& str) { str.method(); // expected-note {{in instantiation of member function}} } } } } namespace test3 { struct Used { Used(); Used(int); Used(int, int); }; struct __attribute__((warn_unused)) Unused { Unused(); Unused(int); Unused(int, int); }; void f() { Used(); Used(1); Used(1, 1); Unused(); // expected-warning {{expression result unused}} Unused(1); // expected-warning {{expression result unused}} Unused(1, 1); // expected-warning {{expression result unused}} } } namespace std { struct type_info {}; } namespace test4 { struct Good { Good &f(); }; struct Bad { virtual Bad& f(); }; void f() { int i = 0; (void)typeid(++i); // expected-warning {{expression with side effects has no effect in an unevaluated context}} Good g; (void)typeid(g.f()); // Ok; not a polymorphic use of a glvalue. // This is a polymorphic use of a glvalue, which results in the typeid being // evaluated instead of unevaluated. Bad b; (void)typeid(b.f()); // expected-warning {{expression with side effects will be evaluated despite being used as an operand to 'typeid'}} // A dereference of a volatile pointer is a side effecting operation, however // since it is idiomatic code, and the alternatives induce higher maintenance // costs, it is allowed. int * volatile x; (void)sizeof(*x); // Ok } }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/new-delete-cxx0x.cpp
// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11 -triple=i686-pc-linux-gnu void ugly_news(int *ip) { // These are ill-formed according to one reading of C++98, and at the least // have undefined behavior. // FIXME: They're ill-formed in C++11. (void)new int[-1]; // expected-warning {{array size is negative}} (void)new int[2000000000]; // expected-warning {{array is too large}} } struct S { S(int); S(); ~S(); }; struct T { // expected-note 2 {{not viable}} T(int); // expected-note {{not viable}} }; void fn() { (void) new int[2] {1, 2}; (void) new S[2] {1, 2}; // C++11 [expr.new]p19: // If the new-expression creates an object or an array of objects of class // type, access and ambiguity control are done for the allocation function, // the deallocation function (12.5), and the constructor (12.1). // // Note that this happens even if the array bound is constant and the // initializer initializes every array element. (void) new T[2] {1, 2}; // expected-error {{no matching constructor}} expected-note {{in implicit initialization of array element 2}} }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/type-traits-incomplete.cpp
// RUN: %clang_cc1 -fsyntax-only -verify %s struct S; // expected-note 2 {{forward declaration of 'S'}} void f() { __is_pod(S); // expected-error{{incomplete type 'S' used in type trait expression}} __is_pod(S[]); // expected-error{{incomplete type 'S' used in type trait expression}} }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/PR6618.cpp
// RUN: %clang_cc1 -fsyntax-only -verify %s class bar; // expected-note {{forward declaration of 'bar'}} struct zed { bar g; // expected-error {{field has incomplete type}} }; class baz { zed h; }; void f() { enum { e = sizeof(baz) }; }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/constructor-initializer.cpp
// RUN: %clang_cc1 -Wreorder -fsyntax-only -verify %s class A { int m; public: A() : A::m(17) { } // expected-error {{member initializer 'm' does not name a non-static data member or base class}} A(int); }; class B : public A { public: B() : A(), m(1), n(3.14) { } private: int m; float n; }; class C : public virtual B { public: C() : B() { } }; class D : public C { public: D() : B(), C() { } }; class E : public D, public B { // expected-warning{{direct base 'B' is inaccessible due to ambiguity:\n class E -> class D -> class C -> class B\n class E -> class B}} public: E() : B(), D() { } // expected-error{{base class initializer 'B' names both a direct base class and an inherited virtual base class}} }; typedef int INT; class F : public B { public: int B; F() : B(17), m(17), // expected-error{{member initializer 'm' does not name a non-static data member or base class}} INT(17) // expected-error{{constructor initializer 'INT' (aka 'int') does not name a class}} { } }; class G : A { G() : A(10); // expected-error{{expected '{'}} }; void f() : a(242) { } // expected-error{{only constructors take base initializers}} class H : A { H(); }; H::H() : A(10) { } class X {}; class Y {}; struct S : Y, virtual X { S (); }; struct Z : S { Z() : X(), S(), E() {} // expected-error {{type 'E' is not a direct or virtual base of 'Z'}} }; class U { union { int a; char* p; }; union { int b; double d; }; U() : a(1), // expected-note {{previous initialization is here}} p(0), // expected-error {{initializing multiple members of union}} d(1.0) {} }; struct V {}; struct Base {}; struct Base1 {}; struct Derived : Base, Base1, virtual V { Derived (); }; struct Current : Derived { int Derived; Current() : Derived(1), ::Derived(), // expected-warning {{field 'Derived' will be initialized after base '::Derived'}} \ // expected-warning {{base class '::Derived' will be initialized after base 'Derived::V'}} ::Derived::Base(), // expected-error {{type '::Derived::Base' is not a direct or virtual base of 'Current'}} Derived::Base1(), // expected-error {{type 'Derived::Base1' is not a direct or virtual base of 'Current'}} Derived::V(), ::NonExisting(), // expected-error {{member initializer 'NonExisting' does not name a non-static data member or}} INT::NonExisting() {} // expected-error {{'INT' (aka 'int') is not a class, namespace, or enumeration}} \ // expected-error {{member initializer 'NonExisting' does not name a non-static data member or}} }; struct M { // expected-note 2 {{candidate constructor (the implicit copy constructor)}} \ // expected-note {{declared here}} \ // expected-note {{declared here}} M(int i, int j); // expected-note 2 {{candidate constructor}} }; struct N : M { N() : M(1), // expected-error {{no matching constructor for initialization of 'M'}} m1(100) { } // expected-error {{no matching constructor for initialization of 'M'}} M m1; }; struct P : M { P() { } // expected-error {{constructor for 'P' must explicitly initialize the base class 'M' which does not have a default constructor}} \ // expected-error {{member 'm'}} M m; // expected-note {{member is declared here}} }; struct Q { Q() : f1(1,2), // expected-error {{excess elements in scalar initializer}} pf(0.0) { } // expected-error {{cannot initialize a member subobject of type 'float *' with an rvalue of type 'double'}} float f1; float *pf; }; // A silly class used to demonstrate field-is-uninitialized in constructors with // multiple params. int IntParam(int i) { return 0; }; class TwoInOne { public: TwoInOne(TwoInOne a, TwoInOne b) {} }; class InitializeUsingSelfTest { bool A; char* B; int C; TwoInOne D; int E; InitializeUsingSelfTest(int F) : A(A), // expected-warning {{field 'A' is uninitialized when used here}} B((((B)))), // expected-warning {{field 'B' is uninitialized when used here}} C(A && InitializeUsingSelfTest::C), // expected-warning {{field 'C' is uninitialized when used here}} D(D, // expected-warning {{field 'D' is uninitialized when used here}} D), // expected-warning {{field 'D' is uninitialized when used here}} E(IntParam(E)) {} // expected-warning {{field 'E' is uninitialized when used here}} }; int IntWrapper(int &i) { return 0; }; class InitializeUsingSelfExceptions { int A; int B; int C; void *P; InitializeUsingSelfExceptions(int B) : A(IntWrapper(A)), // Due to a conservative implementation, we do not report warnings inside function/ctor calls even though it is possible to do so. B(B), // Not a warning; B is a local variable. C(sizeof(C)), // sizeof doesn't reference contents, do not warn P(&P) {} // address-of doesn't reference contents (the pointer may be dereferenced in the same expression but it would be rare; and weird) }; class CopyConstructorTest { bool A, B, C; CopyConstructorTest(const CopyConstructorTest& rhs) : A(rhs.A), B(B), // expected-warning {{field 'B' is uninitialized when used here}} C(rhs.C || C) { } // expected-warning {{field 'C' is uninitialized when used here}} }; // Make sure we aren't marking default constructors when we shouldn't be. template<typename T> struct NDC { T &ref; NDC() { } NDC(T &ref) : ref(ref) { } }; struct X0 : NDC<int> { X0(int &ref) : NDC<int>(ref), ndc(ref) { } NDC<int> ndc; }; namespace Test0 { struct A { A(); }; struct B { B() { } const A a; }; } namespace Test1 { struct A { enum Kind { Foo } Kind; A() : Kind(Foo) {} }; } namespace Test2 { struct A { A(const A&); }; struct B : virtual A { }; struct C : A, B { }; // expected-warning{{direct base 'Test2::A' is inaccessible due to ambiguity:\n struct Test2::C -> struct Test2::A\n struct Test2::C -> struct Test2::B -> struct Test2::A}} C f(C c) { return c; } } // Don't build implicit initializers for anonymous union fields when we already // have an explicit initializer for another field in the union. namespace PR7402 { struct S { union { void* ptr_; struct { int i_; }; }; template <typename T> S(T) : ptr_(0) { } }; void f() { S s(3); } } // <rdar://problem/8308215>: don't crash. // Lots of questionable recovery here; errors can change. namespace test3 { class A : public std::exception {}; // expected-error {{undeclared identifier}} expected-error {{expected class name}} expected-note 2 {{candidate}} class B : public A { public: B(const String& s, int e=0) // expected-error {{unknown type name}} : A(e), m_String(s) , m_ErrorStr(__null) {} // expected-error {{no matching constructor}} expected-error {{does not name}} B(const B& e) : A(e), m_String(e.m_String), m_ErrorStr(__null) { // expected-error {{does not name}} \ // expected-error {{no member named 'm_String' in 'test3::B'}} } }; } // PR8075 namespace PR8075 { struct S1 { enum { FOO = 42 }; static const int bar = 42; static int baz(); S1(int); }; const int S1::bar; struct S2 { S1 s1; S2() : s1(s1.FOO) {} }; struct S3 { S1 s1; S3() : s1(s1.bar) {} }; struct S4 { S1 s1; S4() : s1(s1.baz()) {} }; } namespace PR12049 { int function(); class Class { public: Class() : member(function() {} // expected-note {{to match this '('}} int member; // expected-error {{expected ')'}} }; } namespace PR14073 { struct S1 { union { int n; }; S1() : n(n) {} }; // expected-warning {{field 'n' is uninitialized when used here}} struct S2 { union { union { int n; }; char c; }; S2() : n(n) {} }; // expected-warning {{field 'n' is uninitialized when used here}} struct S3 { struct { int n; }; S3() : n(n) {} }; // expected-warning {{field 'n' is uninitialized when used here}} }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/attr-noreturn.cpp
// RUN: %clang_cc1 -fsyntax-only -verify %s // Reachability tests have to come first because they get suppressed // if any errors have occurred. namespace test5 { struct A { __attribute__((noreturn)) void fail(); void nofail(); } a; int &test1() { a.nofail(); } // expected-warning {{control reaches end of non-void function}} int &test2() { a.fail(); } } namespace destructor_tests { __attribute__((noreturn)) void fail(); struct A { ~A() __attribute__((noreturn)) { fail(); } }; struct B { B() {} ~B() __attribute__((noreturn)) { fail(); } }; struct C : A {}; struct D : B {}; struct E : virtual A {}; struct F : A, virtual B {}; struct G : E {}; struct H : virtual D {}; struct I : A {}; struct J : I {}; struct K : virtual A {}; struct L : K {}; struct M : virtual C {}; struct N : M {}; struct O { N n; }; __attribute__((noreturn)) void test_1() { A a; } __attribute__((noreturn)) void test_2() { B b; } __attribute__((noreturn)) void test_3() { C c; } __attribute__((noreturn)) void test_4() { D d; } __attribute__((noreturn)) void test_5() { E e; } __attribute__((noreturn)) void test_6() { F f; } __attribute__((noreturn)) void test_7() { G g; } __attribute__((noreturn)) void test_8() { H h; } __attribute__((noreturn)) void test_9() { I i; } __attribute__((noreturn)) void test_10() { J j; } __attribute__((noreturn)) void test_11() { K k; } __attribute__((noreturn)) void test_12() { L l; } __attribute__((noreturn)) void test_13() { M m; } __attribute__((noreturn)) void test_14() { N n; } __attribute__((noreturn)) void test_15() { O o; } __attribute__((noreturn)) void test_16() { const A& a = A(); } __attribute__((noreturn)) void test_17() { const B& b = B(); } __attribute__((noreturn)) void test_18() { const C& c = C(); } __attribute__((noreturn)) void test_19() { const D& d = D(); } __attribute__((noreturn)) void test_20() { const E& e = E(); } __attribute__((noreturn)) void test_21() { const F& f = F(); } __attribute__((noreturn)) void test_22() { const G& g = G(); } __attribute__((noreturn)) void test_23() { const H& h = H(); } __attribute__((noreturn)) void test_24() { const I& i = I(); } __attribute__((noreturn)) void test_25() { const J& j = J(); } __attribute__((noreturn)) void test_26() { const K& k = K(); } __attribute__((noreturn)) void test_27() { const L& l = L(); } __attribute__((noreturn)) void test_28() { const M& m = M(); } __attribute__((noreturn)) void test_29() { const N& n = N(); } __attribute__((noreturn)) void test_30() { const O& o = O(); } struct AA {}; struct BB { BB() {} ~BB() {} }; struct CC : AA {}; struct DD : BB {}; struct EE : virtual AA {}; struct FF : AA, virtual BB {}; struct GG : EE {}; struct HH : virtual DD {}; struct II : AA {}; struct JJ : II {}; struct KK : virtual AA {}; struct LL : KK {}; struct MM : virtual CC {}; struct NN : MM {}; struct OO { NN n; }; __attribute__((noreturn)) void test_31() { AA a; BB b; CC c; DD d; EE e; FF f; GG g; HH h; II i; JJ j; KK k; LL l; MM m; NN n; OO o; const AA& aa = AA(); const BB& bb = BB(); const CC& cc = CC(); const DD& dd = DD(); const EE& ee = EE(); const FF& ff = FF(); const GG& gg = GG(); const HH& hh = HH(); const II& ii = II(); const JJ& jj = JJ(); const KK& kk = KK(); const LL& ll = LL(); const MM& mm = MM(); const NN& nn = NN(); const OO& oo = OO(); } // expected-warning {{function declared 'noreturn' should not return}} struct P { ~P() __attribute__((noreturn)) { fail(); } void foo() {} }; struct Q : P { }; __attribute__((noreturn)) void test31() { P().foo(); } __attribute__((noreturn)) void test32() { Q().foo(); } struct R { A a[5]; }; __attribute__((noreturn)) void test33() { R r; } // FIXME: Code flow analysis does not preserve information about non-null // pointers, so it can't determine that this function is noreturn. __attribute__((noreturn)) void test34() { A *a = new A; delete a; } // expected-warning {{function declared 'noreturn' should not return}} struct S { virtual ~S(); }; struct T : S { __attribute__((noreturn)) ~T(); }; // FIXME: Code flow analysis does not preserve information about non-null // pointers or derived class pointers, so it can't determine that this // function is noreturn. __attribute__((noreturn)) void test35() { S *s = new T; delete s; } // expected-warning {{function declared 'noreturn' should not return}} } // PR5620 void f0() __attribute__((__noreturn__)); void f1(void (*)()); void f2() { f1(f0); } // Taking the address of a noreturn function void test_f0a() { void (*fp)() = f0; void (*fp1)() __attribute__((noreturn)) = f0; } // Taking the address of an overloaded noreturn function void f0(int) __attribute__((__noreturn__)); void test_f0b() { void (*fp)() = f0; void (*fp1)() __attribute__((noreturn)) = f0; } // No-returned function pointers typedef void (* noreturn_fp)() __attribute__((noreturn)); void f3(noreturn_fp); // expected-note{{candidate function}} void test_f3() { f3(f0); // okay f3(f2); // expected-error{{no matching function for call}} } class xpto { int blah() __attribute__((noreturn)); }; int xpto::blah() { return 3; // expected-warning {{function 'blah' declared 'noreturn' should not return}} } // PR12948 namespace PR12948 { template<int> void foo() __attribute__((__noreturn__)); template<int> void foo() { while (1) continue; } void bar() __attribute__((__noreturn__)); void bar() { foo<0>(); } void baz() __attribute__((__noreturn__)); typedef void voidfn(); voidfn baz; template<typename> void wibble() __attribute__((__noreturn__)); template<typename> voidfn wibble; } // PR15291 // Overload resolution per over.over should allow implicit noreturn adjustment. namespace PR15291 { __attribute__((noreturn)) void foo(int) {} __attribute__((noreturn)) void foo(double) {} template <typename T> __attribute__((noreturn)) void bar(T) {} void baz(int) {} void baz(double) {} template <typename T> void qux(T) {} // expected-note@+5 {{candidate function [with T = void (*)(int) __attribute__((noreturn))] not viable: no overload of 'baz' matching 'void (*)(int) __attribute__((noreturn))' for 1st argument}} // expected-note@+4 {{candidate function [with T = void (*)(int) __attribute__((noreturn))] not viable: no overload of 'qux' matching 'void (*)(int) __attribute__((noreturn))' for 1st argument}} // expected-note@+3 {{candidate function [with T = void (*)(int) __attribute__((noreturn))] not viable: no overload of 'bar' matching 'void (*)(int) __attribute__((noreturn))' for 1st argument}} // expected-note@+2 {{candidate function [with T = void (*)(int)] not viable: no overload of 'bar' matching 'void (*)(int)' for 1st argument}} // expected-note@+1 {{candidate function [with T = void (int)] not viable: no overload of 'bar' matching 'void (*)(int)' for 1st argument}} template <typename T> void accept_T(T) {} // expected-note@+1 {{candidate function not viable: no overload of 'bar' matching 'void (*)(int)' for 1st argument}} void accept_fptr(void (*f)(int)) { f(42); } // expected-note@+2 {{candidate function not viable: no overload of 'baz' matching 'void (*)(int) __attribute__((noreturn))' for 1st argument}} // expected-note@+1 {{candidate function not viable: no overload of 'qux' matching 'void (*)(int) __attribute__((noreturn))' for 1st argument}} void accept_noreturn_fptr(void __attribute__((noreturn)) (*f)(int)) { f(42); } typedef void (*fptr_t)(int); typedef void __attribute__((noreturn)) (*fptr_noreturn_t)(int); // expected-note@+1 {{candidate function not viable: no overload of 'bar' matching 'fptr_t' (aka 'void (*)(int)') for 1st argument}} void accept_fptr_t(fptr_t f) { f(42); } // expected-note@+2 {{candidate function not viable: no overload of 'baz' matching 'fptr_noreturn_t' (aka 'void (*)(int) __attribute__((noreturn))') for 1st argument}} // expected-note@+1 {{candidate function not viable: no overload of 'qux' matching 'fptr_noreturn_t' (aka 'void (*)(int) __attribute__((noreturn))') for 1st argument}} void accept_fptr_noreturn_t(fptr_noreturn_t f) { f(42); } // Stripping noreturn should work if everything else is correct. void strip_noreturn() { accept_fptr(foo); accept_fptr(bar<int>); accept_fptr(bar<double>); // expected-error {{no matching function for call to 'accept_fptr'}} accept_fptr_t(foo); accept_fptr_t(bar<int>); accept_fptr_t(bar<double>); // expected-error {{no matching function for call to 'accept_fptr_t'}} accept_T<void __attribute__((noreturn)) (*)(int)>(foo); accept_T<void __attribute__((noreturn)) (*)(int)>(bar<int>); accept_T<void __attribute__((noreturn)) (*)(int)>(bar<double>); // expected-error {{no matching function for call to 'accept_T'}} accept_T<void (*)(int)>(foo); accept_T<void (*)(int)>(bar<int>); accept_T<void (*)(int)>(bar<double>); // expected-error {{no matching function for call to 'accept_T'}} accept_T<void (int)>(foo); accept_T<void (int)>(bar<int>); accept_T<void (int)>(bar<double>); // expected-error {{no matching function for call to 'accept_T'}} } // Introducing noreturn should not work. void introduce_noreturn() { accept_noreturn_fptr(baz); // expected-error {{no matching function for call to 'accept_noreturn_fptr'}} accept_noreturn_fptr(qux<int>); // expected-error {{no matching function for call to 'accept_noreturn_fptr'}} accept_fptr_noreturn_t(baz); // expected-error {{no matching function for call to 'accept_fptr_noreturn_t'}} accept_fptr_noreturn_t(qux<int>); // expected-error {{no matching function for call to 'accept_fptr_noreturn_t'}} accept_T<void __attribute__((noreturn)) (*)(int)>(baz); // expected-error {{no matching function for call to 'accept_T'}} accept_T<void __attribute__((noreturn)) (*)(int)>(qux<int>); // expected-error {{no matching function for call to 'accept_T'}} } }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/libstdcxx_atomic_ns_hack.cpp
// RUN: %clang_cc1 -fsyntax-only -std=c++11 -verify %s // libstdc++ 4.6.x contains a bug where it defines std::__atomic[0,1,2] as a // non-inline namespace, then selects one of those namespaces and reopens it // as inline, as a strange way of providing something like a using-directive. // Clang has an egregious hack to work around the problem, by allowing a // namespace to be converted from non-inline to inline in this one specific // case. #ifdef BE_THE_HEADER #pragma clang system_header namespace std { namespace __atomic0 { typedef int foobar; } namespace __atomic1 { typedef void foobar; } inline namespace __atomic0 {} } #else #define BE_THE_HEADER #include "libstdcxx_atomic_ns_hack.cpp" std::foobar fb; using T = void; // expected-note {{here}} using T = std::foobar; // expected-error {{different types ('std::foobar' (aka 'int') vs 'void')}} #endif
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/short-enums.cpp
// RUN: %clang_cc1 -fshort-enums -fsyntax-only %s // This shouldn't crash: PR9474 enum E { VALUE_1 }; template <typename T> struct A {}; template <E Enum> struct B : A<B<Enum> > {}; void bar(int x) { switch (x) { case sizeof(B<VALUE_1>): ; } }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/function-overloaded-redecl.cpp
// RUN: %clang_cc1 -fsyntax-only -verify %s typedef const int cInt; void f (int); void f (const int); // redecl void f (int) { } // expected-note {{previous definition is here}} void f (cInt) { } // expected-error {{redefinition of 'f'}}
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/ns_returns_retained_block_return.cpp
// RUN: %clang_cc1 -fblocks -fsyntax-only -verify %s // RUN: %clang_cc1 -fblocks -fobjc-arc -fsyntax-only -verify %s // expected-no-diagnostics // rdar://17259812 typedef void (^BT) (); class S { BT br() __attribute__((ns_returns_retained)) { return ^{}; } BT br1() __attribute__((ns_returns_retained)); }; BT S::br1() { return ^{}; }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/invalid-instantiated-field-decl.cpp
// RUN: %clang_cc1 -fsyntax-only -verify %s template <typename T> class SmallVectorImpl { public: explicit SmallVectorImpl(unsigned N) { } ~SmallVectorImpl() { } }; template <typename T, unsigned N> class SmallVector : public SmallVectorImpl<T> { typedef typename SmallVectorImpl<T>::U U; // expected-error {{no type named 'U' in 'SmallVectorImpl<CallSite>'}} enum { MinUs = (static_cast<unsigned int>(sizeof(T))*N + // expected-error {{invalid application of 'sizeof' to an incomplete type 'CallSite'}} static_cast<unsigned int>(sizeof(U)) - 1) / static_cast<unsigned int>(sizeof(U)), NumInlineEltsElts = MinUs }; U InlineElts[NumInlineEltsElts]; public: SmallVector() : SmallVectorImpl<T>(NumInlineEltsElts) { } }; class CallSite; // expected-note {{forward declaration of 'CallSite'}} class InlineFunctionInfo { public: explicit InlineFunctionInfo() {} SmallVector<CallSite, 2> DevirtualizedCalls; // expected-note {{in instantiation of template class 'SmallVector<CallSite, 2>' requested}} };
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/decltype-crash.cpp
// RUN: %clang_cc1 -fsyntax-only -verify -Wc++11-compat %s int& a(); void f() { decltype(a()) c; // expected-warning {{'decltype' is a keyword in C++11}} expected-error {{use of undeclared identifier 'decltype'}} }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/warn-logical-not-compare.cpp
// RUN: %clang_cc1 -fsyntax-only -Wlogical-not-parentheses -verify %s // RUN: %clang_cc1 -fsyntax-only -Wlogical-not-parentheses -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s bool getBool(); int getInt(); bool test1(int i1, int i2, bool b1, bool b2) { bool ret; ret = !i1 == i2; // expected-warning@-1 {{logical not is only applied to the left hand side of this comparison}} // expected-note@-2 {{add parentheses after the '!' to evaluate the comparison first}} // expected-note@-3 {{add parentheses around left hand side expression to silence this warning}} // CHECK: to evaluate the comparison first // CHECK: fix-it:"{{.*}}":{10:10-10:10}:"(" // CHECK: fix-it:"{{.*}}":{10:18-10:18}:")" // CHECK: to silence this warning // CHECK: fix-it:"{{.*}}":{10:9-10:9}:"(" // CHECK: fix-it:"{{.*}}":{10:12-10:12}:")" ret = !i1 != i2; //expected-warning@-1 {{logical not is only applied to the left hand side of this comparison}} // expected-note@-2 {{add parentheses after the '!' to evaluate the comparison first}} // expected-note@-3 {{add parentheses around left hand side expression to silence this warning}} // CHECK: to evaluate the comparison first // CHECK: fix-it:"{{.*}}":{21:10-21:10}:"(" // CHECK: fix-it:"{{.*}}":{21:18-21:18}:")" // CHECK: to silence this warning // CHECK: fix-it:"{{.*}}":{21:9-21:9}:"(" // CHECK: fix-it:"{{.*}}":{21:12-21:12}:")" ret = !i1 < i2; //expected-warning@-1 {{logical not is only applied to the left hand side of this comparison}} // expected-note@-2 {{add parentheses after the '!' to evaluate the comparison first}} // expected-note@-3 {{add parentheses around left hand side expression to silence this warning}} // CHECK: to evaluate the comparison first // CHECK: fix-it:"{{.*}}":{32:10-32:10}:"(" // CHECK: fix-it:"{{.*}}":{32:17-32:17}:")" // CHECK: to silence this warning // CHECK: fix-it:"{{.*}}":{32:9-32:9}:"(" // CHECK: fix-it:"{{.*}}":{32:12-32:12}:")" ret = !i1 > i2; //expected-warning@-1 {{logical not is only applied to the left hand side of this comparison}} // expected-note@-2 {{add parentheses after the '!' to evaluate the comparison first}} // expected-note@-3 {{add parentheses around left hand side expression to silence this warning}} // CHECK: to evaluate the comparison first // CHECK: fix-it:"{{.*}}":{43:10-43:10}:"(" // CHECK: fix-it:"{{.*}}":{43:17-43:17}:")" // CHECK: to silence this warning // CHECK: fix-it:"{{.*}}":{43:9-43:9}:"(" // CHECK: fix-it:"{{.*}}":{43:12-43:12}:")" ret = !i1 <= i2; //expected-warning@-1 {{logical not is only applied to the left hand side of this comparison}} // expected-note@-2 {{add parentheses after the '!' to evaluate the comparison first}} // expected-note@-3 {{add parentheses around left hand side expression to silence this warning}} // CHECK: to evaluate the comparison first // CHECK: fix-it:"{{.*}}":{54:10-54:10}:"(" // CHECK: fix-it:"{{.*}}":{54:18-54:18}:")" // CHECK: to silence this warning // CHECK: fix-it:"{{.*}}":{54:9-54:9}:"(" // CHECK: fix-it:"{{.*}}":{54:12-54:12}:")" ret = !i1 >= i2; //expected-warning@-1 {{logical not is only applied to the left hand side of this comparison}} // expected-note@-2 {{add parentheses after the '!' to evaluate the comparison first}} // expected-note@-3 {{add parentheses around left hand side expression to silence this warning}} // CHECK: to evaluate the comparison first // CHECK: fix-it:"{{.*}}":{65:10-65:10}:"(" // CHECK: fix-it:"{{.*}}":{65:18-65:18}:")" // CHECK: to silence this warning // CHECK: fix-it:"{{.*}}":{65:9-65:9}:"(" // CHECK: fix-it:"{{.*}}":{65:12-65:12}:")" ret = i1 == i2; ret = i1 != i2; ret = i1 < i2; ret = i1 > i2; ret = i1 <= i2; ret = i1 >= i2; // Warning silenced by parens. ret = (!i1) == i2; ret = (!i1) != i2; ret = (!i1) < i2; ret = (!i1) > i2; ret = (!i1) <= i2; ret = (!i1) >= i2; ret = !b1 == b2; ret = !b1 != b2; ret = !b1 < b2; ret = !b1 > b2; ret = !b1 <= b2; ret = !b1 >= b2; ret = !getInt() == i1; // expected-warning@-1 {{logical not is only applied to the left hand side of this comparison}} // expected-note@-2 {{add parentheses after the '!' to evaluate the comparison first}} // expected-note@-3 {{add parentheses around left hand side expression to silence this warning}} // CHECK: to evaluate the comparison first // CHECK: fix-it:"{{.*}}":{98:10-98:10}:"(" // CHECK: fix-it:"{{.*}}":{98:24-98:24}:")" // CHECK: to silence this warning // CHECK: fix-it:"{{.*}}":{98:9-98:9}:"(" // CHECK: fix-it:"{{.*}}":{98:18-98:18}:")" ret = (!getInt()) == i1; ret = !getBool() == b1; return ret; } enum E {e1, e2}; E getE(); bool test2 (E e) { bool ret; ret = e == e1; ret = e == getE(); ret = getE() == e1; ret = getE() == getE(); ret = !e == e1; // expected-warning@-1 {{logical not is only applied to the left hand side of this comparison}} // expected-note@-2 {{add parentheses after the '!' to evaluate the comparison first}} // expected-note@-3 {{add parentheses around left hand side expression to silence this warning}} // CHECK: to evaluate the comparison first // CHECK: fix-it:"{{.*}}":{124:10-124:10}:"(" // CHECK: fix-it:"{{.*}}":{124:17-124:17}:")" // CHECK: to silence this warning // CHECK: fix-it:"{{.*}}":{124:9-124:9}:"(" // CHECK: fix-it:"{{.*}}":{124:11-124:11}:")" ret = !e == getE(); // expected-warning@-1 {{logical not is only applied to the left hand side of this comparison}} // expected-note@-2 {{add parentheses after the '!' to evaluate the comparison first}} // expected-note@-3 {{add parentheses around left hand side expression to silence this warning}} // CHECK: to evaluate the comparison first // CHECK: fix-it:"{{.*}}":{135:10-135:10}:"(" // CHECK: fix-it:"{{.*}}":{135:21-135:21}:")" // CHECK: to silence this warning // CHECK: fix-it:"{{.*}}":{135:9-135:9}:"(" // CHECK: fix-it:"{{.*}}":{135:11-135:11}:")" ret = !getE() == e1; // expected-warning@-1 {{logical not is only applied to the left hand side of this comparison}} // expected-note@-2 {{add parentheses after the '!' to evaluate the comparison first}} // expected-note@-3 {{add parentheses around left hand side expression to silence this warning}} // CHECK: to evaluate the comparison first // CHECK: fix-it:"{{.*}}":{146:10-146:10}:"(" // CHECK: fix-it:"{{.*}}":{146:22-146:22}:")" // CHECK: to silence this warning // CHECK: fix-it:"{{.*}}":{146:9-146:9}:"(" // CHECK: fix-it:"{{.*}}":{146:16-146:16}:")" ret = !getE() == getE(); // expected-warning@-1 {{logical not is only applied to the left hand side of this comparison}} // expected-note@-2 {{add parentheses after the '!' to evaluate the comparison first}} // expected-note@-3 {{add parentheses around left hand side expression to silence this warning}} // CHECK: to evaluate the comparison first // CHECK: fix-it:"{{.*}}":{157:10-157:10}:"(" // CHECK: fix-it:"{{.*}}":{157:26-157:26}:")" // CHECK: to silence this warning // CHECK: fix-it:"{{.*}}":{157:9-157:9}:"(" // CHECK: fix-it:"{{.*}}":{157:16-157:16}:")" ret = !(e == e1); ret = !(e == getE()); ret = !(getE() == e1); ret = !(getE() == getE()); ret = (!e) == e1; ret = (!e) == getE(); ret = (!getE()) == e1; ret = (!getE()) == getE(); return ret; } bool PR16673(int x) { bool ret; // Make sure we don't emit a fixit for the left paren, but not the right paren. #define X(x) x ret = X(!x == 1 && 1); // expected-warning@-1 {{logical not is only applied to the left hand side of this comparison}} // expected-note@-2 {{add parentheses after the '!' to evaluate the comparison first}} // expected-note@-3 {{add parentheses around left hand side expression to silence this warning}} // CHECK: to evaluate the comparison first // CHECK-NOT: fix-it // CHECK: to silence this warning // CHECK-NOT: fix-it return ret; }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/builtin-ptrtomember-overload.cpp
// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11 // expected-no-diagnostics struct A {}; struct B { operator A*(); }; struct C : B { }; void foo(C c, B b, int A::* pmf) { int j = c->*pmf; int i = b->*pmf; } struct D { operator const D *(); }; struct DPtr { operator volatile int D::*(); }; int test(D d, DPtr dptr) { return d->*dptr; }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/warn-assignment-condition.cpp
// RUN: %clang_cc1 -fsyntax-only -Wparentheses -verify %s struct A { int foo(); friend A operator+(const A&, const A&); A operator|=(const A&); operator bool(); }; void test() { int x, *p; A a, b; // With scalars. if (x = 7) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ // expected-note{{use '==' to turn this assignment into an equality comparison}} \ // expected-note{{place parentheses around the assignment to silence this warning}} if ((x = 7)) {} do { } while (x = 7); // expected-warning {{using the result of an assignment as a condition without parentheses}} \ // expected-note{{use '==' to turn this assignment into an equality comparison}} \ // expected-note{{place parentheses around the assignment to silence this warning}} do { } while ((x = 7)); while (x = 7) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ // expected-note{{use '==' to turn this assignment into an equality comparison}} \ // expected-note{{place parentheses around the assignment to silence this warning}} while ((x = 7)) {} for (; x = 7; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ // expected-note{{use '==' to turn this assignment into an equality comparison}} \ // expected-note{{place parentheses around the assignment to silence this warning}} for (; (x = 7); ) {} if (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ // expected-note{{use '==' to turn this assignment into an equality comparison}} \ // expected-note{{place parentheses around the assignment to silence this warning}} if ((p = p)) {} do { } while (p = p); // expected-warning {{using the result of an assignment as a condition without parentheses}} \ // expected-note{{use '==' to turn this assignment into an equality comparison}} \ // expected-note{{place parentheses around the assignment to silence this warning}} do { } while ((p = p)); while (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ // expected-note{{use '==' to turn this assignment into an equality comparison}} \ // expected-note{{place parentheses around the assignment to silence this warning}} while ((p = p)) {} for (; p = p; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ // expected-note{{use '==' to turn this assignment into an equality comparison}} \ // expected-note{{place parentheses around the assignment to silence this warning}} for (; (p = p); ) {} // Initializing variables (shouldn't warn). if (int y = x) {} while (int y = x) {} if (A y = a) {} while (A y = a) {} // With temporaries. if (x = (b+b).foo()) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ // expected-note{{use '==' to turn this assignment into an equality comparison}} \ // expected-note{{place parentheses around the assignment to silence this warning}} if ((x = (b+b).foo())) {} do { } while (x = (b+b).foo()); // expected-warning {{using the result of an assignment as a condition without parentheses}} \ // expected-note{{use '==' to turn this assignment into an equality comparison}} \ // expected-note{{place parentheses around the assignment to silence this warning}} do { } while ((x = (b+b).foo())); while (x = (b+b).foo()) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ // expected-note{{use '==' to turn this assignment into an equality comparison}} \ // expected-note{{place parentheses around the assignment to silence this warning}} while ((x = (b+b).foo())) {} for (; x = (b+b).foo(); ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ // expected-note{{use '==' to turn this assignment into an equality comparison}} \ // expected-note{{place parentheses around the assignment to silence this warning}} for (; (x = (b+b).foo()); ) {} // With a user-defined operator. if (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ // expected-note{{use '==' to turn this assignment into an equality comparison}} \ // expected-note{{place parentheses around the assignment to silence this warning}} if ((a = b + b)) {} do { } while (a = b + b); // expected-warning {{using the result of an assignment as a condition without parentheses}} \ // expected-note{{use '==' to turn this assignment into an equality comparison}} \ // expected-note{{place parentheses around the assignment to silence this warning}} do { } while ((a = b + b)); while (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ // expected-note{{use '==' to turn this assignment into an equality comparison}} \ // expected-note{{place parentheses around the assignment to silence this warning}} while ((a = b + b)) {} for (; a = b + b; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ // expected-note{{use '==' to turn this assignment into an equality comparison}} \ // expected-note{{place parentheses around the assignment to silence this warning}} for (; (a = b + b); ) {} // Compound assignments. if (x |= 2) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ // expected-note{{use '!=' to turn this compound assignment into an inequality comparison}} \ // expected-note{{place parentheses around the assignment to silence this warning}} if (a |= b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \ // expected-note{{use '!=' to turn this compound assignment into an inequality comparison}} \ // expected-note{{place parentheses around the assignment to silence this warning}} if ((x == 5)) {} // expected-warning {{equality comparison with extraneous parentheses}} \ // expected-note {{use '=' to turn this equality comparison into an assignment}} \ // expected-note {{remove extraneous parentheses around the comparison to silence this warning}} #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wparentheses-equality" if ((x == 5)) {} // no-warning #pragma clang diagnostic pop if ((5 == x)) {} #define EQ(x,y) ((x) == (y)) if (EQ(x, 5)) {} #undef EQ } void (*fn)(); void test2() { if ((fn == test2)) {} // expected-warning {{equality comparison with extraneous parentheses}} \ // expected-note {{use '=' to turn this equality comparison into an assignment}} \ // expected-note {{remove extraneous parentheses around the comparison to silence this warning}} if ((test2 == fn)) {} } namespace rdar9027658 { template <typename T> void f(T t) { if ((t.g == 3)) { } // expected-warning {{equality comparison with extraneous parentheses}} \ // expected-note {{use '=' to turn this equality comparison into an assignment}} \ // expected-note {{remove extraneous parentheses around the comparison to silence this warning}} } struct S { int g; }; void test() { f(S()); // expected-note {{in instantiation}} } }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/warn-thread-safety-parsing.cpp
// RUN: %clang_cc1 -fsyntax-only -verify -Wthread-safety %s #define LOCKABLE __attribute__ ((lockable)) #define SCOPED_LOCKABLE __attribute__ ((scoped_lockable)) #define GUARDED_BY(x) __attribute__ ((guarded_by(x))) #define GUARDED_VAR __attribute__ ((guarded_var)) #define PT_GUARDED_BY(x) __attribute__ ((pt_guarded_by(x))) #define PT_GUARDED_VAR __attribute__ ((pt_guarded_var)) #define ACQUIRED_AFTER(...) __attribute__ ((acquired_after(__VA_ARGS__))) #define ACQUIRED_BEFORE(...) __attribute__ ((acquired_before(__VA_ARGS__))) #define EXCLUSIVE_LOCK_FUNCTION(...) __attribute__ ((exclusive_lock_function(__VA_ARGS__))) #define SHARED_LOCK_FUNCTION(...) __attribute__ ((shared_lock_function(__VA_ARGS__))) #define ASSERT_EXCLUSIVE_LOCK(...) __attribute__ ((assert_exclusive_lock(__VA_ARGS__))) #define ASSERT_SHARED_LOCK(...) __attribute__ ((assert_shared_lock(__VA_ARGS__))) #define EXCLUSIVE_TRYLOCK_FUNCTION(...) __attribute__ ((exclusive_trylock_function(__VA_ARGS__))) #define SHARED_TRYLOCK_FUNCTION(...) __attribute__ ((shared_trylock_function(__VA_ARGS__))) #define UNLOCK_FUNCTION(...) __attribute__ ((unlock_function(__VA_ARGS__))) #define LOCK_RETURNED(x) __attribute__ ((lock_returned(x))) #define LOCKS_EXCLUDED(...) __attribute__ ((locks_excluded(__VA_ARGS__))) #define EXCLUSIVE_LOCKS_REQUIRED(...) \ __attribute__ ((exclusive_locks_required(__VA_ARGS__))) #define SHARED_LOCKS_REQUIRED(...) \ __attribute__ ((shared_locks_required(__VA_ARGS__))) #define NO_THREAD_SAFETY_ANALYSIS __attribute__ ((no_thread_safety_analysis)) class LOCKABLE Mutex { public: void Lock() EXCLUSIVE_LOCK_FUNCTION(); void ReaderLock() SHARED_LOCK_FUNCTION(); void Unlock() UNLOCK_FUNCTION(); bool TryLock() EXCLUSIVE_TRYLOCK_FUNCTION(true); bool ReaderTryLock() SHARED_TRYLOCK_FUNCTION(true); void AssertHeld() ASSERT_EXCLUSIVE_LOCK(); void AssertReaderHeld() ASSERT_SHARED_LOCK(); }; class UnlockableMu{ }; class MuWrapper { public: Mutex mu; Mutex getMu() { return mu; } Mutex * getMuPointer() { return &mu; } }; class MuDoubleWrapper { public: MuWrapper* muWrapper; MuWrapper* getWrapper() { return muWrapper; } }; Mutex mu1; UnlockableMu umu; Mutex mu2; MuWrapper muWrapper; MuDoubleWrapper muDoubleWrapper; Mutex* muPointer; Mutex** muDoublePointer = & muPointer; Mutex& muRef = mu1; //---------------------------------------// // Scoping tests //--------------------------------------// class Foo { Mutex foomu; void needLock() EXCLUSIVE_LOCK_FUNCTION(foomu); }; class Foo2 { void needLock() EXCLUSIVE_LOCK_FUNCTION(foomu); Mutex foomu; }; class Bar { Mutex barmu; Mutex barmu2 ACQUIRED_AFTER(barmu); }; //-----------------------------------------// // No Thread Safety Analysis (noanal) // //-----------------------------------------// // FIXME: Right now we cannot parse attributes put on function definitions // We would like to patch this at some point. #if !__has_attribute(no_thread_safety_analysis) #error "Should support no_thread_safety_analysis attribute" #endif void noanal_fun() NO_THREAD_SAFETY_ANALYSIS; void noanal_fun_args() __attribute__((no_thread_safety_analysis(1))); // \ // expected-error {{'no_thread_safety_analysis' attribute takes no arguments}} int noanal_testfn(int y) NO_THREAD_SAFETY_ANALYSIS; int noanal_testfn(int y) { int x NO_THREAD_SAFETY_ANALYSIS = y; // \ // expected-warning {{'no_thread_safety_analysis' attribute only applies to functions}} return x; }; int noanal_test_var NO_THREAD_SAFETY_ANALYSIS; // \ // expected-warning {{'no_thread_safety_analysis' attribute only applies to functions}} class NoanalFoo { private: int test_field NO_THREAD_SAFETY_ANALYSIS; // \ // expected-warning {{'no_thread_safety_analysis' attribute only applies to functions}} void test_method() NO_THREAD_SAFETY_ANALYSIS; }; class NO_THREAD_SAFETY_ANALYSIS NoanalTestClass { // \ // expected-warning {{'no_thread_safety_analysis' attribute only applies to functions}} }; void noanal_fun_params(int lvar NO_THREAD_SAFETY_ANALYSIS); // \ // expected-warning {{'no_thread_safety_analysis' attribute only applies to functions}} //-----------------------------------------// // Guarded Var Attribute (gv) //-----------------------------------------// #if !__has_attribute(guarded_var) #error "Should support guarded_var attribute" #endif int gv_var_noargs GUARDED_VAR; int gv_var_args __attribute__((guarded_var(1))); // \ // expected-error {{'guarded_var' attribute takes no arguments}} class GVFoo { private: int gv_field_noargs GUARDED_VAR; int gv_field_args __attribute__((guarded_var(1))); // \ // expected-error {{'guarded_var' attribute takes no arguments}} }; class GUARDED_VAR GV { // \ // expected-warning {{'guarded_var' attribute only applies to fields and global variables}} }; void gv_function() GUARDED_VAR; // \ // expected-warning {{'guarded_var' attribute only applies to fields and global variables}} void gv_function_params(int gv_lvar GUARDED_VAR); // \ // expected-warning {{'guarded_var' attribute only applies to fields and global variables}} int gv_testfn(int y){ int x GUARDED_VAR = y; // \ // expected-warning {{'guarded_var' attribute only applies to fields and global variables}} return x; } //-----------------------------------------// // Pt Guarded Var Attribute (pgv) //-----------------------------------------// //FIXME: add support for boost::scoped_ptr<int> fancyptr and references #if !__has_attribute(pt_guarded_var) #error "Should support pt_guarded_var attribute" #endif int *pgv_pt_var_noargs PT_GUARDED_VAR; int pgv_var_noargs PT_GUARDED_VAR; // \ // expected-warning {{'pt_guarded_var' only applies to pointer types; type here is 'int'}} class PGVFoo { private: int *pt_field_noargs PT_GUARDED_VAR; int field_noargs PT_GUARDED_VAR; // \ // expected-warning {{'pt_guarded_var' only applies to pointer types; type here is 'int'}} int *gv_field_args __attribute__((pt_guarded_var(1))); // \ // expected-error {{'pt_guarded_var' attribute takes no arguments}} }; class PT_GUARDED_VAR PGV { // \ // expected-warning {{'pt_guarded_var' attribute only applies to fields and global variables}} }; int *pgv_var_args __attribute__((pt_guarded_var(1))); // \ // expected-error {{'pt_guarded_var' attribute takes no arguments}} void pgv_function() PT_GUARDED_VAR; // \ // expected-warning {{'pt_guarded_var' attribute only applies to fields and global variables}} void pgv_function_params(int *gv_lvar PT_GUARDED_VAR); // \ // expected-warning {{'pt_guarded_var' attribute only applies to fields and global variables}} void pgv_testfn(int y){ int *x PT_GUARDED_VAR = new int(0); // \ // expected-warning {{'pt_guarded_var' attribute only applies to fields and global variables}} delete x; } //-----------------------------------------// // Lockable Attribute (l) //-----------------------------------------// //FIXME: In future we may want to add support for structs, ObjC classes, etc. #if !__has_attribute(lockable) #error "Should support lockable attribute" #endif class LOCKABLE LTestClass { }; class __attribute__((lockable (1))) LTestClass_args { // \ // expected-error {{'lockable' attribute takes no arguments}} }; void l_test_function() LOCKABLE; // \ // expected-warning {{'lockable' attribute only applies to struct, union or class}} int l_testfn(int y) { int x LOCKABLE = y; // \ // expected-warning {{'lockable' attribute only applies to struct, union or class}} return x; } int l_test_var LOCKABLE; // \ // expected-warning {{'lockable' attribute only applies to struct, union or class}} class LFoo { private: int test_field LOCKABLE; // \ // expected-warning {{'lockable' attribute only applies to struct, union or class}} void test_method() LOCKABLE; // \ // expected-warning {{'lockable' attribute only applies to struct, union or class}} }; void l_function_params(int lvar LOCKABLE); // \ // expected-warning {{'lockable' attribute only applies to struct, union or class}} //-----------------------------------------// // Scoped Lockable Attribute (sl) //-----------------------------------------// #if !__has_attribute(scoped_lockable) #error "Should support scoped_lockable attribute" #endif class SCOPED_LOCKABLE SLTestClass { }; class __attribute__((scoped_lockable (1))) SLTestClass_args { // \ // expected-error {{'scoped_lockable' attribute takes no arguments}} }; void sl_test_function() SCOPED_LOCKABLE; // \ // expected-warning {{'scoped_lockable' attribute only applies to struct, union or class}} int sl_testfn(int y) { int x SCOPED_LOCKABLE = y; // \ // expected-warning {{'scoped_lockable' attribute only applies to struct, union or class}} return x; } int sl_test_var SCOPED_LOCKABLE; // \ // expected-warning {{'scoped_lockable' attribute only applies to struct, union or class}} class SLFoo { private: int test_field SCOPED_LOCKABLE; // \ // expected-warning {{'scoped_lockable' attribute only applies to struct, union or class}} void test_method() SCOPED_LOCKABLE; // \ // expected-warning {{'scoped_lockable' attribute only applies to struct, union or class}} }; void sl_function_params(int lvar SCOPED_LOCKABLE); // \ // expected-warning {{'scoped_lockable' attribute only applies to struct, union or class}} //-----------------------------------------// // Guarded By Attribute (gb) //-----------------------------------------// // FIXME: Eventually, would we like this attribute to take more than 1 arg? #if !__has_attribute(guarded_by) #error "Should support guarded_by attribute" #endif //1. Check applied to the right types & argument number int gb_var_arg GUARDED_BY(mu1); int gb_non_ascii GUARDED_BY(L"wide"); // expected-warning {{ignoring 'guarded_by' attribute because its argument is invalid}} int gb_var_args __attribute__((guarded_by(mu1, mu2))); // \ // expected-error {{'guarded_by' attribute takes one argument}} int gb_var_noargs __attribute__((guarded_by)); // \ // expected-error {{'guarded_by' attribute takes one argument}} class GBFoo { private: int gb_field_noargs __attribute__((guarded_by)); // \ // expected-error {{'guarded_by' attribute takes one argument}} int gb_field_args GUARDED_BY(mu1); }; class GUARDED_BY(mu1) GB { // \ // expected-warning {{'guarded_by' attribute only applies to fields and global variables}} }; void gb_function() GUARDED_BY(mu1); // \ // expected-warning {{'guarded_by' attribute only applies to fields and global variables}} void gb_function_params(int gv_lvar GUARDED_BY(mu1)); // \ // expected-warning {{'guarded_by' attribute only applies to fields and global variables}} int gb_testfn(int y){ int x GUARDED_BY(mu1) = y; // \ // expected-warning {{'guarded_by' attribute only applies to fields and global variables}} return x; } //2. Check argument parsing. // legal attribute arguments int gb_var_arg_1 GUARDED_BY(muWrapper.mu); int gb_var_arg_2 GUARDED_BY(muDoubleWrapper.muWrapper->mu); int gb_var_arg_3 GUARDED_BY(muWrapper.getMu()); int gb_var_arg_4 GUARDED_BY(*muWrapper.getMuPointer()); int gb_var_arg_5 GUARDED_BY(&mu1); int gb_var_arg_6 GUARDED_BY(muRef); int gb_var_arg_7 GUARDED_BY(muDoubleWrapper.getWrapper()->getMu()); int gb_var_arg_8 GUARDED_BY(muPointer); // illegal attribute arguments int gb_var_arg_bad_1 GUARDED_BY(1); // \ // expected-warning {{'guarded_by' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'int'}} int gb_var_arg_bad_2 GUARDED_BY("mu"); // \ // expected-warning {{ignoring 'guarded_by' attribute because its argument is invalid}} int gb_var_arg_bad_3 GUARDED_BY(muDoublePointer); // \ // expected-warning {{'guarded_by' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'Mutex **'}} int gb_var_arg_bad_4 GUARDED_BY(umu); // \ // expected-warning {{'guarded_by' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'UnlockableMu'}} //3. // Thread Safety analysis tests //-----------------------------------------// // Pt Guarded By Attribute (pgb) //-----------------------------------------// #if !__has_attribute(pt_guarded_by) #error "Should support pt_guarded_by attribute" #endif //1. Check applied to the right types & argument number int *pgb_var_noargs __attribute__((pt_guarded_by)); // \ // expected-error {{'pt_guarded_by' attribute takes one argument}} int *pgb_ptr_var_arg PT_GUARDED_BY(mu1); int *pgb_ptr_var_args __attribute__((pt_guarded_by(mu1, mu2))); // \ // expected-error {{'pt_guarded_by' attribute takes one argument}} int pgb_var_args PT_GUARDED_BY(mu1); // \ // expected-warning {{'pt_guarded_by' only applies to pointer types; type here is 'int'}} class PGBFoo { private: int *pgb_field_noargs __attribute__((pt_guarded_by)); // \ // expected-error {{'pt_guarded_by' attribute takes one argument}} int *pgb_field_args PT_GUARDED_BY(mu1); }; class PT_GUARDED_BY(mu1) PGB { // \ // expected-warning {{'pt_guarded_by' attribute only applies to fields and global variables}} }; void pgb_function() PT_GUARDED_BY(mu1); // \ // expected-warning {{'pt_guarded_by' attribute only applies to fields and global variables}} void pgb_function_params(int gv_lvar PT_GUARDED_BY(mu1)); // \ // expected-warning {{'pt_guarded_by' attribute only applies to fields and global variables}} void pgb_testfn(int y){ int *x PT_GUARDED_BY(mu1) = new int(0); // \ // expected-warning {{'pt_guarded_by' attribute only applies to fields and global variables}} delete x; } //2. Check argument parsing. // legal attribute arguments int * pgb_var_arg_1 PT_GUARDED_BY(muWrapper.mu); int * pgb_var_arg_2 PT_GUARDED_BY(muDoubleWrapper.muWrapper->mu); int * pgb_var_arg_3 PT_GUARDED_BY(muWrapper.getMu()); int * pgb_var_arg_4 PT_GUARDED_BY(*muWrapper.getMuPointer()); int * pgb_var_arg_5 PT_GUARDED_BY(&mu1); int * pgb_var_arg_6 PT_GUARDED_BY(muRef); int * pgb_var_arg_7 PT_GUARDED_BY(muDoubleWrapper.getWrapper()->getMu()); int * pgb_var_arg_8 PT_GUARDED_BY(muPointer); // illegal attribute arguments int * pgb_var_arg_bad_1 PT_GUARDED_BY(1); // \ // expected-warning {{'pt_guarded_by' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'int'}} int * pgb_var_arg_bad_2 PT_GUARDED_BY("mu"); // \ // expected-warning {{ignoring 'pt_guarded_by' attribute because its argument is invalid}} int * pgb_var_arg_bad_3 PT_GUARDED_BY(muDoublePointer); // \ // expected-warning {{'pt_guarded_by' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'Mutex **'}} int * pgb_var_arg_bad_4 PT_GUARDED_BY(umu); // \ // expected-warning {{'pt_guarded_by' attribute requires arguments whose type is annotated with 'capability' attribute}} //-----------------------------------------// // Acquired After (aa) //-----------------------------------------// // FIXME: Would we like this attribute to take more than 1 arg? #if !__has_attribute(acquired_after) #error "Should support acquired_after attribute" #endif Mutex mu_aa ACQUIRED_AFTER(mu1); Mutex aa_var_noargs __attribute__((acquired_after)); // \ // expected-error {{'acquired_after' attribute takes at least 1 argument}} class AAFoo { private: Mutex aa_field_noargs __attribute__((acquired_after)); // \ // expected-error {{'acquired_after' attribute takes at least 1 argument}} Mutex aa_field_args ACQUIRED_AFTER(mu1); }; class ACQUIRED_AFTER(mu1) AA { // \ // expected-warning {{'acquired_after' attribute only applies to fields and global variables}} }; void aa_function() ACQUIRED_AFTER(mu1); // \ // expected-warning {{'acquired_after' attribute only applies to fields and global variables}} void aa_function_params(int gv_lvar ACQUIRED_AFTER(mu1)); // \ // expected-warning {{'acquired_after' attribute only applies to fields and global variables}} void aa_testfn(int y){ Mutex x ACQUIRED_AFTER(mu1) = Mutex(); // \ // expected-warning {{'acquired_after' attribute only applies to fields and global variables}} } //Check argument parsing. // legal attribute arguments Mutex aa_var_arg_1 ACQUIRED_AFTER(muWrapper.mu); Mutex aa_var_arg_2 ACQUIRED_AFTER(muDoubleWrapper.muWrapper->mu); Mutex aa_var_arg_3 ACQUIRED_AFTER(muWrapper.getMu()); Mutex aa_var_arg_4 ACQUIRED_AFTER(*muWrapper.getMuPointer()); Mutex aa_var_arg_5 ACQUIRED_AFTER(&mu1); Mutex aa_var_arg_6 ACQUIRED_AFTER(muRef); Mutex aa_var_arg_7 ACQUIRED_AFTER(muDoubleWrapper.getWrapper()->getMu()); Mutex aa_var_arg_8 ACQUIRED_AFTER(muPointer); // illegal attribute arguments Mutex aa_var_arg_bad_1 ACQUIRED_AFTER(1); // \ // expected-warning {{'acquired_after' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'int'}} Mutex aa_var_arg_bad_2 ACQUIRED_AFTER("mu"); // \ // expected-warning {{ignoring 'acquired_after' attribute because its argument is invalid}} Mutex aa_var_arg_bad_3 ACQUIRED_AFTER(muDoublePointer); // \ // expected-warning {{'acquired_after' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'Mutex **'}} Mutex aa_var_arg_bad_4 ACQUIRED_AFTER(umu); // \ // expected-warning {{'acquired_after' attribute requires arguments whose type is annotated with 'capability' attribute}} UnlockableMu aa_var_arg_bad_5 ACQUIRED_AFTER(mu_aa); // \ // expected-warning {{'acquired_after' attribute can only be applied in a context annotated with 'capability("mutex")' attribute}} //-----------------------------------------// // Acquired Before (ab) //-----------------------------------------// #if !__has_attribute(acquired_before) #error "Should support acquired_before attribute" #endif Mutex mu_ab ACQUIRED_BEFORE(mu1); Mutex ab_var_noargs __attribute__((acquired_before)); // \ // expected-error {{'acquired_before' attribute takes at least 1 argument}} class ABFoo { private: Mutex ab_field_noargs __attribute__((acquired_before)); // \ // expected-error {{'acquired_before' attribute takes at least 1 argument}} Mutex ab_field_args ACQUIRED_BEFORE(mu1); }; class ACQUIRED_BEFORE(mu1) AB { // \ // expected-warning {{'acquired_before' attribute only applies to fields and global variables}} }; void ab_function() ACQUIRED_BEFORE(mu1); // \ // expected-warning {{'acquired_before' attribute only applies to fields and global variables}} void ab_function_params(int gv_lvar ACQUIRED_BEFORE(mu1)); // \ // expected-warning {{'acquired_before' attribute only applies to fields and global variables}} void ab_testfn(int y){ Mutex x ACQUIRED_BEFORE(mu1) = Mutex(); // \ // expected-warning {{'acquired_before' attribute only applies to fields and global variables}} } // Note: illegal int ab_int ACQUIRED_BEFORE(mu1) will // be taken care of by warnings that ab__int is not lockable. //Check argument parsing. // legal attribute arguments Mutex ab_var_arg_1 ACQUIRED_BEFORE(muWrapper.mu); Mutex ab_var_arg_2 ACQUIRED_BEFORE(muDoubleWrapper.muWrapper->mu); Mutex ab_var_arg_3 ACQUIRED_BEFORE(muWrapper.getMu()); Mutex ab_var_arg_4 ACQUIRED_BEFORE(*muWrapper.getMuPointer()); Mutex ab_var_arg_5 ACQUIRED_BEFORE(&mu1); Mutex ab_var_arg_6 ACQUIRED_BEFORE(muRef); Mutex ab_var_arg_7 ACQUIRED_BEFORE(muDoubleWrapper.getWrapper()->getMu()); Mutex ab_var_arg_8 ACQUIRED_BEFORE(muPointer); // illegal attribute arguments Mutex ab_var_arg_bad_1 ACQUIRED_BEFORE(1); // \ // expected-warning {{'acquired_before' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'int'}} Mutex ab_var_arg_bad_2 ACQUIRED_BEFORE("mu"); // \ // expected-warning {{ignoring 'acquired_before' attribute because its argument is invalid}} Mutex ab_var_arg_bad_3 ACQUIRED_BEFORE(muDoublePointer); // \ // expected-warning {{'acquired_before' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'Mutex **'}} Mutex ab_var_arg_bad_4 ACQUIRED_BEFORE(umu); // \ // expected-warning {{'acquired_before' attribute requires arguments whose type is annotated with 'capability' attribute}} UnlockableMu ab_var_arg_bad_5 ACQUIRED_BEFORE(mu_ab); // \ // expected-warning {{'acquired_before' attribute can only be applied in a context annotated with 'capability("mutex")' attribute}} //-----------------------------------------// // Exclusive Lock Function (elf) //-----------------------------------------// #if !__has_attribute(exclusive_lock_function) #error "Should support exclusive_lock_function attribute" #endif // takes zero or more arguments, all locks (vars/fields) void elf_function() EXCLUSIVE_LOCK_FUNCTION(); void elf_function_args() EXCLUSIVE_LOCK_FUNCTION(mu1, mu2); int elf_testfn(int y) EXCLUSIVE_LOCK_FUNCTION(); int elf_testfn(int y) { int x EXCLUSIVE_LOCK_FUNCTION() = y; // \ // expected-warning {{'exclusive_lock_function' attribute only applies to functions}} return x; }; int elf_test_var EXCLUSIVE_LOCK_FUNCTION(); // \ // expected-warning {{'exclusive_lock_function' attribute only applies to functions}} class ElfFoo { private: int test_field EXCLUSIVE_LOCK_FUNCTION(); // \ // expected-warning {{'exclusive_lock_function' attribute only applies to functions}} void test_method() EXCLUSIVE_LOCK_FUNCTION(); }; class EXCLUSIVE_LOCK_FUNCTION() ElfTestClass { // \ // expected-warning {{'exclusive_lock_function' attribute only applies to functions}} }; void elf_fun_params(int lvar EXCLUSIVE_LOCK_FUNCTION()); // \ // expected-warning {{'exclusive_lock_function' attribute only applies to functions}} // Check argument parsing. // legal attribute arguments int elf_function_1() EXCLUSIVE_LOCK_FUNCTION(muWrapper.mu); int elf_function_2() EXCLUSIVE_LOCK_FUNCTION(muDoubleWrapper.muWrapper->mu); int elf_function_3() EXCLUSIVE_LOCK_FUNCTION(muWrapper.getMu()); int elf_function_4() EXCLUSIVE_LOCK_FUNCTION(*muWrapper.getMuPointer()); int elf_function_5() EXCLUSIVE_LOCK_FUNCTION(&mu1); int elf_function_6() EXCLUSIVE_LOCK_FUNCTION(muRef); int elf_function_7() EXCLUSIVE_LOCK_FUNCTION(muDoubleWrapper.getWrapper()->getMu()); int elf_function_8() EXCLUSIVE_LOCK_FUNCTION(muPointer); int elf_function_9(Mutex x) EXCLUSIVE_LOCK_FUNCTION(1); int elf_function_9(Mutex x, Mutex y) EXCLUSIVE_LOCK_FUNCTION(1,2); // illegal attribute arguments int elf_function_bad_2() EXCLUSIVE_LOCK_FUNCTION("mu"); // \ // expected-warning {{ignoring 'exclusive_lock_function' attribute because its argument is invalid}} int elf_function_bad_3() EXCLUSIVE_LOCK_FUNCTION(muDoublePointer); // \ // expected-warning {{'exclusive_lock_function' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'Mutex **'}} int elf_function_bad_4() EXCLUSIVE_LOCK_FUNCTION(umu); // \ // expected-warning {{'exclusive_lock_function' attribute requires arguments whose type is annotated with 'capability' attribute}} int elf_function_bad_1() EXCLUSIVE_LOCK_FUNCTION(1); // \ // expected-error {{'exclusive_lock_function' attribute parameter 1 is out of bounds: no parameters to index into}} int elf_function_bad_5(Mutex x) EXCLUSIVE_LOCK_FUNCTION(0); // \ // expected-error {{'exclusive_lock_function' attribute parameter 1 is out of bounds: can only be 1, since there is one parameter}} int elf_function_bad_6(Mutex x, Mutex y) EXCLUSIVE_LOCK_FUNCTION(0); // \ // expected-error {{'exclusive_lock_function' attribute parameter 1 is out of bounds: must be between 1 and 2}} int elf_function_bad_7() EXCLUSIVE_LOCK_FUNCTION(0); // \ // expected-error {{'exclusive_lock_function' attribute parameter 1 is out of bounds: no parameters to index into}} //-----------------------------------------// // Shared Lock Function (slf) //-----------------------------------------// #if !__has_attribute(shared_lock_function) #error "Should support shared_lock_function attribute" #endif // takes zero or more arguments, all locks (vars/fields) void slf_function() SHARED_LOCK_FUNCTION(); void slf_function_args() SHARED_LOCK_FUNCTION(mu1, mu2); int slf_testfn(int y) SHARED_LOCK_FUNCTION(); int slf_testfn(int y) { int x SHARED_LOCK_FUNCTION() = y; // \ // expected-warning {{'shared_lock_function' attribute only applies to functions}} return x; }; int slf_test_var SHARED_LOCK_FUNCTION(); // \ // expected-warning {{'shared_lock_function' attribute only applies to functions}} void slf_fun_params(int lvar SHARED_LOCK_FUNCTION()); // \ // expected-warning {{'shared_lock_function' attribute only applies to functions}} class SlfFoo { private: int test_field SHARED_LOCK_FUNCTION(); // \ // expected-warning {{'shared_lock_function' attribute only applies to functions}} void test_method() SHARED_LOCK_FUNCTION(); }; class SHARED_LOCK_FUNCTION() SlfTestClass { // \ // expected-warning {{'shared_lock_function' attribute only applies to functions}} }; // Check argument parsing. // legal attribute arguments int slf_function_1() SHARED_LOCK_FUNCTION(muWrapper.mu); int slf_function_2() SHARED_LOCK_FUNCTION(muDoubleWrapper.muWrapper->mu); int slf_function_3() SHARED_LOCK_FUNCTION(muWrapper.getMu()); int slf_function_4() SHARED_LOCK_FUNCTION(*muWrapper.getMuPointer()); int slf_function_5() SHARED_LOCK_FUNCTION(&mu1); int slf_function_6() SHARED_LOCK_FUNCTION(muRef); int slf_function_7() SHARED_LOCK_FUNCTION(muDoubleWrapper.getWrapper()->getMu()); int slf_function_8() SHARED_LOCK_FUNCTION(muPointer); int slf_function_9(Mutex x) SHARED_LOCK_FUNCTION(1); int slf_function_9(Mutex x, Mutex y) SHARED_LOCK_FUNCTION(1,2); // illegal attribute arguments int slf_function_bad_2() SHARED_LOCK_FUNCTION("mu"); // \ // expected-warning {{ignoring 'shared_lock_function' attribute because its argument is invalid}} int slf_function_bad_3() SHARED_LOCK_FUNCTION(muDoublePointer); // \ // expected-warning {{'shared_lock_function' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'Mutex **'}} int slf_function_bad_4() SHARED_LOCK_FUNCTION(umu); // \ // expected-warning {{'shared_lock_function' attribute requires arguments whose type is annotated with 'capability' attribute}} int slf_function_bad_1() SHARED_LOCK_FUNCTION(1); // \ // expected-error {{'shared_lock_function' attribute parameter 1 is out of bounds: no parameters to index into}} int slf_function_bad_5(Mutex x) SHARED_LOCK_FUNCTION(0); // \ // expected-error {{'shared_lock_function' attribute parameter 1 is out of bounds: can only be 1, since there is one parameter}} int slf_function_bad_6(Mutex x, Mutex y) SHARED_LOCK_FUNCTION(0); // \ // expected-error {{'shared_lock_function' attribute parameter 1 is out of bounds: must be between 1 and 2}} int slf_function_bad_7() SHARED_LOCK_FUNCTION(0); // \ // expected-error {{'shared_lock_function' attribute parameter 1 is out of bounds: no parameters to index into}} //-----------------------------------------// // Exclusive TryLock Function (etf) //-----------------------------------------// #if !__has_attribute(exclusive_trylock_function) #error "Should support exclusive_trylock_function attribute" #endif // takes a mandatory boolean or integer argument specifying the retval // plus an optional list of locks (vars/fields) void etf_function() __attribute__((exclusive_trylock_function)); // \ // expected-error {{'exclusive_trylock_function' attribute takes at least 1 argument}} void etf_function_args() EXCLUSIVE_TRYLOCK_FUNCTION(1, mu2); void etf_function_arg() EXCLUSIVE_TRYLOCK_FUNCTION(1); int etf_testfn(int y) EXCLUSIVE_TRYLOCK_FUNCTION(1); int etf_testfn(int y) { int x EXCLUSIVE_TRYLOCK_FUNCTION(1) = y; // \ // expected-warning {{'exclusive_trylock_function' attribute only applies to functions}} return x; }; int etf_test_var EXCLUSIVE_TRYLOCK_FUNCTION(1); // \ // expected-warning {{'exclusive_trylock_function' attribute only applies to functions}} class EtfFoo { private: int test_field EXCLUSIVE_TRYLOCK_FUNCTION(1); // \ // expected-warning {{'exclusive_trylock_function' attribute only applies to functions}} void test_method() EXCLUSIVE_TRYLOCK_FUNCTION(1); }; class EXCLUSIVE_TRYLOCK_FUNCTION(1) EtfTestClass { // \ // expected-warning {{'exclusive_trylock_function' attribute only applies to functions}} }; void etf_fun_params(int lvar EXCLUSIVE_TRYLOCK_FUNCTION(1)); // \ // expected-warning {{'exclusive_trylock_function' attribute only applies to functions}} // Check argument parsing. // legal attribute arguments int etf_function_1() EXCLUSIVE_TRYLOCK_FUNCTION(1, muWrapper.mu); int etf_function_2() EXCLUSIVE_TRYLOCK_FUNCTION(1, muDoubleWrapper.muWrapper->mu); int etf_function_3() EXCLUSIVE_TRYLOCK_FUNCTION(1, muWrapper.getMu()); int etf_function_4() EXCLUSIVE_TRYLOCK_FUNCTION(1, *muWrapper.getMuPointer()); int etf_function_5() EXCLUSIVE_TRYLOCK_FUNCTION(1, &mu1); int etf_function_6() EXCLUSIVE_TRYLOCK_FUNCTION(1, muRef); int etf_function_7() EXCLUSIVE_TRYLOCK_FUNCTION(1, muDoubleWrapper.getWrapper()->getMu()); int etf_functetfn_8() EXCLUSIVE_TRYLOCK_FUNCTION(1, muPointer); int etf_function_9() EXCLUSIVE_TRYLOCK_FUNCTION(true); // illegal attribute arguments int etf_function_bad_1() EXCLUSIVE_TRYLOCK_FUNCTION(mu1); // \ // expected-error {{'exclusive_trylock_function' attribute requires parameter 1 to be int or bool}} int etf_function_bad_2() EXCLUSIVE_TRYLOCK_FUNCTION("mu"); // \ // expected-error {{'exclusive_trylock_function' attribute requires parameter 1 to be int or bool}} int etf_function_bad_3() EXCLUSIVE_TRYLOCK_FUNCTION(muDoublePointer); // \ // expected-error {{'exclusive_trylock_function' attribute requires parameter 1 to be int or bool}} int etf_function_bad_4() EXCLUSIVE_TRYLOCK_FUNCTION(1, "mu"); // \ // expected-warning {{ignoring 'exclusive_trylock_function' attribute because its argument is invalid}} int etf_function_bad_5() EXCLUSIVE_TRYLOCK_FUNCTION(1, muDoublePointer); // \ // expected-warning {{'exclusive_trylock_function' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'Mutex **'}} int etf_function_bad_6() EXCLUSIVE_TRYLOCK_FUNCTION(1, umu); // \ // expected-warning {{'exclusive_trylock_function' attribute requires arguments whose type is annotated with 'capability' attribute}} //-----------------------------------------// // Shared TryLock Function (stf) //-----------------------------------------// #if !__has_attribute(shared_trylock_function) #error "Should support shared_trylock_function attribute" #endif // takes a mandatory boolean or integer argument specifying the retval // plus an optional list of locks (vars/fields) void stf_function() __attribute__((shared_trylock_function)); // \ // expected-error {{'shared_trylock_function' attribute takes at least 1 argument}} void stf_function_args() SHARED_TRYLOCK_FUNCTION(1, mu2); void stf_function_arg() SHARED_TRYLOCK_FUNCTION(1); int stf_testfn(int y) SHARED_TRYLOCK_FUNCTION(1); int stf_testfn(int y) { int x SHARED_TRYLOCK_FUNCTION(1) = y; // \ // expected-warning {{'shared_trylock_function' attribute only applies to functions}} return x; }; int stf_test_var SHARED_TRYLOCK_FUNCTION(1); // \ // expected-warning {{'shared_trylock_function' attribute only applies to functions}} void stf_fun_params(int lvar SHARED_TRYLOCK_FUNCTION(1)); // \ // expected-warning {{'shared_trylock_function' attribute only applies to functions}} class StfFoo { private: int test_field SHARED_TRYLOCK_FUNCTION(1); // \ // expected-warning {{'shared_trylock_function' attribute only applies to functions}} void test_method() SHARED_TRYLOCK_FUNCTION(1); }; class SHARED_TRYLOCK_FUNCTION(1) StfTestClass { // \ // expected-warning {{'shared_trylock_function' attribute only applies to functions}} }; // Check argument parsing. // legal attribute arguments int stf_function_1() SHARED_TRYLOCK_FUNCTION(1, muWrapper.mu); int stf_function_2() SHARED_TRYLOCK_FUNCTION(1, muDoubleWrapper.muWrapper->mu); int stf_function_3() SHARED_TRYLOCK_FUNCTION(1, muWrapper.getMu()); int stf_function_4() SHARED_TRYLOCK_FUNCTION(1, *muWrapper.getMuPointer()); int stf_function_5() SHARED_TRYLOCK_FUNCTION(1, &mu1); int stf_function_6() SHARED_TRYLOCK_FUNCTION(1, muRef); int stf_function_7() SHARED_TRYLOCK_FUNCTION(1, muDoubleWrapper.getWrapper()->getMu()); int stf_function_8() SHARED_TRYLOCK_FUNCTION(1, muPointer); int stf_function_9() SHARED_TRYLOCK_FUNCTION(true); // illegal attribute arguments int stf_function_bad_1() SHARED_TRYLOCK_FUNCTION(mu1); // \ // expected-error {{'shared_trylock_function' attribute requires parameter 1 to be int or bool}} int stf_function_bad_2() SHARED_TRYLOCK_FUNCTION("mu"); // \ // expected-error {{'shared_trylock_function' attribute requires parameter 1 to be int or bool}} int stf_function_bad_3() SHARED_TRYLOCK_FUNCTION(muDoublePointer); // \ // expected-error {{'shared_trylock_function' attribute requires parameter 1 to be int or bool}} int stf_function_bad_4() SHARED_TRYLOCK_FUNCTION(1, "mu"); // \ // expected-warning {{ignoring 'shared_trylock_function' attribute because its argument is invalid}} int stf_function_bad_5() SHARED_TRYLOCK_FUNCTION(1, muDoublePointer); // \ // expected-warning {{'shared_trylock_function' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'Mutex **'}} int stf_function_bad_6() SHARED_TRYLOCK_FUNCTION(1, umu); // \ // expected-warning {{'shared_trylock_function' attribute requires arguments whose type is annotated with 'capability' attribute}} //-----------------------------------------// // Unlock Function (uf) //-----------------------------------------// #if !__has_attribute(unlock_function) #error "Should support unlock_function attribute" #endif // takes zero or more arguments, all locks (vars/fields) void uf_function() UNLOCK_FUNCTION(); void uf_function_args() UNLOCK_FUNCTION(mu1, mu2); int uf_testfn(int y) UNLOCK_FUNCTION(); int uf_testfn(int y) { int x UNLOCK_FUNCTION() = y; // \ // expected-warning {{'unlock_function' attribute only applies to functions}} return x; }; int uf_test_var UNLOCK_FUNCTION(); // \ // expected-warning {{'unlock_function' attribute only applies to functions}} class UfFoo { private: int test_field UNLOCK_FUNCTION(); // \ // expected-warning {{'unlock_function' attribute only applies to functions}} void test_method() UNLOCK_FUNCTION(); }; class NO_THREAD_SAFETY_ANALYSIS UfTestClass { // \ // expected-warning {{'no_thread_safety_analysis' attribute only applies to functions}} }; void uf_fun_params(int lvar UNLOCK_FUNCTION()); // \ // expected-warning {{'unlock_function' attribute only applies to functions}} // Check argument parsing. // legal attribute arguments int uf_function_1() UNLOCK_FUNCTION(muWrapper.mu); int uf_function_2() UNLOCK_FUNCTION(muDoubleWrapper.muWrapper->mu); int uf_function_3() UNLOCK_FUNCTION(muWrapper.getMu()); int uf_function_4() UNLOCK_FUNCTION(*muWrapper.getMuPointer()); int uf_function_5() UNLOCK_FUNCTION(&mu1); int uf_function_6() UNLOCK_FUNCTION(muRef); int uf_function_7() UNLOCK_FUNCTION(muDoubleWrapper.getWrapper()->getMu()); int uf_function_8() UNLOCK_FUNCTION(muPointer); int uf_function_9(Mutex x) UNLOCK_FUNCTION(1); int uf_function_9(Mutex x, Mutex y) UNLOCK_FUNCTION(1,2); // illegal attribute arguments int uf_function_bad_2() UNLOCK_FUNCTION("mu"); // \ // expected-warning {{ignoring 'unlock_function' attribute because its argument is invalid}} int uf_function_bad_3() UNLOCK_FUNCTION(muDoublePointer); // \ // expected-warning {{'unlock_function' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'Mutex **'}} int uf_function_bad_4() UNLOCK_FUNCTION(umu); // \ // expected-warning {{'unlock_function' attribute requires arguments whose type is annotated with 'capability' attribute}} int uf_function_bad_1() UNLOCK_FUNCTION(1); // \ // expected-error {{'unlock_function' attribute parameter 1 is out of bounds: no parameters to index into}} int uf_function_bad_5(Mutex x) UNLOCK_FUNCTION(0); // \ // expected-error {{'unlock_function' attribute parameter 1 is out of bounds: can only be 1, since there is one parameter}} int uf_function_bad_6(Mutex x, Mutex y) UNLOCK_FUNCTION(0); // \ // expected-error {{'unlock_function' attribute parameter 1 is out of bounds: must be between 1 and 2}} int uf_function_bad_7() UNLOCK_FUNCTION(0); // \ // expected-error {{'unlock_function' attribute parameter 1 is out of bounds: no parameters to index into}} //-----------------------------------------// // Lock Returned (lr) //-----------------------------------------// #if !__has_attribute(lock_returned) #error "Should support lock_returned attribute" #endif // Takes exactly one argument, a var/field void lr_function() __attribute__((lock_returned)); // \ // expected-error {{'lock_returned' attribute takes one argument}} void lr_function_arg() LOCK_RETURNED(mu1); void lr_function_args() __attribute__((lock_returned(mu1, mu2))); // \ // expected-error {{'lock_returned' attribute takes one argument}} int lr_testfn(int y) LOCK_RETURNED(mu1); int lr_testfn(int y) { int x LOCK_RETURNED(mu1) = y; // \ // expected-warning {{'lock_returned' attribute only applies to functions}} return x; }; int lr_test_var LOCK_RETURNED(mu1); // \ // expected-warning {{'lock_returned' attribute only applies to functions}} void lr_fun_params(int lvar LOCK_RETURNED(mu1)); // \ // expected-warning {{'lock_returned' attribute only applies to functions}} class LrFoo { private: int test_field LOCK_RETURNED(mu1); // \ // expected-warning {{'lock_returned' attribute only applies to functions}} void test_method() LOCK_RETURNED(mu1); }; class LOCK_RETURNED(mu1) LrTestClass { // \ // expected-warning {{'lock_returned' attribute only applies to functions}} }; // Check argument parsing. // legal attribute arguments int lr_function_1() LOCK_RETURNED(muWrapper.mu); int lr_function_2() LOCK_RETURNED(muDoubleWrapper.muWrapper->mu); int lr_function_3() LOCK_RETURNED(muWrapper.getMu()); int lr_function_4() LOCK_RETURNED(*muWrapper.getMuPointer()); int lr_function_5() LOCK_RETURNED(&mu1); int lr_function_6() LOCK_RETURNED(muRef); int lr_function_7() LOCK_RETURNED(muDoubleWrapper.getWrapper()->getMu()); int lr_function_8() LOCK_RETURNED(muPointer); // illegal attribute arguments int lr_function_bad_1() LOCK_RETURNED(1); // \ // expected-warning {{'lock_returned' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'int'}} int lr_function_bad_2() LOCK_RETURNED("mu"); // \ // expected-warning {{ignoring 'lock_returned' attribute because its argument is invalid}} int lr_function_bad_3() LOCK_RETURNED(muDoublePointer); // \ // expected-warning {{'lock_returned' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'Mutex **'}} int lr_function_bad_4() LOCK_RETURNED(umu); // \ // expected-warning {{'lock_returned' attribute requires arguments whose type is annotated with 'capability' attribute}} //-----------------------------------------// // Locks Excluded (le) //-----------------------------------------// #if !__has_attribute(locks_excluded) #error "Should support locks_excluded attribute" #endif // takes one or more arguments, all locks (vars/fields) void le_function() __attribute__((locks_excluded)); // \ // expected-error {{'locks_excluded' attribute takes at least 1 argument}} void le_function_arg() LOCKS_EXCLUDED(mu1); void le_function_args() LOCKS_EXCLUDED(mu1, mu2); int le_testfn(int y) LOCKS_EXCLUDED(mu1); int le_testfn(int y) { int x LOCKS_EXCLUDED(mu1) = y; // \ // expected-warning {{'locks_excluded' attribute only applies to functions}} return x; }; int le_test_var LOCKS_EXCLUDED(mu1); // \ // expected-warning {{'locks_excluded' attribute only applies to functions}} void le_fun_params(int lvar LOCKS_EXCLUDED(mu1)); // \ // expected-warning {{'locks_excluded' attribute only applies to functions}} class LeFoo { private: int test_field LOCKS_EXCLUDED(mu1); // \ // expected-warning {{'locks_excluded' attribute only applies to functions}} void test_method() LOCKS_EXCLUDED(mu1); }; class LOCKS_EXCLUDED(mu1) LeTestClass { // \ // expected-warning {{'locks_excluded' attribute only applies to functions}} }; // Check argument parsing. // legal attribute arguments int le_function_1() LOCKS_EXCLUDED(muWrapper.mu); int le_function_2() LOCKS_EXCLUDED(muDoubleWrapper.muWrapper->mu); int le_function_3() LOCKS_EXCLUDED(muWrapper.getMu()); int le_function_4() LOCKS_EXCLUDED(*muWrapper.getMuPointer()); int le_function_5() LOCKS_EXCLUDED(&mu1); int le_function_6() LOCKS_EXCLUDED(muRef); int le_function_7() LOCKS_EXCLUDED(muDoubleWrapper.getWrapper()->getMu()); int le_function_8() LOCKS_EXCLUDED(muPointer); // illegal attribute arguments int le_function_bad_1() LOCKS_EXCLUDED(1); // \ // expected-warning {{'locks_excluded' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'int'}} int le_function_bad_2() LOCKS_EXCLUDED("mu"); // \ // expected-warning {{ignoring 'locks_excluded' attribute because its argument is invalid}} int le_function_bad_3() LOCKS_EXCLUDED(muDoublePointer); // \ // expected-warning {{'locks_excluded' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'Mutex **'}} int le_function_bad_4() LOCKS_EXCLUDED(umu); // \ // expected-warning {{'locks_excluded' attribute requires arguments whose type is annotated with 'capability' attribute}} //-----------------------------------------// // Exclusive Locks Required (elr) //-----------------------------------------// #if !__has_attribute(exclusive_locks_required) #error "Should support exclusive_locks_required attribute" #endif // takes one or more arguments, all locks (vars/fields) void elr_function() __attribute__((exclusive_locks_required)); // \ // expected-error {{'exclusive_locks_required' attribute takes at least 1 argument}} void elr_function_arg() EXCLUSIVE_LOCKS_REQUIRED(mu1); void elr_function_args() EXCLUSIVE_LOCKS_REQUIRED(mu1, mu2); int elr_testfn(int y) EXCLUSIVE_LOCKS_REQUIRED(mu1); int elr_testfn(int y) { int x EXCLUSIVE_LOCKS_REQUIRED(mu1) = y; // \ // expected-warning {{'exclusive_locks_required' attribute only applies to functions}} return x; }; int elr_test_var EXCLUSIVE_LOCKS_REQUIRED(mu1); // \ // expected-warning {{'exclusive_locks_required' attribute only applies to functions}} void elr_fun_params(int lvar EXCLUSIVE_LOCKS_REQUIRED(mu1)); // \ // expected-warning {{'exclusive_locks_required' attribute only applies to functions}} class ElrFoo { private: int test_field EXCLUSIVE_LOCKS_REQUIRED(mu1); // \ // expected-warning {{'exclusive_locks_required' attribute only applies to functions}} void test_method() EXCLUSIVE_LOCKS_REQUIRED(mu1); }; class EXCLUSIVE_LOCKS_REQUIRED(mu1) ElrTestClass { // \ // expected-warning {{'exclusive_locks_required' attribute only applies to functions}} }; // Check argument parsing. // legal attribute arguments int elr_function_1() EXCLUSIVE_LOCKS_REQUIRED(muWrapper.mu); int elr_function_2() EXCLUSIVE_LOCKS_REQUIRED(muDoubleWrapper.muWrapper->mu); int elr_function_3() EXCLUSIVE_LOCKS_REQUIRED(muWrapper.getMu()); int elr_function_4() EXCLUSIVE_LOCKS_REQUIRED(*muWrapper.getMuPointer()); int elr_function_5() EXCLUSIVE_LOCKS_REQUIRED(&mu1); int elr_function_6() EXCLUSIVE_LOCKS_REQUIRED(muRef); int elr_function_7() EXCLUSIVE_LOCKS_REQUIRED(muDoubleWrapper.getWrapper()->getMu()); int elr_function_8() EXCLUSIVE_LOCKS_REQUIRED(muPointer); // illegal attribute arguments int elr_function_bad_1() EXCLUSIVE_LOCKS_REQUIRED(1); // \ // expected-warning {{'exclusive_locks_required' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'int'}} int elr_function_bad_2() EXCLUSIVE_LOCKS_REQUIRED("mu"); // \ // expected-warning {{ignoring 'exclusive_locks_required' attribute because its argument is invalid}} int elr_function_bad_3() EXCLUSIVE_LOCKS_REQUIRED(muDoublePointer); // \ // expected-warning {{'exclusive_locks_required' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'Mutex **'}} int elr_function_bad_4() EXCLUSIVE_LOCKS_REQUIRED(umu); // \ // expected-warning {{'exclusive_locks_required' attribute requires arguments whose type is annotated with 'capability' attribute}} //-----------------------------------------// // Shared Locks Required (slr) //-----------------------------------------// #if !__has_attribute(shared_locks_required) #error "Should support shared_locks_required attribute" #endif // takes one or more arguments, all locks (vars/fields) void slr_function() __attribute__((shared_locks_required)); // \ // expected-error {{'shared_locks_required' attribute takes at least 1 argument}} void slr_function_arg() SHARED_LOCKS_REQUIRED(mu1); void slr_function_args() SHARED_LOCKS_REQUIRED(mu1, mu2); int slr_testfn(int y) SHARED_LOCKS_REQUIRED(mu1); int slr_testfn(int y) { int x SHARED_LOCKS_REQUIRED(mu1) = y; // \ // expected-warning {{'shared_locks_required' attribute only applies to functions}} return x; }; int slr_test_var SHARED_LOCKS_REQUIRED(mu1); // \ // expected-warning {{'shared_locks_required' attribute only applies to functions}} void slr_fun_params(int lvar SHARED_LOCKS_REQUIRED(mu1)); // \ // expected-warning {{'shared_locks_required' attribute only applies to functions}} class SlrFoo { private: int test_field SHARED_LOCKS_REQUIRED(mu1); // \ // expected-warning {{'shared_locks_required' attribute only applies to functions}} void test_method() SHARED_LOCKS_REQUIRED(mu1); }; class SHARED_LOCKS_REQUIRED(mu1) SlrTestClass { // \ // expected-warning {{'shared_locks_required' attribute only applies to functions}} }; // Check argument parsing. // legal attribute arguments int slr_function_1() SHARED_LOCKS_REQUIRED(muWrapper.mu); int slr_function_2() SHARED_LOCKS_REQUIRED(muDoubleWrapper.muWrapper->mu); int slr_function_3() SHARED_LOCKS_REQUIRED(muWrapper.getMu()); int slr_function_4() SHARED_LOCKS_REQUIRED(*muWrapper.getMuPointer()); int slr_function_5() SHARED_LOCKS_REQUIRED(&mu1); int slr_function_6() SHARED_LOCKS_REQUIRED(muRef); int slr_function_7() SHARED_LOCKS_REQUIRED(muDoubleWrapper.getWrapper()->getMu()); int slr_function_8() SHARED_LOCKS_REQUIRED(muPointer); // illegal attribute arguments int slr_function_bad_1() SHARED_LOCKS_REQUIRED(1); // \ // expected-warning {{'shared_locks_required' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'int'}} int slr_function_bad_2() SHARED_LOCKS_REQUIRED("mu"); // \ // expected-warning {{ignoring 'shared_locks_required' attribute because its argument is invalid}} int slr_function_bad_3() SHARED_LOCKS_REQUIRED(muDoublePointer); // \ // expected-warning {{'shared_locks_required' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'Mutex **'}} int slr_function_bad_4() SHARED_LOCKS_REQUIRED(umu); // \ // expected-warning {{'shared_locks_required' attribute requires arguments whose type is annotated with 'capability' attribute}} //-----------------------------------------// // Regression tests for unusual cases. //-----------------------------------------// int trivially_false_edges(bool b) { // Create NULL (never taken) edges in CFG if (false) return 1; else return 2; } // Possible Clang bug -- method pointer in template parameter class UnFoo { public: void foo(); }; template<void (UnFoo::*methptr)()> class MCaller { public: static void call_method_ptr(UnFoo *f) { // FIXME: Possible Clang bug: // getCalleeDecl() returns NULL in the following case: (f->*methptr)(); } }; void call_method_ptr_inst(UnFoo* f) { MCaller<&UnFoo::foo>::call_method_ptr(f); } int temp; void empty_back_edge() { // Create a back edge to a block with with no statements for (;;) { ++temp; if (temp > 10) break; } } struct Foomger { void operator++(); }; struct Foomgoper { Foomger f; bool done(); void invalid_back_edge() { do { // FIXME: Possible Clang bug: // The first statement in this basic block has no source location ++f; } while (!done()); } }; //----------------------------------------------------- // Parsing of member variables and function parameters //------------------------------------------------------ Mutex gmu; class StaticMu { static Mutex statmu; }; class FooLate { public: void foo1() EXCLUSIVE_LOCKS_REQUIRED(gmu) { } void foo2() EXCLUSIVE_LOCKS_REQUIRED(mu) { } void foo3(Mutex *m) EXCLUSIVE_LOCKS_REQUIRED(m) { } void foo3(FooLate *f) EXCLUSIVE_LOCKS_REQUIRED(f->mu) { } void foo4(FooLate *f) EXCLUSIVE_LOCKS_REQUIRED(f->mu); static void foo5() EXCLUSIVE_LOCKS_REQUIRED(mu); // \ // expected-error {{invalid use of member 'mu' in static member function}} template <class T> void foo6() EXCLUSIVE_LOCKS_REQUIRED(T::statmu) { } template <class T> void foo7(T* f) EXCLUSIVE_LOCKS_REQUIRED(f->mu) { } int a GUARDED_BY(gmu); int b GUARDED_BY(mu); int c GUARDED_BY(this->mu); Mutex mu; }; //------------------------- // Empty argument lists //------------------------- class LOCKABLE EmptyArgListsTest { void lock() EXCLUSIVE_LOCK_FUNCTION() { } void unlock() UNLOCK_FUNCTION() { } }; namespace FunctionDefinitionParseTest { // Test parsing of attributes on function definitions. class Foo { public: Mutex mu_; void foo1(); void foo2(Foo *f); }; template <class T> class Bar { public: Mutex mu_; void bar(); }; void Foo::foo1() EXCLUSIVE_LOCKS_REQUIRED(mu_) { } void Foo::foo2(Foo *f) EXCLUSIVE_LOCKS_REQUIRED(f->mu_) { } template <class T> void Bar<T>::bar() EXCLUSIVE_LOCKS_REQUIRED(mu_) { } void baz(Foo *f) EXCLUSIVE_LOCKS_REQUIRED(f->mu_) { } } // end namespace namespace TestMultiDecl { class Foo { public: int GUARDED_BY(mu_) a; int GUARDED_BY(mu_) b, c; private: Mutex mu_; }; } // end namespace TestMultiDecl namespace NestedClassLateDecl { class Foo { class Bar { int a GUARDED_BY(mu); int b GUARDED_BY(fooMuStatic); void bar() EXCLUSIVE_LOCKS_REQUIRED(mu) { a = 0; } void bar2(Bar* b) EXCLUSIVE_LOCKS_REQUIRED(b->mu) { b->a = 0; } void bar3(Foo* f) EXCLUSIVE_LOCKS_REQUIRED(f->fooMu) { f->a = 0; } Mutex mu; }; int a GUARDED_BY(fooMu); Mutex fooMu; static Mutex fooMuStatic; }; } namespace PointerToMemberTest { // Empty string should be ignored. int testEmptyAttribute GUARDED_BY(""); void testEmptyAttributeFunction() EXCLUSIVE_LOCKS_REQUIRED(""); class Graph { public: Mutex mu_; static Mutex* get_static_mu() LOCK_RETURNED(&Graph::mu_); }; class Node { public: void foo() EXCLUSIVE_LOCKS_REQUIRED(&Graph::mu_); int a GUARDED_BY(&Graph::mu_); }; } namespace SmartPointerTest { template<class T> class smart_ptr { public: T* operator->() { return ptr_; } T& operator*() { return ptr_; } private: T* ptr_; }; Mutex gmu; smart_ptr<int> gdat PT_GUARDED_BY(gmu); class MyClass { public: Mutex mu_; smart_ptr<Mutex> smu_; smart_ptr<int> a PT_GUARDED_BY(mu_); int b GUARDED_BY(smu_); }; } namespace InheritanceTest { class LOCKABLE Base { public: void lock() EXCLUSIVE_LOCK_FUNCTION(); void unlock() UNLOCK_FUNCTION(); }; class Base2 { }; class Derived1 : public Base { }; class Derived2 : public Base2, public Derived1 { }; class Derived3 : public Base2 { }; class Foo { Derived1 mu1_; Derived2 mu2_; Derived3 mu3_; int a GUARDED_BY(mu1_); int b GUARDED_BY(mu2_); int c GUARDED_BY(mu3_); // \ // expected-warning {{'guarded_by' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'InheritanceTest::Derived3'}} void foo() EXCLUSIVE_LOCKS_REQUIRED(mu1_, mu2_) { a = 0; b = 0; } }; } namespace InvalidDeclTest { class Foo { }; namespace { void Foo::bar(Mutex* mu) LOCKS_EXCLUDED(mu) { } // \ // expected-error {{cannot define or redeclare 'bar' here because namespace '' does not enclose namespace 'Foo'}} \ // expected-warning {{attribute locks_excluded ignored, because it is not attached to a declaration}} } } // end namespace InvalidDeclTest namespace StaticScopeTest { class FooStream; class Foo { mutable Mutex mu; int a GUARDED_BY(mu); static int si GUARDED_BY(mu); // \ // expected-error {{invalid use of non-static data member 'mu'}} static void foo() EXCLUSIVE_LOCKS_REQUIRED(mu); // \ // expected-error {{invalid use of member 'mu' in static member function}} friend FooStream& operator<<(FooStream& s, const Foo& f) EXCLUSIVE_LOCKS_REQUIRED(mu); // \ // expected-error {{invalid use of non-static data member 'mu'}} }; } // end namespace StaticScopeTest namespace FunctionAttributesInsideClass_ICE_Test { class Foo { public: /* Originally found when parsing foo() as an ordinary method after the * the following: template <class T> void syntaxErrorMethod(int i) { if (i) { foo( } } */ void method() { void foo() EXCLUSIVE_LOCKS_REQUIRED(mu); // \ // expected-error {{use of undeclared identifier 'mu'}} } }; } // end namespace FunctionAttributesInsideClass_ICE_Test
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/crashes.cpp
// RUN: %clang_cc1 -fsyntax-only -verify %s // <rdar://problem/8124080> template<typename _Alloc> class allocator; template<class _CharT> struct char_traits; template<typename _CharT, typename _Traits = char_traits<_CharT>, typename _Alloc = allocator<_CharT> > class basic_string; template<typename _CharT, typename _Traits, typename _Alloc> const typename basic_string<_CharT, _Traits, _Alloc>::size_type basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_max_size // expected-error{{no member named '_Rep' in 'basic_string<_CharT, _Traits, _Alloc>'}} = (((npos - sizeof(_Rep_base))/sizeof(_CharT)) - 1) / 4; // PR7118 template<typename T> class Foo { class Bar; void f() { Bar i; } }; // PR7625 template<typename T> struct a : T { struct x : T { int aa() { return p; } // expected-error{{use of undeclared identifier 'p'}} }; }; // rdar://8605381 namespace rdar8605381 { struct X {}; struct Y { // expected-note{{candidate}} Y(); }; struct { Y obj; } objs[] = { new Y // expected-error{{no viable conversion}} }; } // http://llvm.org/PR8234 namespace PR8234 { template<typename Signature> class callback { }; template<typename R , typename ARG_TYPE0> class callback<R( ARG_TYPE0)> { public: callback() {} }; template< typename ARG_TYPE0> class callback<void( ARG_TYPE0)> { public: callback() {} }; void f() { callback<void(const int&)> op; } } namespace PR9007 { struct bar { enum xxx { yyy = sizeof(struct foo*) }; foo *xxx(); }; } namespace PR9026 { class InfallibleTArray { }; class Variant; class CompVariant { operator const InfallibleTArray&() const; }; class Variant { operator const CompVariant&() const; }; void Write(const Variant& __v); void Write(const InfallibleTArray& __v); Variant x; void Write2() { Write(x); } } namespace PR10270 { template<typename T> class C; template<typename T> void f() { if (C<T> == 1) // expected-error{{expected unqualified-id}} \ // expected-error{{invalid '==' at end of declaration}} return; } } namespace rdar11806334 { class cc_YCbCr; class cc_rgb { public: cc_rgb( uint p ); // expected-error {{unknown type name}} cc_rgb( cc_YCbCr v_in ); }; class cc_hsl { public: cc_rgb rgb(); cc_YCbCr YCbCr(); }; class cc_YCbCr { public: cc_YCbCr( const cc_rgb v_in ); }; cc_YCbCr cc_hsl::YCbCr() { cc_YCbCr v_out = cc_YCbCr( rgb()); return v_out; } } namespace test1 { int getString(const int*); template<int a> class ELFObjectFile { const int* sh; ELFObjectFile() { switch (*sh) { } int SectionName(getString(sh)); } }; } namespace test2 { struct fltSemantics ; const fltSemantics &foobar(); void VisitCastExpr(int x) { switch (x) { case 42: const fltSemantics &Sem = foobar(); } } } namespace test3 { struct nsCSSRect { }; static int nsCSSRect::* sides; nsCSSRect dimenX; void ParseBoxCornerRadii(int y) { switch (y) { } int& x = dimenX.*sides; } } namespace pr16964 { template<typename> struct bs { bs(); static int* member(); // expected-note{{possible target}} member(); // expected-error{{C++ requires a type specifier for all declarations}} static member(); // expected-error{{C++ requires a type specifier for all declarations}} static int* member(int); // expected-note{{possible target}} }; template<typename T> bs<T>::bs() { member; } // expected-error{{did you mean to call it}} bs<int> test() { return bs<int>(); // expected-note{{in instantiation}} } } namespace pr12791 { template<class _Alloc> class allocator {}; template<class _CharT> struct char_traits; struct input_iterator_tag {}; struct forward_iterator_tag : public input_iterator_tag {}; template<typename _CharT, typename _Traits, typename _Alloc> struct basic_string { struct _Alloc_hider : _Alloc { _Alloc_hider(_CharT*, const _Alloc&); }; mutable _Alloc_hider _M_dataplus; template<class _InputIterator> basic_string(_InputIterator __beg, _InputIterator __end, const _Alloc& __a = _Alloc()); template<class _InIterator> static _CharT* _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a, input_iterator_tag); template<class _FwdIterator> static _CharT* _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a, forward_iterator_tag); static _CharT* _S_construct(size_type __req, _CharT __c, const _Alloc& __a); // expected-error{{unknown type name 'size_type'}} }; template<typename _CharT, typename _Traits, typename _Alloc> template<typename _InputIterator> basic_string<_CharT, _Traits, _Alloc>:: basic_string(_InputIterator __beg, _InputIterator __end, const _Alloc& __a) : _M_dataplus(_S_construct(__beg, __end, __a, input_iterator_tag()), __a) {} template<typename _CharT, typename _Traits = char_traits<_CharT>, typename _Alloc = allocator<_CharT> > struct basic_stringbuf { typedef _CharT char_type; typedef basic_string<char_type, _Traits, _Alloc> __string_type; __string_type str() const {__string_type((char_type*)0,(char_type*)0);} }; template class basic_stringbuf<char>; } namespace pr16989 { class C { template <class T> C tpl_mem(T *) { return } // expected-error{{expected expression}} void mem(int *p) { tpl_mem(p); } }; class C2 { void f(); }; void C2::f() {} } namespace pr20660 { appendList(int[]...); // expected-error {{C++ requires a type specifier for all declarations}} appendList(int[]...) { } // expected-error {{C++ requires a type specifier for all declarations}} }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/warn-overloaded-virtual.cpp
// RUN: %clang_cc1 -fsyntax-only -Woverloaded-virtual -verify %s struct B1 { virtual void foo(int); // expected-note {{declared here}} virtual void foo(); // expected-note {{declared here}} }; struct S1 : public B1 { void foo(float); // expected-warning {{hides overloaded virtual functions}} }; struct S2 : public B1 { void foo(); // expected-note {{declared here}} }; struct B2 { virtual void foo(void*); // expected-note {{declared here}} }; struct MS1 : public S2, public B2 { virtual void foo(int); // expected-warning {{hides overloaded virtual functions}} }; struct B3 { virtual void foo(int); virtual void foo(); }; struct S3 : public B3 { using B3::foo; void foo(float); }; struct B4 { virtual void foo(); }; struct S4 : public B4 { void foo(float); void foo(); }; namespace PR9182 { struct Base { virtual void foo(int); }; void Base::foo(int) { } struct Derived : public Base { virtual void foo(int); void foo(int, int); }; } namespace PR9396 { class A { public: virtual void f(int) {} }; class B : public A { public: static void f() {} }; } namespace ThreeLayer { struct A { virtual void f(); }; struct B: A { void f(); void f(int); }; struct C: B { void f(int); using A::f; }; } namespace UnbalancedVirtual { struct Base { virtual void func(); }; struct Derived1: virtual Base { virtual void func(); }; struct Derived2: virtual Base { }; struct MostDerived: Derived1, Derived2 { void func(int); void func(); }; } namespace UnbalancedVirtual2 { struct Base { virtual void func(); }; struct Derived1: virtual Base { virtual void func(); }; struct Derived2: virtual Base { }; struct Derived3: Derived1 { virtual void func(); }; struct MostDerived: Derived3, Derived2 { void func(int); void func(); }; } namespace { class A { virtual int foo(bool) const; // expected-note@-1{{type mismatch at 1st parameter ('bool' vs 'int')}} virtual int foo(int, int) const; // expected-note@-1{{different number of parameters (2 vs 1)}} virtual int foo(int*) const; // expected-note@-1{{type mismatch at 1st parameter ('int *' vs 'int')}} virtual int foo(int) volatile; // expected-note@-1{{different qualifiers (volatile vs const)}} }; class B : public A { virtual int foo(int) const; // expected-warning@-1{{hides overloaded virtual functions}} }; } namespace { struct base { void f(char) {} }; struct derived : base { void f(int) {} }; void foo(derived &d) { d.f('1'); // FIXME: this should warn about calling (anonymous namespace)::derived::f(int) // instead of (anonymous namespace)::base::f(char). // Note: this should be under a new diagnostic flag and eventually moved to a // new test case since it's not strictly related to virtual functions. d.f(12); // This should not warn. } }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/pr18284-crash-on-invalid.cpp
// RUN: %clang_cc1 -fsyntax-only -verify %s // Don't crash (PR18284). namespace n1 { class A { }; class C { A a; }; A::RunTest() {} // expected-error {{C++ requires a type specifier for all declarations}} void f() { new C; } } // namespace n1 namespace n2 { class A { }; class C : public A { }; A::RunTest() {} // expected-error {{C++ requires a type specifier for all declarations}} void f() { new C; } } // namespace n2
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/nonnull.cpp
// RUN: %clang_cc1 -fsyntax-only -verify %s template<int I> struct TS { __attribute__((returns_nonnull)) void *value_dependent(void) { return I; // no-warning } __attribute__((returns_nonnull)) void *value_independent(void) { return 0; // expected-warning {{null returned from function that requires a non-null return value}} } }; namespace Template { template<typename T> __attribute__((nonnull)) void f(T t); void g() { f((void*)0); } // expected-warning {{null passed to a callee that requires a non-null argument}} void h() { f(0); } }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/ms-novtable.cpp
// RUN: %clang_cc1 %s -fsyntax-only -verify -fms-extensions -Wno-microsoft -std=c++11 struct __declspec(novtable) S {}; enum __declspec(novtable) E {}; // expected-warning{{'novtable' attribute only applies to classes}} int __declspec(novtable) I; // expected-warning{{'novtable' attribute only applies to classes}} typedef struct T __declspec(novtable) U; // expected-warning{{'novtable' attribute only applies to classes}} auto z = []() __declspec(novtable) { return nullptr; }; // expected-warning{{'novtable' attribute only applies to classes}}
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/types_compatible_p.cpp
// RUN: %clang_cc1 -fsyntax-only -verify -x c++ %s // RUN: %clang_cc1 -fsyntax-only -x c %s // Test that GNU C extension __builtin_types_compatible_p() is not available in C++ mode. int f() { return __builtin_types_compatible_p(int, const int); // expected-error{{expected '(' for function-style cast or type construction}} \ // expected-error{{expected expression}} }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/no-rtti.cpp
// RUN: %clang_cc1 -fsyntax-only -verify -fno-rtti %s namespace std { class type_info; } void f() { (void)typeid(int); // expected-error {{cannot use typeid with -fno-rtti}} } namespace { struct A { virtual ~A(){}; }; struct B : public A { B() : A() {} }; } bool isa_B(A *a) { return dynamic_cast<B *>(a) != 0; // expected-error {{cannot use dynamic_cast with -fno-rtti}} } void* getMostDerived(A* a) { // This cast does not use RTTI. return dynamic_cast<void *>(a); }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/conversion-incomplete-type.cpp
// RUN: %clang_cc1 -fsyntax-only -verify %s struct string {}; class StringPiece; // expected-note {{forward declaration of 'StringPiece'}} \ // expected-note {{forward declaration of 'StringPiece'}} struct Test { void expectStringPiece(const StringPiece& blah) {}; // expected-note {{passing argument to parameter 'blah' here}} void test(const string& s) { expectStringPiece(s); // expected-error {{no viable conversion from 'const string' to incomplete type 'const StringPiece'}} } }; struct TestStatic { static void expectStringPiece(const StringPiece& blah) {}; // expected-note {{passing argument to parameter 'blah' here}} static void test(const string& s) { expectStringPiece(s); // expected-error {{no viable conversion from 'const string' to incomplete type 'const StringPiece'}} } };
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/conversion-function.cpp
// RUN: %clang_cc1 -fsyntax-only -Wbind-to-temporary-copy -verify %s class X { public: operator bool(); operator int() const; bool f() { return operator bool(); } float g() { return operator float(); // expected-error{{use of undeclared 'operator float'}} } static operator short(); // expected-error{{conversion function must be a non-static member function}} }; operator int(); // expected-error{{conversion function must be a non-static member function}} operator int; // expected-error{{'operator int' cannot be the name of a variable or data member}} typedef int func_type(int); typedef int array_type[10]; class Y { public: void operator bool(int, ...) const; // expected-error{{conversion function cannot have a return type}} \ // expected-error{{conversion function cannot have any parameters}} operator bool(int a = 4, int b = 6) const; // expected-error{{conversion function cannot have any parameters}} operator float(...) const; // expected-error{{conversion function cannot be variadic}} operator func_type(); // expected-error{{conversion function cannot convert to a function type}} operator array_type(); // expected-error{{conversion function cannot convert to an array type}} }; typedef int INT; typedef INT* INT_PTR; class Z { operator int(); // expected-note {{previous declaration is here}} operator int**(); // expected-note {{previous declaration is here}} operator INT(); // expected-error{{conversion function cannot be redeclared}} operator INT_PTR*(); // expected-error{{conversion function cannot be redeclared}} }; class A { }; class B : public A { public: operator A&() const; // expected-warning{{conversion function converting 'B' to its base class 'A' will never be used}} operator const void() const; // expected-warning{{conversion function converting 'B' to 'const void' will never be used}} operator const B(); // expected-warning{{conversion function converting 'B' to itself will never be used}} }; // This used to crash Clang. struct Flip; struct Flop { Flop(); Flop(const Flip&); // expected-note{{candidate constructor}} }; struct Flip { operator Flop() const; // expected-note{{candidate function}} }; Flop flop = Flip(); // expected-error {{conversion from 'Flip' to 'Flop' is ambiguous}} // This tests that we don't add the second conversion declaration to the list of user conversions struct C { operator const char *() const; }; C::operator const char*() const { return 0; } void f(const C& c) { const char* v = c; } // Test. Conversion in base class is visible in derived class. class XB { public: operator int(); // expected-note {{candidate function}} }; class Yb : public XB { public: operator char(); // expected-note {{candidate function}} }; void f(Yb& a) { if (a) { } // expected-error {{conversion from 'Yb' to 'bool' is ambiguous}} int i = a; // OK. calls XB::operator int(); char ch = a; // OK. calls Yb::operator char(); } // Test conversion + copy construction. class AutoPtrRef { }; class AutoPtr { AutoPtr(AutoPtr &); // expected-note{{declared private here}} public: AutoPtr(); AutoPtr(AutoPtrRef); operator AutoPtrRef(); }; AutoPtr make_auto_ptr(); AutoPtr test_auto_ptr(bool Cond) { AutoPtr p1( make_auto_ptr() ); AutoPtr p; if (Cond) return p; // expected-error{{calling a private constructor}} return AutoPtr(); } struct A1 { A1(const char *); ~A1(); private: A1(const A1&); // expected-note 2 {{declared private here}} }; A1 f() { // FIXME: redundant diagnostics! return "Hello"; // expected-error {{calling a private constructor}} expected-warning {{an accessible copy constructor}} } namespace source_locations { template<typename T> struct sneaky_int { typedef int type; }; template<typename T, typename U> struct A { }; template<typename T> struct A<T, T> : A<T, int> { }; struct E { template<typename T> operator A<T, typename sneaky_int<T>::type>&() const; // expected-note{{candidate function}} }; void f() { A<float, float> &af = E(); // expected-error{{no viable conversion}} A<float, int> &af2 = E(); const A<float, int> &caf2 = E(); } // Check template<typename T> struct E2 { operator T * // expected-error{{pointer to a reference}} () const; }; E2<int&> e2i; // expected-note{{in instantiation}} } namespace crazy_declarators { struct A { (&operator bool())(); // expected-error {{use a typedef to declare a conversion to 'bool (&)()'}} *operator int(); // expected-error {{put the complete type after 'operator'}} // No suggestion of using a typedef here; that's not possible. template<typename T> (&operator T())(); // expected-error-re {{cannot specify any part of a return type in the declaration of a conversion function{{$}}}} }; } namespace smart_ptr { class Y { class YRef { }; Y(Y&); public: Y(); Y(YRef); operator YRef(); // expected-note{{candidate function}} }; struct X { // expected-note{{candidate constructor (the implicit copy constructor) not}} explicit X(Y); }; Y make_Y(); X f() { X x = make_Y(); // expected-error{{no viable conversion from 'smart_ptr::Y' to 'smart_ptr::X'}} X x2(make_Y()); return X(Y()); } } struct Any { Any(...); }; struct Other { Other(const Other &); Other(); }; void test_any() { Any any = Other(); // expected-error{{cannot pass object of non-POD type 'Other' through variadic constructor; call will abort at runtime}} } namespace PR7055 { // Make sure that we don't allow too many conversions in an // auto_ptr-like template. In particular, we can't create multiple // temporary objects when binding to a reference. struct auto_ptr { struct auto_ptr_ref { }; auto_ptr(auto_ptr&); auto_ptr(auto_ptr_ref); explicit auto_ptr(int *); operator auto_ptr_ref(); }; struct X { X(auto_ptr); }; X f() { X x(auto_ptr(new int)); return X(auto_ptr(new int)); } auto_ptr foo(); X e(foo()); struct Y { Y(X); }; Y f2(foo()); } namespace PR7934 { typedef unsigned char uint8; struct MutablePtr { MutablePtr() : ptr(0) {} void *ptr; operator void*() { return ptr; } private: operator uint8*() { return reinterpret_cast<uint8*>(ptr); } operator const char*() const { return reinterpret_cast<const char*>(ptr); } }; void fake_memcpy(const void *); void use() { MutablePtr ptr; fake_memcpy(ptr); } } namespace rdar8018274 { struct X { }; struct Y { operator const struct X *() const; }; struct Z : Y { operator struct X * (); }; void test() { Z x; (void) (x != __null); } struct Base { operator int(); }; struct Derived1 : Base { }; struct Derived2 : Base { }; struct SuperDerived : Derived1, Derived2 { using Derived1::operator int; }; struct UeberDerived : SuperDerived { operator long(); }; void test2(UeberDerived ud) { int i = ud; // expected-error{{ambiguous conversion from derived class 'rdar8018274::SuperDerived' to base class 'rdar8018274::Base'}} } struct Base2 { operator int(); }; struct Base3 { operator int(); }; struct Derived23 : Base2, Base3 { using Base2::operator int; }; struct ExtraDerived23 : Derived23 { }; void test3(ExtraDerived23 ed) { int i = ed; } } namespace PR8065 { template <typename T> struct Iterator; template <typename T> struct Container; template<> struct Iterator<int> { typedef Container<int> container_type; }; template <typename T> struct Container { typedef typename Iterator<T>::container_type X; operator X(void) { return X(); } }; Container<int> test; } namespace PR8034 { struct C { operator int(); private: template <typename T> operator T(); }; int x = C().operator int(); } namespace PR9336 { template<class T> struct generic_list { template<class Container> operator Container() { Container ar; T* i; ar[0]=*i; return ar; } }; template<class T> struct array { T& operator[](int); const T& operator[](int)const; }; generic_list<generic_list<int> > l; array<array<int> > a = l; } namespace PR8800 { struct A; struct C { operator A&(); }; void f() { C c; A& a1(c); A& a2 = c; A& a3 = static_cast<A&>(c); A& a4 = (A&)c; } } namespace PR12712 { struct A {}; struct B { operator A(); operator A() const; }; struct C : B {}; A f(const C c) { return c; } } namespace PR18234 { struct A { operator enum E { e } (); // expected-error {{'PR18234::A::E' cannot be defined in a type specifier}} operator struct S { int n; } (); // expected-error {{'PR18234::A::S' cannot be defined in a type specifier}} } a; A::S s = a; A::E e = a; // expected-note {{here}} bool k1 = e == A::e; // expected-error {{no member named 'e'}} bool k2 = e.n == 0; }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/expressions.cpp
// RUN: %clang_cc1 -fsyntax-only -verify -Wno-constant-conversion %s void choice(int); int choice(bool); void test() { // Result of ! must be type bool. int i = choice(!1); } // rdar://8018252 void f0() { extern void f0_1(int*); register int x; f0_1(&x); } namespace test1 { template <class T> void bar(T &x) { T::fail(); } template <class T> void bar(volatile T &x) {} void test_ints() { volatile int x; bar(x = 5); bar(x += 5); } enum E { E_zero }; void test_enums() { volatile E x; bar(x = E_zero); bar(x += E_zero); // expected-error {{incompatible type}} } } int test2(int x) { return x && 4; // expected-warning {{use of logical '&&' with constant operand}} \ // expected-note {{use '&' for a bitwise operation}} \ // expected-note {{remove constant to silence this warning}} return x && sizeof(int) == 4; // no warning, RHS is logical op. return x && true; return x && false; return x || true; return x || false; return x && (unsigned)0; // expected-warning {{use of logical '&&' with constant operand}} \ // expected-note {{use '&' for a bitwise operation}} \ // expected-note {{remove constant to silence this warning}} return x || (unsigned)1; // expected-warning {{use of logical '||' with constant operand}} \ // expected-note {{use '|' for a bitwise operation}} return x || 0; // expected-warning {{use of logical '||' with constant operand}} \ // expected-note {{use '|' for a bitwise operation}} return x || 1; // expected-warning {{use of logical '||' with constant operand}} \ // expected-note {{use '|' for a bitwise operation}} return x || -1; // expected-warning {{use of logical '||' with constant operand}} \ // expected-note {{use '|' for a bitwise operation}} return x || 5; // expected-warning {{use of logical '||' with constant operand}} \ // expected-note {{use '|' for a bitwise operation}} return x && 0; // expected-warning {{use of logical '&&' with constant operand}} \ // expected-note {{use '&' for a bitwise operation}} \ // expected-note {{remove constant to silence this warning}} return x && 1; // expected-warning {{use of logical '&&' with constant operand}} \ // expected-note {{use '&' for a bitwise operation}} \ // expected-note {{remove constant to silence this warning}} return x && -1; // expected-warning {{use of logical '&&' with constant operand}} \ // expected-note {{use '&' for a bitwise operation}} \ // expected-note {{remove constant to silence this warning}} return x && 5; // expected-warning {{use of logical '&&' with constant operand}} \ // expected-note {{use '&' for a bitwise operation}} \ // expected-note {{remove constant to silence this warning}} return x || (0); // expected-warning {{use of logical '||' with constant operand}} \ // expected-note {{use '|' for a bitwise operation}} return x || (1); // expected-warning {{use of logical '||' with constant operand}} \ // expected-note {{use '|' for a bitwise operation}} return x || (-1); // expected-warning {{use of logical '||' with constant operand}} \ // expected-note {{use '|' for a bitwise operation}} return x || (5); // expected-warning {{use of logical '||' with constant operand}} \ // expected-note {{use '|' for a bitwise operation}} return x && (0); // expected-warning {{use of logical '&&' with constant operand}} \ // expected-note {{use '&' for a bitwise operation}} \ // expected-note {{remove constant to silence this warning}} return x && (1); // expected-warning {{use of logical '&&' with constant operand}} \ // expected-note {{use '&' for a bitwise operation}} \ // expected-note {{remove constant to silence this warning}} return x && (-1); // expected-warning {{use of logical '&&' with constant operand}} \ // expected-note {{use '&' for a bitwise operation}} \ // expected-note {{remove constant to silence this warning}} return x && (5); // expected-warning {{use of logical '&&' with constant operand}} \ // expected-note {{use '&' for a bitwise operation}} \ // expected-note {{remove constant to silence this warning}} } template<unsigned int A, unsigned int B> struct S { enum { e1 = A && B, e2 = A && 7 // expected-warning {{use of logical '&&' with constant operand}} \ // expected-note {{use '&' for a bitwise operation}} \ // expected-note {{remove constant to silence this warning}} }; int foo() { int x = A && B; int y = B && 3; // expected-warning {{use of logical '&&' with constant operand}} \ // expected-note {{use '&' for a bitwise operation}} \ // expected-note {{remove constant to silence this warning}} return x + y; } }; void test3() { S<5, 8> s1; S<2, 7> s2; (void)s1.foo(); (void)s2.foo(); } namespace pr16992 { typedef int T; unsigned getsz() { return (sizeof T()); } } void test4() { #define X 0 #define Y 1 bool r1 = X || Y; #define Y2 2 bool r2 = X || Y2; // expected-warning {{use of logical '||' with constant operand}} \ // expected-note {{use '|' for a bitwise operation}} }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/aggregate-initialization.cpp
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s // Verify that using an initializer list for a non-aggregate looks for // constructors.. // Note that due to a (likely) standard bug, this is technically an aggregate, // but we do not treat it as one. struct NonAggr1 { // expected-note 2 {{candidate constructor}} NonAggr1(int, int) { } // expected-note {{candidate constructor}} int m; }; struct Base { }; struct NonAggr2 : public Base { // expected-note 3 {{candidate constructor}} int m; }; class NonAggr3 { // expected-note 3 {{candidate constructor}} int m; }; struct NonAggr4 { // expected-note 3 {{candidate constructor}} int m; virtual void f(); }; NonAggr1 na1 = { 17 }; // expected-error{{no matching constructor for initialization of 'NonAggr1'}} NonAggr2 na2 = { 17 }; // expected-error{{no matching constructor for initialization of 'NonAggr2'}} NonAggr3 na3 = { 17 }; // expected-error{{no matching constructor for initialization of 'NonAggr3'}} NonAggr4 na4 = { 17 }; // expected-error{{no matching constructor for initialization of 'NonAggr4'}} // PR5817 typedef int type[][2]; const type foo = {0}; // Vector initialization. typedef short __v4hi __attribute__ ((__vector_size__ (8))); __v4hi v1 = { (void *)1, 2, 3 }; // expected-error {{cannot initialize a vector element of type 'short' with an rvalue of type 'void *'}} // Array initialization. int a[] = { (void *)1 }; // expected-error {{cannot initialize an array element of type 'int' with an rvalue of type 'void *'}} // Struct initialization. struct S { int a; } s = { (void *)1 }; // expected-error {{cannot initialize a member subobject of type 'int' with an rvalue of type 'void *'}} // Check that we're copy-initializing the structs. struct A { A(); A(int); ~A(); A(const A&) = delete; // expected-note 2 {{'A' has been explicitly marked deleted here}} }; struct B { A a; }; struct C { const A& a; }; void f() { A as1[1] = { }; A as2[1] = { 1 }; // expected-error {{copying array element of type 'A' invokes deleted constructor}} B b1 = { }; B b2 = { 1 }; // expected-error {{copying member subobject of type 'A' invokes deleted constructor}} C c1 = { 1 }; } class Agg { public: int i, j; }; class AggAgg { public: Agg agg1; Agg agg2; }; AggAgg aggagg = { 1, 2, 3, 4 };
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/function-pointer-arguments.cpp
//RUN: %clang_cc1 -fsyntax-only -verify %s namespace PR16570 { int f1(int, int); int f2(const int, int); int f3(int&, int); int f4(const int&, int); void good() { int(*g1)(int, int) = f1; int(*g2)(const int, int) = f1; int(*g3)(volatile int, int) = f1; int(*g4)(int, int) = f2; int(*g5)(const int, int) = f2; int(*g6)(volatile int, int) = f2; int(*g7)(int&, int) = f3; int(*g8)(const int&, int) = f4; } void bad() { void (*g1)(int, int) = f1; // expected-error@-1 {{different return type ('void' vs 'int'}} const int (*g2)(int, int) = f1; // expected-error@-1 {{different return type ('const int' vs 'int')}} int (*g3)(char, int) = f1; // expected-error@-1 {{type mismatch at 1st parameter ('char' vs 'int')}} int (*g4)(int, char) = f1; // expected-error@-1 {{type mismatch at 2nd parameter ('char' vs 'int')}} int (*g5)(int) = f1; // expected-error@-1 {{different number of parameters (1 vs 2)}} int (*g6)(int, int, int) = f1; // expected-error@-1 {{different number of parameters (3 vs 2)}} int (*g7)(const int, char) = f1; // expected-error@-1 {{type mismatch at 2nd parameter ('char' vs 'int')}} int (*g8)(int, char) = f2; // expected-error@-1 {{type mismatch at 2nd parameter ('char' vs 'int')}} int (*g9)(const int&, char) = f3; // expected-error@-1 {{type mismatch at 1st parameter ('const int &' vs 'int &')}} int (*g10)(int&, char) = f4; // expected-error@-1 {{type mismatch at 1st parameter ('int &' vs 'const int &')}} } typedef void (*F)(const char * __restrict__, int); void g(const char *, unsigned); F f = g; // expected-error@-1 {{type mismatch at 2nd parameter ('int' vs 'unsigned int')}} }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/cxx0x-initializer-stdinitializerlist.cpp
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s // This must obviously come before the definition of std::initializer_list. void missing_initializerlist() { auto l = {1, 2, 3, 4}; // expected-error {{std::initializer_list was not found}} } namespace std { typedef decltype(sizeof(int)) size_t; // libc++'s implementation template <class _E> class initializer_list { const _E* __begin_; size_t __size_; initializer_list(const _E* __b, size_t __s) : __begin_(__b), __size_(__s) {} public: typedef _E value_type; typedef const _E& reference; typedef const _E& const_reference; typedef size_t size_type; typedef const _E* iterator; typedef const _E* const_iterator; initializer_list() : __begin_(nullptr), __size_(0) {} size_t size() const {return __size_;} const _E* begin() const {return __begin_;} const _E* end() const {return __begin_ + __size_;} }; } template <typename T, typename U> struct same_type { static const bool value = false; }; template <typename T> struct same_type<T, T> { static const bool value = true; }; struct one { char c[1]; }; struct two { char c[2]; }; struct A { int a, b; }; struct B { B(); B(int, int); }; void simple_list() { std::initializer_list<int> il = { 1, 2, 3 }; std::initializer_list<double> dl = { 1.0, 2.0, 3 }; std::initializer_list<A> al = { {1, 2}, {2, 3}, {3, 4} }; std::initializer_list<B> bl = { {1, 2}, {2, 3}, {} }; } void function_call() { void f(std::initializer_list<int>); f({1, 2, 3}); void g(std::initializer_list<B>); g({ {1, 2}, {2, 3}, {} }); } struct C { C(int); }; struct D { D(); operator int(); operator C(); }; void overloaded_call() { one overloaded(std::initializer_list<int>); two overloaded(std::initializer_list<B>); static_assert(sizeof(overloaded({1, 2, 3})) == sizeof(one), "bad overload"); static_assert(sizeof(overloaded({ {1, 2}, {2, 3}, {} })) == sizeof(two), "bad overload"); void ambiguous(std::initializer_list<A>); // expected-note {{candidate}} void ambiguous(std::initializer_list<B>); // expected-note {{candidate}} ambiguous({ {1, 2}, {2, 3}, {3, 4} }); // expected-error {{ambiguous}} one ov2(std::initializer_list<int>); // expected-note {{candidate}} two ov2(std::initializer_list<C>); // expected-note {{candidate}} // Worst sequence to int is identity, whereas to C it's user-defined. static_assert(sizeof(ov2({1, 2, 3})) == sizeof(one), "bad overload"); // But here, user-defined is worst in both cases. ov2({1, 2, D()}); // expected-error {{ambiguous}} } template <typename T> T deduce(std::initializer_list<T>); // expected-note {{conflicting types for parameter 'T' ('int' vs. 'double')}} template <typename T> T deduce_ref(const std::initializer_list<T>&); // expected-note {{conflicting types for parameter 'T' ('int' vs. 'double')}} void argument_deduction() { static_assert(same_type<decltype(deduce({1, 2, 3})), int>::value, "bad deduction"); static_assert(same_type<decltype(deduce({1.0, 2.0, 3.0})), double>::value, "bad deduction"); deduce({1, 2.0}); // expected-error {{no matching function}} static_assert(same_type<decltype(deduce_ref({1, 2, 3})), int>::value, "bad deduction"); static_assert(same_type<decltype(deduce_ref({1.0, 2.0, 3.0})), double>::value, "bad deduction"); deduce_ref({1, 2.0}); // expected-error {{no matching function}} } void auto_deduction() { auto l = {1, 2, 3, 4}; auto l2 {1, 2, 3, 4}; // expected-warning {{will change meaning in a future version of Clang}} static_assert(same_type<decltype(l), std::initializer_list<int>>::value, ""); auto bl = {1, 2.0}; // expected-error {{cannot deduce}} for (int i : {1, 2, 3, 4}) {} } void dangle() { new auto{1, 2, 3}; // expected-error {{cannot use list-initialization}} new std::initializer_list<int>{1, 2, 3}; // expected-warning {{at the end of the full-expression}} } struct haslist1 { std::initializer_list<int> il = {1, 2, 3}; // expected-warning{{at the end of the constructor}} std::initializer_list<int> jl{1, 2, 3}; // expected-warning{{at the end of the constructor}} haslist1(); }; haslist1::haslist1() : il{1, 2, 3} // expected-warning{{at the end of the constructor}} {} namespace PR12119 { // Deduction with nested initializer lists. template<typename T> void f(std::initializer_list<T>); template<typename T> void g(std::initializer_list<std::initializer_list<T>>); void foo() { f({0, {1}}); // expected-warning{{braces around scalar initializer}} g({{0, 1}, {2, 3}}); std::initializer_list<int> il = {1, 2}; g({il, {2, 3}}); } } namespace Decay { template<typename T> void f(std::initializer_list<T>) { T x = 1; // expected-error{{cannot initialize a variable of type 'const char *' with an rvalue of type 'int'}} } void g() { f({"A", "BB", "CCC"}); // expected-note{{in instantiation of function template specialization 'Decay::f<const char *>' requested here}} auto x = { "A", "BB", "CCC" }; std::initializer_list<const char *> *il = &x; for( auto s : {"A", "BB", "CCC", "DDD"}) { } } } namespace PR12436 { struct X { template<typename T> X(std::initializer_list<int>, T); }; X x({}, 17); } namespace rdar11948732 { template<typename T> struct X {}; struct XCtorInit { XCtorInit(std::initializer_list<X<int>>); }; void f(X<int> &xi) { XCtorInit xc = { xi, xi }; } } namespace PR14272 { auto x { { 0, 0 } }; // expected-error {{cannot deduce actual type for variable 'x' with type 'auto' from initializer list}} } namespace initlist_of_array { void f(std::initializer_list<int[2]>) {} void f(std::initializer_list<int[2][2]>) = delete; void h() { f({{1,2},{3,4}}); } } namespace init_list_deduction_failure { void f(); void f(int); template<typename T> void g(std::initializer_list<T>); // expected-note@-1 {{candidate template ignored: couldn't resolve reference to overloaded function 'f'}} void h() { g({f}); } // expected-error@-1 {{no matching function for call to 'g'}} } namespace deleted_copy { struct X { X(int i) {} X(const X& x) = delete; // expected-note {{here}} void operator=(const X& x) = delete; }; std::initializer_list<X> x{1}; // expected-error {{invokes deleted constructor}} } namespace RefVersusInitList { struct S {}; void f(const S &) = delete; void f(std::initializer_list<S>); void g(S s) { f({S()}); } } namespace PR18013 { int f(); std::initializer_list<long (*)()> x = {f}; // expected-error {{cannot initialize an array element of type 'long (*const)()' with an lvalue of type 'int ()': different return type ('long' vs 'int')}} } namespace DR1070 { struct S { S(std::initializer_list<int>); }; S s[3] = { {1, 2, 3}, {4, 5} }; // ok S *p = new S[3] { {1, 2, 3}, {4, 5} }; // ok } namespace ListInitInstantiate { struct A { A(std::initializer_list<A>); A(std::initializer_list<int>); }; struct B : A { B(int); }; template<typename T> struct X { X(); A a; }; template<typename T> X<T>::X() : a{B{0}, B{1}} {} X<int> x; int f(const A&); template<typename T> void g() { int k = f({0}); } template void g<int>(); } namespace TemporaryInitListSourceRange_PR22367 { struct A { constexpr A() {} A(std::initializer_list<int>); // expected-note {{here}} }; constexpr int f(A) { return 0; } constexpr int k = f( // expected-error {{must be initialized by a constant expression}} // The point of this test is to check that the caret points to // 'std::initializer_list', not to '{0}'. std::initializer_list // expected-note {{constructor}} <int> {0} ); } namespace ParameterPackNestedInitializerLists_PR23904c3 { template <typename ...T> void f(std::initializer_list<std::initializer_list<T>> ...tt); void foo() { f({{0}}, {{'\0'}}); } }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/warn-unused-private-field.cpp
// RUN: %clang_cc1 -fsyntax-only -Wunused-private-field -Wused-but-marked-unused -Wno-uninitialized -verify -std=c++11 %s class NotFullyDefined { public: NotFullyDefined(); private: int y; }; class HasUndefinedNestedClass { class Undefined; int unused_; }; class HasUndefinedPureVirtualDestructor { virtual ~HasUndefinedPureVirtualDestructor() = 0; int unused_; }; class HasDefinedNestedClasses { class DefinedHere {}; class DefinedOutside; int unused_; // expected-warning{{private field 'unused_' is not used}} }; class HasDefinedNestedClasses::DefinedOutside {}; class HasUndefinedFriendFunction { friend void undefinedFriendFunction(); int unused_; }; class HasUndefinedFriendClass { friend class NotFullyDefined; friend class NotDefined; int unused_; }; class HasFriend { friend class FriendClass; friend void friendFunction(HasFriend f); int unused_; // expected-warning{{private field 'unused_' is not used}} int used_by_friend_class_; int used_by_friend_function_; }; class ClassWithTemplateFriend { template <typename T> friend class TemplateFriend; int used_by_friend_; int unused_; }; template <typename T> class TemplateFriend { public: TemplateFriend(ClassWithTemplateFriend my_friend) { int var = my_friend.used_by_friend_; } }; class FriendClass { HasFriend my_friend_; void use() { my_friend_.used_by_friend_class_ = 42; } }; void friendFunction(HasFriend my_friend) { my_friend.used_by_friend_function_ = 42; } class NonTrivialConstructor { public: NonTrivialConstructor() {} }; class NonTrivialDestructor { public: ~NonTrivialDestructor() {} }; class Trivial { public: Trivial() = default; Trivial(int a) {} }; int side_effect() { return 42; } class A { public: A() : primitive_type_(42), default_initializer_(), other_initializer_(42), trivial_(), user_constructor_(42), initialized_with_side_effect_(side_effect()) { used_ = 42; attr_used_ = 42; // expected-warning{{'attr_used_' was marked unused but was used}} } A(int x, A* a) : pointer_(a) {} private: int primitive_type_; // expected-warning{{private field 'primitive_type_' is not used}} A* pointer_; // expected-warning{{private field 'pointer_' is not used}} int no_initializer_; // expected-warning{{private field 'no_initializer_' is not used}} int default_initializer_; // expected-warning{{private field 'default_initializer_' is not used}} int other_initializer_; // expected-warning{{private field 'other_initializer_' is not used}} int used_, unused_; // expected-warning{{private field 'unused_' is not used}} int in_class_initializer_ = 42; // expected-warning{{private field 'in_class_initializer_' is not used}} int in_class_initializer_with_side_effect_ = side_effect(); Trivial trivial_initializer_ = Trivial(); // expected-warning{{private field 'trivial_initializer_' is not used}} Trivial non_trivial_initializer_ = Trivial(42); int initialized_with_side_effect_; static int static_fields_are_ignored_; Trivial trivial_; // expected-warning{{private field 'trivial_' is not used}} Trivial user_constructor_; NonTrivialConstructor non_trivial_constructor_; NonTrivialDestructor non_trivial_destructor_; int attr_ __attribute__((unused)); int attr_used_ __attribute__((unused)); }; class EverythingUsed { public: EverythingUsed() : as_array_index_(0), var_(by_initializer_) { var_ = sizeof(sizeof_); int *use = &by_reference_; int test[2]; test[as_array_index_] = 42; } template<class T> void useStuff(T t) { by_template_function_ = 42; } private: int var_; int sizeof_; int by_reference_; int by_template_function_; int as_array_index_; int by_initializer_; }; class HasFeatureTest { #if __has_feature(attribute_unused_on_fields) int unused_; // expected-warning{{private field 'unused_' is not used}} int unused2_ __attribute__((unused)); // no-warning #endif }; namespace templates { class B { template <typename T> void f(T t); int a; }; } // namespace templates namespace mutual_friends { // Undefined methods make mutual friends undefined. class A { int a; friend class B; void doSomethingToAOrB(); }; class B { int b; friend class A; }; // Undefined friends do not make a mutual friend undefined. class C { int c; void doSomethingElse() {} friend class E; friend class D; }; class D { int d; // expected-warning{{private field 'd' is not used}} friend class C; }; // Undefined nested classes make mutual friends undefined. class F { int f; class G; friend class H; }; class H { int h; friend class F; }; } // namespace mutual_friends namespace anonymous_structs_unions { class A { private: // FIXME: Look at the DeclContext for anonymous structs/unions. union { int *Aligner; unsigned char Data[8]; }; }; union S { private: int *Aligner; unsigned char Data[8]; }; } // namespace anonymous_structs_unions namespace pr13413 { class A { A() : p_(__null), b_(false), a_(this), p2_(nullptr) {} void* p_; // expected-warning{{private field 'p_' is not used}} bool b_; // expected-warning{{private field 'b_' is not used}} A* a_; // expected-warning{{private field 'a_' is not used}} void* p2_; // expected-warning{{private field 'p2_' is not used}} }; } namespace pr13543 { void f(int); void f(char); struct S { S() : p(&f) {} private: void (*p)(int); // expected-warning{{private field 'p' is not used}} }; struct A { int n; }; struct B { B() : a(A()) {} B(char) {} B(int n) : a{n}, b{(f(n), 0)} {} private: A a = A(); // expected-warning{{private field 'a' is not used}} A b; }; struct X { ~X(); }; class C { X x[4]; // no-warning }; }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/libstdcxx_map_base_hack.cpp
// RUN: %clang_cc1 -fsyntax-only %s // libstdc++ 4.2.x contains a bug where a friend struct template // declaration for std::tr1::__detail::_Map base has different // template arguments than the real declaration. Clang has an // egregious hack to work around this problem, since we can't modify // all of the world's libstdc++'s. namespace std { namespace tr1 { namespace __detail { template<typename _Key, typename _Value, typename _Ex, bool __unique, typename _Hashtable> struct _Map_base { }; } } } namespace std { namespace tr1 { template<typename T> struct X1 { template<typename _Key2, typename _Pair, typename _Hashtable> friend struct __detail::_Map_base; }; } } std::tr1::X1<int> x1i;
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/reinterpret-fn-obj-pedantic.cpp
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 -pedantic %s void fnptrs() { typedef void (*fnptr)(); fnptr fp = 0; void *vp = reinterpret_cast<void*>(fp); // expected-warning {{cast between pointer-to-function and pointer-to-object is an extension}} (void)reinterpret_cast<fnptr>(vp); // expected-warning {{cast between pointer-to-function and pointer-to-object is an extension}} }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/comma.cpp
// RUN: %clang_cc1 -fsyntax-only -verify %s // expected-no-diagnostics // PR6076 void f(); void (&g)() = (void(), f); int a[1]; int (&b)[1] = (void(), a);
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/nullability-declspec.cpp
// RUN: %clang_cc1 -fblocks -Werror=nullability-declspec -verify %s struct X { }; _Nullable int *ip1; // expected-error{{nullability specifier '_Nullable' cannot be applied to non-pointer type 'int'; did you mean to apply the specifier to the pointer?}} _Nullable int (*fp1)(int); // expected-error{{nullability specifier '_Nullable' cannot be applied to non-pointer type 'int'; did you mean to apply the specifier to the function pointer?}} _Nonnull int (^bp1)(int); // expected-error{{nullability specifier '_Nonnull' cannot be applied to non-pointer type 'int'; did you mean to apply the specifier to the block pointer?}} _Nonnull int X::*pmd1; // expected-error{{nullability specifier '_Nonnull' cannot be applied to non-pointer type 'int'; did you mean to apply the specifier to the member pointer?}} _Nonnull int (X::*pmf1)(int); // expected-error{{nullability specifier '_Nonnull' cannot be applied to non-pointer type 'int'; did you mean to apply the specifier to the member function pointer?}}
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/explicit.cpp
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s namespace Constructor { struct A { A(int); }; struct B { // expected-note+ {{candidate}} explicit B(int); }; B::B(int) { } // expected-note+ {{here}} struct C { void f(const A&); void f(const B&); }; void f(C c) { c.f(10); } A a0 = 0; A a1(0); A &&a2 = 0; A &&a3(0); A a4{0}; A &&a5 = {0}; A &&a6{0}; B b0 = 0; // expected-error {{no viable conversion}} B b1(0); B &&b2 = 0; // expected-error {{could not bind}} B &&b3(0); // expected-error {{could not bind}} B b4{0}; B &&b5 = {0}; // expected-error {{chosen constructor is explicit}} B &&b6{0}; } namespace Conversion { struct A { operator int(); explicit operator bool(); }; A::operator bool() { return false; } struct B { void f(int); void f(bool); }; void f(A a, B b) { b.f(a); } void testExplicit() { // Taken from 12.3.2p2 class X { X(); }; class Y { }; // expected-note+ {{candidate constructor (the implicit}} struct Z { explicit operator X() const; explicit operator Y() const; explicit operator int() const; }; Z z; // 13.3.1.4p1 & 8.5p16: Y y2 = z; // expected-error {{no viable conversion from 'Z' to 'Y'}} Y y2b(z); Y y3 = (Y)z; Y y4 = Y(z); Y y5 = static_cast<Y>(z); // 13.3.1.5p1 & 8.5p16: int i1 = (int)z; int i2 = int(z); int i3 = static_cast<int>(z); int i4(z); // 13.3.1.6p1 & 8.5.3p5: const Y& y6 = z; // expected-error {{no viable conversion from 'Z' to 'const Y'}} const int& y7 = z; // expected-error {{no viable conversion from 'Z' to 'const int'}} const Y& y8(z); const int& y9(z); // Y is an aggregate, so aggregate-initialization is performed and the // conversion function is not considered. const Y y10{z}; // expected-error {{excess elements}} const Y& y11{z}; // expected-error {{excess elements}} expected-note {{in initialization of temporary of type 'const Y'}} const int& y12{z}; // X is not an aggregate, so constructors are considered, // per 13.3.3.1/4 & DR1467. const X x1{z}; const X& x2{z}; } void testBool() { struct Bool { operator bool(); }; struct NotBool { explicit operator bool(); // expected-note {{conversion to integral type 'bool'}} }; Bool b; NotBool n; (void) (1 + b); (void) (1 + n); // expected-error {{invalid operands to binary expression ('int' and 'NotBool')}} // 5.3.1p9: (void) (!b); (void) (!n); // 5.14p1: (void) (b && true); (void) (n && true); // 5.15p1: (void) (b || true); (void) (n || true); // 5.16p1: (void) (b ? 0 : 1); (void) (n ? 0: 1); // 5.19p5: // TODO: After constexpr has been implemented // 6.4p4: if (b) {} if (n) {} // 6.4.2p2: switch (b) {} // expected-warning {{switch condition has boolean value}} switch (n) {} // expected-error {{switch condition type 'NotBool' requires explicit conversion to 'bool'}} \ expected-warning {{switch condition has boolean value}} // 6.5.1: while (b) {} while (n) {} // 6.5.2p1: do {} while (b); do {} while (n); // 6.5.3: for (;b;) {} for (;n;) {} // 13.3.1.5p1: bool direct1(b); bool direct2(n); int direct3(b); int direct4(n); // expected-error {{no viable conversion}} const bool &direct5(b); const bool &direct6(n); const int &direct7(b); const int &direct8(n); // expected-error {{no viable conversion}} bool directList1{b}; bool directList2{n}; int directList3{b}; int directList4{n}; // expected-error {{no viable conversion}} const bool &directList5{b}; const bool &directList6{n}; const int &directList7{b}; const int &directList8{n}; // expected-error {{no viable conversion}} bool copy1 = b; bool copy2 = n; // expected-error {{no viable conversion}} int copy3 = b; int copy4 = n; // expected-error {{no viable conversion}} const bool &copy5 = b; const bool &copy6 = n; // expected-error {{no viable conversion}} const int &copy7 = b; const int &copy8 = n; // expected-error {{no viable conversion}} bool copyList1 = {b}; bool copyList2 = {n}; // expected-error {{no viable conversion}} int copyList3 = {b}; int copyList4 = {n}; // expected-error {{no viable conversion}} const bool &copyList5 = {b}; const bool &copyList6 = {n}; // expected-error {{no viable conversion}} const int &copyList7 = {b}; const int &copyList8 = {n}; // expected-error {{no viable conversion}} } void testNew() { // 5.3.4p6: struct Int { operator int(); }; struct NotInt { explicit operator int(); // expected-note {{conversion to integral type 'int' declared here}} }; Int i; NotInt ni; new int[i]; new int[ni]; // expected-error {{array size expression of type 'NotInt' requires explicit conversion to type 'int'}} } void testDelete() { // 5.3.5pp2: struct Ptr { operator int*(); }; struct NotPtr { explicit operator int*(); // expected-note {{conversion}} }; Ptr p; NotPtr np; delete p; delete np; // expected-error {{converting delete expression from type 'NotPtr' to type 'int *' invokes an explicit conversion function}} } void testFunctionPointer() { // 13.3.1.1.2p2: using Func = void(*)(int); struct FP { operator Func(); }; struct NotFP { explicit operator Func(); }; FP fp; NotFP nfp; fp(1); nfp(1); // expected-error {{type 'NotFP' does not provide a call operator}} } } namespace pr8264 { struct Test { explicit explicit Test(int x); // expected-warning{{duplicate 'explicit' declaration specifier}} }; } namespace PR18777 { struct S { explicit operator bool() const; } s; int *p = new int(s); // expected-error {{no viable conversion}} }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/default-assignment-operator.cpp
// RUN: %clang_cc1 -fsyntax-only -verify %s class Base { // expected-error {{cannot define the implicit copy assignment operator for 'Base', because non-static reference member 'ref' can't use copy assignment operator}} \ // expected-warning{{class 'Base' does not declare any constructor to initialize its non-modifiable members}} int &ref; // expected-note {{declared here}} \ // expected-note{{reference member 'ref' will never be initialized}} }; class X : Base { // // expected-error {{cannot define the implicit copy assignment operator for 'X', because non-static const member 'cint' can't use copy assignment operator}} \ // expected-note{{assignment operator for 'Base' first required here}} public: X(); const int cint; // expected-note {{declared here}} }; struct Y : X { Y(); Y& operator=(const Y&); Y& operator=(volatile Y&); Y& operator=(const volatile Y&); Y& operator=(Y&); }; class Z : Y {}; Z z1; Z z2; // Test1 void f(X x, const X cx) { x = cx; // expected-note{{assignment operator for 'X' first required here}} x = cx; z1 = z2; } // Test2 class T {}; T t1; T t2; void g() { t1 = t2; } // Test3 class V { public: V(); V &operator = (V &b); }; class W : V {}; W w1, w2; void h() { w1 = w2; } // Test4 class B1 { public: B1(); B1 &operator = (B1 b); }; class D1 : B1 {}; D1 d1, d2; void i() { d1 = d2; } // Test5 class E1 { // expected-error{{cannot define the implicit copy assignment operator for 'E1', because non-static const member 'a' can't use copy assignment operator}} public: const int a; // expected-note{{declared here}} E1() : a(0) {} }; E1 e1, e2; void j() { e1 = e2; // expected-note{{assignment operator for 'E1' first required here}} } namespace ProtectedCheck { struct X { protected: X &operator=(const X&); // expected-note{{declared protected here}} }; struct Y : public X { }; void f(Y y) { y = y; } struct Z { // expected-error{{'operator=' is a protected member of 'ProtectedCheck::X'}} X x; }; void f(Z z) { z = z; } // expected-note{{implicit copy assignment operator}} } namespace MultiplePaths { struct X0 { X0 &operator=(const X0&); }; struct X1 : public virtual X0 { }; struct X2 : X0, X1 { }; // expected-warning{{direct base 'MultiplePaths::X0' is inaccessible due to ambiguity:\n struct MultiplePaths::X2 -> struct MultiplePaths::X0\n struct MultiplePaths::X2 -> struct MultiplePaths::X1 -> struct MultiplePaths::X0}} void f(X2 x2) { x2 = x2; } }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/wchar_t.cpp
// RUN: %clang_cc1 -fsyntax-only -pedantic -verify %s wchar_t x; void f(wchar_t p) { wchar_t x; unsigned wchar_t y; // expected-warning {{'wchar_t' cannot be signed or unsigned}} signed wchar_t z; // expected-warning {{'wchar_t' cannot be signed or unsigned}} ++x; } // PR4502 wchar_t const c = L'c'; int a[c == L'c' ? 1 : -1]; // PR5917 template<typename _CharT> struct basic_string { }; template<typename _CharT> basic_string<_CharT> operator+ (const basic_string<_CharT>&, _CharT); int t(void) { basic_string<wchar_t>() + L'-'; return (0); } // rdar://8040728 wchar_t in[] = L"\x434" "\x434"; // No warning
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/align_value.cpp
// RUN: %clang_cc1 -fsyntax-only -verify %s typedef double * __attribute__((align_value(64))) aligned_double; void foo(aligned_double x, double * y __attribute__((align_value(32))), double & z __attribute__((align_value(128)))) { }; template <typename T, int Q> struct x { typedef T* aligned_int __attribute__((align_value(32+8*Q))); aligned_int V; void foo(aligned_int a, T &b __attribute__((align_value(sizeof(T)*4)))); }; x<float, 4> y; template <typename T, int Q> struct nope { // expected-error@+1 {{requested alignment is not a power of 2}} void foo(T &b __attribute__((align_value(sizeof(T)+1)))); }; // expected-note@+1 {{in instantiation of template class 'nope<long double, 4>' requested here}} nope<long double, 4> y2;
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/funcdname.cpp
// RUN: %clang_cc1 -std=c++1y -triple i386-pc-win32 -fms-compatibility -fms-extensions -fsyntax-only -verify %s // expected-no-diagnostics int foo() { static_assert(sizeof(__FUNCDNAME__) == 12, "?foo@@YAHXZ"); return 0; } // Within templates. template <typename T> int baz() { static_assert(sizeof(__FUNCDNAME__) == 16, "??$baz@H@@YAHXZ"); return 0; } struct A { A() { static_assert(sizeof(__FUNCDNAME__) == 13, "??0A@@QAE@XZ"); } ~A() { static_assert(sizeof(__FUNCDNAME__) == 13, "??1A@@QAE@XZ"); } }; int main() { static_assert(sizeof(__FUNCDNAME__) == 5, "main"); baz<int>(); return 0; }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/auto-cxx98.cpp
// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++98 -Wc++11-compat void f() { auto int a; // expected-warning {{'auto' storage class specifier is redundant and incompatible with C++11}} int auto b; // expected-warning {{'auto' storage class specifier is redundant and incompatible with C++11}} auto c; // expected-warning {{C++11 extension}} expected-error {{requires an initializer}} static auto d = 0; // expected-warning {{C++11 extension}} auto static e = 0; // expected-warning {{C++11 extension}} }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/attr-no-sanitize-memory.cpp
// RUN: %clang_cc1 -fsyntax-only -verify %s #define NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory)) #if !__has_attribute(no_sanitize_memory) #error "Should support no_sanitize_memory" #endif void noanal_fun() NO_SANITIZE_MEMORY; void noanal_fun_args() __attribute__((no_sanitize_memory(1))); // \ // expected-error {{'no_sanitize_memory' attribute takes no arguments}} int noanal_testfn(int y) NO_SANITIZE_MEMORY; int noanal_testfn(int y) { int x NO_SANITIZE_MEMORY = y; // \ // expected-error {{'no_sanitize_memory' attribute only applies to functions}} return x; } int noanal_test_var NO_SANITIZE_MEMORY; // \ // expected-error {{'no_sanitize_memory' attribute only applies to functions}} class NoanalFoo { private: int test_field NO_SANITIZE_MEMORY; // \ // expected-error {{'no_sanitize_memory' attribute only applies to functions}} void test_method() NO_SANITIZE_MEMORY; }; class NO_SANITIZE_MEMORY NoanalTestClass { // \ // expected-error {{'no_sanitize_memory' attribute only applies to functions}} }; void noanal_fun_params(int lvar NO_SANITIZE_MEMORY); // \ // expected-error {{'no_sanitize_memory' attribute only applies to functions}}
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/warn-global-constructors.cpp
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wglobal-constructors %s -verify int opaque_int(); namespace test0 { // These should never require global constructors. int a; int b = 20; float c = 5.0f; // This global constructor is avoidable based on initialization order. int d = b; // expected-warning {{global constructor}} // These global constructors are unavoidable. int e = opaque_int(); // expected-warning {{global constructor}} int f = b; // expected-warning {{global constructor}} } namespace test1 { struct A { int x; }; A a; A b = A(); A c = { 10 }; A d = { opaque_int() }; // expected-warning {{global constructor}} A e = A(A()); A f = A(a); // expected-warning {{global constructor}} A g(a); // expected-warning {{global constructor}} A h((A())); // elided A i((A(A()))); // elided } namespace test2 { struct A { A(); }; A a; // expected-warning {{global constructor}} A b[10]; // expected-warning {{global constructor}} A c[10][10]; // expected-warning {{global constructor}} A &d = a; A &e = b[5]; A &f = c[5][7]; } namespace test3 { struct A { ~A(); }; A a; // expected-warning {{global destructor}} A b[10]; // expected-warning {{global destructor}} A c[10][10]; // expected-warning {{global destructor}} A &d = a; A &e = b[5]; A &f = c[5][7]; } namespace test4 { char a[] = "hello"; char b[6] = "hello"; char c[][6] = { "hello" }; } namespace test5 { struct A { A(); }; void f1() { static A a; } void f2() { static A& a = *new A; } } namespace test6 { struct A { ~A(); }; void f1() { static A a; } void f2() { static A& a = *new A; } } namespace pr8095 { struct Foo { int x; Foo(int x1) : x(x1) {} }; void foo() { static Foo a(0); } struct Bar { ~Bar(); }; void bar() { static Bar b; } } namespace referencemember { struct A { int &a; }; int a; A b = { a }; } namespace pr19253 { struct A { ~A() = default; }; A a; struct B { ~B(); }; struct C : B { ~C() = default; }; C c; // expected-warning {{global destructor}} class D { friend struct E; ~D() = default; }; struct E : D { D d; ~E() = default; }; E e; } namespace pr20420 { // No warning is expected. This used to crash. void *array_storage[1]; const int &global_reference = *(int *)array_storage; }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/pr13394-crash-on-invalid.cpp
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s // Don't crash (PR13394). namespace stretch_v1 { struct closure_t { const stretch_v1::ops_t* d_methods; // expected-error {{no type named 'ops_t' in namespace 'stretch_v1'}} }; } namespace gatekeeper_v1 { namespace gatekeeper_factory_v1 { struct closure_t { // expected-note {{'closure_t' declared here}} expected-note {{'gatekeeper_factory_v1::closure_t' declared here}} gatekeeper_v1::closure_t* create(); // expected-error {{no type named 'closure_t' in namespace 'gatekeeper_v1'; did you mean simply 'closure_t'?}} }; } // FIXME: Typo correction should remove the 'gatekeeper_v1::' name specifier gatekeeper_v1::closure_t *x; // expected-error {{no type named 'closure_t' in namespace 'gatekeeper_v1'; did you mean 'gatekeeper_factory_v1::closure_t'}} } namespace Foo { struct Base { void Bar() {} // expected-note{{'Bar' declared here}} }; } struct Derived : public Foo::Base { void test() { Foo::Bar(); // expected-error{{no member named 'Bar' in namespace 'Foo'; did you mean simply 'Bar'?}} } };
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/class-layout.cpp
// RUN: %clang_cc1 -triple x86_64-unknown-unknown %s -fsyntax-only -verify -std=c++98 -Wno-inaccessible-base // RUN: %clang_cc1 -triple x86_64-unknown-unknown %s -fsyntax-only -verify -std=c++11 -Wno-inaccessible-base // expected-no-diagnostics #define SA(n, p) int a##n[(p) ? 1 : -1] struct A { int a; char b; }; SA(0, sizeof(A) == 8); struct B : A { char c; }; SA(1, sizeof(B) == 12); struct C { // Make fields private so C won't be a POD type. private: int a; char b; }; SA(2, sizeof(C) == 8); struct D : C { char c; }; SA(3, sizeof(D) == 8); struct __attribute__((packed)) E { char b; int a; }; SA(4, sizeof(E) == 5); struct __attribute__((packed)) F : E { char d; }; SA(5, sizeof(F) == 6); struct G { G(); }; struct H : G { }; SA(6, sizeof(H) == 1); struct I { char b; int a; } __attribute__((packed)); SA(6_1, sizeof(I) == 5); // PR5580 namespace PR5580 { class A { bool iv0 : 1; }; SA(7, sizeof(A) == 1); class B : A { bool iv0 : 1; }; SA(8, sizeof(B) == 2); struct C { bool iv0 : 1; }; SA(9, sizeof(C) == 1); struct D : C { bool iv0 : 1; }; SA(10, sizeof(D) == 2); } namespace Test1 { // Test that we don't assert on this hierarchy. struct A { }; struct B : A { virtual void b(); }; class C : virtual A { int c; }; struct D : virtual B { }; struct E : C, virtual D { }; class F : virtual E { }; struct G : virtual E, F { }; SA(0, sizeof(G) == 24); } namespace Test2 { // Test that this somewhat complex class structure is laid out correctly. struct A { }; struct B : A { virtual void b(); }; struct C : virtual B { }; struct D : virtual A { }; struct E : virtual B, D { }; struct F : E, virtual C { }; struct G : virtual F, A { }; struct H { G g; }; SA(0, sizeof(H) == 24); } namespace PR16537 { namespace test1 { struct pod_in_11_only { private: long long x; }; struct tail_padded_pod_in_11_only { pod_in_11_only pod11; char tail_padding; }; struct might_use_tail_padding : public tail_padded_pod_in_11_only { char may_go_into_tail_padding; }; SA(0, sizeof(might_use_tail_padding) == 16); } namespace test2 { struct pod_in_11_only { private: long long x; }; struct tail_padded_pod_in_11_only { pod_in_11_only pod11 __attribute__((aligned(16))); }; struct might_use_tail_padding : public tail_padded_pod_in_11_only { char may_go_into_tail_padding; }; SA(0, sizeof(might_use_tail_padding) == 16); } namespace test3 { struct pod_in_11_only { private: long long x; }; struct tail_padded_pod_in_11_only { pod_in_11_only pod11; char tail_padding; }; struct second_base { char foo; }; struct might_use_tail_padding : public tail_padded_pod_in_11_only, public second_base { }; SA(0, sizeof(might_use_tail_padding) == 16); } namespace test4 { struct pod_in_11_only { private: long long x; }; struct tail_padded_pod_in_11_only { pod_in_11_only pod11; char tail_padding; }; struct second_base { char foo; }; struct might_use_tail_padding : public tail_padded_pod_in_11_only, public second_base { char may_go_into_tail_padding; }; SA(0, sizeof(might_use_tail_padding) == 16); } namespace test5 { struct pod_in_11_only { private: long long x; }; struct pod_in_11_only2 { private: long long x; }; struct tail_padded_pod_in_11_only { pod_in_11_only pod11; char tail_padding; }; struct second_base { pod_in_11_only2 two; char foo; }; struct might_use_tail_padding : public tail_padded_pod_in_11_only, public second_base { char may_go_into_tail_padding; }; SA(0, sizeof(might_use_tail_padding) == 32); } namespace test6 { struct pod_in_11_only { private: long long x; }; struct pod_in_11_only2 { private: long long x; }; struct tail_padded_pod_in_11_only { pod_in_11_only pod11; char tail_padding; }; struct second_base { pod_in_11_only2 two; char foo; }; struct might_use_tail_padding : public tail_padded_pod_in_11_only, public second_base { char may_go_into_tail_padding; }; SA(0, sizeof(might_use_tail_padding) == 32); } namespace test7 { struct pod_in_11_only { private: long long x; }; struct tail_padded_pod_in_11_only { pod_in_11_only pod11; pod_in_11_only pod12; char tail_padding; }; struct might_use_tail_padding : public tail_padded_pod_in_11_only { char may_go_into_tail_padding; }; SA(0, sizeof(might_use_tail_padding) == 24); } namespace test8 { struct pod_in_11_only { private: long long x; }; struct tail_padded_pod_in_11_only { pod_in_11_only pod11; char tail_padding; }; struct another_layer { tail_padded_pod_in_11_only pod; char padding; }; struct might_use_tail_padding : public another_layer { char may_go_into_tail_padding; }; SA(0, sizeof(might_use_tail_padding) == 24); } namespace test9 { struct pod_in_11_only { private: long long x; }; struct tail_padded_pod_in_11_only { pod_in_11_only pod11; char tail_padding; }; struct another_layer : tail_padded_pod_in_11_only { }; struct might_use_tail_padding : public another_layer { char may_go_into_tail_padding; }; SA(0, sizeof(might_use_tail_padding) == 16); } namespace test10 { struct pod_in_11_only { private: long long x; }; struct A { pod_in_11_only a; char apad; }; struct B { char b; }; struct C { pod_in_11_only c; char cpad; }; struct D { char d; }; struct might_use_tail_padding : public A, public B, public C, public D { }; SA(0, sizeof(might_use_tail_padding) == 32); } namespace test11 { struct pod_in_11_only { private: long long x; }; struct A { pod_in_11_only a; char apad; }; struct B { char b_pre; pod_in_11_only b; char bpad; }; struct C { char c_pre; pod_in_11_only c; char cpad; }; struct D { char d_pre; pod_in_11_only d; char dpad; }; struct might_use_tail_padding : public A, public B, public C, public D { char m; }; SA(0, sizeof(might_use_tail_padding) == 88); } namespace test12 { struct pod_in_11_only { private: long long x; }; struct A { pod_in_11_only a __attribute__((aligned(128))); }; struct B { char bpad; }; struct C { char cpad; }; struct D { char dpad; }; struct might_use_tail_padding : public A, public B, public C, public D { char m; }; SA(0, sizeof(might_use_tail_padding) == 128); } namespace test13 { struct pod_in_11_only { private: long long x; }; struct A { pod_in_11_only a; char apad; }; struct B { }; struct C { char c_pre; pod_in_11_only c; char cpad; }; struct D { }; struct might_use_tail_padding : public A, public B, public C, public D { char m; }; SA(0, sizeof(might_use_tail_padding) == 40); } namespace test14 { struct pod_in_11_only { private: long long x; }; struct A { pod_in_11_only a; char apad; }; struct might_use_tail_padding : public A { struct { int : 0; } x; }; SA(0, sizeof(might_use_tail_padding) == 16); } namespace test15 { struct pod_in_11_only { private: long long x; }; struct A { pod_in_11_only a; char apad; }; struct might_use_tail_padding : public A { struct { char a:1; char b:2; char c:2; char d:2; char e:1; } x; }; SA(0, sizeof(might_use_tail_padding) == 16); } namespace test16 { struct pod_in_11_only { private: long long x; }; struct A { pod_in_11_only a; char apad; }; struct B { char bpod; pod_in_11_only b; char bpad; }; struct C : public A, public B { }; struct D : public C { }; struct might_use_tail_padding : public D { char m; }; SA(0, sizeof(might_use_tail_padding) == 40); } namespace test17 { struct pod_in_11_only { private: long long x; }; struct A { pod_in_11_only a __attribute__((aligned(512))); }; struct B { char bpad; pod_in_11_only foo; char btail; }; struct C { char cpad; }; struct D { char dpad; }; struct might_use_tail_padding : public A, public B, public C, public D { char a; }; SA(0, sizeof(might_use_tail_padding) == 512); } namespace test18 { struct pod_in_11_only { private: long long x; }; struct A { pod_in_11_only a; char apad; }; struct B { char bpod; pod_in_11_only b; char bpad; }; struct A1 { pod_in_11_only a; char apad; }; struct B1 { char bpod; pod_in_11_only b; char bpad; }; struct C : public A, public B { }; struct D : public A1, public B1 { }; struct E : public D, public C { }; struct F : public E { }; struct might_use_tail_padding : public F { char m; }; SA(0, sizeof(might_use_tail_padding) == 80); } } // namespace PR16537
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/builtins.cpp
// RUN: %clang_cc1 %s -fsyntax-only -verify -std=c++11 typedef const struct __CFString * CFStringRef; #define CFSTR __builtin___CFStringMakeConstantString void f() { (void)CFStringRef(CFSTR("Hello")); } void a() { __builtin_va_list x, y; ::__builtin_va_copy(x, y); } // <rdar://problem/10063539> template<int (*Compare)(const char *s1, const char *s2)> int equal(const char *s1, const char *s2) { return Compare(s1, s2) == 0; } // FIXME: Our error recovery here sucks template int equal<&__builtin_strcmp>(const char*, const char*); // expected-error {{builtin functions must be directly called}} expected-error {{expected unqualified-id}} expected-error {{expected ')'}} expected-note {{to match this '('}} // PR13195 void f2() { __builtin_isnan; // expected-error {{builtin functions must be directly called}} } // pr14895 typedef __typeof(sizeof(int)) size_t; extern "C" void *__builtin_alloca (size_t); namespace addressof { struct S {} s; static_assert(__builtin_addressof(s) == &s, ""); struct T { constexpr T *operator&() const { return nullptr; } int n; } t; constexpr T *pt = __builtin_addressof(t); static_assert(&pt->n == &t.n, ""); struct U { int n : 5; } u; int *pbf = __builtin_addressof(u.n); // expected-error {{address of bit-field requested}} S *ptmp = __builtin_addressof(S{}); // expected-error {{taking the address of a temporary}} } void no_ms_builtins() { __assume(1); // expected-error {{use of undeclared}} __noop(1); // expected-error {{use of undeclared}} __debugbreak(); // expected-error {{use of undeclared}} }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/anonymous-struct.cpp
// RUN: %clang_cc1 -fsyntax-only -verify %s struct S { S(); // expected-note {{because type 'S' has a user-provided default constructor}} }; struct { // expected-error {{anonymous structs and classes must be class members}} }; struct E { struct { S x; // expected-error {{anonymous struct member 'x' has a non-trivial constructor}} }; static struct { }; }; template <class T> void foo(T); typedef struct { // expected-note {{use a tag name here to establish linkage prior to definition}} expected-note {{declared here}} void test() { foo(this); // expected-warning {{template argument uses unnamed type}} } } A; // expected-error {{unsupported: typedef changes linkage of anonymous type, but linkage was already computed}}
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/cxx0x-defaulted-functions.cpp
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify -fcxx-exceptions %s void fn() = default; // expected-error {{only special member}} struct foo { void fn() = default; // expected-error {{only special member}} foo() = default; foo(const foo&) = default; foo(foo&&) = default; foo& operator = (const foo&) = default; foo& operator = (foo&&) = default; ~foo() = default; }; struct bar { bar(); bar(const bar&); bar(bar&&); bar& operator = (const bar&); bar& operator = (bar&&); ~bar(); }; bar::bar() = default; bar::bar(const bar&) = default; bar::bar(bar&&) = default; bar& bar::operator = (const bar&) = default; bar& bar::operator = (bar&&) = default; bar::~bar() = default; static_assert(__is_trivial(foo), "foo should be trivial"); static_assert(!__has_trivial_destructor(bar), "bar's destructor isn't trivial"); static_assert(!__has_trivial_constructor(bar), "bar's default constructor isn't trivial"); static_assert(!__has_trivial_copy(bar), "bar has no trivial copy"); static_assert(!__has_trivial_assign(bar), "bar has no trivial assign"); void tester() { foo f, g(f); bar b, c(b); f = g; b = c; } template<typename T> struct S : T { constexpr S() = default; constexpr S(const S&) = default; constexpr S(S&&) = default; }; struct lit { constexpr lit() {} }; S<lit> s_lit; // ok S<bar> s_bar; // ok struct Friends { friend S<bar>::S(); friend S<bar>::S(const S&); friend S<bar>::S(S&&); }; namespace DefaultedFnExceptionSpec { // DR1330: The exception-specification of an implicitly-declared special // member function is evaluated as needed. template<typename T> T &&declval(); template<typename T> struct pair { pair(const pair&) noexcept(noexcept(T(declval<T>()))); }; struct Y; struct X { X(); X(const Y&); }; struct Y { pair<X> p; }; template<typename T> struct A { pair<T> p; }; struct B { B(); B(const A<B>&); }; // Don't crash here. void f() { X x = X(); (void)noexcept(B(declval<B>())); } template<typename T> struct Error { // FIXME: Type canonicalization causes all the errors to point at the first // declaration which has the type 'void () noexcept (T::error)'. We should // get one error for 'Error<int>::Error()' and one for 'Error<int>::~Error()'. void f() noexcept(T::error); // expected-error 2{{has no members}} Error() noexcept(T::error); Error(const Error&) noexcept(T::error); Error(Error&&) noexcept(T::error); Error &operator=(const Error&) noexcept(T::error); Error &operator=(Error&&) noexcept(T::error); ~Error() noexcept(T::error); }; struct DelayImplicit { Error<int> e; }; // Don't instantiate the exception specification here. void test1(decltype(declval<DelayImplicit>() = DelayImplicit(DelayImplicit()))); void test2(decltype(declval<DelayImplicit>() = declval<const DelayImplicit>())); void test3(decltype(DelayImplicit(declval<const DelayImplicit>()))); // Any odr-use causes the exception specification to be evaluated. struct OdrUse { // \ expected-note {{instantiation of exception specification for 'Error'}} \ expected-note {{instantiation of exception specification for '~Error'}} Error<int> e; }; OdrUse use; // expected-note {{implicit default constructor for 'DefaultedFnExceptionSpec::OdrUse' first required here}} } namespace PR13527 { struct X { X() = delete; // expected-note {{here}} X(const X&) = delete; // expected-note {{here}} X(X&&) = delete; // expected-note {{here}} X &operator=(const X&) = delete; // expected-note {{here}} X &operator=(X&&) = delete; // expected-note {{here}} ~X() = delete; // expected-note {{here}} }; X::X() = default; // expected-error {{redefinition}} X::X(const X&) = default; // expected-error {{redefinition}} X::X(X&&) = default; // expected-error {{redefinition}} X &X::operator=(const X&) = default; // expected-error {{redefinition}} X &X::operator=(X&&) = default; // expected-error {{redefinition}} X::~X() = default; // expected-error {{redefinition}} struct Y { Y() = default; Y(const Y&) = default; Y(Y&&) = default; Y &operator=(const Y&) = default; Y &operator=(Y&&) = default; ~Y() = default; }; Y::Y() = default; // expected-error {{definition of explicitly defaulted}} Y::Y(const Y&) = default; // expected-error {{definition of explicitly defaulted}} Y::Y(Y&&) = default; // expected-error {{definition of explicitly defaulted}} Y &Y::operator=(const Y&) = default; // expected-error {{definition of explicitly defaulted}} Y &Y::operator=(Y&&) = default; // expected-error {{definition of explicitly defaulted}} Y::~Y() = default; // expected-error {{definition of explicitly defaulted}} } namespace PR14577 { template<typename T> struct Outer { template<typename U> struct Inner1 { ~Inner1(); }; template<typename U> struct Inner2 { ~Inner2(); }; }; template<typename T> Outer<T>::Inner1<T>::~Inner1() = delete; // expected-error {{nested name specifier 'Outer<T>::Inner1<T>::' for declaration does not refer into a class, class template or class template partial specialization}} expected-error {{only functions can have deleted definitions}} template<typename T> Outer<T>::Inner2<T>::~Inner2() = default; // expected-error {{nested name specifier 'Outer<T>::Inner2<T>::' for declaration does not refer into a class, class template or class template partial specialization}} expected-error {{only special member functions may be defaulted}} } extern "C" { template<typename _Tp> // expected-error {{templates must have C++ linkage}} void PR13573(const _Tp&) = delete; } namespace PR15597 { template<typename T> struct A { A() noexcept(true) = default; ~A() noexcept(true) = default; }; template<typename T> struct B { B() noexcept(false) = default; // expected-error {{does not match the calculated one}} ~B() noexcept(false) = default; // expected-error {{does not match the calculated one}} }; A<int> a; B<int> b; // expected-note {{here}} }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/switch-implicit-fallthrough-per-method.cpp
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wimplicit-fallthrough-per-function %s int fallthrough(int n) { switch (n / 10) { case 0: n += 100; case 1: // expected-warning{{unannotated fall-through}} expected-note{{insert '[[clang::fallthrough]];' to silence this warning}} expected-note{{insert 'break;' to avoid fall-through}} switch (n) { case 111: n += 111; [[clang::fallthrough]]; case 112: n += 112; case 113: // expected-warning{{unannotated fall-through}} expected-note{{insert '[[clang::fallthrough]];' to silence this warning}} expected-note{{insert 'break;' to avoid fall-through}} n += 113; break ; } } return n; } int fallthrough2(int n) { switch (n / 10) { case 0: n += 100; case 1: // no warning, as we didn't "opt-in" for it in this method switch (n) { case 111: n += 111; case 112: // no warning, as we didn't "opt-in" for it in this method n += 112; case 113: // no warning, as we didn't "opt-in" for it in this method n += 113; break ; } } return n; } void unscoped(int n) { switch (n % 2) { case 0: // FIXME: This should be typo-corrected, probably. [[fallthrough]]; // expected-warning{{unknown attribute 'fallthrough' ignored}} case 2: // expected-warning{{unannotated fall-through}} expected-note{{clang::fallthrough}} expected-note{{break;}} [[clang::fallthrough]]; case 1: break; } }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/derived-to-base-ambig.cpp
// RUN: %clang_cc1 -fsyntax-only -verify %s class A { }; class B : public A { }; class C : public A { }; class D : public B, public C { }; void f(D* d) { A* a; a = d; // expected-error{{ambiguous conversion from derived class 'D' to base class 'A':}} expected-error{{assigning to 'A *' from incompatible type 'D *'}} } class Object2 { }; class A2 : public Object2 { }; class B2 : public virtual A2 { }; class C2 : virtual public A2 { }; class D2 : public B2, public C2 { }; class E2 : public D2, public C2, public virtual A2 { }; // expected-warning{{direct base 'C2' is inaccessible due to ambiguity:\n class E2 -> class D2 -> class C2\n class E2 -> class C2}} class F2 : public E2, public A2 { }; // expected-warning{{direct base 'A2' is inaccessible due to ambiguity:\n class F2 -> class E2 -> class D2 -> class B2 -> class A2\n class F2 -> class A2}} void g(E2* e2, F2* f2) { Object2* o2; o2 = e2; o2 = f2; // expected-error{{ambiguous conversion from derived class 'F2' to base class 'Object2':}} expected-error{{assigning to 'Object2 *' from incompatible type 'F2 *'}} } // Test that ambiguous/inaccessibility checking does not trigger too // early, because it should not apply during overload resolution. void overload_okay(Object2*); void overload_okay(E2*); void overload_call(F2* f2) { overload_okay(f2); }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/attr-print.cpp
// RUN: %clang_cc1 %s -ast-print -fms-extensions | FileCheck %s // CHECK: int x __attribute__((aligned(4))); int x __attribute__((aligned(4))); // FIXME: Print this at a valid location for a __declspec attr. // CHECK: int y __declspec(align(4)); __declspec(align(4)) int y; // CHECK: void foo() __attribute__((const)); void foo() __attribute__((const)); // CHECK: void bar() __attribute__((__const)); void bar() __attribute__((__const)); // FIXME: Print this with correct format and order. // CHECK: void foo1() __attribute__((pure)) __attribute__((noinline)); void foo1() __attribute__((noinline, pure)); // CHECK: typedef int Small1 __attribute__((mode(byte))); typedef int Small1 __attribute__((mode(byte))); // CHECK: int small __attribute__((mode(byte))); int small __attribute__((mode(byte))); // CHECK: int v __attribute__((visibility("hidden"))); int v __attribute__((visibility("hidden"))); // CHECK: class __attribute__((consumable("unknown"))) AttrTester1 class __attribute__((consumable(unknown))) AttrTester1 { // CHECK: void callableWhen() __attribute__((callable_when("unconsumed", "consumed"))); void callableWhen() __attribute__((callable_when("unconsumed", "consumed"))); };
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/typeid-ref.cpp
// RUN: %clang_cc1 -triple %itanium_abi_triple -emit-llvm -o - %s | FileCheck %s namespace std { class type_info; } struct X { }; void f() { // CHECK: @_ZTS1X = linkonce_odr constant // CHECK: @_ZTI1X = linkonce_odr constant (void)typeid(X&); }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/decltype.cpp
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s // PR5290 int const f0(); void f0_test() { decltype(0, f0()) i = 0; i = 0; } struct A { int a[1]; A() { } }; typedef A const AC; int &f1(int*); float &f2(int const*); void test_f2() { float &fr = f2(AC().a); } template <class T> struct Future { explicit Future(T v); template <class F> auto call(F&& fn) -> decltype(fn(T())) { return fn(T()); } template <class B, class F> auto then(F&& fn) -> decltype(call(fn)) { return fn(T()); } }; void rdar16527205() { Future<int> f1(42); f1.call([](int){ return Future<float>(0); }); } namespace pr10154 { class A{ A(decltype(nullptr) param); }; } template<typename T> struct S {}; template<typename T> auto f(T t) -> decltype(S<int>(t)) { using U = decltype(S<int>(t)); using U = S<int>; return S<int>(t); } struct B { B(decltype(undeclared)); // expected-error {{undeclared identifier}} }; struct C { C(decltype(undeclared; // expected-error {{undeclared identifier}} \ // expected-error {{expected ')'}} expected-note {{to match this '('}} }; namespace PR16529 { struct U {}; template <typename T> struct S { static decltype(T{}, U{}) &f(); }; U &r = S<int>::f(); } namespace PR18876 { struct A { ~A() = delete; }; // expected-note +{{here}} A f(); decltype(f()) *a; // ok, function call decltype(A()) *b; // expected-error {{attempt to use a deleted function}} decltype(0, f()) *c; // ok, function call on RHS of comma decltype(0, A()) *d; // expected-error {{attempt to use a deleted function}} decltype(f(), 0) *e; // expected-error {{attempt to use a deleted function}} } namespace D5789 { struct P1 { char x[6]; } g1 = { "foo" }; struct LP1 { struct P1 p1; }; // expected-warning@+3 {{subobject initialization overrides}} // expected-note@+2 {{previous initialization}} // expected-note@+1 {{previous definition}} template<class T> void foo(decltype(T(LP1{ .p1 = g1, .p1.x[1] = 'x' }))) {} // expected-warning@+3 {{subobject initialization overrides}} // expected-note@+2 {{previous initialization}} template<class T> void foo(decltype(T(LP1{ .p1 = g1, .p1.x[1] = 'r' }))) {} // okay // expected-warning@+3 {{subobject initialization overrides}} // expected-note@+2 {{previous initialization}} template<class T> void foo(decltype(T(LP1{ .p1 = { "foo" }, .p1.x[1] = 'x'}))) {} // okay // expected-warning@+3 {{subobject initialization overrides}} // expected-note@+2 {{previous initialization}} // expected-error@+1 {{redefinition of 'foo'}} template<class T> void foo(decltype(T(LP1{ .p1 = g1, .p1.x[1] = 'x' }))) {} } template<typename> class conditional { }; void foo(conditional<decltype((1),int>) { // expected-note 2 {{to match this '('}} expected-error {{expected ')'}} } // expected-error {{expected function body after function declarator}} expected-error 2 {{expected '>'}} expected-error {{expected ')'}}
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/pragma-pack.cpp
// RUN: %clang_cc1 -triple i686-apple-darwin9 -fsyntax-only -verify %s // expected-no-diagnostics namespace rdar8745206 { struct Base { int i; }; #pragma pack(push, 1) struct Sub : public Base { char c; }; #pragma pack(pop) int check[sizeof(Sub) == 5 ? 1 : -1]; } namespace check2 { struct Base { virtual ~Base(); int x; }; #pragma pack(push, 1) struct Sub : virtual Base { char c; }; #pragma pack(pop) int check[sizeof(Sub) == 13 ? 1 : -1]; } namespace llvm_support_endian { template<typename, bool> struct X; #pragma pack(push) #pragma pack(1) template<typename T> struct X<T, true> { T t; }; #pragma pack(pop) #pragma pack(push) #pragma pack(2) template<> struct X<long double, true> { long double c; }; #pragma pack(pop) int check1[__alignof(X<int, true>) == 1 ? 1 : -1]; int check2[__alignof(X<long double, true>) == 2 ? 1 : -1]; }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/constant-expression-cxx1y.cpp
// RUN: %clang_cc1 -std=c++1y -verify %s -fcxx-exceptions -triple=x86_64-linux-gnu struct S { // dummy ctor to make this a literal type constexpr S(int); S(); int arr[10]; constexpr int &get(int n) { return arr[n]; } constexpr const int &get(int n) const { return arr[n]; } }; S s = S(); const S &sr = s; static_assert(&s.get(4) - &sr.get(2) == 2, ""); // Compound-statements can be used in constexpr functions. constexpr int e() {{{{}} return 5; }} static_assert(e() == 5, ""); // Types can be defined in constexpr functions. constexpr int f() { enum E { e1, e2, e3 }; struct S { constexpr S(E e) : e(e) {} constexpr int get() { return e; } E e; }; return S(e2).get(); } static_assert(f() == 1, ""); // Variables can be declared in constexpr functions. constexpr int g(int k) { const int n = 9; int k2 = k * k; int k3 = k2 * k; return 3 * k3 + 5 * k2 + n * k - 20; } static_assert(g(2) == 42, ""); constexpr int h(int n) { static const int m = n; // expected-error {{static variable not permitted in a constexpr function}} return m; } constexpr int i(int n) { thread_local const int m = n; // expected-error {{thread_local variable not permitted in a constexpr function}} return m; } // if-statements can be used in constexpr functions. constexpr int j(int k) { if (k == 5) return 1; if (k == 1) return 5; else { if (int n = 2 * k - 4) { return n + 1; return 2; } } } // expected-note 2{{control reached end of constexpr function}} static_assert(j(0) == -3, ""); static_assert(j(1) == 5, ""); static_assert(j(2), ""); // expected-error {{constant expression}} expected-note {{in call to 'j(2)'}} static_assert(j(3) == 3, ""); static_assert(j(4) == 5, ""); static_assert(j(5) == 1, ""); // There can be 0 return-statements. constexpr void k() { } // If the return type is not 'void', no return statements => never a constant // expression, so still diagnose that case. [[noreturn]] constexpr int fn() { // expected-error {{no return statement in constexpr function}} fn(); } // We evaluate the body of a constexpr constructor, to check for side-effects. struct U { constexpr U(int n) { if (j(n)) {} // expected-note {{in call to 'j(2)'}} } }; constexpr U u1{1}; constexpr U u2{2}; // expected-error {{constant expression}} expected-note {{in call to 'U(2)'}} // We allow expression-statements. constexpr int l(bool b) { if (b) throw "invalid value for b!"; // expected-note {{subexpression not valid}} return 5; } static_assert(l(false) == 5, ""); static_assert(l(true), ""); // expected-error {{constant expression}} expected-note {{in call to 'l(true)'}} // Potential constant expression checking is still applied where possible. constexpr int htonl(int x) { // expected-error {{never produces a constant expression}} typedef unsigned char uchar; uchar arr[4] = { uchar(x >> 24), uchar(x >> 16), uchar(x >> 8), uchar(x) }; return *reinterpret_cast<int*>(arr); // expected-note {{reinterpret_cast is not allowed in a constant expression}} } constexpr int maybe_htonl(bool isBigEndian, int x) { if (isBigEndian) return x; typedef unsigned char uchar; uchar arr[4] = { uchar(x >> 24), uchar(x >> 16), uchar(x >> 8), uchar(x) }; return *reinterpret_cast<int*>(arr); // expected-note {{reinterpret_cast is not allowed in a constant expression}} } constexpr int swapped = maybe_htonl(false, 123); // expected-error {{constant expression}} expected-note {{in call}} namespace NS { constexpr int n = 0; } constexpr int namespace_alias() { namespace N = NS; return N::n; } namespace assign { constexpr int a = 0; const int b = 0; int c = 0; // expected-note {{here}} constexpr void set(const int &a, int b) { const_cast<int&>(a) = b; // expected-note 3{{constant expression cannot modify an object that is visible outside that expression}} } constexpr int wrap(int a, int b) { set(a, b); return a; } static_assert((set(a, 1), a) == 1, ""); // expected-error {{constant expression}} expected-note {{in call to 'set(a, 1)'}} static_assert((set(b, 1), b) == 1, ""); // expected-error {{constant expression}} expected-note {{in call to 'set(b, 1)'}} static_assert((set(c, 1), c) == 1, ""); // expected-error {{constant expression}} expected-note {{in call to 'set(c, 1)'}} static_assert(wrap(a, 1) == 1, ""); static_assert(wrap(b, 1) == 1, ""); static_assert(wrap(c, 1) == 1, ""); // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}} } namespace string_assign { template<typename T> constexpr void swap(T &a, T &b) { T tmp = a; a = b; b = tmp; } template<typename Iterator> constexpr void reverse(Iterator begin, Iterator end) { while (begin != end && begin != --end) swap(*begin++, *end); } template<typename Iterator1, typename Iterator2> constexpr bool equal(Iterator1 a, Iterator1 ae, Iterator2 b, Iterator2 be) { while (a != ae && b != be) if (*a++ != *b++) return false; return a == ae && b == be; } constexpr bool test1(int n) { char stuff[100] = "foobarfoo"; const char stuff2[100] = "oofraboof"; reverse(stuff, stuff + n); // expected-note {{cannot refer to element 101 of array of 100 elements}} return equal(stuff, stuff + n, stuff2, stuff2 + n); } static_assert(!test1(1), ""); static_assert(test1(3), ""); static_assert(!test1(6), ""); static_assert(test1(9), ""); static_assert(!test1(100), ""); static_assert(!test1(101), ""); // expected-error {{constant expression}} expected-note {{in call to 'test1(101)'}} // FIXME: We should be able to reject this before it's called constexpr void f() { char foo[10] = { "z" }; // expected-note {{here}} foo[10] = 'x'; // expected-warning {{past the end}} expected-note {{assignment to dereferenced one-past-the-end pointer}} } constexpr int k = (f(), 0); // expected-error {{constant expression}} expected-note {{in call}} } namespace array_resize { constexpr int do_stuff(int k1, int k2) { int arr[1234] = { 1, 2, 3, 4 }; arr[k1] = 5; // expected-note {{past-the-end}} expected-note {{cannot refer to element 1235}} expected-note {{cannot refer to element -1}} return arr[k2]; } static_assert(do_stuff(1, 2) == 3, ""); static_assert(do_stuff(0, 0) == 5, ""); static_assert(do_stuff(1233, 1233) == 5, ""); static_assert(do_stuff(1233, 0) == 1, ""); static_assert(do_stuff(1234, 0) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}} static_assert(do_stuff(1235, 0) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}} static_assert(do_stuff(-1, 0) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}} } namespace potential_const_expr { constexpr void set(int &n) { n = 1; } constexpr int div_zero_1() { int z = 0; set(z); return 100 / z; } // no error constexpr int div_zero_2() { // expected-error {{never produces a constant expression}} int z = 0; return 100 / (set(z), 0); // expected-note {{division by zero}} } int n; // expected-note {{declared here}} constexpr int ref() { // expected-error {{never produces a constant expression}} int &r = n; return r; // expected-note {{read of non-const variable 'n'}} } } namespace subobject { union A { constexpr A() : y(5) {} int x, y; }; struct B { A a; }; struct C : B {}; union D { constexpr D() : c() {} constexpr D(int n) : n(n) {} C c; int n; }; constexpr void f(D &d) { d.c.a.y = 3; // expected-note@-1 {{cannot modify an object that is visible outside}} // expected-note@-2 {{assignment to member 'c' of union with active member 'n'}} } constexpr bool check(D &d) { return d.c.a.y == 3; } constexpr bool g() { D d; f(d); return d.c.a.y == 3; } static_assert(g(), ""); D d; constexpr bool h() { f(d); return check(d); } // expected-note {{in call}} static_assert(h(), ""); // expected-error {{constant expression}} expected-note {{in call}} constexpr bool i() { D d(0); f(d); return check(d); } // expected-note {{in call}} static_assert(i(), ""); // expected-error {{constant expression}} expected-note {{in call}} constexpr bool j() { D d; d.c.a.x = 3; return check(d); } // expected-note {{assignment to member 'x' of union with active member 'y'}} static_assert(j(), ""); // expected-error {{constant expression}} expected-note {{in call}} } namespace lifetime { constexpr int &&id(int &&n) { return static_cast<int&&>(n); } constexpr int &&dead() { return id(0); } // expected-note {{temporary created here}} constexpr int bad() { int &&n = dead(); n = 1; return n; } // expected-note {{assignment to temporary whose lifetime has ended}} static_assert(bad(), ""); // expected-error {{constant expression}} expected-note {{in call}} } namespace const_modify { constexpr int modify(int &n) { return n = 1; } // expected-note 2 {{modification of object of const-qualified type 'const int'}} constexpr int test1() { int k = 0; return modify(k); } constexpr int test2() { const int k = 0; return modify(const_cast<int&>(k)); } // expected-note 2 {{in call}} static_assert(test1() == 1, ""); static_assert(test2() == 1, ""); // expected-error {{constant expression}} expected-note {{in call}} constexpr int i = test2(); // expected-error {{constant expression}} expected-note {{in call}} } namespace null { constexpr int test(int *p) { return *p = 123; // expected-note {{assignment to dereferenced null pointer}} } static_assert(test(0), ""); // expected-error {{constant expression}} expected-note {{in call}} } namespace incdec { template<typename T> constexpr T &ref(T &&r) { return r; } template<typename T> constexpr T postinc(T &&r) { return (r++, r); } template<typename T> constexpr T postdec(T &&r) { return (r--, r); } static_assert(++ref(0) == 1, ""); static_assert(ref(0)++ == 0, ""); static_assert(postinc(0) == 1, ""); static_assert(--ref(0) == -1, ""); static_assert(ref(0)-- == 0, ""); static_assert(postdec(0) == -1, ""); constexpr int overflow_int_inc_1 = ref(0x7fffffff)++; // expected-error {{constant}} expected-note {{2147483648}} constexpr int overflow_int_inc_1_ok = ref(0x7ffffffe)++; constexpr int overflow_int_inc_2 = ++ref(0x7fffffff); // expected-error {{constant}} expected-note {{2147483648}} constexpr int overflow_int_inc_2_ok = ++ref(0x7ffffffe); // inc/dec on short can't overflow because we promote to int first static_assert(++ref<short>(0x7fff) == (int)0xffff8000u, ""); static_assert(--ref<short>(0x8000) == 0x7fff, ""); // inc on bool sets to true static_assert(++ref(false), ""); // expected-warning {{deprecated}} static_assert(++ref(true), ""); // expected-warning {{deprecated}} int arr[10]; static_assert(++ref(&arr[0]) == &arr[1], ""); static_assert(++ref(&arr[9]) == &arr[10], ""); static_assert(++ref(&arr[10]) == &arr[11], ""); // expected-error {{constant}} expected-note {{cannot refer to element 11}} static_assert(ref(&arr[0])++ == &arr[0], ""); static_assert(ref(&arr[10])++ == &arr[10], ""); // expected-error {{constant}} expected-note {{cannot refer to element 11}} static_assert(postinc(&arr[0]) == &arr[1], ""); static_assert(--ref(&arr[10]) == &arr[9], ""); static_assert(--ref(&arr[1]) == &arr[0], ""); static_assert(--ref(&arr[0]) != &arr[0], ""); // expected-error {{constant}} expected-note {{cannot refer to element -1}} static_assert(ref(&arr[1])-- == &arr[1], ""); static_assert(ref(&arr[0])-- == &arr[0], ""); // expected-error {{constant}} expected-note {{cannot refer to element -1}} static_assert(postdec(&arr[1]) == &arr[0], ""); int x; static_assert(++ref(&x) == &x + 1, ""); static_assert(++ref(0.0) == 1.0, ""); static_assert(ref(0.0)++ == 0.0, ""); static_assert(postinc(0.0) == 1.0, ""); static_assert(--ref(0.0) == -1.0, ""); static_assert(ref(0.0)-- == 0.0, ""); static_assert(postdec(0.0) == -1.0, ""); static_assert(++ref(1e100) == 1e100, ""); static_assert(--ref(1e100) == 1e100, ""); union U { int a, b; }; constexpr int f(U u) { return ++u.b; // expected-note {{increment of member 'b' of union with active member 'a'}} } constexpr int wrong_member = f({0}); // expected-error {{constant}} expected-note {{in call to 'f({.a = 0})'}} constexpr int vol = --ref<volatile int>(0); // expected-error {{constant}} expected-note {{decrement of volatile-qualified}} constexpr int incr(int k) { int x = k; if (x++ == 100) return x; return incr(x); } static_assert(incr(0) == 101, ""); } namespace compound_assign { constexpr bool test_int() { int a = 3; a += 6; if (a != 9) return false; a -= 2; if (a != 7) return false; a *= 3; if (a != 21) return false; if (&(a /= 10) != &a) return false; if (a != 2) return false; a <<= 3; if (a != 16) return false; a %= 6; if (a != 4) return false; a >>= 1; if (a != 2) return false; a ^= 10; if (a != 8) return false; a |= 5; if (a != 13) return false; a &= 14; if (a != 12) return false; return true; } static_assert(test_int(), ""); constexpr bool test_float() { float f = 123.; f *= 2; if (f != 246.) return false; if ((f -= 0.5) != 245.5) return false; if (f != 245.5) return false; f /= 0.5; if (f != 491.) return false; f += -40; if (f != 451.) return false; return true; } static_assert(test_float(), ""); constexpr bool test_ptr() { int arr[123] = {}; int *p = arr; if ((p += 4) != &arr[4]) return false; if (p != &arr[4]) return false; p += -1; if (p != &arr[3]) return false; if ((p -= -10) != &arr[13]) return false; if (p != &arr[13]) return false; p -= 11; if (p != &arr[2]) return false; return true; } static_assert(test_ptr(), ""); template<typename T> constexpr bool test_overflow() { T a = 1; while (a != a / 2) a *= 2; // expected-note {{value 2147483648 is outside the range}} expected-note {{ 9223372036854775808 }} expected-note {{floating point arithmetic produces an infinity}} return true; } static_assert(test_overflow<int>(), ""); // expected-error {{constant}} expected-note {{call}} static_assert(test_overflow<unsigned>(), ""); // ok, unsigned overflow is defined static_assert(test_overflow<short>(), ""); // ok, short is promoted to int before multiplication static_assert(test_overflow<unsigned short>(), ""); // ok static_assert(test_overflow<unsigned long long>(), ""); // ok static_assert(test_overflow<long long>(), ""); // expected-error {{constant}} expected-note {{call}} static_assert(test_overflow<float>(), ""); // expected-error {{constant}} expected-note {{call}} constexpr short test_promotion(short k) { short s = k; s *= s; return s; } static_assert(test_promotion(100) == 10000, ""); static_assert(test_promotion(200) == -25536, ""); static_assert(test_promotion(256) == 0, ""); constexpr const char *test_bounds(const char *p, int o) { return p += o; // expected-note {{element 5 of}} expected-note {{element -1 of}} expected-note {{element 1000 of}} } static_assert(test_bounds("foo", 0)[0] == 'f', ""); static_assert(test_bounds("foo", 3)[0] == 0, ""); static_assert(test_bounds("foo", 4)[-3] == 'o', ""); static_assert(test_bounds("foo" + 4, -4)[0] == 'f', ""); static_assert(test_bounds("foo", 5) != 0, ""); // expected-error {{constant}} expected-note {{call}} static_assert(test_bounds("foo", -1) != 0, ""); // expected-error {{constant}} expected-note {{call}} static_assert(test_bounds("foo", 1000) != 0, ""); // expected-error {{constant}} expected-note {{call}} } namespace loops { constexpr int fib_loop(int a) { int f_k = 0, f_k_plus_one = 1; for (int k = 1; k != a; ++k) { int f_k_plus_two = f_k + f_k_plus_one; f_k = f_k_plus_one; f_k_plus_one = f_k_plus_two; } return f_k_plus_one; } static_assert(fib_loop(46) == 1836311903, ""); constexpr bool breaks_work() { int a = 0; for (int n = 0; n != 100; ++n) { ++a; if (a == 5) continue; if ((a % 5) == 0) break; } int b = 0; while (b != 17) { ++b; if (b == 6) continue; if ((b % 6) == 0) break; } int c = 0; do { ++c; if (c == 7) continue; if ((c % 7) == 0) break; } while (c != 21); return a == 10 && b == 12 & c == 14; } static_assert(breaks_work(), ""); void not_constexpr(); constexpr bool no_cont_after_break() { for (;;) { break; not_constexpr(); } while (true) { break; not_constexpr(); } do { break; not_constexpr(); } while (true); return true; } static_assert(no_cont_after_break(), ""); constexpr bool cond() { for (int a = 1; bool b = a != 3; ++a) { if (!b) return false; } while (bool b = true) { b = false; break; } return true; } static_assert(cond(), ""); constexpr int range_for() { int arr[] = { 1, 2, 3, 4, 5 }; int sum = 0; for (int x : arr) sum += x; return sum; } static_assert(range_for() == 15, ""); template<int...N> struct ints {}; template<typename A, typename B> struct join_ints; template<int...As, int...Bs> struct join_ints<ints<As...>, ints<Bs...>> { using type = ints<As..., sizeof...(As) + Bs...>; }; template<unsigned N> struct make_ints { using type = typename join_ints<typename make_ints<N/2>::type, typename make_ints<(N+1)/2>::type>::type; }; template<> struct make_ints<0> { using type = ints<>; }; template<> struct make_ints<1> { using type = ints<0>; }; struct ignore { template<typename ...Ts> constexpr ignore(Ts &&...) {} }; template<typename T, unsigned N> struct array { constexpr array() : arr{} {} template<typename ...X> constexpr array(X ...x) : arr{} { init(typename make_ints<sizeof...(X)>::type{}, x...); } template<int ...I, typename ...X> constexpr void init(ints<I...>, X ...x) { ignore{arr[I] = x ...}; } T arr[N]; struct iterator { T *p; constexpr explicit iterator(T *p) : p(p) {} constexpr bool operator!=(iterator o) { return p != o.p; } constexpr iterator &operator++() { ++p; return *this; } constexpr T &operator*() { return *p; } }; constexpr iterator begin() { return iterator(arr); } constexpr iterator end() { return iterator(arr + N); } }; constexpr int range_for_2() { array<int, 5> arr { 1, 2, 3, 4, 5 }; int sum = 0; for (int k : arr) { sum += k; if (sum > 8) break; } return sum; } static_assert(range_for_2() == 10, ""); } namespace assignment_op { struct A { constexpr A() : n(5) {} int n; struct B { int k = 1; union U { constexpr U() : y(4) {} int x; int y; } u; } b; }; constexpr bool testA() { A a, b; a.n = 7; a.b.u.y = 5; b = a; return b.n == 7 && b.b.u.y == 5 && b.b.k == 1; } static_assert(testA(), ""); struct B { bool assigned = false; constexpr B &operator=(const B&) { assigned = true; return *this; } }; struct C : B { B b; int n = 5; }; constexpr bool testC() { C c, d; c.n = 7; d = c; c.n = 3; return d.n == 7 && d.assigned && d.b.assigned; } static_assert(testC(), ""); } namespace switch_stmt { constexpr int f(char k) { bool b = false; int z = 6; switch (k) { return -1; case 0: if (false) { case 1: z = 1; for (; b;) { return 5; while (0) case 2: return 2; case 7: z = 7; do case 6: { return z; if (false) case 3: return 3; case 4: z = 4; } while (1); case 5: b = true; case 9: z = 9; } return z; } else if (false) case 8: z = 8; else if (false) { case 10: z = -10; break; } else z = 0; return z; default: return -1; } return -z; } static_assert(f(0) == 0, ""); static_assert(f(1) == 1, ""); static_assert(f(2) == 2, ""); static_assert(f(3) == 3, ""); static_assert(f(4) == 4, ""); static_assert(f(5) == 5, ""); static_assert(f(6) == 6, ""); static_assert(f(7) == 7, ""); static_assert(f(8) == 8, ""); static_assert(f(9) == 9, ""); static_assert(f(10) == 10, ""); // Check that we can continue an outer loop from within a switch. constexpr bool contin() { for (int n = 0; n != 10; ++n) { switch (n) { case 0: ++n; continue; case 1: return false; case 2: return true; } } return false; } static_assert(contin(), ""); constexpr bool switch_into_for() { int n = 0; switch (n) { for (; n == 1; ++n) { return n == 1; case 0: ; } } return false; } static_assert(switch_into_for(), ""); constexpr void duff_copy(char *a, const char *b, int n) { switch ((n - 1) % 8 + 1) { for ( ; n; n = (n - 1) & ~7) { case 8: a[n-8] = b[n-8]; case 7: a[n-7] = b[n-7]; case 6: a[n-6] = b[n-6]; case 5: a[n-5] = b[n-5]; case 4: a[n-4] = b[n-4]; case 3: a[n-3] = b[n-3]; case 2: a[n-2] = b[n-2]; case 1: a[n-1] = b[n-1]; } case 0: ; } } constexpr bool test_copy(const char *str, int n) { char buffer[16] = {}; duff_copy(buffer, str, n); for (int i = 0; i != sizeof(buffer); ++i) if (buffer[i] != (i < n ? str[i] : 0)) return false; return true; } static_assert(test_copy("foo", 0), ""); static_assert(test_copy("foo", 1), ""); static_assert(test_copy("foo", 2), ""); static_assert(test_copy("hello world", 0), ""); static_assert(test_copy("hello world", 7), ""); static_assert(test_copy("hello world", 8), ""); static_assert(test_copy("hello world", 9), ""); static_assert(test_copy("hello world", 10), ""); static_assert(test_copy("hello world", 10), ""); } namespace deduced_return_type { constexpr auto f() { return 0; } template<typename T> constexpr auto g(T t) { return t; } static_assert(f() == 0, ""); static_assert(g(true), ""); } namespace modify_temporary_during_construction { struct A { int &&temporary; int x; int y; }; constexpr int f(int &r) { r *= 9; return r - 12; } constexpr A a = { 6, f(a.temporary), a.temporary }; // expected-note {{temporary created here}} static_assert(a.x == 42, ""); static_assert(a.y == 54, ""); constexpr int k = a.temporary++; // expected-error {{constant expression}} expected-note {{outside the expression that created the temporary}} } namespace std { typedef decltype(sizeof(int)) size_t; template <class _E> class initializer_list { const _E* __begin_; size_t __size_; constexpr initializer_list(const _E* __b, size_t __s) : __begin_(__b), __size_(__s) {} public: typedef _E value_type; typedef const _E& reference; typedef const _E& const_reference; typedef size_t size_type; typedef const _E* iterator; typedef const _E* const_iterator; constexpr initializer_list() : __begin_(nullptr), __size_(0) {} constexpr size_t size() const {return __size_;} constexpr const _E* begin() const {return __begin_;} constexpr const _E* end() const {return __begin_ + __size_;} }; } namespace InitializerList { constexpr int sum(std::initializer_list<int> ints) { int total = 0; for (int n : ints) total += n; return total; } static_assert(sum({1, 2, 3, 4, 5}) == 15, ""); } namespace StmtExpr { constexpr int f(int k) { switch (k) { case 0: return 0; ({ case 1: // expected-note {{not supported}} return 1; }); } } static_assert(f(1) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}} constexpr int g() { // expected-error {{never produces a constant}} return ({ int n; n; }); // expected-note {{object of type 'int' is not initialized}} } // FIXME: We should handle the void statement expression case. constexpr int h() { // expected-error {{never produces a constant}} ({ if (true) {} }); // expected-note {{not supported}} return 0; } } namespace VirtualFromBase { struct S1 { virtual int f() const; }; struct S2 { virtual int f(); }; template <typename T> struct X : T { constexpr X() {} double d = 0.0; constexpr int f() { return sizeof(T); } }; // Non-virtual f(), OK. constexpr X<X<S1>> xxs1; constexpr X<S1> *p = const_cast<X<X<S1>>*>(&xxs1); static_assert(p->f() == sizeof(S1), ""); // Virtual f(), not OK. constexpr X<X<S2>> xxs2; constexpr X<S2> *q = const_cast<X<X<S2>>*>(&xxs2); static_assert(q->f() == sizeof(X<S2>), ""); // expected-error {{constant expression}} expected-note {{virtual function call}} } namespace Lifetime { constexpr int &get(int &&r) { return r; } constexpr int f() { int &r = get(123); return r; // expected-note {{read of object outside its lifetime}} } static_assert(f() == 123, ""); // expected-error {{constant expression}} expected-note {{in call}} constexpr int g() { int *p = 0; { int n = 0; p = &n; n = 42; } *p = 123; // expected-note {{assignment to object outside its lifetime}} return *p; } static_assert(g() == 42, ""); // expected-error {{constant expression}} expected-note {{in call}} constexpr int h(int n) { int *p[4] = {}; int &&r = 1; p[0] = &r; while (int a = 1) { p[1] = &a; for (int b = 1; int c = 1; ) { p[2] = &b, p[3] = &c; break; } break; } *p[n] = 0; // expected-note 3{{assignment to object outside its lifetime}} return *p[n]; } static_assert(h(0) == 0, ""); // ok, lifetime-extended static_assert(h(1) == 0, ""); // expected-error {{constant expression}} expected-note {{in call}} static_assert(h(2) == 0, ""); // expected-error {{constant expression}} expected-note {{in call}} static_assert(h(3) == 0, ""); // expected-error {{constant expression}} expected-note {{in call}} // FIXME: This function should be treated as non-constant. constexpr void lifetime_versus_loops() { int *p = 0; for (int i = 0; i != 2; ++i) { int *q = p; int n = 0; p = &n; if (i) // This modifies the 'n' from the previous iteration of the loop outside // its lifetime. ++*q; } } static_assert((lifetime_versus_loops(), true), ""); } namespace Bitfields { struct A { bool b : 3; int n : 4; unsigned u : 5; }; constexpr bool test() { A a {}; a.b += 2; --a.n; --a.u; a.n = -a.n * 3; return a.b == false && a.n == 3 && a.u == 31; } static_assert(test(), ""); } namespace PR17615 { struct A { int &&r; constexpr A(int &&r) : r(static_cast<int &&>(r)) {} constexpr A() : A(0) { (void)+r; // expected-note {{outside its lifetime}} } }; constexpr int k = A().r; // expected-error {{constant expression}} expected-note {{in call to}} } namespace PR17331 { template<typename T, unsigned int N> constexpr T sum(const T (&arr)[N]) { T result = 0; for (T i : arr) result += i; return result; } constexpr int ARR[] = { 1, 2, 3, 4, 5 }; static_assert(sum(ARR) == 15, ""); } namespace EmptyClass { struct E1 {} e1; union E2 {} e2; // expected-note 4{{here}} struct E3 : E1 {} e3; template<typename E> constexpr int f(E &a, int kind) { switch (kind) { case 0: { E e(a); return 0; } // expected-note {{read}} expected-note {{in call}} case 1: { E e(static_cast<E&&>(a)); return 0; } // expected-note {{read}} expected-note {{in call}} case 2: { E e; e = a; return 0; } // expected-note {{read}} expected-note {{in call}} case 3: { E e; e = static_cast<E&&>(a); return 0; } // expected-note {{read}} expected-note {{in call}} } } constexpr int test1 = f(e1, 0); constexpr int test2 = f(e2, 0); // expected-error {{constant expression}} expected-note {{in call}} constexpr int test3 = f(e3, 0); constexpr int test4 = f(e1, 1); constexpr int test5 = f(e2, 1); // expected-error {{constant expression}} expected-note {{in call}} constexpr int test6 = f(e3, 1); constexpr int test7 = f(e1, 2); constexpr int test8 = f(e2, 2); // expected-error {{constant expression}} expected-note {{in call}} constexpr int test9 = f(e3, 2); constexpr int testa = f(e1, 3); constexpr int testb = f(e2, 3); // expected-error {{constant expression}} expected-note {{in call}} constexpr int testc = f(e3, 3); }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/MicrosoftExtensions.cpp
// RUN: %clang_cc1 %s -triple i686-pc-win32 -fsyntax-only -Wmicrosoft -Wc++11-extensions -Wno-long-long -verify -fms-extensions -fexceptions -fcxx-exceptions // Microsoft doesn't validate exception specification. namespace microsoft_exception_spec { void foo(); // expected-note {{previous declaration}} void foo() throw(); // expected-warning {{exception specification in declaration does not match previous declaration}} void r6() throw(...); // expected-note {{previous declaration}} void r6() throw(int); // expected-warning {{exception specification in declaration does not match previous declaration}} struct Base { virtual void f2(); virtual void f3() throw(...); }; struct Derived : Base { virtual void f2() throw(...); virtual void f3(); }; class A { virtual ~A() throw(); // expected-note {{overridden virtual function is here}} }; class B : public A { virtual ~B(); // expected-warning {{exception specification of overriding function is more lax than base version}} }; } // MSVC allows type definition in anonymous union and struct struct A { union { int a; struct B // expected-warning {{types declared in an anonymous union are a Microsoft extension}} { int c; } d; union C // expected-warning {{types declared in an anonymous union are a Microsoft extension}} { int e; int ee; } f; typedef int D; // expected-warning {{types declared in an anonymous union are a Microsoft extension}} struct F; // expected-warning {{types declared in an anonymous union are a Microsoft extension}} }; struct { int a2; struct B2 // expected-warning {{types declared in an anonymous struct are a Microsoft extension}} { int c2; } d2; union C2 // expected-warning {{types declared in an anonymous struct are a Microsoft extension}} { int e2; int ee2; } f2; typedef int D2; // expected-warning {{types declared in an anonymous struct are a Microsoft extension}} struct F2; // expected-warning {{types declared in an anonymous struct are a Microsoft extension}} }; }; // __stdcall handling struct M { int __stdcall addP(); float __stdcall subtractP(); }; // __unaligned handling typedef char __unaligned *aligned_type; typedef struct UnalignedTag { int f; } __unaligned *aligned_type2; template<typename T> void h1(T (__stdcall M::* const )()) { } void m1() { h1<int>(&M::addP); h1(&M::subtractP); } void f(long long); void f(int); int main() { // This is an ambiguous call in standard C++. // This calls f(long long) in Microsoft mode because LL is always signed. f(0xffffffffffffffffLL); f(0xffffffffffffffffi64); } // Enumeration types with a fixed underlying type. const int seventeen = 17; typedef int Int; struct X0 { enum E1 : Int { SomeOtherValue } field; // expected-warning{{enumeration types with a fixed underlying type are a C++11 extension}} enum E1 : seventeen; }; enum : long long { // expected-warning{{enumeration types with a fixed underlying type are a C++11 extension}} SomeValue = 0x100000000 }; class AAA { __declspec(dllimport) void f(void) { } void f2(void); // expected-note{{previous declaration is here}} }; __declspec(dllimport) void AAA::f2(void) { // expected-error{{dllimport cannot be applied to non-inline function definition}} // expected-error@-1{{redeclaration of 'AAA::f2' cannot add 'dllimport' attribute}} } template <class T> class BB { public: void f(int g = 10 ); // expected-note {{previous definition is here}} }; template <class T> void BB<T>::f(int g = 0) { } // expected-warning {{redefinition of default argument}} extern void static_func(); void static_func(); // expected-note {{previous declaration is here}} static void static_func() // expected-warning {{redeclaring non-static 'static_func' as static is a Microsoft extension}} { } extern const int static_var; // expected-note {{previous declaration is here}} static const int static_var = 3; // expected-warning {{redeclaring non-static 'static_var' as static is a Microsoft extension}} long function_prototype(int a); long (*function_ptr)(int a); void function_to_voidptr_conv() { void *a1 = function_prototype; void *a2 = &function_prototype; void *a3 = function_ptr; } void pointer_to_integral_type_conv(char* ptr) { char ch = (char)ptr; short sh = (short)ptr; ch = (char)ptr; sh = (short)ptr; // These are valid C++. bool b = (bool)ptr; b = static_cast<bool>(ptr); // This is bad. b = reinterpret_cast<bool>(ptr); // expected-error {{cast from pointer to smaller type 'bool' loses information}} } struct PR11150 { class X { virtual void f() = 0; }; int array[__is_abstract(X)? 1 : -1]; }; void f() { int __except = 0; } void ::f(); // expected-warning{{extra qualification on member 'f'}} class C { C::C(); // expected-warning{{extra qualification on member 'C'}} }; struct StructWithProperty { __declspec(property(get=GetV)) int V1; __declspec(property(put=SetV)) int V2; __declspec(property(get=GetV, put=SetV_NotExist)) int V3; __declspec(property(get=GetV_NotExist, put=SetV)) int V4; __declspec(property(get=GetV, put=SetV)) int V5; int GetV() { return 123; } void SetV(int i) {} }; void TestProperty() { StructWithProperty sp; int i = sp.V2; // expected-error{{no getter defined for property 'V2'}} sp.V1 = 12; // expected-error{{no setter defined for property 'V1'}} int j = sp.V4; // expected-error{{no member named 'GetV_NotExist' in 'StructWithProperty'}} expected-error{{cannot find suitable getter for property 'V4'}} sp.V3 = 14; // expected-error{{no member named 'SetV_NotExist' in 'StructWithProperty'}} expected-error{{cannot find suitable setter for property 'V3'}} int k = sp.V5; sp.V5 = k++; } /* 4 tests for PseudoObject, begin */ struct SP1 { bool operator()() { return true; } }; struct SP2 { __declspec(property(get=GetV)) SP1 V; SP1 GetV() { return SP1(); } }; void TestSP2() { SP2 sp2; bool b = sp2.V(); } struct SP3 { template <class T> void f(T t) {} }; template <class T> struct SP4 { __declspec(property(get=GetV)) int V; int GetV() { return 123; } void f() { SP3 s2; s2.f(V); } }; void TestSP4() { SP4<int> s; s.f(); } template <class T> struct SP5 { __declspec(property(get=GetV)) T V; int GetV() { return 123; } void f() { int *p = new int[V]; } }; template <class T> struct SP6 { public: __declspec(property(get=GetV)) T V; T GetV() { return 123; } void f() { int t = V; } }; void TestSP6() { SP6<int> c; c.f(); } /* 4 tests for PseudoObject, end */ // Property access: explicit, implicit, with Qualifier struct SP7 { __declspec(property(get=GetV, put=SetV)) int V; int GetV() { return 123; } void SetV(int v) {} void ImplicitAccess() { int i = V; V = i; } void ExplicitAccess() { int i = this->V; this->V = i; } }; struct SP8: public SP7 { void AccessWithQualifier() { int i = SP7::V; SP7::V = i; } }; // Property usage template <class T> struct SP9 { __declspec(property(get=GetV, put=SetV)) T V; T GetV() { return 0; } void SetV(T v) {} bool f() { V = this->V; return V < this->V; } void g() { V++; } void h() { V*=2; } }; struct SP10 { SP10(int v) {} bool operator<(const SP10& v) { return true; } SP10 operator*(int v) { return *this; } SP10 operator+(int v) { return *this; } SP10& operator=(const SP10& v) { return *this; } }; void TestSP9() { SP9<int> c; int i = c.V; // Decl initializer i = c.V; // Binary op operand c.SetV(c.V); // CallExpr arg int *p = new int[c.V + 1]; // Array size p[c.V] = 1; // Array index c.V = 123; // Setter c.V++; // Unary op operand c.V *= 2; // Unary op operand SP9<int*> c2; c2.V[0] = 123; // Array SP9<SP10> c3; c3.f(); // Overloaded binary op operand c3.g(); // Overloaded incdec op operand c3.h(); // Overloaded unary op operand } union u { int *i1; int &i2; // expected-warning {{union member 'i2' has reference type 'int &', which is a Microsoft extension}} }; // Property getter using reference. struct SP11 { __declspec(property(get=GetV)) int V; int _v; int& GetV() { return _v; } void UseV(); void TakePtr(int *) {} void TakeRef(int &) {} void TakeVal(int) {} }; void SP11::UseV() { TakePtr(&V); TakeRef(V); TakeVal(V); } struct StructWithUnnamedMember { __declspec(property(get=GetV)) int : 10; // expected-error {{anonymous property is not supported}} }; struct MSPropertyClass { int get() { return 42; } int __declspec(property(get = get)) n; }; int *f(MSPropertyClass &x) { return &x.n; // expected-error {{address of property expression requested}} } int MSPropertyClass::*g() { return &MSPropertyClass::n; // expected-error {{address of property expression requested}} } namespace rdar14250378 { class Bar {}; namespace NyNamespace { class Foo { public: Bar* EnsureBar(); }; class Baz : public Foo { public: friend class Bar; }; Bar* Foo::EnsureBar() { return 0; } } } // expected-error@+1 {{'sealed' keyword not permitted with interface types}} __interface InterfaceWithSealed sealed { }; struct SomeBase { virtual void OverrideMe(); // expected-note@+2 {{overridden virtual function is here}} // expected-warning@+1 {{'sealed' keyword is a Microsoft extension}} virtual void SealedFunction() sealed; // expected-note {{overridden virtual function is here}} }; // expected-note@+2 {{'SealedType' declared here}} // expected-warning@+1 {{'sealed' keyword is a Microsoft extension}} struct SealedType sealed : SomeBase { // expected-error@+2 {{declaration of 'SealedFunction' overrides a 'sealed' function}} // FIXME. warning can be suppressed if we're also issuing error for overriding a 'final' function. virtual void SealedFunction(); // expected-warning {{'SealedFunction' overrides a member function but is not marked 'override'}} // expected-warning@+1 {{'override' keyword is a C++11 extension}} virtual void OverrideMe() override; }; // expected-error@+1 {{base 'SealedType' is marked 'sealed'}} struct InheritFromSealed : SealedType {}; void AfterClassBody() { // expected-warning@+1 {{attribute 'deprecated' is ignored, place it after "struct" to apply attribute to type declaration}} struct D {} __declspec(deprecated); struct __declspec(align(4)) S {} __declspec(align(8)) s1; S s2; _Static_assert(__alignof(S) == 4, ""); _Static_assert(__alignof(s1) == 8, ""); _Static_assert(__alignof(s2) == 4, ""); }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/warn-self-move.cpp
// RUN: %clang_cc1 -fsyntax-only -Wself-move -std=c++11 -verify %s // definitions for std::move namespace std { inline namespace foo { template <class T> struct remove_reference { typedef T type; }; template <class T> struct remove_reference<T&> { typedef T type; }; template <class T> struct remove_reference<T&&> { typedef T type; }; template <class T> typename remove_reference<T>::type &&move(T &&t); } } void int_test() { int x = 5; x = std::move(x); // expected-warning{{explicitly moving}} (x) = std::move(x); // expected-warning{{explicitly moving}} using std::move; x = move(x); // expected-warning{{explicitly moving}} } int global; void global_int_test() { global = std::move(global); // expected-warning{{explicitly moving}} (global) = std::move(global); // expected-warning{{explicitly moving}} using std::move; global = move(global); // expected-warning{{explicitly moving}} } class field_test { int x; field_test(field_test&& other) { x = std::move(x); // expected-warning{{explicitly moving}} x = std::move(other.x); other.x = std::move(x); other.x = std::move(other.x); // expected-warning{{explicitly moving}} } }; struct A {}; struct B { A a; }; struct C { C() {}; ~C() {} }; void struct_test() { A a; a = std::move(a); // expected-warning{{explicitly moving}} B b; b = std::move(b); // expected-warning{{explicitly moving}} b.a = std::move(b.a); // expected-warning{{explicitly moving}} C c; c = std::move(c); // expected-warning{{explicitly moving}} }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/qualification-conversion.cpp
// RUN: %clang_cc1 -fsyntax-only -pedantic -verify %s int* quals1(int const * p); int* quals2(int const * const * pp); int* quals3(int const * * const * ppp); // expected-note{{candidate function}} void test_quals(int * p, int * * pp, int * * * ppp) { int const * const * pp2 = pp; quals1(p); quals2(pp); quals3(ppp); // expected-error {{no matching}} } struct A {}; void mquals1(int const A::*p); void mquals2(int const A::* const A::*pp); void mquals3(int const A::* A::* const A::*ppp); // expected-note{{candidate function}} void test_mquals(int A::*p, int A::* A::*pp, int A::* A::* A::*ppp) { int const A::* const A::* pp2 = pp; mquals1(p); mquals2(pp); mquals3(ppp); // expected-error {{no matching}} } void aquals1(int const (*p)[1]); void aquals2(int * const (*pp)[1]); void aquals2a(int const * (*pp2)[1]); // expected-note{{candidate function}} void test_aquals(int (*p)[1], int * (*pp)[1], int * (*pp2)[1]) { int const (*p2)[1] = p; aquals1(p); aquals2(pp); aquals2a(pp2); // expected-error {{no matching}} }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/empty-class-layout.cpp
// RUN: %clang_cc1 -triple x86_64-unknown-unknown %s -fsyntax-only -verify -Wno-inaccessible-base // expected-no-diagnostics #define SA(n, p) int a##n[(p) ? 1 : -1] namespace Test0 { struct A { int a; }; SA(0, sizeof(A) == 4); struct B { }; SA(1, sizeof(B) == 1); struct C : A, B { }; SA(2, sizeof(C) == 4); struct D { }; struct E : D { }; struct F : E { }; struct G : E, F { }; SA(3, sizeof(G) == 2); struct Empty { Empty(); }; struct I : Empty { Empty e; }; SA(4, sizeof(I) == 2); struct J : Empty { Empty e[2]; }; SA(5, sizeof(J) == 3); template<int N> struct Derived : Empty, Derived<N - 1> { }; template<> struct Derived<0> : Empty { }; struct S1 : virtual Derived<10> { Empty e; }; SA(6, sizeof(S1) == 24); struct S2 : virtual Derived<10> { Empty e[2]; }; SA(7, sizeof(S2) == 24); struct S3 { Empty e; }; struct S4 : Empty, S3 { }; SA(8, sizeof(S4) == 2); struct S5 : S3, Empty {}; SA(9, sizeof(S5) == 2); struct S6 : S5 { }; SA(10, sizeof(S6) == 2); struct S7 : Empty { void *v; }; SA(11, sizeof(S7) == 8); struct S8 : Empty, A { }; SA(12, sizeof(S8) == 4); } namespace Test1 { // Test that we don't try to place both A subobjects at offset 0. struct A { }; class B { virtual void f(); }; class C : A, virtual B { }; struct D : virtual C { }; struct E : virtual A { }; class F : D, E { }; SA(0, sizeof(F) == 24); } namespace Test2 { // Test that B::a isn't laid out at offset 0. struct Empty { }; struct A : Empty { }; struct B : Empty { A a; }; SA(0, sizeof(B) == 2); } namespace Test3 { // Test that B::a isn't laid out at offset 0. struct Empty { }; struct A { Empty e; }; struct B : Empty { A a; }; SA(0, sizeof(B) == 2); } namespace Test4 { // Test that C::Empty isn't laid out at offset 0. struct Empty { }; struct A : Empty { }; struct B { A a; }; struct C : B, Empty { }; SA(0, sizeof(C) == 2); } namespace Test5 { // Test that B::Empty isn't laid out at offset 0. struct Empty { }; struct Field : virtual Empty { }; struct A { Field f; }; struct B : A, Empty { }; SA(0, sizeof(B) == 16); } namespace Test6 { // Test that B::A isn't laid out at offset 0. struct Empty { }; struct Field : virtual Empty { }; struct A { Field f; }; struct B : Empty, A { }; SA(0, sizeof(B) == 16); } namespace Test7 { // Make sure we reserve enough space for both bases; PR11745. struct Empty { }; struct Base1 : Empty { }; struct Base2 : Empty { }; struct Test : Base1, Base2 { char c; }; SA(0, sizeof(Test) == 2); } namespace Test8 { // Test that type sugar doesn't make us incorrectly determine the size of an // array of empty classes. struct Empty1 {}; struct Empty2 {}; struct Empties : Empty1, Empty2 {}; typedef Empty1 Sugar[4]; struct A : Empty2, Empties { // This must go at offset 2, because if it were at offset 0, // V[0][1] would overlap Empties::Empty1. Sugar V[1]; }; SA(0, sizeof(A) == 6); }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/null_in_arithmetic_ops.cpp
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fsyntax-only -fblocks -Wnull-arithmetic -verify -Wno-string-plus-int -Wno-tautological-pointer-compare %s #include <stddef.h> void f() { int a; bool b; void (^c)(); class X; void (X::*d) (); extern void e(); int f[2]; const void *v; a = 0 ? NULL + a : a + NULL; // expected-warning 2{{use of NULL in arithmetic operation}} a = 0 ? NULL - a : a - NULL; // expected-warning 2{{use of NULL in arithmetic operation}} a = 0 ? NULL / a : a / NULL; // expected-warning 2{{use of NULL in arithmetic operation}} \ // expected-warning {{division by zero is undefined}} a = 0 ? NULL * a : a * NULL; // expected-warning 2{{use of NULL in arithmetic operation}} a = 0 ? NULL >> a : a >> NULL; // expected-warning 2{{use of NULL in arithmetic operation}} a = 0 ? NULL << a : a << NULL; // expected-warning 2{{use of NULL in arithmetic operation}} a = 0 ? NULL % a : a % NULL; // expected-warning 2{{use of NULL in arithmetic operation}} \ expected-warning {{remainder by zero is undefined}} a = 0 ? NULL & a : a & NULL; // expected-warning 2{{use of NULL in arithmetic operation}} a = 0 ? NULL | a : a | NULL; // expected-warning 2{{use of NULL in arithmetic operation}} a = 0 ? NULL ^ a : a ^ NULL; // expected-warning 2{{use of NULL in arithmetic operation}} // Check for warnings or errors when doing arithmetic on pointers and other // types. v = 0 ? NULL + &a : &a + NULL; // expected-warning 2{{use of NULL in arithmetic operation}} v = 0 ? NULL + c : c + NULL; // \ expected-error {{invalid operands to binary expression ('long' and 'void (^)()')}} \ expected-error {{invalid operands to binary expression ('void (^)()' and 'long')}} v = 0 ? NULL + d : d + NULL; // \ expected-error {{invalid operands to binary expression ('long' and 'void (X::*)()')}} \ expected-error {{invalid operands to binary expression ('void (X::*)()' and 'long')}} v = 0 ? NULL + e : e + NULL; // expected-error 2{{arithmetic on a pointer to the function type 'void ()'}} v = 0 ? NULL + f : f + NULL; // expected-warning 2{{use of NULL in arithmetic operation}} v = 0 ? NULL + "f" : "f" + NULL; // expected-warning 2{{use of NULL in arithmetic operation}} // Using two NULLs should only give one error instead of two. a = NULL + NULL; // expected-warning{{use of NULL in arithmetic operation}} a = NULL - NULL; // expected-warning{{use of NULL in arithmetic operation}} a = NULL / NULL; // expected-warning{{use of NULL in arithmetic operation}} \ // expected-warning{{division by zero is undefined}} a = NULL * NULL; // expected-warning{{use of NULL in arithmetic operation}} a = NULL >> NULL; // expected-warning{{use of NULL in arithmetic operation}} a = NULL << NULL; // expected-warning{{use of NULL in arithmetic operation}} a = NULL % NULL; // expected-warning{{use of NULL in arithmetic operation}} \ // expected-warning{{remainder by zero is undefined}} a = NULL & NULL; // expected-warning{{use of NULL in arithmetic operation}} a = NULL | NULL; // expected-warning{{use of NULL in arithmetic operation}} a = NULL ^ NULL; // expected-warning{{use of NULL in arithmetic operation}} a += NULL; // expected-warning{{use of NULL in arithmetic operation}} a -= NULL; // expected-warning{{use of NULL in arithmetic operation}} a /= NULL; // expected-warning{{use of NULL in arithmetic operation}} \ // expected-warning{{division by zero is undefined}} a *= NULL; // expected-warning{{use of NULL in arithmetic operation}} a >>= NULL; // expected-warning{{use of NULL in arithmetic operation}} a <<= NULL; // expected-warning{{use of NULL in arithmetic operation}} a %= NULL; // expected-warning{{use of NULL in arithmetic operation}} \ // expected-warning{{remainder by zero is undefined}} a &= NULL; // expected-warning{{use of NULL in arithmetic operation}} a |= NULL; // expected-warning{{use of NULL in arithmetic operation}} a ^= NULL; // expected-warning{{use of NULL in arithmetic operation}} b = a < NULL || a > NULL; // expected-warning 2{{comparison between NULL and non-pointer ('int' and NULL)}} b = NULL < a || NULL > a; // expected-warning 2{{comparison between NULL and non-pointer (NULL and 'int')}} b = a <= NULL || a >= NULL; // expected-warning 2{{comparison between NULL and non-pointer ('int' and NULL)}} b = NULL <= a || NULL >= a; // expected-warning 2{{comparison between NULL and non-pointer (NULL and 'int')}} b = a == NULL || a != NULL; // expected-warning 2{{comparison between NULL and non-pointer ('int' and NULL)}} b = NULL == a || NULL != a; // expected-warning 2{{comparison between NULL and non-pointer (NULL and 'int')}} b = &a < NULL || NULL < &a || &a > NULL || NULL > &a; b = &a <= NULL || NULL <= &a || &a >= NULL || NULL >= &a; b = &a == NULL || NULL == &a || &a != NULL || NULL != &a; b = 0 == a; b = 0 == &a; b = NULL < NULL || NULL > NULL; b = NULL <= NULL || NULL >= NULL; b = NULL == NULL || NULL != NULL; b = ((NULL)) != a; // expected-warning{{comparison between NULL and non-pointer (NULL and 'int')}} // Check that even non-standard pointers don't warn. b = c == NULL || NULL == c || c != NULL || NULL != c; b = d == NULL || NULL == d || d != NULL || NULL != d; b = e == NULL || NULL == e || e != NULL || NULL != e; b = f == NULL || NULL == f || f != NULL || NULL != f; b = "f" == NULL || NULL == "f" || "f" != NULL || NULL != "f"; return NULL; // expected-error{{void function 'f' should not return a value}} }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/PR9902.cpp
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s // expected-no-diagnostics template <class _Tp, class _Up, bool = false> struct __allocator_traits_rebind { }; template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up> struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false> { typedef _Alloc<_Up, _Args...> type; }; template <class Alloc> struct allocator_traits { template <class T> using rebind_alloc = typename __allocator_traits_rebind<Alloc, T>::type; template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>; }; template <class T> struct allocator {}; int main() { allocator_traits<allocator<char>>::rebind_alloc<int> a; }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/address-of-temporary.cpp
// RUN: %clang_cc1 -fsyntax-only -Wno-error=address-of-temporary -verify -std=gnu++11 %s struct X { X(); X(int); X(int, int); }; void f0() { (void)&X(); } // expected-warning{{taking the address of a temporary object}} void f1() { (void)&X(1); } // expected-warning{{taking the address of a temporary object}} void f2() { (void)&X(1, 2); } // expected-warning{{taking the address of a temporary object}} void f3() { (void)&(X)1; } // expected-warning{{taking the address of a temporary object}} namespace PointerToArrayDecay { struct Y { int a[4]; }; struct Z { int n; ~Z(); }; typedef int A[4]; typedef Z AZ[4]; template<typename T> void consume(T); struct S { int *p; }; void g0() { int *p = Y().a; } // expected-warning{{pointer is initialized by a temporary array}} void g1() { int *p = Y{}.a; } // expected-warning{{pointer is initialized by a temporary array}} void g2() { int *p = A{}; } // expected-warning{{pointer is initialized by a temporary array}} void g3() { int *p = (A){}; } // expected-warning{{pointer is initialized by a temporary array}} void g4() { Z *p = AZ{}; } // expected-warning{{pointer is initialized by a temporary array}} void h0() { consume(Y().a); } void h1() { consume(Y{}.a); } void h2() { consume(A{}); } void h3() { consume((A){}); } void h4() { consume(AZ{}); } void i0() { S s = { Y().a }; } // expected-warning{{pointer is initialized by a temporary array}} void i1() { S s = { Y{}.a }; } // expected-warning{{pointer is initialized by a temporary array}} void i2() { S s = { A{} }; } // expected-warning{{pointer is initialized by a temporary array}} void i3() { S s = { (A){} }; } // expected-warning{{pointer is initialized by a temporary array}} void j0() { (void)S { Y().a }; } void j1() { (void)S { Y{}.a }; } void j2() { (void)S { A{} }; } void j3() { (void)S { (A){} }; } void k0() { consume(S { Y().a }); } void k1() { consume(S { Y{}.a }); } void k2() { consume(S { A{} }); } void k3() { consume(S { (A){} }); } }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/attr-gnu.cpp
// RUN: %clang_cc1 -std=gnu++11 -fsyntax-only -fms-compatibility -verify %s void f() { // GNU-style attributes are prohibited in this position. auto P = new int * __attribute__((vector_size(8))); // expected-error {{an attribute list cannot appear here}} \ // expected-error {{invalid vector element type 'int *'}} // Ensure that MS type attribute keywords are still supported in this // position. auto P2 = new int * __sptr; // Ok } void g(int a[static [[]] 5]); // expected-error {{static array size is a C99 feature, not permitted in C++}} namespace { class B { public: virtual void test() {} virtual void test2() {} virtual void test3() {} }; class D : public B { public: void test() __attribute__((deprecated)) final {} // expected-warning {{GCC does not allow an attribute in this position on a function declaration}} void test2() [[]] override {} // Ok void test3() __attribute__((cf_unknown_transfer)) override {} // Ok, not known to GCC. }; }
0
repos/DirectXShaderCompiler/tools/clang/test
repos/DirectXShaderCompiler/tools/clang/test/SemaCXX/printf-block.cpp
// RUN: %clang_cc1 -fsyntax-only -fblocks -Wformat -verify %s -Wno-error=non-pod-varargs int (^block) (int, const char *,...) __attribute__((__format__(__printf__,2,3))) = ^ __attribute__((__format__(__printf__,2,3))) (int arg, const char *format,...) {return 5;}; class HasNoCStr { const char *str; public: HasNoCStr(const char *s): str(s) { } const char *not_c_str() {return str;} }; void test_block() { const char str[] = "test"; HasNoCStr hncs(str); int n = 4; block(n, "%s %d", str, n); // no-warning block(n, "%s %s", hncs, n); // expected-warning{{cannot pass non-POD object of type 'HasNoCStr' to variadic block; expected type from format string was 'char *'}} expected-warning{{format specifies type 'char *' but the argument has type 'int'}} }