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