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