Skip to content

Commit 33bd308

Browse files
authored
Merge pull request #6194 from apple/jan_svoboda/20221013-cherry-pick
🍒 Dependency scanning patches
2 parents 158bf63 + f312341 commit 33bd308

File tree

15 files changed

+261
-444
lines changed

15 files changed

+261
-444
lines changed

clang/include/clang-c/Dependencies.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,6 @@ typedef struct {
7474

7575
/**
7676
* The canonical command line to build this module.
77-
*
78-
* If getFileDependencies_v3 or later was used to get this dependency, it is
79-
* a complete command line. When using getFileDependencies_v2, it excludes
80-
* arguments containing modules-related paths:
81-
* "-fmodule-file=", "-o", "-fmodule-map-file=".
8277
*/
8378
CXStringSet *BuildArguments;
8479
} CXModuleDependency;
@@ -300,16 +295,6 @@ typedef size_t CXModuleLookupOutputCallback(void *Context,
300295
CXOutputKind OutputKind,
301296
char *Output, size_t MaxLen);
302297

303-
/**
304-
* See \c clang_experimental_DependencyScannerWorker_getFileDependencies_v5.
305-
*/
306-
CINDEX_LINKAGE CXFileDependencies *
307-
clang_experimental_DependencyScannerWorker_getFileDependencies_v3(
308-
CXDependencyScannerWorker Worker, int argc, const char *const *argv,
309-
const char *ModuleName, const char *WorkingDirectory, void *MDCContext,
310-
CXModuleDiscoveredCallback *MDC, void *MLOContext,
311-
CXModuleLookupOutputCallback *MLO, unsigned Options, CXString *error);
312-
313298
/**
314299
* See \c clang_experimental_DependencyScannerWorker_getFileDependencies_v5.
315300
* Returns diagnostics in an unstructured CXString instead of CXDiagnosticSet.

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

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,14 @@ namespace dependencies {
4040
using LookupModuleOutputCallback =
4141
llvm::function_ref<std::string(const ModuleID &, ModuleOutputKind)>;
4242

43+
/// Graph of modular dependencies.
44+
using ModuleDepsGraph = std::vector<ModuleDeps>;
45+
4346
/// The full dependencies and module graph for a specific input.
44-
struct FullDependencies {
47+
struct TranslationUnitDeps {
48+
/// The graph of direct and transitive modular dependencies.
49+
ModuleDepsGraph ModuleGraph;
50+
4551
/// The identifier of the C++20 module this translation unit exports.
4652
///
4753
/// If the translation unit is not a module then \c ID.ModuleName is empty.
@@ -77,11 +83,6 @@ struct FullDependencies {
7783
std::vector<std::string> DriverCommandLine;
7884
};
7985

80-
struct FullDependenciesResult {
81-
FullDependencies FullDeps;
82-
std::vector<ModuleDeps> DiscoveredModules;
83-
};
84-
8586
/// The high-level implementation of the dependency discovery tool that runs on
8687
/// an individual worker thread.
8788
class DependencyScanningTool {
@@ -93,14 +94,12 @@ class DependencyScanningTool {
9394

9495
/// Print out the dependency information into a string using the dependency
9596
/// file format that is specified in the options (-MD is the default) and
96-
/// return it. If \p ModuleName isn't empty, this function returns the
97-
/// dependency information of module \p ModuleName.
97+
/// return it.
9898
///
9999
/// \returns A \c StringError with the diagnostic output if clang errors
100100
/// occurred, dependency file contents otherwise.
101101
llvm::Expected<std::string>
102-
getDependencyFile(const std::vector<std::string> &CommandLine, StringRef CWD,
103-
llvm::Optional<StringRef> ModuleName = None);
102+
getDependencyFile(const std::vector<std::string> &CommandLine, StringRef CWD);
104103

105104
/// Collect dependency tree.
106105
llvm::Expected<llvm::cas::ObjectProxy>
@@ -133,9 +132,8 @@ class DependencyScanningTool {
133132
DiagnosticConsumer &DiagsConsumer, raw_ostream *VerboseOS,
134133
bool DiagGenerationAsCompilation);
135134

136-
/// Collect the full module dependency graph for the input, ignoring any
137-
/// modules which have already been seen. If \p ModuleName isn't empty, this
138-
/// function returns the full dependency information of module \p ModuleName.
135+
/// Given a Clang driver command-line for a translation unit, gather the
136+
/// modular dependencies and return the information needed for explicit build.
139137
///
140138
/// \param AlreadySeen This stores modules which have previously been
141139
/// reported. Use the same instance for all calls to this
@@ -147,13 +145,23 @@ class DependencyScanningTool {
147145
/// arguments for dependencies.
148146
///
149147
/// \returns a \c StringError with the diagnostic output if clang errors
150-
/// occurred, \c FullDependencies otherwise.
151-
llvm::Expected<FullDependenciesResult>
152-
getFullDependencies(const std::vector<std::string> &CommandLine,
153-
StringRef CWD, const llvm::StringSet<> &AlreadySeen,
154-
LookupModuleOutputCallback LookupModuleOutput,
155-
llvm::Optional<StringRef> ModuleName = None,
156-
DepscanPrefixMapping PrefixMapping = {});
148+
/// occurred, \c TranslationUnitDeps otherwise.
149+
llvm::Expected<TranslationUnitDeps>
150+
getTranslationUnitDependencies(const std::vector<std::string> &CommandLine,
151+
StringRef CWD,
152+
const llvm::StringSet<> &AlreadySeen,
153+
LookupModuleOutputCallback LookupModuleOutput,
154+
DepscanPrefixMapping PrefixMapping);
155+
156+
/// Given a compilation context specified via the Clang driver command-line,
157+
/// gather modular dependencies of module with the given name, and return the
158+
/// information needed for explicit build.
159+
llvm::Expected<ModuleDepsGraph>
160+
getModuleDependencies(StringRef ModuleName,
161+
const std::vector<std::string> &CommandLine,
162+
StringRef CWD, const llvm::StringSet<> &AlreadySeen,
163+
LookupModuleOutputCallback LookupModuleOutput,
164+
DepscanPrefixMapping PrefixMapping);
157165

158166
ScanningOutputFormat getScanningFormat() const {
159167
return Worker.getScanningFormat();
@@ -172,12 +180,6 @@ class DependencyScanningTool {
172180
return Worker.getOrCreateFileManager();
173181
}
174182

175-
llvm::Expected<FullDependenciesResult> getFullDependenciesLegacyDriverCommand(
176-
const std::vector<std::string> &CommandLine, StringRef CWD,
177-
const llvm::StringSet<> &AlreadySeen,
178-
LookupModuleOutputCallback LookupModuleOutput,
179-
llvm::Optional<StringRef> ModuleName = None);
180-
181183
private:
182184
DependencyScanningWorker Worker;
183185
};
@@ -186,12 +188,10 @@ class FullDependencyConsumer : public DependencyConsumer {
186188
public:
187189
FullDependencyConsumer(const llvm::StringSet<> &AlreadySeen,
188190
LookupModuleOutputCallback LookupModuleOutput,
189-
bool EagerLoadModules,
190191
CachingOnDiskFileSystemPtr CacheFS = nullptr,
191192
DepscanPrefixMapping PrefixMapping = {})
192193
: CacheFS(std::move(CacheFS)), PrefixMapping(std::move(PrefixMapping)),
193-
AlreadySeen(AlreadySeen), LookupModuleOutput(LookupModuleOutput),
194-
EagerLoadModules(EagerLoadModules) {}
194+
AlreadySeen(AlreadySeen), LookupModuleOutput(LookupModuleOutput) {}
195195

196196
llvm::Error initialize(CompilerInstance &ScanInstance,
197197
CompilerInvocation &NewInvocation) override;
@@ -239,10 +239,8 @@ class FullDependencyConsumer : public DependencyConsumer {
239239
return LookupModuleOutput(ID, Kind);
240240
}
241241

242-
FullDependenciesResult getFullDependenciesLegacyDriverCommand(
243-
const std::vector<std::string> &OriginalCommandLine) const;
244-
245-
FullDependenciesResult takeFullDependencies();
242+
TranslationUnitDeps takeTranslationUnitDeps();
243+
ModuleDepsGraph takeModuleGraphDeps();
246244

247245
private:
248246
std::vector<std::string> Dependencies;
@@ -259,7 +257,6 @@ class FullDependencyConsumer : public DependencyConsumer {
259257
std::vector<std::string> OutputPaths;
260258
const llvm::StringSet<> &AlreadySeen;
261259
LookupModuleOutputCallback LookupModuleOutput;
262-
bool EagerLoadModules;
263260
};
264261

265262
} // end namespace dependencies

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ struct DepscanPrefixMapping;
3535

3636
/// A command-line tool invocation that is part of building a TU.
3737
///
38-
/// \see FullDependencies::Commands.
38+
/// \see TranslationUnitDeps::Commands.
3939
struct Command {
4040
std::string Executable;
4141
std::vector<std::string> Arguments;

clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp

Lines changed: 29 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,13 @@ using namespace tooling;
2222
using namespace dependencies;
2323
using llvm::Error;
2424

25-
static std::vector<std::string>
26-
makeTUCommandLineWithoutPaths(ArrayRef<std::string> OriginalCommandLine) {
27-
std::vector<std::string> Args = OriginalCommandLine;
28-
29-
Args.push_back("-fno-implicit-modules");
30-
Args.push_back("-fno-implicit-module-maps");
31-
32-
// These arguments are unused in explicit compiles.
33-
llvm::erase_if(Args, [](StringRef Arg) {
34-
if (Arg.consume_front("-fmodules-")) {
35-
return Arg.startswith("cache-path=") ||
36-
Arg.startswith("prune-interval=") ||
37-
Arg.startswith("prune-after=") ||
38-
Arg == "validate-once-per-build-session";
39-
}
40-
return Arg.startswith("-fbuild-session-file=");
41-
});
42-
43-
return Args;
44-
}
45-
4625
DependencyScanningTool::DependencyScanningTool(
4726
DependencyScanningService &Service,
4827
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)
4928
: Worker(Service, std::move(FS)) {}
5029

5130
llvm::Expected<std::string> DependencyScanningTool::getDependencyFile(
52-
const std::vector<std::string> &CommandLine, StringRef CWD,
53-
llvm::Optional<StringRef> ModuleName) {
31+
const std::vector<std::string> &CommandLine, StringRef CWD) {
5432
/// Prints out all of the gathered dependencies into a string.
5533
class MakeDependencyPrinterConsumer : public DependencyConsumer {
5634
public:
@@ -111,8 +89,7 @@ llvm::Expected<std::string> DependencyScanningTool::getDependencyFile(
11189
};
11290

11391
MakeDependencyPrinterConsumer Consumer;
114-
auto Result =
115-
Worker.computeDependencies(CWD, CommandLine, Consumer, ModuleName);
92+
auto Result = Worker.computeDependencies(CWD, CommandLine, Consumer);
11693
if (Result)
11794
return std::move(Result);
11895
std::string Output;
@@ -144,8 +121,8 @@ class GetDependencyTree : public FullDependencyConsumer {
144121

145122
GetDependencyTree(llvm::cas::CachingOnDiskFileSystem &FS,
146123
DepscanPrefixMapping PrefixMapping)
147-
: FullDependencyConsumer(AlreadySeen, nullptr, /*EagerModules=*/false,
148-
&FS, std::move(PrefixMapping)),
124+
: FullDependencyConsumer(AlreadySeen, nullptr, &FS,
125+
std::move(PrefixMapping)),
149126
FS(FS) {}
150127

151128
private:
@@ -560,36 +537,32 @@ DependencyScanningTool::getIncludeTreeFromCompilerInvocation(
560537
return Consumer.getIncludeTree();
561538
}
562539

563-
llvm::Expected<FullDependenciesResult>
564-
DependencyScanningTool::getFullDependencies(
540+
llvm::Expected<TranslationUnitDeps>
541+
DependencyScanningTool::getTranslationUnitDependencies(
565542
const std::vector<std::string> &CommandLine, StringRef CWD,
566543
const llvm::StringSet<> &AlreadySeen,
567544
LookupModuleOutputCallback LookupModuleOutput,
568-
llvm::Optional<StringRef> ModuleName, DepscanPrefixMapping PrefixMapping) {
545+
DepscanPrefixMapping PrefixMapping) {
569546
FullDependencyConsumer Consumer(AlreadySeen, LookupModuleOutput,
570-
Worker.shouldEagerLoadModules(),
571547
Worker.getCASFS(), std::move(PrefixMapping));
572-
llvm::Error Result =
573-
Worker.computeDependencies(CWD, CommandLine, Consumer, ModuleName);
548+
llvm::Error Result = Worker.computeDependencies(CWD, CommandLine, Consumer);
574549
if (Result)
575550
return std::move(Result);
576-
return Consumer.takeFullDependencies();
551+
return Consumer.takeTranslationUnitDeps();
577552
}
578553

579-
llvm::Expected<FullDependenciesResult>
580-
DependencyScanningTool::getFullDependenciesLegacyDriverCommand(
581-
const std::vector<std::string> &CommandLine, StringRef CWD,
582-
const llvm::StringSet<> &AlreadySeen,
554+
llvm::Expected<ModuleDepsGraph> DependencyScanningTool::getModuleDependencies(
555+
StringRef ModuleName, const std::vector<std::string> &CommandLine,
556+
StringRef CWD, const llvm::StringSet<> &AlreadySeen,
583557
LookupModuleOutputCallback LookupModuleOutput,
584-
llvm::Optional<StringRef> ModuleName) {
558+
DepscanPrefixMapping PrefixMapping) {
585559
FullDependencyConsumer Consumer(AlreadySeen, LookupModuleOutput,
586-
Worker.shouldEagerLoadModules(),
587-
Worker.getCASFS());
560+
Worker.getCASFS(), std::move(PrefixMapping));
588561
llvm::Error Result =
589562
Worker.computeDependencies(CWD, CommandLine, Consumer, ModuleName);
590563
if (Result)
591564
return std::move(Result);
592-
return Consumer.getFullDependenciesLegacyDriverCommand(CommandLine);
565+
return Consumer.takeModuleGraphDeps();
593566
}
594567

595568
Error FullDependencyConsumer::initialize(CompilerInstance &ScanInstance,
@@ -733,75 +706,40 @@ Error FullDependencyConsumer::finalizeModuleInvocation(CompilerInvocation &CI,
733706
return llvm::Error::success();
734707
}
735708

736-
FullDependenciesResult FullDependencyConsumer::takeFullDependencies() {
737-
FullDependenciesResult FDR;
738-
FullDependencies &FD = FDR.FullDeps;
709+
TranslationUnitDeps FullDependencyConsumer::takeTranslationUnitDeps() {
710+
TranslationUnitDeps TU;
739711

740-
FD.ID.ContextHash = std::move(ContextHash);
741-
FD.FileDeps = std::move(Dependencies);
742-
FD.PrebuiltModuleDeps = std::move(PrebuiltModuleDeps);
743-
FD.Commands = std::move(Commands);
744-
FD.CASFileSystemRootID = CASFileSystemRootID;
712+
TU.ID.ContextHash = std::move(ContextHash);
713+
TU.FileDeps = std::move(Dependencies);
714+
TU.PrebuiltModuleDeps = std::move(PrebuiltModuleDeps);
715+
TU.Commands = std::move(Commands);
716+
TU.CASFileSystemRootID = CASFileSystemRootID;
745717

746718
for (auto &&M : ClangModuleDeps) {
747719
auto &MD = M.second;
748720
if (MD.ImportedByMainFile)
749-
FD.ClangModuleDeps.push_back(MD.ID);
721+
TU.ClangModuleDeps.push_back(MD.ID);
750722
// TODO: Avoid handleModuleDependency even being called for modules
751723
// we've already seen.
752724
if (AlreadySeen.count(M.first))
753725
continue;
754-
FDR.DiscoveredModules.push_back(std::move(MD));
726+
TU.ModuleGraph.push_back(std::move(MD));
755727
}
756728

757-
return FDR;
729+
return TU;
758730
}
759731

760-
FullDependenciesResult
761-
FullDependencyConsumer::getFullDependenciesLegacyDriverCommand(
762-
const std::vector<std::string> &OriginalCommandLine) const {
763-
FullDependencies FD;
764-
765-
FD.DriverCommandLine = makeTUCommandLineWithoutPaths(
766-
ArrayRef<std::string>(OriginalCommandLine).slice(1));
767-
768-
FD.ID.ContextHash = std::move(ContextHash);
769-
770-
FD.FileDeps.assign(Dependencies.begin(), Dependencies.end());
771-
772-
for (const PrebuiltModuleDep &PMD : PrebuiltModuleDeps)
773-
FD.DriverCommandLine.push_back("-fmodule-file=" + PMD.PCMFile);
732+
ModuleDepsGraph FullDependencyConsumer::takeModuleGraphDeps() {
733+
ModuleDepsGraph ModuleGraph;
774734

775735
for (auto &&M : ClangModuleDeps) {
776736
auto &MD = M.second;
777-
if (MD.ImportedByMainFile) {
778-
FD.ClangModuleDeps.push_back(MD.ID);
779-
auto PCMPath = LookupModuleOutput(MD.ID, ModuleOutputKind::ModuleFile);
780-
if (EagerLoadModules) {
781-
FD.DriverCommandLine.push_back("-fmodule-file=" + PCMPath);
782-
} else {
783-
FD.DriverCommandLine.push_back("-fmodule-map-file=" +
784-
MD.ClangModuleMapFile);
785-
FD.DriverCommandLine.push_back("-fmodule-file=" + MD.ID.ModuleName +
786-
"=" + PCMPath);
787-
}
788-
}
789-
}
790-
791-
FD.PrebuiltModuleDeps = std::move(PrebuiltModuleDeps);
792-
793-
FD.CASFileSystemRootID = CASFileSystemRootID;
794-
795-
FullDependenciesResult FDR;
796-
797-
for (auto &&M : ClangModuleDeps) {
798737
// TODO: Avoid handleModuleDependency even being called for modules
799738
// we've already seen.
800739
if (AlreadySeen.count(M.first))
801740
continue;
802-
FDR.DiscoveredModules.push_back(std::move(M.second));
741+
ModuleGraph.push_back(std::move(MD));
803742
}
804743

805-
FDR.FullDeps = std::move(FD);
806-
return FDR;
744+
return ModuleGraph;
807745
}

0 commit comments

Comments
 (0)