Explorar el Código

Generate warning when overflow happen during const expr cast (#2942)

Vishal Sharma hace 5 años
padre
commit
f44834ce73

+ 6 - 0
tools/clang/include/clang/Basic/DiagnosticASTKinds.td

@@ -263,4 +263,10 @@ def err_odr_non_type_parameter_type_inconsistent : Error<
   "non-type template parameter declared with incompatible types in different "
   "non-type template parameter declared with incompatible types in different "
   "translation units (%0 vs. %1)">;
   "translation units (%0 vs. %1)">;
 def err_unsupported_ast_node: Error<"cannot import unsupported AST node %0">;
 def err_unsupported_ast_node: Error<"cannot import unsupported AST node %0">;
+
+// HLSL changes begin
+def warn_hlsl_constexpr_overflow : Warning<
+  "overflow in the expression when converting to %0 type">,
+  InGroup<DiagGroup<"const-expr-overflow">>;
+// HLSL changes end
 }
 }

+ 5 - 0
tools/clang/lib/AST/ExprConstant.cpp

@@ -1546,6 +1546,11 @@ static void HandleOverflow(EvalInfo &Info, const Expr *E,
                            const T &SrcValue, QualType DestType) {
                            const T &SrcValue, QualType DestType) {
   Info.CCEDiag(E, diag::note_constexpr_overflow)
   Info.CCEDiag(E, diag::note_constexpr_overflow)
     << SrcValue << DestType;
     << SrcValue << DestType;
+  // HLSL changes begin
+  if (Info.getLangOpts().HLSL)
+    Info.Ctx.getDiagnostics().Report(E->getExprLoc(),
+      diag::warn_hlsl_constexpr_overflow) << DestType;
+  // HLSL changes end
 }
 }
 
 
 static bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E,
 static bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E,

+ 24 - 0
tools/clang/test/HLSLFileCheck/hlsl/diagnostics/warnings/constexpr_cast_overflow_nowarning.hlsl

@@ -0,0 +1,24 @@
+// RUN: %dxc -E real_lit_to_flt_nowarning -T vs_6_0 %s | FileCheck %s
+// RUN: %dxc -E real_lit_to_half_nowarning -T vs_6_0 %s | FileCheck %s
+// RUN: %dxc -E int_lit_to_half_nowarning -T vs_6_0 %s | FileCheck %s
+// RUN: %dxc -E real_lit_to_int_nowarning -T vs_6_0 %s | FileCheck %s
+// CHECK-NOT: warning: overflow in expression when converting to
+
+// Verify that when a constant is cast to a different type falls with in the
+// the valid range of destination type, then no overflow warning is reported.
+
+float real_lit_to_flt_nowarning() {
+  return 3.4e10;
+}
+
+min16float real_lit_to_half_nowarning() {
+  return 65500.0;
+}
+
+min16float int_lit_to_half_nowarning() {
+  return 65500;
+}
+
+int real_lit_to_int_nowarning() {  
+  return 3.4;
+}

+ 27 - 0
tools/clang/test/HLSLFileCheck/hlsl/diagnostics/warnings/constexpr_cast_overflow_warning.hlsl

@@ -0,0 +1,27 @@
+// RUN: %dxc -E real_lit_to_flt_warning -T vs_6_0 %s | FileCheck -check-prefix=CHK-FLT %s
+// RUN: %dxc -E real_lit_to_half_warning -T vs_6_0 %s | FileCheck -check-prefix=CHK-MINFLT1 %s
+// RUN: %dxc -E int_lit_to_half_warning -T vs_6_0 %s | FileCheck -check-prefix=CHK-MINFLT2 %s
+// RUN: %dxc -E real_lit_to_int_warning -T vs_6_0 %s | FileCheck -check-prefix=CHK-INT %s
+
+// Verify that when a constant is cast to a different type leading to overflow
+// a warning is generated notifying the same.
+
+// CHK-FLT: warning: overflow in the expression when converting to 'float' type
+float real_lit_to_flt_warning() {  
+  return 3.4e50;
+}
+
+// CHK-MINFLT1: warning: overflow in the expression when converting to 'min16float' type
+min16float real_lit_to_half_warning() {
+  return 65520.0;
+}
+
+// CHK-MINFLT2: warning: overflow in the expression when converting to 'min16float' type
+min16float int_lit_to_half_warning() {
+  return 65520;
+}
+
+// CHK-INT: warning: overflow in the expression when converting to 'int' type
+int real_lit_to_int_warning() {  
+  return 3.4e20;
+}