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 #include "llvm/Support/Debug.h" 13 #include "llvm/Support/FileSystem.h" 14 #include "llvm/Support/Path.h" 15 #include "llvm/Support/raw_ostream.h" 16 #include <fstream> 17 #include <set> 18 #define DEBUG_TYPE "libclang-test" 19 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 26 TEST(libclang, clang_createTranslationUnit_InvalidArgs) { 27 EXPECT_EQ(nullptr, clang_createTranslationUnit(nullptr, nullptr)); 28 } 29 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 45 TestVFO(const char *Contents) : Contents(Contents) { 46 VFO = clang_VirtualFileOverlay_create(0); 47 } 48 49 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 54 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 59 ~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 free(BufPtr); 67 } 68 clang_VirtualFileOverlay_dispose(VFO); 69 } 70 }; 71 } 72 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 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 117 TEST(libclang, VirtualFileOverlay_InvalidArgs) { 118 TestVFO T(nullptr); 119 T.mapError("/path/./virtual/../foo.h", "/real/foo.h", 120 CXError_InvalidArguments); 121 } 122 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 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 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 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 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 319 TEST(libclang, ModuleMapDescriptor) { 320 const char *Contents = 321 "framework module TestFrame {\n" 322 " umbrella header \"TestFrame.h\"\n" 323 "\n" 324 " export *\n" 325 " module * { export * }\n" 326 "}\n"; 327 328 CXModuleMapDescriptor MMD = clang_ModuleMapDescriptor_create(0); 329 330 clang_ModuleMapDescriptor_setFrameworkModuleName(MMD, "TestFrame"); 331 clang_ModuleMapDescriptor_setUmbrellaHeader(MMD, "TestFrame.h"); 332 333 char *BufPtr; 334 unsigned BufSize; 335 clang_ModuleMapDescriptor_writeToBuffer(MMD, 0, &BufPtr, &BufSize); 336 std::string BufStr(BufPtr, BufSize); 337 EXPECT_STREQ(Contents, BufStr.c_str()); 338 free(BufPtr); 339 clang_ModuleMapDescriptor_dispose(MMD); 340 } 341 342 class LibclangReparseTest : public ::testing::Test { 343 std::set<std::string> Files; 344 public: 345 std::string TestDir; 346 CXIndex Index; 347 CXTranslationUnit ClangTU; 348 unsigned TUFlags; 349 350 void SetUp() { 351 llvm::SmallString<256> Dir; 352 ASSERT_FALSE(llvm::sys::fs::createUniqueDirectory("libclang-test", Dir)); 353 TestDir = Dir.str(); 354 TUFlags = CXTranslationUnit_DetailedPreprocessingRecord | 355 clang_defaultEditingTranslationUnitOptions(); 356 Index = clang_createIndex(0, 0); 357 } 358 void TearDown() { 359 clang_disposeTranslationUnit(ClangTU); 360 clang_disposeIndex(Index); 361 for (const std::string &Path : Files) 362 llvm::sys::fs::remove(Path); 363 llvm::sys::fs::remove(TestDir); 364 } 365 void WriteFile(std::string &Filename, const std::string &Contents) { 366 if (!llvm::sys::path::is_absolute(Filename)) { 367 llvm::SmallString<256> Path(TestDir); 368 llvm::sys::path::append(Path, Filename); 369 Filename = Path.str(); 370 Files.insert(Filename); 371 } 372 std::ofstream OS(Filename); 373 OS << Contents; 374 } 375 void DisplayDiagnostics() { 376 unsigned NumDiagnostics = clang_getNumDiagnostics(ClangTU); 377 for (unsigned i = 0; i < NumDiagnostics; ++i) { 378 auto Diag = clang_getDiagnostic(ClangTU, i); 379 DEBUG(llvm::dbgs() << clang_getCString(clang_formatDiagnostic( 380 Diag, clang_defaultDiagnosticDisplayOptions())) << "\n"); 381 clang_disposeDiagnostic(Diag); 382 } 383 } 384 bool ReparseTU(unsigned num_unsaved_files, CXUnsavedFile* unsaved_files) { 385 if (clang_reparseTranslationUnit(ClangTU, num_unsaved_files, unsaved_files, 386 clang_defaultReparseOptions(ClangTU))) { 387 DEBUG(llvm::dbgs() << "Reparse failed\n"); 388 return false; 389 } 390 DisplayDiagnostics(); 391 return true; 392 } 393 }; 394 395 396 TEST_F(LibclangReparseTest, Reparse) { 397 const char *HeaderTop = "#ifndef H\n#define H\nstruct Foo { int bar;"; 398 const char *HeaderBottom = "\n};\n#endif\n"; 399 const char *CppFile = "#include \"HeaderFile.h\"\nint main() {" 400 " Foo foo; foo.bar = 7; foo.baz = 8; }\n"; 401 std::string HeaderName = "HeaderFile.h"; 402 std::string CppName = "CppFile.cpp"; 403 WriteFile(CppName, CppFile); 404 WriteFile(HeaderName, std::string(HeaderTop) + HeaderBottom); 405 406 ClangTU = clang_parseTranslationUnit(Index, CppName.c_str(), nullptr, 0, 407 nullptr, 0, TUFlags); 408 EXPECT_EQ(1U, clang_getNumDiagnostics(ClangTU)); 409 DisplayDiagnostics(); 410 411 // Immedaitely reparse. 412 ASSERT_TRUE(ReparseTU(0, nullptr /* No unsaved files. */)); 413 EXPECT_EQ(1U, clang_getNumDiagnostics(ClangTU)); 414 415 std::string NewHeaderContents = 416 std::string(HeaderTop) + "int baz;" + HeaderBottom; 417 WriteFile(HeaderName, NewHeaderContents); 418 419 // Reparse after fix. 420 ASSERT_TRUE(ReparseTU(0, nullptr /* No unsaved files. */)); 421 EXPECT_EQ(0U, clang_getNumDiagnostics(ClangTU)); 422 } 423 424 TEST_F(LibclangReparseTest, ReparseWithModule) { 425 const char *HeaderTop = "#ifndef H\n#define H\nstruct Foo { int bar;"; 426 const char *HeaderBottom = "\n};\n#endif\n"; 427 const char *MFile = "#include \"HeaderFile.h\"\nint main() {" 428 " struct Foo foo; foo.bar = 7; foo.baz = 8; }\n"; 429 const char *ModFile = "module A { header \"HeaderFile.h\" }\n"; 430 std::string HeaderName = "HeaderFile.h"; 431 std::string MName = "MFile.m"; 432 std::string ModName = "module.modulemap"; 433 WriteFile(MName, MFile); 434 WriteFile(HeaderName, std::string(HeaderTop) + HeaderBottom); 435 WriteFile(ModName, ModFile); 436 437 std::string ModulesCache = std::string("-fmodules-cache-path=") + TestDir; 438 const char *Args[] = { "-fmodules", ModulesCache.c_str(), 439 "-I", TestDir.c_str() }; 440 int NumArgs = sizeof(Args) / sizeof(Args[0]); 441 ClangTU = clang_parseTranslationUnit(Index, MName.c_str(), Args, NumArgs, 442 nullptr, 0, TUFlags); 443 EXPECT_EQ(1U, clang_getNumDiagnostics(ClangTU)); 444 DisplayDiagnostics(); 445 446 // Immedaitely reparse. 447 ASSERT_TRUE(ReparseTU(0, nullptr /* No unsaved files. */)); 448 EXPECT_EQ(1U, clang_getNumDiagnostics(ClangTU)); 449 450 std::string NewHeaderContents = 451 std::string(HeaderTop) + "int baz;" + HeaderBottom; 452 WriteFile(HeaderName, NewHeaderContents); 453 454 // Reparse after fix. 455 ASSERT_TRUE(ReparseTU(0, nullptr /* No unsaved files. */)); 456 EXPECT_EQ(0U, clang_getNumDiagnostics(ClangTU)); 457 } 458