Skip to content

Commit ac97cff

Browse files
ChuanqiXu9tstellar
authored andcommitted
[C++20] [Modules] Don't diagnose duplicated declarations in different modules which is not in file scope
Close #126373 Although the root problems should be we shouldn't place the friend declaration to the incorrect module, let's avoid bleeding the edge by stoping diagnosing declarations not in file scope. (cherry picked from commit 569e94f)
1 parent 923d35b commit ac97cff

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

clang/lib/Serialization/ASTReaderDecl.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3751,6 +3751,13 @@ void ASTDeclReader::checkMultipleDefinitionInNamedModules(ASTReader &Reader,
37513751
if (D->getFriendObjectKind() || Previous->getFriendObjectKind())
37523752
return;
37533753

3754+
// Skip diagnosing in-class declarations.
3755+
if (!Previous->getLexicalDeclContext()
3756+
->getNonTransparentContext()
3757+
->isFileContext() ||
3758+
!D->getLexicalDeclContext()->getNonTransparentContext()->isFileContext())
3759+
return;
3760+
37543761
Module *M = Previous->getOwningModule();
37553762
if (!M)
37563763
return;

clang/test/Modules/pr126373.cppm

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// RUN: rm -rf %t
2+
// RUN: mkdir -p %t
3+
// RUN: split-file %s %t
4+
//
5+
// RUN: %clang_cc1 -std=c++20 %t/module1.cppm -emit-module-interface -o %t/module1.pcm
6+
// RUN: %clang_cc1 -std=c++20 -fmodule-file=module1=%t/module1.pcm %t/module2.cppm \
7+
// RUN: -emit-module-interface -o %t/module2.pcm
8+
// RUN: %clang_cc1 -std=c++20 %t/module2.pcm -fmodule-file=module1=%t/module1.pcm \
9+
// RUN: -emit-llvm -o - | FileCheck %t/module2.cppm
10+
11+
//--- test.h
12+
template<typename T>
13+
struct Test {
14+
template<typename U>
15+
friend class Test;
16+
};
17+
18+
//--- module1.cppm
19+
module;
20+
#include "test.h"
21+
export module module1;
22+
export void f1(Test<int>) {}
23+
24+
//--- module2.cppm
25+
module;
26+
#include "test.h"
27+
export module module2;
28+
import module1;
29+
export void f2(Test<float>) {}
30+
31+
extern "C" void func() {}
32+
33+
// Fine enough to check the IR is emitted correctly.
34+
// CHECK: define{{.*}}@func

0 commit comments

Comments
 (0)