https://github.com/HerrCai0907 created 
https://github.com/llvm/llvm-project/pull/95265

`#` and `##` preprocessing tokens cannot be replaced by constexpr function. It 
should be ignored in check.

>From eaeb5b6493e0c3980a60f9acbc5572de0d619419 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0...@163.com>
Date: Thu, 13 Jun 2024 00:04:38 +0800
Subject: [PATCH] [clang-tidy] ignoring macro with hash preprocessing token in
 cppcoreguidelines-macro-usage

---
 .../clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp       | 7 +++++--
 clang-tools-extra/docs/ReleaseNotes.rst                    | 4 ++++
 .../clang-tidy/checks/cppcoreguidelines/macro-usage.rst    | 1 +
 .../clang-tidy/checkers/cppcoreguidelines/macro-usage.cpp  | 4 ++++
 4 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp 
b/clang-tools-extra/clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp
index 0cd4bf6fdfd87..11eb056e916d3 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp
@@ -7,12 +7,12 @@
 
//===----------------------------------------------------------------------===//
 
 #include "MacroUsageCheck.h"
+#include "clang/Basic/TokenKinds.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Lex/PPCallbacks.h"
 #include "clang/Lex/Preprocessor.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/Support/Regex.h"
-#include <algorithm>
 #include <cctype>
 #include <functional>
 
@@ -37,7 +37,10 @@ class MacroUsageCallbacks : public PPCallbacks {
                     const MacroDirective *MD) override {
     if (SM.isWrittenInBuiltinFile(MD->getLocation()) ||
         MD->getMacroInfo()->isUsedForHeaderGuard() ||
-        MD->getMacroInfo()->getNumTokens() == 0)
+        MD->getMacroInfo()->tokens_empty() ||
+        llvm::any_of(MD->getMacroInfo()->tokens(), [](const Token &T) {
+          return T.isOneOf(tok::TokenKind::hash, tok::TokenKind::hashhash);
+        }))
       return;
 
     if (IgnoreCommandLineMacros &&
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 6bf70c5cf4f8a..5d5aecd67b2d7 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -266,6 +266,10 @@ Changes in existing checks
   <clang-tidy/checks/bugprone/use-after-move>` check to also handle
   calls to ``std::forward``.
 
+- Improved :doc:`cppcoreguidelines-macro-usage
+  <clang-tidy/checks/cppcoreguidelines/macro-usage>` check by ignoring macro 
with
+  hash preprocessing token.
+
 - Improved :doc:`cppcoreguidelines-missing-std-forward
   <clang-tidy/checks/cppcoreguidelines/missing-std-forward>` check by no longer
   giving false positives for deleted functions, by fixing false negatives when 
only
diff --git 
a/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/macro-usage.rst 
b/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/macro-usage.rst
index 8b763c28479e6..49417effbb6ff 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/macro-usage.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/macro-usage.rst
@@ -17,6 +17,7 @@ Examples:
   #define C 0
   #define F1(x, y) ((a) > (b) ? (a) : (b))
   #define F2(...) (__VA_ARGS__)
+  #define F3(x, y) x##y
   #define COMMA ,
   #define NORETURN [[noreturn]]
   #define DEPRECATED attribute((deprecated))
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/macro-usage.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/macro-usage.cpp
index 404aafb6b1c45..865ef9df1182e 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/macro-usage.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/macro-usage.cpp
@@ -31,6 +31,10 @@
 // CHECK-MESSAGES: [[@LINE-1]]:9: warning: variadic macro 
'PROBLEMATIC_VARIADIC2' used; consider using a 'constexpr' variadic template 
function
 
 // These are all examples of common macros that shouldn't have constexpr 
suggestions.
+#define CONCAT_NAME(a, b) a##b
+
+#define CONCAT_STR(a, b) #a #b
+
 #define COMMA ,
 
 #define NORETURN [[noreturn]]

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to