llvm.org GIT mirror llvm / 98b92f3
Rewrite the removeAttr() method. This now uses the AttributeSet object instead of the Attribute / AttributeWithIndex objects. It's fairly simple now. It goes through all of the subsets before the one we're modifying, adds them to the new set. It then adds the modified subset (with the requested attributes removed). And then adds the rest of the subsets. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@173660 91177308-0d34-0410-b5e6-96231b3b80d8 Bill Wendling 7 years ago
2 changed file(s) with 41 addition(s) and 53 deletion(s). Raw diff Collapse all Expand all
215215 /// for the result are denoted with Idx = 0.
216216 Attribute getAttributes(unsigned Idx) const;
217217
218 /// \brief Add the specified attribute at the specified index to this
219 /// attribute list. Since attribute lists are immutable, this returns the new
220 /// list.
221 AttributeSet addAttr(LLVMContext &C, unsigned Idx, AttributeSet Attrs) const;
222
223 /// \brief Remove the specified attribute at the specified index from this
224 /// attribute list. Since attribute lists are immutable, this returns the new
225 /// list.
226 AttributeSet removeAttr(LLVMContext &C, unsigned Idx, Attribute Attrs) const;
227
228218 /// \brief Create an AttributeSet from the AttributeWithIndex structures.
229219 /// N.B. this is only temporary. It will be disappearing in the future.
230220 static AttributeSet get(LLVMContext &C, ArrayRef Attrs);
770770
771771 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
772772 Attribute::AttrKind Attr) const {
773 return addAttr(C, Idx, AttributeSet::get(C, Idx, Attr));
773 return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
774774 }
775775
776776 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
777777 AttributeSet Attrs) const {
778 return addAttr(C, Idx, Attrs);
779 }
780
781 AttributeSet AttributeSet::addAttr(LLVMContext &C, unsigned Idx,
782 AttributeSet Attrs) const {
783778 if (!pImpl) return Attrs;
784779 if (!Attrs.pImpl) return *this;
785780
829824
830825 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
831826 Attribute::AttrKind Attr) const {
832 return removeAttr(C, Idx, Attribute::get(C, Attr));
827 return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
833828 }
834829
835830 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
836831 AttributeSet Attrs) const {
837 return removeAttr(C, Idx, Attrs.getAttributes(Idx));
838 }
839
840 AttributeSet AttributeSet::removeAttr(LLVMContext &C, unsigned Idx,
841 Attribute Attrs) const {
832 if (!pImpl) return AttributeSet();
833 if (!Attrs.pImpl) return *this;
834
842835 #ifndef NDEBUG
843836 // FIXME it is not obvious how this should work for alignment.
844837 // For now, say we can't pass in alignment, which no current use does.
845 assert(!Attrs.hasAttribute(Attribute::Alignment) &&
846 "Attempt to exclude alignment!");
838 assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) &&
839 "Attempt to change alignment!");
847840 #endif
848 if (pImpl == 0) return AttributeSet();
849
850 Attribute OldAttrs = getAttributes(Idx);
851 AttrBuilder NewAttrs =
852 AttrBuilder(OldAttrs).removeAttributes(Attrs);
853 if (NewAttrs == AttrBuilder(OldAttrs))
854 return *this;
855
856 SmallVector NewAttrList;
857 ArrayRef OldAttrList = pImpl->getAttributes();
858 unsigned i = 0, e = OldAttrList.size();
859
860 // Copy attributes for arguments before this one.
861 for (; i != e && OldAttrList[i].Index < Idx; ++i)
862 NewAttrList.push_back(OldAttrList[i]);
863
864 // If there are attributes already at this index, merge them in.
865 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
866 Attrs = Attribute::get(C, AttrBuilder(OldAttrList[i].Attrs).
867 removeAttributes(Attrs));
868 ++i;
869 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
870 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
871
872 // Copy attributes for arguments after this one.
873 NewAttrList.insert(NewAttrList.end(),
874 OldAttrList.begin()+i, OldAttrList.end());
875
876 return get(C, NewAttrList);
841
842 // Add the attribute slots before the one we're trying to add.
843 SmallVector AttrSet;
844 uint64_t NumAttrs = pImpl->getNumAttributes();
845 AttributeSet AS;
846 uint64_t LastIndex = 0;
847 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
848 if (getSlotIndex(I) >= Idx) {
849 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
850 break;
851 }
852 LastIndex = I + 1;
853 AttrSet.push_back(getSlotAttributes(I));
854 }
855
856 // Now add the attribute into the correct slot. There may already be an
857 // AttributeSet there.
858 AttrBuilder B(AS, Idx);
859
860 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
861 if (Attrs.getSlotIndex(I) == Idx) {
862 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
863 IE = Attrs.pImpl->end(I); II != IE; ++II)
864 B.removeAttributes(*II);
865 break;
866 }
867
868 AttrSet.push_back(AttributeSet::get(C, Idx, B));
869
870 // Add the remaining attribute slots.
871 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
872 AttrSet.push_back(getSlotAttributes(I));
873
874 return get(C, AttrSet);
877875 }
878876
879877 void AttributeSet::dump() const {