diff options
-rw-r--r-- | include/clang/Basic/DiagnosticSemaKinds.td | 5 | ||||
-rw-r--r-- | include/clang/Parse/Action.h | 3 | ||||
-rw-r--r-- | lib/Frontend/PrintParserCallbacks.cpp | 3 | ||||
-rw-r--r-- | lib/Parse/ParseDecl.cpp | 3 | ||||
-rw-r--r-- | lib/Sema/Sema.h | 8 | ||||
-rw-r--r-- | lib/Sema/SemaDeclCXX.cpp | 15 | ||||
-rw-r--r-- | lib/Sema/SemaExpr.cpp | 12 | ||||
-rw-r--r-- | test/SemaCXX/default2.cpp | 10 |
8 files changed, 49 insertions, 10 deletions
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 2d55d4c4c5..b0a3da0d04 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -572,6 +572,11 @@ def err_param_default_argument_nonfunc : Error< "default arguments can only be specified for parameters in a function " "declaration">; +def err_use_of_default_argument_to_function_declared_later : Error< + "use of default argument to function %0 that is declared later in class %1">; +def note_default_argument_declared_here : Note< + "default argument declared here">; + def ext_param_promoted_not_compatible_with_prototype : ExtWarn< "promoted type %0 of K&R function parameter is not compatible with the " "parameter type %1 declared in a previous prototype">; diff --git a/include/clang/Parse/Action.h b/include/clang/Parse/Action.h index 579fe6cb03..813b5974e3 100644 --- a/include/clang/Parse/Action.h +++ b/include/clang/Parse/Action.h @@ -916,7 +916,8 @@ public: /// because we're inside a class definition. Note that this default /// argument will be parsed later. virtual void ActOnParamUnparsedDefaultArgument(DeclPtrTy param, - SourceLocation EqualLoc) { } + SourceLocation EqualLoc, + SourceLocation ArgLoc) { } /// ActOnParamDefaultArgumentError - Parsing or semantic analysis of /// the default argument for the parameter param failed. diff --git a/lib/Frontend/PrintParserCallbacks.cpp b/lib/Frontend/PrintParserCallbacks.cpp index b9fe0680af..026e605c79 100644 --- a/lib/Frontend/PrintParserCallbacks.cpp +++ b/lib/Frontend/PrintParserCallbacks.cpp @@ -701,7 +701,8 @@ namespace { } virtual void ActOnParamUnparsedDefaultArgument(DeclPtrTy param, - SourceLocation EqualLoc) { + SourceLocation EqualLoc, + SourceLocation ArgLoc) { Out << __FUNCTION__ << "\n"; } diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp index 40fbc0ccad..9073c6dbd1 100644 --- a/lib/Parse/ParseDecl.cpp +++ b/lib/Parse/ParseDecl.cpp @@ -2470,7 +2470,8 @@ void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D, DefArgToks = 0; Actions.ActOnParamDefaultArgumentError(Param); } else - Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc); + Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, + (*DefArgToks)[1].getLocation()); } else { // Consume the '='. ConsumeToken(); diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h index 2df1ae2950..c4b3b2d050 100644 --- a/lib/Sema/Sema.h +++ b/lib/Sema/Sema.h @@ -412,8 +412,14 @@ public: SourceLocation EqualLoc, ExprArg defarg); virtual void ActOnParamUnparsedDefaultArgument(DeclPtrTy param, - SourceLocation EqualLoc); + SourceLocation EqualLoc, + SourceLocation ArgLoc); virtual void ActOnParamDefaultArgumentError(DeclPtrTy param); + + // Contains the locations of the beginning of unparsed default + // argument locations. + llvm::DenseMap<ParmVarDecl *,SourceLocation> UnparsedDefaultArgLocs; + virtual void AddInitializerToDecl(DeclPtrTy dcl, FullExprArg init); void AddInitializerToDecl(DeclPtrTy dcl, ExprArg init, bool DirectInit); void ActOnUninitializedDecl(DeclPtrTy dcl); diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp index b59ac879d1..8f64e78c52 100644 --- a/lib/Sema/SemaDeclCXX.cpp +++ b/lib/Sema/SemaDeclCXX.cpp @@ -108,6 +108,8 @@ void Sema::ActOnParamDefaultArgument(DeclPtrTy param, SourceLocation EqualLoc, ExprArg defarg) { ParmVarDecl *Param = cast<ParmVarDecl>(param.getAs<Decl>()); + UnparsedDefaultArgLocs.erase(Param); + ExprOwningPtr<Expr> DefaultArg(this, defarg.takeAs<Expr>()); QualType ParamType = Param->getType(); @@ -154,16 +156,23 @@ Sema::ActOnParamDefaultArgument(DeclPtrTy param, SourceLocation EqualLoc, /// because we're inside a class definition. Note that this default /// argument will be parsed later. void Sema::ActOnParamUnparsedDefaultArgument(DeclPtrTy param, - SourceLocation EqualLoc) { + SourceLocation EqualLoc, + SourceLocation ArgLoc) { ParmVarDecl *Param = cast<ParmVarDecl>(param.getAs<Decl>()); if (Param) Param->setUnparsedDefaultArg(); + + UnparsedDefaultArgLocs[Param] = ArgLoc; } /// ActOnParamDefaultArgumentError - Parsing or semantic analysis of /// the default argument for the parameter param failed. void Sema::ActOnParamDefaultArgumentError(DeclPtrTy param) { - cast<ParmVarDecl>(param.getAs<Decl>())->setInvalidDecl(); + ParmVarDecl *Param = cast<ParmVarDecl>(param.getAs<Decl>()); + + Param->setInvalidDecl(); + + UnparsedDefaultArgLocs.erase(Param); } /// CheckExtraCXXDefaultArguments - Check for any extra default @@ -285,7 +294,7 @@ void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) { // in a semantically valid state. for (p = 0; p <= LastMissingDefaultArg; ++p) { ParmVarDecl *Param = FD->getParamDecl(p); - if (Param->getDefaultArg()) { + if (Param->hasDefaultArg()) { if (!Param->hasUnparsedDefaultArg()) Param->getDefaultArg()->Destroy(Context); Param->setDefaultArg(0); diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 1abd5fb302..c01c812be6 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -2484,9 +2484,19 @@ Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, // Pass the argument. if (PerformCopyInitialization(Arg, ProtoArgType, "passing")) return true; - } else + } else { + if (FDecl->getParamDecl(i)->hasUnparsedDefaultArg()) { + Diag (Call->getSourceRange().getBegin(), + diag::err_use_of_default_argument_to_function_declared_later) << + FDecl << cast<CXXRecordDecl>(FDecl->getDeclContext())->getDeclName(); + Diag(UnparsedDefaultArgLocs[FDecl->getParamDecl(i)], + diag::note_default_argument_declared_here); + } + // We already type-checked the argument, so we know it works. Arg = new (Context) CXXDefaultArgExpr(FDecl->getParamDecl(i)); + } + QualType ArgType = Arg->getType(); Call->setArg(i, Arg); diff --git a/test/SemaCXX/default2.cpp b/test/SemaCXX/default2.cpp index f99e45415d..edbd6b3e20 100644 --- a/test/SemaCXX/default2.cpp +++ b/test/SemaCXX/default2.cpp @@ -115,9 +115,15 @@ void test_Z(const Z& z) { } struct ZZ { - void f(ZZ z = g()); // expected-error{{no matching constructor for initialization}} - static ZZ g(int = 17); + void f(ZZ z = g()); // expected-error{{no matching constructor for initialization}} + ZZ(ZZ&, int = 17); // expected-note{{candidate function}} }; + +// http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#325 +class C2 { + static void g(int = f()); // expected-error{{use of default argument to function 'f' that is declared later in class 'C2'}} + static int f(int = 10); // expected-note{{default argument declared here}} +}; |