[clang-tools-extra] [clang-tidy] Add `modernize-use-if-consteval` check (PR #189743)
Yanzuo Liu via cfe-commits
cfe-commits at lists.llvm.org
Fri May 29 07:48:46 PDT 2026
================
@@ -0,0 +1,136 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 "UseIfConstevalCheck.h"
+
+#include "../utils/BracesAroundStatement.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Basic/CharInfo.h"
+#include "clang/Lex/Lexer.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::modernize {
+
+namespace {
+
+struct BraceFix {
+ bool NeedsBraces = false;
+ utils::BraceInsertionHints Hints;
+};
+
+} // namespace
+
+static std::optional<SourceRange> getHeaderRange(const IfStmt *If,
+ const SourceManager &SM,
+ const LangOptions &LangOpts) {
+ if (If->getLParenLoc().isMacroID() || If->getRParenLoc().isMacroID())
+ return std::nullopt;
+
+ const SourceRange HeaderRange(If->getLParenLoc(), If->getRParenLoc());
+ // Validate that the token range is safely rewriteable in file source before
+ // offering a fix-it.
+ if (Lexer::makeFileCharRange(CharSourceRange::getTokenRange(HeaderRange), SM,
+ LangOpts)
+ .isInvalid())
+ return std::nullopt;
+ return HeaderRange;
+}
+
+static std::optional<BraceFix>
+getBraceFix(const Stmt *S, const LangOptions &LangOpts, const SourceManager &SM,
+ SourceLocation StartLoc,
+ SourceLocation EndLocHint = SourceLocation()) {
+ if (S)
+ S = S->stripLabelLikeStatements();
+ if (!S || isa<CompoundStmt>(S))
+ return BraceFix();
+
+ const auto Hints =
+ utils::getBraceInsertionsHints(S, LangOpts, SM, StartLoc, EndLocHint);
+ if (!Hints || !Hints.offersFixIts())
+ return std::nullopt;
+
+ return BraceFix{true, Hints};
+}
+
+static bool needsLeadingSpaceBeforeConsteval(SourceLocation LParenLoc,
+ const SourceManager &SM) {
+ bool Invalid = false;
+ const char *LParen = SM.getCharacterData(LParenLoc, &Invalid);
+ return Invalid || !isWhitespace(LParen[-1]);
+}
+
+void UseIfConstevalCheck::registerMatchers(MatchFinder *Finder) {
+ const auto IsConstantEvaluatedCall =
+ callExpr(callee(functionDecl(hasName("is_constant_evaluated"),
+ isInStdNamespace())))
+ .bind("call");
+ const auto IsNegatedConstantEvaluatedExpr =
+ unaryOperator(hasOperatorName("!"), hasUnaryOperand(ignoringParenImpCasts(
+ IsConstantEvaluatedCall)))
+ .bind("negation");
+ const auto IsConstantEvaluatedExpr =
+ expr(anyOf(ignoringParenImpCasts(IsConstantEvaluatedCall),
+ ignoringParenImpCasts(IsNegatedConstantEvaluatedExpr)));
----------------
zwuis wrote:
`ignoringParenImpCasts(anyOf(...))`
https://github.com/llvm/llvm-project/pull/189743
More information about the cfe-commits
mailing list