Skip to content

Commit 8a4451c

Browse files
committed
[llvm-tblgen] Support conditional definitions using !casts clauses
This is a follow on to D145108. This started as simply fixing the crash on an error case reported against that change, but I think this also ends up fixing the original reported issue (#49830) as well. More accurately, D145108 fixed the case where the cast resolves to an existing record, and this change fixes the case where the named record doesn't exist. Differential Revision: https://reviews.llvm.org/D145711
1 parent c380156 commit 8a4451c

File tree

2 files changed

+37
-26
lines changed

2 files changed

+37
-26
lines changed

llvm/lib/TableGen/Record.cpp

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -800,37 +800,40 @@ Init *UnOpInit::Fold(Record *CurRec, bool IsFinal) const {
800800

801801
} else if (isa<RecordRecTy>(getType())) {
802802
if (StringInit *Name = dyn_cast<StringInit>(LHS)) {
803-
if (!CurRec && !IsFinal)
804-
break;
805-
assert(CurRec && "NULL pointer");
806-
Record *D;
807-
808-
// Self-references are allowed, but their resolution is delayed until
809-
// the final resolve to ensure that we get the correct type for them.
810-
auto *Anonymous = dyn_cast<AnonymousNameInit>(CurRec->getNameInit());
811-
if (Name == CurRec->getNameInit() ||
812-
(Anonymous && Name == Anonymous->getNameInit())) {
813-
if (!IsFinal)
814-
break;
815-
D = CurRec;
816-
} else {
817-
D = CurRec->getRecords().getDef(Name->getValue());
818-
if (!D) {
819-
if (IsFinal)
820-
PrintFatalError(CurRec->getLoc(),
821-
Twine("Undefined reference to record: '") +
822-
Name->getValue() + "'\n");
823-
break;
803+
Record *D = RK.getDef(Name->getValue());
804+
if (!D && CurRec) {
805+
// Self-references are allowed, but their resolution is delayed until
806+
// the final resolve to ensure that we get the correct type for them.
807+
auto *Anonymous = dyn_cast<AnonymousNameInit>(CurRec->getNameInit());
808+
if (Name == CurRec->getNameInit() ||
809+
(Anonymous && Name == Anonymous->getNameInit())) {
810+
if (!IsFinal)
811+
break;
812+
D = CurRec;
824813
}
825814
}
826815

816+
auto PrintFatalErrorHelper = [CurRec](const Twine &T) {
817+
if (CurRec)
818+
PrintFatalError(CurRec->getLoc(), T);
819+
else
820+
PrintFatalError(T);
821+
};
822+
823+
if (!D) {
824+
if (IsFinal) {
825+
PrintFatalErrorHelper(Twine("Undefined reference to record: '") +
826+
Name->getValue() + "'\n");
827+
}
828+
break;
829+
}
830+
827831
DefInit *DI = DefInit::get(D);
828832
if (!DI->getType()->typeIsA(getType())) {
829-
PrintFatalError(CurRec->getLoc(),
830-
Twine("Expected type '") +
831-
getType()->getAsString() + "', got '" +
832-
DI->getType()->getAsString() + "' in: " +
833-
getAsString() + "\n");
833+
PrintFatalErrorHelper(Twine("Expected type '") +
834+
getType()->getAsString() + "', got '" +
835+
DI->getType()->getAsString() + "' in: " +
836+
getAsString() + "\n");
834837
}
835838
return DI;
836839
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: not llvm-tblgen %s 2>&1 | FileCheck %s
2+
// XFAIL: vg_leak
3+
4+
class A { int x; }
5+
6+
// CHECK: Undefined reference to record: ''
7+
if !cast<A>("").x then
8+
def x;

0 commit comments

Comments
 (0)