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