aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Sema/SemaChecking.cpp
AgeCommit message (Collapse)AuthorFilesLines
2025-01-22[Clang] Re-write codegen for atomic_test_and_set and atomic_clear (#121943)Oliver Stannard1-15/+50
Re-write the sema and codegen for the atomic_test_and_set and atomic_clear builtin functions to go via AtomicExpr, like the other atomic builtins do. This simplifies the code, because AtomicExpr already handles things like generating code for to dynamically select the memory ordering, which was duplicated for these builtins. This also fixes a few crash bugs, one when passing an integer to the pointer argument, and one when using an array. This also adds diagnostics for the memory orderings which are not valid for atomic_clear according to https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html, which were missing before. Fixes https://github.com/llvm/llvm-project/issues/111293. This is a re-land of #120449, modified to allow any non-const pointer type for the first argument.
2025-01-20[Clang] Fix warning for non std functions with name `infinity` (#123417)Amr Hesham1-15/+32
Fix reporting diagnostic for non std functions that has the name `infinity` Fixes: #123231
2025-01-11Reapply "[clang] Avoid re-evaluating field bitwidth" (#122289)Timm Baeder1-5/+5
2025-01-08Revert "[clang] Avoid re-evaluating field bitwidth (#117732)"Timm Bäder1-5/+5
This reverts commit 81fc3add1e627c23b7270fe2739cdacc09063e54. This breaks some LLDB tests, e.g. SymbolFile/DWARF/x86/no_unique_address-with-bitfields.cpp: lldb: ../llvm-project/clang/lib/AST/Decl.cpp:4604: unsigned int clang::FieldDecl::getBitWidthValue() const: Assertion `isa<ConstantExpr>(getBitWidth())' failed.
2025-01-08[clang] Avoid re-evaluating field bitwidth (#117732)Timm Baeder1-5/+5
Save the bitwidth value as a `ConstantExpr` with the value set. Remove the `ASTContext` parameter from `getBitWidthValue()`, so the latter simply returns the value from the `ConstantExpr` instead of constant-evaluating the bitwidth expression every time it is called.
2025-01-06[SPIRV] Add Target Builtins using Distance ext as an example (#121598)Farzon Lotfi1-0/+3
- Update pr labeler so new SPIRV files get properly labeled. - Add distance target builtin to BuiltinsSPIRV.td. - Update TargetBuiltins.h to account for spirv builtins. - Update clang basic CMakeLists.txt to build spirv builtin tablegen. - Hook up sema for SPIRV in Sema.h|cpp, SemaSPIRV.h|cpp, and SemaChecking.cpp. - Hookup sprv target builtins to SPIR.h|SPIR.cpp target. - Update GBuiltin.cpp to emit spirv intrinsics when we get the expected spirv target builtin. Consensus was reach in this RFC to add both target builtins and pattern matching: https://discourse.llvm.org/t/rfc-add-targetbuiltins-for-spirv-to-support-hlsl/83329. pattern matching will come in a separate pr this one just sets up the groundwork to do target builtins for spirv. partially resolves [#99107](https://github.com/llvm/llvm-project/issues/99107)
2024-12-23[Clang][Sema] Process warnings conditionally (#120591)Dmitry Chestnykh1-12/+18
There are a few functions that emit warnings related to positional arguments in format strings. These functions use `getLocationOfByte()` which has O(n) complexity and may lead to silent hang of compilation in some cases. But such warnings is not widely used and actually don't emit if user didn't pass the appropriate `-W...` flag, so if the flag is not passed dont make the call to `EmitFormatDiagnostic` for such diags. Fix #120462
2024-12-20Revert "[Clang] Re-write codegen for atomic_test_and_set and atomic_clear ↵Mikhail Goncharov1-28/+7
(#120449)" This reverts commit 9fc2fadbfcb34df5f72bdaed28a7874bf584eed7. See https://github.com/llvm/llvm-project/pull/120449#issuecomment-2556089016
2024-12-19[clang][Sema] Enable the kprintf format attribute (#100669)Brad Smith1-3/+4
2024-12-19[Clang] Fix crash in __builtin_assume_aligned (#114217)Oliver Stannard1-2/+4
The CodeGen for __builtin_assume_aligned assumes that the first argument is a pointer, so crashes if the int-conversion error is downgraded or disabled. Emit a non-downgradable error if the argument is not a pointer, like we currently do for __builtin_launder. Fixes #110914.
2024-12-19[Clang] Re-write codegen for atomic_test_and_set and atomic_clear (#120449)Oliver Stannard1-7/+28
Re-write the sema and codegen for the atomic_test_and_set and atomic_clear builtin functions to go via AtomicExpr, like the other atomic builtins do. This simplifies the code, because AtomicExpr already handles things like generating code for to dynamically select the memory ordering, which was duplicated for these builtins. This also fixes a few crash bugs, one when passing an integer to the pointer argument, and one when using an array. This also adds diagnostics for the memory orderings which are not valid for atomic_clear according to https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html, which were missing before. Fixes #111293.
2024-11-28[clang] Improve the lifetime_capture_by diagnostic on the constructor. (#117792)Haojian Wu1-0/+6
With this change, the lifetime_capture_by code path will not handle the constructor decl to avoid bogus diagnostics (see the testcase). Instead, we reuse the lifetimebound code as the lifetime_capture_by(this) has the same semantic as lifetimebound in constructor. The downside is that the lifetimebound diagnostic is reused for the capture case (I think it is not a big issue). Fixes #117680
2024-11-27[Clang] Improve Sema diagnostic performance for __builtin_counted_by_ref ↵Bill Wendling1-0/+39
(#116719) Implement the sema checks with a placeholder. We then check for that placeholder in all of the places we care to emit a diagnostic. Fixes: #115520
2024-11-22[clang] Warn const integer-overflow of member in temporary struct bound to ↵Youngsuk Kim1-1/+2
rvalue reference (#117225) Fixes #46755 --------- Co-authored-by: Sirraide <aeternalmail@gmail.com>
2024-11-20[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)Utkarsh Saxena1-1/+44
This PR uses the existing lifetime analysis for the `capture_by` attribute. The analysis is behind `-Wdangling-capture` warning and is disabled by default for now. Once it is found to be stable, it will be default enabled. Planned followup: - add implicit inference of this attribute on STL container methods like `std::vector::push_back`. - (consider) warning if capturing `X` cannot capture anything. It should be a reference, pointer or a view type. - refactoring temporary visitors and other related handlers. - start discussing `__global` vs `global` in the annotation in a separate PR. --------- Co-authored-by: Boaz Brickner <brickner@google.com>
2024-11-16[Sema] Remove unused includes (NFC) (#116461)Kazu Hirata1-7/+0
Identified with misc-include-cleaner.
2024-11-07[Clang] Add __builtin_counted_by_ref builtin (#114495)Bill Wendling1-0/+53
The __builtin_counted_by_ref builtin is used on a flexible array pointer and returns a pointer to the "counted_by" attribute's COUNT argument, which is a field in the same non-anonymous struct as the flexible array member. This is useful for automatically setting the count field without needing the programmer's intervention. Otherwise it's possible to get this anti-pattern: ptr = alloc(<ty>, ..., COUNT); ptr->FAM[9] = 42; /* <<< Sanitizer will complain */ ptr->count = COUNT; To prevent this anti-pattern, the user can create an allocator that automatically performs the assignment: #define alloc(TY, FAM, COUNT) ({ \ TY __p = alloc(get_size(TY, COUNT)); \ if (__builtin_counted_by_ref(__p->FAM)) \ *__builtin_counted_by_ref(__p->FAM) = COUNT; \ __p; \ }) The builtin's behavior is heavily dependent upon the "counted_by" attribute existing. It's main utility is during allocation to avoid the above anti-pattern. If the flexible array member doesn't have that attribute, the builtin becomes a no-op. Therefore, if the flexible array member has a "count" field not referenced by "counted_by", it must be set explicitly after the allocation as this builtin will return a "nullptr" and the assignment will most likely be elided. --------- Co-authored-by: Bill Wendling <isanbard@gmail.com> Co-authored-by: Aaron Ballman <aaron@aaronballman.com>
2024-11-04[PowerPC] Support set_flt_rounds builtin (#73750)Qiu Chaofan1-1/+3
2024-10-30[clang] Fix 71315698c9 in presence of incomplete types (#114095)serge-sans-paille1-3/+8
Incomplete types are not considered trivially copyable by clang but we don't want to warn about invalid argument for memcpy / memset in that case because we cannot prove they are not Trivially Copyable.
2024-10-29[Clang] fix range calculation for conditionals with throw expressions (#112081)Oleksandr T.1-85/+131
Fixes #111854 --- The issue arises when `GetExprRange` encounters a `ConditionalOperator` with a `CXXThrowExpr` ```md ConditionalOperator 0x1108658e0 'int' |-CXXBoolLiteralExpr 0x110865878 '_Bool' true |-CXXThrowExpr 0x1108658a8 'void' | `-IntegerLiteral 0x110865888 'int' 0 `-IntegerLiteral 0x1108658c0 'int' 0 ``` https://github.com/llvm/llvm-project/blob/ed3d05178274890fb804f43ae1bcdfd33b5fd8f0/clang/lib/Sema/SemaChecking.cpp#L9628-L9631 The current behavior causes the `GetExprRange` to proceed with the throw expression (`CO->getTrueExpr()`/`void` type)
2024-10-28[clang] Warn about memset/memcpy to NonTriviallyCopyable types (#111434)serge-sans-paille1-0/+18
This implements a warning that's similar to what GCC does in that context: both memcpy and memset require their first and second operand to be trivially copyable, let's warn if that's not the case.
2024-10-24[clang] Use {} instead of std::nullopt to initialize empty ArrayRef (#109399)Jay Foad1-2/+2
Follow up to #109133.
2024-10-01[Clang] Add __builtin_(elementwise|reduce)_(max|min)imum (#110198)Francis Visoiu Mistrih1-13/+36
We have the LLVM intrinsics, and we're missing the clang builtins to be used directly in code that needs to make the distinction in NaN semantics.
2024-10-01[HLSL][clang] Add elementwise builtin for atan2 (p3) (#110187)Tex Riddell1-0/+1
This change is part of this proposal: https://discourse.llvm.org/t/rfc-all-the-math-intrinsics/78294 - Add HLSL frontend for atan2 - Add clang Builtin, map to new llvm.atan2 - SemaChecking restrict to floating point and 2 args - SemaHLSL restrict to float or half. - Add to clang ReleaseNotes.rst and LanguageExtensions.rst - Add half-float-only-errors2.hlsl for 2 arg intrinsics, and update half-float-only-errors.hlsl with scalar case for consistency - Remove fmod-errors.hlsl and pow-errors.hlsl now covered in half-float-only-errors2.hlsl Part 3 for Implement the atan2 HLSL Function #70096.
2024-09-27[HLSL] Implementation of the elementwise fmod builtin (#108849)Zhengxing li1-0/+1
This change add the elementwise fmod builtin to support HLSL function 'fmod' in clang for #99118 Builtins.td - add the fmod builtin CGBuiltin.cpp - lower the builtin to llvm FRem instruction hlsl_intrinsics.h - add the fmod api SemaChecking.cpp - add type checks for builtin SemaHLSL.cpp - add HLSL type checks for builtin clang/docs/LanguageExtensions.rst - add the builtin in *Elementwise Builtins* clang/docs/ReleaseNotes.rst - announce the builtin
2024-09-19[clang] Don't call raw_string_ostream::flush() (NFC)Youngsuk Kim1-1/+0
Don't call raw_string_ostream::flush(), which is essentially a no-op. As specified in the docs, raw_string_ostream is always unbuffered
2024-09-18[HLSL] Implement elementwise popcount (#108121)Sarah Spall1-1/+1
Add new elementwise popcount builtin to support HLSL function 'countbits'. elementwise popcount only accepts integer types. Add hlsl intrinsic 'countbits' Closes #99094
2024-09-05Fix handling of FP-classify where the last arg fails to converterichkeane1-4/+13
The last argument of an FP-classify function was checked for vailidity as an expression, but we never ensured that the usual unary conversions/etc properly resulted in a valid value. Thus, when we got the value, it was null, so we had a null dereference. This patch instead fails out/marks the function call as invalid if the argument is incorrect. I DID consider just allowing it to continue, but the result was an extraneous error about how the last argument wasn't a float (in this case, it was an overload set). Fixes: #107411
2024-09-05[Clang] Add __builtin_is_within_lifetime to implement P2641R4's ↵Mital Ashok1-0/+40
std::is_within_lifetime (#91895) [P2641R4](https://wg21.link/P2641R4) This new builtin function is declared `consteval`. Support for `-fexperimental-new-constant-interpreter` will be added in a later patch. --------- Co-authored-by: cor3ntin <corentinjabot@gmail.com>
2024-08-31[HLSL] Implement output parameter (#101083)Chris B1-0/+12
HLSL output parameters are denoted with the `inout` and `out` keywords in the function declaration. When an argument to an output parameter is constructed a temporary value is constructed for the argument. For `inout` pamameters the argument is initialized via copy-initialization from the argument lvalue expression to the parameter type. For `out` parameters the argument is not initialized before the call. In both cases on return of the function the temporary value is written back to the argument lvalue expression through an implicit assignment binary operator with casting as required. This change introduces a new HLSLOutArgExpr ast node which represents the output argument behavior. The OutArgExpr has three defined children: - An OpaqueValueExpr of the argument lvalue expression. - An OpaqueValueExpr of the copy-initialized parameter. - A BinaryOpExpr assigning the first with the value of the second. Fixes #87526 --------- Co-authored-by: Damyan Pepper <damyanp@microsoft.com> Co-authored-by: John McCall <rjmccall@gmail.com>
2024-08-29[NFC][Sema] Move `Sema::AssignmentAction` into its own scoped enum (#106453)Dan Liew1-1/+2
The primary motivation behind this is to allow the enum type to be referred to earlier in the Sema.h file which is needed for #106321. It was requested in #106321 that a scoped enum be used (rather than moving the enum declaration earlier in the Sema class declaration). Unfortunately doing this creates a lot of churn as all use sites of the enum constants had to be changed. Appologies to all downstream forks in advanced. Note the AA_ prefix has been dropped from the enum value names as they are now redundant.
2024-08-02[NFC][Clang] Remove unused arg (#101650)Dmitry Chestnykh1-4/+4
`BuiltinID` is not used inside `CheckBuiltinTargetInSupported`
2024-08-01[Clang] Fix definition of layout-compatible to ignore empty classes (#92103)Mital Ashok1-50/+24
Also changes the behaviour of `__builtin_is_layout_compatible` None of the historic nor the current definition of layout-compatible classes mention anything about base classes (other than implicitly through being standard-layout) and are defined in terms of members, not direct members.
2024-07-26[Clang] Use private address space for builtin_alloca return type for OpenCL ↵Vikash Gupta1-0/+15
(#95750) The __builtin_alloca was returning a flat pointer with no address space when compiled using openCL1.2 or below but worked fine with openCL2.0 and above. This accounts to the fact that later uses the concept of generic address space which supports cast to other address space(i.e to private address space which is used for stack allocation) . But, in actuality, as it returns pointer to the stack, it should be pointing to private address space irrespective of openCL version becuase builtin_alloca allocates stack memory used for current function in which it is called. Thus,it requires redefintion of the builtin function with appropraite return pointer to private address space.
2024-07-25[clang][Sema] Add support for OpenBSD's syslog format attribute (#97366)Brad Smith1-2/+3
2024-07-25[Sema] Fix a warningKazu Hirata1-2/+1
This patch fixes: clang/lib/Sema/SemaChecking.cpp:8220:3: error: default label in switch which covers all enumeration values [-Werror,-Wcovered-switch-default]
2024-07-25[CLANG] Emit warning in finite math mode when INF and NAN are used. (#99672)Zahira Ammarguellat1-9/+36
In finite math mode when special math builtins `__builtin_inf` and `__builtin_nan` are used a warning is emitted when the builtin is expanded and at call point. This warning at call point was missing for` __builtin_inf` and this patch fixes the issue (https://github.com/llvm/llvm-project/issues/98018).
2024-07-24[PAC] Define __builtin_ptrauth_type_discriminator (#100204)Akira Hatanaka1-3/+7
The builtin computes the discriminator for a type, which can be used to sign/authenticate function pointers and member function pointers. If the type passed to the builtin is a C++ member function pointer type, the result is the discriminator used to signed member function pointers of that type. If the type is a function, function pointer, or function reference type, the result is the discriminator used to sign functions of that type. It is ill-formed to use this builtin with any other type. A call to this function is an integer constant expression. Co-Authored-By: John McCall rjmccall@apple.com
2024-07-01[clang][NFC] Move documentation of `Sema` functions into `Sema.h`Vlad Serebrennikov1-136/+0
This patch moves documentation of `Sema` functions from `.cpp` files to `Sema.h` when there was no documentation in the latter, or it can be trivially subsumed. More complicated cases when there's less trivial divergence between documentation attached to declaration and the one attached to implementation are left for a later PR that would require review. It appears that doxygen can find the documentation for a function defined out-of-line even if it's attached to an implementation, and not declaration. But other tools, e.g. clangd, are not as powerful. So this patch significantly improves autocompletion experience for (at least) clangd-based IDEs.
2024-06-30[clang][Sema] Fix crash on atomic builtins with incomplete type args (#96374)Takuya Shimizu1-0/+3
This patch fixes the crash when pointers to incomplete type are passed to atomic builtins such as `__atomic_load`. `ASTContext::getTypeInfoInChars` assumes that the argument type is a complete type, so I added a check to eliminate cases where incomplete types gets passed to this function Relevant PR: https://github.com/llvm/llvm-project/pull/91057 Fixes https://github.com/llvm/llvm-project/issues/96289
2024-06-27[clang][NFC] Move more things out of `SemaChecking.cpp` (#96641)Vlad Serebrennikov1-1093/+157
This patch moves some functions out of `SemaChecking.cpp`. ObjC-, HLSL-, OpenCL-related functions are affected. This patch continues the effort of splitting `Sema` into parts. Additional context can be found in https://github.com/llvm/llvm-project/pull/84184 and https://github.com/llvm/llvm-project/pull/92682.
2024-06-25Add support for __builtin_verbose_trap (#79230)Akira Hatanaka1-0/+32
The builtin causes the program to stop its execution abnormally and shows a human-readable description of the reason for the termination when a debugger is attached or in a symbolicated crash log. The motivation for the builtin is explained in the following RFC: https://discourse.llvm.org/t/rfc-adding-builtin-verbose-trap-string-literal/75845 clang's CodeGen lowers the builtin to `llvm.trap` and emits debugging information that represents an artificial inline frame whose name encodes the category and reason strings passed to the builtin.
2024-06-22[HLSL][clang] Add elementwise builtins for trig intrinsics (#95999)Farzon Lotfi1-0/+12
This change is part of this proposal: https://discourse.llvm.org/t/rfc-all-the-math-intrinsics/78294 This is part 3 of 4 PRs. It sets the ground work for using the intrinsics in HLSL. Add HLSL frontend apis for `acos`, `asin`, `atan`, `cosh`, `sinh`, and `tanh` https://github.com/llvm/llvm-project/issues/70079 https://github.com/llvm/llvm-project/issues/70080 https://github.com/llvm/llvm-project/issues/70081 https://github.com/llvm/llvm-project/issues/70083 https://github.com/llvm/llvm-project/issues/70084 https://github.com/llvm/llvm-project/issues/95966
2024-06-20[clang] Define ptrauth_sign_constant builtin. (#93904)Ahmed Bougacha1-15/+108
This is a constant-expression equivalent to ptrauth_sign_unauthenticated. Its constant nature lets us guarantee a non-attackable sequence is generated, unlike ptrauth_sign_unauthenticated which we generally discourage using. It being a constant also allows its usage in global initializers, though requiring constant pointers and discriminators. The value must be a constant expression of pointer type which evaluates to a non-null pointer. The key must be a constant expression of type ptrauth_key. The extra data must be a constant expression of pointer or integer type; if an integer, it will be coerced to ptrauth_extra_data_t. The result will have the same type as the original value. This can be used in constant expressions. Co-authored-by: John McCall <rjmccall@apple.com>
2024-06-20[clang] Define ptrauth_string_discriminator builtin. (#93903)Ahmed Bougacha1-0/+20
This exposes the ABI-stable hash function that allows computing a 16-bit discriminator from a constant string. This allows manually matching the implicit string discriminators computed in the ABI (e.g., from mangled names for vtable pointer/entry signing), as well as enabling the use of interesting discriminators when manually annotating specific pointers with the __ptrauth qualifier. The argument must be a string literal of char character type. The result has type ptrauth_extra_data_t. The result value is never zero and always within range for both the __ptrauth qualifier and ptrauth_blend_discriminator. This can be used in constant expressions. Co-authored-by: John McCall <rjmccall@apple.com>
2024-06-11[clang] Replace X && isa<Y>(X) with isa_and_nonnull<Y>(X). NFC (#94987)Pavel Samolysov1-5/+5
This addresses a clang-tidy suggestion.
2024-05-30[clang] Introduce target-specific `Sema` components (#93179)Vlad Serebrennikov1-2991/+27
This patch introduces `SemaAMDGPU`, `SemaARM`, `SemaBPF`, `SemaHexagon`, `SemaLoongArch`, `SemaMIPS`, `SemaNVPTX`, `SemaPPC`, `SemaSystemZ`, `SemaWasm`. This continues previous efforts to split Sema up. Additional context can be found in #84184 and #92682. I decided to bundle target-specific components together because of their low impact on `Sema`. That said, their impact on `SemaChecking.cpp` is far from low, and I consider it a success. Somewhat accidentally, I also moved Wasm- and AMDGPU-specific function from `SemaDeclAttr.cpp`, because they were exposed in `Sema`. That went well, and I consider it a success, too. I'd like to move the rest of static target-specific functions out of `SemaDeclAttr.cpp` like we're doing with built-ins in `SemaChecking.cpp` .
2024-05-30[Clang][AArch64] NFC: Simplify checkArmStreamingBuiltin.Sander de Smalen1-12/+6
Changing this into if -> else if -> else if > else is NFC, because the values of FnType are mutually exclusive.
2024-05-27[Clang] Issue an error when an atomic builtin is called with a pointer to a ↵Hendrik Hübner1-5/+12
zero-size object (#91057) When an atomic builtin is called with a pointer to an object of size zero, an arithmetic exception gets thrown because there is a modulo operation with the objects size in codegen. Diagnose this in sema instead. Fixes #90330.
2024-05-23[AMDGPU][Clang] Add check of size for __builtin_amdgcn_global_load_lds (#93064)Shilei Tian1-0/+22