File: macro-to-enum.cpp

package info (click to toggle)
llvm-toolchain-17 1%3A17.0.6-22
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,799,624 kB
  • sloc: cpp: 6,428,607; ansic: 1,383,196; asm: 793,408; python: 223,504; objc: 75,364; f90: 60,502; lisp: 33,869; pascal: 15,282; sh: 9,684; perl: 7,453; ml: 4,937; awk: 3,523; makefile: 2,889; javascript: 2,149; xml: 888; fortran: 619; cs: 573
file content (443 lines) | stat: -rw-r--r-- 15,762 bytes parent folder | download | duplicates (13)
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
443
// RUN: %check_clang_tidy -std=c++14-or-later %s modernize-macro-to-enum %t -- -- -I%S/Inputs/macro-to-enum -fno-delayed-template-parsing
// C++14 or later required for binary literals.

#if 1
#include "modernize-macro-to-enum.h"

// These macros are skipped due to being inside a conditional compilation block.
#define GOO_RED 1
#define GOO_GREEN 2
#define GOO_BLUE 3

#endif

// Macros expanding to expressions involving only literals are converted.
#define EXPR1 1 - 1
#define EXPR2 1 + 1
#define EXPR3 1 * 1
#define EXPR4 1 / 1
#define EXPR5 1 | 1
#define EXPR6 1 & 1
#define EXPR7 1 << 1
#define EXPR8 1 >> 1
#define EXPR9 1 % 2
#define EXPR10 1 ^ 1
#define EXPR11 (1 + (2))
#define EXPR12 ((1) + (2 + 0) + (1 * 1) + (1 / 1) + (1 | 1 ) + (1 & 1) + (1 << 1) + (1 >> 1) + (1 % 2) + (1 ^ 1))
// CHECK-MESSAGES: :[[@LINE-12]]:1: warning: replace macro with enum [modernize-macro-to-enum]
// CHECK-MESSAGES: :[[@LINE-13]]:9: warning: macro 'EXPR1' defines an integral constant; prefer an enum instead
// CHECK-MESSAGES: :[[@LINE-13]]:9: warning: macro 'EXPR2' defines an integral constant; prefer an enum instead
// CHECK-MESSAGES: :[[@LINE-13]]:9: warning: macro 'EXPR3' defines an integral constant; prefer an enum instead
// CHECK-MESSAGES: :[[@LINE-13]]:9: warning: macro 'EXPR4' defines an integral constant; prefer an enum instead
// CHECK-MESSAGES: :[[@LINE-13]]:9: warning: macro 'EXPR5' defines an integral constant; prefer an enum instead
// CHECK-MESSAGES: :[[@LINE-13]]:9: warning: macro 'EXPR6' defines an integral constant; prefer an enum instead
// CHECK-MESSAGES: :[[@LINE-13]]:9: warning: macro 'EXPR7' defines an integral constant; prefer an enum instead
// CHECK-MESSAGES: :[[@LINE-13]]:9: warning: macro 'EXPR8' defines an integral constant; prefer an enum instead
// CHECK-MESSAGES: :[[@LINE-13]]:9: warning: macro 'EXPR9' defines an integral constant; prefer an enum instead
// CHECK-MESSAGES: :[[@LINE-13]]:9: warning: macro 'EXPR10' defines an integral constant; prefer an enum instead
// CHECK-MESSAGES: :[[@LINE-13]]:9: warning: macro 'EXPR11' defines an integral constant; prefer an enum instead
// CHECK-MESSAGES: :[[@LINE-13]]:9: warning: macro 'EXPR12' defines an integral constant; prefer an enum instead
// CHECK-FIXES: enum {
// CHECK-FIXES-NEXT: EXPR1 = 1 - 1,
// CHECK-FIXES-NEXT: EXPR2 = 1 + 1,
// CHECK-FIXES-NEXT: EXPR3 = 1 * 1,
// CHECK-FIXES-NEXT: EXPR4 = 1 / 1,
// CHECK-FIXES-NEXT: EXPR5 = 1 | 1,
// CHECK-FIXES-NEXT: EXPR6 = 1 & 1,
// CHECK-FIXES-NEXT: EXPR7 = 1 << 1,
// CHECK-FIXES-NEXT: EXPR8 = 1 >> 1,
// CHECK-FIXES-NEXT: EXPR9 = 1 % 2,
// CHECK-FIXES-NEXT: EXPR10 = 1 ^ 1,
// CHECK-FIXES-NEXT: EXPR11 = (1 + (2)),
// CHECK-FIXES-NEXT: EXPR12 = ((1) + (2 + 0) + (1 * 1) + (1 / 1) + (1 | 1 ) + (1 & 1) + (1 << 1) + (1 >> 1) + (1 % 2) + (1 ^ 1))
// CHECK-FIXES-NEXT: };

