153 lines · cpp
1// RUN: %check_clang_tidy -std=c++98,c++11,c++14,c++17,c++20 -check-suffixes=,CXX %s readability-use-concise-preprocessor-directives %t2// RUN: %check_clang_tidy -std=c++23-or-later -check-suffixes=,23,CXX,CXX23 %s readability-use-concise-preprocessor-directives %t3 4// RUN: %check_clang_tidy -std=c99,c11,c17 %s readability-use-concise-preprocessor-directives %t -- -- -x c5// RUN: %check_clang_tidy -std=c23-or-later -check-suffixes=,23 %s readability-use-concise-preprocessor-directives %t -- -- -x c6 7// CHECK-MESSAGES: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#ifdef' [readability-use-concise-preprocessor-directives]8// CHECK-FIXES: #ifdef FOO9#if defined(FOO)10// CHECK-MESSAGES-23: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#elifdef' [readability-use-concise-preprocessor-directives]11// CHECK-FIXES-23: #elifdef BAR12#elif defined(BAR)13#endif14 15// CHECK-MESSAGES: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#ifdef' [readability-use-concise-preprocessor-directives]16// CHECK-FIXES: #ifdef FOO17#if defined FOO18// CHECK-MESSAGES-23: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#elifdef' [readability-use-concise-preprocessor-directives]19// CHECK-FIXES-23: #elifdef BAR20#elif defined BAR21#endif22 23// CHECK-MESSAGES: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#ifdef' [readability-use-concise-preprocessor-directives]24// CHECK-FIXES: #ifdef FOO25#if (defined(FOO))26// CHECK-MESSAGES-23: :[[@LINE+2]]:4: warning: preprocessor condition can be written more concisely using '#elifdef' [readability-use-concise-preprocessor-directives]27// CHECK-FIXES-23: # elifdef BAR28# elif (defined(BAR))29#endif30 31// CHECK-MESSAGES: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#ifdef' [readability-use-concise-preprocessor-directives]32// CHECK-FIXES: #ifdef FOO33#if(defined(FOO))34// CHECK-MESSAGES-23: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#elifdef' [readability-use-concise-preprocessor-directives]35// CHECK-FIXES-23: #elifdef BAR36#elif(defined(BAR))37#endif38 39// CHECK-MESSAGES: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#ifdef' [readability-use-concise-preprocessor-directives]40// CHECK-FIXES: #ifdef FOO41#if (defined FOO)42// CHECK-MESSAGES-23: :[[@LINE+2]]:4: warning: preprocessor condition can be written more concisely using '#elifdef' [readability-use-concise-preprocessor-directives]43// CHECK-FIXES-23: # elifdef BAR44# elif (defined BAR)45#endif46 47// CHECK-MESSAGES: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#ifndef' [readability-use-concise-preprocessor-directives]48// CHECK-FIXES: #ifndef FOO49#if !defined(FOO)50// CHECK-MESSAGES-23: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#elifndef' [readability-use-concise-preprocessor-directives]51// CHECK-FIXES-23: #elifndef BAR52#elif !defined(BAR)53#endif54 55#ifdef __cplusplus56// CHECK-MESSAGES-CXX: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#ifndef' [readability-use-concise-preprocessor-directives]57// CHECK-FIXES-CXX: #ifndef FOO58#if not defined(FOO)59// CHECK-MESSAGES-CXX23: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#elifndef' [readability-use-concise-preprocessor-directives]60// CHECK-FIXES-CXX23: #elifndef BAR61#elif not defined(BAR)62#endif63#endif // __cplusplus64 65// CHECK-MESSAGES: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#ifndef' [readability-use-concise-preprocessor-directives]66// CHECK-FIXES: #ifndef FOO67#if !defined FOO68// CHECK-MESSAGES-23: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#elifndef' [readability-use-concise-preprocessor-directives]69// CHECK-FIXES-23: #elifndef BAR70#elif !defined BAR71#endif72 73#ifdef __cplusplus74// CHECK-MESSAGES-CXX: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#ifndef' [readability-use-concise-preprocessor-directives]75// CHECK-FIXES-CXX: #ifndef FOO76#if not defined FOO77// CHECK-MESSAGES-CXX23: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#elifndef' [readability-use-concise-preprocessor-directives]78// CHECK-FIXES-CXX23: #elifndef BAR79#elif not defined BAR80#endif81#endif // __cplusplus82 83// CHECK-MESSAGES: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#ifndef' [readability-use-concise-preprocessor-directives]84// CHECK-FIXES: #ifndef FOO85#if (!defined(FOO))86// CHECK-MESSAGES-23: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#elifndef' [readability-use-concise-preprocessor-directives]87// CHECK-FIXES-23: #elifndef BAR88#elif (!defined(BAR))89#endif90 91// CHECK-MESSAGES: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#ifndef' [readability-use-concise-preprocessor-directives]92// CHECK-FIXES: #ifndef FOO93#if (!defined FOO)94// CHECK-MESSAGES-23: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#elifndef' [readability-use-concise-preprocessor-directives]95// CHECK-FIXES-23: #elifndef BAR96#elif (!defined BAR)97#endif98 99// CHECK-MESSAGES: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#ifndef' [readability-use-concise-preprocessor-directives]100// CHECK-FIXES: #ifndef FOO101#if !(defined(FOO))102// CHECK-MESSAGES-23: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#elifndef' [readability-use-concise-preprocessor-directives]103// CHECK-FIXES-23: #elifndef BAR104#elif !(defined(BAR))105#endif106 107// CHECK-MESSAGES: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#ifndef' [readability-use-concise-preprocessor-directives]108// CHECK-FIXES: #ifndef FOO109#if !(defined FOO)110// CHECK-MESSAGES-23: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#elifndef' [readability-use-concise-preprocessor-directives]111// CHECK-FIXES-23: #elifndef BAR112#elif !(defined BAR)113#endif114 115// These cases with many parentheses and negations are unrealistic, but116// handling them doesn't really add any complexity to the implementation.117// Test them for good measure.118 119// CHECK-MESSAGES: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#ifndef' [readability-use-concise-preprocessor-directives]120// CHECK-FIXES: #ifndef FOO121#if !((!!(defined(FOO))))122// CHECK-MESSAGES-23: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#elifdef' [readability-use-concise-preprocessor-directives]123// CHECK-FIXES-23: #elifdef BAR124#elif ((!(!(defined(BAR)))))125#endif126 127// CHECK-MESSAGES: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#ifndef' [readability-use-concise-preprocessor-directives]128// CHECK-FIXES: #ifndef FOO129#if !((!!(defined FOO)))130// CHECK-MESSAGES-23: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#elifdef' [readability-use-concise-preprocessor-directives]131// CHECK-FIXES-23: #elifdef BAR132#elif ((!(!(defined BAR))))133#endif134 135// CHECK-MESSAGES: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#ifndef' [readability-use-concise-preprocessor-directives]136// CHECK-FIXES: #ifndef FOO137#if !( (!! ( defined FOO )) )138// CHECK-MESSAGES-23: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#elifdef' [readability-use-concise-preprocessor-directives]139// CHECK-FIXES-23: #elifdef BAR140#elif ( ( !(!( defined BAR) ) ))141#endif142 143#if FOO144#elif BAR145#endif146 147#if defined(FOO) && defined(BAR)148#elif defined(FOO) && defined(BAR)149#endif150 151#if defined FOO && BAR152#endif153