Skip to content

Fix crash with delayed typo correction #140571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,9 @@ Improvements to Clang's diagnostics
constant expression. Prior to this, the error inaccurately implied that assert
could not be used at all in a constant expression (#GH130458)

- No longer crashing during code generation due to delayed typo correction not
causing an unrecoverable error until the end of the translation unit. (#GH140461)

- A new off-by-default warning ``-Wms-bitfield-padding`` has been added to alert to cases where bit-field
packing may differ under the MS struct ABI (#GH117428).

Expand Down
12 changes: 12 additions & 0 deletions clang/include/clang/Basic/Diagnostic.h
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,18 @@ class DiagnosticsEngine : public RefCountedBase<DiagnosticsEngine> {

bool hasErrorOccurred() const { return ErrorOccurred; }

/// Sometimes we know an error will be produced at the end of a translation
/// unit, such as a delayed typo correction. However, CodeGen is run on each
/// top-level decl in an incremental fashion. We don't want CodeGen to run on
/// a declaration if an unrecoverable error occurred, but in those cases, the
/// error has not yet been emitted. This helper function lets you specify
/// that an unrecoverable error will occur later, specifically to ensure that
/// CodeGen is not run on those declarations.
void setUnrecoverableErrorWillOccur() {
UnrecoverableErrorOccurred = true;
ErrorOccurred = true;
}

/// Errors that actually prevent compilation, not those that are
/// upgraded from a warning by -Werror.
bool hasUncompilableErrorOccurred() const {
Expand Down
7 changes: 7 additions & 0 deletions clang/lib/Sema/SemaLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5420,6 +5420,13 @@ TypoExpr *Sema::CorrectTypoDelayed(
TypoDiagnosticGenerator TDG, TypoRecoveryCallback TRC, CorrectTypoKind Mode,
DeclContext *MemberContext, bool EnteringContext,
const ObjCObjectPointerType *OPT) {

// A typo is an unrecoverable diagnostic. However, we sometimes perform code
// gen incrementally rather than waiting until the end of the TU, which means
// we want to tell the diagnostics engine that an unrecoverable error will
// occur eventually.
getDiagnostics().setUnrecoverableErrorWillOccur();

auto Consumer = makeTypoCorrectionConsumer(
TypoName, LookupKind, S, SS, CCC, MemberContext, EnteringContext, OPT,
Mode == CorrectTypoKind::ErrorRecovery);
Expand Down
11 changes: 11 additions & 0 deletions clang/test/CodeGenCXX/typo-correction-delayed.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: %clang_cc1 -emit-llvm %s -verify -o /dev/null

// Previously, delayed typo correction would cause a crash because codegen is
// run on each top-level declaration as we finish with it unless an
// unrecoverable error occured. However, with delayed typo correction, the
// error is not emit until the end of the TU, so CodeGen would be run on
// invalid declarations.
namespace GH140461 {
auto s{new auto(one)}; // expected-error {{use of undeclared identifier 'one'}}
} // namespace GH140461

Loading