[clang-tools-extra] [clang-tidy] Add `modernize-use-if-consteval` check (PR #189743)
Daniil Dudkin via cfe-commits
cfe-commits at lists.llvm.org
Sun Apr 19 11:07:33 PDT 2026
================
@@ -0,0 +1,150 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 const Stmt *ignoreAttributedStmt(const Stmt *S) {
+ while (const auto *AS = dyn_cast_or_null<AttributedStmt>(S))
+ S = AS->getSubStmt();
+ return S;
+}
+
+static std::optional<CharSourceRange>
+getHeaderRange(const IfStmt *If, const SourceManager &SM,
+ const LangOptions &LangOpts) {
+ if (If->getLParenLoc().isMacroID() || If->getRParenLoc().isMacroID())
+ return std::nullopt;
+
+ const CharSourceRange HeaderRange = Lexer::makeFileCharRange(
----------------
unterumarmung wrote:
I use `Lexer::makeFileCharRange` to make sure that the fixit is actually safe for cases like:
```cpp
// main.cpp
if (
#include "cond.inc"
return 1;
// cond.inc
std::is_constant_evaluated())
```
Maybe it's excessive..
But I switched to using `SourceRange` as the fixit provider and `Lexer::makeFileCharRange` is only used to check whether we are not overwriting between files
https://github.com/llvm/llvm-project/pull/189743
More information about the cfe-commits
mailing list