[prev in list] [next in list] [prev in thread] [next in thread] 

List:       cfe-commits
Subject:    Re: [PATCH] D11194: Instantiate function declarations in instantiated functions.
From:       Serge Pavlov via cfe-commits <cfe-commits () lists ! llvm ! org>
Date:       2015-08-23 10:23:33
Message-ID: 5a99b7bbc483b77e0430e5de9fb8d370 () localhost ! localdomain
[Download RAW message or body]

This revision was automatically updated to reflect the committed changes.
Closed by commit rL245810: Instantiate function declarations in instantiated \
functions. (authored by sepavloff).

Changed prior to commit:
  http://reviews.llvm.org/D11194?vs=32597&id=32923#toc

Repository:
  rL LLVM

http://reviews.llvm.org/D11194

Files:
  cfe/trunk/include/clang/AST/DeclBase.h
  cfe/trunk/lib/AST/DeclBase.cpp
  cfe/trunk/lib/Sema/SemaTemplateInstantiate.cpp
  cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp
  cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/default-arguments.cpp
  cfe/trunk/test/SemaTemplate/default-arguments.cpp
  cfe/trunk/test/SemaTemplate/instantiate-exception-spec-cxx11.cpp


["D11194.32923.patch" (text/x-patch)]

Index: cfe/trunk/include/clang/AST/DeclBase.h
===================================================================
--- cfe/trunk/include/clang/AST/DeclBase.h
+++ cfe/trunk/include/clang/AST/DeclBase.h
@@ -728,6 +728,15 @@
     return getParentFunctionOrMethod() == nullptr;
   }
 
+  /// \brief Returns true if this declaration lexically is inside a function.
+  /// It recognizes non-defining declarations as well as members of local
+  /// classes:
+  /// \code
+  ///     void foo() { void bar(); }
+  ///     void foo2() { class ABC { void bar(); }; }
+  /// \endcode
+  bool isLexicallyWithinFunctionOrMethod() const;
+
   /// \brief If this decl is defined inside a function/method/block it returns
   /// the corresponding DeclContext, otherwise it returns null.
   const DeclContext *getParentFunctionOrMethod() const;
Index: cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/default-arguments.cpp
===================================================================
--- cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/default-arguments.cpp
+++ cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/default-arguments.cpp
@@ -26,23 +26,26 @@
 };
 
 struct NoDefaultCtor {
-  NoDefaultCtor(const NoDefaultCtor&); // expected-note{{candidate constructor}}
+  NoDefaultCtor(const NoDefaultCtor&); // expected-note{{candidate constructor}} \
+                                       // expected-note{{candidate constructor not \
viable: requires 1 argument, but 0 were provided}}  ~NoDefaultCtor();
 };
 
 template<typename T>
 void defargs_in_template_unused(T t) {
-  auto l1 = [](const T& value = T()) { };
+  auto l1 = [](const T& value = T()) { };  // expected-error{{no matching \
constructor for initialization of 'NoDefaultCtor'}}  l1(t);
 }
 
 template void defargs_in_template_unused(NonPOD);
-template void defargs_in_template_unused(NoDefaultCtor);
+template void defargs_in_template_unused(NoDefaultCtor);  // expected-note{{in \
instantiation of function template specialization \
'defargs_in_template_unused<NoDefaultCtor>' requested here}}  
 template<typename T>
 void defargs_in_template_used() {
-  auto l1 = [](const T& value = T()) { }; // expected-error{{no matching constructor \
                for initialization of 'NoDefaultCtor'}}
-  l1(); // expected-note{{in instantiation of default function argument expression \
for 'operator()<NoDefaultCtor>' required here}} +  auto l1 = [](const T& value = T()) \
{ }; // expected-error{{no matching constructor for initialization of \
'NoDefaultCtor'}} \ +                                          // \
expected-note{{candidate function not viable: requires single argument 'value', but \
no arguments were provided}} \ +                                          // \
expected-note{{conversion candidate of type 'void (*)(const NoDefaultCtor &)'}} +  \
l1(); // expected-error{{no matching function for call to object of type '(lambda at \
}}  }
 
 template void defargs_in_template_used<NonPOD>();
Index: cfe/trunk/test/SemaTemplate/instantiate-exception-spec-cxx11.cpp
===================================================================
--- cfe/trunk/test/SemaTemplate/instantiate-exception-spec-cxx11.cpp
+++ cfe/trunk/test/SemaTemplate/instantiate-exception-spec-cxx11.cpp
@@ -178,3 +178,11 @@
   }
 
 }
