Andreas Färber <afaer...@suse.de> writes: > Am 29.01.2013 11:03, schrieb Markus Armbruster: >> Andreas Färber <afaer...@suse.de> writes: >> >>> b) Working around an issue resulting from hiding C statements inside a >>> preprocessor macro is totally backwards compared to properly using the C >>> language in the first place. Its mechanism for reuse are functions, and >>> for performance considerations static inline functions. >> >> Nobody denies inline functions are to be preferred in many cases. >> >> Valid exceptions include cases where we want genericity, which a macro >> can easily provide, but an inline function can't. >> >>> Therefore I disagree with you that b) is not an entirely different >>> disussion as you repeatedly suggest and that according to a) it is not >>> *generally* necessary to put do { ... } while (0) into any random macro >>> that one writes. It depends on the contents and on the context. >> >> I don't think Peter suggested to put do ... while (0) into random >> macros! He asked to put it exactly into *statement-like* macros, as per >> standard C practice. Most macros are expression-like. > > Quoting PMM from last night: > <<< > "do ... while (0)" is completely standard practice > for writing robust macros in C. Patches which don't do that > should fail code review. >>>> > > This is what sparked the most heated part of this debate! I completely > object to this generalized statement without restriction to > statement-like macros.
Well, you quite obviously can't use "do ... while (0)" in any but statement-like macros! If you use it in an expression-like macro, it's no longer an expression. Pointlessly breaks uses in non-statement expression context. If you use it in a declaration-like macro, it's no longer a declaration, and the declaration part goes into its own scope, which tends to be counter-productive. If you use it in a file-level macro, you get syntax errors. >> I think you're overstating your case. Statement-like macros should be >> rare. > > This RFC was just a beginning. An average 50%+ of per target-*/ files > contained macros that were touched here. Even more files are in hw/. Per > file it was up to ~5 such macro definitions. And my main issue is > deriving a convention for new files from this discussion. Stick to established C practice: 1. Avoid statement-like macros when there are superior alternatives, like inline functions, or expression-like macros. 2. If you have to use a statement-like macro, ensure it expands into a complete statement regardless of context. The idiomatic technique for that is "do ... while (0)".