#define RED 0xFF0000
#define GREEN 0x00FF00
#define BLUE 0x0000FF
// CHECK-MESSAGES: :[[@LINE-3]]:1: warning: replace macro with enum [modernize-macro-to-enum]
// CHECK-MESSAGES: :[[@LINE-4]]:9: warning: macro 'RED' defines an integral constant; prefer an enum instead
// CHECK-MESSAGES: :[[@LINE-4]]:9: warning: macro 'GREEN' defines an integral constant; prefer an enum instead
// CHECK-MESSAGES: :[[@LINE-4]]:9: warning: macro 'BLUE' defines an integral constant; prefer an enum instead
// CHECK-FIXES: enum {
// CHECK-FIXES-NEXT: RED = 0xFF0000,
// CHECK-FIXES-NEXT: GREEN = 0x00FF00,
// CHECK-FIXES-NEXT: BLUE = 0x0000FF
// CHECK-FIXES-NEXT: };

// Verify that comments are preserved.
#define CoordModeOrigin         0   /* relative to the origin */
#define CoordModePrevious       1   /* relative to previous point */
// CHECK-MESSAGES: :[[@LINE-2]]:1: warning: replace macro with enum
// CHECK-MESSAGES: :[[@LINE-3]]:9: warning: macro 'CoordModeOrigin' defines an integral constant; prefer an enum instead
// CHECK-MESSAGES: :[[@LINE-3]]:9: warning: macro 'CoordModePrevious' defines an integral constant; prefer an enum instead
// CHECK-FIXES: enum {
// CHECK-FIXES-NEXT: CoordModeOrigin =         0,   /* relative to the origin */
// CHECK-FIXES-NEXT: CoordModePrevious =       1   /* relative to previous point */
// CHECK-FIXES-NEXT: };

// Verify that multiline comments are preserved.
#define BadDrawable         9   /* parameter not a Pixmap or Window */
#define BadAccess           10  /* depending on context:
                                - key/button already grabbed
                                - attempt to free an illegal 
                                  cmap entry 
                                - attempt to store into a read-only 
                                  color map entry. */
                                // - attempt to modify the access control
                                //   list from other than the local host.
                                //
#define BadAlloc            11  /* insufficient resources */
// CHECK-MESSAGES: :[[@LINE-11]]:1: warning: replace macro with enum
// CHECK-MESSAGES: :[[@LINE-12]]:9: warning: macro 'BadDrawable' defines an integral constant; prefer an enum instead
// CHECK-MESSAGES: :[[@LINE-12]]:9: warning: macro 'BadAccess' defines an integral constant; prefer an enum instead
// CHECK-MESSAGES: :[[@LINE-4]]:9: warning: macro 'BadAlloc' defines an integral constant; prefer an enum instead
// CHECK-FIXES: enum {
// CHECK-FIXES-NEXT: BadDrawable =         9,   /* parameter not a Pixmap or Window */
// CHECK-FIXES-NEXT: BadAccess =           10,  /* depending on context:
// CHECK-FIXES-NEXT:                                 - key/button already grabbed
// CHECK-FIXES-NEXT:                                 - attempt to free an illegal 
// CHECK-FIXES-NEXT:                                   cmap entry 
// CHECK-FIXES-NEXT:                                 - attempt to store into a read-only 
// CHECK-FIXES-NEXT:                                   color map entry. */
// CHECK-FIXES-NEXT:                                 // - attempt to modify the access control
// CHECK-FIXES-NEXT:                                 //   list from other than the local host.
// CHECK-FIXES-NEXT:                                 //
// CHECK-FIXES-NEXT: BadAlloc =            11  /* insufficient resources */
// CHECK-FIXES-NEXT: };

