How to debug and step into custom language sources transpiled to C++?

Viewed 344

I would like to use a custom preprocessor language along with C++. My sources would be first transpiled to valid C++ with my custom transpiler, then compiled with a regular C++ compiler. Example:

my_transpiler -o source_gen.cpp source.mycpp
g++ -o myapp source_gen.cpp

In that scenario, the debug information generated are associated with the source_gen.cpp file. So I could debug and step into source_gen.cpp. But what if I want to step into the original source file source.mycpp ?

Does debugger as gdb or visual studio, or compiler as clang, gcc, or msvc provide mechanisms to map debug information to the original source file?

2 Answers

As was hinted in a comment to the question, the usual approach to this issue is the #line directive. In particular,

# line digit-sequence " s-char-sequenceopt " new-line

Your transpiler should put this directive for each source line in the original file into the generated file:

#line 3 "source.mycpp"

If your C++ compiler generates debug information based on these directives (the ones I've used do), when you step into the code you'll step into the appropriate spot in source.mycpp.

Instead of generation xxx_gen.cpp files, you can create an additional directory for the generated files and keep the file name for the files. If you have a directory hierarchy, you also can duplicate the whole tree.

After compilation you can in gdb set the source path. This will result in finding the "original" files.

The information will only by valid, if each source line will only generate a single target line. :-)

Related