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