Index: clang-tidy/modernize/CMakeLists.txt =================================================================== --- clang-tidy/modernize/CMakeLists.txt +++ clang-tidy/modernize/CMakeLists.txt @@ -30,6 +30,7 @@ UseNoexceptCheck.cpp UseNullptrCheck.cpp UseOverrideCheck.cpp + UseTrailingReturnCheck.cpp UseTransparentFunctorsCheck.cpp UseUncaughtExceptionsCheck.cpp UseUsingCheck.cpp Index: clang-tidy/modernize/ModernizeTidyModule.cpp =================================================================== --- clang-tidy/modernize/ModernizeTidyModule.cpp +++ clang-tidy/modernize/ModernizeTidyModule.cpp @@ -35,6 +35,7 @@ #include "UseNoexceptCheck.h" #include "UseNullptrCheck.h" #include "UseOverrideCheck.h" +#include "UseTrailingReturnCheck.h" #include "UseTransparentFunctorsCheck.h" #include "UseUncaughtExceptionsCheck.h" #include "UseUsingCheck.h" @@ -87,6 +88,8 @@ CheckFactories.registerCheck("modernize-use-noexcept"); CheckFactories.registerCheck("modernize-use-nullptr"); CheckFactories.registerCheck("modernize-use-override"); + CheckFactories.registerCheck( + "modernize-use-trailing-return"); CheckFactories.registerCheck( "modernize-use-transparent-functors"); CheckFactories.registerCheck( Index: clang-tidy/modernize/UseTrailingReturnCheck.h =================================================================== --- /dev/null +++ clang-tidy/modernize/UseTrailingReturnCheck.h @@ -0,0 +1,48 @@ +//===--- UseTrailingReturnCheck.h - clang-tidy-------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USETRAILINGRETURNCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USETRAILINGRETURNCHECK_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace modernize { + +/// Rewrites function signatures to use a trailing return type. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/modernize-use-trailing-return.html +class UseTrailingReturnCheck : public ClangTidyCheck { +public: + UseTrailingReturnCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + +private: + SourceLocation findTrailingReturnTypeSourceLocation( + const FunctionDecl &F, const FunctionTypeLoc &FTL, const ASTContext &Ctx, + const SourceManager &SM, const LangOptions &LangOpts); + llvm::Optional> + nonMacroTokensBeforeFunctionName(const FunctionDecl &F, const ASTContext &Ctx, + const SourceManager &SM, + const LangOptions &LangOpts); + SourceRange findReturnTypeAndCVSourceRange(const FunctionDecl &F, + const ASTContext &Ctx, + const SourceManager &SM, + const LangOptions &LangOpts); +}; + +} // namespace modernize +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USETRAILINGRETURNCHECK_H Index: clang-tidy/modernize/UseTrailingReturnCheck.cpp =================================================================== --- /dev/null +++ clang-tidy/modernize/UseTrailingReturnCheck.cpp @@ -0,0 +1,345 @@ +//===--- UseTrailingReturnCheck.cpp - clang-tidy---------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "UseTrailingReturnCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Tooling/FixIt.h" + +#include + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace modernize { +namespace { +struct UnqualNameVisitor : public RecursiveASTVisitor { +public: + UnqualNameVisitor(const FunctionDecl &F, const SourceManager &SM) + : F(F), SM(SM) {} + + bool Collision = false; + + bool shouldWalkTypesOfTypeLocs() const { return false; } + + bool VisitUnqualName(StringRef UnqualName) { + // Check for collisions with function arguments + for (ParmVarDecl *Param : F.parameters()) + if (const IdentifierInfo *Ident = Param->getIdentifier()) + if (Ident->getName() == UnqualName) { + Collision = true; + return true; + } + return false; + } + + bool TraverseTypeLoc(TypeLoc TL, bool Elaborated = false) { + if (TL.isNull()) + return true; + + if (!Elaborated) { + switch (TL.getTypeLocClass()) { + case TypeLoc::Record: + if (VisitUnqualName( + TL.getAs().getTypePtr()->getDecl()->getName())) + return false; + break; + case TypeLoc::Enum: + if (VisitUnqualName( + TL.getAs().getTypePtr()->getDecl()->getName())) + return false; + break; + case TypeLoc::TemplateSpecialization: + if (VisitUnqualName(TL.getAs() + .getTypePtr() + ->getTemplateName() + .getAsTemplateDecl() + ->getName())) + return false; + break; + default: + break; + } + } + + return RecursiveASTVisitor::TraverseTypeLoc(TL); + } + + // Replace the base method in order to call ower own + // TraverseTypeLoc(). + // TODO: Maybe RecursiveASTVisitor should call + // getDerived().TraverseTypeLoc(...). + bool TraverseQualifiedTypeLoc(QualifiedTypeLoc TL) { + return TraverseTypeLoc(TL.getUnqualifiedLoc()); + } + + // Replace the base version to inform TraverseTypeLoc that the type is + // elaborated. + bool TraverseElaboratedTypeLoc(ElaboratedTypeLoc TL) { + if (TL.getQualifierLoc()) + if (!TraverseNestedNameSpecifierLoc(TL.getQualifierLoc())) + return false; + if (!TraverseTypeLoc(TL.getNamedTypeLoc(), true)) + return false; + return true; + } + + bool VisitDeclRefExpr(DeclRefExpr *S) { + const DeclarationName Name = S->getNameInfo().getName(); + if (!S->getQualifierLoc() && Name.isIdentifier() && + VisitUnqualName(Name.getAsIdentifierInfo()->getName())) + return false; + return true; + } + +private: + const FunctionDecl &F; + const SourceManager &SM; +}; +} // namespace + +constexpr llvm::StringLiteral Message = + "use a trailing return type for this function"; + +static SourceLocation expandIfMacroId(SourceLocation Loc, + const SourceManager &SM) { + if (Loc.isMacroID()) + Loc = SM.getImmediateExpansionRange(Loc).getBegin(); + return Loc; +} + +SourceLocation UseTrailingReturnCheck::findTrailingReturnTypeSourceLocation( + const FunctionDecl &F, const FunctionTypeLoc &FTL, const ASTContext &Ctx, + const SourceManager &SM, const LangOptions &LangOpts) { + // We start with the location of the closing parenthesis. + SourceRange ExceptionSpecRange = F.getExceptionSpecSourceRange(); + if (ExceptionSpecRange.isValid()) + return Lexer::getLocForEndOfToken(ExceptionSpecRange.getEnd(), 0, SM, + LangOpts); + + // If the function argument list ends inside of a macro, it is dangerous to + // start lexing from here - bail out. + SourceLocation ClosingParen = FTL.getRParenLoc(); + if (ClosingParen.isMacroID()) + return {}; + + SourceLocation Result = + Lexer::getLocForEndOfToken(ClosingParen, 0, SM, LangOpts); + + // Skip subsequent CV and ref qualifiers. + std::pair Loc = SM.getDecomposedLoc(Result); + StringRef File = SM.getBufferData(Loc.first); + const char *TokenBegin = File.data() + Loc.second; + Lexer Lexer(SM.getLocForStartOfFile(Loc.first), LangOpts, File.begin(), + TokenBegin, File.end()); + Token T; + while (!Lexer.LexFromRawLexer(T)) { + if (T.is(tok::raw_identifier)) { + IdentifierInfo &Info = Ctx.Idents.get( + StringRef(SM.getCharacterData(T.getLocation()), T.getLength())); + T.setIdentifierInfo(&Info); + T.setKind(Info.getTokenID()); + } + + if (T.isOneOf(tok::amp, tok::ampamp, tok::kw_const, tok::kw_volatile)) { + Result = T.getEndLoc(); + continue; + } + + break; + } + + return Result; +} + +llvm::Optional> +UseTrailingReturnCheck::nonMacroTokensBeforeFunctionName( + const FunctionDecl &F, const ASTContext &Ctx, const SourceManager &SM, + const LangOptions &LangOpts) { + SourceLocation BeginF = expandIfMacroId(F.getBeginLoc(), SM); + SourceLocation BeginNameF = expandIfMacroId(F.getLocation(), SM); + + // Create tokens for everything before the name of the function. + std::pair Loc = SM.getDecomposedLoc(BeginF); + StringRef File = SM.getBufferData(Loc.first); + const char *TokenBegin = File.data() + Loc.second; + Lexer Lexer(SM.getLocForStartOfFile(Loc.first), LangOpts, File.begin(), + TokenBegin, File.end()); + Token T; + SmallVector Tokens; + while (!Lexer.LexFromRawLexer(T) && + SM.isBeforeInTranslationUnit(T.getLocation(), BeginNameF)) { + if (T.is(tok::raw_identifier)) { + IdentifierInfo &Info = Ctx.Idents.get( + StringRef(SM.getCharacterData(T.getLocation()), T.getLength())); + if (Info.hasMacroDefinition()) { + // The CV qualifiers of the return type are inside macros + diag(F.getLocation(), Message); + return {}; + } + T.setIdentifierInfo(&Info); + T.setKind(Info.getTokenID()); + } + Tokens.push_back(T); + } + return Tokens; +} + +static bool hasAnyNestedLocalQualifiers(QualType Type) { + bool Result = Type.hasLocalQualifiers(); + if (Type->isPointerType()) + Result = Result || hasAnyNestedLocalQualifiers( + Type->castAs()->getPointeeType()); + if (Type->isReferenceType()) + Result = Result || hasAnyNestedLocalQualifiers( + Type->castAs()->getPointeeType()); + return Result; +} + +SourceRange UseTrailingReturnCheck::findReturnTypeAndCVSourceRange( + const FunctionDecl &F, const ASTContext &Ctx, const SourceManager &SM, + const LangOptions &LangOpts) { + + // We start with the range of the return type and expand to neighboring + // 'const' and 'volatile'. + SourceRange ReturnTypeRange = F.getReturnTypeSourceRange(); + if (ReturnTypeRange.isInvalid()) { + // Happens if e.g. clang cannot resolve all includes and the return type is + // unknown. + diag(F.getLocation(), Message); + return {}; + } + + // If the return type has no local qualifiers, it's source range is accurate. + if (!hasAnyNestedLocalQualifiers(F.getReturnType())) + return ReturnTypeRange; + + // Include const and volatile to the left and right of the return type. + llvm::Optional> MaybeTokens = + nonMacroTokensBeforeFunctionName(F, Ctx, SM, LangOpts); + if (!MaybeTokens) + return {}; + const SmallVector &Tokens = *MaybeTokens; + + auto IsCV = [](Token T) { + return T.isOneOf(tok::kw_const, tok::kw_volatile); + }; + + bool ExtendedLeft = false; + for (size_t I = 0; I < Tokens.size(); I++) { + // If we found the beginning of the return type, include const and volatile + // to the left. + if (!SM.isBeforeInTranslationUnit(Tokens[I].getLocation(), + ReturnTypeRange.getBegin()) && + !ExtendedLeft) { + for (int J = static_cast(I) - 1; J >= 0 && IsCV(Tokens[J]); J--) + ReturnTypeRange.setBegin(Tokens[J].getLocation()); + ExtendedLeft = true; + } + // If we found the end of the return type, include const and volatile to the + // right. + if (SM.isBeforeInTranslationUnit(ReturnTypeRange.getEnd(), + Tokens[I].getLocation())) { + for (size_t J = I; J < Tokens.size() && IsCV(Tokens[J]); J++) + ReturnTypeRange.setEnd(Tokens[J].getLocation()); + break; + } + } + + return ReturnTypeRange; +} + +void UseTrailingReturnCheck::registerMatchers(MatchFinder *Finder) { + if (!getLangOpts().CPlusPlus11) + return; + + Finder->addMatcher( + functionDecl(unless(anyOf(hasTrailingReturn(), returns(voidType()), + returns(autoType()), cxxConversionDecl(), + cxxMethodDecl(isImplicit())))) + .bind("f"), + this); +} + +void UseTrailingReturnCheck::check(const MatchFinder::MatchResult &Result) { + const auto *F = Result.Nodes.getNodeAs("f"); + assert(F && "Matcher is expected to find only FunctionDecls"); + + if (F->getLocation().isInvalid()) + return; + + // TODO: implement those + if (F->getDeclaredReturnType()->isFunctionPointerType() || + F->getDeclaredReturnType()->isMemberFunctionPointerType() || + F->getDeclaredReturnType()->isMemberPointerType() || + F->getDeclaredReturnType()->getAs() != nullptr) { + diag(F->getLocation(), Message); + return; + } + + const ASTContext &Ctx = *Result.Context; + const SourceManager &SM = *Result.SourceManager; + const LangOptions &LangOpts = getLangOpts(); + + const TypeSourceInfo *TSI = F->getTypeSourceInfo(); + if (!TSI) { + diag(F->getLocation(), Message); + return; + } + FunctionTypeLoc FTL = + TSI->getTypeLoc().IgnoreParens().getAs(); + if (!FTL) { + diag(F->getLocation(), Message); + return; + } + + SourceLocation InsertionLoc = + findTrailingReturnTypeSourceLocation(*F, FTL, Ctx, SM, LangOpts); + if (InsertionLoc.isInvalid()) { + diag(F->getLocation(), Message); + return; + } + + // Using the declared return type via F->getDeclaredReturnType().getAsString() + // discards user formatting and order of const, volatile, type, whitespace, + // space before & ... . + SourceRange ReturnTypeCVRange = + findReturnTypeAndCVSourceRange(*F, Ctx, SM, LangOpts); + if (ReturnTypeCVRange.isInvalid()) + return; + + // Check if unqualified names in the return type conflict with other entities + // after the rewrite. + // FIXME: this could be done better, by performing a lookup of all + // unqualified names in the return type in the scope of the function. If the + // lookup finds a different entity than the original entity identified by the + // name, then we can either not perform a rewrite or explicitely qualify the + // entity. Such entities could be function parameter names, (inherited) class + // members, template parameters, etc. + UnqualNameVisitor UNV{*F, SM}; + UNV.TraverseTypeLoc(FTL.getReturnLoc()); + if (UNV.Collision) { + diag(F->getLocation(), Message); + return; + } + + StringRef ReturnType = tooling::fixit::getText(ReturnTypeCVRange, Ctx); + StringRef Auto = std::isspace(*ReturnType.end()) // FIXME (dereferencing end) + ? "auto" + : "auto "; + diag(F->getLocation(), Message) + << FixItHint::CreateReplacement(ReturnTypeCVRange, Auto) + << FixItHint::CreateInsertion(InsertionLoc, (" -> " + ReturnType).str()); +} + +} // namespace modernize +} // namespace tidy +} // namespace clang Index: docs/ReleaseNotes.rst =================================================================== --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -102,6 +102,11 @@ :doc:`objc-property-declaration ` check have been removed. +- New :doc:`modernize-use-trailing-return + ` check. + + Rewrites function signatures to use a trailing return type. + Improvements to include-fixer ----------------------------- Index: docs/clang-tidy/checks/list.rst =================================================================== --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -215,6 +215,7 @@ modernize-use-noexcept modernize-use-nullptr modernize-use-override + modernize-use-trailing-return modernize-use-transparent-functors modernize-use-uncaught-exceptions modernize-use-using Index: docs/clang-tidy/checks/modernize-use-trailing-return.rst =================================================================== --- /dev/null +++ docs/clang-tidy/checks/modernize-use-trailing-return.rst @@ -0,0 +1,68 @@ +.. title:: clang-tidy - modernize-use-trailing-return + +modernize-use-trailing-return +============================= + +Rewrites function signatures to use a trailing return type +(introduced in C++11). This transformation is purely stylistic. +The return type before the function name is replaced by ``auto`` +and inserted after the function parameter list (and qualifiers). + +Example +------- + +.. code-block:: c++ + + int f1(); + inline int f2(int arg) noexcept; + virtual float f3() const && = delete; + +transforms to: + +.. code-block:: c++ + + auto f1() -> int; + inline auto f2(int arg) -> int noexcept; + virtual auto f3() const && -> float = delete; + +Known Limitations +----------------- + +The following categories of return types cannot be rewritten currently: +* function pointers +* member function pointers +* member pointers +* decltype, when it is the top level expression + +Unqualified names in the return type might erroneously refer to different entities after the rewrite. +Preventing such errors requires a full lookup of all unqualified names present in the return type in the scope of the trailing return type location. +This location includes e.g. function parameter names and members of the enclosing class (including all inherited classes). +Such a lookup is currently not implemented. + +Given the following piece of code + +.. code-block:: c++ + + struct Object { long long value; }; + Object f(unsigned Object) { return {Object * 2}; } + class CC { + int Object; + struct Object m(); + }; + Object CC::m() { return {0}; } + +a careless rewrite would produce the following output: + +.. code-block:: c++ + + struct Object { long long value; }; + auto f(unsigned Object) -> Object { return {Object * 2}; } // error + class CC { + int Object; + auto m() -> struct Object; + }; + auto CC::m() -> Object { return {0}; } // error + +This code fails to compile because the Object in the context of f refers to the equally named function parameter. +Similarly, the Object in the context of m refers to the equally named class member. +The check can currently only detect a clash with a function parameter name. Index: test/clang-tidy/modernize-use-trailing-return.cpp =================================================================== --- /dev/null +++ test/clang-tidy/modernize-use-trailing-return.cpp @@ -0,0 +1,433 @@ +// RUN: %check_clang_tidy %s modernize-use-trailing-return %t -- -- --std=c++14 -fdeclspec + +namespace std { + template + class vector; + + template + class array; + + class string; + + template + auto declval() -> T; +} + +// +// Functions +// + +int f(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto f() -> int;{{$}} +int ((f))(); +// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto ((f))() -> int;{{$}} +int f(int); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto f(int) -> int;{{$}} +int f(int arg); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto f(int arg) -> int;{{$}} +int f(int arg1, int arg2, int arg3); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto f(int arg1, int arg2, int arg3) -> int;{{$}} +int f(int arg1, int arg2, int arg3, ...); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto f(int arg1, int arg2, int arg3, ...) -> int;{{$}} +template int f(T t); +// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}template auto f(T t) -> int;{{$}} + +// +// Functions with formatting +// + +int a1() { return 42; } +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto a1() -> int { return 42; }{{$}} +int a2() { + return 42; +} +// CHECK-MESSAGES: :[[@LINE-3]]:5: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto a2() -> int {{{$}} +int a3() +{ + return 42; +} +// CHECK-MESSAGES: :[[@LINE-4]]:5: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto a3() -> int{{$}} +int a4(int arg ) ; +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto a4(int arg ) -> int ;{{$}} +int a5 +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto a5{{$}} +(int arg); +// CHECK-FIXES: {{^}}(int arg) -> int;{{$}} +const +int +* +a7 +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use a trailing return type for this function [modernize-use-trailing-return] +() +// CHECK-FIXES: {{^}}() -> const{{$}} +// CHECK-FIXES: {{^}}int{{$}} +// CHECK-FIXES: {{^}}*{{$}} +; + +int*a7(int arg); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto a7(int arg) -> int*;{{$}} + + +// +// Functions with qualifiers and specifiers +// + +inline int d1(int arg); +// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}inline auto d1(int arg) -> int;{{$}} +extern "C" int d2(int arg); +// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}extern "C" auto d2(int arg) -> int;{{$}} +inline int d3(int arg) noexcept(true); +// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}inline auto d3(int arg) noexcept(true) -> int;{{$}} +inline int d4(int arg) try { } catch(...) { } +// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}inline auto d4(int arg) -> int try { } catch(...) { }{{$}} +int d5(int arg) throw(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto d5(int arg) throw() -> int;{{$}} + +// +// Functions in namespaces +// + +namespace N { + int e1(); +} +// CHECK-MESSAGES: :[[@LINE-2]]:9: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}} auto e1() -> int;{{$}} +int N::e1() {} +// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto N::e1() -> int {}{{$}} + +// +// Functions with unsupported return types +// +int (*e3())(double); +// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}int (*e3())(double);{{$}} +struct A; +int A::* e5(); +// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}int A::* e5();{{$}} +int std::vector::* e6(); +// CHECK-MESSAGES: :[[@LINE-1]]:33: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}int std::vector::* e6();{{$}} +int (std::vector::*e7())(double); +// CHECK-MESSAGES: :[[@LINE-1]]:33: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}int (std::vector::*e7())(double);{{$}} + +// +// Functions with complex return types +// + +inline volatile const std::vector e2(); +// CHECK-MESSAGES: :[[@LINE-1]]:48: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}inline auto e2() -> volatile const std::vector;{{$}} +inline const std::vector volatile e2(); +// CHECK-MESSAGES: :[[@LINE-1]]:48: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}inline auto e2() -> const std::vector volatile;{{$}} +inline std::vector const volatile e2(); +// CHECK-MESSAGES: :[[@LINE-1]]:48: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}inline auto e2() -> std::vector const volatile;{{$}} +int* e8(); +// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto e8() -> int*;{{$}} +static const char* e9(void* user_data); +// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}static auto e9(void* user_data) -> const char*;{{$}} +static const char* const e10(void* user_data); +// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}static auto e10(void* user_data) -> const char* const;{{$}} +static const char** volatile * const & e11(void* user_data); +// CHECK-MESSAGES: :[[@LINE-1]]:40: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}static auto e11(void* user_data) -> const char** volatile * const &;{{$}} +static const char* const * const * const e12(void* user_data); +// CHECK-MESSAGES: :[[@LINE-1]]:42: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}static auto e12(void* user_data) -> const char* const * const * const;{{$}} +struct A e13(); +// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto e13() -> struct A;{{$}} + +// +// decltype (unsupported if top level expression) +// + +decltype(1 + 2) dec1() { return 1 + 2; } +// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use a trailing return type for this function [modernize-use-trailing-return] +// TODO: source range of DecltypeTypeLoc not yet implemented +// _HECK-FIXES: {{^}}auto dec1() -> decltype(1 + 2) { return 1 + 2; }{{$}} +template +decltype(std::declval(std::declval)) dec2(F f, T t) { return f(t); } +// CHECK-MESSAGES: :[[@LINE-1]]:44: warning: use a trailing return type for this function [modernize-use-trailing-return] +// TODO: source range of DecltypeTypeLoc not yet implemented +// _HECK-FIXES: {{^}}auto dec2(F f, T t) -> decltype(std::declval(std::declval)) { return f(t); }{{$}} +template +typename decltype(std::declval())::value_type dec3(); +// CHECK-MESSAGES: :[[@LINE-1]]:50: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto dec3() -> typename decltype(std::declval())::value_type;{{$}} +template +decltype(std::declval())* dec4(); +// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto dec4() -> decltype(std::declval())*;{{$}} + +// +// Methods +// + +struct B { + B& operator=(const B&); +// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}} auto operator=(const B&) -> B&;{{$}} + + double base1(int, bool b); +// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}} auto base1(int, bool b) -> double;{{$}} + + virtual double base2(int, bool b) {} +// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}} virtual auto base2(int, bool b) -> double {}{{$}} + + virtual float base3() const = 0; +// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}} virtual auto base3() const -> float = 0;{{$}} + + virtual float base4() volatile = 0; +// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}} virtual auto base4() volatile -> float = 0;{{$}} + + double base5(int, bool b) &&; +// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}} auto base5(int, bool b) && -> double;{{$}} + + double base6(int, bool b) const &&; +// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}} auto base6(int, bool b) const && -> double;{{$}} + + double base7(int, bool b) const & = delete; +// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}} auto base7(int, bool b) const & -> double = delete;{{$}} + + double base8(int, bool b) const volatile & = delete; +// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}} auto base8(int, bool b) const volatile & -> double = delete;{{$}} + + virtual const char * base9() const noexcept { return ""; } +// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}} virtual auto base9() const noexcept -> const char * { return ""; }{{$}} +}; + +double B::base1(int, bool b) {} +// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto B::base1(int, bool b) -> double {}{{$}} + +struct D : B { + virtual double f1(int, bool b) final; +// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}} virtual auto f1(int, bool b) -> double final;{{$}} + + virtual double base2(int, bool b) override; +// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}} virtual auto base2(int, bool b) -> double override;{{$}} + + virtual float base3() const override final { } +// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}} virtual auto base3() const -> float override final { }{{$}} + + const char * base9() const noexcept override { return ""; } +// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}} auto base9() const noexcept -> const char * override { return ""; }{{$}} +}; + +// +// Functions with attributes +// + +int g1() [[asdf]]; +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto g1() -> int {{[[][[]}}asdf{{[]][]]}};{{$}} +[[noreturn]] int g2(); +// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}{{[[][[]}}noreturn{{[]][]]}} auto g2() -> int;{{$}} +int g2 [[noreturn]] (); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto g2 {{[[][[]}}noreturn{{[]][]]}} () -> int;{{$}} + +// +// Templates +// +template +[[maybe_unused]] typename Container::value_type const volatile&& t1(Container& C) noexcept; +// CHECK-MESSAGES: :[[@LINE-1]]:66: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}{{[[][[]}}maybe_unused{{[]][]]}} auto t1(Container& C) noexcept -> typename Container::value_type const volatile&&;{{$}} + +// +// Macros +// + +#define DWORD unsigned int +DWORD h1(); +// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto h1() -> DWORD;{{$}} +#define INT int +#define UNSIGNED unsigned +UNSIGNED INT h2(); +// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto h2() -> UNSIGNED INT;{{$}} +#define CONST const +CONST int h3(); +// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use a trailing return type for this function [modernize-use-trailing-return] +#define ALWAYS_INLINE inline +#define DLL_EXPORT __declspec(dllexport) +ALWAYS_INLINE DLL_EXPORT int h4(); +// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}ALWAYS_INLINE DLL_EXPORT auto h4() -> int;{{$}} +#define ANOTHER_ATTRIBUTE __attribute__((deprecated)) +int h5() ANOTHER_ATTRIBUTE; +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto h5() -> int ANOTHER_ATTRIBUTE;{{$}} +#define FUNCTION_NAME(a, b) a##b +int FUNCTION_NAME(foo, bar)(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto FUNCTION_NAME(foo, bar)() -> int;{{$}} +#define DEFINE_FUNCTION_1(a, b) int a##b() +DEFINE_FUNCTION_1(foo, bar); +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use a trailing return type for this function [modernize-use-trailing-return] +#define DEFINE_FUNCTION_2 int foo(int arg); +DEFINE_FUNCTION_2 +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use a trailing return type for this function [modernize-use-trailing-return] +#define DLL_EXPORT_CONST __declspec(dllexport) const +DLL_EXPORT_CONST int h6(); +// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: use a trailing return type for this function [modernize-use-trailing-return] + +template +using Real = T; +#define PRECISION float +Real h7() { return 0.; } +// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto h7() -> Real { return 0.; }{{$}} + +#define MAYBE_UNUSED_MACRO [[maybe_unused]] +template +MAYBE_UNUSED_MACRO typename Container::value_type const volatile** const h8(Container& C) noexcept; +// CHECK-MESSAGES: :[[@LINE-1]]:74: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}MAYBE_UNUSED_MACRO auto h8(Container& C) noexcept -> typename Container::value_type const volatile** const;{{$}} + +#define NOEXCEPT noexcept +int h9(int arg) NOEXCEPT; +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto h9(int arg) NOEXCEPT -> int;{{$}} + +// +// Name collisions +// +struct Object { long long value; }; + +Object j1(unsigned Object) { return {Object * 2}; } +// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}Object j1(unsigned Object) { return {Object * 2}; }{{$}} +::Object j1(unsigned Object); +// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto j1(unsigned Object) -> ::Object;{{$}} +const Object& j2(unsigned a, int b, char Object, long l); +// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}const Object& j2(unsigned a, int b, char Object, long l);{{$}} +const struct Object& j2(unsigned a, int b, char Object, long l); +// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto j2(unsigned a, int b, char Object, long l) -> const struct Object&;{{$}} +std::vector j3(unsigned Object); +// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}std::vector j3(unsigned Object);{{$}} +std::vector j7(unsigned Object); +// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}std::vector j7(unsigned Object);{{$}} +std::vector j4(unsigned vector); +// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto j4(unsigned vector) -> std::vector;{{$}} +std::vector<::Object> j4(unsigned vector); +// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto j4(unsigned vector) -> std::vector<::Object>;{{$}} +std::vector j4(unsigned vector); +// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto j4(unsigned vector) -> std::vector;{{$}} +std::vector j4(unsigned Vector); +// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto j4(unsigned Vector) -> std::vector;{{$}} +using std::vector; +vector j5(unsigned vector); +// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}vector j5(unsigned vector);{{$}} +constexpr auto Size = 5; +std::array j6(unsigned Size); +// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}std::array j6(unsigned Size);{{$}} +std::array j8(unsigned Size); +// CHECK-MESSAGES: :[[@LINE-1]]:44: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}std::array j8(unsigned Size);{{$}} + +class CC { + int Object; + struct Object m(); +// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}} auto m() -> struct Object;{{$}} +}; +Object CC::m() { return {0}; } +// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto CC::m() -> Object { return {0}; }{{$}} +class DD : public CC { + ::Object g(); +// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}} auto g() -> ::Object;{{$}} +}; +Object DD::g() { +// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use a trailing return type for this function [modernize-use-trailing-return] +// CHECK-FIXES: {{^}}auto DD::g() -> Object {{{$}} + return {0}; +} + + +// +// Samples which do not trigger the check +// + +auto f() -> int; +auto f(int) -> int; +auto f(int arg) -> int; +auto f(int arg1, int arg2, int arg3) -> int; +auto f(int arg1, int arg2, int arg3, ...) -> int; +template auto f(T t) -> int; + +auto ff(); +decltype(auto) fff(); + +void c(); +void c(int arg); +void c(int arg) { return; } + +struct D2 : B { + D2(); + virtual ~D2(); + + virtual auto f1(int, bool b) -> double final; + virtual auto base2(int, bool b) -> double override; + virtual auto base3() const -> float override final { } + + operator double(); +}; + +auto l1 = [](int arg) {}; +auto l2 = [](int arg) -> double {};