[clang-tools-extra] [clang-tidy] Add performance-expensive-value-or check (PR #200166)
Yanzuo Liu via cfe-commits
cfe-commits at lists.llvm.org
Fri Jul 10 07:54:48 PDT 2026
Endre =?utf-8?q?Fülöp?= <endre.fulop at sigmatechnology.com>,
Endre =?utf-8?q?Fülöp?= <endre.fulop at sigmatechnology.com>,
Endre =?utf-8?q?Fülöp?= <endre.fulop at sigmatechnology.com>,
Endre =?utf-8?q?Fülöp?= <endre.fulop at sigmatechnology.com>,
Endre =?utf-8?q?Fülöp?= <endre.fulop at sigmatechnology.com>,
Endre =?utf-8?q?Fülöp?= <endre.fulop at sigmatechnology.com>,
Endre =?utf-8?q?Fülöp?= <endre.fulop at sigmatechnology.com>,
Endre =?utf-8?q?Fülöp?= <endre.fulop at sigmatechnology.com>,
Endre =?utf-8?q?Fülöp?= <endre.fulop at sigmatechnology.com>,
Endre =?utf-8?q?Fülöp?= <endre.fulop at sigmatechnology.com>,
Endre =?utf-8?q?Fülöp?= <endre.fulop at sigmatechnology.com>,
Endre =?utf-8?q?Fülöp?= <endre.fulop at sigmatechnology.com>,
Endre =?utf-8?q?Fülöp?= <endre.fulop at sigmatechnology.com>,
Endre =?utf-8?q?Fülöp?= <endre.fulop at sigmatechnology.com>,
Endre =?utf-8?q?Fülöp?= <endre.fulop at sigmatechnology.com>,
Endre =?utf-8?q?Fülöp?= <endre.fulop at sigmatechnology.com>,
Endre =?utf-8?q?Fülöp?= <endre.fulop at sigmatechnology.com>,
Endre =?utf-8?q?Fülöp?= <endre.fulop at sigmatechnology.com>,
Endre =?utf-8?q?Fülöp?= <endre.fulop at sigmatechnology.com>,
Endre =?utf-8?q?Fülöp?= <endre.fulop at sigmatechnology.com>,
Endre =?utf-8?q?Fülöp?= <endre.fulop at sigmatechnology.com>,
Endre =?utf-8?q?Fülöp?= <endre.fulop at sigmatechnology.com>,
Endre =?utf-8?q?Fülöp?= <endre.fulop at sigmatechnology.com>,
Endre =?utf-8?q?Fülöp?= <endre.fulop at sigmatechnology.com>,
Endre =?utf-8?q?Fülöp?= <endre.fulop at sigmatechnology.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/200166 at github.com>
================
@@ -0,0 +1,174 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 "ExpensiveValueOrCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
+#include "../utils/TypeTraits.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Lexer.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::performance {
+
+static bool hasOperatorStar(const CXXRecordDecl *RD) {
+ return llvm::any_of(RD->methods(), [](const CXXMethodDecl *M) {
+ return M->getOverloadedOperator() == OO_Star;
+ });
+}
+
+static StringRef findValueMethod(const CXXRecordDecl *RD) {
+ for (const auto *M : RD->methods()) {
+ if (!M->getDeclName().isIdentifier())
+ continue;
+ StringRef Name = M->getName();
+ if (Name == "value" || Name == "Value")
+ return Name;
+ }
+ return {};
+}
+
+static std::string buildSuggestion(const CXXRecordDecl *OptionalClass) {
+ const bool HasDeref = hasOperatorStar(OptionalClass);
+ StringRef ValueName = findValueMethod(OptionalClass);
+
+ if (HasDeref && !ValueName.empty())
+ return (llvm::Twine("consider using 'operator*' or '") + ValueName +
+ "()' with a separate fallback")
+ .str();
+ if (HasDeref)
+ return "consider using 'operator*' with a separate fallback";
+ if (!ValueName.empty())
+ return (llvm::Twine("consider using '") + ValueName +
+ "()' with a separate fallback")
+ .str();
+ return "consider avoiding the copy";
+}
+
+static std::optional<FixItHint>
+buildFixIt(const CXXMemberCallExpr *Call, const Expr *ObjExpr,
+ const Expr *FallbackArg, const CXXRecordDecl *OptionalClass,
+ const SourceManager &SM, const LangOptions &LO) {
+ if (Call->getBeginLoc().isMacroID())
+ return std::nullopt;
+ if (!ObjExpr->isLValue())
+ return std::nullopt;
+ if (!hasOperatorStar(OptionalClass))
+ return std::nullopt;
+
+ StringRef ObjText = Lexer::getSourceText(
+ CharSourceRange::getTokenRange(ObjExpr->getSourceRange()), SM, LO);
+ StringRef ArgText = Lexer::getSourceText(
+ CharSourceRange::getTokenRange(FallbackArg->getSourceRange()), SM, LO);
+
+ if (ObjText.empty() || ArgText.empty())
+ return std::nullopt;
+
+ const std::string Replacement =
+ ("(" + ObjText + " ? *" + ObjText + " : " + ArgText + ")").str();
+ return FixItHint::CreateReplacement(Call->getSourceRange(), Replacement);
+}
+
+ExpensiveValueOrCheck::ExpensiveValueOrCheck(StringRef Name,
+ ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context),
+ SizeThreshold(Options.get("SizeThreshold", 16U)),
+ OptionalTypes(utils::options::parseStringList(
+ Options.get("OptionalTypes",
+ "::std::optional;::absl::optional;::boost::optional"))),
+ WarnOnOwnershipTaking(Options.get("WarnOnOwnershipTaking", false)) {}
+
+void ExpensiveValueOrCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
+ Options.store(Opts, "SizeThreshold", SizeThreshold);
+ Options.store(Opts, "OptionalTypes",
+ utils::options::serializeStringList(OptionalTypes));
+ Options.store(Opts, "WarnOnOwnershipTaking", WarnOnOwnershipTaking);
+}
+
+void ExpensiveValueOrCheck::registerMatchers(MatchFinder *Finder) {
+ auto OptionalTypesMatcher =
+ matchers::matchesAnyListedRegexName(OptionalTypes);
----------------
zwuis wrote:
Oh I didn't read user documentation carefully. Sorry for the noise.
https://github.com/llvm/llvm-project/pull/200166
More information about the cfe-commits
mailing list