[PATCH] D93814: [TableGen] Fix bug in !interleave operator
Paul C. Anagnostopoulos via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Dec 25 08:56:54 PST 2020
Paul-C-Anagnostopoulos created this revision.
Paul-C-Anagnostopoulos added reviewers: lattner, madhur13490, dblaikie, craig.topper.
Herald added a subscriber: hiraditya.
Paul-C-Anagnostopoulos requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
I forgot to account for the possibility that some elements of the list being interleaved might not be resolved and so final evaluation has to be delayed. Each element must be checked to see if it can be dyn_cast to a StringInit or IntInit.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D93814
Files:
llvm/lib/TableGen/Record.cpp
llvm/test/TableGen/interleave.td
Index: llvm/test/TableGen/interleave.td
===================================================================
--- llvm/test/TableGen/interleave.td
+++ llvm/test/TableGen/interleave.td
@@ -77,6 +77,15 @@
code OperatorList = !interleave(!listconcat(Operators, [[{;}]]), ", ");
}
+// CHECK: def Rec7
+// CHECK: str = "foo/bar/zoo";
+
+def Rec7 {
+ string foo = "foo";
+ string zoo = "oops, not zoo";
+ string str = !interleave([foo, "bar", zoo], "/");
+ let zoo = "zoo";
+}
#ifdef ERROR1
def op;
Index: llvm/lib/TableGen/Record.cpp
===================================================================
--- llvm/lib/TableGen/Record.cpp
+++ llvm/lib/TableGen/Record.cpp
@@ -856,14 +856,19 @@
const StringInit *Delim) {
if (List->size() == 0)
return StringInit::get("");
- SmallString<80> Result(cast<StringInit>(List->getElement(0))->getValue());
+ StringInit *Element = dyn_cast<StringInit>(List->getElement(0));
+ if (!Element)
+ return nullptr;
+ SmallString<80> Result(Element->getValue());
StringInit::StringFormat Fmt = StringInit::SF_String;
for (unsigned I = 1, E = List->size(); I < E; ++I) {
Result.append(Delim->getValue());
- auto *StrInit = cast<StringInit>(List->getElement(I));
- Result.append(StrInit->getValue());
- Fmt = StringInit::determineFormat(Fmt, StrInit->getFormat());
+ StringInit *Element = dyn_cast<StringInit>(List->getElement(I));
+ if (!Element)
+ return nullptr;
+ Result.append(Element->getValue());
+ Fmt = StringInit::determineFormat(Fmt, Element->getFormat());
}
return StringInit::get(Result, Fmt);
}
@@ -872,18 +877,26 @@
const StringInit *Delim) {
if (List->size() == 0)
return StringInit::get("");
- SmallString<80> Result(
- cast<IntInit>(List->getElement(0)->getCastTo(IntRecTy::get()))
- ->getAsString());
+ IntInit *Element =
+ dyn_cast<IntInit>(List->getElement(0)->getCastTo(IntRecTy::get()));
+ if (!Element)
+ return nullptr;
+ SmallString<80> Result(Element->getAsString());
for (unsigned I = 1, E = List->size(); I < E; ++I) {
Result.append(Delim->getValue());
- Result.append(cast<IntInit>(List->getElement(I)->getCastTo(IntRecTy::get()))
- ->getAsString());
+ IntInit *Element =
+ dyn_cast<IntInit>(List->getElement(I)->getCastTo(IntRecTy::get()));
+ if (!Element)
+ return nullptr;
+ Result.append(Element->getAsString());
}
return StringInit::get(Result);
}
+//// Result.append(cast<IntInit>(List->getElement(I)->getCastTo(IntRecTy::get()))
+//// ->getAsString());
+
Init *BinOpInit::getStrConcat(Init *I0, Init *I1) {
// Shortcut for the common case of concatenating two strings.
if (const StringInit *I0s = dyn_cast<StringInit>(I0))
@@ -975,10 +988,13 @@
ListInit *List = dyn_cast<ListInit>(LHS);
StringInit *Delim = dyn_cast<StringInit>(RHS);
if (List && Delim) {
+ StringInit *Result;
if (isa<StringRecTy>(List->getElementType()))
- return interleaveStringList(List, Delim);
+ Result = interleaveStringList(List, Delim);
else
- return interleaveIntList(List, Delim);
+ Result = interleaveIntList(List, Delim);
+ if (Result)
+ return Result;
}
break;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D93814.313732.patch
Type: text/x-patch
Size: 3374 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201225/917bdc83/attachment.bin>
More information about the llvm-commits
mailing list