[clang][ExtractAPI] Modify declaration fragment methods to add a new fragment at...
authorruturaj4 <ruturajkvaidya@ku.edu>
Sun, 21 May 2023 17:34:56 +0000 (12:34 -0500)
committerruturaj4 <ruturajkvaidya@ku.edu>
Tue, 30 May 2023 22:30:22 +0000 (17:30 -0500)
The current implementation doesn't support merging declaration fragments at arbitrary offsets. This patch adds that support
by modifying declaration fragment methods.

Differential Revision: https://reviews.llvm.org/D151048

clang/include/clang/ExtractAPI/DeclarationFragments.h
clang/include/clang/ExtractAPI/ExtractAPIVisitor.h

index 90121a1..0eb240d 100644 (file)
@@ -99,25 +99,37 @@ public:
 
   const std::vector<Fragment> &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<size_t>(Index);
+      if (offset > Fragments.size()) {
+        offset = Fragments.size();
+      }
+      return offset;
+    }
+    return Fragments.size() + static_cast<size_t>(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
index 8b3721a..1b82f26 100644 (file)
@@ -110,15 +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.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;
     }
   }