1 //===--- MathMissingParenthesesCheck.cpp - clang-tidy ---------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "MathMissingParenthesesCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Lex/Lexer.h"
13
14 using namespace clang::ast_matchers;
15
16 namespace clang::tidy::readability {
17
registerMatchers(MatchFinder * Finder)18 void MathMissingParenthesesCheck::registerMatchers(MatchFinder *Finder) {
19 Finder->addMatcher(binaryOperator(unless(hasParent(binaryOperator())),
20 unless(isAssignmentOperator()),
21 unless(isComparisonOperator()),
22 unless(hasAnyOperatorName("&&", "||")),
23 hasDescendant(binaryOperator()))
24 .bind("binOp"),
25 this);
26 }
27
getPrecedence(const BinaryOperator * BinOp)28 static int getPrecedence(const BinaryOperator *BinOp) {
29 if (!BinOp)
30 return 0;
31 switch (BinOp->getOpcode()) {
32 case BO_Mul:
33 case BO_Div:
34 case BO_Rem:
35 return 5;
36 case BO_Add:
37 case BO_Sub:
38 return 4;
39 case BO_And:
40 return 3;
41 case BO_Xor:
42 return 2;
43 case BO_Or:
44 return 1;
45 default:
46 return 0;
47 }
48 }
addParantheses(const BinaryOperator * BinOp,const BinaryOperator * ParentBinOp,ClangTidyCheck * Check,const clang::SourceManager & SM,const clang::LangOptions & LangOpts)49 static void addParantheses(const BinaryOperator *BinOp,
50 const BinaryOperator *ParentBinOp,
51 ClangTidyCheck *Check,
52 const clang::SourceManager &SM,
53 const clang::LangOptions &LangOpts) {
54 if (!BinOp)
55 return;
56
57 int Precedence1 = getPrecedence(BinOp);
58 int Precedence2 = getPrecedence(ParentBinOp);
59
60 if (ParentBinOp != nullptr && Precedence1 != Precedence2 && Precedence1 > 0 &&
61 Precedence2 > 0) {
62 const clang::SourceLocation StartLoc = BinOp->getBeginLoc();
63 const clang::SourceLocation EndLoc =
64 clang::Lexer::getLocForEndOfToken(BinOp->getEndLoc(), 0, SM, LangOpts);
65
66 auto Diag =
67 Check->diag(StartLoc,
68 "'%0' has higher precedence than '%1'; add parentheses to "
69 "explicitly specify the order of operations")
70 << (Precedence1 > Precedence2 ? BinOp->getOpcodeStr()
71 : ParentBinOp->getOpcodeStr())
72 << (Precedence1 > Precedence2 ? ParentBinOp->getOpcodeStr()
73 : BinOp->getOpcodeStr())
74 << SourceRange(StartLoc, EndLoc);
75
76 if (EndLoc.isValid()) {
77 Diag << FixItHint::CreateInsertion(StartLoc, "(")
78 << FixItHint::CreateInsertion(EndLoc, ")");
79 }
80 }
81
82 addParantheses(dyn_cast<BinaryOperator>(BinOp->getLHS()->IgnoreImpCasts()),
83 BinOp, Check, SM, LangOpts);
84 addParantheses(dyn_cast<BinaryOperator>(BinOp->getRHS()->IgnoreImpCasts()),
85 BinOp, Check, SM, LangOpts);
86 }
87
check(const MatchFinder::MatchResult & Result)88 void MathMissingParenthesesCheck::check(
89 const MatchFinder::MatchResult &Result) {
90 const auto *BinOp = Result.Nodes.getNodeAs<BinaryOperator>("binOp");
91 std::vector<
92 std::pair<clang::SourceRange, std::pair<const clang::BinaryOperator *,
93 const clang::BinaryOperator *>>>
94 Insertions;
95 const SourceManager &SM = *Result.SourceManager;
96 const clang::LangOptions &LO = Result.Context->getLangOpts();
97 addParantheses(BinOp, nullptr, this, SM, LO);
98 }
99
100 } // namespace clang::tidy::readability
101