Haojian Wu | b7da0eb | 2017-03-10 10:30:14 +0000 | [diff] [blame] | 1 | //===-- ClangRenameTests.cpp - clang-rename 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 | |
Argyrios Kyrtzidis | adc178e | 2018-09-03 16:26:36 +0000 | [diff] [blame] | 10 | #ifndef LLVM_CLANG_UNITTESTS_RENAME_CLANGRENAMETEST_H |
| 11 | #define LLVM_CLANG_UNITTESTS_RENAME_CLANGRENAMETEST_H |
| 12 | |
Haojian Wu | b7da0eb | 2017-03-10 10:30:14 +0000 | [diff] [blame] | 13 | #include "unittests/Tooling/RewriterTestContext.h" |
| 14 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 15 | #include "clang/Basic/FileManager.h" |
| 16 | #include "clang/Basic/FileSystemOptions.h" |
Haojian Wu | b7da0eb | 2017-03-10 10:30:14 +0000 | [diff] [blame] | 17 | #include "clang/Format/Format.h" |
| 18 | #include "clang/Frontend/CompilerInstance.h" |
| 19 | #include "clang/Frontend/PCHContainerOperations.h" |
| 20 | #include "clang/Tooling/Refactoring.h" |
Alex Lorenz | 4abbd92 | 2017-06-30 16:36:09 +0000 | [diff] [blame] | 21 | #include "clang/Tooling/Refactoring/Rename/RenamingAction.h" |
| 22 | #include "clang/Tooling/Refactoring/Rename/USRFindingAction.h" |
Haojian Wu | b7da0eb | 2017-03-10 10:30:14 +0000 | [diff] [blame] | 23 | #include "clang/Tooling/Tooling.h" |
| 24 | #include "llvm/ADT/IntrusiveRefCntPtr.h" |
| 25 | #include "llvm/ADT/StringRef.h" |
| 26 | #include "llvm/Support/Format.h" |
| 27 | #include "llvm/Support/MemoryBuffer.h" |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame^] | 28 | #include "llvm/Support/VirtualFileSystem.h" |
Haojian Wu | b7da0eb | 2017-03-10 10:30:14 +0000 | [diff] [blame] | 29 | #include "gtest/gtest.h" |
| 30 | #include <memory> |
| 31 | #include <string> |
| 32 | #include <vector> |
| 33 | |
| 34 | namespace clang { |
| 35 | namespace clang_rename { |
Haojian Wu | 74f823a | 2017-04-04 09:30:06 +0000 | [diff] [blame] | 36 | namespace test { |
Haojian Wu | b7da0eb | 2017-03-10 10:30:14 +0000 | [diff] [blame] | 37 | |
| 38 | struct Case { |
| 39 | std::string Before; |
| 40 | std::string After; |
Haojian Wu | 74f823a | 2017-04-04 09:30:06 +0000 | [diff] [blame] | 41 | std::string OldName; |
| 42 | std::string NewName; |
Haojian Wu | b7da0eb | 2017-03-10 10:30:14 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | class ClangRenameTest : public testing::Test, |
| 46 | public testing::WithParamInterface<Case> { |
| 47 | protected: |
Haojian Wu | 74f823a | 2017-04-04 09:30:06 +0000 | [diff] [blame] | 48 | void AppendToHeader(StringRef Code) { HeaderContent += Code.str(); } |
Haojian Wu | b7da0eb | 2017-03-10 10:30:14 +0000 | [diff] [blame] | 49 | |
| 50 | std::string runClangRenameOnCode(llvm::StringRef Code, |
| 51 | llvm::StringRef OldName, |
| 52 | llvm::StringRef NewName) { |
| 53 | std::string NewCode; |
| 54 | llvm::raw_string_ostream(NewCode) << llvm::format( |
| 55 | "#include \"%s\"\n%s", HeaderName.c_str(), Code.str().c_str()); |
| 56 | tooling::FileContentMappings FileContents = {{HeaderName, HeaderContent}, |
| 57 | {CCName, NewCode}}; |
| 58 | clang::RewriterTestContext Context; |
| 59 | Context.createInMemoryFile(HeaderName, HeaderContent); |
| 60 | clang::FileID InputFileID = Context.createInMemoryFile(CCName, NewCode); |
| 61 | |
Alex Lorenz | 4abbd92 | 2017-06-30 16:36:09 +0000 | [diff] [blame] | 62 | tooling::USRFindingAction FindingAction({}, {OldName}, false); |
Haojian Wu | b7da0eb | 2017-03-10 10:30:14 +0000 | [diff] [blame] | 63 | std::unique_ptr<tooling::FrontendActionFactory> USRFindingActionFactory = |
| 64 | tooling::newFrontendActionFactory(&FindingAction); |
| 65 | |
| 66 | if (!tooling::runToolOnCodeWithArgs( |
| 67 | USRFindingActionFactory->create(), NewCode, {"-std=c++11"}, CCName, |
| 68 | "clang-rename", std::make_shared<PCHContainerOperations>(), |
| 69 | FileContents)) |
| 70 | return ""; |
| 71 | |
| 72 | const std::vector<std::vector<std::string>> &USRList = |
| 73 | FindingAction.getUSRList(); |
Haojian Wu | b7da0eb | 2017-03-10 10:30:14 +0000 | [diff] [blame] | 74 | std::vector<std::string> NewNames = {NewName}; |
| 75 | std::map<std::string, tooling::Replacements> FileToReplacements; |
Alex Lorenz | 4abbd92 | 2017-06-30 16:36:09 +0000 | [diff] [blame] | 76 | tooling::QualifiedRenamingAction RenameAction(NewNames, USRList, |
| 77 | FileToReplacements); |
Haojian Wu | b7da0eb | 2017-03-10 10:30:14 +0000 | [diff] [blame] | 78 | auto RenameActionFactory = tooling::newFrontendActionFactory(&RenameAction); |
| 79 | if (!tooling::runToolOnCodeWithArgs( |
| 80 | RenameActionFactory->create(), NewCode, {"-std=c++11"}, CCName, |
| 81 | "clang-rename", std::make_shared<PCHContainerOperations>(), |
| 82 | FileContents)) |
| 83 | return ""; |
| 84 | |
| 85 | formatAndApplyAllReplacements(FileToReplacements, Context.Rewrite, "llvm"); |
| 86 | return Context.getRewrittenText(InputFileID); |
| 87 | } |
| 88 | |
| 89 | void CompareSnippets(StringRef Expected, StringRef Actual) { |
| 90 | std::string ExpectedCode; |
| 91 | llvm::raw_string_ostream(ExpectedCode) << llvm::format( |
| 92 | "#include \"%s\"\n%s", HeaderName.c_str(), Expected.str().c_str()); |
| 93 | EXPECT_EQ(format(ExpectedCode), format(Actual)); |
| 94 | } |
| 95 | |
| 96 | std::string format(llvm::StringRef Code) { |
| 97 | tooling::Replacements Replaces = format::reformat( |
| 98 | format::getLLVMStyle(), Code, {tooling::Range(0, Code.size())}); |
| 99 | auto ChangedCode = tooling::applyAllReplacements(Code, Replaces); |
| 100 | EXPECT_TRUE(static_cast<bool>(ChangedCode)); |
| 101 | if (!ChangedCode) { |
| 102 | llvm::errs() << llvm::toString(ChangedCode.takeError()); |
| 103 | return ""; |
| 104 | } |
| 105 | return *ChangedCode; |
| 106 | } |
| 107 | |
| 108 | std::string HeaderContent; |
| 109 | std::string HeaderName = "header.h"; |
| 110 | std::string CCName = "input.cc"; |
| 111 | }; |
| 112 | |
Haojian Wu | 74f823a | 2017-04-04 09:30:06 +0000 | [diff] [blame] | 113 | } // namespace test |
Haojian Wu | b7da0eb | 2017-03-10 10:30:14 +0000 | [diff] [blame] | 114 | } // namespace clang_rename |
| 115 | } // namesdpace clang |
Argyrios Kyrtzidis | adc178e | 2018-09-03 16:26:36 +0000 | [diff] [blame] | 116 | |
| 117 | #endif |