xref: /minix3/external/bsd/llvm/dist/clang/test/CXX/basic/basic.link/p6.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify -std=c++1y %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc // expected-no-diagnostics
4*f4a2713aSLionel Sambuc 
5*f4a2713aSLionel Sambuc // C++11 [basic.link]p6:
6*f4a2713aSLionel Sambuc //   The name of a function declared in block scope and the name
7*f4a2713aSLionel Sambuc //   of a variable declared by a block scope extern declaration
8*f4a2713aSLionel Sambuc //   have linkage. If there is a visible declaration of an entity
9*f4a2713aSLionel Sambuc //   with linkage having the same name and type, ignoring entities
10*f4a2713aSLionel Sambuc //   declared outside the innermost enclosing namespace scope, the
11*f4a2713aSLionel Sambuc //   block scope declaration declares that same entity and
12*f4a2713aSLionel Sambuc //   receives the linkage of the previous declaration.
13*f4a2713aSLionel Sambuc 
14*f4a2713aSLionel Sambuc extern int same_entity;
get1()15*f4a2713aSLionel Sambuc constexpr int *get1() {
16*f4a2713aSLionel Sambuc   int same_entity = 0; // not the same entity
17*f4a2713aSLionel Sambuc   {
18*f4a2713aSLionel Sambuc     extern int same_entity;
19*f4a2713aSLionel Sambuc     return &same_entity;
20*f4a2713aSLionel Sambuc   }
21*f4a2713aSLionel Sambuc }
22*f4a2713aSLionel Sambuc static_assert(get1() == &same_entity, "failed to find previous decl");
23*f4a2713aSLionel Sambuc 
24*f4a2713aSLionel Sambuc static int same_entity_2[3];
get2()25*f4a2713aSLionel Sambuc constexpr int *get2() {
26*f4a2713aSLionel Sambuc   // This is a redeclaration of the same entity, even though it doesn't
27*f4a2713aSLionel Sambuc   // inherit the type of the prior declaration.
28*f4a2713aSLionel Sambuc   extern int same_entity_2[];
29*f4a2713aSLionel Sambuc   return same_entity_2;
30*f4a2713aSLionel Sambuc }
31*f4a2713aSLionel Sambuc static_assert(get2() == same_entity_2, "failed to find previous decl");
32*f4a2713aSLionel Sambuc 
33*f4a2713aSLionel Sambuc static int different_entities;
get3()34*f4a2713aSLionel Sambuc constexpr int *get3() {
35*f4a2713aSLionel Sambuc   int different_entities = 0;
36*f4a2713aSLionel Sambuc   {
37*f4a2713aSLionel Sambuc     // FIXME: This is not a redeclaration of the prior entity, because
38*f4a2713aSLionel Sambuc     // it is not visible here. Under DR426, this is ill-formed, and without
39*f4a2713aSLionel Sambuc     // it, the static_assert below should fail.
40*f4a2713aSLionel Sambuc     extern int different_entities;
41*f4a2713aSLionel Sambuc     return &different_entities;
42*f4a2713aSLionel Sambuc   }
43*f4a2713aSLionel Sambuc }
44*f4a2713aSLionel Sambuc static_assert(get3() == &different_entities, "failed to find previous decl");
45