Skip to content

Commit 569e94f

Browse files
committed
[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.
1 parent 170b9ca commit 569e94f

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
@@ -3753,6 +3753,13 @@ void ASTDeclReader::checkMultipleDefinitionInNamedModules(ASTReader &Reader,
37533753
if (D->getFriendObjectKind() || Previous->getFriendObjectKind())
37543754
return;
37553755

3756+
// Skip diagnosing in-class declarations.
3757+
if (!Previous->getLexicalDeclContext()
3758+
->getNonTransparentContext()
3759+
->isFileContext() ||
3760+
!D->getLexicalDeclContext()->getNonTransparentContext()->isFileContext())
3761+
return;
3762+
37563763
Module *M = Previous->getOwningModule();
37573764
if (!M)
37583765
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)