Skip to content

Commit d73829d

Browse files
Disallow volatile keyword (#8397)
`volatile` is not a valid keyword in HLSL, it is meaningless in this language. However, it comes as a result of the fact that HLSL is build on a C/C++ compiler, so there are some C++ artifacts that are part of the language. This scenario goes into more detail: llvm/wg-hlsl#300 Though DXC currently does and seems to always have compiled with the volatile keyword, this is not sensible and should be disallowed. Fixes #8391 Assisted by: Github Copilot --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 3b57f8e commit d73829d

8 files changed

Lines changed: 310 additions & 340 deletions

File tree

tools/clang/include/clang/Lex/Token.h

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -294,25 +294,18 @@ class Token {
294294

295295
// HLSL Change Starts
296296
bool isHLSLReserved() const {
297-
return
298-
is(tok::kw___is_signed) ||
299-
is(tok::kw___declspec) ||
300-
is(tok::kw___forceinline) ||
301-
is(tok::kw_auto) ||
302-
is(tok::kw_catch) || is(tok::kw_const_cast) ||
303-
is(tok::kw_delete) || is(tok::kw_dynamic_cast) ||
304-
is(tok::kw_enum) || is(tok::kw_explicit) ||
305-
is(tok::kw_friend) ||
306-
is(tok::kw_goto) ||
307-
is(tok::kw_mutable) ||
308-
is(tok::kw_new) ||
309-
is(tok::kw_operator) ||
310-
is(tok::kw_protected) || is(tok::kw_private) || is(tok::kw_public) ||
311-
is(tok::kw_reinterpret_cast) ||
312-
is(tok::kw_signed) || is(tok::kw_sizeof) || is(tok::kw_static_cast) ||
313-
is(tok::kw_template) || is(tok::kw_throw) || is(tok::kw_try) || is(tok::kw_typename) ||
314-
is(tok::kw_union) || is(tok::kw_using) ||
315-
is(tok::kw_virtual);
297+
return is(tok::kw___is_signed) || is(tok::kw___declspec) ||
298+
is(tok::kw___forceinline) || is(tok::kw_auto) || is(tok::kw_catch) ||
299+
is(tok::kw_const_cast) || is(tok::kw_delete) ||
300+
is(tok::kw_dynamic_cast) || is(tok::kw_enum) ||
301+
is(tok::kw_explicit) || is(tok::kw_friend) || is(tok::kw_goto) ||
302+
is(tok::kw_mutable) || is(tok::kw_new) || is(tok::kw_operator) ||
303+
is(tok::kw_protected) || is(tok::kw_private) || is(tok::kw_public) ||
304+
is(tok::kw_reinterpret_cast) || is(tok::kw_signed) ||
305+
is(tok::kw_sizeof) || is(tok::kw_static_cast) ||
306+
is(tok::kw_template) || is(tok::kw_throw) || is(tok::kw_try) ||
307+
is(tok::kw_typename) || is(tok::kw_union) || is(tok::kw_using) ||
308+
is(tok::kw_virtual) || is(tok::kw_volatile);
316309
}
317310
// HLSL Change Starts
318311

tools/clang/lib/Parse/ParseDecl.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4193,6 +4193,10 @@ void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
41934193
getLangOpts());
41944194
break;
41954195
case tok::kw_volatile:
4196+
// HLSL Change - volatile is reserved for HLSL
4197+
if (getLangOpts().HLSL)
4198+
goto HLSLReservedKeyword;
4199+
// HLSL Change Ends
41964200
isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
41974201
getLangOpts());
41984202
break;

tools/clang/test/HLSL/cpp-errors-hv2015.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ struct s_with_friend {
6060
};
6161

6262
typedef int (*fn_int_const)(int) const; // expected-error {{expected ';' after top level declarator}} expected-error {{pointers are unsupported in HLSL}} expected-warning {{declaration does not declare anything}}
63-
typedef int (*fn_int_volatile)(int) volatile; // expected-error {{expected ';' after top level declarator}} expected-error {{pointers are unsupported in HLSL}} expected-warning {{declaration does not declare anything}}
63+
typedef int (*fn_int_volatile)(int) volatile; // expected-error {{'volatile' is a reserved keyword in HLSL}} expected-error {{expected ';' after top level declarator}} expected-error {{pointers are unsupported in HLSL}} expected-warning {{declaration does not declare anything}}
6464

6565
void fn_throw() throw() { } // expected-error {{exception specification is unsupported in HLSL}}
6666

tools/clang/test/HLSL/cpp-errors.hlsl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ _Bool g_Bool; // expected-error {{unknown type name '_Bool'}}
3333
_vector int altivec_vector; // expected-error {{expected unqualified-id}} expected-error {{unknown type name '_vector'}}
3434

3535
restrict int g_restrict; // expected-error {{expected unqualified-id}} expected-error {{unknown type name 'restrict'}}
36+
volatile int g_volatile; // expected-error {{'volatile' is a reserved keyword in HLSL}}
3637

3738
__underlying_type(int) g_underlying_type; // expected-error {{__underlying_type is unsupported in HLSL}}
3839
_Atomic(something) g_Atomic; // expected-error {{'_Atomic' is a reserved keyword in HLSL}} expected-error {{HLSL requires a type specifier for all declarations}}
@@ -56,7 +57,7 @@ struct s_with_friend {
5657
};
5758

5859
typedef int (*fn_int_const)(int) const; // expected-error {{expected ';' after top level declarator}} expected-error {{pointers are unsupported in HLSL}} expected-warning {{declaration does not declare anything}}
59-
typedef int (*fn_int_volatile)(int) volatile; // expected-error {{expected ';' after top level declarator}} expected-error {{pointers are unsupported in HLSL}} expected-warning {{declaration does not declare anything}}
60+
typedef int (*fn_int_volatile)(int) volatile; // expected-error {{'volatile' is a reserved keyword in HLSL}} expected-error {{expected ';' after top level declarator}} expected-error {{pointers are unsupported in HLSL}} expected-warning {{declaration does not declare anything}}
6061

6162
void fn_throw() throw() { } // expected-error {{exception specification is unsupported in HLSL}}
6263

