Delete variable definitions and it's references

I want to create a tool which deletes variable definitions and references in a C++ code snippet. I was wondering which option I should pick:

Option 1) Use LLVM to convert the C++ to an IR format and then write the deletion pass for it?
Option 2) Create a custom parser, write the deletion pass for it?
Option 3) Any suggestions?

Keep in mind I want to expand this to other object types too for e.g. Later I would also want to extend to this to functions - where functions are deleted/replaced with a NULL instructions.

Thank you!

generally trying to map LLVM IR changes back to source isn’t likely to be viable

Using Clang Tooling/clang-tidy is probably the right direction for a refactoring like that.

A follow up question- do you mean to write the deletion pass using a clang frontend library?

I am not specifically targeting C++ but another language (had taken C++ as an example). Was looking for a design decision…

Ah - yeah, Clang doesn’t have “passes” - you can check out the Clang Tooling infrastructure, and see how it’s structured and emulate something like that? Basically the AST has the info and so you inspect the AST, compute a textual change (like “the AST says the text from to is some expression and I want to remove those characters from the input file”)

1 Like

Seems like this might work - thank you!