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