[clang-tools-extra] [clang-tidy] add new check readability-enum-initial-value (PR #86129)
Julian Schmidt via cfe-commits
cfe-commits at lists.llvm.org
Sat Mar 30 13:39:58 PDT 2024
================
@@ -0,0 +1,199 @@
+//===--- EnumInitialValueCheck.cpp - clang-tidy ---------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "EnumInitialValueCheck.h"
+#include "../utils/LexerUtils.h"
+#include "clang/AST/Decl.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/Basic/Diagnostic.h"
+#include "clang/Basic/SourceLocation.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallString.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+static bool isNoneEnumeratorsInitialized(const EnumDecl &Node) {
+ return llvm::all_of(Node.enumerators(), [](const EnumConstantDecl *ECD) {
+ return ECD->getInitExpr() == nullptr;
+ });
+}
+
+static bool isOnlyFirstEnumeratorInitialized(const EnumDecl &Node) {
+ bool IsFirst = true;
+ for (const EnumConstantDecl *ECD : Node.enumerators()) {
+ if ((IsFirst && ECD->getInitExpr() == nullptr) ||
+ (!IsFirst && ECD->getInitExpr() != nullptr))
+ return false;
+ IsFirst = false;
+ }
+ return !IsFirst;
+}
+
+static bool areAllEnumeratorsInitialized(const EnumDecl &Node) {
+ return llvm::all_of(Node.enumerators(), [](const EnumConstantDecl *ECD) {
+ return ECD->getInitExpr() != nullptr;
+ });
+}
+
+/// Check if \p Enumerator is initialized with a (potentially negated) \c
+/// IntegerLiteral.
+static bool isInitializedByLiteral(const EnumConstantDecl *Enumerator) {
+ const Expr *const Init = Enumerator->getInitExpr();
+ if (!Init)
+ return false;
+ return Init->isIntegerConstantExpr(Enumerator->getASTContext());
+}
+
+static void cleanInitialValue(DiagnosticBuilder &Diag,
+ const EnumConstantDecl *ECD,
+ const SourceManager &SM,
+ const LangOptions &LangOpts) {
+ std::optional<Token> EqualToken = utils::lexer::findNextTokenSkippingComments(
+ ECD->getLocation(), SM, LangOpts);
+ if (!EqualToken.has_value())
+ return;
+ SourceLocation EqualLoc{EqualToken->getLocation()};
+ if (EqualLoc.isInvalid() || EqualLoc.isMacroID())
+ return;
+ SourceRange InitExprRange = ECD->getInitExpr()->getSourceRange();
+ if (InitExprRange.isInvalid() || InitExprRange.getBegin().isMacroID() ||
+ InitExprRange.getEnd().isMacroID())
+ return;
+ Diag << FixItHint::CreateRemoval(EqualLoc)
+ << FixItHint::CreateRemoval(InitExprRange);
+ return;
+}
+
+namespace {
+
+AST_MATCHER(EnumDecl, isMacro) {
+ SourceLocation Loc = Node.getBeginLoc();
+ return Loc.isMacroID();
+}
+
+AST_MATCHER(EnumDecl, hasConsistentInitialValues) {
+ return isNoneEnumeratorsInitialized(Node) ||
+ isOnlyFirstEnumeratorInitialized(Node) ||
+ areAllEnumeratorsInitialized(Node);
+}
+
+AST_MATCHER(EnumDecl, hasZeroInitialValueForFirstEnumerator) {
+ EnumDecl::enumerator_range Enumerators = Node.enumerators();
----------------
5chmidti wrote:
Please add `const`
https://github.com/llvm/llvm-project/pull/86129
More information about the cfe-commits
mailing list