Skip to content

Commit a62441d

Browse files
[clang][analyzer][NFC] UnixAPIMisuseChecker inherits from Checker<check::PreCall> (#83027)
1 parent 6e755c5 commit a62441d

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

clang/lib/StaticAnalyzer/Checkers/UnixAPIChecker.cpp

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "clang/StaticAnalyzer/Core/BugReporter/CommonBugCategories.h"
1818
#include "clang/StaticAnalyzer/Core/Checker.h"
1919
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
20+
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
2021
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
2122
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h"
2223
#include "llvm/ADT/STLExtras.h"
@@ -41,8 +42,7 @@ enum class OpenVariant {
4142
namespace {
4243

4344
class UnixAPIMisuseChecker
44-
: public Checker<check::PreStmt<CallExpr>,
45-
check::ASTDecl<TranslationUnitDecl>> {
45+
: public Checker<check::PreCall, check::ASTDecl<TranslationUnitDecl>> {
4646
const BugType BT_open{this, "Improper use of 'open'", categories::UnixAPI};
4747
const BugType BT_pthreadOnce{this, "Improper use of 'pthread_once'",
4848
categories::UnixAPI};
@@ -52,14 +52,14 @@ class UnixAPIMisuseChecker
5252
void checkASTDecl(const TranslationUnitDecl *TU, AnalysisManager &Mgr,
5353
BugReporter &BR) const;
5454

55-
void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
55+
void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
5656

57-
void CheckOpen(CheckerContext &C, const CallExpr *CE) const;
58-
void CheckOpenAt(CheckerContext &C, const CallExpr *CE) const;
59-
void CheckPthreadOnce(CheckerContext &C, const CallExpr *CE) const;
57+
void CheckOpen(CheckerContext &C, const CallEvent &Call) const;
58+
void CheckOpenAt(CheckerContext &C, const CallEvent &Call) const;
59+
void CheckPthreadOnce(CheckerContext &C, const CallEvent &Call) const;
6060

61-
void CheckOpenVariant(CheckerContext &C,
62-
const CallExpr *CE, OpenVariant Variant) const;
61+
void CheckOpenVariant(CheckerContext &C, const CallEvent &Call,
62+
OpenVariant Variant) const;
6363

6464
void ReportOpenBug(CheckerContext &C, ProgramStateRef State, const char *Msg,
6565
SourceRange SR) const;
@@ -113,9 +113,9 @@ void UnixAPIMisuseChecker::checkASTDecl(const TranslationUnitDecl *TU,
113113
// "open" (man 2 open)
114114
//===----------------------------------------------------------------------===/
115115

116-
void UnixAPIMisuseChecker::checkPreStmt(const CallExpr *CE,
116+
void UnixAPIMisuseChecker::checkPreCall(const CallEvent &Call,
117117
CheckerContext &C) const {
118-
const FunctionDecl *FD = C.getCalleeDecl(CE);
118+
const FunctionDecl *FD = dyn_cast_if_present<FunctionDecl>(Call.getDecl());
119119
if (!FD || FD->getKind() != Decl::Function)
120120
return;
121121

@@ -130,13 +130,13 @@ void UnixAPIMisuseChecker::checkPreStmt(const CallExpr *CE,
130130
return;
131131

132132
if (FName == "open")
133-
CheckOpen(C, CE);
133+
CheckOpen(C, Call);
134134

135135
else if (FName == "openat")
136-
CheckOpenAt(C, CE);
136+
CheckOpenAt(C, Call);
137137

138138
else if (FName == "pthread_once")
139-
CheckPthreadOnce(C, CE);
139+
CheckPthreadOnce(C, Call);
140140
}
141141
void UnixAPIMisuseChecker::ReportOpenBug(CheckerContext &C,
142142
ProgramStateRef State,
@@ -152,17 +152,17 @@ void UnixAPIMisuseChecker::ReportOpenBug(CheckerContext &C,
152152
}
153153

154154
void UnixAPIMisuseChecker::CheckOpen(CheckerContext &C,
155-
const CallExpr *CE) const {
156-
CheckOpenVariant(C, CE, OpenVariant::Open);
155+
const CallEvent &Call) const {
156+
CheckOpenVariant(C, Call, OpenVariant::Open);
157157
}
158158

159159
void UnixAPIMisuseChecker::CheckOpenAt(CheckerContext &C,
160-
const CallExpr *CE) const {
161-
CheckOpenVariant(C, CE, OpenVariant::OpenAt);
160+
const CallEvent &Call) const {
161+
CheckOpenVariant(C, Call, OpenVariant::OpenAt);
162162
}
163163

164164
void UnixAPIMisuseChecker::CheckOpenVariant(CheckerContext &C,
165-
const CallExpr *CE,
165+
const CallEvent &Call,
166166
OpenVariant Variant) const {
167167
// The index of the argument taking the flags open flags (O_RDONLY,
168168
// O_WRONLY, O_CREAT, etc.),
@@ -191,11 +191,11 @@ void UnixAPIMisuseChecker::CheckOpenVariant(CheckerContext &C,
191191

192192
ProgramStateRef state = C.getState();
193193

194-
if (CE->getNumArgs() < MinArgCount) {
194+
if (Call.getNumArgs() < MinArgCount) {
195195
// The frontend should issue a warning for this case. Just return.
196196
return;
197-
} else if (CE->getNumArgs() == MaxArgCount) {
198-
const Expr *Arg = CE->getArg(CreateModeArgIndex);
197+
} else if (Call.getNumArgs() == MaxArgCount) {
198+
const Expr *Arg = Call.getArgExpr(CreateModeArgIndex);
199199
QualType QT = Arg->getType();
200200
if (!QT->isIntegerType()) {
201201
SmallString<256> SBuf;
@@ -209,15 +209,15 @@ void UnixAPIMisuseChecker::CheckOpenVariant(CheckerContext &C,
209209
Arg->getSourceRange());
210210
return;
211211
}
212-
} else if (CE->getNumArgs() > MaxArgCount) {
212+
} else if (Call.getNumArgs() > MaxArgCount) {
213213
SmallString<256> SBuf;
214214
llvm::raw_svector_ostream OS(SBuf);
215215
OS << "Call to '" << VariantName << "' with more than " << MaxArgCount
216216
<< " arguments";
217217

218218
ReportOpenBug(C, state,
219219
SBuf.c_str(),
220-
CE->getArg(MaxArgCount)->getSourceRange());
220+
Call.getArgExpr(MaxArgCount)->getSourceRange());
221221
return;
222222
}
223223

@@ -226,8 +226,8 @@ void UnixAPIMisuseChecker::CheckOpenVariant(CheckerContext &C,
226226
}
227227

228228
// Now check if oflags has O_CREAT set.
229-
const Expr *oflagsEx = CE->getArg(FlagsArgIndex);
230-
const SVal V = C.getSVal(oflagsEx);
229+
const Expr *oflagsEx = Call.getArgExpr(FlagsArgIndex);
230+
const SVal V = Call.getArgSVal(FlagsArgIndex);
231231
if (!isa<NonLoc>(V)) {
232232
// The case where 'V' can be a location can only be due to a bad header,
233233
// so in this case bail out.
@@ -253,7 +253,7 @@ void UnixAPIMisuseChecker::CheckOpenVariant(CheckerContext &C,
253253
if (!(trueState && !falseState))
254254
return;
255255

256-
if (CE->getNumArgs() < MaxArgCount) {
256+
if (Call.getNumArgs() < MaxArgCount) {
257257
SmallString<256> SBuf;
258258
llvm::raw_svector_ostream OS(SBuf);
259259
OS << "Call to '" << VariantName << "' requires a "
@@ -271,18 +271,18 @@ void UnixAPIMisuseChecker::CheckOpenVariant(CheckerContext &C,
271271
//===----------------------------------------------------------------------===//
272272

273273
void UnixAPIMisuseChecker::CheckPthreadOnce(CheckerContext &C,
274-
const CallExpr *CE) const {
274+
const CallEvent &Call) const {
275275

276276
// This is similar to 'CheckDispatchOnce' in the MacOSXAPIChecker.
277277
// They can possibly be refactored.
278278

279-
if (CE->getNumArgs() < 1)
279+
if (Call.getNumArgs() < 1)
280280
return;
281281

282282
// Check if the first argument is stack allocated. If so, issue a warning
283283
// because that's likely to be bad news.
284284
ProgramStateRef state = C.getState();
285-
const MemRegion *R = C.getSVal(CE->getArg(0)).getAsRegion();
285+
const MemRegion *R = Call.getArgSVal(0).getAsRegion();
286286
if (!R || !isa<StackSpaceRegion>(R->getMemorySpace()))
287287
return;
288288

@@ -304,7 +304,7 @@ void UnixAPIMisuseChecker::CheckPthreadOnce(CheckerContext &C,
304304

305305
auto report =
306306
std::make_unique<PathSensitiveBugReport>(BT_pthreadOnce, os.str(), N);
307-
report->addRange(CE->getArg(0)->getSourceRange());
307+
report->addRange(Call.getArgExpr(0)->getSourceRange());
308308
C.emitReport(std::move(report));
309309
}
310310

0 commit comments

Comments
 (0)