Skip to content

Commit 65063e8

Browse files
committed
[clang][deps] Separate clangDependencyScanning and DependencyScanningTool (NFC)
This patch is the first of two in refactoring Clang's dependency scanning tooling to remove its dependency on clangDriver. It separates Tooling/DependencyScanningTool.cpp from the rest of clangDependencyScanning and moves clangDependencyScanning out of clangTooling into its own library. No functional changes are introduced. The follow-up patch will restrict clangDependencyScanning to handling only -cc1 command line inputs and move functionality related to handling driver commands into clangTooling (DependencyScanningTool.cpp). This is part of a broader effort to support driver-managed builds for compilations using C++ named modules and/or Clang modules. It is required for linking the dependency scanning tooling against the driver without introducing cyclic dependencies, which would otherwise cause build failures when dynamic linking is enabled. The RFC for this change can be found here: https://discourse.llvm.org/t/rfc-new-clangoptions-library-remove-dependency-on-clangdriver-from-clangfrontend-and-flangfrontend/88773?u=naveen-seth
1 parent 4dc29b8 commit 65063e8

27 files changed

+417
-347
lines changed

clang-tools-extra/clangd/ScanningProjectModules.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
#include "ProjectModules.h"
1010
#include "support/Logger.h"
11-
#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
12-
#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
11+
#include "clang/DependencyScanning/DependencyScanningService.h"
12+
#include "clang/Tooling/DependencyScanningTool.h"
1313

1414
namespace clang::clangd {
1515
namespace {
@@ -36,8 +36,8 @@ class ModuleDependencyScanner {
3636
std::shared_ptr<const clang::tooling::CompilationDatabase> CDB,
3737
const ThreadsafeFS &TFS)
3838
: CDB(CDB), TFS(TFS),
39-
Service(tooling::dependencies::ScanningMode::CanonicalPreprocessing,
40-
tooling::dependencies::ScanningOutputFormat::P1689) {}
39+
Service(dependencies::ScanningMode::CanonicalPreprocessing,
40+
dependencies::ScanningOutputFormat::P1689) {}
4141

4242
/// The scanned modules dependency information for a specific source file.
4343
struct ModuleDependencyInfo {
@@ -81,7 +81,7 @@ class ModuleDependencyScanner {
8181
// Whether the scanner has scanned the project globally.
8282
bool GlobalScanned = false;
8383

84-
clang::tooling::dependencies::DependencyScanningService Service;
84+
clang::dependencies::DependencyScanningService Service;
8585

8686
// TODO: Add a scanning cache.
8787

clang/lib/Tooling/DependencyScanning/DependencyScannerImpl.h renamed to clang/include/clang/DependencyScanning/DependencyScannerImpl.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
#ifndef LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_DEPENDENCYSCANNER_H
1010
#define LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_DEPENDENCYSCANNER_H
1111

12+
#include "clang/DependencyScanning/DependencyScanningFilesystem.h"
13+
#include "clang/DependencyScanning/ModuleDepCollector.h"
1214
#include "clang/Driver/Compilation.h"
15+
#include "clang/Driver/Driver.h"
1316
#include "clang/Frontend/CompilerInstance.h"
1417
#include "clang/Frontend/CompilerInvocation.h"
1518
#include "clang/Frontend/TextDiagnosticPrinter.h"
1619
#include "clang/Serialization/ObjectFilePCHContainerReader.h"
17-
#include "clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h"
18-
#include "clang/Tooling/DependencyScanning/ModuleDepCollector.h"
1920

2021
namespace clang {
2122
class DiagnosticConsumer;
2223

23-
namespace tooling {
2424
namespace dependencies {
2525
class DependencyScanningService;
2626
class DependencyScanningWorker;
@@ -206,7 +206,6 @@ class CompilerInstanceWithContext {
206206
llvm::Error handleReturnStatus(bool Success);
207207
};
208208
} // namespace dependencies
209-
} // namespace tooling
210209
} // namespace clang
211210

212211
#endif

clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h renamed to clang/include/clang/DependencyScanning/DependencyScanningFilesystem.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
//===- DependencyScanningFilesystem.h - clang-scan-deps fs ===---*- C++ -*-===//
1+
//===- DependencyScanningFilesystem.h - Optimized Scanning FS ---*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_DEPENDENCYSCANNINGFILESYSTEM_H
10-
#define LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_DEPENDENCYSCANNINGFILESYSTEM_H
9+
#ifndef LLVM_CLANG_DEPENDENCYSCANNING_DEPENDENCYSCANNINGFILESYSTEM_H
10+
#define LLVM_CLANG_DEPENDENCYSCANNING_DEPENDENCYSCANNINGFILESYSTEM_H
1111

1212
#include "clang/Basic/LLVM.h"
1313
#include "clang/Lex/DependencyDirectivesScanner.h"
@@ -21,7 +21,6 @@
2121
#include <variant>
2222

2323
namespace clang {
24-
namespace tooling {
2524
namespace dependencies {
2625

2726
using DependencyDirectivesTy =
@@ -521,7 +520,6 @@ class DependencyScanningWorkerFilesystem
521520
};
522521

523522
} // end namespace dependencies
524-
} // end namespace tooling
525523
} // end namespace clang
526524

527-
#endif // LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_DEPENDENCYSCANNINGFILESYSTEM_H
525+
#endif // LLVM_CLANG_DEPENDENCYSCANNING_DEPENDENCYSCANNINGFILESYSTEM_H

clang/include/clang/Tooling/DependencyScanning/DependencyScanningService.h renamed to clang/include/clang/DependencyScanning/DependencyScanningService.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
//===- DependencyScanningService.h - clang-scan-deps service ===-*- C++ -*-===//
1+
//===- DependencyScanningService.h - Scanning Service -----------*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_DEPENDENCYSCANNINGSERVICE_H
10-
#define LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_DEPENDENCYSCANNINGSERVICE_H
9+
#ifndef LLVM_CLANG_DEPENDENCYSCANNING_DEPENDENCYSCANNINGSERVICE_H
10+
#define LLVM_CLANG_DEPENDENCYSCANNING_DEPENDENCYSCANNINGSERVICE_H
1111

12-
#include "clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h"
13-
#include "clang/Tooling/DependencyScanning/InProcessModuleCache.h"
12+
#include "clang/DependencyScanning/DependencyScanningFilesystem.h"
13+
#include "clang/DependencyScanning/InProcessModuleCache.h"
1414
#include "llvm/ADT/BitmaskEnum.h"
1515
#include "llvm/Support/Chrono.h"
1616

1717
namespace clang {
18-
namespace tooling {
1918
namespace dependencies {
2019

2120
/// The mode in which the dependency scanner will operate to find the
@@ -125,7 +124,6 @@ class DependencyScanningService {
125124
};
126125

127126
} // end namespace dependencies
128-
} // end namespace tooling
129127
} // end namespace clang
130128

131-
#endif // LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_DEPENDENCYSCANNINGSERVICE_H
129+
#endif // LLVM_CLANG_DEPENDENCYSCANNING_DEPENDENCYSCANNINGSERVICE_H
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
//===- DependencyScanningUtils.h - Common Scanning Utilities ----*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_CLANG_DEPENDENCYSCANNING_DEPENDENCYSCANNINGUTILS_H
10+
#define LLVM_CLANG_DEPENDENCYSCANNING_DEPENDENCYSCANNINGUTILS_H
11+
12+
#include "clang/DependencyScanning/DependencyScannerImpl.h"
13+
#include "clang/DependencyScanning/DependencyScanningWorker.h"
14+
#include "clang/DependencyScanning/ModuleDepCollector.h"
15+
16+
namespace clang {
17+
namespace dependencies {
18+
19+
/// Graph of modular dependencies.
20+
using ModuleDepsGraph = std::vector<clang::dependencies::ModuleDeps>;
21+
22+
/// The full dependencies and module graph for a specific input.
23+
struct TranslationUnitDeps {
24+
/// The graph of direct and transitive modular dependencies.
25+
ModuleDepsGraph ModuleGraph;
26+
27+
/// The identifier of the C++20 module this translation unit exports.
28+
///
29+
/// If the translation unit is not a module then \c ID.ModuleName is empty.
30+
clang::dependencies::ModuleID ID;
31+
32+
/// A collection of absolute paths to files that this translation unit
33+
/// directly depends on, not including transitive dependencies.
34+
std::vector<std::string> FileDeps;
35+
36+
/// A collection of prebuilt modules this translation unit directly depends
37+
/// on, not including transitive dependencies.
38+
std::vector<clang::dependencies::PrebuiltModuleDep> PrebuiltModuleDeps;
39+
40+
/// A list of modules this translation unit directly depends on, not including
41+
/// transitive dependencies.
42+
///
43+
/// This may include modules with a different context hash when it can be
44+
/// determined that the differences are benign for this compilation.
45+
std::vector<clang::dependencies::ModuleID> ClangModuleDeps;
46+
47+
/// A list of module names that are visible to this translation unit. This
48+
/// includes both direct and transitive module dependencies.
49+
std::vector<std::string> VisibleModules;
50+
51+
/// A list of the C++20 named modules this translation unit depends on.
52+
std::vector<std::string> NamedModuleDeps;
53+
54+
/// The sequence of commands required to build the translation unit. Commands
55+
/// should be executed in order.
56+
///
57+
/// FIXME: If we add support for multi-arch builds in clang-scan-deps, we
58+
/// should make the dependencies between commands explicit to enable parallel
59+
/// builds of each architecture.
60+
std::vector<clang::dependencies::Command> Commands;
61+
62+
/// Deprecated driver command-line. This will be removed in a future version.
63+
std::vector<std::string> DriverCommandLine;
64+
};
65+
66+
class FullDependencyConsumer : public clang::dependencies::DependencyConsumer {
67+
public:
68+
FullDependencyConsumer(
69+
const llvm::DenseSet<clang::dependencies::ModuleID> &AlreadySeen)
70+
: AlreadySeen(AlreadySeen) {}
71+
72+
void handleBuildCommand(clang::dependencies::Command Cmd) override {
73+
Commands.push_back(std::move(Cmd));
74+
}
75+
76+
void handleDependencyOutputOpts(const DependencyOutputOptions &) override {}
77+
78+
void handleFileDependency(StringRef File) override {
79+
Dependencies.push_back(std::string(File));
80+
}
81+
82+
void handlePrebuiltModuleDependency(
83+
clang::dependencies::PrebuiltModuleDep PMD) override {
84+
PrebuiltModuleDeps.emplace_back(std::move(PMD));
85+
}
86+
87+
void handleModuleDependency(clang::dependencies::ModuleDeps MD) override {
88+
ClangModuleDeps[MD.ID] = std::move(MD);
89+
}
90+
91+
void handleDirectModuleDependency(clang::dependencies::ModuleID ID) override {
92+
DirectModuleDeps.push_back(ID);
93+
}
94+
95+
void handleVisibleModule(std::string ModuleName) override {
96+
VisibleModules.push_back(ModuleName);
97+
}
98+
99+
void handleContextHash(std::string Hash) override {
100+
ContextHash = std::move(Hash);
101+
}
102+
103+
void handleProvidedAndRequiredStdCXXModules(
104+
std::optional<clang::dependencies::P1689ModuleInfo> Provided,
105+
std::vector<clang::dependencies::P1689ModuleInfo> Requires) override {
106+
ModuleName = Provided ? Provided->ModuleName : "";
107+
llvm::transform(Requires, std::back_inserter(NamedModuleDeps),
108+
[](const auto &Module) { return Module.ModuleName; });
109+
}
110+
111+
TranslationUnitDeps takeTranslationUnitDeps();
112+
113+
private:
114+
std::vector<std::string> Dependencies;
115+
std::vector<clang::dependencies::PrebuiltModuleDep> PrebuiltModuleDeps;
116+
llvm::MapVector<clang::dependencies::ModuleID,
117+
clang::dependencies::ModuleDeps>
118+
ClangModuleDeps;
119+
std::string ModuleName;
120+
std::vector<std::string> NamedModuleDeps;
121+
std::vector<clang::dependencies::ModuleID> DirectModuleDeps;
122+
std::vector<std::string> VisibleModules;
123+
std::vector<clang::dependencies::Command> Commands;
124+
std::string ContextHash;
125+
const llvm::DenseSet<clang::dependencies::ModuleID> &AlreadySeen;
126+
};
127+
128+
/// A callback to lookup module outputs for "-fmodule-file=", "-o" etc.
129+
using LookupModuleOutputCallback =
130+
llvm::function_ref<std::string(const clang::dependencies::ModuleDeps &,
131+
clang::dependencies::ModuleOutputKind)>;
132+
133+
/// A simple dependency action controller that uses a callback. If no callback
134+
/// is provided, it is assumed that looking up module outputs is unreachable.
135+
class CallbackActionController
136+
: public clang::dependencies::DependencyActionController {
137+
public:
138+
virtual ~CallbackActionController();
139+
140+
static std::string
141+
lookupUnreachableModuleOutput(const clang::dependencies::ModuleDeps &MD,
142+
clang::dependencies::ModuleOutputKind Kind) {
143+
llvm::report_fatal_error("unexpected call to lookupModuleOutput");
144+
};
145+
146+
CallbackActionController(LookupModuleOutputCallback LMO)
147+
: LookupModuleOutput(std::move(LMO)) {
148+
if (!LookupModuleOutput) {
149+
LookupModuleOutput = lookupUnreachableModuleOutput;
150+
}
151+
}
152+
153+
std::string
154+
lookupModuleOutput(const clang::dependencies::ModuleDeps &MD,
155+
clang::dependencies::ModuleOutputKind Kind) override {
156+
return LookupModuleOutput(MD, Kind);
157+
}
158+
159+
private:
160+
LookupModuleOutputCallback LookupModuleOutput;
161+
};
162+
163+
} // end namespace dependencies
164+
} // end namespace clang
165+
166+
#endif // LLVM_CLANG_DEPENDENCYSCANNING_DEPENDENCYSCANNINGUTILS_H

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
//===- DependencyScanningWorker.h - clang-scan-deps worker ===---*- C++ -*-===//
1+
//===- DependencyScanningWorker.h - Thread-Safe Scanning Worker -*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_DEPENDENCYSCANNINGWORKER_H
10-
#define LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_DEPENDENCYSCANNINGWORKER_H
9+
#ifndef LLVM_CLANG_DEPENDENCYSCANNING_DEPENDENCYSCANNINGWORKER_H
10+
#define LLVM_CLANG_DEPENDENCYSCANNING_DEPENDENCYSCANNINGWORKER_H
1111

1212
#include "clang/Basic/DiagnosticOptions.h"
1313
#include "clang/Basic/FileManager.h"
1414
#include "clang/Basic/LLVM.h"
15+
#include "clang/DependencyScanning/DependencyScanningService.h"
16+
#include "clang/DependencyScanning/ModuleDepCollector.h"
1517
#include "clang/Frontend/PCHContainerOperations.h"
16-
#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
17-
#include "clang/Tooling/DependencyScanning/ModuleDepCollector.h"
1818
#include "llvm/Support/Error.h"
1919
#include "llvm/Support/FileSystem.h"
2020
#include "llvm/Support/MemoryBufferRef.h"
@@ -25,7 +25,6 @@ namespace clang {
2525

2626
class DependencyOutputOptions;
2727

28-
namespace tooling {
2928
namespace dependencies {
3029

3130
class DependencyScanningWorkerFilesystem;
@@ -185,7 +184,6 @@ class DependencyScanningWorker {
185184
};
186185

187186
} // end namespace dependencies
188-
} // end namespace tooling
189187
} // end namespace clang
190188

191-
#endif // LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_DEPENDENCYSCANNINGWORKER_H
189+
#endif // LLVM_CLANG_DEPENDENCYSCANNING_DEPENDENCYSCANNINGWORKER_H
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
//===----------------------------------------------------------------------===//
1+
//===- InProcessModuleCache.h - Implicit Module Cache -----------*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_INPROCESSMODULECACHE_H
10-
#define LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_INPROCESSMODULECACHE_H
9+
#ifndef LLVM_CLANG_DEPENDENCYSCANNING_INPROCESSMODULECACHE_H
10+
#define LLVM_CLANG_DEPENDENCYSCANNING_INPROCESSMODULECACHE_H
1111

1212
#include "clang/Serialization/ModuleCache.h"
1313
#include "llvm/ADT/StringMap.h"
@@ -16,8 +16,8 @@
1616
#include <shared_mutex>
1717

1818
namespace clang {
19-
namespace tooling {
2019
namespace dependencies {
20+
2121
struct ModuleCacheEntry {
2222
std::shared_mutex CompilationMutex;
2323
std::atomic<std::time_t> Timestamp = 0;
@@ -30,8 +30,8 @@ struct ModuleCacheEntries {
3030

3131
IntrusiveRefCntPtr<ModuleCache>
3232
makeInProcessModuleCache(ModuleCacheEntries &Entries);
33+
3334
} // namespace dependencies
34-
} // namespace tooling
3535
} // namespace clang
3636

37-
#endif
37+
#endif // LLVM_CLANG_DEPENDENCYSCANNING_INPROCESSMODULECACHE_H

0 commit comments

Comments
 (0)