|
| 1 | +//===-------------- ModuleDependencyScanner.h --------------------*- 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_TOOLS_EXTRA_CLANGD_MODULEDEPENDENCYSCANNER_H |
| 10 | +#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEDEPENDENCYSCANNER_H |
| 11 | + |
| 12 | +#include "GlobalCompilationDatabase.h" |
| 13 | +#include "support/Path.h" |
| 14 | +#include "support/ThreadsafeFS.h" |
| 15 | + |
| 16 | +#include "clang/Tooling/DependencyScanning/DependencyScanningService.h" |
| 17 | +#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h" |
| 18 | + |
| 19 | +#include "llvm/ADT/SmallString.h" |
| 20 | +#include "llvm/ADT/StringMap.h" |
| 21 | + |
| 22 | +namespace clang { |
| 23 | +namespace clangd { |
| 24 | + |
| 25 | +/// A scanner to query the dependency information for C++20 Modules. |
| 26 | +/// |
| 27 | +/// The scanner can scan a single file with `scan(PathRef)` member function |
| 28 | +/// or scan the whole project with `globalScan(PathRef)` member function. See |
| 29 | +/// the comments of `globalScan` to see the details. |
| 30 | +/// |
| 31 | +/// ModuleDependencyScanner should only be used via ScanningAllProjectModules. |
| 32 | +/// |
| 33 | +/// The ModuleDependencyScanner can get the directly required module name for a |
| 34 | +/// specific source file. Also the ModuleDependencyScanner can get the source |
| 35 | +/// file declaring a specific module name. |
| 36 | +/// |
| 37 | +/// IMPORTANT NOTE: we assume that every module unit is only declared once in a |
| 38 | +/// source file in the project. But the assumption is not strictly true even |
| 39 | +/// besides the invalid projects. The language specification requires that every |
| 40 | +/// module unit should be unique in a valid program. But a project can contain |
| 41 | +/// multiple programs. Then it is valid that we can have multiple source files |
| 42 | +/// declaring the same module in a project as long as these source files don't |
| 43 | +/// interere with each other.` |
| 44 | +class ModuleDependencyScanner { |
| 45 | +public: |
| 46 | + ModuleDependencyScanner(const GlobalCompilationDatabase &CDB, |
| 47 | + const ThreadsafeFS &TFS) |
| 48 | + : CDB(CDB), TFS(TFS), |
| 49 | + Service(tooling::dependencies::ScanningMode::CanonicalPreprocessing, |
| 50 | + tooling::dependencies::ScanningOutputFormat::P1689) {} |
| 51 | + |
| 52 | + // The scanned modules dependency information for a specific source file. |
| 53 | + struct ModuleDependencyInfo { |
| 54 | + // The name of the module if the file is a module unit. |
| 55 | + std::optional<std::string> ModuleName; |
| 56 | + // A list of names for the modules that the file directly depends. |
| 57 | + std::vector<std::string> RequiredModules; |
| 58 | + }; |
| 59 | + |
| 60 | + /// Scanning the single file specified by \param FilePath. |
| 61 | + /// NOTE: This is only used by unittests for external uses. |
| 62 | + std::optional<ModuleDependencyInfo> scan(PathRef FilePath); |
| 63 | + |
| 64 | + /// Scanning every source file in the current project to get the |
| 65 | + /// <module-name> to <module-unit-source> map. |
| 66 | + /// It looks unefficiency to scan the whole project especially for |
| 67 | + /// every version of every file! |
| 68 | + /// TODO: We should find an efficient method to get the <module-name> |
| 69 | + /// to <module-unit-source> map. We can make it either by providing |
| 70 | + /// a global module dependency scanner to monitor every file. Or we |
| 71 | + /// can simply require the build systems (or even if the end users) |
| 72 | + /// to provide the map. |
| 73 | + void globalScan(const std::vector<std::string> &AllFiles); |
| 74 | + bool isGlobalScanned() const { return GlobalScanned; } |
| 75 | + |
| 76 | + /// Get the source file from the module name. Note that the language |
| 77 | + /// guarantees all the module names are unique in a valid program. |
| 78 | + /// This function should only be called after globalScan. |
| 79 | + /// |
| 80 | + /// FIXME: We should handle the case that there are multiple source files |
| 81 | + /// declaring the same module. |
| 82 | + PathRef getSourceForModuleName(StringRef ModuleName) const; |
| 83 | + |
| 84 | + /// Return the direct required modules. Indirect required modules are not |
| 85 | + /// included. |
| 86 | + std::vector<std::string> getRequiredModules(PathRef File); |
| 87 | + |
| 88 | +private: |
| 89 | + const GlobalCompilationDatabase &CDB; |
| 90 | + const ThreadsafeFS &TFS; |
| 91 | + |
| 92 | + // Whether the scanner has scanned the project globally. |
| 93 | + bool GlobalScanned = false; |
| 94 | + |
| 95 | + clang::tooling::dependencies::DependencyScanningService Service; |
| 96 | + |
| 97 | + // TODO: Add a scanning cache. |
| 98 | + |
| 99 | + // Map module name to source file path. |
| 100 | + llvm::StringMap<std::string> ModuleNameToSource; |
| 101 | +}; |
| 102 | + |
| 103 | +} // namespace clangd |
| 104 | +} // namespace clang |
| 105 | + |
| 106 | +#endif |
0 commit comments