aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Sema/SemaInit.cpp
AgeCommit message (Collapse)AuthorFilesLines
2023-12-22[Sema] Fix crash on invalid code with parenthesized aggregate initialization ↵Ilya Biryukov1-0/+8
(#76232) Fixes #76228. Use the same logic as braced init lists, also adds a test that puts incomplete types in various positions to check for regressions in the future.
2023-12-22[Sema] NFC. Simplify code in a few places of TryOrBuildParenListInitializationIlya Biryukov1-13/+6
2023-12-19[clang] Fix CTAD not work for C++ explicit type conversion (functional ↵Haojian Wu1-4/+7
annotation). (#75779) This fixes https://github.com/llvm/llvm-project/issues/64347. The CTAD for an aggregate class is missing to handle the explicit type conversion case, e.g. `TemplateFooClass(1, 2);`. Per C++ expr.type.conv p1, the deduced type is the return type of the deduction guide selected by the CTAD for the reminder. In the deduction implementation `DeduceTemplateSpecializationFromInitializer`, the parenthesized express-list case relies on the `ParenListExpr` parameter (default is nullptr), the AST `ParenListExpr` node is not built for all variant initializer cases (`BuildCXXTypeConstructorExpr`, `BuildCXXNew` etc), thus the deduction doesn't perform for these cases. This patch fixes it by removing the `ParenListExpr` and using the `Inits` instead (which also simplifies the interface and implementation).
2023-12-18Revert "[clang] Fix false positive -Wmissing-field-initializer for anonymous ↵Podchishchaeva, Mariya1-91/+62
unions (#70829)" This reverts commit a01307a6ee788fc6ac2e09e58f0f52e5666def86 and its follow-up fix 32d5221ec4810dd723ccebaabbda1df5d3b4cfcf. It caused unexpected warnings emitted for nested designators in C.
2023-12-15[clang] Fix unexpected warnings after a01307a (#75591)Mariya Podchishchaeva1-0/+11
a01307a broke silencing of -Wmissing-field-initializers warnings in C for nested designators. This fixes the issue.
2023-12-15[clang] Report narrowing conversions with const references (#75332)Mariya Podchishchaeva1-12/+18
Fixes https://github.com/llvm/llvm-project/issues/63151 --------- Co-authored-by: Erich Keane <ekeane@nvidia.com>
2023-12-13[clang] Fix false positive -Wmissing-field-initializer for anonymous unions ↵Mariya Podchishchaeva1-62/+80
(#70829) Normally warning is not reported when a field has default initializer. Do so for anonymous unions with default initializers as well. No release note since it is a regression in clang 18. Fixes https://github.com/llvm/llvm-project/issues/70384
2023-12-08[Clang] Fix ResolveConstructorOverload to not select a conversion function ↵Shafik Yaghmour1-26/+25
if we are going use copy elision ResolveConstructorOverload needs to check properly if we are going to use copy elision we can't use a conversion function. This fixes: https://github.com/llvm/llvm-project/issues/39319 https://github.com/llvm/llvm-project/issues/60182 https://github.com/llvm/llvm-project/issues/62157 https://github.com/llvm/llvm-project/issues/64885 https://github.com/llvm/llvm-project/issues/65568 Differential Revision: https://reviews.llvm.org/D148474
2023-12-06[Clang][Sema] Don't say "is declared here" for invalid template locationsJustin Bogner1-1/+1
If a template is defined via an external AST source, it won't have a location. When we emit warnings about misusing such templates we shouldn't emit a "template is declared here" warning with no location, as that's just confusing. Reviewers: llvm-beanz, erichkeane, AaronBallman Reviewed By: erichkeane, AaronBallman Pull Request: https://github.com/llvm/llvm-project/pull/71264
2023-11-28[clang] fix typo (#73644)Zhikai Zeng1-1/+1
2023-11-22[coroutines] Introduce [[clang::coro_lifetimebound]] (#72851)Utkarsh Saxena1-1/+6
Adds attribute `[[clang::coro_lifetimebound]]`. All arguments to a function are considered to be **lifetime bound** if the function returns a type annotated with ``[[clang::coro_lifetimebound]]`` and ``[[clang::coro_return_type]]``.
2023-11-05[clang][NFC] Refactor `CXXConstructExpr::ConstructionKind`Vlad Serebrennikov1-24/+17
This patch converts `CXXConstructExpr::ConstructionKind` into a scoped enum in namespace scope, making it eligible for forward declaring. This is useful in cases like annotating bit-fields with `preferred_type`.
2023-11-05[clang][NFC] Refactor `StringLiteral::StringKind`Vlad Serebrennikov1-7/+7
This patch converts `StringLiteral::StringKind` to a scoped enum in namespace scope. This enabled forward-declarations of this enum where necessary, e.g. for `preferred_type` annotation for bit-fields.
2023-10-31[clang][NFC] Refactor `VectorType::VectorKind`Vlad Serebrennikov1-2/+2
This patch moves `VectorKind` to namespace scope, and make it complete at the point its bit-field is declared. It also converts it to a scoped enum.
2023-10-31[clang][NFC] Refactor `ArrayType::ArraySizeModifier`Vlad Serebrennikov1-11/+8
This patch moves `ArraySizeModifier` before `Type` declaration so that it's complete at `ArrayTypeBitfields` declaration. It's also converted to scoped enum along the way.
2023-10-16[clang] Use IgnoreParensSingleStep in more placesArthur Eubanks1-30/+7
Addresses a post-commit comment on D146764. Reviewed By: hans Differential Revision: https://reviews.llvm.org/D151479
2023-10-02[C++] Implement "Deducing this" (P0847R7)Corentin Jabot1-3/+4
This patch implements P0847R7 (partially), CWG2561 and CWG2653. Reviewed By: aaron.ballman, #clang-language-wg Differential Revision: https://reviews.llvm.org/D140828
2023-09-30[Sema] add cast from IncompleteArrayType to ConstantArrayType in ↵Congcong Cai1-0/+12
TryReferenceListInitialization (#65918)
2023-09-15[clang] Fix wrong warning about missing init for flexible array members (#66341)Mariya Podchishchaeva1-1/+2
91088978d712cd7b33610c59f69d87d5a39e3113 shouldn't have removed an additional check that field has incomplete array type. Fixes https://github.com/llvm/llvm-project/issues/66300 Co-authored-by: Aaron Ballman <aaron@aaronballman.com>
2023-08-27[clang] Remove redundant control flow statements (NFC)Kazu Hirata1-2/+0
2023-08-24[clang] Properly print unnamed members in diagnosticsPodchishchaeva, Mariya1-1/+2
Use member's type when printing. This also fixes a bug in warning diagnostic for out of order initialization with designated initializers so it points to a valid source location when an anonymous member is being initialized. Fixes https://github.com/llvm/llvm-project/issues/63759 Reviewed By: aaron.ballman, shafik Differential Revision: https://reviews.llvm.org/D158526
2023-08-21[clang] Report missing designated initializers in C++Podchishchaeva, Mariya1-17/+29
Prior to this change clang didn't emit missing-field-initializers warning for designated initializers. The comments say that it is done to match gcc behavior. However, gcc behaves so only for C. For C++ warnings are emitted. Fixes https://github.com/llvm/llvm-project/issues/56628 Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D157879
2023-08-11[C23] Rename C2x -> C23; NFCAaron Ballman1-1/+1
This does the rename for most internal uses of C2x, but does not rename or reword diagnostics (those will be done in a follow-up). I also updated standards references and citations to the final wording in the standard.
2023-08-04[Clang] Implement P2169 A nice placeholder with no nameCorentin Jabot1-17/+19
This is a C++ feature that allows the use of `_` to declare multiple variable of that name in the same scope; these variables can then not be referred to. In addition, while P2169 does not extend to parameter declarations, we stop warning on unused parameters of that name, for consistency. The feature is backported to all C++ language modes. Reviewed By: #clang-language-wg, aaron.ballman Differential Revision: https://reviews.llvm.org/D153536
2023-07-19[Clang] Fix the location of default init expressionsCorentin Jabot1-1/+2
Default member initializations constructed from a parenthesized aggregate initialization should be constructed at the location of the left paren, to be consistent with brace initialization. Otherwise we get diagmostics and source_location in the wrong places. Fixes #63903 Reviewed By: aaron.ballman, #clang-language-wg Differential Revision: https://reviews.llvm.org/D155573
2023-07-14[Clang] Fix crash when emitting diagnostic for out of order designated ↵Shafik Yaghmour1-1/+5
initializers in C++ In C++ we are not allowed to use designated initializers to initialize fields out of order. In some cases when diagnosing this we are crashing because we are not indexing correctly and therefore going out of bounds. This fixes: https://github.com/llvm/llvm-project/issues/63605 Differential Revision: https://reviews.llvm.org/D154675
2023-07-12[NFC] Fix potential dereferencing of nullptrSindhu Chittireddy1-1/+5
Differential Revision: https://reviews.llvm.org/D139148
2023-07-10[Clang][NFC] Fix "initalizer" typoCorentin Jabot1-1/+1
2023-07-07Implement P2361 Unevaluated string literalsCorentin Jabot1-0/+3
This patch proposes to handle in an uniform fashion the parsing of strings that are never evaluated, in asm statement, static assert, attrributes, extern, etc. Unevaluated strings are UTF-8 internally and so currently behave as narrow strings, but these things will diverge with D93031. The big question both for this patch and the P2361 paper is whether we risk breaking code by disallowing encoding prefixes in this context. I hope this patch may allow to gather some data on that. Future work: Improve the rendering of unicode characters, line break and so forth in static-assert messages Reviewed By: aaron.ballman, shafik Differential Revision: https://reviews.llvm.org/D105759
2023-07-05Fix aggregate CTAD with string literals adding extra constMital Ashok1-1/+2
Missing a `withConst`, so when deducing from a string literal, a `const` is erroneously added to the deduced type. Reviewed By: ychen Differential Revision: https://reviews.llvm.org/D154301
2023-07-05[Clang] Fix a crash when trying to initialize an invalid aggregate.Corentin Jabot1-9/+9
We did not return an error when failing to producing a valid expression when performing the initialization of an aggregate initialized with a parenthesized list of expressions. This is a regression introduced in Clang 17. Fixes #63278 Reviewed By: aaron.ballman, #clang-language-wg Differential Revision: https://reviews.llvm.org/D154486
2023-07-01[clang] P1816R0 and P2082R1 should work for cxx20 and after onlyYuanfang Chen1-2/+2
For commit 632dd6a4ca0036009f
2023-06-29[Clang] Implements CTAD for aggregates P1816R0 and P2082R1Yuanfang Chen1-120/+306
Differential Revision: https://reviews.llvm.org/D139837
2023-06-25[llvm] Add missing StringExtras.h includesElliot Goodrich1-0/+1
In preparation for removing the `#include "llvm/ADT/StringExtras.h"` from the header to source file of `llvm/Support/Error.h`, first add in all the missing includes that were previously included transitively through this header.
2023-05-31[clang] Fix crash when passing a braced-init list to a parentehsized ↵Alan Zhao1-1/+1
aggregate init expression The previous code incorrectly assumed that we would never call warnBracedScalarInit(...) with a EK_ParenAggInitMember. This patch fixes the bug by warning when a scalar member is initialized via a braced-init list when performing a parentehsized aggregate initialization. This behavior is consistent with parentehsized list aggregate initialization. Fixes #63008 Reviewed By: shafik Differential Revision: https://reviews.llvm.org/D151763
2023-05-22[clang] Fix crash on attempt to initialize union with flexible array memberMariya Podchishchaeva1-1/+1
Due to missing check on union, there was a null expression added to init list that caused crash later. Fixes https://github.com/llvm/llvm-project/issues/61746 Reviewed By: aaron.ballman, shafik Differential Revision: https://reviews.llvm.org/D150435
2023-05-10Reland [clang] Make predefined expressions string literals under -fms-extensionsArthur Eubanks1-0/+11
MSVC makes these string literals [1][2]. [1] https://godbolt.org/z/6vnTzbExx [2] https://learn.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=msvc-170 Fixes #114 Initial commit didn't check if there was a function name when stepping through expressions ignoring parens. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D146764
2023-05-08[Clang] Update warning on some designator initializer cases involving unionsShafik Yaghmour1-4/+11
Currently when using designated initializers in C++ we have a few extension. Two extension which are dangerous involved assigning to multiple members of union which will likely be a mistake since unions can only have one active member. I have updated to be a warning by default. The second case if when we assign to multiple union members and one of the previous members had a non-trivial destructor, which could lead to leaking resources. This one is now an error by default. Fixes: https://github.com/llvm/llvm-project/issues/62156 Differential Revision: https://reviews.llvm.org/D149694
2023-05-07Revert "[clang] Make predefined expressions string literals under ↵Arthur Eubanks1-11/+0
-fms-extensions" This reverts commit 856f384bf94513c89e754906b7d80fbe5377ab53. Breaks bots, e.g. https://lab.llvm.org/buildbot/#/builders/123/builds/18775
2023-05-07[clang] Make predefined expressions string literals under -fms-extensionsArthur Eubanks1-0/+11
MSVC makes these string literals [1][2]. [1] https://godbolt.org/z/6vnTzbExx [2] https://learn.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=msvc-170 Fixes #114 Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D146764
2023-05-04[clang][Sema][NFC] Move `EnterExpressionEvaluationContext` to its own fileDavid Stone1-0/+1
Sema.h is huge. This makes a small reduction to it by moving EnterExpressionEvaluationContext into a new header, since it is an independent component. Differential Revision: https://reviews.llvm.org/D149796
2023-05-03Revert "[clang] Reject flexible array member in a union in C++"Mariya Podchishchaeva1-1/+1
This reverts commit 22e2db6010b029ebd4c6d3d1fd30224d8b3109ef. Broke buildbots on Windows. It seems standard headers on Windows contain flexible array members in unions
2023-05-03[clang] Reject flexible array member in a union in C++Mariya Podchishchaeva1-1/+1
It was rejected in C, and in a strange way accepted in C++. However, the support was never properly tested and fully implemented, so just reject it in C++ mode as well. This change also fixes crash on attempt to initialize union with flexible array member. Due to missing check on union, there was a null expression added to init list that caused crash later. Fixes https://github.com/llvm/llvm-project/issues/61746 Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D147626
2023-05-01[clang] Fix overly aggressive lifetime checks for parenthesized aggregate ↵Alan Zhao1-133/+200
initialization Before this patch, initialized class members would have the LifetimeKind LK_MemInitializer, which does not allow for binding a temporary to a reference. Binding to a temporary however is allowed in parenthesized aggregate initialization, even if it leads to a dangling reference. To fix this, we create a new EntityKind, EK_ParenAggInitMember, which has LifetimeKind LK_FullExpression. This patch does *not* attempt to diagnose dangling references as a result of using this feature. This patch also refactors TryOrBuildParenListInitialization(...) to accomodate creating different InitializedEntity objects. Fixes #61567 [0]: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0960r3.html Reviewed By: shafik Differential Revision: https://reviews.llvm.org/D148274
2023-05-01[clang] Fix default initializers being ignored when initializing templated ↵Alan Zhao1-7/+9
aggregate types Previously, when checking whether an in-class initializer exists when performing parenthesized aggregate initialization, Clang checks that the output of FieldDecl::getInClassInitializer() is non-null. This is incorrect; if the field is part of a templated type, then getInClassInitializer() will return nullptr if we haven't called Sem::BuildCXXDefaultInitExpr(...) before, even if FieldDecl::hasInClassInitializer() returns true. The end result is that Clang incorrectly ignores the in class initializer and value-initializes the field. The fix therefore is to instead call FieldDecl::hasInClassInitializer(), which is what we do for braced init lists [0]. Before this patch, Clang does correctly recognize the in-class field initializer in certain cases. This is Sema::BuildCXXDefaultInitExpr(...) populates the in class initializer of the corresponding FieldDecl object. Therefore, if that method was previously called with the same FieldDecl object, as can happen with a decltype(...) or a braced list initialization, FieldDecl::getInClassInitializer() will return a non-null expression, and the field becomes properly initialized. Fixes 62266 [0]: https://github.com/llvm/llvm-project/blob/be5f35e24f4c15caf3c4aeccddc54c52560c28a0/clang/lib/Sema/SemaInit.cpp#L685 Reviewed By: shafik Differential Revision: https://reviews.llvm.org/D149389
2023-04-27[clang] Fix a crash with parenthesized aggregate initialization and base classesAlan Zhao1-2/+3
When calling InitializeBase(...), TryOrBuidlParenListInit(...) needs to pass in the parent entity; otherwise, we erroneously try to cast CurContext to a CXXConstructorDecl[0], which can't be done since we're performing aggregate initialization, not constructor initialization. Field initialization is not affected, but this patch still adds some tests for it. Fixes 62296 [0]: https://github.com/llvm/llvm-project/blob/33d6bd1c667456f7f4a9d338a7996a30a3af50a3/clang/lib/Sema/SemaAccess.cpp#L1696 Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D149301
2023-04-26Pass the found declaration to DiagnoseUseOfDecl.Richard Smith1-4/+2
Don't pass in the resolved declaration, because that might be an inheriting constructor declaration, which should never be used directly and for which constraint satisfaction checking doesn't work. Fixes #62361.
2023-04-18[Clang][NFC] Rename methods/vars to reflect their real usageBill Wendling1-4/+4
The "getField" method is a bit confusing considering we also have a "getFieldName" method. Instead, use "getFieldDecl" rather than "getField". Differential Revision: https://reviews.llvm.org/D147743
2023-04-07[Clang] Improve designated inits diagnostic locationBill Wendling1-2/+9
A "null" designator won't have a valid location. Try to approximate this location as best we can in that situation. Closes 61118 Closes 46132 Reviewed By: shafik Differential Revision: https://reviews.llvm.org/D147673
2023-04-06Reject attempts to initialize non-aggregate types from a designatedRichard Smith1-2/+30
initializer list. This previously led to some weird behaviors where we would unwrap the initializer list expression and then try to use the DesignatedInitExprs as constructor arguments. Under the C++20 language rules, it's not valid to initialize a reference-to-aggregate from a designated initializer list, but we have historically accepted that, as do other compilers, and we continue to accept that with this change. I've asked WG21 whether this should be considered a wording defect.