|
7 | 7 | //===----------------------------------------------------------------------===// |
8 | 8 |
|
9 | 9 | #include "clang-c/Index.h" |
| 10 | +#include "clang-c/Rewrite.h" |
10 | 11 | #include "llvm/ADT/StringRef.h" |
11 | 12 | #include "llvm/Support/Debug.h" |
12 | 13 | #include "llvm/Support/FileSystem.h" |
@@ -842,3 +843,90 @@ TEST_F(LibclangParseTest, clang_Cursor_hasVarDeclExternalStorageTrue) { |
842 | 843 | }, |
843 | 844 | nullptr); |
844 | 845 | } |
| 846 | +class LibclangRewriteTest : public LibclangParseTest { |
| 847 | +public: |
| 848 | + CXRewriter Rew = nullptr; |
| 849 | + std::string Filename; |
| 850 | + CXFile File = nullptr; |
| 851 | + |
| 852 | + void SetUp() override { |
| 853 | + LibclangParseTest::SetUp(); |
| 854 | + Filename = "file.cpp"; |
| 855 | + WriteFile(Filename, "int main() { return 0; }"); |
| 856 | + ClangTU = clang_parseTranslationUnit(Index, Filename.c_str(), nullptr, 0, |
| 857 | + nullptr, 0, TUFlags); |
| 858 | + Rew = clang_CXRewriter_create(ClangTU); |
| 859 | + File = clang_getFile(ClangTU, Filename.c_str()); |
| 860 | + } |
| 861 | + void TearDown() override { |
| 862 | + clang_CXRewriter_dispose(Rew); |
| 863 | + LibclangParseTest::TearDown(); |
| 864 | + } |
| 865 | +}; |
| 866 | + |
| 867 | +static std::string getFileContent(const std::string& Filename) { |
| 868 | + std::ifstream RewrittenFile(Filename); |
| 869 | + std::string RewrittenFileContent; |
| 870 | + std::string Line; |
| 871 | + while (std::getline(RewrittenFile, Line)) { |
| 872 | + if (RewrittenFileContent.empty()) |
| 873 | + RewrittenFileContent = Line; |
| 874 | + else { |
| 875 | + RewrittenFileContent += "\n" + Line; |
| 876 | + } |
| 877 | + } |
| 878 | + return RewrittenFileContent; |
| 879 | +} |
| 880 | + |
| 881 | +TEST_F(LibclangRewriteTest, RewriteReplace) { |
| 882 | + CXSourceLocation B = clang_getLocation(ClangTU, File, 1, 5); |
| 883 | + CXSourceLocation E = clang_getLocation(ClangTU, File, 1, 9); |
| 884 | + CXSourceRange Rng = clang_getRange(B, E); |
| 885 | + |
| 886 | + clang_CXRewriter_replaceText(Rew, Rng, "MAIN"); |
| 887 | + |
| 888 | + ASSERT_EQ(clang_CXRewriter_overwriteChangedFiles(Rew), 0); |
| 889 | + EXPECT_EQ(getFileContent(Filename), "int MAIN() { return 0; }"); |
| 890 | +} |
| 891 | + |
| 892 | +TEST_F(LibclangRewriteTest, RewriteReplaceShorter) { |
| 893 | + CXSourceLocation B = clang_getLocation(ClangTU, File, 1, 5); |
| 894 | + CXSourceLocation E = clang_getLocation(ClangTU, File, 1, 9); |
| 895 | + CXSourceRange Rng = clang_getRange(B, E); |
| 896 | + |
| 897 | + clang_CXRewriter_replaceText(Rew, Rng, "foo"); |
| 898 | + |
| 899 | + ASSERT_EQ(clang_CXRewriter_overwriteChangedFiles(Rew), 0); |
| 900 | + EXPECT_EQ(getFileContent(Filename), "int foo() { return 0; }"); |
| 901 | +} |
| 902 | + |
| 903 | +TEST_F(LibclangRewriteTest, RewriteReplaceLonger) { |
| 904 | + CXSourceLocation B = clang_getLocation(ClangTU, File, 1, 5); |
| 905 | + CXSourceLocation E = clang_getLocation(ClangTU, File, 1, 9); |
| 906 | + CXSourceRange Rng = clang_getRange(B, E); |
| 907 | + |
| 908 | + clang_CXRewriter_replaceText(Rew, Rng, "patatino"); |
| 909 | + |
| 910 | + ASSERT_EQ(clang_CXRewriter_overwriteChangedFiles(Rew), 0); |
| 911 | + EXPECT_EQ(getFileContent(Filename), "int patatino() { return 0; }"); |
| 912 | +} |
| 913 | + |
| 914 | +TEST_F(LibclangRewriteTest, RewriteInsert) { |
| 915 | + CXSourceLocation Loc = clang_getLocation(ClangTU, File, 1, 5); |
| 916 | + |
| 917 | + clang_CXRewriter_insertTextBefore(Rew, Loc, "ro"); |
| 918 | + |
| 919 | + ASSERT_EQ(clang_CXRewriter_overwriteChangedFiles(Rew), 0); |
| 920 | + EXPECT_EQ(getFileContent(Filename), "int romain() { return 0; }"); |
| 921 | +} |
| 922 | + |
| 923 | +TEST_F(LibclangRewriteTest, RewriteRemove) { |
| 924 | + CXSourceLocation B = clang_getLocation(ClangTU, File, 1, 5); |
| 925 | + CXSourceLocation E = clang_getLocation(ClangTU, File, 1, 9); |
| 926 | + CXSourceRange Rng = clang_getRange(B, E); |
| 927 | + |
| 928 | + clang_CXRewriter_removeText(Rew, Rng); |
| 929 | + |
| 930 | + ASSERT_EQ(clang_CXRewriter_overwriteChangedFiles(Rew), 0); |
| 931 | + EXPECT_EQ(getFileContent(Filename), "int () { return 0; }"); |
| 932 | +} |
0 commit comments