Skip to content

Commit fbae463

Browse files
committed
[clang-tidy][nfc] refactor use-trailing-return-type-check private methods to be static functions and fix styling issues
1 parent 1043f5c commit fbae463

File tree

3 files changed

+37
-51
lines changed

3 files changed

+37
-51
lines changed

clang-tools-extra/clang-tidy/modernize/UseTrailingReturnTypeCheck.cpp

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ struct UnqualNameVisitor : public RecursiveASTVisitor<UnqualNameVisitor> {
113113
};
114114
} // namespace
115115

116-
constexpr llvm::StringLiteral Message =
116+
constexpr llvm::StringLiteral MessageFunction =
117117
"use a trailing return type for this function";
118118

119119
static SourceLocation expandIfMacroId(SourceLocation Loc,
@@ -125,7 +125,7 @@ static SourceLocation expandIfMacroId(SourceLocation Loc,
125125
return Loc;
126126
}
127127

128-
SourceLocation UseTrailingReturnTypeCheck::findTrailingReturnTypeSourceLocation(
128+
static SourceLocation findTrailingReturnTypeSourceLocation(
129129
const FunctionDecl &F, const FunctionTypeLoc &FTL, const ASTContext &Ctx,
130130
const SourceManager &SM, const LangOptions &LangOpts) {
131131
// We start with the location of the closing parenthesis.
@@ -217,10 +217,11 @@ classifyToken(const FunctionDecl &F, Preprocessor &PP, Token Tok) {
217217
return CT;
218218
}
219219

220-
std::optional<SmallVector<ClassifiedToken, 8>>
221-
UseTrailingReturnTypeCheck::classifyTokensBeforeFunctionName(
222-
const FunctionDecl &F, const ASTContext &Ctx, const SourceManager &SM,
223-
const LangOptions &LangOpts) {
220+
static std::optional<SmallVector<ClassifiedToken, 8>>
221+
classifyTokensBeforeFunctionName(const FunctionDecl &F, const ASTContext &Ctx,
222+
const SourceManager &SM,
223+
const LangOptions &LangOpts,
224+
Preprocessor *PP) {
224225
SourceLocation BeginF = expandIfMacroId(F.getBeginLoc(), SM);
225226
SourceLocation BeginNameF = expandIfMacroId(F.getLocation(), SM);
226227

@@ -242,7 +243,6 @@ UseTrailingReturnTypeCheck::classifyTokensBeforeFunctionName(
242243
const MacroInfo *MI = PP->getMacroInfo(&Info);
243244
if (!MI || MI->isFunctionLike()) {
244245
// Cannot handle function style macros.
245-
diag(F.getLocation(), Message);
246246
return std::nullopt;
247247
}
248248
}
@@ -253,10 +253,8 @@ UseTrailingReturnTypeCheck::classifyTokensBeforeFunctionName(
253253

254254
if (std::optional<ClassifiedToken> CT = classifyToken(F, *PP, T))
255255
ClassifiedTokens.push_back(*CT);
256-
else {
257-
diag(F.getLocation(), Message);
256+
else
258257
return std::nullopt;
259-
}
260258
}
261259

262260
return ClassifiedTokens;
@@ -273,17 +271,17 @@ static bool hasAnyNestedLocalQualifiers(QualType Type) {
273271
return Result;
274272
}
275273

276-
SourceRange UseTrailingReturnTypeCheck::findReturnTypeAndCVSourceRange(
277-
const FunctionDecl &F, const TypeLoc &ReturnLoc, const ASTContext &Ctx,
278-
const SourceManager &SM, const LangOptions &LangOpts) {
274+
static SourceRange
275+
findReturnTypeAndCVSourceRange(const FunctionDecl &F, const TypeLoc &ReturnLoc,
276+
const ASTContext &Ctx, const SourceManager &SM,
277+
const LangOptions &LangOpts, Preprocessor *PP) {
279278

280279
// We start with the range of the return type and expand to neighboring
281280
// qualifiers (const, volatile and restrict).
282281
SourceRange ReturnTypeRange = F.getReturnTypeSourceRange();
283282
if (ReturnTypeRange.isInvalid()) {
284283
// Happens if e.g. clang cannot resolve all includes and the return type is
285284
// unknown.
286-
diag(F.getLocation(), Message);
287285
return {};
288286
}
289287

@@ -294,7 +292,7 @@ SourceRange UseTrailingReturnTypeCheck::findReturnTypeAndCVSourceRange(
294292

295293
// Include qualifiers to the left and right of the return type.
296294
std::optional<SmallVector<ClassifiedToken, 8>> MaybeTokens =
297-
classifyTokensBeforeFunctionName(F, Ctx, SM, LangOpts);
295+
classifyTokensBeforeFunctionName(F, Ctx, SM, LangOpts, PP);
298296
if (!MaybeTokens)
299297
return {};
300298
const SmallVector<ClassifiedToken, 8> &Tokens = *MaybeTokens;
@@ -331,10 +329,11 @@ SourceRange UseTrailingReturnTypeCheck::findReturnTypeAndCVSourceRange(
331329
return ReturnTypeRange;
332330
}
333331

334-
void UseTrailingReturnTypeCheck::keepSpecifiers(
335-
std::string &ReturnType, std::string &Auto, SourceRange ReturnTypeCVRange,
336-
const FunctionDecl &F, const FriendDecl *Fr, const ASTContext &Ctx,
337-
const SourceManager &SM, const LangOptions &LangOpts) {
332+
static void keepSpecifiers(std::string &ReturnType, std::string &Auto,
333+
SourceRange ReturnTypeCVRange, const FunctionDecl &F,
334+
const FriendDecl *Fr, const ASTContext &Ctx,
335+
const SourceManager &SM, const LangOptions &LangOpts,
336+
Preprocessor *PP) {
338337
// Check if there are specifiers inside the return type. E.g. unsigned
339338
// inline int.
340339
const auto *M = dyn_cast<CXXMethodDecl>(&F);
@@ -346,7 +345,7 @@ void UseTrailingReturnTypeCheck::keepSpecifiers(
346345
// Tokenize return type. If it contains macros which contain a mix of
347346
// qualifiers, specifiers and types, give up.
348347
std::optional<SmallVector<ClassifiedToken, 8>> MaybeTokens =
349-
classifyTokensBeforeFunctionName(F, Ctx, SM, LangOpts);
348+
classifyTokensBeforeFunctionName(F, Ctx, SM, LangOpts, PP);
350349
if (!MaybeTokens)
351350
return;
352351

@@ -383,6 +382,10 @@ void UseTrailingReturnTypeCheck::keepSpecifiers(
383382
}
384383
}
385384

385+
UseTrailingReturnTypeCheck::UseTrailingReturnTypeCheck(
386+
StringRef Name, ClangTidyContext *Context)
387+
: ClangTidyCheck(Name, Context) {}
388+
386389
void UseTrailingReturnTypeCheck::registerMatchers(MatchFinder *Finder) {
387390
auto F = functionDecl(
388391
unless(anyOf(hasTrailingReturn(), returns(voidType()),
@@ -423,7 +426,7 @@ void UseTrailingReturnTypeCheck::check(const MatchFinder::MatchResult &Result) {
423426
if (F->getDeclaredReturnType()->isFunctionPointerType() ||
424427
F->getDeclaredReturnType()->isMemberFunctionPointerType() ||
425428
F->getDeclaredReturnType()->isMemberPointerType()) {
426-
diag(F->getLocation(), Message);
429+
diag(F->getLocation(), MessageFunction);
427430
return;
428431
}
429432

@@ -440,24 +443,26 @@ void UseTrailingReturnTypeCheck::check(const MatchFinder::MatchResult &Result) {
440443
// FIXME: This may happen if we have __attribute__((...)) on the function.
441444
// We abort for now. Remove this when the function type location gets
442445
// available in clang.
443-
diag(F->getLocation(), Message);
446+
diag(F->getLocation(), MessageFunction);
444447
return;
445448
}
446449

447450
SourceLocation InsertionLoc =
448451
findTrailingReturnTypeSourceLocation(*F, FTL, Ctx, SM, LangOpts);
449452
if (InsertionLoc.isInvalid()) {
450-
diag(F->getLocation(), Message);
453+
diag(F->getLocation(), MessageFunction);
451454
return;
452455
}
453456

454457
// Using the declared return type via F->getDeclaredReturnType().getAsString()
455458
// discards user formatting and order of const, volatile, type, whitespace,
456459
// space before & ... .
457-
SourceRange ReturnTypeCVRange =
458-
findReturnTypeAndCVSourceRange(*F, FTL.getReturnLoc(), Ctx, SM, LangOpts);
459-
if (ReturnTypeCVRange.isInvalid())
460+
SourceRange ReturnTypeCVRange = findReturnTypeAndCVSourceRange(
461+
*F, FTL.getReturnLoc(), Ctx, SM, LangOpts, PP);
462+
if (ReturnTypeCVRange.isInvalid()) {
463+
diag(F->getLocation(), MessageFunction);
460464
return;
465+
}
461466

462467
// Check if unqualified names in the return type conflict with other entities
463468
// after the rewrite.
@@ -470,7 +475,7 @@ void UseTrailingReturnTypeCheck::check(const MatchFinder::MatchResult &Result) {
470475
UnqualNameVisitor UNV{*F};
471476
UNV.TraverseTypeLoc(FTL.getReturnLoc());
472477
if (UNV.Collision) {
473-
diag(F->getLocation(), Message);
478+
diag(F->getLocation(), MessageFunction);
474479
return;
475480
}
476481

@@ -486,10 +491,10 @@ void UseTrailingReturnTypeCheck::check(const MatchFinder::MatchResult &Result) {
486491
std::string Auto = NeedSpaceAfterAuto ? "auto " : "auto";
487492
std::string ReturnType =
488493
std::string(tooling::fixit::getText(ReturnTypeCVRange, Ctx));
489-
keepSpecifiers(ReturnType, Auto, ReturnTypeCVRange, *F, Fr, Ctx, SM,
490-
LangOpts);
494+
keepSpecifiers(ReturnType, Auto, ReturnTypeCVRange, *F, Fr, Ctx, SM, LangOpts,
495+
PP);
491496

492-
diag(F->getLocation(), Message)
497+
diag(F->getLocation(), MessageFunction)
493498
<< FixItHint::CreateReplacement(ReturnTypeCVRange, Auto)
494499
<< FixItHint::CreateInsertion(InsertionLoc, " -> " + ReturnType);
495500
}

clang-tools-extra/clang-tidy/modernize/UseTrailingReturnTypeCheck.h

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
#include "../ClangTidyCheck.h"
1313
#include "clang/Lex/Token.h"
14-
#include <optional>
1514

1615
namespace clang::tidy::modernize {
1716

@@ -27,8 +26,7 @@ struct ClassifiedToken {
2726
/// http://clang.llvm.org/extra/clang-tidy/checks/modernize/use-trailing-return-type.html
2827
class UseTrailingReturnTypeCheck : public ClangTidyCheck {
2928
public:
30-
UseTrailingReturnTypeCheck(StringRef Name, ClangTidyContext *Context)
31-
: ClangTidyCheck(Name, Context) {}
29+
UseTrailingReturnTypeCheck(StringRef Name, ClangTidyContext *Context);
3230
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
3331
return LangOpts.CPlusPlus11;
3432
}
@@ -39,23 +37,6 @@ class UseTrailingReturnTypeCheck : public ClangTidyCheck {
3937

4038
private:
4139
Preprocessor *PP = nullptr;
42-
43-
SourceLocation findTrailingReturnTypeSourceLocation(
44-
const FunctionDecl &F, const FunctionTypeLoc &FTL, const ASTContext &Ctx,
45-
const SourceManager &SM, const LangOptions &LangOpts);
46-
std::optional<SmallVector<ClassifiedToken, 8>>
47-
classifyTokensBeforeFunctionName(const FunctionDecl &F, const ASTContext &Ctx,
48-
const SourceManager &SM,
49-
const LangOptions &LangOpts);
50-
SourceRange findReturnTypeAndCVSourceRange(const FunctionDecl &F,
51-
const TypeLoc &ReturnLoc,
52-
const ASTContext &Ctx,
53-
const SourceManager &SM,
54-
const LangOptions &LangOpts);
55-
void keepSpecifiers(std::string &ReturnType, std::string &Auto,
56-
SourceRange ReturnTypeCVRange, const FunctionDecl &F,
57-
const FriendDecl *Fr, const ASTContext &Ctx,
58-
const SourceManager &SM, const LangOptions &LangOpts);
5940
};
6041

6142
} // namespace clang::tidy::modernize

clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type-cxx20.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %check_clang_tidy -std=c++20 %s modernize-use-trailing-return-type %t
1+
// RUN: %check_clang_tidy -std=c++20-or-later %s modernize-use-trailing-return-type %t
22

33
namespace std {
44
template <typename T, typename U>

0 commit comments

Comments
 (0)