================
@@ -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 {
+
+static const Expr *strip(const Expr *E) {
+ return E ? E->IgnoreParenImpCasts() : nullptr;
+}
+
+static bool isSimpleConstantExpr(const Expr *E) {
+ E = strip(E);
+ if (!E)
+ return false;
+
+ if (isa<IntegerLiteral>(E) || isa<FloatingLiteral>(E) ||
+ isa<StringLiteral>(E) || isa<CharacterLiteral>(E) ||
+ isa<CXXBoolLiteralExpr>(E) || isa<CXXNullPtrLiteralExpr>(E) ||
+ isa<GNUNullExpr>(E))
+ return true;
+
+ if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) {
+ if (isa<EnumConstantDecl>(DRE->getDecl()))
+ return true;
+ if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
+ return VD->isConstexpr() || VD->getType().isConstQualified();
+ }
+
+ return false;
+}
+
+static bool hasSideEffectsExpr(const Expr *E, ASTContext &Ctx) {
+ E = strip(E);
+ return E && E->HasSideEffects(Ctx);
+}
+
+static std::string invertOperatorText(llvm::StringRef Op) {
----------------
localspook wrote:
```suggestion
static StringRef invertOperatorText(llvm::StringRef Op) {
```
https://github.com/llvm/llvm-project/pull/167158
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits