Skip to content

[clang][ExtractAPI] Fix iteration order of TopLevelRecords #106411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

daniel-grumberg
Copy link
Contributor

Fixes #106355

@daniel-grumberg daniel-grumberg requested a review from nikic August 28, 2024 16:04
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Aug 28, 2024
@llvmbot
Copy link
Member

llvmbot commented Aug 28, 2024

@llvm/pr-subscribers-clang

Author: Daniel Grumberg (daniel-grumberg)

Changes

Fixes #106355


Full diff: https://github.com/llvm/llvm-project/pull/106411.diff

2 Files Affected:

  • (modified) clang/include/clang/ExtractAPI/API.h (+5-6)
  • (modified) clang/lib/ExtractAPI/API.cpp (+4-2)
diff --git a/clang/include/clang/ExtractAPI/API.h b/clang/include/clang/ExtractAPI/API.h
index 188e35b72117b5..4f34fcc575e807 100644
--- a/clang/include/clang/ExtractAPI/API.h
+++ b/clang/include/clang/ExtractAPI/API.h
@@ -23,7 +23,7 @@
 #include "clang/AST/RawCommentList.h"
 #include "clang/Basic/SourceLocation.h"
 #include "clang/ExtractAPI/DeclarationFragments.h"
-#include "llvm/ADT/SmallPtrSet.h"
+#include "llvm/ADT/SmallVector.h"
 #include "llvm/Support/Allocator.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/TargetParser/Triple.h"
@@ -1420,9 +1420,8 @@ class APISet {
   typename std::enable_if_t<std::is_base_of_v<APIRecord, RecordTy>, RecordTy> *
   createRecord(StringRef USR, StringRef Name, CtorArgsContTy &&...CtorArgs);
 
-  auto getTopLevelRecords() const {
-    return llvm::iterator_range<decltype(TopLevelRecords)::iterator>(
-        TopLevelRecords);
+  ArrayRef<const APIRecord *> getTopLevelRecords() const {
+    return TopLevelRecords;
   }
 
   void removeRecord(StringRef USR);
@@ -1455,7 +1454,7 @@ class APISet {
   // lives in the BumpPtrAllocator.
   using APIRecordStoredPtr = std::unique_ptr<APIRecord, APIRecordDeleter>;
   llvm::DenseMap<StringRef, APIRecordStoredPtr> USRBasedLookupTable;
-  llvm::SmallPtrSet<const APIRecord *, 32> TopLevelRecords;
+  llvm::SmallVector<const APIRecord *, 32> TopLevelRecords;
 
 public:
   const std::string ProductName;
@@ -1481,7 +1480,7 @@ APISet::createRecord(StringRef USR, StringRef Name,
             dyn_cast_if_present<RecordContext>(Record->Parent.Record))
       ParentContext->addToRecordChain(Record);
     else
-      TopLevelRecords.insert(Record);
+      TopLevelRecords.push_back(Record);
   } else {
     Record = dyn_cast<RecordTy>(Result.first->second.get());
   }
diff --git a/clang/lib/ExtractAPI/API.cpp b/clang/lib/ExtractAPI/API.cpp
index 9dbc023885c37f..a6ca0ae8d0d51d 100644
--- a/clang/lib/ExtractAPI/API.cpp
+++ b/clang/lib/ExtractAPI/API.cpp
@@ -150,11 +150,13 @@ void APISet::removeRecord(StringRef USR) {
       if (auto *RecordAsCtx = llvm::dyn_cast<RecordContext>(Record))
         ParentCtx->stealRecordChain(*RecordAsCtx);
     } else {
-      TopLevelRecords.erase(Record);
+      auto *It = llvm::find(TopLevelRecords, Record);
+      if (It != TopLevelRecords.end())
+        TopLevelRecords.erase(It);
       if (auto *RecordAsCtx = llvm::dyn_cast<RecordContext>(Record)) {
         for (const auto *Child = RecordAsCtx->First; Child != nullptr;
              Child = Child->getNextInContext())
-          TopLevelRecords.insert(Child);
+          TopLevelRecords.push_back(Child);
       }
     }
     USRBasedLookupTable.erase(Result);

@daniel-grumberg daniel-grumberg merged commit b9f4afa into llvm:main Aug 29, 2024
10 checks passed
daniel-grumberg added a commit to daniel-grumberg/llvm-project that referenced this pull request Aug 29, 2024
daniel-grumberg added a commit to swiftlang/llvm-project that referenced this pull request Aug 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

reverse-iteration tests fail in the CI
3 participants