2
0
Эх сурвалжийг харах

Add an error when bitfields are used. (#1719)

Tristan Labelle 6 жил өмнө
parent
commit
2ae1365f91

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

@@ -7446,6 +7446,8 @@ def err_hlsl_attribute_expects_string_literal_from_list: Error<
   "attribute %0 must have one of these values: %1">;
 def err_hlsl_attribute_valid_on_function_only: Error<
   "attribute is valid only on functions">;
+def err_hlsl_bitfields: Error<
+  "bitfields are not supported in HLSL">;
 def err_hlsl_cannot_convert: Error<
   "cannot %select{implicitly |}0convert %select{|output parameter }1from %2 to %3">;
 def err_hlsl_half_load_store: Error<

+ 1 - 1
tools/clang/include/clang/Sema/Sema.h

@@ -8995,7 +8995,7 @@ private:
   mutable IdentifierInfo *Ident___float128;
 
   // HLSL Change Starts
-  bool DiagnoseHLSLDecl(Declarator& D, DeclContext* DC, TypeSourceInfo* TInfo, bool isParameter);
+  bool DiagnoseHLSLDecl(Declarator& D, DeclContext* DC, Expr *BitWidth, TypeSourceInfo* TInfo, bool isParameter);
   bool DiagnoseHLSLLookup(const LookupResult &R);
   void TransferUnusualAttributes(Declarator& D, NamedDecl* NewDecl);
   // HLSL Change Ends

+ 1 - 0
tools/clang/lib/CodeGen/CGHLSLMS.cpp

@@ -922,6 +922,7 @@ unsigned CGMSHLSLRuntime::ConstructStructAnnotation(DxilStructAnnotation *annota
     std::string fieldSemName = "";
 
     QualType fieldTy = fieldDecl->getType();
+    DXASSERT(!fieldDecl->isBitField(), "We should have already ensured we have no bitfields.");
     
     // Align offset.
     offset = AlignBaseOffset(fieldTy, offset, bDefaultRowMajor, CGM, dataLayout);

+ 3 - 3
tools/clang/lib/Sema/SemaDecl.cpp

@@ -4935,7 +4935,7 @@ NamedDecl *Sema::HandleDeclarator(Scope *S, Declarator &D,
   // HLSL Change Starts
   if (getLangOpts().HLSL) {
     const bool IsParameterFalse = false;
-    if (!DiagnoseHLSLDecl(D, DC, TInfo, IsParameterFalse)) {
+    if (!DiagnoseHLSLDecl(D, DC, /*BitWidth*/ nullptr, TInfo, IsParameterFalse)) {
       assert(D.isInvalidType() && "otherwise DiagnoseHLSLDecl failed but "
                                   "didn't invalidate declaration");
       return 0;
@@ -10271,7 +10271,7 @@ Decl *Sema::ActOnParamDeclarator(Scope *S, Declarator &D) {
       Diag(D.getLocStart(), diag::err_hlsl_unsupported_string_decl) << selectParamIdx;
       D.setInvalidType();
     }
-    if (!DiagnoseHLSLDecl(D, Context.getTranslationUnitDecl(), TInfo, IsParm)) {
+    if (!DiagnoseHLSLDecl(D, Context.getTranslationUnitDecl(), /*BitWidth*/ nullptr, TInfo, IsParm)) {
       assert(D.isInvalidType() && "otherwise DiagnoseHLSLDecl failed but "
                                   "didn't invalidate declaration");
     }
@@ -12687,7 +12687,7 @@ FieldDecl *Sema::HandleField(Scope *S, RecordDecl *Record,
   // HLSL Changes Start
   if (getLangOpts().HLSL) {
     const bool IsParameterFalse = false;
-    if (!DiagnoseHLSLDecl(D, CurContext, TInfo, IsParameterFalse)) {
+    if (!DiagnoseHLSLDecl(D, CurContext, BitWidth, TInfo, IsParameterFalse)) {
       // Let the diagnostic provide errors, don't actually return nullptr here;
       // compilation will recover, which is helpful because HLSL diagnostics
       // need not interrupt the declaration processing.

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

@@ -11361,7 +11361,7 @@ static bool IsUsageAttributeCompatible(AttributeList::Kind kind, bool &usageIn,
 }
 
 // Diagnose valid/invalid modifiers for HLSL.
-bool Sema::DiagnoseHLSLDecl(Declarator &D, DeclContext *DC,
+bool Sema::DiagnoseHLSLDecl(Declarator &D, DeclContext *DC, Expr *BitWidth,
                             TypeSourceInfo *TInfo, bool isParameter) {
   assert(getLangOpts().HLSL &&
          "otherwise this is called without checking language first");
@@ -11820,6 +11820,12 @@ bool Sema::DiagnoseHLSLDecl(Declarator &D, DeclContext *DC,
 #endif // ENABLE_SPIRV_CODEGEN
   // SPIRV change ends
 
+  // Disallow bitfields
+  if (BitWidth) {
+    Diag(BitWidth->getExprLoc(), diag::err_hlsl_bitfields);
+    result = false;
+  }
+
   // Validate unusual annotations.
   hlsl::DiagnoseUnusualAnnotationsForHLSL(*this, D.UnusualAnnotations);
   auto && unusualIter = D.UnusualAnnotations.begin();

+ 6 - 0
tools/clang/test/CodeGenHLSL/quick-test/bitfields_error.hlsl

@@ -0,0 +1,6 @@
+// RUN: %dxc /T ps_6_0 /E main %s | FileCheck %s
+
+// CHECK: error: bitfields are not supported in HLSL
+
+struct Struct { uint field : 1; };
+float main() : SV_Target { return 0; }

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

@@ -52,7 +52,7 @@ __FUNCTION__ g___FUNCTION; // expected-error {{expected unqualified-id}}
 __PRETTY__ g___PRETTY; // expected-error {{unknown type name '__PRETTY__'}}
 
 struct s_with_bitfield {
-  int f_bitfield : 3;
+  int f_bitfield : 3; // expected-error {{bitfields are not supported in HLSL}}
 };
 
 struct s_with_friend {

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

@@ -52,7 +52,7 @@ __FUNCTION__ g___FUNCTION; // expected-error {{expected unqualified-id}}
 __PRETTY__ g___PRETTY; // expected-error {{unknown type name '__PRETTY__'}}
 
 struct s_with_bitfield {
-  int f_bitfield : 3;
+  int f_bitfield : 3; // expected-error {{bitfields are not supported in HLSL}}
 };
 
 struct s_with_friend {

+ 0 - 3
tools/clang/test/HLSL/rewriter/correct_rewrites/cpp-errors_gold.hlsl

@@ -9,9 +9,6 @@ const s_arr_i_f arr_struct_one[] = { 1, 2 };
 const s_arr_i_f arr_struct_two[] = { 1, 2, 3, 4 };
 const int g_int;
 const unsigned int g_unsigned_int;
-struct s_with_bitfield {
-  int f_bitfield : 3;
-};
 static int static_int() {
   return 1;
 }

+ 3 - 3
tools/clang/test/HLSL/rewriter/cpp-errors_noerr.hlsl

@@ -51,9 +51,9 @@ unsigned int g_unsigned_int;
 //__FUNCTION__ g___FUNCTION; // expected-error {{expected unqualified-id}}
 //__PRETTY__ g___PRETTY; // expected-error {{unknown type name '__PRETTY__'}}
 
-struct s_with_bitfield {
-  int f_bitfield : 3;
-};
+//struct s_with_bitfield {
+//  int f_bitfield : 3; // expected-error {{bitfields are not supported in HLSL}}
+//};
 
 //struct s_with_friend {
 //  friend void some_fn(); // expected-error {{'friend' is a reserved keyword in HLSL}}