Home | History | Annotate | Download | only in Misc
      1 // RUN: %clang_cc1 -fsyntax-only %s 2>&1 | FileCheck %s -strict-whitespace
      2 
      3 #define M1(x) x
      4 #define M2 1;
      5 void foo() {
      6   M1(
      7     M2);
      8   // CHECK: {{.*}}:7:{{[0-9]+}}: warning: expression result unused
      9   // CHECK: {{.*}}:4:{{[0-9]+}}: note: expanded from macro 'M2'
     10   // CHECK: {{.*}}:3:{{[0-9]+}}: note: expanded from macro 'M1'
     11 }
     12 
     13 #define A(x) x
     14 #define B(x) A(x)
     15 #define C(x) B(x)
     16 void bar() {
     17   C(1);
     18   // CHECK: {{.*}}:17:5: warning: expression result unused
     19   // CHECK: {{.*}}:15:16: note: expanded from macro 'C'
     20   // CHECK: {{.*}}:14:16: note: expanded from macro 'B'
     21   // CHECK: {{.*}}:13:14: note: expanded from macro 'A'
     22 }
     23 
     24 // rdar://7597492
     25 #define sprintf(str, A, B) \
     26 __builtin___sprintf_chk (str, 0, 42, A, B)
     27 
     28 void baz(char *Msg) {
     29   sprintf(Msg,  "  sizeof FoooLib            : =%3u\n",   12LL);
     30 }
     31 
     32 
     33 // PR9279: comprehensive tests for multi-level macro back traces
     34 #define macro_args1(x) x
     35 #define macro_args2(x) macro_args1(x)
     36 #define macro_args3(x) macro_args2(x)
     37 
     38 #define macro_many_args1(x, y, z) y
     39 #define macro_many_args2(x, y, z) macro_many_args1(x, y, z)
     40 #define macro_many_args3(x, y, z) macro_many_args2(x, y, z)
     41 
     42 void test() {
     43   macro_args3(11);
     44   // CHECK: {{.*}}:43:15: warning: expression result unused
     45   // Also check that the 'caret' printing agrees with the location here where
     46   // its easy to FileCheck.
     47   // CHECK-NEXT:      macro_args3(11);
     48   // CHECK-NEXT: {{^              \^~}}
     49   // CHECK: {{.*}}:36:36: note: expanded from macro 'macro_args3'
     50   // CHECK: {{.*}}:35:36: note: expanded from macro 'macro_args2'
     51   // CHECK: {{.*}}:34:24: note: expanded from macro 'macro_args1'
     52 
     53   macro_many_args3(
     54     1,
     55     2,
     56     3);
     57   // CHECK: {{.*}}:55:5: warning: expression result unused
     58   // CHECK: {{.*}}:40:55: note: expanded from macro 'macro_many_args3'
     59   // CHECK: {{.*}}:39:55: note: expanded from macro 'macro_many_args2'
     60   // CHECK: {{.*}}:38:35: note: expanded from macro 'macro_many_args1'
     61 
     62   macro_many_args3(
     63     1,
     64     M2,
     65     3);
     66   // CHECK: {{.*}}:64:5: warning: expression result unused
     67   // CHECK: {{.*}}:4:12: note: expanded from macro 'M2'
     68   // CHECK: {{.*}}:40:55: note: expanded from macro 'macro_many_args3'
     69   // CHECK: {{.*}}:39:55: note: expanded from macro 'macro_many_args2'
     70   // CHECK: {{.*}}:38:35: note: expanded from macro 'macro_many_args1'
     71 
     72   macro_many_args3(
     73     1,
     74     macro_args2(22),
     75     3);
     76   // CHECK: {{.*}}:74:17: warning: expression result unused
     77   // This caret location needs to be printed *inside* a different macro's
     78   // arguments.
     79   // CHECK-NEXT:        macro_args2(22),
     80   // CHECK-NEXT: {{^                \^~}}
     81   // CHECK: {{.*}}:35:36: note: expanded from macro 'macro_args2'
     82   // CHECK: {{.*}}:34:24: note: expanded from macro 'macro_args1'
     83   // CHECK: {{.*}}:40:55: note: expanded from macro 'macro_many_args3'
     84   // CHECK: {{.*}}:39:55: note: expanded from macro 'macro_many_args2'
     85   // CHECK: {{.*}}:38:35: note: expanded from macro 'macro_many_args1'
     86 }
     87 
     88 #define variadic_args1(x, y, ...) y
     89 #define variadic_args2(x, ...) variadic_args1(x, __VA_ARGS__)
     90 #define variadic_args3(x, y, ...) variadic_args2(x, y, __VA_ARGS__)
     91 
     92 void test2() {
     93   variadic_args3(1, 22, 3, 4);
     94   // CHECK: {{.*}}:93:21: warning: expression result unused
     95   // CHECK-NEXT:      variadic_args3(1, 22, 3, 4);
     96   // CHECK-NEXT: {{^                    \^~}}
     97   // CHECK: {{.*}}:90:53: note: expanded from macro 'variadic_args3'
     98   // CHECK: {{.*}}:89:50: note: expanded from macro 'variadic_args2'
     99   // CHECK: {{.*}}:88:35: note: expanded from macro 'variadic_args1'
    100 }
    101 
    102 #define variadic_pasting_args1(x, y, z) y
    103 #define variadic_pasting_args2(x, ...) variadic_pasting_args1(x ## __VA_ARGS__)
    104 #define variadic_pasting_args2a(x, y, ...) variadic_pasting_args1(x, y ## __VA_ARGS__)
    105 #define variadic_pasting_args3(x, y, ...) variadic_pasting_args2(x, y, __VA_ARGS__)
    106 #define variadic_pasting_args3a(x, y, ...) variadic_pasting_args2a(x, y, __VA_ARGS__)
    107 
    108 void test3() {
    109   variadic_pasting_args3(1, 2, 3, 4);
    110   // CHECK: {{.*}}:109:32: warning: expression result unused
    111   // CHECK: {{.*}}:105:72: note: expanded from macro 'variadic_pasting_args3'
    112   // CHECK: {{.*}}:103:68: note: expanded from macro 'variadic_pasting_args2'
    113   // CHECK: {{.*}}:102:41: note: expanded from macro 'variadic_pasting_args1'
    114 
    115   variadic_pasting_args3a(1, 2, 3, 4);
    116   // CHECK:        {{.*}}:115:3: warning: expression result unused
    117   // CHECK-NEXT:     variadic_pasting_args3a(1, 2, 3, 4);
    118   // CHECK-NEXT: {{  \^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}}
    119   // CHECK:        {{.*}}:106:44: note: expanded from macro 'variadic_pasting_args3a'
    120   // CHECK-NEXT:   #define variadic_pasting_args3a(x, y, ...) variadic_pasting_args2a(x, y, __VA_ARGS__)
    121   // CHECK-NEXT: {{                                           \^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}}
    122   // CHECK:        {{.*}}:104:70: note: expanded from macro 'variadic_pasting_args2a'
    123   // CHECK-NEXT:   #define variadic_pasting_args2a(x, y, ...) variadic_pasting_args1(x, y ## __VA_ARGS__)
    124   // CHECK-NEXT: {{                                                                     \^~~~~~~~~~~~~~~~}}
    125   // CHECK:        {{.*}}:102:41: note: expanded from macro 'variadic_pasting_args1'
    126   // CHECK-NEXT:   #define variadic_pasting_args1(x, y, z) y
    127   // CHECK-NEXT: {{                                        \^}}
    128 }
    129 
    130 #define BAD_CONDITIONAL_OPERATOR (2<3)?2:3
    131 int test4 = BAD_CONDITIONAL_OPERATOR+BAD_CONDITIONAL_OPERATOR;
    132 // CHECK:         {{.*}}:130:39: note: expanded from macro 'BAD_CONDITIONAL_OPERATOR'
    133 // CHECK-NEXT:    #define BAD_CONDITIONAL_OPERATOR (2<3)?2:3
    134 // CHECK-NEXT: {{^                                      \^}}
    135 // CHECK:         {{.*}}:130:39: note: expanded from macro 'BAD_CONDITIONAL_OPERATOR'
    136 // CHECK-NEXT:    #define BAD_CONDITIONAL_OPERATOR (2<3)?2:3
    137 // CHECK-NEXT: {{^                                      \^}}
    138 // CHECK:         {{.*}}:130:39: note: expanded from macro 'BAD_CONDITIONAL_OPERATOR'
    139 // CHECK-NEXT:    #define BAD_CONDITIONAL_OPERATOR (2<3)?2:3
    140 // CHECK-NEXT: {{^                                 ~~~~~\^~~~}}
    141 
    142 #define QMARK ?
    143 #define TWOL (2<
    144 #define X 1+TWOL 3) QMARK 4:5
    145 int x = X;
    146 // CHECK:         {{.*}}:145:9: note: place parentheses around the '+' expression to silence this warning
    147 // CHECK-NEXT:    int x = X;
    148 // CHECK-NEXT: {{^        \^}}
    149 // CHECK-NEXT:    {{.*}}:144:21: note: expanded from macro 'X'
    150 // CHECK-NEXT:    #define X 1+TWOL 3) QMARK 4:5
    151 // CHECK-NEXT: {{^          ~~~~~~~~~ \^}}
    152 // CHECK-NEXT:    {{.*}}:142:15: note: expanded from macro 'QMARK'
    153 // CHECK-NEXT:    #define QMARK ?
    154 // CHECK-NEXT: {{^              \^}}
    155 // CHECK-NEXT:    {{.*}}:145:9: note: place parentheses around the '?:' expression to evaluate it first
    156 // CHECK-NEXT:    int x = X;
    157 // CHECK-NEXT: {{^        \^}}
    158 // CHECK-NEXT:    {{.*}}:144:21: note: expanded from macro 'X'
    159 // CHECK-NEXT:    #define X 1+TWOL 3) QMARK 4:5
    160 // CHECK-NEXT: {{^            ~~~~~~~~\^~~~~~~~~}}
    161 
    162 #define ONEPLUS 1+
    163 #define Y ONEPLUS (2<3) QMARK 4:5
    164 int y = Y;
    165 // CHECK:         {{.*}}:164:9: warning: operator '?:' has lower precedence than '+'; '+' will be evaluated first
    166 // CHECK-NEXT:    int y = Y;
    167 // CHECK-NEXT: {{^        \^}}
    168 // CHECK-NEXT:    {{.*}}:163:25: note: expanded from macro 'Y'
    169 // CHECK-NEXT:    #define Y ONEPLUS (2<3) QMARK 4:5
    170 // CHECK-NEXT: {{^          ~~~~~~~~~~~~~ \^}}
    171 // CHECK-NEXT:    {{.*}}:142:15: note: expanded from macro 'QMARK'
    172 // CHECK-NEXT:    #define QMARK ?
    173 // CHECK-NEXT: {{^              \^}}
    174 
    175 // PR14399
    176 void iequals(int,int,int);
    177 void foo_aa(char* s)
    178 {
    179 #define /* */ BARC(c, /* */b, a) (a + b ? c : c)
    180   iequals(__LINE__, BARC(123, (456 < 345), 789), 8);
    181 }
    182 // CHECK:         {{.*}}:180:21: warning: operator '?:' has lower precedence than '+'
    183 // CHECK-NEXT:      iequals(__LINE__, BARC(123, (456 < 345), 789), 8);
    184 // CHECK-NEXT: {{^                    \^~~~~~~~~~~~~~~~~~~~~~~~~~~}}
    185 // CHECK-NEXT:    {{.*}}:179:41: note: expanded from macro 'BARC'
    186 // CHECK-NEXT:    #define /* */ BARC(c, /* */b, a) (a + b ? c : c)
    187 // CHECK-NEXT: {{^                                  ~~~~~ \^}}
    188 
    189 #define APPEND2(NUM, SUFF) -1 != NUM ## SUFF
    190 #define APPEND(NUM, SUFF) APPEND2(NUM, SUFF)
    191 #define UTARG_MAX_U APPEND (MAX_UINT, UL)
    192 #define MAX_UINT 18446744073709551615
    193 #if UTARG_MAX_U
    194 #endif
    195 
    196 // CHECK:         {{.*}}:193:5: warning: left side of operator converted from negative value to unsigned: -1 to 18446744073709551615
    197 // CHECK-NEXT:    #if UTARG_MAX_U
    198 // CHECK-NEXT: {{^    \^~~~~~~~~~~}}
    199 // CHECK-NEXT:    {{.*}}:191:21: note: expanded from macro 'UTARG_MAX_U'
    200 // CHECK-NEXT:    #define UTARG_MAX_U APPEND (MAX_UINT, UL)
    201 // CHECK-NEXT: {{^                    \^~~~~~~~~~~~~~~~~~~~~}}
    202 // CHECK-NEXT:    {{.*}}:190:27: note: expanded from macro 'APPEND'
    203 // CHECK-NEXT:    #define APPEND(NUM, SUFF) APPEND2(NUM, SUFF)
    204 // CHECK-NEXT: {{^                          \^~~~~~~~~~~~~~~~~~}}
    205 // CHECK-NEXT:    {{.*}}:189:31: note: expanded from macro 'APPEND2'
    206 // CHECK-NEXT:    #define APPEND2(NUM, SUFF) -1 != NUM ## SUFF
    207 // CHECK-NEXT: {{^                           ~~ \^  ~~~~~~~~~~~}}
    208 
    209 unsigned long strlen_test(const char *s);
    210 #define __darwin_obsz(object) __builtin_object_size (object, 1)
    211 #define sprintf2(str, ...) \
    212   __builtin___sprintf_chk (str, 0, __darwin_obsz(str), __VA_ARGS__)
    213 #define Cstrlen(a)  strlen_test(a)
    214 #define Csprintf    sprintf2
    215 void f(char* pMsgBuf, char* pKeepBuf) {
    216 Csprintf(pMsgBuf,"\nEnter minimum anagram length (2-%1d): ", Cstrlen(pKeepBuf));
    217 }
    218 // CHECK:         {{.*}}:216:62: warning: format specifies type 'int' but the argument has type 'unsigned long'
    219 // CHECK-NEXT:    Csprintf(pMsgBuf,"\nEnter minimum anagram length (2-%1d): ", Cstrlen(pKeepBuf));
    220 // CHECK-NEXT: {{^                                                    ~~~      \^}}
    221 // CHECK-NEXT: {{^                                                    %1lu}}
    222 // CHECK-NEXT:    {{.*}}:213:21: note: expanded from macro 'Cstrlen'
    223 // CHECK-NEXT:    #define Cstrlen(a)  strlen_test(a)
    224 // CHECK-NEXT: {{^                    \^}}
    225 // CHECK-NEXT:    {{.*}}:212:56: note: expanded from macro 'sprintf2'
    226 // CHECK-NEXT:      __builtin___sprintf_chk (str, 0, __darwin_obsz(str), __VA_ARGS__)
    227 // CHECK-NEXT: {{^                                                       \^}}
    228