diff options
author | Patrick Palka <ppalka@gcc.gnu.org> | 2015-08-02 17:35:33 +0000 |
---|---|---|
committer | Patrick Palka <ppalka@gcc.gnu.org> | 2015-08-02 17:35:33 +0000 |
commit | 1a1e101ff50564a44dcd12522236f04caaa6dcab (patch) | |
tree | 00619fb5fac630edad177b21a9a582673145f26c /gcc | |
parent | 992118a1f9192614d3916e112e3e9a833d00566c (diff) | |
download | gcc-1a1e101ff50564a44dcd12522236f04caaa6dcab.zip gcc-1a1e101ff50564a44dcd12522236f04caaa6dcab.tar.gz gcc-1a1e101ff50564a44dcd12522236f04caaa6dcab.tar.bz2 |
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.
From-SVN: r226478
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/c-family/ChangeLog | 8 | ||||
-rw-r--r-- | 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 8dfc81b..bb74de9 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,5 +1,13 @@ 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. (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 544b0d4..cdf0372 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; } } |