From my experience, the C preprocessor just behaves as no-op when running on a previously preprocessed source. But is this behaviour guaranteed by the standard? Or maybe an implementation could have a preprocessor that modifies previously preprocessed code and for example removes/modifies line directives, or performs other modifications that could confuse the compiler?
In general, preprocessing via cpp
is not guaranteed to be idempotent (a noop after the first run). A simple counterexample:
#define X #define Y z
X
Y
The first invocation will yield:
#define Y z
Y
The second one:
z
Having said that, valid C code shouldn't be doing something like that (because the output wouldn't be valid input for next stages of the compiler).
Moreover, depending on what you are trying to do, cpp
has options like -fpreprocessed
that may help.
Cop is not a c compiler.
@P__JsupportswomeninPoland OP commented that he/she was using
cpp
.I think you mean that it's not guaranteed to be idempotent. It's not guaranteed to be a no-op either, of course, since it's expected that the preprocessor will, in fact, do something (unless the source has no preprocessor directives or mandatory macros). But that's not surprising
@rici That's the proper term, yep. I was trying to use OP's own words. Thanks!
I think there are far more "practical" answers where this doesn't work. One obvious problem is predefined macros; you'd need to be able to turn them all off entirely in order to feed the preprocessed source back in, or any instance of them in the preprocessor output would get double-expanded. I recall seeing/making even better examples but can't think of them right now.