[comp.std.c] Comments and pragma lines

markhall@pyramid.pyramid.com (Mark Hall) (11/29/88)

Consider the following source:

#pragma rotate lid counterclockwise    /* store pointers in
					* Best Foods 
					* Mayonaise Jars.
					*/

Is it legal to start the comment on the pragma line?  From what I read*
in the standard, it is illegal.  Fine.  For such cases, I'd like to see the 
message:

error: <file>.c: line 1, cannot open comment on #pragma line

But if the two characters `/*' can occur legally in a
#pragma line, then I don't see how the compiler can flag the error.
For example, *if* the following is legal:

#pragma ascii sunbakedchar &^%$/*()!

then of course the compiler can't know in general that ``/*'' in the
pragma line is the ``open comment'' token.  The consequence of this is
that, for the original source above, the compiler will give some other
complaint like:

error: <file>.c: line 2, lhs of `*' operator must be l-value.
error: <file>.c: line 2, undeclared id `Best'

or worse, it could give some really dreadful message way down in the
source listing that was actually due to the /* in the #pragma.  (you
know how hard it is to find those smoking open-comment/close-comment
mismatch errors).

So, the question is:  can /* occur legally on the #pragma line?

_______________________ 
*did I say `read'?  I meant, ``someone walked by my office with
 the standard in hand and I saw an open page''.

gwyn@smoke.BRL.MIL (Doug Gwyn ) (11/29/88)

In article <48957@pyramid.pyramid.com> markhall@pyramid.UUCP (Mark Hall) writes:
-#pragma rotate lid counterclockwise    /* store pointers in
-					* Best Foods 
-					* Mayonaise Jars.
-					*/
-Is it legal to start the comment on the pragma line?  From what I read*
-in the standard, it is illegal.

How about explaining how you deduce that?
I'm away from my copy of the standard, but I'm sure the comment was
supposed to have been replaced by a space character before the #pragma
line was ever (pre)processed.

henry@utzoo.uucp (Henry Spencer) (11/30/88)

In article <48957@pyramid.pyramid.com> markhall@pyramid.UUCP (Mark Hall) writes:
>Is it legal to start the comment on the pragma line?  From what I read*
>in the standard, it is illegal...

Sorry, wrong, see 2.1.1.2, which clearly specifies that comments disappear
long before #pragma and its kin are recognized.  Comments are legal anywhere.
It's even legal to write "/* foo */ # /* bar */ pragma ...".
-- 
SunOSish, adj:  requiring      |     Henry Spencer at U of Toronto Zoology
32-bit bug numbers.            | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

katzung@laidbak.UUCP (Brian Katzung) (11/30/88)

In article <48957@pyramid.pyramid.com> markhall@pyramid.UUCP (Mark Hall) writes:
>Consider the following source:
>
>#pragma rotate lid counterclockwise    /* store pointers in
>					* Best Foods 
>					* Mayonaise Jars.
>					*/
>
>Is it legal to start the comment on the pragma line?  From what I read*
>in the standard, it is illegal.  Fine.  For such cases, I'd like to see the 
>message:
>
>error: <file>.c: line 1, cannot open comment on #pragma line

Which part of the standard were you "reading"?

The comment should have been reduced to a single space by the time the
#pragma directive is interpreted.  At that time, you might get a com-
plaint about "extra tokens" or some other syntax error, but only if the
preprocessor doesn't like trailing white space.

>#pragma ascii sunbakedchar &^%$/*()!

If that is supposed to be a character list, the preprocessor/compiler
writer can probably make it a lot easier on herself by using a string
literal.

>then of course the compiler can't know in general that ``/*'' in the
>pragma line is the ``open comment'' token.  The consequence of this is

Comments (really white space) are no longer significant by the time the
compilation phase occurs--white space preprocessor tokens would normally
just be discarded when the compiler token stream is created.

 -- Brian Katzung  ...!{sun,spl1}!laidbak!katzung

friedl@vsi.COM (Stephen J. Friedl) (11/30/88)

In article <48957@pyramid.pyramid.com>, markhall@pyramid.pyramid.com (Mark Hall) writes:
> Is it legal to start the comment on the pragma line?  From what I read
> in the standard, it is illegal.

