UnwrappedLineParser.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  1. //===--- UnwrappedLineParser.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 contains the implementation of the UnwrappedLineParser,
  12. /// which turns a stream of tokens into UnwrappedLines.
  13. ///
  14. //===----------------------------------------------------------------------===//
  15. #define DEBUG_TYPE "format-parser"
  16. #include "UnwrappedLineParser.h"
  17. #include "llvm/Support/Debug.h"
  18. namespace clang {
  19. namespace format {
  20. class FormatTokenSource {
  21. public:
  22. virtual ~FormatTokenSource() {}
  23. virtual FormatToken *getNextToken() = 0;
  24. virtual unsigned getPosition() = 0;
  25. virtual FormatToken *setPosition(unsigned Position) = 0;
  26. };
  27. namespace {
  28. class ScopedDeclarationState {
  29. public:
  30. ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
  31. bool MustBeDeclaration)
  32. : Line(Line), Stack(Stack) {
  33. Line.MustBeDeclaration = MustBeDeclaration;
  34. Stack.push_back(MustBeDeclaration);
  35. }
  36. ~ScopedDeclarationState() {
  37. Stack.pop_back();
  38. if (!Stack.empty())
  39. Line.MustBeDeclaration = Stack.back();
  40. else
  41. Line.MustBeDeclaration = true;
  42. }
  43. private:
  44. UnwrappedLine &Line;
  45. std::vector<bool> &Stack;
  46. };
  47. class ScopedMacroState : public FormatTokenSource {
  48. public:
  49. ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
  50. FormatToken *&ResetToken, bool &StructuralError)
  51. : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
  52. PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
  53. StructuralError(StructuralError),
  54. PreviousStructuralError(StructuralError), Token(NULL) {
  55. TokenSource = this;
  56. Line.Level = 0;
  57. Line.InPPDirective = true;
  58. }
  59. ~ScopedMacroState() {
  60. TokenSource = PreviousTokenSource;
  61. ResetToken = Token;
  62. Line.InPPDirective = false;
  63. Line.Level = PreviousLineLevel;
  64. StructuralError = PreviousStructuralError;
  65. }
  66. virtual FormatToken *getNextToken() {
  67. // The \c UnwrappedLineParser guards against this by never calling
  68. // \c getNextToken() after it has encountered the first eof token.
  69. assert(!eof());
  70. Token = PreviousTokenSource->getNextToken();
  71. if (eof())
  72. return getFakeEOF();
  73. return Token;
  74. }
  75. virtual unsigned getPosition() { return PreviousTokenSource->getPosition(); }
  76. virtual FormatToken *setPosition(unsigned Position) {
  77. Token = PreviousTokenSource->setPosition(Position);
  78. return Token;
  79. }
  80. private:
  81. bool eof() { return Token && Token->HasUnescapedNewline; }
  82. FormatToken *getFakeEOF() {
  83. static bool EOFInitialized = false;
  84. static FormatToken FormatTok;
  85. if (!EOFInitialized) {
  86. FormatTok.Tok.startToken();
  87. FormatTok.Tok.setKind(tok::eof);
  88. EOFInitialized = true;
  89. }
  90. return &FormatTok;
  91. }
  92. UnwrappedLine &Line;
  93. FormatTokenSource *&TokenSource;
  94. FormatToken *&ResetToken;
  95. unsigned PreviousLineLevel;
  96. FormatTokenSource *PreviousTokenSource;
  97. bool &StructuralError;
  98. bool PreviousStructuralError;
  99. FormatToken *Token;
  100. };
  101. } // end anonymous namespace
  102. class ScopedLineState {
  103. public:
  104. ScopedLineState(UnwrappedLineParser &Parser,
  105. bool SwitchToPreprocessorLines = false)
  106. : Parser(Parser) {
  107. OriginalLines = Parser.CurrentLines;
  108. if (SwitchToPreprocessorLines)
  109. Parser.CurrentLines = &Parser.PreprocessorDirectives;
  110. else if (!Parser.Line->Tokens.empty())
  111. Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
  112. PreBlockLine = Parser.Line.take();
  113. Parser.Line.reset(new UnwrappedLine());
  114. Parser.Line->Level = PreBlockLine->Level;
  115. Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
  116. }
  117. ~ScopedLineState() {
  118. if (!Parser.Line->Tokens.empty()) {
  119. Parser.addUnwrappedLine();
  120. }
  121. assert(Parser.Line->Tokens.empty());
  122. Parser.Line.reset(PreBlockLine);
  123. if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
  124. Parser.MustBreakBeforeNextToken = true;
  125. Parser.CurrentLines = OriginalLines;
  126. }
  127. private:
  128. UnwrappedLineParser &Parser;
  129. UnwrappedLine *PreBlockLine;
  130. SmallVectorImpl<UnwrappedLine> *OriginalLines;
  131. };
  132. namespace {
  133. class IndexedTokenSource : public FormatTokenSource {
  134. public:
  135. IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
  136. : Tokens(Tokens), Position(-1) {}
  137. virtual FormatToken *getNextToken() {
  138. ++Position;
  139. return Tokens[Position];
  140. }
  141. virtual unsigned getPosition() {
  142. assert(Position >= 0);
  143. return Position;
  144. }
  145. virtual FormatToken *setPosition(unsigned P) {
  146. Position = P;
  147. return Tokens[Position];
  148. }
  149. void reset() { Position = -1; }
  150. private:
  151. ArrayRef<FormatToken *> Tokens;
  152. int Position;
  153. };
  154. } // end anonymous namespace
  155. UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
  156. ArrayRef<FormatToken *> Tokens,
  157. UnwrappedLineConsumer &Callback)
  158. : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
  159. CurrentLines(&Lines), StructuralError(false), Style(Style), Tokens(NULL),
  160. Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1) {}
  161. void UnwrappedLineParser::reset() {
  162. PPBranchLevel = -1;
  163. Line.reset(new UnwrappedLine);
  164. CommentsBeforeNextToken.clear();
  165. FormatTok = NULL;
  166. MustBreakBeforeNextToken = false;
  167. PreprocessorDirectives.clear();
  168. CurrentLines = &Lines;
  169. DeclarationScopeStack.clear();
  170. StructuralError = false;
  171. PPStack.clear();
  172. }
  173. bool UnwrappedLineParser::parse() {
  174. IndexedTokenSource TokenSource(AllTokens);
  175. do {
  176. DEBUG(llvm::dbgs() << "----\n");
  177. reset();
  178. Tokens = &TokenSource;
  179. TokenSource.reset();
  180. readToken();
  181. parseFile();
  182. // Create line with eof token.
  183. pushToken(FormatTok);
  184. addUnwrappedLine();
  185. for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
  186. E = Lines.end();
  187. I != E; ++I) {
  188. Callback.consumeUnwrappedLine(*I);
  189. }
  190. Callback.finishRun();
  191. Lines.clear();
  192. while (!PPLevelBranchIndex.empty() &&
  193. PPLevelBranchIndex.back() + 1 == PPLevelBranchCount.back()) {
  194. PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
  195. PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
  196. }
  197. if (!PPLevelBranchIndex.empty()) {
  198. ++PPLevelBranchIndex.back();
  199. assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
  200. assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
  201. }
  202. } while (!PPLevelBranchIndex.empty());
  203. return StructuralError;
  204. }
  205. void UnwrappedLineParser::parseFile() {
  206. ScopedDeclarationState DeclarationState(
  207. *Line, DeclarationScopeStack,
  208. /*MustBeDeclaration=*/ !Line->InPPDirective);
  209. parseLevel(/*HasOpeningBrace=*/false);
  210. // Make sure to format the remaining tokens.
  211. flushComments(true);
  212. addUnwrappedLine();
  213. }
  214. void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
  215. bool SwitchLabelEncountered = false;
  216. do {
  217. switch (FormatTok->Tok.getKind()) {
  218. case tok::comment:
  219. nextToken();
  220. addUnwrappedLine();
  221. break;
  222. case tok::l_brace:
  223. // FIXME: Add parameter whether this can happen - if this happens, we must
  224. // be in a non-declaration context.
  225. parseBlock(/*MustBeDeclaration=*/false);
  226. addUnwrappedLine();
  227. break;
  228. case tok::r_brace:
  229. if (HasOpeningBrace)
  230. return;
  231. StructuralError = true;
  232. nextToken();
  233. addUnwrappedLine();
  234. break;
  235. case tok::kw_default:
  236. case tok::kw_case:
  237. if (!SwitchLabelEncountered &&
  238. (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
  239. ++Line->Level;
  240. SwitchLabelEncountered = true;
  241. parseStructuralElement();
  242. break;
  243. default:
  244. parseStructuralElement();
  245. break;
  246. }
  247. } while (!eof());
  248. }
  249. void UnwrappedLineParser::calculateBraceTypes() {
  250. // We'll parse forward through the tokens until we hit
  251. // a closing brace or eof - note that getNextToken() will
  252. // parse macros, so this will magically work inside macro
  253. // definitions, too.
  254. unsigned StoredPosition = Tokens->getPosition();
  255. unsigned Position = StoredPosition;
  256. FormatToken *Tok = FormatTok;
  257. // Keep a stack of positions of lbrace tokens. We will
  258. // update information about whether an lbrace starts a
  259. // braced init list or a different block during the loop.
  260. SmallVector<FormatToken *, 8> LBraceStack;
  261. assert(Tok->Tok.is(tok::l_brace));
  262. do {
  263. // Get next none-comment token.
  264. FormatToken *NextTok;
  265. unsigned ReadTokens = 0;
  266. do {
  267. NextTok = Tokens->getNextToken();
  268. ++ReadTokens;
  269. } while (NextTok->is(tok::comment));
  270. switch (Tok->Tok.getKind()) {
  271. case tok::l_brace:
  272. LBraceStack.push_back(Tok);
  273. break;
  274. case tok::r_brace:
  275. if (!LBraceStack.empty()) {
  276. if (LBraceStack.back()->BlockKind == BK_Unknown) {
  277. // If there is a comma, semicolon or right paren after the closing
  278. // brace, we assume this is a braced initializer list. Note that
  279. // regardless how we mark inner braces here, we will overwrite the
  280. // BlockKind later if we parse a braced list (where all blocks inside
  281. // are by default braced lists), or when we explicitly detect blocks
  282. // (for example while parsing lambdas).
  283. //
  284. // We exclude + and - as they can be ObjC visibility modifiers.
  285. if (NextTok->isOneOf(tok::comma, tok::semi, tok::r_paren,
  286. tok::r_square, tok::l_brace, tok::colon) ||
  287. (NextTok->isBinaryOperator() &&
  288. !NextTok->isOneOf(tok::plus, tok::minus))) {
  289. Tok->BlockKind = BK_BracedInit;
  290. LBraceStack.back()->BlockKind = BK_BracedInit;
  291. } else {
  292. Tok->BlockKind = BK_Block;
  293. LBraceStack.back()->BlockKind = BK_Block;
  294. }
  295. }
  296. LBraceStack.pop_back();
  297. }
  298. break;
  299. case tok::semi:
  300. case tok::kw_if:
  301. case tok::kw_while:
  302. case tok::kw_for:
  303. case tok::kw_switch:
  304. case tok::kw_try:
  305. if (!LBraceStack.empty())
  306. LBraceStack.back()->BlockKind = BK_Block;
  307. break;
  308. default:
  309. break;
  310. }
  311. Tok = NextTok;
  312. Position += ReadTokens;
  313. } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
  314. // Assume other blocks for all unclosed opening braces.
  315. for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
  316. if (LBraceStack[i]->BlockKind == BK_Unknown)
  317. LBraceStack[i]->BlockKind = BK_Block;
  318. }
  319. FormatTok = Tokens->setPosition(StoredPosition);
  320. }
  321. void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel) {
  322. assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
  323. unsigned InitialLevel = Line->Level;
  324. nextToken();
  325. addUnwrappedLine();
  326. ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
  327. MustBeDeclaration);
  328. if (AddLevel)
  329. ++Line->Level;
  330. parseLevel(/*HasOpeningBrace=*/true);
  331. if (!FormatTok->Tok.is(tok::r_brace)) {
  332. Line->Level = InitialLevel;
  333. StructuralError = true;
  334. return;
  335. }
  336. nextToken(); // Munch the closing brace.
  337. Line->Level = InitialLevel;
  338. }
  339. void UnwrappedLineParser::parseChildBlock() {
  340. FormatTok->BlockKind = BK_Block;
  341. nextToken();
  342. {
  343. ScopedLineState LineState(*this);
  344. ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
  345. /*MustBeDeclaration=*/false);
  346. Line->Level += 1;
  347. parseLevel(/*HasOpeningBrace=*/true);
  348. Line->Level -= 1;
  349. }
  350. nextToken();
  351. }
  352. void UnwrappedLineParser::parsePPDirective() {
  353. assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
  354. ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
  355. nextToken();
  356. if (FormatTok->Tok.getIdentifierInfo() == NULL) {
  357. parsePPUnknown();
  358. return;
  359. }
  360. switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
  361. case tok::pp_define:
  362. parsePPDefine();
  363. return;
  364. case tok::pp_if:
  365. parsePPIf(/*IfDef=*/false);
  366. break;
  367. case tok::pp_ifdef:
  368. case tok::pp_ifndef:
  369. parsePPIf(/*IfDef=*/true);
  370. break;
  371. case tok::pp_else:
  372. parsePPElse();
  373. break;
  374. case tok::pp_elif:
  375. parsePPElIf();
  376. break;
  377. case tok::pp_endif:
  378. parsePPEndIf();
  379. break;
  380. default:
  381. parsePPUnknown();
  382. break;
  383. }
  384. }
  385. void UnwrappedLineParser::pushPPConditional() {
  386. if (!PPStack.empty() && PPStack.back() == PP_Unreachable)
  387. PPStack.push_back(PP_Unreachable);
  388. else
  389. PPStack.push_back(PP_Conditional);
  390. }
  391. void UnwrappedLineParser::parsePPIf(bool IfDef) {
  392. ++PPBranchLevel;
  393. assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
  394. if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
  395. PPLevelBranchIndex.push_back(0);
  396. PPLevelBranchCount.push_back(0);
  397. }
  398. PPChainBranchIndex.push(0);
  399. nextToken();
  400. bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
  401. StringRef(FormatTok->Tok.getLiteralData(),
  402. FormatTok->Tok.getLength()) == "0") ||
  403. FormatTok->Tok.is(tok::kw_false);
  404. if ((!IfDef && IsLiteralFalse) || PPLevelBranchIndex[PPBranchLevel] > 0) {
  405. PPStack.push_back(PP_Unreachable);
  406. } else {
  407. pushPPConditional();
  408. }
  409. parsePPUnknown();
  410. }
  411. void UnwrappedLineParser::parsePPElse() {
  412. if (!PPStack.empty())
  413. PPStack.pop_back();
  414. assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
  415. if (!PPChainBranchIndex.empty())
  416. ++PPChainBranchIndex.top();
  417. if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
  418. PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top()) {
  419. PPStack.push_back(PP_Unreachable);
  420. } else {
  421. pushPPConditional();
  422. }
  423. parsePPUnknown();
  424. }
  425. void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
  426. void UnwrappedLineParser::parsePPEndIf() {
  427. assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
  428. if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
  429. if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
  430. assert(PPLevelBranchCount[PPBranchLevel] == 0);
  431. PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
  432. }
  433. }
  434. --PPBranchLevel;
  435. if (!PPChainBranchIndex.empty())
  436. PPChainBranchIndex.pop();
  437. if (!PPStack.empty())
  438. PPStack.pop_back();
  439. parsePPUnknown();
  440. }
  441. void UnwrappedLineParser::parsePPDefine() {
  442. nextToken();
  443. if (FormatTok->Tok.getKind() != tok::identifier) {
  444. parsePPUnknown();
  445. return;
  446. }
  447. nextToken();
  448. if (FormatTok->Tok.getKind() == tok::l_paren &&
  449. FormatTok->WhitespaceRange.getBegin() ==
  450. FormatTok->WhitespaceRange.getEnd()) {
  451. parseParens();
  452. }
  453. addUnwrappedLine();
  454. Line->Level = 1;
  455. // Errors during a preprocessor directive can only affect the layout of the
  456. // preprocessor directive, and thus we ignore them. An alternative approach
  457. // would be to use the same approach we use on the file level (no
  458. // re-indentation if there was a structural error) within the macro
  459. // definition.
  460. parseFile();
  461. }
  462. void UnwrappedLineParser::parsePPUnknown() {
  463. do {
  464. nextToken();
  465. } while (!eof());
  466. addUnwrappedLine();
  467. }
  468. // Here we blacklist certain tokens that are not usually the first token in an
  469. // unwrapped line. This is used in attempt to distinguish macro calls without
  470. // trailing semicolons from other constructs split to several lines.
  471. bool tokenCanStartNewLine(clang::Token Tok) {
  472. // Semicolon can be a null-statement, l_square can be a start of a macro or
  473. // a C++11 attribute, but this doesn't seem to be common.
  474. return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
  475. Tok.isNot(tok::l_square) &&
  476. // Tokens that can only be used as binary operators and a part of
  477. // overloaded operator names.
  478. Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
  479. Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
  480. Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
  481. Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
  482. Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
  483. Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
  484. Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
  485. Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
  486. Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
  487. Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
  488. Tok.isNot(tok::lesslessequal) &&
  489. // Colon is used in labels, base class lists, initializer lists,
  490. // range-based for loops, ternary operator, but should never be the
  491. // first token in an unwrapped line.
  492. Tok.isNot(tok::colon);
  493. }
  494. void UnwrappedLineParser::parseStructuralElement() {
  495. assert(!FormatTok->Tok.is(tok::l_brace));
  496. switch (FormatTok->Tok.getKind()) {
  497. case tok::at:
  498. nextToken();
  499. if (FormatTok->Tok.is(tok::l_brace)) {
  500. parseBracedList();
  501. break;
  502. }
  503. switch (FormatTok->Tok.getObjCKeywordID()) {
  504. case tok::objc_public:
  505. case tok::objc_protected:
  506. case tok::objc_package:
  507. case tok::objc_private:
  508. return parseAccessSpecifier();
  509. case tok::objc_interface:
  510. case tok::objc_implementation:
  511. return parseObjCInterfaceOrImplementation();
  512. case tok::objc_protocol:
  513. return parseObjCProtocol();
  514. case tok::objc_end:
  515. return; // Handled by the caller.
  516. case tok::objc_optional:
  517. case tok::objc_required:
  518. nextToken();
  519. addUnwrappedLine();
  520. return;
  521. default:
  522. break;
  523. }
  524. break;
  525. case tok::kw_namespace:
  526. parseNamespace();
  527. return;
  528. case tok::kw_inline:
  529. nextToken();
  530. if (FormatTok->Tok.is(tok::kw_namespace)) {
  531. parseNamespace();
  532. return;
  533. }
  534. break;
  535. case tok::kw_public:
  536. case tok::kw_protected:
  537. case tok::kw_private:
  538. parseAccessSpecifier();
  539. return;
  540. case tok::kw_if:
  541. parseIfThenElse();
  542. return;
  543. case tok::kw_for:
  544. case tok::kw_while:
  545. parseForOrWhileLoop();
  546. return;
  547. case tok::kw_do:
  548. parseDoWhile();
  549. return;
  550. case tok::kw_switch:
  551. parseSwitch();
  552. return;
  553. case tok::kw_default:
  554. nextToken();
  555. parseLabel();
  556. return;
  557. case tok::kw_case:
  558. parseCaseLabel();
  559. return;
  560. case tok::kw_return:
  561. parseReturn();
  562. return;
  563. case tok::kw_extern:
  564. nextToken();
  565. if (FormatTok->Tok.is(tok::string_literal)) {
  566. nextToken();
  567. if (FormatTok->Tok.is(tok::l_brace)) {
  568. parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
  569. addUnwrappedLine();
  570. return;
  571. }
  572. }
  573. // In all other cases, parse the declaration.
  574. break;
  575. default:
  576. break;
  577. }
  578. do {
  579. switch (FormatTok->Tok.getKind()) {
  580. case tok::at:
  581. nextToken();
  582. if (FormatTok->Tok.is(tok::l_brace))
  583. parseBracedList();
  584. break;
  585. case tok::kw_enum:
  586. parseEnum();
  587. break;
  588. case tok::kw_struct:
  589. case tok::kw_union:
  590. case tok::kw_class:
  591. parseRecord();
  592. // A record declaration or definition is always the start of a structural
  593. // element.
  594. break;
  595. case tok::semi:
  596. nextToken();
  597. addUnwrappedLine();
  598. return;
  599. case tok::r_brace:
  600. addUnwrappedLine();
  601. return;
  602. case tok::l_paren:
  603. parseParens();
  604. break;
  605. case tok::caret:
  606. nextToken();
  607. if (FormatTok->is(tok::l_brace)) {
  608. parseChildBlock();
  609. }
  610. break;
  611. case tok::l_brace:
  612. if (!tryToParseBracedList()) {
  613. // A block outside of parentheses must be the last part of a
  614. // structural element.
  615. // FIXME: Figure out cases where this is not true, and add projections
  616. // for them (the one we know is missing are lambdas).
  617. if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
  618. Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup ||
  619. Style.BreakBeforeBraces == FormatStyle::BS_Allman)
  620. addUnwrappedLine();
  621. parseBlock(/*MustBeDeclaration=*/false);
  622. addUnwrappedLine();
  623. return;
  624. }
  625. // Otherwise this was a braced init list, and the structural
  626. // element continues.
  627. break;
  628. case tok::identifier: {
  629. StringRef Text = FormatTok->TokenText;
  630. nextToken();
  631. if (Line->Tokens.size() == 1) {
  632. if (FormatTok->Tok.is(tok::colon)) {
  633. parseLabel();
  634. return;
  635. }
  636. // Recognize function-like macro usages without trailing semicolon.
  637. if (FormatTok->Tok.is(tok::l_paren)) {
  638. parseParens();
  639. if (FormatTok->HasUnescapedNewline &&
  640. tokenCanStartNewLine(FormatTok->Tok)) {
  641. addUnwrappedLine();
  642. return;
  643. }
  644. } else if (FormatTok->HasUnescapedNewline && Text.size() >= 5 &&
  645. Text == Text.upper()) {
  646. // Recognize free-standing macros like Q_OBJECT.
  647. addUnwrappedLine();
  648. return;
  649. }
  650. }
  651. break;
  652. }
  653. case tok::equal:
  654. nextToken();
  655. if (FormatTok->Tok.is(tok::l_brace)) {
  656. parseBracedList();
  657. }
  658. break;
  659. case tok::l_square:
  660. tryToParseLambda();
  661. break;
  662. default:
  663. nextToken();
  664. break;
  665. }
  666. } while (!eof());
  667. }
  668. void UnwrappedLineParser::tryToParseLambda() {
  669. // FIXME: This is a dirty way to access the previous token. Find a better
  670. // solution.
  671. if (!Line->Tokens.empty() &&
  672. Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator)) {
  673. nextToken();
  674. return;
  675. }
  676. assert(FormatTok->is(tok::l_square));
  677. FormatToken &LSquare = *FormatTok;
  678. if (!tryToParseLambdaIntroducer())
  679. return;
  680. while (FormatTok->isNot(tok::l_brace)) {
  681. switch (FormatTok->Tok.getKind()) {
  682. case tok::l_brace:
  683. break;
  684. case tok::l_paren:
  685. parseParens();
  686. break;
  687. case tok::identifier:
  688. case tok::kw_mutable:
  689. nextToken();
  690. break;
  691. default:
  692. return;
  693. }
  694. }
  695. LSquare.Type = TT_LambdaLSquare;
  696. parseChildBlock();
  697. }
  698. bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
  699. nextToken();
  700. if (FormatTok->is(tok::equal)) {
  701. nextToken();
  702. if (FormatTok->is(tok::r_square)) {
  703. nextToken();
  704. return true;
  705. }
  706. if (FormatTok->isNot(tok::comma))
  707. return false;
  708. nextToken();
  709. } else if (FormatTok->is(tok::amp)) {
  710. nextToken();
  711. if (FormatTok->is(tok::r_square)) {
  712. nextToken();
  713. return true;
  714. }
  715. if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
  716. return false;
  717. }
  718. if (FormatTok->is(tok::comma))
  719. nextToken();
  720. } else if (FormatTok->is(tok::r_square)) {
  721. nextToken();
  722. return true;
  723. }
  724. do {
  725. if (FormatTok->is(tok::amp))
  726. nextToken();
  727. if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
  728. return false;
  729. nextToken();
  730. if (FormatTok->is(tok::comma)) {
  731. nextToken();
  732. } else if (FormatTok->is(tok::r_square)) {
  733. nextToken();
  734. return true;
  735. } else {
  736. return false;
  737. }
  738. } while (!eof());
  739. return false;
  740. }
  741. bool UnwrappedLineParser::tryToParseBracedList() {
  742. if (FormatTok->BlockKind == BK_Unknown)
  743. calculateBraceTypes();
  744. assert(FormatTok->BlockKind != BK_Unknown);
  745. if (FormatTok->BlockKind == BK_Block)
  746. return false;
  747. parseBracedList();
  748. return true;
  749. }
  750. bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
  751. bool HasError = false;
  752. nextToken();
  753. // FIXME: Once we have an expression parser in the UnwrappedLineParser,
  754. // replace this by using parseAssigmentExpression() inside.
  755. do {
  756. // FIXME: When we start to support lambdas, we'll want to parse them away
  757. // here, otherwise our bail-out scenarios below break. The better solution
  758. // might be to just implement a more or less complete expression parser.
  759. switch (FormatTok->Tok.getKind()) {
  760. case tok::caret:
  761. nextToken();
  762. if (FormatTok->is(tok::l_brace)) {
  763. parseChildBlock();
  764. }
  765. break;
  766. case tok::l_square:
  767. tryToParseLambda();
  768. break;
  769. case tok::l_brace:
  770. // Assume there are no blocks inside a braced init list apart
  771. // from the ones we explicitly parse out (like lambdas).
  772. FormatTok->BlockKind = BK_BracedInit;
  773. parseBracedList();
  774. break;
  775. case tok::r_brace:
  776. nextToken();
  777. return !HasError;
  778. case tok::semi:
  779. HasError = true;
  780. if (!ContinueOnSemicolons)
  781. return !HasError;
  782. nextToken();
  783. break;
  784. case tok::comma:
  785. nextToken();
  786. break;
  787. default:
  788. nextToken();
  789. break;
  790. }
  791. } while (!eof());
  792. return false;
  793. }
  794. void UnwrappedLineParser::parseReturn() {
  795. nextToken();
  796. do {
  797. switch (FormatTok->Tok.getKind()) {
  798. case tok::l_brace:
  799. parseBracedList();
  800. if (FormatTok->Tok.isNot(tok::semi)) {
  801. // Assume missing ';'.
  802. addUnwrappedLine();
  803. return;
  804. }
  805. break;
  806. case tok::l_paren:
  807. parseParens();
  808. break;
  809. case tok::r_brace:
  810. // Assume missing ';'.
  811. addUnwrappedLine();
  812. return;
  813. case tok::semi:
  814. nextToken();
  815. addUnwrappedLine();
  816. return;
  817. case tok::l_square:
  818. tryToParseLambda();
  819. break;
  820. default:
  821. nextToken();
  822. break;
  823. }
  824. } while (!eof());
  825. }
  826. void UnwrappedLineParser::parseParens() {
  827. assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
  828. nextToken();
  829. do {
  830. switch (FormatTok->Tok.getKind()) {
  831. case tok::l_paren:
  832. parseParens();
  833. break;
  834. case tok::r_paren:
  835. nextToken();
  836. return;
  837. case tok::r_brace:
  838. // A "}" inside parenthesis is an error if there wasn't a matching "{".
  839. return;
  840. case tok::l_square:
  841. tryToParseLambda();
  842. break;
  843. case tok::l_brace: {
  844. if (!tryToParseBracedList()) {
  845. parseChildBlock();
  846. }
  847. break;
  848. }
  849. case tok::at:
  850. nextToken();
  851. if (FormatTok->Tok.is(tok::l_brace))
  852. parseBracedList();
  853. break;
  854. default:
  855. nextToken();
  856. break;
  857. }
  858. } while (!eof());
  859. }
  860. void UnwrappedLineParser::parseIfThenElse() {
  861. assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
  862. nextToken();
  863. if (FormatTok->Tok.is(tok::l_paren))
  864. parseParens();
  865. bool NeedsUnwrappedLine = false;
  866. if (FormatTok->Tok.is(tok::l_brace)) {
  867. if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
  868. addUnwrappedLine();
  869. parseBlock(/*MustBeDeclaration=*/false);
  870. if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
  871. addUnwrappedLine();
  872. else
  873. NeedsUnwrappedLine = true;
  874. } else {
  875. addUnwrappedLine();
  876. ++Line->Level;
  877. parseStructuralElement();
  878. --Line->Level;
  879. }
  880. if (FormatTok->Tok.is(tok::kw_else)) {
  881. nextToken();
  882. if (FormatTok->Tok.is(tok::l_brace)) {
  883. if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
  884. addUnwrappedLine();
  885. parseBlock(/*MustBeDeclaration=*/false);
  886. addUnwrappedLine();
  887. } else if (FormatTok->Tok.is(tok::kw_if)) {
  888. parseIfThenElse();
  889. } else {
  890. addUnwrappedLine();
  891. ++Line->Level;
  892. parseStructuralElement();
  893. --Line->Level;
  894. }
  895. } else if (NeedsUnwrappedLine) {
  896. addUnwrappedLine();
  897. }
  898. }
  899. void UnwrappedLineParser::parseNamespace() {
  900. assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
  901. nextToken();
  902. if (FormatTok->Tok.is(tok::identifier))
  903. nextToken();
  904. if (FormatTok->Tok.is(tok::l_brace)) {
  905. if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
  906. Style.BreakBeforeBraces == FormatStyle::BS_Allman)
  907. addUnwrappedLine();
  908. bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
  909. (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
  910. DeclarationScopeStack.size() > 1);
  911. parseBlock(/*MustBeDeclaration=*/true, AddLevel);
  912. // Munch the semicolon after a namespace. This is more common than one would
  913. // think. Puttin the semicolon into its own line is very ugly.
  914. if (FormatTok->Tok.is(tok::semi))
  915. nextToken();
  916. addUnwrappedLine();
  917. }
  918. // FIXME: Add error handling.
  919. }
  920. void UnwrappedLineParser::parseForOrWhileLoop() {
  921. assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while)) &&
  922. "'for' or 'while' expected");
  923. nextToken();
  924. if (FormatTok->Tok.is(tok::l_paren))
  925. parseParens();
  926. if (FormatTok->Tok.is(tok::l_brace)) {
  927. if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
  928. addUnwrappedLine();
  929. parseBlock(/*MustBeDeclaration=*/false);
  930. addUnwrappedLine();
  931. } else {
  932. addUnwrappedLine();
  933. ++Line->Level;
  934. parseStructuralElement();
  935. --Line->Level;
  936. }
  937. }
  938. void UnwrappedLineParser::parseDoWhile() {
  939. assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
  940. nextToken();
  941. if (FormatTok->Tok.is(tok::l_brace)) {
  942. if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
  943. addUnwrappedLine();
  944. parseBlock(/*MustBeDeclaration=*/false);
  945. } else {
  946. addUnwrappedLine();
  947. ++Line->Level;
  948. parseStructuralElement();
  949. --Line->Level;
  950. }
  951. // FIXME: Add error handling.
  952. if (!FormatTok->Tok.is(tok::kw_while)) {
  953. addUnwrappedLine();
  954. return;
  955. }
  956. nextToken();
  957. parseStructuralElement();
  958. }
  959. void UnwrappedLineParser::parseLabel() {
  960. nextToken();
  961. unsigned OldLineLevel = Line->Level;
  962. if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
  963. --Line->Level;
  964. if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
  965. if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
  966. addUnwrappedLine();
  967. parseBlock(/*MustBeDeclaration=*/false);
  968. if (FormatTok->Tok.is(tok::kw_break)) {
  969. // "break;" after "}" on its own line only for BS_Allman
  970. if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
  971. addUnwrappedLine();
  972. parseStructuralElement();
  973. }
  974. }
  975. addUnwrappedLine();
  976. Line->Level = OldLineLevel;
  977. }
  978. void UnwrappedLineParser::parseCaseLabel() {
  979. assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
  980. // FIXME: fix handling of complex expressions here.
  981. do {
  982. nextToken();
  983. } while (!eof() && !FormatTok->Tok.is(tok::colon));
  984. parseLabel();
  985. }
  986. void UnwrappedLineParser::parseSwitch() {
  987. assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
  988. nextToken();
  989. if (FormatTok->Tok.is(tok::l_paren))
  990. parseParens();
  991. if (FormatTok->Tok.is(tok::l_brace)) {
  992. if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
  993. addUnwrappedLine();
  994. parseBlock(/*MustBeDeclaration=*/false);
  995. addUnwrappedLine();
  996. } else {
  997. addUnwrappedLine();
  998. ++Line->Level;
  999. parseStructuralElement();
  1000. --Line->Level;
  1001. }
  1002. }
  1003. void UnwrappedLineParser::parseAccessSpecifier() {
  1004. nextToken();
  1005. // Otherwise, we don't know what it is, and we'd better keep the next token.
  1006. if (FormatTok->Tok.is(tok::colon))
  1007. nextToken();
  1008. addUnwrappedLine();
  1009. }
  1010. void UnwrappedLineParser::parseEnum() {
  1011. nextToken();
  1012. // Eat up enum class ...
  1013. if (FormatTok->Tok.is(tok::kw_class) ||
  1014. FormatTok->Tok.is(tok::kw_struct))
  1015. nextToken();
  1016. while (FormatTok->Tok.getIdentifierInfo() ||
  1017. FormatTok->isOneOf(tok::colon, tok::coloncolon)) {
  1018. nextToken();
  1019. // We can have macros or attributes in between 'enum' and the enum name.
  1020. if (FormatTok->Tok.is(tok::l_paren)) {
  1021. parseParens();
  1022. }
  1023. if (FormatTok->Tok.is(tok::identifier))
  1024. nextToken();
  1025. }
  1026. if (FormatTok->Tok.is(tok::l_brace)) {
  1027. FormatTok->BlockKind = BK_Block;
  1028. bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
  1029. if (HasError) {
  1030. if (FormatTok->is(tok::semi))
  1031. nextToken();
  1032. addUnwrappedLine();
  1033. }
  1034. }
  1035. // We fall through to parsing a structural element afterwards, so that in
  1036. // enum A {} n, m;
  1037. // "} n, m;" will end up in one unwrapped line.
  1038. }
  1039. void UnwrappedLineParser::parseRecord() {
  1040. nextToken();
  1041. if (FormatTok->Tok.is(tok::identifier) ||
  1042. FormatTok->Tok.is(tok::kw___attribute) ||
  1043. FormatTok->Tok.is(tok::kw___declspec) ||
  1044. FormatTok->Tok.is(tok::kw_alignas)) {
  1045. nextToken();
  1046. // We can have macros or attributes in between 'class' and the class name.
  1047. if (FormatTok->Tok.is(tok::l_paren)) {
  1048. parseParens();
  1049. }
  1050. // The actual identifier can be a nested name specifier, and in macros
  1051. // it is often token-pasted.
  1052. while (FormatTok->Tok.is(tok::identifier) ||
  1053. FormatTok->Tok.is(tok::coloncolon) ||
  1054. FormatTok->Tok.is(tok::hashhash))
  1055. nextToken();
  1056. // Note that parsing away template declarations here leads to incorrectly
  1057. // accepting function declarations as record declarations.
  1058. // In general, we cannot solve this problem. Consider:
  1059. // class A<int> B() {}
  1060. // which can be a function definition or a class definition when B() is a
  1061. // macro. If we find enough real-world cases where this is a problem, we
  1062. // can parse for the 'template' keyword in the beginning of the statement,
  1063. // and thus rule out the record production in case there is no template
  1064. // (this would still leave us with an ambiguity between template function
  1065. // and class declarations).
  1066. if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
  1067. while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
  1068. if (FormatTok->Tok.is(tok::semi))
  1069. return;
  1070. nextToken();
  1071. }
  1072. }
  1073. }
  1074. if (FormatTok->Tok.is(tok::l_brace)) {
  1075. if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
  1076. Style.BreakBeforeBraces == FormatStyle::BS_Allman)
  1077. addUnwrappedLine();
  1078. parseBlock(/*MustBeDeclaration=*/true);
  1079. }
  1080. // We fall through to parsing a structural element afterwards, so
  1081. // class A {} n, m;
  1082. // will end up in one unwrapped line.
  1083. }
  1084. void UnwrappedLineParser::parseObjCProtocolList() {
  1085. assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
  1086. do
  1087. nextToken();
  1088. while (!eof() && FormatTok->Tok.isNot(tok::greater));
  1089. nextToken(); // Skip '>'.
  1090. }
  1091. void UnwrappedLineParser::parseObjCUntilAtEnd() {
  1092. do {
  1093. if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
  1094. nextToken();
  1095. addUnwrappedLine();
  1096. break;
  1097. }
  1098. if (FormatTok->is(tok::l_brace)) {
  1099. parseBlock(/*MustBeDeclaration=*/false);
  1100. // In ObjC interfaces, nothing should be following the "}".
  1101. addUnwrappedLine();
  1102. } else {
  1103. parseStructuralElement();
  1104. }
  1105. } while (!eof());
  1106. }
  1107. void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
  1108. nextToken();
  1109. nextToken(); // interface name
  1110. // @interface can be followed by either a base class, or a category.
  1111. if (FormatTok->Tok.is(tok::colon)) {
  1112. nextToken();
  1113. nextToken(); // base class name
  1114. } else if (FormatTok->Tok.is(tok::l_paren))
  1115. // Skip category, if present.
  1116. parseParens();
  1117. if (FormatTok->Tok.is(tok::less))
  1118. parseObjCProtocolList();
  1119. // If instance variables are present, keep the '{' on the first line too.
  1120. if (FormatTok->Tok.is(tok::l_brace))
  1121. parseBlock(/*MustBeDeclaration=*/true);
  1122. // With instance variables, this puts '}' on its own line. Without instance
  1123. // variables, this ends the @interface line.
  1124. addUnwrappedLine();
  1125. parseObjCUntilAtEnd();
  1126. }
  1127. void UnwrappedLineParser::parseObjCProtocol() {
  1128. nextToken();
  1129. nextToken(); // protocol name
  1130. if (FormatTok->Tok.is(tok::less))
  1131. parseObjCProtocolList();
  1132. // Check for protocol declaration.
  1133. if (FormatTok->Tok.is(tok::semi)) {
  1134. nextToken();
  1135. return addUnwrappedLine();
  1136. }
  1137. addUnwrappedLine();
  1138. parseObjCUntilAtEnd();
  1139. }
  1140. LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
  1141. StringRef Prefix = "") {
  1142. llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
  1143. << (Line.InPPDirective ? " MACRO" : "") << ": ";
  1144. for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
  1145. E = Line.Tokens.end();
  1146. I != E; ++I) {
  1147. llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
  1148. }
  1149. for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
  1150. E = Line.Tokens.end();
  1151. I != E; ++I) {
  1152. const UnwrappedLineNode &Node = *I;
  1153. for (SmallVectorImpl<UnwrappedLine>::const_iterator
  1154. I = Node.Children.begin(),
  1155. E = Node.Children.end();
  1156. I != E; ++I) {
  1157. printDebugInfo(*I, "\nChild: ");
  1158. }
  1159. }
  1160. llvm::dbgs() << "\n";
  1161. }
  1162. void UnwrappedLineParser::addUnwrappedLine() {
  1163. if (Line->Tokens.empty())
  1164. return;
  1165. DEBUG({
  1166. if (CurrentLines == &Lines)
  1167. printDebugInfo(*Line);
  1168. });
  1169. CurrentLines->push_back(*Line);
  1170. Line->Tokens.clear();
  1171. if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
  1172. for (SmallVectorImpl<UnwrappedLine>::iterator
  1173. I = PreprocessorDirectives.begin(),
  1174. E = PreprocessorDirectives.end();
  1175. I != E; ++I) {
  1176. CurrentLines->push_back(*I);
  1177. }
  1178. PreprocessorDirectives.clear();
  1179. }
  1180. }
  1181. bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
  1182. void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
  1183. bool JustComments = Line->Tokens.empty();
  1184. for (SmallVectorImpl<FormatToken *>::const_iterator
  1185. I = CommentsBeforeNextToken.begin(),
  1186. E = CommentsBeforeNextToken.end();
  1187. I != E; ++I) {
  1188. if ((*I)->NewlinesBefore && JustComments) {
  1189. addUnwrappedLine();
  1190. }
  1191. pushToken(*I);
  1192. }
  1193. if (NewlineBeforeNext && JustComments) {
  1194. addUnwrappedLine();
  1195. }
  1196. CommentsBeforeNextToken.clear();
  1197. }
  1198. void UnwrappedLineParser::nextToken() {
  1199. if (eof())
  1200. return;
  1201. flushComments(FormatTok->NewlinesBefore > 0);
  1202. pushToken(FormatTok);
  1203. readToken();
  1204. }
  1205. void UnwrappedLineParser::readToken() {
  1206. bool CommentsInCurrentLine = true;
  1207. do {
  1208. FormatTok = Tokens->getNextToken();
  1209. while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
  1210. (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
  1211. // If there is an unfinished unwrapped line, we flush the preprocessor
  1212. // directives only after that unwrapped line was finished later.
  1213. bool SwitchToPreprocessorLines =
  1214. !Line->Tokens.empty() && CurrentLines == &Lines;
  1215. ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
  1216. // Comments stored before the preprocessor directive need to be output
  1217. // before the preprocessor directive, at the same level as the
  1218. // preprocessor directive, as we consider them to apply to the directive.
  1219. flushComments(FormatTok->NewlinesBefore > 0);
  1220. parsePPDirective();
  1221. }
  1222. if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
  1223. !Line->InPPDirective) {
  1224. continue;
  1225. }
  1226. if (!FormatTok->Tok.is(tok::comment))
  1227. return;
  1228. if (FormatTok->NewlinesBefore > 0 || FormatTok->IsFirst) {
  1229. CommentsInCurrentLine = false;
  1230. }
  1231. if (CommentsInCurrentLine) {
  1232. pushToken(FormatTok);
  1233. } else {
  1234. CommentsBeforeNextToken.push_back(FormatTok);
  1235. }
  1236. } while (!eof());
  1237. }
  1238. void UnwrappedLineParser::pushToken(FormatToken *Tok) {
  1239. Line->Tokens.push_back(UnwrappedLineNode(Tok));
  1240. if (MustBreakBeforeNextToken) {
  1241. Line->Tokens.back().Tok->MustBreakBefore = true;
  1242. MustBreakBeforeNextToken = false;
  1243. }
  1244. }
  1245. } // end namespace format
  1246. } // end namespace clang