2019-08-02 17:12:39 +08:00
|
|
|
//===-- TweakTesting.cpp ------------------------------------------------*-===//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "TweakTesting.h"
|
|
|
|
|
|
|
|
#include "Annotations.h"
|
|
|
|
#include "SourceCode.h"
|
2019-09-06 15:49:40 +08:00
|
|
|
#include "TestFS.h"
|
2019-09-09 20:28:44 +08:00
|
|
|
#include "refactor/Tweak.h"
|
2019-08-02 17:12:39 +08:00
|
|
|
#include "clang/Tooling/Core/Replacement.h"
|
|
|
|
#include "llvm/Support/Error.h"
|
2019-10-28 14:18:07 +08:00
|
|
|
#include "gmock/gmock.h"
|
2019-09-06 15:49:40 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include <string>
|
2019-08-02 17:12:39 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
namespace {
|
|
|
|
using Context = TweakTest::CodeContext;
|
|
|
|
|
|
|
|
std::pair<llvm::StringRef, llvm::StringRef> wrapping(Context Ctx) {
|
|
|
|
switch (Ctx) {
|
|
|
|
case TweakTest::File:
|
|
|
|
return {"",""};
|
|
|
|
case TweakTest::Function:
|
|
|
|
return {"void wrapperFunction(){\n", "\n}"};
|
|
|
|
case TweakTest::Expression:
|
|
|
|
return {"auto expressionWrapper(){return\n", "\n;}"};
|
|
|
|
}
|
2019-08-02 20:55:04 +08:00
|
|
|
llvm_unreachable("Unknown TweakTest::CodeContext enum");
|
2019-08-02 17:12:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string wrap(Context Ctx, llvm::StringRef Inner) {
|
|
|
|
auto Wrapping = wrapping(Ctx);
|
|
|
|
return (Wrapping.first + Inner + Wrapping.second).str();
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::StringRef unwrap(Context Ctx, llvm::StringRef Outer) {
|
|
|
|
auto Wrapping = wrapping(Ctx);
|
|
|
|
// Unwrap only if the code matches the expected wrapping.
|
|
|
|
// Don't allow the begin/end wrapping to overlap!
|
|
|
|
if (Outer.startswith(Wrapping.first) && Outer.endswith(Wrapping.second) &&
|
|
|
|
Outer.size() >= Wrapping.first.size() + Wrapping.second.size())
|
|
|
|
return Outer.drop_front(Wrapping.first.size()).drop_back(Wrapping.second.size());
|
|
|
|
return Outer;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<unsigned, unsigned> rangeOrPoint(const Annotations &A) {
|
|
|
|
Range SelectionRng;
|
|
|
|
if (A.points().size() != 0) {
|
|
|
|
assert(A.ranges().size() == 0 &&
|
|
|
|
"both a cursor point and a selection range were specified");
|
|
|
|
SelectionRng = Range{A.point(), A.point()};
|
|
|
|
} else {
|
|
|
|
SelectionRng = A.range();
|
|
|
|
}
|
|
|
|
return {cantFail(positionToOffset(A.code(), SelectionRng.start)),
|
|
|
|
cantFail(positionToOffset(A.code(), SelectionRng.end))};
|
|
|
|
}
|
|
|
|
|
2020-02-23 23:09:36 +08:00
|
|
|
// Prepare and apply the specified tweak based on the selection in Input.
|
|
|
|
// Returns None if and only if prepare() failed.
|
|
|
|
llvm::Optional<llvm::Expected<Tweak::Effect>>
|
|
|
|
applyTweak(ParsedAST &AST, const Annotations &Input, StringRef TweakID,
|
|
|
|
const SymbolIndex *Index) {
|
|
|
|
auto Range = rangeOrPoint(Input);
|
|
|
|
llvm::Optional<llvm::Expected<Tweak::Effect>> Result;
|
|
|
|
SelectionTree::createEach(AST.getASTContext(), AST.getTokens(), Range.first,
|
|
|
|
Range.second, [&](SelectionTree ST) {
|
|
|
|
Tweak::Selection S(Index, AST, Range.first,
|
|
|
|
Range.second, std::move(ST));
|
|
|
|
if (auto T = prepareTweak(TweakID, S)) {
|
|
|
|
Result = (*T)->apply(S);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
llvm::consumeError(T.takeError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2019-10-22 00:03:38 +08:00
|
|
|
MATCHER_P7(TweakIsAvailable, TweakID, Ctx, Header, ExtraArgs, ExtraFiles, Index,
|
|
|
|
FileName,
|
2019-08-02 17:12:39 +08:00
|
|
|
(TweakID + (negation ? " is unavailable" : " is available")).str()) {
|
|
|
|
std::string WrappedCode = wrap(Ctx, arg);
|
|
|
|
Annotations Input(WrappedCode);
|
|
|
|
TestTU TU;
|
2020-01-29 03:23:46 +08:00
|
|
|
TU.Filename = std::string(FileName);
|
2019-08-02 17:12:39 +08:00
|
|
|
TU.HeaderCode = Header;
|
2020-01-29 03:23:46 +08:00
|
|
|
TU.Code = std::string(Input.code());
|
2019-10-28 14:18:07 +08:00
|
|
|
TU.ExtraArgs = ExtraArgs;
|
2019-10-28 14:21:04 +08:00
|
|
|
TU.AdditionalFiles = std::move(ExtraFiles);
|
2019-08-02 17:12:39 +08:00
|
|
|
ParsedAST AST = TU.build();
|
2020-02-23 23:09:36 +08:00
|
|
|
auto Result = applyTweak(AST, Input, TweakID, Index);
|
|
|
|
// We only care if prepare() succeeded, but must handle Errors.
|
|
|
|
if (Result && !*Result)
|
|
|
|
consumeError(Result->takeError());
|
|
|
|
return Result.hasValue();
|
2019-08-02 17:12:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-09-06 15:49:40 +08:00
|
|
|
std::string TweakTest::apply(llvm::StringRef MarkedCode,
|
|
|
|
llvm::StringMap<std::string> *EditedFiles) const {
|
2019-08-02 17:12:39 +08:00
|
|
|
std::string WrappedCode = wrap(Context, MarkedCode);
|
|
|
|
Annotations Input(WrappedCode);
|
|
|
|
TestTU TU;
|
2020-01-29 03:23:46 +08:00
|
|
|
TU.Filename = std::string(FileName);
|
2019-08-02 17:12:39 +08:00
|
|
|
TU.HeaderCode = Header;
|
2019-09-06 15:49:40 +08:00
|
|
|
TU.AdditionalFiles = std::move(ExtraFiles);
|
2020-01-29 03:23:46 +08:00
|
|
|
TU.Code = std::string(Input.code());
|
2019-10-13 21:15:27 +08:00
|
|
|
TU.ExtraArgs = ExtraArgs;
|
2019-08-02 17:12:39 +08:00
|
|
|
ParsedAST AST = TU.build();
|
|
|
|
|
2020-02-23 23:09:36 +08:00
|
|
|
auto Result = applyTweak(AST, Input, TweakID, Index.get());
|
2019-12-17 02:07:49 +08:00
|
|
|
if (!Result)
|
2020-02-23 23:09:36 +08:00
|
|
|
return "unavailable";
|
|
|
|
if (!*Result)
|
|
|
|
return "fail: " + llvm::toString(Result->takeError());
|
|
|
|
const auto &Effect = **Result;
|
|
|
|
if ((*Result)->ShowMessage)
|
|
|
|
return "message:\n" + *Effect.ShowMessage;
|
|
|
|
if (Effect.ApplyEdits.empty())
|
2019-09-09 20:28:44 +08:00
|
|
|
return "no effect";
|
2019-10-26 00:40:01 +08:00
|
|
|
|
2019-09-06 15:49:40 +08:00
|
|
|
std::string EditedMainFile;
|
2020-02-23 23:09:36 +08:00
|
|
|
for (auto &It : Effect.ApplyEdits) {
|
2019-09-06 15:49:40 +08:00
|
|
|
auto NewText = It.second.apply();
|
|
|
|
if (!NewText)
|
|
|
|
return "bad edits: " + llvm::toString(NewText.takeError());
|
|
|
|
llvm::StringRef Unwrapped = unwrap(Context, *NewText);
|
|
|
|
if (It.first() == testPath(TU.Filename))
|
2020-01-29 03:23:46 +08:00
|
|
|
EditedMainFile = std::string(Unwrapped);
|
2019-09-06 15:49:40 +08:00
|
|
|
else {
|
|
|
|
if (!EditedFiles)
|
|
|
|
ADD_FAILURE() << "There were changes to additional files, but client "
|
|
|
|
"provided a nullptr for EditedFiles.";
|
|
|
|
else
|
2019-10-22 00:16:40 +08:00
|
|
|
EditedFiles->insert_or_assign(It.first(), Unwrapped.str());
|
2019-09-06 15:49:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return EditedMainFile;
|
2019-08-02 17:12:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
::testing::Matcher<llvm::StringRef> TweakTest::isAvailable() const {
|
2019-10-28 14:21:04 +08:00
|
|
|
return TweakIsAvailable(llvm::StringRef(TweakID), Context, Header, ExtraArgs,
|
2019-10-22 00:03:38 +08:00
|
|
|
ExtraFiles, Index.get(), FileName);
|
2019-08-02 17:12:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> TweakTest::expandCases(llvm::StringRef MarkedCode) {
|
|
|
|
Annotations Test(MarkedCode);
|
|
|
|
llvm::StringRef Code = Test.code();
|
|
|
|
std::vector<std::string> Cases;
|
|
|
|
for (const auto& Point : Test.points()) {
|
|
|
|
size_t Offset = llvm::cantFail(positionToOffset(Code, Point));
|
|
|
|
Cases.push_back((Code.substr(0, Offset) + "^" + Code.substr(Offset)).str());
|
|
|
|
}
|
|
|
|
for (const auto& Range : Test.ranges()) {
|
|
|
|
size_t Begin = llvm::cantFail(positionToOffset(Code, Range.start));
|
|
|
|
size_t End = llvm::cantFail(positionToOffset(Code, Range.end));
|
|
|
|
Cases.push_back((Code.substr(0, Begin) + "[[" +
|
|
|
|
Code.substr(Begin, End - Begin) + "]]" + Code.substr(End))
|
|
|
|
.str());
|
|
|
|
}
|
|
|
|
assert(!Cases.empty() && "No markings in MarkedCode?");
|
|
|
|
return Cases;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|