hokein created this revision.
hokein added a reviewer: ilya-biryukov.

Otherwise clang will provide an unexpected diagnostic error.


Repository:
  rC Clang

https://reviews.llvm.org/D46684

Files:
  lib/Sema/SemaDecl.cpp
  test/Index/skipped-auto-fn-templates.cpp


Index: test/Index/skipped-auto-fn-templates.cpp
===================================================================
--- /dev/null
+++ test/Index/skipped-auto-fn-templates.cpp
@@ -0,0 +1,10 @@
+// RUN: env CINDEXTEST_SKIP_FUNCTION_BODIES=1 c-index-test -test-load-source 
all %s 2>&1 \
+// RUN: | FileCheck %s
+
+template <typename T>
+auto foo(T a) {
+  return a;
+}
+
+int b = foo(0);
+// CHECK-NOT: error: function 'foo<int>' with deduced return type cannot be 
used before it is defined
Index: lib/Sema/SemaDecl.cpp
===================================================================
--- lib/Sema/SemaDecl.cpp
+++ lib/Sema/SemaDecl.cpp
@@ -12637,8 +12637,12 @@
   // rest of the file.
   // We cannot skip the body of a function with an undeduced return type,
   // because any callers of that function need to know the type.
+  // We cannot skip the body of a function template with an 'auto' return type
+  // which is dependent on a template parameter, we need to see the function
+  // body before using it.
   if (const FunctionDecl *FD = D->getAsFunction())
-    if (FD->isConstexpr() || FD->getReturnType()->isUndeducedType())
+    if (FD->isConstexpr() || FD->getReturnType()->isUndeducedType() ||
+        FD->getReturnType()->isInstantiationDependentType())
       return false;
   return Consumer.shouldSkipFunctionBody(D);
 }


Index: test/Index/skipped-auto-fn-templates.cpp
===================================================================
--- /dev/null
+++ test/Index/skipped-auto-fn-templates.cpp
@@ -0,0 +1,10 @@
+// RUN: env CINDEXTEST_SKIP_FUNCTION_BODIES=1 c-index-test -test-load-source all %s 2>&1 \
+// RUN: | FileCheck %s
+
+template <typename T>
+auto foo(T a) {
+  return a;
+}
+
+int b = foo(0);
+// CHECK-NOT: error: function 'foo<int>' with deduced return type cannot be used before it is defined
Index: lib/Sema/SemaDecl.cpp
===================================================================
--- lib/Sema/SemaDecl.cpp
+++ lib/Sema/SemaDecl.cpp
@@ -12637,8 +12637,12 @@
   // rest of the file.
   // We cannot skip the body of a function with an undeduced return type,
   // because any callers of that function need to know the type.
+  // We cannot skip the body of a function template with an 'auto' return type
+  // which is dependent on a template parameter, we need to see the function
+  // body before using it.
   if (const FunctionDecl *FD = D->getAsFunction())
-    if (FD->isConstexpr() || FD->getReturnType()->isUndeducedType())
+    if (FD->isConstexpr() || FD->getReturnType()->isUndeducedType() ||
+        FD->getReturnType()->isInstantiationDependentType())
       return false;
   return Consumer.shouldSkipFunctionBody(D);
 }
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D46684: [... Haojian Wu via Phabricator via cfe-commits

Reply via email to