This is an archive of the discontinued LLVM Phabricator instance.

[AST] Include the TranslationUnitDecl when traversing with TraversalScope
ClosedPublic

Authored by sammccall on Jun 10 2021, 4:07 PM.

Details

Summary

Given int foo, bar;, TraverseAST reveals this tree:

TranslationUnitDecl
 - foo
 - bar

Before this patch, with the TraversalScope set to {foo}, TraverseAST yields:

foo

After this patch it yields:

TranslationUnitDecl
- foo

Also, TraverseDecl(TranslationUnitDecl) now respects the traversal scope.


The main effect of this today is that clang-tidy checks that match the
translationUnitDecl(), either in order to traverse it or check
parentage, should work.

Diff Detail

Event Timeline

sammccall created this revision.Jun 10 2021, 4:07 PM
sammccall requested review of this revision.Jun 10 2021, 4:07 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptJun 10 2021, 4:07 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript

This includes both the traversal change and the clangd changes to adapt to it, since there ended up being few.

I chickened out of the idea of changing the behavior of TranslationUnitDecl::decls(), for a couple of reasons:

  • having it apply to RecursiveASTVisitor only is similar to the previous version, so this is a slightly less invasive/risky change
  • it's actually slightly awkward to change the node's behavior as decls() is not virtual in DeclContext

This includes both the traversal change and the clangd changes to adapt to it, since there ended up being few.

I chickened out of the idea of changing the behavior of TranslationUnitDecl::decls(), for a couple of reasons:

  • having it apply to RecursiveASTVisitor only is similar to the previous version, so this is a slightly less invasive/risky change
  • it's actually slightly awkward to change the node's behavior as decls() is not virtual in DeclContext

Thanks, this seems a nice approach considering all trade-offs.

IIUC, the key point of the approach is that we change the RAV behavior on traversing TUDecl, to make it respect decls from traversal scope, which seems a reasonable mental model.
I think we should mention this in the patch description, and document somewhere around the RAV?

While it solves the critical problem in clang-tidy skip-headers, we should bear in mind that:

  1. The default TU-decl behavior in RAV can be overrode in RAV's subclass (I bet it should be very rare, and we probably don't worry about this case )
  2. users can still use "TU->decls()" to traverse all decls, which might cause clangd to deserialize preamble and get performance hit (this is an existing problem)

All in all, I'm up for this patch.

clang/include/clang/AST/RecursiveASTVisitor.h
1502

I would use a more meaningful name -- it is unclear to me what "trivial" means here without reading the code very hard.

IIUC it distinguishes the conventional case ( a single translation-unit decl) vs the case (where e.g. the TraversalScope is set to main-file decls).

sammccall updated this revision to Diff 351365.Jun 11 2021, 1:51 AM
sammccall marked an inline comment as done.

Expand/clarify comments and variable names

hokein accepted this revision.Jun 11 2021, 3:28 AM

Per https://reviews.llvm.org/harbormaster/unit/view/779496/, window test seems to be failing...

Thanks! I think we should probably document this RAV behavior change in the release note.

This revision is now accepted and ready to land.Jun 11 2021, 3:28 AM

Per https://reviews.llvm.org/harbormaster/unit/view/779496/, window test seems to be failing...

Oops, there was a test I forgot to update (failed on all platforms). Fixed.

Thanks! I think we should probably document this RAV behavior change in the release note.

Happy to do this if you like, but I wouldn't be inclined - there are no in-tree uses of setTraversalScope apart from clangd, and in google's internal codebase (quite a lot of clang API usage) I only found one caller and no changes were needed.

This revision was landed with ongoing or failed builds.Jun 11 2021, 5:29 AM
This revision was automatically updated to reflect the committed changes.