It's an error to use a function declared in a class definition as a default argument before the function has been declared.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@73234 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td
index 2d55d4c..b0a3da0 100644
--- a/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/include/clang/Basic/DiagnosticSemaKinds.td
@@ -572,6 +572,11 @@
   "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 579fe6c..813b597 100644
--- a/include/clang/Parse/Action.h
+++ b/include/clang/Parse/Action.h
@@ -916,7 +916,8 @@
   /// 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 b9fe068..026e605 100644
--- a/lib/Frontend/PrintParserCallbacks.cpp
+++ b/lib/Frontend/PrintParserCallbacks.cpp
@@ -701,7 +701,8 @@
     }
 
     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 40fbc0c..9073c6d 100644
--- a/lib/Parse/ParseDecl.cpp
+++ b/lib/Parse/ParseDecl.cpp
@@ -2470,7 +2470,8 @@
             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 2df1ae2..c4b3b2d 100644
--- a/lib/Sema/Sema.h
+++ b/lib/Sema/Sema.h
@@ -412,8 +412,14 @@
                                          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 b59ac87..8f64e78 100644
--- a/lib/Sema/SemaDeclCXX.cpp
+++ b/lib/Sema/SemaDeclCXX.cpp
@@ -108,6 +108,8 @@
 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 @@
 /// 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 @@
     // 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 1abd5fb..c01c812 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -2484,9 +2484,19 @@
       // 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 f99e454..edbd6b3 100644
--- a/test/SemaCXX/default2.cpp
+++ b/test/SemaCXX/default2.cpp
@@ -115,9 +115,15 @@
 }
 
 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}}
+};