Format.cpp 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  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 "ContinuationIndenter.h"
  17. #include "TokenAnnotator.h"
  18. #include "UnwrappedLineParser.h"
  19. #include "WhitespaceManager.h"
  20. #include "clang/Basic/Diagnostic.h"
  21. #include "clang/Basic/SourceManager.h"
  22. #include "clang/Format/Format.h"
  23. #include "clang/Lex/Lexer.h"
  24. #include "llvm/ADT/STLExtras.h"
  25. #include "llvm/Support/Allocator.h"
  26. #include "llvm/Support/Debug.h"
  27. #include "llvm/Support/YAMLTraits.h"
  28. #include "llvm/Support/Path.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, "Cpp03", clang::format::FormatStyle::LS_Cpp03);
  38. IO.enumCase(Value, "C++03", clang::format::FormatStyle::LS_Cpp03);
  39. IO.enumCase(Value, "Cpp11", clang::format::FormatStyle::LS_Cpp11);
  40. IO.enumCase(Value, "C++11", clang::format::FormatStyle::LS_Cpp11);
  41. IO.enumCase(Value, "Auto", clang::format::FormatStyle::LS_Auto);
  42. }
  43. };
  44. template <>
  45. struct ScalarEnumerationTraits<clang::format::FormatStyle::UseTabStyle> {
  46. static void enumeration(IO &IO,
  47. clang::format::FormatStyle::UseTabStyle &Value) {
  48. IO.enumCase(Value, "Never", clang::format::FormatStyle::UT_Never);
  49. IO.enumCase(Value, "false", clang::format::FormatStyle::UT_Never);
  50. IO.enumCase(Value, "Always", clang::format::FormatStyle::UT_Always);
  51. IO.enumCase(Value, "true", clang::format::FormatStyle::UT_Always);
  52. IO.enumCase(Value, "ForIndentation",
  53. clang::format::FormatStyle::UT_ForIndentation);
  54. }
  55. };
  56. template <>
  57. struct ScalarEnumerationTraits<clang::format::FormatStyle::BraceBreakingStyle> {
  58. static void
  59. enumeration(IO &IO, clang::format::FormatStyle::BraceBreakingStyle &Value) {
  60. IO.enumCase(Value, "Attach", clang::format::FormatStyle::BS_Attach);
  61. IO.enumCase(Value, "Linux", clang::format::FormatStyle::BS_Linux);
  62. IO.enumCase(Value, "Stroustrup", clang::format::FormatStyle::BS_Stroustrup);
  63. IO.enumCase(Value, "Allman", clang::format::FormatStyle::BS_Allman);
  64. }
  65. };
  66. template <>
  67. struct ScalarEnumerationTraits<
  68. clang::format::FormatStyle::NamespaceIndentationKind> {
  69. static void
  70. enumeration(IO &IO,
  71. clang::format::FormatStyle::NamespaceIndentationKind &Value) {
  72. IO.enumCase(Value, "None", clang::format::FormatStyle::NI_None);
  73. IO.enumCase(Value, "Inner", clang::format::FormatStyle::NI_Inner);
  74. IO.enumCase(Value, "All", clang::format::FormatStyle::NI_All);
  75. }
  76. };
  77. template <> struct MappingTraits<clang::format::FormatStyle> {
  78. static void mapping(llvm::yaml::IO &IO, clang::format::FormatStyle &Style) {
  79. if (IO.outputting()) {
  80. StringRef StylesArray[] = { "LLVM", "Google", "Chromium",
  81. "Mozilla", "WebKit" };
  82. ArrayRef<StringRef> Styles(StylesArray);
  83. for (size_t i = 0, e = Styles.size(); i < e; ++i) {
  84. StringRef StyleName(Styles[i]);
  85. clang::format::FormatStyle PredefinedStyle;
  86. if (clang::format::getPredefinedStyle(StyleName, &PredefinedStyle) &&
  87. Style == PredefinedStyle) {
  88. IO.mapOptional("# BasedOnStyle", StyleName);
  89. break;
  90. }
  91. }
  92. } else {
  93. StringRef BasedOnStyle;
  94. IO.mapOptional("BasedOnStyle", BasedOnStyle);
  95. if (!BasedOnStyle.empty())
  96. if (!clang::format::getPredefinedStyle(BasedOnStyle, &Style)) {
  97. IO.setError(Twine("Unknown value for BasedOnStyle: ", BasedOnStyle));
  98. return;
  99. }
  100. }
  101. IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset);
  102. IO.mapOptional("ConstructorInitializerIndentWidth",
  103. Style.ConstructorInitializerIndentWidth);
  104. IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft);
  105. IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments);
  106. IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine",
  107. Style.AllowAllParametersOfDeclarationOnNextLine);
  108. IO.mapOptional("AllowShortIfStatementsOnASingleLine",
  109. Style.AllowShortIfStatementsOnASingleLine);
  110. IO.mapOptional("AllowShortLoopsOnASingleLine",
  111. Style.AllowShortLoopsOnASingleLine);
  112. IO.mapOptional("AlwaysBreakTemplateDeclarations",
  113. Style.AlwaysBreakTemplateDeclarations);
  114. IO.mapOptional("AlwaysBreakBeforeMultilineStrings",
  115. Style.AlwaysBreakBeforeMultilineStrings);
  116. IO.mapOptional("BreakBeforeBinaryOperators",
  117. Style.BreakBeforeBinaryOperators);
  118. IO.mapOptional("BreakConstructorInitializersBeforeComma",
  119. Style.BreakConstructorInitializersBeforeComma);
  120. IO.mapOptional("BinPackParameters", Style.BinPackParameters);
  121. IO.mapOptional("ColumnLimit", Style.ColumnLimit);
  122. IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine",
  123. Style.ConstructorInitializerAllOnOneLineOrOnePerLine);
  124. IO.mapOptional("DerivePointerBinding", Style.DerivePointerBinding);
  125. IO.mapOptional("ExperimentalAutoDetectBinPacking",
  126. Style.ExperimentalAutoDetectBinPacking);
  127. IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels);
  128. IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep);
  129. IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation);
  130. IO.mapOptional("ObjCSpaceBeforeProtocolList",
  131. Style.ObjCSpaceBeforeProtocolList);
  132. IO.mapOptional("PenaltyBreakBeforeFirstCallParameter",
  133. Style.PenaltyBreakBeforeFirstCallParameter);
  134. IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment);
  135. IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString);
  136. IO.mapOptional("PenaltyBreakFirstLessLess",
  137. Style.PenaltyBreakFirstLessLess);
  138. IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter);
  139. IO.mapOptional("PenaltyReturnTypeOnItsOwnLine",
  140. Style.PenaltyReturnTypeOnItsOwnLine);
  141. IO.mapOptional("PointerBindsToType", Style.PointerBindsToType);
  142. IO.mapOptional("SpacesBeforeTrailingComments",
  143. Style.SpacesBeforeTrailingComments);
  144. IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle);
  145. IO.mapOptional("Standard", Style.Standard);
  146. IO.mapOptional("IndentWidth", Style.IndentWidth);
  147. IO.mapOptional("TabWidth", Style.TabWidth);
  148. IO.mapOptional("UseTab", Style.UseTab);
  149. IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces);
  150. IO.mapOptional("IndentFunctionDeclarationAfterType",
  151. Style.IndentFunctionDeclarationAfterType);
  152. IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses);
  153. IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses);
  154. IO.mapOptional("SpacesInCStyleCastParentheses",
  155. Style.SpacesInCStyleCastParentheses);
  156. IO.mapOptional("SpaceAfterControlStatementKeyword",
  157. Style.SpaceAfterControlStatementKeyword);
  158. IO.mapOptional("SpaceBeforeAssignmentOperators",
  159. Style.SpaceBeforeAssignmentOperators);
  160. IO.mapOptional("ContinuationIndentWidth", Style.ContinuationIndentWidth);
  161. }
  162. };
  163. }
  164. }
  165. namespace clang {
  166. namespace format {
  167. void setDefaultPenalties(FormatStyle &Style) {
  168. Style.PenaltyBreakComment = 60;
  169. Style.PenaltyBreakFirstLessLess = 120;
  170. Style.PenaltyBreakString = 1000;
  171. Style.PenaltyExcessCharacter = 1000000;
  172. }
  173. FormatStyle getLLVMStyle() {
  174. FormatStyle LLVMStyle;
  175. LLVMStyle.AccessModifierOffset = -2;
  176. LLVMStyle.AlignEscapedNewlinesLeft = false;
  177. LLVMStyle.AlignTrailingComments = true;
  178. LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
  179. LLVMStyle.AllowShortIfStatementsOnASingleLine = false;
  180. LLVMStyle.AllowShortLoopsOnASingleLine = false;
  181. LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
  182. LLVMStyle.AlwaysBreakTemplateDeclarations = false;
  183. LLVMStyle.BinPackParameters = true;
  184. LLVMStyle.BreakBeforeBinaryOperators = false;
  185. LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
  186. LLVMStyle.BreakConstructorInitializersBeforeComma = false;
  187. LLVMStyle.ColumnLimit = 80;
  188. LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
  189. LLVMStyle.ConstructorInitializerIndentWidth = 4;
  190. LLVMStyle.Cpp11BracedListStyle = false;
  191. LLVMStyle.DerivePointerBinding = false;
  192. LLVMStyle.ExperimentalAutoDetectBinPacking = false;
  193. LLVMStyle.IndentCaseLabels = false;
  194. LLVMStyle.IndentFunctionDeclarationAfterType = false;
  195. LLVMStyle.IndentWidth = 2;
  196. LLVMStyle.TabWidth = 8;
  197. LLVMStyle.MaxEmptyLinesToKeep = 1;
  198. LLVMStyle.NamespaceIndentation = FormatStyle::NI_None;
  199. LLVMStyle.ObjCSpaceBeforeProtocolList = true;
  200. LLVMStyle.PointerBindsToType = false;
  201. LLVMStyle.SpacesBeforeTrailingComments = 1;
  202. LLVMStyle.Standard = FormatStyle::LS_Cpp03;
  203. LLVMStyle.UseTab = FormatStyle::UT_Never;
  204. LLVMStyle.SpacesInParentheses = false;
  205. LLVMStyle.SpaceInEmptyParentheses = false;
  206. LLVMStyle.SpacesInCStyleCastParentheses = false;
  207. LLVMStyle.SpaceAfterControlStatementKeyword = true;
  208. LLVMStyle.SpaceBeforeAssignmentOperators = true;
  209. LLVMStyle.ContinuationIndentWidth = 4;
  210. setDefaultPenalties(LLVMStyle);
  211. LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
  212. LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19;
  213. return LLVMStyle;
  214. }
  215. FormatStyle getGoogleStyle() {
  216. FormatStyle GoogleStyle;
  217. GoogleStyle.AccessModifierOffset = -1;
  218. GoogleStyle.AlignEscapedNewlinesLeft = true;
  219. GoogleStyle.AlignTrailingComments = true;
  220. GoogleStyle.AllowAllParametersOfDeclarationOnNextLine = true;
  221. GoogleStyle.AllowShortIfStatementsOnASingleLine = true;
  222. GoogleStyle.AllowShortLoopsOnASingleLine = true;
  223. GoogleStyle.AlwaysBreakBeforeMultilineStrings = true;
  224. GoogleStyle.AlwaysBreakTemplateDeclarations = true;
  225. GoogleStyle.BinPackParameters = true;
  226. GoogleStyle.BreakBeforeBinaryOperators = false;
  227. GoogleStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
  228. GoogleStyle.BreakConstructorInitializersBeforeComma = false;
  229. GoogleStyle.ColumnLimit = 80;
  230. GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  231. GoogleStyle.ConstructorInitializerIndentWidth = 4;
  232. GoogleStyle.Cpp11BracedListStyle = true;
  233. GoogleStyle.DerivePointerBinding = true;
  234. GoogleStyle.ExperimentalAutoDetectBinPacking = false;
  235. GoogleStyle.IndentCaseLabels = true;
  236. GoogleStyle.IndentFunctionDeclarationAfterType = true;
  237. GoogleStyle.IndentWidth = 2;
  238. GoogleStyle.TabWidth = 8;
  239. GoogleStyle.MaxEmptyLinesToKeep = 1;
  240. GoogleStyle.NamespaceIndentation = FormatStyle::NI_None;
  241. GoogleStyle.ObjCSpaceBeforeProtocolList = false;
  242. GoogleStyle.PointerBindsToType = true;
  243. GoogleStyle.SpacesBeforeTrailingComments = 2;
  244. GoogleStyle.Standard = FormatStyle::LS_Auto;
  245. GoogleStyle.UseTab = FormatStyle::UT_Never;
  246. GoogleStyle.SpacesInParentheses = false;
  247. GoogleStyle.SpaceInEmptyParentheses = false;
  248. GoogleStyle.SpacesInCStyleCastParentheses = false;
  249. GoogleStyle.SpaceAfterControlStatementKeyword = true;
  250. GoogleStyle.SpaceBeforeAssignmentOperators = true;
  251. GoogleStyle.ContinuationIndentWidth = 4;
  252. setDefaultPenalties(GoogleStyle);
  253. GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
  254. GoogleStyle.PenaltyBreakBeforeFirstCallParameter = 1;
  255. return GoogleStyle;
  256. }
  257. FormatStyle getChromiumStyle() {
  258. FormatStyle ChromiumStyle = getGoogleStyle();
  259. ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false;
  260. ChromiumStyle.AllowShortIfStatementsOnASingleLine = false;
  261. ChromiumStyle.AllowShortLoopsOnASingleLine = false;
  262. ChromiumStyle.BinPackParameters = false;
  263. ChromiumStyle.DerivePointerBinding = false;
  264. ChromiumStyle.Standard = FormatStyle::LS_Cpp03;
  265. return ChromiumStyle;
  266. }
  267. FormatStyle getMozillaStyle() {
  268. FormatStyle MozillaStyle = getLLVMStyle();
  269. MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false;
  270. MozillaStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  271. MozillaStyle.DerivePointerBinding = true;
  272. MozillaStyle.IndentCaseLabels = true;
  273. MozillaStyle.ObjCSpaceBeforeProtocolList = false;
  274. MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200;
  275. MozillaStyle.PointerBindsToType = true;
  276. return MozillaStyle;
  277. }
  278. FormatStyle getWebKitStyle() {
  279. FormatStyle Style = getLLVMStyle();
  280. Style.AccessModifierOffset = -4;
  281. Style.AlignTrailingComments = false;
  282. Style.BreakBeforeBinaryOperators = true;
  283. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  284. Style.BreakConstructorInitializersBeforeComma = true;
  285. Style.ColumnLimit = 0;
  286. Style.IndentWidth = 4;
  287. Style.NamespaceIndentation = FormatStyle::NI_Inner;
  288. Style.PointerBindsToType = true;
  289. return Style;
  290. }
  291. bool getPredefinedStyle(StringRef Name, FormatStyle *Style) {
  292. if (Name.equals_lower("llvm"))
  293. *Style = getLLVMStyle();
  294. else if (Name.equals_lower("chromium"))
  295. *Style = getChromiumStyle();
  296. else if (Name.equals_lower("mozilla"))
  297. *Style = getMozillaStyle();
  298. else if (Name.equals_lower("google"))
  299. *Style = getGoogleStyle();
  300. else if (Name.equals_lower("webkit"))
  301. *Style = getWebKitStyle();
  302. else
  303. return false;
  304. return true;
  305. }
  306. llvm::error_code parseConfiguration(StringRef Text, FormatStyle *Style) {
  307. if (Text.trim().empty())
  308. return llvm::make_error_code(llvm::errc::invalid_argument);
  309. llvm::yaml::Input Input(Text);
  310. Input >> *Style;
  311. return Input.error();
  312. }
  313. std::string configurationAsText(const FormatStyle &Style) {
  314. std::string Text;
  315. llvm::raw_string_ostream Stream(Text);
  316. llvm::yaml::Output Output(Stream);
  317. // We use the same mapping method for input and output, so we need a non-const
  318. // reference here.
  319. FormatStyle NonConstStyle = Style;
  320. Output << NonConstStyle;
  321. return Stream.str();
  322. }
  323. namespace {
  324. class NoColumnLimitFormatter {
  325. public:
  326. NoColumnLimitFormatter(ContinuationIndenter *Indenter) : Indenter(Indenter) {}
  327. /// \brief Formats the line starting at \p State, simply keeping all of the
  328. /// input's line breaking decisions.
  329. void format(unsigned FirstIndent, const AnnotatedLine *Line) {
  330. LineState State =
  331. Indenter->getInitialState(FirstIndent, Line, /*DryRun=*/false);
  332. while (State.NextToken != NULL) {
  333. bool Newline =
  334. Indenter->mustBreak(State) ||
  335. (Indenter->canBreak(State) && State.NextToken->NewlinesBefore > 0);
  336. Indenter->addTokenToState(State, Newline, /*DryRun=*/false);
  337. }
  338. }
  339. private:
  340. ContinuationIndenter *Indenter;
  341. };
  342. class UnwrappedLineFormatter {
  343. public:
  344. UnwrappedLineFormatter(ContinuationIndenter *Indenter,
  345. WhitespaceManager *Whitespaces,
  346. const FormatStyle &Style, const AnnotatedLine &Line)
  347. : Indenter(Indenter), Whitespaces(Whitespaces), Style(Style), Line(Line),
  348. Count(0) {}
  349. /// \brief Formats an \c UnwrappedLine and returns the penalty.
  350. ///
  351. /// If \p DryRun is \c false, directly applies the changes.
  352. unsigned format(unsigned FirstIndent, bool DryRun = false) {
  353. LineState State = Indenter->getInitialState(FirstIndent, &Line, DryRun);
  354. // If the ObjC method declaration does not fit on a line, we should format
  355. // it with one arg per line.
  356. if (Line.Type == LT_ObjCMethodDecl)
  357. State.Stack.back().BreakBeforeParameter = true;
  358. // Find best solution in solution space.
  359. return analyzeSolutionSpace(State, DryRun);
  360. }
  361. private:
  362. /// \brief An edge in the solution space from \c Previous->State to \c State,
  363. /// inserting a newline dependent on the \c NewLine.
  364. struct StateNode {
  365. StateNode(const LineState &State, bool NewLine, StateNode *Previous)
  366. : State(State), NewLine(NewLine), Previous(Previous) {}
  367. LineState State;
  368. bool NewLine;
  369. StateNode *Previous;
  370. };
  371. /// \brief A pair of <penalty, count> that is used to prioritize the BFS on.
  372. ///
  373. /// In case of equal penalties, we want to prefer states that were inserted
  374. /// first. During state generation we make sure that we insert states first
  375. /// that break the line as late as possible.
  376. typedef std::pair<unsigned, unsigned> OrderedPenalty;
  377. /// \brief An item in the prioritized BFS search queue. The \c StateNode's
  378. /// \c State has the given \c OrderedPenalty.
  379. typedef std::pair<OrderedPenalty, StateNode *> QueueItem;
  380. /// \brief The BFS queue type.
  381. typedef std::priority_queue<QueueItem, std::vector<QueueItem>,
  382. std::greater<QueueItem> > QueueType;
  383. /// \brief Analyze the entire solution space starting from \p InitialState.
  384. ///
  385. /// This implements a variant of Dijkstra's algorithm on the graph that spans
  386. /// the solution space (\c LineStates are the nodes). The algorithm tries to
  387. /// find the shortest path (the one with lowest penalty) from \p InitialState
  388. /// to a state where all tokens are placed. Returns the penalty.
  389. ///
  390. /// If \p DryRun is \c false, directly applies the changes.
  391. unsigned analyzeSolutionSpace(LineState &InitialState, bool DryRun = false) {
  392. std::set<LineState> Seen;
  393. // Insert start element into queue.
  394. StateNode *Node =
  395. new (Allocator.Allocate()) StateNode(InitialState, false, NULL);
  396. Queue.push(QueueItem(OrderedPenalty(0, Count), Node));
  397. ++Count;
  398. unsigned Penalty = 0;
  399. // While not empty, take first element and follow edges.
  400. while (!Queue.empty()) {
  401. Penalty = Queue.top().first.first;
  402. StateNode *Node = Queue.top().second;
  403. if (Node->State.NextToken == NULL) {
  404. DEBUG(llvm::dbgs() << "\n---\nPenalty for line: " << Penalty << "\n");
  405. break;
  406. }
  407. Queue.pop();
  408. // Cut off the analysis of certain solutions if the analysis gets too
  409. // complex. See description of IgnoreStackForComparison.
  410. if (Count > 10000)
  411. Node->State.IgnoreStackForComparison = true;
  412. if (!Seen.insert(Node->State).second)
  413. // State already examined with lower penalty.
  414. continue;
  415. FormatDecision LastFormat = Node->State.NextToken->Decision;
  416. if (LastFormat == FD_Unformatted || LastFormat == FD_Continue)
  417. addNextStateToQueue(Penalty, Node, /*NewLine=*/false);
  418. if (LastFormat == FD_Unformatted || LastFormat == FD_Break)
  419. addNextStateToQueue(Penalty, Node, /*NewLine=*/true);
  420. }
  421. if (Queue.empty()) {
  422. // We were unable to find a solution, do nothing.
  423. // FIXME: Add diagnostic?
  424. DEBUG(llvm::dbgs() << "Could not find a solution.\n");
  425. return 0;
  426. }
  427. // Reconstruct the solution.
  428. if (!DryRun)
  429. reconstructPath(InitialState, Queue.top().second);
  430. DEBUG(llvm::dbgs() << "Total number of analyzed states: " << Count << "\n");
  431. DEBUG(llvm::dbgs() << "---\n");
  432. return Penalty;
  433. }
  434. void reconstructPath(LineState &State, StateNode *Current) {
  435. std::deque<StateNode *> Path;
  436. // We do not need a break before the initial token.
  437. while (Current->Previous) {
  438. Path.push_front(Current);
  439. Current = Current->Previous;
  440. }
  441. for (std::deque<StateNode *>::iterator I = Path.begin(), E = Path.end();
  442. I != E; ++I) {
  443. unsigned Penalty = 0;
  444. formatChildren(State, (*I)->NewLine, /*DryRun=*/false, Penalty);
  445. Penalty += Indenter->addTokenToState(State, (*I)->NewLine, false);
  446. DEBUG({
  447. if ((*I)->NewLine) {
  448. llvm::dbgs() << "Penalty for placing "
  449. << (*I)->Previous->State.NextToken->Tok.getName() << ": "
  450. << Penalty << "\n";
  451. }
  452. });
  453. }
  454. }
  455. /// \brief Add the following state to the analysis queue \c Queue.
  456. ///
  457. /// Assume the current state is \p PreviousNode and has been reached with a
  458. /// penalty of \p Penalty. Insert a line break if \p NewLine is \c true.
  459. void addNextStateToQueue(unsigned Penalty, StateNode *PreviousNode,
  460. bool NewLine) {
  461. if (NewLine && !Indenter->canBreak(PreviousNode->State))
  462. return;
  463. if (!NewLine && Indenter->mustBreak(PreviousNode->State))
  464. return;
  465. StateNode *Node = new (Allocator.Allocate())
  466. StateNode(PreviousNode->State, NewLine, PreviousNode);
  467. if (!formatChildren(Node->State, NewLine, /*DryRun=*/true, Penalty))
  468. return;
  469. Penalty += Indenter->addTokenToState(Node->State, NewLine, true);
  470. Queue.push(QueueItem(OrderedPenalty(Penalty, Count), Node));
  471. ++Count;
  472. }
  473. /// \brief If the \p State's next token is an r_brace closing a nested block,
  474. /// format the nested block before it.
  475. ///
  476. /// Returns \c true if all children could be placed successfully and adapts
  477. /// \p Penalty as well as \p State. If \p DryRun is false, also directly
  478. /// creates changes using \c Whitespaces.
  479. ///
  480. /// The crucial idea here is that children always get formatted upon
  481. /// encountering the closing brace right after the nested block. Now, if we
  482. /// are currently trying to keep the "}" on the same line (i.e. \p NewLine is
  483. /// \c false), the entire block has to be kept on the same line (which is only
  484. /// possible if it fits on the line, only contains a single statement, etc.
  485. ///
  486. /// If \p NewLine is true, we format the nested block on separate lines, i.e.
  487. /// break after the "{", format all lines with correct indentation and the put
  488. /// the closing "}" on yet another new line.
  489. ///
  490. /// This enables us to keep the simple structure of the
  491. /// \c UnwrappedLineFormatter, where we only have two options for each token:
  492. /// break or don't break.
  493. bool formatChildren(LineState &State, bool NewLine, bool DryRun,
  494. unsigned &Penalty) {
  495. const FormatToken &Previous = *State.NextToken->Previous;
  496. const FormatToken *LBrace = State.NextToken->getPreviousNonComment();
  497. if (!LBrace || LBrace->isNot(tok::l_brace) ||
  498. LBrace->BlockKind != BK_Block || Previous.Children.size() == 0)
  499. // The previous token does not open a block. Nothing to do. We don't
  500. // assert so that we can simply call this function for all tokens.
  501. return true;
  502. if (NewLine) {
  503. unsigned ParentIndent = State.Stack.back().Indent;
  504. for (SmallVector<AnnotatedLine *, 1>::const_iterator
  505. I = Previous.Children.begin(),
  506. E = Previous.Children.end();
  507. I != E; ++I) {
  508. unsigned Indent =
  509. ParentIndent + ((*I)->Level - Line.Level - 1) * Style.IndentWidth;
  510. if (!DryRun) {
  511. unsigned Newlines = std::min((*I)->First->NewlinesBefore,
  512. Style.MaxEmptyLinesToKeep + 1);
  513. Newlines = std::max(1u, Newlines);
  514. Whitespaces->replaceWhitespace(
  515. *(*I)->First, Newlines, (*I)->Level, /*Spaces=*/Indent,
  516. /*StartOfTokenColumn=*/Indent, Line.InPPDirective);
  517. }
  518. UnwrappedLineFormatter Formatter(Indenter, Whitespaces, Style, **I);
  519. Penalty += Formatter.format(Indent, DryRun);
  520. }
  521. return true;
  522. }
  523. if (Previous.Children.size() > 1)
  524. return false; // Cannot merge multiple statements into a single line.
  525. // We can't put the closing "}" on a line with a trailing comment.
  526. if (Previous.Children[0]->Last->isTrailingComment())
  527. return false;
  528. if (!DryRun) {
  529. Whitespaces->replaceWhitespace(
  530. *Previous.Children[0]->First,
  531. /*Newlines=*/0, /*IndentLevel=*/0, /*Spaces=*/1,
  532. /*StartOfTokenColumn=*/State.Column, State.Line->InPPDirective);
  533. UnwrappedLineFormatter Formatter(Indenter, Whitespaces, Style,
  534. *Previous.Children[0]);
  535. Penalty += Formatter.format(State.Column + 1, DryRun);
  536. }
  537. State.Column += 1 + Previous.Children[0]->Last->TotalLength;
  538. return true;
  539. }
  540. ContinuationIndenter *Indenter;
  541. WhitespaceManager *Whitespaces;
  542. FormatStyle Style;
  543. const AnnotatedLine &Line;
  544. llvm::SpecificBumpPtrAllocator<StateNode> Allocator;
  545. QueueType Queue;
  546. // Increasing count of \c StateNode items we have created. This is used
  547. // to create a deterministic order independent of the container.
  548. unsigned Count;
  549. };
  550. class FormatTokenLexer {
  551. public:
  552. FormatTokenLexer(Lexer &Lex, SourceManager &SourceMgr, FormatStyle &Style,
  553. encoding::Encoding Encoding)
  554. : FormatTok(NULL), GreaterStashed(false), Column(0),
  555. TrailingWhitespace(0), Lex(Lex), SourceMgr(SourceMgr), Style(Style),
  556. IdentTable(getFormattingLangOpts()), Encoding(Encoding) {
  557. Lex.SetKeepWhitespaceMode(true);
  558. }
  559. ArrayRef<FormatToken *> lex() {
  560. assert(Tokens.empty());
  561. do {
  562. Tokens.push_back(getNextToken());
  563. maybeJoinPreviousTokens();
  564. } while (Tokens.back()->Tok.isNot(tok::eof));
  565. return Tokens;
  566. }
  567. IdentifierTable &getIdentTable() { return IdentTable; }
  568. private:
  569. void maybeJoinPreviousTokens() {
  570. if (Tokens.size() < 4)
  571. return;
  572. FormatToken *Last = Tokens.back();
  573. if (!Last->is(tok::r_paren))
  574. return;
  575. FormatToken *String = Tokens[Tokens.size() - 2];
  576. if (!String->is(tok::string_literal) || String->IsMultiline)
  577. return;
  578. if (!Tokens[Tokens.size() - 3]->is(tok::l_paren))
  579. return;
  580. FormatToken *Macro = Tokens[Tokens.size() - 4];
  581. if (Macro->TokenText != "_T")
  582. return;
  583. const char *Start = Macro->TokenText.data();
  584. const char *End = Last->TokenText.data() + Last->TokenText.size();
  585. String->TokenText = StringRef(Start, End - Start);
  586. String->IsFirst = Macro->IsFirst;
  587. String->LastNewlineOffset = Macro->LastNewlineOffset;
  588. String->WhitespaceRange = Macro->WhitespaceRange;
  589. String->OriginalColumn = Macro->OriginalColumn;
  590. String->ColumnWidth = encoding::columnWidthWithTabs(
  591. String->TokenText, String->OriginalColumn, Style.TabWidth, Encoding);
  592. Tokens.pop_back();
  593. Tokens.pop_back();
  594. Tokens.pop_back();
  595. Tokens.back() = String;
  596. }
  597. FormatToken *getNextToken() {
  598. if (GreaterStashed) {
  599. // Create a synthesized second '>' token.
  600. // FIXME: Increment Column and set OriginalColumn.
  601. Token Greater = FormatTok->Tok;
  602. FormatTok = new (Allocator.Allocate()) FormatToken;
  603. FormatTok->Tok = Greater;
  604. SourceLocation GreaterLocation =
  605. FormatTok->Tok.getLocation().getLocWithOffset(1);
  606. FormatTok->WhitespaceRange =
  607. SourceRange(GreaterLocation, GreaterLocation);
  608. FormatTok->TokenText = ">";
  609. FormatTok->ColumnWidth = 1;
  610. GreaterStashed = false;
  611. return FormatTok;
  612. }
  613. FormatTok = new (Allocator.Allocate()) FormatToken;
  614. readRawToken(*FormatTok);
  615. SourceLocation WhitespaceStart =
  616. FormatTok->Tok.getLocation().getLocWithOffset(-TrailingWhitespace);
  617. if (SourceMgr.getFileOffset(WhitespaceStart) == 0)
  618. FormatTok->IsFirst = true;
  619. // Consume and record whitespace until we find a significant token.
  620. unsigned WhitespaceLength = TrailingWhitespace;
  621. while (FormatTok->Tok.is(tok::unknown)) {
  622. for (int i = 0, e = FormatTok->TokenText.size(); i != e; ++i) {
  623. switch (FormatTok->TokenText[i]) {
  624. case '\n':
  625. ++FormatTok->NewlinesBefore;
  626. // FIXME: This is technically incorrect, as it could also
  627. // be a literal backslash at the end of the line.
  628. if (i == 0 || (FormatTok->TokenText[i - 1] != '\\' &&
  629. (FormatTok->TokenText[i - 1] != '\r' || i == 1 ||
  630. FormatTok->TokenText[i - 2] != '\\')))
  631. FormatTok->HasUnescapedNewline = true;
  632. FormatTok->LastNewlineOffset = WhitespaceLength + i + 1;
  633. Column = 0;
  634. break;
  635. case '\r':
  636. case '\f':
  637. case '\v':
  638. Column = 0;
  639. break;
  640. case ' ':
  641. ++Column;
  642. break;
  643. case '\t':
  644. Column += Style.TabWidth - Column % Style.TabWidth;
  645. break;
  646. case '\\':
  647. ++Column;
  648. if (i + 1 == e || (FormatTok->TokenText[i + 1] != '\r' &&
  649. FormatTok->TokenText[i + 1] != '\n'))
  650. FormatTok->Type = TT_ImplicitStringLiteral;
  651. break;
  652. default:
  653. FormatTok->Type = TT_ImplicitStringLiteral;
  654. ++Column;
  655. break;
  656. }
  657. }
  658. if (FormatTok->Type == TT_ImplicitStringLiteral)
  659. break;
  660. WhitespaceLength += FormatTok->Tok.getLength();
  661. readRawToken(*FormatTok);
  662. }
  663. // In case the token starts with escaped newlines, we want to
  664. // take them into account as whitespace - this pattern is quite frequent
  665. // in macro definitions.
  666. // FIXME: Add a more explicit test.
  667. while (FormatTok->TokenText.size() > 1 && FormatTok->TokenText[0] == '\\' &&
  668. FormatTok->TokenText[1] == '\n') {
  669. // FIXME: ++FormatTok->NewlinesBefore is missing...
  670. WhitespaceLength += 2;
  671. Column = 0;
  672. FormatTok->TokenText = FormatTok->TokenText.substr(2);
  673. }
  674. FormatTok->WhitespaceRange = SourceRange(
  675. WhitespaceStart, WhitespaceStart.getLocWithOffset(WhitespaceLength));
  676. FormatTok->OriginalColumn = Column;
  677. TrailingWhitespace = 0;
  678. if (FormatTok->Tok.is(tok::comment)) {
  679. // FIXME: Add the trimmed whitespace to Column.
  680. StringRef UntrimmedText = FormatTok->TokenText;
  681. FormatTok->TokenText = FormatTok->TokenText.rtrim(" \t\v\f");
  682. TrailingWhitespace = UntrimmedText.size() - FormatTok->TokenText.size();
  683. } else if (FormatTok->Tok.is(tok::raw_identifier)) {
  684. IdentifierInfo &Info = IdentTable.get(FormatTok->TokenText);
  685. FormatTok->Tok.setIdentifierInfo(&Info);
  686. FormatTok->Tok.setKind(Info.getTokenID());
  687. } else if (FormatTok->Tok.is(tok::greatergreater)) {
  688. FormatTok->Tok.setKind(tok::greater);
  689. FormatTok->TokenText = FormatTok->TokenText.substr(0, 1);
  690. GreaterStashed = true;
  691. }
  692. // Now FormatTok is the next non-whitespace token.
  693. StringRef Text = FormatTok->TokenText;
  694. size_t FirstNewlinePos = Text.find('\n');
  695. if (FirstNewlinePos == StringRef::npos) {
  696. // FIXME: ColumnWidth actually depends on the start column, we need to
  697. // take this into account when the token is moved.
  698. FormatTok->ColumnWidth =
  699. encoding::columnWidthWithTabs(Text, Column, Style.TabWidth, Encoding);
  700. Column += FormatTok->ColumnWidth;
  701. } else {
  702. FormatTok->IsMultiline = true;
  703. // FIXME: ColumnWidth actually depends on the start column, we need to
  704. // take this into account when the token is moved.
  705. FormatTok->ColumnWidth = encoding::columnWidthWithTabs(
  706. Text.substr(0, FirstNewlinePos), Column, Style.TabWidth, Encoding);
  707. // The last line of the token always starts in column 0.
  708. // Thus, the length can be precomputed even in the presence of tabs.
  709. FormatTok->LastLineColumnWidth = encoding::columnWidthWithTabs(
  710. Text.substr(Text.find_last_of('\n') + 1), 0, Style.TabWidth,
  711. Encoding);
  712. Column = FormatTok->LastLineColumnWidth;
  713. }
  714. return FormatTok;
  715. }
  716. FormatToken *FormatTok;
  717. bool GreaterStashed;
  718. unsigned Column;
  719. unsigned TrailingWhitespace;
  720. Lexer &Lex;
  721. SourceManager &SourceMgr;
  722. FormatStyle &Style;
  723. IdentifierTable IdentTable;
  724. encoding::Encoding Encoding;
  725. llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
  726. SmallVector<FormatToken *, 16> Tokens;
  727. void readRawToken(FormatToken &Tok) {
  728. Lex.LexFromRawLexer(Tok.Tok);
  729. Tok.TokenText = StringRef(SourceMgr.getCharacterData(Tok.Tok.getLocation()),
  730. Tok.Tok.getLength());
  731. // For formatting, treat unterminated string literals like normal string
  732. // literals.
  733. if (Tok.is(tok::unknown) && !Tok.TokenText.empty() &&
  734. Tok.TokenText[0] == '"') {
  735. Tok.Tok.setKind(tok::string_literal);
  736. Tok.IsUnterminatedLiteral = true;
  737. }
  738. }
  739. };
  740. class Formatter : public UnwrappedLineConsumer {
  741. public:
  742. Formatter(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr,
  743. const std::vector<CharSourceRange> &Ranges)
  744. : Style(Style), Lex(Lex), SourceMgr(SourceMgr),
  745. Whitespaces(SourceMgr, Style, inputUsesCRLF(Lex.getBuffer())),
  746. Ranges(Ranges), UnwrappedLines(1),
  747. Encoding(encoding::detectEncoding(Lex.getBuffer())) {
  748. DEBUG(llvm::dbgs() << "File encoding: "
  749. << (Encoding == encoding::Encoding_UTF8 ? "UTF8"
  750. : "unknown")
  751. << "\n");
  752. }
  753. tooling::Replacements format() {
  754. tooling::Replacements Result;
  755. FormatTokenLexer Tokens(Lex, SourceMgr, Style, Encoding);
  756. UnwrappedLineParser Parser(Style, Tokens.lex(), *this);
  757. bool StructuralError = Parser.parse();
  758. assert(UnwrappedLines.rbegin()->empty());
  759. for (unsigned Run = 0, RunE = UnwrappedLines.size(); Run + 1 != RunE;
  760. ++Run) {
  761. DEBUG(llvm::dbgs() << "Run " << Run << "...\n");
  762. SmallVector<AnnotatedLine *, 16> AnnotatedLines;
  763. for (unsigned i = 0, e = UnwrappedLines[Run].size(); i != e; ++i) {
  764. AnnotatedLines.push_back(new AnnotatedLine(UnwrappedLines[Run][i]));
  765. }
  766. tooling::Replacements RunResult =
  767. format(AnnotatedLines, StructuralError, Tokens);
  768. DEBUG({
  769. llvm::dbgs() << "Replacements for run " << Run << ":\n";
  770. for (tooling::Replacements::iterator I = RunResult.begin(),
  771. E = RunResult.end();
  772. I != E; ++I) {
  773. llvm::dbgs() << I->toString() << "\n";
  774. }
  775. });
  776. for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
  777. delete AnnotatedLines[i];
  778. }
  779. Result.insert(RunResult.begin(), RunResult.end());
  780. Whitespaces.reset();
  781. }
  782. return Result;
  783. }
  784. tooling::Replacements format(SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
  785. bool StructuralError, FormatTokenLexer &Tokens) {
  786. TokenAnnotator Annotator(Style, Tokens.getIdentTable().get("in"));
  787. for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
  788. Annotator.annotate(*AnnotatedLines[i]);
  789. }
  790. deriveLocalStyle(AnnotatedLines);
  791. for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
  792. Annotator.calculateFormattingInformation(*AnnotatedLines[i]);
  793. }
  794. Annotator.setCommentLineLevels(AnnotatedLines);
  795. std::vector<int> IndentForLevel;
  796. bool PreviousLineWasTouched = false;
  797. const AnnotatedLine *PreviousLine = NULL;
  798. bool FormatPPDirective = false;
  799. for (SmallVectorImpl<AnnotatedLine *>::iterator I = AnnotatedLines.begin(),
  800. E = AnnotatedLines.end();
  801. I != E; ++I) {
  802. const AnnotatedLine &TheLine = **I;
  803. const FormatToken *FirstTok = TheLine.First;
  804. int Offset = getIndentOffset(*TheLine.First);
  805. // Check whether this line is part of a formatted preprocessor directive.
  806. if (FirstTok->HasUnescapedNewline)
  807. FormatPPDirective = false;
  808. if (!FormatPPDirective && TheLine.InPPDirective &&
  809. (touchesLine(TheLine) || touchesPPDirective(I + 1, E)))
  810. FormatPPDirective = true;
  811. // Determine indent and try to merge multiple unwrapped lines.
  812. while (IndentForLevel.size() <= TheLine.Level)
  813. IndentForLevel.push_back(-1);
  814. IndentForLevel.resize(TheLine.Level + 1);
  815. unsigned Indent = getIndent(IndentForLevel, TheLine.Level);
  816. if (static_cast<int>(Indent) + Offset >= 0)
  817. Indent += Offset;
  818. tryFitMultipleLinesInOne(Indent, I, E);
  819. bool WasMoved = PreviousLineWasTouched && FirstTok->NewlinesBefore == 0;
  820. if (TheLine.First->is(tok::eof)) {
  821. if (PreviousLineWasTouched) {
  822. unsigned Newlines = std::min(FirstTok->NewlinesBefore, 1u);
  823. Whitespaces.replaceWhitespace(*TheLine.First, Newlines,
  824. /*IndentLevel=*/0, /*Spaces=*/0,
  825. /*TargetColumn=*/0);
  826. }
  827. } else if (TheLine.Type != LT_Invalid &&
  828. (WasMoved || FormatPPDirective || touchesLine(TheLine))) {
  829. unsigned LevelIndent = getIndent(IndentForLevel, TheLine.Level);
  830. if (FirstTok->WhitespaceRange.isValid()) {
  831. formatFirstToken(*TheLine.First, PreviousLine, TheLine.Level, Indent,
  832. TheLine.InPPDirective);
  833. } else {
  834. Indent = LevelIndent = FirstTok->OriginalColumn;
  835. }
  836. ContinuationIndenter Indenter(Style, SourceMgr, Whitespaces, Encoding,
  837. BinPackInconclusiveFunctions);
  838. // If everything fits on a single line, just put it there.
  839. unsigned ColumnLimit = Style.ColumnLimit;
  840. AnnotatedLine *NextLine = *(I + 1);
  841. if ((I + 1) != E && NextLine->InPPDirective &&
  842. !NextLine->First->HasUnescapedNewline)
  843. ColumnLimit = getColumnLimit(TheLine.InPPDirective);
  844. if (TheLine.Last->TotalLength + Indent <= ColumnLimit) {
  845. LineState State =
  846. Indenter.getInitialState(Indent, &TheLine, /*DryRun=*/false);
  847. while (State.NextToken != NULL)
  848. Indenter.addTokenToState(State, false, false);
  849. } else if (Style.ColumnLimit == 0) {
  850. NoColumnLimitFormatter Formatter(&Indenter);
  851. Formatter.format(Indent, &TheLine);
  852. } else {
  853. UnwrappedLineFormatter Formatter(&Indenter, &Whitespaces, Style,
  854. TheLine);
  855. Formatter.format(Indent);
  856. }
  857. IndentForLevel[TheLine.Level] = LevelIndent;
  858. PreviousLineWasTouched = true;
  859. } else {
  860. // Format the first token if necessary, and notify the WhitespaceManager
  861. // about the unchanged whitespace.
  862. for (FormatToken *Tok = TheLine.First; Tok != NULL; Tok = Tok->Next) {
  863. if (Tok == TheLine.First &&
  864. (Tok->NewlinesBefore > 0 || Tok->IsFirst)) {
  865. unsigned LevelIndent = Tok->OriginalColumn;
  866. // Remove trailing whitespace of the previous line if it was
  867. // touched.
  868. if (PreviousLineWasTouched || touchesEmptyLineBefore(TheLine)) {
  869. formatFirstToken(*Tok, PreviousLine, TheLine.Level, LevelIndent,
  870. TheLine.InPPDirective);
  871. } else {
  872. Whitespaces.addUntouchableToken(*Tok, TheLine.InPPDirective);
  873. }
  874. if (static_cast<int>(LevelIndent) - Offset >= 0)
  875. LevelIndent -= Offset;
  876. if (Tok->isNot(tok::comment))
  877. IndentForLevel[TheLine.Level] = LevelIndent;
  878. } else {
  879. Whitespaces.addUntouchableToken(*Tok, TheLine.InPPDirective);
  880. }
  881. }
  882. // If we did not reformat this unwrapped line, the column at the end of
  883. // the last token is unchanged - thus, we can calculate the end of the
  884. // last token.
  885. PreviousLineWasTouched = false;
  886. }
  887. for (FormatToken *Tok = TheLine.First; Tok != NULL; Tok = Tok->Next) {
  888. Tok->Finalized = true;
  889. }
  890. PreviousLine = *I;
  891. }
  892. return Whitespaces.generateReplacements();
  893. }
  894. private:
  895. static bool inputUsesCRLF(StringRef Text) {
  896. return Text.count('\r') * 2 > Text.count('\n');
  897. }
  898. void
  899. deriveLocalStyle(const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
  900. unsigned CountBoundToVariable = 0;
  901. unsigned CountBoundToType = 0;
  902. bool HasCpp03IncompatibleFormat = false;
  903. bool HasBinPackedFunction = false;
  904. bool HasOnePerLineFunction = false;
  905. for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
  906. if (!AnnotatedLines[i]->First->Next)
  907. continue;
  908. FormatToken *Tok = AnnotatedLines[i]->First->Next;
  909. while (Tok->Next) {
  910. if (Tok->Type == TT_PointerOrReference) {
  911. bool SpacesBefore =
  912. Tok->WhitespaceRange.getBegin() != Tok->WhitespaceRange.getEnd();
  913. bool SpacesAfter = Tok->Next->WhitespaceRange.getBegin() !=
  914. Tok->Next->WhitespaceRange.getEnd();
  915. if (SpacesBefore && !SpacesAfter)
  916. ++CountBoundToVariable;
  917. else if (!SpacesBefore && SpacesAfter)
  918. ++CountBoundToType;
  919. }
  920. if (Tok->WhitespaceRange.getBegin() == Tok->WhitespaceRange.getEnd()) {
  921. if (Tok->is(tok::coloncolon) &&
  922. Tok->Previous->Type == TT_TemplateOpener)
  923. HasCpp03IncompatibleFormat = true;
  924. if (Tok->Type == TT_TemplateCloser &&
  925. Tok->Previous->Type == TT_TemplateCloser)
  926. HasCpp03IncompatibleFormat = true;
  927. }
  928. if (Tok->PackingKind == PPK_BinPacked)
  929. HasBinPackedFunction = true;
  930. if (Tok->PackingKind == PPK_OnePerLine)
  931. HasOnePerLineFunction = true;
  932. Tok = Tok->Next;
  933. }
  934. }
  935. if (Style.DerivePointerBinding) {
  936. if (CountBoundToType > CountBoundToVariable)
  937. Style.PointerBindsToType = true;
  938. else if (CountBoundToType < CountBoundToVariable)
  939. Style.PointerBindsToType = false;
  940. }
  941. if (Style.Standard == FormatStyle::LS_Auto) {
  942. Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11
  943. : FormatStyle::LS_Cpp03;
  944. }
  945. BinPackInconclusiveFunctions =
  946. HasBinPackedFunction || !HasOnePerLineFunction;
  947. }
  948. /// \brief Get the indent of \p Level from \p IndentForLevel.
  949. ///
  950. /// \p IndentForLevel must contain the indent for the level \c l
  951. /// at \p IndentForLevel[l], or a value < 0 if the indent for
  952. /// that level is unknown.
  953. unsigned getIndent(const std::vector<int> IndentForLevel, unsigned Level) {
  954. if (IndentForLevel[Level] != -1)
  955. return IndentForLevel[Level];
  956. if (Level == 0)
  957. return 0;
  958. return getIndent(IndentForLevel, Level - 1) + Style.IndentWidth;
  959. }
  960. /// \brief Get the offset of the line relatively to the level.
  961. ///
  962. /// For example, 'public:' labels in classes are offset by 1 or 2
  963. /// characters to the left from their level.
  964. int getIndentOffset(const FormatToken &RootToken) {
  965. if (RootToken.isAccessSpecifier(false) || RootToken.isObjCAccessSpecifier())
  966. return Style.AccessModifierOffset;
  967. return 0;
  968. }
  969. /// \brief Tries to merge lines into one.
  970. ///
  971. /// This will change \c Line and \c AnnotatedLine to contain the merged line,
  972. /// if possible; note that \c I will be incremented when lines are merged.
  973. void tryFitMultipleLinesInOne(unsigned Indent,
  974. SmallVectorImpl<AnnotatedLine *>::iterator &I,
  975. SmallVectorImpl<AnnotatedLine *>::iterator E) {
  976. // We can never merge stuff if there are trailing line comments.
  977. AnnotatedLine *TheLine = *I;
  978. if (TheLine->Last->Type == TT_LineComment)
  979. return;
  980. if (Indent > Style.ColumnLimit)
  981. return;
  982. unsigned Limit = Style.ColumnLimit - Indent;
  983. // If we already exceed the column limit, we set 'Limit' to 0. The different
  984. // tryMerge..() functions can then decide whether to still do merging.
  985. Limit = TheLine->Last->TotalLength > Limit
  986. ? 0
  987. : Limit - TheLine->Last->TotalLength;
  988. if (I + 1 == E || (*(I + 1))->Type == LT_Invalid)
  989. return;
  990. if (TheLine->Last->is(tok::l_brace)) {
  991. tryMergeSimpleBlock(I, E, Limit);
  992. } else if (Style.AllowShortIfStatementsOnASingleLine &&
  993. TheLine->First->is(tok::kw_if)) {
  994. tryMergeSimpleControlStatement(I, E, Limit);
  995. } else if (Style.AllowShortLoopsOnASingleLine &&
  996. TheLine->First->isOneOf(tok::kw_for, tok::kw_while)) {
  997. tryMergeSimpleControlStatement(I, E, Limit);
  998. } else if (TheLine->InPPDirective && (TheLine->First->HasUnescapedNewline ||
  999. TheLine->First->IsFirst)) {
  1000. tryMergeSimplePPDirective(I, E, Limit);
  1001. }
  1002. }
  1003. void tryMergeSimplePPDirective(SmallVectorImpl<AnnotatedLine *>::iterator &I,
  1004. SmallVectorImpl<AnnotatedLine *>::iterator E,
  1005. unsigned Limit) {
  1006. if (Limit == 0)
  1007. return;
  1008. AnnotatedLine &Line = **I;
  1009. if (!(*(I + 1))->InPPDirective || (*(I + 1))->First->HasUnescapedNewline)
  1010. return;
  1011. if (I + 2 != E && (*(I + 2))->InPPDirective &&
  1012. !(*(I + 2))->First->HasUnescapedNewline)
  1013. return;
  1014. if (1 + (*(I + 1))->Last->TotalLength > Limit)
  1015. return;
  1016. join(Line, **(++I));
  1017. }
  1018. void
  1019. tryMergeSimpleControlStatement(SmallVectorImpl<AnnotatedLine *>::iterator &I,
  1020. SmallVectorImpl<AnnotatedLine *>::iterator E,
  1021. unsigned Limit) {
  1022. if (Limit == 0)
  1023. return;
  1024. if (Style.BreakBeforeBraces == FormatStyle::BS_Allman &&
  1025. (*(I + 1))->First->is(tok::l_brace))
  1026. return;
  1027. if ((*(I + 1))->InPPDirective != (*I)->InPPDirective ||
  1028. ((*(I + 1))->InPPDirective && (*(I + 1))->First->HasUnescapedNewline))
  1029. return;
  1030. AnnotatedLine &Line = **I;
  1031. if (Line.Last->isNot(tok::r_paren))
  1032. return;
  1033. if (1 + (*(I + 1))->Last->TotalLength > Limit)
  1034. return;
  1035. if ((*(I + 1))->First->isOneOf(tok::semi, tok::kw_if, tok::kw_for,
  1036. tok::kw_while) ||
  1037. (*(I + 1))->First->Type == TT_LineComment)
  1038. return;
  1039. // Only inline simple if's (no nested if or else).
  1040. if (I + 2 != E && Line.First->is(tok::kw_if) &&
  1041. (*(I + 2))->First->is(tok::kw_else))
  1042. return;
  1043. join(Line, **(++I));
  1044. }
  1045. void tryMergeSimpleBlock(SmallVectorImpl<AnnotatedLine *>::iterator &I,
  1046. SmallVectorImpl<AnnotatedLine *>::iterator E,
  1047. unsigned Limit) {
  1048. // No merging if the brace already is on the next line.
  1049. if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
  1050. return;
  1051. // First, check that the current line allows merging. This is the case if
  1052. // we're not in a control flow statement and the last token is an opening
  1053. // brace.
  1054. AnnotatedLine &Line = **I;
  1055. if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::r_brace,
  1056. tok::kw_else, tok::kw_try, tok::kw_catch,
  1057. tok::kw_for,
  1058. // This gets rid of all ObjC @ keywords and methods.
  1059. tok::at, tok::minus, tok::plus))
  1060. return;
  1061. FormatToken *Tok = (*(I + 1))->First;
  1062. if (Tok->is(tok::r_brace) && !Tok->MustBreakBefore &&
  1063. (Tok->getNextNonComment() == NULL ||
  1064. Tok->getNextNonComment()->is(tok::semi))) {
  1065. // We merge empty blocks even if the line exceeds the column limit.
  1066. Tok->SpacesRequiredBefore = 0;
  1067. Tok->CanBreakBefore = true;
  1068. join(Line, **(I + 1));
  1069. I += 1;
  1070. } else if (Limit != 0 && Line.First->isNot(tok::kw_namespace)) {
  1071. // Check that we still have three lines and they fit into the limit.
  1072. if (I + 2 == E || (*(I + 2))->Type == LT_Invalid ||
  1073. !nextTwoLinesFitInto(I, Limit))
  1074. return;
  1075. // Second, check that the next line does not contain any braces - if it
  1076. // does, readability declines when putting it into a single line.
  1077. if ((*(I + 1))->Last->Type == TT_LineComment || Tok->MustBreakBefore)
  1078. return;
  1079. do {
  1080. if (Tok->isOneOf(tok::l_brace, tok::r_brace))
  1081. return;
  1082. Tok = Tok->Next;
  1083. } while (Tok != NULL);
  1084. // Last, check that the third line contains a single closing brace.
  1085. Tok = (*(I + 2))->First;
  1086. if (Tok->getNextNonComment() != NULL || Tok->isNot(tok::r_brace) ||
  1087. Tok->MustBreakBefore)
  1088. return;
  1089. join(Line, **(I + 1));
  1090. join(Line, **(I + 2));
  1091. I += 2;
  1092. }
  1093. }
  1094. bool nextTwoLinesFitInto(SmallVectorImpl<AnnotatedLine *>::iterator I,
  1095. unsigned Limit) {
  1096. return 1 + (*(I + 1))->Last->TotalLength + 1 +
  1097. (*(I + 2))->Last->TotalLength <=
  1098. Limit;
  1099. }
  1100. void join(AnnotatedLine &A, const AnnotatedLine &B) {
  1101. assert(!A.Last->Next);
  1102. assert(!B.First->Previous);
  1103. A.Last->Next = B.First;
  1104. B.First->Previous = A.Last;
  1105. unsigned LengthA = A.Last->TotalLength + B.First->SpacesRequiredBefore;
  1106. for (FormatToken *Tok = B.First; Tok; Tok = Tok->Next) {
  1107. Tok->TotalLength += LengthA;
  1108. A.Last = Tok;
  1109. }
  1110. }
  1111. bool touchesRanges(const CharSourceRange &Range) {
  1112. for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
  1113. if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),
  1114. Ranges[i].getBegin()) &&
  1115. !SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(),
  1116. Range.getBegin()))
  1117. return true;
  1118. }
  1119. return false;
  1120. }
  1121. bool touchesLine(const AnnotatedLine &TheLine) {
  1122. const FormatToken *First = TheLine.First;
  1123. const FormatToken *Last = TheLine.Last;
  1124. CharSourceRange LineRange = CharSourceRange::getCharRange(
  1125. First->WhitespaceRange.getBegin().getLocWithOffset(
  1126. First->LastNewlineOffset),
  1127. Last->getStartOfNonWhitespace().getLocWithOffset(
  1128. Last->TokenText.size() - 1));
  1129. return touchesRanges(LineRange);
  1130. }
  1131. bool touchesPPDirective(SmallVectorImpl<AnnotatedLine *>::iterator I,
  1132. SmallVectorImpl<AnnotatedLine *>::iterator E) {
  1133. for (; I != E; ++I) {
  1134. if ((*I)->First->HasUnescapedNewline)
  1135. return false;
  1136. if (touchesLine(**I))
  1137. return true;
  1138. }
  1139. return false;
  1140. }
  1141. bool touchesEmptyLineBefore(const AnnotatedLine &TheLine) {
  1142. const FormatToken *First = TheLine.First;
  1143. CharSourceRange LineRange = CharSourceRange::getCharRange(
  1144. First->WhitespaceRange.getBegin(),
  1145. First->WhitespaceRange.getBegin().getLocWithOffset(
  1146. First->LastNewlineOffset));
  1147. return touchesRanges(LineRange);
  1148. }
  1149. virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) {
  1150. assert(!UnwrappedLines.empty());
  1151. UnwrappedLines.back().push_back(TheLine);
  1152. }
  1153. virtual void finishRun() {
  1154. UnwrappedLines.push_back(SmallVector<UnwrappedLine, 16>());
  1155. }
  1156. /// \brief Add a new line and the required indent before the first Token
  1157. /// of the \c UnwrappedLine if there was no structural parsing error.
  1158. void formatFirstToken(FormatToken &RootToken,
  1159. const AnnotatedLine *PreviousLine, unsigned IndentLevel,
  1160. unsigned Indent, bool InPPDirective) {
  1161. unsigned Newlines =
  1162. std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);
  1163. // Remove empty lines before "}" where applicable.
  1164. if (RootToken.is(tok::r_brace) &&
  1165. (!RootToken.Next ||
  1166. (RootToken.Next->is(tok::semi) && !RootToken.Next->Next)))
  1167. Newlines = std::min(Newlines, 1u);
  1168. if (Newlines == 0 && !RootToken.IsFirst)
  1169. Newlines = 1;
  1170. // Insert extra new line before access specifiers.
  1171. if (PreviousLine && PreviousLine->Last->isOneOf(tok::semi, tok::r_brace) &&
  1172. RootToken.isAccessSpecifier() && RootToken.NewlinesBefore == 1)
  1173. ++Newlines;
  1174. // Remove empty lines after access specifiers.
  1175. if (PreviousLine && PreviousLine->First->isAccessSpecifier())
  1176. Newlines = std::min(1u, Newlines);
  1177. Whitespaces.replaceWhitespace(
  1178. RootToken, Newlines, IndentLevel, Indent, Indent,
  1179. InPPDirective && !RootToken.HasUnescapedNewline);
  1180. }
  1181. unsigned getColumnLimit(bool InPPDirective) const {
  1182. // In preprocessor directives reserve two chars for trailing " \"
  1183. return Style.ColumnLimit - (InPPDirective ? 2 : 0);
  1184. }
  1185. FormatStyle Style;
  1186. Lexer &Lex;
  1187. SourceManager &SourceMgr;
  1188. WhitespaceManager Whitespaces;
  1189. std::vector<CharSourceRange> Ranges;
  1190. SmallVector<SmallVector<UnwrappedLine, 16>, 2> UnwrappedLines;
  1191. encoding::Encoding Encoding;
  1192. bool BinPackInconclusiveFunctions;
  1193. };
  1194. } // end anonymous namespace
  1195. tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
  1196. SourceManager &SourceMgr,
  1197. std::vector<CharSourceRange> Ranges) {
  1198. Formatter formatter(Style, Lex, SourceMgr, Ranges);
  1199. return formatter.format();
  1200. }
  1201. tooling::Replacements reformat(const FormatStyle &Style, StringRef Code,
  1202. std::vector<tooling::Range> Ranges,
  1203. StringRef FileName) {
  1204. FileManager Files((FileSystemOptions()));
  1205. DiagnosticsEngine Diagnostics(
  1206. IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
  1207. new DiagnosticOptions);
  1208. SourceManager SourceMgr(Diagnostics, Files);
  1209. llvm::MemoryBuffer *Buf = llvm::MemoryBuffer::getMemBuffer(Code, FileName);
  1210. const clang::FileEntry *Entry =
  1211. Files.getVirtualFile(FileName, Buf->getBufferSize(), 0);
  1212. SourceMgr.overrideFileContents(Entry, Buf);
  1213. FileID ID =
  1214. SourceMgr.createFileID(Entry, SourceLocation(), clang::SrcMgr::C_User);
  1215. Lexer Lex(ID, SourceMgr.getBuffer(ID), SourceMgr,
  1216. getFormattingLangOpts(Style.Standard));
  1217. SourceLocation StartOfFile = SourceMgr.getLocForStartOfFile(ID);
  1218. std::vector<CharSourceRange> CharRanges;
  1219. for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
  1220. SourceLocation Start = StartOfFile.getLocWithOffset(Ranges[i].getOffset());
  1221. SourceLocation End = Start.getLocWithOffset(Ranges[i].getLength());
  1222. CharRanges.push_back(CharSourceRange::getCharRange(Start, End));
  1223. }
  1224. return reformat(Style, Lex, SourceMgr, CharRanges);
  1225. }
  1226. LangOptions getFormattingLangOpts(FormatStyle::LanguageStandard Standard) {
  1227. LangOptions LangOpts;
  1228. LangOpts.CPlusPlus = 1;
  1229. LangOpts.CPlusPlus11 = Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
  1230. LangOpts.LineComment = 1;
  1231. LangOpts.Bool = 1;
  1232. LangOpts.ObjC1 = 1;
  1233. LangOpts.ObjC2 = 1;
  1234. return LangOpts;
  1235. }
  1236. const char *StyleOptionHelpDescription =
  1237. "Coding style, currently supports:\n"
  1238. " LLVM, Google, Chromium, Mozilla, WebKit.\n"
  1239. "Use -style=file to load style configuration from\n"
  1240. ".clang-format file located in one of the parent\n"
  1241. "directories of the source file (or current\n"
  1242. "directory for stdin).\n"
  1243. "Use -style=\"{key: value, ...}\" to set specific\n"
  1244. "parameters, e.g.:\n"
  1245. " -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\"";
  1246. FormatStyle getStyle(StringRef StyleName, StringRef FileName) {
  1247. // Fallback style in case the rest of this function can't determine a style.
  1248. StringRef FallbackStyle = "LLVM";
  1249. FormatStyle Style;
  1250. getPredefinedStyle(FallbackStyle, &Style);
  1251. if (StyleName.startswith("{")) {
  1252. // Parse YAML/JSON style from the command line.
  1253. if (llvm::error_code ec = parseConfiguration(StyleName, &Style)) {
  1254. llvm::errs() << "Error parsing -style: " << ec.message() << ", using "
  1255. << FallbackStyle << " style\n";
  1256. }
  1257. return Style;
  1258. }
  1259. if (!StyleName.equals_lower("file")) {
  1260. if (!getPredefinedStyle(StyleName, &Style))
  1261. llvm::errs() << "Invalid value for -style, using " << FallbackStyle
  1262. << " style\n";
  1263. return Style;
  1264. }
  1265. SmallString<128> Path(FileName);
  1266. llvm::sys::fs::make_absolute(Path);
  1267. for (StringRef Directory = Path; !Directory.empty();
  1268. Directory = llvm::sys::path::parent_path(Directory)) {
  1269. if (!llvm::sys::fs::is_directory(Directory))
  1270. continue;
  1271. SmallString<128> ConfigFile(Directory);
  1272. llvm::sys::path::append(ConfigFile, ".clang-format");
  1273. DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
  1274. bool IsFile = false;
  1275. // Ignore errors from is_regular_file: we only need to know if we can read
  1276. // the file or not.
  1277. llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile);
  1278. if (!IsFile) {
  1279. // Try _clang-format too, since dotfiles are not commonly used on Windows.
  1280. ConfigFile = Directory;
  1281. llvm::sys::path::append(ConfigFile, "_clang-format");
  1282. DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
  1283. llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile);
  1284. }
  1285. if (IsFile) {
  1286. OwningPtr<llvm::MemoryBuffer> Text;
  1287. if (llvm::error_code ec =
  1288. llvm::MemoryBuffer::getFile(ConfigFile.c_str(), Text)) {
  1289. llvm::errs() << ec.message() << "\n";
  1290. continue;
  1291. }
  1292. if (llvm::error_code ec = parseConfiguration(Text->getBuffer(), &Style)) {
  1293. llvm::errs() << "Error reading " << ConfigFile << ": " << ec.message()
  1294. << "\n";
  1295. continue;
  1296. }
  1297. DEBUG(llvm::dbgs() << "Using configuration file " << ConfigFile << "\n");
  1298. return Style;
  1299. }
  1300. }
  1301. llvm::errs() << "Can't find usable .clang-format, using " << FallbackStyle
  1302. << " style\n";
  1303. return Style;
  1304. }
  1305. } // namespace format
  1306. } // namespace clang