Skip to content

Commit bf8a718

Browse files
committed
Allowing link decls in explicit submodules.
1 parent 42e1685 commit bf8a718

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

clang/lib/Lex/ModuleMapFile.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ struct ModuleMapFileParser {
119119
std::optional<UmbrellaDirDecl>
120120
parseUmbrellaDirDecl(SourceLocation UmbrellaLoc);
121121
std::optional<LinkDecl>
122-
parseLinkDecl(llvm::StringMap<SourceLocation> &SeenLinkDecl, bool TopLevel);
122+
parseLinkDecl(llvm::StringMap<SourceLocation> &SeenLinkDecl, bool Allowed);
123123

124124
SourceLocation consumeToken();
125125
void skipUntil(MMToken::TokenKind K);
@@ -407,7 +407,9 @@ std::optional<ModuleDecl> ModuleMapFileParser::parseModuleDecl(bool TopLevel) {
407407
break;
408408

409409
case MMToken::LinkKeyword:
410-
SubDecl = parseLinkDecl(SeenLinkDecl, TopLevel);
410+
// Link decls are only allowed in top level modules or explicit
411+
// submodules.
412+
SubDecl = parseLinkDecl(SeenLinkDecl, TopLevel || MDecl.Explicit);
411413
break;
412414

413415
default:
@@ -825,7 +827,7 @@ ModuleMapFileParser::parseUmbrellaDirDecl(clang::SourceLocation UmbrellaLoc) {
825827
/// module-declaration:
826828
/// 'link' 'framework'[opt] string-literal
827829
std::optional<LinkDecl> ModuleMapFileParser::parseLinkDecl(
828-
llvm::StringMap<SourceLocation> &SeenLinkDecl, bool TopLevel) {
830+
llvm::StringMap<SourceLocation> &SeenLinkDecl, bool Allowed) {
829831
assert(Tok.is(MMToken::LinkKeyword));
830832
LinkDecl LD;
831833
LD.Location = consumeToken();
@@ -853,7 +855,7 @@ std::optional<LinkDecl> ModuleMapFileParser::parseLinkDecl(
853855

854856
// Make sure we eat all the tokens when we report the errors so parsing
855857
// can continue.
856-
if (!TopLevel) {
858+
if (!Allowed) {
857859
Diags.Report(LD.Location, diag::err_mmap_submodule_link_decl);
858860
HadError = true;
859861
return std::nullopt;

clang/test/ClangScanDeps/link-libraries-diag-dup.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ module A {
1111
link "libraryB"
1212
}
1313

14+
explicit module D {
15+
header "D.h"
16+
link "libraryD"
17+
}
18+
1419
link "libraryA"
1520
link "libraryA"
1621
}
@@ -26,9 +31,12 @@ module C {
2631
// empty
2732
//--- C.h
2833
// empty
34+
//--- D.h
35+
// empty
2936
//--- TU.c
3037
#include "A.h"
3138
#include "C.h"
39+
#include "D.h"
3240

3341
//--- cdb.json.template
3442
[{
@@ -41,7 +49,9 @@ module C {
4149
// RUN: not clang-scan-deps -compilation-database %t/cdb.json -format \
4250
// RUN: experimental-full 2>&1 | FileCheck %s
4351

44-
// CHECK: module.modulemap:6:5: error: link decl is not allowed in submodules
45-
// CHECK-NEXT: module.modulemap:10:3: error: redeclaration of link library 'libraryA'
46-
// CHECK-NEXT: module.modulemap:9:3: note: previously declared here
47-
// CHECK-NOT: module.modulemap:15:3: error: redeclaration of link library 'libraryA'
52+
// Note that module D does not report an error because it is explicit.
53+
// Therefore we can use CHECK-NEXT for the redeclaration error on line 15.
54+
// CHECK: module.modulemap:6:5: error: link declaration is not allowed in submodules
55+
// CHECK-NEXT: module.modulemap:15:3: error: redeclaration of link library 'libraryA'
56+
// CHECK-NEXT: module.modulemap:14:3: note: previously declared here
57+
// CHECK-NOT: module.modulemap:20:3: error: redeclaration of link library 'libraryA'

0 commit comments

Comments
 (0)