summaryrefslogtreecommitdiff
path: root/tests/synclines.at
blob: e8da93a388ed8f2bd0caf0900e345b7e5a3338de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
# Executing Actions.                               -*- Autotest -*-

# Copyright (C) 2002, 2004-2005, 2007, 2009-2015, 2018-2022 Free
# Software Foundation, Inc.

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

AT_BANNER([[Sync Lines.]])


# _AT_SYNCLINES_COMPILE(FILE)
# ---------------------------
# Compile FILE expecting an error, and save in the file stdout the
# normalized output.  Ignore the exit status, since some compilers
# (e.g. c89 on IRIX 6.5) trigger warnings on '#error', instead of
# errors.
#
# Transform the file 'stderr' into something like this:
#
#   input.y:4: #error "4"
#
# In case GCC displays column information, strip it down.
#
#   input.y:4:2: #error "4"    or
#   input.y:4.2: #error "4"    or
#   input.y:4:2: error: #error "4"
# =>
#   input.y:4: #error "4"
#
# It may also issue more context information:
#
#   input.y: In function 'yyparse':
#   input.y:8: #error "8"
# =>
#   input.y:4: #error "8"
#
# The message may include a caret-error (indented by GCC 4.8,
# not by clang 3.2):
#
#   input.y:1:2: error: #error "1"
#    #error "1"
#     ^
#
# Possibly distcc adds its bits.
#
#   distcc[33187] ERROR: compile (null) on localhost failed
#   syncline.c:1:2: error: #error "1"
#   distcc[33185] ERROR: compile syncline.c on localhost failed
#
# or even
#
#   distcc[35882] (dcc_connect_by_name) ERROR: failed to look up host "chrisimac": Unknown host
#   distcc[35882] Warning: failed to distribute input.c to chrisimac/4, running locally instead
#
# The compiler might end by the number of messages issued (Clang 3.2):
#
#    syncline.c:1:2: error: "1"
#    #error "1"
#     ^
#    1 error generated.
#
# When c++ is used to compiler C, we might have more messages (Clang 3.2):
#
#    clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated
#
#
# xlc reports things like:
#
#    "input.yy", line 80.21: 1540-0218 (S) The call does not match any parameter list for "operator<<".
#    "/usr/vacpp/include/iosfwd", line 32.6: 1506-205 (S) #error This file to be used only with IBM VisualAge C++ v4 and later compilers
#
# Newer GCCs go further and if the function is declared static, complain
# that it's not used.
#
# GCC 7 also underlines the error.
#
#    syncline.c:4:2: error: #error "4"
#     #error "4"
#      ^~~~~
#
# GCC 9 displays a left-hand margin with the line number:
#
#    syncline.c: In function 'foo':
#    syncline.c:4:2: error: #error "4"
#        4 | #error "4"
#          |  ^~~~~
m4_define([_AT_SYNCLINES_COMPILE],
[AT_CHECK([$CC $CFLAGS $CPPFLAGS -c $1], [ignore], [], [stderr])
AT_CHECK([["$PERL" -p -0777 - stderr <<\EOF || exit 77
  # Remove left-hand margin.
  s/^[\d ]{6}\| //gm;

  # 1. Remove useless lines.

  # distcc clutter.
  s/^distcc\[\d+\] .*\n//gm;
  # c vs. c++.
  s/^clang: warning: treating 'c' input as 'c\+\+'.*\n//gm;
  # Function context.
  s/^[^:]*: In function '[^']+':\n//gm;
  # Caret error (with possible '~' to underline).
  s/^ *#error.*\n *\^~*\n//gm;
  # Number of errors.
  s/^1 error generated\.\n//gm;

  # 2. Normalize the lines we kept.

  # xlc messages.  Remove also error identifier (e.g., "1540-0218 (S)").
  s/^"(.*?)", line ([\w.]*): \d+-\d+ \(.\) /$][1:$][2: /gm;
  # Remove column.
  s/^([^:]+:\d+)[.:][^:]+:(.+)$/$][1:$][2/gm;
  # Map all combinations of "error: " and "#error: " to "#error ".
  s/^([^:]+:\d+):( |#error|error|:)+/$][1: #error /gm;
EOF
]],
  0, [stdout])
])



