Format.cpp 66 KB

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