[libc-commits] [libc] [libc] Use `if constexpr` for compile-time conditionals (PR #113417)

via libc-commits libc-commits at lists.llvm.org
Tue Oct 22 21:37:57 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Roland McGrath (frobtech)

<details>
<summary>Changes</summary>

Don't use plain `if` for things that are compile-time constants.
Instead, use `if constexpr`.  This both ensures that these are
properly wired up constant expressions as intended, and prevents
warnings from the compiler about useless `if` checks that look in
the source like they're meant to do something at runtime but will
just be compiled away.


---
Full diff: https://github.com/llvm/llvm-project/pull/113417.diff


1 Files Affected:

- (modified) libc/test/UnitTest/FPMatcher.h (+10-10) 


``````````diff
diff --git a/libc/test/UnitTest/FPMatcher.h b/libc/test/UnitTest/FPMatcher.h
index bdcc22ef94e76b..7fcc6a32025b5d 100644
--- a/libc/test/UnitTest/FPMatcher.h
+++ b/libc/test/UnitTest/FPMatcher.h
@@ -124,35 +124,35 @@ template <typename T, TestCond Condition> class CFPMatcher : public Matcher<T> {
 
   bool match(T actualValue) {
     actual = actualValue;
-    if (cpp::is_complex_type_same<T, _Complex float>())
+    if constexpr (cpp::is_complex_type_same<T, _Complex float>())
       return matchComplex<float>();
-    else if (cpp::is_complex_type_same<T, _Complex double>())
+    else if constexpr (cpp::is_complex_type_same<T, _Complex double>())
       return matchComplex<double>();
-    else if (cpp::is_complex_type_same<T, _Complex long double>())
+    else if constexpr (cpp::is_complex_type_same<T, _Complex long double>())
       return matchComplex<long double>();
 #ifdef LIBC_TYPES_HAS_CFLOAT16
-    else if (cpp::is_complex_type_same<T, cfloat16>)
+    else if constexpr (cpp::is_complex_type_same<T, cfloat16>)
       return matchComplex<float16>();
 #endif
 #ifdef LIBC_TYPES_HAS_CFLOAT128
-    else if (cpp::is_complex_type_same<T, cfloat128>)
+    else if constexpr (cpp::is_complex_type_same<T, cfloat128>)
       return matchComplex<float128>();
 #endif
   }
 
   void explainError() override {
-    if (cpp::is_complex_type_same<T, _Complex float>())
+    if constexpr (cpp::is_complex_type_same<T, _Complex float>())
       return explainErrorComplex<float>();
-    else if (cpp::is_complex_type_same<T, _Complex double>())
+    else if constexpr (cpp::is_complex_type_same<T, _Complex double>())
       return explainErrorComplex<double>();
-    else if (cpp::is_complex_type_same<T, _Complex long double>())
+    else if constexpr (cpp::is_complex_type_same<T, _Complex long double>())
       return explainErrorComplex<long double>();
 #ifdef LIBC_TYPES_HAS_CFLOAT16
-    else if (cpp::is_complex_type_same<T, cfloat16>)
+    else if constexpr (cpp::is_complex_type_same<T, cfloat16>)
       return explainErrorComplex<float16>();
 #endif
 #ifdef LIBC_TYPES_HAS_CFLOAT128
-    else if (cpp::is_complex_type_same<T, cfloat128>)
+    else if constexpr (cpp::is_complex_type_same<T, cfloat128>)
       return explainErrorComplex<float128>();
 #endif
   }

``````````

</details>


https://github.com/llvm/llvm-project/pull/113417


More information about the libc-commits mailing list