From f95bfdd4a25cfa2862cce383cde5040b63752be6 Mon Sep 17 00:00:00 2001 From: ppalka Date: Sun, 2 Aug 2015 17:35:33 +0000 Subject: [PATCH] Remove is_first_nonwhitespace_on_line(), instead improve get_visual_column() gcc/c-family/ChangeLog: * 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. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@226478 138bc75d-0d04-0410-961f-82ee72b054a4 --- gcc/c-family/ChangeLog | 8 +++++++ gcc/c-family/c-indentation.c | 53 ++++++++++++++++++++------------------------ 2 files changed, 32 insertions(+), 29 deletions(-) diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index 8dfc81bdee9..bb74de9935a 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,5 +1,13 @@ 2015-08-02 Patrick Palka + * 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 + * c-indentation.h (struct token_indent_info): Define. (get_token_indent_info): Define. (warn_for_misleading_information): Declare. diff --git a/gcc/c-family/c-indentation.c b/gcc/c-family/c-indentation.c index 544b0d43739..cdf0372aed3 100644 --- a/gcc/c-family/c-indentation.c +++ b/gcc/c-family/c-indentation.c @@ -33,11 +33,16 @@ extern cpp_options *cpp_opts; /* 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); @@ -47,6 +52,13 @@ get_visual_column (expanded_location exploc, unsigned int *out) 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. */ @@ -57,33 +69,10 @@ get_visual_column (expanded_location exploc, unsigned int *out) 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; } @@ -279,9 +268,15 @@ should_warn_for_misleading_indentation (const token_indent_info &guard_tinfo, /* 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; } } -- 2.11.4.GIT