clang 20.0.0git
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"
20#include "clang/Lex/Lexer.h"
21
22namespace clang {
23namespace tooling {
24
25/// Traverses the AST and visits the occurrence of each named symbol in the
26/// given nodes.
27template <typename T>
29 : public RecursiveASTVisitor<RecursiveSymbolVisitor<T>> {
31
32public:
34 : SM(SM), LangOpts(LangOpts) {}
35
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
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
65 return visit(Expr->getFoundDecl(), Expr->getLocation());