]> granicus.if.org Git - clang/commitdiff
[ODRHash] Fix hashing for friend functions.
authorRichard Trieu <rtrieu@google.com>
Thu, 22 Feb 2018 05:50:29 +0000 (05:50 +0000)
committerRichard Trieu <rtrieu@google.com>
Thu, 22 Feb 2018 05:50:29 +0000 (05:50 +0000)
When hashing a templated function, use the hash of the function it was
instantiated from.

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

lib/AST/Decl.cpp
lib/AST/ODRHash.cpp
test/Modules/Inputs/odr_hash-Friend/Bad.h [new file with mode: 0644]
test/Modules/Inputs/odr_hash-Friend/Good.h [new file with mode: 0644]
test/Modules/Inputs/odr_hash-Friend/module.modulemap
test/Modules/odr_hash-Friend.cpp

index 30599ea7cfbe025f8b54c9f1b53422c2c8f9f87d..c4da87c013d92b1ea16de310f1e94f7883a43224 100644 (file)
@@ -3625,6 +3625,12 @@ unsigned FunctionDecl::getODRHash() {
     }
   }
 
+  if (auto *FT = getInstantiatedFromMemberFunction()) {
+    HasODRHash = true;
+    ODRHash = FT->getODRHash();
+    return ODRHash;
+  }
+
   class ODRHash Hash;
   Hash.AddFunctionDecl(this);
   HasODRHash = true;
index 4104d3454e52367f58897eda5581bb6f2d800a3d..528e32aaba2190f1101a750be40555e4204401bb 100644 (file)
@@ -484,9 +484,6 @@ void ODRHash::AddFunctionDecl(const FunctionDecl *Function) {
   if (!Function->hasBody()) return;
   if (!Function->getBody()) return;
 
-  // TODO: Fix hashing friend functions.
-  if (Function->getFriendObjectKind()) return;
-
   // Skip functions that are specializations or in specialization context.
   const DeclContext *DC = Function;
   while (DC) {
diff --git a/test/Modules/Inputs/odr_hash-Friend/Bad.h b/test/Modules/Inputs/odr_hash-Friend/Bad.h
new file mode 100644 (file)
index 0000000..c0a6d1f
--- /dev/null
@@ -0,0 +1,17 @@
+template <class T>
+struct iterator {
+  void Compare(const iterator &x) { return; }
+  friend void Check(iterator) { return; }
+};
+
+template <class T = int> struct Box {
+  iterator<T> I;
+
+  void test() {
+    Check(I);
+    I.Compare(I);
+  }
+};
+
+// Force instantiation of Box<int>
+Box<> B;
diff --git a/test/Modules/Inputs/odr_hash-Friend/Good.h b/test/Modules/Inputs/odr_hash-Friend/Good.h
new file mode 100644 (file)
index 0000000..4c38392
--- /dev/null
@@ -0,0 +1,17 @@
+template <class T>
+struct iterator {
+  void Compare(const iterator &x) { }
+  friend void Check(iterator) {}
+};
+
+template <class T = int> struct Box {
+  iterator<T> I;
+
+  void test() {
+    Check(I);
+    I.Compare(I);
+  }
+};
+
+// Force instantiation of Box<int>
+Box<> B;
index 28e1832e30e9a97d92316cd3a8433f1da4136e4f..449d51e7f70915fe2141a9eed742b468bc8c92ee 100644 (file)
@@ -13,3 +13,11 @@ module Module2 {
 module Module3 {
   header "M3.h"
 }
+
+module Good {
+  header "Good.h"
+}
+
+module Bad {
+  header "Bad.h"
+}
index 39c0c4b762c21e0adb17448e253b7a758c279862..8a2513e2f9cd013015095ce22a1adca34a9cf8ca 100644 (file)
@@ -1,21 +1,89 @@
 // RUN: rm -rf %t
 
-// RUN: %clang_cc1 -fmodules -fmodules-cache-path=%t/modules.cache \
+// PR35939: MicrosoftMangle.cpp triggers an assertion failure on this test.
+// UNSUPPORTED: system-windows
+
+// RUN: %clang_cc1 \
 // RUN:  -I %S/Inputs/odr_hash-Friend \
 // RUN:  -emit-obj -o /dev/null \
 // RUN:  -fmodules \
 // RUN:  -fimplicit-module-maps \
 // RUN:  -fmodules-cache-path=%t/modules.cache \
-// RUN:  -std=c++11 -x c++ %s -verify
+// RUN:  -std=c++11 -x c++ %s -verify -DTEST1 -fcolor-diagnostics
 
-// PR35939: MicrosoftMangle.cpp triggers an assertion failure on this test.
-// UNSUPPORTED: system-windows
+// RUN: %clang_cc1 \
+// RUN:  -I %S/Inputs/odr_hash-Friend \
+// RUN:  -emit-obj -o /dev/null \
+// RUN:  -fmodules \
+// RUN:  -fimplicit-module-maps \
+// RUN:  -fmodules-cache-path=%t/modules.cache \
+// RUN:  -std=c++11 -x c++ %s -verify -DTEST2 -fcolor-diagnostics
+
+// RUN: %clang_cc1 \
+// RUN:  -I %S/Inputs/odr_hash-Friend \
+// RUN:  -emit-obj -o /dev/null \
+// RUN:  -fmodules \
+// RUN:  -fimplicit-module-maps \
+// RUN:  -fmodules-cache-path=%t/modules.cache \
+// RUN:  -std=c++11 -x c++ %s -verify -DTEST3 -fcolor-diagnostics
+
+// RUN: %clang_cc1 \
+// RUN:  -I %S/Inputs/odr_hash-Friend \
+// RUN:  -emit-obj -o /dev/null \
+// RUN:  -fmodules \
+// RUN:  -fimplicit-module-maps \
+// RUN:  -fmodules-cache-path=%t/modules.cache \
+// RUN:  -std=c++11 -x c++ %s -verify -DTEST3 -fcolor-diagnostics
+
+// RUN: %clang_cc1 \
+// RUN:  -I %S/Inputs/odr_hash-Friend \
+// RUN:  -emit-obj -o /dev/null \
+// RUN:  -fmodules \
+// RUN:  -fimplicit-module-maps \
+// RUN:  -fmodules-cache-path=%t/modules.cache \
+// RUN:  -std=c++11 -x c++ %s -verify -DTEST3 -fcolor-diagnostics
 
+#if defined(TEST1)
+#include "Box.h"
+#include "M1.h"
+#include "M3.h"
 // expected-no-diagnostics
+#endif
 
+#if defined(TEST2)
 #include "Box.h"
 #include "M1.h"
 #include "M3.h"
+#include "Good.h"
+// expected-no-diagnostics
+#endif
+
+#if defined(TEST3)
+#include "Good.h"
+#include "Box.h"
+#include "M1.h"
+#include "M3.h"
+// expected-no-diagnostics
+#endif
+
+#if defined(TEST4)
+#include "Box.h"
+#include "M1.h"
+#include "M3.h"
+#include "Bad.h"
+// expected-error@Bad.h:* {{'Check' has different definitions in different modules; definition in module 'Bad' first difference is function body}}
+// expected-note@Box.h:* {{but in 'Box' found a different body}}
+#endif
+
+#if defined(TEST5)
+#include "Bad.h"
+#include "Box.h"
+#include "M1.h"
+#include "M3.h"
+// expected-error@Bad.h:* {{'Check' has different definitions in different modules; definition in module 'Bad' first difference is function body}}
+// expected-note@Box.h:* {{but in 'Box' found a different body}}
+#endif
+
 
 void Run() {
   Box<> Present;