[PATCH] unsigned count() for ADT

David Blaikie dblaikie at gmail.com
Tue Jun 10 08:24:27 PDT 2014

Comment at: lib/CodeGen/AsmPrinter/WinCodeViewLineTables.cpp:311
@@ -310,3 +310,3 @@
   const Function *GV = MF->getFunction();
-  assert(FnDebugInfo.count(GV) == true);
+  assert(FnDebugInfo.count(GV) == 1);
   assert(CurFn == &FnDebugInfo[GV]);
It's common to write a "does this container contain this thing" as simply boolean testing the result of 'count':


Personally this is easier for me to read because I don't have to worry about "is this container a multimap and are they testing that there's /exactly/ one element" (and writing it as "> 0" when it's a single map is also a little confusing, even though it's semantically equivalent to the non-zero count test above)

Not a requirement, just a though - maybe others feel more strongly.

Comment at: lib/IR/Metadata.cpp:666
@@ -665,3 +665,3 @@
   assert((hasMetadataHashEntry() ==
-          getContext().pImpl->MetadataStore.count(this)) &&
+          (getContext().pImpl->MetadataStore.count(this) == 1)) &&
          "HasMetadata bit out of date!");
same here, though I guess this is a bit more interesting as in this case the bool on the LHS would be converted to an int and compared... but you're == 1, so it is the same whether it's a multimap or not (ie: it's only true if there's exactly one thing)... not sure how I'd prefer to write this one.


More information about the llvm-commits mailing list