Fix off-by-one bug in AttributeList::addAttributes index handling

getParamAlignment expects an argument number, not an AttributeList
index.

Johan Englan, who works on LDC, found this bug and told me about it off
list.

llvm-svn: 303458
This commit is contained in:
Reid Kleckner 2017-05-19 22:23:47 +00:00
parent 708aeafa8b
commit 1bad4eeadf
2 changed files with 20 additions and 1 deletions

View File

@ -1058,7 +1058,7 @@ AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
#ifndef NDEBUG
// FIXME it is not obvious how this should work for alignment. For now, say
// we can't change a known alignment.
unsigned OldAlign = getParamAlignment(Index);
unsigned OldAlign = getAttributes(Index).getAlignment();
unsigned NewAlign = B.getAlignment();
assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
"Attempt to change alignment!");

View File

@ -63,4 +63,23 @@ TEST(Attributes, AddAttributes) {
EXPECT_TRUE(AL.hasFnAttribute(Attribute::NoReturn));
}
TEST(Attributes, AddMatchingAlignAttr) {
LLVMContext C;
AttributeList AL;
AL = AL.addAttribute(C, AttributeList::FirstArgIndex,
Attribute::getWithAlignment(C, 8));
AL = AL.addAttribute(C, AttributeList::FirstArgIndex + 1,
Attribute::getWithAlignment(C, 32));
EXPECT_EQ(8U, AL.getParamAlignment(0));
EXPECT_EQ(32U, AL.getParamAlignment(1));
AttrBuilder B;
B.addAttribute(Attribute::NonNull);
B.addAlignmentAttr(8);
AL = AL.addAttributes(C, AttributeList::FirstArgIndex, B);
EXPECT_EQ(8U, AL.getParamAlignment(0));
EXPECT_EQ(32U, AL.getParamAlignment(1));
EXPECT_TRUE(AL.hasParamAttribute(0, Attribute::NonNull));
}
} // end anonymous namespace