[clang] 8a86261 - [FPEnv] Use typed accessors in FPOptions

Serge Pavlov via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 18 00:17:47 PDT 2020


Author: Serge Pavlov
Date: 2020-09-18T14:16:43+07:00
New Revision: 8a86261c511e09629aa48c13cb84172b9be26c0c

URL: https://github.com/llvm/llvm-project/commit/8a86261c511e09629aa48c13cb84172b9be26c0c
DIFF: https://github.com/llvm/llvm-project/commit/8a86261c511e09629aa48c13cb84172b9be26c0c.diff

LOG: [FPEnv] Use typed accessors in FPOptions

Previously methods `FPOptions::get*` returned unsigned value even if the
corresponding property was represented by specific enumeration type. With
this change such methods return actual type of the property. It also
allows printing value of a property as text rather than integer code.

Differential Revision: https://reviews.llvm.org/D87812

Added: 
    

Modified: 
    clang/include/clang/Basic/FPOptions.def
    clang/include/clang/Basic/LangOptions.h
    clang/test/AST/ast-dump-fpfeatures.cpp
    llvm/include/llvm/ADT/FloatingPointMode.h

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/FPOptions.def b/clang/include/clang/Basic/FPOptions.def
index 1733689775d4..a93fa475cd5f 100644
--- a/clang/include/clang/Basic/FPOptions.def
+++ b/clang/include/clang/Basic/FPOptions.def
@@ -14,7 +14,7 @@
 
 // OPTION(name, type, width, previousName)
 OPTION(FPContractMode, LangOptions::FPModeKind, 2, First)
-OPTION(RoundingMode, RoundingMode, 3, FPContractMode)
+OPTION(RoundingMode, LangOptions::RoundingMode, 3, FPContractMode)
 OPTION(FPExceptionMode, LangOptions::FPExceptionModeKind, 2, RoundingMode)
 OPTION(AllowFEnvAccess, bool, 1, FPExceptionMode)
 OPTION(AllowFPReassociate, bool, 1, AllowFEnvAccess)

diff  --git a/clang/include/clang/Basic/LangOptions.h b/clang/include/clang/Basic/LangOptions.h
index 3614496ded96..84d25c359c55 100644
--- a/clang/include/clang/Basic/LangOptions.h
+++ b/clang/include/clang/Basic/LangOptions.h
@@ -432,8 +432,7 @@ class FPOptions {
   }
 
   bool isFPConstrained() const {
-    return getRoundingMode() !=
-               static_cast<unsigned>(RoundingMode::NearestTiesToEven) ||
+    return getRoundingMode() != llvm::RoundingMode::NearestTiesToEven ||
            getFPExceptionMode() != LangOptions::FPE_Ignore ||
            getAllowFEnvAccess();
   }
