Skip to content

Commit 1dbe26b

Browse files
committed
c-family: Use BULTINS_LOCATION for predefined macros changed upon optimize or target pragmas [PR103012]
The following testcases ICE when an optimize or target pragma is followed by a long line (4096+ chars). This is because on such long lines we can't use columns anymore, but the cpp_define calls performed by c_cpp_builtins_optimize_pragma or from the backend hooks for target pragma are done on temporary buffers and expect to get columns from whatever line they appear on (which happens to be the long line after optimize/target pragma), and we run into: #0 fancy_abort (file=0x3abec67 "../../libcpp/line-map.c", line=502, function=0x3abecfc "linemap_add") at ../../gcc/diagnostic.c:1986 #1 0x0000000002e7c335 in linemap_add (set=0x7ffff7fca000, reason=LC_RENAME, sysp=0, to_file=0x41287a0 "pr103012.i", to_line=3) at ../../libcpp/line-map.c:502 #2 0x0000000002e7cc24 in linemap_line_start (set=0x7ffff7fca000, to_line=3, max_column_hint=128) at ../../libcpp/line-map.c:827 #3 0x0000000002e7ce2b in linemap_position_for_column (set=0x7ffff7fca000, to_column=1) at ../../libcpp/line-map.c:898 #4 0x0000000002e771f9 in _cpp_lex_direct (pfile=0x40c3b60) at ../../libcpp/lex.c:3592 #5 0x0000000002e76c3e in _cpp_lex_token (pfile=0x40c3b60) at ../../libcpp/lex.c:3394 #6 0x0000000002e610ef in lex_macro_node (pfile=0x40c3b60, is_def_or_undef=true) at ../../libcpp/directives.c:601 #7 0x0000000002e61226 in do_define (pfile=0x40c3b60) at ../../libcpp/directives.c:639 #8 0x0000000002e610b2 in run_directive (pfile=0x40c3b60, dir_no=0, buf=0x7fffffffd430 "__OPTIMIZE__ 1\n", count=14) at ../../libcpp/directives.c:589 #9 0x0000000002e650c1 in cpp_define (pfile=0x40c3b60, str=0x2f784d1 "__OPTIMIZE__") at ../../libcpp/directives.c:2513 #10 0x0000000002e65100 in cpp_define_unused (pfile=0x40c3b60, str=0x2f784d1 "__OPTIMIZE__") at ../../libcpp/directives.c:2522 #11 0x0000000000f50685 in c_cpp_builtins_optimize_pragma (pfile=0x40c3b60, prev_tree=<optimization_node 0x7fffea042000>, cur_tree=<optimization_node 0x7fffea042020>) at ../../gcc/c-family/c-cppbuiltin.c:600 assertion that LC_RENAME doesn't happen first. I think the right fix is emit those predefined macros upon optimize/target pragmas with BUILTINS_LOCATION, like we already do for those macros at the start of the TU, they don't appear in columns of the next line after it. Another possibility would be to force them at the location of the pragma. 2021-12-30 Jakub Jelinek <[email protected]> PR c++/103012 gcc/ * config/i386/i386-c.c (ix86_pragma_target_parse): Perform cpp_define/cpp_undef calls with forced token locations BUILTINS_LOCATION. * config/arm/arm-c.c (arm_pragma_target_parse): Likewise. * config/aarch64/aarch64-c.c (aarch64_pragma_target_parse): Likewise. * config/s390/s390-c.c (s390_pragma_target_parse): Likewise. gcc/c-family/ * c-cppbuiltin.c (c_cpp_builtins_optimize_pragma): Perform cpp_define_unused/cpp_undef calls with forced token locations BUILTINS_LOCATION. gcc/testsuite/ PR c++/103012 * g++.dg/cpp/pr103012.C: New test. * g++.target/i386/pr103012.C: New test.
1 parent 1820137 commit 1dbe26b

File tree

7 files changed

+53
-0
lines changed

7 files changed

+53
-0
lines changed

