"BartC" <
b...@freeuk.com> writes:
> "Eric Sosman" <eso...@ieee-dot-org.invalid> wrote in message
> news:j9m058$sil$1...@dont-email.me...
>> On 11/12/2011 8:57 AM, BartC wrote:
>
>>> And as things are now, we must disallow comments like my example.
>>>
>>> I can't see why one is any worse than the other.
>>
>> Would you like to be able to use a contraction like
>>
>> if (p < endp) /* don't run off the end! */
>
> (I still don't see what the problem would be with this example; what's
> supposed to happen when that line in enclosed in another /*...*/ comment?)
Currently, /*...*/ comments don't nest. As a result, once the
compiler sees a /* sequence that's not inside another token (a string
literal or, rarely, a character constant), it only needs to scan
for a */ sequence, regardless of the context in which it appears.
With nesting comments, after seeing an opening /*, there are several
possible approaches:
1. Scan for /* and */ without tokenization and keep a count of the
nesting level. This causes problems if a comment contains a string
literal containing /* or */:
/* const char open_comment[] = "/*"; */
The */ at the end of the line terminates the inner comment, not the
outer one. Presumably the idea of allowing nested comments is to
make it possible to use /* and */ to comment out a chunk of code
that itself contains /*...*/ comments, but this example shows that
that still wouldn't work in all cases.
2. Tokenize everything inside /*...*/ comments, so that the compiler
can detect cases like the above and ignore comment delimiters in
commented-out string literals. But this comment from Eric's post:
if (p < endp) /* don't run off the end! */
doesn't contain a valid token sequence.
3. Define a new set of tokenization rules for use inside comments,
so that comment delimiters within string literals within comments
are ignored, but arbitrary text that doesn't necesssarily form a
valid token sequence is accepted. Note that '"' and '*/' are valid
character constants. Since the content of a comment may or may not
be some approximation of C source code, it's difficult to decide
whether a given sequence of characters that happens to include
double-quote charactes should be treated as a C string literal
or not.
And of course *any* of these changes would break existing code,
which means the odd of any of them being adopted are close to zero.
>> in a comment? Then you don't want comments that nest.
>>
>> Comments are not for suppressing blocks of code; comments are
>> for commentary.
>
> Nevertheless that's what the OP wants to do. Does the Standard specifically
> tell us what content is allowed in a comment, and what is frowned upon?
Not directly, no. But it does clearly say that /*...*/ comments don't
nest, from which one can easily infer that you can't use /*...*/ to
comment out a block of code that already contains /*...*/ comments.
And if you want to comment out a block of code, you can always insert
a // at the beginning of each line. This makes it easier to see
at a glance which lines are commented out and avoids all the above
problems. (It does require you to have a compiler that recognizes //
comments, which is an issue if you want to use strict C90 or C95 mode.)