[llvm] [ADT] Add [[nodiscad]] to AP*.h (NFC) (PR #161776)

via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 2 21:50:36 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-adt

Author: Kazu Hirata (kazutakahirata)

<details>
<summary>Changes</summary>

Identified with modernize-use-nodiscard.

This patch adjusts one of the unit tests.


---

Patch is 58.30 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/161776.diff


5 Files Affected:

- (modified) llvm/include/llvm/ADT/APFixedPoint.h (+34-25) 
- (modified) llvm/include/llvm/ADT/APFloat.h (+113-88) 
- (modified) llvm/include/llvm/ADT/APInt.h (+117-93) 
- (modified) llvm/include/llvm/ADT/APSInt.h (+12-8) 
- (modified) llvm/unittests/ADT/APFloatTest.cpp (+2-2) 


``````````diff
diff --git a/llvm/include/llvm/ADT/APFixedPoint.h b/llvm/include/llvm/ADT/APFixedPoint.h
index 0a47cd50516d4..6ce49f25fb988 100644
--- a/llvm/include/llvm/ADT/APFixedPoint.h
+++ b/llvm/include/llvm/ADT/APFixedPoint.h
@@ -56,28 +56,33 @@ class FixedPointSemantics {
 
   /// Check if the Semantic follow the requirements of an older more limited
   /// version of this class
-  bool isValidLegacySema() const {
+  [[nodiscard]] bool isValidLegacySema() const {
     return LsbWeight <= 0 && static_cast<int>(Width) >= -LsbWeight;
   }
-  unsigned getWidth() const { return Width; }
-  unsigned getScale() const { assert(isValidLegacySema()); return -LsbWeight; }
-  int getLsbWeight() const { return LsbWeight; }
-  int getMsbWeight() const {
+  [[nodiscard]] unsigned getWidth() const { return Width; }
+  [[nodiscard]] unsigned getScale() const {
+    assert(isValidLegacySema());
+    return -LsbWeight;
+  }
+  [[nodiscard]] int getLsbWeight() const { return LsbWeight; }
+  [[nodiscard]] int getMsbWeight() const {
     return LsbWeight + Width - 1 /*Both lsb and msb are both part of width*/;
   }
-  bool isSigned() const { return IsSigned; }
-  bool isSaturated() const { return IsSaturated; }
-  bool hasUnsignedPadding() const { return HasUnsignedPadding; }
+  [[nodiscard]] bool isSigned() const { return IsSigned; }
+  [[nodiscard]] bool isSaturated() const { return IsSaturated; }
+  [[nodiscard]] bool hasUnsignedPadding() const { return HasUnsignedPadding; }
 
   void setSaturated(bool Saturated) { IsSaturated = Saturated; }
 
   /// return true if the first bit doesn't have a strictly positive weight
-  bool hasSignOrPaddingBit() const { return IsSigned || HasUnsignedPadding; }
+  [[nodiscard]] bool hasSignOrPaddingBit() const {
+    return IsSigned || HasUnsignedPadding;
+  }
 
   /// Return the number of integral bits represented by these semantics. These
   /// are separate from the fractional bits and do not include the sign or
   /// padding bit.
-  unsigned getIntegralBits() const {
+  [[nodiscard]] unsigned getIntegralBits() const {
     return std::max(getMsbWeight() + 1 - hasSignOrPaddingBit(), 0);
   }
 
@@ -85,7 +90,7 @@ class FixedPointSemantics {
   /// precision semantic that can precisely represent the precision and ranges
   /// of both input values. This does not compute the resulting semantics for a
   /// given binary operation.
-  LLVM_ABI FixedPointSemantics
+  [[nodiscard]] LLVM_ABI FixedPointSemantics
   getCommonSemantics(const FixedPointSemantics &Other) const;
 
   /// Print semantics for debug purposes
@@ -98,7 +103,8 @@ class FixedPointSemantics {
   /// minimum integer representation of 127 and -128, respectively. If both of
   /// these values can be represented (possibly inexactly) in the floating
   /// point semantic without overflowing, this returns true.
-  LLVM_ABI bool fitsInFloatSemantics(const fltSemantics &FloatSema) const;
+  [[nodiscard]] LLVM_ABI bool
+  fitsInFloatSemantics(const fltSemantics &FloatSema) const;
 
   /// Return the FixedPointSemantics for an integer type.
   static FixedPointSemantics GetIntegerSemantics(unsigned Width,
@@ -119,7 +125,7 @@ class FixedPointSemantics {
   /// The result is dependent on the host endianness and not stable across LLVM
   /// versions. See getFromOpaqueInt() to convert it back to a
   /// FixedPointSemantics object.
-  LLVM_ABI uint32_t toOpaqueInt() const;
+  [[nodiscard]] LLVM_ABI uint32_t toOpaqueInt() const;
   /// Create a FixedPointSemantics object from an integer created via
   /// toOpaqueInt().
   LLVM_ABI static FixedPointSemantics getFromOpaqueInt(uint32_t);
@@ -177,16 +183,18 @@ class APFixedPoint {
   APFixedPoint(const FixedPointSemantics &Sema) : APFixedPoint(0, Sema) {}
 
   APSInt getValue() const { return APSInt(Val, !Sema.isSigned()); }
-  inline unsigned getWidth() const { return Sema.getWidth(); }
-  inline unsigned getScale() const { return Sema.getScale(); }
-  int getLsbWeight() const { return Sema.getLsbWeight(); }
-  int getMsbWeight() const { return Sema.getMsbWeight(); }
-  inline bool isSaturated() const { return Sema.isSaturated(); }
-  inline bool isSigned() const { return Sema.isSigned(); }
-  inline bool hasPadding() const { return Sema.hasUnsignedPadding(); }
-  FixedPointSemantics getSemantics() const { return Sema; }
+  [[nodiscard]] inline unsigned getWidth() const { return Sema.getWidth(); }
+  [[nodiscard]] inline unsigned getScale() const { return Sema.getScale(); }
+  [[nodiscard]] int getLsbWeight() const { return Sema.getLsbWeight(); }
+  [[nodiscard]] int getMsbWeight() const { return Sema.getMsbWeight(); }
+  [[nodiscard]] inline bool isSaturated() const { return Sema.isSaturated(); }
+  [[nodiscard]] inline bool isSigned() const { return Sema.isSigned(); }
+  [[nodiscard]] inline bool hasPadding() const {
+    return Sema.hasUnsignedPadding();
+  }
+  [[nodiscard]] FixedPointSemantics getSemantics() const { return Sema; }
 
-  bool getBoolValue() const { return Val.getBoolValue(); }
+  [[nodiscard]] bool getBoolValue() const { return Val.getBoolValue(); }
 
   // Convert this number to match the semantics provided. If the overflow
   // parameter is provided, set this value to true or false to indicate if this
@@ -244,10 +252,11 @@ class APFixedPoint {
 
   /// Convert this fixed point number to a floating point value with the
   /// provided semantics.
-  LLVM_ABI APFloat convertToFloat(const fltSemantics &FloatSema) const;
+  [[nodiscard]] LLVM_ABI APFloat
+  convertToFloat(const fltSemantics &FloatSema) const;
 
   LLVM_ABI void toString(SmallVectorImpl<char> &Str) const;
-  std::string toString() const {
+  [[nodiscard]] std::string toString() const {
     SmallString<40> S;
     toString(S);
     return std::string(S);
@@ -260,7 +269,7 @@ class APFixedPoint {
 #endif
 
   // If LHS > RHS, return 1. If LHS == RHS, return 0. If LHS < RHS, return -1.
-  LLVM_ABI int compare(const APFixedPoint &Other) const;
+  [[nodiscard]] LLVM_ABI int compare(const APFixedPoint &Other) const;
   bool operator==(const APFixedPoint &Other) const {
     return compare(Other) == 0;
   }
diff --git a/llvm/include/llvm/ADT/APFloat.h b/llvm/include/llvm/ADT/APFloat.h
index 6bc62845fddf4..79336fd48852f 100644
--- a/llvm/include/llvm/ADT/APFloat.h
+++ b/llvm/include/llvm/ADT/APFloat.h
@@ -420,7 +420,7 @@ class IEEEFloat final {
   /// @}
 
   /// Returns whether this instance allocated memory.
-  bool needsCleanup() const { return partCount() > 1; }
+  [[nodiscard]] bool needsCleanup() const { return partCount() > 1; }
 
   /// \name Convenience "constructors"
   /// @{
@@ -462,11 +462,11 @@ class IEEEFloat final {
   LLVM_ABI opStatus convertFromAPInt(const APInt &, bool, roundingMode);
   LLVM_ABI Expected<opStatus> convertFromString(StringRef, roundingMode);
   LLVM_ABI APInt bitcastToAPInt() const;
-  LLVM_ABI double convertToDouble() const;
+  [[nodiscard]] LLVM_ABI double convertToDouble() const;
 #ifdef HAS_IEE754_FLOAT128
-  LLVM_ABI float128 convertToQuad() const;
+  [[nodiscard]] LLVM_ABI float128 convertToQuad() const;
 #endif
-  LLVM_ABI float convertToFloat() const;
+  [[nodiscard]] LLVM_ABI float convertToFloat() const;
 
   /// @}
 
@@ -477,10 +477,10 @@ class IEEEFloat final {
 
   /// IEEE comparison with another floating point number (NaNs compare
   /// unordered, 0==-0).
-  LLVM_ABI cmpResult compare(const IEEEFloat &) const;
+  [[nodiscard]] LLVM_ABI cmpResult compare(const IEEEFloat &) const;
 
   /// Bitwise comparison for equality (QNaNs compare equal, 0!=-0).
-  LLVM_ABI bool bitwiseIsEqual(const IEEEFloat &) const;
+  [[nodiscard]] LLVM_ABI bool bitwiseIsEqual(const IEEEFloat &) const;
 
   /// Write out a hexadecimal representation of the floating point value to DST,
   /// which must be of sufficient size, in the C99 form [-]0xh.hhhhp[+-]d.
@@ -495,62 +495,66 @@ class IEEEFloat final {
   /// negative.
   ///
   /// This applies to zeros and NaNs as well.
-  bool isNegative() const { return sign; }
+  [[nodiscard]] bool isNegative() const { return sign; }
 
   /// IEEE-754R isNormal: Returns true if and only if the current value is normal.
   ///
   /// This implies that the current value of the float is not zero, subnormal,
   /// infinite, or NaN following the definition of normality from IEEE-754R.
-  bool isNormal() const { return !isDenormal() && isFiniteNonZero(); }
+  [[nodiscard]] bool isNormal() const {
+    return !isDenormal() && isFiniteNonZero();
+  }
 
   /// Returns true if and only if the current value is zero, subnormal, or
   /// normal.
   ///
   /// This means that the value is not infinite or NaN.
-  bool isFinite() const { return !isNaN() && !isInfinity(); }
+  [[nodiscard]] bool isFinite() const { return !isNaN() && !isInfinity(); }
 
   /// Returns true if and only if the float is plus or minus zero.
-  bool isZero() const { return category == fltCategory::fcZero; }
+  [[nodiscard]] bool isZero() const { return category == fltCategory::fcZero; }
 
   /// IEEE-754R isSubnormal(): Returns true if and only if the float is a
   /// denormal.
-  LLVM_ABI bool isDenormal() const;
+  [[nodiscard]] LLVM_ABI bool isDenormal() const;
 
   /// IEEE-754R isInfinite(): Returns true if and only if the float is infinity.
-  bool isInfinity() const { return category == fcInfinity; }
+  [[nodiscard]] bool isInfinity() const { return category == fcInfinity; }
 
   /// Returns true if and only if the float is a quiet or signaling NaN.
-  bool isNaN() const { return category == fcNaN; }
+  [[nodiscard]] bool isNaN() const { return category == fcNaN; }
 
   /// Returns true if and only if the float is a signaling NaN.
-  LLVM_ABI bool isSignaling() const;
+  [[nodiscard]] LLVM_ABI bool isSignaling() const;
 
   /// @}
 
   /// \name Simple Queries
   /// @{
 
-  fltCategory getCategory() const { return category; }
-  const fltSemantics &getSemantics() const { return *semantics; }
-  bool isNonZero() const { return category != fltCategory::fcZero; }
-  bool isFiniteNonZero() const { return isFinite() && !isZero(); }
-  bool isPosZero() const { return isZero() && !isNegative(); }
-  bool isNegZero() const { return isZero() && isNegative(); }
+  [[nodiscard]] fltCategory getCategory() const { return category; }
+  [[nodiscard]] const fltSemantics &getSemantics() const { return *semantics; }
+  [[nodiscard]] bool isNonZero() const {
+    return category != fltCategory::fcZero;
+  }
+  [[nodiscard]] bool isFiniteNonZero() const { return isFinite() && !isZero(); }
+  [[nodiscard]] bool isPosZero() const { return isZero() && !isNegative(); }
+  [[nodiscard]] bool isNegZero() const { return isZero() && isNegative(); }
 
   /// Returns true if and only if the number has the smallest possible non-zero
   /// magnitude in the current semantics.
-  LLVM_ABI bool isSmallest() const;
+  [[nodiscard]] LLVM_ABI bool isSmallest() const;
 
   /// Returns true if this is the smallest (by magnitude) normalized finite
   /// number in the given semantics.
-  LLVM_ABI bool isSmallestNormalized() const;
+  [[nodiscard]] LLVM_ABI bool isSmallestNormalized() const;
 
   /// Returns true if and only if the number has the largest possible finite
   /// magnitude in the current semantics.
-  LLVM_ABI bool isLargest() const;
+  [[nodiscard]] LLVM_ABI bool isLargest() const;
 
   /// Returns true if and only if the number is an exact integer.
-  LLVM_ABI bool isInteger() const;
+  [[nodiscard]] LLVM_ABI bool isInteger() const;
 
   /// @}
 
@@ -599,7 +603,7 @@ class IEEEFloat final {
                          unsigned FormatMaxPadding = 3,
                          bool TruncateZero = true) const;
 
-  LLVM_ABI LLVM_READONLY int getExactLog2Abs() const;
+  [[nodiscard]] LLVM_ABI LLVM_READONLY int getExactLog2Abs() const;
 
   LLVM_ABI friend int ilogb(const IEEEFloat &Arg);
 
@@ -626,15 +630,16 @@ class IEEEFloat final {
 
   /// @}
 
-  LLVM_ABI cmpResult compareAbsoluteValue(const IEEEFloat &) const;
+  [[nodiscard]] LLVM_ABI cmpResult
+  compareAbsoluteValue(const IEEEFloat &) const;
 
 private:
   /// \name Simple Queries
   /// @{
 
   integerPart *significandParts();
-  const integerPart *significandParts() const;
-  LLVM_ABI unsigned int partCount() const;
+  [[nodiscard]] const integerPart *significandParts() const;
+  [[nodiscard]] LLVM_ABI unsigned int partCount() const;
 
   /// @}
 
@@ -654,16 +659,16 @@ class IEEEFloat final {
   void initialize(const fltSemantics *);
   void shiftSignificandLeft(unsigned int);
   lostFraction shiftSignificandRight(unsigned int);
-  unsigned int significandLSB() const;
-  unsigned int significandMSB() const;
+  [[nodiscard]] unsigned int significandLSB() const;
+  [[nodiscard]] unsigned int significandMSB() const;
   void zeroSignificand();
-  unsigned int getNumHighBits() const;
+  [[nodiscard]] unsigned int getNumHighBits() const;
   /// Return true if the significand excluding the integral bit is all ones.
-  bool isSignificandAllOnes() const;
-  bool isSignificandAllOnesExceptLSB() const;
+  [[nodiscard]] bool isSignificandAllOnes() const;
+  [[nodiscard]] bool isSignificandAllOnesExceptLSB() const;
   /// Return true if the significand excluding the integral bit is all zeros.
-  bool isSignificandAllZeros() const;
-  bool isSignificandAllZerosExceptMSB() const;
+  [[nodiscard]] bool isSignificandAllZeros() const;
+  [[nodiscard]] bool isSignificandAllZerosExceptMSB() const;
 
   /// @}
 
@@ -685,7 +690,8 @@ class IEEEFloat final {
   opStatus normalize(roundingMode, lostFraction);
   opStatus addOrSubtract(const IEEEFloat &, roundingMode, bool subtract);
   opStatus handleOverflow(roundingMode);
-  bool roundAwayFromZero(roundingMode, lostFraction, unsigned int) const;
+  [[nodiscard]] bool roundAwayFromZero(roundingMode, lostFraction,
+                                       unsigned int) const;
   opStatus convertToSignExtendedInteger(MutableArrayRef<integerPart>,
                                         unsigned int, bool, roundingMode,
                                         bool *) const;
@@ -697,9 +703,9 @@ class IEEEFloat final {
                                  roundingMode) const;
   opStatus roundSignificandWithExponent(const integerPart *, unsigned int, int,
                                         roundingMode);
-  ExponentType exponentNaN() const;
-  ExponentType exponentInf() const;
-  ExponentType exponentZero() const;
+  [[nodiscard]] ExponentType exponentNaN() const;
+  [[nodiscard]] ExponentType exponentInf() const;
+  [[nodiscard]] ExponentType exponentZero() const;
 
   /// @}
 
@@ -823,12 +829,12 @@ class DoubleAPFloat final {
   LLVM_ABI DoubleAPFloat &operator=(const DoubleAPFloat &RHS);
   inline DoubleAPFloat &operator=(DoubleAPFloat &&RHS);
 
-  bool needsCleanup() const { return Floats != nullptr; }
+  [[nodiscard]] bool needsCleanup() const { return Floats != nullptr; }
 
   inline APFloat &getFirst();
-  inline const APFloat &getFirst() const;
+  [[nodiscard]] inline const APFloat &getFirst() const;
   inline APFloat &getSecond();
-  inline const APFloat &getSecond() const;
+  [[nodiscard]] inline const APFloat &getSecond() const;
 
   LLVM_ABI opStatus add(const DoubleAPFloat &RHS, roundingMode RM);
   LLVM_ABI opStatus subtract(const DoubleAPFloat &RHS, roundingMode RM);
@@ -841,10 +847,11 @@ class DoubleAPFloat final {
                                      roundingMode RM);
   LLVM_ABI opStatus roundToIntegral(roundingMode RM);
   LLVM_ABI void changeSign();
-  LLVM_ABI cmpResult compareAbsoluteValue(const DoubleAPFloat &RHS) const;
+  [[nodiscard]] LLVM_ABI cmpResult
+  compareAbsoluteValue(const DoubleAPFloat &RHS) const;
 
-  LLVM_ABI fltCategory getCategory() const;
-  LLVM_ABI bool isNegative() const;
+  [[nodiscard]] LLVM_ABI fltCategory getCategory() const;
+  [[nodiscard]] LLVM_ABI bool isNegative() const;
 
   LLVM_ABI void makeInf(bool Neg);
   LLVM_ABI void makeZero(bool Neg);
@@ -853,8 +860,8 @@ class DoubleAPFloat final {
   LLVM_ABI void makeSmallestNormalized(bool Neg);
   LLVM_ABI void makeNaN(bool SNaN, bool Neg, const APInt *fill);
 
-  LLVM_ABI cmpResult compare(const DoubleAPFloat &RHS) const;
-  LLVM_ABI bool bitwiseIsEqual(const DoubleAPFloat &RHS) const;
+  [[nodiscard]] LLVM_ABI cmpResult compare(const DoubleAPFloat &RHS) const;
+  [[nodiscard]] LLVM_ABI bool bitwiseIsEqual(const DoubleAPFloat &RHS) const;
   LLVM_ABI APInt bitcastToAPInt() const;
   LLVM_ABI Expected<opStatus> convertFromString(StringRef, roundingMode);
   LLVM_ABI opStatus next(bool nextDown);
@@ -868,17 +875,17 @@ class DoubleAPFloat final {
                                            bool UpperCase,
                                            roundingMode RM) const;
 
-  LLVM_ABI bool isDenormal() const;
-  LLVM_ABI bool isSmallest() const;
-  LLVM_ABI bool isSmallestNormalized() const;
-  LLVM_ABI bool isLargest() const;
-  LLVM_ABI bool isInteger() const;
+  [[nodiscard]] LLVM_ABI bool isDenormal() const;
+  [[nodiscard]] LLVM_ABI bool isSmallest() const;
+  [[nodiscard]] LLVM_ABI bool isSmallestNormalized() const;
+  [[nodiscard]] LLVM_ABI bool isLargest() const;
+  [[nodiscard]] LLVM_ABI bool isInteger() const;
 
   LLVM_ABI void toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision,
                          unsigned FormatMaxPadding,
                          bool TruncateZero = true) const;
 
-  LLVM_ABI LLVM_READONLY int getExactLog2Abs() const;
+  [[nodiscard]] LLVM_ABI LLVM_READONLY int getExactLog2Abs() const;
 
   LLVM_ABI friend int ilogb(const DoubleAPFloat &X);
   LLVM_ABI friend DoubleAPFloat scalbn(const DoubleAPFloat &X, int Exp,
@@ -1009,7 +1016,7 @@ class APFloat : public APFloatBase {
     llvm_unreachable("Unexpected semantics");
   }
 
-  const IEEEFloat &getIEEE() const {
+  [[nodiscard]] const IEEEFloat &getIEEE() const {
     if (usesLayout<IEEEFloat>(*U.semantics))
       return U.IEEE;
     if (usesLayout<DoubleAPFloat>(*U.semantics))
@@ -1043,7 +1050,7 @@ class APFloat : public APFloatBase {
 
   // Compares the absolute value of this APFloat with another.  Both operands
   // must be finite non-zero.
-  cmpResult compareAbsoluteValue(const APFloat &RHS) const {
+  [[nodiscard]] cmpResult compareAbsoluteValue(const APFloat &RHS) const {
     assert(&getSemantics() == &RHS.getSemantics() &&
            "Should only compare APFloats with the same semantics");
     if (usesLayout<IEEEFloat>(getSemantics()))
@@ -1071,7 +1078,9 @@ class APFloat : public APFloatBase {
 
   ~APFloat() = default;
 
-  bool needsCleanup() const { APFLOAT_DISPATCH_ON_SEMANTICS(needsCleanup()); }
+  [[nodiscard]] bool needsCleanup() const {
+    APFLOAT_DISPATCH_ON_SEMANTICS(needsCleanup());
+  }
 
   /// Factory for Positive and Negative Zero.
   ///
@@ -1313,7 +1322,7 @@ class APFloat : public APFloatBase {
 
   /// Assuming this is an IEEE-754 NaN value, quiet its signaling bit.
   /// This preserves the sign and payload bits.
-  APFloat makeQuiet() const {
+  [[nodiscard]] APFloat makeQuiet() const {
     APFloat Result(*this);
     Result.getIEEE().makeQuiet();
     return Result;
@@ -1359,7 +1368,7 @@ class APFloat : public APFloatBase {
   /// \pre The APFloat must be built using semantics, that can be represented by
   /// the host double type without loss of precision. It can be IEEEdouble and
   /// shorter semantics, like IEEEsingle and others.
-  LLVM_ABI double convertToDouble() const;
+  [[nodiscard]] LLVM_ABI double convertToDouble() const;
 
   /// Converts this APFloat to host float value.
   ///
@@ -1367,7 +1376,7 @@ class APFloat : public APFloatBase {
   /// the host float type without loss of precision. It can be IEEEquad and
   /// shorter semantics, like IEEEdouble and others.
 #ifdef HAS_IEE754_FLOAT128
-  LLVM_ABI float128 convertToQuad() const;
+  [[nodiscard]] LLVM_ABI float128 convertToQuad() const;
 #endif
 
   /// Converts this APFloat to host float value.
@@ -1375,7 +1384,7 @@ class APFloat : public APFloatBase {
   /// \pre The APFloat must be built using semantics, that can be represented by
   /// the host float type without loss of precision. It can be IEEEsingle and
   /// shorter semantics, like IEEEhalf.
-  LLVM_ABI float convertToFloat() const;
+  [[nodiscard]] LLVM_ABI float convertToFloat() const;
 
   bool operator==(const APFloat &RHS) const { return compare(RHS) == cmpEqual; }
 
@@ -1401,7 +1410,7 @@ class APFloat : public APFloatBase {
 
   // IEEE comparison with another floating point number (NaNs compare unordered,
   // 0==-0).
-  cmpResult compar...
[truncated]

``````````

</details>


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


More information about the llvm-commits mailing list