]> granicus.if.org Git - clang/commitdiff
It's an error to use a function declared in a class definition as a default argument...
authorAnders Carlsson <andersca@mac.com>
Fri, 12 Jun 2009 16:51:40 +0000 (16:51 +0000)
committerAnders Carlsson <andersca@mac.com>
Fri, 12 Jun 2009 16:51:40 +0000 (16:51 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@73234 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
include/clang/Parse/Action.h
lib/Frontend/PrintParserCallbacks.cpp
lib/Parse/ParseDecl.cpp
lib/Sema/Sema.h
lib/Sema/SemaDeclCXX.cpp
lib/Sema/SemaExpr.cpp
test/SemaCXX/default2.cpp

index 2d55d4c4c5e7a93ae28606e965d56d4996052aeb..b0a3da0d04625338959734a5b68c4f9763427998 100644 (file)
@@ -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">;
index 579fe6cb038ce87d6a406809dfe3ce09400c050d..813b5974e3f4f375bc2e3c8c5bfa12c45fa254e2 100644 (file)
@@ -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.
index b9fe0680afd00ff6dae210bc5ef55882d8f70fe3..026e605c79437efacb18d04dba69a9bd3158bad9 100644 (file)
@@ -701,7 +701,8 @@ namespace {
     }
 
     virtual void ActOnParamUnparsedDefaultArgument(DeclPtrTy param,
-                                                   SourceLocation EqualLoc) {
+                                                   SourceLocation EqualLoc,
+                                                   SourceLocation ArgLoc) {
       Out << __FUNCTION__ << "\n";
     }
 
index 40fbc0ccad42d251bf8ae8ce5e9f36b3fd39a9d7..9073c6dbd19306ab13d15faf103160f7229326e2 100644 (file)
@@ -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();
index 2df1ae295044bdb1564bf9e20bfcbdb8c1eecf2d..c4b3b2d050a77946eb608473b2d5f8789b85c8ca 100644 (file)
@@ -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);
index b59ac879d190e87461c8b5c630e07ad7237b7737..8f64e78c522b422180937e0e110bbbadba9d8dc4 100644 (file)
@@ -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);
index 1abd5fb302e7e948a87eb8c7c7eaafe74c525074..c01c812be60142e1f9a2706af3a35c6a3513ef50 100644 (file)
@@ -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);
index f99e45415dc48fc9d21a7d7d2a3533d8b5c78ecd..edbd6b3e206aa3c048be5b52b777ed763f840f41 100644 (file)
@@ -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}}
+};