Skip to content
Open
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
2 changes: 0 additions & 2 deletions clang/include/clang/Basic/DiagnosticLexKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -915,8 +915,6 @@ def err_mmap_expected_attribute : Error<"expected an attribute name">;
def warn_mmap_link_redeclaration : Warning<"redeclaration of link library '%0'">,
InGroup<DiagGroup<"module-link-redeclaration">>, DefaultError;
def note_mmap_prev_link_declaration : Note<"previously declared here">;
def err_mmap_submodule_link_decl
: Error<"link declaration is not allowed in submodules">;
def warn_mmap_unknown_attribute : Warning<"unknown attribute '%0'">,
InGroup<IgnoredAttributes>;
def warn_mmap_mismatched_private_submodule : Warning<
Expand Down
18 changes: 4 additions & 14 deletions clang/lib/Lex/ModuleMapFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ struct ModuleMapFileParser {
std::optional<UmbrellaDirDecl>
parseUmbrellaDirDecl(SourceLocation UmbrellaLoc);
std::optional<LinkDecl>
parseLinkDecl(llvm::StringMap<SourceLocation> &SeenLinkDecl, bool Allowed);
parseLinkDecl(llvm::StringMap<SourceLocation> &SeenLinkDecl);

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

case MMToken::LinkKeyword:
// Link decls are only allowed in top level modules or explicit
// submodules.
SubDecl = parseLinkDecl(SeenLinkDecl, TopLevel || MDecl.Explicit);
SubDecl = parseLinkDecl(SeenLinkDecl);
break;

default:
Expand Down Expand Up @@ -827,7 +825,7 @@ ModuleMapFileParser::parseUmbrellaDirDecl(clang::SourceLocation UmbrellaLoc) {
/// module-declaration:
/// 'link' 'framework'[opt] string-literal
std::optional<LinkDecl> ModuleMapFileParser::parseLinkDecl(
llvm::StringMap<SourceLocation> &SeenLinkDecl, bool Allowed) {
llvm::StringMap<SourceLocation> &SeenLinkDecl) {
assert(Tok.is(MMToken::LinkKeyword));
LinkDecl LD;
LD.Location = consumeToken();
Expand All @@ -853,21 +851,13 @@ std::optional<LinkDecl> ModuleMapFileParser::parseLinkDecl(
LD.Library = Library;
consumeToken();

// Make sure we eat all the tokens when we report the errors so parsing
// Make sure we eat all the token when we report the errors so parsing
// can continue.
if (!Allowed) {
Diags.Report(LD.Location, diag::err_mmap_submodule_link_decl);
HadError = true;
return std::nullopt;
}

auto [It, Inserted] =
SeenLinkDecl.insert(std::make_pair(Library, LD.Location));
if (!Inserted) {
Diags.Report(LD.Location, diag::warn_mmap_link_redeclaration) << Library;
Diags.Report(It->second, diag::note_mmap_prev_link_declaration);
HadError = true;
return std::nullopt;
}

return std::move(LD);
Expand Down
15 changes: 9 additions & 6 deletions clang/test/ClangScanDeps/link-libraries-diag-dup.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ module A {
explicit module D {
header "D.h"
link "libraryD"
link "libraryD"
}

link "libraryB"
link "libraryA"
link "libraryA"
}
Expand Down Expand Up @@ -49,9 +51,10 @@ module C {
// RUN: not clang-scan-deps -compilation-database %t/cdb.json -format \
// RUN: experimental-full 2>&1 | FileCheck %s

// Note that module D does not report an error because it is explicit.
// Therefore we can use CHECK-NEXT for the redeclaration error on line 15.
// CHECK: module.modulemap:6:5: error: link declaration is not allowed in submodules
// CHECK-NEXT: module.modulemap:15:3: error: redeclaration of link library 'libraryA' [-Wmodule-link-redeclaration]
// CHECK-NEXT: module.modulemap:14:3: note: previously declared here
// CHECK-NOT: module.modulemap:20:3: error: redeclaration of link library 'libraryA'
// Note that the `link "libraryB"` in the top level module A does not
// cause an issue because we only check within a module.
// CHECK: 12:5: error: redeclaration of link library 'libraryD' [-Wmodule-link-redeclaration]
// CHECK-NEXT: 11:5: note: previously declared here
// CHECK-NEXT: 17:3: error: redeclaration of link library 'libraryA' [-Wmodule-link-redeclaration]
// CHECK-NEXT: 16:3: note: previously declared here
// CHECK-NOT: error: redeclaration of link library 'libraryB'