[llvm-branch-commits] [llvm] 4820af9 - [TableGen] Fix bug in !interleave operator

Paul C. Anagnostopoulos via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Dec 28 09:22:00 PST 2020


Author: Paul C. Anagnostopoulos
Date: 2020-12-28T12:17:24-05:00
New Revision: 4820af99ddc3271198ecccce4fdd867dc65b11f5

URL: https://github.com/llvm/llvm-project/commit/4820af99ddc3271198ecccce4fdd867dc65b11f5
DIFF: https://github.com/llvm/llvm-project/commit/4820af99ddc3271198ecccce4fdd867dc65b11f5.diff

LOG: [TableGen] Fix bug in !interleave operator

I forgot to account for unresolved elements of the list.

Differential Revision: https://reviews.llvm.org/D93814

Added: 
    

Modified: 
    llvm/lib/TableGen/Record.cpp
    llvm/test/TableGen/interleave.td

Removed: 
    


################################################################################
diff  --git a/llvm/lib/TableGen/Record.cpp b/llvm/lib/TableGen/Record.cpp
index 9c0464d4e1bf..3e6daeb4d238 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<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,14 +877,21 @@ static StringInit *interleaveIntList(const ListInit *List,
                                      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_or_null<IntInit>(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<IntInit>(List->getElement(I)->getCastTo(IntRecTy::get()))
-                      ->getAsString());
+    IntInit *Element =
+        dyn_cast_or_null<IntInit>(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<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;
   }

diff  --git a/llvm/test/TableGen/interleave.td b/llvm/test/TableGen/interleave.td
index 098542ab29a1..c4d5c825d0fc 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;


        


More information about the llvm-branch-commits mailing list