Index: clang/include/clang/AST/Decl.h =================================================================== --- clang/include/clang/AST/Decl.h +++ clang/include/clang/AST/Decl.h @@ -1921,6 +1921,20 @@ void setRangeEnd(SourceLocation E) { EndRangeLoc = E; } + /// Returns the source range covered by the ellipsis of a variadic function. + /// + /// If the ellipsis is expanded from a macro, it returns the location of the + /// macro. Otherwise, it returns the location of the end of the ellipsis. + SourceRange getEllipsisSourceRange() const { + const auto *FPT = getType()->getAs(); + if (!FPT || !FPT->isVariadic()) + return SourceRange(); + + SourceLocation Begin = FPT->getEllipsisLoc(); + SourceLocation End = Begin.isMacroID() ? Begin : Begin.getLocWithOffset(2); + return SourceRange(Begin, End); + } + SourceRange getSourceRange() const override LLVM_READONLY; // Function definitions. @@ -2335,6 +2349,11 @@ /// limited representation in the AST. SourceRange getReturnTypeSourceRange() const; + /// Attempt to compute an informative source range covering the + /// function parameters. The source range is invalid if there are no + /// parameters. + SourceRange getParametersSourceRange() const; + /// Get the declared return type, which may differ from the actual return /// type if the return type is deduced. QualType getDeclaredReturnType() const { Index: clang/include/clang/AST/Type.h =================================================================== --- clang/include/clang/AST/Type.h +++ clang/include/clang/AST/Type.h @@ -3720,9 +3720,9 @@ : public FunctionType, public llvm::FoldingSetNode, private llvm::TrailingObjects< - FunctionProtoType, QualType, FunctionType::FunctionTypeExtraBitfields, - FunctionType::ExceptionType, Expr *, FunctionDecl *, - FunctionType::ExtParameterInfo, Qualifiers> { + FunctionProtoType, QualType, SourceLocation, + FunctionType::FunctionTypeExtraBitfields, FunctionType::ExceptionType, + Expr *, FunctionDecl *, FunctionType::ExtParameterInfo, Qualifiers> { friend class ASTContext; // ASTContext creates these. friend TrailingObjects; @@ -3733,6 +3733,9 @@ // Always present. Note that for the vast majority of FunctionProtoType, // these will be the only trailing objects. // + // * Optionally if the function is variadic, the SourceLocation of the + // ellipsis. + // // * Optionally if some extra data is stored in FunctionTypeExtraBitfields // (see FunctionTypeExtraBitfields and FunctionTypeBitfields): // a single FunctionTypeExtraBitfields. Present if and only if @@ -3804,6 +3807,7 @@ RefQualifierKind RefQualifier = RQ_None; ExceptionSpecInfo ExceptionSpec; const ExtParameterInfo *ExtParameterInfos = nullptr; + SourceLocation EllipsisLoc; ExtProtoInfo() : Variadic(false), HasTrailingReturn(false) {} @@ -3822,6 +3826,10 @@ return getNumParams(); } + unsigned numTrailingObjects(OverloadToken) const { + return isVariadic(); + } + unsigned numTrailingObjects(OverloadToken) const { return hasExtraBitfields(); } @@ -3933,6 +3941,7 @@ ExtProtoInfo EPI; EPI.ExtInfo = getExtInfo(); EPI.Variadic = isVariadic(); + EPI.EllipsisLoc = getEllipsisLoc(); EPI.HasTrailingReturn = hasTrailingReturn(); EPI.ExceptionSpec.Type = getExceptionSpecType(); EPI.TypeQuals = getMethodQuals(); @@ -4034,6 +4043,10 @@ /// Whether this function prototype is variadic. bool isVariadic() const { return FunctionTypeBits.Variadic; } + SourceLocation getEllipsisLoc() const { + return *getTrailingObjects(); + } + /// Determines whether this function prototype contains a /// parameter pack at the end. /// Index: clang/lib/AST/ASTContext.cpp =================================================================== --- clang/lib/AST/ASTContext.cpp +++ clang/lib/AST/ASTContext.cpp @@ -3787,10 +3787,11 @@ auto ESH = FunctionProtoType::getExceptionSpecSize( EPI.ExceptionSpec.Type, EPI.ExceptionSpec.Exceptions.size()); size_t Size = FunctionProtoType::totalSizeToAlloc< - QualType, FunctionType::FunctionTypeExtraBitfields, + QualType, SourceLocation, FunctionType::FunctionTypeExtraBitfields, FunctionType::ExceptionType, Expr *, FunctionDecl *, FunctionProtoType::ExtParameterInfo, Qualifiers>( - NumArgs, FunctionProtoType::hasExtraBitfields(EPI.ExceptionSpec.Type), + NumArgs, EPI.Variadic, + FunctionProtoType::hasExtraBitfields(EPI.ExceptionSpec.Type), ESH.NumExceptionType, ESH.NumExprPtr, ESH.NumFunctionDeclPtr, EPI.ExtParameterInfos ? NumArgs : 0, EPI.TypeQuals.hasNonFastQualifiers() ? 1 : 0); Index: clang/lib/AST/Decl.cpp =================================================================== --- clang/lib/AST/Decl.cpp +++ clang/lib/AST/Decl.cpp @@ -3302,6 +3302,22 @@ return RTRange; } +SourceRange FunctionDecl::getParametersSourceRange() const { + unsigned NP = getNumParams(); + auto EllipsisRange = getEllipsisSourceRange(); + + if (NP == 0 && !EllipsisRange.isValid()) + return SourceRange(); + + auto Begin = NP > 0 ? ParamInfo[0]->getSourceRange().getBegin() + : EllipsisRange.getBegin(); + auto End = EllipsisRange.isValid() + ? EllipsisRange.getEnd() + : ParamInfo[NP - 1]->getSourceRange().getEnd(); + + return SourceRange(Begin, End); +} + SourceRange FunctionDecl::getExceptionSpecSourceRange() const { const TypeSourceInfo *TSI = getTypeSourceInfo(); if (!TSI) Index: clang/lib/AST/Type.cpp =================================================================== --- clang/lib/AST/Type.cpp +++ clang/lib/AST/Type.cpp @@ -3000,6 +3000,12 @@ } else { FunctionTypeBits.HasExtQuals = 0; } + + // Fill in the Ellipsis location info if present. + if (epi.Variadic) { + auto &EllipsisLoc = *getTrailingObjects(); + EllipsisLoc = epi.EllipsisLoc; + } } bool FunctionProtoType::hasDependentExceptionSpec() const { Index: clang/lib/Sema/SemaType.cpp =================================================================== --- clang/lib/Sema/SemaType.cpp +++ clang/lib/Sema/SemaType.cpp @@ -4760,6 +4760,7 @@ FunctionProtoType::ExtProtoInfo EPI; EPI.ExtInfo = EI; EPI.Variadic = FTI.isVariadic; + EPI.EllipsisLoc = FTI.getEllipsisLoc(); EPI.HasTrailingReturn = FTI.hasTrailingReturnType(); EPI.TypeQuals.addCVRUQualifiers( FTI.MethodQualifiers ? FTI.MethodQualifiers->getTypeQualifiers() Index: clang/unittests/AST/SourceLocationTest.cpp =================================================================== --- clang/unittests/AST/SourceLocationTest.cpp +++ clang/unittests/AST/SourceLocationTest.cpp @@ -648,6 +648,112 @@ Language::Lang_CXX11)); } +class FunctionDeclParametersRangeVerifier : public RangeVerifier { +protected: + SourceRange getRange(const FunctionDecl &Function) override { + return Function.getParametersSourceRange(); + } +}; + +TEST(FunctionDeclParameters, FunctionDeclOnlyVariadic) { + FunctionDeclParametersRangeVerifier Verifier; + Verifier.expectRange(1, 8, 1, 10); + EXPECT_TRUE(Verifier.match("void f(...);\n", functionDecl())); +} + +TEST(FunctionDeclParameters, FunctionDeclVariadic) { + FunctionDeclParametersRangeVerifier Verifier; + Verifier.expectRange(1, 8, 1, 17); + EXPECT_TRUE(Verifier.match("void f(int a, ...);\n", functionDecl())); +} + +TEST(FunctionDeclParameters, FunctionDeclMacroVariadic) { + FunctionDeclParametersRangeVerifier Verifier; + Verifier.expectRange(2, 8, 1, 18); + EXPECT_TRUE(Verifier.match("#define VARIADIC ...\n" + "void f(int a, VARIADIC);\n", + functionDecl())); +} + +TEST(FunctionDeclParameters, FunctionDeclMacroParams) { + FunctionDeclParametersRangeVerifier Verifier; + Verifier.expectRange(1, 16, 2, 20); + EXPECT_TRUE(Verifier.match("#define PARAMS int a, int b\n" + "void f(PARAMS, int c);", + functionDecl())); +} + +TEST(FunctionDeclParameters, FunctionDeclSingleParameter) { + FunctionDeclParametersRangeVerifier Verifier; + Verifier.expectRange(1, 8, 1, 12); + EXPECT_TRUE(Verifier.match("void f(int a);\n", functionDecl())); +} + +TEST(FunctionDeclParameters, MemberFunctionDecl) { + FunctionDeclParametersRangeVerifier Verifier; + Verifier.expectRange(2, 8, 2, 12); + EXPECT_TRUE(Verifier.match("class A{\n" + "void f(int a);\n" + "};", + functionDecl())); +} + +TEST(FunctionDeclParameters, MemberFunctionDeclVariadic) { + FunctionDeclParametersRangeVerifier Verifier; + Verifier.expectRange(2, 8, 2, 17); + EXPECT_TRUE(Verifier.match("class A{\n" + "void f(int a, ...);\n" + "};", + functionDecl())); +} + +TEST(FunctionDeclParameters, StaticFunctionDecl) { + FunctionDeclParametersRangeVerifier Verifier; + Verifier.expectRange(2, 15, 2, 19); + EXPECT_TRUE(Verifier.match("class A{\n" + "static void f(int a);\n" + "};", + functionDecl())); +} + +TEST(FunctionDeclParameters, FunctionDeclMultipleParameters) { + FunctionDeclParametersRangeVerifier Verifier; + Verifier.expectRange(1, 8, 1, 28); + EXPECT_TRUE( + Verifier.match("void f(int a, int b, char *c);\n", functionDecl())); +} + +TEST(FunctionDeclParameters, FunctionDeclWithDefaultValue) { + FunctionDeclParametersRangeVerifier Verifier; + Verifier.expectRange(1, 8, 1, 16); + EXPECT_TRUE(Verifier.match("void f(int a = 5);\n", functionDecl())); +} + +TEST(FunctionDeclParameters, FunctionDeclWithVolatile) { + FunctionDeclParametersRangeVerifier Verifier; + Verifier.expectRange(1, 8, 1, 22); + EXPECT_TRUE(Verifier.match("void f(volatile int *i);", functionDecl())); +} + +TEST(FunctionDeclParameters, FunctionDeclWithConstParam) { + FunctionDeclParametersRangeVerifier Verifier; + Verifier.expectRange(1, 8, 1, 19); + EXPECT_TRUE(Verifier.match("void f(const int *i);", functionDecl())); +} + +TEST(FunctionDeclParameters, FunctionDeclWithConstVolatileParam) { + FunctionDeclParametersRangeVerifier Verifier; + Verifier.expectRange(1, 8, 1, 28); + EXPECT_TRUE(Verifier.match("void f(const volatile int *i);", functionDecl())); +} + +TEST(FunctionDeclParameters, FunctionDeclWithParamAttribute) { + FunctionDeclParametersRangeVerifier Verifier; + Verifier.expectRange(1, 8, 1, 36); + EXPECT_TRUE(Verifier.match("void f(__attribute__((unused)) int a) {}", + functionDecl())); +} + TEST(CXXMethodDecl, CXXMethodDeclWithThrowSpecification) { RangeVerifier Verifier; Verifier.expectRange(2, 1, 2, 16);