clang 19.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());
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
llvm::MachO::Record Record
Definition: MachO.h:30
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:1485
CXXTemporary * getTemporary()
Definition: ExprCXX.h:1503
const Expr * getSubExpr() const
Definition: ExprCXX.h:1507
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: ExprCXX.h:1511
Represents a call to a C++ constructor.
Definition: ExprCXX.h:1540
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: ExprCXX.cpp:513
CXXConstructorDecl * getConstructor() const
Get the constructor that this expression will (ultimately) call.
Definition: ExprCXX.h:1603
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2532
Represents a C++ base or member initializer.
Definition: DeclCXX.h:2297
A default argument (C++ [dcl.fct.default]).
Definition: ExprCXX.h:1264
A use of a default initializer in a constructor or in aggregate initialization.
Definition: ExprCXX.h:1371
Expr * getExpr()
Get the initialization expression that will be used.
Definition: ExprCXX.cpp:1035
Represents a delete expression for memory deallocation and destructor calls, e.g.
Definition: ExprCXX.h:2491
FunctionDecl * getOperatorDelete() const
Definition: ExprCXX.h:2530
SourceLocation getBeginLoc() const
Definition: ExprCXX.h:2541
QualType getDestroyedType() const
Retrieve the type being destroyed.
Definition: ExprCXX.cpp:291
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2796
Represents a new-expression for memory allocation and constructor calls, e.g: "new CXXNewExpr(foo)".
Definition: ExprCXX.h:2234
SourceLocation getBeginLoc() const
Definition: ExprCXX.h:2471
FunctionDecl * getOperatorDelete() const
Definition: ExprCXX.h:2339
FunctionDecl * getOperatorNew() const
Definition: ExprCXX.h:2337
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
const CXXDestructorDecl * getDestructor() const
Definition: ExprCXX.h:1464
This captures a statement into a function.
Definition: Stmt.h:3762
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1260
ValueDecl * getDecl()
Definition: Expr.h:1328
SourceLocation getLocation() const
Definition: Expr.h:1336
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:85
EvaluatedExprVisitor - This class visits 'Expr *'s.
Describes an C or C++ initializer list.
Definition: Expr.h:4847
bool hasArrayFiller() const
Return true if this is an array initializer and its array "filler" has been set.
Definition: Expr.h:4951
Expr * getArrayFiller()
If this initializer list initializes an array with more elements than there are initializers in the l...
Definition: Expr.h:4941
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition: Expr.h:3172
SourceLocation getMemberLoc() const
getMemberLoc - Return the location of the "member", in X->F, it is the location of 'F'.
Definition: Expr.h:3361
ValueDecl * getMemberDecl() const
Retrieve the member declaration to which this expression refers.
Definition: Expr.h:3255
Expr * getBase() const
Definition: Expr.h:3249
A (possibly-)qualified type.
Definition: Type.h:738
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition: Type.h:805
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:5339
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:457
ASTContext & Context
Definition: Sema.h:858
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:7913
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.