mirror of
https://github.com/llvm/llvm-project.git
synced 2025-04-25 06:36:06 +00:00
[clang-format] Move #include related style to libToolingCore
Summary: This will be shared by include insertion/deletion library. Reviewers: ilya-biryukov Reviewed By: ilya-biryukov Subscribers: mgorny, klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D46758 llvm-svn: 332284
This commit is contained in:
parent
d9f2873511
commit
9d92c027e8
@ -16,6 +16,7 @@
|
||||
#define LLVM_CLANG_FORMAT_FORMAT_H
|
||||
|
||||
#include "clang/Basic/LangOptions.h"
|
||||
#include "clang/Tooling/Core/IncludeStyle.h"
|
||||
#include "clang/Tooling/Core/Replacement.h"
|
||||
#include "llvm/ADT/ArrayRef.h"
|
||||
#include <system_error>
|
||||
@ -998,91 +999,7 @@ struct FormatStyle {
|
||||
/// For example: BOOST_FOREACH.
|
||||
std::vector<std::string> ForEachMacros;
|
||||
|
||||
/// 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``.
|
||||
///
|
||||
/// 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: '.*'
|
||||
/// 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;
|
||||
tooling::IncludeStyle IncludeStyle;
|
||||
|
||||
/// Indent case labels one level from the switch statement.
|
||||
///
|
||||
@ -1735,8 +1652,8 @@ struct FormatStyle {
|
||||
R.ExperimentalAutoDetectBinPacking &&
|
||||
FixNamespaceComments == R.FixNamespaceComments &&
|
||||
ForEachMacros == R.ForEachMacros &&
|
||||
IncludeBlocks == R.IncludeBlocks &&
|
||||
IncludeCategories == R.IncludeCategories &&
|
||||
IncludeStyle.IncludeBlocks == R.IncludeStyle.IncludeBlocks &&
|
||||
IncludeStyle.IncludeCategories == R.IncludeStyle.IncludeCategories &&
|
||||
IndentCaseLabels == R.IndentCaseLabels &&
|
||||
IndentPPDirectives == R.IndentPPDirectives &&
|
||||
IndentWidth == R.IndentWidth && Language == R.Language &&
|
||||
@ -1753,8 +1670,7 @@ struct FormatStyle {
|
||||
ObjCBlockIndentWidth == R.ObjCBlockIndentWidth &&
|
||||
ObjCSpaceAfterProperty == R.ObjCSpaceAfterProperty &&
|
||||
ObjCSpaceBeforeProtocolList == R.ObjCSpaceBeforeProtocolList &&
|
||||
PenaltyBreakAssignment ==
|
||||
R.PenaltyBreakAssignment &&
|
||||
PenaltyBreakAssignment == R.PenaltyBreakAssignment &&
|
||||
PenaltyBreakBeforeFirstCallParameter ==
|
||||
R.PenaltyBreakBeforeFirstCallParameter &&
|
||||
PenaltyBreakComment == R.PenaltyBreakComment &&
|
||||
|
133
clang/include/clang/Tooling/Core/IncludeStyle.h
Normal file
133
clang/include/clang/Tooling/Core/IncludeStyle.h
Normal file
@ -0,0 +1,133 @@
|
||||
//===--- 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_CORE_INCLUDESTYLE_H
|
||||
#define LLVM_CLANG_TOOLING_CORE_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``.
|
||||
///
|
||||
/// 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: '.*'
|
||||
/// 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_CORE_INCLUDESTYLE_H
|
@ -48,7 +48,6 @@
|
||||
|
||||
using clang::format::FormatStyle;
|
||||
|
||||
LLVM_YAML_IS_SEQUENCE_VECTOR(clang::format::FormatStyle::IncludeCategory)
|
||||
LLVM_YAML_IS_SEQUENCE_VECTOR(clang::format::FormatStyle::RawStringFormat)
|
||||
|
||||
namespace llvm {
|
||||
@ -372,9 +371,9 @@ template <> struct MappingTraits<FormatStyle> {
|
||||
Style.ExperimentalAutoDetectBinPacking);
|
||||
IO.mapOptional("FixNamespaceComments", Style.FixNamespaceComments);
|
||||
IO.mapOptional("ForEachMacros", Style.ForEachMacros);
|
||||
IO.mapOptional("IncludeBlocks", Style.IncludeBlocks);
|
||||
IO.mapOptional("IncludeCategories", Style.IncludeCategories);
|
||||
IO.mapOptional("IncludeIsMainRegex", Style.IncludeIsMainRegex);
|
||||
IO.mapOptional("IncludeBlocks", Style.IncludeStyle.IncludeBlocks);
|
||||
IO.mapOptional("IncludeCategories", Style.IncludeStyle.IncludeCategories);
|
||||
IO.mapOptional("IncludeIsMainRegex", Style.IncludeStyle.IncludeIsMainRegex);
|
||||
IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels);
|
||||
IO.mapOptional("IndentPPDirectives", Style.IndentPPDirectives);
|
||||
IO.mapOptional("IndentWidth", Style.IndentWidth);
|
||||
@ -456,21 +455,6 @@ template <> struct MappingTraits<FormatStyle::BraceWrappingFlags> {
|
||||
}
|
||||
};
|
||||
|
||||
template <> struct MappingTraits<FormatStyle::IncludeCategory> {
|
||||
static void mapping(IO &IO, FormatStyle::IncludeCategory &Category) {
|
||||
IO.mapOptional("Regex", Category.Regex);
|
||||
IO.mapOptional("Priority", Category.Priority);
|
||||
}
|
||||
};
|
||||
|
||||
template <> struct ScalarEnumerationTraits<FormatStyle::IncludeBlocksStyle> {
|
||||
static void enumeration(IO &IO, FormatStyle::IncludeBlocksStyle &Value) {
|
||||
IO.enumCase(Value, "Preserve", FormatStyle::IBS_Preserve);
|
||||
IO.enumCase(Value, "Merge", FormatStyle::IBS_Merge);
|
||||
IO.enumCase(Value, "Regroup", FormatStyle::IBS_Regroup);
|
||||
}
|
||||
};
|
||||
|
||||
template <> struct MappingTraits<FormatStyle::RawStringFormat> {
|
||||
static void mapping(IO &IO, FormatStyle::RawStringFormat &Format) {
|
||||
IO.mapOptional("Language", Format.Language);
|
||||
@ -639,11 +623,12 @@ FormatStyle getLLVMStyle() {
|
||||
LLVMStyle.ForEachMacros.push_back("foreach");
|
||||
LLVMStyle.ForEachMacros.push_back("Q_FOREACH");
|
||||
LLVMStyle.ForEachMacros.push_back("BOOST_FOREACH");
|
||||
LLVMStyle.IncludeCategories = {{"^\"(llvm|llvm-c|clang|clang-c)/", 2},
|
||||
{"^(<|\"(gtest|gmock|isl|json)/)", 3},
|
||||
{".*", 1}};
|
||||
LLVMStyle.IncludeIsMainRegex = "(Test)?$";
|
||||
LLVMStyle.IncludeBlocks = FormatStyle::IBS_Preserve;
|
||||
LLVMStyle.IncludeStyle.IncludeCategories = {
|
||||
{"^\"(llvm|llvm-c|clang|clang-c)/", 2},
|
||||
{"^(<|\"(gtest|gmock|isl|json)/)", 3},
|
||||
{".*", 1}};
|
||||
LLVMStyle.IncludeStyle.IncludeIsMainRegex = "(Test)?$";
|
||||
LLVMStyle.IncludeStyle.IncludeBlocks = tooling::IncludeStyle::IBS_Preserve;
|
||||
LLVMStyle.IndentCaseLabels = false;
|
||||
LLVMStyle.IndentPPDirectives = FormatStyle::PPDIS_None;
|
||||
LLVMStyle.IndentWrappedFunctionNames = false;
|
||||
@ -711,9 +696,9 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
|
||||
GoogleStyle.AlwaysBreakTemplateDeclarations = true;
|
||||
GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
|
||||
GoogleStyle.DerivePointerAlignment = true;
|
||||
GoogleStyle.IncludeCategories = {
|
||||
GoogleStyle.IncludeStyle.IncludeCategories = {
|
||||
{"^<ext/.*\\.h>", 2}, {"^<.*\\.h>", 1}, {"^<.*", 2}, {".*", 3}};
|
||||
GoogleStyle.IncludeIsMainRegex = "([-_](test|unittest))?$";
|
||||
GoogleStyle.IncludeStyle.IncludeIsMainRegex = "([-_](test|unittest))?$";
|
||||
GoogleStyle.IndentCaseLabels = true;
|
||||
GoogleStyle.KeepEmptyLinesAtTheStartOfBlocks = false;
|
||||
GoogleStyle.ObjCBinPackProtocolList = FormatStyle::BPS_Never;
|
||||
@ -1661,14 +1646,15 @@ static void sortCppIncludes(const FormatStyle &Style,
|
||||
// the entire block. Otherwise, no replacement is generated.
|
||||
if (Indices.size() == Includes.size() &&
|
||||
std::is_sorted(Indices.begin(), Indices.end()) &&
|
||||
Style.IncludeBlocks == FormatStyle::IBS_Preserve)
|
||||
Style.IncludeStyle.IncludeBlocks == tooling::IncludeStyle::IBS_Preserve)
|
||||
return;
|
||||
|
||||
std::string result;
|
||||
for (unsigned Index : Indices) {
|
||||
if (!result.empty()) {
|
||||
result += "\n";
|
||||
if (Style.IncludeBlocks == FormatStyle::IBS_Regroup &&
|
||||
if (Style.IncludeStyle.IncludeBlocks ==
|
||||
tooling::IncludeStyle::IBS_Regroup &&
|
||||
CurrentCategory != Includes[Index].Category)
|
||||
result += "\n";
|
||||
}
|
||||
@ -1697,7 +1683,7 @@ public:
|
||||
IncludeCategoryManager(const FormatStyle &Style, StringRef FileName)
|
||||
: Style(Style), FileName(FileName) {
|
||||
FileStem = llvm::sys::path::stem(FileName);
|
||||
for (const auto &Category : Style.IncludeCategories)
|
||||
for (const auto &Category : Style.IncludeStyle.IncludeCategories)
|
||||
CategoryRegexs.emplace_back(Category.Regex, llvm::Regex::IgnoreCase);
|
||||
IsMainFile = FileName.endswith(".c") || FileName.endswith(".cc") ||
|
||||
FileName.endswith(".cpp") || FileName.endswith(".c++") ||
|
||||
@ -1713,7 +1699,7 @@ public:
|
||||
int Ret = INT_MAX;
|
||||
for (unsigned i = 0, e = CategoryRegexs.size(); i != e; ++i)
|
||||
if (CategoryRegexs[i].match(IncludeName)) {
|
||||
Ret = Style.IncludeCategories[i].Priority;
|
||||
Ret = Style.IncludeStyle.IncludeCategories[i].Priority;
|
||||
break;
|
||||
}
|
||||
if (CheckMainHeader && IsMainFile && Ret > 0 && isMainHeader(IncludeName))
|
||||
@ -1730,7 +1716,7 @@ private:
|
||||
if (FileStem.startswith(HeaderStem) ||
|
||||
FileStem.startswith_lower(HeaderStem)) {
|
||||
llvm::Regex MainIncludeRegex(
|
||||
(HeaderStem + Style.IncludeIsMainRegex).str(),
|
||||
(HeaderStem + Style.IncludeStyle.IncludeIsMainRegex).str(),
|
||||
llvm::Regex::IgnoreCase);
|
||||
if (MainIncludeRegex.match(FileStem))
|
||||
return true;
|
||||
@ -1786,8 +1772,10 @@ tooling::Replacements sortCppIncludes(const FormatStyle &Style, StringRef Code,
|
||||
FormattingOff = false;
|
||||
|
||||
const bool EmptyLineSkipped =
|
||||
Trimmed.empty() && (Style.IncludeBlocks == FormatStyle::IBS_Merge ||
|
||||
Style.IncludeBlocks == FormatStyle::IBS_Regroup);
|
||||
Trimmed.empty() &&
|
||||
(Style.IncludeStyle.IncludeBlocks == tooling::IncludeStyle::IBS_Merge ||
|
||||
Style.IncludeStyle.IncludeBlocks ==
|
||||
tooling::IncludeStyle::IBS_Regroup);
|
||||
|
||||
if (!FormattingOff && !Line.endswith("\\")) {
|
||||
if (IncludeRegex.match(Line, &Matches)) {
|
||||
@ -2103,7 +2091,7 @@ HeaderIncludes::HeaderIncludes(StringRef FileName, StringRef Code,
|
||||
// Add 0 for main header and INT_MAX for headers that are not in any
|
||||
// category.
|
||||
Priorities = {0, INT_MAX};
|
||||
for (const auto &Category : Style.IncludeCategories)
|
||||
for (const auto &Category : Style.IncludeStyle.IncludeCategories)
|
||||
Priorities.insert(Category.Priority);
|
||||
SmallVector<StringRef, 32> Lines;
|
||||
Code.drop_front(MinInsertOffset).split(Lines, "\n");
|
||||
|
@ -1,6 +1,7 @@
|
||||
set(LLVM_LINK_COMPONENTS support)
|
||||
|
||||
add_clang_library(clangToolingCore
|
||||
IncludeStyle.cpp
|
||||
Lookup.cpp
|
||||
Replacement.cpp
|
||||
Diagnostic.cpp
|
||||
|
32
clang/lib/Tooling/Core/IncludeStyle.cpp
Normal file
32
clang/lib/Tooling/Core/IncludeStyle.cpp
Normal file
@ -0,0 +1,32 @@
|
||||
//===--- IncludeStyle.cpp - 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.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "clang/Tooling/Core/IncludeStyle.h"
|
||||
|
||||
|
||||
using clang::tooling::IncludeStyle;
|
||||
|
||||
namespace llvm {
|
||||
namespace yaml {
|
||||
|
||||
void MappingTraits<IncludeStyle::IncludeCategory>::mapping(
|
||||
IO &IO, IncludeStyle::IncludeCategory &Category) {
|
||||
IO.mapOptional("Regex", Category.Regex);
|
||||
IO.mapOptional("Priority", Category.Priority);
|
||||
}
|
||||
|
||||
void ScalarEnumerationTraits<IncludeStyle::IncludeBlocksStyle>::enumeration(
|
||||
IO &IO, IncludeStyle::IncludeBlocksStyle &Value) {
|
||||
IO.enumCase(Value, "Preserve", IncludeStyle::IBS_Preserve);
|
||||
IO.enumCase(Value, "Merge", IncludeStyle::IBS_Merge);
|
||||
IO.enumCase(Value, "Regroup", IncludeStyle::IBS_Regroup);
|
||||
}
|
||||
|
||||
} // namespace yaml
|
||||
} // namespace llvm
|
@ -10665,16 +10665,17 @@ TEST_F(FormatTest, ParsesConfiguration) {
|
||||
CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
|
||||
BoostAndQForeach);
|
||||
|
||||
Style.IncludeCategories.clear();
|
||||
std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2},
|
||||
{".*", 1}};
|
||||
Style.IncludeStyle.IncludeCategories.clear();
|
||||
std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = {
|
||||
{"abc/.*", 2}, {".*", 1}};
|
||||
CHECK_PARSE("IncludeCategories:\n"
|
||||
" - Regex: abc/.*\n"
|
||||
" Priority: 2\n"
|
||||
" - Regex: .*\n"
|
||||
" Priority: 1",
|
||||
IncludeCategories, ExpectedCategories);
|
||||
CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$");
|
||||
IncludeStyle.IncludeCategories, ExpectedCategories);
|
||||
CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex,
|
||||
"abc$");
|
||||
|
||||
Style.RawStringFormats.clear();
|
||||
std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
|
||||
|
@ -26,12 +26,12 @@ protected:
|
||||
|
||||
std::string sort(StringRef Code, std::vector<tooling::Range> Ranges,
|
||||
StringRef FileName = "input.cc") {
|
||||
auto Replaces = sortIncludes(Style, Code, Ranges, FileName);
|
||||
auto Replaces = sortIncludes(FmtStyle, Code, Ranges, FileName);
|
||||
Ranges = tooling::calculateRangesAfterReplacements(Replaces, Ranges);
|
||||
auto Sorted = applyAllReplacements(Code, Replaces);
|
||||
EXPECT_TRUE(static_cast<bool>(Sorted));
|
||||
auto Result = applyAllReplacements(
|
||||
*Sorted, reformat(Style, *Sorted, Ranges, FileName));
|
||||
*Sorted, reformat(FmtStyle, *Sorted, Ranges, FileName));
|
||||
EXPECT_TRUE(static_cast<bool>(Result));
|
||||
return *Result;
|
||||
}
|
||||
@ -41,12 +41,12 @@ protected:
|
||||
}
|
||||
|
||||
unsigned newCursor(llvm::StringRef Code, unsigned Cursor) {
|
||||
sortIncludes(Style, Code, GetCodeRange(Code), "input.cpp", &Cursor);
|
||||
sortIncludes(FmtStyle, Code, GetCodeRange(Code), "input.cpp", &Cursor);
|
||||
return Cursor;
|
||||
}
|
||||
|
||||
FormatStyle Style = getLLVMStyle();
|
||||
|
||||
FormatStyle FmtStyle = getLLVMStyle();
|
||||
tooling::IncludeStyle &Style = FmtStyle.IncludeStyle;
|
||||
};
|
||||
|
||||
TEST_F(SortIncludesTest, BasicSorting) {
|
||||
@ -74,11 +74,11 @@ TEST_F(SortIncludesTest, NoReplacementsForValidIncludes) {
|
||||
"#include <d>\n"
|
||||
"#include <e>\n"
|
||||
"#include <f>\n";
|
||||
EXPECT_TRUE(sortIncludes(Style, Code, GetCodeRange(Code), "a.cc").empty());
|
||||
EXPECT_TRUE(sortIncludes(FmtStyle, Code, GetCodeRange(Code), "a.cc").empty());
|
||||
}
|
||||
|
||||
TEST_F(SortIncludesTest, SortedIncludesInMultipleBlocksAreMerged) {
|
||||
Style.IncludeBlocks = FormatStyle::IBS_Merge;
|
||||
Style.IncludeBlocks = tooling::IncludeStyle::IBS_Merge;
|
||||
EXPECT_EQ("#include \"a.h\"\n"
|
||||
"#include \"b.h\"\n"
|
||||
"#include \"c.h\"\n",
|
||||
@ -88,7 +88,7 @@ TEST_F(SortIncludesTest, SortedIncludesInMultipleBlocksAreMerged) {
|
||||
"\n"
|
||||
"#include \"b.h\"\n"));
|
||||
|
||||
Style.IncludeBlocks = FormatStyle::IBS_Regroup;
|
||||
Style.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup;
|
||||
EXPECT_EQ("#include \"a.h\"\n"
|
||||
"#include \"b.h\"\n"
|
||||
"#include \"c.h\"\n",
|
||||
@ -119,7 +119,7 @@ TEST_F(SortIncludesTest, SupportClangFormatOff) {
|
||||
}
|
||||
|
||||
TEST_F(SortIncludesTest, IncludeSortingCanBeDisabled) {
|
||||
Style.SortIncludes = false;
|
||||
FmtStyle.SortIncludes = false;
|
||||
EXPECT_EQ("#include \"a.h\"\n"
|
||||
"#include \"c.h\"\n"
|
||||
"#include \"b.h\"\n",
|
||||
@ -182,7 +182,7 @@ TEST_F(SortIncludesTest, SortsLocallyInEachBlock) {
|
||||
}
|
||||
|
||||
TEST_F(SortIncludesTest, SortsAllBlocksWhenMerging) {
|
||||
Style.IncludeBlocks = FormatStyle::IBS_Merge;
|
||||
Style.IncludeBlocks = tooling::IncludeStyle::IBS_Merge;
|
||||
EXPECT_EQ("#include \"a.h\"\n"
|
||||
"#include \"b.h\"\n"
|
||||
"#include \"c.h\"\n",
|
||||
@ -202,7 +202,7 @@ TEST_F(SortIncludesTest, CommentsAlwaysSeparateGroups) {
|
||||
"// comment\n"
|
||||
"#include \"b.h\"\n"));
|
||||
|
||||
Style.IncludeBlocks = FormatStyle::IBS_Merge;
|
||||
Style.IncludeBlocks = tooling::IncludeStyle::IBS_Merge;
|
||||
EXPECT_EQ("#include \"a.h\"\n"
|
||||
"#include \"c.h\"\n"
|
||||
"// comment\n"
|
||||
@ -212,7 +212,7 @@ TEST_F(SortIncludesTest, CommentsAlwaysSeparateGroups) {
|
||||
"// comment\n"
|
||||
"#include \"b.h\"\n"));
|
||||
|
||||
Style.IncludeBlocks = FormatStyle::IBS_Regroup;
|
||||
Style.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup;
|
||||
EXPECT_EQ("#include \"a.h\"\n"
|
||||
"#include \"c.h\"\n"
|
||||
"// comment\n"
|
||||
@ -233,7 +233,7 @@ TEST_F(SortIncludesTest, HandlesAngledIncludesAsSeparateBlocks) {
|
||||
"#include \"c.h\"\n"
|
||||
"#include \"a.h\"\n"));
|
||||
|
||||
Style = getGoogleStyle(FormatStyle::LK_Cpp);
|
||||
FmtStyle = getGoogleStyle(FormatStyle::LK_Cpp);
|
||||
EXPECT_EQ("#include <b.h>\n"
|
||||
"#include <d.h>\n"
|
||||
"#include \"a.h\"\n"
|
||||
@ -245,7 +245,7 @@ TEST_F(SortIncludesTest, HandlesAngledIncludesAsSeparateBlocks) {
|
||||
}
|
||||
|
||||
TEST_F(SortIncludesTest, RegroupsAngledIncludesInSeparateBlocks) {
|
||||
Style.IncludeBlocks = FormatStyle::IBS_Regroup;
|
||||
Style.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup;
|
||||
EXPECT_EQ("#include \"a.h\"\n"
|
||||
"#include \"c.h\"\n"
|
||||
"\n"
|
||||
@ -345,7 +345,7 @@ TEST_F(SortIncludesTest, LeavesMainHeaderFirst) {
|
||||
|
||||
TEST_F(SortIncludesTest, RecognizeMainHeaderInAllGroups) {
|
||||
Style.IncludeIsMainRegex = "([-_](test|unittest))?$";
|
||||
Style.IncludeBlocks = FormatStyle::IBS_Merge;
|
||||
Style.IncludeBlocks = tooling::IncludeStyle::IBS_Merge;
|
||||
|
||||
EXPECT_EQ("#include \"c.h\"\n"
|
||||
"#include \"a.h\"\n"
|
||||
@ -359,7 +359,7 @@ TEST_F(SortIncludesTest, RecognizeMainHeaderInAllGroups) {
|
||||
|
||||
TEST_F(SortIncludesTest, MainHeaderIsSeparatedWhenRegroupping) {
|
||||
Style.IncludeIsMainRegex = "([-_](test|unittest))?$";
|
||||
Style.IncludeBlocks = FormatStyle::IBS_Regroup;
|
||||
Style.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup;
|
||||
|
||||
EXPECT_EQ("#include \"a.h\"\n"
|
||||
"\n"
|
||||
@ -417,7 +417,7 @@ TEST_F(SortIncludesTest, NegativePriorities) {
|
||||
|
||||
TEST_F(SortIncludesTest, PriorityGroupsAreSeparatedWhenRegroupping) {
|
||||
Style.IncludeCategories = {{".*important_os_header.*", -1}, {".*", 1}};
|
||||
Style.IncludeBlocks = FormatStyle::IBS_Regroup;
|
||||
Style.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup;
|
||||
|
||||
EXPECT_EQ("#include \"important_os_header.h\"\n"
|
||||
"\n"
|
||||
@ -467,7 +467,7 @@ TEST_F(SortIncludesTest, DeduplicateIncludes) {
|
||||
"#include <b>\n"
|
||||
"#include <c>\n"));
|
||||
|
||||
Style.IncludeBlocks = FormatStyle::IBS_Merge;
|
||||
Style.IncludeBlocks = tooling::IncludeStyle::IBS_Merge;
|
||||
EXPECT_EQ("#include <a>\n"
|
||||
"#include <b>\n"
|
||||
"#include <c>\n",
|
||||
@ -479,7 +479,7 @@ TEST_F(SortIncludesTest, DeduplicateIncludes) {
|
||||
"#include <b>\n"
|
||||
"#include <c>\n"));
|
||||
|
||||
Style.IncludeBlocks = FormatStyle::IBS_Regroup;
|
||||
Style.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup;
|
||||
EXPECT_EQ("#include <a>\n"
|
||||
"#include <b>\n"
|
||||
"#include <c>\n",
|
||||
@ -503,7 +503,7 @@ TEST_F(SortIncludesTest, SortAndDeduplicateIncludes) {
|
||||
"#include <c>\n"
|
||||
"#include <b>\n"));
|
||||
|
||||
Style.IncludeBlocks = FormatStyle::IBS_Merge;
|
||||
Style.IncludeBlocks = tooling::IncludeStyle::IBS_Merge;
|
||||
EXPECT_EQ("#include <a>\n"
|
||||
"#include <b>\n"
|
||||
"#include <c>\n",
|
||||
@ -515,7 +515,7 @@ TEST_F(SortIncludesTest, SortAndDeduplicateIncludes) {
|
||||
"#include <c>\n"
|
||||
"#include <b>\n"));
|
||||
|
||||
Style.IncludeBlocks = FormatStyle::IBS_Regroup;
|
||||
Style.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup;
|
||||
EXPECT_EQ("#include <a>\n"
|
||||
"#include <b>\n"
|
||||
"#include <c>\n",
|
||||
@ -573,7 +573,7 @@ TEST_F(SortIncludesTest, ValidAffactedRangesAfterDeduplicatingIncludes) {
|
||||
"\n"
|
||||
" int x ;";
|
||||
std::vector<tooling::Range> Ranges = {tooling::Range(0, 52)};
|
||||
auto Replaces = sortIncludes(Style, Code, Ranges, "input.cpp");
|
||||
auto Replaces = sortIncludes(FmtStyle, Code, Ranges, "input.cpp");
|
||||
Ranges = tooling::calculateRangesAfterReplacements(Replaces, Ranges);
|
||||
EXPECT_EQ(1u, Ranges.size());
|
||||
EXPECT_EQ(0u, Ranges[0].getOffset());
|
||||
|
Loading…
x
Reference in New Issue
Block a user