1

I'm working on a code generator tool. I'm planing to use # LINE "filename" directives like the ones generated by the C preprocessor to show the relationship between the generated code and the original source. It would be nice to navigate the generated code and see side-to-side the original source file while moving through it. Is anyone aware if such a tool exists, or can VIM customized to do that?

A simple example:

main.c

#include "main.h"

main () {

}

main.h


int a=1;

$ gcc main.c -E

would output the following:

# 1 "main.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 31 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 32 "<command-line>" 2
# 1 "main.c"

# 1 "main.h" 1

int a=1;
# 3 "main.c" 2

main () {
}

Observe the # line "file" directives

The question is about correlating the 2 views as we navigate in one of them. In a way the error window (:copen) does something like this. The idea is that as I move over the generated file view, I can see automatically the corresponding high level relevant source line on the first view. My generator is not a C-style preprocessor but I intend to use a similar directive to correlate source and generated code. The generated and source views are very distinct, so having a visual way to correlate can be very helpful.

2
  • I'm not sure I completely understood your question but as a pointer maybe you want to read :h expand() and :h <cfile>: You could create a function which would get the filename under the cursor with <cfile>, parse the line to get the line number before the filename and then use what you got to open the file and go to the desired line.
    – statox
    Jul 2 '19 at 11:55
  • So far the closet to what I want is using the quickfix window and formatting the line file markers as a compiler would on an error message (alternatively redefining the errorformat vim setting). Hitting enter on the generated view would make the source one to jump to the right place. However not quite as clean as I was looking for (as in having the two views scroll-synchronized)
    – acppcoder
    Jul 4 '19 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.