// Undefining a macro invalidates adjacent macros
// from being considered as an enum.
#define REMOVED1 1
#define REMOVED2 2
#define REMOVED3 3
#undef REMOVED2
#define VALID1 1
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: replace macro with enum
// CHECK-MESSAGES: :[[@LINE-2]]:9: warning: macro 'VALID1' defines an integral constant; prefer an enum instead
// CHECK-FIXES: enum {
// CHECK-FIXES-NEXT: VALID1 = 1
// CHECK-FIXES-NEXT: };

#define UNDEF1 1
#define UNDEF2 2
#define UNDEF3 3

// Undefining a macro later invalidates the set of possible adjacent macros
// from being considered as an enum.
#undef UNDEF2

// Integral constants can have an optional sign
#define SIGNED1 +1
#define SIGNED2 -1
// CHECK-MESSAGES: :[[@LINE-2]]:1: warning: replace macro with enum
// CHECK-MESSAGES: :[[@LINE-3]]:9: warning: macro 'SIGNED1' defines an integral constant; prefer an enum instead
// CHECK-MESSAGES: :[[@LINE-3]]:9: warning: macro 'SIGNED2' defines an integral constant; prefer an enum instead
// CHECK-FIXES: enum {
// CHECK-FIXES-NEXT: SIGNED1 = +1,
// CHECK-FIXES-NEXT: SIGNED2 = -1
// CHECK-FIXES-NEXT: };

// Integral constants with bitwise negated values
#define UNOP1 ~0U
#define UNOP2 ~1U
// CHECK-MESSAGES: :[[@LINE-2]]:1: warning: replace macro with enum
// CHECK-MESSAGES: :[[@LINE-3]]:9: warning: macro 'UNOP1' defines an integral constant; prefer an enum instead
// CHECK-MESSAGES: :[[@LINE-3]]:9: warning: macro 'UNOP2' defines an integral constant; prefer an enum instead
// CHECK-FIXES: enum {
// CHECK-FIXES-NEXT: UNOP1 = ~0U,
// CHECK-FIXES-NEXT: UNOP2 = ~1U
// CHECK-FIXES-NEXT: };

// Integral constants in other bases and with suffixes are OK
#define BASE1 0777    // octal
#define BASE2 0xDEAD  // hexadecimal
#define BASE3 0b0011  // binary
#define SUFFIX1 +1U
#define SUFFIX2 -1L
#define SUFFIX3 +1UL
#define SUFFIX4 -1LL
#define SUFFIX5 +1ULL
// CHECK-MESSAGES: :[[@LINE-8]]:1: warning: replace macro with enum
// CHECK-MESSAGES: :[[@LINE-9]]:9: warning: macro 'BASE1' defines an integral constant; prefer an enum instead
// CHECK-MESSAGES: :[[@LINE-9]]:9: warning: macro 'BASE2' defines an integral constant; prefer an enum instead
// CHECK-MESSAGES: :[[@LINE-9]]:9: warning: macro 'BASE3' defines an integral constant; prefer an enum instead
// CHECK-MESSAGES: :[[@LINE-9]]:9: warning: macro 'SUFFIX1' defines an integral constant; prefer an enum instead
// CHECK-MESSAGES: :[[@LINE-9]]:9: warning: macro 'SUFFIX2' defines an integral constant; prefer an enum instead
// CHECK-MESSAGES: :[[@LINE-9]]:9: warning: macro 'SUFFIX3' defines an integral constant; prefer an enum instead
// CHECK-MESSAGES: :[[@LINE-9]]:9: warning: macro 'SUFFIX4' defines an integral constant; prefer an enum instead
// CHECK-MESSAGES: :[[@LINE-9]]:9: warning: macro 'SUFFIX5' defines an integral constant; prefer an enum instead
// CHECK-FIXES: enum {
// CHECK-FIXES-NEXT: BASE1 = 0777,    // octal
// CHECK-FIXES-NEXT: BASE2 = 0xDEAD,  // hexadecimal
// CHECK-FIXES-NEXT: BASE3 = 0b0011,  // binary
// CHECK-FIXES-NEXT: SUFFIX1 = +1U,
// CHECK-FIXES-NEXT: SUFFIX2 = -1L,
// CHECK-FIXES-NEXT: SUFFIX3 = +1UL,
// CHECK-FIXES-NEXT: SUFFIX4 = -1LL,
// CHECK-FIXES-NEXT: SUFFIX5 = +1ULL
// CHECK-FIXES-NEXT: };

