5

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?

Dragnalith
  • 195
  • 1
  • 9

2 Answers2

4

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.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • if instead of a preprocessing language I was using a completely different language, would it be possible to map symbol name and type with a similar method? – Dragnalith Sep 03 '20 at 05:32
  • @Dragnalith -- yes. The experience that I alluded to elliptically was with a Java to C translator. – Pete Becker Sep 03 '20 at 12:17
0

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. :-)

Klaus
  • 24,205
  • 7
  • 58
  • 113