53 lines · cpp
1// RUN: %check_clang_tidy %s cppcoreguidelines-macro-usage -std=c++17-or-later %t -- -header-filter=.* -system-headers --2 3#ifndef INCLUDE_GUARD4#define INCLUDE_GUARD5 6#define PROBLEMATIC_CONSTANT 07// CHECK-MESSAGES: [[@LINE-1]]:9: warning: macro 'PROBLEMATIC_CONSTANT' used to declare a constant; consider using a 'constexpr' constant8 9#define PROBLEMATIC_CONSTANT_CHAR '0'10// CHECK-MESSAGES: [[@LINE-1]]:9: warning: macro 'PROBLEMATIC_CONSTANT_CHAR' used to declare a constant; consider using a 'constexpr' constant11 12#define PROBLEMATIC_CONSTANT_WIDE_CHAR L'0'13// CHECK-MESSAGES: [[@LINE-1]]:9: warning: macro 'PROBLEMATIC_CONSTANT_WIDE_CHAR' used to declare a constant; consider using a 'constexpr' constant14 15#define PROBLEMATIC_CONSTANT_UTF8_CHAR u8'0'16// CHECK-MESSAGES: [[@LINE-1]]:9: warning: macro 'PROBLEMATIC_CONSTANT_UTF8_CHAR' used to declare a constant; consider using a 'constexpr' constant17 18#define PROBLEMATIC_CONSTANT_UTF16_CHAR u'0'19// CHECK-MESSAGES: [[@LINE-1]]:9: warning: macro 'PROBLEMATIC_CONSTANT_UTF16_CHAR' used to declare a constant; consider using a 'constexpr' constant20 21#define PROBLEMATIC_CONSTANT_UTF32_CHAR U'0'22// CHECK-MESSAGES: [[@LINE-1]]:9: warning: macro 'PROBLEMATIC_CONSTANT_UTF32_CHAR' used to declare a constant; consider using a 'constexpr' constant23 24#define PROBLEMATIC_FUNCTION(x, y) ((a) > (b) ? (a) : (b))25// CHECK-MESSAGES: [[@LINE-1]]:9: warning: function-like macro 'PROBLEMATIC_FUNCTION' used; consider a 'constexpr' template function26 27#define PROBLEMATIC_VARIADIC(...) (__VA_ARGS__)28// CHECK-MESSAGES: [[@LINE-1]]:9: warning: variadic macro 'PROBLEMATIC_VARIADIC' used; consider using a 'constexpr' variadic template function29 30#define PROBLEMATIC_VARIADIC2(x, ...) (__VA_ARGS__)31// CHECK-MESSAGES: [[@LINE-1]]:9: warning: variadic macro 'PROBLEMATIC_VARIADIC2' used; consider using a 'constexpr' variadic template function32 33// These are all examples of common macros that shouldn't have constexpr suggestions.34#define CONCAT_NAME(a, b) a##b35 36#define CONCAT_STR(a, b) #a #b37 38#define COMMA ,39 40#define NORETURN [[noreturn]]41 42#define DEPRECATED attribute((deprecated))43 44#if LIB_EXPORTS45#define DLLEXPORTS __declspec(dllexport)46#else47#define DLLEXPORTS __declspec(dllimport)48#endif49 50#define ATTRIBUTE_MACRO(...) __attribute__(__VA_ARGS__)51 52#endif53