This is the mail archive of the
mailing list for the GCC project.
Could preprocessor warn for unsafe macros and side-effects?
- From: "sarah at hederstierna dot com" <fredrik at hederstierna dot com>
- To: "gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>
- Date: Wed, 12 Jul 2017 05:50:06 +0000
- Subject: Could preprocessor warn for unsafe macros and side-effects?
- Authentication-results: sourceware.org; auth=none
- References: <263ef4cd6f55417e97d180530b2e64ba@DAG03.HMC.local>
Reading about macro pitfalls and eg duplication side-effects
would it be possible to let the preprocessor generate warnings for any of these pitfalls?
Maybe all language specific parts are not know at this early preprocessing stage, but possibly some info could be stored for use in later pass?
I'm thinking of eg. for "function-like macros" with arguments, checking
* IF function-like macro expands/duplicates an argument more than once THEN
WARN if function() is part as the argument
WARN if unary ++ or -- is used on variable as part of argument
WARN if assignment operator = is part of argument
WARN if volatile variable part as the argument
* WARN if macro argument contains an expression with operator(s), an a _higher_ precedence operator is used within the macro on this argument, without parenthesis around
I'm not sure its even possible at preprocessing stage, but it would be nice to have,
I saw some static code analysis tools like Coverity detects these
Of course it might generate some false-positives so warning might not be enabled by default, maybe just -Wall or -Wextra,
but perhaps it hard to solve, and I'm not sure where and how to implement the checking algorithm.
Thanks for any feedback!