2019-05-22 18:56:18 +00: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"
|
2022-06-29 10:38:14 +02:00
|
|
|
#include "clang/Basic/DiagnosticIDs.h"
|
2020-06-29 16:05:51 +01:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2019-05-27 08:09:02 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2023-01-07 20:02:20 -08:00
|
|
|
#include <optional>
|
2019-05-22 18:56:18 +00:00
|
|
|
|
2023-01-14 17:40:54 +00:00
|
|
|
namespace clang::tidy::utils {
|
2022-03-21 20:38:40 +00:00
|
|
|
using transformer::RewriteRuleWith;
|
2019-05-22 18:56:18 +00:00
|
|
|
|
2019-06-27 12:47:57 +00:00
|
|
|
#ifndef NDEBUG
|
2022-03-21 20:38:40 +00:00
|
|
|
static bool hasGenerator(const transformer::Generator<std::string> &G) {
|
|
|
|
return G != nullptr;
|
2019-06-26 16:04:38 +00:00
|
|
|
}
|
2019-06-27 12:47:57 +00:00
|
|
|
#endif
|
2019-06-26 16:04:38 +00:00
|
|
|
|
2022-03-21 20:38:40 +00:00
|
|
|
static void verifyRule(const RewriteRuleWith<std::string> &Rule) {
|
|
|
|
assert(llvm::all_of(Rule.Metadata, hasGenerator) &&
|
2020-11-16 14:30:21 +00:00
|
|
|
"clang-tidy checks must have an explanation by default;"
|
|
|
|
" explicitly provide an empty explanation if none is desired");
|
|
|
|
}
|
|
|
|
|
2022-05-04 18:47:08 +00:00
|
|
|
// If a string unintentionally containing '%' is passed as a diagnostic, Clang
|
|
|
|
// will claim the string is ill-formed and assert-fail. This function escapes
|
|
|
|
// such strings so they can be safely used in diagnostics.
|
|
|
|
std::string escapeForDiagnostic(std::string ToEscape) {
|
|
|
|
// Optimize for the common case that the string does not contain `%` at the
|
|
|
|
// cost of an extra scan over the string in the slow case.
|
|
|
|
auto Pos = ToEscape.find('%');
|
2023-08-26 16:53:49 +00:00
|
|
|
if (Pos == std::string::npos)
|
2022-05-04 18:47:08 +00:00
|
|
|
return ToEscape;
|
|
|
|
|
|
|
|
std::string Result;
|
|
|
|
Result.reserve(ToEscape.size());
|
|
|
|
// Convert position to a count.
|
|
|
|
++Pos;
|
|
|
|
Result.append(ToEscape, 0, Pos);
|
|
|
|
Result += '%';
|
|
|
|
|
|
|
|
for (auto N = ToEscape.size(); Pos < N; ++Pos) {
|
|
|
|
const char C = ToEscape.at(Pos);
|
|
|
|
Result += C;
|
|
|
|
if (C == '%')
|
|
|
|
Result += '%';
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2020-11-16 14:30:21 +00:00
|
|
|
TransformerClangTidyCheck::TransformerClangTidyCheck(StringRef Name,
|
|
|
|
ClangTidyContext *Context)
|
|
|
|
: ClangTidyCheck(Name, Context),
|
[clang-tidy] Add a Standalone diagnostics mode to clang-tidy
Adds a flag to `ClangTidyContext` that is used to indicate to checks that fixes will only be applied one at a time.
This is to indicate to checks that each fix emitted should not depend on any other fixes emitted across the translation unit.
I've currently implemented the `IncludeInserter`, `LoopConvertCheck` and `PreferMemberInitializerCheck` to use these support these modes.
Reasoning behind this is in use cases like `clangd` it's only possible to apply one fix at a time.
For include inserter checks, the include is only added once for the first diagnostic that requires it, this will result in subsequent fixes not having the included needed.
A similar issue is seen in the `PreferMemberInitializerCheck` where the `:` will only be added for the first member that needs fixing.
Fixes emitted in `StandaloneDiagsMode` will likely result in malformed code if they are applied all together, conversely fixes currently emitted may result in malformed code if they are applied one at a time.
For this reason invoking `clang-tidy` from the binary will always with `StandaloneDiagsMode` disabled, However using it as a library its possible to select the mode you wish to use, `clangd` always selects `StandaloneDiagsMode`.
This is an example of the current behaviour failing
```lang=c++
struct Foo {
int A, B;
Foo(int D, int E) {
A = D;
B = E; // Fix Here
}
};
```
Incorrectly transformed to:
```lang=c++
struct Foo {
int A, B;
Foo(int D, int E), B(E) {
A = D;
// Fix Here
}
};
```
In `StandaloneDiagsMode`, it gets transformed to:
```lang=c++
struct Foo {
int A, B;
Foo(int D, int E) : B(E) {
A = D;
// Fix Here
}
};
```
Reviewed By: sammccall
Differential Revision: https://reviews.llvm.org/D97121
2022-04-16 09:53:32 +01:00
|
|
|
Inserter(Options.getLocalOrGlobal("IncludeStyle", IncludeSorter::IS_LLVM),
|
|
|
|
areDiagsSelfContained()) {}
|
2020-11-16 14:30:21 +00:00
|
|
|
|
2019-06-26 16:04:38 +00: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(
|
2023-01-07 20:19:42 -08:00
|
|
|
std::function<std::optional<RewriteRuleWith<std::string>>(
|
|
|
|
const LangOptions &, const OptionsView &)>
|
2019-06-26 16:04:38 +00:00
|
|
|
MakeRule,
|
|
|
|
StringRef Name, ClangTidyContext *Context)
|
2020-11-16 14:30:21 +00:00
|
|
|
: TransformerClangTidyCheck(Name, Context) {
|
2023-01-07 20:19:42 -08:00
|
|
|
if (std::optional<RewriteRuleWith<std::string>> R =
|
2022-03-21 20:38:40 +00:00
|
|
|
MakeRule(getLangOpts(), Options))
|
2020-11-16 14:30:21 +00:00
|
|
|
setRule(std::move(*R));
|
2019-06-26 16:04:38 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 20:38:40 +00:00
|
|
|
TransformerClangTidyCheck::TransformerClangTidyCheck(
|
|
|
|
RewriteRuleWith<std::string> R, StringRef Name, ClangTidyContext *Context)
|
2020-11-16 14:30:21 +00:00
|
|
|
: TransformerClangTidyCheck(Name, Context) {
|
|
|
|
setRule(std::move(R));
|
|
|
|
}
|
|
|
|
|
2022-03-21 20:38:40 +00:00
|
|
|
void TransformerClangTidyCheck::setRule(
|
|
|
|
transformer::RewriteRuleWith<std::string> R) {
|
2020-11-16 14:30:21 +00:00
|
|
|
verifyRule(R);
|
|
|
|
Rule = std::move(R);
|
2019-05-24 16:32:03 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 13:25:07 +00:00
|
|
|
void TransformerClangTidyCheck::registerPPCallbacks(
|
|
|
|
const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
|
2020-08-11 13:45:08 +00:00
|
|
|
Inserter.registerPreprocessor(PP);
|
2019-07-02 13:25:07 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 18:56:18 +00:00
|
|
|
void TransformerClangTidyCheck::registerMatchers(
|
|
|
|
ast_matchers::MatchFinder *Finder) {
|
2020-11-16 14:30:21 +00:00
|
|
|
if (!Rule.Cases.empty())
|
|
|
|
for (auto &Matcher : transformer::detail::buildMatchers(Rule))
|
2019-08-13 14:48:13 +00:00
|
|
|
Finder->addDynamicMatcher(Matcher, this);
|
2019-05-22 18:56:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TransformerClangTidyCheck::check(
|
|
|
|
const ast_matchers::MatchFinder::MatchResult &Result) {
|
|
|
|
if (Result.Context->getDiagnostics().hasErrorOccurred())
|
|
|
|
return;
|
|
|
|
|
2022-03-21 20:38:40 +00:00
|
|
|
size_t I = transformer::detail::findSelectedCase(Result, Rule);
|
|
|
|
Expected<SmallVector<transformer::Edit, 1>> Edits =
|
|
|
|
Rule.Cases[I].Edits(Result);
|
2020-04-03 13:10:51 -04:00
|
|
|
if (!Edits) {
|
|
|
|
llvm::errs() << "Rewrite failed: " << llvm::toString(Edits.takeError())
|
|
|
|
<< "\n";
|
2019-05-22 18:56:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No rewrite applied, but no error encountered either.
|
2020-04-03 13:10:51 -04:00
|
|
|
if (Edits->empty())
|
2019-05-22 18:56:18 +00:00
|
|
|
return;
|
|
|
|
|
2022-03-21 20:38:40 +00:00
|
|
|
Expected<std::string> Explanation = Rule.Metadata[I]->eval(Result);
|
2019-05-24 16:32:03 +00:00
|
|
|
if (!Explanation) {
|
|
|
|
llvm::errs() << "Error in explanation: "
|
|
|
|
<< llvm::toString(Explanation.takeError()) << "\n";
|
|
|
|
return;
|
2019-05-22 18:56:18 +00:00
|
|
|
}
|
2019-08-26 15:17:29 +00:00
|
|
|
|
|
|
|
// Associate the diagnostic with the location of the first change.
|
2022-06-29 10:38:14 +02:00
|
|
|
{
|
|
|
|
DiagnosticBuilder Diag =
|
|
|
|
diag((*Edits)[0].Range.getBegin(), escapeForDiagnostic(*Explanation));
|
|
|
|
for (const auto &T : *Edits) {
|
|
|
|
switch (T.Kind) {
|
|
|
|
case transformer::EditKind::Range:
|
|
|
|
Diag << FixItHint::CreateReplacement(T.Range, T.Replacement);
|
|
|
|
break;
|
|
|
|
case transformer::EditKind::AddInclude:
|
|
|
|
Diag << Inserter.createIncludeInsertion(
|
|
|
|
Result.SourceManager->getFileID(T.Range.getBegin()), T.Replacement);
|
|
|
|
break;
|
|
|
|
}
|
2020-08-11 13:45:08 +00:00
|
|
|
}
|
2022-06-29 10:38:14 +02:00
|
|
|
}
|
|
|
|
// Emit potential notes.
|
|
|
|
for (const auto &T : *Edits) {
|
|
|
|
if (!T.Note.empty()) {
|
|
|
|
diag(T.Range.getBegin(), escapeForDiagnostic(T.Note),
|
|
|
|
DiagnosticIDs::Note);
|
|
|
|
}
|
|
|
|
}
|
2019-05-22 18:56:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-16 01:14:55 +01:00
|
|
|
void TransformerClangTidyCheck::storeOptions(
|
|
|
|
ClangTidyOptions::OptionMap &Opts) {
|
2020-07-27 12:48:53 +01:00
|
|
|
Options.store(Opts, "IncludeStyle", Inserter.getStyle());
|
2020-05-16 01:14:55 +01:00
|
|
|
}
|
|
|
|
|
2023-01-14 17:40:54 +00:00
|
|
|
} // namespace clang::tidy::utils
|