Anders Carlsson | abea951 | 2011-02-28 00:40:07 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 %s -fcxx-exceptions -fexceptions -fsyntax-only -verify -fblocks -Wunreachable-code -Wno-unused-value |
Mike Stump | 4c45aa1 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 2 | |
Anders Carlsson | 5d1d7ae | 2010-09-03 00:25:02 +0000 | [diff] [blame] | 3 | int &halt() __attribute__((noreturn)); |
Mike Stump | 55f988e | 2010-01-21 17:21:23 +0000 | [diff] [blame] | 4 | int &live(); |
Mike Stump | 4c45aa1 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 5 | int dead(); |
| 6 | int liveti() throw(int); |
| 7 | int (*livetip)() throw(int); |
| 8 | |
| 9 | int test1() { |
| 10 | try { |
| 11 | live(); |
| 12 | } catch (int i) { |
| 13 | live(); |
| 14 | } |
| 15 | return 1; |
| 16 | } |
| 17 | |
| 18 | void test2() { |
| 19 | try { |
| 20 | live(); |
| 21 | } catch (int i) { |
| 22 | live(); |
| 23 | } |
| 24 | try { |
| 25 | liveti(); |
| 26 | } catch (int i) { |
| 27 | live(); |
| 28 | } |
| 29 | try { |
| 30 | livetip(); |
| 31 | } catch (int i) { |
| 32 | live(); |
| 33 | } |
| 34 | throw 1; |
| 35 | dead(); // expected-warning {{will never be executed}} |
| 36 | } |
Mike Stump | 55f988e | 2010-01-21 17:21:23 +0000 | [diff] [blame] | 37 | |
| 38 | |
| 39 | void test3() { |
| 40 | halt() |
| 41 | --; // expected-warning {{will never be executed}} |
Ted Kremenek | 7dd3c73 | 2010-12-16 08:22:16 +0000 | [diff] [blame] | 42 | // FIXME: The unreachable part is just the '?', but really all of this |
| 43 | // code is unreachable and shouldn't be separately reported. |
| 44 | halt() // expected-warning {{will never be executed}} |
| 45 | ? |
Mike Stump | e5fba70 | 2010-01-21 19:44:04 +0000 | [diff] [blame] | 46 | dead() : dead(); |
Mike Stump | 2d6ceab | 2010-01-21 22:12:18 +0000 | [diff] [blame] | 47 | live(), |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 48 | float |
| 49 | (halt()); // expected-warning {{will never be executed}} |
Mike Stump | 55f988e | 2010-01-21 17:21:23 +0000 | [diff] [blame] | 50 | } |
Mike Stump | b5c7755 | 2010-01-21 23:15:53 +0000 | [diff] [blame] | 51 | |
| 52 | void test4() { |
| 53 | struct S { |
| 54 | int mem; |
| 55 | } s; |
| 56 | S &foor(); |
Zhongxing Xu | 9107166 | 2010-02-24 02:19:28 +0000 | [diff] [blame] | 57 | halt(), foor()// expected-warning {{will never be executed}} |
| 58 | .mem; |
Mike Stump | b5c7755 | 2010-01-21 23:15:53 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void test5() { |
| 62 | struct S { |
| 63 | int mem; |
| 64 | } s; |
Anders Carlsson | 5d1d7ae | 2010-09-03 00:25:02 +0000 | [diff] [blame] | 65 | S &foor() __attribute__((noreturn)); |
Mike Stump | b5c7755 | 2010-01-21 23:15:53 +0000 | [diff] [blame] | 66 | foor() |
| 67 | .mem; // expected-warning {{will never be executed}} |
| 68 | } |
| 69 | |
| 70 | void test6() { |
| 71 | struct S { |
| 72 | ~S() { } |
| 73 | S(int i) { } |
| 74 | }; |
| 75 | live(), |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 76 | S |
| 77 | (halt()); // expected-warning {{will never be executed}} |
Mike Stump | b5c7755 | 2010-01-21 23:15:53 +0000 | [diff] [blame] | 78 | } |
Ted Kremenek | 5dfee06 | 2011-11-30 21:22:09 +0000 | [diff] [blame] | 79 | |
| 80 | // Don't warn about unreachable code in template instantiations, as |
| 81 | // they may only be unreachable in that specific instantiation. |
| 82 | void isUnreachable(); |
| 83 | |
| 84 | template <typename T> void test_unreachable_templates() { |
| 85 | T::foo(); |
| 86 | isUnreachable(); // no-warning |
| 87 | } |
| 88 | |
| 89 | struct TestUnreachableA { |
| 90 | static void foo() __attribute__((noreturn)); |
| 91 | }; |
| 92 | struct TestUnreachableB { |
| 93 | static void foo(); |
| 94 | }; |
| 95 | |
| 96 | void test_unreachable_templates_harness() { |
| 97 | test_unreachable_templates<TestUnreachableA>(); |
| 98 | test_unreachable_templates<TestUnreachableB>(); |
| 99 | } |
| 100 | |
Ted Kremenek | 75df4ee | 2011-12-01 00:59:17 +0000 | [diff] [blame] | 101 | // Do warn about explict template specializations, as they represent |
| 102 | // actual concrete functions that somebody wrote. |
| 103 | |
| 104 | template <typename T> void funcToSpecialize() {} |
| 105 | template <> void funcToSpecialize<int>() { |
| 106 | halt(); |
| 107 | dead(); // expected-warning {{will never be executed}} |
| 108 | } |
David Blaikie | 25f4c19 | 2012-01-24 04:29:27 +0000 | [diff] [blame] | 109 | |