[libcxx-commits] [PATCH] D89545: [libcxx] Fix printf formats in two tests.

Simon Tatham via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Fri Oct 16 05:43:01 PDT 2020


simon_tatham created this revision.
simon_tatham added reviewers: ldionne, miyuki.
Herald added subscribers: libcxx-commits, dexonsmith.
Herald added a project: libc++.
Herald added a reviewer: libc++.
simon_tatham requested review of this revision.

rGcc69d211d0d65d7b <https://reviews.llvm.org/rGcc69d211d0d65d7bf0335fecbc323f784ac3afcc> introduced several uses of `printf` with format
directives `%lu` and `%ld` to format values of type `size_t` and
`ptrdiff_t` respectively.

That doesn't reliably work in all C implementations, because those
types aren't necessarily the same thing as 'long int': sometimes
they're not even the same size, and when they are the same size, they
might be officially defined as int rather than long (for example),
which causes clang to emit a diagnostic for the mismatch.

C has special-purpose printf modifier letters for these two types, so
it's safer to use them. Changed all `%lu` on `size_t` to `%zu`, and
all `%ld` on `ptrdiff_t` to `%td`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D89545

Files:
  libcxx/test/libcxx/containers/sequences/deque/spare_block_handling.pass.cpp
  libcxx/test/std/containers/associative/map/map.cons/copy_assign.pass.cpp


Index: libcxx/test/std/containers/associative/map/map.cons/copy_assign.pass.cpp
===================================================================
--- libcxx/test/std/containers/associative/map/map.cons/copy_assign.pass.cpp
+++ libcxx/test/std/containers/associative/map/map.cons/copy_assign.pass.cpp
@@ -77,7 +77,8 @@
 bool balanced_allocs() {
     std::vector<int> temp1, temp2;
 
-    std::printf("Allocations = %lu, deallocations = %lu\n", ca_allocs.size(), ca_deallocs.size());
+    std::printf("Allocations = %zu, deallocations = %zu\n", ca_allocs.size(),
+                ca_deallocs.size());
     if (ca_allocs.size() != ca_deallocs.size())
         return false;
 
@@ -85,12 +86,12 @@
     std::sort(temp1.begin(), temp1.end());
     temp2.clear();
     std::unique_copy(temp1.begin(), temp1.end(), std::back_inserter<std::vector<int>>(temp2));
-    std::printf("There were %lu different allocators\n", temp2.size());
+    std::printf("There were %zu different allocators\n", temp2.size());
 
     for (std::vector<int>::const_iterator it = temp2.begin(); it != temp2.end(); ++it ) {
         std::ptrdiff_t const allocs = std::count(ca_allocs.begin(), ca_allocs.end(), *it);
         std::ptrdiff_t const deallocs = std::count(ca_deallocs.begin(), ca_deallocs.end(), *it);
-        std::printf("%d: %ld vs %ld\n", *it, allocs, deallocs);
+        std::printf("%d: %td vs %td\n", *it, allocs, deallocs);
         if (allocs != deallocs)
             return false;
     }
@@ -99,12 +100,12 @@
     std::sort(temp1.begin(), temp1.end());
     temp2.clear();
     std::unique_copy(temp1.begin(), temp1.end(), std::back_inserter<std::vector<int>>(temp2));
-    std::printf("There were %lu different (de)allocators\n", temp2.size());
+    std::printf("There were %zu different (de)allocators\n", temp2.size());
 
     for (std::vector<int>::const_iterator it = ca_deallocs.begin(); it != ca_deallocs.end(); ++it ) {
         std::ptrdiff_t const allocs = std::count(ca_allocs.begin(), ca_allocs.end(), *it);
         std::ptrdiff_t const deallocs = std::count(ca_deallocs.begin(), ca_deallocs.end(), *it);
-        std::printf("%d: %ld vs %ld\n", *it, allocs, deallocs);
+        std::printf("%d: %td vs %td\n", *it, allocs, deallocs);
         if (allocs != deallocs)
             return false;
     }
Index: libcxx/test/libcxx/containers/sequences/deque/spare_block_handling.pass.cpp
===================================================================
--- libcxx/test/libcxx/containers/sequences/deque/spare_block_handling.pass.cpp
+++ libcxx/test/libcxx/containers/sequences/deque/spare_block_handling.pass.cpp
@@ -31,12 +31,12 @@
 
 template <class Deque>
 static void print(const Deque& d) {
-  std::printf("%lu : __front_spare() == %lu"
-                 " : __back_spare() == %lu"
-                 " : __capacity() == %lu"
-                 " : bytes allocated == %lu\n",
-      d.size(), d.__front_spare(), d.__back_spare(), d.__capacity(),
-      malloc_allocator_base::outstanding_bytes);
+  std::printf("%zu : __front_spare() == %zu"
+              " : __back_spare() == %zu"
+              " : __capacity() == %zu"
+              " : bytes allocated == %zu\n",
+              d.size(), d.__front_spare(), d.__back_spare(), d.__capacity(),
+              malloc_allocator_base::outstanding_bytes);
 }
 
 template <class T>


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D89545.298603.patch
Type: text/x-patch
Size: 3340 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20201016/cb50125b/attachment-0001.bin>


More information about the libcxx-commits mailing list