]> granicus.if.org Git - clang/commitdiff
Tweak "key function" rules so that they work for templates with virtual
authorEli Friedman <eli.friedman@gmail.com>
Sat, 5 Dec 2009 04:55:55 +0000 (04:55 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Sat, 5 Dec 2009 04:55:55 +0000 (04:55 +0000)
inline functions.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@90645 91177308-0d34-0410-b5e6-96231b3b80d8

lib/AST/RecordLayoutBuilder.cpp
test/CodeGenCXX/virt-template-vtable.cpp [new file with mode: 0644]

index 3a1e248dbc0bf76718b9eb343f536ed617a4ed21..4c7b9119348edd13f2d8809179c80c237ae22185 100644 (file)
@@ -663,6 +663,20 @@ void ASTRecordLayoutBuilder::UpdateAlignment(unsigned NewAlignment) {
   Alignment = NewAlignment;
 }
 
+static bool MethodHasBody(const CXXMethodDecl *MD, const FunctionDecl *&fn) {
+  // Simple case: function has a body
+  if (MD->getBody(fn))
+    return true;
+
+  // Complex case: function is an instantiation of a function which has a
+  // body, but the definition hasn't been instantiated.
+  const FunctionDecl *PatternDecl = MD->getTemplateInstantiationPattern();
+  if (PatternDecl && PatternDecl->getBody(fn))
+    return true;
+
+  return false;
+}
+
 static const CXXMethodDecl *GetKeyFunction(const CXXRecordDecl *RD) {
   if (!RD->isDynamicClass())
     return 0;
@@ -683,7 +697,7 @@ static const CXXMethodDecl *GetKeyFunction(const CXXRecordDecl *RD) {
       continue;
     
     const FunctionDecl *fn;
-    if (MD->getBody(fn) && !fn->isOutOfLine())
+    if (MethodHasBody(MD, fn) && !fn->isOutOfLine())
       continue;
     
     // We found it.
diff --git a/test/CodeGenCXX/virt-template-vtable.cpp b/test/CodeGenCXX/virt-template-vtable.cpp
new file mode 100644 (file)
index 0000000..478daa7
--- /dev/null
@@ -0,0 +1,12 @@
+// RUN: clang-cc %s -emit-llvm -o - | FileCheck %s
+
+template<class T> class A {
+  A() {}
+  virtual void a() {}
+};
+class B : A<int> {
+  B();
+};
+B::B() {}
+
+// CHECK: @_ZTV1AIiE = linkonce_odr constant