Skip to content

[clang-tidy] Speed up misc-header-include-cycle #148757

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
76 changes: 36 additions & 40 deletions clang-tools-extra/clang-tidy/misc/HeaderIncludeCycleCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#include "clang/Lex/Preprocessor.h"
#include "llvm/Support/Regex.h"
#include <algorithm>
#include <deque>
#include <optional>
#include <vector>

using namespace clang::ast_matchers;

Expand All @@ -23,8 +23,8 @@ namespace clang::tidy::misc {
namespace {

struct Include {
FileID Id;
llvm::StringRef Name;
const FileEntry *File;
StringRef Name;
SourceLocation Loc;
};

Expand All @@ -50,31 +50,27 @@ class CyclicDependencyCallbacks : public PPCallbacks {
if (Reason != EnterFile && Reason != ExitFile)
return;

FileID Id = SM.getFileID(Loc);
const FileID Id = SM.getFileID(Loc);
if (Id.isInvalid())
return;

const FileEntry *NewFile = SM.getFileEntryForID(Id);
const FileEntry *PrevFile = SM.getFileEntryForID(PrevFID);

if (Reason == ExitFile) {
if ((Files.size() > 1U) && (Files.back().Id == PrevFID) &&
(Files[Files.size() - 2U].Id == Id))
if ((Files.size() > 1U) && (Files.back().File == PrevFile) &&
(Files[Files.size() - 2U].File == NewFile))
Files.pop_back();
return;
}

if (!Files.empty() && Files.back().Id == Id)
if (!Files.empty() && Files.back().File == NewFile)
return;

std::optional<llvm::StringRef> FilePath = SM.getNonBuiltinFilenameForID(Id);
llvm::StringRef FileName =
FilePath ? llvm::sys::path::filename(*FilePath) : llvm::StringRef();

if (!NextToEnter)
NextToEnter = Include{Id, FileName, SourceLocation()};

assert(NextToEnter->Name == FileName);
NextToEnter->Id = Id;
Files.emplace_back(*NextToEnter);
NextToEnter.reset();
const std::optional<StringRef> FilePath = SM.getNonBuiltinFilenameForID(Id);
const StringRef FileName =
FilePath ? llvm::sys::path::filename(*FilePath) : StringRef();
Files.push_back({NewFile, FileName, std::exchange(NextToEnter, {})});
}

void InclusionDirective(SourceLocation, const Token &, StringRef FilePath,
Expand All @@ -85,36 +81,26 @@ class CyclicDependencyCallbacks : public PPCallbacks {
if (FileType != clang::SrcMgr::C_User)
return;

llvm::StringRef FileName = llvm::sys::path::filename(FilePath);
NextToEnter = {FileID(), FileName, Range.getBegin()};
NextToEnter = Range.getBegin();

if (!File)
return;

FileID Id = SM.translateFile(*File);
if (Id.isInvalid())
return;

checkForDoubleInclude(Id, FileName, Range.getBegin());
}

void EndOfMainFile() override {
if (!Files.empty() && Files.back().Id == SM.getMainFileID())
Files.pop_back();

assert(Files.empty());
checkForDoubleInclude(&File->getFileEntry(),
llvm::sys::path::filename(FilePath),
Range.getBegin());
}

void checkForDoubleInclude(FileID Id, llvm::StringRef FileName,
void checkForDoubleInclude(const FileEntry *File, StringRef FileName,
SourceLocation Loc) {
auto It =
std::find_if(Files.rbegin(), Files.rend(),
[&](const Include &Entry) { return Entry.Id == Id; });
const auto It =
llvm::find_if(llvm::reverse(Files),
[&](const Include &Entry) { return Entry.File == File; });
if (It == Files.rend())
return;

const std::optional<StringRef> FilePath = SM.getNonBuiltinFilenameForID(Id);
if (!FilePath || isFileIgnored(*FilePath))
const StringRef FilePath = File->tryGetRealPathName();
if (FilePath.empty() || isFileIgnored(FilePath))
return;

if (It == Files.rbegin()) {
Expand Down Expand Up @@ -144,9 +130,19 @@ class CyclicDependencyCallbacks : public PPCallbacks {
});
}

#ifndef NDEBUG
void EndOfMainFile() override {
if (!Files.empty() &&
Files.back().File == SM.getFileEntryForID(SM.getMainFileID()))
Files.pop_back();

assert(Files.empty());
}
#endif

private:
std::deque<Include> Files;
std::optional<Include> NextToEnter;
std::vector<Include> Files;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deque were used because there are situation when you can have 200 includes and to avoid re-allocation.
You could consider using llvm::SmallVector with some pre-allocation, to speed things up even more.

SourceLocation NextToEnter;
HeaderIncludeCycleCheck &Check;
const SourceManager &SM;
std::vector<llvm::Regex> IgnoredFilesRegexes;
Expand Down
3 changes: 3 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ Changes in existing checks
`AnalyzePointers` option and fixing false positives when using const array
type.

- Improved :doc:`misc-header-include-cycle
<clang-tidy/checks/misc/header-include-cycle>` check performance.

- Improved :doc:`misc-include-cleaner
<clang-tidy/checks/misc/include-cleaner>` check by adding the options
`UnusedIncludes` and `MissingIncludes`, which specify whether the check should
Expand Down