Ver código fonte

CheckForModifiableLvalue for CompoundAssignOperator. (#58)

Xiang Li 8 anos atrás
pai
commit
26550b7bf4

+ 15 - 0
tools/clang/lib/Sema/SemaHLSL.cpp

@@ -7292,6 +7292,21 @@ void HLSLExternalSource::CheckBinOpForHLSL(
   // Handle Assign and Comma operators and return
   switch (Opc)
   {
+  case BO_AddAssign:
+  case BO_AndAssign:
+  case BO_DivAssign:
+  case BO_MulAssign:
+  case BO_RemAssign:
+  case BO_ShlAssign:
+  case BO_ShrAssign:
+  case BO_SubAssign:
+  case BO_XorAssign: {
+    extern bool CheckForModifiableLvalue(Expr * E, SourceLocation Loc,
+                                         Sema & S);
+    if (CheckForModifiableLvalue(LHS.get(), OpLoc, *m_sema)) {
+      return;
+    }
+  } break;
   case BO_Assign: {
       extern bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S);
       if (CheckForModifiableLvalue(LHS.get(), OpLoc, *m_sema)) {

+ 9 - 0
tools/clang/test/HLSL/const-assign.hlsl

@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -fsyntax-only -Wno-unused-value  -ffreestanding -verify %s
+
+float4 main() : SV_TARGET
+{
+    const float c = 2.0; // expected-note {{variable 'c' declared const here}} expected-note {{variable 'c' declared const here}}
+    c = c + 3.0; // expected-error {{cannot assign to variable 'c' with const-qualified type 'const float'}}
+    c += 3.0; // expected-error {{cannot assign to variable 'c' with const-qualified type 'const float'}}
+    return (float4)c;
+}

+ 5 - 0
tools/clang/unittests/HLSL/VerifierTest.cpp

@@ -32,6 +32,7 @@ public:
 
   TEST_METHOD(RunAttributes);
   TEST_METHOD(RunConstExpr);
+  TEST_METHOD(RunConstAssign);
   TEST_METHOD(RunCppErrors);
   TEST_METHOD(RunFunctions);
   TEST_METHOD(RunIndexingOperator);
@@ -122,6 +123,10 @@ TEST_F(VerifierTest, RunConstExpr) {
   CheckVerifiesHLSL(L"const-expr.hlsl");
 }
 
+TEST_F(VerifierTest, RunConstAssign) {
+  CheckVerifiesHLSL(L"const-assign.hlsl");
+}
+
 TEST_F(VerifierTest, RunCppErrors) {
   CheckVerifiesHLSL(L"cpp-errors.hlsl");
 }