LibclangTest.cpp revision 58878f85ab89b13e9eea4af3ccf055e42c557bc8
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 20TEST(libclang, clang_parseTranslationUnit2_InvalidArgs) { 21 EXPECT_EQ(CXError_InvalidArguments, 22 clang_parseTranslationUnit2(nullptr, nullptr, nullptr, 0, nullptr, 23 0, 0, nullptr)); 24} 25 26TEST(libclang, clang_createTranslationUnit_InvalidArgs) { 27 EXPECT_EQ(nullptr, clang_createTranslationUnit(nullptr, nullptr)); 28} 29 30TEST(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 40namespace { 41struct 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 73TEST(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 95TEST(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 117TEST(libclang, VirtualFileOverlay_InvalidArgs) { 118 TestVFO T(nullptr); 119 T.mapError("/path/./virtual/../foo.h", "/real/foo.h", 120 CXError_InvalidArguments); 121} 122 123TEST(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 175TEST(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 199TEST(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 251TEST(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 297TEST(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 319TEST(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 329TEST(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 free(BufPtr); 349 clang_ModuleMapDescriptor_dispose(MMD); 350} 351 352class LibclangReparseTest : public ::testing::Test { 353 std::set<std::string> Files; 354public: 355 std::string TestDir; 356 CXIndex Index; 357 CXTranslationUnit ClangTU; 358 unsigned TUFlags; 359 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 } 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 } 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 std::ofstream OS(Filename); 383 OS << Contents; 384 } 385 void DisplayDiagnostics() { 386 unsigned NumDiagnostics = clang_getNumDiagnostics(ClangTU); 387 for (unsigned i = 0; i < NumDiagnostics; ++i) { 388 auto Diag = clang_getDiagnostic(ClangTU, i); 389 DEBUG(llvm::dbgs() << clang_getCString(clang_formatDiagnostic( 390 Diag, clang_defaultDiagnosticDisplayOptions())) << "\n"); 391 clang_disposeDiagnostic(Diag); 392 } 393 } 394 bool ReparseTU(unsigned num_unsaved_files, CXUnsavedFile* unsaved_files) { 395 if (clang_reparseTranslationUnit(ClangTU, num_unsaved_files, unsaved_files, 396 clang_defaultReparseOptions(ClangTU))) { 397 DEBUG(llvm::dbgs() << "Reparse failed\n"); 398 return false; 399 } 400 DisplayDiagnostics(); 401 return true; 402 } 403}; 404 405 406TEST_F(LibclangReparseTest, Reparse) { 407 const char *HeaderTop = "#ifndef H\n#define H\nstruct Foo { int bar;"; 408 const char *HeaderBottom = "\n};\n#endif\n"; 409 const char *CppFile = "#include \"HeaderFile.h\"\nint main() {" 410 " Foo foo; foo.bar = 7; foo.baz = 8; }\n"; 411 std::string HeaderName = "HeaderFile.h"; 412 std::string CppName = "CppFile.cpp"; 413 WriteFile(CppName, CppFile); 414 WriteFile(HeaderName, std::string(HeaderTop) + HeaderBottom); 415 416 ClangTU = clang_parseTranslationUnit(Index, CppName.c_str(), nullptr, 0, 417 nullptr, 0, TUFlags); 418 EXPECT_EQ(1U, clang_getNumDiagnostics(ClangTU)); 419 DisplayDiagnostics(); 420 421 // Immedaitely reparse. 422 ASSERT_TRUE(ReparseTU(0, nullptr /* No unsaved files. */)); 423 EXPECT_EQ(1U, clang_getNumDiagnostics(ClangTU)); 424 425 std::string NewHeaderContents = 426 std::string(HeaderTop) + "int baz;" + HeaderBottom; 427 WriteFile(HeaderName, NewHeaderContents); 428 429 // Reparse after fix. 430 ASSERT_TRUE(ReparseTU(0, nullptr /* No unsaved files. */)); 431 EXPECT_EQ(0U, clang_getNumDiagnostics(ClangTU)); 432} 433 434TEST_F(LibclangReparseTest, ReparseWithModule) { 435 const char *HeaderTop = "#ifndef H\n#define H\nstruct Foo { int bar;"; 436 const char *HeaderBottom = "\n};\n#endif\n"; 437 const char *MFile = "#include \"HeaderFile.h\"\nint main() {" 438 " struct Foo foo; foo.bar = 7; foo.baz = 8; }\n"; 439 const char *ModFile = "module A { header \"HeaderFile.h\" }\n"; 440 std::string HeaderName = "HeaderFile.h"; 441 std::string MName = "MFile.m"; 442 std::string ModName = "module.modulemap"; 443 WriteFile(MName, MFile); 444 WriteFile(HeaderName, std::string(HeaderTop) + HeaderBottom); 445 WriteFile(ModName, ModFile); 446 447 std::string ModulesCache = std::string("-fmodules-cache-path=") + TestDir; 448 const char *Args[] = { "-fmodules", ModulesCache.c_str(), 449 "-I", TestDir.c_str() }; 450 int NumArgs = sizeof(Args) / sizeof(Args[0]); 451 ClangTU = clang_parseTranslationUnit(Index, MName.c_str(), Args, NumArgs, 452 nullptr, 0, TUFlags); 453 EXPECT_EQ(1U, clang_getNumDiagnostics(ClangTU)); 454 DisplayDiagnostics(); 455 456 // Immedaitely reparse. 457 ASSERT_TRUE(ReparseTU(0, nullptr /* No unsaved files. */)); 458 EXPECT_EQ(1U, clang_getNumDiagnostics(ClangTU)); 459 460 std::string NewHeaderContents = 461 std::string(HeaderTop) + "int baz;" + HeaderBottom; 462 WriteFile(HeaderName, NewHeaderContents); 463 464 // Reparse after fix. 465 ASSERT_TRUE(ReparseTU(0, nullptr /* No unsaved files. */)); 466 EXPECT_EQ(0U, clang_getNumDiagnostics(ClangTU)); 467} 468