https://github.com/qiongsiwu created 
https://github.com/llvm/llvm-project/pull/157154

…ations in `modulemap`s (#148959)"

This reverts commit 538e9e8ebd09233b3900ed2dfd23e4e1ca5c9fc0 for two reasons.

1. Link decls in submodules can make sense even if the submodule is not 
explicit. We need to review the error check. This PR reverts the check so we 
still allow link decls in submodules. 
2. It is not a fatal error to have duplicating link decls. The linker 
deduplicates them anyways. 

rdar://159467837



>From 275af094099dfd19fae69797f451ca2c5774adbf Mon Sep 17 00:00:00 2001
From: Qiongsi Wu <qiongsi...@apple.com>
Date: Fri, 5 Sep 2025 11:00:30 -0700
Subject: [PATCH] Revert "[clang][Modules] Reporting Errors for Duplicating
 Link Declarations in `modulemap`s (#148959)"

This reverts commit 538e9e8ebd09233b3900ed2dfd23e4e1ca5c9fc0.
---
 .../include/clang/Basic/DiagnosticLexKinds.td |  5 --
 clang/lib/Lex/ModuleMapFile.cpp               | 34 ++---------
 .../ClangScanDeps/link-libraries-diag-dup.c   | 57 -------------------
 3 files changed, 4 insertions(+), 92 deletions(-)
 delete mode 100644 clang/test/ClangScanDeps/link-libraries-diag-dup.c

diff --git a/clang/include/clang/Basic/DiagnosticLexKinds.td 
b/clang/include/clang/Basic/DiagnosticLexKinds.td
index c03c4033cd3a6..c7fe6e1db6d1f 100644
--- a/clang/include/clang/Basic/DiagnosticLexKinds.td
+++ b/clang/include/clang/Basic/DiagnosticLexKinds.td
@@ -912,11 +912,6 @@ def err_mmap_nested_submodule_id : Error<
   "qualified module name can only be used to define modules at the top level">;
 def err_mmap_expected_feature : Error<"expected a feature name">;
 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<
diff --git a/clang/lib/Lex/ModuleMapFile.cpp b/clang/lib/Lex/ModuleMapFile.cpp
index f0cd9d2bee82a..183e919d14c22 100644
--- a/clang/lib/Lex/ModuleMapFile.cpp
+++ b/clang/lib/Lex/ModuleMapFile.cpp
@@ -118,8 +118,7 @@ struct ModuleMapFileParser {
   std::optional<ExcludeDecl> parseExcludeDecl(clang::SourceLocation 
LeadingLoc);
   std::optional<UmbrellaDirDecl>
   parseUmbrellaDirDecl(SourceLocation UmbrellaLoc);
-  std::optional<LinkDecl>
-  parseLinkDecl(llvm::StringMap<SourceLocation> &SeenLinkDecl, bool Allowed);
+  std::optional<LinkDecl> parseLinkDecl();
 
   SourceLocation consumeToken();
   void skipUntil(MMToken::TokenKind K);
@@ -326,7 +325,6 @@ std::optional<ModuleDecl> 
ModuleMapFileParser::parseModuleDecl(bool TopLevel) {
   SourceLocation LBraceLoc = consumeToken();
 
   bool Done = false;
-  llvm::StringMap<SourceLocation> SeenLinkDecl;
   do {
     std::optional<Decl> SubDecl;
     switch (Tok.Kind) {
@@ -407,9 +405,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();
       break;
 
     default:
@@ -826,8 +822,7 @@ 
ModuleMapFileParser::parseUmbrellaDirDecl(clang::SourceLocation UmbrellaLoc) {
 ///
 ///   module-declaration:
 ///     'link' 'framework'[opt] string-literal
-std::optional<LinkDecl> ModuleMapFileParser::parseLinkDecl(
-    llvm::StringMap<SourceLocation> &SeenLinkDecl, bool Allowed) {
+std::optional<LinkDecl> ModuleMapFileParser::parseLinkDecl() {
   assert(Tok.is(MMToken::LinkKeyword));
   LinkDecl LD;
   LD.Location = consumeToken();
@@ -843,33 +838,12 @@ std::optional<LinkDecl> 
ModuleMapFileParser::parseLinkDecl(
   if (!Tok.is(MMToken::StringLiteral)) {
     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_library_name)
         << LD.Framework << SourceRange(LD.Location);
-    consumeToken();
     HadError = true;
     return std::nullopt;
   }
 
-  StringRef Library = Tok.getString();
-
-  LD.Library = Library;
+  LD.Library = Tok.getString();
   consumeToken();
-
-  // Make sure we eat all the tokens 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);
 }
 
diff --git a/clang/test/ClangScanDeps/link-libraries-diag-dup.c 
b/clang/test/ClangScanDeps/link-libraries-diag-dup.c
deleted file mode 100644
index e6612ca7bd216..0000000000000
--- a/clang/test/ClangScanDeps/link-libraries-diag-dup.c
+++ /dev/null
@@ -1,57 +0,0 @@
-// RUN: rm -rf %t
-// RUN: mkdir %t
-// RUN: split-file %s %t
-
-//--- module.modulemap
-module A {
-  umbrella header "A.h"
-
-  module B {
-    header "B.h"
-    link "libraryB"
-  }
-
-  explicit module D {
-    header "D.h"
-    link "libraryD"
-  }
-
-  link "libraryA"
-  link "libraryA"
-}
-
-module C {
-  header "C.h"
-  link "libraryA"
-}
-
-//--- A.h
-#include "B.h"
-//--- B.h
-// empty
-//--- C.h
-// empty
-//--- D.h
-// empty
-//--- TU.c
-#include "A.h"
-#include "C.h"
-#include "D.h"
-
-//--- cdb.json.template
-[{
-  "file": "DIR/TU.c",
-  "directory": "DIR",
-  "command": "clang -fmodules -fmodules-cache-path=DIR/cache -I DIR -c 
DIR/TU.c"
-}]
-
-// RUN: sed "s|DIR|%/t|g" %t/cdb.json.template > %t/cdb.json
-// 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'

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to