3303 Commits

Author SHA1 Message Date
Karl-Johan Karlsson
d25e0aac41
[Sema] Fixed faulty shift count warning (#69521)
Constant values of _BitInt have the bitwith to exactly fit the constant
number. This patch fix a problem in Sema when building an APInt where
the supplied bitwidth can become too small and simply truncate the value
leading to a faulty warning.
2023-10-25 09:48:01 +02:00
Kazu Hirata
f9306f6de3
[ADT] Rename llvm::erase_value to llvm::erase (NFC) (#70156)
C++20 comes with std::erase to erase a value from std::vector.  This
patch renames llvm::erase_value to llvm::erase for consistency with
C++20.

We could make llvm::erase more similar to std::erase by having it
return the number of elements removed, but I'm not doing that for now
because nobody seems to care about that in our code base.

Since there are only 50 occurrences of erase_value in our code base,
this patch replaces all of them with llvm::erase and deprecates
llvm::erase_value.
2023-10-24 23:03:13 -07:00
Lawrence Benson
de65b6bec6
[Clang] Add __builtin_vectorelements to get number of elements in vector (#69010)
Adds a new `__builtin_vectorelements()` function which returns the
number of elements for a given vector either at compile-time for
fixed-sized vectors, e.g., created via `__attribute__((vector_size(N)))`
or at runtime via a call to `@llvm.vscale.i32()` for scalable vectors,
e.g., SVE or RISCV V.

The new builtin follows a similar path as `sizeof()`, as it essentially
does the same thing but for the number of elements in vector instead of
the number of bytes. This allows us to re-use a lot of the existing
logic to handle types etc.

A small side addition is `Type::isSizelessVectorType()`, which we need
to distinguish between sizeless vectors (SVE, RISCV V) and sizeless
types (WASM).

This is the [corresponding
discussion](https://discourse.llvm.org/t/new-builtin-function-to-get-number-of-lanes-in-simd-vectors/73911).
2023-10-19 10:45:08 +02:00
Aaron Ballman
343bed8d3a Canonically identical types are allowed in compound expressions in C
We did not have a catch-all for when the two operand types are
identical after canonicalization. Instead, we handled that on a case by
case basis. Thus, we would diagnose code like:
```
mat4 test(int a) {
  typedef float mat4 __attribute((matrix_type(4, 4)));
  mat4 transform;
  return (a > 0) ? transform : transform;
}
```
This simplifies the logic and will be more forwards
compatible with other extended datatypes.

Fixes https://github.com/llvm/llvm-project/issues/69008
2023-10-14 11:03:35 -04:00
Bill Wendling
769bc11f68
[Clang] Implement the 'counted_by' attribute (#68750)
The 'counted_by' attribute is used on flexible array members. The
argument for the attribute is the name of the field member in the same
structure holding the count of elements in the flexible array. This
information can be used to improve the results of the array bound
sanitizer and the '__builtin_dynamic_object_size' builtin.

This example specifies the that the flexible array member 'array' has
the number of elements allocated for it in 'count':

  struct bar;
  struct foo {
    size_t count;
     /* ... */
    struct bar *array[] __attribute__((counted_by(count)));
  };

This establishes a relationship between 'array' and 'count',
specifically that 'p->array' must have *at least* 'p->count' number of
elements available. It's the user's responsibility to ensure that this
relationship is maintained through changes to the structure.

In the following, the allocated array erroneously has fewer elements
than what's specified by 'p->count'. This would result in an
out-of-bounds access not not being detected:

  struct foo *p;

  void foo_alloc(size_t count) {
    p = malloc(MAX(sizeof(struct foo),
                   offsetof(struct foo, array[0]) + count *
                       sizeof(struct bar *)));
    p->count = count + 42;
  }

The next example updates 'p->count', breaking the relationship
requirement that 'p->array' must have at least 'p->count' number of
elements available:

  struct foo *p;

  void foo_alloc(size_t count) {
    p = malloc(MAX(sizeof(struct foo),
                   offsetof(struct foo, array[0]) + count *
                       sizeof(struct bar *)));
    p->count = count + 42;
  }

  void use_foo(int index) {
    p->count += 42;
    p->array[index] = 0; /* The sanitizer cannot properly check this access */
  }

Reviewed By: nickdesaulniers, aaron.ballman

Differential Revision: https://reviews.llvm.org/D148381
2023-10-14 04:18:02 -07:00
Congcong Cai
19d1da59dc
[clang]Avoid diagnose invalid consteval call for invalid function decl (#68646)
Fixes:#68542
It‘s meaningless to diagnose further error for invalid function
declaration.
2023-10-10 15:32:10 +08:00
kasuga-fj
5fa5ffeb6c
[Clang] Fix missing diagnostic for non-standard layout type in offsetof (#65246)
Fixes #64619

Clang warns diagnostic for non-standard layout types in `offsetof` only
if they are in evaluated context. With this patch, you'll also get
diagnostic if you use `offsetof` on non-standard layout types in any
other contexts
2023-10-10 15:31:21 +09:00
cor3ntin
c72d3a0966
[Clang] Handle consteval expression in array bounds expressions (#66222)
The bounds of a c++ array is a _constant-expression_. And in C++ it is
also a constant expression.

But we also support VLAs, ie arrays with non-constant bounds.

We need to take care to handle the case of a consteval function (which
are specified to be only immediately called in non-constant contexts)
that appear in arrays bounds.

This introduces `Sema::isAlwayConstantEvaluatedContext`, and a flag in
ExpressionEvaluationContextRecord, such that immediate functions in
array bounds are always immediately invoked.

Sema had both `isConstantEvaluatedContext` and
`isConstantEvaluated`, so I took the opportunity to cleanup that.

The change in `TimeProfilerTest.cpp` is an unfortunate manifestation of
the problem that #66203 seeks to address.

Fixes #65520
2023-10-05 11:36:27 +02:00
zyn0217
077e1b892d
[clang] Preserve UDL nodes in RemoveNestedImmediateInvocation (#66641)
D63960 performs a tree transformation on AST to prevent evaluating
constant expressions eagerly by removing recorded immediate consteval
invocations from subexpressions. (See
https://reviews.llvm.org/D63960#inline-600736 for its motivation.)

However, the UDL node has been replaced with a CallExpr in the default
TreeTransform since ca844ab0 (inadvertently?). This confuses clangd as
it relies on the exact AST node type to decide whether or not to present
inlay hints for an expression.

With regard to the fix, I think it's enough to return the UDL expression
as-is during the transformation: We've bound it to temporary in its
construction, and there's no ConstantExpr to visit under a UDL.

Fixes https://github.com/llvm/llvm-project/issues/63898.
2023-10-04 04:45:46 -05:00
Alex Voicu
4d680f5647 [HIP][Clang][Sema] Add Sema support for hipstdpar
This patch adds the Sema changes needed for enabling HIP parallel algorithm offload on AMDGPU targets. This change impacts the CUDA / HIP language specific checks, and only manifests if compiling in `hipstdpar` mode. In this case, we essentially do three things:

1. Allow device side callers to call host side callees - since the user visible HLL would be standard C++, with no annotations / restriction mechanisms, we cannot unambiguously establish that such a call is an error, so we conservatively allow all such calls, deferring actual cleanup to a subsequent pass over IR;
2. Allow host formed lambdas to capture by reference;
3. Allow device functions to use host global variables.

Reviewed by: yaxunl

Differential Revision: https://reviews.llvm.org/D155833
2023-10-03 13:29:12 +01:00
Corentin Jabot
af4751738d [C++] Implement "Deducing this" (P0847R7)
This patch implements P0847R7 (partially),
CWG2561 and CWG2653.

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

Differential Revision: https://reviews.llvm.org/D140828
2023-10-02 14:33:02 +02:00
Sam McCall
880fa7faa9 Revert "[clang][SemaCXX] Diagnose tautological uses of consteval if and is_constant_evaluated"
This reverts commit 491b2810fb7fe5f080fa9c4f5945ed0a6909dc92.

This change broke valid code and generated incorrect diagnostics, see
https://reviews.llvm.org/D155064
2023-09-27 18:58:01 +02:00
Takuya Shimizu
491b2810fb [clang][SemaCXX] Diagnose tautological uses of consteval if and is_constant_evaluated
This patch makes clang diagnose extensive cases of consteval if and is_constant_evaluated usage that are tautologically true or false.
This introduces a new IsRuntimeEvaluated boolean flag to Sema::ExpressionEvaluationContextRecord that means the immediate appearance of if consteval or is_constant_evaluated are tautologically false(e.g. inside if !consteval {} block or non-constexpr-qualified function definition body)
This patch also pushes new expression evaluation context when parsing the condition of if constexpr and initializer of constexpr variables so that Sema can be aware that the use of consteval if and is_consteval are tautologically true in if constexpr condition and constexpr variable initializers.
BEFORE this patch, the warning for is_constant_evaluated was emitted from constant evaluator. This patch moves the warning logic to Sema in order to diagnose tautological use of is_constant_evaluated in the same way as consteval if.

This patch separates initializer evaluation context from InitializerScopeRAII.
This fixes a bug that was happening when user takes address of function address in initializers of non-local variables.

Fixes https://github.com/llvm/llvm-project/issues/43760
Fixes https://github.com/llvm/llvm-project/issues/51567

Reviewed By: cor3ntin, ldionne
Differential Revision: https://reviews.llvm.org/D155064
2023-09-27 09:26:06 +09:00
Yaxun (Sam) Liu
f2a1331a01
[CUDA][HIP] Do not mark extern shared var (#65990)
Fixes: https://github.com/llvm/llvm-project/issues/65806

Currently clang put extern shared var ODR-used by host device functions
in global var __clang_gpu_used_external. This behavior was due to
https://reviews.llvm.org/D123441. However, clang should not do that for
extern shared vars since their addresses are per warp, therefore cannot
be accessed by host code.
2023-09-11 17:04:55 -04:00
Chris Bieneman
400d3261a0 [HLSL] Cleanup support for this as an l-value
The goal of this change is to clean up some of the code surrounding
HLSL using CXXThisExpr as a non-pointer l-value. This change cleans up
a bunch of assumptions and inconsistencies around how the type of
`this` is handled through the AST and code generation.

This change is be mostly NFC for HLSL, and completely NFC for other
language modes.

This change introduces a new member to query for the this object's type
and seeks to clarify the normal usages of the this type.

With the introudction of HLSL to clang, CXXThisExpr may now be an
l-value and behave like a reference type rather than C++'s normal
method of it being an r-value of pointer type.

With this change there are now three ways in which a caller might need
to query the type of `this`:

* The type of the `CXXThisExpr`
* The type of the object `this` referrs to
* The type of the implicit (or explicit) `this` argument

This change codifies those three ways you may need to query
respectively as:

* CXXMethodDecl::getThisType()
* CXXMethodDecl::getThisObjectType()
* CXXMethodDecl::getThisArgType()

This change then revisits all uses of `getThisType()`, and in cases
where the only use was to resolve the pointee type, it replaces the
call with `getThisObjectType()`. In other cases it evaluates whether
the desired returned type is the type of the `this` expr, or the type
of the `this` function argument. The `this` expr type is used for
creating additional expr AST nodes and for member lookup, while the
argument type is used mostly for code generation.

Additionally some cases that used `getThisType` in simple queries could
be substituted for `getThisObjectType`. Since `getThisType` is
implemented in terms of `getThisObjectType` calling the later should be
more efficient if the former isn't needed.

Reviewed By: aaron.ballman, bogner

Differential Revision: https://reviews.llvm.org/D159247
2023-09-05 19:38:50 -05:00
Richard Dzenis
e6d305e64f Add support of Windows Trace Logging macros
Consider the following code:

    #include <windows.h>
    #include <TraceLoggingActivity.h>
    #include <TraceLoggingProvider.h>
    #include <winmeta.h>

    TRACELOGGING_DEFINE_PROVIDER(
        g_hMyComponentProvider,
        "SimpleTraceLoggingProvider",
        // {0205c616-cf97-5c11-9756-56a2cee02ca7}
        (0x0205c616,0xcf97,0x5c11,0x97,0x56,0x56,0xa2,0xce,0xe0,0x2c,0xa7));

    void test()
    {
        TraceLoggingFunction(g_hMyComponentProvider);
    }

    int main()
    {
        TraceLoggingRegister(g_hMyComponentProvider);
        test();
        TraceLoggingUnregister(g_hMyComponentProvider);
    }

It compiles with MSVC, but clang-cl reports an error:

    C:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\shared/TraceLoggingActivity.h(377,30): note: expanded from macro '_tlgThisFunctionName'
    #define _tlgThisFunctionName __FUNCTION__
                                 ^
    .\tl.cpp(14,5): error: cannot initialize an array element of type 'char' with an lvalue of type 'const char[5]'
        TraceLoggingFunction(g_hMyComponentProvider);
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The second commit is not needed to support above code, however, during isolated tests in ms_predefined_expr.cpp
I found that MSVC accepts code with constexpr, whereas clang-cl does not.
I see that in most places PredefinedExpr is supported in constant evaluation, so I didn't wrap my code with ``if(MicrosoftExt)``.

Reviewed By: cor3ntin

Differential Revision: https://reviews.llvm.org/D158591
2023-09-04 16:54:42 +02:00
Antonio Abbatangelo
fa1dc06a1b [clang][X86] Update excessive register save diagnostic to more closely follow the interrupt attribute spec
The original diagnostic does not cover all cases according to my reading
of the spec.

For the interrupt attribute, the spec indicates that if the compiler
does not support saving SSE, MMX, or x87 then the function should
be compiled with '-mgeneral-regs-only' (GCC requires this).
Alternatively, calling functions with the `no_caller_saved_registers`
attribute will not clobber state and can be done without disabling
these features.

The warning as implemented in upstream only detects the latter case but
does not consider that disabling the above features also solves the issue
of these register saves being undesirable due to inefficiency.

For the no_caller_saved_registers attribute, the interrupt spec also
indicates that in the absence of saving SSE, MMX and x87 state, these
functions should be compiled with '-mgeneral-regs-only' (also required
by GCC). It does not make any statements about calling other functions
with the attribute, but by extension the result is the same as with the
interrupt attribute (in clang, at least).

This patch handles the remaining cases by adjusting the diagnostic to:

1. Not be shown if the function is compiled without the SSE, MMX or x87
   features enabled (i.e. with '-mgeneral-regs-only')
2. Also be shown for functions with the 'no_caller_saved_registers'
   attribute
3. In addition to advising that the function should only call functions
   with the `no_caller_saved_registers` attribute, the text also suggests
   compiling with `-mgeneral-regs-only` as an alternative.

The interrupt spec is available at https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=5ed3cc7b66af4758f7849ed6f65f4365be8223be
and was taken from the issue that resulted in this diagnostic being
added (#26787)

Reviewed By: pengfei

Differential Revision: https://reviews.llvm.org/D159068
2023-08-30 11:52:04 +08:00
Corentin Jabot
6824d156d5 [Clang] Fix a crash when an invalid immediate function call appears in a cast
Fixes #64949

Reviewed By: Fznamznon, erichkeane, shafik

Differential Revision: https://reviews.llvm.org/D158733
2023-08-25 08:50:41 +02:00
Corentin Jabot
158f4f30ad [Clang] Do not change the type of captured vars when checking lambda constraints
When checking the constraint of a lambda, we need to respect the constness
of the call operator when establishing the type of capture variables.

In D124351, this was done by adding const to the captured variable...
However, that would change the type of the variable outside of the scope
of the lambda, which is clearly not the desired outcome.

Instead, to ensure const-correctness, we need to populate
a LambdaScopeInfo with the capture variables before checking the
constraints of a generic lambda.

There is no changelog as I'd like to tentatively propose we backport
this change to RC3 as it is a regression introduced in the Clang 17
cycle.

Fixes #61267

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

Differential Revision: https://reviews.llvm.org/D158433
2023-08-24 16:10:08 +02:00
Timm Bäder
e1dfbc4e24 [clang][Sema][NFC] Modernize ActOnCallExpr 2023-08-20 10:37:58 +02:00
Jianjian GUAN
28741a23c9 [clang][SVE] Rename isVLSTBuiltinType, NFC
Since we also have VLST for rvv now, it is not clear to keep using `isVLSTBuiltinType`, so I added prefix SVE to it.

Reviewed By: paulwalker-arm

Differential Revision: https://reviews.llvm.org/D158045
2023-08-17 14:18:32 +08:00
Aaron Ballman
9c4ade0623 [C23] Rename C2x->C23 in diagnostics
This renames C2x to C23 in diagnostic identifiers and messages. The
changes were made mechanically.
2023-08-11 08:42:01 -04:00
Aaron Ballman
0ce056a814 [C23] Rename C2x -> C23; NFC
This does the rename for most internal uses of C2x, but does not rename
or reword diagnostics (those will be done in a follow-up).

I also updated standards references and citations to the final wording
in the standard.
2023-08-11 07:43:43 -04:00
Richard Dzenis
66c43fbd27 Enable concatenation of predefined identifiers
Predefined identifiers like __FUNCTION__ are treated like string
literals in MSVC, which means they can be concatentated together with
an adjacent string literal. Clang now supports this behavior as well,
in Microsoft extensions mode.

Fixes https://github.com/llvm/llvm-project/issues/63563
Differential Revision: https://reviews.llvm.org/D153914
2023-08-09 13:55:03 -04:00
Sander de Smalen
28b5f3087a [Clang][AArch64] Add/implement ACLE keywords for SME.
This patch adds all the language-level function keywords defined in:

  https://github.com/ARM-software/acle/pull/188 (merged)
  https://github.com/ARM-software/acle/pull/261 (update after D148700 landed)

The keywords are used to control PSTATE.ZA and PSTATE.SM, which are
respectively used for enabling the use of the ZA matrix array and Streaming
mode. This information needs to be available on call sites, since the use
of ZA or streaming mode may have to be enabled or disabled around the
call-site (depending on the IR attributes set on the caller and the
callee). For calls to functions from a function pointer, there is no IR
declaration available, so the IR attributes must be added explicitly to the
call-site.

With the exception of '__arm_locally_streaming' and '__arm_new_za' the
information is part of the function's interface, not just the function
definition, and thus needs to be propagated through the
FunctionProtoType::ExtProtoInfo.

This patch adds the defintions of these keywords, as well as codegen and
semantic analysis to ensure conversions between function pointers are valid
and that no conflicting keywords are set. For example, '__arm_streaming'
and '__arm_streaming_compatible' are mutually exclusive.

Differential Revision: https://reviews.llvm.org/D127762
2023-08-08 07:00:59 +00:00
Shivam Gupta
a845252337 Revert "[Clang] Fix -Wconstant-logical-operand when LHS is a constant"
This reverts commit dfdfd306cfaf54fbc43e2d5eb36489dac3eb9976.

An issue is reported for wrong warning, this has to be reconsidered.

Differential Revision: https://reviews.llvm.org/D157352
2023-08-08 08:00:02 +05:30
Corentin Jabot
a8bef8865e [Clang] Implement P2169 A nice placeholder with no name
This is a C++ feature that allows the use of `_` to
declare multiple variable of that name in the same scope;
these variables can then not be referred to.

In addition, while P2169 does not extend to parameter
declarations, we stop warning on unused parameters of that name,
for consistency.

The feature is backported to all C++ language modes.

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

Differential Revision: https://reviews.llvm.org/D153536
2023-08-04 16:51:15 +02:00
Aaron Ballman
9f72df7e30 Fixing a formatting mistake; NFC
There was a brace that was hanging out in the middle of nowhere, this
fixes that issue.
2023-08-03 08:07:59 -04:00
Corentin Jabot
8698262a43 [Clang] Fix consteval propagation for aggregates and defaulted constructors
This patch does a few things:

* Fix aggregate initialization.
  When an aggregate has an initializer that is immediate-escalating,
  the context in which it is used automatically becomes an immediate function.
  The wording does that by rpretending an aggregate initialization is itself
  an invocation which is not really how clang works, so my previous attempt
  was... wrong.

* Fix initialization of defaulted constructors with immediate escalating
  default member initializers.
  The wording was silent about that case and I did not handled it fully
  https://cplusplus.github.io/CWG/issues/2760.html

* Fix diagnostics
  In some cases clang would produce additional and unhelpful
  diagnostics by listing the invalid references to consteval
  function that appear in immediate escalating functions

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

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

Differential Revision: https://reviews.llvm.org/D155175
2023-07-24 18:15:38 +02:00
dingfei
3cb16f6d2d [Sema][ObjC] Invalidate BlockDecl with invalid ParmVarDecl
BlockDecl should be invalidated because of its invalid ParmVarDecl.

Fixes #1 of https://github.com/llvm/llvm-project/issues/64005

Differential Revision: https://reviews.llvm.org/D155984
2023-07-24 18:32:46 +08:00
dingfei
106bde9ab2 [Sema][ObjC] Invalidate BlockDecl with invalid return expr & its parent BlockExpr
Invalidate BlockDecl with implicit return type, in case any of the return value exprs is invalid.
Propagating the error info up by replacing BlockExpr with a RecoveryExpr.

The idea of this fix is given by @hokein(Haojian Wu)

Fix https://github.com/llvm/llvm-project/issues/63863.

Differential Revision: https://reviews.llvm.org/D155396
2023-07-24 18:24:32 +08:00
Corentin Jabot
2021910606 [Clang] Diagnose jumps into statement expressions
Such jumps are not allowed by GCC and allowing them
can lead to situations where we jumps into unevaluated
statements.

Fixes #63682

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

Differential Revision: https://reviews.llvm.org/D154696
2023-07-21 15:08:51 +02:00
Sindhu Chittireddy
2ce662c5d5 [NFC] Remove needless nullchecks.
Differential Revision: https://reviews.llvm.org/D155774
2023-07-20 23:33:33 -07:00
Shivam Gupta
dfdfd306cf [Clang] Fix -Wconstant-logical-operand when LHS is a constant
This fix PR37919

The below code produces -Wconstant-logical-operand for the first statement,
but not the second.

void foo(int x) {
if (x && 5) {}
if (5 && x) {}
}

Reviewed By: nickdesaulniers

Differential Revision: https://reviews.llvm.org/D142609
2023-07-19 12:12:11 +05:30
Corentin Jabot
7d54aae2f1 Revert "[Clang] Diagnose jumps into statement expressions"
This reverts commit b0cc947b5d0a74f4ffe63c53b32978b21498e72e.

Breaks in presence of asm goto
https://reviews.llvm.org/D154696#4493805
2023-07-12 17:54:16 +02:00
Corentin Jabot
b0cc947b5d [Clang] Diagnose jumps into statement expressions
Such jumps are not allowed by GCC and allowing them
can lead to situations where we jumps into unevaluated
statements.

Fixes #63682

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

Differential Revision: https://reviews.llvm.org/D154696
2023-07-11 21:41:14 +02:00
Sergio Afonso
63ca93c7d1
[OpenMP][OMPIRBuilder] Rename IsEmbedded and IsTargetCodegen flags
This patch renames the `OpenMPIRBuilderConfig` flags to reduce confusion over
their meaning. `IsTargetCodegen` becomes `IsGPU`, whereas `IsEmbedded` becomes
`IsTargetDevice`. The `-fopenmp-is-device` compiler option is also renamed to
`-fopenmp-is-target-device` and the `omp.is_device` MLIR attribute is renamed
to `omp.is_target_device`. Getters and setters of all these renamed properties
are also updated accordingly. Many unit tests have been updated to use the new
names, but an alias for the `-fopenmp-is-device` option is created so that
external programs do not stop working after the name change.

`IsGPU` is set when the target triple is AMDGCN or NVIDIA PTX, and it is only
valid if `IsTargetDevice` is specified as well. `IsTargetDevice` is set by the
`-fopenmp-is-target-device` compiler frontend option, which is only added to
the OpenMP device invocation for offloading-enabled programs.

Differential Revision: https://reviews.llvm.org/D154591
2023-07-10 14:14:16 +01:00
Corentin Jabot
95f50964fb Implement P2361 Unevaluated string literals
This patch proposes to handle in an uniform fashion
the parsing of strings that are never evaluated,
in asm statement, static assert, attrributes, extern,
etc.

Unevaluated strings are UTF-8 internally and so currently
behave as narrow strings, but these things will diverge with
D93031.

The big question both for this patch and the P2361 paper
is whether we risk breaking code by disallowing
encoding prefixes in this context.
I hope this patch may allow to gather some data on that.

Future work:
Improve the rendering of unicode characters, line break
and so forth in static-assert messages

Reviewed By: aaron.ballman, shafik

Differential Revision: https://reviews.llvm.org/D105759
2023-07-07 13:30:27 +02:00
Mariya Podchishchaeva
550fa4eabd [clang] Do not discard cleanups while processing immediate invocation
Since an immediate invocation is a full expression itself - it requires
an additional ExprWithCleanups node, but it can participate to a bigger
full expression which actually requires cleanups to be run after.

Thanks @ilya-biryukov for helping reducing the reproducer and confirming
that the analysis is correct.

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

Reviewed By: ilya-biryukov

Differential Revision: https://reviews.llvm.org/D153962
2023-06-30 04:50:08 -04:00
Takuya Shimizu
409a8097c5 [clang][Diagnostics] Provide parameter source range to arity-mismatch notes
Consider the following piece of code:
```
void func( int aa,
           int bb,
           int cc) {}

void arity_mismatch() {
  func(2, 4);
}
```
BEFORE:
```
source.cpp:6:3: error: no matching function for call to 'func'
    6 |   func(2, 4);
      |   ^~~~
source.cpp:1:6: note: candidate function not viable: requires 3 arguments, but 2 were provided
    1 | void func( int aa,
      |      ^
```
AFTER:
```
source.cpp:6:3: error: no matching function for call to 'func'
    6 |   func(2, 4);
      |   ^~~~
source.cpp:1:6: note: candidate function not viable: requires 3 arguments, but 2 were provided
    1 | void func( int aa,
      |      ^     ~~~~~~~
    2 |            int bb,
      |            ~~~~~~~
    3 |            int cc) {}
      |            ~~~~~~
```

Reviewed By: cjdb, aaron.ballman

Differential Revision: https://reviews.llvm.org/D153267
2023-06-26 00:27:15 +09:00
Zahira Ammarguellat
63b0b82fd6 When float_t and double_t types are used inside a scope with
a '#pragma clang fp eval_method, it can lead to ABI breakage.
See https://godbolt.org/z/56zG4Wo91
This patch prevents this.

Differential Revision: https://reviews.llvm.org/D153590
2023-06-23 15:12:51 -04:00
Manna, Soumi
982a87ab74 [CLANG] Fix potential null pointer dereference bugs
This patch uses castAs instead of getAs which will assert if the type doesn't match and adds nullptr check if needed.

Also this patch improves the codes and passes I.getData() instead of doing a lookup in dumpVarDefinitionName()
since we're iterating over the same map in LocalVariableMap::dumpContex().

Reviewed By: aaron.ballman, aaronpuchert

Differential Revision: https://reviews.llvm.org/D153033
2023-06-22 12:58:38 -07:00
Haojian Wu
752b971297 [AST] Always set dependent-type for the CallExpr for error-recovery in C.
When build CallExpr for error-recovery where we have any dependent
child nodes), we should set a dependent type for CallExpr to avoid
running into some unexpected following semantic analysis.

This also aligns with the C++ behavior.

This fixes the symptom crashes: https://github.com/llvm/llvm-project/issues/50244

Differential Revision: https://reviews.llvm.org/D152561
2023-06-12 07:04:39 +02:00
Paulo Matos
55aeb23fe0 [clang][WebAssembly] Implement support for table types and builtins
This commit implements support for WebAssembly table types and
respective builtins. Table tables are WebAssembly objects to store
reference types. They have a large amount of semantic restrictions
including, but not limited to, only being allowed to be declared
at the top-level as static arrays of zero-length. Not being arguments
or result of functions, not being stored ot memory, etc.

This commit introduces the __attribute__((wasm_table)) to attach to
arrays of WebAssembly reference types. And the following builtins to
manage tables:

* ref   __builtin_wasm_table_get(table, idx)
* void  __builtin_wasm_table_set(table, idx, ref)
* uint  __builtin_wasm_table_size(table)
* uint  __builtin_wasm_table_grow(table, ref, uint)
* void  __builtin_wasm_table_fill(table, idx, ref, uint)
* void  __builtin_wasm_table_copy(table, table, uint, uint, uint)

This commit also enables reference-types feature at bleeding-edge.

This is joint work with Alex Bradbury (@asb).

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D139010
2023-06-10 15:53:13 +02:00
Corentin Jabot
4676885270 [clang] Implement P2564 "consteval must propagate up"
Reviewed By: aaron.ballman, #clang-language-wg

Differential Revision: https://reviews.llvm.org/D151094
2023-06-07 20:45:36 +02:00
Manna, Soumi
3581e6b857 [NFC][CLANG] Fix nullptr dereference issue in checkSizelessVectorShift()
This patch uses castAs instead of getAs which will assert if the type doesn't match in checkSizelessVectorShift(clang::Sema &, clang::ActionResult<clang::Expr *, true> &, clang::ActionResult<clang::Expr *, true> &, clang::SourceLocation, bool).

Reviewed By: erichkeane

Differential Revision: https://reviews.llvm.org/D152107
2023-06-05 08:12:33 -07:00
Aaron Ballman
12728e1449 [C] Support _Generic expressions with a type operand
_Generic accepts an expression operand whose type is matched against a
list of associations. The expression operand is unevaluated, but the
type matched is the type after lvalue conversion. This conversion loses
type information, which makes it more difficult to match against
qualified or incomplete types.

This extension allows _Generic to accept a type operand instead of an
expression operand. The type operand form does not undergo any
conversions and is matched directly against the association list.

This extension is also supported in C++ as we already supported
_Generic selection expressions there.

The RFC for this extension can be found at:
https://discourse.llvm.org/t/rfc-generic-selection-expression-with-a-type-operand/70388

Differential Revision: https://reviews.llvm.org/D149904
2023-06-05 11:09:58 -04:00
M. Zeeshan Siddiqui
e621757365 [Clang][BFloat16] Upgrade __bf16 to arithmetic type, change mangling, and extend excess precision support
Pursuant to discussions at
https://discourse.llvm.org/t/rfc-c-23-p1467r9-extended-floating-point-types-and-standard-names/70033/22,
this commit enhances the handling of the __bf16 type in Clang.
- Firstly, it upgrades __bf16 from a storage-only type to an arithmetic
  type.
- Secondly, it changes the mangling of __bf16 to DF16b on all
  architectures except ARM. This change has been made in
  accordance with the finalization of the mangling for the
  std::bfloat16_t type, as discussed at
  https://github.com/itanium-cxx-abi/cxx-abi/pull/147.
- Finally, this commit extends the existing excess precision support to
  the __bf16 type. This applies to hardware architectures that do not
  natively support bfloat16 arithmetic.
Appropriate tests have been added to verify the effects of these
changes and ensure no regressions in other areas of the compiler.

Reviewed By: rjmccall, pengfei, zahiraam

Differential Revision: https://reviews.llvm.org/D150913
2023-05-27 13:33:50 +08:00
yronglin
f75b73549d [Clang][Attribute] Improve the AST/diagnoses fidelity of alignas and _Alignas
- Fix diagnoses when the argument to `alignas` or `_Alignas` is an incomplete type.

Before:
```
./alignas.cpp:1:15: error: invalid application of 'alignof' to an incomplete type 'void'
class alignas(void) Foo {};
             ~^~~~~
1 error generated.
```
Now:
```
./alignas.cpp:1:15: error: invalid application of 'alignas' to an incomplete type 'void'
class alignas(void) Foo {};
             ~^~~~~
1 error generated.
```

- Improve the AST fidelity of `alignas` and `_Alignas` attribute.

Before:
```
AlignedAttr 0x13f07f278 <col:7> alignas
    `-ConstantExpr 0x13f07f258 <col:15, col:21> 'unsigned long'
      |-value: Int 8
      `-UnaryExprOrTypeTraitExpr 0x13f07f118 <col:15, col:21> 'unsigned long' alignof 'void *'
```

Now:
```
AlignedAttr 0x14288c608 <col:7> alignas 'void *'
```

Reviewed By: erichkeane

Differential Revision: https://reviews.llvm.org/D150528
2023-05-26 07:41:26 +08:00
Erich Keane
fca4e2add0 Make dereferencing a void* a hard-error instead of warn-as-error
Clang 16 changed to consider dereferencing a void* to be a
warning-as-error, plus made this an error in SFINAE contexts, since this
resulted in incorrect template instantiation.  When doing so, the Clang
16 documentation was updated to reflect that this was likely to change
again to a non-disablable error in the next version.

As there has been no response to changing from a warning to an error, I
believe this is a non-controversial change.

This patch changes this to be an Error, consistent with the standard and
other compilers.

This was discussed in this RFC:
https://discourse.llvm.org/t/rfc-can-we-stop-the-extension-to-allow-dereferencing-void-in-c/65708

Differential Revision: https://reviews.llvm.org/D150875
2023-05-24 11:27:13 -07:00