Skip to content

[clang] Do not serialize function definitions without a body #121550

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

Merged
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
8 changes: 8 additions & 0 deletions clang/lib/Serialization/ASTWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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));
}

Expand Down
42 changes: 42 additions & 0 deletions clang/test/Modules/missing-body-in-import.cpp
Original file line number Diff line number Diff line change
@@ -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
Loading