public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* C pre-processor: Better grammar for multi-line macros
@ 2022-07-26  8:55 Yair Lenga
  0 siblings, 0 replies; only message in thread
From: Yair Lenga @ 2022-07-26  8:55 UTC (permalink / raw)
  To: Yair Lenga via Gcc

Hi,

I'm trying to get community feedback for a proposal to enhance the C
preprocessor support for multi-line macros.

For this discussion, I prefer to put aside the philosophical debate about
using macro vs generics - I'm assuming preprocessor macros are a valid
solution to implement generic logic (and other constructs) that are not
part of the "C" language.

Summary: When implementing long macro that can not be practical expressed
in a single line, one must use escaped new lines to implement the macros.
This result in hard to maintain code - it's not possible to move code
between regular functions, and macro functions. Also, it is hard to enter
comments, or format the code for readability, as each unescaped newline
will break macro definition.

Proposing: adding a new directive '#macro' which will define a multi-line
macro. The macro body will end with "#endmacro". (Similar to #if ...
#endif).

For example, a macro to count the number of elements that match a specific
condition, using statement expressions. This is a relatively simple
example, as there are macros that span > 30 (or more) lines in various
projects.

---
#macro count_if(array, length, item, condition)
   ({
      int count = 0 ;

      for (int i=0 ; i<length ; i++ ) {
          typeof(array[u]) *item = &array ;
          if ( condition ) count++ ;
      } ;

      count ;
   })

#endmacro
---

Compare with alternative:

---
#define count_if(array, length, item, condition) \
   ({ \
      int count = 0 ; \
\
      for (int i=0 ; i<length ; i++ ) { \
          typeof(array[u]) *item = &array ; \
          if ( condition ) count++ ; \
      } ; \
      count ; \
   })
---

Or (hope this align well):
---
#define count_if(array, length, item, condition)          \
   ({
       \
      int count = 0 ;                                                      \

      \
      for (int i=0 ; i<length ; i++ ) {                                \
          typeof(array[u]) *item = &array ;                      \
          if ( condition ) count++ ;                                  \
      } ;
      \
      count ;
  \
   })
---
Implementation is relatively easy - I have a prototype working.

Looking for feedback, before attempting to submit.

Yair

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-07-26  8:55 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-26  8:55 C pre-processor: Better grammar for multi-line macros Yair Lenga

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).