|
| 1 | +//===-- TweakTesting.cpp ------------------------------------------------*-===// |
| 2 | +// |
| 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 |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "TweakTesting.h" |
| 10 | + |
| 11 | +#include "Annotations.h" |
| 12 | +#include "refactor/Tweak.h" |
| 13 | +#include "SourceCode.h" |
| 14 | +#include "clang/Tooling/Core/Replacement.h" |
| 15 | +#include "llvm/Support/Error.h" |
| 16 | + |
| 17 | +namespace clang { |
| 18 | +namespace clangd { |
| 19 | +namespace { |
| 20 | +using Context = TweakTest::CodeContext; |
| 21 | + |
| 22 | +std::pair<llvm::StringRef, llvm::StringRef> wrapping(Context Ctx) { |
| 23 | + switch (Ctx) { |
| 24 | + case TweakTest::File: |
| 25 | + return {"",""}; |
| 26 | + case TweakTest::Function: |
| 27 | + return {"void wrapperFunction(){\n", "\n}"}; |
| 28 | + case TweakTest::Expression: |
| 29 | + return {"auto expressionWrapper(){return\n", "\n;}"}; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +std::string wrap(Context Ctx, llvm::StringRef Inner) { |
| 34 | + auto Wrapping = wrapping(Ctx); |
| 35 | + return (Wrapping.first + Inner + Wrapping.second).str(); |
| 36 | +} |
| 37 | + |
| 38 | +llvm::StringRef unwrap(Context Ctx, llvm::StringRef Outer) { |
| 39 | + auto Wrapping = wrapping(Ctx); |
| 40 | + // Unwrap only if the code matches the expected wrapping. |
| 41 | + // Don't allow the begin/end wrapping to overlap! |
| 42 | + if (Outer.startswith(Wrapping.first) && Outer.endswith(Wrapping.second) && |
| 43 | + Outer.size() >= Wrapping.first.size() + Wrapping.second.size()) |
| 44 | + return Outer.drop_front(Wrapping.first.size()).drop_back(Wrapping.second.size()); |
| 45 | + return Outer; |
| 46 | +} |
| 47 | + |
| 48 | +std::pair<unsigned, unsigned> rangeOrPoint(const Annotations &A) { |
| 49 | + Range SelectionRng; |
| 50 | + if (A.points().size() != 0) { |
| 51 | + assert(A.ranges().size() == 0 && |
| 52 | + "both a cursor point and a selection range were specified"); |
| 53 | + SelectionRng = Range{A.point(), A.point()}; |
| 54 | + } else { |
| 55 | + SelectionRng = A.range(); |
| 56 | + } |
| 57 | + return {cantFail(positionToOffset(A.code(), SelectionRng.start)), |
| 58 | + cantFail(positionToOffset(A.code(), SelectionRng.end))}; |
| 59 | +} |
| 60 | + |
| 61 | +MATCHER_P3(TweakIsAvailable, TweakID, Ctx, Header, |
| 62 | + (TweakID + (negation ? " is unavailable" : " is available")).str()) { |
| 63 | + std::string WrappedCode = wrap(Ctx, arg); |
| 64 | + Annotations Input(WrappedCode); |
| 65 | + auto Selection = rangeOrPoint(Input); |
| 66 | + TestTU TU; |
| 67 | + TU.HeaderCode = Header; |
| 68 | + TU.Code = Input.code(); |
| 69 | + ParsedAST AST = TU.build(); |
| 70 | + Tweak::Selection S(AST, Selection.first, Selection.second); |
| 71 | + auto PrepareResult = prepareTweak(TweakID, S); |
| 72 | + if (PrepareResult) |
| 73 | + return true; |
| 74 | + llvm::consumeError(PrepareResult.takeError()); |
| 75 | + return false; |
| 76 | +} |
| 77 | + |
| 78 | +} // namespace |
| 79 | + |
| 80 | +std::string TweakTest::apply(llvm::StringRef MarkedCode) const { |
| 81 | + std::string WrappedCode = wrap(Context, MarkedCode); |
| 82 | + Annotations Input(WrappedCode); |
| 83 | + auto Selection = rangeOrPoint(Input); |
| 84 | + TestTU TU; |
| 85 | + TU.HeaderCode = Header; |
| 86 | + TU.Code = Input.code(); |
| 87 | + ParsedAST AST = TU.build(); |
| 88 | + Tweak::Selection S(AST, Selection.first, Selection.second); |
| 89 | + |
| 90 | + auto T = prepareTweak(TweakID, S); |
| 91 | + if (!T) |
| 92 | + return "unavailable"; |
| 93 | + llvm::Expected<Tweak::Effect> Result = (*T)->apply(S); |
| 94 | + if (!Result) |
| 95 | + return "fail: " + llvm::toString(Result.takeError()); |
| 96 | + if (Result->ShowMessage) |
| 97 | + return "message:\n" + *Result->ShowMessage; |
| 98 | + if (Result->ApplyEdit) { |
| 99 | + if (auto NewText = |
| 100 | + tooling::applyAllReplacements(Input.code(), *Result->ApplyEdit)) |
| 101 | + return unwrap(Context, *NewText); |
| 102 | + else |
| 103 | + return "bad edits: " + llvm::toString(NewText.takeError()); |
| 104 | + } |
| 105 | + return "no effect"; |
| 106 | +} |
| 107 | + |
| 108 | +::testing::Matcher<llvm::StringRef> TweakTest::isAvailable() const { |
| 109 | + return TweakIsAvailable(llvm::StringRef(TweakID), Context, Header); |
| 110 | +} |
| 111 | + |
| 112 | +std::vector<std::string> TweakTest::expandCases(llvm::StringRef MarkedCode) { |
| 113 | + Annotations Test(MarkedCode); |
| 114 | + llvm::StringRef Code = Test.code(); |
| 115 | + std::vector<std::string> Cases; |
| 116 | + for (const auto& Point : Test.points()) { |
| 117 | + size_t Offset = llvm::cantFail(positionToOffset(Code, Point)); |
| 118 | + Cases.push_back((Code.substr(0, Offset) + "^" + Code.substr(Offset)).str()); |
| 119 | + } |
| 120 | + for (const auto& Range : Test.ranges()) { |
| 121 | + size_t Begin = llvm::cantFail(positionToOffset(Code, Range.start)); |
| 122 | + size_t End = llvm::cantFail(positionToOffset(Code, Range.end)); |
| 123 | + Cases.push_back((Code.substr(0, Begin) + "[[" + |
| 124 | + Code.substr(Begin, End - Begin) + "]]" + Code.substr(End)) |
| 125 | + .str()); |
| 126 | + } |
| 127 | + assert(!Cases.empty() && "No markings in MarkedCode?"); |
| 128 | + return Cases; |
| 129 | +} |
| 130 | + |
| 131 | +} // namespace clangd |
| 132 | +} // namespace clang |
0 commit comments