Jordan Rose | 41c98d9 | 2012-07-27 01:15:02 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=core,debug.ExprInspection -fblocks -verify %s |
Jordan Rose | 1e0e400 | 2012-09-10 21:27:35 +0000 | [diff] [blame] | 2 | // RUN: %clang_cc1 -analyze -analyzer-checker=core,debug.ExprInspection -fblocks -analyzer-config c++-template-inlining=false -DNO_INLINE -verify %s |
Jordan Rose | 41c98d9 | 2012-07-27 01:15:02 +0000 | [diff] [blame] | 3 | |
| 4 | void clang_analyzer_eval(bool); |
Anna Zaks | b4e71e8 | 2012-05-14 22:38:24 +0000 | [diff] [blame] | 5 | |
| 6 | // Do not crash on this templated code which uses a block. |
| 7 | typedef void (^my_block)(void); |
| 8 | static void useBlock(my_block block){} |
| 9 | template<class T> class MyClass; |
| 10 | typedef MyClass<float> Mf; |
| 11 | |
| 12 | template<class T> |
| 13 | class MyClass |
| 14 | { |
| 15 | public: |
| 16 | MyClass() {} |
| 17 | MyClass(T a); |
| 18 | void I(); |
| 19 | private: |
| 20 | static const T one; |
| 21 | }; |
| 22 | |
| 23 | template<class T> const T MyClass<T>::one = static_cast<T>(1); |
| 24 | template<class T> inline MyClass<T>::MyClass(T a){} |
| 25 | template<class T> void MyClass<T>::I() { |
| 26 | static MyClass<T>* mPtr = 0; |
| 27 | useBlock(^{ mPtr = new MyClass<T> (MyClass<T>::one); }); |
| 28 | }; |
| 29 | int main(){ |
| 30 | Mf m; |
| 31 | m.I(); |
| 32 | } |
Jordan Rose | 41c98d9 | 2012-07-27 01:15:02 +0000 | [diff] [blame] | 33 | |
| 34 | |
| 35 | // <rdar://problem/11949235> |
| 36 | template<class T, unsigned N> |
| 37 | inline unsigned array_lengthof(T (&)[N]) { |
| 38 | return N; |
| 39 | } |
| 40 | |
| 41 | void testNonTypeTemplateInstantiation() { |
| 42 | const char *S[] = { "a", "b" }; |
Jordan Rose | 1e0e400 | 2012-09-10 21:27:35 +0000 | [diff] [blame] | 43 | clang_analyzer_eval(array_lengthof(S) == 2); |
| 44 | #ifndef NO_INLINE |
| 45 | // expected-warning@-2 {{TRUE}} |
| 46 | #else |
| 47 | // expected-warning@-4 {{UNKNOWN}} |
| 48 | #endif |
Jordan Rose | 41c98d9 | 2012-07-27 01:15:02 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Jordan Rose | 1bfe9c7 | 2013-05-22 18:09:44 +0000 | [diff] [blame] | 51 | namespace rdar13954714 { |
| 52 | template <bool VALUE> |
| 53 | bool blockInTemplate() { |
| 54 | return (^() { |
| 55 | return VALUE; |
| 56 | })(); |
| 57 | } |
| 58 | |
| 59 | // force instantiation |
| 60 | template bool blockInTemplate<true>(); |
| 61 | |
| 62 | template <bool VALUE> |
| 63 | void blockWithStatic() { |
| 64 | (void)^() { |
| 65 | static int x; |
| 66 | return ++x; |
| 67 | }; |
| 68 | } |
| 69 | |
| 70 | // force instantiation |
| 71 | template void blockWithStatic<true>(); |
| 72 | } |