From 92180dae775f66193069279ee7532735ea2fee06 Mon Sep 17 00:00:00 2001 From: ruturaj4 Date: Sat, 27 May 2023 18:54:56 -0500 Subject: [PATCH] [clang][ExtractAPI] Modify declaration fragment methods to add a new fragment at an arbitrary offset. --- .../clang/ExtractAPI/DeclarationFragments.h | 40 ++++++++++------------ clang/include/clang/ExtractAPI/ExtractAPIVisitor.h | 20 +++++------ 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/clang/include/clang/ExtractAPI/DeclarationFragments.h b/clang/include/clang/ExtractAPI/DeclarationFragments.h index 0eb240d..4c1b830 100644 --- a/clang/include/clang/ExtractAPI/DeclarationFragments.h +++ b/clang/include/clang/ExtractAPI/DeclarationFragments.h @@ -97,34 +97,32 @@ public: Declaration(Declaration) {} }; + using FragmentIterator = std::vector::iterator; + using ConstFragmentIterator = std::vector::const_iterator; + const std::vector &getFragments() const { return Fragments; } - size_t calculateOffset(intmax_t Index) const { - if (Index >= 0) { - size_t offset = static_cast(Index); - if (offset > Fragments.size()) { - offset = Fragments.size(); - } - return offset; - } - return Fragments.size() + static_cast(Index); - } + FragmentIterator begin() { return Fragments.begin(); } + + FragmentIterator end() { return Fragments.end(); } + + ConstFragmentIterator cbegin() const { return Fragments.cbegin(); } + + ConstFragmentIterator cend() const { return Fragments.cend(); } // Add a new Fragment at an arbitrary offset. - DeclarationFragments &insertAtIndex(intmax_t Index, StringRef Spelling, - FragmentKind Kind, - StringRef PreciseIdentifier = "", - const Decl *Declaration = nullptr) { - Fragments.insert( - Fragments.begin() + calculateOffset(Index), - std::move(Fragment(Spelling, Kind, PreciseIdentifier, Declaration))); + DeclarationFragments &insert(FragmentIterator It, StringRef Spelling, + FragmentKind Kind, + StringRef PreciseIdentifier = "", + const Decl *Declaration = nullptr) { + Fragments.insert(It, std::move(Fragment(Spelling, Kind, PreciseIdentifier, + Declaration))); return *this; } - DeclarationFragments &insertAtIndex(intmax_t Index, - DeclarationFragments &&Other) { - Fragments.insert(Fragments.begin() + calculateOffset(Index), - std::make_move_iterator(Other.Fragments.begin()), + DeclarationFragments &insert(FragmentIterator It, + DeclarationFragments &&Other) { + Fragments.insert(It, std::make_move_iterator(Other.Fragments.begin()), std::make_move_iterator(Other.Fragments.end())); Other.Fragments.clear(); return *this; diff --git a/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h b/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h index 1b82f26..f0882af 100644 --- a/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h +++ b/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h @@ -110,16 +110,16 @@ template static void modifyRecords(const T &Records, const StringRef &Name) { for (const auto &Record : Records) { if (Name == Record.second.get()->Name) { - Record.second.get() - ->Declaration - .insertAtIndex(0, "typedef", - DeclarationFragments::FragmentKind::Keyword, "", - nullptr) - .insertAtIndex(1, " ", DeclarationFragments::FragmentKind::Text) - .insertAtIndex(-1, " { ... } ", - DeclarationFragments::FragmentKind::Text) - .insertAtIndex(-1, Name, - DeclarationFragments::FragmentKind::Identifier); + auto &DeclFragment = Record.second->Declaration; + DeclFragment.insert(DeclFragment.begin(), " ", + DeclarationFragments::FragmentKind::Text); + DeclFragment.insert(DeclFragment.begin(), "typedef", + DeclarationFragments::FragmentKind::Keyword, "", + nullptr); + DeclFragment.insert(--DeclFragment.end(), " { ... } ", + DeclarationFragments::FragmentKind::Text); + DeclFragment.insert(--DeclFragment.end(), Name, + DeclarationFragments::FragmentKind::Identifier); break; } } -- 2.7.4