+
+namespace NondefDecls {
+  template<typename T> void f1() {
+    int g1(int) noexcept(T::error); // expected-error{{type 'int' cannot be used \
prior to '::' because it has no members}} +  }
+  template void f1<int>(); // expected-note{{in instantiation of function template \
specialization 'NondefDecls::f1<int>' requested here}} +}
+
Index: cfe/trunk/test/SemaTemplate/default-arguments.cpp
===================================================================
--- cfe/trunk/test/SemaTemplate/default-arguments.cpp
+++ cfe/trunk/test/SemaTemplate/default-arguments.cpp
@@ -159,3 +159,10 @@
 
   int g() { X<int>::f(0); } // expected-note {{in instantiation of template class \
'DR1635::X<int>' requested here}}  }
+
+namespace NondefDecls {
+  template<typename T> void f1() {
+    int g1(int defarg = T::error);  // expected-error{{type 'int' cannot be used \
prior to '::' because it has no members}} +  }
+  template void f1<int>();  // expected-note{{in instantiation of function template \
specialization 'NondefDecls::f1<int>' requested here}} +}
Index: cfe/trunk/lib/AST/DeclBase.cpp
===================================================================
--- cfe/trunk/lib/AST/DeclBase.cpp
+++ cfe/trunk/lib/AST/DeclBase.cpp
@@ -266,6 +266,18 @@
   }
 }
 
+bool Decl::isLexicallyWithinFunctionOrMethod() const {
+  const DeclContext *LDC = getLexicalDeclContext();
+  do {
+    if (LDC->isFunctionOrMethod())
+      return true;
+    if (!isa<TagDecl>(LDC))
+      return false;
+    LDC = LDC->getParent();
+  } while (LDC);
+  return false;
+}
+
 bool Decl::isInAnonymousNamespace() const {
   const DeclContext *DC = getDeclContext();
   do {
Index: cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp
===================================================================
--- cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -3247,16 +3247,11 @@
     // exception specification.
     // DR1484: Local classes and their members are instantiated along with the
     // containing function.
-    bool RequireInstantiation = false;
-    if (CXXRecordDecl *Cls = dyn_cast<CXXRecordDecl>(Tmpl->getDeclContext())) {
-      if (Cls->isLocalClass())
-        RequireInstantiation = true;
-    }
     if (SemaRef.getLangOpts().CPlusPlus11 &&
         EPI.ExceptionSpec.Type != EST_None &&
         EPI.ExceptionSpec.Type != EST_DynamicNone &&
         EPI.ExceptionSpec.Type != EST_BasicNoexcept &&
-        !RequireInstantiation) {
+        !Tmpl->isLexicallyWithinFunctionOrMethod()) {
       FunctionDecl *ExceptionSpecTemplate = Tmpl;
       if (EPI.ExceptionSpec.Type == EST_Uninstantiated)
         ExceptionSpecTemplate = EPI.ExceptionSpec.SourceTemplate;
Index: cfe/trunk/lib/Sema/SemaTemplateInstantiate.cpp
===================================================================
--- cfe/trunk/lib/Sema/SemaTemplateInstantiate.cpp
+++ cfe/trunk/lib/Sema/SemaTemplateInstantiate.cpp
@@ -1682,11 +1682,10 @@
     UnparsedDefaultArgInstantiations[OldParm].push_back(NewParm);
   } else if (Expr *Arg = OldParm->getDefaultArg()) {
     FunctionDecl *OwningFunc = cast<FunctionDecl>(OldParm->getDeclContext());
-    CXXRecordDecl *ClassD = dyn_cast<CXXRecordDecl>(OwningFunc->getDeclContext());
-    if (ClassD && ClassD->isLocalClass() && !ClassD->isLambda()) {
-      // If this is a method of a local class, as per DR1484 its default
-      // arguments must be instantiated.
-      Sema::ContextRAII SavedContext(*this, ClassD);
+    if (OwningFunc->isLexicallyWithinFunctionOrMethod()) {
+      // Instantiate default arguments for methods of local classes (DR1484)
+      // and non-defining declarations.
+      Sema::ContextRAII SavedContext(*this, OwningFunc);
       LocalInstantiationScope Local(*this);
       ExprResult NewArg = SubstExpr(Arg, TemplateArgs);
       if (NewArg.isUsable())


[Attachment #4 (text/plain)]

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic