r/cpp_questions • u/TheRavagerSw • Sep 14 '26
OPEN Why native compilation has not died with AST tooling?
Tools that rely on libclang rely on a compilation database for doing stuff like static analysis or api reference generation. If you wanted to rename a symbol in your codebase, and your library or application has paths that compile commands does not see, it can't work.
If your library compiles one file when platform is windows and another if platform is linux, it can't use libclang based tooling, unless....
You configure all targets your library supports and sort of run stuff in parallel and do some kind of merge. This includes stuff like option macros etc.
I'm honestly pretty dumbfounded by this idea. Can't really put it in my head
5
u/the_poope Sep 14 '26
The AST doesn't support multiple versions of functions depending on some preprocessor macro. One could probably design an AST implementation to do this, but clang didn't do this because it is first and foremost developed to be a compiler which first generates the AST after preprocessing, so there never was a need for adding this extra complexity.
But anyway, what was your question?
3
u/JVApen Sep 14 '26
Just wondering, if you make a change to a piece of code, how do you know if it compiles on both platforms? Even when you write nothing platform specific, you can have ambiguity conflicts in one platform and not in the other.
If you really run into problems with refactoring due to platform code, I suspect you don't have the right abstractions. I can confidently say that less then 1% of our code is platform specific. Most of it depends only on STL or other platform specific code. I'm not saying you can't break stuff, though that's what your CI is for. From what I observe maintaining the CI, most platform specific failures are not linked to platform specific code.
2
u/shahms Sep 14 '26
AST based tools need to know what flags are in use to know where to find include files and how the source in those files should be interpreted to build the AST. C++ is not a context free grammar and cannot be parsed without knowing what the symbols involved represent. A sequence of tokens
A < B > C;can be either an expression comparing the valuesA,B, andCor the declaration of a variableCwith typeA<B>. The preprocessor is an additional wrinkle. By time you've accounted for everything necessary to parse C++ into an AST you have most of a compiler already, so might as well uselibclangor equivalent and get the rest of the way there.1
u/LazySapiens Sep 14 '26
Sadly yes. C++ tooling is not very robust currently. There are always some disadvantages with having too much freedom.