diff --git a/clang/include/clang/ExtractAPI/DeclarationFragments.h b/clang/include/clang/ExtractAPI/DeclarationFragments.h --- a/clang/include/clang/ExtractAPI/DeclarationFragments.h +++ b/clang/include/clang/ExtractAPI/DeclarationFragments.h @@ -99,25 +99,37 @@ const std::vector &getFragments() const { return Fragments; } - // Add a new Fragment to the beginning of the Fragments. - DeclarationFragments &appendFront(StringRef Spelling, FragmentKind Kind, - StringRef PreciseIdentifier = "", - const Decl *Declaration = nullptr) { - Fragments.emplace(Fragments.begin(), Spelling, Kind, PreciseIdentifier, - Declaration); + 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); + } + + // 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))); return *this; } - DeclarationFragments &appendFront(DeclarationFragments &&Other) { - Fragments.insert(Fragments.begin(), + DeclarationFragments &insertAtIndex(intmax_t Index, + DeclarationFragments &&Other) { + Fragments.insert(Fragments.begin() + calculateOffset(Index), std::make_move_iterator(Other.Fragments.begin()), std::make_move_iterator(Other.Fragments.end())); Other.Fragments.clear(); return *this; } - void removeLast() { Fragments.pop_back(); } - /// Append a new Fragment to the end of the Fragments. /// /// \returns a reference to the DeclarationFragments object itself after diff --git a/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h b/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h --- a/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h +++ b/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h @@ -110,15 +110,16 @@ static void modifyRecords(const T &Records, const StringRef &Name) { for (const auto &Record : Records) { if (Name == Record.second.get()->Name) { - Record.second.get()->Declaration.removeLast(); Record.second.get() ->Declaration - .appendFront(" ", DeclarationFragments::FragmentKind::Text) - .appendFront("typedef", DeclarationFragments::FragmentKind::Keyword, - "", nullptr) - .append(" { ... } ", DeclarationFragments::FragmentKind::Text) - .append(Name, DeclarationFragments::FragmentKind::Identifier) - .append(";", DeclarationFragments::FragmentKind::Text); + .insertAtIndex(0, "typedef", + DeclarationFragments::FragmentKind::Keyword, "", + nullptr) + .insertAtIndex(1, " ", DeclarationFragments::FragmentKind::Text) + .insertAtIndex(-1, " { ... } ", + DeclarationFragments::FragmentKind::Text) + .insertAtIndex(-1, Name, + DeclarationFragments::FragmentKind::Identifier); break; } }