aboutsummaryrefslogtreecommitdiff
path: root/gcc/c-family
diff options
context:
space:
mode:
authorPatrick Palka <ppalka@gcc.gnu.org>2015-08-02 17:31:55 +0000
committerPatrick Palka <ppalka@gcc.gnu.org>2015-08-02 17:31:55 +0000
commit992118a1f9192614d3916e112e3e9a833d00566c (patch)
tree4382b53b305ac8e976613091b7987f093d40123f /gcc/c-family
parentfea8f6c692a091dd9e7639949e45ca7445fb53a0 (diff)
downloadgcc-992118a1f9192614d3916e112e3e9a833d00566c.zip
gcc-992118a1f9192614d3916e112e3e9a833d00566c.tar.gz
gcc-992118a1f9192614d3916e112e3e9a833d00566c.tar.bz2
Refactor entry point to -Wmisleading-indentation
gcc/c-family/ChangeLog: * c-indentation.h (struct token_indent_info): Define. (get_token_indent_info): Define. (warn_for_misleading_information): Declare. * c-common.h (warn_for_misleading_information): Remove. * c-identation.c (warn_for_misleading_indentation): Change declaration to take three token_indent_infos. Adjust accordingly. * c-identation.c (should_warn_for_misleading_indentation): Likewise. Bail out early if the body is a compound statement. (guard_tinfo_to_string): Define. gcc/c/ChangeLog: * c-parser.c (c_parser_if_body): Take token_indent_info argument. Call warn_for_misleading_indentation even when the body is a semicolon. Extract token_indent_infos corresponding to the guard, body and next tokens. Adjust call to warn_for_misleading_indentation accordingly. (c_parser_else_body): Likewise. (c_parser_if_statement): Likewise. (c_parser_while_statement): Likewise. (c_parser_for_statement): Likewise. gcc/cp/ChangeLog: * parser.c (cp_parser_selection_statement): Move handling of semicolon body to ... (cp_parser_implicitly_scoped_statement): .. here. Call warn_for_misleading_indentation even when the body is a semicolon. Extract token_indent_infos corresponding to the guard, body and next tokens. Adjust call to warn_for_misleading_indentation accordingly. Take token_indent_info argument. (cp_parser_already_scoped_statement): Likewise. (cp_parser_selection_statement, cp_parser_iteration_statement): Extract a token_indent_info corresponding to the guard token. From-SVN: r226477
Diffstat (limited to 'gcc/c-family')
-rw-r--r--gcc/c-family/ChangeLog13
-rw-r--r--gcc/c-family/c-common.h7
-rw-r--r--gcc/c-family/c-indentation.c79
-rw-r--r--gcc/c-family/c-indentation.h52
4 files changed, 126 insertions, 25 deletions
diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog
index 9ab9551..8dfc81b 100644
--- a/gcc/c-family/ChangeLog
+++ b/gcc/c-family/ChangeLog
@@ -1,3 +1,16 @@
+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.
+ * c-common.h (warn_for_misleading_information): Remove.
+ * c-identation.c (warn_for_misleading_indentation):
+ Change declaration to take three token_indent_infos. Adjust
+ accordingly.
+ * c-identation.c (should_warn_for_misleading_indentation):
+ Likewise. Bail out early if the body is a compound statement.
+ (guard_tinfo_to_string): Define.
+
2015-07-30 Jason Merrill <jason@redhat.com>
* c-pretty-print.c (unary_expression) [INDIRECT_REF]: Don't print
diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h
index f0640c7..ff74e53 100644
--- a/gcc/c-family/c-common.h
+++ b/gcc/c-family/c-common.h
@@ -1431,12 +1431,5 @@ extern bool contains_cilk_spawn_stmt (tree);
extern tree cilk_for_number_of_iterations (tree);
extern bool check_no_cilk (tree, const char *, const char *,
location_t loc = UNKNOWN_LOCATION);
-/* In c-indentation.c. */
-extern void
-warn_for_misleading_indentation (location_t guard_loc,
- location_t body_loc,
- location_t next_stmt_loc,
- enum cpp_ttype next_tok_type,
- const char *guard_kind);
#endif /* ! GCC_C_COMMON_H */
diff --git a/gcc/c-family/c-indentation.c b/gcc/c-family/c-indentation.c
index 252467e..544b0d4 100644
--- a/gcc/c-family/c-indentation.c
+++ b/gcc/c-family/c-indentation.c
@@ -26,6 +26,7 @@ along with GCC; see the file COPYING3. If not see
#include "stringpool.h"
#include "stor-layout.h"
#include "c-common.h"
+#include "c-indentation.h"
extern cpp_options *cpp_opts;
@@ -184,11 +185,16 @@ detect_preprocessor_logic (expanded_location body_exploc,
description of that function below. */
static bool
-should_warn_for_misleading_indentation (location_t guard_loc,
- location_t body_loc,
- location_t next_stmt_loc,
- enum cpp_ttype next_tok_type)
+should_warn_for_misleading_indentation (const token_indent_info &guard_tinfo,
+ const token_indent_info &body_tinfo,
+ const token_indent_info &next_tinfo)
{
+ location_t guard_loc = guard_tinfo.location;
+ location_t body_loc = body_tinfo.location;
+ location_t next_stmt_loc = next_tinfo.location;
+
+ enum cpp_ttype next_tok_type = next_tinfo.type;
+
/* Don't attempt to compare the indentation of BODY_LOC and NEXT_STMT_LOC
if either are within macros. */
if (linemap_location_from_macro_expansion_p (line_table, body_loc)
@@ -214,7 +220,22 @@ should_warn_for_misleading_indentation (location_t guard_loc,
if (line_table->seen_line_directive)
return false;
- if (next_tok_type == CPP_CLOSE_BRACE)
+ /* If the token following the body is a close brace or an "else"
+ then while indentation may be sloppy, there is not much ambiguity
+ about control flow, e.g.
+
+ if (foo) <- GUARD
+ bar (); <- BODY
+ else baz (); <- NEXT
+
+ {
+ while (foo) <- GUARD
+ bar (); <- BODY
+ } <- NEXT
+ baz ();
+ */
+ if (next_tok_type == CPP_CLOSE_BRACE
+ || next_tinfo.keyword == RID_ELSE)
return false;
/* Don't warn here about spurious semicolons. */
@@ -341,6 +362,28 @@ should_warn_for_misleading_indentation (location_t guard_loc,
return false;
}
+/* Return the string identifier corresponding to the given guard token. */
+
+static const char *
+guard_tinfo_to_string (const token_indent_info &guard_tinfo)
+{
+ switch (guard_tinfo.keyword)
+ {
+ case RID_FOR:
+ return "for";
+ case RID_ELSE:
+ return "else";
+ case RID_IF:
+ return "if";
+ case RID_WHILE:
+ return "while";
+ case RID_DO:
+ return "do";
+ default:
+ gcc_unreachable ();
+ }
+}
+
/* Called by the C/C++ frontends when we have a guarding statement at
GUARD_LOC containing a statement at BODY_LOC, where the block wasn't
written using braces, like this:
@@ -368,11 +411,9 @@ should_warn_for_misleading_indentation (location_t guard_loc,
GUARD_KIND identifies the kind of clause e.g. "if", "else" etc. */
void
-warn_for_misleading_indentation (location_t guard_loc,
- location_t body_loc,
- location_t next_stmt_loc,
- enum cpp_ttype next_tok_type,
- const char *guard_kind)
+warn_for_misleading_indentation (const token_indent_info &guard_tinfo,
+ const token_indent_info &body_tinfo,
+ const token_indent_info &next_tinfo)
{
/* Early reject for the case where -Wmisleading-indentation is disabled,
to avoid doing work only to have the warning suppressed inside the
@@ -380,12 +421,14 @@ warn_for_misleading_indentation (location_t guard_loc,
if (!warn_misleading_indentation)
return;
- if (should_warn_for_misleading_indentation (guard_loc,
- body_loc,
- next_stmt_loc,
- next_tok_type))
- if (warning_at (next_stmt_loc, OPT_Wmisleading_indentation,
- "statement is indented as if it were guarded by..."))
- inform (guard_loc,
- "...this %qs clause, but it is not", guard_kind);
+ if (should_warn_for_misleading_indentation (guard_tinfo,
+ body_tinfo,
+ next_tinfo))
+ {
+ if (warning_at (next_tinfo.location, OPT_Wmisleading_indentation,
+ "statement is indented as if it were guarded by..."))
+ inform (guard_tinfo.location,
+ "...this %qs clause, but it is not",
+ guard_tinfo_to_string (guard_tinfo));
+ }
}
diff --git a/gcc/c-family/c-indentation.h b/gcc/c-family/c-indentation.h
new file mode 100644
index 0000000..7fb6bb4
--- /dev/null
+++ b/gcc/c-family/c-indentation.h
@@ -0,0 +1,52 @@
+/* Definitions for c-indentation.c.
+ Copyright (C) 2015 Free Software Foundation, Inc.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 3, or (at your option) any later
+version.
+
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3. If not see
+<http://www.gnu.org/licenses/>. */
+
+#ifndef GCC_C_INDENTATION_H
+#define GCC_C_INDENTATION_H
+
+/* Token information used by the -Wmisleading-indentation implementation. */
+
+struct token_indent_info
+{
+ location_t location;
+ ENUM_BITFIELD (cpp_ttype) type : 8;
+ ENUM_BITFIELD (rid) keyword : 8;
+};
+
+/* Extract token information from TOKEN, which ought to either be a
+ cp_token * or a c_token *. */
+
+template <typename T>
+inline token_indent_info
+get_token_indent_info (const T *token)
+{
+ token_indent_info info;
+ info.location = token->location;
+ info.type = token->type;
+ info.keyword = token->keyword;
+
+ return info;
+}
+
+extern void
+warn_for_misleading_indentation (const token_indent_info &guard_tinfo,
+ const token_indent_info &body_tinfo,
+ const token_indent_info &next_tinfo);
+
+#endif /* ! GCC_C_INDENTATION_H */