Modules: Search for a visible definition of the decl context when computing visibilit...
authorDavid Blaikie <dblaikie@gmail.com>
Fri, 19 Apr 2019 23:02:30 +0000 (23:02 +0000)
committerDavid Blaikie <dblaikie@gmail.com>
Fri, 19 Apr 2019 23:02:30 +0000 (23:02 +0000)
The code is/was already correct for the case where a parameter is a
parameter of its enclosing lexical DeclContext (functions and classes).
But for other templates (alias and variable templates) they don't create
their own scope to be members of - in those cases, they parameter should
be considered visible if any definition of the lexical decl context is
visible.

[this should cleanup the failure on the libstdc++ modules buildbot]
[this doesn't actually fix the variable template case for a
secondary/compounding reason (its lexical decl context is incorrectly
considered to be the translation unit)]

Test covers all 4 kinds of templates with default args, including a
regression test for the still broken variable template case.

Reviewers: rsmith

Differential Revision: https://reviews.llvm.org/D60892

llvm-svn: 358795

15 files changed:
clang/lib/Sema/SemaLookup.cpp
clang/test/Modules/Inputs/nested-template-default-arg-redecl/alias.h [new file with mode: 0644]
clang/test/Modules/Inputs/nested-template-default-arg-redecl/alias1.h [new file with mode: 0644]
clang/test/Modules/Inputs/nested-template-default-arg-redecl/alias2.h [new file with mode: 0644]
clang/test/Modules/Inputs/nested-template-default-arg-redecl/func.h [new file with mode: 0644]
clang/test/Modules/Inputs/nested-template-default-arg-redecl/func1.h [new file with mode: 0644]
clang/test/Modules/Inputs/nested-template-default-arg-redecl/func2.h [new file with mode: 0644]
clang/test/Modules/Inputs/nested-template-default-arg-redecl/module.modulemap [new file with mode: 0644]
clang/test/Modules/Inputs/nested-template-default-arg-redecl/strct.h [new file with mode: 0644]
clang/test/Modules/Inputs/nested-template-default-arg-redecl/strct1.h [new file with mode: 0644]
clang/test/Modules/Inputs/nested-template-default-arg-redecl/strct2.h [new file with mode: 0644]
clang/test/Modules/Inputs/nested-template-default-arg-redecl/var.h [new file with mode: 0644]
clang/test/Modules/Inputs/nested-template-default-arg-redecl/var1.h [new file with mode: 0644]
clang/test/Modules/Inputs/nested-template-default-arg-redecl/var2.h [new file with mode: 0644]
clang/test/Modules/nested-template-default-arg-redecl.cpp [new file with mode: 0644]

