llvm-project/clang/test/SemaCXX/constexpr-function-recovery-crash.cpp
yronglin 3cfdef3715
[Clang] Fix the for statement disappearing in AST when an error occurs in the conditional expression of the for statement (#65381)
Consider:
```
constexpr int f() {
    int sum = 0;
    for (int i = 0; undefined_var; ++i) {
        sum += i;
    }
    return sum;
}

static_assert(f());
```

The AST before this patch:
```
|-FunctionDecl <line:1:1, line:7:1> line:1:15 used constexpr f 'int ()' implicit-inline
| `-CompoundStmt <col:19, line:7:1>
|   |-DeclStmt <line:2:5, col:16>
|   | `-VarDecl <col:5, col:15> col:9 used sum 'int' cinit
|   |   `-IntegerLiteral <col:15> 'int' 0
|   `-ReturnStmt <line:6:5, col:12>
|     `-ImplicitCastExpr <col:12> 'int' <LValueToRValue>
|       `-DeclRefExpr <col:12> 'int' lvalue Var 0xb870518 'sum' 'int'
```

The AST after this patch:
```
|-FunctionDecl 0x11d0f63f8 <./main.cpp:1:1, line:7:1> line:1:15 used constexpr f 'int ()' implicit-inline
| `-CompoundStmt 0x11d110880 <col:19, line:7:1>
|   |-DeclStmt 0x11d0f65c8 <line:2:5, col:16>
|   | `-VarDecl 0x11d0f6528 <col:5, col:15> col:9 used sum 'int' cinit
|   |   `-IntegerLiteral 0x11d0f6590 <col:15> 'int' 0
|   |-ForStmt 0x11d110800 <line:3:5, line:5:5>
|   | |-DeclStmt 0x11d0f66a0 <line:3:10, col:19>
|   | | `-VarDecl 0x11d0f6600 <col:10, col:18> col:14 used i 'int' cinit
|   | |   `-IntegerLiteral 0x11d0f6668 <col:18> 'int' 0
|   | |-<<<NULL>>>
|   | |-RecoveryExpr 0x11d0f66e8 <col:21> 'bool' contains-errors
|   | |-UnaryOperator 0x11d0f6728 <col:36, col:38> 'int' lvalue prefix '++'
|   | | `-DeclRefExpr 0x11d0f6708 <col:38> 'int' lvalue Var 0x11d0f6600 'i' 'int'
|   | `-CompoundStmt 0x11d0f67c8 <col:41, line:5:5>
|   |   `-CompoundAssignOperator 0x11d0f6798 <line:4:9, col:16> 'int' lvalue '+=' ComputeLHSTy='int' ComputeResultTy='int'
|   |     |-DeclRefExpr 0x11d0f6740 <col:9> 'int' lvalue Var 0x11d0f6528 'sum' 'int'
|   |     `-ImplicitCastExpr 0x11d0f6780 <col:16> 'int' <LValueToRValue>
|   |       `-DeclRefExpr 0x11d0f6760 <col:16> 'int' lvalue Var 0x11d0f6600 'i' 'int'
|   `-ReturnStmt 0x11d110870 <line:6:5, col:12>
|     `-ImplicitCastExpr 0x11d110858 <col:12> 'int' <LValueToRValue>
|       `-DeclRefExpr 0x11d110838 <col:12> 'int' lvalue Var 0x11d0f6528 'sum' 'int'
```

---------

Co-authored-by: Shafik Yaghmour <shafik@users.noreply.github.com>
2023-09-09 00:48:06 +08:00

110 lines
4.6 KiB
C++

// RUN: %clang_cc1 %s -std=c++20 -fsyntax-only -fcxx-exceptions -verify
// verify no value-dependent-assertion crash in constexpr function body and no
// bogus diagnostics.
class Foo {
constexpr Foo() {
while (invalid()) {} // expected-error {{use of undeclared identifier}}
if (invalid()) {} // expected-error {{use of undeclared identifier}}
}
};
constexpr void test1() {
while (invalid()) {} // expected-error {{use of undeclared identifier}}
if (invalid()) {} // expected-error {{use of undeclared identifier}}
}
struct A {
int *p = new int(invalid()); // expected-error {{use of undeclared identifier}}
constexpr ~A() { delete p; }
};
constexpr int test2() {
A a;
return 1;
}
constexpr int test3() {
return invalid(); // expected-error {{use of undeclared identifier}}
}
constexpr int test4() {
if (invalid()) // expected-error {{use of undeclared identifier}}
return 1;
else
return 0;
}
constexpr int test5() { // expected-error {{constexpr function never produce}}
for (;; a++); // expected-error {{use of undeclared identifier}} \
expected-note {{constexpr evaluation hit maximum step limit; possible infinite loop?}}
return 1;
}
constexpr int test6() { // expected-error {{constexpr function never produce}}
int n = 0;
switch (n) {
for (;; a++) { // expected-error {{use of undeclared identifier}}
case 0:; // expected-note {{constexpr evaluation hit maximum step limit; possible infinite loop?}}
}
}
return 0;
}
constexpr bool test7() {
for (int n = 0; ; invalid()) { if (n == 1) return true; } // expected-error {{use of undeclared identifier}}
throw "bad";
}
constexpr void test8() {
do {} while (invalid()); // expected-error {{use of undeclared identifier}}
throw "bad";
}
template<int x> constexpr int f(int y) { // expected-note {{candidate template ignored}}
return x * y;
}
constexpr int test9(int x) {
return f<1>(f<x>(1)); // expected-error {{no matching function for call to 'f'}}
}
constexpr int test10() { return undef(); } // expected-error {{use of undeclared identifier 'undef'}}
static_assert(test10() <= 1, "should not crash"); // expected-error {{static assertion expression is not an integral constant expression}}
struct X {} array[] = {undef()}; // expected-error {{use of undeclared identifier 'undef'}}
constexpr void test11() {
for (X& e : array) {}
}
constexpr int test12() { return "wrong"; } // expected-error {{cannot initialize return object of type 'int'}}
constexpr int force12 = test12(); // expected-error {{must be initialized by a constant}}
#define TEST_EVALUATE(Name, X) \
constexpr int testEvaluate##Name() { \
X return 0; \
} \
constexpr int forceEvaluate##Name = testEvaluate##Name()
// Check that a variety of broken loops don't crash constant evaluation.
// We're not checking specific recovery here so don't assert diagnostics.
TEST_EVALUATE(Switch, switch (!!){}); // expected-error + {{}}
TEST_EVALUATE(SwitchInit, switch (auto x = !!){}); // expected-error + {{}}
TEST_EVALUATE(SwitchCondValDep, switch (invalid_value) { default: break; }); // expected-error + {{}}
TEST_EVALUATE(For, for (!!){}); // expected-error + {{}}
// FIXME: should bail out instead of looping.
// expected-note@-2 + {{infinite loop}}
// expected-note@-3 {{in call}}
TEST_EVALUATE(ForRange, for (auto x : !!){}); // expected-error + {{}}
TEST_EVALUATE(While, while (!!){}); // expected-error + {{}}
TEST_EVALUATE(DoWhile, do {} while (!!);); // expected-error + {{}}
TEST_EVALUATE(DoWhileCond, do {} while (some_cond < 10);); // expected-error {{use of undeclared identifier}} \
// expected-error {{constexpr variable 'forceEvaluateDoWhileCond' must be initialized by a constant expression}}
TEST_EVALUATE(If, if (!!){};); // expected-error + {{}}
TEST_EVALUATE(IfInit, if (auto x = !!; 1){};);// expected-error + {{}}
TEST_EVALUATE(ForInit, for (!!;;){};);// expected-error + {{}}
// expected-note@-1 + {{infinite loop}}
// expected-note@-2 {{in call}}
TEST_EVALUATE(ForCond, for (; !!;){};);// expected-error + {{}}
TEST_EVALUATE(ForInc, for (;; !!){};);// expected-error + {{}}
// expected-note@-1 + {{infinite loop}}
// expected-note@-2 {{in call}}
TEST_EVALUATE(ForCondUnDef, for (;some_cond;){};); // expected-error + {{}}