https://github.com/alejandro-alvarez-sonarsource created https://github.com/llvm/llvm-project/pull/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`. From fe4adf2f95aeb72436eead3567767c8c539811c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20=C3=81lvarez=20Ayll=C3=B3n?= <alejandro.alva...@sonarsource.com> Date: Fri, 3 Jan 2025 08:58:23 +0100 Subject: [PATCH] [clang] Do not serialize function definitions without a body An instantiated templated function definition may not have a body due to parsing errors inside the templated function. When serializing, an assert is tripped inside `ASTRecordWriter::AddFunctionDefinition` when building with assertions enabled. The instantiation may happen on an intermediate module. The test case was reduced from `mp-units`. --- clang/lib/Serialization/ASTWriter.cpp | 6 ++- clang/test/Modules/missing-body-in-import.cpp | 42 +++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) 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 4a6027943072c0..36b9e3e2ba1720 100644 --- a/clang/lib/Serialization/ASTWriter.cpp +++ b/clang/lib/Serialization/ASTWriter.cpp @@ -6227,8 +6227,10 @@ void ASTWriter::WriteDeclUpdatesBlocks(ASTContext &Context, // Add a trailing update record, if any. These must go last because we // lazily load their attached statement. if (!GeneratingReducedBMI || !CanElideDeclDef(D)) { - if (HasUpdatedBody) { - const auto *Def = cast<FunctionDecl>(D); + assert(!(HasUpdatedBody && HasAddedVarDefinition) && + "Declaration can not be both a FunctionDecl and a VarDecl"); + if (const auto *Def = dyn_cast<FunctionDecl>(D); + HasUpdatedBody && Def->doesThisDeclarationHaveABody()) { Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION); Record.push_back(Def->isInlined()); Record.AddSourceLocation(Def->getInnerLocStart()); 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 00000000000000..b52ebba15087a3 --- /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 <unsigned N, unsigned M> +class A { +public: + constexpr A(const char[], const char[]) { + auto x = BrokenExpr; // expected-error {{use of undeclared identifier 'BrokenExpr'}} + } +}; + +export template<A<1,1> 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 <A{"a", "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 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits