aboutsummaryrefslogtreecommitdiff
path: root/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
blob: 1d5c6cca5a82dc8ff708fada8d7370c0b9cd17c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//===--- MathMissingParenthesesCheck.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 "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 addParantheses(const BinaryOperator *BinOp,
                           const BinaryOperator *ParentBinOp,
                           ClangTidyCheck *Check,
                           const clang::SourceManager &SM,
                           const clang::LangOptions &LangOpts) {
  if (!BinOp)
    return;

  int Precedence1 = getPrecedence(BinOp);
  int Precedence2 = getPrecedence(ParentBinOp);

  if (ParentBinOp != nullptr && Precedence1 != Precedence2 && Precedence1 > 0 &&
      Precedence2 > 0) {
    const clang::SourceLocation StartLoc = BinOp->getBeginLoc();
    const clang::SourceLocation EndLoc =
        clang::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, ")");
    }
  }

  addParantheses(dyn_cast<BinaryOperator>(BinOp->getLHS()->IgnoreImpCasts()),
                 BinOp, Check, SM, LangOpts);
  addParantheses(dyn_cast<BinaryOperator>(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 clang::LangOptions &LO = Result.Context->getLangOpts();
  addParantheses(BinOp, nullptr, this, SM, LO);
}

} // namespace clang::tidy::readability