• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "llvm/Support/Debug.h"
12 #include "llvm/Support/FileSystem.h"
13 #include "llvm/Support/Path.h"
14 #include "llvm/Support/raw_ostream.h"
15 #include "gtest/gtest.h"
16 #include <fstream>
17 #include <set>
18 #define DEBUG_TYPE "libclang-test"
19 
TEST(libclang,clang_parseTranslationUnit2_InvalidArgs)20 TEST(libclang, clang_parseTranslationUnit2_InvalidArgs) {
21   EXPECT_EQ(CXError_InvalidArguments,
22             clang_parseTranslationUnit2(nullptr, nullptr, nullptr, 0, nullptr,
23                                         0, 0, nullptr));
24 }
25 
TEST(libclang,clang_createTranslationUnit_InvalidArgs)26 TEST(libclang, clang_createTranslationUnit_InvalidArgs) {
27   EXPECT_EQ(nullptr, clang_createTranslationUnit(nullptr, nullptr));
28 }
29 
TEST(libclang,clang_createTranslationUnit2_InvalidArgs)30 TEST(libclang, clang_createTranslationUnit2_InvalidArgs) {
31   EXPECT_EQ(CXError_InvalidArguments,
32             clang_createTranslationUnit2(nullptr, nullptr, nullptr));
33 
34   CXTranslationUnit TU = reinterpret_cast<CXTranslationUnit>(1);
35   EXPECT_EQ(CXError_InvalidArguments,
36             clang_createTranslationUnit2(nullptr, nullptr, &TU));
37   EXPECT_EQ(nullptr, TU);
38 }
39 
40 namespace {
41 struct TestVFO {
42   const char *Contents;
43   CXVirtualFileOverlay VFO;
44 
TestVFO__anonb2e0c2dc0111::TestVFO45   TestVFO(const char *Contents) : Contents(Contents) {
46     VFO = clang_VirtualFileOverlay_create(0);
47   }
48 
map__anonb2e0c2dc0111::TestVFO49   void map(const char *VPath, const char *RPath) {
50     CXErrorCode Err = clang_VirtualFileOverlay_addFileMapping(VFO, VPath, RPath);
51     EXPECT_EQ(Err, CXError_Success);
52   }
53 
mapError__anonb2e0c2dc0111::TestVFO54   void mapError(const char *VPath, const char *RPath, CXErrorCode ExpErr) {
55     CXErrorCode Err = clang_VirtualFileOverlay_addFileMapping(VFO, VPath, RPath);
56     EXPECT_EQ(Err, ExpErr);
57   }
58 
~TestVFO__anonb2e0c2dc0111::TestVFO59   ~TestVFO() {
60     if (Contents) {
61       char *BufPtr;
62       unsigned BufSize;
63       clang_VirtualFileOverlay_writeToBuffer(VFO, 0, &BufPtr, &BufSize);
64       std::string BufStr(BufPtr, BufSize);
65       EXPECT_STREQ(Contents, BufStr.c_str());
66       clang_free(BufPtr);
67     }
68     clang_VirtualFileOverlay_dispose(VFO);
69   }
70 };
71 }
72 
TEST(libclang,VirtualFileOverlay_Basic)73 TEST(libclang, VirtualFileOverlay_Basic) {
74   const char *contents =
75       "{\n"
76       "  'version': 0,\n"
77       "  'roots': [\n"
78       "    {\n"
79       "      'type': 'directory',\n"
80       "      'name': \"/path/virtual\",\n"
81       "      'contents': [\n"
82       "        {\n"
83       "          'type': 'file',\n"
84       "          'name': \"foo.h\",\n"
85       "          'external-contents': \"/real/foo.h\"\n"
86       "        }\n"
87       "      ]\n"
88       "    }\n"
89       "  ]\n"
90       "}\n";
91   TestVFO T(contents);
92   T.map("/path/virtual/foo.h", "/real/foo.h");
93 }
94 
TEST(libclang,VirtualFileOverlay_Unicode)95 TEST(libclang, VirtualFileOverlay_Unicode) {
96   const char *contents =
97       "{\n"
98       "  'version': 0,\n"
99       "  'roots': [\n"
100       "    {\n"
101       "      'type': 'directory',\n"
102       "      'name': \"/path/\\u266B\",\n"
103       "      'contents': [\n"
104       "        {\n"
105       "          'type': 'file',\n"
106       "          'name': \"\\u2602.h\",\n"
107       "          'external-contents': \"/real/\\u2602.h\"\n"
108       "        }\n"
109       "      ]\n"
110       "    }\n"
111       "  ]\n"
112       "}\n";
113   TestVFO T(contents);
114   T.map("/path/♫/☂.h", "/real/☂.h");
115 }
116 
TEST(libclang,VirtualFileOverlay_InvalidArgs)117 TEST(libclang, VirtualFileOverlay_InvalidArgs) {
118   TestVFO T(nullptr);
119   T.mapError("/path/./virtual/../foo.h", "/real/foo.h",
120              CXError_InvalidArguments);
121 }
122 
TEST(libclang,VirtualFileOverlay_RemapDirectories)123 TEST(libclang, VirtualFileOverlay_RemapDirectories) {
124   const char *contents =
125       "{\n"
126       "  'version': 0,\n"
127       "  'roots': [\n"
128       "    {\n"
129       "      'type': 'directory',\n"
130       "      'name': \"/another/dir\",\n"
131       "      'contents': [\n"
132       "        {\n"
133       "          'type': 'file',\n"
134       "          'name': \"foo2.h\",\n"
135       "          'external-contents': \"/real/foo2.h\"\n"
136       "        }\n"
137       "      ]\n"
138       "    },\n"
139       "    {\n"
140       "      'type': 'directory',\n"
141       "      'name': \"/path/virtual/dir\",\n"
142       "      'contents': [\n"
143       "        {\n"
144       "          'type': 'file',\n"
145       "          'name': \"foo1.h\",\n"
146       "          'external-contents': \"/real/foo1.h\"\n"
147       "        },\n"
148       "        {\n"
149       "          'type': 'file',\n"
150       "          'name': \"foo3.h\",\n"
151       "          'external-contents': \"/real/foo3.h\"\n"
152       "        },\n"
153       "        {\n"
154       "          'type': 'directory',\n"
155       "          'name': \"in/subdir\",\n"
156       "          'contents': [\n"
157       "            {\n"
158       "              'type': 'file',\n"
159       "              'name': \"foo4.h\",\n"
160       "              'external-contents': \"/real/foo4.h\"\n"
161       "            }\n"
162       "          ]\n"
163       "        }\n"
164       "      ]\n"
165       "    }\n"
166       "  ]\n"
167       "}\n";
168   TestVFO T(contents);
169   T.map("/path/virtual/dir/foo1.h", "/real/foo1.h");
170   T.map("/another/dir/foo2.h", "/real/foo2.h");
171   T.map("/path/virtual/dir/foo3.h", "/real/foo3.h");
172   T.map("/path/virtual/dir/in/subdir/foo4.h", "/real/foo4.h");
173 }
174 
TEST(libclang,VirtualFileOverlay_CaseInsensitive)175 TEST(libclang, VirtualFileOverlay_CaseInsensitive) {
176   const char *contents =
177       "{\n"
178       "  'version': 0,\n"
179       "  'case-sensitive': 'false',\n"
180       "  'roots': [\n"
181       "    {\n"
182       "      'type': 'directory',\n"
183       "      'name': \"/path/virtual\",\n"
184       "      'contents': [\n"
185       "        {\n"
186       "          'type': 'file',\n"
187       "          'name': \"foo.h\",\n"
188       "          'external-contents': \"/real/foo.h\"\n"
189       "        }\n"
190       "      ]\n"
191       "    }\n"
192       "  ]\n"
193       "}\n";
194   TestVFO T(contents);
195   T.map("/path/virtual/foo.h", "/real/foo.h");
196   clang_VirtualFileOverlay_setCaseSensitivity(T.VFO, false);
197 }
198 
TEST(libclang,VirtualFileOverlay_SharedPrefix)199 TEST(libclang, VirtualFileOverlay_SharedPrefix) {
200   const char *contents =
201       "{\n"
202       "  'version': 0,\n"
203       "  'roots': [\n"
204       "    {\n"
205       "      'type': 'directory',\n"
206       "      'name': \"/path/foo\",\n"
207       "      'contents': [\n"
208       "        {\n"
209       "          'type': 'file',\n"
210       "          'name': \"bar\",\n"
211       "          'external-contents': \"/real/bar\"\n"
212       "        },\n"
213       "        {\n"
214       "          'type': 'file',\n"
215       "          'name': \"bar.h\",\n"
216       "          'external-contents': \"/real/bar.h\"\n"
217       "        }\n"
218       "      ]\n"
219       "    },\n"
220       "    {\n"
221       "      'type': 'directory',\n"
222       "      'name': \"/path/foobar\",\n"
223       "      'contents': [\n"
224       "        {\n"
225       "          'type': 'file',\n"
226       "          'name': \"baz.h\",\n"
227       "          'external-contents': \"/real/baz.h\"\n"
228       "        }\n"
229       "      ]\n"
230       "    },\n"
231       "    {\n"
232       "      'type': 'directory',\n"
233       "      'name': \"/path\",\n"
234       "      'contents': [\n"
235       "        {\n"
236       "          'type': 'file',\n"
237       "          'name': \"foobarbaz.h\",\n"
238       "          'external-contents': \"/real/foobarbaz.h\"\n"
239       "        }\n"
240       "      ]\n"
241       "    }\n"
242       "  ]\n"
243       "}\n";
244   TestVFO T(contents);
245   T.map("/path/foo/bar.h", "/real/bar.h");
246   T.map("/path/foo/bar", "/real/bar");
247   T.map("/path/foobar/baz.h", "/real/baz.h");
248   T.map("/path/foobarbaz.h", "/real/foobarbaz.h");
249 }
250 
TEST(libclang,VirtualFileOverlay_AdjacentDirectory)251 TEST(libclang, VirtualFileOverlay_AdjacentDirectory) {
252   const char *contents =
253       "{\n"
254       "  'version': 0,\n"
255       "  'roots': [\n"
256       "    {\n"
257       "      'type': 'directory',\n"
258       "      'name': \"/path/dir1\",\n"
259       "      'contents': [\n"
260       "        {\n"
261       "          'type': 'file',\n"
262       "          'name': \"foo.h\",\n"
263       "          'external-contents': \"/real/foo.h\"\n"
264       "        },\n"
265       "        {\n"
266       "          'type': 'directory',\n"
267       "          'name': \"subdir\",\n"
268       "          'contents': [\n"
269       "            {\n"
270       "              'type': 'file',\n"
271       "              'name': \"bar.h\",\n"
272       "              'external-contents': \"/real/bar.h\"\n"
273       "            }\n"
274       "          ]\n"
275       "        }\n"
276       "      ]\n"
277       "    },\n"
278       "    {\n"
279       "      'type': 'directory',\n"
280       "      'name': \"/path/dir2\",\n"
281       "      'contents': [\n"
282       "        {\n"
283       "          'type': 'file',\n"
284       "          'name': \"baz.h\",\n"
285       "          'external-contents': \"/real/baz.h\"\n"
286       "        }\n"
287       "      ]\n"
288       "    }\n"
289       "  ]\n"
290       "}\n";
291   TestVFO T(contents);
292   T.map("/path/dir1/foo.h", "/real/foo.h");
293   T.map("/path/dir1/subdir/bar.h", "/real/bar.h");
294   T.map("/path/dir2/baz.h", "/real/baz.h");
295 }
296 
TEST(libclang,VirtualFileOverlay_TopLevel)297 TEST(libclang, VirtualFileOverlay_TopLevel) {
298   const char *contents =
299       "{\n"
300       "  'version': 0,\n"
301       "  'roots': [\n"
302       "    {\n"
303       "      'type': 'directory',\n"
304       "      'name': \"/\",\n"
305       "      'contents': [\n"
306       "        {\n"
307       "          'type': 'file',\n"
308       "          'name': \"foo.h\",\n"
309       "          'external-contents': \"/real/foo.h\"\n"
310       "        }\n"
311       "      ]\n"
312       "    }\n"
313       "  ]\n"
314       "}\n";
315   TestVFO T(contents);
316   T.map("/foo.h", "/real/foo.h");
317 }
318 
TEST(libclang,VirtualFileOverlay_Empty)319 TEST(libclang, VirtualFileOverlay_Empty) {
320   const char *contents =
321       "{\n"
322       "  'version': 0,\n"
323       "  'roots': [\n"
324       "  ]\n"
325       "}\n";
326   TestVFO T(contents);
327 }
328 
TEST(libclang,ModuleMapDescriptor)329 TEST(libclang, ModuleMapDescriptor) {
330   const char *Contents =
331     "framework module TestFrame {\n"
332     "  umbrella header \"TestFrame.h\"\n"
333     "\n"
334     "  export *\n"
335     "  module * { export * }\n"
336     "}\n";
337 
338   CXModuleMapDescriptor MMD = clang_ModuleMapDescriptor_create(0);
339 
340   clang_ModuleMapDescriptor_setFrameworkModuleName(MMD, "TestFrame");
341   clang_ModuleMapDescriptor_setUmbrellaHeader(MMD, "TestFrame.h");
342 
343   char *BufPtr;
344   unsigned BufSize;
345   clang_ModuleMapDescriptor_writeToBuffer(MMD, 0, &BufPtr, &BufSize);
346   std::string BufStr(BufPtr, BufSize);
347   EXPECT_STREQ(Contents, BufStr.c_str());
348   clang_free(BufPtr);
349   clang_ModuleMapDescriptor_dispose(MMD);
350 }
351 
352 class LibclangReparseTest : public ::testing::Test {
353   std::set<std::string> Files;
354 public:
355   std::string TestDir;
356   CXIndex Index;
357   CXTranslationUnit ClangTU;
358   unsigned TUFlags;
359 
SetUp()360   void SetUp() override {
361     llvm::SmallString<256> Dir;
362     ASSERT_FALSE(llvm::sys::fs::createUniqueDirectory("libclang-test", Dir));
363     TestDir = Dir.str();
364     TUFlags = CXTranslationUnit_DetailedPreprocessingRecord |
365               clang_defaultEditingTranslationUnitOptions();
366     Index = clang_createIndex(0, 0);
367   }
TearDown()368   void TearDown() override {
369     clang_disposeTranslationUnit(ClangTU);
370     clang_disposeIndex(Index);
371     for (const std::string &Path : Files)
372       llvm::sys::fs::remove(Path);
373     llvm::sys::fs::remove(TestDir);
374   }
WriteFile(std::string & Filename,const std::string & Contents)375   void WriteFile(std::string &Filename, const std::string &Contents) {
376     if (!llvm::sys::path::is_absolute(Filename)) {
377       llvm::SmallString<256> Path(TestDir);
378       llvm::sys::path::append(Path, Filename);
379       Filename = Path.str();
380       Files.insert(Filename);
381     }
382     llvm::sys::fs::create_directories(llvm::sys::path::parent_path(Filename));
383     std::ofstream OS(Filename);
384     OS << Contents;
385     assert(OS.good());
386   }
DisplayDiagnostics()387   void DisplayDiagnostics() {
388     unsigned NumDiagnostics = clang_getNumDiagnostics(ClangTU);
389     for (unsigned i = 0; i < NumDiagnostics; ++i) {
390       auto Diag = clang_getDiagnostic(ClangTU, i);
391       DEBUG(llvm::dbgs() << clang_getCString(clang_formatDiagnostic(
392           Diag, clang_defaultDiagnosticDisplayOptions())) << "\n");
393       clang_disposeDiagnostic(Diag);
394     }
395   }
ReparseTU(unsigned num_unsaved_files,CXUnsavedFile * unsaved_files)396   bool ReparseTU(unsigned num_unsaved_files, CXUnsavedFile* unsaved_files) {
397     if (clang_reparseTranslationUnit(ClangTU, num_unsaved_files, unsaved_files,
398                                      clang_defaultReparseOptions(ClangTU))) {
399       DEBUG(llvm::dbgs() << "Reparse failed\n");
400       return false;
401     }
402     DisplayDiagnostics();
403     return true;
404   }
405 };
406 
407 
TEST_F(LibclangReparseTest,Reparse)408 TEST_F(LibclangReparseTest, Reparse) {
409   const char *HeaderTop = "#ifndef H\n#define H\nstruct Foo { int bar;";
410   const char *HeaderBottom = "\n};\n#endif\n";
411   const char *CppFile = "#include \"HeaderFile.h\"\nint main() {"
412                          " Foo foo; foo.bar = 7; foo.baz = 8; }\n";
413   std::string HeaderName = "HeaderFile.h";
414   std::string CppName = "CppFile.cpp";
415   WriteFile(CppName, CppFile);
416   WriteFile(HeaderName, std::string(HeaderTop) + HeaderBottom);
417 
418   ClangTU = clang_parseTranslationUnit(Index, CppName.c_str(), nullptr, 0,
419                                        nullptr, 0, TUFlags);
420   EXPECT_EQ(1U, clang_getNumDiagnostics(ClangTU));
421   DisplayDiagnostics();
422 
423   // Immedaitely reparse.
424   ASSERT_TRUE(ReparseTU(0, nullptr /* No unsaved files. */));
425   EXPECT_EQ(1U, clang_getNumDiagnostics(ClangTU));
426 
427   std::string NewHeaderContents =
428       std::string(HeaderTop) + "int baz;" + HeaderBottom;
429   WriteFile(HeaderName, NewHeaderContents);
430 
431   // Reparse after fix.
432   ASSERT_TRUE(ReparseTU(0, nullptr /* No unsaved files. */));
433   EXPECT_EQ(0U, clang_getNumDiagnostics(ClangTU));
434 }
435 
TEST_F(LibclangReparseTest,ReparseWithModule)436 TEST_F(LibclangReparseTest, ReparseWithModule) {
437   const char *HeaderTop = "#ifndef H\n#define H\nstruct Foo { int bar;";
438   const char *HeaderBottom = "\n};\n#endif\n";
439   const char *MFile = "#include \"HeaderFile.h\"\nint main() {"
440                          " struct Foo foo; foo.bar = 7; foo.baz = 8; }\n";
441   const char *ModFile = "module A { header \"HeaderFile.h\" }\n";
442   std::string HeaderName = "HeaderFile.h";
443   std::string MName = "MFile.m";
444   std::string ModName = "module.modulemap";
445   WriteFile(MName, MFile);
446   WriteFile(HeaderName, std::string(HeaderTop) + HeaderBottom);
447   WriteFile(ModName, ModFile);
448 
449   std::string ModulesCache = std::string("-fmodules-cache-path=") + TestDir;
450   const char *Args[] = { "-fmodules", ModulesCache.c_str(),
451                          "-I", TestDir.c_str() };
452   int NumArgs = sizeof(Args) / sizeof(Args[0]);
453   ClangTU = clang_parseTranslationUnit(Index, MName.c_str(), Args, NumArgs,
454                                        nullptr, 0, TUFlags);
455   EXPECT_EQ(1U, clang_getNumDiagnostics(ClangTU));
456   DisplayDiagnostics();
457 
458   // Immedaitely reparse.
459   ASSERT_TRUE(ReparseTU(0, nullptr /* No unsaved files. */));
460   EXPECT_EQ(1U, clang_getNumDiagnostics(ClangTU));
461 
462   std::string NewHeaderContents =
463       std::string(HeaderTop) + "int baz;" + HeaderBottom;
464   WriteFile(HeaderName, NewHeaderContents);
465 
466   // Reparse after fix.
467   ASSERT_TRUE(ReparseTU(0, nullptr /* No unsaved files. */));
468   EXPECT_EQ(0U, clang_getNumDiagnostics(ClangTU));
469 }
470 
TEST_F(LibclangReparseTest,clang_parseTranslationUnit2FullArgv)471 TEST_F(LibclangReparseTest, clang_parseTranslationUnit2FullArgv) {
472   // Provide a fake GCC 99.9.9 standard library that always overrides any local
473   // GCC installation.
474   std::string EmptyFiles[] = {"lib/gcc/arm-linux-gnueabi/99.9.9/crtbegin.o",
475                               "include/arm-linux-gnueabi/.keep",
476                               "include/c++/99.9.9/vector"};
477 
478   for (auto &Name : EmptyFiles)
479     WriteFile(Name, "\n");
480 
481   std::string Filename = "test.cc";
482   WriteFile(Filename, "#include <vector>\n");
483 
484   std::string Clang = "bin/clang";
485   WriteFile(Clang, "");
486 
487   const char *Argv[] = {Clang.c_str(), "-target", "arm-linux-gnueabi",
488                         "-stdlib=libstdc++", "--gcc-toolchain="};
489 
490   EXPECT_EQ(CXError_Success,
491             clang_parseTranslationUnit2FullArgv(Index, Filename.c_str(), Argv,
492                                                 sizeof(Argv) / sizeof(Argv[0]),
493                                                 nullptr, 0, TUFlags, &ClangTU));
494   EXPECT_EQ(0U, clang_getNumDiagnostics(ClangTU));
495   DisplayDiagnostics();
496 }
497