bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Interest in Converting Macros to Inline Functions


From: Koichi Murase
Subject: Re: Interest in Converting Macros to Inline Functions
Date: Tue, 15 Nov 2022 09:45:43 +0900

 2022年11月15日(火) 8:46 Brent Pappas <pappasbrent@knights.ucf.edu>:
> I noticed that Bash code sometimes uses macros where a static inline function
> would appear to work equally as well.
> For instance, consider the macro ALL_ELEMENT_SUB defined in array.h:

The inline keyword is introduced in C99, but Bash is written in C89,
and some parts are in K&R. Then, the discussion would be whether we
would discard the support for C89 and entirely switch to C99+. If we
would switch to C99, there are actually many more places where we can
update in addition to the macros. I think a recent answer to a related
discussion is

https://lists.gnu.org/archive/html/bug-bash/2022-08/msg00141.html

(though the "C89-style function definition" in that discussion
actually points to the K&R style, and "C99 style" points to the C89
style).

> The reason why one would want to do this is because functions are often easier
> to reason about than macros.

``It's easier to reason about'' doesn't seem to be a sufficient reason
to make large changes. We want to discuss the practical amount of work
to rewrite the codebase vs the maintenance cost in the future. Also,
if we rewrite the large codebase, we would need to care about new bugs
caused by some oversights and mistakes in the rewriting process.

> The GNU C Preprocessor manual even has a list of pitfalls one can fall into
> when programming with macros.
> So it may be worthwhile to turn such macros into functions to prevent
> developers from accidentally falling into one of these pitfalls.

If you know about the detailed behavior of the preprocessor, we can
avoid the pitfalls of macros. In my opinion, the pitfalls of using
macros are easier to avoid than many of the general pitfalls of C,
which are related to pointers, etc. At least, the maintainer of Bash
will check the incoming patches, so I don't think bugs caused by wrong
usages of macros would enter the Bash codebase in the future.
Nevertheless, I agree that we should use inline functions instead of
macros when we start a new C99 project, but we need to be careful in
the case of rewriting the existing codebase because there are more
chances of creating bugs in the rewriting process itself than that for
bugs that would be caused by macros.

--
Koichi



reply via email to

[Prev in Thread] Current Thread [Next in Thread]