1 // PR c++/66561 - __builtin_LINE at al. should yield constant expressions
2 // { dg-do compile { target c++11 } }
3 #define A(expr) static_assert ((expr), #expr)
4
5 #define FILE_1 "file_name.suffix"
6 #define FILE_2 "some_other_file_name.suffix"
7
8 #line 1 FILE_1
9 constexpr const char*
file1()10 file1 ()
11 {
12 #if __cplusplus >= 201402L
13 // Do extra checking in C++ 14 and later.
14 constexpr const char *f1 = __FILE__;
15 constexpr const char *f2 = __builtin_FILE ();
16 A (0 == __builtin_strcmp (f1, f2));
17 return f1;
18 #else
19 // In C++ 11, a constexpr function body must consist of a single
20 // return statement and no declaratations.
21 return __builtin_FILE ();
22 #endif
23 }
24
25 #line 1 FILE_2
26 constexpr const char*
file2()27 file2 ()
28 {
29 #if __cplusplus >= 201402L
30 constexpr const char *f1 = __FILE__;
31 constexpr const char *f2 = __builtin_FILE ();
32 A (0 == __builtin_strcmp (f1, f2));
33 return f1;
34 #else
35 return __builtin_FILE ();
36 #endif
37 }
38
39 #line 1 "bogus file name"
40 constexpr const char*
41 this_file (const char *fname = __builtin_FILE ())
42 {
43 return fname;
44 }
45
46 constexpr const char*
function()47 function ()
48 {
49 #if __cplusplus >= 201402L
50 constexpr const char *f1 = __FUNCTION__;
51 constexpr const char *f2 = __builtin_FUNCTION ();
52 A (0 == __builtin_strcmp (f1, f2));
53 return f1;
54 #else
55 return __builtin_FUNCTION ();
56 #endif
57 }
58
59 constexpr const char*
60 this_function (const char *func = __builtin_FUNCTION ())
61 {
62 return func;
63 }
64
65 constexpr int
line()66 line ()
67 {
68 #if __cplusplus >= 201402L
69 #line 123
70 constexpr int n1 = __LINE__;
71 constexpr int n2 = __builtin_LINE ();
72 A (123 == n1);
73 A (n1 + 1 == n2);
74 return n2;
75 #else
76 #line 123
77 // Newline.
78 return __builtin_LINE ();
79 #endif
80 }
81
82 constexpr int
83 this_line (int line = __builtin_LINE ())
84 {
85 return line;
86 }
87
88
89 // Exercise __builtin_FILE().
90 #line 1 "foobar"
91 constexpr const char* f1 = file1 ();
92 A (0 == __builtin_strcmp (f1, FILE_1));
93
94 #line 2 "foobar"
95 constexpr const char* f2 = file2 ();
96 A (0 == __builtin_strcmp (f2, FILE_2));
97
98 #define FILE_3 "this_file_name_right_here.this_suffix"
99 #line 1 FILE_3
100 constexpr const char* f3 = this_file ();
101 A (0 == __builtin_strcmp (f3, FILE_3));
102
103 #define FILE_4 "next_file_name.another_suffix"
104 #line 1 "foobar"
105 constexpr const char* f4 = this_file
106 (
107 #line 1 FILE_4
108 )
109 #line 1 "foobar"
110 ;
111 A (0 == __builtin_strcmp (f4, FILE_4));
112
113
114 // Exercise __builtin_FUNCTION().
115
116 // Verify that __builtin_FUNCTION() returns the name of the function
117 // in which it is called.
118 constexpr const char* fun1 = function ();
119 A (0 == __builtin_strcmp (fun1, "function"));
120
121 // Verify that __builtin_FUNCTION() returns the empty string when
122 // it's invoked to set the default argument value in a function
123 // called at file scope.
124 constexpr const char* fun2 = this_function ();
125 A (0 == __builtin_strcmp (fun2, ""));
126
127 constexpr const char*
named_function()128 named_function ()
129 {
130 return this_function ();
131 }
132
133 constexpr const char* fun3 = named_function ();
134 A (0 == __builtin_strcmp (fun3, "named_function"));
135
136
137 // Exercise __builtin_LINE().
138 // Verify the line numbe returned by the built-in.
139 #line 4
140 constexpr int n1 = __builtin_LINE ();
141 A (n1 == 4);
142
143 // Verify the line number obtained by a constexpr function.
144 #line 5
145 constexpr int n2 = line ();
146 A (n2 == 124);
147
148 // Verify the line number determined by the default argument.
149 #line 6
150 constexpr int n3 = this_line ();
151 A (n3 == 6);
152
153 // Verify that the line number accounts for each of the calls.
154 #line 7
155 constexpr int n4 = this_line () + this_line ();
156 A (n4 == 14);
157
158 // Verify that the line number accounts for each of the calls when
159 // split over multiple lines.
160 #line 1
161 constexpr int n5 = this_line ()
162 #line 8
163 + this_line ();
164 A (n5 == 9);
165
166 // Verify that the line number corresponds to the closing parenthesis
167 // of the function call.
168 #line 1
169 constexpr int n6 = this_line
170 (
171 #line 99
172 )
173 #line 1
174 ;
175 A (n6 == 99);
176