Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1 | //===--- Format.cpp - Format C++ code -------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// \brief This file implements functions declared in Format.h. This will be |
| 12 | /// split into separate files as we go. |
| 13 | /// |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 16 | #define DEBUG_TYPE "format-formatter" |
| 17 | |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 18 | #include "ContinuationIndenter.h" |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 19 | #include "TokenAnnotator.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "UnwrappedLineParser.h" |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 21 | #include "WhitespaceManager.h" |
Daniel Jasper | 8a99945 | 2013-05-16 10:40:07 +0000 | [diff] [blame] | 22 | #include "clang/Basic/Diagnostic.h" |
Chandler Carruth | b99083e | 2013-01-02 10:28:36 +0000 | [diff] [blame] | 23 | #include "clang/Basic/SourceManager.h" |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 24 | #include "clang/Format/Format.h" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 25 | #include "clang/Lex/Lexer.h" |
Alexander Kornienko | 5262dd9 | 2013-03-27 11:52:18 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/STLExtras.h" |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Allocator.h" |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Debug.h" |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 29 | #include "llvm/Support/YAMLTraits.h" |
Edwin Vane | f4e12c8 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Path.h" |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 31 | #include <queue> |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 32 | #include <string> |
| 33 | |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 34 | namespace llvm { |
| 35 | namespace yaml { |
| 36 | template <> |
| 37 | struct ScalarEnumerationTraits<clang::format::FormatStyle::LanguageStandard> { |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 38 | static void enumeration(IO &IO, |
| 39 | clang::format::FormatStyle::LanguageStandard &Value) { |
Alexander Kornienko | 9321e87 | 2013-09-04 14:09:13 +0000 | [diff] [blame] | 40 | IO.enumCase(Value, "Cpp03", clang::format::FormatStyle::LS_Cpp03); |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 41 | IO.enumCase(Value, "C++03", clang::format::FormatStyle::LS_Cpp03); |
Alexander Kornienko | 9321e87 | 2013-09-04 14:09:13 +0000 | [diff] [blame] | 42 | IO.enumCase(Value, "Cpp11", clang::format::FormatStyle::LS_Cpp11); |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 43 | IO.enumCase(Value, "C++11", clang::format::FormatStyle::LS_Cpp11); |
| 44 | IO.enumCase(Value, "Auto", clang::format::FormatStyle::LS_Auto); |
| 45 | } |
| 46 | }; |
| 47 | |
Daniel Jasper | 1fb8d88 | 2013-05-14 09:30:02 +0000 | [diff] [blame] | 48 | template <> |
Alexander Kornienko | 3d9ffcf | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 49 | struct ScalarEnumerationTraits<clang::format::FormatStyle::UseTabStyle> { |
Alexander Kornienko | f0fc89c | 2013-10-14 00:46:35 +0000 | [diff] [blame] | 50 | static void enumeration(IO &IO, |
| 51 | clang::format::FormatStyle::UseTabStyle &Value) { |
Alexander Kornienko | 3d9ffcf | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 52 | IO.enumCase(Value, "Never", clang::format::FormatStyle::UT_Never); |
| 53 | IO.enumCase(Value, "false", clang::format::FormatStyle::UT_Never); |
| 54 | IO.enumCase(Value, "Always", clang::format::FormatStyle::UT_Always); |
| 55 | IO.enumCase(Value, "true", clang::format::FormatStyle::UT_Always); |
| 56 | IO.enumCase(Value, "ForIndentation", |
| 57 | clang::format::FormatStyle::UT_ForIndentation); |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | template <> |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 62 | struct ScalarEnumerationTraits<clang::format::FormatStyle::BraceBreakingStyle> { |
| 63 | static void |
| 64 | enumeration(IO &IO, clang::format::FormatStyle::BraceBreakingStyle &Value) { |
| 65 | IO.enumCase(Value, "Attach", clang::format::FormatStyle::BS_Attach); |
| 66 | IO.enumCase(Value, "Linux", clang::format::FormatStyle::BS_Linux); |
| 67 | IO.enumCase(Value, "Stroustrup", clang::format::FormatStyle::BS_Stroustrup); |
Manuel Klimek | e490705 | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 68 | IO.enumCase(Value, "Allman", clang::format::FormatStyle::BS_Allman); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 69 | } |
| 70 | }; |
| 71 | |
Daniel Jasper | eff18b9 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 72 | template <> |
| 73 | struct ScalarEnumerationTraits< |
| 74 | clang::format::FormatStyle::NamespaceIndentationKind> { |
| 75 | static void |
| 76 | enumeration(IO &IO, |
| 77 | clang::format::FormatStyle::NamespaceIndentationKind &Value) { |
| 78 | IO.enumCase(Value, "None", clang::format::FormatStyle::NI_None); |
| 79 | IO.enumCase(Value, "Inner", clang::format::FormatStyle::NI_Inner); |
| 80 | IO.enumCase(Value, "All", clang::format::FormatStyle::NI_All); |
| 81 | } |
| 82 | }; |
| 83 | |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 84 | template <> struct MappingTraits<clang::format::FormatStyle> { |
| 85 | static void mapping(llvm::yaml::IO &IO, clang::format::FormatStyle &Style) { |
Alexander Kornienko | dd25631 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 86 | if (IO.outputting()) { |
Alexander Kornienko | 4e65c98 | 2013-09-02 16:39:23 +0000 | [diff] [blame] | 87 | StringRef StylesArray[] = { "LLVM", "Google", "Chromium", |
| 88 | "Mozilla", "WebKit" }; |
Alexander Kornienko | dd25631 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 89 | ArrayRef<StringRef> Styles(StylesArray); |
| 90 | for (size_t i = 0, e = Styles.size(); i < e; ++i) { |
| 91 | StringRef StyleName(Styles[i]); |
Alexander Kornienko | 885f87b | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 92 | clang::format::FormatStyle PredefinedStyle; |
| 93 | if (clang::format::getPredefinedStyle(StyleName, &PredefinedStyle) && |
| 94 | Style == PredefinedStyle) { |
Alexander Kornienko | dd25631 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 95 | IO.mapOptional("# BasedOnStyle", StyleName); |
| 96 | break; |
| 97 | } |
| 98 | } |
| 99 | } else { |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 100 | StringRef BasedOnStyle; |
| 101 | IO.mapOptional("BasedOnStyle", BasedOnStyle); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 102 | if (!BasedOnStyle.empty()) |
Alexander Kornienko | 885f87b | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 103 | if (!clang::format::getPredefinedStyle(BasedOnStyle, &Style)) { |
| 104 | IO.setError(Twine("Unknown value for BasedOnStyle: ", BasedOnStyle)); |
| 105 | return; |
| 106 | } |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset); |
Daniel Jasper | 6315fec | 2013-08-13 10:58:30 +0000 | [diff] [blame] | 110 | IO.mapOptional("ConstructorInitializerIndentWidth", |
| 111 | Style.ConstructorInitializerIndentWidth); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 112 | IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft); |
Daniel Jasper | 893ea8d | 2013-07-31 23:55:15 +0000 | [diff] [blame] | 113 | IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 114 | IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine", |
| 115 | Style.AllowAllParametersOfDeclarationOnNextLine); |
| 116 | IO.mapOptional("AllowShortIfStatementsOnASingleLine", |
| 117 | Style.AllowShortIfStatementsOnASingleLine); |
Daniel Jasper | f11bbb9 | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 118 | IO.mapOptional("AllowShortLoopsOnASingleLine", |
| 119 | Style.AllowShortLoopsOnASingleLine); |
Daniel Jasper | bbc8776 | 2013-05-29 12:07:31 +0000 | [diff] [blame] | 120 | IO.mapOptional("AlwaysBreakTemplateDeclarations", |
| 121 | Style.AlwaysBreakTemplateDeclarations); |
Alexander Kornienko | 5631202 | 2013-07-04 12:02:44 +0000 | [diff] [blame] | 122 | IO.mapOptional("AlwaysBreakBeforeMultilineStrings", |
| 123 | Style.AlwaysBreakBeforeMultilineStrings); |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 124 | IO.mapOptional("BreakBeforeBinaryOperators", |
| 125 | Style.BreakBeforeBinaryOperators); |
| 126 | IO.mapOptional("BreakConstructorInitializersBeforeComma", |
| 127 | Style.BreakConstructorInitializersBeforeComma); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 128 | IO.mapOptional("BinPackParameters", Style.BinPackParameters); |
| 129 | IO.mapOptional("ColumnLimit", Style.ColumnLimit); |
| 130 | IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine", |
| 131 | Style.ConstructorInitializerAllOnOneLineOrOnePerLine); |
| 132 | IO.mapOptional("DerivePointerBinding", Style.DerivePointerBinding); |
Daniel Jasper | c7bd68f | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 133 | IO.mapOptional("ExperimentalAutoDetectBinPacking", |
| 134 | Style.ExperimentalAutoDetectBinPacking); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 135 | IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels); |
| 136 | IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep); |
Daniel Jasper | eff18b9 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 137 | IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 138 | IO.mapOptional("ObjCSpaceBeforeProtocolList", |
| 139 | Style.ObjCSpaceBeforeProtocolList); |
Alexander Kornienko | 2785b9a | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 140 | IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment); |
| 141 | IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString); |
Daniel Jasper | faec47b | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 142 | IO.mapOptional("PenaltyBreakFirstLessLess", |
| 143 | Style.PenaltyBreakFirstLessLess); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 144 | IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter); |
| 145 | IO.mapOptional("PenaltyReturnTypeOnItsOwnLine", |
| 146 | Style.PenaltyReturnTypeOnItsOwnLine); |
| 147 | IO.mapOptional("PointerBindsToType", Style.PointerBindsToType); |
| 148 | IO.mapOptional("SpacesBeforeTrailingComments", |
| 149 | Style.SpacesBeforeTrailingComments); |
Daniel Jasper | b5dc3f4 | 2013-07-16 18:22:10 +0000 | [diff] [blame] | 150 | IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 151 | IO.mapOptional("Standard", Style.Standard); |
Manuel Klimek | 07a64ec | 2013-05-13 08:42:42 +0000 | [diff] [blame] | 152 | IO.mapOptional("IndentWidth", Style.IndentWidth); |
Alexander Kornienko | 0b62cc3 | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 153 | IO.mapOptional("TabWidth", Style.TabWidth); |
Manuel Klimek | 7c9a93e | 2013-05-13 09:22:11 +0000 | [diff] [blame] | 154 | IO.mapOptional("UseTab", Style.UseTab); |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 155 | IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces); |
Manuel Klimek | a9a7f10 | 2013-06-21 17:25:42 +0000 | [diff] [blame] | 156 | IO.mapOptional("IndentFunctionDeclarationAfterType", |
| 157 | Style.IndentFunctionDeclarationAfterType); |
Daniel Jasper | 7df56bf | 2013-08-20 12:36:34 +0000 | [diff] [blame] | 158 | IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses); |
Daniel Jasper | 34f3d05 | 2013-08-21 08:39:01 +0000 | [diff] [blame] | 159 | IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses); |
Daniel Jasper | 7df56bf | 2013-08-20 12:36:34 +0000 | [diff] [blame] | 160 | IO.mapOptional("SpacesInCStyleCastParentheses", |
| 161 | Style.SpacesInCStyleCastParentheses); |
| 162 | IO.mapOptional("SpaceAfterControlStatementKeyword", |
| 163 | Style.SpaceAfterControlStatementKeyword); |
Daniel Jasper | 9b4de85 | 2013-09-25 15:15:02 +0000 | [diff] [blame] | 164 | IO.mapOptional("SpaceBeforeAssignmentOperators", |
| 165 | Style.SpaceBeforeAssignmentOperators); |
Daniel Jasper | c2827ec | 2013-10-18 10:38:14 +0000 | [diff] [blame] | 166 | IO.mapOptional("ContinuationIndentWidth", Style.ContinuationIndentWidth); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 167 | } |
| 168 | }; |
| 169 | } |
| 170 | } |
| 171 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 172 | namespace clang { |
| 173 | namespace format { |
| 174 | |
Daniel Jasper | faec47b | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 175 | void setDefaultPenalties(FormatStyle &Style) { |
Daniel Jasper | f546178 | 2013-08-28 10:03:58 +0000 | [diff] [blame] | 176 | Style.PenaltyBreakComment = 60; |
Daniel Jasper | 9637dda | 2013-07-15 14:33:14 +0000 | [diff] [blame] | 177 | Style.PenaltyBreakFirstLessLess = 120; |
Daniel Jasper | faec47b | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 178 | Style.PenaltyBreakString = 1000; |
| 179 | Style.PenaltyExcessCharacter = 1000000; |
| 180 | } |
| 181 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 182 | FormatStyle getLLVMStyle() { |
| 183 | FormatStyle LLVMStyle; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 184 | LLVMStyle.AccessModifierOffset = -2; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 185 | LLVMStyle.AlignEscapedNewlinesLeft = false; |
Daniel Jasper | 893ea8d | 2013-07-31 23:55:15 +0000 | [diff] [blame] | 186 | LLVMStyle.AlignTrailingComments = true; |
Daniel Jasper | f157960 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 187 | LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true; |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 188 | LLVMStyle.AllowShortIfStatementsOnASingleLine = false; |
Daniel Jasper | f11bbb9 | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 189 | LLVMStyle.AllowShortLoopsOnASingleLine = false; |
Alexander Kornienko | 5631202 | 2013-07-04 12:02:44 +0000 | [diff] [blame] | 190 | LLVMStyle.AlwaysBreakBeforeMultilineStrings = false; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 191 | LLVMStyle.AlwaysBreakTemplateDeclarations = false; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 192 | LLVMStyle.BinPackParameters = true; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 193 | LLVMStyle.BreakBeforeBinaryOperators = false; |
| 194 | LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach; |
| 195 | LLVMStyle.BreakConstructorInitializersBeforeComma = false; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 196 | LLVMStyle.ColumnLimit = 80; |
| 197 | LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false; |
Daniel Jasper | 6315fec | 2013-08-13 10:58:30 +0000 | [diff] [blame] | 198 | LLVMStyle.ConstructorInitializerIndentWidth = 4; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 199 | LLVMStyle.Cpp11BracedListStyle = false; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 200 | LLVMStyle.DerivePointerBinding = false; |
Daniel Jasper | c7bd68f | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 201 | LLVMStyle.ExperimentalAutoDetectBinPacking = false; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 202 | LLVMStyle.IndentCaseLabels = false; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 203 | LLVMStyle.IndentFunctionDeclarationAfterType = false; |
| 204 | LLVMStyle.IndentWidth = 2; |
Alexander Kornienko | 0b62cc3 | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 205 | LLVMStyle.TabWidth = 8; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 206 | LLVMStyle.MaxEmptyLinesToKeep = 1; |
Daniel Jasper | eff18b9 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 207 | LLVMStyle.NamespaceIndentation = FormatStyle::NI_None; |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 208 | LLVMStyle.ObjCSpaceBeforeProtocolList = true; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 209 | LLVMStyle.PointerBindsToType = false; |
| 210 | LLVMStyle.SpacesBeforeTrailingComments = 1; |
| 211 | LLVMStyle.Standard = FormatStyle::LS_Cpp03; |
Alexander Kornienko | 3d9ffcf | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 212 | LLVMStyle.UseTab = FormatStyle::UT_Never; |
Daniel Jasper | 7df56bf | 2013-08-20 12:36:34 +0000 | [diff] [blame] | 213 | LLVMStyle.SpacesInParentheses = false; |
| 214 | LLVMStyle.SpaceInEmptyParentheses = false; |
| 215 | LLVMStyle.SpacesInCStyleCastParentheses = false; |
| 216 | LLVMStyle.SpaceAfterControlStatementKeyword = true; |
Daniel Jasper | 9b4de85 | 2013-09-25 15:15:02 +0000 | [diff] [blame] | 217 | LLVMStyle.SpaceBeforeAssignmentOperators = true; |
Daniel Jasper | c2827ec | 2013-10-18 10:38:14 +0000 | [diff] [blame] | 218 | LLVMStyle.ContinuationIndentWidth = 4; |
Daniel Jasper | faec47b | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 219 | |
| 220 | setDefaultPenalties(LLVMStyle); |
| 221 | LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60; |
| 222 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 223 | return LLVMStyle; |
| 224 | } |
| 225 | |
| 226 | FormatStyle getGoogleStyle() { |
| 227 | FormatStyle GoogleStyle; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 228 | GoogleStyle.AccessModifierOffset = -1; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 229 | GoogleStyle.AlignEscapedNewlinesLeft = true; |
Daniel Jasper | 893ea8d | 2013-07-31 23:55:15 +0000 | [diff] [blame] | 230 | GoogleStyle.AlignTrailingComments = true; |
Daniel Jasper | f157960 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 231 | GoogleStyle.AllowAllParametersOfDeclarationOnNextLine = true; |
Daniel Jasper | 94d6ad7 | 2013-04-24 13:46:00 +0000 | [diff] [blame] | 232 | GoogleStyle.AllowShortIfStatementsOnASingleLine = true; |
Daniel Jasper | 1bee073 | 2013-05-23 18:05:18 +0000 | [diff] [blame] | 233 | GoogleStyle.AllowShortLoopsOnASingleLine = true; |
Alexander Kornienko | 5631202 | 2013-07-04 12:02:44 +0000 | [diff] [blame] | 234 | GoogleStyle.AlwaysBreakBeforeMultilineStrings = true; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 235 | GoogleStyle.AlwaysBreakTemplateDeclarations = true; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 236 | GoogleStyle.BinPackParameters = true; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 237 | GoogleStyle.BreakBeforeBinaryOperators = false; |
| 238 | GoogleStyle.BreakBeforeBraces = FormatStyle::BS_Attach; |
| 239 | GoogleStyle.BreakConstructorInitializersBeforeComma = false; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 240 | GoogleStyle.ColumnLimit = 80; |
| 241 | GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; |
Daniel Jasper | 6315fec | 2013-08-13 10:58:30 +0000 | [diff] [blame] | 242 | GoogleStyle.ConstructorInitializerIndentWidth = 4; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 243 | GoogleStyle.Cpp11BracedListStyle = true; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 244 | GoogleStyle.DerivePointerBinding = true; |
Daniel Jasper | c7bd68f | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 245 | GoogleStyle.ExperimentalAutoDetectBinPacking = false; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 246 | GoogleStyle.IndentCaseLabels = true; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 247 | GoogleStyle.IndentFunctionDeclarationAfterType = true; |
| 248 | GoogleStyle.IndentWidth = 2; |
Alexander Kornienko | 0b62cc3 | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 249 | GoogleStyle.TabWidth = 8; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 250 | GoogleStyle.MaxEmptyLinesToKeep = 1; |
Daniel Jasper | eff18b9 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 251 | GoogleStyle.NamespaceIndentation = FormatStyle::NI_None; |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 252 | GoogleStyle.ObjCSpaceBeforeProtocolList = false; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 253 | GoogleStyle.PointerBindsToType = true; |
| 254 | GoogleStyle.SpacesBeforeTrailingComments = 2; |
| 255 | GoogleStyle.Standard = FormatStyle::LS_Auto; |
Alexander Kornienko | 3d9ffcf | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 256 | GoogleStyle.UseTab = FormatStyle::UT_Never; |
Daniel Jasper | 7df56bf | 2013-08-20 12:36:34 +0000 | [diff] [blame] | 257 | GoogleStyle.SpacesInParentheses = false; |
| 258 | GoogleStyle.SpaceInEmptyParentheses = false; |
| 259 | GoogleStyle.SpacesInCStyleCastParentheses = false; |
| 260 | GoogleStyle.SpaceAfterControlStatementKeyword = true; |
Daniel Jasper | 9b4de85 | 2013-09-25 15:15:02 +0000 | [diff] [blame] | 261 | GoogleStyle.SpaceBeforeAssignmentOperators = true; |
Daniel Jasper | c2827ec | 2013-10-18 10:38:14 +0000 | [diff] [blame] | 262 | GoogleStyle.ContinuationIndentWidth = 4; |
Daniel Jasper | faec47b | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 263 | |
| 264 | setDefaultPenalties(GoogleStyle); |
| 265 | GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200; |
| 266 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 267 | return GoogleStyle; |
| 268 | } |
| 269 | |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 270 | FormatStyle getChromiumStyle() { |
| 271 | FormatStyle ChromiumStyle = getGoogleStyle(); |
Daniel Jasper | f157960 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 272 | ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false; |
Daniel Jasper | 94d6ad7 | 2013-04-24 13:46:00 +0000 | [diff] [blame] | 273 | ChromiumStyle.AllowShortIfStatementsOnASingleLine = false; |
Daniel Jasper | f11bbb9 | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 274 | ChromiumStyle.AllowShortLoopsOnASingleLine = false; |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 275 | ChromiumStyle.BinPackParameters = false; |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 276 | ChromiumStyle.DerivePointerBinding = false; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 277 | ChromiumStyle.Standard = FormatStyle::LS_Cpp03; |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 278 | return ChromiumStyle; |
| 279 | } |
| 280 | |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 281 | FormatStyle getMozillaStyle() { |
| 282 | FormatStyle MozillaStyle = getLLVMStyle(); |
| 283 | MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false; |
| 284 | MozillaStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; |
| 285 | MozillaStyle.DerivePointerBinding = true; |
| 286 | MozillaStyle.IndentCaseLabels = true; |
| 287 | MozillaStyle.ObjCSpaceBeforeProtocolList = false; |
| 288 | MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200; |
| 289 | MozillaStyle.PointerBindsToType = true; |
| 290 | return MozillaStyle; |
| 291 | } |
| 292 | |
Daniel Jasper | e05dc6d | 2013-07-24 13:10:59 +0000 | [diff] [blame] | 293 | FormatStyle getWebKitStyle() { |
| 294 | FormatStyle Style = getLLVMStyle(); |
Daniel Jasper | eff18b9 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 295 | Style.AccessModifierOffset = -4; |
Daniel Jasper | 893ea8d | 2013-07-31 23:55:15 +0000 | [diff] [blame] | 296 | Style.AlignTrailingComments = false; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 297 | Style.BreakBeforeBinaryOperators = true; |
Daniel Jasper | eff18b9 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 298 | Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 299 | Style.BreakConstructorInitializersBeforeComma = true; |
Daniel Jasper | eff18b9 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 300 | Style.ColumnLimit = 0; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 301 | Style.IndentWidth = 4; |
Daniel Jasper | eff18b9 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 302 | Style.NamespaceIndentation = FormatStyle::NI_Inner; |
Daniel Jasper | e05dc6d | 2013-07-24 13:10:59 +0000 | [diff] [blame] | 303 | Style.PointerBindsToType = true; |
| 304 | return Style; |
| 305 | } |
| 306 | |
Alexander Kornienko | 885f87b | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 307 | bool getPredefinedStyle(StringRef Name, FormatStyle *Style) { |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 308 | if (Name.equals_lower("llvm")) |
Alexander Kornienko | 885f87b | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 309 | *Style = getLLVMStyle(); |
| 310 | else if (Name.equals_lower("chromium")) |
| 311 | *Style = getChromiumStyle(); |
| 312 | else if (Name.equals_lower("mozilla")) |
| 313 | *Style = getMozillaStyle(); |
| 314 | else if (Name.equals_lower("google")) |
| 315 | *Style = getGoogleStyle(); |
Daniel Jasper | e05dc6d | 2013-07-24 13:10:59 +0000 | [diff] [blame] | 316 | else if (Name.equals_lower("webkit")) |
| 317 | *Style = getWebKitStyle(); |
Alexander Kornienko | 885f87b | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 318 | else |
| 319 | return false; |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 320 | |
Alexander Kornienko | 885f87b | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 321 | return true; |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | llvm::error_code parseConfiguration(StringRef Text, FormatStyle *Style) { |
Alexander Kornienko | 107db3c | 2013-05-20 15:18:01 +0000 | [diff] [blame] | 325 | if (Text.trim().empty()) |
| 326 | return llvm::make_error_code(llvm::errc::invalid_argument); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 327 | llvm::yaml::Input Input(Text); |
| 328 | Input >> *Style; |
| 329 | return Input.error(); |
| 330 | } |
| 331 | |
| 332 | std::string configurationAsText(const FormatStyle &Style) { |
| 333 | std::string Text; |
| 334 | llvm::raw_string_ostream Stream(Text); |
| 335 | llvm::yaml::Output Output(Stream); |
| 336 | // We use the same mapping method for input and output, so we need a non-const |
| 337 | // reference here. |
| 338 | FormatStyle NonConstStyle = Style; |
| 339 | Output << NonConstStyle; |
Alexander Kornienko | 2b6acb6 | 2013-05-13 12:56:35 +0000 | [diff] [blame] | 340 | return Stream.str(); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Craig Topper | 83f81d7 | 2013-06-30 22:29:28 +0000 | [diff] [blame] | 343 | namespace { |
| 344 | |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 345 | class NoColumnLimitFormatter { |
| 346 | public: |
Daniel Jasper | 34f3d05 | 2013-08-21 08:39:01 +0000 | [diff] [blame] | 347 | NoColumnLimitFormatter(ContinuationIndenter *Indenter) : Indenter(Indenter) {} |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 348 | |
| 349 | /// \brief Formats the line starting at \p State, simply keeping all of the |
| 350 | /// input's line breaking decisions. |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 351 | void format(unsigned FirstIndent, const AnnotatedLine *Line) { |
Daniel Jasper | b77d741 | 2013-09-06 07:54:20 +0000 | [diff] [blame] | 352 | LineState State = |
| 353 | Indenter->getInitialState(FirstIndent, Line, /*DryRun=*/false); |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 354 | while (State.NextToken != NULL) { |
| 355 | bool Newline = |
| 356 | Indenter->mustBreak(State) || |
| 357 | (Indenter->canBreak(State) && State.NextToken->NewlinesBefore > 0); |
| 358 | Indenter->addTokenToState(State, Newline, /*DryRun=*/false); |
| 359 | } |
| 360 | } |
Daniel Jasper | 34f3d05 | 2013-08-21 08:39:01 +0000 | [diff] [blame] | 361 | |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 362 | private: |
| 363 | ContinuationIndenter *Indenter; |
| 364 | }; |
| 365 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 366 | class UnwrappedLineFormatter { |
| 367 | public: |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 368 | UnwrappedLineFormatter(ContinuationIndenter *Indenter, |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 369 | WhitespaceManager *Whitespaces, |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 370 | const FormatStyle &Style, const AnnotatedLine &Line) |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 371 | : Indenter(Indenter), Whitespaces(Whitespaces), Style(Style), Line(Line), |
| 372 | Count(0) {} |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 373 | |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 374 | /// \brief Formats an \c UnwrappedLine and returns the penalty. |
| 375 | /// |
| 376 | /// If \p DryRun is \c false, directly applies the changes. |
| 377 | unsigned format(unsigned FirstIndent, bool DryRun = false) { |
Daniel Jasper | b77d741 | 2013-09-06 07:54:20 +0000 | [diff] [blame] | 378 | LineState State = Indenter->getInitialState(FirstIndent, &Line, DryRun); |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 379 | |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 380 | // If the ObjC method declaration does not fit on a line, we should format |
| 381 | // it with one arg per line. |
| 382 | if (Line.Type == LT_ObjCMethodDecl) |
| 383 | State.Stack.back().BreakBeforeParameter = true; |
| 384 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 385 | // Find best solution in solution space. |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 386 | return analyzeSolutionSpace(State, DryRun); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | private: |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 390 | /// \brief An edge in the solution space from \c Previous->State to \c State, |
| 391 | /// inserting a newline dependent on the \c NewLine. |
| 392 | struct StateNode { |
| 393 | StateNode(const LineState &State, bool NewLine, StateNode *Previous) |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 394 | : State(State), NewLine(NewLine), Previous(Previous) {} |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 395 | LineState State; |
| 396 | bool NewLine; |
| 397 | StateNode *Previous; |
| 398 | }; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 399 | |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 400 | /// \brief A pair of <penalty, count> that is used to prioritize the BFS on. |
| 401 | /// |
| 402 | /// In case of equal penalties, we want to prefer states that were inserted |
| 403 | /// first. During state generation we make sure that we insert states first |
| 404 | /// that break the line as late as possible. |
| 405 | typedef std::pair<unsigned, unsigned> OrderedPenalty; |
| 406 | |
| 407 | /// \brief An item in the prioritized BFS search queue. The \c StateNode's |
| 408 | /// \c State has the given \c OrderedPenalty. |
| 409 | typedef std::pair<OrderedPenalty, StateNode *> QueueItem; |
| 410 | |
| 411 | /// \brief The BFS queue type. |
| 412 | typedef std::priority_queue<QueueItem, std::vector<QueueItem>, |
| 413 | std::greater<QueueItem> > QueueType; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 414 | |
| 415 | /// \brief Analyze the entire solution space starting from \p InitialState. |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 416 | /// |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 417 | /// This implements a variant of Dijkstra's algorithm on the graph that spans |
| 418 | /// the solution space (\c LineStates are the nodes). The algorithm tries to |
| 419 | /// find the shortest path (the one with lowest penalty) from \p InitialState |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 420 | /// to a state where all tokens are placed. Returns the penalty. |
| 421 | /// |
| 422 | /// If \p DryRun is \c false, directly applies the changes. |
| 423 | unsigned analyzeSolutionSpace(LineState &InitialState, bool DryRun = false) { |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 424 | std::set<LineState> Seen; |
| 425 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 426 | // Insert start element into queue. |
Daniel Jasper | fc75908 | 2013-02-14 14:26:07 +0000 | [diff] [blame] | 427 | StateNode *Node = |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 428 | new (Allocator.Allocate()) StateNode(InitialState, false, NULL); |
| 429 | Queue.push(QueueItem(OrderedPenalty(0, Count), Node)); |
| 430 | ++Count; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 431 | |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 432 | unsigned Penalty = 0; |
| 433 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 434 | // While not empty, take first element and follow edges. |
| 435 | while (!Queue.empty()) { |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 436 | Penalty = Queue.top().first.first; |
Daniel Jasper | fc75908 | 2013-02-14 14:26:07 +0000 | [diff] [blame] | 437 | StateNode *Node = Queue.top().second; |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 438 | if (Node->State.NextToken == NULL) { |
Alexander Kornienko | dd25631 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 439 | DEBUG(llvm::dbgs() << "\n---\nPenalty for line: " << Penalty << "\n"); |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 440 | break; |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 441 | } |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 442 | Queue.pop(); |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 443 | |
Daniel Jasper | 54b4e44 | 2013-05-22 05:27:42 +0000 | [diff] [blame] | 444 | // Cut off the analysis of certain solutions if the analysis gets too |
| 445 | // complex. See description of IgnoreStackForComparison. |
| 446 | if (Count > 10000) |
| 447 | Node->State.IgnoreStackForComparison = true; |
| 448 | |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 449 | if (!Seen.insert(Node->State).second) |
| 450 | // State already examined with lower penalty. |
| 451 | continue; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 452 | |
Manuel Klimek | ae76f7f | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 453 | FormatDecision LastFormat = Node->State.NextToken->Decision; |
| 454 | if (LastFormat == FD_Unformatted || LastFormat == FD_Continue) |
| 455 | addNextStateToQueue(Penalty, Node, /*NewLine=*/false); |
| 456 | if (LastFormat == FD_Unformatted || LastFormat == FD_Break) |
| 457 | addNextStateToQueue(Penalty, Node, /*NewLine=*/true); |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Manuel Klimek | ae76f7f | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 460 | if (Queue.empty()) { |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 461 | // We were unable to find a solution, do nothing. |
| 462 | // FIXME: Add diagnostic? |
Manuel Klimek | ae76f7f | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 463 | DEBUG(llvm::dbgs() << "Could not find a solution.\n"); |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 464 | return 0; |
Manuel Klimek | ae76f7f | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 465 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 466 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 467 | // Reconstruct the solution. |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 468 | if (!DryRun) |
| 469 | reconstructPath(InitialState, Queue.top().second); |
| 470 | |
Alexander Kornienko | dd25631 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 471 | DEBUG(llvm::dbgs() << "Total number of analyzed states: " << Count << "\n"); |
| 472 | DEBUG(llvm::dbgs() << "---\n"); |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 473 | |
| 474 | return Penalty; |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | void reconstructPath(LineState &State, StateNode *Current) { |
Manuel Klimek | 9c333b9 | 2013-05-29 15:10:11 +0000 | [diff] [blame] | 478 | std::deque<StateNode *> Path; |
| 479 | // We do not need a break before the initial token. |
| 480 | while (Current->Previous) { |
| 481 | Path.push_front(Current); |
| 482 | Current = Current->Previous; |
| 483 | } |
| 484 | for (std::deque<StateNode *>::iterator I = Path.begin(), E = Path.end(); |
| 485 | I != E; ++I) { |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 486 | unsigned Penalty = 0; |
| 487 | formatChildren(State, (*I)->NewLine, /*DryRun=*/false, Penalty); |
| 488 | Penalty += Indenter->addTokenToState(State, (*I)->NewLine, false); |
| 489 | |
Manuel Klimek | 9c333b9 | 2013-05-29 15:10:11 +0000 | [diff] [blame] | 490 | DEBUG({ |
| 491 | if ((*I)->NewLine) { |
Daniel Jasper | d4a03db | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 492 | llvm::dbgs() << "Penalty for placing " |
Manuel Klimek | 9c333b9 | 2013-05-29 15:10:11 +0000 | [diff] [blame] | 493 | << (*I)->Previous->State.NextToken->Tok.getName() << ": " |
Daniel Jasper | d4a03db | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 494 | << Penalty << "\n"; |
Manuel Klimek | 9c333b9 | 2013-05-29 15:10:11 +0000 | [diff] [blame] | 495 | } |
| 496 | }); |
Manuel Klimek | 9c333b9 | 2013-05-29 15:10:11 +0000 | [diff] [blame] | 497 | } |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Manuel Klimek | 62a48fb | 2013-02-13 10:54:19 +0000 | [diff] [blame] | 500 | /// \brief Add the following state to the analysis queue \c Queue. |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 501 | /// |
Manuel Klimek | 62a48fb | 2013-02-13 10:54:19 +0000 | [diff] [blame] | 502 | /// Assume the current state is \p PreviousNode and has been reached with a |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 503 | /// penalty of \p Penalty. Insert a line break if \p NewLine is \c true. |
Manuel Klimek | 62a48fb | 2013-02-13 10:54:19 +0000 | [diff] [blame] | 504 | void addNextStateToQueue(unsigned Penalty, StateNode *PreviousNode, |
| 505 | bool NewLine) { |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 506 | if (NewLine && !Indenter->canBreak(PreviousNode->State)) |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 507 | return; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 508 | if (!NewLine && Indenter->mustBreak(PreviousNode->State)) |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 509 | return; |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 510 | |
| 511 | StateNode *Node = new (Allocator.Allocate()) |
| 512 | StateNode(PreviousNode->State, NewLine, PreviousNode); |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 513 | if (!formatChildren(Node->State, NewLine, /*DryRun=*/true, Penalty)) |
| 514 | return; |
| 515 | |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 516 | Penalty += Indenter->addTokenToState(Node->State, NewLine, true); |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 517 | |
| 518 | Queue.push(QueueItem(OrderedPenalty(Penalty, Count), Node)); |
| 519 | ++Count; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 520 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 521 | |
Daniel Jasper | 1a925bc | 2013-09-05 10:48:50 +0000 | [diff] [blame] | 522 | /// \brief If the \p State's next token is an r_brace closing a nested block, |
| 523 | /// format the nested block before it. |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 524 | /// |
| 525 | /// Returns \c true if all children could be placed successfully and adapts |
| 526 | /// \p Penalty as well as \p State. If \p DryRun is false, also directly |
| 527 | /// creates changes using \c Whitespaces. |
| 528 | /// |
| 529 | /// The crucial idea here is that children always get formatted upon |
| 530 | /// encountering the closing brace right after the nested block. Now, if we |
| 531 | /// are currently trying to keep the "}" on the same line (i.e. \p NewLine is |
| 532 | /// \c false), the entire block has to be kept on the same line (which is only |
| 533 | /// possible if it fits on the line, only contains a single statement, etc. |
| 534 | /// |
| 535 | /// If \p NewLine is true, we format the nested block on separate lines, i.e. |
| 536 | /// break after the "{", format all lines with correct indentation and the put |
| 537 | /// the closing "}" on yet another new line. |
| 538 | /// |
| 539 | /// This enables us to keep the simple structure of the |
| 540 | /// \c UnwrappedLineFormatter, where we only have two options for each token: |
| 541 | /// break or don't break. |
| 542 | bool formatChildren(LineState &State, bool NewLine, bool DryRun, |
| 543 | unsigned &Penalty) { |
Daniel Jasper | 15eef85 | 2013-10-20 17:28:32 +0000 | [diff] [blame^] | 544 | const FormatToken &Previous = *State.NextToken->Previous; |
| 545 | const FormatToken *LBrace = State.NextToken->getPreviousNonComment(); |
| 546 | if (!LBrace || LBrace->isNot(tok::l_brace) || |
| 547 | LBrace->BlockKind != BK_Block || Previous.Children.size() == 0) |
Daniel Jasper | 1a925bc | 2013-09-05 10:48:50 +0000 | [diff] [blame] | 548 | // The previous token does not open a block. Nothing to do. We don't |
| 549 | // assert so that we can simply call this function for all tokens. |
Daniel Jasper | 2f0a020 | 2013-09-06 08:54:24 +0000 | [diff] [blame] | 550 | return true; |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 551 | |
| 552 | if (NewLine) { |
| 553 | unsigned ParentIndent = State.Stack.back().Indent; |
| 554 | for (SmallVector<AnnotatedLine *, 1>::const_iterator |
Daniel Jasper | 15eef85 | 2013-10-20 17:28:32 +0000 | [diff] [blame^] | 555 | I = Previous.Children.begin(), |
| 556 | E = Previous.Children.end(); |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 557 | I != E; ++I) { |
| 558 | unsigned Indent = |
Daniel Jasper | 5798120 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 559 | ParentIndent + ((*I)->Level - Line.Level - 1) * Style.IndentWidth; |
Daniel Jasper | 14e25c0 | 2013-09-08 14:07:57 +0000 | [diff] [blame] | 560 | if (!DryRun) { |
| 561 | unsigned Newlines = std::min((*I)->First->NewlinesBefore, |
| 562 | Style.MaxEmptyLinesToKeep + 1); |
| 563 | Newlines = std::max(1u, Newlines); |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 564 | Whitespaces->replaceWhitespace( |
Alexander Kornienko | 3d9ffcf | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 565 | *(*I)->First, Newlines, (*I)->Level, /*Spaces=*/Indent, |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 566 | /*StartOfTokenColumn=*/Indent, Line.InPPDirective); |
Daniel Jasper | 14e25c0 | 2013-09-08 14:07:57 +0000 | [diff] [blame] | 567 | } |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 568 | UnwrappedLineFormatter Formatter(Indenter, Whitespaces, Style, **I); |
| 569 | Penalty += Formatter.format(Indent, DryRun); |
| 570 | } |
| 571 | return true; |
| 572 | } |
| 573 | |
Daniel Jasper | 15eef85 | 2013-10-20 17:28:32 +0000 | [diff] [blame^] | 574 | if (Previous.Children.size() > 1) |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 575 | return false; // Cannot merge multiple statements into a single line. |
| 576 | |
| 577 | // We can't put the closing "}" on a line with a trailing comment. |
Daniel Jasper | 15eef85 | 2013-10-20 17:28:32 +0000 | [diff] [blame^] | 578 | if (Previous.Children[0]->Last->isTrailingComment()) |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 579 | return false; |
| 580 | |
| 581 | if (!DryRun) { |
Alexander Kornienko | 3d9ffcf | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 582 | Whitespaces->replaceWhitespace( |
Daniel Jasper | 15eef85 | 2013-10-20 17:28:32 +0000 | [diff] [blame^] | 583 | *Previous.Children[0]->First, |
Alexander Kornienko | f0fc89c | 2013-10-14 00:46:35 +0000 | [diff] [blame] | 584 | /*Newlines=*/0, /*IndentLevel=*/0, /*Spaces=*/1, |
Alexander Kornienko | 3d9ffcf | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 585 | /*StartOfTokenColumn=*/State.Column, State.Line->InPPDirective); |
Daniel Jasper | 14e25c0 | 2013-09-08 14:07:57 +0000 | [diff] [blame] | 586 | UnwrappedLineFormatter Formatter(Indenter, Whitespaces, Style, |
Daniel Jasper | 15eef85 | 2013-10-20 17:28:32 +0000 | [diff] [blame^] | 587 | *Previous.Children[0]); |
Daniel Jasper | 14e25c0 | 2013-09-08 14:07:57 +0000 | [diff] [blame] | 588 | Penalty += Formatter.format(State.Column + 1, DryRun); |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Daniel Jasper | 15eef85 | 2013-10-20 17:28:32 +0000 | [diff] [blame^] | 591 | State.Column += 1 + Previous.Children[0]->Last->TotalLength; |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 592 | return true; |
| 593 | } |
| 594 | |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 595 | ContinuationIndenter *Indenter; |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 596 | WhitespaceManager *Whitespaces; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 597 | FormatStyle Style; |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 598 | const AnnotatedLine &Line; |
Manuel Klimek | 62a48fb | 2013-02-13 10:54:19 +0000 | [diff] [blame] | 599 | |
| 600 | llvm::SpecificBumpPtrAllocator<StateNode> Allocator; |
| 601 | QueueType Queue; |
| 602 | // Increasing count of \c StateNode items we have created. This is used |
| 603 | // to create a deterministic order independent of the container. |
| 604 | unsigned Count; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 605 | }; |
| 606 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 607 | class FormatTokenLexer { |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 608 | public: |
Manuel Klimek | c41e819 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 609 | FormatTokenLexer(Lexer &Lex, SourceManager &SourceMgr, FormatStyle &Style, |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 610 | encoding::Encoding Encoding) |
Manuel Klimek | c41e819 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 611 | : FormatTok(NULL), GreaterStashed(false), Column(0), |
| 612 | TrailingWhitespace(0), Lex(Lex), SourceMgr(SourceMgr), Style(Style), |
| 613 | IdentTable(getFormattingLangOpts()), Encoding(Encoding) { |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 614 | Lex.SetKeepWhitespaceMode(true); |
| 615 | } |
| 616 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 617 | ArrayRef<FormatToken *> lex() { |
| 618 | assert(Tokens.empty()); |
| 619 | do { |
| 620 | Tokens.push_back(getNextToken()); |
Alexander Kornienko | 2c2f729 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 621 | maybeJoinPreviousTokens(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 622 | } while (Tokens.back()->Tok.isNot(tok::eof)); |
| 623 | return Tokens; |
| 624 | } |
| 625 | |
| 626 | IdentifierTable &getIdentTable() { return IdentTable; } |
| 627 | |
| 628 | private: |
Alexander Kornienko | 2c2f729 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 629 | void maybeJoinPreviousTokens() { |
| 630 | if (Tokens.size() < 4) |
| 631 | return; |
| 632 | FormatToken *Last = Tokens.back(); |
| 633 | if (!Last->is(tok::r_paren)) |
| 634 | return; |
| 635 | |
| 636 | FormatToken *String = Tokens[Tokens.size() - 2]; |
| 637 | if (!String->is(tok::string_literal) || String->IsMultiline) |
| 638 | return; |
| 639 | |
| 640 | if (!Tokens[Tokens.size() - 3]->is(tok::l_paren)) |
| 641 | return; |
| 642 | |
| 643 | FormatToken *Macro = Tokens[Tokens.size() - 4]; |
| 644 | if (Macro->TokenText != "_T") |
| 645 | return; |
| 646 | |
| 647 | const char *Start = Macro->TokenText.data(); |
| 648 | const char *End = Last->TokenText.data() + Last->TokenText.size(); |
| 649 | String->TokenText = StringRef(Start, End - Start); |
| 650 | String->IsFirst = Macro->IsFirst; |
| 651 | String->LastNewlineOffset = Macro->LastNewlineOffset; |
| 652 | String->WhitespaceRange = Macro->WhitespaceRange; |
| 653 | String->OriginalColumn = Macro->OriginalColumn; |
| 654 | String->ColumnWidth = encoding::columnWidthWithTabs( |
| 655 | String->TokenText, String->OriginalColumn, Style.TabWidth, Encoding); |
| 656 | |
| 657 | Tokens.pop_back(); |
| 658 | Tokens.pop_back(); |
| 659 | Tokens.pop_back(); |
| 660 | Tokens.back() = String; |
| 661 | } |
| 662 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 663 | FormatToken *getNextToken() { |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 664 | if (GreaterStashed) { |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 665 | // Create a synthesized second '>' token. |
Manuel Klimek | c41e819 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 666 | // FIXME: Increment Column and set OriginalColumn. |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 667 | Token Greater = FormatTok->Tok; |
| 668 | FormatTok = new (Allocator.Allocate()) FormatToken; |
| 669 | FormatTok->Tok = Greater; |
Manuel Klimek | ad3094b | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 670 | SourceLocation GreaterLocation = |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 671 | FormatTok->Tok.getLocation().getLocWithOffset(1); |
| 672 | FormatTok->WhitespaceRange = |
| 673 | SourceRange(GreaterLocation, GreaterLocation); |
Alexander Kornienko | 54e6c9d | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 674 | FormatTok->TokenText = ">"; |
Alexander Kornienko | 83a7dcd | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 675 | FormatTok->ColumnWidth = 1; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 676 | GreaterStashed = false; |
| 677 | return FormatTok; |
| 678 | } |
| 679 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 680 | FormatTok = new (Allocator.Allocate()) FormatToken; |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 681 | readRawToken(*FormatTok); |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 682 | SourceLocation WhitespaceStart = |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 683 | FormatTok->Tok.getLocation().getLocWithOffset(-TrailingWhitespace); |
Manuel Klimek | ad3094b | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 684 | if (SourceMgr.getFileOffset(WhitespaceStart) == 0) |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 685 | FormatTok->IsFirst = true; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 686 | |
| 687 | // Consume and record whitespace until we find a significant token. |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 688 | unsigned WhitespaceLength = TrailingWhitespace; |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 689 | while (FormatTok->Tok.is(tok::unknown)) { |
Manuel Klimek | c41e819 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 690 | for (int i = 0, e = FormatTok->TokenText.size(); i != e; ++i) { |
| 691 | switch (FormatTok->TokenText[i]) { |
| 692 | case '\n': |
| 693 | ++FormatTok->NewlinesBefore; |
| 694 | // FIXME: This is technically incorrect, as it could also |
| 695 | // be a literal backslash at the end of the line. |
Alexander Kornienko | 73d845c | 2013-09-11 12:25:57 +0000 | [diff] [blame] | 696 | if (i == 0 || (FormatTok->TokenText[i - 1] != '\\' && |
| 697 | (FormatTok->TokenText[i - 1] != '\r' || i == 1 || |
| 698 | FormatTok->TokenText[i - 2] != '\\'))) |
Manuel Klimek | c41e819 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 699 | FormatTok->HasUnescapedNewline = true; |
| 700 | FormatTok->LastNewlineOffset = WhitespaceLength + i + 1; |
| 701 | Column = 0; |
| 702 | break; |
Daniel Jasper | 1d82b1a | 2013-10-11 19:45:02 +0000 | [diff] [blame] | 703 | case '\r': |
| 704 | case '\f': |
| 705 | case '\v': |
| 706 | Column = 0; |
| 707 | break; |
Manuel Klimek | c41e819 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 708 | case ' ': |
| 709 | ++Column; |
| 710 | break; |
| 711 | case '\t': |
Alexander Kornienko | 0b62cc3 | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 712 | Column += Style.TabWidth - Column % Style.TabWidth; |
Manuel Klimek | c41e819 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 713 | break; |
Daniel Jasper | 1d82b1a | 2013-10-11 19:45:02 +0000 | [diff] [blame] | 714 | case '\\': |
| 715 | ++Column; |
| 716 | if (i + 1 == e || (FormatTok->TokenText[i + 1] != '\r' && |
| 717 | FormatTok->TokenText[i + 1] != '\n')) |
| 718 | FormatTok->Type = TT_ImplicitStringLiteral; |
| 719 | break; |
Manuel Klimek | c41e819 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 720 | default: |
Daniel Jasper | 1d82b1a | 2013-10-11 19:45:02 +0000 | [diff] [blame] | 721 | FormatTok->Type = TT_ImplicitStringLiteral; |
Manuel Klimek | c41e819 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 722 | ++Column; |
| 723 | break; |
| 724 | } |
| 725 | } |
| 726 | |
Daniel Jasper | 1d82b1a | 2013-10-11 19:45:02 +0000 | [diff] [blame] | 727 | if (FormatTok->Type == TT_ImplicitStringLiteral) |
| 728 | break; |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 729 | WhitespaceLength += FormatTok->Tok.getLength(); |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 730 | |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 731 | readRawToken(*FormatTok); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 732 | } |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 733 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 734 | // In case the token starts with escaped newlines, we want to |
| 735 | // take them into account as whitespace - this pattern is quite frequent |
| 736 | // in macro definitions. |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 737 | // FIXME: Add a more explicit test. |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 738 | while (FormatTok->TokenText.size() > 1 && FormatTok->TokenText[0] == '\\' && |
| 739 | FormatTok->TokenText[1] == '\n') { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 740 | // FIXME: ++FormatTok->NewlinesBefore is missing... |
Manuel Klimek | ad3094b | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 741 | WhitespaceLength += 2; |
Manuel Klimek | c41e819 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 742 | Column = 0; |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 743 | FormatTok->TokenText = FormatTok->TokenText.substr(2); |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 744 | } |
Alexander Kornienko | 83a7dcd | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 745 | |
| 746 | FormatTok->WhitespaceRange = SourceRange( |
| 747 | WhitespaceStart, WhitespaceStart.getLocWithOffset(WhitespaceLength)); |
| 748 | |
Manuel Klimek | c41e819 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 749 | FormatTok->OriginalColumn = Column; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 750 | |
Alexander Kornienko | 54e6c9d | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 751 | TrailingWhitespace = 0; |
| 752 | if (FormatTok->Tok.is(tok::comment)) { |
Manuel Klimek | c41e819 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 753 | // FIXME: Add the trimmed whitespace to Column. |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 754 | StringRef UntrimmedText = FormatTok->TokenText; |
Alexander Kornienko | 51bb5d9 | 2013-09-06 17:24:54 +0000 | [diff] [blame] | 755 | FormatTok->TokenText = FormatTok->TokenText.rtrim(" \t\v\f"); |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 756 | TrailingWhitespace = UntrimmedText.size() - FormatTok->TokenText.size(); |
Alexander Kornienko | 54e6c9d | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 757 | } else if (FormatTok->Tok.is(tok::raw_identifier)) { |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 758 | IdentifierInfo &Info = IdentTable.get(FormatTok->TokenText); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 759 | FormatTok->Tok.setIdentifierInfo(&Info); |
| 760 | FormatTok->Tok.setKind(Info.getTokenID()); |
Alexander Kornienko | 54e6c9d | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 761 | } else if (FormatTok->Tok.is(tok::greatergreater)) { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 762 | FormatTok->Tok.setKind(tok::greater); |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 763 | FormatTok->TokenText = FormatTok->TokenText.substr(0, 1); |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 764 | GreaterStashed = true; |
| 765 | } |
| 766 | |
Alexander Kornienko | 54e6c9d | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 767 | // Now FormatTok is the next non-whitespace token. |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 768 | |
Alexander Kornienko | 83a7dcd | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 769 | StringRef Text = FormatTok->TokenText; |
| 770 | size_t FirstNewlinePos = Text.find('\n'); |
Alexander Kornienko | 6f6154c | 2013-09-10 12:29:48 +0000 | [diff] [blame] | 771 | if (FirstNewlinePos == StringRef::npos) { |
| 772 | // FIXME: ColumnWidth actually depends on the start column, we need to |
| 773 | // take this into account when the token is moved. |
| 774 | FormatTok->ColumnWidth = |
| 775 | encoding::columnWidthWithTabs(Text, Column, Style.TabWidth, Encoding); |
| 776 | Column += FormatTok->ColumnWidth; |
| 777 | } else { |
Alexander Kornienko | 83a7dcd | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 778 | FormatTok->IsMultiline = true; |
Alexander Kornienko | 6f6154c | 2013-09-10 12:29:48 +0000 | [diff] [blame] | 779 | // FIXME: ColumnWidth actually depends on the start column, we need to |
| 780 | // take this into account when the token is moved. |
| 781 | FormatTok->ColumnWidth = encoding::columnWidthWithTabs( |
| 782 | Text.substr(0, FirstNewlinePos), Column, Style.TabWidth, Encoding); |
| 783 | |
Alexander Kornienko | 83a7dcd | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 784 | // The last line of the token always starts in column 0. |
| 785 | // Thus, the length can be precomputed even in the presence of tabs. |
| 786 | FormatTok->LastLineColumnWidth = encoding::columnWidthWithTabs( |
| 787 | Text.substr(Text.find_last_of('\n') + 1), 0, Style.TabWidth, |
| 788 | Encoding); |
Alexander Kornienko | 6f6154c | 2013-09-10 12:29:48 +0000 | [diff] [blame] | 789 | Column = FormatTok->LastLineColumnWidth; |
Alexander Kornienko | 4b762a9 | 2013-09-02 13:58:14 +0000 | [diff] [blame] | 790 | } |
Alexander Kornienko | 83a7dcd | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 791 | |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 792 | return FormatTok; |
| 793 | } |
| 794 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 795 | FormatToken *FormatTok; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 796 | bool GreaterStashed; |
Manuel Klimek | c41e819 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 797 | unsigned Column; |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 798 | unsigned TrailingWhitespace; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 799 | Lexer &Lex; |
| 800 | SourceManager &SourceMgr; |
Manuel Klimek | c41e819 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 801 | FormatStyle &Style; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 802 | IdentifierTable IdentTable; |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 803 | encoding::Encoding Encoding; |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 804 | llvm::SpecificBumpPtrAllocator<FormatToken> Allocator; |
| 805 | SmallVector<FormatToken *, 16> Tokens; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 806 | |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 807 | void readRawToken(FormatToken &Tok) { |
| 808 | Lex.LexFromRawLexer(Tok.Tok); |
| 809 | Tok.TokenText = StringRef(SourceMgr.getCharacterData(Tok.Tok.getLocation()), |
| 810 | Tok.Tok.getLength()); |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 811 | // For formatting, treat unterminated string literals like normal string |
| 812 | // literals. |
| 813 | if (Tok.is(tok::unknown) && !Tok.TokenText.empty() && |
| 814 | Tok.TokenText[0] == '"') { |
| 815 | Tok.Tok.setKind(tok::string_literal); |
| 816 | Tok.IsUnterminatedLiteral = true; |
| 817 | } |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 818 | } |
| 819 | }; |
| 820 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 821 | class Formatter : public UnwrappedLineConsumer { |
| 822 | public: |
Daniel Jasper | caf42a3 | 2013-05-15 08:14:19 +0000 | [diff] [blame] | 823 | Formatter(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr, |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 824 | const std::vector<CharSourceRange> &Ranges) |
Daniel Jasper | caf42a3 | 2013-05-15 08:14:19 +0000 | [diff] [blame] | 825 | : Style(Style), Lex(Lex), SourceMgr(SourceMgr), |
Alexander Kornienko | 73d845c | 2013-09-11 12:25:57 +0000 | [diff] [blame] | 826 | Whitespaces(SourceMgr, Style, inputUsesCRLF(Lex.getBuffer())), |
Manuel Klimek | ae76f7f | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 827 | Ranges(Ranges), UnwrappedLines(1), |
| 828 | Encoding(encoding::detectEncoding(Lex.getBuffer())) { |
Daniel Jasper | 9637dda | 2013-07-15 14:33:14 +0000 | [diff] [blame] | 829 | DEBUG(llvm::dbgs() << "File encoding: " |
| 830 | << (Encoding == encoding::Encoding_UTF8 ? "UTF8" |
| 831 | : "unknown") |
| 832 | << "\n"); |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 833 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 834 | |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 835 | tooling::Replacements format() { |
Manuel Klimek | ae76f7f | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 836 | tooling::Replacements Result; |
Manuel Klimek | c41e819 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 837 | FormatTokenLexer Tokens(Lex, SourceMgr, Style, Encoding); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 838 | |
| 839 | UnwrappedLineParser Parser(Style, Tokens.lex(), *this); |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 840 | bool StructuralError = Parser.parse(); |
Manuel Klimek | ae76f7f | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 841 | assert(UnwrappedLines.rbegin()->empty()); |
| 842 | for (unsigned Run = 0, RunE = UnwrappedLines.size(); Run + 1 != RunE; |
| 843 | ++Run) { |
| 844 | DEBUG(llvm::dbgs() << "Run " << Run << "...\n"); |
| 845 | SmallVector<AnnotatedLine *, 16> AnnotatedLines; |
| 846 | for (unsigned i = 0, e = UnwrappedLines[Run].size(); i != e; ++i) { |
| 847 | AnnotatedLines.push_back(new AnnotatedLine(UnwrappedLines[Run][i])); |
| 848 | } |
| 849 | tooling::Replacements RunResult = |
| 850 | format(AnnotatedLines, StructuralError, Tokens); |
| 851 | DEBUG({ |
| 852 | llvm::dbgs() << "Replacements for run " << Run << ":\n"; |
| 853 | for (tooling::Replacements::iterator I = RunResult.begin(), |
| 854 | E = RunResult.end(); |
| 855 | I != E; ++I) { |
| 856 | llvm::dbgs() << I->toString() << "\n"; |
| 857 | } |
| 858 | }); |
| 859 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
| 860 | delete AnnotatedLines[i]; |
| 861 | } |
| 862 | Result.insert(RunResult.begin(), RunResult.end()); |
| 863 | Whitespaces.reset(); |
| 864 | } |
| 865 | return Result; |
| 866 | } |
| 867 | |
| 868 | tooling::Replacements format(SmallVectorImpl<AnnotatedLine *> &AnnotatedLines, |
| 869 | bool StructuralError, FormatTokenLexer &Tokens) { |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 870 | TokenAnnotator Annotator(Style, Tokens.getIdentTable().get("in")); |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 871 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 872 | Annotator.annotate(*AnnotatedLines[i]); |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 873 | } |
Manuel Klimek | ae76f7f | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 874 | deriveLocalStyle(AnnotatedLines); |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 875 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 876 | Annotator.calculateFormattingInformation(*AnnotatedLines[i]); |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 877 | } |
Daniel Jasper | 5999f76 | 2013-04-09 17:46:55 +0000 | [diff] [blame] | 878 | |
Daniel Jasper | b77d741 | 2013-09-06 07:54:20 +0000 | [diff] [blame] | 879 | Annotator.setCommentLineLevels(AnnotatedLines); |
Daniel Jasper | 5999f76 | 2013-04-09 17:46:55 +0000 | [diff] [blame] | 880 | |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 881 | std::vector<int> IndentForLevel; |
| 882 | bool PreviousLineWasTouched = false; |
Daniel Jasper | 63cfb89 | 2013-10-06 11:40:08 +0000 | [diff] [blame] | 883 | const AnnotatedLine *PreviousLine = NULL; |
Daniel Jasper | 89b3a7f | 2013-05-10 13:00:49 +0000 | [diff] [blame] | 884 | bool FormatPPDirective = false; |
Daniel Jasper | b77d741 | 2013-09-06 07:54:20 +0000 | [diff] [blame] | 885 | for (SmallVectorImpl<AnnotatedLine *>::iterator I = AnnotatedLines.begin(), |
| 886 | E = AnnotatedLines.end(); |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 887 | I != E; ++I) { |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 888 | const AnnotatedLine &TheLine = **I; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 889 | const FormatToken *FirstTok = TheLine.First; |
| 890 | int Offset = getIndentOffset(*TheLine.First); |
Daniel Jasper | 89b3a7f | 2013-05-10 13:00:49 +0000 | [diff] [blame] | 891 | |
| 892 | // Check whether this line is part of a formatted preprocessor directive. |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 893 | if (FirstTok->HasUnescapedNewline) |
Daniel Jasper | 89b3a7f | 2013-05-10 13:00:49 +0000 | [diff] [blame] | 894 | FormatPPDirective = false; |
| 895 | if (!FormatPPDirective && TheLine.InPPDirective && |
| 896 | (touchesLine(TheLine) || touchesPPDirective(I + 1, E))) |
| 897 | FormatPPDirective = true; |
| 898 | |
Daniel Jasper | 1fb8d88 | 2013-05-14 09:30:02 +0000 | [diff] [blame] | 899 | // Determine indent and try to merge multiple unwrapped lines. |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 900 | while (IndentForLevel.size() <= TheLine.Level) |
| 901 | IndentForLevel.push_back(-1); |
| 902 | IndentForLevel.resize(TheLine.Level + 1); |
Daniel Jasper | 1fb8d88 | 2013-05-14 09:30:02 +0000 | [diff] [blame] | 903 | unsigned Indent = getIndent(IndentForLevel, TheLine.Level); |
| 904 | if (static_cast<int>(Indent) + Offset >= 0) |
| 905 | Indent += Offset; |
| 906 | tryFitMultipleLinesInOne(Indent, I, E); |
| 907 | |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 908 | bool WasMoved = PreviousLineWasTouched && FirstTok->NewlinesBefore == 0; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 909 | if (TheLine.First->is(tok::eof)) { |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 910 | if (PreviousLineWasTouched) { |
Alexander Kornienko | f0fc89c | 2013-10-14 00:46:35 +0000 | [diff] [blame] | 911 | unsigned Newlines = std::min(FirstTok->NewlinesBefore, 1u); |
| 912 | Whitespaces.replaceWhitespace(*TheLine.First, Newlines, |
Alexander Kornienko | 3d9ffcf | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 913 | /*IndentLevel=*/0, /*Spaces=*/0, |
| 914 | /*TargetColumn=*/0); |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 915 | } |
| 916 | } else if (TheLine.Type != LT_Invalid && |
Daniel Jasper | 89b3a7f | 2013-05-10 13:00:49 +0000 | [diff] [blame] | 917 | (WasMoved || FormatPPDirective || touchesLine(TheLine))) { |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 918 | unsigned LevelIndent = getIndent(IndentForLevel, TheLine.Level); |
Daniel Jasper | 0baf33b | 2013-10-18 17:20:57 +0000 | [diff] [blame] | 919 | if (FirstTok->WhitespaceRange.isValid()) { |
Alexander Kornienko | f0fc89c | 2013-10-14 00:46:35 +0000 | [diff] [blame] | 920 | formatFirstToken(*TheLine.First, PreviousLine, TheLine.Level, Indent, |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 921 | TheLine.InPPDirective); |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 922 | } else { |
Manuel Klimek | c41e819 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 923 | Indent = LevelIndent = FirstTok->OriginalColumn; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 924 | } |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 925 | ContinuationIndenter Indenter(Style, SourceMgr, Whitespaces, Encoding, |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 926 | BinPackInconclusiveFunctions); |
| 927 | |
| 928 | // If everything fits on a single line, just put it there. |
| 929 | unsigned ColumnLimit = Style.ColumnLimit; |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 930 | AnnotatedLine *NextLine = *(I + 1); |
| 931 | if ((I + 1) != E && NextLine->InPPDirective && |
| 932 | !NextLine->First->HasUnescapedNewline) |
| 933 | ColumnLimit = getColumnLimit(TheLine.InPPDirective); |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 934 | |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 935 | if (TheLine.Last->TotalLength + Indent <= ColumnLimit) { |
Daniel Jasper | b77d741 | 2013-09-06 07:54:20 +0000 | [diff] [blame] | 936 | LineState State = |
| 937 | Indenter.getInitialState(Indent, &TheLine, /*DryRun=*/false); |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 938 | while (State.NextToken != NULL) |
| 939 | Indenter.addTokenToState(State, false, false); |
| 940 | } else if (Style.ColumnLimit == 0) { |
| 941 | NoColumnLimitFormatter Formatter(&Indenter); |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 942 | Formatter.format(Indent, &TheLine); |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 943 | } else { |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 944 | UnwrappedLineFormatter Formatter(&Indenter, &Whitespaces, Style, |
| 945 | TheLine); |
| 946 | Formatter.format(Indent); |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 947 | } |
| 948 | |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 949 | IndentForLevel[TheLine.Level] = LevelIndent; |
| 950 | PreviousLineWasTouched = true; |
| 951 | } else { |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 952 | // Format the first token if necessary, and notify the WhitespaceManager |
| 953 | // about the unchanged whitespace. |
Manuel Klimek | ae76f7f | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 954 | for (FormatToken *Tok = TheLine.First; Tok != NULL; Tok = Tok->Next) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 955 | if (Tok == TheLine.First && |
| 956 | (Tok->NewlinesBefore > 0 || Tok->IsFirst)) { |
Manuel Klimek | c41e819 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 957 | unsigned LevelIndent = Tok->OriginalColumn; |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 958 | // Remove trailing whitespace of the previous line if it was |
| 959 | // touched. |
| 960 | if (PreviousLineWasTouched || touchesEmptyLineBefore(TheLine)) { |
Alexander Kornienko | f0fc89c | 2013-10-14 00:46:35 +0000 | [diff] [blame] | 961 | formatFirstToken(*Tok, PreviousLine, TheLine.Level, LevelIndent, |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 962 | TheLine.InPPDirective); |
| 963 | } else { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 964 | Whitespaces.addUntouchableToken(*Tok, TheLine.InPPDirective); |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 965 | } |
Daniel Jasper | 1fb8d88 | 2013-05-14 09:30:02 +0000 | [diff] [blame] | 966 | |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 967 | if (static_cast<int>(LevelIndent) - Offset >= 0) |
| 968 | LevelIndent -= Offset; |
| 969 | if (Tok->isNot(tok::comment)) |
| 970 | IndentForLevel[TheLine.Level] = LevelIndent; |
| 971 | } else { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 972 | Whitespaces.addUntouchableToken(*Tok, TheLine.InPPDirective); |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 973 | } |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 974 | } |
| 975 | // If we did not reformat this unwrapped line, the column at the end of |
| 976 | // the last token is unchanged - thus, we can calculate the end of the |
| 977 | // last token. |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 978 | PreviousLineWasTouched = false; |
| 979 | } |
Manuel Klimek | ae76f7f | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 980 | for (FormatToken *Tok = TheLine.First; Tok != NULL; Tok = Tok->Next) { |
| 981 | Tok->Finalized = true; |
| 982 | } |
Daniel Jasper | 63cfb89 | 2013-10-06 11:40:08 +0000 | [diff] [blame] | 983 | PreviousLine = *I; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 984 | } |
| 985 | return Whitespaces.generateReplacements(); |
| 986 | } |
| 987 | |
| 988 | private: |
Alexander Kornienko | 73d845c | 2013-09-11 12:25:57 +0000 | [diff] [blame] | 989 | static bool inputUsesCRLF(StringRef Text) { |
| 990 | return Text.count('\r') * 2 > Text.count('\n'); |
| 991 | } |
| 992 | |
Manuel Klimek | ae76f7f | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 993 | void |
| 994 | deriveLocalStyle(const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) { |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 995 | unsigned CountBoundToVariable = 0; |
| 996 | unsigned CountBoundToType = 0; |
| 997 | bool HasCpp03IncompatibleFormat = false; |
Daniel Jasper | c7bd68f | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 998 | bool HasBinPackedFunction = false; |
| 999 | bool HasOnePerLineFunction = false; |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1000 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1001 | if (!AnnotatedLines[i]->First->Next) |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1002 | continue; |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1003 | FormatToken *Tok = AnnotatedLines[i]->First->Next; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1004 | while (Tok->Next) { |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1005 | if (Tok->Type == TT_PointerOrReference) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1006 | bool SpacesBefore = |
| 1007 | Tok->WhitespaceRange.getBegin() != Tok->WhitespaceRange.getEnd(); |
| 1008 | bool SpacesAfter = Tok->Next->WhitespaceRange.getBegin() != |
| 1009 | Tok->Next->WhitespaceRange.getEnd(); |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1010 | if (SpacesBefore && !SpacesAfter) |
| 1011 | ++CountBoundToVariable; |
| 1012 | else if (!SpacesBefore && SpacesAfter) |
| 1013 | ++CountBoundToType; |
| 1014 | } |
| 1015 | |
Daniel Jasper | 78a4e61 | 2013-10-12 05:16:06 +0000 | [diff] [blame] | 1016 | if (Tok->WhitespaceRange.getBegin() == Tok->WhitespaceRange.getEnd()) { |
| 1017 | if (Tok->is(tok::coloncolon) && |
| 1018 | Tok->Previous->Type == TT_TemplateOpener) |
| 1019 | HasCpp03IncompatibleFormat = true; |
| 1020 | if (Tok->Type == TT_TemplateCloser && |
| 1021 | Tok->Previous->Type == TT_TemplateCloser) |
| 1022 | HasCpp03IncompatibleFormat = true; |
| 1023 | } |
Daniel Jasper | c7bd68f | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 1024 | |
| 1025 | if (Tok->PackingKind == PPK_BinPacked) |
| 1026 | HasBinPackedFunction = true; |
| 1027 | if (Tok->PackingKind == PPK_OnePerLine) |
| 1028 | HasOnePerLineFunction = true; |
| 1029 | |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1030 | Tok = Tok->Next; |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1031 | } |
| 1032 | } |
| 1033 | if (Style.DerivePointerBinding) { |
| 1034 | if (CountBoundToType > CountBoundToVariable) |
| 1035 | Style.PointerBindsToType = true; |
| 1036 | else if (CountBoundToType < CountBoundToVariable) |
| 1037 | Style.PointerBindsToType = false; |
| 1038 | } |
| 1039 | if (Style.Standard == FormatStyle::LS_Auto) { |
| 1040 | Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11 |
| 1041 | : FormatStyle::LS_Cpp03; |
| 1042 | } |
Daniel Jasper | c7bd68f | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 1043 | BinPackInconclusiveFunctions = |
| 1044 | HasBinPackedFunction || !HasOnePerLineFunction; |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 1047 | /// \brief Get the indent of \p Level from \p IndentForLevel. |
| 1048 | /// |
| 1049 | /// \p IndentForLevel must contain the indent for the level \c l |
| 1050 | /// at \p IndentForLevel[l], or a value < 0 if the indent for |
| 1051 | /// that level is unknown. |
Daniel Jasper | fc75908 | 2013-02-14 14:26:07 +0000 | [diff] [blame] | 1052 | unsigned getIndent(const std::vector<int> IndentForLevel, unsigned Level) { |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 1053 | if (IndentForLevel[Level] != -1) |
| 1054 | return IndentForLevel[Level]; |
Manuel Klimek | 52635ff | 2013-02-08 19:53:32 +0000 | [diff] [blame] | 1055 | if (Level == 0) |
| 1056 | return 0; |
Manuel Klimek | 07a64ec | 2013-05-13 08:42:42 +0000 | [diff] [blame] | 1057 | return getIndent(IndentForLevel, Level - 1) + Style.IndentWidth; |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | /// \brief Get the offset of the line relatively to the level. |
| 1061 | /// |
| 1062 | /// For example, 'public:' labels in classes are offset by 1 or 2 |
| 1063 | /// characters to the left from their level. |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1064 | int getIndentOffset(const FormatToken &RootToken) { |
Alexander Kornienko | 94b748f | 2013-03-27 17:08:02 +0000 | [diff] [blame] | 1065 | if (RootToken.isAccessSpecifier(false) || RootToken.isObjCAccessSpecifier()) |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 1066 | return Style.AccessModifierOffset; |
| 1067 | return 0; |
| 1068 | } |
| 1069 | |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1070 | /// \brief Tries to merge lines into one. |
| 1071 | /// |
| 1072 | /// This will change \c Line and \c AnnotatedLine to contain the merged line, |
| 1073 | /// if possible; note that \c I will be incremented when lines are merged. |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1074 | void tryFitMultipleLinesInOne(unsigned Indent, |
Daniel Jasper | b77d741 | 2013-09-06 07:54:20 +0000 | [diff] [blame] | 1075 | SmallVectorImpl<AnnotatedLine *>::iterator &I, |
| 1076 | SmallVectorImpl<AnnotatedLine *>::iterator E) { |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1077 | // We can never merge stuff if there are trailing line comments. |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1078 | AnnotatedLine *TheLine = *I; |
| 1079 | if (TheLine->Last->Type == TT_LineComment) |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1080 | return; |
| 1081 | |
Daniel Jasper | e05dc6d | 2013-07-24 13:10:59 +0000 | [diff] [blame] | 1082 | if (Indent > Style.ColumnLimit) |
| 1083 | return; |
| 1084 | |
Daniel Jasper | a4d4621 | 2013-02-28 11:05:57 +0000 | [diff] [blame] | 1085 | unsigned Limit = Style.ColumnLimit - Indent; |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1086 | // If we already exceed the column limit, we set 'Limit' to 0. The different |
| 1087 | // tryMerge..() functions can then decide whether to still do merging. |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1088 | Limit = TheLine->Last->TotalLength > Limit |
| 1089 | ? 0 |
| 1090 | : Limit - TheLine->Last->TotalLength; |
Daniel Jasper | 55b08e7 | 2013-01-16 07:02:34 +0000 | [diff] [blame] | 1091 | |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1092 | if (I + 1 == E || (*(I + 1))->Type == LT_Invalid) |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1093 | return; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1094 | |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1095 | if (TheLine->Last->is(tok::l_brace)) { |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1096 | tryMergeSimpleBlock(I, E, Limit); |
Daniel Jasper | f11bbb9 | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 1097 | } else if (Style.AllowShortIfStatementsOnASingleLine && |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1098 | TheLine->First->is(tok::kw_if)) { |
Daniel Jasper | f11bbb9 | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 1099 | tryMergeSimpleControlStatement(I, E, Limit); |
| 1100 | } else if (Style.AllowShortLoopsOnASingleLine && |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1101 | TheLine->First->isOneOf(tok::kw_for, tok::kw_while)) { |
Daniel Jasper | f11bbb9 | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 1102 | tryMergeSimpleControlStatement(I, E, Limit); |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1103 | } else if (TheLine->InPPDirective && (TheLine->First->HasUnescapedNewline || |
| 1104 | TheLine->First->IsFirst)) { |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1105 | tryMergeSimplePPDirective(I, E, Limit); |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1106 | } |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
Daniel Jasper | b77d741 | 2013-09-06 07:54:20 +0000 | [diff] [blame] | 1109 | void tryMergeSimplePPDirective(SmallVectorImpl<AnnotatedLine *>::iterator &I, |
| 1110 | SmallVectorImpl<AnnotatedLine *>::iterator E, |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1111 | unsigned Limit) { |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1112 | if (Limit == 0) |
| 1113 | return; |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1114 | AnnotatedLine &Line = **I; |
| 1115 | if (!(*(I + 1))->InPPDirective || (*(I + 1))->First->HasUnescapedNewline) |
Daniel Jasper | 2b9c10b | 2013-01-14 15:52:06 +0000 | [diff] [blame] | 1116 | return; |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1117 | if (I + 2 != E && (*(I + 2))->InPPDirective && |
| 1118 | !(*(I + 2))->First->HasUnescapedNewline) |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1119 | return; |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1120 | if (1 + (*(I + 1))->Last->TotalLength > Limit) |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1121 | return; |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1122 | join(Line, **(++I)); |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Daniel Jasper | b77d741 | 2013-09-06 07:54:20 +0000 | [diff] [blame] | 1125 | void |
| 1126 | tryMergeSimpleControlStatement(SmallVectorImpl<AnnotatedLine *>::iterator &I, |
| 1127 | SmallVectorImpl<AnnotatedLine *>::iterator E, |
| 1128 | unsigned Limit) { |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1129 | if (Limit == 0) |
| 1130 | return; |
Manuel Klimek | d3a247c | 2013-08-07 19:20:45 +0000 | [diff] [blame] | 1131 | if (Style.BreakBeforeBraces == FormatStyle::BS_Allman && |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1132 | (*(I + 1))->First->is(tok::l_brace)) |
Manuel Klimek | d3a247c | 2013-08-07 19:20:45 +0000 | [diff] [blame] | 1133 | return; |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1134 | if ((*(I + 1))->InPPDirective != (*I)->InPPDirective || |
| 1135 | ((*(I + 1))->InPPDirective && (*(I + 1))->First->HasUnescapedNewline)) |
Manuel Klimek | 4c12812 | 2013-01-18 14:46:43 +0000 | [diff] [blame] | 1136 | return; |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1137 | AnnotatedLine &Line = **I; |
Daniel Jasper | 55b08e7 | 2013-01-16 07:02:34 +0000 | [diff] [blame] | 1138 | if (Line.Last->isNot(tok::r_paren)) |
| 1139 | return; |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1140 | if (1 + (*(I + 1))->Last->TotalLength > Limit) |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1141 | return; |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1142 | if ((*(I + 1))->First->isOneOf(tok::semi, tok::kw_if, tok::kw_for, |
| 1143 | tok::kw_while) || |
| 1144 | (*(I + 1))->First->Type == TT_LineComment) |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1145 | return; |
| 1146 | // Only inline simple if's (no nested if or else). |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1147 | if (I + 2 != E && Line.First->is(tok::kw_if) && |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1148 | (*(I + 2))->First->is(tok::kw_else)) |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1149 | return; |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1150 | join(Line, **(++I)); |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
Daniel Jasper | b77d741 | 2013-09-06 07:54:20 +0000 | [diff] [blame] | 1153 | void tryMergeSimpleBlock(SmallVectorImpl<AnnotatedLine *>::iterator &I, |
| 1154 | SmallVectorImpl<AnnotatedLine *>::iterator E, |
Daniel Jasper | 1a1ce83 | 2013-01-29 11:27:30 +0000 | [diff] [blame] | 1155 | unsigned Limit) { |
Daniel Jasper | 5be59ba | 2013-05-15 14:09:55 +0000 | [diff] [blame] | 1156 | // No merging if the brace already is on the next line. |
| 1157 | if (Style.BreakBeforeBraces != FormatStyle::BS_Attach) |
| 1158 | return; |
| 1159 | |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1160 | // First, check that the current line allows merging. This is the case if |
| 1161 | // we're not in a control flow statement and the last token is an opening |
| 1162 | // brace. |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1163 | AnnotatedLine &Line = **I; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1164 | if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::r_brace, |
| 1165 | tok::kw_else, tok::kw_try, tok::kw_catch, |
Daniel Jasper | 8893b8a | 2013-05-31 14:56:20 +0000 | [diff] [blame] | 1166 | tok::kw_for, |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1167 | // This gets rid of all ObjC @ keywords and methods. |
| 1168 | tok::at, tok::minus, tok::plus)) |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1169 | return; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1170 | |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1171 | FormatToken *Tok = (*(I + 1))->First; |
Daniel Jasper | 8893b8a | 2013-05-31 14:56:20 +0000 | [diff] [blame] | 1172 | if (Tok->is(tok::r_brace) && !Tok->MustBreakBefore && |
Alexander Kornienko | 0bdc643 | 2013-07-04 14:47:51 +0000 | [diff] [blame] | 1173 | (Tok->getNextNonComment() == NULL || |
| 1174 | Tok->getNextNonComment()->is(tok::semi))) { |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1175 | // We merge empty blocks even if the line exceeds the column limit. |
Daniel Jasper | 729a743 | 2013-02-11 12:36:37 +0000 | [diff] [blame] | 1176 | Tok->SpacesRequiredBefore = 0; |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1177 | Tok->CanBreakBefore = true; |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1178 | join(Line, **(I + 1)); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1179 | I += 1; |
Daniel Jasper | 8893b8a | 2013-05-31 14:56:20 +0000 | [diff] [blame] | 1180 | } else if (Limit != 0 && Line.First->isNot(tok::kw_namespace)) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1181 | // Check that we still have three lines and they fit into the limit. |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1182 | if (I + 2 == E || (*(I + 2))->Type == LT_Invalid || |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1183 | !nextTwoLinesFitInto(I, Limit)) |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1184 | return; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1185 | |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1186 | // Second, check that the next line does not contain any braces - if it |
| 1187 | // does, readability declines when putting it into a single line. |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1188 | if ((*(I + 1))->Last->Type == TT_LineComment || Tok->MustBreakBefore) |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1189 | return; |
| 1190 | do { |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1191 | if (Tok->isOneOf(tok::l_brace, tok::r_brace)) |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1192 | return; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1193 | Tok = Tok->Next; |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1194 | } while (Tok != NULL); |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1195 | |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1196 | // Last, check that the third line contains a single closing brace. |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1197 | Tok = (*(I + 2))->First; |
Alexander Kornienko | 0bdc643 | 2013-07-04 14:47:51 +0000 | [diff] [blame] | 1198 | if (Tok->getNextNonComment() != NULL || Tok->isNot(tok::r_brace) || |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1199 | Tok->MustBreakBefore) |
| 1200 | return; |
| 1201 | |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1202 | join(Line, **(I + 1)); |
| 1203 | join(Line, **(I + 2)); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1204 | I += 2; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1205 | } |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
Daniel Jasper | b77d741 | 2013-09-06 07:54:20 +0000 | [diff] [blame] | 1208 | bool nextTwoLinesFitInto(SmallVectorImpl<AnnotatedLine *>::iterator I, |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1209 | unsigned Limit) { |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1210 | return 1 + (*(I + 1))->Last->TotalLength + 1 + |
| 1211 | (*(I + 2))->Last->TotalLength <= |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1212 | Limit; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1215 | void join(AnnotatedLine &A, const AnnotatedLine &B) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1216 | assert(!A.Last->Next); |
| 1217 | assert(!B.First->Previous); |
| 1218 | A.Last->Next = B.First; |
| 1219 | B.First->Previous = A.Last; |
| 1220 | unsigned LengthA = A.Last->TotalLength + B.First->SpacesRequiredBefore; |
| 1221 | for (FormatToken *Tok = B.First; Tok; Tok = Tok->Next) { |
| 1222 | Tok->TotalLength += LengthA; |
| 1223 | A.Last = Tok; |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1224 | } |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
Daniel Jasper | 6f21a98 | 2013-03-13 07:49:51 +0000 | [diff] [blame] | 1227 | bool touchesRanges(const CharSourceRange &Range) { |
Daniel Jasper | f302354 | 2013-03-07 20:50:00 +0000 | [diff] [blame] | 1228 | for (unsigned i = 0, e = Ranges.size(); i != e; ++i) { |
| 1229 | if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), |
| 1230 | Ranges[i].getBegin()) && |
| 1231 | !SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(), |
| 1232 | Range.getBegin())) |
| 1233 | return true; |
| 1234 | } |
| 1235 | return false; |
| 1236 | } |
| 1237 | |
| 1238 | bool touchesLine(const AnnotatedLine &TheLine) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1239 | const FormatToken *First = TheLine.First; |
| 1240 | const FormatToken *Last = TheLine.Last; |
Daniel Jasper | 84f5ddf | 2013-05-14 10:31:09 +0000 | [diff] [blame] | 1241 | CharSourceRange LineRange = CharSourceRange::getCharRange( |
Manuel Klimek | ad3094b | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 1242 | First->WhitespaceRange.getBegin().getLocWithOffset( |
| 1243 | First->LastNewlineOffset), |
Alexander Kornienko | 2c2f729 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 1244 | Last->getStartOfNonWhitespace().getLocWithOffset( |
| 1245 | Last->TokenText.size() - 1)); |
Daniel Jasper | f302354 | 2013-03-07 20:50:00 +0000 | [diff] [blame] | 1246 | return touchesRanges(LineRange); |
| 1247 | } |
| 1248 | |
Daniel Jasper | b77d741 | 2013-09-06 07:54:20 +0000 | [diff] [blame] | 1249 | bool touchesPPDirective(SmallVectorImpl<AnnotatedLine *>::iterator I, |
| 1250 | SmallVectorImpl<AnnotatedLine *>::iterator E) { |
Daniel Jasper | 89b3a7f | 2013-05-10 13:00:49 +0000 | [diff] [blame] | 1251 | for (; I != E; ++I) { |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1252 | if ((*I)->First->HasUnescapedNewline) |
Daniel Jasper | 89b3a7f | 2013-05-10 13:00:49 +0000 | [diff] [blame] | 1253 | return false; |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1254 | if (touchesLine(**I)) |
Daniel Jasper | 89b3a7f | 2013-05-10 13:00:49 +0000 | [diff] [blame] | 1255 | return true; |
| 1256 | } |
| 1257 | return false; |
| 1258 | } |
| 1259 | |
Daniel Jasper | f302354 | 2013-03-07 20:50:00 +0000 | [diff] [blame] | 1260 | bool touchesEmptyLineBefore(const AnnotatedLine &TheLine) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1261 | const FormatToken *First = TheLine.First; |
Daniel Jasper | f302354 | 2013-03-07 20:50:00 +0000 | [diff] [blame] | 1262 | CharSourceRange LineRange = CharSourceRange::getCharRange( |
Manuel Klimek | ad3094b | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 1263 | First->WhitespaceRange.getBegin(), |
| 1264 | First->WhitespaceRange.getBegin().getLocWithOffset( |
| 1265 | First->LastNewlineOffset)); |
Daniel Jasper | f302354 | 2013-03-07 20:50:00 +0000 | [diff] [blame] | 1266 | return touchesRanges(LineRange); |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) { |
Manuel Klimek | ae76f7f | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 1270 | assert(!UnwrappedLines.empty()); |
| 1271 | UnwrappedLines.back().push_back(TheLine); |
| 1272 | } |
| 1273 | |
| 1274 | virtual void finishRun() { |
| 1275 | UnwrappedLines.push_back(SmallVector<UnwrappedLine, 16>()); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1278 | /// \brief Add a new line and the required indent before the first Token |
| 1279 | /// of the \c UnwrappedLine if there was no structural parsing error. |
Manuel Klimek | ae76f7f | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 1280 | void formatFirstToken(FormatToken &RootToken, |
Alexander Kornienko | f0fc89c | 2013-10-14 00:46:35 +0000 | [diff] [blame] | 1281 | const AnnotatedLine *PreviousLine, unsigned IndentLevel, |
| 1282 | unsigned Indent, bool InPPDirective) { |
Daniel Jasper | 1a1ce83 | 2013-01-29 11:27:30 +0000 | [diff] [blame] | 1283 | unsigned Newlines = |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1284 | std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1); |
Daniel Jasper | 15f33f0 | 2013-06-03 16:16:41 +0000 | [diff] [blame] | 1285 | // Remove empty lines before "}" where applicable. |
| 1286 | if (RootToken.is(tok::r_brace) && |
| 1287 | (!RootToken.Next || |
| 1288 | (RootToken.Next->is(tok::semi) && !RootToken.Next->Next))) |
| 1289 | Newlines = std::min(Newlines, 1u); |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1290 | if (Newlines == 0 && !RootToken.IsFirst) |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1291 | Newlines = 1; |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1292 | |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 1293 | // Insert extra new line before access specifiers. |
Daniel Jasper | 63cfb89 | 2013-10-06 11:40:08 +0000 | [diff] [blame] | 1294 | if (PreviousLine && PreviousLine->Last->isOneOf(tok::semi, tok::r_brace) && |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1295 | RootToken.isAccessSpecifier() && RootToken.NewlinesBefore == 1) |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 1296 | ++Newlines; |
Alexander Kornienko | 94b748f | 2013-03-27 17:08:02 +0000 | [diff] [blame] | 1297 | |
Daniel Jasper | 63cfb89 | 2013-10-06 11:40:08 +0000 | [diff] [blame] | 1298 | // Remove empty lines after access specifiers. |
| 1299 | if (PreviousLine && PreviousLine->First->isAccessSpecifier()) |
| 1300 | Newlines = std::min(1u, Newlines); |
| 1301 | |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1302 | Whitespaces.replaceWhitespace( |
Alexander Kornienko | f0fc89c | 2013-10-14 00:46:35 +0000 | [diff] [blame] | 1303 | RootToken, Newlines, IndentLevel, Indent, Indent, |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1304 | InPPDirective && !RootToken.HasUnescapedNewline); |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1307 | unsigned getColumnLimit(bool InPPDirective) const { |
| 1308 | // In preprocessor directives reserve two chars for trailing " \" |
| 1309 | return Style.ColumnLimit - (InPPDirective ? 2 : 0); |
| 1310 | } |
| 1311 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1312 | FormatStyle Style; |
| 1313 | Lexer &Lex; |
| 1314 | SourceManager &SourceMgr; |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 1315 | WhitespaceManager Whitespaces; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1316 | std::vector<CharSourceRange> Ranges; |
Manuel Klimek | ae76f7f | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 1317 | SmallVector<SmallVector<UnwrappedLine, 16>, 2> UnwrappedLines; |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1318 | |
| 1319 | encoding::Encoding Encoding; |
Daniel Jasper | c7bd68f | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 1320 | bool BinPackInconclusiveFunctions; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1321 | }; |
| 1322 | |
Craig Topper | 83f81d7 | 2013-06-30 22:29:28 +0000 | [diff] [blame] | 1323 | } // end anonymous namespace |
| 1324 | |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 1325 | tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex, |
| 1326 | SourceManager &SourceMgr, |
Daniel Jasper | caf42a3 | 2013-05-15 08:14:19 +0000 | [diff] [blame] | 1327 | std::vector<CharSourceRange> Ranges) { |
| 1328 | Formatter formatter(Style, Lex, SourceMgr, Ranges); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1329 | return formatter.format(); |
| 1330 | } |
| 1331 | |
Daniel Jasper | 8a99945 | 2013-05-16 10:40:07 +0000 | [diff] [blame] | 1332 | tooling::Replacements reformat(const FormatStyle &Style, StringRef Code, |
| 1333 | std::vector<tooling::Range> Ranges, |
| 1334 | StringRef FileName) { |
| 1335 | FileManager Files((FileSystemOptions())); |
| 1336 | DiagnosticsEngine Diagnostics( |
| 1337 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), |
| 1338 | new DiagnosticOptions); |
| 1339 | SourceManager SourceMgr(Diagnostics, Files); |
| 1340 | llvm::MemoryBuffer *Buf = llvm::MemoryBuffer::getMemBuffer(Code, FileName); |
| 1341 | const clang::FileEntry *Entry = |
| 1342 | Files.getVirtualFile(FileName, Buf->getBufferSize(), 0); |
| 1343 | SourceMgr.overrideFileContents(Entry, Buf); |
| 1344 | FileID ID = |
| 1345 | SourceMgr.createFileID(Entry, SourceLocation(), clang::SrcMgr::C_User); |
Alexander Kornienko | a1753f4 | 2013-06-28 12:51:24 +0000 | [diff] [blame] | 1346 | Lexer Lex(ID, SourceMgr.getBuffer(ID), SourceMgr, |
| 1347 | getFormattingLangOpts(Style.Standard)); |
Daniel Jasper | 8a99945 | 2013-05-16 10:40:07 +0000 | [diff] [blame] | 1348 | SourceLocation StartOfFile = SourceMgr.getLocForStartOfFile(ID); |
| 1349 | std::vector<CharSourceRange> CharRanges; |
| 1350 | for (unsigned i = 0, e = Ranges.size(); i != e; ++i) { |
| 1351 | SourceLocation Start = StartOfFile.getLocWithOffset(Ranges[i].getOffset()); |
| 1352 | SourceLocation End = Start.getLocWithOffset(Ranges[i].getLength()); |
| 1353 | CharRanges.push_back(CharSourceRange::getCharRange(Start, End)); |
| 1354 | } |
| 1355 | return reformat(Style, Lex, SourceMgr, CharRanges); |
| 1356 | } |
| 1357 | |
Alexander Kornienko | a1753f4 | 2013-06-28 12:51:24 +0000 | [diff] [blame] | 1358 | LangOptions getFormattingLangOpts(FormatStyle::LanguageStandard Standard) { |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1359 | LangOptions LangOpts; |
| 1360 | LangOpts.CPlusPlus = 1; |
Alexander Kornienko | a1753f4 | 2013-06-28 12:51:24 +0000 | [diff] [blame] | 1361 | LangOpts.CPlusPlus11 = Standard == FormatStyle::LS_Cpp03 ? 0 : 1; |
Daniel Jasper | b64eca0 | 2013-03-22 10:01:29 +0000 | [diff] [blame] | 1362 | LangOpts.LineComment = 1; |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1363 | LangOpts.Bool = 1; |
| 1364 | LangOpts.ObjC1 = 1; |
| 1365 | LangOpts.ObjC2 = 1; |
| 1366 | return LangOpts; |
| 1367 | } |
| 1368 | |
Edwin Vane | f4e12c8 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 1369 | const char *StyleOptionHelpDescription = |
| 1370 | "Coding style, currently supports:\n" |
| 1371 | " LLVM, Google, Chromium, Mozilla, WebKit.\n" |
| 1372 | "Use -style=file to load style configuration from\n" |
| 1373 | ".clang-format file located in one of the parent\n" |
| 1374 | "directories of the source file (or current\n" |
| 1375 | "directory for stdin).\n" |
| 1376 | "Use -style=\"{key: value, ...}\" to set specific\n" |
| 1377 | "parameters, e.g.:\n" |
| 1378 | " -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\""; |
| 1379 | |
| 1380 | FormatStyle getStyle(StringRef StyleName, StringRef FileName) { |
| 1381 | // Fallback style in case the rest of this function can't determine a style. |
| 1382 | StringRef FallbackStyle = "LLVM"; |
| 1383 | FormatStyle Style; |
| 1384 | getPredefinedStyle(FallbackStyle, &Style); |
| 1385 | |
| 1386 | if (StyleName.startswith("{")) { |
| 1387 | // Parse YAML/JSON style from the command line. |
| 1388 | if (llvm::error_code ec = parseConfiguration(StyleName, &Style)) { |
Alexander Kornienko | f0fc89c | 2013-10-14 00:46:35 +0000 | [diff] [blame] | 1389 | llvm::errs() << "Error parsing -style: " << ec.message() << ", using " |
| 1390 | << FallbackStyle << " style\n"; |
Edwin Vane | f4e12c8 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 1391 | } |
| 1392 | return Style; |
| 1393 | } |
| 1394 | |
| 1395 | if (!StyleName.equals_lower("file")) { |
| 1396 | if (!getPredefinedStyle(StyleName, &Style)) |
| 1397 | llvm::errs() << "Invalid value for -style, using " << FallbackStyle |
| 1398 | << " style\n"; |
| 1399 | return Style; |
| 1400 | } |
| 1401 | |
| 1402 | SmallString<128> Path(FileName); |
| 1403 | llvm::sys::fs::make_absolute(Path); |
Alexander Kornienko | f0fc89c | 2013-10-14 00:46:35 +0000 | [diff] [blame] | 1404 | for (StringRef Directory = Path; !Directory.empty(); |
Edwin Vane | f4e12c8 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 1405 | Directory = llvm::sys::path::parent_path(Directory)) { |
| 1406 | if (!llvm::sys::fs::is_directory(Directory)) |
| 1407 | continue; |
| 1408 | SmallString<128> ConfigFile(Directory); |
| 1409 | |
| 1410 | llvm::sys::path::append(ConfigFile, ".clang-format"); |
| 1411 | DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n"); |
| 1412 | bool IsFile = false; |
| 1413 | // Ignore errors from is_regular_file: we only need to know if we can read |
| 1414 | // the file or not. |
| 1415 | llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile); |
| 1416 | |
| 1417 | if (!IsFile) { |
| 1418 | // Try _clang-format too, since dotfiles are not commonly used on Windows. |
| 1419 | ConfigFile = Directory; |
| 1420 | llvm::sys::path::append(ConfigFile, "_clang-format"); |
| 1421 | DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n"); |
| 1422 | llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile); |
| 1423 | } |
| 1424 | |
| 1425 | if (IsFile) { |
| 1426 | OwningPtr<llvm::MemoryBuffer> Text; |
| 1427 | if (llvm::error_code ec = llvm::MemoryBuffer::getFile(ConfigFile, Text)) { |
| 1428 | llvm::errs() << ec.message() << "\n"; |
| 1429 | continue; |
| 1430 | } |
| 1431 | if (llvm::error_code ec = parseConfiguration(Text->getBuffer(), &Style)) { |
| 1432 | llvm::errs() << "Error reading " << ConfigFile << ": " << ec.message() |
| 1433 | << "\n"; |
| 1434 | continue; |
| 1435 | } |
| 1436 | DEBUG(llvm::dbgs() << "Using configuration file " << ConfigFile << "\n"); |
| 1437 | return Style; |
| 1438 | } |
| 1439 | } |
| 1440 | llvm::errs() << "Can't find usable .clang-format, using " << FallbackStyle |
| 1441 | << " style\n"; |
| 1442 | return Style; |
| 1443 | } |
| 1444 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1445 | } // namespace format |
| 1446 | } // namespace clang |