[clang] Fix a crash during code completion
authorAdam Czachorowski <adamcz@google.com>
Tue, 1 Jun 2021 16:24:33 +0000 (18:24 +0200)
committerAdam Czachorowski <adamcz@google.com>
Mon, 7 Jun 2021 11:29:58 +0000 (13:29 +0200)
commit721476e6b2119a93033903109b54f429b6e8c91b
tree4e28823197d23a28fd1bdabb376cffd2834b5bba
parent8b58092de49b8be964e760c161585665e8d8c48f
[clang] Fix a crash during code completion

During code completion, lookupInDeclContext() calls
CodeCompletionDeclConsumer::FoundDecl(),which can mutate StoredDeclsMap,
over which lookupInDeclContext() iterates. This can lead to invalidation
of iterators and an assert()-crash.

Example code where this happens:
 #include <list>
 int main() {
   std::list<int>;
   std::^
 }
with code completion on ^ with -std=c++20.

I do not have a repro case that does not need standard library.

This fix stores pointers to NamedDecls in a temporary vector, then
visits them outside of the main loop, when StoredDeclsMap iterators are
gone.

Differential Revision: https://reviews.llvm.org/D103472
clang/lib/Sema/SemaLookup.cpp