63 lines · plain
1.. title:: clang-tidy - cppcoreguidelines-macro-usage2 3cppcoreguidelines-macro-usage4=============================5 6Finds macro usage that is considered problematic because better language7constructs exist for the task.8 9The relevant sections in the C++ Core Guidelines are10`ES.31 <https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es31-dont-use-macros-for-constants-or-functions>`_, and11`ES.32 <https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es32-use-all_caps-for-all-macro-names>`_.12 13Examples:14 15.. code-block:: c++16 17 #define C 018 #define F1(x, y) ((a) > (b) ? (a) : (b))19 #define F2(...) (__VA_ARGS__)20 #define F3(x, y) x##y21 #define COMMA ,22 #define NORETURN [[noreturn]]23 #define DEPRECATED attribute((deprecated))24 #if LIB_EXPORTS25 #define DLLEXPORTS __declspec(dllexport)26 #else27 #define DLLEXPORTS __declspec(dllimport)28 #endif29 30results in the following warnings::31 32 4 warnings generated.33 test.cpp:1:9: warning: macro 'C' used to declare a constant; consider using a 'constexpr' constant [cppcoreguidelines-macro-usage]34 #define C 035 ^36 test.cpp:2:9: warning: function-like macro 'F1' used; consider a 'constexpr' template function [cppcoreguidelines-macro-usage]37 #define F1(x, y) ((a) > (b) ? (a) : (b))38 ^39 test.cpp:3:9: warning: variadic macro 'F2' used; consider using a 'constexpr' variadic template function [cppcoreguidelines-macro-usage]40 #define F2(...) (__VA_ARGS__)41 ^42 43 44Options45-------46 47.. option:: AllowedRegexp48 49 A regular expression to filter allowed macros. For example50 `DEBUG*|LIBTORRENT*|TORRENT*|UNI*` could be applied to filter `libtorrent`.51 Default value is `^DEBUG_*`.52 53.. option:: CheckCapsOnly54 55 Boolean flag to warn on all macros except those with CAPS_ONLY names.56 This option is intended to ease introduction of this check into older57 code bases. Default value is `false`.58 59.. option:: IgnoreCommandLineMacros60 61 Boolean flag to toggle ignoring command-line-defined macros.62 Default value is `true`.63