clang-tools 23.0.0git
ModulesBuilder.h
Go to the documentation of this file.
1//===----------------- ModulesBuilder.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// Experimental support for C++20 Modules.
10//
11// Currently we simplify the implementations by preventing reusing module files
12// across different versions and different source files. But this is clearly a
13// waste of time and space in the end of the day.
14//
15// TODO: Supporting reusing module files across different versions and
16// different source files.
17//
18//===----------------------------------------------------------------------===//
19
20#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULES_BUILDER_H
21#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULES_BUILDER_H
22
24#include "ProjectModules.h"
25#include "support/Path.h"
27#include "clang/Frontend/CompilerInvocation.h"
28#include "llvm/ADT/SmallString.h"
29#include "llvm/ADT/StringSet.h"
30#include <memory>
31
32namespace clang {
33namespace clangd {
34
35/// Store all the needed module files information to parse a single
36/// source file. e.g.,
37///
38/// ```
39/// // a.cppm
40/// export module a;
41///
42/// // b.cppm
43/// export module b;
44/// import a;
45///
46/// // c.cppm
47/// export module c;
48/// import b;
49/// ```
50///
51/// For the source file `c.cppm`, an instance of the class will store
52/// the module files for `a.cppm` and `b.cppm`. But the module file for `c.cppm`
53/// won't be stored. Since it is not needed to parse `c.cppm`.
54///
55/// Users should only get PrerequisiteModules from
56/// `ModulesBuilder::buildPrerequisiteModulesFor(...)`.
57///
58/// Users can detect whether the PrerequisiteModules is still up to date by
59/// calling the `canReuse()` member function.
60///
61/// The users should call `adjustHeaderSearchOptions(...)` to update the
62/// compilation commands to select the built module files first. Before calling
63/// `adjustHeaderSearchOptions()`, users should call `canReuse()` first to check
64/// if all the stored module files are valid. In case they are not valid,
65/// users should call `ModulesBuilder::buildPrerequisiteModulesFor(...)` again
66/// to get the new PrerequisiteModules.
68public:
69 /// Change commands to load the module files recorded in this
70 /// PrerequisiteModules first.
71 virtual void
72 adjustHeaderSearchOptions(HeaderSearchOptions &Options) const = 0;
73
74 /// Whether or not the built module files are up to date.
75 /// Note that this can only be used after building the module files.
76 virtual bool
77 canReuse(const CompilerInvocation &CI,
78 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>) const = 0;
79
80 /// Returns the set of directly required module names
81 virtual llvm::StringSet<> getRequiredModuleNames() const = 0;
82
83 virtual ~PrerequisiteModules() = default;
84};
85
86/// This class handles building module files for a given source file.
87///
88/// In the future, we want the class to manage the module files acorss
89/// different versions and different source files.
91public:
94
95 ModulesBuilder(const ModulesBuilder &) = delete;
97
100
101 std::unique_ptr<PrerequisiteModules>
103
105
106 /// Returns the list of directly required module names
107 std::vector<std::string> getRequiredModuleNames(PathRef File);
108
109private:
110 class ModulesBuilderImpl;
111 std::unique_ptr<ModulesBuilderImpl> Impl;
112};
113
114} // namespace clangd
115} // namespace clang
116
117#endif
Provides compilation arguments used for parsing C and C++ files.
ModulesBuilder & operator=(ModulesBuilder &&)=delete
bool hasRequiredModules(PathRef File)
std::unique_ptr< PrerequisiteModules > buildPrerequisiteModulesFor(PathRef File, const ThreadsafeFS &TFS)
ModulesBuilder & operator=(const ModulesBuilder &)=delete
std::vector< std::string > getRequiredModuleNames(PathRef File)
Returns the list of directly required module names.
ModulesBuilder(const ModulesBuilder &)=delete
ModulesBuilder(const GlobalCompilationDatabase &CDB)
ModulesBuilder(ModulesBuilder &&)=delete
Store all the needed module files information to parse a single source file.
virtual llvm::StringSet getRequiredModuleNames() const =0
Returns the set of directly required module names.
virtual bool canReuse(const CompilerInvocation &CI, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem >) const =0
Whether or not the built module files are up to date.
virtual void adjustHeaderSearchOptions(HeaderSearchOptions &Options) const =0
Change commands to load the module files recorded in this PrerequisiteModules first.
virtual ~PrerequisiteModules()=default
Wrapper for vfs::FileSystem for use in multithreaded programs like clangd.
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
Definition AST.cpp:44
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition Path.h:29
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//