Home | History | Annotate | Download | only in SemaCXX
      1 // RUN: %clang_cc1 %s -fsyntax-only -verify -Wreturn-type -Wmissing-noreturn -Wno-unreachable-code -Wno-covered-switch-default
      2 // RUN: %clang_cc1 %s -fsyntax-only -std=c++11 -verify -Wreturn-type -Wmissing-noreturn -Wno-unreachable-code -Wno-covered-switch-default
      3 
      4 // A destructor may be marked noreturn and should still influence the CFG.
      5 void pr6884_abort() __attribute__((noreturn));
      6 
      7 struct pr6884_abort_struct {
      8   pr6884_abort_struct() {}
      9   ~pr6884_abort_struct() __attribute__((noreturn)) { pr6884_abort(); }
     10 };
     11 
     12 struct other { ~other() {} };
     13 
     14 // Ensure that destructors from objects are properly modeled in the CFG despite
     15 // the presence of switches, case statements, labels, and blocks. These tests
     16 // try to cover bugs reported in both PR6884 and PR10063.
     17 namespace abort_struct_complex_cfgs {
     18   int basic(int x) {
     19     switch (x) { default: pr6884_abort(); }
     20   }
     21   int f1(int x) {
     22     switch (x) default: pr6884_abort_struct();
     23   }
     24   int f2(int x) {
     25     switch (x) { default: pr6884_abort_struct(); }
     26   }
     27   int f2_positive(int x) {
     28     switch (x) { default: ; }
     29   } // expected-warning {{control reaches end of non-void function}}
     30   int f3(int x) {
     31     switch (x) { default: { pr6884_abort_struct(); } }
     32   }
     33   int f4(int x) {
     34     switch (x) default: L1: L2: case 4: pr6884_abort_struct();
     35   }
     36   int f5(int x) {
     37     switch (x) default: L1: { L2: case 4: pr6884_abort_struct(); }
     38   }
     39   int f6(int x) {
     40     switch (x) default: L1: L2: case 4: { pr6884_abort_struct(); }
     41   }
     42 
     43   // FIXME: detect noreturn destructors triggered by calls to delete.
     44   int f7(int x) {
     45     switch (x) default: L1: L2: case 4: {
     46       pr6884_abort_struct *p = new pr6884_abort_struct();
     47       delete p;
     48     }
     49   } // expected-warning {{control reaches end of non-void function}}
     50 
     51   // Test that these constructs work even when extraneous blocks are created
     52   // before and after the switch due to implicit destructors.
     53   int g1(int x) {
     54     other o;
     55     switch (x) default: pr6884_abort_struct();
     56   }
     57   int g2(int x) {
     58     other o;
     59     switch (x) { default: pr6884_abort_struct(); }
     60   }
     61   int g2_positive(int x) {
     62     other o;
     63     switch (x) { default: ; }
     64   } // expected-warning {{control reaches end of non-void function}}
     65   int g3(int x) {
     66     other o;
     67     switch (x) { default: { pr6884_abort_struct(); } }
     68   }
     69   int g4(int x) {
     70     other o;
     71     switch (x) default: L1: L2: case 4: pr6884_abort_struct();
     72   }
     73   int g5(int x) {
     74     other o;
     75     switch (x) default: L1: { L2: case 4: pr6884_abort_struct(); }
     76   }
     77   int g6(int x) {
     78     other o;
     79     switch (x) default: L1: L2: case 4: { pr6884_abort_struct(); }
     80   }
     81 
     82   // Test that these constructs work even with variables carrying the no-return
     83   // destructor instead of temporaries.
     84   int h1(int x) {
     85     other o;
     86     switch (x) default: pr6884_abort_struct a;
     87   }
     88   int h2(int x) {
     89     other o;
     90     switch (x) { default: pr6884_abort_struct a; }
     91   }
     92   int h3(int x) {
     93     other o;
     94     switch (x) { default: { pr6884_abort_struct a; } }
     95   }
     96   int h4(int x) {
     97     other o;
     98     switch (x) default: L1: L2: case 4: pr6884_abort_struct a;
     99   }
    100   int h5(int x) {
    101     other o;
    102     switch (x) default: L1: { L2: case 4: pr6884_abort_struct a; }
    103   }
    104   int h6(int x) {
    105     other o;
    106     switch (x) default: L1: L2: case 4: { pr6884_abort_struct a; }
    107   }
    108 }
    109 
    110 // PR9380
    111 struct PR9380 {
    112   ~PR9380();
    113 };
    114 struct PR9380_B : public PR9380 {
    115   PR9380_B( const PR9380& str );
    116 };
    117 void test_PR9380(const PR9380& aKey) {
    118   const PR9380& flatKey = PR9380_B(aKey);
    119 }
    120 
    121 // Array of objects with destructors.  This is purely a coverage test case.
    122 void test_array() {
    123   PR9380 a[2];
    124 }
    125 
    126 // Test classes wrapped in typedefs.  This is purely a coverage test case
    127 // for CFGImplictDtor::getDestructorDecl().
    128 void test_typedefs() {
    129   typedef PR9380 PR9380_Ty;
    130   PR9380_Ty test;
    131   PR9380_Ty test2[20];
    132 }
    133 
    134 // PR9412 - Handle CFG traversal with null successors.
    135 enum PR9412_MatchType { PR9412_Exact };
    136 
    137 template <PR9412_MatchType type> int PR9412_t() {
    138   switch (type) {
    139     case PR9412_Exact:
    140     default:
    141         break;
    142   }
    143 } // expected-warning {{control reaches end of non-void function}}
    144 
    145 void PR9412_f() {
    146     PR9412_t<PR9412_Exact>(); // expected-note {{in instantiation of function template specialization 'PR9412_t<0>' requested here}}
    147 }
    148 
    149