clang-tools 19.0.0git
MathMissingParenthesesCheck.cpp
Go to the documentation of this file.
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
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/Lex/Lexer.h"
13
14using namespace clang::ast_matchers;
15
17
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
28static 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}
49static 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) {
61 const clang::SourceLocation StartLoc = BinOp->getBeginLoc();
62 const clang::SourceLocation EndLoc =
63 clang::Lexer::getLocForEndOfToken(BinOp->getEndLoc(), 0, SM, LangOpts);
64
65 auto Diag =
66 Check->diag(StartLoc,
67 "'%0' has higher precedence than '%1'; add parentheses to "
68 "explicitly specify the order of operations")
69 << (Precedence1 > Precedence2 ? BinOp->getOpcodeStr()
70 : ParentBinOp->getOpcodeStr())
71 << (Precedence1 > Precedence2 ? ParentBinOp->getOpcodeStr()
72 : BinOp->getOpcodeStr())
73 << SourceRange(StartLoc, EndLoc);
74
75 if (EndLoc.isValid()) {
76 Diag << FixItHint::CreateInsertion(StartLoc, "(")
77 << FixItHint::CreateInsertion(EndLoc, ")");
78 }
79 }
80
81 addParantheses(dyn_cast<BinaryOperator>(BinOp->getLHS()->IgnoreImpCasts()),
82 BinOp, Check, SM, LangOpts);
83 addParantheses(dyn_cast<BinaryOperator>(BinOp->getRHS()->IgnoreImpCasts()),
84 BinOp, Check, SM, LangOpts);
85}
86
88 const MatchFinder::MatchResult &Result) {
89 const auto *BinOp = Result.Nodes.getNodeAs<BinaryOperator>("binOp");
90 std::vector<
91 std::pair<clang::SourceRange, std::pair<const clang::BinaryOperator *,
92 const clang::BinaryOperator *>>>
93 Insertions;
94 const SourceManager &SM = *Result.SourceManager;
95 const clang::LangOptions &LO = Result.Context->getLangOpts();
96 addParantheses(BinOp, nullptr, this, SM, LO);
97}
98
99} // namespace clang::tidy::readability
Base class for all clang-tidy checks.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
static int getPrecedence(const BinaryOperator *BinOp)
static void addParantheses(const BinaryOperator *BinOp, const BinaryOperator *ParentBinOp, ClangTidyCheck *Check, const clang::SourceManager &SM, const clang::LangOptions &LangOpts)