1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
//===------------------ ProjectModules.h -------------------------*- C++-*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "ProjectModules.h"
#include "support/Logger.h"
#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
namespace clang::clangd {
namespace {
/// A scanner to query the dependency information for C++20 Modules.
///
/// The scanner can scan a single file with `scan(PathRef)` member function
/// or scan the whole project with `globalScan(vector<PathRef>)` member
/// function. See the comments of `globalScan` to see the details.
///
/// The ModuleDependencyScanner can get the directly required module names for a
/// specific source file. Also the ModuleDependencyScanner can get the source
/// file declaring the primary module interface for a specific module name.
///
/// IMPORTANT NOTE: we assume that every module unit is only declared once in a
/// source file in the project. But the assumption is not strictly true even
/// besides the invalid projects. The language specification requires that every
/// module unit should be unique in a valid program. But a project can contain
/// multiple programs. Then it is valid that we can have multiple source files
/// declaring the same module in a project as long as these source files don't
/// interfere with each other.
class ModuleDependencyScanner {
public:
ModuleDependencyScanner(
std::shared_ptr<const clang::tooling::CompilationDatabase> CDB,
const ThreadsafeFS &TFS)
: CDB(CDB), TFS(TFS),
Service(tooling::dependencies::ScanningMode::CanonicalPreprocessing,
tooling::dependencies::ScanningOutputFormat::P1689) {}
/// The scanned modules dependency information for a specific source file.
struct ModuleDependencyInfo {
/// The name of the module if the file is a module unit.
std::optional<std::string> ModuleName;
/// A list of names for the modules that the file directly depends.
std::vector<std::string> RequiredModules;
};
/// Scanning the single file specified by \param FilePath.
std::optional<ModuleDependencyInfo> scan(PathRef FilePath);
/// Scanning every source file in the current project to get the
/// <module-name> to <module-unit-source> map.
/// TODO: We should find an efficient method to get the <module-name>
/// to <module-unit-source> map. We can make it either by providing
/// a global module dependency scanner to monitor every file. Or we
/// can simply require the build systems (or even the end users)
/// to provide the map.
void globalScan();
/// Get the source file from the module name. Note that the language
/// guarantees all the module names are unique in a valid program.
/// This function should only be called after globalScan.
///
/// TODO: We should handle the case that there are multiple source files
/// declaring the same module.
PathRef getSourceForModuleName(llvm::StringRef ModuleName) const;
/// Return the direct required modules. Indirect required modules are not
/// included.
std::vector<std::string> getRequiredModules(PathRef File);
private:
std::shared_ptr<const clang::tooling::CompilationDatabase> CDB;
const ThreadsafeFS &TFS;
// Whether the scanner has scanned the project globally.
bool GlobalScanned = false;
clang::tooling::dependencies::DependencyScanningService Service;
// TODO: Add a scanning cache.
// Map module name to source file path.
llvm::StringMap<std::string> ModuleNameToSource;
};
std::optional<ModuleDependencyScanner::ModuleDependencyInfo>
ModuleDependencyScanner::scan(PathRef FilePath) {
auto Candidates = CDB->getCompileCommands(FilePath);
if (Candidates.empty())
return std::nullopt;
// Choose the first candidates as the compile commands as the file.
// Following the same logic with
// DirectoryBasedGlobalCompilationDatabase::getCompileCommand.
tooling::CompileCommand Cmd = std::move(Candidates.front());
static int StaticForMainAddr; // Just an address in this process.
Cmd.CommandLine.push_back("-resource-dir=" +
CompilerInvocation::GetResourcesPath(
"clangd", (void *)&StaticForMainAddr));
using namespace clang::tooling::dependencies;
llvm::SmallString<128> FilePathDir(FilePath);
llvm::sys::path::remove_filename(FilePathDir);
DependencyScanningTool ScanningTool(Service, TFS.view(FilePathDir));
llvm::Expected<P1689Rule> ScanningResult =
ScanningTool.getP1689ModuleDependencyFile(Cmd, Cmd.Directory);
if (auto E = ScanningResult.takeError()) {
elog("Scanning modules dependencies for {0} failed: {1}", FilePath,
llvm::toString(std::move(E)));
return std::nullopt;
}
ModuleDependencyInfo Result;
if (ScanningResult->Provides) {
ModuleNameToSource[ScanningResult->Provides->ModuleName] = FilePath;
Result.ModuleName = ScanningResult->Provides->ModuleName;
}
for (auto &Required : ScanningResult->Requires)
Result.RequiredModules.push_back(Required.ModuleName);
return Result;
}
void ModuleDependencyScanner::globalScan() {
for (auto &File : CDB->getAllFiles())
scan(File);
GlobalScanned = true;
}
PathRef ModuleDependencyScanner::getSourceForModuleName(
llvm::StringRef ModuleName) const {
assert(
GlobalScanned &&
"We should only call getSourceForModuleName after calling globalScan()");
if (auto It = ModuleNameToSource.find(ModuleName);
It != ModuleNameToSource.end())
return It->second;
return {};
}
std::vector<std::string>
ModuleDependencyScanner::getRequiredModules(PathRef File) {
auto ScanningResult = scan(File);
if (!ScanningResult)
return {};
return ScanningResult->RequiredModules;
}
} // namespace
/// TODO: The existing `ScanningAllProjectModules` is not efficient. See the
/// comments in ModuleDependencyScanner for detail.
///
/// In the future, we wish the build system can provide a well design
/// compilation database for modules then we can query that new compilation
/// database directly. Or we need to have a global long-live scanner to detect
/// the state of each file.
class ScanningAllProjectModules : public ProjectModules {
public:
ScanningAllProjectModules(
std::shared_ptr<const clang::tooling::CompilationDatabase> CDB,
const ThreadsafeFS &TFS)
: Scanner(CDB, TFS) {}
~ScanningAllProjectModules() override = default;
std::vector<std::string> getRequiredModules(PathRef File) override {
return Scanner.getRequiredModules(File);
}
/// RequiredSourceFile is not used intentionally. See the comments of
/// ModuleDependencyScanner for detail.
PathRef
getSourceForModuleName(llvm::StringRef ModuleName,
PathRef RequiredSourceFile = PathRef()) override {
Scanner.globalScan();
return Scanner.getSourceForModuleName(ModuleName);
}
private:
ModuleDependencyScanner Scanner;
};
std::unique_ptr<ProjectModules> scanningProjectModules(
std::shared_ptr<const clang::tooling::CompilationDatabase> CDB,
const ThreadsafeFS &TFS) {
return std::make_unique<ScanningAllProjectModules>(CDB, TFS);
}
} // namespace clang::clangd
|