Discussion:
Generating all possible codes for a conditinal compilation that uses cl.exe
(too old to reply)
Sin Jeong-hun
2009-11-11 02:45:32 UTC
Permalink
Hello.

At our company, we use preprocessor directives excessively for
conditional compilation. The language is not C, but we use Microsoft's
cl.exe just for preprocessing purposes. We mostly use simple #if,
#define or #ifdef. The problem is, we cannot, at least, check for
syntactical errors for all the possible codes that could be generated
after the preprocessing. Sometimes very simple syntactical errors such
as undeclared variables or mismatching control structures are found
later on.

I was thinking about writing my own C preprocessor to generate all
possible result codes, but as I am often told I don't need to reinvent
the wheel right? I thought the problem may also be true for general C/C
++, not only for our situation. Is it somehow possible to make the
cl.exe generate all possible preprocessed output?
Igor Tandetnik
2009-11-11 04:41:20 UTC
Permalink
Post by Sin Jeong-hun
At our company, we use preprocessor directives excessively for
conditional compilation. The language is not C, but we use Microsoft's
cl.exe just for preprocessing purposes. We mostly use simple #if,
#define or #ifdef. The problem is, we cannot, at least, check for
syntactical errors for all the possible codes that could be generated
after the preprocessing. Sometimes very simple syntactical errors such
as undeclared variables or mismatching control structures are found
later on.
I was thinking about writing my own C preprocessor to generate all
possible result codes, but as I am often told I don't need to reinvent
the wheel right? I thought the problem may also be true for general C/C
++, not only for our situation. Is it somehow possible to make the
cl.exe generate all possible preprocessed output?
Even if such a thing were possible in general, it would be impractical. For one thing, the size of the output may grow exponentially in the size of the input. Consider:

char* s =
#ifdef VAR_1
"1"
#else
"0"
#endif
#ifdef VAR_2
"1"
#else
"0"
#endif
...
#ifdef VAR_1000
"1"
#else
"0"
#endif
;

The size of this file is some multiple of 1000, but it could produce 2^1000 posible different preprocessed files.

Or, consider this:

#if X*X < 0
blah blah blah
#endif

Should your "guessing" preprocessor, given this input, produce a (clearly syntactically invalid) file containing "blah blah blah"? A real preprocessor won't, regardless of how X is defined.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Ulrich Eckhardt
2009-11-11 07:58:44 UTC
Permalink
Post by Sin Jeong-hun
I was thinking about writing my own C preprocessor to generate all
possible result codes, but as I am often told I don't need to reinvent
the wheel right?
Take a look at Boost.Wave[1], both the executable and the library.

Uli

[1] http://www.boost.org/doc/libs/
--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
Loading...