2017-03-10 18:30:14 +08:00
|
|
|
//===-- ClangRenameTests.cpp - clang-rename unit tests --------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-03-10 18:30:14 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-09-04 00:26:36 +08:00
|
|
|
#ifndef LLVM_CLANG_UNITTESTS_RENAME_CLANGRENAMETEST_H
|
|
|
|
#define LLVM_CLANG_UNITTESTS_RENAME_CLANGRENAMETEST_H
|
|
|
|
|
2017-03-10 18:30:14 +08:00
|
|
|
#include "unittests/Tooling/RewriterTestContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Basic/FileSystemOptions.h"
|
|
|
|
#include "clang/Format/Format.h"
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/PCHContainerOperations.h"
|
|
|
|
#include "clang/Tooling/Refactoring.h"
|
2017-07-01 00:36:09 +08:00
|
|
|
#include "clang/Tooling/Refactoring/Rename/RenamingAction.h"
|
|
|
|
#include "clang/Tooling/Refactoring/Rename/USRFindingAction.h"
|
2017-03-10 18:30:14 +08:00
|
|
|
#include "clang/Tooling/Tooling.h"
|
|
|
|
#include "llvm/ADT/IntrusiveRefCntPtr.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/Support/Format.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2018-10-10 21:27:25 +08:00
|
|
|
#include "llvm/Support/VirtualFileSystem.h"
|
2017-03-10 18:30:14 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clang_rename {
|
2017-04-04 17:30:06 +08:00
|
|
|
namespace test {
|
2017-03-10 18:30:14 +08:00
|
|
|
|
|
|
|
struct Case {
|
|
|
|
std::string Before;
|
|
|
|
std::string After;
|
2017-04-04 17:30:06 +08:00
|
|
|
std::string OldName;
|
|
|
|
std::string NewName;
|
2017-03-10 18:30:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class ClangRenameTest : public testing::Test,
|
|
|
|
public testing::WithParamInterface<Case> {
|
|
|
|
protected:
|
2017-04-04 17:30:06 +08:00
|
|
|
void AppendToHeader(StringRef Code) { HeaderContent += Code.str(); }
|
2017-03-10 18:30:14 +08:00
|
|
|
|
|
|
|
std::string runClangRenameOnCode(llvm::StringRef Code,
|
|
|
|
llvm::StringRef OldName,
|
|
|
|
llvm::StringRef NewName) {
|
|
|
|
std::string NewCode;
|
|
|
|
llvm::raw_string_ostream(NewCode) << llvm::format(
|
|
|
|
"#include \"%s\"\n%s", HeaderName.c_str(), Code.str().c_str());
|
|
|
|
tooling::FileContentMappings FileContents = {{HeaderName, HeaderContent},
|
|
|
|
{CCName, NewCode}};
|
|
|
|
clang::RewriterTestContext Context;
|
|
|
|
Context.createInMemoryFile(HeaderName, HeaderContent);
|
|
|
|
clang::FileID InputFileID = Context.createInMemoryFile(CCName, NewCode);
|
|
|
|
|
2020-01-29 03:23:46 +08:00
|
|
|
tooling::USRFindingAction FindingAction({}, {std::string(OldName)}, false);
|
2017-03-10 18:30:14 +08:00
|
|
|
std::unique_ptr<tooling::FrontendActionFactory> USRFindingActionFactory =
|
|
|
|
tooling::newFrontendActionFactory(&FindingAction);
|
|
|
|
|
|
|
|
if (!tooling::runToolOnCodeWithArgs(
|
|
|
|
USRFindingActionFactory->create(), NewCode, {"-std=c++11"}, CCName,
|
|
|
|
"clang-rename", std::make_shared<PCHContainerOperations>(),
|
|
|
|
FileContents))
|
|
|
|
return "";
|
|
|
|
|
|
|
|
const std::vector<std::vector<std::string>> &USRList =
|
|
|
|
FindingAction.getUSRList();
|
2020-01-29 03:23:46 +08:00
|
|
|
std::vector<std::string> NewNames = {std::string(NewName)};
|
2017-03-10 18:30:14 +08:00
|
|
|
std::map<std::string, tooling::Replacements> FileToReplacements;
|
2017-07-01 00:36:09 +08:00
|
|
|
tooling::QualifiedRenamingAction RenameAction(NewNames, USRList,
|
|
|
|
FileToReplacements);
|
2017-03-10 18:30:14 +08:00
|
|
|
auto RenameActionFactory = tooling::newFrontendActionFactory(&RenameAction);
|
|
|
|
if (!tooling::runToolOnCodeWithArgs(
|
|
|
|
RenameActionFactory->create(), NewCode, {"-std=c++11"}, CCName,
|
|
|
|
"clang-rename", std::make_shared<PCHContainerOperations>(),
|
|
|
|
FileContents))
|
|
|
|
return "";
|
|
|
|
|
|
|
|
formatAndApplyAllReplacements(FileToReplacements, Context.Rewrite, "llvm");
|
|
|
|
return Context.getRewrittenText(InputFileID);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompareSnippets(StringRef Expected, StringRef Actual) {
|
|
|
|
std::string ExpectedCode;
|
|
|
|
llvm::raw_string_ostream(ExpectedCode) << llvm::format(
|
|
|
|
"#include \"%s\"\n%s", HeaderName.c_str(), Expected.str().c_str());
|
|
|
|
EXPECT_EQ(format(ExpectedCode), format(Actual));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string format(llvm::StringRef Code) {
|
|
|
|
tooling::Replacements Replaces = format::reformat(
|
|
|
|
format::getLLVMStyle(), Code, {tooling::Range(0, Code.size())});
|
|
|
|
auto ChangedCode = tooling::applyAllReplacements(Code, Replaces);
|
|
|
|
EXPECT_TRUE(static_cast<bool>(ChangedCode));
|
|
|
|
if (!ChangedCode) {
|
|
|
|
llvm::errs() << llvm::toString(ChangedCode.takeError());
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return *ChangedCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string HeaderContent;
|
|
|
|
std::string HeaderName = "header.h";
|
|
|
|
std::string CCName = "input.cc";
|
|
|
|
};
|
|
|
|
|
2017-04-04 17:30:06 +08:00
|
|
|
} // namespace test
|
2017-03-10 18:30:14 +08:00
|
|
|
} // namespace clang_rename
|
|
|
|
} // namesdpace clang
|
2018-09-04 00:26:36 +08:00
|
|
|
|
|
|
|
#endif
|