clang 24.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
95
97 asImpl().Visit(E->getExpr());
98 Inherited::VisitCXXDefaultArgExpr(E);
99 }
100
102 asImpl().Visit(E->getExpr());
103 Inherited::VisitCXXDefaultInitExpr(E);
104 }
105
107 if (ILE->hasArrayFiller())
108 asImpl().Visit(ILE->getArrayFiller());
109 Inherited::VisitInitListExpr(ILE);
110 }
111
113 if (auto *CD = dyn_cast<CapturedDecl>(D)) {
114 if (auto *S = CD->getBody()) {
115 asImpl().Visit(S);
116 }
117 } else if (auto *CD = dyn_cast<BlockDecl>(D)) {
118 if (auto *S = CD->getBody()) {
119 asImpl().Visit(S);
120 }
121 }
122 }
123};
124} // end namespace clang
125
126#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:1498
CXXTemporary * getTemporary()
Definition ExprCXX.h:1516
const Expr * getSubExpr() const
Definition ExprCXX.h:1520
SourceLocation getBeginLoc() const LLVM_READONLY
Definition ExprCXX.h:1524
Represents a call to a C++ constructor.
Definition ExprCXX.h:1553
SourceLocation getBeginLoc() const LLVM_READONLY
Definition ExprCXX.cpp:605
CXXConstructorDecl * getConstructor() const
Get the constructor that this expression will (ultimately) call.
Definition ExprCXX.h:1616
Represents a C++ constructor within a class.
Definition DeclCXX.h:2642
Represents a C++ base or member initializer.
Definition DeclCXX.h:2407
A default argument (C++ [dcl.fct.default]).
Definition ExprCXX.h:1275
A use of a default initializer in a constructor or in aggregate initialization.
Definition ExprCXX.h:1382
Expr * getExpr()
Get the initialization expression that will be used.
Definition ExprCXX.cpp:1137
Represents a delete expression for memory deallocation and destructor calls, e.g.
Definition ExprCXX.h:2631
FunctionDecl * getOperatorDelete() const
Definition ExprCXX.h:2670
SourceLocation getBeginLoc() const
Definition ExprCXX.h:2681
QualType getDestroyedType() const
Retrieve the type being destroyed.
Definition ExprCXX.cpp:345
Represents a C++ destructor within a class.
Definition DeclCXX.h:2907
Represents a call to an inherited base class constructor from an inheriting constructor.
Definition ExprCXX.h:1756
SourceLocation getBeginLoc() const LLVM_READONLY
Definition ExprCXX.h:1810
CXXConstructorDecl * getConstructor() const
Get the constructor that this expression will call.
Definition ExprCXX.h:1793
Represents a new-expression for memory allocation and constructor calls, e.g: "new CXXNewExpr(foo)".
Definition ExprCXX.h:2360
SourceLocation getBeginLoc() const
Definition ExprCXX.h:2611
FunctionDecl * getOperatorDelete() const
Definition ExprCXX.h:2466
FunctionDecl * getOperatorNew() const
Definition ExprCXX.h:2464
const CXXDestructorDecl * getDestructor() const
Definition ExprCXX.h:1475
This captures a statement into a function.
Definition Stmt.h:3949
CapturedDecl * getCapturedDecl()
Retrieve the outlined function declaration.
Definition Stmt.cpp:1493
SourceLocation getBeginLoc() const LLVM_READONLY
Definition Stmt.h:4144
A reference to a declared variable, function, enum, etc.
Definition Expr.h:1294
ValueDecl * getDecl()
Definition Expr.h:1362
SourceLocation getLocation() const
Definition Expr.h:1370
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:5356
bool hasArrayFiller() const
Return true if this is an array initializer and its array "filler" has been set.
Definition Expr.h:5469
Expr * getArrayFiller()
If this initializer list initializes an array with more elements than there are initializers in the l...
Definition Expr.h:5459
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition Expr.h:3412
SourceLocation getMemberLoc() const
getMemberLoc - Return the location of the "member", in X->F, it is the location of 'F'.
Definition Expr.h:3601
ValueDecl * getMemberDecl() const
Retrieve the member declaration to which this expression refers.
Definition Expr.h:3495
Expr * getBase() const
Definition Expr.h:3489
A (possibly-)qualified type.
Definition TypeBase.h:938
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition TypeBase.h:1005
Sema - This implements semantic analysis and AST building for C.
Definition Sema.h:863
Encodes a location in the source.
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 VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E)
void VisitCapturedStmt(CapturedStmt *Node)
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
Definition Address.h:330