llvm-project/clang/test/AST/ast-dump-expr-errors.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

47 lines
2.1 KiB
C++
Raw Normal View History

[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 -ast-dump -frecovery-ast %s | FileCheck -strict-whitespace %s
// Check errors flag is set for RecoveryExpr.
//
// CHECK: VarDecl {{.*}} a
// CHECK-NEXT:`-RecoveryExpr {{.*}} contains-errors
// CHECK-NEXT: `-UnresolvedLookupExpr {{.*}} 'bar'
int a = bar();
// The flag propagates through more complicated calls.
//
// CHECK: VarDecl {{.*}} b
// 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'
int b = bar(baz(), qux());
// Also propagates through more complicated expressions.
//
// CHECK: |-VarDecl {{.*}} c
// CHECK-NEXT:| `-BinaryOperator {{.*}} '<dependent type>' contains-errors '*'
// CHECK-NEXT:| |-UnaryOperator {{.*}} '<dependent type>' contains-errors prefix '&'
// CHECK-NEXT:| | `-ParenExpr {{.*}} '<dependent type>' contains-errors
// CHECK-NEXT:| | `-BinaryOperator {{.*}} '<dependent type>' contains-errors '+'
// CHECK-NEXT:| | |-RecoveryExpr {{.*}} '<dependent type>' contains-errors
// CHECK-NEXT:| | | `-UnresolvedLookupExpr {{.*}} 'bar'
// CHECK-NEXT:| | `-RecoveryExpr {{.*}} '<dependent type>' contains-errors
// CHECK-NEXT:| | `-UnresolvedLookupExpr {{.*}} 'baz'
int c = &(bar() + baz()) * 10;
// Errors flag propagates even when type is not dependent anymore.
// CHECK: |-VarDecl {{.*}} d
// CHECK-NEXT:| `-CXXStaticCastExpr {{.*}} 'int' contains-errors
// CHECK-NEXT:| `-BinaryOperator {{.*}} '<dependent type>' contains-errors '+'
// CHECK-NEXT:| |-RecoveryExpr {{.*}} '<dependent type>' contains-errors
// CHECK-NEXT:| | `-UnresolvedLookupExpr {{.*}} 'bar'
// CHECK-NEXT:| `-IntegerLiteral {{.*}} 1
int d = static_cast<int>(bar() + 1);
// Error type should result in an invalid decl.
// CHECK: -VarDecl {{.*}} invalid f 'decltype(<recovery-expr>(bar))'
decltype(bar()) f;