1 /* Run expensive selftests. */
2 /* { dg-options "-O" } */
4 #include "gcc-plugin.h"
8 #include "diagnostic.h"
9 #include "edit-context.h"
11 #include "selftest-diagnostic.h"
13 int plugin_is_GPL_compatible
;
19 /* Subroutine of test_fixit_on_very_long_line.
20 Verify that LOC has the EXPECTED_COLUMN, apart from the various
21 cases where it can't. */
24 verify_column (location_t loc
,
25 const line_map_ordinary
*ord_map
,
29 ASSERT_TRUE (/* Normal case. */
30 LOCATION_COLUMN (loc
) == expected_column
31 /* ord_map can't store columns e.g. due to
32 max_column_hint being too high. */
33 || ord_map
->m_column_and_range_bits
== 0
34 /* Running out of location_t values. */
35 || loc
> LINE_MAP_MAX_LOCATION_WITH_COLS
36 /* column exceeds LINE_MAP_MAX_COLUMN_NUMBER. */
37 || expected_column
> (int)LINE_MAP_MAX_COLUMN_NUMBER
38 /* column exceeds max_column_hint for ord_map. */
39 || expected_column
> line_width
);
42 /* Subroutine of test_fixit_on_very_long_line.
43 Run various things for RICHLOC, but don't check; we just want them
47 test_richloc (rich_location
*richloc
)
49 /* Run the diagnostic and fix-it printing code. */
50 test_diagnostic_context dc
;
51 diagnostic_show_locus (&dc
, richloc
, DK_ERROR
, dc
.m_printer
);
53 /* Generate a diff. */
54 edit_context
ec (global_dc
->get_file_cache ());
55 ec
.add_fixits (richloc
);
56 char *diff
= ec
.generate_diff (true);
60 /* Verify that the fix-it-printing code can cope with very long lines
64 test_fixit_on_very_long_line (const line_table_case
&case_
)
66 /* Various interesting column/line-width values, to try to tickle
68 const int VERY_LONG_LINE
= 8192;
69 const int columns
[] = {0,
72 LINE_MAP_MAX_COLUMN_NUMBER
- 2,
73 LINE_MAP_MAX_COLUMN_NUMBER
- 1,
74 LINE_MAP_MAX_COLUMN_NUMBER
,
75 LINE_MAP_MAX_COLUMN_NUMBER
+ 1,
76 LINE_MAP_MAX_COLUMN_NUMBER
+ 2,
79 for (unsigned int width_idx
= 0; width_idx
< ARRAY_SIZE (columns
);
82 int line_width
= columns
[width_idx
];
84 /* Create a source file with a very long line. */
85 named_temp_file
tmp (".c");
86 FILE *f
= fopen (tmp
.get_filename (), "w");
87 for (int i
= 0; i
< line_width
; i
++)
92 line_table_test
ltt (case_
);
93 const line_map_ordinary
*ord_map
= linemap_check_ordinary
94 (linemap_add (line_table
, LC_ENTER
, false, tmp
.get_filename (), 0));
95 linemap_line_start (line_table
, 1, line_width
);
97 for (unsigned int start_idx
= 0; start_idx
< ARRAY_SIZE (columns
);
100 int start_col
= columns
[start_idx
];
102 = linemap_position_for_line_and_column (line_table
, ord_map
, 1,
104 verify_column (start_loc
, ord_map
, line_width
, start_col
);
105 for (unsigned int finish_idx
= 0; finish_idx
< ARRAY_SIZE (columns
);
108 int finish_col
= columns
[finish_idx
];
109 location_t finish_loc
110 = linemap_position_for_line_and_column (line_table
, ord_map
, 1,
112 verify_column (finish_loc
, ord_map
, line_width
, finish_col
);
114 /* Now use start-finish to exercise the fix-it code.
115 In each case, run the printing code, but don't check;
116 we just want it to survive. */
120 rich_location
richloc (line_table
, start_loc
);
121 richloc
.add_fixit_insert_after (start_loc
, "insertion");
122 test_richloc (&richloc
);
127 rich_location
richloc (line_table
, start_loc
);
129 = source_range::from_locations (start_loc
, finish_loc
);
130 richloc
.add_fixit_replace (range
, "replacement");
131 test_richloc (&richloc
);
136 rich_location
richloc (line_table
, start_loc
);
138 = source_range::from_locations (start_loc
, finish_loc
);
139 richloc
.add_fixit_remove (range
);
140 test_richloc (&richloc
);
147 /* Callback handler for the PLUGIN_FINISH event.
148 At this point, all GCC subsystems should be initialized and
149 "warmed up"; this is where we run our unit tests. */
152 expensive_tests (void */
*gcc_data*/
, void */
*user_data*/
)
154 test_runner
r ("expensive_selftests_plugin");
156 for_each_line_table_case (test_fixit_on_very_long_line
);
159 } // namespace selftest
161 #endif /* #if CHECKING_P */
164 plugin_init (struct plugin_name_args
*plugin_info
,
165 struct plugin_gcc_version
*version
)
168 const char *plugin_name
= plugin_info
->base_name
;
169 register_callback (plugin_info
->base_name
,
171 selftest::expensive_tests
,
172 NULL
); /* void *user_data */
174 inform (UNKNOWN_LOCATION
, "self-tests are not enabled in this build");
175 #endif /* #if CHECKING_P */