2019-05-23 02:56:18 +08:00
|
|
|
//===---------- TransformerClangTidyCheck.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 "TransformerClangTidyCheck.h"
|
2019-05-27 16:09:02 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2019-05-23 02:56:18 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
namespace utils {
|
|
|
|
using tooling::RewriteRule;
|
|
|
|
|
2019-06-27 20:47:57 +08:00
|
|
|
#ifndef NDEBUG
|
2019-06-27 00:04:38 +08:00
|
|
|
static bool hasExplanation(const RewriteRule::Case &C) {
|
|
|
|
return C.Explanation != nullptr;
|
|
|
|
}
|
2019-06-27 20:47:57 +08:00
|
|
|
#endif
|
2019-06-27 00:04:38 +08:00
|
|
|
|
|
|
|
// This constructor cannot dispatch to the simpler one (below), because, in
|
|
|
|
// order to get meaningful results from `getLangOpts` and `Options`, we need the
|
|
|
|
// `ClangTidyCheck()` constructor to have been called. If we were to dispatch,
|
|
|
|
// we would be accessing `getLangOpts` and `Options` before the underlying
|
|
|
|
// `ClangTidyCheck` instance was properly initialized.
|
|
|
|
TransformerClangTidyCheck::TransformerClangTidyCheck(
|
|
|
|
std::function<Optional<RewriteRule>(const LangOptions &,
|
|
|
|
const OptionsView &)>
|
|
|
|
MakeRule,
|
|
|
|
StringRef Name, ClangTidyContext *Context)
|
|
|
|
: ClangTidyCheck(Name, Context), Rule(MakeRule(getLangOpts(), Options)) {
|
|
|
|
if (Rule)
|
|
|
|
assert(llvm::all_of(Rule->Cases, hasExplanation) &&
|
|
|
|
"clang-tidy checks must have an explanation by default;"
|
|
|
|
" explicitly provide an empty explanation if none is desired");
|
|
|
|
}
|
|
|
|
|
2019-05-27 16:09:02 +08:00
|
|
|
TransformerClangTidyCheck::TransformerClangTidyCheck(RewriteRule R,
|
2019-05-25 00:32:03 +08:00
|
|
|
StringRef Name,
|
|
|
|
ClangTidyContext *Context)
|
|
|
|
: ClangTidyCheck(Name, Context), Rule(std::move(R)) {
|
2019-06-27 00:04:38 +08:00
|
|
|
assert(llvm::all_of(Rule->Cases, hasExplanation) &&
|
2019-05-27 16:09:02 +08:00
|
|
|
"clang-tidy checks must have an explanation by default;"
|
|
|
|
" explicitly provide an empty explanation if none is desired");
|
2019-05-25 00:32:03 +08:00
|
|
|
}
|
|
|
|
|
2019-07-02 21:25:07 +08:00
|
|
|
void TransformerClangTidyCheck::registerPPCallbacks(
|
|
|
|
const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
|
|
|
|
// Only allocate and register the IncludeInsert when some `Case` will add
|
|
|
|
// includes.
|
|
|
|
if (Rule && llvm::any_of(Rule->Cases, [](const RewriteRule::Case &C) {
|
|
|
|
return !C.AddedIncludes.empty();
|
|
|
|
})) {
|
2019-08-15 07:52:23 +08:00
|
|
|
Inserter = std::make_unique<IncludeInserter>(
|
2019-07-02 21:25:07 +08:00
|
|
|
SM, getLangOpts(), utils::IncludeSorter::IS_LLVM);
|
|
|
|
PP->addPPCallbacks(Inserter->CreatePPCallbacks());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 02:56:18 +08:00
|
|
|
void TransformerClangTidyCheck::registerMatchers(
|
|
|
|
ast_matchers::MatchFinder *Finder) {
|
2019-06-27 00:04:38 +08:00
|
|
|
if (Rule)
|
2019-08-13 22:48:13 +08:00
|
|
|
for (auto &Matcher : tooling::detail::buildMatchers(*Rule))
|
|
|
|
Finder->addDynamicMatcher(Matcher, this);
|
2019-05-23 02:56:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TransformerClangTidyCheck::check(
|
|
|
|
const ast_matchers::MatchFinder::MatchResult &Result) {
|
|
|
|
if (Result.Context->getDiagnostics().hasErrorOccurred())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Verify the existence and validity of the AST node that roots this rule.
|
|
|
|
const ast_matchers::BoundNodes::IDToNodeMap &NodesMap = Result.Nodes.getMap();
|
|
|
|
auto Root = NodesMap.find(RewriteRule::RootID);
|
|
|
|
assert(Root != NodesMap.end() && "Transformation failed: missing root node.");
|
|
|
|
SourceLocation RootLoc = Result.SourceManager->getExpansionLoc(
|
|
|
|
Root->second.getSourceRange().getBegin());
|
|
|
|
assert(RootLoc.isValid() && "Invalid location for Root node of match.");
|
|
|
|
|
2019-06-27 00:04:38 +08:00
|
|
|
assert(Rule && "check() should not fire if Rule is None");
|
|
|
|
RewriteRule::Case Case = tooling::detail::findSelectedCase(Result, *Rule);
|
2019-05-23 02:56:18 +08:00
|
|
|
Expected<SmallVector<tooling::detail::Transformation, 1>> Transformations =
|
|
|
|
tooling::detail::translateEdits(Result, Case.Edits);
|
|
|
|
if (!Transformations) {
|
|
|
|
llvm::errs() << "Rewrite failed: "
|
|
|
|
<< llvm::toString(Transformations.takeError()) << "\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No rewrite applied, but no error encountered either.
|
|
|
|
if (Transformations->empty())
|
|
|
|
return;
|
|
|
|
|
2019-05-25 00:32:03 +08:00
|
|
|
Expected<std::string> Explanation = Case.Explanation(Result);
|
|
|
|
if (!Explanation) {
|
|
|
|
llvm::errs() << "Error in explanation: "
|
|
|
|
<< llvm::toString(Explanation.takeError()) << "\n";
|
|
|
|
return;
|
2019-05-23 02:56:18 +08:00
|
|
|
}
|
2019-05-25 00:32:03 +08:00
|
|
|
DiagnosticBuilder Diag = diag(RootLoc, *Explanation);
|
2019-05-23 02:56:18 +08:00
|
|
|
for (const auto &T : *Transformations) {
|
|
|
|
Diag << FixItHint::CreateReplacement(T.Range, T.Replacement);
|
|
|
|
}
|
2019-07-02 21:25:07 +08:00
|
|
|
|
|
|
|
for (const auto &I : Case.AddedIncludes) {
|
|
|
|
auto &Header = I.first;
|
|
|
|
if (Optional<FixItHint> Fix = Inserter->CreateIncludeInsertion(
|
|
|
|
Result.SourceManager->getMainFileID(), Header,
|
|
|
|
/*IsAngled=*/I.second == tooling::IncludeFormat::Angled)) {
|
|
|
|
Diag << *Fix;
|
|
|
|
}
|
|
|
|
}
|
2019-05-23 02:56:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace utils
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|