From dd2455b9a2c7064f24aef28dffa9272161eaf9ac Mon Sep 17 00:00:00 2001
From: Ben Langmuir <blangmuir@apple.com>
Date: Sat, 14 Nov 2015 03:26:14 +0000
Subject: [PATCH] [modules] Allow "redefinition" of typedef of anon tag from
 unimported submodule

r233345 started being stricter about typedef names for linkage purposes
in non-visible modules, but broke languages without the ODR.

rdar://23527954

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@253123 91177308-0d34-0410-b5e6-96231b3b80d8
---
 lib/AST/Decl.cpp                         | 3 ++-
 lib/Sema/SemaDecl.cpp                    | 3 ++-
 test/Index/usrs.m                        | 2 +-
 test/Modules/Inputs/module.map           | 7 +++++++
 test/Modules/Inputs/typedef-tag-hidden.h | 1 +
 test/Modules/Inputs/typedef-tag.h        | 1 +
 test/Modules/typedef-tag-not-visible.m   | 8 ++++++++
 7 files changed, 22 insertions(+), 3 deletions(-)
 create mode 100644 test/Modules/Inputs/typedef-tag-hidden.h
 create mode 100644 test/Modules/Inputs/typedef-tag.h
 create mode 100644 test/Modules/typedef-tag-not-visible.m

diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp
index 3f6941ab72d..edbbc6a8c4f 100644
--- a/lib/AST/Decl.cpp
+++ b/lib/AST/Decl.cpp
@@ -1256,7 +1256,8 @@ static LinkageInfo computeLVForDecl(const NamedDecl *D,
     case Decl::TypeAlias:
       // A typedef declaration has linkage if it gives a type a name for
       // linkage purposes.
-      if (!cast<TypedefNameDecl>(D)
+      if (!D->getASTContext().getLangOpts().CPlusPlus ||
+          !cast<TypedefNameDecl>(D)
                ->getAnonDeclWithTypedefName(/*AnyRedecl*/true))
         return LinkageInfo::none();
       break;
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index e7c10d18a66..d051c4318dd 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/lib/Sema/SemaDecl.cpp
@@ -1829,7 +1829,8 @@ static void filterNonConflictingPreviousTypedefDecls(Sema &S,
 
       // If both declarations give a tag declaration a typedef name for linkage
       // purposes, then they declare the same entity.
-      if (OldTD->getAnonDeclWithTypedefName(/*AnyRedecl*/true) &&
+      if (S.getLangOpts().CPlusPlus &&
+          OldTD->getAnonDeclWithTypedefName(/*AnyRedecl*/true) &&
           Decl->getAnonDeclWithTypedefName())
         continue;
     }
diff --git a/test/Index/usrs.m b/test/Index/usrs.m
index aa0c4a04fc7..fc3fbc91057 100644
--- a/test/Index/usrs.m
+++ b/test/Index/usrs.m
@@ -119,7 +119,7 @@ int test_multi_declaration(void) {
 // CHECK: usrs.m c:@SA@MyStruct Extent=[15:9 - 18:2]
 // CHECK: usrs.m c:@SA@MyStruct@FI@wa Extent=[16:3 - 16:9]
 // CHECK: usrs.m c:@SA@MyStruct@FI@moo Extent=[17:3 - 17:10]
-// CHECK: usrs.m c:@T@MyStruct Extent=[15:1 - 18:11]
+// CHECK: usrs.m c:usrs.m@T@MyStruct Extent=[15:1 - 18:11]
 // CHECK: usrs.m c:@E@Pizza Extent=[20:1 - 23:2]
 // CHECK: usrs.m c:@E@Pizza@CHEESE Extent=[21:3 - 21:9]
 // CHECK: usrs.m c:@E@Pizza@MUSHROOMS Extent=[22:3 - 22:12]
diff --git a/test/Modules/Inputs/module.map b/test/Modules/Inputs/module.map
index d195e2f0980..226d45fdf44 100644
--- a/test/Modules/Inputs/module.map
+++ b/test/Modules/Inputs/module.map
@@ -379,3 +379,10 @@ module DebugSubmodules {
 module ExtensionTestA {
   header "ExtensionTestA.h"
 }
+
+module TypedefTag {
+  header "typedef-tag.h"
+  explicit module Hidden {
+    header "typedef-tag-hidden.h"
+  }
+}
diff --git a/test/Modules/Inputs/typedef-tag-hidden.h b/test/Modules/Inputs/typedef-tag-hidden.h
new file mode 100644
index 00000000000..eb59d69cc6e
--- /dev/null
+++ b/test/Modules/Inputs/typedef-tag-hidden.h
@@ -0,0 +1 @@
+typedef struct { int x; } TypedefStructHidden_t;
diff --git a/test/Modules/Inputs/typedef-tag.h b/test/Modules/Inputs/typedef-tag.h
new file mode 100644
index 00000000000..77dff951174
--- /dev/null
+++ b/test/Modules/Inputs/typedef-tag.h
@@ -0,0 +1 @@
+typedef struct { int x; } TypedefStructVisible_t;
diff --git a/test/Modules/typedef-tag-not-visible.m b/test/Modules/typedef-tag-not-visible.m
new file mode 100644
index 00000000000..e1a640633f6
--- /dev/null
+++ b/test/Modules/typedef-tag-not-visible.m
@@ -0,0 +1,8 @@
+// RUN: rm -rf %t
+// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -I %S/Inputs %s -verify
+
+@import TypedefTag;
+
+typedef struct { int x; } TypedefStructHidden_t;
+typedef struct { int x; } TypedefStructVisible_t; // expected-error{{typedef redefinition}}
+// expected-note@typedef-tag.h:1 {{here}}
-- 
GitLab