xref: /llvm-project/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/macro-usage.rst (revision b06da39cae0f7e2c6b8bc0bb03b734f9715c0bf3)
1.. title:: clang-tidy - cppcoreguidelines-macro-usage
2
3cppcoreguidelines-macro-usage
4=============================
5
6Finds macro usage that is considered problematic because better language
7constructs exist for the task.
8
9The relevant sections in the C++ Core Guidelines are
10`ES.31 <https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es31-dont-use-macros-for-constants-or-functions>`_, and
11`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 0
18  #define F1(x, y) ((a) > (b) ? (a) : (b))
19  #define F2(...) (__VA_ARGS__)
20  #define F3(x, y) x##y
21  #define COMMA ,
22  #define NORETURN [[noreturn]]
23  #define DEPRECATED attribute((deprecated))
24  #if LIB_EXPORTS
25  #define DLLEXPORTS __declspec(dllexport)
26  #else
27  #define DLLEXPORTS __declspec(dllimport)
28  #endif
29
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 0
35          ^
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
44Options
45-------
46
47.. option:: AllowedRegexp
48
49    A regular expression to filter allowed macros. For example
50    `DEBUG*|LIBTORRENT*|TORRENT*|UNI*` could be applied to filter `libtorrent`.
51    Default value is `^DEBUG_*`.
52
53.. option:: CheckCapsOnly
54
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 older
57    code bases. Default value is `false`.
58
59.. option:: IgnoreCommandLineMacros
60
61    Boolean flag to toggle ignoring command-line-defined macros.
62    Default value is `true`.
63