[clang] 97f1bf1 - [analyzer][NFC] Consolidate the inner representation of CallDescriptions

Balazs Benics via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 19 09:32:57 PST 2021


Author: Balazs Benics
Date: 2021-11-19T18:32:13+01:00
New Revision: 97f1bf15b154ef32608fe17b82f2f312401d150c

URL: https://github.com/llvm/llvm-project/commit/97f1bf15b154ef32608fe17b82f2f312401d150c
DIFF: https://github.com/llvm/llvm-project/commit/97f1bf15b154ef32608fe17b82f2f312401d150c.diff

LOG: [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

Added: 
    

Modified: 
    clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h
    clang/lib/StaticAnalyzer/Core/CallDescription.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h
index abc2b93e32fe..88f67a03acfe 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h
@@ -40,12 +40,14 @@ enum CallDescriptionFlags : int {
 /// arguments and the name of the function.
 class CallDescription {
   friend class CallEvent;
+  using MaybeUInt = Optional<unsigned>;
+
   mutable Optional<const IdentifierInfo *> II;
   // The list of the qualified names used to identify the specified CallEvent,
   // e.g. "{a, b}" represent the qualified names, like "a::b".
   std::vector<std::string> QualifiedName;
-  Optional<unsigned> RequiredArgs;
-  Optional<size_t> RequiredParams;
+  MaybeUInt RequiredArgs;
+  MaybeUInt RequiredParams;
   int Flags;
 
 public:
@@ -60,13 +62,13 @@ class CallDescription {
   /// call. Omit this parameter to match every occurrence of call with a given
   /// name regardless the number of arguments.
   CallDescription(int Flags, ArrayRef<const char *> QualifiedName,
-                  Optional<unsigned> RequiredArgs = None,
-                  Optional<size_t> RequiredParams = None);
+                  MaybeUInt RequiredArgs = None,
+                  MaybeUInt RequiredParams = None);
 
   /// Construct a CallDescription with default flags.
   CallDescription(ArrayRef<const char *> QualifiedName,
-                  Optional<unsigned> RequiredArgs = None,
-                  Optional<size_t> RequiredParams = None);
+                  MaybeUInt RequiredArgs = None,
+                  MaybeUInt RequiredParams = None);
 
   CallDescription(std::nullptr_t) = delete;
 

diff  --git a/clang/lib/StaticAnalyzer/Core/CallDescription.cpp b/clang/lib/StaticAnalyzer/Core/CallDescription.cpp
index af541bdcfd59..9274f8a41165 100644
--- a/clang/lib/StaticAnalyzer/Core/CallDescription.cpp
+++ b/clang/lib/StaticAnalyzer/Core/CallDescription.cpp
@@ -22,20 +22,22 @@
 using namespace llvm;
 using namespace clang;
 
+using MaybeUInt = Optional<unsigned>;
+
 // A constructor helper.
-static Optional<size_t> readRequiredParams(Optional<unsigned> RequiredArgs,
-                                           Optional<size_t> RequiredParams) {
+static MaybeUInt readRequiredParams(MaybeUInt RequiredArgs,
+                                    MaybeUInt RequiredParams) {
   if (RequiredParams)
     return RequiredParams;
   if (RequiredArgs)
-    return static_cast<size_t>(*RequiredArgs);
+    return RequiredArgs;
   return None;
 }
 
-ento::CallDescription::CallDescription(
-    int Flags, ArrayRef<const char *> QualifiedName,
-    Optional<unsigned> RequiredArgs /*= None*/,
-    Optional<size_t> RequiredParams /*= None*/)
+ento::CallDescription::CallDescription(int Flags,
+                                       ArrayRef<const char *> QualifiedName,
+                                       MaybeUInt RequiredArgs /*= None*/,
+                                       MaybeUInt RequiredParams /*= None*/)
     : RequiredArgs(RequiredArgs),
       RequiredParams(readRequiredParams(RequiredArgs, RequiredParams)),
       Flags(Flags) {
@@ -45,10 +47,9 @@ ento::CallDescription::CallDescription(
 }
 
 /// Construct a CallDescription with default flags.
-ento::CallDescription::CallDescription(
-    ArrayRef<const char *> QualifiedName,
-    Optional<unsigned> RequiredArgs /*= None*/,
-    Optional<size_t> RequiredParams /*= None*/)
+ento::CallDescription::CallDescription(ArrayRef<const char *> QualifiedName,
+                                       MaybeUInt RequiredArgs /*= None*/,
+                                       MaybeUInt RequiredParams /*= None*/)
     : CallDescription(0, QualifiedName, RequiredArgs, RequiredParams) {}
 
 bool ento::CallDescription::matches(const CallEvent &Call) const {
@@ -62,8 +63,8 @@ bool ento::CallDescription::matches(const CallEvent &Call) const {
 
   if (Flags & CDF_MaybeBuiltin) {
     return CheckerContext::isCLibraryFunction(FD, getFunctionName()) &&
-           (!RequiredArgs || RequiredArgs <= Call.getNumArgs()) &&
-           (!RequiredParams || RequiredParams <= Call.parameters().size());
+           (!RequiredArgs || *RequiredArgs <= Call.getNumArgs()) &&
+           (!RequiredParams || *RequiredParams <= Call.parameters().size());
   }
 
   if (!II.hasValue()) {
@@ -87,9 +88,9 @@ bool ento::CallDescription::matches(const CallEvent &Call) const {
   const auto ExactMatchArgAndParamCounts =
       [](const CallEvent &Call, const CallDescription &CD) -> bool {
     const bool ArgsMatch =
-        !CD.RequiredArgs || CD.RequiredArgs == Call.getNumArgs();
+        !CD.RequiredArgs || *CD.RequiredArgs == Call.getNumArgs();
     const bool ParamsMatch =
-        !CD.RequiredParams || CD.RequiredParams == Call.parameters().size();
+        !CD.RequiredParams || *CD.RequiredParams == Call.parameters().size();
     return ArgsMatch && ParamsMatch;
   };
 


        


More information about the cfe-commits mailing list