clang-tidy - cppcoreguidelines-macro-usage — Extra Clang Tools 16.0.6 documentation

# Extra Clang Tools 16.0.6 documentation

## clang-tidy - cppcoreguidelines-macro-usage

# cppcoreguidelines-macro-usage

Finds macro usage that is considered problematic because better language constructs exist for the task.

The relevant sections in the C++ Core Guidelines are ES.31, and ES.32.

Examples:

    #define C 0
    #define F1(x, y) ((a) > (b) ? (a) : (b))
    #define F2(...) (__VA_ARGS__)
    #define COMMA ,
    #define NORETURN [[noreturn]]
    #define DEPRECATED attribute((deprecated))
    #if LIB_EXPORTS
    #define DLLEXPORTS __declspec(dllexport)
    #else
    #define DLLEXPORTS __declspec(dllimport)
    #endif

results in the following warnings:

    4 warnings generated.
    test.cpp:1:9: warning: macro 'C' used to declare a constant; consider using a 'constexpr' constant [cppcoreguidelines-macro-usage]
    #define C 0
            ^
    test.cpp:2:9: warning: function-like macro 'F1' used; consider a 'constexpr' template function [cppcoreguidelines-macro-usage]
    #define F1(x, y) ((a) > (b) ? (a) : (b))
            ^
    test.cpp:3:9: warning: variadic macro 'F2' used; consider using a 'constexpr' variadic template function [cppcoreguidelines-macro-usage]
    #define F2(...) (__VA_ARGS__)
            ^

## Options

AllowedRegexp

  A regular expression to filter allowed macros. For example DEBUG*|LIBTORRENT*|TORRENT*|UNI* could be applied to filter libtorrent. Default value is ^DEBUG_*.

CheckCapsOnly

  Boolean flag to warn on all macros except those with CAPS_ONLY names. This option is intended to ease introduction of this check into older code bases. Default value is false.

IgnoreCommandLineMacros

  Boolean flag to toggle ignoring command-line-defined macros. Default value is true.