llvm-project/clang/test/SemaCXX/constant-expression.cpp
Matheus Izvekov 91cdd35008
[clang] Improve nested name specifier AST representation (#147835)
This is a major change on how we represent nested name qualifications in
the AST.

* The nested name specifier itself and how it's stored is changed. The
prefixes for types are handled within the type hierarchy, which makes
canonicalization for them super cheap, no memory allocation required.
Also translating a type into nested name specifier form becomes a no-op.
An identifier is stored as a DependentNameType. The nested name
specifier gains a lightweight handle class, to be used instead of
passing around pointers, which is similar to what is implemented for
TemplateName. There is still one free bit available, and this handle can
be used within a PointerUnion and PointerIntPair, which should keep
bit-packing aficionados happy.
* The ElaboratedType node is removed, all type nodes in which it could
previously apply to can now store the elaborated keyword and name
qualifier, tail allocating when present.
* TagTypes can now point to the exact declaration found when producing
these, as opposed to the previous situation of there only existing one
TagType per entity. This increases the amount of type sugar retained,
and can have several applications, for example in tracking module
ownership, and other tools which care about source file origins, such as
IWYU. These TagTypes are lazily allocated, in order to limit the
increase in AST size.

This patch offers a great performance benefit.

It greatly improves compilation time for
[stdexec](https://github.com/NVIDIA/stdexec). For one datapoint, for
`test_on2.cpp` in that project, which is the slowest compiling test,
this patch improves `-c` compilation time by about 7.2%, with the
`-fsyntax-only` improvement being at ~12%.

This has great results on compile-time-tracker as well:

![image](https://github.com/user-attachments/assets/700dce98-2cab-4aa8-97d1-b038c0bee831)

This patch also further enables other optimziations in the future, and
will reduce the performance impact of template specialization resugaring
when that lands.

It has some other miscelaneous drive-by fixes.

About the review: Yes the patch is huge, sorry about that. Part of the
reason is that I started by the nested name specifier part, before the
ElaboratedType part, but that had a huge performance downside, as
ElaboratedType is a big performance hog. I didn't have the steam to go
back and change the patch after the fact.

There is also a lot of internal API changes, and it made sense to remove
ElaboratedType in one go, versus removing it from one type at a time, as
that would present much more churn to the users. Also, the nested name
specifier having a different API avoids missing changes related to how
prefixes work now, which could make existing code compile but not work.

How to review: The important changes are all in
`clang/include/clang/AST` and `clang/lib/AST`, with also important
changes in `clang/lib/Sema/TreeTransform.h`.

The rest and bulk of the changes are mostly consequences of the changes
in API.

PS: TagType::getDecl is renamed to `getOriginalDecl` in this patch, just
for easier to rebasing. I plan to rename it back after this lands.

Fixes #136624
Fixes https://github.com/llvm/llvm-project/issues/43179
Fixes https://github.com/llvm/llvm-project/issues/68670
Fixes https://github.com/llvm/llvm-project/issues/92757
2025-08-09 05:06:53 -03:00

161 lines
5.1 KiB
C++

// RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 -pedantic %s
// C++ [expr.const]p1:
// In several places, C++ requires expressions that evaluate to an integral
// or enumeration constant: as array bounds, as case expressions, as
// bit-field lengths, as enumerator initializers, as static member
// initializers, and as integral or enumeration non-type template arguments.
// An integral constant-expression can involve only literals, enumerators,
// const variables or static data members of integral or enumeration types
// initialized with constant expressions, and sizeof expressions. Floating
// literals can appear only if they are cast to integral or enumeration types.
enum Enum { eval = 1 };
const int cval = 2;
const Enum ceval = eval;
struct Struct {
static const int sval = 3;
static const Enum seval = eval;
};
template <int itval, Enum etval> struct C {
enum E {
v1 = 1,
v2 = eval,
v3 = cval,
v4 = ceval,
v5 = Struct::sval,
v6 = Struct::seval,
v7 = itval,
v8 = etval,
v9 = (int)1.5,
v10 = sizeof(Struct),
v11 = true? 1 + cval * Struct::sval ^ itval / (int)1.5 - sizeof(Struct) : 0
};
unsigned
b1 : 1,
b2 : eval,
b3 : cval,
b4 : ceval,
b5 : Struct::sval,
b6 : Struct::seval,
b7 : itval,
b8 : etval,
b9 : (int)1.5,
b10 : sizeof(Struct),
b11 : true? 1 + cval * Struct::sval ^ itval / (int)1.5 - sizeof(Struct) : 0
;
static const int
i1 = 1,
i2 = eval,
i3 = cval,
i4 = ceval,
i5 = Struct::sval,
i6 = Struct::seval,
i7 = itval,
i8 = etval,
i9 = (int)1.5,
i10 = sizeof(Struct),
i11 = true? 1 + cval * Struct::sval ^ itval / (int)1.5 - sizeof(Struct) : 0
;
void f(int cond) {
switch(cond) {
case 0 + 1:
case 100 + eval:
case 200 + cval:
case 300 + ceval:
case 400 + Struct::sval:
case 500 + Struct::seval:
case 600 + itval:
case 700 + etval:
case 800 + (int)1.5:
case 900 + sizeof(Struct):
case 1000 + (true? 1 + cval * Struct::sval ^
itval / (int)1.5 - sizeof(Struct) : 0):
;
}
}
typedef C<itval, etval> T0;
};
template struct C<1, eval>;
template struct C<cval, ceval>;
template struct C<Struct::sval, Struct::seval>;
enum {
a = sizeof(int) == 8,
b = a? 8 : 4
};
void diags(int n) {
switch (n) {
case (1/0, 1): // expected-error {{not an integral constant expression}} expected-note {{division by zero}} expected-warning {{left operand of comma operator has no effect}}
case (int)(1/0, 2.0): // expected-error {{not an integral constant expression}} expected-note {{division by zero}} expected-warning {{left operand of comma operator has no effect}}
case __imag(1/0): // expected-error {{not an integral constant expression}} expected-note {{division by zero}}
case (int)__imag((double)(1/0)): // expected-error {{not an integral constant expression}} expected-note {{division by zero}}
;
}
}
namespace IntOrEnum {
const int k = 0;
const int &p = k; // expected-note {{declared here}}
template<int n> struct S {};
S<p> s; // expected-error {{not an integral constant expression}} expected-note {{read of variable 'p' of non-integral, non-enumeration type 'const int &'}}
}
extern const int recurse1;
// recurse2 cannot be used in a constant expression because it is not
// initialized by a constant expression. The same expression appearing later in
// the TU would be a constant expression, but here it is not.
const int recurse2 = recurse1; // expected-note {{here}}
const int recurse1 = 1;
int array1[recurse1]; // ok
int array2[recurse2]; // expected-warning 2{{variable length array}} expected-note {{initializer of 'recurse2' is not a constant expression}}
namespace FloatConvert {
typedef int a[(int)42.3];
typedef int a[(int)42.997];
typedef int b[(long long)4e20]; // expected-warning {{variable length}} expected-error {{variable length}} expected-warning {{'long long' is a C++11 extension}} expected-note {{value 4.0E+20 is outside the range of representable values}}
}
// PR12626
namespace test3 {
struct X; // expected-note {{forward declaration of 'test3::X'}}
struct Y { bool b; X x; }; // expected-error {{field has incomplete type 'X'}}
int f() { return Y().b; }
}
// PR18283
namespace test4 {
template <int> struct A {};
int const i = { 42 };
// i can be used as non-type template-parameter as "const int x = { 42 };" is
// equivalent to "const int x = 42;" as per C++03 8.5/p13.
typedef A<i> Ai; // ok
}
namespace rdar16064952 {
template < typename T > void fn1() {
T b;
unsigned w = ({int a = b.val[sizeof(0)]; 0; }); // expected-warning {{use of GNU statement expression extension}}
}
}
char PR17381_ice = 1000000 * 1000000; // expected-warning {{overflow}} expected-warning {{changes value}}
namespace PR31701 {
struct C {
template<int i> static int n; // expected-warning {{extension}}
};
template <int M> class D;
template <int M>
template<int i> void D<M>::set() { // expected-error {{from class 'PR31701::D<M>' without definition}}
const C c = C::n<i>;
}
}
struct PR65784s{
int *ptr;
} const PR65784[] = {(int *)""};
PR65784s PR65784f() { return *PR65784; }