aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Serialization
AgeCommit message (Collapse)AuthorFilesLines
2 days[Serialization] Fix crash while lazy-loading template specializations (#150430)Michael Jabbour1-0/+1
## 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()`.
12 daysReland [Clang] Make the SizeType, SignedSizeType and PtrdiffType be named ↵YexuanXiao2-1/+10
sugar types (#149613) The checks for the 'z' and 't' format specifiers added in the original PR #143653 had some issues and were overly strict, causing some build failures and were consequently reverted at https://github.com/llvm/llvm-project/commit/4c85bf2fe8042c855c9dd5be4b02191e9d071ffd. In the latest commit https://github.com/llvm/llvm-project/pull/149613/commits/27c58629ec76a703fde9c0b99b170573170b4a7a, I relaxed the checks for the 'z' and 't' format specifiers, so warnings are now only issued when they are used with mismatched types. The original intent of these checks was to diagnose code that assumes the underlying type of `size_t` is `unsigned` or `unsigned long`, for example: ```c printf("%zu", 1ul); // Not portable, but not an error when size_t is unsigned long ``` However, it produced a significant number of false positives. This was partly because Clang does not treat the `typedef` `size_t` and `__size_t` as having a common "sugar" type, and partly because a large amount of existing code either assumes `unsigned` (or `unsigned long`) is `size_t`, or they define the equivalent of size_t in their own way (such as sanitizer_internal_defs.h).https://github.com/llvm/llvm-project/blob/2e67dcfdcd023df2f06e0823eeea23990ce41534/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h#L203
13 daysRevert "[Clang] Make the SizeType, SignedSizeType and PtrdiffType be named ↵Kazu Hirata2-10/+1
sugar types instead of built-in types (#143653)" This reverts commit c27e283cfbca2bd22f34592430e98ee76ed60ad8. A builbot failure has been reported: https://lab.llvm.org/buildbot/#/builders/186/builds/10819/steps/10/logs/stdio I'm also getting a large number of warnings related to %zu and %zx.
13 days[Clang] Make the SizeType, SignedSizeType and PtrdiffType be named sugar ↵YexuanXiao2-1/+10
types instead of built-in types (#143653) Including the results of `sizeof`, `sizeof...`, `__datasizeof`, `__alignof`, `_Alignof`, `alignof`, `_Countof`, `size_t` literals, and signed `size_t` literals, the results of pointer-pointer subtraction and checks for standard library functions (and their calls). The goal is to enable clang and downstream tools such as clangd and clang-tidy to provide more portable hints and diagnostics. The previous discussion can be found at #136542. This PR implements this feature by introducing a new subtype of `Type` called `PredefinedSugarType`, which was considered appropriate in discussions. I tried to keep `PredefinedSugarType` simple enough yet not limited to `size_t` and `ptrdiff_t` so that it can be used for other purposes. `PredefinedSugarType` wraps a canonical `Type` and provides a name, conceptually similar to a compiler internal `TypedefType` but without depending on a `TypedefDecl` or a source file. Additionally, checks for the `z` and `t` format specifiers in format strings for `scanf` and `printf` were added. It will precisely match expressions using `typedef`s or built-in expressions. The affected tests indicates that it works very well. Several code require that `SizeType` is canonical, so I kept `SizeType` to its canonical form. The failed tests in CI are allowed to fail. See the [comment](https://github.com/llvm/llvm-project/pull/135386#issuecomment-3049426611) in another PR #135386.
13 days[Clang][AST][NFC] Introduce `NamespaceBaseDecl` (#149123)Yanzuo Liu3-14/+2
Add `NamespaceBaseDecl` as common base class of `NamespaceDecl` and `NamespaceAliasDecl`. This simplifies `NestedNameSpecifier` a bit. Co-authored-by: Matheus Izvekov <mizvekov@gmail.com>
2025-07-15[clang][modules] Serialize `CodeGenOptions` (#146422)Jan Svoboda3-7/+150
Some `LangOptions` duplicate their `CodeGenOptions` counterparts. My understanding is that this was done solely because some infrastructure (like preprocessor initialization, serialization, module compatibility checks, etc.) were only possible/convenient for `LangOptions`. This PR implements the missing support for `CodeGenOptions`, which makes it possible to remove some duplicate `LangOptions` fields and simplify the logic. Motivated by https://github.com/llvm/llvm-project/pull/146342.
2025-07-10[Sema] Fix lifetime extension for temporaries in range-based for loops in ↵Marco Vitale2-0/+3
C++23 (#145164) C++23 mandates that temporaries used in range-based for loops are lifetime-extended to cover the full loop. This patch adds a check for loop variables and compiler- generated `__range` bindings to apply the correct extension. Includes test cases based on examples from CWG900/P2644R1. Fixes https://github.com/llvm/llvm-project/issues/109793
2025-07-07[clang] Refactor `LangOptions` to specify compatibility as X macro arg (#146766)Jan Svoboda2-42/+48
This removes the `{BENIGN,COMPATIBLE}{,_ENUM,_VALUE}_LANGOPT` X macros controlling `LangOptions`. These are permutations of the base `LANGOPT`, `ENUM_LANGOPT` and `VALUE_LANGOPT` X macros that also carry the information of their effect on AST (and therefore module compatibility). Their functionality is now implemented by passing `Benign`, `Compatible` or `NotCompatible` argument to the base X macros and using C++17 `if constexpr` in the clients to achieve the same codegen. This PR solves this FIXME: ``` // FIXME: Clients should be able to more easily select whether they want // different levels of compatibility versus how to handle different kinds // of option. ``` The base X macros are preserved, since they are used in `LangOptions.h` to generate different kinds of field and function declarations for flags, values and enums, which can't be achieved with `if constexpr`. The new syntax also forces developers to think about compatibility when adding new language option, hopefully reducing the number of new options that are affecting by default even though they are benign or compatible. Note that the `BENIGN_` macros used to forward to their `COMPATIBLE_` counterparts. I don't think this ever kicked in, since there are no clients of the `.def` file that define `COMPATIBLE_` without also defining `BENIGN_`. However, this might be something downstream forks need to take care of by doing `if constexpr (CK::Compatibility == CK::Benign || CK::Compatibility == CK::Compatible)` in place of `#define COMPATIBLE_`.
2025-07-07NFC, use structured binding to simplify the code.Haojian Wu1-3/+1
2025-07-07[clang] Remove source range from CXXOperatorCallExpr (#147028)Haojian Wu3-3/+2
This patch stops storing a source range in `CXXOperatorCallExpr` and keeps only the begin location. This change allows us to retain the optimization #141058 when switching to 64-bit source locations. Performance results: https://llvm-compile-time-tracker.com/compare.php?from=0588e8188c647460b641b09467fe6b13a8d510d5&to=5958f83476a8b8ba97936f262396d3ff98fb1662&stat=instructions:u
2025-07-06[Serialization] Remove an unnecessary cast (NFC) (#147204)Kazu Hirata1-1/+1
IndexFromEnd is already of int.
2025-07-03[Serialization] Remove an unnecessary cast (NFC) (#146986)Kazu Hirata1-2/+1
Lambda is already of CXXRecordDecl *.
2025-07-03[Modules] Record side effect info in EvaluatedStmt (#146468)Henrik G. Olsson4-8/+6
All deserialized VarDecl initializers are EvaluatedStmt, but not all EvaluatedStmt initializers are from a PCH. Calling `VarDecl::hasInitWithSideEffects` can trigger constant evaluation, but it's hard to know ahead of time whether that will trigger deserialization - even if the initializer is fully deserialized, it may contain a call to a constructor whose body is not deserialized. By caching the result of `VarDecl::hasInitWithSideEffects` and populating that cache during deserialization we can guarantee that calling it won't trigger deserialization regardless of the state of the initializer. This also reduces memory usage by removing the `InitSideEffectVars` set in `ASTReader`. rdar://154717930
2025-07-03[C++20] [Modules] Use current named module to do module local lookupChuanqi Xu1-4/+12
See the attached test for the motiviation. Previously we dependent on the module ownership of the decl context to perform module local lookup. But if the lookup is unqualified, we may perform the lookup with canonical decl, which belongs to the incorrect named module
2025-07-01[Serialization] Use SourceLocation::UIntTy for the offset type, NFCHaojian Wu1-1/+1
2025-06-28[clang] Remove unused includes (NFC) (#146254)Kazu Hirata1-1/+0
These are identified by misc-include-cleaner. I've filtered out those that break builds. Also, I'm staying away from llvm-config.h, config.h, and Compiler.h, which likely cause platform- or compiler-specific build failures.
2025-06-27[ADT] Deprecate MutableArrayRef(std::nullopt) (#146113)Kazu Hirata1-1/+1
ArrayRef(std::nullopt) just got deprecated. This patch does the same to MutableArrayRef(std::nullopt). Since there are only a couple of uses, this patch does migration and deprecation at the same time.
2025-06-26[clang] NFC: Add alias for std::pair<FileID, unsigned> used in ↵Haojian Wu1-2/+1
SourceLocation (#145711) Introduce a type alias for the commonly used `std::pair<FileID, unsigned>` to improve code readability, and make it easier for future updates (64-bit source locations).
2025-06-26[Modules] Add merged Files to UsedModuleFilesChuanqi Xu1-1/+6
This is needed by no casacading chanegs feature. A BMI of a module interface needs to merge the hash value of all the module files that the users can touched actually.
2025-06-25[Serialization] Remove delta encoding optimization (#145670)Haojian Wu2-42/+26
See the discussion in https://github.com/llvm/llvm-project/pull/145529. This will slightly increase the PCM size (~5%), some data (in-memory preamble size in clangd): - SemaExpr.cpp: 77MB -> 80MB - FindTarget.cpp: 71MB -> 75MB
2025-06-25[NFC] [Modules] Adding profiling points to template lookups in serializerChuanqi Xu2-0/+11
2025-06-25[C++20] [Modules] Avoid crash with calls to (this auto) syntaxChuanqi Xu1-1/+1
Due to we didn't consider (this, auto) information when setting abbrev for calls, we use incorrect format for calls, which cause crashes. From https://github.com/llvm/llvm-project/issues/118137
2025-06-24[OpenMP][clang] 6.0: parsing/sema for num_threads 'strict' modifier (#145490)Robert Imschweiler2-0/+4
Implement parsing and semantic analysis support for the optional 'strict' modifier of the num_threads clause. This modifier has been introduced in OpenMP 6.0, section 12.1.2. Note: this is basically 1:1 https://reviews.llvm.org/D138328.
2025-06-23Reland "[Modules] Record whether VarDecl initializers contain side effects" ↵Henrik G. Olsson3-6/+15
(#145447) This reverts commit 329ae86 and adds an early exit for EvaluateInPlace when the expression's type is null.
2025-06-23Revert "[Modules] Record whether VarDecl initializers contain side effects" ↵Jonas Devlieghere3-15/+6
(#145407) Reverts llvm/llvm-project#143739 because it triggers an assert: ``` Assertion failed: (!isNull() && "Cannot retrieve a NULL type pointer"), function getCommonPtr, file Type.h, line 952. ```
2025-06-23[Modules] Record whether VarDecl initializers contain side effects (#143739)Henrik G. Olsson3-6/+15
Calling `DeclMustBeEmitted` should not lead to more deserialization, as it may occur before previous deserialization has finished. When passed a `VarDecl` with an initializer however, `DeclMustBeEmitted` needs to know whether that initializer contains side effects. When the `VarDecl` is deserialized but the initializer is not, this triggers deserialization of the initializer. To avoid this we add a bit to the serialization format for `VarDecl`s, indicating whether its initializer contains side effects or not, so that the `ASTReader` can query this information directly without deserializing the initializer. rdar://153085264
2025-06-23[C++20] [Modules] Don't make enum constant members always visibleChuanqi Xu1-1/+31
Close https://github.com/llvm/llvm-project/issues/131058 See the comments in ASTWriter.cpp:ASTDeclContextNameLookupTrait::getLookupVisibility and SemaLookup.cpp:Sema::makeMergedDefinitionVisible for details.
2025-06-19[NFC][Clang] Adopt simplified `getTrailingObjects` in ASTReader (#144438)Rahul Joshi2-23/+20
2025-06-19[NFC] [Serialization] Some Code Cleanups for Name lookup table thingsChuanqi Xu4-156/+153
2025-06-18[Clang][OpenMP][LoopTransformations] Fix incorrect number of generated loops ↵Walter J.T.V1-3/+2
for Tile and Reverse directives (#140532) This patch is closely related to #139293 and addresses an existing issue in the loop transformation codebase. Specifically, it corrects the handling of the `NumGeneratedLoops` variable in `OMPLoopTransformationDirective` AST nodes and its inheritors (such as OMPUnrollDirective, OMPTileDirective, etc.). Previously, this variable was inaccurately set for certain transformations like reverse or tile. While this did not lead to functional bugs, since the value was only checked to determine whether it was greater than zero or equal to zero, the inconsistency could introduce problems when supporting more complex directives in the future.
2025-06-17[C++20][Modules] Disable preferred_name when writing a C++20 header unit ↵Dmitry Polukhin1-2/+3
(#144377) https://reviews.llvm.org/D130331 added workaround for named modules only. But the same issue happens for headees units. Link issue #56490
2025-06-15[Clang] add fix-it hints for unknown attributes (#141305)Oleksandr T.1-2/+2
This patch adds fix-it hints for unknown attribute names when Clang suggests a correction
2025-06-13Remove delayed typo expressions (#143423)Aaron Ballman2-10/+0
This removes the delayed typo correction functionality from Clang (regular typo correction still remains) due to fragility of the solution. An RFC was posted here: https://discourse.llvm.org/t/rfc-removing-support-for-delayed-typo-correction/86631 and while that RFC was asking for folks to consider stepping up to be maintainers, and we did have a few new contributors show some interest, experiments show that it's likely worth it to remove this functionality entirely and focus efforts on improving regular typo correction. This removal fixes ~20 open issues (quite possibly more), improves compile time performance by roughly .3-.4% (https://llvm-compile-time-tracker.com/?config=Overview&stat=instructions%3Au&remote=AaronBallman&sortBy=date), and does not appear to regress diagnostic behavior in a way we wouldn't find acceptable. Fixes #142457 Fixes #139913 Fixes #138850 Fixes #137867 Fixes #137860 Fixes #107840 Fixes #93308 Fixes #69470 Fixes #59391 Fixes #58172 Fixes #46215 Fixes #45915 Fixes #45891 Fixes #44490 Fixes #36703 Fixes #32903 Fixes #23312 Fixes #69874
2025-06-12[C++20] [Modules] [Reduced BMI] Don't write specializations with local argsChuanqi Xu1-0/+45
Close https://github.com/llvm/llvm-project/issues/119947 As discussed in the above thread, we shouldn't write specializations with local args in reduced BMI. Since users can't find such specializations any way.
2025-06-08[Clang] Support constexpr asm at global scope. (#143268)Corentin Jabot1-1/+1
I previously failed to realize this feature existed... Fixes #137459 Fixes #143242
2025-06-03[clang][modules] rename "AST" to precompiled in diagnostic messages NFCI ↵Cyndy Ishida1-5/+6
(#142635) As follow up to: https://github.com/llvm/llvm-project/commit/883130e33325282cfd31b68f5db52891442c20b7
2025-06-03[clang][AST] Reduce some AST node size. (#142585)Haojian Wu2-10/+10
This patch reduces the size of several AST nodes by moving some fields into the free bitfield space in the base `Stmt` class: * `CXXForRangeStmt`: 96 → 88 bytes * `ChooseExpr`: 56 → 48 bytes * `ArrayTypeTraitExpr`: 56 → 48 bytes * `ExpressionTraitExpr`: 40 → 32 bytes * `CXXFoldExpr`: 64 → 56 bytes * `ShuffleExpr`: 40 → 32 bytes * `PackIndexingExpr`: 48 → 40 bytes There are no noticeable memory savings (`Expr/Stmt` memory usage 125,824,496 vs 125,826,336 bytes for `SemaExpr.cpp`) in my testing, likely because these node types are not among the most common in typical ASTs.
2025-06-03[clang][modules] Fix lambda and its enclosing function are not loaded from ↵Haojian Wu1-1/+1
same module. (#142467) This is a follow-up fix to https://github.com/llvm/llvm-project/pull/109167. Previously, we stored a mapping between the enclosing function and the lambda class declaration. When loading the enclosing function, we also loaded the corresponding lambda class declaration. However, loading the lambda class declaration does not guarantee that its call operator (a `CXXMethodDecl`) is loaded as well. As a result, if the lambda call operator is later loaded from a different module, we can end up with a `DeclRefExpr` that refers to a `VarDecl` from a different module — leading to inconsistencies. To fix this, we should ensure the lambda call operator itself is loaded. Fixes #141582
2025-06-02[clang] Rename diag notes that assumed precompiled dependencies are pch's, ↵Cyndy Ishida1-7/+7
NFCI (#142161)
2025-06-02[ASTWriter] Do not write ObjCCategories if empty. (#141841)David Green2-5/+7
This is a fix for a completely unrelated patch, that started to cause failures in the explicit-build.cpp test because the size of the b.pcm and b-not-a.pcm files became the same. The alignment added by empty ObjCCategory blobs being written to the file causes them to become the same size, and the error 'module file has a different size than expected' will not be emitted as the pcms only track module size, not content, for whether they are valid. This prevents that issue by not saving the ObjCCategories if it is empty. The change in clang/lib/Serialization/ASTReaderDecl.cpp is just formatting, but shows that the only use of ObjCCategoriesMap loaded from the file will be OK with null (never loaded) data. It is a bit of a weird fix, but should help decrease the size of the modules for objects that are not used.
2025-06-01[Serialization] Remove unused includes (NFC) (#142300)Kazu Hirata7-28/+0
These are identified by misc-include-cleaner. I've filtered out those that break builds. Also, I'm staying away from llvm-config.h, config.h, and Compiler.h, which likely cause platform- or compiler-specific build failures.
2025-05-29[Clang] Optimize some `getBeginLoc` implementations (#141058)cor3ntin2-0/+7
The bulk of the changes are in `CallExpr` We cache Begin/End source locs in the trailing objects, in the space left by making the offset to the trailing objects static. We also set a flag to indicate that we are calling an explicit object member function, further reducing the cost of getBeginLoc. Fixes #140876
2025-05-29[clang] Serialization: support hashing null template arguments (#141890)Matheus Izvekov1-1/+3
2025-05-28[AArch64] Rename AArch64SVEACLETypes.def and add base SVE_TYPE.David Green2-2/+2
2025-05-27[clang][AST][NFC] fix spelling typos in clang AST files (#141346)Baranov Victor1-1/+1
2025-05-27[HLSL] Implement `SpirvType` and `SpirvOpaqueType` (#134034)Cassandra Beckley2-0/+17
This implements the design proposed by [Representing SpirvType in Clang's Type System](https://github.com/llvm/wg-hlsl/pull/181). It creates `HLSLInlineSpirvType` as a new `Type` subclass, and `__hlsl_spirv_type` as a new builtin type template to create such a type. This new type is lowered to the `spirv.Type` target extension type, as described in [Target Extension Types for Inline SPIR-V and Decorated Types](https://github.com/llvm/wg-hlsl/blob/main/proposals/0017-inline-spirv-and-decorated-types.md).
2025-05-24[Serialization] Remove an unused local variable (NFC) (#141358)Kazu Hirata1-2/+0
Note that getTimestampFilename just constructs a file name, so it's mostly a "pure" function except possible heap allocation.
2025-05-22Reapply "[clang] Remove intrusive reference count from `DiagnosticOptions` ↵Jan Svoboda1-13/+12
(#139584)" This reverts commit e2a885537f11f8d9ced1c80c2c90069ab5adeb1d. Build failures were fixed right away and reverting the original commit without the fixes breaks the build again.
2025-05-22Revert "[clang] Remove intrusive reference count from `DiagnosticOptions` ↵Kazu Hirata1-12/+13
(#139584)" This reverts commit 9e306ad4600c4d3392c194a8be88919ee758425c. Multiple builtbot failures have been reported: https://github.com/llvm/llvm-project/pull/139584
2025-05-22[clang] Remove intrusive reference count from `DiagnosticOptions` (#139584)Jan Svoboda1-13/+12
The `DiagnosticOptions` class is currently intrusively reference-counted, which makes reasoning about its lifetime very difficult in some cases. For example, `CompilerInvocation` owns the `DiagnosticOptions` instance (wrapped in `llvm::IntrusiveRefCntPtr`) and only exposes an accessor returning `DiagnosticOptions &`. One would think this gives `CompilerInvocation` exclusive ownership of the object, but that's not the case: ```c++ void shareOwnership(CompilerInvocation &CI) { llvm::IntrusiveRefCntPtr<DiagnosticOptions> CoOwner = &CI.getDiagnosticOptions(); // ... } ``` This is a perfectly valid pattern that is being actually used in the codebase. I would like to ensure the ownership of `DiagnosticOptions` by `CompilerInvocation` is guaranteed to be exclusive. This can be leveraged for a copy-on-write optimization later on. This PR changes usages of `DiagnosticOptions` across `clang`, `clang-tools-extra` and `lldb` to not be intrusively reference-counted.