clang
20.0.0git
include
clang
Tooling
Refactoring
RecursiveSymbolVisitor.h
Go to the documentation of this file.
1
//===--- RecursiveSymbolVisitor.h - Clang refactoring library -------------===//
2
//
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
6
//
7
//===----------------------------------------------------------------------===//
8
///
9
/// \file
10
/// A wrapper class around \c RecursiveASTVisitor that visits each
11
/// occurrences of a named symbol.
12
///
13
//===----------------------------------------------------------------------===//
14
15
#ifndef LLVM_CLANG_TOOLING_REFACTORING_RECURSIVESYMBOLVISITOR_H
16
#define LLVM_CLANG_TOOLING_REFACTORING_RECURSIVESYMBOLVISITOR_H
17
18
#include "
clang/AST/AST.h
"
19
#include "
clang/AST/RecursiveASTVisitor.h
"
20
#include "
clang/Lex/Lexer.h
"
21
22
namespace
clang
{
23
namespace
tooling {
24
25
/// Traverses the AST and visits the occurrence of each named symbol in the
26
/// given nodes.
27
template
<
typename
T>
28
class
RecursiveSymbolVisitor
29
:
public
RecursiveASTVisitor
<RecursiveSymbolVisitor<T>> {
30
using
BaseType
=
RecursiveASTVisitor<RecursiveSymbolVisitor<T>
>;
31
32
public
:
33
RecursiveSymbolVisitor
(
const
SourceManager
&SM,
const
LangOptions
&LangOpts)
34
:
SM
(
SM
), LangOpts(LangOpts) {}
35
36
bool
visitSymbolOccurrence
(
const
NamedDecl
*ND,
37
ArrayRef<SourceRange>
NameRanges) {
38
return
true
;
39
}
40
41
// Declaration visitors:
42
43
bool
VisitNamedDecl
(
const
NamedDecl
*
D
) {
44
return
isa<CXXConversionDecl>(
D
) ?
true
: visit(
D
,
D
->getLocation());
45
}
46
47
bool
VisitCXXConstructorDecl
(
const
CXXConstructorDecl
*CD) {
48
for
(
const
auto
*
Initializer
: CD->
inits
()) {
49
// Ignore implicit initializers.
50
if
(!
Initializer
->isWritten())
51
continue
;
52
if
(
const
FieldDecl
*FD =
Initializer
->getMember()) {
53
if
(!visit(FD,
Initializer
->getSourceLocation(),
54
Lexer::getLocForEndOfToken
(
Initializer
->getSourceLocation(),
55
0, SM, LangOpts)))
56
return
false
;
57
}
58
}
59
return
true
;
60
}
61
62
// Expression visitors:
63
64
bool
VisitDeclRefExpr
(
const
DeclRefExpr
*
Expr
) {
65
return
visit(
Expr
->getFoundDecl(),
Expr
->getLocation());