[Serialization] Fix crash while lazy-loading template specializations (#150430) ## Problem This is a regression that was observed in Clang 20 on modules code that uses import std. The lazy-loading mechanism for template specializations introduced in #119333 can currently load additional nodes when called multiple times, which breaks assumptions made by code that iterates over specializations. This leads to iterator invalidation crashes in some scenarios. The core issue occurs when: 1. Code calls `spec_begin()` to get an iterator over template specializations. This invokes `LoadLazySpecializations()`. 2. Code then calls `spec_end()` to get the end iterator. 3. During the `spec_end()` call, `LoadExternalSpecializations()` is invoked again. 4. This can load additional specializations for certain cases, invalidating the begin iterator returned in 1. I was able to trigger the problem when constructing a ParentMapContext. The regression test demonstrates two ways to trigger the construction of the ParentMapContext on problematic code: - The ArrayBoundV2 checker - Unsigned overflow detection in sanitized builds Unfortunately, simply dumping the ast (e.g. using `-ast-dump-all`) doesn't trigger the crash because dumping requires completing the redecl chain before iterating over the specializations. ## Solution The fix ensures that the redeclaration chain is always completed **before** loading external specializations by calling `CompleteRedeclChain(D)` at the start of `LoadExternalSpecializations()`. The idea is to ensure that all `SpecLookups` are fully known and loaded before the call to `LoadExternalSpecializationsImpl()`.
Welcome to the LLVM project!
This repository contains the source code for LLVM, a toolkit for the construction of highly optimized compilers, optimizers, and run-time environments.
The LLVM project has multiple components. The core of the project is itself called “LLVM”. This contains all of the tools, libraries, and header files needed to process intermediate representations and convert them into object files. Tools include an assembler, disassembler, bitcode analyzer, and bitcode optimizer.
C-like languages use the Clang frontend. This component compiles C, C++, Objective-C, and Objective-C++ code into LLVM bitcode -- and from there into object files, using LLVM.
Other components include: the libc++ C++ standard library, the LLD linker, and more.
Consult the Getting Started with LLVM page for information on building and running LLVM.
For information on how to contribute to the LLVM project, please take a look at the Contributing to LLVM guide.
Join the LLVM Discourse forums, Discord chat, LLVM Office Hours or Regular sync-ups.
The LLVM project has adopted a code of conduct for participants to all modes of communication within the project.