Thread: parsing of individual arguments in a C macro

  1. #1
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,337

    parsing of individual arguments in a C macro

    I've searched around, and have yet to find an example, so I'm thinking maybe it can't be done.

    What I want to do is parse an individual argument, like x, passed to a macro.

    For instance,
    Code:
    #define PROTO(x) ........    // <-- my macro 
    .
    .
    .
    PROTO(static int foo(char *)) ;    // a function prototype, wrapped inside a macro.
    I want to be able to break x apart into each token, and beyond that, be able to recognize that foo is a function name by the placement of the token after the return type (which might be a pointer), and the fact that there is a left paren at the end of it.

    Can this be done with the C macro facility? Basically, I want to substsring x and compare the substringed values to constants.

    My overall objective is to generate 2 function prototypes from different compilation units.

    In one CU, I just want the standard function prototype to be output:

    Code:
    static int foo(char *) ;

    which is done by merely coding:
    Code:
    #define PROTO(x) x
    However, I also want the following function prototype generated based on other environment characteristics:
    Code:
    int (* foo)(char 8) ;
    In short, the static modifier will be dropped and the function name foo will be converted to function pointer foo.

    If this can't be done with the Macro facility, I have already worked out how to do this in an ugly way, by comma-separating the tokens:

    Code:
    #define PROTO(w,x,y,z) w x y ## z  
    
    or 
    
    #define PROTO(w,x,y,z)  x (* y) ## z 
    PROTO(static, int, foo, (char *)) ;
    and this works fine, but it's kinda/sorta convoluted due to the addition of the commas.
    Mainframe assembler programmer by trade. C coder when I can.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,644
    Your "ugly" way is the only way.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,337
    Thank you John.c.
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,644
    The problem is that there is no way to run C code at the preprocessor stage, and the preprocessor itself doesn't have any string processing capability built in, let alone a "de-stringify" operator to turn the results back into tokens.

    The gcc preprocessor (cpp) is described here:
    Top (The C Preprocessor)
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    956
    You might want to look at a Lisp (Common Lisp, Scheme, etc.). Those languages have very powerful macro systems which can manipulate code as data (because code is data in Lisp).

  6. #6
    Registered User
    Join Date
    Apr 2021
    Posts
    142
    First, it is technically possible to "parse" space-delimited words in the C preprocessor. But it is terribly difficult, and not very satisfying. There is a particular combination of tricks you can do to "eat" the leading word by using token pasting with a defined prefix (like FOO_ ## x) which then expands to a nesting structure with meaning. (Of course, this requires you lay out all your identifiers with meaning in advance. You can't account for things like typedef names.)

    You may be able to use the new typeof keyword, or the GNU/Clang __typeof__ extension, or the MSC decltype (I think?) extension, to extract the type you want, with or without the pointer tag.

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    1,644
    @aghast, you should post your source for this information. Presumably this or similar:
    How to parse tokens separated by whitespace in C++ preprocessor? - Stack Overflow
    I assume the typeof idea is actually yours. Nice one.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,337
    Thanks christop. I normally use assembler, and in the assembler I use, this task would be child's play in the macro facility.
    Mainframe assembler programmer by trade. C coder when I can.

  9. #9
    Registered User
    Join Date
    May 2024
    Posts
    4
    While typeof and similar extensions might offer some relief, it seems like there's no perfect solution within the C preprocessor itself. Given these constraints, has anyone tried integrating a small script or tool that preprocesses the code before feeding it to the compiler?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Macro arguments
    By grahamlabdon in forum C Programming
    Replies: 1
    Last Post: 09-23-2015, 08:43 AM
  2. Parsing cmd line arguments as variables
    By graison in forum C Programming
    Replies: 10
    Last Post: 08-03-2011, 04:16 PM
  3. variable number of arguments for a function or a macro
    By mynickmynick in forum C Programming
    Replies: 5
    Last Post: 05-19-2010, 11:35 AM
  4. UNIX: Parsing *argv[] with quoted arguments
    By Enfors in forum C Programming
    Replies: 19
    Last Post: 04-03-2007, 06:19 AM
  5. Replies: 2
    Last Post: 05-05-2002, 09:27 AM

Tags for this Thread