Format.cpp 43 KB

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