diff --git a/llvm/lib/TableGen/Record.cpp b/llvm/lib/TableGen/Record.cpp --- a/llvm/lib/TableGen/Record.cpp +++ b/llvm/lib/TableGen/Record.cpp @@ -800,37 +800,40 @@ } else if (isa(getType())) { if (StringInit *Name = dyn_cast(LHS)) { - if (!CurRec && !IsFinal) - break; - assert(CurRec && "NULL pointer"); - Record *D; - - // Self-references are allowed, but their resolution is delayed until - // the final resolve to ensure that we get the correct type for them. - auto *Anonymous = dyn_cast(CurRec->getNameInit()); - if (Name == CurRec->getNameInit() || - (Anonymous && Name == Anonymous->getNameInit())) { - if (!IsFinal) - break; - D = CurRec; - } else { - D = CurRec->getRecords().getDef(Name->getValue()); - if (!D) { - if (IsFinal) - PrintFatalError(CurRec->getLoc(), - Twine("Undefined reference to record: '") + - Name->getValue() + "'\n"); - break; + Record *D = RK.getDef(Name->getValue()); + if (!D && CurRec) { + // Self-references are allowed, but their resolution is delayed until + // the final resolve to ensure that we get the correct type for them. + auto *Anonymous = dyn_cast(CurRec->getNameInit()); + if (Name == CurRec->getNameInit() || + (Anonymous && Name == Anonymous->getNameInit())) { + if (!IsFinal) + break; + D = CurRec; } } + auto PrintFatalErrorHelper = [CurRec](const Twine &T) { + if (CurRec) + PrintFatalError(CurRec->getLoc(), T); + else + PrintFatalError(T); + }; + + if (!D) { + if (IsFinal) { + PrintFatalErrorHelper(Twine("Undefined reference to record: '") + + Name->getValue() + "'\n"); + } + break; + } + DefInit *DI = DefInit::get(D); if (!DI->getType()->typeIsA(getType())) { - PrintFatalError(CurRec->getLoc(), - Twine("Expected type '") + - getType()->getAsString() + "', got '" + - DI->getType()->getAsString() + "' in: " + - getAsString() + "\n"); + PrintFatalErrorHelper(Twine("Expected type '") + + getType()->getAsString() + "', got '" + + DI->getType()->getAsString() + "' in: " + + getAsString() + "\n"); } return DI; } diff --git a/llvm/test/TableGen/if-unresolved-cast.td b/llvm/test/TableGen/if-unresolved-cast.td new file mode 100644 --- /dev/null +++ b/llvm/test/TableGen/if-unresolved-cast.td @@ -0,0 +1,8 @@ +// RUN: not llvm-tblgen %s 2>&1 | FileCheck %s +// XFAIL: vg_leak + +class A { int x; } + +// CHECK: Undefined reference to record: '' +if !cast("").x then + def x;