aeubanks created this revision. Herald added subscribers: dexonsmith, jdoerfert, arphaman, hiraditya. aeubanks requested review of this revision. Herald added projects: LLDB, LLVM. Herald added subscribers: llvm-commits, lldb-commits. Herald added a subscriber: JDevlieghere.
We expose the fact that we rely on unsigned wrapping to iterate through all indexes. This can be confusing. Rather, keeping it as an implementation detail through an iterator is less confusing and is less code. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D110885 Files: lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptx86ABIFixups.cpp llvm/include/llvm/IR/Attributes.h llvm/lib/Bitcode/Writer/BitcodeWriter.cpp llvm/lib/Bitcode/Writer/ValueEnumerator.cpp llvm/lib/IR/Attributes.cpp llvm/lib/Transforms/Utils/FunctionComparator.cpp llvm/tools/llvm-reduce/deltas/ReduceAttributes.cpp
Index: llvm/tools/llvm-reduce/deltas/ReduceAttributes.cpp =================================================================== --- llvm/tools/llvm-reduce/deltas/ReduceAttributes.cpp +++ llvm/tools/llvm-reduce/deltas/ReduceAttributes.cpp @@ -84,8 +84,7 @@ AttrPtrVecVecTy &AttributeSetsToPreserve) { assert(AttributeSetsToPreserve.empty() && "Should not be sharing vectors."); AttributeSetsToPreserve.reserve(AL.getNumAttrSets()); - for (unsigned SetIdx = AL.index_begin(), SetEndIdx = AL.index_end(); - SetIdx != SetEndIdx; ++SetIdx) { + for (unsigned SetIdx : AL.indexes()) { AttrPtrIdxVecVecTy AttributesToPreserve; AttributesToPreserve.first = SetIdx; visitAttributeSet(AL.getAttributes(AttributesToPreserve.first), Index: llvm/lib/Transforms/Utils/FunctionComparator.cpp =================================================================== --- llvm/lib/Transforms/Utils/FunctionComparator.cpp +++ llvm/lib/Transforms/Utils/FunctionComparator.cpp @@ -110,7 +110,7 @@ if (int Res = cmpNumbers(L.getNumAttrSets(), R.getNumAttrSets())) return Res; - for (unsigned i = L.index_begin(), e = L.index_end(); i != e; ++i) { + for (unsigned i : L.indexes()) { AttributeSet LAS = L.getAttributes(i); AttributeSet RAS = R.getAttributes(i); AttributeSet::iterator LI = LAS.begin(), LE = LAS.end(); Index: llvm/lib/IR/Attributes.cpp =================================================================== --- llvm/lib/IR/Attributes.cpp +++ llvm/lib/IR/Attributes.cpp @@ -1527,7 +1527,7 @@ void AttributeList::print(raw_ostream &O) const { O << "AttributeList[\n"; - for (unsigned i = index_begin(), e = index_end(); i != e; ++i) { + for (unsigned i : indexes()) { if (!getAttributes(i).hasAttributes()) continue; O << " { "; Index: llvm/lib/Bitcode/Writer/ValueEnumerator.cpp =================================================================== --- llvm/lib/Bitcode/Writer/ValueEnumerator.cpp +++ llvm/lib/Bitcode/Writer/ValueEnumerator.cpp @@ -1039,7 +1039,7 @@ } // Do lookups for all attribute groups. - for (unsigned i = PAL.index_begin(), e = PAL.index_end(); i != e; ++i) { + for (unsigned i : PAL.indexes()) { AttributeSet AS = PAL.getAttributes(i); if (!AS.hasAttributes()) continue; Index: llvm/lib/Bitcode/Writer/BitcodeWriter.cpp =================================================================== --- llvm/lib/Bitcode/Writer/BitcodeWriter.cpp +++ llvm/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -835,7 +835,7 @@ SmallVector<uint64_t, 64> Record; for (unsigned i = 0, e = Attrs.size(); i != e; ++i) { AttributeList AL = Attrs[i]; - for (unsigned i = AL.index_begin(), e = AL.index_end(); i != e; ++i) { + for (auto i : AL.indexes()) { AttributeSet AS = AL.getAttributes(i); if (AS.hasAttributes()) Record.push_back(VE.getAttributeGroupID({i, AS})); Index: llvm/include/llvm/IR/Attributes.h =================================================================== --- llvm/include/llvm/IR/Attributes.h +++ llvm/include/llvm/IR/Attributes.h @@ -851,9 +851,27 @@ unsigned getNumAttrSets() const; - /// Use these to iterate over the valid attribute indices. - unsigned index_begin() const { return AttributeList::FunctionIndex; } - unsigned index_end() const { return getNumAttrSets() - 1; } + struct index_iterator { + unsigned NumAttrSets; + index_iterator(int NumAttrSets) : NumAttrSets(NumAttrSets) {} + struct int_wrapper { + int_wrapper(unsigned i) : i(i) {} + unsigned i; + unsigned operator*() { return i; } + bool operator!=(const int_wrapper &Other) { return i != Other.i; } + int_wrapper &operator++() { + ++i; + return *this; + } + }; + + int_wrapper begin() { return int_wrapper(AttributeList::FunctionIndex); } + + int_wrapper end() { return int_wrapper(NumAttrSets - 1); } + }; + + /// Use this to iterate over the valid attribute indices. + index_iterator indexes() const { return index_iterator(getNumAttrSets()); } /// operator==/!= - Provide equality predicates. bool operator==(const AttributeList &RHS) const { return pImpl == RHS.pImpl; } Index: lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptx86ABIFixups.cpp =================================================================== --- lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptx86ABIFixups.cpp +++ lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptx86ABIFixups.cpp @@ -237,8 +237,7 @@ llvm::AttributeList call_attribs = call_inst->getAttributes(); // iterate over the argument attributes - for (unsigned I = call_attribs.index_begin(); I != call_attribs.index_end(); - I++) { + for (unsigned I : call_attribs.indexes()) { // if this argument is passed by val if (call_attribs.hasAttributeAtIndex(I, llvm::Attribute::ByVal)) { // strip away the byval attribute
_______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits