Format.cpp 64 KB

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