Skip to content

Commit 7f29cb0

Browse files
committed
Changing DependencyScanningTool::getModuleDependencies to take a list of module names instead of one module name.
1 parent 7c58089 commit 7f29cb0

File tree

7 files changed

+56
-38
lines changed

7 files changed

+56
-38
lines changed

clang/include/clang/Frontend/FrontendActions.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,12 +320,12 @@ class PrintPreprocessedAction : public PreprocessorFrontendAction {
320320
};
321321

322322
class GetDependenciesByModuleNameAction : public PreprocessOnlyAction {
323-
StringRef ModuleName;
323+
ArrayRef<StringRef> ModuleNames;
324324
void ExecuteAction() override;
325325

326326
public:
327-
GetDependenciesByModuleNameAction(StringRef ModuleName)
328-
: ModuleName(ModuleName) {}
327+
GetDependenciesByModuleNameAction(ArrayRef<StringRef> ModuleNames)
328+
: ModuleNames(ModuleNames) {}
329329
};
330330

331331
} // end namespace clang

clang/include/clang/Tooling/DependencyScanning/DependencyScanningTool.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,14 @@ class DependencyScanningTool {
144144
std::optional<llvm::MemoryBufferRef> TUBuffer = std::nullopt);
145145

146146
/// Given a compilation context specified via the Clang driver command-line,
147-
/// gather modular dependencies of module with the given name, and return the
148-
/// information needed for explicit build.
149-
llvm::Expected<ModuleDepsGraph> getModuleDependencies(
150-
StringRef ModuleName, const std::vector<std::string> &CommandLine,
151-
StringRef CWD, const llvm::DenseSet<ModuleID> &AlreadySeen,
152-
LookupModuleOutputCallback LookupModuleOutput);
147+
/// gather modular dependencies of modules specified by the the given list of
148+
/// names, and return the information needed for explicit build.
149+
llvm::Expected<ModuleDepsGraph>
150+
getModuleDependencies(ArrayRef<StringRef> ModuleNames,
151+
const std::vector<std::string> &CommandLine,
152+
StringRef CWD,
153+
const llvm::DenseSet<ModuleID> &AlreadySeen,
154+
LookupModuleOutputCallback LookupModuleOutput);
153155

154156
llvm::vfs::FileSystem &getWorkerVFS() const { return Worker.getVFS(); }
155157

clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class DependencyScanningWorker {
111111
DependencyConsumer &DepConsumer,
112112
DependencyActionController &Controller,
113113
DiagnosticConsumer &DiagConsumer,
114-
StringRef ModuleName);
114+
ArrayRef<StringRef> ModuleNames);
115115

116116
/// Run the dependency scanning tool for a given clang driver command-line
117117
/// for a specific translation unit via file system or memory buffer.
@@ -132,7 +132,7 @@ class DependencyScanningWorker {
132132
const std::vector<std::string> &CommandLine,
133133
DependencyConsumer &Consumer,
134134
DependencyActionController &Controller,
135-
StringRef ModuleName);
135+
ArrayRef<StringRef> ModuleNames);
136136

137137
llvm::vfs::FileSystem &getVFS() const { return *BaseFS; }
138138

@@ -156,7 +156,7 @@ class DependencyScanningWorker {
156156
DependencyActionController &Controller,
157157
DiagnosticConsumer &DC,
158158
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
159-
std::optional<StringRef> ModuleName);
159+
std::optional<ArrayRef<StringRef>> ModuleNames);
160160
};
161161

162162
} // end namespace dependencies

clang/lib/Frontend/FrontendActions.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,11 +1213,17 @@ void GetDependenciesByModuleNameAction::ExecuteAction() {
12131213
Preprocessor &PP = CI.getPreprocessor();
12141214
SourceManager &SM = PP.getSourceManager();
12151215
FileID MainFileID = SM.getMainFileID();
1216-
SourceLocation FileStart = SM.getLocForStartOfFile(MainFileID);
1217-
SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
1218-
IdentifierInfo *ModuleID = PP.getIdentifierInfo(ModuleName);
1219-
Path.push_back(std::make_pair(ModuleID, FileStart));
1220-
auto ModResult = CI.loadModule(FileStart, Path, Module::Hidden, false);
1221-
PPCallbacks *CB = PP.getPPCallbacks();
1222-
CB->moduleImport(SourceLocation(), Path, ModResult);
1216+
SourceLocation SLoc = SM.getLocForStartOfFile(MainFileID);
1217+
for (auto ModuleName : ModuleNames) {
1218+
SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
1219+
IdentifierInfo *ModuleID = PP.getIdentifierInfo(ModuleName);
1220+
Path.push_back(std::make_pair(ModuleID, SLoc));
1221+
auto ModResult = CI.loadModule(SLoc, Path, Module::Hidden, false);
1222+
PPCallbacks *CB = PP.getPPCallbacks();
1223+
CB->moduleImport(SourceLocation(), Path, ModResult);
1224+
// FIXME: how do you know that this offset is correct?
1225+
SLoc = SLoc.getLocWithOffset(1);
1226+
assert(SLoc <= SM.getLocForEndOfFile(MainFileID) &&
1227+
"Import location extends past file");
1228+
}
12231229
}

clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,16 @@ DependencyScanningTool::getTranslationUnitDependencies(
155155
}
156156

157157
llvm::Expected<ModuleDepsGraph> DependencyScanningTool::getModuleDependencies(
158-
StringRef ModuleName, const std::vector<std::string> &CommandLine,
159-
StringRef CWD, const llvm::DenseSet<ModuleID> &AlreadySeen,
158+
ArrayRef<StringRef> ModuleNames,
159+
const std::vector<std::string> &CommandLine, StringRef CWD,
160+
const llvm::DenseSet<ModuleID> &AlreadySeen,
160161
LookupModuleOutputCallback LookupModuleOutput) {
161162
FullDependencyConsumer Consumer(AlreadySeen);
162163
CallbackActionController Controller(LookupModuleOutput);
164+
165+
assert(ModuleNames.size() && "GettingModuleDependencies for an empty list!");
163166
llvm::Error Result = Worker.computeDependencies(CWD, CommandLine, Consumer,
164-
Controller, ModuleName);
167+
Controller, ModuleNames);
165168
if (Result)
166169
return std::move(Result);
167170
return Consumer.takeModuleGraphDeps();

clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,11 @@ class DependencyScanningAction : public tooling::ToolAction {
287287
DependencyScanningService &Service, StringRef WorkingDirectory,
288288
DependencyConsumer &Consumer, DependencyActionController &Controller,
289289
llvm::IntrusiveRefCntPtr<DependencyScanningWorkerFilesystem> DepFS,
290-
bool DisableFree, std::optional<StringRef> ModuleName = std::nullopt)
290+
bool DisableFree,
291+
std::optional<ArrayRef<StringRef>> ModuleNames = std::nullopt)
291292
: Service(Service), WorkingDirectory(WorkingDirectory),
292293
Consumer(Consumer), Controller(Controller), DepFS(std::move(DepFS)),
293-
DisableFree(DisableFree), ModuleName(ModuleName) {}
294+
DisableFree(DisableFree), ModuleNames(ModuleNames) {}
294295

295296
bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation,
296297
FileManager *DriverFileMgr,
@@ -431,8 +432,9 @@ class DependencyScanningAction : public tooling::ToolAction {
431432

432433
if (Service.getFormat() == ScanningOutputFormat::P1689)
433434
Action = std::make_unique<PreprocessOnlyAction>();
434-
else if (ModuleName)
435-
Action = std::make_unique<GetDependenciesByModuleNameAction>(*ModuleName);
435+
else if (ModuleNames)
436+
Action =
437+
std::make_unique<GetDependenciesByModuleNameAction>(*ModuleNames);
436438
else
437439
Action = std::make_unique<ReadPCHAndPreprocessAction>();
438440

@@ -478,7 +480,7 @@ class DependencyScanningAction : public tooling::ToolAction {
478480
DependencyActionController &Controller;
479481
llvm::IntrusiveRefCntPtr<DependencyScanningWorkerFilesystem> DepFS;
480482
bool DisableFree;
481-
std::optional<StringRef> ModuleName;
483+
std::optional<ArrayRef<StringRef>> ModuleNames;
482484
std::optional<CompilerInstance> ScanInstanceStorage;
483485
std::shared_ptr<ModuleDepCollector> MDC;
484486
std::vector<std::string> LastCC1Arguments;
@@ -546,7 +548,7 @@ llvm::Error DependencyScanningWorker::computeDependencies(
546548
llvm::Error DependencyScanningWorker::computeDependencies(
547549
StringRef WorkingDirectory, const std::vector<std::string> &CommandLine,
548550
DependencyConsumer &Consumer, DependencyActionController &Controller,
549-
StringRef ModuleName) {
551+
ArrayRef<StringRef> ModuleNames) {
550552
// Capture the emitted diagnostics and report them to the client
551553
// in the case of a failure.
552554
std::string DiagnosticOutput;
@@ -555,7 +557,7 @@ llvm::Error DependencyScanningWorker::computeDependencies(
555557
TextDiagnosticPrinter DiagPrinter(DiagnosticsOS, DiagOpts.release());
556558

557559
if (computeDependencies(WorkingDirectory, CommandLine, Consumer, Controller,
558-
DiagPrinter, ModuleName))
560+
DiagPrinter, ModuleNames))
559561
return llvm::Error::success();
560562
return llvm::make_error<llvm::StringError>(DiagnosticsOS.str(),
561563
llvm::inconvertibleErrorCode());
@@ -625,7 +627,7 @@ bool DependencyScanningWorker::scanDependencies(
625627
StringRef WorkingDirectory, const std::vector<std::string> &CommandLine,
626628
DependencyConsumer &Consumer, DependencyActionController &Controller,
627629
DiagnosticConsumer &DC, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
628-
std::optional<StringRef> ModuleName) {
630+
std::optional<ArrayRef<StringRef>> ModuleNames) {
629631
auto FileMgr =
630632
llvm::makeIntrusiveRefCnt<FileManager>(FileSystemOptions{}, FS);
631633

@@ -648,7 +650,7 @@ bool DependencyScanningWorker::scanDependencies(
648650
// always true for a driver invocation.
649651
bool DisableFree = true;
650652
DependencyScanningAction Action(Service, WorkingDirectory, Consumer,
651-
Controller, DepFS, DisableFree, ModuleName);
653+
Controller, DepFS, DisableFree, ModuleNames);
652654

653655
bool Success = false;
654656
if (CommandLine[1] == "-cc1") {
@@ -729,13 +731,14 @@ bool DependencyScanningWorker::computeDependencies(
729731
auto &FinalFS = ModifiedFS ? ModifiedFS : BaseFS;
730732

731733
return scanDependencies(WorkingDirectory, FinalCommandLine, Consumer,
732-
Controller, DC, FinalFS, /*ModuleName=*/std::nullopt);
734+
Controller, DC, FinalFS,
735+
/*ModuleNames=*/std::nullopt);
733736
}
734737

735738
bool DependencyScanningWorker::computeDependencies(
736739
StringRef WorkingDirectory, const std::vector<std::string> &CommandLine,
737740
DependencyConsumer &Consumer, DependencyActionController &Controller,
738-
DiagnosticConsumer &DC, StringRef ModuleName) {
741+
DiagnosticConsumer &DC, ArrayRef<StringRef> ModuleNames) {
739742
// Reset what might have been modified in the previous worker invocation.
740743
BaseFS->setCurrentWorkingDirectory(WorkingDirectory);
741744

@@ -748,17 +751,21 @@ bool DependencyScanningWorker::computeDependencies(
748751
InMemoryFS->setCurrentWorkingDirectory(WorkingDirectory);
749752
SmallString<128> FakeInputPath;
750753
// TODO: We should retry the creation if the path already exists.
751-
llvm::sys::fs::createUniquePath(ModuleName + "-%%%%%%%%.input", FakeInputPath,
754+
// FIXME: should we create files for multiple modules? I think so?
755+
llvm::sys::fs::createUniquePath(ModuleNames[0] + "-%%%%%%%%.input",
756+
FakeInputPath,
752757
/*MakeAbsolute=*/false);
753-
InMemoryFS->addFile(FakeInputPath, 0, llvm::MemoryBuffer::getMemBuffer(""));
758+
std::string FakeString(ModuleNames.size(), ' ');
759+
InMemoryFS->addFile(FakeInputPath, 0,
760+
llvm::MemoryBuffer::getMemBuffer(FakeString));
754761
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> InMemoryOverlay = InMemoryFS;
755762

756763
OverlayFS->pushOverlay(InMemoryOverlay);
757764
auto ModifiedCommandLine = CommandLine;
758765
ModifiedCommandLine.emplace_back(FakeInputPath);
759766

760767
return scanDependencies(WorkingDirectory, ModifiedCommandLine, Consumer,
761-
Controller, DC, OverlayFS, ModuleName);
768+
Controller, DC, OverlayFS, ModuleNames);
762769
}
763770

764771
DependencyActionController::~DependencyActionController() {}

clang/tools/clang-scan-deps/ClangScanDeps.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,8 +1025,8 @@ int clang_scan_deps_main(int argc, char **argv, const llvm::ToolContext &) {
10251025
}
10261026
} else if (ModuleName) {
10271027
auto MaybeModuleDepsGraph = WorkerTool.getModuleDependencies(
1028-
*ModuleName, Input->CommandLine, CWD, AlreadySeenModules,
1029-
LookupOutput);
1028+
ArrayRef<StringRef>({*ModuleName}), Input->CommandLine, CWD,
1029+
AlreadySeenModules, LookupOutput);
10301030
if (handleModuleResult(*ModuleName, MaybeModuleDepsGraph, *FD,
10311031
LocalIndex, DependencyOS, Errs))
10321032
HadErrors = true;

0 commit comments

Comments
 (0)