25 #include "llvm/ADT/SmallString.h" 26 #include "llvm/ADT/StringSwitch.h" 27 #include "llvm/Support/raw_ostream.h" 29 using namespace clang;
33 class MacOSXAPIChecker :
public Checker< check::PreStmt<CallExpr> > {
34 mutable std::unique_ptr<BugType> BT_dispatchOnce;
42 StringRef FName)
const;
46 StringRef FName)
const;
55 MacOSXAPIChecker::getParentIvarRegion(
const MemRegion *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)"));
143 auto report = llvm::make_unique<BugReport>(*BT_dispatchOnce, os.str(), N);
152 void MacOSXAPIChecker::checkPreStmt(
const CallExpr *CE,
159 llvm::StringSwitch<SubChecker>(Name)
160 .Cases(
"dispatch_once",
163 &MacOSXAPIChecker::CheckDispatchOnce)
167 (this->*SC)(C, CE, Name);
MemRegion - The root abstract class for all memory regions.
ExplodedNode * generateErrorNode(ProgramStateRef State=nullptr, const ProgramPointTag *Tag=nullptr)
Generate a transition to a node that will be used to report an error.
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this call.
MemSpaceRegion - A memory region that represents a "memory space"; for example, the set of global var...
Represents a variable declaration or definition.
SVal getSVal(const Stmt *S) const
Get the value of arbitrary expressions at this point in the path.
const MemRegion * getSuperRegion() const
StringRef getCalleeName(const FunctionDecl *FunDecl) const
Get the name of the called function (path-sensitive).
const MemSpaceRegion * getMemorySpace() const
CHECKER * registerChecker(AT... Args)
Used to register checkers.
void emitReport(std::unique_ptr< BugReport > R)
Emit the diagnostics report.
Dataflow Directional Tag Classes.
SourceLocation getLocStart() const LLVM_READONLY
SubRegion - A region that subsets another larger region.
bool isStaticLocal() const
Returns true if a variable with function scope is a static local variable.
const MemRegion * getBaseRegion() const
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.