reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306
  307
  308
  309
  310
  311
  312
  313
  314
  315
  316
  317
  318
  319
  320
  321
  322
  323
  324
  325
  326
  327
  328
  329
  330
  331
  332
  333
  334
  335
  336
  337
  338
  339
  340
  341
  342
  343
  344
  345
  346
  347
  348
  349
  350
  351
  352
  353
  354
  355
  356
  357
  358
  359
  360
  361
  362
  363
  364
  365
  366
  367
  368
  369
  370
  371
  372
  373
  374
  375
  376
  377
  378
  379
  380
  381
  382
  383
  384
  385
  386
  387
  388
  389
  390
  391
  392
  393
  394
  395
  396
  397
  398
  399
  400
  401
  402
  403
  404
  405
  406
  407
  408
  409
  410
  411
  412
  413
  414
  415
  416
  417
  418
  419
  420
  421
  422
  423
  424
  425
  426
  427
  428
  429
  430
  431
  432
  433
  434
  435
  436
  437
  438
  439
  440
  441
  442
// Force x86-64 because some of our heuristics are actually based
// on integer sizes.

// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -pedantic -verify -Wsign-compare -Wtautological-constant-in-range-compare -std=c++11 %s
// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -pedantic -verify -Wsign-compare -Wtype-limits -std=c++11 %s