# AT_SYNCLINES_COMPILE(FILE)
# --------------------------
# Compile FILE expecting an error, and save in the file 'stdout' the
# normalized output.  If we can't get a trustable location
# from the compiler, just skip the test.
#
# It seems impossible to find a generic scheme to check the location
# of an error.  Even requiring GCC is not sufficient, since for instance
# the version modified by Apple:
#
# | Reading specs from /usr/libexec/gcc/darwin/ppc/2.95.2/specs
# | Apple Computer, Inc. version gcc-934.3, based on gcc version 2.95.2
# | 19991024 (release) configure:2124: $? = 0
#
# instead of:
#
# | input.y:2: #error "2"
#
# it reports:
#
# | input.y:2: "2"
# | cpp-precomp: warning: errors during smart preprocessing, retrying in basic mode
m4_define([AT_SYNCLINES_COMPILE],
[# Check if we can trust location translation.
AT_DATA([syncline.c],
[[void foo (void);
void foo (void)
{
#error "4"
}
]])

_AT_SYNCLINES_COMPILE([syncline.c])
AT_REQUIRE([[test "`cat stdout`" = 'syncline.c:4: @%:@error "4"']])

# Then work for real.
_AT_SYNCLINES_COMPILE([$1])
])


# AT_TEST(TITLE, INPUT, ERROR-MSG)
# --------------------------------
# Check that compiling the parser produced from INPUT cause GCC
# to issue ERROR-MSG.
m4_pushdef([AT_TEST],
[AT_SETUP([$1])
AT_BISON_OPTION_PUSHDEFS

AT_DATA([[input.y]], [$2])
AT_BISON_CHECK([-o input.c input.y])
AT_SYNCLINES_COMPILE([input.c])
AT_CHECK([cat stdout], 0, [$3])
AT_BISON_OPTION_POPDEFS
AT_CLEANUP
])


## ------------------- ##
## Prologue syncline.  ##
## ------------------- ##


AT_TEST([Prologue syncline],
[[%{
#error "2"
%}
%code {
  ]AT_YYERROR_DECLARE_EXTERN[
  ]AT_YYLEX_DECLARE_EXTERN[
}
%%
exp: '0';
%%
]],
[input.y:2: #error "2"
])


## ----------------- ##
## %union syncline.  ##
## ----------------- ##

AT_TEST([%union syncline],
[[%union {
#error "2"
  char dummy;
}
%code {
  ]AT_YYERROR_DECLARE_EXTERN[
  ]AT_YYLEX_DECLARE_EXTERN[
}
%%
exp: '0';
%%
]],
[input.y:2: #error "2"
])


## ---------------------- ##
## %union name syncline.  ##
## ---------------------- ##

# Check that invalid union names are properly reported in the
# source file.
AT_SETUP([%union name syncline])
AT_BISON_OPTION_PUSHDEFS
AT_DATA([[input.y]],
[[%union break
{
  char dummy;
}
%code {
  ]AT_YYERROR_DECLARE_EXTERN[
  ]AT_YYLEX_DECLARE_EXTERN[
%}
%%
exp: '0';
%%
]])

AT_BISON_CHECK([-o input.c input.y])
AT_SYNCLINES_COMPILE([input.c])
AT_CHECK([[grep '^input.y:1' stdout]], 0, [ignore])
AT_BISON_OPTION_POPDEFS
AT_CLEANUP


## ----------------------- ##
## Postprologue syncline.  ##
## ----------------------- ##

