Skip to content

Commit e65d52a

Browse files
[clang-tidy][readability-redundant-parentheses] add option to prevent widely used work around (#164827)
Part of #164125 Add a new option to ignore some decls. --------- Co-authored-by: EugeneZelenko <[email protected]>
1 parent bf99f66 commit e65d52a

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed

clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "RedundantParenthesesCheck.h"
10+
#include "../utils/Matchers.h"
11+
#include "../utils/OptionsUtils.h"
1012
#include "clang/AST/Expr.h"
1113
#include "clang/ASTMatchers/ASTMatchFinder.h"
1214
#include "clang/ASTMatchers/ASTMatchers.h"
@@ -32,15 +34,30 @@ AST_MATCHER(ParenExpr, isInMacro) {
3234

3335
} // namespace
3436

37+
RedundantParenthesesCheck::RedundantParenthesesCheck(StringRef Name,
38+
ClangTidyContext *Context)
39+
: ClangTidyCheck(Name, Context),
40+
AllowedDecls(utils::options::parseStringList(
41+
Options.get("AllowedDecls", "std::max;std::min"))) {}
42+
43+
void RedundantParenthesesCheck::storeOptions(
44+
ClangTidyOptions::OptionMap &Opts) {
45+
Options.store(Opts, "AllowedDecls",
46+
utils::options::serializeStringList(AllowedDecls));
47+
}
48+
3549
void RedundantParenthesesCheck::registerMatchers(MatchFinder *Finder) {
3650
const auto ConstantExpr =
3751
expr(anyOf(integerLiteral(), floatLiteral(), characterLiteral(),
3852
cxxBoolLiteral(), stringLiteral(), cxxNullPtrLiteralExpr()));
3953
Finder->addMatcher(
40-
parenExpr(subExpr(anyOf(parenExpr(), ConstantExpr, declRefExpr())),
41-
unless(anyOf(isInMacro(),
42-
// sizeof(...) is common used.
43-
hasParent(unaryExprOrTypeTraitExpr()))))
54+
parenExpr(
55+
subExpr(anyOf(parenExpr(), ConstantExpr,
56+
declRefExpr(to(namedDecl(unless(
57+
matchers::matchesAnyListedName(AllowedDecls))))))),
58+
unless(anyOf(isInMacro(),
59+
// sizeof(...) is common used.
60+
hasParent(unaryExprOrTypeTraitExpr()))))
4461
.bind("dup"),
4562
this);
4663
}

clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ namespace clang::tidy::readability {
2020
/// https://clang.llvm.org/extra/clang-tidy/checks/readability/redundant-parentheses.html
2121
class RedundantParenthesesCheck : public ClangTidyCheck {
2222
public:
23-
RedundantParenthesesCheck(StringRef Name, ClangTidyContext *Context)
24-
: ClangTidyCheck(Name, Context) {}
23+
RedundantParenthesesCheck(StringRef Name, ClangTidyContext *Context);
24+
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
2525
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
2626
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
2727
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
2828
return LangOpts.CPlusPlus | LangOpts.C99;
2929
}
30+
31+
private:
32+
const std::vector<StringRef> AllowedDecls;
3033
};
3134

3235
} // namespace clang::tidy::readability

clang-tools-extra/docs/clang-tidy/checks/readability/redundant-parentheses.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,16 @@ affect the semantics.
2727
.. code-block:: c++
2828

2929
int a = (1 * 2) + 3; // no warning
30+
31+
Options
32+
-------
33+
34+
.. option:: AllowedDecls
35+
36+
Semicolon-separated list of regular expressions matching names of declarations
37+
to ignore when the parentheses are around. Declarations can include variables
38+
or functions. The default is an `std::max;std::min`.
39+
40+
Some STL library functions may have the same name as widely used function-like
41+
macro. For example, ``std::max`` and ``max`` macro. A workaround to distinguish
42+
them is adding parentheses around functions to prevent function-like macro.

clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,12 @@ void exceptions() {
6262
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant parentheses around expression [readability-redundant-parentheses]
6363
// CHECK-FIXES: alignof(3);
6464
}
65+
66+
namespace std {
67+
template<class T> T max(T, T);
68+
template<class T> T min(T, T);
69+
} // namespace std
70+
void ignoreStdMaxMin() {
71+
(std::max)(1,2);
72+
(std::min)(1,2);
73+
}

0 commit comments

Comments
 (0)