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