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