Format.cpp 78 KB

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