Debian Codegraph @

gcc/testsuite/g++.dg/modules/internal-4_b.C

raw
1 // { dg-additional-options "-fmodules-ts" }
2 // { dg-module-cmi !bad }
3 // Test for determining various kinds of entities being marked TU-local
4
5 export module bad;
6 import "internal-4_a.H";
7
8
9 // A type, function variable, or template with internal linkage
10 namespace {
11 struct internal_t { int m; };
12 enum internal_e {};
13 void internal_f() {}
14 int internal_v;
15 template <typename T> void internal_x() {}
16 namespace internal_ns {}
17 }
18
19 inline void expose_type() { // { dg-error "exposes TU-local entity" }
20 internal_t x;
21 }
22 inline void expose_func() { // { dg-error "exposes TU-local entity" }
23 internal_f();
24 }
25 inline void expose_var() { // { dg-error "exposes TU-local entity" }
26 int* p = &internal_v;
27 }
28 inline void expose_tmpl() { // { dg-error "exposes TU-local entity" }
29 internal_x<int>();
30 }
31 inline void expose_header_decl() { // { dg-error "exposes TU-local entity" }
32 header_f();
33 }
34
35 // A namespace with internal linkage is not inherently an exposure.
36 namespace expose_ns = internal_ns; // { dg-bogus "exposes TU-local entity" }
37
38 // But we don't consider a weakref as being TU-local, despite being
39 // marked static; this is to support uses of weakrefs in header files
40 // (such as via the standard library).
41 static void weakref() __attribute__((weakref("target")));
42 inline void expose_weakref() {
43 weakref();
44 }
45
46
47 // Does not have a name with linkage and is declared, or introduced by
48 // a lambda-expression, within the definition of a TU-local entity
49 static auto get_local_ok() {
50 return 0;
51 }
52 static auto get_local_type() {
53 struct no_linkage {};
54 return no_linkage();
55 }
56 static auto get_local_lambda() {
57 return []{};
58 }
59 using T = decltype(get_local_ok());
60 T* expose_t; // OK
61 using U = decltype(get_local_type());
62 U* expose_u; // { dg-error "exposes TU-local entity" }
63 using V = decltype(get_local_lambda());
64 V* expose_v; // { dg-error "exposes TU-local entity" }
65
66 static auto internal_lambda = []{ internal_f(); }; // OK
67 auto expose_lambda = internal_lambda; // { dg-error "exposes TU-local entity" }
68
69 int not_in_tu_local
70 = ([]{ internal_f(); }(), // { dg-error "exposes TU-local entity" }
71 0);
72
73
74 // A type with no name that is defined outside a class-specifier, function
75 // body, or initializer
76
77 struct {} no_name; // { dg-error "exposes TU-local entity" }
78 enum {} e; // { dg-error "exposes TU-local entity" }
79 using not_an_initializer = class {};
80 not_an_initializer expose_nai; // { dg-error "exposes TU-local entity" }
81
82 class in_class_specifier { struct {} x; }; // OK
83 void in_function_body() { struct {} x; } // OK
84 auto in_initializer = []{}; // OK
85
86 #if __cplusplus >= 202002L
87 decltype([]{}) d_lambda; // { dg-error "exposes TU-local entity" "" { target c++20 } }
88 using alias_lambda = decltype([]{});
89 alias_lambda expose_lam; // { dg-error "exposes TU-local entity" "" { target c++20 } }
90
91 template <typename T>
92 concept in_constraint_expression = requires {
93 // Strictly by the standard this is currently ill-formed
94 // (this is a constraint-expression not an initializer)
95 // but I don't think that is intended; see CWG2988.
96 []{}; // { dg-bogus "exposes TU-local entity" }
97 };
98 #endif
99
100 // (But consider unnamed types with names for linkage purposes as having names)
101 typedef struct {} no_name_typedef_t;
102 no_name_typedef_t linkage_name_struct; // OK
103
104 enum { enum_name } linkage_name_enum; // OK
105
106
107 // Specialisation of a TU-local template
108 template <typename T> static void f(T) {}
109 template <> void f(int) {} // OK
110 inline void f_use(int x) { // { dg-error "exposes TU-local entity" }
111 f(x);
112 }
113
114
115 // Specialisation of a template with any TU-local argument
116 template <typename T> void g(T) {}
117 template <> void g(internal_t) { internal_f(); } // OK
118 template <> void g(internal_e) { internal_f(); } // OK
119 template <> void g(decltype(no_name)) { internal_f(); } // OK
120 template <> void g(decltype(get_local_lambda())) { internal_f(); } // OK
121
122 template <auto X> struct h {};
123 template struct h<&internal_v>;
124 template <> struct h<&internal_f> { internal_t x; }; // OK
125 template <> struct h<&internal_t::m> { void foo() { internal_f(); } }; // OK
126
127
128 // TODO: I can't come up with testcases for these that aren't already covered
129 // by one of the above cases:
130 //
131 // - A type with no name introduced by a defining-type-specifier that is
132 // used to declare only TU-local entities
133 // - A specialisation of a template whose (possibly instantiated) declaration
134 // is an exposure