index 874e561..c26fbac 100644 (file)
@@ -1543,8 +1543,21 @@ bool LookupResult::isVisibleSlow(Sema &SemaRef, NamedDecl *D) {
     // and in C we must not because each declaration of a function gets its own
     // set of declarations for tags in prototype scope.
     bool VisibleWithinParent;
-    if (D->isTemplateParameter() || isa<ParmVarDecl>(D) ||
-        (isa<FunctionDecl>(DC) && !SemaRef.getLangOpts().CPlusPlus))
+    if (D->isTemplateParameter()) {
+      bool SearchDefinitions = true;
+      if (const auto *DCD = dyn_cast<Decl>(DC)) {
+        if (const auto *TD = DCD->getDescribedTemplate()) {
+          TemplateParameterList *TPL = TD->getTemplateParameters();
+          auto Index = getDepthAndIndex(D).second;
+          SearchDefinitions = Index >= TPL->size() || TPL->getParam(Index) != D;
+        }
+      }
+      if (SearchDefinitions)
+        VisibleWithinParent = SemaRef.hasVisibleDefinition(cast<NamedDecl>(DC));
+      else
+        VisibleWithinParent = isVisible(SemaRef, cast<NamedDecl>(DC));
+    } else if (isa<ParmVarDecl>(D) ||
+               (isa<FunctionDecl>(DC) && !SemaRef.getLangOpts().CPlusPlus))
       VisibleWithinParent = isVisible(SemaRef, cast<NamedDecl>(DC));
     else if (D->isModulePrivate()) {
       // A module-private declaration is only visible if an enclosing lexical
diff --git a/clang/test/Modules/Inputs/nested-template-default-arg-redecl/alias.h b/clang/test/Modules/Inputs/nested-template-default-arg-redecl/alias.h
new file mode 100644 (file)
index 0000000..cff3329
--- /dev/null
@@ -0,0 +1,7 @@
+#ifndef ALIAS_H
+#define ALIAS_H
+struct alias_outer {
+  template <typename = int>
+  using alias = int;
+};
+#endif
diff --git a/clang/test/Modules/Inputs/nested-template-default-arg-redecl/alias1.h b/clang/test/Modules/Inputs/nested-template-default-arg-redecl/alias1.h
new file mode 100644 (file)
index 0000000..736abcb
--- /dev/null
@@ -0,0 +1 @@
+#include "alias.h"
diff --git a/clang/test/Modules/Inputs/nested-template-default-arg-redecl/alias2.h b/clang/test/Modules/Inputs/nested-template-default-arg-redecl/alias2.h
new file mode 100644 (file)
index 0000000..736abcb
--- /dev/null
@@ -0,0 +1 @@
+#include "alias.h"
diff --git a/clang/test/Modules/Inputs/nested-template-default-arg-redecl/func.h b/clang/test/Modules/Inputs/nested-template-default-arg-redecl/func.h
new file mode 100644 (file)
index 0000000..7a15c69
--- /dev/null
@@ -0,0 +1,7 @@
+#ifndef FUNC_H
+#define FUNC_H
+struct func_outer {
+  template <typename = int>
+  void func();
+};
+#endif
diff --git a/clang/test/Modules/Inputs/nested-template-default-arg-redecl/func1.h b/clang/test/Modules/Inputs/nested-template-default-arg-redecl/func1.h
new file mode 100644 (file)
index 0000000..940d767
--- /dev/null
@@ -0,0 +1 @@
+#include "func.h"
diff --git a/clang/test/Modules/Inputs/nested-template-default-arg-redecl/func2.h b/clang/test/Modules/Inputs/nested-template-default-arg-redecl/func2.h
new file mode 100644 (file)
index 0000000..940d767
--- /dev/null
@@ -0,0 +1 @@
+#include "func.h"
diff --git a/clang/test/Modules/Inputs/nested-template-default-arg-redecl/module.modulemap b/clang/test/Modules/Inputs/nested-template-default-arg-redecl/module.modulemap
new file mode 100644 (file)
index 0000000..a0071fd
--- /dev/null
@@ -0,0 +1,24 @@
+module ALIAS1 {
+  header "alias1.h"
+  module ALIAS2 {
+    header "alias2.h"
+  }
+}
+module VAR1 {
+  header "var1.h"
+  module VAR2 {
+    header "var2.h"
+  }
+}
+module FUNC1 {
+  header "func1.h"
+  module FUNC2 {
+    header "func2.h"
+  }
+}
+module STRCT1 {
+  header "strct1.h"
+  module STRCT2 {
+    header "strct2.h"
+  }
+}
diff --git a/clang/test/Modules/Inputs/nested-template-default-arg-redecl/strct.h b/clang/test/Modules/Inputs/nested-template-default-arg-redecl/strct.h
new file mode 100644 (file)
index 0000000..04d1244
--- /dev/null
@@ -0,0 +1,7 @@
+#ifndef STRCT_H
+#define STRCT_H
+struct strct_outer {
+  template <typename = int>
+  struct strct;
+};
+#endif
diff --git a/clang/test/Modules/Inputs/nested-template-default-arg-redecl/strct1.h b/clang/test/Modules/Inputs/nested-template-default-arg-redecl/strct1.h
new file mode 100644 (file)
index 0000000..5c29b94
--- /dev/null
@@ -0,0 +1 @@
+#include "strct.h"
diff --git a/clang/test/Modules/Inputs/nested-template-default-arg-redecl/strct2.h b/clang/test/Modules/Inputs/nested-template-default-arg-redecl/strct2.h
new file mode 100644 (file)
index 0000000..5c29b94
--- /dev/null
@@ -0,0 +1 @@
+#include "strct.h"
diff --git a/clang/test/Modules/Inputs/nested-template-default-arg-redecl/var.h b/clang/test/Modules/Inputs/nested-template-default-arg-redecl/var.h
new file mode 100644 (file)
index 0000000..3c3584d
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef VAR_H
+#define VAR_H
+struct var_outer {
+  template <typename = int>
+  static int var;
+};
+#endif
+
+
diff --git a/clang/test/Modules/Inputs/nested-template-default-arg-redecl/var1.h b/clang/test/Modules/Inputs/nested-template-default-arg-redecl/var1.h
new file mode 100644 (file)
index 0000000..89cee81
--- /dev/null
@@ -0,0 +1 @@
+#include "var.h"
diff --git a/clang/test/Modules/Inputs/nested-template-default-arg-redecl/var2.h b/clang/test/Modules/Inputs/nested-template-default-arg-redecl/var2.h
new file mode 100644 (file)
index 0000000..89cee81
--- /dev/null
@@ -0,0 +1 @@
+#include "var.h"
diff --git a/clang/test/Modules/nested-template-default-arg-redecl.cpp b/clang/test/Modules/nested-template-default-arg-redecl.cpp
new file mode 100644 (file)
index 0000000..3baa202
--- /dev/null
@@ -0,0 +1,18 @@
+// RUN: rm -rf %t
+// RUN: %clang_cc1 -x c++ -fmodules -fimplicit-module-maps -fmodules-cache-path=%t \
+// RUN:     -I %S/Inputs/nested-template-default-arg-redecl -std=c++14 \
+// RUN:     -fmodules-local-submodule-visibility -verify %s
+#include "alias2.h"
+#include "var2.h"
+#include "strct2.h"
+#include "func2.h"
+
+// FIXME: Variable templates lexical decl context appears to be the translation
+// unit, which is incorrect. Fixing this will hopefully address the following
+// error/bug:
+
+// expected-note@Inputs/nested-template-default-arg-redecl/var.h:4 {{default argument declared here}}
+auto var = &var_outer::var<>; // expected-error {{default argument of 'var' must be imported from module 'VAR1' before it is required}}
+auto func = &func_outer::func<>;
+strct_outer::strct<> *strct;
+alias_outer::alias<> *alias;