Skip to content

Commit 68bc270

Browse files
[IncludeTree] Suppot export_as field in ModuleMap
Add support for `export_as` in module map for include-tree. rdar://125421391
1 parent b7b175a commit 68bc270

File tree

5 files changed

+96
-7
lines changed

5 files changed

+96
-7
lines changed

clang/include/clang/CAS/IncludeTree.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,16 +462,24 @@ class IncludeTree::Module : public IncludeTreeBase<Module> {
462462
bool InferSubmodules : 1;
463463
bool InferExplicitSubmodules : 1;
464464
bool InferExportWildcard : 1;
465+
bool UseExportAsModuleLinkName: 1;
465466
ModuleFlags()
466467
: IsFramework(false), IsExplicit(false), IsExternC(false),
467468
IsSystem(false), InferSubmodules(false),
468-
InferExplicitSubmodules(false), InferExportWildcard(false) {}
469+
InferExplicitSubmodules(false), InferExportWildcard(false),
470+
UseExportAsModuleLinkName(false) {}
469471
};
470472

471473
ModuleFlags getFlags() const;
472474

473475
/// The name of the current (sub)module.
474-
StringRef getName() const { return dataAfterFlags(); }
476+
StringRef getName() const {
477+
return dataAfterFlags().split('\0').first;
478+
}
479+
480+
StringRef getExportAsModule() const {
481+
return dataAfterFlags().split('\0').second;
482+
}
475483

476484
size_t getNumSubmodules() const;
477485

@@ -505,7 +513,7 @@ class IncludeTree::Module : public IncludeTreeBase<Module> {
505513
llvm::Error print(llvm::raw_ostream &OS, unsigned Indent = 0);
506514

507515
static Expected<Module> create(ObjectStore &DB, StringRef ModuleName,
508-
ModuleFlags Flags,
516+
StringRef ExportAs, ModuleFlags Flags,
509517
ArrayRef<ObjectRef> Submodules,
510518
std::optional<ObjectRef> ExportList,
511519
std::optional<ObjectRef> LinkLibraries);

clang/lib/CAS/IncludeTree.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ static constexpr uint16_t ModuleFlagInferExplicitSubmodules = 1 << 5;
333333
static constexpr uint16_t ModuleFlagInferInferExportWildcard = 1 << 6;
334334
static constexpr uint16_t ModuleFlagHasExports = 1 << 7;
335335
static constexpr uint16_t ModuleFlagHasLinkLibraries = 1 << 8;
336+
static constexpr uint16_t ModuleFlagUseExportAsModuleLinkName = 1 << 9;
336337

337338
IncludeTree::Module::ModuleFlags IncludeTree::Module::getFlags() const {
338339
uint16_t Raw = rawFlags();
@@ -344,6 +345,7 @@ IncludeTree::Module::ModuleFlags IncludeTree::Module::getFlags() const {
344345
Flags.InferSubmodules = Raw & ModuleFlagInferSubmodules;
345346
Flags.InferExplicitSubmodules = Raw & ModuleFlagInferExplicitSubmodules;
346347
Flags.InferExportWildcard = Raw & ModuleFlagInferInferExportWildcard;
348+
Flags.UseExportAsModuleLinkName = Raw & ModuleFlagUseExportAsModuleLinkName;
347349
return Flags;
348350
}
349351

@@ -372,12 +374,14 @@ llvm::Error IncludeTree::Module::forEachSubmodule(
372374

373375
Expected<IncludeTree::Module>
374376
IncludeTree::Module::create(ObjectStore &DB, StringRef ModuleName,
375-
ModuleFlags Flags, ArrayRef<ObjectRef> Submodules,
377+
StringRef ExportAs, ModuleFlags Flags,
378+
ArrayRef<ObjectRef> Submodules,
376379
std::optional<ObjectRef> ExportList,
377380
std::optional<ObjectRef> LinkLibraries) {
378381
// Data:
379382
// - 2 bytes for Flags
380-
// - ModuleName (String)
383+
// - ModuleName (String, null-terminated)
384+
// - (optional) ExportAsModule
381385
// Refs:
382386
// - Submodules (IncludeTreeModule)
383387
// - (optional) ExportList
@@ -402,13 +406,17 @@ IncludeTree::Module::create(ObjectStore &DB, StringRef ModuleName,
402406
RawFlags |= ModuleFlagHasExports;
403407
if (LinkLibraries)
404408
RawFlags |= ModuleFlagHasLinkLibraries;
409+
if (Flags.UseExportAsModuleLinkName)
410+
RawFlags |= ModuleFlagUseExportAsModuleLinkName;
405411

406412
SmallString<64> Buffer;
407413
llvm::raw_svector_ostream BufOS(Buffer);
408414
llvm::support::endian::Writer Writer(BufOS, llvm::support::little);
409415
Writer.write(RawFlags);
410416

411417
Buffer.append(ModuleName);
418+
Buffer.append(StringRef("\0", 1));
419+
Buffer.append(ExportAs);
412420

413421
SmallVector<ObjectRef> Refs(Submodules);
414422
if (ExportList)
@@ -727,6 +735,9 @@ llvm::Error IncludeTree::Module::print(llvm::raw_ostream &OS, unsigned Indent) {
727735
if (Flags.IsSystem)
728736
OS << " (system)";
729737
OS << '\n';
738+
auto ExportAs = getExportAsModule();
739+
if (!ExportAs.empty())
740+
OS << " export_as " << ExportAs << "\n";
730741
if (Flags.InferSubmodules) {
731742
if (Flags.InferExplicitSubmodules)
732743
OS << " explicit module *";

clang/lib/Frontend/FrontendAction.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,8 @@ static Expected<Module *> makeIncludeTreeModule(CompilerInstance &CI,
574574
M->InferSubmodules = Flags.InferSubmodules;
575575
M->InferExplicitSubmodules = Flags.InferExplicitSubmodules;
576576
M->InferExportWildcard = Flags.InferExportWildcard;
577+
M->UseExportAsModuleLinkName = Flags.UseExportAsModuleLinkName;
578+
M->ExportAsModule = Mod.getExportAsModule();
577579

578580
auto ExportList = Mod.getExports();
579581
if (!ExportList)

clang/lib/Tooling/DependencyScanning/IncludeTreeActionController.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,7 @@ getIncludeTreeModule(cas::ObjectStore &DB, Module *M) {
541541
Flags.InferSubmodules = M->InferSubmodules;
542542
Flags.InferExplicitSubmodules = M->InferExplicitSubmodules;
543543
Flags.InferExportWildcard = M->InferExportWildcard;
544+
Flags.UseExportAsModuleLinkName = M->UseExportAsModuleLinkName;
544545

545546
bool GlobalWildcardExport = false;
546547
SmallVector<ITModule::ExportList::Export> Exports;
@@ -574,8 +575,8 @@ getIncludeTreeModule(cas::ObjectStore &DB, Module *M) {
574575
LinkLibraries = LL->getRef();
575576
}
576577

577-
return ITModule::create(DB, M->Name, Flags, Submodules, ExportList,
578-
LinkLibraries);
578+
return ITModule::create(DB, M->Name, M->ExportAsModule, Flags, Submodules,
579+
ExportList, LinkLibraries);
579580
}
580581

581582
Expected<cas::IncludeTreeRoot>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// REQUIRES: ondisk_cas
2+
3+
// RUN: rm -rf %t
4+
// RUN: split-file %s %t
5+
// RUN: sed "s|DIR|%/t|g" %t/cdb.json.template > %t/cdb.json
6+
7+
// RUN: clang-scan-deps -compilation-database %t/cdb.json \
8+
// RUN: -cas-path %t/cas -module-files-dir %t/outputs \
9+
// RUN: -format experimental-include-tree-full -mode preprocess-dependency-directives \
10+
// RUN: > %t/deps.json
11+
12+
// Extract the include-tree commands
13+
// RUN: %deps-to-rsp %t/deps.json --module-name Top > %t/Top.rsp
14+
// RUN: %deps-to-rsp %t/deps.json --module-name Left > %t/Left.rsp
15+
// RUN: %deps-to-rsp %t/deps.json --tu-index 0 > %t/tu.rsp
16+
17+
// Extract include-tree casids
18+
// RUN: cat %t/Top.rsp | sed -E 's|.*"-fcas-include-tree" "(llvmcas://[[:xdigit:]]+)".*|\1|' > %t/Top.casid
19+
// RUN: cat %t/Left.rsp | sed -E 's|.*"-fcas-include-tree" "(llvmcas://[[:xdigit:]]+)".*|\1|' > %t/Left.casid
20+
// RUN: clang-cas-test -cas %t/cas -print-include-tree @%t/Top.casid | FileCheck %s -check-prefix=TOP
21+
// RUN: clang-cas-test -cas %t/cas -print-include-tree @%t/Left.casid | FileCheck %s -check-prefix=LEFT
22+
23+
// TOP: Module Map:
24+
// TOP-NEXT: Top
25+
// TOP-NEXT: export_as Left
26+
// LEFT: Module Map:
27+
// LEFT-NEXT Left
28+
// LEFT-NOT: export_as
29+
30+
//--- cdb.json.template
31+
[{
32+
"file": "DIR/tu.m",
33+
"directory": "DIR",
34+
"command": "clang -fsyntax-only DIR/tu.m -I DIR -fmodules -fimplicit-modules -fimplicit-module-maps -fmodules-cache-path=DIR/module-cache -Rcompile-job-cache -fapinotes-modules -iapinotes-modules DIR"
35+
}]
36+
37+
//--- module.modulemap
38+
module Top {
39+
header "Top.h"
40+
export_as Left
41+
export *
42+
}
43+
module Left {
44+
header "Left.h"
45+
export *
46+
}
47+
48+
//--- Top.h
49+
#pragma once
50+
struct Top {
51+
int x;
52+
};
53+
void top(void);
54+
55+
//--- Left.h
56+
#include "Top.h"
57+
void left(void);
58+
59+
//--- tu.m
60+
#import "Left.h"
61+
62+
void tu(void) {
63+
top(); // expected-error {{'top' is unavailable: don't use this}}
64+
left();
65+
}
66+
// [email protected]:5{{'top' has been explicitly marked unavailable here}}
67+

0 commit comments

Comments
 (0)