1482 Commits

Author SHA1 Message Date
Takuya Shimizu
615d812696 [clang][ExprConstant] Improve error message of compound assignment against uninitialized object
BEFORE this patch, compound assignment operator against uninitialized object such as uninit += 1 was diagnosed as subexpression not valid
This patch clarifies the reason for the error by saying that uninit is an uninitialized object.

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

Reviewed By: shafik, tbaeder
Differential Revision: https://reviews.llvm.org/D157855
2023-08-25 16:08:07 +09:00
Corentin Jabot
3f98cdc815 [Clang] Always constant-evaluate operands of comparisons to nullptr
even if we know what the result is going to be.
There may be side effects we ought not to ignore,

Fixes #64923

Reviewed By: ChuanqiXu

Differential Revision: https://reviews.llvm.org/D158601
2023-08-24 16:11:10 +02:00
Takuya Shimizu
985a72b6b3 [clang][Diagnostics] Provide source range to integer-overflow warnings
BEFORE:

```
overflow.cpp:1:21: warning: overflow in expression; result is -2147483648 with type 'int' [-Winteger-overflow]
    1 | int x = __INT_MAX__ + 1 + 3;
      |                     ^
overflow.cpp:2:9: warning: overflow in expression; result is -2147483648 with type 'int' [-Winteger-overflow]
    2 | int a = -(1 << 31) + 1;
      |         ^
```
AFTER:

```
overflow.cpp:1:21: warning: overflow in expression; result is -2147483648 with type 'int' [-Winteger-overflow]
    1 | int x = __INT_MAX__ + 1 + 3;
      |         ~~~~~~~~~~~~^~~
overflow.cpp:2:9: warning: overflow in expression; result is -2147483648 with type 'int' [-Winteger-overflow]
    2 | int a = -(1 << 31) + 1;
      |         ^~~~~~~~~~
```

Reviewed By: tbaeder
Differential Revision: https://reviews.llvm.org/D157383
2023-08-19 22:05:12 +09: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
Timm Bäder
871ee94141 [clang][ExprConst] Use call source range for 'in call to' diags
Differential Revision: https://reviews.llvm.org/D156604
2023-08-16 15:22:29 +02:00
Alejandro Aguirre
00158ae236 [clang] Enable constexpr on LZCNT/POPCNT MS extension intrinsics
As discussed on #46593 - this enables us to use __lzcnt / __popcnt intrinsics inside constexpr code.

Differential Revision: https://reviews.llvm.org/D157420
2023-08-14 11:33:33 +01:00
Timm Bäder
f6ee4e3f55 [clang][ExprConst] Add RHS source range to div by zero diags
Differential Revision: https://reviews.llvm.org/D157074
2023-08-09 15:48:50 +02:00
Timm Bäder
925ec544cf Revert "[clang][ExprConst] Add RHS source range to div by zero diags"
This reverts commit 74c141a467caf9ebb4835458bc4ffbedb172a63a.

Looks like this breaks a whole bunch of unexpected tests:
https://lab.llvm.org/buildbot/#/builders/109/builds/70813
2023-08-08 18:16:35 +02:00
Timm Bäder
74c141a467 [clang][ExprConst] Add RHS source range to div by zero diags
Differential Revision: https://reviews.llvm.org/D157074
2023-08-08 17:59:13 +02:00
Takuya Shimizu
24c91d4432 [clang][ExprConstant] Fix crash on uninitialized base class subobject
This patch fixes the reported regression caused by D146358 through adding notes about an uninitialized base class when we diagnose uninitialized constructor.

This also changes the wording from the old one in order to make it clear that the uninitialized subobject is a base class and its constructor is not called.
Wording changes:
BEFORE: `subobject of type 'Base' is not initialized`
AFTER: `constructor of base class 'Base' is not called`

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

Reviewed By: aaron.ballman
Differential Revision: https://reviews.llvm.org/D153969
2023-08-08 15:53:17 +09:00
Nick Desaulniers
610ec954e1 [clang] allow const structs/unions/arrays to be constant expressions for C
For code like:
struct foo { ... };
struct bar { struct foo foo; };
const struct foo my_foo = { ... };
struct bar my_bar = { .foo = my_foo };

Eli Friedman points out the relevant part of the C standard seems to
have some flexibility in what is considered a constant expression:

6.6 paragraph 10:
An implementation may accept other forms of constant expressions.

GCC 8 added support for these, so clang not supporting them has been a
constant thorn in the side of source code portability within the Linux
kernel.

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

Reviewed By: efriedma

