diff options
author | Piotr Zegar <me@piotrzegar.pl> | 2023-04-15 13:16:35 +0000 |
---|---|---|
committer | Piotr Zegar <me@piotrzegar.pl> | 2023-04-15 13:16:43 +0000 |
commit | 26e164fada3721b0939e6b7c2dfe1793d95229bb (patch) | |
tree | 8a1627d86650f8f2fb2239208748522ca12e55d6 /clang-tools-extra/clang-tidy | |
parent | a25b962a7f456171f01d6dbe8eb12524e4ece05f (diff) | |
download | llvm-26e164fada3721b0939e6b7c2dfe1793d95229bb.tar.gz |
Revert "[clang-tidy] Add misc-header-include-cycle check"
This reverts commit 9ece8753d5e6f49c31c2d988ef69225c036b25e0.
Diffstat (limited to 'clang-tools-extra/clang-tidy')
4 files changed, 0 insertions, 218 deletions
diff --git a/clang-tools-extra/clang-tidy/misc/CMakeLists.txt b/clang-tools-extra/clang-tidy/misc/CMakeLists.txt index f5ffd3cddba7..a72362906e0b 100644 --- a/clang-tools-extra/clang-tidy/misc/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/misc/CMakeLists.txt @@ -19,7 +19,6 @@ add_clang_library(clangTidyMiscModule ConstCorrectnessCheck.cpp DefinitionsInHeadersCheck.cpp ConfusableIdentifierCheck.cpp - HeaderIncludeCycleCheck.cpp MiscTidyModule.cpp MisleadingBidirectional.cpp MisleadingIdentifier.cpp diff --git a/clang-tools-extra/clang-tidy/misc/HeaderIncludeCycleCheck.cpp b/clang-tools-extra/clang-tidy/misc/HeaderIncludeCycleCheck.cpp deleted file mode 100644 index bebd6e390ed5..000000000000 --- a/clang-tools-extra/clang-tidy/misc/HeaderIncludeCycleCheck.cpp +++ /dev/null @@ -1,180 +0,0 @@ -//===--- HeaderIncludeCycleCheck.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 "HeaderIncludeCycleCheck.h" -#include "../utils/OptionsUtils.h" -#include "clang/AST/ASTContext.h" -#include "clang/ASTMatchers/ASTMatchFinder.h" -#include "clang/Lex/PPCallbacks.h" -#include "clang/Lex/Preprocessor.h" -#include "llvm/ADT/SmallVector.h" -#include "llvm/Support/Regex.h" -#include <algorithm> -#include <deque> -#include <optional> -#include <string> - -using namespace clang::ast_matchers; - -namespace clang::tidy::misc { - -namespace { - -struct Include { - FileID Id; - llvm::StringRef Name; - SourceLocation Loc; -}; - -class CyclicDependencyCallbacks : public PPCallbacks { -public: - CyclicDependencyCallbacks(HeaderIncludeCycleCheck &Check, - const SourceManager &SM, - const std::vector<StringRef> &IgnoredFilesList) - : Check(Check), SM(SM) { - IgnoredFilesRegexes.reserve(IgnoredFilesList.size()); - for (const StringRef &It : IgnoredFilesList) { - if (!It.empty()) - IgnoredFilesRegexes.emplace_back(It); - } - } - - void FileChanged(SourceLocation Loc, FileChangeReason Reason, - SrcMgr::CharacteristicKind FileType, - FileID PrevFID) override { - if (FileType != clang::SrcMgr::C_User) - return; - - if (Reason != EnterFile && Reason != ExitFile) - return; - - FileID Id = SM.getFileID(Loc); - if (Id.isInvalid()) - return; - - if (Reason == ExitFile) { - if ((Files.size() > 1U) && (Files.back().Id == PrevFID) && - (Files[Files.size() - 2U].Id == Id)) - Files.pop_back(); - return; - } - - if (!Files.empty() && Files.back().Id == Id) - 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(); - } - - void InclusionDirective(SourceLocation, const Token &, StringRef FilePath, - bool, CharSourceRange Range, - OptionalFileEntryRef File, StringRef, StringRef, - const Module *, - SrcMgr::CharacteristicKind FileType) override { - if (FileType != clang::SrcMgr::C_User) - return; - - llvm::StringRef FileName = llvm::sys::path::filename(FilePath); - NextToEnter = {FileID(), FileName, 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()); - } - - void checkForDoubleInclude(FileID Id, llvm::StringRef FileName, - SourceLocation Loc) { - auto It = - std::find_if(Files.rbegin(), Files.rend(), - [&](const Include &Entry) { return Entry.Id == Id; }); - if (It == Files.rend()) - return; - - const std::optional<StringRef> FilePath = SM.getNonBuiltinFilenameForID(Id); - if (!FilePath || isFileIgnored(*FilePath)) - return; - - if (It == Files.rbegin()) { - Check.diag(Loc, "direct self-inclusion of header file '%0'") << FileName; - return; - } - - Check.diag(Loc, "circular header file dependency detected while including " - "'%0', please check the include path") - << FileName; - - const bool IsIncludePathValid = - std::all_of(Files.rbegin(), It, [](const Include &Elem) { - return !Elem.Name.empty() && Elem.Loc.isValid(); - }); - - if (!IsIncludePathValid) - return; - - auto CurrentIt = Files.rbegin(); - do { - Check.diag(CurrentIt->Loc, "'%0' included from here", DiagnosticIDs::Note) - << CurrentIt->Name; - } while (CurrentIt++ != It); - } - - bool isFileIgnored(StringRef FileName) const { - return llvm::any_of(IgnoredFilesRegexes, [&](const llvm::Regex &It) { - return It.match(FileName); - }); - } - -private: - std::deque<Include> Files; - std::optional<Include> NextToEnter; - HeaderIncludeCycleCheck &Check; - const SourceManager &SM; - std::vector<llvm::Regex> IgnoredFilesRegexes; -}; - -} // namespace - -HeaderIncludeCycleCheck::HeaderIncludeCycleCheck(StringRef Name, - ClangTidyContext *Context) - : ClangTidyCheck(Name, Context), - IgnoredFilesList(utils::options::parseStringList( - Options.get("IgnoredFilesList", ""))) {} - -void HeaderIncludeCycleCheck::registerPPCallbacks( - const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) { - PP->addPPCallbacks( - std::make_unique<CyclicDependencyCallbacks>(*this, SM, IgnoredFilesList)); -} - -void HeaderIncludeCycleCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { - Options.store(Opts, "IgnoredFilesList", - utils::options::serializeStringList(IgnoredFilesList)); -} - -} // namespace clang::tidy::misc diff --git a/clang-tools-extra/clang-tidy/misc/HeaderIncludeCycleCheck.h b/clang-tools-extra/clang-tidy/misc/HeaderIncludeCycleCheck.h deleted file mode 100644 index 9a1a72399f42..000000000000 --- a/clang-tools-extra/clang-tidy/misc/HeaderIncludeCycleCheck.h +++ /dev/null @@ -1,34 +0,0 @@ -//===--- HeaderIncludeCycleCheck.h - clang-tidy -----------------*- C++ -*-===// -// -// 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 -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_HEADERINCLUDECYCLECHECK_H -#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_HEADERINCLUDECYCLECHECK_H - -#include "../ClangTidyCheck.h" -#include <vector> - -namespace clang::tidy::misc { - -/// Check detects cyclic #include dependencies between user-defined headers. -/// -/// For the user-facing documentation see: -/// http://clang.llvm.org/extra/clang-tidy/checks/misc/header-include-cycle.html -class HeaderIncludeCycleCheck : public ClangTidyCheck { -public: - HeaderIncludeCycleCheck(StringRef Name, ClangTidyContext *Context); - void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, - Preprocessor *ModuleExpanderPP) override; - void storeOptions(ClangTidyOptions::OptionMap &Opts) override; - -private: - const std::vector<StringRef> IgnoredFilesList; -}; - -} // namespace clang::tidy::misc - -#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_HEADERINCLUDECYCLECHECK_H diff --git a/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp b/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp index 5cac3d7e752a..2ec61f891263 100644 --- a/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp @@ -12,7 +12,6 @@ #include "ConfusableIdentifierCheck.h" #include "ConstCorrectnessCheck.h" #include "DefinitionsInHeadersCheck.h" -#include "HeaderIncludeCycleCheck.h" #include "MisleadingBidirectional.h" #include "MisleadingIdentifier.h" #include "MisplacedConstCheck.h" @@ -42,8 +41,6 @@ public: "misc-const-correctness"); CheckFactories.registerCheck<DefinitionsInHeadersCheck>( "misc-definitions-in-headers"); - CheckFactories.registerCheck<HeaderIncludeCycleCheck>( - "misc-header-include-cycle"); CheckFactories.registerCheck<MisleadingBidirectionalCheck>( "misc-misleading-bidirectional"); CheckFactories.registerCheck<MisleadingIdentifierCheck>( |