diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2018-07-28 11:06:01 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2018-07-28 11:06:01 +0000 |
commit | 486754660bb926339aefcf012a3f848592babb8b (patch) | |
tree | ecdbc446c9876f4f120f701c243373cd3cb43db3 /include/clang/Tooling/Inclusions | |
parent | 55e6d896ad333f07bb3b1ba487df214fc268a4ab (diff) | |
download | src-486754660bb926339aefcf012a3f848592babb8b.tar.gz src-486754660bb926339aefcf012a3f848592babb8b.zip |
Vendor import of clang trunk r338150:vendor/clang/clang-trunk-r338150
Notes
Notes:
svn path=/vendor/clang/dist/; revision=336815
svn path=/vendor/clang/clang-trunk-r338150/; revision=336816; tag=vendor/clang/clang-trunk-r338150
Diffstat (limited to 'include/clang/Tooling/Inclusions')
-rw-r--r-- | include/clang/Tooling/Inclusions/HeaderIncludes.h | 137 | ||||
-rw-r--r-- | include/clang/Tooling/Inclusions/IncludeStyle.h | 139 |
2 files changed, 276 insertions, 0 deletions
diff --git a/include/clang/Tooling/Inclusions/HeaderIncludes.h b/include/clang/Tooling/Inclusions/HeaderIncludes.h new file mode 100644 index 000000000000..d99a3283168c --- /dev/null +++ b/include/clang/Tooling/Inclusions/HeaderIncludes.h @@ -0,0 +1,137 @@ +//===--- HeaderIncludes.h - Insert/Delete #includes for C++ code--*- C++-*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#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 <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; + +private: + bool isMainHeader(StringRef IncludeName) const; + + const IncludeStyle Style; + bool IsMainFile; + std::string FileName; + // This refers to a substring in FileName. + StringRef FileStem; + // Regex is not thread-safe. + mutable SmallVector<llvm::Regex, 4> CategoryRegexs; +}; + +/// 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 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 None. + llvm::Optional<tooling::Replacement> insert(llvm::StringRef Header, + bool IsAngled) const; + + /// Removes all existing #includes 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 with + /// exactly the same spelling. + tooling::Replacements remove(llvm::StringRef Header, bool IsAngled) const; + +private: + struct Include { + Include(StringRef Name, tooling::Range R) : Name(Name), R(R) {} + + // An include header quoted with either <> or "". + std::string Name; + // The range of the whole line of include directive including any eading + // whitespaces and trailing comment. + tooling::Range R; + }; + + 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. + llvm::StringMap<llvm::SmallVector<Include, 1>> 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; + + // Matches a whole #include directive. + llvm::Regex IncludeRegex; +}; + + +} // namespace tooling +} // namespace clang + +#endif // LLVM_CLANG_TOOLING_INCLUSIONS_HEADERINCLUDES_H diff --git a/include/clang/Tooling/Inclusions/IncludeStyle.h b/include/clang/Tooling/Inclusions/IncludeStyle.h new file mode 100644 index 000000000000..a093dff27728 --- /dev/null +++ b/include/clang/Tooling/Inclusions/IncludeStyle.h @@ -0,0 +1,139 @@ +//===--- IncludeStyle.h - Style of C++ #include directives -------*- C++-*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#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. + 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; + bool operator==(const IncludeCategory &Other) const { + return Regex == Other.Regex && Priority == Other.Priority; + } + }; + + /// Regular expressions denoting the different ``#include`` categories + /// used for ordering ``#includes``. + /// + /// `POSIX extended + /// <http://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`` + /// (http://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. + /// + /// To configure this in the .clang-format file, use: + /// \code{.yaml} + /// IncludeCategories: + /// - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + /// Priority: 2 + /// - Regex: '^(<|"(gtest|gmock|isl|json)/)' + /// Priority: 3 + /// - Regex: '<[[:alnum:].]+>' + /// Priority: 4 + /// - Regex: '.*' + /// Priority: 1 + /// \endcode + 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. + std::string IncludeIsMainRegex; +}; + +} // 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 |