Refactor loadExternalAST method of CrossTranslationUnitContext in order to
reduce maintenance burden and so that features are easier to add in the future.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
- Build Status
Buildable 35016 Build 35015: arc lint + arc unit
Event Timeline
Thank you Endre, this patch is a great initiative.
However, I think we can do better encapsulation then just the reorganization of the functions:
We could encapsulate into a nested class NameASTUnitMap and the functions which operate on this (getCachedASTUnitForName,
loadFromASTFileCached).
We could do the same with NameFileMap, lazyInitCTUIndex, getASTFileNameForLookup.
And we could also encapsulate NumASTLoaded and its related function (checkThresholdReached). In this case perhaps we could use RAII to increase the counter.
clang/include/clang/CrossTU/CrossTranslationUnit.h | ||
---|---|---|
167 | Could you please provide comments for these new functions? E.g. it would be useful to know what is the IndexName param. | |
168 | LookupName is the USR? | |
185 | Could you please add comments for these maps (caches) about what we use them for? | |
clang/lib/CrossTU/CrossTranslationUnit.cpp | ||
364 | I don't think we should use auto here, because it is not obvious what will be the return type. It is important to see that we deal with an Expected<...>. | |
448 | This comment might be also placed in the .h file as a oneliner above the function's prototype. |
I have a feeling that LoadPass and LoagGuard could be (should be) merged together, other than that we are getting close.
clang/include/clang/CrossTU/CrossTranslationUnit.h | ||
---|---|---|
197 | Perhaps this comment is not necessary, because it is not obvious where does the mentioned "section" ends and this is a bit redundant with the comments for the classes below. | |
241 | We may not need this comment. | |
245 | Could you please add these descriptions above the member declarations? | |
257 | This name CanBegin sounds strange to me. Perhaps Enabled? | |
263 | I have a feeling that LoadPass and LoagGuard could be (should be) merged together. | |
clang/lib/CrossTU/CrossTranslationUnit.cpp | ||
352 | I think you could leave the implementation of LoadPass:: member functions in the header, because they are so small. And that way I think code comprehension could improve. |
Thanks for pointing out these issues. Most of them are agreed. Merging the RAII counter with the threshold checker class, however, does not seem like a good decision for me. What would be the benefits of merging the two?
clang/include/clang/CrossTU/CrossTranslationUnit.h | ||
---|---|---|
245 | Good point! Will do that. | |
257 | First I was thinking about making the variable names "read" as an english sentence. However CanBegin does not appear during use, so it is fair that Enable is a better name. Changing it... | |
263 | LoadPass is the RAII object, the other is decider whether the threshold is reached. Note also that LoadGuard owns the loaded-counter variable. IMHO these should be two separate classes, and I think if they were to be implemented in the header these two would be simple, and easy-to-understand classes. | |
clang/lib/CrossTU/CrossTranslationUnit.cpp | ||
352 | Good point! |
Updated the revision.
I find this solution definitely more compact with responsibilities more separated. One more thing that comes to mind is that maybe the whole explicit passing of CTUDir and IndexName arguments is a bit verbose. What are your thoughts about these being injected earlier than the query operations (eg.: in getASTUnitForFunction and getFileForFunction)?
clang/lib/CrossTU/CrossTranslationUnit.cpp | ||
---|---|---|
473 | LoadOperation should not be successful if the condition here is true. |
I discovered that there are problems with this change. The number of loaded AST units is not incremented correctly and the "CTU loaded AST file" is displayed for every access to a AST unit (even if it was got from cache). The problem is that the counting and print of AST loads does not handle the case when the load returned a cached result (and no real loading happened). This causes "annoying" messages and the AST unit limit is not handled correctly.
Could you please provide comments for these new functions? E.g. it would be useful to know what is the IndexName param.