int test0(long a, unsigned long b) {
  enum EnumA {A};
  enum EnumB {B};
  enum EnumC {C = 0x10000};
  return
         // (a,b)
         (a == (unsigned long) b) +  // expected-warning {{comparison of integers of different signs}}
         (a == (unsigned int) b) +
         (a == (unsigned short) b) +
         (a == (unsigned char) b) +
         ((long) a == b) +  // expected-warning {{comparison of integers of different signs}}
         ((int) a == b) +  // expected-warning {{comparison of integers of different signs}}
         ((short) a == b) +  // expected-warning {{comparison of integers of different signs}}
         ((signed char) a == b) +  // expected-warning {{comparison of integers of different signs}}
         ((long) a == (unsigned long) b) +  // expected-warning {{comparison of integers of different signs}}
         ((int) a == (unsigned int) b) +  // expected-warning {{comparison of integers of different signs}}
         ((short) a == (unsigned short) b) +
         ((signed char) a == (unsigned char) b) +
         (a < (unsigned long) b) +  // expected-warning {{comparison of integers of different signs}}
         (a < (unsigned int) b) +
         (a < (unsigned short) b) +
         (a < (unsigned char) b) +
         ((long) a < b) +  // expected-warning {{comparison of integers of different signs}}
         ((int) a < b) +  // expected-warning {{comparison of integers of different signs}}
         ((short) a < b) +  // expected-warning {{comparison of integers of different signs}}
         ((signed char) a < b) +  // expected-warning {{comparison of integers of different signs}}
         ((long) a < (unsigned long) b) +  // expected-warning {{comparison of integers of different signs}}
         ((int) a < (unsigned int) b) +  // expected-warning {{comparison of integers of different signs}}
         ((short) a < (unsigned short) b) +
         ((signed char) a < (unsigned char) b) +

         // (A,b)
         (A == (unsigned long) b) +
         (A == (unsigned int) b) +
         (A == (unsigned short) b) +
         (A == (unsigned char) b) +
         ((long) A == b) +
         ((int) A == b) +
         ((short) A == b) +
         ((signed char) A == b) +
         ((long) A == (unsigned long) b) +
         ((int) A == (unsigned int) b) +
         ((short) A == (unsigned short) b) +
         ((signed char) A == (unsigned char) b) +
         (A < (unsigned long) b) +
         (A < (unsigned int) b) +
         (A < (unsigned short) b) +
         (A < (unsigned char) b) +
         ((long) A < b) +
         ((int) A < b) +
         ((short) A < b) +
         ((signed char) A < b) +
         ((long) A < (unsigned long) b) +
         ((int) A < (unsigned int) b) +
         ((short) A < (unsigned short) b) +
         ((signed char) A < (unsigned char) b) +

         // (a,B)
         (a == (unsigned long) B) +
         (a == (unsigned int) B) +
         (a == (unsigned short) B) +
         (a == (unsigned char) B) +
         ((long) a == B) +
         ((int) a == B) +
         ((short) a == B) +
         ((signed char) a == B) +
         ((long) a == (unsigned long) B) +
         ((int) a == (unsigned int) B) +
         ((short) a == (unsigned short) B) +
         ((signed char) a == (unsigned char) B) +
         (a < (unsigned long) B) +  // expected-warning {{comparison of unsigned expression < 0 is always false}}
         (a < (unsigned int) B) +
         (a < (unsigned short) B) +
         (a < (unsigned char) B) +
         ((long) a < B) +
         ((int) a < B) +
         ((short) a < B) +
         ((signed char) a < B) +
         ((long) a < (unsigned long) B) +  // expected-warning {{comparison of unsigned expression < 0 is always false}}
         ((int) a < (unsigned int) B) +  // expected-warning {{comparison of unsigned expression < 0 is always false}}
         ((short) a < (unsigned short) B) +
         ((signed char) a < (unsigned char) B) +

         // (C,b)
         (C == (unsigned long) b) +
         (C == (unsigned int) b) +
         (C == (unsigned short) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned short' is always false}}
         (C == (unsigned char) b) +  // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned char' is always false}}
         ((long) C == b) +
         ((int) C == b) +
         ((short) C == b) +
         ((signed char) C == b) +
         ((long) C == (unsigned long) b) +
         ((int) C == (unsigned int) b) +
         ((short) C == (unsigned short) b) +
         ((signed char) C == (unsigned char) b) +
         (C < (unsigned long) b) +
         (C < (unsigned int) b) +
         (C < (unsigned short) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned short' is always false}}
         (C < (unsigned char) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned char' is always false}}
         ((long) C < b) +
         ((int) C < b) +
         ((short) C < b) +
         ((signed char) C < b) +
         ((long) C < (unsigned long) b) +
         ((int) C < (unsigned int) b) +
         ((short) C < (unsigned short) b) +
         ((signed char) C < (unsigned char) b) +

         // (a,C)
         (a == (unsigned long) C) +
         (a == (unsigned int) C) +
         (a == (unsigned short) C) +
         (a == (unsigned char) C) +
         ((long) a == C) +
         ((int) a == C) +
         ((short) a == C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'short' is always false}}
         ((signed char) a == C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'signed char' is always false}}
         ((long) a == (unsigned long) C) +
         ((int) a == (unsigned int) C) +
         ((short) a == (unsigned short) C) +
         ((signed char) a == (unsigned char) C) +
         (a < (unsigned long) C) +  // expected-warning {{comparison of integers of different signs}}
         (a < (unsigned int) C) +
         (a < (unsigned short) C) +
         (a < (unsigned char) C) +
         ((long) a < C) +
         ((int) a < C) +
         ((short) a < C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'short' is always true}}
         ((signed char) a < C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'signed char' is always true}}
         ((long) a < (unsigned long) C) +  // expected-warning {{comparison of integers of different signs}}
         ((int) a < (unsigned int) C) +  // expected-warning {{comparison of integers of different signs}}
         ((short) a < (unsigned short) C) +
         ((signed char) a < (unsigned char) C) +

         // (0x80000,b)
         (0x80000 == (unsigned long) b) +
         (0x80000 == (unsigned int) b) +
         (0x80000 == (unsigned short) b) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned short' is always false}}
         (0x80000 == (unsigned char) b) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned char' is always false}}
         ((long) 0x80000 == b) +
         ((int) 0x80000 == b) +
         ((short) 0x80000 == b) +
         ((signed char) 0x80000 == b) +
         ((long) 0x80000 == (unsigned long) b) +
         ((int) 0x80000 == (unsigned int) b) +
         ((short) 0x80000 == (unsigned short) b) +
         ((signed char) 0x80000 == (unsigned char) b) +
         (0x80000 < (unsigned long) b) +
         (0x80000 < (unsigned int) b) +
         (0x80000 < (unsigned short) b) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned short' is always false}}
         (0x80000 < (unsigned char) b) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned char' is always false}}
         ((long) 0x80000 < b) +
         ((int) 0x80000 < b) +
         ((short) 0x80000 < b) +
         ((signed char) 0x80000 < b) +
         ((long) 0x80000 < (unsigned long) b) +
         ((int) 0x80000 < (unsigned int) b) +
         ((short) 0x80000 < (unsigned short) b) +
         ((signed char) 0x80000 < (unsigned char) b) +

         // (a,0x80000)
         (a == (unsigned long) 0x80000) +
         (a == (unsigned int) 0x80000) +
         (a == (unsigned short) 0x80000) +
         (a == (unsigned char) 0x80000) +
         ((long) a == 0x80000) +
         ((int) a == 0x80000) +
         ((short) a == 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'short' is always false}}
         ((signed char) a == 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'signed char' is always false}}
         ((long) a == (unsigned long) 0x80000) +
         ((int) a == (unsigned int) 0x80000) +
         ((short) a == (unsigned short) 0x80000) +
         ((signed char) a == (unsigned char) 0x80000) +
         (a < (unsigned long) 0x80000) +  // expected-warning {{comparison of integers of different signs}}
         (a < (unsigned int) 0x80000) +
         (a < (unsigned short) 0x80000) +
         (a < (unsigned char) 0x80000) +
         ((long) a < 0x80000) +
         ((int) a < 0x80000) +
         ((short) a < 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'short' is always true}}
         ((signed char) a < 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'signed char' is always true}}
         ((long) a < (unsigned long) 0x80000) +  // expected-warning {{comparison of integers of different signs}}
         ((int) a < (unsigned int) 0x80000) +  // expected-warning {{comparison of integers of different signs}}
         ((short) a < (unsigned short) 0x80000) +
         ((signed char) a < (unsigned char) 0x80000) +

         10
    ;
}

