It is necessary to understand how macro replacement works in C, particularly in the context of concatenating tokens using the ##
operator and converting macro parameters to strings using the #
operator.
Concatenating Tokens
The {{\ Wiki Markup ##
}} preprocessing operator is used to merge two tokens into one while expanding macros. This is called token pasting or token concatenation. When a macro is expanded, the two tokens on either side of each ## operator are combined into a single token, which replaces the {{\##
}} and the two original tokens in the macro expansion \[ [FSF 2005|AA. Bibliography#FSF 05]\].
Token pasting is most useful when one or both of the tokens come from a macro argument. If either of the tokens next to an ##
is a parameter name, it is replaced by its actual argument before ##
executes. The actual argument is not macro expanded first.
Stringification
Parameters are not replaced inside string constants, but you can use the {{\ Wiki Markup #
}} preprocessing operator instead. When a macro parameter is used with a leading {{\#
}}, the preprocessor replaces it with the literal text of the actual argument, converted to a string constant \ [[FSF 2005|AA. Bibliography#FSF 05]\].
Noncompliant Code Example
...
Code Block |
---|
#define static_assert(e) \ typedef char JOIN(assertion_failed_at_line_, __LINE__) \ [(e) ? 1 : -1] |
...
{{\_
\_LINE
\_
\_
}} is a predefined macro names which expands to an integer constant representing the presumed line number of the current source line within the current source file \ [[ISO/IEC 9899:1999|AA. Bibliography#ISO/IEC 9899-1999]\].
If the intention is to expand the __LINE__
macro, which is likely the case here, the following definition for JOIN()
is noncompliant:
...
Tool | Version | Checker | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
|
|
...
ISO/IEC 9899:1999 Section 6.10.3, "Macro replacement," Section 6.10.3.3, "The ##
operator," Section 6.10.3.2, "The #
operator," Section 6.10.3.4, "Rescanning and further replacement," and Section 6.10.8, "Predefined macro names"
Bibliography
\[[FSF 2005|AA. Bibliography#FSF 05]\] Section 3.4, "[Stringification|http://gcc.gnu.org/onlinedocs/cpp/Stringification.html]" and Section 3.5, "[Concatenation|http://gcc.gnu.org/onlinedocs/gcc-4.3.0/cpp/Concatenation.html#Concatenation]"
\ Wiki Markup
[[Saks 2008|AA. Bibliography#Saks 08]\]
...
01. Preprocessor (PRE) PRE06-C. Enclose header files in an inclusion guard