clang  14.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 
19 namespace clang {
20 template <class Derived>
21 class UsedDeclVisitor : public EvaluatedExprVisitor<Derived> {
22 protected:
23  Sema &S;
24 
25 public:
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  Inherited::VisitCXXConstructExpr(E);
86  }
87 
89  asImpl().Visit(E->getExpr());
90  }
91 
93  if (auto *CD = dyn_cast<CapturedDecl>(D)) {
94  if (auto *S = CD->getBody()) {
95  asImpl().Visit(S);
96  }
97  } else if (auto *CD = dyn_cast<BlockDecl>(D)) {
98  if (auto *S = CD->getBody()) {
99  asImpl().Visit(S);
100  }
101  }
102  }
103 };
104 } // end namespace clang
105 
106 #endif // LLVM_CLANG_LIB_SEMA_USEDDECLVISITOR_H
clang::DeclRefExpr::getLocation
SourceLocation getLocation() const
Definition: Expr.h:1297
clang::UsedDeclVisitor
Definition: UsedDeclVisitor.h:21
clang::CXXDefaultArgExpr::getExpr
const Expr * getExpr() const
Definition: ExprCXX.h:1280
SemaInternal.h
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:88
EvaluatedExprVisitor.h
clang::CXXNewExpr::getOperatorNew
FunctionDecl * getOperatorNew() const
Definition: ExprCXX.h:2257
clang::CXXBindTemporaryExpr::getSubExpr
const Expr * getSubExpr() const
Definition: ExprCXX.h:1434
clang::CXXRecordDecl::getDefinition
CXXRecordDecl * getDefinition() const
Definition: DeclCXX.h:535
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:673
clang::ASTContext::getBaseElementType
QualType getBaseElementType(const ArrayType *VAT) const
Return the innermost element type of an array type.
Definition: ASTContext.cpp:6297
clang::CXXNewExpr
Represents a new-expression for memory allocation and constructor calls, e.g: "new CXXNewExpr(foo)".
Definition: ExprCXX.h:2139
clang::UsedDeclVisitor::VisitCXXBindTemporaryExpr
void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E)
Definition: UsedDeclVisitor.h:52
clang::UsedDeclVisitor::VisitCXXDefaultArgExpr
void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E)
Definition: UsedDeclVisitor.h:88
clang::UsedDeclVisitor::visitUsedDecl
void visitUsedDecl(SourceLocation Loc, Decl *D)
Definition: UsedDeclVisitor.h:92
clang::CXXBindTemporaryExpr::getTemporary
CXXTemporary * getTemporary()
Definition: ExprCXX.h:1430
clang::Sema::Context
ASTContext & Context
Definition: Sema.h:411
clang::RecordType
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:4613
Node
DynTypedNode Node
Definition: ASTMatchFinder.cpp:67
clang::CXXBindTemporaryExpr
Represents binding an expression to a temporary.
Definition: ExprCXX.h:1412
clang::UsedDeclVisitor::asImpl
Derived & asImpl()
Definition: UsedDeclVisitor.h:30
clang::UsedDeclVisitor::VisitCXXDeleteExpr
void VisitCXXDeleteExpr(CXXDeleteExpr *E)
Definition: UsedDeclVisitor.h:67
clang::Type::getAs
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:7161
clang::CXXDestructorDecl
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2668
clang::DeclRefExpr::getDecl
ValueDecl * getDecl()
Definition: Expr.h:1289
clang::CXXDeleteExpr::getOperatorDelete
FunctionDecl * getOperatorDelete() const
Definition: ExprCXX.h:2437
clang::MemberExpr::getMemberLoc
SourceLocation getMemberLoc() const
getMemberLoc - Return the location of the "member", in X->F, it is the location of 'F'.
Definition: Expr.h:3351
clang::Sema::LookupDestructor
CXXDestructorDecl * LookupDestructor(CXXRecordDecl *Class)
Look for the destructor of the given class.
Definition: SemaLookup.cpp:3365
clang::CXXConstructExpr::getConstructor
CXXConstructorDecl * getConstructor() const
Get the constructor that this expression will (ultimately) call.
Definition: ExprCXX.h:1532
clang::CapturedStmt
This captures a statement into a function.
Definition: Stmt.h:3500
clang::CXXDefaultArgExpr
A default argument (C++ [dcl.fct.default]).
Definition: ExprCXX.h:1241
clang::MemberExpr::getMemberDecl
ValueDecl * getMemberDecl() const
Retrieve the member declaration to which this expression refers.
Definition: Expr.h:3245
clang::UsedDeclVisitor::VisitCapturedStmt
void VisitCapturedStmt(CapturedStmt *Node)
Definition: UsedDeclVisitor.h:47
clang::MemberExpr::getBase
Expr * getBase() const
Definition: Expr.h:3239
clang::CXXRecordDecl
Represents a C++ struct/union/class.
Definition: DeclCXX.h:255
clang::UsedDeclVisitor::S
Sema & S
Definition: UsedDeclVisitor.h:23
clang::CXXTemporary::getDestructor
const CXXDestructorDecl * getDestructor() const
Definition: ExprCXX.h:1391
clang::UsedDeclVisitor::VisitMemberExpr
void VisitMemberExpr(MemberExpr *E)
Definition: UsedDeclVisitor.h:39
clang::CXXDeleteExpr::getBeginLoc
SourceLocation getBeginLoc() const
Definition: ExprCXX.h:2448
clang::CXXDeleteExpr
Represents a delete expression for memory deallocation and destructor calls, e.g.
Definition: ExprCXX.h:2398
clang::QualType::isNull
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition: Type.h:738
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:89
clang::Sema
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:355
clang::UsedDeclVisitor::VisitCXXNewExpr
void VisitCXXNewExpr(CXXNewExpr *E)
Definition: UsedDeclVisitor.h:59
clang::CXXDeleteExpr::getDestroyedType
QualType getDestroyedType() const
Retrieve the type being destroyed.
Definition: ExprCXX.cpp:287
clang::UsedDeclVisitor::UsedDeclVisitor
UsedDeclVisitor(Sema &S)
Definition: UsedDeclVisitor.h:28
clang
Definition: CalledOnceCheck.h:17
clang::CXXBindTemporaryExpr::getBeginLoc
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: ExprCXX.h:1438
clang::UsedDeclVisitor::VisitDeclRefExpr
void VisitDeclRefExpr(DeclRefExpr *E)
Definition: UsedDeclVisitor.h:32
clang::CXXNewExpr::getOperatorDelete
FunctionDecl * getOperatorDelete() const
Definition: ExprCXX.h:2259
clang::MemberExpr
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition: Expr.h:3162
clang::UsedDeclVisitor::VisitCXXConstructExpr
void VisitCXXConstructExpr(CXXConstructExpr *E)
Definition: UsedDeclVisitor.h:83
clang::EvaluatedExprVisitor
EvaluatedExprVisitor - This class visits 'Expr *'s.
Definition: EvaluatedExprVisitor.h:128
clang::UsedDeclVisitor::Inherited
EvaluatedExprVisitor< Derived > Inherited
Definition: UsedDeclVisitor.h:26
clang::CXXNewExpr::getBeginLoc
SourceLocation getBeginLoc() const
Definition: ExprCXX.h:2378
clang::CXXConstructExpr::getBeginLoc
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: ExprCXX.cpp:509
clang::DeclRefExpr
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1217
clang::CXXConstructExpr
Represents a call to a C++ constructor.
Definition: ExprCXX.h:1460
clang::EvaluatedExprVisitorBase< std::add_pointer, Derived >::Context
const ASTContext & Context
Definition: EvaluatedExprVisitor.h:32