lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [lmi] master e1482a3 5/6: Add unit tests for stifle_unused


From: Greg Chicares
Subject: [lmi-commits] [lmi] master e1482a3 5/6: Add unit tests for stifle_unused_warning()
Date: Wed, 27 Oct 2021 18:22:22 -0400 (EDT)

branch: master
commit e1482a3ca4fe377ba5b541bfbf35d1e06d24bb18
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>

    Add unit tests for stifle_unused_warning()
---
 miscellany_test.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)

diff --git a/miscellany_test.cpp b/miscellany_test.cpp
index 9a0506f..0de17a6 100644
--- a/miscellany_test.cpp
+++ b/miscellany_test.cpp
@@ -28,6 +28,7 @@
 #include <cfloat>                       // DBL_MAX
 #include <cmath>                        // HUGE_VAL
 #include <cstdio>                       // remove()
+#include <ctime>                        // clock()
 #include <fstream>
 #include <iomanip>
 #include <limits>
@@ -449,6 +450,61 @@ void test_scoped_ios_format()
     LMI_TEST_EQUAL(oss.str(), s);
 }
 
+class partly_unused
+{
+  public:
+    partly_unused
+        (int used
+        ,int unused
+        )
+        :used_   {used}
+        ,unused_ {unused}
+        {
+        // Suppress clang '-Wunused-private-field' warnings:
+        stifle_unused_warning(unused_);
+        }
+    int used() {return used_;}
+
+  private:
+    int used_;
+    int unused_;
+};
+
+void test_stifle_unused_warning()
+{
+    // Variable neither initialized nor used.
+    int a;
+    stifle_unused_warning(a);
+
+    // Variable initialized but not used.
+    int b {2};
+    stifle_unused_warning(b);
+
+    // Variable initialized and used, but only conditionally.
+    int c;
+#if defined some_undefined_condition
+    c = 3;
+    std::cout << c << " This should not print" << std::endl;
+#endif // defined some_undefined_condition
+    stifle_unused_warning(c);
+
+    // Variable initialized and later used...
+    int volatile d {4};
+//  stifle_unused_warning(d); // [see below]
+    // ...but last value assigned...
+    for(int i = 0; i < 7; ++i)
+        {
+        d = static_cast<int>(std::clock());
+        }
+    // ...is not subsequently used. In this case, for clang at least,
+    // it is necessary to stifle the warning here:
+    stifle_unused_warning(d);
+    // rather than in the commented-out location above. See:
+    //   https://lists.nongnu.org/archive/html/lmi/2021-04/msg00058.html
+
+    partly_unused {0, 1};
+}
+
 int test_main(int, char*[])
 {
     test_each_equal();
@@ -459,6 +515,7 @@ int test_main(int, char*[])
     test_scale_power();
     test_trimming();
     test_scoped_ios_format();
+    test_stifle_unused_warning();
 
     return 0;
 }



reply via email to

[Prev in Thread] Current Thread [Next in Thread]