Multi-statement macros (again)

Martin Weitzel martin at mwtech.UUCP
Thu Feb 22 01:35:17 AEST 1990


In article <17000001 at hpcvra.CV.HP.COM> frankw at hpcvra.CV.HP.COM (Frank Wales) writes:
} In some article, sullivan at aqdata.uucp (Michael T. Sullivan) wrote:
} [an article about writing macros which do things that are forbidden
}  in expressions, like using return, which concluded with...]
} 
} >I then changed BEGIN_ and END_BLOCK to:
} 
} >#define	BEGIN_BLOCK	do { if (1) {
} >#define	END_BLOCK	} } while (0)
} 
} >This seems to make everything happy.  My question is, will this make other
} >machines happy?  We are using a 3B2/400 SVR3 and it didn't even complain
} >when just braces were used (resulting in "};").  Does this shut everything
} >up on YOUR machine?
} 
} I have used the do { ... } while(0) trick on many occasions to write macros
} which declare temporary variables.  This produces a warning of the form
} "constant in conditional context" when compiled on an HP9000 PA machine
} (s600 or s800), which I live with (a compiler directive to get rid of
} it on a case-by-case basis would be useful, but I'm not that bothered;
} I would definitely not want an option which just lost all these warnings).
} --
} Frank Wales, Guest of HP Corvallis, [frank at zen.co.uk||frankw at hpcvdq.cv.hp.com]
} Zengrange Ltd., Greenfield Rd., LEEDS, England, LS9 8DB. (+44) 532 489048 x217

Compiler writers, here you have another argument for separating
'cc'-ing and 'lint'-ing some source. Then it would be easy to put
an #ifdef-ed wrapper around those bogus but sometimes necessary
stuff, which makes such macros look like functions while the source is
'lint'-ed.

To Frank: Here you have my hint what to do (esp. if you use make
for compilations): Disable *all* warnings when you do the 'real'
compilation but have some additional target. In this case you may
use lint or let your compiler issue all warnings. If you use lint,
the macro 'lint' will automatically be defined, otherwise you must
call your compiler simply with "-Dlint". Then let your multi-statement
macros appears as if they were functions, depending on #ifdef lint.
-- 
Martin Weitzel, email: martin at mwtech.UUCP, voice: 49-(0)6151-6 56 83
-- 
Martin Weitzel, email: martin at mwtech.UUCP, voice: 49-(0)6151-6 56 83



More information about the Comp.lang.c mailing list