Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1 | //===--- Format.cpp - Format C++ code -------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// \brief This file implements functions declared in Format.h. This will be |
| 12 | /// split into separate files as we go. |
| 13 | /// |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 16 | #define DEBUG_TYPE "format-formatter" |
| 17 | |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 18 | #include "ContinuationIndenter.h" |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 19 | #include "TokenAnnotator.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "UnwrappedLineParser.h" |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 21 | #include "WhitespaceManager.h" |
Daniel Jasper | 8a99945 | 2013-05-16 10:40:07 +0000 | [diff] [blame] | 22 | #include "clang/Basic/Diagnostic.h" |
Chandler Carruth | b99083e | 2013-01-02 10:28:36 +0000 | [diff] [blame] | 23 | #include "clang/Basic/SourceManager.h" |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 24 | #include "clang/Format/Format.h" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 25 | #include "clang/Lex/Lexer.h" |
Alexander Kornienko | 5262dd9 | 2013-03-27 11:52:18 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/STLExtras.h" |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Allocator.h" |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Debug.h" |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 29 | #include "llvm/Support/YAMLTraits.h" |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 30 | #include <queue> |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 31 | #include <string> |
| 32 | |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 33 | namespace llvm { |
| 34 | namespace yaml { |
| 35 | template <> |
| 36 | struct ScalarEnumerationTraits<clang::format::FormatStyle::LanguageStandard> { |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 37 | static void enumeration(IO &IO, |
| 38 | clang::format::FormatStyle::LanguageStandard &Value) { |
Alexander Kornienko | 9321e87 | 2013-09-04 14:09:13 +0000 | [diff] [blame] | 39 | IO.enumCase(Value, "Cpp03", clang::format::FormatStyle::LS_Cpp03); |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 40 | IO.enumCase(Value, "C++03", clang::format::FormatStyle::LS_Cpp03); |
Alexander Kornienko | 9321e87 | 2013-09-04 14:09:13 +0000 | [diff] [blame] | 41 | IO.enumCase(Value, "Cpp11", clang::format::FormatStyle::LS_Cpp11); |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 42 | IO.enumCase(Value, "C++11", clang::format::FormatStyle::LS_Cpp11); |
| 43 | IO.enumCase(Value, "Auto", clang::format::FormatStyle::LS_Auto); |
| 44 | } |
| 45 | }; |
| 46 | |
Daniel Jasper | 1fb8d88 | 2013-05-14 09:30:02 +0000 | [diff] [blame] | 47 | template <> |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 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); |
Manuel Klimek | e490705 | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 54 | IO.enumCase(Value, "Allman", clang::format::FormatStyle::BS_Allman); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 55 | } |
| 56 | }; |
| 57 | |
Daniel Jasper | eff18b9 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 58 | template <> |
| 59 | struct ScalarEnumerationTraits< |
| 60 | clang::format::FormatStyle::NamespaceIndentationKind> { |
| 61 | static void |
| 62 | enumeration(IO &IO, |
| 63 | clang::format::FormatStyle::NamespaceIndentationKind &Value) { |
| 64 | IO.enumCase(Value, "None", clang::format::FormatStyle::NI_None); |
| 65 | IO.enumCase(Value, "Inner", clang::format::FormatStyle::NI_Inner); |
| 66 | IO.enumCase(Value, "All", clang::format::FormatStyle::NI_All); |
| 67 | } |
| 68 | }; |
| 69 | |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 70 | template <> struct MappingTraits<clang::format::FormatStyle> { |
| 71 | static void mapping(llvm::yaml::IO &IO, clang::format::FormatStyle &Style) { |
Alexander Kornienko | dd25631 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 72 | if (IO.outputting()) { |
Alexander Kornienko | 4e65c98 | 2013-09-02 16:39:23 +0000 | [diff] [blame] | 73 | StringRef StylesArray[] = { "LLVM", "Google", "Chromium", |
| 74 | "Mozilla", "WebKit" }; |
Alexander Kornienko | dd25631 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 75 | ArrayRef<StringRef> Styles(StylesArray); |
| 76 | for (size_t i = 0, e = Styles.size(); i < e; ++i) { |
| 77 | StringRef StyleName(Styles[i]); |
Alexander Kornienko | 885f87b | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 78 | clang::format::FormatStyle PredefinedStyle; |
| 79 | if (clang::format::getPredefinedStyle(StyleName, &PredefinedStyle) && |
| 80 | Style == PredefinedStyle) { |
Alexander Kornienko | dd25631 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 81 | IO.mapOptional("# BasedOnStyle", StyleName); |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | } else { |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 86 | StringRef BasedOnStyle; |
| 87 | IO.mapOptional("BasedOnStyle", BasedOnStyle); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 88 | if (!BasedOnStyle.empty()) |
Alexander Kornienko | 885f87b | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 89 | if (!clang::format::getPredefinedStyle(BasedOnStyle, &Style)) { |
| 90 | IO.setError(Twine("Unknown value for BasedOnStyle: ", BasedOnStyle)); |
| 91 | return; |
| 92 | } |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset); |
Daniel Jasper | 6315fec | 2013-08-13 10:58:30 +0000 | [diff] [blame] | 96 | IO.mapOptional("ConstructorInitializerIndentWidth", |
| 97 | Style.ConstructorInitializerIndentWidth); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 98 | IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft); |
Daniel Jasper | 893ea8d | 2013-07-31 23:55:15 +0000 | [diff] [blame] | 99 | IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 100 | IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine", |
| 101 | Style.AllowAllParametersOfDeclarationOnNextLine); |
| 102 | IO.mapOptional("AllowShortIfStatementsOnASingleLine", |
| 103 | Style.AllowShortIfStatementsOnASingleLine); |
Daniel Jasper | f11bbb9 | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 104 | IO.mapOptional("AllowShortLoopsOnASingleLine", |
| 105 | Style.AllowShortLoopsOnASingleLine); |
Daniel Jasper | bbc8776 | 2013-05-29 12:07:31 +0000 | [diff] [blame] | 106 | IO.mapOptional("AlwaysBreakTemplateDeclarations", |
| 107 | Style.AlwaysBreakTemplateDeclarations); |
Alexander Kornienko | 5631202 | 2013-07-04 12:02:44 +0000 | [diff] [blame] | 108 | IO.mapOptional("AlwaysBreakBeforeMultilineStrings", |
| 109 | Style.AlwaysBreakBeforeMultilineStrings); |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 110 | IO.mapOptional("BreakBeforeBinaryOperators", |
| 111 | Style.BreakBeforeBinaryOperators); |
| 112 | IO.mapOptional("BreakConstructorInitializersBeforeComma", |
| 113 | Style.BreakConstructorInitializersBeforeComma); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 114 | IO.mapOptional("BinPackParameters", Style.BinPackParameters); |
| 115 | IO.mapOptional("ColumnLimit", Style.ColumnLimit); |
| 116 | IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine", |
| 117 | Style.ConstructorInitializerAllOnOneLineOrOnePerLine); |
| 118 | IO.mapOptional("DerivePointerBinding", Style.DerivePointerBinding); |
Daniel Jasper | c7bd68f | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 119 | IO.mapOptional("ExperimentalAutoDetectBinPacking", |
| 120 | Style.ExperimentalAutoDetectBinPacking); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 121 | IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels); |
| 122 | IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep); |
Daniel Jasper | eff18b9 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 123 | IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 124 | IO.mapOptional("ObjCSpaceBeforeProtocolList", |
| 125 | Style.ObjCSpaceBeforeProtocolList); |
Alexander Kornienko | 2785b9a | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 126 | IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment); |
| 127 | IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString); |
Daniel Jasper | faec47b | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 128 | IO.mapOptional("PenaltyBreakFirstLessLess", |
| 129 | Style.PenaltyBreakFirstLessLess); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 130 | IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter); |
| 131 | IO.mapOptional("PenaltyReturnTypeOnItsOwnLine", |
| 132 | Style.PenaltyReturnTypeOnItsOwnLine); |
| 133 | IO.mapOptional("PointerBindsToType", Style.PointerBindsToType); |
| 134 | IO.mapOptional("SpacesBeforeTrailingComments", |
| 135 | Style.SpacesBeforeTrailingComments); |
Daniel Jasper | b5dc3f4 | 2013-07-16 18:22:10 +0000 | [diff] [blame] | 136 | IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 137 | IO.mapOptional("Standard", Style.Standard); |
Manuel Klimek | 07a64ec | 2013-05-13 08:42:42 +0000 | [diff] [blame] | 138 | IO.mapOptional("IndentWidth", Style.IndentWidth); |
Manuel Klimek | 7c9a93e | 2013-05-13 09:22:11 +0000 | [diff] [blame] | 139 | IO.mapOptional("UseTab", Style.UseTab); |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 140 | IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces); |
Manuel Klimek | a9a7f10 | 2013-06-21 17:25:42 +0000 | [diff] [blame] | 141 | IO.mapOptional("IndentFunctionDeclarationAfterType", |
| 142 | Style.IndentFunctionDeclarationAfterType); |
Daniel Jasper | 7df56bf | 2013-08-20 12:36:34 +0000 | [diff] [blame] | 143 | IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses); |
Daniel Jasper | 34f3d05 | 2013-08-21 08:39:01 +0000 | [diff] [blame] | 144 | IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses); |
Daniel Jasper | 7df56bf | 2013-08-20 12:36:34 +0000 | [diff] [blame] | 145 | IO.mapOptional("SpacesInCStyleCastParentheses", |
| 146 | Style.SpacesInCStyleCastParentheses); |
| 147 | IO.mapOptional("SpaceAfterControlStatementKeyword", |
| 148 | Style.SpaceAfterControlStatementKeyword); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 149 | } |
| 150 | }; |
| 151 | } |
| 152 | } |
| 153 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 154 | namespace clang { |
| 155 | namespace format { |
| 156 | |
Daniel Jasper | faec47b | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 157 | void setDefaultPenalties(FormatStyle &Style) { |
Daniel Jasper | f546178 | 2013-08-28 10:03:58 +0000 | [diff] [blame] | 158 | Style.PenaltyBreakComment = 60; |
Daniel Jasper | 9637dda | 2013-07-15 14:33:14 +0000 | [diff] [blame] | 159 | Style.PenaltyBreakFirstLessLess = 120; |
Daniel Jasper | faec47b | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 160 | Style.PenaltyBreakString = 1000; |
| 161 | Style.PenaltyExcessCharacter = 1000000; |
| 162 | } |
| 163 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 164 | FormatStyle getLLVMStyle() { |
| 165 | FormatStyle LLVMStyle; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 166 | LLVMStyle.AccessModifierOffset = -2; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 167 | LLVMStyle.AlignEscapedNewlinesLeft = false; |
Daniel Jasper | 893ea8d | 2013-07-31 23:55:15 +0000 | [diff] [blame] | 168 | LLVMStyle.AlignTrailingComments = true; |
Daniel Jasper | f157960 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 169 | LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true; |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 170 | LLVMStyle.AllowShortIfStatementsOnASingleLine = false; |
Daniel Jasper | f11bbb9 | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 171 | LLVMStyle.AllowShortLoopsOnASingleLine = false; |
Alexander Kornienko | 5631202 | 2013-07-04 12:02:44 +0000 | [diff] [blame] | 172 | LLVMStyle.AlwaysBreakBeforeMultilineStrings = false; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 173 | LLVMStyle.AlwaysBreakTemplateDeclarations = false; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 174 | LLVMStyle.BinPackParameters = true; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 175 | LLVMStyle.BreakBeforeBinaryOperators = false; |
| 176 | LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach; |
| 177 | LLVMStyle.BreakConstructorInitializersBeforeComma = false; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 178 | LLVMStyle.ColumnLimit = 80; |
| 179 | LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false; |
Daniel Jasper | 6315fec | 2013-08-13 10:58:30 +0000 | [diff] [blame] | 180 | LLVMStyle.ConstructorInitializerIndentWidth = 4; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 181 | LLVMStyle.Cpp11BracedListStyle = false; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 182 | LLVMStyle.DerivePointerBinding = false; |
Daniel Jasper | c7bd68f | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 183 | LLVMStyle.ExperimentalAutoDetectBinPacking = false; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 184 | LLVMStyle.IndentCaseLabels = false; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 185 | LLVMStyle.IndentFunctionDeclarationAfterType = false; |
| 186 | LLVMStyle.IndentWidth = 2; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 187 | LLVMStyle.MaxEmptyLinesToKeep = 1; |
Daniel Jasper | eff18b9 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 188 | LLVMStyle.NamespaceIndentation = FormatStyle::NI_None; |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 189 | LLVMStyle.ObjCSpaceBeforeProtocolList = true; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 190 | LLVMStyle.PointerBindsToType = false; |
| 191 | LLVMStyle.SpacesBeforeTrailingComments = 1; |
| 192 | LLVMStyle.Standard = FormatStyle::LS_Cpp03; |
Manuel Klimek | 7c9a93e | 2013-05-13 09:22:11 +0000 | [diff] [blame] | 193 | LLVMStyle.UseTab = false; |
Daniel Jasper | 7df56bf | 2013-08-20 12:36:34 +0000 | [diff] [blame] | 194 | LLVMStyle.SpacesInParentheses = false; |
| 195 | LLVMStyle.SpaceInEmptyParentheses = false; |
| 196 | LLVMStyle.SpacesInCStyleCastParentheses = false; |
| 197 | LLVMStyle.SpaceAfterControlStatementKeyword = true; |
Daniel Jasper | faec47b | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 198 | |
| 199 | setDefaultPenalties(LLVMStyle); |
| 200 | LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60; |
| 201 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 202 | return LLVMStyle; |
| 203 | } |
| 204 | |
| 205 | FormatStyle getGoogleStyle() { |
| 206 | FormatStyle GoogleStyle; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 207 | GoogleStyle.AccessModifierOffset = -1; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 208 | GoogleStyle.AlignEscapedNewlinesLeft = true; |
Daniel Jasper | 893ea8d | 2013-07-31 23:55:15 +0000 | [diff] [blame] | 209 | GoogleStyle.AlignTrailingComments = true; |
Daniel Jasper | f157960 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 210 | GoogleStyle.AllowAllParametersOfDeclarationOnNextLine = true; |
Daniel Jasper | 94d6ad7 | 2013-04-24 13:46:00 +0000 | [diff] [blame] | 211 | GoogleStyle.AllowShortIfStatementsOnASingleLine = true; |
Daniel Jasper | 1bee073 | 2013-05-23 18:05:18 +0000 | [diff] [blame] | 212 | GoogleStyle.AllowShortLoopsOnASingleLine = true; |
Alexander Kornienko | 5631202 | 2013-07-04 12:02:44 +0000 | [diff] [blame] | 213 | GoogleStyle.AlwaysBreakBeforeMultilineStrings = true; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 214 | GoogleStyle.AlwaysBreakTemplateDeclarations = true; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 215 | GoogleStyle.BinPackParameters = true; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 216 | GoogleStyle.BreakBeforeBinaryOperators = false; |
| 217 | GoogleStyle.BreakBeforeBraces = FormatStyle::BS_Attach; |
| 218 | GoogleStyle.BreakConstructorInitializersBeforeComma = false; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 219 | GoogleStyle.ColumnLimit = 80; |
| 220 | GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; |
Daniel Jasper | 6315fec | 2013-08-13 10:58:30 +0000 | [diff] [blame] | 221 | GoogleStyle.ConstructorInitializerIndentWidth = 4; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 222 | GoogleStyle.Cpp11BracedListStyle = true; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 223 | GoogleStyle.DerivePointerBinding = true; |
Daniel Jasper | c7bd68f | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 224 | GoogleStyle.ExperimentalAutoDetectBinPacking = false; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 225 | GoogleStyle.IndentCaseLabels = true; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 226 | GoogleStyle.IndentFunctionDeclarationAfterType = true; |
| 227 | GoogleStyle.IndentWidth = 2; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 228 | GoogleStyle.MaxEmptyLinesToKeep = 1; |
Daniel Jasper | eff18b9 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 229 | GoogleStyle.NamespaceIndentation = FormatStyle::NI_None; |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 230 | GoogleStyle.ObjCSpaceBeforeProtocolList = false; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 231 | GoogleStyle.PointerBindsToType = true; |
| 232 | GoogleStyle.SpacesBeforeTrailingComments = 2; |
| 233 | GoogleStyle.Standard = FormatStyle::LS_Auto; |
Manuel Klimek | 7c9a93e | 2013-05-13 09:22:11 +0000 | [diff] [blame] | 234 | GoogleStyle.UseTab = false; |
Daniel Jasper | 7df56bf | 2013-08-20 12:36:34 +0000 | [diff] [blame] | 235 | GoogleStyle.SpacesInParentheses = false; |
| 236 | GoogleStyle.SpaceInEmptyParentheses = false; |
| 237 | GoogleStyle.SpacesInCStyleCastParentheses = false; |
| 238 | GoogleStyle.SpaceAfterControlStatementKeyword = true; |
Daniel Jasper | faec47b | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 239 | |
| 240 | setDefaultPenalties(GoogleStyle); |
| 241 | GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200; |
| 242 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 243 | return GoogleStyle; |
| 244 | } |
| 245 | |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 246 | FormatStyle getChromiumStyle() { |
| 247 | FormatStyle ChromiumStyle = getGoogleStyle(); |
Daniel Jasper | f157960 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 248 | ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false; |
Daniel Jasper | 94d6ad7 | 2013-04-24 13:46:00 +0000 | [diff] [blame] | 249 | ChromiumStyle.AllowShortIfStatementsOnASingleLine = false; |
Daniel Jasper | f11bbb9 | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 250 | ChromiumStyle.AllowShortLoopsOnASingleLine = false; |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 251 | ChromiumStyle.BinPackParameters = false; |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 252 | ChromiumStyle.DerivePointerBinding = false; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 253 | ChromiumStyle.Standard = FormatStyle::LS_Cpp03; |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 254 | return ChromiumStyle; |
| 255 | } |
| 256 | |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 257 | FormatStyle getMozillaStyle() { |
| 258 | FormatStyle MozillaStyle = getLLVMStyle(); |
| 259 | MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false; |
| 260 | MozillaStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; |
| 261 | MozillaStyle.DerivePointerBinding = true; |
| 262 | MozillaStyle.IndentCaseLabels = true; |
| 263 | MozillaStyle.ObjCSpaceBeforeProtocolList = false; |
| 264 | MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200; |
| 265 | MozillaStyle.PointerBindsToType = true; |
| 266 | return MozillaStyle; |
| 267 | } |
| 268 | |
Daniel Jasper | e05dc6d | 2013-07-24 13:10:59 +0000 | [diff] [blame] | 269 | FormatStyle getWebKitStyle() { |
| 270 | FormatStyle Style = getLLVMStyle(); |
Daniel Jasper | eff18b9 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 271 | Style.AccessModifierOffset = -4; |
Daniel Jasper | 893ea8d | 2013-07-31 23:55:15 +0000 | [diff] [blame] | 272 | Style.AlignTrailingComments = false; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 273 | Style.BreakBeforeBinaryOperators = true; |
Daniel Jasper | eff18b9 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 274 | Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 275 | Style.BreakConstructorInitializersBeforeComma = true; |
Daniel Jasper | eff18b9 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 276 | Style.ColumnLimit = 0; |
Daniel Jasper | e8b10d3 | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 277 | Style.IndentWidth = 4; |
Daniel Jasper | eff18b9 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 278 | Style.NamespaceIndentation = FormatStyle::NI_Inner; |
Daniel Jasper | e05dc6d | 2013-07-24 13:10:59 +0000 | [diff] [blame] | 279 | Style.PointerBindsToType = true; |
| 280 | return Style; |
| 281 | } |
| 282 | |
Alexander Kornienko | 885f87b | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 283 | bool getPredefinedStyle(StringRef Name, FormatStyle *Style) { |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 284 | if (Name.equals_lower("llvm")) |
Alexander Kornienko | 885f87b | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 285 | *Style = getLLVMStyle(); |
| 286 | else if (Name.equals_lower("chromium")) |
| 287 | *Style = getChromiumStyle(); |
| 288 | else if (Name.equals_lower("mozilla")) |
| 289 | *Style = getMozillaStyle(); |
| 290 | else if (Name.equals_lower("google")) |
| 291 | *Style = getGoogleStyle(); |
Daniel Jasper | e05dc6d | 2013-07-24 13:10:59 +0000 | [diff] [blame] | 292 | else if (Name.equals_lower("webkit")) |
| 293 | *Style = getWebKitStyle(); |
Alexander Kornienko | 885f87b | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 294 | else |
| 295 | return false; |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 296 | |
Alexander Kornienko | 885f87b | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 297 | return true; |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | llvm::error_code parseConfiguration(StringRef Text, FormatStyle *Style) { |
Alexander Kornienko | 107db3c | 2013-05-20 15:18:01 +0000 | [diff] [blame] | 301 | if (Text.trim().empty()) |
| 302 | return llvm::make_error_code(llvm::errc::invalid_argument); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 303 | llvm::yaml::Input Input(Text); |
| 304 | Input >> *Style; |
| 305 | return Input.error(); |
| 306 | } |
| 307 | |
| 308 | std::string configurationAsText(const FormatStyle &Style) { |
| 309 | std::string Text; |
| 310 | llvm::raw_string_ostream Stream(Text); |
| 311 | llvm::yaml::Output Output(Stream); |
| 312 | // We use the same mapping method for input and output, so we need a non-const |
| 313 | // reference here. |
| 314 | FormatStyle NonConstStyle = Style; |
| 315 | Output << NonConstStyle; |
Alexander Kornienko | 2b6acb6 | 2013-05-13 12:56:35 +0000 | [diff] [blame] | 316 | return Stream.str(); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Craig Topper | 83f81d7 | 2013-06-30 22:29:28 +0000 | [diff] [blame] | 319 | namespace { |
| 320 | |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 321 | class NoColumnLimitFormatter { |
| 322 | public: |
Daniel Jasper | 34f3d05 | 2013-08-21 08:39:01 +0000 | [diff] [blame] | 323 | NoColumnLimitFormatter(ContinuationIndenter *Indenter) : Indenter(Indenter) {} |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 324 | |
| 325 | /// \brief Formats the line starting at \p State, simply keeping all of the |
| 326 | /// input's line breaking decisions. |
| 327 | void format() { |
| 328 | LineState State = Indenter->getInitialState(); |
| 329 | while (State.NextToken != NULL) { |
| 330 | bool Newline = |
| 331 | Indenter->mustBreak(State) || |
| 332 | (Indenter->canBreak(State) && State.NextToken->NewlinesBefore > 0); |
| 333 | Indenter->addTokenToState(State, Newline, /*DryRun=*/false); |
| 334 | } |
| 335 | } |
Daniel Jasper | 34f3d05 | 2013-08-21 08:39:01 +0000 | [diff] [blame] | 336 | |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 337 | private: |
| 338 | ContinuationIndenter *Indenter; |
| 339 | }; |
| 340 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 341 | class UnwrappedLineFormatter { |
| 342 | public: |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 343 | UnwrappedLineFormatter(ContinuationIndenter *Indenter, |
| 344 | const FormatStyle &Style, const AnnotatedLine &Line) |
| 345 | : Indenter(Indenter), Style(Style), Line(Line), Count(0) {} |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 346 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 347 | /// \brief Formats an \c UnwrappedLine. |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 348 | void format() { |
| 349 | LineState State = Indenter->getInitialState(); |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 350 | |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 351 | // If the ObjC method declaration does not fit on a line, we should format |
| 352 | // it with one arg per line. |
| 353 | if (Line.Type == LT_ObjCMethodDecl) |
| 354 | State.Stack.back().BreakBeforeParameter = true; |
| 355 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 356 | // Find best solution in solution space. |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 357 | analyzeSolutionSpace(State); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | private: |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 361 | /// \brief An edge in the solution space from \c Previous->State to \c State, |
| 362 | /// inserting a newline dependent on the \c NewLine. |
| 363 | struct StateNode { |
| 364 | StateNode(const LineState &State, bool NewLine, StateNode *Previous) |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 365 | : State(State), NewLine(NewLine), Previous(Previous) {} |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 366 | LineState State; |
| 367 | bool NewLine; |
| 368 | StateNode *Previous; |
| 369 | }; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 370 | |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 371 | /// \brief A pair of <penalty, count> that is used to prioritize the BFS on. |
| 372 | /// |
| 373 | /// In case of equal penalties, we want to prefer states that were inserted |
| 374 | /// first. During state generation we make sure that we insert states first |
| 375 | /// that break the line as late as possible. |
| 376 | typedef std::pair<unsigned, unsigned> OrderedPenalty; |
| 377 | |
| 378 | /// \brief An item in the prioritized BFS search queue. The \c StateNode's |
| 379 | /// \c State has the given \c OrderedPenalty. |
| 380 | typedef std::pair<OrderedPenalty, StateNode *> QueueItem; |
| 381 | |
| 382 | /// \brief The BFS queue type. |
| 383 | typedef std::priority_queue<QueueItem, std::vector<QueueItem>, |
| 384 | std::greater<QueueItem> > QueueType; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 385 | |
| 386 | /// \brief Analyze the entire solution space starting from \p InitialState. |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 387 | /// |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 388 | /// This implements a variant of Dijkstra's algorithm on the graph that spans |
| 389 | /// the solution space (\c LineStates are the nodes). The algorithm tries to |
| 390 | /// find the shortest path (the one with lowest penalty) from \p InitialState |
| 391 | /// to a state where all tokens are placed. |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 392 | void analyzeSolutionSpace(LineState &InitialState) { |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 393 | std::set<LineState> Seen; |
| 394 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 395 | // Insert start element into queue. |
Daniel Jasper | fc75908 | 2013-02-14 14:26:07 +0000 | [diff] [blame] | 396 | StateNode *Node = |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 397 | new (Allocator.Allocate()) StateNode(InitialState, false, NULL); |
| 398 | Queue.push(QueueItem(OrderedPenalty(0, Count), Node)); |
| 399 | ++Count; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 400 | |
| 401 | // While not empty, take first element and follow edges. |
| 402 | while (!Queue.empty()) { |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 403 | unsigned Penalty = Queue.top().first.first; |
Daniel Jasper | fc75908 | 2013-02-14 14:26:07 +0000 | [diff] [blame] | 404 | StateNode *Node = Queue.top().second; |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 405 | if (Node->State.NextToken == NULL) { |
Alexander Kornienko | dd25631 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 406 | DEBUG(llvm::dbgs() << "\n---\nPenalty for line: " << Penalty << "\n"); |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 407 | break; |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 408 | } |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 409 | Queue.pop(); |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 410 | |
Daniel Jasper | 54b4e44 | 2013-05-22 05:27:42 +0000 | [diff] [blame] | 411 | // Cut off the analysis of certain solutions if the analysis gets too |
| 412 | // complex. See description of IgnoreStackForComparison. |
| 413 | if (Count > 10000) |
| 414 | Node->State.IgnoreStackForComparison = true; |
| 415 | |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 416 | if (!Seen.insert(Node->State).second) |
| 417 | // State already examined with lower penalty. |
| 418 | continue; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 419 | |
Nico Weber | 2726877 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 420 | addNextStateToQueue(Penalty, Node, /*NewLine=*/false); |
| 421 | addNextStateToQueue(Penalty, Node, /*NewLine=*/true); |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | if (Queue.empty()) |
| 425 | // We were unable to find a solution, do nothing. |
| 426 | // FIXME: Add diagnostic? |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 427 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 428 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 429 | // Reconstruct the solution. |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 430 | reconstructPath(InitialState, Queue.top().second); |
Alexander Kornienko | dd25631 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 431 | DEBUG(llvm::dbgs() << "Total number of analyzed states: " << Count << "\n"); |
| 432 | DEBUG(llvm::dbgs() << "---\n"); |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | void reconstructPath(LineState &State, StateNode *Current) { |
Manuel Klimek | 9c333b9 | 2013-05-29 15:10:11 +0000 | [diff] [blame] | 436 | std::deque<StateNode *> Path; |
| 437 | // We do not need a break before the initial token. |
| 438 | while (Current->Previous) { |
| 439 | Path.push_front(Current); |
| 440 | Current = Current->Previous; |
| 441 | } |
| 442 | for (std::deque<StateNode *>::iterator I = Path.begin(), E = Path.end(); |
| 443 | I != E; ++I) { |
Daniel Jasper | d4a03db | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 444 | unsigned Penalty = Indenter->addTokenToState(State, (*I)->NewLine, false); |
Daniel Jasper | 259118e | 2013-08-22 16:11:46 +0000 | [diff] [blame] | 445 | (void)Penalty; |
Manuel Klimek | 9c333b9 | 2013-05-29 15:10:11 +0000 | [diff] [blame] | 446 | DEBUG({ |
| 447 | if ((*I)->NewLine) { |
Daniel Jasper | d4a03db | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 448 | llvm::dbgs() << "Penalty for placing " |
Manuel Klimek | 9c333b9 | 2013-05-29 15:10:11 +0000 | [diff] [blame] | 449 | << (*I)->Previous->State.NextToken->Tok.getName() << ": " |
Daniel Jasper | d4a03db | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 450 | << Penalty << "\n"; |
Manuel Klimek | 9c333b9 | 2013-05-29 15:10:11 +0000 | [diff] [blame] | 451 | } |
| 452 | }); |
Manuel Klimek | 9c333b9 | 2013-05-29 15:10:11 +0000 | [diff] [blame] | 453 | } |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Manuel Klimek | 62a48fb | 2013-02-13 10:54:19 +0000 | [diff] [blame] | 456 | /// \brief Add the following state to the analysis queue \c Queue. |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 457 | /// |
Manuel Klimek | 62a48fb | 2013-02-13 10:54:19 +0000 | [diff] [blame] | 458 | /// 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] | 459 | /// 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] | 460 | void addNextStateToQueue(unsigned Penalty, StateNode *PreviousNode, |
| 461 | bool NewLine) { |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 462 | if (NewLine && !Indenter->canBreak(PreviousNode->State)) |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 463 | return; |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 464 | if (!NewLine && Indenter->mustBreak(PreviousNode->State)) |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 465 | return; |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 466 | |
| 467 | StateNode *Node = new (Allocator.Allocate()) |
| 468 | StateNode(PreviousNode->State, NewLine, PreviousNode); |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 469 | Penalty += Indenter->addTokenToState(Node->State, NewLine, true); |
| 470 | if (Node->State.Column > Indenter->getColumnLimit()) { |
| 471 | unsigned ExcessCharacters = |
| 472 | Node->State.Column - Indenter->getColumnLimit(); |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 473 | Penalty += Style.PenaltyExcessCharacter * ExcessCharacters; |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 474 | } |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 475 | |
| 476 | Queue.push(QueueItem(OrderedPenalty(Penalty, Count), Node)); |
| 477 | ++Count; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 478 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 479 | |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 480 | ContinuationIndenter *Indenter; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 481 | FormatStyle Style; |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 482 | const AnnotatedLine &Line; |
Manuel Klimek | 62a48fb | 2013-02-13 10:54:19 +0000 | [diff] [blame] | 483 | |
| 484 | llvm::SpecificBumpPtrAllocator<StateNode> Allocator; |
| 485 | QueueType Queue; |
| 486 | // Increasing count of \c StateNode items we have created. This is used |
| 487 | // to create a deterministic order independent of the container. |
| 488 | unsigned Count; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 489 | }; |
| 490 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 491 | class FormatTokenLexer { |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 492 | public: |
Manuel Klimek | c41e819 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 493 | FormatTokenLexer(Lexer &Lex, SourceManager &SourceMgr, FormatStyle &Style, |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 494 | encoding::Encoding Encoding) |
Manuel Klimek | c41e819 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 495 | : FormatTok(NULL), GreaterStashed(false), Column(0), |
| 496 | TrailingWhitespace(0), Lex(Lex), SourceMgr(SourceMgr), Style(Style), |
| 497 | IdentTable(getFormattingLangOpts()), Encoding(Encoding) { |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 498 | Lex.SetKeepWhitespaceMode(true); |
| 499 | } |
| 500 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 501 | ArrayRef<FormatToken *> lex() { |
| 502 | assert(Tokens.empty()); |
| 503 | do { |
| 504 | Tokens.push_back(getNextToken()); |
| 505 | } while (Tokens.back()->Tok.isNot(tok::eof)); |
| 506 | return Tokens; |
| 507 | } |
| 508 | |
| 509 | IdentifierTable &getIdentTable() { return IdentTable; } |
| 510 | |
| 511 | private: |
| 512 | FormatToken *getNextToken() { |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 513 | if (GreaterStashed) { |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 514 | // Create a synthesized second '>' token. |
Manuel Klimek | c41e819 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 515 | // FIXME: Increment Column and set OriginalColumn. |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 516 | Token Greater = FormatTok->Tok; |
| 517 | FormatTok = new (Allocator.Allocate()) FormatToken; |
| 518 | FormatTok->Tok = Greater; |
Manuel Klimek | ad3094b | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 519 | SourceLocation GreaterLocation = |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 520 | FormatTok->Tok.getLocation().getLocWithOffset(1); |
| 521 | FormatTok->WhitespaceRange = |
| 522 | SourceRange(GreaterLocation, GreaterLocation); |
Alexander Kornienko | 54e6c9d | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 523 | FormatTok->TokenText = ">"; |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 524 | FormatTok->CodePointCount = 1; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 525 | GreaterStashed = false; |
| 526 | return FormatTok; |
| 527 | } |
| 528 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 529 | FormatTok = new (Allocator.Allocate()) FormatToken; |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 530 | readRawToken(*FormatTok); |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 531 | SourceLocation WhitespaceStart = |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 532 | FormatTok->Tok.getLocation().getLocWithOffset(-TrailingWhitespace); |
Manuel Klimek | ad3094b | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 533 | if (SourceMgr.getFileOffset(WhitespaceStart) == 0) |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 534 | FormatTok->IsFirst = true; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 535 | |
| 536 | // Consume and record whitespace until we find a significant token. |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 537 | unsigned WhitespaceLength = TrailingWhitespace; |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 538 | while (FormatTok->Tok.is(tok::unknown)) { |
Manuel Klimek | c41e819 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 539 | for (int i = 0, e = FormatTok->TokenText.size(); i != e; ++i) { |
| 540 | switch (FormatTok->TokenText[i]) { |
| 541 | case '\n': |
| 542 | ++FormatTok->NewlinesBefore; |
| 543 | // FIXME: This is technically incorrect, as it could also |
| 544 | // be a literal backslash at the end of the line. |
| 545 | if (i == 0 || FormatTok->TokenText[i-1] != '\\') |
| 546 | FormatTok->HasUnescapedNewline = true; |
| 547 | FormatTok->LastNewlineOffset = WhitespaceLength + i + 1; |
| 548 | Column = 0; |
| 549 | break; |
| 550 | case ' ': |
| 551 | ++Column; |
| 552 | break; |
| 553 | case '\t': |
| 554 | Column += Style.IndentWidth - Column % Style.IndentWidth; |
| 555 | break; |
| 556 | default: |
| 557 | ++Column; |
| 558 | break; |
| 559 | } |
| 560 | } |
| 561 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 562 | WhitespaceLength += FormatTok->Tok.getLength(); |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 563 | |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 564 | readRawToken(*FormatTok); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 565 | } |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 566 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 567 | // In case the token starts with escaped newlines, we want to |
| 568 | // take them into account as whitespace - this pattern is quite frequent |
| 569 | // in macro definitions. |
| 570 | // FIXME: What do we want to do with other escaped spaces, and escaped |
| 571 | // spaces or newlines in the middle of tokens? |
| 572 | // FIXME: Add a more explicit test. |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 573 | while (FormatTok->TokenText.size() > 1 && FormatTok->TokenText[0] == '\\' && |
| 574 | FormatTok->TokenText[1] == '\n') { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 575 | // FIXME: ++FormatTok->NewlinesBefore is missing... |
Manuel Klimek | ad3094b | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 576 | WhitespaceLength += 2; |
Manuel Klimek | c41e819 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 577 | Column = 0; |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 578 | FormatTok->TokenText = FormatTok->TokenText.substr(2); |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 579 | } |
Manuel Klimek | c41e819 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 580 | FormatTok->OriginalColumn = Column; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 581 | |
Alexander Kornienko | 54e6c9d | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 582 | TrailingWhitespace = 0; |
| 583 | if (FormatTok->Tok.is(tok::comment)) { |
Manuel Klimek | c41e819 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 584 | // FIXME: Add the trimmed whitespace to Column. |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 585 | StringRef UntrimmedText = FormatTok->TokenText; |
| 586 | FormatTok->TokenText = FormatTok->TokenText.rtrim(); |
| 587 | TrailingWhitespace = UntrimmedText.size() - FormatTok->TokenText.size(); |
Alexander Kornienko | 54e6c9d | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 588 | } else if (FormatTok->Tok.is(tok::raw_identifier)) { |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 589 | IdentifierInfo &Info = IdentTable.get(FormatTok->TokenText); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 590 | FormatTok->Tok.setIdentifierInfo(&Info); |
| 591 | FormatTok->Tok.setKind(Info.getTokenID()); |
Alexander Kornienko | 54e6c9d | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 592 | } else if (FormatTok->Tok.is(tok::greatergreater)) { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 593 | FormatTok->Tok.setKind(tok::greater); |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 594 | FormatTok->TokenText = FormatTok->TokenText.substr(0, 1); |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 595 | GreaterStashed = true; |
| 596 | } |
| 597 | |
Alexander Kornienko | 54e6c9d | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 598 | // Now FormatTok is the next non-whitespace token. |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 599 | FormatTok->CodePointCount = |
| 600 | encoding::getCodePointCount(FormatTok->TokenText, Encoding); |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 601 | |
Alexander Kornienko | 4b762a9 | 2013-09-02 13:58:14 +0000 | [diff] [blame] | 602 | if (FormatTok->isOneOf(tok::string_literal, tok::comment)) { |
| 603 | StringRef Text = FormatTok->TokenText; |
| 604 | size_t FirstNewlinePos = Text.find('\n'); |
| 605 | if (FirstNewlinePos != StringRef::npos) { |
| 606 | FormatTok->CodePointsInFirstLine = encoding::getCodePointCount( |
| 607 | Text.substr(0, FirstNewlinePos), Encoding); |
| 608 | FormatTok->CodePointsInLastLine = encoding::getCodePointCount( |
| 609 | Text.substr(Text.find_last_of('\n') + 1), Encoding); |
| 610 | } |
| 611 | } |
Alexander Kornienko | dcc0c5b | 2013-08-29 17:32:57 +0000 | [diff] [blame] | 612 | // FIXME: Add the CodePointCount to Column. |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 613 | FormatTok->WhitespaceRange = SourceRange( |
Manuel Klimek | ad3094b | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 614 | WhitespaceStart, WhitespaceStart.getLocWithOffset(WhitespaceLength)); |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 615 | return FormatTok; |
| 616 | } |
| 617 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 618 | FormatToken *FormatTok; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 619 | bool GreaterStashed; |
Manuel Klimek | c41e819 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 620 | unsigned Column; |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 621 | unsigned TrailingWhitespace; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 622 | Lexer &Lex; |
| 623 | SourceManager &SourceMgr; |
Manuel Klimek | c41e819 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 624 | FormatStyle &Style; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 625 | IdentifierTable IdentTable; |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 626 | encoding::Encoding Encoding; |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 627 | llvm::SpecificBumpPtrAllocator<FormatToken> Allocator; |
| 628 | SmallVector<FormatToken *, 16> Tokens; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 629 | |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 630 | void readRawToken(FormatToken &Tok) { |
| 631 | Lex.LexFromRawLexer(Tok.Tok); |
| 632 | Tok.TokenText = StringRef(SourceMgr.getCharacterData(Tok.Tok.getLocation()), |
| 633 | Tok.Tok.getLength()); |
Daniel Jasper | 561211d | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 634 | // For formatting, treat unterminated string literals like normal string |
| 635 | // literals. |
| 636 | if (Tok.is(tok::unknown) && !Tok.TokenText.empty() && |
| 637 | Tok.TokenText[0] == '"') { |
| 638 | Tok.Tok.setKind(tok::string_literal); |
| 639 | Tok.IsUnterminatedLiteral = true; |
| 640 | } |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 641 | } |
| 642 | }; |
| 643 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 644 | class Formatter : public UnwrappedLineConsumer { |
| 645 | public: |
Daniel Jasper | caf42a3 | 2013-05-15 08:14:19 +0000 | [diff] [blame] | 646 | Formatter(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr, |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 647 | const std::vector<CharSourceRange> &Ranges) |
Daniel Jasper | caf42a3 | 2013-05-15 08:14:19 +0000 | [diff] [blame] | 648 | : Style(Style), Lex(Lex), SourceMgr(SourceMgr), |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 649 | Whitespaces(SourceMgr, Style), Ranges(Ranges), |
| 650 | Encoding(encoding::detectEncoding(Lex.getBuffer())) { |
Daniel Jasper | 9637dda | 2013-07-15 14:33:14 +0000 | [diff] [blame] | 651 | DEBUG(llvm::dbgs() << "File encoding: " |
| 652 | << (Encoding == encoding::Encoding_UTF8 ? "UTF8" |
| 653 | : "unknown") |
| 654 | << "\n"); |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 655 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 656 | |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 657 | virtual ~Formatter() {} |
Daniel Jasper | accb0b0 | 2012-12-04 21:05:31 +0000 | [diff] [blame] | 658 | |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 659 | tooling::Replacements format() { |
Manuel Klimek | c41e819 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 660 | FormatTokenLexer Tokens(Lex, SourceMgr, Style, Encoding); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 661 | |
| 662 | UnwrappedLineParser Parser(Style, Tokens.lex(), *this); |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 663 | bool StructuralError = Parser.parse(); |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 664 | TokenAnnotator Annotator(Style, Tokens.getIdentTable().get("in")); |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 665 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
| 666 | Annotator.annotate(AnnotatedLines[i]); |
| 667 | } |
| 668 | deriveLocalStyle(); |
| 669 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
| 670 | Annotator.calculateFormattingInformation(AnnotatedLines[i]); |
| 671 | } |
Daniel Jasper | 5999f76 | 2013-04-09 17:46:55 +0000 | [diff] [blame] | 672 | |
| 673 | // Adapt level to the next line if this is a comment. |
| 674 | // FIXME: Can/should this be done in the UnwrappedLineParser? |
Alexander Kornienko | 0bdc643 | 2013-07-04 14:47:51 +0000 | [diff] [blame] | 675 | const AnnotatedLine *NextNonCommentLine = NULL; |
Daniel Jasper | 5999f76 | 2013-04-09 17:46:55 +0000 | [diff] [blame] | 676 | for (unsigned i = AnnotatedLines.size() - 1; i > 0; --i) { |
Alexander Kornienko | 0bdc643 | 2013-07-04 14:47:51 +0000 | [diff] [blame] | 677 | if (NextNonCommentLine && AnnotatedLines[i].First->is(tok::comment) && |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 678 | !AnnotatedLines[i].First->Next) |
Alexander Kornienko | 0bdc643 | 2013-07-04 14:47:51 +0000 | [diff] [blame] | 679 | AnnotatedLines[i].Level = NextNonCommentLine->Level; |
Daniel Jasper | 5999f76 | 2013-04-09 17:46:55 +0000 | [diff] [blame] | 680 | else |
Daniel Jasper | 2a409b6 | 2013-07-08 14:34:09 +0000 | [diff] [blame] | 681 | NextNonCommentLine = AnnotatedLines[i].First->isNot(tok::r_brace) |
| 682 | ? &AnnotatedLines[i] |
| 683 | : NULL; |
Daniel Jasper | 5999f76 | 2013-04-09 17:46:55 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 686 | std::vector<int> IndentForLevel; |
| 687 | bool PreviousLineWasTouched = false; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 688 | const FormatToken *PreviousLineLastToken = 0; |
Daniel Jasper | 89b3a7f | 2013-05-10 13:00:49 +0000 | [diff] [blame] | 689 | bool FormatPPDirective = false; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 690 | for (std::vector<AnnotatedLine>::iterator I = AnnotatedLines.begin(), |
| 691 | E = AnnotatedLines.end(); |
| 692 | I != E; ++I) { |
| 693 | const AnnotatedLine &TheLine = *I; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 694 | const FormatToken *FirstTok = TheLine.First; |
| 695 | int Offset = getIndentOffset(*TheLine.First); |
Daniel Jasper | 89b3a7f | 2013-05-10 13:00:49 +0000 | [diff] [blame] | 696 | |
| 697 | // Check whether this line is part of a formatted preprocessor directive. |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 698 | if (FirstTok->HasUnescapedNewline) |
Daniel Jasper | 89b3a7f | 2013-05-10 13:00:49 +0000 | [diff] [blame] | 699 | FormatPPDirective = false; |
| 700 | if (!FormatPPDirective && TheLine.InPPDirective && |
| 701 | (touchesLine(TheLine) || touchesPPDirective(I + 1, E))) |
| 702 | FormatPPDirective = true; |
| 703 | |
Daniel Jasper | 1fb8d88 | 2013-05-14 09:30:02 +0000 | [diff] [blame] | 704 | // Determine indent and try to merge multiple unwrapped lines. |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 705 | while (IndentForLevel.size() <= TheLine.Level) |
| 706 | IndentForLevel.push_back(-1); |
| 707 | IndentForLevel.resize(TheLine.Level + 1); |
Daniel Jasper | 1fb8d88 | 2013-05-14 09:30:02 +0000 | [diff] [blame] | 708 | unsigned Indent = getIndent(IndentForLevel, TheLine.Level); |
| 709 | if (static_cast<int>(Indent) + Offset >= 0) |
| 710 | Indent += Offset; |
| 711 | tryFitMultipleLinesInOne(Indent, I, E); |
| 712 | |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 713 | bool WasMoved = PreviousLineWasTouched && FirstTok->NewlinesBefore == 0; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 714 | if (TheLine.First->is(tok::eof)) { |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 715 | if (PreviousLineWasTouched) { |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 716 | unsigned NewLines = std::min(FirstTok->NewlinesBefore, 1u); |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 717 | Whitespaces.replaceWhitespace(*TheLine.First, NewLines, /*Indent*/ 0, |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 718 | /*TargetColumn*/ 0); |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 719 | } |
| 720 | } else if (TheLine.Type != LT_Invalid && |
Daniel Jasper | 89b3a7f | 2013-05-10 13:00:49 +0000 | [diff] [blame] | 721 | (WasMoved || FormatPPDirective || touchesLine(TheLine))) { |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 722 | unsigned LevelIndent = getIndent(IndentForLevel, TheLine.Level); |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 723 | if (FirstTok->WhitespaceRange.isValid() && |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 724 | // Insert a break even if there is a structural error in case where |
| 725 | // we break apart a line consisting of multiple unwrapped lines. |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 726 | (FirstTok->NewlinesBefore == 0 || !StructuralError)) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 727 | formatFirstToken(*TheLine.First, PreviousLineLastToken, Indent, |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 728 | TheLine.InPPDirective); |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 729 | } else { |
Manuel Klimek | c41e819 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 730 | Indent = LevelIndent = FirstTok->OriginalColumn; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 731 | } |
Daniel Jasper | 6b2afe4 | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 732 | ContinuationIndenter Indenter(Style, SourceMgr, TheLine, Indent, |
| 733 | Whitespaces, Encoding, |
| 734 | BinPackInconclusiveFunctions); |
| 735 | |
| 736 | // If everything fits on a single line, just put it there. |
| 737 | unsigned ColumnLimit = Style.ColumnLimit; |
| 738 | if ((I + 1) != E && (I + 1)->InPPDirective && |
| 739 | !(I + 1)->First->HasUnescapedNewline) |
| 740 | ColumnLimit = Indenter.getColumnLimit(); |
| 741 | |
| 742 | if (I->Last->TotalLength + Indent <= ColumnLimit) { |
| 743 | LineState State = Indenter.getInitialState(); |
| 744 | while (State.NextToken != NULL) |
| 745 | Indenter.addTokenToState(State, false, false); |
| 746 | } else if (Style.ColumnLimit == 0) { |
| 747 | NoColumnLimitFormatter Formatter(&Indenter); |
| 748 | Formatter.format(); |
| 749 | } else { |
| 750 | UnwrappedLineFormatter Formatter(&Indenter, Style, TheLine); |
| 751 | Formatter.format(); |
| 752 | } |
| 753 | |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 754 | IndentForLevel[TheLine.Level] = LevelIndent; |
| 755 | PreviousLineWasTouched = true; |
| 756 | } else { |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 757 | // Format the first token if necessary, and notify the WhitespaceManager |
| 758 | // about the unchanged whitespace. |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 759 | for (const FormatToken *Tok = TheLine.First; Tok != NULL; |
| 760 | Tok = Tok->Next) { |
| 761 | if (Tok == TheLine.First && |
| 762 | (Tok->NewlinesBefore > 0 || Tok->IsFirst)) { |
Manuel Klimek | c41e819 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 763 | unsigned LevelIndent = Tok->OriginalColumn; |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 764 | // Remove trailing whitespace of the previous line if it was |
| 765 | // touched. |
| 766 | if (PreviousLineWasTouched || touchesEmptyLineBefore(TheLine)) { |
| 767 | formatFirstToken(*Tok, PreviousLineLastToken, LevelIndent, |
| 768 | TheLine.InPPDirective); |
| 769 | } else { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 770 | Whitespaces.addUntouchableToken(*Tok, TheLine.InPPDirective); |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 771 | } |
Daniel Jasper | 1fb8d88 | 2013-05-14 09:30:02 +0000 | [diff] [blame] | 772 | |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 773 | if (static_cast<int>(LevelIndent) - Offset >= 0) |
| 774 | LevelIndent -= Offset; |
| 775 | if (Tok->isNot(tok::comment)) |
| 776 | IndentForLevel[TheLine.Level] = LevelIndent; |
| 777 | } else { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 778 | Whitespaces.addUntouchableToken(*Tok, TheLine.InPPDirective); |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 779 | } |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 780 | } |
| 781 | // If we did not reformat this unwrapped line, the column at the end of |
| 782 | // the last token is unchanged - thus, we can calculate the end of the |
| 783 | // last token. |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 784 | PreviousLineWasTouched = false; |
| 785 | } |
Alexander Kornienko | 94b748f | 2013-03-27 17:08:02 +0000 | [diff] [blame] | 786 | PreviousLineLastToken = I->Last; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 787 | } |
| 788 | return Whitespaces.generateReplacements(); |
| 789 | } |
| 790 | |
| 791 | private: |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 792 | void deriveLocalStyle() { |
| 793 | unsigned CountBoundToVariable = 0; |
| 794 | unsigned CountBoundToType = 0; |
| 795 | bool HasCpp03IncompatibleFormat = false; |
Daniel Jasper | c7bd68f | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 796 | bool HasBinPackedFunction = false; |
| 797 | bool HasOnePerLineFunction = false; |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 798 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 799 | if (!AnnotatedLines[i].First->Next) |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 800 | continue; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 801 | FormatToken *Tok = AnnotatedLines[i].First->Next; |
| 802 | while (Tok->Next) { |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 803 | if (Tok->Type == TT_PointerOrReference) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 804 | bool SpacesBefore = |
| 805 | Tok->WhitespaceRange.getBegin() != Tok->WhitespaceRange.getEnd(); |
| 806 | bool SpacesAfter = Tok->Next->WhitespaceRange.getBegin() != |
| 807 | Tok->Next->WhitespaceRange.getEnd(); |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 808 | if (SpacesBefore && !SpacesAfter) |
| 809 | ++CountBoundToVariable; |
| 810 | else if (!SpacesBefore && SpacesAfter) |
| 811 | ++CountBoundToType; |
| 812 | } |
| 813 | |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 814 | if (Tok->Type == TT_TemplateCloser && |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 815 | Tok->Previous->Type == TT_TemplateCloser && |
| 816 | Tok->WhitespaceRange.getBegin() == Tok->WhitespaceRange.getEnd()) |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 817 | HasCpp03IncompatibleFormat = true; |
Daniel Jasper | c7bd68f | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 818 | |
| 819 | if (Tok->PackingKind == PPK_BinPacked) |
| 820 | HasBinPackedFunction = true; |
| 821 | if (Tok->PackingKind == PPK_OnePerLine) |
| 822 | HasOnePerLineFunction = true; |
| 823 | |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 824 | Tok = Tok->Next; |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 825 | } |
| 826 | } |
| 827 | if (Style.DerivePointerBinding) { |
| 828 | if (CountBoundToType > CountBoundToVariable) |
| 829 | Style.PointerBindsToType = true; |
| 830 | else if (CountBoundToType < CountBoundToVariable) |
| 831 | Style.PointerBindsToType = false; |
| 832 | } |
| 833 | if (Style.Standard == FormatStyle::LS_Auto) { |
| 834 | Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11 |
| 835 | : FormatStyle::LS_Cpp03; |
| 836 | } |
Daniel Jasper | c7bd68f | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 837 | BinPackInconclusiveFunctions = |
| 838 | HasBinPackedFunction || !HasOnePerLineFunction; |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 841 | /// \brief Get the indent of \p Level from \p IndentForLevel. |
| 842 | /// |
| 843 | /// \p IndentForLevel must contain the indent for the level \c l |
| 844 | /// at \p IndentForLevel[l], or a value < 0 if the indent for |
| 845 | /// that level is unknown. |
Daniel Jasper | fc75908 | 2013-02-14 14:26:07 +0000 | [diff] [blame] | 846 | unsigned getIndent(const std::vector<int> IndentForLevel, unsigned Level) { |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 847 | if (IndentForLevel[Level] != -1) |
| 848 | return IndentForLevel[Level]; |
Manuel Klimek | 52635ff | 2013-02-08 19:53:32 +0000 | [diff] [blame] | 849 | if (Level == 0) |
| 850 | return 0; |
Manuel Klimek | 07a64ec | 2013-05-13 08:42:42 +0000 | [diff] [blame] | 851 | return getIndent(IndentForLevel, Level - 1) + Style.IndentWidth; |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | /// \brief Get the offset of the line relatively to the level. |
| 855 | /// |
| 856 | /// For example, 'public:' labels in classes are offset by 1 or 2 |
| 857 | /// characters to the left from their level. |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 858 | int getIndentOffset(const FormatToken &RootToken) { |
Alexander Kornienko | 94b748f | 2013-03-27 17:08:02 +0000 | [diff] [blame] | 859 | if (RootToken.isAccessSpecifier(false) || RootToken.isObjCAccessSpecifier()) |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 860 | return Style.AccessModifierOffset; |
| 861 | return 0; |
| 862 | } |
| 863 | |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 864 | /// \brief Tries to merge lines into one. |
| 865 | /// |
| 866 | /// This will change \c Line and \c AnnotatedLine to contain the merged line, |
| 867 | /// if possible; note that \c I will be incremented when lines are merged. |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 868 | void tryFitMultipleLinesInOne(unsigned Indent, |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 869 | std::vector<AnnotatedLine>::iterator &I, |
| 870 | std::vector<AnnotatedLine>::iterator E) { |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 871 | // We can never merge stuff if there are trailing line comments. |
| 872 | if (I->Last->Type == TT_LineComment) |
| 873 | return; |
| 874 | |
Daniel Jasper | e05dc6d | 2013-07-24 13:10:59 +0000 | [diff] [blame] | 875 | if (Indent > Style.ColumnLimit) |
| 876 | return; |
| 877 | |
Daniel Jasper | a4d4621 | 2013-02-28 11:05:57 +0000 | [diff] [blame] | 878 | unsigned Limit = Style.ColumnLimit - Indent; |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 879 | // If we already exceed the column limit, we set 'Limit' to 0. The different |
| 880 | // tryMerge..() functions can then decide whether to still do merging. |
| 881 | Limit = I->Last->TotalLength > Limit ? 0 : Limit - I->Last->TotalLength; |
Daniel Jasper | 55b08e7 | 2013-01-16 07:02:34 +0000 | [diff] [blame] | 882 | |
Daniel Jasper | 9c8c40e | 2013-01-21 14:18:28 +0000 | [diff] [blame] | 883 | if (I + 1 == E || (I + 1)->Type == LT_Invalid) |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 884 | return; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 885 | |
Daniel Jasper | 5be59ba | 2013-05-15 14:09:55 +0000 | [diff] [blame] | 886 | if (I->Last->is(tok::l_brace)) { |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 887 | tryMergeSimpleBlock(I, E, Limit); |
Daniel Jasper | f11bbb9 | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 888 | } else if (Style.AllowShortIfStatementsOnASingleLine && |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 889 | I->First->is(tok::kw_if)) { |
Daniel Jasper | f11bbb9 | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 890 | tryMergeSimpleControlStatement(I, E, Limit); |
| 891 | } else if (Style.AllowShortLoopsOnASingleLine && |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 892 | I->First->isOneOf(tok::kw_for, tok::kw_while)) { |
Daniel Jasper | f11bbb9 | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 893 | tryMergeSimpleControlStatement(I, E, Limit); |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 894 | } else if (I->InPPDirective && |
| 895 | (I->First->HasUnescapedNewline || I->First->IsFirst)) { |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 896 | tryMergeSimplePPDirective(I, E, Limit); |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 897 | } |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 898 | } |
| 899 | |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 900 | void tryMergeSimplePPDirective(std::vector<AnnotatedLine>::iterator &I, |
| 901 | std::vector<AnnotatedLine>::iterator E, |
| 902 | unsigned Limit) { |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 903 | if (Limit == 0) |
| 904 | return; |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 905 | AnnotatedLine &Line = *I; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 906 | if (!(I + 1)->InPPDirective || (I + 1)->First->HasUnescapedNewline) |
Daniel Jasper | 2b9c10b | 2013-01-14 15:52:06 +0000 | [diff] [blame] | 907 | return; |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 908 | if (I + 2 != E && (I + 2)->InPPDirective && |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 909 | !(I + 2)->First->HasUnescapedNewline) |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 910 | return; |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 911 | if (1 + (I + 1)->Last->TotalLength > Limit) |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 912 | return; |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 913 | join(Line, *(++I)); |
| 914 | } |
| 915 | |
Daniel Jasper | f11bbb9 | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 916 | void tryMergeSimpleControlStatement(std::vector<AnnotatedLine>::iterator &I, |
| 917 | std::vector<AnnotatedLine>::iterator E, |
| 918 | unsigned Limit) { |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 919 | if (Limit == 0) |
| 920 | return; |
Manuel Klimek | d3a247c | 2013-08-07 19:20:45 +0000 | [diff] [blame] | 921 | if (Style.BreakBeforeBraces == FormatStyle::BS_Allman && |
| 922 | (I + 1)->First->is(tok::l_brace)) |
| 923 | return; |
Manuel Klimek | 4c12812 | 2013-01-18 14:46:43 +0000 | [diff] [blame] | 924 | if ((I + 1)->InPPDirective != I->InPPDirective || |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 925 | ((I + 1)->InPPDirective && (I + 1)->First->HasUnescapedNewline)) |
Manuel Klimek | 4c12812 | 2013-01-18 14:46:43 +0000 | [diff] [blame] | 926 | return; |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 927 | AnnotatedLine &Line = *I; |
Daniel Jasper | 55b08e7 | 2013-01-16 07:02:34 +0000 | [diff] [blame] | 928 | if (Line.Last->isNot(tok::r_paren)) |
| 929 | return; |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 930 | if (1 + (I + 1)->Last->TotalLength > Limit) |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 931 | return; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 932 | if ((I + 1)->First->isOneOf(tok::semi, tok::kw_if, tok::kw_for, |
| 933 | tok::kw_while) || |
| 934 | (I + 1)->First->Type == TT_LineComment) |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 935 | return; |
| 936 | // Only inline simple if's (no nested if or else). |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 937 | if (I + 2 != E && Line.First->is(tok::kw_if) && |
| 938 | (I + 2)->First->is(tok::kw_else)) |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 939 | return; |
| 940 | join(Line, *(++I)); |
| 941 | } |
| 942 | |
| 943 | void tryMergeSimpleBlock(std::vector<AnnotatedLine>::iterator &I, |
Daniel Jasper | 1a1ce83 | 2013-01-29 11:27:30 +0000 | [diff] [blame] | 944 | std::vector<AnnotatedLine>::iterator E, |
| 945 | unsigned Limit) { |
Daniel Jasper | 5be59ba | 2013-05-15 14:09:55 +0000 | [diff] [blame] | 946 | // No merging if the brace already is on the next line. |
| 947 | if (Style.BreakBeforeBraces != FormatStyle::BS_Attach) |
| 948 | return; |
| 949 | |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 950 | // First, check that the current line allows merging. This is the case if |
| 951 | // we're not in a control flow statement and the last token is an opening |
| 952 | // brace. |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 953 | AnnotatedLine &Line = *I; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 954 | if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::r_brace, |
| 955 | tok::kw_else, tok::kw_try, tok::kw_catch, |
Daniel Jasper | 8893b8a | 2013-05-31 14:56:20 +0000 | [diff] [blame] | 956 | tok::kw_for, |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 957 | // This gets rid of all ObjC @ keywords and methods. |
| 958 | tok::at, tok::minus, tok::plus)) |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 959 | return; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 960 | |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 961 | FormatToken *Tok = (I + 1)->First; |
Daniel Jasper | 8893b8a | 2013-05-31 14:56:20 +0000 | [diff] [blame] | 962 | if (Tok->is(tok::r_brace) && !Tok->MustBreakBefore && |
Alexander Kornienko | 0bdc643 | 2013-07-04 14:47:51 +0000 | [diff] [blame] | 963 | (Tok->getNextNonComment() == NULL || |
| 964 | Tok->getNextNonComment()->is(tok::semi))) { |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 965 | // We merge empty blocks even if the line exceeds the column limit. |
Daniel Jasper | 729a743 | 2013-02-11 12:36:37 +0000 | [diff] [blame] | 966 | Tok->SpacesRequiredBefore = 0; |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 967 | Tok->CanBreakBefore = true; |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 968 | join(Line, *(I + 1)); |
| 969 | I += 1; |
Daniel Jasper | 8893b8a | 2013-05-31 14:56:20 +0000 | [diff] [blame] | 970 | } else if (Limit != 0 && Line.First->isNot(tok::kw_namespace)) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 971 | // Check that we still have three lines and they fit into the limit. |
| 972 | if (I + 2 == E || (I + 2)->Type == LT_Invalid || |
| 973 | !nextTwoLinesFitInto(I, Limit)) |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 974 | return; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 975 | |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 976 | // Second, check that the next line does not contain any braces - if it |
| 977 | // does, readability declines when putting it into a single line. |
| 978 | if ((I + 1)->Last->Type == TT_LineComment || Tok->MustBreakBefore) |
| 979 | return; |
| 980 | do { |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 981 | if (Tok->isOneOf(tok::l_brace, tok::r_brace)) |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 982 | return; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 983 | Tok = Tok->Next; |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 984 | } while (Tok != NULL); |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 985 | |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 986 | // Last, check that the third line contains a single closing brace. |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 987 | Tok = (I + 2)->First; |
Alexander Kornienko | 0bdc643 | 2013-07-04 14:47:51 +0000 | [diff] [blame] | 988 | if (Tok->getNextNonComment() != NULL || Tok->isNot(tok::r_brace) || |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 989 | Tok->MustBreakBefore) |
| 990 | return; |
| 991 | |
| 992 | join(Line, *(I + 1)); |
| 993 | join(Line, *(I + 2)); |
| 994 | I += 2; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 995 | } |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | bool nextTwoLinesFitInto(std::vector<AnnotatedLine>::iterator I, |
| 999 | unsigned Limit) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1000 | return 1 + (I + 1)->Last->TotalLength + 1 + (I + 2)->Last->TotalLength <= |
| 1001 | Limit; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1004 | void join(AnnotatedLine &A, const AnnotatedLine &B) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1005 | assert(!A.Last->Next); |
| 1006 | assert(!B.First->Previous); |
| 1007 | A.Last->Next = B.First; |
| 1008 | B.First->Previous = A.Last; |
| 1009 | unsigned LengthA = A.Last->TotalLength + B.First->SpacesRequiredBefore; |
| 1010 | for (FormatToken *Tok = B.First; Tok; Tok = Tok->Next) { |
| 1011 | Tok->TotalLength += LengthA; |
| 1012 | A.Last = Tok; |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1013 | } |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Daniel Jasper | 6f21a98 | 2013-03-13 07:49:51 +0000 | [diff] [blame] | 1016 | bool touchesRanges(const CharSourceRange &Range) { |
Daniel Jasper | f302354 | 2013-03-07 20:50:00 +0000 | [diff] [blame] | 1017 | for (unsigned i = 0, e = Ranges.size(); i != e; ++i) { |
| 1018 | if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), |
| 1019 | Ranges[i].getBegin()) && |
| 1020 | !SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(), |
| 1021 | Range.getBegin())) |
| 1022 | return true; |
| 1023 | } |
| 1024 | return false; |
| 1025 | } |
| 1026 | |
| 1027 | bool touchesLine(const AnnotatedLine &TheLine) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1028 | const FormatToken *First = TheLine.First; |
| 1029 | const FormatToken *Last = TheLine.Last; |
Daniel Jasper | 84f5ddf | 2013-05-14 10:31:09 +0000 | [diff] [blame] | 1030 | CharSourceRange LineRange = CharSourceRange::getCharRange( |
Manuel Klimek | ad3094b | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 1031 | First->WhitespaceRange.getBegin().getLocWithOffset( |
| 1032 | First->LastNewlineOffset), |
Alexander Kornienko | 54e6c9d | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 1033 | Last->Tok.getLocation().getLocWithOffset(Last->TokenText.size() - 1)); |
Daniel Jasper | f302354 | 2013-03-07 20:50:00 +0000 | [diff] [blame] | 1034 | return touchesRanges(LineRange); |
| 1035 | } |
| 1036 | |
Daniel Jasper | 89b3a7f | 2013-05-10 13:00:49 +0000 | [diff] [blame] | 1037 | bool touchesPPDirective(std::vector<AnnotatedLine>::iterator I, |
| 1038 | std::vector<AnnotatedLine>::iterator E) { |
| 1039 | for (; I != E; ++I) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1040 | if (I->First->HasUnescapedNewline) |
Daniel Jasper | 89b3a7f | 2013-05-10 13:00:49 +0000 | [diff] [blame] | 1041 | return false; |
| 1042 | if (touchesLine(*I)) |
| 1043 | return true; |
| 1044 | } |
| 1045 | return false; |
| 1046 | } |
| 1047 | |
Daniel Jasper | f302354 | 2013-03-07 20:50:00 +0000 | [diff] [blame] | 1048 | bool touchesEmptyLineBefore(const AnnotatedLine &TheLine) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1049 | const FormatToken *First = TheLine.First; |
Daniel Jasper | f302354 | 2013-03-07 20:50:00 +0000 | [diff] [blame] | 1050 | CharSourceRange LineRange = CharSourceRange::getCharRange( |
Manuel Klimek | ad3094b | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 1051 | First->WhitespaceRange.getBegin(), |
| 1052 | First->WhitespaceRange.getBegin().getLocWithOffset( |
| 1053 | First->LastNewlineOffset)); |
Daniel Jasper | f302354 | 2013-03-07 20:50:00 +0000 | [diff] [blame] | 1054 | return touchesRanges(LineRange); |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) { |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 1058 | AnnotatedLines.push_back(AnnotatedLine(TheLine)); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1061 | /// \brief Add a new line and the required indent before the first Token |
| 1062 | /// of the \c UnwrappedLine if there was no structural parsing error. |
| 1063 | /// Returns the indent level of the \c UnwrappedLine. |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1064 | void formatFirstToken(const FormatToken &RootToken, |
| 1065 | const FormatToken *PreviousToken, unsigned Indent, |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 1066 | bool InPPDirective) { |
Daniel Jasper | 1a1ce83 | 2013-01-29 11:27:30 +0000 | [diff] [blame] | 1067 | unsigned Newlines = |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1068 | std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1); |
Daniel Jasper | 15f33f0 | 2013-06-03 16:16:41 +0000 | [diff] [blame] | 1069 | // Remove empty lines before "}" where applicable. |
| 1070 | if (RootToken.is(tok::r_brace) && |
| 1071 | (!RootToken.Next || |
| 1072 | (RootToken.Next->is(tok::semi) && !RootToken.Next->Next))) |
| 1073 | Newlines = std::min(Newlines, 1u); |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1074 | if (Newlines == 0 && !RootToken.IsFirst) |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1075 | Newlines = 1; |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1076 | |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 1077 | // Insert extra new line before access specifiers. |
| 1078 | if (PreviousToken && PreviousToken->isOneOf(tok::semi, tok::r_brace) && |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1079 | RootToken.isAccessSpecifier() && RootToken.NewlinesBefore == 1) |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 1080 | ++Newlines; |
Alexander Kornienko | 94b748f | 2013-03-27 17:08:02 +0000 | [diff] [blame] | 1081 | |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1082 | Whitespaces.replaceWhitespace( |
| 1083 | RootToken, Newlines, Indent, Indent, |
| 1084 | InPPDirective && !RootToken.HasUnescapedNewline); |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1087 | FormatStyle Style; |
| 1088 | Lexer &Lex; |
| 1089 | SourceManager &SourceMgr; |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 1090 | WhitespaceManager Whitespaces; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1091 | std::vector<CharSourceRange> Ranges; |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1092 | std::vector<AnnotatedLine> AnnotatedLines; |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1093 | |
| 1094 | encoding::Encoding Encoding; |
Daniel Jasper | c7bd68f | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 1095 | bool BinPackInconclusiveFunctions; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1096 | }; |
| 1097 | |
Craig Topper | 83f81d7 | 2013-06-30 22:29:28 +0000 | [diff] [blame] | 1098 | } // end anonymous namespace |
| 1099 | |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 1100 | tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex, |
| 1101 | SourceManager &SourceMgr, |
Daniel Jasper | caf42a3 | 2013-05-15 08:14:19 +0000 | [diff] [blame] | 1102 | std::vector<CharSourceRange> Ranges) { |
| 1103 | Formatter formatter(Style, Lex, SourceMgr, Ranges); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1104 | return formatter.format(); |
| 1105 | } |
| 1106 | |
Daniel Jasper | 8a99945 | 2013-05-16 10:40:07 +0000 | [diff] [blame] | 1107 | tooling::Replacements reformat(const FormatStyle &Style, StringRef Code, |
| 1108 | std::vector<tooling::Range> Ranges, |
| 1109 | StringRef FileName) { |
| 1110 | FileManager Files((FileSystemOptions())); |
| 1111 | DiagnosticsEngine Diagnostics( |
| 1112 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), |
| 1113 | new DiagnosticOptions); |
| 1114 | SourceManager SourceMgr(Diagnostics, Files); |
| 1115 | llvm::MemoryBuffer *Buf = llvm::MemoryBuffer::getMemBuffer(Code, FileName); |
| 1116 | const clang::FileEntry *Entry = |
| 1117 | Files.getVirtualFile(FileName, Buf->getBufferSize(), 0); |
| 1118 | SourceMgr.overrideFileContents(Entry, Buf); |
| 1119 | FileID ID = |
| 1120 | SourceMgr.createFileID(Entry, SourceLocation(), clang::SrcMgr::C_User); |
Alexander Kornienko | a1753f4 | 2013-06-28 12:51:24 +0000 | [diff] [blame] | 1121 | Lexer Lex(ID, SourceMgr.getBuffer(ID), SourceMgr, |
| 1122 | getFormattingLangOpts(Style.Standard)); |
Daniel Jasper | 8a99945 | 2013-05-16 10:40:07 +0000 | [diff] [blame] | 1123 | SourceLocation StartOfFile = SourceMgr.getLocForStartOfFile(ID); |
| 1124 | std::vector<CharSourceRange> CharRanges; |
| 1125 | for (unsigned i = 0, e = Ranges.size(); i != e; ++i) { |
| 1126 | SourceLocation Start = StartOfFile.getLocWithOffset(Ranges[i].getOffset()); |
| 1127 | SourceLocation End = Start.getLocWithOffset(Ranges[i].getLength()); |
| 1128 | CharRanges.push_back(CharSourceRange::getCharRange(Start, End)); |
| 1129 | } |
| 1130 | return reformat(Style, Lex, SourceMgr, CharRanges); |
| 1131 | } |
| 1132 | |
Alexander Kornienko | a1753f4 | 2013-06-28 12:51:24 +0000 | [diff] [blame] | 1133 | LangOptions getFormattingLangOpts(FormatStyle::LanguageStandard Standard) { |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1134 | LangOptions LangOpts; |
| 1135 | LangOpts.CPlusPlus = 1; |
Alexander Kornienko | a1753f4 | 2013-06-28 12:51:24 +0000 | [diff] [blame] | 1136 | LangOpts.CPlusPlus11 = Standard == FormatStyle::LS_Cpp03 ? 0 : 1; |
Daniel Jasper | b64eca0 | 2013-03-22 10:01:29 +0000 | [diff] [blame] | 1137 | LangOpts.LineComment = 1; |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1138 | LangOpts.Bool = 1; |
| 1139 | LangOpts.ObjC1 = 1; |
| 1140 | LangOpts.ObjC2 = 1; |
| 1141 | return LangOpts; |
| 1142 | } |
| 1143 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1144 | } // namespace format |
| 1145 | } // namespace clang |