[llvm] 30bbfda - Improve error messages for attributes in the wrong context.
Nick Lewycky via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 29 01:44:25 PDT 2021
Author: Nick Lewycky
Date: 2021-04-29T01:44:16-07:00
New Revision: 30bbfda01fb6c6c56c55c6729348d9df7f68ac31
URL: https://github.com/llvm/llvm-project/commit/30bbfda01fb6c6c56c55c6729348d9df7f68ac31
DIFF: https://github.com/llvm/llvm-project/commit/30bbfda01fb6c6c56c55c6729348d9df7f68ac31.diff
LOG: Improve error messages for attributes in the wrong context.
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)').
Differential revision: https://reviews.llvm.org/D101484
Added:
Modified:
llvm/lib/IR/Attributes.cpp
llvm/lib/IR/Verifier.cpp
llvm/unittests/IR/AttributesTest.cpp
Removed:
################################################################################
diff --git a/llvm/lib/IR/Attributes.cpp b/llvm/lib/IR/Attributes.cpp
index 30730a4374a5f..d4bf3b1aa44ce 100644
--- a/llvm/lib/IR/Attributes.cpp
+++ b/llvm/lib/IR/Attributes.cpp
@@ -1693,11 +1693,23 @@ unsigned AttributeList::getNumAttrSets() const {
}
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())
+ continue;
+ 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";
diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp
index e83599f7d08f9..856d1896489ff 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -1895,13 +1895,13 @@ void Verifier::verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
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);
}
}
}
diff --git a/llvm/unittests/IR/AttributesTest.cpp b/llvm/unittests/IR/AttributesTest.cpp
index 03b4cef404f5e..f260f0f9bf864 100644
--- a/llvm/unittests/IR/AttributesTest.cpp
+++ b/llvm/unittests/IR/AttributesTest.cpp
@@ -217,4 +217,39 @@ TEST(Attributes, HasParentContext) {
}
}
+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
More information about the llvm-commits
mailing list