Home | History | Annotate | Download | only in SemaCXX
      1 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wimplicit-fallthrough -DCOMMAND_LINE_FALLTHROUGH=[[clang::fallthrough]] %s
      2 
      3 int fallthrough_compatibility_macro_from_command_line(int n) {
      4   switch (n) {
      5     case 0:
      6       n = n * 10;
      7     case 1: // expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert 'COMMAND_LINE_FALLTHROUGH;' to silence this warning}} expected-note{{insert 'break;' to avoid fall-through}}
      8       ;
      9   }
     10   return n;
     11 }
     12 
     13 #ifdef __clang__
     14 #if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough")
     15 #define COMPATIBILITY_FALLTHROUGH   [ [ /* test */  clang /* test */ \
     16     ::  fallthrough  ]  ]    // testing whitespace and comments in macro definition
     17 #endif
     18 #endif
     19 
     20 #ifndef COMPATIBILITY_FALLTHROUGH
     21 #define COMPATIBILITY_FALLTHROUGH do { } while (0)
     22 #endif
     23 
     24 int fallthrough_compatibility_macro_from_source(int n) {
     25   switch (n) {
     26     case 0:
     27       n = n * 20;
     28     case 1: // expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert 'COMPATIBILITY_FALLTHROUGH;' to silence this warning}} expected-note{{insert 'break;' to avoid fall-through}}
     29       ;
     30   }
     31   return n;
     32 }
     33 
     34 // Deeper macro substitution
     35 #define M1 [[clang::fallthrough]]
     36 #ifdef __clang__
     37 #define M2 M1
     38 #else
     39 #define M2
     40 #endif
     41 
     42 #define WRONG_MACRO1 clang::fallthrough
     43 #define WRONG_MACRO2 [[clang::fallthrough]
     44 #define WRONG_MACRO3 [[clang::fall through]]
     45 #define WRONG_MACRO4 [[clang::fallthrough]]]
     46 
     47 int fallthrough_compatibility_macro_in_macro(int n) {
     48   switch (n) {
     49     case 0:
     50       n = n * 20;
     51     case 1: // expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert 'M1;' to silence this warning}} expected-note{{insert 'break;' to avoid fall-through}}
     52                                                                           // there was an idea that this ^ should be M2
     53       ;
     54   }
     55   return n;
     56 }
     57 
     58 #undef M1
     59 #undef M2
     60 #undef COMPATIBILITY_FALLTHROUGH
     61 #undef COMMAND_LINE_FALLTHROUGH
     62 
     63 int fallthrough_compatibility_macro_undefined(int n) {
     64   switch (n) {
     65     case 0:
     66       n = n * 20;
     67     case 1: // expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert '[[clang::fallthrough]];' to silence this warning}} expected-note{{insert 'break;' to avoid fall-through}}
     68       ;
     69   }
     70 #define TOO_LATE [[clang::fallthrough]]
     71   return n;
     72 }
     73 #undef TOO_LATE
     74 
     75 #define MACRO_WITH_HISTORY 11111111
     76 #undef MACRO_WITH_HISTORY
     77 #define MACRO_WITH_HISTORY [[clang::fallthrough]]
     78 #undef MACRO_WITH_HISTORY
     79 #define MACRO_WITH_HISTORY 2222222
     80 
     81 int fallthrough_compatibility_macro_history(int n) {
     82   switch (n) {
     83     case 0:
     84       n = n * 20;
     85 #undef MACRO_WITH_HISTORY
     86     case 1: // expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert '[[clang::fallthrough]];' to silence this warning}} expected-note{{insert 'break;' to avoid fall-through}}
     87       ;
     88 #define MACRO_WITH_HISTORY [[clang::fallthrough]]
     89   }
     90   return n;
     91 }
     92 
     93 #undef MACRO_WITH_HISTORY
     94 #define MACRO_WITH_HISTORY 11111111
     95 #undef MACRO_WITH_HISTORY
     96 #define MACRO_WITH_HISTORY [[clang::fallthrough]]
     97 #undef MACRO_WITH_HISTORY
     98 #define MACRO_WITH_HISTORY 2222222
     99 #undef MACRO_WITH_HISTORY
    100 
    101 int fallthrough_compatibility_macro_history2(int n) {
    102   switch (n) {
    103     case 0:
    104       n = n * 20;
    105 #define MACRO_WITH_HISTORY [[clang::fallthrough]]
    106     case 1: // expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert 'MACRO_WITH_HISTORY;' to silence this warning}} expected-note{{insert 'break;' to avoid fall-through}}
    107       ;
    108 #undef MACRO_WITH_HISTORY
    109 #define MACRO_WITH_HISTORY 3333333
    110 #undef MACRO_WITH_HISTORY
    111 #define MACRO_WITH_HISTORY 4444444
    112 #undef MACRO_WITH_HISTORY
    113 #define MACRO_WITH_HISTORY 5555555
    114   }
    115   return n;
    116 }
    117 
    118 template<const int N>
    119 int fallthrough_compatibility_macro_history_template(int n) {
    120   switch (N * n) {
    121     case 0:
    122       n = n * 20;
    123 #define MACRO_WITH_HISTORY2 [[clang::fallthrough]]
    124     case 1: // expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert 'MACRO_WITH_HISTORY2;' to silence this warning}} expected-note{{insert 'break;' to avoid fall-through}}
    125       ;
    126 #undef MACRO_WITH_HISTORY2
    127 #define MACRO_WITH_HISTORY2 3333333
    128   }
    129   return n;
    130 }
    131 
    132 #undef MACRO_WITH_HISTORY2
    133 #define MACRO_WITH_HISTORY2 4444444
    134 #undef MACRO_WITH_HISTORY2
    135 #define MACRO_WITH_HISTORY2 5555555
    136 
    137 void f() {
    138   fallthrough_compatibility_macro_history_template<1>(0); // expected-note{{in instantiation of function template specialization 'fallthrough_compatibility_macro_history_template<1>' requested here}}
    139 }
    140