UnwrappedLineParser.cpp 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921
  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. #include "UnwrappedLineParser.h"
  16. #include "llvm/ADT/STLExtras.h"
  17. #include "llvm/Support/Debug.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. #define DEBUG_TYPE "format-parser"
  20. namespace clang {
  21. namespace format {
  22. class FormatTokenSource {
  23. public:
  24. virtual ~FormatTokenSource() {}
  25. virtual FormatToken *getNextToken() = 0;
  26. virtual unsigned getPosition() = 0;
  27. virtual FormatToken *setPosition(unsigned Position) = 0;
  28. };
  29. namespace {
  30. class ScopedDeclarationState {
  31. public:
  32. ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
  33. bool MustBeDeclaration)
  34. : Line(Line), Stack(Stack) {
  35. Line.MustBeDeclaration = MustBeDeclaration;
  36. Stack.push_back(MustBeDeclaration);
  37. }
  38. ~ScopedDeclarationState() {
  39. Stack.pop_back();
  40. if (!Stack.empty())
  41. Line.MustBeDeclaration = Stack.back();
  42. else
  43. Line.MustBeDeclaration = true;
  44. }
  45. private:
  46. UnwrappedLine &Line;
  47. std::vector<bool> &Stack;
  48. };
  49. class ScopedMacroState : public FormatTokenSource {
  50. public:
  51. ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
  52. FormatToken *&ResetToken)
  53. : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
  54. PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
  55. Token(nullptr) {
  56. TokenSource = this;
  57. Line.Level = 0;
  58. Line.InPPDirective = true;
  59. }
  60. ~ScopedMacroState() override {
  61. TokenSource = PreviousTokenSource;
  62. ResetToken = Token;
  63. Line.InPPDirective = false;
  64. Line.Level = PreviousLineLevel;
  65. }
  66. FormatToken *getNextToken() override {
  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. unsigned getPosition() override { return PreviousTokenSource->getPosition(); }
  76. FormatToken *setPosition(unsigned Position) override {
  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. FormatToken *Token;
  98. };
  99. } // end anonymous namespace
  100. class ScopedLineState {
  101. public:
  102. ScopedLineState(UnwrappedLineParser &Parser,
  103. bool SwitchToPreprocessorLines = false)
  104. : Parser(Parser), OriginalLines(Parser.CurrentLines) {
  105. if (SwitchToPreprocessorLines)
  106. Parser.CurrentLines = &Parser.PreprocessorDirectives;
  107. else if (!Parser.Line->Tokens.empty())
  108. Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
  109. PreBlockLine = std::move(Parser.Line);
  110. Parser.Line = llvm::make_unique<UnwrappedLine>();
  111. Parser.Line->Level = PreBlockLine->Level;
  112. Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
  113. }
  114. ~ScopedLineState() {
  115. if (!Parser.Line->Tokens.empty()) {
  116. Parser.addUnwrappedLine();
  117. }
  118. assert(Parser.Line->Tokens.empty());
  119. Parser.Line = std::move(PreBlockLine);
  120. if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
  121. Parser.MustBreakBeforeNextToken = true;
  122. Parser.CurrentLines = OriginalLines;
  123. }
  124. private:
  125. UnwrappedLineParser &Parser;
  126. std::unique_ptr<UnwrappedLine> PreBlockLine;
  127. SmallVectorImpl<UnwrappedLine> *OriginalLines;
  128. };
  129. class CompoundStatementIndenter {
  130. public:
  131. CompoundStatementIndenter(UnwrappedLineParser *Parser,
  132. const FormatStyle &Style, unsigned &LineLevel)
  133. : LineLevel(LineLevel), OldLineLevel(LineLevel) {
  134. if (Style.BraceWrapping.AfterControlStatement)
  135. Parser->addUnwrappedLine();
  136. if (Style.BraceWrapping.IndentBraces)
  137. ++LineLevel;
  138. }
  139. ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
  140. private:
  141. unsigned &LineLevel;
  142. unsigned OldLineLevel;
  143. };
  144. namespace {
  145. class IndexedTokenSource : public FormatTokenSource {
  146. public:
  147. IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
  148. : Tokens(Tokens), Position(-1) {}
  149. FormatToken *getNextToken() override {
  150. ++Position;
  151. return Tokens[Position];
  152. }
  153. unsigned getPosition() override {
  154. assert(Position >= 0);
  155. return Position;
  156. }
  157. FormatToken *setPosition(unsigned P) override {
  158. Position = P;
  159. return Tokens[Position];
  160. }
  161. void reset() { Position = -1; }
  162. private:
  163. ArrayRef<FormatToken *> Tokens;
  164. int Position;
  165. };
  166. } // end anonymous namespace
  167. UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
  168. const AdditionalKeywords &Keywords,
  169. ArrayRef<FormatToken *> Tokens,
  170. UnwrappedLineConsumer &Callback)
  171. : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
  172. CurrentLines(&Lines), Style(Style), Keywords(Keywords), Tokens(nullptr),
  173. Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1) {}
  174. void UnwrappedLineParser::reset() {
  175. PPBranchLevel = -1;
  176. Line.reset(new UnwrappedLine);
  177. CommentsBeforeNextToken.clear();
  178. FormatTok = nullptr;
  179. MustBreakBeforeNextToken = false;
  180. PreprocessorDirectives.clear();
  181. CurrentLines = &Lines;
  182. DeclarationScopeStack.clear();
  183. PPStack.clear();
  184. }
  185. void UnwrappedLineParser::parse() {
  186. IndexedTokenSource TokenSource(AllTokens);
  187. do {
  188. DEBUG(llvm::dbgs() << "----\n");
  189. reset();
  190. Tokens = &TokenSource;
  191. TokenSource.reset();
  192. readToken();
  193. parseFile();
  194. // Create line with eof token.
  195. pushToken(FormatTok);
  196. addUnwrappedLine();
  197. for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
  198. E = Lines.end();
  199. I != E; ++I) {
  200. Callback.consumeUnwrappedLine(*I);
  201. }
  202. Callback.finishRun();
  203. Lines.clear();
  204. while (!PPLevelBranchIndex.empty() &&
  205. PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
  206. PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
  207. PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
  208. }
  209. if (!PPLevelBranchIndex.empty()) {
  210. ++PPLevelBranchIndex.back();
  211. assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
  212. assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
  213. }
  214. } while (!PPLevelBranchIndex.empty());
  215. }
  216. void UnwrappedLineParser::parseFile() {
  217. // The top-level context in a file always has declarations, except for pre-
  218. // processor directives and JavaScript files.
  219. bool MustBeDeclaration =
  220. !Line->InPPDirective && Style.Language != FormatStyle::LK_JavaScript;
  221. ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
  222. MustBeDeclaration);
  223. parseLevel(/*HasOpeningBrace=*/false);
  224. // Make sure to format the remaining tokens.
  225. flushComments(true);
  226. addUnwrappedLine();
  227. }
  228. void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
  229. bool SwitchLabelEncountered = false;
  230. do {
  231. tok::TokenKind kind = FormatTok->Tok.getKind();
  232. if (FormatTok->Type == TT_MacroBlockBegin) {
  233. kind = tok::l_brace;
  234. } else if (FormatTok->Type == TT_MacroBlockEnd) {
  235. kind = tok::r_brace;
  236. }
  237. switch (kind) {
  238. case tok::comment:
  239. nextToken();
  240. addUnwrappedLine();
  241. break;
  242. case tok::l_brace:
  243. // FIXME: Add parameter whether this can happen - if this happens, we must
  244. // be in a non-declaration context.
  245. if (!FormatTok->is(TT_MacroBlockBegin) && tryToParseBracedList())
  246. continue;
  247. parseBlock(/*MustBeDeclaration=*/false);
  248. addUnwrappedLine();
  249. break;
  250. case tok::r_brace:
  251. if (HasOpeningBrace)
  252. return;
  253. nextToken();
  254. addUnwrappedLine();
  255. break;
  256. case tok::kw_default:
  257. case tok::kw_case:
  258. if (!SwitchLabelEncountered &&
  259. (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
  260. ++Line->Level;
  261. SwitchLabelEncountered = true;
  262. parseStructuralElement();
  263. break;
  264. default:
  265. parseStructuralElement();
  266. break;
  267. }
  268. } while (!eof());
  269. }
  270. void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
  271. // We'll parse forward through the tokens until we hit
  272. // a closing brace or eof - note that getNextToken() will
  273. // parse macros, so this will magically work inside macro
  274. // definitions, too.
  275. unsigned StoredPosition = Tokens->getPosition();
  276. FormatToken *Tok = FormatTok;
  277. // Keep a stack of positions of lbrace tokens. We will
  278. // update information about whether an lbrace starts a
  279. // braced init list or a different block during the loop.
  280. SmallVector<FormatToken *, 8> LBraceStack;
  281. assert(Tok->Tok.is(tok::l_brace));
  282. do {
  283. // Get next non-comment token.
  284. FormatToken *NextTok;
  285. unsigned ReadTokens = 0;
  286. do {
  287. NextTok = Tokens->getNextToken();
  288. ++ReadTokens;
  289. } while (NextTok->is(tok::comment));
  290. switch (Tok->Tok.getKind()) {
  291. case tok::l_brace:
  292. Tok->BlockKind = BK_Unknown;
  293. LBraceStack.push_back(Tok);
  294. break;
  295. case tok::r_brace:
  296. if (!LBraceStack.empty()) {
  297. if (LBraceStack.back()->BlockKind == BK_Unknown) {
  298. bool ProbablyBracedList = false;
  299. if (Style.Language == FormatStyle::LK_Proto) {
  300. ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
  301. } else {
  302. // Using OriginalColumn to distinguish between ObjC methods and
  303. // binary operators is a bit hacky.
  304. bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
  305. NextTok->OriginalColumn == 0;
  306. // If there is a comma, semicolon or right paren after the closing
  307. // brace, we assume this is a braced initializer list. Note that
  308. // regardless how we mark inner braces here, we will overwrite the
  309. // BlockKind later if we parse a braced list (where all blocks
  310. // inside are by default braced lists), or when we explicitly detect
  311. // blocks (for example while parsing lambdas).
  312. //
  313. // We exclude + and - as they can be ObjC visibility modifiers.
  314. ProbablyBracedList =
  315. NextTok->isOneOf(tok::comma, tok::period, tok::colon,
  316. tok::r_paren, tok::r_square, tok::l_brace,
  317. tok::l_paren, tok::ellipsis) ||
  318. (NextTok->is(tok::semi) &&
  319. (!ExpectClassBody || LBraceStack.size() != 1)) ||
  320. (NextTok->isBinaryOperator() && !NextIsObjCMethod);
  321. }
  322. if (ProbablyBracedList) {
  323. Tok->BlockKind = BK_BracedInit;
  324. LBraceStack.back()->BlockKind = BK_BracedInit;
  325. } else {
  326. Tok->BlockKind = BK_Block;
  327. LBraceStack.back()->BlockKind = BK_Block;
  328. }
  329. }
  330. LBraceStack.pop_back();
  331. }
  332. break;
  333. case tok::at:
  334. case tok::semi:
  335. case tok::kw_if:
  336. case tok::kw_while:
  337. case tok::kw_for:
  338. case tok::kw_switch:
  339. case tok::kw_try:
  340. case tok::kw___try:
  341. if (!LBraceStack.empty())
  342. LBraceStack.back()->BlockKind = BK_Block;
  343. break;
  344. default:
  345. break;
  346. }
  347. Tok = NextTok;
  348. } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
  349. // Assume other blocks for all unclosed opening braces.
  350. for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
  351. if (LBraceStack[i]->BlockKind == BK_Unknown)
  352. LBraceStack[i]->BlockKind = BK_Block;
  353. }
  354. FormatTok = Tokens->setPosition(StoredPosition);
  355. }
  356. void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
  357. bool MunchSemi) {
  358. assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) &&
  359. "'{' or macro block token expected");
  360. const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin);
  361. FormatTok->BlockKind = BK_Block;
  362. unsigned InitialLevel = Line->Level;
  363. nextToken();
  364. if (MacroBlock && FormatTok->is(tok::l_paren))
  365. parseParens();
  366. addUnwrappedLine();
  367. ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
  368. MustBeDeclaration);
  369. if (AddLevel)
  370. ++Line->Level;
  371. parseLevel(/*HasOpeningBrace=*/true);
  372. if (MacroBlock ? !FormatTok->is(TT_MacroBlockEnd)
  373. : !FormatTok->is(tok::r_brace)) {
  374. Line->Level = InitialLevel;
  375. FormatTok->BlockKind = BK_Block;
  376. return;
  377. }
  378. nextToken(); // Munch the closing brace.
  379. if (MacroBlock && FormatTok->is(tok::l_paren))
  380. parseParens();
  381. if (MunchSemi && FormatTok->Tok.is(tok::semi))
  382. nextToken();
  383. Line->Level = InitialLevel;
  384. }
  385. static bool isGoogScope(const UnwrappedLine &Line) {
  386. // FIXME: Closure-library specific stuff should not be hard-coded but be
  387. // configurable.
  388. if (Line.Tokens.size() < 4)
  389. return false;
  390. auto I = Line.Tokens.begin();
  391. if (I->Tok->TokenText != "goog")
  392. return false;
  393. ++I;
  394. if (I->Tok->isNot(tok::period))
  395. return false;
  396. ++I;
  397. if (I->Tok->TokenText != "scope")
  398. return false;
  399. ++I;
  400. return I->Tok->is(tok::l_paren);
  401. }
  402. static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
  403. const FormatToken &InitialToken) {
  404. if (InitialToken.is(tok::kw_namespace))
  405. return Style.BraceWrapping.AfterNamespace;
  406. if (InitialToken.is(tok::kw_class))
  407. return Style.BraceWrapping.AfterClass;
  408. if (InitialToken.is(tok::kw_union))
  409. return Style.BraceWrapping.AfterUnion;
  410. if (InitialToken.is(tok::kw_struct))
  411. return Style.BraceWrapping.AfterStruct;
  412. return false;
  413. }
  414. void UnwrappedLineParser::parseChildBlock() {
  415. FormatTok->BlockKind = BK_Block;
  416. nextToken();
  417. {
  418. bool GoogScope =
  419. Style.Language == FormatStyle::LK_JavaScript && isGoogScope(*Line);
  420. ScopedLineState LineState(*this);
  421. ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
  422. /*MustBeDeclaration=*/false);
  423. Line->Level += GoogScope ? 0 : 1;
  424. parseLevel(/*HasOpeningBrace=*/true);
  425. flushComments(isOnNewLine(*FormatTok));
  426. Line->Level -= GoogScope ? 0 : 1;
  427. }
  428. nextToken();
  429. }
  430. void UnwrappedLineParser::parsePPDirective() {
  431. assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
  432. ScopedMacroState MacroState(*Line, Tokens, FormatTok);
  433. nextToken();
  434. if (!FormatTok->Tok.getIdentifierInfo()) {
  435. parsePPUnknown();
  436. return;
  437. }
  438. switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
  439. case tok::pp_define:
  440. parsePPDefine();
  441. return;
  442. case tok::pp_if:
  443. parsePPIf(/*IfDef=*/false);
  444. break;
  445. case tok::pp_ifdef:
  446. case tok::pp_ifndef:
  447. parsePPIf(/*IfDef=*/true);
  448. break;
  449. case tok::pp_else:
  450. parsePPElse();
  451. break;
  452. case tok::pp_elif:
  453. parsePPElIf();
  454. break;
  455. case tok::pp_endif:
  456. parsePPEndIf();
  457. break;
  458. default:
  459. parsePPUnknown();
  460. break;
  461. }
  462. }
  463. void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
  464. if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable))
  465. PPStack.push_back(PP_Unreachable);
  466. else
  467. PPStack.push_back(PP_Conditional);
  468. }
  469. void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
  470. ++PPBranchLevel;
  471. assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
  472. if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
  473. PPLevelBranchIndex.push_back(0);
  474. PPLevelBranchCount.push_back(0);
  475. }
  476. PPChainBranchIndex.push(0);
  477. bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
  478. conditionalCompilationCondition(Unreachable || Skip);
  479. }
  480. void UnwrappedLineParser::conditionalCompilationAlternative() {
  481. if (!PPStack.empty())
  482. PPStack.pop_back();
  483. assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
  484. if (!PPChainBranchIndex.empty())
  485. ++PPChainBranchIndex.top();
  486. conditionalCompilationCondition(
  487. PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
  488. PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
  489. }
  490. void UnwrappedLineParser::conditionalCompilationEnd() {
  491. assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
  492. if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
  493. if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
  494. PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
  495. }
  496. }
  497. // Guard against #endif's without #if.
  498. if (PPBranchLevel > 0)
  499. --PPBranchLevel;
  500. if (!PPChainBranchIndex.empty())
  501. PPChainBranchIndex.pop();
  502. if (!PPStack.empty())
  503. PPStack.pop_back();
  504. }
  505. void UnwrappedLineParser::parsePPIf(bool IfDef) {
  506. nextToken();
  507. bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
  508. FormatTok->Tok.getLiteralData() != nullptr &&
  509. StringRef(FormatTok->Tok.getLiteralData(),
  510. FormatTok->Tok.getLength()) == "0") ||
  511. FormatTok->Tok.is(tok::kw_false);
  512. conditionalCompilationStart(!IfDef && IsLiteralFalse);
  513. parsePPUnknown();
  514. }
  515. void UnwrappedLineParser::parsePPElse() {
  516. conditionalCompilationAlternative();
  517. parsePPUnknown();
  518. }
  519. void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
  520. void UnwrappedLineParser::parsePPEndIf() {
  521. conditionalCompilationEnd();
  522. parsePPUnknown();
  523. }
  524. void UnwrappedLineParser::parsePPDefine() {
  525. nextToken();
  526. if (FormatTok->Tok.getKind() != tok::identifier) {
  527. parsePPUnknown();
  528. return;
  529. }
  530. nextToken();
  531. if (FormatTok->Tok.getKind() == tok::l_paren &&
  532. FormatTok->WhitespaceRange.getBegin() ==
  533. FormatTok->WhitespaceRange.getEnd()) {
  534. parseParens();
  535. }
  536. addUnwrappedLine();
  537. Line->Level = 1;
  538. // Errors during a preprocessor directive can only affect the layout of the
  539. // preprocessor directive, and thus we ignore them. An alternative approach
  540. // would be to use the same approach we use on the file level (no
  541. // re-indentation if there was a structural error) within the macro
  542. // definition.
  543. parseFile();
  544. }
  545. void UnwrappedLineParser::parsePPUnknown() {
  546. do {
  547. nextToken();
  548. } while (!eof());
  549. addUnwrappedLine();
  550. }
  551. // Here we blacklist certain tokens that are not usually the first token in an
  552. // unwrapped line. This is used in attempt to distinguish macro calls without
  553. // trailing semicolons from other constructs split to several lines.
  554. static bool tokenCanStartNewLine(const clang::Token &Tok) {
  555. // Semicolon can be a null-statement, l_square can be a start of a macro or
  556. // a C++11 attribute, but this doesn't seem to be common.
  557. return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
  558. Tok.isNot(tok::l_square) &&
  559. // Tokens that can only be used as binary operators and a part of
  560. // overloaded operator names.
  561. Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
  562. Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
  563. Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
  564. Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
  565. Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
  566. Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
  567. Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
  568. Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
  569. Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
  570. Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
  571. Tok.isNot(tok::lesslessequal) &&
  572. // Colon is used in labels, base class lists, initializer lists,
  573. // range-based for loops, ternary operator, but should never be the
  574. // first token in an unwrapped line.
  575. Tok.isNot(tok::colon) &&
  576. // 'noexcept' is a trailing annotation.
  577. Tok.isNot(tok::kw_noexcept);
  578. }
  579. void UnwrappedLineParser::parseStructuralElement() {
  580. assert(!FormatTok->Tok.is(tok::l_brace));
  581. switch (FormatTok->Tok.getKind()) {
  582. case tok::at:
  583. nextToken();
  584. if (FormatTok->Tok.is(tok::l_brace)) {
  585. parseBracedList();
  586. break;
  587. }
  588. switch (FormatTok->Tok.getObjCKeywordID()) {
  589. case tok::objc_public:
  590. case tok::objc_protected:
  591. case tok::objc_package:
  592. case tok::objc_private:
  593. return parseAccessSpecifier();
  594. case tok::objc_interface:
  595. case tok::objc_implementation:
  596. return parseObjCInterfaceOrImplementation();
  597. case tok::objc_protocol:
  598. return parseObjCProtocol();
  599. case tok::objc_end:
  600. return; // Handled by the caller.
  601. case tok::objc_optional:
  602. case tok::objc_required:
  603. nextToken();
  604. addUnwrappedLine();
  605. return;
  606. case tok::objc_autoreleasepool:
  607. nextToken();
  608. if (FormatTok->Tok.is(tok::l_brace)) {
  609. if (Style.BraceWrapping.AfterObjCDeclaration)
  610. addUnwrappedLine();
  611. parseBlock(/*MustBeDeclaration=*/false);
  612. }
  613. addUnwrappedLine();
  614. return;
  615. case tok::objc_try:
  616. // This branch isn't strictly necessary (the kw_try case below would
  617. // do this too after the tok::at is parsed above). But be explicit.
  618. parseTryCatch();
  619. return;
  620. default:
  621. break;
  622. }
  623. break;
  624. case tok::kw_asm:
  625. nextToken();
  626. if (FormatTok->is(tok::l_brace)) {
  627. FormatTok->Type = TT_InlineASMBrace;
  628. nextToken();
  629. while (FormatTok && FormatTok->isNot(tok::eof)) {
  630. if (FormatTok->is(tok::r_brace)) {
  631. FormatTok->Type = TT_InlineASMBrace;
  632. nextToken();
  633. addUnwrappedLine();
  634. break;
  635. }
  636. FormatTok->Finalized = true;
  637. nextToken();
  638. }
  639. }
  640. break;
  641. case tok::kw_namespace:
  642. parseNamespace();
  643. return;
  644. case tok::kw_inline:
  645. nextToken();
  646. if (FormatTok->Tok.is(tok::kw_namespace)) {
  647. parseNamespace();
  648. return;
  649. }
  650. break;
  651. case tok::kw_public:
  652. case tok::kw_protected:
  653. case tok::kw_private:
  654. if (Style.Language == FormatStyle::LK_Java ||
  655. Style.Language == FormatStyle::LK_JavaScript)
  656. nextToken();
  657. else
  658. parseAccessSpecifier();
  659. return;
  660. case tok::kw_if:
  661. parseIfThenElse();
  662. return;
  663. case tok::kw_for:
  664. case tok::kw_while:
  665. parseForOrWhileLoop();
  666. return;
  667. case tok::kw_do:
  668. parseDoWhile();
  669. return;
  670. case tok::kw_switch:
  671. parseSwitch();
  672. return;
  673. case tok::kw_default:
  674. nextToken();
  675. parseLabel();
  676. return;
  677. case tok::kw_case:
  678. parseCaseLabel();
  679. return;
  680. case tok::kw_try:
  681. case tok::kw___try:
  682. parseTryCatch();
  683. return;
  684. case tok::kw_extern:
  685. nextToken();
  686. if (FormatTok->Tok.is(tok::string_literal)) {
  687. nextToken();
  688. if (FormatTok->Tok.is(tok::l_brace)) {
  689. parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
  690. addUnwrappedLine();
  691. return;
  692. }
  693. }
  694. break;
  695. case tok::kw_export:
  696. if (Style.Language == FormatStyle::LK_JavaScript) {
  697. parseJavaScriptEs6ImportExport();
  698. return;
  699. }
  700. break;
  701. case tok::identifier:
  702. if (FormatTok->is(TT_ForEachMacro)) {
  703. parseForOrWhileLoop();
  704. return;
  705. }
  706. if (FormatTok->is(TT_MacroBlockBegin)) {
  707. parseBlock(/*MustBeDeclaration=*/false, /*AddLevel=*/true,
  708. /*MunchSemi=*/false);
  709. return;
  710. }
  711. if (Style.Language == FormatStyle::LK_JavaScript &&
  712. FormatTok->is(Keywords.kw_import)) {
  713. parseJavaScriptEs6ImportExport();
  714. return;
  715. }
  716. if (FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals,
  717. Keywords.kw_slots, Keywords.kw_qslots)) {
  718. nextToken();
  719. if (FormatTok->is(tok::colon)) {
  720. nextToken();
  721. addUnwrappedLine();
  722. }
  723. return;
  724. }
  725. // In all other cases, parse the declaration.
  726. break;
  727. default:
  728. break;
  729. }
  730. do {
  731. switch (FormatTok->Tok.getKind()) {
  732. case tok::at:
  733. nextToken();
  734. if (FormatTok->Tok.is(tok::l_brace))
  735. parseBracedList();
  736. break;
  737. case tok::kw_enum:
  738. // parseEnum falls through and does not yet add an unwrapped line as an
  739. // enum definition can start a structural element.
  740. parseEnum();
  741. // This only applies for C++.
  742. if (Style.Language != FormatStyle::LK_Cpp) {
  743. addUnwrappedLine();
  744. return;
  745. }
  746. break;
  747. case tok::kw_typedef:
  748. nextToken();
  749. if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
  750. Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
  751. parseEnum();
  752. break;
  753. case tok::kw_struct:
  754. case tok::kw_union:
  755. case tok::kw_class:
  756. // parseRecord falls through and does not yet add an unwrapped line as a
  757. // record declaration or definition can start a structural element.
  758. parseRecord();
  759. // This does not apply for Java and JavaScript.
  760. if (Style.Language == FormatStyle::LK_Java ||
  761. Style.Language == FormatStyle::LK_JavaScript) {
  762. addUnwrappedLine();
  763. return;
  764. }
  765. break;
  766. case tok::period:
  767. nextToken();
  768. // In Java, classes have an implicit static member "class".
  769. if (Style.Language == FormatStyle::LK_Java && FormatTok &&
  770. FormatTok->is(tok::kw_class))
  771. nextToken();
  772. if (Style.Language == FormatStyle::LK_JavaScript && FormatTok &&
  773. FormatTok->Tok.getIdentifierInfo())
  774. // JavaScript only has pseudo keywords, all keywords are allowed to
  775. // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6
  776. nextToken();
  777. break;
  778. case tok::semi:
  779. nextToken();
  780. addUnwrappedLine();
  781. return;
  782. case tok::r_brace:
  783. addUnwrappedLine();
  784. return;
  785. case tok::l_paren:
  786. parseParens();
  787. break;
  788. case tok::kw_operator:
  789. nextToken();
  790. if (FormatTok->isBinaryOperator())
  791. nextToken();
  792. break;
  793. case tok::caret:
  794. nextToken();
  795. if (FormatTok->Tok.isAnyIdentifier() ||
  796. FormatTok->isSimpleTypeSpecifier())
  797. nextToken();
  798. if (FormatTok->is(tok::l_paren))
  799. parseParens();
  800. if (FormatTok->is(tok::l_brace))
  801. parseChildBlock();
  802. break;
  803. case tok::l_brace:
  804. if (!tryToParseBracedList()) {
  805. // A block outside of parentheses must be the last part of a
  806. // structural element.
  807. // FIXME: Figure out cases where this is not true, and add projections
  808. // for them (the one we know is missing are lambdas).
  809. if (Style.BraceWrapping.AfterFunction)
  810. addUnwrappedLine();
  811. FormatTok->Type = TT_FunctionLBrace;
  812. parseBlock(/*MustBeDeclaration=*/false);
  813. addUnwrappedLine();
  814. return;
  815. }
  816. // Otherwise this was a braced init list, and the structural
  817. // element continues.
  818. break;
  819. case tok::kw_try:
  820. // We arrive here when parsing function-try blocks.
  821. parseTryCatch();
  822. return;
  823. case tok::identifier: {
  824. if (FormatTok->is(TT_MacroBlockEnd)) {
  825. addUnwrappedLine();
  826. return;
  827. }
  828. // Parse function literal unless 'function' is the first token in a line
  829. // in which case this should be treated as a free-standing function.
  830. if (Style.Language == FormatStyle::LK_JavaScript &&
  831. FormatTok->is(Keywords.kw_function) && Line->Tokens.size() > 0) {
  832. tryToParseJSFunction();
  833. break;
  834. }
  835. if ((Style.Language == FormatStyle::LK_JavaScript ||
  836. Style.Language == FormatStyle::LK_Java) &&
  837. FormatTok->is(Keywords.kw_interface)) {
  838. parseRecord();
  839. addUnwrappedLine();
  840. return;
  841. }
  842. StringRef Text = FormatTok->TokenText;
  843. nextToken();
  844. if (Line->Tokens.size() == 1 &&
  845. // JS doesn't have macros, and within classes colons indicate fields,
  846. // not labels.
  847. Style.Language != FormatStyle::LK_JavaScript) {
  848. if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) {
  849. parseLabel();
  850. return;
  851. }
  852. // Recognize function-like macro usages without trailing semicolon as
  853. // well as free-standing macros like Q_OBJECT.
  854. bool FunctionLike = FormatTok->is(tok::l_paren);
  855. if (FunctionLike)
  856. parseParens();
  857. bool FollowedByNewline =
  858. CommentsBeforeNextToken.empty()
  859. ? FormatTok->NewlinesBefore > 0
  860. : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
  861. if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) &&
  862. tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
  863. addUnwrappedLine();
  864. return;
  865. }
  866. }
  867. break;
  868. }
  869. case tok::equal:
  870. // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType
  871. // TT_JsFatArrow. The always start an expression or a child block if
  872. // followed by a curly.
  873. if (FormatTok->is(TT_JsFatArrow)) {
  874. nextToken();
  875. if (FormatTok->is(tok::l_brace))
  876. parseChildBlock();
  877. break;
  878. }
  879. nextToken();
  880. if (FormatTok->Tok.is(tok::l_brace)) {
  881. parseBracedList();
  882. }
  883. break;
  884. case tok::l_square:
  885. parseSquare();
  886. break;
  887. case tok::kw_new:
  888. parseNew();
  889. break;
  890. default:
  891. nextToken();
  892. break;
  893. }
  894. } while (!eof());
  895. }
  896. bool UnwrappedLineParser::tryToParseLambda() {
  897. if (Style.Language != FormatStyle::LK_Cpp) {
  898. nextToken();
  899. return false;
  900. }
  901. // FIXME: This is a dirty way to access the previous token. Find a better
  902. // solution.
  903. if (!Line->Tokens.empty() &&
  904. (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator,
  905. tok::kw_new, tok::kw_delete) ||
  906. Line->Tokens.back().Tok->closesScope() ||
  907. Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
  908. nextToken();
  909. return false;
  910. }
  911. assert(FormatTok->is(tok::l_square));
  912. FormatToken &LSquare = *FormatTok;
  913. if (!tryToParseLambdaIntroducer())
  914. return false;
  915. while (FormatTok->isNot(tok::l_brace)) {
  916. if (FormatTok->isSimpleTypeSpecifier()) {
  917. nextToken();
  918. continue;
  919. }
  920. switch (FormatTok->Tok.getKind()) {
  921. case tok::l_brace:
  922. break;
  923. case tok::l_paren:
  924. parseParens();
  925. break;
  926. case tok::amp:
  927. case tok::star:
  928. case tok::kw_const:
  929. case tok::comma:
  930. case tok::less:
  931. case tok::greater:
  932. case tok::identifier:
  933. case tok::numeric_constant:
  934. case tok::coloncolon:
  935. case tok::kw_mutable:
  936. nextToken();
  937. break;
  938. case tok::arrow:
  939. FormatTok->Type = TT_LambdaArrow;
  940. nextToken();
  941. break;
  942. default:
  943. return true;
  944. }
  945. }
  946. LSquare.Type = TT_LambdaLSquare;
  947. parseChildBlock();
  948. return true;
  949. }
  950. bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
  951. nextToken();
  952. if (FormatTok->is(tok::equal)) {
  953. nextToken();
  954. if (FormatTok->is(tok::r_square)) {
  955. nextToken();
  956. return true;
  957. }
  958. if (FormatTok->isNot(tok::comma))
  959. return false;
  960. nextToken();
  961. } else if (FormatTok->is(tok::amp)) {
  962. nextToken();
  963. if (FormatTok->is(tok::r_square)) {
  964. nextToken();
  965. return true;
  966. }
  967. if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
  968. return false;
  969. }
  970. if (FormatTok->is(tok::comma))
  971. nextToken();
  972. } else if (FormatTok->is(tok::r_square)) {
  973. nextToken();
  974. return true;
  975. }
  976. do {
  977. if (FormatTok->is(tok::amp))
  978. nextToken();
  979. if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
  980. return false;
  981. nextToken();
  982. if (FormatTok->is(tok::ellipsis))
  983. nextToken();
  984. if (FormatTok->is(tok::comma)) {
  985. nextToken();
  986. } else if (FormatTok->is(tok::r_square)) {
  987. nextToken();
  988. return true;
  989. } else {
  990. return false;
  991. }
  992. } while (!eof());
  993. return false;
  994. }
  995. void UnwrappedLineParser::tryToParseJSFunction() {
  996. nextToken();
  997. // Consume function name.
  998. if (FormatTok->is(tok::identifier))
  999. nextToken();
  1000. if (FormatTok->isNot(tok::l_paren))
  1001. return;
  1002. // Parse formal parameter list.
  1003. parseParens();
  1004. if (FormatTok->is(tok::colon)) {
  1005. // Parse a type definition.
  1006. nextToken();
  1007. // Eat the type declaration. For braced inline object types, balance braces,
  1008. // otherwise just parse until finding an l_brace for the function body.
  1009. if (FormatTok->is(tok::l_brace))
  1010. tryToParseBracedList();
  1011. else
  1012. while (FormatTok->isNot(tok::l_brace) && !eof())
  1013. nextToken();
  1014. }
  1015. parseChildBlock();
  1016. }
  1017. bool UnwrappedLineParser::tryToParseBracedList() {
  1018. if (FormatTok->BlockKind == BK_Unknown)
  1019. calculateBraceTypes();
  1020. assert(FormatTok->BlockKind != BK_Unknown);
  1021. if (FormatTok->BlockKind == BK_Block)
  1022. return false;
  1023. parseBracedList();
  1024. return true;
  1025. }
  1026. bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
  1027. bool HasError = false;
  1028. nextToken();
  1029. // FIXME: Once we have an expression parser in the UnwrappedLineParser,
  1030. // replace this by using parseAssigmentExpression() inside.
  1031. do {
  1032. if (Style.Language == FormatStyle::LK_JavaScript) {
  1033. if (FormatTok->is(Keywords.kw_function)) {
  1034. tryToParseJSFunction();
  1035. continue;
  1036. }
  1037. if (FormatTok->is(TT_JsFatArrow)) {
  1038. nextToken();
  1039. // Fat arrows can be followed by simple expressions or by child blocks
  1040. // in curly braces.
  1041. if (FormatTok->is(tok::l_brace)) {
  1042. parseChildBlock();
  1043. continue;
  1044. }
  1045. }
  1046. }
  1047. switch (FormatTok->Tok.getKind()) {
  1048. case tok::caret:
  1049. nextToken();
  1050. if (FormatTok->is(tok::l_brace)) {
  1051. parseChildBlock();
  1052. }
  1053. break;
  1054. case tok::l_square:
  1055. tryToParseLambda();
  1056. break;
  1057. case tok::l_brace:
  1058. // Assume there are no blocks inside a braced init list apart
  1059. // from the ones we explicitly parse out (like lambdas).
  1060. FormatTok->BlockKind = BK_BracedInit;
  1061. parseBracedList();
  1062. break;
  1063. case tok::l_paren:
  1064. parseParens();
  1065. // JavaScript can just have free standing methods and getters/setters in
  1066. // object literals. Detect them by a "{" following ")".
  1067. if (Style.Language == FormatStyle::LK_JavaScript) {
  1068. if (FormatTok->is(tok::l_brace))
  1069. parseChildBlock();
  1070. break;
  1071. }
  1072. break;
  1073. case tok::r_brace:
  1074. nextToken();
  1075. return !HasError;
  1076. case tok::semi:
  1077. HasError = true;
  1078. if (!ContinueOnSemicolons)
  1079. return !HasError;
  1080. nextToken();
  1081. break;
  1082. case tok::comma:
  1083. nextToken();
  1084. break;
  1085. default:
  1086. nextToken();
  1087. break;
  1088. }
  1089. } while (!eof());
  1090. return false;
  1091. }
  1092. void UnwrappedLineParser::parseParens() {
  1093. assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
  1094. nextToken();
  1095. do {
  1096. switch (FormatTok->Tok.getKind()) {
  1097. case tok::l_paren:
  1098. parseParens();
  1099. if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
  1100. parseChildBlock();
  1101. break;
  1102. case tok::r_paren:
  1103. nextToken();
  1104. return;
  1105. case tok::r_brace:
  1106. // A "}" inside parenthesis is an error if there wasn't a matching "{".
  1107. return;
  1108. case tok::l_square:
  1109. tryToParseLambda();
  1110. break;
  1111. case tok::l_brace:
  1112. if (!tryToParseBracedList())
  1113. parseChildBlock();
  1114. break;
  1115. case tok::at:
  1116. nextToken();
  1117. if (FormatTok->Tok.is(tok::l_brace))
  1118. parseBracedList();
  1119. break;
  1120. case tok::identifier:
  1121. if (Style.Language == FormatStyle::LK_JavaScript &&
  1122. FormatTok->is(Keywords.kw_function))
  1123. tryToParseJSFunction();
  1124. else
  1125. nextToken();
  1126. break;
  1127. default:
  1128. nextToken();
  1129. break;
  1130. }
  1131. } while (!eof());
  1132. }
  1133. void UnwrappedLineParser::parseSquare() {
  1134. assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
  1135. if (tryToParseLambda())
  1136. return;
  1137. do {
  1138. switch (FormatTok->Tok.getKind()) {
  1139. case tok::l_paren:
  1140. parseParens();
  1141. break;
  1142. case tok::r_square:
  1143. nextToken();
  1144. return;
  1145. case tok::r_brace:
  1146. // A "}" inside parenthesis is an error if there wasn't a matching "{".
  1147. return;
  1148. case tok::l_square:
  1149. parseSquare();
  1150. break;
  1151. case tok::l_brace: {
  1152. if (!tryToParseBracedList())
  1153. parseChildBlock();
  1154. break;
  1155. }
  1156. case tok::at:
  1157. nextToken();
  1158. if (FormatTok->Tok.is(tok::l_brace))
  1159. parseBracedList();
  1160. break;
  1161. default:
  1162. nextToken();
  1163. break;
  1164. }
  1165. } while (!eof());
  1166. }
  1167. void UnwrappedLineParser::parseIfThenElse() {
  1168. assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
  1169. nextToken();
  1170. if (FormatTok->Tok.is(tok::l_paren))
  1171. parseParens();
  1172. bool NeedsUnwrappedLine = false;
  1173. if (FormatTok->Tok.is(tok::l_brace)) {
  1174. CompoundStatementIndenter Indenter(this, Style, Line->Level);
  1175. parseBlock(/*MustBeDeclaration=*/false);
  1176. if (Style.BraceWrapping.BeforeElse)
  1177. addUnwrappedLine();
  1178. else
  1179. NeedsUnwrappedLine = true;
  1180. } else {
  1181. addUnwrappedLine();
  1182. ++Line->Level;
  1183. parseStructuralElement();
  1184. --Line->Level;
  1185. }
  1186. if (FormatTok->Tok.is(tok::kw_else)) {
  1187. nextToken();
  1188. if (FormatTok->Tok.is(tok::l_brace)) {
  1189. CompoundStatementIndenter Indenter(this, Style, Line->Level);
  1190. parseBlock(/*MustBeDeclaration=*/false);
  1191. addUnwrappedLine();
  1192. } else if (FormatTok->Tok.is(tok::kw_if)) {
  1193. parseIfThenElse();
  1194. } else {
  1195. addUnwrappedLine();
  1196. ++Line->Level;
  1197. parseStructuralElement();
  1198. --Line->Level;
  1199. }
  1200. } else if (NeedsUnwrappedLine) {
  1201. addUnwrappedLine();
  1202. }
  1203. }
  1204. void UnwrappedLineParser::parseTryCatch() {
  1205. assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
  1206. nextToken();
  1207. bool NeedsUnwrappedLine = false;
  1208. if (FormatTok->is(tok::colon)) {
  1209. // We are in a function try block, what comes is an initializer list.
  1210. nextToken();
  1211. while (FormatTok->is(tok::identifier)) {
  1212. nextToken();
  1213. if (FormatTok->is(tok::l_paren))
  1214. parseParens();
  1215. if (FormatTok->is(tok::comma))
  1216. nextToken();
  1217. }
  1218. }
  1219. // Parse try with resource.
  1220. if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
  1221. parseParens();
  1222. }
  1223. if (FormatTok->is(tok::l_brace)) {
  1224. CompoundStatementIndenter Indenter(this, Style, Line->Level);
  1225. parseBlock(/*MustBeDeclaration=*/false);
  1226. if (Style.BraceWrapping.BeforeCatch) {
  1227. addUnwrappedLine();
  1228. } else {
  1229. NeedsUnwrappedLine = true;
  1230. }
  1231. } else if (!FormatTok->is(tok::kw_catch)) {
  1232. // The C++ standard requires a compound-statement after a try.
  1233. // If there's none, we try to assume there's a structuralElement
  1234. // and try to continue.
  1235. addUnwrappedLine();
  1236. ++Line->Level;
  1237. parseStructuralElement();
  1238. --Line->Level;
  1239. }
  1240. while (1) {
  1241. if (FormatTok->is(tok::at))
  1242. nextToken();
  1243. if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
  1244. tok::kw___finally) ||
  1245. ((Style.Language == FormatStyle::LK_Java ||
  1246. Style.Language == FormatStyle::LK_JavaScript) &&
  1247. FormatTok->is(Keywords.kw_finally)) ||
  1248. (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
  1249. FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
  1250. break;
  1251. nextToken();
  1252. while (FormatTok->isNot(tok::l_brace)) {
  1253. if (FormatTok->is(tok::l_paren)) {
  1254. parseParens();
  1255. continue;
  1256. }
  1257. if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
  1258. return;
  1259. nextToken();
  1260. }
  1261. NeedsUnwrappedLine = false;
  1262. CompoundStatementIndenter Indenter(this, Style, Line->Level);
  1263. parseBlock(/*MustBeDeclaration=*/false);
  1264. if (Style.BraceWrapping.BeforeCatch)
  1265. addUnwrappedLine();
  1266. else
  1267. NeedsUnwrappedLine = true;
  1268. }
  1269. if (NeedsUnwrappedLine)
  1270. addUnwrappedLine();
  1271. }
  1272. void UnwrappedLineParser::parseNamespace() {
  1273. assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
  1274. const FormatToken &InitialToken = *FormatTok;
  1275. nextToken();
  1276. while (FormatTok->isOneOf(tok::identifier, tok::coloncolon))
  1277. nextToken();
  1278. if (FormatTok->Tok.is(tok::l_brace)) {
  1279. if (ShouldBreakBeforeBrace(Style, InitialToken))
  1280. addUnwrappedLine();
  1281. bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
  1282. (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
  1283. DeclarationScopeStack.size() > 1);
  1284. parseBlock(/*MustBeDeclaration=*/true, AddLevel);
  1285. // Munch the semicolon after a namespace. This is more common than one would
  1286. // think. Puttin the semicolon into its own line is very ugly.
  1287. if (FormatTok->Tok.is(tok::semi))
  1288. nextToken();
  1289. addUnwrappedLine();
  1290. }
  1291. // FIXME: Add error handling.
  1292. }
  1293. void UnwrappedLineParser::parseNew() {
  1294. assert(FormatTok->is(tok::kw_new) && "'new' expected");
  1295. nextToken();
  1296. if (Style.Language != FormatStyle::LK_Java)
  1297. return;
  1298. // In Java, we can parse everything up to the parens, which aren't optional.
  1299. do {
  1300. // There should not be a ;, { or } before the new's open paren.
  1301. if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
  1302. return;
  1303. // Consume the parens.
  1304. if (FormatTok->is(tok::l_paren)) {
  1305. parseParens();
  1306. // If there is a class body of an anonymous class, consume that as child.
  1307. if (FormatTok->is(tok::l_brace))
  1308. parseChildBlock();
  1309. return;
  1310. }
  1311. nextToken();
  1312. } while (!eof());
  1313. }
  1314. void UnwrappedLineParser::parseForOrWhileLoop() {
  1315. assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
  1316. "'for', 'while' or foreach macro expected");
  1317. nextToken();
  1318. if (FormatTok->Tok.is(tok::l_paren))
  1319. parseParens();
  1320. if (FormatTok->Tok.is(tok::l_brace)) {
  1321. CompoundStatementIndenter Indenter(this, Style, Line->Level);
  1322. parseBlock(/*MustBeDeclaration=*/false);
  1323. addUnwrappedLine();
  1324. } else {
  1325. addUnwrappedLine();
  1326. ++Line->Level;
  1327. parseStructuralElement();
  1328. --Line->Level;
  1329. }
  1330. }
  1331. void UnwrappedLineParser::parseDoWhile() {
  1332. assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
  1333. nextToken();
  1334. if (FormatTok->Tok.is(tok::l_brace)) {
  1335. CompoundStatementIndenter Indenter(this, Style, Line->Level);
  1336. parseBlock(/*MustBeDeclaration=*/false);
  1337. if (Style.BraceWrapping.IndentBraces)
  1338. addUnwrappedLine();
  1339. } else {
  1340. addUnwrappedLine();
  1341. ++Line->Level;
  1342. parseStructuralElement();
  1343. --Line->Level;
  1344. }
  1345. // FIXME: Add error handling.
  1346. if (!FormatTok->Tok.is(tok::kw_while)) {
  1347. addUnwrappedLine();
  1348. return;
  1349. }
  1350. nextToken();
  1351. parseStructuralElement();
  1352. }
  1353. void UnwrappedLineParser::parseLabel() {
  1354. nextToken();
  1355. unsigned OldLineLevel = Line->Level;
  1356. if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
  1357. --Line->Level;
  1358. if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
  1359. CompoundStatementIndenter Indenter(this, Style, Line->Level);
  1360. parseBlock(/*MustBeDeclaration=*/false);
  1361. if (FormatTok->Tok.is(tok::kw_break)) {
  1362. if (Style.BraceWrapping.AfterControlStatement)
  1363. addUnwrappedLine();
  1364. parseStructuralElement();
  1365. }
  1366. addUnwrappedLine();
  1367. } else {
  1368. if (FormatTok->is(tok::semi))
  1369. nextToken();
  1370. addUnwrappedLine();
  1371. }
  1372. Line->Level = OldLineLevel;
  1373. }
  1374. void UnwrappedLineParser::parseCaseLabel() {
  1375. assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
  1376. // FIXME: fix handling of complex expressions here.
  1377. do {
  1378. nextToken();
  1379. } while (!eof() && !FormatTok->Tok.is(tok::colon));
  1380. parseLabel();
  1381. }
  1382. void UnwrappedLineParser::parseSwitch() {
  1383. assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
  1384. nextToken();
  1385. if (FormatTok->Tok.is(tok::l_paren))
  1386. parseParens();
  1387. if (FormatTok->Tok.is(tok::l_brace)) {
  1388. CompoundStatementIndenter Indenter(this, Style, Line->Level);
  1389. parseBlock(/*MustBeDeclaration=*/false);
  1390. addUnwrappedLine();
  1391. } else {
  1392. addUnwrappedLine();
  1393. ++Line->Level;
  1394. parseStructuralElement();
  1395. --Line->Level;
  1396. }
  1397. }
  1398. void UnwrappedLineParser::parseAccessSpecifier() {
  1399. nextToken();
  1400. // Understand Qt's slots.
  1401. if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
  1402. nextToken();
  1403. // Otherwise, we don't know what it is, and we'd better keep the next token.
  1404. if (FormatTok->Tok.is(tok::colon))
  1405. nextToken();
  1406. addUnwrappedLine();
  1407. }
  1408. void UnwrappedLineParser::parseEnum() {
  1409. // Won't be 'enum' for NS_ENUMs.
  1410. if (FormatTok->Tok.is(tok::kw_enum))
  1411. nextToken();
  1412. // Eat up enum class ...
  1413. if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
  1414. nextToken();
  1415. while (FormatTok->Tok.getIdentifierInfo() ||
  1416. FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
  1417. tok::greater, tok::comma, tok::question)) {
  1418. nextToken();
  1419. // We can have macros or attributes in between 'enum' and the enum name.
  1420. if (FormatTok->is(tok::l_paren))
  1421. parseParens();
  1422. if (FormatTok->is(tok::identifier)) {
  1423. nextToken();
  1424. // If there are two identifiers in a row, this is likely an elaborate
  1425. // return type. In Java, this can be "implements", etc.
  1426. if (Style.Language == FormatStyle::LK_Cpp &&
  1427. FormatTok->is(tok::identifier))
  1428. return;
  1429. }
  1430. }
  1431. // Just a declaration or something is wrong.
  1432. if (FormatTok->isNot(tok::l_brace))
  1433. return;
  1434. FormatTok->BlockKind = BK_Block;
  1435. if (Style.Language == FormatStyle::LK_Java) {
  1436. // Java enums are different.
  1437. parseJavaEnumBody();
  1438. return;
  1439. } else if (Style.Language == FormatStyle::LK_Proto) {
  1440. parseBlock(/*MustBeDeclaration=*/true);
  1441. return;
  1442. }
  1443. // Parse enum body.
  1444. bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
  1445. if (HasError) {
  1446. if (FormatTok->is(tok::semi))
  1447. nextToken();
  1448. addUnwrappedLine();
  1449. }
  1450. // There is no addUnwrappedLine() here so that we fall through to parsing a
  1451. // structural element afterwards. Thus, in "enum A {} n, m;",
  1452. // "} n, m;" will end up in one unwrapped line.
  1453. }
  1454. void UnwrappedLineParser::parseJavaEnumBody() {
  1455. // Determine whether the enum is simple, i.e. does not have a semicolon or
  1456. // constants with class bodies. Simple enums can be formatted like braced
  1457. // lists, contracted to a single line, etc.
  1458. unsigned StoredPosition = Tokens->getPosition();
  1459. bool IsSimple = true;
  1460. FormatToken *Tok = Tokens->getNextToken();
  1461. while (Tok) {
  1462. if (Tok->is(tok::r_brace))
  1463. break;
  1464. if (Tok->isOneOf(tok::l_brace, tok::semi)) {
  1465. IsSimple = false;
  1466. break;
  1467. }
  1468. // FIXME: This will also mark enums with braces in the arguments to enum
  1469. // constants as "not simple". This is probably fine in practice, though.
  1470. Tok = Tokens->getNextToken();
  1471. }
  1472. FormatTok = Tokens->setPosition(StoredPosition);
  1473. if (IsSimple) {
  1474. parseBracedList();
  1475. addUnwrappedLine();
  1476. return;
  1477. }
  1478. // Parse the body of a more complex enum.
  1479. // First add a line for everything up to the "{".
  1480. nextToken();
  1481. addUnwrappedLine();
  1482. ++Line->Level;
  1483. // Parse the enum constants.
  1484. while (FormatTok) {
  1485. if (FormatTok->is(tok::l_brace)) {
  1486. // Parse the constant's class body.
  1487. parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
  1488. /*MunchSemi=*/false);
  1489. } else if (FormatTok->is(tok::l_paren)) {
  1490. parseParens();
  1491. } else if (FormatTok->is(tok::comma)) {
  1492. nextToken();
  1493. addUnwrappedLine();
  1494. } else if (FormatTok->is(tok::semi)) {
  1495. nextToken();
  1496. addUnwrappedLine();
  1497. break;
  1498. } else if (FormatTok->is(tok::r_brace)) {
  1499. addUnwrappedLine();
  1500. break;
  1501. } else {
  1502. nextToken();
  1503. }
  1504. }
  1505. // Parse the class body after the enum's ";" if any.
  1506. parseLevel(/*HasOpeningBrace=*/true);
  1507. nextToken();
  1508. --Line->Level;
  1509. addUnwrappedLine();
  1510. }
  1511. void UnwrappedLineParser::parseRecord() {
  1512. const FormatToken &InitialToken = *FormatTok;
  1513. nextToken();
  1514. // The actual identifier can be a nested name specifier, and in macros
  1515. // it is often token-pasted.
  1516. while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
  1517. tok::kw___attribute, tok::kw___declspec,
  1518. tok::kw_alignas) ||
  1519. ((Style.Language == FormatStyle::LK_Java ||
  1520. Style.Language == FormatStyle::LK_JavaScript) &&
  1521. FormatTok->isOneOf(tok::period, tok::comma))) {
  1522. bool IsNonMacroIdentifier =
  1523. FormatTok->is(tok::identifier) &&
  1524. FormatTok->TokenText != FormatTok->TokenText.upper();
  1525. nextToken();
  1526. // We can have macros or attributes in between 'class' and the class name.
  1527. if (!IsNonMacroIdentifier && FormatTok->Tok.is(tok::l_paren))
  1528. parseParens();
  1529. }
  1530. // Note that parsing away template declarations here leads to incorrectly
  1531. // accepting function declarations as record declarations.
  1532. // In general, we cannot solve this problem. Consider:
  1533. // class A<int> B() {}
  1534. // which can be a function definition or a class definition when B() is a
  1535. // macro. If we find enough real-world cases where this is a problem, we
  1536. // can parse for the 'template' keyword in the beginning of the statement,
  1537. // and thus rule out the record production in case there is no template
  1538. // (this would still leave us with an ambiguity between template function
  1539. // and class declarations).
  1540. if (FormatTok->isOneOf(tok::colon, tok::less)) {
  1541. while (!eof()) {
  1542. if (FormatTok->is(tok::l_brace)) {
  1543. calculateBraceTypes(/*ExpectClassBody=*/true);
  1544. if (!tryToParseBracedList())
  1545. break;
  1546. }
  1547. if (FormatTok->Tok.is(tok::semi))
  1548. return;
  1549. nextToken();
  1550. }
  1551. }
  1552. if (FormatTok->Tok.is(tok::l_brace)) {
  1553. if (ShouldBreakBeforeBrace(Style, InitialToken))
  1554. addUnwrappedLine();
  1555. parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
  1556. /*MunchSemi=*/false);
  1557. }
  1558. // There is no addUnwrappedLine() here so that we fall through to parsing a
  1559. // structural element afterwards. Thus, in "class A {} n, m;",
  1560. // "} n, m;" will end up in one unwrapped line.
  1561. }
  1562. void UnwrappedLineParser::parseObjCProtocolList() {
  1563. assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
  1564. do
  1565. nextToken();
  1566. while (!eof() && FormatTok->Tok.isNot(tok::greater));
  1567. nextToken(); // Skip '>'.
  1568. }
  1569. void UnwrappedLineParser::parseObjCUntilAtEnd() {
  1570. do {
  1571. if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
  1572. nextToken();
  1573. addUnwrappedLine();
  1574. break;
  1575. }
  1576. if (FormatTok->is(tok::l_brace)) {
  1577. parseBlock(/*MustBeDeclaration=*/false);
  1578. // In ObjC interfaces, nothing should be following the "}".
  1579. addUnwrappedLine();
  1580. } else if (FormatTok->is(tok::r_brace)) {
  1581. // Ignore stray "}". parseStructuralElement doesn't consume them.
  1582. nextToken();
  1583. addUnwrappedLine();
  1584. } else {
  1585. parseStructuralElement();
  1586. }
  1587. } while (!eof());
  1588. }
  1589. void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
  1590. nextToken();
  1591. nextToken(); // interface name
  1592. // @interface can be followed by either a base class, or a category.
  1593. if (FormatTok->Tok.is(tok::colon)) {
  1594. nextToken();
  1595. nextToken(); // base class name
  1596. } else if (FormatTok->Tok.is(tok::l_paren))
  1597. // Skip category, if present.
  1598. parseParens();
  1599. if (FormatTok->Tok.is(tok::less))
  1600. parseObjCProtocolList();
  1601. if (FormatTok->Tok.is(tok::l_brace)) {
  1602. if (Style.BraceWrapping.AfterObjCDeclaration)
  1603. addUnwrappedLine();
  1604. parseBlock(/*MustBeDeclaration=*/true);
  1605. }
  1606. // With instance variables, this puts '}' on its own line. Without instance
  1607. // variables, this ends the @interface line.
  1608. addUnwrappedLine();
  1609. parseObjCUntilAtEnd();
  1610. }
  1611. void UnwrappedLineParser::parseObjCProtocol() {
  1612. nextToken();
  1613. nextToken(); // protocol name
  1614. if (FormatTok->Tok.is(tok::less))
  1615. parseObjCProtocolList();
  1616. // Check for protocol declaration.
  1617. if (FormatTok->Tok.is(tok::semi)) {
  1618. nextToken();
  1619. return addUnwrappedLine();
  1620. }
  1621. addUnwrappedLine();
  1622. parseObjCUntilAtEnd();
  1623. }
  1624. void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
  1625. assert(FormatTok->isOneOf(Keywords.kw_import, tok::kw_export));
  1626. nextToken();
  1627. // Consume the "default" in "export default class/function".
  1628. if (FormatTok->is(tok::kw_default))
  1629. nextToken();
  1630. // Consume "function" and "default function", so that these get parsed as
  1631. // free-standing JS functions, i.e. do not require a trailing semicolon.
  1632. if (FormatTok->is(Keywords.kw_function)) {
  1633. nextToken();
  1634. return;
  1635. }
  1636. if (FormatTok->isOneOf(tok::kw_const, tok::kw_class, tok::kw_enum,
  1637. Keywords.kw_let, Keywords.kw_var))
  1638. return; // Fall through to parsing the corresponding structure.
  1639. if (FormatTok->is(tok::l_brace)) {
  1640. FormatTok->BlockKind = BK_Block;
  1641. parseBracedList();
  1642. }
  1643. while (!eof() && FormatTok->isNot(tok::semi) &&
  1644. FormatTok->isNot(tok::l_brace)) {
  1645. nextToken();
  1646. }
  1647. }
  1648. LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
  1649. StringRef Prefix = "") {
  1650. llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
  1651. << (Line.InPPDirective ? " MACRO" : "") << ": ";
  1652. for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
  1653. E = Line.Tokens.end();
  1654. I != E; ++I) {
  1655. llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
  1656. }
  1657. for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
  1658. E = Line.Tokens.end();
  1659. I != E; ++I) {
  1660. const UnwrappedLineNode &Node = *I;
  1661. for (SmallVectorImpl<UnwrappedLine>::const_iterator
  1662. I = Node.Children.begin(),
  1663. E = Node.Children.end();
  1664. I != E; ++I) {
  1665. printDebugInfo(*I, "\nChild: ");
  1666. }
  1667. }
  1668. llvm::dbgs() << "\n";
  1669. }
  1670. void UnwrappedLineParser::addUnwrappedLine() {
  1671. if (Line->Tokens.empty())
  1672. return;
  1673. DEBUG({
  1674. if (CurrentLines == &Lines)
  1675. printDebugInfo(*Line);
  1676. });
  1677. CurrentLines->push_back(std::move(*Line));
  1678. Line->Tokens.clear();
  1679. if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
  1680. CurrentLines->append(
  1681. std::make_move_iterator(PreprocessorDirectives.begin()),
  1682. std::make_move_iterator(PreprocessorDirectives.end()));
  1683. PreprocessorDirectives.clear();
  1684. }
  1685. }
  1686. bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
  1687. bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
  1688. return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
  1689. FormatTok.NewlinesBefore > 0;
  1690. }
  1691. void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
  1692. bool JustComments = Line->Tokens.empty();
  1693. for (SmallVectorImpl<FormatToken *>::const_iterator
  1694. I = CommentsBeforeNextToken.begin(),
  1695. E = CommentsBeforeNextToken.end();
  1696. I != E; ++I) {
  1697. if (isOnNewLine(**I) && JustComments)
  1698. addUnwrappedLine();
  1699. pushToken(*I);
  1700. }
  1701. if (NewlineBeforeNext && JustComments)
  1702. addUnwrappedLine();
  1703. CommentsBeforeNextToken.clear();
  1704. }
  1705. void UnwrappedLineParser::nextToken() {
  1706. if (eof())
  1707. return;
  1708. flushComments(isOnNewLine(*FormatTok));
  1709. pushToken(FormatTok);
  1710. readToken();
  1711. }
  1712. void UnwrappedLineParser::readToken() {
  1713. bool CommentsInCurrentLine = true;
  1714. do {
  1715. FormatTok = Tokens->getNextToken();
  1716. assert(FormatTok);
  1717. while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
  1718. (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
  1719. // If there is an unfinished unwrapped line, we flush the preprocessor
  1720. // directives only after that unwrapped line was finished later.
  1721. bool SwitchToPreprocessorLines = !Line->Tokens.empty();
  1722. ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
  1723. // Comments stored before the preprocessor directive need to be output
  1724. // before the preprocessor directive, at the same level as the
  1725. // preprocessor directive, as we consider them to apply to the directive.
  1726. flushComments(isOnNewLine(*FormatTok));
  1727. parsePPDirective();
  1728. }
  1729. while (FormatTok->Type == TT_ConflictStart ||
  1730. FormatTok->Type == TT_ConflictEnd ||
  1731. FormatTok->Type == TT_ConflictAlternative) {
  1732. if (FormatTok->Type == TT_ConflictStart) {
  1733. conditionalCompilationStart(/*Unreachable=*/false);
  1734. } else if (FormatTok->Type == TT_ConflictAlternative) {
  1735. conditionalCompilationAlternative();
  1736. } else if (FormatTok->Type == TT_ConflictEnd) {
  1737. conditionalCompilationEnd();
  1738. }
  1739. FormatTok = Tokens->getNextToken();
  1740. FormatTok->MustBreakBefore = true;
  1741. }
  1742. if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
  1743. !Line->InPPDirective) {
  1744. continue;
  1745. }
  1746. if (!FormatTok->Tok.is(tok::comment))
  1747. return;
  1748. if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
  1749. CommentsInCurrentLine = false;
  1750. }
  1751. if (CommentsInCurrentLine) {
  1752. pushToken(FormatTok);
  1753. } else {
  1754. CommentsBeforeNextToken.push_back(FormatTok);
  1755. }
  1756. } while (!eof());
  1757. }
  1758. void UnwrappedLineParser::pushToken(FormatToken *Tok) {
  1759. Line->Tokens.push_back(UnwrappedLineNode(Tok));
  1760. if (MustBreakBeforeNextToken) {
  1761. Line->Tokens.back().Tok->MustBreakBefore = true;
  1762. MustBreakBeforeNextToken = false;
  1763. }
  1764. }
  1765. } // end namespace format
  1766. } // end namespace clang