Skip to content

Commit b06da39

Browse files
authored
[clang-tidy] ignoring macro with hash preprocessing token in cppcoreguidelines-macro-usage (#95265)
`#` and `##` preprocessing tokens cannot be replaced by constexpr function. It should be ignored in check.
1 parent 6784bf7 commit b06da39

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

clang-tools-extra/clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "MacroUsageCheck.h"
10+
#include "clang/Basic/TokenKinds.h"
1011
#include "clang/Frontend/CompilerInstance.h"
1112
#include "clang/Lex/PPCallbacks.h"
1213
#include "clang/Lex/Preprocessor.h"
1314
#include "llvm/ADT/STLExtras.h"
1415
#include "llvm/Support/Regex.h"
15-
#include <algorithm>
1616
#include <cctype>
1717
#include <functional>
1818

@@ -37,7 +37,10 @@ class MacroUsageCallbacks : public PPCallbacks {
3737
const MacroDirective *MD) override {
3838
if (SM.isWrittenInBuiltinFile(MD->getLocation()) ||
3939
MD->getMacroInfo()->isUsedForHeaderGuard() ||
40-
MD->getMacroInfo()->getNumTokens() == 0)
40+
MD->getMacroInfo()->tokens_empty() ||
41+
llvm::any_of(MD->getMacroInfo()->tokens(), [](const Token &T) {
42+
return T.isOneOf(tok::TokenKind::hash, tok::TokenKind::hashhash);
43+
}))
4144
return;
4245

4346
if (IgnoreCommandLineMacros &&

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ Changes in existing checks
266266
<clang-tidy/checks/bugprone/use-after-move>` check to also handle
267267
calls to ``std::forward``.
268268

269+
- Improved :doc:`cppcoreguidelines-macro-usage
270+
<clang-tidy/checks/cppcoreguidelines/macro-usage>` check by ignoring macro with
271+
hash preprocessing token.
272+
269273
- Improved :doc:`cppcoreguidelines-missing-std-forward
270274
<clang-tidy/checks/cppcoreguidelines/missing-std-forward>` check by no longer
271275
giving false positives for deleted functions, by fixing false negatives when only

clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/macro-usage.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Examples:
1717
#define C 0
1818
#define F1(x, y) ((a) > (b) ? (a) : (b))
1919
#define F2(...) (__VA_ARGS__)
20+
#define F3(x, y) x##y
2021
#define COMMA ,
2122
#define NORETURN [[noreturn]]
2223
#define DEPRECATED attribute((deprecated))

clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/macro-usage.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
// CHECK-MESSAGES: [[@LINE-1]]:9: warning: variadic macro 'PROBLEMATIC_VARIADIC2' used; consider using a 'constexpr' variadic template function
3232

3333
// These are all examples of common macros that shouldn't have constexpr suggestions.
34+
#define CONCAT_NAME(a, b) a##b
35+
36+
#define CONCAT_STR(a, b) #a #b
37+
3438
#define COMMA ,
3539

3640
#define NORETURN [[noreturn]]

0 commit comments

Comments
 (0)