Format.cpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  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 <queue>
  29. #include <string>
  30. namespace llvm {
  31. namespace yaml {
  32. template <>
  33. struct ScalarEnumerationTraits<clang::format::FormatStyle::LanguageStandard> {
  34. static void enumeration(IO &IO,
  35. clang::format::FormatStyle::LanguageStandard &Value) {
  36. IO.enumCase(Value, "C++03", clang::format::FormatStyle::LS_Cpp03);
  37. IO.enumCase(Value, "C++11", clang::format::FormatStyle::LS_Cpp11);
  38. IO.enumCase(Value, "Auto", clang::format::FormatStyle::LS_Auto);
  39. }
  40. };
  41. template <>
  42. struct ScalarEnumerationTraits<clang::format::FormatStyle::BraceBreakingStyle> {
  43. static void
  44. enumeration(IO &IO, clang::format::FormatStyle::BraceBreakingStyle &Value) {
  45. IO.enumCase(Value, "Attach", clang::format::FormatStyle::BS_Attach);
  46. IO.enumCase(Value, "Linux", clang::format::FormatStyle::BS_Linux);
  47. IO.enumCase(Value, "Stroustrup", clang::format::FormatStyle::BS_Stroustrup);
  48. IO.enumCase(Value, "Allman", clang::format::FormatStyle::BS_Allman);
  49. }
  50. };
  51. template <>
  52. struct ScalarEnumerationTraits<
  53. clang::format::FormatStyle::NamespaceIndentationKind> {
  54. static void
  55. enumeration(IO &IO,
  56. clang::format::FormatStyle::NamespaceIndentationKind &Value) {
  57. IO.enumCase(Value, "None", clang::format::FormatStyle::NI_None);
  58. IO.enumCase(Value, "Inner", clang::format::FormatStyle::NI_Inner);
  59. IO.enumCase(Value, "All", clang::format::FormatStyle::NI_All);
  60. }
  61. };
  62. template <> struct MappingTraits<clang::format::FormatStyle> {
  63. static void mapping(llvm::yaml::IO &IO, clang::format::FormatStyle &Style) {
  64. if (IO.outputting()) {
  65. StringRef StylesArray[] = { "LLVM", "Google", "Chromium",
  66. "Mozilla", "WebKit" };
  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("ConstructorInitializerIndentWidth",
  88. Style.ConstructorInitializerIndentWidth);
  89. IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft);
  90. IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments);
  91. IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine",
  92. Style.AllowAllParametersOfDeclarationOnNextLine);
  93. IO.mapOptional("AllowShortIfStatementsOnASingleLine",
  94. Style.AllowShortIfStatementsOnASingleLine);
  95. IO.mapOptional("AllowShortLoopsOnASingleLine",
  96. Style.AllowShortLoopsOnASingleLine);
  97. IO.mapOptional("AlwaysBreakTemplateDeclarations",
  98. Style.AlwaysBreakTemplateDeclarations);
  99. IO.mapOptional("AlwaysBreakBeforeMultilineStrings",
  100. Style.AlwaysBreakBeforeMultilineStrings);
  101. IO.mapOptional("BreakBeforeBinaryOperators",
  102. Style.BreakBeforeBinaryOperators);
  103. IO.mapOptional("BreakConstructorInitializersBeforeComma",
  104. Style.BreakConstructorInitializersBeforeComma);
  105. IO.mapOptional("BinPackParameters", Style.BinPackParameters);
  106. IO.mapOptional("ColumnLimit", Style.ColumnLimit);
  107. IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine",
  108. Style.ConstructorInitializerAllOnOneLineOrOnePerLine);
  109. IO.mapOptional("DerivePointerBinding", Style.DerivePointerBinding);
  110. IO.mapOptional("ExperimentalAutoDetectBinPacking",
  111. Style.ExperimentalAutoDetectBinPacking);
  112. IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels);
  113. IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep);
  114. IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation);
  115. IO.mapOptional("ObjCSpaceBeforeProtocolList",
  116. Style.ObjCSpaceBeforeProtocolList);
  117. IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment);
  118. IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString);
  119. IO.mapOptional("PenaltyBreakFirstLessLess",
  120. Style.PenaltyBreakFirstLessLess);
  121. IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter);
  122. IO.mapOptional("PenaltyReturnTypeOnItsOwnLine",
  123. Style.PenaltyReturnTypeOnItsOwnLine);
  124. IO.mapOptional("PointerBindsToType", Style.PointerBindsToType);
  125. IO.mapOptional("SpacesBeforeTrailingComments",
  126. Style.SpacesBeforeTrailingComments);
  127. IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle);
  128. IO.mapOptional("Standard", Style.Standard);
  129. IO.mapOptional("IndentWidth", Style.IndentWidth);
  130. IO.mapOptional("UseTab", Style.UseTab);
  131. IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces);
  132. IO.mapOptional("IndentFunctionDeclarationAfterType",
  133. Style.IndentFunctionDeclarationAfterType);
  134. IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses);
  135. IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses);
  136. IO.mapOptional("SpacesInCStyleCastParentheses",
  137. Style.SpacesInCStyleCastParentheses);
  138. IO.mapOptional("SpaceAfterControlStatementKeyword",
  139. Style.SpaceAfterControlStatementKeyword);
  140. }
  141. };
  142. }
  143. }
  144. namespace clang {
  145. namespace format {
  146. void setDefaultPenalties(FormatStyle &Style) {
  147. Style.PenaltyBreakComment = 60;
  148. Style.PenaltyBreakFirstLessLess = 120;
  149. Style.PenaltyBreakString = 1000;
  150. Style.PenaltyExcessCharacter = 1000000;
  151. }
  152. FormatStyle getLLVMStyle() {
  153. FormatStyle LLVMStyle;
  154. LLVMStyle.AccessModifierOffset = -2;
  155. LLVMStyle.AlignEscapedNewlinesLeft = false;
  156. LLVMStyle.AlignTrailingComments = true;
  157. LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
  158. LLVMStyle.AllowShortIfStatementsOnASingleLine = false;
  159. LLVMStyle.AllowShortLoopsOnASingleLine = false;
  160. LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
  161. LLVMStyle.AlwaysBreakTemplateDeclarations = false;
  162. LLVMStyle.BinPackParameters = true;
  163. LLVMStyle.BreakBeforeBinaryOperators = false;
  164. LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
  165. LLVMStyle.BreakConstructorInitializersBeforeComma = false;
  166. LLVMStyle.ColumnLimit = 80;
  167. LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
  168. LLVMStyle.ConstructorInitializerIndentWidth = 4;
  169. LLVMStyle.Cpp11BracedListStyle = false;
  170. LLVMStyle.DerivePointerBinding = false;
  171. LLVMStyle.ExperimentalAutoDetectBinPacking = false;
  172. LLVMStyle.IndentCaseLabels = false;
  173. LLVMStyle.IndentFunctionDeclarationAfterType = false;
  174. LLVMStyle.IndentWidth = 2;
  175. LLVMStyle.MaxEmptyLinesToKeep = 1;
  176. LLVMStyle.NamespaceIndentation = FormatStyle::NI_None;
  177. LLVMStyle.ObjCSpaceBeforeProtocolList = true;
  178. LLVMStyle.PointerBindsToType = false;
  179. LLVMStyle.SpacesBeforeTrailingComments = 1;
  180. LLVMStyle.Standard = FormatStyle::LS_Cpp03;
  181. LLVMStyle.UseTab = false;
  182. LLVMStyle.SpacesInParentheses = false;
  183. LLVMStyle.SpaceInEmptyParentheses = false;
  184. LLVMStyle.SpacesInCStyleCastParentheses = false;
  185. LLVMStyle.SpaceAfterControlStatementKeyword = true;
  186. setDefaultPenalties(LLVMStyle);
  187. LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
  188. return LLVMStyle;
  189. }
  190. FormatStyle getGoogleStyle() {
  191. FormatStyle GoogleStyle;
  192. GoogleStyle.AccessModifierOffset = -1;
  193. GoogleStyle.AlignEscapedNewlinesLeft = true;
  194. GoogleStyle.AlignTrailingComments = true;
  195. GoogleStyle.AllowAllParametersOfDeclarationOnNextLine = true;
  196. GoogleStyle.AllowShortIfStatementsOnASingleLine = true;
  197. GoogleStyle.AllowShortLoopsOnASingleLine = true;
  198. GoogleStyle.AlwaysBreakBeforeMultilineStrings = true;
  199. GoogleStyle.AlwaysBreakTemplateDeclarations = true;
  200. GoogleStyle.BinPackParameters = true;
  201. GoogleStyle.BreakBeforeBinaryOperators = false;
  202. GoogleStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
  203. GoogleStyle.BreakConstructorInitializersBeforeComma = false;
  204. GoogleStyle.ColumnLimit = 80;
  205. GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  206. GoogleStyle.ConstructorInitializerIndentWidth = 4;
  207. GoogleStyle.Cpp11BracedListStyle = true;
  208. GoogleStyle.DerivePointerBinding = true;
  209. GoogleStyle.ExperimentalAutoDetectBinPacking = false;
  210. GoogleStyle.IndentCaseLabels = true;
  211. GoogleStyle.IndentFunctionDeclarationAfterType = true;
  212. GoogleStyle.IndentWidth = 2;
  213. GoogleStyle.MaxEmptyLinesToKeep = 1;
  214. GoogleStyle.NamespaceIndentation = FormatStyle::NI_None;
  215. GoogleStyle.ObjCSpaceBeforeProtocolList = false;
  216. GoogleStyle.PointerBindsToType = true;
  217. GoogleStyle.SpacesBeforeTrailingComments = 2;
  218. GoogleStyle.Standard = FormatStyle::LS_Auto;
  219. GoogleStyle.UseTab = false;
  220. GoogleStyle.SpacesInParentheses = false;
  221. GoogleStyle.SpaceInEmptyParentheses = false;
  222. GoogleStyle.SpacesInCStyleCastParentheses = false;
  223. GoogleStyle.SpaceAfterControlStatementKeyword = true;
  224. setDefaultPenalties(GoogleStyle);
  225. GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
  226. return GoogleStyle;
  227. }
  228. FormatStyle getChromiumStyle() {
  229. FormatStyle ChromiumStyle = getGoogleStyle();
  230. ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false;
  231. ChromiumStyle.AllowShortIfStatementsOnASingleLine = false;
  232. ChromiumStyle.AllowShortLoopsOnASingleLine = false;
  233. ChromiumStyle.BinPackParameters = false;
  234. ChromiumStyle.DerivePointerBinding = false;
  235. ChromiumStyle.Standard = FormatStyle::LS_Cpp03;
  236. return ChromiumStyle;
  237. }
  238. FormatStyle getMozillaStyle() {
  239. FormatStyle MozillaStyle = getLLVMStyle();
  240. MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false;
  241. MozillaStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  242. MozillaStyle.DerivePointerBinding = true;
  243. MozillaStyle.IndentCaseLabels = true;
  244. MozillaStyle.ObjCSpaceBeforeProtocolList = false;
  245. MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200;
  246. MozillaStyle.PointerBindsToType = true;
  247. return MozillaStyle;
  248. }
  249. FormatStyle getWebKitStyle() {
  250. FormatStyle Style = getLLVMStyle();
  251. Style.AccessModifierOffset = -4;
  252. Style.AlignTrailingComments = false;
  253. Style.BreakBeforeBinaryOperators = true;
  254. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  255. Style.BreakConstructorInitializersBeforeComma = true;
  256. Style.ColumnLimit = 0;
  257. Style.IndentWidth = 4;
  258. Style.NamespaceIndentation = FormatStyle::NI_Inner;
  259. Style.PointerBindsToType = true;
  260. return Style;
  261. }
  262. bool getPredefinedStyle(StringRef Name, FormatStyle *Style) {
  263. if (Name.equals_lower("llvm"))
  264. *Style = getLLVMStyle();
  265. else if (Name.equals_lower("chromium"))
  266. *Style = getChromiumStyle();
  267. else if (Name.equals_lower("mozilla"))
  268. *Style = getMozillaStyle();
  269. else if (Name.equals_lower("google"))
  270. *Style = getGoogleStyle();
  271. else if (Name.equals_lower("webkit"))
  272. *Style = getWebKitStyle();
  273. else
  274. return false;
  275. return true;
  276. }
  277. llvm::error_code parseConfiguration(StringRef Text, FormatStyle *Style) {
  278. if (Text.trim().empty())
  279. return llvm::make_error_code(llvm::errc::invalid_argument);
  280. llvm::yaml::Input Input(Text);
  281. Input >> *Style;
  282. return Input.error();
  283. }
  284. std::string configurationAsText(const FormatStyle &Style) {
  285. std::string Text;
  286. llvm::raw_string_ostream Stream(Text);
  287. llvm::yaml::Output Output(Stream);
  288. // We use the same mapping method for input and output, so we need a non-const
  289. // reference here.
  290. FormatStyle NonConstStyle = Style;
  291. Output << NonConstStyle;
  292. return Stream.str();
  293. }
  294. namespace {
  295. class NoColumnLimitFormatter {
  296. public:
  297. NoColumnLimitFormatter(ContinuationIndenter *Indenter) : Indenter(Indenter) {}
  298. /// \brief Formats the line starting at \p State, simply keeping all of the
  299. /// input's line breaking decisions.
  300. void format() {
  301. LineState State = Indenter->getInitialState();
  302. while (State.NextToken != NULL) {
  303. bool Newline =
  304. Indenter->mustBreak(State) ||
  305. (Indenter->canBreak(State) && State.NextToken->NewlinesBefore > 0);
  306. Indenter->addTokenToState(State, Newline, /*DryRun=*/false);
  307. }
  308. }
  309. private:
  310. ContinuationIndenter *Indenter;
  311. };
  312. class UnwrappedLineFormatter {
  313. public:
  314. UnwrappedLineFormatter(ContinuationIndenter *Indenter,
  315. const FormatStyle &Style, const AnnotatedLine &Line)
  316. : Indenter(Indenter), Style(Style), Line(Line), Count(0) {}
  317. /// \brief Formats an \c UnwrappedLine.
  318. void format() {
  319. LineState State = Indenter->getInitialState();
  320. // If the ObjC method declaration does not fit on a line, we should format
  321. // it with one arg per line.
  322. if (Line.Type == LT_ObjCMethodDecl)
  323. State.Stack.back().BreakBeforeParameter = true;
  324. // Find best solution in solution space.
  325. analyzeSolutionSpace(State);
  326. }
  327. private:
  328. /// \brief An edge in the solution space from \c Previous->State to \c State,
  329. /// inserting a newline dependent on the \c NewLine.
  330. struct StateNode {
  331. StateNode(const LineState &State, bool NewLine, StateNode *Previous)
  332. : State(State), NewLine(NewLine), Previous(Previous) {}
  333. LineState State;
  334. bool NewLine;
  335. StateNode *Previous;
  336. };
  337. /// \brief A pair of <penalty, count> that is used to prioritize the BFS on.
  338. ///
  339. /// In case of equal penalties, we want to prefer states that were inserted
  340. /// first. During state generation we make sure that we insert states first
  341. /// that break the line as late as possible.
  342. typedef std::pair<unsigned, unsigned> OrderedPenalty;
  343. /// \brief An item in the prioritized BFS search queue. The \c StateNode's
  344. /// \c State has the given \c OrderedPenalty.
  345. typedef std::pair<OrderedPenalty, StateNode *> QueueItem;
  346. /// \brief The BFS queue type.
  347. typedef std::priority_queue<QueueItem, std::vector<QueueItem>,
  348. std::greater<QueueItem> > QueueType;
  349. /// \brief Analyze the entire solution space starting from \p InitialState.
  350. ///
  351. /// This implements a variant of Dijkstra's algorithm on the graph that spans
  352. /// the solution space (\c LineStates are the nodes). The algorithm tries to
  353. /// find the shortest path (the one with lowest penalty) from \p InitialState
  354. /// to a state where all tokens are placed.
  355. void analyzeSolutionSpace(LineState &InitialState) {
  356. std::set<LineState> Seen;
  357. // Insert start element into queue.
  358. StateNode *Node =
  359. new (Allocator.Allocate()) StateNode(InitialState, false, NULL);
  360. Queue.push(QueueItem(OrderedPenalty(0, Count), Node));
  361. ++Count;
  362. // While not empty, take first element and follow edges.
  363. while (!Queue.empty()) {
  364. unsigned Penalty = Queue.top().first.first;
  365. StateNode *Node = Queue.top().second;
  366. if (Node->State.NextToken == NULL) {
  367. DEBUG(llvm::dbgs() << "\n---\nPenalty for line: " << Penalty << "\n");
  368. break;
  369. }
  370. Queue.pop();
  371. // Cut off the analysis of certain solutions if the analysis gets too
  372. // complex. See description of IgnoreStackForComparison.
  373. if (Count > 10000)
  374. Node->State.IgnoreStackForComparison = true;
  375. if (!Seen.insert(Node->State).second)
  376. // State already examined with lower penalty.
  377. continue;
  378. addNextStateToQueue(Penalty, Node, /*NewLine=*/false);
  379. addNextStateToQueue(Penalty, Node, /*NewLine=*/true);
  380. }
  381. if (Queue.empty())
  382. // We were unable to find a solution, do nothing.
  383. // FIXME: Add diagnostic?
  384. return;
  385. // Reconstruct the solution.
  386. reconstructPath(InitialState, Queue.top().second);
  387. DEBUG(llvm::dbgs() << "Total number of analyzed states: " << Count << "\n");
  388. DEBUG(llvm::dbgs() << "---\n");
  389. }
  390. void reconstructPath(LineState &State, StateNode *Current) {
  391. std::deque<StateNode *> Path;
  392. // We do not need a break before the initial token.
  393. while (Current->Previous) {
  394. Path.push_front(Current);
  395. Current = Current->Previous;
  396. }
  397. for (std::deque<StateNode *>::iterator I = Path.begin(), E = Path.end();
  398. I != E; ++I) {
  399. unsigned Penalty = Indenter->addTokenToState(State, (*I)->NewLine, false);
  400. (void)Penalty;
  401. DEBUG({
  402. if ((*I)->NewLine) {
  403. llvm::dbgs() << "Penalty for placing "
  404. << (*I)->Previous->State.NextToken->Tok.getName() << ": "
  405. << Penalty << "\n";
  406. }
  407. });
  408. }
  409. }
  410. /// \brief Add the following state to the analysis queue \c Queue.
  411. ///
  412. /// Assume the current state is \p PreviousNode and has been reached with a
  413. /// penalty of \p Penalty. Insert a line break if \p NewLine is \c true.
  414. void addNextStateToQueue(unsigned Penalty, StateNode *PreviousNode,
  415. bool NewLine) {
  416. if (NewLine && !Indenter->canBreak(PreviousNode->State))
  417. return;
  418. if (!NewLine && Indenter->mustBreak(PreviousNode->State))
  419. return;
  420. StateNode *Node = new (Allocator.Allocate())
  421. StateNode(PreviousNode->State, NewLine, PreviousNode);
  422. Penalty += Indenter->addTokenToState(Node->State, NewLine, true);
  423. if (Node->State.Column > Indenter->getColumnLimit()) {
  424. unsigned ExcessCharacters =
  425. Node->State.Column - Indenter->getColumnLimit();
  426. Penalty += Style.PenaltyExcessCharacter * ExcessCharacters;
  427. }
  428. Queue.push(QueueItem(OrderedPenalty(Penalty, Count), Node));
  429. ++Count;
  430. }
  431. ContinuationIndenter *Indenter;
  432. FormatStyle Style;
  433. const AnnotatedLine &Line;
  434. llvm::SpecificBumpPtrAllocator<StateNode> Allocator;
  435. QueueType Queue;
  436. // Increasing count of \c StateNode items we have created. This is used
  437. // to create a deterministic order independent of the container.
  438. unsigned Count;
  439. };
  440. class FormatTokenLexer {
  441. public:
  442. FormatTokenLexer(Lexer &Lex, SourceManager &SourceMgr, FormatStyle &Style,
  443. encoding::Encoding Encoding)
  444. : FormatTok(NULL), GreaterStashed(false), Column(0),
  445. TrailingWhitespace(0), Lex(Lex), SourceMgr(SourceMgr), Style(Style),
  446. IdentTable(getFormattingLangOpts()), Encoding(Encoding) {
  447. Lex.SetKeepWhitespaceMode(true);
  448. }
  449. ArrayRef<FormatToken *> lex() {
  450. assert(Tokens.empty());
  451. do {
  452. Tokens.push_back(getNextToken());
  453. } while (Tokens.back()->Tok.isNot(tok::eof));
  454. return Tokens;
  455. }
  456. IdentifierTable &getIdentTable() { return IdentTable; }
  457. private:
  458. FormatToken *getNextToken() {
  459. if (GreaterStashed) {
  460. // Create a synthesized second '>' token.
  461. // FIXME: Increment Column and set OriginalColumn.
  462. Token Greater = FormatTok->Tok;
  463. FormatTok = new (Allocator.Allocate()) FormatToken;
  464. FormatTok->Tok = Greater;
  465. SourceLocation GreaterLocation =
  466. FormatTok->Tok.getLocation().getLocWithOffset(1);
  467. FormatTok->WhitespaceRange =
  468. SourceRange(GreaterLocation, GreaterLocation);
  469. FormatTok->TokenText = ">";
  470. FormatTok->CodePointCount = 1;
  471. GreaterStashed = false;
  472. return FormatTok;
  473. }
  474. FormatTok = new (Allocator.Allocate()) FormatToken;
  475. readRawToken(*FormatTok);
  476. SourceLocation WhitespaceStart =
  477. FormatTok->Tok.getLocation().getLocWithOffset(-TrailingWhitespace);
  478. if (SourceMgr.getFileOffset(WhitespaceStart) == 0)
  479. FormatTok->IsFirst = true;
  480. // Consume and record whitespace until we find a significant token.
  481. unsigned WhitespaceLength = TrailingWhitespace;
  482. while (FormatTok->Tok.is(tok::unknown)) {
  483. for (int i = 0, e = FormatTok->TokenText.size(); i != e; ++i) {
  484. switch (FormatTok->TokenText[i]) {
  485. case '\n':
  486. ++FormatTok->NewlinesBefore;
  487. // FIXME: This is technically incorrect, as it could also
  488. // be a literal backslash at the end of the line.
  489. if (i == 0 || FormatTok->TokenText[i-1] != '\\')
  490. FormatTok->HasUnescapedNewline = true;
  491. FormatTok->LastNewlineOffset = WhitespaceLength + i + 1;
  492. Column = 0;
  493. break;
  494. case ' ':
  495. ++Column;
  496. break;
  497. case '\t':
  498. Column += Style.IndentWidth - Column % Style.IndentWidth;
  499. break;
  500. default:
  501. ++Column;
  502. break;
  503. }
  504. }
  505. WhitespaceLength += FormatTok->Tok.getLength();
  506. readRawToken(*FormatTok);
  507. }
  508. // In case the token starts with escaped newlines, we want to
  509. // take them into account as whitespace - this pattern is quite frequent
  510. // in macro definitions.
  511. // FIXME: What do we want to do with other escaped spaces, and escaped
  512. // spaces or newlines in the middle of tokens?
  513. // FIXME: Add a more explicit test.
  514. while (FormatTok->TokenText.size() > 1 && FormatTok->TokenText[0] == '\\' &&
  515. FormatTok->TokenText[1] == '\n') {
  516. // FIXME: ++FormatTok->NewlinesBefore is missing...
  517. WhitespaceLength += 2;
  518. Column = 0;
  519. FormatTok->TokenText = FormatTok->TokenText.substr(2);
  520. }
  521. FormatTok->OriginalColumn = Column;
  522. TrailingWhitespace = 0;
  523. if (FormatTok->Tok.is(tok::comment)) {
  524. // FIXME: Add the trimmed whitespace to Column.
  525. StringRef UntrimmedText = FormatTok->TokenText;
  526. FormatTok->TokenText = FormatTok->TokenText.rtrim();
  527. TrailingWhitespace = UntrimmedText.size() - FormatTok->TokenText.size();
  528. } else if (FormatTok->Tok.is(tok::raw_identifier)) {
  529. IdentifierInfo &Info = IdentTable.get(FormatTok->TokenText);
  530. FormatTok->Tok.setIdentifierInfo(&Info);
  531. FormatTok->Tok.setKind(Info.getTokenID());
  532. } else if (FormatTok->Tok.is(tok::greatergreater)) {
  533. FormatTok->Tok.setKind(tok::greater);
  534. FormatTok->TokenText = FormatTok->TokenText.substr(0, 1);
  535. GreaterStashed = true;
  536. }
  537. // Now FormatTok is the next non-whitespace token.
  538. FormatTok->CodePointCount =
  539. encoding::getCodePointCount(FormatTok->TokenText, Encoding);
  540. if (FormatTok->isOneOf(tok::string_literal, tok::comment)) {
  541. StringRef Text = FormatTok->TokenText;
  542. size_t FirstNewlinePos = Text.find('\n');
  543. if (FirstNewlinePos != StringRef::npos) {
  544. FormatTok->CodePointsInFirstLine = encoding::getCodePointCount(
  545. Text.substr(0, FirstNewlinePos), Encoding);
  546. FormatTok->CodePointsInLastLine = encoding::getCodePointCount(
  547. Text.substr(Text.find_last_of('\n') + 1), Encoding);
  548. }
  549. }
  550. // FIXME: Add the CodePointCount to Column.
  551. FormatTok->WhitespaceRange = SourceRange(
  552. WhitespaceStart, WhitespaceStart.getLocWithOffset(WhitespaceLength));
  553. return FormatTok;
  554. }
  555. FormatToken *FormatTok;
  556. bool GreaterStashed;
  557. unsigned Column;
  558. unsigned TrailingWhitespace;
  559. Lexer &Lex;
  560. SourceManager &SourceMgr;
  561. FormatStyle &Style;
  562. IdentifierTable IdentTable;
  563. encoding::Encoding Encoding;
  564. llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
  565. SmallVector<FormatToken *, 16> Tokens;
  566. void readRawToken(FormatToken &Tok) {
  567. Lex.LexFromRawLexer(Tok.Tok);
  568. Tok.TokenText = StringRef(SourceMgr.getCharacterData(Tok.Tok.getLocation()),
  569. Tok.Tok.getLength());
  570. // For formatting, treat unterminated string literals like normal string
  571. // literals.
  572. if (Tok.is(tok::unknown) && !Tok.TokenText.empty() &&
  573. Tok.TokenText[0] == '"') {
  574. Tok.Tok.setKind(tok::string_literal);
  575. Tok.IsUnterminatedLiteral = true;
  576. }
  577. }
  578. };
  579. class Formatter : public UnwrappedLineConsumer {
  580. public:
  581. Formatter(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr,
  582. const std::vector<CharSourceRange> &Ranges)
  583. : Style(Style), Lex(Lex), SourceMgr(SourceMgr),
  584. Whitespaces(SourceMgr, Style), Ranges(Ranges),
  585. Encoding(encoding::detectEncoding(Lex.getBuffer())) {
  586. DEBUG(llvm::dbgs() << "File encoding: "
  587. << (Encoding == encoding::Encoding_UTF8 ? "UTF8"
  588. : "unknown")
  589. << "\n");
  590. }
  591. virtual ~Formatter() {}
  592. tooling::Replacements format() {
  593. FormatTokenLexer Tokens(Lex, SourceMgr, Style, Encoding);
  594. UnwrappedLineParser Parser(Style, Tokens.lex(), *this);
  595. bool StructuralError = Parser.parse();
  596. TokenAnnotator Annotator(Style, Tokens.getIdentTable().get("in"));
  597. for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
  598. Annotator.annotate(AnnotatedLines[i]);
  599. }
  600. deriveLocalStyle();
  601. for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
  602. Annotator.calculateFormattingInformation(AnnotatedLines[i]);
  603. }
  604. // Adapt level to the next line if this is a comment.
  605. // FIXME: Can/should this be done in the UnwrappedLineParser?
  606. const AnnotatedLine *NextNonCommentLine = NULL;
  607. for (unsigned i = AnnotatedLines.size() - 1; i > 0; --i) {
  608. if (NextNonCommentLine && AnnotatedLines[i].First->is(tok::comment) &&
  609. !AnnotatedLines[i].First->Next)
  610. AnnotatedLines[i].Level = NextNonCommentLine->Level;
  611. else
  612. NextNonCommentLine = AnnotatedLines[i].First->isNot(tok::r_brace)
  613. ? &AnnotatedLines[i]
  614. : NULL;
  615. }
  616. std::vector<int> IndentForLevel;
  617. bool PreviousLineWasTouched = false;
  618. const FormatToken *PreviousLineLastToken = 0;
  619. bool FormatPPDirective = false;
  620. for (std::vector<AnnotatedLine>::iterator I = AnnotatedLines.begin(),
  621. E = AnnotatedLines.end();
  622. I != E; ++I) {
  623. const AnnotatedLine &TheLine = *I;
  624. const FormatToken *FirstTok = TheLine.First;
  625. int Offset = getIndentOffset(*TheLine.First);
  626. // Check whether this line is part of a formatted preprocessor directive.
  627. if (FirstTok->HasUnescapedNewline)
  628. FormatPPDirective = false;
  629. if (!FormatPPDirective && TheLine.InPPDirective &&
  630. (touchesLine(TheLine) || touchesPPDirective(I + 1, E)))
  631. FormatPPDirective = true;
  632. // Determine indent and try to merge multiple unwrapped lines.
  633. while (IndentForLevel.size() <= TheLine.Level)
  634. IndentForLevel.push_back(-1);
  635. IndentForLevel.resize(TheLine.Level + 1);
  636. unsigned Indent = getIndent(IndentForLevel, TheLine.Level);
  637. if (static_cast<int>(Indent) + Offset >= 0)
  638. Indent += Offset;
  639. tryFitMultipleLinesInOne(Indent, I, E);
  640. bool WasMoved = PreviousLineWasTouched && FirstTok->NewlinesBefore == 0;
  641. if (TheLine.First->is(tok::eof)) {
  642. if (PreviousLineWasTouched) {
  643. unsigned NewLines = std::min(FirstTok->NewlinesBefore, 1u);
  644. Whitespaces.replaceWhitespace(*TheLine.First, NewLines, /*Indent*/ 0,
  645. /*TargetColumn*/ 0);
  646. }
  647. } else if (TheLine.Type != LT_Invalid &&
  648. (WasMoved || FormatPPDirective || touchesLine(TheLine))) {
  649. unsigned LevelIndent = getIndent(IndentForLevel, TheLine.Level);
  650. if (FirstTok->WhitespaceRange.isValid() &&
  651. // Insert a break even if there is a structural error in case where
  652. // we break apart a line consisting of multiple unwrapped lines.
  653. (FirstTok->NewlinesBefore == 0 || !StructuralError)) {
  654. formatFirstToken(*TheLine.First, PreviousLineLastToken, Indent,
  655. TheLine.InPPDirective);
  656. } else {
  657. Indent = LevelIndent = FirstTok->OriginalColumn;
  658. }
  659. ContinuationIndenter Indenter(Style, SourceMgr, TheLine, Indent,
  660. Whitespaces, Encoding,
  661. BinPackInconclusiveFunctions);
  662. // If everything fits on a single line, just put it there.
  663. unsigned ColumnLimit = Style.ColumnLimit;
  664. if ((I + 1) != E && (I + 1)->InPPDirective &&
  665. !(I + 1)->First->HasUnescapedNewline)
  666. ColumnLimit = Indenter.getColumnLimit();
  667. if (I->Last->TotalLength + Indent <= ColumnLimit) {
  668. LineState State = Indenter.getInitialState();
  669. while (State.NextToken != NULL)
  670. Indenter.addTokenToState(State, false, false);
  671. } else if (Style.ColumnLimit == 0) {
  672. NoColumnLimitFormatter Formatter(&Indenter);
  673. Formatter.format();
  674. } else {
  675. UnwrappedLineFormatter Formatter(&Indenter, Style, TheLine);
  676. Formatter.format();
  677. }
  678. IndentForLevel[TheLine.Level] = LevelIndent;
  679. PreviousLineWasTouched = true;
  680. } else {
  681. // Format the first token if necessary, and notify the WhitespaceManager
  682. // about the unchanged whitespace.
  683. for (const FormatToken *Tok = TheLine.First; Tok != NULL;
  684. Tok = Tok->Next) {
  685. if (Tok == TheLine.First &&
  686. (Tok->NewlinesBefore > 0 || Tok->IsFirst)) {
  687. unsigned LevelIndent = Tok->OriginalColumn;
  688. // Remove trailing whitespace of the previous line if it was
  689. // touched.
  690. if (PreviousLineWasTouched || touchesEmptyLineBefore(TheLine)) {
  691. formatFirstToken(*Tok, PreviousLineLastToken, LevelIndent,
  692. TheLine.InPPDirective);
  693. } else {
  694. Whitespaces.addUntouchableToken(*Tok, TheLine.InPPDirective);
  695. }
  696. if (static_cast<int>(LevelIndent) - Offset >= 0)
  697. LevelIndent -= Offset;
  698. if (Tok->isNot(tok::comment))
  699. IndentForLevel[TheLine.Level] = LevelIndent;
  700. } else {
  701. Whitespaces.addUntouchableToken(*Tok, TheLine.InPPDirective);
  702. }
  703. }
  704. // If we did not reformat this unwrapped line, the column at the end of
  705. // the last token is unchanged - thus, we can calculate the end of the
  706. // last token.
  707. PreviousLineWasTouched = false;
  708. }
  709. PreviousLineLastToken = I->Last;
  710. }
  711. return Whitespaces.generateReplacements();
  712. }
  713. private:
  714. void deriveLocalStyle() {
  715. unsigned CountBoundToVariable = 0;
  716. unsigned CountBoundToType = 0;
  717. bool HasCpp03IncompatibleFormat = false;
  718. bool HasBinPackedFunction = false;
  719. bool HasOnePerLineFunction = false;
  720. for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
  721. if (!AnnotatedLines[i].First->Next)
  722. continue;
  723. FormatToken *Tok = AnnotatedLines[i].First->Next;
  724. while (Tok->Next) {
  725. if (Tok->Type == TT_PointerOrReference) {
  726. bool SpacesBefore =
  727. Tok->WhitespaceRange.getBegin() != Tok->WhitespaceRange.getEnd();
  728. bool SpacesAfter = Tok->Next->WhitespaceRange.getBegin() !=
  729. Tok->Next->WhitespaceRange.getEnd();
  730. if (SpacesBefore && !SpacesAfter)
  731. ++CountBoundToVariable;
  732. else if (!SpacesBefore && SpacesAfter)
  733. ++CountBoundToType;
  734. }
  735. if (Tok->Type == TT_TemplateCloser &&
  736. Tok->Previous->Type == TT_TemplateCloser &&
  737. Tok->WhitespaceRange.getBegin() == Tok->WhitespaceRange.getEnd())
  738. HasCpp03IncompatibleFormat = true;
  739. if (Tok->PackingKind == PPK_BinPacked)
  740. HasBinPackedFunction = true;
  741. if (Tok->PackingKind == PPK_OnePerLine)
  742. HasOnePerLineFunction = true;
  743. Tok = Tok->Next;
  744. }
  745. }
  746. if (Style.DerivePointerBinding) {
  747. if (CountBoundToType > CountBoundToVariable)
  748. Style.PointerBindsToType = true;
  749. else if (CountBoundToType < CountBoundToVariable)
  750. Style.PointerBindsToType = false;
  751. }
  752. if (Style.Standard == FormatStyle::LS_Auto) {
  753. Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11
  754. : FormatStyle::LS_Cpp03;
  755. }
  756. BinPackInconclusiveFunctions =
  757. HasBinPackedFunction || !HasOnePerLineFunction;
  758. }
  759. /// \brief Get the indent of \p Level from \p IndentForLevel.
  760. ///
  761. /// \p IndentForLevel must contain the indent for the level \c l
  762. /// at \p IndentForLevel[l], or a value < 0 if the indent for
  763. /// that level is unknown.
  764. unsigned getIndent(const std::vector<int> IndentForLevel, unsigned Level) {
  765. if (IndentForLevel[Level] != -1)
  766. return IndentForLevel[Level];
  767. if (Level == 0)
  768. return 0;
  769. return getIndent(IndentForLevel, Level - 1) + Style.IndentWidth;
  770. }
  771. /// \brief Get the offset of the line relatively to the level.
  772. ///
  773. /// For example, 'public:' labels in classes are offset by 1 or 2
  774. /// characters to the left from their level.
  775. int getIndentOffset(const FormatToken &RootToken) {
  776. if (RootToken.isAccessSpecifier(false) || RootToken.isObjCAccessSpecifier())
  777. return Style.AccessModifierOffset;
  778. return 0;
  779. }
  780. /// \brief Tries to merge lines into one.
  781. ///
  782. /// This will change \c Line and \c AnnotatedLine to contain the merged line,
  783. /// if possible; note that \c I will be incremented when lines are merged.
  784. void tryFitMultipleLinesInOne(unsigned Indent,
  785. std::vector<AnnotatedLine>::iterator &I,
  786. std::vector<AnnotatedLine>::iterator E) {
  787. // We can never merge stuff if there are trailing line comments.
  788. if (I->Last->Type == TT_LineComment)
  789. return;
  790. if (Indent > Style.ColumnLimit)
  791. return;
  792. unsigned Limit = Style.ColumnLimit - Indent;
  793. // If we already exceed the column limit, we set 'Limit' to 0. The different
  794. // tryMerge..() functions can then decide whether to still do merging.
  795. Limit = I->Last->TotalLength > Limit ? 0 : Limit - I->Last->TotalLength;
  796. if (I + 1 == E || (I + 1)->Type == LT_Invalid)
  797. return;
  798. if (I->Last->is(tok::l_brace)) {
  799. tryMergeSimpleBlock(I, E, Limit);
  800. } else if (Style.AllowShortIfStatementsOnASingleLine &&
  801. I->First->is(tok::kw_if)) {
  802. tryMergeSimpleControlStatement(I, E, Limit);
  803. } else if (Style.AllowShortLoopsOnASingleLine &&
  804. I->First->isOneOf(tok::kw_for, tok::kw_while)) {
  805. tryMergeSimpleControlStatement(I, E, Limit);
  806. } else if (I->InPPDirective &&
  807. (I->First->HasUnescapedNewline || I->First->IsFirst)) {
  808. tryMergeSimplePPDirective(I, E, Limit);
  809. }
  810. }
  811. void tryMergeSimplePPDirective(std::vector<AnnotatedLine>::iterator &I,
  812. std::vector<AnnotatedLine>::iterator E,
  813. unsigned Limit) {
  814. if (Limit == 0)
  815. return;
  816. AnnotatedLine &Line = *I;
  817. if (!(I + 1)->InPPDirective || (I + 1)->First->HasUnescapedNewline)
  818. return;
  819. if (I + 2 != E && (I + 2)->InPPDirective &&
  820. !(I + 2)->First->HasUnescapedNewline)
  821. return;
  822. if (1 + (I + 1)->Last->TotalLength > Limit)
  823. return;
  824. join(Line, *(++I));
  825. }
  826. void tryMergeSimpleControlStatement(std::vector<AnnotatedLine>::iterator &I,
  827. std::vector<AnnotatedLine>::iterator E,
  828. unsigned Limit) {
  829. if (Limit == 0)
  830. return;
  831. if (Style.BreakBeforeBraces == FormatStyle::BS_Allman &&
  832. (I + 1)->First->is(tok::l_brace))
  833. return;
  834. if ((I + 1)->InPPDirective != I->InPPDirective ||
  835. ((I + 1)->InPPDirective && (I + 1)->First->HasUnescapedNewline))
  836. return;
  837. AnnotatedLine &Line = *I;
  838. if (Line.Last->isNot(tok::r_paren))
  839. return;
  840. if (1 + (I + 1)->Last->TotalLength > Limit)
  841. return;
  842. if ((I + 1)->First->isOneOf(tok::semi, tok::kw_if, tok::kw_for,
  843. tok::kw_while) ||
  844. (I + 1)->First->Type == TT_LineComment)
  845. return;
  846. // Only inline simple if's (no nested if or else).
  847. if (I + 2 != E && Line.First->is(tok::kw_if) &&
  848. (I + 2)->First->is(tok::kw_else))
  849. return;
  850. join(Line, *(++I));
  851. }
  852. void tryMergeSimpleBlock(std::vector<AnnotatedLine>::iterator &I,
  853. std::vector<AnnotatedLine>::iterator E,
  854. unsigned Limit) {
  855. // No merging if the brace already is on the next line.
  856. if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
  857. return;
  858. // First, check that the current line allows merging. This is the case if
  859. // we're not in a control flow statement and the last token is an opening
  860. // brace.
  861. AnnotatedLine &Line = *I;
  862. if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::r_brace,
  863. tok::kw_else, tok::kw_try, tok::kw_catch,
  864. tok::kw_for,
  865. // This gets rid of all ObjC @ keywords and methods.
  866. tok::at, tok::minus, tok::plus))
  867. return;
  868. FormatToken *Tok = (I + 1)->First;
  869. if (Tok->is(tok::r_brace) && !Tok->MustBreakBefore &&
  870. (Tok->getNextNonComment() == NULL ||
  871. Tok->getNextNonComment()->is(tok::semi))) {
  872. // We merge empty blocks even if the line exceeds the column limit.
  873. Tok->SpacesRequiredBefore = 0;
  874. Tok->CanBreakBefore = true;
  875. join(Line, *(I + 1));
  876. I += 1;
  877. } else if (Limit != 0 && Line.First->isNot(tok::kw_namespace)) {
  878. // Check that we still have three lines and they fit into the limit.
  879. if (I + 2 == E || (I + 2)->Type == LT_Invalid ||
  880. !nextTwoLinesFitInto(I, Limit))
  881. return;
  882. // Second, check that the next line does not contain any braces - if it
  883. // does, readability declines when putting it into a single line.
  884. if ((I + 1)->Last->Type == TT_LineComment || Tok->MustBreakBefore)
  885. return;
  886. do {
  887. if (Tok->isOneOf(tok::l_brace, tok::r_brace))
  888. return;
  889. Tok = Tok->Next;
  890. } while (Tok != NULL);
  891. // Last, check that the third line contains a single closing brace.
  892. Tok = (I + 2)->First;
  893. if (Tok->getNextNonComment() != NULL || Tok->isNot(tok::r_brace) ||
  894. Tok->MustBreakBefore)
  895. return;
  896. join(Line, *(I + 1));
  897. join(Line, *(I + 2));
  898. I += 2;
  899. }
  900. }
  901. bool nextTwoLinesFitInto(std::vector<AnnotatedLine>::iterator I,
  902. unsigned Limit) {
  903. return 1 + (I + 1)->Last->TotalLength + 1 + (I + 2)->Last->TotalLength <=
  904. Limit;
  905. }
  906. void join(AnnotatedLine &A, const AnnotatedLine &B) {
  907. assert(!A.Last->Next);
  908. assert(!B.First->Previous);
  909. A.Last->Next = B.First;
  910. B.First->Previous = A.Last;
  911. unsigned LengthA = A.Last->TotalLength + B.First->SpacesRequiredBefore;
  912. for (FormatToken *Tok = B.First; Tok; Tok = Tok->Next) {
  913. Tok->TotalLength += LengthA;
  914. A.Last = Tok;
  915. }
  916. }
  917. bool touchesRanges(const CharSourceRange &Range) {
  918. for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
  919. if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),
  920. Ranges[i].getBegin()) &&
  921. !SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(),
  922. Range.getBegin()))
  923. return true;
  924. }
  925. return false;
  926. }
  927. bool touchesLine(const AnnotatedLine &TheLine) {
  928. const FormatToken *First = TheLine.First;
  929. const FormatToken *Last = TheLine.Last;
  930. CharSourceRange LineRange = CharSourceRange::getCharRange(
  931. First->WhitespaceRange.getBegin().getLocWithOffset(
  932. First->LastNewlineOffset),
  933. Last->Tok.getLocation().getLocWithOffset(Last->TokenText.size() - 1));
  934. return touchesRanges(LineRange);
  935. }
  936. bool touchesPPDirective(std::vector<AnnotatedLine>::iterator I,
  937. std::vector<AnnotatedLine>::iterator E) {
  938. for (; I != E; ++I) {
  939. if (I->First->HasUnescapedNewline)
  940. return false;
  941. if (touchesLine(*I))
  942. return true;
  943. }
  944. return false;
  945. }
  946. bool touchesEmptyLineBefore(const AnnotatedLine &TheLine) {
  947. const FormatToken *First = TheLine.First;
  948. CharSourceRange LineRange = CharSourceRange::getCharRange(
  949. First->WhitespaceRange.getBegin(),
  950. First->WhitespaceRange.getBegin().getLocWithOffset(
  951. First->LastNewlineOffset));
  952. return touchesRanges(LineRange);
  953. }
  954. virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) {
  955. AnnotatedLines.push_back(AnnotatedLine(TheLine));
  956. }
  957. /// \brief Add a new line and the required indent before the first Token
  958. /// of the \c UnwrappedLine if there was no structural parsing error.
  959. /// Returns the indent level of the \c UnwrappedLine.
  960. void formatFirstToken(const FormatToken &RootToken,
  961. const FormatToken *PreviousToken, unsigned Indent,
  962. bool InPPDirective) {
  963. unsigned Newlines =
  964. std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);
  965. // Remove empty lines before "}" where applicable.
  966. if (RootToken.is(tok::r_brace) &&
  967. (!RootToken.Next ||
  968. (RootToken.Next->is(tok::semi) && !RootToken.Next->Next)))
  969. Newlines = std::min(Newlines, 1u);
  970. if (Newlines == 0 && !RootToken.IsFirst)
  971. Newlines = 1;
  972. // Insert extra new line before access specifiers.
  973. if (PreviousToken && PreviousToken->isOneOf(tok::semi, tok::r_brace) &&
  974. RootToken.isAccessSpecifier() && RootToken.NewlinesBefore == 1)
  975. ++Newlines;
  976. Whitespaces.replaceWhitespace(
  977. RootToken, Newlines, Indent, Indent,
  978. InPPDirective && !RootToken.HasUnescapedNewline);
  979. }
  980. FormatStyle Style;
  981. Lexer &Lex;
  982. SourceManager &SourceMgr;
  983. WhitespaceManager Whitespaces;
  984. std::vector<CharSourceRange> Ranges;
  985. std::vector<AnnotatedLine> AnnotatedLines;
  986. encoding::Encoding Encoding;
  987. bool BinPackInconclusiveFunctions;
  988. };
  989. } // end anonymous namespace
  990. tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
  991. SourceManager &SourceMgr,
  992. std::vector<CharSourceRange> Ranges) {
  993. Formatter formatter(Style, Lex, SourceMgr, Ranges);
  994. return formatter.format();
  995. }
  996. tooling::Replacements reformat(const FormatStyle &Style, StringRef Code,
  997. std::vector<tooling::Range> Ranges,
  998. StringRef FileName) {
  999. FileManager Files((FileSystemOptions()));
  1000. DiagnosticsEngine Diagnostics(
  1001. IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
  1002. new DiagnosticOptions);
  1003. SourceManager SourceMgr(Diagnostics, Files);
  1004. llvm::MemoryBuffer *Buf = llvm::MemoryBuffer::getMemBuffer(Code, FileName);
  1005. const clang::FileEntry *Entry =
  1006. Files.getVirtualFile(FileName, Buf->getBufferSize(), 0);
  1007. SourceMgr.overrideFileContents(Entry, Buf);
  1008. FileID ID =
  1009. SourceMgr.createFileID(Entry, SourceLocation(), clang::SrcMgr::C_User);
  1010. Lexer Lex(ID, SourceMgr.getBuffer(ID), SourceMgr,
  1011. getFormattingLangOpts(Style.Standard));
  1012. SourceLocation StartOfFile = SourceMgr.getLocForStartOfFile(ID);
  1013. std::vector<CharSourceRange> CharRanges;
  1014. for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
  1015. SourceLocation Start = StartOfFile.getLocWithOffset(Ranges[i].getOffset());
  1016. SourceLocation End = Start.getLocWithOffset(Ranges[i].getLength());
  1017. CharRanges.push_back(CharSourceRange::getCharRange(Start, End));
  1018. }
  1019. return reformat(Style, Lex, SourceMgr, CharRanges);
  1020. }
  1021. LangOptions getFormattingLangOpts(FormatStyle::LanguageStandard Standard) {
  1022. LangOptions LangOpts;
  1023. LangOpts.CPlusPlus = 1;
  1024. LangOpts.CPlusPlus11 = Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
  1025. LangOpts.LineComment = 1;
  1026. LangOpts.Bool = 1;
  1027. LangOpts.ObjC1 = 1;
  1028. LangOpts.ObjC2 = 1;
  1029. return LangOpts;
  1030. }
  1031. } // namespace format
  1032. } // namespace clang