C Macros textually substituting for preprocessor directives. 
Author Message
 C Macros textually substituting for preprocessor directives.

Let me be as direct as possible, I want to be able to centralize common
preprocessor statements within a macro. Then using parameters I passed
in have them substituted (see example i). Currently I'm going about
doing this by using the concatenation operator and the trigraph
statements to produce another header file with the correct formatting of
the preprocessor statements (ii). The problem is the header file
generated from step ii doesn't have the correct formatting. The trigraph
concatenation works for the first pound and PREPROCESSOR token (iii),
but not for anyones following it. So I'm really wondering why the first
substitution for the trigraph and token work together the first time,
but not the second. The next major problem is the formatting of the
output from cl /E, where it brings adjoining lines together from the  \  
symbol (iv).  I understand this is correct formatting in regards to the
preprocessor, but I was hoping that there was some way to insert a
newline. The last problem I face is with the #line statements scattered
across the /E output and the missed #define directives (v). So the final
question becomes is this possible with C, and can it be done elegantly
doing multiple passes on the preprocessor. If I can't solve these three
problems it doesn't look likely. If it isn't possible what should I use
a scripting language like perl or ruby?

Thanks for the help ahead of time and I hope I didn't mess with anyones
mind too much :).

(i)
---------preprocessor.h----------------
#define DEFINE ?##?##=##define  //Trigraphs for the # sign              
                                        //concatenated with define
#define IFNDEF ?##?##=##ifndef
#define ENDIF  ?##?##=##endif

#define HEADER(symbol)  \
        IFNDEF __##symbol       \
        DEFINE __##symbol

#define FOOTER  ENDIF

--------programheader.h----------------
HEADER(programheader)
DEFINE PRINTME 1
FOOTER

--------------program.cpp--------------
#include <stdio.h>
#include "programheader.h"

int main(void)
{
        printf("%d", PRINTME);

Quote:
}

---------------------------------------

(ii)
When I do:
cl /E program.cpp
or
gcc -E program.cpp
It's probably worthwhile to mention that -E outputs the preprocessor
formatting.
I get roughly:

//stdio header file stuff
#ifndef __programheader \ #define __programheader
??#define PRINTME 1
#endif

int main(void)
{
        printf("%d", PRINTME);

Quote:
}

(iii)
look back at (ii) and notice #define __programheader works, but
??#define PRINTME does NOT work. It turns the ?##?##= to ??# instead of
??=, which is rather surprising and odd. Again this hasn't been tested
under gcc. But, I'd like to think that this is true between all ANSI C
compilers.

(iv).
Again look back to (ii). and notice:
#ifndef __programheader \ #define __programheader
is different from (i):
#define HEADER(symbol)  \
        IFNDEF __##symbol       \
        DEFINE __##symbol      
I've noticed that sometimes microsofts' compiler does output like:
#ifndef __symbol \ ^M
#define __symbol
But that's only the case when some sort of errors going on, but the
point is it inserts the ^M which I assume was a CR/LF, but it isn't.

(v)
If you were to compile this and you looked at the output from:
cl /E
You'd notice the spot where I said:
//stdio header file stuff
previously and notice that there's all sorts of:
#line "file.h" 11
scattered around. These are unnecessary in this output.

The problem with the #define statements in stdio.h is that it needs to
use DEFINE the macro crafted in one to have it carried over in the
output from (i).
--



Wed, 09 Apr 2003 12:57:21 GMT  
 C Macros textually substituting for preprocessor directives.
You simply cannot do what you're trying to do.  Standard C
preprocessing does not rescan expanded macros looking for
preprocessing directives.  If you really have to take an
approach like this, use a separate program (m4 perhaps) to
perform more general macro processing on your source code
before it is fed to the C compiler.
--



Thu, 10 Apr 2003 13:00:51 GMT  
 C Macros textually substituting for preprocessor directives.

Quote:
> You simply cannot do what you're trying to do.  Standard C
> preprocessing does not rescan expanded macros looking for
> preprocessing directives.  If you really have to take an
> approach like this, use a separate program (m4 perhaps) to
> perform more general macro processing on your source code
> before it is fed to the C compiler.

m4 is great! Thanks a million I was doing all sorts of {*filter*} things with
sed and grep to parse the output I was getting from the compiler to
generate another .h and .c files that had the correct preprocessor
statements.

Anyhow, the point being anyone and everyone needing to do heavy
preprocessor work should use m4!!! Thanks again.

-D
--



Mon, 14 Apr 2003 13:32:33 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Preprocessor: defining a macro inside a #define directive

2. Able To Use Preprocessor Directives Inside Macros?

3. Nesting of a preprocessor directive within a macro defn

4. preprocessor directives in macros

5. preprocessor substituting comments?

6. preprocessor directives (the code in them)

7. #ident (preprocessor directive)

8. C# preprocessor directives

9. Preprocessor directive to replace with source line number

10. Is this a standard preprocessor directive?

11. Conditional inclusion of preprocessor directives

12. preprocessor directives across assemblies

 

 
Powered by phpBB® Forum Software