// A limited form of constant expression is recognized: a parenthesized
// literal or a parenthesized literal with the unary operators +, - or ~.
#define PAREN1 (-1)
#define PAREN2 (1)
#define PAREN3 (+1)
#define PAREN4 (~1)
// CHECK-MESSAGES: :[[@LINE-4]]:1: warning: replace macro with enum
// CHECK-MESSAGES: :[[@LINE-5]]:9: warning: macro 'PAREN1' defines an integral constant; prefer an enum instead
// CHECK-MESSAGES: :[[@LINE-5]]:9: warning: macro 'PAREN2' defines an integral constant; prefer an enum instead
// CHECK-MESSAGES: :[[@LINE-5]]:9: warning: macro 'PAREN3' defines an integral constant; prefer an enum instead
// CHECK-MESSAGES: :[[@LINE-5]]:9: warning: macro 'PAREN4' defines an integral constant; prefer an enum instead
// CHECK-FIXES: enum {
// CHECK-FIXES-NEXT: PAREN1 = (-1),
// CHECK-FIXES-NEXT: PAREN2 = (1),
// CHECK-FIXES-NEXT: PAREN3 = (+1),
// CHECK-FIXES-NEXT: PAREN4 = (~1)
// CHECK-FIXES-NEXT: };

// More complicated parenthesized expressions are excluded.
// Expansions that are not surrounded by parentheses are excluded.
// Nested matching parentheses are stripped.
#define COMPLEX_PAREN1 (x+1)
#define COMPLEX_PAREN2 (x+1
#define COMPLEX_PAREN3 (())
#define COMPLEX_PAREN4 ()
#define COMPLEX_PAREN5 (+1)
#define COMPLEX_PAREN6 ((+1))
// CHECK-MESSAGES: :[[@LINE-2]]:1: warning: replace macro with enum
// CHECK-MESSAGES: :[[@LINE-3]]:9: warning: macro 'COMPLEX_PAREN5' defines an integral constant; prefer an enum instead
// CHECK-MESSAGES: :[[@LINE-3]]:9: warning: macro 'COMPLEX_PAREN6' defines an integral constant; prefer an enum instead
// CHECK-FIXES: enum {
// CHECK-FIXES-NEXT: COMPLEX_PAREN5 = (+1),
// CHECK-FIXES-NEXT: COMPLEX_PAREN6 = ((+1))
// CHECK-FIXES-NEXT: };

#define GOOD_COMMA (1, 2)
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: replace macro with enum
// CHECK-MESSAGES: :[[@LINE-2]]:9: warning: macro 'GOOD_COMMA' defines an integral constant; prefer an enum instead
// CHECK-FIXES: enum {
// CHECK-FIXES-NEXT: GOOD_COMMA = (1, 2)
// CHECK-FIXES-NEXT: };

// Macros appearing in conditional expressions can't be replaced
// by enums.
#define USE_FOO 1
#define USE_BAR 0
#define USE_IF 1
#define USE_ELIF 1
#define USE_IFDEF 1
#define USE_IFNDEF 1

// Undef'ing first and then defining later should still exclude this macro
#undef USE_UINT64
#define USE_UINT64 0
#undef USE_INT64
#define USE_INT64 0

#if defined(USE_FOO) && USE_FOO
extern void foo();
#else
inline void foo() {}
#endif

#if USE_BAR
extern void bar();
#else
inline void bar() {}
#endif

#if USE_IF
inline void used_if() {}
#endif

#if 0
#elif USE_ELIF
inline void used_elif() {}
#endif

#ifdef USE_IFDEF
inline void used_ifdef() {}
#endif

#ifndef USE_IFNDEF
#else
inline void used_ifndef() {}
#endif