@@ -453,8 +452,8 @@ class FPOptions {
 
   // We can define most of the accessors automatically:
 #define OPTION(NAME, TYPE, WIDTH, PREVIOUS)                                    \
-  unsigned get##NAME() const {                                                 \
-    return static_cast<unsigned>(TYPE((Value & NAME##Mask) >> NAME##Shift));   \
+  TYPE get##NAME() const {                                                     \
+    return static_cast<TYPE>((Value & NAME##Mask) >> NAME##Shift);             \
   }                                                                            \
   void set##NAME(TYPE value) {                                                 \
     Value = (Value & ~NAME##Mask) | (storage_type(value) << NAME##Shift);      \
@@ -561,7 +560,7 @@ class FPOptionsOverride {
   bool has##NAME##Override() const {                                           \
     return OverrideMask & FPOptions::NAME##Mask;                               \
   }                                                                            \
-  unsigned get##NAME##Override() const {                                       \
+  TYPE get##NAME##Override() const {                                           \
     assert(has##NAME##Override());                                             \
     return Options.get##NAME();                                                \
   }                                                                            \

diff  --git a/clang/test/AST/ast-dump-fpfeatures.cpp b/clang/test/AST/ast-dump-fpfeatures.cpp
index 01af3a8fd7e9..70b14ec3b3c0 100644
--- a/clang/test/AST/ast-dump-fpfeatures.cpp
+++ b/clang/test/AST/ast-dump-fpfeatures.cpp
@@ -87,7 +87,7 @@ float func_10(float x, float y) {
 }
 
 // CHECK-LABEL: FunctionDecl {{.*}} func_10 'float (float, float)'
-// CHECK:         BinaryOperator {{.*}} 'float' '+' RoundingMode=3
+// CHECK:         BinaryOperator {{.*}} 'float' '+' RoundingMode=downward
 
 float func_11(float x, float y) {
   if (x < 0) {
@@ -98,8 +98,8 @@ float func_11(float x, float y) {
 }
 
 // CHECK-LABEL: FunctionDecl {{.*}} func_11 'float (float, float)'
-// CHECK:         BinaryOperator {{.*}} 'float' '+' RoundingMode=2
-// CHECK:         BinaryOperator {{.*}} 'float' '-' RoundingMode=3
+// CHECK:         BinaryOperator {{.*}} 'float' '+' RoundingMode=upward
+// CHECK:         BinaryOperator {{.*}} 'float' '-' RoundingMode=downward
 
 
 #pragma STDC FENV_ROUND FE_DYNAMIC
@@ -109,7 +109,7 @@ float func_12(float x, float y) {
 }
 
 // CHECK-LABEL: FunctionDecl {{.*}} func_12 'float (float, float)'
-// CHECK:         BinaryOperator {{.*}} 'float' '+' RoundingMode=1
+// CHECK:         BinaryOperator {{.*}} 'float' '+' RoundingMode=tonearest
 
 #pragma STDC FENV_ROUND FE_TONEAREST
 
@@ -118,7 +118,7 @@ float func_13(float x, float y) {
 }
 
 // CHECK-LABEL: FunctionDecl {{.*}} func_13 'float (float, float)'
-// CHECK:         BinaryOperator {{.*}} 'float' '+' RoundingMode=1
+// CHECK:         BinaryOperator {{.*}} 'float' '+' RoundingMode=tonearest
 
 
 template <typename T>
@@ -136,8 +136,8 @@ float func_15(float x, float y) {
 // CHECK:         FunctionDecl {{.*}} func_14 'T (T, T)'
 // CHECK:           CompoundStmt
 // CHECK-NEXT:        ReturnStmt
-// CHECK-NEXT:          BinaryOperator {{.*}} '+' RoundingMode=0
+// CHECK-NEXT:          BinaryOperator {{.*}} '+' RoundingMode=towardzero
 // CHECK:         FunctionDecl {{.*}} func_14 'float (float, float)'
 // CHECK:           CompoundStmt
 // CHECK-NEXT:        ReturnStmt
-// CHECK-NEXT:          BinaryOperator {{.*}} 'float' '+' RoundingMode=0
+// CHECK-NEXT:          BinaryOperator {{.*}} 'float' '+' RoundingMode=towardzero

diff  --git a/llvm/include/llvm/ADT/FloatingPointMode.h b/llvm/include/llvm/ADT/FloatingPointMode.h
index 3ba8ae1b2855..698830937870 100644
--- a/llvm/include/llvm/ADT/FloatingPointMode.h
+++ b/llvm/include/llvm/ADT/FloatingPointMode.h
@@ -44,6 +44,24 @@ enum class RoundingMode : int8_t {
   Invalid = -1    ///< Denotes invalid value.
 };
 
+/// Returns text representation of the given rounding mode.
+inline StringRef spell(RoundingMode RM) {
+  switch (RM) {
+  case RoundingMode::TowardZero: return "towardzero";
+  case RoundingMode::NearestTiesToEven: return "tonearest";
+  case RoundingMode::TowardPositive: return "upward";
+  case RoundingMode::TowardNegative: return "downward";
+  case RoundingMode::NearestTiesToAway: return "tonearestaway";
+  case RoundingMode::Dynamic: return "dynamic";
+  default: return "invalid";
+  }
+}
+
+inline raw_ostream &operator << (raw_ostream &OS, RoundingMode RM) {
+  OS << spell(RM);
+  return OS;
+}
+
 /// Represent subnormal handling kind for floating point instruction inputs and
 /// outputs.
 struct DenormalMode {


        


More information about the cfe-commits mailing list