Browse Source

Disable function call inside function parameter declaration (#144)

This change is to syntactically check function declarations inside a function parameter.
Note that in vanilla clang they allow this to pass in a function to a function (c99, 6.9.1). So this check should be changed once we support function pointers in HLSL.
Young Kim 8 years ago
parent
commit
2f3a28076c

+ 2 - 0
tools/clang/include/clang/Basic/DiagnosticSemaKinds.td

@@ -7639,6 +7639,8 @@ def warn_hlsl_unused_call : Warning<
   "ignoring return value of function that only reads data">,
   InGroup<UnusedValue>;
 }
+def err_hlsl_func_in_func_decl : Error<
+   "function declaration is not allowed in function parameters">;
 // HLSL Change Ends
 
 let CategoryName = "OpenMP Issue" in {

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

@@ -9975,6 +9975,14 @@ bool Sema::DiagnoseHLSLDecl(Declarator &D, DeclContext *DC,
 
   bool hasSignSpec = D.getDeclSpec().getTypeSpecSign() != DeclSpec::TSS::TSS_unspecified;
 
+  // Function declarations are not allowed in parameter declaration
+  // TODO : Remove this check once we support function declarations/pointers in HLSL
+  if (isParameter && isFunction) {
+      Diag(D.getLocStart(), diag::err_hlsl_func_in_func_decl);
+      D.setInvalidType();
+      return false;
+  }
+
   assert(
     (1 == (isLocalVar ? 1 : 0) + (isGlobal ? 1 : 0) + (isField ? 1 : 0) +
     (isTypedef ? 1 : 0) + (isFunction ? 1 : 0) + (isMethod ? 1 : 0) +

+ 3 - 0
tools/clang/test/HLSL/functions.hlsl

@@ -10,6 +10,9 @@
 void fn_params_complete(int a = 0, int b = 0);
 void fn_params_last(int a, int b = 0);
 void fn_params_wrong(int a = 0, int b); // expected-error {{missing default argument on parameter 'b'}} fxc-error {{X3044: 'fn_params_wrong': missing default value for parameter 'b'}}
+void fn_params_fn_declaration(int fn(), int b);             /* expected-error {{function declaration is not allowed in function parameters}} fxc-error {{X3000: syntax error: unexpected token '('}} fxc-error {{X3000: syntax error: unexpected token ','}} */
+void fn_params_fn_declaration(fn());    /* expected-error {{HLSL requires a type specifier for all declarations}} expected-error {{function declaration is not allowed in function parameters}} fxc-error {{X3000: unrecognized identifier 'fn'}} */
+void fn_params_fn_pointer(int (*fn)(), int b);              /* expected-error {{pointers are unsupported in HLSL}} fxc-error {{X3000: syntax error: unexpected token '('}} */
 
 // Look at the 'mul' issues.
 void fn_mul_test() {