// Regular conditional compilation blocks should leave previous
// macro enums alone.
#if 0
#include <non-existent.h>
#endif

// Conditional compilation blocks invalidate adjacent macros
// from being considered as an enum.  Conditionally compiled
// blocks could contain macros that should rightly be included
// in the enum, but we can't explore multiple branches of a
// conditionally compiled section in clang-tidy, only the active
// branch based on compilation options.
#define CONDITION1 1
#define CONDITION2 2
#if 0
#define CONDITION3 3
#else
#define CONDITION3 -3
#endif

#define IFDEF1 1
#define IFDEF2 2
#ifdef FROB
#define IFDEF3 3
#endif

#define IFNDEF1 1
#define IFNDEF2 2
#ifndef GOINK
#define IFNDEF3 3
#endif

// Macros used in conditions are invalidated, even if they look
// like enums after they are used in conditions.
#if DEFINED_LATER1
#endif
#ifdef DEFINED_LATER2
#endif
#ifndef DEFINED_LATER3
#endif
#undef DEFINED_LATER4
#if ((defined(DEFINED_LATER5) || DEFINED_LATER6) && DEFINED_LATER7) || (DEFINED_LATER8 > 10)
#endif

#define DEFINED_LATER1 1
#define DEFINED_LATER2 2
#define DEFINED_LATER3 3
#define DEFINED_LATER4 4
#define DEFINED_LATER5 5
#define DEFINED_LATER6 6
#define DEFINED_LATER7 7
#define DEFINED_LATER8 8

// Sometimes an argument to ifdef can be classified as a keyword token.
#ifdef __restrict
#endif

// These macros do not expand to integral constants.
#define HELLO "Hello, "
#define WORLD "World"
#define EPS1 1.0F
#define EPS2 1e5
#define EPS3 1.

// Ignore macros invoking comma operator unless they are inside parens.
#define BAD_COMMA 1, 2

extern void draw(unsigned int Color);

void f()
{
  // Usage of macros converted to enums should still compile.
  draw(RED);
  draw(GREEN | RED);
  draw(BLUE + RED);
}

// Ignore macros defined inside a top-level function definition.
void g(int x)
{
  if (x != 0) {
#define INSIDE1 1
#define INSIDE2 2
    if (INSIDE1 > 1) {
      f();
    }
  } else {
    if (INSIDE2 == 1) {
      f();
    }
  }
}

// Ignore macros defined inside a top-level function declaration.
extern void g2(
#define INSIDE3 3
#define INSIDE4 4
);

// Ignore macros defined inside a record (structure) declaration.
struct S {
#define INSIDE5 5
#define INSIDE6 6
  char storage[INSIDE5];
};
class C {
#define INSIDE7 7
#define INSIDE8 8
};

// Ignore macros defined inside a template function definition.
template <int N>
#define INSIDE9 9
bool fn()
{
#define INSIDE10 10
  return INSIDE9 > 1 || INSIDE10 < N;
}

// Ignore macros defined inside a variable declaration.
extern int
#define INSIDE11 11
v;

// Ignore macros defined inside a template class definition.
template <int N>
class C2 {
public:
#define INSIDE12 12
    char storage[N];
  bool f() {
    return N > INSIDE12;
  }
  bool g();
};

// Ignore macros defined inside a template member function definition.
template <int N>
#define INSIDE13 13
bool C2<N>::g() {
#define INSIDE14 14
  return N < INSIDE12 || N > INSIDE13 || INSIDE14 > N;
};

// Ignore macros defined inside a template type alias.
template <typename T>
class C3 {
  T data;
};
template <typename T>
#define INSIDE15 15
using Data = C3<T[INSIDE15]>;

// Ignore macros defined inside a type alias.
using Data2 =
#define INSIDE16 16
    char[INSIDE16];

// Ignore macros defined inside a (constexpr) variable definition.
constexpr int
#define INSIDE17 17
value = INSIDE17;

// Ignore macros used in the expansion of other macros
#define INSIDE18 18
#define INSIDE19 19

#define CONCAT(n_, s_) n_##s_
#define FN_NAME(n_, s_) CONCAT(n_, s_)

extern void FN_NAME(g, INSIDE18)();

void gg()
{
    g18();
}