Clang Project

clang_source_code/test/Sema/sign-compare-enum.c
1// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -fsyntax-only -verify -Wsign-compare %s
2// RUN: %clang_cc1 -triple=x86_64-pc-win32 -fsyntax-only -verify -Wsign-compare %s
3// RUN: %clang_cc1 -x c++ -triple=x86_64-pc-linux-gnu -fsyntax-only -verify -Wsign-compare %s
4// RUN: %clang_cc1 -x c++ -triple=x86_64-pc-win32 -fsyntax-only -verify -Wsign-compare %s
5
6// Check that -Wsign-compare is off by default.
7// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -fsyntax-only -verify -DSILENCE %s
8
9#ifdef SILENCE
10// expected-no-diagnostics
11#endif
12
13enum PosEnum {
14  A_a = 0,
15  A_b = 1,
16  A_c = 10
17};
18
19static const int message[] = {0, 1};
20
21int test_pos(enum PosEnum a) {
22  if (a < 2)
23    return 0;
24
25  // No warning, except in Windows C mode, where PosEnum is 'int' and it can
26  // take on any value according to the C standard.
27#if !defined(SILENCE) && defined(_WIN32) && !defined(__cplusplus)
28  // expected-warning@+2 {{comparison of integers of different signs}}
29#endif
30  if (a < 2U)
31    return 0;
32
33  unsigned uv = 2;
34#if !defined(SILENCE) && defined(_WIN32) && !defined(__cplusplus)
35  // expected-warning@+2 {{comparison of integers of different signs}}
36#endif
37  if (a < uv)
38    return 1;
39
40#if !defined(SILENCE) && defined(_WIN32) && !defined(__cplusplus)
41  // expected-warning@+2 {{comparison of integers of different signs}}
42#endif
43  if (a < sizeof(message)/sizeof(message[0]))
44    return 0;
45  return 4;
46}
47
48enum NegEnum {
49  NE_a = -1,
50  NE_b = 0,
51  NE_c = 10
52};
53
54int test_neg(enum NegEnum a) {
55  if (a < 2)
56    return 0;
57
58#ifndef SILENCE
59  // expected-warning@+2 {{comparison of integers of different signs}}
60#endif
61  if (a < 2U)
62    return 0;
63
64  unsigned uv = 2;
65#ifndef SILENCE
66  // expected-warning@+2 {{comparison of integers of different signs}}
67#endif
68  if (a < uv)
69    return 1;
70
71#ifndef SILENCE
72  // expected-warning@+2 {{comparison of integers of different signs}}
73#endif
74  if (a < sizeof(message)/sizeof(message[0]))
75    return 0;
76  return 4;
77}
78