Skip to content

Commit cad6bba

Browse files
authoredJan 23, 2025
[C++20][Modules] Fix crash/compiler error due broken AST links (#123648)
Summary: This PR fixes bugreport llvm/llvm-project#122493 The root problem is the same as before lambda function and DeclRefExpr references a variable that does not belong to the same module as the enclosing function body. Therefore iteration over the function body doesn’t visit the VarDecl. Before this change RelatedDeclsMap was created only for canonical decl but in reality it has to be done for the definition of the function that does not always match the canonical decl. Test Plan: check-clang
1 parent 1930635 commit cad6bba

File tree

4 files changed

+132
-16
lines changed

4 files changed

+132
-16
lines changed
 

‎clang/docs/ReleaseNotes.rst

+2
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,8 @@ Bug Fixes to C++ Support
975975
- Fixed a nested lambda substitution issue for constraint evaluation. (#GH123441)
976976
- Fixed various false diagnostics related to the use of immediate functions. (#GH123472)
977977
- Fix immediate escalation not propagating through inherited constructors. (#GH112677)
978+
- Fixed assertions or false compiler diagnostics in the case of C++ modules for
979+
lambda functions or inline friend functions defined inside templates (#GH122493).
978980

979981
Bug Fixes to AST Handling
980982
^^^^^^^^^^^^^^^^^^^^^^^^^

‎clang/include/clang/Serialization/ASTReader.h

+12-5
Original file line numberDiff line numberDiff line change
@@ -546,11 +546,18 @@ class ASTReader
546546

547547
/// Mapping from main decl ID to the related decls IDs.
548548
///
549-
/// These related decls have to be loaded right after the main decl.
550-
/// It is required to have canonical declaration for related decls from the
551-
/// same module as the enclosing main decl. Without this, due to lazy
552-
/// deserialization, canonical declarations for the main decl and related can
553-
/// be selected from different modules.
549+
/// The key is the main decl ID, and the value is a vector of related decls
550+
/// that must be loaded immediately after the main decl. This is necessary
551+
/// to ensure that the definition for related decls comes from the same module
552+
/// as the enclosing main decl. Without this, due to lazy deserialization,
553+
/// the definition for the main decl and related decls may come from different
554+
/// modules. It is used for the following cases:
555+
/// - Lambda inside a template function definition: The main declaration is
556+
/// the enclosing function, and the related declarations are the lambda
557+
/// declarations.
558+
/// - Friend function defined inside a template CXXRecord declaration: The
559+
/// main declaration is the enclosing record, and the related declarations
560+
/// are the friend functions.
554561
llvm::DenseMap<GlobalDeclID, SmallVector<GlobalDeclID, 4>> RelatedDeclsMap;
555562

556563
struct PendingUpdateRecord {

‎clang/lib/Serialization/ASTWriterDecl.cpp

+26-11
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@
2727
using namespace clang;
2828
using namespace serialization;
2929

30+
//===----------------------------------------------------------------------===//
31+
// Utility functions
32+
//===----------------------------------------------------------------------===//
33+
34+
namespace {
35+
36+
// Helper function that returns true if the decl passed in the argument is
37+
// a defintion in dependent contxt.
38+
template <typename DT> bool isDefinitionInDependentContext(DT *D) {
39+
return D->isDependentContext() && D->isThisDeclarationADefinition();
40+
}
41+
42+
} // namespace
43+
3044
//===----------------------------------------------------------------------===//
3145
// Declaration serialization
3246
//===----------------------------------------------------------------------===//
@@ -801,14 +815,14 @@ void ASTDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
801815
}
802816

803817
if (D->getFriendObjectKind()) {
804-
// For a function defined inline within a class template, we have to force
805-
// the canonical definition to be the one inside the canonical definition of
806-
// the template. Remember this relation to deserialize them together.
807-
if (auto *RD = dyn_cast<CXXRecordDecl>(D->getLexicalParent()))
808-
if (RD->isDependentContext() && RD->isThisDeclarationADefinition()) {
809-
Writer.RelatedDeclsMap[Writer.GetDeclRef(RD)].push_back(
810-
Writer.GetDeclRef(D));
811-
}
818+
// For a friend function defined inline within a class template, we have to
819+
// force the definition to be the one inside the definition of the template
820+
// class. Remember this relation to deserialize them together.
821+
if (auto *RD = dyn_cast<CXXRecordDecl>(D->getLexicalParent());
822+
RD && isDefinitionInDependentContext(RD)) {
823+
Writer.RelatedDeclsMap[Writer.GetDeclRef(RD)].push_back(
824+
Writer.GetDeclRef(D));
825+
}
812826
}
813827

814828
Record.push_back(D->param_size());
@@ -1583,9 +1597,10 @@ void ASTDeclWriter::VisitCXXRecordDecl(CXXRecordDecl *D) {
15831597
} else {
15841598
Record.push_back(0);
15851599
}
1586-
// For lambdas inside canonical FunctionDecl remember the mapping.
1587-
if (auto FD = llvm::dyn_cast_or_null<FunctionDecl>(D->getDeclContext());
1588-
FD && FD->isCanonicalDecl()) {
1600+
// For lambdas inside template functions, remember the mapping to
1601+
// deserialize them together.
1602+
if (auto *FD = llvm::dyn_cast_or_null<FunctionDecl>(D->getDeclContext());
1603+
FD && isDefinitionInDependentContext(FD)) {
15891604
Writer.RelatedDeclsMap[Writer.GetDeclRef(FD)].push_back(
15901605
Writer.GetDeclRef(D));
15911606
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// RUN: rm -fR %t
2+
// RUN: split-file %s %t
3+
// RUN: cd %t
4+
// RUN: %clang_cc1 -verify -std=c++20 -Werror=uninitialized -xc++ -emit-module module.cppmap -fmodule-name=mock_resolver -o mock_resolver.pcm
5+
// RUN: %clang_cc1 -verify -std=c++20 -Werror=uninitialized -xc++ -emit-module module.cppmap -fmodule-name=sql_internal -o sql_internal.pcm
6+
// RUN: %clang_cc1 -verify -std=c++20 -Werror=uninitialized -xc++ -fmodule-file=mock_resolver.pcm -fmodule-file=sql_internal.pcm main.cc -o main.o
7+
8+
//--- module.cppmap
9+
module "mock_resolver" {
10+
export *
11+
module "mock_resolver.h" {
12+
export *
13+
header "mock_resolver.h"
14+
}
15+
}
16+
17+
module "sql_internal" {
18+
export *
19+
module "sql_transform_builder.h" {
20+
export *
21+
header "sql_transform_builder.h"
22+
}
23+
}
24+
25+
//--- set_bits2.h
26+
// expected-no-diagnostics
27+
#pragma once
28+
29+
template <typename T>
30+
void fwd(const T& x) {}
31+
32+
namespace vox::bitset {
33+
34+
template <typename TFunc>
35+
void ForEachSetBit2(const TFunc&) {
36+
fwd([](int) {
37+
const int bit_index_base = 0;
38+
(void)[&](int) {
39+
int v = bit_index_base;
40+
};
41+
});
42+
}
43+
44+
} // namespace vox::bitset
45+
46+
//--- sql_transform_builder.h
47+
// expected-no-diagnostics
48+
#pragma once
49+
50+
#include "set_bits2.h"
51+
52+
class QualifyingSet3 {
53+
public:
54+
void GetIndexes() const {
55+
vox::bitset::ForEachSetBit2([]() {});
56+
}
57+
};
58+
59+
template <typename T>
60+
void DoTransform() {
61+
vox::bitset::ForEachSetBit2([]() {});
62+
}
63+
64+
//--- mock_resolver.h
65+
// expected-no-diagnostics
66+
#pragma once
67+
#include "set_bits2.h"
68+
69+
class QualifyingSet2 {
70+
public:
71+
void GetIndexes() const {
72+
vox::bitset::ForEachSetBit2([]() {});
73+
}
74+
};
75+
76+
//--- main.cc
77+
// expected-no-diagnostics
78+
#include "sql_transform_builder.h"
79+
80+
template <typename Callable>
81+
void get(const Callable& fn) {
82+
fwd<Callable>(fn);
83+
}
84+
85+
namespace {
86+
87+
void test() {
88+
get([]() {});
89+
DoTransform<int>();
90+
}
91+
92+
} // namespace

0 commit comments

Comments
 (0)