24 #include "llvm/ADT/SmallString.h" 25 #include "llvm/ADT/StringSwitch.h" 26 #include "llvm/Support/raw_ostream.h" 28 using namespace clang;
32 class MacOSXAPIChecker :
public Checker< check::PreStmt<CallExpr> > {
33 mutable std::unique_ptr<BugType> BT_dispatchOnce;
35 static const ObjCIvarRegion *getParentIvarRegion(
const MemRegion *R);
38 void checkPreStmt(
const CallExpr *CE, CheckerContext &C)
const;
40 void CheckDispatchOnce(CheckerContext &C,
const CallExpr *CE,
41 StringRef FName)
const;
43 typedef void (MacOSXAPIChecker::*SubChecker)(CheckerContext &,
45 StringRef FName)
const;
53 const ObjCIvarRegion *
54 MacOSXAPIChecker::getParentIvarRegion(
const MemRegion *R) {
55 const SubRegion *SR = dyn_cast<SubRegion>(R);
57 if (
const ObjCIvarRegion *IR = dyn_cast<ObjCIvarRegion>(SR))
59 SR = dyn_cast<SubRegion>(SR->getSuperRegion());
64 void MacOSXAPIChecker::CheckDispatchOnce(CheckerContext &C,
const CallExpr *CE,
65 StringRef FName)
const {
71 const MemRegion *R = C.getSVal(CE->
getArg(0)).getAsRegion();
76 const MemRegion *RB = R->getBaseRegion();
77 const MemSpaceRegion *RS = RB->getMemorySpace();
78 if (isa<GlobalsSpaceRegion>(RS))
86 StringRef TrimmedFName = FName.ltrim(
'_');
87 if (TrimmedFName != FName)
92 llvm::raw_svector_ostream os(S);
93 bool SuggestStatic =
false;
94 os <<
"Call to '" << FName <<
"' uses";
95 if (
const VarRegion *VR = dyn_cast<VarRegion>(RB)) {
96 const VarDecl *VD = VR->getDecl();
106 os <<
" memory within";
108 os <<
" the block variable '";
110 os <<
" the local variable '";
111 os << VR->getDecl()->
getName() <<
'\'';
112 SuggestStatic =
true;
113 }
else if (
const ObjCIvarRegion *IVR = getParentIvarRegion(R)) {
115 os <<
" memory within";
116 os <<
" the instance variable '" << IVR->getDecl()->getName() <<
'\'';
117 }
else if (isa<HeapSpaceRegion>(RS)) {
118 os <<
" heap-allocated memory";
119 }
else if (isa<UnknownSpaceRegion>(RS)) {
127 os <<
" stack allocated memory";
129 os <<
" for the predicate value. Using such transient memory for " 130 "the predicate is potentially dangerous.";
132 os <<
" Perhaps you intended to declare the variable as 'static'?";
134 ExplodedNode *N = C.generateErrorNode();
138 if (!BT_dispatchOnce)
139 BT_dispatchOnce.reset(
new BugType(
this,
"Improper use of 'dispatch_once'",
140 "API Misuse (Apple)"));
142 auto report = llvm::make_unique<BugReport>(*BT_dispatchOnce, os.str(), N);
144 C.emitReport(std::move(report));
151 void MacOSXAPIChecker::checkPreStmt(
const CallExpr *CE,
152 CheckerContext &C)
const {
153 StringRef Name = C.getCalleeName(CE);
158 llvm::StringSwitch<SubChecker>(Name)
159 .Cases(
"dispatch_once",
162 &MacOSXAPIChecker::CheckDispatchOnce)
166 (this->*SC)(C, CE, Name);
173 void ento::registerMacOSXAPIChecker(CheckerManager &mgr) {
174 mgr.registerChecker<MacOSXAPIChecker>();
177 bool ento::shouldRegisterMacOSXAPIChecker(
const LangOptions &LO) {
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this call.
Represents a variable declaration or definition.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Dataflow Directional Tag Classes.
bool isStaticLocal() const
Returns true if a variable with function scope is a static local variable.
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Defines the clang::TargetInfo interface.
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
SourceLocation getBeginLoc() const LLVM_READONLY