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
, dc
.m_source_printing
,
52 richloc
, DK_ERROR
, dc
.get_reference_printer ());
54 /* Generate a diff. */
55 edit_context
ec (global_dc
->get_file_cache ());
56 ec
.add_fixits (richloc
);
57 char *diff
= ec
.generate_diff (true);
61 /* Verify that the fix-it-printing code can cope with very long lines
65 test_fixit_on_very_long_line (const line_table_case
&case_
)
67 /* Various interesting column/line-width values, to try to tickle out bugs. In
68 64-bit location mode, we can't test the max because the maximum supported
69 column is unreasonably large. */
70 const int VERY_LONG_LINE
= 8192;
71 const int columns
[] = {0,
76 for (unsigned int width_idx
= 0; width_idx
< ARRAY_SIZE (columns
);
79 int line_width
= columns
[width_idx
];
81 /* Create a source file with a very long line. */
82 named_temp_file
tmp (".c");
83 FILE *f
= fopen (tmp
.get_filename (), "w");
84 for (int i
= 0; i
< line_width
; i
++)
89 line_table_test
ltt (case_
);
90 const line_map_ordinary
*ord_map
= linemap_check_ordinary
91 (linemap_add (line_table
, LC_ENTER
, false, tmp
.get_filename (), 0));
92 linemap_line_start (line_table
, 1, line_width
);
94 for (unsigned int start_idx
= 0; start_idx
< ARRAY_SIZE (columns
);
97 int start_col
= columns
[start_idx
];
99 = linemap_position_for_line_and_column (line_table
, ord_map
, 1,
101 verify_column (start_loc
, ord_map
, line_width
, start_col
);
102 for (unsigned int finish_idx
= 0; finish_idx
< ARRAY_SIZE (columns
);
105 int finish_col
= columns
[finish_idx
];
106 location_t finish_loc
107 = linemap_position_for_line_and_column (line_table
, ord_map
, 1,
109 verify_column (finish_loc
, ord_map
, line_width
, finish_col
);
111 /* Now use start-finish to exercise the fix-it code.
112 In each case, run the printing code, but don't check;
113 we just want it to survive. */
117 rich_location
richloc (line_table
, start_loc
);
118 richloc
.add_fixit_insert_after (start_loc
, "insertion");
119 test_richloc (&richloc
);
124 rich_location
richloc (line_table
, start_loc
);
126 = source_range::from_locations (start_loc
, finish_loc
);
127 richloc
.add_fixit_replace (range
, "replacement");
128 test_richloc (&richloc
);
133 rich_location
richloc (line_table
, start_loc
);
135 = source_range::from_locations (start_loc
, finish_loc
);
136 richloc
.add_fixit_remove (range
);
137 test_richloc (&richloc
);
144 /* Callback handler for the PLUGIN_FINISH event.
145 At this point, all GCC subsystems should be initialized and
146 "warmed up"; this is where we run our unit tests. */
149 expensive_tests (void */
*gcc_data*/
, void */
*user_data*/
)
151 test_runner
r ("expensive_selftests_plugin");
153 for_each_line_table_case (test_fixit_on_very_long_line
);
156 } // namespace selftest
158 #endif /* #if CHECKING_P */
161 plugin_init (struct plugin_name_args
*plugin_info
,
162 struct plugin_gcc_version
*version
)
165 const char *plugin_name
= plugin_info
->base_name
;
166 register_callback (plugin_info
->base_name
,
168 selftest::expensive_tests
,
169 NULL
); /* void *user_data */
171 inform (UNKNOWN_LOCATION
, "self-tests are not enabled in this build");
172 #endif /* #if CHECKING_P */