Differential Revision: https://reviews.llvm.org/D76096
2023-08-02 15:32:37 -07:00
Takuya Shimizu
e90f4fc6ac [clang][ExprConstant] Print template arguments when describing stack frame
This patch adds additional printing of template argument list when the described function is a template specialization.
This can be useful when handling complex template functions in constexpr evaluator.

Reviewed By: cjdb, dblaikie
Differential Revision: https://reviews.llvm.org/D154366
2023-07-31 17:05:56 +09:00
Corentin Jabot
45ab2b48bd [Clang] Improve the handling of large arrays evaluation.
This is a temporary fix (for clang 17) that caps the size of
any array we try to constant evaluate:

    There are 2 limits:
      * We cap to UINT_MAX the size of ant constant evaluated array,
        because the constant evaluator does not support size_t.
      * We cap to `-fconstexpr-steps` elements the size of each individual
        array and dynamic array allocations.
        This works out because the number of constexpr steps already limits
        how many array elements can be initialized, which makes this new
        limit conservatively generous.
        This ensure that the compiler does not crash when attempting to
        constant-fold valid programs.

    If the limit is reached by a given array, constant evaluation will fail,
    and the program will be ill-formed, until a bigger limit is given.
    Or, constant folding will fail and the array will be evaluated at runtime.

    Fixes #63562

Reviewed By: efriedma

Differential Revision: https://reviews.llvm.org/D155955
2023-07-28 14:38:22 +02:00
Timm Bäder
81fb216245 [clang][Diagnostics] Provide source range to invalid casts in const expr
Differential Revision: https://reviews.llvm.org/D153241
2023-07-28 12:28:45 +02:00
Timm Bäder
7a3ad8ed77 [clang] Provide source range to 'invalid subexpr in const expr' diags
Differential Revision: https://reviews.llvm.org/D150566
2023-07-26 09:12:11 +02:00
Nick Desaulniers
b54294e2c9 [clang][ConstantEmitter] have tryEmitPrivate[ForVarInit] try ConstExprEmitter fast-path first
As suggested by @efriedma in:
https://reviews.llvm.org/D76096#4370369

This should speed up evaluating whether an expression is constant or
not, but due to the complexity of these two different implementations,
we may start getting different answers for edge cases for which we do
not yet have test cases in-tree (or perhaps even performance regressions
for some cases). As such, contributors have carte blanche to revert if
necessary.

For additional historical context about ExprConstant vs CGExprConstant,
here's snippets from a private conversation on discord:

  ndesaulniers:
  why do we have clang/lib/AST/ExprConstant.cpp and
  clang/lib/CodeGen/CGExprConstant.cpp? Does clang constant fold during
  ast walking/creation AND during LLVM codegen?
  efriedma:
  originally, clang needed to handle two things: integer constant
  expressions (the "5" in "int x[5];"), and constant global initializers
  (the "5" in "int x = 5;").  pre-C++11, the two could be handled mostly
  separately; so we had the code for integer constants in AST/, and the
  code for globals in CodeGen/.  C++11 constexpr sort of destroyed that
  separation, though. so now we do both kinds of constant evaluation on
  the AST, then CGExprConstant translates the result of that evaluation
  to LLVM IR.  but we kept around some bits of the old cgexprconstant to
  avoid performance/memory usage regressions on large arrays.

Reviewed By: efriedma

Differential Revision: https://reviews.llvm.org/D151587
2023-07-24 13:50:45 -07:00
Corentin Jabot
47ccfd7a89 [Clang] Implement P2741R3 - user-generated static_assert messages
Reviewed By: #clang-language-wg, aaron.ballman

Differential Revision: https://reviews.llvm.org/D154290
2023-07-20 08:33:19 +02:00
Sindhu Chittireddy
e9877eca40 [NFC] Initialize class member pointers to nullptr.
Fix clang-format issues in surrounding code.
Differential revision: https://reviews.llvm.org/D153892
2023-07-12 11:04:38 -07:00
Timm Bäder
a83079f754 [clang][ConstExpr][NFC] Make Frame::describe() const 2023-07-09 15:49:32 +02:00
yronglin
36f67434f7 [AST] Stop evaluate constant expression if the condition expression which in switch statement contains errors
This fix issue: https://github.com/llvm/llvm-project/issues/63453

```
constexpr int foo(unsigned char c) {
    switch (f) {
    case 0:
        return 7;
    default:
        break;
    }
    return 0;
}

static_assert(foo('d'));

```

Reviewed By: aaron.ballman, erichkeane, hokein

