r321174 - [Frontend] Handle skipped bodies in template instantiations

Ilya Biryukov via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 20 06:32:38 PST 2017


Author: ibiryukov
Date: Wed Dec 20 06:32:38 2017
New Revision: 321174

URL: http://llvm.org/viewvc/llvm-project?rev=321174&view=rev
Log:
[Frontend] Handle skipped bodies in template instantiations

Summary:
- Fixed an assert in Sema::InstantiateFunctionDefinition and added
  support for instantiating a function template with skipped body.
- Properly call setHasSkippedBody for FunctionTemplateDecl passed to
  Sema::ActOnSkippedFunctionBody.

Reviewers: sepavloff, bkramer

Reviewed By: sepavloff

Subscribers: klimek, cfe-commits

Differential Revision: https://reviews.llvm.org/D41237

Added:
    cfe/trunk/test/Index/skipped-bodies-templates.cpp
Modified:
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=321174&r1=321173&r2=321174&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Wed Dec 20 06:32:38 2017
@@ -12179,9 +12179,11 @@ bool Sema::canSkipFunctionBody(Decl *D)
 }
 
 Decl *Sema::ActOnSkippedFunctionBody(Decl *Decl) {
-  if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(Decl))
+  if (!Decl)
+    return nullptr;
+  if (FunctionDecl *FD = Decl->getAsFunction())
     FD->setHasSkippedBody();
-  else if (ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(Decl))
+  else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(Decl))
     MD->setHasSkippedBody();
   return Decl;
 }

Modified: cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp?rev=321174&r1=321173&r2=321174&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp Wed Dec 20 06:32:38 2017
@@ -3855,7 +3855,8 @@ void Sema::InstantiateFunctionDefinition
   }
 
   // Note, we should never try to instantiate a deleted function template.
-  assert((Pattern || PatternDecl->isDefaulted()) &&
+  assert((Pattern || PatternDecl->isDefaulted() ||
+          PatternDecl->hasSkippedBody()) &&
          "unexpected kind of function template definition");
 
   // C++1y [temp.explicit]p10:
@@ -3940,16 +3941,20 @@ void Sema::InstantiateFunctionDefinition
       }
     }
 
-    // Instantiate the function body.
-    StmtResult Body = SubstStmt(Pattern, TemplateArgs);
-
-    if (Body.isInvalid())
-      Function->setInvalidDecl();
-
-    // FIXME: finishing the function body while in an expression evaluation
-    // context seems wrong. Investigate more.
-    ActOnFinishFunctionBody(Function, Body.get(),
-                            /*IsInstantiation=*/true);
+    if (PatternDecl->hasSkippedBody()) {
+      ActOnSkippedFunctionBody(Function);
+    } else {
+      // Instantiate the function body.
+      StmtResult Body = SubstStmt(Pattern, TemplateArgs);
+
+      if (Body.isInvalid())
+        Function->setInvalidDecl();
+
+      // FIXME: finishing the function body while in an expression evaluation
+      // context seems wrong. Investigate more.
+      ActOnFinishFunctionBody(Function, Body.get(),
+                              /*IsInstantiation=*/true);
+    }
 
     PerformDependentDiagnostics(PatternDecl, TemplateArgs);
 

Added: cfe/trunk/test/Index/skipped-bodies-templates.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/skipped-bodies-templates.cpp?rev=321174&view=auto
==============================================================================
--- cfe/trunk/test/Index/skipped-bodies-templates.cpp (added)
+++ cfe/trunk/test/Index/skipped-bodies-templates.cpp Wed Dec 20 06:32:38 2017
@@ -0,0 +1,27 @@
+// RUN: env CINDEXTEST_SKIP_FUNCTION_BODIES=1 c-index-test -test-load-source all %s 2>&1 \
+// RUN: | FileCheck %s
+
+
+template <class T>
+struct Foo {
+  inline int with_body() {
+    return 100;
+  }
+
+  inline int without_body();
+};
+
+
+int bar = Foo<int>().with_body() + Foo<int>().without_body();
+// CHECK-NOT: warning: inline function 'Foo<int>::with_body' is not defined
+// CHECK: warning: inline function 'Foo<int>::without_body' is not defined
+
+template <class T>
+inline int with_body() { return 10; }
+
+template <class T>
+inline int without_body();
+
+int baz = with_body<int>() + without_body<int>();
+// CHECK-NOT: warning: inline function 'with_body<int>' is not defined
+// CHECK: warning: inline function 'without_body<int>' is not defined




More information about the cfe-commits mailing list