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 | |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 18 | #include "BreakableToken.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" |
Daniel Jasper | 675d2e3 | 2012-12-21 10:20:02 +0000 | [diff] [blame] | 23 | #include "clang/Basic/OperatorPrecedence.h" |
Chandler Carruth | b99083e | 2013-01-02 10:28:36 +0000 | [diff] [blame] | 24 | #include "clang/Basic/SourceManager.h" |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 25 | #include "clang/Format/Format.h" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 26 | #include "clang/Lex/Lexer.h" |
Alexander Kornienko | 5262dd9 | 2013-03-27 11:52:18 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/STLExtras.h" |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Allocator.h" |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Debug.h" |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 30 | #include "llvm/Support/YAMLTraits.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) { |
| 40 | IO.enumCase(Value, "C++03", clang::format::FormatStyle::LS_Cpp03); |
| 41 | IO.enumCase(Value, "C++11", clang::format::FormatStyle::LS_Cpp11); |
| 42 | IO.enumCase(Value, "Auto", clang::format::FormatStyle::LS_Auto); |
| 43 | } |
| 44 | }; |
| 45 | |
Daniel Jasper | 1fb8d88 | 2013-05-14 09:30:02 +0000 | [diff] [blame] | 46 | template <> |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 47 | struct ScalarEnumerationTraits<clang::format::FormatStyle::BraceBreakingStyle> { |
| 48 | static void |
| 49 | enumeration(IO &IO, clang::format::FormatStyle::BraceBreakingStyle &Value) { |
| 50 | IO.enumCase(Value, "Attach", clang::format::FormatStyle::BS_Attach); |
| 51 | IO.enumCase(Value, "Linux", clang::format::FormatStyle::BS_Linux); |
| 52 | IO.enumCase(Value, "Stroustrup", clang::format::FormatStyle::BS_Stroustrup); |
Manuel Klimek | e490705 | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 53 | IO.enumCase(Value, "Allman", clang::format::FormatStyle::BS_Allman); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 54 | } |
| 55 | }; |
| 56 | |
Daniel Jasper | eff18b9 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 57 | template <> |
| 58 | struct ScalarEnumerationTraits< |
| 59 | clang::format::FormatStyle::NamespaceIndentationKind> { |
| 60 | static void |
| 61 | enumeration(IO &IO, |
| 62 | clang::format::FormatStyle::NamespaceIndentationKind &Value) { |
| 63 | IO.enumCase(Value, "None", clang::format::FormatStyle::NI_None); |
| 64 | IO.enumCase(Value, "Inner", clang::format::FormatStyle::NI_Inner); |
| 65 | IO.enumCase(Value, "All", clang::format::FormatStyle::NI_All); |
| 66 | } |
| 67 | }; |
| 68 | |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 69 | template <> struct MappingTraits<clang::format::FormatStyle> { |
| 70 | static void mapping(llvm::yaml::IO &IO, clang::format::FormatStyle &Style) { |
Alexander Kornienko | dd25631 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 71 | if (IO.outputting()) { |
| 72 | StringRef StylesArray[] = { "LLVM", "Google", "Chromium", "Mozilla" }; |
| 73 | ArrayRef<StringRef> Styles(StylesArray); |
| 74 | for (size_t i = 0, e = Styles.size(); i < e; ++i) { |
| 75 | StringRef StyleName(Styles[i]); |
Alexander Kornienko | 885f87b | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 76 | clang::format::FormatStyle PredefinedStyle; |
| 77 | if (clang::format::getPredefinedStyle(StyleName, &PredefinedStyle) && |
| 78 | Style == PredefinedStyle) { |
Alexander Kornienko | dd25631 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 79 | IO.mapOptional("# BasedOnStyle", StyleName); |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | } else { |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 84 | StringRef BasedOnStyle; |
| 85 | IO.mapOptional("BasedOnStyle", BasedOnStyle); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 86 | if (!BasedOnStyle.empty()) |
Alexander Kornienko | 885f87b | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 87 | if (!clang::format::getPredefinedStyle(BasedOnStyle, &Style)) { |
| 88 | IO.setError(Twine("Unknown value for BasedOnStyle: ", BasedOnStyle)); |
| 89 | return; |
| 90 | } |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset); |
| 94 | IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft); |
Daniel Jasper | 893ea8d | 2013-07-31 23:55:15 +0000 | [diff] [blame] | 95 | IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 96 | IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine", |
| 97 | Style.AllowAllParametersOfDeclarationOnNextLine); |
| 98 | IO.mapOptional("AllowShortIfStatementsOnASingleLine", |
| 99 | Style.AllowShortIfStatementsOnASingleLine); |
Daniel Jasper | f11bbb9 | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 100 | IO.mapOptional("AllowShortLoopsOnASingleLine", |
| 101 | Style.AllowShortLoopsOnASingleLine); |
Daniel Jasper | bbc8776 | 2013-05-29 12:07:31 +0000 | [diff] [blame] | 102 | IO.mapOptional("AlwaysBreakTemplateDeclarations", |
| 103 | Style.AlwaysBreakTemplateDeclarations); |
Alexander Kornienko | 5631202 | 2013-07-04 12:02:44 +0000 | [diff] [blame] | 104 | IO.mapOptional("AlwaysBreakBeforeMultilineStrings", |
| 105 | Style.AlwaysBreakBeforeMultilineStrings); |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 106 | IO.mapOptional("BreakBeforeBinaryOperators", |
| 107 | Style.BreakBeforeBinaryOperators); |
| 108 | IO.mapOptional("BreakConstructorInitializersBeforeComma", |
| 109 | Style.BreakConstructorInitializersBeforeComma); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 110 | IO.mapOptional("BinPackParameters", Style.BinPackParameters); |
| 111 | IO.mapOptional("ColumnLimit", Style.ColumnLimit); |
| 112 | IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine", |
| 113 | Style.ConstructorInitializerAllOnOneLineOrOnePerLine); |
| 114 | IO.mapOptional("DerivePointerBinding", Style.DerivePointerBinding); |
Daniel Jasper | c7bd68f | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 115 | IO.mapOptional("ExperimentalAutoDetectBinPacking", |
| 116 | Style.ExperimentalAutoDetectBinPacking); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 117 | IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels); |
| 118 | IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep); |
Daniel Jasper | eff18b9 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 119 | IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 120 | IO.mapOptional("ObjCSpaceBeforeProtocolList", |
| 121 | Style.ObjCSpaceBeforeProtocolList); |
Alexander Kornienko | 2785b9a | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 122 | IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment); |
| 123 | IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString); |
Daniel Jasper | faec47b | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 124 | IO.mapOptional("PenaltyBreakFirstLessLess", |
| 125 | Style.PenaltyBreakFirstLessLess); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 126 | IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter); |
| 127 | IO.mapOptional("PenaltyReturnTypeOnItsOwnLine", |
| 128 | Style.PenaltyReturnTypeOnItsOwnLine); |
| 129 | IO.mapOptional("PointerBindsToType", Style.PointerBindsToType); |
| 130 | IO.mapOptional("SpacesBeforeTrailingComments", |
| 131 | Style.SpacesBeforeTrailingComments); |
Daniel Jasper | b5dc3f4 | 2013-07-16 18:22:10 +0000 | [diff] [blame] | 132 | IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 133 | IO.mapOptional("Standard", Style.Standard); |
Manuel Klimek | 07a64ec | 2013-05-13 08:42:42 +0000 | [diff] [blame] | 134 | IO.mapOptional("IndentWidth", Style.IndentWidth); |
Manuel Klimek | 7c9a93e | 2013-05-13 09:22:11 +0000 | [diff] [blame] | 135 | IO.mapOptional("UseTab", Style.UseTab); |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 136 | IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces); |
Manuel Klimek | a9a7f10 | 2013-06-21 17:25:42 +0000 | [diff] [blame] | 137 | IO.mapOptional("IndentFunctionDeclarationAfterType", |
| 138 | Style.IndentFunctionDeclarationAfterType); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 139 | } |
| 140 | }; |
| 141 | } |
| 142 | } |
| 143 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 144 | namespace clang { |
| 145 | namespace format { |
| 146 | |
Daniel Jasper | faec47b | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 147 | void setDefaultPenalties(FormatStyle &Style) { |
| 148 | Style.PenaltyBreakComment = 45; |
Daniel Jasper | 9637dda | 2013-07-15 14:33:14 +0000 | [diff] [blame] | 149 | Style.PenaltyBreakFirstLessLess = 120; |
Daniel Jasper | faec47b | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 150 | Style.PenaltyBreakString = 1000; |
| 151 | Style.PenaltyExcessCharacter = 1000000; |
| 152 | } |
| 153 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 154 | FormatStyle getLLVMStyle() { |
| 155 | FormatStyle LLVMStyle; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 156 | LLVMStyle.AccessModifierOffset = -2; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 157 | LLVMStyle.AlignEscapedNewlinesLeft = false; |
Daniel Jasper | 893ea8d | 2013-07-31 23:55:15 +0000 | [diff] [blame] | 158 | LLVMStyle.AlignTrailingComments = true; |
Daniel Jasper | f157960 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 159 | LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true; |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 160 | LLVMStyle.AllowShortIfStatementsOnASingleLine = false; |
Daniel Jasper | f11bbb9 | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 161 | LLVMStyle.AllowShortLoopsOnASingleLine = false; |
Alexander Kornienko | 5631202 | 2013-07-04 12:02:44 +0000 | [diff] [blame] | 162 | LLVMStyle.AlwaysBreakBeforeMultilineStrings = false; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 163 | LLVMStyle.AlwaysBreakTemplateDeclarations = false; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 164 | LLVMStyle.BinPackParameters = true; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 165 | LLVMStyle.BreakBeforeBinaryOperators = false; |
| 166 | LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach; |
| 167 | LLVMStyle.BreakConstructorInitializersBeforeComma = false; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 168 | LLVMStyle.ColumnLimit = 80; |
| 169 | LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 170 | LLVMStyle.Cpp11BracedListStyle = false; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 171 | LLVMStyle.DerivePointerBinding = false; |
Daniel Jasper | c7bd68f | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 172 | LLVMStyle.ExperimentalAutoDetectBinPacking = false; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 173 | LLVMStyle.IndentCaseLabels = false; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 174 | LLVMStyle.IndentFunctionDeclarationAfterType = false; |
| 175 | LLVMStyle.IndentWidth = 2; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 176 | LLVMStyle.MaxEmptyLinesToKeep = 1; |
Daniel Jasper | eff18b9 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 177 | LLVMStyle.NamespaceIndentation = FormatStyle::NI_None; |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 178 | LLVMStyle.ObjCSpaceBeforeProtocolList = true; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 179 | LLVMStyle.PointerBindsToType = false; |
| 180 | LLVMStyle.SpacesBeforeTrailingComments = 1; |
| 181 | LLVMStyle.Standard = FormatStyle::LS_Cpp03; |
Manuel Klimek | 7c9a93e | 2013-05-13 09:22:11 +0000 | [diff] [blame] | 182 | LLVMStyle.UseTab = false; |
Daniel Jasper | faec47b | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 183 | |
| 184 | setDefaultPenalties(LLVMStyle); |
| 185 | LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60; |
| 186 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 187 | return LLVMStyle; |
| 188 | } |
| 189 | |
| 190 | FormatStyle getGoogleStyle() { |
| 191 | FormatStyle GoogleStyle; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 192 | GoogleStyle.AccessModifierOffset = -1; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 193 | GoogleStyle.AlignEscapedNewlinesLeft = true; |
Daniel Jasper | 893ea8d | 2013-07-31 23:55:15 +0000 | [diff] [blame] | 194 | GoogleStyle.AlignTrailingComments = true; |
Daniel Jasper | f157960 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 195 | GoogleStyle.AllowAllParametersOfDeclarationOnNextLine = true; |
Daniel Jasper | 94d6ad7 | 2013-04-24 13:46:00 +0000 | [diff] [blame] | 196 | GoogleStyle.AllowShortIfStatementsOnASingleLine = true; |
Daniel Jasper | 1bee073 | 2013-05-23 18:05:18 +0000 | [diff] [blame] | 197 | GoogleStyle.AllowShortLoopsOnASingleLine = true; |
Alexander Kornienko | 5631202 | 2013-07-04 12:02:44 +0000 | [diff] [blame] | 198 | GoogleStyle.AlwaysBreakBeforeMultilineStrings = true; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 199 | GoogleStyle.AlwaysBreakTemplateDeclarations = true; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 200 | GoogleStyle.BinPackParameters = true; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 201 | GoogleStyle.BreakBeforeBinaryOperators = false; |
| 202 | GoogleStyle.BreakBeforeBraces = FormatStyle::BS_Attach; |
| 203 | GoogleStyle.BreakConstructorInitializersBeforeComma = false; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 204 | GoogleStyle.ColumnLimit = 80; |
| 205 | GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 206 | GoogleStyle.Cpp11BracedListStyle = true; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 207 | GoogleStyle.DerivePointerBinding = true; |
Daniel Jasper | c7bd68f | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 208 | GoogleStyle.ExperimentalAutoDetectBinPacking = false; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 209 | GoogleStyle.IndentCaseLabels = true; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 210 | GoogleStyle.IndentFunctionDeclarationAfterType = true; |
| 211 | GoogleStyle.IndentWidth = 2; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 212 | GoogleStyle.MaxEmptyLinesToKeep = 1; |
Daniel Jasper | eff18b9 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 213 | GoogleStyle.NamespaceIndentation = FormatStyle::NI_None; |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 214 | GoogleStyle.ObjCSpaceBeforeProtocolList = false; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 215 | GoogleStyle.PointerBindsToType = true; |
| 216 | GoogleStyle.SpacesBeforeTrailingComments = 2; |
| 217 | GoogleStyle.Standard = FormatStyle::LS_Auto; |
Manuel Klimek | 7c9a93e | 2013-05-13 09:22:11 +0000 | [diff] [blame] | 218 | GoogleStyle.UseTab = false; |
Daniel Jasper | faec47b | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 219 | |
| 220 | setDefaultPenalties(GoogleStyle); |
| 221 | GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200; |
| 222 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 223 | return GoogleStyle; |
| 224 | } |
| 225 | |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 226 | FormatStyle getChromiumStyle() { |
| 227 | FormatStyle ChromiumStyle = getGoogleStyle(); |
Daniel Jasper | f157960 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 228 | ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false; |
Daniel Jasper | 94d6ad7 | 2013-04-24 13:46:00 +0000 | [diff] [blame] | 229 | ChromiumStyle.AllowShortIfStatementsOnASingleLine = false; |
Daniel Jasper | f11bbb9 | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 230 | ChromiumStyle.AllowShortLoopsOnASingleLine = false; |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 231 | ChromiumStyle.BinPackParameters = false; |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 232 | ChromiumStyle.DerivePointerBinding = false; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 233 | ChromiumStyle.Standard = FormatStyle::LS_Cpp03; |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 234 | return ChromiumStyle; |
| 235 | } |
| 236 | |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 237 | FormatStyle getMozillaStyle() { |
| 238 | FormatStyle MozillaStyle = getLLVMStyle(); |
| 239 | MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false; |
| 240 | MozillaStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; |
| 241 | MozillaStyle.DerivePointerBinding = true; |
| 242 | MozillaStyle.IndentCaseLabels = true; |
| 243 | MozillaStyle.ObjCSpaceBeforeProtocolList = false; |
| 244 | MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200; |
| 245 | MozillaStyle.PointerBindsToType = true; |
| 246 | return MozillaStyle; |
| 247 | } |
| 248 | |
Daniel Jasper | e05dc6d | 2013-07-24 13:10:59 +0000 | [diff] [blame] | 249 | FormatStyle getWebKitStyle() { |
| 250 | FormatStyle Style = getLLVMStyle(); |
Daniel Jasper | eff18b9 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 251 | Style.AccessModifierOffset = -4; |
Daniel Jasper | 893ea8d | 2013-07-31 23:55:15 +0000 | [diff] [blame] | 252 | Style.AlignTrailingComments = false; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 253 | Style.BreakBeforeBinaryOperators = true; |
Daniel Jasper | eff18b9 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 254 | Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 255 | Style.BreakConstructorInitializersBeforeComma = true; |
Daniel Jasper | eff18b9 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 256 | Style.ColumnLimit = 0; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 257 | Style.IndentWidth = 4; |
Daniel Jasper | eff18b9 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 258 | Style.NamespaceIndentation = FormatStyle::NI_Inner; |
Daniel Jasper | e05dc6d | 2013-07-24 13:10:59 +0000 | [diff] [blame] | 259 | Style.PointerBindsToType = true; |
| 260 | return Style; |
| 261 | } |
| 262 | |
Alexander Kornienko | 885f87b | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 263 | bool getPredefinedStyle(StringRef Name, FormatStyle *Style) { |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 264 | if (Name.equals_lower("llvm")) |
Alexander Kornienko | 885f87b | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 265 | *Style = getLLVMStyle(); |
| 266 | else if (Name.equals_lower("chromium")) |
| 267 | *Style = getChromiumStyle(); |
| 268 | else if (Name.equals_lower("mozilla")) |
| 269 | *Style = getMozillaStyle(); |
| 270 | else if (Name.equals_lower("google")) |
| 271 | *Style = getGoogleStyle(); |
Daniel Jasper | e05dc6d | 2013-07-24 13:10:59 +0000 | [diff] [blame] | 272 | else if (Name.equals_lower("webkit")) |
| 273 | *Style = getWebKitStyle(); |
Alexander Kornienko | 885f87b | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 274 | else |
| 275 | return false; |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 276 | |
Alexander Kornienko | 885f87b | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 277 | return true; |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | llvm::error_code parseConfiguration(StringRef Text, FormatStyle *Style) { |
Alexander Kornienko | 107db3c | 2013-05-20 15:18:01 +0000 | [diff] [blame] | 281 | if (Text.trim().empty()) |
| 282 | return llvm::make_error_code(llvm::errc::invalid_argument); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 283 | llvm::yaml::Input Input(Text); |
| 284 | Input >> *Style; |
| 285 | return Input.error(); |
| 286 | } |
| 287 | |
| 288 | std::string configurationAsText(const FormatStyle &Style) { |
| 289 | std::string Text; |
| 290 | llvm::raw_string_ostream Stream(Text); |
| 291 | llvm::yaml::Output Output(Stream); |
| 292 | // We use the same mapping method for input and output, so we need a non-const |
| 293 | // reference here. |
| 294 | FormatStyle NonConstStyle = Style; |
| 295 | Output << NonConstStyle; |
Alexander Kornienko | 2b6acb6 | 2013-05-13 12:56:35 +0000 | [diff] [blame] | 296 | return Stream.str(); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 299 | // Returns the length of everything up to the first possible line break after |
| 300 | // the ), ], } or > matching \c Tok. |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 301 | static unsigned getLengthToMatchingParen(const FormatToken &Tok) { |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 302 | if (Tok.MatchingParen == NULL) |
| 303 | return 0; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 304 | FormatToken *End = Tok.MatchingParen; |
| 305 | while (End->Next && !End->Next->CanBreakBefore) { |
| 306 | End = End->Next; |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 307 | } |
| 308 | return End->TotalLength - Tok.TotalLength + 1; |
| 309 | } |
| 310 | |
Craig Topper | 83f81d7 | 2013-06-30 22:29:28 +0000 | [diff] [blame] | 311 | namespace { |
| 312 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 313 | class UnwrappedLineFormatter { |
| 314 | public: |
Manuel Klimek | 94fc6f1 | 2013-01-10 19:17:33 +0000 | [diff] [blame] | 315 | UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr, |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 316 | const AnnotatedLine &Line, unsigned FirstIndent, |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 317 | const FormatToken *RootToken, |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 318 | WhitespaceManager &Whitespaces, |
Daniel Jasper | c7bd68f | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 319 | encoding::Encoding Encoding, |
| 320 | bool BinPackInconclusiveFunctions) |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 321 | : Style(Style), SourceMgr(SourceMgr), Line(Line), |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 322 | FirstIndent(FirstIndent), RootToken(RootToken), |
Daniel Jasper | c7bd68f | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 323 | Whitespaces(Whitespaces), Count(0), Encoding(Encoding), |
| 324 | BinPackInconclusiveFunctions(BinPackInconclusiveFunctions) {} |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 325 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 326 | /// \brief Formats an \c UnwrappedLine. |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 327 | void format(const AnnotatedLine *NextLine) { |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 328 | // Initialize state dependent on indent. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 329 | LineState State; |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 330 | State.Column = FirstIndent; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 331 | State.NextToken = RootToken; |
Daniel Jasper | 2a409b6 | 2013-07-08 14:34:09 +0000 | [diff] [blame] | 332 | State.Stack.push_back(ParenState(FirstIndent, FirstIndent, |
| 333 | /*AvoidBinPacking=*/false, |
| 334 | /*NoLineBreak=*/false)); |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 335 | State.LineContainsContinuedForLoopSection = false; |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 336 | State.ParenLevel = 0; |
Manuel Klimek | b56b6d1 | 2013-02-20 15:25:48 +0000 | [diff] [blame] | 337 | State.StartOfStringLiteral = 0; |
Daniel Jasper | cf5767d | 2013-02-18 11:05:07 +0000 | [diff] [blame] | 338 | State.StartOfLineLevel = State.ParenLevel; |
Daniel Jasper | 07ca547 | 2013-07-05 09:14:35 +0000 | [diff] [blame] | 339 | State.LowestLevelOnLine = State.ParenLevel; |
Daniel Jasper | 54b4e44 | 2013-05-22 05:27:42 +0000 | [diff] [blame] | 340 | State.IgnoreStackForComparison = false; |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 341 | |
| 342 | // The first token has already been indented and thus consumed. |
Daniel Jasper | 0fde950 | 2013-07-12 11:37:05 +0000 | [diff] [blame] | 343 | moveStateToNextToken(State, /*DryRun=*/false, /*Newline=*/false); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 344 | |
Daniel Jasper | e05dc6d | 2013-07-24 13:10:59 +0000 | [diff] [blame] | 345 | if (Style.ColumnLimit == 0) { |
| 346 | formatWithoutColumnLimit(State); |
| 347 | return; |
| 348 | } |
| 349 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 350 | // If everything fits on a single line, just put it there. |
Daniel Jasper | a4d4621 | 2013-02-28 11:05:57 +0000 | [diff] [blame] | 351 | unsigned ColumnLimit = Style.ColumnLimit; |
| 352 | if (NextLine && NextLine->InPPDirective && |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 353 | !NextLine->First->HasUnescapedNewline) |
Daniel Jasper | a4d4621 | 2013-02-28 11:05:57 +0000 | [diff] [blame] | 354 | ColumnLimit = getColumnLimit(); |
| 355 | if (Line.Last->TotalLength <= ColumnLimit - FirstIndent) { |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 356 | while (State.NextToken != NULL) { |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 357 | addTokenToState(false, false, State); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 358 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 359 | } |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 360 | |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 361 | // If the ObjC method declaration does not fit on a line, we should format |
| 362 | // it with one arg per line. |
| 363 | if (Line.Type == LT_ObjCMethodDecl) |
| 364 | State.Stack.back().BreakBeforeParameter = true; |
| 365 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 366 | // Find best solution in solution space. |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 367 | analyzeSolutionSpace(State); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | private: |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 371 | void DebugTokenState(const FormatToken &FormatTok) { |
| 372 | const Token &Tok = FormatTok.Tok; |
Alexander Kornienko | dd25631 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 373 | llvm::dbgs() << StringRef(SourceMgr.getCharacterData(Tok.getLocation()), |
Daniel Jasper | 1a1ce83 | 2013-01-29 11:27:30 +0000 | [diff] [blame] | 374 | Tok.getLength()); |
Alexander Kornienko | dd25631 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 375 | llvm::dbgs(); |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 378 | struct ParenState { |
Daniel Jasper | d399bff | 2013-02-05 09:41:21 +0000 | [diff] [blame] | 379 | ParenState(unsigned Indent, unsigned LastSpace, bool AvoidBinPacking, |
Daniel Jasper | 001bf4e | 2013-04-22 07:59:53 +0000 | [diff] [blame] | 380 | bool NoLineBreak) |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 381 | : Indent(Indent), LastSpace(LastSpace), FirstLessLess(0), |
| 382 | BreakBeforeClosingBrace(false), QuestionColumn(0), |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 383 | AvoidBinPacking(AvoidBinPacking), BreakBeforeParameter(false), |
Daniel Jasper | 001bf4e | 2013-04-22 07:59:53 +0000 | [diff] [blame] | 384 | NoLineBreak(NoLineBreak), ColonPos(0), StartOfFunctionCall(0), |
Daniel Jasper | 011c35d | 2013-07-12 11:19:37 +0000 | [diff] [blame] | 385 | StartOfArraySubscripts(0), NestedNameSpecifierContinuation(0), |
| 386 | CallContinuation(0), VariablePos(0), ContainsLineBreak(false) {} |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 387 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 388 | /// \brief The position to which a specific parenthesis level needs to be |
| 389 | /// indented. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 390 | unsigned Indent; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 391 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 392 | /// \brief The position of the last space on each level. |
| 393 | /// |
| 394 | /// Used e.g. to break like: |
| 395 | /// functionCall(Parameter, otherCall( |
| 396 | /// OtherParameter)); |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 397 | unsigned LastSpace; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 398 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 399 | /// \brief The position the first "<<" operator encountered on each level. |
| 400 | /// |
| 401 | /// Used to align "<<" operators. 0 if no such operator has been encountered |
| 402 | /// on a level. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 403 | unsigned FirstLessLess; |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 404 | |
Manuel Klimek | c8c8a47 | 2013-01-10 15:58:26 +0000 | [diff] [blame] | 405 | /// \brief Whether a newline needs to be inserted before the block's closing |
| 406 | /// brace. |
| 407 | /// |
| 408 | /// We only want to insert a newline before the closing brace if there also |
| 409 | /// was a newline after the beginning left brace. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 410 | bool BreakBeforeClosingBrace; |
| 411 | |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 412 | /// \brief The column of a \c ? in a conditional expression; |
| 413 | unsigned QuestionColumn; |
| 414 | |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 415 | /// \brief Avoid bin packing, i.e. multiple parameters/elements on multiple |
| 416 | /// lines, in this context. |
| 417 | bool AvoidBinPacking; |
| 418 | |
| 419 | /// \brief Break after the next comma (or all the commas in this context if |
| 420 | /// \c AvoidBinPacking is \c true). |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 421 | bool BreakBeforeParameter; |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 422 | |
Daniel Jasper | 001bf4e | 2013-04-22 07:59:53 +0000 | [diff] [blame] | 423 | /// \brief Line breaking in this context would break a formatting rule. |
| 424 | bool NoLineBreak; |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 425 | |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 426 | /// \brief The position of the colon in an ObjC method declaration/call. |
| 427 | unsigned ColonPos; |
Daniel Jasper | c4615b7 | 2013-02-20 12:56:39 +0000 | [diff] [blame] | 428 | |
Daniel Jasper | 2484971 | 2013-03-01 16:48:32 +0000 | [diff] [blame] | 429 | /// \brief The start of the most recent function in a builder-type call. |
| 430 | unsigned StartOfFunctionCall; |
| 431 | |
Daniel Jasper | 011c35d | 2013-07-12 11:19:37 +0000 | [diff] [blame] | 432 | /// \brief Contains the start of array subscript expressions, so that they |
| 433 | /// can be aligned. |
| 434 | unsigned StartOfArraySubscripts; |
| 435 | |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 436 | /// \brief If a nested name specifier was broken over multiple lines, this |
| 437 | /// contains the start column of the second line. Otherwise 0. |
| 438 | unsigned NestedNameSpecifierContinuation; |
| 439 | |
| 440 | /// \brief If a call expression was broken over multiple lines, this |
| 441 | /// contains the start column of the second line. Otherwise 0. |
| 442 | unsigned CallContinuation; |
| 443 | |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 444 | /// \brief The column of the first variable name in a variable declaration. |
| 445 | /// |
| 446 | /// Used to align further variables if necessary. |
| 447 | unsigned VariablePos; |
| 448 | |
Daniel Jasper | 88cc562 | 2013-07-08 14:25:23 +0000 | [diff] [blame] | 449 | /// \brief \c true if this \c ParenState already contains a line-break. |
Daniel Jasper | c3df5ff | 2013-05-13 09:19:24 +0000 | [diff] [blame] | 450 | /// |
Daniel Jasper | 88cc562 | 2013-07-08 14:25:23 +0000 | [diff] [blame] | 451 | /// The first line break in a certain \c ParenState causes extra penalty so |
| 452 | /// that clang-format prefers similar breaks, i.e. breaks in the same |
| 453 | /// parenthesis. |
| 454 | bool ContainsLineBreak; |
Daniel Jasper | c3df5ff | 2013-05-13 09:19:24 +0000 | [diff] [blame] | 455 | |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 456 | bool operator<(const ParenState &Other) const { |
| 457 | if (Indent != Other.Indent) |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 458 | return Indent < Other.Indent; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 459 | if (LastSpace != Other.LastSpace) |
| 460 | return LastSpace < Other.LastSpace; |
| 461 | if (FirstLessLess != Other.FirstLessLess) |
| 462 | return FirstLessLess < Other.FirstLessLess; |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 463 | if (BreakBeforeClosingBrace != Other.BreakBeforeClosingBrace) |
| 464 | return BreakBeforeClosingBrace; |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 465 | if (QuestionColumn != Other.QuestionColumn) |
| 466 | return QuestionColumn < Other.QuestionColumn; |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 467 | if (AvoidBinPacking != Other.AvoidBinPacking) |
| 468 | return AvoidBinPacking; |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 469 | if (BreakBeforeParameter != Other.BreakBeforeParameter) |
| 470 | return BreakBeforeParameter; |
Daniel Jasper | 001bf4e | 2013-04-22 07:59:53 +0000 | [diff] [blame] | 471 | if (NoLineBreak != Other.NoLineBreak) |
| 472 | return NoLineBreak; |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 473 | if (ColonPos != Other.ColonPos) |
| 474 | return ColonPos < Other.ColonPos; |
Daniel Jasper | 2484971 | 2013-03-01 16:48:32 +0000 | [diff] [blame] | 475 | if (StartOfFunctionCall != Other.StartOfFunctionCall) |
| 476 | return StartOfFunctionCall < Other.StartOfFunctionCall; |
Daniel Jasper | 011c35d | 2013-07-12 11:19:37 +0000 | [diff] [blame] | 477 | if (StartOfArraySubscripts != Other.StartOfArraySubscripts) |
| 478 | return StartOfArraySubscripts < Other.StartOfArraySubscripts; |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 479 | if (CallContinuation != Other.CallContinuation) |
| 480 | return CallContinuation < Other.CallContinuation; |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 481 | if (VariablePos != Other.VariablePos) |
| 482 | return VariablePos < Other.VariablePos; |
Daniel Jasper | 88cc562 | 2013-07-08 14:25:23 +0000 | [diff] [blame] | 483 | if (ContainsLineBreak != Other.ContainsLineBreak) |
| 484 | return ContainsLineBreak < Other.ContainsLineBreak; |
Daniel Jasper | b312314 | 2013-01-12 07:36:22 +0000 | [diff] [blame] | 485 | return false; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 486 | } |
| 487 | }; |
| 488 | |
| 489 | /// \brief The current state when indenting a unwrapped line. |
| 490 | /// |
| 491 | /// As the indenting tries different combinations this is copied by value. |
| 492 | struct LineState { |
| 493 | /// \brief The number of used columns in the current line. |
| 494 | unsigned Column; |
| 495 | |
| 496 | /// \brief The token that needs to be next formatted. |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 497 | const FormatToken *NextToken; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 498 | |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 499 | /// \brief \c true if this line contains a continued for-loop section. |
| 500 | bool LineContainsContinuedForLoopSection; |
| 501 | |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 502 | /// \brief The level of nesting inside (), [], <> and {}. |
| 503 | unsigned ParenLevel; |
| 504 | |
Daniel Jasper | cf5767d | 2013-02-18 11:05:07 +0000 | [diff] [blame] | 505 | /// \brief The \c ParenLevel at the start of this line. |
| 506 | unsigned StartOfLineLevel; |
| 507 | |
Daniel Jasper | 07ca547 | 2013-07-05 09:14:35 +0000 | [diff] [blame] | 508 | /// \brief The lowest \c ParenLevel on the current line. |
| 509 | unsigned LowestLevelOnLine; |
Daniel Jasper | 259a038 | 2013-05-27 11:50:16 +0000 | [diff] [blame] | 510 | |
Manuel Klimek | b56b6d1 | 2013-02-20 15:25:48 +0000 | [diff] [blame] | 511 | /// \brief The start column of the string literal, if we're in a string |
| 512 | /// literal sequence, 0 otherwise. |
| 513 | unsigned StartOfStringLiteral; |
| 514 | |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 515 | /// \brief A stack keeping track of properties applying to parenthesis |
| 516 | /// levels. |
| 517 | std::vector<ParenState> Stack; |
| 518 | |
Daniel Jasper | 54b4e44 | 2013-05-22 05:27:42 +0000 | [diff] [blame] | 519 | /// \brief Ignore the stack of \c ParenStates for state comparison. |
| 520 | /// |
| 521 | /// In long and deeply nested unwrapped lines, the current algorithm can |
| 522 | /// be insufficient for finding the best formatting with a reasonable amount |
| 523 | /// of time and memory. Setting this flag will effectively lead to the |
| 524 | /// algorithm not analyzing some combinations. However, these combinations |
| 525 | /// rarely contain the optimal solution: In short, accepting a higher |
| 526 | /// penalty early would need to lead to different values in the \c |
| 527 | /// ParenState stack (in an otherwise identical state) and these different |
| 528 | /// values would need to lead to a significant amount of avoided penalty |
| 529 | /// later. |
| 530 | /// |
| 531 | /// FIXME: Come up with a better algorithm instead. |
| 532 | bool IgnoreStackForComparison; |
| 533 | |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 534 | /// \brief Comparison operator to be able to used \c LineState in \c map. |
| 535 | bool operator<(const LineState &Other) const { |
Daniel Jasper | d789670 | 2013-02-19 09:28:55 +0000 | [diff] [blame] | 536 | if (NextToken != Other.NextToken) |
| 537 | return NextToken < Other.NextToken; |
| 538 | if (Column != Other.Column) |
| 539 | return Column < Other.Column; |
Daniel Jasper | d789670 | 2013-02-19 09:28:55 +0000 | [diff] [blame] | 540 | if (LineContainsContinuedForLoopSection != |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 541 | Other.LineContainsContinuedForLoopSection) |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 542 | return LineContainsContinuedForLoopSection; |
Daniel Jasper | d789670 | 2013-02-19 09:28:55 +0000 | [diff] [blame] | 543 | if (ParenLevel != Other.ParenLevel) |
| 544 | return ParenLevel < Other.ParenLevel; |
| 545 | if (StartOfLineLevel != Other.StartOfLineLevel) |
| 546 | return StartOfLineLevel < Other.StartOfLineLevel; |
Daniel Jasper | 07ca547 | 2013-07-05 09:14:35 +0000 | [diff] [blame] | 547 | if (LowestLevelOnLine != Other.LowestLevelOnLine) |
| 548 | return LowestLevelOnLine < Other.LowestLevelOnLine; |
Manuel Klimek | b56b6d1 | 2013-02-20 15:25:48 +0000 | [diff] [blame] | 549 | if (StartOfStringLiteral != Other.StartOfStringLiteral) |
| 550 | return StartOfStringLiteral < Other.StartOfStringLiteral; |
Daniel Jasper | 54b4e44 | 2013-05-22 05:27:42 +0000 | [diff] [blame] | 551 | if (IgnoreStackForComparison || Other.IgnoreStackForComparison) |
| 552 | return false; |
Daniel Jasper | d789670 | 2013-02-19 09:28:55 +0000 | [diff] [blame] | 553 | return Stack < Other.Stack; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 554 | } |
| 555 | }; |
| 556 | |
Daniel Jasper | e05dc6d | 2013-07-24 13:10:59 +0000 | [diff] [blame] | 557 | /// \brief Formats the line starting at \p State, simply keeping all of the |
| 558 | /// input's line breaking decisions. |
| 559 | void formatWithoutColumnLimit(LineState &State) { |
| 560 | while (State.NextToken != NULL) { |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 561 | bool Newline = mustBreak(State) || |
| 562 | (canBreak(State) && State.NextToken->NewlinesBefore > 0); |
Daniel Jasper | e05dc6d | 2013-07-24 13:10:59 +0000 | [diff] [blame] | 563 | addTokenToState(Newline, /*DryRun=*/false, State); |
| 564 | } |
| 565 | } |
| 566 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 567 | /// \brief Appends the next token to \p State and updates information |
| 568 | /// necessary for indentation. |
| 569 | /// |
Nico Weber | 1907c57 | 2013-06-26 02:42:46 +0000 | [diff] [blame] | 570 | /// Puts the token on the current line if \p Newline is \c false and adds a |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 571 | /// line break and necessary indentation otherwise. |
| 572 | /// |
| 573 | /// If \p DryRun is \c false, also creates and stores the required |
| 574 | /// \c Replacement. |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 575 | unsigned addTokenToState(bool Newline, bool DryRun, LineState &State) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 576 | const FormatToken &Current = *State.NextToken; |
| 577 | const FormatToken &Previous = *State.NextToken->Previous; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 578 | |
Daniel Jasper | faec47b | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 579 | // Extra penalty that needs to be added because of the way certain line |
| 580 | // breaks are chosen. |
| 581 | unsigned ExtraPenalty = 0; |
| 582 | |
Daniel Jasper | 92f9faf | 2013-03-20 15:58:10 +0000 | [diff] [blame] | 583 | if (State.Stack.size() == 0 || Current.Type == TT_ImplicitStringLiteral) { |
Manuel Klimek | ad3094b | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 584 | // FIXME: Is this correct? |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 585 | int WhitespaceLength = SourceMgr.getSpellingColumnNumber( |
| 586 | State.NextToken->WhitespaceRange.getEnd()) - |
| 587 | SourceMgr.getSpellingColumnNumber( |
| 588 | State.NextToken->WhitespaceRange.getBegin()); |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 589 | State.Column += WhitespaceLength + State.NextToken->CodePointCount; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 590 | State.NextToken = State.NextToken->Next; |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 591 | return 0; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Daniel Jasper | 3776ef3 | 2013-04-03 07:21:51 +0000 | [diff] [blame] | 594 | // If we are continuing an expression, we want to indent an extra 4 spaces. |
| 595 | unsigned ContinuationIndent = |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 596 | std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) + 4; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 597 | if (Newline) { |
Daniel Jasper | 88cc562 | 2013-07-08 14:25:23 +0000 | [diff] [blame] | 598 | State.Stack.back().ContainsLineBreak = true; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 599 | if (Current.is(tok::r_brace)) { |
Daniel Jasper | 0de1c4d | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 600 | if (Current.BlockKind == BK_BracedInit) |
| 601 | State.Column = State.Stack[State.Stack.size() - 2].LastSpace; |
| 602 | else |
Daniel Jasper | 15ec3a8 | 2013-07-11 21:27:40 +0000 | [diff] [blame] | 603 | State.Column = FirstIndent; |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 604 | } else if (Current.is(tok::string_literal) && |
Manuel Klimek | b56b6d1 | 2013-02-20 15:25:48 +0000 | [diff] [blame] | 605 | State.StartOfStringLiteral != 0) { |
| 606 | State.Column = State.StartOfStringLiteral; |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 607 | State.Stack.back().BreakBeforeParameter = true; |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 608 | } else if (Current.is(tok::lessless) && |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 609 | State.Stack.back().FirstLessLess != 0) { |
| 610 | State.Column = State.Stack.back().FirstLessLess; |
Daniel Jasper | 5ad390d | 2013-05-28 11:30:49 +0000 | [diff] [blame] | 611 | } else if (Current.isOneOf(tok::period, tok::arrow) && |
| 612 | Current.Type != TT_DesignatedInitializerPeriod) { |
Daniel Jasper | 3776ef3 | 2013-04-03 07:21:51 +0000 | [diff] [blame] | 613 | if (State.Stack.back().CallContinuation == 0) { |
| 614 | State.Column = ContinuationIndent; |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 615 | State.Stack.back().CallContinuation = State.Column; |
Daniel Jasper | 3776ef3 | 2013-04-03 07:21:51 +0000 | [diff] [blame] | 616 | } else { |
| 617 | State.Column = State.Stack.back().CallContinuation; |
| 618 | } |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 619 | } else if (Current.Type == TT_ConditionalExpr) { |
| 620 | State.Column = State.Stack.back().QuestionColumn; |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 621 | } else if (Previous.is(tok::comma) && |
| 622 | State.Stack.back().VariablePos != 0) { |
| 623 | State.Column = State.Stack.back().VariablePos; |
Daniel Jasper | 3c08a81 | 2013-02-24 18:54:32 +0000 | [diff] [blame] | 624 | } else if (Previous.ClosesTemplateDeclaration || |
Daniel Jasper | 6561f6a | 2013-07-09 07:43:55 +0000 | [diff] [blame] | 625 | ((Current.Type == TT_StartOfName || |
| 626 | Current.is(tok::kw_operator)) && |
| 627 | State.ParenLevel == 0 && |
Manuel Klimek | a9a7f10 | 2013-06-21 17:25:42 +0000 | [diff] [blame] | 628 | (!Style.IndentFunctionDeclarationAfterType || |
| 629 | Line.StartsDefinition))) { |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 630 | State.Column = State.Stack.back().Indent; |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 631 | } else if (Current.Type == TT_ObjCSelectorName) { |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 632 | if (State.Stack.back().ColonPos > Current.CodePointCount) { |
| 633 | State.Column = State.Stack.back().ColonPos - Current.CodePointCount; |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 634 | } else { |
| 635 | State.Column = State.Stack.back().Indent; |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 636 | State.Stack.back().ColonPos = State.Column + Current.CodePointCount; |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 637 | } |
Daniel Jasper | 011c35d | 2013-07-12 11:19:37 +0000 | [diff] [blame] | 638 | } else if (Current.is(tok::l_square) && |
| 639 | Current.Type != TT_ObjCMethodExpr) { |
| 640 | if (State.Stack.back().StartOfArraySubscripts != 0) |
| 641 | State.Column = State.Stack.back().StartOfArraySubscripts; |
| 642 | else |
| 643 | State.Column = ContinuationIndent; |
Daniel Jasper | b2f063a | 2013-05-08 10:00:18 +0000 | [diff] [blame] | 644 | } else if (Current.Type == TT_StartOfName || |
| 645 | Previous.isOneOf(tok::coloncolon, tok::equal) || |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 646 | Previous.Type == TT_ObjCMethodExpr) { |
Daniel Jasper | 3776ef3 | 2013-04-03 07:21:51 +0000 | [diff] [blame] | 647 | State.Column = ContinuationIndent; |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 648 | } else { |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 649 | State.Column = State.Stack.back().Indent; |
Daniel Jasper | 3776ef3 | 2013-04-03 07:21:51 +0000 | [diff] [blame] | 650 | // Ensure that we fall back to indenting 4 spaces instead of just |
| 651 | // flushing continuations left. |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 652 | if (State.Column == FirstIndent) |
| 653 | State.Column += 4; |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Daniel Jasper | 7878a7b | 2013-02-15 11:07:25 +0000 | [diff] [blame] | 656 | if (Current.is(tok::question)) |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 657 | State.Stack.back().BreakBeforeParameter = true; |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 658 | if ((Previous.isOneOf(tok::comma, tok::semi) && |
| 659 | !State.Stack.back().AvoidBinPacking) || |
| 660 | Previous.Type == TT_BinaryOperator) |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 661 | State.Stack.back().BreakBeforeParameter = false; |
Daniel Jasper | 33f4b90 | 2013-05-15 09:35:08 +0000 | [diff] [blame] | 662 | if (Previous.Type == TT_TemplateCloser && State.ParenLevel == 0) |
| 663 | State.Stack.back().BreakBeforeParameter = false; |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 664 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 665 | if (!DryRun) { |
Daniel Jasper | 1ef81d5 | 2013-02-26 13:10:34 +0000 | [diff] [blame] | 666 | unsigned NewLines = 1; |
Alexander Kornienko | e3f1197 | 2013-06-12 19:04:12 +0000 | [diff] [blame] | 667 | if (Current.is(tok::comment)) |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 668 | NewLines = std::max( |
| 669 | NewLines, |
| 670 | std::min(Current.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1)); |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 671 | Whitespaces.replaceWhitespace(Current, NewLines, State.Column, |
| 672 | State.Column, Line.InPPDirective); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 673 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 674 | |
Daniel Jasper | e1f9a8e | 2013-07-11 13:48:16 +0000 | [diff] [blame] | 675 | if (!Current.isTrailingComment()) |
| 676 | State.Stack.back().LastSpace = State.Column; |
Daniel Jasper | 5ad390d | 2013-05-28 11:30:49 +0000 | [diff] [blame] | 677 | if (Current.isOneOf(tok::arrow, tok::period) && |
| 678 | Current.Type != TT_DesignatedInitializerPeriod) |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 679 | State.Stack.back().LastSpace += Current.CodePointCount; |
Daniel Jasper | cf5767d | 2013-02-18 11:05:07 +0000 | [diff] [blame] | 680 | State.StartOfLineLevel = State.ParenLevel; |
Daniel Jasper | 07ca547 | 2013-07-05 09:14:35 +0000 | [diff] [blame] | 681 | State.LowestLevelOnLine = State.ParenLevel; |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 682 | |
| 683 | // Any break on this level means that the parent level has been broken |
| 684 | // and we need to avoid bin packing there. |
| 685 | for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) { |
| 686 | State.Stack[i].BreakBeforeParameter = true; |
| 687 | } |
Alexander Kornienko | 0bdc643 | 2013-07-04 14:47:51 +0000 | [diff] [blame] | 688 | const FormatToken *TokenBefore = Current.getPreviousNonComment(); |
Daniel Jasper | 01218ff | 2013-04-15 22:36:37 +0000 | [diff] [blame] | 689 | if (TokenBefore && !TokenBefore->isOneOf(tok::comma, tok::semi) && |
Daniel Jasper | 33f4b90 | 2013-05-15 09:35:08 +0000 | [diff] [blame] | 690 | TokenBefore->Type != TT_TemplateCloser && |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 691 | TokenBefore->Type != TT_BinaryOperator && !TokenBefore->opensScope()) |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 692 | State.Stack.back().BreakBeforeParameter = true; |
| 693 | |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 694 | // If we break after {, we should also break before the corresponding }. |
| 695 | if (Previous.is(tok::l_brace)) |
| 696 | State.Stack.back().BreakBeforeClosingBrace = true; |
| 697 | |
| 698 | if (State.Stack.back().AvoidBinPacking) { |
| 699 | // If we are breaking after '(', '{', '<', this is not bin packing |
| 700 | // unless AllowAllParametersOfDeclarationOnNextLine is false. |
Daniel Jasper | d741f02 | 2013-05-14 20:39:56 +0000 | [diff] [blame] | 701 | if (!(Previous.isOneOf(tok::l_paren, tok::l_brace) || |
| 702 | Previous.Type == TT_BinaryOperator) || |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 703 | (!Style.AllowAllParametersOfDeclarationOnNextLine && |
| 704 | Line.MustBeDeclaration)) |
| 705 | State.Stack.back().BreakBeforeParameter = true; |
| 706 | } |
Daniel Jasper | faec47b | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 707 | |
| 708 | // Breaking before the first "<<" is generally not desirable. |
| 709 | if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0) |
| 710 | ExtraPenalty += Style.PenaltyBreakFirstLessLess; |
| 711 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 712 | } else { |
Daniel Jasper | 9c3e71a | 2013-02-25 15:59:54 +0000 | [diff] [blame] | 713 | if (Current.is(tok::equal) && |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 714 | (RootToken->is(tok::kw_for) || State.ParenLevel == 0) && |
Daniel Jasper | adc0f09 | 2013-04-05 09:38:50 +0000 | [diff] [blame] | 715 | State.Stack.back().VariablePos == 0) { |
| 716 | State.Stack.back().VariablePos = State.Column; |
| 717 | // Move over * and & if they are bound to the variable name. |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 718 | const FormatToken *Tok = &Previous; |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 719 | while (Tok && State.Stack.back().VariablePos >= Tok->CodePointCount) { |
| 720 | State.Stack.back().VariablePos -= Tok->CodePointCount; |
Daniel Jasper | adc0f09 | 2013-04-05 09:38:50 +0000 | [diff] [blame] | 721 | if (Tok->SpacesRequiredBefore != 0) |
| 722 | break; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 723 | Tok = Tok->Previous; |
Daniel Jasper | adc0f09 | 2013-04-05 09:38:50 +0000 | [diff] [blame] | 724 | } |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 725 | if (Previous.PartOfMultiVariableDeclStmt) |
| 726 | State.Stack.back().LastSpace = State.Stack.back().VariablePos; |
| 727 | } |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 728 | |
Daniel Jasper | 729a743 | 2013-02-11 12:36:37 +0000 | [diff] [blame] | 729 | unsigned Spaces = State.NextToken->SpacesRequiredBefore; |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 730 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 731 | if (!DryRun) |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 732 | Whitespaces.replaceWhitespace(Current, 0, Spaces, |
| 733 | State.Column + Spaces); |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 734 | |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 735 | if (Current.Type == TT_ObjCSelectorName && |
| 736 | State.Stack.back().ColonPos == 0) { |
| 737 | if (State.Stack.back().Indent + Current.LongestObjCSelectorName > |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 738 | State.Column + Spaces + Current.CodePointCount) |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 739 | State.Stack.back().ColonPos = |
| 740 | State.Stack.back().Indent + Current.LongestObjCSelectorName; |
| 741 | else |
| 742 | State.Stack.back().ColonPos = |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 743 | State.Column + Spaces + Current.CodePointCount; |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Daniel Jasper | ac3223e | 2013-04-10 09:49:49 +0000 | [diff] [blame] | 746 | if (Previous.opensScope() && Previous.Type != TT_ObjCMethodExpr && |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 747 | Current.Type != TT_LineComment) |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 748 | State.Stack.back().Indent = State.Column + Spaces; |
Daniel Jasper | 001bf4e | 2013-04-22 07:59:53 +0000 | [diff] [blame] | 749 | if (Previous.is(tok::comma) && !Current.isTrailingComment() && |
| 750 | State.Stack.back().AvoidBinPacking) |
| 751 | State.Stack.back().NoLineBreak = true; |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 752 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 753 | State.Column += Spaces; |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 754 | if (Current.is(tok::l_paren) && Previous.isOneOf(tok::kw_if, tok::kw_for)) |
Daniel Jasper | e438bac | 2013-01-23 20:41:06 +0000 | [diff] [blame] | 755 | // Treat the condition inside an if as if it was a second function |
| 756 | // parameter, i.e. let nested calls have an indent of 4. |
| 757 | State.Stack.back().LastSpace = State.Column + 1; // 1 is length of "(". |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 758 | else if (Previous.is(tok::comma)) |
Daniel Jasper | e438bac | 2013-01-23 20:41:06 +0000 | [diff] [blame] | 759 | State.Stack.back().LastSpace = State.Column; |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 760 | else if ((Previous.Type == TT_BinaryOperator || |
Daniel Jasper | 02b771e | 2013-01-28 13:31:35 +0000 | [diff] [blame] | 761 | Previous.Type == TT_ConditionalExpr || |
| 762 | Previous.Type == TT_CtorInitializerColon) && |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 763 | !(Previous.getPrecedence() == prec::Assignment && |
Daniel Jasper | 512843a | 2013-05-27 12:45:09 +0000 | [diff] [blame] | 764 | Current.FakeLParens.empty())) |
| 765 | // Always indent relative to the RHS of the expression unless this is a |
| 766 | // simple assignment without binary expression on the RHS. |
Daniel Jasper | ae8699b | 2013-01-28 09:35:24 +0000 | [diff] [blame] | 767 | State.Stack.back().LastSpace = State.Column; |
Daniel Jasper | 6cabab4 | 2013-02-14 08:42:54 +0000 | [diff] [blame] | 768 | else if (Previous.Type == TT_InheritanceColon) |
| 769 | State.Stack.back().Indent = State.Column; |
Daniel Jasper | b149179 | 2013-07-09 11:57:27 +0000 | [diff] [blame] | 770 | else if (Previous.opensScope()) { |
| 771 | // If a function has multiple parameters (including a single parameter |
Daniel Jasper | 2ca3741 | 2013-07-09 14:36:48 +0000 | [diff] [blame] | 772 | // that is a binary expression) or a trailing call, indent all |
Daniel Jasper | b149179 | 2013-07-09 11:57:27 +0000 | [diff] [blame] | 773 | // parameters from the opening parenthesis. This avoids confusing |
| 774 | // indents like: |
| 775 | // OuterFunction(InnerFunctionCall( |
| 776 | // ParameterToInnerFunction), |
| 777 | // SecondParameterToOuterFunction); |
| 778 | bool HasMultipleParameters = !Current.FakeLParens.empty(); |
| 779 | bool HasTrailingCall = false; |
| 780 | if (Previous.MatchingParen) { |
| 781 | const FormatToken *Next = Previous.MatchingParen->getNextNonComment(); |
| 782 | if (Next && Next->isOneOf(tok::period, tok::arrow)) |
| 783 | HasTrailingCall = true; |
| 784 | } |
| 785 | if (HasMultipleParameters || HasTrailingCall) |
| 786 | State.Stack.back().LastSpace = State.Column; |
| 787 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 788 | } |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 789 | |
Daniel Jasper | 0fde950 | 2013-07-12 11:37:05 +0000 | [diff] [blame] | 790 | return moveStateToNextToken(State, DryRun, Newline) + ExtraPenalty; |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 791 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 792 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 793 | /// \brief Mark the next token as consumed in \p State and modify its stacks |
| 794 | /// accordingly. |
Daniel Jasper | 0fde950 | 2013-07-12 11:37:05 +0000 | [diff] [blame] | 795 | unsigned moveStateToNextToken(LineState &State, bool DryRun, bool Newline) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 796 | const FormatToken &Current = *State.NextToken; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 797 | assert(State.Stack.size()); |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 798 | |
Daniel Jasper | 6cabab4 | 2013-02-14 08:42:54 +0000 | [diff] [blame] | 799 | if (Current.Type == TT_InheritanceColon) |
| 800 | State.Stack.back().AvoidBinPacking = true; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 801 | if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0) |
| 802 | State.Stack.back().FirstLessLess = State.Column; |
Daniel Jasper | 011c35d | 2013-07-12 11:19:37 +0000 | [diff] [blame] | 803 | if (Current.is(tok::l_square) && |
| 804 | State.Stack.back().StartOfArraySubscripts == 0) |
| 805 | State.Stack.back().StartOfArraySubscripts = State.Column; |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 806 | if (Current.is(tok::question)) |
| 807 | State.Stack.back().QuestionColumn = State.Column; |
Daniel Jasper | 07ca547 | 2013-07-05 09:14:35 +0000 | [diff] [blame] | 808 | if (!Current.opensScope() && !Current.closesScope()) |
| 809 | State.LowestLevelOnLine = |
| 810 | std::min(State.LowestLevelOnLine, State.ParenLevel); |
| 811 | if (Current.isOneOf(tok::period, tok::arrow) && |
| 812 | Line.Type == LT_BuilderTypeCall && State.ParenLevel == 0) |
| 813 | State.Stack.back().StartOfFunctionCall = |
| 814 | Current.LastInChainOfCalls ? 0 |
| 815 | : State.Column + Current.CodePointCount; |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 816 | if (Current.Type == TT_CtorInitializerColon) { |
Manuel Klimek | 07a64ec | 2013-05-13 08:42:42 +0000 | [diff] [blame] | 817 | // Indent 2 from the column, so: |
| 818 | // SomeClass::SomeClass() |
| 819 | // : First(...), ... |
| 820 | // Next(...) |
| 821 | // ^ line up here. |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 822 | if (!Style.BreakConstructorInitializersBeforeComma) |
| 823 | State.Stack.back().Indent = State.Column + 2; |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 824 | if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine) |
| 825 | State.Stack.back().AvoidBinPacking = true; |
| 826 | State.Stack.back().BreakBeforeParameter = false; |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 827 | } |
Daniel Jasper | 3776ef3 | 2013-04-03 07:21:51 +0000 | [diff] [blame] | 828 | |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 829 | // If return returns a binary expression, align after it. |
| 830 | if (Current.is(tok::kw_return) && !Current.FakeLParens.empty()) |
| 831 | State.Stack.back().LastSpace = State.Column + 7; |
| 832 | |
Daniel Jasper | 3776ef3 | 2013-04-03 07:21:51 +0000 | [diff] [blame] | 833 | // In ObjC method declaration we align on the ":" of parameters, but we need |
| 834 | // to ensure that we indent parameters on subsequent lines by at least 4. |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 835 | if (Current.Type == TT_ObjCMethodSpecifier) |
| 836 | State.Stack.back().Indent += 4; |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 837 | |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 838 | // Insert scopes created by fake parenthesis. |
Alexander Kornienko | 0bdc643 | 2013-07-04 14:47:51 +0000 | [diff] [blame] | 839 | const FormatToken *Previous = Current.getPreviousNonComment(); |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 840 | // Don't add extra indentation for the first fake parenthesis after |
| 841 | // 'return', assignements or opening <({[. The indentation for these cases |
| 842 | // is special cased. |
| 843 | bool SkipFirstExtraIndent = |
| 844 | Current.is(tok::kw_return) || |
Daniel Jasper | ac3223e | 2013-04-10 09:49:49 +0000 | [diff] [blame] | 845 | (Previous && (Previous->opensScope() || |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 846 | Previous->getPrecedence() == prec::Assignment)); |
Craig Topper | 163fbf8 | 2013-07-08 03:55:09 +0000 | [diff] [blame] | 847 | for (SmallVectorImpl<prec::Level>::const_reverse_iterator |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 848 | I = Current.FakeLParens.rbegin(), |
| 849 | E = Current.FakeLParens.rend(); |
| 850 | I != E; ++I) { |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 851 | ParenState NewParenState = State.Stack.back(); |
Daniel Jasper | 88cc562 | 2013-07-08 14:25:23 +0000 | [diff] [blame] | 852 | NewParenState.ContainsLineBreak = false; |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 853 | NewParenState.Indent = |
| 854 | std::max(std::max(State.Column, NewParenState.Indent), |
| 855 | State.Stack.back().LastSpace); |
| 856 | |
| 857 | // Always indent conditional expressions. Never indent expression where |
| 858 | // the 'operator' is ',', ';' or an assignment (i.e. *I <= |
| 859 | // prec::Assignment) as those have different indentation rules. Indent |
| 860 | // other expression, unless the indentation needs to be skipped. |
| 861 | if (*I == prec::Conditional || |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 862 | (!SkipFirstExtraIndent && *I > prec::Assignment && |
| 863 | !Style.BreakBeforeBinaryOperators)) |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 864 | NewParenState.Indent += 4; |
Daniel Jasper | ac3223e | 2013-04-10 09:49:49 +0000 | [diff] [blame] | 865 | if (Previous && !Previous->opensScope()) |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 866 | NewParenState.BreakBeforeParameter = false; |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 867 | State.Stack.push_back(NewParenState); |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 868 | SkipFirstExtraIndent = false; |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 871 | // If we encounter an opening (, [, { or <, we add a level to our stacks to |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 872 | // prepare for the following tokens. |
Daniel Jasper | ac3223e | 2013-04-10 09:49:49 +0000 | [diff] [blame] | 873 | if (Current.opensScope()) { |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 874 | unsigned NewIndent; |
Daniel Jasper | c3df5ff | 2013-05-13 09:19:24 +0000 | [diff] [blame] | 875 | unsigned LastSpace = State.Stack.back().LastSpace; |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 876 | bool AvoidBinPacking; |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 877 | if (Current.is(tok::l_brace)) { |
Daniel Jasper | b5dc3f4 | 2013-07-16 18:22:10 +0000 | [diff] [blame] | 878 | NewIndent = |
| 879 | LastSpace + (Style.Cpp11BracedListStyle ? 4 : Style.IndentWidth); |
Alexander Kornienko | 0bdc643 | 2013-07-04 14:47:51 +0000 | [diff] [blame] | 880 | const FormatToken *NextNoComment = Current.getNextNonComment(); |
Daniel Jasper | 5ad390d | 2013-05-28 11:30:49 +0000 | [diff] [blame] | 881 | AvoidBinPacking = NextNoComment && |
| 882 | NextNoComment->Type == TT_DesignatedInitializerPeriod; |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 883 | } else { |
Daniel Jasper | c3df5ff | 2013-05-13 09:19:24 +0000 | [diff] [blame] | 884 | NewIndent = |
| 885 | 4 + std::max(LastSpace, State.Stack.back().StartOfFunctionCall); |
Daniel Jasper | c7bd68f | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 886 | AvoidBinPacking = !Style.BinPackParameters || |
| 887 | (Style.ExperimentalAutoDetectBinPacking && |
| 888 | (Current.PackingKind == PPK_OnePerLine || |
| 889 | (!BinPackInconclusiveFunctions && |
| 890 | Current.PackingKind == PPK_Inconclusive))); |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 891 | } |
Daniel Jasper | fca24bc | 2013-04-25 13:31:51 +0000 | [diff] [blame] | 892 | |
Daniel Jasper | c3df5ff | 2013-05-13 09:19:24 +0000 | [diff] [blame] | 893 | State.Stack.push_back(ParenState(NewIndent, LastSpace, AvoidBinPacking, |
| 894 | State.Stack.back().NoLineBreak)); |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 895 | ++State.ParenLevel; |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 898 | // If this '[' opens an ObjC call, determine whether all parameters fit into |
| 899 | // one line and put one per line if they don't. |
| 900 | if (Current.is(tok::l_square) && Current.Type == TT_ObjCMethodExpr && |
| 901 | Current.MatchingParen != NULL) { |
| 902 | if (getLengthToMatchingParen(Current) + State.Column > getColumnLimit()) |
| 903 | State.Stack.back().BreakBeforeParameter = true; |
| 904 | } |
| 905 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 906 | // If we encounter a closing ), ], } or >, we can remove a level from our |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 907 | // stacks. |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 908 | if (Current.isOneOf(tok::r_paren, tok::r_square) || |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 909 | (Current.is(tok::r_brace) && State.NextToken != RootToken) || |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 910 | State.NextToken->Type == TT_TemplateCloser) { |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 911 | State.Stack.pop_back(); |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 912 | --State.ParenLevel; |
| 913 | } |
Daniel Jasper | 011c35d | 2013-07-12 11:19:37 +0000 | [diff] [blame] | 914 | if (Current.is(tok::r_square)) { |
| 915 | // If this ends the array subscript expr, reset the corresponding value. |
| 916 | const FormatToken *NextNonComment = Current.getNextNonComment(); |
| 917 | if (NextNonComment && NextNonComment->isNot(tok::l_square)) |
Daniel Jasper | 9637dda | 2013-07-15 14:33:14 +0000 | [diff] [blame] | 918 | State.Stack.back().StartOfArraySubscripts = 0; |
Daniel Jasper | 011c35d | 2013-07-12 11:19:37 +0000 | [diff] [blame] | 919 | } |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 920 | |
| 921 | // Remove scopes created by fake parenthesis. |
| 922 | for (unsigned i = 0, e = Current.FakeRParens; i != e; ++i) { |
Daniel Jasper | abfc9c1 | 2013-04-04 19:31:00 +0000 | [diff] [blame] | 923 | unsigned VariablePos = State.Stack.back().VariablePos; |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 924 | State.Stack.pop_back(); |
Daniel Jasper | abfc9c1 | 2013-04-04 19:31:00 +0000 | [diff] [blame] | 925 | State.Stack.back().VariablePos = VariablePos; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 926 | } |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 927 | |
Daniel Jasper | 27c7f54 | 2013-05-13 20:50:15 +0000 | [diff] [blame] | 928 | if (Current.is(tok::string_literal) && State.StartOfStringLiteral == 0) { |
Manuel Klimek | b56b6d1 | 2013-02-20 15:25:48 +0000 | [diff] [blame] | 929 | State.StartOfStringLiteral = State.Column; |
Daniel Jasper | 27c7f54 | 2013-05-13 20:50:15 +0000 | [diff] [blame] | 930 | } else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash, |
| 931 | tok::string_literal)) { |
Daniel Jasper | 9a2f8d0 | 2013-05-16 04:26:02 +0000 | [diff] [blame] | 932 | State.StartOfStringLiteral = 0; |
Manuel Klimek | b56b6d1 | 2013-02-20 15:25:48 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 935 | State.Column += Current.CodePointCount; |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 936 | |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 937 | State.NextToken = State.NextToken->Next; |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 938 | |
Daniel Jasper | 0fde950 | 2013-07-12 11:37:05 +0000 | [diff] [blame] | 939 | if (!Newline && Style.AlwaysBreakBeforeMultilineStrings && |
Daniel Jasper | 75e58bb | 2013-08-02 11:01:15 +0000 | [diff] [blame] | 940 | Current.is(tok::string_literal) && Current.CanBreakBefore) |
Daniel Jasper | 0fde950 | 2013-07-12 11:37:05 +0000 | [diff] [blame] | 941 | return 0; |
| 942 | |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 943 | return breakProtrudingToken(Current, State, DryRun); |
| 944 | } |
| 945 | |
| 946 | /// \brief If the current token sticks out over the end of the line, break |
| 947 | /// it if possible. |
Manuel Klimek | 2a9805d | 2013-05-14 09:04:24 +0000 | [diff] [blame] | 948 | /// |
| 949 | /// \returns An extra penalty if a token was broken, otherwise 0. |
| 950 | /// |
Alexander Kornienko | d446f73 | 2013-07-01 13:42:42 +0000 | [diff] [blame] | 951 | /// The returned penalty will cover the cost of the additional line breaks and |
| 952 | /// column limit violation in all lines except for the last one. The penalty |
| 953 | /// for the column limit violation in the last line (and in single line |
| 954 | /// tokens) is handled in \c addNextStateToQueue. |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 955 | unsigned breakProtrudingToken(const FormatToken &Current, LineState &State, |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 956 | bool DryRun) { |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 957 | llvm::OwningPtr<BreakableToken> Token; |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 958 | unsigned StartColumn = State.Column - Current.CodePointCount; |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 959 | unsigned OriginalStartColumn = |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 960 | SourceMgr.getSpellingColumnNumber(Current.getStartOfNonWhitespace()) - |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 961 | 1; |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 962 | |
Daniel Jasper | 5d5b424 | 2013-05-16 12:59:13 +0000 | [diff] [blame] | 963 | if (Current.is(tok::string_literal) && |
| 964 | Current.Type != TT_ImplicitStringLiteral) { |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 965 | // Only break up default narrow strings. |
Alexander Kornienko | 16a0ec6 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 966 | if (!Current.TokenText.startswith("\"")) |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 967 | return 0; |
Alexander Kornienko | 10c26b2 | 2013-07-16 21:06:13 +0000 | [diff] [blame] | 968 | // Don't break string literals with escaped newlines. As clang-format must |
| 969 | // not change the string's content, it is unlikely that we'll end up with |
| 970 | // a better format. |
| 971 | if (Current.TokenText.find("\\\n") != StringRef::npos) |
| 972 | return 0; |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 973 | // Exempts unterminated string literals from line breaking. The user will |
| 974 | // likely want to terminate the string before any line breaking is done. |
| 975 | if (Current.IsUnterminatedLiteral) |
| 976 | return 0; |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 977 | |
Alexander Kornienko | 16a0ec6 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 978 | Token.reset(new BreakableStringLiteral(Current, StartColumn, |
| 979 | Line.InPPDirective, Encoding)); |
Alexander Kornienko | b4b4a52 | 2013-07-16 23:47:22 +0000 | [diff] [blame] | 980 | } else if (Current.Type == TT_BlockComment && Current.isTrailingComment()) { |
Alexander Kornienko | 16a0ec6 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 981 | Token.reset(new BreakableBlockComment( |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 982 | Style, Current, StartColumn, OriginalStartColumn, !Current.Previous, |
Alexander Kornienko | 16a0ec6 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 983 | Line.InPPDirective, Encoding)); |
Daniel Jasper | 7ff96ed | 2013-05-06 10:24:51 +0000 | [diff] [blame] | 984 | } else if (Current.Type == TT_LineComment && |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 985 | (Current.Previous == NULL || |
| 986 | Current.Previous->Type != TT_ImplicitStringLiteral)) { |
Alexander Kornienko | 10c26b2 | 2013-07-16 21:06:13 +0000 | [diff] [blame] | 987 | // Don't break line comments with escaped newlines. These look like |
| 988 | // separate line comments, but in fact contain a single line comment with |
| 989 | // multiple lines including leading whitespace and the '//' markers. |
| 990 | // |
| 991 | // FIXME: If we want to handle them correctly, we'll need to adjust |
| 992 | // leading whitespace in consecutive lines when changing indentation of |
| 993 | // the first line similar to what we do with block comments. |
| 994 | StringRef::size_type EscapedNewlinePos = Current.TokenText.find("\\\n"); |
| 995 | if (EscapedNewlinePos != StringRef::npos) { |
| 996 | State.Column = |
| 997 | StartColumn + |
| 998 | encoding::getCodePointCount( |
| 999 | Current.TokenText.substr(0, EscapedNewlinePos), Encoding) + |
| 1000 | 1; |
| 1001 | return 0; |
| 1002 | } |
| 1003 | |
Alexander Kornienko | 16a0ec6 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 1004 | Token.reset(new BreakableLineComment(Current, StartColumn, |
| 1005 | Line.InPPDirective, Encoding)); |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 1006 | } else { |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 1007 | return 0; |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 1008 | } |
Alexander Kornienko | 16a0ec6 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 1009 | if (Current.UnbreakableTailLength >= getColumnLimit()) |
Manuel Klimek | 2a9805d | 2013-05-14 09:04:24 +0000 | [diff] [blame] | 1010 | return 0; |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 1011 | |
Alexander Kornienko | c36c5c2 | 2013-06-19 19:50:11 +0000 | [diff] [blame] | 1012 | unsigned RemainingSpace = getColumnLimit() - Current.UnbreakableTailLength; |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 1013 | bool BreakInserted = false; |
| 1014 | unsigned Penalty = 0; |
Alexander Kornienko | c36c5c2 | 2013-06-19 19:50:11 +0000 | [diff] [blame] | 1015 | unsigned RemainingTokenColumns = 0; |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 1016 | for (unsigned LineIndex = 0, EndIndex = Token->getLineCount(); |
| 1017 | LineIndex != EndIndex; ++LineIndex) { |
Alexander Kornienko | 16a0ec6 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 1018 | if (!DryRun) |
| 1019 | Token->replaceWhitespaceBefore(LineIndex, Whitespaces); |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 1020 | unsigned TailOffset = 0; |
Alexander Kornienko | c36c5c2 | 2013-06-19 19:50:11 +0000 | [diff] [blame] | 1021 | RemainingTokenColumns = Token->getLineLengthAfterSplit( |
Alexander Kornienko | 2785b9a | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 1022 | LineIndex, TailOffset, StringRef::npos); |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1023 | while (RemainingTokenColumns > RemainingSpace) { |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 1024 | BreakableToken::Split Split = |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 1025 | Token->getSplit(LineIndex, TailOffset, getColumnLimit()); |
Alexander Kornienko | d446f73 | 2013-07-01 13:42:42 +0000 | [diff] [blame] | 1026 | if (Split.first == StringRef::npos) { |
| 1027 | // The last line's penalty is handled in addNextStateToQueue(). |
| 1028 | if (LineIndex < EndIndex - 1) |
| 1029 | Penalty += Style.PenaltyExcessCharacter * |
| 1030 | (RemainingTokenColumns - RemainingSpace); |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 1031 | break; |
Alexander Kornienko | d446f73 | 2013-07-01 13:42:42 +0000 | [diff] [blame] | 1032 | } |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 1033 | assert(Split.first != 0); |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1034 | unsigned NewRemainingTokenColumns = Token->getLineLengthAfterSplit( |
Alexander Kornienko | 2785b9a | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 1035 | LineIndex, TailOffset + Split.first + Split.second, |
| 1036 | StringRef::npos); |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1037 | assert(NewRemainingTokenColumns < RemainingTokenColumns); |
Alexander Kornienko | 16a0ec6 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 1038 | if (!DryRun) |
| 1039 | Token->insertBreak(LineIndex, TailOffset, Split, Whitespaces); |
Alexander Kornienko | 2785b9a | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 1040 | Penalty += Current.is(tok::string_literal) ? Style.PenaltyBreakString |
| 1041 | : Style.PenaltyBreakComment; |
| 1042 | unsigned ColumnsUsed = |
| 1043 | Token->getLineLengthAfterSplit(LineIndex, TailOffset, Split.first); |
| 1044 | if (ColumnsUsed > getColumnLimit()) { |
| 1045 | Penalty += |
| 1046 | Style.PenaltyExcessCharacter * (ColumnsUsed - getColumnLimit()); |
| 1047 | } |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 1048 | TailOffset += Split.first + Split.second; |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1049 | RemainingTokenColumns = NewRemainingTokenColumns; |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 1050 | BreakInserted = true; |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 1051 | } |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
Alexander Kornienko | c36c5c2 | 2013-06-19 19:50:11 +0000 | [diff] [blame] | 1054 | State.Column = RemainingTokenColumns; |
| 1055 | |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 1056 | if (BreakInserted) { |
Alexander Kornienko | 22d0e29 | 2013-06-17 12:59:44 +0000 | [diff] [blame] | 1057 | // If we break the token inside a parameter list, we need to break before |
| 1058 | // the next parameter on all levels, so that the next parameter is clearly |
| 1059 | // visible. Line comments already introduce a break. |
| 1060 | if (Current.Type != TT_LineComment) { |
| 1061 | for (unsigned i = 0, e = State.Stack.size(); i != e; ++i) |
| 1062 | State.Stack[i].BreakBeforeParameter = true; |
| 1063 | } |
| 1064 | |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 1065 | State.Stack.back().LastSpace = StartColumn; |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 1066 | } |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 1067 | return Penalty; |
| 1068 | } |
| 1069 | |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 1070 | unsigned getColumnLimit() { |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 1071 | // In preprocessor directives reserve two chars for trailing " \" |
| 1072 | return Style.ColumnLimit - (Line.InPPDirective ? 2 : 0); |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 1075 | /// \brief An edge in the solution space from \c Previous->State to \c State, |
| 1076 | /// inserting a newline dependent on the \c NewLine. |
| 1077 | struct StateNode { |
| 1078 | StateNode(const LineState &State, bool NewLine, StateNode *Previous) |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1079 | : State(State), NewLine(NewLine), Previous(Previous) {} |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 1080 | LineState State; |
| 1081 | bool NewLine; |
| 1082 | StateNode *Previous; |
| 1083 | }; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1084 | |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 1085 | /// \brief A pair of <penalty, count> that is used to prioritize the BFS on. |
| 1086 | /// |
| 1087 | /// In case of equal penalties, we want to prefer states that were inserted |
| 1088 | /// first. During state generation we make sure that we insert states first |
| 1089 | /// that break the line as late as possible. |
| 1090 | typedef std::pair<unsigned, unsigned> OrderedPenalty; |
| 1091 | |
| 1092 | /// \brief An item in the prioritized BFS search queue. The \c StateNode's |
| 1093 | /// \c State has the given \c OrderedPenalty. |
| 1094 | typedef std::pair<OrderedPenalty, StateNode *> QueueItem; |
| 1095 | |
| 1096 | /// \brief The BFS queue type. |
| 1097 | typedef std::priority_queue<QueueItem, std::vector<QueueItem>, |
| 1098 | std::greater<QueueItem> > QueueType; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1099 | |
| 1100 | /// \brief Analyze the entire solution space starting from \p InitialState. |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1101 | /// |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1102 | /// This implements a variant of Dijkstra's algorithm on the graph that spans |
| 1103 | /// the solution space (\c LineStates are the nodes). The algorithm tries to |
| 1104 | /// find the shortest path (the one with lowest penalty) from \p InitialState |
| 1105 | /// to a state where all tokens are placed. |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 1106 | void analyzeSolutionSpace(LineState &InitialState) { |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 1107 | std::set<LineState> Seen; |
| 1108 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1109 | // Insert start element into queue. |
Daniel Jasper | fc75908 | 2013-02-14 14:26:07 +0000 | [diff] [blame] | 1110 | StateNode *Node = |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 1111 | new (Allocator.Allocate()) StateNode(InitialState, false, NULL); |
| 1112 | Queue.push(QueueItem(OrderedPenalty(0, Count), Node)); |
| 1113 | ++Count; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1114 | |
| 1115 | // While not empty, take first element and follow edges. |
| 1116 | while (!Queue.empty()) { |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 1117 | unsigned Penalty = Queue.top().first.first; |
Daniel Jasper | fc75908 | 2013-02-14 14:26:07 +0000 | [diff] [blame] | 1118 | StateNode *Node = Queue.top().second; |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 1119 | if (Node->State.NextToken == NULL) { |
Alexander Kornienko | dd25631 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 1120 | DEBUG(llvm::dbgs() << "\n---\nPenalty for line: " << Penalty << "\n"); |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1121 | break; |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 1122 | } |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 1123 | Queue.pop(); |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1124 | |
Daniel Jasper | 54b4e44 | 2013-05-22 05:27:42 +0000 | [diff] [blame] | 1125 | // Cut off the analysis of certain solutions if the analysis gets too |
| 1126 | // complex. See description of IgnoreStackForComparison. |
| 1127 | if (Count > 10000) |
| 1128 | Node->State.IgnoreStackForComparison = true; |
| 1129 | |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 1130 | if (!Seen.insert(Node->State).second) |
| 1131 | // State already examined with lower penalty. |
| 1132 | continue; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1133 | |
Nico Weber | 2726877 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1134 | addNextStateToQueue(Penalty, Node, /*NewLine=*/false); |
| 1135 | addNextStateToQueue(Penalty, Node, /*NewLine=*/true); |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | if (Queue.empty()) |
| 1139 | // We were unable to find a solution, do nothing. |
| 1140 | // FIXME: Add diagnostic? |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 1141 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1142 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1143 | // Reconstruct the solution. |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 1144 | reconstructPath(InitialState, Queue.top().second); |
Alexander Kornienko | dd25631 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 1145 | DEBUG(llvm::dbgs() << "Total number of analyzed states: " << Count << "\n"); |
| 1146 | DEBUG(llvm::dbgs() << "---\n"); |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | void reconstructPath(LineState &State, StateNode *Current) { |
Manuel Klimek | 9c333b9 | 2013-05-29 15:10:11 +0000 | [diff] [blame] | 1150 | std::deque<StateNode *> Path; |
| 1151 | // We do not need a break before the initial token. |
| 1152 | while (Current->Previous) { |
| 1153 | Path.push_front(Current); |
| 1154 | Current = Current->Previous; |
| 1155 | } |
| 1156 | for (std::deque<StateNode *>::iterator I = Path.begin(), E = Path.end(); |
| 1157 | I != E; ++I) { |
| 1158 | DEBUG({ |
| 1159 | if ((*I)->NewLine) { |
| 1160 | llvm::dbgs() << "Penalty for splitting before " |
| 1161 | << (*I)->Previous->State.NextToken->Tok.getName() << ": " |
| 1162 | << (*I)->Previous->State.NextToken->SplitPenalty << "\n"; |
| 1163 | } |
| 1164 | }); |
| 1165 | addTokenToState((*I)->NewLine, false, State); |
| 1166 | } |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
Manuel Klimek | 62a48fb | 2013-02-13 10:54:19 +0000 | [diff] [blame] | 1169 | /// \brief Add the following state to the analysis queue \c Queue. |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1170 | /// |
Manuel Klimek | 62a48fb | 2013-02-13 10:54:19 +0000 | [diff] [blame] | 1171 | /// 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] | 1172 | /// 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] | 1173 | void addNextStateToQueue(unsigned Penalty, StateNode *PreviousNode, |
| 1174 | bool NewLine) { |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 1175 | if (NewLine && !canBreak(PreviousNode->State)) |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1176 | return; |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 1177 | if (!NewLine && mustBreak(PreviousNode->State)) |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1178 | return; |
Daniel Jasper | 88cc562 | 2013-07-08 14:25:23 +0000 | [diff] [blame] | 1179 | if (NewLine) { |
| 1180 | if (!PreviousNode->State.Stack.back().ContainsLineBreak) |
| 1181 | Penalty += 15; |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 1182 | Penalty += PreviousNode->State.NextToken->SplitPenalty; |
Daniel Jasper | 88cc562 | 2013-07-08 14:25:23 +0000 | [diff] [blame] | 1183 | } |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 1184 | |
| 1185 | StateNode *Node = new (Allocator.Allocate()) |
| 1186 | StateNode(PreviousNode->State, NewLine, PreviousNode); |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 1187 | Penalty += addTokenToState(NewLine, true, Node->State); |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 1188 | if (Node->State.Column > getColumnLimit()) { |
| 1189 | unsigned ExcessCharacters = Node->State.Column - getColumnLimit(); |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 1190 | Penalty += Style.PenaltyExcessCharacter * ExcessCharacters; |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 1191 | } |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 1192 | |
| 1193 | Queue.push(QueueItem(OrderedPenalty(Penalty, Count), Node)); |
| 1194 | ++Count; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1195 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1196 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1197 | /// \brief Returns \c true, if a line break after \p State is allowed. |
| 1198 | bool canBreak(const LineState &State) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1199 | const FormatToken &Current = *State.NextToken; |
| 1200 | const FormatToken &Previous = *Current.Previous; |
| 1201 | assert(&Previous == Current.Previous); |
Daniel Jasper | 399914b | 2013-05-17 09:35:01 +0000 | [diff] [blame] | 1202 | if (!Current.CanBreakBefore && |
| 1203 | !(Current.is(tok::r_brace) && |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1204 | State.Stack.back().BreakBeforeClosingBrace)) |
| 1205 | return false; |
Daniel Jasper | 399914b | 2013-05-17 09:35:01 +0000 | [diff] [blame] | 1206 | // The opening "{" of a braced list has to be on the same line as the first |
| 1207 | // element if it is nested in another braced init list or function call. |
| 1208 | if (!Current.MustBreakBefore && Previous.is(tok::l_brace) && |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1209 | Previous.Previous && |
| 1210 | Previous.Previous->isOneOf(tok::l_brace, tok::l_paren, tok::comma)) |
Daniel Jasper | 399914b | 2013-05-17 09:35:01 +0000 | [diff] [blame] | 1211 | return false; |
Daniel Jasper | 259a038 | 2013-05-27 11:50:16 +0000 | [diff] [blame] | 1212 | // This prevents breaks like: |
| 1213 | // ... |
| 1214 | // SomeParameter, OtherParameter).DoSomething( |
| 1215 | // ... |
| 1216 | // As they hide "DoSomething" and are generally bad for readability. |
Daniel Jasper | 07ca547 | 2013-07-05 09:14:35 +0000 | [diff] [blame] | 1217 | if (Previous.opensScope() && |
| 1218 | State.LowestLevelOnLine < State.StartOfLineLevel) |
Daniel Jasper | 259a038 | 2013-05-27 11:50:16 +0000 | [diff] [blame] | 1219 | return false; |
Daniel Jasper | 001bf4e | 2013-04-22 07:59:53 +0000 | [diff] [blame] | 1220 | return !State.Stack.back().NoLineBreak; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1221 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1222 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1223 | /// \brief Returns \c true, if a line break after \p State is mandatory. |
| 1224 | bool mustBreak(const LineState &State) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1225 | const FormatToken &Current = *State.NextToken; |
| 1226 | const FormatToken &Previous = *Current.Previous; |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 1227 | if (Current.MustBreakBefore || Current.Type == TT_InlineASMColon) |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1228 | return true; |
Daniel Jasper | b5dc3f4 | 2013-07-16 18:22:10 +0000 | [diff] [blame] | 1229 | if (!Style.Cpp11BracedListStyle && Current.is(tok::r_brace) && |
| 1230 | State.Stack.back().BreakBeforeClosingBrace) |
| 1231 | return true; |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 1232 | if (Previous.is(tok::semi) && State.LineContainsContinuedForLoopSection) |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1233 | return true; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 1234 | if (Style.BreakConstructorInitializersBeforeComma) { |
| 1235 | if (Previous.Type == TT_CtorInitializerComma) |
| 1236 | return false; |
| 1237 | if (Current.Type == TT_CtorInitializerComma) |
| 1238 | return true; |
| 1239 | } |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 1240 | if ((Previous.isOneOf(tok::comma, tok::semi) || Current.is(tok::question) || |
Daniel Jasper | ab3ce59 | 2013-08-01 22:05:00 +0000 | [diff] [blame] | 1241 | (Current.Type == TT_ConditionalExpr && |
| 1242 | !(Current.is(tok::colon) && Previous.is(tok::question)))) && |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 1243 | State.Stack.back().BreakBeforeParameter && |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 1244 | !Current.isTrailingComment() && |
| 1245 | !Current.isOneOf(tok::r_paren, tok::r_brace)) |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1246 | return true; |
Daniel Jasper | 215c57f | 2013-07-17 15:38:19 +0000 | [diff] [blame] | 1247 | if (Style.AlwaysBreakBeforeMultilineStrings && |
| 1248 | State.Column > State.Stack.back().Indent && |
| 1249 | Current.is(tok::string_literal) && Previous.isNot(tok::lessless) && |
| 1250 | Previous.Type != TT_InlineASMColon && |
| 1251 | ((Current.getNextNonComment() && |
| 1252 | Current.getNextNonComment()->is(tok::string_literal)) || |
| 1253 | (Current.TokenText.find("\\\n") != StringRef::npos))) |
| 1254 | return true; |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 1255 | |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 1256 | if (!Style.BreakBeforeBinaryOperators) { |
| 1257 | // If we need to break somewhere inside the LHS of a binary expression, we |
| 1258 | // should also break after the operator. Otherwise, the formatting would |
| 1259 | // hide the operator precedence, e.g. in: |
| 1260 | // if (aaaaaaaaaaaaaa == |
| 1261 | // bbbbbbbbbbbbbb && c) {.. |
| 1262 | // For comparisons, we only apply this rule, if the LHS is a binary |
| 1263 | // expression itself as otherwise, the line breaks seem superfluous. |
| 1264 | // We need special cases for ">>" which we have split into two ">" while |
| 1265 | // lexing in order to make template parsing easier. |
| 1266 | // |
| 1267 | // FIXME: We'll need something similar for styles that break before binary |
| 1268 | // operators. |
| 1269 | bool IsComparison = (Previous.getPrecedence() == prec::Relational || |
| 1270 | Previous.getPrecedence() == prec::Equality) && |
| 1271 | Previous.Previous && Previous.Previous->Type != |
| 1272 | TT_BinaryOperator; // For >>. |
| 1273 | bool LHSIsBinaryExpr = |
| 1274 | Previous.Previous && Previous.Previous->FakeRParens > 0; |
| 1275 | if (Previous.Type == TT_BinaryOperator && |
| 1276 | (!IsComparison || LHSIsBinaryExpr) && |
| 1277 | Current.Type != TT_BinaryOperator && // For >>. |
| 1278 | !Current.isTrailingComment() && |
| 1279 | !Previous.isOneOf(tok::lessless, tok::question) && |
| 1280 | Previous.getPrecedence() != prec::Assignment && |
| 1281 | State.Stack.back().BreakBeforeParameter) |
| 1282 | return true; |
| 1283 | } |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 1284 | |
Daniel Jasper | a0740f5 | 2013-07-12 15:14:05 +0000 | [diff] [blame] | 1285 | // Same as above, but for the first "<<" operator. |
| 1286 | if (Current.is(tok::lessless) && State.Stack.back().BreakBeforeParameter && |
| 1287 | State.Stack.back().FirstLessLess == 0) |
| 1288 | return true; |
| 1289 | |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 1290 | // FIXME: Comparing LongestObjCSelectorName to 0 is a hacky way of finding |
| 1291 | // out whether it is the first parameter. Clean this up. |
| 1292 | if (Current.Type == TT_ObjCSelectorName && |
| 1293 | Current.LongestObjCSelectorName == 0 && |
| 1294 | State.Stack.back().BreakBeforeParameter) |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1295 | return true; |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 1296 | if ((Current.Type == TT_CtorInitializerColon || |
| 1297 | (Previous.ClosesTemplateDeclaration && State.ParenLevel == 0))) |
Daniel Jasper | 923ebef | 2013-03-14 13:45:21 +0000 | [diff] [blame] | 1298 | return true; |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 1299 | |
Daniel Jasper | 6561f6a | 2013-07-09 07:43:55 +0000 | [diff] [blame] | 1300 | if ((Current.Type == TT_StartOfName || Current.is(tok::kw_operator)) && |
| 1301 | Line.MightBeFunctionDecl && State.Stack.back().BreakBeforeParameter && |
| 1302 | State.ParenLevel == 0) |
Daniel Jasper | 33f4b90 | 2013-05-15 09:35:08 +0000 | [diff] [blame] | 1303 | return true; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1304 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
Daniel Jasper | 3af59ce | 2013-03-15 14:57:30 +0000 | [diff] [blame] | 1307 | // Returns the total number of columns required for the remaining tokens. |
| 1308 | unsigned getRemainingLength(const LineState &State) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1309 | if (State.NextToken && State.NextToken->Previous) |
| 1310 | return Line.Last->TotalLength - State.NextToken->Previous->TotalLength; |
Daniel Jasper | 3af59ce | 2013-03-15 14:57:30 +0000 | [diff] [blame] | 1311 | return 0; |
| 1312 | } |
| 1313 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1314 | FormatStyle Style; |
| 1315 | SourceManager &SourceMgr; |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1316 | const AnnotatedLine &Line; |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1317 | const unsigned FirstIndent; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1318 | const FormatToken *RootToken; |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 1319 | WhitespaceManager &Whitespaces; |
Manuel Klimek | 62a48fb | 2013-02-13 10:54:19 +0000 | [diff] [blame] | 1320 | |
| 1321 | llvm::SpecificBumpPtrAllocator<StateNode> Allocator; |
| 1322 | QueueType Queue; |
| 1323 | // Increasing count of \c StateNode items we have created. This is used |
| 1324 | // to create a deterministic order independent of the container. |
| 1325 | unsigned Count; |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1326 | encoding::Encoding Encoding; |
Daniel Jasper | c7bd68f | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 1327 | bool BinPackInconclusiveFunctions; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1328 | }; |
| 1329 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1330 | class FormatTokenLexer { |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1331 | public: |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1332 | FormatTokenLexer(Lexer &Lex, SourceManager &SourceMgr, |
| 1333 | encoding::Encoding Encoding) |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1334 | : FormatTok(NULL), GreaterStashed(false), TrailingWhitespace(0), Lex(Lex), |
Daniel Jasper | b7000ca | 2013-08-01 17:58:23 +0000 | [diff] [blame] | 1335 | SourceMgr(SourceMgr), IdentTable(getFormattingLangOpts()), |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1336 | Encoding(Encoding) { |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1337 | Lex.SetKeepWhitespaceMode(true); |
| 1338 | } |
| 1339 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1340 | ArrayRef<FormatToken *> lex() { |
| 1341 | assert(Tokens.empty()); |
| 1342 | do { |
| 1343 | Tokens.push_back(getNextToken()); |
| 1344 | } while (Tokens.back()->Tok.isNot(tok::eof)); |
| 1345 | return Tokens; |
| 1346 | } |
| 1347 | |
| 1348 | IdentifierTable &getIdentTable() { return IdentTable; } |
| 1349 | |
| 1350 | private: |
| 1351 | FormatToken *getNextToken() { |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1352 | if (GreaterStashed) { |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 1353 | // Create a synthesized second '>' token. |
| 1354 | Token Greater = FormatTok->Tok; |
| 1355 | FormatTok = new (Allocator.Allocate()) FormatToken; |
| 1356 | FormatTok->Tok = Greater; |
Manuel Klimek | ad3094b | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 1357 | SourceLocation GreaterLocation = |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1358 | FormatTok->Tok.getLocation().getLocWithOffset(1); |
| 1359 | FormatTok->WhitespaceRange = |
| 1360 | SourceRange(GreaterLocation, GreaterLocation); |
Alexander Kornienko | 54e6c9d | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 1361 | FormatTok->TokenText = ">"; |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1362 | FormatTok->CodePointCount = 1; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1363 | GreaterStashed = false; |
| 1364 | return FormatTok; |
| 1365 | } |
| 1366 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1367 | FormatTok = new (Allocator.Allocate()) FormatToken; |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 1368 | readRawToken(*FormatTok); |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 1369 | SourceLocation WhitespaceStart = |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1370 | FormatTok->Tok.getLocation().getLocWithOffset(-TrailingWhitespace); |
Manuel Klimek | ad3094b | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 1371 | if (SourceMgr.getFileOffset(WhitespaceStart) == 0) |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1372 | FormatTok->IsFirst = true; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1373 | |
| 1374 | // Consume and record whitespace until we find a significant token. |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 1375 | unsigned WhitespaceLength = TrailingWhitespace; |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1376 | while (FormatTok->Tok.is(tok::unknown)) { |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 1377 | unsigned Newlines = FormatTok->TokenText.count('\n'); |
Daniel Jasper | 1eee6c4 | 2013-03-04 13:43:19 +0000 | [diff] [blame] | 1378 | if (Newlines > 0) |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 1379 | FormatTok->LastNewlineOffset = |
| 1380 | WhitespaceLength + FormatTok->TokenText.rfind('\n') + 1; |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1381 | FormatTok->NewlinesBefore += Newlines; |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 1382 | unsigned EscapedNewlines = FormatTok->TokenText.count("\\\n"); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1383 | FormatTok->HasUnescapedNewline |= EscapedNewlines != Newlines; |
| 1384 | WhitespaceLength += FormatTok->Tok.getLength(); |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1385 | |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 1386 | readRawToken(*FormatTok); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1387 | } |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 1388 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1389 | // In case the token starts with escaped newlines, we want to |
| 1390 | // take them into account as whitespace - this pattern is quite frequent |
| 1391 | // in macro definitions. |
| 1392 | // FIXME: What do we want to do with other escaped spaces, and escaped |
| 1393 | // spaces or newlines in the middle of tokens? |
| 1394 | // FIXME: Add a more explicit test. |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 1395 | while (FormatTok->TokenText.size() > 1 && FormatTok->TokenText[0] == '\\' && |
| 1396 | FormatTok->TokenText[1] == '\n') { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1397 | // FIXME: ++FormatTok->NewlinesBefore is missing... |
Manuel Klimek | ad3094b | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 1398 | WhitespaceLength += 2; |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 1399 | FormatTok->TokenText = FormatTok->TokenText.substr(2); |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
Alexander Kornienko | 54e6c9d | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 1402 | TrailingWhitespace = 0; |
| 1403 | if (FormatTok->Tok.is(tok::comment)) { |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 1404 | StringRef UntrimmedText = FormatTok->TokenText; |
| 1405 | FormatTok->TokenText = FormatTok->TokenText.rtrim(); |
| 1406 | TrailingWhitespace = UntrimmedText.size() - FormatTok->TokenText.size(); |
Alexander Kornienko | 54e6c9d | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 1407 | } else if (FormatTok->Tok.is(tok::raw_identifier)) { |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 1408 | IdentifierInfo &Info = IdentTable.get(FormatTok->TokenText); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1409 | FormatTok->Tok.setIdentifierInfo(&Info); |
| 1410 | FormatTok->Tok.setKind(Info.getTokenID()); |
Alexander Kornienko | 54e6c9d | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 1411 | } else if (FormatTok->Tok.is(tok::greatergreater)) { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1412 | FormatTok->Tok.setKind(tok::greater); |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 1413 | FormatTok->TokenText = FormatTok->TokenText.substr(0, 1); |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1414 | GreaterStashed = true; |
| 1415 | } |
| 1416 | |
Alexander Kornienko | 54e6c9d | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 1417 | // Now FormatTok is the next non-whitespace token. |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 1418 | FormatTok->CodePointCount = |
| 1419 | encoding::getCodePointCount(FormatTok->TokenText, Encoding); |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1420 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1421 | FormatTok->WhitespaceRange = SourceRange( |
Manuel Klimek | ad3094b | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 1422 | WhitespaceStart, WhitespaceStart.getLocWithOffset(WhitespaceLength)); |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1423 | return FormatTok; |
| 1424 | } |
| 1425 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1426 | FormatToken *FormatTok; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1427 | bool GreaterStashed; |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 1428 | unsigned TrailingWhitespace; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1429 | Lexer &Lex; |
| 1430 | SourceManager &SourceMgr; |
| 1431 | IdentifierTable IdentTable; |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1432 | encoding::Encoding Encoding; |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1433 | llvm::SpecificBumpPtrAllocator<FormatToken> Allocator; |
| 1434 | SmallVector<FormatToken *, 16> Tokens; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1435 | |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 1436 | void readRawToken(FormatToken &Tok) { |
| 1437 | Lex.LexFromRawLexer(Tok.Tok); |
| 1438 | Tok.TokenText = StringRef(SourceMgr.getCharacterData(Tok.Tok.getLocation()), |
| 1439 | Tok.Tok.getLength()); |
| 1440 | |
| 1441 | // For formatting, treat unterminated string literals like normal string |
| 1442 | // literals. |
| 1443 | if (Tok.is(tok::unknown) && !Tok.TokenText.empty() && |
| 1444 | Tok.TokenText[0] == '"') { |
| 1445 | Tok.Tok.setKind(tok::string_literal); |
| 1446 | Tok.IsUnterminatedLiteral = true; |
| 1447 | } |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1448 | } |
| 1449 | }; |
| 1450 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1451 | class Formatter : public UnwrappedLineConsumer { |
| 1452 | public: |
Daniel Jasper | caf42a3 | 2013-05-15 08:14:19 +0000 | [diff] [blame] | 1453 | Formatter(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr, |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1454 | const std::vector<CharSourceRange> &Ranges) |
Daniel Jasper | caf42a3 | 2013-05-15 08:14:19 +0000 | [diff] [blame] | 1455 | : Style(Style), Lex(Lex), SourceMgr(SourceMgr), |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1456 | Whitespaces(SourceMgr, Style), Ranges(Ranges), |
| 1457 | Encoding(encoding::detectEncoding(Lex.getBuffer())) { |
Daniel Jasper | 9637dda | 2013-07-15 14:33:14 +0000 | [diff] [blame] | 1458 | DEBUG(llvm::dbgs() << "File encoding: " |
| 1459 | << (Encoding == encoding::Encoding_UTF8 ? "UTF8" |
| 1460 | : "unknown") |
| 1461 | << "\n"); |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1462 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1463 | |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 1464 | virtual ~Formatter() {} |
Daniel Jasper | accb0b0 | 2012-12-04 21:05:31 +0000 | [diff] [blame] | 1465 | |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1466 | tooling::Replacements format() { |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1467 | FormatTokenLexer Tokens(Lex, SourceMgr, Encoding); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1468 | |
| 1469 | UnwrappedLineParser Parser(Style, Tokens.lex(), *this); |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 1470 | bool StructuralError = Parser.parse(); |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1471 | TokenAnnotator Annotator(Style, Tokens.getIdentTable().get("in")); |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1472 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
| 1473 | Annotator.annotate(AnnotatedLines[i]); |
| 1474 | } |
| 1475 | deriveLocalStyle(); |
| 1476 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
| 1477 | Annotator.calculateFormattingInformation(AnnotatedLines[i]); |
| 1478 | } |
Daniel Jasper | 5999f76 | 2013-04-09 17:46:55 +0000 | [diff] [blame] | 1479 | |
| 1480 | // Adapt level to the next line if this is a comment. |
| 1481 | // FIXME: Can/should this be done in the UnwrappedLineParser? |
Alexander Kornienko | 0bdc643 | 2013-07-04 14:47:51 +0000 | [diff] [blame] | 1482 | const AnnotatedLine *NextNonCommentLine = NULL; |
Daniel Jasper | 5999f76 | 2013-04-09 17:46:55 +0000 | [diff] [blame] | 1483 | for (unsigned i = AnnotatedLines.size() - 1; i > 0; --i) { |
Alexander Kornienko | 0bdc643 | 2013-07-04 14:47:51 +0000 | [diff] [blame] | 1484 | if (NextNonCommentLine && AnnotatedLines[i].First->is(tok::comment) && |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1485 | !AnnotatedLines[i].First->Next) |
Alexander Kornienko | 0bdc643 | 2013-07-04 14:47:51 +0000 | [diff] [blame] | 1486 | AnnotatedLines[i].Level = NextNonCommentLine->Level; |
Daniel Jasper | 5999f76 | 2013-04-09 17:46:55 +0000 | [diff] [blame] | 1487 | else |
Daniel Jasper | 2a409b6 | 2013-07-08 14:34:09 +0000 | [diff] [blame] | 1488 | NextNonCommentLine = AnnotatedLines[i].First->isNot(tok::r_brace) |
| 1489 | ? &AnnotatedLines[i] |
| 1490 | : NULL; |
Daniel Jasper | 5999f76 | 2013-04-09 17:46:55 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1493 | std::vector<int> IndentForLevel; |
| 1494 | bool PreviousLineWasTouched = false; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1495 | const FormatToken *PreviousLineLastToken = 0; |
Daniel Jasper | 89b3a7f | 2013-05-10 13:00:49 +0000 | [diff] [blame] | 1496 | bool FormatPPDirective = false; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1497 | for (std::vector<AnnotatedLine>::iterator I = AnnotatedLines.begin(), |
| 1498 | E = AnnotatedLines.end(); |
| 1499 | I != E; ++I) { |
| 1500 | const AnnotatedLine &TheLine = *I; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1501 | const FormatToken *FirstTok = TheLine.First; |
| 1502 | int Offset = getIndentOffset(*TheLine.First); |
Daniel Jasper | 89b3a7f | 2013-05-10 13:00:49 +0000 | [diff] [blame] | 1503 | |
| 1504 | // Check whether this line is part of a formatted preprocessor directive. |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 1505 | if (FirstTok->HasUnescapedNewline) |
Daniel Jasper | 89b3a7f | 2013-05-10 13:00:49 +0000 | [diff] [blame] | 1506 | FormatPPDirective = false; |
| 1507 | if (!FormatPPDirective && TheLine.InPPDirective && |
| 1508 | (touchesLine(TheLine) || touchesPPDirective(I + 1, E))) |
| 1509 | FormatPPDirective = true; |
| 1510 | |
Daniel Jasper | 1fb8d88 | 2013-05-14 09:30:02 +0000 | [diff] [blame] | 1511 | // Determine indent and try to merge multiple unwrapped lines. |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1512 | while (IndentForLevel.size() <= TheLine.Level) |
| 1513 | IndentForLevel.push_back(-1); |
| 1514 | IndentForLevel.resize(TheLine.Level + 1); |
Daniel Jasper | 1fb8d88 | 2013-05-14 09:30:02 +0000 | [diff] [blame] | 1515 | unsigned Indent = getIndent(IndentForLevel, TheLine.Level); |
| 1516 | if (static_cast<int>(Indent) + Offset >= 0) |
| 1517 | Indent += Offset; |
| 1518 | tryFitMultipleLinesInOne(Indent, I, E); |
| 1519 | |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 1520 | bool WasMoved = PreviousLineWasTouched && FirstTok->NewlinesBefore == 0; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1521 | if (TheLine.First->is(tok::eof)) { |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1522 | if (PreviousLineWasTouched) { |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 1523 | unsigned NewLines = std::min(FirstTok->NewlinesBefore, 1u); |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1524 | Whitespaces.replaceWhitespace(*TheLine.First, NewLines, /*Indent*/ 0, |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 1525 | /*TargetColumn*/ 0); |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1526 | } |
| 1527 | } else if (TheLine.Type != LT_Invalid && |
Daniel Jasper | 89b3a7f | 2013-05-10 13:00:49 +0000 | [diff] [blame] | 1528 | (WasMoved || FormatPPDirective || touchesLine(TheLine))) { |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1529 | unsigned LevelIndent = getIndent(IndentForLevel, TheLine.Level); |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 1530 | if (FirstTok->WhitespaceRange.isValid() && |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 1531 | // Insert a break even if there is a structural error in case where |
| 1532 | // we break apart a line consisting of multiple unwrapped lines. |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 1533 | (FirstTok->NewlinesBefore == 0 || !StructuralError)) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1534 | formatFirstToken(*TheLine.First, PreviousLineLastToken, Indent, |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 1535 | TheLine.InPPDirective); |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 1536 | } else { |
| 1537 | Indent = LevelIndent = |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 1538 | SourceMgr.getSpellingColumnNumber(FirstTok->Tok.getLocation()) - |
| 1539 | 1; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1540 | } |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1541 | UnwrappedLineFormatter Formatter(Style, SourceMgr, TheLine, Indent, |
Daniel Jasper | c7bd68f | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 1542 | TheLine.First, Whitespaces, Encoding, |
| 1543 | BinPackInconclusiveFunctions); |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 1544 | Formatter.format(I + 1 != E ? &*(I + 1) : NULL); |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1545 | IndentForLevel[TheLine.Level] = LevelIndent; |
| 1546 | PreviousLineWasTouched = true; |
| 1547 | } else { |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 1548 | // Format the first token if necessary, and notify the WhitespaceManager |
| 1549 | // about the unchanged whitespace. |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1550 | for (const FormatToken *Tok = TheLine.First; Tok != NULL; |
| 1551 | Tok = Tok->Next) { |
| 1552 | if (Tok == TheLine.First && |
| 1553 | (Tok->NewlinesBefore > 0 || Tok->IsFirst)) { |
| 1554 | unsigned LevelIndent = |
| 1555 | SourceMgr.getSpellingColumnNumber(Tok->Tok.getLocation()) - 1; |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 1556 | // Remove trailing whitespace of the previous line if it was |
| 1557 | // touched. |
| 1558 | if (PreviousLineWasTouched || touchesEmptyLineBefore(TheLine)) { |
| 1559 | formatFirstToken(*Tok, PreviousLineLastToken, LevelIndent, |
| 1560 | TheLine.InPPDirective); |
| 1561 | } else { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1562 | Whitespaces.addUntouchableToken(*Tok, TheLine.InPPDirective); |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 1563 | } |
Daniel Jasper | 1fb8d88 | 2013-05-14 09:30:02 +0000 | [diff] [blame] | 1564 | |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 1565 | if (static_cast<int>(LevelIndent) - Offset >= 0) |
| 1566 | LevelIndent -= Offset; |
| 1567 | if (Tok->isNot(tok::comment)) |
| 1568 | IndentForLevel[TheLine.Level] = LevelIndent; |
| 1569 | } else { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1570 | Whitespaces.addUntouchableToken(*Tok, TheLine.InPPDirective); |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 1571 | } |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1572 | } |
| 1573 | // If we did not reformat this unwrapped line, the column at the end of |
| 1574 | // the last token is unchanged - thus, we can calculate the end of the |
| 1575 | // last token. |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1576 | PreviousLineWasTouched = false; |
| 1577 | } |
Alexander Kornienko | 94b748f | 2013-03-27 17:08:02 +0000 | [diff] [blame] | 1578 | PreviousLineLastToken = I->Last; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1579 | } |
| 1580 | return Whitespaces.generateReplacements(); |
| 1581 | } |
| 1582 | |
| 1583 | private: |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1584 | void deriveLocalStyle() { |
| 1585 | unsigned CountBoundToVariable = 0; |
| 1586 | unsigned CountBoundToType = 0; |
| 1587 | bool HasCpp03IncompatibleFormat = false; |
Daniel Jasper | c7bd68f | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 1588 | bool HasBinPackedFunction = false; |
| 1589 | bool HasOnePerLineFunction = false; |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1590 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1591 | if (!AnnotatedLines[i].First->Next) |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1592 | continue; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1593 | FormatToken *Tok = AnnotatedLines[i].First->Next; |
| 1594 | while (Tok->Next) { |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1595 | if (Tok->Type == TT_PointerOrReference) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1596 | bool SpacesBefore = |
| 1597 | Tok->WhitespaceRange.getBegin() != Tok->WhitespaceRange.getEnd(); |
| 1598 | bool SpacesAfter = Tok->Next->WhitespaceRange.getBegin() != |
| 1599 | Tok->Next->WhitespaceRange.getEnd(); |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1600 | if (SpacesBefore && !SpacesAfter) |
| 1601 | ++CountBoundToVariable; |
| 1602 | else if (!SpacesBefore && SpacesAfter) |
| 1603 | ++CountBoundToType; |
| 1604 | } |
| 1605 | |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 1606 | if (Tok->Type == TT_TemplateCloser && |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1607 | Tok->Previous->Type == TT_TemplateCloser && |
| 1608 | Tok->WhitespaceRange.getBegin() == Tok->WhitespaceRange.getEnd()) |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1609 | HasCpp03IncompatibleFormat = true; |
Daniel Jasper | c7bd68f | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 1610 | |
| 1611 | if (Tok->PackingKind == PPK_BinPacked) |
| 1612 | HasBinPackedFunction = true; |
| 1613 | if (Tok->PackingKind == PPK_OnePerLine) |
| 1614 | HasOnePerLineFunction = true; |
| 1615 | |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1616 | Tok = Tok->Next; |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1617 | } |
| 1618 | } |
| 1619 | if (Style.DerivePointerBinding) { |
| 1620 | if (CountBoundToType > CountBoundToVariable) |
| 1621 | Style.PointerBindsToType = true; |
| 1622 | else if (CountBoundToType < CountBoundToVariable) |
| 1623 | Style.PointerBindsToType = false; |
| 1624 | } |
| 1625 | if (Style.Standard == FormatStyle::LS_Auto) { |
| 1626 | Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11 |
| 1627 | : FormatStyle::LS_Cpp03; |
| 1628 | } |
Daniel Jasper | c7bd68f | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 1629 | BinPackInconclusiveFunctions = |
| 1630 | HasBinPackedFunction || !HasOnePerLineFunction; |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 1633 | /// \brief Get the indent of \p Level from \p IndentForLevel. |
| 1634 | /// |
| 1635 | /// \p IndentForLevel must contain the indent for the level \c l |
| 1636 | /// at \p IndentForLevel[l], or a value < 0 if the indent for |
| 1637 | /// that level is unknown. |
Daniel Jasper | fc75908 | 2013-02-14 14:26:07 +0000 | [diff] [blame] | 1638 | unsigned getIndent(const std::vector<int> IndentForLevel, unsigned Level) { |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 1639 | if (IndentForLevel[Level] != -1) |
| 1640 | return IndentForLevel[Level]; |
Manuel Klimek | 52635ff | 2013-02-08 19:53:32 +0000 | [diff] [blame] | 1641 | if (Level == 0) |
| 1642 | return 0; |
Manuel Klimek | 07a64ec | 2013-05-13 08:42:42 +0000 | [diff] [blame] | 1643 | return getIndent(IndentForLevel, Level - 1) + Style.IndentWidth; |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | /// \brief Get the offset of the line relatively to the level. |
| 1647 | /// |
| 1648 | /// For example, 'public:' labels in classes are offset by 1 or 2 |
| 1649 | /// characters to the left from their level. |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1650 | int getIndentOffset(const FormatToken &RootToken) { |
Alexander Kornienko | 94b748f | 2013-03-27 17:08:02 +0000 | [diff] [blame] | 1651 | if (RootToken.isAccessSpecifier(false) || RootToken.isObjCAccessSpecifier()) |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 1652 | return Style.AccessModifierOffset; |
| 1653 | return 0; |
| 1654 | } |
| 1655 | |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1656 | /// \brief Tries to merge lines into one. |
| 1657 | /// |
| 1658 | /// This will change \c Line and \c AnnotatedLine to contain the merged line, |
| 1659 | /// 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] | 1660 | void tryFitMultipleLinesInOne(unsigned Indent, |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1661 | std::vector<AnnotatedLine>::iterator &I, |
| 1662 | std::vector<AnnotatedLine>::iterator E) { |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1663 | // We can never merge stuff if there are trailing line comments. |
| 1664 | if (I->Last->Type == TT_LineComment) |
| 1665 | return; |
| 1666 | |
Daniel Jasper | e05dc6d | 2013-07-24 13:10:59 +0000 | [diff] [blame] | 1667 | if (Indent > Style.ColumnLimit) |
| 1668 | return; |
| 1669 | |
Daniel Jasper | a4d4621 | 2013-02-28 11:05:57 +0000 | [diff] [blame] | 1670 | unsigned Limit = Style.ColumnLimit - Indent; |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1671 | // If we already exceed the column limit, we set 'Limit' to 0. The different |
| 1672 | // tryMerge..() functions can then decide whether to still do merging. |
| 1673 | Limit = I->Last->TotalLength > Limit ? 0 : Limit - I->Last->TotalLength; |
Daniel Jasper | 55b08e7 | 2013-01-16 07:02:34 +0000 | [diff] [blame] | 1674 | |
Daniel Jasper | 9c8c40e | 2013-01-21 14:18:28 +0000 | [diff] [blame] | 1675 | if (I + 1 == E || (I + 1)->Type == LT_Invalid) |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1676 | return; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1677 | |
Daniel Jasper | 5be59ba | 2013-05-15 14:09:55 +0000 | [diff] [blame] | 1678 | if (I->Last->is(tok::l_brace)) { |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1679 | tryMergeSimpleBlock(I, E, Limit); |
Daniel Jasper | f11bbb9 | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 1680 | } else if (Style.AllowShortIfStatementsOnASingleLine && |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1681 | I->First->is(tok::kw_if)) { |
Daniel Jasper | f11bbb9 | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 1682 | tryMergeSimpleControlStatement(I, E, Limit); |
| 1683 | } else if (Style.AllowShortLoopsOnASingleLine && |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1684 | I->First->isOneOf(tok::kw_for, tok::kw_while)) { |
Daniel Jasper | f11bbb9 | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 1685 | tryMergeSimpleControlStatement(I, E, Limit); |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1686 | } else if (I->InPPDirective && |
| 1687 | (I->First->HasUnescapedNewline || I->First->IsFirst)) { |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1688 | tryMergeSimplePPDirective(I, E, Limit); |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1689 | } |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1692 | void tryMergeSimplePPDirective(std::vector<AnnotatedLine>::iterator &I, |
| 1693 | std::vector<AnnotatedLine>::iterator E, |
| 1694 | unsigned Limit) { |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1695 | if (Limit == 0) |
| 1696 | return; |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1697 | AnnotatedLine &Line = *I; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1698 | if (!(I + 1)->InPPDirective || (I + 1)->First->HasUnescapedNewline) |
Daniel Jasper | 2b9c10b | 2013-01-14 15:52:06 +0000 | [diff] [blame] | 1699 | return; |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1700 | if (I + 2 != E && (I + 2)->InPPDirective && |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1701 | !(I + 2)->First->HasUnescapedNewline) |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1702 | return; |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1703 | if (1 + (I + 1)->Last->TotalLength > Limit) |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1704 | return; |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1705 | join(Line, *(++I)); |
| 1706 | } |
| 1707 | |
Daniel Jasper | f11bbb9 | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 1708 | void tryMergeSimpleControlStatement(std::vector<AnnotatedLine>::iterator &I, |
| 1709 | std::vector<AnnotatedLine>::iterator E, |
| 1710 | unsigned Limit) { |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1711 | if (Limit == 0) |
| 1712 | return; |
Manuel Klimek | d3a247c | 2013-08-07 19:20:45 +0000 | [diff] [blame^] | 1713 | if (Style.BreakBeforeBraces == FormatStyle::BS_Allman && |
| 1714 | (I + 1)->First->is(tok::l_brace)) |
| 1715 | return; |
Manuel Klimek | 4c12812 | 2013-01-18 14:46:43 +0000 | [diff] [blame] | 1716 | if ((I + 1)->InPPDirective != I->InPPDirective || |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1717 | ((I + 1)->InPPDirective && (I + 1)->First->HasUnescapedNewline)) |
Manuel Klimek | 4c12812 | 2013-01-18 14:46:43 +0000 | [diff] [blame] | 1718 | return; |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1719 | AnnotatedLine &Line = *I; |
Daniel Jasper | 55b08e7 | 2013-01-16 07:02:34 +0000 | [diff] [blame] | 1720 | if (Line.Last->isNot(tok::r_paren)) |
| 1721 | return; |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1722 | if (1 + (I + 1)->Last->TotalLength > Limit) |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1723 | return; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1724 | if ((I + 1)->First->isOneOf(tok::semi, tok::kw_if, tok::kw_for, |
| 1725 | tok::kw_while) || |
| 1726 | (I + 1)->First->Type == TT_LineComment) |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1727 | return; |
| 1728 | // Only inline simple if's (no nested if or else). |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1729 | if (I + 2 != E && Line.First->is(tok::kw_if) && |
| 1730 | (I + 2)->First->is(tok::kw_else)) |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1731 | return; |
| 1732 | join(Line, *(++I)); |
| 1733 | } |
| 1734 | |
| 1735 | void tryMergeSimpleBlock(std::vector<AnnotatedLine>::iterator &I, |
Daniel Jasper | 1a1ce83 | 2013-01-29 11:27:30 +0000 | [diff] [blame] | 1736 | std::vector<AnnotatedLine>::iterator E, |
| 1737 | unsigned Limit) { |
Daniel Jasper | 5be59ba | 2013-05-15 14:09:55 +0000 | [diff] [blame] | 1738 | // No merging if the brace already is on the next line. |
| 1739 | if (Style.BreakBeforeBraces != FormatStyle::BS_Attach) |
| 1740 | return; |
| 1741 | |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1742 | // First, check that the current line allows merging. This is the case if |
| 1743 | // we're not in a control flow statement and the last token is an opening |
| 1744 | // brace. |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1745 | AnnotatedLine &Line = *I; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1746 | if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::r_brace, |
| 1747 | tok::kw_else, tok::kw_try, tok::kw_catch, |
Daniel Jasper | 8893b8a | 2013-05-31 14:56:20 +0000 | [diff] [blame] | 1748 | tok::kw_for, |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1749 | // This gets rid of all ObjC @ keywords and methods. |
| 1750 | tok::at, tok::minus, tok::plus)) |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1751 | return; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1752 | |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1753 | FormatToken *Tok = (I + 1)->First; |
Daniel Jasper | 8893b8a | 2013-05-31 14:56:20 +0000 | [diff] [blame] | 1754 | if (Tok->is(tok::r_brace) && !Tok->MustBreakBefore && |
Alexander Kornienko | 0bdc643 | 2013-07-04 14:47:51 +0000 | [diff] [blame] | 1755 | (Tok->getNextNonComment() == NULL || |
| 1756 | Tok->getNextNonComment()->is(tok::semi))) { |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1757 | // We merge empty blocks even if the line exceeds the column limit. |
Daniel Jasper | 729a743 | 2013-02-11 12:36:37 +0000 | [diff] [blame] | 1758 | Tok->SpacesRequiredBefore = 0; |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1759 | Tok->CanBreakBefore = true; |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1760 | join(Line, *(I + 1)); |
| 1761 | I += 1; |
Daniel Jasper | 8893b8a | 2013-05-31 14:56:20 +0000 | [diff] [blame] | 1762 | } else if (Limit != 0 && Line.First->isNot(tok::kw_namespace)) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1763 | // Check that we still have three lines and they fit into the limit. |
| 1764 | if (I + 2 == E || (I + 2)->Type == LT_Invalid || |
| 1765 | !nextTwoLinesFitInto(I, Limit)) |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1766 | return; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1767 | |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1768 | // Second, check that the next line does not contain any braces - if it |
| 1769 | // does, readability declines when putting it into a single line. |
| 1770 | if ((I + 1)->Last->Type == TT_LineComment || Tok->MustBreakBefore) |
| 1771 | return; |
| 1772 | do { |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1773 | if (Tok->isOneOf(tok::l_brace, tok::r_brace)) |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1774 | return; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1775 | Tok = Tok->Next; |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1776 | } while (Tok != NULL); |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1777 | |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1778 | // Last, check that the third line contains a single closing brace. |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1779 | Tok = (I + 2)->First; |
Alexander Kornienko | 0bdc643 | 2013-07-04 14:47:51 +0000 | [diff] [blame] | 1780 | if (Tok->getNextNonComment() != NULL || Tok->isNot(tok::r_brace) || |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1781 | Tok->MustBreakBefore) |
| 1782 | return; |
| 1783 | |
| 1784 | join(Line, *(I + 1)); |
| 1785 | join(Line, *(I + 2)); |
| 1786 | I += 2; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1787 | } |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1788 | } |
| 1789 | |
| 1790 | bool nextTwoLinesFitInto(std::vector<AnnotatedLine>::iterator I, |
| 1791 | unsigned Limit) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1792 | return 1 + (I + 1)->Last->TotalLength + 1 + (I + 2)->Last->TotalLength <= |
| 1793 | Limit; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1796 | void join(AnnotatedLine &A, const AnnotatedLine &B) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1797 | assert(!A.Last->Next); |
| 1798 | assert(!B.First->Previous); |
| 1799 | A.Last->Next = B.First; |
| 1800 | B.First->Previous = A.Last; |
| 1801 | unsigned LengthA = A.Last->TotalLength + B.First->SpacesRequiredBefore; |
| 1802 | for (FormatToken *Tok = B.First; Tok; Tok = Tok->Next) { |
| 1803 | Tok->TotalLength += LengthA; |
| 1804 | A.Last = Tok; |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1805 | } |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 1806 | } |
| 1807 | |
Daniel Jasper | 6f21a98 | 2013-03-13 07:49:51 +0000 | [diff] [blame] | 1808 | bool touchesRanges(const CharSourceRange &Range) { |
Daniel Jasper | f302354 | 2013-03-07 20:50:00 +0000 | [diff] [blame] | 1809 | for (unsigned i = 0, e = Ranges.size(); i != e; ++i) { |
| 1810 | if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), |
| 1811 | Ranges[i].getBegin()) && |
| 1812 | !SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(), |
| 1813 | Range.getBegin())) |
| 1814 | return true; |
| 1815 | } |
| 1816 | return false; |
| 1817 | } |
| 1818 | |
| 1819 | bool touchesLine(const AnnotatedLine &TheLine) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1820 | const FormatToken *First = TheLine.First; |
| 1821 | const FormatToken *Last = TheLine.Last; |
Daniel Jasper | 84f5ddf | 2013-05-14 10:31:09 +0000 | [diff] [blame] | 1822 | CharSourceRange LineRange = CharSourceRange::getCharRange( |
Manuel Klimek | ad3094b | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 1823 | First->WhitespaceRange.getBegin().getLocWithOffset( |
| 1824 | First->LastNewlineOffset), |
Alexander Kornienko | 54e6c9d | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 1825 | Last->Tok.getLocation().getLocWithOffset(Last->TokenText.size() - 1)); |
Daniel Jasper | f302354 | 2013-03-07 20:50:00 +0000 | [diff] [blame] | 1826 | return touchesRanges(LineRange); |
| 1827 | } |
| 1828 | |
Daniel Jasper | 89b3a7f | 2013-05-10 13:00:49 +0000 | [diff] [blame] | 1829 | bool touchesPPDirective(std::vector<AnnotatedLine>::iterator I, |
| 1830 | std::vector<AnnotatedLine>::iterator E) { |
| 1831 | for (; I != E; ++I) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1832 | if (I->First->HasUnescapedNewline) |
Daniel Jasper | 89b3a7f | 2013-05-10 13:00:49 +0000 | [diff] [blame] | 1833 | return false; |
| 1834 | if (touchesLine(*I)) |
| 1835 | return true; |
| 1836 | } |
| 1837 | return false; |
| 1838 | } |
| 1839 | |
Daniel Jasper | f302354 | 2013-03-07 20:50:00 +0000 | [diff] [blame] | 1840 | bool touchesEmptyLineBefore(const AnnotatedLine &TheLine) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1841 | const FormatToken *First = TheLine.First; |
Daniel Jasper | f302354 | 2013-03-07 20:50:00 +0000 | [diff] [blame] | 1842 | CharSourceRange LineRange = CharSourceRange::getCharRange( |
Manuel Klimek | ad3094b | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 1843 | First->WhitespaceRange.getBegin(), |
| 1844 | First->WhitespaceRange.getBegin().getLocWithOffset( |
| 1845 | First->LastNewlineOffset)); |
Daniel Jasper | f302354 | 2013-03-07 20:50:00 +0000 | [diff] [blame] | 1846 | return touchesRanges(LineRange); |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 1847 | } |
| 1848 | |
| 1849 | virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) { |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 1850 | AnnotatedLines.push_back(AnnotatedLine(TheLine)); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1853 | /// \brief Add a new line and the required indent before the first Token |
| 1854 | /// of the \c UnwrappedLine if there was no structural parsing error. |
| 1855 | /// Returns the indent level of the \c UnwrappedLine. |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1856 | void formatFirstToken(const FormatToken &RootToken, |
| 1857 | const FormatToken *PreviousToken, unsigned Indent, |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 1858 | bool InPPDirective) { |
Daniel Jasper | 1a1ce83 | 2013-01-29 11:27:30 +0000 | [diff] [blame] | 1859 | unsigned Newlines = |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1860 | std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1); |
Daniel Jasper | 15f33f0 | 2013-06-03 16:16:41 +0000 | [diff] [blame] | 1861 | // Remove empty lines before "}" where applicable. |
| 1862 | if (RootToken.is(tok::r_brace) && |
| 1863 | (!RootToken.Next || |
| 1864 | (RootToken.Next->is(tok::semi) && !RootToken.Next->Next))) |
| 1865 | Newlines = std::min(Newlines, 1u); |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1866 | if (Newlines == 0 && !RootToken.IsFirst) |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1867 | Newlines = 1; |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1868 | |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 1869 | // Insert extra new line before access specifiers. |
| 1870 | if (PreviousToken && PreviousToken->isOneOf(tok::semi, tok::r_brace) && |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1871 | RootToken.isAccessSpecifier() && RootToken.NewlinesBefore == 1) |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 1872 | ++Newlines; |
Alexander Kornienko | 94b748f | 2013-03-27 17:08:02 +0000 | [diff] [blame] | 1873 | |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1874 | Whitespaces.replaceWhitespace( |
| 1875 | RootToken, Newlines, Indent, Indent, |
| 1876 | InPPDirective && !RootToken.HasUnescapedNewline); |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1879 | FormatStyle Style; |
| 1880 | Lexer &Lex; |
| 1881 | SourceManager &SourceMgr; |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 1882 | WhitespaceManager Whitespaces; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1883 | std::vector<CharSourceRange> Ranges; |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1884 | std::vector<AnnotatedLine> AnnotatedLines; |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1885 | |
| 1886 | encoding::Encoding Encoding; |
Daniel Jasper | c7bd68f | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 1887 | bool BinPackInconclusiveFunctions; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1888 | }; |
| 1889 | |
Craig Topper | 83f81d7 | 2013-06-30 22:29:28 +0000 | [diff] [blame] | 1890 | } // end anonymous namespace |
| 1891 | |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 1892 | tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex, |
| 1893 | SourceManager &SourceMgr, |
Daniel Jasper | caf42a3 | 2013-05-15 08:14:19 +0000 | [diff] [blame] | 1894 | std::vector<CharSourceRange> Ranges) { |
| 1895 | Formatter formatter(Style, Lex, SourceMgr, Ranges); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1896 | return formatter.format(); |
| 1897 | } |
| 1898 | |
Daniel Jasper | 8a99945 | 2013-05-16 10:40:07 +0000 | [diff] [blame] | 1899 | tooling::Replacements reformat(const FormatStyle &Style, StringRef Code, |
| 1900 | std::vector<tooling::Range> Ranges, |
| 1901 | StringRef FileName) { |
| 1902 | FileManager Files((FileSystemOptions())); |
| 1903 | DiagnosticsEngine Diagnostics( |
| 1904 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), |
| 1905 | new DiagnosticOptions); |
| 1906 | SourceManager SourceMgr(Diagnostics, Files); |
| 1907 | llvm::MemoryBuffer *Buf = llvm::MemoryBuffer::getMemBuffer(Code, FileName); |
| 1908 | const clang::FileEntry *Entry = |
| 1909 | Files.getVirtualFile(FileName, Buf->getBufferSize(), 0); |
| 1910 | SourceMgr.overrideFileContents(Entry, Buf); |
| 1911 | FileID ID = |
| 1912 | SourceMgr.createFileID(Entry, SourceLocation(), clang::SrcMgr::C_User); |
Alexander Kornienko | a1753f4 | 2013-06-28 12:51:24 +0000 | [diff] [blame] | 1913 | Lexer Lex(ID, SourceMgr.getBuffer(ID), SourceMgr, |
| 1914 | getFormattingLangOpts(Style.Standard)); |
Daniel Jasper | 8a99945 | 2013-05-16 10:40:07 +0000 | [diff] [blame] | 1915 | SourceLocation StartOfFile = SourceMgr.getLocForStartOfFile(ID); |
| 1916 | std::vector<CharSourceRange> CharRanges; |
| 1917 | for (unsigned i = 0, e = Ranges.size(); i != e; ++i) { |
| 1918 | SourceLocation Start = StartOfFile.getLocWithOffset(Ranges[i].getOffset()); |
| 1919 | SourceLocation End = Start.getLocWithOffset(Ranges[i].getLength()); |
| 1920 | CharRanges.push_back(CharSourceRange::getCharRange(Start, End)); |
| 1921 | } |
| 1922 | return reformat(Style, Lex, SourceMgr, CharRanges); |
| 1923 | } |
| 1924 | |
Alexander Kornienko | a1753f4 | 2013-06-28 12:51:24 +0000 | [diff] [blame] | 1925 | LangOptions getFormattingLangOpts(FormatStyle::LanguageStandard Standard) { |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1926 | LangOptions LangOpts; |
| 1927 | LangOpts.CPlusPlus = 1; |
Alexander Kornienko | a1753f4 | 2013-06-28 12:51:24 +0000 | [diff] [blame] | 1928 | LangOpts.CPlusPlus11 = Standard == FormatStyle::LS_Cpp03 ? 0 : 1; |
Daniel Jasper | b64eca0 | 2013-03-22 10:01:29 +0000 | [diff] [blame] | 1929 | LangOpts.LineComment = 1; |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1930 | LangOpts.Bool = 1; |
| 1931 | LangOpts.ObjC1 = 1; |
| 1932 | LangOpts.ObjC2 = 1; |
| 1933 | return LangOpts; |
| 1934 | } |
| 1935 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1936 | } // namespace format |
| 1937 | } // namespace clang |