int test1(int i) {
  enum en { zero };
  return i > zero;
}

enum E { e };
void test2(int i, void *vp) {
  if (&i == vp) { } // ok
  if (test1 == vp) { } // expected-warning{{equality comparison between function pointer and void pointer}}
  if (test1 == e) { } // expected-error{{comparison between pointer and integer}}
  if (vp < 0) { } // expected-error {{comparison between pointer and zero}}
  if (test1 < e) { } // expected-error{{comparison between pointer and integer}}
}

// PR7536
static const unsigned int kMax = 0;
int pr7536() {
  return (kMax > 0);
}

// -Wsign-compare should not warn when ?: operands have different signedness.
// This will be caught by -Wsign-conversion
void test3() {
  unsigned long a;
  signed long b;
  (void) (true ? a : b);
  (void) (true ? (unsigned int)a : (signed int)b);
  (void) (true ? b : a);
  (void) (true ? (unsigned char)b : (signed char)a);
}

// Test comparison of short to unsigned.  If tautological compare does not
// trigger, then the signed comparison warning will.
void test4(short s) {
  // A is max short plus 1.  All zero and positive shorts are smaller than it.
  // All negative shorts are cast towards the max unsigned range.  Relation
  // comparisons are possible, but equality comparisons are tautological.
  const unsigned A = 32768;
  void (s < A); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
  void (s > A); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
  void (s <= A); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
  void (s >= A); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}

  void (s == A); // expected-warning{{comparison of constant 32768 with expression of type 'short' is always false}}
  void (s != A); // expected-warning{{comparison of constant 32768 with expression of type 'short' is always true}}

  // When negative one is converted to an unsigned value, it becomes the max
  // unsigned.  Likewise, a negative one short can also be converted to max
  // unsigned.
  const unsigned B = -1;
  void (s < B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
  void (s > B); // expected-warning{{comparison 'short' > 4294967295 is always false}}
  void (s <= B); // expected-warning{{comparison 'short' <= 4294967295 is always true}}
  void (s >= B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
  void (s == B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
  void (s != B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}

}

void test5(bool b) {
  (void) (b < -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always false}}
  (void) (b > -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always true}}
  (void) (b == -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always false}}
  (void) (b != -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always true}}
  (void) (b <= -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always false}}
  (void) (b >= -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always true}}

  (void) (b < -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always false}}
  (void) (b > -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always true}}
  (void) (b == -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always false}}
  (void) (b != -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always true}}
  (void) (b <= -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always false}}
  (void) (b >= -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always true}}

  (void) (b < 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always true}}
  (void) (b > 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always false}}
  (void) (b == 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always false}}
  (void) (b != 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always true}}
  (void) (b <= 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always true}}
  (void) (b >= 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always false}}

  (void) (b < 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always true}}
  (void) (b > 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always false}}
  (void) (b == 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always false}}
  (void) (b != 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always true}}
  (void) (b <= 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always true}}
  (void) (b >= 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always false}}
}

void test6(signed char sc) {
  (void)(sc < 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}}
  (void)(sc > 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}}
  (void)(sc <= 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}}
  (void)(sc >= 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}}
  (void)(sc == 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}}
  (void)(sc != 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}}

  (void)(200 < sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}}
  (void)(200 > sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}}
  (void)(200 <= sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}}
  (void)(200 >= sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}}
  (void)(200 == sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}}
  (void)(200 != sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}}
}

