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 165 166 167 168 169 170 171 172 173 174 175 176
|
//===---- TransformerClangTidyCheckTest.cpp - clang-tidy ------------------===//
//
// 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 "../clang-tidy/utils/TransformerClangTidyCheck.h"
#include "ClangTidyTest.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Tooling/Refactoring/RangeSelector.h"
#include "clang/Tooling/Refactoring/Stencil.h"
#include "clang/Tooling/Refactoring/Transformer.h"
#include "gtest/gtest.h"
namespace clang {
namespace tidy {
namespace utils {
namespace {
using tooling::change;
using tooling::IncludeFormat;
using tooling::RewriteRule;
using tooling::text;
using tooling::stencil::cat;
// Invert the code of an if-statement, while maintaining its semantics.
RewriteRule invertIf() {
using namespace ::clang::ast_matchers;
using tooling::node;
using tooling::statement;
StringRef C = "C", T = "T", E = "E";
RewriteRule Rule = tooling::makeRule(
ifStmt(hasCondition(expr().bind(C)), hasThen(stmt().bind(T)),
hasElse(stmt().bind(E))),
change(
statement(RewriteRule::RootID),
cat("if(!(", node(C), ")) ", statement(E), " else ", statement(T))),
text("negate condition and reverse `then` and `else` branches"));
return Rule;
}
class IfInverterCheck : public TransformerClangTidyCheck {
public:
IfInverterCheck(StringRef Name, ClangTidyContext *Context)
: TransformerClangTidyCheck(invertIf(), Name, Context) {}
};
// Basic test of using a rewrite rule as a ClangTidy.
TEST(TransformerClangTidyCheckTest, Basic) {
const std::string Input = R"cc(
void log(const char* msg);
void foo() {
if (10 > 1.0)
log("oh no!");
else
log("ok");
}
)cc";
const std::string Expected = R"(
void log(const char* msg);
void foo() {
if(!(10 > 1.0)) log("ok"); else log("oh no!");
}
)";
EXPECT_EQ(Expected, test::runCheckOnCode<IfInverterCheck>(Input));
}
// A trivial rewrite-rule generator that requires Objective-C code.
Optional<RewriteRule> needsObjC(const LangOptions &LangOpts,
const ClangTidyCheck::OptionsView &Options) {
if (!LangOpts.ObjC)
return None;
return tooling::makeRule(clang::ast_matchers::functionDecl(),
change(cat("void changed() {}")), text("no message"));
}
class NeedsObjCCheck : public TransformerClangTidyCheck {
public:
NeedsObjCCheck(StringRef Name, ClangTidyContext *Context)
: TransformerClangTidyCheck(needsObjC, Name, Context) {}
};
// Verify that the check only rewrites the code when the input is Objective-C.
TEST(TransformerClangTidyCheckTest, DisableByLang) {
const std::string Input = "void log() {}";
EXPECT_EQ(Input,
test::runCheckOnCode<NeedsObjCCheck>(Input, nullptr, "input.cc"));
EXPECT_EQ("void changed() {}",
test::runCheckOnCode<NeedsObjCCheck>(Input, nullptr, "input.mm"));
}
// A trivial rewrite rule generator that checks config options.
Optional<RewriteRule> noSkip(const LangOptions &LangOpts,
const ClangTidyCheck::OptionsView &Options) {
if (Options.get("Skip", "false") == "true")
return None;
return tooling::makeRule(clang::ast_matchers::functionDecl(),
change(cat("void nothing()")), text("no message"));
}
class ConfigurableCheck : public TransformerClangTidyCheck {
public:
ConfigurableCheck(StringRef Name, ClangTidyContext *Context)
: TransformerClangTidyCheck(noSkip, Name, Context) {}
};
// Tests operation with config option "Skip" set to true and false.
TEST(TransformerClangTidyCheckTest, DisableByConfig) {
const std::string Input = "void log(int);";
const std::string Expected = "void nothing();";
ClangTidyOptions Options;
Options.CheckOptions["test-check-0.Skip"] = "true";
EXPECT_EQ(Input, test::runCheckOnCode<ConfigurableCheck>(
Input, nullptr, "input.cc", None, Options));
Options.CheckOptions["test-check-0.Skip"] = "false";
EXPECT_EQ(Expected, test::runCheckOnCode<ConfigurableCheck>(
Input, nullptr, "input.cc", None, Options));
}
RewriteRule replaceCall(IncludeFormat Format) {
using namespace ::clang::ast_matchers;
RewriteRule Rule = makeRule(callExpr(callee(functionDecl(hasName("f")))),
change(text("other()")), text("no message"));
addInclude(Rule, "clang/OtherLib.h", Format);
return Rule;
}
template <IncludeFormat Format>
class IncludeCheck : public TransformerClangTidyCheck {
public:
IncludeCheck(StringRef Name, ClangTidyContext *Context)
: TransformerClangTidyCheck(replaceCall(Format), Name, Context) {}
};
TEST(TransformerClangTidyCheckTest, AddIncludeQuoted) {
std::string Input = R"cc(
int f(int x);
int h(int x) { return f(x); }
)cc";
std::string Expected = R"cc(#include "clang/OtherLib.h"
int f(int x);
int h(int x) { return other(); }
)cc";
EXPECT_EQ(Expected,
test::runCheckOnCode<IncludeCheck<IncludeFormat::Quoted>>(Input));
}
TEST(TransformerClangTidyCheckTest, AddIncludeAngled) {
std::string Input = R"cc(
int f(int x);
int h(int x) { return f(x); }
)cc";
std::string Expected = R"cc(#include <clang/OtherLib.h>
int f(int x);
int h(int x) { return other(); }
)cc";
EXPECT_EQ(Expected,
test::runCheckOnCode<IncludeCheck<IncludeFormat::Angled>>(Input));
}
} // namespace
} // namespace utils
} // namespace tidy
} // namespace clang
|