[Bug c/80116] Warn about macros expanding to multiple statements
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80116 Marek Polacek changed: What|Removed |Added Status|ASSIGNED|RESOLVED Resolution|--- |FIXED --- Comment #6 from Marek Polacek --- Done for GCC 8.
[Bug c/80116] Warn about macros expanding to multiple statements
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80116 --- Comment #5 from Marek Polacek --- Author: mpolacek Date: Mon Jun 26 10:02:27 2017 New Revision: 249643 URL: https://gcc.gnu.org/viewcvs?rev=249643=gcc=rev Log: 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. Added: trunk/gcc/testsuite/c-c++-common/Wmultistatement-macros-1.c trunk/gcc/testsuite/c-c++-common/Wmultistatement-macros-10.c trunk/gcc/testsuite/c-c++-common/Wmultistatement-macros-11.c trunk/gcc/testsuite/c-c++-common/Wmultistatement-macros-2.c trunk/gcc/testsuite/c-c++-common/Wmultistatement-macros-3.c trunk/gcc/testsuite/c-c++-common/Wmultistatement-macros-4.c trunk/gcc/testsuite/c-c++-common/Wmultistatement-macros-5.c trunk/gcc/testsuite/c-c++-common/Wmultistatement-macros-6.c trunk/gcc/testsuite/c-c++-common/Wmultistatement-macros-7.c trunk/gcc/testsuite/c-c++-common/Wmultistatement-macros-8.c trunk/gcc/testsuite/c-c++-common/Wmultistatement-macros-9.c Modified: trunk/gcc/ChangeLog trunk/gcc/c-family/ChangeLog trunk/gcc/c-family/c-common.h trunk/gcc/c-family/c-indentation.c trunk/gcc/c-family/c-indentation.h trunk/gcc/c-family/c-warn.c trunk/gcc/c-family/c.opt trunk/gcc/c/ChangeLog trunk/gcc/c/c-parser.c trunk/gcc/cp/ChangeLog trunk/gcc/cp/parser.c trunk/gcc/doc/invoke.texi trunk/gcc/testsuite/ChangeLog
[Bug c/80116] Warn about macros expanding to multiple statements
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80116 Marek Polacek changed: What|Removed |Added Status|NEW |ASSIGNED Assignee|unassigned at gcc dot gnu.org |mpolacek at gcc dot gnu.org --- Comment #4 from Marek Polacek --- I got a prototype patch.
[Bug c/80116] Warn about macros expanding to multiple statements
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80116 --- Comment #3 from Marek Polacek --- A testcase: #define SWAP(x, y) \ tmp = x; \ x = y; \ y = tmp int x, y, tmp; void fn1 (void) { if (x) SWAP(x, y); // warn } void fn2 (void) { SWAP(x, y); } void fn3 (void) { if (x) { SWAP(x, y); } } void fn4 (void) { if (x) x = 10; else SWAP(x, y); // warn }
[Bug c/80116] Warn about macros expanding to multiple statements
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80116 Eric Gallager changed: What|Removed |Added CC||egall at gwmail dot gwu.edu --- Comment #2 from Eric Gallager --- (In reply to Martin Sebor from comment #1) > Confirmed. The CERT C Coding Standard outlines a number of common problems > with macros that might be worth considering for inclusion in the proposed > warning. The following is the subject of this enhancement request: > > PRE10-C. Wrap multistatement macros in a do-while loop > https://www.securecoding.cert.org/confluence/x/jgL7 > > Some other checkers: > > PRE01-C. Use parentheses within macros around parameter names > https://www.securecoding.cert.org/confluence/x/CgU > > PRE02-C. Macro replacement lists should be parenthesized > https://www.securecoding.cert.org/confluence/x/HAs > > PRE11-C. Do not conclude macro definitions with a semicolon > https://www.securecoding.cert.org/confluence/x/wgBlAQ > > PRE12-C. Do not define unsafe macros > https://www.securecoding.cert.org/confluence/x/TIF3Ag > > PRE31-C. Avoid side effects in arguments to unsafe macros > https://www.securecoding.cert.org/confluence/x/agBi Many of these are also in the CPP manual at: https://gcc.gnu.org/onlinedocs/cpp/Macro-Pitfalls.html#Macro-Pitfalls (even though no actual warning is output for any of them yet)
[Bug c/80116] Warn about macros expanding to multiple statements
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80116 Martin Sebor changed: What|Removed |Added Keywords||diagnostic Status|UNCONFIRMED |NEW Last reconfirmed||2017-03-20 CC||msebor at gcc dot gnu.org Ever confirmed|0 |1 Severity|normal |enhancement --- Comment #1 from Martin Sebor --- Confirmed. The CERT C Coding Standard outlines a number of common problems with macros that might be worth considering for inclusion in the proposed warning. The following is the subject of this enhancement request: PRE10-C. Wrap multistatement macros in a do-while loop https://www.securecoding.cert.org/confluence/x/jgL7 Some other checkers: PRE01-C. Use parentheses within macros around parameter names https://www.securecoding.cert.org/confluence/x/CgU PRE02-C. Macro replacement lists should be parenthesized https://www.securecoding.cert.org/confluence/x/HAs PRE11-C. Do not conclude macro definitions with a semicolon https://www.securecoding.cert.org/confluence/x/wgBlAQ PRE12-C. Do not define unsafe macros https://www.securecoding.cert.org/confluence/x/TIF3Ag PRE31-C. Avoid side effects in arguments to unsafe macros https://www.securecoding.cert.org/confluence/x/agBi