clang-tools  15.0.0git
ModulesTests.cpp
Go to the documentation of this file.
1 //===-- ModulesTests.cpp ---------------------------------------*- 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 #include "TestFS.h"
10 #include "TestTU.h"
11 #include "gmock/gmock.h"
12 #include "gtest/gtest.h"
13 
14 #include <memory>
15 #include <string>
16 
17 namespace clang {
18 namespace clangd {
19 namespace {
20 
21 TEST(Modules, TextualIncludeInPreamble) {
22  TestTU TU = TestTU::withCode(R"cpp(
23  #include "Textual.h"
24 
25  void foo() {}
26 )cpp");
27  TU.ExtraArgs.push_back("-fmodule-name=M");
28  TU.ExtraArgs.push_back("-fmodule-map-file=" + testPath("m.modulemap"));
29  TU.AdditionalFiles["Textual.h"] = "void foo();";
30  TU.AdditionalFiles["m.modulemap"] = R"modulemap(
31  module M {
32  module Textual {
33  textual header "Textual.h"
34  }
35  }
36 )modulemap";
37  // Test that we do not crash.
38  TU.index();
39 }
40 
41 // Verify that visibility of AST nodes belonging to modules, but loaded from
42 // preamble PCH, is restored.
43 TEST(Modules, PreambleBuildVisibility) {
44  TestTU TU = TestTU::withCode(R"cpp(
45  #include "module.h"
46 
47  foo x;
48 )cpp");
49  TU.OverlayRealFileSystemForModules = true;
50  TU.ExtraArgs.push_back("-fmodules");
51  TU.ExtraArgs.push_back("-fmodules-strict-decluse");
52  TU.ExtraArgs.push_back("-Xclang");
53  TU.ExtraArgs.push_back("-fmodules-local-submodule-visibility");
54  TU.ExtraArgs.push_back("-fmodule-map-file=" + testPath("m.modulemap"));
55  TU.AdditionalFiles["module.h"] = R"cpp(
56  typedef int foo;
57 )cpp";
58  TU.AdditionalFiles["m.modulemap"] = R"modulemap(
59  module M {
60  header "module.h"
61  }
62 )modulemap";
63  EXPECT_TRUE(TU.build().getDiagnostics()->empty());
64 }
65 
66 TEST(Modules, Diagnostic) {
67  // Produce a diagnostic while building an implicit module. Use
68  // -fmodules-strict-decluse, but any non-silenced diagnostic will do.
69  TestTU TU = TestTU::withCode(R"cpp(
70  /*error-ok*/
71  #include "modular.h"
72 
73  void bar() {}
74 )cpp");
75  TU.OverlayRealFileSystemForModules = true;
76  TU.ExtraArgs.push_back("-fmodule-map-file=" + testPath("m.modulemap"));
77  TU.ExtraArgs.push_back("-fmodules");
78  TU.ExtraArgs.push_back("-fimplicit-modules");
79  TU.ExtraArgs.push_back("-fmodules-strict-decluse");
80  TU.AdditionalFiles["modular.h"] = R"cpp(
81  #include "non-modular.h"
82  )cpp";
83  TU.AdditionalFiles["non-modular.h"] = "";
84  TU.AdditionalFiles["m.modulemap"] = R"modulemap(
85  module M {
86  header "modular.h"
87  }
88 )modulemap";
89 
90  // Test that we do not crash.
91  TU.build();
92 }
93 
94 // Unknown module formats are a fatal failure for clang. Ensure we don't crash.
95 TEST(Modules, UnknownFormat) {
96  TestTU TU = TestTU::withCode(R"(#include "modular.h")");
97  TU.OverlayRealFileSystemForModules = true;
98  TU.ExtraArgs.push_back("-Xclang");
99  TU.ExtraArgs.push_back("-fmodule-format=obj");
100  TU.ExtraArgs.push_back("-fmodule-map-file=" + testPath("m.modulemap"));
101  TU.ExtraArgs.push_back("-fmodules");
102  TU.ExtraArgs.push_back("-fimplicit-modules");
103  TU.AdditionalFiles["modular.h"] = "";
104  TU.AdditionalFiles["m.modulemap"] = R"modulemap(
105  module M {
106  header "modular.h"
107  })modulemap";
108 
109  // Test that we do not crash.
110  TU.build();
111 }
112 } // namespace
113 } // namespace clangd
114 } // namespace clang
clang::clangd::TEST
TEST(BackgroundQueueTest, Priority)
Definition: BackgroundIndexTests.cpp:750
clang::clangd::testPath
std::string testPath(PathRef File, llvm::sys::path::Style Style)
Definition: TestFS.cpp:94
TestTU.h
Diagnostic
DiagnosticCallback Diagnostic
Definition: ConfigCompile.cpp:100
TestFS.h
clang::clangd::TestTU::withCode
static TestTU withCode(llvm::StringRef Code)
Definition: TestTU.h:36
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27