Author: Congcong Cai
Date: 2024-06-13T09:36:33+08:00
New Revision: b06da39cae0f7e2c6b8bc0bb03b734f9715c0bf3

URL: 
https://github.com/llvm/llvm-project/commit/b06da39cae0f7e2c6b8bc0bb03b734f9715c0bf3
DIFF: 
https://github.com/llvm/llvm-project/commit/b06da39cae0f7e2c6b8bc0bb03b734f9715c0bf3.diff

LOG: [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.

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/macro-usage.rst
    clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/macro-usage.cpp

Removed: 
    


################################################################################
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