diff options
author | thegeorg <thegeorg@yandex-team.com> | 2024-03-13 13:58:24 +0300 |
---|---|---|
committer | thegeorg <thegeorg@yandex-team.com> | 2024-03-13 14:11:53 +0300 |
commit | 11a895b7e15d1c5a1f52706396b82e3f9db953cb (patch) | |
tree | fabc6d883b0f946151f61ae7865cee9f529a1fdd /contrib/libs/clang16/include/clang/Tooling/Inclusions | |
parent | 9685917341315774aad5733b1793b1e533a88bbb (diff) | |
download | ydb-11a895b7e15d1c5a1f52706396b82e3f9db953cb.tar.gz |
Export clang-format16 via ydblib project
6e6be3a95868fde888d801b7590af4044049563f
Diffstat (limited to 'contrib/libs/clang16/include/clang/Tooling/Inclusions')
3 files changed, 401 insertions, 0 deletions
diff --git a/contrib/libs/clang16/include/clang/Tooling/Inclusions/HeaderAnalysis.h b/contrib/libs/clang16/include/clang/Tooling/Inclusions/HeaderAnalysis.h new file mode 100644 index 0000000000..74b614c530 --- /dev/null +++ b/contrib/libs/clang16/include/clang/Tooling/Inclusions/HeaderAnalysis.h @@ -0,0 +1,57 @@ +#pragma once + +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-parameter" +#endif + +//===--- HeaderAnalysis.h -----------------------------------------*-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_TOOLING_INCLUSIONS_HEADER_ANALYSIS_H +#define LLVM_CLANG_TOOLING_INCLUSIONS_HEADER_ANALYSIS_H + +#include "llvm/ADT/StringRef.h" +#include <optional> + +namespace clang { +class FileEntry; +class SourceManager; +class HeaderSearch; + +namespace tooling { + +/// Returns true if the given physical file is a self-contained header. +/// +/// A header is considered self-contained if +// - it has a proper header guard or has been #imported or contains #import(s) +// - *and* it doesn't have a dont-include-me pattern. +/// +/// This function can be expensive as it may scan the source code to find out +/// dont-include-me pattern heuristically. +bool isSelfContainedHeader(const FileEntry *FE, const SourceManager &SM, + HeaderSearch &HeaderInfo); + +/// This scans the given source code to see if it contains #import(s). +bool codeContainsImports(llvm::StringRef Code); + +/// If Text begins an Include-What-You-Use directive, returns it. +/// Given "// IWYU pragma: keep", returns "keep". +/// Input is a null-terminated char* as provided by SM.getCharacterData(). +/// (This should not be StringRef as we do *not* want to scan for its length). +/// For multi-line comments, we return only the first line. +std::optional<llvm::StringRef> parseIWYUPragma(const char *Text); + +} // namespace tooling +} // namespace clang + +#endif // LLVM_CLANG_TOOLING_INCLUSIONS_HEADER_ANALYSIS_H + +#ifdef __GNUC__ +#pragma GCC diagnostic pop +#endif diff --git a/contrib/libs/clang16/include/clang/Tooling/Inclusions/HeaderIncludes.h b/contrib/libs/clang16/include/clang/Tooling/Inclusions/HeaderIncludes.h new file mode 100644 index 0000000000..c0f74b1cf5 --- /dev/null +++ b/contrib/libs/clang16/include/clang/Tooling/Inclusions/HeaderIncludes.h @@ -0,0 +1,153 @@ +#pragma once + +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-parameter" +#endif + +//===--- HeaderIncludes.h - Insert/Delete #includes for C++ code--*- 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_TOOLING_INCLUSIONS_HEADERINCLUDES_H +#define LLVM_CLANG_TOOLING_INCLUSIONS_HEADERINCLUDES_H + +#include "clang/Basic/SourceManager.h" +#include "clang/Tooling/Core/Replacement.h" +#include "clang/Tooling/Inclusions/IncludeStyle.h" +#include "llvm/Support/Path.h" +#include "llvm/Support/Regex.h" +#include <list> +#include <optional> +#include <unordered_map> + +namespace clang { +namespace tooling { + +/// This class manages priorities of C++ #include categories and calculates +/// priorities for headers. +/// FIXME(ioeric): move this class into implementation file when clang-format's +/// include sorting functions are also moved here. +class IncludeCategoryManager { +public: + IncludeCategoryManager(const IncludeStyle &Style, StringRef FileName); + + /// Returns the priority of the category which \p IncludeName belongs to. + /// If \p CheckMainHeader is true and \p IncludeName is a main header, returns + /// 0. Otherwise, returns the priority of the matching category or INT_MAX. + /// NOTE: this API is not thread-safe! + int getIncludePriority(StringRef IncludeName, bool CheckMainHeader) const; + int getSortIncludePriority(StringRef IncludeName, bool CheckMainHeader) const; + +private: + bool isMainHeader(StringRef IncludeName) const; + + const IncludeStyle Style; + bool IsMainFile; + std::string FileName; + SmallVector<llvm::Regex, 4> CategoryRegexs; +}; + +enum class IncludeDirective { Include, Import }; + +/// Generates replacements for inserting or deleting #include directives in a +/// file. +class HeaderIncludes { +public: + HeaderIncludes(llvm::StringRef FileName, llvm::StringRef Code, + const IncludeStyle &Style); + + /// Inserts an #include or #import directive of \p Header into the code. + /// If \p IsAngled is true, \p Header will be quoted with <> in the directive; + /// otherwise, it will be quoted with "". + /// + /// When searching for points to insert new header, this ignores #include's + /// after the #include block(s) in the beginning of a file to avoid inserting + /// headers into code sections where new #include's should not be added by + /// default. These code sections include: + /// - raw string literals (containing #include). + /// - #if blocks. + /// - Special #include's among declarations (e.g. functions). + /// + /// Returns a replacement that inserts the new header into a suitable #include + /// block of the same category. This respects the order of the existing + /// #includes in the block; if the existing #includes are not already sorted, + /// this will simply insert the #include in front of the first #include of the + /// same category in the code that should be sorted after \p IncludeName. If + /// \p IncludeName already exists (with exactly the same spelling), this + /// returns std::nullopt. + std::optional<tooling::Replacement> insert(llvm::StringRef Header, + bool IsAngled, + IncludeDirective Directive) const; + + /// Removes all existing #includes and #imports of \p Header quoted with <> if + /// \p IsAngled is true or "" if \p IsAngled is false. + /// This doesn't resolve the header file path; it only deletes #includes and + /// #imports with exactly the same spelling. + tooling::Replacements remove(llvm::StringRef Header, bool IsAngled) const; + + // Matches a whole #include directive. + static const llvm::Regex IncludeRegex; + +private: + struct Include { + Include(StringRef Name, tooling::Range R, IncludeDirective D) + : Name(Name), R(R), Directive(D) {} + + // An include header quoted with either <> or "". + std::string Name; + // The range of the whole line of include directive including any leading + // whitespaces and trailing comment. + tooling::Range R; + // Either #include or #import. + IncludeDirective Directive; + }; + + void addExistingInclude(Include IncludeToAdd, unsigned NextLineOffset); + + std::string FileName; + std::string Code; + + // Map from include name (quotation trimmed) to a list of existing includes + // (in case there are more than one) with the name in the current file. <x> + // and "x" will be treated as the same header when deleting #includes. + // std::list is used for pointers stability (see IncludesByPriority) + llvm::StringMap<std::list<Include>> ExistingIncludes; + + /// Map from priorities of #include categories to all #includes in the same + /// category. This is used to find #includes of the same category when + /// inserting new #includes. #includes in the same categories are sorted in + /// in the order they appear in the source file. + /// See comment for "FormatStyle::IncludeCategories" for details about include + /// priorities. + std::unordered_map<int, llvm::SmallVector<const Include *, 8>> + IncludesByPriority; + + int FirstIncludeOffset; + // All new headers should be inserted after this offset (e.g. after header + // guards, file comment). + unsigned MinInsertOffset; + // Max insertion offset in the original code. For example, we want to avoid + // inserting new #includes into the actual code section (e.g. after a + // declaration). + unsigned MaxInsertOffset; + IncludeCategoryManager Categories; + // Record the offset of the end of the last include in each category. + std::unordered_map<int, int> CategoryEndOffsets; + + // All possible priorities. + std::set<int> Priorities; +}; + +} // namespace tooling +} // namespace clang + +#endif // LLVM_CLANG_TOOLING_INCLUSIONS_HEADERINCLUDES_H + +#ifdef __GNUC__ +#pragma GCC diagnostic pop +#endif diff --git a/contrib/libs/clang16/include/clang/Tooling/Inclusions/IncludeStyle.h b/contrib/libs/clang16/include/clang/Tooling/Inclusions/IncludeStyle.h new file mode 100644 index 0000000000..a0c1f280b8 --- /dev/null +++ b/contrib/libs/clang16/include/clang/Tooling/Inclusions/IncludeStyle.h @@ -0,0 +1,191 @@ +#pragma once + +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-parameter" +#endif + +//===--- IncludeStyle.h - Style of C++ #include directives -------*- 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_TOOLING_INCLUSIONS_INCLUDESTYLE_H +#define LLVM_CLANG_TOOLING_INCLUSIONS_INCLUDESTYLE_H + +#include "llvm/Support/YAMLTraits.h" +#include <string> +#include <vector> + +namespace clang { +namespace tooling { + +/// Style for sorting and grouping C++ #include directives. +struct IncludeStyle { + /// Styles for sorting multiple ``#include`` blocks. + enum IncludeBlocksStyle { + /// Sort each ``#include`` block separately. + /// \code + /// #include "b.h" into #include "b.h" + /// + /// #include <lib/main.h> #include "a.h" + /// #include "a.h" #include <lib/main.h> + /// \endcode + IBS_Preserve, + /// Merge multiple ``#include`` blocks together and sort as one. + /// \code + /// #include "b.h" into #include "a.h" + /// #include "b.h" + /// #include <lib/main.h> #include <lib/main.h> + /// #include "a.h" + /// \endcode + IBS_Merge, + /// Merge multiple ``#include`` blocks together and sort as one. + /// Then split into groups based on category priority. See + /// ``IncludeCategories``. + /// \code + /// #include "b.h" into #include "a.h" + /// #include "b.h" + /// #include <lib/main.h> + /// #include "a.h" #include <lib/main.h> + /// \endcode + IBS_Regroup, + }; + + /// Dependent on the value, multiple ``#include`` blocks can be sorted + /// as one and divided based on category. + /// \version 6 + IncludeBlocksStyle IncludeBlocks; + + /// See documentation of ``IncludeCategories``. + struct IncludeCategory { + /// The regular expression that this category matches. + std::string Regex; + /// The priority to assign to this category. + int Priority; + /// The custom priority to sort before grouping. + int SortPriority; + /// If the regular expression is case sensitive. + bool RegexIsCaseSensitive; + bool operator==(const IncludeCategory &Other) const { + return Regex == Other.Regex && Priority == Other.Priority && + RegexIsCaseSensitive == Other.RegexIsCaseSensitive; + } + }; + + /// Regular expressions denoting the different ``#include`` categories + /// used for ordering ``#includes``. + /// + /// `POSIX extended + /// <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html>`_ + /// regular expressions are supported. + /// + /// These regular expressions are matched against the filename of an include + /// (including the <> or "") in order. The value belonging to the first + /// matching regular expression is assigned and ``#includes`` are sorted first + /// according to increasing category number and then alphabetically within + /// each category. + /// + /// If none of the regular expressions match, INT_MAX is assigned as + /// category. The main header for a source file automatically gets category 0. + /// so that it is generally kept at the beginning of the ``#includes`` + /// (https://llvm.org/docs/CodingStandards.html#include-style). However, you + /// can also assign negative priorities if you have certain headers that + /// always need to be first. + /// + /// There is a third and optional field ``SortPriority`` which can used while + /// ``IncludeBlocks = IBS_Regroup`` to define the priority in which + /// ``#includes`` should be ordered. The value of ``Priority`` defines the + /// order of ``#include blocks`` and also allows the grouping of ``#includes`` + /// of different priority. ``SortPriority`` is set to the value of + /// ``Priority`` as default if it is not assigned. + /// + /// Each regular expression can be marked as case sensitive with the field + /// ``CaseSensitive``, per default it is not. + /// + /// To configure this in the .clang-format file, use: + /// \code{.yaml} + /// IncludeCategories: + /// - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + /// Priority: 2 + /// SortPriority: 2 + /// CaseSensitive: true + /// - Regex: '^((<|")(gtest|gmock|isl|json)/)' + /// Priority: 3 + /// - Regex: '<[[:alnum:].]+>' + /// Priority: 4 + /// - Regex: '.*' + /// Priority: 1 + /// SortPriority: 0 + /// \endcode + /// \version 3.8 + std::vector<IncludeCategory> IncludeCategories; + + /// Specify a regular expression of suffixes that are allowed in the + /// file-to-main-include mapping. + /// + /// When guessing whether a #include is the "main" include (to assign + /// category 0, see above), use this regex of allowed suffixes to the header + /// stem. A partial match is done, so that: + /// - "" means "arbitrary suffix" + /// - "$" means "no suffix" + /// + /// For example, if configured to "(_test)?$", then a header a.h would be seen + /// as the "main" include in both a.cc and a_test.cc. + /// \version 3.9 + std::string IncludeIsMainRegex; + + /// Specify a regular expression for files being formatted + /// that are allowed to be considered "main" in the + /// file-to-main-include mapping. + /// + /// By default, clang-format considers files as "main" only when they end + /// with: ``.c``, ``.cc``, ``.cpp``, ``.c++``, ``.cxx``, ``.m`` or ``.mm`` + /// extensions. + /// For these files a guessing of "main" include takes place + /// (to assign category 0, see above). This config option allows for + /// additional suffixes and extensions for files to be considered as "main". + /// + /// For example, if this option is configured to ``(Impl\.hpp)$``, + /// then a file ``ClassImpl.hpp`` is considered "main" (in addition to + /// ``Class.c``, ``Class.cc``, ``Class.cpp`` and so on) and "main + /// include file" logic will be executed (with *IncludeIsMainRegex* setting + /// also being respected in later phase). Without this option set, + /// ``ClassImpl.hpp`` would not have the main include file put on top + /// before any other include. + /// \version 10 + std::string IncludeIsMainSourceRegex; +}; + +} // namespace tooling +} // namespace clang + +LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::IncludeStyle::IncludeCategory) + +namespace llvm { +namespace yaml { + +template <> +struct MappingTraits<clang::tooling::IncludeStyle::IncludeCategory> { + static void mapping(IO &IO, + clang::tooling::IncludeStyle::IncludeCategory &Category); +}; + +template <> +struct ScalarEnumerationTraits< + clang::tooling::IncludeStyle::IncludeBlocksStyle> { + static void + enumeration(IO &IO, clang::tooling::IncludeStyle::IncludeBlocksStyle &Value); +}; + +} // namespace yaml +} // namespace llvm + +#endif // LLVM_CLANG_TOOLING_INCLUSIONS_INCLUDESTYLE_H + +#ifdef __GNUC__ +#pragma GCC diagnostic pop +#endif |