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