1 //===----------------- ModulesBuilder.h --------------------------*- C++-*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Experimental support for C++20 Modules.
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.
15 // TODO: Supporting reusing module files across different versions and
16 // different source files.
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULES_BUILDER_H
21 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULES_BUILDER_H
23 #include "GlobalCompilationDatabase.h"
24 #include "ProjectModules.h"
25 #include "support/Path.h"
26 #include "support/ThreadsafeFS.h"
27 #include "clang/Frontend/CompilerInvocation.h"
28 #include "llvm/ADT/SmallString.h"
34 /// Store all the needed module files information to parse a single
35 /// source file. e.g.,
50 /// For the source file `c.cppm`, an instance of the class will store
51 /// the module files for `a.cppm` and `b.cppm`. But the module file for `c.cppm`
52 /// won't be stored. Since it is not needed to parse `c.cppm`.
54 /// Users should only get PrerequisiteModules from
55 /// `ModulesBuilder::buildPrerequisiteModulesFor(...)`.
57 /// Users can detect whether the PrerequisiteModules is still up to date by
58 /// calling the `canReuse()` member function.
60 /// The users should call `adjustHeaderSearchOptions(...)` to update the
61 /// compilation commands to select the built module files first. Before calling
62 /// `adjustHeaderSearchOptions()`, users should call `canReuse()` first to check
63 /// if all the stored module files are valid. In case they are not valid,
64 /// users should call `ModulesBuilder::buildPrerequisiteModulesFor(...)` again
65 /// to get the new PrerequisiteModules.
66 class PrerequisiteModules
{
68 /// Change commands to load the module files recorded in this
69 /// PrerequisiteModules first.
71 adjustHeaderSearchOptions(HeaderSearchOptions
&Options
) const = 0;
73 /// Whether or not the built module files are up to date.
74 /// Note that this can only be used after building the module files.
76 canReuse(const CompilerInvocation
&CI
,
77 llvm::IntrusiveRefCntPtr
<llvm::vfs::FileSystem
>) const = 0;
79 virtual ~PrerequisiteModules() = default;
82 /// This class handles building module files for a given source file.
84 /// In the future, we want the class to manage the module files acorss
85 /// different versions and different source files.
86 class ModulesBuilder
{
88 ModulesBuilder(const GlobalCompilationDatabase
&CDB
) : CDB(CDB
) {}
90 ModulesBuilder(const ModulesBuilder
&) = delete;
91 ModulesBuilder(ModulesBuilder
&&) = delete;
93 ModulesBuilder
&operator=(const ModulesBuilder
&) = delete;
94 ModulesBuilder
&operator=(ModulesBuilder
&&) = delete;
96 std::unique_ptr
<PrerequisiteModules
>
97 buildPrerequisiteModulesFor(PathRef File
, const ThreadsafeFS
&TFS
) const;
100 const GlobalCompilationDatabase
&CDB
;
103 } // namespace clangd