Differential Revision: https://reviews.llvm.org/D153296
2023-07-07 19:56:47 +08:00
Timm Bäder
3ec12740d0 [clang][NFC] Move two declarations closer to their point of use 2023-07-05 08:54:24 +02:00
Aaron Ballman
7b69eabdc1 [C11] Correct global atomic pointer initialization from an integer constant
This is a follow-up to 2e275e24355cb224981f9beb2b026a3169fc7232 and
1395cde24b3641e284bb1daae7d56c189a2635e3 which corrects a missed case:
initializing an _Atomic(T *) from a null pointer constant in the form
of the integer literal 0.

Fixes https://github.com/llvm/llvm-project/issues/63550
Differential Revision: https://reviews.llvm.org/D154284
2023-07-02 14:41:21 -04:00
Takuya Shimizu
f6be96aa4e [clang][ExprConstant] Fix display of syntactically-invalid note for member function calls
This patch makes the display of member function calls more true to the user-written code by making use of the syntactical structure of the function calls.
This patch also changes the display of conventional value-based printing from arrow operator to dot operator.
This avoids the syntactical invalidness in notes previously caused by the display of & operator
(lack of parentheses and reference of rvalue)

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

Reviewed By: cjdb
Differential Revision: https://reviews.llvm.org/D151720
2023-06-28 00:19:46 +09:00
Corentin Jabot
f27afedc6c [Clang] Implement P2738R1 - constexpr cast from void*
Reviewed By: #clang-language-wg, erichkeane

Differential Revision: https://reviews.llvm.org/D153702
2023-06-26 16:45:07 +02:00
Elliot Goodrich
b0abd4893f [llvm] Add missing StringExtras.h includes
In preparation for removing the `#include "llvm/ADT/StringExtras.h"`
from the header to source file of `llvm/Support/Error.h`, first add in
all the missing includes that were previously included transitively
through this header.
2023-06-25 15:42:22 +01:00
Manna, Soumi
5e12f5ab2d [CLANG] Fix uninitialized scalar field issues
Reviewed By: erichkeane, steakhal, tahonermann, shafik

Differential Revision: https://reviews.llvm.org/D150744
2023-06-22 12:09:14 -07:00
Serge Pavlov
7dd387d297 [clang] Add __builtin_isfpclass
A new builtin function __builtin_isfpclass is added. It is called as:

    __builtin_isfpclass(<floating point value>, <test>)

and returns an integer value, which is non-zero if the floating point
argument falls into one of the classes specified by the second argument,
and zero otherwise. The set of classes is an integer value, where each
value class is represented by a bit. There are ten data classes, as
defined by the IEEE-754 standard, they are represented by bits:

    0x0001 (__FPCLASS_SNAN)         - Signaling NaN
    0x0002 (__FPCLASS_QNAN)         - Quiet NaN
    0x0004 (__FPCLASS_NEGINF)       - Negative infinity
    0x0008 (__FPCLASS_NEGNORMAL)    - Negative normal
    0x0010 (__FPCLASS_NEGSUBNORMAL) - Negative subnormal
    0x0020 (__FPCLASS_NEGZERO)      - Negative zero
    0x0040 (__FPCLASS_POSZERO)      - Positive zero
    0x0080 (__FPCLASS_POSSUBNORMAL) - Positive subnormal
    0x0100 (__FPCLASS_POSNORMAL)    - Positive normal
    0x0200 (__FPCLASS_POSINF)       - Positive infinity