// Test many signedness combinations.
void test7(unsigned long other) {
  // Common unsigned, other unsigned, constant unsigned
  (void)((unsigned)other != (unsigned long)(0x1ffffffff)); // expected-warning{{true}}
  (void)((unsigned)other != (unsigned long)(0xffffffff));
  (void)((unsigned long)other != (unsigned)(0x1ffffffff));
  (void)((unsigned long)other != (unsigned)(0xffffffff));

  // Common unsigned, other signed, constant unsigned
  (void)((int)other != (unsigned long)(0xffffffffffffffff)); // expected-warning{{different signs}}
  (void)((int)other != (unsigned long)(0x00000000ffffffff)); // expected-warning{{true}}
  (void)((int)other != (unsigned long)(0x000000000fffffff));
  (void)((int)other < (unsigned long)(0x00000000ffffffff));  // expected-warning{{different signs}}
  (void)((int)other == (unsigned)(0x800000000));

  // Common unsigned, other unsigned, constant signed
  (void)((unsigned long)other != (int)(0xffffffff));  // expected-warning{{different signs}}

  // Common unsigned, other signed, constant signed
  // Should not be possible as the common type should also be signed.

  // Common signed, other signed, constant signed
  (void)((int)other != (long)(0xffffffff));  // expected-warning{{true}}
  (void)((int)other != (long)(0xffffffff00000000));  // expected-warning{{true}}
  (void)((int)other != (long)(0xfffffff));
  (void)((int)other != (long)(0xfffffffff0000000));

  // Common signed, other signed, constant unsigned
  (void)((int)other != (unsigned char)(0xffff));
  (void)((int)other != (unsigned char)(0xff));

  // Common signed, other unsigned, constant signed
  (void)((unsigned char)other != (int)(0xff));
  (void)((unsigned char)other != (int)(0xffff));  // expected-warning{{true}}

  // Common signed, other unsigned, constant unsigned
  (void)((unsigned char)other != (unsigned short)(0xff));
  (void)((unsigned char)other != (unsigned short)(0x100)); // expected-warning{{true}}
  (void)((unsigned short)other != (unsigned char)(0xff));
}

void test8(int x) {
  enum E {
    Negative = -1,
    Positive = 1
  };

  (void)((E)x == 1);
  (void)((E)x == -1);
}

void test9(int x) {
  enum E : int {
    Positive = 1
  };
  (void)((E)x == 1);
}

namespace templates {
  template<class T> T max();

  template<> constexpr int max<int>() { return 2147483647; };

  template<typename T>
  bool less_than_max(short num, T value) {
    const T vmax = max<T>();
    return (vmax >= num);  // no warning
  }

  template<typename T>
  bool less_than_max(short num) {
    // This should trigger one warning on the template pattern, and not a
    // warning per specialization.
    return num < max<int>();  // expected-warning{{comparison of constant 2147483647 with expression of type 'short' is always true}}
  }

  void test10(short num, int x) {
    less_than_max(num, x);
    less_than_max<int>(num);
    less_than_max<long>(num);
    less_than_max<short>(num);
  }

  template<typename T>
  inline bool less_than_zero(T num, T value) {
    return num < 0;  // no warning
  }

  template<typename T>
  inline bool less_than_zero(unsigned num) {
    // This should trigger one warning on the template pattern, and not a
    // warning per specialization.
    return num < 0;  // expected-warning{{comparison of unsigned expression < 0 is always false}}
  }

  void test11(unsigned num) {
    less_than_zero(num, num);
    less_than_zero<int>(num);
    less_than_zero<long>(num);
    less_than_zero<short>(num);
  }

  template<unsigned n> bool compare(unsigned k) { return k >= n; }

  void test12() {
    compare<0>(42);
  }

  struct A { static int x; };
  struct B { static int x; };
  typedef A otherA;

  template <typename T>
  void testx() {
    if (A::x == T::x &&  // no warning
        A::x == otherA::x)  // expected-warning{{self-comparison always evaluates to true}}
      return;
  }

  void test13() {
    testx<A>();
    testx<B>();
  }
}

namespace tautological_enum {
  enum E { a, b, c } e;

  // FIXME: We should warn about constructing this out-of-range numeration value.
  const E invalid = (E)-1;
  // ... but we should not warn about comparing against it.
  bool x = e == invalid;

  // We should not warn about relational comparisons for enumerators, even if
  // they're tautological.
  bool y = e >= a && e <= b;
  const E first_in_range = a;
  const E last_in_range = b;
  bool z = e >= first_in_range && e <= last_in_range;
}