AT_TEST([Postprologue syncline],
[[%code {
  ]AT_YYERROR_DECLARE_EXTERN[
  ]AT_YYLEX_DECLARE_EXTERN[
}
%{
  /* Preprologue.  */
%}
%union
{
  int ival;
}
%{
#error "13"
%}
%%
exp: '0';
%%
]],
[input.y:13: #error "13"
])


## ----------------- ##
## Action syncline.  ##
## ----------------- ##

AT_TEST([Action syncline],
[[%code {
  ]AT_YYERROR_DECLARE_EXTERN[
  ]AT_YYLEX_DECLARE_EXTERN[
}
%%
exp:
{
#error "8"
};
]],
[input.y:8: #error "8"
])


## ------------------- ##
## Epilogue syncline.  ##
## ------------------- ##

AT_TEST([Epilogue syncline],
[[%code {
  ]AT_YYERROR_DECLARE_EXTERN[
  ]AT_YYLEX_DECLARE_EXTERN[
}
%%
exp: '0';
%%
#error "8"
]],
[input.y:8: #error "8"
])

## -------------------- ##
## %code top syncline.  ##
## -------------------- ##

AT_TEST([%code top syncline],
[[%code top {
#error "2"
}
%code {
  ]AT_YYERROR_DECLARE_EXTERN[
  ]AT_YYLEX_DECLARE_EXTERN[
}
%%
exp: '0';
%%
]],
[input.y:2: #error "2"
])

## ---------------------- ##
## %destructor syncline.  ##
## ---------------------- ##

AT_TEST([%destructor syncline],
[[%destructor {
#error "2"
} <ival>
%{
]AT_YYERROR_DECLARE_EXTERN[
]AT_YYLEX_DECLARE_EXTERN[
%}
%union {
  int ival;
}
%type <ival> exp
%%
exp: '0' { $$ = 0; };
%%
]],
[input.y:2: #error "2"
])


## ------------------- ##
## %printer syncline.  ##
## ------------------- ##

AT_TEST([%printer syncline],
[[%printer {
#error "2"
} <ival>
%debug
%code {
  ]AT_YYERROR_DECLARE_EXTERN[
  ]AT_YYLEX_DECLARE_EXTERN[
}
%union {
  int ival;
}
%type <ival> exp
%%
exp: '0' { $$ = 0; };
%%
]],
[input.y:2: #error "2"
])

m4_popdef([AT_TEST])



## ------------------ ##
## syncline escapes.  ##
## ------------------ ##

