From a13bcf3ced35b0df89ac13670690b4482052e47f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20=C3=81lvarez=20Ayll=C3=B3n?= Date: Mon, 6 Jan 2025 11:52:11 +0100 Subject: [PATCH] [clang] Do not serialize function definitions without a body (#121550) An instantiated templated function definition may not have a body due to parsing errors inside the templated function. When serializing, an assert is triggered inside `ASTRecordWriter::AddFunctionDefinition`. The instantiation may happen on an intermediate module. The test case was reduced from `mp-units`. --- clang/lib/Serialization/ASTWriter.cpp | 8 ++++ clang/test/Modules/missing-body-in-import.cpp | 42 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 clang/test/Modules/missing-body-in-import.cpp diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp index 4a6027943072c..7fa9322e7551f 100644 --- a/clang/lib/Serialization/ASTWriter.cpp +++ b/clang/lib/Serialization/ASTWriter.cpp @@ -7230,6 +7230,10 @@ void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) { if (!D->isFromASTFile()) return; // Declaration not imported from PCH. + // The function definition may not have a body due to parsing errors. + if (!D->doesThisDeclarationHaveABody()) + return; + // Implicit function decl from a PCH was defined. DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION)); } @@ -7249,6 +7253,10 @@ void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) { if (!D->isFromASTFile()) return; + // The function definition may not have a body due to parsing errors. + if (!D->doesThisDeclarationHaveABody()) + return; + DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION)); } diff --git a/clang/test/Modules/missing-body-in-import.cpp b/clang/test/Modules/missing-body-in-import.cpp new file mode 100644 index 0000000000000..b52ebba15087a --- /dev/null +++ b/clang/test/Modules/missing-body-in-import.cpp @@ -0,0 +1,42 @@ +// RUN: rm -rf %t +// RUN: split-file %s %t +// RUN: cd %t + +// RUN: %clang_cc1 -std=c++23 mod1.cppm -emit-module-interface -o mod1.pcm -fallow-pcm-with-compiler-errors -verify +// RUN: %clang_cc1 -std=c++23 mod2.cppm -emit-module-interface -o mod2.pcm -fmodule-file=mod1=mod1.pcm -verify -fallow-pcm-with-compiler-errors +// RUN: %clang_cc1 -std=c++23 mod3.cppm -emit-module-interface -o mod3.pcm -fmodule-file=mod1=mod1.pcm -fmodule-file=mod2=mod2.pcm -verify -fallow-pcm-with-compiler-errors +// RUN: %clang_cc1 -std=c++23 main.cpp -fmodule-file=mod1=mod1.pcm -fmodule-file=mod2=mod2.pcm -fmodule-file=mod3=mod3.pcm -verify -fallow-pcm-with-compiler-errors -ast-dump-all + +//--- mod1.cppm +export module mod1; + +export template +class A { +public: + constexpr A(const char[], const char[]) { + auto x = BrokenExpr; // expected-error {{use of undeclared identifier 'BrokenExpr'}} + } +}; + +export template NTTP> +struct B {}; + +template < unsigned N, unsigned M > +A(const char (&)[N], const char (&)[M]) -> A< 1, 1 >; + +//--- mod2.cppm +export module mod2; +import mod1; + +struct C: B { // expected-error {{non-type template argument is not a constant expression}} + constexpr C(int a) { } +}; + +//--- mod3.cppm +// expected-no-diagnostics +export module mod3; +export import mod2; + +//--- main.cpp +// expected-no-diagnostics +import mod3; // no crash