259 Commits

Author SHA1 Message Date
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
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
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
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
061b1d1149
[clang][bytecode] Redo RUN lines in the builtin-functions test (#132762)
Make sure we run each configuration once with the bytecode interpreter
and once with the current one. Add a triple to the one that was
previously without.
2025-03-24 18:33:36 +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
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
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
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
Sirraide
f01b56ffb3
[Clang] [NFC] Introduce helpers for defining compatibilty warnings (#132129)
This introduces some tablegen helpers for defining compatibility
warnings. The main aim of this is to both simplify adding new
compatibility warnings as well as to unify the naming of compatibility
warnings.

I’ve refactored ~half of the compatiblity warnings (that follow the
usual scheme) in `DiagnosticSemaKinds.td` for illustration purposes and
also to simplify/unify the wording of some of them (I also corrected a
typo in one of them as a drive-by fix).

I haven’t (yet) migrated *all* warnings even in that one file, and there
are some more specialised ones for which the scheme I’ve established
here doesn’t work (e.g. because they’re warning+error instead of
warning+extwarn; however, warning+extension *is* supported), but the
point of this isn’t to implement *all* compatibility-related warnings
this way, only to make the common case a bit easier to handle.

This currently also only handles C++ compatibility warnings, but it
should be fairly straight-forward to extend the tablegen code so it can
also be used for C compatibility warnings (if this gets merged, I’m
planning to do that in a follow-up pr).

The vast majority of compatibility warnings are emitted by writing
```c++
Diag(Loc, getLangOpts().CPlusPlusYZ ? diag::ext_... : diag::warn_...)
```
in accordance with which I’ve chosen the following naming scheme:
```c++
Diag(Loc, getLangOpts().CPlusPlusYZ ? diag::compat_cxxyz_foo : diag::compat_pre_cxxyz_foo)
```
That is, for a warning about a C++20 feature—i.e. C++≤17
compatibility—we get:
```c++
Diag(Loc, getLangOpts().CPlusPlus20 ? diag::compat_cxx20_foo : diag::compat_pre_cxx20_foo)
```
While there is an argument to be made against writing ‘`compat_cxx20`’
here since is technically a case of ‘C++17 compatibility’ and not ‘C++20
compatibility’, I at least find this easier to reason about, because I
can just write the same number 3 times instead of having to use
`ext_cxx20_foo` but `warn_cxx17_foo`. Instead, I like to read this as a
warning about the ‘compatibility *of* a C++20 feature’ rather than
‘*with* C++17’.

I also experimented with moving all compatibility warnings to a separate
file, but 1. I don’t think it’s worth the effort, and 2. I think it
hurts compile times a bit because at least in my testing I felt that I
had to recompile more code than if we just keep e.g. Sema-specific
compat warnings in the Sema diagnostics file.

Instead, I’ve opted to put them all in the same place within any one
file; currently this is a the very top but I don’t really have strong
opinions about this.
2025-03-21 03:55:42 +01: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
Aaron Ballman
c65fa9163e
[C23] Fix compound literals within function prototype (#132097)
WG14 N2819 clarified that a compound literal within a function prototype
has a lifetime similar to that of a local variable within the function,
not a file scope variable.
2025-03-20 08:03:52 -04: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
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
Timm Baeder
cfa07ccdfc
[clang][bytecode] Fix builtin_memchr with non-0 start index (#131633) 2025-03-17 19:02:55 +01:00
Timm Baeder
ca1bde0b91
[clang][bytecode] Check dtor instance pointers for active-ness (#128732)
And diagnose if we're trying to destroy an inactive member of a union.
2025-03-17 19:01:35 +01:00
Younan Zhang
f4218753ad
[Clang] Implement P0963R3 "Structured binding declaration as a condition" (#130228)
This implements the R2 semantics of P0963.

The R1 semantics, as outlined in the paper, were introduced in Clang 6.
In addition to that, the paper proposes swapping the evaluation order of
condition expressions and the initialization of binding declarations
(i.e. std::tuple-like decompositions).
2025-03-11 15:41:56 +08:00
Timm Baeder
cf6a520a7a
[clang][bytecode] Fix builtin_memcmp buffer sizes for pointers (#130570)
Don't use the pointer size, but the number of elements multiplied by the
element size.
2025-03-10 15:51:31 +01:00
Nikita Popov
07f3388fff Revert "[clang] Implement instantiation context note for checking template parameters (#126088)"
This reverts commit a24523ac8dc07f3478311a5969184b922b520395.

This is causing significant compile-time regressions for C++ code, see:
https://github.com/llvm/llvm-project/pull/126088#issuecomment-2704874202
2025-03-10 10:32:08 +01:00
Timm Baeder
0f732481ac
[clang][bytecode] Fix getting pointer element type in __builtin_memcmp (#130485)
When such a pointer is heap allocated, the type we get is a pointer
type. Take the pointee type in that case.
2025-03-09 12:57:42 +01:00
Timm Baeder
aff6ab9d90
[clang][bytecode] Surround bcp condition with Start/EndSpeculation (#130427)
This is similar to what the current interpreter is doing - the
FoldConstant RAII object surrounds the entire HandleConditionalOperator
call, which means the condition and both TrueExpr or FalseExpr.
2025-03-08 19:37:20 +01:00
Timm Baeder
46d218d1af
[clang][bytecode] Implement __builtin_{memchr,strchr,char_memchr} (#130420)
llvm has recently started to use `__builitn_memchr` at compile time, so
implement this. Still needs some work but the basics are done.
2025-03-08 16:52:06 +01:00
Timm Baeder
3b8f9a228c
[clang][bytecode] Loosen assertion This() for array elements (#130399)
getRecord() returns null on array elements, even for composite arrays.
The assertion here was overly restrictive and having an array element as
instance pointer should be fine otherwise.
2025-03-08 13:13:52 +01:00
Timm Baeder
d08cf7900d
[clang][bytecode] Implement __builtin_constant_p (#130143)
Use the regular code paths for interpreting.

Add new instructions: `StartSpeculation` will reset the diagnostics
pointers to `nullptr`, which will keep us from reporting any diagnostics
during speculation. `EndSpeculation` will undo this.

The rest depends on what `Emitter` we use.

For `EvalEmitter`, we have no bytecode, so we implement `speculate()` by
simply visiting the first argument of `__builtin_constant_p`. If the
evaluation fails, we push a `0` on the stack, otherwise a `1`.

For `ByteCodeEmitter`, add another instrucion called `BCP`, that
interprets all the instructions following it until the next
`EndSpeculation` instruction. If any of those instructions fails, we
jump to the `EndLabel`, which brings us right before the
`EndSpeculation`. We then push the result on the stack.
2025-03-08 06:06:14 +01:00
Matheus Izvekov
a24523ac8d
[clang] Implement instantiation context note for checking template parameters (#126088)
Instead of manually adding a note pointing to the relevant template
parameter to every relevant error, which is very easy to miss, this
patch adds a new instantiation context note, so that this can work using
RAII magic.

This fixes a bunch of places where these notes were missing, and is more
future-proof.

Some diagnostics are reworked to make better use of this note:
- Errors about missing template arguments now refer to the parameter
which is missing an argument.
- Template Template parameter mismatches now refer to template
parameters as parameters instead of arguments.

It's likely this will add the note to some diagnostics where the
parameter is not super relevant, but this can be reworked with time and
the decrease in maintenance burden makes up for it.

This bypasses the templight dumper for the new context entry, as the
tests are very hard to update.

This depends on #125453, which is needed to avoid losing the context
note for errors occuring during template argument deduction.
2025-03-06 14:58:42 -03:00
Timm Baeder
bdbc434498
[clang][bytecode] Ignore function calls with depth > 0... (#129887)
... when checking for a potential constant expression. This is also what
the current interpreter does.
2025-03-05 16:21:03 +01:00
Timm Baeder
107fe0ec6c
[clang][bytecode] Fix a crash in CheckConstantExpression (#129752)
The APValue we generated for a pointer with a LValueReferenceType base
had an incorrect lvalue path attached.

The attached test case is extracted from libc++'s regex.cpp.
2025-03-05 08:21:51 +01:00
Timm Baeder
aeca2aa193
[clang][bytecode] Fix CallPtr return type check (#129722)
CallExpr::getType() isn't enough here in some cases, we need to use
CallExpr::getCallReturnType().
2025-03-04 17:14:13 +01:00
Timm Baeder
1d8eb436ca
[clang][bytecode] Diagnose member calls on inactive union fields (#129709)
Unless the function is a constructor, which is allowed to do this since
it will activate the member.
2025-03-04 16:14:47 +01:00
Timm Baeder
f838a5e96c
[clang][bytecode] Fix diagnostic difference with opaque call cmps (#129702)
Try to dig out the call expression and diagnose this as an opaque call.
2025-03-04 15:04:57 +01:00
Timm Baeder
53d433e702
[clang][bytecode] Only emit literal_comparison for string literals (#129691)
This is what the current interpreter does as well.
2025-03-04 14:07:53 +01:00
Timm Baeder
06fc7d68ff
[clang][bytecode] Don't error out on incomplete declarations (#129685)
Later operations on these are invalid, but the declaration is fine, if
extern.
2025-03-04 12:41:34 +01:00
Timm Baeder
3dafa486a6
[clang][bytecode] Don't narrow() when dereferencing to array type (#129524)
It doesn't make sense to do this if the result is supposed to be an
array.
2025-03-03 17:14:54 +01:00
Timm Baeder
96336acb48
[clang][bytecode] Tighten double-destroy check (#129528)
The instance pointer of the current function being the same as the one
we're destroying is only relevant if said function is also a destructor.
2025-03-03 16:26:56 +01:00
Timm Baeder
a955426a16
[clang][bytecode] Handle UsingDirectiveDecls (#128888)
By ignoring them.
2025-02-26 16:55:15 +01:00
Timm Baeder
3f648992bf
[clang][bytecode] Fix initing incomplete arrays from ImplicitValueIni… (#128729)
…tExpr

If the ImplicitValueInitExpr is of incomplete array type, we ignore it
in its Visit function. This is a special case here, so pull out the
element type and zero the elements.
2025-02-26 08:14:00 +01:00
Timm Baeder
dff2ca424c
[clang][bytecode] Add special case for anonymous unions (#128681)
This fixes the expected output to match the one of the current
interpreter.
2025-02-25 12:46:06 +01:00
Timm Baeder
dfa3af9255
[clang][bytecode] Expand subscript base if of pointer type (#128511)
This is similar to what we do in the AddOffset instruction when adding
an offset to a pointer.
2025-02-25 11:40:05 +01:00
Timm Baeder
19a39e98ff
[clang][bytecode] Handle non-primitive array index expressions (#128479)
By rejecting them instead of asserting in `classifyPrim()`.
2025-02-24 09:36:48 +01:00
Timm Baeder
8102fec00b
[clang][bytecode] Reject calls to pure virtual functions (#128412) 2025-02-23 11:44:37 +01:00
Timm Baeder
c38befd94f
[clang][bytecode] Fix delete[] dtor order (#128411)
As always, call array dtors in reverse order.
2025-02-23 11:32:35 +01:00
Timm Baeder
6db96c9ecc
[clang][bytecode] Always reject ctors of invalid parent decls (#128295)
The copy constructor of an invalid declaration might still be perfectly
valid, but we still need to reject it.
2025-02-22 22:04:44 +01:00