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