-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathModule.cpp
More file actions
176 lines (158 loc) · 4.71 KB
/
Module.cpp
File metadata and controls
176 lines (158 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "Test/Test.h"
#include "Compiler/Compilation.h"
#include "Support/Compression.h"
#include "llvm/Support/ToolOutputFile.h"
namespace clice::testing {
namespace {
using namespace std::literals;
auto buildPCM = [](llvm::StringRef file, llvm::StringRef code) {
llvm::SmallString<128> outPath;
fs::createUniquePath(llvm::Twine(file) + "%%%%%%.pcm", outPath, true);
std::string path = file.str();
CompilationParams params;
params.output_file = outPath;
params.arguments = {
"clang++",
"-std=c++20",
"-xc++",
path.c_str(),
};
params.add_remapped_file(file, code);
params.add_remapped_file("./test.h", "export int foo2();");
PCMInfo pcm;
if(!compile(params, pcm)) {
llvm::errs() << "Failed to build PCM\n";
std::abort();
} else {
compressPreCompiledFile(pcm.path);
}
return pcm;
};
auto scan = [](llvm::StringRef content) {
CompilationParams params;
params.arguments = {
"clang++",
"-std=c++20",
"-xc++",
"main.ixx",
};
params.add_remapped_file("main.ixx", content);
params.add_remapped_file("./test.h", "export module A");
auto info = scanModule(params);
if(!info) {
/// std::println("Fail to scan module: {}", info.error());
std::abort();
}
return std::move(*info);
};
suite<"Module"> module = [] {
test("Scan") = [&] {
/// Simple case.
const char* content = R"(
export module A;
import B;
)";
auto info = scan(content);
expect(that % info.isInterfaceUnit == true);
expect(that % info.name == "A"sv);
expect(that % info.mods.size() == 1);
expect(that % info.mods[0] == "B"sv);
/// FIXME: Fix standard library search path(resource dir).
/// With global module fragment and private module fragment.
/// content = R"(
/// module;
/// #include <iostream>
/// export module A;
/// import B;
/// import C;
/// module : private;
///)";
/// info = scan(content);
/// expect(that % info.isInterfaceUnit == true);
/// expect(that % info.name == "A");
/// expect(that % info.mods.size() == 2);
/// expect(that % info.mods[0] == "B");
/// expect(that % info.mods[1] == "C");
/// With module partition.
/// content = R"(
/// module;
/// #include <iostream>
/// export module A:B;
/// import B;
/// import C;
/// module : private;
///)";
/// info = scan(content);
/// expect(that % info.isInterfaceUnit == true);
/// expect(that % info.name == "A:B");
/// expect(that % info.mods.size() == 2);
/// expect(that % info.mods[0] == "B");
/// expect(that % info.mods[1] == "C");
content = R"(
module A;
import B;
import C;
)";
info = scan(content);
expect(that % info.isInterfaceUnit == false);
expect(that % info.name == "A"sv);
expect(that % info.mods.size() == 2);
expect(that % info.mods[0] == "B"sv);
expect(that % info.mods[1] == "C"sv);
};
test("Normal") = [&] {
const char* content = R"(
export module A;
)";
auto pcm = buildPCM("A.ixx", content);
// expect(that % pcm.isInterfaceUnit == true);
// expect(that % pcm.name == "A");
// expect(that % pcm.mods.size() == 0);
};
};
// TEST(Module, ScanModuleName) {
// CompilationParams params;
//
// /// Test module name not in condition directive.
// params.content = "export module A;";
// ASSERT_EQ(scanModuleName(params), "A");
//
// params.content = "export module A.B.C.D;";
// ASSERT_EQ(scanModuleName(params), "A.B.C.D");
//
// params.content = "export module A:B;";
// ASSERT_EQ(scanModuleName(params), "A:B");
//
// params.content = R"(
// module;
// #ifdef TEST
// #include <iostream>
// #endif
// export module A;
//)";
// ASSERT_EQ(scanModuleName(params), "A");
//
// /// Test non-module interface unit.
// params.content = "module A;";
// ASSERT_EQ(scanModuleName(params), "");
//
// params.content = "";
// ASSERT_EQ(scanModuleName(params), "");
//
// /// Test module name in condition directive.
// params.content = R"(
// #ifdef TEST
// export module A;
// #else
// export module B;
// #endif
//)";
// params.srcPath = "main.cppm";
// params.command = "clang++ -std=c++20 -x c++ main.cppm -DTEST";
// ASSERT_EQ(scanModuleName(params), "A");
//
// params.command = "clang++ -std=c++20 -x c++ main.cppm";
// ASSERT_EQ(scanModuleName(params), "B");
// }
} // namespace
} // namespace clice::testing