Commit Graph

9863 Commits

Author SHA1 Message Date
Erich Keane 5293016287 Revert "GH58368: Correct concept checking in a lambda defined in concept"
This reverts commit b7c922607c.

This seems to cause some problems with some modules related things,
which makes me think I should have updated the version-major in
ast-bit-codes?  Going to revert to confirm this was a problem, then
change that and re-try a commit.
2022-10-24 10:21:22 -07:00
Erich Keane b7c922607c GH58368: Correct concept checking in a lambda defined in concept
As that bug reports, the problem here is that the lambda's
'context-decl' was not set to the concept, and the lambda picked up
template arguments from the concept.  SO, we failed to get the correct
template arguments in SemaTemplateInstantiate.

However, a Concept Specialization is NOT a decl, its an expression, so
we weren't able to put the concept in the decl tree like we needed.
This patch introduces a ConceptSpecializationDecl, which is the smallest
type possible to use for this purpose, containing only the template
arguments.

The net memory impliciation of this is turning a
trailing-objects into a pointer to a type with trailing-objects,  so it
should be minor.

As future work, we may consider giving this type more responsibility, or
figuring out how to better merge duplicates, but as this is just a
template-argument collection at the moment, there isn't much value to
it.

Differential Revision: https://reviews.llvm.org/D136451
2022-10-24 06:32:18 -07:00
Rong Xu 6cee539337 [Clang] Change AnonStructIds in MangleContext to per-function based
Clang is generating different mangled names for the same lambda
function in slightly changed builds (like with non-related
source/Macro change). This is due to the fact that clang uses a
cross-translation-unit sequential string "$_<n>" in lambda's
mangled name. Here, "n" is the AnonStructIds field in MangleContext.

Different mangled names for a unchanged function is undesirable:
it makes perf comparison harder, and can cause some unnecessary
profile mismatch in SampleFDO.

This patch makes mangled name for lambda functions more stable
by changing AnonStructIds to a per-function based seq number if the
DeclContext is a function.

Differential Revision: https://reviews.llvm.org/D136397
2022-10-23 22:33:52 -07:00
Evgeny Shulgin 2bb50a55b0 [clang] Fix time profile in "isIntegerConstantExpr"
The time profiler in `Expr::isIntegerConstantExpr` used to
call `Loc->printToString`, it was inconsistent with other time
profiles in the file and caused segfaults if `Loc` was `nullptr`.

Fixes https://github.com/llvm/llvm-project/issues/58551

Reviewed By: dyung, jloser

Differential Revision: https://reviews.llvm.org/D136549
2022-10-23 23:17:56 +00:00
Kazu Hirata f92980c726 [clang] Fix a warning
This patch fixes:

  clang/lib/AST/Interp/ByteCodeExprGen.cpp:978:24: warning: variable
  ‘T’ set but not used [-Wunused-but-set-variable]

T and ReturnType were introduced on August 19, 2022 in commit
8e41e6a4ea.

Their last uses were removed on October 13, 2022 in commit
0e754cfadc.
2022-10-23 09:34:49 -07:00
Timm Bäder 0e754cfadc [clang][Interp][NFC] Unify Call() implementations
The type parameter we used to pass to call() was unused. Use the same
implementation for void and value-returning function calls.
2022-10-22 10:32:05 +02:00
Timm Bäder cc79ddb52c [clang][Interp] Check instance pointers before calling functions on them
Remove the double Call() implementation to reduce code duplication. Then
fix Function::getSource() so we can diagnose instance pointers being
null.

Differential Revision: https://reviews.llvm.org/D135513
2022-10-22 10:32:05 +02:00
Timm Bäder 8013ab4f98 [clang][Interp][NFC] Simplify creating parameter descriptors
... using value_or instead of the if-else statement.
2022-10-22 10:32:05 +02:00
Timm Bäder 4abd868304 [clang][Interp][NFC] Trim InterpFrame includes 2022-10-22 10:32:05 +02:00
Timm Bäder c01ac372fc [clang][Interp][NFC] Add assertions to VM entry points
Assert that the previous call left the stack empty, as well as that
successful interpretations leave an empty stack.
2022-10-22 10:19:42 +02:00
Timm Bäder 7a66e6e19b [clang][Interp][NFC] Unify Div/Rem operation checks 2022-10-22 10:19:42 +02:00
Evgeny Shulgin 27d8eedd5a [clang] Add time profile for constant evaluation
Add time profiler for various constexpr evaluation events
so that slow event could be visible on the visualized flame chart.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D136022
2022-10-21 23:24:44 +00:00
Evgeny Shulgin 5b567637e2 [Clang] Add __has_constexpr_builtin support
The `__has_constexpr_builtin` macro can be used to check
whether the builtin in constant-evaluated by Clang frontend.

Reviewed By: aaron.ballman, shafik

Differential Revision: https://reviews.llvm.org/D136036
2022-10-21 11:23:10 +00:00
Timm Bäder 9cb4e90e72 [clang][Interp] Support base class constructors
Differential Revision: https://reviews.llvm.org/D135025
2022-10-21 10:49:45 +02:00
Timm Bäder 09bbc903a5 [clang][Interp] Array initialization via ImplicitValueInitExpr
Differential Revision: https://reviews.llvm.org/D135013
2022-10-21 10:49:45 +02:00
Timm Bäder d6cb1fd7b3 [clang][Interp][NFC] Remove some unused aliases. 2022-10-21 10:49:45 +02:00
Bill Wendling 283e0a81ef [clang] Correct sanitizer behavior in union FAMs
Clang doesn't have the same behavior as GCC does with union flexible
array members. (Technically, union FAMs are probably not acceptable in
C99 and are an extension of GCC and Clang.)

Both Clang and GCC treat *all* arrays at the end of a structure as FAMs.
GCC does the same with unions. Clang does it for some arrays in unions
(incomplete, '0', and '1'), but not for all. Instead of having this
half-supported feature, sync Clang's behavior with GCC's.

Reviewed By: kees

Differential Revision: https://reviews.llvm.org/D135727
2022-10-20 16:08:11 -07:00
Xiang Li 464926ef44 [HLSL] Disable integer promotion to avoid int16_t being promoted to int for HLSL.
short will be promoted to int in UsualUnaryConversions.
Disable it for HLSL to keep int16_t as 16bit.

Reviewed By: aaron.ballman, rjmccall

Differential Revision: https://reviews.llvm.org/D133668
2022-10-20 16:06:25 -07:00
Xiang Li 7e04c0ad63 [HLSL] Add groupshare address space.
Added keyword, LangAS and TypeAttrbute for groupshared.

Tanslate it to LangAS with asHLSLLangAS.

