Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I just want to say how much I value your site. I hope the good work keeps up there. It's really helped me..."

Geography

Where in the world do Tek-Tips members come from?
ohmygosh2 (Programmer)
1 Oct 06 15:07
This is to propose an improvement to the #line directive of the C preprocessor, or to ask for a workaround.

Currently, it seems that the #line directive is "ignored" if it lies between a #if and a #endif directives !
I believe that in most practical cases, this behavior is undesired.

Example:

#if 0
.....
#line 10
.....
#endif

Hence, if a special purpose program adds a few lines to a C source file that happen to be enclosed by #if and #endif directives, the correction to the line numbering in the file will not take effect. Unless that special purpose program also detects the presence of the #if and #endif and adds additional #line directives. Which makes the job harder.

So the correction could like

#if 0
.....
#line 10
.....
#endif
#line 20

But things could be a lot easier if the #line directive were always processed independently of the presence or absence of #if, #ifdef, #elif, #else, and #endif directives. Indeed, why should line numbering in the file be a function of #if directives that handle compilation issues rather than file layout? Except perhaps in very unusual cases.

Does anyone know of a simple workaround?

xwb (Programmer)
1 Oct 06 16:04
I haven't actually used #line for debugging for ages.  I find that it hinders debugging.  If you don't use it at all then

CODE

printf ("%d\n", __LINE__);
will print the line number you're on and you just go to it.
If however, you have #line 617 then you'll have to know the nearest line number less than 617, say 500 and go 117 lines down from it.  If you don't have a #line then whether or not you have #ifs.

I only find it of use in code generators like lex/yacc or the very old C++ interpreter CFront where you can find the line in the original source.
cpjust (Programmer)
1 Oct 06 16:49
Your example uses #if 0
That basically disables all the code in that #if block.
If you use #if 1 does the #line directive still not work right?
ohmygosh2 (Programmer)
1 Oct 06 17:16
Replacing #if 0 with #if 1: of course then it works.
My point is that line numbering should be independent of other compiler directives. Line numbering should not change if I use #if 0 or #if 1. The line number in the file does not change!!!

We could distinguish between two things:

- Regular compiler directives (macros, conditional compilation, error reporting, etc).

- File handling and line numbering: in most cases, it has nothing to do with conditional compilations.

Hence, the second point should not be connected to the first point (unless perhaps in very rare cases).

Salem (Programmer)
1 Oct 06 17:50
> Hence, if a special purpose program adds a few lines to a C source
You mean like automated debug / test code which you use to gather some kind of statistical information about the running program?

#line is an internal solution to the problem which the pre-processor introduces, namely that the line numbering which the compiler sees has nothing to do with the source code line numbers after all the substitutions have been made.

#line is normally in the output of the pre-processor, not the input.

If you want to instrument your code and keep meaningful source code line numbers (from the programmers perspective), then you should run the pre-processor first (say gcc -E prog.c > prog.i), then pass it through your own program, then through the compiler proper.

--

cpjust (Programmer)
1 Oct 06 19:04
If you feel like writing your own non-standard C compiler, be my guest.  winky smile  But I don't think any existing C compilers are going to make the change you're suggesting since it would undoubtedly break a lot of existing programs that assume the current method of handling pre-processor directives...
ohmygosh2 (Programmer)
1 Oct 06 20:20
Yes, one option is to process the file after the C preprocessor. But this option has its own issues.

For example, if I insert new lines in the file, how do I inform the compiler of the new line numbering? Is there a standard method or is it compiler dependent?
cpjust (Programmer)
1 Oct 06 21:01
Couldn't you just do this:

CODE

#if 0
.....
#line 10
.....
#else
#line 10
#endif
#line 20

That way, #line 10 is there whether you have #if 0 or #if 1.
ohmygosh2 (Programmer)
2 Oct 06 2:27
Yes cpjust, this is the solution I gave. After every #else or #endif you recalculate the line number and issue a #line.

But that makes you program more complex. Which is unfortunate. The C standard should have specified that #line is not subject to #if conditions.
Salem (Programmer)
2 Oct 06 2:41
> if I insert new lines in the file, how do I inform the compiler of the new line numbering?
You keep a count, and observe current #line numbers, and output a #line after everything you insert.

CODE

#line 4
foo
bar
#line 10
baz

And you want to change it to

CODE

#line 4
foo
stuff
stuff
stuff
#line 5

bar
#line 10
baz

--

ohmygosh2 (Programmer)
2 Oct 06 3:29
Salem, this is the solution I currently use before the C preprocessor. It has the weakness that if there is a #if/#endif enclosing the lines you added, then the scheme fails.

If I were to work using your suggestion: after the C preprocessor, then the #line directive won't work I think. I looked at the output of gcc -E ... (as well as cc -E ...)

It uses things such as

# 10 "file.c" x y z

where 10 is the line number, and x y z are more information for the compiler, which I can't understand. I need to figure out these numbers in order to work after the C preprocessor.

Nevertheless, I prefer to solve the problem before the C preprocessor (even though harder) but for other reasons.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close