clang-tools 20.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 <memory>
30
31namespace clang {
32namespace clangd {
33
34/// Store all the needed module files information to parse a single
35/// source file. e.g.,
36///
37/// ```
38/// // a.cppm
39/// export module a;
40///
41/// // b.cppm
42/// export module b;
43/// import a;
44///
45/// // c.cppm
46/// export module c;
47/// import b;
48/// ```
49///
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`.
53///
54/// Users should only get PrerequisiteModules from
55/// `ModulesBuilder::buildPrerequisiteModulesFor(...)`.
56///
57/// Users can detect whether the PrerequisiteModules is still up to date by
58/// calling the `canReuse()` member function.
59///
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.
67public:
68 /// Change commands to load the module files recorded in this
69 /// PrerequisiteModules first.
70 virtual void
71 adjustHeaderSearchOptions(HeaderSearchOptions &Options) const = 0;
72
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.
75 virtual bool
76 canReuse(const CompilerInvocation &CI,
77 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>) const = 0;
78
79 virtual ~PrerequisiteModules() = default;
80};
81
82/// This class handles building module files for a given source file.
83///
84/// In the future, we want the class to manage the module files acorss
85/// different versions and different source files.
87public:
90
91 ModulesBuilder(const ModulesBuilder &) = delete;
93
96
97 std::unique_ptr<PrerequisiteModules>
99
100private:
101 class ModulesBuilderImpl;
102 std::unique_ptr<ModulesBuilderImpl> Impl;
103};
104
105} // namespace clangd
106} // namespace clang
107
108#endif
std::unique_ptr< CompilerInvocation > CI
Provides compilation arguments used for parsing C and C++ files.
This class handles building module files for a given source file.
ModulesBuilder & operator=(ModulesBuilder &&)=delete
std::unique_ptr< PrerequisiteModules > buildPrerequisiteModulesFor(PathRef File, const ThreadsafeFS &TFS)
ModulesBuilder & operator=(const ModulesBuilder &)=delete
ModulesBuilder(const ModulesBuilder &)=delete
ModulesBuilder(ModulesBuilder &&)=delete
Store all the needed module files information to parse a single source file.
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.
Definition: ThreadsafeFS.h:26
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:29
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//