blob: 213222c6cf8512ce6e66c8c379363feb04f10a77 [file] [log] [blame]
Stephen Hines651f13c2014-04-23 16:59:28 -07001//===- unittests/libclang/LibclangTest.cpp --- libclang tests -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "clang-c/Index.h"
11#include "gtest/gtest.h"
12
13TEST(libclang, clang_parseTranslationUnit2_InvalidArgs) {
14 EXPECT_EQ(CXError_InvalidArguments,
15 clang_parseTranslationUnit2(0, 0, 0, 0, 0, 0, 0, 0));
16}
17
18TEST(libclang, clang_createTranslationUnit_InvalidArgs) {
19 EXPECT_EQ(0, clang_createTranslationUnit(0, 0));
20}
21
22TEST(libclang, clang_createTranslationUnit2_InvalidArgs) {
23 EXPECT_EQ(CXError_InvalidArguments,
24 clang_createTranslationUnit2(0, 0, 0));
25
26 CXTranslationUnit TU = reinterpret_cast<CXTranslationUnit>(1);
27 EXPECT_EQ(CXError_InvalidArguments,
28 clang_createTranslationUnit2(0, 0, &TU));
29 EXPECT_EQ(0, TU);
30}
31
32namespace {
33struct TestVFO {
34 const char *Contents;
35 CXVirtualFileOverlay VFO;
36
37 TestVFO(const char *Contents) : Contents(Contents) {
38 VFO = clang_VirtualFileOverlay_create(0);
39 }
40
41 void map(const char *VPath, const char *RPath) {
42 CXErrorCode Err = clang_VirtualFileOverlay_addFileMapping(VFO, VPath, RPath);
43 EXPECT_EQ(Err, CXError_Success);
44 }
45
46 void mapError(const char *VPath, const char *RPath, CXErrorCode ExpErr) {
47 CXErrorCode Err = clang_VirtualFileOverlay_addFileMapping(VFO, VPath, RPath);
48 EXPECT_EQ(Err, ExpErr);
49 }
50
51 ~TestVFO() {
Stephen Hines6bcf27b2014-05-29 04:14:42 -070052 if (Contents) {
53 char *BufPtr;
54 unsigned BufSize;
55 clang_VirtualFileOverlay_writeToBuffer(VFO, 0, &BufPtr, &BufSize);
56 std::string BufStr(BufPtr, BufSize);
57 EXPECT_STREQ(Contents, BufStr.c_str());
58 free(BufPtr);
59 }
Stephen Hines651f13c2014-04-23 16:59:28 -070060 clang_VirtualFileOverlay_dispose(VFO);
61 }
62};
63}
64
Stephen Hines6bcf27b2014-05-29 04:14:42 -070065TEST(libclang, VirtualFileOverlay_Basic) {
66 const char *contents =
67 "{\n"
68 " 'version': 0,\n"
69 " 'roots': [\n"
70 " {\n"
71 " 'type': 'directory',\n"
72 " 'name': \"/path/virtual\",\n"
73 " 'contents': [\n"
74 " {\n"
75 " 'type': 'file',\n"
76 " 'name': \"foo.h\",\n"
77 " 'external-contents': \"/real/foo.h\"\n"
78 " }\n"
79 " ]\n"
80 " }\n"
81 " ]\n"
82 "}\n";
83 TestVFO T(contents);
84 T.map("/path/virtual/foo.h", "/real/foo.h");
85}
86
87TEST(libclang, VirtualFileOverlay_Unicode) {
88 const char *contents =
89 "{\n"
90 " 'version': 0,\n"
91 " 'roots': [\n"
92 " {\n"
93 " 'type': 'directory',\n"
94 " 'name': \"/path/\\u266B\",\n"
95 " 'contents': [\n"
96 " {\n"
97 " 'type': 'file',\n"
98 " 'name': \"\\u2602.h\",\n"
99 " 'external-contents': \"/real/\\u2602.h\"\n"
100 " }\n"
101 " ]\n"
102 " }\n"
103 " ]\n"
104 "}\n";
105 TestVFO T(contents);
106 T.map("/path/♫/☂.h", "/real/☂.h");
107}
108
109TEST(libclang, VirtualFileOverlay_InvalidArgs) {
110 TestVFO T(NULL);
111 T.mapError("/path/./virtual/../foo.h", "/real/foo.h",
112 CXError_InvalidArguments);
113}
114
115TEST(libclang, VirtualFileOverlay_RemapDirectories) {
116 const char *contents =
117 "{\n"
118 " 'version': 0,\n"
119 " 'roots': [\n"
120 " {\n"
121 " 'type': 'directory',\n"
122 " 'name': \"/another/dir\",\n"
123 " 'contents': [\n"
124 " {\n"
125 " 'type': 'file',\n"
126 " 'name': \"foo2.h\",\n"
127 " 'external-contents': \"/real/foo2.h\"\n"
128 " }\n"
129 " ]\n"
130 " },\n"
131 " {\n"
132 " 'type': 'directory',\n"
133 " 'name': \"/path/virtual/dir\",\n"
134 " 'contents': [\n"
135 " {\n"
136 " 'type': 'file',\n"
137 " 'name': \"foo1.h\",\n"
138 " 'external-contents': \"/real/foo1.h\"\n"
139 " },\n"
140 " {\n"
141 " 'type': 'file',\n"
142 " 'name': \"foo3.h\",\n"
143 " 'external-contents': \"/real/foo3.h\"\n"
144 " },\n"
145 " {\n"
146 " 'type': 'directory',\n"
147 " 'name': \"in/subdir\",\n"
148 " 'contents': [\n"
149 " {\n"
150 " 'type': 'file',\n"
151 " 'name': \"foo4.h\",\n"
152 " 'external-contents': \"/real/foo4.h\"\n"
153 " }\n"
154 " ]\n"
155 " }\n"
156 " ]\n"
157 " }\n"
158 " ]\n"
159 "}\n";
160 TestVFO T(contents);
161 T.map("/path/virtual/dir/foo1.h", "/real/foo1.h");
162 T.map("/another/dir/foo2.h", "/real/foo2.h");
163 T.map("/path/virtual/dir/foo3.h", "/real/foo3.h");
164 T.map("/path/virtual/dir/in/subdir/foo4.h", "/real/foo4.h");
165}
166
167TEST(libclang, VirtualFileOverlay_CaseInsensitive) {
168 const char *contents =
169 "{\n"
170 " 'version': 0,\n"
171 " 'case-sensitive': 'false',\n"
172 " 'roots': [\n"
173 " {\n"
174 " 'type': 'directory',\n"
175 " 'name': \"/path/virtual\",\n"
176 " 'contents': [\n"
177 " {\n"
178 " 'type': 'file',\n"
179 " 'name': \"foo.h\",\n"
180 " 'external-contents': \"/real/foo.h\"\n"
181 " }\n"
182 " ]\n"
183 " }\n"
184 " ]\n"
185 "}\n";
186 TestVFO T(contents);
187 T.map("/path/virtual/foo.h", "/real/foo.h");
188 clang_VirtualFileOverlay_setCaseSensitivity(T.VFO, false);
189}
190
191TEST(libclang, VirtualFileOverlay_SharedPrefix) {
192 const char *contents =
193 "{\n"
194 " 'version': 0,\n"
195 " 'roots': [\n"
196 " {\n"
197 " 'type': 'directory',\n"
198 " 'name': \"/path/foo\",\n"
199 " 'contents': [\n"
200 " {\n"
201 " 'type': 'file',\n"
202 " 'name': \"bar\",\n"
203 " 'external-contents': \"/real/bar\"\n"
204 " },\n"
205 " {\n"
206 " 'type': 'file',\n"
207 " 'name': \"bar.h\",\n"
208 " 'external-contents': \"/real/bar.h\"\n"
209 " }\n"
210 " ]\n"
211 " },\n"
212 " {\n"
213 " 'type': 'directory',\n"
214 " 'name': \"/path/foobar\",\n"
215 " 'contents': [\n"
216 " {\n"
217 " 'type': 'file',\n"
218 " 'name': \"baz.h\",\n"
219 " 'external-contents': \"/real/baz.h\"\n"
220 " }\n"
221 " ]\n"
222 " },\n"
223 " {\n"
224 " 'type': 'directory',\n"
225 " 'name': \"/path\",\n"
226 " 'contents': [\n"
227 " {\n"
228 " 'type': 'file',\n"
229 " 'name': \"foobarbaz.h\",\n"
230 " 'external-contents': \"/real/foobarbaz.h\"\n"
231 " }\n"
232 " ]\n"
233 " }\n"
234 " ]\n"
235 "}\n";
236 TestVFO T(contents);
237 T.map("/path/foo/bar.h", "/real/bar.h");
238 T.map("/path/foo/bar", "/real/bar");
239 T.map("/path/foobar/baz.h", "/real/baz.h");
240 T.map("/path/foobarbaz.h", "/real/foobarbaz.h");
241}
242
243TEST(libclang, VirtualFileOverlay_AdjacentDirectory) {
244 const char *contents =
245 "{\n"
246 " 'version': 0,\n"
247 " 'roots': [\n"
248 " {\n"
249 " 'type': 'directory',\n"
250 " 'name': \"/path/dir1\",\n"
251 " 'contents': [\n"
252 " {\n"
253 " 'type': 'file',\n"
254 " 'name': \"foo.h\",\n"
255 " 'external-contents': \"/real/foo.h\"\n"
256 " },\n"
257 " {\n"
258 " 'type': 'directory',\n"
259 " 'name': \"subdir\",\n"
260 " 'contents': [\n"
261 " {\n"
262 " 'type': 'file',\n"
263 " 'name': \"bar.h\",\n"
264 " 'external-contents': \"/real/bar.h\"\n"
265 " }\n"
266 " ]\n"
267 " }\n"
268 " ]\n"
269 " },\n"
270 " {\n"
271 " 'type': 'directory',\n"
272 " 'name': \"/path/dir2\",\n"
273 " 'contents': [\n"
274 " {\n"
275 " 'type': 'file',\n"
276 " 'name': \"baz.h\",\n"
277 " 'external-contents': \"/real/baz.h\"\n"
278 " }\n"
279 " ]\n"
280 " }\n"
281 " ]\n"
282 "}\n";
283 TestVFO T(contents);
284 T.map("/path/dir1/foo.h", "/real/foo.h");
285 T.map("/path/dir1/subdir/bar.h", "/real/bar.h");
286 T.map("/path/dir2/baz.h", "/real/baz.h");
287}
288
289TEST(libclang, VirtualFileOverlay_TopLevel) {
290 const char *contents =
291 "{\n"
292 " 'version': 0,\n"
293 " 'roots': [\n"
294 " {\n"
295 " 'type': 'directory',\n"
296 " 'name': \"/\",\n"
297 " 'contents': [\n"
298 " {\n"
299 " 'type': 'file',\n"
300 " 'name': \"foo.h\",\n"
301 " 'external-contents': \"/real/foo.h\"\n"
302 " }\n"
303 " ]\n"
304 " }\n"
305 " ]\n"
306 "}\n";
307 TestVFO T(contents);
308 T.map("/foo.h", "/real/foo.h");
Stephen Hines651f13c2014-04-23 16:59:28 -0700309}
310
311TEST(libclang, ModuleMapDescriptor) {
312 const char *Contents =
313 "framework module TestFrame {\n"
314 " umbrella header \"TestFrame.h\"\n"
315 "\n"
316 " export *\n"
317 " module * { export * }\n"
318 "}\n";
319
320 CXModuleMapDescriptor MMD = clang_ModuleMapDescriptor_create(0);
321
322 clang_ModuleMapDescriptor_setFrameworkModuleName(MMD, "TestFrame");
323 clang_ModuleMapDescriptor_setUmbrellaHeader(MMD, "TestFrame.h");
324
325 char *BufPtr;
326 unsigned BufSize;
327 clang_ModuleMapDescriptor_writeToBuffer(MMD, 0, &BufPtr, &BufSize);
328 std::string BufStr(BufPtr, BufSize);
329 EXPECT_STREQ(Contents, BufStr.c_str());
330 free(BufPtr);
331 clang_ModuleMapDescriptor_dispose(MMD);
332}