2

Is there any way you do something like...

#define {  { printf("%s,%s",_FUNCTION_, _LINE_); { 

this won't compile. But I'm wondering if there is some kind of trick to effectively get the same functionality? (other than writing a tool to hook at the preprocessing step)

The point of doing this is a pondering on how to get a poor mans code coverage.

Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156

5 Answers5

7

It's certainly not possible with a macro. The name of a macro must be an identifier. The brace characters are punctuators, not identifiers.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • sorry, edited the question, I know its not possible with macros as is.... was looking to see if there's a trick, or even a non standard compiler trick – Keith Nicholas Jul 22 '10 at 00:14
  • 1
    @Keith: A trick to change the way brace punctuators are handled? I doubt it; that's a rather unusual "feature request" :-). – James McNellis Jul 22 '10 at 00:17
  • 1
    I know..... I know.... but how cool would that be :-) It's C after all, give me the power to Nuke myself in the head :-) – Keith Nicholas Jul 22 '10 at 00:21
1

I've seen this done (not that I endorse it...) with #define BEGIN ... and #define END ....

e.g.

void foo(void)
BEGIN
    stuff();
END
bstpierre
  • 30,042
  • 15
  • 70
  • 103
  • really don't want that, would like to be able to adhoc change the { and } to something else, as someone said, poor mans AOP to get poor mans code coverage – Keith Nicholas Jul 22 '10 at 00:16
1

Get rich-man's code coverage with the tools discussed in these questions, particularly gcov, which is part of GCC.

Community
  • 1
  • 1
Ken Bloom
  • 57,498
  • 14
  • 111
  • 168
  • yes, I realize I could use a rich tool, but I was also going to inject debug code for an embedded system so you could trace what its doing. Sort of a real time coverage system.... – Keith Nicholas Jul 22 '10 at 00:36
0

You can't change the way the compiler interprets {}'s. It's an assumption that they make to be able to syntaxically determine if the code is correct and what it is supposed to do.

If you indeed would like to do something like that, I suggest doing a search-and-replace on "{" "{ MY_MACRO;"

Simon
  • 773
  • 3
  • 11
0

Not with standard macros, no. The C standard (C11) has a specific requirement for macros in 6.10.3 Macro replacement:

# define identifier replacement-list new-line

with identifier being explicitly defined in 6.4.2.1 Identifiers, General as:

identifier:
    identifier-nondigit
    identifier identifier-nondigit
    identifier digit
identifier-nondigit:
    nondigit
    universal-character-name
    other implementation-defined characters
nondigit: one of
    _ a b c d e f g h i j k l m
    n o p q r s t u v w x y z
    A B C D E F G H I J K L M
    N O P Q R S T U V W X Y Z
digit: one of
    0 1 2 3 4 5 6 7 8 9

So the only *possible loophole would be other implementation-defined characters but that's not covered by the standard, hence not really portable.

In any case, it wouldn't work very well with code like:

typedef struct {
    int field1;
    int field2;
} tMyStruct;

since it would rather annoyingly place a C statement where no statements should ever exist :-)

I think if you really want to do this, you're going to have pre-process your file with a more intelligent pre-processor (one that can tell where and where not the code should go), or modify the code to explicitly put in a macro where they should be, such as selecting behaviour with:

#ifdef PING_DEBUGGING
    #define MYPING printf("%s,%s", _FUNCTION_, _LINE_)
#else
    #define MY_PING
#endif

and using it with:

void myFunc(void) { MYPING;
    // proper body of function.
}
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953