# AT_TEST([SKELETON])
# -------------------
m4_pushdef([AT_TEST],
[AT_SETUP([syncline escapes: $1])

AT_BISON_OPTION_PUSHDEFS([%skeleton "$1"])

# ICC is unable to handle these file names: it fails to pass them
# properly to the linker, and is unable to save a file named this way.
# Don't try with such compilers.
AT_DATA([\"\\\"\".AT_LANG_EXT],
[[int main (void) { return 0; }
]])

AT_REQUIRE([AT_CXX_IF([$CXX $CXXFLAGS], [$CC $CFLAGS]) $CPPFLAGS \"\\\"\".]AT_LANG_EXT[ -o \"\\\"\"],
                 [0], [ignore], [ignore])

AT_DATA_GRAMMAR([\"\\\"\".y],
[[%skeleton "$1"
%code {
  ]AT_YYERROR_DECLARE[
  ]AT_YYLEX_DECLARE[
}
%destructor {} <>
%printer {} <>
%%
exp: '0'
%%
]AT_YYERROR_DEFINE[
]AT_YYLEX_DEFINE[
]AT_MAIN_DEFINE[
]])

# Maybe we tried to create a file name that the file system does not
# support.
AT_SKIP_IF([[test ! -f \"\\\"\".y]])
AT_FULL_COMPILE([\"\\\"\"])
AT_BISON_OPTION_POPDEFS

AT_CLEANUP
])

m4_map_args([AT_TEST], [yacc.c], [glr.c], [lalr1.cc], [glr.cc], [glr2.cc])

m4_popdef([AT_TEST])



## ----------- ##
## %no-lines.  ##
## ----------- ##

# AT_TEST([SKELETON])
# -------------------
m4_pushdef([AT_TEST],
[AT_SETUP([%no-lines: $1])

AT_BISON_OPTION_PUSHDEFS([%skeleton "$1" %header])
AT_DATA_GRAMMAR([input.y],
[[%skeleton "$1" %header
%{
]AT_YYERROR_DECLARE_EXTERN[
]AT_YYLEX_DECLARE_EXTERN[
%}
%union {
  int ival;
}
%%
exp: '0'
]])

# Generate without.* without the #line.  Don't use -o without.c which
# would change the content (e.g., CPP guard depend on the output file
# name).
AT_BISON_CHECK([--no-lines -o input.AT_LANG_EXT -d input.y])
AT_CHECK([mv input.]AT_LANG_EXT[ without.]AT_LANG_EXT[])
AT_CHECK([mv input.]AT_LANG_HDR[ without.]AT_LANG_HDR[])
# There is no #line at all.
AT_CHECK([grep '#line' *.]AT_LANG_EXT[ *.]AT_LANG_HDR[], 1)

# Generate with.* without the #line.
AT_BISON_CHECK([-o input.AT_LANG_EXT -d input.y])
AT_CHECK([mv input.]AT_LANG_EXT[ with.]AT_LANG_EXT[])
AT_CHECK([mv input.]AT_LANG_HDR[ with.]AT_LANG_HDR[])

# The implementation file with --no-line is exactly the
# original one with #lines removed.
AT_CHECK([grep -v '#line' with.]AT_LANG_EXT[ >expout])
AT_CHECK([cat without.AT_LANG_EXT], 0, [expout])

# Likewise for the header.
AT_CHECK([grep -v '#line' with.]AT_LANG_HDR[ >expout])
AT_CHECK([cat without.AT_LANG_HDR], 0, [expout])

AT_BISON_OPTION_POPDEFS

AT_CLEANUP
])

m4_map_args([AT_TEST], [yacc.c], [glr.c], [lalr1.cc], [glr.cc], [glr2.cc])

m4_popdef([AT_TEST])



## ---------------- ##
## Output columns.  ##
## ---------------- ##

AT_SETUP([Output columns])

# This test is fragile: its point is to check the compiler's error
# message, but it seems too hard to do portability (even between
# version of GCC).  So instead, let's just check the generated code
# itself.

AT_BISON_OPTION_PUSHDEFS
AT_DATA([input.y],
[[%{
]AT_YYERROR_DECLARE_EXTERN[
]AT_YYLEX_DECLARE_EXTERN[
%}
%define api.value.type union
%type <int> '0' exp
%destructor { /* --BEGIN */
              destructor
              /* --END   */ } <*>
%printer { /* --BEGIN */
           printer
           /* --END   */ } <*>



%left '+'
%%
exp: exp '+' exp {  /* --BEGIN */
                    $$ = $1 + $3;
                    @$ = @1 + @3;
                    /* --END */ }
   | '0'
]])

AT_BISON_CHECK([-o input.c input.y])
AT_CHECK([[sed -ne '/--BEGIN/,/--END/{' \
    -e '/input.c/s/ [0-9]* / LINE /;' \
    -e 'p;}' \
    input.c]], 0,
[[         { /* --BEGIN */
           printer
           /* --END   */ }
         { /* --BEGIN */
           printer
           /* --END   */ }
            { /* --BEGIN */
              destructor
              /* --END   */ }
            { /* --BEGIN */
              destructor
              /* --END   */ }
                 {  /* --BEGIN */
                    (yyval.exp) = (yyvsp[-2].exp) + (yyvsp[0].exp);
                    (yyloc) = (yylsp[-2]) + (yylsp[0]);
                    /* --END */ }
]])

AT_BISON_OPTION_POPDEFS
AT_CLEANUP