https://github.com/AaronBallman updated https://github.com/llvm/llvm-project/pull/140571
>From e63781710d4f4307f4201670d96df9b511ad9551 Mon Sep 17 00:00:00 2001 From: Aaron Ballman <aa...@aaronballman.com> Date: Mon, 19 May 2025 12:33:24 -0400 Subject: [PATCH] Fix crash with delayed typo correction Delayed typo correction does not emit a diagnostic until the end of the TU. However, codegen is run on each top-level declaration unless an unrecoverable error occurred. The check for the diagnostic ensures that CodeGen does not have to handle generating code for invalid constructs. Due to typo correction delaying unrecoverable diagnostic emission until the end of the TU, we would try to run CodeGen on invalid AST nodes and that broke some invariants. Now, we specify that an unrecoverable error will be emitted when emitting the delayed typo correction so that we skip performing code generation. Fixes #140461 --- clang/docs/ReleaseNotes.rst | 3 +++ clang/include/clang/Basic/Diagnostic.h | 12 ++++++++++++ clang/lib/Sema/SemaLookup.cpp | 7 +++++++ clang/test/CodeGenCXX/typo-correction-delayed.cpp | 11 +++++++++++ 4 files changed, 33 insertions(+) create mode 100644 clang/test/CodeGenCXX/typo-correction-delayed.cpp diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 4a3c1bee82831..9a6ed5ac55b39 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -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). diff --git a/clang/include/clang/Basic/Diagnostic.h b/clang/include/clang/Basic/Diagnostic.h index 49ef22d4e4eb6..0e540fb2db346 100644 --- a/clang/include/clang/Basic/Diagnostic.h +++ b/clang/include/clang/Basic/Diagnostic.h @@ -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 { diff --git a/clang/lib/Sema/SemaLookup.cpp b/clang/lib/Sema/SemaLookup.cpp index 29bf274f8a39f..f263feeda17a2 100644 --- a/clang/lib/Sema/SemaLookup.cpp +++ b/clang/lib/Sema/SemaLookup.cpp @@ -5419,6 +5419,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); diff --git a/clang/test/CodeGenCXX/typo-correction-delayed.cpp b/clang/test/CodeGenCXX/typo-correction-delayed.cpp new file mode 100644 index 0000000000000..3000e768fd163 --- /dev/null +++ b/clang/test/CodeGenCXX/typo-correction-delayed.cpp @@ -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 + _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits