Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify -std=c++1y %s |
| 2 | |
| 3 | // expected-no-diagnostics |
John McCall | b65e8fe | 2013-04-01 18:34:28 +0000 | [diff] [blame] | 4 | |
| 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 Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 14 | extern int same_entity; |
| 15 | constexpr int *get1() { |
| 16 | int same_entity = 0; // not the same entity |
| 17 | { |
| 18 | extern int same_entity; |
| 19 | return &same_entity; |
John McCall | b65e8fe | 2013-04-01 18:34:28 +0000 | [diff] [blame] | 20 | } |
| 21 | } |
Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 22 | static_assert(get1() == &same_entity, "failed to find previous decl"); |
John McCall | b65e8fe | 2013-04-01 18:34:28 +0000 | [diff] [blame] | 23 | |
Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 24 | static int same_entity_2[3]; |
| 25 | constexpr 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 McCall | b65e8fe | 2013-04-01 18:34:28 +0000 | [diff] [blame] | 30 | } |
Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 31 | static_assert(get2() == same_entity_2, "failed to find previous decl"); |
John McCall | b65e8fe | 2013-04-01 18:34:28 +0000 | [diff] [blame] | 32 | |
Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 33 | static int different_entities; |
| 34 | constexpr 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 McCall | b65e8fe | 2013-04-01 18:34:28 +0000 | [diff] [blame] | 42 | } |
| 43 | } |
Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 44 | static_assert(get3() == &different_entities, "failed to find previous decl"); |