-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Add clang-tidy check readability-math-missing-parentheses #84481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8fdf630
add clang-tidy check readability-math-missing-parentheses
11happy dcf739e
use ast,remove manual parsing
11happy de33e97
add ignoreimpcasts, use const variables, use createinsertion instead …
11happy fa95ab7
remove unnecessary header
11happy 112692a
major fix
11happy 1e0331a
fix merge conflict
11happy 399605b
empty commit
11happy 562fc31
small fixes
11happy 183d123
correct clarify
11happy 7e3f39c
operator wise warning
11happy c6acf02
remove the vector storing locations
11happy 41818f5
add macro tests
11happy bae50eb
fix merge conflict
11happy 6700402
remove trailing whitespace and stream diagnostic directly
11happy f3cb538
format code
11happy e103563
return for invalid endloc
11happy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
//===--- 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(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) { | ||
const clang::SourceLocation StartLoc = BinOp->getBeginLoc(); | ||
const clang::SourceLocation EndLoc = | ||
clang::Lexer::getLocForEndOfToken(BinOp->getEndLoc(), 0, SM, LangOpts); | ||
if (EndLoc.isInvalid()) | ||
return; | ||
|
||
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()) | ||
<< FixItHint::CreateInsertion(StartLoc, "(") | ||
<< FixItHint::CreateInsertion(EndLoc, ")") | ||
<< SourceRange(StartLoc, 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"); | ||
std::vector< | ||
std::pair<clang::SourceRange, std::pair<const clang::BinaryOperator *, | ||
const clang::BinaryOperator *>>> | ||
Insertions; | ||
const SourceManager &SM = *Result.SourceManager; | ||
const clang::LangOptions &LO = Result.Context->getLangOpts(); | ||
addParantheses(BinOp, nullptr, this, SM, LO); | ||
} | ||
|
||
} // namespace clang::tidy::readability |
34 changes: 34 additions & 0 deletions
34
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//===--- MathMissingParenthesesCheck.h - clang-tidy -------------*- 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_MATHMISSINGPARENTHESESCHECK_H | ||
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_MATHMISSINGPARENTHESESCHECK_H | ||
|
||
#include "../ClangTidyCheck.h" | ||
|
||
namespace clang::tidy::readability { | ||
|
||
/// Check for mising parantheses in mathematical expressions that involve | ||
/// operators of different priorities. | ||
/// | ||
/// For the user-facing documentation see: | ||
/// http://clang.llvm.org/extra/clang-tidy/checks/readability/math-missing-parentheses.html | ||
class MathMissingParenthesesCheck : public ClangTidyCheck { | ||
public: | ||
MathMissingParenthesesCheck(StringRef Name, ClangTidyContext *Context) | ||
: ClangTidyCheck(Name, Context) {} | ||
void registerMatchers(ast_matchers::MatchFinder *Finder) override; | ||
void check(const ast_matchers::MatchFinder::MatchResult &Result) override; | ||
PiotrZSL marked this conversation as resolved.
Show resolved
Hide resolved
|
||
std::optional<TraversalKind> getCheckTraversalKind() const override { | ||
return TK_IgnoreUnlessSpelledInSource; | ||
} | ||
}; | ||
|
||
} // namespace clang::tidy::readability | ||
|
||
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_MATHMISSINGPARENTHESESCHECK_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
clang-tools-extra/docs/clang-tidy/checks/readability/math-missing-parentheses.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
.. title:: clang-tidy - readability-math-missing-parentheses | ||
|
||
readability-math-missing-parentheses | ||
==================================== | ||
|
||
Check for missing parentheses in mathematical expressions that involve operators | ||
of different priorities. | ||
|
||
PiotrZSL marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Parentheses in mathematical expressions clarify the order | ||
of operations, especially with different-priority operators. Lengthy or multiline | ||
expressions can obscure this order, leading to coding errors. IDEs can aid clarity | ||
by highlighting parentheses. Explicitly using parentheses also clarifies what the | ||
developer had in mind when writing the expression. Ensuring their presence reduces | ||
ambiguity and errors, promoting clearer and more maintainable code. | ||
|
||
Before: | ||
|
||
.. code-block:: c++ | ||
|
||
int x = 1 + 2 * 3 - 4 / 5; | ||
|
||
|
||
After: | ||
|
||
.. code-block:: c++ | ||
|
||
int x = 1 + (2 * 3) - (4 / 5); |
120 changes: 120 additions & 0 deletions
120
clang-tools-extra/test/clang-tidy/checkers/readability/math-missing-parentheses.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// RUN: %check_clang_tidy %s readability-math-missing-parentheses %t | ||
|
||
#define MACRO_AND & | ||
#define MACRO_ADD + | ||
#define MACRO_OR | | ||
#define MACRO_MULTIPLY * | ||
#define MACRO_XOR ^ | ||
#define MACRO_SUBTRACT - | ||
#define MACRO_DIVIDE / | ||
|
||
int foo(){ | ||
return 5; | ||
} | ||
|
||
int bar(){ | ||
return 4; | ||
} | ||
|
||
class fun{ | ||
public: | ||
int A; | ||
double B; | ||
fun(){ | ||
A = 5; | ||
B = 5.4; | ||
} | ||
}; | ||
|
||
void f(){ | ||
//CHECK-MESSAGES: :[[@LINE+2]]:17: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] | ||
//CHECK-FIXES: int a = 1 + (2 * 3); | ||
int a = 1 + 2 * 3; | ||
|
||
int a_negative = 1 + (2 * 3); // No warning | ||
|
||
int b = 1 + 2 + 3; // No warning | ||
|
||
int c = 1 * 2 * 3; // No warning | ||
|
||
//CHECK-MESSAGES: :[[@LINE+3]]:17: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] | ||
//CHECK-MESSAGES: :[[@LINE+2]]:25: warning: '/' has higher precedence than '-'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] | ||
//CHECK-FIXES: int d = 1 + (2 * 3) - (4 / 5); | ||
int d = 1 + 2 * 3 - 4 / 5; | ||
PiotrZSL marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
int d_negative = 1 + (2 * 3) - (4 / 5); // No warning | ||
|
||
//CHECK-MESSAGES: :[[@LINE+4]]:13: warning: '&' has higher precedence than '|'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] | ||
//CHECK-MESSAGES: :[[@LINE+3]]:17: warning: '+' has higher precedence than '&'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] | ||
//CHECK-MESSAGES: :[[@LINE+2]]:25: warning: '*' has higher precedence than '|'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] | ||
//CHECK-FIXES: int e = (1 & (2 + 3)) | (4 * 5); | ||
int e = 1 & 2 + 3 | 4 * 5; | ||
|
||
int e_negative = (1 & (2 + 3)) | (4 * 5); // No warning | ||
|
||
//CHECK-MESSAGES: :[[@LINE+2]]:13: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] | ||
//CHECK-FIXES: int f = (1 * -2) + 4; | ||
int f = 1 * -2 + 4; | ||
|
||
int f_negative = (1 * -2) + 4; // No warning | ||
|
||
//CHECK-MESSAGES: :[[@LINE+2]]:13: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] | ||
//CHECK-FIXES: int g = (1 * 2 * 3) + 4 + 5; | ||
int g = 1 * 2 * 3 + 4 + 5; | ||
|
||
int g_negative = (1 * 2 * 3) + 4 + 5; // No warning | ||
|
||
//CHECK-MESSAGES: :[[@LINE+4]]:13: warning: '&' has higher precedence than '|'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] | ||
//CHECK-MESSAGES: :[[@LINE+3]]:19: warning: '+' has higher precedence than '&'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] | ||
//CHECK-MESSAGES: :[[@LINE+2]]:27: warning: '*' has higher precedence than '|'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] | ||
//CHECK-FIXES: int h = (120 & (2 + 3)) | (22 * 5); | ||
int h = 120 & 2 + 3 | 22 * 5; | ||
|
||
int h_negative = (120 & (2 + 3)) | (22 * 5); // No warning | ||
|
||
int i = 1 & 2 & 3; // No warning | ||
|
||
int j = 1 | 2 | 3; // No warning | ||
|
||
int k = 1 ^ 2 ^ 3; // No warning | ||
|
||
//CHECK-MESSAGES: :[[@LINE+2]]:13: warning: '+' has higher precedence than '^'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] | ||
//CHECK-FIXES: int l = (1 + 2) ^ 3; | ||
int l = 1 + 2 ^ 3; | ||
|
||
int l_negative = (1 + 2) ^ 3; // No warning | ||
|
||
//CHECK-MESSAGES: :[[@LINE+2]]:13: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] | ||
//CHECK-FIXES: int m = (2 * foo()) + bar(); | ||
int m = 2 * foo() + bar(); | ||
|
||
int m_negative = (2 * foo()) + bar(); // No warning | ||
|
||
//CHECK-MESSAGES: :[[@LINE+2]]:13: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] | ||
//CHECK-FIXES: int n = (1.05 * foo()) + double(bar()); | ||
int n = 1.05 * foo() + double(bar()); | ||
|
||
int n_negative = (1.05 * foo()) + double(bar()); // No warning | ||
|
||
//CHECK-MESSAGES: :[[@LINE+3]]:17: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] | ||
//CHECK-FIXES: int o = 1 + (obj.A * 3) + obj.B; | ||
fun obj; | ||
int o = 1 + obj.A * 3 + obj.B; | ||
|
||
int o_negative = 1 + (obj.A * 3) + obj.B; // No warning | ||
|
||
//CHECK-MESSAGES: :[[@LINE+2]]:18: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] | ||
//CHECK-FIXES: int p = 1U + (2 * 3); | ||
int p = 1U + 2 * 3; | ||
|
||
int p_negative = 1U + (2 * 3); // No warning | ||
|
||
//CHECK-MESSAGES: :[[@LINE+7]]:13: warning: '+' has higher precedence than '|'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] | ||
//CHECK-MESSAGES: :[[@LINE+6]]:25: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] | ||
//CHECK-MESSAGES: :[[@LINE+5]]:53: warning: '&' has higher precedence than '^'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] | ||
//CHECK-MESSAGES: :[[@LINE+4]]:53: warning: '^' has higher precedence than '|'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] | ||
//CHECK-MESSAGES: :[[@LINE+3]]:77: warning: '-' has higher precedence than '^'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] | ||
//CHECK-MESSAGES: :[[@LINE+2]]:94: warning: '/' has higher precedence than '-'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] | ||
//CHECK-FIXES: int q = (1 MACRO_ADD (2 MACRO_MULTIPLY 3)) MACRO_OR ((4 MACRO_AND 5) MACRO_XOR (6 MACRO_SUBTRACT (7 MACRO_DIVIDE 8))); | ||
int q = 1 MACRO_ADD 2 MACRO_MULTIPLY 3 MACRO_OR 4 MACRO_AND 5 MACRO_XOR 6 MACRO_SUBTRACT 7 MACRO_DIVIDE 8; // No warning | ||
} | ||
PiotrZSL marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.