+2015-08-02 Patrick Palka <ppalka@gcc.gnu.org>
+
+ * c-indentation.c (get_visual_column): Add parameter first_nws,
+ use it. Update comment documenting the function.
+ (is_first_nonwhitespace_on_line): Remove.
+ (should_warn_for_misleading_indentation): Replace usage of
+ of is_first_nonwhitespace_on_line with get_visual_column.
+
2015-08-02 Patrick Palka <ppalka@gcc.gnu.org>
* c-indentation.h (struct token_indent_info): Define.
/* Convert libcpp's notion of a column (a 1-based char count) to
the "visual column" (0-based column, respecting tabs), by reading the
relevant line.
+
Returns true if a conversion was possible, writing the result to OUT,
- otherwise returns false. */
+ otherwise returns false. If FIRST_NWS is not NULL, then write to it
+ the visual column corresponding to the first non-whitespace character
+ on the line. */
static bool
-get_visual_column (expanded_location exploc, unsigned int *out)
+get_visual_column (expanded_location exploc,
+ unsigned int *out,
+ unsigned int *first_nws = NULL)
{
int line_len;
const char *line = location_get_source_line (exploc, &line_len);
for (int i = 1; i < exploc.column; i++)
{
unsigned char ch = line[i - 1];
+
+ if (first_nws != NULL && !ISSPACE (ch))
+ {
+ *first_nws = vis_column;
+ first_nws = NULL;
+ }
+
if (ch == '\t')
{
/* Round up to nearest tab stop. */
vis_column++;
}
- *out = vis_column;
- return true;
-}
-
-/* Is the token at LOC the first non-whitespace on its line?
- Helper function for should_warn_for_misleading_indentation. */
+ if (first_nws != NULL)
+ *first_nws = vis_column;
-static bool
-is_first_nonwhitespace_on_line (expanded_location exploc)
-{
- int line_len;
- const char *line = location_get_source_line (exploc, &line_len);
-
- /* If we can't determine it, return false so that we don't issue a
- warning. This is sometimes 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 (!line)
- return false;
-
- for (int i = 1; i < exploc.column; i++)
- {
- unsigned char ch = line[i - 1];
- if (!ISSPACE (ch))
- return false;
- }
+ *out = vis_column;
return true;
}
/* They're all on the same line. */
gcc_assert (guard_exploc.file == next_stmt_exploc.file);
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,
+ &guard_vis_column,
+ &guard_line_first_nws))
+ return false;
/* Heuristic: only warn if the guard is the first thing
on its line. */
- if (is_first_nonwhitespace_on_line (guard_exploc))
+ if (guard_vis_column == guard_line_first_nws)
return true;
}
}