r280984 - [modules] Apply ODR merging for function scoped tags only in C++ mode.

Vassil Vassilev via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 8 13:34:41 PDT 2016


Author: vvassilev
Date: Thu Sep  8 15:34:41 2016
New Revision: 280984

URL: http://llvm.org/viewvc/llvm-project?rev=280984&view=rev
Log:
[modules] Apply ODR merging for function scoped tags only in C++ mode.

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!

Added:
    cfe/trunk/test/Modules/Inputs/merge-fn-prototype-tags/
    cfe/trunk/test/Modules/Inputs/merge-fn-prototype-tags/a.h
    cfe/trunk/test/Modules/Inputs/merge-fn-prototype-tags/b.h
    cfe/trunk/test/Modules/Inputs/merge-fn-prototype-tags/c.h
    cfe/trunk/test/Modules/Inputs/merge-fn-prototype-tags/module.modulemap
    cfe/trunk/test/Modules/merge-fn-prototype-tags.c
Modified:
    cfe/trunk/lib/Sema/SemaLookup.cpp

Modified: cfe/trunk/lib/Sema/SemaLookup.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaLookup.cpp?rev=280984&r1=280983&r2=280984&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaLookup.cpp (original)
+++ cfe/trunk/lib/Sema/SemaLookup.cpp Thu Sep  8 15:34:41 2016
@@ -1543,7 +1543,12 @@ bool LookupResult::isVisibleSlow(Sema &S
     // 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() &&

Added: cfe/trunk/test/Modules/Inputs/merge-fn-prototype-tags/a.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/merge-fn-prototype-tags/a.h?rev=280984&view=auto
==============================================================================
--- cfe/trunk/test/Modules/Inputs/merge-fn-prototype-tags/a.h (added)
+++ cfe/trunk/test/Modules/Inputs/merge-fn-prototype-tags/a.h Thu Sep  8 15:34:41 2016
@@ -0,0 +1,2 @@
+#include "c.h"
+void ftw(struct stat);

Added: cfe/trunk/test/Modules/Inputs/merge-fn-prototype-tags/b.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/merge-fn-prototype-tags/b.h?rev=280984&view=auto
==============================================================================
--- cfe/trunk/test/Modules/Inputs/merge-fn-prototype-tags/b.h (added)
+++ cfe/trunk/test/Modules/Inputs/merge-fn-prototype-tags/b.h Thu Sep  8 15:34:41 2016
@@ -0,0 +1 @@
+struct stat;

Added: cfe/trunk/test/Modules/Inputs/merge-fn-prototype-tags/c.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/merge-fn-prototype-tags/c.h?rev=280984&view=auto
==============================================================================
--- cfe/trunk/test/Modules/Inputs/merge-fn-prototype-tags/c.h (added)
+++ cfe/trunk/test/Modules/Inputs/merge-fn-prototype-tags/c.h Thu Sep  8 15:34:41 2016
@@ -0,0 +1,4 @@
+#ifndef _STAT_H_
+#define _STAT_H_
+struct stat {};
+#endif

Added: cfe/trunk/test/Modules/Inputs/merge-fn-prototype-tags/module.modulemap
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/merge-fn-prototype-tags/module.modulemap?rev=280984&view=auto
==============================================================================
--- cfe/trunk/test/Modules/Inputs/merge-fn-prototype-tags/module.modulemap (added)
+++ cfe/trunk/test/Modules/Inputs/merge-fn-prototype-tags/module.modulemap Thu Sep  8 15:34:41 2016
@@ -0,0 +1,5 @@
+module M {
+  header "a.h"
+  module mod_c { header "c.h" }
+  header "b.h"
+}

Added: cfe/trunk/test/Modules/merge-fn-prototype-tags.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/merge-fn-prototype-tags.c?rev=280984&view=auto
==============================================================================
--- cfe/trunk/test/Modules/merge-fn-prototype-tags.c (added)
+++ cfe/trunk/test/Modules/merge-fn-prototype-tags.c Thu Sep  8 15:34:41 2016
@@ -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




More information about the cfe-commits mailing list