Author: mitchell
Date: 2026-01-28T18:48:31+08:00
New Revision: 24bf018be2055e131c440ffd1b8251e660d3d226

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

LOG: [clang-tidy] performance-enum-size should ignore enums within extern "C" 
(#178233)

Closes [#178114](https://github.com/llvm/llvm-project/issues/178114)

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/performance/EnumSizeCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/test/clang-tidy/checkers/performance/enum-size.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/performance/EnumSizeCheck.cpp 
b/clang-tools-extra/clang-tidy/performance/EnumSizeCheck.cpp
index 8147ae4163ea7..47bcd5086cad9 100644
--- a/clang-tools-extra/clang-tidy/performance/EnumSizeCheck.cpp
+++ b/clang-tools-extra/clang-tidy/performance/EnumSizeCheck.cpp
@@ -25,6 +25,17 @@ namespace {
 
 AST_MATCHER(EnumDecl, hasEnumerators) { return !Node.enumerators().empty(); }
 
+AST_MATCHER(EnumDecl, isExternC) {
+  return Node.getDeclContext()->isExternCContext();
+}
+
+AST_MATCHER_P(EnumDecl, hasTypedefNameForAnonDecl,
+              ast_matchers::internal::Matcher<NamedDecl>, InnerMatcher) {
+  if (const TypedefNameDecl *TD = Node.getTypedefNameForAnonDecl())
+    return InnerMatcher.matches(*TD, Finder, Builder);
+  return false;
+}
+
 const std::uint64_t Min8 =
     std::imaxabs(std::numeric_limits<std::int8_t>::min());
 const std::uint64_t Max8 = std::numeric_limits<std::int8_t>::max();
@@ -90,8 +101,11 @@ bool EnumSizeCheck::isLanguageVersionSupported(
 void EnumSizeCheck::registerMatchers(MatchFinder *Finder) {
   Finder->addMatcher(
       enumDecl(unless(isExpansionInSystemHeader()), isDefinition(),
-               hasEnumerators(),
-               unless(matchers::matchesAnyListedRegexName(EnumIgnoreList)))
+               hasEnumerators(), unless(isExternC()),
+               unless(anyOf(
+                   matchers::matchesAnyListedRegexName(EnumIgnoreList),
+                   hasTypedefNameForAnonDecl(
+                       matchers::matchesAnyListedRegexName(EnumIgnoreList)))))
           .bind("e"),
       this);
 }

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 1a056890e66c3..754880bd1a381 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -155,6 +155,13 @@ Changes in existing checks
   <clang-tidy/checks/modernize/use-using>` check by avoiding the generation
   of invalid code for function types with redundant parentheses.
 
+- Improved :doc:`performance-enum-size
+  <clang-tidy/checks/performance/enum-size>` check:
+
+  - Exclude ``enum`` in ``extern "C"`` blocks.
+
+  - Improved the ignore list to correctly handle ``typedef`` and  ``enum``.
+
 - Improved :doc:`performance-move-const-arg
   <clang-tidy/checks/performance/move-const-arg>` check by avoiding false
   positives on trivially copyable types with a non-public copy constructor.

diff  --git 
a/clang-tools-extra/test/clang-tidy/checkers/performance/enum-size.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/performance/enum-size.cpp
index 782c12080f518..0d0868f530264 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/performance/enum-size.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/performance/enum-size.cpp
@@ -1,5 +1,5 @@
 // RUN: %check_clang_tidy -std=c++17-or-later %s performance-enum-size %t -- \
-// RUN:   -config="{CheckOptions: {performance-enum-size.EnumIgnoreList: 
'::IgnoredEnum;IgnoredSecondEnum'}}"
+// RUN:   -config="{CheckOptions: {performance-enum-size.EnumIgnoreList: 
'::IgnoredEnum;IgnoredSecondEnum;IgnoredTypedefEnum'}}"
 
 namespace std
 {
@@ -106,3 +106,15 @@ enum class EnumClassWithoutValues : int {};
 enum EnumWithoutValues {};
 
 }
+
+extern "C" {
+enum ExternCEnum {
+    ec1,
+    ec2
+};
+
+typedef enum {
+    tec1,
+    tec2
+} IgnoredTypedefEnum;
+} // extern "C"


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to