Skip to content

Commit 97f1bf1

Browse files
author
Balazs Benics
committed
[analyzer][NFC] Consolidate the inner representation of CallDescriptions
`CallDescriptions` have a `RequiredArgs` and `RequiredParams` members, but they are of different types, `unsigned` and `size_t` respectively. In the patch I use only `unsigned` for both, that should be large enough anyway. I also introduce the `MaybeUInt` type alias for `Optional<unsigned>`. Additionally, I also avoid the use of the //smart// less-than operator. template <typename T> constexpr bool operator<=(const Optional<T> &X, const T &Y); Which would check if the optional **has** a value and compare the data only after. I found it surprising, thus I think we are better off without it. Reviewed By: martong, xazax.hun Differential Revision: https://reviews.llvm.org/D113594
1 parent de9d7e4 commit 97f1bf1

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ enum CallDescriptionFlags : int {
4040
/// arguments and the name of the function.
4141
class CallDescription {
4242
friend class CallEvent;
43+
using MaybeUInt = Optional<unsigned>;
44+
4345
mutable Optional<const IdentifierInfo *> II;
4446
// The list of the qualified names used to identify the specified CallEvent,
4547
// e.g. "{a, b}" represent the qualified names, like "a::b".
4648
std::vector<std::string> QualifiedName;
47-
Optional<unsigned> RequiredArgs;
48-
Optional<size_t> RequiredParams;
49+
MaybeUInt RequiredArgs;
50+
MaybeUInt RequiredParams;
4951
int Flags;
5052

5153
public:
@@ -60,13 +62,13 @@ class CallDescription {
6062
/// call. Omit this parameter to match every occurrence of call with a given
6163
/// name regardless the number of arguments.
6264
CallDescription(int Flags, ArrayRef<const char *> QualifiedName,
63-
Optional<unsigned> RequiredArgs = None,
64-
Optional<size_t> RequiredParams = None);
65+
MaybeUInt RequiredArgs = None,
66+
MaybeUInt RequiredParams = None);
6567

6668
/// Construct a CallDescription with default flags.
6769
CallDescription(ArrayRef<const char *> QualifiedName,
68-
Optional<unsigned> RequiredArgs = None,
69-
Optional<size_t> RequiredParams = None);
70+
MaybeUInt RequiredArgs = None,
71+
MaybeUInt RequiredParams = None);
7072

7173
CallDescription(std::nullptr_t) = delete;
7274

clang/lib/StaticAnalyzer/Core/CallDescription.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,22 @@
2222
using namespace llvm;
2323
using namespace clang;
2424

25+
using MaybeUInt = Optional<unsigned>;
26+
2527
// A constructor helper.
26-
static Optional<size_t> readRequiredParams(Optional<unsigned> RequiredArgs,
27-
Optional<size_t> RequiredParams) {
28+
static MaybeUInt readRequiredParams(MaybeUInt RequiredArgs,
29+
MaybeUInt RequiredParams) {
2830
if (RequiredParams)
2931
return RequiredParams;
3032
if (RequiredArgs)
31-
return static_cast<size_t>(*RequiredArgs);
33+
return RequiredArgs;
3234
return None;
3335
}
3436

35-
ento::CallDescription::CallDescription(
36-
int Flags, ArrayRef<const char *> QualifiedName,
37-
Optional<unsigned> RequiredArgs /*= None*/,
38-
Optional<size_t> RequiredParams /*= None*/)
37+
ento::CallDescription::CallDescription(int Flags,
38+
ArrayRef<const char *> QualifiedName,
39+
MaybeUInt RequiredArgs /*= None*/,
40+
MaybeUInt RequiredParams /*= None*/)
3941
: RequiredArgs(RequiredArgs),
4042
RequiredParams(readRequiredParams(RequiredArgs, RequiredParams)),
4143
Flags(Flags) {
@@ -45,10 +47,9 @@ ento::CallDescription::CallDescription(
4547
}
4648

4749
/// Construct a CallDescription with default flags.
48-
ento::CallDescription::CallDescription(
49-
ArrayRef<const char *> QualifiedName,
50-
Optional<unsigned> RequiredArgs /*= None*/,
51-
Optional<size_t> RequiredParams /*= None*/)
50+
ento::CallDescription::CallDescription(ArrayRef<const char *> QualifiedName,
51+
MaybeUInt RequiredArgs /*= None*/,
52+
MaybeUInt RequiredParams /*= None*/)
5253
: CallDescription(0, QualifiedName, RequiredArgs, RequiredParams) {}
5354

5455
bool ento::CallDescription::matches(const CallEvent &Call) const {
@@ -62,8 +63,8 @@ bool ento::CallDescription::matches(const CallEvent &Call) const {
6263

6364
if (Flags & CDF_MaybeBuiltin) {
6465
return CheckerContext::isCLibraryFunction(FD, getFunctionName()) &&
65-
(!RequiredArgs || RequiredArgs <= Call.getNumArgs()) &&
66-
(!RequiredParams || RequiredParams <= Call.parameters().size());
66+
(!RequiredArgs || *RequiredArgs <= Call.getNumArgs()) &&
67+
(!RequiredParams || *RequiredParams <= Call.parameters().size());
6768
}
6869

6970
if (!II.hasValue()) {
@@ -87,9 +88,9 @@ bool ento::CallDescription::matches(const CallEvent &Call) const {
8788
const auto ExactMatchArgAndParamCounts =
8889
[](const CallEvent &Call, const CallDescription &CD) -> bool {
8990
const bool ArgsMatch =
90-
!CD.RequiredArgs || CD.RequiredArgs == Call.getNumArgs();
91+
!CD.RequiredArgs || *CD.RequiredArgs == Call.getNumArgs();
9192
const bool ParamsMatch =
92-
!CD.RequiredParams || CD.RequiredParams == Call.parameters().size();
93+
!CD.RequiredParams || *CD.RequiredParams == Call.parameters().size();
9394
return ArgsMatch && ParamsMatch;
9495
};
9596

0 commit comments

Comments
 (0)