Make sure it translated into address space 3 for DirectX target.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D135060
2022-10-20 09:29:09 -07:00
Yuanfang Chen 340eac01f7 [C++20] Implement P2113R0: Changes to the Partial Ordering of Constrained Functions
This implementation matches GCC behavior in that [[ https://eel.is/c++draft/temp.func.order#6.2.1 | temp.func.order p6.2.1 ]] is not implemented [1]. I reached out to the GCC author to confirm that some changes elsewhere to overload resolution are probably needed, but no solution has been developed sufficiently [3].

Most of the wordings are implemented straightforwardly. However,
for [[ https://eel.is/c++draft/temp.func.order#6.2.2 | temp.func.order p6.2.2 ]] "... or if the function parameters that positionally correspond between the two templates are not of the same type", the "same type" is not very clear ([2] is a bug related to this). Here is a quick example
```
template <C T, C U>        int f(T, U);
template <typename T, C U> int f(U, T);

int x = f(0, 0);
```
Is the `U` and `T` from different `f`s the "same type"? The answer is NO even though both `U` and `T` are deduced to be `int` in this case. The reason is that `U` and `T` are dependent types, according to [[ https://eel.is/c++draft/temp.over.link#3 |  temp.over.link p3 ]], they can not be the "same type".

To check if two function parameters are the "same type":
* For //function template//: compare the function parameter canonical types and return type between two function templates.
* For //class template/partial specialization//: by [[ https://eel.is/c++draft/temp.spec.partial.order#1.2 | temp.spec.partial.order p1.2 ]], compare the injected template arguments between two templates using hashing(TemplateArgument::Profile) is enough.

[1] https://gcc.gnu.org/git/gitweb.cgi?p=gcc.git;h=57b4daf8dc4ed7b669cc70638866ddb00f5b7746
[2] https://github.com/llvm/llvm-project/issues/49308
[3] https://lists.isocpp.org/core/2020/06/index.php#msg9392

Fixes https://github.com/llvm/llvm-project/issues/54039
Fixes https://github.com/llvm/llvm-project/issues/49308 (PR49964)

Reviewed By: royjacobson, #clang-language-wg, mizvekov

Differential Revision: https://reviews.llvm.org/D128750
2022-10-18 11:58:57 -07:00
Joseph Huber bb3c90d3ec [OpenMP] Make device functions have hidden visibility
In OpenMP target offloading an in other offloading languages, we
maintain a difference between device functions and kernel functions.
Kernel functions must be visible to the host and act as the entry point
to the target device. Device functions however cannot be called directly
by the host and must be called by a kernel function. Currently, we make
all definitions on the device protected by default. Because device
functions cannot be called or used by the host they should have hidden
visibility. This allows for the definitions to be better optimized via
LTO or other passes.

This patch marks every device function in the AST as having `hidden`
visibility. The kernel function is generated later at code-gen and we
set its visibility explicitly so it should not be affected. This
prevents the user from overriding the visibility, but since the user
can't do anything with these symbols anyway there is no point exporting
them right now.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D136111
2022-10-18 08:15:39 -05:00
Volodymyr Sapsai 2662009c87 [ODRHash] Hash `ObjCMethodDecl` and diagnose discovered mismatches.
Differential Revision: https://reviews.llvm.org/D130325
2022-10-17 18:48:24 -07:00
Volodymyr Sapsai 37fdca21f7 [ODRHash] Rename `isDeclToBeProcessed` to `isSubDeclToBeProcessed`. NFC intended.
The method is used only for sub-Decls, so reflect that in the name.
2022-10-17 18:24:44 -07:00
Volodymyr Sapsai 9c79eab7fd [ODRHash] Hash `ObjCProtocolDecl` and diagnose discovered mismatches.
Differential Revision: https://reviews.llvm.org/D130324
2022-10-17 16:29:52 -07:00
Kazu Hirata 08901c8a98 [clang] Use llvm::reverse (NFC) 2022-10-15 21:54:13 -07:00
Matheus Izvekov bcd9ba2b7e
[clang] Track the templated entity in type substitution.
This is a change to how we represent type subsitution in the AST.
Instead of only storing the replaced type, we track the templated
entity we are substituting, plus an index.
We modify MLTAL to track the templated entity at each level.

Otherwise, it's much more expensive to go from the template parameter back
to the templated entity, and not possible to do in some cases, as when
we instantiate outer templates, parameters might still reference the
original entity.

This also allows us to very cheaply lookup the templated entity we saw in
the naming context and find the corresponding argument it was replaced
from, such as for implementing template specialization resugaring.

Signed-off-by: Matheus Izvekov <mizvekov@gmail.com>

Differential Revision: https://reviews.llvm.org/D131858
2022-10-15 22:08:36 +02:00
David Blaikie 037f856681 Itanium ABI: Pack non-pod members of packed types
Seems there's a narrow case - where a packed type doesn't pack its base
subobjects (only fields), but when packing a field of the derived type,
GCC does pack the resulting total object - effectively packing the base
subobject.

So ensure that this non-pod type (owing to it having a base class) that
is packed, gets packed when placed in /another/ type that is also
packed.

This is a (smallish?) ABI fix to a regression introduced by D117616 -
but that regression/ABI break hasn't been released in LLVM as-yet (it's
been reverted on the release branch from the last two LLVM releases - I
probably should've just reverted the whole patch while we hashed out
this and other issues) so this change isn't itself an ABI break, as far
as LLVM releases are concerned (for folks releasing their own copies of
LLVM from ToT/without the LLVM release branch, and didn't opt into the
clang-abi-compat 14 or below (soon to be 15 or below, I guess I should
say) then this would be an ABI break against clang from the last 9
months or so)

Differential Revision: https://reviews.llvm.org/D135916
2022-10-14 19:32:57 +00:00
Aaron Ballman 3a31970ee2 [C2x] Implement support for nullptr and nullptr_t
This introduces support for nullptr and nullptr_t in C2x mode. The
proposal accepted by WG14 is:
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3042.htm

Note, there are quite a few incompatibilities with the C++ feature in
some of the edge cases of this feature. Therefore, there are some FIXME
comments in tests for testing behavior that might change after WG14 has
resolved national body comments (a process we've not yet started). So
this implementation might change slightly depending on the resolution
of comments. This is called out explicitly in the release notes as
well.

Differential Revision: https://reviews.llvm.org/D135099
2022-10-14 10:06:02 -04:00
Timm Bäder 5a859432f3 [clang][Interp] Implement while and do-while loops
Differential Revision: https://reviews.llvm.org/D135433
2022-10-14 14:41:05 +02:00
Timm Bäder 6fad7127cb [clang][Interp][NFC] Remove unused parameter from emitConst() 2022-10-14 14:41:05 +02:00
Aaron Ballman 19e984ef8f Properly print unnamed TagDecl objects in diagnostics
The diagnostics engine is very smart about being passed a NamedDecl to
print as part of a diagnostic; it gets the "right" form of the name,
quotes it properly, etc. However, the result of using an unnamed tag
declaration was to print '' instead of anything useful.

This patch causes us to print the same information we'd have gotten if
we had printed the type of the declaration rather than the name of it,
as that's the most relevant information we can display.

Differential Revision: https://reviews.llvm.org/D134813
2022-10-14 08:18:28 -04:00
Timm Bäder ce4d5ae9dc [clang][Interp] Implement bitwise Or operations
Analogous to the bitAnd implementation, do the same for bitwise or.

Differential Revision: https://reviews.llvm.org/D135361
2022-10-14 14:15:08 +02:00
Timm Bäder 62a58050ba [clang][Interp] Implement bitwise and operations
Differential Revision: https://reviews.llvm.org/D135012
2022-10-14 14:00:07 +02:00
Timm Bäder 11f3604695 [clang][Interp][NFC] Remove an unnecessary local variable 2022-10-14 13:33:04 +02:00
Timm Bäder 1b731bd872 [clang][Interp][NFC] Zero-initialize Function::FrameSize
It's never going to be used since it's only uninitialized if the
function is invalid, but let's zero it for readbility of
Function::dump().
2022-10-14 13:32:55 +02:00
Timm Bäder 7fbfe5518c [clang][Interp][NFC] Rename a parameter to be more descriptive 2022-10-14 13:32:31 +02:00
Timm Bäder 9b2f2d8463 [clang][Interp][NFC] Remove unused function 2022-10-14 13:32:00 +02:00
Timm Bäder 77aaf469a7 [clang][Interp] Implement ConditionalOperators
Differential Revision: https://reviews.llvm.org/D134801
2022-10-14 13:16:10 +02:00
Timm Bäder d704ba26b9 [clang][Interp] Implement div opcode
Implement an opcode for division of two integrals.

Differential Revision: https://reviews.llvm.org/D134749
2022-10-14 12:57:57 +02:00
Timm Bäder c9ad877844 [clang][Interp] Implement rem opcode
Implement an opcode to get the remainder of the divison between LHS and
RHS.

Differential Revision: https://reviews.llvm.org/D134744
2022-10-14 12:57:57 +02:00
Timm Bäder 542123465f [clang][Interp] Implement bitwise not operations
Differential Revision: https://reviews.llvm.org/D134804
2022-10-14 12:57:57 +02:00
Timm Bäder 81c5b5d80e [clang][Interp][NFC] Simplify Integral using constexpr if
Just keep one version of the function and differentiate between
std::is_signed() and unsigned using a constexpr if, instead of having
two different versions for the signed and unsigned cases.
2022-10-14 12:47:07 +02:00
Timm Bäder 699449d71e [clang][Interp][NFC] Use a SourceRange for errors
This makes the error message generated by bail() a bit more pleasant to
read.
2022-10-14 12:46:51 +02:00
Timm Bäder 4d2d426a51 [clang][Interp] Fix Pointer::toAPValue() LValuePath order 2022-10-14 12:31:24 +02:00
Timm Bäder 55c7ad31aa [clang][Interp][NFC] Pass Function* pointers around as const 2022-10-14 12:31:24 +02:00
Timm Bäder 6e83209f62 [clang][Interp] Fix copy constructors with record array members
Differential Revision: https://reviews.llvm.org/D134523
2022-10-14 11:57:26 +02:00
Timm Bäder ae27323428 [clang][Interp] Classify ArrayInitIndexExpr type
We can't just push a uint64 unconditionally here, since on 32bit arches
we later expect a different type, e.g. uint32.

This broke e.g. these builders:
https://lab.llvm.org/buildbot#builders/171/builds/21514
https://lab.llvm.org/buildbot#builders/38/builds/6643
2022-10-14 11:10:28 +02:00
Benjamin Kramer 2f357054d7 [Interp] Silence warning in release builds. NFC. 2022-10-14 10:38:32 +02:00
Timm Bäder 33b52836de [clang][Interp] Fix using default copy constructors
Implement ArrayInitLoopExprs, which are used in copy constructors to
copy arrays. Also fix problems encountered while doing that.

Differential Revision: https://reviews.llvm.org/D134361
2022-10-14 10:21:53 +02:00
Timm Bäder cb5f205828 [clang][Interp] Implement nested struct initialization
Recurse into visitInitializer() if necessary.

Differential Revision: https://reviews.llvm.org/D134175
2022-10-14 10:21:53 +02:00
Timm Bäder 0ddd13acc9 [clang][Interp] Implement This pointer passing to methods
Implement passing the this pointer to member functions and constructors.
The this pointer is passed via the stack. This changes the functions to
explicitly track whether they have a RVO pointer and a this pointer.

Differential Revision: https://reviews.llvm.org/D134699
2022-10-14 10:21:53 +02:00
Timm Bäder 1942a2538b [clang][Interp] Start implementing record types
Implement simple constructors as well as member access expressions.

Differential Revision: https://reviews.llvm.org/D134057
2022-10-14 10:21:53 +02:00
Timm Bäder 1928da1ef7 [clang][Interp] Don't run functions immediately after compiling them
This doesn't make much sense with functions that expect valid parameters
and/or a certain call stack on the caller side like This/RVO pointers.

Differential Revision: https://reviews.llvm.org/D135569
2022-10-14 10:21:53 +02:00
Timm Bäder 773b468543 [clang][Interp][NFC] Add a TODO comment
We can ignore casts where FromT and ToT are the same type. But that's a
performance optimization that I'd like to do later. For now, this code
is doing the right thing.
2022-10-14 10:21:52 +02:00
David Blaikie 9363071303 Move GCC-compatible pod-packing change to v16/old behavior available at v15 and below
Change matches D126334/e59f648d698e since this change got punted from
v15 too.
2022-10-13 21:13:19 +00:00
Sam McCall 2eaf6f973c [AST] Preserve more structure in UsingEnumDecl node.
- store NestedNameSpecifier & Loc for the qualifiers
  This information was entirely missing from the AST.
- expose the location information for qualifier/identifier/typedefs as typeloc
  This allows many traversals/astmatchers etc to handle these generically along
  with other references. The decl vs type split can help preserve typedef
  sugar when https://github.com/llvm/llvm-project/issues/57659 is resolved.
- fix the SourceRange of UsingEnumDecl to include 'using'.

Fixes https://github.com/clangd/clangd/issues/1283

Differential Revision: https://reviews.llvm.org/D134303
2022-10-12 19:54:51 +02:00
Arthur Eubanks eab5c2f94f [LLDB] Fix crash when printing a struct with a static wchar_t member
Similar to D135170.

Reviewed By: DavidSpickett

Differential Revision: https://reviews.llvm.org/D135461
2022-10-11 11:04:32 -07:00
Evgeny Shulgin ec32386404 [Clang] Support constexpr builtin fmin
Support constexpr version of __builtin_fmin and its variations.

Reviewed By: jcranmer-intel

Differential Revision: https://reviews.llvm.org/D135493
2022-10-10 16:06:23 +00:00
Evgeny Shulgin 0edff6faa2 [Clang] Support constexpr builtin fmax
Support constexpr version of __builtin_fmax and its variations.

Reviewed By: jcranmer-intel

Differential Revision: https://reviews.llvm.org/D134369
2022-10-07 20:27:17 +00:00
Aleksandr Platonov 5a42c90b77 [clang] Make variables of undeduced types to have dependent alignment
Without this patch `VarDecl::hasDependent()` checks only undeduced auto types, so can give false negatives result for other undeduced types.
This lead to crashes in sequence `!VarDecl::hasDepentent()` => `getDeclAlign()`.

It seems this problem appeared since D105380

Reviewed By: mizvekov

Differential Revision: https://reviews.llvm.org/D135362
2022-10-07 20:40:03 +08:00
David Spickett 5a9e213058 [LLDB] Fix crash when printing a struct with a static signed char member
As with static bool for whatever reason printing them on their own
worked fine but wasn't handled when you printed the whole type.

I don't see a good way to test this from clang's side so our existing
tests will have to do.

We can now print all of the struct "A", so there's no need for a separate
one for static bool testing. I've not checked the output, just that it
succeeds. This saves us having to handle different min/max between systems.

Depends on D135169

Reviewed By: aeubanks, shafik

Differential Revision: https://reviews.llvm.org/D135170
2022-10-07 09:11:15 +00:00
Bill Wendling 7404b855e5 [clang][NFC] Use enum for -fstrict-flex-arrays
Use enums for the strict flex arrays flag so that it's more readable.

Differential Revision: https://reviews.llvm.org/D135107
2022-10-06 10:45:41 -07:00
Erich Keane 6ebc3abd76 [NFC] Replace use of !isTranslationUnit && !isNamespace with !isFileContext
isFileContext is exactly equal to these two, so simplify the function.
2022-10-06 09:40:51 -07:00
David Spickett 7448f38898 [clang] Add Create method for CXXBoolLiteralExpr
Reviewed By: shafik

Differential Revision: https://reviews.llvm.org/D135256
2022-10-06 09:35:14 +00:00
Argyrios Kyrtzidis 371883f46d [clang/Sema] Fix non-deterministic order for certain kind of diagnostics
In the context of caching clang invocations it is important to emit diagnostics in deterministic order;
the same clang invocation should result in the same diagnostic output.

rdar://100336989

Differential Revision: https://reviews.llvm.org/D135118
2022-10-05 12:58:01 -07:00
Akira Hatanaka 65a961f719 [Sema][ObjC] Fix assertion failure in getCommonNonSugarTypeNode
Instead of checking that the protocols of both types are all equal,
check that the canonical decls are equal.
2022-10-05 10:38:25 -07:00
Stella Laurenzo e28b15b572 Add APFloat and MLIR type support for fp8 (e5m2).
(Re-Apply with fixes to clang MicrosoftMangle.cpp)

This is a first step towards high level representation for fp8 types
that have been built in to hardware with near term roadmaps. Like the
BFLOAT16 type, the family of fp8 types are inspired by IEEE-754 binary
floating point formats but, due to the size limits, have been tweaked in
various ways in order to maximally use the range/precision in various
scenarios. The list of variants is small/finite and bounded by real
hardware.

This patch introduces the E5M2 FP8 format as proposed by Nvidia, ARM,
and Intel in the paper: https://arxiv.org/pdf/2209.05433.pdf

As the more conformant of the two implemented datatypes, we are plumbing
it through LLVM's APFloat type and MLIR's type system first as a
template. It will be followed by the range optimized E4M3 FP8 format
described in the paper. Since that format deviates further from the
IEEE-754 norms, it may require more debate and implementation
complexity.

Given that we see two parts of the FP8 implementation space represented
by these cases, we are recommending naming of:

* `F8M<N>` : For FP8 types that can be conceived of as following the
  same rules as FP16 but with a smaller number of mantissa/exponent
  bits. Including the number of mantissa bits in the type name is enough
  to fully specify the type. This naming scheme is used to represent
  the E5M2 type described in the paper.
* `F8M<N>F` : For FP8 types such as E4M3 which only support finite
  values.

The first of these (this patch) seems fairly non-controversial. The
second is previewed here to illustrate options for extending to the
other known variant (but can be discussed in detail in the patch
which implements it).

Many conversations about these types focus on the Machine-Learning
ecosystem where they are used to represent mixed-datatype computations
at a high level. At that level (which is why we also expose them in
MLIR), it is important to retain the actual type definition so that when
lowering to actual kernels or target specific code, the correct
promotions, casts and rescalings can be done as needed. We expect that
most LLVM backends will only experience these types as opaque `I8`
values that are applicable to some instructions.

MLIR does not make it particularly easy to add new floating point types
(i.e. the FloatType hierarchy is not open). Given the need to fully
model FloatTypes and make them interop with tooling, such types will
always be "heavy-weight" and it is not expected that a highly open type
system will be particularly helpful. There are also a bounded number of
floating point types in use for current and upcoming hardware, and we
can just implement them like this (perhaps looking for some cosmetic
ways to reduce the number of places that need to change). Creating a
more generic mechanism for extending floating point types seems like it
wouldn't be worth it and we should just deal with defining them one by
one on an as-needed basis when real hardware implements a new scheme.
Hopefully, with some additional production use and complete software
stacks, hardware makers will converge on a set of such types that is not
terribly divergent at the level that the compiler cares about.

(I cleaned up some old formatting and sorted some items for this case:
If we converge on landing this in some form, I will NFC commit format
only changes as a separate commit)

Differential Revision: https://reviews.llvm.org/D133823
2022-10-04 17:18:17 -07:00
Yuanfang Chen 5d69937b9f [Clang] make canonical AutoType constraints-free
As @mizvekov suggested in D134772. This works great for D128750 when
dealing with AutoType's.

Reviewed By: mizvekov, erichkeane

Differential Revision: https://reviews.llvm.org/D135088
2022-10-04 11:56:03 -07:00
serge-sans-paille 3460a5d795 [clang] Unify Sema and CodeGen implementation of isFlexibleArrayMemberExpr
Turn it into a single Expr::isFlexibleArrayMemberLike method, as discussed in

        https://discourse.llvm.org/t/rfc-harmonize-flexible-array-members-handling

Keep different behavior with respect to macro / template substitution, and
harmonize sharp edges: ObjC interface now behave as C struct wrt. FAM and
-fstrict-flex-arrays.

This does not impact __builtin_object_size interactions with FAM.

Differential Revision: https://reviews.llvm.org/D134791
2022-10-04 20:42:36 +02:00
Nathan James 1376c73927
[clang] Add fix-it note to defaulted-function-deleted warning
Adds a fix to the diagnostic of replacing the `= default` to `= delete`

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D134549
2022-10-04 19:38:10 +01:00
Aaron Ballman 42ad305bdb Modify the qualified/unqualified getter for TypeOfType; NFC
Post-commit feedback observed that returning the TypeOfKind from the
type instead of a Boolean cleans up code using that interface.
2022-10-04 12:14:38 -04:00
Tom Honermann 4409a83c29 [clang] Correct handling of lambdas in lambda default arguments in dependent contexts.
Previously, a lambda expression in a dependent context with a default argument
containing an immediately invoked lambda expression would produce a closure
class object that, if invoked such that the default argument was used, resulted
in a compiler crash or one of the following assertion failures during code
generation. The failures occurred regardless of whether the lambda expressions
were dependent.

  clang/lib/CodeGen/CGCall.cpp:
  Assertion `(isGenericMethod || Ty->isVariablyModifiedType() || Ty.getNonReferenceType()->isObjCRetainableType() || getContext() .getCanonicalType(Ty.getNonReferenceType()) .getTypePtr() == getContext().getCanonicalType((*Arg)->getType()).getTypePtr()) && "type mismatch in call argument!"' failed.

  clang/lib/AST/Decl.cpp:
  Assertion `!Init->isValueDependent()' failed.

Default arguments in declarations in local context are instantiated along with
their enclosing function or variable template (since such declarations can't
be explicitly specialized). Previously, such instantiations were performed at
the same time that their associated parameters were instantiated. However, that
approach fails in cases like the following in which the context for the inner
lambda is the outer lambda, but construction of the outer lambda is dependent
on the parameters of the inner lambda. This change resolves this dependency by
delyaing instantiation of default arguments in local contexts until after
construction of the enclosing context.
  template <typename T>
  auto f() {
    return [](T = []{ return T{}; }()) { return 0; };
  }

Refactoring included with this change results in the same code now being used
to instantiate default arguments that appear in local context and those that
are only instantiated when used at a call site; previously, such code was
duplicated and out of sync.

Fixes https://github.com/llvm/llvm-project/issues/49178

Reviewed By: erichkeane

Differential Revision: https://reviews.llvm.org/D133500
2022-10-04 09:04:54 -07:00
Tom Honermann 4247cdb568 [clang]: Add DeclContext::dumpAsDecl().
This change enables a declaration to be conveniently displayed within
a debugger when only a pointer to its DeclContext is available. For example,
in gdb:
  (gdb) p Ctx
  $1 = (const clang::DeclContext *) 0x14c1a580
  (gdb) p Ctx->dumpAsDecl()
  ClassTemplateSpecializationDecl 0x14c1a540 <t.cpp:1:1, line:7:1> line:2:8 struct ct
  `-TemplateArgument type 'int'
    `-BuiltinType 0x14bac420 'int'
  $2 = void

In the event that the pointed to DeclContext is invalid (that it has an
invalid DeclKind as a result of a dangling pointer, memory corruption, etc...)
it is not possible to dump its associated declaration. In this case, the
DeclContext will be reported as invalid. For example, in gdb:
  (gdb) p Ctx->dumpAsDecl()
  DeclContext 0x14c1a580 <unrecognized Decl kind 127>
  $3 = void
2022-10-03 17:25:44 -04:00
Matheus Izvekov 69a6417406
[clang] Implement divergence for TypedefType and UsingType
With this patch, TypedefTypes and UsingTypes can have an
underlying type which diverges from their corresponding
declarations.

For the TypedefType case, this can be seen when getting
the common sugared type between two redeclarations with
different sugar.

For both cases, this will become important as resugaring
is implemented, as this will allow us to resugar these
when they were dependent before instantiation.

Signed-off-by: Matheus Izvekov <mizvekov@gmail.com>

Differential Revision: https://reviews.llvm.org/D133468
2022-10-03 23:23:58 +02:00
Erich Keane 939a3d22e2 [Concepts] Fix Concepts on generic lambda in a VarTemplateSpecDecl
As fallout of the Deferred Concept Instantiation patch (babdef27c5), we
got a number of reports of a regression, where we asserted when
instantiating a constraint on a generic lambda inside of a variable
template. See: https://github.com/llvm/llvm-project/issues/57958

The problem was that getTemplateInstantiationArgs function only walked
up declaration contexts, and missed that this is not necessarily the
case with a lambda (which can ALSO be in a separate context).

This patch refactors the getTemplateInstantiationArgs function in a way
that is hopefully more readable, and fixes the problem with the concepts
on a generic lambda.

Differential Revision: https://reviews.llvm.org/D134874
2022-10-03 12:44:21 -07:00
Kazu Hirata 9d0d4046c0 [clang] Use std::enable_if_t (NFC) 2022-10-01 17:24:54 -07:00
David Green 781b491bba [Clang][AArch64] Support AArch64 target(..) attribute formats.
This adds support under AArch64 for the target("..") attributes. The
current parsing is very X86-shaped, this patch attempts to bring it line
with the GCC implementation from
https://gcc.gnu.org/onlinedocs/gcc/AArch64-Function-Attributes.html#AArch64-Function-Attributes.

The supported formats are:
- "arch=<arch>" strings, that specify the architecture features for a
  function as per the -march=arch+feature option.
- "cpu=<cpu>" strings, that specify the target-cpu and any implied
  atributes as per the -mcpu=cpu+feature option.
- "tune=<cpu>" strings, that specify the tune-cpu cpu for a function as
  per -mtune.
- "+<feature>", "+no<feature>" enables/disables the specific feature, for
  compatibility with GCC target attributes.
- "<feature>", "no-<feature>" enabled/disables the specific feature, for
  backward compatibility with previous releases.

To do this, the parsing of target attributes has been moved into
TargetInfo to give the target the opportunity to override the existing
parsing. The only non-aarch64 change should be a minor alteration to the
error message, specifying using "CPU" to describe the cpu, not
"architecture", and the DuplicateArch/Tune from ParsedTargetAttr have
been combined into a single option.

Differential Revision: https://reviews.llvm.org/D133848
2022-10-01 15:40:59 +01:00
Timm Bäder 1ecaa617e7 [clang][Interp] Handle enums
Handle DeclRefExprs of enum types. They are otherwise handled like
integers.

Differential Revision: https://reviews.llvm.org/D134020
2022-09-30 08:18:14 +02:00
Timm Bäder f444a7cc16 Revert "[clang][Interp] Handle enums"
This reverts commit c090295916.

One of the test cases fails. Revert this until I know a way to make it
work reliably.
2022-09-29 13:45:52 +02:00
Timm Bäder 971c1b9647 [clang][Interp][NFC] Remove unused prototypes 2022-09-29 12:50:58 +02:00
Timm Bäder b721558a91 [clang][Interp][NFC] Make EvalEmitter::isActive() const 2022-09-29 12:50:57 +02:00
Timm Bäder 62bf6acae6 [clang][Interp][NFC] Unifty ReadArg() impl in Disasm.cpp
We can use another if constexpr here to make this shorter and easier to
understand.
2022-09-29 12:50:57 +02:00
Timm Bäder f55c4b4ee3 [clang][Interp] Print Function address in dump()
It's used in the bytecode dump of the function itself, so useful to
identify which function is being called.
2022-09-29 12:50:57 +02:00
Timm Bäder 9837a3bd4b [clang][Interp][NFC] Remove unused opcode argument types 2022-09-29 12:50:57 +02:00
Timm Bäder 7c65d57af0 [clang][Interp][NFC] Unify the two ReadArg() implementations
Just use a constexpr if here instead of two different implementations.
[#
2022-09-29 12:50:57 +02:00
Timm Bäder e8ad1339d4 [clang][Interp][NFC] Remove unused function 2022-09-29 12:50:57 +02:00
Timm Bäder f927b71376 [clang][Interp] Specify Boolean conversion operartors as (u)int32
Follow up to https://reviews.llvm.org/D133934
2022-09-29 12:50:57 +02:00
Timm Bäder 84f1df8aac [clang][Interp] Properly destruct allocated Records
We are otherwise leaking some memory the records might allocate
themselves.

Differential Revision: https://reviews.llvm.org/D134054
2022-09-29 12:50:56 +02:00
Timm Bäder d5360b932e [clang][Interp][NFC] Make classes final that can be final 2022-09-29 12:50:56 +02:00
Timm Bäder 8f5b061254 [clang][Interp][NFC] Make some Record methods const 2022-09-29 12:50:56 +02:00
Timm Bäder eadd505b0c [clang][Interp][NFC] Limit includes to neccessary ones 2022-09-29 12:50:56 +02:00
Timm Bäder 202ff42f89 [clang][Interp] Rename a local variable to be more specific
It's called BaseSize in the Record class as well, so call it BaseSize
when creating the Record.
2022-09-29 12:50:56 +02:00
Timm Bäder c090295916 [clang][Interp] Handle enums
Handle DeclRefExprs of enum types. They are otherwise handled like
integers.

Differential Revision: https://reviews.llvm.org/D134020
2022-09-29 12:50:56 +02:00
Timm Bäder df1cc801da [clang][Interp] Record item types in InterpStack
The type information is lost when pushing things on the stack. When
later pop()ing items of the wrong type, we can instead simply get
garbage values and those problems are hard to find. Add another stack to
record the type of item we pushed and use that for debugging.

Differential Revision: https://reviews.llvm.org/D133941
2022-09-29 12:50:55 +02:00
Timm Bäder 7c4cad4330 [clang][Interp][NFC] Make a few InterpStack functions const 2022-09-29 12:50:55 +02:00
Timm Bäder ee2e414d66 [clang][Interp] Handle sizeof()
Implement visiting UnaryExprOrTypeTraitExprs to handle sizeof()
expressions.

Differential Revision: https://reviews.llvm.org/D133934
2022-09-29 12:50:55 +02:00
Timm Bäder 1c35f3b93a [clang][Interp][NFC] Unify emit() implementations
Instead of two overloads, use a if constexpr to differentiate between
pointer and non-pointer parameters
2022-09-29 12:50:55 +02:00
Timm Bäder 1d31549e93 [clang][Interp][NFC] Forward-declare Pointer in InterpFrame.h
We don't need the full include here.
2022-09-29 12:50:55 +02:00
Timm Bäder 18a5fc52b2 [clang][Interp][NFC] Remove an unnecessary <vector> include. 2022-09-29 12:50:55 +02:00
Timm Bäder da62ed6ceb [clang][Interp] Pass initializer when creating globals
This is dead code right now but will be used for implementing array
fillers, where we need some information from the initializer when
allocaing the Descriptors.

Differential Revision: https://reviews.llvm.org/D133856
2022-09-29 12:50:54 +02:00
Aaron Ballman 60727d8569 [C2x] implement typeof and typeof_unqual
This implements WG14 N2927 and WG14 N2930, which together define the
feature for typeof and typeof_unqual, which get the type of their
argument as either fully qualified or fully unqualified. The argument
to either operator is either a type name or an expression. If given a
type name, the type information is pulled directly from the given name.
If given an expression, the type information is pulled from the
expression. Recursive use of these operators is allowed and has the
expected behavior (the innermost operator is resolved to a type, and
that's used to resolve the next layer of typeof specifier, until a
fully resolved type is determined.

Note, we already supported typeof in GNU mode as a non-conforming
extension and we are *not* exposing typeof_unqual as a non-conforming
extension in that mode, nor are we exposing typeof or typeof_unqual as
a nonconforming extension in other language modes. The GNU variant of
typeof supports a form where the parentheses are elided from the
operator when given an expression (e.g., typeof 0 i = 12;). When in C2x
mode, we do not support this extension.

Differential Revision: https://reviews.llvm.org/D134286
2022-09-28 13:27:52 -04:00
Yuanfang Chen f3e02989e6 [NFC][Clang] Remove dead code
And correct a few typos.
2022-09-26 18:32:52 -07:00
Erich Keane 684a78968b Reapply "[Concepts] Recover properly from a RecoveryExpr in a concept"
This reverts commit 192d69f7e6.

This fixes the condition to check whether this is a situation where we
are in a recovery-expr'ed concept a little better, so we don't access an
inactive member of a union, which should make the bots happy.

Differential Revision: https://reviews.llvm.org/D134542
2022-09-26 08:39:10 -07:00
Erich Keane 192d69f7e6 Revert "[Concepts] Recover properly from a RecoveryExpr in a concept"
This reverts commit e3d14bee23.

There are apparently a large number of crashes in libcxx and some JSON
Parser thing, so clearly this has some sort of serious issue.  Reverting
so I can take some time to figure out what is going on.
2022-09-26 06:55:25 -07:00
Erich Keane e3d14bee23 [Concepts] Recover properly from a RecoveryExpr in a concept
Discovered by reducing a different problem, we currently assert because
we failed to make the constraint expressions not dependent, since a
RecoveryExpr cannot be transformed.

This patch fixes that, and gets reasonably nice diagnostics by
introducing a concept (hah!) of "ContainsErrors" to the Satisfaction
types, which causes us to treat the candidate as non-viable.

However, just making THAT candidate non-viable would result in choosing
the 'next best' canddiate, which can result in awkward errors, where we
start evaluating a candidate that is not intended to be selected.
Because of this, and to make diagnostics more relevant, we now just
cause the entire lookup to result in a 'no-viable-candidates'.

This means we will only emit the list of candidates, rather than any
cascading failures.
2022-09-26 06:33:48 -07:00
Erich Keane babdef27c5 Re-apply "Deferred Concept Instantiation Implementation"
This reverts commit 95d94a6775.

This implements the deferred concepts instantiation, which should allow
the libstdc++ ranges to properly compile, and for the CRTP to work for
constrained functions.

Since the last attempt, this has fixed the issues from @wlei and
@mordante.

Differential Revision: https://reviews.llvm.org/D126907
2022-09-22 05:53:59 -07:00
Xiang Li 782ac2182c [HLSL] Support cbuffer/tbuffer for hlsl.
This is first part for support cbuffer/tbuffer.

The format for cbuffer/tbuffer is
BufferType [Name] [: register(b#)] { VariableDeclaration [: packoffset(c#.xyzw)]; ... };

More details at https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-constants

New keyword 'cbuffer' and 'tbuffer' are added.
New AST node HLSLBufferDecl is added.
Build AST for simple cbuffer/tbuffer without attribute support.

The special thing is variables declared inside cbuffer is exposed into global scope.
So isTransparentContext should return true for HLSLBuffer.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D129883
2022-09-21 10:07:43 -07:00
Matheus Izvekov ef4bbfe338
[clang] AST: SubstTemplateTypeParmType support for non-canonical underlying type
This change allows us to represent in the AST some specific
circumstances where we substitute a template parameter type
which is part of the underlying type of a previous substitution.

This presently happens in some circumstances dealing with
substitution of defaulted parameters of template template
parameters, and in some other cases during concepts substitution.

The main motivation for this change is for the future use in the
implementation of template specialization resugaring, as this will
allow us to represent a substitution with sugared types.

Signed-off-by: Matheus Izvekov <mizvekov@gmail.com>

Differential Revision: https://reviews.llvm.org/D132816
2022-09-21 14:22:19 +02:00
Xiang Li 649a59712f [clang] Allow vector of BitInt
Remove check which disable BitInt as element type for ext_vector.

Enabling it for HLSL to use _BitInt(16) as 16bit int at https://reviews.llvm.org/D133668

Reviewed By: erichkeane

Differential Revision: https://reviews.llvm.org/D133634
2022-09-19 09:26:56 -07:00
Kazu Hirata 981cbfb592 [clang] Don't include StringSwitch.h (NFC)
These files don't seem to use StringSwitch.
2022-09-18 22:21:32 -07:00
Xiang Li edbf36c5e9 [HLSL] [clang] Add vector version of abs for HLSL
Add vector version of abs as
```
__attribute__((clang_builtin_alias(__builtin_elementwise_abs)))
int2 abs (int2 );
__attribute__((clang_builtin_alias(__builtin_elementwise_abs)))
int3 abs (int3 );
```
To make this work.
Allowed custom type checking builtins to be recelareable.

Reviewed By: RKSimon

Differential Revision: https://reviews.llvm.org/D133737
2022-09-18 14:59:30 -07:00
Kazu Hirata 8009d236e5 [clang] Don't include SetVector.h (NFC) 2022-09-17 13:36:13 -07:00
Matheus Izvekov 52dce8900c
[clang] Fix AST representation of expanded template arguments.
Extend clang's SubstTemplateTypeParm to represent the pack substitution index.

Fixes PR56099.

Signed-off-by: Matheus Izvekov <mizvekov@gmail.com>

Differential Revision: https://reviews.llvm.org/D128113
2022-09-17 01:24:46 +02:00
Michael Buch e456d2ba8b [clang][ASTImporter] DeclContext::localUncachedLookup: Continue lookup into decl chain when regular lookup fails
The uncached lookup is mainly used in the ASTImporter/LLDB code-path
where we're not allowed to load from external storage. When importing
a FieldDecl with a DeclContext that had no external visible storage
(but came from a Clang module or PCH) the above call to `lookup(Name)`
the regular `DeclContext::lookup` fails because:
1. `DeclContext::buildLookup` doesn't set `LookupPtr` for decls
   that came from a module
2. LLDB doesn't use the `SharedImporterState`

In such a case we would never continue with the "slow" path of iterating
through the decl chain on the DeclContext. In some cases this means that
ASTNodeImporter::VisitFieldDecl ends up importing a decl into the
DeclContext a second time.

The patch removes the short-circuit in the case where we don't find
any decls via the regular lookup.

**Tests**

* Un-skip the failing LLDB API tests

Differential Revision: https://reviews.llvm.org/D133945
2022-09-16 12:38:50 -04:00
Matheus Izvekov f4ea3bd4b2
[clang] Fixes how we represent / emulate builtin templates
We change the template specialization of builtin templates to
behave like aliases.

Though unlike real alias templates, these might still produce a canonical
TemplateSpecializationType when some important argument is dependent.

For example, we can't do anything about make_integer_seq when the
count is dependent, or a type_pack_element when the index is dependent.

We change type deduction to not try to deduce canonical TSTs of
builtin templates.

We also change those buitin templates to produce substitution sugar,
just like a real instantiation would, making the resulting type correctly
represent the template arguments used to specialize the underlying template.

And make_integer_seq will now produce a TST for the specialization
of it's first argument, which we use as the underlying type of
the builtin alias.

When performing member access on the resulting type, it's now
possible to map from a Subst* node to the template argument
as-written used in a regular fashion, without special casing.

And this fixes a bunch of bugs with relation to these builtin
templates factoring into deduction.

Fixes GH42102 and GH51928.

Depends on D133261

Signed-off-by: Matheus Izvekov <mizvekov@gmail.com>

Differential Revision: https://reviews.llvm.org/D133262
2022-09-16 17:44:12 +02:00
Matheus Izvekov 00ce271712
[clang] extend getCommonSugaredType to merge sugar nodes
This continues D111283 by extending the getCommonSugaredType
implementation to also merge non-canonical type nodes.

We merge these nodes by going up starting from the canonical
node, calculating their merged properties on the way.

If we reach a pair that is too different, or which we could not
otherwise unify, we bail out and don't try to keep going on to
the next pair, in effect striping out all the remaining top-level
sugar nodes. This avoids mismatching 'companion' nodes, such as
ElaboratedType, so that they don't end up elaborating some other
unrelated thing.

Signed-off-by: Matheus Izvekov <mizvekov@gmail.com>

Differential Revision: https://reviews.llvm.org/D130308
2022-09-16 17:04:10 +02:00
Matheus Izvekov 989f76ce90
[clang] template / auto deduction deduces common sugar
After upgrading the type deduction machinery to retain type sugar in
D110216, we were left with a situation where there is no general
well behaved mechanism in Clang to unify the type sugar of multiple
deductions of the same type parameter.

So we ended up making an arbitrary choice: keep the sugar of the first
deduction, ignore subsequent ones.

In general, we already had this problem, but in a smaller scale.
The result of the conditional operator and many other binary ops
could benefit from such a mechanism.

This patch implements such a type sugar unification mechanism.

The basics:

This patch introduces a `getCommonSugaredType(QualType X, QualType Y)`
method to ASTContext which implements this functionality, and uses it
for unifying the results of type deduction and return type deduction.
This will return the most derived type sugar which occurs in both X and
Y.

Example:

Suppose we have these types:
```
using Animal = int;
using Cat = Animal;
using Dog = Animal;

using Tom = Cat;
using Spike = Dog;
using Tyke = Dog;
```
For `X = Tom, Y = Spike`, this will result in `Animal`.
For `X = Spike, Y = Tyke`, this will result in `Dog`.

How it works:

We take two types, X and Y, which we wish to unify as input.
These types must have the same (qualified or unqualified) canonical
type.

We dive down fast through top-level type sugar nodes, to the
underlying canonical node. If these canonical nodes differ, we
build a common one out of the two, unifying any sugar they had.
Note that this might involve a recursive call to unify any children
of those. We then return that canonical node, handling any qualifiers.

If they don't differ, we walk up the list of sugar type nodes we dived
through, finding the last identical pair, and returning that as the
result, again handling qualifiers.

Note that this patch will not unify sugar nodes if they are not
identical already. We will simply strip off top-level sugar nodes that
differ between X and Y. This sugar node unification will instead be
implemented in a subsequent patch.

This patch also implements a few users of this mechanism:
* Template argument deduction.
* Auto deduction, for functions returning auto / decltype(auto), with
  special handling for initializer_list as well.

Further users will be implemented in a subsequent patch.

Signed-off-by: Matheus Izvekov <mizvekov@gmail.com>

Differential Revision: https://reviews.llvm.org/D111283
2022-09-16 11:20:10 +02:00
Alexander Kornienko 637da9de4c Revert "[clang] template / auto deduction deduces common sugar"
This reverts commit d200db3863, which causes a
clang crash. See https://reviews.llvm.org/D111283#3785755

Test case for convenience:
```
template <typename T>
using P = int T::*;

template <typename T, typename... A>
void j(P<T>, T, A...);

template <typename T>
void j(P<T>, T);

struct S {
  int b;
};
void g(P<S> k, S s) { j(k, s); }
```
2022-09-13 12:18:07 +02:00
Timm Bäder 53d8687a13 [clang][Interp][NFC] Use constexpr if in OffsetHelper
Add is a template parameter, so we can use constexpr if here.
2022-09-13 10:41:34 +02:00
Timm Bäder b02f6890f5 [clang][Interp][NFC] InterpFrame::getParam can be const
Make stackRef() const as well and use that.
2022-09-13 10:41:28 +02:00
Timm Bäder 5d2451468c [clang][Interp][NFC] InterpFrame::getLocal() can be const
Make localRef() const and use that.
2022-09-13 10:41:24 +02:00
Timm Bäder f756ddba17 [clang][Interp][NFC] InterpFrame: localBlock() can be const 2022-09-13 10:41:18 +02:00
Timm Bäder 9b087a70e6 [clang][Interp][NFC] Forward-declare Pointer in PrimType header
No need to include the full Pointer.h here.
2022-09-13 10:40:52 +02:00
Timm Bäder bf3efa8b16 [clang][Interp] Handle DeclRefExpr of reference types
References are implemented through pointers, so we need a second deref
when encountering a DeclRefExpr of a reference type.

Differential Revision: https://reviews.llvm.org/D132997
2022-09-13 10:40:51 +02:00
Muhammad Usman Shahid ea26ed1f9c Rewording note note_constexpr_invalid_cast
The diagnostics here are correct, but the note is really silly. It
talks about reinterpret_cast in C code. So rewording it for c mode by
using another %select{}.
```
int array[(long)(char *)0];
```
previous note:
```
cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression
```
reworded note:
```
this conversion is not allowed in a constant expression
```

Differential Revision: https://reviews.llvm.org/D133194
2022-09-12 07:47:39 -04:00
Corentin Jabot 0e8afddaa1 [Clang] NFC: Remove duplicated variable def in CheckLValueConstantExpression 2022-09-10 19:10:23 +02:00
Joe Loser 1b3a78d1d5 [clang] Use std::size instead of llvm::array_lengthof
LLVM contains a helpful function for getting the size of a C-style
array: `llvm::array_lengthof`. This is useful prior to C++17, but not as
helpful for C++17 or later: `std::size` already has support for C-style
arrays.

Change call sites to use `std::size` instead. Leave the few call sites that
use a locally defined `array_lengthof` that are meant to test previous bugs
with NTTPs in clang analyzer and SemaTemplate.

Differential Revision: https://reviews.llvm.org/D133520
2022-09-08 17:20:25 -06:00
Fangrui Song 48203b327d Revert D130308 "[clang] extend getCommonSugaredType to merge sugar nodes"
This reverts commit 16e5d6d7f9.
There are multiple complaints on the review.

In addition, it may cause spurious
```
error: invalid operands to binary expression ('SinkPrinter' and 'char[cluster_name_length]')
note: candidate template ignored: substitution failure: variably modified type 'char *' cannot be used as a template argument SinkPrinter operator<<(const SinkPrinter &s, T) {
```
for some C++ code
2022-09-08 15:09:39 -07:00
Fangrui Song 2f23dc9127 [AST/Interp] Fix left shift overflow bug in D64146
Noticed by Dmitri Gribenko
2022-09-08 13:49:07 -07:00
Fangrui Song 49c289879c [AST/Interp] Fix latent unitialized use of memory bug in D64146
Exposed by D132727: clang/test/AST/Interp/arrays.cpp fails with --config=msan
2022-09-08 13:39:30 -07:00
Matheus Izvekov 16e5d6d7f9
[clang] extend getCommonSugaredType to merge sugar nodes
This continues D111283 by extending the getCommonSugaredType
implementation to also merge non-canonical type nodes.

We merge these nodes by going up starting from the canonical
node, calculating their merged properties on the way.

If we reach a pair that is too different, or which we could not
otherwise unify, we bail out and don't try to keep going on to
the next pair, in effect striping out all the remaining top-level
sugar nodes. This avoids mismatching 'companion' nodes, such as
ElaboratedType, so that they don't end up elaborating some other
unrelated thing.

Depends on D111509

Signed-off-by: Matheus Izvekov <mizvekov@gmail.com>

Differential Revision: https://reviews.llvm.org/D130308
2022-09-08 19:17:53 +02:00
Matheus Izvekov d200db3863
[clang] template / auto deduction deduces common sugar
After upgrading the type deduction machinery to retain type sugar in
D110216, we were left with a situation where there is no general
well behaved mechanism in Clang to unify the type sugar of multiple
deductions of the same type parameter.

So we ended up making an arbitrary choice: keep the sugar of the first
deduction, ignore subsequent ones.

In general, we already had this problem, but in a smaller scale.
The result of the conditional operator and many other binary ops
could benefit from such a mechanism.

This patch implements such a type sugar unification mechanism.

The basics:

This patch introduces a `getCommonSugaredType(QualType X, QualType Y)`
method to ASTContext which implements this functionality, and uses it
for unifying the results of type deduction and return type deduction.
This will return the most derived type sugar which occurs in both X and
Y.

Example:

Suppose we have these types:
```
using Animal = int;
using Cat = Animal;
using Dog = Animal;

using Tom = Cat;
using Spike = Dog;
using Tyke = Dog;
```
For `X = Tom, Y = Spike`, this will result in `Animal`.
For `X = Spike, Y = Tyke`, this will result in `Dog`.

How it works:

We take two types, X and Y, which we wish to unify as input.
These types must have the same (qualified or unqualified) canonical
type.

We dive down fast through top-level type sugar nodes, to the
underlying canonical node. If these canonical nodes differ, we
build a common one out of the two, unifying any sugar they had.
Note that this might involve a recursive call to unify any children
of those. We then return that canonical node, handling any qualifiers.

If they don't differ, we walk up the list of sugar type nodes we dived
through, finding the last identical pair, and returning that as the
result, again handling qualifiers.

Note that this patch will not unify sugar nodes if they are not
identical already. We will simply strip off top-level sugar nodes that
differ between X and Y. This sugar node unification will instead be
implemented in a subsequent patch.

This patch also implements a few users of this mechanism:
* Template argument deduction.
* Auto deduction, for functions returning auto / decltype(auto), with
  special handling for initializer_list as well.

Further users will be implemented in a subsequent patch.

Signed-off-by: Matheus Izvekov <mizvekov@gmail.com>

Differential Revision: https://reviews.llvm.org/D111283
2022-09-08 19:17:48 +02:00
Kadir Cetinkaya abd2b1a9d0
[clang] Fix a crash in constant evaluation 2022-09-08 18:21:44 +02:00
Timm Bäder 86271798e5 [clang][Interp] Only initialize initmaps for primitive arrays
As the comment states, this code should only run for primitive arrays.

This should fix the memory sanitize builds.
2022-09-08 10:03:06 +02:00
Timm Bäder 651f4ce7b1 [clang][Interp][NFC] Use constexpr if when possible in Integral.h 2022-09-08 07:31:08 +02:00
Timm Bäder 0da7e409e0 [clang][Interp][NFC] Context::classify() can be const 2022-09-08 07:31:08 +02:00
Timm Bäder 3a7d476087 [clang][Interp] Implement array initializers and subscript expressions
Differential Revision: https://reviews.llvm.org/D132727
2022-09-08 07:31:07 +02:00
Timm Bäder aa7c5c9c4e [clang][Interp] Handle missing local initializers better
This is illegal in a constexpr context. We can already figure that out,
but we'd still run into an assertion later on when trying to visit the
missing initializer or run the invalid function.

Differential Revision: https://reviews.llvm.org/D132832
2022-09-08 07:31:07 +02:00
Timm Bäder 5c4dbff0b6 [clang][Interp] Handle SubstNonTypeTemplateParmExprs
Differential Revision: https://reviews.llvm.org/D132831
2022-09-08 07:31:07 +02:00
Timm Bäder 4d700ffe67 [clang][Interp] Implement ImplicitValueInitExprs
Take the existing Zero opcode and emit it.

Differential Revision: https://reviews.llvm.org/D132829
2022-09-08 07:31:07 +02:00
Timm Bäder 95e6a407d9 [clang][Interp] Implement IntegralToBoolean casts
Redo how we do IntegralCasts and implement IntegralToBoolean casts using
the already existing cast op.

Differential Revision: https://reviews.llvm.org/D132739
2022-09-08 07:31:07 +02:00
Timm Bäder 8e41e6a4ea [clang][Interp] Implement function calls
Add Call() and CallVoid() ops and use them to call functions. Only
FunctionDecls are supported for now.

Differential Revision: https://reviews.llvm.org/D132286
2022-09-08 07:31:07 +02:00
Timm Bäder 5777c05d16 [clang] Perform implicit lvalue-to-rvalue cast with new interpreter
The EvaluateAsRValue() documentation mentions that an implicit
lvalue-to-rvalue cast is being performed if the result is an lvalue.
However, that was not being done if the new constant interpreter was in
use.

Just always do it.

Differential Revision: https://reviews.llvm.org/D132136
2022-09-08 07:31:07 +02:00
Volodymyr Sapsai 3e7350f317 [ODRHash diagnostics] Move `ODRDiagsEmitter` to libAST in separate files. NFC.
Intend to use `ODRDiagsEmitter` during parsing to diagnose a parsed
definition differing from a definition with the same name from a hidden
[sub]module.

Differential Revision: https://reviews.llvm.org/D128695
2022-09-07 14:40:37 -07:00
Matheus Izvekov acb767f5cd
[clang] fix profiling of template arguments of template and declaration kind
Template arguments of template and declaration kind were being profiled
only by their canonical properties, which would cause incorrect
uniquing of constrained AutoTypes, leading to a crash in some cases.

This exposed some places in CheckTemplateArgumentList where non-canonical
arguments where being pushed into the resulting converted list.

We also throw in some asserts to catch early and explain the crashes.

Note that the fix for the 'declaration' kind is untestable at this point,
because there should be no cases right now in the AST where we try
to unique a non-canonical converted template argument.

This fixes GH55567.

Signed-off-by: Matheus Izvekov <mizvekov@gmail.com>

Differential Revision: https://reviews.llvm.org/D133072
2022-09-06 18:27:39 +02:00
Nathan Ridge 898c421975 [clangd] Avoid crash when printing call to string literal operator template
Differential Revision: https://reviews.llvm.org/D132830
2022-09-05 03:42:08 -04:00
Kazu Hirata b7a7aeee90 [clang] Qualify auto in range-based for loops (NFC) 2022-09-03 23:27:27 -07:00
Egor Zhdan 0cb0809336 [Clang][Comments] Parse `<img src=""/>` in doc comments correctly
This is a valid HTML5 tag. Previously it triggered a Clang error (`HTML start tag prematurely ended, expected attribute name or '>'`) since Clang was treating `/>` as a text token. This was happening because after lexing the closing quote (`"`) the lexer state was reset to "Normal" while the tag was not actually closed yet: `>` was not yet parsed at that point.

rdar://91464292

Differential Revision: https://reviews.llvm.org/D132932
2022-09-01 13:17:42 +01:00
YingChi Long e3bd67eddf
[clang][Sema] check default argument promotions for printf
The main focus of this patch is to make ArgType::matchesType check for
possible default parameter promotions when the argType is not a pointer.
If so, no warning will be given for `int`, `unsigned int` types as
corresponding arguments to %hhd and %hd. However, the usage of %hhd
corresponding to short is relatively rare, and it is more likely to be a
misuse. This patch keeps the original behavior of clang like this as
much as possible, while making it more convenient to consider the
default arguments promotion.

Fixes https://github.com/llvm/llvm-project/issues/57102

Reviewed By: aaron.ballman, nickdesaulniers, #clang-language-wg

Differential Revision: https://reviews.llvm.org/D132568
2022-09-01 10:10:10 +08:00
Kadir Cetinkaya b58ed43a7f
Revert "[clang] Fix a crash in constant evaluation"
This reverts commit a5ab650714.
2022-08-31 10:12:52 +02:00