From: Paul C. Anagnostopoulos Date: Fri, 25 Dec 2020 16:50:12 +0000 (-0500) Subject: [TableGen] Fix bug in !interleave operator X-Git-Tag: llvmorg-13-init~2509 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=4820af99ddc3271198ecccce4fdd867dc65b11f5;p=platform%2Fupstream%2Fllvm.git [TableGen] Fix bug in !interleave operator I forgot to account for unresolved elements of the list. Differential Revision: https://reviews.llvm.org/D93814 --- diff --git a/llvm/lib/TableGen/Record.cpp b/llvm/lib/TableGen/Record.cpp index 9c0464d..3e6daeb4 100644 --- a/llvm/lib/TableGen/Record.cpp +++ b/llvm/lib/TableGen/Record.cpp @@ -856,14 +856,19 @@ static StringInit *interleaveStringList(const ListInit *List, const StringInit *Delim) { if (List->size() == 0) return StringInit::get(""); - SmallString<80> Result(cast(List->getElement(0))->getValue()); + StringInit *Element = dyn_cast(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(List->getElement(I)); - Result.append(StrInit->getValue()); - Fmt = StringInit::determineFormat(Fmt, StrInit->getFormat()); + StringInit *Element = dyn_cast(List->getElement(I)); + if (!Element) + return nullptr; + Result.append(Element->getValue()); + Fmt = StringInit::determineFormat(Fmt, Element->getFormat()); } return StringInit::get(Result, Fmt); } @@ -872,14 +877,21 @@ static StringInit *interleaveIntList(const ListInit *List, const StringInit *Delim) { if (List->size() == 0) return StringInit::get(""); - SmallString<80> Result( - cast(List->getElement(0)->getCastTo(IntRecTy::get())) - ->getAsString()); + IntInit *Element = + dyn_cast_or_null(List->getElement(0) + ->convertInitializerTo(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(List->getElement(I)->getCastTo(IntRecTy::get())) - ->getAsString()); + IntInit *Element = + dyn_cast_or_null(List->getElement(I) + ->convertInitializerTo(IntRecTy::get())); + if (!Element) + return nullptr; + Result.append(Element->getAsString()); } return StringInit::get(Result); } @@ -975,10 +987,13 @@ Init *BinOpInit::Fold(Record *CurRec) const { ListInit *List = dyn_cast(LHS); StringInit *Delim = dyn_cast(RHS); if (List && Delim) { + StringInit *Result; if (isa(List->getElementType())) - return interleaveStringList(List, Delim); + Result = interleaveStringList(List, Delim); else - return interleaveIntList(List, Delim); + Result = interleaveIntList(List, Delim); + if (Result) + return Result; } break; } diff --git a/llvm/test/TableGen/interleave.td b/llvm/test/TableGen/interleave.td index 098542a..c4d5c82 100644 --- a/llvm/test/TableGen/interleave.td +++ b/llvm/test/TableGen/interleave.td @@ -77,6 +77,15 @@ def Rec6 { 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;