multiline cpp macros without \

A

Andrey Vul

Is there a way to do multiline preprocessor macros without using the
format
#define foo(x) \
something \
something_else \
another_thing

E.g.
#define foo(x) {
something
something_else
another_thing
}

I'm doing C++-style templates via preprocessor and all compiler errors/
warnings have the same line number.
 
B

Ben Pfaff

Andrey Vul said:
Is there a way to do multiline preprocessor macros without using the
format
#define foo(x) \
something \
something_else \
another_thing

No.
 
P

Peter Nilsson

Is there a way to do multiline preprocessor macros without
using the
format
#define foo(x) \
something \
something_else \
another_thing

E.g.
#define foo(x) {
something
something_else
another_thing
}
No.

I'm doing C++-style templates via preprocessor and all
compiler errors/warnings have the same line number.

One tip is to run it through a preprocessor and then use
a source code formatter to clean it up (or do it by hand.)
Then compile the cleaned up source.
 
S

spinoza1111

Is there a way to do multiline preprocessor macros without using the
format
#define foo(x) \
something \
something_else \
another_thing

E.g.
#define foo(x) {
something
something_else
another_thing

}

I'm doing C++-style templates via preprocessor and all compiler errors/
warnings have the same line number.

Consider using inline.
 
A

Andrey Vul

     Probably won't help much, since the textual representation
of his macro's expansion is likely to look like

        something something_else another_thing

... because the spliced newlines vanish before things like
#define are recognized.  The #define directive sees all the
spliced physical lines as one long logical line.

     I recall a similar problem with macros that expanded into
complete function definitions.  Since the entire definition --
function signature, body, the whole works -- was all on one
very long spliced "line," the debugger couldn't set a breakpoint
on a line inside a macroized function, nor "step to next line,"
nor any of those other helpful things ...

     Them's the breaks when you use macros, I guess.

I think that the only logical solution is to make a quick & dirty
preprocessor that does multiline expansion by tokenizing strictly to
whitespace and embedding \t, \n, etc in the tokens.

Here's what I'm doing (functions not yet implemented, only data
structures);

typedef struct {
/* token */
char *str;
/* substitution index:
* 0 = no substitution
* 1 = 1st arg
* n = nth arg
*/
unsigned sub;
} token;

typedef struct {
char *macroname;
size_t nargs;
token *tokens;
size_t ntokens;
} macro;

const char mlpp_declare_begin[] = "/*mlpp_declare_begin";
const char mlpp_declare_end[] = "/*mlpp_declare_end";
const char mlpp_define[] = "/*mlpp_define";
const char mlpp_arg_terminator = "*/";

as a parser, so that this can done in a makefile:

../mlpp foo.c foo.mlpp.c
$(CC) foo.mlpp.c -c -o foo.o $(CFLAGS)

The embedded tabs and newlines make it easier to do debugging because $
(CC) will have useful line numbers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top