[PATCH] D44476: TableGen: Explicitly forbid self-references to field members
Nicolai Hähnle via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Mar 14 08:24:20 PDT 2018
nhaehnle created this revision.
nhaehnle added reviewers: arsenm, craig.topper, tra, MartinO.
Herald added a subscriber: wdng.
Otherwise, patterns like in the test case produce cryptic error
messages about fields being resolved incompletely.
Change-Id: I713c0191f00fe140ad698675803ab1f8823dc5bd
Repository:
rL LLVM
https://reviews.llvm.org/D44476
Files:
include/llvm/TableGen/Record.h
lib/TableGen/Record.cpp
lib/TableGen/TGParser.cpp
test/TableGen/self-reference-recursion.td
Index: test/TableGen/self-reference-recursion.td
===================================================================
--- /dev/null
+++ test/TableGen/self-reference-recursion.td
@@ -0,0 +1,9 @@
+// RUN: not llvm-tblgen %s 2>&1 | FileCheck %s
+// XFAIL: vg_leak
+
+class A<string self> {
+ int x = !cast<A>(self).x;
+}
+
+// CHECK: error: Attempting to access field 'x' of 'A0' is a forbidden self-reference
+def A0 : A<"A0">;
Index: lib/TableGen/TGParser.cpp
===================================================================
--- lib/TableGen/TGParser.cpp
+++ lib/TableGen/TGParser.cpp
@@ -1943,7 +1943,7 @@
Result->getAsString() + "'");
return nullptr;
}
- Result = FieldInit::get(Result, FieldName)->Fold();
+ Result = FieldInit::get(Result, FieldName)->Fold(CurRec);
Lex.Lex(); // eat field name
break;
}
Index: lib/TableGen/Record.cpp
===================================================================
--- lib/TableGen/Record.cpp
+++ lib/TableGen/Record.cpp
@@ -1684,13 +1684,19 @@
Init *FieldInit::resolveReferences(Resolver &R) const {
Init *NewRec = Rec->resolveReferences(R);
if (NewRec != Rec)
- return FieldInit::get(NewRec, FieldName)->Fold();
+ return FieldInit::get(NewRec, FieldName)->Fold(R.getCurrentRecord());
return const_cast<FieldInit *>(this);
}
-Init *FieldInit::Fold() const {
+Init *FieldInit::Fold(Record *CurRec) const {
if (DefInit *DI = dyn_cast<DefInit>(Rec)) {
- Init *FieldVal = DI->getDef()->getValue(FieldName)->getValue();
+ Record *Def = DI->getDef();
+ if (Def == CurRec)
+ PrintFatalError(CurRec->getLoc(),
+ Twine("Attempting to access field '") +
+ FieldName->getAsUnquotedString() + "' of '" +
+ Rec->getAsString() + "' is a forbidden self-reference");
+ Init *FieldVal = Def->getValue(FieldName)->getValue();
if (FieldVal->isComplete())
return FieldVal;
}
Index: include/llvm/TableGen/Record.h
===================================================================
--- include/llvm/TableGen/Record.h
+++ include/llvm/TableGen/Record.h
@@ -1200,7 +1200,7 @@
Init *getBit(unsigned Bit) const override;
Init *resolveReferences(Resolver &R) const override;
- Init *Fold() const;
+ Init *Fold(Record *CurRec) const;
std::string getAsString() const override {
return Rec->getAsString() + "." + FieldName->getValue().str();
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D44476.138367.patch
Type: text/x-patch
Size: 2458 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180314/86f7da2a/attachment-0001.bin>
More information about the llvm-commits
mailing list