Debian Codegraph @

gcc/cobol/show_parse.h

raw
1 /*
2 * Copyright (c) 2021-2025 Symas Corporation
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of the Symas Corporation nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 #ifndef SHOW_PARSE_H_
31 #define SHOW_PARSE_H_
32
33 // These macros provide information about what the compiler is doing,
34 // and about what the compiled code is doing.
35
36 // SHOW_PARSE gives information when parser_xxx functions are entered, and
37 // then attempts to give as much information as it can at compile time about
38 // variables and their characteristics, the contents of literals, and such. It
39 // doesn't affect the executable at all.
40
41 // TRACE1 lays down code for run-time tracing.
42
43 // SHOW_PARSE must be followed by a bracketed set of instructions, no semicolon
44
45 extern bool bSHOW_PARSE;
46 extern bool show_parse_sol;
47 extern int show_parse_indent;
48
49 extern char const *bTRACE1;
50 extern tree trace_handle;
51 extern tree trace_indent;
52 extern bool cursor_at_sol;
53
54 #pragma GCC diagnostic push
55 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
56
57 /*
58 * In syntax-only mode, return immediately. By using these macros, the parser
59 * can call code-generation functions unconditionally because it does not rely
60 * on the results.
61 */
62 #define RETURN_IF_PARSE_ONLY \
63 do { if( mode_syntax_only() ) return; } while(0)
64
65 #define RETURN_XX_PARSE_ONLY(XX) \
66 do { if( mode_syntax_only() ) return XX; } while(0)
67
68 #define SHOW_PARSE1 if(bSHOW_PARSE)
69 #define SHOW_PARSE RETURN_IF_PARSE_ONLY; if(bSHOW_PARSE)
70 #define SHOW_IF_PARSE(XX) RETURN_XX_PARSE_ONLY((XX)); if(bSHOW_PARSE)
71
72 // _HEADER and _END are generally the first and last things inside the
73 // SHOW_PARSE statement. They don't have to be; SHOW_PARSE can be used
74 // anywhere
75 #define SHOW_PARSE_HEADER do \
76 { \
77 if(!show_parse_sol){fprintf(stderr, "\n");} \
78 show_parse_indent=fprintf(stderr, \
79 "( %d ) %s():" , \
80 (CURRENT_LINE_NUMBER), __func__); \
81 show_parse_sol=false; \
82 }while(0);
83 #define SHOW_PARSE_END do{fprintf(stderr, "\n");show_parse_sol=true;}while(0);
84
85 // This does one simple text string
86 #define SHOW_PARSE_TEXT(a) do \
87 { \
88 fprintf(stderr, "%s", a); \
89 show_parse_sol=false; \
90 }while(0);
91
92 #define SHOW_PARSE_INDENT do{ \
93 if(!show_parse_sol){fprintf(stderr, "\n");} \
94 for(int i=0; i<show_parse_indent-1; i++) \
95 {fprintf(stderr, " ");} \
96 fprintf(stderr, ": "); \
97 show_parse_sol=false; \
98 }while(0);
99
100 // This does three simple text strings.
101 #define SHOW_PARSE_TEXT_AB(pre, a, post) do \
102 { \
103 SHOW_PARSE_TEXT(pre);SHOW_PARSE_TEXT(a);SHOW_PARSE_TEXT(post) \
104 }while(0);
105
106 //
107 #define SHOW_PARSE_FIELD(pre, b) \
108 do \
109 { \
110 fprintf(stderr, "%s", pre); \
111 if( !(b) ) \
112 { \
113 fprintf(stderr, "parameter " #b " is NULL"); \
114 } \
115 else \
116 { \
117 fprintf(stderr, "%s", (b)->name); \
118 if( (b)->type == FldLiteralA || (b)->type == FldLiteralN ) \
119 { \
120 fprintf(stderr, " \"%s\"", (b)->data.initial); \
121 } \
122 else \
123 { \
124 fprintf(stderr, "<%s>", cbl_field_type_str((b)->type)); \
125 } \
126 } \
127 show_parse_sol = false; \
128 } while(0);
129
130 #define SHOW_PARSE_REF(pre, b) \
131 do \
132 { \
133 fprintf(stderr, "%s", pre); \
134 if( !(b).field ) \
135 { \
136 fprintf(stderr, "parameter " #b".field is NULL"); \
137 } \
138 else \
139 { \
140 fprintf(stderr, "%s", (b).field->name); \
141 if( (b).field->type == FldLiteralA || (b).field->type == FldLiteralN ) \
142 { \
143 fprintf(stderr, " \"%s\"", (b).field->data.initial); \
144 } \
145 else \
146 { \
147 fprintf(stderr, "<%s>", cbl_field_type_str((b).field->type)); \
148 } \
149 } \
150 if( (b).nsubscript()) \
151 { \
152 fprintf(stderr,"("); \
153 for(size_t jjj=0; jjj<(b).nsubscript(); jjj++) \
154 { \
155 if(jjj) \
156 { \
157 SHOW_PARSE_FIELD(" ", (b).subscripts[jjj].field) \
158 } \
159 else \
160 { \
161 SHOW_PARSE_FIELD("", (b).subscripts[jjj].field) \
162 } \
163 } \
164 fprintf(stderr,")"); \
165 } \
166 show_parse_sol = false; \
167 } while(0);
168
169 #define SHOW_PARSE_LABEL(a, b) \
170 do \
171 { \
172 fprintf(stderr, "%s", a); \
173 if( !b ) \
174 { \
175 fprintf(stderr, "label " #b " is NULL"); \
176 } \
177 else \
178 { \
179 fprintf(stderr, " %p:%s (%s)", static_cast<void*>(b), b->name, b->type_str()); \
180 } \
181 show_parse_sol = false; \
182 } while(0);
183
184 // Use this version when b is known to be valid. This is necessary to quiet
185 // cppcheck nullPointerRedundantCheck warnings
186 #define SHOW_PARSE_LABEL_OK(a, b) \
187 do \
188 { \
189 fprintf(stderr, "%s", a); \
190 fprintf(stderr, " %p:%s (%s)", static_cast<void*>(b), b->name, b->type_str()); \
191 show_parse_sol = false; \
192 } while(0);
193
194 #define TRACE1 if(bTRACE1)
195 #define TRACE1_HEADER do \
196 { \
197 if(!cursor_at_sol){gg_fprintf(trace_handle , 0, "\n");} \
198 gg_assign(trace_indent, \
199 gg_fprintf( trace_handle , \
200 2, \
201 ">>>>>>( %d )(%s) ", \
202 build_int_cst_type(INT, CURRENT_LINE_NUMBER), \
203 gg_string_literal(__func__))); \
204 }while(0);
205
206 #define TRACE1_INDENT do{ \
207 if(!cursor_at_sol){gg_fprintf(trace_handle , 0, "\n");} \
208 tree counter = gg_define_int(); \
209 gg_assign(counter, integer_zero_node); \
210 WHILE(counter, lt_op, trace_indent) \
211 gg_fprintf(trace_handle , 0, " "); \
212 gg_increment(counter); \
213 WEND \
214 }while(0);
215
216 #define TRACE1_END do{gg_fprintf(trace_handle, 0, "\n");cursor_at_sol=true;}while(0);
217
218 #define TRACE1_TEXT(a) do{cursor_at_sol=false;gg_fprintf(trace_handle, 1, "%s", gg_string_literal(a));}while(0);
219 #define TRACE1_TEXT_ABC(a,b,c) do{TRACE1_TEXT(a);TRACE1_TEXT(b);TRACE1_TEXT(c)}while(0);
220
221 #define TRACE1_FIELD_VALUE(a, field, b) \
222 do \
223 { \
224 gcc_assert(field); \
225 cursor_at_sol=false; \
226 if ( field->type == FldConditional ) \
227 { \
228 gg_fprintf(trace_handle, 1, "%s \"", gg_string_literal(a)); \
229 parser_display_internal_field(trace_handle, field, false); \
230 gg_fprintf(trace_handle, 1, "\" %s", gg_string_literal(b)); \
231 } \
232 else \
233 { \
234 IF( member(field->var_decl_node, "data"), eq_op, gg_cast(UCHAR_P, null_pointer_node) ) \
235 { \
236 gg_fprintf(trace_handle, 1, "%s ", gg_string_literal(a)); \
237 gg_fprintf(trace_handle, 0, "NULL"); \
238 gg_fprintf(trace_handle, 1, " %s", gg_string_literal(b)); \
239 } \
240 ELSE \
241 { \
242 if( field->type == FldGroup \
243 || field->type == FldAlphanumeric \
244 || field->type == FldAlphaEdited \
245 || field->type == FldLiteralA ) \
246 { \
247 gg_fprintf(trace_handle, 1, "%s \"", gg_string_literal(a)); \
248 parser_display_internal_field(trace_handle, field, false); \
249 gg_fprintf(trace_handle, 1, "\" %s", gg_string_literal(b)); \
250 } \
251 else \
252 { \
253 gg_fprintf(trace_handle, 1, "%s [", gg_string_literal(a)); \
254 parser_display_internal_field(trace_handle, field, false); \
255 gg_fprintf(trace_handle, 1, "] %s", gg_string_literal(b)); \
256 } \
257 } \
258 ENDIF \
259 } \
260 }while(0);
261
262 #define TRACE1_REFER_VALUE(a, refer, b) \
263 do \
264 { \
265 if( refer.field ) \
266 { \
267 cursor_at_sol=false; \
268 IF( member(refer.field->var_decl_node, "data"), eq_op, gg_cast(UCHAR_P, null_pointer_node) ) \
269 { \
270 gg_fprintf(trace_handle, 1, "%s ", gg_string_literal(a)); \
271 gg_fprintf(trace_handle, 0, "NULL"); \
272 gg_fprintf(trace_handle, 1, " %s", gg_string_literal(b)); \
273 } \
274 ELSE \
275 { \
276 if( refer.field->type == FldGroup \
277 || refer.field->type == FldAlphanumeric \
278 || refer.field->type == FldAlphaEdited \
279 || refer.field->type == FldLiteralA ) \
280 { \
281 gg_fprintf(trace_handle, 1, "%s \"", gg_string_literal(a)); \
282 parser_display_internal(trace_handle, refer, false); \
283 gg_fprintf(trace_handle, 1, "\" %s", gg_string_literal(b)); \
284 } \
285 else \
286 { \
287 gg_fprintf(trace_handle, 1, "%s [", gg_string_literal(a)); \
288 parser_display_internal(trace_handle, refer, false); \
289 gg_fprintf(trace_handle, 1, "] %s", gg_string_literal(b)); \
290 } \
291 } \
292 ENDIF \
293 } \
294 else \
295 { \
296 gg_fprintf(trace_handle, 0, "refer.field is NULL"); \
297 } \
298 }while(0);
299
300 #define TRACE1_FIELD_INFO(pre, b) \
301 do{ \
302 cursor_at_sol=false; \
303 gg_fprintf(trace_handle, 1, "%s", gg_string_literal(pre)); \
304 if( !b ) \
305 { \
306 gg_fprintf(trace_handle, 0, "field " #b " is NULL"); \
307 } \
308 else \
309 { \
310 gg_fprintf(trace_handle, 1, "%s", gg_string_literal(b->name)); \
311 gg_fprintf(trace_handle, 1, " (%s", gg_string_literal(cbl_field_type_str((b)->type))); \
312 if( b->type != FldLiteralN && b->type != FldConditional ) \
313 { \
314 cbl_field_t* B(b); \
315 if( !b->var_decl_node ) \
316 { \
317 gg_fprintf(trace_handle, 0, #b "->var_decl_node is NULL", NULL_TREE); \
318 } \
319 else \
320 { \
321 gg_fprintf(trace_handle, 1, " attr 0x%lx", member(B, "attr" )); \
322 gg_fprintf(trace_handle, 1, " c:o:d:r %ld", member(B, "capacity")); \
323 gg_fprintf(trace_handle, 1, ":%ld", member(B, "offset" )); \
324 gg_fprintf(trace_handle, 1, ":%d", gg_cast(INT, (member(B, "digits" )))); \
325 gg_fprintf(trace_handle, 1, ":%d", gg_cast(INT, (member(B, "rdigits" )))); \
326 } \
327 } \
328 else if( b->type == FldLiteralN ) \
329 { \
330 gg_fprintf(trace_handle, 1, " attr 0x%lx", build_int_cst_type(SIZE_T, b->attr)); \
331 gg_fprintf(trace_handle, 1, " c:o:d:r %ld", build_int_cst_type(SIZE_T, b->data.capacity)); \
332 gg_fprintf(trace_handle, 1, ":%ld", build_int_cst_type(SIZE_T, b->offset)); \
333 gg_fprintf(trace_handle, 1, ":%d", build_int_cst_type(INT, b->data.digits)); \
334 gg_fprintf(trace_handle, 1, ":%d", build_int_cst_type(INT, b->data.rdigits)); \
335 } \
336 gg_fprintf(trace_handle, 0, ")"); \
337 } \
338 }while(0);
339
340 #define TRACE1_REFER_INFO(pre, b) \
341 do{ \
342 cursor_at_sol=false; \
343 gg_fprintf(trace_handle, 1, "%s", gg_string_literal(pre)); \
344 if( !(b).field ) \
345 { \
346 gg_fprintf(trace_handle, 0, #b ".field is NULL"); \
347 } \
348 else \
349 { \
350 gg_fprintf(trace_handle, 1, "%s", gg_string_literal( (b).field->name ? (b).field->name:"")); \
351 if( b.nsubscript() ) \
352 { \
353 gg_fprintf(trace_handle, 0, "("); \
354 for(unsigned int i=0; i<b.nsubscript(); i++) \
355 { \
356 gg_fprintf(trace_handle, 1, "%s", gg_string_literal( b.subscripts[i].field->name ? b.subscripts[i].field->name : "" )); \
357 if( i<b.nsubscript()-1 ) \
358 { \
359 gg_fprintf(trace_handle, 0, " "); \
360 } \
361 } \
362 if( b.refmod.from || b.refmod.len ) \
363 { \
364 gg_fprintf(trace_handle, 0, "("); \
365 if( b.refmod.from ) \
366 { \
367 gg_fprintf(trace_handle, 1, "%s", gg_string_literal( b.refmod.from->name() ? b.refmod.from->name() : "" )); \
368 } \
369 gg_fprintf(trace_handle, 0, ":"); \
370 if( b.refmod.len ) \
371 { \
372 gg_fprintf(trace_handle, 1, "%s", gg_string_literal( b.refmod.len->name() ? b.refmod.len->name() : "" )); \
373 } \
374 gg_fprintf(trace_handle, 0, "("); \
375 } \
376 gg_fprintf(trace_handle, 0, ")"); \
377 } \
378 gg_fprintf(trace_handle, 1, " (%s", gg_string_literal(cbl_field_type_str((b).field->type))); \
379 if( (b).field->type != FldLiteralN && (b).field->type != FldConditional ) \
380 { \
381 if( !(b).field->var_decl_node ) \
382 { \
383 gg_fprintf(trace_handle, 0, #b ".field->var_decl_node is NULL", NULL_TREE); \
384 } \
385 else \
386 { \
387 gg_fprintf(trace_handle, 1, " attr 0x%lx", member(b.field, "attr" )); \
388 gg_fprintf(trace_handle, 1, " c:o:d:r %ld", member(b.field, "capacity")); \
389 gg_fprintf(trace_handle, 1, ":%ld", member(b.field, "offset" )); \
390 gg_fprintf(trace_handle, 1, ":%d", gg_cast(INT, (member(b.field, "digits" )))); \
391 gg_fprintf(trace_handle, 1, ":%d)", gg_cast(INT, (member(b.field, "rdigits" )))); \
392 } \
393 } \
394 else if( (b).field->type == FldLiteralN ) \
395 { \
396 gg_fprintf(trace_handle, 1, " attr 0x%lx", build_int_cst_type(SIZE_T, (b).field->attr)); \
397 gg_fprintf(trace_handle, 1, " c:o:d:r %ld", build_int_cst_type(SIZE_T, (b).field->data.capacity)); \
398 gg_fprintf(trace_handle, 1, ":%ld", build_int_cst_type(SIZE_T, (b).field->offset)); \
399 gg_fprintf(trace_handle, 1, ":%d", build_int_cst_type(INT, (b).field->data.digits)); \
400 gg_fprintf(trace_handle, 1, ":%d)", build_int_cst_type(INT, (b).field->data.rdigits)); \
401 } \
402 } \
403 }while(0);
404
405 #define TRACE1_FIELD(a, b, c) \
406 do{ \
407 TRACE1_FIELD_INFO(a, b) \
408 TRACE1_FIELD_VALUE("", b, c) \
409 }while(0);
410
411 #define TRACE1_REFER(a, b, c) \
412 do{ \
413 TRACE1_REFER_INFO(a, b) \
414 TRACE1_REFER_VALUE("", b, c) \
415 }while(0);
416
417 #define TRACE1_LABEL(a, b, c) \
418 do{ \
419 cursor_at_sol=false; \
420 gg_fprintf(trace_handle, 1, "%s", gg_string_literal(a)); \
421 if( !b ) \
422 { \
423 gg_fprintf(trace_handle, 0, "label " #b " is NULL"); \
424 } \
425 else \
426 { \
427 gg_fprintf(trace_handle, 2, \
428 "%s (%s)", \
429 gg_string_literal(b->name), \
430 gg_string_literal(b->type_str()), \
431 NULL_TREE); \
432 } \
433 gg_fprintf(trace_handle, 1, "%s", gg_string_literal(c)); \
434 } while(0);
435
436 // Use CHECK_FIELD when a should be non-null, and a->var_decl_node also should
437 // by non-null. (The useless calls to abort() are because cppcheck doesn't
438 // understand that gcc_unreachable doesn't return);
439
440 // Use this after doing any SHOW_PARSE stuff, to avoid cppcheck complaints
441 // about nullPointerRedundantCheck
442 #define CHECK_FIELD(a) \
443 do { \
444 if(!a) \
445 { \
446 yywarn("%s: parameter %<" #a "%> is NULL", __func__); \
447 gcc_unreachable(); \
448 abort(); \
449 } \
450 if( !a->var_decl_node ) \
451 { \
452 yywarn("%s: parameter %<" #a "%> is variable " \
453 "%s<%s> with NULL %<var_decl_node%>", \
454 __func__, \
455 a->name, \
456 cbl_field_type_str(a->type) ); \
457 gcc_unreachable(); \
458 abort(); \
459 } \
460 } while(0);
461
462 // This version is a bit more lax, for special cases
463 #define CHECK_FIELD2(a) \
464 do { \
465 if(!a) \
466 { \
467 yywarn("%s: parameter %<" #a "%> is NULL", __func__); \
468 gcc_unreachable(); \
469 abort(); \
470 } \
471 if( !a->var_decl_node && a->type != FldConditional && a->type != FldLiteralA) \
472 { \
473 yywarn("%s: parameter %<" #a "%> is variable " \
474 "%s<%s> with NULL %<var_decl_node%>", \
475 __func__, \
476 a->name, \
477 cbl_field_type_str(a->type) ); \
478 gcc_unreachable(); \
479 abort(); \
480 } \
481 } while(0);
482
483
484 #define CHECK_LABEL(a) \
485 do{ \
486 if(!a) \
487 { \
488 yywarn("%s: parameter %<" #a "%> is NULL", __func__); \
489 gcc_unreachable(); \
490 abort(); \
491 } \
492 }while(0);
493
494 #ifdef INCORPORATE_ANALYZER
495 // The analyzer requires a C++17 compiler because of the inline static variable
496 class ANALYZE
497 {
498 private:
499 const char *func;
500 int level;
501 inline static int analyze_level=1;
502 public:
503 ANALYZE(const char *func_) : func(func_)
504 {
505 level = 0;
506 if( getenv("Analyze") )
507 {
508 level = analyze_level++;
509 char ach[128];
510 snprintf(ach, sizeof(ach), "# %s analyze_enter %d", func, level);
511 if( !mode_syntax_only() )
512 {
513 gg_insert_into_assembler(ach);
514 }
515 }
516 }
517 ~ANALYZE()
518 {
519 ExitMessage();
520 }
521 void ExitMessage()
522 {
523 if( getenv("Analyze") )
524 {
525 char ach[128];
526 snprintf(ach, sizeof(ach), "# %s analyze_exit %d", func, level);
527 if( !mode_syntax_only() )
528 {
529 gg_insert_into_assembler(ach);
530 }
531 }
532 }
533 void Message(const char *msg)
534 {
535 if( getenv("Analyze") )
536 {
537 char ach[128];
538 snprintf(ach, sizeof(ach), "# %s %s %d", func, msg, level);
539 if( !mode_syntax_only() )
540 {
541 gg_insert_into_assembler(ach);
542 }
543 }
544 }
545 };
546 #else
547 // cppcheck-suppress ctuOneDefinitionRuleViolation
548 class ANALYZE
549 {
550 public:
551 explicit ANALYZE(const char *)
552 {
553 }
554 ~ANALYZE()
555 {
556 ExitMessage();
557 }
558 void ExitMessage()
559 {
560 }
561 void Message(const char *)
562 {
563 }
564 };
565 #endif
566
567 #define Analyze() ANALYZE Analyzer(__func__);
568
569 #pragma GCC diagnostic pop
570
571 #endif