clang-tools 24.0.0git
RedundantParenthesesCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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 "../utils/Matchers.h"
12#include "clang/AST/Expr.h"
13#include "clang/AST/TypeLoc.h"
14#include "clang/ASTMatchers/ASTMatchFinder.h"
15#include "clang/ASTMatchers/ASTMatchers.h"
16#include "clang/ASTMatchers/ASTMatchersMacros.h"
17#include "clang/Lex/Lexer.h"
18#include <cassert>
19
20using namespace clang::ast_matchers;
21
23
24namespace {
25
26AST_MATCHER_P(ParenExpr, subExpr, ast_matchers::internal::Matcher<Expr>,
27 InnerMatcher) {
28 return InnerMatcher.matches(*Node.getSubExpr(), Finder, Builder);
29}
30
31AST_MATCHER(ParenExpr, isInMacro) {
32 const Expr *E = Node.getSubExpr();
33 return Node.getLParen().isMacroID() || Node.getRParen().isMacroID() ||
34 E->getBeginLoc().isMacroID() || E->getEndLoc().isMacroID();
35}
36
37AST_MATCHER(TypeLoc, isTypeOfExprTypeLoc) {
38 return !Node.getUnqualifiedLoc().getAs<TypeOfExprTypeLoc>().isNull();
39}
40
41} // namespace
42
43static FixItHint createSpacedRemoval(SourceLocation Loc,
44 const SourceManager &SM,
45 const LangOptions &LangOpts) {
46 if (Loc.isValid() && !Loc.isMacroID()) {
47 const auto LocInfo = SM.getDecomposedLoc(Loc);
48 bool Invalid = false;
49 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
50 if (!Invalid && LocInfo.second > 0 && LocInfo.second + 1 < Buffer.size() &&
51 Lexer::isAsciiIdentifierContinueChar(Buffer[LocInfo.second - 1],
52 LangOpts) &&
53 Lexer::isAsciiIdentifierContinueChar(Buffer[LocInfo.second + 1],
54 LangOpts))
55 return FixItHint::CreateReplacement(SourceRange(Loc, Loc), " ");
56 }
57 return FixItHint::CreateRemoval(Loc);
58}
59
61 ClangTidyContext *Context)
62 : ClangTidyCheck(Name, Context),
63 AllowedDecls(utils::options::parseStringList(
64 Options.get("AllowedDecls", "std::max;std::min"))) {}
65
68 Options.store(Opts, "AllowedDecls",
70}
71
73 const auto ConstantExpr =
74 expr(anyOf(integerLiteral(), floatLiteral(), characterLiteral(),
75 cxxBoolLiteral(), stringLiteral(), cxxNullPtrLiteralExpr()));
76 Finder->addMatcher(
77 parenExpr(subExpr(anyOf(
78 parenExpr(), ConstantExpr,
79 declRefExpr(to(namedDecl(unless(
81 memberExpr(),
82 callExpr(unless(cxxOperatorCallExpr(
83 unless(hasAnyOperatorName("()", "[]"))))),
84 arraySubscriptExpr())),
85 unless(anyOf(isInMacro(),
86 // sizeof(...) is common used.
87 hasParent(unaryExprOrTypeTraitExpr()),
88 // typeof(...) parentheses are required syntax.
89 hasParent(typeLoc(isTypeOfExprTypeLoc())))))
90 .bind("dup"),
91 this);
92}
93
94void RedundantParenthesesCheck::check(const MatchFinder::MatchResult &Result) {
95 const auto *PE = Result.Nodes.getNodeAs<ParenExpr>("dup");
96 diag(PE->getBeginLoc(), "redundant parentheses around expression")
97 << createSpacedRemoval(PE->getLParen(), *Result.SourceManager,
98 getLangOpts())
99 << FixItHint::CreateRemoval(PE->getRParen());
100}
101
102} // namespace clang::tidy::readability
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
RedundantParenthesesCheck(StringRef Name, ClangTidyContext *Context)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
AST_MATCHER_P(Stmt, isStatementIdenticalToBoundNode, std::string, ID)
AST_MATCHER(BinaryOperator, isRelationalOperator)
inline ::clang::ast_matchers::internal::Matcher< NamedDecl > matchesAnyListedRegexName(llvm::ArrayRef< StringRef > NameList)
static FixItHint createSpacedRemoval(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
std::string serializeStringList(ArrayRef< StringRef > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
llvm::StringMap< ClangTidyValue > OptionMap