Skip to content

Commit caf7781

Browse files
author
Gabor Spaits
committed
Move functions into CallEvent class
1 parent 894ea8f commit caf7781

File tree

4 files changed

+37
-40
lines changed

4 files changed

+37
-40
lines changed

clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,14 @@ class CallEvent {
455455
/// If the call returns a C++ record type then the region of its return value
456456
/// can be retrieved from its construction context.
457457
std::optional<SVal> getReturnValueUnderConstruction() const;
458+
459+
// Returns the CallEvent representing the caller of this function
460+
const CallEventRef<> getCaller() const;
461+
462+
// Returns true if the function was called from a standard library function.
463+
// If not or could not get the caller (it may be a top level function)
464+
// returns false.
465+
bool calledFromSystemHeader() const;
458466

459467
// Iterator access to formal parameters and their types.
460468
private:

clang/lib/StaticAnalyzer/Checkers/StdVariantChecker.cpp

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,7 @@ using namespace tagged_union_modeling;
2929

3030
REGISTER_MAP_WITH_PROGRAMSTATE(VariantHeldTypeMap, const MemRegion *, QualType)
3131

32-
namespace clang {
33-
namespace ento {
34-
namespace tagged_union_modeling {
35-
36-
CallEventRef<> getCaller(const CallEvent &Call, const ProgramStateRef &State) {
37-
const auto *CallLocationContext = Call.getLocationContext();
38-
if (!CallLocationContext || CallLocationContext->inTopFrame())
39-
return nullptr;
40-
41-
const auto *CallStackFrameContext = CallLocationContext->getStackFrame();
42-
if (!CallStackFrameContext)
43-
return nullptr;
44-
45-
CallEventManager &CEMgr = State->getStateManager().getCallEventManager();
46-
return CEMgr.getCaller(CallStackFrameContext, State);
47-
}
32+
namespace clang::ento::tagged_union_modeling {
4833

4934
const CXXConstructorDecl *
5035
getConstructorDeclarationForCall(const CallEvent &Call) {
@@ -102,21 +87,7 @@ bool isStdVariant(const Type *Type) {
10287
return isStdType(Type, llvm::StringLiteral("variant"));
10388
}
10489

105-
bool calledFromSystemHeader(const CallEvent &Call,
106-
const ProgramStateRef &State) {
107-
if (CallEventRef<> Caller = getCaller(Call, State))
108-
return Caller->isInSystemHeader();
109-
110-
return false;
111-
}
112-
113-
bool calledFromSystemHeader(const CallEvent &Call, CheckerContext &C) {
114-
return calledFromSystemHeader(Call, C.getState());
115-
}
116-
117-
} // end of namespace tagged_union_modeling
118-
} // end of namespace ento
119-
} // end of namespace clang
90+
} // end of namespace clang::ento::tagged_union_modeling
12091

12192
static std::optional<ArrayRef<TemplateArgument>>
12293
getTemplateArgsFromVariant(const Type *VariantType) {
@@ -171,14 +142,17 @@ class StdVariantChecker : public Checker<eval::Call, check::RegionChanges> {
171142
ArrayRef<const MemRegion *> Regions,
172143
const LocationContext *,
173144
const CallEvent *Call) const {
145+
if (!Call)
146+
return State;
147+
174148
return removeInformationStoredForDeadInstances<VariantHeldTypeMap>(
175-
Call, State, Regions);
149+
*Call, State, Regions);
176150
}
177151

178152
bool evalCall(const CallEvent &Call, CheckerContext &C) const {
179153
// Check if the call was not made from a system header. If it was then
180154
// we do an early return because it is part of the implementation.
181-
if (calledFromSystemHeader(Call, C))
155+
if (Call.calledFromSystemHeader())
182156
return false;
183157

184158
if (StdGet.matches(Call))

clang/lib/StaticAnalyzer/Checkers/TaggedUnionModeling.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,24 @@ namespace clang::ento::tagged_union_modeling {
2424
// The implementation of all these functions can be found in the file
2525
// StdVariantChecker.cpp under the same directory as this file.
2626

27-
// Returns the CallEvent representing the caller of the function
28-
// It is needed because the CallEvent class does not contain enough information
29-
// to tell who called it. Checker context is needed.
30-
CallEventRef<> getCaller(const CallEvent &Call, CheckerContext &C);
3127
bool isCopyConstructorCall(const CallEvent &Call);
3228
bool isCopyAssignmentCall(const CallEvent &Call);
3329
bool isMoveAssignmentCall(const CallEvent &Call);
3430
bool isMoveConstructorCall(const CallEvent &Call);
3531
bool isStdType(const Type *Type, const std::string &TypeName);
3632
bool isStdVariant(const Type *Type);
37-
bool calledFromSystemHeader(const CallEvent &Call, CheckerContext &C);
3833

3934
// When invalidating regions, we also have to follow that by invalidating the
4035
// corresponding custom data in the program state.
4136
template <class TypeMap>
4237
ProgramStateRef
43-
removeInformationStoredForDeadInstances(const CallEvent *Call,
38+
removeInformationStoredForDeadInstances(const CallEvent &Call,
4439
ProgramStateRef State,
4540
ArrayRef<const MemRegion *> Regions) {
4641
// If we do not know anything about the call we shall not continue.
4742
// If the call is happens within a system header it is implementation detail.
4843
// We should not take it into consideration.
49-
if (!Call || Call->isInSystemHeader())
44+
if (Call.isInSystemHeader())
5045
return State;
5146

5247
for (const MemRegion *Region : Regions)

clang/lib/StaticAnalyzer/Core/CallEvent.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,26 @@ const ConstructionContext *CallEvent::getConstructionContext() const {
517517
return nullptr;
518518
}
519519

520+
const CallEventRef<> CallEvent::getCaller() const {
521+
const auto *CallLocationContext = this->getLocationContext();
522+
if (!CallLocationContext || CallLocationContext->inTopFrame())
523+
return nullptr;
524+
525+
const auto *CallStackFrameContext = CallLocationContext->getStackFrame();
526+
if (!CallStackFrameContext)
527+
return nullptr;
528+
529+
CallEventManager &CEMgr = State->getStateManager().getCallEventManager();
530+
return CEMgr.getCaller(CallStackFrameContext, State);
531+
}
532+
533+
bool CallEvent::calledFromSystemHeader() const {
534+
if (const CallEventRef<> Caller = getCaller())
535+
return Caller->isInSystemHeader();
536+
537+
return false;
538+
}
539+
520540
std::optional<SVal> CallEvent::getReturnValueUnderConstruction() const {
521541
const auto *CC = getConstructionContext();
522542
if (!CC)

0 commit comments

Comments
 (0)