Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2006

Tip: Looking for answers? Try searching our database.

compiler error with preprocessing directives

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
cody - 29 Mar 2006 13:36 GMT
the following leads to an error (note that TEST is not defined):

#if TEST
string s = @"
#"; // <-- the error is "preprocessing directive expected"
#endif

also, here we get the same error:

#if TEST
/*
# */
#endif

iam not sure wheather this is a problem in the compiler or the spec, but in
my opinion even in such directives the compiler should parse comments and
verbatim string to ensure we can use #if statements everywhere and do not
have to change to code to make if work.
Jon Skeet [C# MVP] - 29 Mar 2006 14:39 GMT
> the following leads to an error (note that TEST is not defined):
>
[quoted text clipped - 14 lines]
> verbatim string to ensure we can use #if statements everywhere and do not
> have to change to code to make if work.

The spec disagrees:

<quote>
The remaining conditional-sections, if any, are processed as skipped-
sections: except for pre-processing directives, the source code in the
section need not adhere to the lexical grammar; no tokens are generated
from the source code in the section; and pre-processing directives in
the section must be lexically correct but are not otherwise processed.
Within a conditional-section that is being processed as a skipped-
section, any nested conditional-sections (contained in nested #if...
#endif and #region...#endregion constructs) are also processed as
skipped-sections.
</quote>

Basically, #"; isn't lexically correct as a preprocessing directive.
The comments business is interesting - I assume that's where the "no
tokens are generated" bit comes in.

Can you give an example where it's actually *useful* to have lexically
invalid preprocessor directives in your code?

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

cody - 30 Mar 2006 19:09 GMT
> <quote>
> The remaining conditional-sections, if any, are processed as skipped-
[quoted text clipped - 14 lines]
> Can you give an example where it's actually *useful* to have lexically
> invalid preprocessor directives in your code?

Useful or not but it happened. I copied sql script from a file into my code
and used @ to make a verbatim string so that I do not have to change
something in the string and do not need to make "stuff"+ for every single
line.
The sql code contained sql comments (#) and they were located at line
beginnings.
Because this code wasn't ready for release I decided to put #if..#endif
around it.
So that was my story :-)

Iam sure there are more situations where similar things might happen. imho
the best thing would be if the the compiler would ignore all # inside a #if
block unless it is the #endif directive which would really minimize the
chance that such things happen.
Jon Skeet [C# MVP] - 30 Mar 2006 20:36 GMT
> > Basically, #"; isn't lexically correct as a preprocessing directive.
> > The comments business is interesting - I assume that's where the "no
[quoted text clipped - 17 lines]
> block unless it is the #endif directive which would really minimize the
> chance that such things happen.

Except it also has to look for #if so that it can match the right
number of levels of #endif.

#if isn't meant to be a way of commenting out code - do that with
multi-line comments, or get the IDE to add // at the start of each line
(I think you can just highlight the relevant section and then press a
shortcut - I don't know the key off hand.)

So, to me it sounds like changing your behaviour in this case would be
a better answer than changing the spec to suit this particular case.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

cody - 31 Mar 2006 10:58 GMT
>> Iam sure there are more situations where similar things might happen.
>> imho
[quoted text clipped - 5 lines]
> Except it also has to look for #if so that it can match the right
> number of levels of #endif.

Which it also has to do at the moment, otherwise the following wouldn't
compile:

#if false
#if false
#endif
#endif

> #if isn't meant to be a way of commenting out code - do that with
> multi-line comments, or get the IDE to add // at the start of each line
[quoted text clipped - 3 lines]
> So, to me it sounds like changing your behaviour in this case would be
> a better answer than changing the spec to suit this particular case.

I was conditionally compiling my code to that I have unfinished code only in
DEBUG mode.
Jon Skeet [C# MVP] - 31 Mar 2006 17:32 GMT
> >> Iam sure there are more situations where similar things might happen.
> >> imho
[quoted text clipped - 13 lines]
> #endif
> #endif

That's my point - it has to validate *some* preprocessor directives, so
I don't see that it's a big problem to have to validate all
preprocessor directives.

> > #if isn't meant to be a way of commenting out code - do that with
> > multi-line comments, or get the IDE to add // at the start of each line
[quoted text clipped - 6 lines]
> I was conditionally compiling my code to that I have unfinished code only in
> DEBUG mode.

But the code you "conditioned" out was invalid - if it had been valid
code, it would have been okay. The only exception to that is the
comment with the preprocessor directive in it - it would be reasonable
to expect that to work.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.