Skip to content

Commit

Permalink
[NFC] Simplify IncludeInsertions appending to diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
njames93 committed Jun 19, 2020
1 parent 3bd7acf commit c3b4486
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 44 deletions.
13 changes: 5 additions & 8 deletions clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,11 @@ void PassByValueCheck::check(const MatchFinder::MatchResult &Result) {
// Use std::move in the initialization list.
Diag << FixItHint::CreateInsertion(Initializer->getRParenLoc(), ")")
<< FixItHint::CreateInsertion(
Initializer->getLParenLoc().getLocWithOffset(1), "std::move(");

if (auto IncludeFixit = Inserter->CreateIncludeInsertion(
Result.SourceManager->getFileID(Initializer->getSourceLocation()),
"utility",
/*IsAngled=*/true)) {
Diag << *IncludeFixit;
}
Initializer->getLParenLoc().getLocWithOffset(1), "std::move(")
<< Inserter->CreateIncludeInsertion(
Result.SourceManager->getFileID(Initializer->getSourceLocation()),
"utility",
/*IsAngled=*/true);
}

} // namespace modernize
Expand Down
14 changes: 6 additions & 8 deletions clang-tools-extra/clang-tidy/modernize/ReplaceAutoPtrCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,12 @@ void ReplaceAutoPtrCheck::check(const MatchFinder::MatchResult &Result) {
if (Range.isInvalid())
return;

auto Diag = diag(Range.getBegin(), "use std::move to transfer ownership")
<< FixItHint::CreateInsertion(Range.getBegin(), "std::move(")
<< FixItHint::CreateInsertion(Range.getEnd(), ")");

if (auto Fix =
Inserter->CreateIncludeInsertion(SM.getMainFileID(), "utility",
/*IsAngled=*/true))
Diag << *Fix;
auto Diag =
diag(Range.getBegin(), "use std::move to transfer ownership")
<< FixItHint::CreateInsertion(Range.getBegin(), "std::move(")
<< FixItHint::CreateInsertion(Range.getEnd(), ")")
<< Inserter->CreateIncludeInsertion(SM.getMainFileID(), "utility",
/*IsAngled=*/true);

return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,10 @@ void ReplaceRandomShuffleCheck::check(const MatchFinder::MatchResult &Result) {

Diag << FixItHint::CreateRemoval(MatchedDecl->getSourceRange());
Diag << FixItHint::CreateInsertion(MatchedDecl->getBeginLoc(), NewName);

if (Optional<FixItHint> IncludeFixit =
IncludeInserter->CreateIncludeInsertion(
Result.Context->getSourceManager().getFileID(
MatchedCallExpr->getBeginLoc()),
"random", /*IsAngled=*/true))
Diag << IncludeFixit.getValue();
Diag << IncludeInserter->CreateIncludeInsertion(
Result.Context->getSourceManager().getFileID(
MatchedCallExpr->getBeginLoc()),
"random", /*IsAngled=*/true);
}

} // namespace modernize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,9 @@ void TypePromotionInMathFnCheck::check(const MatchFinder::MatchResult &Result) {
// <math.h>, because the functions we're suggesting moving away from are all
// declared in <math.h>.
if (FnInCmath)
if (auto IncludeFixit = IncludeInserter->CreateIncludeInsertion(
Result.Context->getSourceManager().getFileID(Call->getBeginLoc()),
"cmath", /*IsAngled=*/true))
Diag << *IncludeFixit;
Diag << IncludeInserter->CreateIncludeInsertion(
Result.Context->getSourceManager().getFileID(Call->getBeginLoc()),
"cmath", /*IsAngled=*/true);
}

} // namespace performance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,10 @@ void UnnecessaryValueParamCheck::handleMoveFix(const ParmVarDecl &Var,
auto EndLoc = Lexer::getLocForEndOfToken(CopyArgument.getLocation(), 0, SM,
Context.getLangOpts());
Diag << FixItHint::CreateInsertion(CopyArgument.getBeginLoc(), "std::move(")
<< FixItHint::CreateInsertion(EndLoc, ")");
if (auto IncludeFixit = Inserter->CreateIncludeInsertion(
SM.getFileID(CopyArgument.getBeginLoc()), "utility",
/*IsAngled=*/true))
Diag << *IncludeFixit;
<< FixItHint::CreateInsertion(EndLoc, ")")
<< Inserter->CreateIncludeInsertion(
SM.getFileID(CopyArgument.getBeginLoc()), "utility",
/*IsAngled=*/true);
}

} // namespace performance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,9 @@ void TransformerClangTidyCheck::check(
Diag << FixItHint::CreateReplacement(T.Range, T.Replacement);

for (const auto &I : Case.AddedIncludes) {
auto &Header = I.first;
if (Optional<FixItHint> Fix = Inserter->CreateIncludeInsertion(
Result.SourceManager->getMainFileID(), Header,
/*IsAngled=*/I.second == transformer::IncludeFormat::Angled)) {
Diag << *Fix;
}
Diag << Inserter->CreateIncludeInsertion(
Result.SourceManager->getMainFileID(), I.first,
/*IsAngled=*/I.second == transformer::IncludeFormat::Angled);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,9 @@ class IncludeInserterCheckBase : public ClangTidyCheck {
void check(const ast_matchers::MatchFinder::MatchResult &Result) override {
auto Diag = diag(Result.Nodes.getNodeAs<DeclStmt>("stmt")->getBeginLoc(),
"foo, bar");
for (StringRef header : HeadersToInclude()) {
auto Fixit = Inserter->CreateIncludeInsertion(
Result.SourceManager->getMainFileID(), header, IsAngledInclude());
if (Fixit) {
Diag << *Fixit;
}
for (StringRef Header : HeadersToInclude()) {
Diag << Inserter->CreateIncludeInsertion(
Result.SourceManager->getMainFileID(), Header, IsAngledInclude());
}
}

Expand Down

0 comments on commit c3b4486

Please sign in to comment.