[PATCH] D101484: Improve error messages for attributes in the wrong context.

Nick Lewycky via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 28 13:20:24 PDT 2021


nickwasmer created this revision.
nickwasmer added reviewers: dexonsmith, craig.topper.
nickwasmer added a project: LLVM.
Herald added subscribers: jdoerfert, hiraditya.
nickwasmer requested review of this revision.
Herald added a subscriber: llvm-commits.

verifyFunctionAttrs has a comment that the value V is printed in error messages. The recently added errors for attributes didn't print V. Make them print V.

      

Change the stringification of AttributeList. Firstly they started with 'PAL[' which stood for ParamAttrsList. Change that to 'AttributeList[' matching its current name AttributeList. Print out semantic meaning of the index instead of the raw index value (i.e. 'return', 'function' or 'arg(n)').


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D101484

Files:
  llvm/lib/IR/Attributes.cpp
  llvm/lib/IR/Verifier.cpp
  llvm/unittests/IR/AttributesTest.cpp


Index: llvm/unittests/IR/AttributesTest.cpp
===================================================================
--- llvm/unittests/IR/AttributesTest.cpp
+++ llvm/unittests/IR/AttributesTest.cpp
@@ -217,4 +217,39 @@
   }
 }
 
+TEST(Attributes, AttributeListPrinting) {
+  LLVMContext C;
+
+  {
+    std::string S;
+    raw_string_ostream OS(S);
+    AttributeList AL;
+    AL.addAttribute(C, AttributeList::FunctionIndex, Attribute::AlwaysInline)
+        .print(OS);
+    EXPECT_EQ(S, "AttributeList[\n"
+                 "  { function => alwaysinline }\n"
+                 "]\n");
+  }
+
+  {
+    std::string S;
+    raw_string_ostream OS(S);
+    AttributeList AL;
+    AL.addAttribute(C, AttributeList::ReturnIndex, Attribute::SExt).print(OS);
+    EXPECT_EQ(S, "AttributeList[\n"
+                 "  { return => signext }\n"
+                 "]\n");
+  }
+
+  {
+    std::string S;
+    raw_string_ostream OS(S);
+    AttributeList AL;
+    AL.addParamAttribute(C, 5, Attribute::ZExt).print(OS);
+    EXPECT_EQ(S, "AttributeList[\n"
+                 "  { arg(5) => zeroext }\n"
+                 "]\n");
+  }
+}
+
 } // end anonymous namespace
Index: llvm/lib/IR/Verifier.cpp
===================================================================
--- llvm/lib/IR/Verifier.cpp
+++ llvm/lib/IR/Verifier.cpp
@@ -1895,13 +1895,13 @@
 
   if (AttributeListsVisited.insert(Attrs.getRawPointer()).second) {
     Assert(Attrs.hasParentContext(Context),
-           "Attribute list does not match Module context!", &Attrs);
+           "Attribute list does not match Module context!", &Attrs, V);
     for (const auto &AttrSet : Attrs) {
       Assert(!AttrSet.hasAttributes() || AttrSet.hasParentContext(Context),
-             "Attribute set does not match Module context!", &AttrSet);
+             "Attribute set does not match Module context!", &AttrSet, V);
       for (const auto &A : AttrSet) {
         Assert(A.hasParentContext(Context),
-               "Attribute does not match Module context!", &A);
+               "Attribute does not match Module context!", &A, V);
       }
     }
   }
Index: llvm/lib/IR/Attributes.cpp
===================================================================
--- llvm/lib/IR/Attributes.cpp
+++ llvm/lib/IR/Attributes.cpp
@@ -1693,11 +1693,23 @@
 }
 
 void AttributeList::print(raw_ostream &O) const {
-  O << "PAL[\n";
+  O << "AttributeList[\n";
 
   for (unsigned i = index_begin(), e = index_end(); i != e; ++i) {
-    if (getAttributes(i).hasAttributes())
-      O << "  { " << i << " => " << getAsString(i) << " }\n";
+    if (getAttributes(i).hasAttributes()) {
+      O << "  { ";
+      switch (i) {
+      case AttrIndex::ReturnIndex:
+        O << "return";
+        break;
+      case AttrIndex::FunctionIndex:
+        O << "function";
+        break;
+      default:
+          O << "arg(" << i - AttrIndex::FirstArgIndex << ")";
+      }
+      O << " => " << getAsString(i) << " }\n";
+    }
   }
 
   O << "]\n";


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D101484.341299.patch
Type: text/x-patch
Size: 2971 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210428/ad65bd5d/attachment.bin>


More information about the llvm-commits mailing list