Hacker News new | ask | show | jobs
by groovy2shoes 2992 days ago
You can map locations in generated C code to locations in some other file, too. Standard C has a pragma for it:

    #line <num> "filename"
or, if the filename is the same as the previous pragma, you can omit it from the pragma:

    #line <num>
This pragma is used to inform the compiler of what information it should use when displaying error messages and such. I think it's meant to be used by the C preprocessor, but the preprocessor isn't required to use it, and other programs aren't prohibited from using it.

You might wind up generating a lot of these, but they're really easy to generate as long as you preserve that information all the way through compilation (which I'd assume you'd have to do for LLVM IR regardless).

1 comments

Interesting, I wasn't aware of the #line directive, thanks!

Yes, you would need to keep track of the source locations for each node in the AST. There is a nice example in the LLVM Kaleidoscope tutorial here: https://llvm.org/docs/tutorial/LangImpl09.html

Bison and lex use #line to make generated C/C++ code (lexer, parser) debuggable, or at least their GNU implementation does, I don't know about other implementations.