Skip to content

Commit

Permalink
[clang][ExtractAPI] Modify declaration fragment methods to add a new …
Browse files Browse the repository at this point in the history
…fragment at an arbitrary offset.
  • Loading branch information
Ruturaj4 committed May 30, 2023
1 parent 135ce2f commit 92180da
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 31 deletions.
40 changes: 19 additions & 21 deletions clang/include/clang/ExtractAPI/DeclarationFragments.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,34 +97,32 @@ class DeclarationFragments {
Declaration(Declaration) {}
};

using FragmentIterator = std::vector<Fragment>::iterator;
using ConstFragmentIterator = std::vector<Fragment>::const_iterator;

const std::vector<Fragment> &getFragments() const { return Fragments; }

size_t calculateOffset(intmax_t Index) const {
if (Index >= 0) {
size_t offset = static_cast<size_t>(Index);
if (offset > Fragments.size()) {
offset = Fragments.size();
}
return offset;
}
return Fragments.size() + static_cast<size_t>(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;
Expand Down
20 changes: 10 additions & 10 deletions clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,16 @@ template <typename T>
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;
}
}
Expand Down

0 comments on commit 92180da

Please sign in to comment.