@@ -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
119119static 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+
386389void 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}
0 commit comments