blob: ac6dc2f1f1a394dd2e8c867bd6cbc9ebed3e75dc [file] [log] [blame]
Richard Smith1c34fb72013-08-13 18:18:50 +00001// RUN: %clang_cc1 -fsyntax-only -verify -std=c++1y %s
2
3// expected-no-diagnostics
John McCallb65e8fe2013-04-01 18:34:28 +00004
5// C++11 [basic.link]p6:
6// The name of a function declared in block scope and the name
7// of a variable declared by a block scope extern declaration
8// have linkage. If there is a visible declaration of an entity
9// with linkage having the same name and type, ignoring entities
10// declared outside the innermost enclosing namespace scope, the
11// block scope declaration declares that same entity and
12// receives the linkage of the previous declaration.
13
Richard Smith1c34fb72013-08-13 18:18:50 +000014extern int same_entity;
15constexpr int *get1() {
16 int same_entity = 0; // not the same entity
17 {
18 extern int same_entity;
19 return &same_entity;
John McCallb65e8fe2013-04-01 18:34:28 +000020 }
21}
Richard Smith1c34fb72013-08-13 18:18:50 +000022static_assert(get1() == &same_entity, "failed to find previous decl");
John McCallb65e8fe2013-04-01 18:34:28 +000023
Richard Smith1c34fb72013-08-13 18:18:50 +000024static int same_entity_2[3];
25constexpr int *get2() {
26 // This is a redeclaration of the same entity, even though it doesn't
27 // inherit the type of the prior declaration.
28 extern int same_entity_2[];
29 return same_entity_2;
John McCallb65e8fe2013-04-01 18:34:28 +000030}
Richard Smith1c34fb72013-08-13 18:18:50 +000031static_assert(get2() == same_entity_2, "failed to find previous decl");
John McCallb65e8fe2013-04-01 18:34:28 +000032
Richard Smith1c34fb72013-08-13 18:18:50 +000033static int different_entities;
34constexpr int *get3() {
35 int different_entities = 0;
36 {
37 // FIXME: This is not a redeclaration of the prior entity, because
38 // it is not visible here. Under DR426, this is ill-formed, and without
39 // it, the static_assert below should fail.
40 extern int different_entities;
41 return &different_entities;
John McCallb65e8fe2013-04-01 18:34:28 +000042 }
43}
Richard Smith1c34fb72013-08-13 18:18:50 +000044static_assert(get3() == &different_entities, "failed to find previous decl");