tools/clang/test/HLSL/rewriter/correct_rewrites/varmods-syntax_gold.hlsl

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@ precise uniform const float g_pre_uni_init = 1.F;
3737
static float g_sta_init = 1.F;
3838
static const float g_sta_con_init = 1.F;
3939
uniform const float g_uni_init = 1.F;
40-
typedef volatile float2 t_pre_vol;
41-
typedef const volatile float2 t_pre_vol_con;
4240
typedef const float2 t_pre_con;
43-
typedef volatile float2 t_vol;
44-
typedef const volatile float2 t_vol_con;
4541
typedef const float2 t_con;
4642
struct s_storage_mods {
4743
precise float2 f_pre;
@@ -222,22 +218,12 @@ float4 foo_interpolation_different_decl(sample float4 val) {
222218
void vain() {
223219
precise float l_pre;
224220
static precise float l_pre_sta;
225-
static precise volatile float l_pre_sta_vol;
226-
static precise const volatile float l_pre_sta_vol_con;
227221
static precise const float l_pre_sta_con;
228-
precise volatile float l_pre_vol;
229222
static float l_sta;
230-
static volatile float l_sta_vol;
231-
static const volatile float l_sta_vol_con;
232223
static const float l_sta_con;
233-
volatile float l_vol;
234-
static precise const volatile float l_pre_sta_vol_con_init = 0.;
235224
static precise const float l_pre_sta_con_init = 0.;
236-
precise const volatile float l_pre_vol_con_init = 0.;
237225
precise const float l_pre_con_init = 0.;
238-
static const volatile float l_sta_vol_con_init = 0.;
239226
static const float l_sta_con_init = 0.;
240-
const volatile float l_vol_con_init = 0.;
241227
const float l_con_init = 0.;
242228
}
243229

tools/clang/test/HLSL/rewriter/varmods-syntax_noerr.hlsl

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ uniform float g_uni_init = 1.0f;
288288
//typedef precise uniform volatile float2 t_pre_uni_vol; /* expected-error {{'uniform' is not a valid modifier for a typedef}} fxc-error {{X3000: syntax error: unexpected token 'uniform'}} */
289289
//typedef precise uniform volatile const float2 t_pre_uni_vol_con; /* expected-error {{'uniform' is not a valid modifier for a typedef}} fxc-error {{X3000: syntax error: unexpected token 'uniform'}} */
290290
//typedef precise uniform const float2 t_pre_uni_con; /* expected-error {{'uniform' is not a valid modifier for a typedef}} fxc-error {{X3000: syntax error: unexpected token 'uniform'}} */
291-
typedef precise volatile float2 t_pre_vol;
292-
typedef precise volatile const float2 t_pre_vol_con;
291+
//typedef precise volatile float2 t_pre_vol; /* expected-error {{'volatile' is a reserved keyword in HLSL}} */
292+
//typedef precise volatile const float2 t_pre_vol_con; /* expected-error {{'volatile' is a reserved keyword in HLSL}} */
293293
typedef precise const float2 t_pre_con;
294294
//typedef static float2 t_sta; /* expected-error {{cannot combine with previous 'typedef' declaration specifier}} fxc-error {{X3000: syntax error: unexpected token 'static'}} */
295295
//typedef static volatile float2 t_sta_vol; /* expected-error {{cannot combine with previous 'typedef' declaration specifier}} fxc-error {{X3000: syntax error: unexpected token 'static'}} */
@@ -299,8 +299,8 @@ typedef precise const float2 t_pre_con;
299299
//typedef uniform volatile float2 t_uni_vol; /* expected-error {{'uniform' is not a valid modifier for a typedef}} fxc-error {{X3000: syntax error: unexpected token 'uniform'}} */
300300
//typedef uniform volatile const float2 t_uni_vol_con; /* expected-error {{'uniform' is not a valid modifier for a typedef}} fxc-error {{X3000: syntax error: unexpected token 'uniform'}} */
301301
//typedef uniform const float2 t_uni_con; /* expected-error {{'uniform' is not a valid modifier for a typedef}} fxc-error {{X3000: syntax error: unexpected token 'uniform'}} */
302-
typedef volatile float2 t_vol;
303-
typedef volatile const float2 t_vol_con;
302+
//typedef volatile float2 t_vol; /* expected-error {{'volatile' is a reserved keyword in HLSL}} */
303+
//typedef volatile const float2 t_vol_con; /* expected-error {{'volatile' is a reserved keyword in HLSL}} */
304304
typedef const float2 t_con;
305305
// GENERATED_CODE:END
306306

@@ -668,26 +668,26 @@ void vain() {
668668
//extern const float l_ext_con; /* expected-error {{'extern' is not a valid modifier for a local variable}} fxc-error {{X3006: 'l_ext_con': local variables cannot be declared 'extern'}} fxc-error {{X3012: 'l_ext_con': missing initial value}} */
669669
precise float l_pre;
670670
precise static float l_pre_sta;
671-
precise static volatile float l_pre_sta_vol;
672-
precise static volatile const float l_pre_sta_vol_con;
671+
//precise static volatile float l_pre_sta_vol; /* expected-error {{'volatile' is a reserved keyword in HLSL}} */
672+
//precise static volatile const float l_pre_sta_vol_con; /* expected-error {{'volatile' is a reserved keyword in HLSL}} */
673673
precise static const float l_pre_sta_con;
674674
//precise uniform float l_pre_uni; /* expected-error {{'uniform' is not a valid modifier for a local variable}} fxc-error {{X3047: 'l_pre_uni': local variables cannot be declared 'uniform'}} */
675675
//precise uniform volatile float l_pre_uni_vol; /* expected-error {{'uniform' is not a valid modifier for a local variable}} fxc-error {{X3047: 'l_pre_uni_vol': local variables cannot be declared 'uniform'}} */
676676
//precise uniform volatile const float l_pre_uni_vol_con; /* expected-error {{'uniform' is not a valid modifier for a local variable}} fxc-error {{X3012: 'l_pre_uni_vol_con': missing initial value}} fxc-error {{X3047: 'l_pre_uni_vol_con': local variables cannot be declared 'uniform'}} */
677677
//precise uniform const float l_pre_uni_con; /* expected-error {{'uniform' is not a valid modifier for a local variable}} fxc-error {{X3012: 'l_pre_uni_con': missing initial value}} fxc-error {{X3047: 'l_pre_uni_con': local variables cannot be declared 'uniform'}} */
678-
precise volatile float l_pre_vol;
679-
//precise volatile const float l_pre_vol_con; /* fxc-error {{X3012: 'l_pre_vol_con': missing initial value}} */
678+
//precise volatile float l_pre_vol; /* expected-error {{'volatile' is a reserved keyword in HLSL}} */
679+
//precise volatile const float l_pre_vol_con; /* fxc-error {{X3012: 'l_pre_vol_con': missing initial value}} expected-error {{'volatile' is a reserved keyword in HLSL}} */
680680
//precise const float l_pre_con; /* fxc-error {{X3012: 'l_pre_con': missing initial value}} */
681681
static float l_sta;
682-
static volatile float l_sta_vol;
683-
static volatile const float l_sta_vol_con;
682+
//static volatile float l_sta_vol; /* expected-error {{'volatile' is a reserved keyword in HLSL}} */
683+
//static volatile const float l_sta_vol_con; /* expected-error {{'volatile' is a reserved keyword in HLSL}} */
684684
static const float l_sta_con;
685685
//uniform float l_uni; /* expected-error {{'uniform' is not a valid modifier for a local variable}} fxc-error {{X3047: 'l_uni': local variables cannot be declared 'uniform'}} */
686686
//uniform volatile float l_uni_vol; /* expected-error {{'uniform' is not a valid modifier for a local variable}} fxc-error {{X3047: 'l_uni_vol': local variables cannot be declared 'uniform'}} */
687687
//uniform volatile const float l_uni_vol_con; /* expected-error {{'uniform' is not a valid modifier for a local variable}} fxc-error {{X3012: 'l_uni_vol_con': missing initial value}} fxc-error {{X3047: 'l_uni_vol_con': local variables cannot be declared 'uniform'}} */
688688
//uniform const float l_uni_con; /* expected-error {{'uniform' is not a valid modifier for a local variable}} fxc-error {{X3012: 'l_uni_con': missing initial value}} fxc-error {{X3047: 'l_uni_con': local variables cannot be declared 'uniform'}} */
689-
volatile float l_vol;
690-
//volatile const float l_vol_con; /* fxc-error {{X3012: 'l_vol_con': missing initial value}} */
689+
//volatile float l_vol; /* expected-error {{'volatile' is a reserved keyword in HLSL}} */
690+
//volatile const float l_vol_con; /* fxc-error {{X3012: 'l_vol_con': missing initial value}} expected-error {{'volatile' is a reserved keyword in HLSL}} */
691691
//const float l_con; /* fxc-error {{X3012: 'l_con': missing initial value}} */
692692
// GENERATED_CODE:END
693693
// Now with const vars initialized:
@@ -713,17 +713,17 @@ void vain() {
713713
//extern uniform const float l_ext_uni_con_init = 0.0; /* expected-error {{'extern' is not a valid modifier for a local variable}} expected-error {{'uniform' is not a valid modifier for a local variable}} fxc-error {{X3006: 'l_ext_uni_con_init': local variables cannot be declared 'extern'}} fxc-error {{X3012: 'l_ext_uni_con_init': missing initial value}} fxc-error {{X3047: 'l_ext_uni_con_init': local variables cannot be declared 'uniform'}} */
714714
//extern volatile const float l_ext_vol_con_init = 0.0; /* expected-error {{'extern' is not a valid modifier for a local variable}} fxc-error {{X3006: 'l_ext_vol_con_init': local variables cannot be declared 'extern'}} fxc-error {{X3012: 'l_ext_vol_con_init': missing initial value}} */
715715
//extern const float l_ext_con_init = 0.0; /* expected-error {{'extern' is not a valid modifier for a local variable}} fxc-error {{X3006: 'l_ext_con_init': local variables cannot be declared 'extern'}} fxc-error {{X3012: 'l_ext_con_init': missing initial value}} */
716-
precise static volatile const float l_pre_sta_vol_con_init = 0.0;
716+
//precise static volatile const float l_pre_sta_vol_con_init = 0.0; /* expected-error {{'volatile' is a reserved keyword in HLSL}} */
717717
precise static const float l_pre_sta_con_init = 0.0;
718718
//precise uniform volatile const float l_pre_uni_vol_con_init = 0.0; /* expected-error {{'uniform' is not a valid modifier for a local variable}} fxc-error {{X3012: 'l_pre_uni_vol_con_init': missing initial value}} fxc-error {{X3047: 'l_pre_uni_vol_con_init': local variables cannot be declared 'uniform'}} */
719719
//precise uniform const float l_pre_uni_con_init = 0.0; /* expected-error {{'uniform' is not a valid modifier for a local variable}} fxc-error {{X3012: 'l_pre_uni_con_init': missing initial value}} fxc-error {{X3047: 'l_pre_uni_con_init': local variables cannot be declared 'uniform'}} */
720-
precise volatile const float l_pre_vol_con_init = 0.0;
720+
//precise volatile const float l_pre_vol_con_init = 0.0; /* expected-error {{'volatile' is a reserved keyword in HLSL}} */
721721
precise const float l_pre_con_init = 0.0;
722-
static volatile const float l_sta_vol_con_init = 0.0;
722+
//static volatile const float l_sta_vol_con_init = 0.0; /* expected-error {{'volatile' is a reserved keyword in HLSL}} */
723723
static const float l_sta_con_init = 0.0;
724724
//uniform volatile const float l_uni_vol_con_init = 0.0; /* expected-error {{'uniform' is not a valid modifier for a local variable}} fxc-error {{X3012: 'l_uni_vol_con_init': missing initial value}} fxc-error {{X3047: 'l_uni_vol_con_init': local variables cannot be declared 'uniform'}} */
725725
//uniform const float l_uni_con_init = 0.0; /* expected-error {{'uniform' is not a valid modifier for a local variable}} fxc-error {{X3012: 'l_uni_con_init': missing initial value}} fxc-error {{X3047: 'l_uni_con_init': local variables cannot be declared 'uniform'}} */
726-
volatile const float l_vol_con_init = 0.0;
726+
//volatile const float l_vol_con_init = 0.0; /* expected-error {{'volatile' is a reserved keyword in HLSL}} */
727727
const float l_con_init = 0.0;
728728
// GENERATED_CODE:END
729729

@@ -969,4 +969,4 @@ class C
969969
//out float fn_out() { return 1.0f; } /* expected-error {{HLSL usage 'out' is only valid on a parameter}} fxc-error {{X3000: syntax error: unexpected token 'out'}} */
970970
//inout float fn_inout() { return 1.0f; } /* expected-error {{HLSL usage 'inout' is only valid on a parameter}} fxc-error {{X3000: syntax error: unexpected token 'inout'}} */
971971

972-
};
972+
};

tools/clang/test/HLSLFileCheck/hlsl/types/conversions/varmods-syntax_Mod.hlsl

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def make_trunc(num):
4040
return trunc
4141
linear_mods = ['linear', 'sample', 'noperspective', 'centroid']
4242
interp_combos = [('nointerpolation',)] + [('nointerpolation', mod) for mod in linear_mods] + list(gen_combos(linear_mods))
43-
storage_mods = 'groupshared extern precise static uniform volatile const'.split()
43+
storage_mods = 'groupshared extern precise static uniform const'.split()
4444
bad_storage_combos = [('groupshared', 'extern'),
4545
('extern', 'static'),
4646
('static', 'uniform')]
@@ -127,11 +127,7 @@ const float g_con_init = 1.0f; /* fxc-warning {{X32
127127
// <py::lines('GENERATED_CODE')>modify(lines, gen_code('typedef %(mods)s float2 t_%(id)s;', storage_combos))</py>
128128
// GENERATED_CODE:BEGIN
129129
typedef precise float2 t_pre;
130-
typedef precise volatile float2 t_pre_vol;
131-
typedef precise volatile const float2 t_pre_vol_con;
132130
typedef precise const float2 t_pre_con;
133-
typedef volatile float2 t_vol;
134-
typedef volatile const float2 t_vol_con;
135131
typedef const float2 t_con;
136132
// GENERATED_CODE:END
137133

@@ -294,26 +290,16 @@ void main() {
294290
// GENERATED_CODE:BEGIN
295291
precise float l_pre;
296292
precise static float l_pre_sta;
297-
precise static volatile float l_pre_sta_vol;
298-
precise static volatile const float l_pre_sta_vol_con;
299293
precise static const float l_pre_sta_con;
300-
precise volatile float l_pre_vol;
301294
static float l_sta;
302-
static volatile float l_sta_vol;
303-
static volatile const float l_sta_vol_con;
304295
static const float l_sta_con;
305-
volatile float l_vol;
306296
// GENERATED_CODE:END
307297
// Now with const vars initialized:
308298
// <py::lines('GENERATED_CODE')>modify(lines, gen_code('%(mods)s float l_%(id)s_init = 0.0;', filter(lambda combo: 'const' in combo, storage_combos)))</py>
309299
// GENERATED_CODE:BEGIN
310-
precise static volatile const float l_pre_sta_vol_con_init = 0.0;
311300
precise static const float l_pre_sta_con_init = 0.0;
312-
precise volatile const float l_pre_vol_con_init = 0.0;
313301
precise const float l_pre_con_init = 0.0;
314-
static volatile const float l_sta_vol_con_init = 0.0;
315302
static const float l_sta_con_init = 0.0;
316-
volatile const float l_vol_con_init = 0.0;
317303
const float l_con_init = 0.0;
318304
// GENERATED_CODE:END
319305

@@ -378,4 +364,4 @@ class C
378364
// GENERATED_CODE:END
379365

380366

381-
};
367+
};

0 commit comments

Comments
 (0)