1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
//===-- 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 "SourceCode.h"
#include "TestTU.h"
#include "refactor/Tweak.h"
#include "llvm/Support/Error.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <string>
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;}"};
}
llvm_unreachable("Unknown TweakTest::CodeContext enum");
}
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;
}
llvm::Annotations::Range rangeOrPoint(const llvm::Annotations &A) {
if (A.points().size() != 0) {
assert(A.ranges().size() == 0 &&
"both a cursor point and a selection range were specified");
return {A.point(), A.point()};
}
return A.range();
}
// 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, llvm::Annotations::Range Range, StringRef TweakID,
const SymbolIndex *Index, llvm::vfs::FileSystem *FS) {
llvm::Optional<llvm::Expected<Tweak::Effect>> Result;
SelectionTree::createEach(AST.getASTContext(), AST.getTokens(), Range.Begin,
Range.End, [&](SelectionTree ST) {
Tweak::Selection S(Index, AST, Range.Begin,
Range.End, std::move(ST), FS);
if (auto T = prepareTweak(TweakID, S, nullptr)) {
Result = (*T)->apply(S);
return true;
} else {
llvm::consumeError(T.takeError());
return false;
}
});
return Result;
}
} // namespace
std::string TweakTest::apply(llvm::StringRef MarkedCode,
llvm::StringMap<std::string> *EditedFiles) const {
std::string WrappedCode = wrap(Context, MarkedCode);
llvm::Annotations Input(WrappedCode);
TestTU TU;
TU.Filename = std::string(FileName);
TU.HeaderCode = Header;
TU.AdditionalFiles = std::move(ExtraFiles);
TU.Code = std::string(Input.code());
TU.ExtraArgs = ExtraArgs;
ParsedAST AST = TU.build();
auto Result = applyTweak(
AST, rangeOrPoint(Input), TweakID, Index.get(),
&AST.getSourceManager().getFileManager().getVirtualFileSystem());
if (!Result)
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())
return "no effect";
std::string EditedMainFile;
for (auto &It : Effect.ApplyEdits) {
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))
EditedMainFile = std::string(Unwrapped);
else {
if (!EditedFiles)
ADD_FAILURE() << "There were changes to additional files, but client "
"provided a nullptr for EditedFiles.";
else
EditedFiles->insert_or_assign(It.first(), Unwrapped.str());
}
}
return EditedMainFile;
}
bool TweakTest::isAvailable(WrappedAST &AST,
llvm::Annotations::Range Range) const {
// Adjust range for wrapping offset.
Range.Begin += AST.second;
Range.End += AST.second;
auto Result = applyTweak(
AST.first, Range, TweakID, Index.get(),
&AST.first.getSourceManager().getFileManager().getVirtualFileSystem());
// We only care if prepare() succeeded, but must handle Errors.
if (Result && !*Result)
consumeError(Result->takeError());
return Result.has_value();
}
TweakTest::WrappedAST TweakTest::build(llvm::StringRef Code) const {
TestTU TU;
TU.Filename = std::string(FileName);
TU.HeaderCode = Header;
TU.Code = wrap(Context, Code);
TU.ExtraArgs = ExtraArgs;
TU.AdditionalFiles = std::move(ExtraFiles);
return {TU.build(), wrapping(Context).first.size()};
}
std::string TweakTest::decorate(llvm::StringRef Code, unsigned Point) {
return (Code.substr(0, Point) + "^" + Code.substr(Point)).str();
}
std::string TweakTest::decorate(llvm::StringRef Code,
llvm::Annotations::Range Range) {
return (Code.substr(0, Range.Begin) + "[[" +
Code.substr(Range.Begin, Range.End) + "]]" + Code.substr(Range.End))
.str();
}
} // namespace clangd
} // namespace clang
|