[clang-tools-extra] [clangd] [C++20] [Modules] Introduce initial support for C++20 Modules (PR #66462)

Chuanqi Xu via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 9 02:56:10 PDT 2023


================
@@ -0,0 +1,118 @@
+//===----------------- ModuleFilesInfo.h -------------------------*- C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Experimental support for C++20 Modules.
+//
+// Currently we simplify the implementations by preventing reusing module files
+// across different versions and different source files. But this is clearly a
+// waste of time and space in the end of the day.
+//
+// FIXME: Supporting reusing module files across different versions and different
+// source files.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEFILESINFO_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEFILESINFO_H
+
+#include "Compiler.h"
+#include "GlobalCompilationDatabase.h"
+#include "ModuleDependencyScanner.h"
+#include "support/Path.h"
+
+#include "clang/Lex/HeaderSearchOptions.h"
+
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringMap.h"
+
+namespace clang {
+namespace clangd {
+
+/// Store module files information for a single file.
+///
+/// A ModuleFilesInfo should only be initialized by
+/// `ModuleFilesInfo::buildModuleFilesInfoFor(PathRef, const ThreadsafeFS *, const GlobalCompilationDatabase &)`
+/// static member method. This method will block the current thread and build all the needed
+/// module files. All the built module files won't be shared with other source files.
+///
+/// Users can detect whether the ModuleFilesInfo is still up to date by calling
+/// the `CanReuse()` member function.
+///
+/// The users should call `ReplaceHeaderSearchOptions(...)` or `ReplaceCompileCommands(CompileCommand&)`
+/// member function to update the compilation commands to select the built module files first.
+struct ModuleFilesInfo {
+  ModuleFilesInfo() = default;
+  ~ModuleFilesInfo();
+
+  ModuleFilesInfo(const ModuleFilesInfo &) = delete;
+  ModuleFilesInfo operator=(const ModuleFilesInfo &) = delete;
+
+  ModuleFilesInfo(ModuleFilesInfo &&Other)
+      : Successed(std::exchange(Other.Successed, false)),
+        UniqueModuleFilesPathPrefix(
+            std::move(Other.UniqueModuleFilesPathPrefix)),
+        DependentModuleNames(std::move(Other.DependentModuleNames)) {
+    Other.UniqueModuleFilesPathPrefix.clear();
+  }
+  ModuleFilesInfo &operator=(ModuleFilesInfo &&Other) {
+    if (this == &Other)
+      return *this;
+
+    this->~ModuleFilesInfo();
+    new (this) ModuleFilesInfo(std::move(Other));
+
+    return *this;
+  }
+
+  /// Build all the required module files for \param File.
+  /// Note that only the module files recorded by \param CDB can be built.
+  static ModuleFilesInfo
+  buildModuleFilesInfoFor(PathRef File, const ThreadsafeFS *TFS,
+                          const GlobalCompilationDatabase &CDB);
+
+  /// Return true if the modile file specified by ModuleName is built.
+  /// Note that this interface will only check the existence of the module
+  /// file instead of checking the validness of the module file. 
+  bool IsModuleUnitBuilt(StringRef ModuleName) const;
----------------
ChuanqiXu9 wrote:

This is used in unittests. I've added a comment.

https://github.com/llvm/llvm-project/pull/66462


More information about the cfe-commits mailing list