]> granicus.if.org Git - clang/commitdiff
[modules] Apply ODR merging for function scoped tags only in C++ mode.
authorVassil Vassilev <v.g.vassilev@gmail.com>
Thu, 8 Sep 2016 20:34:41 +0000 (20:34 +0000)
committerVassil Vassilev <v.g.vassilev@gmail.com>
Thu, 8 Sep 2016 20:34:41 +0000 (20:34 +0000)
In C mode, if we have a visible declaration but not a visible definition, a tag
defined in the declaration should be have a visible definition. In C++ we rely
on the ODR merging, whereas in C we cannot because each declaration of a
function gets its own set of declarations in its prototype scope.

Patch developed in collaboration with Richard Smith!

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

lib/Sema/SemaLookup.cpp
test/Modules/Inputs/merge-fn-prototype-tags/a.h [new file with mode: 0644]
test/Modules/Inputs/merge-fn-prototype-tags/b.h [new file with mode: 0644]
test/Modules/Inputs/merge-fn-prototype-tags/c.h [new file with mode: 0644]
test/Modules/Inputs/merge-fn-prototype-tags/module.modulemap [new file with mode: 0644]
test/Modules/merge-fn-prototype-tags.c [new file with mode: 0644]

index 19df1e304235ae1c783d86a65528bf809fbf74fa..40ad686321bd5a19331829de39b884410409aff1 100644 (file)
@@ -1543,7 +1543,12 @@ bool LookupResult::isVisibleSlow(Sema &SemaRef, NamedDecl *D) {
     // For a parameter, check whether our current template declaration's
     // lexical context is visible, not whether there's some other visible
     // definition of it, because parameters aren't "within" the definition.
-    if ((D->isTemplateParameter() || isa<ParmVarDecl>(D))
+    //
+    // In C++ we need to check for a visible definition due to ODR merging,
+    // and in C we must not because each declaration of a function gets its own
+    // set of declarations for tags in prototype scope.
+    if ((D->isTemplateParameter() || isa<ParmVarDecl>(D)
+         || (isa<FunctionDecl>(DC) && !SemaRef.getLangOpts().CPlusPlus))
             ? isVisible(SemaRef, cast<NamedDecl>(DC))
             : SemaRef.hasVisibleDefinition(cast<NamedDecl>(DC))) {
       if (SemaRef.ActiveTemplateInstantiations.empty() &&
diff --git a/test/Modules/Inputs/merge-fn-prototype-tags/a.h b/test/Modules/Inputs/merge-fn-prototype-tags/a.h
new file mode 100644 (file)
index 0000000..8dd717c
--- /dev/null
@@ -0,0 +1,2 @@
+#include "c.h"
+void ftw(struct stat);
diff --git a/test/Modules/Inputs/merge-fn-prototype-tags/b.h b/test/Modules/Inputs/merge-fn-prototype-tags/b.h
new file mode 100644 (file)
index 0000000..566381f
--- /dev/null
@@ -0,0 +1 @@
+struct stat;
diff --git a/test/Modules/Inputs/merge-fn-prototype-tags/c.h b/test/Modules/Inputs/merge-fn-prototype-tags/c.h
new file mode 100644 (file)
index 0000000..cb52624
--- /dev/null
@@ -0,0 +1,4 @@
+#ifndef _STAT_H_
+#define _STAT_H_
+struct stat {};
+#endif
diff --git a/test/Modules/Inputs/merge-fn-prototype-tags/module.modulemap b/test/Modules/Inputs/merge-fn-prototype-tags/module.modulemap
new file mode 100644 (file)
index 0000000..b324f05
--- /dev/null
@@ -0,0 +1,5 @@
+module M {
+  header "a.h"
+  module mod_c { header "c.h" }
+  header "b.h"
+}
diff --git a/test/Modules/merge-fn-prototype-tags.c b/test/Modules/merge-fn-prototype-tags.c
new file mode 100644 (file)
index 0000000..f43f1dc
--- /dev/null
@@ -0,0 +1,8 @@
+// RUN: rm -rf %t
+// RUN: %clang_cc1 -x c -I%S/Inputs/merge-fn-prototype-tags -verify %s
+// RUN: %clang_cc1 -fmodules -fmodule-map-file=%S/Inputs/merge-fn-prototype-tags/module.modulemap -fmodules-cache-path=%t -x c -I%S/Inputs/merge-fn-prototype-tags -verify %s
+
+#include "c.h"
+void mmalloc_attach() { struct stat sbuf; }
+
+// expected-no-diagnostics