25#include "llvm/ADT/SmallString.h"
26#include "llvm/ADT/StringSwitch.h"
27#include "llvm/Support/raw_ostream.h"
33class MacOSXAPIChecker :
public Checker< check::PreStmt<CallExpr> > {
34 mutable std::unique_ptr<BugType> BT_dispatchOnce;
42 StringRef FName)
const;
46 StringRef FName)
const;
55MacOSXAPIChecker::getParentIvarRegion(
const MemRegion *R) {
56 const SubRegion *SR = dyn_cast<SubRegion>(R);
66 StringRef FName)
const {
79 if (isa<GlobalsSpaceRegion>(RS))
87 StringRef TrimmedFName = FName.ltrim(
'_');
88 if (TrimmedFName != FName)
93 llvm::raw_svector_ostream os(S);
94 bool SuggestStatic =
false;
95 os <<
"Call to '" << FName <<
"' uses";
96 if (
const VarRegion *VR = dyn_cast<VarRegion>(RB)) {
97 const VarDecl *VD = VR->getDecl();
107 os <<
" memory within";
109 os <<
" the block variable '";
111 os <<
" the local variable '";
112 os << VR->getDecl()->
getName() <<
'\'';
113 SuggestStatic =
true;
116 os <<
" memory within";
117 os <<
" the instance variable '" << IVR->getDecl()->getName() <<
'\'';
118 }
else if (isa<HeapSpaceRegion>(RS)) {
119 os <<
" heap-allocated memory";
120 }
else if (isa<UnknownSpaceRegion>(RS)) {
128 os <<
" stack allocated memory";
130 os <<
" for the predicate value. Using such transient memory for "
131 "the predicate is potentially dangerous.";
133 os <<
" Perhaps you intended to declare the variable as 'static'?";
139 if (!BT_dispatchOnce)
140 BT_dispatchOnce.reset(
new BugType(
this,
"Improper use of 'dispatch_once'",
141 "API Misuse (Apple)"));
144 std::make_unique<PathSensitiveBugReport>(*BT_dispatchOnce, os.str(), N);
146 C.emitReport(std::move(report));
153void MacOSXAPIChecker::checkPreStmt(
const CallExpr *CE,
155 StringRef Name =
C.getCalleeName(CE);
160 llvm::StringSwitch<SubChecker>(Name)
161 .Cases(
"dispatch_once",
164 &MacOSXAPIChecker::CheckDispatchOnce)
168 (this->*SC)(
C, CE, Name);
179bool ento::shouldRegisterMacOSXAPIChecker(
const CheckerManager &mgr) {
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
SourceLocation getBeginLoc() const LLVM_READONLY
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this call.
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Represents a variable declaration or definition.
bool isStaticLocal() const
Returns true if a variable with function scope is a static local variable.
CHECKER * registerChecker(AT &&... Args)
Used to register checkers.
MemRegion - The root abstract class for all memory regions.
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemSpaceRegion * getMemorySpace() const
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemRegion * getBaseRegion() const
MemSpaceRegion - A memory region that represents a "memory space"; for example, the set of global var...
SubRegion - A region that subsets another larger region.
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemRegion * getSuperRegion() const
Defines the clang::TargetInfo interface.