================ @@ -0,0 +1,165 @@ +//===--- UseIntegerSignComparisonCheck.cpp - clang-tidy -------------------===// +// +// 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 "UseIntegerSignComparisonCheck.h" +#include "clang/AST/Expr.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; +using namespace clang::ast_matchers::internal; + +namespace clang::tidy::modernize { +UseIntegerSignComparisonCheck::UseIntegerSignComparisonCheck( + StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context), + IncludeInserter(Options.getLocalOrGlobal("IncludeStyle", + utils::IncludeSorter::IS_LLVM), + areDiagsSelfContained()), + IsQtApplication(Options.get("IsQtApplication", false)), + StringsMatchHeader(Options.get("StringsMatchHeader", "<utility>")) {} + +void UseIntegerSignComparisonCheck::storeOptions( + ClangTidyOptions::OptionMap &Opts) { + Options.store(Opts, "IsQtApplication", IsQtApplication); + Options.store(Opts, "StringsMatchHeader", StringsMatchHeader); +} + +void UseIntegerSignComparisonCheck::registerMatchers(MatchFinder *Finder) { + const auto SignedIntCastExpr = intCastExpression(true, "sIntCastExpression"); + const auto UnSignedIntCastExpr = + intCastExpression(false, "uIntCastExpression"); + + // Flag all operators "==", "<=", ">=", "<", ">", "!=" + // that are used between signed/unsigned + const auto CompareOperator = + expr(binaryOperator(hasAnyOperatorName("==", "<=", ">=", "<", ">", "!="), + anyOf(allOf(hasLHS(SignedIntCastExpr), + hasRHS(UnSignedIntCastExpr)), + allOf(hasLHS(UnSignedIntCastExpr), + hasRHS(SignedIntCastExpr))))) + .bind("intComparison"); + + Finder->addMatcher(CompareOperator, this); +} + +BindableMatcher<clang::Stmt> UseIntegerSignComparisonCheck::intCastExpression( + bool IsSigned, const std::string &CastBindName) const { + auto IntTypeExpr = expr(); + if (IsSigned) { + IntTypeExpr = expr(hasType(qualType(isInteger(), isSignedInteger()))); + } else { + IntTypeExpr = + expr(hasType(qualType(isInteger(), unless(isSignedInteger())))); + } ---------------- 5chmidti wrote:
nit: For control structures, LLVM mostly avoids adding braces for single, simple statements like here https://github.com/llvm/llvm-project/pull/113144 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits