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

R4444 via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat May 27 16:56:42 PDT 2023


Ruturaj4 updated this revision to Diff 526296.
Ruturaj4 added a comment.



1. Updating D151048 <https://reviews.llvm.org/D151048>: [clang][ExtractAPI] Modify declaration fragment methods to add a new fragment at an arbitrary offset. #
2. Enter a brief description of the changes included in this update.
3. The first line is used as subject, next lines as comment. #
4. If you intended to create a new revision, use:
5. $ arc diff --create

I made all the suggested changes.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D151048/new/

https://reviews.llvm.org/D151048

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


Index: clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
===================================================================
--- clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
+++ clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
@@ -110,16 +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
-          .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;
     }
   }
Index: clang/include/clang/ExtractAPI/DeclarationFragments.h
===================================================================
--- clang/include/clang/ExtractAPI/DeclarationFragments.h
+++ clang/include/clang/ExtractAPI/DeclarationFragments.h
@@ -97,34 +97,32 @@
           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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D151048.526296.patch
Type: text/x-patch
Size: 4129 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230527/4bb23b5c/attachment.bin>


More information about the cfe-commits mailing list