We have a de-facto policy in clang-tidy to not qualify names unless absolutely necessary. We're *mostly* consistent about that (especially in new code), but a number of deviations have accumulated over the years. We even have cases where the same name is sometimes qualified and sometimes not *in the same file*. This makes it jarring to read the code, and, I imagine, more confusing for newcomers to contribute to the project (do I qualify X or not?). This PR tries to improve the situation and regularize the codebase.
102 lines
3.3 KiB
C++
102 lines
3.3 KiB
C++
//===----------------------------------------------------------------------===//
|
|
//
|
|
// 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 "MathMissingParenthesesCheck.h"
|
|
#include "clang/AST/ASTContext.h"
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
#include "clang/Lex/Lexer.h"
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
namespace clang::tidy::readability {
|
|
|
|
void MathMissingParenthesesCheck::registerMatchers(MatchFinder *Finder) {
|
|
Finder->addMatcher(
|
|
binaryOperator(
|
|
unless(hasParent(binaryOperator(unless(isAssignmentOperator()),
|
|
unless(isComparisonOperator())))),
|
|
unless(isAssignmentOperator()), unless(isComparisonOperator()),
|
|
unless(hasAnyOperatorName("&&", "||")),
|
|
hasDescendant(binaryOperator()))
|
|
.bind("binOp"),
|
|
this);
|
|
}
|
|
|
|
static int getPrecedence(const BinaryOperator *BinOp) {
|
|
if (!BinOp)
|
|
return 0;
|
|
switch (BinOp->getOpcode()) {
|
|
case BO_Mul:
|
|
case BO_Div:
|
|
case BO_Rem:
|
|
return 5;
|
|
case BO_Add:
|
|
case BO_Sub:
|
|
return 4;
|
|
case BO_And:
|
|
return 3;
|
|
case BO_Xor:
|
|
return 2;
|
|
case BO_Or:
|
|
return 1;
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
static void addParentheses(const Expr *E, const BinaryOperator *ParentBinOp,
|
|
ClangTidyCheck *Check, const SourceManager &SM,
|
|
const LangOptions &LangOpts) {
|
|
if (const auto *Paren = dyn_cast<ParenExpr>(E)) {
|
|
addParentheses(Paren->getSubExpr()->IgnoreImpCasts(), nullptr, Check, SM,
|
|
LangOpts);
|
|
return;
|
|
}
|
|
|
|
const auto *BinOp = dyn_cast<BinaryOperator>(E);
|
|
if (!BinOp)
|
|
return;
|
|
|
|
const int Precedence1 = getPrecedence(BinOp);
|
|
const int Precedence2 = getPrecedence(ParentBinOp);
|
|
|
|
if (ParentBinOp != nullptr && Precedence1 != Precedence2 && Precedence1 > 0 &&
|
|
Precedence2 > 0) {
|
|
const SourceLocation StartLoc = BinOp->getBeginLoc();
|
|
const SourceLocation EndLoc =
|
|
Lexer::getLocForEndOfToken(BinOp->getEndLoc(), 0, SM, LangOpts);
|
|
|
|
auto Diag =
|
|
Check->diag(StartLoc,
|
|
"'%0' has higher precedence than '%1'; add parentheses to "
|
|
"explicitly specify the order of operations")
|
|
<< (Precedence1 > Precedence2 ? BinOp->getOpcodeStr()
|
|
: ParentBinOp->getOpcodeStr())
|
|
<< (Precedence1 > Precedence2 ? ParentBinOp->getOpcodeStr()
|
|
: BinOp->getOpcodeStr())
|
|
<< SourceRange(StartLoc, EndLoc);
|
|
|
|
if (EndLoc.isValid()) {
|
|
Diag << FixItHint::CreateInsertion(StartLoc, "(")
|
|
<< FixItHint::CreateInsertion(EndLoc, ")");
|
|
}
|
|
}
|
|
|
|
addParentheses(BinOp->getLHS()->IgnoreImpCasts(), BinOp, Check, SM, LangOpts);
|
|
addParentheses(BinOp->getRHS()->IgnoreImpCasts(), BinOp, Check, SM, LangOpts);
|
|
}
|
|
|
|
void MathMissingParenthesesCheck::check(
|
|
const MatchFinder::MatchResult &Result) {
|
|
const auto *BinOp = Result.Nodes.getNodeAs<BinaryOperator>("binOp");
|
|
const SourceManager &SM = *Result.SourceManager;
|
|
const LangOptions &LO = Result.Context->getLangOpts();
|
|
addParentheses(BinOp, nullptr, this, SM, LO);
|
|
}
|
|
|
|
} // namespace clang::tidy::readability
|