1 //===--- WalkAST.cpp - Find declaration references in the AST -------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "AnalysisInternal.h"
10 #include "clang/AST/RecursiveASTVisitor.h"
13 namespace include_cleaner
{
15 using DeclCallback
= llvm::function_ref
<void(SourceLocation
, NamedDecl
&)>;
17 class ASTWalker
: public RecursiveASTVisitor
<ASTWalker
> {
18 DeclCallback Callback
;
20 void report(SourceLocation Loc
, NamedDecl
*ND
) {
21 if (!ND
|| Loc
.isInvalid())
23 Callback(Loc
, *cast
<NamedDecl
>(ND
->getCanonicalDecl()));
27 ASTWalker(DeclCallback Callback
) : Callback(Callback
) {}
29 bool VisitTagTypeLoc(TagTypeLoc TTL
) {
30 report(TTL
.getNameLoc(), TTL
.getDecl());
34 bool VisitDeclRefExpr(DeclRefExpr
*DRE
) {
35 report(DRE
->getLocation(), DRE
->getFoundDecl());
42 void walkAST(Decl
&Root
, DeclCallback Callback
) {
43 ASTWalker(Callback
).TraverseDecl(&Root
);
46 } // namespace include_cleaner