1f4a2713aSLionel Sambuc //===- unittest/Tooling/RefactoringTest.cpp - Refactoring unit tests ------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc
10f4a2713aSLionel Sambuc #include "RewriterTestContext.h"
11f4a2713aSLionel Sambuc #include "clang/AST/ASTConsumer.h"
12f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
13f4a2713aSLionel Sambuc #include "clang/AST/DeclCXX.h"
14f4a2713aSLionel Sambuc #include "clang/AST/DeclGroup.h"
15f4a2713aSLionel Sambuc #include "clang/AST/RecursiveASTVisitor.h"
16f4a2713aSLionel Sambuc #include "clang/Basic/Diagnostic.h"
17f4a2713aSLionel Sambuc #include "clang/Basic/DiagnosticOptions.h"
18f4a2713aSLionel Sambuc #include "clang/Basic/FileManager.h"
19f4a2713aSLionel Sambuc #include "clang/Basic/LangOptions.h"
20f4a2713aSLionel Sambuc #include "clang/Basic/SourceManager.h"
21f4a2713aSLionel Sambuc #include "clang/Frontend/CompilerInstance.h"
22f4a2713aSLionel Sambuc #include "clang/Frontend/FrontendAction.h"
23f4a2713aSLionel Sambuc #include "clang/Frontend/TextDiagnosticPrinter.h"
24f4a2713aSLionel Sambuc #include "clang/Rewrite/Core/Rewriter.h"
25f4a2713aSLionel Sambuc #include "clang/Tooling/Refactoring.h"
26f4a2713aSLionel Sambuc #include "clang/Tooling/Tooling.h"
27f4a2713aSLionel Sambuc #include "llvm/ADT/SmallString.h"
28f4a2713aSLionel Sambuc #include "llvm/Support/Path.h"
29f4a2713aSLionel Sambuc #include "gtest/gtest.h"
30f4a2713aSLionel Sambuc
31f4a2713aSLionel Sambuc namespace clang {
32f4a2713aSLionel Sambuc namespace tooling {
33f4a2713aSLionel Sambuc
34f4a2713aSLionel Sambuc class ReplacementTest : public ::testing::Test {
35f4a2713aSLionel Sambuc protected:
createReplacement(SourceLocation Start,unsigned Length,llvm::StringRef ReplacementText)36f4a2713aSLionel Sambuc Replacement createReplacement(SourceLocation Start, unsigned Length,
37f4a2713aSLionel Sambuc llvm::StringRef ReplacementText) {
38f4a2713aSLionel Sambuc return Replacement(Context.Sources, Start, Length, ReplacementText);
39f4a2713aSLionel Sambuc }
40f4a2713aSLionel Sambuc
41f4a2713aSLionel Sambuc RewriterTestContext Context;
42f4a2713aSLionel Sambuc };
43f4a2713aSLionel Sambuc
TEST_F(ReplacementTest,CanDeleteAllText)44f4a2713aSLionel Sambuc TEST_F(ReplacementTest, CanDeleteAllText) {
45f4a2713aSLionel Sambuc FileID ID = Context.createInMemoryFile("input.cpp", "text");
46f4a2713aSLionel Sambuc SourceLocation Location = Context.getLocation(ID, 1, 1);
47f4a2713aSLionel Sambuc Replacement Replace(createReplacement(Location, 4, ""));
48f4a2713aSLionel Sambuc EXPECT_TRUE(Replace.apply(Context.Rewrite));
49f4a2713aSLionel Sambuc EXPECT_EQ("", Context.getRewrittenText(ID));
50f4a2713aSLionel Sambuc }
51f4a2713aSLionel Sambuc
TEST_F(ReplacementTest,CanDeleteAllTextInTextWithNewlines)52f4a2713aSLionel Sambuc TEST_F(ReplacementTest, CanDeleteAllTextInTextWithNewlines) {
53f4a2713aSLionel Sambuc FileID ID = Context.createInMemoryFile("input.cpp", "line1\nline2\nline3");
54f4a2713aSLionel Sambuc SourceLocation Location = Context.getLocation(ID, 1, 1);
55f4a2713aSLionel Sambuc Replacement Replace(createReplacement(Location, 17, ""));
56f4a2713aSLionel Sambuc EXPECT_TRUE(Replace.apply(Context.Rewrite));
57f4a2713aSLionel Sambuc EXPECT_EQ("", Context.getRewrittenText(ID));
58f4a2713aSLionel Sambuc }
59f4a2713aSLionel Sambuc
TEST_F(ReplacementTest,CanAddText)60f4a2713aSLionel Sambuc TEST_F(ReplacementTest, CanAddText) {
61f4a2713aSLionel Sambuc FileID ID = Context.createInMemoryFile("input.cpp", "");
62f4a2713aSLionel Sambuc SourceLocation Location = Context.getLocation(ID, 1, 1);
63f4a2713aSLionel Sambuc Replacement Replace(createReplacement(Location, 0, "result"));
64f4a2713aSLionel Sambuc EXPECT_TRUE(Replace.apply(Context.Rewrite));
65f4a2713aSLionel Sambuc EXPECT_EQ("result", Context.getRewrittenText(ID));
66f4a2713aSLionel Sambuc }
67f4a2713aSLionel Sambuc
TEST_F(ReplacementTest,CanReplaceTextAtPosition)68f4a2713aSLionel Sambuc TEST_F(ReplacementTest, CanReplaceTextAtPosition) {
69f4a2713aSLionel Sambuc FileID ID = Context.createInMemoryFile("input.cpp",
70f4a2713aSLionel Sambuc "line1\nline2\nline3\nline4");
71f4a2713aSLionel Sambuc SourceLocation Location = Context.getLocation(ID, 2, 3);
72f4a2713aSLionel Sambuc Replacement Replace(createReplacement(Location, 12, "x"));
73f4a2713aSLionel Sambuc EXPECT_TRUE(Replace.apply(Context.Rewrite));
74f4a2713aSLionel Sambuc EXPECT_EQ("line1\nlixne4", Context.getRewrittenText(ID));
75f4a2713aSLionel Sambuc }
76f4a2713aSLionel Sambuc
TEST_F(ReplacementTest,CanReplaceTextAtPositionMultipleTimes)77f4a2713aSLionel Sambuc TEST_F(ReplacementTest, CanReplaceTextAtPositionMultipleTimes) {
78f4a2713aSLionel Sambuc FileID ID = Context.createInMemoryFile("input.cpp",
79f4a2713aSLionel Sambuc "line1\nline2\nline3\nline4");
80f4a2713aSLionel Sambuc SourceLocation Location1 = Context.getLocation(ID, 2, 3);
81f4a2713aSLionel Sambuc Replacement Replace1(createReplacement(Location1, 12, "x\ny\n"));
82f4a2713aSLionel Sambuc EXPECT_TRUE(Replace1.apply(Context.Rewrite));
83f4a2713aSLionel Sambuc EXPECT_EQ("line1\nlix\ny\nne4", Context.getRewrittenText(ID));
84f4a2713aSLionel Sambuc
85f4a2713aSLionel Sambuc // Since the original source has not been modified, the (4, 4) points to the
86f4a2713aSLionel Sambuc // 'e' in the original content.
87f4a2713aSLionel Sambuc SourceLocation Location2 = Context.getLocation(ID, 4, 4);
88f4a2713aSLionel Sambuc Replacement Replace2(createReplacement(Location2, 1, "f"));
89f4a2713aSLionel Sambuc EXPECT_TRUE(Replace2.apply(Context.Rewrite));
90f4a2713aSLionel Sambuc EXPECT_EQ("line1\nlix\ny\nnf4", Context.getRewrittenText(ID));
91f4a2713aSLionel Sambuc }
92f4a2713aSLionel Sambuc
TEST_F(ReplacementTest,ApplyFailsForNonExistentLocation)93f4a2713aSLionel Sambuc TEST_F(ReplacementTest, ApplyFailsForNonExistentLocation) {
94f4a2713aSLionel Sambuc Replacement Replace("nonexistent-file.cpp", 0, 1, "");
95f4a2713aSLionel Sambuc EXPECT_FALSE(Replace.apply(Context.Rewrite));
96f4a2713aSLionel Sambuc }
97f4a2713aSLionel Sambuc
TEST_F(ReplacementTest,CanRetrivePath)98f4a2713aSLionel Sambuc TEST_F(ReplacementTest, CanRetrivePath) {
99f4a2713aSLionel Sambuc Replacement Replace("/path/to/file.cpp", 0, 1, "");
100f4a2713aSLionel Sambuc EXPECT_EQ("/path/to/file.cpp", Replace.getFilePath());
101f4a2713aSLionel Sambuc }
102f4a2713aSLionel Sambuc
TEST_F(ReplacementTest,ReturnsInvalidPath)103f4a2713aSLionel Sambuc TEST_F(ReplacementTest, ReturnsInvalidPath) {
104f4a2713aSLionel Sambuc Replacement Replace1(Context.Sources, SourceLocation(), 0, "");
105f4a2713aSLionel Sambuc EXPECT_TRUE(Replace1.getFilePath().empty());
106f4a2713aSLionel Sambuc
107f4a2713aSLionel Sambuc Replacement Replace2;
108f4a2713aSLionel Sambuc EXPECT_TRUE(Replace2.getFilePath().empty());
109f4a2713aSLionel Sambuc }
110f4a2713aSLionel Sambuc
TEST_F(ReplacementTest,CanApplyReplacements)111f4a2713aSLionel Sambuc TEST_F(ReplacementTest, CanApplyReplacements) {
112f4a2713aSLionel Sambuc FileID ID = Context.createInMemoryFile("input.cpp",
113f4a2713aSLionel Sambuc "line1\nline2\nline3\nline4");
114f4a2713aSLionel Sambuc Replacements Replaces;
115f4a2713aSLionel Sambuc Replaces.insert(Replacement(Context.Sources, Context.getLocation(ID, 2, 1),
116f4a2713aSLionel Sambuc 5, "replaced"));
117f4a2713aSLionel Sambuc Replaces.insert(Replacement(Context.Sources, Context.getLocation(ID, 3, 1),
118f4a2713aSLionel Sambuc 5, "other"));
119f4a2713aSLionel Sambuc EXPECT_TRUE(applyAllReplacements(Replaces, Context.Rewrite));
120f4a2713aSLionel Sambuc EXPECT_EQ("line1\nreplaced\nother\nline4", Context.getRewrittenText(ID));
121f4a2713aSLionel Sambuc }
122f4a2713aSLionel Sambuc
123f4a2713aSLionel Sambuc // FIXME: Remove this test case when Replacements is implemented as std::vector
124f4a2713aSLionel Sambuc // instead of std::set. The other ReplacementTest tests will need to be updated
125f4a2713aSLionel Sambuc // at that point as well.
TEST_F(ReplacementTest,VectorCanApplyReplacements)126f4a2713aSLionel Sambuc TEST_F(ReplacementTest, VectorCanApplyReplacements) {
127f4a2713aSLionel Sambuc FileID ID = Context.createInMemoryFile("input.cpp",
128f4a2713aSLionel Sambuc "line1\nline2\nline3\nline4");
129f4a2713aSLionel Sambuc std::vector<Replacement> Replaces;
130f4a2713aSLionel Sambuc Replaces.push_back(Replacement(Context.Sources, Context.getLocation(ID, 2, 1),
131f4a2713aSLionel Sambuc 5, "replaced"));
132f4a2713aSLionel Sambuc Replaces.push_back(
133f4a2713aSLionel Sambuc Replacement(Context.Sources, Context.getLocation(ID, 3, 1), 5, "other"));
134f4a2713aSLionel Sambuc EXPECT_TRUE(applyAllReplacements(Replaces, Context.Rewrite));
135f4a2713aSLionel Sambuc EXPECT_EQ("line1\nreplaced\nother\nline4", Context.getRewrittenText(ID));
136f4a2713aSLionel Sambuc }
137f4a2713aSLionel Sambuc
TEST_F(ReplacementTest,SkipsDuplicateReplacements)138f4a2713aSLionel Sambuc TEST_F(ReplacementTest, SkipsDuplicateReplacements) {
139f4a2713aSLionel Sambuc FileID ID = Context.createInMemoryFile("input.cpp",
140f4a2713aSLionel Sambuc "line1\nline2\nline3\nline4");
141f4a2713aSLionel Sambuc Replacements Replaces;
142f4a2713aSLionel Sambuc Replaces.insert(Replacement(Context.Sources, Context.getLocation(ID, 2, 1),
143f4a2713aSLionel Sambuc 5, "replaced"));
144f4a2713aSLionel Sambuc Replaces.insert(Replacement(Context.Sources, Context.getLocation(ID, 2, 1),
145f4a2713aSLionel Sambuc 5, "replaced"));
146f4a2713aSLionel Sambuc Replaces.insert(Replacement(Context.Sources, Context.getLocation(ID, 2, 1),
147f4a2713aSLionel Sambuc 5, "replaced"));
148f4a2713aSLionel Sambuc EXPECT_TRUE(applyAllReplacements(Replaces, Context.Rewrite));
149f4a2713aSLionel Sambuc EXPECT_EQ("line1\nreplaced\nline3\nline4", Context.getRewrittenText(ID));
150f4a2713aSLionel Sambuc }
151f4a2713aSLionel Sambuc
TEST_F(ReplacementTest,ApplyAllFailsIfOneApplyFails)152f4a2713aSLionel Sambuc TEST_F(ReplacementTest, ApplyAllFailsIfOneApplyFails) {
153f4a2713aSLionel Sambuc // This test depends on the value of the file name of an invalid source
154f4a2713aSLionel Sambuc // location being in the range ]a, z[.
155f4a2713aSLionel Sambuc FileID IDa = Context.createInMemoryFile("a.cpp", "text");
156f4a2713aSLionel Sambuc FileID IDz = Context.createInMemoryFile("z.cpp", "text");
157f4a2713aSLionel Sambuc Replacements Replaces;
158f4a2713aSLionel Sambuc Replaces.insert(Replacement(Context.Sources, Context.getLocation(IDa, 1, 1),
159f4a2713aSLionel Sambuc 4, "a"));
160f4a2713aSLionel Sambuc Replaces.insert(Replacement(Context.Sources, SourceLocation(),
161f4a2713aSLionel Sambuc 5, "2"));
162f4a2713aSLionel Sambuc Replaces.insert(Replacement(Context.Sources, Context.getLocation(IDz, 1, 1),
163f4a2713aSLionel Sambuc 4, "z"));
164f4a2713aSLionel Sambuc EXPECT_FALSE(applyAllReplacements(Replaces, Context.Rewrite));
165f4a2713aSLionel Sambuc EXPECT_EQ("a", Context.getRewrittenText(IDa));
166f4a2713aSLionel Sambuc EXPECT_EQ("z", Context.getRewrittenText(IDz));
167f4a2713aSLionel Sambuc }
168f4a2713aSLionel Sambuc
TEST(ShiftedCodePositionTest,FindsNewCodePosition)169f4a2713aSLionel Sambuc TEST(ShiftedCodePositionTest, FindsNewCodePosition) {
170f4a2713aSLionel Sambuc Replacements Replaces;
171f4a2713aSLionel Sambuc Replaces.insert(Replacement("", 0, 1, ""));
172f4a2713aSLionel Sambuc Replaces.insert(Replacement("", 4, 3, " "));
173f4a2713aSLionel Sambuc // Assume ' int i;' is turned into 'int i;' and cursor is located at '|'.
174f4a2713aSLionel Sambuc EXPECT_EQ(0u, shiftedCodePosition(Replaces, 0)); // |int i;
175f4a2713aSLionel Sambuc EXPECT_EQ(0u, shiftedCodePosition(Replaces, 1)); // |nt i;
176f4a2713aSLionel Sambuc EXPECT_EQ(1u, shiftedCodePosition(Replaces, 2)); // i|t i;
177f4a2713aSLionel Sambuc EXPECT_EQ(2u, shiftedCodePosition(Replaces, 3)); // in| i;
178f4a2713aSLionel Sambuc EXPECT_EQ(3u, shiftedCodePosition(Replaces, 4)); // int| i;
179f4a2713aSLionel Sambuc EXPECT_EQ(4u, shiftedCodePosition(Replaces, 5)); // int | i;
180f4a2713aSLionel Sambuc EXPECT_EQ(4u, shiftedCodePosition(Replaces, 6)); // int |i;
181f4a2713aSLionel Sambuc EXPECT_EQ(4u, shiftedCodePosition(Replaces, 7)); // int |;
182f4a2713aSLionel Sambuc EXPECT_EQ(5u, shiftedCodePosition(Replaces, 8)); // int i|
183f4a2713aSLionel Sambuc }
184f4a2713aSLionel Sambuc
185f4a2713aSLionel Sambuc // FIXME: Remove this test case when Replacements is implemented as std::vector
186f4a2713aSLionel Sambuc // instead of std::set. The other ReplacementTest tests will need to be updated
187f4a2713aSLionel Sambuc // at that point as well.
TEST(ShiftedCodePositionTest,VectorFindsNewCodePositionWithInserts)188f4a2713aSLionel Sambuc TEST(ShiftedCodePositionTest, VectorFindsNewCodePositionWithInserts) {
189f4a2713aSLionel Sambuc std::vector<Replacement> Replaces;
190f4a2713aSLionel Sambuc Replaces.push_back(Replacement("", 0, 1, ""));
191f4a2713aSLionel Sambuc Replaces.push_back(Replacement("", 4, 3, " "));
192f4a2713aSLionel Sambuc // Assume ' int i;' is turned into 'int i;' and cursor is located at '|'.
193f4a2713aSLionel Sambuc EXPECT_EQ(0u, shiftedCodePosition(Replaces, 0)); // |int i;
194f4a2713aSLionel Sambuc EXPECT_EQ(0u, shiftedCodePosition(Replaces, 1)); // |nt i;
195f4a2713aSLionel Sambuc EXPECT_EQ(1u, shiftedCodePosition(Replaces, 2)); // i|t i;
196f4a2713aSLionel Sambuc EXPECT_EQ(2u, shiftedCodePosition(Replaces, 3)); // in| i;
197f4a2713aSLionel Sambuc EXPECT_EQ(3u, shiftedCodePosition(Replaces, 4)); // int| i;
198f4a2713aSLionel Sambuc EXPECT_EQ(4u, shiftedCodePosition(Replaces, 5)); // int | i;
199f4a2713aSLionel Sambuc EXPECT_EQ(4u, shiftedCodePosition(Replaces, 6)); // int |i;
200f4a2713aSLionel Sambuc EXPECT_EQ(4u, shiftedCodePosition(Replaces, 7)); // int |;
201f4a2713aSLionel Sambuc EXPECT_EQ(5u, shiftedCodePosition(Replaces, 8)); // int i|
202f4a2713aSLionel Sambuc }
203f4a2713aSLionel Sambuc
TEST(ShiftedCodePositionTest,FindsNewCodePositionWithInserts)204f4a2713aSLionel Sambuc TEST(ShiftedCodePositionTest, FindsNewCodePositionWithInserts) {
205f4a2713aSLionel Sambuc Replacements Replaces;
206f4a2713aSLionel Sambuc Replaces.insert(Replacement("", 4, 0, "\"\n\""));
207f4a2713aSLionel Sambuc // Assume '"12345678"' is turned into '"1234"\n"5678"'.
208f4a2713aSLionel Sambuc EXPECT_EQ(4u, shiftedCodePosition(Replaces, 4)); // "123|5678"
209f4a2713aSLionel Sambuc EXPECT_EQ(8u, shiftedCodePosition(Replaces, 5)); // "1234|678"
210f4a2713aSLionel Sambuc }
211f4a2713aSLionel Sambuc
212f4a2713aSLionel Sambuc class FlushRewrittenFilesTest : public ::testing::Test {
213f4a2713aSLionel Sambuc public:
FlushRewrittenFilesTest()214f4a2713aSLionel Sambuc FlushRewrittenFilesTest() {}
215f4a2713aSLionel Sambuc
~FlushRewrittenFilesTest()216f4a2713aSLionel Sambuc ~FlushRewrittenFilesTest() {
217f4a2713aSLionel Sambuc for (llvm::StringMap<std::string>::iterator I = TemporaryFiles.begin(),
218f4a2713aSLionel Sambuc E = TemporaryFiles.end();
219f4a2713aSLionel Sambuc I != E; ++I) {
220f4a2713aSLionel Sambuc llvm::StringRef Name = I->second;
221*0a6a1f1dSLionel Sambuc std::error_code EC = llvm::sys::fs::remove(Name);
222f4a2713aSLionel Sambuc (void)EC;
223f4a2713aSLionel Sambuc assert(!EC);
224f4a2713aSLionel Sambuc }
225f4a2713aSLionel Sambuc }
226f4a2713aSLionel Sambuc
createFile(llvm::StringRef Name,llvm::StringRef Content)227f4a2713aSLionel Sambuc FileID createFile(llvm::StringRef Name, llvm::StringRef Content) {
228f4a2713aSLionel Sambuc SmallString<1024> Path;
229f4a2713aSLionel Sambuc int FD;
230*0a6a1f1dSLionel Sambuc std::error_code EC = llvm::sys::fs::createTemporaryFile(Name, "", FD, Path);
231f4a2713aSLionel Sambuc assert(!EC);
232f4a2713aSLionel Sambuc (void)EC;
233f4a2713aSLionel Sambuc
234f4a2713aSLionel Sambuc llvm::raw_fd_ostream OutStream(FD, true);
235f4a2713aSLionel Sambuc OutStream << Content;
236f4a2713aSLionel Sambuc OutStream.close();
237f4a2713aSLionel Sambuc const FileEntry *File = Context.Files.getFile(Path);
238*0a6a1f1dSLionel Sambuc assert(File != nullptr);
239f4a2713aSLionel Sambuc
240*0a6a1f1dSLionel Sambuc StringRef Found =
241*0a6a1f1dSLionel Sambuc TemporaryFiles.insert(std::make_pair(Name, Path.str())).first->second;
242f4a2713aSLionel Sambuc assert(Found == Path);
243f4a2713aSLionel Sambuc (void)Found;
244f4a2713aSLionel Sambuc return Context.Sources.createFileID(File, SourceLocation(), SrcMgr::C_User);
245f4a2713aSLionel Sambuc }
246f4a2713aSLionel Sambuc
getFileContentFromDisk(llvm::StringRef Name)247f4a2713aSLionel Sambuc std::string getFileContentFromDisk(llvm::StringRef Name) {
248f4a2713aSLionel Sambuc std::string Path = TemporaryFiles.lookup(Name);
249f4a2713aSLionel Sambuc assert(!Path.empty());
250f4a2713aSLionel Sambuc // We need to read directly from the FileManager without relaying through
251f4a2713aSLionel Sambuc // a FileEntry, as otherwise we'd read through an already opened file
252f4a2713aSLionel Sambuc // descriptor, which might not see the changes made.
253f4a2713aSLionel Sambuc // FIXME: Figure out whether there is a way to get the SourceManger to
254f4a2713aSLionel Sambuc // reopen the file.
255*0a6a1f1dSLionel Sambuc auto FileBuffer = Context.Files.getBufferForFile(Path);
256*0a6a1f1dSLionel Sambuc return (*FileBuffer)->getBuffer();
257f4a2713aSLionel Sambuc }
258f4a2713aSLionel Sambuc
259f4a2713aSLionel Sambuc llvm::StringMap<std::string> TemporaryFiles;
260f4a2713aSLionel Sambuc RewriterTestContext Context;
261f4a2713aSLionel Sambuc };
262f4a2713aSLionel Sambuc
TEST_F(FlushRewrittenFilesTest,StoresChangesOnDisk)263f4a2713aSLionel Sambuc TEST_F(FlushRewrittenFilesTest, StoresChangesOnDisk) {
264f4a2713aSLionel Sambuc FileID ID = createFile("input.cpp", "line1\nline2\nline3\nline4");
265f4a2713aSLionel Sambuc Replacements Replaces;
266f4a2713aSLionel Sambuc Replaces.insert(Replacement(Context.Sources, Context.getLocation(ID, 2, 1),
267f4a2713aSLionel Sambuc 5, "replaced"));
268f4a2713aSLionel Sambuc EXPECT_TRUE(applyAllReplacements(Replaces, Context.Rewrite));
269f4a2713aSLionel Sambuc EXPECT_FALSE(Context.Rewrite.overwriteChangedFiles());
270f4a2713aSLionel Sambuc EXPECT_EQ("line1\nreplaced\nline3\nline4",
271f4a2713aSLionel Sambuc getFileContentFromDisk("input.cpp"));
272f4a2713aSLionel Sambuc }
273f4a2713aSLionel Sambuc
274f4a2713aSLionel Sambuc namespace {
275f4a2713aSLionel Sambuc template <typename T>
276f4a2713aSLionel Sambuc class TestVisitor : public clang::RecursiveASTVisitor<T> {
277f4a2713aSLionel Sambuc public:
runOver(StringRef Code)278f4a2713aSLionel Sambuc bool runOver(StringRef Code) {
279f4a2713aSLionel Sambuc return runToolOnCode(new TestAction(this), Code);
280f4a2713aSLionel Sambuc }
281f4a2713aSLionel Sambuc
282f4a2713aSLionel Sambuc protected:
283f4a2713aSLionel Sambuc clang::SourceManager *SM;
284f4a2713aSLionel Sambuc
285f4a2713aSLionel Sambuc private:
286f4a2713aSLionel Sambuc class FindConsumer : public clang::ASTConsumer {
287f4a2713aSLionel Sambuc public:
FindConsumer(TestVisitor * Visitor)288f4a2713aSLionel Sambuc FindConsumer(TestVisitor *Visitor) : Visitor(Visitor) {}
289f4a2713aSLionel Sambuc
HandleTranslationUnit(clang::ASTContext & Context)290f4a2713aSLionel Sambuc virtual void HandleTranslationUnit(clang::ASTContext &Context) {
291f4a2713aSLionel Sambuc Visitor->TraverseDecl(Context.getTranslationUnitDecl());
292f4a2713aSLionel Sambuc }
293f4a2713aSLionel Sambuc
294f4a2713aSLionel Sambuc private:
295f4a2713aSLionel Sambuc TestVisitor *Visitor;
296f4a2713aSLionel Sambuc };
297f4a2713aSLionel Sambuc
298f4a2713aSLionel Sambuc class TestAction : public clang::ASTFrontendAction {
299f4a2713aSLionel Sambuc public:
TestAction(TestVisitor * Visitor)300f4a2713aSLionel Sambuc TestAction(TestVisitor *Visitor) : Visitor(Visitor) {}
301f4a2713aSLionel Sambuc
302*0a6a1f1dSLionel Sambuc virtual std::unique_ptr<clang::ASTConsumer>
CreateASTConsumer(clang::CompilerInstance & compiler,llvm::StringRef dummy)303*0a6a1f1dSLionel Sambuc CreateASTConsumer(clang::CompilerInstance &compiler,
304*0a6a1f1dSLionel Sambuc llvm::StringRef dummy) {
305f4a2713aSLionel Sambuc Visitor->SM = &compiler.getSourceManager();
306f4a2713aSLionel Sambuc /// TestConsumer will be deleted by the framework calling us.
307*0a6a1f1dSLionel Sambuc return llvm::make_unique<FindConsumer>(Visitor);
308f4a2713aSLionel Sambuc }
309f4a2713aSLionel Sambuc
310f4a2713aSLionel Sambuc private:
311f4a2713aSLionel Sambuc TestVisitor *Visitor;
312f4a2713aSLionel Sambuc };
313f4a2713aSLionel Sambuc };
314f4a2713aSLionel Sambuc } // end namespace
315f4a2713aSLionel Sambuc
expectReplacementAt(const Replacement & Replace,StringRef File,unsigned Offset,unsigned Length)316f4a2713aSLionel Sambuc void expectReplacementAt(const Replacement &Replace,
317f4a2713aSLionel Sambuc StringRef File, unsigned Offset, unsigned Length) {
318f4a2713aSLionel Sambuc ASSERT_TRUE(Replace.isApplicable());
319f4a2713aSLionel Sambuc EXPECT_EQ(File, Replace.getFilePath());
320f4a2713aSLionel Sambuc EXPECT_EQ(Offset, Replace.getOffset());
321f4a2713aSLionel Sambuc EXPECT_EQ(Length, Replace.getLength());
322f4a2713aSLionel Sambuc }
323f4a2713aSLionel Sambuc
324f4a2713aSLionel Sambuc class ClassDeclXVisitor : public TestVisitor<ClassDeclXVisitor> {
325f4a2713aSLionel Sambuc public:
VisitCXXRecordDecl(CXXRecordDecl * Record)326f4a2713aSLionel Sambuc bool VisitCXXRecordDecl(CXXRecordDecl *Record) {
327f4a2713aSLionel Sambuc if (Record->getName() == "X") {
328f4a2713aSLionel Sambuc Replace = Replacement(*SM, Record, "");
329f4a2713aSLionel Sambuc }
330f4a2713aSLionel Sambuc return true;
331f4a2713aSLionel Sambuc }
332f4a2713aSLionel Sambuc Replacement Replace;
333f4a2713aSLionel Sambuc };
334f4a2713aSLionel Sambuc
TEST(Replacement,CanBeConstructedFromNode)335f4a2713aSLionel Sambuc TEST(Replacement, CanBeConstructedFromNode) {
336f4a2713aSLionel Sambuc ClassDeclXVisitor ClassDeclX;
337f4a2713aSLionel Sambuc EXPECT_TRUE(ClassDeclX.runOver(" class X;"));
338f4a2713aSLionel Sambuc expectReplacementAt(ClassDeclX.Replace, "input.cc", 5, 7);
339f4a2713aSLionel Sambuc }
340f4a2713aSLionel Sambuc
TEST(Replacement,ReplacesAtSpellingLocation)341f4a2713aSLionel Sambuc TEST(Replacement, ReplacesAtSpellingLocation) {
342f4a2713aSLionel Sambuc ClassDeclXVisitor ClassDeclX;
343f4a2713aSLionel Sambuc EXPECT_TRUE(ClassDeclX.runOver("#define A(Y) Y\nA(class X);"));
344f4a2713aSLionel Sambuc expectReplacementAt(ClassDeclX.Replace, "input.cc", 17, 7);
345f4a2713aSLionel Sambuc }
346f4a2713aSLionel Sambuc
347f4a2713aSLionel Sambuc class CallToFVisitor : public TestVisitor<CallToFVisitor> {
348f4a2713aSLionel Sambuc public:
VisitCallExpr(CallExpr * Call)349f4a2713aSLionel Sambuc bool VisitCallExpr(CallExpr *Call) {
350f4a2713aSLionel Sambuc if (Call->getDirectCallee()->getName() == "F") {
351f4a2713aSLionel Sambuc Replace = Replacement(*SM, Call, "");
352f4a2713aSLionel Sambuc }
353f4a2713aSLionel Sambuc return true;
354f4a2713aSLionel Sambuc }
355f4a2713aSLionel Sambuc Replacement Replace;
356f4a2713aSLionel Sambuc };
357f4a2713aSLionel Sambuc
TEST(Replacement,FunctionCall)358f4a2713aSLionel Sambuc TEST(Replacement, FunctionCall) {
359f4a2713aSLionel Sambuc CallToFVisitor CallToF;
360f4a2713aSLionel Sambuc EXPECT_TRUE(CallToF.runOver("void F(); void G() { F(); }"));
361f4a2713aSLionel Sambuc expectReplacementAt(CallToF.Replace, "input.cc", 21, 3);
362f4a2713aSLionel Sambuc }
363f4a2713aSLionel Sambuc
TEST(Replacement,TemplatedFunctionCall)364f4a2713aSLionel Sambuc TEST(Replacement, TemplatedFunctionCall) {
365f4a2713aSLionel Sambuc CallToFVisitor CallToF;
366f4a2713aSLionel Sambuc EXPECT_TRUE(CallToF.runOver(
367f4a2713aSLionel Sambuc "template <typename T> void F(); void G() { F<int>(); }"));
368f4a2713aSLionel Sambuc expectReplacementAt(CallToF.Replace, "input.cc", 43, 8);
369f4a2713aSLionel Sambuc }
370f4a2713aSLionel Sambuc
TEST(Range,overlaps)371f4a2713aSLionel Sambuc TEST(Range, overlaps) {
372f4a2713aSLionel Sambuc EXPECT_TRUE(Range(10, 10).overlapsWith(Range(0, 11)));
373f4a2713aSLionel Sambuc EXPECT_TRUE(Range(0, 11).overlapsWith(Range(10, 10)));
374f4a2713aSLionel Sambuc EXPECT_FALSE(Range(10, 10).overlapsWith(Range(0, 10)));
375f4a2713aSLionel Sambuc EXPECT_FALSE(Range(0, 10).overlapsWith(Range(10, 10)));
376f4a2713aSLionel Sambuc EXPECT_TRUE(Range(0, 10).overlapsWith(Range(2, 6)));
377f4a2713aSLionel Sambuc EXPECT_TRUE(Range(2, 6).overlapsWith(Range(0, 10)));
378f4a2713aSLionel Sambuc }
379f4a2713aSLionel Sambuc
TEST(Range,contains)380f4a2713aSLionel Sambuc TEST(Range, contains) {
381f4a2713aSLionel Sambuc EXPECT_TRUE(Range(0, 10).contains(Range(0, 10)));
382f4a2713aSLionel Sambuc EXPECT_TRUE(Range(0, 10).contains(Range(2, 6)));
383f4a2713aSLionel Sambuc EXPECT_FALSE(Range(2, 6).contains(Range(0, 10)));
384f4a2713aSLionel Sambuc EXPECT_FALSE(Range(0, 10).contains(Range(0, 11)));
385f4a2713aSLionel Sambuc }
386f4a2713aSLionel Sambuc
TEST(DeduplicateTest,removesDuplicates)387f4a2713aSLionel Sambuc TEST(DeduplicateTest, removesDuplicates) {
388f4a2713aSLionel Sambuc std::vector<Replacement> Input;
389f4a2713aSLionel Sambuc Input.push_back(Replacement("fileA", 50, 0, " foo "));
390f4a2713aSLionel Sambuc Input.push_back(Replacement("fileA", 10, 3, " bar "));
391f4a2713aSLionel Sambuc Input.push_back(Replacement("fileA", 10, 2, " bar ")); // Length differs
392f4a2713aSLionel Sambuc Input.push_back(Replacement("fileA", 9, 3, " bar ")); // Offset differs
393f4a2713aSLionel Sambuc Input.push_back(Replacement("fileA", 50, 0, " foo ")); // Duplicate
394f4a2713aSLionel Sambuc Input.push_back(Replacement("fileA", 51, 3, " bar "));
395f4a2713aSLionel Sambuc Input.push_back(Replacement("fileB", 51, 3, " bar ")); // Filename differs!
396*0a6a1f1dSLionel Sambuc Input.push_back(Replacement("fileB", 60, 1, " bar "));
397*0a6a1f1dSLionel Sambuc Input.push_back(Replacement("fileA", 60, 2, " bar "));
398f4a2713aSLionel Sambuc Input.push_back(Replacement("fileA", 51, 3, " moo ")); // Replacement text
399f4a2713aSLionel Sambuc // differs!
400f4a2713aSLionel Sambuc
401f4a2713aSLionel Sambuc std::vector<Replacement> Expected;
402f4a2713aSLionel Sambuc Expected.push_back(Replacement("fileA", 9, 3, " bar "));
403f4a2713aSLionel Sambuc Expected.push_back(Replacement("fileA", 10, 2, " bar "));
404f4a2713aSLionel Sambuc Expected.push_back(Replacement("fileA", 10, 3, " bar "));
405f4a2713aSLionel Sambuc Expected.push_back(Replacement("fileA", 50, 0, " foo "));
406f4a2713aSLionel Sambuc Expected.push_back(Replacement("fileA", 51, 3, " bar "));
407f4a2713aSLionel Sambuc Expected.push_back(Replacement("fileA", 51, 3, " moo "));
408*0a6a1f1dSLionel Sambuc Expected.push_back(Replacement("fileB", 60, 1, " bar "));
409*0a6a1f1dSLionel Sambuc Expected.push_back(Replacement("fileA", 60, 2, " bar "));
410f4a2713aSLionel Sambuc
411f4a2713aSLionel Sambuc std::vector<Range> Conflicts; // Ignored for this test
412f4a2713aSLionel Sambuc deduplicate(Input, Conflicts);
413f4a2713aSLionel Sambuc
414*0a6a1f1dSLionel Sambuc EXPECT_EQ(3U, Conflicts.size());
415*0a6a1f1dSLionel Sambuc EXPECT_EQ(Expected, Input);
416f4a2713aSLionel Sambuc }
417f4a2713aSLionel Sambuc
TEST(DeduplicateTest,detectsConflicts)418f4a2713aSLionel Sambuc TEST(DeduplicateTest, detectsConflicts) {
419f4a2713aSLionel Sambuc {
420f4a2713aSLionel Sambuc std::vector<Replacement> Input;
421f4a2713aSLionel Sambuc Input.push_back(Replacement("fileA", 0, 5, " foo "));
422f4a2713aSLionel Sambuc Input.push_back(Replacement("fileA", 0, 5, " foo ")); // Duplicate not a
423f4a2713aSLionel Sambuc // conflict.
424f4a2713aSLionel Sambuc Input.push_back(Replacement("fileA", 2, 6, " bar "));
425f4a2713aSLionel Sambuc Input.push_back(Replacement("fileA", 7, 3, " moo "));
426f4a2713aSLionel Sambuc
427f4a2713aSLionel Sambuc std::vector<Range> Conflicts;
428f4a2713aSLionel Sambuc deduplicate(Input, Conflicts);
429f4a2713aSLionel Sambuc
430f4a2713aSLionel Sambuc // One duplicate is removed and the remaining three items form one
431f4a2713aSLionel Sambuc // conflicted range.
432f4a2713aSLionel Sambuc ASSERT_EQ(3u, Input.size());
433f4a2713aSLionel Sambuc ASSERT_EQ(1u, Conflicts.size());
434f4a2713aSLionel Sambuc ASSERT_EQ(0u, Conflicts.front().getOffset());
435f4a2713aSLionel Sambuc ASSERT_EQ(3u, Conflicts.front().getLength());
436f4a2713aSLionel Sambuc }
437f4a2713aSLionel Sambuc {
438f4a2713aSLionel Sambuc std::vector<Replacement> Input;
439f4a2713aSLionel Sambuc
440f4a2713aSLionel Sambuc // Expected sorted order is shown. It is the sorted order to which the
441f4a2713aSLionel Sambuc // returned conflict info refers to.
442f4a2713aSLionel Sambuc Input.push_back(Replacement("fileA", 0, 5, " foo ")); // 0
443f4a2713aSLionel Sambuc Input.push_back(Replacement("fileA", 5, 5, " bar ")); // 1
444f4a2713aSLionel Sambuc Input.push_back(Replacement("fileA", 6, 0, " bar ")); // 3
445f4a2713aSLionel Sambuc Input.push_back(Replacement("fileA", 5, 5, " moo ")); // 2
446f4a2713aSLionel Sambuc Input.push_back(Replacement("fileA", 7, 2, " bar ")); // 4
447f4a2713aSLionel Sambuc Input.push_back(Replacement("fileA", 15, 5, " golf ")); // 5
448f4a2713aSLionel Sambuc Input.push_back(Replacement("fileA", 16, 5, " bag ")); // 6
449f4a2713aSLionel Sambuc Input.push_back(Replacement("fileA", 10, 3, " club ")); // 7
450f4a2713aSLionel Sambuc
451f4a2713aSLionel Sambuc // #3 is special in that it is completely contained by another conflicting
452f4a2713aSLionel Sambuc // Replacement. #4 ensures #3 hasn't messed up the conflicting range size.
453f4a2713aSLionel Sambuc
454f4a2713aSLionel Sambuc std::vector<Range> Conflicts;
455f4a2713aSLionel Sambuc deduplicate(Input, Conflicts);
456f4a2713aSLionel Sambuc
457f4a2713aSLionel Sambuc // No duplicates
458f4a2713aSLionel Sambuc ASSERT_EQ(8u, Input.size());
459f4a2713aSLionel Sambuc ASSERT_EQ(2u, Conflicts.size());
460f4a2713aSLionel Sambuc ASSERT_EQ(1u, Conflicts[0].getOffset());
461f4a2713aSLionel Sambuc ASSERT_EQ(4u, Conflicts[0].getLength());
462f4a2713aSLionel Sambuc ASSERT_EQ(6u, Conflicts[1].getOffset());
463f4a2713aSLionel Sambuc ASSERT_EQ(2u, Conflicts[1].getLength());
464f4a2713aSLionel Sambuc }
465f4a2713aSLionel Sambuc }
466f4a2713aSLionel Sambuc
467f4a2713aSLionel Sambuc } // end namespace tooling
468f4a2713aSLionel Sambuc } // end namespace clang
469