[PATCH] D41237: [Frontend] Handle skipped bodies in template instantiations

Ilya Biryukov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 19 01:56:53 PST 2017


ilya-biryukov updated this revision to Diff 127473.
ilya-biryukov marked an inline comment as done.
ilya-biryukov added a comment.

- Added a check for null in ActOnSkippedBody


Repository:
  rC Clang

https://reviews.llvm.org/D41237

Files:
  lib/Sema/SemaDecl.cpp
  lib/Sema/SemaTemplateInstantiateDecl.cpp
  test/Index/skipped-bodies-templates.cpp


Index: test/Index/skipped-bodies-templates.cpp
===================================================================
--- /dev/null
+++ test/Index/skipped-bodies-templates.cpp
@@ -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
Index: lib/Sema/SemaTemplateInstantiateDecl.cpp
===================================================================
--- lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -3855,7 +3855,8 @@
   }
 
   // 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 @@
       }
     }
 
-    // Instantiate the function body.
-    StmtResult Body = SubstStmt(Pattern, TemplateArgs);
+    if (PatternDecl->hasSkippedBody()) {
+      ActOnSkippedFunctionBody(Function);
+    } else {
+      // Instantiate the function body.
+      StmtResult Body = SubstStmt(Pattern, TemplateArgs);
 
-    if (Body.isInvalid())
-      Function->setInvalidDecl();
+      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);
+      // FIXME: finishing the function body while in an expression evaluation
+      // context seems wrong. Investigate more.
+      ActOnFinishFunctionBody(Function, Body.get(),
+                              /*IsInstantiation=*/true);
+    }
 
     PerformDependentDiagnostics(PatternDecl, TemplateArgs);
 
Index: lib/Sema/SemaDecl.cpp
===================================================================
--- lib/Sema/SemaDecl.cpp
+++ lib/Sema/SemaDecl.cpp
@@ -12179,9 +12179,11 @@
 }
 
 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;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D41237.127473.patch
Type: text/x-patch
Size: 3121 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20171219/265fdea2/attachment.bin>


More information about the cfe-commits mailing list