From ccd63c08661e585a27f9ec1c6aa51f81f9e2623a Mon Sep 17 00:00:00 2001
From: Richard Smith <richard-llvm@metafoo.co.uk>
Date: Wed, 3 Jan 2018 02:34:35 +0000
Subject: [PATCH] PR35697: look at the first declaration when determining
 whether a function or variable is extern "C" in linkage calculations.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@321686 91177308-0d34-0410-b5e6-96231b3b80d8
---
 lib/AST/Decl.cpp          |  6 +++---
 test/SemaCXX/extern-c.cpp | 17 +++++++++++++++++
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp
index 4c1d591b41e..918466236bf 100644
--- a/lib/AST/Decl.cpp
+++ b/lib/AST/Decl.cpp
@@ -779,7 +779,7 @@ LinkageComputer::getLVForNamespaceScopeDecl(const NamedDecl *D,
     // unique-external linkage, it's not legally usable from outside
     // this translation unit.  However, we should use the C linkage
     // rules instead for extern "C" declarations.
-    if (Context.getLangOpts().CPlusPlus && !Function->isInExternCContext()) {
+    if (Context.getLangOpts().CPlusPlus && !isFirstInExternCContext(Function)) {
       // Only look at the type-as-written. Otherwise, deducing the return type
       // of a function could change its linkage.
       QualType TypeAsWritten = Function->getType();
@@ -1165,7 +1165,7 @@ LinkageInfo LinkageComputer::getLVForLocalDecl(const NamedDecl *D,
                                                LVComputationKind computation) {
   if (const auto *Function = dyn_cast<FunctionDecl>(D)) {
     if (Function->isInAnonymousNamespace() &&
-        !Function->isInExternCContext())
+        !isFirstInExternCContext(Function))
       return getInternalLinkageFor(Function);
 
     // This is a "void f();" which got merged with a file static.
@@ -1188,7 +1188,7 @@ LinkageInfo LinkageComputer::getLVForLocalDecl(const NamedDecl *D,
 
   if (const auto *Var = dyn_cast<VarDecl>(D)) {
     if (Var->hasExternalStorage()) {
-      if (Var->isInAnonymousNamespace() && !Var->isInExternCContext())
+      if (Var->isInAnonymousNamespace() && !isFirstInExternCContext(Var))
         return getInternalLinkageFor(Var);
 
       LinkageInfo LV;
diff --git a/test/SemaCXX/extern-c.cpp b/test/SemaCXX/extern-c.cpp
index fa6c2b1990c..1a32beec7ba 100644
--- a/test/SemaCXX/extern-c.cpp
+++ b/test/SemaCXX/extern-c.cpp
@@ -242,3 +242,20 @@ namespace tag_hiding {
   ExternCStruct3 *q3 = p3;
   ExternCStruct4 *q4 = p4; // expected-error {{ambiguous}}
 }
+
+namespace PR35697 {
+  typedef struct {} *ReturnStruct;
+  extern "C" ReturnStruct PR35697_f();
+  extern "C" ReturnStruct PR35697_v;
+  ReturnStruct PR35697_f();
+  ReturnStruct PR35697_v;
+
+  namespace {
+    extern "C" ReturnStruct PR35697_f();
+    extern "C" ReturnStruct PR35697_v;
+    void q() {
+      extern ReturnStruct PR35697_f();
+      extern ReturnStruct PR35697_v;
+    }
+  }
+}
-- 
GitLab