xref: /llvm-project/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/macro-usage.rst (revision b06da39cae0f7e2c6b8bc0bb03b734f9715c0bf3)
16e566bc5SRichard.. title:: clang-tidy - cppcoreguidelines-macro-usage
26e566bc5SRichard
36e566bc5SRichardcppcoreguidelines-macro-usage
46e566bc5SRichard=============================
56e566bc5SRichard
66e566bc5SRichardFinds macro usage that is considered problematic because better language
76e566bc5SRichardconstructs exist for the task.
86e566bc5SRichard
96e566bc5SRichardThe relevant sections in the C++ Core Guidelines are
100f1f1d45SPiotr Zegar`ES.31 <https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es31-dont-use-macros-for-constants-or-functions>`_, and
110f1f1d45SPiotr Zegar`ES.32 <https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es32-use-all_caps-for-all-macro-names>`_.
126e566bc5SRichard
136e566bc5SRichardExamples:
146e566bc5SRichard
156e566bc5SRichard.. code-block:: c++
166e566bc5SRichard
176e566bc5SRichard  #define C 0
186e566bc5SRichard  #define F1(x, y) ((a) > (b) ? (a) : (b))
196e566bc5SRichard  #define F2(...) (__VA_ARGS__)
20*b06da39cSCongcong Cai  #define F3(x, y) x##y
216e566bc5SRichard  #define COMMA ,
226e566bc5SRichard  #define NORETURN [[noreturn]]
236e566bc5SRichard  #define DEPRECATED attribute((deprecated))
246e566bc5SRichard  #if LIB_EXPORTS
256e566bc5SRichard  #define DLLEXPORTS __declspec(dllexport)
266e566bc5SRichard  #else
276e566bc5SRichard  #define DLLEXPORTS __declspec(dllimport)
286e566bc5SRichard  #endif
296e566bc5SRichard
306e566bc5SRichardresults in the following warnings::
316e566bc5SRichard
326e566bc5SRichard  4 warnings generated.
336e566bc5SRichard  test.cpp:1:9: warning: macro 'C' used to declare a constant; consider using a 'constexpr' constant [cppcoreguidelines-macro-usage]
346e566bc5SRichard  #define C 0
356e566bc5SRichard          ^
366e566bc5SRichard  test.cpp:2:9: warning: function-like macro 'F1' used; consider a 'constexpr' template function [cppcoreguidelines-macro-usage]
376e566bc5SRichard  #define F1(x, y) ((a) > (b) ? (a) : (b))
386e566bc5SRichard          ^
396e566bc5SRichard  test.cpp:3:9: warning: variadic macro 'F2' used; consider using a 'constexpr' variadic template function [cppcoreguidelines-macro-usage]
406e566bc5SRichard  #define F2(...) (__VA_ARGS__)
416e566bc5SRichard          ^
426e566bc5SRichard
436e566bc5SRichard
446e566bc5SRichardOptions
456e566bc5SRichard-------
466e566bc5SRichard
476e566bc5SRichard.. option:: AllowedRegexp
486e566bc5SRichard
496e566bc5SRichard    A regular expression to filter allowed macros. For example
506e566bc5SRichard    `DEBUG*|LIBTORRENT*|TORRENT*|UNI*` could be applied to filter `libtorrent`.
516e566bc5SRichard    Default value is `^DEBUG_*`.
526e566bc5SRichard
536e566bc5SRichard.. option:: CheckCapsOnly
546e566bc5SRichard
556e566bc5SRichard    Boolean flag to warn on all macros except those with CAPS_ONLY names.
566e566bc5SRichard    This option is intended to ease introduction of this check into older
576e566bc5SRichard    code bases. Default value is `false`.
586e566bc5SRichard
596e566bc5SRichard.. option:: IgnoreCommandLineMacros
606e566bc5SRichard
616e566bc5SRichard    Boolean flag to toggle ignoring command-line-defined macros.
626e566bc5SRichard    Default value is `true`.
63