Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1 | //===--- Format.cpp - Format C++ code -------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// \brief This file implements functions declared in Format.h. This will be |
| 12 | /// split into separate files as we go. |
| 13 | /// |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Daniel Jasper | 85c472d | 2015-09-29 07:53:08 +0000 | [diff] [blame] | 16 | #include "clang/Format/Format.h" |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 17 | #include "ContinuationIndenter.h" |
Daniel Jasper | 7a6d09b | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 18 | #include "TokenAnnotator.h" |
Daniel Jasper | 0df5093 | 2014-12-10 19:00:42 +0000 | [diff] [blame] | 19 | #include "UnwrappedLineFormatter.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "UnwrappedLineParser.h" |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 21 | #include "WhitespaceManager.h" |
Daniel Jasper | ec04c0d | 2013-05-16 10:40:07 +0000 | [diff] [blame] | 22 | #include "clang/Basic/Diagnostic.h" |
Benjamin Kramer | f3ca2698 | 2014-05-10 16:31:55 +0000 | [diff] [blame] | 23 | #include "clang/Basic/DiagnosticOptions.h" |
Chandler Carruth | 44eb4f6 | 2013-01-02 10:28:36 +0000 | [diff] [blame] | 24 | #include "clang/Basic/SourceManager.h" |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 25 | #include "clang/Lex/Lexer.h" |
Alexander Kornienko | ffd6d04 | 2013-03-27 11:52:18 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/STLExtras.h" |
Manuel Klimek | 2ef908e | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Allocator.h" |
Manuel Klimek | 2499810 | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Debug.h" |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Path.h" |
Birunthan Mohanathas | b001a0b | 2015-07-03 17:25:16 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Regex.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 31 | #include "llvm/Support/YAMLTraits.h" |
Manuel Klimek | 2ef908e | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 32 | #include <queue> |
Daniel Jasper | 8b52971 | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 33 | #include <string> |
| 34 | |
Chandler Carruth | 1034666 | 2014-04-22 03:17:02 +0000 | [diff] [blame] | 35 | #define DEBUG_TYPE "format-formatter" |
| 36 | |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 37 | using clang::format::FormatStyle; |
| 38 | |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 39 | LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(std::string) |
Daniel Jasper | 8ce1b8d | 2015-10-06 11:54:18 +0000 | [diff] [blame] | 40 | LLVM_YAML_IS_SEQUENCE_VECTOR(clang::format::FormatStyle::IncludeCategory) |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 41 | |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 42 | namespace llvm { |
| 43 | namespace yaml { |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 44 | template <> struct ScalarEnumerationTraits<FormatStyle::LanguageKind> { |
| 45 | static void enumeration(IO &IO, FormatStyle::LanguageKind &Value) { |
| 46 | IO.enumCase(Value, "Cpp", FormatStyle::LK_Cpp); |
Daniel Jasper | c58c70e | 2014-09-15 11:21:46 +0000 | [diff] [blame] | 47 | IO.enumCase(Value, "Java", FormatStyle::LK_Java); |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 48 | IO.enumCase(Value, "JavaScript", FormatStyle::LK_JavaScript); |
Daniel Jasper | 7052ce6 | 2014-01-19 09:04:08 +0000 | [diff] [blame] | 49 | IO.enumCase(Value, "Proto", FormatStyle::LK_Proto); |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 50 | } |
| 51 | }; |
| 52 | |
| 53 | template <> struct ScalarEnumerationTraits<FormatStyle::LanguageStandard> { |
| 54 | static void enumeration(IO &IO, FormatStyle::LanguageStandard &Value) { |
| 55 | IO.enumCase(Value, "Cpp03", FormatStyle::LS_Cpp03); |
| 56 | IO.enumCase(Value, "C++03", FormatStyle::LS_Cpp03); |
| 57 | IO.enumCase(Value, "Cpp11", FormatStyle::LS_Cpp11); |
| 58 | IO.enumCase(Value, "C++11", FormatStyle::LS_Cpp11); |
| 59 | IO.enumCase(Value, "Auto", FormatStyle::LS_Auto); |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | template <> struct ScalarEnumerationTraits<FormatStyle::UseTabStyle> { |
| 64 | static void enumeration(IO &IO, FormatStyle::UseTabStyle &Value) { |
| 65 | IO.enumCase(Value, "Never", FormatStyle::UT_Never); |
| 66 | IO.enumCase(Value, "false", FormatStyle::UT_Never); |
| 67 | IO.enumCase(Value, "Always", FormatStyle::UT_Always); |
| 68 | IO.enumCase(Value, "true", FormatStyle::UT_Always); |
| 69 | IO.enumCase(Value, "ForIndentation", FormatStyle::UT_ForIndentation); |
| 70 | } |
| 71 | }; |
| 72 | |
Daniel Jasper | d74cf40 | 2014-04-08 12:46:38 +0000 | [diff] [blame] | 73 | template <> struct ScalarEnumerationTraits<FormatStyle::ShortFunctionStyle> { |
| 74 | static void enumeration(IO &IO, FormatStyle::ShortFunctionStyle &Value) { |
| 75 | IO.enumCase(Value, "None", FormatStyle::SFS_None); |
| 76 | IO.enumCase(Value, "false", FormatStyle::SFS_None); |
| 77 | IO.enumCase(Value, "All", FormatStyle::SFS_All); |
| 78 | IO.enumCase(Value, "true", FormatStyle::SFS_All); |
| 79 | IO.enumCase(Value, "Inline", FormatStyle::SFS_Inline); |
Daniel Jasper | 9e70935 | 2014-11-26 10:43:58 +0000 | [diff] [blame] | 80 | IO.enumCase(Value, "Empty", FormatStyle::SFS_Empty); |
Daniel Jasper | d74cf40 | 2014-04-08 12:46:38 +0000 | [diff] [blame] | 81 | } |
| 82 | }; |
| 83 | |
Daniel Jasper | ac043c9 | 2014-09-15 11:11:00 +0000 | [diff] [blame] | 84 | template <> struct ScalarEnumerationTraits<FormatStyle::BinaryOperatorStyle> { |
| 85 | static void enumeration(IO &IO, FormatStyle::BinaryOperatorStyle &Value) { |
| 86 | IO.enumCase(Value, "All", FormatStyle::BOS_All); |
| 87 | IO.enumCase(Value, "true", FormatStyle::BOS_All); |
| 88 | IO.enumCase(Value, "None", FormatStyle::BOS_None); |
| 89 | IO.enumCase(Value, "false", FormatStyle::BOS_None); |
| 90 | IO.enumCase(Value, "NonAssignment", FormatStyle::BOS_NonAssignment); |
| 91 | } |
| 92 | }; |
| 93 | |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 94 | template <> struct ScalarEnumerationTraits<FormatStyle::BraceBreakingStyle> { |
| 95 | static void enumeration(IO &IO, FormatStyle::BraceBreakingStyle &Value) { |
| 96 | IO.enumCase(Value, "Attach", FormatStyle::BS_Attach); |
| 97 | IO.enumCase(Value, "Linux", FormatStyle::BS_Linux); |
Birunthan Mohanathas | 305fa9c | 2015-07-12 03:13:54 +0000 | [diff] [blame] | 98 | IO.enumCase(Value, "Mozilla", FormatStyle::BS_Mozilla); |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 99 | IO.enumCase(Value, "Stroustrup", FormatStyle::BS_Stroustrup); |
| 100 | IO.enumCase(Value, "Allman", FormatStyle::BS_Allman); |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 101 | IO.enumCase(Value, "GNU", FormatStyle::BS_GNU); |
Roman Kashitsyn | 291f64f | 2015-08-10 13:43:19 +0000 | [diff] [blame] | 102 | IO.enumCase(Value, "WebKit", FormatStyle::BS_WebKit); |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 103 | IO.enumCase(Value, "Custom", FormatStyle::BS_Custom); |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 104 | } |
| 105 | }; |
| 106 | |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 107 | template <> |
| 108 | struct ScalarEnumerationTraits<FormatStyle::DefinitionReturnTypeBreakingStyle> { |
| 109 | static void |
| 110 | enumeration(IO &IO, FormatStyle::DefinitionReturnTypeBreakingStyle &Value) { |
Birunthan Mohanathas | a0388a8 | 2015-06-29 15:30:42 +0000 | [diff] [blame] | 111 | IO.enumCase(Value, "None", FormatStyle::DRTBS_None); |
| 112 | IO.enumCase(Value, "All", FormatStyle::DRTBS_All); |
| 113 | IO.enumCase(Value, "TopLevel", FormatStyle::DRTBS_TopLevel); |
| 114 | |
| 115 | // For backward compatibility. |
| 116 | IO.enumCase(Value, "false", FormatStyle::DRTBS_None); |
| 117 | IO.enumCase(Value, "true", FormatStyle::DRTBS_All); |
| 118 | } |
| 119 | }; |
| 120 | |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 121 | template <> |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 122 | struct ScalarEnumerationTraits<FormatStyle::NamespaceIndentationKind> { |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 123 | static void enumeration(IO &IO, |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 124 | FormatStyle::NamespaceIndentationKind &Value) { |
| 125 | IO.enumCase(Value, "None", FormatStyle::NI_None); |
| 126 | IO.enumCase(Value, "Inner", FormatStyle::NI_Inner); |
| 127 | IO.enumCase(Value, "All", FormatStyle::NI_All); |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 128 | } |
| 129 | }; |
| 130 | |
Daniel Jasper | 6501f7e | 2015-10-27 12:38:37 +0000 | [diff] [blame] | 131 | template <> struct ScalarEnumerationTraits<FormatStyle::BracketAlignmentStyle> { |
| 132 | static void enumeration(IO &IO, FormatStyle::BracketAlignmentStyle &Value) { |
| 133 | IO.enumCase(Value, "Align", FormatStyle::BAS_Align); |
| 134 | IO.enumCase(Value, "DontAlign", FormatStyle::BAS_DontAlign); |
| 135 | IO.enumCase(Value, "AlwaysBreak", FormatStyle::BAS_AlwaysBreak); |
| 136 | |
| 137 | // For backward compatibility. |
| 138 | IO.enumCase(Value, "true", FormatStyle::BAS_Align); |
| 139 | IO.enumCase(Value, "false", FormatStyle::BAS_DontAlign); |
| 140 | } |
| 141 | }; |
| 142 | |
Jacques Pienaar | fc27511 | 2015-02-18 23:48:37 +0000 | [diff] [blame] | 143 | template <> struct ScalarEnumerationTraits<FormatStyle::PointerAlignmentStyle> { |
| 144 | static void enumeration(IO &IO, FormatStyle::PointerAlignmentStyle &Value) { |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 145 | IO.enumCase(Value, "Middle", FormatStyle::PAS_Middle); |
| 146 | IO.enumCase(Value, "Left", FormatStyle::PAS_Left); |
| 147 | IO.enumCase(Value, "Right", FormatStyle::PAS_Right); |
| 148 | |
Alp Toker | 958027b | 2014-07-14 19:42:55 +0000 | [diff] [blame] | 149 | // For backward compatibility. |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 150 | IO.enumCase(Value, "true", FormatStyle::PAS_Left); |
| 151 | IO.enumCase(Value, "false", FormatStyle::PAS_Right); |
| 152 | } |
| 153 | }; |
| 154 | |
| 155 | template <> |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 156 | struct ScalarEnumerationTraits<FormatStyle::SpaceBeforeParensOptions> { |
Manuel Klimek | a8eb914 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 157 | static void enumeration(IO &IO, |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 158 | FormatStyle::SpaceBeforeParensOptions &Value) { |
| 159 | IO.enumCase(Value, "Never", FormatStyle::SBPO_Never); |
Alexander Kornienko | fdca83d | 2013-12-10 10:18:34 +0000 | [diff] [blame] | 160 | IO.enumCase(Value, "ControlStatements", |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 161 | FormatStyle::SBPO_ControlStatements); |
| 162 | IO.enumCase(Value, "Always", FormatStyle::SBPO_Always); |
Alexander Kornienko | fdca83d | 2013-12-10 10:18:34 +0000 | [diff] [blame] | 163 | |
| 164 | // For backward compatibility. |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 165 | IO.enumCase(Value, "false", FormatStyle::SBPO_Never); |
| 166 | IO.enumCase(Value, "true", FormatStyle::SBPO_ControlStatements); |
Alexander Kornienko | fdca83d | 2013-12-10 10:18:34 +0000 | [diff] [blame] | 167 | } |
| 168 | }; |
| 169 | |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 170 | template <> struct MappingTraits<FormatStyle> { |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 171 | static void mapping(IO &IO, FormatStyle &Style) { |
| 172 | // When reading, read the language first, we need it for getPredefinedStyle. |
| 173 | IO.mapOptional("Language", Style.Language); |
| 174 | |
Alexander Kornienko | 4914967 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 175 | if (IO.outputting()) { |
Jacques Pienaar | fc27511 | 2015-02-18 23:48:37 +0000 | [diff] [blame] | 176 | StringRef StylesArray[] = {"LLVM", "Google", "Chromium", |
| 177 | "Mozilla", "WebKit", "GNU"}; |
Alexander Kornienko | 4914967 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 178 | ArrayRef<StringRef> Styles(StylesArray); |
| 179 | for (size_t i = 0, e = Styles.size(); i < e; ++i) { |
| 180 | StringRef StyleName(Styles[i]); |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 181 | FormatStyle PredefinedStyle; |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 182 | if (getPredefinedStyle(StyleName, Style.Language, &PredefinedStyle) && |
Alexander Kornienko | 006b5c8 | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 183 | Style == PredefinedStyle) { |
Alexander Kornienko | 4914967 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 184 | IO.mapOptional("# BasedOnStyle", StyleName); |
| 185 | break; |
| 186 | } |
| 187 | } |
| 188 | } else { |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 189 | StringRef BasedOnStyle; |
| 190 | IO.mapOptional("BasedOnStyle", BasedOnStyle); |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 191 | if (!BasedOnStyle.empty()) { |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 192 | FormatStyle::LanguageKind OldLanguage = Style.Language; |
| 193 | FormatStyle::LanguageKind Language = |
| 194 | ((FormatStyle *)IO.getContext())->Language; |
| 195 | if (!getPredefinedStyle(BasedOnStyle, Language, &Style)) { |
Alexander Kornienko | 006b5c8 | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 196 | IO.setError(Twine("Unknown value for BasedOnStyle: ", BasedOnStyle)); |
| 197 | return; |
| 198 | } |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 199 | Style.Language = OldLanguage; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 200 | } |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Birunthan Mohanathas | 50a6f91 | 2015-06-28 14:52:34 +0000 | [diff] [blame] | 203 | // For backward compatibility. |
| 204 | if (!IO.outputting()) { |
| 205 | IO.mapOptional("DerivePointerBinding", Style.DerivePointerAlignment); |
| 206 | IO.mapOptional("IndentFunctionDeclarationAfterType", |
| 207 | Style.IndentWrappedFunctionNames); |
| 208 | IO.mapOptional("PointerBindsToType", Style.PointerAlignment); |
| 209 | IO.mapOptional("SpaceAfterControlStatementKeyword", |
| 210 | Style.SpaceBeforeParens); |
| 211 | } |
| 212 | |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 213 | IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset); |
Daniel Jasper | 3aa9a6a | 2014-11-18 23:55:27 +0000 | [diff] [blame] | 214 | IO.mapOptional("AlignAfterOpenBracket", Style.AlignAfterOpenBracket); |
Birunthan Mohanathas | 35cfbd7 | 2015-06-28 14:51:17 +0000 | [diff] [blame] | 215 | IO.mapOptional("AlignConsecutiveAssignments", |
| 216 | Style.AlignConsecutiveAssignments); |
Daniel Jasper | e12597c | 2015-10-01 10:06:54 +0000 | [diff] [blame] | 217 | IO.mapOptional("AlignConsecutiveDeclarations", |
| 218 | Style.AlignConsecutiveDeclarations); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 219 | IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft); |
Daniel Jasper | 3219e43 | 2014-12-02 13:24:51 +0000 | [diff] [blame] | 220 | IO.mapOptional("AlignOperands", Style.AlignOperands); |
Daniel Jasper | 552f4a7 | 2013-07-31 23:55:15 +0000 | [diff] [blame] | 221 | IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 222 | IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine", |
| 223 | Style.AllowAllParametersOfDeclarationOnNextLine); |
Daniel Jasper | 17605d3 | 2014-05-14 09:33:35 +0000 | [diff] [blame] | 224 | IO.mapOptional("AllowShortBlocksOnASingleLine", |
| 225 | Style.AllowShortBlocksOnASingleLine); |
Daniel Jasper | b87899b | 2014-09-10 13:11:45 +0000 | [diff] [blame] | 226 | IO.mapOptional("AllowShortCaseLabelsOnASingleLine", |
| 227 | Style.AllowShortCaseLabelsOnASingleLine); |
Birunthan Mohanathas | 35cfbd7 | 2015-06-28 14:51:17 +0000 | [diff] [blame] | 228 | IO.mapOptional("AllowShortFunctionsOnASingleLine", |
| 229 | Style.AllowShortFunctionsOnASingleLine); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 230 | IO.mapOptional("AllowShortIfStatementsOnASingleLine", |
| 231 | Style.AllowShortIfStatementsOnASingleLine); |
Daniel Jasper | 3a685df | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 232 | IO.mapOptional("AllowShortLoopsOnASingleLine", |
| 233 | Style.AllowShortLoopsOnASingleLine); |
Daniel Jasper | ca4ea1c | 2014-08-05 12:16:31 +0000 | [diff] [blame] | 234 | IO.mapOptional("AlwaysBreakAfterDefinitionReturnType", |
| 235 | Style.AlwaysBreakAfterDefinitionReturnType); |
Alexander Kornienko | 5861171 | 2013-07-04 12:02:44 +0000 | [diff] [blame] | 236 | IO.mapOptional("AlwaysBreakBeforeMultilineStrings", |
| 237 | Style.AlwaysBreakBeforeMultilineStrings); |
Birunthan Mohanathas | 35cfbd7 | 2015-06-28 14:51:17 +0000 | [diff] [blame] | 238 | IO.mapOptional("AlwaysBreakTemplateDeclarations", |
| 239 | Style.AlwaysBreakTemplateDeclarations); |
| 240 | IO.mapOptional("BinPackArguments", Style.BinPackArguments); |
| 241 | IO.mapOptional("BinPackParameters", Style.BinPackParameters); |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 242 | IO.mapOptional("BraceWrapping", Style.BraceWrapping); |
Daniel Jasper | e33d4af | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 243 | IO.mapOptional("BreakBeforeBinaryOperators", |
| 244 | Style.BreakBeforeBinaryOperators); |
Birunthan Mohanathas | 35cfbd7 | 2015-06-28 14:51:17 +0000 | [diff] [blame] | 245 | IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces); |
Daniel Jasper | 165b29e | 2013-11-08 00:57:11 +0000 | [diff] [blame] | 246 | IO.mapOptional("BreakBeforeTernaryOperators", |
| 247 | Style.BreakBeforeTernaryOperators); |
Daniel Jasper | e33d4af | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 248 | IO.mapOptional("BreakConstructorInitializersBeforeComma", |
| 249 | Style.BreakConstructorInitializersBeforeComma); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 250 | IO.mapOptional("ColumnLimit", Style.ColumnLimit); |
Birunthan Mohanathas | 35cfbd7 | 2015-06-28 14:51:17 +0000 | [diff] [blame] | 251 | IO.mapOptional("CommentPragmas", Style.CommentPragmas); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 252 | IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine", |
| 253 | Style.ConstructorInitializerAllOnOneLineOrOnePerLine); |
Daniel Jasper | 50d634b | 2014-10-28 16:53:38 +0000 | [diff] [blame] | 254 | IO.mapOptional("ConstructorInitializerIndentWidth", |
| 255 | Style.ConstructorInitializerIndentWidth); |
Birunthan Mohanathas | 35cfbd7 | 2015-06-28 14:51:17 +0000 | [diff] [blame] | 256 | IO.mapOptional("ContinuationIndentWidth", Style.ContinuationIndentWidth); |
| 257 | IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle); |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 258 | IO.mapOptional("DerivePointerAlignment", Style.DerivePointerAlignment); |
Birunthan Mohanathas | 35cfbd7 | 2015-06-28 14:51:17 +0000 | [diff] [blame] | 259 | IO.mapOptional("DisableFormat", Style.DisableFormat); |
Daniel Jasper | b10cbc4 | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 260 | IO.mapOptional("ExperimentalAutoDetectBinPacking", |
| 261 | Style.ExperimentalAutoDetectBinPacking); |
Birunthan Mohanathas | 35cfbd7 | 2015-06-28 14:51:17 +0000 | [diff] [blame] | 262 | IO.mapOptional("ForEachMacros", Style.ForEachMacros); |
Daniel Jasper | 8ce1b8d | 2015-10-06 11:54:18 +0000 | [diff] [blame] | 263 | IO.mapOptional("IncludeCategories", Style.IncludeCategories); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 264 | IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels); |
Birunthan Mohanathas | 35cfbd7 | 2015-06-28 14:51:17 +0000 | [diff] [blame] | 265 | IO.mapOptional("IndentWidth", Style.IndentWidth); |
| 266 | IO.mapOptional("IndentWrappedFunctionNames", |
| 267 | Style.IndentWrappedFunctionNames); |
Daniel Jasper | a26fc5c | 2014-03-21 13:43:14 +0000 | [diff] [blame] | 268 | IO.mapOptional("KeepEmptyLinesAtTheStartOfBlocks", |
| 269 | Style.KeepEmptyLinesAtTheStartOfBlocks); |
Birunthan Mohanathas | b001a0b | 2015-07-03 17:25:16 +0000 | [diff] [blame] | 270 | IO.mapOptional("MacroBlockBegin", Style.MacroBlockBegin); |
| 271 | IO.mapOptional("MacroBlockEnd", Style.MacroBlockEnd); |
Birunthan Mohanathas | 35cfbd7 | 2015-06-28 14:51:17 +0000 | [diff] [blame] | 272 | IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep); |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 273 | IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation); |
Daniel Jasper | 50d634b | 2014-10-28 16:53:38 +0000 | [diff] [blame] | 274 | IO.mapOptional("ObjCBlockIndentWidth", Style.ObjCBlockIndentWidth); |
Daniel Jasper | e9beea2 | 2014-01-28 15:20:33 +0000 | [diff] [blame] | 275 | IO.mapOptional("ObjCSpaceAfterProperty", Style.ObjCSpaceAfterProperty); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 276 | IO.mapOptional("ObjCSpaceBeforeProtocolList", |
| 277 | Style.ObjCSpaceBeforeProtocolList); |
Daniel Jasper | 33b909c | 2013-10-25 14:29:37 +0000 | [diff] [blame] | 278 | IO.mapOptional("PenaltyBreakBeforeFirstCallParameter", |
| 279 | Style.PenaltyBreakBeforeFirstCallParameter); |
Alexander Kornienko | dd7ece5 | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 280 | IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment); |
Daniel Jasper | 4e9678f | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 281 | IO.mapOptional("PenaltyBreakFirstLessLess", |
| 282 | Style.PenaltyBreakFirstLessLess); |
Birunthan Mohanathas | 35cfbd7 | 2015-06-28 14:51:17 +0000 | [diff] [blame] | 283 | IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 284 | IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter); |
| 285 | IO.mapOptional("PenaltyReturnTypeOnItsOwnLine", |
| 286 | Style.PenaltyReturnTypeOnItsOwnLine); |
Daniel Jasper | da44677 | 2015-11-16 12:38:56 +0000 | [diff] [blame] | 287 | IO.mapOptional("SortIncludes", Style.SortIncludes); |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 288 | IO.mapOptional("PointerAlignment", Style.PointerAlignment); |
Daniel Jasper | db986eb | 2014-09-03 07:37:29 +0000 | [diff] [blame] | 289 | IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast); |
Daniel Jasper | d94bff3 | 2013-09-25 15:15:02 +0000 | [diff] [blame] | 290 | IO.mapOptional("SpaceBeforeAssignmentOperators", |
| 291 | Style.SpaceBeforeAssignmentOperators); |
Birunthan Mohanathas | 35cfbd7 | 2015-06-28 14:51:17 +0000 | [diff] [blame] | 292 | IO.mapOptional("SpaceBeforeParens", Style.SpaceBeforeParens); |
| 293 | IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses); |
| 294 | IO.mapOptional("SpacesBeforeTrailingComments", |
| 295 | Style.SpacesBeforeTrailingComments); |
| 296 | IO.mapOptional("SpacesInAngles", Style.SpacesInAngles); |
| 297 | IO.mapOptional("SpacesInContainerLiterals", |
| 298 | Style.SpacesInContainerLiterals); |
| 299 | IO.mapOptional("SpacesInCStyleCastParentheses", |
| 300 | Style.SpacesInCStyleCastParentheses); |
| 301 | IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses); |
| 302 | IO.mapOptional("SpacesInSquareBrackets", Style.SpacesInSquareBrackets); |
| 303 | IO.mapOptional("Standard", Style.Standard); |
| 304 | IO.mapOptional("TabWidth", Style.TabWidth); |
| 305 | IO.mapOptional("UseTab", Style.UseTab); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 306 | } |
| 307 | }; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 308 | |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 309 | template <> struct MappingTraits<FormatStyle::BraceWrappingFlags> { |
| 310 | static void mapping(IO &IO, FormatStyle::BraceWrappingFlags &Wrapping) { |
| 311 | IO.mapOptional("AfterClass", Wrapping.AfterClass); |
| 312 | IO.mapOptional("AfterControlStatement", Wrapping.AfterControlStatement); |
| 313 | IO.mapOptional("AfterEnum", Wrapping.AfterEnum); |
| 314 | IO.mapOptional("AfterFunction", Wrapping.AfterFunction); |
| 315 | IO.mapOptional("AfterNamespace", Wrapping.AfterNamespace); |
| 316 | IO.mapOptional("AfterObjCDeclaration", Wrapping.AfterObjCDeclaration); |
| 317 | IO.mapOptional("AfterStruct", Wrapping.AfterStruct); |
| 318 | IO.mapOptional("AfterUnion", Wrapping.AfterUnion); |
| 319 | IO.mapOptional("BeforeCatch", Wrapping.BeforeCatch); |
| 320 | IO.mapOptional("BeforeElse", Wrapping.BeforeElse); |
| 321 | IO.mapOptional("IndentBraces", Wrapping.IndentBraces); |
| 322 | } |
| 323 | }; |
| 324 | |
Daniel Jasper | 8ce1b8d | 2015-10-06 11:54:18 +0000 | [diff] [blame] | 325 | template <> struct MappingTraits<FormatStyle::IncludeCategory> { |
| 326 | static void mapping(IO &IO, FormatStyle::IncludeCategory &Category) { |
| 327 | IO.mapOptional("Regex", Category.Regex); |
| 328 | IO.mapOptional("Priority", Category.Priority); |
| 329 | } |
| 330 | }; |
| 331 | |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 332 | // Allows to read vector<FormatStyle> while keeping default values. |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 333 | // IO.getContext() should contain a pointer to the FormatStyle structure, that |
| 334 | // will be used to get default values for missing keys. |
| 335 | // If the first element has no Language specified, it will be treated as the |
| 336 | // default one for the following elements. |
Jacques Pienaar | fc27511 | 2015-02-18 23:48:37 +0000 | [diff] [blame] | 337 | template <> struct DocumentListTraits<std::vector<FormatStyle>> { |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 338 | static size_t size(IO &IO, std::vector<FormatStyle> &Seq) { |
| 339 | return Seq.size(); |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 340 | } |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 341 | static FormatStyle &element(IO &IO, std::vector<FormatStyle> &Seq, |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 342 | size_t Index) { |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 343 | if (Index >= Seq.size()) { |
| 344 | assert(Index == Seq.size()); |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 345 | FormatStyle Template; |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 346 | if (Seq.size() > 0 && Seq[0].Language == FormatStyle::LK_None) { |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 347 | Template = Seq[0]; |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 348 | } else { |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 349 | Template = *((const FormatStyle *)IO.getContext()); |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 350 | Template.Language = FormatStyle::LK_None; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 351 | } |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 352 | Seq.resize(Index + 1, Template); |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 353 | } |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 354 | return Seq[Index]; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 355 | } |
| 356 | }; |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 357 | } // namespace yaml |
| 358 | } // namespace llvm |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 359 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 360 | namespace clang { |
| 361 | namespace format { |
| 362 | |
Rafael Espindola | 6d0d89b | 2014-06-12 03:31:26 +0000 | [diff] [blame] | 363 | const std::error_category &getParseCategory() { |
Rafael Espindola | d013670 | 2014-06-12 02:50:04 +0000 | [diff] [blame] | 364 | static ParseErrorCategory C; |
| 365 | return C; |
| 366 | } |
| 367 | std::error_code make_error_code(ParseError e) { |
Rafael Espindola | 6d0d89b | 2014-06-12 03:31:26 +0000 | [diff] [blame] | 368 | return std::error_code(static_cast<int>(e), getParseCategory()); |
Rafael Espindola | d013670 | 2014-06-12 02:50:04 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | const char *ParseErrorCategory::name() const LLVM_NOEXCEPT { |
| 372 | return "clang-format.parse_error"; |
| 373 | } |
| 374 | |
| 375 | std::string ParseErrorCategory::message(int EV) const { |
| 376 | switch (static_cast<ParseError>(EV)) { |
| 377 | case ParseError::Success: |
| 378 | return "Success"; |
| 379 | case ParseError::Error: |
| 380 | return "Invalid argument"; |
| 381 | case ParseError::Unsuitable: |
| 382 | return "Unsuitable"; |
| 383 | } |
Saleem Abdulrasool | fbfbaf6 | 2014-06-12 19:33:26 +0000 | [diff] [blame] | 384 | llvm_unreachable("unexpected parse error"); |
Rafael Espindola | d013670 | 2014-06-12 02:50:04 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 387 | static FormatStyle expandPresets(const FormatStyle &Style) { |
Daniel Jasper | 55bbe66 | 2015-10-07 04:06:10 +0000 | [diff] [blame] | 388 | if (Style.BreakBeforeBraces == FormatStyle::BS_Custom) |
| 389 | return Style; |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 390 | FormatStyle Expanded = Style; |
| 391 | Expanded.BraceWrapping = {false, false, false, false, false, false, |
| 392 | false, false, false, false, false}; |
| 393 | switch (Style.BreakBeforeBraces) { |
| 394 | case FormatStyle::BS_Linux: |
| 395 | Expanded.BraceWrapping.AfterClass = true; |
| 396 | Expanded.BraceWrapping.AfterFunction = true; |
| 397 | Expanded.BraceWrapping.AfterNamespace = true; |
| 398 | Expanded.BraceWrapping.BeforeElse = true; |
| 399 | break; |
| 400 | case FormatStyle::BS_Mozilla: |
| 401 | Expanded.BraceWrapping.AfterClass = true; |
| 402 | Expanded.BraceWrapping.AfterEnum = true; |
| 403 | Expanded.BraceWrapping.AfterFunction = true; |
| 404 | Expanded.BraceWrapping.AfterStruct = true; |
| 405 | Expanded.BraceWrapping.AfterUnion = true; |
| 406 | break; |
| 407 | case FormatStyle::BS_Stroustrup: |
| 408 | Expanded.BraceWrapping.AfterFunction = true; |
| 409 | Expanded.BraceWrapping.BeforeCatch = true; |
| 410 | Expanded.BraceWrapping.BeforeElse = true; |
| 411 | break; |
| 412 | case FormatStyle::BS_Allman: |
| 413 | Expanded.BraceWrapping.AfterClass = true; |
| 414 | Expanded.BraceWrapping.AfterControlStatement = true; |
| 415 | Expanded.BraceWrapping.AfterEnum = true; |
| 416 | Expanded.BraceWrapping.AfterFunction = true; |
| 417 | Expanded.BraceWrapping.AfterNamespace = true; |
| 418 | Expanded.BraceWrapping.AfterObjCDeclaration = true; |
| 419 | Expanded.BraceWrapping.AfterStruct = true; |
| 420 | Expanded.BraceWrapping.BeforeCatch = true; |
| 421 | Expanded.BraceWrapping.BeforeElse = true; |
| 422 | break; |
| 423 | case FormatStyle::BS_GNU: |
| 424 | Expanded.BraceWrapping = {true, true, true, true, true, true, |
| 425 | true, true, true, true, true}; |
| 426 | break; |
| 427 | case FormatStyle::BS_WebKit: |
| 428 | Expanded.BraceWrapping.AfterFunction = true; |
Daniel Jasper | 49dbbdb | 2015-11-04 22:49:32 +0000 | [diff] [blame] | 429 | Expanded.BraceWrapping.BeforeElse = true; |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 430 | break; |
| 431 | default: |
| 432 | break; |
| 433 | } |
| 434 | return Expanded; |
| 435 | } |
| 436 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 437 | FormatStyle getLLVMStyle() { |
| 438 | FormatStyle LLVMStyle; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 439 | LLVMStyle.Language = FormatStyle::LK_Cpp; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 440 | LLVMStyle.AccessModifierOffset = -2; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 441 | LLVMStyle.AlignEscapedNewlinesLeft = false; |
Daniel Jasper | 6501f7e | 2015-10-27 12:38:37 +0000 | [diff] [blame] | 442 | LLVMStyle.AlignAfterOpenBracket = FormatStyle::BAS_Align; |
Daniel Jasper | 3219e43 | 2014-12-02 13:24:51 +0000 | [diff] [blame] | 443 | LLVMStyle.AlignOperands = true; |
Daniel Jasper | 552f4a7 | 2013-07-31 23:55:15 +0000 | [diff] [blame] | 444 | LLVMStyle.AlignTrailingComments = true; |
Daniel Jasper | a4499133 | 2015-04-29 13:06:49 +0000 | [diff] [blame] | 445 | LLVMStyle.AlignConsecutiveAssignments = false; |
Daniel Jasper | e12597c | 2015-10-01 10:06:54 +0000 | [diff] [blame] | 446 | LLVMStyle.AlignConsecutiveDeclarations = false; |
Daniel Jasper | f7db433 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 447 | LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true; |
Daniel Jasper | d74cf40 | 2014-04-08 12:46:38 +0000 | [diff] [blame] | 448 | LLVMStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; |
Daniel Jasper | 17605d3 | 2014-05-14 09:33:35 +0000 | [diff] [blame] | 449 | LLVMStyle.AllowShortBlocksOnASingleLine = false; |
Daniel Jasper | b87899b | 2014-09-10 13:11:45 +0000 | [diff] [blame] | 450 | LLVMStyle.AllowShortCaseLabelsOnASingleLine = false; |
Daniel Jasper | 1b750ed | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 451 | LLVMStyle.AllowShortIfStatementsOnASingleLine = false; |
Daniel Jasper | 3a685df | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 452 | LLVMStyle.AllowShortLoopsOnASingleLine = false; |
Birunthan Mohanathas | a0388a8 | 2015-06-29 15:30:42 +0000 | [diff] [blame] | 453 | LLVMStyle.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_None; |
Alexander Kornienko | 5861171 | 2013-07-04 12:02:44 +0000 | [diff] [blame] | 454 | LLVMStyle.AlwaysBreakBeforeMultilineStrings = false; |
Daniel Jasper | e33d4af | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 455 | LLVMStyle.AlwaysBreakTemplateDeclarations = false; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 456 | LLVMStyle.BinPackParameters = true; |
Daniel Jasper | 18210d7 | 2014-10-09 09:52:05 +0000 | [diff] [blame] | 457 | LLVMStyle.BinPackArguments = true; |
Daniel Jasper | ac043c9 | 2014-09-15 11:11:00 +0000 | [diff] [blame] | 458 | LLVMStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_None; |
Daniel Jasper | 165b29e | 2013-11-08 00:57:11 +0000 | [diff] [blame] | 459 | LLVMStyle.BreakBeforeTernaryOperators = true; |
Daniel Jasper | e33d4af | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 460 | LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach; |
Daniel Jasper | 55bbe66 | 2015-10-07 04:06:10 +0000 | [diff] [blame] | 461 | LLVMStyle.BraceWrapping = {false, false, false, false, false, false, |
| 462 | false, false, false, false, false}; |
Daniel Jasper | e33d4af | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 463 | LLVMStyle.BreakConstructorInitializersBeforeComma = false; |
Nico Weber | 2cd92f1 | 2015-10-15 16:03:01 +0000 | [diff] [blame] | 464 | LLVMStyle.BreakAfterJavaFieldAnnotations = false; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 465 | LLVMStyle.ColumnLimit = 80; |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 466 | LLVMStyle.CommentPragmas = "^ IWYU pragma:"; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 467 | LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false; |
Daniel Jasper | cdaffa4 | 2013-08-13 10:58:30 +0000 | [diff] [blame] | 468 | LLVMStyle.ConstructorInitializerIndentWidth = 4; |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 469 | LLVMStyle.ContinuationIndentWidth = 4; |
Chandler Carruth | f8b7266 | 2014-03-02 12:37:31 +0000 | [diff] [blame] | 470 | LLVMStyle.Cpp11BracedListStyle = true; |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 471 | LLVMStyle.DerivePointerAlignment = false; |
Daniel Jasper | b10cbc4 | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 472 | LLVMStyle.ExperimentalAutoDetectBinPacking = false; |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 473 | LLVMStyle.ForEachMacros.push_back("foreach"); |
| 474 | LLVMStyle.ForEachMacros.push_back("Q_FOREACH"); |
| 475 | LLVMStyle.ForEachMacros.push_back("BOOST_FOREACH"); |
Daniel Jasper | 85c472d | 2015-09-29 07:53:08 +0000 | [diff] [blame] | 476 | LLVMStyle.IncludeCategories = {{"^\"(llvm|llvm-c|clang|clang-c)/", 2}, |
| 477 | {"^(<|\"(gtest|isl|json)/)", 3}, |
| 478 | {".*", 1}}; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 479 | LLVMStyle.IndentCaseLabels = false; |
Daniel Jasper | c75e1ef | 2014-07-09 08:42:42 +0000 | [diff] [blame] | 480 | LLVMStyle.IndentWrappedFunctionNames = false; |
Daniel Jasper | e33d4af | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 481 | LLVMStyle.IndentWidth = 2; |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 482 | LLVMStyle.TabWidth = 8; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 483 | LLVMStyle.MaxEmptyLinesToKeep = 1; |
Daniel Jasper | a26fc5c | 2014-03-21 13:43:14 +0000 | [diff] [blame] | 484 | LLVMStyle.KeepEmptyLinesAtTheStartOfBlocks = true; |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 485 | LLVMStyle.NamespaceIndentation = FormatStyle::NI_None; |
Daniel Jasper | 50d634b | 2014-10-28 16:53:38 +0000 | [diff] [blame] | 486 | LLVMStyle.ObjCBlockIndentWidth = 2; |
Daniel Jasper | e9beea2 | 2014-01-28 15:20:33 +0000 | [diff] [blame] | 487 | LLVMStyle.ObjCSpaceAfterProperty = false; |
Nico Weber | a608775 | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 488 | LLVMStyle.ObjCSpaceBeforeProtocolList = true; |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 489 | LLVMStyle.PointerAlignment = FormatStyle::PAS_Right; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 490 | LLVMStyle.SpacesBeforeTrailingComments = 1; |
Chandler Carruth | f8b7266 | 2014-03-02 12:37:31 +0000 | [diff] [blame] | 491 | LLVMStyle.Standard = FormatStyle::LS_Cpp11; |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 492 | LLVMStyle.UseTab = FormatStyle::UT_Never; |
Daniel Jasper | b55acad | 2013-08-20 12:36:34 +0000 | [diff] [blame] | 493 | LLVMStyle.SpacesInParentheses = false; |
Daniel Jasper | ad981f8 | 2014-08-26 11:41:14 +0000 | [diff] [blame] | 494 | LLVMStyle.SpacesInSquareBrackets = false; |
Daniel Jasper | b55acad | 2013-08-20 12:36:34 +0000 | [diff] [blame] | 495 | LLVMStyle.SpaceInEmptyParentheses = false; |
Daniel Jasper | b2e10a5 | 2014-01-15 15:09:08 +0000 | [diff] [blame] | 496 | LLVMStyle.SpacesInContainerLiterals = true; |
Daniel Jasper | b55acad | 2013-08-20 12:36:34 +0000 | [diff] [blame] | 497 | LLVMStyle.SpacesInCStyleCastParentheses = false; |
Daniel Jasper | db986eb | 2014-09-03 07:37:29 +0000 | [diff] [blame] | 498 | LLVMStyle.SpaceAfterCStyleCast = false; |
Alexander Kornienko | fdca83d | 2013-12-10 10:18:34 +0000 | [diff] [blame] | 499 | LLVMStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements; |
Daniel Jasper | d94bff3 | 2013-09-25 15:15:02 +0000 | [diff] [blame] | 500 | LLVMStyle.SpaceBeforeAssignmentOperators = true; |
Daniel Jasper | dd978ae | 2013-10-29 14:52:02 +0000 | [diff] [blame] | 501 | LLVMStyle.SpacesInAngles = false; |
Daniel Jasper | 4e9678f | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 502 | |
Daniel Jasper | 19a541e | 2013-12-19 16:45:34 +0000 | [diff] [blame] | 503 | LLVMStyle.PenaltyBreakComment = 300; |
Alexander Kornienko | fe7a57f | 2013-12-10 15:42:15 +0000 | [diff] [blame] | 504 | LLVMStyle.PenaltyBreakFirstLessLess = 120; |
| 505 | LLVMStyle.PenaltyBreakString = 1000; |
| 506 | LLVMStyle.PenaltyExcessCharacter = 1000000; |
Daniel Jasper | 4e9678f | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 507 | LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60; |
Daniel Jasper | 33b909c | 2013-10-25 14:29:37 +0000 | [diff] [blame] | 508 | LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19; |
Daniel Jasper | 4e9678f | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 509 | |
Daniel Jasper | c64b09a | 2014-05-22 15:12:22 +0000 | [diff] [blame] | 510 | LLVMStyle.DisableFormat = false; |
Daniel Jasper | da44677 | 2015-11-16 12:38:56 +0000 | [diff] [blame] | 511 | LLVMStyle.SortIncludes = true; |
Daniel Jasper | c64b09a | 2014-05-22 15:12:22 +0000 | [diff] [blame] | 512 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 513 | return LLVMStyle; |
| 514 | } |
| 515 | |
Nico Weber | 514ecc8 | 2014-02-02 20:50:45 +0000 | [diff] [blame] | 516 | FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) { |
Alexander Kornienko | fe7a57f | 2013-12-10 15:42:15 +0000 | [diff] [blame] | 517 | FormatStyle GoogleStyle = getLLVMStyle(); |
Nico Weber | 514ecc8 | 2014-02-02 20:50:45 +0000 | [diff] [blame] | 518 | GoogleStyle.Language = Language; |
| 519 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 520 | GoogleStyle.AccessModifierOffset = -1; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 521 | GoogleStyle.AlignEscapedNewlinesLeft = true; |
Daniel Jasper | 085a2ed | 2013-04-24 13:46:00 +0000 | [diff] [blame] | 522 | GoogleStyle.AllowShortIfStatementsOnASingleLine = true; |
Daniel Jasper | 5bd0b9e | 2013-05-23 18:05:18 +0000 | [diff] [blame] | 523 | GoogleStyle.AllowShortLoopsOnASingleLine = true; |
Alexander Kornienko | 5861171 | 2013-07-04 12:02:44 +0000 | [diff] [blame] | 524 | GoogleStyle.AlwaysBreakBeforeMultilineStrings = true; |
Daniel Jasper | e33d4af | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 525 | GoogleStyle.AlwaysBreakTemplateDeclarations = true; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 526 | GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 527 | GoogleStyle.DerivePointerAlignment = true; |
Daniel Jasper | 85c472d | 2015-09-29 07:53:08 +0000 | [diff] [blame] | 528 | GoogleStyle.IncludeCategories = {{"^<.*\\.h>", 1}, {"^<.*", 2}, {".*", 3}}; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 529 | GoogleStyle.IndentCaseLabels = true; |
Daniel Jasper | a26fc5c | 2014-03-21 13:43:14 +0000 | [diff] [blame] | 530 | GoogleStyle.KeepEmptyLinesAtTheStartOfBlocks = false; |
Daniel Jasper | e9beea2 | 2014-01-28 15:20:33 +0000 | [diff] [blame] | 531 | GoogleStyle.ObjCSpaceAfterProperty = false; |
Nico Weber | a608775 | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 532 | GoogleStyle.ObjCSpaceBeforeProtocolList = false; |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 533 | GoogleStyle.PointerAlignment = FormatStyle::PAS_Left; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 534 | GoogleStyle.SpacesBeforeTrailingComments = 2; |
| 535 | GoogleStyle.Standard = FormatStyle::LS_Auto; |
Daniel Jasper | 4e9678f | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 536 | |
Daniel Jasper | 4e9678f | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 537 | GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200; |
Daniel Jasper | 33b909c | 2013-10-25 14:29:37 +0000 | [diff] [blame] | 538 | GoogleStyle.PenaltyBreakBeforeFirstCallParameter = 1; |
Daniel Jasper | 4e9678f | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 539 | |
Daniel Jasper | c58c70e | 2014-09-15 11:21:46 +0000 | [diff] [blame] | 540 | if (Language == FormatStyle::LK_Java) { |
Daniel Jasper | 6501f7e | 2015-10-27 12:38:37 +0000 | [diff] [blame] | 541 | GoogleStyle.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; |
Daniel Jasper | 3219e43 | 2014-12-02 13:24:51 +0000 | [diff] [blame] | 542 | GoogleStyle.AlignOperands = false; |
Daniel Jasper | fd4ed18 | 2015-01-04 20:40:45 +0000 | [diff] [blame] | 543 | GoogleStyle.AlignTrailingComments = false; |
Daniel Jasper | 9e70935 | 2014-11-26 10:43:58 +0000 | [diff] [blame] | 544 | GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; |
Daniel Jasper | fd4ed18 | 2015-01-04 20:40:45 +0000 | [diff] [blame] | 545 | GoogleStyle.AllowShortIfStatementsOnASingleLine = false; |
Daniel Jasper | 1cd3c71 | 2015-01-14 12:24:59 +0000 | [diff] [blame] | 546 | GoogleStyle.AlwaysBreakBeforeMultilineStrings = false; |
Daniel Jasper | c58c70e | 2014-09-15 11:21:46 +0000 | [diff] [blame] | 547 | GoogleStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; |
| 548 | GoogleStyle.ColumnLimit = 100; |
| 549 | GoogleStyle.SpaceAfterCStyleCast = true; |
Daniel Jasper | 61d8197 | 2014-11-14 08:22:46 +0000 | [diff] [blame] | 550 | GoogleStyle.SpacesBeforeTrailingComments = 1; |
Daniel Jasper | c58c70e | 2014-09-15 11:21:46 +0000 | [diff] [blame] | 551 | } else if (Language == FormatStyle::LK_JavaScript) { |
Daniel Jasper | 6501f7e | 2015-10-27 12:38:37 +0000 | [diff] [blame] | 552 | GoogleStyle.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; |
| 553 | GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; |
| 554 | GoogleStyle.AlwaysBreakBeforeMultilineStrings = false; |
Daniel Jasper | e551bb7 | 2014-11-05 17:22:31 +0000 | [diff] [blame] | 555 | GoogleStyle.BreakBeforeTernaryOperators = false; |
Daniel Jasper | 8f83a90 | 2014-05-09 10:28:58 +0000 | [diff] [blame] | 556 | GoogleStyle.MaxEmptyLinesToKeep = 3; |
Nico Weber | 514ecc8 | 2014-02-02 20:50:45 +0000 | [diff] [blame] | 557 | GoogleStyle.SpacesInContainerLiterals = false; |
| 558 | } else if (Language == FormatStyle::LK_Proto) { |
Daniel Jasper | d74cf40 | 2014-04-08 12:46:38 +0000 | [diff] [blame] | 559 | GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; |
Daniel Jasper | 783bac6 | 2014-04-15 09:54:30 +0000 | [diff] [blame] | 560 | GoogleStyle.SpacesInContainerLiterals = false; |
Nico Weber | 514ecc8 | 2014-02-02 20:50:45 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 563 | return GoogleStyle; |
| 564 | } |
| 565 | |
Nico Weber | 514ecc8 | 2014-02-02 20:50:45 +0000 | [diff] [blame] | 566 | FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) { |
| 567 | FormatStyle ChromiumStyle = getGoogleStyle(Language); |
Nico Weber | 450425c | 2014-11-26 16:43:18 +0000 | [diff] [blame] | 568 | if (Language == FormatStyle::LK_Java) { |
Daniel Jasper | fd4ed18 | 2015-01-04 20:40:45 +0000 | [diff] [blame] | 569 | ChromiumStyle.AllowShortIfStatementsOnASingleLine = true; |
Nico Weber | 2cd92f1 | 2015-10-15 16:03:01 +0000 | [diff] [blame] | 570 | ChromiumStyle.BreakAfterJavaFieldAnnotations = true; |
Nico Weber | 450425c | 2014-11-26 16:43:18 +0000 | [diff] [blame] | 571 | ChromiumStyle.ContinuationIndentWidth = 8; |
Nico Weber | 2cd92f1 | 2015-10-15 16:03:01 +0000 | [diff] [blame] | 572 | ChromiumStyle.IndentWidth = 4; |
Nico Weber | 450425c | 2014-11-26 16:43:18 +0000 | [diff] [blame] | 573 | } else { |
| 574 | ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false; |
| 575 | ChromiumStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; |
| 576 | ChromiumStyle.AllowShortIfStatementsOnASingleLine = false; |
| 577 | ChromiumStyle.AllowShortLoopsOnASingleLine = false; |
| 578 | ChromiumStyle.BinPackParameters = false; |
| 579 | ChromiumStyle.DerivePointerAlignment = false; |
| 580 | } |
Daniel Jasper | 1b750ed | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 581 | return ChromiumStyle; |
| 582 | } |
| 583 | |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 584 | FormatStyle getMozillaStyle() { |
| 585 | FormatStyle MozillaStyle = getLLVMStyle(); |
| 586 | MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false; |
Birunthan Mohanathas | a081002 | 2015-06-29 15:18:58 +0000 | [diff] [blame] | 587 | MozillaStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; |
Birunthan Mohanathas | a0388a8 | 2015-06-29 15:30:42 +0000 | [diff] [blame] | 588 | MozillaStyle.AlwaysBreakAfterDefinitionReturnType = |
| 589 | FormatStyle::DRTBS_TopLevel; |
Birunthan Mohanathas | a081002 | 2015-06-29 15:18:58 +0000 | [diff] [blame] | 590 | MozillaStyle.AlwaysBreakTemplateDeclarations = true; |
Birunthan Mohanathas | 305fa9c | 2015-07-12 03:13:54 +0000 | [diff] [blame] | 591 | MozillaStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; |
Birunthan Mohanathas | a081002 | 2015-06-29 15:18:58 +0000 | [diff] [blame] | 592 | MozillaStyle.BreakConstructorInitializersBeforeComma = true; |
| 593 | MozillaStyle.ConstructorInitializerIndentWidth = 2; |
| 594 | MozillaStyle.ContinuationIndentWidth = 2; |
Chandler Carruth | f8b7266 | 2014-03-02 12:37:31 +0000 | [diff] [blame] | 595 | MozillaStyle.Cpp11BracedListStyle = false; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 596 | MozillaStyle.IndentCaseLabels = true; |
Daniel Jasper | e9beea2 | 2014-01-28 15:20:33 +0000 | [diff] [blame] | 597 | MozillaStyle.ObjCSpaceAfterProperty = true; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 598 | MozillaStyle.ObjCSpaceBeforeProtocolList = false; |
| 599 | MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200; |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 600 | MozillaStyle.PointerAlignment = FormatStyle::PAS_Left; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 601 | return MozillaStyle; |
| 602 | } |
| 603 | |
Daniel Jasper | ffefb3d | 2013-07-24 13:10:59 +0000 | [diff] [blame] | 604 | FormatStyle getWebKitStyle() { |
| 605 | FormatStyle Style = getLLVMStyle(); |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 606 | Style.AccessModifierOffset = -4; |
Daniel Jasper | 6501f7e | 2015-10-27 12:38:37 +0000 | [diff] [blame] | 607 | Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; |
Daniel Jasper | 3219e43 | 2014-12-02 13:24:51 +0000 | [diff] [blame] | 608 | Style.AlignOperands = false; |
Daniel Jasper | 552f4a7 | 2013-07-31 23:55:15 +0000 | [diff] [blame] | 609 | Style.AlignTrailingComments = false; |
Daniel Jasper | ac043c9 | 2014-09-15 11:11:00 +0000 | [diff] [blame] | 610 | Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; |
Roman Kashitsyn | 291f64f | 2015-08-10 13:43:19 +0000 | [diff] [blame] | 611 | Style.BreakBeforeBraces = FormatStyle::BS_WebKit; |
Daniel Jasper | e33d4af | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 612 | Style.BreakConstructorInitializersBeforeComma = true; |
Chandler Carruth | f8b7266 | 2014-03-02 12:37:31 +0000 | [diff] [blame] | 613 | Style.Cpp11BracedListStyle = false; |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 614 | Style.ColumnLimit = 0; |
Daniel Jasper | e33d4af | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 615 | Style.IndentWidth = 4; |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 616 | Style.NamespaceIndentation = FormatStyle::NI_Inner; |
Daniel Jasper | 50d634b | 2014-10-28 16:53:38 +0000 | [diff] [blame] | 617 | Style.ObjCBlockIndentWidth = 4; |
Daniel Jasper | e9beea2 | 2014-01-28 15:20:33 +0000 | [diff] [blame] | 618 | Style.ObjCSpaceAfterProperty = true; |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 619 | Style.PointerAlignment = FormatStyle::PAS_Left; |
Chandler Carruth | f8b7266 | 2014-03-02 12:37:31 +0000 | [diff] [blame] | 620 | Style.Standard = FormatStyle::LS_Cpp03; |
Daniel Jasper | ffefb3d | 2013-07-24 13:10:59 +0000 | [diff] [blame] | 621 | return Style; |
| 622 | } |
| 623 | |
Alexander Kornienko | fe7a57f | 2013-12-10 15:42:15 +0000 | [diff] [blame] | 624 | FormatStyle getGNUStyle() { |
| 625 | FormatStyle Style = getLLVMStyle(); |
Birunthan Mohanathas | a0388a8 | 2015-06-29 15:30:42 +0000 | [diff] [blame] | 626 | Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; |
Daniel Jasper | ac043c9 | 2014-09-15 11:11:00 +0000 | [diff] [blame] | 627 | Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 628 | Style.BreakBeforeBraces = FormatStyle::BS_GNU; |
Alexander Kornienko | fe7a57f | 2013-12-10 15:42:15 +0000 | [diff] [blame] | 629 | Style.BreakBeforeTernaryOperators = true; |
Chandler Carruth | f8b7266 | 2014-03-02 12:37:31 +0000 | [diff] [blame] | 630 | Style.Cpp11BracedListStyle = false; |
Alexander Kornienko | fe7a57f | 2013-12-10 15:42:15 +0000 | [diff] [blame] | 631 | Style.ColumnLimit = 79; |
Alexander Kornienko | fe7a57f | 2013-12-10 15:42:15 +0000 | [diff] [blame] | 632 | Style.SpaceBeforeParens = FormatStyle::SBPO_Always; |
Chandler Carruth | f8b7266 | 2014-03-02 12:37:31 +0000 | [diff] [blame] | 633 | Style.Standard = FormatStyle::LS_Cpp03; |
Alexander Kornienko | fe7a57f | 2013-12-10 15:42:15 +0000 | [diff] [blame] | 634 | return Style; |
| 635 | } |
| 636 | |
Daniel Jasper | c64b09a | 2014-05-22 15:12:22 +0000 | [diff] [blame] | 637 | FormatStyle getNoStyle() { |
| 638 | FormatStyle NoStyle = getLLVMStyle(); |
| 639 | NoStyle.DisableFormat = true; |
Daniel Jasper | da44677 | 2015-11-16 12:38:56 +0000 | [diff] [blame] | 640 | NoStyle.SortIncludes = false; |
Daniel Jasper | c64b09a | 2014-05-22 15:12:22 +0000 | [diff] [blame] | 641 | return NoStyle; |
| 642 | } |
| 643 | |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 644 | bool getPredefinedStyle(StringRef Name, FormatStyle::LanguageKind Language, |
| 645 | FormatStyle *Style) { |
| 646 | if (Name.equals_lower("llvm")) { |
Alexander Kornienko | 006b5c8 | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 647 | *Style = getLLVMStyle(); |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 648 | } else if (Name.equals_lower("chromium")) { |
Nico Weber | 514ecc8 | 2014-02-02 20:50:45 +0000 | [diff] [blame] | 649 | *Style = getChromiumStyle(Language); |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 650 | } else if (Name.equals_lower("mozilla")) { |
Alexander Kornienko | 006b5c8 | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 651 | *Style = getMozillaStyle(); |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 652 | } else if (Name.equals_lower("google")) { |
Nico Weber | 514ecc8 | 2014-02-02 20:50:45 +0000 | [diff] [blame] | 653 | *Style = getGoogleStyle(Language); |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 654 | } else if (Name.equals_lower("webkit")) { |
Daniel Jasper | ffefb3d | 2013-07-24 13:10:59 +0000 | [diff] [blame] | 655 | *Style = getWebKitStyle(); |
Alexander Kornienko | fe7a57f | 2013-12-10 15:42:15 +0000 | [diff] [blame] | 656 | } else if (Name.equals_lower("gnu")) { |
| 657 | *Style = getGNUStyle(); |
Daniel Jasper | c64b09a | 2014-05-22 15:12:22 +0000 | [diff] [blame] | 658 | } else if (Name.equals_lower("none")) { |
| 659 | *Style = getNoStyle(); |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 660 | } else { |
Alexander Kornienko | 006b5c8 | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 661 | return false; |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 662 | } |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 663 | |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 664 | Style->Language = Language; |
Alexander Kornienko | 006b5c8 | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 665 | return true; |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Rafael Espindola | c080917 | 2014-06-12 14:02:15 +0000 | [diff] [blame] | 668 | std::error_code parseConfiguration(StringRef Text, FormatStyle *Style) { |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 669 | assert(Style); |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 670 | FormatStyle::LanguageKind Language = Style->Language; |
| 671 | assert(Language != FormatStyle::LK_None); |
Alexander Kornienko | 06e0033 | 2013-05-20 15:18:01 +0000 | [diff] [blame] | 672 | if (Text.trim().empty()) |
Rafael Espindola | d013670 | 2014-06-12 02:50:04 +0000 | [diff] [blame] | 673 | return make_error_code(ParseError::Error); |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 674 | |
| 675 | std::vector<FormatStyle> Styles; |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 676 | llvm::yaml::Input Input(Text); |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 677 | // DocumentListTraits<vector<FormatStyle>> uses the context to get default |
| 678 | // values for the fields, keys for which are missing from the configuration. |
| 679 | // Mapping also uses the context to get the language to find the correct |
| 680 | // base style. |
| 681 | Input.setContext(Style); |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 682 | Input >> Styles; |
| 683 | if (Input.error()) |
| 684 | return Input.error(); |
| 685 | |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 686 | for (unsigned i = 0; i < Styles.size(); ++i) { |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 687 | // Ensures that only the first configuration can skip the Language option. |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 688 | if (Styles[i].Language == FormatStyle::LK_None && i != 0) |
Rafael Espindola | d013670 | 2014-06-12 02:50:04 +0000 | [diff] [blame] | 689 | return make_error_code(ParseError::Error); |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 690 | // Ensure that each language is configured at most once. |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 691 | for (unsigned j = 0; j < i; ++j) { |
| 692 | if (Styles[i].Language == Styles[j].Language) { |
| 693 | DEBUG(llvm::dbgs() |
| 694 | << "Duplicate languages in the config file on positions " << j |
| 695 | << " and " << i << "\n"); |
Rafael Espindola | d013670 | 2014-06-12 02:50:04 +0000 | [diff] [blame] | 696 | return make_error_code(ParseError::Error); |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 697 | } |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 698 | } |
| 699 | } |
| 700 | // Look for a suitable configuration starting from the end, so we can |
| 701 | // find the configuration for the specific language first, and the default |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 702 | // configuration (which can only be at slot 0) after it. |
| 703 | for (int i = Styles.size() - 1; i >= 0; --i) { |
| 704 | if (Styles[i].Language == Language || |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 705 | Styles[i].Language == FormatStyle::LK_None) { |
| 706 | *Style = Styles[i]; |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 707 | Style->Language = Language; |
Rafael Espindola | d013670 | 2014-06-12 02:50:04 +0000 | [diff] [blame] | 708 | return make_error_code(ParseError::Success); |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 709 | } |
| 710 | } |
Rafael Espindola | d013670 | 2014-06-12 02:50:04 +0000 | [diff] [blame] | 711 | return make_error_code(ParseError::Unsuitable); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | std::string configurationAsText(const FormatStyle &Style) { |
| 715 | std::string Text; |
| 716 | llvm::raw_string_ostream Stream(Text); |
| 717 | llvm::yaml::Output Output(Stream); |
| 718 | // We use the same mapping method for input and output, so we need a non-const |
| 719 | // reference here. |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 720 | FormatStyle NonConstStyle = expandPresets(Style); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 721 | Output << NonConstStyle; |
Alexander Kornienko | 9a38ec2 | 2013-05-13 12:56:35 +0000 | [diff] [blame] | 722 | return Stream.str(); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 723 | } |
| 724 | |
Craig Topper | af35e85 | 2013-06-30 22:29:28 +0000 | [diff] [blame] | 725 | namespace { |
| 726 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 727 | class FormatTokenLexer { |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 728 | public: |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 729 | FormatTokenLexer(SourceManager &SourceMgr, FileID ID, FormatStyle &Style, |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 730 | encoding::Encoding Encoding) |
Craig Topper | 2145bc0 | 2014-05-09 08:15:10 +0000 | [diff] [blame] | 731 | : FormatTok(nullptr), IsFirstToken(true), GreaterStashed(false), |
Jacques Pienaar | fc27511 | 2015-02-18 23:48:37 +0000 | [diff] [blame] | 732 | LessStashed(false), Column(0), TrailingWhitespace(0), |
| 733 | SourceMgr(SourceMgr), ID(ID), Style(Style), |
| 734 | IdentTable(getFormattingLangOpts(Style)), Keywords(IdentTable), |
Birunthan Mohanathas | b001a0b | 2015-07-03 17:25:16 +0000 | [diff] [blame] | 735 | Encoding(Encoding), FirstInLineIndex(0), FormattingDisabled(false), |
| 736 | MacroBlockBeginRegex(Style.MacroBlockBegin), |
| 737 | MacroBlockEndRegex(Style.MacroBlockEnd) { |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 738 | Lex.reset(new Lexer(ID, SourceMgr.getBuffer(ID), SourceMgr, |
| 739 | getFormattingLangOpts(Style))); |
| 740 | Lex->SetKeepWhitespaceMode(true); |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 741 | |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 742 | for (const std::string &ForEachMacro : Style.ForEachMacros) |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 743 | ForEachMacros.push_back(&IdentTable.get(ForEachMacro)); |
| 744 | std::sort(ForEachMacros.begin(), ForEachMacros.end()); |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 747 | ArrayRef<FormatToken *> lex() { |
| 748 | assert(Tokens.empty()); |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 749 | assert(FirstInLineIndex == 0); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 750 | do { |
| 751 | Tokens.push_back(getNextToken()); |
Daniel Jasper | 265309e | 2015-10-18 07:02:28 +0000 | [diff] [blame] | 752 | if (Style.Language == FormatStyle::LK_JavaScript) |
| 753 | tryParseJSRegexLiteral(); |
Alexander Kornienko | 9aa6240 | 2013-11-21 12:43:57 +0000 | [diff] [blame] | 754 | tryMergePreviousTokens(); |
Daniel Jasper | a0ef4f3 | 2015-02-20 13:47:38 +0000 | [diff] [blame] | 755 | if (Tokens.back()->NewlinesBefore > 0 || Tokens.back()->IsMultiline) |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 756 | FirstInLineIndex = Tokens.size() - 1; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 757 | } while (Tokens.back()->Tok.isNot(tok::eof)); |
| 758 | return Tokens; |
| 759 | } |
| 760 | |
Daniel Jasper | d0ec0d6 | 2014-11-04 12:41:02 +0000 | [diff] [blame] | 761 | const AdditionalKeywords &getKeywords() { return Keywords; } |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 762 | |
| 763 | private: |
Alexander Kornienko | 9aa6240 | 2013-11-21 12:43:57 +0000 | [diff] [blame] | 764 | void tryMergePreviousTokens() { |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 765 | if (tryMerge_TMacro()) |
| 766 | return; |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 767 | if (tryMergeConflictMarkers()) |
| 768 | return; |
Jacques Pienaar | fc27511 | 2015-02-18 23:48:37 +0000 | [diff] [blame] | 769 | if (tryMergeLessLess()) |
| 770 | return; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 771 | |
| 772 | if (Style.Language == FormatStyle::LK_JavaScript) { |
Daniel Jasper | a0ef4f3 | 2015-02-20 13:47:38 +0000 | [diff] [blame] | 773 | if (tryMergeTemplateString()) |
| 774 | return; |
Daniel Jasper | f9ae312 | 2014-05-08 07:01:45 +0000 | [diff] [blame] | 775 | |
Benjamin Kramer | 28b45ce | 2015-03-08 16:06:46 +0000 | [diff] [blame] | 776 | static const tok::TokenKind JSIdentity[] = {tok::equalequal, tok::equal}; |
| 777 | static const tok::TokenKind JSNotIdentity[] = {tok::exclaimequal, |
| 778 | tok::equal}; |
| 779 | static const tok::TokenKind JSShiftEqual[] = {tok::greater, tok::greater, |
| 780 | tok::greaterequal}; |
| 781 | static const tok::TokenKind JSRightArrow[] = {tok::equal, tok::greater}; |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame] | 782 | // FIXME: Investigate what token type gives the correct operator priority. |
| 783 | if (tryMergeTokens(JSIdentity, TT_BinaryOperator)) |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 784 | return; |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame] | 785 | if (tryMergeTokens(JSNotIdentity, TT_BinaryOperator)) |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 786 | return; |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame] | 787 | if (tryMergeTokens(JSShiftEqual, TT_BinaryOperator)) |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 788 | return; |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame] | 789 | if (tryMergeTokens(JSRightArrow, TT_JsFatArrow)) |
Daniel Jasper | 7821439 | 2014-05-19 07:27:02 +0000 | [diff] [blame] | 790 | return; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 791 | } |
Alexander Kornienko | 9aa6240 | 2013-11-21 12:43:57 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Jacques Pienaar | fc27511 | 2015-02-18 23:48:37 +0000 | [diff] [blame] | 794 | bool tryMergeLessLess() { |
| 795 | // Merge X,less,less,Y into X,lessless,Y unless X or Y is less. |
Jacques Pienaar | 68a7dbf | 2015-02-20 21:09:01 +0000 | [diff] [blame] | 796 | if (Tokens.size() < 3) |
| 797 | return false; |
Jacques Pienaar | fc27511 | 2015-02-18 23:48:37 +0000 | [diff] [blame] | 798 | |
Jacques Pienaar | 68a7dbf | 2015-02-20 21:09:01 +0000 | [diff] [blame] | 799 | bool FourthTokenIsLess = false; |
| 800 | if (Tokens.size() > 3) |
| 801 | FourthTokenIsLess = (Tokens.end() - 4)[0]->is(tok::less); |
Jacques Pienaar | fc27511 | 2015-02-18 23:48:37 +0000 | [diff] [blame] | 802 | |
Jacques Pienaar | 68a7dbf | 2015-02-20 21:09:01 +0000 | [diff] [blame] | 803 | auto First = Tokens.end() - 3; |
| 804 | if (First[2]->is(tok::less) || First[1]->isNot(tok::less) || |
| 805 | First[0]->isNot(tok::less) || FourthTokenIsLess) |
Jacques Pienaar | fc27511 | 2015-02-18 23:48:37 +0000 | [diff] [blame] | 806 | return false; |
| 807 | |
| 808 | // Only merge if there currently is no whitespace between the two "<". |
Jacques Pienaar | 68a7dbf | 2015-02-20 21:09:01 +0000 | [diff] [blame] | 809 | if (First[1]->WhitespaceRange.getBegin() != |
| 810 | First[1]->WhitespaceRange.getEnd()) |
Jacques Pienaar | fc27511 | 2015-02-18 23:48:37 +0000 | [diff] [blame] | 811 | return false; |
| 812 | |
Jacques Pienaar | 68a7dbf | 2015-02-20 21:09:01 +0000 | [diff] [blame] | 813 | First[0]->Tok.setKind(tok::lessless); |
| 814 | First[0]->TokenText = "<<"; |
| 815 | First[0]->ColumnWidth += 1; |
Jacques Pienaar | fc27511 | 2015-02-18 23:48:37 +0000 | [diff] [blame] | 816 | Tokens.erase(Tokens.end() - 2); |
| 817 | return true; |
| 818 | } |
| 819 | |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame] | 820 | bool tryMergeTokens(ArrayRef<tok::TokenKind> Kinds, TokenType NewType) { |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 821 | if (Tokens.size() < Kinds.size()) |
Alexander Kornienko | 9aa6240 | 2013-11-21 12:43:57 +0000 | [diff] [blame] | 822 | return false; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 823 | |
| 824 | SmallVectorImpl<FormatToken *>::const_iterator First = |
| 825 | Tokens.end() - Kinds.size(); |
| 826 | if (!First[0]->is(Kinds[0])) |
Alexander Kornienko | 9aa6240 | 2013-11-21 12:43:57 +0000 | [diff] [blame] | 827 | return false; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 828 | unsigned AddLength = 0; |
| 829 | for (unsigned i = 1; i < Kinds.size(); ++i) { |
Jacques Pienaar | fc27511 | 2015-02-18 23:48:37 +0000 | [diff] [blame] | 830 | if (!First[i]->is(Kinds[i]) || |
| 831 | First[i]->WhitespaceRange.getBegin() != |
| 832 | First[i]->WhitespaceRange.getEnd()) |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 833 | return false; |
| 834 | AddLength += First[i]->TokenText.size(); |
| 835 | } |
| 836 | Tokens.resize(Tokens.size() - Kinds.size() + 1); |
| 837 | First[0]->TokenText = StringRef(First[0]->TokenText.data(), |
| 838 | First[0]->TokenText.size() + AddLength); |
| 839 | First[0]->ColumnWidth += AddLength; |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame] | 840 | First[0]->Type = NewType; |
Alexander Kornienko | 9aa6240 | 2013-11-21 12:43:57 +0000 | [diff] [blame] | 841 | return true; |
| 842 | } |
| 843 | |
Daniel Jasper | 265309e | 2015-10-18 07:02:28 +0000 | [diff] [blame] | 844 | // Returns \c true if \p Tok can only be followed by an operand in JavaScript. |
| 845 | bool precedesOperand(FormatToken *Tok) { |
| 846 | // NB: This is not entirely correct, as an r_paren can introduce an operand |
| 847 | // location in e.g. `if (foo) /bar/.exec(...);`. That is a rare enough |
| 848 | // corner case to not matter in practice, though. |
| 849 | return Tok->isOneOf(tok::period, tok::l_paren, tok::comma, tok::l_brace, |
| 850 | tok::r_brace, tok::l_square, tok::semi, tok::exclaim, |
| 851 | tok::colon, tok::question, tok::tilde) || |
| 852 | Tok->isOneOf(tok::kw_return, tok::kw_do, tok::kw_case, tok::kw_throw, |
| 853 | tok::kw_else, tok::kw_new, tok::kw_delete, tok::kw_void, |
| 854 | tok::kw_typeof, Keywords.kw_instanceof, |
| 855 | Keywords.kw_in) || |
| 856 | Tok->isBinaryOperator(); |
| 857 | } |
| 858 | |
| 859 | bool canPrecedeRegexLiteral(FormatToken *Prev) { |
| 860 | if (!Prev) |
| 861 | return true; |
| 862 | |
| 863 | // Regex literals can only follow after prefix unary operators, not after |
| 864 | // postfix unary operators. If the '++' is followed by a non-operand |
| 865 | // introducing token, the slash here is the operand and not the start of a |
| 866 | // regex. |
| 867 | if (Prev->isOneOf(tok::plusplus, tok::minusminus)) |
| 868 | return (Tokens.size() < 3 || precedesOperand(Tokens[Tokens.size() - 3])); |
| 869 | |
| 870 | // The previous token must introduce an operand location where regex |
| 871 | // literals can occur. |
| 872 | if (!precedesOperand(Prev)) |
Daniel Jasper | fb4333b | 2014-05-12 11:29:50 +0000 | [diff] [blame] | 873 | return false; |
Daniel Jasper | 265309e | 2015-10-18 07:02:28 +0000 | [diff] [blame] | 874 | |
Daniel Jasper | fb4333b | 2014-05-12 11:29:50 +0000 | [diff] [blame] | 875 | return true; |
| 876 | } |
| 877 | |
Daniel Jasper | 265309e | 2015-10-18 07:02:28 +0000 | [diff] [blame] | 878 | // Tries to parse a JavaScript Regex literal starting at the current token, |
| 879 | // if that begins with a slash and is in a location where JavaScript allows |
| 880 | // regex literals. Changes the current token to a regex literal and updates |
| 881 | // its text if successful. |
| 882 | void tryParseJSRegexLiteral() { |
| 883 | FormatToken *RegexToken = Tokens.back(); |
| 884 | if (!RegexToken->isOneOf(tok::slash, tok::slashequal)) |
| 885 | return; |
Daniel Jasper | 6b8d26c | 2015-06-24 16:01:02 +0000 | [diff] [blame] | 886 | |
Daniel Jasper | 265309e | 2015-10-18 07:02:28 +0000 | [diff] [blame] | 887 | FormatToken *Prev = nullptr; |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 888 | for (auto I = Tokens.rbegin() + 1, E = Tokens.rend(); I != E; ++I) { |
Daniel Jasper | 265309e | 2015-10-18 07:02:28 +0000 | [diff] [blame] | 889 | // NB: Because previous pointers are not initialized yet, this cannot use |
| 890 | // Token.getPreviousNonComment. |
| 891 | if ((*I)->isNot(tok::comment)) { |
| 892 | Prev = *I; |
| 893 | break; |
Daniel Jasper | 8d0e223 | 2015-10-12 03:13:48 +0000 | [diff] [blame] | 894 | } |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 895 | } |
Daniel Jasper | 265309e | 2015-10-18 07:02:28 +0000 | [diff] [blame] | 896 | |
| 897 | if (!canPrecedeRegexLiteral(Prev)) |
| 898 | return; |
| 899 | |
| 900 | // 'Manually' lex ahead in the current file buffer. |
| 901 | const char *Offset = Lex->getBufferLocation(); |
| 902 | const char *RegexBegin = Offset - RegexToken->TokenText.size(); |
| 903 | StringRef Buffer = Lex->getBuffer(); |
| 904 | bool InCharacterClass = false; |
| 905 | bool HaveClosingSlash = false; |
| 906 | for (; !HaveClosingSlash && Offset != Buffer.end(); ++Offset) { |
| 907 | // Regular expressions are terminated with a '/', which can only be |
| 908 | // escaped using '\' or a character class between '[' and ']'. |
| 909 | // See http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.5. |
| 910 | switch (*Offset) { |
| 911 | case '\\': |
| 912 | // Skip the escaped character. |
| 913 | ++Offset; |
| 914 | break; |
| 915 | case '[': |
| 916 | InCharacterClass = true; |
| 917 | break; |
| 918 | case ']': |
| 919 | InCharacterClass = false; |
| 920 | break; |
| 921 | case '/': |
| 922 | if (!InCharacterClass) |
| 923 | HaveClosingSlash = true; |
| 924 | break; |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | RegexToken->Type = TT_RegexLiteral; |
| 929 | // Treat regex literals like other string_literals. |
| 930 | RegexToken->Tok.setKind(tok::string_literal); |
| 931 | RegexToken->TokenText = StringRef(RegexBegin, Offset - RegexBegin); |
| 932 | RegexToken->ColumnWidth = RegexToken->TokenText.size(); |
| 933 | |
| 934 | resetLexer(SourceMgr.getFileOffset(Lex->getSourceLocation(Offset))); |
Daniel Jasper | f9ae312 | 2014-05-08 07:01:45 +0000 | [diff] [blame] | 935 | } |
| 936 | |
Daniel Jasper | a0ef4f3 | 2015-02-20 13:47:38 +0000 | [diff] [blame] | 937 | bool tryMergeTemplateString() { |
| 938 | if (Tokens.size() < 2) |
| 939 | return false; |
| 940 | |
| 941 | FormatToken *EndBacktick = Tokens.back(); |
Daniel Jasper | f69b922 | 2015-05-02 08:05:38 +0000 | [diff] [blame] | 942 | // Backticks get lexed as tok::unknown tokens. If a template string contains |
Daniel Jasper | 0d6ac27 | 2015-04-16 08:20:51 +0000 | [diff] [blame] | 943 | // a comment start, it gets lexed as a tok::comment, or tok::unknown if |
| 944 | // unterminated. |
Daniel Jasper | 2ebb0c5 | 2015-06-14 07:16:57 +0000 | [diff] [blame] | 945 | if (!EndBacktick->isOneOf(tok::comment, tok::string_literal, |
| 946 | tok::char_constant, tok::unknown)) |
Daniel Jasper | 0d6ac27 | 2015-04-16 08:20:51 +0000 | [diff] [blame] | 947 | return false; |
| 948 | size_t CommentBacktickPos = EndBacktick->TokenText.find('`'); |
| 949 | // Unknown token that's not actually a backtick, or a comment that doesn't |
| 950 | // contain a backtick. |
| 951 | if (CommentBacktickPos == StringRef::npos) |
Daniel Jasper | a0ef4f3 | 2015-02-20 13:47:38 +0000 | [diff] [blame] | 952 | return false; |
| 953 | |
| 954 | unsigned TokenCount = 0; |
| 955 | bool IsMultiline = false; |
Daniel Jasper | f69b922 | 2015-05-02 08:05:38 +0000 | [diff] [blame] | 956 | unsigned EndColumnInFirstLine = |
| 957 | EndBacktick->OriginalColumn + EndBacktick->ColumnWidth; |
Daniel Jasper | a0ef4f3 | 2015-02-20 13:47:38 +0000 | [diff] [blame] | 958 | for (auto I = Tokens.rbegin() + 1, E = Tokens.rend(); I != E; I++) { |
| 959 | ++TokenCount; |
Daniel Jasper | 553a5b0 | 2015-07-02 13:08:28 +0000 | [diff] [blame] | 960 | if (I[0]->IsMultiline) |
Daniel Jasper | a0ef4f3 | 2015-02-20 13:47:38 +0000 | [diff] [blame] | 961 | IsMultiline = true; |
| 962 | |
| 963 | // If there was a preceding template string, this must be the start of a |
| 964 | // template string, not the end. |
| 965 | if (I[0]->is(TT_TemplateString)) |
| 966 | return false; |
| 967 | |
| 968 | if (I[0]->isNot(tok::unknown) || I[0]->TokenText != "`") { |
| 969 | // Keep track of the rhs offset of the last token to wrap across lines - |
| 970 | // its the rhs offset of the first line of the template string, used to |
| 971 | // determine its width. |
| 972 | if (I[0]->IsMultiline) |
| 973 | EndColumnInFirstLine = I[0]->OriginalColumn + I[0]->ColumnWidth; |
| 974 | // If the token has newlines, the token before it (if it exists) is the |
| 975 | // rhs end of the previous line. |
Daniel Jasper | 553a5b0 | 2015-07-02 13:08:28 +0000 | [diff] [blame] | 976 | if (I[0]->NewlinesBefore > 0 && (I + 1 != E)) { |
Daniel Jasper | a0ef4f3 | 2015-02-20 13:47:38 +0000 | [diff] [blame] | 977 | EndColumnInFirstLine = I[1]->OriginalColumn + I[1]->ColumnWidth; |
Daniel Jasper | 553a5b0 | 2015-07-02 13:08:28 +0000 | [diff] [blame] | 978 | IsMultiline = true; |
| 979 | } |
Daniel Jasper | a0ef4f3 | 2015-02-20 13:47:38 +0000 | [diff] [blame] | 980 | continue; |
| 981 | } |
| 982 | |
| 983 | Tokens.resize(Tokens.size() - TokenCount); |
| 984 | Tokens.back()->Type = TT_TemplateString; |
Daniel Jasper | 0d6ac27 | 2015-04-16 08:20:51 +0000 | [diff] [blame] | 985 | const char *EndOffset = |
| 986 | EndBacktick->TokenText.data() + 1 + CommentBacktickPos; |
| 987 | if (CommentBacktickPos != 0) { |
| 988 | // If the backtick was not the first character (e.g. in a comment), |
| 989 | // re-lex after the backtick position. |
| 990 | SourceLocation Loc = EndBacktick->Tok.getLocation(); |
| 991 | resetLexer(SourceMgr.getFileOffset(Loc) + CommentBacktickPos + 1); |
| 992 | } |
Daniel Jasper | a0ef4f3 | 2015-02-20 13:47:38 +0000 | [diff] [blame] | 993 | Tokens.back()->TokenText = |
| 994 | StringRef(Tokens.back()->TokenText.data(), |
| 995 | EndOffset - Tokens.back()->TokenText.data()); |
Daniel Jasper | f69b922 | 2015-05-02 08:05:38 +0000 | [diff] [blame] | 996 | |
| 997 | unsigned EndOriginalColumn = EndBacktick->OriginalColumn; |
| 998 | if (EndOriginalColumn == 0) { |
| 999 | SourceLocation Loc = EndBacktick->Tok.getLocation(); |
| 1000 | EndOriginalColumn = SourceMgr.getSpellingColumnNumber(Loc); |
| 1001 | } |
| 1002 | // If the ` is further down within the token (e.g. in a comment). |
| 1003 | EndOriginalColumn += CommentBacktickPos; |
| 1004 | |
Daniel Jasper | a0ef4f3 | 2015-02-20 13:47:38 +0000 | [diff] [blame] | 1005 | if (IsMultiline) { |
| 1006 | // ColumnWidth is from backtick to last token in line. |
| 1007 | // LastLineColumnWidth is 0 to backtick. |
| 1008 | // x = `some content |
| 1009 | // until here`; |
| 1010 | Tokens.back()->ColumnWidth = |
| 1011 | EndColumnInFirstLine - Tokens.back()->OriginalColumn; |
Daniel Jasper | 553a5b0 | 2015-07-02 13:08:28 +0000 | [diff] [blame] | 1012 | // +1 for the ` itself. |
| 1013 | Tokens.back()->LastLineColumnWidth = EndOriginalColumn + 1; |
Daniel Jasper | a0ef4f3 | 2015-02-20 13:47:38 +0000 | [diff] [blame] | 1014 | Tokens.back()->IsMultiline = true; |
| 1015 | } else { |
| 1016 | // Token simply spans from start to end, +1 for the ` itself. |
| 1017 | Tokens.back()->ColumnWidth = |
Daniel Jasper | f69b922 | 2015-05-02 08:05:38 +0000 | [diff] [blame] | 1018 | EndOriginalColumn - Tokens.back()->OriginalColumn + 1; |
Daniel Jasper | a0ef4f3 | 2015-02-20 13:47:38 +0000 | [diff] [blame] | 1019 | } |
| 1020 | return true; |
| 1021 | } |
| 1022 | return false; |
| 1023 | } |
| 1024 | |
Alexander Kornienko | 9aa6240 | 2013-11-21 12:43:57 +0000 | [diff] [blame] | 1025 | bool tryMerge_TMacro() { |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 1026 | if (Tokens.size() < 4) |
Alexander Kornienko | 9aa6240 | 2013-11-21 12:43:57 +0000 | [diff] [blame] | 1027 | return false; |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 1028 | FormatToken *Last = Tokens.back(); |
| 1029 | if (!Last->is(tok::r_paren)) |
Alexander Kornienko | 9aa6240 | 2013-11-21 12:43:57 +0000 | [diff] [blame] | 1030 | return false; |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 1031 | |
| 1032 | FormatToken *String = Tokens[Tokens.size() - 2]; |
| 1033 | if (!String->is(tok::string_literal) || String->IsMultiline) |
Alexander Kornienko | 9aa6240 | 2013-11-21 12:43:57 +0000 | [diff] [blame] | 1034 | return false; |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 1035 | |
| 1036 | if (!Tokens[Tokens.size() - 3]->is(tok::l_paren)) |
Alexander Kornienko | 9aa6240 | 2013-11-21 12:43:57 +0000 | [diff] [blame] | 1037 | return false; |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 1038 | |
| 1039 | FormatToken *Macro = Tokens[Tokens.size() - 4]; |
| 1040 | if (Macro->TokenText != "_T") |
Alexander Kornienko | 9aa6240 | 2013-11-21 12:43:57 +0000 | [diff] [blame] | 1041 | return false; |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 1042 | |
| 1043 | const char *Start = Macro->TokenText.data(); |
| 1044 | const char *End = Last->TokenText.data() + Last->TokenText.size(); |
| 1045 | String->TokenText = StringRef(Start, End - Start); |
| 1046 | String->IsFirst = Macro->IsFirst; |
| 1047 | String->LastNewlineOffset = Macro->LastNewlineOffset; |
| 1048 | String->WhitespaceRange = Macro->WhitespaceRange; |
| 1049 | String->OriginalColumn = Macro->OriginalColumn; |
| 1050 | String->ColumnWidth = encoding::columnWidthWithTabs( |
| 1051 | String->TokenText, String->OriginalColumn, Style.TabWidth, Encoding); |
Daniel Jasper | e99c72f | 2015-03-26 14:47:35 +0000 | [diff] [blame] | 1052 | String->NewlinesBefore = Macro->NewlinesBefore; |
| 1053 | String->HasUnescapedNewline = Macro->HasUnescapedNewline; |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 1054 | |
| 1055 | Tokens.pop_back(); |
| 1056 | Tokens.pop_back(); |
| 1057 | Tokens.pop_back(); |
| 1058 | Tokens.back() = String; |
Alexander Kornienko | 9aa6240 | 2013-11-21 12:43:57 +0000 | [diff] [blame] | 1059 | return true; |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 1062 | bool tryMergeConflictMarkers() { |
| 1063 | if (Tokens.back()->NewlinesBefore == 0 && Tokens.back()->isNot(tok::eof)) |
| 1064 | return false; |
| 1065 | |
| 1066 | // Conflict lines look like: |
| 1067 | // <marker> <text from the vcs> |
| 1068 | // For example: |
| 1069 | // >>>>>>> /file/in/file/system at revision 1234 |
| 1070 | // |
| 1071 | // We merge all tokens in a line that starts with a conflict marker |
| 1072 | // into a single token with a special token type that the unwrapped line |
| 1073 | // parser will use to correctly rebuild the underlying code. |
| 1074 | |
| 1075 | FileID ID; |
| 1076 | // Get the position of the first token in the line. |
| 1077 | unsigned FirstInLineOffset; |
| 1078 | std::tie(ID, FirstInLineOffset) = SourceMgr.getDecomposedLoc( |
| 1079 | Tokens[FirstInLineIndex]->getStartOfNonWhitespace()); |
| 1080 | StringRef Buffer = SourceMgr.getBuffer(ID)->getBuffer(); |
| 1081 | // Calculate the offset of the start of the current line. |
| 1082 | auto LineOffset = Buffer.rfind('\n', FirstInLineOffset); |
| 1083 | if (LineOffset == StringRef::npos) { |
| 1084 | LineOffset = 0; |
| 1085 | } else { |
| 1086 | ++LineOffset; |
| 1087 | } |
| 1088 | |
| 1089 | auto FirstSpace = Buffer.find_first_of(" \n", LineOffset); |
| 1090 | StringRef LineStart; |
| 1091 | if (FirstSpace == StringRef::npos) { |
| 1092 | LineStart = Buffer.substr(LineOffset); |
| 1093 | } else { |
| 1094 | LineStart = Buffer.substr(LineOffset, FirstSpace - LineOffset); |
| 1095 | } |
| 1096 | |
| 1097 | TokenType Type = TT_Unknown; |
| 1098 | if (LineStart == "<<<<<<<" || LineStart == ">>>>") { |
| 1099 | Type = TT_ConflictStart; |
| 1100 | } else if (LineStart == "|||||||" || LineStart == "=======" || |
| 1101 | LineStart == "====") { |
| 1102 | Type = TT_ConflictAlternative; |
| 1103 | } else if (LineStart == ">>>>>>>" || LineStart == "<<<<") { |
| 1104 | Type = TT_ConflictEnd; |
| 1105 | } |
| 1106 | |
| 1107 | if (Type != TT_Unknown) { |
| 1108 | FormatToken *Next = Tokens.back(); |
| 1109 | |
| 1110 | Tokens.resize(FirstInLineIndex + 1); |
| 1111 | // We do not need to build a complete token here, as we will skip it |
| 1112 | // during parsing anyway (as we must not touch whitespace around conflict |
| 1113 | // markers). |
| 1114 | Tokens.back()->Type = Type; |
| 1115 | Tokens.back()->Tok.setKind(tok::kw___unknown_anytype); |
| 1116 | |
| 1117 | Tokens.push_back(Next); |
| 1118 | return true; |
| 1119 | } |
| 1120 | |
| 1121 | return false; |
| 1122 | } |
| 1123 | |
Jacques Pienaar | fc27511 | 2015-02-18 23:48:37 +0000 | [diff] [blame] | 1124 | FormatToken *getStashedToken() { |
| 1125 | // Create a synthesized second '>' or '<' token. |
| 1126 | Token Tok = FormatTok->Tok; |
| 1127 | StringRef TokenText = FormatTok->TokenText; |
| 1128 | |
| 1129 | unsigned OriginalColumn = FormatTok->OriginalColumn; |
| 1130 | FormatTok = new (Allocator.Allocate()) FormatToken; |
| 1131 | FormatTok->Tok = Tok; |
| 1132 | SourceLocation TokLocation = |
Jacques Pienaar | 411b251 | 2015-02-24 23:23:24 +0000 | [diff] [blame] | 1133 | FormatTok->Tok.getLocation().getLocWithOffset(Tok.getLength() - 1); |
| 1134 | FormatTok->Tok.setLocation(TokLocation); |
Jacques Pienaar | fc27511 | 2015-02-18 23:48:37 +0000 | [diff] [blame] | 1135 | FormatTok->WhitespaceRange = SourceRange(TokLocation, TokLocation); |
| 1136 | FormatTok->TokenText = TokenText; |
| 1137 | FormatTok->ColumnWidth = 1; |
Jacques Pienaar | 411b251 | 2015-02-24 23:23:24 +0000 | [diff] [blame] | 1138 | FormatTok->OriginalColumn = OriginalColumn + 1; |
| 1139 | |
Jacques Pienaar | fc27511 | 2015-02-18 23:48:37 +0000 | [diff] [blame] | 1140 | return FormatTok; |
| 1141 | } |
| 1142 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1143 | FormatToken *getNextToken() { |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1144 | if (GreaterStashed) { |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1145 | GreaterStashed = false; |
Jacques Pienaar | fc27511 | 2015-02-18 23:48:37 +0000 | [diff] [blame] | 1146 | return getStashedToken(); |
| 1147 | } |
| 1148 | if (LessStashed) { |
| 1149 | LessStashed = false; |
| 1150 | return getStashedToken(); |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1153 | FormatTok = new (Allocator.Allocate()) FormatToken; |
Daniel Jasper | 8369aa5 | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 1154 | readRawToken(*FormatTok); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 1155 | SourceLocation WhitespaceStart = |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1156 | FormatTok->Tok.getLocation().getLocWithOffset(-TrailingWhitespace); |
Alexander Kornienko | 393e308 | 2013-11-13 14:04:17 +0000 | [diff] [blame] | 1157 | FormatTok->IsFirst = IsFirstToken; |
| 1158 | IsFirstToken = false; |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1159 | |
| 1160 | // Consume and record whitespace until we find a significant token. |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 1161 | unsigned WhitespaceLength = TrailingWhitespace; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1162 | while (FormatTok->Tok.is(tok::unknown)) { |
Daniel Jasper | e2408e3 | 2015-05-06 11:16:43 +0000 | [diff] [blame] | 1163 | StringRef Text = FormatTok->TokenText; |
| 1164 | auto EscapesNewline = [&](int pos) { |
| 1165 | // A '\r' here is just part of '\r\n'. Skip it. |
| 1166 | if (pos >= 0 && Text[pos] == '\r') |
| 1167 | --pos; |
| 1168 | // See whether there is an odd number of '\' before this. |
| 1169 | unsigned count = 0; |
| 1170 | for (; pos >= 0; --pos, ++count) |
Daniel Jasper | f0fd1c6 | 2015-05-10 08:00:25 +0000 | [diff] [blame] | 1171 | if (Text[pos] != '\\') |
Daniel Jasper | e2408e3 | 2015-05-06 11:16:43 +0000 | [diff] [blame] | 1172 | break; |
| 1173 | return count & 1; |
| 1174 | }; |
Daniel Jasper | a0ef4f3 | 2015-02-20 13:47:38 +0000 | [diff] [blame] | 1175 | // FIXME: This miscounts tok:unknown tokens that are not just |
| 1176 | // whitespace, e.g. a '`' character. |
Daniel Jasper | e2408e3 | 2015-05-06 11:16:43 +0000 | [diff] [blame] | 1177 | for (int i = 0, e = Text.size(); i != e; ++i) { |
| 1178 | switch (Text[i]) { |
Manuel Klimek | 31c8592 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 1179 | case '\n': |
| 1180 | ++FormatTok->NewlinesBefore; |
Daniel Jasper | e2408e3 | 2015-05-06 11:16:43 +0000 | [diff] [blame] | 1181 | FormatTok->HasUnescapedNewline = !EscapesNewline(i - 1); |
Manuel Klimek | 31c8592 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 1182 | FormatTok->LastNewlineOffset = WhitespaceLength + i + 1; |
| 1183 | Column = 0; |
| 1184 | break; |
Daniel Jasper | 877615c | 2013-10-11 19:45:02 +0000 | [diff] [blame] | 1185 | case '\r': |
Daniel Jasper | 30029c6 | 2015-02-05 11:05:31 +0000 | [diff] [blame] | 1186 | FormatTok->LastNewlineOffset = WhitespaceLength + i + 1; |
| 1187 | Column = 0; |
| 1188 | break; |
Daniel Jasper | 877615c | 2013-10-11 19:45:02 +0000 | [diff] [blame] | 1189 | case '\f': |
| 1190 | case '\v': |
| 1191 | Column = 0; |
| 1192 | break; |
Manuel Klimek | 31c8592 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 1193 | case ' ': |
| 1194 | ++Column; |
| 1195 | break; |
| 1196 | case '\t': |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 1197 | Column += Style.TabWidth - Column % Style.TabWidth; |
Manuel Klimek | 31c8592 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 1198 | break; |
Daniel Jasper | 877615c | 2013-10-11 19:45:02 +0000 | [diff] [blame] | 1199 | case '\\': |
Daniel Jasper | e2408e3 | 2015-05-06 11:16:43 +0000 | [diff] [blame] | 1200 | if (i + 1 == e || (Text[i + 1] != '\r' && Text[i + 1] != '\n')) |
Daniel Jasper | 877615c | 2013-10-11 19:45:02 +0000 | [diff] [blame] | 1201 | FormatTok->Type = TT_ImplicitStringLiteral; |
| 1202 | break; |
Manuel Klimek | 31c8592 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 1203 | default: |
Daniel Jasper | 877615c | 2013-10-11 19:45:02 +0000 | [diff] [blame] | 1204 | FormatTok->Type = TT_ImplicitStringLiteral; |
Manuel Klimek | 31c8592 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 1205 | break; |
| 1206 | } |
| 1207 | } |
| 1208 | |
Daniel Jasper | a98b7b0 | 2014-11-25 10:05:17 +0000 | [diff] [blame] | 1209 | if (FormatTok->is(TT_ImplicitStringLiteral)) |
Daniel Jasper | 877615c | 2013-10-11 19:45:02 +0000 | [diff] [blame] | 1210 | break; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1211 | WhitespaceLength += FormatTok->Tok.getLength(); |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1212 | |
Daniel Jasper | 8369aa5 | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 1213 | readRawToken(*FormatTok); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1214 | } |
Manuel Klimek | ef92069 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 1215 | |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1216 | // In case the token starts with escaped newlines, we want to |
| 1217 | // take them into account as whitespace - this pattern is quite frequent |
| 1218 | // in macro definitions. |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1219 | // FIXME: Add a more explicit test. |
Daniel Jasper | 8369aa5 | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 1220 | while (FormatTok->TokenText.size() > 1 && FormatTok->TokenText[0] == '\\' && |
| 1221 | FormatTok->TokenText[1] == '\n') { |
Manuel Klimek | 1fcbe67 | 2014-04-11 12:27:47 +0000 | [diff] [blame] | 1222 | ++FormatTok->NewlinesBefore; |
Manuel Klimek | 5c24cca | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 1223 | WhitespaceLength += 2; |
Daniel Jasper | e2408e3 | 2015-05-06 11:16:43 +0000 | [diff] [blame] | 1224 | FormatTok->LastNewlineOffset = 2; |
Manuel Klimek | 31c8592 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 1225 | Column = 0; |
Daniel Jasper | 8369aa5 | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 1226 | FormatTok->TokenText = FormatTok->TokenText.substr(2); |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1227 | } |
Alexander Kornienko | 39856b7 | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 1228 | |
| 1229 | FormatTok->WhitespaceRange = SourceRange( |
| 1230 | WhitespaceStart, WhitespaceStart.getLocWithOffset(WhitespaceLength)); |
| 1231 | |
Manuel Klimek | 31c8592 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 1232 | FormatTok->OriginalColumn = Column; |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1233 | |
Alexander Kornienko | ee4ca9b | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 1234 | TrailingWhitespace = 0; |
| 1235 | if (FormatTok->Tok.is(tok::comment)) { |
Manuel Klimek | 31c8592 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 1236 | // FIXME: Add the trimmed whitespace to Column. |
Daniel Jasper | 8369aa5 | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 1237 | StringRef UntrimmedText = FormatTok->TokenText; |
Alexander Kornienko | 9ab4a77 | 2013-09-06 17:24:54 +0000 | [diff] [blame] | 1238 | FormatTok->TokenText = FormatTok->TokenText.rtrim(" \t\v\f"); |
Daniel Jasper | 8369aa5 | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 1239 | TrailingWhitespace = UntrimmedText.size() - FormatTok->TokenText.size(); |
Alexander Kornienko | ee4ca9b | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 1240 | } else if (FormatTok->Tok.is(tok::raw_identifier)) { |
Daniel Jasper | 8369aa5 | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 1241 | IdentifierInfo &Info = IdentTable.get(FormatTok->TokenText); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1242 | FormatTok->Tok.setIdentifierInfo(&Info); |
| 1243 | FormatTok->Tok.setKind(Info.getTokenID()); |
Daniel Jasper | fe2cf66 | 2014-11-19 14:11:11 +0000 | [diff] [blame] | 1244 | if (Style.Language == FormatStyle::LK_Java && |
| 1245 | FormatTok->isOneOf(tok::kw_struct, tok::kw_union, tok::kw_delete)) { |
| 1246 | FormatTok->Tok.setKind(tok::identifier); |
| 1247 | FormatTok->Tok.setIdentifierInfo(nullptr); |
| 1248 | } |
Alexander Kornienko | ee4ca9b | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 1249 | } else if (FormatTok->Tok.is(tok::greatergreater)) { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1250 | FormatTok->Tok.setKind(tok::greater); |
Daniel Jasper | 8369aa5 | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 1251 | FormatTok->TokenText = FormatTok->TokenText.substr(0, 1); |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1252 | GreaterStashed = true; |
Jacques Pienaar | fc27511 | 2015-02-18 23:48:37 +0000 | [diff] [blame] | 1253 | } else if (FormatTok->Tok.is(tok::lessless)) { |
| 1254 | FormatTok->Tok.setKind(tok::less); |
| 1255 | FormatTok->TokenText = FormatTok->TokenText.substr(0, 1); |
| 1256 | LessStashed = true; |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
Alexander Kornienko | ee4ca9b | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 1259 | // Now FormatTok is the next non-whitespace token. |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1260 | |
Alexander Kornienko | 39856b7 | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 1261 | StringRef Text = FormatTok->TokenText; |
| 1262 | size_t FirstNewlinePos = Text.find('\n'); |
Alexander Kornienko | 917f9e0 | 2013-09-10 12:29:48 +0000 | [diff] [blame] | 1263 | if (FirstNewlinePos == StringRef::npos) { |
| 1264 | // FIXME: ColumnWidth actually depends on the start column, we need to |
| 1265 | // take this into account when the token is moved. |
| 1266 | FormatTok->ColumnWidth = |
| 1267 | encoding::columnWidthWithTabs(Text, Column, Style.TabWidth, Encoding); |
| 1268 | Column += FormatTok->ColumnWidth; |
| 1269 | } else { |
Alexander Kornienko | 39856b7 | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 1270 | FormatTok->IsMultiline = true; |
Alexander Kornienko | 917f9e0 | 2013-09-10 12:29:48 +0000 | [diff] [blame] | 1271 | // FIXME: ColumnWidth actually depends on the start column, we need to |
| 1272 | // take this into account when the token is moved. |
| 1273 | FormatTok->ColumnWidth = encoding::columnWidthWithTabs( |
| 1274 | Text.substr(0, FirstNewlinePos), Column, Style.TabWidth, Encoding); |
| 1275 | |
Alexander Kornienko | 39856b7 | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 1276 | // The last line of the token always starts in column 0. |
| 1277 | // Thus, the length can be precomputed even in the presence of tabs. |
| 1278 | FormatTok->LastLineColumnWidth = encoding::columnWidthWithTabs( |
| 1279 | Text.substr(Text.find_last_of('\n') + 1), 0, Style.TabWidth, |
| 1280 | Encoding); |
Alexander Kornienko | 917f9e0 | 2013-09-10 12:29:48 +0000 | [diff] [blame] | 1281 | Column = FormatTok->LastLineColumnWidth; |
Alexander Kornienko | 632abb9 | 2013-09-02 13:58:14 +0000 | [diff] [blame] | 1282 | } |
Alexander Kornienko | 39856b7 | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 1283 | |
Birunthan Mohanathas | b001a0b | 2015-07-03 17:25:16 +0000 | [diff] [blame] | 1284 | if (Style.Language == FormatStyle::LK_Cpp) { |
| 1285 | if (!(Tokens.size() > 0 && Tokens.back()->Tok.getIdentifierInfo() && |
| 1286 | Tokens.back()->Tok.getIdentifierInfo()->getPPKeywordID() == |
| 1287 | tok::pp_define) && |
| 1288 | std::find(ForEachMacros.begin(), ForEachMacros.end(), |
| 1289 | FormatTok->Tok.getIdentifierInfo()) != ForEachMacros.end()) { |
| 1290 | FormatTok->Type = TT_ForEachMacro; |
| 1291 | } else if (FormatTok->is(tok::identifier)) { |
| 1292 | if (MacroBlockBeginRegex.match(Text)) { |
| 1293 | FormatTok->Type = TT_MacroBlockBegin; |
| 1294 | } else if (MacroBlockEndRegex.match(Text)) { |
| 1295 | FormatTok->Type = TT_MacroBlockEnd; |
| 1296 | } |
| 1297 | } |
| 1298 | } |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 1299 | |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1300 | return FormatTok; |
| 1301 | } |
| 1302 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1303 | FormatToken *FormatTok; |
Alexander Kornienko | 393e308 | 2013-11-13 14:04:17 +0000 | [diff] [blame] | 1304 | bool IsFirstToken; |
Jacques Pienaar | fc27511 | 2015-02-18 23:48:37 +0000 | [diff] [blame] | 1305 | bool GreaterStashed, LessStashed; |
Manuel Klimek | 31c8592 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 1306 | unsigned Column; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 1307 | unsigned TrailingWhitespace; |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 1308 | std::unique_ptr<Lexer> Lex; |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1309 | SourceManager &SourceMgr; |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 1310 | FileID ID; |
Manuel Klimek | 31c8592 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 1311 | FormatStyle &Style; |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1312 | IdentifierTable IdentTable; |
Daniel Jasper | d0ec0d6 | 2014-11-04 12:41:02 +0000 | [diff] [blame] | 1313 | AdditionalKeywords Keywords; |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1314 | encoding::Encoding Encoding; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1315 | llvm::SpecificBumpPtrAllocator<FormatToken> Allocator; |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 1316 | // Index (in 'Tokens') of the last token that starts a new line. |
| 1317 | unsigned FirstInLineIndex; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1318 | SmallVector<FormatToken *, 16> Tokens; |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 1319 | SmallVector<IdentifierInfo *, 8> ForEachMacros; |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1320 | |
NAKAMURA Takumi | 7160c4d | 2014-08-06 16:53:13 +0000 | [diff] [blame] | 1321 | bool FormattingDisabled; |
Daniel Jasper | 47189443 | 2014-08-06 13:40:26 +0000 | [diff] [blame] | 1322 | |
Birunthan Mohanathas | b001a0b | 2015-07-03 17:25:16 +0000 | [diff] [blame] | 1323 | llvm::Regex MacroBlockBeginRegex; |
| 1324 | llvm::Regex MacroBlockEndRegex; |
| 1325 | |
Daniel Jasper | 8369aa5 | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 1326 | void readRawToken(FormatToken &Tok) { |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 1327 | Lex->LexFromRawLexer(Tok.Tok); |
Daniel Jasper | 8369aa5 | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 1328 | Tok.TokenText = StringRef(SourceMgr.getCharacterData(Tok.Tok.getLocation()), |
| 1329 | Tok.Tok.getLength()); |
Daniel Jasper | 8369aa5 | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 1330 | // For formatting, treat unterminated string literals like normal string |
| 1331 | // literals. |
Daniel Jasper | 86fee2f | 2014-01-31 12:49:42 +0000 | [diff] [blame] | 1332 | if (Tok.is(tok::unknown)) { |
| 1333 | if (!Tok.TokenText.empty() && Tok.TokenText[0] == '"') { |
| 1334 | Tok.Tok.setKind(tok::string_literal); |
| 1335 | Tok.IsUnterminatedLiteral = true; |
| 1336 | } else if (Style.Language == FormatStyle::LK_JavaScript && |
| 1337 | Tok.TokenText == "''") { |
| 1338 | Tok.Tok.setKind(tok::char_constant); |
| 1339 | } |
Daniel Jasper | 8369aa5 | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 1340 | } |
Roman Kashitsyn | 650ecb5 | 2014-09-11 14:47:20 +0000 | [diff] [blame] | 1341 | |
| 1342 | if (Tok.is(tok::comment) && (Tok.TokenText == "// clang-format on" || |
| 1343 | Tok.TokenText == "/* clang-format on */")) { |
Daniel Jasper | 47189443 | 2014-08-06 13:40:26 +0000 | [diff] [blame] | 1344 | FormattingDisabled = false; |
Roman Kashitsyn | 650ecb5 | 2014-09-11 14:47:20 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
Daniel Jasper | 47189443 | 2014-08-06 13:40:26 +0000 | [diff] [blame] | 1347 | Tok.Finalized = FormattingDisabled; |
Roman Kashitsyn | 650ecb5 | 2014-09-11 14:47:20 +0000 | [diff] [blame] | 1348 | |
| 1349 | if (Tok.is(tok::comment) && (Tok.TokenText == "// clang-format off" || |
| 1350 | Tok.TokenText == "/* clang-format off */")) { |
Daniel Jasper | 47189443 | 2014-08-06 13:40:26 +0000 | [diff] [blame] | 1351 | FormattingDisabled = true; |
Roman Kashitsyn | 650ecb5 | 2014-09-11 14:47:20 +0000 | [diff] [blame] | 1352 | } |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1353 | } |
Daniel Jasper | 49a9a28 | 2014-10-29 16:51:38 +0000 | [diff] [blame] | 1354 | |
| 1355 | void resetLexer(unsigned Offset) { |
| 1356 | StringRef Buffer = SourceMgr.getBufferData(ID); |
| 1357 | Lex.reset(new Lexer(SourceMgr.getLocForStartOfFile(ID), |
| 1358 | getFormattingLangOpts(Style), Buffer.begin(), |
| 1359 | Buffer.begin() + Offset, Buffer.end())); |
| 1360 | Lex->SetKeepWhitespaceMode(true); |
Daniel Jasper | 55c384e | 2015-07-02 14:01:34 +0000 | [diff] [blame] | 1361 | TrailingWhitespace = 0; |
Daniel Jasper | 49a9a28 | 2014-10-29 16:51:38 +0000 | [diff] [blame] | 1362 | } |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1363 | }; |
| 1364 | |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 1365 | static StringRef getLanguageName(FormatStyle::LanguageKind Language) { |
| 1366 | switch (Language) { |
| 1367 | case FormatStyle::LK_Cpp: |
| 1368 | return "C++"; |
Daniel Jasper | c58c70e | 2014-09-15 11:21:46 +0000 | [diff] [blame] | 1369 | case FormatStyle::LK_Java: |
| 1370 | return "Java"; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 1371 | case FormatStyle::LK_JavaScript: |
| 1372 | return "JavaScript"; |
Daniel Jasper | 7052ce6 | 2014-01-19 09:04:08 +0000 | [diff] [blame] | 1373 | case FormatStyle::LK_Proto: |
| 1374 | return "Proto"; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 1375 | default: |
| 1376 | return "Unknown"; |
| 1377 | } |
| 1378 | } |
| 1379 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1380 | class Formatter : public UnwrappedLineConsumer { |
| 1381 | public: |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 1382 | Formatter(const FormatStyle &Style, SourceManager &SourceMgr, FileID ID, |
Benjamin Kramer | d0eed3a | 2014-10-03 18:52:48 +0000 | [diff] [blame] | 1383 | ArrayRef<CharSourceRange> Ranges) |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 1384 | : Style(Style), ID(ID), SourceMgr(SourceMgr), |
| 1385 | Whitespaces(SourceMgr, Style, |
| 1386 | inputUsesCRLF(SourceMgr.getBufferData(ID))), |
Daniel Jasper | bbf5c1c | 2013-11-05 19:10:03 +0000 | [diff] [blame] | 1387 | Ranges(Ranges.begin(), Ranges.end()), UnwrappedLines(1), |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 1388 | Encoding(encoding::detectEncoding(SourceMgr.getBufferData(ID))) { |
Daniel Jasper | fa21c07 | 2013-07-15 14:33:14 +0000 | [diff] [blame] | 1389 | DEBUG(llvm::dbgs() << "File encoding: " |
| 1390 | << (Encoding == encoding::Encoding_UTF8 ? "UTF8" |
| 1391 | : "unknown") |
| 1392 | << "\n"); |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 1393 | DEBUG(llvm::dbgs() << "Language: " << getLanguageName(Style.Language) |
| 1394 | << "\n"); |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1395 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1396 | |
Manuel Klimek | ec5c3db | 2015-05-07 12:26:30 +0000 | [diff] [blame] | 1397 | tooling::Replacements format(bool *IncompleteFormat) { |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 1398 | tooling::Replacements Result; |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 1399 | FormatTokenLexer Tokens(SourceMgr, ID, Style, Encoding); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1400 | |
Daniel Jasper | d0ec0d6 | 2014-11-04 12:41:02 +0000 | [diff] [blame] | 1401 | UnwrappedLineParser Parser(Style, Tokens.getKeywords(), Tokens.lex(), |
| 1402 | *this); |
Manuel Klimek | 20e0af6 | 2015-05-06 11:56:29 +0000 | [diff] [blame] | 1403 | Parser.parse(); |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 1404 | assert(UnwrappedLines.rbegin()->empty()); |
| 1405 | for (unsigned Run = 0, RunE = UnwrappedLines.size(); Run + 1 != RunE; |
| 1406 | ++Run) { |
| 1407 | DEBUG(llvm::dbgs() << "Run " << Run << "...\n"); |
| 1408 | SmallVector<AnnotatedLine *, 16> AnnotatedLines; |
| 1409 | for (unsigned i = 0, e = UnwrappedLines[Run].size(); i != e; ++i) { |
| 1410 | AnnotatedLines.push_back(new AnnotatedLine(UnwrappedLines[Run][i])); |
| 1411 | } |
Manuel Klimek | ec5c3db | 2015-05-07 12:26:30 +0000 | [diff] [blame] | 1412 | tooling::Replacements RunResult = |
| 1413 | format(AnnotatedLines, Tokens, IncompleteFormat); |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 1414 | DEBUG({ |
| 1415 | llvm::dbgs() << "Replacements for run " << Run << ":\n"; |
| 1416 | for (tooling::Replacements::iterator I = RunResult.begin(), |
| 1417 | E = RunResult.end(); |
| 1418 | I != E; ++I) { |
| 1419 | llvm::dbgs() << I->toString() << "\n"; |
| 1420 | } |
| 1421 | }); |
| 1422 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
| 1423 | delete AnnotatedLines[i]; |
| 1424 | } |
| 1425 | Result.insert(RunResult.begin(), RunResult.end()); |
| 1426 | Whitespaces.reset(); |
| 1427 | } |
| 1428 | return Result; |
| 1429 | } |
| 1430 | |
| 1431 | tooling::Replacements format(SmallVectorImpl<AnnotatedLine *> &AnnotatedLines, |
Daniel Jasper | e6fcf7d | 2015-06-17 13:08:06 +0000 | [diff] [blame] | 1432 | FormatTokenLexer &Tokens, |
| 1433 | bool *IncompleteFormat) { |
Daniel Jasper | d0ec0d6 | 2014-11-04 12:41:02 +0000 | [diff] [blame] | 1434 | TokenAnnotator Annotator(Style, Tokens.getKeywords()); |
Alexander Kornienko | 62b85b9 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1435 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1436 | Annotator.annotate(*AnnotatedLines[i]); |
Alexander Kornienko | 62b85b9 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1437 | } |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 1438 | deriveLocalStyle(AnnotatedLines); |
Alexander Kornienko | 62b85b9 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1439 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1440 | Annotator.calculateFormattingInformation(*AnnotatedLines[i]); |
Alexander Kornienko | 62b85b9 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1441 | } |
Daniel Jasper | 5500f61 | 2013-11-25 11:08:59 +0000 | [diff] [blame] | 1442 | computeAffectedLines(AnnotatedLines.begin(), AnnotatedLines.end()); |
Daniel Jasper | b67cc42 | 2013-04-09 17:46:55 +0000 | [diff] [blame] | 1443 | |
Daniel Jasper | 1c5d9df | 2013-09-06 07:54:20 +0000 | [diff] [blame] | 1444 | Annotator.setCommentLineLevels(AnnotatedLines); |
Daniel Jasper | d0ec0d6 | 2014-11-04 12:41:02 +0000 | [diff] [blame] | 1445 | ContinuationIndenter Indenter(Style, Tokens.getKeywords(), SourceMgr, |
| 1446 | Whitespaces, Encoding, |
Daniel Jasper | bbf5c1c | 2013-11-05 19:10:03 +0000 | [diff] [blame] | 1447 | BinPackInconclusiveFunctions); |
Manuel Klimek | d3585db | 2015-05-11 08:21:35 +0000 | [diff] [blame] | 1448 | UnwrappedLineFormatter(&Indenter, &Whitespaces, Style, Tokens.getKeywords(), |
| 1449 | IncompleteFormat) |
| 1450 | .format(AnnotatedLines); |
Alexander Kornienko | 62b85b9 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1451 | return Whitespaces.generateReplacements(); |
| 1452 | } |
| 1453 | |
| 1454 | private: |
Daniel Jasper | 5500f61 | 2013-11-25 11:08:59 +0000 | [diff] [blame] | 1455 | // Determines which lines are affected by the SourceRanges given as input. |
Daniel Jasper | 9c19956 | 2013-11-28 15:58:55 +0000 | [diff] [blame] | 1456 | // Returns \c true if at least one line between I and E or one of their |
| 1457 | // children is affected. |
Daniel Jasper | 5500f61 | 2013-11-25 11:08:59 +0000 | [diff] [blame] | 1458 | bool computeAffectedLines(SmallVectorImpl<AnnotatedLine *>::iterator I, |
| 1459 | SmallVectorImpl<AnnotatedLine *>::iterator E) { |
| 1460 | bool SomeLineAffected = false; |
Craig Topper | 2145bc0 | 2014-05-09 08:15:10 +0000 | [diff] [blame] | 1461 | const AnnotatedLine *PreviousLine = nullptr; |
Daniel Jasper | 5500f61 | 2013-11-25 11:08:59 +0000 | [diff] [blame] | 1462 | while (I != E) { |
| 1463 | AnnotatedLine *Line = *I; |
| 1464 | Line->LeadingEmptyLinesAffected = affectsLeadingEmptyLines(*Line->First); |
| 1465 | |
| 1466 | // If a line is part of a preprocessor directive, it needs to be formatted |
| 1467 | // if any token within the directive is affected. |
| 1468 | if (Line->InPPDirective) { |
| 1469 | FormatToken *Last = Line->Last; |
| 1470 | SmallVectorImpl<AnnotatedLine *>::iterator PPEnd = I + 1; |
| 1471 | while (PPEnd != E && !(*PPEnd)->First->HasUnescapedNewline) { |
| 1472 | Last = (*PPEnd)->Last; |
| 1473 | ++PPEnd; |
| 1474 | } |
| 1475 | |
| 1476 | if (affectsTokenRange(*Line->First, *Last, |
| 1477 | /*IncludeLeadingNewlines=*/false)) { |
| 1478 | SomeLineAffected = true; |
| 1479 | markAllAsAffected(I, PPEnd); |
| 1480 | } |
| 1481 | I = PPEnd; |
| 1482 | continue; |
| 1483 | } |
| 1484 | |
Daniel Jasper | 38c8240 | 2013-11-29 09:27:43 +0000 | [diff] [blame] | 1485 | if (nonPPLineAffected(Line, PreviousLine)) |
Daniel Jasper | 5500f61 | 2013-11-25 11:08:59 +0000 | [diff] [blame] | 1486 | SomeLineAffected = true; |
Daniel Jasper | 5500f61 | 2013-11-25 11:08:59 +0000 | [diff] [blame] | 1487 | |
Daniel Jasper | 38c8240 | 2013-11-29 09:27:43 +0000 | [diff] [blame] | 1488 | PreviousLine = Line; |
Daniel Jasper | 5500f61 | 2013-11-25 11:08:59 +0000 | [diff] [blame] | 1489 | ++I; |
| 1490 | } |
| 1491 | return SomeLineAffected; |
| 1492 | } |
| 1493 | |
Daniel Jasper | 9c19956 | 2013-11-28 15:58:55 +0000 | [diff] [blame] | 1494 | // Determines whether 'Line' is affected by the SourceRanges given as input. |
| 1495 | // Returns \c true if line or one if its children is affected. |
Daniel Jasper | 38c8240 | 2013-11-29 09:27:43 +0000 | [diff] [blame] | 1496 | bool nonPPLineAffected(AnnotatedLine *Line, |
| 1497 | const AnnotatedLine *PreviousLine) { |
Daniel Jasper | 9c19956 | 2013-11-28 15:58:55 +0000 | [diff] [blame] | 1498 | bool SomeLineAffected = false; |
| 1499 | Line->ChildrenAffected = |
| 1500 | computeAffectedLines(Line->Children.begin(), Line->Children.end()); |
| 1501 | if (Line->ChildrenAffected) |
| 1502 | SomeLineAffected = true; |
| 1503 | |
| 1504 | // Stores whether one of the line's tokens is directly affected. |
| 1505 | bool SomeTokenAffected = false; |
| 1506 | // Stores whether we need to look at the leading newlines of the next token |
| 1507 | // in order to determine whether it was affected. |
| 1508 | bool IncludeLeadingNewlines = false; |
| 1509 | |
| 1510 | // Stores whether the first child line of any of this line's tokens is |
| 1511 | // affected. |
| 1512 | bool SomeFirstChildAffected = false; |
| 1513 | |
| 1514 | for (FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) { |
| 1515 | // Determine whether 'Tok' was affected. |
| 1516 | if (affectsTokenRange(*Tok, *Tok, IncludeLeadingNewlines)) |
| 1517 | SomeTokenAffected = true; |
| 1518 | |
| 1519 | // Determine whether the first child of 'Tok' was affected. |
| 1520 | if (!Tok->Children.empty() && Tok->Children.front()->Affected) |
| 1521 | SomeFirstChildAffected = true; |
| 1522 | |
| 1523 | IncludeLeadingNewlines = Tok->Children.empty(); |
| 1524 | } |
| 1525 | |
| 1526 | // Was this line moved, i.e. has it previously been on the same line as an |
| 1527 | // affected line? |
Daniel Jasper | 38c8240 | 2013-11-29 09:27:43 +0000 | [diff] [blame] | 1528 | bool LineMoved = PreviousLine && PreviousLine->Affected && |
| 1529 | Line->First->NewlinesBefore == 0; |
Daniel Jasper | 9c19956 | 2013-11-28 15:58:55 +0000 | [diff] [blame] | 1530 | |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 1531 | bool IsContinuedComment = |
| 1532 | Line->First->is(tok::comment) && Line->First->Next == nullptr && |
| 1533 | Line->First->NewlinesBefore < 2 && PreviousLine && |
| 1534 | PreviousLine->Affected && PreviousLine->Last->is(tok::comment); |
Daniel Jasper | 38c8240 | 2013-11-29 09:27:43 +0000 | [diff] [blame] | 1535 | |
| 1536 | if (SomeTokenAffected || SomeFirstChildAffected || LineMoved || |
| 1537 | IsContinuedComment) { |
Daniel Jasper | 9c19956 | 2013-11-28 15:58:55 +0000 | [diff] [blame] | 1538 | Line->Affected = true; |
Daniel Jasper | 9c19956 | 2013-11-28 15:58:55 +0000 | [diff] [blame] | 1539 | SomeLineAffected = true; |
Daniel Jasper | 9c19956 | 2013-11-28 15:58:55 +0000 | [diff] [blame] | 1540 | } |
| 1541 | return SomeLineAffected; |
| 1542 | } |
| 1543 | |
Daniel Jasper | 5500f61 | 2013-11-25 11:08:59 +0000 | [diff] [blame] | 1544 | // Marks all lines between I and E as well as all their children as affected. |
| 1545 | void markAllAsAffected(SmallVectorImpl<AnnotatedLine *>::iterator I, |
| 1546 | SmallVectorImpl<AnnotatedLine *>::iterator E) { |
| 1547 | while (I != E) { |
| 1548 | (*I)->Affected = true; |
| 1549 | markAllAsAffected((*I)->Children.begin(), (*I)->Children.end()); |
| 1550 | ++I; |
| 1551 | } |
| 1552 | } |
| 1553 | |
| 1554 | // Returns true if the range from 'First' to 'Last' intersects with one of the |
| 1555 | // input ranges. |
| 1556 | bool affectsTokenRange(const FormatToken &First, const FormatToken &Last, |
| 1557 | bool IncludeLeadingNewlines) { |
| 1558 | SourceLocation Start = First.WhitespaceRange.getBegin(); |
| 1559 | if (!IncludeLeadingNewlines) |
| 1560 | Start = Start.getLocWithOffset(First.LastNewlineOffset); |
Daniel Jasper | 5877bf1 | 2013-11-25 11:53:05 +0000 | [diff] [blame] | 1561 | SourceLocation End = Last.getStartOfNonWhitespace(); |
Daniel Jasper | ac29eac | 2014-10-29 23:40:50 +0000 | [diff] [blame] | 1562 | End = End.getLocWithOffset(Last.TokenText.size()); |
Daniel Jasper | 5500f61 | 2013-11-25 11:08:59 +0000 | [diff] [blame] | 1563 | CharSourceRange Range = CharSourceRange::getCharRange(Start, End); |
| 1564 | return affectsCharSourceRange(Range); |
| 1565 | } |
| 1566 | |
| 1567 | // Returns true if one of the input ranges intersect the leading empty lines |
| 1568 | // before 'Tok'. |
| 1569 | bool affectsLeadingEmptyLines(const FormatToken &Tok) { |
| 1570 | CharSourceRange EmptyLineRange = CharSourceRange::getCharRange( |
| 1571 | Tok.WhitespaceRange.getBegin(), |
| 1572 | Tok.WhitespaceRange.getBegin().getLocWithOffset(Tok.LastNewlineOffset)); |
| 1573 | return affectsCharSourceRange(EmptyLineRange); |
| 1574 | } |
| 1575 | |
| 1576 | // Returns true if 'Range' intersects with one of the input ranges. |
| 1577 | bool affectsCharSourceRange(const CharSourceRange &Range) { |
| 1578 | for (SmallVectorImpl<CharSourceRange>::const_iterator I = Ranges.begin(), |
| 1579 | E = Ranges.end(); |
| 1580 | I != E; ++I) { |
| 1581 | if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), I->getBegin()) && |
| 1582 | !SourceMgr.isBeforeInTranslationUnit(I->getEnd(), Range.getBegin())) |
| 1583 | return true; |
| 1584 | } |
| 1585 | return false; |
| 1586 | } |
| 1587 | |
Alexander Kornienko | 9e649af | 2013-09-11 12:25:57 +0000 | [diff] [blame] | 1588 | static bool inputUsesCRLF(StringRef Text) { |
| 1589 | return Text.count('\r') * 2 > Text.count('\n'); |
| 1590 | } |
| 1591 | |
Daniel Jasper | 352f0df | 2015-07-18 16:35:30 +0000 | [diff] [blame] | 1592 | bool |
| 1593 | hasCpp03IncompatibleFormat(const SmallVectorImpl<AnnotatedLine *> &Lines) { |
| 1594 | for (const AnnotatedLine* Line : Lines) { |
| 1595 | if (hasCpp03IncompatibleFormat(Line->Children)) |
| 1596 | return true; |
| 1597 | for (FormatToken *Tok = Line->First->Next; Tok; Tok = Tok->Next) { |
| 1598 | if (Tok->WhitespaceRange.getBegin() == Tok->WhitespaceRange.getEnd()) { |
| 1599 | if (Tok->is(tok::coloncolon) && Tok->Previous->is(TT_TemplateOpener)) |
| 1600 | return true; |
| 1601 | if (Tok->is(TT_TemplateCloser) && |
| 1602 | Tok->Previous->is(TT_TemplateCloser)) |
| 1603 | return true; |
| 1604 | } |
| 1605 | } |
| 1606 | } |
| 1607 | return false; |
| 1608 | } |
| 1609 | |
| 1610 | int countVariableAlignments(const SmallVectorImpl<AnnotatedLine *> &Lines) { |
| 1611 | int AlignmentDiff = 0; |
| 1612 | for (const AnnotatedLine* Line : Lines) { |
| 1613 | AlignmentDiff += countVariableAlignments(Line->Children); |
| 1614 | for (FormatToken *Tok = Line->First; Tok && Tok->Next; Tok = Tok->Next) { |
| 1615 | if (!Tok->is(TT_PointerOrReference)) |
| 1616 | continue; |
| 1617 | bool SpaceBefore = |
| 1618 | Tok->WhitespaceRange.getBegin() != Tok->WhitespaceRange.getEnd(); |
| 1619 | bool SpaceAfter = Tok->Next->WhitespaceRange.getBegin() != |
| 1620 | Tok->Next->WhitespaceRange.getEnd(); |
| 1621 | if (SpaceBefore && !SpaceAfter) |
| 1622 | ++AlignmentDiff; |
| 1623 | if (!SpaceBefore && SpaceAfter) |
| 1624 | --AlignmentDiff; |
| 1625 | } |
| 1626 | } |
| 1627 | return AlignmentDiff; |
| 1628 | } |
| 1629 | |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 1630 | void |
| 1631 | deriveLocalStyle(const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) { |
Daniel Jasper | b10cbc4 | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 1632 | bool HasBinPackedFunction = false; |
| 1633 | bool HasOnePerLineFunction = false; |
Daniel Jasper | 7fce3ab | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1634 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1635 | if (!AnnotatedLines[i]->First->Next) |
Daniel Jasper | 7fce3ab | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1636 | continue; |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1637 | FormatToken *Tok = AnnotatedLines[i]->First->Next; |
Manuel Klimek | 6e6310e | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1638 | while (Tok->Next) { |
Daniel Jasper | b10cbc4 | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 1639 | if (Tok->PackingKind == PPK_BinPacked) |
| 1640 | HasBinPackedFunction = true; |
| 1641 | if (Tok->PackingKind == PPK_OnePerLine) |
| 1642 | HasOnePerLineFunction = true; |
| 1643 | |
Manuel Klimek | 6e6310e | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1644 | Tok = Tok->Next; |
Daniel Jasper | 7fce3ab | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1645 | } |
| 1646 | } |
Daniel Jasper | 352f0df | 2015-07-18 16:35:30 +0000 | [diff] [blame] | 1647 | if (Style.DerivePointerAlignment) |
| 1648 | Style.PointerAlignment = countVariableAlignments(AnnotatedLines) <= 0 |
| 1649 | ? FormatStyle::PAS_Left |
| 1650 | : FormatStyle::PAS_Right; |
| 1651 | if (Style.Standard == FormatStyle::LS_Auto) |
| 1652 | Style.Standard = hasCpp03IncompatibleFormat(AnnotatedLines) |
| 1653 | ? FormatStyle::LS_Cpp11 |
| 1654 | : FormatStyle::LS_Cpp03; |
Daniel Jasper | b10cbc4 | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 1655 | BinPackInconclusiveFunctions = |
| 1656 | HasBinPackedFunction || !HasOnePerLineFunction; |
Daniel Jasper | 7fce3ab | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 1659 | void consumeUnwrappedLine(const UnwrappedLine &TheLine) override { |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 1660 | assert(!UnwrappedLines.empty()); |
| 1661 | UnwrappedLines.back().push_back(TheLine); |
| 1662 | } |
| 1663 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 1664 | void finishRun() override { |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 1665 | UnwrappedLines.push_back(SmallVector<UnwrappedLine, 16>()); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1666 | } |
| 1667 | |
| 1668 | FormatStyle Style; |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 1669 | FileID ID; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1670 | SourceManager &SourceMgr; |
Daniel Jasper | aa701fa | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 1671 | WhitespaceManager Whitespaces; |
Daniel Jasper | bbf5c1c | 2013-11-05 19:10:03 +0000 | [diff] [blame] | 1672 | SmallVector<CharSourceRange, 8> Ranges; |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 1673 | SmallVector<SmallVector<UnwrappedLine, 16>, 2> UnwrappedLines; |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1674 | |
| 1675 | encoding::Encoding Encoding; |
Daniel Jasper | b10cbc4 | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 1676 | bool BinPackInconclusiveFunctions; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1677 | }; |
| 1678 | |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 1679 | struct IncludeDirective { |
| 1680 | StringRef Filename; |
| 1681 | StringRef Text; |
| 1682 | unsigned Offset; |
Daniel Jasper | 85c472d | 2015-09-29 07:53:08 +0000 | [diff] [blame] | 1683 | unsigned Category; |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 1684 | }; |
| 1685 | |
Craig Topper | af35e85 | 2013-06-30 22:29:28 +0000 | [diff] [blame] | 1686 | } // end anonymous namespace |
| 1687 | |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 1688 | // Determines whether 'Ranges' intersects with ('Start', 'End'). |
| 1689 | static bool affectsRange(ArrayRef<tooling::Range> Ranges, unsigned Start, |
| 1690 | unsigned End) { |
| 1691 | for (auto Range : Ranges) { |
| 1692 | if (Range.getOffset() < End && |
| 1693 | Range.getOffset() + Range.getLength() > Start) |
| 1694 | return true; |
| 1695 | } |
| 1696 | return false; |
| 1697 | } |
| 1698 | |
| 1699 | // Sorts a block of includes given by 'Includes' alphabetically adding the |
| 1700 | // necessary replacement to 'Replaces'. 'Includes' must be in strict source |
| 1701 | // order. |
| 1702 | static void sortIncludes(const FormatStyle &Style, |
| 1703 | const SmallVectorImpl<IncludeDirective> &Includes, |
| 1704 | ArrayRef<tooling::Range> Ranges, StringRef FileName, |
| 1705 | tooling::Replacements &Replaces) { |
| 1706 | if (!affectsRange(Ranges, Includes.front().Offset, |
| 1707 | Includes.back().Offset + Includes.back().Text.size())) |
| 1708 | return; |
| 1709 | SmallVector<unsigned, 16> Indices; |
| 1710 | for (unsigned i = 0, e = Includes.size(); i != e; ++i) |
| 1711 | Indices.push_back(i); |
| 1712 | std::sort(Indices.begin(), Indices.end(), [&](unsigned LHSI, unsigned RHSI) { |
Daniel Jasper | 85c472d | 2015-09-29 07:53:08 +0000 | [diff] [blame] | 1713 | return std::tie(Includes[LHSI].Category, Includes[LHSI].Filename) < |
| 1714 | std::tie(Includes[RHSI].Category, Includes[RHSI].Filename); |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 1715 | }); |
| 1716 | |
| 1717 | // If the #includes are out of order, we generate a single replacement fixing |
| 1718 | // the entire block. Otherwise, no replacement is generated. |
| 1719 | bool OutOfOrder = false; |
| 1720 | for (unsigned i = 1, e = Indices.size(); i != e; ++i) { |
| 1721 | if (Indices[i] != i) { |
| 1722 | OutOfOrder = true; |
| 1723 | break; |
| 1724 | } |
| 1725 | } |
| 1726 | if (!OutOfOrder) |
| 1727 | return; |
| 1728 | |
| 1729 | std::string result = Includes[Indices[0]].Text; |
| 1730 | for (unsigned i = 1, e = Indices.size(); i != e; ++i) { |
| 1731 | result += "\n"; |
| 1732 | result += Includes[Indices[i]].Text; |
| 1733 | } |
| 1734 | |
| 1735 | // Sorting #includes shouldn't change their total number of characters. |
| 1736 | // This would otherwise mess up 'Ranges'. |
| 1737 | assert(result.size() == |
| 1738 | Includes.back().Offset + Includes.back().Text.size() - |
| 1739 | Includes.front().Offset); |
| 1740 | |
| 1741 | Replaces.insert(tooling::Replacement(FileName, Includes.front().Offset, |
| 1742 | result.size(), result)); |
| 1743 | } |
| 1744 | |
| 1745 | tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code, |
| 1746 | ArrayRef<tooling::Range> Ranges, |
| 1747 | StringRef FileName) { |
| 1748 | tooling::Replacements Replaces; |
Daniel Jasper | da44677 | 2015-11-16 12:38:56 +0000 | [diff] [blame] | 1749 | if (!Style.SortIncludes) |
| 1750 | return Replaces; |
| 1751 | |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 1752 | unsigned Prev = 0; |
| 1753 | unsigned SearchFrom = 0; |
Daniel Jasper | 85c472d | 2015-09-29 07:53:08 +0000 | [diff] [blame] | 1754 | llvm::Regex IncludeRegex( |
Nico Weber | ff06370 | 2015-10-21 17:13:45 +0000 | [diff] [blame] | 1755 | R"(^[\t\ ]*#[\t\ ]*(import|include)[^"<]*(["<][^">]*[">]))"); |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 1756 | SmallVector<StringRef, 4> Matches; |
| 1757 | SmallVector<IncludeDirective, 16> IncludesInBlock; |
Daniel Jasper | 85c472d | 2015-09-29 07:53:08 +0000 | [diff] [blame] | 1758 | |
| 1759 | // In compiled files, consider the first #include to be the main #include of |
| 1760 | // the file if it is not a system #include. This ensures that the header |
| 1761 | // doesn't have hidden dependencies |
| 1762 | // (http://llvm.org/docs/CodingStandards.html#include-style). |
| 1763 | // |
| 1764 | // FIXME: Do some sanity checking, e.g. edit distance of the base name, to fix |
| 1765 | // cases where the first #include is unlikely to be the main header. |
| 1766 | bool LookForMainHeader = FileName.endswith(".c") || |
| 1767 | FileName.endswith(".cc") || |
| 1768 | FileName.endswith(".cpp")|| |
| 1769 | FileName.endswith(".c++")|| |
Nico Weber | afa62fa | 2015-10-19 01:36:09 +0000 | [diff] [blame] | 1770 | FileName.endswith(".cxx") || |
| 1771 | FileName.endswith(".m")|| |
| 1772 | FileName.endswith(".mm"); |
Daniel Jasper | 85c472d | 2015-09-29 07:53:08 +0000 | [diff] [blame] | 1773 | |
| 1774 | // Create pre-compiled regular expressions for the #include categories. |
| 1775 | SmallVector<llvm::Regex, 4> CategoryRegexs; |
Daniel Jasper | 8ce1b8d | 2015-10-06 11:54:18 +0000 | [diff] [blame] | 1776 | for (const auto &Category : Style.IncludeCategories) |
| 1777 | CategoryRegexs.emplace_back(Category.Regex); |
Daniel Jasper | 85c472d | 2015-09-29 07:53:08 +0000 | [diff] [blame] | 1778 | |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 1779 | for (;;) { |
| 1780 | auto Pos = Code.find('\n', SearchFrom); |
| 1781 | StringRef Line = |
| 1782 | Code.substr(Prev, (Pos != StringRef::npos ? Pos : Code.size()) - Prev); |
| 1783 | if (!Line.endswith("\\")) { |
| 1784 | if (IncludeRegex.match(Line, &Matches)) { |
Nico Weber | ff06370 | 2015-10-21 17:13:45 +0000 | [diff] [blame] | 1785 | StringRef IncludeName = Matches[2]; |
Daniel Jasper | 85c472d | 2015-09-29 07:53:08 +0000 | [diff] [blame] | 1786 | unsigned Category; |
Nico Weber | ff06370 | 2015-10-21 17:13:45 +0000 | [diff] [blame] | 1787 | if (LookForMainHeader && !IncludeName.startswith("<")) { |
Daniel Jasper | 85c472d | 2015-09-29 07:53:08 +0000 | [diff] [blame] | 1788 | Category = 0; |
| 1789 | } else { |
| 1790 | Category = UINT_MAX; |
| 1791 | for (unsigned i = 0, e = CategoryRegexs.size(); i != e; ++i) { |
Nico Weber | ff06370 | 2015-10-21 17:13:45 +0000 | [diff] [blame] | 1792 | if (CategoryRegexs[i].match(IncludeName)) { |
Daniel Jasper | 8ce1b8d | 2015-10-06 11:54:18 +0000 | [diff] [blame] | 1793 | Category = Style.IncludeCategories[i].Priority; |
Daniel Jasper | 85c472d | 2015-09-29 07:53:08 +0000 | [diff] [blame] | 1794 | break; |
| 1795 | } |
| 1796 | } |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 1797 | } |
Daniel Jasper | 85c472d | 2015-09-29 07:53:08 +0000 | [diff] [blame] | 1798 | LookForMainHeader = false; |
Nico Weber | ff06370 | 2015-10-21 17:13:45 +0000 | [diff] [blame] | 1799 | IncludesInBlock.push_back({IncludeName, Line, Prev, Category}); |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 1800 | } else if (!IncludesInBlock.empty()) { |
| 1801 | sortIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces); |
| 1802 | IncludesInBlock.clear(); |
| 1803 | } |
| 1804 | Prev = Pos + 1; |
| 1805 | } |
| 1806 | if (Pos == StringRef::npos || Pos + 1 == Code.size()) |
| 1807 | break; |
| 1808 | SearchFrom = Pos + 1; |
| 1809 | } |
| 1810 | if (!IncludesInBlock.empty()) |
| 1811 | sortIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces); |
| 1812 | return Replaces; |
| 1813 | } |
| 1814 | |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 1815 | tooling::Replacements reformat(const FormatStyle &Style, |
| 1816 | SourceManager &SourceMgr, FileID ID, |
Manuel Klimek | ec5c3db | 2015-05-07 12:26:30 +0000 | [diff] [blame] | 1817 | ArrayRef<CharSourceRange> Ranges, |
| 1818 | bool *IncompleteFormat) { |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 1819 | FormatStyle Expanded = expandPresets(Style); |
| 1820 | if (Expanded.DisableFormat) |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 1821 | return tooling::Replacements(); |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 1822 | Formatter formatter(Expanded, SourceMgr, ID, Ranges); |
Manuel Klimek | ec5c3db | 2015-05-07 12:26:30 +0000 | [diff] [blame] | 1823 | return formatter.format(IncompleteFormat); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1824 | } |
| 1825 | |
Daniel Jasper | ec04c0d | 2013-05-16 10:40:07 +0000 | [diff] [blame] | 1826 | tooling::Replacements reformat(const FormatStyle &Style, StringRef Code, |
Benjamin Kramer | d0eed3a | 2014-10-03 18:52:48 +0000 | [diff] [blame] | 1827 | ArrayRef<tooling::Range> Ranges, |
Daniel Jasper | e6fcf7d | 2015-06-17 13:08:06 +0000 | [diff] [blame] | 1828 | StringRef FileName, bool *IncompleteFormat) { |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 1829 | if (Style.DisableFormat) |
| 1830 | return tooling::Replacements(); |
| 1831 | |
Benjamin Kramer | 2e2351a | 2015-10-06 10:04:08 +0000 | [diff] [blame] | 1832 | IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( |
| 1833 | new vfs::InMemoryFileSystem); |
| 1834 | FileManager Files(FileSystemOptions(), InMemoryFileSystem); |
Daniel Jasper | ec04c0d | 2013-05-16 10:40:07 +0000 | [diff] [blame] | 1835 | DiagnosticsEngine Diagnostics( |
| 1836 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), |
| 1837 | new DiagnosticOptions); |
| 1838 | SourceManager SourceMgr(Diagnostics, Files); |
Benjamin Kramer | 2e2351a | 2015-10-06 10:04:08 +0000 | [diff] [blame] | 1839 | InMemoryFileSystem->addFile(FileName, 0, |
| 1840 | llvm::MemoryBuffer::getMemBuffer(Code, FileName)); |
| 1841 | FileID ID = SourceMgr.createFileID(Files.getFile(FileName), SourceLocation(), |
| 1842 | clang::SrcMgr::C_User); |
Daniel Jasper | ec04c0d | 2013-05-16 10:40:07 +0000 | [diff] [blame] | 1843 | SourceLocation StartOfFile = SourceMgr.getLocForStartOfFile(ID); |
| 1844 | std::vector<CharSourceRange> CharRanges; |
Benjamin Kramer | d0eed3a | 2014-10-03 18:52:48 +0000 | [diff] [blame] | 1845 | for (const tooling::Range &Range : Ranges) { |
| 1846 | SourceLocation Start = StartOfFile.getLocWithOffset(Range.getOffset()); |
| 1847 | SourceLocation End = Start.getLocWithOffset(Range.getLength()); |
Daniel Jasper | ec04c0d | 2013-05-16 10:40:07 +0000 | [diff] [blame] | 1848 | CharRanges.push_back(CharSourceRange::getCharRange(Start, End)); |
| 1849 | } |
Manuel Klimek | ec5c3db | 2015-05-07 12:26:30 +0000 | [diff] [blame] | 1850 | return reformat(Style, SourceMgr, ID, CharRanges, IncompleteFormat); |
Daniel Jasper | ec04c0d | 2013-05-16 10:40:07 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
Daniel Jasper | 4db69bd | 2014-09-04 18:23:42 +0000 | [diff] [blame] | 1853 | LangOptions getFormattingLangOpts(const FormatStyle &Style) { |
Daniel Jasper | c1fa281 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1854 | LangOptions LangOpts; |
| 1855 | LangOpts.CPlusPlus = 1; |
Daniel Jasper | 4db69bd | 2014-09-04 18:23:42 +0000 | [diff] [blame] | 1856 | LangOpts.CPlusPlus11 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1; |
| 1857 | LangOpts.CPlusPlus14 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1; |
Daniel Jasper | 5521365 | 2013-03-22 10:01:29 +0000 | [diff] [blame] | 1858 | LangOpts.LineComment = 1; |
Daniel Jasper | 1662bfe | 2015-04-03 21:15:46 +0000 | [diff] [blame] | 1859 | bool AlternativeOperators = Style.Language == FormatStyle::LK_Cpp; |
Daniel Jasper | 30a2406 | 2014-11-14 09:02:28 +0000 | [diff] [blame] | 1860 | LangOpts.CXXOperatorNames = AlternativeOperators ? 1 : 0; |
Daniel Jasper | c1fa281 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1861 | LangOpts.Bool = 1; |
| 1862 | LangOpts.ObjC1 = 1; |
| 1863 | LangOpts.ObjC2 = 1; |
Nico Weber | fac2371 | 2015-02-04 15:26:27 +0000 | [diff] [blame] | 1864 | LangOpts.MicrosoftExt = 1; // To get kw___try, kw___finally. |
Saleem Abdulrasool | d170c4b | 2015-10-04 17:51:05 +0000 | [diff] [blame] | 1865 | LangOpts.DeclSpecKeyword = 1; // To get __declspec. |
Daniel Jasper | c1fa281 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1866 | return LangOpts; |
| 1867 | } |
| 1868 | |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 1869 | const char *StyleOptionHelpDescription = |
| 1870 | "Coding style, currently supports:\n" |
| 1871 | " LLVM, Google, Chromium, Mozilla, WebKit.\n" |
| 1872 | "Use -style=file to load style configuration from\n" |
| 1873 | ".clang-format file located in one of the parent\n" |
| 1874 | "directories of the source file (or current\n" |
| 1875 | "directory for stdin).\n" |
| 1876 | "Use -style=\"{key: value, ...}\" to set specific\n" |
| 1877 | "parameters, e.g.:\n" |
| 1878 | " -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\""; |
| 1879 | |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 1880 | static FormatStyle::LanguageKind getLanguageByFileName(StringRef FileName) { |
Daniel Jasper | c58c70e | 2014-09-15 11:21:46 +0000 | [diff] [blame] | 1881 | if (FileName.endswith(".java")) { |
| 1882 | return FormatStyle::LK_Java; |
Daniel Jasper | 8c68a64 | 2015-03-11 14:58:38 +0000 | [diff] [blame] | 1883 | } else if (FileName.endswith_lower(".js") || FileName.endswith_lower(".ts")) { |
| 1884 | // JavaScript or TypeScript. |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 1885 | return FormatStyle::LK_JavaScript; |
Daniel Jasper | 7052ce6 | 2014-01-19 09:04:08 +0000 | [diff] [blame] | 1886 | } else if (FileName.endswith_lower(".proto") || |
| 1887 | FileName.endswith_lower(".protodevel")) { |
| 1888 | return FormatStyle::LK_Proto; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 1889 | } |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 1890 | return FormatStyle::LK_Cpp; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
Alexander Kornienko | bc4ae44 | 2013-12-02 15:21:38 +0000 | [diff] [blame] | 1893 | FormatStyle getStyle(StringRef StyleName, StringRef FileName, |
| 1894 | StringRef FallbackStyle) { |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 1895 | FormatStyle Style = getLLVMStyle(); |
| 1896 | Style.Language = getLanguageByFileName(FileName); |
| 1897 | if (!getPredefinedStyle(FallbackStyle, Style.Language, &Style)) { |
Alexander Kornienko | bc4ae44 | 2013-12-02 15:21:38 +0000 | [diff] [blame] | 1898 | llvm::errs() << "Invalid fallback style \"" << FallbackStyle |
| 1899 | << "\" using LLVM style\n"; |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 1900 | return Style; |
Alexander Kornienko | bc4ae44 | 2013-12-02 15:21:38 +0000 | [diff] [blame] | 1901 | } |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 1902 | |
| 1903 | if (StyleName.startswith("{")) { |
| 1904 | // Parse YAML/JSON style from the command line. |
Rafael Espindola | c080917 | 2014-06-12 14:02:15 +0000 | [diff] [blame] | 1905 | if (std::error_code ec = parseConfiguration(StyleName, &Style)) { |
Alexander Kornienko | e2e0387 | 2013-10-14 00:46:35 +0000 | [diff] [blame] | 1906 | llvm::errs() << "Error parsing -style: " << ec.message() << ", using " |
| 1907 | << FallbackStyle << " style\n"; |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 1908 | } |
| 1909 | return Style; |
| 1910 | } |
| 1911 | |
| 1912 | if (!StyleName.equals_lower("file")) { |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 1913 | if (!getPredefinedStyle(StyleName, Style.Language, &Style)) |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 1914 | llvm::errs() << "Invalid value for -style, using " << FallbackStyle |
| 1915 | << " style\n"; |
| 1916 | return Style; |
| 1917 | } |
| 1918 | |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 1919 | // Look for .clang-format/_clang-format file in the file's parent directories. |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 1920 | SmallString<128> UnsuitableConfigFiles; |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 1921 | SmallString<128> Path(FileName); |
| 1922 | llvm::sys::fs::make_absolute(Path); |
Alexander Kornienko | e2e0387 | 2013-10-14 00:46:35 +0000 | [diff] [blame] | 1923 | for (StringRef Directory = Path; !Directory.empty(); |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 1924 | Directory = llvm::sys::path::parent_path(Directory)) { |
| 1925 | if (!llvm::sys::fs::is_directory(Directory)) |
| 1926 | continue; |
| 1927 | SmallString<128> ConfigFile(Directory); |
| 1928 | |
| 1929 | llvm::sys::path::append(ConfigFile, ".clang-format"); |
| 1930 | DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n"); |
| 1931 | bool IsFile = false; |
| 1932 | // Ignore errors from is_regular_file: we only need to know if we can read |
| 1933 | // the file or not. |
| 1934 | llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile); |
| 1935 | |
| 1936 | if (!IsFile) { |
| 1937 | // Try _clang-format too, since dotfiles are not commonly used on Windows. |
| 1938 | ConfigFile = Directory; |
| 1939 | llvm::sys::path::append(ConfigFile, "_clang-format"); |
| 1940 | DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n"); |
| 1941 | llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile); |
| 1942 | } |
| 1943 | |
| 1944 | if (IsFile) { |
Rafael Espindola | 2d2b420 | 2014-07-06 17:43:24 +0000 | [diff] [blame] | 1945 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text = |
| 1946 | llvm::MemoryBuffer::getFile(ConfigFile.c_str()); |
| 1947 | if (std::error_code EC = Text.getError()) { |
| 1948 | llvm::errs() << EC.message() << "\n"; |
Alexander Kornienko | bc4ae44 | 2013-12-02 15:21:38 +0000 | [diff] [blame] | 1949 | break; |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 1950 | } |
Rafael Espindola | 2d2b420 | 2014-07-06 17:43:24 +0000 | [diff] [blame] | 1951 | if (std::error_code ec = |
| 1952 | parseConfiguration(Text.get()->getBuffer(), &Style)) { |
Rafael Espindola | d013670 | 2014-06-12 02:50:04 +0000 | [diff] [blame] | 1953 | if (ec == ParseError::Unsuitable) { |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 1954 | if (!UnsuitableConfigFiles.empty()) |
| 1955 | UnsuitableConfigFiles.append(", "); |
| 1956 | UnsuitableConfigFiles.append(ConfigFile); |
Alexander Kornienko | bc4ae44 | 2013-12-02 15:21:38 +0000 | [diff] [blame] | 1957 | continue; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 1958 | } |
Alexander Kornienko | bc4ae44 | 2013-12-02 15:21:38 +0000 | [diff] [blame] | 1959 | llvm::errs() << "Error reading " << ConfigFile << ": " << ec.message() |
| 1960 | << "\n"; |
| 1961 | break; |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 1962 | } |
| 1963 | DEBUG(llvm::dbgs() << "Using configuration file " << ConfigFile << "\n"); |
| 1964 | return Style; |
| 1965 | } |
| 1966 | } |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 1967 | if (!UnsuitableConfigFiles.empty()) { |
| 1968 | llvm::errs() << "Configuration file(s) do(es) not support " |
| 1969 | << getLanguageName(Style.Language) << ": " |
| 1970 | << UnsuitableConfigFiles << "\n"; |
| 1971 | } |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 1972 | return Style; |
| 1973 | } |
| 1974 | |
Daniel Jasper | 8d1832e | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1975 | } // namespace format |
| 1976 | } // namespace clang |