They have corresponding builtin macros to facilitate using the builtin
function:

    if (__builtin_isfpclass(x, __FPCLASS_NEGZERO | __FPCLASS_POSZERO) {
      // x is any zero.
    }

The data class encoding is identical to that used in llvm.is.fpclass
function.

Differential Revision: https://reviews.llvm.org/D152351
2023-06-18 22:53:32 +07:00
Dimitry Andric
69d42eef4b [Clang] Show type in enum out of range diagnostic
When the diagnostic for an out of range enum value is printed, it
currently does not show the actual enum type in question, for example:

    v8/src/base/bit-field.h:43:29: error: integer value 7 is outside the valid range of values [0, 3] for this enumeration type [-Wenum-constexpr-conversion]
      static constexpr T kMax = static_cast<T>(kNumValues - 1);
                                ^

This can make it cumbersome to find the cause for the problem. Add the
enum type to the diagnostic message, to make it easier.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D152788
2023-06-14 20:34:19 +02:00
Nick Desaulniers
9daab7fb02 [ExprConstant] fix ICE from Evaluate on _Atomic structs
Fixes two crashes reported by @efriedma and @ahatanak, as per
@ahatanak's analysis.

Link: https://reviews.llvm.org/D151587#4397446
Link: https://reviews.llvm.org/D151587#4399684

Reviewed By: ahatanak, aaron.ballman

Differential Revision: https://reviews.llvm.org/D152303
2023-06-08 13:20:24 -07: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
b6a5aeadb5 [NFC][CLANG] Fix issue with dereference null return value in EvaluateBuiltinClassifyType()
This patch uses cast instead of dyn_cast which will assert if the type doesn't match.

Reviewed By: erichkeane

Differential Revision: https://reviews.llvm.org/D151469
2023-05-28 20:09:09 -07: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
Takuya Shimizu
456d072405 Reland: [clang][AST] Print name instead of type when diagnosing uninitialized subobject in constexpr variables
This patch improves the diagnostic on uninitialized subobjects in constexpr variables by modifying the diagnostic message to display the subobject's name instead of its type.

Fixes https://github.com/llvm/llvm-project/issues/58601
Differential Revision: https://reviews.llvm.org/D146358
2023-05-24 21:31:25 +09:00
Nick Desaulniers
804af933f7 Reland: [clang][ExprConstant] fix __builtin_object_size for flexible array members
As reported by @kees, GCC treats __builtin_object_size of structures
containing flexible array members (aka arrays with incomplete type) not
just as the sizeof the underlying type, but additionally the size of the
members in a designated initializer list.

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

Reviewed By: erichkeane, efriedma

Differential Revision: https://reviews.llvm.org/D150892
2023-05-23 14:41:46 -07:00
Manna, Soumi
64e9ba7048 [NFC][CLANG] Fix static code analyzer concerns
Reported by Static Code Analyzer Tool, Coverity:

Dereference null return value

Inside "ExprConstant.cpp" file, in <unnamed>::RecordExprEvaluator::VisitCXXStdInitializerListExpr(clang::CXXStdInitializerListExpr const *): Return value of function which returns null is dereferenced without checking.

  bool RecordExprEvaluator::VisitCXXStdInitializerListExpr(
   const CXXStdInitializerListExpr *E) {
       // returned_null: getAsConstantArrayType returns nullptr (checked 81 out of 93 times).
       //var_assigned: Assigning: ArrayType = nullptr return value from getAsConstantArrayType.
    const ConstantArrayType *ArrayType =
       Info.Ctx.getAsConstantArrayType(E->getSubExpr()->getType());
    LValue Array;
    //Condition !EvaluateLValue(E->getSubExpr(), Array, this->Info, false), taking false branch.
    if (!EvaluateLValue(E->getSubExpr(), Array, Info))
     return false;

    // Get a pointer to the first element of the array.

    //Dereference null return value (NULL_RETURNS)
    //dereference: Dereferencing a pointer that might be nullptr ArrayType when calling addArray.
    Array.addArray(Info, E, ArrayType);

This patch adds an assert for unexpected type for array initializer.

Reviewed By: erichkeane

Differential Revision: https://reviews.llvm.org/D151040
2023-05-23 07:42:15 -07:00
Krasimir Georgiev
f5af7d2d98 Revert "[clang][ExprConstant] fix __builtin_object_size for flexible array members"
This reverts commit 57c5c1ab2a188b7962c9de5ac0f95e3c7441940a.

Causes an assertion failure: https://reviews.llvm.org/D150892#4363080
2023-05-23 07:59:38 +00:00
Nick Desaulniers
57c5c1ab2a [clang][ExprConstant] fix __builtin_object_size for flexible array members
As reported by @kees, GCC treats __builtin_object_size of structures
containing flexible array members (aka arrays with incomplete type) not
just as the sizeof the underlying type, but additionally the size of the
members in a designated initializer list.

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

Reviewed By: erichkeane

Differential Revision: https://reviews.llvm.org/D150892
2023-05-22 11:38:38 -07:00
Erich Keane
34e49d3e85 Revert "[clang][AST] Print name instead of type when diagnosing uninitialized subobject in constexpr variables"
This reverts commit 0e167fc0a2147c9b673b8afd5fea001b0d127781.

This patch causes its assertion to fire when doing AST-dump, so
reverting so that the author has time to fix this.
2023-05-18 06:44:39 -07:00
Takuya Shimizu
0e167fc0a2 [clang][AST] Print name instead of type when diagnosing uninitialized subobject in constexpr variables
This patch improves the diagnostic on uninitialized subobjects in constexpr variables by modifying the diagnostic message to display the subobject's name instead of its type.

Fixes https://github.com/llvm/llvm-project/issues/58601
Differential Revision: https://reviews.llvm.org/D146358
2023-05-16 21:49:57 +09:00
Corentin Jabot
9857caf9d1 [Clang] Correctly handle allocation in template arguments
Fixes #62462

Reviewed By: #clang-language-wg, erichkeane

Differential Revision: https://reviews.llvm.org/D150036
2023-05-08 16:28:06 +02:00
Mark de Wever
ba15d186e5 [clang] Use -std=c++23 instead of -std=c++2b
During the ISO C++ Committee meeting plenary session the C++23 Standard
has been voted as technical complete.

This updates the reference to c++2b to c++23 and updates the __cplusplus
macro.

Drive-by fixes c++1z -> c++17 and c++2a -> c++20 when seen.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D149553
2023-05-04 19:19:52 +02:00
Aaron Ballman
1395cde24b Fix codegen for initialization of global atomics
This amends 2e275e24355cb224981f9beb2b026a3169fc7232. That commit added
a null to pointer cast kind when determining whether the expression can
be a valid constant initializer, but failed to update the constant
expression evaluator to perform the evaluation. This commit updates the
constant expression evaluator to handle that cast kind.
2023-04-25 11:37:06 -04:00
Takuya Shimizu
a4edc2c9fa Constexpr evaluator should treat [[gnu::weak]] member pointer comparisons as evaluation failure
This patch fixes the wrong signal from the constexpr evaluator that
[[gnu::weak]] member pointer comparison is valid, while it is emitting
notes on them.

I found a crashing case fixed by this change and added it as a test
case: https://godbolt.org/z/8391fGjGn

I noticed this while I was working on D146358.

Differential Revision: https://reviews.llvm.org/D148419
2023-04-17 09:50:46 -04:00
Takuya Shimizu
6b48d202ef [clang][AST] Improve diagnostic for nullptr constexpr function pointer call
This patch improves diagnostic for clang constexpr evaluator by adding
a check for nullptr in function pointer call evaluations.

ex.
```
constexpr int foo(int (*bla)(void)) {
  return bla();
}

static_assert(foo(nullptr) == 1);
```

BEFORE this patch, clang generates the following diagnostic for the
code above:

```
<source>:5:15: error: static assertion expression is not an integral constant expression
static_assert(foo(nullptr) == 1);
              ^~~~~~~~~~~~~~~~~
<source>:2:10: note: subexpression not valid in a constant expression
  return bla();
         ^
<source>:5:15: note: in call to 'foo(nullptr)'
static_assert(foo(nullptr) == 1);
              ^
1 error generated.
```

AFTER this patch, subexpression not valid in a constant expression note
is replaced with 'bla' evaluates to a null function pointer.

Fixes https://github.com/llvm/llvm-project/issues/59872
Differential Revision: https://reviews.llvm.org/D145793
2023-03-13 12:53:12 -04:00
Mariya Podchishchaeva
af682f0df8 [clang] Fix single-element array initialization in constexpr
https://reviews.llvm.org/D130791 added an improvement that in case array
element has a trivial constructor, it is evaluated once and the result is
re-used for remaining elements. Make sure the constructor is evaluated
for single-elements arrays too.

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

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D145486
2023-03-07 10:57:35 -05:00
Mariya Podchishchaeva
2fbf18b400 [clang] Fix aggregate initialization inside lambda constexpr
Constant evaluator only considered access to `this` pointer to be
possible if `this` poitners was captured. However `this` can also appear if
there was a default member initializer.

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

Reviewed By: shafik

Differential Revision: https://reviews.llvm.org/D144866
2023-03-06 03:38:49 -05:00
Kazu Hirata
a28b252d85 Use APInt::getSignificantBits instead of APInt::getMinSignedBits (NFC)
Note that getMinSignedBits has been soft-deprecated in favor of
getSignificantBits.
2023-02-19 23:56:52 -08:00
Kazu Hirata
f8f3db2756 Use APInt::count{l,r}_{zero,one} (NFC) 2023-02-19 22:04:47 -08:00
Kazu Hirata
cbde2124f1 Use APInt::popcount instead of APInt::countPopulation (NFC)
This is for consistency with the C++20-style bit manipulation
functions in <bit>.
2023-02-19 11:29:12 -08:00
Paulo Matos
890146b192 [WebAssembly] Initial support for reference type externref in clang
This patch introduces a new type __externref_t that denotes a WebAssembly opaque
reference type. It also implements builtin __builtin_wasm_ref_null_extern(),
that returns a null value of __externref_t. This lays the ground work
for further builtins and reference types.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D122215
2023-02-17 18:48:48 -08:00