0

I have a prebuild-event tool (written in Ruby) in my C++ toolchain, that generates additional C++ code from existing C++ source code. I would like to replace this tool with a faster generator and using clang would be the best option.

Is there a way to write a C++ application that parses C++ source code of a file, so I can implement this prebuild tool in Clang? I am looking for a keyword or page with how to start. Any help is highly appreciated!

Daniel Stephens
  • 2,371
  • 8
  • 34
  • 86
  • 1
    are you looking for something like "Clang AST"? – kmdreko Nov 23 '19 at 06:47
  • Almost, I am looking for an AST but the macros are not resolved yet – Daniel Stephens Nov 23 '19 at 06:55
  • 4
    _Is there a way to write a C++ application that parses C++ source code of a file_ Yes. That tool is called a "C++ compiler". Sorry, I couldn't resist. Of course, anything done in Ruby can be done in C++ as well. Please, describe in detail how you want to parse C++ code. A simple pattern matching (with replace) is one case but a full fledged C++ parsing is something (very) else. – Scheff's Cat Nov 23 '19 at 07:16
  • 1
    Concerning macros: You can run a compiler in pre-processor-only mode. (In gcc/clang it's command line option `-E`.) This will provide the C++ code with all macro replacement done. (Don't be surprised about file size. All the `#include`s will be resolved as well.) – Scheff's Cat Nov 23 '19 at 07:19

1 Answers1

2

Parsing C++ is not a simple thing. Compile-time trickery and implicit semantics make it incredibly hard. Because of this I would suggest going with Clang. Its developers made it possible to use Clang as a library. Check out this guide to see different interfaces Clang has. If you want real C++ experience you might want to choose LibTooling.

I want to warn you though that in order for any C/C++ parser to work as expected they absolutely need compilation options used by the real compiler. Without include directories or macro definitions the code can make little to no sense. Basically your build system should tell your custom tool how to compile each file. The simplest approach would be using compilation database. It is a go-to solution for many Clang-based tools. However, it looks like you're making it a part of your build system, so maybe incorporating your tool and using options directly from the build system can be not such of a burden for you.

I hope this information is helpful!

Valeriy Savchenko
  • 1,524
  • 8
  • 19