clang-tools 24.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
35struct FileEvent;
36
37/// Store all the needed module files information to parse a single
38/// source file. e.g.,
39///
40/// ```
41/// // a.cppm
42/// export module a;
43///
44/// // b.cppm
45/// export module b;
46/// import a;
47///
48/// // c.cppm
49/// export module c;
50/// import b;
51/// ```
52///
53/// For the source file `c.cppm`, an instance of the class will store
54/// the module files for `a.cppm` and `b.cppm`. But the module file for `c.cppm`
55/// won't be stored. Since it is not needed to parse `c.cppm`.
56///
57/// Users should only get PrerequisiteModules from
58/// `ModulesBuilder::buildPrerequisiteModulesFor(...)`.
59///
60/// Users can detect whether the PrerequisiteModules is still up to date by
61/// calling the `canReuse()` member function.
62///
63/// The users should call `adjustHeaderSearchOptions(...)` to update the
64/// compilation commands to select the built module files first. Before calling
65/// `adjustHeaderSearchOptions()`, users should call `canReuse()` first to check
66/// if all the stored module files are valid. In case they are not valid,
67/// users should call `ModulesBuilder::buildPrerequisiteModulesFor(...)` again
68/// to get the new PrerequisiteModules.
70public:
71 /// Change commands to load the module files recorded in this
72 /// PrerequisiteModules first.
73 virtual void
74 adjustHeaderSearchOptions(HeaderSearchOptions &Options) const = 0;
75
76 /// Whether or not the built module files are up to date.
77 /// Note that this can only be used after building the module files.
78 virtual bool
79 canReuse(const CompilerInvocation &CI,
80 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>) const = 0;
81
82 /// Returns the set of directly required module names
83 virtual llvm::StringSet<> getRequiredModuleNames() const = 0;
84
85 virtual ~PrerequisiteModules() = default;
86};
87
88/// This class handles building module files for a given source file.
89///
90/// In the future, we want the class to manage the module files acorss
91/// different versions and different source files.
93public:
96
97 ModulesBuilder(const ModulesBuilder &) = delete;
99
102
103 /// Makes an opened module interface available to project module queries.
104 /// Returns whether a new module path was observed.
106
107 /// Updates project module queries after a watched file event. Returns whether
108 /// the event may have changed an observed module.
109 bool onFileEvent(const FileEvent &Event);
110
111 std::unique_ptr<PrerequisiteModules>
113
115
116 /// Returns the list of directly required module names
117 std::vector<std::string> getRequiredModuleNames(PathRef File);
118
119private:
120 class ModulesBuilderImpl;
121 std::unique_ptr<ModulesBuilderImpl> Impl;
122};
123
124} // namespace clangd
125} // namespace clang
126
127#endif
An Event<T> allows events of type T to be broadcast to listeners.
Definition Function.h:31
Provides compilation arguments used for parsing C and C++ files.
ModulesBuilder & operator=(ModulesBuilder &&)=delete
bool onFileEvent(const FileEvent &Event)
Updates project module queries after a watched file event.
bool observeSourcePath(PathRef File)
Makes an opened module interface available to project module queries.
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++ -*-===//