Skip to content

Commit 11a411a

Browse files
committed
Revert "[clang-tidy][NFC] Remove duplicated code"
This reverts commit b6f6be4.
1 parent b6f6be4 commit 11a411a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+288
-298
lines changed

clang-tools-extra/clang-tidy/abseil/AbseilMatcher.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_ABSEILMATCHER_H
10-
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_ABSEILMATCHER_H
11-
129
#include "clang/AST/ASTContext.h"
1310
#include "clang/ASTMatchers/ASTMatchFinder.h"
1411
#include <algorithm>
@@ -60,5 +57,3 @@ AST_POLYMORPHIC_MATCHER(
6057
}
6158

6259
} // namespace clang::ast_matchers
63-
64-
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_ABSEILMATCHER_H

clang-tools-extra/clang-tidy/abseil/DurationFactoryFloatCheck.cpp

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

99
#include "DurationFactoryFloatCheck.h"
10-
#include "../utils/LexerUtils.h"
1110
#include "DurationRewriter.h"
1211
#include "clang/AST/ASTContext.h"
1312
#include "clang/ASTMatchers/ASTMatchFinder.h"
@@ -19,6 +18,15 @@ using namespace clang::ast_matchers;
1918

2019
namespace clang::tidy::abseil {
2120

21+
// Returns `true` if `Range` is inside a macro definition.
22+
static bool insideMacroDefinition(const MatchFinder::MatchResult &Result,
23+
SourceRange Range) {
24+
return !clang::Lexer::makeFileCharRange(
25+
clang::CharSourceRange::getCharRange(Range),
26+
*Result.SourceManager, Result.Context->getLangOpts())
27+
.isValid();
28+
}
29+
2230
void DurationFactoryFloatCheck::registerMatchers(MatchFinder *Finder) {
2331
Finder->addMatcher(
2432
callExpr(callee(functionDecl(DurationFactoryFunction())),
@@ -37,9 +45,7 @@ void DurationFactoryFloatCheck::check(const MatchFinder::MatchResult &Result) {
3745
const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>("call");
3846

3947
// Don't try and replace things inside of macro definitions.
40-
if (tidy::utils::lexer::insideMacroDefinition(MatchedCall->getSourceRange(),
41-
*Result.SourceManager,
42-
Result.Context->getLangOpts()))
48+
if (insideMacroDefinition(Result, MatchedCall->getSourceRange()))
4349
return;
4450

4551
const Expr *Arg = MatchedCall->getArg(0)->IgnoreImpCasts();

clang-tools-extra/clang-tidy/abseil/StringFindStrContainsCheck.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,12 @@ using ::clang::transformer::makeRule;
3030
using ::clang::transformer::node;
3131
using ::clang::transformer::RewriteRuleWith;
3232

33-
namespace {
34-
3533
AST_MATCHER(Type, isCharType) { return Node.isCharType(); }
36-
} // namespace
3734

38-
static const char StringLikeClassesDefault[] = "::std::basic_string;"
35+
static const char DefaultStringLikeClasses[] = "::std::basic_string;"
3936
"::std::basic_string_view;"
4037
"::absl::string_view";
41-
static const char AbseilStringsMatchHeaderDefault[] = "absl/strings/match.h";
38+
static const char DefaultAbseilStringsMatchHeader[] = "absl/strings/match.h";
4239

4340
static transformer::RewriteRuleWith<std::string>
4441
makeRewriteRule(ArrayRef<StringRef> StringLikeClassNames,
@@ -87,9 +84,9 @@ StringFindStrContainsCheck::StringFindStrContainsCheck(
8784
StringRef Name, ClangTidyContext *Context)
8885
: TransformerClangTidyCheck(Name, Context),
8986
StringLikeClassesOption(utils::options::parseStringList(
90-
Options.get("StringLikeClasses", StringLikeClassesDefault))),
87+
Options.get("StringLikeClasses", DefaultStringLikeClasses))),
9188
AbseilStringsMatchHeaderOption(Options.get(
92-
"AbseilStringsMatchHeader", AbseilStringsMatchHeaderDefault)) {
89+
"AbseilStringsMatchHeader", DefaultAbseilStringsMatchHeader)) {
9390
setRule(
9491
makeRewriteRule(StringLikeClassesOption, AbseilStringsMatchHeaderOption));
9592
}

clang-tools-extra/clang-tidy/abseil/TimeSubtractionCheck.cpp

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

99
#include "TimeSubtractionCheck.h"
10-
#include "../utils/LexerUtils.h"
1110
#include "DurationRewriter.h"
1211
#include "clang/AST/ASTContext.h"
1312
#include "clang/ASTMatchers/ASTMatchFinder.h"
@@ -19,6 +18,15 @@ using namespace clang::ast_matchers;
1918

2019
namespace clang::tidy::abseil {
2120

21+
// Returns `true` if `Range` is inside a macro definition.
22+
static bool insideMacroDefinition(const MatchFinder::MatchResult &Result,
23+
SourceRange Range) {
24+
return !clang::Lexer::makeFileCharRange(
25+
clang::CharSourceRange::getCharRange(Range),
26+
*Result.SourceManager, Result.Context->getLangOpts())
27+
.isValid();
28+
}
29+
2230
static bool isConstructorAssignment(const MatchFinder::MatchResult &Result,
2331
const Expr *Node) {
2432
// For C++14 and earlier there are elidable constructors that must be matched
@@ -122,9 +130,7 @@ void TimeSubtractionCheck::check(const MatchFinder::MatchResult &Result) {
122130
const auto *BinOp = Result.Nodes.getNodeAs<BinaryOperator>("binop");
123131
std::string InverseName =
124132
Result.Nodes.getNodeAs<FunctionDecl>("func_decl")->getNameAsString();
125-
if (tidy::utils::lexer::insideMacroDefinition(BinOp->getSourceRange(),
126-
*Result.SourceManager,
127-
Result.Context->getLangOpts()))
133+
if (insideMacroDefinition(Result, BinOp->getSourceRange()))
128134
return;
129135

130136
std::optional<DurationScale> Scale = getScaleForTimeInverse(InverseName);
@@ -133,9 +139,7 @@ void TimeSubtractionCheck::check(const MatchFinder::MatchResult &Result) {
133139

134140
const auto *OuterCall = Result.Nodes.getNodeAs<CallExpr>("outer_call");
135141
if (OuterCall) {
136-
if (tidy::utils::lexer::insideMacroDefinition(
137-
OuterCall->getSourceRange(), *Result.SourceManager,
138-
Result.Context->getLangOpts()))
142+
if (insideMacroDefinition(Result, OuterCall->getSourceRange()))
139143
return;
140144

141145
// We're working with the first case of matcher, and need to replace the
@@ -161,9 +165,7 @@ void TimeSubtractionCheck::check(const MatchFinder::MatchResult &Result) {
161165
.bind("arg"))),
162166
*BinOp, *Result.Context));
163167
if (MaybeCallArg && MaybeCallArg->getArg(0)->IgnoreImpCasts() == BinOp &&
164-
!tidy::utils::lexer::insideMacroDefinition(
165-
MaybeCallArg->getSourceRange(), *Result.SourceManager,
166-
Result.Context->getLangOpts())) {
168+
!insideMacroDefinition(Result, MaybeCallArg->getSourceRange())) {
167169
// Handle the case where the matched expression is inside a call which
168170
// converts it from the inverse to a Duration. In this case, we replace
169171
// the outer with just the subtraction expression, which gives the right

clang-tools-extra/clang-tidy/abseil/UpgradeDurationConversionsCheck.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ void UpgradeDurationConversionsCheck::check(
139139

140140
// We gather source locations from template matches not in template
141141
// instantiations for future matches.
142-
auto IsInsideTemplate = stmt(
143-
hasAncestor(decl(anyOf(classTemplateDecl(), functionTemplateDecl()))));
142+
internal::Matcher<Stmt> IsInsideTemplate =
143+
hasAncestor(decl(anyOf(classTemplateDecl(), functionTemplateDecl())));
144144
if (!match(IsInsideTemplate, *ArgExpr, *Result.Context).empty())
145145
MatchedTemplateLocations.insert(Loc);
146146

clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ void BadSignalToKillThreadCheck::registerMatchers(MatchFinder *Finder) {
2525
this);
2626
}
2727

28+
static Preprocessor *PP;
29+
2830
void BadSignalToKillThreadCheck::check(const MatchFinder::MatchResult &Result) {
2931
const auto IsSigterm = [](const auto &KeyValue) -> bool {
3032
return KeyValue.first->getName() == "SIGTERM" &&
3133
KeyValue.first->hasMacroDefinition();
3234
};
3335
const auto TryExpandAsInteger =
34-
[this](Preprocessor::macro_iterator It) -> std::optional<unsigned> {
36+
[](Preprocessor::macro_iterator It) -> std::optional<unsigned> {
3537
if (It == PP->macro_end())
3638
return std::nullopt;
3739
const MacroInfo *MI = PP->getMacroInfo(It->first);
@@ -62,8 +64,8 @@ void BadSignalToKillThreadCheck::check(const MatchFinder::MatchResult &Result) {
6264
}
6365

6466
void BadSignalToKillThreadCheck::registerPPCallbacks(
65-
const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
66-
this->PP = PP;
67+
const SourceManager &SM, Preprocessor *Pp, Preprocessor *ModuleExpanderPP) {
68+
PP = Pp;
6769
}
6870

6971
} // namespace clang::tidy::bugprone

clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ class BadSignalToKillThreadCheck : public ClangTidyCheck {
2626
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
2727
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
2828
Preprocessor *ModuleExpanderPP) override;
29-
30-
private:
3129
std::optional<unsigned> SigtermValue;
32-
Preprocessor *PP = nullptr;
3330
};
3431

3532
} // namespace clang::tidy::bugprone

clang-tools-extra/clang-tidy/bugprone/DynamicStaticInitializersCheck.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ using namespace clang::ast_matchers;
1515

1616
namespace clang::tidy::bugprone {
1717

18-
namespace {
19-
2018
AST_MATCHER(clang::VarDecl, hasConstantDeclaration) {
2119
const Expr *Init = Node.getInit();
2220
if (Init && !Init->isValueDependent()) {
@@ -27,8 +25,6 @@ AST_MATCHER(clang::VarDecl, hasConstantDeclaration) {
2725
return false;
2826
}
2927

30-
} // namespace
31-
3228
DynamicStaticInitializersCheck::DynamicStaticInitializersCheck(
3329
StringRef Name, ClangTidyContext *Context)
3430
: ClangTidyCheck(Name, Context),

clang-tools-extra/clang-tidy/bugprone/EasilySwappableParametersCheck.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,8 +1881,6 @@ static bool prefixSuffixCoverUnderThreshold(std::size_t Threshold,
18811881

18821882
} // namespace filter
18831883

1884-
namespace {
1885-
18861884
/// Matches functions that have at least the specified amount of parameters.
18871885
AST_MATCHER_P(FunctionDecl, parameterCountGE, unsigned, N) {
18881886
return Node.getNumParams() >= N;
@@ -1905,8 +1903,6 @@ AST_MATCHER(FunctionDecl, isOverloadedUnaryOrBinaryOperator) {
19051903
}
19061904
}
19071905

1908-
} // namespace
1909-
19101906
/// Returns the DefaultMinimumLength if the Value of requested minimum length
19111907
/// is less than 2. Minimum lengths of 0 or 1 are not accepted.
19121908
static inline unsigned clampMinimumLength(const unsigned Value) {

clang-tools-extra/clang-tidy/bugprone/EmptyCatchCheck.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ using ::clang::ast_matchers::internal::Matcher;
2020
namespace clang::tidy::bugprone {
2121

2222
namespace {
23-
AST_MATCHER(CXXCatchStmt, isCatchInMacro) {
23+
AST_MATCHER(CXXCatchStmt, isInMacro) {
2424
return Node.getBeginLoc().isMacroID() || Node.getEndLoc().isMacroID() ||
2525
Node.getCatchLoc().isMacroID();
2626
}
@@ -89,8 +89,7 @@ void EmptyCatchCheck::registerMatchers(MatchFinder *Finder) {
8989
hasCanonicalType(AllowedNamedExceptionTypes)));
9090

9191
Finder->addMatcher(
92-
cxxCatchStmt(unless(isExpansionInSystemHeader()),
93-
unless(isCatchInMacro()),
92+
cxxCatchStmt(unless(isExpansionInSystemHeader()), unless(isInMacro()),
9493
unless(hasCaughtType(IgnoredExceptionType)),
9594
hasHandler(compoundStmt(
9695
statementCountIs(0),

clang-tools-extra/clang-tidy/bugprone/IncDecInConditionsCheck.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ using namespace clang::ast_matchers;
1515

1616
namespace clang::tidy::bugprone {
1717

18-
namespace {
1918
AST_MATCHER(BinaryOperator, isLogicalOperator) { return Node.isLogicalOp(); }
2019

2120
AST_MATCHER(UnaryOperator, isUnaryPrePostOperator) {
@@ -27,8 +26,6 @@ AST_MATCHER(CXXOperatorCallExpr, isPrePostOperator) {
2726
Node.getOperator() == OO_MinusMinus;
2827
}
2928

30-
} // namespace
31-
3229
void IncDecInConditionsCheck::registerMatchers(MatchFinder *Finder) {
3330
auto OperatorMatcher = expr(
3431
anyOf(binaryOperator(anyOf(isComparisonOperator(), isLogicalOperator())),

clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ AST_MATCHER(FunctionType, typeHasNoReturnAttr) {
3333
} // namespace ast_matchers
3434
namespace tidy::bugprone {
3535

36-
static ast_matchers::internal::Matcher<Stmt>
37-
loopEndingStmt(ast_matchers::internal::Matcher<Stmt> Internal) {
38-
ast_matchers::internal::Matcher<QualType> isNoReturnFunType =
36+
static internal::Matcher<Stmt>
37+
loopEndingStmt(internal::Matcher<Stmt> Internal) {
38+
internal::Matcher<QualType> isNoReturnFunType =
3939
ignoringParens(functionType(typeHasNoReturnAttr()));
40-
ast_matchers::internal::Matcher<Decl> isNoReturnDecl =
40+
internal::Matcher<Decl> isNoReturnDecl =
4141
anyOf(declHasNoReturnAttr(), functionDecl(hasType(isNoReturnFunType)),
4242
varDecl(hasType(blockPointerType(pointee(isNoReturnFunType)))));
4343

clang-tools-extra/clang-tidy/bugprone/MultipleNewInOneExpressionCheck.cpp

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

99
#include "MultipleNewInOneExpressionCheck.h"
10-
#include "../utils/Matchers.h"
1110
#include "clang/AST/ASTContext.h"
1211
#include "clang/ASTMatchers/ASTMatchFinder.h"
1312
#include "clang/Lex/Lexer.h"
@@ -52,6 +51,29 @@ bool isExprValueStored(const Expr *E, ASTContext &C) {
5251

5352
} // namespace
5453

54+
AST_MATCHER_P(CXXTryStmt, hasHandlerFor,
55+
ast_matchers::internal::Matcher<QualType>, InnerMatcher) {
56+
for (unsigned NH = Node.getNumHandlers(), I = 0; I < NH; ++I) {
57+
const CXXCatchStmt *CatchS = Node.getHandler(I);
58+
// Check for generic catch handler (match anything).
59+
if (CatchS->getCaughtType().isNull())
60+
return true;
61+
ast_matchers::internal::BoundNodesTreeBuilder Result(*Builder);
62+
if (InnerMatcher.matches(CatchS->getCaughtType(), Finder, &Result)) {
63+
*Builder = std::move(Result);
64+
return true;
65+
}
66+
}
67+
return false;
68+
}
69+
70+
AST_MATCHER(CXXNewExpr, mayThrow) {
71+
FunctionDecl *OperatorNew = Node.getOperatorNew();
72+
if (!OperatorNew)
73+
return false;
74+
return !OperatorNew->getType()->castAs<FunctionProtoType>()->isNothrow();
75+
}
76+
5577
void MultipleNewInOneExpressionCheck::registerMatchers(MatchFinder *Finder) {
5678
auto BadAllocType =
5779
recordType(hasDeclaration(cxxRecordDecl(hasName("::std::bad_alloc"))));
@@ -63,10 +85,9 @@ void MultipleNewInOneExpressionCheck::registerMatchers(MatchFinder *Finder) {
6385
auto CatchBadAllocType =
6486
qualType(hasCanonicalType(anyOf(BadAllocType, BadAllocReferenceType,
6587
ExceptionType, ExceptionReferenceType)));
66-
auto BadAllocCatchingTryBlock =
67-
cxxTryStmt(matchers::hasHandlerFor(CatchBadAllocType));
88+
auto BadAllocCatchingTryBlock = cxxTryStmt(hasHandlerFor(CatchBadAllocType));
6889

69-
auto NewExprMayThrow = cxxNewExpr(matchers::mayThrow());
90+
auto NewExprMayThrow = cxxNewExpr(mayThrow());
7091
auto HasNewExpr1 = expr(anyOf(NewExprMayThrow.bind("new1"),
7192
hasDescendant(NewExprMayThrow.bind("new1"))));
7293
auto HasNewExpr2 = expr(anyOf(NewExprMayThrow.bind("new2"),
@@ -94,7 +115,7 @@ void MultipleNewInOneExpressionCheck::registerMatchers(MatchFinder *Finder) {
94115
hasAncestor(BadAllocCatchingTryBlock)),
95116
this);
96117
Finder->addMatcher(
97-
cxxNewExpr(matchers::mayThrow(),
118+
cxxNewExpr(mayThrow(),
98119
hasDescendant(NewExprMayThrow.bind("new2_in_new1")),
99120
hasAncestor(BadAllocCatchingTryBlock))
100121
.bind("new1"),

clang-tools-extra/clang-tidy/bugprone/MultipleStatementMacroCheck.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@ namespace clang::tidy::bugprone {
1616

1717
namespace {
1818

19-
AST_MATCHER(Expr, isExprInMacro) { return Node.getBeginLoc().isMacroID(); }
20-
21-
} // namespace
19+
AST_MATCHER(Expr, isInMacro) { return Node.getBeginLoc().isMacroID(); }
2220

2321
/// Find the next statement after `S`.
24-
static const Stmt *nextStmt(const MatchFinder::MatchResult &Result,
25-
const Stmt *S) {
22+
const Stmt *nextStmt(const MatchFinder::MatchResult &Result, const Stmt *S) {
2623
auto Parents = Result.Context->getParents(*S);
2724
if (Parents.empty())
2825
return nullptr;
@@ -43,8 +40,8 @@ using ExpansionRanges = std::vector<SourceRange>;
4340
/// \brief Get all the macro expansion ranges related to `Loc`.
4441
///
4542
/// The result is ordered from most inner to most outer.
46-
static ExpansionRanges
47-
getExpansionRanges(SourceLocation Loc, const MatchFinder::MatchResult &Result) {
43+
ExpansionRanges getExpansionRanges(SourceLocation Loc,
44+
const MatchFinder::MatchResult &Result) {
4845
ExpansionRanges Locs;
4946
while (Loc.isMacroID()) {
5047
Locs.push_back(
@@ -54,9 +51,10 @@ getExpansionRanges(SourceLocation Loc, const MatchFinder::MatchResult &Result) {
5451
return Locs;
5552
}
5653

54+
} // namespace
55+
5756
void MultipleStatementMacroCheck::registerMatchers(MatchFinder *Finder) {
58-
const auto Inner =
59-
expr(isExprInMacro(), unless(compoundStmt())).bind("inner");
57+
const auto Inner = expr(isInMacro(), unless(compoundStmt())).bind("inner");
6058
Finder->addMatcher(
6159
stmt(anyOf(ifStmt(hasThen(Inner)), ifStmt(hasElse(Inner)).bind("else"),
6260
whileStmt(hasBody(Inner)), forStmt(hasBody(Inner))))

clang-tools-extra/clang-tidy/bugprone/SignalHandlerCheck.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,12 +322,12 @@ SourceRange getSourceRangeOfStmt(const Stmt *S, ASTContext &Ctx) {
322322
return P.getSourceRange();
323323
}
324324

325+
} // namespace
326+
325327
AST_MATCHER(FunctionDecl, isStandardFunction) {
326328
return isStandardFunction(&Node);
327329
}
328330

329-
} // namespace
330-
331331
SignalHandlerCheck::SignalHandlerCheck(StringRef Name,
332332
ClangTidyContext *Context)
333333
: ClangTidyCheck(Name, Context),

0 commit comments

Comments
 (0)