Hi Clang Developers,
This post got motivated from the review process in [clang] fix merging of UsingShadowDecl by mizvekov · Pull Request #80245 · llvm/llvm-project · GitHub. But it is not required to read it to get the idea though.
The main motivation and reason of the post is that checking dumpped AST is hard to maintain and looks not pretty. We can improve them by utilizing unittests.
Generally we write tests in lit test, which tests the overall behavior of the compilation invocations, it is pretty good and efficient. However, there are cases the things we’re developing is not easy to be tested with the lit test since we need to observe the intermediate state. Then in such cases, there is a practice to dump the AST and match the dumpped AST.
But it is not good. It is pretty fragile to changes to the AST dump. To be honest, as a developer, every time I modify something and find the test with matching dumpped AST fails, I would take a deep breath and I need to make my mind to focus to look at it.
Also from the perspective of testing, it is not good as it adds new dependencies, the printing component. And it would become a disaster once we want to change the printer.
And I want to propose to prefer unittests over matching dumpped AST if we only want to get the intermediate state. An example may be ⚙ D120397 [C++20] [Modules] Make the linkage consistent for template and its specialization. I think it is better since it just tests what we want to test. It makes things pretty clear. It helped me to understand the code structure more too. One downside may be some biolerplate in unittests. But I think it is completely acceptable. One the one side, I think it is worthy to write some additonal lines of code in the unittests to make things tested properly. On the other hand, we can improve the biolerplate by refactoring and I think it offers a good oppotunity to us about which things will get reused more.
So I want to propose, in the new added test, we should forbid matching dumpped AST unless we’re implementing it. (e.g., changing the printer itself).
CC: @mizvekov @AaronBallman @erichkeane @cor3ntin @Endill @shafik @ilya-biryukov