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
|
//===----------------------------------------------------------------------===//
//
// 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 "RedundantParenthesesCheck.h"
#include "clang/AST/Expr.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchersMacros.h"
#include <cassert>
using namespace clang::ast_matchers;
namespace clang::tidy::readability {
namespace {
AST_MATCHER_P(ParenExpr, subExpr, ast_matchers::internal::Matcher<Expr>,
InnerMatcher) {
return InnerMatcher.matches(*Node.getSubExpr(), Finder, Builder);
}
AST_MATCHER(ParenExpr, isInMacro) {
const Expr *E = Node.getSubExpr();
return Node.getLParen().isMacroID() || Node.getRParen().isMacroID() ||
E->getBeginLoc().isMacroID() || E->getEndLoc().isMacroID();
}
} // namespace
void RedundantParenthesesCheck::registerMatchers(MatchFinder *Finder) {
const auto ConstantExpr =
expr(anyOf(integerLiteral(), floatLiteral(), characterLiteral(),
cxxBoolLiteral(), stringLiteral(), cxxNullPtrLiteralExpr()));
Finder->addMatcher(
parenExpr(subExpr(anyOf(parenExpr(), ConstantExpr, declRefExpr())),
unless(anyOf(isInMacro(),
// sizeof(...) is common used.
hasParent(unaryExprOrTypeTraitExpr()))))
.bind("dup"),
this);
}
void RedundantParenthesesCheck::check(const MatchFinder::MatchResult &Result) {
const auto *PE = Result.Nodes.getNodeAs<ParenExpr>("dup");
diag(PE->getBeginLoc(), "redundant parentheses around expression")
<< FixItHint::CreateRemoval(PE->getLParen())
<< FixItHint::CreateRemoval(PE->getRParen());
}
} // namespace clang::tidy::readability
|