diff options
author | Serge Guelton <sguelton@redhat.com> | 2021-03-24 16:45:04 -0400 |
---|---|---|
committer | serge-sans-paille <sguelton@redhat.com> | 2021-04-17 08:17:33 +0200 |
commit | d6de1e1a71406c75a4ea4d5a2fe84289f07ea3a1 (patch) | |
tree | abbf039ca49e20d35a9796d4fae72300ac4f5072 /llvm/lib/IR/Attributes.cpp | |
parent | bbba69425c6131283163af99201577c296aa3877 (diff) | |
download | llvm-d6de1e1a71406c75a4ea4d5a2fe84289f07ea3a1.zip llvm-d6de1e1a71406c75a4ea4d5a2fe84289f07ea3a1.tar.gz llvm-d6de1e1a71406c75a4ea4d5a2fe84289f07ea3a1.tar.bz2 |
Normalize interaction with boolean attributes
Such attributes can either be unset, or set to "true" or "false" (as string).
throughout the codebase, this led to inelegant checks ranging from
if (Fn->getFnAttribute("no-jump-tables").getValueAsString() == "true")
to
if (Fn->hasAttribute("no-jump-tables") && Fn->getFnAttribute("no-jump-tables").getValueAsString() == "true")
Introduce a getValueAsBool that normalize the check, with the following
behavior:
no attributes or attribute set to "false" => return false
attribute set to "true" => return true
Differential Revision: https://reviews.llvm.org/D99299
Diffstat (limited to 'llvm/lib/IR/Attributes.cpp')
-rw-r--r-- | llvm/lib/IR/Attributes.cpp | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/llvm/lib/IR/Attributes.cpp b/llvm/lib/IR/Attributes.cpp index 60ad3b8..30730a4 100644 --- a/llvm/lib/IR/Attributes.cpp +++ b/llvm/lib/IR/Attributes.cpp @@ -287,6 +287,13 @@ uint64_t Attribute::getValueAsInt() const { return pImpl->getValueAsInt(); } +bool Attribute::getValueAsBool() const { + if (!pImpl) return false; + assert(isStringAttribute() && + "Expected the attribute to be a string attribute!"); + return pImpl->getValueAsBool(); +} + StringRef Attribute::getKindAsString() const { if (!pImpl) return {}; assert(isStringAttribute() && @@ -650,6 +657,11 @@ uint64_t AttributeImpl::getValueAsInt() const { return static_cast<const IntAttributeImpl *>(this)->getValue(); } +bool AttributeImpl::getValueAsBool() const { + assert(getValueAsString().empty() || getValueAsString() == "false" || getValueAsString() == "true"); + return getValueAsString() == "true"; +} + StringRef AttributeImpl::getKindAsString() const { assert(isStringAttribute()); return static_cast<const StringAttributeImpl *>(this)->getStringKind(); |