[AST] Add RecoveryExpr to retain expressions on semantic errors
Normally clang avoids creating expressions when it encounters semantic
errors, even if the parser knows which expression to produce.
This works well for the compiler. However, this is not ideal for
source-level tools that have to deal with broken code, e.g. clangd is
not able to provide navigation features even for names that compiler
knows how to resolve.
The new RecoveryExpr aims to capture the minimal set of information
useful for the tools that need to deal with incorrect code:
source range of the expression being dropped,
subexpressions of the expression.
We aim to make constructing RecoveryExprs as simple as possible to
ensure writing code to avoid dropping expressions is easy.
Producing RecoveryExprs can result in new code paths being taken in the
frontend. In particular, clang can produce some new diagnostics now and
we aim to suppress bogus ones based on Expr::containsErrors.
We deliberately produce RecoveryExprs only in the parser for now to
minimize the code affected by this patch. Producing RecoveryExprs in
Sema potentially allows to preserve more information (e.g. type of an
expression), but also results in more code being affected. E.g.
SFINAE checks will have to take presence of RecoveryExprs into account.
Initial implementation only works in C++ mode, as it relies on compiler
postponing diagnostics on dependent expressions. C and ObjC often do not
do this, so they require more work to make sure we do not produce too
many bogus diagnostics on the new expressions.
See documentation of RecoveryExpr for more details.
original patch from Ilya
This change is based on https://reviews.llvm.org/D61722
Reviewers: sammccall, rsmith
Reviewed By: sammccall, rsmith
Tags: #clang
Differential Revision: https://reviews.llvm.org/D69330
2020-03-19 23:30:40 +08:00
|
|
|
// RUN: not %clang_cc1 -triple x86_64-unknown-unknown -Wno-unused-value -fcxx-exceptions -std=gnu++17 -frecovery-ast -ast-dump %s | FileCheck -strict-whitespace %s
|
|
|
|
// RUN: not %clang_cc1 -triple x86_64-unknown-unknown -Wno-unused-value -fcxx-exceptions -std=gnu++17 -fno-recovery-ast -ast-dump %s | FileCheck --check-prefix=DISABLED -strict-whitespace %s
|
|
|
|
|
|
|
|
int some_func(int *);
|
|
|
|
|
|
|
|
// CHECK: VarDecl {{.*}} invalid_call
|
|
|
|
// CHECK-NEXT:`-RecoveryExpr {{.*}} contains-errors
|
|
|
|
// CHECK-NEXT: |-UnresolvedLookupExpr {{.*}} 'some_func'
|
|
|
|
// CHECK-NEXT: `-IntegerLiteral {{.*}} 123
|
|
|
|
// DISABLED-NOT: -RecoveryExpr {{.*}} contains-errors
|
|
|
|
int invalid_call = some_func(123);
|
|
|
|
|
|
|
|
int ambig_func(double);
|
|
|
|
int ambig_func(float);
|
|
|
|
|
|
|
|
// CHECK: VarDecl {{.*}} ambig_call
|
|
|
|
// CHECK-NEXT:`-RecoveryExpr {{.*}} contains-errors
|
|
|
|
// CHECK-NEXT: |-UnresolvedLookupExpr {{.*}} 'ambig_func'
|
|
|
|
// CHECK-NEXT: `-IntegerLiteral {{.*}} 123
|
|
|
|
// DISABLED-NOT: -RecoveryExpr {{.*}} contains-errors
|
|
|
|
int ambig_call = ambig_func(123);
|
|
|
|
|
|
|
|
// CHECK: VarDecl {{.*}} unresolved_call1
|
|
|
|
// CHECK-NEXT:`-RecoveryExpr {{.*}} contains-errors
|
|
|
|
// CHECK-NEXT: `-UnresolvedLookupExpr {{.*}} 'bar'
|
|
|
|
// DISABLED-NOT: -RecoveryExpr {{.*}} contains-errors
|
|
|
|
int unresolved_call1 = bar();
|
|
|
|
|
|
|
|
// CHECK: VarDecl {{.*}} unresolved_call2
|
|
|
|
// CHECK-NEXT:`-CallExpr {{.*}} contains-errors
|
|
|
|
// CHECK-NEXT: |-UnresolvedLookupExpr {{.*}} 'bar'
|
|
|
|
// CHECK-NEXT: |-RecoveryExpr {{.*}} contains-errors
|
|
|
|
// CHECK-NEXT: | `-UnresolvedLookupExpr {{.*}} 'baz'
|
|
|
|
// CHECK-NEXT: `-RecoveryExpr {{.*}} contains-errors
|
|
|
|
// CHECK-NEXT: `-UnresolvedLookupExpr {{.*}} 'qux'
|
|
|
|
// DISABLED-NOT: -RecoveryExpr {{.*}} contains-errors
|
|
|
|
int unresolved_call2 = bar(baz(), qux());
|
|
|
|
|
|
|
|
constexpr int a = 10;
|
|
|
|
|
|
|
|
// CHECK: VarDecl {{.*}} postfix_inc
|
|
|
|
// CHECK-NEXT:`-RecoveryExpr {{.*}} contains-errors
|
|
|
|
// CHECK-NEXT: `-DeclRefExpr {{.*}} 'a'
|
|
|
|
// DISABLED-NOT: -RecoveryExpr {{.*}} contains-errors
|
|
|
|
int postfix_inc = a++;
|
|
|
|
|
|
|
|
// CHECK: VarDecl {{.*}} prefix_inc
|
|
|
|
// CHECK-NEXT:`-RecoveryExpr {{.*}} contains-errors
|
|
|
|
// CHECK-NEXT: `-DeclRefExpr {{.*}} 'a'
|
|
|
|
// DISABLED-NOT: -RecoveryExpr {{.*}} contains-errors
|
|
|
|
int prefix_inc = ++a;
|
|
|
|
|
|
|
|
// CHECK: VarDecl {{.*}} unary_address
|
|
|
|
// CHECK-NEXT:`-RecoveryExpr {{.*}} contains-errors
|
|
|
|
// CHECK-NEXT: `-ParenExpr {{.*}}
|
|
|
|
// CHECK-NEXT: `-BinaryOperator {{.*}} '+'
|
|
|
|
// CHECK-NEXT: |-ImplicitCastExpr
|
|
|
|
// CHECK-NEXT: | `-DeclRefExpr {{.*}} 'a'
|
|
|
|
// DISABLED-NOT: -RecoveryExpr {{.*}} contains-errors
|
|
|
|
int unary_address = &(a + 1);
|
|
|
|
|
|
|
|
// CHECK: VarDecl {{.*}} unary_bitinverse
|
|
|
|
// CHECK-NEXT:`-RecoveryExpr {{.*}} contains-errors
|
|
|
|
// CHECK-NEXT: `-ParenExpr {{.*}}
|
|
|
|
// CHECK-NEXT: `-BinaryOperator {{.*}} '+'
|
|
|
|
// CHECK-NEXT: |-ImplicitCastExpr
|
|
|
|
// CHECK-NEXT: | `-ImplicitCastExpr
|
|
|
|
// CHECK-NEXT: | `-DeclRefExpr {{.*}} 'a'
|
|
|
|
// DISABLED-NOT: -RecoveryExpr {{.*}} contains-errors
|
|
|
|
int unary_bitinverse = ~(a + 0.0);
|
|
|
|
|
|
|
|
// CHECK: VarDecl {{.*}} binary
|
|
|
|
// CHECK-NEXT:`-RecoveryExpr {{.*}} contains-errors
|
|
|
|
// CHECK-NEXT: |-DeclRefExpr {{.*}} 'a'
|
|
|
|
// CHECK-NEXT: `-CXXNullPtrLiteralExpr
|
|
|
|
// DISABLED-NOT: -RecoveryExpr {{.*}} contains-errors
|
|
|
|
int binary = a + nullptr;
|
|
|
|
|
|
|
|
// CHECK: VarDecl {{.*}} ternary
|
|
|
|
// CHECK-NEXT:`-RecoveryExpr {{.*}} contains-errors
|
|
|
|
// CHECK-NEXT: |-DeclRefExpr {{.*}} 'a'
|
|
|
|
// CHECK-NEXT: |-CXXNullPtrLiteralExpr
|
|
|
|
// CHECK-NEXT: `-DeclRefExpr {{.*}} 'a'
|
|
|
|
// DISABLED-NOT: -RecoveryExpr {{.*}} contains-errors
|
|
|
|
int ternary = a ? nullptr : a;
|
2020-03-25 19:43:53 +08:00
|
|
|
|
|
|
|
// CHECK: FunctionDecl
|
|
|
|
// CHECK-NEXT:|-ParmVarDecl {{.*}} x
|
|
|
|
// CHECK-NEXT:`-CompoundStmt
|
|
|
|
// CHECK-NEXT: |-RecoveryExpr {{.*}} contains-errors
|
|
|
|
// CHECK-NEXT: | `-DeclRefExpr {{.*}} 'foo'
|
|
|
|
// CHECK-NEXT: `-CallExpr {{.*}} contains-errors
|
|
|
|
// CHECK-NEXT: |-RecoveryExpr {{.*}} contains-errors
|
|
|
|
// CHECK-NEXT: | `-DeclRefExpr {{.*}} 'foo'
|
|
|
|
// CHECK-NEXT: `-DeclRefExpr {{.*}} 'x'
|
|
|
|
struct Foo {} foo;
|
|
|
|
void test(int x) {
|
|
|
|
foo.abc;
|
|
|
|
foo->func(x);
|
2020-04-13 05:06:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// CHECK: |-AlignedAttr {{.*}} alignas
|
|
|
|
// CHECK-NEXT:| `-RecoveryExpr {{.*}} contains-errors
|
|
|
|
// CHECK-NEXT:| `-UnresolvedLookupExpr {{.*}} 'invalid'
|
|
|
|
struct alignas(invalid()) Aligned {};
|
2020-04-21 15:57:06 +08:00
|
|
|
|
|
|
|
void InvalidInitalizer(int x) {
|
|
|
|
struct Bar { Bar(); };
|
|
|
|
// CHECK: `-VarDecl {{.*}} a1 'Bar'
|
|
|
|
// CHECK-NEXT: `-RecoveryExpr {{.*}} contains-errors
|
|
|
|
// CHECK-NEXT: `-IntegerLiteral {{.*}} 'int' 1
|
|
|
|
Bar a1(1);
|
|
|
|
// CHECK: `-VarDecl {{.*}} a2 'Bar'
|
|
|
|
// CHECK-NEXT: `-RecoveryExpr {{.*}} contains-errors
|
|
|
|
// CHECK-NEXT: `-DeclRefExpr {{.*}} 'x'
|
|
|
|
Bar a2(x);
|
|
|
|
// CHECK: `-VarDecl {{.*}} a3 'Bar'
|
|
|
|
// CHECK-NEXT: `-RecoveryExpr {{.*}} contains-errors
|
|
|
|
// CHECK-NEXT: `-InitListExpr
|
|
|
|
// CHECK-NEDT: `-DeclRefExpr {{.*}} 'x'
|
|
|
|
Bar a3{x};
|
|
|
|
// CHECK: `-VarDecl {{.*}} a4 'Bar'
|
|
|
|
// CHECK-NEXT: `-ParenListExpr {{.*}} 'NULL TYPE' contains-errors
|
|
|
|
// CHECK-NEXT: `-RecoveryExpr {{.*}} contains-errors
|
|
|
|
// CHECK-NEXT: `-UnresolvedLookupExpr {{.*}} 'invalid'
|
|
|
|
Bar a4(invalid());
|
|
|
|
// CHECK: `-VarDecl {{.*}} a5 'Bar'
|
|
|
|
// CHECK-NEXT: `-InitListExpr {{.*}} contains-errors
|
|
|
|
// CHECK-NEXT: `-RecoveryExpr {{.*}} contains-errors
|
|
|
|
// CHECK-NEXT: `-UnresolvedLookupExpr {{.*}} 'invalid'
|
|
|
|
Bar a5{invalid()};
|
|
|
|
|
|
|
|
// CHECK: `-VarDecl {{.*}} b1 'Bar'
|
|
|
|
// CHECK-NEXT: `-RecoveryExpr {{.*}} contains-errors
|
|
|
|
// CHECK-NEXT: `-IntegerLiteral {{.*}} 'int' 1
|
|
|
|
Bar b1 = 1;
|
|
|
|
// CHECK: `-VarDecl {{.*}} b2 'Bar'
|
|
|
|
// CHECK-NEXT: `-RecoveryExpr {{.*}} contains-errors
|
|
|
|
// CHECK-NEXT: `-InitListExpr
|
|
|
|
Bar b2 = {1};
|
|
|
|
// FIXME: preserve the invalid initializer.
|
|
|
|
// CHECK: `-VarDecl {{.*}} b3 'Bar'
|
|
|
|
Bar b3 = Bar(x);
|
|
|
|
// FIXME: preserve the invalid initializer.
|
|
|
|
// CHECK: `-VarDecl {{.*}} b4 'Bar'
|
|
|
|
Bar b4 = Bar{x};
|
|
|
|
// CHECK: `-VarDecl {{.*}} b5 'Bar'
|
|
|
|
// CHECK-NEXT: `-CXXUnresolvedConstructExpr {{.*}} 'Bar' contains-errors 'Bar'
|
|
|
|
// CHECK-NEXT: `-RecoveryExpr {{.*}} contains-errors
|
|
|
|
// CHECK-NEXT: `-UnresolvedLookupExpr {{.*}} 'invalid'
|
|
|
|
Bar b5 = Bar(invalid());
|
|
|
|
// CHECK: `-VarDecl {{.*}} b6 'Bar'
|
|
|
|
// CHECK-NEXT: `-CXXUnresolvedConstructExpr {{.*}} 'Bar' contains-errors 'Bar'
|
|
|
|
// CHECK-NEXT: `-InitListExpr {{.*}} contains-errors
|
|
|
|
// CHECK-NEXT: `-RecoveryExpr {{.*}} contains-errors
|
|
|
|
// CHECK-NEXT: `-UnresolvedLookupExpr {{.*}} 'invalid'
|
|
|
|
Bar b6 = Bar{invalid()};
|
|
|
|
}
|