[clang] 2e38c50 - [clang-repl] Fix remove invalidates iterators in CleanUpPTU() (#85378)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 26 07:07:09 PDT 2024
Author: Stefan Gränitz
Date: 2024-03-26T15:07:05+01:00
New Revision: 2e38c50e5c53d66d4968fbd47b78e71a220a28ca
URL: https://github.com/llvm/llvm-project/commit/2e38c50e5c53d66d4968fbd47b78e71a220a28ca
DIFF: https://github.com/llvm/llvm-project/commit/2e38c50e5c53d66d4968fbd47b78e71a220a28ca.diff
LOG: [clang-repl] Fix remove invalidates iterators in CleanUpPTU() (#85378)
Using remove() on DeclContext::lookup_result list invalidates iterators.
This assertion failure was one (fortunate) symptom:
```
clang/include/clang/AST/DeclBase.h:1337: reference clang::DeclListNode::iterator::operator*() const: Assertion `Ptr && "dereferencing end() iterator"' failed.
```
Added:
Modified:
clang/include/clang/AST/DeclContextInternals.h
clang/lib/Interpreter/IncrementalParser.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/AST/DeclContextInternals.h b/clang/include/clang/AST/DeclContextInternals.h
index 903cdb7bfcc822..c4734ab5789538 100644
--- a/clang/include/clang/AST/DeclContextInternals.h
+++ b/clang/include/clang/AST/DeclContextInternals.h
@@ -205,7 +205,7 @@ class StoredDeclsList {
Data.setPointer(Head);
}
- /// Return an array of all the decls that this list represents.
+ /// Return the list of all the decls.
DeclContext::lookup_result getLookupResult() const {
return DeclContext::lookup_result(Data.getPointer());
}
diff --git a/clang/lib/Interpreter/IncrementalParser.cpp b/clang/lib/Interpreter/IncrementalParser.cpp
index 370bcbfee8b014..5eec2a2fd6d1a6 100644
--- a/clang/lib/Interpreter/IncrementalParser.cpp
+++ b/clang/lib/Interpreter/IncrementalParser.cpp
@@ -375,16 +375,22 @@ void IncrementalParser::CleanUpPTU(PartialTranslationUnit &PTU) {
TranslationUnitDecl *MostRecentTU = PTU.TUPart;
TranslationUnitDecl *FirstTU = MostRecentTU->getFirstDecl();
if (StoredDeclsMap *Map = FirstTU->getPrimaryContext()->getLookupPtr()) {
- for (auto I = Map->begin(); I != Map->end(); ++I) {
- StoredDeclsList &List = I->second;
+ for (auto &&[Key, List] : *Map) {
DeclContextLookupResult R = List.getLookupResult();
+ std::vector<NamedDecl *> NamedDeclsToRemove;
+ bool RemoveAll = true;
for (NamedDecl *D : R) {
- if (D->getTranslationUnitDecl() == MostRecentTU) {
+ if (D->getTranslationUnitDecl() == MostRecentTU)
+ NamedDeclsToRemove.push_back(D);
+ else
+ RemoveAll = false;
+ }
+ if (LLVM_LIKELY(RemoveAll)) {
+ Map->erase(Key);
+ } else {
+ for (NamedDecl *D : NamedDeclsToRemove)
List.remove(D);
- }
}
- if (List.isNull())
- Map->erase(I);
}
}
}
More information about the cfe-commits
mailing list