Format.cpp 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815
  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. #define DEBUG_TYPE "format-formatter"
  16. #include "BreakableToken.h"
  17. #include "TokenAnnotator.h"
  18. #include "UnwrappedLineParser.h"
  19. #include "WhitespaceManager.h"
  20. #include "clang/Basic/Diagnostic.h"
  21. #include "clang/Basic/OperatorPrecedence.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/YAMLTraits.h"
  29. #include <queue>
  30. #include <string>
  31. namespace llvm {
  32. namespace yaml {
  33. template <>
  34. struct ScalarEnumerationTraits<clang::format::FormatStyle::LanguageStandard> {
  35. static void enumeration(IO &IO,
  36. clang::format::FormatStyle::LanguageStandard &Value) {
  37. IO.enumCase(Value, "C++03", clang::format::FormatStyle::LS_Cpp03);
  38. IO.enumCase(Value, "C++11", clang::format::FormatStyle::LS_Cpp11);
  39. IO.enumCase(Value, "Auto", clang::format::FormatStyle::LS_Auto);
  40. }
  41. };
  42. template <>
  43. struct ScalarEnumerationTraits<clang::format::FormatStyle::BraceBreakingStyle> {
  44. static void
  45. enumeration(IO &IO, clang::format::FormatStyle::BraceBreakingStyle &Value) {
  46. IO.enumCase(Value, "Attach", clang::format::FormatStyle::BS_Attach);
  47. IO.enumCase(Value, "Linux", clang::format::FormatStyle::BS_Linux);
  48. IO.enumCase(Value, "Stroustrup", clang::format::FormatStyle::BS_Stroustrup);
  49. }
  50. };
  51. template <> struct MappingTraits<clang::format::FormatStyle> {
  52. static void mapping(llvm::yaml::IO &IO, clang::format::FormatStyle &Style) {
  53. if (IO.outputting()) {
  54. StringRef StylesArray[] = { "LLVM", "Google", "Chromium", "Mozilla" };
  55. ArrayRef<StringRef> Styles(StylesArray);
  56. for (size_t i = 0, e = Styles.size(); i < e; ++i) {
  57. StringRef StyleName(Styles[i]);
  58. clang::format::FormatStyle PredefinedStyle;
  59. if (clang::format::getPredefinedStyle(StyleName, &PredefinedStyle) &&
  60. Style == PredefinedStyle) {
  61. IO.mapOptional("# BasedOnStyle", StyleName);
  62. break;
  63. }
  64. }
  65. } else {
  66. StringRef BasedOnStyle;
  67. IO.mapOptional("BasedOnStyle", BasedOnStyle);
  68. if (!BasedOnStyle.empty())
  69. if (!clang::format::getPredefinedStyle(BasedOnStyle, &Style)) {
  70. IO.setError(Twine("Unknown value for BasedOnStyle: ", BasedOnStyle));
  71. return;
  72. }
  73. }
  74. IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset);
  75. IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft);
  76. IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine",
  77. Style.AllowAllParametersOfDeclarationOnNextLine);
  78. IO.mapOptional("AllowShortIfStatementsOnASingleLine",
  79. Style.AllowShortIfStatementsOnASingleLine);
  80. IO.mapOptional("AllowShortLoopsOnASingleLine",
  81. Style.AllowShortLoopsOnASingleLine);
  82. IO.mapOptional("AlwaysBreakTemplateDeclarations",
  83. Style.AlwaysBreakTemplateDeclarations);
  84. IO.mapOptional("AlwaysBreakBeforeMultilineStrings",
  85. Style.AlwaysBreakBeforeMultilineStrings);
  86. IO.mapOptional("BinPackParameters", Style.BinPackParameters);
  87. IO.mapOptional("ColumnLimit", Style.ColumnLimit);
  88. IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine",
  89. Style.ConstructorInitializerAllOnOneLineOrOnePerLine);
  90. IO.mapOptional("DerivePointerBinding", Style.DerivePointerBinding);
  91. IO.mapOptional("ExperimentalAutoDetectBinPacking",
  92. Style.ExperimentalAutoDetectBinPacking);
  93. IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels);
  94. IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep);
  95. IO.mapOptional("ObjCSpaceBeforeProtocolList",
  96. Style.ObjCSpaceBeforeProtocolList);
  97. IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment);
  98. IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString);
  99. IO.mapOptional("PenaltyBreakFirstLessLess",
  100. Style.PenaltyBreakFirstLessLess);
  101. IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter);
  102. IO.mapOptional("PenaltyReturnTypeOnItsOwnLine",
  103. Style.PenaltyReturnTypeOnItsOwnLine);
  104. IO.mapOptional("PointerBindsToType", Style.PointerBindsToType);
  105. IO.mapOptional("SpacesBeforeTrailingComments",
  106. Style.SpacesBeforeTrailingComments);
  107. IO.mapOptional("SpacesInBracedLists", Style.SpacesInBracedLists);
  108. IO.mapOptional("Standard", Style.Standard);
  109. IO.mapOptional("IndentWidth", Style.IndentWidth);
  110. IO.mapOptional("UseTab", Style.UseTab);
  111. IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces);
  112. IO.mapOptional("IndentFunctionDeclarationAfterType",
  113. Style.IndentFunctionDeclarationAfterType);
  114. }
  115. };
  116. }
  117. }
  118. namespace clang {
  119. namespace format {
  120. void setDefaultPenalties(FormatStyle &Style) {
  121. Style.PenaltyBreakComment = 45;
  122. Style.PenaltyBreakFirstLessLess = 100;
  123. Style.PenaltyBreakString = 1000;
  124. Style.PenaltyExcessCharacter = 1000000;
  125. }
  126. FormatStyle getLLVMStyle() {
  127. FormatStyle LLVMStyle;
  128. LLVMStyle.AccessModifierOffset = -2;
  129. LLVMStyle.AlignEscapedNewlinesLeft = false;
  130. LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
  131. LLVMStyle.AllowShortIfStatementsOnASingleLine = false;
  132. LLVMStyle.AllowShortLoopsOnASingleLine = false;
  133. LLVMStyle.AlwaysBreakTemplateDeclarations = false;
  134. LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
  135. LLVMStyle.BinPackParameters = true;
  136. LLVMStyle.ColumnLimit = 80;
  137. LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
  138. LLVMStyle.DerivePointerBinding = false;
  139. LLVMStyle.ExperimentalAutoDetectBinPacking = false;
  140. LLVMStyle.IndentCaseLabels = false;
  141. LLVMStyle.MaxEmptyLinesToKeep = 1;
  142. LLVMStyle.ObjCSpaceBeforeProtocolList = true;
  143. LLVMStyle.PointerBindsToType = false;
  144. LLVMStyle.SpacesBeforeTrailingComments = 1;
  145. LLVMStyle.SpacesInBracedLists = true;
  146. LLVMStyle.Standard = FormatStyle::LS_Cpp03;
  147. LLVMStyle.IndentWidth = 2;
  148. LLVMStyle.UseTab = false;
  149. LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
  150. LLVMStyle.IndentFunctionDeclarationAfterType = false;
  151. setDefaultPenalties(LLVMStyle);
  152. LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
  153. return LLVMStyle;
  154. }
  155. FormatStyle getGoogleStyle() {
  156. FormatStyle GoogleStyle;
  157. GoogleStyle.AccessModifierOffset = -1;
  158. GoogleStyle.AlignEscapedNewlinesLeft = true;
  159. GoogleStyle.AllowAllParametersOfDeclarationOnNextLine = true;
  160. GoogleStyle.AllowShortIfStatementsOnASingleLine = true;
  161. GoogleStyle.AllowShortLoopsOnASingleLine = true;
  162. GoogleStyle.AlwaysBreakTemplateDeclarations = true;
  163. GoogleStyle.AlwaysBreakBeforeMultilineStrings = true;
  164. GoogleStyle.BinPackParameters = true;
  165. GoogleStyle.ColumnLimit = 80;
  166. GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  167. GoogleStyle.DerivePointerBinding = true;
  168. GoogleStyle.ExperimentalAutoDetectBinPacking = false;
  169. GoogleStyle.IndentCaseLabels = true;
  170. GoogleStyle.MaxEmptyLinesToKeep = 1;
  171. GoogleStyle.ObjCSpaceBeforeProtocolList = false;
  172. GoogleStyle.PointerBindsToType = true;
  173. GoogleStyle.SpacesBeforeTrailingComments = 2;
  174. GoogleStyle.SpacesInBracedLists = false;
  175. GoogleStyle.Standard = FormatStyle::LS_Auto;
  176. GoogleStyle.IndentWidth = 2;
  177. GoogleStyle.UseTab = false;
  178. GoogleStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
  179. GoogleStyle.IndentFunctionDeclarationAfterType = true;
  180. setDefaultPenalties(GoogleStyle);
  181. GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
  182. return GoogleStyle;
  183. }
  184. FormatStyle getChromiumStyle() {
  185. FormatStyle ChromiumStyle = getGoogleStyle();
  186. ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false;
  187. ChromiumStyle.AllowShortIfStatementsOnASingleLine = false;
  188. ChromiumStyle.AllowShortLoopsOnASingleLine = false;
  189. ChromiumStyle.BinPackParameters = false;
  190. ChromiumStyle.Standard = FormatStyle::LS_Cpp03;
  191. ChromiumStyle.DerivePointerBinding = false;
  192. return ChromiumStyle;
  193. }
  194. FormatStyle getMozillaStyle() {
  195. FormatStyle MozillaStyle = getLLVMStyle();
  196. MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false;
  197. MozillaStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  198. MozillaStyle.DerivePointerBinding = true;
  199. MozillaStyle.IndentCaseLabels = true;
  200. MozillaStyle.ObjCSpaceBeforeProtocolList = false;
  201. MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200;
  202. MozillaStyle.PointerBindsToType = true;
  203. return MozillaStyle;
  204. }
  205. bool getPredefinedStyle(StringRef Name, FormatStyle *Style) {
  206. if (Name.equals_lower("llvm"))
  207. *Style = getLLVMStyle();
  208. else if (Name.equals_lower("chromium"))
  209. *Style = getChromiumStyle();
  210. else if (Name.equals_lower("mozilla"))
  211. *Style = getMozillaStyle();
  212. else if (Name.equals_lower("google"))
  213. *Style = getGoogleStyle();
  214. else
  215. return false;
  216. return true;
  217. }
  218. llvm::error_code parseConfiguration(StringRef Text, FormatStyle *Style) {
  219. if (Text.trim().empty())
  220. return llvm::make_error_code(llvm::errc::invalid_argument);
  221. llvm::yaml::Input Input(Text);
  222. Input >> *Style;
  223. return Input.error();
  224. }
  225. std::string configurationAsText(const FormatStyle &Style) {
  226. std::string Text;
  227. llvm::raw_string_ostream Stream(Text);
  228. llvm::yaml::Output Output(Stream);
  229. // We use the same mapping method for input and output, so we need a non-const
  230. // reference here.
  231. FormatStyle NonConstStyle = Style;
  232. Output << NonConstStyle;
  233. return Stream.str();
  234. }
  235. // Returns the length of everything up to the first possible line break after
  236. // the ), ], } or > matching \c Tok.
  237. static unsigned getLengthToMatchingParen(const FormatToken &Tok) {
  238. if (Tok.MatchingParen == NULL)
  239. return 0;
  240. FormatToken *End = Tok.MatchingParen;
  241. while (End->Next && !End->Next->CanBreakBefore) {
  242. End = End->Next;
  243. }
  244. return End->TotalLength - Tok.TotalLength + 1;
  245. }
  246. namespace {
  247. class UnwrappedLineFormatter {
  248. public:
  249. UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr,
  250. const AnnotatedLine &Line, unsigned FirstIndent,
  251. const FormatToken *RootToken,
  252. WhitespaceManager &Whitespaces,
  253. encoding::Encoding Encoding,
  254. bool BinPackInconclusiveFunctions)
  255. : Style(Style), SourceMgr(SourceMgr), Line(Line),
  256. FirstIndent(FirstIndent), RootToken(RootToken),
  257. Whitespaces(Whitespaces), Count(0), Encoding(Encoding),
  258. BinPackInconclusiveFunctions(BinPackInconclusiveFunctions) {}
  259. /// \brief Formats an \c UnwrappedLine.
  260. void format(const AnnotatedLine *NextLine) {
  261. // Initialize state dependent on indent.
  262. LineState State;
  263. State.Column = FirstIndent;
  264. State.NextToken = RootToken;
  265. State.Stack.push_back(ParenState(FirstIndent, FirstIndent,
  266. /*AvoidBinPacking=*/false,
  267. /*NoLineBreak=*/false));
  268. State.LineContainsContinuedForLoopSection = false;
  269. State.ParenLevel = 0;
  270. State.StartOfStringLiteral = 0;
  271. State.StartOfLineLevel = State.ParenLevel;
  272. State.LowestLevelOnLine = State.ParenLevel;
  273. State.IgnoreStackForComparison = false;
  274. // The first token has already been indented and thus consumed.
  275. moveStateToNextToken(State, /*DryRun=*/false, /*Newline=*/false);
  276. // If everything fits on a single line, just put it there.
  277. unsigned ColumnLimit = Style.ColumnLimit;
  278. if (NextLine && NextLine->InPPDirective &&
  279. !NextLine->First->HasUnescapedNewline)
  280. ColumnLimit = getColumnLimit();
  281. if (Line.Last->TotalLength <= ColumnLimit - FirstIndent) {
  282. while (State.NextToken != NULL) {
  283. addTokenToState(false, false, State);
  284. }
  285. }
  286. // If the ObjC method declaration does not fit on a line, we should format
  287. // it with one arg per line.
  288. if (Line.Type == LT_ObjCMethodDecl)
  289. State.Stack.back().BreakBeforeParameter = true;
  290. // Find best solution in solution space.
  291. analyzeSolutionSpace(State);
  292. }
  293. private:
  294. void DebugTokenState(const FormatToken &FormatTok) {
  295. const Token &Tok = FormatTok.Tok;
  296. llvm::dbgs() << StringRef(SourceMgr.getCharacterData(Tok.getLocation()),
  297. Tok.getLength());
  298. llvm::dbgs();
  299. }
  300. struct ParenState {
  301. ParenState(unsigned Indent, unsigned LastSpace, bool AvoidBinPacking,
  302. bool NoLineBreak)
  303. : Indent(Indent), LastSpace(LastSpace), FirstLessLess(0),
  304. BreakBeforeClosingBrace(false), QuestionColumn(0),
  305. AvoidBinPacking(AvoidBinPacking), BreakBeforeParameter(false),
  306. NoLineBreak(NoLineBreak), ColonPos(0), StartOfFunctionCall(0),
  307. StartOfArraySubscripts(0), NestedNameSpecifierContinuation(0),
  308. CallContinuation(0), VariablePos(0), ContainsLineBreak(false) {}
  309. /// \brief The position to which a specific parenthesis level needs to be
  310. /// indented.
  311. unsigned Indent;
  312. /// \brief The position of the last space on each level.
  313. ///
  314. /// Used e.g. to break like:
  315. /// functionCall(Parameter, otherCall(
  316. /// OtherParameter));
  317. unsigned LastSpace;
  318. /// \brief The position the first "<<" operator encountered on each level.
  319. ///
  320. /// Used to align "<<" operators. 0 if no such operator has been encountered
  321. /// on a level.
  322. unsigned FirstLessLess;
  323. /// \brief Whether a newline needs to be inserted before the block's closing
  324. /// brace.
  325. ///
  326. /// We only want to insert a newline before the closing brace if there also
  327. /// was a newline after the beginning left brace.
  328. bool BreakBeforeClosingBrace;
  329. /// \brief The column of a \c ? in a conditional expression;
  330. unsigned QuestionColumn;
  331. /// \brief Avoid bin packing, i.e. multiple parameters/elements on multiple
  332. /// lines, in this context.
  333. bool AvoidBinPacking;
  334. /// \brief Break after the next comma (or all the commas in this context if
  335. /// \c AvoidBinPacking is \c true).
  336. bool BreakBeforeParameter;
  337. /// \brief Line breaking in this context would break a formatting rule.
  338. bool NoLineBreak;
  339. /// \brief The position of the colon in an ObjC method declaration/call.
  340. unsigned ColonPos;
  341. /// \brief The start of the most recent function in a builder-type call.
  342. unsigned StartOfFunctionCall;
  343. /// \brief Contains the start of array subscript expressions, so that they
  344. /// can be aligned.
  345. unsigned StartOfArraySubscripts;
  346. /// \brief If a nested name specifier was broken over multiple lines, this
  347. /// contains the start column of the second line. Otherwise 0.
  348. unsigned NestedNameSpecifierContinuation;
  349. /// \brief If a call expression was broken over multiple lines, this
  350. /// contains the start column of the second line. Otherwise 0.
  351. unsigned CallContinuation;
  352. /// \brief The column of the first variable name in a variable declaration.
  353. ///
  354. /// Used to align further variables if necessary.
  355. unsigned VariablePos;
  356. /// \brief \c true if this \c ParenState already contains a line-break.
  357. ///
  358. /// The first line break in a certain \c ParenState causes extra penalty so
  359. /// that clang-format prefers similar breaks, i.e. breaks in the same
  360. /// parenthesis.
  361. bool ContainsLineBreak;
  362. bool operator<(const ParenState &Other) const {
  363. if (Indent != Other.Indent)
  364. return Indent < Other.Indent;
  365. if (LastSpace != Other.LastSpace)
  366. return LastSpace < Other.LastSpace;
  367. if (FirstLessLess != Other.FirstLessLess)
  368. return FirstLessLess < Other.FirstLessLess;
  369. if (BreakBeforeClosingBrace != Other.BreakBeforeClosingBrace)
  370. return BreakBeforeClosingBrace;
  371. if (QuestionColumn != Other.QuestionColumn)
  372. return QuestionColumn < Other.QuestionColumn;
  373. if (AvoidBinPacking != Other.AvoidBinPacking)
  374. return AvoidBinPacking;
  375. if (BreakBeforeParameter != Other.BreakBeforeParameter)
  376. return BreakBeforeParameter;
  377. if (NoLineBreak != Other.NoLineBreak)
  378. return NoLineBreak;
  379. if (ColonPos != Other.ColonPos)
  380. return ColonPos < Other.ColonPos;
  381. if (StartOfFunctionCall != Other.StartOfFunctionCall)
  382. return StartOfFunctionCall < Other.StartOfFunctionCall;
  383. if (StartOfArraySubscripts != Other.StartOfArraySubscripts)
  384. return StartOfArraySubscripts < Other.StartOfArraySubscripts;
  385. if (CallContinuation != Other.CallContinuation)
  386. return CallContinuation < Other.CallContinuation;
  387. if (VariablePos != Other.VariablePos)
  388. return VariablePos < Other.VariablePos;
  389. if (ContainsLineBreak != Other.ContainsLineBreak)
  390. return ContainsLineBreak < Other.ContainsLineBreak;
  391. return false;
  392. }
  393. };
  394. /// \brief The current state when indenting a unwrapped line.
  395. ///
  396. /// As the indenting tries different combinations this is copied by value.
  397. struct LineState {
  398. /// \brief The number of used columns in the current line.
  399. unsigned Column;
  400. /// \brief The token that needs to be next formatted.
  401. const FormatToken *NextToken;
  402. /// \brief \c true if this line contains a continued for-loop section.
  403. bool LineContainsContinuedForLoopSection;
  404. /// \brief The level of nesting inside (), [], <> and {}.
  405. unsigned ParenLevel;
  406. /// \brief The \c ParenLevel at the start of this line.
  407. unsigned StartOfLineLevel;
  408. /// \brief The lowest \c ParenLevel on the current line.
  409. unsigned LowestLevelOnLine;
  410. /// \brief The start column of the string literal, if we're in a string
  411. /// literal sequence, 0 otherwise.
  412. unsigned StartOfStringLiteral;
  413. /// \brief A stack keeping track of properties applying to parenthesis
  414. /// levels.
  415. std::vector<ParenState> Stack;
  416. /// \brief Ignore the stack of \c ParenStates for state comparison.
  417. ///
  418. /// In long and deeply nested unwrapped lines, the current algorithm can
  419. /// be insufficient for finding the best formatting with a reasonable amount
  420. /// of time and memory. Setting this flag will effectively lead to the
  421. /// algorithm not analyzing some combinations. However, these combinations
  422. /// rarely contain the optimal solution: In short, accepting a higher
  423. /// penalty early would need to lead to different values in the \c
  424. /// ParenState stack (in an otherwise identical state) and these different
  425. /// values would need to lead to a significant amount of avoided penalty
  426. /// later.
  427. ///
  428. /// FIXME: Come up with a better algorithm instead.
  429. bool IgnoreStackForComparison;
  430. /// \brief Comparison operator to be able to used \c LineState in \c map.
  431. bool operator<(const LineState &Other) const {
  432. if (NextToken != Other.NextToken)
  433. return NextToken < Other.NextToken;
  434. if (Column != Other.Column)
  435. return Column < Other.Column;
  436. if (LineContainsContinuedForLoopSection !=
  437. Other.LineContainsContinuedForLoopSection)
  438. return LineContainsContinuedForLoopSection;
  439. if (ParenLevel != Other.ParenLevel)
  440. return ParenLevel < Other.ParenLevel;
  441. if (StartOfLineLevel != Other.StartOfLineLevel)
  442. return StartOfLineLevel < Other.StartOfLineLevel;
  443. if (LowestLevelOnLine != Other.LowestLevelOnLine)
  444. return LowestLevelOnLine < Other.LowestLevelOnLine;
  445. if (StartOfStringLiteral != Other.StartOfStringLiteral)
  446. return StartOfStringLiteral < Other.StartOfStringLiteral;
  447. if (IgnoreStackForComparison || Other.IgnoreStackForComparison)
  448. return false;
  449. return Stack < Other.Stack;
  450. }
  451. };
  452. /// \brief Appends the next token to \p State and updates information
  453. /// necessary for indentation.
  454. ///
  455. /// Puts the token on the current line if \p Newline is \c false and adds a
  456. /// line break and necessary indentation otherwise.
  457. ///
  458. /// If \p DryRun is \c false, also creates and stores the required
  459. /// \c Replacement.
  460. unsigned addTokenToState(bool Newline, bool DryRun, LineState &State) {
  461. const FormatToken &Current = *State.NextToken;
  462. const FormatToken &Previous = *State.NextToken->Previous;
  463. // Extra penalty that needs to be added because of the way certain line
  464. // breaks are chosen.
  465. unsigned ExtraPenalty = 0;
  466. if (State.Stack.size() == 0 || Current.Type == TT_ImplicitStringLiteral) {
  467. // FIXME: Is this correct?
  468. int WhitespaceLength = SourceMgr.getSpellingColumnNumber(
  469. State.NextToken->WhitespaceRange.getEnd()) -
  470. SourceMgr.getSpellingColumnNumber(
  471. State.NextToken->WhitespaceRange.getBegin());
  472. State.Column += WhitespaceLength + State.NextToken->CodePointCount;
  473. State.NextToken = State.NextToken->Next;
  474. return 0;
  475. }
  476. // If we are continuing an expression, we want to indent an extra 4 spaces.
  477. unsigned ContinuationIndent =
  478. std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) + 4;
  479. if (Newline) {
  480. State.Stack.back().ContainsLineBreak = true;
  481. if (Current.is(tok::r_brace)) {
  482. if (Current.BlockKind == BK_BracedInit)
  483. State.Column = State.Stack[State.Stack.size() - 2].LastSpace;
  484. else
  485. State.Column = FirstIndent;
  486. } else if (Current.is(tok::string_literal) &&
  487. State.StartOfStringLiteral != 0) {
  488. State.Column = State.StartOfStringLiteral;
  489. State.Stack.back().BreakBeforeParameter = true;
  490. } else if (Current.is(tok::lessless) &&
  491. State.Stack.back().FirstLessLess != 0) {
  492. State.Column = State.Stack.back().FirstLessLess;
  493. } else if (Current.isOneOf(tok::period, tok::arrow) &&
  494. Current.Type != TT_DesignatedInitializerPeriod) {
  495. if (State.Stack.back().CallContinuation == 0) {
  496. State.Column = ContinuationIndent;
  497. State.Stack.back().CallContinuation = State.Column;
  498. } else {
  499. State.Column = State.Stack.back().CallContinuation;
  500. }
  501. } else if (Current.Type == TT_ConditionalExpr) {
  502. State.Column = State.Stack.back().QuestionColumn;
  503. } else if (Previous.is(tok::comma) &&
  504. State.Stack.back().VariablePos != 0) {
  505. State.Column = State.Stack.back().VariablePos;
  506. } else if (Previous.ClosesTemplateDeclaration ||
  507. ((Current.Type == TT_StartOfName ||
  508. Current.is(tok::kw_operator)) &&
  509. State.ParenLevel == 0 &&
  510. (!Style.IndentFunctionDeclarationAfterType ||
  511. Line.StartsDefinition))) {
  512. State.Column = State.Stack.back().Indent;
  513. } else if (Current.Type == TT_ObjCSelectorName) {
  514. if (State.Stack.back().ColonPos > Current.CodePointCount) {
  515. State.Column = State.Stack.back().ColonPos - Current.CodePointCount;
  516. } else {
  517. State.Column = State.Stack.back().Indent;
  518. State.Stack.back().ColonPos = State.Column + Current.CodePointCount;
  519. }
  520. } else if (Current.is(tok::l_square) &&
  521. Current.Type != TT_ObjCMethodExpr) {
  522. if (State.Stack.back().StartOfArraySubscripts != 0)
  523. State.Column = State.Stack.back().StartOfArraySubscripts;
  524. else
  525. State.Column = ContinuationIndent;
  526. } else if (Current.Type == TT_StartOfName ||
  527. Previous.isOneOf(tok::coloncolon, tok::equal) ||
  528. Previous.Type == TT_ObjCMethodExpr) {
  529. State.Column = ContinuationIndent;
  530. } else {
  531. State.Column = State.Stack.back().Indent;
  532. // Ensure that we fall back to indenting 4 spaces instead of just
  533. // flushing continuations left.
  534. if (State.Column == FirstIndent)
  535. State.Column += 4;
  536. }
  537. if (Current.is(tok::question))
  538. State.Stack.back().BreakBeforeParameter = true;
  539. if ((Previous.isOneOf(tok::comma, tok::semi) &&
  540. !State.Stack.back().AvoidBinPacking) ||
  541. Previous.Type == TT_BinaryOperator)
  542. State.Stack.back().BreakBeforeParameter = false;
  543. if (Previous.Type == TT_TemplateCloser && State.ParenLevel == 0)
  544. State.Stack.back().BreakBeforeParameter = false;
  545. if (!DryRun) {
  546. unsigned NewLines = 1;
  547. if (Current.is(tok::comment))
  548. NewLines = std::max(
  549. NewLines,
  550. std::min(Current.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1));
  551. Whitespaces.replaceWhitespace(Current, NewLines, State.Column,
  552. State.Column, Line.InPPDirective);
  553. }
  554. if (!Current.isTrailingComment())
  555. State.Stack.back().LastSpace = State.Column;
  556. if (Current.isOneOf(tok::arrow, tok::period) &&
  557. Current.Type != TT_DesignatedInitializerPeriod)
  558. State.Stack.back().LastSpace += Current.CodePointCount;
  559. State.StartOfLineLevel = State.ParenLevel;
  560. State.LowestLevelOnLine = State.ParenLevel;
  561. // Any break on this level means that the parent level has been broken
  562. // and we need to avoid bin packing there.
  563. for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) {
  564. State.Stack[i].BreakBeforeParameter = true;
  565. }
  566. const FormatToken *TokenBefore = Current.getPreviousNonComment();
  567. if (TokenBefore && !TokenBefore->isOneOf(tok::comma, tok::semi) &&
  568. TokenBefore->Type != TT_TemplateCloser &&
  569. TokenBefore->Type != TT_BinaryOperator && !TokenBefore->opensScope())
  570. State.Stack.back().BreakBeforeParameter = true;
  571. // If we break after {, we should also break before the corresponding }.
  572. if (Previous.is(tok::l_brace))
  573. State.Stack.back().BreakBeforeClosingBrace = true;
  574. if (State.Stack.back().AvoidBinPacking) {
  575. // If we are breaking after '(', '{', '<', this is not bin packing
  576. // unless AllowAllParametersOfDeclarationOnNextLine is false.
  577. if (!(Previous.isOneOf(tok::l_paren, tok::l_brace) ||
  578. Previous.Type == TT_BinaryOperator) ||
  579. (!Style.AllowAllParametersOfDeclarationOnNextLine &&
  580. Line.MustBeDeclaration))
  581. State.Stack.back().BreakBeforeParameter = true;
  582. }
  583. // Breaking before the first "<<" is generally not desirable.
  584. if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0)
  585. ExtraPenalty += Style.PenaltyBreakFirstLessLess;
  586. } else {
  587. if (Current.is(tok::equal) &&
  588. (RootToken->is(tok::kw_for) || State.ParenLevel == 0) &&
  589. State.Stack.back().VariablePos == 0) {
  590. State.Stack.back().VariablePos = State.Column;
  591. // Move over * and & if they are bound to the variable name.
  592. const FormatToken *Tok = &Previous;
  593. while (Tok && State.Stack.back().VariablePos >= Tok->CodePointCount) {
  594. State.Stack.back().VariablePos -= Tok->CodePointCount;
  595. if (Tok->SpacesRequiredBefore != 0)
  596. break;
  597. Tok = Tok->Previous;
  598. }
  599. if (Previous.PartOfMultiVariableDeclStmt)
  600. State.Stack.back().LastSpace = State.Stack.back().VariablePos;
  601. }
  602. unsigned Spaces = State.NextToken->SpacesRequiredBefore;
  603. if (!DryRun)
  604. Whitespaces.replaceWhitespace(Current, 0, Spaces,
  605. State.Column + Spaces);
  606. if (Current.Type == TT_ObjCSelectorName &&
  607. State.Stack.back().ColonPos == 0) {
  608. if (State.Stack.back().Indent + Current.LongestObjCSelectorName >
  609. State.Column + Spaces + Current.CodePointCount)
  610. State.Stack.back().ColonPos =
  611. State.Stack.back().Indent + Current.LongestObjCSelectorName;
  612. else
  613. State.Stack.back().ColonPos =
  614. State.Column + Spaces + Current.CodePointCount;
  615. }
  616. if (Previous.opensScope() && Previous.Type != TT_ObjCMethodExpr &&
  617. Current.Type != TT_LineComment)
  618. State.Stack.back().Indent = State.Column + Spaces;
  619. if (Previous.is(tok::comma) && !Current.isTrailingComment() &&
  620. State.Stack.back().AvoidBinPacking)
  621. State.Stack.back().NoLineBreak = true;
  622. State.Column += Spaces;
  623. if (Current.is(tok::l_paren) && Previous.isOneOf(tok::kw_if, tok::kw_for))
  624. // Treat the condition inside an if as if it was a second function
  625. // parameter, i.e. let nested calls have an indent of 4.
  626. State.Stack.back().LastSpace = State.Column + 1; // 1 is length of "(".
  627. else if (Previous.is(tok::comma))
  628. State.Stack.back().LastSpace = State.Column;
  629. else if ((Previous.Type == TT_BinaryOperator ||
  630. Previous.Type == TT_ConditionalExpr ||
  631. Previous.Type == TT_CtorInitializerColon) &&
  632. !(Previous.getPrecedence() == prec::Assignment &&
  633. Current.FakeLParens.empty()))
  634. // Always indent relative to the RHS of the expression unless this is a
  635. // simple assignment without binary expression on the RHS.
  636. State.Stack.back().LastSpace = State.Column;
  637. else if (Previous.Type == TT_InheritanceColon)
  638. State.Stack.back().Indent = State.Column;
  639. else if (Previous.opensScope()) {
  640. // If a function has multiple parameters (including a single parameter
  641. // that is a binary expression) or a trailing call, indent all
  642. // parameters from the opening parenthesis. This avoids confusing
  643. // indents like:
  644. // OuterFunction(InnerFunctionCall(
  645. // ParameterToInnerFunction),
  646. // SecondParameterToOuterFunction);
  647. bool HasMultipleParameters = !Current.FakeLParens.empty();
  648. bool HasTrailingCall = false;
  649. if (Previous.MatchingParen) {
  650. const FormatToken *Next = Previous.MatchingParen->getNextNonComment();
  651. if (Next && Next->isOneOf(tok::period, tok::arrow))
  652. HasTrailingCall = true;
  653. }
  654. if (HasMultipleParameters || HasTrailingCall)
  655. State.Stack.back().LastSpace = State.Column;
  656. }
  657. }
  658. return moveStateToNextToken(State, DryRun, Newline) + ExtraPenalty;
  659. }
  660. /// \brief Mark the next token as consumed in \p State and modify its stacks
  661. /// accordingly.
  662. unsigned moveStateToNextToken(LineState &State, bool DryRun, bool Newline) {
  663. const FormatToken &Current = *State.NextToken;
  664. assert(State.Stack.size());
  665. if (Current.Type == TT_InheritanceColon)
  666. State.Stack.back().AvoidBinPacking = true;
  667. if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0)
  668. State.Stack.back().FirstLessLess = State.Column;
  669. if (Current.is(tok::l_square) &&
  670. State.Stack.back().StartOfArraySubscripts == 0)
  671. State.Stack.back().StartOfArraySubscripts = State.Column;
  672. if (Current.is(tok::question))
  673. State.Stack.back().QuestionColumn = State.Column;
  674. if (!Current.opensScope() && !Current.closesScope())
  675. State.LowestLevelOnLine =
  676. std::min(State.LowestLevelOnLine, State.ParenLevel);
  677. if (Current.isOneOf(tok::period, tok::arrow) &&
  678. Line.Type == LT_BuilderTypeCall && State.ParenLevel == 0)
  679. State.Stack.back().StartOfFunctionCall =
  680. Current.LastInChainOfCalls ? 0
  681. : State.Column + Current.CodePointCount;
  682. if (Current.Type == TT_CtorInitializerColon) {
  683. // Indent 2 from the column, so:
  684. // SomeClass::SomeClass()
  685. // : First(...), ...
  686. // Next(...)
  687. // ^ line up here.
  688. State.Stack.back().Indent = State.Column + 2;
  689. if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
  690. State.Stack.back().AvoidBinPacking = true;
  691. State.Stack.back().BreakBeforeParameter = false;
  692. }
  693. // If return returns a binary expression, align after it.
  694. if (Current.is(tok::kw_return) && !Current.FakeLParens.empty())
  695. State.Stack.back().LastSpace = State.Column + 7;
  696. // In ObjC method declaration we align on the ":" of parameters, but we need
  697. // to ensure that we indent parameters on subsequent lines by at least 4.
  698. if (Current.Type == TT_ObjCMethodSpecifier)
  699. State.Stack.back().Indent += 4;
  700. // Insert scopes created by fake parenthesis.
  701. const FormatToken *Previous = Current.getPreviousNonComment();
  702. // Don't add extra indentation for the first fake parenthesis after
  703. // 'return', assignements or opening <({[. The indentation for these cases
  704. // is special cased.
  705. bool SkipFirstExtraIndent =
  706. Current.is(tok::kw_return) ||
  707. (Previous && (Previous->opensScope() ||
  708. Previous->getPrecedence() == prec::Assignment));
  709. for (SmallVectorImpl<prec::Level>::const_reverse_iterator
  710. I = Current.FakeLParens.rbegin(),
  711. E = Current.FakeLParens.rend();
  712. I != E; ++I) {
  713. ParenState NewParenState = State.Stack.back();
  714. NewParenState.ContainsLineBreak = false;
  715. NewParenState.Indent =
  716. std::max(std::max(State.Column, NewParenState.Indent),
  717. State.Stack.back().LastSpace);
  718. // Always indent conditional expressions. Never indent expression where
  719. // the 'operator' is ',', ';' or an assignment (i.e. *I <=
  720. // prec::Assignment) as those have different indentation rules. Indent
  721. // other expression, unless the indentation needs to be skipped.
  722. if (*I == prec::Conditional ||
  723. (!SkipFirstExtraIndent && *I > prec::Assignment))
  724. NewParenState.Indent += 4;
  725. if (Previous && !Previous->opensScope())
  726. NewParenState.BreakBeforeParameter = false;
  727. State.Stack.push_back(NewParenState);
  728. SkipFirstExtraIndent = false;
  729. }
  730. // If we encounter an opening (, [, { or <, we add a level to our stacks to
  731. // prepare for the following tokens.
  732. if (Current.opensScope()) {
  733. unsigned NewIndent;
  734. unsigned LastSpace = State.Stack.back().LastSpace;
  735. bool AvoidBinPacking;
  736. if (Current.is(tok::l_brace)) {
  737. NewIndent = Style.IndentWidth + LastSpace;
  738. const FormatToken *NextNoComment = Current.getNextNonComment();
  739. AvoidBinPacking = NextNoComment &&
  740. NextNoComment->Type == TT_DesignatedInitializerPeriod;
  741. } else {
  742. NewIndent =
  743. 4 + std::max(LastSpace, State.Stack.back().StartOfFunctionCall);
  744. AvoidBinPacking = !Style.BinPackParameters ||
  745. (Style.ExperimentalAutoDetectBinPacking &&
  746. (Current.PackingKind == PPK_OnePerLine ||
  747. (!BinPackInconclusiveFunctions &&
  748. Current.PackingKind == PPK_Inconclusive)));
  749. }
  750. State.Stack.push_back(ParenState(NewIndent, LastSpace, AvoidBinPacking,
  751. State.Stack.back().NoLineBreak));
  752. ++State.ParenLevel;
  753. }
  754. // If this '[' opens an ObjC call, determine whether all parameters fit into
  755. // one line and put one per line if they don't.
  756. if (Current.is(tok::l_square) && Current.Type == TT_ObjCMethodExpr &&
  757. Current.MatchingParen != NULL) {
  758. if (getLengthToMatchingParen(Current) + State.Column > getColumnLimit())
  759. State.Stack.back().BreakBeforeParameter = true;
  760. }
  761. // If we encounter a closing ), ], } or >, we can remove a level from our
  762. // stacks.
  763. if (Current.isOneOf(tok::r_paren, tok::r_square) ||
  764. (Current.is(tok::r_brace) && State.NextToken != RootToken) ||
  765. State.NextToken->Type == TT_TemplateCloser) {
  766. State.Stack.pop_back();
  767. --State.ParenLevel;
  768. }
  769. if (Current.is(tok::r_square)) {
  770. // If this ends the array subscript expr, reset the corresponding value.
  771. const FormatToken *NextNonComment = Current.getNextNonComment();
  772. if (NextNonComment && NextNonComment->isNot(tok::l_square))
  773. State.Stack.back().StartOfArraySubscripts = 0;
  774. }
  775. // Remove scopes created by fake parenthesis.
  776. for (unsigned i = 0, e = Current.FakeRParens; i != e; ++i) {
  777. unsigned VariablePos = State.Stack.back().VariablePos;
  778. State.Stack.pop_back();
  779. State.Stack.back().VariablePos = VariablePos;
  780. }
  781. if (Current.is(tok::string_literal) && State.StartOfStringLiteral == 0) {
  782. State.StartOfStringLiteral = State.Column;
  783. } else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash,
  784. tok::string_literal)) {
  785. State.StartOfStringLiteral = 0;
  786. }
  787. State.Column += Current.CodePointCount;
  788. State.NextToken = State.NextToken->Next;
  789. if (!Newline && Style.AlwaysBreakBeforeMultilineStrings &&
  790. Current.is(tok::string_literal))
  791. return 0;
  792. return breakProtrudingToken(Current, State, DryRun);
  793. }
  794. /// \brief If the current token sticks out over the end of the line, break
  795. /// it if possible.
  796. ///
  797. /// \returns An extra penalty if a token was broken, otherwise 0.
  798. ///
  799. /// The returned penalty will cover the cost of the additional line breaks and
  800. /// column limit violation in all lines except for the last one. The penalty
  801. /// for the column limit violation in the last line (and in single line
  802. /// tokens) is handled in \c addNextStateToQueue.
  803. unsigned breakProtrudingToken(const FormatToken &Current, LineState &State,
  804. bool DryRun) {
  805. llvm::OwningPtr<BreakableToken> Token;
  806. unsigned StartColumn = State.Column - Current.CodePointCount;
  807. unsigned OriginalStartColumn =
  808. SourceMgr.getSpellingColumnNumber(Current.getStartOfNonWhitespace()) -
  809. 1;
  810. if (Current.is(tok::string_literal) &&
  811. Current.Type != TT_ImplicitStringLiteral) {
  812. // Only break up default narrow strings.
  813. if (!Current.TokenText.startswith("\""))
  814. return 0;
  815. Token.reset(new BreakableStringLiteral(Current, StartColumn,
  816. Line.InPPDirective, Encoding));
  817. } else if (Current.Type == TT_BlockComment) {
  818. Token.reset(new BreakableBlockComment(
  819. Style, Current, StartColumn, OriginalStartColumn, !Current.Previous,
  820. Line.InPPDirective, Encoding));
  821. } else if (Current.Type == TT_LineComment &&
  822. (Current.Previous == NULL ||
  823. Current.Previous->Type != TT_ImplicitStringLiteral)) {
  824. Token.reset(new BreakableLineComment(Current, StartColumn,
  825. Line.InPPDirective, Encoding));
  826. } else {
  827. return 0;
  828. }
  829. if (Current.UnbreakableTailLength >= getColumnLimit())
  830. return 0;
  831. unsigned RemainingSpace = getColumnLimit() - Current.UnbreakableTailLength;
  832. bool BreakInserted = false;
  833. unsigned Penalty = 0;
  834. unsigned RemainingTokenColumns = 0;
  835. for (unsigned LineIndex = 0, EndIndex = Token->getLineCount();
  836. LineIndex != EndIndex; ++LineIndex) {
  837. if (!DryRun)
  838. Token->replaceWhitespaceBefore(LineIndex, Whitespaces);
  839. unsigned TailOffset = 0;
  840. RemainingTokenColumns = Token->getLineLengthAfterSplit(
  841. LineIndex, TailOffset, StringRef::npos);
  842. while (RemainingTokenColumns > RemainingSpace) {
  843. BreakableToken::Split Split =
  844. Token->getSplit(LineIndex, TailOffset, getColumnLimit());
  845. if (Split.first == StringRef::npos) {
  846. // The last line's penalty is handled in addNextStateToQueue().
  847. if (LineIndex < EndIndex - 1)
  848. Penalty += Style.PenaltyExcessCharacter *
  849. (RemainingTokenColumns - RemainingSpace);
  850. break;
  851. }
  852. assert(Split.first != 0);
  853. unsigned NewRemainingTokenColumns = Token->getLineLengthAfterSplit(
  854. LineIndex, TailOffset + Split.first + Split.second,
  855. StringRef::npos);
  856. assert(NewRemainingTokenColumns < RemainingTokenColumns);
  857. if (!DryRun)
  858. Token->insertBreak(LineIndex, TailOffset, Split, Whitespaces);
  859. Penalty += Current.is(tok::string_literal) ? Style.PenaltyBreakString
  860. : Style.PenaltyBreakComment;
  861. unsigned ColumnsUsed =
  862. Token->getLineLengthAfterSplit(LineIndex, TailOffset, Split.first);
  863. if (ColumnsUsed > getColumnLimit()) {
  864. Penalty +=
  865. Style.PenaltyExcessCharacter * (ColumnsUsed - getColumnLimit());
  866. }
  867. TailOffset += Split.first + Split.second;
  868. RemainingTokenColumns = NewRemainingTokenColumns;
  869. BreakInserted = true;
  870. }
  871. }
  872. State.Column = RemainingTokenColumns;
  873. if (BreakInserted) {
  874. // If we break the token inside a parameter list, we need to break before
  875. // the next parameter on all levels, so that the next parameter is clearly
  876. // visible. Line comments already introduce a break.
  877. if (Current.Type != TT_LineComment) {
  878. for (unsigned i = 0, e = State.Stack.size(); i != e; ++i)
  879. State.Stack[i].BreakBeforeParameter = true;
  880. }
  881. State.Stack.back().LastSpace = StartColumn;
  882. }
  883. return Penalty;
  884. }
  885. unsigned getColumnLimit() {
  886. // In preprocessor directives reserve two chars for trailing " \"
  887. return Style.ColumnLimit - (Line.InPPDirective ? 2 : 0);
  888. }
  889. /// \brief An edge in the solution space from \c Previous->State to \c State,
  890. /// inserting a newline dependent on the \c NewLine.
  891. struct StateNode {
  892. StateNode(const LineState &State, bool NewLine, StateNode *Previous)
  893. : State(State), NewLine(NewLine), Previous(Previous) {}
  894. LineState State;
  895. bool NewLine;
  896. StateNode *Previous;
  897. };
  898. /// \brief A pair of <penalty, count> that is used to prioritize the BFS on.
  899. ///
  900. /// In case of equal penalties, we want to prefer states that were inserted
  901. /// first. During state generation we make sure that we insert states first
  902. /// that break the line as late as possible.
  903. typedef std::pair<unsigned, unsigned> OrderedPenalty;
  904. /// \brief An item in the prioritized BFS search queue. The \c StateNode's
  905. /// \c State has the given \c OrderedPenalty.
  906. typedef std::pair<OrderedPenalty, StateNode *> QueueItem;
  907. /// \brief The BFS queue type.
  908. typedef std::priority_queue<QueueItem, std::vector<QueueItem>,
  909. std::greater<QueueItem> > QueueType;
  910. /// \brief Analyze the entire solution space starting from \p InitialState.
  911. ///
  912. /// This implements a variant of Dijkstra's algorithm on the graph that spans
  913. /// the solution space (\c LineStates are the nodes). The algorithm tries to
  914. /// find the shortest path (the one with lowest penalty) from \p InitialState
  915. /// to a state where all tokens are placed.
  916. void analyzeSolutionSpace(LineState &InitialState) {
  917. std::set<LineState> Seen;
  918. // Insert start element into queue.
  919. StateNode *Node =
  920. new (Allocator.Allocate()) StateNode(InitialState, false, NULL);
  921. Queue.push(QueueItem(OrderedPenalty(0, Count), Node));
  922. ++Count;
  923. // While not empty, take first element and follow edges.
  924. while (!Queue.empty()) {
  925. unsigned Penalty = Queue.top().first.first;
  926. StateNode *Node = Queue.top().second;
  927. if (Node->State.NextToken == NULL) {
  928. DEBUG(llvm::dbgs() << "\n---\nPenalty for line: " << Penalty << "\n");
  929. break;
  930. }
  931. Queue.pop();
  932. // Cut off the analysis of certain solutions if the analysis gets too
  933. // complex. See description of IgnoreStackForComparison.
  934. if (Count > 10000)
  935. Node->State.IgnoreStackForComparison = true;
  936. if (!Seen.insert(Node->State).second)
  937. // State already examined with lower penalty.
  938. continue;
  939. addNextStateToQueue(Penalty, Node, /*NewLine=*/false);
  940. addNextStateToQueue(Penalty, Node, /*NewLine=*/true);
  941. }
  942. if (Queue.empty())
  943. // We were unable to find a solution, do nothing.
  944. // FIXME: Add diagnostic?
  945. return;
  946. // Reconstruct the solution.
  947. reconstructPath(InitialState, Queue.top().second);
  948. DEBUG(llvm::dbgs() << "Total number of analyzed states: " << Count << "\n");
  949. DEBUG(llvm::dbgs() << "---\n");
  950. }
  951. void reconstructPath(LineState &State, StateNode *Current) {
  952. std::deque<StateNode *> Path;
  953. // We do not need a break before the initial token.
  954. while (Current->Previous) {
  955. Path.push_front(Current);
  956. Current = Current->Previous;
  957. }
  958. for (std::deque<StateNode *>::iterator I = Path.begin(), E = Path.end();
  959. I != E; ++I) {
  960. DEBUG({
  961. if ((*I)->NewLine) {
  962. llvm::dbgs() << "Penalty for splitting before "
  963. << (*I)->Previous->State.NextToken->Tok.getName() << ": "
  964. << (*I)->Previous->State.NextToken->SplitPenalty << "\n";
  965. }
  966. });
  967. addTokenToState((*I)->NewLine, false, State);
  968. }
  969. }
  970. /// \brief Add the following state to the analysis queue \c Queue.
  971. ///
  972. /// Assume the current state is \p PreviousNode and has been reached with a
  973. /// penalty of \p Penalty. Insert a line break if \p NewLine is \c true.
  974. void addNextStateToQueue(unsigned Penalty, StateNode *PreviousNode,
  975. bool NewLine) {
  976. if (NewLine && !canBreak(PreviousNode->State))
  977. return;
  978. if (!NewLine && mustBreak(PreviousNode->State))
  979. return;
  980. if (NewLine) {
  981. if (!PreviousNode->State.Stack.back().ContainsLineBreak)
  982. Penalty += 15;
  983. Penalty += PreviousNode->State.NextToken->SplitPenalty;
  984. }
  985. StateNode *Node = new (Allocator.Allocate())
  986. StateNode(PreviousNode->State, NewLine, PreviousNode);
  987. Penalty += addTokenToState(NewLine, true, Node->State);
  988. if (Node->State.Column > getColumnLimit()) {
  989. unsigned ExcessCharacters = Node->State.Column - getColumnLimit();
  990. Penalty += Style.PenaltyExcessCharacter * ExcessCharacters;
  991. }
  992. Queue.push(QueueItem(OrderedPenalty(Penalty, Count), Node));
  993. ++Count;
  994. }
  995. /// \brief Returns \c true, if a line break after \p State is allowed.
  996. bool canBreak(const LineState &State) {
  997. const FormatToken &Current = *State.NextToken;
  998. const FormatToken &Previous = *Current.Previous;
  999. assert(&Previous == Current.Previous);
  1000. if (!Current.CanBreakBefore &&
  1001. !(Current.is(tok::r_brace) &&
  1002. State.Stack.back().BreakBeforeClosingBrace))
  1003. return false;
  1004. // The opening "{" of a braced list has to be on the same line as the first
  1005. // element if it is nested in another braced init list or function call.
  1006. if (!Current.MustBreakBefore && Previous.is(tok::l_brace) &&
  1007. Previous.Previous &&
  1008. Previous.Previous->isOneOf(tok::l_brace, tok::l_paren, tok::comma))
  1009. return false;
  1010. // This prevents breaks like:
  1011. // ...
  1012. // SomeParameter, OtherParameter).DoSomething(
  1013. // ...
  1014. // As they hide "DoSomething" and are generally bad for readability.
  1015. if (Previous.opensScope() &&
  1016. State.LowestLevelOnLine < State.StartOfLineLevel)
  1017. return false;
  1018. return !State.Stack.back().NoLineBreak;
  1019. }
  1020. /// \brief Returns \c true, if a line break after \p State is mandatory.
  1021. bool mustBreak(const LineState &State) {
  1022. const FormatToken &Current = *State.NextToken;
  1023. const FormatToken &Previous = *Current.Previous;
  1024. if (Current.MustBreakBefore || Current.Type == TT_InlineASMColon)
  1025. return true;
  1026. if (Current.is(tok::r_brace) && State.Stack.back().BreakBeforeClosingBrace)
  1027. return true;
  1028. if (Previous.is(tok::semi) && State.LineContainsContinuedForLoopSection)
  1029. return true;
  1030. if ((Previous.isOneOf(tok::comma, tok::semi) || Current.is(tok::question) ||
  1031. Current.Type == TT_ConditionalExpr) &&
  1032. State.Stack.back().BreakBeforeParameter &&
  1033. !Current.isTrailingComment() &&
  1034. !Current.isOneOf(tok::r_paren, tok::r_brace))
  1035. return true;
  1036. // If we need to break somewhere inside the LHS of a binary expression, we
  1037. // should also break after the operator. Otherwise, the formatting would
  1038. // hide the operator precedence, e.g. in:
  1039. // if (aaaaaaaaaaaaaa ==
  1040. // bbbbbbbbbbbbbb && c) {..
  1041. // For comparisons, we only apply this rule, if the LHS is a binary
  1042. // expression itself as otherwise, the line breaks seem superfluous.
  1043. // We need special cases for ">>" which we have split into two ">" while
  1044. // lexing in order to make template parsing easier.
  1045. bool IsComparison = (Previous.getPrecedence() == prec::Relational ||
  1046. Previous.getPrecedence() == prec::Equality) &&
  1047. Previous.Previous &&
  1048. Previous.Previous->Type != TT_BinaryOperator; // For >>.
  1049. bool LHSIsBinaryExpr =
  1050. Previous.Previous && Previous.Previous->FakeRParens > 0;
  1051. if (Previous.Type == TT_BinaryOperator &&
  1052. (!IsComparison || LHSIsBinaryExpr) &&
  1053. Current.Type != TT_BinaryOperator && // For >>.
  1054. !Current.isTrailingComment() &&
  1055. !Previous.isOneOf(tok::lessless, tok::question) &&
  1056. Previous.getPrecedence() != prec::Assignment &&
  1057. State.Stack.back().BreakBeforeParameter)
  1058. return true;
  1059. // Same as above, but for the first "<<" operator.
  1060. if (Current.is(tok::lessless) && State.Stack.back().BreakBeforeParameter &&
  1061. State.Stack.back().FirstLessLess == 0)
  1062. return true;
  1063. // FIXME: Comparing LongestObjCSelectorName to 0 is a hacky way of finding
  1064. // out whether it is the first parameter. Clean this up.
  1065. if (Current.Type == TT_ObjCSelectorName &&
  1066. Current.LongestObjCSelectorName == 0 &&
  1067. State.Stack.back().BreakBeforeParameter)
  1068. return true;
  1069. if ((Current.Type == TT_CtorInitializerColon ||
  1070. (Previous.ClosesTemplateDeclaration && State.ParenLevel == 0)))
  1071. return true;
  1072. if ((Current.Type == TT_StartOfName || Current.is(tok::kw_operator)) &&
  1073. Line.MightBeFunctionDecl && State.Stack.back().BreakBeforeParameter &&
  1074. State.ParenLevel == 0)
  1075. return true;
  1076. return false;
  1077. }
  1078. // Returns the total number of columns required for the remaining tokens.
  1079. unsigned getRemainingLength(const LineState &State) {
  1080. if (State.NextToken && State.NextToken->Previous)
  1081. return Line.Last->TotalLength - State.NextToken->Previous->TotalLength;
  1082. return 0;
  1083. }
  1084. FormatStyle Style;
  1085. SourceManager &SourceMgr;
  1086. const AnnotatedLine &Line;
  1087. const unsigned FirstIndent;
  1088. const FormatToken *RootToken;
  1089. WhitespaceManager &Whitespaces;
  1090. llvm::SpecificBumpPtrAllocator<StateNode> Allocator;
  1091. QueueType Queue;
  1092. // Increasing count of \c StateNode items we have created. This is used
  1093. // to create a deterministic order independent of the container.
  1094. unsigned Count;
  1095. encoding::Encoding Encoding;
  1096. bool BinPackInconclusiveFunctions;
  1097. };
  1098. class FormatTokenLexer {
  1099. public:
  1100. FormatTokenLexer(Lexer &Lex, SourceManager &SourceMgr,
  1101. encoding::Encoding Encoding)
  1102. : FormatTok(NULL), GreaterStashed(false), TrailingWhitespace(0), Lex(Lex),
  1103. SourceMgr(SourceMgr), IdentTable(Lex.getLangOpts()),
  1104. Encoding(Encoding) {
  1105. Lex.SetKeepWhitespaceMode(true);
  1106. }
  1107. ArrayRef<FormatToken *> lex() {
  1108. assert(Tokens.empty());
  1109. do {
  1110. Tokens.push_back(getNextToken());
  1111. } while (Tokens.back()->Tok.isNot(tok::eof));
  1112. return Tokens;
  1113. }
  1114. IdentifierTable &getIdentTable() { return IdentTable; }
  1115. private:
  1116. FormatToken *getNextToken() {
  1117. if (GreaterStashed) {
  1118. // Create a synthesized second '>' token.
  1119. Token Greater = FormatTok->Tok;
  1120. FormatTok = new (Allocator.Allocate()) FormatToken;
  1121. FormatTok->Tok = Greater;
  1122. SourceLocation GreaterLocation =
  1123. FormatTok->Tok.getLocation().getLocWithOffset(1);
  1124. FormatTok->WhitespaceRange =
  1125. SourceRange(GreaterLocation, GreaterLocation);
  1126. FormatTok->TokenText = ">";
  1127. FormatTok->CodePointCount = 1;
  1128. GreaterStashed = false;
  1129. return FormatTok;
  1130. }
  1131. FormatTok = new (Allocator.Allocate()) FormatToken;
  1132. Lex.LexFromRawLexer(FormatTok->Tok);
  1133. StringRef Text = rawTokenText(FormatTok->Tok);
  1134. SourceLocation WhitespaceStart =
  1135. FormatTok->Tok.getLocation().getLocWithOffset(-TrailingWhitespace);
  1136. if (SourceMgr.getFileOffset(WhitespaceStart) == 0)
  1137. FormatTok->IsFirst = true;
  1138. // Consume and record whitespace until we find a significant token.
  1139. unsigned WhitespaceLength = TrailingWhitespace;
  1140. while (FormatTok->Tok.is(tok::unknown)) {
  1141. unsigned Newlines = Text.count('\n');
  1142. if (Newlines > 0)
  1143. FormatTok->LastNewlineOffset = WhitespaceLength + Text.rfind('\n') + 1;
  1144. FormatTok->NewlinesBefore += Newlines;
  1145. unsigned EscapedNewlines = Text.count("\\\n");
  1146. FormatTok->HasUnescapedNewline |= EscapedNewlines != Newlines;
  1147. WhitespaceLength += FormatTok->Tok.getLength();
  1148. Lex.LexFromRawLexer(FormatTok->Tok);
  1149. Text = rawTokenText(FormatTok->Tok);
  1150. }
  1151. // In case the token starts with escaped newlines, we want to
  1152. // take them into account as whitespace - this pattern is quite frequent
  1153. // in macro definitions.
  1154. // FIXME: What do we want to do with other escaped spaces, and escaped
  1155. // spaces or newlines in the middle of tokens?
  1156. // FIXME: Add a more explicit test.
  1157. while (Text.size() > 1 && Text[0] == '\\' && Text[1] == '\n') {
  1158. // FIXME: ++FormatTok->NewlinesBefore is missing...
  1159. WhitespaceLength += 2;
  1160. Text = Text.substr(2);
  1161. }
  1162. TrailingWhitespace = 0;
  1163. if (FormatTok->Tok.is(tok::comment)) {
  1164. StringRef UntrimmedText = Text;
  1165. Text = Text.rtrim();
  1166. TrailingWhitespace = UntrimmedText.size() - Text.size();
  1167. } else if (FormatTok->Tok.is(tok::raw_identifier)) {
  1168. IdentifierInfo &Info = IdentTable.get(Text);
  1169. FormatTok->Tok.setIdentifierInfo(&Info);
  1170. FormatTok->Tok.setKind(Info.getTokenID());
  1171. } else if (FormatTok->Tok.is(tok::greatergreater)) {
  1172. FormatTok->Tok.setKind(tok::greater);
  1173. Text = Text.substr(0, 1);
  1174. GreaterStashed = true;
  1175. }
  1176. // Now FormatTok is the next non-whitespace token.
  1177. FormatTok->TokenText = Text;
  1178. FormatTok->CodePointCount = encoding::getCodePointCount(Text, Encoding);
  1179. FormatTok->WhitespaceRange = SourceRange(
  1180. WhitespaceStart, WhitespaceStart.getLocWithOffset(WhitespaceLength));
  1181. return FormatTok;
  1182. }
  1183. FormatToken *FormatTok;
  1184. bool GreaterStashed;
  1185. unsigned TrailingWhitespace;
  1186. Lexer &Lex;
  1187. SourceManager &SourceMgr;
  1188. IdentifierTable IdentTable;
  1189. encoding::Encoding Encoding;
  1190. llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
  1191. SmallVector<FormatToken *, 16> Tokens;
  1192. /// Returns the text of \c FormatTok.
  1193. StringRef rawTokenText(Token &Tok) {
  1194. return StringRef(SourceMgr.getCharacterData(Tok.getLocation()),
  1195. Tok.getLength());
  1196. }
  1197. };
  1198. class Formatter : public UnwrappedLineConsumer {
  1199. public:
  1200. Formatter(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr,
  1201. const std::vector<CharSourceRange> &Ranges)
  1202. : Style(Style), Lex(Lex), SourceMgr(SourceMgr),
  1203. Whitespaces(SourceMgr, Style), Ranges(Ranges),
  1204. Encoding(encoding::detectEncoding(Lex.getBuffer())) {
  1205. DEBUG(llvm::dbgs()
  1206. << "File encoding: "
  1207. << (Encoding == encoding::Encoding_UTF8 ? "UTF8" : "unknown")
  1208. << "\n");
  1209. }
  1210. virtual ~Formatter() {}
  1211. tooling::Replacements format() {
  1212. FormatTokenLexer Tokens(Lex, SourceMgr, Encoding);
  1213. UnwrappedLineParser Parser(Style, Tokens.lex(), *this);
  1214. bool StructuralError = Parser.parse();
  1215. TokenAnnotator Annotator(Style, Tokens.getIdentTable().get("in"));
  1216. for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
  1217. Annotator.annotate(AnnotatedLines[i]);
  1218. }
  1219. deriveLocalStyle();
  1220. for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
  1221. Annotator.calculateFormattingInformation(AnnotatedLines[i]);
  1222. }
  1223. // Adapt level to the next line if this is a comment.
  1224. // FIXME: Can/should this be done in the UnwrappedLineParser?
  1225. const AnnotatedLine *NextNonCommentLine = NULL;
  1226. for (unsigned i = AnnotatedLines.size() - 1; i > 0; --i) {
  1227. if (NextNonCommentLine && AnnotatedLines[i].First->is(tok::comment) &&
  1228. !AnnotatedLines[i].First->Next)
  1229. AnnotatedLines[i].Level = NextNonCommentLine->Level;
  1230. else
  1231. NextNonCommentLine = AnnotatedLines[i].First->isNot(tok::r_brace)
  1232. ? &AnnotatedLines[i]
  1233. : NULL;
  1234. }
  1235. std::vector<int> IndentForLevel;
  1236. bool PreviousLineWasTouched = false;
  1237. const FormatToken *PreviousLineLastToken = 0;
  1238. bool FormatPPDirective = false;
  1239. for (std::vector<AnnotatedLine>::iterator I = AnnotatedLines.begin(),
  1240. E = AnnotatedLines.end();
  1241. I != E; ++I) {
  1242. const AnnotatedLine &TheLine = *I;
  1243. const FormatToken *FirstTok = TheLine.First;
  1244. int Offset = getIndentOffset(*TheLine.First);
  1245. // Check whether this line is part of a formatted preprocessor directive.
  1246. if (FirstTok->HasUnescapedNewline)
  1247. FormatPPDirective = false;
  1248. if (!FormatPPDirective && TheLine.InPPDirective &&
  1249. (touchesLine(TheLine) || touchesPPDirective(I + 1, E)))
  1250. FormatPPDirective = true;
  1251. // Determine indent and try to merge multiple unwrapped lines.
  1252. while (IndentForLevel.size() <= TheLine.Level)
  1253. IndentForLevel.push_back(-1);
  1254. IndentForLevel.resize(TheLine.Level + 1);
  1255. unsigned Indent = getIndent(IndentForLevel, TheLine.Level);
  1256. if (static_cast<int>(Indent) + Offset >= 0)
  1257. Indent += Offset;
  1258. tryFitMultipleLinesInOne(Indent, I, E);
  1259. bool WasMoved = PreviousLineWasTouched && FirstTok->NewlinesBefore == 0;
  1260. if (TheLine.First->is(tok::eof)) {
  1261. if (PreviousLineWasTouched) {
  1262. unsigned NewLines = std::min(FirstTok->NewlinesBefore, 1u);
  1263. Whitespaces.replaceWhitespace(*TheLine.First, NewLines, /*Indent*/ 0,
  1264. /*TargetColumn*/ 0);
  1265. }
  1266. } else if (TheLine.Type != LT_Invalid &&
  1267. (WasMoved || FormatPPDirective || touchesLine(TheLine))) {
  1268. unsigned LevelIndent = getIndent(IndentForLevel, TheLine.Level);
  1269. if (FirstTok->WhitespaceRange.isValid() &&
  1270. // Insert a break even if there is a structural error in case where
  1271. // we break apart a line consisting of multiple unwrapped lines.
  1272. (FirstTok->NewlinesBefore == 0 || !StructuralError)) {
  1273. formatFirstToken(*TheLine.First, PreviousLineLastToken, Indent,
  1274. TheLine.InPPDirective);
  1275. } else {
  1276. Indent = LevelIndent =
  1277. SourceMgr.getSpellingColumnNumber(FirstTok->Tok.getLocation()) -
  1278. 1;
  1279. }
  1280. UnwrappedLineFormatter Formatter(Style, SourceMgr, TheLine, Indent,
  1281. TheLine.First, Whitespaces, Encoding,
  1282. BinPackInconclusiveFunctions);
  1283. Formatter.format(I + 1 != E ? &*(I + 1) : NULL);
  1284. IndentForLevel[TheLine.Level] = LevelIndent;
  1285. PreviousLineWasTouched = true;
  1286. } else {
  1287. // Format the first token if necessary, and notify the WhitespaceManager
  1288. // about the unchanged whitespace.
  1289. for (const FormatToken *Tok = TheLine.First; Tok != NULL;
  1290. Tok = Tok->Next) {
  1291. if (Tok == TheLine.First &&
  1292. (Tok->NewlinesBefore > 0 || Tok->IsFirst)) {
  1293. unsigned LevelIndent =
  1294. SourceMgr.getSpellingColumnNumber(Tok->Tok.getLocation()) - 1;
  1295. // Remove trailing whitespace of the previous line if it was
  1296. // touched.
  1297. if (PreviousLineWasTouched || touchesEmptyLineBefore(TheLine)) {
  1298. formatFirstToken(*Tok, PreviousLineLastToken, LevelIndent,
  1299. TheLine.InPPDirective);
  1300. } else {
  1301. Whitespaces.addUntouchableToken(*Tok, TheLine.InPPDirective);
  1302. }
  1303. if (static_cast<int>(LevelIndent) - Offset >= 0)
  1304. LevelIndent -= Offset;
  1305. if (Tok->isNot(tok::comment))
  1306. IndentForLevel[TheLine.Level] = LevelIndent;
  1307. } else {
  1308. Whitespaces.addUntouchableToken(*Tok, TheLine.InPPDirective);
  1309. }
  1310. }
  1311. // If we did not reformat this unwrapped line, the column at the end of
  1312. // the last token is unchanged - thus, we can calculate the end of the
  1313. // last token.
  1314. PreviousLineWasTouched = false;
  1315. }
  1316. PreviousLineLastToken = I->Last;
  1317. }
  1318. return Whitespaces.generateReplacements();
  1319. }
  1320. private:
  1321. void deriveLocalStyle() {
  1322. unsigned CountBoundToVariable = 0;
  1323. unsigned CountBoundToType = 0;
  1324. bool HasCpp03IncompatibleFormat = false;
  1325. bool HasBinPackedFunction = false;
  1326. bool HasOnePerLineFunction = false;
  1327. for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
  1328. if (!AnnotatedLines[i].First->Next)
  1329. continue;
  1330. FormatToken *Tok = AnnotatedLines[i].First->Next;
  1331. while (Tok->Next) {
  1332. if (Tok->Type == TT_PointerOrReference) {
  1333. bool SpacesBefore =
  1334. Tok->WhitespaceRange.getBegin() != Tok->WhitespaceRange.getEnd();
  1335. bool SpacesAfter = Tok->Next->WhitespaceRange.getBegin() !=
  1336. Tok->Next->WhitespaceRange.getEnd();
  1337. if (SpacesBefore && !SpacesAfter)
  1338. ++CountBoundToVariable;
  1339. else if (!SpacesBefore && SpacesAfter)
  1340. ++CountBoundToType;
  1341. }
  1342. if (Tok->Type == TT_TemplateCloser &&
  1343. Tok->Previous->Type == TT_TemplateCloser &&
  1344. Tok->WhitespaceRange.getBegin() == Tok->WhitespaceRange.getEnd())
  1345. HasCpp03IncompatibleFormat = true;
  1346. if (Tok->PackingKind == PPK_BinPacked)
  1347. HasBinPackedFunction = true;
  1348. if (Tok->PackingKind == PPK_OnePerLine)
  1349. HasOnePerLineFunction = true;
  1350. Tok = Tok->Next;
  1351. }
  1352. }
  1353. if (Style.DerivePointerBinding) {
  1354. if (CountBoundToType > CountBoundToVariable)
  1355. Style.PointerBindsToType = true;
  1356. else if (CountBoundToType < CountBoundToVariable)
  1357. Style.PointerBindsToType = false;
  1358. }
  1359. if (Style.Standard == FormatStyle::LS_Auto) {
  1360. Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11
  1361. : FormatStyle::LS_Cpp03;
  1362. }
  1363. BinPackInconclusiveFunctions =
  1364. HasBinPackedFunction || !HasOnePerLineFunction;
  1365. }
  1366. /// \brief Get the indent of \p Level from \p IndentForLevel.
  1367. ///
  1368. /// \p IndentForLevel must contain the indent for the level \c l
  1369. /// at \p IndentForLevel[l], or a value < 0 if the indent for
  1370. /// that level is unknown.
  1371. unsigned getIndent(const std::vector<int> IndentForLevel, unsigned Level) {
  1372. if (IndentForLevel[Level] != -1)
  1373. return IndentForLevel[Level];
  1374. if (Level == 0)
  1375. return 0;
  1376. return getIndent(IndentForLevel, Level - 1) + Style.IndentWidth;
  1377. }
  1378. /// \brief Get the offset of the line relatively to the level.
  1379. ///
  1380. /// For example, 'public:' labels in classes are offset by 1 or 2
  1381. /// characters to the left from their level.
  1382. int getIndentOffset(const FormatToken &RootToken) {
  1383. if (RootToken.isAccessSpecifier(false) || RootToken.isObjCAccessSpecifier())
  1384. return Style.AccessModifierOffset;
  1385. return 0;
  1386. }
  1387. /// \brief Tries to merge lines into one.
  1388. ///
  1389. /// This will change \c Line and \c AnnotatedLine to contain the merged line,
  1390. /// if possible; note that \c I will be incremented when lines are merged.
  1391. void tryFitMultipleLinesInOne(unsigned Indent,
  1392. std::vector<AnnotatedLine>::iterator &I,
  1393. std::vector<AnnotatedLine>::iterator E) {
  1394. // We can never merge stuff if there are trailing line comments.
  1395. if (I->Last->Type == TT_LineComment)
  1396. return;
  1397. unsigned Limit = Style.ColumnLimit - Indent;
  1398. // If we already exceed the column limit, we set 'Limit' to 0. The different
  1399. // tryMerge..() functions can then decide whether to still do merging.
  1400. Limit = I->Last->TotalLength > Limit ? 0 : Limit - I->Last->TotalLength;
  1401. if (I + 1 == E || (I + 1)->Type == LT_Invalid)
  1402. return;
  1403. if (I->Last->is(tok::l_brace)) {
  1404. tryMergeSimpleBlock(I, E, Limit);
  1405. } else if (Style.AllowShortIfStatementsOnASingleLine &&
  1406. I->First->is(tok::kw_if)) {
  1407. tryMergeSimpleControlStatement(I, E, Limit);
  1408. } else if (Style.AllowShortLoopsOnASingleLine &&
  1409. I->First->isOneOf(tok::kw_for, tok::kw_while)) {
  1410. tryMergeSimpleControlStatement(I, E, Limit);
  1411. } else if (I->InPPDirective &&
  1412. (I->First->HasUnescapedNewline || I->First->IsFirst)) {
  1413. tryMergeSimplePPDirective(I, E, Limit);
  1414. }
  1415. }
  1416. void tryMergeSimplePPDirective(std::vector<AnnotatedLine>::iterator &I,
  1417. std::vector<AnnotatedLine>::iterator E,
  1418. unsigned Limit) {
  1419. if (Limit == 0)
  1420. return;
  1421. AnnotatedLine &Line = *I;
  1422. if (!(I + 1)->InPPDirective || (I + 1)->First->HasUnescapedNewline)
  1423. return;
  1424. if (I + 2 != E && (I + 2)->InPPDirective &&
  1425. !(I + 2)->First->HasUnescapedNewline)
  1426. return;
  1427. if (1 + (I + 1)->Last->TotalLength > Limit)
  1428. return;
  1429. join(Line, *(++I));
  1430. }
  1431. void tryMergeSimpleControlStatement(std::vector<AnnotatedLine>::iterator &I,
  1432. std::vector<AnnotatedLine>::iterator E,
  1433. unsigned Limit) {
  1434. if (Limit == 0)
  1435. return;
  1436. if ((I + 1)->InPPDirective != I->InPPDirective ||
  1437. ((I + 1)->InPPDirective && (I + 1)->First->HasUnescapedNewline))
  1438. return;
  1439. AnnotatedLine &Line = *I;
  1440. if (Line.Last->isNot(tok::r_paren))
  1441. return;
  1442. if (1 + (I + 1)->Last->TotalLength > Limit)
  1443. return;
  1444. if ((I + 1)->First->isOneOf(tok::semi, tok::kw_if, tok::kw_for,
  1445. tok::kw_while) ||
  1446. (I + 1)->First->Type == TT_LineComment)
  1447. return;
  1448. // Only inline simple if's (no nested if or else).
  1449. if (I + 2 != E && Line.First->is(tok::kw_if) &&
  1450. (I + 2)->First->is(tok::kw_else))
  1451. return;
  1452. join(Line, *(++I));
  1453. }
  1454. void tryMergeSimpleBlock(std::vector<AnnotatedLine>::iterator &I,
  1455. std::vector<AnnotatedLine>::iterator E,
  1456. unsigned Limit) {
  1457. // No merging if the brace already is on the next line.
  1458. if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
  1459. return;
  1460. // First, check that the current line allows merging. This is the case if
  1461. // we're not in a control flow statement and the last token is an opening
  1462. // brace.
  1463. AnnotatedLine &Line = *I;
  1464. if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::r_brace,
  1465. tok::kw_else, tok::kw_try, tok::kw_catch,
  1466. tok::kw_for,
  1467. // This gets rid of all ObjC @ keywords and methods.
  1468. tok::at, tok::minus, tok::plus))
  1469. return;
  1470. FormatToken *Tok = (I + 1)->First;
  1471. if (Tok->is(tok::r_brace) && !Tok->MustBreakBefore &&
  1472. (Tok->getNextNonComment() == NULL ||
  1473. Tok->getNextNonComment()->is(tok::semi))) {
  1474. // We merge empty blocks even if the line exceeds the column limit.
  1475. Tok->SpacesRequiredBefore = 0;
  1476. Tok->CanBreakBefore = true;
  1477. join(Line, *(I + 1));
  1478. I += 1;
  1479. } else if (Limit != 0 && Line.First->isNot(tok::kw_namespace)) {
  1480. // Check that we still have three lines and they fit into the limit.
  1481. if (I + 2 == E || (I + 2)->Type == LT_Invalid ||
  1482. !nextTwoLinesFitInto(I, Limit))
  1483. return;
  1484. // Second, check that the next line does not contain any braces - if it
  1485. // does, readability declines when putting it into a single line.
  1486. if ((I + 1)->Last->Type == TT_LineComment || Tok->MustBreakBefore)
  1487. return;
  1488. do {
  1489. if (Tok->isOneOf(tok::l_brace, tok::r_brace))
  1490. return;
  1491. Tok = Tok->Next;
  1492. } while (Tok != NULL);
  1493. // Last, check that the third line contains a single closing brace.
  1494. Tok = (I + 2)->First;
  1495. if (Tok->getNextNonComment() != NULL || Tok->isNot(tok::r_brace) ||
  1496. Tok->MustBreakBefore)
  1497. return;
  1498. join(Line, *(I + 1));
  1499. join(Line, *(I + 2));
  1500. I += 2;
  1501. }
  1502. }
  1503. bool nextTwoLinesFitInto(std::vector<AnnotatedLine>::iterator I,
  1504. unsigned Limit) {
  1505. return 1 + (I + 1)->Last->TotalLength + 1 + (I + 2)->Last->TotalLength <=
  1506. Limit;
  1507. }
  1508. void join(AnnotatedLine &A, const AnnotatedLine &B) {
  1509. assert(!A.Last->Next);
  1510. assert(!B.First->Previous);
  1511. A.Last->Next = B.First;
  1512. B.First->Previous = A.Last;
  1513. unsigned LengthA = A.Last->TotalLength + B.First->SpacesRequiredBefore;
  1514. for (FormatToken *Tok = B.First; Tok; Tok = Tok->Next) {
  1515. Tok->TotalLength += LengthA;
  1516. A.Last = Tok;
  1517. }
  1518. }
  1519. bool touchesRanges(const CharSourceRange &Range) {
  1520. for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
  1521. if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),
  1522. Ranges[i].getBegin()) &&
  1523. !SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(),
  1524. Range.getBegin()))
  1525. return true;
  1526. }
  1527. return false;
  1528. }
  1529. bool touchesLine(const AnnotatedLine &TheLine) {
  1530. const FormatToken *First = TheLine.First;
  1531. const FormatToken *Last = TheLine.Last;
  1532. CharSourceRange LineRange = CharSourceRange::getCharRange(
  1533. First->WhitespaceRange.getBegin().getLocWithOffset(
  1534. First->LastNewlineOffset),
  1535. Last->Tok.getLocation().getLocWithOffset(Last->TokenText.size() - 1));
  1536. return touchesRanges(LineRange);
  1537. }
  1538. bool touchesPPDirective(std::vector<AnnotatedLine>::iterator I,
  1539. std::vector<AnnotatedLine>::iterator E) {
  1540. for (; I != E; ++I) {
  1541. if (I->First->HasUnescapedNewline)
  1542. return false;
  1543. if (touchesLine(*I))
  1544. return true;
  1545. }
  1546. return false;
  1547. }
  1548. bool touchesEmptyLineBefore(const AnnotatedLine &TheLine) {
  1549. const FormatToken *First = TheLine.First;
  1550. CharSourceRange LineRange = CharSourceRange::getCharRange(
  1551. First->WhitespaceRange.getBegin(),
  1552. First->WhitespaceRange.getBegin().getLocWithOffset(
  1553. First->LastNewlineOffset));
  1554. return touchesRanges(LineRange);
  1555. }
  1556. virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) {
  1557. AnnotatedLines.push_back(AnnotatedLine(TheLine));
  1558. }
  1559. /// \brief Add a new line and the required indent before the first Token
  1560. /// of the \c UnwrappedLine if there was no structural parsing error.
  1561. /// Returns the indent level of the \c UnwrappedLine.
  1562. void formatFirstToken(const FormatToken &RootToken,
  1563. const FormatToken *PreviousToken, unsigned Indent,
  1564. bool InPPDirective) {
  1565. unsigned Newlines =
  1566. std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);
  1567. // Remove empty lines before "}" where applicable.
  1568. if (RootToken.is(tok::r_brace) &&
  1569. (!RootToken.Next ||
  1570. (RootToken.Next->is(tok::semi) && !RootToken.Next->Next)))
  1571. Newlines = std::min(Newlines, 1u);
  1572. if (Newlines == 0 && !RootToken.IsFirst)
  1573. Newlines = 1;
  1574. // Insert extra new line before access specifiers.
  1575. if (PreviousToken && PreviousToken->isOneOf(tok::semi, tok::r_brace) &&
  1576. RootToken.isAccessSpecifier() && RootToken.NewlinesBefore == 1)
  1577. ++Newlines;
  1578. Whitespaces.replaceWhitespace(
  1579. RootToken, Newlines, Indent, Indent,
  1580. InPPDirective && !RootToken.HasUnescapedNewline);
  1581. }
  1582. FormatStyle Style;
  1583. Lexer &Lex;
  1584. SourceManager &SourceMgr;
  1585. WhitespaceManager Whitespaces;
  1586. std::vector<CharSourceRange> Ranges;
  1587. std::vector<AnnotatedLine> AnnotatedLines;
  1588. encoding::Encoding Encoding;
  1589. bool BinPackInconclusiveFunctions;
  1590. };
  1591. } // end anonymous namespace
  1592. tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
  1593. SourceManager &SourceMgr,
  1594. std::vector<CharSourceRange> Ranges) {
  1595. Formatter formatter(Style, Lex, SourceMgr, Ranges);
  1596. return formatter.format();
  1597. }
  1598. tooling::Replacements reformat(const FormatStyle &Style, StringRef Code,
  1599. std::vector<tooling::Range> Ranges,
  1600. StringRef FileName) {
  1601. FileManager Files((FileSystemOptions()));
  1602. DiagnosticsEngine Diagnostics(
  1603. IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
  1604. new DiagnosticOptions);
  1605. SourceManager SourceMgr(Diagnostics, Files);
  1606. llvm::MemoryBuffer *Buf = llvm::MemoryBuffer::getMemBuffer(Code, FileName);
  1607. const clang::FileEntry *Entry =
  1608. Files.getVirtualFile(FileName, Buf->getBufferSize(), 0);
  1609. SourceMgr.overrideFileContents(Entry, Buf);
  1610. FileID ID =
  1611. SourceMgr.createFileID(Entry, SourceLocation(), clang::SrcMgr::C_User);
  1612. Lexer Lex(ID, SourceMgr.getBuffer(ID), SourceMgr,
  1613. getFormattingLangOpts(Style.Standard));
  1614. SourceLocation StartOfFile = SourceMgr.getLocForStartOfFile(ID);
  1615. std::vector<CharSourceRange> CharRanges;
  1616. for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
  1617. SourceLocation Start = StartOfFile.getLocWithOffset(Ranges[i].getOffset());
  1618. SourceLocation End = Start.getLocWithOffset(Ranges[i].getLength());
  1619. CharRanges.push_back(CharSourceRange::getCharRange(Start, End));
  1620. }
  1621. return reformat(Style, Lex, SourceMgr, CharRanges);
  1622. }
  1623. LangOptions getFormattingLangOpts(FormatStyle::LanguageStandard Standard) {
  1624. LangOptions LangOpts;
  1625. LangOpts.CPlusPlus = 1;
  1626. LangOpts.CPlusPlus11 = Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
  1627. LangOpts.LineComment = 1;
  1628. LangOpts.Bool = 1;
  1629. LangOpts.ObjC1 = 1;
  1630. LangOpts.ObjC2 = 1;
  1631. return LangOpts;
  1632. }
  1633. } // namespace format
  1634. } // namespace clang