llvm-project/clang/unittests
Sam McCall 814e7974c6 [AST] Allow limiting the scope of common AST traversals (getParents, RAV).
Summary:
The goal is to allow analyses such as clang-tidy checks to run on a
subset of the AST, e.g. "only on main-file decls" for interactive tools.

Today, these become "problematically global" by running RecursiveASTVisitors
rooted at the TUDecl, or by navigating up via ASTContext::getParent().

The scope is restricted using a set of top-level-decls that RecursiveASTVisitors
should be rooted at. This also applies to the visitor that populates the
parent map, and so the top-level-decls are considered to have no parents.

This patch makes the traversal scope a mutable property of ASTContext.
The more obvious way to do this is to pass the top-level decls to
relevant functions directly, but this has some problems:
 - it's error-prone: accidentally mixing restricted and unrestricted
   scopes is a performance trap. Interleaving multiple analyses is
   common (many clang-tidy checks run matchers or RAVs from matcher callbacks)
 - it doesn't map well to the actual use cases, where we really do want
   *all* traversals to be restricted.
 - it involves a lot of plumbing in parts of the code that don't care
   about traversals.
This approach was tried out in D54259 and D54261, I wanted to like it
but it feels pretty awful in practice.

Caveats: to get scope-limiting behavior of RecursiveASTVisitors, callers
have to call the new TraverseAST(Ctx) function instead of TraverseDecl(TU).
I think this is an improvement to the API regardless.

Reviewers: klimek, ioeric

Subscribers: mgorny, cfe-commits

Differential Revision: https://reviews.llvm.org/D54309

llvm-svn: 346847
2018-11-14 10:33:30 +00:00
..
AST [AST] Allow limiting the scope of common AST traversals (getParents, RAV). 2018-11-14 10:33:30 +00:00
ASTMatchers [ASTMatchers] overload ignoringParens for Expr 2018-11-09 20:54:06 +00:00
Analysis [NFC] Declare instead of define non-void functions in unit tests. 2018-09-19 20:27:25 +00:00
Basic Lift VFS from clang to llvm (NFC) 2018-10-10 13:27:25 +00:00
CodeGen Unit tests for TBAA metadata generation. 2017-12-22 15:22:45 +00:00
CrossTU IWYU for llvm-config.h in clang. See r331124 for details. 2018-04-30 13:52:15 +00:00
Driver Lift VFS from clang to llvm (NFC) 2018-10-10 13:27:25 +00:00
Format [clang-format] Do not treat the asm clobber [ as ObjCExpr 2018-11-13 15:38:12 +00:00
Frontend [Fixed Point Arithmetic] Fix for FixedPointValueToString 2018-08-06 16:05:08 +00:00
Index Lift VFS from clang to llvm (NFC) 2018-10-10 13:27:25 +00:00
Lex Lift VFS from clang to llvm (NFC) 2018-10-10 13:27:25 +00:00
Rename Lift VFS from clang to llvm (NFC) 2018-10-10 13:27:25 +00:00
Rewrite [CMake] Use PRIVATE in target_link_libraries for executables 2017-12-05 21:49:56 +00:00
Sema [SemaCodeComplete] Make sure visited contexts are passed to completion results handler. 2018-07-04 10:01:18 +00:00
StaticAnalyzer [analyzer] Restrict AnalyzerOptions' interface so that non-checker objects have to be registered 2018-11-05 03:50:37 +00:00
Tooling [AST] Allow limiting the scope of common AST traversals (getParents, RAV). 2018-11-14 10:33:30 +00:00
libclang Revert r336590 "[libclang] evalute compound statement cursors before trying to evaluate" 2018-07-10 19:49:07 +00:00
CMakeLists.txt [Index] Add an option to collect macros from preprocesor. 2018-09-18 08:51:08 +00:00