[clang-tools-extra] [clang-tidy] Add readability-constant-operand-order check (PR #167158)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Nov 8 09:08:50 PST 2025
================
@@ -0,0 +1,197 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 "ConstantOperandOrderCheck.h"
+#include "clang/AST/Decl.h"
+#include "clang/AST/Expr.h"
+#include "clang/AST/OperationKinds.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Lexer.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/StringExtras.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+/// Out-of-line ctor so vtable is emitted.
+ConstantOperandOrderCheck::ConstantOperandOrderCheck(StringRef Name,
+ ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context) {
+ // Read options (StringRef -> std::string).
+ PreferredSide = Options.get(PreferredSideOption, "Right").str();
+
+ // Parse BinaryOperators option (comma-separated).
+ std::string OpsCSV =
+ Options.get(BinaryOperatorsOption, "==,!=,<,<=,>,>=").str();
+
+ llvm::SmallVector<llvm::StringRef, 8> Tokens;
+ llvm::StringRef(OpsCSV).split(Tokens, ',');
+ Operators.clear();
+ for (auto Tok : Tokens) {
+ llvm::StringRef Trim = Tok.trim();
+ if (!Trim.empty())
+ Operators.emplace_back(Trim.str());
+ }
+}
+
+ConstantOperandOrderCheck::~ConstantOperandOrderCheck() = default;
+
+void ConstantOperandOrderCheck::storeOptions(
+ ClangTidyOptions::OptionMap &Opts) {
+ Options.store(Opts, PreferredSideOption, PreferredSide);
+ Options.store(Opts, BinaryOperatorsOption,
+ llvm::join(Operators.begin(), Operators.end(), ","));
+}
+
+// ------------------------ helpers ------------------------
+
+namespace {
----------------
EugeneZelenko wrote:
No need for anonymous namespace. `static` is enough.
https://github.com/llvm/llvm-project/pull/167158
More information about the cfe-commits
mailing list