clang 22.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 (auto *Record = Destroyed->getAsCXXRecordDecl();
74 Record &&
75 (Record->isBeingDefined() || Record->isCompleteDefinition()))
76 asImpl().visitUsedDecl(E->getBeginLoc(), S.LookupDestructor(Record));
77 }
78
79 Inherited::VisitCXXDeleteExpr(E);
80 }
81
83 asImpl().visitUsedDecl(E->getBeginLoc(), E->getConstructor());
85 for (const CXXCtorInitializer *Init : D->inits()) {
86 if (Init->isInClassMemberInitializer())
87 asImpl().Visit(Init->getInit());
88 }
89 Inherited::VisitCXXConstructExpr(E);
90 }
91
93 asImpl().Visit(E->getExpr());
94 Inherited::VisitCXXDefaultArgExpr(E);
95 }
96
98 asImpl().Visit(E->getExpr());
99 Inherited::VisitCXXDefaultInitExpr(E);
100 }
101
103 if (ILE->hasArrayFiller())
104 asImpl().Visit(ILE->getArrayFiller());
105 Inherited::VisitInitListExpr(ILE);
106 }
107
109 if (auto *CD = dyn_cast<CapturedDecl>(D)) {
110 if (auto *S = CD->getBody()) {
111 asImpl().Visit(S);
112 }
113 } else if (auto *CD = dyn_cast<BlockDecl>(D)) {
114 if (auto *S = CD->getBody()) {
115 asImpl().Visit(S);
116 }
117 }
118 }
119};
120} // end namespace clang
121
122#endif // LLVM_CLANG_LIB_SEMA_USEDDECLVISITOR_H
llvm::MachO::Record Record
Definition MachO.h:31
Represents binding an expression to a temporary.
Definition ExprCXX.h:1494
CXXTemporary * getTemporary()
Definition ExprCXX.h:1512
const Expr * getSubExpr() const
Definition ExprCXX.h:1516
SourceLocation getBeginLoc() const LLVM_READONLY
Definition ExprCXX.h:1520
Represents a call to a C++ constructor.
Definition ExprCXX.h:1549
SourceLocation getBeginLoc() const LLVM_READONLY
Definition ExprCXX.cpp:575
CXXConstructorDecl * getConstructor() const
Get the constructor that this expression will (ultimately) call.
Definition ExprCXX.h:1612
Represents a C++ constructor within a class.
Definition DeclCXX.h:2604
Represents a C++ base or member initializer.
Definition DeclCXX.h:2369
A default argument (C++ [dcl.fct.default]).
Definition ExprCXX.h:1271
A use of a default initializer in a constructor or in aggregate initialization.
Definition ExprCXX.h:1378
Expr * getExpr()
Get the initialization expression that will be used.
Definition ExprCXX.cpp:1105
Represents a delete expression for memory deallocation and destructor calls, e.g.
Definition ExprCXX.h:2620
FunctionDecl * getOperatorDelete() const
Definition ExprCXX.h:2659
SourceLocation getBeginLoc() const
Definition ExprCXX.h:2670
QualType getDestroyedType() const
Retrieve the type being destroyed.
Definition ExprCXX.cpp:338
Represents a C++ destructor within a class.
Definition DeclCXX.h:2869
Represents a new-expression for memory allocation and constructor calls, e.g: "new CXXNewExpr(foo)".
Definition ExprCXX.h:2349
SourceLocation getBeginLoc() const
Definition ExprCXX.h:2600
FunctionDecl * getOperatorDelete() const
Definition ExprCXX.h:2455
FunctionDecl * getOperatorNew() const
Definition ExprCXX.h:2453
const CXXDestructorDecl * getDestructor() const
Definition ExprCXX.h:1471
This captures a statement into a function.
Definition Stmt.h:3886
CapturedDecl * getCapturedDecl()
Retrieve the outlined function declaration.
Definition Stmt.cpp:1451
SourceLocation getBeginLoc() const LLVM_READONLY
Definition Stmt.h:4081
A reference to a declared variable, function, enum, etc.
Definition Expr.h:1270
ValueDecl * getDecl()
Definition Expr.h:1338
SourceLocation getLocation() const
Definition Expr.h:1346
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
EvaluatedExprVisitor(const ASTContext &Context)
Describes an C or C++ initializer list.
Definition Expr.h:5233
bool hasArrayFiller() const
Return true if this is an array initializer and its array "filler" has been set.
Definition Expr.h:5345
Expr * getArrayFiller()
If this initializer list initializes an array with more elements than there are initializers in the l...
Definition Expr.h:5335
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition Expr.h:3298
SourceLocation getMemberLoc() const
getMemberLoc - Return the location of the "member", in X->F, it is the location of 'F'.
Definition Expr.h:3487
ValueDecl * getMemberDecl() const
Retrieve the member declaration to which this expression refers.
Definition Expr.h:3381
Expr * getBase() const
Definition Expr.h:3375
A (possibly-)qualified type.
Definition TypeBase.h:937
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition TypeBase.h:1004
Sema - This implements semantic analysis and AST building for C.
Definition Sema.h:854
Encodes a location in the source.
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition Type.h:26
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< DeferredDiagnosticsEmitter > Inherited
void VisitCapturedStmt(CapturedStmt *Node)
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition Address.h:330