gcc/c-family/c-cppbuiltin.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,10 @@ c_cpp_builtins_optimize_pragma (cpp_reader *pfile, tree prev_tree,
589589
if (flag_undef)
590590
return;
591591

592+
/* Make sure all of the builtins about to be declared have
593+
BUILTINS_LOCATION has their location_t. */
594+
cpp_force_token_locations (parse_in, BUILTINS_LOCATION);
595+
592596
/* Other target-independent built-ins determined by command-line
593597
options. */
594598
if (!prev->x_optimize_size && cur->x_optimize_size)
@@ -653,6 +657,8 @@ c_cpp_builtins_optimize_pragma (cpp_reader *pfile, tree prev_tree,
653657
cpp_define_unused (pfile, "__ROUNDING_MATH__");
654658
else if (prev->x_flag_rounding_math && !cur->x_flag_rounding_math)
655659
cpp_undef (pfile, "__ROUNDING_MATH__");
660+
661+
cpp_stop_forcing_token_locations (parse_in);
656662
}
657663

658664

gcc/config/aarch64/aarch64-c.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,9 @@ aarch64_pragma_target_parse (tree args, tree pop_target)
259259
unsigned char saved_warn_unused_macros = cpp_opts->warn_unused_macros;
260260
cpp_opts->warn_unused_macros = 0;
261261

262+
cpp_force_token_locations (parse_in, BUILTINS_LOCATION);
262263
aarch64_update_cpp_builtins (parse_in);
264+
cpp_stop_forcing_token_locations (parse_in);
263265

264266
cpp_opts->warn_unused_macros = saved_warn_unused_macros;
265267

gcc/config/arm/arm-c.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,9 @@ arm_pragma_target_parse (tree args, tree pop_target)
464464
acond_macro = get_identifier ("__ARM_FEATURE_LDREX");
465465
C_CPP_HASHNODE (acond_macro)->flags |= NODE_CONDITIONAL;
466466

467+
cpp_force_token_locations (parse_in, BUILTINS_LOCATION);
467468
arm_cpu_builtins (parse_in);
469+
cpp_stop_forcing_token_locations (parse_in);
468470

469471
cpp_opts->warn_unused_macros = saved_warn_unused_macros;
470472

gcc/config/i386/i386-c.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,12 +702,14 @@ ix86_pragma_target_parse (tree args, tree pop_target)
702702
cur_tune = prev_tune = PROCESSOR_max;
703703

704704
/* Undef all of the macros for that are no longer current. */
705+
cpp_force_token_locations (parse_in, BUILTINS_LOCATION);
705706
ix86_target_macros_internal (prev_isa & diff_isa,
706707
prev_isa2 & diff_isa2,
707708
prev_arch,
708709
prev_tune,
709710
(enum fpmath_unit) prev_opt->x_ix86_fpmath,
710711
cpp_undef);
712+
cpp_stop_forcing_token_locations (parse_in);
711713

712714
/* For the definitions, ensure all newly defined macros are considered
713715
as used for -Wunused-macros. There is no point warning about the
@@ -717,12 +719,14 @@ ix86_pragma_target_parse (tree args, tree pop_target)
717719
cpp_opts->warn_unused_macros = 0;
718720

719721
/* Define all of the macros for new options that were just turned on. */
722+
cpp_force_token_locations (parse_in, BUILTINS_LOCATION);
720723
ix86_target_macros_internal (cur_isa & diff_isa,
721724
cur_isa2 & diff_isa2,
722725
cur_arch,
723726
cur_tune,
724727
(enum fpmath_unit) cur_opt->x_ix86_fpmath,
725728
cpp_define);
729+
cpp_stop_forcing_token_locations (parse_in);
726730

727731
cpp_opts->warn_unused_macros = saved_warn_unused_macros;
728732

gcc/config/s390/s390-c.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,9 @@ s390_pragma_target_parse (tree args, tree pop_target)
457457
cpp_opts->warn_unused_macros = 0;
458458

459459
/* Define all of the macros for new options that were just turned on. */
460+
cpp_force_token_locations (parse_in, BUILTINS_LOCATION);
460461
s390_cpu_cpp_builtins_internal (parse_in, cur_opt, prev_opt);
462+
cpp_stop_forcing_token_locations (parse_in);
461463

462464
cpp_opts->warn_unused_macros = saved_warn_unused_macros;
463465
}

gcc/testsuite/g++.dg/cpp/pr103012.C

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// PR c++/103012
2+
// { dg-do compile }
3+
4+
int a = 1;
5+
#pragma GCC optimize "Og"
6+
#define A(a) a +
7+
#define B(a) A(a)A(a)
8+
#define C(a) B(a)B(a)
9+
#define D(a) C(a)C(a)
10+
#define E(a) D(a)D(a)
11+
#define F(a) E(a)E(a)
12+
#define G(a) F(a)F(a)
13+
#define H(a) G(a)G(a)
14+
#define I(a) H(a)H(a)
15+
#define J(a) I(a)I(a)
16+
#define K(a) J(a)J(a)
17+
#define L(a) K(a)K(a)
18+
int b = L(a) 1;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// PR c++/103012
2+
// { dg-do compile }
3+
// { dg-options "-mno-avx2" }
4+
5+
int a = 1;
6+
#pragma GCC target "avx2"
7+
#define A(a) a +
8+
#define B(a) A(a)A(a)
9+
#define C(a) B(a)B(a)
10+
#define D(a) C(a)C(a)
11+
#define E(a) D(a)D(a)
12+
#define F(a) E(a)E(a)
13+
#define G(a) F(a)F(a)
14+
#define H(a) G(a)G(a)
15+
#define I(a) H(a)H(a)
16+
#define J(a) I(a)I(a)
17+
#define K(a) J(a)J(a)
18+
#define L(a) K(a)K(a)
19+
int b = L(a) 1;

0 commit comments

Comments
 (0)