aboutsummaryrefslogtreecommitdiff
path: root/gcc/c-family/c-indentation.cc
diff options
context:
space:
mode:
Diffstat (limited to 'gcc/c-family/c-indentation.cc')
-rw-r--r--gcc/c-family/c-indentation.cc73
1 files changed, 46 insertions, 27 deletions
diff --git a/gcc/c-family/c-indentation.cc b/gcc/c-family/c-indentation.cc
index 5b4614f..a81dffd 100644
--- a/gcc/c-family/c-indentation.cc
+++ b/gcc/c-family/c-indentation.cc
@@ -45,12 +45,13 @@ next_tab_stop (unsigned int vis_column, unsigned int tab_width)
on the line (up to or before EXPLOC). */
static bool
-get_visual_column (expanded_location exploc,
+get_visual_column (file_cache &fc,
+ expanded_location exploc,
unsigned int *out,
unsigned int *first_nws,
unsigned int tab_width)
{
- char_span line = location_get_source_line (exploc.file, exploc.line);
+ char_span line = fc.get_source_line (exploc.file, exploc.line);
if (!line)
return false;
if ((size_t)exploc.column > line.length ())
@@ -87,13 +88,14 @@ get_visual_column (expanded_location exploc,
Otherwise, return false, leaving *FIRST_NWS untouched. */
static bool
-get_first_nws_vis_column (const char *file, int line_num,
+get_first_nws_vis_column (file_cache &fc,
+ const char *file, int line_num,
unsigned int *first_nws,
unsigned int tab_width)
{
gcc_assert (first_nws);
- char_span line = location_get_source_line (file, line_num);
+ char_span line = fc.get_source_line (file, line_num);
if (!line)
return false;
unsigned int vis_column = 0;
@@ -158,7 +160,8 @@ get_first_nws_vis_column (const char *file, int line_num,
Return true if such an unindent/outdent is detected. */
static bool
-detect_intervening_unindent (const char *file,
+detect_intervening_unindent (file_cache &fc,
+ const char *file,
int body_line,
int next_stmt_line,
unsigned int vis_column,
@@ -170,7 +173,7 @@ detect_intervening_unindent (const char *file,
for (int line = body_line + 1; line < next_stmt_line; line++)
{
unsigned int line_vis_column;
- if (get_first_nws_vis_column (file, line, &line_vis_column, tab_width))
+ if (get_first_nws_vis_column (fc, file, line, &line_vis_column, tab_width))
if (line_vis_column < vis_column)
return true;
}
@@ -337,6 +340,8 @@ should_warn_for_misleading_indentation (const token_indent_info &guard_tinfo,
if (next_stmt_exploc.file != body_exploc.file)
return false;
+ file_cache &fc = global_dc->get_file_cache ();
+
/* If NEXT_STMT_LOC and BODY_LOC are on the same line, consider
the location of the guard.
@@ -376,7 +381,8 @@ should_warn_for_misleading_indentation (const token_indent_info &guard_tinfo,
gcc_assert (guard_exploc.line == next_stmt_exploc.line);
unsigned int guard_vis_column;
unsigned int guard_line_first_nws;
- if (!get_visual_column (guard_exploc,
+ if (!get_visual_column (fc,
+ guard_exploc,
&guard_vis_column,
&guard_line_first_nws, tab_width))
return false;
@@ -436,15 +442,18 @@ should_warn_for_misleading_indentation (const token_indent_info &guard_tinfo,
the case for input files containing #line directives, and these
are often for autogenerated sources (e.g. from .md files), where
it's not clear that it's meaningful to look at indentation. */
- if (!get_visual_column (next_stmt_exploc,
+ if (!get_visual_column (fc,
+ next_stmt_exploc,
&next_stmt_vis_column,
&next_stmt_line_first_nws, tab_width))
return false;
- if (!get_visual_column (body_exploc,
+ if (!get_visual_column (fc,
+ body_exploc,
&body_vis_column,
&body_line_first_nws, tab_width))
return false;
- if (!get_visual_column (guard_exploc,
+ if (!get_visual_column (fc,
+ guard_exploc,
&guard_vis_column,
&guard_line_first_nws, tab_width))
return false;
@@ -528,7 +537,8 @@ should_warn_for_misleading_indentation (const token_indent_info &guard_tinfo,
/* Don't warn if there is an unindent between the two statements. */
int vis_column = MIN (next_stmt_vis_column, body_vis_column);
- if (detect_intervening_unindent (body_exploc.file, body_exploc.line,
+ if (detect_intervening_unindent (fc,
+ body_exploc.file, body_exploc.line,
next_stmt_exploc.line,
vis_column, tab_width))
return false;
@@ -686,6 +696,7 @@ test_next_tab_stop ()
static void
assert_get_visual_column_succeeds (const location &loc,
+ file_cache &fc,
const char *file, int line, int column,
const unsigned int tab_width,
unsigned int expected_visual_column,
@@ -699,7 +710,8 @@ assert_get_visual_column_succeeds (const location &loc,
exploc.sysp = false;
unsigned int actual_visual_column;
unsigned int actual_first_nws;
- bool result = get_visual_column (exploc,
+ bool result = get_visual_column (fc,
+ exploc,
&actual_visual_column,
&actual_first_nws, tab_width);
ASSERT_TRUE_AT (loc, result);
@@ -710,12 +722,14 @@ assert_get_visual_column_succeeds (const location &loc,
/* Verify that the given call to get_visual_column succeeds, with
the given results. */
-#define ASSERT_GET_VISUAL_COLUMN_SUCCEEDS(FILENAME, LINE, COLUMN, \
+#define ASSERT_GET_VISUAL_COLUMN_SUCCEEDS(FILE_CACHE, \
+ FILENAME, LINE, COLUMN, \
TAB_WIDTH, \
EXPECTED_VISUAL_COLUMN, \
EXPECTED_FIRST_NWS) \
SELFTEST_BEGIN_STMT \
assert_get_visual_column_succeeds (SELFTEST_LOCATION, \
+ FILE_CACHE, \
FILENAME, LINE, COLUMN, \
TAB_WIDTH, \
EXPECTED_VISUAL_COLUMN, \
@@ -726,6 +740,7 @@ assert_get_visual_column_succeeds (const location &loc,
static void
assert_get_visual_column_fails (const location &loc,
+ file_cache &fc,
const char *file, int line, int column,
const unsigned int tab_width)
{
@@ -737,7 +752,8 @@ assert_get_visual_column_fails (const location &loc,
exploc.sysp = false;
unsigned int actual_visual_column;
unsigned int actual_first_nws;
- bool result = get_visual_column (exploc,
+ bool result = get_visual_column (fc,
+ exploc,
&actual_visual_column,
&actual_first_nws, tab_width);
ASSERT_FALSE_AT (loc, result);
@@ -745,10 +761,12 @@ assert_get_visual_column_fails (const location &loc,
/* Verify that the given call to get_visual_column fails gracefully. */
-#define ASSERT_GET_VISUAL_COLUMN_FAILS(FILENAME, LINE, COLUMN, \
+#define ASSERT_GET_VISUAL_COLUMN_FAILS(FILE_CACHE, \
+ FILENAME, LINE, COLUMN, \
TAB_WIDTH) \
SELFTEST_BEGIN_STMT \
assert_get_visual_column_fails (SELFTEST_LOCATION, \
+ FILE_CACHE, \
FILENAME, LINE, COLUMN, \
TAB_WIDTH); \
SELFTEST_END_STMT
@@ -770,6 +788,7 @@ test_get_visual_column ()
"\t line 2\n");
line_table_test ltt;
temp_source_file tmp (SELFTEST_LOCATION, ".txt", content);
+ file_cache fc;
const unsigned int tab_width = 8;
const char *file = tmp.get_filename ();
@@ -777,27 +796,27 @@ test_get_visual_column ()
/* Line 1 (space-based indentation). */
{
const int line = 1;
- ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (file, line, 1, tab_width, 0, 0);
- ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (file, line, 2, tab_width, 1, 1);
- ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (file, line, 3, tab_width, 2, 2);
+ ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (fc, file, line, 1, tab_width, 0, 0);
+ ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (fc, file, line, 2, tab_width, 1, 1);
+ ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (fc, file, line, 3, tab_width, 2, 2);
/* first_nws should have stopped increasing. */
- ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (file, line, 4, tab_width, 3, 2);
+ ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (fc, file, line, 4, tab_width, 3, 2);
/* Verify the end-of-line boundary. */
- ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (file, line, 8, tab_width, 7, 2);
- ASSERT_GET_VISUAL_COLUMN_FAILS (file, line, 9, tab_width);
+ ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (fc, file, line, 8, tab_width, 7, 2);
+ ASSERT_GET_VISUAL_COLUMN_FAILS (fc, file, line, 9, tab_width);
}
/* Line 2 (tab-based indentation). */
{
const int line = 2;
- ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (file, line, 1, tab_width, 0, 0);
- ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (file, line, 2, tab_width, 8, 8);
- ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (file, line, 3, tab_width, 9, 9);
+ ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (fc, file, line, 1, tab_width, 0, 0);
+ ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (fc, file, line, 2, tab_width, 8, 8);
+ ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (fc, file, line, 3, tab_width, 9, 9);
/* first_nws should have stopped increasing. */
- ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (file, line, 4, tab_width, 10, 9);
+ ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (fc, file, line, 4, tab_width, 10, 9);
/* Verify the end-of-line boundary. */
- ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (file, line, 8, tab_width, 14, 9);
- ASSERT_GET_VISUAL_COLUMN_FAILS (file, line, 9, tab_width);
+ ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (fc, file, line, 8, tab_width, 14, 9);
+ ASSERT_GET_VISUAL_COLUMN_FAILS (fc, file, line, 9, tab_width);
}
}