1 //===-- ClangRenameTests.cpp - clang-rename unit tests --------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CLANG_UNITTESTS_RENAME_CLANGRENAMETEST_H
10 #define LLVM_CLANG_UNITTESTS_RENAME_CLANGRENAMETEST_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/Format/Format.h"
17 #include "clang/Frontend/CompilerInstance.h"
18 #include "clang/Frontend/PCHContainerOperations.h"
19 #include "clang/Tooling/Refactoring.h"
20 #include "clang/Tooling/Refactoring/Rename/RenamingAction.h"
21 #include "clang/Tooling/Refactoring/Rename/USRFindingAction.h"
22 #include "clang/Tooling/Tooling.h"
23 #include "llvm/ADT/IntrusiveRefCntPtr.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/Support/Format.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/VirtualFileSystem.h"
28 #include "gtest/gtest.h"
34 namespace clang_rename
{
44 class ClangRenameTest
: public testing::Test
,
45 public testing::WithParamInterface
<Case
> {
47 void AppendToHeader(StringRef Code
) { HeaderContent
+= Code
.str(); }
49 std::string
runClangRenameOnCode(llvm::StringRef Code
,
50 llvm::StringRef OldName
,
51 llvm::StringRef NewName
) {
53 llvm::raw_string_ostream(NewCode
) << llvm::format(
54 "#include \"%s\"\n%s", HeaderName
.c_str(), Code
.str().c_str());
55 tooling::FileContentMappings FileContents
= {{HeaderName
, HeaderContent
},
57 clang::RewriterTestContext Context
;
58 Context
.createInMemoryFile(HeaderName
, HeaderContent
);
59 clang::FileID InputFileID
= Context
.createInMemoryFile(CCName
, NewCode
);
61 tooling::USRFindingAction
FindingAction({}, {std::string(OldName
)}, false);
62 std::unique_ptr
<tooling::FrontendActionFactory
> USRFindingActionFactory
=
63 tooling::newFrontendActionFactory(&FindingAction
);
65 if (!tooling::runToolOnCodeWithArgs(
66 USRFindingActionFactory
->create(), NewCode
, {"-std=c++11"}, CCName
,
67 "clang-rename", std::make_shared
<PCHContainerOperations
>(),
71 const std::vector
<std::vector
<std::string
>> &USRList
=
72 FindingAction
.getUSRList();
73 std::vector
<std::string
> NewNames
= {std::string(NewName
)};
74 std::map
<std::string
, tooling::Replacements
> FileToReplacements
;
75 tooling::QualifiedRenamingAction
RenameAction(NewNames
, USRList
,
77 auto RenameActionFactory
= tooling::newFrontendActionFactory(&RenameAction
);
78 if (!tooling::runToolOnCodeWithArgs(
79 RenameActionFactory
->create(), NewCode
, {"-std=c++11"}, CCName
,
80 "clang-rename", std::make_shared
<PCHContainerOperations
>(),
84 formatAndApplyAllReplacements(FileToReplacements
, Context
.Rewrite
, "llvm");
85 return Context
.getRewrittenText(InputFileID
);
88 void CompareSnippets(StringRef Expected
, StringRef Actual
) {
89 std::string ExpectedCode
;
90 llvm::raw_string_ostream(ExpectedCode
) << llvm::format(
91 "#include \"%s\"\n%s", HeaderName
.c_str(), Expected
.str().c_str());
92 EXPECT_EQ(format(ExpectedCode
), format(Actual
));
95 std::string
format(llvm::StringRef Code
) {
96 tooling::Replacements Replaces
= format::reformat(
97 format::getLLVMStyle(), Code
, {tooling::Range(0, Code
.size())});
98 auto ChangedCode
= tooling::applyAllReplacements(Code
, Replaces
);
99 EXPECT_TRUE(static_cast<bool>(ChangedCode
));
101 llvm::errs() << llvm::toString(ChangedCode
.takeError());
107 std::string HeaderContent
;
108 std::string HeaderName
= "header.h";
109 std::string CCName
= "input.cc";
113 } // namespace clang_rename
114 } // namesdpace clang