By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,651 Members | 1,676 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,651 IT Pros & Developers. It's quick & easy.

selective preprocessor expansion

P: n/a
hi.

i want to examine preprocessed source which only has certain macros
expanded, for example i would like to have:

#include <stdio.h>
#include "other.c"
int main() {
...
}

tranformed into:

#include <stdio.h>
<insert here the result of other.c inclusion>
int main() {
...
}

so that #include <stdio.h> is not expanded... in general i would like to
have standard library header macros not expanded...

up to now i only have come up with smt like this:

#ifdef INCLUDE_STDIO
#include <stdio.h>
#endif
#include "other.c"
int main() {
...
}

so that i compile using:

$ cc -DINCLUDE_STDIO source.c

*or* i can examine preprocessed source using:

$ cc -E -P source.c

[note: this is gcc specific, but i think it is clear anyway: -D option
defines a macro, -E option does preprocessing only, -P "cleans up"
output a bit]

any simpler/better/moregeneral/morestandard/moretested method?

bye

max
Nov 14 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
max(01)* wrote:
hi.

i want to examine preprocessed source which only has certain macros
expanded, for example i would like to have:
[...]


Question 10.18 in the comp.lang.c Frequently Asked
Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

has some pointers you may find helpful.

--
Er*********@sun.com

Nov 14 '05 #2

P: n/a
"max(01)*" <ma*@fisso.casa> wrote in message
news:Bz*********************@news3.tin.it...
any simpler/better/moregeneral/morestandard/moretested method?


If you are willing (or can) annotate the #include directives, yes. You can
create a file called, for example, nothing.h which has no contents. Then define
a macro to remap the inclusions that you want:

#ifndef DISABLE_STD_INCLUDE
#define STD(file) <file>
#else
#define STD(file) "nothing.h"
#endif

#include STD(stdio.h)

Regards,
Paul Mensonides
Nov 14 '05 #3

P: n/a
I propose you to use CodeWorker, a universal parsing tool and a source
code generator, which can combinate both parsing and code generation
for transforming programs. CodeWorker is a freeware, available at
"http://www.codeworker.org".

I wrote you two little scripts that, I hope, answer your needs. Type:
codeworker ExpandIncludes.cws <c-file1> ... <c-fileN> -I <path1> ...
-I <pathn>

where the leader script "ExpandIncludes.cws" is worth:
// This function searches the location of an included file path,
// or returns an empty string if not found
function getIncludedFilePath(sInclude : value, sCSourcePath : value)
{
// do not expand the file twice
if this.findElement(sInclude.getShortFilename()) return false;
insert this[sInclude.getShortFilename()];
// normalize the path of the included file: '/' only
sInclude = sInclude.replaceString('\\', '/');
// if an absolute path, its location is known with certainty
if sInclude.startString('/') || (sInclude.charAt(1) == ':' &&
sInclude.charAt(2) == '/') {
return sInclude;
}
// is the included file relative to the C source file?
local iIndex = sCSourcePath.findLastString('/');
sCSourcePath = sCSourcePath.leftString($iIndex + 1$);
if existFile(sCSourcePath + sInclude) return sCSourcePath +
sInclude;
// if not, looks into -I switches passed on the command line
if existFile(sInclude) return sInclude;
// failure: included file not found
traceLine("warning: unable to find file \"" + sInclude + "\"");
return false;
}

// iterates all C files passed on the command line and
// expand them
foreach i in _ARGS {
translate("ExpandIncludes.cwp", project, i, i + 'c');
}

and where the BNF translation script "ExpandIncludes.cwp" is worth:
#implicitCopy // What We Parse Is What We Write

// BNF non terminal, whose production rule scans the C source and
// replaces '#include' directives having the file name between double
quotes,
// by the content of the file.
preprocessing ::=
// C++-like comments and whitespaces are ignored
// between BNF terminals and non-terminals, so C are
#ignore(C++)
[
// jumps to the next '#include' or to the next constant string
->[
// do not copy what is parsed during this sequence
// into the output file
#explicitCopy
// BNF terminals to match
'#'
"include"
// read the content of a C-like constant string
#readCString:sInputFile
// do not ignore whitespaces or comments anymore,
// up to the end of the sequence
#!ignore
->'\n'
// construct the correct path for the included file
=> local sPath = getIncludedFilePath(sInputFile,
getInputFilename());
// continue only if the path isn't empty
#check(sPath)
=> {
// the path was found, load its content and
// inject it in the output file (template-based syntax)
// after having applied the expansion on it recursively
traceLine("expand the include file \"" + sPath + "\"");
local sContent = loadFile(sPath);
sContent = translateString("ExpandIncludes.cwp", this,
sContent);
@@sContent@@
}
|
// handle the special case of constant string:
// we may encounter one with '#include ...' inside!
#readCString
// now, the danger is gone!
]
]*
// copy implicitly up to the end of the file
->#empty
;
Note that, to make the script shorter, I don't resolve the conditional
preprocessor directives and I suppose that you don't have two files
with the same name at different directories.
Regards,

Cedric
--
"max(01)*" <ma*@fisso.casa> wrote in message news:<Bz*********************@news3.tin.it>...
i want to examine preprocessed source which only has certain macros
expanded, for example i would like to have:

#include <stdio.h>
#include "other.c"
int main() {
...
}

tranformed into:

#include <stdio.h>
<insert here the result of other.c inclusion>
int main() {
...
}

so that #include <stdio.h> is not expanded... in general i would like to
have standard library header macros not expanded...

Nov 14 '05 #4

P: n/a
Eric Sosman wrote:
max(01)* wrote:
hi.

i want to examine preprocessed source which only has certain macros
expanded, for example i would like to have:
[...]

Question 10.18 in the comp.lang.c Frequently Asked
Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

has some pointers you may find helpful.


oops! my apologies... :-(
thank you very mucho for the pointers...
<OT> anyways, if anyone knows some technique which applies to the gnu
compiler, suggestions are welcome ;-( </OT>

bye, max
Nov 14 '05 #5

P: n/a
many thanks to all kind repliers

max
Nov 14 '05 #6

P: n/a
max(01)* wrote on 12/08/04 :
<OT> anyways, if anyone knows some technique which applies to the gnu
compiler, suggestions are welcome ;-( </OT>


They know:

news:gnu.gcc.help

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.