aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Interpreter/IncrementalParser.cpp
diff options
context:
space:
mode:
authorStefan Gränitz <stefan.graenitz@gmail.com>2024-03-26 15:07:05 +0100
committerGitHub <noreply@github.com>2024-03-26 15:07:05 +0100
commit2e38c50e5c53d66d4968fbd47b78e71a220a28ca (patch)
treee9cc39f0059a0dd15f2788cc30a3b3efa636266a /clang/lib/Interpreter/IncrementalParser.cpp
parent89ef3130cf16f1965475396ad3a50760558cc08a (diff)
downloadllvm-2e38c50e5c53d66d4968fbd47b78e71a220a28ca.zip
llvm-2e38c50e5c53d66d4968fbd47b78e71a220a28ca.tar.gz
llvm-2e38c50e5c53d66d4968fbd47b78e71a220a28ca.tar.bz2
[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. ```
Diffstat (limited to 'clang/lib/Interpreter/IncrementalParser.cpp')
-rw-r--r--clang/lib/Interpreter/IncrementalParser.cpp18
1 files changed, 12 insertions, 6 deletions
diff --git a/clang/lib/Interpreter/IncrementalParser.cpp b/clang/lib/Interpreter/IncrementalParser.cpp
index 370bcbf..5eec2a2 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);
}
}
}