Skip to content

Commit 06fc7d6

Browse files
authored
[clang][bytecode] Don't error out on incomplete declarations (#129685)
Later operations on these are invalid, but the declaration is fine, if extern.
1 parent 25479a3 commit 06fc7d6

File tree

6 files changed

+29
-10
lines changed

6 files changed

+29
-10
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2843,6 +2843,8 @@ bool Compiler<Emitter>::VisitLambdaExpr(const LambdaExpr *E) {
28432843

28442844
assert(Initializing);
28452845
const Record *R = P.getOrCreateRecord(E->getLambdaClass());
2846+
if (!R)
2847+
return false;
28462848

28472849
auto *CaptureInitIt = E->capture_init_begin();
28482850
// Initialize all fields (which represent lambda captures) of the
@@ -4087,9 +4089,8 @@ bool Compiler<Emitter>::visitZeroRecordInitializer(const Record *R,
40874089
} else if (D->isRecord()) {
40884090
if (!this->visitZeroRecordInitializer(D->ElemRecord, E))
40894091
return false;
4090-
} else {
4091-
assert(false);
4092-
}
4092+
} else
4093+
return false;
40934094

40944095
if (!this->emitFinishInitPop(E))
40954096
return false;

clang/lib/AST/ByteCode/Descriptor.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,10 +404,10 @@ Descriptor::Descriptor(const DeclTy &D, const Record *R, MetadataSize MD,
404404
}
405405

406406
/// Dummy.
407-
Descriptor::Descriptor(const DeclTy &D)
408-
: Source(D), ElemSize(1), Size(1), MDSize(0), AllocSize(MDSize),
409-
ElemRecord(nullptr), IsConst(true), IsMutable(false), IsTemporary(false),
410-
IsDummy(true) {
407+
Descriptor::Descriptor(const DeclTy &D, MetadataSize MD)
408+
: Source(D), ElemSize(1), Size(1), MDSize(MD.value_or(0)),
409+
AllocSize(MDSize), ElemRecord(nullptr), IsConst(true), IsMutable(false),
410+
IsTemporary(false), IsDummy(true) {
411411
assert(Source && "Missing source");
412412
}
413413

clang/lib/AST/ByteCode/Descriptor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ struct Descriptor final {
200200
bool IsTemporary, bool IsMutable);
201201

202202
/// Allocates a dummy descriptor.
203-
Descriptor(const DeclTy &D);
203+
Descriptor(const DeclTy &D, MetadataSize MD = std::nullopt);
204204

205205
/// Make this descriptor a dummy descriptor.
206206
void makeDummy() { IsDummy = true; }
@@ -263,7 +263,7 @@ struct Descriptor final {
263263
bool isUnknownSizeArray() const { return Size == UnknownSizeMark; }
264264

265265
/// Checks if the descriptor is of a primitive.
266-
bool isPrimitive() const { return !IsArray && !ElemRecord; }
266+
bool isPrimitive() const { return !IsArray && !ElemRecord && !IsDummy; }
267267

268268
/// Checks if the descriptor is of an array.
269269
bool isArray() const { return IsArray; }

clang/lib/AST/ByteCode/Pointer.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,12 @@ APValue Pointer::toAPValue(const ASTContext &ASTCtx) const {
248248
Index = Ptr.getIndex();
249249

250250
QualType ElemType = Desc->getElemQualType();
251-
Offset += (Index * ASTCtx.getTypeSizeInChars(ElemType));
251+
if (const auto *RD = ElemType->getAsRecordDecl();
252+
RD && !RD->getDefinition()) {
253+
// Ignore this for the offset.
254+
} else {
255+
Offset += (Index * ASTCtx.getTypeSizeInChars(ElemType));
256+
}
252257
if (Ptr.getArray().getType()->isArrayType())
253258
Path.push_back(APValue::LValuePathEntry::ArrayIndex(Index));
254259
Ptr = Ptr.getArray();

clang/lib/AST/ByteCode/Program.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ Descriptor *Program::createDescriptor(const DeclTy &D, const Type *Ty,
393393
if (const auto *Record = getOrCreateRecord(RT->getDecl()))
394394
return allocateDescriptor(D, Record, MDSize, IsConst, IsTemporary,
395395
IsMutable);
396+
return allocateDescriptor(D, MDSize);
396397
}
397398

398399
// Arrays.

clang/test/AST/ByteCode/records.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,3 +1747,15 @@ namespace CtorOfInvalidClass {
17471747
template<ReferenceOf<InvalidCtor> auto R, typename Rep> int F; // both-error {{non-type template argument is not a constant expression}}
17481748
#endif
17491749
}
1750+
1751+
namespace IncompleteTypes {
1752+
struct Incomplete;
1753+
1754+
constexpr bool foo() {
1755+
extern Incomplete bounded[10];
1756+
extern Incomplete unbounded[];
1757+
extern Incomplete IT;
1758+
return true;
1759+
}
1760+
static_assert(foo(), "");
1761+
}

0 commit comments

Comments
 (0)