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