From 269798ba2cc9eda11d342d8ef68330cfd018e0db Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Tue, 13 Jan 2015 07:42:33 +0000 Subject: [PATCH] Parse: Don't crash when default argument in typedef consists of sole '=' We'd crash trying to make the SourceRange for the tokens we'd like to highlight. Don't assume there is more than one token makes up the default argument. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@225774 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaDeclCXX.cpp | 9 +++++++-- test/Parser/cxx-default-args.cpp | 1 + 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp index b84ea7b04f..510738ea81 100644 --- a/lib/Sema/SemaDeclCXX.cpp +++ b/lib/Sema/SemaDeclCXX.cpp @@ -388,9 +388,14 @@ void Sema::CheckExtraCXXDefaultArguments(Declarator &D) { ParmVarDecl *Param = cast(chunk.Fun.Params[argIdx].Param); if (Param->hasUnparsedDefaultArg()) { CachedTokens *Toks = chunk.Fun.Params[argIdx].DefaultArgTokens; + SourceRange SR; + if (Toks->size() > 1) + SR = SourceRange((*Toks)[1].getLocation(), + Toks->back().getLocation()); + else + SR = UnparsedDefaultArgLocs[Param]; Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) - << SourceRange((*Toks)[1].getLocation(), - Toks->back().getLocation()); + << SR; delete Toks; chunk.Fun.Params[argIdx].DefaultArgTokens = nullptr; } else if (Param->getDefaultArg()) { diff --git a/test/Parser/cxx-default-args.cpp b/test/Parser/cxx-default-args.cpp index 0c028f7366..0095a2f04d 100644 --- a/test/Parser/cxx-default-args.cpp +++ b/test/Parser/cxx-default-args.cpp @@ -39,4 +39,5 @@ struct S { struct U { void i(int x = ) {} // expected-error{{expected expression}} + typedef int *fp(int x = ); // expected-error{{default arguments can only be specified for parameters in a function declaration}} }; -- 2.40.0