[clang] 135ce2f - [clang][ExtractAPI] Modify declaration fragment methods to add a new fragment at an arbitrary offset.

via cfe-commits cfe-commits at lists.llvm.org
Tue May 30 15:31:04 PDT 2023


Author: ruturaj4
Date: 2023-05-30T17:30:22-05:00
New Revision: 135ce2f820d881d5a7c5d90feab109174918a21f

URL: https://github.com/llvm/llvm-project/commit/135ce2f820d881d5a7c5d90feab109174918a21f
DIFF: https://github.com/llvm/llvm-project/commit/135ce2f820d881d5a7c5d90feab109174918a21f.diff

LOG: [clang][ExtractAPI] Modify declaration fragment methods to add a new fragment at an arbitrary offset.

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/ExtractAPI/DeclarationFragments.h b/clang/include/clang/ExtractAPI/DeclarationFragments.h
index 90121a138175c..0eb240d2b5930 100644
--- a/clang/include/clang/ExtractAPI/DeclarationFragments.h
+++ b/clang/include/clang/ExtractAPI/DeclarationFragments.h
@@ -99,25 +99,37 @@ class DeclarationFragments {
 
   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

diff  --git a/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h b/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
index 8b3721a4d7adb..1b82f2604403d 100644
--- a/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
+++ b/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
@@ -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;
     }
   }


        


More information about the cfe-commits mailing list