diff options
author | Marek Polacek <polacek@redhat.com> | 2017-06-26 10:02:27 +0000 |
---|---|---|
committer | Marek Polacek <mpolacek@gcc.gnu.org> | 2017-06-26 10:02:27 +0000 |
commit | 3e2becc495c2ed8e48b055707430c8204826d7bf (patch) | |
tree | eb5ce085c45fa8f538a1c2ea1da0f6b4aee02485 /gcc/doc/invoke.texi | |
parent | 945653db613d34683696341759c4b070600b350c (diff) | |
download | gcc-3e2becc495c2ed8e48b055707430c8204826d7bf.zip gcc-3e2becc495c2ed8e48b055707430c8204826d7bf.tar.gz gcc-3e2becc495c2ed8e48b055707430c8204826d7bf.tar.bz2 |
re PR c/80116 (Warn about macros expanding to multiple statements)
PR c/80116
* c-common.h (warn_for_multistatement_macros): Declare.
* c-warn.c: Include "c-family/c-indentation.h".
(warn_for_multistatement_macros): New function.
* c.opt (Wmultistatement-macros): New option.
* c-indentation.c (guard_tinfo_to_string): No longer static.
Change the parameter type to "enum rid". Handle RID_SWITCH.
* c-indentation.h (guard_tinfo_to_string): Declare.
* c-parser.c (c_parser_if_body): Set the location of the
body of the conditional after parsing all the labels. Call
warn_for_multistatement_macros.
(c_parser_else_body): Likewise.
(c_parser_switch_statement): Likewise.
(c_parser_while_statement): Likewise.
(c_parser_for_statement): Likewise.
(c_parser_statement): Add a default argument. Save the location
after labels have been parsed.
(c_parser_c99_block_statement): Likewise.
* parser.c (cp_parser_statement): Add a default argument. Save the
location of the expression-statement after labels have been parsed.
(cp_parser_implicitly_scoped_statement): Set the location of the
body of the conditional after parsing all the labels. Call
warn_for_multistatement_macros.
(cp_parser_already_scoped_statement): Likewise.
* doc/invoke.texi: Document -Wmultistatement-macros.
* c-c++-common/Wmultistatement-macros-1.c: New test.
* c-c++-common/Wmultistatement-macros-2.c: New test.
* c-c++-common/Wmultistatement-macros-3.c: New test.
* c-c++-common/Wmultistatement-macros-4.c: New test.
* c-c++-common/Wmultistatement-macros-5.c: New test.
* c-c++-common/Wmultistatement-macros-6.c: New test.
* c-c++-common/Wmultistatement-macros-7.c: New test.
* c-c++-common/Wmultistatement-macros-8.c: New test.
* c-c++-common/Wmultistatement-macros-9.c: New test.
* c-c++-common/Wmultistatement-macros-10.c: New test.
* c-c++-common/Wmultistatement-macros-11.c: New test.
From-SVN: r249643
Diffstat (limited to 'gcc/doc/invoke.texi')
-rw-r--r-- | gcc/doc/invoke.texi | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index e4ca1b4..d1e097b 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -294,7 +294,7 @@ Objective-C and Objective-C++ Dialects}. -Wmain -Wmaybe-uninitialized -Wmemset-elt-size -Wmemset-transposed-args @gol -Wmisleading-indentation -Wmissing-braces @gol -Wmissing-field-initializers -Wmissing-include-dirs @gol --Wno-multichar -Wnonnull -Wnonnull-compare @gol +-Wno-multichar -Wmultistatement-macros -Wnonnull -Wnonnull-compare @gol -Wnormalized=@r{[}none@r{|}id@r{|}nfc@r{|}nfkc@r{]} @gol -Wnull-dereference -Wodr -Wno-overflow -Wopenmp-simd @gol -Woverride-init-side-effects -Woverlength-strings @gol @@ -3842,6 +3842,7 @@ Options} and @ref{Objective-C and Objective-C++ Dialect Options}. -Wmemset-transposed-args @gol -Wmisleading-indentation @r{(only for C/C++)} @gol -Wmissing-braces @r{(only for C/ObjC)} @gol +-Wmultistatement-macros @gol -Wnarrowing @r{(only for C++)} @gol -Wnonnull @gol -Wnonnull-compare @gol @@ -4514,6 +4515,32 @@ This warning is enabled by @option{-Wall}. @opindex Wno-missing-include-dirs Warn if a user-supplied include directory does not exist. +@item -Wmultistatement-macros +@opindex Wmultistatement-macros +@opindex Wno-multistatement-macros +Warn about unsafe multiple statement macros that appear to be guarded +by a clause such as @code{if}, @code{else}, @code{for}, @code{switch}, or +@code{while}, in which only the first statement is actually guarded after +the macro is expanded. + +For example: + +@smallexample +#define DOIT x++; y++ +if (c) + DOIT; +@end smallexample + +will increment @code{y} unconditionally, not just when @code{c} holds. +The can usually be fixed by wrapping the macro in a do-while loop: +@smallexample +#define DOIT do @{ x++; y++; @} while (0) +if (c) + DOIT; +@end smallexample + +This warning is enabled by @option{-Wall} in C and C++. + @item -Wparentheses @opindex Wparentheses @opindex Wno-parentheses |