[clang-tools-extra] [clang-tidy] Improve performance-enum-size to exclude empty enums (PR #71640)

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 8 01:12:09 PST 2023


https://github.com/PiotrZSL created https://github.com/llvm/llvm-project/pull/71640

Enums without enumerators (empty) are now excluded from analysis as it's not possible to peroperly determinate new narrowed type, and such enums can be used in diffrent way, like as strong-types.

Closes #71544

>From eeacce5ffae9eca72484a7e51f9e55592fe4ca13 Mon Sep 17 00:00:00 2001
From: Piotr Zegar <piotr.zegar at nokia.com>
Date: Wed, 8 Nov 2023 09:02:54 +0000
Subject: [PATCH] [clang-tidy] Improve performance-enum-size to exclude empty
 enums

Enums without enumerators (empty) are now excluded
from analysis as it's not possible to peroperly determinate
new narrowed type, and such enums can be used in diffrent
way, like as strong-types.
---
 clang-tools-extra/clang-tidy/performance/EnumSizeCheck.cpp   | 5 +++++
 .../docs/clang-tidy/checks/performance/enum-size.rst         | 2 ++
 .../test/clang-tidy/checkers/performance/enum-size.cpp       | 3 +++
 3 files changed, 10 insertions(+)

diff --git a/clang-tools-extra/clang-tidy/performance/EnumSizeCheck.cpp b/clang-tools-extra/clang-tidy/performance/EnumSizeCheck.cpp
index 0d44b8c7706c3c4..b1df9d74cf661ee 100644
--- a/clang-tools-extra/clang-tidy/performance/EnumSizeCheck.cpp
+++ b/clang-tools-extra/clang-tidy/performance/EnumSizeCheck.cpp
@@ -23,6 +23,10 @@ namespace clang::tidy::performance {
 
 namespace {
 
+AST_MATCHER(EnumDecl, hasEnumerators) {
+  return Node.enumerator_begin() != Node.enumerator_end();
+}
+
 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();
@@ -93,6 +97,7 @@ bool EnumSizeCheck::isLanguageVersionSupported(
 void EnumSizeCheck::registerMatchers(MatchFinder *Finder) {
   Finder->addMatcher(
       enumDecl(unless(isExpansionInSystemHeader()), isDefinition(),
+               hasEnumerators(),
                unless(matchers::matchesAnyListedName(EnumIgnoreList)))
           .bind("e"),
       this);
diff --git a/clang-tools-extra/docs/clang-tidy/checks/performance/enum-size.rst b/clang-tools-extra/docs/clang-tidy/checks/performance/enum-size.rst
index 08054123366eee4..f72b8c7eabc2221 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/performance/enum-size.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/performance/enum-size.rst
@@ -58,6 +58,8 @@ terms of memory usage and cache performance. However, it's important to
 consider the trade-offs and potential impact on code readability and
 maintainability.
 
+Enums without enumerators (empty) are excluded from analysis.
+
 Requires C++11 or above.
 Does not provide auto-fixes.
 
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 37481a8141c5c45..782c12080f5180e 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
@@ -102,4 +102,7 @@ enum class IgnoredSecondEnum
     unused2 = 2
 };
 
+enum class EnumClassWithoutValues : int {};
+enum EnumWithoutValues {};
+
 }



More information about the cfe-commits mailing list