Skip to content

Commit 30bbfda

Browse files
committed
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
1 parent 56d923e commit 30bbfda

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

llvm/lib/IR/Attributes.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,11 +1693,23 @@ unsigned AttributeList::getNumAttrSets() const {
16931693
}
16941694

16951695
void AttributeList::print(raw_ostream &O) const {
1696-
O << "PAL[\n";
1696+
O << "AttributeList[\n";
16971697

16981698
for (unsigned i = index_begin(), e = index_end(); i != e; ++i) {
1699-
if (getAttributes(i).hasAttributes())
1700-
O << " { " << i << " => " << getAsString(i) << " }\n";
1699+
if (!getAttributes(i).hasAttributes())
1700+
continue;
1701+
O << " { ";
1702+
switch (i) {
1703+
case AttrIndex::ReturnIndex:
1704+
O << "return";
1705+
break;
1706+
case AttrIndex::FunctionIndex:
1707+
O << "function";
1708+
break;
1709+
default:
1710+
O << "arg(" << i - AttrIndex::FirstArgIndex << ")";
1711+
}
1712+
O << " => " << getAsString(i) << " }\n";
17011713
}
17021714

17031715
O << "]\n";

llvm/lib/IR/Verifier.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,13 +1895,13 @@ void Verifier::verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
18951895

18961896
if (AttributeListsVisited.insert(Attrs.getRawPointer()).second) {
18971897
Assert(Attrs.hasParentContext(Context),
1898-
"Attribute list does not match Module context!", &Attrs);
1898+
"Attribute list does not match Module context!", &Attrs, V);
18991899
for (const auto &AttrSet : Attrs) {
19001900
Assert(!AttrSet.hasAttributes() || AttrSet.hasParentContext(Context),
1901-
"Attribute set does not match Module context!", &AttrSet);
1901+
"Attribute set does not match Module context!", &AttrSet, V);
19021902
for (const auto &A : AttrSet) {
19031903
Assert(A.hasParentContext(Context),
1904-
"Attribute does not match Module context!", &A);
1904+
"Attribute does not match Module context!", &A, V);
19051905
}
19061906
}
19071907
}

llvm/unittests/IR/AttributesTest.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,39 @@ TEST(Attributes, HasParentContext) {
217217
}
218218
}
219219

220+
TEST(Attributes, AttributeListPrinting) {
221+
LLVMContext C;
222+
223+
{
224+
std::string S;
225+
raw_string_ostream OS(S);
226+
AttributeList AL;
227+
AL.addAttribute(C, AttributeList::FunctionIndex, Attribute::AlwaysInline)
228+
.print(OS);
229+
EXPECT_EQ(S, "AttributeList[\n"
230+
" { function => alwaysinline }\n"
231+
"]\n");
232+
}
233+
234+
{
235+
std::string S;
236+
raw_string_ostream OS(S);
237+
AttributeList AL;
238+
AL.addAttribute(C, AttributeList::ReturnIndex, Attribute::SExt).print(OS);
239+
EXPECT_EQ(S, "AttributeList[\n"
240+
" { return => signext }\n"
241+
"]\n");
242+
}
243+
244+
{
245+
std::string S;
246+
raw_string_ostream OS(S);
247+
AttributeList AL;
248+
AL.addParamAttribute(C, 5, Attribute::ZExt).print(OS);
249+
EXPECT_EQ(S, "AttributeList[\n"
250+
" { arg(5) => zeroext }\n"
251+
"]\n");
252+
}
253+
}
254+
220255
} // end anonymous namespace

0 commit comments

Comments
 (0)