Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1 | //===--- Format.cpp - Format C++ code -------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// \brief This file implements functions declared in Format.h. This will be |
| 12 | /// split into separate files as we go. |
| 13 | /// |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Daniel Jasper | 85c472d | 2015-09-29 07:53:08 +0000 | [diff] [blame] | 16 | #include "clang/Format/Format.h" |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 17 | #include "AffectedRangeManager.h" |
Daniel Jasper | de0328a | 2013-08-16 11:20:30 +0000 | [diff] [blame] | 18 | #include "ContinuationIndenter.h" |
Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 19 | #include "FormatTokenLexer.h" |
Krasimir Georgiev | 7cb267a | 2017-02-27 13:28:36 +0000 | [diff] [blame] | 20 | #include "NamespaceEndCommentsFixer.h" |
Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 21 | #include "SortJavaScriptImports.h" |
| 22 | #include "TokenAnalyzer.h" |
Daniel Jasper | 7a6d09b | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 23 | #include "TokenAnnotator.h" |
Daniel Jasper | 0df5093 | 2014-12-10 19:00:42 +0000 | [diff] [blame] | 24 | #include "UnwrappedLineFormatter.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 25 | #include "UnwrappedLineParser.h" |
Krasimir Georgiev | b03877a | 2017-06-21 12:03:12 +0000 | [diff] [blame] | 26 | #include "UsingDeclarationsSorter.h" |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 27 | #include "WhitespaceManager.h" |
Daniel Jasper | ec04c0d | 2013-05-16 10:40:07 +0000 | [diff] [blame] | 28 | #include "clang/Basic/Diagnostic.h" |
Benjamin Kramer | f3ca2698 | 2014-05-10 16:31:55 +0000 | [diff] [blame] | 29 | #include "clang/Basic/DiagnosticOptions.h" |
Chandler Carruth | 44eb4f6 | 2013-01-02 10:28:36 +0000 | [diff] [blame] | 30 | #include "clang/Basic/SourceManager.h" |
Marianne Mailhot-Sarrasin | 4988fa1 | 2016-04-14 14:47:37 +0000 | [diff] [blame] | 31 | #include "clang/Basic/VirtualFileSystem.h" |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 32 | #include "clang/Lex/Lexer.h" |
Alexander Kornienko | ffd6d04 | 2013-03-27 11:52:18 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/STLExtras.h" |
Manuel Klimek | 2ef908e | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Allocator.h" |
Manuel Klimek | 2499810 | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Debug.h" |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Path.h" |
Birunthan Mohanathas | b001a0b | 2015-07-03 17:25:16 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Regex.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 38 | #include "llvm/Support/YAMLTraits.h" |
Martin Probst | 081f176 | 2016-06-01 15:19:53 +0000 | [diff] [blame] | 39 | #include <algorithm> |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 40 | #include <memory> |
Daniel Jasper | 8b52971 | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 41 | #include <string> |
| 42 | |
Chandler Carruth | 1034666 | 2014-04-22 03:17:02 +0000 | [diff] [blame] | 43 | #define DEBUG_TYPE "format-formatter" |
| 44 | |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 45 | using clang::format::FormatStyle; |
| 46 | |
Daniel Jasper | 8ce1b8d | 2015-10-06 11:54:18 +0000 | [diff] [blame] | 47 | LLVM_YAML_IS_SEQUENCE_VECTOR(clang::format::FormatStyle::IncludeCategory) |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 48 | |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 49 | namespace llvm { |
| 50 | namespace yaml { |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 51 | template <> struct ScalarEnumerationTraits<FormatStyle::LanguageKind> { |
| 52 | static void enumeration(IO &IO, FormatStyle::LanguageKind &Value) { |
| 53 | IO.enumCase(Value, "Cpp", FormatStyle::LK_Cpp); |
Daniel Jasper | c58c70e | 2014-09-15 11:21:46 +0000 | [diff] [blame] | 54 | IO.enumCase(Value, "Java", FormatStyle::LK_Java); |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 55 | IO.enumCase(Value, "JavaScript", FormatStyle::LK_JavaScript); |
Daniel Jasper | 03a04fe | 2016-12-12 12:42:29 +0000 | [diff] [blame] | 56 | IO.enumCase(Value, "ObjC", FormatStyle::LK_ObjC); |
Daniel Jasper | 7052ce6 | 2014-01-19 09:04:08 +0000 | [diff] [blame] | 57 | IO.enumCase(Value, "Proto", FormatStyle::LK_Proto); |
Daniel Jasper | 498f558 | 2015-12-25 08:53:31 +0000 | [diff] [blame] | 58 | IO.enumCase(Value, "TableGen", FormatStyle::LK_TableGen); |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame^] | 59 | IO.enumCase(Value, "TextProto", FormatStyle::LK_TextProto); |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 60 | } |
| 61 | }; |
| 62 | |
| 63 | template <> struct ScalarEnumerationTraits<FormatStyle::LanguageStandard> { |
| 64 | static void enumeration(IO &IO, FormatStyle::LanguageStandard &Value) { |
| 65 | IO.enumCase(Value, "Cpp03", FormatStyle::LS_Cpp03); |
| 66 | IO.enumCase(Value, "C++03", FormatStyle::LS_Cpp03); |
| 67 | IO.enumCase(Value, "Cpp11", FormatStyle::LS_Cpp11); |
| 68 | IO.enumCase(Value, "C++11", FormatStyle::LS_Cpp11); |
| 69 | IO.enumCase(Value, "Auto", FormatStyle::LS_Auto); |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | template <> struct ScalarEnumerationTraits<FormatStyle::UseTabStyle> { |
| 74 | static void enumeration(IO &IO, FormatStyle::UseTabStyle &Value) { |
| 75 | IO.enumCase(Value, "Never", FormatStyle::UT_Never); |
| 76 | IO.enumCase(Value, "false", FormatStyle::UT_Never); |
| 77 | IO.enumCase(Value, "Always", FormatStyle::UT_Always); |
| 78 | IO.enumCase(Value, "true", FormatStyle::UT_Always); |
| 79 | IO.enumCase(Value, "ForIndentation", FormatStyle::UT_ForIndentation); |
Marianne Mailhot-Sarrasin | 51fe279 | 2016-04-14 14:52:26 +0000 | [diff] [blame] | 80 | IO.enumCase(Value, "ForContinuationAndIndentation", |
| 81 | FormatStyle::UT_ForContinuationAndIndentation); |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 82 | } |
| 83 | }; |
| 84 | |
Daniel Jasper | abd1f57 | 2016-03-02 22:44:03 +0000 | [diff] [blame] | 85 | template <> struct ScalarEnumerationTraits<FormatStyle::JavaScriptQuoteStyle> { |
| 86 | static void enumeration(IO &IO, FormatStyle::JavaScriptQuoteStyle &Value) { |
| 87 | IO.enumCase(Value, "Leave", FormatStyle::JSQS_Leave); |
| 88 | IO.enumCase(Value, "Single", FormatStyle::JSQS_Single); |
| 89 | IO.enumCase(Value, "Double", FormatStyle::JSQS_Double); |
| 90 | } |
| 91 | }; |
| 92 | |
Daniel Jasper | d74cf40 | 2014-04-08 12:46:38 +0000 | [diff] [blame] | 93 | template <> struct ScalarEnumerationTraits<FormatStyle::ShortFunctionStyle> { |
| 94 | static void enumeration(IO &IO, FormatStyle::ShortFunctionStyle &Value) { |
| 95 | IO.enumCase(Value, "None", FormatStyle::SFS_None); |
| 96 | IO.enumCase(Value, "false", FormatStyle::SFS_None); |
| 97 | IO.enumCase(Value, "All", FormatStyle::SFS_All); |
| 98 | IO.enumCase(Value, "true", FormatStyle::SFS_All); |
| 99 | IO.enumCase(Value, "Inline", FormatStyle::SFS_Inline); |
Francois Ferrand | d3f0e3d | 2017-06-21 13:56:02 +0000 | [diff] [blame] | 100 | IO.enumCase(Value, "InlineOnly", FormatStyle::SFS_InlineOnly); |
Daniel Jasper | 9e70935 | 2014-11-26 10:43:58 +0000 | [diff] [blame] | 101 | IO.enumCase(Value, "Empty", FormatStyle::SFS_Empty); |
Daniel Jasper | d74cf40 | 2014-04-08 12:46:38 +0000 | [diff] [blame] | 102 | } |
| 103 | }; |
| 104 | |
Daniel Jasper | ac043c9 | 2014-09-15 11:11:00 +0000 | [diff] [blame] | 105 | template <> struct ScalarEnumerationTraits<FormatStyle::BinaryOperatorStyle> { |
| 106 | static void enumeration(IO &IO, FormatStyle::BinaryOperatorStyle &Value) { |
| 107 | IO.enumCase(Value, "All", FormatStyle::BOS_All); |
| 108 | IO.enumCase(Value, "true", FormatStyle::BOS_All); |
| 109 | IO.enumCase(Value, "None", FormatStyle::BOS_None); |
| 110 | IO.enumCase(Value, "false", FormatStyle::BOS_None); |
| 111 | IO.enumCase(Value, "NonAssignment", FormatStyle::BOS_NonAssignment); |
| 112 | } |
| 113 | }; |
| 114 | |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 115 | template <> struct ScalarEnumerationTraits<FormatStyle::BraceBreakingStyle> { |
| 116 | static void enumeration(IO &IO, FormatStyle::BraceBreakingStyle &Value) { |
| 117 | IO.enumCase(Value, "Attach", FormatStyle::BS_Attach); |
| 118 | IO.enumCase(Value, "Linux", FormatStyle::BS_Linux); |
Birunthan Mohanathas | 305fa9c | 2015-07-12 03:13:54 +0000 | [diff] [blame] | 119 | IO.enumCase(Value, "Mozilla", FormatStyle::BS_Mozilla); |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 120 | IO.enumCase(Value, "Stroustrup", FormatStyle::BS_Stroustrup); |
| 121 | IO.enumCase(Value, "Allman", FormatStyle::BS_Allman); |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 122 | IO.enumCase(Value, "GNU", FormatStyle::BS_GNU); |
Roman Kashitsyn | 291f64f | 2015-08-10 13:43:19 +0000 | [diff] [blame] | 123 | IO.enumCase(Value, "WebKit", FormatStyle::BS_WebKit); |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 124 | IO.enumCase(Value, "Custom", FormatStyle::BS_Custom); |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 125 | } |
| 126 | }; |
| 127 | |
Francois Ferrand | a6b6d51 | 2017-05-24 11:36:58 +0000 | [diff] [blame] | 128 | template <> struct ScalarEnumerationTraits<FormatStyle::BreakConstructorInitializersStyle> { |
| 129 | static void enumeration(IO &IO, FormatStyle::BreakConstructorInitializersStyle &Value) { |
| 130 | IO.enumCase(Value, "BeforeColon", FormatStyle::BCIS_BeforeColon); |
| 131 | IO.enumCase(Value, "BeforeComma", FormatStyle::BCIS_BeforeComma); |
| 132 | IO.enumCase(Value, "AfterColon", FormatStyle::BCIS_AfterColon); |
| 133 | } |
| 134 | }; |
| 135 | |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 136 | template <> |
Zachary Turner | 448592e | 2015-12-18 22:20:15 +0000 | [diff] [blame] | 137 | struct ScalarEnumerationTraits<FormatStyle::ReturnTypeBreakingStyle> { |
| 138 | static void enumeration(IO &IO, FormatStyle::ReturnTypeBreakingStyle &Value) { |
| 139 | IO.enumCase(Value, "None", FormatStyle::RTBS_None); |
| 140 | IO.enumCase(Value, "All", FormatStyle::RTBS_All); |
| 141 | IO.enumCase(Value, "TopLevel", FormatStyle::RTBS_TopLevel); |
| 142 | IO.enumCase(Value, "TopLevelDefinitions", |
| 143 | FormatStyle::RTBS_TopLevelDefinitions); |
| 144 | IO.enumCase(Value, "AllDefinitions", FormatStyle::RTBS_AllDefinitions); |
| 145 | } |
| 146 | }; |
| 147 | |
| 148 | template <> |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 149 | struct ScalarEnumerationTraits<FormatStyle::DefinitionReturnTypeBreakingStyle> { |
| 150 | static void |
| 151 | enumeration(IO &IO, FormatStyle::DefinitionReturnTypeBreakingStyle &Value) { |
Birunthan Mohanathas | a0388a8 | 2015-06-29 15:30:42 +0000 | [diff] [blame] | 152 | IO.enumCase(Value, "None", FormatStyle::DRTBS_None); |
| 153 | IO.enumCase(Value, "All", FormatStyle::DRTBS_All); |
| 154 | IO.enumCase(Value, "TopLevel", FormatStyle::DRTBS_TopLevel); |
| 155 | |
| 156 | // For backward compatibility. |
| 157 | IO.enumCase(Value, "false", FormatStyle::DRTBS_None); |
| 158 | IO.enumCase(Value, "true", FormatStyle::DRTBS_All); |
| 159 | } |
| 160 | }; |
| 161 | |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 162 | template <> |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 163 | struct ScalarEnumerationTraits<FormatStyle::NamespaceIndentationKind> { |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 164 | static void enumeration(IO &IO, |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 165 | FormatStyle::NamespaceIndentationKind &Value) { |
| 166 | IO.enumCase(Value, "None", FormatStyle::NI_None); |
| 167 | IO.enumCase(Value, "Inner", FormatStyle::NI_Inner); |
| 168 | IO.enumCase(Value, "All", FormatStyle::NI_All); |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 169 | } |
| 170 | }; |
| 171 | |
Daniel Jasper | 6501f7e | 2015-10-27 12:38:37 +0000 | [diff] [blame] | 172 | template <> struct ScalarEnumerationTraits<FormatStyle::BracketAlignmentStyle> { |
| 173 | static void enumeration(IO &IO, FormatStyle::BracketAlignmentStyle &Value) { |
| 174 | IO.enumCase(Value, "Align", FormatStyle::BAS_Align); |
| 175 | IO.enumCase(Value, "DontAlign", FormatStyle::BAS_DontAlign); |
| 176 | IO.enumCase(Value, "AlwaysBreak", FormatStyle::BAS_AlwaysBreak); |
| 177 | |
| 178 | // For backward compatibility. |
| 179 | IO.enumCase(Value, "true", FormatStyle::BAS_Align); |
| 180 | IO.enumCase(Value, "false", FormatStyle::BAS_DontAlign); |
| 181 | } |
| 182 | }; |
| 183 | |
Daniel Jasper | 7fdbb3f | 2017-05-08 15:08:00 +0000 | [diff] [blame] | 184 | template <> struct ScalarEnumerationTraits<FormatStyle::EscapedNewlineAlignmentStyle> { |
| 185 | static void enumeration(IO &IO, FormatStyle::EscapedNewlineAlignmentStyle &Value) { |
| 186 | IO.enumCase(Value, "DontAlign", FormatStyle::ENAS_DontAlign); |
| 187 | IO.enumCase(Value, "Left", FormatStyle::ENAS_Left); |
| 188 | IO.enumCase(Value, "Right", FormatStyle::ENAS_Right); |
| 189 | |
| 190 | // For backward compatibility. |
| 191 | IO.enumCase(Value, "true", FormatStyle::ENAS_Left); |
| 192 | IO.enumCase(Value, "false", FormatStyle::ENAS_Right); |
| 193 | } |
| 194 | }; |
| 195 | |
Jacques Pienaar | fc27511 | 2015-02-18 23:48:37 +0000 | [diff] [blame] | 196 | template <> struct ScalarEnumerationTraits<FormatStyle::PointerAlignmentStyle> { |
| 197 | static void enumeration(IO &IO, FormatStyle::PointerAlignmentStyle &Value) { |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 198 | IO.enumCase(Value, "Middle", FormatStyle::PAS_Middle); |
| 199 | IO.enumCase(Value, "Left", FormatStyle::PAS_Left); |
| 200 | IO.enumCase(Value, "Right", FormatStyle::PAS_Right); |
| 201 | |
Alp Toker | 958027b | 2014-07-14 19:42:55 +0000 | [diff] [blame] | 202 | // For backward compatibility. |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 203 | IO.enumCase(Value, "true", FormatStyle::PAS_Left); |
| 204 | IO.enumCase(Value, "false", FormatStyle::PAS_Right); |
| 205 | } |
| 206 | }; |
| 207 | |
| 208 | template <> |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 209 | struct ScalarEnumerationTraits<FormatStyle::SpaceBeforeParensOptions> { |
Manuel Klimek | a8eb914 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 210 | static void enumeration(IO &IO, |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 211 | FormatStyle::SpaceBeforeParensOptions &Value) { |
| 212 | IO.enumCase(Value, "Never", FormatStyle::SBPO_Never); |
Alexander Kornienko | fdca83d | 2013-12-10 10:18:34 +0000 | [diff] [blame] | 213 | IO.enumCase(Value, "ControlStatements", |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 214 | FormatStyle::SBPO_ControlStatements); |
| 215 | IO.enumCase(Value, "Always", FormatStyle::SBPO_Always); |
Alexander Kornienko | fdca83d | 2013-12-10 10:18:34 +0000 | [diff] [blame] | 216 | |
| 217 | // For backward compatibility. |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 218 | IO.enumCase(Value, "false", FormatStyle::SBPO_Never); |
| 219 | IO.enumCase(Value, "true", FormatStyle::SBPO_ControlStatements); |
Alexander Kornienko | fdca83d | 2013-12-10 10:18:34 +0000 | [diff] [blame] | 220 | } |
| 221 | }; |
| 222 | |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 223 | template <> struct MappingTraits<FormatStyle> { |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 224 | static void mapping(IO &IO, FormatStyle &Style) { |
| 225 | // When reading, read the language first, we need it for getPredefinedStyle. |
| 226 | IO.mapOptional("Language", Style.Language); |
| 227 | |
Alexander Kornienko | 4914967 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 228 | if (IO.outputting()) { |
Jacques Pienaar | fc27511 | 2015-02-18 23:48:37 +0000 | [diff] [blame] | 229 | StringRef StylesArray[] = {"LLVM", "Google", "Chromium", |
| 230 | "Mozilla", "WebKit", "GNU"}; |
Alexander Kornienko | 4914967 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 231 | ArrayRef<StringRef> Styles(StylesArray); |
| 232 | for (size_t i = 0, e = Styles.size(); i < e; ++i) { |
| 233 | StringRef StyleName(Styles[i]); |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 234 | FormatStyle PredefinedStyle; |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 235 | if (getPredefinedStyle(StyleName, Style.Language, &PredefinedStyle) && |
Alexander Kornienko | 006b5c8 | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 236 | Style == PredefinedStyle) { |
Alexander Kornienko | 4914967 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 237 | IO.mapOptional("# BasedOnStyle", StyleName); |
| 238 | break; |
| 239 | } |
| 240 | } |
| 241 | } else { |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 242 | StringRef BasedOnStyle; |
| 243 | IO.mapOptional("BasedOnStyle", BasedOnStyle); |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 244 | if (!BasedOnStyle.empty()) { |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 245 | FormatStyle::LanguageKind OldLanguage = Style.Language; |
| 246 | FormatStyle::LanguageKind Language = |
| 247 | ((FormatStyle *)IO.getContext())->Language; |
| 248 | if (!getPredefinedStyle(BasedOnStyle, Language, &Style)) { |
Alexander Kornienko | 006b5c8 | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 249 | IO.setError(Twine("Unknown value for BasedOnStyle: ", BasedOnStyle)); |
| 250 | return; |
| 251 | } |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 252 | Style.Language = OldLanguage; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 253 | } |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Birunthan Mohanathas | 50a6f91 | 2015-06-28 14:52:34 +0000 | [diff] [blame] | 256 | // For backward compatibility. |
| 257 | if (!IO.outputting()) { |
Daniel Jasper | 7fdbb3f | 2017-05-08 15:08:00 +0000 | [diff] [blame] | 258 | IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlines); |
Birunthan Mohanathas | 50a6f91 | 2015-06-28 14:52:34 +0000 | [diff] [blame] | 259 | IO.mapOptional("DerivePointerBinding", Style.DerivePointerAlignment); |
| 260 | IO.mapOptional("IndentFunctionDeclarationAfterType", |
| 261 | Style.IndentWrappedFunctionNames); |
| 262 | IO.mapOptional("PointerBindsToType", Style.PointerAlignment); |
| 263 | IO.mapOptional("SpaceAfterControlStatementKeyword", |
| 264 | Style.SpaceBeforeParens); |
| 265 | } |
| 266 | |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 267 | IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset); |
Daniel Jasper | 3aa9a6a | 2014-11-18 23:55:27 +0000 | [diff] [blame] | 268 | IO.mapOptional("AlignAfterOpenBracket", Style.AlignAfterOpenBracket); |
Birunthan Mohanathas | 35cfbd7 | 2015-06-28 14:51:17 +0000 | [diff] [blame] | 269 | IO.mapOptional("AlignConsecutiveAssignments", |
| 270 | Style.AlignConsecutiveAssignments); |
Daniel Jasper | e12597c | 2015-10-01 10:06:54 +0000 | [diff] [blame] | 271 | IO.mapOptional("AlignConsecutiveDeclarations", |
| 272 | Style.AlignConsecutiveDeclarations); |
Daniel Jasper | 7fdbb3f | 2017-05-08 15:08:00 +0000 | [diff] [blame] | 273 | IO.mapOptional("AlignEscapedNewlines", Style.AlignEscapedNewlines); |
Daniel Jasper | 3219e43 | 2014-12-02 13:24:51 +0000 | [diff] [blame] | 274 | IO.mapOptional("AlignOperands", Style.AlignOperands); |
Daniel Jasper | 552f4a7 | 2013-07-31 23:55:15 +0000 | [diff] [blame] | 275 | IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 276 | IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine", |
| 277 | Style.AllowAllParametersOfDeclarationOnNextLine); |
Daniel Jasper | 17605d3 | 2014-05-14 09:33:35 +0000 | [diff] [blame] | 278 | IO.mapOptional("AllowShortBlocksOnASingleLine", |
| 279 | Style.AllowShortBlocksOnASingleLine); |
Daniel Jasper | b87899b | 2014-09-10 13:11:45 +0000 | [diff] [blame] | 280 | IO.mapOptional("AllowShortCaseLabelsOnASingleLine", |
| 281 | Style.AllowShortCaseLabelsOnASingleLine); |
Birunthan Mohanathas | 35cfbd7 | 2015-06-28 14:51:17 +0000 | [diff] [blame] | 282 | IO.mapOptional("AllowShortFunctionsOnASingleLine", |
| 283 | Style.AllowShortFunctionsOnASingleLine); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 284 | IO.mapOptional("AllowShortIfStatementsOnASingleLine", |
| 285 | Style.AllowShortIfStatementsOnASingleLine); |
Daniel Jasper | 3a685df | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 286 | IO.mapOptional("AllowShortLoopsOnASingleLine", |
| 287 | Style.AllowShortLoopsOnASingleLine); |
Daniel Jasper | ca4ea1c | 2014-08-05 12:16:31 +0000 | [diff] [blame] | 288 | IO.mapOptional("AlwaysBreakAfterDefinitionReturnType", |
| 289 | Style.AlwaysBreakAfterDefinitionReturnType); |
Zachary Turner | 448592e | 2015-12-18 22:20:15 +0000 | [diff] [blame] | 290 | IO.mapOptional("AlwaysBreakAfterReturnType", |
| 291 | Style.AlwaysBreakAfterReturnType); |
| 292 | // If AlwaysBreakAfterDefinitionReturnType was specified but |
| 293 | // AlwaysBreakAfterReturnType was not, initialize the latter from the |
| 294 | // former for backwards compatibility. |
| 295 | if (Style.AlwaysBreakAfterDefinitionReturnType != FormatStyle::DRTBS_None && |
| 296 | Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_None) { |
| 297 | if (Style.AlwaysBreakAfterDefinitionReturnType == FormatStyle::DRTBS_All) |
| 298 | Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; |
| 299 | else if (Style.AlwaysBreakAfterDefinitionReturnType == |
| 300 | FormatStyle::DRTBS_TopLevel) |
| 301 | Style.AlwaysBreakAfterReturnType = |
| 302 | FormatStyle::RTBS_TopLevelDefinitions; |
| 303 | } |
| 304 | |
Alexander Kornienko | 5861171 | 2013-07-04 12:02:44 +0000 | [diff] [blame] | 305 | IO.mapOptional("AlwaysBreakBeforeMultilineStrings", |
| 306 | Style.AlwaysBreakBeforeMultilineStrings); |
Birunthan Mohanathas | 35cfbd7 | 2015-06-28 14:51:17 +0000 | [diff] [blame] | 307 | IO.mapOptional("AlwaysBreakTemplateDeclarations", |
| 308 | Style.AlwaysBreakTemplateDeclarations); |
| 309 | IO.mapOptional("BinPackArguments", Style.BinPackArguments); |
| 310 | IO.mapOptional("BinPackParameters", Style.BinPackParameters); |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 311 | IO.mapOptional("BraceWrapping", Style.BraceWrapping); |
Daniel Jasper | e33d4af | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 312 | IO.mapOptional("BreakBeforeBinaryOperators", |
| 313 | Style.BreakBeforeBinaryOperators); |
Birunthan Mohanathas | 35cfbd7 | 2015-06-28 14:51:17 +0000 | [diff] [blame] | 314 | IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces); |
Francois Ferrand | e56a829 | 2017-06-14 12:29:47 +0000 | [diff] [blame] | 315 | IO.mapOptional("BreakBeforeInheritanceComma", |
| 316 | Style.BreakBeforeInheritanceComma); |
Daniel Jasper | 165b29e | 2013-11-08 00:57:11 +0000 | [diff] [blame] | 317 | IO.mapOptional("BreakBeforeTernaryOperators", |
| 318 | Style.BreakBeforeTernaryOperators); |
Francois Ferrand | a6b6d51 | 2017-05-24 11:36:58 +0000 | [diff] [blame] | 319 | |
| 320 | bool BreakConstructorInitializersBeforeComma = false; |
Daniel Jasper | e33d4af | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 321 | IO.mapOptional("BreakConstructorInitializersBeforeComma", |
Francois Ferrand | a6b6d51 | 2017-05-24 11:36:58 +0000 | [diff] [blame] | 322 | BreakConstructorInitializersBeforeComma); |
| 323 | IO.mapOptional("BreakConstructorInitializers", |
| 324 | Style.BreakConstructorInitializers); |
| 325 | // If BreakConstructorInitializersBeforeComma was specified but |
| 326 | // BreakConstructorInitializers was not, initialize the latter from the |
| 327 | // former for backwards compatibility. |
| 328 | if (BreakConstructorInitializersBeforeComma && |
| 329 | Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon) |
| 330 | Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; |
| 331 | |
Daniel Jasper | e1a7b76 | 2016-02-01 11:21:02 +0000 | [diff] [blame] | 332 | IO.mapOptional("BreakAfterJavaFieldAnnotations", |
| 333 | Style.BreakAfterJavaFieldAnnotations); |
| 334 | IO.mapOptional("BreakStringLiterals", Style.BreakStringLiterals); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 335 | IO.mapOptional("ColumnLimit", Style.ColumnLimit); |
Birunthan Mohanathas | 35cfbd7 | 2015-06-28 14:51:17 +0000 | [diff] [blame] | 336 | IO.mapOptional("CommentPragmas", Style.CommentPragmas); |
Francois Ferrand | e56a829 | 2017-06-14 12:29:47 +0000 | [diff] [blame] | 337 | IO.mapOptional("CompactNamespaces", Style.CompactNamespaces); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 338 | IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine", |
| 339 | Style.ConstructorInitializerAllOnOneLineOrOnePerLine); |
Daniel Jasper | 50d634b | 2014-10-28 16:53:38 +0000 | [diff] [blame] | 340 | IO.mapOptional("ConstructorInitializerIndentWidth", |
| 341 | Style.ConstructorInitializerIndentWidth); |
Birunthan Mohanathas | 35cfbd7 | 2015-06-28 14:51:17 +0000 | [diff] [blame] | 342 | IO.mapOptional("ContinuationIndentWidth", Style.ContinuationIndentWidth); |
| 343 | IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle); |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 344 | IO.mapOptional("DerivePointerAlignment", Style.DerivePointerAlignment); |
Birunthan Mohanathas | 35cfbd7 | 2015-06-28 14:51:17 +0000 | [diff] [blame] | 345 | IO.mapOptional("DisableFormat", Style.DisableFormat); |
Daniel Jasper | b10cbc4 | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 346 | IO.mapOptional("ExperimentalAutoDetectBinPacking", |
| 347 | Style.ExperimentalAutoDetectBinPacking); |
Krasimir Georgiev | 32eaa86 | 2017-03-01 15:35:39 +0000 | [diff] [blame] | 348 | IO.mapOptional("FixNamespaceComments", Style.FixNamespaceComments); |
Birunthan Mohanathas | 35cfbd7 | 2015-06-28 14:51:17 +0000 | [diff] [blame] | 349 | IO.mapOptional("ForEachMacros", Style.ForEachMacros); |
Daniel Jasper | 8ce1b8d | 2015-10-06 11:54:18 +0000 | [diff] [blame] | 350 | IO.mapOptional("IncludeCategories", Style.IncludeCategories); |
Daniel Jasper | 9c8ff35 | 2016-03-21 14:11:27 +0000 | [diff] [blame] | 351 | IO.mapOptional("IncludeIsMainRegex", Style.IncludeIsMainRegex); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 352 | IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels); |
Birunthan Mohanathas | 35cfbd7 | 2015-06-28 14:51:17 +0000 | [diff] [blame] | 353 | IO.mapOptional("IndentWidth", Style.IndentWidth); |
| 354 | IO.mapOptional("IndentWrappedFunctionNames", |
| 355 | Style.IndentWrappedFunctionNames); |
Martin Probst | 0cd74ee | 2016-06-13 16:39:50 +0000 | [diff] [blame] | 356 | IO.mapOptional("JavaScriptQuotes", Style.JavaScriptQuotes); |
| 357 | IO.mapOptional("JavaScriptWrapImports", Style.JavaScriptWrapImports); |
Daniel Jasper | a26fc5c | 2014-03-21 13:43:14 +0000 | [diff] [blame] | 358 | IO.mapOptional("KeepEmptyLinesAtTheStartOfBlocks", |
| 359 | Style.KeepEmptyLinesAtTheStartOfBlocks); |
Birunthan Mohanathas | b001a0b | 2015-07-03 17:25:16 +0000 | [diff] [blame] | 360 | IO.mapOptional("MacroBlockBegin", Style.MacroBlockBegin); |
| 361 | IO.mapOptional("MacroBlockEnd", Style.MacroBlockEnd); |
Birunthan Mohanathas | 35cfbd7 | 2015-06-28 14:51:17 +0000 | [diff] [blame] | 362 | IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep); |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 363 | IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation); |
Daniel Jasper | 50d634b | 2014-10-28 16:53:38 +0000 | [diff] [blame] | 364 | IO.mapOptional("ObjCBlockIndentWidth", Style.ObjCBlockIndentWidth); |
Daniel Jasper | e9beea2 | 2014-01-28 15:20:33 +0000 | [diff] [blame] | 365 | IO.mapOptional("ObjCSpaceAfterProperty", Style.ObjCSpaceAfterProperty); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 366 | IO.mapOptional("ObjCSpaceBeforeProtocolList", |
| 367 | Style.ObjCSpaceBeforeProtocolList); |
Francois Ferrand | 9976efa | 2017-05-22 08:28:17 +0000 | [diff] [blame] | 368 | IO.mapOptional("PenaltyBreakAssignment", |
| 369 | Style.PenaltyBreakAssignment); |
Daniel Jasper | 33b909c | 2013-10-25 14:29:37 +0000 | [diff] [blame] | 370 | IO.mapOptional("PenaltyBreakBeforeFirstCallParameter", |
| 371 | Style.PenaltyBreakBeforeFirstCallParameter); |
Alexander Kornienko | dd7ece5 | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 372 | IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment); |
Daniel Jasper | 4e9678f | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 373 | IO.mapOptional("PenaltyBreakFirstLessLess", |
| 374 | Style.PenaltyBreakFirstLessLess); |
Birunthan Mohanathas | 35cfbd7 | 2015-06-28 14:51:17 +0000 | [diff] [blame] | 375 | IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 376 | IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter); |
| 377 | IO.mapOptional("PenaltyReturnTypeOnItsOwnLine", |
| 378 | Style.PenaltyReturnTypeOnItsOwnLine); |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 379 | IO.mapOptional("PointerAlignment", Style.PointerAlignment); |
Daniel Jasper | a0a5039 | 2015-12-01 13:28:53 +0000 | [diff] [blame] | 380 | IO.mapOptional("ReflowComments", Style.ReflowComments); |
| 381 | IO.mapOptional("SortIncludes", Style.SortIncludes); |
Krasimir Georgiev | ac16a20 | 2017-06-23 11:46:03 +0000 | [diff] [blame] | 382 | IO.mapOptional("SortUsingDeclarations", Style.SortUsingDeclarations); |
Daniel Jasper | db986eb | 2014-09-03 07:37:29 +0000 | [diff] [blame] | 383 | IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast); |
Sylvestre Ledru | 83bbd57 | 2016-08-09 14:24:40 +0000 | [diff] [blame] | 384 | IO.mapOptional("SpaceAfterTemplateKeyword", Style.SpaceAfterTemplateKeyword); |
Daniel Jasper | d94bff3 | 2013-09-25 15:15:02 +0000 | [diff] [blame] | 385 | IO.mapOptional("SpaceBeforeAssignmentOperators", |
| 386 | Style.SpaceBeforeAssignmentOperators); |
Birunthan Mohanathas | 35cfbd7 | 2015-06-28 14:51:17 +0000 | [diff] [blame] | 387 | IO.mapOptional("SpaceBeforeParens", Style.SpaceBeforeParens); |
| 388 | IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses); |
| 389 | IO.mapOptional("SpacesBeforeTrailingComments", |
| 390 | Style.SpacesBeforeTrailingComments); |
| 391 | IO.mapOptional("SpacesInAngles", Style.SpacesInAngles); |
| 392 | IO.mapOptional("SpacesInContainerLiterals", |
| 393 | Style.SpacesInContainerLiterals); |
| 394 | IO.mapOptional("SpacesInCStyleCastParentheses", |
| 395 | Style.SpacesInCStyleCastParentheses); |
| 396 | IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses); |
| 397 | IO.mapOptional("SpacesInSquareBrackets", Style.SpacesInSquareBrackets); |
| 398 | IO.mapOptional("Standard", Style.Standard); |
| 399 | IO.mapOptional("TabWidth", Style.TabWidth); |
| 400 | IO.mapOptional("UseTab", Style.UseTab); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 401 | } |
| 402 | }; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 403 | |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 404 | template <> struct MappingTraits<FormatStyle::BraceWrappingFlags> { |
| 405 | static void mapping(IO &IO, FormatStyle::BraceWrappingFlags &Wrapping) { |
| 406 | IO.mapOptional("AfterClass", Wrapping.AfterClass); |
| 407 | IO.mapOptional("AfterControlStatement", Wrapping.AfterControlStatement); |
| 408 | IO.mapOptional("AfterEnum", Wrapping.AfterEnum); |
| 409 | IO.mapOptional("AfterFunction", Wrapping.AfterFunction); |
| 410 | IO.mapOptional("AfterNamespace", Wrapping.AfterNamespace); |
| 411 | IO.mapOptional("AfterObjCDeclaration", Wrapping.AfterObjCDeclaration); |
| 412 | IO.mapOptional("AfterStruct", Wrapping.AfterStruct); |
| 413 | IO.mapOptional("AfterUnion", Wrapping.AfterUnion); |
| 414 | IO.mapOptional("BeforeCatch", Wrapping.BeforeCatch); |
| 415 | IO.mapOptional("BeforeElse", Wrapping.BeforeElse); |
| 416 | IO.mapOptional("IndentBraces", Wrapping.IndentBraces); |
Francois Ferrand | ad72256 | 2017-06-30 20:25:55 +0000 | [diff] [blame] | 417 | IO.mapOptional("SplitEmptyFunction", Wrapping.SplitEmptyFunction); |
| 418 | IO.mapOptional("SplitEmptyRecord", Wrapping.SplitEmptyRecord); |
| 419 | IO.mapOptional("SplitEmptyNamespace", Wrapping.SplitEmptyNamespace); |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 420 | } |
| 421 | }; |
| 422 | |
Daniel Jasper | 8ce1b8d | 2015-10-06 11:54:18 +0000 | [diff] [blame] | 423 | template <> struct MappingTraits<FormatStyle::IncludeCategory> { |
| 424 | static void mapping(IO &IO, FormatStyle::IncludeCategory &Category) { |
| 425 | IO.mapOptional("Regex", Category.Regex); |
| 426 | IO.mapOptional("Priority", Category.Priority); |
| 427 | } |
| 428 | }; |
| 429 | |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 430 | // Allows to read vector<FormatStyle> while keeping default values. |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 431 | // IO.getContext() should contain a pointer to the FormatStyle structure, that |
| 432 | // will be used to get default values for missing keys. |
| 433 | // If the first element has no Language specified, it will be treated as the |
| 434 | // default one for the following elements. |
Jacques Pienaar | fc27511 | 2015-02-18 23:48:37 +0000 | [diff] [blame] | 435 | template <> struct DocumentListTraits<std::vector<FormatStyle>> { |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 436 | static size_t size(IO &IO, std::vector<FormatStyle> &Seq) { |
| 437 | return Seq.size(); |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 438 | } |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 439 | static FormatStyle &element(IO &IO, std::vector<FormatStyle> &Seq, |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 440 | size_t Index) { |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 441 | if (Index >= Seq.size()) { |
| 442 | assert(Index == Seq.size()); |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 443 | FormatStyle Template; |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 444 | if (Seq.size() > 0 && Seq[0].Language == FormatStyle::LK_None) { |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 445 | Template = Seq[0]; |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 446 | } else { |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 447 | Template = *((const FormatStyle *)IO.getContext()); |
Alexander Kornienko | 6d2c88e | 2013-12-10 10:30:34 +0000 | [diff] [blame] | 448 | Template.Language = FormatStyle::LK_None; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 449 | } |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 450 | Seq.resize(Index + 1, Template); |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 451 | } |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 452 | return Seq[Index]; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 453 | } |
| 454 | }; |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 455 | } // namespace yaml |
| 456 | } // namespace llvm |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 457 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 458 | namespace clang { |
| 459 | namespace format { |
| 460 | |
Rafael Espindola | 6d0d89b | 2014-06-12 03:31:26 +0000 | [diff] [blame] | 461 | const std::error_category &getParseCategory() { |
Rafael Espindola | d013670 | 2014-06-12 02:50:04 +0000 | [diff] [blame] | 462 | static ParseErrorCategory C; |
| 463 | return C; |
| 464 | } |
| 465 | std::error_code make_error_code(ParseError e) { |
Rafael Espindola | 6d0d89b | 2014-06-12 03:31:26 +0000 | [diff] [blame] | 466 | return std::error_code(static_cast<int>(e), getParseCategory()); |
Rafael Espindola | d013670 | 2014-06-12 02:50:04 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Antonio Maiorano | 3adfb6a | 2017-01-17 00:12:27 +0000 | [diff] [blame] | 469 | inline llvm::Error make_string_error(const llvm::Twine &Message) { |
| 470 | return llvm::make_error<llvm::StringError>(Message, |
| 471 | llvm::inconvertibleErrorCode()); |
| 472 | } |
| 473 | |
Reid Kleckner | 6432d45 | 2016-10-19 23:39:55 +0000 | [diff] [blame] | 474 | const char *ParseErrorCategory::name() const noexcept { |
Rafael Espindola | d013670 | 2014-06-12 02:50:04 +0000 | [diff] [blame] | 475 | return "clang-format.parse_error"; |
| 476 | } |
| 477 | |
| 478 | std::string ParseErrorCategory::message(int EV) const { |
| 479 | switch (static_cast<ParseError>(EV)) { |
| 480 | case ParseError::Success: |
| 481 | return "Success"; |
| 482 | case ParseError::Error: |
| 483 | return "Invalid argument"; |
| 484 | case ParseError::Unsuitable: |
| 485 | return "Unsuitable"; |
| 486 | } |
Saleem Abdulrasool | fbfbaf6 | 2014-06-12 19:33:26 +0000 | [diff] [blame] | 487 | llvm_unreachable("unexpected parse error"); |
Rafael Espindola | d013670 | 2014-06-12 02:50:04 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 490 | static FormatStyle expandPresets(const FormatStyle &Style) { |
Daniel Jasper | 55bbe66 | 2015-10-07 04:06:10 +0000 | [diff] [blame] | 491 | if (Style.BreakBeforeBraces == FormatStyle::BS_Custom) |
| 492 | return Style; |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 493 | FormatStyle Expanded = Style; |
| 494 | Expanded.BraceWrapping = {false, false, false, false, false, false, |
Francois Ferrand | ad72256 | 2017-06-30 20:25:55 +0000 | [diff] [blame] | 495 | false, false, false, false, false, true, |
| 496 | true, true}; |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 497 | switch (Style.BreakBeforeBraces) { |
| 498 | case FormatStyle::BS_Linux: |
| 499 | Expanded.BraceWrapping.AfterClass = true; |
| 500 | Expanded.BraceWrapping.AfterFunction = true; |
| 501 | Expanded.BraceWrapping.AfterNamespace = true; |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 502 | break; |
| 503 | case FormatStyle::BS_Mozilla: |
| 504 | Expanded.BraceWrapping.AfterClass = true; |
| 505 | Expanded.BraceWrapping.AfterEnum = true; |
| 506 | Expanded.BraceWrapping.AfterFunction = true; |
| 507 | Expanded.BraceWrapping.AfterStruct = true; |
| 508 | Expanded.BraceWrapping.AfterUnion = true; |
Francois Ferrand | ad72256 | 2017-06-30 20:25:55 +0000 | [diff] [blame] | 509 | Expanded.BraceWrapping.SplitEmptyFunction = false; |
| 510 | Expanded.BraceWrapping.SplitEmptyRecord = false; |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 511 | break; |
| 512 | case FormatStyle::BS_Stroustrup: |
| 513 | Expanded.BraceWrapping.AfterFunction = true; |
| 514 | Expanded.BraceWrapping.BeforeCatch = true; |
| 515 | Expanded.BraceWrapping.BeforeElse = true; |
| 516 | break; |
| 517 | case FormatStyle::BS_Allman: |
| 518 | Expanded.BraceWrapping.AfterClass = true; |
| 519 | Expanded.BraceWrapping.AfterControlStatement = true; |
| 520 | Expanded.BraceWrapping.AfterEnum = true; |
| 521 | Expanded.BraceWrapping.AfterFunction = true; |
| 522 | Expanded.BraceWrapping.AfterNamespace = true; |
| 523 | Expanded.BraceWrapping.AfterObjCDeclaration = true; |
| 524 | Expanded.BraceWrapping.AfterStruct = true; |
| 525 | Expanded.BraceWrapping.BeforeCatch = true; |
| 526 | Expanded.BraceWrapping.BeforeElse = true; |
| 527 | break; |
| 528 | case FormatStyle::BS_GNU: |
| 529 | Expanded.BraceWrapping = {true, true, true, true, true, true, |
Francois Ferrand | ad72256 | 2017-06-30 20:25:55 +0000 | [diff] [blame] | 530 | true, true, true, true, true, true, |
| 531 | true, true}; |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 532 | break; |
| 533 | case FormatStyle::BS_WebKit: |
| 534 | Expanded.BraceWrapping.AfterFunction = true; |
| 535 | break; |
| 536 | default: |
| 537 | break; |
| 538 | } |
| 539 | return Expanded; |
| 540 | } |
| 541 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 542 | FormatStyle getLLVMStyle() { |
| 543 | FormatStyle LLVMStyle; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 544 | LLVMStyle.Language = FormatStyle::LK_Cpp; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 545 | LLVMStyle.AccessModifierOffset = -2; |
Daniel Jasper | 7fdbb3f | 2017-05-08 15:08:00 +0000 | [diff] [blame] | 546 | LLVMStyle.AlignEscapedNewlines = FormatStyle::ENAS_Right; |
Daniel Jasper | 6501f7e | 2015-10-27 12:38:37 +0000 | [diff] [blame] | 547 | LLVMStyle.AlignAfterOpenBracket = FormatStyle::BAS_Align; |
Daniel Jasper | 3219e43 | 2014-12-02 13:24:51 +0000 | [diff] [blame] | 548 | LLVMStyle.AlignOperands = true; |
Daniel Jasper | 552f4a7 | 2013-07-31 23:55:15 +0000 | [diff] [blame] | 549 | LLVMStyle.AlignTrailingComments = true; |
Daniel Jasper | a4499133 | 2015-04-29 13:06:49 +0000 | [diff] [blame] | 550 | LLVMStyle.AlignConsecutiveAssignments = false; |
Daniel Jasper | e12597c | 2015-10-01 10:06:54 +0000 | [diff] [blame] | 551 | LLVMStyle.AlignConsecutiveDeclarations = false; |
Daniel Jasper | f7db433 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 552 | LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true; |
Daniel Jasper | d74cf40 | 2014-04-08 12:46:38 +0000 | [diff] [blame] | 553 | LLVMStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; |
Daniel Jasper | 17605d3 | 2014-05-14 09:33:35 +0000 | [diff] [blame] | 554 | LLVMStyle.AllowShortBlocksOnASingleLine = false; |
Daniel Jasper | b87899b | 2014-09-10 13:11:45 +0000 | [diff] [blame] | 555 | LLVMStyle.AllowShortCaseLabelsOnASingleLine = false; |
Daniel Jasper | 1b750ed | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 556 | LLVMStyle.AllowShortIfStatementsOnASingleLine = false; |
Daniel Jasper | 3a685df | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 557 | LLVMStyle.AllowShortLoopsOnASingleLine = false; |
Zachary Turner | 448592e | 2015-12-18 22:20:15 +0000 | [diff] [blame] | 558 | LLVMStyle.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; |
Birunthan Mohanathas | a0388a8 | 2015-06-29 15:30:42 +0000 | [diff] [blame] | 559 | LLVMStyle.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_None; |
Alexander Kornienko | 5861171 | 2013-07-04 12:02:44 +0000 | [diff] [blame] | 560 | LLVMStyle.AlwaysBreakBeforeMultilineStrings = false; |
Daniel Jasper | e33d4af | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 561 | LLVMStyle.AlwaysBreakTemplateDeclarations = false; |
Daniel Jasper | 18210d7 | 2014-10-09 09:52:05 +0000 | [diff] [blame] | 562 | LLVMStyle.BinPackArguments = true; |
Francois Ferrand | e56a829 | 2017-06-14 12:29:47 +0000 | [diff] [blame] | 563 | LLVMStyle.BinPackParameters = true; |
Daniel Jasper | ac043c9 | 2014-09-15 11:11:00 +0000 | [diff] [blame] | 564 | LLVMStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_None; |
Daniel Jasper | 165b29e | 2013-11-08 00:57:11 +0000 | [diff] [blame] | 565 | LLVMStyle.BreakBeforeTernaryOperators = true; |
Daniel Jasper | e33d4af | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 566 | LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach; |
Daniel Jasper | 55bbe66 | 2015-10-07 04:06:10 +0000 | [diff] [blame] | 567 | LLVMStyle.BraceWrapping = {false, false, false, false, false, false, |
Francois Ferrand | ad72256 | 2017-06-30 20:25:55 +0000 | [diff] [blame] | 568 | false, false, false, false, false, true, |
| 569 | true, true}; |
Nico Weber | 2cd92f1 | 2015-10-15 16:03:01 +0000 | [diff] [blame] | 570 | LLVMStyle.BreakAfterJavaFieldAnnotations = false; |
Francois Ferrand | a6b6d51 | 2017-05-24 11:36:58 +0000 | [diff] [blame] | 571 | LLVMStyle.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; |
Andi-Bogdan Postelnicu | 0ef8ee1 | 2017-03-10 15:10:37 +0000 | [diff] [blame] | 572 | LLVMStyle.BreakBeforeInheritanceComma = false; |
Daniel Jasper | e1a7b76 | 2016-02-01 11:21:02 +0000 | [diff] [blame] | 573 | LLVMStyle.BreakStringLiterals = true; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 574 | LLVMStyle.ColumnLimit = 80; |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 575 | LLVMStyle.CommentPragmas = "^ IWYU pragma:"; |
Francois Ferrand | e56a829 | 2017-06-14 12:29:47 +0000 | [diff] [blame] | 576 | LLVMStyle.CompactNamespaces = false; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 577 | LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false; |
Daniel Jasper | cdaffa4 | 2013-08-13 10:58:30 +0000 | [diff] [blame] | 578 | LLVMStyle.ConstructorInitializerIndentWidth = 4; |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 579 | LLVMStyle.ContinuationIndentWidth = 4; |
Chandler Carruth | f8b7266 | 2014-03-02 12:37:31 +0000 | [diff] [blame] | 580 | LLVMStyle.Cpp11BracedListStyle = true; |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 581 | LLVMStyle.DerivePointerAlignment = false; |
Daniel Jasper | b10cbc4 | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 582 | LLVMStyle.ExperimentalAutoDetectBinPacking = false; |
Krasimir Georgiev | 32eaa86 | 2017-03-01 15:35:39 +0000 | [diff] [blame] | 583 | LLVMStyle.FixNamespaceComments = true; |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 584 | LLVMStyle.ForEachMacros.push_back("foreach"); |
| 585 | LLVMStyle.ForEachMacros.push_back("Q_FOREACH"); |
| 586 | LLVMStyle.ForEachMacros.push_back("BOOST_FOREACH"); |
Daniel Jasper | 85c472d | 2015-09-29 07:53:08 +0000 | [diff] [blame] | 587 | LLVMStyle.IncludeCategories = {{"^\"(llvm|llvm-c|clang|clang-c)/", 2}, |
Chandler Carruth | d676ab1 | 2017-06-29 23:20:54 +0000 | [diff] [blame] | 588 | {"^(<|\"(gtest|gmock|isl|json)/)", 3}, |
Daniel Jasper | 85c472d | 2015-09-29 07:53:08 +0000 | [diff] [blame] | 589 | {".*", 1}}; |
Chandler Carruth | d676ab1 | 2017-06-29 23:20:54 +0000 | [diff] [blame] | 590 | LLVMStyle.IncludeIsMainRegex = "(Test)?$"; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 591 | LLVMStyle.IndentCaseLabels = false; |
Daniel Jasper | c75e1ef | 2014-07-09 08:42:42 +0000 | [diff] [blame] | 592 | LLVMStyle.IndentWrappedFunctionNames = false; |
Daniel Jasper | e33d4af | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 593 | LLVMStyle.IndentWidth = 2; |
Martin Probst | fb2342d | 2016-06-13 17:50:10 +0000 | [diff] [blame] | 594 | LLVMStyle.JavaScriptQuotes = FormatStyle::JSQS_Leave; |
| 595 | LLVMStyle.JavaScriptWrapImports = true; |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 596 | LLVMStyle.TabWidth = 8; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 597 | LLVMStyle.MaxEmptyLinesToKeep = 1; |
Daniel Jasper | a26fc5c | 2014-03-21 13:43:14 +0000 | [diff] [blame] | 598 | LLVMStyle.KeepEmptyLinesAtTheStartOfBlocks = true; |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 599 | LLVMStyle.NamespaceIndentation = FormatStyle::NI_None; |
Daniel Jasper | 50d634b | 2014-10-28 16:53:38 +0000 | [diff] [blame] | 600 | LLVMStyle.ObjCBlockIndentWidth = 2; |
Daniel Jasper | e9beea2 | 2014-01-28 15:20:33 +0000 | [diff] [blame] | 601 | LLVMStyle.ObjCSpaceAfterProperty = false; |
Nico Weber | a608775 | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 602 | LLVMStyle.ObjCSpaceBeforeProtocolList = true; |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 603 | LLVMStyle.PointerAlignment = FormatStyle::PAS_Right; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 604 | LLVMStyle.SpacesBeforeTrailingComments = 1; |
Chandler Carruth | f8b7266 | 2014-03-02 12:37:31 +0000 | [diff] [blame] | 605 | LLVMStyle.Standard = FormatStyle::LS_Cpp11; |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 606 | LLVMStyle.UseTab = FormatStyle::UT_Never; |
Daniel Jasper | a0a5039 | 2015-12-01 13:28:53 +0000 | [diff] [blame] | 607 | LLVMStyle.ReflowComments = true; |
Daniel Jasper | b55acad | 2013-08-20 12:36:34 +0000 | [diff] [blame] | 608 | LLVMStyle.SpacesInParentheses = false; |
Daniel Jasper | ad981f8 | 2014-08-26 11:41:14 +0000 | [diff] [blame] | 609 | LLVMStyle.SpacesInSquareBrackets = false; |
Daniel Jasper | b55acad | 2013-08-20 12:36:34 +0000 | [diff] [blame] | 610 | LLVMStyle.SpaceInEmptyParentheses = false; |
Daniel Jasper | b2e10a5 | 2014-01-15 15:09:08 +0000 | [diff] [blame] | 611 | LLVMStyle.SpacesInContainerLiterals = true; |
Daniel Jasper | b55acad | 2013-08-20 12:36:34 +0000 | [diff] [blame] | 612 | LLVMStyle.SpacesInCStyleCastParentheses = false; |
Daniel Jasper | db986eb | 2014-09-03 07:37:29 +0000 | [diff] [blame] | 613 | LLVMStyle.SpaceAfterCStyleCast = false; |
Sylvestre Ledru | 83bbd57 | 2016-08-09 14:24:40 +0000 | [diff] [blame] | 614 | LLVMStyle.SpaceAfterTemplateKeyword = true; |
Alexander Kornienko | fdca83d | 2013-12-10 10:18:34 +0000 | [diff] [blame] | 615 | LLVMStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements; |
Daniel Jasper | d94bff3 | 2013-09-25 15:15:02 +0000 | [diff] [blame] | 616 | LLVMStyle.SpaceBeforeAssignmentOperators = true; |
Daniel Jasper | dd978ae | 2013-10-29 14:52:02 +0000 | [diff] [blame] | 617 | LLVMStyle.SpacesInAngles = false; |
Daniel Jasper | 4e9678f | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 618 | |
Francois Ferrand | 9976efa | 2017-05-22 08:28:17 +0000 | [diff] [blame] | 619 | LLVMStyle.PenaltyBreakAssignment = prec::Assignment; |
Daniel Jasper | 19a541e | 2013-12-19 16:45:34 +0000 | [diff] [blame] | 620 | LLVMStyle.PenaltyBreakComment = 300; |
Alexander Kornienko | fe7a57f | 2013-12-10 15:42:15 +0000 | [diff] [blame] | 621 | LLVMStyle.PenaltyBreakFirstLessLess = 120; |
| 622 | LLVMStyle.PenaltyBreakString = 1000; |
| 623 | LLVMStyle.PenaltyExcessCharacter = 1000000; |
Daniel Jasper | 4e9678f | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 624 | LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60; |
Daniel Jasper | 33b909c | 2013-10-25 14:29:37 +0000 | [diff] [blame] | 625 | LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19; |
Daniel Jasper | 4e9678f | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 626 | |
Daniel Jasper | c64b09a | 2014-05-22 15:12:22 +0000 | [diff] [blame] | 627 | LLVMStyle.DisableFormat = false; |
Daniel Jasper | da44677 | 2015-11-16 12:38:56 +0000 | [diff] [blame] | 628 | LLVMStyle.SortIncludes = true; |
Krasimir Georgiev | ac16a20 | 2017-06-23 11:46:03 +0000 | [diff] [blame] | 629 | LLVMStyle.SortUsingDeclarations = true; |
Daniel Jasper | c64b09a | 2014-05-22 15:12:22 +0000 | [diff] [blame] | 630 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 631 | return LLVMStyle; |
| 632 | } |
| 633 | |
Nico Weber | 514ecc8 | 2014-02-02 20:50:45 +0000 | [diff] [blame] | 634 | FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) { |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame^] | 635 | if (Language == FormatStyle::LK_TextProto) { |
| 636 | FormatStyle GoogleStyle = getGoogleStyle(FormatStyle::LK_Proto); |
| 637 | GoogleStyle.Language = FormatStyle::LK_TextProto; |
| 638 | return GoogleStyle; |
| 639 | } |
| 640 | |
Alexander Kornienko | fe7a57f | 2013-12-10 15:42:15 +0000 | [diff] [blame] | 641 | FormatStyle GoogleStyle = getLLVMStyle(); |
Nico Weber | 514ecc8 | 2014-02-02 20:50:45 +0000 | [diff] [blame] | 642 | GoogleStyle.Language = Language; |
| 643 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 644 | GoogleStyle.AccessModifierOffset = -1; |
Daniel Jasper | 7fdbb3f | 2017-05-08 15:08:00 +0000 | [diff] [blame] | 645 | GoogleStyle.AlignEscapedNewlines = FormatStyle::ENAS_Left; |
Daniel Jasper | 085a2ed | 2013-04-24 13:46:00 +0000 | [diff] [blame] | 646 | GoogleStyle.AllowShortIfStatementsOnASingleLine = true; |
Daniel Jasper | 5bd0b9e | 2013-05-23 18:05:18 +0000 | [diff] [blame] | 647 | GoogleStyle.AllowShortLoopsOnASingleLine = true; |
Alexander Kornienko | 5861171 | 2013-07-04 12:02:44 +0000 | [diff] [blame] | 648 | GoogleStyle.AlwaysBreakBeforeMultilineStrings = true; |
Daniel Jasper | e33d4af | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 649 | GoogleStyle.AlwaysBreakTemplateDeclarations = true; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 650 | GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 651 | GoogleStyle.DerivePointerAlignment = true; |
Daniel Jasper | 85c472d | 2015-09-29 07:53:08 +0000 | [diff] [blame] | 652 | GoogleStyle.IncludeCategories = {{"^<.*\\.h>", 1}, {"^<.*", 2}, {".*", 3}}; |
Daniel Jasper | 9c8ff35 | 2016-03-21 14:11:27 +0000 | [diff] [blame] | 653 | GoogleStyle.IncludeIsMainRegex = "([-_](test|unittest))?$"; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 654 | GoogleStyle.IndentCaseLabels = true; |
Daniel Jasper | a26fc5c | 2014-03-21 13:43:14 +0000 | [diff] [blame] | 655 | GoogleStyle.KeepEmptyLinesAtTheStartOfBlocks = false; |
Daniel Jasper | e9beea2 | 2014-01-28 15:20:33 +0000 | [diff] [blame] | 656 | GoogleStyle.ObjCSpaceAfterProperty = false; |
Nico Weber | a608775 | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 657 | GoogleStyle.ObjCSpaceBeforeProtocolList = false; |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 658 | GoogleStyle.PointerAlignment = FormatStyle::PAS_Left; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 659 | GoogleStyle.SpacesBeforeTrailingComments = 2; |
| 660 | GoogleStyle.Standard = FormatStyle::LS_Auto; |
Daniel Jasper | 4e9678f | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 661 | |
Daniel Jasper | 4e9678f | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 662 | GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200; |
Daniel Jasper | 33b909c | 2013-10-25 14:29:37 +0000 | [diff] [blame] | 663 | GoogleStyle.PenaltyBreakBeforeFirstCallParameter = 1; |
Daniel Jasper | 4e9678f | 2013-07-11 20:41:21 +0000 | [diff] [blame] | 664 | |
Daniel Jasper | c58c70e | 2014-09-15 11:21:46 +0000 | [diff] [blame] | 665 | if (Language == FormatStyle::LK_Java) { |
Daniel Jasper | 6501f7e | 2015-10-27 12:38:37 +0000 | [diff] [blame] | 666 | GoogleStyle.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; |
Daniel Jasper | 3219e43 | 2014-12-02 13:24:51 +0000 | [diff] [blame] | 667 | GoogleStyle.AlignOperands = false; |
Daniel Jasper | fd4ed18 | 2015-01-04 20:40:45 +0000 | [diff] [blame] | 668 | GoogleStyle.AlignTrailingComments = false; |
Daniel Jasper | 9e70935 | 2014-11-26 10:43:58 +0000 | [diff] [blame] | 669 | GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; |
Daniel Jasper | fd4ed18 | 2015-01-04 20:40:45 +0000 | [diff] [blame] | 670 | GoogleStyle.AllowShortIfStatementsOnASingleLine = false; |
Daniel Jasper | 1cd3c71 | 2015-01-14 12:24:59 +0000 | [diff] [blame] | 671 | GoogleStyle.AlwaysBreakBeforeMultilineStrings = false; |
Daniel Jasper | c58c70e | 2014-09-15 11:21:46 +0000 | [diff] [blame] | 672 | GoogleStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; |
| 673 | GoogleStyle.ColumnLimit = 100; |
| 674 | GoogleStyle.SpaceAfterCStyleCast = true; |
Daniel Jasper | 61d8197 | 2014-11-14 08:22:46 +0000 | [diff] [blame] | 675 | GoogleStyle.SpacesBeforeTrailingComments = 1; |
Daniel Jasper | c58c70e | 2014-09-15 11:21:46 +0000 | [diff] [blame] | 676 | } else if (Language == FormatStyle::LK_JavaScript) { |
Daniel Jasper | 6501f7e | 2015-10-27 12:38:37 +0000 | [diff] [blame] | 677 | GoogleStyle.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; |
Daniel Jasper | 41a2bf7 | 2015-12-21 13:52:19 +0000 | [diff] [blame] | 678 | GoogleStyle.AlignOperands = false; |
Daniel Jasper | 28d8a5a | 2016-09-07 23:01:13 +0000 | [diff] [blame] | 679 | GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; |
Daniel Jasper | 6501f7e | 2015-10-27 12:38:37 +0000 | [diff] [blame] | 680 | GoogleStyle.AlwaysBreakBeforeMultilineStrings = false; |
Daniel Jasper | e551bb7 | 2014-11-05 17:22:31 +0000 | [diff] [blame] | 681 | GoogleStyle.BreakBeforeTernaryOperators = false; |
Martin Probst | 2083f31 | 2017-05-09 12:45:48 +0000 | [diff] [blame] | 682 | // taze:, triple slash directives (`/// <...`), @tag followed by { for a lot |
| 683 | // of JSDoc tags, and @see, which is commonly followed by overlong URLs. |
| 684 | GoogleStyle.CommentPragmas = |
| 685 | "(taze:|^/[ \t]*<|(@[A-Za-z_0-9-]+[ \\t]*{)|@see)"; |
Daniel Jasper | 8f83a90 | 2014-05-09 10:28:58 +0000 | [diff] [blame] | 686 | GoogleStyle.MaxEmptyLinesToKeep = 3; |
Martin Probst | ece8c0c | 2016-06-13 16:41:28 +0000 | [diff] [blame] | 687 | GoogleStyle.NamespaceIndentation = FormatStyle::NI_All; |
Nico Weber | 514ecc8 | 2014-02-02 20:50:45 +0000 | [diff] [blame] | 688 | GoogleStyle.SpacesInContainerLiterals = false; |
Daniel Jasper | abd1f57 | 2016-03-02 22:44:03 +0000 | [diff] [blame] | 689 | GoogleStyle.JavaScriptQuotes = FormatStyle::JSQS_Single; |
Martin Probst | 0cd74ee | 2016-06-13 16:39:50 +0000 | [diff] [blame] | 690 | GoogleStyle.JavaScriptWrapImports = false; |
Nico Weber | 514ecc8 | 2014-02-02 20:50:45 +0000 | [diff] [blame] | 691 | } else if (Language == FormatStyle::LK_Proto) { |
Daniel Jasper | d74cf40 | 2014-04-08 12:46:38 +0000 | [diff] [blame] | 692 | GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; |
Daniel Jasper | 783bac6 | 2014-04-15 09:54:30 +0000 | [diff] [blame] | 693 | GoogleStyle.SpacesInContainerLiterals = false; |
Daniel Jasper | 03a04fe | 2016-12-12 12:42:29 +0000 | [diff] [blame] | 694 | } else if (Language == FormatStyle::LK_ObjC) { |
| 695 | GoogleStyle.ColumnLimit = 100; |
Nico Weber | 514ecc8 | 2014-02-02 20:50:45 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 698 | return GoogleStyle; |
| 699 | } |
| 700 | |
Nico Weber | 514ecc8 | 2014-02-02 20:50:45 +0000 | [diff] [blame] | 701 | FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) { |
| 702 | FormatStyle ChromiumStyle = getGoogleStyle(Language); |
Nico Weber | 450425c | 2014-11-26 16:43:18 +0000 | [diff] [blame] | 703 | if (Language == FormatStyle::LK_Java) { |
Daniel Jasper | fd4ed18 | 2015-01-04 20:40:45 +0000 | [diff] [blame] | 704 | ChromiumStyle.AllowShortIfStatementsOnASingleLine = true; |
Nico Weber | 2cd92f1 | 2015-10-15 16:03:01 +0000 | [diff] [blame] | 705 | ChromiumStyle.BreakAfterJavaFieldAnnotations = true; |
Nico Weber | 450425c | 2014-11-26 16:43:18 +0000 | [diff] [blame] | 706 | ChromiumStyle.ContinuationIndentWidth = 8; |
Nico Weber | 2cd92f1 | 2015-10-15 16:03:01 +0000 | [diff] [blame] | 707 | ChromiumStyle.IndentWidth = 4; |
Nico Weber | ea64969 | 2017-01-04 02:33:36 +0000 | [diff] [blame] | 708 | } else if (Language == FormatStyle::LK_JavaScript) { |
| 709 | ChromiumStyle.AllowShortIfStatementsOnASingleLine = false; |
| 710 | ChromiumStyle.AllowShortLoopsOnASingleLine = false; |
Nico Weber | 450425c | 2014-11-26 16:43:18 +0000 | [diff] [blame] | 711 | } else { |
| 712 | ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false; |
| 713 | ChromiumStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; |
| 714 | ChromiumStyle.AllowShortIfStatementsOnASingleLine = false; |
| 715 | ChromiumStyle.AllowShortLoopsOnASingleLine = false; |
| 716 | ChromiumStyle.BinPackParameters = false; |
| 717 | ChromiumStyle.DerivePointerAlignment = false; |
Nico Weber | 9e2bc30 | 2017-01-31 18:42:05 +0000 | [diff] [blame] | 718 | if (Language == FormatStyle::LK_ObjC) |
| 719 | ChromiumStyle.ColumnLimit = 80; |
Nico Weber | 450425c | 2014-11-26 16:43:18 +0000 | [diff] [blame] | 720 | } |
Daniel Jasper | 1b750ed | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 721 | return ChromiumStyle; |
| 722 | } |
| 723 | |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 724 | FormatStyle getMozillaStyle() { |
| 725 | FormatStyle MozillaStyle = getLLVMStyle(); |
| 726 | MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false; |
Birunthan Mohanathas | a081002 | 2015-06-29 15:18:58 +0000 | [diff] [blame] | 727 | MozillaStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; |
Zachary Turner | 448592e | 2015-12-18 22:20:15 +0000 | [diff] [blame] | 728 | MozillaStyle.AlwaysBreakAfterReturnType = |
Sylvestre Ledru | dcb038d | 2016-12-14 16:09:29 +0000 | [diff] [blame] | 729 | FormatStyle::RTBS_TopLevel; |
Birunthan Mohanathas | a0388a8 | 2015-06-29 15:30:42 +0000 | [diff] [blame] | 730 | MozillaStyle.AlwaysBreakAfterDefinitionReturnType = |
| 731 | FormatStyle::DRTBS_TopLevel; |
Birunthan Mohanathas | a081002 | 2015-06-29 15:18:58 +0000 | [diff] [blame] | 732 | MozillaStyle.AlwaysBreakTemplateDeclarations = true; |
Sylvestre Ledru | dcb038d | 2016-12-14 16:09:29 +0000 | [diff] [blame] | 733 | MozillaStyle.BinPackParameters = false; |
| 734 | MozillaStyle.BinPackArguments = false; |
Birunthan Mohanathas | 305fa9c | 2015-07-12 03:13:54 +0000 | [diff] [blame] | 735 | MozillaStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; |
Francois Ferrand | a6b6d51 | 2017-05-24 11:36:58 +0000 | [diff] [blame] | 736 | MozillaStyle.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; |
Andi-Bogdan Postelnicu | 0ef8ee1 | 2017-03-10 15:10:37 +0000 | [diff] [blame] | 737 | MozillaStyle.BreakBeforeInheritanceComma = true; |
Birunthan Mohanathas | a081002 | 2015-06-29 15:18:58 +0000 | [diff] [blame] | 738 | MozillaStyle.ConstructorInitializerIndentWidth = 2; |
| 739 | MozillaStyle.ContinuationIndentWidth = 2; |
Chandler Carruth | f8b7266 | 2014-03-02 12:37:31 +0000 | [diff] [blame] | 740 | MozillaStyle.Cpp11BracedListStyle = false; |
Krasimir Georgiev | 32eaa86 | 2017-03-01 15:35:39 +0000 | [diff] [blame] | 741 | MozillaStyle.FixNamespaceComments = false; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 742 | MozillaStyle.IndentCaseLabels = true; |
Daniel Jasper | e9beea2 | 2014-01-28 15:20:33 +0000 | [diff] [blame] | 743 | MozillaStyle.ObjCSpaceAfterProperty = true; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 744 | MozillaStyle.ObjCSpaceBeforeProtocolList = false; |
| 745 | MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200; |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 746 | MozillaStyle.PointerAlignment = FormatStyle::PAS_Left; |
Sylvestre Ledru | 83bbd57 | 2016-08-09 14:24:40 +0000 | [diff] [blame] | 747 | MozillaStyle.SpaceAfterTemplateKeyword = false; |
Alexander Kornienko | c860266 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 748 | return MozillaStyle; |
| 749 | } |
| 750 | |
Daniel Jasper | ffefb3d | 2013-07-24 13:10:59 +0000 | [diff] [blame] | 751 | FormatStyle getWebKitStyle() { |
| 752 | FormatStyle Style = getLLVMStyle(); |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 753 | Style.AccessModifierOffset = -4; |
Daniel Jasper | 6501f7e | 2015-10-27 12:38:37 +0000 | [diff] [blame] | 754 | Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; |
Daniel Jasper | 3219e43 | 2014-12-02 13:24:51 +0000 | [diff] [blame] | 755 | Style.AlignOperands = false; |
Daniel Jasper | 552f4a7 | 2013-07-31 23:55:15 +0000 | [diff] [blame] | 756 | Style.AlignTrailingComments = false; |
Daniel Jasper | ac043c9 | 2014-09-15 11:11:00 +0000 | [diff] [blame] | 757 | Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; |
Roman Kashitsyn | 291f64f | 2015-08-10 13:43:19 +0000 | [diff] [blame] | 758 | Style.BreakBeforeBraces = FormatStyle::BS_WebKit; |
Francois Ferrand | a6b6d51 | 2017-05-24 11:36:58 +0000 | [diff] [blame] | 759 | Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; |
Chandler Carruth | f8b7266 | 2014-03-02 12:37:31 +0000 | [diff] [blame] | 760 | Style.Cpp11BracedListStyle = false; |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 761 | Style.ColumnLimit = 0; |
Krasimir Georgiev | 32eaa86 | 2017-03-01 15:35:39 +0000 | [diff] [blame] | 762 | Style.FixNamespaceComments = false; |
Daniel Jasper | e33d4af | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 763 | Style.IndentWidth = 4; |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 764 | Style.NamespaceIndentation = FormatStyle::NI_Inner; |
Daniel Jasper | 50d634b | 2014-10-28 16:53:38 +0000 | [diff] [blame] | 765 | Style.ObjCBlockIndentWidth = 4; |
Daniel Jasper | e9beea2 | 2014-01-28 15:20:33 +0000 | [diff] [blame] | 766 | Style.ObjCSpaceAfterProperty = true; |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 767 | Style.PointerAlignment = FormatStyle::PAS_Left; |
Daniel Jasper | ffefb3d | 2013-07-24 13:10:59 +0000 | [diff] [blame] | 768 | return Style; |
| 769 | } |
| 770 | |
Alexander Kornienko | fe7a57f | 2013-12-10 15:42:15 +0000 | [diff] [blame] | 771 | FormatStyle getGNUStyle() { |
| 772 | FormatStyle Style = getLLVMStyle(); |
Birunthan Mohanathas | a0388a8 | 2015-06-29 15:30:42 +0000 | [diff] [blame] | 773 | Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; |
Zachary Turner | 448592e | 2015-12-18 22:20:15 +0000 | [diff] [blame] | 774 | Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; |
Daniel Jasper | ac043c9 | 2014-09-15 11:11:00 +0000 | [diff] [blame] | 775 | Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 776 | Style.BreakBeforeBraces = FormatStyle::BS_GNU; |
Alexander Kornienko | fe7a57f | 2013-12-10 15:42:15 +0000 | [diff] [blame] | 777 | Style.BreakBeforeTernaryOperators = true; |
Chandler Carruth | f8b7266 | 2014-03-02 12:37:31 +0000 | [diff] [blame] | 778 | Style.Cpp11BracedListStyle = false; |
Alexander Kornienko | fe7a57f | 2013-12-10 15:42:15 +0000 | [diff] [blame] | 779 | Style.ColumnLimit = 79; |
Krasimir Georgiev | 32eaa86 | 2017-03-01 15:35:39 +0000 | [diff] [blame] | 780 | Style.FixNamespaceComments = false; |
Alexander Kornienko | fe7a57f | 2013-12-10 15:42:15 +0000 | [diff] [blame] | 781 | Style.SpaceBeforeParens = FormatStyle::SBPO_Always; |
Chandler Carruth | f8b7266 | 2014-03-02 12:37:31 +0000 | [diff] [blame] | 782 | Style.Standard = FormatStyle::LS_Cpp03; |
Alexander Kornienko | fe7a57f | 2013-12-10 15:42:15 +0000 | [diff] [blame] | 783 | return Style; |
| 784 | } |
| 785 | |
Daniel Jasper | c64b09a | 2014-05-22 15:12:22 +0000 | [diff] [blame] | 786 | FormatStyle getNoStyle() { |
| 787 | FormatStyle NoStyle = getLLVMStyle(); |
| 788 | NoStyle.DisableFormat = true; |
Daniel Jasper | da44677 | 2015-11-16 12:38:56 +0000 | [diff] [blame] | 789 | NoStyle.SortIncludes = false; |
Krasimir Georgiev | ac16a20 | 2017-06-23 11:46:03 +0000 | [diff] [blame] | 790 | NoStyle.SortUsingDeclarations = false; |
Daniel Jasper | c64b09a | 2014-05-22 15:12:22 +0000 | [diff] [blame] | 791 | return NoStyle; |
| 792 | } |
| 793 | |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 794 | bool getPredefinedStyle(StringRef Name, FormatStyle::LanguageKind Language, |
| 795 | FormatStyle *Style) { |
| 796 | if (Name.equals_lower("llvm")) { |
Alexander Kornienko | 006b5c8 | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 797 | *Style = getLLVMStyle(); |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 798 | } else if (Name.equals_lower("chromium")) { |
Nico Weber | 514ecc8 | 2014-02-02 20:50:45 +0000 | [diff] [blame] | 799 | *Style = getChromiumStyle(Language); |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 800 | } else if (Name.equals_lower("mozilla")) { |
Alexander Kornienko | 006b5c8 | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 801 | *Style = getMozillaStyle(); |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 802 | } else if (Name.equals_lower("google")) { |
Nico Weber | 514ecc8 | 2014-02-02 20:50:45 +0000 | [diff] [blame] | 803 | *Style = getGoogleStyle(Language); |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 804 | } else if (Name.equals_lower("webkit")) { |
Daniel Jasper | ffefb3d | 2013-07-24 13:10:59 +0000 | [diff] [blame] | 805 | *Style = getWebKitStyle(); |
Alexander Kornienko | fe7a57f | 2013-12-10 15:42:15 +0000 | [diff] [blame] | 806 | } else if (Name.equals_lower("gnu")) { |
| 807 | *Style = getGNUStyle(); |
Daniel Jasper | c64b09a | 2014-05-22 15:12:22 +0000 | [diff] [blame] | 808 | } else if (Name.equals_lower("none")) { |
| 809 | *Style = getNoStyle(); |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 810 | } else { |
Alexander Kornienko | 006b5c8 | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 811 | return false; |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 812 | } |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 813 | |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 814 | Style->Language = Language; |
Alexander Kornienko | 006b5c8 | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 815 | return true; |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Rafael Espindola | c080917 | 2014-06-12 14:02:15 +0000 | [diff] [blame] | 818 | std::error_code parseConfiguration(StringRef Text, FormatStyle *Style) { |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 819 | assert(Style); |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 820 | FormatStyle::LanguageKind Language = Style->Language; |
| 821 | assert(Language != FormatStyle::LK_None); |
Alexander Kornienko | 06e0033 | 2013-05-20 15:18:01 +0000 | [diff] [blame] | 822 | if (Text.trim().empty()) |
Rafael Espindola | d013670 | 2014-06-12 02:50:04 +0000 | [diff] [blame] | 823 | return make_error_code(ParseError::Error); |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 824 | |
| 825 | std::vector<FormatStyle> Styles; |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 826 | llvm::yaml::Input Input(Text); |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 827 | // DocumentListTraits<vector<FormatStyle>> uses the context to get default |
| 828 | // values for the fields, keys for which are missing from the configuration. |
| 829 | // Mapping also uses the context to get the language to find the correct |
| 830 | // base style. |
| 831 | Input.setContext(Style); |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 832 | Input >> Styles; |
| 833 | if (Input.error()) |
| 834 | return Input.error(); |
| 835 | |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 836 | for (unsigned i = 0; i < Styles.size(); ++i) { |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 837 | // Ensures that only the first configuration can skip the Language option. |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 838 | if (Styles[i].Language == FormatStyle::LK_None && i != 0) |
Rafael Espindola | d013670 | 2014-06-12 02:50:04 +0000 | [diff] [blame] | 839 | return make_error_code(ParseError::Error); |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 840 | // Ensure that each language is configured at most once. |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 841 | for (unsigned j = 0; j < i; ++j) { |
| 842 | if (Styles[i].Language == Styles[j].Language) { |
| 843 | DEBUG(llvm::dbgs() |
| 844 | << "Duplicate languages in the config file on positions " << j |
| 845 | << " and " << i << "\n"); |
Rafael Espindola | d013670 | 2014-06-12 02:50:04 +0000 | [diff] [blame] | 846 | return make_error_code(ParseError::Error); |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 847 | } |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 848 | } |
| 849 | } |
| 850 | // Look for a suitable configuration starting from the end, so we can |
| 851 | // find the configuration for the specific language first, and the default |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 852 | // configuration (which can only be at slot 0) after it. |
| 853 | for (int i = Styles.size() - 1; i >= 0; --i) { |
| 854 | if (Styles[i].Language == Language || |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 855 | Styles[i].Language == FormatStyle::LK_None) { |
| 856 | *Style = Styles[i]; |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 857 | Style->Language = Language; |
Rafael Espindola | d013670 | 2014-06-12 02:50:04 +0000 | [diff] [blame] | 858 | return make_error_code(ParseError::Success); |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 859 | } |
| 860 | } |
Rafael Espindola | d013670 | 2014-06-12 02:50:04 +0000 | [diff] [blame] | 861 | return make_error_code(ParseError::Unsuitable); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | std::string configurationAsText(const FormatStyle &Style) { |
| 865 | std::string Text; |
| 866 | llvm::raw_string_ostream Stream(Text); |
| 867 | llvm::yaml::Output Output(Stream); |
| 868 | // We use the same mapping method for input and output, so we need a non-const |
| 869 | // reference here. |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 870 | FormatStyle NonConstStyle = expandPresets(Style); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 871 | Output << NonConstStyle; |
Alexander Kornienko | 9a38ec2 | 2013-05-13 12:56:35 +0000 | [diff] [blame] | 872 | return Stream.str(); |
Alexander Kornienko | d653833 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 873 | } |
| 874 | |
Craig Topper | af35e85 | 2013-06-30 22:29:28 +0000 | [diff] [blame] | 875 | namespace { |
| 876 | |
Daniel Jasper | 496c199 | 2016-09-07 22:48:53 +0000 | [diff] [blame] | 877 | class JavaScriptRequoter : public TokenAnalyzer { |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 878 | public: |
Daniel Jasper | 496c199 | 2016-09-07 22:48:53 +0000 | [diff] [blame] | 879 | JavaScriptRequoter(const Environment &Env, const FormatStyle &Style) |
| 880 | : TokenAnalyzer(Env, Style) {} |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 881 | |
| 882 | tooling::Replacements |
| 883 | analyze(TokenAnnotator &Annotator, |
| 884 | SmallVectorImpl<AnnotatedLine *> &AnnotatedLines, |
Martin Probst | a9855af | 2016-09-02 14:29:48 +0000 | [diff] [blame] | 885 | FormatTokenLexer &Tokens) override { |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 886 | AffectedRangeMgr.computeAffectedLines(AnnotatedLines.begin(), |
| 887 | AnnotatedLines.end()); |
Daniel Jasper | 496c199 | 2016-09-07 22:48:53 +0000 | [diff] [blame] | 888 | tooling::Replacements Result; |
| 889 | requoteJSStringLiteral(AnnotatedLines, Result); |
| 890 | return Result; |
Alexander Kornienko | 62b85b9 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | private: |
Daniel Jasper | 496c199 | 2016-09-07 22:48:53 +0000 | [diff] [blame] | 894 | // Replaces double/single-quoted string literal as appropriate, re-escaping |
| 895 | // the contents in the process. |
Daniel Jasper | 9743992 | 2016-03-17 13:03:41 +0000 | [diff] [blame] | 896 | void requoteJSStringLiteral(SmallVectorImpl<AnnotatedLine *> &Lines, |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 897 | tooling::Replacements &Result) { |
Daniel Jasper | 9743992 | 2016-03-17 13:03:41 +0000 | [diff] [blame] | 898 | for (AnnotatedLine *Line : Lines) { |
| 899 | requoteJSStringLiteral(Line->Children, Result); |
| 900 | if (!Line->Affected) |
| 901 | continue; |
| 902 | for (FormatToken *FormatTok = Line->First; FormatTok; |
| 903 | FormatTok = FormatTok->Next) { |
| 904 | StringRef Input = FormatTok->TokenText; |
Martin Probst | a166979 | 2016-05-12 11:20:32 +0000 | [diff] [blame] | 905 | if (FormatTok->Finalized || !FormatTok->isStringLiteral() || |
Daniel Jasper | 9743992 | 2016-03-17 13:03:41 +0000 | [diff] [blame] | 906 | // NB: testing for not starting with a double quote to avoid |
Daniel Jasper | 496c199 | 2016-09-07 22:48:53 +0000 | [diff] [blame] | 907 | // breaking `template strings`. |
Eric Liu | 635423e | 2016-04-28 07:52:03 +0000 | [diff] [blame] | 908 | (Style.JavaScriptQuotes == FormatStyle::JSQS_Single && |
Daniel Jasper | 9743992 | 2016-03-17 13:03:41 +0000 | [diff] [blame] | 909 | !Input.startswith("\"")) || |
Eric Liu | 635423e | 2016-04-28 07:52:03 +0000 | [diff] [blame] | 910 | (Style.JavaScriptQuotes == FormatStyle::JSQS_Double && |
Daniel Jasper | 9743992 | 2016-03-17 13:03:41 +0000 | [diff] [blame] | 911 | !Input.startswith("\'"))) |
| 912 | continue; |
| 913 | |
| 914 | // Change start and end quote. |
Eric Liu | 635423e | 2016-04-28 07:52:03 +0000 | [diff] [blame] | 915 | bool IsSingle = Style.JavaScriptQuotes == FormatStyle::JSQS_Single; |
Daniel Jasper | 9743992 | 2016-03-17 13:03:41 +0000 | [diff] [blame] | 916 | SourceLocation Start = FormatTok->Tok.getLocation(); |
| 917 | auto Replace = [&](SourceLocation Start, unsigned Length, |
| 918 | StringRef ReplacementText) { |
Eric Liu | 40ef2fb | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 919 | auto Err = Result.add(tooling::Replacement( |
| 920 | Env.getSourceManager(), Start, Length, ReplacementText)); |
| 921 | // FIXME: handle error. For now, print error message and skip the |
| 922 | // replacement for release version. |
Piotr Padlewski | 1ec383c | 2016-12-23 11:40:44 +0000 | [diff] [blame] | 923 | if (Err) { |
Eric Liu | 40ef2fb | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 924 | llvm::errs() << llvm::toString(std::move(Err)) << "\n"; |
Piotr Padlewski | 1ec383c | 2016-12-23 11:40:44 +0000 | [diff] [blame] | 925 | assert(false); |
| 926 | } |
Daniel Jasper | 9743992 | 2016-03-17 13:03:41 +0000 | [diff] [blame] | 927 | }; |
| 928 | Replace(Start, 1, IsSingle ? "'" : "\""); |
| 929 | Replace(FormatTok->Tok.getEndLoc().getLocWithOffset(-1), 1, |
| 930 | IsSingle ? "'" : "\""); |
| 931 | |
| 932 | // Escape internal quotes. |
Daniel Jasper | 9743992 | 2016-03-17 13:03:41 +0000 | [diff] [blame] | 933 | bool Escaped = false; |
| 934 | for (size_t i = 1; i < Input.size() - 1; i++) { |
| 935 | switch (Input[i]) { |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 936 | case '\\': |
| 937 | if (!Escaped && i + 1 < Input.size() && |
| 938 | ((IsSingle && Input[i + 1] == '"') || |
| 939 | (!IsSingle && Input[i + 1] == '\''))) { |
| 940 | // Remove this \, it's escaping a " or ' that no longer needs |
| 941 | // escaping |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 942 | Replace(Start.getLocWithOffset(i), 1, ""); |
| 943 | continue; |
| 944 | } |
| 945 | Escaped = !Escaped; |
| 946 | break; |
| 947 | case '\"': |
| 948 | case '\'': |
| 949 | if (!Escaped && IsSingle == (Input[i] == '\'')) { |
| 950 | // Escape the quote. |
| 951 | Replace(Start.getLocWithOffset(i), 0, "\\"); |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 952 | } |
| 953 | Escaped = false; |
| 954 | break; |
| 955 | default: |
| 956 | Escaped = false; |
| 957 | break; |
Daniel Jasper | 9743992 | 2016-03-17 13:03:41 +0000 | [diff] [blame] | 958 | } |
| 959 | } |
Daniel Jasper | 9743992 | 2016-03-17 13:03:41 +0000 | [diff] [blame] | 960 | } |
| 961 | } |
| 962 | } |
Daniel Jasper | 496c199 | 2016-09-07 22:48:53 +0000 | [diff] [blame] | 963 | }; |
| 964 | |
| 965 | class Formatter : public TokenAnalyzer { |
| 966 | public: |
| 967 | Formatter(const Environment &Env, const FormatStyle &Style, |
Krasimir Georgiev | bcda54b | 2017-04-21 14:35:20 +0000 | [diff] [blame] | 968 | FormattingAttemptStatus *Status) |
| 969 | : TokenAnalyzer(Env, Style), Status(Status) {} |
Daniel Jasper | 496c199 | 2016-09-07 22:48:53 +0000 | [diff] [blame] | 970 | |
| 971 | tooling::Replacements |
| 972 | analyze(TokenAnnotator &Annotator, |
| 973 | SmallVectorImpl<AnnotatedLine *> &AnnotatedLines, |
| 974 | FormatTokenLexer &Tokens) override { |
| 975 | tooling::Replacements Result; |
| 976 | deriveLocalStyle(AnnotatedLines); |
| 977 | AffectedRangeMgr.computeAffectedLines(AnnotatedLines.begin(), |
| 978 | AnnotatedLines.end()); |
| 979 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
| 980 | Annotator.calculateFormattingInformation(*AnnotatedLines[i]); |
| 981 | } |
| 982 | Annotator.setCommentLineLevels(AnnotatedLines); |
| 983 | |
| 984 | WhitespaceManager Whitespaces( |
| 985 | Env.getSourceManager(), Style, |
| 986 | inputUsesCRLF(Env.getSourceManager().getBufferData(Env.getFileID()))); |
| 987 | ContinuationIndenter Indenter(Style, Tokens.getKeywords(), |
| 988 | Env.getSourceManager(), Whitespaces, Encoding, |
| 989 | BinPackInconclusiveFunctions); |
| 990 | UnwrappedLineFormatter(&Indenter, &Whitespaces, Style, Tokens.getKeywords(), |
Krasimir Georgiev | bcda54b | 2017-04-21 14:35:20 +0000 | [diff] [blame] | 991 | Env.getSourceManager(), Status) |
Daniel Jasper | 496c199 | 2016-09-07 22:48:53 +0000 | [diff] [blame] | 992 | .format(AnnotatedLines); |
| 993 | for (const auto &R : Whitespaces.generateReplacements()) |
| 994 | if (Result.add(R)) |
| 995 | return Result; |
| 996 | return Result; |
| 997 | } |
| 998 | |
| 999 | private: |
Daniel Jasper | 9743992 | 2016-03-17 13:03:41 +0000 | [diff] [blame] | 1000 | |
Alexander Kornienko | 9e649af | 2013-09-11 12:25:57 +0000 | [diff] [blame] | 1001 | static bool inputUsesCRLF(StringRef Text) { |
| 1002 | return Text.count('\r') * 2 > Text.count('\n'); |
| 1003 | } |
| 1004 | |
Daniel Jasper | 352f0df | 2015-07-18 16:35:30 +0000 | [diff] [blame] | 1005 | bool |
| 1006 | hasCpp03IncompatibleFormat(const SmallVectorImpl<AnnotatedLine *> &Lines) { |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 1007 | for (const AnnotatedLine *Line : Lines) { |
Daniel Jasper | 352f0df | 2015-07-18 16:35:30 +0000 | [diff] [blame] | 1008 | if (hasCpp03IncompatibleFormat(Line->Children)) |
| 1009 | return true; |
| 1010 | for (FormatToken *Tok = Line->First->Next; Tok; Tok = Tok->Next) { |
| 1011 | if (Tok->WhitespaceRange.getBegin() == Tok->WhitespaceRange.getEnd()) { |
| 1012 | if (Tok->is(tok::coloncolon) && Tok->Previous->is(TT_TemplateOpener)) |
| 1013 | return true; |
| 1014 | if (Tok->is(TT_TemplateCloser) && |
| 1015 | Tok->Previous->is(TT_TemplateCloser)) |
| 1016 | return true; |
| 1017 | } |
| 1018 | } |
| 1019 | } |
| 1020 | return false; |
| 1021 | } |
| 1022 | |
| 1023 | int countVariableAlignments(const SmallVectorImpl<AnnotatedLine *> &Lines) { |
| 1024 | int AlignmentDiff = 0; |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 1025 | for (const AnnotatedLine *Line : Lines) { |
Daniel Jasper | 352f0df | 2015-07-18 16:35:30 +0000 | [diff] [blame] | 1026 | AlignmentDiff += countVariableAlignments(Line->Children); |
| 1027 | for (FormatToken *Tok = Line->First; Tok && Tok->Next; Tok = Tok->Next) { |
| 1028 | if (!Tok->is(TT_PointerOrReference)) |
| 1029 | continue; |
| 1030 | bool SpaceBefore = |
| 1031 | Tok->WhitespaceRange.getBegin() != Tok->WhitespaceRange.getEnd(); |
| 1032 | bool SpaceAfter = Tok->Next->WhitespaceRange.getBegin() != |
| 1033 | Tok->Next->WhitespaceRange.getEnd(); |
| 1034 | if (SpaceBefore && !SpaceAfter) |
| 1035 | ++AlignmentDiff; |
| 1036 | if (!SpaceBefore && SpaceAfter) |
| 1037 | --AlignmentDiff; |
| 1038 | } |
| 1039 | } |
| 1040 | return AlignmentDiff; |
| 1041 | } |
| 1042 | |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 1043 | void |
| 1044 | deriveLocalStyle(const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) { |
Daniel Jasper | b10cbc4 | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 1045 | bool HasBinPackedFunction = false; |
| 1046 | bool HasOnePerLineFunction = false; |
Daniel Jasper | 7fce3ab | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1047 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1048 | if (!AnnotatedLines[i]->First->Next) |
Daniel Jasper | 7fce3ab | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1049 | continue; |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1050 | FormatToken *Tok = AnnotatedLines[i]->First->Next; |
Manuel Klimek | 6e6310e | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1051 | while (Tok->Next) { |
Daniel Jasper | b10cbc4 | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 1052 | if (Tok->PackingKind == PPK_BinPacked) |
| 1053 | HasBinPackedFunction = true; |
| 1054 | if (Tok->PackingKind == PPK_OnePerLine) |
| 1055 | HasOnePerLineFunction = true; |
| 1056 | |
Manuel Klimek | 6e6310e | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1057 | Tok = Tok->Next; |
Daniel Jasper | 7fce3ab | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1058 | } |
| 1059 | } |
Eric Liu | 635423e | 2016-04-28 07:52:03 +0000 | [diff] [blame] | 1060 | if (Style.DerivePointerAlignment) |
| 1061 | Style.PointerAlignment = countVariableAlignments(AnnotatedLines) <= 0 |
| 1062 | ? FormatStyle::PAS_Left |
| 1063 | : FormatStyle::PAS_Right; |
| 1064 | if (Style.Standard == FormatStyle::LS_Auto) |
| 1065 | Style.Standard = hasCpp03IncompatibleFormat(AnnotatedLines) |
| 1066 | ? FormatStyle::LS_Cpp11 |
| 1067 | : FormatStyle::LS_Cpp03; |
Daniel Jasper | b10cbc4 | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 1068 | BinPackInconclusiveFunctions = |
| 1069 | HasBinPackedFunction || !HasOnePerLineFunction; |
Daniel Jasper | 7fce3ab | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
Daniel Jasper | b10cbc4 | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 1072 | bool BinPackInconclusiveFunctions; |
Krasimir Georgiev | bcda54b | 2017-04-21 14:35:20 +0000 | [diff] [blame] | 1073 | FormattingAttemptStatus *Status; |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 1074 | }; |
| 1075 | |
| 1076 | // This class clean up the erroneous/redundant code around the given ranges in |
| 1077 | // file. |
| 1078 | class Cleaner : public TokenAnalyzer { |
| 1079 | public: |
Eric Liu | 635423e | 2016-04-28 07:52:03 +0000 | [diff] [blame] | 1080 | Cleaner(const Environment &Env, const FormatStyle &Style) |
| 1081 | : TokenAnalyzer(Env, Style), |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 1082 | DeletedTokens(FormatTokenLess(Env.getSourceManager())) {} |
| 1083 | |
| 1084 | // FIXME: eliminate unused parameters. |
| 1085 | tooling::Replacements |
| 1086 | analyze(TokenAnnotator &Annotator, |
| 1087 | SmallVectorImpl<AnnotatedLine *> &AnnotatedLines, |
Martin Probst | a9855af | 2016-09-02 14:29:48 +0000 | [diff] [blame] | 1088 | FormatTokenLexer &Tokens) override { |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 1089 | // FIXME: in the current implementation the granularity of affected range |
| 1090 | // is an annotated line. However, this is not sufficient. Furthermore, |
| 1091 | // redundant code introduced by replacements does not necessarily |
| 1092 | // intercept with ranges of replacements that result in the redundancy. |
| 1093 | // To determine if some redundant code is actually introduced by |
| 1094 | // replacements(e.g. deletions), we need to come up with a more |
| 1095 | // sophisticated way of computing affected ranges. |
| 1096 | AffectedRangeMgr.computeAffectedLines(AnnotatedLines.begin(), |
| 1097 | AnnotatedLines.end()); |
| 1098 | |
| 1099 | checkEmptyNamespace(AnnotatedLines); |
| 1100 | |
Eric Liu | ce5e4bc | 2016-05-18 08:02:56 +0000 | [diff] [blame] | 1101 | for (auto &Line : AnnotatedLines) { |
| 1102 | if (Line->Affected) { |
| 1103 | cleanupRight(Line->First, tok::comma, tok::comma); |
| 1104 | cleanupRight(Line->First, TT_CtorInitializerColon, tok::comma); |
Eric Liu | 2574d15 | 2016-09-13 15:02:43 +0000 | [diff] [blame] | 1105 | cleanupRight(Line->First, tok::l_paren, tok::comma); |
| 1106 | cleanupLeft(Line->First, tok::comma, tok::r_paren); |
Eric Liu | ce5e4bc | 2016-05-18 08:02:56 +0000 | [diff] [blame] | 1107 | cleanupLeft(Line->First, TT_CtorInitializerComma, tok::l_brace); |
| 1108 | cleanupLeft(Line->First, TT_CtorInitializerColon, tok::l_brace); |
Malcolm Parsons | 5d8cdb8 | 2016-10-20 14:58:45 +0000 | [diff] [blame] | 1109 | cleanupLeft(Line->First, TT_CtorInitializerColon, tok::equal); |
Eric Liu | ce5e4bc | 2016-05-18 08:02:56 +0000 | [diff] [blame] | 1110 | } |
| 1111 | } |
| 1112 | |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 1113 | return generateFixes(); |
| 1114 | } |
| 1115 | |
| 1116 | private: |
| 1117 | bool containsOnlyComments(const AnnotatedLine &Line) { |
| 1118 | for (FormatToken *Tok = Line.First; Tok != nullptr; Tok = Tok->Next) { |
| 1119 | if (Tok->isNot(tok::comment)) |
| 1120 | return false; |
| 1121 | } |
| 1122 | return true; |
| 1123 | } |
| 1124 | |
| 1125 | // Iterate through all lines and remove any empty (nested) namespaces. |
| 1126 | void checkEmptyNamespace(SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) { |
Eric Liu | 7956c40 | 2016-10-05 15:49:01 +0000 | [diff] [blame] | 1127 | std::set<unsigned> DeletedLines; |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 1128 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
| 1129 | auto &Line = *AnnotatedLines[i]; |
| 1130 | if (Line.startsWith(tok::kw_namespace) || |
| 1131 | Line.startsWith(tok::kw_inline, tok::kw_namespace)) { |
Eric Liu | 7956c40 | 2016-10-05 15:49:01 +0000 | [diff] [blame] | 1132 | checkEmptyNamespace(AnnotatedLines, i, i, DeletedLines); |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | for (auto Line : DeletedLines) { |
| 1137 | FormatToken *Tok = AnnotatedLines[Line]->First; |
| 1138 | while (Tok) { |
| 1139 | deleteToken(Tok); |
| 1140 | Tok = Tok->Next; |
| 1141 | } |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | // The function checks if the namespace, which starts from \p CurrentLine, and |
| 1146 | // its nested namespaces are empty and delete them if they are empty. It also |
| 1147 | // sets \p NewLine to the last line checked. |
| 1148 | // Returns true if the current namespace is empty. |
| 1149 | bool checkEmptyNamespace(SmallVectorImpl<AnnotatedLine *> &AnnotatedLines, |
Eric Liu | 7956c40 | 2016-10-05 15:49:01 +0000 | [diff] [blame] | 1150 | unsigned CurrentLine, unsigned &NewLine, |
| 1151 | std::set<unsigned> &DeletedLines) { |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 1152 | unsigned InitLine = CurrentLine, End = AnnotatedLines.size(); |
Eric Liu | 635423e | 2016-04-28 07:52:03 +0000 | [diff] [blame] | 1153 | if (Style.BraceWrapping.AfterNamespace) { |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 1154 | // If the left brace is in a new line, we should consume it first so that |
| 1155 | // it does not make the namespace non-empty. |
| 1156 | // FIXME: error handling if there is no left brace. |
| 1157 | if (!AnnotatedLines[++CurrentLine]->startsWith(tok::l_brace)) { |
| 1158 | NewLine = CurrentLine; |
| 1159 | return false; |
| 1160 | } |
| 1161 | } else if (!AnnotatedLines[CurrentLine]->endsWith(tok::l_brace)) { |
| 1162 | return false; |
| 1163 | } |
| 1164 | while (++CurrentLine < End) { |
| 1165 | if (AnnotatedLines[CurrentLine]->startsWith(tok::r_brace)) |
| 1166 | break; |
| 1167 | |
| 1168 | if (AnnotatedLines[CurrentLine]->startsWith(tok::kw_namespace) || |
| 1169 | AnnotatedLines[CurrentLine]->startsWith(tok::kw_inline, |
| 1170 | tok::kw_namespace)) { |
Eric Liu | 7956c40 | 2016-10-05 15:49:01 +0000 | [diff] [blame] | 1171 | if (!checkEmptyNamespace(AnnotatedLines, CurrentLine, NewLine, |
| 1172 | DeletedLines)) |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 1173 | return false; |
| 1174 | CurrentLine = NewLine; |
| 1175 | continue; |
| 1176 | } |
| 1177 | |
| 1178 | if (containsOnlyComments(*AnnotatedLines[CurrentLine])) |
| 1179 | continue; |
| 1180 | |
| 1181 | // If there is anything other than comments or nested namespaces in the |
| 1182 | // current namespace, the namespace cannot be empty. |
| 1183 | NewLine = CurrentLine; |
| 1184 | return false; |
| 1185 | } |
| 1186 | |
| 1187 | NewLine = CurrentLine; |
| 1188 | if (CurrentLine >= End) |
| 1189 | return false; |
| 1190 | |
| 1191 | // Check if the empty namespace is actually affected by changed ranges. |
| 1192 | if (!AffectedRangeMgr.affectsCharSourceRange(CharSourceRange::getCharRange( |
| 1193 | AnnotatedLines[InitLine]->First->Tok.getLocation(), |
| 1194 | AnnotatedLines[CurrentLine]->Last->Tok.getEndLoc()))) |
| 1195 | return false; |
| 1196 | |
| 1197 | for (unsigned i = InitLine; i <= CurrentLine; ++i) { |
| 1198 | DeletedLines.insert(i); |
| 1199 | } |
| 1200 | |
| 1201 | return true; |
| 1202 | } |
| 1203 | |
Eric Liu | ce5e4bc | 2016-05-18 08:02:56 +0000 | [diff] [blame] | 1204 | // Checks pairs {start, start->next},..., {end->previous, end} and deletes one |
| 1205 | // of the token in the pair if the left token has \p LK token kind and the |
| 1206 | // right token has \p RK token kind. If \p DeleteLeft is true, the left token |
| 1207 | // is deleted on match; otherwise, the right token is deleted. |
| 1208 | template <typename LeftKind, typename RightKind> |
| 1209 | void cleanupPair(FormatToken *Start, LeftKind LK, RightKind RK, |
| 1210 | bool DeleteLeft) { |
| 1211 | auto NextNotDeleted = [this](const FormatToken &Tok) -> FormatToken * { |
| 1212 | for (auto *Res = Tok.Next; Res; Res = Res->Next) |
| 1213 | if (!Res->is(tok::comment) && |
| 1214 | DeletedTokens.find(Res) == DeletedTokens.end()) |
| 1215 | return Res; |
| 1216 | return nullptr; |
| 1217 | }; |
| 1218 | for (auto *Left = Start; Left;) { |
| 1219 | auto *Right = NextNotDeleted(*Left); |
| 1220 | if (!Right) |
| 1221 | break; |
| 1222 | if (Left->is(LK) && Right->is(RK)) { |
| 1223 | deleteToken(DeleteLeft ? Left : Right); |
Eric Liu | 01426ff | 2016-09-09 17:50:49 +0000 | [diff] [blame] | 1224 | for (auto *Tok = Left->Next; Tok && Tok != Right; Tok = Tok->Next) |
| 1225 | deleteToken(Tok); |
Eric Liu | ce5e4bc | 2016-05-18 08:02:56 +0000 | [diff] [blame] | 1226 | // If the right token is deleted, we should keep the left token |
| 1227 | // unchanged and pair it with the new right token. |
| 1228 | if (!DeleteLeft) |
| 1229 | continue; |
| 1230 | } |
| 1231 | Left = Right; |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | template <typename LeftKind, typename RightKind> |
| 1236 | void cleanupLeft(FormatToken *Start, LeftKind LK, RightKind RK) { |
| 1237 | cleanupPair(Start, LK, RK, /*DeleteLeft=*/true); |
| 1238 | } |
| 1239 | |
| 1240 | template <typename LeftKind, typename RightKind> |
| 1241 | void cleanupRight(FormatToken *Start, LeftKind LK, RightKind RK) { |
| 1242 | cleanupPair(Start, LK, RK, /*DeleteLeft=*/false); |
| 1243 | } |
| 1244 | |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 1245 | // Delete the given token. |
| 1246 | inline void deleteToken(FormatToken *Tok) { |
| 1247 | if (Tok) |
| 1248 | DeletedTokens.insert(Tok); |
| 1249 | } |
| 1250 | |
| 1251 | tooling::Replacements generateFixes() { |
| 1252 | tooling::Replacements Fixes; |
| 1253 | std::vector<FormatToken *> Tokens; |
| 1254 | std::copy(DeletedTokens.begin(), DeletedTokens.end(), |
| 1255 | std::back_inserter(Tokens)); |
| 1256 | |
| 1257 | // Merge multiple continuous token deletions into one big deletion so that |
| 1258 | // the number of replacements can be reduced. This makes computing affected |
| 1259 | // ranges more efficient when we run reformat on the changed code. |
| 1260 | unsigned Idx = 0; |
| 1261 | while (Idx < Tokens.size()) { |
| 1262 | unsigned St = Idx, End = Idx; |
| 1263 | while ((End + 1) < Tokens.size() && |
| 1264 | Tokens[End]->Next == Tokens[End + 1]) { |
| 1265 | End++; |
| 1266 | } |
| 1267 | auto SR = CharSourceRange::getCharRange(Tokens[St]->Tok.getLocation(), |
| 1268 | Tokens[End]->Tok.getEndLoc()); |
Eric Liu | 40ef2fb | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 1269 | auto Err = |
| 1270 | Fixes.add(tooling::Replacement(Env.getSourceManager(), SR, "")); |
| 1271 | // FIXME: better error handling. for now just print error message and skip |
| 1272 | // for the release version. |
Piotr Padlewski | 1ec383c | 2016-12-23 11:40:44 +0000 | [diff] [blame] | 1273 | if (Err) { |
Eric Liu | 40ef2fb | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 1274 | llvm::errs() << llvm::toString(std::move(Err)) << "\n"; |
Piotr Padlewski | 1ec383c | 2016-12-23 11:40:44 +0000 | [diff] [blame] | 1275 | assert(false && "Fixes must not conflict!"); |
| 1276 | } |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 1277 | Idx = End + 1; |
| 1278 | } |
| 1279 | |
| 1280 | return Fixes; |
| 1281 | } |
| 1282 | |
| 1283 | // Class for less-than inequality comparason for the set `RedundantTokens`. |
| 1284 | // We store tokens in the order they appear in the translation unit so that |
| 1285 | // we do not need to sort them in `generateFixes()`. |
| 1286 | struct FormatTokenLess { |
Eric Liu | 635423e | 2016-04-28 07:52:03 +0000 | [diff] [blame] | 1287 | FormatTokenLess(const SourceManager &SM) : SM(SM) {} |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 1288 | |
Eric Liu | 2874ac3 | 2016-05-18 08:14:49 +0000 | [diff] [blame] | 1289 | bool operator()(const FormatToken *LHS, const FormatToken *RHS) const { |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 1290 | return SM.isBeforeInTranslationUnit(LHS->Tok.getLocation(), |
| 1291 | RHS->Tok.getLocation()); |
| 1292 | } |
Eric Liu | 635423e | 2016-04-28 07:52:03 +0000 | [diff] [blame] | 1293 | const SourceManager &SM; |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 1294 | }; |
| 1295 | |
| 1296 | // Tokens to be deleted. |
| 1297 | std::set<FormatToken *, FormatTokenLess> DeletedTokens; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1298 | }; |
| 1299 | |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 1300 | struct IncludeDirective { |
| 1301 | StringRef Filename; |
| 1302 | StringRef Text; |
| 1303 | unsigned Offset; |
Daniel Jasper | d2629dc | 2015-12-16 10:10:16 +0000 | [diff] [blame] | 1304 | int Category; |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 1305 | }; |
| 1306 | |
Craig Topper | af35e85 | 2013-06-30 22:29:28 +0000 | [diff] [blame] | 1307 | } // end anonymous namespace |
| 1308 | |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 1309 | // Determines whether 'Ranges' intersects with ('Start', 'End'). |
| 1310 | static bool affectsRange(ArrayRef<tooling::Range> Ranges, unsigned Start, |
| 1311 | unsigned End) { |
| 1312 | for (auto Range : Ranges) { |
| 1313 | if (Range.getOffset() < End && |
| 1314 | Range.getOffset() + Range.getLength() > Start) |
| 1315 | return true; |
| 1316 | } |
| 1317 | return false; |
| 1318 | } |
| 1319 | |
Eric Liu | a992afe | 2016-08-10 09:32:23 +0000 | [diff] [blame] | 1320 | // Returns a pair (Index, OffsetToEOL) describing the position of the cursor |
| 1321 | // before sorting/deduplicating. Index is the index of the include under the |
| 1322 | // cursor in the original set of includes. If this include has duplicates, it is |
| 1323 | // the index of the first of the duplicates as the others are going to be |
| 1324 | // removed. OffsetToEOL describes the cursor's position relative to the end of |
| 1325 | // its current line. |
| 1326 | // If `Cursor` is not on any #include, `Index` will be UINT_MAX. |
| 1327 | static std::pair<unsigned, unsigned> |
| 1328 | FindCursorIndex(const SmallVectorImpl<IncludeDirective> &Includes, |
| 1329 | const SmallVectorImpl<unsigned> &Indices, unsigned Cursor) { |
| 1330 | unsigned CursorIndex = UINT_MAX; |
| 1331 | unsigned OffsetToEOL = 0; |
| 1332 | for (int i = 0, e = Includes.size(); i != e; ++i) { |
| 1333 | unsigned Start = Includes[Indices[i]].Offset; |
| 1334 | unsigned End = Start + Includes[Indices[i]].Text.size(); |
| 1335 | if (!(Cursor >= Start && Cursor < End)) |
| 1336 | continue; |
| 1337 | CursorIndex = Indices[i]; |
| 1338 | OffsetToEOL = End - Cursor; |
| 1339 | // Put the cursor on the only remaining #include among the duplicate |
| 1340 | // #includes. |
| 1341 | while (--i >= 0 && Includes[CursorIndex].Text == Includes[Indices[i]].Text) |
| 1342 | CursorIndex = i; |
| 1343 | break; |
| 1344 | } |
| 1345 | return std::make_pair(CursorIndex, OffsetToEOL); |
| 1346 | } |
| 1347 | |
| 1348 | // Sorts and deduplicate a block of includes given by 'Includes' alphabetically |
| 1349 | // adding the necessary replacement to 'Replaces'. 'Includes' must be in strict |
| 1350 | // source order. |
| 1351 | // #include directives with the same text will be deduplicated, and only the |
| 1352 | // first #include in the duplicate #includes remains. If the `Cursor` is |
| 1353 | // provided and put on a deleted #include, it will be moved to the remaining |
| 1354 | // #include in the duplicate #includes. |
Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 1355 | static void sortCppIncludes(const FormatStyle &Style, |
Eric Liu | a992afe | 2016-08-10 09:32:23 +0000 | [diff] [blame] | 1356 | const SmallVectorImpl<IncludeDirective> &Includes, |
| 1357 | ArrayRef<tooling::Range> Ranges, StringRef FileName, |
| 1358 | tooling::Replacements &Replaces, unsigned *Cursor) { |
| 1359 | unsigned IncludesBeginOffset = Includes.front().Offset; |
Daniel Jasper | d6a0078 | 2016-08-30 21:33:41 +0000 | [diff] [blame] | 1360 | unsigned IncludesEndOffset = |
| 1361 | Includes.back().Offset + Includes.back().Text.size(); |
| 1362 | unsigned IncludesBlockSize = IncludesEndOffset - IncludesBeginOffset; |
| 1363 | if (!affectsRange(Ranges, IncludesBeginOffset, IncludesEndOffset)) |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 1364 | return; |
| 1365 | SmallVector<unsigned, 16> Indices; |
| 1366 | for (unsigned i = 0, e = Includes.size(); i != e; ++i) |
| 1367 | Indices.push_back(i); |
Daniel Jasper | 94a96fc | 2016-03-03 17:34:14 +0000 | [diff] [blame] | 1368 | std::stable_sort( |
| 1369 | Indices.begin(), Indices.end(), [&](unsigned LHSI, unsigned RHSI) { |
| 1370 | return std::tie(Includes[LHSI].Category, Includes[LHSI].Filename) < |
| 1371 | std::tie(Includes[RHSI].Category, Includes[RHSI].Filename); |
| 1372 | }); |
Eric Liu | a992afe | 2016-08-10 09:32:23 +0000 | [diff] [blame] | 1373 | // The index of the include on which the cursor will be put after |
| 1374 | // sorting/deduplicating. |
| 1375 | unsigned CursorIndex; |
| 1376 | // The offset from cursor to the end of line. |
| 1377 | unsigned CursorToEOLOffset; |
| 1378 | if (Cursor) |
| 1379 | std::tie(CursorIndex, CursorToEOLOffset) = |
| 1380 | FindCursorIndex(Includes, Indices, *Cursor); |
| 1381 | |
| 1382 | // Deduplicate #includes. |
| 1383 | Indices.erase(std::unique(Indices.begin(), Indices.end(), |
| 1384 | [&](unsigned LHSI, unsigned RHSI) { |
| 1385 | return Includes[LHSI].Text == Includes[RHSI].Text; |
| 1386 | }), |
| 1387 | Indices.end()); |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 1388 | |
| 1389 | // If the #includes are out of order, we generate a single replacement fixing |
| 1390 | // the entire block. Otherwise, no replacement is generated. |
Eric Liu | a992afe | 2016-08-10 09:32:23 +0000 | [diff] [blame] | 1391 | if (Indices.size() == Includes.size() && |
| 1392 | std::is_sorted(Indices.begin(), Indices.end())) |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 1393 | return; |
| 1394 | |
Daniel Jasper | b68aabf | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 1395 | std::string result; |
Daniel Jasper | b68aabf | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 1396 | for (unsigned Index : Indices) { |
| 1397 | if (!result.empty()) |
| 1398 | result += "\n"; |
| 1399 | result += Includes[Index].Text; |
Eric Liu | a992afe | 2016-08-10 09:32:23 +0000 | [diff] [blame] | 1400 | if (Cursor && CursorIndex == Index) |
| 1401 | *Cursor = IncludesBeginOffset + result.size() - CursorToEOLOffset; |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
Eric Liu | 40ef2fb | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 1404 | auto Err = Replaces.add(tooling::Replacement( |
Eric Liu | a992afe | 2016-08-10 09:32:23 +0000 | [diff] [blame] | 1405 | FileName, Includes.front().Offset, IncludesBlockSize, result)); |
Eric Liu | 40ef2fb | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 1406 | // FIXME: better error handling. For now, just skip the replacement for the |
| 1407 | // release version. |
Piotr Padlewski | 1ec383c | 2016-12-23 11:40:44 +0000 | [diff] [blame] | 1408 | if (Err) { |
Eric Liu | 40ef2fb | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 1409 | llvm::errs() << llvm::toString(std::move(Err)) << "\n"; |
Piotr Padlewski | 1ec383c | 2016-12-23 11:40:44 +0000 | [diff] [blame] | 1410 | assert(false); |
| 1411 | } |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
Eric Liu | 659afd5 | 2016-05-31 13:34:20 +0000 | [diff] [blame] | 1414 | namespace { |
| 1415 | |
| 1416 | // This class manages priorities of #include categories and calculates |
| 1417 | // priorities for headers. |
| 1418 | class IncludeCategoryManager { |
| 1419 | public: |
| 1420 | IncludeCategoryManager(const FormatStyle &Style, StringRef FileName) |
| 1421 | : Style(Style), FileName(FileName) { |
| 1422 | FileStem = llvm::sys::path::stem(FileName); |
| 1423 | for (const auto &Category : Style.IncludeCategories) |
Chandler Carruth | d676ab1 | 2017-06-29 23:20:54 +0000 | [diff] [blame] | 1424 | CategoryRegexs.emplace_back(Category.Regex, llvm::Regex::IgnoreCase); |
Eric Liu | 659afd5 | 2016-05-31 13:34:20 +0000 | [diff] [blame] | 1425 | IsMainFile = FileName.endswith(".c") || FileName.endswith(".cc") || |
| 1426 | FileName.endswith(".cpp") || FileName.endswith(".c++") || |
| 1427 | FileName.endswith(".cxx") || FileName.endswith(".m") || |
| 1428 | FileName.endswith(".mm"); |
| 1429 | } |
| 1430 | |
| 1431 | // Returns the priority of the category which \p IncludeName belongs to. |
| 1432 | // If \p CheckMainHeader is true and \p IncludeName is a main header, returns |
| 1433 | // 0. Otherwise, returns the priority of the matching category or INT_MAX. |
| 1434 | int getIncludePriority(StringRef IncludeName, bool CheckMainHeader) { |
| 1435 | int Ret = INT_MAX; |
| 1436 | for (unsigned i = 0, e = CategoryRegexs.size(); i != e; ++i) |
| 1437 | if (CategoryRegexs[i].match(IncludeName)) { |
| 1438 | Ret = Style.IncludeCategories[i].Priority; |
| 1439 | break; |
| 1440 | } |
| 1441 | if (CheckMainHeader && IsMainFile && Ret > 0 && isMainHeader(IncludeName)) |
| 1442 | Ret = 0; |
| 1443 | return Ret; |
| 1444 | } |
| 1445 | |
| 1446 | private: |
| 1447 | bool isMainHeader(StringRef IncludeName) const { |
| 1448 | if (!IncludeName.startswith("\"")) |
| 1449 | return false; |
| 1450 | StringRef HeaderStem = |
| 1451 | llvm::sys::path::stem(IncludeName.drop_front(1).drop_back(1)); |
Chandler Carruth | d676ab1 | 2017-06-29 23:20:54 +0000 | [diff] [blame] | 1452 | if (FileStem.startswith(HeaderStem) || |
| 1453 | FileStem.startswith_lower(HeaderStem)) { |
Eric Liu | 659afd5 | 2016-05-31 13:34:20 +0000 | [diff] [blame] | 1454 | llvm::Regex MainIncludeRegex( |
Chandler Carruth | d676ab1 | 2017-06-29 23:20:54 +0000 | [diff] [blame] | 1455 | (HeaderStem + Style.IncludeIsMainRegex).str(), |
| 1456 | llvm::Regex::IgnoreCase); |
Eric Liu | 659afd5 | 2016-05-31 13:34:20 +0000 | [diff] [blame] | 1457 | if (MainIncludeRegex.match(FileStem)) |
| 1458 | return true; |
| 1459 | } |
| 1460 | return false; |
| 1461 | } |
| 1462 | |
| 1463 | const FormatStyle &Style; |
| 1464 | bool IsMainFile; |
| 1465 | StringRef FileName; |
| 1466 | StringRef FileStem; |
| 1467 | SmallVector<llvm::Regex, 4> CategoryRegexs; |
| 1468 | }; |
| 1469 | |
| 1470 | const char IncludeRegexPattern[] = |
| 1471 | R"(^[\t\ ]*#[\t\ ]*(import|include)[^"<]*(["<][^">]*[">]))"; |
| 1472 | |
| 1473 | } // anonymous namespace |
| 1474 | |
Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 1475 | tooling::Replacements sortCppIncludes(const FormatStyle &Style, StringRef Code, |
| 1476 | ArrayRef<tooling::Range> Ranges, |
| 1477 | StringRef FileName, |
| 1478 | tooling::Replacements &Replaces, |
| 1479 | unsigned *Cursor) { |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 1480 | unsigned Prev = 0; |
| 1481 | unsigned SearchFrom = 0; |
Eric Liu | 659afd5 | 2016-05-31 13:34:20 +0000 | [diff] [blame] | 1482 | llvm::Regex IncludeRegex(IncludeRegexPattern); |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 1483 | SmallVector<StringRef, 4> Matches; |
| 1484 | SmallVector<IncludeDirective, 16> IncludesInBlock; |
Daniel Jasper | 85c472d | 2015-09-29 07:53:08 +0000 | [diff] [blame] | 1485 | |
| 1486 | // In compiled files, consider the first #include to be the main #include of |
| 1487 | // the file if it is not a system #include. This ensures that the header |
| 1488 | // doesn't have hidden dependencies |
| 1489 | // (http://llvm.org/docs/CodingStandards.html#include-style). |
| 1490 | // |
| 1491 | // FIXME: Do some sanity checking, e.g. edit distance of the base name, to fix |
| 1492 | // cases where the first #include is unlikely to be the main header. |
Eric Liu | 659afd5 | 2016-05-31 13:34:20 +0000 | [diff] [blame] | 1493 | IncludeCategoryManager Categories(Style, FileName); |
Daniel Jasper | 32d75fa | 2015-12-21 13:40:49 +0000 | [diff] [blame] | 1494 | bool FirstIncludeBlock = true; |
Daniel Jasper | a252f5d | 2015-12-21 17:28:24 +0000 | [diff] [blame] | 1495 | bool MainIncludeFound = false; |
Daniel Jasper | 9b8c7c7 | 2015-11-21 09:17:08 +0000 | [diff] [blame] | 1496 | bool FormattingOff = false; |
| 1497 | |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 1498 | for (;;) { |
| 1499 | auto Pos = Code.find('\n', SearchFrom); |
| 1500 | StringRef Line = |
| 1501 | Code.substr(Prev, (Pos != StringRef::npos ? Pos : Code.size()) - Prev); |
Daniel Jasper | 9b8c7c7 | 2015-11-21 09:17:08 +0000 | [diff] [blame] | 1502 | |
| 1503 | StringRef Trimmed = Line.trim(); |
| 1504 | if (Trimmed == "// clang-format off") |
| 1505 | FormattingOff = true; |
| 1506 | else if (Trimmed == "// clang-format on") |
| 1507 | FormattingOff = false; |
| 1508 | |
| 1509 | if (!FormattingOff && !Line.endswith("\\")) { |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 1510 | if (IncludeRegex.match(Line, &Matches)) { |
Nico Weber | ff06370 | 2015-10-21 17:13:45 +0000 | [diff] [blame] | 1511 | StringRef IncludeName = Matches[2]; |
Eric Liu | 659afd5 | 2016-05-31 13:34:20 +0000 | [diff] [blame] | 1512 | int Category = Categories.getIncludePriority( |
| 1513 | IncludeName, |
| 1514 | /*CheckMainHeader=*/!MainIncludeFound && FirstIncludeBlock); |
| 1515 | if (Category == 0) |
| 1516 | MainIncludeFound = true; |
Nico Weber | ff06370 | 2015-10-21 17:13:45 +0000 | [diff] [blame] | 1517 | IncludesInBlock.push_back({IncludeName, Line, Prev, Category}); |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 1518 | } else if (!IncludesInBlock.empty()) { |
Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 1519 | sortCppIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces, |
| 1520 | Cursor); |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 1521 | IncludesInBlock.clear(); |
Daniel Jasper | 32d75fa | 2015-12-21 13:40:49 +0000 | [diff] [blame] | 1522 | FirstIncludeBlock = false; |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 1523 | } |
| 1524 | Prev = Pos + 1; |
| 1525 | } |
| 1526 | if (Pos == StringRef::npos || Pos + 1 == Code.size()) |
| 1527 | break; |
| 1528 | SearchFrom = Pos + 1; |
| 1529 | } |
| 1530 | if (!IncludesInBlock.empty()) |
Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 1531 | sortCppIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces, Cursor); |
| 1532 | return Replaces; |
| 1533 | } |
| 1534 | |
Martin Probst | fa37b18 | 2017-01-27 09:09:11 +0000 | [diff] [blame] | 1535 | bool isMpegTS(StringRef Code) { |
| 1536 | // MPEG transport streams use the ".ts" file extension. clang-format should |
| 1537 | // not attempt to format those. MPEG TS' frame format starts with 0x47 every |
| 1538 | // 189 bytes - detect that and return. |
| 1539 | return Code.size() > 188 && Code[0] == 0x47 && Code[188] == 0x47; |
| 1540 | } |
| 1541 | |
Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 1542 | tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code, |
| 1543 | ArrayRef<tooling::Range> Ranges, |
| 1544 | StringRef FileName, unsigned *Cursor) { |
| 1545 | tooling::Replacements Replaces; |
| 1546 | if (!Style.SortIncludes) |
| 1547 | return Replaces; |
Martin Probst | fa37b18 | 2017-01-27 09:09:11 +0000 | [diff] [blame] | 1548 | if (Style.Language == FormatStyle::LanguageKind::LK_JavaScript && |
| 1549 | isMpegTS(Code)) |
| 1550 | return Replaces; |
Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 1551 | if (Style.Language == FormatStyle::LanguageKind::LK_JavaScript) |
| 1552 | return sortJavaScriptImports(Style, Code, Ranges, FileName); |
| 1553 | sortCppIncludes(Style, Code, Ranges, FileName, Replaces, Cursor); |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 1554 | return Replaces; |
| 1555 | } |
| 1556 | |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 1557 | template <typename T> |
Eric Liu | 4f8d994 | 2016-07-11 13:53:12 +0000 | [diff] [blame] | 1558 | static llvm::Expected<tooling::Replacements> |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 1559 | processReplacements(T ProcessFunc, StringRef Code, |
| 1560 | const tooling::Replacements &Replaces, |
| 1561 | const FormatStyle &Style) { |
Manuel Klimek | b12e5a5 | 2016-03-01 12:37:30 +0000 | [diff] [blame] | 1562 | if (Replaces.empty()) |
| 1563 | return tooling::Replacements(); |
| 1564 | |
Eric Liu | 4f8d994 | 2016-07-11 13:53:12 +0000 | [diff] [blame] | 1565 | auto NewCode = applyAllReplacements(Code, Replaces); |
| 1566 | if (!NewCode) |
| 1567 | return NewCode.takeError(); |
Eric Liu | 40ef2fb | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 1568 | std::vector<tooling::Range> ChangedRanges = Replaces.getAffectedRanges(); |
Manuel Klimek | b12e5a5 | 2016-03-01 12:37:30 +0000 | [diff] [blame] | 1569 | StringRef FileName = Replaces.begin()->getFilePath(); |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 1570 | |
Manuel Klimek | b12e5a5 | 2016-03-01 12:37:30 +0000 | [diff] [blame] | 1571 | tooling::Replacements FormatReplaces = |
Eric Liu | 4f8d994 | 2016-07-11 13:53:12 +0000 | [diff] [blame] | 1572 | ProcessFunc(Style, *NewCode, ChangedRanges, FileName); |
Manuel Klimek | b12e5a5 | 2016-03-01 12:37:30 +0000 | [diff] [blame] | 1573 | |
Eric Liu | 40ef2fb | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 1574 | return Replaces.merge(FormatReplaces); |
Manuel Klimek | b12e5a5 | 2016-03-01 12:37:30 +0000 | [diff] [blame] | 1575 | } |
| 1576 | |
Eric Liu | 4f8d994 | 2016-07-11 13:53:12 +0000 | [diff] [blame] | 1577 | llvm::Expected<tooling::Replacements> |
| 1578 | formatReplacements(StringRef Code, const tooling::Replacements &Replaces, |
| 1579 | const FormatStyle &Style) { |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 1580 | // We need to use lambda function here since there are two versions of |
Eric Liu | baf58c2 | 2016-05-18 13:43:48 +0000 | [diff] [blame] | 1581 | // `sortIncludes`. |
| 1582 | auto SortIncludes = [](const FormatStyle &Style, StringRef Code, |
| 1583 | std::vector<tooling::Range> Ranges, |
| 1584 | StringRef FileName) -> tooling::Replacements { |
| 1585 | return sortIncludes(Style, Code, Ranges, FileName); |
| 1586 | }; |
Eric Liu | 4f8d994 | 2016-07-11 13:53:12 +0000 | [diff] [blame] | 1587 | auto SortedReplaces = |
Eric Liu | baf58c2 | 2016-05-18 13:43:48 +0000 | [diff] [blame] | 1588 | processReplacements(SortIncludes, Code, Replaces, Style); |
Eric Liu | 4f8d994 | 2016-07-11 13:53:12 +0000 | [diff] [blame] | 1589 | if (!SortedReplaces) |
| 1590 | return SortedReplaces.takeError(); |
Eric Liu | baf58c2 | 2016-05-18 13:43:48 +0000 | [diff] [blame] | 1591 | |
| 1592 | // We need to use lambda function here since there are two versions of |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 1593 | // `reformat`. |
| 1594 | auto Reformat = [](const FormatStyle &Style, StringRef Code, |
| 1595 | std::vector<tooling::Range> Ranges, |
| 1596 | StringRef FileName) -> tooling::Replacements { |
| 1597 | return reformat(Style, Code, Ranges, FileName); |
| 1598 | }; |
Eric Liu | 4f8d994 | 2016-07-11 13:53:12 +0000 | [diff] [blame] | 1599 | return processReplacements(Reformat, Code, *SortedReplaces, Style); |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 1600 | } |
| 1601 | |
Eric Liu | 659afd5 | 2016-05-31 13:34:20 +0000 | [diff] [blame] | 1602 | namespace { |
| 1603 | |
| 1604 | inline bool isHeaderInsertion(const tooling::Replacement &Replace) { |
Eric Liu | c0d3a80 | 2016-09-23 15:10:56 +0000 | [diff] [blame] | 1605 | return Replace.getOffset() == UINT_MAX && Replace.getLength() == 0 && |
Eric Liu | 659afd5 | 2016-05-31 13:34:20 +0000 | [diff] [blame] | 1606 | llvm::Regex(IncludeRegexPattern).match(Replace.getReplacementText()); |
| 1607 | } |
| 1608 | |
Eric Liu | c0d3a80 | 2016-09-23 15:10:56 +0000 | [diff] [blame] | 1609 | inline bool isHeaderDeletion(const tooling::Replacement &Replace) { |
| 1610 | return Replace.getOffset() == UINT_MAX && Replace.getLength() == 1; |
| 1611 | } |
| 1612 | |
Eric Liu | 964782a | 2016-12-02 11:01:43 +0000 | [diff] [blame] | 1613 | // Returns the offset after skipping a sequence of tokens, matched by \p |
| 1614 | // GetOffsetAfterSequence, from the start of the code. |
| 1615 | // \p GetOffsetAfterSequence should be a function that matches a sequence of |
| 1616 | // tokens and returns an offset after the sequence. |
| 1617 | unsigned getOffsetAfterTokenSequence( |
| 1618 | StringRef FileName, StringRef Code, const FormatStyle &Style, |
Krasimir Georgiev | 317c539 | 2017-03-08 09:13:25 +0000 | [diff] [blame] | 1619 | llvm::function_ref<unsigned(const SourceManager &, Lexer &, Token &)> |
| 1620 | GetOffsetAfterSequence) { |
Eric Liu | 964782a | 2016-12-02 11:01:43 +0000 | [diff] [blame] | 1621 | std::unique_ptr<Environment> Env = |
| 1622 | Environment::CreateVirtualEnvironment(Code, FileName, /*Ranges=*/{}); |
| 1623 | const SourceManager &SourceMgr = Env->getSourceManager(); |
| 1624 | Lexer Lex(Env->getFileID(), SourceMgr.getBuffer(Env->getFileID()), SourceMgr, |
| 1625 | getFormattingLangOpts(Style)); |
| 1626 | Token Tok; |
| 1627 | // Get the first token. |
| 1628 | Lex.LexFromRawLexer(Tok); |
Krasimir Georgiev | 317c539 | 2017-03-08 09:13:25 +0000 | [diff] [blame] | 1629 | return GetOffsetAfterSequence(SourceMgr, Lex, Tok); |
Eric Liu | 3528832 | 2016-06-06 11:00:13 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | // Check if a sequence of tokens is like "#<Name> <raw_identifier>". If it is, |
| 1633 | // \p Tok will be the token after this directive; otherwise, it can be any token |
| 1634 | // after the given \p Tok (including \p Tok). |
| 1635 | bool checkAndConsumeDirectiveWithName(Lexer &Lex, StringRef Name, Token &Tok) { |
| 1636 | bool Matched = Tok.is(tok::hash) && !Lex.LexFromRawLexer(Tok) && |
| 1637 | Tok.is(tok::raw_identifier) && |
| 1638 | Tok.getRawIdentifier() == Name && !Lex.LexFromRawLexer(Tok) && |
Eric Liu | 93459d3 | 2016-12-19 10:41:05 +0000 | [diff] [blame] | 1639 | Tok.is(tok::raw_identifier); |
Eric Liu | 3528832 | 2016-06-06 11:00:13 +0000 | [diff] [blame] | 1640 | if (Matched) |
| 1641 | Lex.LexFromRawLexer(Tok); |
| 1642 | return Matched; |
| 1643 | } |
| 1644 | |
Eric Liu | 964782a | 2016-12-02 11:01:43 +0000 | [diff] [blame] | 1645 | void skipComments(Lexer &Lex, Token &Tok) { |
| 1646 | while (Tok.is(tok::comment)) |
| 1647 | if (Lex.LexFromRawLexer(Tok)) |
| 1648 | return; |
| 1649 | } |
| 1650 | |
| 1651 | // Returns the offset after header guard directives and any comments |
| 1652 | // before/after header guards. If no header guard presents in the code, this |
| 1653 | // will returns the offset after skipping all comments from the start of the |
| 1654 | // code. |
Eric Liu | 3528832 | 2016-06-06 11:00:13 +0000 | [diff] [blame] | 1655 | unsigned getOffsetAfterHeaderGuardsAndComments(StringRef FileName, |
| 1656 | StringRef Code, |
Eric Liu | 43d67b6 | 2016-06-11 11:45:08 +0000 | [diff] [blame] | 1657 | const FormatStyle &Style) { |
Eric Liu | 964782a | 2016-12-02 11:01:43 +0000 | [diff] [blame] | 1658 | return getOffsetAfterTokenSequence( |
| 1659 | FileName, Code, Style, |
| 1660 | [](const SourceManager &SM, Lexer &Lex, Token Tok) { |
| 1661 | skipComments(Lex, Tok); |
| 1662 | unsigned InitialOffset = SM.getFileOffset(Tok.getLocation()); |
| 1663 | if (checkAndConsumeDirectiveWithName(Lex, "ifndef", Tok)) { |
| 1664 | skipComments(Lex, Tok); |
| 1665 | if (checkAndConsumeDirectiveWithName(Lex, "define", Tok)) |
| 1666 | return SM.getFileOffset(Tok.getLocation()); |
| 1667 | } |
| 1668 | return InitialOffset; |
| 1669 | }); |
| 1670 | } |
| 1671 | |
| 1672 | // Check if a sequence of tokens is like |
| 1673 | // "#include ("header.h" | <header.h>)". |
| 1674 | // If it is, \p Tok will be the token after this directive; otherwise, it can be |
| 1675 | // any token after the given \p Tok (including \p Tok). |
| 1676 | bool checkAndConsumeInclusiveDirective(Lexer &Lex, Token &Tok) { |
| 1677 | auto Matched = [&]() { |
| 1678 | Lex.LexFromRawLexer(Tok); |
| 1679 | return true; |
| 1680 | }; |
| 1681 | if (Tok.is(tok::hash) && !Lex.LexFromRawLexer(Tok) && |
| 1682 | Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == "include") { |
| 1683 | if (Lex.LexFromRawLexer(Tok)) |
| 1684 | return false; |
| 1685 | if (Tok.is(tok::string_literal)) |
| 1686 | return Matched(); |
| 1687 | if (Tok.is(tok::less)) { |
| 1688 | while (!Lex.LexFromRawLexer(Tok) && Tok.isNot(tok::greater)) { |
| 1689 | } |
| 1690 | if (Tok.is(tok::greater)) |
| 1691 | return Matched(); |
| 1692 | } |
Eric Liu | 3528832 | 2016-06-06 11:00:13 +0000 | [diff] [blame] | 1693 | } |
Eric Liu | 964782a | 2016-12-02 11:01:43 +0000 | [diff] [blame] | 1694 | return false; |
| 1695 | } |
| 1696 | |
| 1697 | // Returns the offset of the last #include directive after which a new |
| 1698 | // #include can be inserted. This ignores #include's after the #include block(s) |
| 1699 | // in the beginning of a file to avoid inserting headers into code sections |
| 1700 | // where new #include's should not be added by default. |
| 1701 | // These code sections include: |
| 1702 | // - raw string literals (containing #include). |
| 1703 | // - #if blocks. |
| 1704 | // - Special #include's among declarations (e.g. functions). |
| 1705 | // |
| 1706 | // If no #include after which a new #include can be inserted, this returns the |
| 1707 | // offset after skipping all comments from the start of the code. |
| 1708 | // Inserting after an #include is not allowed if it comes after code that is not |
| 1709 | // #include (e.g. pre-processing directive that is not #include, declarations). |
| 1710 | unsigned getMaxHeaderInsertionOffset(StringRef FileName, StringRef Code, |
| 1711 | const FormatStyle &Style) { |
| 1712 | return getOffsetAfterTokenSequence( |
| 1713 | FileName, Code, Style, |
| 1714 | [](const SourceManager &SM, Lexer &Lex, Token Tok) { |
| 1715 | skipComments(Lex, Tok); |
| 1716 | unsigned MaxOffset = SM.getFileOffset(Tok.getLocation()); |
| 1717 | while (checkAndConsumeInclusiveDirective(Lex, Tok)) |
| 1718 | MaxOffset = SM.getFileOffset(Tok.getLocation()); |
| 1719 | return MaxOffset; |
| 1720 | }); |
Eric Liu | 3528832 | 2016-06-06 11:00:13 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
Eric Liu | c0d3a80 | 2016-09-23 15:10:56 +0000 | [diff] [blame] | 1723 | bool isDeletedHeader(llvm::StringRef HeaderName, |
Benjamin Kramer | ebac56e | 2016-11-24 15:42:29 +0000 | [diff] [blame] | 1724 | const std::set<llvm::StringRef> &HeadersToDelete) { |
| 1725 | return HeadersToDelete.count(HeaderName) || |
| 1726 | HeadersToDelete.count(HeaderName.trim("\"<>")); |
Eric Liu | c0d3a80 | 2016-09-23 15:10:56 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
Eric Liu | 659afd5 | 2016-05-31 13:34:20 +0000 | [diff] [blame] | 1729 | // FIXME: insert empty lines between newly created blocks. |
| 1730 | tooling::Replacements |
| 1731 | fixCppIncludeInsertions(StringRef Code, const tooling::Replacements &Replaces, |
| 1732 | const FormatStyle &Style) { |
Daniel Jasper | 1dbc210 | 2017-03-31 13:30:24 +0000 | [diff] [blame] | 1733 | if (!Style.isCpp()) |
Eric Liu | 659afd5 | 2016-05-31 13:34:20 +0000 | [diff] [blame] | 1734 | return Replaces; |
| 1735 | |
| 1736 | tooling::Replacements HeaderInsertions; |
Eric Liu | c0d3a80 | 2016-09-23 15:10:56 +0000 | [diff] [blame] | 1737 | std::set<llvm::StringRef> HeadersToDelete; |
Eric Liu | 40ef2fb | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 1738 | tooling::Replacements Result; |
Eric Liu | 659afd5 | 2016-05-31 13:34:20 +0000 | [diff] [blame] | 1739 | for (const auto &R : Replaces) { |
Eric Liu | 40ef2fb | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 1740 | if (isHeaderInsertion(R)) { |
| 1741 | // Replacements from \p Replaces must be conflict-free already, so we can |
| 1742 | // simply consume the error. |
| 1743 | llvm::consumeError(HeaderInsertions.add(R)); |
Eric Liu | c0d3a80 | 2016-09-23 15:10:56 +0000 | [diff] [blame] | 1744 | } else if (isHeaderDeletion(R)) { |
| 1745 | HeadersToDelete.insert(R.getReplacementText()); |
Eric Liu | 40ef2fb | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 1746 | } else if (R.getOffset() == UINT_MAX) { |
Eric Liu | 659afd5 | 2016-05-31 13:34:20 +0000 | [diff] [blame] | 1747 | llvm::errs() << "Insertions other than header #include insertion are " |
| 1748 | "not supported! " |
| 1749 | << R.getReplacementText() << "\n"; |
Eric Liu | 40ef2fb | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 1750 | } else { |
| 1751 | llvm::consumeError(Result.add(R)); |
| 1752 | } |
Eric Liu | 659afd5 | 2016-05-31 13:34:20 +0000 | [diff] [blame] | 1753 | } |
Eric Liu | c0d3a80 | 2016-09-23 15:10:56 +0000 | [diff] [blame] | 1754 | if (HeaderInsertions.empty() && HeadersToDelete.empty()) |
Eric Liu | 659afd5 | 2016-05-31 13:34:20 +0000 | [diff] [blame] | 1755 | return Replaces; |
Eric Liu | 659afd5 | 2016-05-31 13:34:20 +0000 | [diff] [blame] | 1756 | |
| 1757 | llvm::Regex IncludeRegex(IncludeRegexPattern); |
| 1758 | llvm::Regex DefineRegex(R"(^[\t\ ]*#[\t\ ]*define[\t\ ]*[^\\]*$)"); |
| 1759 | SmallVector<StringRef, 4> Matches; |
| 1760 | |
| 1761 | StringRef FileName = Replaces.begin()->getFilePath(); |
| 1762 | IncludeCategoryManager Categories(Style, FileName); |
| 1763 | |
| 1764 | // Record the offset of the end of the last include in each category. |
| 1765 | std::map<int, int> CategoryEndOffsets; |
| 1766 | // All possible priorities. |
| 1767 | // Add 0 for main header and INT_MAX for headers that are not in any category. |
| 1768 | std::set<int> Priorities = {0, INT_MAX}; |
| 1769 | for (const auto &Category : Style.IncludeCategories) |
| 1770 | Priorities.insert(Category.Priority); |
| 1771 | int FirstIncludeOffset = -1; |
Eric Liu | 3528832 | 2016-06-06 11:00:13 +0000 | [diff] [blame] | 1772 | // All new headers should be inserted after this offset. |
| 1773 | unsigned MinInsertOffset = |
| 1774 | getOffsetAfterHeaderGuardsAndComments(FileName, Code, Style); |
Eric Liu | 303baf5 | 2016-06-03 12:52:59 +0000 | [diff] [blame] | 1775 | StringRef TrimmedCode = Code.drop_front(MinInsertOffset); |
Eric Liu | 21d1032 | 2016-12-09 11:45:50 +0000 | [diff] [blame] | 1776 | // Max insertion offset in the original code. |
Eric Liu | 964782a | 2016-12-02 11:01:43 +0000 | [diff] [blame] | 1777 | unsigned MaxInsertOffset = |
Eric Liu | 21d1032 | 2016-12-09 11:45:50 +0000 | [diff] [blame] | 1778 | MinInsertOffset + |
Eric Liu | 964782a | 2016-12-02 11:01:43 +0000 | [diff] [blame] | 1779 | getMaxHeaderInsertionOffset(FileName, TrimmedCode, Style); |
Eric Liu | 659afd5 | 2016-05-31 13:34:20 +0000 | [diff] [blame] | 1780 | SmallVector<StringRef, 32> Lines; |
Eric Liu | 303baf5 | 2016-06-03 12:52:59 +0000 | [diff] [blame] | 1781 | TrimmedCode.split(Lines, '\n'); |
Eric Liu | 3528832 | 2016-06-06 11:00:13 +0000 | [diff] [blame] | 1782 | unsigned Offset = MinInsertOffset; |
| 1783 | unsigned NextLineOffset; |
Eric Liu | 3753f91 | 2016-06-14 14:09:21 +0000 | [diff] [blame] | 1784 | std::set<StringRef> ExistingIncludes; |
Eric Liu | 659afd5 | 2016-05-31 13:34:20 +0000 | [diff] [blame] | 1785 | for (auto Line : Lines) { |
Eric Liu | 3528832 | 2016-06-06 11:00:13 +0000 | [diff] [blame] | 1786 | NextLineOffset = std::min(Code.size(), Offset + Line.size() + 1); |
Eric Liu | 659afd5 | 2016-05-31 13:34:20 +0000 | [diff] [blame] | 1787 | if (IncludeRegex.match(Line, &Matches)) { |
Eric Liu | c0d3a80 | 2016-09-23 15:10:56 +0000 | [diff] [blame] | 1788 | // The header name with quotes or angle brackets. |
Eric Liu | 659afd5 | 2016-05-31 13:34:20 +0000 | [diff] [blame] | 1789 | StringRef IncludeName = Matches[2]; |
Eric Liu | 3753f91 | 2016-06-14 14:09:21 +0000 | [diff] [blame] | 1790 | ExistingIncludes.insert(IncludeName); |
Eric Liu | 964782a | 2016-12-02 11:01:43 +0000 | [diff] [blame] | 1791 | // Only record the offset of current #include if we can insert after it. |
| 1792 | if (Offset <= MaxInsertOffset) { |
| 1793 | int Category = Categories.getIncludePriority( |
| 1794 | IncludeName, /*CheckMainHeader=*/FirstIncludeOffset < 0); |
| 1795 | CategoryEndOffsets[Category] = NextLineOffset; |
| 1796 | if (FirstIncludeOffset < 0) |
| 1797 | FirstIncludeOffset = Offset; |
| 1798 | } |
Eric Liu | c0d3a80 | 2016-09-23 15:10:56 +0000 | [diff] [blame] | 1799 | if (isDeletedHeader(IncludeName, HeadersToDelete)) { |
| 1800 | // If this is the last line without trailing newline, we need to make |
| 1801 | // sure we don't delete across the file boundary. |
| 1802 | unsigned Length = std::min(Line.size() + 1, Code.size() - Offset); |
| 1803 | llvm::Error Err = |
| 1804 | Result.add(tooling::Replacement(FileName, Offset, Length, "")); |
| 1805 | if (Err) { |
| 1806 | // Ignore the deletion on conflict. |
| 1807 | llvm::errs() << "Failed to add header deletion replacement for " |
| 1808 | << IncludeName << ": " << llvm::toString(std::move(Err)) |
| 1809 | << "\n"; |
| 1810 | } |
| 1811 | } |
Eric Liu | 659afd5 | 2016-05-31 13:34:20 +0000 | [diff] [blame] | 1812 | } |
Eric Liu | 3528832 | 2016-06-06 11:00:13 +0000 | [diff] [blame] | 1813 | Offset = NextLineOffset; |
Eric Liu | 659afd5 | 2016-05-31 13:34:20 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | // Populate CategoryEndOfssets: |
| 1817 | // - Ensure that CategoryEndOffset[Highest] is always populated. |
| 1818 | // - If CategoryEndOffset[Priority] isn't set, use the next higher value that |
| 1819 | // is set, up to CategoryEndOffset[Highest]. |
Eric Liu | 659afd5 | 2016-05-31 13:34:20 +0000 | [diff] [blame] | 1820 | auto Highest = Priorities.begin(); |
| 1821 | if (CategoryEndOffsets.find(*Highest) == CategoryEndOffsets.end()) { |
| 1822 | if (FirstIncludeOffset >= 0) |
| 1823 | CategoryEndOffsets[*Highest] = FirstIncludeOffset; |
| 1824 | else |
Eric Liu | 303baf5 | 2016-06-03 12:52:59 +0000 | [diff] [blame] | 1825 | CategoryEndOffsets[*Highest] = MinInsertOffset; |
Eric Liu | 659afd5 | 2016-05-31 13:34:20 +0000 | [diff] [blame] | 1826 | } |
| 1827 | // By this point, CategoryEndOffset[Highest] is always set appropriately: |
| 1828 | // - to an appropriate location before/after existing #includes, or |
| 1829 | // - to right after the header guard, or |
| 1830 | // - to the beginning of the file. |
| 1831 | for (auto I = ++Priorities.begin(), E = Priorities.end(); I != E; ++I) |
| 1832 | if (CategoryEndOffsets.find(*I) == CategoryEndOffsets.end()) |
| 1833 | CategoryEndOffsets[*I] = CategoryEndOffsets[*std::prev(I)]; |
| 1834 | |
Eric Liu | 11a4237 | 2016-10-05 15:42:19 +0000 | [diff] [blame] | 1835 | bool NeedNewLineAtEnd = !Code.empty() && Code.back() != '\n'; |
Eric Liu | 659afd5 | 2016-05-31 13:34:20 +0000 | [diff] [blame] | 1836 | for (const auto &R : HeaderInsertions) { |
| 1837 | auto IncludeDirective = R.getReplacementText(); |
| 1838 | bool Matched = IncludeRegex.match(IncludeDirective, &Matches); |
| 1839 | assert(Matched && "Header insertion replacement must have replacement text " |
| 1840 | "'#include ...'"); |
Benjamin Kramer | 1cb7ee1 | 2016-05-31 14:14:42 +0000 | [diff] [blame] | 1841 | (void)Matched; |
Eric Liu | 659afd5 | 2016-05-31 13:34:20 +0000 | [diff] [blame] | 1842 | auto IncludeName = Matches[2]; |
Eric Liu | 3753f91 | 2016-06-14 14:09:21 +0000 | [diff] [blame] | 1843 | if (ExistingIncludes.find(IncludeName) != ExistingIncludes.end()) { |
| 1844 | DEBUG(llvm::dbgs() << "Skip adding existing include : " << IncludeName |
| 1845 | << "\n"); |
| 1846 | continue; |
| 1847 | } |
Eric Liu | 659afd5 | 2016-05-31 13:34:20 +0000 | [diff] [blame] | 1848 | int Category = |
| 1849 | Categories.getIncludePriority(IncludeName, /*CheckMainHeader=*/true); |
| 1850 | Offset = CategoryEndOffsets[Category]; |
| 1851 | std::string NewInclude = !IncludeDirective.endswith("\n") |
| 1852 | ? (IncludeDirective + "\n").str() |
| 1853 | : IncludeDirective.str(); |
Eric Liu | 11a4237 | 2016-10-05 15:42:19 +0000 | [diff] [blame] | 1854 | // When inserting headers at end of the code, also append '\n' to the code |
| 1855 | // if it does not end with '\n'. |
| 1856 | if (NeedNewLineAtEnd && Offset == Code.size()) { |
| 1857 | NewInclude = "\n" + NewInclude; |
| 1858 | NeedNewLineAtEnd = false; |
| 1859 | } |
Eric Liu | 40ef2fb | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 1860 | auto NewReplace = tooling::Replacement(FileName, Offset, 0, NewInclude); |
| 1861 | auto Err = Result.add(NewReplace); |
| 1862 | if (Err) { |
| 1863 | llvm::consumeError(std::move(Err)); |
Eric Liu | 11a4237 | 2016-10-05 15:42:19 +0000 | [diff] [blame] | 1864 | unsigned NewOffset = Result.getShiftedCodePosition(Offset); |
| 1865 | NewReplace = tooling::Replacement(FileName, NewOffset, 0, NewInclude); |
Eric Liu | 40ef2fb | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 1866 | Result = Result.merge(tooling::Replacements(NewReplace)); |
| 1867 | } |
Eric Liu | 659afd5 | 2016-05-31 13:34:20 +0000 | [diff] [blame] | 1868 | } |
| 1869 | return Result; |
| 1870 | } |
| 1871 | |
| 1872 | } // anonymous namespace |
| 1873 | |
Eric Liu | 4f8d994 | 2016-07-11 13:53:12 +0000 | [diff] [blame] | 1874 | llvm::Expected<tooling::Replacements> |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 1875 | cleanupAroundReplacements(StringRef Code, const tooling::Replacements &Replaces, |
| 1876 | const FormatStyle &Style) { |
| 1877 | // We need to use lambda function here since there are two versions of |
| 1878 | // `cleanup`. |
| 1879 | auto Cleanup = [](const FormatStyle &Style, StringRef Code, |
| 1880 | std::vector<tooling::Range> Ranges, |
| 1881 | StringRef FileName) -> tooling::Replacements { |
| 1882 | return cleanup(Style, Code, Ranges, FileName); |
| 1883 | }; |
Eric Liu | 659afd5 | 2016-05-31 13:34:20 +0000 | [diff] [blame] | 1884 | // Make header insertion replacements insert new headers into correct blocks. |
| 1885 | tooling::Replacements NewReplaces = |
| 1886 | fixCppIncludeInsertions(Code, Replaces, Style); |
| 1887 | return processReplacements(Cleanup, Code, NewReplaces, Style); |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 1888 | } |
| 1889 | |
Daniel Jasper | ec04c0d | 2013-05-16 10:40:07 +0000 | [diff] [blame] | 1890 | tooling::Replacements reformat(const FormatStyle &Style, StringRef Code, |
Benjamin Kramer | d0eed3a | 2014-10-03 18:52:48 +0000 | [diff] [blame] | 1891 | ArrayRef<tooling::Range> Ranges, |
Krasimir Georgiev | bcda54b | 2017-04-21 14:35:20 +0000 | [diff] [blame] | 1892 | StringRef FileName, |
| 1893 | FormattingAttemptStatus *Status) { |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 1894 | FormatStyle Expanded = expandPresets(Style); |
| 1895 | if (Expanded.DisableFormat) |
Daniel Jasper | 2337625 | 2014-09-09 14:37:39 +0000 | [diff] [blame] | 1896 | return tooling::Replacements(); |
Martin Probst | fa37b18 | 2017-01-27 09:09:11 +0000 | [diff] [blame] | 1897 | if (Expanded.Language == FormatStyle::LK_JavaScript && isMpegTS(Code)) |
| 1898 | return tooling::Replacements(); |
Daniel Jasper | 496c199 | 2016-09-07 22:48:53 +0000 | [diff] [blame] | 1899 | |
Krasimir Georgiev | ac16a20 | 2017-06-23 11:46:03 +0000 | [diff] [blame] | 1900 | typedef std::function<tooling::Replacements(const Environment &)> |
| 1901 | AnalyzerPass; |
| 1902 | SmallVector<AnalyzerPass, 4> Passes; |
Krasimir Georgiev | 32eaa86 | 2017-03-01 15:35:39 +0000 | [diff] [blame] | 1903 | |
Krasimir Georgiev | ac16a20 | 2017-06-23 11:46:03 +0000 | [diff] [blame] | 1904 | if (Style.Language == FormatStyle::LK_Cpp) { |
| 1905 | if (Style.FixNamespaceComments) |
| 1906 | Passes.emplace_back([&](const Environment &Env) { |
| 1907 | return NamespaceEndCommentsFixer(Env, Expanded).process(); |
| 1908 | }); |
| 1909 | |
| 1910 | if (Style.SortUsingDeclarations) |
| 1911 | Passes.emplace_back([&](const Environment &Env) { |
| 1912 | return UsingDeclarationsSorter(Env, Expanded).process(); |
| 1913 | }); |
Krasimir Georgiev | 32eaa86 | 2017-03-01 15:35:39 +0000 | [diff] [blame] | 1914 | } |
| 1915 | |
| 1916 | if (Style.Language == FormatStyle::LK_JavaScript && |
Krasimir Georgiev | ac16a20 | 2017-06-23 11:46:03 +0000 | [diff] [blame] | 1917 | Style.JavaScriptQuotes != FormatStyle::JSQS_Leave) |
| 1918 | Passes.emplace_back([&](const Environment &Env) { |
| 1919 | return JavaScriptRequoter(Env, Expanded).process(); |
| 1920 | }); |
| 1921 | |
| 1922 | Passes.emplace_back([&](const Environment &Env) { |
| 1923 | return Formatter(Env, Expanded, Status).process(); |
| 1924 | }); |
| 1925 | |
| 1926 | std::unique_ptr<Environment> Env = |
| 1927 | Environment::CreateVirtualEnvironment(Code, FileName, Ranges); |
| 1928 | llvm::Optional<std::string> CurrentCode = None; |
| 1929 | tooling::Replacements Fixes; |
| 1930 | for (size_t I = 0, E = Passes.size(); I < E; ++I) { |
| 1931 | tooling::Replacements PassFixes = Passes[I](*Env); |
| 1932 | auto NewCode = applyAllReplacements( |
| 1933 | CurrentCode ? StringRef(*CurrentCode) : Code, PassFixes); |
| 1934 | if (NewCode) { |
| 1935 | Fixes = Fixes.merge(PassFixes); |
| 1936 | if (I + 1 < E) { |
| 1937 | CurrentCode = std::move(*NewCode); |
| 1938 | Env = Environment::CreateVirtualEnvironment( |
| 1939 | *CurrentCode, FileName, |
| 1940 | tooling::calculateRangesAfterReplacements(Fixes, Ranges)); |
| 1941 | } |
| 1942 | } |
Daniel Jasper | 496c199 | 2016-09-07 22:48:53 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
Krasimir Georgiev | ac16a20 | 2017-06-23 11:46:03 +0000 | [diff] [blame] | 1945 | return Fixes; |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 1946 | } |
| 1947 | |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 1948 | tooling::Replacements cleanup(const FormatStyle &Style, StringRef Code, |
| 1949 | ArrayRef<tooling::Range> Ranges, |
| 1950 | StringRef FileName) { |
Martin Probst | 816a966 | 2017-05-29 08:41:11 +0000 | [diff] [blame] | 1951 | // cleanups only apply to C++ (they mostly concern ctor commas etc.) |
| 1952 | if (Style.Language != FormatStyle::LK_Cpp) |
| 1953 | return tooling::Replacements(); |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 1954 | std::unique_ptr<Environment> Env = |
Eric Liu | 635423e | 2016-04-28 07:52:03 +0000 | [diff] [blame] | 1955 | Environment::CreateVirtualEnvironment(Code, FileName, Ranges); |
| 1956 | Cleaner Clean(*Env, Style); |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 1957 | return Clean.process(); |
Daniel Jasper | ec04c0d | 2013-05-16 10:40:07 +0000 | [diff] [blame] | 1958 | } |
| 1959 | |
Krasimir Georgiev | bcda54b | 2017-04-21 14:35:20 +0000 | [diff] [blame] | 1960 | tooling::Replacements reformat(const FormatStyle &Style, StringRef Code, |
| 1961 | ArrayRef<tooling::Range> Ranges, |
| 1962 | StringRef FileName, bool *IncompleteFormat) { |
| 1963 | FormattingAttemptStatus Status; |
| 1964 | auto Result = reformat(Style, Code, Ranges, FileName, &Status); |
| 1965 | if (!Status.FormatComplete) |
| 1966 | *IncompleteFormat = true; |
| 1967 | return Result; |
| 1968 | } |
| 1969 | |
Krasimir Georgiev | 7cb267a | 2017-02-27 13:28:36 +0000 | [diff] [blame] | 1970 | tooling::Replacements fixNamespaceEndComments(const FormatStyle &Style, |
| 1971 | StringRef Code, |
| 1972 | ArrayRef<tooling::Range> Ranges, |
| 1973 | StringRef FileName) { |
| 1974 | std::unique_ptr<Environment> Env = |
| 1975 | Environment::CreateVirtualEnvironment(Code, FileName, Ranges); |
| 1976 | NamespaceEndCommentsFixer Fix(*Env, Style); |
| 1977 | return Fix.process(); |
| 1978 | } |
| 1979 | |
Krasimir Georgiev | b03877a | 2017-06-21 12:03:12 +0000 | [diff] [blame] | 1980 | tooling::Replacements sortUsingDeclarations(const FormatStyle &Style, |
| 1981 | StringRef Code, |
| 1982 | ArrayRef<tooling::Range> Ranges, |
| 1983 | StringRef FileName) { |
| 1984 | std::unique_ptr<Environment> Env = |
| 1985 | Environment::CreateVirtualEnvironment(Code, FileName, Ranges); |
| 1986 | UsingDeclarationsSorter Sorter(*Env, Style); |
| 1987 | return Sorter.process(); |
| 1988 | } |
| 1989 | |
Daniel Jasper | 4db69bd | 2014-09-04 18:23:42 +0000 | [diff] [blame] | 1990 | LangOptions getFormattingLangOpts(const FormatStyle &Style) { |
Daniel Jasper | c1fa281 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1991 | LangOptions LangOpts; |
| 1992 | LangOpts.CPlusPlus = 1; |
Daniel Jasper | 4db69bd | 2014-09-04 18:23:42 +0000 | [diff] [blame] | 1993 | LangOpts.CPlusPlus11 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1; |
| 1994 | LangOpts.CPlusPlus14 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1; |
Nico Weber | dc06518 | 2017-04-05 18:10:42 +0000 | [diff] [blame] | 1995 | LangOpts.CPlusPlus1z = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1; |
Daniel Jasper | 5521365 | 2013-03-22 10:01:29 +0000 | [diff] [blame] | 1996 | LangOpts.LineComment = 1; |
Daniel Jasper | 1dbc210 | 2017-03-31 13:30:24 +0000 | [diff] [blame] | 1997 | bool AlternativeOperators = Style.isCpp(); |
Daniel Jasper | 30a2406 | 2014-11-14 09:02:28 +0000 | [diff] [blame] | 1998 | LangOpts.CXXOperatorNames = AlternativeOperators ? 1 : 0; |
Daniel Jasper | c1fa281 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1999 | LangOpts.Bool = 1; |
| 2000 | LangOpts.ObjC1 = 1; |
| 2001 | LangOpts.ObjC2 = 1; |
Eric Liu | 4cfb88a | 2016-04-25 15:09:22 +0000 | [diff] [blame] | 2002 | LangOpts.MicrosoftExt = 1; // To get kw___try, kw___finally. |
Saleem Abdulrasool | d170c4b | 2015-10-04 17:51:05 +0000 | [diff] [blame] | 2003 | LangOpts.DeclSpecKeyword = 1; // To get __declspec. |
Daniel Jasper | c1fa281 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 2004 | return LangOpts; |
| 2005 | } |
| 2006 | |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 2007 | const char *StyleOptionHelpDescription = |
| 2008 | "Coding style, currently supports:\n" |
| 2009 | " LLVM, Google, Chromium, Mozilla, WebKit.\n" |
| 2010 | "Use -style=file to load style configuration from\n" |
| 2011 | ".clang-format file located in one of the parent\n" |
| 2012 | "directories of the source file (or current\n" |
| 2013 | "directory for stdin).\n" |
| 2014 | "Use -style=\"{key: value, ...}\" to set specific\n" |
| 2015 | "parameters, e.g.:\n" |
| 2016 | " -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\""; |
| 2017 | |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 2018 | static FormatStyle::LanguageKind getLanguageByFileName(StringRef FileName) { |
Daniel Jasper | 498f558 | 2015-12-25 08:53:31 +0000 | [diff] [blame] | 2019 | if (FileName.endswith(".java")) |
Daniel Jasper | c58c70e | 2014-09-15 11:21:46 +0000 | [diff] [blame] | 2020 | return FormatStyle::LK_Java; |
Daniel Jasper | 498f558 | 2015-12-25 08:53:31 +0000 | [diff] [blame] | 2021 | if (FileName.endswith_lower(".js") || FileName.endswith_lower(".ts")) |
| 2022 | return FormatStyle::LK_JavaScript; // JavaScript or TypeScript. |
Daniel Jasper | 03a04fe | 2016-12-12 12:42:29 +0000 | [diff] [blame] | 2023 | if (FileName.endswith(".m") || FileName.endswith(".mm")) |
| 2024 | return FormatStyle::LK_ObjC; |
Daniel Jasper | 498f558 | 2015-12-25 08:53:31 +0000 | [diff] [blame] | 2025 | if (FileName.endswith_lower(".proto") || |
| 2026 | FileName.endswith_lower(".protodevel")) |
Daniel Jasper | 7052ce6 | 2014-01-19 09:04:08 +0000 | [diff] [blame] | 2027 | return FormatStyle::LK_Proto; |
Daniel Jasper | 498f558 | 2015-12-25 08:53:31 +0000 | [diff] [blame] | 2028 | if (FileName.endswith_lower(".td")) |
| 2029 | return FormatStyle::LK_TableGen; |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 2030 | return FormatStyle::LK_Cpp; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
Antonio Maiorano | 3adfb6a | 2017-01-17 00:12:27 +0000 | [diff] [blame] | 2033 | llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName, |
Antonio Maiorano | 7eb7507 | 2017-01-20 01:22:42 +0000 | [diff] [blame] | 2034 | StringRef FallbackStyleName, |
| 2035 | StringRef Code, vfs::FileSystem *FS) { |
Eric Liu | 547d879 | 2016-03-24 13:22:42 +0000 | [diff] [blame] | 2036 | if (!FS) { |
| 2037 | FS = vfs::getRealFileSystem().get(); |
| 2038 | } |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 2039 | FormatStyle Style = getLLVMStyle(); |
| 2040 | Style.Language = getLanguageByFileName(FileName); |
Daniel Jasper | 03a04fe | 2016-12-12 12:42:29 +0000 | [diff] [blame] | 2041 | |
| 2042 | // This is a very crude detection of whether a header contains ObjC code that |
| 2043 | // should be improved over time and probably be done on tokens, not one the |
| 2044 | // bare content of the file. |
| 2045 | if (Style.Language == FormatStyle::LK_Cpp && FileName.endswith(".h") && |
| 2046 | (Code.contains("\n- (") || Code.contains("\n+ ("))) |
| 2047 | Style.Language = FormatStyle::LK_ObjC; |
| 2048 | |
Antonio Maiorano | 7eb7507 | 2017-01-20 01:22:42 +0000 | [diff] [blame] | 2049 | FormatStyle FallbackStyle = getNoStyle(); |
| 2050 | if (!getPredefinedStyle(FallbackStyleName, Style.Language, &FallbackStyle)) |
| 2051 | return make_string_error("Invalid fallback style \"" + FallbackStyleName); |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 2052 | |
| 2053 | if (StyleName.startswith("{")) { |
| 2054 | // Parse YAML/JSON style from the command line. |
Antonio Maiorano | 3adfb6a | 2017-01-17 00:12:27 +0000 | [diff] [blame] | 2055 | if (std::error_code ec = parseConfiguration(StyleName, &Style)) |
| 2056 | return make_string_error("Error parsing -style: " + ec.message()); |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 2057 | return Style; |
| 2058 | } |
| 2059 | |
| 2060 | if (!StyleName.equals_lower("file")) { |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 2061 | if (!getPredefinedStyle(StyleName, Style.Language, &Style)) |
Antonio Maiorano | 3adfb6a | 2017-01-17 00:12:27 +0000 | [diff] [blame] | 2062 | return make_string_error("Invalid value for -style"); |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 2063 | return Style; |
| 2064 | } |
| 2065 | |
Alexander Kornienko | c1637f1 | 2013-12-10 11:28:13 +0000 | [diff] [blame] | 2066 | // Look for .clang-format/_clang-format file in the file's parent directories. |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 2067 | SmallString<128> UnsuitableConfigFiles; |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 2068 | SmallString<128> Path(FileName); |
Antonio Maiorano | 3adfb6a | 2017-01-17 00:12:27 +0000 | [diff] [blame] | 2069 | if (std::error_code EC = FS->makeAbsolute(Path)) |
| 2070 | return make_string_error(EC.message()); |
Antonio Maiorano | 34c0376 | 2016-12-22 05:10:07 +0000 | [diff] [blame] | 2071 | |
Alexander Kornienko | e2e0387 | 2013-10-14 00:46:35 +0000 | [diff] [blame] | 2072 | for (StringRef Directory = Path; !Directory.empty(); |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 2073 | Directory = llvm::sys::path::parent_path(Directory)) { |
Eric Liu | 547d879 | 2016-03-24 13:22:42 +0000 | [diff] [blame] | 2074 | |
| 2075 | auto Status = FS->status(Directory); |
| 2076 | if (!Status || |
| 2077 | Status->getType() != llvm::sys::fs::file_type::directory_file) { |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 2078 | continue; |
Eric Liu | 547d879 | 2016-03-24 13:22:42 +0000 | [diff] [blame] | 2079 | } |
| 2080 | |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 2081 | SmallString<128> ConfigFile(Directory); |
| 2082 | |
| 2083 | llvm::sys::path::append(ConfigFile, ".clang-format"); |
| 2084 | DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n"); |
Eric Liu | d475832 | 2016-03-24 13:22:37 +0000 | [diff] [blame] | 2085 | |
Eric Liu | 547d879 | 2016-03-24 13:22:42 +0000 | [diff] [blame] | 2086 | Status = FS->status(ConfigFile.str()); |
Antonio Maiorano | 3adfb6a | 2017-01-17 00:12:27 +0000 | [diff] [blame] | 2087 | bool FoundConfigFile = |
Eric Liu | 547d879 | 2016-03-24 13:22:42 +0000 | [diff] [blame] | 2088 | Status && (Status->getType() == llvm::sys::fs::file_type::regular_file); |
Antonio Maiorano | 3adfb6a | 2017-01-17 00:12:27 +0000 | [diff] [blame] | 2089 | if (!FoundConfigFile) { |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 2090 | // Try _clang-format too, since dotfiles are not commonly used on Windows. |
| 2091 | ConfigFile = Directory; |
| 2092 | llvm::sys::path::append(ConfigFile, "_clang-format"); |
| 2093 | DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n"); |
Eric Liu | 547d879 | 2016-03-24 13:22:42 +0000 | [diff] [blame] | 2094 | Status = FS->status(ConfigFile.str()); |
Antonio Maiorano | 3adfb6a | 2017-01-17 00:12:27 +0000 | [diff] [blame] | 2095 | FoundConfigFile = Status && (Status->getType() == |
| 2096 | llvm::sys::fs::file_type::regular_file); |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 2097 | } |
| 2098 | |
Antonio Maiorano | 3adfb6a | 2017-01-17 00:12:27 +0000 | [diff] [blame] | 2099 | if (FoundConfigFile) { |
Rafael Espindola | 2d2b420 | 2014-07-06 17:43:24 +0000 | [diff] [blame] | 2100 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text = |
Eric Liu | 547d879 | 2016-03-24 13:22:42 +0000 | [diff] [blame] | 2101 | FS->getBufferForFile(ConfigFile.str()); |
Antonio Maiorano | 3adfb6a | 2017-01-17 00:12:27 +0000 | [diff] [blame] | 2102 | if (std::error_code EC = Text.getError()) |
| 2103 | return make_string_error(EC.message()); |
Rafael Espindola | 2d2b420 | 2014-07-06 17:43:24 +0000 | [diff] [blame] | 2104 | if (std::error_code ec = |
| 2105 | parseConfiguration(Text.get()->getBuffer(), &Style)) { |
Rafael Espindola | d013670 | 2014-06-12 02:50:04 +0000 | [diff] [blame] | 2106 | if (ec == ParseError::Unsuitable) { |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 2107 | if (!UnsuitableConfigFiles.empty()) |
| 2108 | UnsuitableConfigFiles.append(", "); |
| 2109 | UnsuitableConfigFiles.append(ConfigFile); |
Alexander Kornienko | bc4ae44 | 2013-12-02 15:21:38 +0000 | [diff] [blame] | 2110 | continue; |
Alexander Kornienko | cabdd73 | 2013-11-29 15:19:43 +0000 | [diff] [blame] | 2111 | } |
Antonio Maiorano | 3adfb6a | 2017-01-17 00:12:27 +0000 | [diff] [blame] | 2112 | return make_string_error("Error reading " + ConfigFile + ": " + |
| 2113 | ec.message()); |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 2114 | } |
| 2115 | DEBUG(llvm::dbgs() << "Using configuration file " << ConfigFile << "\n"); |
| 2116 | return Style; |
| 2117 | } |
| 2118 | } |
Antonio Maiorano | 3adfb6a | 2017-01-17 00:12:27 +0000 | [diff] [blame] | 2119 | if (!UnsuitableConfigFiles.empty()) |
| 2120 | return make_string_error("Configuration file(s) do(es) not support " + |
| 2121 | getLanguageName(Style.Language) + ": " + |
| 2122 | UnsuitableConfigFiles); |
Antonio Maiorano | 7eb7507 | 2017-01-20 01:22:42 +0000 | [diff] [blame] | 2123 | return FallbackStyle; |
Edwin Vane | d544aa7 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 2124 | } |
| 2125 | |
Daniel Jasper | 8d1832e | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 2126 | } // namespace format |
| 2127 | } // namespace clang |