public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Yair Lenga <yair.lenga@gmail.com>
To: Yair Lenga via Gcc <gcc@gcc.gnu.org>
Subject: C pre-processor: Better grammar for multi-line macros
Date: Tue, 26 Jul 2022 11:55:13 +0300	[thread overview]
Message-ID: <CAK3_KpOyt9z9Of97aUz=UpXev=3+dcprv=m0tm=uSZEs_sOU9w@mail.gmail.com> (raw)

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

                 reply	other threads:[~2022-07-26  8:55 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAK3_KpOyt9z9Of97aUz=UpXev=3+dcprv=m0tm=uSZEs_sOU9w@mail.gmail.com' \
    --to=yair.lenga@gmail.com \
    --cc=gcc@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).