From ecfa00c8e3584959e9a78f3898dd4298df2845ce Mon Sep 17 00:00:00 2001 From: Tristan Labelle Date: Tue, 2 Apr 2019 09:31:51 -0700 Subject: [PATCH] Make the array out of bounds error conditional on the HLSL version. --- .../clang/include/clang/Basic/DiagnosticSemaKinds.td | 2 +- tools/clang/lib/Sema/SemaChecking.cpp | 4 ++-- .../test/HLSL/array-index-out-of-bounds-HV-2016.hlsl | 12 ++++++++++++ tools/clang/test/HLSL/array-index-out-of-bounds.hlsl | 4 ++-- tools/clang/unittests/HLSL/VerifierTest.cpp | 1 + 5 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 tools/clang/test/HLSL/array-index-out-of-bounds-HV-2016.hlsl diff --git a/tools/clang/include/clang/Basic/DiagnosticSemaKinds.td b/tools/clang/include/clang/Basic/DiagnosticSemaKinds.td index c91fb9fd7..cfa4818f7 100644 --- a/tools/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/tools/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -7441,7 +7441,7 @@ def err_opencl_constant_no_init : Error< // HLSL Change Starts let CategoryName = "HLSL Issue" in { def err_hlsl_array_element_index_out_of_bounds: Error< - "array element index '%0' is out of bounds">; + "array index %0 is out of bounds">; def err_hlsl_attribute_expects_string_literal: Error< "attribute %0 must have a string literal argument">; def err_hlsl_attribute_expects_string_literal_from_list: Error< diff --git a/tools/clang/lib/Sema/SemaChecking.cpp b/tools/clang/lib/Sema/SemaChecking.cpp index 95d58dcf2..36e14f694 100644 --- a/tools/clang/lib/Sema/SemaChecking.cpp +++ b/tools/clang/lib/Sema/SemaChecking.cpp @@ -8469,7 +8469,7 @@ void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr, } // HLSL Change Starts - if (getLangOpts().HLSL) { + if (getLangOpts().HLSL && getLangOpts().HLSLVersion > 2016) { DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr, PDiag(diag::err_hlsl_array_element_index_out_of_bounds) << index.toString(10, true)); } @@ -8488,7 +8488,7 @@ void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr, } // HLSL Change } else { // HLSL Change Starts - if (getLangOpts().HLSL) { + if (getLangOpts().HLSL && getLangOpts().HLSLVersion > 2016) { DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr, PDiag(diag::err_hlsl_array_element_index_out_of_bounds) << index.toString(10, true)); } diff --git a/tools/clang/test/HLSL/array-index-out-of-bounds-HV-2016.hlsl b/tools/clang/test/HLSL/array-index-out-of-bounds-HV-2016.hlsl new file mode 100644 index 000000000..cc5ee986e --- /dev/null +++ b/tools/clang/test/HLSL/array-index-out-of-bounds-HV-2016.hlsl @@ -0,0 +1,12 @@ +// RUN: %clang_cc1 -Wno-unused-value -fsyntax-only -ffreestanding -HV 2016 -verify -verify-ignore-unexpected=note %s + +void dead() +{ + int array[2]; + array[-1] = 0; /* expected-warning {{array index -1 is before the beginning of the array}} fxc-pass */ + array[0] = 0; + array[1] = 0; + array[2] = 0; /* expected-warning {{array index 2 is past the end of the array (which contains 2 elements)}} fxc-pass */ +} + +void main() {} \ No newline at end of file diff --git a/tools/clang/test/HLSL/array-index-out-of-bounds.hlsl b/tools/clang/test/HLSL/array-index-out-of-bounds.hlsl index fe31e4820..2e89c481b 100644 --- a/tools/clang/test/HLSL/array-index-out-of-bounds.hlsl +++ b/tools/clang/test/HLSL/array-index-out-of-bounds.hlsl @@ -3,8 +3,8 @@ void main() { int array[2]; - array[-1] = 0; /* expected-error {{array element index '-1' is out of bounds}} fxc-error {{X3504: array index out of bounds}} */ + array[-1] = 0; /* expected-error {{array index -1 is out of bounds}} fxc-error {{X3504: array index out of bounds}} */ array[0] = 0; array[1] = 0; - array[2] = 0; /* expected-error {{array element index '2' is out of bounds}} fxc-error {{X3504: array index out of bounds}} */ + array[2] = 0; /* expected-error {{array index 2 is out of bounds}} fxc-error {{X3504: array index out of bounds}} */ } \ No newline at end of file diff --git a/tools/clang/unittests/HLSL/VerifierTest.cpp b/tools/clang/unittests/HLSL/VerifierTest.cpp index 21242ae84..d6ed9e68c 100644 --- a/tools/clang/unittests/HLSL/VerifierTest.cpp +++ b/tools/clang/unittests/HLSL/VerifierTest.cpp @@ -139,6 +139,7 @@ public: TEST_F(VerifierTest, RunArrayIndexOutOfBounds) { CheckVerifiesHLSL(L"array-index-out-of-bounds.hlsl"); + CheckVerifiesHLSL(L"array-index-out-of-bounds-HV-2016.hlsl"); } TEST_F(VerifierTest, RunArrayLength) {