390 Commits

Author SHA1 Message Date
Timm Baeder
51295d6d56
[clang][bytecode] Reject assignments in C (#136126)
Similar to what the current interpreter does.
2025-04-17 13:39:27 +02:00
Timm Baeder
90ddb54440
[clang][bytecode] Enter a non-constant context when revisiting (#136104)
Otherwise, things like __builtin_is_constant_evaluated() return the
wrong value.
2025-04-17 12:50:28 +02:00
Timm Baeder
adba24aa3c
[clang][bytecode] Add missing __builtin_memcpy checks (#135975)
Add a test for type punning and tests and the necessary checks for
non-trivially-copyable types and incomplete types.
2025-04-17 06:19:37 +02:00
Timm Baeder
ab7e0c0fc0
[clang][bytecode] Implement __builtin_wmem{cpy,move} (#135969) 2025-04-16 17:08:10 +02:00
Timm Baeder
38ca73db22
[clang][bytecode] Give typeinfo APValues an LValuePath (#135948)
That's what the current interpreter does as well.
2025-04-16 13:37:03 +02:00
Timm Baeder
2d63faead4
[clang][bytecode][NFC] Remove PT_FnPtr (#135947)
We don't need this anymore since we don't return it from classify()
anymore.
2025-04-16 13:21:25 +02:00
Timm Baeder
559df834df
[clang][bytecode] Fix subtracting zero-sized pointers (#135929)
Add the appropriate diagnostic and fix the d-d case.
2025-04-16 10:48:42 +02:00
Timm Baeder
05eafd9f2b
[clang][bytecode] Explicitly mark constexpr-unknown variables as such (#135806)
Instead of trying to figure out what's constexpr-unknown later on.
2025-04-16 09:00:52 +02:00
Timm Baeder
974bda8f61
[clang][bytecode] Reject constexpr-unknown pointers from Inc ops (#135548)
We used to accept c++ as a known value here, causing wrong codegen.
2025-04-13 18:57:55 +02:00
Timm Baeder
578ca5e469
[clang][bytecode] Print jump lines in Function::dump() (#135482)
E.g. for
```c++
constexpr int foo(int b) {
  int a = 1+1;

  for (int i = 0; i < b; ++i) {
    ++a;
  }
  return a;
}
```

we now print:
```
foo 0x7cc8d4bf0580
frame size: 128
arg size:   8
rvo:        0
this arg:   0
0      InitScope         0
16     ConstSint32       1
32     ConstSint32       1
48     AddSint32
56     SetLocalSint32    40
72     ConstSint32       0
88     SetLocalSint32    104
104    GetPtrLocal       104         <-+
120    LoadPopSint32                   |
128    GetPtrParam       0             |
144    LoadPopSint32                   |
152    LTSint32                        |
160    Jf                80     --+    |
176    GetPtrLocal       40       |    |
192    IncPopSint32      1        |    |
208    GetPtrLocal       104      |    |
224    IncPopSint32      1        |    |
240    Jmp               -152     |  --+
256    GetPtrLocal       40     <-+
272    LoadPopSint32
280    Destroy           0
296    RetSint32
304    Destroy           0
320    NoRet
```
2025-04-13 15:46:01 +02:00
Timm Baeder
09588e93bb
[clang][bytecode] Fix an inconsistency with loop condition jumps (#135530)
When emitting the jump for e.g. a for loop condition, we used to jump
out of the CondScope, leaving the scope initialized, because we skipped
the corresponding Destroy opcode. If that loop was in a loop itself,
that outer loop could then iterate once more, leading to us initializing
a scope that was still initialized.
Fix this by also destroying the scope after the EndLabel.
2025-04-13 12:25:29 +02:00
Timm Baeder
fafeaab6d9
[clang][bytecode] Misc TypeidPointer fixes (#135322)
Fix comparing type id pointers, add mor info when print()ing them, use
the most derived type in GetTypeidPtr() and the canonically unqualified
type when we know the type statically.
2025-04-11 10:35:28 +02:00
Oliver Hunt
1cd59264aa
[RFC] Initial implementation of P2719 (#113510)
This is a basic implementation of P2719: "Type-aware allocation and
deallocation functions" described at http://wg21.link/P2719

The proposal includes some more details but the basic change in
functionality is the addition of support for an additional implicit
parameter in operators `new` and `delete` to act as a type tag. Tag is
of type `std::type_identity<T>` where T is the concrete type being
allocated. So for example, a custom type specific allocator for `int`
say can be provided by the declaration of

  void *operator new(std::type_identity<int>, size_t, std::align_val_t);
  void  operator delete(std::type_identity<int>, void*, size_t, std::align_val_t);

However this becomes more powerful by specifying templated declarations,
for example

template <typename T> void *operator new(std::type_identity<T>, size_t, std::align_val_t);
template <typename T> void operator delete(std::type_identity<T>, void*, size_t, std::align_val_t););

Where the operators being resolved will be the concrete type being
operated over (NB. A completely unconstrained global definition as above
is not recommended as it triggers many problems similar to a general
override of the global operators).

These type aware operators can be declared as either free functions or
in class, and can be specified with or without the other implicit
parameters, with overload resolution performed according to the existing
standard parameter prioritisation, only with type parameterised
operators having higher precedence than non-type aware operators. The
only exception is destroying_delete which for reasons discussed in the
paper we do not support type-aware variants by default.
2025-04-10 17:13:10 -07:00
Timm Baeder
02f923f8e4
[clang][bytecode] Classify function pointers as PT_Ptr (#135026)
The Pointer class already has the capability to be a function pointer,
but we still classifed function pointers as PT_FnPtr/FunctionPointer.
This means when converting from a Pointer to a FunctionPointer, we lost
the information of what the original Pointer pointed to.
2025-04-10 06:40:54 +02:00
Timm Baeder
98ea512f72
[clang][bytecode] Clear inactive union fields when copying (#134982)
When copying unions, we need to only copy the active field of the source
union, which we were already doing. However, we also need to zero out
the (now) inactive fields, so we don't end up with dangling pointers in
those inactive fields.
2025-04-10 06:12:00 +02:00
Timm Baeder
78c86b38b0
[clang][bytecode][NFC] Avoid implicit integer conversion (#134983)
See discussion in https://github.com/llvm/llvm-project/pull/134672
2025-04-09 13:44:24 +02:00
Timm Baeder
65cede26a6
[clang][bytecode] Fix emitting dtors of zero-sized arrays (#134672)
Desc->getNumElems() returning 0 made us underflow here.
2025-04-08 06:09:21 +02:00
Timm Baeder
fb9915a391
[clang][bytecode] Fix emitDestruction() for dummy descriptors (#134665)
This might happen if the referenced declaration is invalid and thus gets
a dummy descriptor. We ran into an assertion later on.
2025-04-08 06:00:35 +02:00
Timm Baeder
bdd087023f
[clang][bytecode] Fix various issues with multidimensional arrays (#134628)
This issue is very convoluted, but in essence, in the new version:

For a Pointer P that points to the root of a multidimensional, primitive
array:

`P.narrow()` does nothing.
`P.atIndex(0)` points `P[0]`
`P.atIndex(0).atIndex(0)` is the same as `P.atIndex(0)` (as before)
`P.atIndex(0).narrow().atIndex(0)` points to `P[0][0]`
`P.atIndex(0).narrow().narrow()` is the same as `P.atIndex(0).narrow()`.
2025-04-08 05:48:55 +02:00
Timm Baeder
7267dbfe10
[clang][bytecode] Fix comparing the addresses of union members (#133852)
Union members get the same address, so we can't just use
`Pointer::getByteOffset()`.
2025-04-01 09:00:46 +02:00
Timm Baeder
a0e1e680d2
[clang][bytecode] Return Invalid() on non-constexpr builtins (#133700)
So the diagnostic output matches with the current interpreter
2025-03-31 18:53:12 +02:00
Timm Baeder
11dd7d98a6
[clang][bytecode] Reject constexpr-unknown values from comparisons (#133701) 2025-03-31 18:53:01 +02:00
Aaron Ballman
00c43ae235
[C2y] Implement WG14 N3369 and N3469 (_Countof) (#133125)
C2y adds the `_Countof` operator which returns the number of elements in
an array. As with `sizeof`, `_Countof` either accepts a parenthesized
type name or an expression. Its operand must be (of) an array type. When
passed a constant-size array operand, the operator is a constant
expression which is valid for use as an integer constant expression.

This is being exposed as an extension in earlier C language modes, but
not in C++. C++ already has `std::extent` and `std::size` to cover these
needs, so the operator doesn't seem to get the user enough benefit to
warrant carrying this as an extension.

Fixes #102836
2025-03-27 13:23:16 -04:00
Timm Baeder
cb7b10c66e
[clang][bytecode] Fail on mutable reads from revisited variables (#133064)
When revisiting a variable, we do that by simply calling visitDecl() for
it, which means it will end up with the same EvalID as the rest of the
evaluation - but this way we end up allowing reads from mutable
variables. Disallow that.
2025-03-26 12:29:31 +01:00
Timm Baeder
6ff3906936
[clang][bytecode] Print more info in Block::dump() (#133062) 2025-03-26 12:25:14 +01:00
Timm Baeder
0bc2c5b2a4
Reapply "[clang][bytecode] Implement __builtin_{wcscmp,wcsncmp} (#132… (#132963)
…723)"

This reverts commit 1e2ad6793ac205607e7c809283cf69e1cc36a69a.


Fix the previous commit on big-endian hosts by _not_ falling through to
the `uint8_t` code path.
2025-03-26 08:19:31 +01:00
Timm Baeder
822aa5ec1a
[clang][bytecode][NFC] use Field::isUnnamedBitField() directly (#132914)
Instead of going though Decl.
2025-03-25 12:32:27 +01:00
Timm Baeder
a29b0d74a1
[clang][bytecode] Fix base cast of nullptr without descriptor (#132909)
The missing descriptor should only happen if the pointer is null
pointer.
2025-03-25 11:37:03 +01:00
Timm Baeder
9b060d1e6a
[clang][bytecode] Fix zero-init of atomic floating point objects (#132782)
We can't pass the AtomicType along to ASTContext::getFloatTypeSemantics.
2025-03-25 08:05:04 +01:00
Timm Baeder
bcedb368e3
[clang][bytecode] Support composite arrays in memcpy op (#132775)
See the attached test case.
2025-03-25 07:17:10 +01:00
Timm Bäder
1e2ad6793a Revert "[clang][bytecode] Implement __builtin_{wcscmp,wcsncmp} (#132723)"
This reverts commit f7aea4d081f77dba48b0fc019f59b678fb679aa8.

This broke the clang-solaris11-sparcv9 builder:
https://lab.llvm.org/buildbot/#/builders/13/builds/6151
2025-03-25 07:15:30 +01:00
Timm Baeder
f7aea4d081
[clang][bytecode] Implement __builtin_{wcscmp,wcsncmp} (#132723) 2025-03-24 15:03:49 +01:00
Timm Baeder
9ab3b6a006
[clang][bytecode] Diagnose integral source/dest in memcpy (#132715)
Like the current interpreter does.
2025-03-24 12:44:35 +01:00
Timm Baeder
c7f14f601f
[clang][bytecode] Implement __builtin_wcschr (#132708)
This is already almost implemented, just need to enable support for it.
2025-03-24 12:28:23 +01:00
Timm Baeder
849e5ea94f
[clang][bytecode] Add Descriptor::getDataType() (#132681)
This returns the type of data in the Block, which might be different
than the type of the expression or declaration we created the block for.
This lets us remove some special cases from CheckNewDeleteForms() and
CheckNewTypeMismatch().
2025-03-24 09:47:52 +01:00
Timm Baeder
db7475a770
[clang][bytecode] Ignore overflow in unary operators if requested (#132557)
Add PreInc and PreDec ops for this purpose and ignore the overflow if
UnaryOperator::canOverflow() returns false.
2025-03-22 18:03:50 +01:00
Timm Baeder
5fc891b965
[clang][bytecode][NFC] Use getElemType() in __builtin_memchr as well (#132550)
For consistency.
2025-03-22 16:34:22 +01:00
Timm Baeder
c51d396f4d
[clang][bytecode] Fix __builtin_memmove type diagnostics (#132544)
Set the source type when allocating primitives so we can later retrieve
it.
2025-03-22 14:58:32 +01:00
Timm Baeder
6c8e9a6192
[clang][bytecode][NFC] Add assert to ptrauth_string_discriminator (#132527)
As pointed out by @shafik, this confuses static analysis and most
probably humans as well. Add an assertion to ensure the given array has
at least one element.
2025-03-22 08:05:28 +01:00
Timm Baeder
d67951694b
[clang][bytecode] Support overlapping regions in __builtin_memmove (#132523)
Unfortunately, a few circumstances make the implementation here less
than ideal, but we need to handle overlapping regions anyway.
2025-03-22 07:12:27 +01:00
Matheus Izvekov
1416566449
Reland: [clang] NFC: Clear some uses of MemberPointerType::getClass (#132317)
Relands Original PR: https://github.com/llvm/llvm-project/pull/131965
Addresses
https://github.com/llvm/llvm-project/pull/131965#issuecomment-2741619498
* Fixes isIncompleteType for injected classes

This clears up some uses of getClass on MemberPointerType when
equivalent uses of getMostRecentCXXRecordDecl would be just as simple or
simpler.
    
This is split-off from a larger patch which removes getClass, in order
to facilitate review.
2025-03-21 10:54:24 -03:00
Matheus Izvekov
335a4614de
Revert "[clang] NFC: Clear some uses of MemberPointerType::getClass" (#132281)
Reverts llvm/llvm-project#131965

Reverted due to issue reported here:
https://github.com/llvm/llvm-project/pull/131965#issuecomment-2741619498
2025-03-20 17:54:21 -03:00
Timm Baeder
7492666482
[clang][bytecode] Implement __builtin_wmemchr (#132254) 2025-03-20 21:01:14 +01:00
Timm Baeder
49f06075a6
[clang][bytecode] Fix union copy/move operator active check (#132238)
Don't call CheckActive for copy/move operators. They will activate the
union member.
2025-03-20 19:08:55 +01:00
Kazu Hirata
c38ef58557
[ByteCode] Avoid repeated hash lookups (NFC) (#132141) 2025-03-20 09:09:57 -07:00
Matheus Izvekov
fd7be0d2e9
[clang] NFC: Clear some uses of MemberPointerType::getClass (#131965) 2025-03-19 21:36:10 -03:00
Ayokunle Amodu
02744c5010
[clang][diagnostics] Update note_constexpr_invalid_cast to use enum_select and adjust its uses (#130868)
Handles #123121

This patch updates `note_constexpr_invalid_cast` diagnostic to use
`enum_select` instead of `select,` improving readability and reducing
reliance on magic numbers in caller sites.
2025-03-19 12:36:08 -07:00
Timm Baeder
e0db41615b
[clang][bytecode] Fix initializing array struct fields from an APValue (#131983)
We need to recurse once more here and move the array case into the
bigger if chain.
2025-03-19 12:43:37 +01:00
cor3ntin
bc8b19c757
[Clang] Introduce a trait to determine the structure binding size (#131515)
Introduce a trait to determine the number of bindings that would be
produced by

```cpp

   auto [...p] = expr;

```

This is necessary to implement P2300
(https://eel.is/c++draft/exec#snd.concepts-5), but can also be used to
implement a general get<N> function that supports aggregates

`__builtin_structured_binding_size` is a unary type trait that evaluates
to the number of bindings in a decomposition

If the argument cannot be decomposed, a sfinae-friendly error is
produced.

A type is considered a valid tuple if `std::tuple_size_v<T>` is a valid
expression, even if there is no valid `std::tuple_element`
specialization or suitable `get` function for that type.


Fixes #46049
2025-03-18 20:50:56 +01:00
Timm Baeder
2f808dd070
[clang][bytecode] Compile most recent function decl (#131730)
We used to always do this because all calls went through the code path
that calls getMostRecentDecl(). Do it now, too.
2025-03-18 07:29:38 +01:00