diff options
| author | Daniel Jasper <djasper@google.com> | 2016-03-21 14:11:27 +0000 |
|---|---|---|
| committer | Daniel Jasper <djasper@google.com> | 2016-03-21 14:11:27 +0000 |
| commit | 9c8ff3551ace965a3232fc4a1b53a75015a706cf (patch) | |
| tree | b79665461141afc99d5bf130a71991d5ac645aaa /clang/lib | |
| parent | 4aeab5fbf241a06fbd8fef719b94466da7f2f6d4 (diff) | |
| download | bcm5719-llvm-9c8ff3551ace965a3232fc4a1b53a75015a706cf.tar.gz bcm5719-llvm-9c8ff3551ace965a3232fc4a1b53a75015a706cf.zip | |
clang-format: Make include sorting's main include detection configurable.
This patch adds a regular expression to configure suffixes of an
included file to check whether it is the "main" include of the current
file. Previously, clang-format has allowed arbitrary suffixes on the
formatted file, which is still the case when no IncludeMainRegex is
specified.
llvm-svn: 263943
Diffstat (limited to 'clang/lib')
| -rw-r--r-- | clang/lib/Format/Format.cpp | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 683a57661e9..5d81ff5ec50 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -300,6 +300,7 @@ template <> struct MappingTraits<FormatStyle> { Style.ExperimentalAutoDetectBinPacking); IO.mapOptional("ForEachMacros", Style.ForEachMacros); IO.mapOptional("IncludeCategories", Style.IncludeCategories); + IO.mapOptional("IncludeIsMainRegex", Style.IncludeIsMainRegex); IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels); IO.mapOptional("IndentWidth", Style.IndentWidth); IO.mapOptional("IndentWrappedFunctionNames", @@ -517,6 +518,7 @@ FormatStyle getLLVMStyle() { LLVMStyle.IncludeCategories = {{"^\"(llvm|llvm-c|clang|clang-c)/", 2}, {"^(<|\"(gtest|isl|json)/)", 3}, {".*", 1}}; + LLVMStyle.IncludeIsMainRegex = "$"; LLVMStyle.IndentCaseLabels = false; LLVMStyle.IndentWrappedFunctionNames = false; LLVMStyle.IndentWidth = 2; @@ -569,6 +571,7 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) { GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; GoogleStyle.DerivePointerAlignment = true; GoogleStyle.IncludeCategories = {{"^<.*\\.h>", 1}, {"^<.*", 2}, {".*", 3}}; + GoogleStyle.IncludeIsMainRegex = "([-_](test|unittest))?$"; GoogleStyle.IndentCaseLabels = true; GoogleStyle.KeepEmptyLinesAtTheStartOfBlocks = false; GoogleStyle.ObjCSpaceAfterProperty = false; @@ -1961,8 +1964,12 @@ tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code, StringRef HeaderStem = llvm::sys::path::stem(IncludeName.drop_front(1).drop_back(1)); if (FileStem.startswith(HeaderStem)) { - Category = 0; - MainIncludeFound = true; + llvm::Regex MainIncludeRegex( + (HeaderStem + Style.IncludeIsMainRegex).str()); + if (MainIncludeRegex.match(FileStem)) { + Category = 0; + MainIncludeFound = true; + } } } IncludesInBlock.push_back({IncludeName, Line, Prev, Category}); |

