[CallBase] Add hasRetAttr version that takes StringRef.

This makes it slightly easier to deal with custom attributes and
CallBase already provides hasFnAttr versions that support both AttrKind
and StringRef arguments in a similar fashion.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D92567
This commit is contained in:
Florian Hahn 2020-12-10 16:58:16 +00:00
parent 9c4cddb53a
commit bb9cef7628
No known key found for this signature in database
GPG Key ID: 61D7554B5CECDC0D
3 changed files with 22 additions and 11 deletions

View File

@ -1553,7 +1553,11 @@ public:
}
/// Determine whether the return value has the given attribute.
bool hasRetAttr(Attribute::AttrKind Kind) const;
bool hasRetAttr(Attribute::AttrKind Kind) const {
return hasRetAttrImpl(Kind);
}
/// Determine whether the return value has the given attribute.
bool hasRetAttr(StringRef Kind) const { return hasRetAttrImpl(Kind); }
/// Determine whether the argument or parameter has the given attribute.
bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const;
@ -2232,6 +2236,18 @@ private:
return hasFnAttrOnCalledFunction(Kind);
}
/// Determine whether the return value has the given attribute. Supports
/// Attribute::AttrKind and StringRef as \p AttrKind types.
template <typename AttrKind> bool hasRetAttrImpl(AttrKind Kind) const {
if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind))
return true;
// Look at the callee, if available.
if (const Function *F = getCalledFunction())
return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind);
return false;
}
};
template <>

View File

@ -322,16 +322,6 @@ Value *CallBase::getReturnedArgOperand() const {
return nullptr;
}
bool CallBase::hasRetAttr(Attribute::AttrKind Kind) const {
if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind))
return true;
// Look at the callee, if available.
if (const Function *F = getCalledFunction())
return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind);
return false;
}
/// Determine whether the argument or parameter has the given attribute.
bool CallBase::paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
assert(ArgNo < getNumArgOperands() && "Param index out of bounds!");

View File

@ -94,6 +94,11 @@ TEST_F(ModuleWithFunctionTest, CallInst) {
EXPECT_EQ(Call->getArgOperand(Idx)->getType(), Arg->getType());
Idx++;
}
Call->addAttribute(llvm::AttributeList::ReturnIndex,
Attribute::get(Call->getContext(), "test-str-attr"));
EXPECT_TRUE(Call->hasRetAttr("test-str-attr"));
EXPECT_FALSE(Call->hasRetAttr("not-on-call"));
}
TEST_F(ModuleWithFunctionTest, InvokeInst) {