Skip to content

[clang-tidy][misc-include-cleaner]Avoid to insert same include header multiple times #65431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions clang-tools-extra/clang-tidy/misc/IncludeCleanerCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Regex.h"
Expand Down Expand Up @@ -199,6 +200,8 @@ void IncludeCleanerCheck::check(const MatchFinder::MatchResult &Result) {

tooling::HeaderIncludes HeaderIncludes(getCurrentMainFile(), Code,
FileStyle->IncludeStyle);
// Deduplicate insertions when running in bulk fix mode.
llvm::StringSet<> InsertedHeaders{};
for (const auto &Inc : Missing) {
std::string Spelling = include_cleaner::spellHeader(
{Inc.Missing, PP->getHeaderSearchInfo(), MainFile});
Expand All @@ -209,14 +212,18 @@ void IncludeCleanerCheck::check(const MatchFinder::MatchResult &Result) {
// main file.
if (auto Replacement =
HeaderIncludes.insert(llvm::StringRef{Spelling}.trim("\"<>"),
Angled, tooling::IncludeDirective::Include))
diag(SM->getSpellingLoc(Inc.SymRef.RefLocation),
"no header providing \"%0\" is directly included")
<< Inc.SymRef.Target.name()
<< FixItHint::CreateInsertion(
SM->getComposedLoc(SM->getMainFileID(),
Replacement->getOffset()),
Replacement->getReplacementText());
Angled, tooling::IncludeDirective::Include)) {
DiagnosticBuilder DB =
diag(SM->getSpellingLoc(Inc.SymRef.RefLocation),
"no header providing \"%0\" is directly included")
<< Inc.SymRef.Target.name();
if (areDiagsSelfContained() ||
InsertedHeaders.insert(Replacement->getReplacementText()).second) {
DB << FixItHint::CreateInsertion(
SM->getComposedLoc(SM->getMainFileID(), Replacement->getOffset()),
Replacement->getReplacementText());
}
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ Changes in existing checks
<clang-tidy/checks/misc/include-cleaner>` check by adding option
`DeduplicateFindings` to output one finding per symbol occurrence.

- Improved :doc:`misc-include-cleaner
<clang-tidy/checks/misc/include-cleaner>` check to avoid fixes insert
same include header multiple times.

- Improved :doc:`misc-redundant-expression
<clang-tidy/checks/misc/redundant-expression>` check to ignore
false-positives in unevaluated context (e.g., ``decltype``).
Expand Down
32 changes: 32 additions & 0 deletions clang-tools-extra/unittests/clang-tidy/IncludeCleanerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,38 @@ int QuxResult = qux();
)"}}));
}


TEST(IncludeCleanerCheckTest, MultipleTimeMissingInclude) {
const char *PreCode = R"(
#include "bar.h"

int BarResult = bar();
int BazResult_0 = baz_0();
int BazResult_1 = baz_1();
)";
const char *PostCode = R"(
#include "bar.h"
#include "baz.h"

int BarResult = bar();
int BazResult_0 = baz_0();
int BazResult_1 = baz_1();
)";

std::vector<ClangTidyError> Errors;
EXPECT_EQ(PostCode,
runCheckOnCode<IncludeCleanerCheck>(
PreCode, &Errors, "file.cpp", std::nullopt, ClangTidyOptions(),
{{"bar.h", R"(#pragma once
#include "baz.h"
int bar();
)"},
{"baz.h", R"(#pragma once
int baz_0();
int baz_1();
)"}}));
}

TEST(IncludeCleanerCheckTest, SystemMissingIncludes) {
const char *PreCode = R"(
#include <vector>
Expand Down