From c589e975638b217501e4e868e7d706bb318e18e9 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Tue, 5 Jan 2016 15:54:46 +0000 Subject: [PATCH] Fix PR c/69122 (-Wmisleading-indentation false positive with empty macros) gcc/c-family/ChangeLog: PR c/69122 * c-indentation.c (get_visual_column): Remove default argument. (should_warn_for_misleading_indentation): For the multiline case, update call to get_visual_column for next_stmt_exploc so that it captures the location of the first non-whitespace character in the relevant line. Don't issue warnings if there is non-whitespace before the next statement. gcc/testsuite/ChangeLog: PR c/69122 * c-c++-common/Wmisleading-indentation.c (pr69122): New function. From-SVN: r232076 --- gcc/c-family/ChangeLog | 10 ++++++++ gcc/c-family/c-indentation.c | 24 +++++++++++++++++-- gcc/testsuite/ChangeLog | 5 ++++ .../c-c++-common/Wmisleading-indentation.c | 10 ++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index 6a4188b29df..7bae1a7649d 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,13 @@ +2016-01-05 David Malcolm + + PR c/69122 + * c-indentation.c (get_visual_column): Remove default argument. + (should_warn_for_misleading_indentation): For the multiline case, + update call to get_visual_column for next_stmt_exploc so that it + captures the location of the first non-whitespace character in the + relevant line. Don't issue warnings if there is non-whitespace + before the next statement. + 2016-01-04 Jakub Jelinek Update copyright years. diff --git a/gcc/c-family/c-indentation.c b/gcc/c-family/c-indentation.c index 2f857f79709..3c09336d15a 100644 --- a/gcc/c-family/c-indentation.c +++ b/gcc/c-family/c-indentation.c @@ -38,7 +38,7 @@ extern cpp_options *cpp_opts; static bool get_visual_column (expanded_location exploc, unsigned int *out, - unsigned int *first_nws = NULL) + unsigned int *first_nws) { int line_len; const char *line = location_get_source_line (exploc.file, exploc.line, @@ -329,12 +329,20 @@ should_warn_for_misleading_indentation (const token_indent_info &guard_tinfo, ; foo (); ^ DON'T WARN HERE + + #define emit + if (flag) + foo (); + emit bar (); + ^ DON'T WARN HERE + */ if (next_stmt_exploc.line > body_exploc.line) { /* Determine if GUARD_LOC and NEXT_STMT_LOC are aligned on the same "visual column"... */ unsigned int next_stmt_vis_column; + unsigned int next_stmt_line_first_nws; unsigned int body_vis_column; unsigned int body_line_first_nws; unsigned int guard_vis_column; @@ -343,7 +351,8 @@ 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, &next_stmt_vis_column)) + if (!get_visual_column (next_stmt_exploc, &next_stmt_vis_column, + &next_stmt_line_first_nws)) return false; if (!get_visual_column (body_exploc, &body_vis_column, @@ -354,6 +363,17 @@ should_warn_for_misleading_indentation (const token_indent_info &guard_tinfo, &guard_line_first_nws)) return false; + /* If the line where the next stmt starts has non-whitespace + on it before the stmt, then don't warn: + #define emit + if (flag) + foo (); + emit bar (); + ^ DON'T WARN HERE + (PR c/69122). */ + if (next_stmt_line_first_nws < next_stmt_vis_column) + return false; + if ((body_type != CPP_SEMICOLON && next_stmt_vis_column == body_vis_column) /* As a special case handle the case where the body is a semicolon diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index e231bbc7190..9ff53ae6c11 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2016-01-05 David Malcolm + + PR c/69122 + * c-c++-common/Wmisleading-indentation.c (pr69122): New function. + 2016-01-05 Nathan Sidwell PR c++/58583 diff --git a/gcc/testsuite/c-c++-common/Wmisleading-indentation.c b/gcc/testsuite/c-c++-common/Wmisleading-indentation.c index a3f5acdd533..491291c171c 100644 --- a/gcc/testsuite/c-c++-common/Wmisleading-indentation.c +++ b/gcc/testsuite/c-c++-common/Wmisleading-indentation.c @@ -891,3 +891,13 @@ fn_39 (void) i++); foo (i); } + +/* We shouldn't complain about the following function. */ +#define emit +void pr69122 (void) +{ + if (flagA) + foo (0); + emit foo (1); +} +#undef emit