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

Fix compiler crash on using incomplete type. (#2073)

Using a forward declared type without a definition may well be illegal, but it shouldn't crash the compiler.
Tristan Labelle 6 жил өмнө
parent
commit
c4e9191dd0

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

@@ -10246,15 +10246,19 @@ bool FlattenedTypeIterator::pushTrackerForType(QualType type, MultiExprArg::iter
 
     if (CXXRecordDecl *cxxRecordDecl =
             dyn_cast<CXXRecordDecl>(recordType->getDecl())) {
-      CXXRecordDecl::base_class_iterator bi, be;
-      bi = cxxRecordDecl->bases_begin();
-      be = cxxRecordDecl->bases_end();
-      if (bi != be) {
-        // Add type tracker for base.
-        // Add base after child to make sure base considered first.
-        m_typeTrackers.push_back(
+      // We'll error elsewhere if the record has no definition,
+      // just don't attempt to use it.
+      if (cxxRecordDecl->hasDefinition()) {
+        CXXRecordDecl::base_class_iterator bi, be;
+        bi = cxxRecordDecl->bases_begin();
+        be = cxxRecordDecl->bases_end();
+        if (bi != be) {
+          // Add type tracker for base.
+          // Add base after child to make sure base considered first.
+          m_typeTrackers.push_back(
             FlattenedTypeIterator::FlattenedTypeTracker(type, bi, be));
-        bAddTracker = true;
+          bAddTracker = true;
+        }
       }
     }
     return bAddTracker;

+ 13 - 0
tools/clang/test/HLSL/incomplete-type.hlsl

@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -Wno-unused-value -fsyntax-only -ffreestanding -verify -verify-ignore-unexpected=note %s
+
+// Tests that the compiler is well-behaved with regard to uses of incomplete types.
+// Regression test for GitHub #2058, which crashed in this case.
+
+struct S;
+ConstantBuffer<S> CB; // expected-error {{variable has incomplete type 'S'}}
+S func( // expected-error {{incomplete result type 'S' in function definition}}
+  S param) // expected-error {{variable has incomplete type 'S'}}
+{
+  S local; // expected-error {{variable has incomplete type 'S'}}
+  return (S)0; // expected-error {{'S' is an incomplete type}}
+}

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

@@ -47,6 +47,7 @@ public:
   TEST_METHOD(RunCXX11Attributes)
   TEST_METHOD(RunEnums)
   TEST_METHOD(RunFunctions)
+  TEST_METHOD(RunIncompleteType)
   TEST_METHOD(RunIndexingOperator)
   TEST_METHOD(RunIntrinsicExamples)
   TEST_METHOD(RunMatrixAssignments)
@@ -189,6 +190,10 @@ TEST_F(VerifierTest, RunFunctions) {
   CheckVerifiesHLSL(L"functions.hlsl");
 }
 
+TEST_F(VerifierTest, RunIncompleteType) {
+  CheckVerifiesHLSL(L"incomplete-type.hlsl");
+}
+
 TEST_F(VerifierTest, RunIndexingOperator) {
   CheckVerifiesHLSL(L"indexing-operator.hlsl");
 }