Comments are removed before any preprocessing directives are recognized, so 

	#pragma foo		/* comment
				   goes
				   here */

removes the entire comment before the #pragma is seen.  This comes from
section 2.1.1.2, `Translation Phases' (page 6 of the May88 dpANS).

     Steve

--- 
Stephen J. Friedl        3B2-kind-of-guy            friedl@vsi.com
V-Systems, Inc.                                 attmail!vsi!friedl
Santa Ana, CA  USA       +1 714 545 6442    {backbones}!vsi!friedl
---------Nancy Reagan on cutting the grass: "Just say mow"--------

markhall@pyramid.pyramid.com (Mark Hall) (12/01/88)

In article <9007@smoke.BRL.MIL> gwyn@brl.arpa (Doug Gwyn (VLD/VMB) <gwyn>) writes:
>In article <48957@pyramid.pyramid.com> markhall@pyramid.UUCP ( me ? ) writes:
>-#pragma rotate lid counterclockwise    /* store pointers in
>-					* Best Foods 
>-					* Mayonaise Jars.
>-					*/
>-Is it legal to start the comment on the pragma line?  From what I read*
>-in the standard, it is illegal.
>
>How about explaining how you deduce that?  I'm sure the comment was
>supposed to have been replaced by a space character before the #pragma
>line was ever (pre)processed.

Did *I* post that noise here?  Dang.  I meant to send it to
rec.humor.funny.  Yes, of course, how stupid of me.

The rest of the question still may be of interest.  I was working on
the `future' cpp and was wondering what I should do about gracefully
ignoring #pragma's that were not supported by our compiler.  If the
following pragma were legal:

>-#pragma sunbakedchars ^%*#%/*!@

then how in the world would my cpp be able to tell, without knowing the
special semantics of `sunbakedchars', that the /* was not an open
comment?  If my `cpp' grabbed it as an open comment, it could wreak havoc
on an otherwise legal program . . . .

The question I should have asked (wait, I did ask it!) was whether this
is legal.  After having actually read the pertinent sections of XJ311
(no really, I read them this time),  I see that /* can only occur as an
open comment or within a string/char literal.  So if you see /* outside
of a " or ' string, you have yerself an open comment.

Hooray for the tokenization of preprocessor directives.

Sorry for the messing up the S/N ratio.  It was getting good for a 
while there.

-Mark Hall (smart mailer): markhall@pyramid.pyramid.com 
     (uucp paths): {amdahl|decwrl|sun|seismo|ames}!pyramid!markhall

katzung@laidbak.UUCP (Brian Katzung) (12/02/88)

In article <1988Nov29.171740.22165@utzoo.uucp> henry@utzoo.uucp (Henry Spencer) writes:
>It's even legal to write "/* foo */ # /* bar */ pragma ...".

Unless I'm mistaken, only in a function-like #define token replacement list!
It won't be a #pragma because the # is not the first character on the line.

 -- Brian Katzung  ...!{sun,spl1}!katzung

gwyn@smoke.BRL.MIL (Doug Gwyn ) (12/04/88)

In article <1832@laidbak.UUCP> katzung@laidbak.UUCP (Brian Katzung) writes:
>In article <1988Nov29.171740.22165@utzoo.uucp> henry@utzoo.uucp (Henry Spencer) writes:
>>It's even legal to write "/* foo */ # /* bar */ pragma ...".

Right.

>Unless I'm mistaken, only in a function-like #define token replacement list!
>It won't be a #pragma because the # is not the first character on the line.

You're mistaken.

henry@utzoo.uucp (Henry Spencer) (12/06/88)

In article <1832@laidbak.UUCP> katzung@laidbak.UUCP (Brian Katzung) writes:
>>It's even legal to write "/* foo */ # /* bar */ pragma ...".
>
>Unless I'm mistaken, only in a function-like #define token replacement list!
>It won't be a #pragma because the # is not the first character on the line.

X3J11 has removed the first-char-on-the-line restriction; now # can be
preceded, as well as followed, by white space (which is what comments are).
-- 
SunOSish, adj:  requiring      |     Henry Spencer at U of Toronto Zoology
32-bit bug numbers.            | uunet!attcan!utzoo!henry henry@zoo.toronto.edu