| 1 |
/* Run expensive selftests. */ |
| 2 |
/* { dg-options "-O" } */ |
| 3 |
|
| 4 |
#include "gcc-plugin.h" |
| 5 |
#include "config.h" |
| 6 |
#include "system.h" |
| 7 |
#include "coretypes.h" |
| 8 |
#include "diagnostic.h" |
| 9 |
#include "edit-context.h" |
| 10 |
#include "selftest.h" |
| 11 |
#include "selftest-diagnostic.h" |
| 12 |
|
| 13 |
int plugin_is_GPL_compatible; |
| 14 |
|
| 15 |
#if CHECKING_P |
| 16 |
|
| 17 |
namespace selftest { |
| 18 |
|
| 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. */ |
| 22 |
|
| 23 |
static void |
| 24 |
verify_column (location_t loc, |
| 25 |
const line_map_ordinary *ord_map, |
| 26 |
int line_width, |
| 27 |
int expected_column) |
| 28 |
{ |
| 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); |
| 40 |
} |
| 41 |
|
| 42 |
/* Subroutine of test_fixit_on_very_long_line. |
| 43 |
Run various things for RICHLOC, but don't check; we just want them |
| 44 |
to survive. */ |
| 45 |
|
| 46 |
static void |
| 47 |
test_richloc (rich_location *richloc) |
| 48 |
{ |
| 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 ()); |
| 53 |
|
| 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); |
| 58 |
free (diff); |
| 59 |
} |
| 60 |
|
| 61 |
/* Verify that the fix-it-printing code can cope with very long lines |
| 62 |
(PR c/82050). */ |
| 63 |
|
| 64 |
static void |
| 65 |
test_fixit_on_very_long_line (const line_table_case &case_) |
| 66 |
{ |
| 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, |
| 72 |
1, |
| 73 |
80, |
| 74 |
VERY_LONG_LINE, |
| 75 |
VERY_LONG_LINE + 5}; |
| 76 |
for (unsigned int width_idx = 0; width_idx < ARRAY_SIZE (columns); |
| 77 |
width_idx++) |
| 78 |
{ |
| 79 |
int line_width = columns[width_idx]; |
| 80 |
|
| 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++) |
| 85 |
fputc (' ', f); |
| 86 |
fputc ('\n', f); |
| 87 |
fclose (f); |
| 88 |
|
| 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); |
| 93 |
|
| 94 |
for (unsigned int start_idx = 0; start_idx < ARRAY_SIZE (columns); |
| 95 |
start_idx++) |
| 96 |
{ |
| 97 |
int start_col = columns[start_idx]; |
| 98 |
location_t start_loc |
| 99 |
= linemap_position_for_line_and_column (line_table, ord_map, 1, |
| 100 |
start_col); |
| 101 |
verify_column (start_loc, ord_map, line_width, start_col); |
| 102 |
for (unsigned int finish_idx = 0; finish_idx < ARRAY_SIZE (columns); |
| 103 |
finish_idx++) |
| 104 |
{ |
| 105 |
int finish_col = columns[finish_idx]; |
| 106 |
location_t finish_loc |
| 107 |
= linemap_position_for_line_and_column (line_table, ord_map, 1, |
| 108 |
finish_col); |
| 109 |
verify_column (finish_loc, ord_map, line_width, finish_col); |
| 110 |
|
| 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. */ |
| 114 |
|
| 115 |
/* Insertion. */ |
| 116 |
{ |
| 117 |
rich_location richloc (line_table, start_loc); |
| 118 |
richloc.add_fixit_insert_after (start_loc, "insertion"); |
| 119 |
test_richloc (&richloc); |
| 120 |
} |
| 121 |
|
| 122 |
/* Replacement. */ |
| 123 |
{ |
| 124 |
rich_location richloc (line_table, start_loc); |
| 125 |
source_range range |
| 126 |
= source_range::from_locations (start_loc, finish_loc); |
| 127 |
richloc.add_fixit_replace (range, "replacement"); |
| 128 |
test_richloc (&richloc); |
| 129 |
} |
| 130 |
|
| 131 |
/* Deletion. */ |
| 132 |
{ |
| 133 |
rich_location richloc (line_table, start_loc); |
| 134 |
source_range range |
| 135 |
= source_range::from_locations (start_loc, finish_loc); |
| 136 |
richloc.add_fixit_remove (range); |
| 137 |
test_richloc (&richloc); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 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. */ |
| 147 |
|
| 148 |
static void |
| 149 |
expensive_tests (void */*gcc_data*/, void */*user_data*/) |
| 150 |
{ |
| 151 |
test_runner r ("expensive_selftests_plugin"); |
| 152 |
|
| 153 |
for_each_line_table_case (test_fixit_on_very_long_line); |
| 154 |
} |
| 155 |
|
| 156 |
} // namespace selftest |
| 157 |
|
| 158 |
#endif /* #if CHECKING_P */ |
| 159 |
|
| 160 |
int |
| 161 |
plugin_init (struct plugin_name_args *plugin_info, |
| 162 |
struct plugin_gcc_version *version) |
| 163 |
{ |
| 164 |
#if CHECKING_P |
| 165 |
const char *plugin_name = plugin_info->base_name; |
| 166 |
register_callback (plugin_info->base_name, |
| 167 |
PLUGIN_FINISH, |
| 168 |
selftest::expensive_tests, |
| 169 |
NULL); /* void *user_data */ |
| 170 |
#else |
| 171 |
inform (UNKNOWN_LOCATION, "self-tests are not enabled in this build"); |
| 172 |
#endif /* #if CHECKING_P */ |
| 173 |
return 0; |
| 174 |
} |