[splint-discuss] Parse Error on Macro, help

Terry Colligan terry-splint at tenberry.com
Wed Sep 6 21:39:36 EDT 2006


On Wednesday 06 September 2006 04:38 pm, Richard A. O'Keefe wrote:
> I wrote:
>     > Note 1: having empty parentheses in a macro definition is technically
>     > undefined in C89.  (I'm not sure, it might be illegal.)
> 
> James Dennett <jdennett at acm.org> queried this:
> 	Are you sure?  It's well-defined in C++98, which is almost
> 	identical to C89/90 in terms of preprocessing.  An empty
> 	parameter name is illegal, but the case of () defines a
> 	function-like macro with no arguments.  AFAIK this is a
> 	moderately common, strictly conforming, technique.
> 
> Over the years I have owned three copies of the ANSI C standard
> (two of them copies of the Australian edition, one of them the
> infamous Herbert Schildt edition).  All of them have walked out
> of my office.  So it's hard to check.  (Now that C89 is no longer
> an official standard, why isn't the standard available free over
> the net?)  Fortunately, WG14 have kept an archive, which you can find on
> the net, and it turns out that my recollection was 100% correct.

  Actually, no, it is not.

  First, let me say that I was a member of the orginal X3J11 committee.
  I have several copies of the X3.159-1989 standard (the predecessor
  to the WG14 committee), and I remember the
  discussions fairly clearly.

  I re-read the standard to confirm my understanding.

  A function-like macro defined with parens and with no arguments
  was in the draft standard from when I started on the committee, around
  1983.  It is not in the base document, the original 1978 K&R book.
  It was added pre-1983.

  It is (and was) clearly necessary to allow macros to replace functions,
  something which has been in C for a long time.

  It *is* clearly in the X3.159-1989 standard, in section 3.8.3.
 
> Allowing an empty argument list for a function-like macro was a
> CHANGE from C89 to C99.  It was proposed by Fred J. Tydeman on 1995-01-09.
> Empty parentheses on a macro really truly were officially undefined
> behaviour, and he knew of a compiler which complained.
> 
> He was talking about empty *actual parameters*, but the two are related.
> There is no point in writing
>     #define FOO() ...
> unless you mean to invoke it as
>     ... FOO() ...

  Yes! Exactly.

> and the latter really was undefined behaviour caught by at least one
> compiler.

  Then that compiler was not conforming to the 1989 standard.

> I'm pretty sure the definition was undefined behaviour as well. 

  No, it wasn't/isn't. The syntax (which is a defining part of the standard)
  clearly allows for parens with no arguments in the #define directive,
  and the descriptive text is written in a form that has a clear interpretation
  for no defined arguments.

  The description is relentlessly and excessively terse, but that was one
  of our goals -- to be as brief as possible so as to reduce the chance of
  the standard being self-conflicting.  Personally, I think we went overboard
  and made the standard much harder to read as a result.

  What C99 upgraded from a common extension to part of the standard was
  the possiblility of empty actual arguments.

> 	> Note 3: having empty parentheses in a macro call is technically
> 	> undefined in C89.  (I'm not sure, it might be illegal.)
> 
> 	It's required to use a function-like macro with no arguments, I
> 	believe.
> 
> So it may be, but you still weren't allowed to do it in C89.

  Yes, you were -- at least in any conforming compiler.

-- 
Terry

Terry Colligan                         terry-splint at tenberry.com


More information about the splint-discuss mailing list