aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndi Kleen <ak@gcc.gnu.org>2025-01-25 22:48:29 -0800
committerAndi Kleen <ak@gcc.gnu.org>2025-02-02 21:59:40 -0800
commit75ab30f77f6d8a555aa78472b45a75a508544c68 (patch)
tree2a09ac57101b291617b8634638a1780b9184fe5d
parentbaf26fccfb51fa54fcf7c668b96cae4cdbe574b3 (diff)
downloadgcc-75ab30f77f6d8a555aa78472b45a75a508544c68.zip
gcc-75ab30f77f6d8a555aa78472b45a75a508544c68.tar.gz
gcc-75ab30f77f6d8a555aa78472b45a75a508544c68.tar.bz2
Add a unit test for random access in the file cache
v2: Remove extra {} gcc/ChangeLog: * input.cc (check_line): New. (test_replacement): New function to test line caching. (input_cc_tests): Call test_replacement
-rw-r--r--gcc/input.cc43
1 files changed, 43 insertions, 0 deletions
diff --git a/gcc/input.cc b/gcc/input.cc
index 3416c7c..66a0ac6 100644
--- a/gcc/input.cc
+++ b/gcc/input.cc
@@ -2301,6 +2301,48 @@ test_make_location_nonpure_range_endpoints (const line_table_case &case_)
ASSERT_FALSE (IS_ADHOC_LOC (get_finish (not_aaa_eq_bbb)));
}
+/* Verify reading of a specific line LINENUM in TMP, FC. */
+
+static void check_line (temp_source_file &tmp, file_cache &fc, int linenum)
+{
+ char_span line = fc.get_source_line (tmp.get_filename (), linenum);
+ int n;
+ /* get_buffer is not null terminated, but the sscanf stops after a number. */
+ ASSERT_TRUE (sscanf (line.get_buffer (), "%d", &n) == 1);
+ ASSERT_EQ (n, linenum);
+}
+
+/* Test file cache replacement. */
+
+static void test_replacement ()
+{
+ const int maxline = 1000;
+
+ char *vec = XNEWVEC (char, maxline * 15);
+ char *p = vec;
+ int i;
+ for (i = 1; i <= maxline; i++)
+ p += sprintf (p, "%d\n", i);
+
+ temp_source_file tmp (SELFTEST_LOCATION, ".txt", vec);
+ free (vec);
+ file_cache fc;
+
+ for (i = 2; i <= maxline; i++)
+ {
+ check_line (tmp, fc, i);
+ check_line (tmp, fc, i - 1);
+ if (i >= 10)
+ check_line (tmp, fc, i - 9);
+ if (i >= 350) /* Exceed the look behind cache. */
+ check_line (tmp, fc, i - 300);
+ }
+ for (i = 5; i <= maxline; i += 100)
+ check_line (tmp, fc, i);
+ for (i = 1; i <= maxline; i++)
+ check_line (tmp, fc, i);
+}
+
/* Verify reading of input files (e.g. for caret-based diagnostics). */
static void
@@ -4251,6 +4293,7 @@ input_cc_tests ()
test_reading_source_line ();
test_reading_source_buffer ();
+ test_replacement ();
test_line_offset_overflow ();