浏览代码

Don't cast function type in HLSLExternalSource::CanConvert. (#190)

Xiang Li 8 年之前
父节点
当前提交
0677af11e4

+ 3 - 1
tools/clang/lib/Sema/SemaHLSL.cpp

@@ -6962,7 +6962,9 @@ bool HLSLExternalSource::CanConvert(
   TYPE_CONVERSION_FLAGS Flags = explicitConversion ? TYPE_CONVERSION_EXPLICIT : TYPE_CONVERSION_DEFAULT;
   TYPE_CONVERSION_REMARKS Remarks = TYPE_CONVERSION_NONE;
   QualType source = sourceExpr->getType();
-  
+  // Cannot cast function type.
+  if (source->isFunctionType())
+    return false;
   // Convert to an r-value to begin with.
   bool needsLValueToRValue = sourceExpr->isLValue() &&
     !target->isLValueReferenceType() && 

+ 11 - 0
tools/clang/test/CodeGenHLSL/func_cast.hlsl

@@ -0,0 +1,11 @@
+// RUN: %dxc -E main -T ps_6_0 %s  | FileCheck %s
+// CHECK: cannot initialize a variable of type 'int' with an lvalue of type 'int ()'
+// CHECK:  non-object type 'int ()' is not assignable
+int f() {
+    return 1;
+}
+void main() {
+    int x = f;
+    int y = 0;
+    f = y;
+}

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

@@ -358,6 +358,7 @@ public:
   TEST_METHOD(CodeGenFirstbitLo)
   TEST_METHOD(CodeGenFloatMaxtessfactor)
   TEST_METHOD(CodeGenFModPS)
+  TEST_METHOD(CodeGenFuncCast)
   TEST_METHOD(CodeGenFunctionalCast)
   TEST_METHOD(CodeGenGather)
   TEST_METHOD(CodeGenGatherCmp)
@@ -2223,6 +2224,10 @@ TEST_F(CompilerTest, CodeGenFModPS) {
   CodeGenTestCheck(L"..\\CodeGenHLSL\\fmodPS.hlsl");
 }
 
+TEST_F(CompilerTest, CodeGenFuncCast) {
+  CodeGenTestCheck(L"..\\CodeGenHLSL\\func_cast.hlsl");
+}
+
 TEST_F(CompilerTest, CodeGenFunctionalCast) {
   CodeGenTestCheck(L"..\\CodeGenHLSL\\functionalCast.hlsl");
 }