clang 20.0.0git
UsedDeclVisitor.h
Go to the documentation of this file.
1//===- UsedDeclVisitor.h - ODR-used declarations visitor --------*- C++ -*-===//
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// This file defines UsedDeclVisitor, a CRTP class which visits all the
9// declarations that are ODR-used by an expression or statement.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_LIB_SEMA_USEDDECLVISITOR_H
14#define LLVM_CLANG_LIB_SEMA_USEDDECLVISITOR_H
15
18
19namespace clang {
20template <class Derived>
21class UsedDeclVisitor : public EvaluatedExprVisitor<Derived> {
22protected:
24
25public:
27
29
30 Derived &asImpl() { return *static_cast<Derived *>(this); }
31
33 auto *D = E->getDecl();
34 if (isa<FunctionDecl>(D) || isa<VarDecl>(D)) {
35 asImpl().visitUsedDecl(E->getLocation(), D);
36 }
37 }
38
40 auto *D = E->getMemberDecl();
41 if (isa<FunctionDecl>(D) || isa<VarDecl>(D)) {
42 asImpl().visitUsedDecl(E->getMemberLoc(), D);
43 }
44 asImpl().Visit(E->getBase());
45 }
46
48 asImpl().visitUsedDecl(Node->getBeginLoc(), Node->getCapturedDecl());
49 Inherited::VisitCapturedStmt(Node);
50 }
51
53 asImpl().visitUsedDecl(
54 E->getBeginLoc(),
55 const_cast<CXXDestructorDecl *>(E->getTemporary()->getDestructor()));
56 asImpl().Visit(E->getSubExpr());
57 }
58
60 if (E->getOperatorNew())
61 asImpl().visitUsedDecl(E->getBeginLoc(), E->getOperatorNew());
62 if (E->getOperatorDelete())
63 asImpl().visitUsedDecl(E->getBeginLoc(), E->getOperatorDelete());
64 Inherited::VisitCXXNewExpr(E);
65 }
66
68 if (E->getOperatorDelete())
69 asImpl().visitUsedDecl(E->getBeginLoc(), E->getOperatorDelete());
70 QualType DestroyedOrNull = E->getDestroyedType();
71 if (!DestroyedOrNull.isNull()) {
72 QualType Destroyed = S.Context.getBaseElementType(DestroyedOrNull);
73 if (const RecordType *DestroyedRec = Destroyed->getAs<RecordType>()) {
74 CXXRecordDecl *Record = cast<CXXRecordDecl>(DestroyedRec->getDecl());
75 if (Record->getDefinition())
76 asImpl().visitUsedDecl(E->getBeginLoc(), S.LookupDestructor(Record));
77 }
78 }
79
80 Inherited::VisitCXXDeleteExpr(E);
81 }
82
84 asImpl().visitUsedDecl(E->getBeginLoc(), E->getConstructor());
85 CXXConstructorDecl *D = E->getConstructor();
86 for (const CXXCtorInitializer *Init : D->inits()) {
87 if (Init->isInClassMemberInitializer())
88 asImpl().Visit(Init->getInit());
89 }
90 Inherited::VisitCXXConstructExpr(E);
91 }
92
94 asImpl().Visit(E->getExpr());
95 Inherited::VisitCXXDefaultArgExpr(E);
96 }
97
99 asImpl().Visit(E->getExpr());
100 Inherited::VisitCXXDefaultInitExpr(E);
101 }
102
104 if (ILE->hasArrayFiller())
105 asImpl().Visit(ILE->getArrayFiller());
106 Inherited::VisitInitListExpr(ILE);
107 }
108
110 if (auto *CD = dyn_cast<CapturedDecl>(D)) {
111 if (auto *S = CD->getBody()) {
112 asImpl().Visit(S);
113 }
114 } else if (auto *CD = dyn_cast<BlockDecl>(D)) {
115 if (auto *S = CD->getBody()) {
116 asImpl().Visit(S);
117 }
118 }
119 }
120};
121} // end namespace clang
122
123#endif // LLVM_CLANG_LIB_SEMA_USEDDECLVISITOR_H
DynTypedNode Node
const Decl * D
Expr * E
llvm::MachO::Record Record
Definition: MachO.h:31
SourceLocation Loc
Definition: SemaObjC.cpp:759
QualType getBaseElementType(const ArrayType *VAT) const
Return the innermost element type of an array type.
Represents binding an expression to a temporary.
Definition: ExprCXX.h:1491
Represents a call to a C++ constructor.
Definition: ExprCXX.h:1546
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2539
Represents a C++ base or member initializer.
Definition: DeclCXX.h:2304
A default argument (C++ [dcl.fct.default]).
Definition: ExprCXX.h:1268
A use of a default initializer in a constructor or in aggregate initialization.
Definition: ExprCXX.h:1375
Represents a delete expression for memory deallocation and destructor calls, e.g.
Definition: ExprCXX.h:2497
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2803
Represents a new-expression for memory allocation and constructor calls, e.g: "new CXXNewExpr(foo)".
Definition: ExprCXX.h:2240
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
This captures a statement into a function.
Definition: Stmt.h:3767
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1265
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
EvaluatedExprVisitor - This class visits 'Expr *'s.
Describes an C or C++ initializer list.
Definition: Expr.h:5039
bool hasArrayFiller() const
Return true if this is an array initializer and its array "filler" has been set.
Definition: Expr.h:5143
Expr * getArrayFiller()
If this initializer list initializes an array with more elements than there are initializers in the l...
Definition: Expr.h:5133
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition: Expr.h:3187
A (possibly-)qualified type.
Definition: Type.h:941
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition: Type.h:1008
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:5965
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:493
ASTContext & Context
Definition: Sema.h:962
CXXDestructorDecl * LookupDestructor(CXXRecordDecl *Class)
Look for the destructor of the given class.
Encodes a location in the source.
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:8540
void VisitCXXConstructExpr(CXXConstructExpr *E)
void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E)
void VisitMemberExpr(MemberExpr *E)
void VisitDeclRefExpr(DeclRefExpr *E)
void VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E)
void VisitCXXDeleteExpr(CXXDeleteExpr *E)
void VisitInitListExpr(InitListExpr *ILE)
void VisitCXXNewExpr(CXXNewExpr *E)
void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E)
void visitUsedDecl(SourceLocation Loc, Decl *D)
EvaluatedExprVisitor< Derived > Inherited
void VisitCapturedStmt(CapturedStmt *Node)
The JSON file list parser is used to communicate input to InstallAPI.