Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1 | //===--- Format.cpp - Format C++ code -------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// \brief This file implements functions declared in Format.h. This will be |
| 12 | /// split into separate files as we go. |
| 13 | /// |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 16 | #define DEBUG_TYPE "format-formatter" |
| 17 | |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 18 | #include "BreakableToken.h" |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 19 | #include "TokenAnnotator.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "UnwrappedLineParser.h" |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 21 | #include "WhitespaceManager.h" |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 22 | #include "clang/Basic/Diagnostic.h" |
Daniel Jasper | 675d2e3 | 2012-12-21 10:20:02 +0000 | [diff] [blame] | 23 | #include "clang/Basic/OperatorPrecedence.h" |
Chandler Carruth | b99083e | 2013-01-02 10:28:36 +0000 | [diff] [blame] | 24 | #include "clang/Basic/SourceManager.h" |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 25 | #include "clang/Format/Format.h" |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 27 | #include "clang/Lex/Lexer.h" |
Alexander Kornienko | 5262dd9 | 2013-03-27 11:52:18 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/STLExtras.h" |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Allocator.h" |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Debug.h" |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 31 | #include "llvm/Support/YAMLTraits.h" |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 32 | #include <queue> |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 33 | #include <string> |
| 34 | |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 35 | namespace llvm { |
| 36 | namespace yaml { |
| 37 | template <> |
| 38 | struct ScalarEnumerationTraits<clang::format::FormatStyle::LanguageStandard> { |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame^] | 39 | static void enumeration(IO &IO, |
| 40 | clang::format::FormatStyle::LanguageStandard &Value) { |
| 41 | IO.enumCase(Value, "C++03", clang::format::FormatStyle::LS_Cpp03); |
| 42 | IO.enumCase(Value, "C++11", clang::format::FormatStyle::LS_Cpp11); |
| 43 | IO.enumCase(Value, "Auto", clang::format::FormatStyle::LS_Auto); |
| 44 | } |
| 45 | }; |
| 46 | |
| 47 | template<> |
| 48 | struct ScalarEnumerationTraits<clang::format::FormatStyle::BraceBreakingStyle> { |
| 49 | static void |
| 50 | enumeration(IO &IO, clang::format::FormatStyle::BraceBreakingStyle &Value) { |
| 51 | IO.enumCase(Value, "Attach", clang::format::FormatStyle::BS_Attach); |
| 52 | IO.enumCase(Value, "Linux", clang::format::FormatStyle::BS_Linux); |
| 53 | IO.enumCase(Value, "Stroustrup", clang::format::FormatStyle::BS_Stroustrup); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 54 | } |
| 55 | }; |
| 56 | |
| 57 | template <> struct MappingTraits<clang::format::FormatStyle> { |
| 58 | static void mapping(llvm::yaml::IO &IO, clang::format::FormatStyle &Style) { |
Alexander Kornienko | dd25631 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 59 | if (IO.outputting()) { |
| 60 | StringRef StylesArray[] = { "LLVM", "Google", "Chromium", "Mozilla" }; |
| 61 | ArrayRef<StringRef> Styles(StylesArray); |
| 62 | for (size_t i = 0, e = Styles.size(); i < e; ++i) { |
| 63 | StringRef StyleName(Styles[i]); |
| 64 | if (Style == clang::format::getPredefinedStyle(StyleName)) { |
| 65 | IO.mapOptional("# BasedOnStyle", StyleName); |
| 66 | break; |
| 67 | } |
| 68 | } |
| 69 | } else { |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 70 | StringRef BasedOnStyle; |
| 71 | IO.mapOptional("BasedOnStyle", BasedOnStyle); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 72 | if (!BasedOnStyle.empty()) |
| 73 | Style = clang::format::getPredefinedStyle(BasedOnStyle); |
| 74 | } |
| 75 | |
| 76 | IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset); |
| 77 | IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft); |
| 78 | IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine", |
| 79 | Style.AllowAllParametersOfDeclarationOnNextLine); |
| 80 | IO.mapOptional("AllowShortIfStatementsOnASingleLine", |
| 81 | Style.AllowShortIfStatementsOnASingleLine); |
| 82 | IO.mapOptional("BinPackParameters", Style.BinPackParameters); |
| 83 | IO.mapOptional("ColumnLimit", Style.ColumnLimit); |
| 84 | IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine", |
| 85 | Style.ConstructorInitializerAllOnOneLineOrOnePerLine); |
| 86 | IO.mapOptional("DerivePointerBinding", Style.DerivePointerBinding); |
| 87 | IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels); |
| 88 | IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep); |
| 89 | IO.mapOptional("ObjCSpaceBeforeProtocolList", |
| 90 | Style.ObjCSpaceBeforeProtocolList); |
| 91 | IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter); |
| 92 | IO.mapOptional("PenaltyReturnTypeOnItsOwnLine", |
| 93 | Style.PenaltyReturnTypeOnItsOwnLine); |
| 94 | IO.mapOptional("PointerBindsToType", Style.PointerBindsToType); |
| 95 | IO.mapOptional("SpacesBeforeTrailingComments", |
| 96 | Style.SpacesBeforeTrailingComments); |
| 97 | IO.mapOptional("Standard", Style.Standard); |
Manuel Klimek | 07a64ec | 2013-05-13 08:42:42 +0000 | [diff] [blame] | 98 | IO.mapOptional("IndentWidth", Style.IndentWidth); |
Manuel Klimek | 7c9a93e | 2013-05-13 09:22:11 +0000 | [diff] [blame] | 99 | IO.mapOptional("UseTab", Style.UseTab); |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame^] | 100 | IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 101 | } |
| 102 | }; |
| 103 | } |
| 104 | } |
| 105 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 106 | namespace clang { |
| 107 | namespace format { |
| 108 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 109 | FormatStyle getLLVMStyle() { |
| 110 | FormatStyle LLVMStyle; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 111 | LLVMStyle.AccessModifierOffset = -2; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 112 | LLVMStyle.AlignEscapedNewlinesLeft = false; |
Daniel Jasper | f157960 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 113 | LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true; |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 114 | LLVMStyle.AllowShortIfStatementsOnASingleLine = false; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 115 | LLVMStyle.BinPackParameters = true; |
| 116 | LLVMStyle.ColumnLimit = 80; |
| 117 | LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false; |
| 118 | LLVMStyle.DerivePointerBinding = false; |
| 119 | LLVMStyle.IndentCaseLabels = false; |
| 120 | LLVMStyle.MaxEmptyLinesToKeep = 1; |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 121 | LLVMStyle.ObjCSpaceBeforeProtocolList = true; |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 122 | LLVMStyle.PenaltyExcessCharacter = 1000000; |
Daniel Jasper | 1407bee | 2013-04-11 14:29:13 +0000 | [diff] [blame] | 123 | LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 75; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 124 | LLVMStyle.PointerBindsToType = false; |
| 125 | LLVMStyle.SpacesBeforeTrailingComments = 1; |
| 126 | LLVMStyle.Standard = FormatStyle::LS_Cpp03; |
Manuel Klimek | 07a64ec | 2013-05-13 08:42:42 +0000 | [diff] [blame] | 127 | LLVMStyle.IndentWidth = 2; |
Manuel Klimek | 7c9a93e | 2013-05-13 09:22:11 +0000 | [diff] [blame] | 128 | LLVMStyle.UseTab = false; |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame^] | 129 | LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 130 | return LLVMStyle; |
| 131 | } |
| 132 | |
| 133 | FormatStyle getGoogleStyle() { |
| 134 | FormatStyle GoogleStyle; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 135 | GoogleStyle.AccessModifierOffset = -1; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 136 | GoogleStyle.AlignEscapedNewlinesLeft = true; |
Daniel Jasper | f157960 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 137 | GoogleStyle.AllowAllParametersOfDeclarationOnNextLine = true; |
Daniel Jasper | 94d6ad7 | 2013-04-24 13:46:00 +0000 | [diff] [blame] | 138 | GoogleStyle.AllowShortIfStatementsOnASingleLine = true; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 139 | GoogleStyle.BinPackParameters = true; |
| 140 | GoogleStyle.ColumnLimit = 80; |
| 141 | GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; |
| 142 | GoogleStyle.DerivePointerBinding = true; |
| 143 | GoogleStyle.IndentCaseLabels = true; |
| 144 | GoogleStyle.MaxEmptyLinesToKeep = 1; |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 145 | GoogleStyle.ObjCSpaceBeforeProtocolList = false; |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 146 | GoogleStyle.PenaltyExcessCharacter = 1000000; |
Daniel Jasper | 1407bee | 2013-04-11 14:29:13 +0000 | [diff] [blame] | 147 | GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 148 | GoogleStyle.PointerBindsToType = true; |
| 149 | GoogleStyle.SpacesBeforeTrailingComments = 2; |
| 150 | GoogleStyle.Standard = FormatStyle::LS_Auto; |
Manuel Klimek | 07a64ec | 2013-05-13 08:42:42 +0000 | [diff] [blame] | 151 | GoogleStyle.IndentWidth = 2; |
Manuel Klimek | 7c9a93e | 2013-05-13 09:22:11 +0000 | [diff] [blame] | 152 | GoogleStyle.UseTab = false; |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame^] | 153 | GoogleStyle.BreakBeforeBraces = FormatStyle::BS_Attach; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 154 | return GoogleStyle; |
| 155 | } |
| 156 | |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 157 | FormatStyle getChromiumStyle() { |
| 158 | FormatStyle ChromiumStyle = getGoogleStyle(); |
Daniel Jasper | f157960 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 159 | ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false; |
Daniel Jasper | 94d6ad7 | 2013-04-24 13:46:00 +0000 | [diff] [blame] | 160 | ChromiumStyle.AllowShortIfStatementsOnASingleLine = false; |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 161 | ChromiumStyle.BinPackParameters = false; |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 162 | ChromiumStyle.Standard = FormatStyle::LS_Cpp03; |
| 163 | ChromiumStyle.DerivePointerBinding = false; |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 164 | return ChromiumStyle; |
| 165 | } |
| 166 | |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 167 | FormatStyle getMozillaStyle() { |
| 168 | FormatStyle MozillaStyle = getLLVMStyle(); |
| 169 | MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false; |
| 170 | MozillaStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; |
| 171 | MozillaStyle.DerivePointerBinding = true; |
| 172 | MozillaStyle.IndentCaseLabels = true; |
| 173 | MozillaStyle.ObjCSpaceBeforeProtocolList = false; |
| 174 | MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200; |
| 175 | MozillaStyle.PointerBindsToType = true; |
| 176 | return MozillaStyle; |
| 177 | } |
| 178 | |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 179 | FormatStyle getPredefinedStyle(StringRef Name) { |
| 180 | if (Name.equals_lower("llvm")) |
| 181 | return getLLVMStyle(); |
| 182 | if (Name.equals_lower("chromium")) |
| 183 | return getChromiumStyle(); |
| 184 | if (Name.equals_lower("mozilla")) |
| 185 | return getMozillaStyle(); |
| 186 | if (Name.equals_lower("google")) |
| 187 | return getGoogleStyle(); |
| 188 | |
| 189 | llvm::errs() << "Unknown style " << Name << ", using Google style.\n"; |
| 190 | return getGoogleStyle(); |
| 191 | } |
| 192 | |
| 193 | llvm::error_code parseConfiguration(StringRef Text, FormatStyle *Style) { |
| 194 | llvm::yaml::Input Input(Text); |
| 195 | Input >> *Style; |
| 196 | return Input.error(); |
| 197 | } |
| 198 | |
| 199 | std::string configurationAsText(const FormatStyle &Style) { |
| 200 | std::string Text; |
| 201 | llvm::raw_string_ostream Stream(Text); |
| 202 | llvm::yaml::Output Output(Stream); |
| 203 | // We use the same mapping method for input and output, so we need a non-const |
| 204 | // reference here. |
| 205 | FormatStyle NonConstStyle = Style; |
| 206 | Output << NonConstStyle; |
Alexander Kornienko | af64032 | 2013-05-13 12:41:08 +0000 | [diff] [blame] | 207 | Stream.flush(); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 208 | return Text; |
| 209 | } |
| 210 | |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 211 | // Returns the length of everything up to the first possible line break after |
| 212 | // the ), ], } or > matching \c Tok. |
| 213 | static unsigned getLengthToMatchingParen(const AnnotatedToken &Tok) { |
| 214 | if (Tok.MatchingParen == NULL) |
| 215 | return 0; |
| 216 | AnnotatedToken *End = Tok.MatchingParen; |
| 217 | while (!End->Children.empty() && !End->Children[0].CanBreakBefore) { |
| 218 | End = &End->Children[0]; |
| 219 | } |
| 220 | return End->TotalLength - Tok.TotalLength + 1; |
| 221 | } |
| 222 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 223 | class UnwrappedLineFormatter { |
| 224 | public: |
Manuel Klimek | 94fc6f1 | 2013-01-10 19:17:33 +0000 | [diff] [blame] | 225 | UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr, |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 226 | const AnnotatedLine &Line, unsigned FirstIndent, |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 227 | const AnnotatedToken &RootToken, |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 228 | WhitespaceManager &Whitespaces) |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 229 | : Style(Style), SourceMgr(SourceMgr), Line(Line), |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 230 | FirstIndent(FirstIndent), RootToken(RootToken), |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 231 | Whitespaces(Whitespaces), Count(0) {} |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 232 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 233 | /// \brief Formats an \c UnwrappedLine. |
| 234 | /// |
| 235 | /// \returns The column after the last token in the last line of the |
| 236 | /// \c UnwrappedLine. |
Daniel Jasper | a4d4621 | 2013-02-28 11:05:57 +0000 | [diff] [blame] | 237 | unsigned format(const AnnotatedLine *NextLine) { |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 238 | // Initialize state dependent on indent. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 239 | LineState State; |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 240 | State.Column = FirstIndent; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 241 | State.NextToken = &RootToken; |
Daniel Jasper | 6f21a98 | 2013-03-13 07:49:51 +0000 | [diff] [blame] | 242 | State.Stack.push_back( |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 243 | ParenState(FirstIndent, FirstIndent, !Style.BinPackParameters, |
Daniel Jasper | 001bf4e | 2013-04-22 07:59:53 +0000 | [diff] [blame] | 244 | /*NoLineBreak=*/ false)); |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 245 | State.LineContainsContinuedForLoopSection = false; |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 246 | State.ParenLevel = 0; |
Manuel Klimek | b56b6d1 | 2013-02-20 15:25:48 +0000 | [diff] [blame] | 247 | State.StartOfStringLiteral = 0; |
Daniel Jasper | cf5767d | 2013-02-18 11:05:07 +0000 | [diff] [blame] | 248 | State.StartOfLineLevel = State.ParenLevel; |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 249 | |
| 250 | // The first token has already been indented and thus consumed. |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 251 | moveStateToNextToken(State, /*DryRun=*/ false); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 252 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 253 | // If everything fits on a single line, just put it there. |
Daniel Jasper | a4d4621 | 2013-02-28 11:05:57 +0000 | [diff] [blame] | 254 | unsigned ColumnLimit = Style.ColumnLimit; |
| 255 | if (NextLine && NextLine->InPPDirective && |
| 256 | !NextLine->First.FormatTok.HasUnescapedNewline) |
| 257 | ColumnLimit = getColumnLimit(); |
| 258 | if (Line.Last->TotalLength <= ColumnLimit - FirstIndent) { |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 259 | while (State.NextToken != NULL) { |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 260 | addTokenToState(false, false, State); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 261 | } |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 262 | return State.Column; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 263 | } |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 264 | |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 265 | // If the ObjC method declaration does not fit on a line, we should format |
| 266 | // it with one arg per line. |
| 267 | if (Line.Type == LT_ObjCMethodDecl) |
| 268 | State.Stack.back().BreakBeforeParameter = true; |
| 269 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 270 | // Find best solution in solution space. |
| 271 | return analyzeSolutionSpace(State); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | private: |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 275 | void DebugTokenState(const AnnotatedToken &AnnotatedTok) { |
| 276 | const Token &Tok = AnnotatedTok.FormatTok.Tok; |
Alexander Kornienko | dd25631 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 277 | llvm::dbgs() << StringRef(SourceMgr.getCharacterData(Tok.getLocation()), |
Daniel Jasper | 1a1ce83 | 2013-01-29 11:27:30 +0000 | [diff] [blame] | 278 | Tok.getLength()); |
Alexander Kornienko | dd25631 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 279 | llvm::dbgs(); |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 282 | struct ParenState { |
Daniel Jasper | d399bff | 2013-02-05 09:41:21 +0000 | [diff] [blame] | 283 | ParenState(unsigned Indent, unsigned LastSpace, bool AvoidBinPacking, |
Daniel Jasper | 001bf4e | 2013-04-22 07:59:53 +0000 | [diff] [blame] | 284 | bool NoLineBreak) |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 285 | : Indent(Indent), LastSpace(LastSpace), FirstLessLess(0), |
| 286 | BreakBeforeClosingBrace(false), QuestionColumn(0), |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 287 | AvoidBinPacking(AvoidBinPacking), BreakBeforeParameter(false), |
Daniel Jasper | 001bf4e | 2013-04-22 07:59:53 +0000 | [diff] [blame] | 288 | NoLineBreak(NoLineBreak), ColonPos(0), StartOfFunctionCall(0), |
| 289 | NestedNameSpecifierContinuation(0), CallContinuation(0), |
Daniel Jasper | c3df5ff | 2013-05-13 09:19:24 +0000 | [diff] [blame] | 290 | VariablePos(0), ForFakeParenthesis(false) {} |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 291 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 292 | /// \brief The position to which a specific parenthesis level needs to be |
| 293 | /// indented. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 294 | unsigned Indent; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 295 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 296 | /// \brief The position of the last space on each level. |
| 297 | /// |
| 298 | /// Used e.g. to break like: |
| 299 | /// functionCall(Parameter, otherCall( |
| 300 | /// OtherParameter)); |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 301 | unsigned LastSpace; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 302 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 303 | /// \brief The position the first "<<" operator encountered on each level. |
| 304 | /// |
| 305 | /// Used to align "<<" operators. 0 if no such operator has been encountered |
| 306 | /// on a level. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 307 | unsigned FirstLessLess; |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 308 | |
Manuel Klimek | c8c8a47 | 2013-01-10 15:58:26 +0000 | [diff] [blame] | 309 | /// \brief Whether a newline needs to be inserted before the block's closing |
| 310 | /// brace. |
| 311 | /// |
| 312 | /// We only want to insert a newline before the closing brace if there also |
| 313 | /// was a newline after the beginning left brace. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 314 | bool BreakBeforeClosingBrace; |
| 315 | |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 316 | /// \brief The column of a \c ? in a conditional expression; |
| 317 | unsigned QuestionColumn; |
| 318 | |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 319 | /// \brief Avoid bin packing, i.e. multiple parameters/elements on multiple |
| 320 | /// lines, in this context. |
| 321 | bool AvoidBinPacking; |
| 322 | |
| 323 | /// \brief Break after the next comma (or all the commas in this context if |
| 324 | /// \c AvoidBinPacking is \c true). |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 325 | bool BreakBeforeParameter; |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 326 | |
Daniel Jasper | 001bf4e | 2013-04-22 07:59:53 +0000 | [diff] [blame] | 327 | /// \brief Line breaking in this context would break a formatting rule. |
| 328 | bool NoLineBreak; |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 329 | |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 330 | /// \brief The position of the colon in an ObjC method declaration/call. |
| 331 | unsigned ColonPos; |
Daniel Jasper | c4615b7 | 2013-02-20 12:56:39 +0000 | [diff] [blame] | 332 | |
Daniel Jasper | 2484971 | 2013-03-01 16:48:32 +0000 | [diff] [blame] | 333 | /// \brief The start of the most recent function in a builder-type call. |
| 334 | unsigned StartOfFunctionCall; |
| 335 | |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 336 | /// \brief If a nested name specifier was broken over multiple lines, this |
| 337 | /// contains the start column of the second line. Otherwise 0. |
| 338 | unsigned NestedNameSpecifierContinuation; |
| 339 | |
| 340 | /// \brief If a call expression was broken over multiple lines, this |
| 341 | /// contains the start column of the second line. Otherwise 0. |
| 342 | unsigned CallContinuation; |
| 343 | |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 344 | /// \brief The column of the first variable name in a variable declaration. |
| 345 | /// |
| 346 | /// Used to align further variables if necessary. |
| 347 | unsigned VariablePos; |
| 348 | |
Daniel Jasper | c3df5ff | 2013-05-13 09:19:24 +0000 | [diff] [blame] | 349 | /// \brief \c true if this \c ParenState was created for a fake parenthesis. |
| 350 | /// |
| 351 | /// Does not need to be considered for memoization / the comparison function |
| 352 | /// as otherwise identical states will have the same fake/non-fake |
| 353 | /// \c ParenStates. |
| 354 | bool ForFakeParenthesis; |
| 355 | |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 356 | bool operator<(const ParenState &Other) const { |
| 357 | if (Indent != Other.Indent) |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 358 | return Indent < Other.Indent; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 359 | if (LastSpace != Other.LastSpace) |
| 360 | return LastSpace < Other.LastSpace; |
| 361 | if (FirstLessLess != Other.FirstLessLess) |
| 362 | return FirstLessLess < Other.FirstLessLess; |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 363 | if (BreakBeforeClosingBrace != Other.BreakBeforeClosingBrace) |
| 364 | return BreakBeforeClosingBrace; |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 365 | if (QuestionColumn != Other.QuestionColumn) |
| 366 | return QuestionColumn < Other.QuestionColumn; |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 367 | if (AvoidBinPacking != Other.AvoidBinPacking) |
| 368 | return AvoidBinPacking; |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 369 | if (BreakBeforeParameter != Other.BreakBeforeParameter) |
| 370 | return BreakBeforeParameter; |
Daniel Jasper | 001bf4e | 2013-04-22 07:59:53 +0000 | [diff] [blame] | 371 | if (NoLineBreak != Other.NoLineBreak) |
| 372 | return NoLineBreak; |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 373 | if (ColonPos != Other.ColonPos) |
| 374 | return ColonPos < Other.ColonPos; |
Daniel Jasper | 2484971 | 2013-03-01 16:48:32 +0000 | [diff] [blame] | 375 | if (StartOfFunctionCall != Other.StartOfFunctionCall) |
| 376 | return StartOfFunctionCall < Other.StartOfFunctionCall; |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 377 | if (CallContinuation != Other.CallContinuation) |
| 378 | return CallContinuation < Other.CallContinuation; |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 379 | if (VariablePos != Other.VariablePos) |
| 380 | return VariablePos < Other.VariablePos; |
Daniel Jasper | b312314 | 2013-01-12 07:36:22 +0000 | [diff] [blame] | 381 | return false; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 382 | } |
| 383 | }; |
| 384 | |
| 385 | /// \brief The current state when indenting a unwrapped line. |
| 386 | /// |
| 387 | /// As the indenting tries different combinations this is copied by value. |
| 388 | struct LineState { |
| 389 | /// \brief The number of used columns in the current line. |
| 390 | unsigned Column; |
| 391 | |
| 392 | /// \brief The token that needs to be next formatted. |
| 393 | const AnnotatedToken *NextToken; |
| 394 | |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 395 | /// \brief \c true if this line contains a continued for-loop section. |
| 396 | bool LineContainsContinuedForLoopSection; |
| 397 | |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 398 | /// \brief The level of nesting inside (), [], <> and {}. |
| 399 | unsigned ParenLevel; |
| 400 | |
Daniel Jasper | cf5767d | 2013-02-18 11:05:07 +0000 | [diff] [blame] | 401 | /// \brief The \c ParenLevel at the start of this line. |
| 402 | unsigned StartOfLineLevel; |
| 403 | |
Manuel Klimek | b56b6d1 | 2013-02-20 15:25:48 +0000 | [diff] [blame] | 404 | /// \brief The start column of the string literal, if we're in a string |
| 405 | /// literal sequence, 0 otherwise. |
| 406 | unsigned StartOfStringLiteral; |
| 407 | |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 408 | /// \brief A stack keeping track of properties applying to parenthesis |
| 409 | /// levels. |
| 410 | std::vector<ParenState> Stack; |
| 411 | |
| 412 | /// \brief Comparison operator to be able to used \c LineState in \c map. |
| 413 | bool operator<(const LineState &Other) const { |
Daniel Jasper | d789670 | 2013-02-19 09:28:55 +0000 | [diff] [blame] | 414 | if (NextToken != Other.NextToken) |
| 415 | return NextToken < Other.NextToken; |
| 416 | if (Column != Other.Column) |
| 417 | return Column < Other.Column; |
Daniel Jasper | d789670 | 2013-02-19 09:28:55 +0000 | [diff] [blame] | 418 | if (LineContainsContinuedForLoopSection != |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 419 | Other.LineContainsContinuedForLoopSection) |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 420 | return LineContainsContinuedForLoopSection; |
Daniel Jasper | d789670 | 2013-02-19 09:28:55 +0000 | [diff] [blame] | 421 | if (ParenLevel != Other.ParenLevel) |
| 422 | return ParenLevel < Other.ParenLevel; |
| 423 | if (StartOfLineLevel != Other.StartOfLineLevel) |
| 424 | return StartOfLineLevel < Other.StartOfLineLevel; |
Manuel Klimek | b56b6d1 | 2013-02-20 15:25:48 +0000 | [diff] [blame] | 425 | if (StartOfStringLiteral != Other.StartOfStringLiteral) |
| 426 | return StartOfStringLiteral < Other.StartOfStringLiteral; |
Daniel Jasper | d789670 | 2013-02-19 09:28:55 +0000 | [diff] [blame] | 427 | return Stack < Other.Stack; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 428 | } |
| 429 | }; |
| 430 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 431 | /// \brief Appends the next token to \p State and updates information |
| 432 | /// necessary for indentation. |
| 433 | /// |
| 434 | /// Puts the token on the current line if \p Newline is \c true and adds a |
| 435 | /// line break and necessary indentation otherwise. |
| 436 | /// |
| 437 | /// If \p DryRun is \c false, also creates and stores the required |
| 438 | /// \c Replacement. |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 439 | unsigned addTokenToState(bool Newline, bool DryRun, LineState &State) { |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 440 | const AnnotatedToken &Current = *State.NextToken; |
| 441 | const AnnotatedToken &Previous = *State.NextToken->Parent; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 442 | |
Daniel Jasper | 92f9faf | 2013-03-20 15:58:10 +0000 | [diff] [blame] | 443 | if (State.Stack.size() == 0 || Current.Type == TT_ImplicitStringLiteral) { |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 444 | State.Column += State.NextToken->FormatTok.WhiteSpaceLength + |
| 445 | State.NextToken->FormatTok.TokenLength; |
| 446 | if (State.NextToken->Children.empty()) |
| 447 | State.NextToken = NULL; |
| 448 | else |
| 449 | State.NextToken = &State.NextToken->Children[0]; |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 450 | return 0; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Daniel Jasper | 3776ef3 | 2013-04-03 07:21:51 +0000 | [diff] [blame] | 453 | // If we are continuing an expression, we want to indent an extra 4 spaces. |
| 454 | unsigned ContinuationIndent = |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 455 | std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) + 4; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 456 | if (Newline) { |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 457 | unsigned WhitespaceStartColumn = State.Column; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 458 | if (Current.is(tok::r_brace)) { |
Manuel Klimek | 07a64ec | 2013-05-13 08:42:42 +0000 | [diff] [blame] | 459 | State.Column = Line.Level * Style.IndentWidth; |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 460 | } else if (Current.is(tok::string_literal) && |
Manuel Klimek | b56b6d1 | 2013-02-20 15:25:48 +0000 | [diff] [blame] | 461 | State.StartOfStringLiteral != 0) { |
| 462 | State.Column = State.StartOfStringLiteral; |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 463 | State.Stack.back().BreakBeforeParameter = true; |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 464 | } else if (Current.is(tok::lessless) && |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 465 | State.Stack.back().FirstLessLess != 0) { |
| 466 | State.Column = State.Stack.back().FirstLessLess; |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 467 | } else if (Current.isOneOf(tok::period, tok::arrow)) { |
Daniel Jasper | 3776ef3 | 2013-04-03 07:21:51 +0000 | [diff] [blame] | 468 | if (State.Stack.back().CallContinuation == 0) { |
| 469 | State.Column = ContinuationIndent; |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 470 | State.Stack.back().CallContinuation = State.Column; |
Daniel Jasper | 3776ef3 | 2013-04-03 07:21:51 +0000 | [diff] [blame] | 471 | } else { |
| 472 | State.Column = State.Stack.back().CallContinuation; |
| 473 | } |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 474 | } else if (Current.Type == TT_ConditionalExpr) { |
| 475 | State.Column = State.Stack.back().QuestionColumn; |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 476 | } else if (Previous.is(tok::comma) && |
| 477 | State.Stack.back().VariablePos != 0) { |
| 478 | State.Column = State.Stack.back().VariablePos; |
Daniel Jasper | 3c08a81 | 2013-02-24 18:54:32 +0000 | [diff] [blame] | 479 | } else if (Previous.ClosesTemplateDeclaration || |
Daniel Jasper | 53e72cd | 2013-05-06 08:27:33 +0000 | [diff] [blame] | 480 | (Current.Type == TT_StartOfName && State.ParenLevel == 0 && |
| 481 | Line.StartsDefinition)) { |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 482 | State.Column = State.Stack.back().Indent; |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 483 | } else if (Current.Type == TT_ObjCSelectorName) { |
| 484 | if (State.Stack.back().ColonPos > Current.FormatTok.TokenLength) { |
| 485 | State.Column = |
| 486 | State.Stack.back().ColonPos - Current.FormatTok.TokenLength; |
| 487 | } else { |
| 488 | State.Column = State.Stack.back().Indent; |
| 489 | State.Stack.back().ColonPos = |
| 490 | State.Column + Current.FormatTok.TokenLength; |
| 491 | } |
Daniel Jasper | b2f063a | 2013-05-08 10:00:18 +0000 | [diff] [blame] | 492 | } else if (Current.Type == TT_StartOfName || |
| 493 | Previous.isOneOf(tok::coloncolon, tok::equal) || |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 494 | Previous.Type == TT_ObjCMethodExpr) { |
Daniel Jasper | 3776ef3 | 2013-04-03 07:21:51 +0000 | [diff] [blame] | 495 | State.Column = ContinuationIndent; |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 496 | } else { |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 497 | State.Column = State.Stack.back().Indent; |
Daniel Jasper | 3776ef3 | 2013-04-03 07:21:51 +0000 | [diff] [blame] | 498 | // Ensure that we fall back to indenting 4 spaces instead of just |
| 499 | // flushing continuations left. |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 500 | if (State.Column == FirstIndent) |
| 501 | State.Column += 4; |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Daniel Jasper | 7878a7b | 2013-02-15 11:07:25 +0000 | [diff] [blame] | 504 | if (Current.is(tok::question)) |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 505 | State.Stack.back().BreakBeforeParameter = true; |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 506 | if ((Previous.isOneOf(tok::comma, tok::semi) && |
| 507 | !State.Stack.back().AvoidBinPacking) || |
| 508 | Previous.Type == TT_BinaryOperator) |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 509 | State.Stack.back().BreakBeforeParameter = false; |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 510 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 511 | if (!DryRun) { |
Daniel Jasper | 1ef81d5 | 2013-02-26 13:10:34 +0000 | [diff] [blame] | 512 | unsigned NewLines = 1; |
| 513 | if (Current.Type == TT_LineComment) |
| 514 | NewLines = |
| 515 | std::max(NewLines, std::min(Current.FormatTok.NewlinesBefore, |
| 516 | Style.MaxEmptyLinesToKeep + 1)); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 517 | if (!Line.InPPDirective) |
Daniel Jasper | c4615b7 | 2013-02-20 12:56:39 +0000 | [diff] [blame] | 518 | Whitespaces.replaceWhitespace(Current, NewLines, State.Column, |
Alexander Kornienko | 052685c | 2013-03-19 17:41:36 +0000 | [diff] [blame] | 519 | WhitespaceStartColumn); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 520 | else |
Daniel Jasper | c4615b7 | 2013-02-20 12:56:39 +0000 | [diff] [blame] | 521 | Whitespaces.replacePPWhitespace(Current, NewLines, State.Column, |
Alexander Kornienko | 052685c | 2013-03-19 17:41:36 +0000 | [diff] [blame] | 522 | WhitespaceStartColumn); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 523 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 524 | |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 525 | State.Stack.back().LastSpace = State.Column; |
Daniel Jasper | 04abbb2 | 2013-05-10 13:37:16 +0000 | [diff] [blame] | 526 | if (Current.isOneOf(tok::arrow, tok::period)) |
| 527 | State.Stack.back().LastSpace += Current.FormatTok.TokenLength; |
Daniel Jasper | cf5767d | 2013-02-18 11:05:07 +0000 | [diff] [blame] | 528 | State.StartOfLineLevel = State.ParenLevel; |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 529 | |
| 530 | // Any break on this level means that the parent level has been broken |
| 531 | // and we need to avoid bin packing there. |
| 532 | for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) { |
| 533 | State.Stack[i].BreakBeforeParameter = true; |
| 534 | } |
Daniel Jasper | 01218ff | 2013-04-15 22:36:37 +0000 | [diff] [blame] | 535 | const AnnotatedToken *TokenBefore = Current.getPreviousNoneComment(); |
| 536 | if (TokenBefore && !TokenBefore->isOneOf(tok::comma, tok::semi) && |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 537 | TokenBefore->Type != TT_BinaryOperator && !TokenBefore->opensScope()) |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 538 | State.Stack.back().BreakBeforeParameter = true; |
| 539 | |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 540 | // If we break after {, we should also break before the corresponding }. |
| 541 | if (Previous.is(tok::l_brace)) |
| 542 | State.Stack.back().BreakBeforeClosingBrace = true; |
| 543 | |
| 544 | if (State.Stack.back().AvoidBinPacking) { |
| 545 | // If we are breaking after '(', '{', '<', this is not bin packing |
| 546 | // unless AllowAllParametersOfDeclarationOnNextLine is false. |
Daniel Jasper | 3c08a81 | 2013-02-24 18:54:32 +0000 | [diff] [blame] | 547 | if ((Previous.isNot(tok::l_paren) && Previous.isNot(tok::l_brace)) || |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 548 | (!Style.AllowAllParametersOfDeclarationOnNextLine && |
| 549 | Line.MustBeDeclaration)) |
| 550 | State.Stack.back().BreakBeforeParameter = true; |
| 551 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 552 | } else { |
Daniel Jasper | 9c3e71a | 2013-02-25 15:59:54 +0000 | [diff] [blame] | 553 | if (Current.is(tok::equal) && |
Daniel Jasper | adc0f09 | 2013-04-05 09:38:50 +0000 | [diff] [blame] | 554 | (RootToken.is(tok::kw_for) || State.ParenLevel == 0) && |
| 555 | State.Stack.back().VariablePos == 0) { |
| 556 | State.Stack.back().VariablePos = State.Column; |
| 557 | // Move over * and & if they are bound to the variable name. |
| 558 | const AnnotatedToken *Tok = &Previous; |
| 559 | while (Tok && |
| 560 | State.Stack.back().VariablePos >= Tok->FormatTok.TokenLength) { |
| 561 | State.Stack.back().VariablePos -= Tok->FormatTok.TokenLength; |
| 562 | if (Tok->SpacesRequiredBefore != 0) |
| 563 | break; |
| 564 | Tok = Tok->Parent; |
| 565 | } |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 566 | if (Previous.PartOfMultiVariableDeclStmt) |
| 567 | State.Stack.back().LastSpace = State.Stack.back().VariablePos; |
| 568 | } |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 569 | |
Daniel Jasper | 729a743 | 2013-02-11 12:36:37 +0000 | [diff] [blame] | 570 | unsigned Spaces = State.NextToken->SpacesRequiredBefore; |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 571 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 572 | if (!DryRun) |
Alexander Kornienko | 052685c | 2013-03-19 17:41:36 +0000 | [diff] [blame] | 573 | Whitespaces.replaceWhitespace(Current, 0, Spaces, State.Column); |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 574 | |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 575 | if (Current.Type == TT_ObjCSelectorName && |
| 576 | State.Stack.back().ColonPos == 0) { |
| 577 | if (State.Stack.back().Indent + Current.LongestObjCSelectorName > |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 578 | State.Column + Spaces + Current.FormatTok.TokenLength) |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 579 | State.Stack.back().ColonPos = |
| 580 | State.Stack.back().Indent + Current.LongestObjCSelectorName; |
| 581 | else |
| 582 | State.Stack.back().ColonPos = |
Daniel Jasper | 9e9e6e0 | 2013-02-06 16:00:26 +0000 | [diff] [blame] | 583 | State.Column + Spaces + Current.FormatTok.TokenLength; |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Daniel Jasper | ac3223e | 2013-04-10 09:49:49 +0000 | [diff] [blame] | 586 | if (Previous.opensScope() && Previous.Type != TT_ObjCMethodExpr && |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 587 | Current.Type != TT_LineComment) |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 588 | State.Stack.back().Indent = State.Column + Spaces; |
Daniel Jasper | 001bf4e | 2013-04-22 07:59:53 +0000 | [diff] [blame] | 589 | if (Previous.is(tok::comma) && !Current.isTrailingComment() && |
| 590 | State.Stack.back().AvoidBinPacking) |
| 591 | State.Stack.back().NoLineBreak = true; |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 592 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 593 | State.Column += Spaces; |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 594 | if (Current.is(tok::l_paren) && Previous.isOneOf(tok::kw_if, tok::kw_for)) |
Daniel Jasper | e438bac | 2013-01-23 20:41:06 +0000 | [diff] [blame] | 595 | // Treat the condition inside an if as if it was a second function |
| 596 | // parameter, i.e. let nested calls have an indent of 4. |
| 597 | State.Stack.back().LastSpace = State.Column + 1; // 1 is length of "(". |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 598 | else if (Previous.is(tok::comma)) |
Daniel Jasper | e438bac | 2013-01-23 20:41:06 +0000 | [diff] [blame] | 599 | State.Stack.back().LastSpace = State.Column; |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 600 | else if ((Previous.Type == TT_BinaryOperator || |
Daniel Jasper | 02b771e | 2013-01-28 13:31:35 +0000 | [diff] [blame] | 601 | Previous.Type == TT_ConditionalExpr || |
| 602 | Previous.Type == TT_CtorInitializerColon) && |
Daniel Jasper | ae8699b | 2013-01-28 09:35:24 +0000 | [diff] [blame] | 603 | getPrecedence(Previous) != prec::Assignment) |
| 604 | State.Stack.back().LastSpace = State.Column; |
Daniel Jasper | 6cabab4 | 2013-02-14 08:42:54 +0000 | [diff] [blame] | 605 | else if (Previous.Type == TT_InheritanceColon) |
| 606 | State.Stack.back().Indent = State.Column; |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 607 | else if (Previous.opensScope() && !Current.FakeLParens.empty()) |
| 608 | // If this function has multiple parameters or a binary expression |
| 609 | // parameter, indent nested calls from the start of the first parameter. |
Daniel Jasper | 986e17f | 2013-01-28 07:35:34 +0000 | [diff] [blame] | 610 | State.Stack.back().LastSpace = State.Column; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 611 | } |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 612 | |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 613 | return moveStateToNextToken(State, DryRun); |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 614 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 615 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 616 | /// \brief Mark the next token as consumed in \p State and modify its stacks |
| 617 | /// accordingly. |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 618 | unsigned moveStateToNextToken(LineState &State, bool DryRun) { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 619 | const AnnotatedToken &Current = *State.NextToken; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 620 | assert(State.Stack.size()); |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 621 | |
Daniel Jasper | 6cabab4 | 2013-02-14 08:42:54 +0000 | [diff] [blame] | 622 | if (Current.Type == TT_InheritanceColon) |
| 623 | State.Stack.back().AvoidBinPacking = true; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 624 | if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0) |
| 625 | State.Stack.back().FirstLessLess = State.Column; |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 626 | if (Current.is(tok::question)) |
| 627 | State.Stack.back().QuestionColumn = State.Column; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 628 | if (Current.isOneOf(tok::period, tok::arrow) && |
Daniel Jasper | 2484971 | 2013-03-01 16:48:32 +0000 | [diff] [blame] | 629 | Line.Type == LT_BuilderTypeCall && State.ParenLevel == 0) |
| 630 | State.Stack.back().StartOfFunctionCall = |
Daniel Jasper | 04abbb2 | 2013-05-10 13:37:16 +0000 | [diff] [blame] | 631 | Current.LastInChainOfCalls ? 0 : State.Column + |
| 632 | Current.FormatTok.TokenLength; |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 633 | if (Current.Type == TT_CtorInitializerColon) { |
Manuel Klimek | 07a64ec | 2013-05-13 08:42:42 +0000 | [diff] [blame] | 634 | // Indent 2 from the column, so: |
| 635 | // SomeClass::SomeClass() |
| 636 | // : First(...), ... |
| 637 | // Next(...) |
| 638 | // ^ line up here. |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 639 | State.Stack.back().Indent = State.Column + 2; |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 640 | if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine) |
| 641 | State.Stack.back().AvoidBinPacking = true; |
| 642 | State.Stack.back().BreakBeforeParameter = false; |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 643 | } |
Daniel Jasper | 3776ef3 | 2013-04-03 07:21:51 +0000 | [diff] [blame] | 644 | |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 645 | // If return returns a binary expression, align after it. |
| 646 | if (Current.is(tok::kw_return) && !Current.FakeLParens.empty()) |
| 647 | State.Stack.back().LastSpace = State.Column + 7; |
| 648 | |
Daniel Jasper | 3776ef3 | 2013-04-03 07:21:51 +0000 | [diff] [blame] | 649 | // In ObjC method declaration we align on the ":" of parameters, but we need |
| 650 | // to ensure that we indent parameters on subsequent lines by at least 4. |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 651 | if (Current.Type == TT_ObjCMethodSpecifier) |
| 652 | State.Stack.back().Indent += 4; |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 653 | |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 654 | // Insert scopes created by fake parenthesis. |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 655 | const AnnotatedToken *Previous = Current.getPreviousNoneComment(); |
| 656 | // Don't add extra indentation for the first fake parenthesis after |
| 657 | // 'return', assignements or opening <({[. The indentation for these cases |
| 658 | // is special cased. |
| 659 | bool SkipFirstExtraIndent = |
| 660 | Current.is(tok::kw_return) || |
Daniel Jasper | ac3223e | 2013-04-10 09:49:49 +0000 | [diff] [blame] | 661 | (Previous && (Previous->opensScope() || |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 662 | getPrecedence(*Previous) == prec::Assignment)); |
| 663 | for (SmallVector<prec::Level, 4>::const_reverse_iterator |
| 664 | I = Current.FakeLParens.rbegin(), |
| 665 | E = Current.FakeLParens.rend(); |
| 666 | I != E; ++I) { |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 667 | ParenState NewParenState = State.Stack.back(); |
Daniel Jasper | c3df5ff | 2013-05-13 09:19:24 +0000 | [diff] [blame] | 668 | NewParenState.ForFakeParenthesis = true; |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 669 | NewParenState.Indent = |
| 670 | std::max(std::max(State.Column, NewParenState.Indent), |
| 671 | State.Stack.back().LastSpace); |
| 672 | |
| 673 | // Always indent conditional expressions. Never indent expression where |
| 674 | // the 'operator' is ',', ';' or an assignment (i.e. *I <= |
| 675 | // prec::Assignment) as those have different indentation rules. Indent |
| 676 | // other expression, unless the indentation needs to be skipped. |
| 677 | if (*I == prec::Conditional || |
| 678 | (!SkipFirstExtraIndent && *I > prec::Assignment)) |
| 679 | NewParenState.Indent += 4; |
Daniel Jasper | ac3223e | 2013-04-10 09:49:49 +0000 | [diff] [blame] | 680 | if (Previous && !Previous->opensScope()) |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 681 | NewParenState.BreakBeforeParameter = false; |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 682 | State.Stack.push_back(NewParenState); |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 683 | SkipFirstExtraIndent = false; |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 686 | // If we encounter an opening (, [, { or <, we add a level to our stacks to |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 687 | // prepare for the following tokens. |
Daniel Jasper | ac3223e | 2013-04-10 09:49:49 +0000 | [diff] [blame] | 688 | if (Current.opensScope()) { |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 689 | unsigned NewIndent; |
Daniel Jasper | c3df5ff | 2013-05-13 09:19:24 +0000 | [diff] [blame] | 690 | unsigned LastSpace = State.Stack.back().LastSpace; |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 691 | bool AvoidBinPacking; |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 692 | if (Current.is(tok::l_brace)) { |
Daniel Jasper | c3df5ff | 2013-05-13 09:19:24 +0000 | [diff] [blame] | 693 | NewIndent = Style.IndentWidth + LastSpace; |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 694 | AvoidBinPacking = false; |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 695 | } else { |
Daniel Jasper | c3df5ff | 2013-05-13 09:19:24 +0000 | [diff] [blame] | 696 | NewIndent = |
| 697 | 4 + std::max(LastSpace, State.Stack.back().StartOfFunctionCall); |
Daniel Jasper | 001bf4e | 2013-04-22 07:59:53 +0000 | [diff] [blame] | 698 | AvoidBinPacking = !Style.BinPackParameters; |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 699 | } |
Daniel Jasper | fca24bc | 2013-04-25 13:31:51 +0000 | [diff] [blame] | 700 | |
| 701 | if (Current.NoMoreTokensOnLevel && Current.FakeLParens.empty()) { |
| 702 | // This parenthesis was the last token possibly making use of Indent and |
Daniel Jasper | c3df5ff | 2013-05-13 09:19:24 +0000 | [diff] [blame] | 703 | // LastSpace of the next higher ParenLevel. Thus, erase them to achieve |
Daniel Jasper | fca24bc | 2013-04-25 13:31:51 +0000 | [diff] [blame] | 704 | // better memoization results. |
Daniel Jasper | c3df5ff | 2013-05-13 09:19:24 +0000 | [diff] [blame] | 705 | for (unsigned i = State.Stack.size() - 1; i > 0; --i) { |
| 706 | State.Stack[i].Indent = 0; |
| 707 | State.Stack[i].LastSpace = 0; |
| 708 | if (!State.Stack[i].ForFakeParenthesis) |
| 709 | break; |
| 710 | } |
Daniel Jasper | fca24bc | 2013-04-25 13:31:51 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Daniel Jasper | c3df5ff | 2013-05-13 09:19:24 +0000 | [diff] [blame] | 713 | State.Stack.push_back(ParenState(NewIndent, LastSpace, AvoidBinPacking, |
| 714 | State.Stack.back().NoLineBreak)); |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 715 | ++State.ParenLevel; |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 718 | // If this '[' opens an ObjC call, determine whether all parameters fit into |
| 719 | // one line and put one per line if they don't. |
| 720 | if (Current.is(tok::l_square) && Current.Type == TT_ObjCMethodExpr && |
| 721 | Current.MatchingParen != NULL) { |
| 722 | if (getLengthToMatchingParen(Current) + State.Column > getColumnLimit()) |
| 723 | State.Stack.back().BreakBeforeParameter = true; |
| 724 | } |
| 725 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 726 | // If we encounter a closing ), ], } or >, we can remove a level from our |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 727 | // stacks. |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 728 | if (Current.isOneOf(tok::r_paren, tok::r_square) || |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 729 | (Current.is(tok::r_brace) && State.NextToken != &RootToken) || |
| 730 | State.NextToken->Type == TT_TemplateCloser) { |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 731 | State.Stack.pop_back(); |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 732 | --State.ParenLevel; |
| 733 | } |
| 734 | |
| 735 | // Remove scopes created by fake parenthesis. |
| 736 | for (unsigned i = 0, e = Current.FakeRParens; i != e; ++i) { |
Daniel Jasper | abfc9c1 | 2013-04-04 19:31:00 +0000 | [diff] [blame] | 737 | unsigned VariablePos = State.Stack.back().VariablePos; |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 738 | State.Stack.pop_back(); |
Daniel Jasper | abfc9c1 | 2013-04-04 19:31:00 +0000 | [diff] [blame] | 739 | State.Stack.back().VariablePos = VariablePos; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 740 | } |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 741 | |
Manuel Klimek | e9a6226 | 2013-02-20 15:32:58 +0000 | [diff] [blame] | 742 | if (Current.is(tok::string_literal)) { |
Manuel Klimek | b56b6d1 | 2013-02-20 15:25:48 +0000 | [diff] [blame] | 743 | State.StartOfStringLiteral = State.Column; |
| 744 | } else if (Current.isNot(tok::comment)) { |
| 745 | State.StartOfStringLiteral = 0; |
| 746 | } |
| 747 | |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 748 | State.Column += Current.FormatTok.TokenLength; |
| 749 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 750 | if (State.NextToken->Children.empty()) |
| 751 | State.NextToken = NULL; |
| 752 | else |
| 753 | State.NextToken = &State.NextToken->Children[0]; |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 754 | |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 755 | return breakProtrudingToken(Current, State, DryRun); |
| 756 | } |
| 757 | |
| 758 | /// \brief If the current token sticks out over the end of the line, break |
| 759 | /// it if possible. |
| 760 | unsigned breakProtrudingToken(const AnnotatedToken &Current, LineState &State, |
| 761 | bool DryRun) { |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 762 | llvm::OwningPtr<BreakableToken> Token; |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 763 | unsigned StartColumn = State.Column - Current.FormatTok.TokenLength; |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 764 | if (Current.is(tok::string_literal)) { |
| 765 | // Only break up default narrow strings. |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 766 | const char *LiteralData = SourceMgr.getCharacterData( |
| 767 | Current.FormatTok.getStartOfNonWhitespace()); |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 768 | if (!LiteralData || *LiteralData != '"') |
| 769 | return 0; |
| 770 | |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 771 | Token.reset(new BreakableStringLiteral(SourceMgr, Current.FormatTok, |
| 772 | StartColumn)); |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 773 | } else if (Current.Type == TT_BlockComment) { |
| 774 | BreakableBlockComment *BBC = |
| 775 | new BreakableBlockComment(SourceMgr, Current, StartColumn); |
| 776 | if (!DryRun) |
| 777 | BBC->alignLines(Whitespaces); |
| 778 | Token.reset(BBC); |
Daniel Jasper | 7ff96ed | 2013-05-06 10:24:51 +0000 | [diff] [blame] | 779 | } else if (Current.Type == TT_LineComment && |
| 780 | (Current.Parent == NULL || |
| 781 | Current.Parent->Type != TT_ImplicitStringLiteral)) { |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 782 | Token.reset(new BreakableLineComment(SourceMgr, Current, StartColumn)); |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 783 | } else { |
| 784 | return 0; |
| 785 | } |
| 786 | |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 787 | bool BreakInserted = false; |
| 788 | unsigned Penalty = 0; |
| 789 | for (unsigned LineIndex = 0; LineIndex < Token->getLineCount(); |
| 790 | ++LineIndex) { |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 791 | unsigned TailOffset = 0; |
| 792 | unsigned RemainingLength = |
| 793 | Token->getLineLengthAfterSplit(LineIndex, TailOffset); |
| 794 | while (RemainingLength > getColumnLimit()) { |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 795 | BreakableToken::Split Split = |
| 796 | Token->getSplit(LineIndex, TailOffset, getColumnLimit()); |
| 797 | if (Split.first == StringRef::npos) |
| 798 | break; |
| 799 | assert(Split.first != 0); |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 800 | unsigned NewRemainingLength = Token->getLineLengthAfterSplit( |
| 801 | LineIndex, TailOffset + Split.first + Split.second); |
| 802 | if (NewRemainingLength >= RemainingLength) |
| 803 | break; |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 804 | if (!DryRun) { |
| 805 | Token->insertBreak(LineIndex, TailOffset, Split, Line.InPPDirective, |
| 806 | Whitespaces); |
| 807 | } |
| 808 | TailOffset += Split.first + Split.second; |
Alexander Kornienko | e2657dd | 2013-04-15 15:47:34 +0000 | [diff] [blame] | 809 | RemainingLength = NewRemainingLength; |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 810 | Penalty += Style.PenaltyExcessCharacter; |
| 811 | BreakInserted = true; |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 812 | } |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 813 | State.Column = RemainingLength; |
| 814 | if (!DryRun) { |
| 815 | Token->trimLine(LineIndex, TailOffset, Line.InPPDirective, Whitespaces); |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | if (BreakInserted) { |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 820 | for (unsigned i = 0, e = State.Stack.size(); i != e; ++i) |
| 821 | State.Stack[i].BreakBeforeParameter = true; |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 822 | State.Stack.back().LastSpace = StartColumn; |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 823 | } |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 824 | return Penalty; |
| 825 | } |
| 826 | |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 827 | unsigned getColumnLimit() { |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 828 | // In preprocessor directives reserve two chars for trailing " \" |
| 829 | return Style.ColumnLimit - (Line.InPPDirective ? 2 : 0); |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 832 | /// \brief An edge in the solution space from \c Previous->State to \c State, |
| 833 | /// inserting a newline dependent on the \c NewLine. |
| 834 | struct StateNode { |
| 835 | StateNode(const LineState &State, bool NewLine, StateNode *Previous) |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 836 | : State(State), NewLine(NewLine), Previous(Previous) {} |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 837 | LineState State; |
| 838 | bool NewLine; |
| 839 | StateNode *Previous; |
| 840 | }; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 841 | |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 842 | /// \brief A pair of <penalty, count> that is used to prioritize the BFS on. |
| 843 | /// |
| 844 | /// In case of equal penalties, we want to prefer states that were inserted |
| 845 | /// first. During state generation we make sure that we insert states first |
| 846 | /// that break the line as late as possible. |
| 847 | typedef std::pair<unsigned, unsigned> OrderedPenalty; |
| 848 | |
| 849 | /// \brief An item in the prioritized BFS search queue. The \c StateNode's |
| 850 | /// \c State has the given \c OrderedPenalty. |
| 851 | typedef std::pair<OrderedPenalty, StateNode *> QueueItem; |
| 852 | |
| 853 | /// \brief The BFS queue type. |
| 854 | typedef std::priority_queue<QueueItem, std::vector<QueueItem>, |
| 855 | std::greater<QueueItem> > QueueType; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 856 | |
| 857 | /// \brief Analyze the entire solution space starting from \p InitialState. |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 858 | /// |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 859 | /// This implements a variant of Dijkstra's algorithm on the graph that spans |
| 860 | /// the solution space (\c LineStates are the nodes). The algorithm tries to |
| 861 | /// find the shortest path (the one with lowest penalty) from \p InitialState |
| 862 | /// to a state where all tokens are placed. |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 863 | unsigned analyzeSolutionSpace(LineState &InitialState) { |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 864 | std::set<LineState> Seen; |
| 865 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 866 | // Insert start element into queue. |
Daniel Jasper | fc75908 | 2013-02-14 14:26:07 +0000 | [diff] [blame] | 867 | StateNode *Node = |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 868 | new (Allocator.Allocate()) StateNode(InitialState, false, NULL); |
| 869 | Queue.push(QueueItem(OrderedPenalty(0, Count), Node)); |
| 870 | ++Count; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 871 | |
| 872 | // While not empty, take first element and follow edges. |
| 873 | while (!Queue.empty()) { |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 874 | unsigned Penalty = Queue.top().first.first; |
Daniel Jasper | fc75908 | 2013-02-14 14:26:07 +0000 | [diff] [blame] | 875 | StateNode *Node = Queue.top().second; |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 876 | if (Node->State.NextToken == NULL) { |
Alexander Kornienko | dd25631 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 877 | DEBUG(llvm::dbgs() << "\n---\nPenalty for line: " << Penalty << "\n"); |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 878 | break; |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 879 | } |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 880 | Queue.pop(); |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 881 | |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 882 | if (!Seen.insert(Node->State).second) |
| 883 | // State already examined with lower penalty. |
| 884 | continue; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 885 | |
Manuel Klimek | 62a48fb | 2013-02-13 10:54:19 +0000 | [diff] [blame] | 886 | addNextStateToQueue(Penalty, Node, /*NewLine=*/ false); |
| 887 | addNextStateToQueue(Penalty, Node, /*NewLine=*/ true); |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | if (Queue.empty()) |
| 891 | // We were unable to find a solution, do nothing. |
| 892 | // FIXME: Add diagnostic? |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 893 | return 0; |
| 894 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 895 | // Reconstruct the solution. |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 896 | reconstructPath(InitialState, Queue.top().second); |
Alexander Kornienko | dd25631 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 897 | DEBUG(llvm::dbgs() << "Total number of analyzed states: " << Count << "\n"); |
| 898 | DEBUG(llvm::dbgs() << "---\n"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 899 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 900 | // Return the column after the last token of the solution. |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 901 | return Queue.top().second->State.Column; |
| 902 | } |
| 903 | |
| 904 | void reconstructPath(LineState &State, StateNode *Current) { |
| 905 | // FIXME: This recursive implementation limits the possible number |
| 906 | // of tokens per line if compiled into a binary with small stack space. |
| 907 | // To become more independent of stack frame limitations we would need |
| 908 | // to also change the TokenAnnotator. |
| 909 | if (Current->Previous == NULL) |
| 910 | return; |
| 911 | reconstructPath(State, Current->Previous); |
| 912 | DEBUG({ |
| 913 | if (Current->NewLine) { |
Alexander Kornienko | dd25631 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 914 | llvm::dbgs() |
Daniel Jasper | a03ab10 | 2013-02-13 20:33:44 +0000 | [diff] [blame] | 915 | << "Penalty for splitting before " |
| 916 | << Current->Previous->State.NextToken->FormatTok.Tok.getName() |
| 917 | << ": " << Current->Previous->State.NextToken->SplitPenalty << "\n"; |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 918 | } |
| 919 | }); |
| 920 | addTokenToState(Current->NewLine, false, State); |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 921 | } |
| 922 | |
Manuel Klimek | 62a48fb | 2013-02-13 10:54:19 +0000 | [diff] [blame] | 923 | /// \brief Add the following state to the analysis queue \c Queue. |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 924 | /// |
Manuel Klimek | 62a48fb | 2013-02-13 10:54:19 +0000 | [diff] [blame] | 925 | /// Assume the current state is \p PreviousNode and has been reached with a |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 926 | /// penalty of \p Penalty. Insert a line break if \p NewLine is \c true. |
Manuel Klimek | 62a48fb | 2013-02-13 10:54:19 +0000 | [diff] [blame] | 927 | void addNextStateToQueue(unsigned Penalty, StateNode *PreviousNode, |
| 928 | bool NewLine) { |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 929 | if (NewLine && !canBreak(PreviousNode->State)) |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 930 | return; |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 931 | if (!NewLine && mustBreak(PreviousNode->State)) |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 932 | return; |
Daniel Jasper | ae8699b | 2013-01-28 09:35:24 +0000 | [diff] [blame] | 933 | if (NewLine) |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 934 | Penalty += PreviousNode->State.NextToken->SplitPenalty; |
| 935 | |
| 936 | StateNode *Node = new (Allocator.Allocate()) |
| 937 | StateNode(PreviousNode->State, NewLine, PreviousNode); |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 938 | Penalty += addTokenToState(NewLine, true, Node->State); |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 939 | if (Node->State.Column > getColumnLimit()) { |
| 940 | unsigned ExcessCharacters = Node->State.Column - getColumnLimit(); |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 941 | Penalty += Style.PenaltyExcessCharacter * ExcessCharacters; |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 942 | } |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 943 | |
| 944 | Queue.push(QueueItem(OrderedPenalty(Penalty, Count), Node)); |
| 945 | ++Count; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 946 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 947 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 948 | /// \brief Returns \c true, if a line break after \p State is allowed. |
| 949 | bool canBreak(const LineState &State) { |
| 950 | if (!State.NextToken->CanBreakBefore && |
| 951 | !(State.NextToken->is(tok::r_brace) && |
| 952 | State.Stack.back().BreakBeforeClosingBrace)) |
| 953 | return false; |
Daniel Jasper | 001bf4e | 2013-04-22 07:59:53 +0000 | [diff] [blame] | 954 | return !State.Stack.back().NoLineBreak; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 955 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 956 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 957 | /// \brief Returns \c true, if a line break after \p State is mandatory. |
| 958 | bool mustBreak(const LineState &State) { |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 959 | const AnnotatedToken &Current = *State.NextToken; |
| 960 | const AnnotatedToken &Previous = *Current.Parent; |
| 961 | if (Current.MustBreakBefore || Current.Type == TT_InlineASMColon) |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 962 | return true; |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 963 | if (Current.is(tok::r_brace) && State.Stack.back().BreakBeforeClosingBrace) |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 964 | return true; |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 965 | if (Previous.is(tok::semi) && State.LineContainsContinuedForLoopSection) |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 966 | return true; |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 967 | if ((Previous.isOneOf(tok::comma, tok::semi) || Current.is(tok::question) || |
| 968 | Current.Type == TT_ConditionalExpr) && |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 969 | State.Stack.back().BreakBeforeParameter && |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 970 | !Current.isTrailingComment() && |
| 971 | !Current.isOneOf(tok::r_paren, tok::r_brace)) |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 972 | return true; |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 973 | |
| 974 | // If we need to break somewhere inside the LHS of a binary expression, we |
| 975 | // should also break after the operator. |
| 976 | if (Previous.Type == TT_BinaryOperator && |
| 977 | !Previous.isOneOf(tok::lessless, tok::question) && |
| 978 | getPrecedence(Previous) != prec::Assignment && |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 979 | State.Stack.back().BreakBeforeParameter) |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 980 | return true; |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 981 | |
| 982 | // FIXME: Comparing LongestObjCSelectorName to 0 is a hacky way of finding |
| 983 | // out whether it is the first parameter. Clean this up. |
| 984 | if (Current.Type == TT_ObjCSelectorName && |
| 985 | Current.LongestObjCSelectorName == 0 && |
| 986 | State.Stack.back().BreakBeforeParameter) |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 987 | return true; |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 988 | if ((Current.Type == TT_CtorInitializerColon || |
| 989 | (Previous.ClosesTemplateDeclaration && State.ParenLevel == 0))) |
Daniel Jasper | 923ebef | 2013-03-14 13:45:21 +0000 | [diff] [blame] | 990 | return true; |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 991 | |
Daniel Jasper | 3af59ce | 2013-03-15 14:57:30 +0000 | [diff] [blame] | 992 | // This prevents breaks like: |
| 993 | // ... |
| 994 | // SomeParameter, OtherParameter).DoSomething( |
| 995 | // ... |
| 996 | // As they hide "DoSomething" and generally bad for readability. |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 997 | if (Current.isOneOf(tok::period, tok::arrow) && |
Daniel Jasper | 3af59ce | 2013-03-15 14:57:30 +0000 | [diff] [blame] | 998 | getRemainingLength(State) + State.Column > getColumnLimit() && |
| 999 | State.ParenLevel < State.StartOfLineLevel) |
| 1000 | return true; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1001 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
Daniel Jasper | 3af59ce | 2013-03-15 14:57:30 +0000 | [diff] [blame] | 1004 | // Returns the total number of columns required for the remaining tokens. |
| 1005 | unsigned getRemainingLength(const LineState &State) { |
| 1006 | if (State.NextToken && State.NextToken->Parent) |
| 1007 | return Line.Last->TotalLength - State.NextToken->Parent->TotalLength; |
| 1008 | return 0; |
| 1009 | } |
| 1010 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1011 | FormatStyle Style; |
| 1012 | SourceManager &SourceMgr; |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1013 | const AnnotatedLine &Line; |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1014 | const unsigned FirstIndent; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1015 | const AnnotatedToken &RootToken; |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 1016 | WhitespaceManager &Whitespaces; |
Manuel Klimek | 62a48fb | 2013-02-13 10:54:19 +0000 | [diff] [blame] | 1017 | |
| 1018 | llvm::SpecificBumpPtrAllocator<StateNode> Allocator; |
| 1019 | QueueType Queue; |
| 1020 | // Increasing count of \c StateNode items we have created. This is used |
| 1021 | // to create a deterministic order independent of the container. |
| 1022 | unsigned Count; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1023 | }; |
| 1024 | |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1025 | class LexerBasedFormatTokenSource : public FormatTokenSource { |
| 1026 | public: |
| 1027 | LexerBasedFormatTokenSource(Lexer &Lex, SourceManager &SourceMgr) |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1028 | : GreaterStashed(false), Lex(Lex), SourceMgr(SourceMgr), |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1029 | IdentTable(Lex.getLangOpts()) { |
| 1030 | Lex.SetKeepWhitespaceMode(true); |
| 1031 | } |
| 1032 | |
| 1033 | virtual FormatToken getNextToken() { |
| 1034 | if (GreaterStashed) { |
| 1035 | FormatTok.NewlinesBefore = 0; |
| 1036 | FormatTok.WhiteSpaceStart = |
| 1037 | FormatTok.Tok.getLocation().getLocWithOffset(1); |
| 1038 | FormatTok.WhiteSpaceLength = 0; |
| 1039 | GreaterStashed = false; |
| 1040 | return FormatTok; |
| 1041 | } |
| 1042 | |
| 1043 | FormatTok = FormatToken(); |
| 1044 | Lex.LexFromRawLexer(FormatTok.Tok); |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 1045 | StringRef Text = rawTokenText(FormatTok.Tok); |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1046 | FormatTok.WhiteSpaceStart = FormatTok.Tok.getLocation(); |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 1047 | if (SourceMgr.getFileOffset(FormatTok.WhiteSpaceStart) == 0) |
| 1048 | FormatTok.IsFirst = true; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1049 | |
| 1050 | // Consume and record whitespace until we find a significant token. |
| 1051 | while (FormatTok.Tok.is(tok::unknown)) { |
Manuel Klimek | a28fc06 | 2013-02-11 12:33:24 +0000 | [diff] [blame] | 1052 | unsigned Newlines = Text.count('\n'); |
Daniel Jasper | 1eee6c4 | 2013-03-04 13:43:19 +0000 | [diff] [blame] | 1053 | if (Newlines > 0) |
| 1054 | FormatTok.LastNewlineOffset = |
| 1055 | FormatTok.WhiteSpaceLength + Text.rfind('\n') + 1; |
Manuel Klimek | a28fc06 | 2013-02-11 12:33:24 +0000 | [diff] [blame] | 1056 | unsigned EscapedNewlines = Text.count("\\\n"); |
| 1057 | FormatTok.NewlinesBefore += Newlines; |
| 1058 | FormatTok.HasUnescapedNewline |= EscapedNewlines != Newlines; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1059 | FormatTok.WhiteSpaceLength += FormatTok.Tok.getLength(); |
| 1060 | |
| 1061 | if (FormatTok.Tok.is(tok::eof)) |
| 1062 | return FormatTok; |
| 1063 | Lex.LexFromRawLexer(FormatTok.Tok); |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 1064 | Text = rawTokenText(FormatTok.Tok); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1065 | } |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 1066 | |
| 1067 | // Now FormatTok is the next non-whitespace token. |
| 1068 | FormatTok.TokenLength = Text.size(); |
| 1069 | |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 1070 | if (FormatTok.Tok.is(tok::comment)) { |
| 1071 | FormatTok.TrailingWhiteSpaceLength = Text.size() - Text.rtrim().size(); |
| 1072 | FormatTok.TokenLength -= FormatTok.TrailingWhiteSpaceLength; |
| 1073 | } |
| 1074 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1075 | // In case the token starts with escaped newlines, we want to |
| 1076 | // take them into account as whitespace - this pattern is quite frequent |
| 1077 | // in macro definitions. |
| 1078 | // FIXME: What do we want to do with other escaped spaces, and escaped |
| 1079 | // spaces or newlines in the middle of tokens? |
| 1080 | // FIXME: Add a more explicit test. |
| 1081 | unsigned i = 0; |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1082 | while (i + 1 < Text.size() && Text[i] == '\\' && Text[i + 1] == '\n') { |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1083 | // FIXME: ++FormatTok.NewlinesBefore is missing... |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1084 | FormatTok.WhiteSpaceLength += 2; |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 1085 | FormatTok.TokenLength -= 2; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1086 | i += 2; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | if (FormatTok.Tok.is(tok::raw_identifier)) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1090 | IdentifierInfo &Info = IdentTable.get(Text); |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 1091 | FormatTok.Tok.setIdentifierInfo(&Info); |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1092 | FormatTok.Tok.setKind(Info.getTokenID()); |
| 1093 | } |
| 1094 | |
| 1095 | if (FormatTok.Tok.is(tok::greatergreater)) { |
| 1096 | FormatTok.Tok.setKind(tok::greater); |
Daniel Jasper | b6f02f3 | 2013-02-28 10:06:05 +0000 | [diff] [blame] | 1097 | FormatTok.TokenLength = 1; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1098 | GreaterStashed = true; |
| 1099 | } |
| 1100 | |
| 1101 | return FormatTok; |
| 1102 | } |
| 1103 | |
Nico Weber | c2e6d2a | 2013-02-11 15:32:15 +0000 | [diff] [blame] | 1104 | IdentifierTable &getIdentTable() { return IdentTable; } |
| 1105 | |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1106 | private: |
| 1107 | FormatToken FormatTok; |
| 1108 | bool GreaterStashed; |
| 1109 | Lexer &Lex; |
| 1110 | SourceManager &SourceMgr; |
| 1111 | IdentifierTable IdentTable; |
| 1112 | |
| 1113 | /// Returns the text of \c FormatTok. |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 1114 | StringRef rawTokenText(Token &Tok) { |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1115 | return StringRef(SourceMgr.getCharacterData(Tok.getLocation()), |
| 1116 | Tok.getLength()); |
| 1117 | } |
| 1118 | }; |
| 1119 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1120 | class Formatter : public UnwrappedLineConsumer { |
| 1121 | public: |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1122 | Formatter(DiagnosticsEngine &Diag, const FormatStyle &Style, Lexer &Lex, |
| 1123 | SourceManager &SourceMgr, |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1124 | const std::vector<CharSourceRange> &Ranges) |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 1125 | : Diag(Diag), Style(Style), Lex(Lex), SourceMgr(SourceMgr), |
Alexander Kornienko | 052685c | 2013-03-19 17:41:36 +0000 | [diff] [blame] | 1126 | Whitespaces(SourceMgr, Style), Ranges(Ranges) {} |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1127 | |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 1128 | virtual ~Formatter() {} |
Daniel Jasper | accb0b0 | 2012-12-04 21:05:31 +0000 | [diff] [blame] | 1129 | |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1130 | tooling::Replacements format() { |
| 1131 | LexerBasedFormatTokenSource Tokens(Lex, SourceMgr); |
| 1132 | UnwrappedLineParser Parser(Diag, Style, Tokens, *this); |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 1133 | bool StructuralError = Parser.parse(); |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1134 | unsigned PreviousEndOfLineColumn = 0; |
| 1135 | TokenAnnotator Annotator(Style, SourceMgr, Lex, |
| 1136 | Tokens.getIdentTable().get("in")); |
| 1137 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
| 1138 | Annotator.annotate(AnnotatedLines[i]); |
| 1139 | } |
| 1140 | deriveLocalStyle(); |
| 1141 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
| 1142 | Annotator.calculateFormattingInformation(AnnotatedLines[i]); |
| 1143 | } |
Daniel Jasper | 5999f76 | 2013-04-09 17:46:55 +0000 | [diff] [blame] | 1144 | |
| 1145 | // Adapt level to the next line if this is a comment. |
| 1146 | // FIXME: Can/should this be done in the UnwrappedLineParser? |
Daniel Jasper | 1407bee | 2013-04-11 14:29:13 +0000 | [diff] [blame] | 1147 | const AnnotatedLine *NextNoneCommentLine = NULL; |
Daniel Jasper | 5999f76 | 2013-04-09 17:46:55 +0000 | [diff] [blame] | 1148 | for (unsigned i = AnnotatedLines.size() - 1; i > 0; --i) { |
| 1149 | if (NextNoneCommentLine && AnnotatedLines[i].First.is(tok::comment) && |
| 1150 | AnnotatedLines[i].First.Children.empty()) |
| 1151 | AnnotatedLines[i].Level = NextNoneCommentLine->Level; |
| 1152 | else |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 1153 | NextNoneCommentLine = |
| 1154 | AnnotatedLines[i].First.isNot(tok::r_brace) ? &AnnotatedLines[i] |
| 1155 | : NULL; |
Daniel Jasper | 5999f76 | 2013-04-09 17:46:55 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1158 | std::vector<int> IndentForLevel; |
| 1159 | bool PreviousLineWasTouched = false; |
Alexander Kornienko | 94b748f | 2013-03-27 17:08:02 +0000 | [diff] [blame] | 1160 | const AnnotatedToken *PreviousLineLastToken = 0; |
Daniel Jasper | 89b3a7f | 2013-05-10 13:00:49 +0000 | [diff] [blame] | 1161 | bool FormatPPDirective = false; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1162 | for (std::vector<AnnotatedLine>::iterator I = AnnotatedLines.begin(), |
| 1163 | E = AnnotatedLines.end(); |
| 1164 | I != E; ++I) { |
| 1165 | const AnnotatedLine &TheLine = *I; |
| 1166 | const FormatToken &FirstTok = TheLine.First.FormatTok; |
| 1167 | int Offset = getIndentOffset(TheLine.First); |
Daniel Jasper | 89b3a7f | 2013-05-10 13:00:49 +0000 | [diff] [blame] | 1168 | |
| 1169 | // Check whether this line is part of a formatted preprocessor directive. |
| 1170 | if (FirstTok.HasUnescapedNewline) |
| 1171 | FormatPPDirective = false; |
| 1172 | if (!FormatPPDirective && TheLine.InPPDirective && |
| 1173 | (touchesLine(TheLine) || touchesPPDirective(I + 1, E))) |
| 1174 | FormatPPDirective = true; |
| 1175 | |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1176 | while (IndentForLevel.size() <= TheLine.Level) |
| 1177 | IndentForLevel.push_back(-1); |
| 1178 | IndentForLevel.resize(TheLine.Level + 1); |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 1179 | bool WasMoved = PreviousLineWasTouched && FirstTok.NewlinesBefore == 0; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1180 | if (TheLine.First.is(tok::eof)) { |
| 1181 | if (PreviousLineWasTouched) { |
| 1182 | unsigned NewLines = std::min(FirstTok.NewlinesBefore, 1u); |
| 1183 | Whitespaces.replaceWhitespace(TheLine.First, NewLines, /*Indent*/ 0, |
Alexander Kornienko | 052685c | 2013-03-19 17:41:36 +0000 | [diff] [blame] | 1184 | /*WhitespaceStartColumn*/ 0); |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1185 | } |
| 1186 | } else if (TheLine.Type != LT_Invalid && |
Daniel Jasper | 89b3a7f | 2013-05-10 13:00:49 +0000 | [diff] [blame] | 1187 | (WasMoved || FormatPPDirective || touchesLine(TheLine))) { |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1188 | unsigned LevelIndent = getIndent(IndentForLevel, TheLine.Level); |
| 1189 | unsigned Indent = LevelIndent; |
| 1190 | if (static_cast<int>(Indent) + Offset >= 0) |
| 1191 | Indent += Offset; |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 1192 | if (FirstTok.WhiteSpaceStart.isValid() && |
| 1193 | // Insert a break even if there is a structural error in case where |
| 1194 | // we break apart a line consisting of multiple unwrapped lines. |
| 1195 | (FirstTok.NewlinesBefore == 0 || !StructuralError)) { |
Alexander Kornienko | 94b748f | 2013-03-27 17:08:02 +0000 | [diff] [blame] | 1196 | formatFirstToken(TheLine.First, PreviousLineLastToken, Indent, |
| 1197 | TheLine.InPPDirective, PreviousEndOfLineColumn); |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 1198 | } else { |
| 1199 | Indent = LevelIndent = |
| 1200 | SourceMgr.getSpellingColumnNumber(FirstTok.Tok.getLocation()) - 1; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1201 | } |
| 1202 | tryFitMultipleLinesInOne(Indent, I, E); |
| 1203 | UnwrappedLineFormatter Formatter(Style, SourceMgr, TheLine, Indent, |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 1204 | TheLine.First, Whitespaces); |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1205 | PreviousEndOfLineColumn = |
| 1206 | Formatter.format(I + 1 != E ? &*(I + 1) : NULL); |
| 1207 | IndentForLevel[TheLine.Level] = LevelIndent; |
| 1208 | PreviousLineWasTouched = true; |
| 1209 | } else { |
| 1210 | if (FirstTok.NewlinesBefore > 0 || FirstTok.IsFirst) { |
| 1211 | unsigned Indent = |
| 1212 | SourceMgr.getSpellingColumnNumber(FirstTok.Tok.getLocation()) - 1; |
| 1213 | unsigned LevelIndent = Indent; |
| 1214 | if (static_cast<int>(LevelIndent) - Offset >= 0) |
| 1215 | LevelIndent -= Offset; |
Daniel Jasper | 83a90e5 | 2013-03-20 14:31:47 +0000 | [diff] [blame] | 1216 | if (TheLine.First.isNot(tok::comment)) |
| 1217 | IndentForLevel[TheLine.Level] = LevelIndent; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1218 | |
| 1219 | // Remove trailing whitespace of the previous line if it was touched. |
| 1220 | if (PreviousLineWasTouched || touchesEmptyLineBefore(TheLine)) |
Alexander Kornienko | 94b748f | 2013-03-27 17:08:02 +0000 | [diff] [blame] | 1221 | formatFirstToken(TheLine.First, PreviousLineLastToken, Indent, |
| 1222 | TheLine.InPPDirective, PreviousEndOfLineColumn); |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1223 | } |
| 1224 | // If we did not reformat this unwrapped line, the column at the end of |
| 1225 | // the last token is unchanged - thus, we can calculate the end of the |
| 1226 | // last token. |
| 1227 | SourceLocation LastLoc = TheLine.Last->FormatTok.Tok.getLocation(); |
| 1228 | PreviousEndOfLineColumn = |
| 1229 | SourceMgr.getSpellingColumnNumber(LastLoc) + |
| 1230 | Lex.MeasureTokenLength(LastLoc, SourceMgr, Lex.getLangOpts()) - 1; |
| 1231 | PreviousLineWasTouched = false; |
Daniel Jasper | c363dbb | 2013-03-22 16:25:51 +0000 | [diff] [blame] | 1232 | if (TheLine.Last->is(tok::comment)) |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 1233 | Whitespaces.addUntouchableComment( |
| 1234 | SourceMgr.getSpellingColumnNumber( |
| 1235 | TheLine.Last->FormatTok.Tok.getLocation()) - |
| 1236 | 1); |
Daniel Jasper | af84976 | 2013-04-24 06:33:59 +0000 | [diff] [blame] | 1237 | else |
| 1238 | Whitespaces.alignComments(); |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1239 | } |
Alexander Kornienko | 94b748f | 2013-03-27 17:08:02 +0000 | [diff] [blame] | 1240 | PreviousLineLastToken = I->Last; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1241 | } |
| 1242 | return Whitespaces.generateReplacements(); |
| 1243 | } |
| 1244 | |
| 1245 | private: |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1246 | void deriveLocalStyle() { |
| 1247 | unsigned CountBoundToVariable = 0; |
| 1248 | unsigned CountBoundToType = 0; |
| 1249 | bool HasCpp03IncompatibleFormat = false; |
| 1250 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
| 1251 | if (AnnotatedLines[i].First.Children.empty()) |
| 1252 | continue; |
| 1253 | AnnotatedToken *Tok = &AnnotatedLines[i].First.Children[0]; |
| 1254 | while (!Tok->Children.empty()) { |
| 1255 | if (Tok->Type == TT_PointerOrReference) { |
| 1256 | bool SpacesBefore = Tok->FormatTok.WhiteSpaceLength > 0; |
| 1257 | bool SpacesAfter = Tok->Children[0].FormatTok.WhiteSpaceLength > 0; |
| 1258 | if (SpacesBefore && !SpacesAfter) |
| 1259 | ++CountBoundToVariable; |
| 1260 | else if (!SpacesBefore && SpacesAfter) |
| 1261 | ++CountBoundToType; |
| 1262 | } |
| 1263 | |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 1264 | if (Tok->Type == TT_TemplateCloser && |
| 1265 | Tok->Parent->Type == TT_TemplateCloser && |
| 1266 | Tok->FormatTok.WhiteSpaceLength == 0) |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1267 | HasCpp03IncompatibleFormat = true; |
| 1268 | Tok = &Tok->Children[0]; |
| 1269 | } |
| 1270 | } |
| 1271 | if (Style.DerivePointerBinding) { |
| 1272 | if (CountBoundToType > CountBoundToVariable) |
| 1273 | Style.PointerBindsToType = true; |
| 1274 | else if (CountBoundToType < CountBoundToVariable) |
| 1275 | Style.PointerBindsToType = false; |
| 1276 | } |
| 1277 | if (Style.Standard == FormatStyle::LS_Auto) { |
| 1278 | Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11 |
| 1279 | : FormatStyle::LS_Cpp03; |
| 1280 | } |
| 1281 | } |
| 1282 | |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 1283 | /// \brief Get the indent of \p Level from \p IndentForLevel. |
| 1284 | /// |
| 1285 | /// \p IndentForLevel must contain the indent for the level \c l |
| 1286 | /// at \p IndentForLevel[l], or a value < 0 if the indent for |
| 1287 | /// that level is unknown. |
Daniel Jasper | fc75908 | 2013-02-14 14:26:07 +0000 | [diff] [blame] | 1288 | unsigned getIndent(const std::vector<int> IndentForLevel, unsigned Level) { |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 1289 | if (IndentForLevel[Level] != -1) |
| 1290 | return IndentForLevel[Level]; |
Manuel Klimek | 52635ff | 2013-02-08 19:53:32 +0000 | [diff] [blame] | 1291 | if (Level == 0) |
| 1292 | return 0; |
Manuel Klimek | 07a64ec | 2013-05-13 08:42:42 +0000 | [diff] [blame] | 1293 | return getIndent(IndentForLevel, Level - 1) + Style.IndentWidth; |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | /// \brief Get the offset of the line relatively to the level. |
| 1297 | /// |
| 1298 | /// For example, 'public:' labels in classes are offset by 1 or 2 |
| 1299 | /// characters to the left from their level. |
Daniel Jasper | c78c6b3 | 2013-02-14 09:58:41 +0000 | [diff] [blame] | 1300 | int getIndentOffset(const AnnotatedToken &RootToken) { |
Alexander Kornienko | 94b748f | 2013-03-27 17:08:02 +0000 | [diff] [blame] | 1301 | if (RootToken.isAccessSpecifier(false) || RootToken.isObjCAccessSpecifier()) |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 1302 | return Style.AccessModifierOffset; |
| 1303 | return 0; |
| 1304 | } |
| 1305 | |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1306 | /// \brief Tries to merge lines into one. |
| 1307 | /// |
| 1308 | /// This will change \c Line and \c AnnotatedLine to contain the merged line, |
| 1309 | /// if possible; note that \c I will be incremented when lines are merged. |
| 1310 | /// |
| 1311 | /// Returns whether the resulting \c Line can fit in a single line. |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1312 | void tryFitMultipleLinesInOne(unsigned Indent, |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1313 | std::vector<AnnotatedLine>::iterator &I, |
| 1314 | std::vector<AnnotatedLine>::iterator E) { |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1315 | // We can never merge stuff if there are trailing line comments. |
| 1316 | if (I->Last->Type == TT_LineComment) |
| 1317 | return; |
| 1318 | |
Daniel Jasper | a4d4621 | 2013-02-28 11:05:57 +0000 | [diff] [blame] | 1319 | unsigned Limit = Style.ColumnLimit - Indent; |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1320 | // If we already exceed the column limit, we set 'Limit' to 0. The different |
| 1321 | // tryMerge..() functions can then decide whether to still do merging. |
| 1322 | Limit = I->Last->TotalLength > Limit ? 0 : Limit - I->Last->TotalLength; |
Daniel Jasper | 55b08e7 | 2013-01-16 07:02:34 +0000 | [diff] [blame] | 1323 | |
Daniel Jasper | 9c8c40e | 2013-01-21 14:18:28 +0000 | [diff] [blame] | 1324 | if (I + 1 == E || (I + 1)->Type == LT_Invalid) |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1325 | return; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1326 | |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1327 | if (I->Last->is(tok::l_brace)) { |
| 1328 | tryMergeSimpleBlock(I, E, Limit); |
| 1329 | } else if (I->First.is(tok::kw_if)) { |
| 1330 | tryMergeSimpleIf(I, E, Limit); |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1331 | } else if (I->InPPDirective && (I->First.FormatTok.HasUnescapedNewline || |
| 1332 | I->First.FormatTok.IsFirst)) { |
| 1333 | tryMergeSimplePPDirective(I, E, Limit); |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1334 | } |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1335 | return; |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1338 | void tryMergeSimplePPDirective(std::vector<AnnotatedLine>::iterator &I, |
| 1339 | std::vector<AnnotatedLine>::iterator E, |
| 1340 | unsigned Limit) { |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1341 | if (Limit == 0) |
| 1342 | return; |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1343 | AnnotatedLine &Line = *I; |
Daniel Jasper | 2b9c10b | 2013-01-14 15:52:06 +0000 | [diff] [blame] | 1344 | if (!(I + 1)->InPPDirective || (I + 1)->First.FormatTok.HasUnescapedNewline) |
| 1345 | return; |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1346 | if (I + 2 != E && (I + 2)->InPPDirective && |
| 1347 | !(I + 2)->First.FormatTok.HasUnescapedNewline) |
| 1348 | return; |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1349 | if (1 + (I + 1)->Last->TotalLength > Limit) |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1350 | return; |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1351 | join(Line, *(++I)); |
| 1352 | } |
| 1353 | |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1354 | void tryMergeSimpleIf(std::vector<AnnotatedLine>::iterator &I, |
| 1355 | std::vector<AnnotatedLine>::iterator E, |
| 1356 | unsigned Limit) { |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1357 | if (Limit == 0) |
| 1358 | return; |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 1359 | if (!Style.AllowShortIfStatementsOnASingleLine) |
| 1360 | return; |
Manuel Klimek | 4c12812 | 2013-01-18 14:46:43 +0000 | [diff] [blame] | 1361 | if ((I + 1)->InPPDirective != I->InPPDirective || |
| 1362 | ((I + 1)->InPPDirective && |
| 1363 | (I + 1)->First.FormatTok.HasUnescapedNewline)) |
| 1364 | return; |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1365 | AnnotatedLine &Line = *I; |
Daniel Jasper | 55b08e7 | 2013-01-16 07:02:34 +0000 | [diff] [blame] | 1366 | if (Line.Last->isNot(tok::r_paren)) |
| 1367 | return; |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1368 | if (1 + (I + 1)->Last->TotalLength > Limit) |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1369 | return; |
| 1370 | if ((I + 1)->First.is(tok::kw_if) || (I + 1)->First.Type == TT_LineComment) |
| 1371 | return; |
| 1372 | // Only inline simple if's (no nested if or else). |
| 1373 | if (I + 2 != E && (I + 2)->First.is(tok::kw_else)) |
| 1374 | return; |
| 1375 | join(Line, *(++I)); |
| 1376 | } |
| 1377 | |
| 1378 | void tryMergeSimpleBlock(std::vector<AnnotatedLine>::iterator &I, |
Daniel Jasper | 1a1ce83 | 2013-01-29 11:27:30 +0000 | [diff] [blame] | 1379 | std::vector<AnnotatedLine>::iterator E, |
| 1380 | unsigned Limit) { |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1381 | // First, check that the current line allows merging. This is the case if |
| 1382 | // we're not in a control flow statement and the last token is an opening |
| 1383 | // brace. |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1384 | AnnotatedLine &Line = *I; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1385 | if (Line.First.isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::r_brace, |
| 1386 | tok::kw_else, tok::kw_try, tok::kw_catch, |
| 1387 | tok::kw_for, |
| 1388 | // This gets rid of all ObjC @ keywords and methods. |
| 1389 | tok::at, tok::minus, tok::plus)) |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1390 | return; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1391 | |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1392 | AnnotatedToken *Tok = &(I + 1)->First; |
| 1393 | if (Tok->Children.empty() && Tok->is(tok::r_brace) && |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1394 | !Tok->MustBreakBefore) { |
| 1395 | // We merge empty blocks even if the line exceeds the column limit. |
Daniel Jasper | 729a743 | 2013-02-11 12:36:37 +0000 | [diff] [blame] | 1396 | Tok->SpacesRequiredBefore = 0; |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1397 | Tok->CanBreakBefore = true; |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1398 | join(Line, *(I + 1)); |
| 1399 | I += 1; |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1400 | } else if (Limit != 0) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1401 | // Check that we still have three lines and they fit into the limit. |
| 1402 | if (I + 2 == E || (I + 2)->Type == LT_Invalid || |
| 1403 | !nextTwoLinesFitInto(I, Limit)) |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1404 | return; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1405 | |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1406 | // Second, check that the next line does not contain any braces - if it |
| 1407 | // does, readability declines when putting it into a single line. |
| 1408 | if ((I + 1)->Last->Type == TT_LineComment || Tok->MustBreakBefore) |
| 1409 | return; |
| 1410 | do { |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1411 | if (Tok->isOneOf(tok::l_brace, tok::r_brace)) |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1412 | return; |
| 1413 | Tok = Tok->Children.empty() ? NULL : &Tok->Children.back(); |
| 1414 | } while (Tok != NULL); |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1415 | |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1416 | // Last, check that the third line contains a single closing brace. |
| 1417 | Tok = &(I + 2)->First; |
| 1418 | if (!Tok->Children.empty() || Tok->isNot(tok::r_brace) || |
| 1419 | Tok->MustBreakBefore) |
| 1420 | return; |
| 1421 | |
| 1422 | join(Line, *(I + 1)); |
| 1423 | join(Line, *(I + 2)); |
| 1424 | I += 2; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1425 | } |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | bool nextTwoLinesFitInto(std::vector<AnnotatedLine>::iterator I, |
| 1429 | unsigned Limit) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1430 | return 1 + (I + 1)->Last->TotalLength + 1 + (I + 2)->Last->TotalLength <= |
| 1431 | Limit; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1434 | void join(AnnotatedLine &A, const AnnotatedLine &B) { |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1435 | unsigned LengthA = A.Last->TotalLength + B.First.SpacesRequiredBefore; |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1436 | A.Last->Children.push_back(B.First); |
| 1437 | while (!A.Last->Children.empty()) { |
| 1438 | A.Last->Children[0].Parent = A.Last; |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1439 | A.Last->Children[0].TotalLength += LengthA; |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1440 | A.Last = &A.Last->Children[0]; |
| 1441 | } |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
Daniel Jasper | 6f21a98 | 2013-03-13 07:49:51 +0000 | [diff] [blame] | 1444 | bool touchesRanges(const CharSourceRange &Range) { |
Daniel Jasper | f302354 | 2013-03-07 20:50:00 +0000 | [diff] [blame] | 1445 | for (unsigned i = 0, e = Ranges.size(); i != e; ++i) { |
| 1446 | if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), |
| 1447 | Ranges[i].getBegin()) && |
| 1448 | !SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(), |
| 1449 | Range.getBegin())) |
| 1450 | return true; |
| 1451 | } |
| 1452 | return false; |
| 1453 | } |
| 1454 | |
| 1455 | bool touchesLine(const AnnotatedLine &TheLine) { |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1456 | const FormatToken *First = &TheLine.First.FormatTok; |
| 1457 | const FormatToken *Last = &TheLine.Last->FormatTok; |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1458 | CharSourceRange LineRange = CharSourceRange::getTokenRange( |
Daniel Jasper | 1eee6c4 | 2013-03-04 13:43:19 +0000 | [diff] [blame] | 1459 | First->WhiteSpaceStart.getLocWithOffset(First->LastNewlineOffset), |
| 1460 | Last->Tok.getLocation()); |
Daniel Jasper | f302354 | 2013-03-07 20:50:00 +0000 | [diff] [blame] | 1461 | return touchesRanges(LineRange); |
| 1462 | } |
| 1463 | |
Daniel Jasper | 89b3a7f | 2013-05-10 13:00:49 +0000 | [diff] [blame] | 1464 | bool touchesPPDirective(std::vector<AnnotatedLine>::iterator I, |
| 1465 | std::vector<AnnotatedLine>::iterator E) { |
| 1466 | for (; I != E; ++I) { |
| 1467 | if (I->First.FormatTok.HasUnescapedNewline) |
| 1468 | return false; |
| 1469 | if (touchesLine(*I)) |
| 1470 | return true; |
| 1471 | } |
| 1472 | return false; |
| 1473 | } |
| 1474 | |
Daniel Jasper | f302354 | 2013-03-07 20:50:00 +0000 | [diff] [blame] | 1475 | bool touchesEmptyLineBefore(const AnnotatedLine &TheLine) { |
| 1476 | const FormatToken *First = &TheLine.First.FormatTok; |
| 1477 | CharSourceRange LineRange = CharSourceRange::getCharRange( |
| 1478 | First->WhiteSpaceStart, |
| 1479 | First->WhiteSpaceStart.getLocWithOffset(First->LastNewlineOffset)); |
| 1480 | return touchesRanges(LineRange); |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) { |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 1484 | AnnotatedLines.push_back(AnnotatedLine(TheLine)); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1487 | /// \brief Add a new line and the required indent before the first Token |
| 1488 | /// of the \c UnwrappedLine if there was no structural parsing error. |
| 1489 | /// Returns the indent level of the \c UnwrappedLine. |
Alexander Kornienko | 94b748f | 2013-03-27 17:08:02 +0000 | [diff] [blame] | 1490 | void formatFirstToken(const AnnotatedToken &RootToken, |
| 1491 | const AnnotatedToken *PreviousToken, unsigned Indent, |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 1492 | bool InPPDirective, unsigned PreviousEndOfLineColumn) { |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 1493 | const FormatToken &Tok = RootToken.FormatTok; |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1494 | |
Daniel Jasper | 1a1ce83 | 2013-01-29 11:27:30 +0000 | [diff] [blame] | 1495 | unsigned Newlines = |
| 1496 | std::min(Tok.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1); |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1497 | if (Newlines == 0 && !Tok.IsFirst) |
| 1498 | Newlines = 1; |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1499 | |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1500 | if (!InPPDirective || Tok.HasUnescapedNewline) { |
Alexander Kornienko | 94b748f | 2013-03-27 17:08:02 +0000 | [diff] [blame] | 1501 | // Insert extra new line before access specifiers. |
| 1502 | if (PreviousToken && PreviousToken->isOneOf(tok::semi, tok::r_brace) && |
| 1503 | RootToken.isAccessSpecifier() && Tok.NewlinesBefore == 1) |
| 1504 | ++Newlines; |
| 1505 | |
Alexander Kornienko | 052685c | 2013-03-19 17:41:36 +0000 | [diff] [blame] | 1506 | Whitespaces.replaceWhitespace(RootToken, Newlines, Indent, 0); |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1507 | } else { |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 1508 | Whitespaces.replacePPWhitespace(RootToken, Newlines, Indent, |
Alexander Kornienko | 052685c | 2013-03-19 17:41:36 +0000 | [diff] [blame] | 1509 | PreviousEndOfLineColumn); |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1510 | } |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
Alexander Kornienko | a4ae9f3 | 2013-01-14 11:34:14 +0000 | [diff] [blame] | 1513 | DiagnosticsEngine &Diag; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1514 | FormatStyle Style; |
| 1515 | Lexer &Lex; |
| 1516 | SourceManager &SourceMgr; |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 1517 | WhitespaceManager Whitespaces; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1518 | std::vector<CharSourceRange> Ranges; |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1519 | std::vector<AnnotatedLine> AnnotatedLines; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1520 | }; |
| 1521 | |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 1522 | tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex, |
| 1523 | SourceManager &SourceMgr, |
| 1524 | std::vector<CharSourceRange> Ranges, |
| 1525 | DiagnosticConsumer *DiagClient) { |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 1526 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); |
Alexander Kornienko | a4ae9f3 | 2013-01-14 11:34:14 +0000 | [diff] [blame] | 1527 | OwningPtr<DiagnosticConsumer> DiagPrinter; |
| 1528 | if (DiagClient == 0) { |
| 1529 | DiagPrinter.reset(new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts)); |
| 1530 | DiagPrinter->BeginSourceFile(Lex.getLangOpts(), Lex.getPP()); |
| 1531 | DiagClient = DiagPrinter.get(); |
| 1532 | } |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 1533 | DiagnosticsEngine Diagnostics( |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1534 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts, |
Alexander Kornienko | a4ae9f3 | 2013-01-14 11:34:14 +0000 | [diff] [blame] | 1535 | DiagClient, false); |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 1536 | Diagnostics.setSourceManager(&SourceMgr); |
| 1537 | Formatter formatter(Diagnostics, Style, Lex, SourceMgr, Ranges); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1538 | return formatter.format(); |
| 1539 | } |
| 1540 | |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1541 | LangOptions getFormattingLangOpts() { |
| 1542 | LangOptions LangOpts; |
| 1543 | LangOpts.CPlusPlus = 1; |
| 1544 | LangOpts.CPlusPlus11 = 1; |
Daniel Jasper | b64eca0 | 2013-03-22 10:01:29 +0000 | [diff] [blame] | 1545 | LangOpts.LineComment = 1; |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1546 | LangOpts.Bool = 1; |
| 1547 | LangOpts.ObjC1 = 1; |
| 1548 | LangOpts.ObjC2 = 1; |
| 1549 | return LangOpts; |
| 1550 | } |
| 1551 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1552 | } // namespace format |
| 1553 | } // namespace clang |