views:

161

answers:

5

Do any editors honer C #line directives with regards to goto line features?

Context: I'm working on a code generator and need to jump to a line of the output but the line is specified relative to the the #line directives I'm adding. I can drop them but then finding the input line is even a worse pain

A: 

#line directives are normally inserted by the precompiler, not into source code, so editors won't usually honor that if the file extension is .c.

However, the normal file extension for post-compiled files is .i or .gch, so you might try using that and see what happens.

Jason Cohen
A: 

@Jason Cohen, no joy on my editor (EditPlus)

BCS
A: 

I've used the following in a header file occasionally to produce clickable items in the VC6 and recent VS(2003+) compiler ouptut window.

Basically, this exploits the fact that items output in the compiler output are essentially being parsed for "PATH(LINENUM): message".

This presumes on the Microsoft compiler's treatment of "pragma remind".

This isn't quite exactly what you asked... but it might be generally helpful in arriving at something you can get the compiler to emit that some editors might honor.

    // The following definitions will allow you to insert
    // clickable items in the output stream of the Microsoft compiler.
    // The error and warning variants will be reported by the
    // IDE as actual warnings and errors... which means you can make
    // them occur in the task list.

    // In theory, the coding standards could be checked to some extent
    // in this way and reminders that show up as warnings or even
    // errors inserted...


    #define strify0(X) #X
    #define strify(X) strify0(X)
    #define remind(S) message(__FILE__ "(" strify( __LINE__ ) ") : " S)

    // example usage
    #pragma remind("warning:  fake warning")
    #pragma remind("error:  fake error")

I haven't tried it in a while but it should still work.

Thomas Kammeyer
+1  A: 

If the editor is scriptable it should be possible to write a script to do the navigation. There might even be a Vim or Emacs script that already does something similar.

FWIW when I writing a lot of Bison/Flexx I wrote a Zeus Lua macro script that attempted to do something similar (i.e. move from input file to the corresponding line of the output file by search for the #line marker).

For any one that might be interested here is that particular macro script.

jussij
best answer to date
BCS
A: 

Use sed or a similar tool to translate the #lines to something else not interpreted by the compiler, so you get C error messages on the real line, but have a reference to the original input file nearby.

I did it the other way; add a flag to the code generator to omit the #lines and rebuild
BCS