1 //===- unittests/libclang/LibclangTest.cpp --- libclang tests -------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 #include "clang-c/Index.h"
11 #include "clang-c/Rewrite.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/Support/Debug.h"
14 #include "llvm/Support/FileSystem.h"
15 #include "llvm/Support/Path.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include "gtest/gtest.h"
25 #define DEBUG_TYPE "libclang-test"
27 TEST(libclang
, clang_parseTranslationUnit2_InvalidArgs
) {
28 EXPECT_EQ(CXError_InvalidArguments
,
29 clang_parseTranslationUnit2(nullptr, nullptr, nullptr, 0, nullptr,
33 TEST(libclang
, clang_createTranslationUnit_InvalidArgs
) {
34 EXPECT_EQ(nullptr, clang_createTranslationUnit(nullptr, nullptr));
37 TEST(libclang
, clang_createTranslationUnit2_InvalidArgs
) {
38 EXPECT_EQ(CXError_InvalidArguments
,
39 clang_createTranslationUnit2(nullptr, nullptr, nullptr));
41 CXTranslationUnit TU
= reinterpret_cast<CXTranslationUnit
>(1);
42 EXPECT_EQ(CXError_InvalidArguments
,
43 clang_createTranslationUnit2(nullptr, nullptr, &TU
));
44 EXPECT_EQ(nullptr, TU
);
50 CXVirtualFileOverlay VFO
;
52 TestVFO(const char *Contents
) : Contents(Contents
) {
53 VFO
= clang_VirtualFileOverlay_create(0);
56 void map(const char *VPath
, const char *RPath
) {
57 CXErrorCode Err
= clang_VirtualFileOverlay_addFileMapping(VFO
, VPath
, RPath
);
58 EXPECT_EQ(Err
, CXError_Success
);
61 void mapError(const char *VPath
, const char *RPath
, CXErrorCode ExpErr
) {
62 CXErrorCode Err
= clang_VirtualFileOverlay_addFileMapping(VFO
, VPath
, RPath
);
63 EXPECT_EQ(Err
, ExpErr
);
70 clang_VirtualFileOverlay_writeToBuffer(VFO
, 0, &BufPtr
, &BufSize
);
71 std::string
BufStr(BufPtr
, BufSize
);
72 EXPECT_STREQ(Contents
, BufStr
.c_str());
75 clang_VirtualFileOverlay_dispose(VFO
);
80 TEST(libclang
, VirtualFileOverlay_Basic
) {
81 const char *contents
=
86 " 'type': 'directory',\n"
87 " 'name': \"/path/virtual\",\n"
91 " 'name': \"foo.h\",\n"
92 " 'external-contents': \"/real/foo.h\"\n"
99 T
.map("/path/virtual/foo.h", "/real/foo.h");
102 TEST(libclang
, VirtualFileOverlay_Unicode
) {
103 const char *contents
=
108 " 'type': 'directory',\n"
109 " 'name': \"/path/\\u266B\",\n"
113 " 'name': \"\\u2602.h\",\n"
114 " 'external-contents': \"/real/\\u2602.h\"\n"
121 T
.map("/path/♫/☂.h", "/real/☂.h");
124 TEST(libclang
, VirtualFileOverlay_InvalidArgs
) {
126 T
.mapError("/path/./virtual/../foo.h", "/real/foo.h",
127 CXError_InvalidArguments
);
130 TEST(libclang
, VirtualFileOverlay_RemapDirectories
) {
131 const char *contents
=
136 " 'type': 'directory',\n"
137 " 'name': \"/another/dir\",\n"
141 " 'name': \"foo2.h\",\n"
142 " 'external-contents': \"/real/foo2.h\"\n"
147 " 'type': 'directory',\n"
148 " 'name': \"/path/virtual/dir\",\n"
152 " 'name': \"foo1.h\",\n"
153 " 'external-contents': \"/real/foo1.h\"\n"
157 " 'name': \"foo3.h\",\n"
158 " 'external-contents': \"/real/foo3.h\"\n"
161 " 'type': 'directory',\n"
162 " 'name': \"in/subdir\",\n"
166 " 'name': \"foo4.h\",\n"
167 " 'external-contents': \"/real/foo4.h\"\n"
176 T
.map("/path/virtual/dir/foo1.h", "/real/foo1.h");
177 T
.map("/another/dir/foo2.h", "/real/foo2.h");
178 T
.map("/path/virtual/dir/foo3.h", "/real/foo3.h");
179 T
.map("/path/virtual/dir/in/subdir/foo4.h", "/real/foo4.h");
182 TEST(libclang
, VirtualFileOverlay_CaseInsensitive
) {
183 const char *contents
=
186 " 'case-sensitive': 'false',\n"
189 " 'type': 'directory',\n"
190 " 'name': \"/path/virtual\",\n"
194 " 'name': \"foo.h\",\n"
195 " 'external-contents': \"/real/foo.h\"\n"
202 T
.map("/path/virtual/foo.h", "/real/foo.h");
203 clang_VirtualFileOverlay_setCaseSensitivity(T
.VFO
, false);
206 TEST(libclang
, VirtualFileOverlay_SharedPrefix
) {
207 const char *contents
=
212 " 'type': 'directory',\n"
213 " 'name': \"/path/foo\",\n"
217 " 'name': \"bar\",\n"
218 " 'external-contents': \"/real/bar\"\n"
222 " 'name': \"bar.h\",\n"
223 " 'external-contents': \"/real/bar.h\"\n"
228 " 'type': 'directory',\n"
229 " 'name': \"/path/foobar\",\n"
233 " 'name': \"baz.h\",\n"
234 " 'external-contents': \"/real/baz.h\"\n"
239 " 'type': 'directory',\n"
240 " 'name': \"/path\",\n"
244 " 'name': \"foobarbaz.h\",\n"
245 " 'external-contents': \"/real/foobarbaz.h\"\n"
252 T
.map("/path/foo/bar.h", "/real/bar.h");
253 T
.map("/path/foo/bar", "/real/bar");
254 T
.map("/path/foobar/baz.h", "/real/baz.h");
255 T
.map("/path/foobarbaz.h", "/real/foobarbaz.h");
258 TEST(libclang
, VirtualFileOverlay_AdjacentDirectory
) {
259 const char *contents
=
264 " 'type': 'directory',\n"
265 " 'name': \"/path/dir1\",\n"
269 " 'name': \"foo.h\",\n"
270 " 'external-contents': \"/real/foo.h\"\n"
273 " 'type': 'directory',\n"
274 " 'name': \"subdir\",\n"
278 " 'name': \"bar.h\",\n"
279 " 'external-contents': \"/real/bar.h\"\n"
286 " 'type': 'directory',\n"
287 " 'name': \"/path/dir2\",\n"
291 " 'name': \"baz.h\",\n"
292 " 'external-contents': \"/real/baz.h\"\n"
299 T
.map("/path/dir1/foo.h", "/real/foo.h");
300 T
.map("/path/dir1/subdir/bar.h", "/real/bar.h");
301 T
.map("/path/dir2/baz.h", "/real/baz.h");
304 TEST(libclang
, VirtualFileOverlay_TopLevel
) {
305 const char *contents
=
310 " 'type': 'directory',\n"
315 " 'name': \"foo.h\",\n"
316 " 'external-contents': \"/real/foo.h\"\n"
323 T
.map("/foo.h", "/real/foo.h");
326 TEST(libclang
, VirtualFileOverlay_Empty
) {
327 const char *contents
=
336 TEST(libclang
, ModuleMapDescriptor
) {
337 const char *Contents
=
338 "framework module TestFrame {\n"
339 " umbrella header \"TestFrame.h\"\n"
342 " module * { export * }\n"
345 CXModuleMapDescriptor MMD
= clang_ModuleMapDescriptor_create(0);
347 clang_ModuleMapDescriptor_setFrameworkModuleName(MMD
, "TestFrame");
348 clang_ModuleMapDescriptor_setUmbrellaHeader(MMD
, "TestFrame.h");
352 clang_ModuleMapDescriptor_writeToBuffer(MMD
, 0, &BufPtr
, &BufSize
);
353 std::string
BufStr(BufPtr
, BufSize
);
354 EXPECT_STREQ(Contents
, BufStr
.c_str());
356 clang_ModuleMapDescriptor_dispose(MMD
);
359 TEST_F(LibclangParseTest
, GlobalOptions
) {
360 EXPECT_EQ(clang_CXIndex_getGlobalOptions(Index
), CXGlobalOpt_None
);
363 class LibclangIndexOptionsTest
: public LibclangParseTest
{
364 virtual void AdjustOptions(CXIndexOptions
&Opts
) {}
367 void CreateIndex() override
{
369 memset(&Opts
, 0, sizeof(Opts
));
370 Opts
.Size
= sizeof(CXIndexOptions
);
372 Index
= clang_createIndexWithOptions(&Opts
);
377 TEST_F(LibclangIndexOptionsTest
, GlobalOptions
) {
378 EXPECT_EQ(clang_CXIndex_getGlobalOptions(Index
), CXGlobalOpt_None
);
381 class LibclangIndexingEnabledIndexOptionsTest
382 : public LibclangIndexOptionsTest
{
383 void AdjustOptions(CXIndexOptions
&Opts
) override
{
384 Opts
.ThreadBackgroundPriorityForIndexing
= CXChoice_Enabled
;
388 TEST_F(LibclangIndexingEnabledIndexOptionsTest
, GlobalOptions
) {
389 EXPECT_EQ(clang_CXIndex_getGlobalOptions(Index
),
390 CXGlobalOpt_ThreadBackgroundPriorityForIndexing
);
393 class LibclangIndexingDisabledEditingEnabledIndexOptionsTest
394 : public LibclangIndexOptionsTest
{
395 void AdjustOptions(CXIndexOptions
&Opts
) override
{
396 Opts
.ThreadBackgroundPriorityForIndexing
= CXChoice_Disabled
;
397 Opts
.ThreadBackgroundPriorityForEditing
= CXChoice_Enabled
;
401 TEST_F(LibclangIndexingDisabledEditingEnabledIndexOptionsTest
, GlobalOptions
) {
402 EXPECT_EQ(clang_CXIndex_getGlobalOptions(Index
),
403 CXGlobalOpt_ThreadBackgroundPriorityForEditing
);
406 class LibclangBothEnabledIndexOptionsTest
: public LibclangIndexOptionsTest
{
407 void AdjustOptions(CXIndexOptions
&Opts
) override
{
408 Opts
.ThreadBackgroundPriorityForIndexing
= CXChoice_Enabled
;
409 Opts
.ThreadBackgroundPriorityForEditing
= CXChoice_Enabled
;
413 TEST_F(LibclangBothEnabledIndexOptionsTest
, GlobalOptions
) {
414 EXPECT_EQ(clang_CXIndex_getGlobalOptions(Index
),
415 CXGlobalOpt_ThreadBackgroundPriorityForAll
);
418 class LibclangPreambleStorageTest
: public LibclangParseTest
{
419 std::string Main
= "main.cpp";
422 std::string PreambleDir
;
423 void InitializePreambleDir() {
424 llvm::SmallString
<128> PathBuffer(TestDir
);
425 llvm::sys::path::append(PathBuffer
, "preambles");
426 namespace fs
= llvm::sys::fs
;
427 ASSERT_FALSE(fs::create_directory(PathBuffer
, false, fs::perms::owner_all
));
429 PreambleDir
= static_cast<std::string
>(PathBuffer
);
430 FilesAndDirsToRemove
.insert(PreambleDir
);
434 void CountPreamblesInPreambleDir(int PreambleCount
) {
435 // For some reason, the preamble is not created without '\n' before `int`.
436 WriteFile(Main
, "\nint main() {}");
438 TUFlags
|= CXTranslationUnit_CreatePreambleOnFirstParse
;
439 ClangTU
= clang_parseTranslationUnit(Index
, Main
.c_str(), nullptr, 0,
440 nullptr, 0, TUFlags
);
444 namespace fs
= llvm::sys::fs
;
446 for (fs::directory_iterator
File(PreambleDir
, EC
), FileEnd
;
447 File
!= FileEnd
&& !EC
; File
.increment(EC
)) {
450 EXPECT_EQ(File
->type(), fs::file_type::regular_file
);
452 const auto Filename
= llvm::sys::path::filename(File
->path());
453 EXPECT_EQ(Filename
.size(), std::strlen("preamble-%%%%%%.pch"));
454 EXPECT_TRUE(Filename
.startswith("preamble-"));
455 EXPECT_TRUE(Filename
.endswith(".pch"));
457 const auto Status
= File
->status();
460 // The permissions assertion below fails, because the .pch.tmp file is
461 // created with default permissions and replaces the .pch file along
462 // with its permissions. Therefore the permissions set in
463 // TempPCHFile::create() don't matter in the end.
464 EXPECT_EQ(Status
->permissions(), fs::owner_read
| fs::owner_write
);
468 EXPECT_EQ(FileCount
, PreambleCount
);
472 class LibclangNotOverriddenPreambleStoragePathTest
473 : public LibclangPreambleStorageTest
{
475 void CreateIndex() override
{
476 InitializePreambleDir();
477 LibclangPreambleStorageTest::CreateIndex();
481 class LibclangSetPreambleStoragePathTest
: public LibclangPreambleStorageTest
{
482 virtual bool StorePreamblesInMemory() { return false; }
483 virtual const char *PreambleStoragePath() = 0;
486 void CreateIndex() override
{
487 InitializePreambleDir();
489 CXIndexOptions Opts
{};
490 Opts
.Size
= sizeof(CXIndexOptions
);
491 Opts
.StorePreamblesInMemory
= StorePreamblesInMemory();
492 Opts
.PreambleStoragePath
= PreambleStoragePath();
493 Index
= clang_createIndexWithOptions(&Opts
);
498 class LibclangNullPreambleStoragePathTest
499 : public LibclangSetPreambleStoragePathTest
{
500 const char *PreambleStoragePath() override
{ return nullptr; }
502 class LibclangEmptyPreambleStoragePathTest
503 : public LibclangSetPreambleStoragePathTest
{
504 const char *PreambleStoragePath() override
{ return ""; }
506 class LibclangPreambleDirPreambleStoragePathTest
507 : public LibclangSetPreambleStoragePathTest
{
508 const char *PreambleStoragePath() override
{ return PreambleDir
.c_str(); }
511 class LibclangStoreInMemoryNullPreambleStoragePathTest
512 : public LibclangNullPreambleStoragePathTest
{
513 bool StorePreamblesInMemory() override
{ return true; }
515 class LibclangStoreInMemoryEmptyPreambleStoragePathTest
516 : public LibclangEmptyPreambleStoragePathTest
{
517 bool StorePreamblesInMemory() override
{ return true; }
519 class LibclangStoreInMemoryPreambleDirPreambleStoragePathTest
520 : public LibclangPreambleDirPreambleStoragePathTest
{
521 bool StorePreamblesInMemory() override
{ return true; }
524 TEST_F(LibclangNotOverriddenPreambleStoragePathTest
, CountPreambles
) {
525 CountPreamblesInPreambleDir(0);
527 TEST_F(LibclangNullPreambleStoragePathTest
, CountPreambles
) {
528 CountPreamblesInPreambleDir(0);
530 TEST_F(LibclangEmptyPreambleStoragePathTest
, CountPreambles
) {
531 CountPreamblesInPreambleDir(0);
533 TEST_F(LibclangPreambleDirPreambleStoragePathTest
, CountPreambles
) {
534 CountPreamblesInPreambleDir(1);
536 TEST_F(LibclangStoreInMemoryNullPreambleStoragePathTest
, CountPreambles
) {
537 CountPreamblesInPreambleDir(0);
539 TEST_F(LibclangStoreInMemoryEmptyPreambleStoragePathTest
, CountPreambles
) {
540 CountPreamblesInPreambleDir(0);
542 TEST_F(LibclangStoreInMemoryPreambleDirPreambleStoragePathTest
,
544 CountPreamblesInPreambleDir(0);
547 TEST_F(LibclangParseTest
, AllSkippedRanges
) {
548 std::string Header
= "header.h", Main
= "main.cpp";
554 "#include \"header.h\"\n"
556 "printf(\"mmm!!\");\n"
559 ClangTU
= clang_parseTranslationUnit(Index
, Main
.c_str(), nullptr, 0,
560 nullptr, 0, TUFlags
);
562 CXSourceRangeList
*Ranges
= clang_getAllSkippedRanges(ClangTU
);
563 EXPECT_EQ(2U, Ranges
->count
);
565 CXSourceLocation cxl
;
567 cxl
= clang_getRangeStart(Ranges
->ranges
[0]);
568 clang_getSpellingLocation(cxl
, nullptr, &line
, nullptr, nullptr);
570 cxl
= clang_getRangeEnd(Ranges
->ranges
[0]);
571 clang_getSpellingLocation(cxl
, nullptr, &line
, nullptr, nullptr);
574 cxl
= clang_getRangeStart(Ranges
->ranges
[1]);
575 clang_getSpellingLocation(cxl
, nullptr, &line
, nullptr, nullptr);
577 cxl
= clang_getRangeEnd(Ranges
->ranges
[1]);
578 clang_getSpellingLocation(cxl
, nullptr, &line
, nullptr, nullptr);
581 clang_disposeSourceRangeList(Ranges
);
584 TEST_F(LibclangParseTest
, EvaluateChildExpression
) {
585 std::string Main
= "main.m";
586 WriteFile(Main
, "#define kFOO @\"foo\"\n"
587 "void foobar(void) {\n"
590 ClangTU
= clang_parseTranslationUnit(Index
, Main
.c_str(), nullptr, 0, nullptr,
593 CXCursor C
= clang_getTranslationUnitCursor(ClangTU
);
596 [](CXCursor cursor
, CXCursor parent
,
597 CXClientData client_data
) -> CXChildVisitResult
{
598 if (clang_getCursorKind(cursor
) == CXCursor_FunctionDecl
) {
599 int numberedStmt
= 0;
602 [](CXCursor cursor
, CXCursor parent
,
603 CXClientData client_data
) -> CXChildVisitResult
{
604 int &numberedStmt
= *((int *)client_data
);
605 if (clang_getCursorKind(cursor
) == CXCursor_CompoundStmt
) {
607 CXEvalResult RE
= clang_Cursor_Evaluate(cursor
);
608 EXPECT_NE(RE
, nullptr);
609 EXPECT_EQ(clang_EvalResult_getKind(RE
),
610 CXEval_ObjCStrLiteral
);
611 clang_EvalResult_dispose(RE
);
612 return CXChildVisit_Break
;
616 return CXChildVisit_Recurse
;
619 EXPECT_EQ(numberedStmt
, 1);
621 return CXChildVisit_Continue
;
626 class LibclangReparseTest
: public LibclangParseTest
{
628 void DisplayDiagnostics() {
629 unsigned NumDiagnostics
= clang_getNumDiagnostics(ClangTU
);
630 for (unsigned i
= 0; i
< NumDiagnostics
; ++i
) {
631 auto Diag
= clang_getDiagnostic(ClangTU
, i
);
632 LLVM_DEBUG(llvm::dbgs()
633 << clang_getCString(clang_formatDiagnostic(
634 Diag
, clang_defaultDiagnosticDisplayOptions()))
636 clang_disposeDiagnostic(Diag
);
639 bool ReparseTU(unsigned num_unsaved_files
, CXUnsavedFile
* unsaved_files
) {
640 if (clang_reparseTranslationUnit(ClangTU
, num_unsaved_files
, unsaved_files
,
641 clang_defaultReparseOptions(ClangTU
))) {
642 LLVM_DEBUG(llvm::dbgs() << "Reparse failed\n");
645 DisplayDiagnostics();
650 TEST_F(LibclangReparseTest
, FileName
) {
651 std::string CppName
= "main.cpp";
652 WriteFile(CppName
, "int main() {}");
653 ClangTU
= clang_parseTranslationUnit(Index
, CppName
.c_str(), nullptr, 0,
654 nullptr, 0, TUFlags
);
655 CXFile cxf
= clang_getFile(ClangTU
, CppName
.c_str());
657 CXString cxname
= clang_getFileName(cxf
);
658 ASSERT_STREQ(clang_getCString(cxname
), CppName
.c_str());
659 clang_disposeString(cxname
);
661 cxname
= clang_File_tryGetRealPathName(cxf
);
662 ASSERT_TRUE(llvm::StringRef(clang_getCString(cxname
)).endswith("main.cpp"));
663 clang_disposeString(cxname
);
666 TEST_F(LibclangReparseTest
, Reparse
) {
667 const char *HeaderTop
= "#ifndef H\n#define H\nstruct Foo { int bar;";
668 const char *HeaderBottom
= "\n};\n#endif\n";
669 const char *CppFile
= "#include \"HeaderFile.h\"\nint main() {"
670 " Foo foo; foo.bar = 7; foo.baz = 8; }\n";
671 std::string HeaderName
= "HeaderFile.h";
672 std::string CppName
= "CppFile.cpp";
673 WriteFile(CppName
, CppFile
);
674 WriteFile(HeaderName
, std::string(HeaderTop
) + HeaderBottom
);
676 ClangTU
= clang_parseTranslationUnit(Index
, CppName
.c_str(), nullptr, 0,
677 nullptr, 0, TUFlags
);
678 EXPECT_EQ(1U, clang_getNumDiagnostics(ClangTU
));
679 DisplayDiagnostics();
681 // Immedaitely reparse.
682 ASSERT_TRUE(ReparseTU(0, nullptr /* No unsaved files. */));
683 EXPECT_EQ(1U, clang_getNumDiagnostics(ClangTU
));
685 std::string NewHeaderContents
=
686 std::string(HeaderTop
) + "int baz;" + HeaderBottom
;
687 WriteFile(HeaderName
, NewHeaderContents
);
689 // Reparse after fix.
690 ASSERT_TRUE(ReparseTU(0, nullptr /* No unsaved files. */));
691 EXPECT_EQ(0U, clang_getNumDiagnostics(ClangTU
));
694 TEST_F(LibclangReparseTest
, ReparseWithModule
) {
695 const char *HeaderTop
= "#ifndef H\n#define H\nstruct Foo { int bar;";
696 const char *HeaderBottom
= "\n};\n#endif\n";
697 const char *MFile
= "#include \"HeaderFile.h\"\nint main() {"
698 " struct Foo foo; foo.bar = 7; foo.baz = 8; }\n";
699 const char *ModFile
= "module A { header \"HeaderFile.h\" }\n";
700 std::string HeaderName
= "HeaderFile.h";
701 std::string MName
= "MFile.m";
702 std::string ModName
= "module.modulemap";
703 WriteFile(MName
, MFile
);
704 WriteFile(HeaderName
, std::string(HeaderTop
) + HeaderBottom
);
705 WriteFile(ModName
, ModFile
);
707 // Removing recursively is necessary to delete the module cache.
708 RemoveTestDirRecursivelyDuringTeardown
= true;
709 std::string ModulesCache
= std::string("-fmodules-cache-path=") + TestDir
;
710 const char *Args
[] = { "-fmodules", ModulesCache
.c_str(),
711 "-I", TestDir
.c_str() };
712 int NumArgs
= std::size(Args
);
713 ClangTU
= clang_parseTranslationUnit(Index
, MName
.c_str(), Args
, NumArgs
,
714 nullptr, 0, TUFlags
);
715 EXPECT_EQ(1U, clang_getNumDiagnostics(ClangTU
));
716 DisplayDiagnostics();
718 // Immedaitely reparse.
719 ASSERT_TRUE(ReparseTU(0, nullptr /* No unsaved files. */));
720 EXPECT_EQ(1U, clang_getNumDiagnostics(ClangTU
));
722 std::string NewHeaderContents
=
723 std::string(HeaderTop
) + "int baz;" + HeaderBottom
;
724 WriteFile(HeaderName
, NewHeaderContents
);
726 // Reparse after fix.
727 ASSERT_TRUE(ReparseTU(0, nullptr /* No unsaved files. */));
728 EXPECT_EQ(0U, clang_getNumDiagnostics(ClangTU
));
731 TEST_F(LibclangReparseTest
, clang_parseTranslationUnit2FullArgv
) {
732 // Provide a fake GCC 99.9.9 standard library that always overrides any local
734 std::string EmptyFiles
[] = {"lib/gcc/arm-linux-gnueabi/99.9.9/crtbegin.o",
735 "include/arm-linux-gnueabi/.keep",
736 "include/c++/99.9.9/vector"};
738 for (auto &Name
: EmptyFiles
)
739 WriteFile(Name
, "\n");
741 std::string Filename
= "test.cc";
742 WriteFile(Filename
, "#include <vector>\n");
744 std::string Clang
= "bin/clang";
745 WriteFile(Clang
, "");
747 const char *Argv
[] = {Clang
.c_str(), "-target", "arm-linux-gnueabi",
748 "-stdlib=libstdc++", "--gcc-toolchain="};
750 EXPECT_EQ(CXError_Success
,
751 clang_parseTranslationUnit2FullArgv(Index
, Filename
.c_str(), Argv
,
753 nullptr, 0, TUFlags
, &ClangTU
));
754 EXPECT_EQ(0U, clang_getNumDiagnostics(ClangTU
));
755 DisplayDiagnostics();
758 class LibclangPrintingPolicyTest
: public LibclangParseTest
{
760 CXPrintingPolicy Policy
= nullptr;
762 void SetUp() override
{
763 LibclangParseTest::SetUp();
764 std::string File
= "file.cpp";
765 WriteFile(File
, "int i;\n");
766 ClangTU
= clang_parseTranslationUnit(Index
, File
.c_str(), nullptr, 0,
767 nullptr, 0, TUFlags
);
768 CXCursor TUCursor
= clang_getTranslationUnitCursor(ClangTU
);
769 Policy
= clang_getCursorPrintingPolicy(TUCursor
);
771 void TearDown() override
{
772 clang_PrintingPolicy_dispose(Policy
);
773 LibclangParseTest::TearDown();
777 TEST_F(LibclangPrintingPolicyTest
, SetAndGetProperties
) {
778 for (unsigned Value
= 0; Value
< 2; ++Value
) {
779 for (int I
= 0; I
< CXPrintingPolicy_LastProperty
; ++I
) {
780 auto Property
= static_cast<enum CXPrintingPolicyProperty
>(I
);
782 clang_PrintingPolicy_setProperty(Policy
, Property
, Value
);
783 EXPECT_EQ(Value
, clang_PrintingPolicy_getProperty(Policy
, Property
));
788 TEST_F(LibclangReparseTest
, PreprocessorSkippedRanges
) {
789 std::string Header
= "header.h", Main
= "main.cpp";
795 "#include \"header.h\"\n"
799 "printf(\"mmm!!\");\n"
802 for (int i
= 0; i
!= 3; ++i
) {
803 unsigned flags
= TUFlags
| CXTranslationUnit_PrecompiledPreamble
;
805 flags
|= CXTranslationUnit_CreatePreambleOnFirstParse
;
808 clang_disposeTranslationUnit(ClangTU
); // dispose from previous iter
811 ClangTU
= clang_parseTranslationUnit(Index
, Main
.c_str(), nullptr, 0,
815 ASSERT_TRUE(ReparseTU(0, nullptr /* No unsaved files. */));
818 // Check all ranges are there
819 CXSourceRangeList
*Ranges
= clang_getAllSkippedRanges(ClangTU
);
820 EXPECT_EQ(3U, Ranges
->count
);
822 CXSourceLocation cxl
;
824 cxl
= clang_getRangeStart(Ranges
->ranges
[0]);
825 clang_getSpellingLocation(cxl
, nullptr, &line
, nullptr, nullptr);
827 cxl
= clang_getRangeEnd(Ranges
->ranges
[0]);
828 clang_getSpellingLocation(cxl
, nullptr, &line
, nullptr, nullptr);
831 cxl
= clang_getRangeStart(Ranges
->ranges
[1]);
832 clang_getSpellingLocation(cxl
, nullptr, &line
, nullptr, nullptr);
834 cxl
= clang_getRangeEnd(Ranges
->ranges
[1]);
835 clang_getSpellingLocation(cxl
, nullptr, &line
, nullptr, nullptr);
838 cxl
= clang_getRangeStart(Ranges
->ranges
[2]);
839 clang_getSpellingLocation(cxl
, nullptr, &line
, nullptr, nullptr);
841 cxl
= clang_getRangeEnd(Ranges
->ranges
[2]);
842 clang_getSpellingLocation(cxl
, nullptr, &line
, nullptr, nullptr);
845 clang_disposeSourceRangeList(Ranges
);
847 // Check obtaining ranges by each file works
848 CXFile cxf
= clang_getFile(ClangTU
, Header
.c_str());
849 Ranges
= clang_getSkippedRanges(ClangTU
, cxf
);
850 EXPECT_EQ(1U, Ranges
->count
);
851 cxl
= clang_getRangeStart(Ranges
->ranges
[0]);
852 clang_getSpellingLocation(cxl
, nullptr, &line
, nullptr, nullptr);
854 clang_disposeSourceRangeList(Ranges
);
856 cxf
= clang_getFile(ClangTU
, Main
.c_str());
857 Ranges
= clang_getSkippedRanges(ClangTU
, cxf
);
858 EXPECT_EQ(2U, Ranges
->count
);
859 cxl
= clang_getRangeStart(Ranges
->ranges
[0]);
860 clang_getSpellingLocation(cxl
, nullptr, &line
, nullptr, nullptr);
862 cxl
= clang_getRangeStart(Ranges
->ranges
[1]);
863 clang_getSpellingLocation(cxl
, nullptr, &line
, nullptr, nullptr);
865 clang_disposeSourceRangeList(Ranges
);
869 class LibclangSerializationTest
: public LibclangParseTest
{
871 bool SaveAndLoadTU(const std::string
&Filename
) {
872 unsigned options
= clang_defaultSaveOptions(ClangTU
);
873 if (clang_saveTranslationUnit(ClangTU
, Filename
.c_str(), options
) !=
875 LLVM_DEBUG(llvm::dbgs() << "Saving failed\n");
879 clang_disposeTranslationUnit(ClangTU
);
881 ClangTU
= clang_createTranslationUnit(Index
, Filename
.c_str());
884 LLVM_DEBUG(llvm::dbgs() << "Loading failed\n");
892 TEST_F(LibclangSerializationTest
, TokenKindsAreCorrectAfterLoading
) {
893 // Ensure that "class" is recognized as a keyword token after serializing
894 // and reloading the AST, as it is not a keyword for the default LangOptions.
895 std::string HeaderName
= "test.h";
896 WriteFile(HeaderName
, "enum class Something {};");
898 const char *Argv
[] = {"-xc++-header", "-std=c++11"};
900 ClangTU
= clang_parseTranslationUnit(Index
, HeaderName
.c_str(), Argv
,
901 std::size(Argv
), nullptr,
904 auto CheckTokenKinds
= [=]() {
905 CXSourceRange Range
=
906 clang_getCursorExtent(clang_getTranslationUnitCursor(ClangTU
));
909 unsigned int NumTokens
;
910 clang_tokenize(ClangTU
, Range
, &Tokens
, &NumTokens
);
912 ASSERT_EQ(6u, NumTokens
);
913 EXPECT_EQ(CXToken_Keyword
, clang_getTokenKind(Tokens
[0]));
914 EXPECT_EQ(CXToken_Keyword
, clang_getTokenKind(Tokens
[1]));
915 EXPECT_EQ(CXToken_Identifier
, clang_getTokenKind(Tokens
[2]));
916 EXPECT_EQ(CXToken_Punctuation
, clang_getTokenKind(Tokens
[3]));
917 EXPECT_EQ(CXToken_Punctuation
, clang_getTokenKind(Tokens
[4]));
918 EXPECT_EQ(CXToken_Punctuation
, clang_getTokenKind(Tokens
[5]));
920 clang_disposeTokens(ClangTU
, Tokens
, NumTokens
);
925 std::string ASTName
= "test.ast";
926 WriteFile(ASTName
, "");
928 ASSERT_TRUE(SaveAndLoadTU(ASTName
));
933 TEST_F(LibclangParseTest
, clang_getVarDeclInitializer
) {
934 std::string Main
= "main.cpp";
935 WriteFile(Main
, "int foo() { return 5; }; const int a = foo();");
936 ClangTU
= clang_parseTranslationUnit(Index
, Main
.c_str(), nullptr, 0, nullptr,
939 CXCursor C
= clang_getTranslationUnitCursor(ClangTU
);
942 [](CXCursor cursor
, CXCursor parent
,
943 CXClientData client_data
) -> CXChildVisitResult
{
944 if (clang_getCursorKind(cursor
) == CXCursor_VarDecl
) {
945 const CXCursor Initializer
= clang_Cursor_getVarDeclInitializer(cursor
);
946 EXPECT_FALSE(clang_Cursor_isNull(Initializer
));
947 CXString Spelling
= clang_getCursorSpelling(Initializer
);
948 const char* const SpellingCSstr
= clang_getCString(Spelling
);
949 EXPECT_TRUE(SpellingCSstr
);
950 EXPECT_EQ(std::string(SpellingCSstr
), std::string("foo"));
951 clang_disposeString(Spelling
);
952 return CXChildVisit_Break
;
954 return CXChildVisit_Continue
;
959 TEST_F(LibclangParseTest
, clang_hasVarDeclGlobalStorageFalse
) {
960 std::string Main
= "main.cpp";
961 WriteFile(Main
, "void foo() { int a; }");
962 ClangTU
= clang_parseTranslationUnit(Index
, Main
.c_str(), nullptr, 0, nullptr,
965 CXCursor C
= clang_getTranslationUnitCursor(ClangTU
);
968 [](CXCursor cursor
, CXCursor parent
,
969 CXClientData client_data
) -> CXChildVisitResult
{
970 if (clang_getCursorKind(cursor
) == CXCursor_VarDecl
) {
971 EXPECT_FALSE(clang_Cursor_hasVarDeclGlobalStorage(cursor
));
972 return CXChildVisit_Break
;
974 return CXChildVisit_Continue
;
979 TEST_F(LibclangParseTest
, clang_Cursor_hasVarDeclGlobalStorageTrue
) {
980 std::string Main
= "main.cpp";
981 WriteFile(Main
, "int a;");
982 ClangTU
= clang_parseTranslationUnit(Index
, Main
.c_str(), nullptr, 0, nullptr,
985 CXCursor C
= clang_getTranslationUnitCursor(ClangTU
);
988 [](CXCursor cursor
, CXCursor parent
,
989 CXClientData client_data
) -> CXChildVisitResult
{
990 if (clang_getCursorKind(cursor
) == CXCursor_VarDecl
) {
991 EXPECT_TRUE(clang_Cursor_hasVarDeclGlobalStorage(cursor
));
992 return CXChildVisit_Break
;
994 return CXChildVisit_Continue
;
999 TEST_F(LibclangParseTest
, clang_Cursor_hasVarDeclExternalStorageFalse
) {
1000 std::string Main
= "main.cpp";
1001 WriteFile(Main
, "int a;");
1002 ClangTU
= clang_parseTranslationUnit(Index
, Main
.c_str(), nullptr, 0, nullptr,
1005 CXCursor C
= clang_getTranslationUnitCursor(ClangTU
);
1006 clang_visitChildren(
1008 [](CXCursor cursor
, CXCursor parent
,
1009 CXClientData client_data
) -> CXChildVisitResult
{
1010 if (clang_getCursorKind(cursor
) == CXCursor_VarDecl
) {
1011 EXPECT_FALSE(clang_Cursor_hasVarDeclExternalStorage(cursor
));
1012 return CXChildVisit_Break
;
1014 return CXChildVisit_Continue
;
1019 TEST_F(LibclangParseTest
, clang_Cursor_hasVarDeclExternalStorageTrue
) {
1020 std::string Main
= "main.cpp";
1021 WriteFile(Main
, "extern int a;");
1022 ClangTU
= clang_parseTranslationUnit(Index
, Main
.c_str(), nullptr, 0, nullptr,
1025 CXCursor C
= clang_getTranslationUnitCursor(ClangTU
);
1026 clang_visitChildren(
1028 [](CXCursor cursor
, CXCursor parent
,
1029 CXClientData client_data
) -> CXChildVisitResult
{
1030 if (clang_getCursorKind(cursor
) == CXCursor_VarDecl
) {
1031 EXPECT_TRUE(clang_Cursor_hasVarDeclExternalStorage(cursor
));
1032 return CXChildVisit_Break
;
1034 return CXChildVisit_Continue
;
1039 TEST_F(LibclangParseTest
, clang_getUnqualifiedTypeRemovesQualifiers
) {
1040 std::string Header
= "header.h";
1041 WriteFile(Header
, "void foo1(const int);\n"
1042 "void foo2(volatile int);\n"
1043 "void foo3(const volatile int);\n"
1044 "void foo4(int* const);\n"
1045 "void foo5(int* volatile);\n"
1046 "void foo6(int* restrict);\n"
1047 "void foo7(int* const volatile);\n"
1048 "void foo8(int* volatile restrict);\n"
1049 "void foo9(int* const restrict);\n"
1050 "void foo10(int* const volatile restrict);\n");
1052 auto is_qualified
= [](CXType type
) -> bool {
1053 return clang_isConstQualifiedType(type
) ||
1054 clang_isVolatileQualifiedType(type
) ||
1055 clang_isRestrictQualifiedType(type
);
1058 ClangTU
= clang_parseTranslationUnit(Index
, Header
.c_str(), nullptr, 0,
1059 nullptr, 0, TUFlags
);
1061 Traverse([&is_qualified
](CXCursor cursor
, CXCursor
) {
1062 if (clang_getCursorKind(cursor
) == CXCursor_FunctionDecl
) {
1063 CXType arg_type
= clang_getArgType(clang_getCursorType(cursor
), 0);
1064 EXPECT_TRUE(is_qualified(arg_type
))
1065 << "Input data '" << fromCXString(clang_getCursorSpelling(cursor
))
1066 << "' first argument does not have a qualified type.";
1068 CXType unqualified_arg_type
= clang_getUnqualifiedType(arg_type
);
1069 EXPECT_FALSE(is_qualified(unqualified_arg_type
))
1070 << "The type '" << fromCXString(clang_getTypeSpelling(arg_type
))
1071 << "' was not unqualified after a call to clang_getUnqualifiedType.";
1074 return CXChildVisit_Continue
;
1078 TEST_F(LibclangParseTest
, clang_getNonReferenceTypeRemovesRefQualifiers
) {
1079 std::string Header
= "header.h";
1080 WriteFile(Header
, "void foo1(int&);\n"
1081 "void foo2(int&&);\n");
1083 auto is_ref_qualified
= [](CXType type
) -> bool {
1084 return (type
.kind
== CXType_LValueReference
) ||
1085 (type
.kind
== CXType_RValueReference
);
1088 const char *Args
[] = {"-xc++"};
1089 ClangTU
= clang_parseTranslationUnit(Index
, Header
.c_str(), Args
, 1, nullptr,
1092 Traverse([&is_ref_qualified
](CXCursor cursor
, CXCursor
) {
1093 if (clang_getCursorKind(cursor
) == CXCursor_FunctionDecl
) {
1094 CXType arg_type
= clang_getArgType(clang_getCursorType(cursor
), 0);
1095 EXPECT_TRUE(is_ref_qualified(arg_type
))
1096 << "Input data '" << fromCXString(clang_getCursorSpelling(cursor
))
1097 << "' first argument does not have a ref-qualified type.";
1099 CXType non_reference_arg_type
= clang_getNonReferenceType(arg_type
);
1100 EXPECT_FALSE(is_ref_qualified(non_reference_arg_type
))
1101 << "The type '" << fromCXString(clang_getTypeSpelling(arg_type
))
1102 << "' ref-qualifier was not removed after a call to "
1103 "clang_getNonReferenceType.";
1106 return CXChildVisit_Continue
;
1110 TEST_F(LibclangParseTest
, VisitUsingTypeLoc
) {
1111 const char testSource
[] = R
"cpp(
1121 void Class1::fun() {}
1123 std::string fileName
= "main.cpp";
1124 WriteFile(fileName
, testSource
);
1125 const char *Args
[] = {"-xc++"};
1126 ClangTU
= clang_parseTranslationUnit(Index
, fileName
.c_str(), Args
, 1,
1127 nullptr, 0, TUFlags
);
1129 std::optional
<CXCursor
> typeRefCsr
;
1130 Traverse([&](CXCursor cursor
, CXCursor parent
) -> CXChildVisitResult
{
1131 if (cursor
.kind
== CXCursor_TypeRef
) {
1132 typeRefCsr
.emplace(cursor
);
1134 return CXChildVisit_Recurse
;
1136 ASSERT_TRUE(typeRefCsr
.has_value());
1137 EXPECT_EQ(fromCXString(clang_getCursorSpelling(*typeRefCsr
)),
1138 "class ns1::Class1");
1141 TEST_F(LibclangParseTest
, BinaryOperator
) {
1142 std::string Main
= "main.cpp";
1143 WriteFile(Main
, "int foo() { return 5 + 9; }");
1144 ClangTU
= clang_parseTranslationUnit(Index
, Main
.c_str(), nullptr, 0, nullptr,
1147 Traverse([](CXCursor cursor
, CXCursor parent
) -> CXChildVisitResult
{
1148 if (cursor
.kind
== CXCursor_BinaryOperator
) {
1149 EXPECT_EQ(clang_getCursorBinaryOperatorKind(cursor
),
1150 CXBinaryOperator_Add
);
1151 return CXChildVisit_Break
;
1154 return CXChildVisit_Recurse
;
1158 TEST_F(LibclangParseTest
, UnaryOperator
) {
1159 std::string Main
= "main.cpp";
1160 WriteFile(Main
, "int foo() { int a = 5; return a++; }");
1161 ClangTU
= clang_parseTranslationUnit(Index
, Main
.c_str(), nullptr, 0, nullptr,
1164 Traverse([](CXCursor cursor
, CXCursor parent
) -> CXChildVisitResult
{
1165 if (cursor
.kind
== CXCursor_UnaryOperator
) {
1166 EXPECT_EQ(clang_getCursorUnaryOperatorKind(cursor
),
1167 CXUnaryOperator_PostInc
);
1168 return CXChildVisit_Break
;
1171 return CXChildVisit_Recurse
;
1175 TEST_F(LibclangParseTest
, VisitStaticAssertDecl_noMessage
) {
1176 const char testSource
[] = R
"cpp(static_assert(true))cpp";
1177 std::string fileName
= "main.cpp";
1178 WriteFile(fileName
, testSource
);
1179 const char *Args
[] = {"-xc++"};
1180 ClangTU
= clang_parseTranslationUnit(Index
, fileName
.c_str(), Args
, 1,
1181 nullptr, 0, TUFlags
);
1183 std::optional
<CXCursor
> staticAssertCsr
;
1184 Traverse([&](CXCursor cursor
, CXCursor parent
) -> CXChildVisitResult
{
1185 if (cursor
.kind
== CXCursor_StaticAssert
) {
1186 staticAssertCsr
.emplace(cursor
);
1187 return CXChildVisit_Break
;
1189 return CXChildVisit_Recurse
;
1191 ASSERT_TRUE(staticAssertCsr
.has_value());
1192 Traverse(*staticAssertCsr
, [](CXCursor cursor
, CXCursor parent
) {
1193 EXPECT_EQ(cursor
.kind
, CXCursor_CXXBoolLiteralExpr
);
1194 return CXChildVisit_Break
;
1196 EXPECT_EQ(fromCXString(clang_getCursorSpelling(*staticAssertCsr
)), "");
1199 TEST_F(LibclangParseTest
, VisitStaticAssertDecl_exprMessage
) {
1200 const char testSource
[] = R
"cpp(
1201 template <unsigned s>
1202 constexpr unsigned size(const char (&)[s])
1208 static constexpr char message[] = "Hello World
!";
1209 constexpr const char* data() const { return message;}
1210 constexpr unsigned size() const
1212 return ::size(message);
1216 static_assert(true, message);
1218 std::string fileName
= "main.cpp";
1219 WriteFile(fileName
, testSource
);
1220 const char *Args
[] = {"-xc++", "-std=c++26"};
1221 ClangTU
= clang_parseTranslationUnit(Index
, fileName
.c_str(), Args
,
1222 std::size(Args
), nullptr, 0, TUFlags
);
1223 ASSERT_EQ(clang_getNumDiagnostics(ClangTU
), 0u);
1224 std::optional
<CXCursor
> staticAssertCsr
;
1225 Traverse([&](CXCursor cursor
, CXCursor parent
) -> CXChildVisitResult
{
1226 if (cursor
.kind
== CXCursor_StaticAssert
) {
1227 staticAssertCsr
.emplace(cursor
);
1229 return CXChildVisit_Continue
;
1231 ASSERT_TRUE(staticAssertCsr
.has_value());
1233 Traverse(*staticAssertCsr
, [&argCnt
](CXCursor cursor
, CXCursor parent
) {
1236 EXPECT_EQ(cursor
.kind
, CXCursor_CXXBoolLiteralExpr
);
1239 EXPECT_EQ(cursor
.kind
, CXCursor_DeclRefExpr
);
1243 return CXChildVisit_Continue
;
1245 ASSERT_EQ(argCnt
, 2);
1246 EXPECT_EQ(fromCXString(clang_getCursorSpelling(*staticAssertCsr
)), "");
1249 TEST_F(LibclangParseTest
, ExposesAnnotateArgs
) {
1250 const char testSource
[] = R
"cpp(
1251 [[clang::annotate("category
", 42)]]
1254 std::string fileName
= "main.cpp";
1255 WriteFile(fileName
, testSource
);
1257 const char *Args
[] = {"-xc++"};
1258 ClangTU
= clang_parseTranslationUnit(Index
, fileName
.c_str(), Args
, 1,
1259 nullptr, 0, TUFlags
);
1264 [&attrCount
](CXCursor cursor
, CXCursor parent
) -> CXChildVisitResult
{
1265 if (cursor
.kind
== CXCursor_AnnotateAttr
) {
1267 clang_visitChildren(
1269 [](CXCursor child
, CXCursor
,
1270 CXClientData data
) -> CXChildVisitResult
{
1271 int *pcount
= static_cast<int *>(data
);
1273 // we only expect one argument here, so bail otherwise
1274 EXPECT_EQ(*pcount
, 0);
1276 auto *result
= clang_Cursor_Evaluate(child
);
1277 EXPECT_NE(result
, nullptr);
1278 EXPECT_EQ(clang_EvalResult_getAsInt(result
), 42);
1279 clang_EvalResult_dispose(result
);
1283 return CXChildVisit_Recurse
;
1287 return CXChildVisit_Continue
;
1289 return CXChildVisit_Recurse
;
1292 EXPECT_EQ(attrCount
, 1);
1295 class LibclangRewriteTest
: public LibclangParseTest
{
1297 CXRewriter Rew
= nullptr;
1298 std::string Filename
;
1299 CXFile File
= nullptr;
1301 void SetUp() override
{
1302 LibclangParseTest::SetUp();
1303 Filename
= "file.cpp";
1304 WriteFile(Filename
, "int main() { return 0; }");
1305 ClangTU
= clang_parseTranslationUnit(Index
, Filename
.c_str(), nullptr, 0,
1306 nullptr, 0, TUFlags
);
1307 Rew
= clang_CXRewriter_create(ClangTU
);
1308 File
= clang_getFile(ClangTU
, Filename
.c_str());
1310 void TearDown() override
{
1311 clang_CXRewriter_dispose(Rew
);
1312 LibclangParseTest::TearDown();
1316 static std::string
getFileContent(const std::string
& Filename
) {
1317 std::ifstream
RewrittenFile(Filename
);
1318 std::string RewrittenFileContent
;
1320 while (std::getline(RewrittenFile
, Line
)) {
1321 if (RewrittenFileContent
.empty())
1322 RewrittenFileContent
= Line
;
1324 RewrittenFileContent
+= "\n" + Line
;
1327 return RewrittenFileContent
;
1330 TEST_F(LibclangRewriteTest
, RewriteReplace
) {
1331 CXSourceLocation B
= clang_getLocation(ClangTU
, File
, 1, 5);
1332 CXSourceLocation E
= clang_getLocation(ClangTU
, File
, 1, 9);
1333 CXSourceRange Rng
= clang_getRange(B
, E
);
1335 clang_CXRewriter_replaceText(Rew
, Rng
, "MAIN");
1337 ASSERT_EQ(clang_CXRewriter_overwriteChangedFiles(Rew
), 0);
1338 EXPECT_EQ(getFileContent(Filename
), "int MAIN() { return 0; }");
1341 TEST_F(LibclangRewriteTest
, RewriteReplaceShorter
) {
1342 CXSourceLocation B
= clang_getLocation(ClangTU
, File
, 1, 5);
1343 CXSourceLocation E
= clang_getLocation(ClangTU
, File
, 1, 9);
1344 CXSourceRange Rng
= clang_getRange(B
, E
);
1346 clang_CXRewriter_replaceText(Rew
, Rng
, "foo");
1348 ASSERT_EQ(clang_CXRewriter_overwriteChangedFiles(Rew
), 0);
1349 EXPECT_EQ(getFileContent(Filename
), "int foo() { return 0; }");
1352 TEST_F(LibclangRewriteTest
, RewriteReplaceLonger
) {
1353 CXSourceLocation B
= clang_getLocation(ClangTU
, File
, 1, 5);
1354 CXSourceLocation E
= clang_getLocation(ClangTU
, File
, 1, 9);
1355 CXSourceRange Rng
= clang_getRange(B
, E
);
1357 clang_CXRewriter_replaceText(Rew
, Rng
, "patatino");
1359 ASSERT_EQ(clang_CXRewriter_overwriteChangedFiles(Rew
), 0);
1360 EXPECT_EQ(getFileContent(Filename
), "int patatino() { return 0; }");
1363 TEST_F(LibclangRewriteTest
, RewriteInsert
) {
1364 CXSourceLocation Loc
= clang_getLocation(ClangTU
, File
, 1, 5);
1366 clang_CXRewriter_insertTextBefore(Rew
, Loc
, "ro");
1368 ASSERT_EQ(clang_CXRewriter_overwriteChangedFiles(Rew
), 0);
1369 EXPECT_EQ(getFileContent(Filename
), "int romain() { return 0; }");
1372 TEST_F(LibclangRewriteTest
, RewriteRemove
) {
1373 CXSourceLocation B
= clang_getLocation(ClangTU
, File
, 1, 5);
1374 CXSourceLocation E
= clang_getLocation(ClangTU
, File
, 1, 9);
1375 CXSourceRange Rng
= clang_getRange(B
, E
);
1377 clang_CXRewriter_removeText(Rew
, Rng
);
1379 ASSERT_EQ(clang_CXRewriter_overwriteChangedFiles(Rew
), 0);
1380 EXPECT_EQ(getFileContent(Filename
), "int () { return 0; }");