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 | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 16 | #include "ContinuationIndenter.h" |
Daniel Jasper | 7a6d09b | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 17 | #include "TokenAnnotator.h" |
Daniel Jasper | 0df5093 | 2014-12-10 19:00:42 +0000 | [diff] [blame^] | 18 | #include "UnwrappedLineFormatter.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "UnwrappedLineParser.h" |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 20 | #include "WhitespaceManager.h" |
Daniel Jasper | ec04c0d | 2013-05-16 10:40:07 +0000 | [diff] [blame] | 21 | #include "clang/Basic/Diagnostic.h" |
Benjamin Kramer | f3ca2698 | 2014-05-10 16:31:55 +0000 | [diff] [blame] | 22 | #include "clang/Basic/DiagnosticOptions.h" |
Chandler Carruth | 44eb4f6 | 2013-01-02 10:28:36 +0000 | [diff] [blame] | 23 | #include "clang/Basic/SourceManager.h" |
Manuel Klimek | 2499810 | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 24 | #include "clang/Format/Format.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" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 30 | #include "llvm/Support/YAMLTraits.h" |
Manuel Klimek | 2ef908e | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 31 | #include <queue> |
Daniel Jasper | 8b52971 | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 32 | #include <string> |
| 33 | |
Chandler Carruth | 1034666 | 2014-04-22 03:17:02 +0000 | [diff] [blame] | 34 | #define DEBUG_TYPE "format-formatter" |
| 35 | |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 36 | using clang::format::FormatStyle; |
| 37 | |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 38 | LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(std::string) |
| 39 | |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 40 | namespace llvm { |
| 41 | namespace yaml { |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 42 | template <> struct ScalarEnumerationTraits<FormatStyle::LanguageKind> { |
| 43 | static void enumeration(IO &IO, FormatStyle::LanguageKind &Value) { |
| 44 | IO.enumCase(Value, "Cpp", FormatStyle::LK_Cpp); |
Daniel Jasper | c58c70e | 2014-09-15 11:21:46 +0000 | [diff] [blame] | 45 | IO.enumCase(Value, "Java", FormatStyle::LK_Java); |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 46 | IO.enumCase(Value, "JavaScript", FormatStyle::LK_JavaScript); |
Daniel Jasper | 7052ce6 | 2014-01-19 09:04:08 +0000 | [diff] [blame] | 47 | IO.enumCase(Value, "Proto", FormatStyle::LK_Proto); |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 48 | } |
| 49 | }; |
| 50 | |
| 51 | template <> struct ScalarEnumerationTraits<FormatStyle::LanguageStandard> { |
| 52 | static void enumeration(IO &IO, FormatStyle::LanguageStandard &Value) { |
| 53 | IO.enumCase(Value, "Cpp03", FormatStyle::LS_Cpp03); |
| 54 | IO.enumCase(Value, "C++03", FormatStyle::LS_Cpp03); |
| 55 | IO.enumCase(Value, "Cpp11", FormatStyle::LS_Cpp11); |
| 56 | IO.enumCase(Value, "C++11", FormatStyle::LS_Cpp11); |
| 57 | IO.enumCase(Value, "Auto", FormatStyle::LS_Auto); |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | template <> struct ScalarEnumerationTraits<FormatStyle::UseTabStyle> { |
| 62 | static void enumeration(IO &IO, FormatStyle::UseTabStyle &Value) { |
| 63 | IO.enumCase(Value, "Never", FormatStyle::UT_Never); |
| 64 | IO.enumCase(Value, "false", FormatStyle::UT_Never); |
| 65 | IO.enumCase(Value, "Always", FormatStyle::UT_Always); |
| 66 | IO.enumCase(Value, "true", FormatStyle::UT_Always); |
| 67 | IO.enumCase(Value, "ForIndentation", FormatStyle::UT_ForIndentation); |
| 68 | } |
| 69 | }; |
| 70 | |
Daniel Jasper | d74cf40 | 2014-04-08 12:46:38 +0000 | [diff] [blame] | 71 | template <> struct ScalarEnumerationTraits<FormatStyle::ShortFunctionStyle> { |
| 72 | static void enumeration(IO &IO, FormatStyle::ShortFunctionStyle &Value) { |
| 73 | IO.enumCase(Value, "None", FormatStyle::SFS_None); |
| 74 | IO.enumCase(Value, "false", FormatStyle::SFS_None); |
| 75 | IO.enumCase(Value, "All", FormatStyle::SFS_All); |
| 76 | IO.enumCase(Value, "true", FormatStyle::SFS_All); |
| 77 | IO.enumCase(Value, "Inline", FormatStyle::SFS_Inline); |
Daniel Jasper | 9e70935 | 2014-11-26 10:43:58 +0000 | [diff] [blame] | 78 | IO.enumCase(Value, "Empty", FormatStyle::SFS_Empty); |
Daniel Jasper | d74cf40 | 2014-04-08 12:46:38 +0000 | [diff] [blame] | 79 | } |
| 80 | }; |
| 81 | |
Daniel Jasper | ac043c9 | 2014-09-15 11:11:00 +0000 | [diff] [blame] | 82 | template <> struct ScalarEnumerationTraits<FormatStyle::BinaryOperatorStyle> { |
| 83 | static void enumeration(IO &IO, FormatStyle::BinaryOperatorStyle &Value) { |
| 84 | IO.enumCase(Value, "All", FormatStyle::BOS_All); |
| 85 | IO.enumCase(Value, "true", FormatStyle::BOS_All); |
| 86 | IO.enumCase(Value, "None", FormatStyle::BOS_None); |
| 87 | IO.enumCase(Value, "false", FormatStyle::BOS_None); |
| 88 | IO.enumCase(Value, "NonAssignment", FormatStyle::BOS_NonAssignment); |
| 89 | } |
| 90 | }; |
| 91 | |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 92 | template <> struct ScalarEnumerationTraits<FormatStyle::BraceBreakingStyle> { |
| 93 | static void enumeration(IO &IO, FormatStyle::BraceBreakingStyle &Value) { |
| 94 | IO.enumCase(Value, "Attach", FormatStyle::BS_Attach); |
| 95 | IO.enumCase(Value, "Linux", FormatStyle::BS_Linux); |
| 96 | IO.enumCase(Value, "Stroustrup", FormatStyle::BS_Stroustrup); |
| 97 | IO.enumCase(Value, "Allman", FormatStyle::BS_Allman); |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 98 | IO.enumCase(Value, "GNU", FormatStyle::BS_GNU); |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 99 | } |
| 100 | }; |
| 101 | |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 102 | template <> |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 103 | struct ScalarEnumerationTraits<FormatStyle::NamespaceIndentationKind> { |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 104 | static void enumeration(IO &IO, |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 105 | FormatStyle::NamespaceIndentationKind &Value) { |
| 106 | IO.enumCase(Value, "None", FormatStyle::NI_None); |
| 107 | IO.enumCase(Value, "Inner", FormatStyle::NI_Inner); |
| 108 | IO.enumCase(Value, "All", FormatStyle::NI_All); |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 109 | } |
| 110 | }; |
| 111 | |
| 112 | template <> |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 113 | struct ScalarEnumerationTraits<FormatStyle::PointerAlignmentStyle> { |
| 114 | static void enumeration(IO &IO, |
| 115 | FormatStyle::PointerAlignmentStyle &Value) { |
| 116 | IO.enumCase(Value, "Middle", FormatStyle::PAS_Middle); |
| 117 | IO.enumCase(Value, "Left", FormatStyle::PAS_Left); |
| 118 | IO.enumCase(Value, "Right", FormatStyle::PAS_Right); |
| 119 | |
Alp Toker | 958027b | 2014-07-14 19:42:55 +0000 | [diff] [blame] | 120 | // For backward compatibility. |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 121 | IO.enumCase(Value, "true", FormatStyle::PAS_Left); |
| 122 | IO.enumCase(Value, "false", FormatStyle::PAS_Right); |
| 123 | } |
| 124 | }; |
| 125 | |
| 126 | template <> |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 127 | struct ScalarEnumerationTraits<FormatStyle::SpaceBeforeParensOptions> { |
Manuel Klimek | a8eb914 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 128 | static void enumeration(IO &IO, |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 129 | FormatStyle::SpaceBeforeParensOptions &Value) { |
| 130 | IO.enumCase(Value, "Never", FormatStyle::SBPO_Never); |
Alexander Kornienko | fdca83d | 2013-12-10 10:18:34 +0000 | [diff] [blame] | 131 | IO.enumCase(Value, "ControlStatements", |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 132 | FormatStyle::SBPO_ControlStatements); |
| 133 | IO.enumCase(Value, "Always", FormatStyle::SBPO_Always); |
Alexander Kornienko | fdca83d | 2013-12-10 10:18:34 +0000 | [diff] [blame] | 134 | |
| 135 | // For backward compatibility. |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 136 | IO.enumCase(Value, "false", FormatStyle::SBPO_Never); |
| 137 | IO.enumCase(Value, "true", FormatStyle::SBPO_ControlStatements); |
Alexander Kornienko | fdca83d | 2013-12-10 10:18:34 +0000 | [diff] [blame] | 138 | } |
| 139 | }; |
| 140 | |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 141 | template <> struct MappingTraits<FormatStyle> { |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 142 | static void mapping(IO &IO, FormatStyle &Style) { |
| 143 | // When reading, read the language first, we need it for getPredefinedStyle. |
| 144 | IO.mapOptional("Language", Style.Language); |
| 145 | |
Alexander Kornienko | 4914967 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 146 | if (IO.outputting()) { |
Alexander Kornienko | e3648fb | 2013-09-02 16:39:23 +0000 | [diff] [blame] | 147 | StringRef StylesArray[] = { "LLVM", "Google", "Chromium", |
Alexander Kornienko | fe7a57f | 2013-12-10 15:42:15 +0000 | [diff] [blame] | 148 | "Mozilla", "WebKit", "GNU" }; |
Alexander Kornienko | 4914967 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 149 | ArrayRef<StringRef> Styles(StylesArray); |
| 150 | for (size_t i = 0, e = Styles.size(); i < e; ++i) { |
| 151 | StringRef StyleName(Styles[i]); |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 152 | FormatStyle PredefinedStyle; |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 153 | if (getPredefinedStyle(StyleName, Style.Language, &PredefinedStyle) && |
Alexander Kornienko | 006b5c8 | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 154 | Style == PredefinedStyle) { |
Alexander Kornienko | 4914967 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 155 | IO.mapOptional("# BasedOnStyle", StyleName); |
| 156 | break; |
| 157 | } |
| 158 | } |
| 159 | } else { |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 160 | StringRef BasedOnStyle; |
| 161 | IO.mapOptional("BasedOnStyle", BasedOnStyle); |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 162 | if (!BasedOnStyle.empty()) { |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 163 | FormatStyle::LanguageKind OldLanguage = Style.Language; |
| 164 | FormatStyle::LanguageKind Language = |
| 165 | ((FormatStyle *)IO.getContext())->Language; |
| 166 | if (!getPredefinedStyle(BasedOnStyle, Language, &Style)) { |
Alexander Kornienko | 006b5c8 | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 167 | IO.setError(Twine("Unknown value for BasedOnStyle: ", BasedOnStyle)); |
| 168 | return; |
| 169 | } |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 170 | Style.Language = OldLanguage; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 171 | } |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset); |
Daniel Jasper | 3aa9a6a | 2014-11-18 23:55:27 +0000 | [diff] [blame] | 175 | IO.mapOptional("AlignAfterOpenBracket", Style.AlignAfterOpenBracket); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 176 | IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft); |
Daniel Jasper | 3219e43 | 2014-12-02 13:24:51 +0000 | [diff] [blame] | 177 | IO.mapOptional("AlignOperands", Style.AlignOperands); |
Daniel Jasper | 552f4a7 | 2013-07-31 23:55:15 +0000 | [diff] [blame] | 178 | IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 179 | IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine", |
| 180 | Style.AllowAllParametersOfDeclarationOnNextLine); |
Daniel Jasper | 17605d3 | 2014-05-14 09:33:35 +0000 | [diff] [blame] | 181 | IO.mapOptional("AllowShortBlocksOnASingleLine", |
| 182 | Style.AllowShortBlocksOnASingleLine); |
Daniel Jasper | b87899b | 2014-09-10 13:11:45 +0000 | [diff] [blame] | 183 | IO.mapOptional("AllowShortCaseLabelsOnASingleLine", |
| 184 | Style.AllowShortCaseLabelsOnASingleLine); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 185 | IO.mapOptional("AllowShortIfStatementsOnASingleLine", |
| 186 | Style.AllowShortIfStatementsOnASingleLine); |
Daniel Jasper | 3a685df | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 187 | IO.mapOptional("AllowShortLoopsOnASingleLine", |
| 188 | Style.AllowShortLoopsOnASingleLine); |
Alexander Kornienko | 3cfa973 | 2013-11-20 16:33:05 +0000 | [diff] [blame] | 189 | IO.mapOptional("AllowShortFunctionsOnASingleLine", |
| 190 | Style.AllowShortFunctionsOnASingleLine); |
Daniel Jasper | ca4ea1c | 2014-08-05 12:16:31 +0000 | [diff] [blame] | 191 | IO.mapOptional("AlwaysBreakAfterDefinitionReturnType", |
| 192 | Style.AlwaysBreakAfterDefinitionReturnType); |
Daniel Jasper | 61e6bbf | 2013-05-29 12:07:31 +0000 | [diff] [blame] | 193 | IO.mapOptional("AlwaysBreakTemplateDeclarations", |
| 194 | Style.AlwaysBreakTemplateDeclarations); |
Alexander Kornienko | 5861171 | 2013-07-04 12:02:44 +0000 | [diff] [blame] | 195 | IO.mapOptional("AlwaysBreakBeforeMultilineStrings", |
| 196 | Style.AlwaysBreakBeforeMultilineStrings); |
Daniel Jasper | e33d4af | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 197 | IO.mapOptional("BreakBeforeBinaryOperators", |
| 198 | Style.BreakBeforeBinaryOperators); |
Daniel Jasper | 165b29e | 2013-11-08 00:57:11 +0000 | [diff] [blame] | 199 | IO.mapOptional("BreakBeforeTernaryOperators", |
| 200 | Style.BreakBeforeTernaryOperators); |
Daniel Jasper | e33d4af | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 201 | IO.mapOptional("BreakConstructorInitializersBeforeComma", |
| 202 | Style.BreakConstructorInitializersBeforeComma); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 203 | IO.mapOptional("BinPackParameters", Style.BinPackParameters); |
Daniel Jasper | 18210d7 | 2014-10-09 09:52:05 +0000 | [diff] [blame] | 204 | IO.mapOptional("BinPackArguments", Style.BinPackArguments); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 205 | IO.mapOptional("ColumnLimit", Style.ColumnLimit); |
| 206 | IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine", |
| 207 | Style.ConstructorInitializerAllOnOneLineOrOnePerLine); |
Daniel Jasper | 50d634b | 2014-10-28 16:53:38 +0000 | [diff] [blame] | 208 | IO.mapOptional("ConstructorInitializerIndentWidth", |
| 209 | Style.ConstructorInitializerIndentWidth); |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 210 | IO.mapOptional("DerivePointerAlignment", Style.DerivePointerAlignment); |
Daniel Jasper | b10cbc4 | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 211 | IO.mapOptional("ExperimentalAutoDetectBinPacking", |
| 212 | Style.ExperimentalAutoDetectBinPacking); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 213 | IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels); |
Daniel Jasper | c75e1ef | 2014-07-09 08:42:42 +0000 | [diff] [blame] | 214 | IO.mapOptional("IndentWrappedFunctionNames", |
| 215 | Style.IndentWrappedFunctionNames); |
| 216 | IO.mapOptional("IndentFunctionDeclarationAfterType", |
| 217 | Style.IndentWrappedFunctionNames); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 218 | IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep); |
Daniel Jasper | a26fc5c | 2014-03-21 13:43:14 +0000 | [diff] [blame] | 219 | IO.mapOptional("KeepEmptyLinesAtTheStartOfBlocks", |
| 220 | Style.KeepEmptyLinesAtTheStartOfBlocks); |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 221 | IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation); |
Daniel Jasper | 50d634b | 2014-10-28 16:53:38 +0000 | [diff] [blame] | 222 | IO.mapOptional("ObjCBlockIndentWidth", Style.ObjCBlockIndentWidth); |
Daniel Jasper | e9beea2 | 2014-01-28 15:20:33 +0000 | [diff] [blame] | 223 | IO.mapOptional("ObjCSpaceAfterProperty", Style.ObjCSpaceAfterProperty); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 224 | IO.mapOptional("ObjCSpaceBeforeProtocolList", |
| 225 | Style.ObjCSpaceBeforeProtocolList); |
Daniel Jasper | 33b909c | 2013-10-25 14:29:37 +0000 | [diff] [blame] | 226 | IO.mapOptional("PenaltyBreakBeforeFirstCallParameter", |
| 227 | Style.PenaltyBreakBeforeFirstCallParameter); |
Alexander Kornienko | dd7ece5 | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 228 | IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment); |
| 229 | IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString); |
Daniel Jasper | 4e9678f | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 230 | IO.mapOptional("PenaltyBreakFirstLessLess", |
| 231 | Style.PenaltyBreakFirstLessLess); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 232 | IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter); |
| 233 | IO.mapOptional("PenaltyReturnTypeOnItsOwnLine", |
| 234 | Style.PenaltyReturnTypeOnItsOwnLine); |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 235 | IO.mapOptional("PointerAlignment", Style.PointerAlignment); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 236 | IO.mapOptional("SpacesBeforeTrailingComments", |
| 237 | Style.SpacesBeforeTrailingComments); |
Daniel Jasper | 6ab5468 | 2013-07-16 18:22:10 +0000 | [diff] [blame] | 238 | IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 239 | IO.mapOptional("Standard", Style.Standard); |
Manuel Klimek | 13b97d8 | 2013-05-13 08:42:42 +0000 | [diff] [blame] | 240 | IO.mapOptional("IndentWidth", Style.IndentWidth); |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 241 | IO.mapOptional("TabWidth", Style.TabWidth); |
Manuel Klimek | b9eae4c | 2013-05-13 09:22:11 +0000 | [diff] [blame] | 242 | IO.mapOptional("UseTab", Style.UseTab); |
Manuel Klimek | a8eb914 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 243 | IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces); |
Daniel Jasper | b55acad | 2013-08-20 12:36:34 +0000 | [diff] [blame] | 244 | IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses); |
Daniel Jasper | ad981f8 | 2014-08-26 11:41:14 +0000 | [diff] [blame] | 245 | IO.mapOptional("SpacesInSquareBrackets", Style.SpacesInSquareBrackets); |
Daniel Jasper | dd978ae | 2013-10-29 14:52:02 +0000 | [diff] [blame] | 246 | IO.mapOptional("SpacesInAngles", Style.SpacesInAngles); |
Daniel Jasper | f110e20 | 2013-08-21 08:39:01 +0000 | [diff] [blame] | 247 | IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses); |
Daniel Jasper | b55acad | 2013-08-20 12:36:34 +0000 | [diff] [blame] | 248 | IO.mapOptional("SpacesInCStyleCastParentheses", |
| 249 | Style.SpacesInCStyleCastParentheses); |
Daniel Jasper | db986eb | 2014-09-03 07:37:29 +0000 | [diff] [blame] | 250 | IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast); |
Daniel Jasper | b2e10a5 | 2014-01-15 15:09:08 +0000 | [diff] [blame] | 251 | IO.mapOptional("SpacesInContainerLiterals", |
| 252 | Style.SpacesInContainerLiterals); |
Daniel Jasper | d94bff3 | 2013-09-25 15:15:02 +0000 | [diff] [blame] | 253 | IO.mapOptional("SpaceBeforeAssignmentOperators", |
| 254 | Style.SpaceBeforeAssignmentOperators); |
Daniel Jasper | 6633ab8 | 2013-10-18 10:38:14 +0000 | [diff] [blame] | 255 | IO.mapOptional("ContinuationIndentWidth", Style.ContinuationIndentWidth); |
Alexander Kornienko | ce9161a | 2014-01-02 15:13:14 +0000 | [diff] [blame] | 256 | IO.mapOptional("CommentPragmas", Style.CommentPragmas); |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 257 | IO.mapOptional("ForEachMacros", Style.ForEachMacros); |
Alexander Kornienko | fdca83d | 2013-12-10 10:18:34 +0000 | [diff] [blame] | 258 | |
| 259 | // For backward compatibility. |
| 260 | if (!IO.outputting()) { |
| 261 | IO.mapOptional("SpaceAfterControlStatementKeyword", |
| 262 | Style.SpaceBeforeParens); |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 263 | IO.mapOptional("PointerBindsToType", Style.PointerAlignment); |
| 264 | IO.mapOptional("DerivePointerBinding", Style.DerivePointerAlignment); |
Alexander Kornienko | fdca83d | 2013-12-10 10:18:34 +0000 | [diff] [blame] | 265 | } |
| 266 | IO.mapOptional("SpaceBeforeParens", Style.SpaceBeforeParens); |
Daniel Jasper | c64b09a | 2014-05-22 15:12:22 +0000 | [diff] [blame] | 267 | IO.mapOptional("DisableFormat", Style.DisableFormat); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 268 | } |
| 269 | }; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 270 | |
| 271 | // Allows to read vector<FormatStyle> while keeping default values. |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 272 | // IO.getContext() should contain a pointer to the FormatStyle structure, that |
| 273 | // will be used to get default values for missing keys. |
| 274 | // If the first element has no Language specified, it will be treated as the |
| 275 | // default one for the following elements. |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 276 | template <> struct DocumentListTraits<std::vector<FormatStyle> > { |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 277 | static size_t size(IO &IO, std::vector<FormatStyle> &Seq) { |
| 278 | return Seq.size(); |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 279 | } |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 280 | static FormatStyle &element(IO &IO, std::vector<FormatStyle> &Seq, |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 281 | size_t Index) { |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 282 | if (Index >= Seq.size()) { |
| 283 | assert(Index == Seq.size()); |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 284 | FormatStyle Template; |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 285 | if (Seq.size() > 0 && Seq[0].Language == FormatStyle::LK_None) { |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 286 | Template = Seq[0]; |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 287 | } else { |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 288 | Template = *((const FormatStyle *)IO.getContext()); |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 289 | Template.Language = FormatStyle::LK_None; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 290 | } |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 291 | Seq.resize(Index + 1, Template); |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 292 | } |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 293 | return Seq[Index]; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 294 | } |
| 295 | }; |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 299 | namespace clang { |
| 300 | namespace format { |
| 301 | |
Rafael Espindola | 6d0d89b | 2014-06-12 03:31:26 +0000 | [diff] [blame] | 302 | const std::error_category &getParseCategory() { |
Rafael Espindola | d013670 | 2014-06-12 02:50:04 +0000 | [diff] [blame] | 303 | static ParseErrorCategory C; |
| 304 | return C; |
| 305 | } |
| 306 | std::error_code make_error_code(ParseError e) { |
Rafael Espindola | 6d0d89b | 2014-06-12 03:31:26 +0000 | [diff] [blame] | 307 | return std::error_code(static_cast<int>(e), getParseCategory()); |
Rafael Espindola | d013670 | 2014-06-12 02:50:04 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | const char *ParseErrorCategory::name() const LLVM_NOEXCEPT { |
| 311 | return "clang-format.parse_error"; |
| 312 | } |
| 313 | |
| 314 | std::string ParseErrorCategory::message(int EV) const { |
| 315 | switch (static_cast<ParseError>(EV)) { |
| 316 | case ParseError::Success: |
| 317 | return "Success"; |
| 318 | case ParseError::Error: |
| 319 | return "Invalid argument"; |
| 320 | case ParseError::Unsuitable: |
| 321 | return "Unsuitable"; |
| 322 | } |
Saleem Abdulrasool | fbfbaf6 | 2014-06-12 19:33:26 +0000 | [diff] [blame] | 323 | llvm_unreachable("unexpected parse error"); |
Rafael Espindola | d013670 | 2014-06-12 02:50:04 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 326 | FormatStyle getLLVMStyle() { |
| 327 | FormatStyle LLVMStyle; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 328 | LLVMStyle.Language = FormatStyle::LK_Cpp; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 329 | LLVMStyle.AccessModifierOffset = -2; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 330 | LLVMStyle.AlignEscapedNewlinesLeft = false; |
Daniel Jasper | 3aa9a6a | 2014-11-18 23:55:27 +0000 | [diff] [blame] | 331 | LLVMStyle.AlignAfterOpenBracket = true; |
Daniel Jasper | 3219e43 | 2014-12-02 13:24:51 +0000 | [diff] [blame] | 332 | LLVMStyle.AlignOperands = true; |
Daniel Jasper | 552f4a7 | 2013-07-31 23:55:15 +0000 | [diff] [blame] | 333 | LLVMStyle.AlignTrailingComments = true; |
Daniel Jasper | f7db433 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 334 | LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true; |
Daniel Jasper | d74cf40 | 2014-04-08 12:46:38 +0000 | [diff] [blame] | 335 | LLVMStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; |
Daniel Jasper | 17605d3 | 2014-05-14 09:33:35 +0000 | [diff] [blame] | 336 | LLVMStyle.AllowShortBlocksOnASingleLine = false; |
Daniel Jasper | b87899b | 2014-09-10 13:11:45 +0000 | [diff] [blame] | 337 | LLVMStyle.AllowShortCaseLabelsOnASingleLine = false; |
Daniel Jasper | 1b750ed | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 338 | LLVMStyle.AllowShortIfStatementsOnASingleLine = false; |
Daniel Jasper | 3a685df | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 339 | LLVMStyle.AllowShortLoopsOnASingleLine = false; |
Daniel Jasper | ca4ea1c | 2014-08-05 12:16:31 +0000 | [diff] [blame] | 340 | LLVMStyle.AlwaysBreakAfterDefinitionReturnType = false; |
Alexander Kornienko | 5861171 | 2013-07-04 12:02:44 +0000 | [diff] [blame] | 341 | LLVMStyle.AlwaysBreakBeforeMultilineStrings = false; |
Daniel Jasper | e33d4af | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 342 | LLVMStyle.AlwaysBreakTemplateDeclarations = false; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 343 | LLVMStyle.BinPackParameters = true; |
Daniel Jasper | 18210d7 | 2014-10-09 09:52:05 +0000 | [diff] [blame] | 344 | LLVMStyle.BinPackArguments = true; |
Daniel Jasper | ac043c9 | 2014-09-15 11:11:00 +0000 | [diff] [blame] | 345 | LLVMStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_None; |
Daniel Jasper | 165b29e | 2013-11-08 00:57:11 +0000 | [diff] [blame] | 346 | LLVMStyle.BreakBeforeTernaryOperators = true; |
Daniel Jasper | e33d4af | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 347 | LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach; |
| 348 | LLVMStyle.BreakConstructorInitializersBeforeComma = false; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 349 | LLVMStyle.ColumnLimit = 80; |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 350 | LLVMStyle.CommentPragmas = "^ IWYU pragma:"; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 351 | LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false; |
Daniel Jasper | cdaffa4 | 2013-08-13 10:58:30 +0000 | [diff] [blame] | 352 | LLVMStyle.ConstructorInitializerIndentWidth = 4; |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 353 | LLVMStyle.ContinuationIndentWidth = 4; |
Chandler Carruth | f8b7266 | 2014-03-02 12:37:31 +0000 | [diff] [blame] | 354 | LLVMStyle.Cpp11BracedListStyle = true; |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 355 | LLVMStyle.DerivePointerAlignment = false; |
Daniel Jasper | b10cbc4 | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 356 | LLVMStyle.ExperimentalAutoDetectBinPacking = false; |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 357 | LLVMStyle.ForEachMacros.push_back("foreach"); |
| 358 | LLVMStyle.ForEachMacros.push_back("Q_FOREACH"); |
| 359 | LLVMStyle.ForEachMacros.push_back("BOOST_FOREACH"); |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 360 | LLVMStyle.IndentCaseLabels = false; |
Daniel Jasper | c75e1ef | 2014-07-09 08:42:42 +0000 | [diff] [blame] | 361 | LLVMStyle.IndentWrappedFunctionNames = false; |
Daniel Jasper | e33d4af | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 362 | LLVMStyle.IndentWidth = 2; |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 363 | LLVMStyle.TabWidth = 8; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 364 | LLVMStyle.MaxEmptyLinesToKeep = 1; |
Daniel Jasper | a26fc5c | 2014-03-21 13:43:14 +0000 | [diff] [blame] | 365 | LLVMStyle.KeepEmptyLinesAtTheStartOfBlocks = true; |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 366 | LLVMStyle.NamespaceIndentation = FormatStyle::NI_None; |
Daniel Jasper | 50d634b | 2014-10-28 16:53:38 +0000 | [diff] [blame] | 367 | LLVMStyle.ObjCBlockIndentWidth = 2; |
Daniel Jasper | e9beea2 | 2014-01-28 15:20:33 +0000 | [diff] [blame] | 368 | LLVMStyle.ObjCSpaceAfterProperty = false; |
Nico Weber | a608775 | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 369 | LLVMStyle.ObjCSpaceBeforeProtocolList = true; |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 370 | LLVMStyle.PointerAlignment = FormatStyle::PAS_Right; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 371 | LLVMStyle.SpacesBeforeTrailingComments = 1; |
Chandler Carruth | f8b7266 | 2014-03-02 12:37:31 +0000 | [diff] [blame] | 372 | LLVMStyle.Standard = FormatStyle::LS_Cpp11; |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 373 | LLVMStyle.UseTab = FormatStyle::UT_Never; |
Daniel Jasper | b55acad | 2013-08-20 12:36:34 +0000 | [diff] [blame] | 374 | LLVMStyle.SpacesInParentheses = false; |
Daniel Jasper | ad981f8 | 2014-08-26 11:41:14 +0000 | [diff] [blame] | 375 | LLVMStyle.SpacesInSquareBrackets = false; |
Daniel Jasper | b55acad | 2013-08-20 12:36:34 +0000 | [diff] [blame] | 376 | LLVMStyle.SpaceInEmptyParentheses = false; |
Daniel Jasper | b2e10a5 | 2014-01-15 15:09:08 +0000 | [diff] [blame] | 377 | LLVMStyle.SpacesInContainerLiterals = true; |
Daniel Jasper | b55acad | 2013-08-20 12:36:34 +0000 | [diff] [blame] | 378 | LLVMStyle.SpacesInCStyleCastParentheses = false; |
Daniel Jasper | db986eb | 2014-09-03 07:37:29 +0000 | [diff] [blame] | 379 | LLVMStyle.SpaceAfterCStyleCast = false; |
Alexander Kornienko | fdca83d | 2013-12-10 10:18:34 +0000 | [diff] [blame] | 380 | LLVMStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements; |
Daniel Jasper | d94bff3 | 2013-09-25 15:15:02 +0000 | [diff] [blame] | 381 | LLVMStyle.SpaceBeforeAssignmentOperators = true; |
Daniel Jasper | dd978ae | 2013-10-29 14:52:02 +0000 | [diff] [blame] | 382 | LLVMStyle.SpacesInAngles = false; |
Daniel Jasper | 4e9678f | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 383 | |
Daniel Jasper | 19a541e | 2013-12-19 16:45:34 +0000 | [diff] [blame] | 384 | LLVMStyle.PenaltyBreakComment = 300; |
Alexander Kornienko | fe7a57f | 2013-12-10 15:42:15 +0000 | [diff] [blame] | 385 | LLVMStyle.PenaltyBreakFirstLessLess = 120; |
| 386 | LLVMStyle.PenaltyBreakString = 1000; |
| 387 | LLVMStyle.PenaltyExcessCharacter = 1000000; |
Daniel Jasper | 4e9678f | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 388 | LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60; |
Daniel Jasper | 33b909c | 2013-10-25 14:29:37 +0000 | [diff] [blame] | 389 | LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19; |
Daniel Jasper | 4e9678f | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 390 | |
Daniel Jasper | c64b09a | 2014-05-22 15:12:22 +0000 | [diff] [blame] | 391 | LLVMStyle.DisableFormat = false; |
| 392 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 393 | return LLVMStyle; |
| 394 | } |
| 395 | |
Nico Weber | 514ecc8 | 2014-02-02 20:50:45 +0000 | [diff] [blame] | 396 | FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) { |
Alexander Kornienko | fe7a57f | 2013-12-10 15:42:15 +0000 | [diff] [blame] | 397 | FormatStyle GoogleStyle = getLLVMStyle(); |
Nico Weber | 514ecc8 | 2014-02-02 20:50:45 +0000 | [diff] [blame] | 398 | GoogleStyle.Language = Language; |
| 399 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 400 | GoogleStyle.AccessModifierOffset = -1; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 401 | GoogleStyle.AlignEscapedNewlinesLeft = true; |
Daniel Jasper | 085a2ed | 2013-04-24 13:46:00 +0000 | [diff] [blame] | 402 | GoogleStyle.AllowShortIfStatementsOnASingleLine = true; |
Daniel Jasper | 5bd0b9e | 2013-05-23 18:05:18 +0000 | [diff] [blame] | 403 | GoogleStyle.AllowShortLoopsOnASingleLine = true; |
Alexander Kornienko | 5861171 | 2013-07-04 12:02:44 +0000 | [diff] [blame] | 404 | GoogleStyle.AlwaysBreakBeforeMultilineStrings = true; |
Daniel Jasper | e33d4af | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 405 | GoogleStyle.AlwaysBreakTemplateDeclarations = true; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 406 | GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 407 | GoogleStyle.DerivePointerAlignment = true; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 408 | GoogleStyle.IndentCaseLabels = true; |
Daniel Jasper | a26fc5c | 2014-03-21 13:43:14 +0000 | [diff] [blame] | 409 | GoogleStyle.KeepEmptyLinesAtTheStartOfBlocks = false; |
Daniel Jasper | e9beea2 | 2014-01-28 15:20:33 +0000 | [diff] [blame] | 410 | GoogleStyle.ObjCSpaceAfterProperty = false; |
Nico Weber | a608775 | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 411 | GoogleStyle.ObjCSpaceBeforeProtocolList = false; |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 412 | GoogleStyle.PointerAlignment = FormatStyle::PAS_Left; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 413 | GoogleStyle.SpacesBeforeTrailingComments = 2; |
| 414 | GoogleStyle.Standard = FormatStyle::LS_Auto; |
Daniel Jasper | 4e9678f | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 415 | |
Daniel Jasper | 4e9678f | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 416 | GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200; |
Daniel Jasper | 33b909c | 2013-10-25 14:29:37 +0000 | [diff] [blame] | 417 | GoogleStyle.PenaltyBreakBeforeFirstCallParameter = 1; |
Daniel Jasper | 4e9678f | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 418 | |
Daniel Jasper | c58c70e | 2014-09-15 11:21:46 +0000 | [diff] [blame] | 419 | if (Language == FormatStyle::LK_Java) { |
Daniel Jasper | 3aa9a6a | 2014-11-18 23:55:27 +0000 | [diff] [blame] | 420 | GoogleStyle.AlignAfterOpenBracket = false; |
Daniel Jasper | 3219e43 | 2014-12-02 13:24:51 +0000 | [diff] [blame] | 421 | GoogleStyle.AlignOperands = false; |
Daniel Jasper | 9e70935 | 2014-11-26 10:43:58 +0000 | [diff] [blame] | 422 | GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; |
Daniel Jasper | c58c70e | 2014-09-15 11:21:46 +0000 | [diff] [blame] | 423 | GoogleStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; |
| 424 | GoogleStyle.ColumnLimit = 100; |
| 425 | GoogleStyle.SpaceAfterCStyleCast = true; |
Daniel Jasper | 61d8197 | 2014-11-14 08:22:46 +0000 | [diff] [blame] | 426 | GoogleStyle.SpacesBeforeTrailingComments = 1; |
Daniel Jasper | c58c70e | 2014-09-15 11:21:46 +0000 | [diff] [blame] | 427 | } else if (Language == FormatStyle::LK_JavaScript) { |
Daniel Jasper | e551bb7 | 2014-11-05 17:22:31 +0000 | [diff] [blame] | 428 | GoogleStyle.BreakBeforeTernaryOperators = false; |
Daniel Jasper | 8f83a90 | 2014-05-09 10:28:58 +0000 | [diff] [blame] | 429 | GoogleStyle.MaxEmptyLinesToKeep = 3; |
Nico Weber | 514ecc8 | 2014-02-02 20:50:45 +0000 | [diff] [blame] | 430 | GoogleStyle.SpacesInContainerLiterals = false; |
Daniel Jasper | 67f8ad2 | 2014-09-30 17:57:06 +0000 | [diff] [blame] | 431 | GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; |
Nico Weber | 514ecc8 | 2014-02-02 20:50:45 +0000 | [diff] [blame] | 432 | } else if (Language == FormatStyle::LK_Proto) { |
Daniel Jasper | d74cf40 | 2014-04-08 12:46:38 +0000 | [diff] [blame] | 433 | GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; |
Daniel Jasper | 783bac6 | 2014-04-15 09:54:30 +0000 | [diff] [blame] | 434 | GoogleStyle.SpacesInContainerLiterals = false; |
Nico Weber | 514ecc8 | 2014-02-02 20:50:45 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 437 | return GoogleStyle; |
| 438 | } |
| 439 | |
Nico Weber | 514ecc8 | 2014-02-02 20:50:45 +0000 | [diff] [blame] | 440 | FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) { |
| 441 | FormatStyle ChromiumStyle = getGoogleStyle(Language); |
Nico Weber | 450425c | 2014-11-26 16:43:18 +0000 | [diff] [blame] | 442 | if (Language == FormatStyle::LK_Java) { |
| 443 | ChromiumStyle.IndentWidth = 4; |
| 444 | ChromiumStyle.ContinuationIndentWidth = 8; |
| 445 | } else { |
| 446 | ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false; |
| 447 | ChromiumStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; |
| 448 | ChromiumStyle.AllowShortIfStatementsOnASingleLine = false; |
| 449 | ChromiumStyle.AllowShortLoopsOnASingleLine = false; |
| 450 | ChromiumStyle.BinPackParameters = false; |
| 451 | ChromiumStyle.DerivePointerAlignment = false; |
| 452 | } |
Daniel Jasper | 1b750ed | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 453 | return ChromiumStyle; |
| 454 | } |
| 455 | |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 456 | FormatStyle getMozillaStyle() { |
| 457 | FormatStyle MozillaStyle = getLLVMStyle(); |
| 458 | MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false; |
Chandler Carruth | f8b7266 | 2014-03-02 12:37:31 +0000 | [diff] [blame] | 459 | MozillaStyle.Cpp11BracedListStyle = false; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 460 | MozillaStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 461 | MozillaStyle.DerivePointerAlignment = true; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 462 | MozillaStyle.IndentCaseLabels = true; |
Daniel Jasper | e9beea2 | 2014-01-28 15:20:33 +0000 | [diff] [blame] | 463 | MozillaStyle.ObjCSpaceAfterProperty = true; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 464 | MozillaStyle.ObjCSpaceBeforeProtocolList = false; |
| 465 | MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200; |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 466 | MozillaStyle.PointerAlignment = FormatStyle::PAS_Left; |
Chandler Carruth | f8b7266 | 2014-03-02 12:37:31 +0000 | [diff] [blame] | 467 | MozillaStyle.Standard = FormatStyle::LS_Cpp03; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 468 | return MozillaStyle; |
| 469 | } |
| 470 | |
Daniel Jasper | ffefb3d | 2013-07-24 13:10:59 +0000 | [diff] [blame] | 471 | FormatStyle getWebKitStyle() { |
| 472 | FormatStyle Style = getLLVMStyle(); |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 473 | Style.AccessModifierOffset = -4; |
Daniel Jasper | 3aa9a6a | 2014-11-18 23:55:27 +0000 | [diff] [blame] | 474 | Style.AlignAfterOpenBracket = false; |
Daniel Jasper | 3219e43 | 2014-12-02 13:24:51 +0000 | [diff] [blame] | 475 | Style.AlignOperands = false; |
Daniel Jasper | 552f4a7 | 2013-07-31 23:55:15 +0000 | [diff] [blame] | 476 | Style.AlignTrailingComments = false; |
Daniel Jasper | ac043c9 | 2014-09-15 11:11:00 +0000 | [diff] [blame] | 477 | Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 478 | Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; |
Daniel Jasper | e33d4af | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 479 | Style.BreakConstructorInitializersBeforeComma = true; |
Chandler Carruth | f8b7266 | 2014-03-02 12:37:31 +0000 | [diff] [blame] | 480 | Style.Cpp11BracedListStyle = false; |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 481 | Style.ColumnLimit = 0; |
Daniel Jasper | e33d4af | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 482 | Style.IndentWidth = 4; |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 483 | Style.NamespaceIndentation = FormatStyle::NI_Inner; |
Daniel Jasper | 50d634b | 2014-10-28 16:53:38 +0000 | [diff] [blame] | 484 | Style.ObjCBlockIndentWidth = 4; |
Daniel Jasper | e9beea2 | 2014-01-28 15:20:33 +0000 | [diff] [blame] | 485 | Style.ObjCSpaceAfterProperty = true; |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 486 | Style.PointerAlignment = FormatStyle::PAS_Left; |
Chandler Carruth | f8b7266 | 2014-03-02 12:37:31 +0000 | [diff] [blame] | 487 | Style.Standard = FormatStyle::LS_Cpp03; |
Daniel Jasper | ffefb3d | 2013-07-24 13:10:59 +0000 | [diff] [blame] | 488 | return Style; |
| 489 | } |
| 490 | |
Alexander Kornienko | fe7a57f | 2013-12-10 15:42:15 +0000 | [diff] [blame] | 491 | FormatStyle getGNUStyle() { |
| 492 | FormatStyle Style = getLLVMStyle(); |
Daniel Jasper | ca4ea1c | 2014-08-05 12:16:31 +0000 | [diff] [blame] | 493 | Style.AlwaysBreakAfterDefinitionReturnType = true; |
Daniel Jasper | ac043c9 | 2014-09-15 11:11:00 +0000 | [diff] [blame] | 494 | Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 495 | Style.BreakBeforeBraces = FormatStyle::BS_GNU; |
Alexander Kornienko | fe7a57f | 2013-12-10 15:42:15 +0000 | [diff] [blame] | 496 | Style.BreakBeforeTernaryOperators = true; |
Chandler Carruth | f8b7266 | 2014-03-02 12:37:31 +0000 | [diff] [blame] | 497 | Style.Cpp11BracedListStyle = false; |
Alexander Kornienko | fe7a57f | 2013-12-10 15:42:15 +0000 | [diff] [blame] | 498 | Style.ColumnLimit = 79; |
Alexander Kornienko | fe7a57f | 2013-12-10 15:42:15 +0000 | [diff] [blame] | 499 | Style.SpaceBeforeParens = FormatStyle::SBPO_Always; |
Chandler Carruth | f8b7266 | 2014-03-02 12:37:31 +0000 | [diff] [blame] | 500 | Style.Standard = FormatStyle::LS_Cpp03; |
Alexander Kornienko | fe7a57f | 2013-12-10 15:42:15 +0000 | [diff] [blame] | 501 | return Style; |
| 502 | } |
| 503 | |
Daniel Jasper | c64b09a | 2014-05-22 15:12:22 +0000 | [diff] [blame] | 504 | FormatStyle getNoStyle() { |
| 505 | FormatStyle NoStyle = getLLVMStyle(); |
| 506 | NoStyle.DisableFormat = true; |
| 507 | return NoStyle; |
| 508 | } |
| 509 | |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 510 | bool getPredefinedStyle(StringRef Name, FormatStyle::LanguageKind Language, |
| 511 | FormatStyle *Style) { |
| 512 | if (Name.equals_lower("llvm")) { |
Alexander Kornienko | 006b5c8 | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 513 | *Style = getLLVMStyle(); |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 514 | } else if (Name.equals_lower("chromium")) { |
Nico Weber | 514ecc8 | 2014-02-02 20:50:45 +0000 | [diff] [blame] | 515 | *Style = getChromiumStyle(Language); |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 516 | } else if (Name.equals_lower("mozilla")) { |
Alexander Kornienko | 006b5c8 | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 517 | *Style = getMozillaStyle(); |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 518 | } else if (Name.equals_lower("google")) { |
Nico Weber | 514ecc8 | 2014-02-02 20:50:45 +0000 | [diff] [blame] | 519 | *Style = getGoogleStyle(Language); |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 520 | } else if (Name.equals_lower("webkit")) { |
Daniel Jasper | ffefb3d | 2013-07-24 13:10:59 +0000 | [diff] [blame] | 521 | *Style = getWebKitStyle(); |
Alexander Kornienko | fe7a57f | 2013-12-10 15:42:15 +0000 | [diff] [blame] | 522 | } else if (Name.equals_lower("gnu")) { |
| 523 | *Style = getGNUStyle(); |
Daniel Jasper | c64b09a | 2014-05-22 15:12:22 +0000 | [diff] [blame] | 524 | } else if (Name.equals_lower("none")) { |
| 525 | *Style = getNoStyle(); |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 526 | } else { |
Alexander Kornienko | 006b5c8 | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 527 | return false; |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 528 | } |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 529 | |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 530 | Style->Language = Language; |
Alexander Kornienko | 006b5c8 | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 531 | return true; |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Rafael Espindola | c080917 | 2014-06-12 14:02:15 +0000 | [diff] [blame] | 534 | std::error_code parseConfiguration(StringRef Text, FormatStyle *Style) { |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 535 | assert(Style); |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 536 | FormatStyle::LanguageKind Language = Style->Language; |
| 537 | assert(Language != FormatStyle::LK_None); |
Alexander Kornienko | 06e0033 | 2013-05-20 15:18:01 +0000 | [diff] [blame] | 538 | if (Text.trim().empty()) |
Rafael Espindola | d013670 | 2014-06-12 02:50:04 +0000 | [diff] [blame] | 539 | return make_error_code(ParseError::Error); |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 540 | |
| 541 | std::vector<FormatStyle> Styles; |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 542 | llvm::yaml::Input Input(Text); |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 543 | // DocumentListTraits<vector<FormatStyle>> uses the context to get default |
| 544 | // values for the fields, keys for which are missing from the configuration. |
| 545 | // Mapping also uses the context to get the language to find the correct |
| 546 | // base style. |
| 547 | Input.setContext(Style); |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 548 | Input >> Styles; |
| 549 | if (Input.error()) |
| 550 | return Input.error(); |
| 551 | |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 552 | for (unsigned i = 0; i < Styles.size(); ++i) { |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 553 | // Ensures that only the first configuration can skip the Language option. |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 554 | if (Styles[i].Language == FormatStyle::LK_None && i != 0) |
Rafael Espindola | d013670 | 2014-06-12 02:50:04 +0000 | [diff] [blame] | 555 | return make_error_code(ParseError::Error); |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 556 | // Ensure that each language is configured at most once. |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 557 | for (unsigned j = 0; j < i; ++j) { |
| 558 | if (Styles[i].Language == Styles[j].Language) { |
| 559 | DEBUG(llvm::dbgs() |
| 560 | << "Duplicate languages in the config file on positions " << j |
| 561 | << " and " << i << "\n"); |
Rafael Espindola | d013670 | 2014-06-12 02:50:04 +0000 | [diff] [blame] | 562 | return make_error_code(ParseError::Error); |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 563 | } |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | // Look for a suitable configuration starting from the end, so we can |
| 567 | // find the configuration for the specific language first, and the default |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 568 | // configuration (which can only be at slot 0) after it. |
| 569 | for (int i = Styles.size() - 1; i >= 0; --i) { |
| 570 | if (Styles[i].Language == Language || |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 571 | Styles[i].Language == FormatStyle::LK_None) { |
| 572 | *Style = Styles[i]; |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 573 | Style->Language = Language; |
Rafael Espindola | d013670 | 2014-06-12 02:50:04 +0000 | [diff] [blame] | 574 | return make_error_code(ParseError::Success); |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 575 | } |
| 576 | } |
Rafael Espindola | d013670 | 2014-06-12 02:50:04 +0000 | [diff] [blame] | 577 | return make_error_code(ParseError::Unsuitable); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | std::string configurationAsText(const FormatStyle &Style) { |
| 581 | std::string Text; |
| 582 | llvm::raw_string_ostream Stream(Text); |
| 583 | llvm::yaml::Output Output(Stream); |
| 584 | // We use the same mapping method for input and output, so we need a non-const |
| 585 | // reference here. |
| 586 | FormatStyle NonConstStyle = Style; |
| 587 | Output << NonConstStyle; |
Alexander Kornienko | 9a38ec2 | 2013-05-13 12:56:35 +0000 | [diff] [blame] | 588 | return Stream.str(); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Craig Topper | af35e85 | 2013-06-30 22:29:28 +0000 | [diff] [blame] | 591 | namespace { |
| 592 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 593 | class FormatTokenLexer { |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 594 | public: |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 595 | FormatTokenLexer(SourceManager &SourceMgr, FileID ID, FormatStyle &Style, |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 596 | encoding::Encoding Encoding) |
Craig Topper | 2145bc0 | 2014-05-09 08:15:10 +0000 | [diff] [blame] | 597 | : FormatTok(nullptr), IsFirstToken(true), GreaterStashed(false), |
Daniel Jasper | d0ec0d6 | 2014-11-04 12:41:02 +0000 | [diff] [blame] | 598 | Column(0), TrailingWhitespace(0), SourceMgr(SourceMgr), ID(ID), |
| 599 | Style(Style), IdentTable(getFormattingLangOpts(Style)), |
| 600 | Keywords(IdentTable), Encoding(Encoding), FirstInLineIndex(0), |
| 601 | FormattingDisabled(false) { |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 602 | Lex.reset(new Lexer(ID, SourceMgr.getBuffer(ID), SourceMgr, |
| 603 | getFormattingLangOpts(Style))); |
| 604 | Lex->SetKeepWhitespaceMode(true); |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 605 | |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 606 | for (const std::string &ForEachMacro : Style.ForEachMacros) |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 607 | ForEachMacros.push_back(&IdentTable.get(ForEachMacro)); |
| 608 | std::sort(ForEachMacros.begin(), ForEachMacros.end()); |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 611 | ArrayRef<FormatToken *> lex() { |
| 612 | assert(Tokens.empty()); |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 613 | assert(FirstInLineIndex == 0); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 614 | do { |
| 615 | Tokens.push_back(getNextToken()); |
Alexander Kornienko | 9aa6240 | 2013-11-21 12:43:57 +0000 | [diff] [blame] | 616 | tryMergePreviousTokens(); |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 617 | if (Tokens.back()->NewlinesBefore > 0) |
| 618 | FirstInLineIndex = Tokens.size() - 1; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 619 | } while (Tokens.back()->Tok.isNot(tok::eof)); |
| 620 | return Tokens; |
| 621 | } |
| 622 | |
Daniel Jasper | d0ec0d6 | 2014-11-04 12:41:02 +0000 | [diff] [blame] | 623 | const AdditionalKeywords &getKeywords() { return Keywords; } |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 624 | |
| 625 | private: |
Alexander Kornienko | 9aa6240 | 2013-11-21 12:43:57 +0000 | [diff] [blame] | 626 | void tryMergePreviousTokens() { |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 627 | if (tryMerge_TMacro()) |
| 628 | return; |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 629 | if (tryMergeConflictMarkers()) |
| 630 | return; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 631 | |
| 632 | if (Style.Language == FormatStyle::LK_JavaScript) { |
Daniel Jasper | f9ae312 | 2014-05-08 07:01:45 +0000 | [diff] [blame] | 633 | if (tryMergeJSRegexLiteral()) |
| 634 | return; |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 635 | if (tryMergeEscapeSequence()) |
| 636 | return; |
Daniel Jasper | f9ae312 | 2014-05-08 07:01:45 +0000 | [diff] [blame] | 637 | |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 638 | static tok::TokenKind JSIdentity[] = { tok::equalequal, tok::equal }; |
| 639 | static tok::TokenKind JSNotIdentity[] = { tok::exclaimequal, tok::equal }; |
| 640 | static tok::TokenKind JSShiftEqual[] = { tok::greater, tok::greater, |
| 641 | tok::greaterequal }; |
Daniel Jasper | 7821439 | 2014-05-19 07:27:02 +0000 | [diff] [blame] | 642 | static tok::TokenKind JSRightArrow[] = { tok::equal, tok::greater }; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 643 | // FIXME: We probably need to change token type to mimic operator with the |
| 644 | // correct priority. |
| 645 | if (tryMergeTokens(JSIdentity)) |
| 646 | return; |
| 647 | if (tryMergeTokens(JSNotIdentity)) |
| 648 | return; |
| 649 | if (tryMergeTokens(JSShiftEqual)) |
| 650 | return; |
Daniel Jasper | 7821439 | 2014-05-19 07:27:02 +0000 | [diff] [blame] | 651 | if (tryMergeTokens(JSRightArrow)) |
| 652 | return; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 653 | } |
Alexander Kornienko | 9aa6240 | 2013-11-21 12:43:57 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 656 | bool tryMergeTokens(ArrayRef<tok::TokenKind> Kinds) { |
| 657 | if (Tokens.size() < Kinds.size()) |
Alexander Kornienko | 9aa6240 | 2013-11-21 12:43:57 +0000 | [diff] [blame] | 658 | return false; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 659 | |
| 660 | SmallVectorImpl<FormatToken *>::const_iterator First = |
| 661 | Tokens.end() - Kinds.size(); |
| 662 | if (!First[0]->is(Kinds[0])) |
Alexander Kornienko | 9aa6240 | 2013-11-21 12:43:57 +0000 | [diff] [blame] | 663 | return false; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 664 | unsigned AddLength = 0; |
| 665 | for (unsigned i = 1; i < Kinds.size(); ++i) { |
| 666 | if (!First[i]->is(Kinds[i]) || First[i]->WhitespaceRange.getBegin() != |
| 667 | First[i]->WhitespaceRange.getEnd()) |
| 668 | return false; |
| 669 | AddLength += First[i]->TokenText.size(); |
| 670 | } |
| 671 | Tokens.resize(Tokens.size() - Kinds.size() + 1); |
| 672 | First[0]->TokenText = StringRef(First[0]->TokenText.data(), |
| 673 | First[0]->TokenText.size() + AddLength); |
| 674 | First[0]->ColumnWidth += AddLength; |
Alexander Kornienko | 9aa6240 | 2013-11-21 12:43:57 +0000 | [diff] [blame] | 675 | return true; |
| 676 | } |
| 677 | |
Daniel Jasper | fb4333b | 2014-05-12 11:29:50 +0000 | [diff] [blame] | 678 | // Tries to merge an escape sequence, i.e. a "\\" and the following |
Alp Toker | c3f36af | 2014-05-15 01:35:53 +0000 | [diff] [blame] | 679 | // character. Use e.g. inside JavaScript regex literals. |
Daniel Jasper | fb4333b | 2014-05-12 11:29:50 +0000 | [diff] [blame] | 680 | bool tryMergeEscapeSequence() { |
| 681 | if (Tokens.size() < 2) |
| 682 | return false; |
| 683 | FormatToken *Previous = Tokens[Tokens.size() - 2]; |
Daniel Jasper | 49a9a28 | 2014-10-29 16:51:38 +0000 | [diff] [blame] | 684 | if (Previous->isNot(tok::unknown) || Previous->TokenText != "\\") |
Daniel Jasper | fb4333b | 2014-05-12 11:29:50 +0000 | [diff] [blame] | 685 | return false; |
Daniel Jasper | 49a9a28 | 2014-10-29 16:51:38 +0000 | [diff] [blame] | 686 | ++Previous->ColumnWidth; |
Daniel Jasper | fb4333b | 2014-05-12 11:29:50 +0000 | [diff] [blame] | 687 | StringRef Text = Previous->TokenText; |
Daniel Jasper | 49a9a28 | 2014-10-29 16:51:38 +0000 | [diff] [blame] | 688 | Previous->TokenText = StringRef(Text.data(), Text.size() + 1); |
| 689 | resetLexer(SourceMgr.getFileOffset(Tokens.back()->Tok.getLocation()) + 1); |
Daniel Jasper | fb4333b | 2014-05-12 11:29:50 +0000 | [diff] [blame] | 690 | Tokens.resize(Tokens.size() - 1); |
Daniel Jasper | 49a9a28 | 2014-10-29 16:51:38 +0000 | [diff] [blame] | 691 | Column = Previous->OriginalColumn + Previous->ColumnWidth; |
Daniel Jasper | fb4333b | 2014-05-12 11:29:50 +0000 | [diff] [blame] | 692 | return true; |
| 693 | } |
| 694 | |
Daniel Jasper | f9ae312 | 2014-05-08 07:01:45 +0000 | [diff] [blame] | 695 | // Try to determine whether the current token ends a JavaScript regex literal. |
| 696 | // We heuristically assume that this is a regex literal if we find two |
| 697 | // unescaped slashes on a line and the token before the first slash is one of |
Daniel Jasper | f7405c1 | 2014-05-08 07:45:18 +0000 | [diff] [blame] | 698 | // "(;,{}![:?", a binary operator or 'return', as those cannot be followed by |
| 699 | // a division. |
Daniel Jasper | f9ae312 | 2014-05-08 07:01:45 +0000 | [diff] [blame] | 700 | bool tryMergeJSRegexLiteral() { |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 701 | if (Tokens.size() < 2) |
| 702 | return false; |
| 703 | // If a regex literal ends in "\//", this gets represented by an unknown |
| 704 | // token "\" and a comment. |
| 705 | bool MightEndWithEscapedSlash = |
| 706 | Tokens.back()->is(tok::comment) && |
| 707 | Tokens.back()->TokenText.startswith("//") && |
| 708 | Tokens[Tokens.size() - 2]->TokenText == "\\"; |
| 709 | if (!MightEndWithEscapedSlash && |
| 710 | (Tokens.back()->isNot(tok::slash) || |
| 711 | (Tokens[Tokens.size() - 2]->is(tok::unknown) && |
| 712 | Tokens[Tokens.size() - 2]->TokenText == "\\"))) |
Daniel Jasper | f9ae312 | 2014-05-08 07:01:45 +0000 | [diff] [blame] | 713 | return false; |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 714 | unsigned TokenCount = 0; |
| 715 | unsigned LastColumn = Tokens.back()->OriginalColumn; |
| 716 | for (auto I = Tokens.rbegin() + 1, E = Tokens.rend(); I != E; ++I) { |
| 717 | ++TokenCount; |
| 718 | if (I[0]->is(tok::slash) && I + 1 != E && |
| 719 | (I[1]->isOneOf(tok::l_paren, tok::semi, tok::l_brace, tok::r_brace, |
| 720 | tok::exclaim, tok::l_square, tok::colon, tok::comma, |
| 721 | tok::question, tok::kw_return) || |
| 722 | I[1]->isBinaryOperator())) { |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 723 | if (MightEndWithEscapedSlash) { |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 724 | // This regex literal ends in '\//'. Skip past the '//' of the last |
| 725 | // token and re-start lexing from there. |
Daniel Jasper | 49a9a28 | 2014-10-29 16:51:38 +0000 | [diff] [blame] | 726 | SourceLocation Loc = Tokens.back()->Tok.getLocation(); |
| 727 | resetLexer(SourceMgr.getFileOffset(Loc) + 2); |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 728 | } |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 729 | Tokens.resize(Tokens.size() - TokenCount); |
| 730 | Tokens.back()->Tok.setKind(tok::unknown); |
| 731 | Tokens.back()->Type = TT_RegexLiteral; |
| 732 | Tokens.back()->ColumnWidth += LastColumn - I[0]->OriginalColumn; |
| 733 | return true; |
| 734 | } |
| 735 | |
| 736 | // There can't be a newline inside a regex literal. |
| 737 | if (I[0]->NewlinesBefore > 0) |
| 738 | return false; |
| 739 | } |
| 740 | return false; |
Daniel Jasper | f9ae312 | 2014-05-08 07:01:45 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Alexander Kornienko | 9aa6240 | 2013-11-21 12:43:57 +0000 | [diff] [blame] | 743 | bool tryMerge_TMacro() { |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 744 | if (Tokens.size() < 4) |
Alexander Kornienko | 9aa6240 | 2013-11-21 12:43:57 +0000 | [diff] [blame] | 745 | return false; |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 746 | FormatToken *Last = Tokens.back(); |
| 747 | if (!Last->is(tok::r_paren)) |
Alexander Kornienko | 9aa6240 | 2013-11-21 12:43:57 +0000 | [diff] [blame] | 748 | return false; |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 749 | |
| 750 | FormatToken *String = Tokens[Tokens.size() - 2]; |
| 751 | if (!String->is(tok::string_literal) || String->IsMultiline) |
Alexander Kornienko | 9aa6240 | 2013-11-21 12:43:57 +0000 | [diff] [blame] | 752 | return false; |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 753 | |
| 754 | if (!Tokens[Tokens.size() - 3]->is(tok::l_paren)) |
Alexander Kornienko | 9aa6240 | 2013-11-21 12:43:57 +0000 | [diff] [blame] | 755 | return false; |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 756 | |
| 757 | FormatToken *Macro = Tokens[Tokens.size() - 4]; |
| 758 | if (Macro->TokenText != "_T") |
Alexander Kornienko | 9aa6240 | 2013-11-21 12:43:57 +0000 | [diff] [blame] | 759 | return false; |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 760 | |
| 761 | const char *Start = Macro->TokenText.data(); |
| 762 | const char *End = Last->TokenText.data() + Last->TokenText.size(); |
| 763 | String->TokenText = StringRef(Start, End - Start); |
| 764 | String->IsFirst = Macro->IsFirst; |
| 765 | String->LastNewlineOffset = Macro->LastNewlineOffset; |
| 766 | String->WhitespaceRange = Macro->WhitespaceRange; |
| 767 | String->OriginalColumn = Macro->OriginalColumn; |
| 768 | String->ColumnWidth = encoding::columnWidthWithTabs( |
| 769 | String->TokenText, String->OriginalColumn, Style.TabWidth, Encoding); |
| 770 | |
| 771 | Tokens.pop_back(); |
| 772 | Tokens.pop_back(); |
| 773 | Tokens.pop_back(); |
| 774 | Tokens.back() = String; |
Alexander Kornienko | 9aa6240 | 2013-11-21 12:43:57 +0000 | [diff] [blame] | 775 | return true; |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 778 | bool tryMergeConflictMarkers() { |
| 779 | if (Tokens.back()->NewlinesBefore == 0 && Tokens.back()->isNot(tok::eof)) |
| 780 | return false; |
| 781 | |
| 782 | // Conflict lines look like: |
| 783 | // <marker> <text from the vcs> |
| 784 | // For example: |
| 785 | // >>>>>>> /file/in/file/system at revision 1234 |
| 786 | // |
| 787 | // We merge all tokens in a line that starts with a conflict marker |
| 788 | // into a single token with a special token type that the unwrapped line |
| 789 | // parser will use to correctly rebuild the underlying code. |
| 790 | |
| 791 | FileID ID; |
| 792 | // Get the position of the first token in the line. |
| 793 | unsigned FirstInLineOffset; |
| 794 | std::tie(ID, FirstInLineOffset) = SourceMgr.getDecomposedLoc( |
| 795 | Tokens[FirstInLineIndex]->getStartOfNonWhitespace()); |
| 796 | StringRef Buffer = SourceMgr.getBuffer(ID)->getBuffer(); |
| 797 | // Calculate the offset of the start of the current line. |
| 798 | auto LineOffset = Buffer.rfind('\n', FirstInLineOffset); |
| 799 | if (LineOffset == StringRef::npos) { |
| 800 | LineOffset = 0; |
| 801 | } else { |
| 802 | ++LineOffset; |
| 803 | } |
| 804 | |
| 805 | auto FirstSpace = Buffer.find_first_of(" \n", LineOffset); |
| 806 | StringRef LineStart; |
| 807 | if (FirstSpace == StringRef::npos) { |
| 808 | LineStart = Buffer.substr(LineOffset); |
| 809 | } else { |
| 810 | LineStart = Buffer.substr(LineOffset, FirstSpace - LineOffset); |
| 811 | } |
| 812 | |
| 813 | TokenType Type = TT_Unknown; |
| 814 | if (LineStart == "<<<<<<<" || LineStart == ">>>>") { |
| 815 | Type = TT_ConflictStart; |
| 816 | } else if (LineStart == "|||||||" || LineStart == "=======" || |
| 817 | LineStart == "====") { |
| 818 | Type = TT_ConflictAlternative; |
| 819 | } else if (LineStart == ">>>>>>>" || LineStart == "<<<<") { |
| 820 | Type = TT_ConflictEnd; |
| 821 | } |
| 822 | |
| 823 | if (Type != TT_Unknown) { |
| 824 | FormatToken *Next = Tokens.back(); |
| 825 | |
| 826 | Tokens.resize(FirstInLineIndex + 1); |
| 827 | // We do not need to build a complete token here, as we will skip it |
| 828 | // during parsing anyway (as we must not touch whitespace around conflict |
| 829 | // markers). |
| 830 | Tokens.back()->Type = Type; |
| 831 | Tokens.back()->Tok.setKind(tok::kw___unknown_anytype); |
| 832 | |
| 833 | Tokens.push_back(Next); |
| 834 | return true; |
| 835 | } |
| 836 | |
| 837 | return false; |
| 838 | } |
| 839 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 840 | FormatToken *getNextToken() { |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 841 | if (GreaterStashed) { |
Manuel Klimek | 591ab5a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 842 | // Create a synthesized second '>' token. |
Manuel Klimek | 31c8592 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 843 | // FIXME: Increment Column and set OriginalColumn. |
Manuel Klimek | 591ab5a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 844 | Token Greater = FormatTok->Tok; |
| 845 | FormatTok = new (Allocator.Allocate()) FormatToken; |
| 846 | FormatTok->Tok = Greater; |
Manuel Klimek | 5c24cca | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 847 | SourceLocation GreaterLocation = |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 848 | FormatTok->Tok.getLocation().getLocWithOffset(1); |
| 849 | FormatTok->WhitespaceRange = |
| 850 | SourceRange(GreaterLocation, GreaterLocation); |
Alexander Kornienko | ee4ca9b | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 851 | FormatTok->TokenText = ">"; |
Alexander Kornienko | 39856b7 | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 852 | FormatTok->ColumnWidth = 1; |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 853 | GreaterStashed = false; |
| 854 | return FormatTok; |
| 855 | } |
| 856 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 857 | FormatTok = new (Allocator.Allocate()) FormatToken; |
Daniel Jasper | 8369aa5 | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 858 | readRawToken(*FormatTok); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 859 | SourceLocation WhitespaceStart = |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 860 | FormatTok->Tok.getLocation().getLocWithOffset(-TrailingWhitespace); |
Alexander Kornienko | 393e308 | 2013-11-13 14:04:17 +0000 | [diff] [blame] | 861 | FormatTok->IsFirst = IsFirstToken; |
| 862 | IsFirstToken = false; |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 863 | |
| 864 | // Consume and record whitespace until we find a significant token. |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 865 | unsigned WhitespaceLength = TrailingWhitespace; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 866 | while (FormatTok->Tok.is(tok::unknown)) { |
Manuel Klimek | 31c8592 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 867 | for (int i = 0, e = FormatTok->TokenText.size(); i != e; ++i) { |
| 868 | switch (FormatTok->TokenText[i]) { |
| 869 | case '\n': |
| 870 | ++FormatTok->NewlinesBefore; |
| 871 | // FIXME: This is technically incorrect, as it could also |
| 872 | // be a literal backslash at the end of the line. |
Alexander Kornienko | 9e649af | 2013-09-11 12:25:57 +0000 | [diff] [blame] | 873 | if (i == 0 || (FormatTok->TokenText[i - 1] != '\\' && |
| 874 | (FormatTok->TokenText[i - 1] != '\r' || i == 1 || |
| 875 | FormatTok->TokenText[i - 2] != '\\'))) |
Manuel Klimek | 31c8592 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 876 | FormatTok->HasUnescapedNewline = true; |
| 877 | FormatTok->LastNewlineOffset = WhitespaceLength + i + 1; |
| 878 | Column = 0; |
| 879 | break; |
Daniel Jasper | 877615c | 2013-10-11 19:45:02 +0000 | [diff] [blame] | 880 | case '\r': |
| 881 | case '\f': |
| 882 | case '\v': |
| 883 | Column = 0; |
| 884 | break; |
Manuel Klimek | 31c8592 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 885 | case ' ': |
| 886 | ++Column; |
| 887 | break; |
| 888 | case '\t': |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 889 | Column += Style.TabWidth - Column % Style.TabWidth; |
Manuel Klimek | 31c8592 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 890 | break; |
Daniel Jasper | 877615c | 2013-10-11 19:45:02 +0000 | [diff] [blame] | 891 | case '\\': |
| 892 | ++Column; |
| 893 | if (i + 1 == e || (FormatTok->TokenText[i + 1] != '\r' && |
| 894 | FormatTok->TokenText[i + 1] != '\n')) |
| 895 | FormatTok->Type = TT_ImplicitStringLiteral; |
| 896 | break; |
Manuel Klimek | 31c8592 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 897 | default: |
Daniel Jasper | 877615c | 2013-10-11 19:45:02 +0000 | [diff] [blame] | 898 | FormatTok->Type = TT_ImplicitStringLiteral; |
Manuel Klimek | 31c8592 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 899 | ++Column; |
| 900 | break; |
| 901 | } |
| 902 | } |
| 903 | |
Daniel Jasper | a98b7b0 | 2014-11-25 10:05:17 +0000 | [diff] [blame] | 904 | if (FormatTok->is(TT_ImplicitStringLiteral)) |
Daniel Jasper | 877615c | 2013-10-11 19:45:02 +0000 | [diff] [blame] | 905 | break; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 906 | WhitespaceLength += FormatTok->Tok.getLength(); |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 907 | |
Daniel Jasper | 8369aa5 | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 908 | readRawToken(*FormatTok); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 909 | } |
Manuel Klimek | ef92069 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 910 | |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 911 | // In case the token starts with escaped newlines, we want to |
| 912 | // take them into account as whitespace - this pattern is quite frequent |
| 913 | // in macro definitions. |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 914 | // FIXME: Add a more explicit test. |
Daniel Jasper | 8369aa5 | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 915 | while (FormatTok->TokenText.size() > 1 && FormatTok->TokenText[0] == '\\' && |
| 916 | FormatTok->TokenText[1] == '\n') { |
Manuel Klimek | 1fcbe67 | 2014-04-11 12:27:47 +0000 | [diff] [blame] | 917 | ++FormatTok->NewlinesBefore; |
Manuel Klimek | 5c24cca | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 918 | WhitespaceLength += 2; |
Manuel Klimek | 31c8592 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 919 | Column = 0; |
Daniel Jasper | 8369aa5 | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 920 | FormatTok->TokenText = FormatTok->TokenText.substr(2); |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 921 | } |
Alexander Kornienko | 39856b7 | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 922 | |
| 923 | FormatTok->WhitespaceRange = SourceRange( |
| 924 | WhitespaceStart, WhitespaceStart.getLocWithOffset(WhitespaceLength)); |
| 925 | |
Manuel Klimek | 31c8592 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 926 | FormatTok->OriginalColumn = Column; |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 927 | |
Alexander Kornienko | ee4ca9b | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 928 | TrailingWhitespace = 0; |
| 929 | if (FormatTok->Tok.is(tok::comment)) { |
Manuel Klimek | 31c8592 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 930 | // FIXME: Add the trimmed whitespace to Column. |
Daniel Jasper | 8369aa5 | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 931 | StringRef UntrimmedText = FormatTok->TokenText; |
Alexander Kornienko | 9ab4a77 | 2013-09-06 17:24:54 +0000 | [diff] [blame] | 932 | FormatTok->TokenText = FormatTok->TokenText.rtrim(" \t\v\f"); |
Daniel Jasper | 8369aa5 | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 933 | TrailingWhitespace = UntrimmedText.size() - FormatTok->TokenText.size(); |
Alexander Kornienko | ee4ca9b | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 934 | } else if (FormatTok->Tok.is(tok::raw_identifier)) { |
Daniel Jasper | 8369aa5 | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 935 | IdentifierInfo &Info = IdentTable.get(FormatTok->TokenText); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 936 | FormatTok->Tok.setIdentifierInfo(&Info); |
| 937 | FormatTok->Tok.setKind(Info.getTokenID()); |
Daniel Jasper | fe2cf66 | 2014-11-19 14:11:11 +0000 | [diff] [blame] | 938 | if (Style.Language == FormatStyle::LK_Java && |
| 939 | FormatTok->isOneOf(tok::kw_struct, tok::kw_union, tok::kw_delete)) { |
| 940 | FormatTok->Tok.setKind(tok::identifier); |
| 941 | FormatTok->Tok.setIdentifierInfo(nullptr); |
| 942 | } |
Alexander Kornienko | ee4ca9b | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 943 | } else if (FormatTok->Tok.is(tok::greatergreater)) { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 944 | FormatTok->Tok.setKind(tok::greater); |
Daniel Jasper | 8369aa5 | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 945 | FormatTok->TokenText = FormatTok->TokenText.substr(0, 1); |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 946 | GreaterStashed = true; |
| 947 | } |
| 948 | |
Alexander Kornienko | ee4ca9b | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 949 | // Now FormatTok is the next non-whitespace token. |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 950 | |
Alexander Kornienko | 39856b7 | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 951 | StringRef Text = FormatTok->TokenText; |
| 952 | size_t FirstNewlinePos = Text.find('\n'); |
Alexander Kornienko | 917f9e0 | 2013-09-10 12:29:48 +0000 | [diff] [blame] | 953 | if (FirstNewlinePos == StringRef::npos) { |
| 954 | // FIXME: ColumnWidth actually depends on the start column, we need to |
| 955 | // take this into account when the token is moved. |
| 956 | FormatTok->ColumnWidth = |
| 957 | encoding::columnWidthWithTabs(Text, Column, Style.TabWidth, Encoding); |
| 958 | Column += FormatTok->ColumnWidth; |
| 959 | } else { |
Alexander Kornienko | 39856b7 | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 960 | FormatTok->IsMultiline = true; |
Alexander Kornienko | 917f9e0 | 2013-09-10 12:29:48 +0000 | [diff] [blame] | 961 | // FIXME: ColumnWidth actually depends on the start column, we need to |
| 962 | // take this into account when the token is moved. |
| 963 | FormatTok->ColumnWidth = encoding::columnWidthWithTabs( |
| 964 | Text.substr(0, FirstNewlinePos), Column, Style.TabWidth, Encoding); |
| 965 | |
Alexander Kornienko | 39856b7 | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 966 | // The last line of the token always starts in column 0. |
| 967 | // Thus, the length can be precomputed even in the presence of tabs. |
| 968 | FormatTok->LastLineColumnWidth = encoding::columnWidthWithTabs( |
| 969 | Text.substr(Text.find_last_of('\n') + 1), 0, Style.TabWidth, |
| 970 | Encoding); |
Alexander Kornienko | 917f9e0 | 2013-09-10 12:29:48 +0000 | [diff] [blame] | 971 | Column = FormatTok->LastLineColumnWidth; |
Alexander Kornienko | 632abb9 | 2013-09-02 13:58:14 +0000 | [diff] [blame] | 972 | } |
Alexander Kornienko | 39856b7 | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 973 | |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 974 | FormatTok->IsForEachMacro = |
| 975 | std::binary_search(ForEachMacros.begin(), ForEachMacros.end(), |
| 976 | FormatTok->Tok.getIdentifierInfo()); |
| 977 | |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 978 | return FormatTok; |
| 979 | } |
| 980 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 981 | FormatToken *FormatTok; |
Alexander Kornienko | 393e308 | 2013-11-13 14:04:17 +0000 | [diff] [blame] | 982 | bool IsFirstToken; |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 983 | bool GreaterStashed; |
Manuel Klimek | 31c8592 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 984 | unsigned Column; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 985 | unsigned TrailingWhitespace; |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 986 | std::unique_ptr<Lexer> Lex; |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 987 | SourceManager &SourceMgr; |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 988 | FileID ID; |
Manuel Klimek | 31c8592 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 989 | FormatStyle &Style; |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 990 | IdentifierTable IdentTable; |
Daniel Jasper | d0ec0d6 | 2014-11-04 12:41:02 +0000 | [diff] [blame] | 991 | AdditionalKeywords Keywords; |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 992 | encoding::Encoding Encoding; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 993 | llvm::SpecificBumpPtrAllocator<FormatToken> Allocator; |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 994 | // Index (in 'Tokens') of the last token that starts a new line. |
| 995 | unsigned FirstInLineIndex; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 996 | SmallVector<FormatToken *, 16> Tokens; |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 997 | SmallVector<IdentifierInfo *, 8> ForEachMacros; |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 998 | |
NAKAMURA Takumi | 7160c4d | 2014-08-06 16:53:13 +0000 | [diff] [blame] | 999 | bool FormattingDisabled; |
Daniel Jasper | 47189443 | 2014-08-06 13:40:26 +0000 | [diff] [blame] | 1000 | |
Daniel Jasper | 8369aa5 | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 1001 | void readRawToken(FormatToken &Tok) { |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 1002 | Lex->LexFromRawLexer(Tok.Tok); |
Daniel Jasper | 8369aa5 | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 1003 | Tok.TokenText = StringRef(SourceMgr.getCharacterData(Tok.Tok.getLocation()), |
| 1004 | Tok.Tok.getLength()); |
Daniel Jasper | 8369aa5 | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 1005 | // For formatting, treat unterminated string literals like normal string |
| 1006 | // literals. |
Daniel Jasper | 86fee2f | 2014-01-31 12:49:42 +0000 | [diff] [blame] | 1007 | if (Tok.is(tok::unknown)) { |
| 1008 | if (!Tok.TokenText.empty() && Tok.TokenText[0] == '"') { |
| 1009 | Tok.Tok.setKind(tok::string_literal); |
| 1010 | Tok.IsUnterminatedLiteral = true; |
| 1011 | } else if (Style.Language == FormatStyle::LK_JavaScript && |
| 1012 | Tok.TokenText == "''") { |
| 1013 | Tok.Tok.setKind(tok::char_constant); |
| 1014 | } |
Daniel Jasper | 8369aa5 | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 1015 | } |
Roman Kashitsyn | 650ecb5 | 2014-09-11 14:47:20 +0000 | [diff] [blame] | 1016 | |
| 1017 | if (Tok.is(tok::comment) && (Tok.TokenText == "// clang-format on" || |
| 1018 | Tok.TokenText == "/* clang-format on */")) { |
Daniel Jasper | 47189443 | 2014-08-06 13:40:26 +0000 | [diff] [blame] | 1019 | FormattingDisabled = false; |
Roman Kashitsyn | 650ecb5 | 2014-09-11 14:47:20 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
Daniel Jasper | 47189443 | 2014-08-06 13:40:26 +0000 | [diff] [blame] | 1022 | Tok.Finalized = FormattingDisabled; |
Roman Kashitsyn | 650ecb5 | 2014-09-11 14:47:20 +0000 | [diff] [blame] | 1023 | |
| 1024 | if (Tok.is(tok::comment) && (Tok.TokenText == "// clang-format off" || |
| 1025 | Tok.TokenText == "/* clang-format off */")) { |
Daniel Jasper | 47189443 | 2014-08-06 13:40:26 +0000 | [diff] [blame] | 1026 | FormattingDisabled = true; |
Roman Kashitsyn | 650ecb5 | 2014-09-11 14:47:20 +0000 | [diff] [blame] | 1027 | } |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1028 | } |
Daniel Jasper | 49a9a28 | 2014-10-29 16:51:38 +0000 | [diff] [blame] | 1029 | |
| 1030 | void resetLexer(unsigned Offset) { |
| 1031 | StringRef Buffer = SourceMgr.getBufferData(ID); |
| 1032 | Lex.reset(new Lexer(SourceMgr.getLocForStartOfFile(ID), |
| 1033 | getFormattingLangOpts(Style), Buffer.begin(), |
| 1034 | Buffer.begin() + Offset, Buffer.end())); |
| 1035 | Lex->SetKeepWhitespaceMode(true); |
| 1036 | } |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1037 | }; |
| 1038 | |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 1039 | static StringRef getLanguageName(FormatStyle::LanguageKind Language) { |
| 1040 | switch (Language) { |
| 1041 | case FormatStyle::LK_Cpp: |
| 1042 | return "C++"; |
Daniel Jasper | c58c70e | 2014-09-15 11:21:46 +0000 | [diff] [blame] | 1043 | case FormatStyle::LK_Java: |
| 1044 | return "Java"; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 1045 | case FormatStyle::LK_JavaScript: |
| 1046 | return "JavaScript"; |
Daniel Jasper | 7052ce6 | 2014-01-19 09:04:08 +0000 | [diff] [blame] | 1047 | case FormatStyle::LK_Proto: |
| 1048 | return "Proto"; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 1049 | default: |
| 1050 | return "Unknown"; |
| 1051 | } |
| 1052 | } |
| 1053 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1054 | class Formatter : public UnwrappedLineConsumer { |
| 1055 | public: |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 1056 | Formatter(const FormatStyle &Style, SourceManager &SourceMgr, FileID ID, |
Benjamin Kramer | d0eed3a | 2014-10-03 18:52:48 +0000 | [diff] [blame] | 1057 | ArrayRef<CharSourceRange> Ranges) |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 1058 | : Style(Style), ID(ID), SourceMgr(SourceMgr), |
| 1059 | Whitespaces(SourceMgr, Style, |
| 1060 | inputUsesCRLF(SourceMgr.getBufferData(ID))), |
Daniel Jasper | bbf5c1c | 2013-11-05 19:10:03 +0000 | [diff] [blame] | 1061 | Ranges(Ranges.begin(), Ranges.end()), UnwrappedLines(1), |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 1062 | Encoding(encoding::detectEncoding(SourceMgr.getBufferData(ID))) { |
Daniel Jasper | fa21c07 | 2013-07-15 14:33:14 +0000 | [diff] [blame] | 1063 | DEBUG(llvm::dbgs() << "File encoding: " |
| 1064 | << (Encoding == encoding::Encoding_UTF8 ? "UTF8" |
| 1065 | : "unknown") |
| 1066 | << "\n"); |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 1067 | DEBUG(llvm::dbgs() << "Language: " << getLanguageName(Style.Language) |
| 1068 | << "\n"); |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1069 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1070 | |
Alexander Kornienko | 62b85b9 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1071 | tooling::Replacements format() { |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 1072 | tooling::Replacements Result; |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 1073 | FormatTokenLexer Tokens(SourceMgr, ID, Style, Encoding); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1074 | |
Daniel Jasper | d0ec0d6 | 2014-11-04 12:41:02 +0000 | [diff] [blame] | 1075 | UnwrappedLineParser Parser(Style, Tokens.getKeywords(), Tokens.lex(), |
| 1076 | *this); |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 1077 | bool StructuralError = Parser.parse(); |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 1078 | assert(UnwrappedLines.rbegin()->empty()); |
| 1079 | for (unsigned Run = 0, RunE = UnwrappedLines.size(); Run + 1 != RunE; |
| 1080 | ++Run) { |
| 1081 | DEBUG(llvm::dbgs() << "Run " << Run << "...\n"); |
| 1082 | SmallVector<AnnotatedLine *, 16> AnnotatedLines; |
| 1083 | for (unsigned i = 0, e = UnwrappedLines[Run].size(); i != e; ++i) { |
| 1084 | AnnotatedLines.push_back(new AnnotatedLine(UnwrappedLines[Run][i])); |
| 1085 | } |
| 1086 | tooling::Replacements RunResult = |
| 1087 | format(AnnotatedLines, StructuralError, Tokens); |
| 1088 | DEBUG({ |
| 1089 | llvm::dbgs() << "Replacements for run " << Run << ":\n"; |
| 1090 | for (tooling::Replacements::iterator I = RunResult.begin(), |
| 1091 | E = RunResult.end(); |
| 1092 | I != E; ++I) { |
| 1093 | llvm::dbgs() << I->toString() << "\n"; |
| 1094 | } |
| 1095 | }); |
| 1096 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
| 1097 | delete AnnotatedLines[i]; |
| 1098 | } |
| 1099 | Result.insert(RunResult.begin(), RunResult.end()); |
| 1100 | Whitespaces.reset(); |
| 1101 | } |
| 1102 | return Result; |
| 1103 | } |
| 1104 | |
| 1105 | tooling::Replacements format(SmallVectorImpl<AnnotatedLine *> &AnnotatedLines, |
| 1106 | bool StructuralError, FormatTokenLexer &Tokens) { |
Daniel Jasper | d0ec0d6 | 2014-11-04 12:41:02 +0000 | [diff] [blame] | 1107 | TokenAnnotator Annotator(Style, Tokens.getKeywords()); |
Alexander Kornienko | 62b85b9 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1108 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1109 | Annotator.annotate(*AnnotatedLines[i]); |
Alexander Kornienko | 62b85b9 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1110 | } |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 1111 | deriveLocalStyle(AnnotatedLines); |
Alexander Kornienko | 62b85b9 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1112 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1113 | Annotator.calculateFormattingInformation(*AnnotatedLines[i]); |
Alexander Kornienko | 62b85b9 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1114 | } |
Daniel Jasper | 5500f61 | 2013-11-25 11:08:59 +0000 | [diff] [blame] | 1115 | computeAffectedLines(AnnotatedLines.begin(), AnnotatedLines.end()); |
Daniel Jasper | b67cc42 | 2013-04-09 17:46:55 +0000 | [diff] [blame] | 1116 | |
Daniel Jasper | 1c5d9df | 2013-09-06 07:54:20 +0000 | [diff] [blame] | 1117 | Annotator.setCommentLineLevels(AnnotatedLines); |
Daniel Jasper | d0ec0d6 | 2014-11-04 12:41:02 +0000 | [diff] [blame] | 1118 | ContinuationIndenter Indenter(Style, Tokens.getKeywords(), SourceMgr, |
| 1119 | Whitespaces, Encoding, |
Daniel Jasper | bbf5c1c | 2013-11-05 19:10:03 +0000 | [diff] [blame] | 1120 | BinPackInconclusiveFunctions); |
Daniel Jasper | 5500f61 | 2013-11-25 11:08:59 +0000 | [diff] [blame] | 1121 | UnwrappedLineFormatter Formatter(&Indenter, &Whitespaces, Style); |
Daniel Jasper | bbf5c1c | 2013-11-05 19:10:03 +0000 | [diff] [blame] | 1122 | Formatter.format(AnnotatedLines, /*DryRun=*/false); |
Alexander Kornienko | 62b85b9 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1123 | return Whitespaces.generateReplacements(); |
| 1124 | } |
| 1125 | |
| 1126 | private: |
Daniel Jasper | 5500f61 | 2013-11-25 11:08:59 +0000 | [diff] [blame] | 1127 | // Determines which lines are affected by the SourceRanges given as input. |
Daniel Jasper | 9c19956 | 2013-11-28 15:58:55 +0000 | [diff] [blame] | 1128 | // Returns \c true if at least one line between I and E or one of their |
| 1129 | // children is affected. |
Daniel Jasper | 5500f61 | 2013-11-25 11:08:59 +0000 | [diff] [blame] | 1130 | bool computeAffectedLines(SmallVectorImpl<AnnotatedLine *>::iterator I, |
| 1131 | SmallVectorImpl<AnnotatedLine *>::iterator E) { |
| 1132 | bool SomeLineAffected = false; |
Craig Topper | 2145bc0 | 2014-05-09 08:15:10 +0000 | [diff] [blame] | 1133 | const AnnotatedLine *PreviousLine = nullptr; |
Daniel Jasper | 5500f61 | 2013-11-25 11:08:59 +0000 | [diff] [blame] | 1134 | while (I != E) { |
| 1135 | AnnotatedLine *Line = *I; |
| 1136 | Line->LeadingEmptyLinesAffected = affectsLeadingEmptyLines(*Line->First); |
| 1137 | |
| 1138 | // If a line is part of a preprocessor directive, it needs to be formatted |
| 1139 | // if any token within the directive is affected. |
| 1140 | if (Line->InPPDirective) { |
| 1141 | FormatToken *Last = Line->Last; |
| 1142 | SmallVectorImpl<AnnotatedLine *>::iterator PPEnd = I + 1; |
| 1143 | while (PPEnd != E && !(*PPEnd)->First->HasUnescapedNewline) { |
| 1144 | Last = (*PPEnd)->Last; |
| 1145 | ++PPEnd; |
| 1146 | } |
| 1147 | |
| 1148 | if (affectsTokenRange(*Line->First, *Last, |
| 1149 | /*IncludeLeadingNewlines=*/false)) { |
| 1150 | SomeLineAffected = true; |
| 1151 | markAllAsAffected(I, PPEnd); |
| 1152 | } |
| 1153 | I = PPEnd; |
| 1154 | continue; |
| 1155 | } |
| 1156 | |
Daniel Jasper | 38c8240 | 2013-11-29 09:27:43 +0000 | [diff] [blame] | 1157 | if (nonPPLineAffected(Line, PreviousLine)) |
Daniel Jasper | 5500f61 | 2013-11-25 11:08:59 +0000 | [diff] [blame] | 1158 | SomeLineAffected = true; |
Daniel Jasper | 5500f61 | 2013-11-25 11:08:59 +0000 | [diff] [blame] | 1159 | |
Daniel Jasper | 38c8240 | 2013-11-29 09:27:43 +0000 | [diff] [blame] | 1160 | PreviousLine = Line; |
Daniel Jasper | 5500f61 | 2013-11-25 11:08:59 +0000 | [diff] [blame] | 1161 | ++I; |
| 1162 | } |
| 1163 | return SomeLineAffected; |
| 1164 | } |
| 1165 | |
Daniel Jasper | 9c19956 | 2013-11-28 15:58:55 +0000 | [diff] [blame] | 1166 | // Determines whether 'Line' is affected by the SourceRanges given as input. |
| 1167 | // Returns \c true if line or one if its children is affected. |
Daniel Jasper | 38c8240 | 2013-11-29 09:27:43 +0000 | [diff] [blame] | 1168 | bool nonPPLineAffected(AnnotatedLine *Line, |
| 1169 | const AnnotatedLine *PreviousLine) { |
Daniel Jasper | 9c19956 | 2013-11-28 15:58:55 +0000 | [diff] [blame] | 1170 | bool SomeLineAffected = false; |
| 1171 | Line->ChildrenAffected = |
| 1172 | computeAffectedLines(Line->Children.begin(), Line->Children.end()); |
| 1173 | if (Line->ChildrenAffected) |
| 1174 | SomeLineAffected = true; |
| 1175 | |
| 1176 | // Stores whether one of the line's tokens is directly affected. |
| 1177 | bool SomeTokenAffected = false; |
| 1178 | // Stores whether we need to look at the leading newlines of the next token |
| 1179 | // in order to determine whether it was affected. |
| 1180 | bool IncludeLeadingNewlines = false; |
| 1181 | |
| 1182 | // Stores whether the first child line of any of this line's tokens is |
| 1183 | // affected. |
| 1184 | bool SomeFirstChildAffected = false; |
| 1185 | |
| 1186 | for (FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) { |
| 1187 | // Determine whether 'Tok' was affected. |
| 1188 | if (affectsTokenRange(*Tok, *Tok, IncludeLeadingNewlines)) |
| 1189 | SomeTokenAffected = true; |
| 1190 | |
| 1191 | // Determine whether the first child of 'Tok' was affected. |
| 1192 | if (!Tok->Children.empty() && Tok->Children.front()->Affected) |
| 1193 | SomeFirstChildAffected = true; |
| 1194 | |
| 1195 | IncludeLeadingNewlines = Tok->Children.empty(); |
| 1196 | } |
| 1197 | |
| 1198 | // Was this line moved, i.e. has it previously been on the same line as an |
| 1199 | // affected line? |
Daniel Jasper | 38c8240 | 2013-11-29 09:27:43 +0000 | [diff] [blame] | 1200 | bool LineMoved = PreviousLine && PreviousLine->Affected && |
| 1201 | Line->First->NewlinesBefore == 0; |
Daniel Jasper | 9c19956 | 2013-11-28 15:58:55 +0000 | [diff] [blame] | 1202 | |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 1203 | bool IsContinuedComment = |
| 1204 | Line->First->is(tok::comment) && Line->First->Next == nullptr && |
| 1205 | Line->First->NewlinesBefore < 2 && PreviousLine && |
| 1206 | PreviousLine->Affected && PreviousLine->Last->is(tok::comment); |
Daniel Jasper | 38c8240 | 2013-11-29 09:27:43 +0000 | [diff] [blame] | 1207 | |
| 1208 | if (SomeTokenAffected || SomeFirstChildAffected || LineMoved || |
| 1209 | IsContinuedComment) { |
Daniel Jasper | 9c19956 | 2013-11-28 15:58:55 +0000 | [diff] [blame] | 1210 | Line->Affected = true; |
Daniel Jasper | 9c19956 | 2013-11-28 15:58:55 +0000 | [diff] [blame] | 1211 | SomeLineAffected = true; |
Daniel Jasper | 9c19956 | 2013-11-28 15:58:55 +0000 | [diff] [blame] | 1212 | } |
| 1213 | return SomeLineAffected; |
| 1214 | } |
| 1215 | |
Daniel Jasper | 5500f61 | 2013-11-25 11:08:59 +0000 | [diff] [blame] | 1216 | // Marks all lines between I and E as well as all their children as affected. |
| 1217 | void markAllAsAffected(SmallVectorImpl<AnnotatedLine *>::iterator I, |
| 1218 | SmallVectorImpl<AnnotatedLine *>::iterator E) { |
| 1219 | while (I != E) { |
| 1220 | (*I)->Affected = true; |
| 1221 | markAllAsAffected((*I)->Children.begin(), (*I)->Children.end()); |
| 1222 | ++I; |
| 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | // Returns true if the range from 'First' to 'Last' intersects with one of the |
| 1227 | // input ranges. |
| 1228 | bool affectsTokenRange(const FormatToken &First, const FormatToken &Last, |
| 1229 | bool IncludeLeadingNewlines) { |
| 1230 | SourceLocation Start = First.WhitespaceRange.getBegin(); |
| 1231 | if (!IncludeLeadingNewlines) |
| 1232 | Start = Start.getLocWithOffset(First.LastNewlineOffset); |
Daniel Jasper | 5877bf1 | 2013-11-25 11:53:05 +0000 | [diff] [blame] | 1233 | SourceLocation End = Last.getStartOfNonWhitespace(); |
Daniel Jasper | ac29eac | 2014-10-29 23:40:50 +0000 | [diff] [blame] | 1234 | End = End.getLocWithOffset(Last.TokenText.size()); |
Daniel Jasper | 5500f61 | 2013-11-25 11:08:59 +0000 | [diff] [blame] | 1235 | CharSourceRange Range = CharSourceRange::getCharRange(Start, End); |
| 1236 | return affectsCharSourceRange(Range); |
| 1237 | } |
| 1238 | |
| 1239 | // Returns true if one of the input ranges intersect the leading empty lines |
| 1240 | // before 'Tok'. |
| 1241 | bool affectsLeadingEmptyLines(const FormatToken &Tok) { |
| 1242 | CharSourceRange EmptyLineRange = CharSourceRange::getCharRange( |
| 1243 | Tok.WhitespaceRange.getBegin(), |
| 1244 | Tok.WhitespaceRange.getBegin().getLocWithOffset(Tok.LastNewlineOffset)); |
| 1245 | return affectsCharSourceRange(EmptyLineRange); |
| 1246 | } |
| 1247 | |
| 1248 | // Returns true if 'Range' intersects with one of the input ranges. |
| 1249 | bool affectsCharSourceRange(const CharSourceRange &Range) { |
| 1250 | for (SmallVectorImpl<CharSourceRange>::const_iterator I = Ranges.begin(), |
| 1251 | E = Ranges.end(); |
| 1252 | I != E; ++I) { |
| 1253 | if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), I->getBegin()) && |
| 1254 | !SourceMgr.isBeforeInTranslationUnit(I->getEnd(), Range.getBegin())) |
| 1255 | return true; |
| 1256 | } |
| 1257 | return false; |
| 1258 | } |
| 1259 | |
Alexander Kornienko | 9e649af | 2013-09-11 12:25:57 +0000 | [diff] [blame] | 1260 | static bool inputUsesCRLF(StringRef Text) { |
| 1261 | return Text.count('\r') * 2 > Text.count('\n'); |
| 1262 | } |
| 1263 | |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 1264 | void |
| 1265 | deriveLocalStyle(const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) { |
Daniel Jasper | 7fce3ab | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1266 | unsigned CountBoundToVariable = 0; |
| 1267 | unsigned CountBoundToType = 0; |
| 1268 | bool HasCpp03IncompatibleFormat = false; |
Daniel Jasper | b10cbc4 | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 1269 | bool HasBinPackedFunction = false; |
| 1270 | bool HasOnePerLineFunction = false; |
Daniel Jasper | 7fce3ab | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1271 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1272 | if (!AnnotatedLines[i]->First->Next) |
Daniel Jasper | 7fce3ab | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1273 | continue; |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1274 | FormatToken *Tok = AnnotatedLines[i]->First->Next; |
Manuel Klimek | 6e6310e | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1275 | while (Tok->Next) { |
Daniel Jasper | a98b7b0 | 2014-11-25 10:05:17 +0000 | [diff] [blame] | 1276 | if (Tok->is(TT_PointerOrReference)) { |
Manuel Klimek | 6e6310e | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1277 | bool SpacesBefore = |
| 1278 | Tok->WhitespaceRange.getBegin() != Tok->WhitespaceRange.getEnd(); |
| 1279 | bool SpacesAfter = Tok->Next->WhitespaceRange.getBegin() != |
| 1280 | Tok->Next->WhitespaceRange.getEnd(); |
Daniel Jasper | 7fce3ab | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1281 | if (SpacesBefore && !SpacesAfter) |
| 1282 | ++CountBoundToVariable; |
| 1283 | else if (!SpacesBefore && SpacesAfter) |
| 1284 | ++CountBoundToType; |
| 1285 | } |
| 1286 | |
Daniel Jasper | fba84ff | 2013-10-12 05:16:06 +0000 | [diff] [blame] | 1287 | if (Tok->WhitespaceRange.getBegin() == Tok->WhitespaceRange.getEnd()) { |
Daniel Jasper | a98b7b0 | 2014-11-25 10:05:17 +0000 | [diff] [blame] | 1288 | if (Tok->is(tok::coloncolon) && Tok->Previous->is(TT_TemplateOpener)) |
Daniel Jasper | fba84ff | 2013-10-12 05:16:06 +0000 | [diff] [blame] | 1289 | HasCpp03IncompatibleFormat = true; |
Daniel Jasper | a98b7b0 | 2014-11-25 10:05:17 +0000 | [diff] [blame] | 1290 | if (Tok->is(TT_TemplateCloser) && |
| 1291 | Tok->Previous->is(TT_TemplateCloser)) |
Daniel Jasper | fba84ff | 2013-10-12 05:16:06 +0000 | [diff] [blame] | 1292 | HasCpp03IncompatibleFormat = true; |
| 1293 | } |
Daniel Jasper | b10cbc4 | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 1294 | |
| 1295 | if (Tok->PackingKind == PPK_BinPacked) |
| 1296 | HasBinPackedFunction = true; |
| 1297 | if (Tok->PackingKind == PPK_OnePerLine) |
| 1298 | HasOnePerLineFunction = true; |
| 1299 | |
Manuel Klimek | 6e6310e | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1300 | Tok = Tok->Next; |
Daniel Jasper | 7fce3ab | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1301 | } |
| 1302 | } |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 1303 | if (Style.DerivePointerAlignment) { |
Daniel Jasper | 7fce3ab | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1304 | if (CountBoundToType > CountBoundToVariable) |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 1305 | Style.PointerAlignment = FormatStyle::PAS_Left; |
Daniel Jasper | 7fce3ab | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1306 | else if (CountBoundToType < CountBoundToVariable) |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 1307 | Style.PointerAlignment = FormatStyle::PAS_Right; |
Daniel Jasper | 7fce3ab | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1308 | } |
| 1309 | if (Style.Standard == FormatStyle::LS_Auto) { |
| 1310 | Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11 |
| 1311 | : FormatStyle::LS_Cpp03; |
| 1312 | } |
Daniel Jasper | b10cbc4 | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 1313 | BinPackInconclusiveFunctions = |
| 1314 | HasBinPackedFunction || !HasOnePerLineFunction; |
Daniel Jasper | 7fce3ab | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 1317 | void consumeUnwrappedLine(const UnwrappedLine &TheLine) override { |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 1318 | assert(!UnwrappedLines.empty()); |
| 1319 | UnwrappedLines.back().push_back(TheLine); |
| 1320 | } |
| 1321 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 1322 | void finishRun() override { |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 1323 | UnwrappedLines.push_back(SmallVector<UnwrappedLine, 16>()); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | FormatStyle Style; |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 1327 | FileID ID; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1328 | SourceManager &SourceMgr; |
Daniel Jasper | aa701fa | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 1329 | WhitespaceManager Whitespaces; |
Daniel Jasper | bbf5c1c | 2013-11-05 19:10:03 +0000 | [diff] [blame] | 1330 | SmallVector<CharSourceRange, 8> Ranges; |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 1331 | SmallVector<SmallVector<UnwrappedLine, 16>, 2> UnwrappedLines; |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1332 | |
| 1333 | encoding::Encoding Encoding; |
Daniel Jasper | b10cbc4 | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 1334 | bool BinPackInconclusiveFunctions; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1335 | }; |
| 1336 | |
Craig Topper | af35e85 | 2013-06-30 22:29:28 +0000 | [diff] [blame] | 1337 | } // end anonymous namespace |
| 1338 | |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 1339 | tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex, |
| 1340 | SourceManager &SourceMgr, |
Benjamin Kramer | d0eed3a | 2014-10-03 18:52:48 +0000 | [diff] [blame] | 1341 | ArrayRef<CharSourceRange> Ranges) { |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 1342 | if (Style.DisableFormat) |
| 1343 | return tooling::Replacements(); |
| 1344 | return reformat(Style, SourceMgr, |
| 1345 | SourceMgr.getFileID(Lex.getSourceLocation()), Ranges); |
| 1346 | } |
Daniel Jasper | c64b09a | 2014-05-22 15:12:22 +0000 | [diff] [blame] | 1347 | |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 1348 | tooling::Replacements reformat(const FormatStyle &Style, |
| 1349 | SourceManager &SourceMgr, FileID ID, |
Benjamin Kramer | d0eed3a | 2014-10-03 18:52:48 +0000 | [diff] [blame] | 1350 | ArrayRef<CharSourceRange> Ranges) { |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 1351 | if (Style.DisableFormat) |
| 1352 | return tooling::Replacements(); |
| 1353 | Formatter formatter(Style, SourceMgr, ID, Ranges); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1354 | return formatter.format(); |
| 1355 | } |
| 1356 | |
Daniel Jasper | ec04c0d | 2013-05-16 10:40:07 +0000 | [diff] [blame] | 1357 | tooling::Replacements reformat(const FormatStyle &Style, StringRef Code, |
Benjamin Kramer | d0eed3a | 2014-10-03 18:52:48 +0000 | [diff] [blame] | 1358 | ArrayRef<tooling::Range> Ranges, |
Daniel Jasper | ec04c0d | 2013-05-16 10:40:07 +0000 | [diff] [blame] | 1359 | StringRef FileName) { |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 1360 | if (Style.DisableFormat) |
| 1361 | return tooling::Replacements(); |
| 1362 | |
Daniel Jasper | ec04c0d | 2013-05-16 10:40:07 +0000 | [diff] [blame] | 1363 | FileManager Files((FileSystemOptions())); |
| 1364 | DiagnosticsEngine Diagnostics( |
| 1365 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), |
| 1366 | new DiagnosticOptions); |
| 1367 | SourceManager SourceMgr(Diagnostics, Files); |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 1368 | std::unique_ptr<llvm::MemoryBuffer> Buf = |
| 1369 | llvm::MemoryBuffer::getMemBuffer(Code, FileName); |
Daniel Jasper | ec04c0d | 2013-05-16 10:40:07 +0000 | [diff] [blame] | 1370 | const clang::FileEntry *Entry = |
| 1371 | Files.getVirtualFile(FileName, Buf->getBufferSize(), 0); |
David Blaikie | 49cc318 | 2014-08-27 20:54:45 +0000 | [diff] [blame] | 1372 | SourceMgr.overrideFileContents(Entry, std::move(Buf)); |
Daniel Jasper | ec04c0d | 2013-05-16 10:40:07 +0000 | [diff] [blame] | 1373 | FileID ID = |
| 1374 | SourceMgr.createFileID(Entry, SourceLocation(), clang::SrcMgr::C_User); |
Daniel Jasper | ec04c0d | 2013-05-16 10:40:07 +0000 | [diff] [blame] | 1375 | SourceLocation StartOfFile = SourceMgr.getLocForStartOfFile(ID); |
| 1376 | std::vector<CharSourceRange> CharRanges; |
Benjamin Kramer | d0eed3a | 2014-10-03 18:52:48 +0000 | [diff] [blame] | 1377 | for (const tooling::Range &Range : Ranges) { |
| 1378 | SourceLocation Start = StartOfFile.getLocWithOffset(Range.getOffset()); |
| 1379 | SourceLocation End = Start.getLocWithOffset(Range.getLength()); |
Daniel Jasper | ec04c0d | 2013-05-16 10:40:07 +0000 | [diff] [blame] | 1380 | CharRanges.push_back(CharSourceRange::getCharRange(Start, End)); |
| 1381 | } |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 1382 | return reformat(Style, SourceMgr, ID, CharRanges); |
Daniel Jasper | ec04c0d | 2013-05-16 10:40:07 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
Daniel Jasper | 4db69bd | 2014-09-04 18:23:42 +0000 | [diff] [blame] | 1385 | LangOptions getFormattingLangOpts(const FormatStyle &Style) { |
Daniel Jasper | c1fa281 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1386 | LangOptions LangOpts; |
| 1387 | LangOpts.CPlusPlus = 1; |
Daniel Jasper | 4db69bd | 2014-09-04 18:23:42 +0000 | [diff] [blame] | 1388 | LangOpts.CPlusPlus11 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1; |
| 1389 | LangOpts.CPlusPlus14 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1; |
Daniel Jasper | 5521365 | 2013-03-22 10:01:29 +0000 | [diff] [blame] | 1390 | LangOpts.LineComment = 1; |
Daniel Jasper | 30a2406 | 2014-11-14 09:02:28 +0000 | [diff] [blame] | 1391 | bool AlternativeOperators = Style.Language != FormatStyle::LK_JavaScript && |
| 1392 | Style.Language != FormatStyle::LK_Java; |
| 1393 | LangOpts.CXXOperatorNames = AlternativeOperators ? 1 : 0; |
Daniel Jasper | c1fa281 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1394 | LangOpts.Bool = 1; |
| 1395 | LangOpts.ObjC1 = 1; |
| 1396 | LangOpts.ObjC2 = 1; |
| 1397 | return LangOpts; |
| 1398 | } |
| 1399 | |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 1400 | const char *StyleOptionHelpDescription = |
| 1401 | "Coding style, currently supports:\n" |
| 1402 | " LLVM, Google, Chromium, Mozilla, WebKit.\n" |
| 1403 | "Use -style=file to load style configuration from\n" |
| 1404 | ".clang-format file located in one of the parent\n" |
| 1405 | "directories of the source file (or current\n" |
| 1406 | "directory for stdin).\n" |
| 1407 | "Use -style=\"{key: value, ...}\" to set specific\n" |
| 1408 | "parameters, e.g.:\n" |
| 1409 | " -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\""; |
| 1410 | |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 1411 | static FormatStyle::LanguageKind getLanguageByFileName(StringRef FileName) { |
Daniel Jasper | c58c70e | 2014-09-15 11:21:46 +0000 | [diff] [blame] | 1412 | if (FileName.endswith(".java")) { |
| 1413 | return FormatStyle::LK_Java; |
| 1414 | } else if (FileName.endswith_lower(".js")) { |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 1415 | return FormatStyle::LK_JavaScript; |
Daniel Jasper | 7052ce6 | 2014-01-19 09:04:08 +0000 | [diff] [blame] | 1416 | } else if (FileName.endswith_lower(".proto") || |
| 1417 | FileName.endswith_lower(".protodevel")) { |
| 1418 | return FormatStyle::LK_Proto; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 1419 | } |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 1420 | return FormatStyle::LK_Cpp; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
Alexander Kornienko | bc4ae44 | 2013-12-02 15:21:38 +0000 | [diff] [blame] | 1423 | FormatStyle getStyle(StringRef StyleName, StringRef FileName, |
| 1424 | StringRef FallbackStyle) { |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 1425 | FormatStyle Style = getLLVMStyle(); |
| 1426 | Style.Language = getLanguageByFileName(FileName); |
| 1427 | if (!getPredefinedStyle(FallbackStyle, Style.Language, &Style)) { |
Alexander Kornienko | bc4ae44 | 2013-12-02 15:21:38 +0000 | [diff] [blame] | 1428 | llvm::errs() << "Invalid fallback style \"" << FallbackStyle |
| 1429 | << "\" using LLVM style\n"; |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 1430 | return Style; |
Alexander Kornienko | bc4ae44 | 2013-12-02 15:21:38 +0000 | [diff] [blame] | 1431 | } |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 1432 | |
| 1433 | if (StyleName.startswith("{")) { |
| 1434 | // Parse YAML/JSON style from the command line. |
Rafael Espindola | c080917 | 2014-06-12 14:02:15 +0000 | [diff] [blame] | 1435 | if (std::error_code ec = parseConfiguration(StyleName, &Style)) { |
Alexander Kornienko | e2e0387 | 2013-10-14 00:46:35 +0000 | [diff] [blame] | 1436 | llvm::errs() << "Error parsing -style: " << ec.message() << ", using " |
| 1437 | << FallbackStyle << " style\n"; |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 1438 | } |
| 1439 | return Style; |
| 1440 | } |
| 1441 | |
| 1442 | if (!StyleName.equals_lower("file")) { |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 1443 | if (!getPredefinedStyle(StyleName, Style.Language, &Style)) |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 1444 | llvm::errs() << "Invalid value for -style, using " << FallbackStyle |
| 1445 | << " style\n"; |
| 1446 | return Style; |
| 1447 | } |
| 1448 | |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 1449 | // 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] | 1450 | SmallString<128> UnsuitableConfigFiles; |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 1451 | SmallString<128> Path(FileName); |
| 1452 | llvm::sys::fs::make_absolute(Path); |
Alexander Kornienko | e2e0387 | 2013-10-14 00:46:35 +0000 | [diff] [blame] | 1453 | for (StringRef Directory = Path; !Directory.empty(); |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 1454 | Directory = llvm::sys::path::parent_path(Directory)) { |
| 1455 | if (!llvm::sys::fs::is_directory(Directory)) |
| 1456 | continue; |
| 1457 | SmallString<128> ConfigFile(Directory); |
| 1458 | |
| 1459 | llvm::sys::path::append(ConfigFile, ".clang-format"); |
| 1460 | DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n"); |
| 1461 | bool IsFile = false; |
| 1462 | // Ignore errors from is_regular_file: we only need to know if we can read |
| 1463 | // the file or not. |
| 1464 | llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile); |
| 1465 | |
| 1466 | if (!IsFile) { |
| 1467 | // Try _clang-format too, since dotfiles are not commonly used on Windows. |
| 1468 | ConfigFile = Directory; |
| 1469 | llvm::sys::path::append(ConfigFile, "_clang-format"); |
| 1470 | DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n"); |
| 1471 | llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile); |
| 1472 | } |
| 1473 | |
| 1474 | if (IsFile) { |
Rafael Espindola | 2d2b420 | 2014-07-06 17:43:24 +0000 | [diff] [blame] | 1475 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text = |
| 1476 | llvm::MemoryBuffer::getFile(ConfigFile.c_str()); |
| 1477 | if (std::error_code EC = Text.getError()) { |
| 1478 | llvm::errs() << EC.message() << "\n"; |
Alexander Kornienko | bc4ae44 | 2013-12-02 15:21:38 +0000 | [diff] [blame] | 1479 | break; |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 1480 | } |
Rafael Espindola | 2d2b420 | 2014-07-06 17:43:24 +0000 | [diff] [blame] | 1481 | if (std::error_code ec = |
| 1482 | parseConfiguration(Text.get()->getBuffer(), &Style)) { |
Rafael Espindola | d013670 | 2014-06-12 02:50:04 +0000 | [diff] [blame] | 1483 | if (ec == ParseError::Unsuitable) { |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 1484 | if (!UnsuitableConfigFiles.empty()) |
| 1485 | UnsuitableConfigFiles.append(", "); |
| 1486 | UnsuitableConfigFiles.append(ConfigFile); |
Alexander Kornienko | bc4ae44 | 2013-12-02 15:21:38 +0000 | [diff] [blame] | 1487 | continue; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 1488 | } |
Alexander Kornienko | bc4ae44 | 2013-12-02 15:21:38 +0000 | [diff] [blame] | 1489 | llvm::errs() << "Error reading " << ConfigFile << ": " << ec.message() |
| 1490 | << "\n"; |
| 1491 | break; |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 1492 | } |
| 1493 | DEBUG(llvm::dbgs() << "Using configuration file " << ConfigFile << "\n"); |
| 1494 | return Style; |
| 1495 | } |
| 1496 | } |
| 1497 | llvm::errs() << "Can't find usable .clang-format, using " << FallbackStyle |
| 1498 | << " style\n"; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 1499 | if (!UnsuitableConfigFiles.empty()) { |
| 1500 | llvm::errs() << "Configuration file(s) do(es) not support " |
| 1501 | << getLanguageName(Style.Language) << ": " |
| 1502 | << UnsuitableConfigFiles << "\n"; |
| 1503 | } |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 1504 | return Style; |
| 1505 | } |
| 1506 | |
Daniel Jasper | 8d1832e | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1507 | } // namespace format |
| 1508 | } // namespace clang |