UnwrappedLineParser.cpp 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425
  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. static bool isLineComment(const FormatToken &FormatTok) {
  50. return FormatTok.is(tok::comment) &&
  51. FormatTok.TokenText.startswith("//");
  52. }
  53. // Checks if \p FormatTok is a line comment that continues the line comment
  54. // \p Previous. The original column of \p MinColumnToken is used to determine
  55. // whether \p FormatTok is indented enough to the right to continue \p Previous.
  56. static bool continuesLineComment(const FormatToken &FormatTok,
  57. const FormatToken *Previous,
  58. const FormatToken *MinColumnToken) {
  59. if (!Previous || !MinColumnToken)
  60. return false;
  61. unsigned MinContinueColumn =
  62. MinColumnToken->OriginalColumn + (isLineComment(*MinColumnToken) ? 0 : 1);
  63. return isLineComment(FormatTok) && FormatTok.NewlinesBefore == 1 &&
  64. isLineComment(*Previous) &&
  65. FormatTok.OriginalColumn >= MinContinueColumn;
  66. }
  67. class ScopedMacroState : public FormatTokenSource {
  68. public:
  69. ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
  70. FormatToken *&ResetToken)
  71. : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
  72. PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
  73. Token(nullptr), PreviousToken(nullptr) {
  74. TokenSource = this;
  75. Line.Level = 0;
  76. Line.InPPDirective = true;
  77. }
  78. ~ScopedMacroState() override {
  79. TokenSource = PreviousTokenSource;
  80. ResetToken = Token;
  81. Line.InPPDirective = false;
  82. Line.Level = PreviousLineLevel;
  83. }
  84. FormatToken *getNextToken() override {
  85. // The \c UnwrappedLineParser guards against this by never calling
  86. // \c getNextToken() after it has encountered the first eof token.
  87. assert(!eof());
  88. PreviousToken = Token;
  89. Token = PreviousTokenSource->getNextToken();
  90. if (eof())
  91. return getFakeEOF();
  92. return Token;
  93. }
  94. unsigned getPosition() override { return PreviousTokenSource->getPosition(); }
  95. FormatToken *setPosition(unsigned Position) override {
  96. PreviousToken = nullptr;
  97. Token = PreviousTokenSource->setPosition(Position);
  98. return Token;
  99. }
  100. private:
  101. bool eof() {
  102. return Token && Token->HasUnescapedNewline &&
  103. !continuesLineComment(*Token, PreviousToken,
  104. /*MinColumnToken=*/PreviousToken);
  105. }
  106. FormatToken *getFakeEOF() {
  107. static bool EOFInitialized = false;
  108. static FormatToken FormatTok;
  109. if (!EOFInitialized) {
  110. FormatTok.Tok.startToken();
  111. FormatTok.Tok.setKind(tok::eof);
  112. EOFInitialized = true;
  113. }
  114. return &FormatTok;
  115. }
  116. UnwrappedLine &Line;
  117. FormatTokenSource *&TokenSource;
  118. FormatToken *&ResetToken;
  119. unsigned PreviousLineLevel;
  120. FormatTokenSource *PreviousTokenSource;
  121. FormatToken *Token;
  122. FormatToken *PreviousToken;
  123. };
  124. } // end anonymous namespace
  125. class ScopedLineState {
  126. public:
  127. ScopedLineState(UnwrappedLineParser &Parser,
  128. bool SwitchToPreprocessorLines = false)
  129. : Parser(Parser), OriginalLines(Parser.CurrentLines) {
  130. if (SwitchToPreprocessorLines)
  131. Parser.CurrentLines = &Parser.PreprocessorDirectives;
  132. else if (!Parser.Line->Tokens.empty())
  133. Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
  134. PreBlockLine = std::move(Parser.Line);
  135. Parser.Line = llvm::make_unique<UnwrappedLine>();
  136. Parser.Line->Level = PreBlockLine->Level;
  137. Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
  138. }
  139. ~ScopedLineState() {
  140. if (!Parser.Line->Tokens.empty()) {
  141. Parser.addUnwrappedLine();
  142. }
  143. assert(Parser.Line->Tokens.empty());
  144. Parser.Line = std::move(PreBlockLine);
  145. if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
  146. Parser.MustBreakBeforeNextToken = true;
  147. Parser.CurrentLines = OriginalLines;
  148. }
  149. private:
  150. UnwrappedLineParser &Parser;
  151. std::unique_ptr<UnwrappedLine> PreBlockLine;
  152. SmallVectorImpl<UnwrappedLine> *OriginalLines;
  153. };
  154. class CompoundStatementIndenter {
  155. public:
  156. CompoundStatementIndenter(UnwrappedLineParser *Parser,
  157. const FormatStyle &Style, unsigned &LineLevel)
  158. : LineLevel(LineLevel), OldLineLevel(LineLevel) {
  159. if (Style.BraceWrapping.AfterControlStatement)
  160. Parser->addUnwrappedLine();
  161. if (Style.BraceWrapping.IndentBraces)
  162. ++LineLevel;
  163. }
  164. ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
  165. private:
  166. unsigned &LineLevel;
  167. unsigned OldLineLevel;
  168. };
  169. namespace {
  170. class IndexedTokenSource : public FormatTokenSource {
  171. public:
  172. IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
  173. : Tokens(Tokens), Position(-1) {}
  174. FormatToken *getNextToken() override {
  175. ++Position;
  176. return Tokens[Position];
  177. }
  178. unsigned getPosition() override {
  179. assert(Position >= 0);
  180. return Position;
  181. }
  182. FormatToken *setPosition(unsigned P) override {
  183. Position = P;
  184. return Tokens[Position];
  185. }
  186. void reset() { Position = -1; }
  187. private:
  188. ArrayRef<FormatToken *> Tokens;
  189. int Position;
  190. };
  191. } // end anonymous namespace
  192. UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
  193. const AdditionalKeywords &Keywords,
  194. ArrayRef<FormatToken *> Tokens,
  195. UnwrappedLineConsumer &Callback)
  196. : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
  197. CurrentLines(&Lines), Style(Style), Keywords(Keywords),
  198. CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr),
  199. Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1) {}
  200. void UnwrappedLineParser::reset() {
  201. PPBranchLevel = -1;
  202. Line.reset(new UnwrappedLine);
  203. CommentsBeforeNextToken.clear();
  204. FormatTok = nullptr;
  205. MustBreakBeforeNextToken = false;
  206. PreprocessorDirectives.clear();
  207. CurrentLines = &Lines;
  208. DeclarationScopeStack.clear();
  209. PPStack.clear();
  210. }
  211. void UnwrappedLineParser::parse() {
  212. IndexedTokenSource TokenSource(AllTokens);
  213. do {
  214. DEBUG(llvm::dbgs() << "----\n");
  215. reset();
  216. Tokens = &TokenSource;
  217. TokenSource.reset();
  218. readToken();
  219. parseFile();
  220. // Create line with eof token.
  221. pushToken(FormatTok);
  222. addUnwrappedLine();
  223. for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
  224. E = Lines.end();
  225. I != E; ++I) {
  226. Callback.consumeUnwrappedLine(*I);
  227. }
  228. Callback.finishRun();
  229. Lines.clear();
  230. while (!PPLevelBranchIndex.empty() &&
  231. PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
  232. PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
  233. PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
  234. }
  235. if (!PPLevelBranchIndex.empty()) {
  236. ++PPLevelBranchIndex.back();
  237. assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
  238. assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
  239. }
  240. } while (!PPLevelBranchIndex.empty());
  241. }
  242. void UnwrappedLineParser::parseFile() {
  243. // The top-level context in a file always has declarations, except for pre-
  244. // processor directives and JavaScript files.
  245. bool MustBeDeclaration =
  246. !Line->InPPDirective && Style.Language != FormatStyle::LK_JavaScript;
  247. ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
  248. MustBeDeclaration);
  249. if (Style.Language == FormatStyle::LK_TextProto)
  250. parseBracedList();
  251. else
  252. parseLevel(/*HasOpeningBrace=*/false);
  253. // Make sure to format the remaining tokens.
  254. flushComments(true);
  255. addUnwrappedLine();
  256. }
  257. void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
  258. bool SwitchLabelEncountered = false;
  259. do {
  260. tok::TokenKind kind = FormatTok->Tok.getKind();
  261. if (FormatTok->Type == TT_MacroBlockBegin) {
  262. kind = tok::l_brace;
  263. } else if (FormatTok->Type == TT_MacroBlockEnd) {
  264. kind = tok::r_brace;
  265. }
  266. switch (kind) {
  267. case tok::comment:
  268. nextToken();
  269. addUnwrappedLine();
  270. break;
  271. case tok::l_brace:
  272. // FIXME: Add parameter whether this can happen - if this happens, we must
  273. // be in a non-declaration context.
  274. if (!FormatTok->is(TT_MacroBlockBegin) && tryToParseBracedList())
  275. continue;
  276. parseBlock(/*MustBeDeclaration=*/false);
  277. addUnwrappedLine();
  278. break;
  279. case tok::r_brace:
  280. if (HasOpeningBrace)
  281. return;
  282. nextToken();
  283. addUnwrappedLine();
  284. break;
  285. case tok::kw_default:
  286. case tok::kw_case:
  287. if (!SwitchLabelEncountered &&
  288. (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
  289. ++Line->Level;
  290. SwitchLabelEncountered = true;
  291. parseStructuralElement();
  292. break;
  293. default:
  294. parseStructuralElement();
  295. break;
  296. }
  297. } while (!eof());
  298. }
  299. void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
  300. // We'll parse forward through the tokens until we hit
  301. // a closing brace or eof - note that getNextToken() will
  302. // parse macros, so this will magically work inside macro
  303. // definitions, too.
  304. unsigned StoredPosition = Tokens->getPosition();
  305. FormatToken *Tok = FormatTok;
  306. const FormatToken *PrevTok = getPreviousToken();
  307. // Keep a stack of positions of lbrace tokens. We will
  308. // update information about whether an lbrace starts a
  309. // braced init list or a different block during the loop.
  310. SmallVector<FormatToken *, 8> LBraceStack;
  311. assert(Tok->Tok.is(tok::l_brace));
  312. do {
  313. // Get next non-comment token.
  314. FormatToken *NextTok;
  315. unsigned ReadTokens = 0;
  316. do {
  317. NextTok = Tokens->getNextToken();
  318. ++ReadTokens;
  319. } while (NextTok->is(tok::comment));
  320. switch (Tok->Tok.getKind()) {
  321. case tok::l_brace:
  322. if (Style.Language == FormatStyle::LK_JavaScript && PrevTok) {
  323. if (PrevTok->is(tok::colon))
  324. // A colon indicates this code is in a type, or a braced list
  325. // following a label in an object literal ({a: {b: 1}}). The code
  326. // below could be confused by semicolons between the individual
  327. // members in a type member list, which would normally trigger
  328. // BK_Block. In both cases, this must be parsed as an inline braced
  329. // init.
  330. Tok->BlockKind = BK_BracedInit;
  331. else if (PrevTok->is(tok::r_paren))
  332. // `) { }` can only occur in function or method declarations in JS.
  333. Tok->BlockKind = BK_Block;
  334. } else {
  335. Tok->BlockKind = BK_Unknown;
  336. }
  337. LBraceStack.push_back(Tok);
  338. break;
  339. case tok::r_brace:
  340. if (LBraceStack.empty())
  341. break;
  342. if (LBraceStack.back()->BlockKind == BK_Unknown) {
  343. bool ProbablyBracedList = false;
  344. if (Style.Language == FormatStyle::LK_Proto) {
  345. ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
  346. } else {
  347. // Using OriginalColumn to distinguish between ObjC methods and
  348. // binary operators is a bit hacky.
  349. bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
  350. NextTok->OriginalColumn == 0;
  351. // If there is a comma, semicolon or right paren after the closing
  352. // brace, we assume this is a braced initializer list. Note that
  353. // regardless how we mark inner braces here, we will overwrite the
  354. // BlockKind later if we parse a braced list (where all blocks
  355. // inside are by default braced lists), or when we explicitly detect
  356. // blocks (for example while parsing lambdas).
  357. // FIXME: Some of these do not apply to JS, e.g. "} {" can never be a
  358. // braced list in JS.
  359. ProbablyBracedList =
  360. (Style.Language == FormatStyle::LK_JavaScript &&
  361. NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in,
  362. Keywords.kw_as)) ||
  363. (Style.isCpp() && NextTok->is(tok::l_paren)) ||
  364. NextTok->isOneOf(tok::comma, tok::period, tok::colon,
  365. tok::r_paren, tok::r_square, tok::l_brace,
  366. tok::l_square, tok::ellipsis) ||
  367. (NextTok->is(tok::identifier) &&
  368. !PrevTok->isOneOf(tok::semi, tok::r_brace, tok::l_brace)) ||
  369. (NextTok->is(tok::semi) &&
  370. (!ExpectClassBody || LBraceStack.size() != 1)) ||
  371. (NextTok->isBinaryOperator() && !NextIsObjCMethod);
  372. }
  373. if (ProbablyBracedList) {
  374. Tok->BlockKind = BK_BracedInit;
  375. LBraceStack.back()->BlockKind = BK_BracedInit;
  376. } else {
  377. Tok->BlockKind = BK_Block;
  378. LBraceStack.back()->BlockKind = BK_Block;
  379. }
  380. }
  381. LBraceStack.pop_back();
  382. break;
  383. case tok::at:
  384. case tok::semi:
  385. case tok::kw_if:
  386. case tok::kw_while:
  387. case tok::kw_for:
  388. case tok::kw_switch:
  389. case tok::kw_try:
  390. case tok::kw___try:
  391. if (!LBraceStack.empty() && LBraceStack.back()->BlockKind == BK_Unknown)
  392. LBraceStack.back()->BlockKind = BK_Block;
  393. break;
  394. default:
  395. break;
  396. }
  397. PrevTok = Tok;
  398. Tok = NextTok;
  399. } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
  400. // Assume other blocks for all unclosed opening braces.
  401. for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
  402. if (LBraceStack[i]->BlockKind == BK_Unknown)
  403. LBraceStack[i]->BlockKind = BK_Block;
  404. }
  405. FormatTok = Tokens->setPosition(StoredPosition);
  406. }
  407. void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
  408. bool MunchSemi) {
  409. assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) &&
  410. "'{' or macro block token expected");
  411. const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin);
  412. FormatTok->BlockKind = BK_Block;
  413. unsigned InitialLevel = Line->Level;
  414. nextToken();
  415. if (MacroBlock && FormatTok->is(tok::l_paren))
  416. parseParens();
  417. addUnwrappedLine();
  418. size_t OpeningLineIndex = CurrentLines->empty()
  419. ? (UnwrappedLine::kInvalidIndex)
  420. : (CurrentLines->size() - 1);
  421. ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
  422. MustBeDeclaration);
  423. if (AddLevel)
  424. ++Line->Level;
  425. parseLevel(/*HasOpeningBrace=*/true);
  426. if (eof())
  427. return;
  428. if (MacroBlock ? !FormatTok->is(TT_MacroBlockEnd)
  429. : !FormatTok->is(tok::r_brace)) {
  430. Line->Level = InitialLevel;
  431. FormatTok->BlockKind = BK_Block;
  432. return;
  433. }
  434. nextToken(); // Munch the closing brace.
  435. if (MacroBlock && FormatTok->is(tok::l_paren))
  436. parseParens();
  437. if (MunchSemi && FormatTok->Tok.is(tok::semi))
  438. nextToken();
  439. Line->Level = InitialLevel;
  440. Line->MatchingOpeningBlockLineIndex = OpeningLineIndex;
  441. if (OpeningLineIndex != UnwrappedLine::kInvalidIndex) {
  442. // Update the opening line to add the forward reference as well
  443. (*CurrentLines)[OpeningLineIndex].MatchingOpeningBlockLineIndex =
  444. CurrentLines->size() - 1;
  445. }
  446. }
  447. static bool isGoogScope(const UnwrappedLine &Line) {
  448. // FIXME: Closure-library specific stuff should not be hard-coded but be
  449. // configurable.
  450. if (Line.Tokens.size() < 4)
  451. return false;
  452. auto I = Line.Tokens.begin();
  453. if (I->Tok->TokenText != "goog")
  454. return false;
  455. ++I;
  456. if (I->Tok->isNot(tok::period))
  457. return false;
  458. ++I;
  459. if (I->Tok->TokenText != "scope")
  460. return false;
  461. ++I;
  462. return I->Tok->is(tok::l_paren);
  463. }
  464. static bool isIIFE(const UnwrappedLine &Line,
  465. const AdditionalKeywords &Keywords) {
  466. // Look for the start of an immediately invoked anonymous function.
  467. // https://en.wikipedia.org/wiki/Immediately-invoked_function_expression
  468. // This is commonly done in JavaScript to create a new, anonymous scope.
  469. // Example: (function() { ... })()
  470. if (Line.Tokens.size() < 3)
  471. return false;
  472. auto I = Line.Tokens.begin();
  473. if (I->Tok->isNot(tok::l_paren))
  474. return false;
  475. ++I;
  476. if (I->Tok->isNot(Keywords.kw_function))
  477. return false;
  478. ++I;
  479. return I->Tok->is(tok::l_paren);
  480. }
  481. static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
  482. const FormatToken &InitialToken) {
  483. if (InitialToken.is(tok::kw_namespace))
  484. return Style.BraceWrapping.AfterNamespace;
  485. if (InitialToken.is(tok::kw_class))
  486. return Style.BraceWrapping.AfterClass;
  487. if (InitialToken.is(tok::kw_union))
  488. return Style.BraceWrapping.AfterUnion;
  489. if (InitialToken.is(tok::kw_struct))
  490. return Style.BraceWrapping.AfterStruct;
  491. return false;
  492. }
  493. void UnwrappedLineParser::parseChildBlock() {
  494. FormatTok->BlockKind = BK_Block;
  495. nextToken();
  496. {
  497. bool SkipIndent =
  498. (Style.Language == FormatStyle::LK_JavaScript &&
  499. (isGoogScope(*Line) || isIIFE(*Line, Keywords)));
  500. ScopedLineState LineState(*this);
  501. ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
  502. /*MustBeDeclaration=*/false);
  503. Line->Level += SkipIndent ? 0 : 1;
  504. parseLevel(/*HasOpeningBrace=*/true);
  505. flushComments(isOnNewLine(*FormatTok));
  506. Line->Level -= SkipIndent ? 0 : 1;
  507. }
  508. nextToken();
  509. }
  510. void UnwrappedLineParser::parsePPDirective() {
  511. assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
  512. ScopedMacroState MacroState(*Line, Tokens, FormatTok);
  513. nextToken();
  514. if (!FormatTok->Tok.getIdentifierInfo()) {
  515. parsePPUnknown();
  516. return;
  517. }
  518. switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
  519. case tok::pp_define:
  520. parsePPDefine();
  521. return;
  522. case tok::pp_if:
  523. parsePPIf(/*IfDef=*/false);
  524. break;
  525. case tok::pp_ifdef:
  526. case tok::pp_ifndef:
  527. parsePPIf(/*IfDef=*/true);
  528. break;
  529. case tok::pp_else:
  530. parsePPElse();
  531. break;
  532. case tok::pp_elif:
  533. parsePPElIf();
  534. break;
  535. case tok::pp_endif:
  536. parsePPEndIf();
  537. break;
  538. default:
  539. parsePPUnknown();
  540. break;
  541. }
  542. }
  543. void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
  544. if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable))
  545. PPStack.push_back(PP_Unreachable);
  546. else
  547. PPStack.push_back(PP_Conditional);
  548. }
  549. void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
  550. ++PPBranchLevel;
  551. assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
  552. if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
  553. PPLevelBranchIndex.push_back(0);
  554. PPLevelBranchCount.push_back(0);
  555. }
  556. PPChainBranchIndex.push(0);
  557. bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
  558. conditionalCompilationCondition(Unreachable || Skip);
  559. }
  560. void UnwrappedLineParser::conditionalCompilationAlternative() {
  561. if (!PPStack.empty())
  562. PPStack.pop_back();
  563. assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
  564. if (!PPChainBranchIndex.empty())
  565. ++PPChainBranchIndex.top();
  566. conditionalCompilationCondition(
  567. PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
  568. PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
  569. }
  570. void UnwrappedLineParser::conditionalCompilationEnd() {
  571. assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
  572. if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
  573. if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
  574. PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
  575. }
  576. }
  577. // Guard against #endif's without #if.
  578. if (PPBranchLevel > 0)
  579. --PPBranchLevel;
  580. if (!PPChainBranchIndex.empty())
  581. PPChainBranchIndex.pop();
  582. if (!PPStack.empty())
  583. PPStack.pop_back();
  584. }
  585. void UnwrappedLineParser::parsePPIf(bool IfDef) {
  586. bool IfNDef = FormatTok->is(tok::pp_ifndef);
  587. nextToken();
  588. bool Unreachable = false;
  589. if (!IfDef && (FormatTok->is(tok::kw_false) || FormatTok->TokenText == "0"))
  590. Unreachable = true;
  591. if (IfDef && !IfNDef && FormatTok->TokenText == "SWIG")
  592. Unreachable = true;
  593. conditionalCompilationStart(Unreachable);
  594. parsePPUnknown();
  595. }
  596. void UnwrappedLineParser::parsePPElse() {
  597. conditionalCompilationAlternative();
  598. parsePPUnknown();
  599. }
  600. void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
  601. void UnwrappedLineParser::parsePPEndIf() {
  602. conditionalCompilationEnd();
  603. parsePPUnknown();
  604. }
  605. void UnwrappedLineParser::parsePPDefine() {
  606. nextToken();
  607. if (FormatTok->Tok.getKind() != tok::identifier) {
  608. parsePPUnknown();
  609. return;
  610. }
  611. nextToken();
  612. if (FormatTok->Tok.getKind() == tok::l_paren &&
  613. FormatTok->WhitespaceRange.getBegin() ==
  614. FormatTok->WhitespaceRange.getEnd()) {
  615. parseParens();
  616. }
  617. addUnwrappedLine();
  618. Line->Level = 1;
  619. // Errors during a preprocessor directive can only affect the layout of the
  620. // preprocessor directive, and thus we ignore them. An alternative approach
  621. // would be to use the same approach we use on the file level (no
  622. // re-indentation if there was a structural error) within the macro
  623. // definition.
  624. parseFile();
  625. }
  626. void UnwrappedLineParser::parsePPUnknown() {
  627. do {
  628. nextToken();
  629. } while (!eof());
  630. addUnwrappedLine();
  631. }
  632. // Here we blacklist certain tokens that are not usually the first token in an
  633. // unwrapped line. This is used in attempt to distinguish macro calls without
  634. // trailing semicolons from other constructs split to several lines.
  635. static bool tokenCanStartNewLine(const clang::Token &Tok) {
  636. // Semicolon can be a null-statement, l_square can be a start of a macro or
  637. // a C++11 attribute, but this doesn't seem to be common.
  638. return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
  639. Tok.isNot(tok::l_square) &&
  640. // Tokens that can only be used as binary operators and a part of
  641. // overloaded operator names.
  642. Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
  643. Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
  644. Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
  645. Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
  646. Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
  647. Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
  648. Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
  649. Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
  650. Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
  651. Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
  652. Tok.isNot(tok::lesslessequal) &&
  653. // Colon is used in labels, base class lists, initializer lists,
  654. // range-based for loops, ternary operator, but should never be the
  655. // first token in an unwrapped line.
  656. Tok.isNot(tok::colon) &&
  657. // 'noexcept' is a trailing annotation.
  658. Tok.isNot(tok::kw_noexcept);
  659. }
  660. static bool mustBeJSIdent(const AdditionalKeywords &Keywords,
  661. const FormatToken *FormatTok) {
  662. // FIXME: This returns true for C/C++ keywords like 'struct'.
  663. return FormatTok->is(tok::identifier) &&
  664. (FormatTok->Tok.getIdentifierInfo() == nullptr ||
  665. !FormatTok->isOneOf(
  666. Keywords.kw_in, Keywords.kw_of, Keywords.kw_as, Keywords.kw_async,
  667. Keywords.kw_await, Keywords.kw_yield, Keywords.kw_finally,
  668. Keywords.kw_function, Keywords.kw_import, Keywords.kw_is,
  669. Keywords.kw_let, Keywords.kw_var, tok::kw_const,
  670. Keywords.kw_abstract, Keywords.kw_extends, Keywords.kw_implements,
  671. Keywords.kw_instanceof, Keywords.kw_interface,
  672. Keywords.kw_throws));
  673. }
  674. static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords,
  675. const FormatToken *FormatTok) {
  676. return FormatTok->Tok.isLiteral() ||
  677. FormatTok->isOneOf(tok::kw_true, tok::kw_false) ||
  678. mustBeJSIdent(Keywords, FormatTok);
  679. }
  680. // isJSDeclOrStmt returns true if |FormatTok| starts a declaration or statement
  681. // when encountered after a value (see mustBeJSIdentOrValue).
  682. static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords,
  683. const FormatToken *FormatTok) {
  684. return FormatTok->isOneOf(
  685. tok::kw_return, Keywords.kw_yield,
  686. // conditionals
  687. tok::kw_if, tok::kw_else,
  688. // loops
  689. tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break,
  690. // switch/case
  691. tok::kw_switch, tok::kw_case,
  692. // exceptions
  693. tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.kw_finally,
  694. // declaration
  695. tok::kw_const, tok::kw_class, Keywords.kw_var, Keywords.kw_let,
  696. Keywords.kw_async, Keywords.kw_function,
  697. // import/export
  698. Keywords.kw_import, tok::kw_export);
  699. }
  700. // readTokenWithJavaScriptASI reads the next token and terminates the current
  701. // line if JavaScript Automatic Semicolon Insertion must
  702. // happen between the current token and the next token.
  703. //
  704. // This method is conservative - it cannot cover all edge cases of JavaScript,
  705. // but only aims to correctly handle certain well known cases. It *must not*
  706. // return true in speculative cases.
  707. void UnwrappedLineParser::readTokenWithJavaScriptASI() {
  708. FormatToken *Previous = FormatTok;
  709. readToken();
  710. FormatToken *Next = FormatTok;
  711. bool IsOnSameLine =
  712. CommentsBeforeNextToken.empty()
  713. ? Next->NewlinesBefore == 0
  714. : CommentsBeforeNextToken.front()->NewlinesBefore == 0;
  715. if (IsOnSameLine)
  716. return;
  717. bool PreviousMustBeValue = mustBeJSIdentOrValue(Keywords, Previous);
  718. bool PreviousStartsTemplateExpr =
  719. Previous->is(TT_TemplateString) && Previous->TokenText.endswith("${");
  720. if (PreviousMustBeValue && Line && Line->Tokens.size() > 1) {
  721. // If the token before the previous one is an '@', the previous token is an
  722. // annotation and can precede another identifier/value.
  723. const FormatToken *PrePrevious = std::prev(Line->Tokens.end(), 2)->Tok;
  724. if (PrePrevious->is(tok::at))
  725. return;
  726. }
  727. if (Next->is(tok::exclaim) && PreviousMustBeValue)
  728. return addUnwrappedLine();
  729. bool NextMustBeValue = mustBeJSIdentOrValue(Keywords, Next);
  730. bool NextEndsTemplateExpr =
  731. Next->is(TT_TemplateString) && Next->TokenText.startswith("}");
  732. if (NextMustBeValue && !NextEndsTemplateExpr && !PreviousStartsTemplateExpr &&
  733. (PreviousMustBeValue ||
  734. Previous->isOneOf(tok::r_square, tok::r_paren, tok::plusplus,
  735. tok::minusminus)))
  736. return addUnwrappedLine();
  737. if (PreviousMustBeValue && isJSDeclOrStmt(Keywords, Next))
  738. return addUnwrappedLine();
  739. }
  740. void UnwrappedLineParser::parseStructuralElement() {
  741. assert(!FormatTok->is(tok::l_brace));
  742. if (Style.Language == FormatStyle::LK_TableGen &&
  743. FormatTok->is(tok::pp_include)) {
  744. nextToken();
  745. if (FormatTok->is(tok::string_literal))
  746. nextToken();
  747. addUnwrappedLine();
  748. return;
  749. }
  750. switch (FormatTok->Tok.getKind()) {
  751. case tok::at:
  752. nextToken();
  753. if (FormatTok->Tok.is(tok::l_brace)) {
  754. nextToken();
  755. parseBracedList();
  756. break;
  757. }
  758. switch (FormatTok->Tok.getObjCKeywordID()) {
  759. case tok::objc_public:
  760. case tok::objc_protected:
  761. case tok::objc_package:
  762. case tok::objc_private:
  763. return parseAccessSpecifier();
  764. case tok::objc_interface:
  765. case tok::objc_implementation:
  766. return parseObjCInterfaceOrImplementation();
  767. case tok::objc_protocol:
  768. return parseObjCProtocol();
  769. case tok::objc_end:
  770. return; // Handled by the caller.
  771. case tok::objc_optional:
  772. case tok::objc_required:
  773. nextToken();
  774. addUnwrappedLine();
  775. return;
  776. case tok::objc_autoreleasepool:
  777. nextToken();
  778. if (FormatTok->Tok.is(tok::l_brace)) {
  779. if (Style.BraceWrapping.AfterObjCDeclaration)
  780. addUnwrappedLine();
  781. parseBlock(/*MustBeDeclaration=*/false);
  782. }
  783. addUnwrappedLine();
  784. return;
  785. case tok::objc_try:
  786. // This branch isn't strictly necessary (the kw_try case below would
  787. // do this too after the tok::at is parsed above). But be explicit.
  788. parseTryCatch();
  789. return;
  790. default:
  791. break;
  792. }
  793. break;
  794. case tok::kw_asm:
  795. nextToken();
  796. if (FormatTok->is(tok::l_brace)) {
  797. FormatTok->Type = TT_InlineASMBrace;
  798. nextToken();
  799. while (FormatTok && FormatTok->isNot(tok::eof)) {
  800. if (FormatTok->is(tok::r_brace)) {
  801. FormatTok->Type = TT_InlineASMBrace;
  802. nextToken();
  803. addUnwrappedLine();
  804. break;
  805. }
  806. FormatTok->Finalized = true;
  807. nextToken();
  808. }
  809. }
  810. break;
  811. case tok::kw_namespace:
  812. parseNamespace();
  813. return;
  814. case tok::kw_inline:
  815. nextToken();
  816. if (FormatTok->Tok.is(tok::kw_namespace)) {
  817. parseNamespace();
  818. return;
  819. }
  820. break;
  821. case tok::kw_public:
  822. case tok::kw_protected:
  823. case tok::kw_private:
  824. if (Style.Language == FormatStyle::LK_Java ||
  825. Style.Language == FormatStyle::LK_JavaScript)
  826. nextToken();
  827. else
  828. parseAccessSpecifier();
  829. return;
  830. case tok::kw_if:
  831. parseIfThenElse();
  832. return;
  833. case tok::kw_for:
  834. case tok::kw_while:
  835. parseForOrWhileLoop();
  836. return;
  837. case tok::kw_do:
  838. parseDoWhile();
  839. return;
  840. case tok::kw_switch:
  841. parseSwitch();
  842. return;
  843. case tok::kw_default:
  844. nextToken();
  845. parseLabel();
  846. return;
  847. case tok::kw_case:
  848. parseCaseLabel();
  849. return;
  850. case tok::kw_try:
  851. case tok::kw___try:
  852. parseTryCatch();
  853. return;
  854. case tok::kw_extern:
  855. nextToken();
  856. if (FormatTok->Tok.is(tok::string_literal)) {
  857. nextToken();
  858. if (FormatTok->Tok.is(tok::l_brace)) {
  859. parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
  860. addUnwrappedLine();
  861. return;
  862. }
  863. }
  864. break;
  865. case tok::kw_export:
  866. if (Style.Language == FormatStyle::LK_JavaScript) {
  867. parseJavaScriptEs6ImportExport();
  868. return;
  869. }
  870. break;
  871. case tok::identifier:
  872. if (FormatTok->is(TT_ForEachMacro)) {
  873. parseForOrWhileLoop();
  874. return;
  875. }
  876. if (FormatTok->is(TT_MacroBlockBegin)) {
  877. parseBlock(/*MustBeDeclaration=*/false, /*AddLevel=*/true,
  878. /*MunchSemi=*/false);
  879. return;
  880. }
  881. if (FormatTok->is(Keywords.kw_import)) {
  882. if (Style.Language == FormatStyle::LK_JavaScript) {
  883. parseJavaScriptEs6ImportExport();
  884. return;
  885. }
  886. if (Style.Language == FormatStyle::LK_Proto) {
  887. nextToken();
  888. if (FormatTok->is(tok::kw_public))
  889. nextToken();
  890. if (!FormatTok->is(tok::string_literal))
  891. return;
  892. nextToken();
  893. if (FormatTok->is(tok::semi))
  894. nextToken();
  895. addUnwrappedLine();
  896. return;
  897. }
  898. }
  899. if (Style.isCpp() &&
  900. FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals,
  901. Keywords.kw_slots, Keywords.kw_qslots)) {
  902. nextToken();
  903. if (FormatTok->is(tok::colon)) {
  904. nextToken();
  905. addUnwrappedLine();
  906. return;
  907. }
  908. }
  909. // In all other cases, parse the declaration.
  910. break;
  911. default:
  912. break;
  913. }
  914. do {
  915. const FormatToken *Previous = getPreviousToken();
  916. switch (FormatTok->Tok.getKind()) {
  917. case tok::at:
  918. nextToken();
  919. if (FormatTok->Tok.is(tok::l_brace)) {
  920. nextToken();
  921. parseBracedList();
  922. }
  923. break;
  924. case tok::kw_enum:
  925. // Ignore if this is part of "template <enum ...".
  926. if (Previous && Previous->is(tok::less)) {
  927. nextToken();
  928. break;
  929. }
  930. // parseEnum falls through and does not yet add an unwrapped line as an
  931. // enum definition can start a structural element.
  932. if (!parseEnum())
  933. break;
  934. // This only applies for C++.
  935. if (!Style.isCpp()) {
  936. addUnwrappedLine();
  937. return;
  938. }
  939. break;
  940. case tok::kw_typedef:
  941. nextToken();
  942. if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
  943. Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
  944. parseEnum();
  945. break;
  946. case tok::kw_struct:
  947. case tok::kw_union:
  948. case tok::kw_class:
  949. // parseRecord falls through and does not yet add an unwrapped line as a
  950. // record declaration or definition can start a structural element.
  951. parseRecord();
  952. // This does not apply for Java and JavaScript.
  953. if (Style.Language == FormatStyle::LK_Java ||
  954. Style.Language == FormatStyle::LK_JavaScript) {
  955. if (FormatTok->is(tok::semi))
  956. nextToken();
  957. addUnwrappedLine();
  958. return;
  959. }
  960. break;
  961. case tok::period:
  962. nextToken();
  963. // In Java, classes have an implicit static member "class".
  964. if (Style.Language == FormatStyle::LK_Java && FormatTok &&
  965. FormatTok->is(tok::kw_class))
  966. nextToken();
  967. if (Style.Language == FormatStyle::LK_JavaScript && FormatTok &&
  968. FormatTok->Tok.getIdentifierInfo())
  969. // JavaScript only has pseudo keywords, all keywords are allowed to
  970. // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6
  971. nextToken();
  972. break;
  973. case tok::semi:
  974. nextToken();
  975. addUnwrappedLine();
  976. return;
  977. case tok::r_brace:
  978. addUnwrappedLine();
  979. return;
  980. case tok::l_paren:
  981. parseParens();
  982. break;
  983. case tok::kw_operator:
  984. nextToken();
  985. if (FormatTok->isBinaryOperator())
  986. nextToken();
  987. break;
  988. case tok::caret:
  989. nextToken();
  990. if (FormatTok->Tok.isAnyIdentifier() ||
  991. FormatTok->isSimpleTypeSpecifier())
  992. nextToken();
  993. if (FormatTok->is(tok::l_paren))
  994. parseParens();
  995. if (FormatTok->is(tok::l_brace))
  996. parseChildBlock();
  997. break;
  998. case tok::l_brace:
  999. if (!tryToParseBracedList()) {
  1000. // A block outside of parentheses must be the last part of a
  1001. // structural element.
  1002. // FIXME: Figure out cases where this is not true, and add projections
  1003. // for them (the one we know is missing are lambdas).
  1004. if (Style.BraceWrapping.AfterFunction)
  1005. addUnwrappedLine();
  1006. FormatTok->Type = TT_FunctionLBrace;
  1007. parseBlock(/*MustBeDeclaration=*/false);
  1008. addUnwrappedLine();
  1009. return;
  1010. }
  1011. // Otherwise this was a braced init list, and the structural
  1012. // element continues.
  1013. break;
  1014. case tok::kw_try:
  1015. // We arrive here when parsing function-try blocks.
  1016. parseTryCatch();
  1017. return;
  1018. case tok::identifier: {
  1019. if (FormatTok->is(TT_MacroBlockEnd)) {
  1020. addUnwrappedLine();
  1021. return;
  1022. }
  1023. // Function declarations (as opposed to function expressions) are parsed
  1024. // on their own unwrapped line by continuing this loop. Function
  1025. // expressions (functions that are not on their own line) must not create
  1026. // a new unwrapped line, so they are special cased below.
  1027. size_t TokenCount = Line->Tokens.size();
  1028. if (Style.Language == FormatStyle::LK_JavaScript &&
  1029. FormatTok->is(Keywords.kw_function) &&
  1030. (TokenCount > 1 || (TokenCount == 1 && !Line->Tokens.front().Tok->is(
  1031. Keywords.kw_async)))) {
  1032. tryToParseJSFunction();
  1033. break;
  1034. }
  1035. if ((Style.Language == FormatStyle::LK_JavaScript ||
  1036. Style.Language == FormatStyle::LK_Java) &&
  1037. FormatTok->is(Keywords.kw_interface)) {
  1038. if (Style.Language == FormatStyle::LK_JavaScript) {
  1039. // In JavaScript/TypeScript, "interface" can be used as a standalone
  1040. // identifier, e.g. in `var interface = 1;`. If "interface" is
  1041. // followed by another identifier, it is very like to be an actual
  1042. // interface declaration.
  1043. unsigned StoredPosition = Tokens->getPosition();
  1044. FormatToken *Next = Tokens->getNextToken();
  1045. FormatTok = Tokens->setPosition(StoredPosition);
  1046. if (Next && !mustBeJSIdent(Keywords, Next)) {
  1047. nextToken();
  1048. break;
  1049. }
  1050. }
  1051. parseRecord();
  1052. addUnwrappedLine();
  1053. return;
  1054. }
  1055. // See if the following token should start a new unwrapped line.
  1056. StringRef Text = FormatTok->TokenText;
  1057. nextToken();
  1058. if (Line->Tokens.size() == 1 &&
  1059. // JS doesn't have macros, and within classes colons indicate fields,
  1060. // not labels.
  1061. Style.Language != FormatStyle::LK_JavaScript) {
  1062. if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) {
  1063. Line->Tokens.begin()->Tok->MustBreakBefore = true;
  1064. parseLabel();
  1065. return;
  1066. }
  1067. // Recognize function-like macro usages without trailing semicolon as
  1068. // well as free-standing macros like Q_OBJECT.
  1069. bool FunctionLike = FormatTok->is(tok::l_paren);
  1070. if (FunctionLike)
  1071. parseParens();
  1072. bool FollowedByNewline =
  1073. CommentsBeforeNextToken.empty()
  1074. ? FormatTok->NewlinesBefore > 0
  1075. : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
  1076. if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) &&
  1077. tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
  1078. addUnwrappedLine();
  1079. return;
  1080. }
  1081. }
  1082. break;
  1083. }
  1084. case tok::equal:
  1085. // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType
  1086. // TT_JsFatArrow. The always start an expression or a child block if
  1087. // followed by a curly.
  1088. if (FormatTok->is(TT_JsFatArrow)) {
  1089. nextToken();
  1090. if (FormatTok->is(tok::l_brace))
  1091. parseChildBlock();
  1092. break;
  1093. }
  1094. nextToken();
  1095. if (FormatTok->Tok.is(tok::l_brace)) {
  1096. nextToken();
  1097. parseBracedList();
  1098. } else if (Style.Language == FormatStyle::LK_Proto &&
  1099. FormatTok->Tok.is(tok::less)) {
  1100. nextToken();
  1101. parseBracedList(/*ContinueOnSemicolons=*/false,
  1102. /*ClosingBraceKind=*/tok::greater);
  1103. }
  1104. break;
  1105. case tok::l_square:
  1106. parseSquare();
  1107. break;
  1108. case tok::kw_new:
  1109. parseNew();
  1110. break;
  1111. default:
  1112. nextToken();
  1113. break;
  1114. }
  1115. } while (!eof());
  1116. }
  1117. bool UnwrappedLineParser::tryToParseLambda() {
  1118. if (!Style.isCpp()) {
  1119. nextToken();
  1120. return false;
  1121. }
  1122. const FormatToken* Previous = getPreviousToken();
  1123. if (Previous &&
  1124. (Previous->isOneOf(tok::identifier, tok::kw_operator, tok::kw_new,
  1125. tok::kw_delete) ||
  1126. Previous->closesScope() || Previous->isSimpleTypeSpecifier())) {
  1127. nextToken();
  1128. return false;
  1129. }
  1130. assert(FormatTok->is(tok::l_square));
  1131. FormatToken &LSquare = *FormatTok;
  1132. if (!tryToParseLambdaIntroducer())
  1133. return false;
  1134. while (FormatTok->isNot(tok::l_brace)) {
  1135. if (FormatTok->isSimpleTypeSpecifier()) {
  1136. nextToken();
  1137. continue;
  1138. }
  1139. switch (FormatTok->Tok.getKind()) {
  1140. case tok::l_brace:
  1141. break;
  1142. case tok::l_paren:
  1143. parseParens();
  1144. break;
  1145. case tok::amp:
  1146. case tok::star:
  1147. case tok::kw_const:
  1148. case tok::comma:
  1149. case tok::less:
  1150. case tok::greater:
  1151. case tok::identifier:
  1152. case tok::numeric_constant:
  1153. case tok::coloncolon:
  1154. case tok::kw_mutable:
  1155. nextToken();
  1156. break;
  1157. case tok::arrow:
  1158. FormatTok->Type = TT_LambdaArrow;
  1159. nextToken();
  1160. break;
  1161. default:
  1162. return true;
  1163. }
  1164. }
  1165. LSquare.Type = TT_LambdaLSquare;
  1166. parseChildBlock();
  1167. return true;
  1168. }
  1169. bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
  1170. nextToken();
  1171. if (FormatTok->is(tok::equal)) {
  1172. nextToken();
  1173. if (FormatTok->is(tok::r_square)) {
  1174. nextToken();
  1175. return true;
  1176. }
  1177. if (FormatTok->isNot(tok::comma))
  1178. return false;
  1179. nextToken();
  1180. } else if (FormatTok->is(tok::amp)) {
  1181. nextToken();
  1182. if (FormatTok->is(tok::r_square)) {
  1183. nextToken();
  1184. return true;
  1185. }
  1186. if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
  1187. return false;
  1188. }
  1189. if (FormatTok->is(tok::comma))
  1190. nextToken();
  1191. } else if (FormatTok->is(tok::r_square)) {
  1192. nextToken();
  1193. return true;
  1194. }
  1195. do {
  1196. if (FormatTok->is(tok::amp))
  1197. nextToken();
  1198. if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
  1199. return false;
  1200. nextToken();
  1201. if (FormatTok->is(tok::ellipsis))
  1202. nextToken();
  1203. if (FormatTok->is(tok::comma)) {
  1204. nextToken();
  1205. } else if (FormatTok->is(tok::r_square)) {
  1206. nextToken();
  1207. return true;
  1208. } else {
  1209. return false;
  1210. }
  1211. } while (!eof());
  1212. return false;
  1213. }
  1214. void UnwrappedLineParser::tryToParseJSFunction() {
  1215. assert(FormatTok->is(Keywords.kw_function) ||
  1216. FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function));
  1217. if (FormatTok->is(Keywords.kw_async))
  1218. nextToken();
  1219. // Consume "function".
  1220. nextToken();
  1221. // Consume * (generator function). Treat it like C++'s overloaded operators.
  1222. if (FormatTok->is(tok::star)) {
  1223. FormatTok->Type = TT_OverloadedOperator;
  1224. nextToken();
  1225. }
  1226. // Consume function name.
  1227. if (FormatTok->is(tok::identifier))
  1228. nextToken();
  1229. if (FormatTok->isNot(tok::l_paren))
  1230. return;
  1231. // Parse formal parameter list.
  1232. parseParens();
  1233. if (FormatTok->is(tok::colon)) {
  1234. // Parse a type definition.
  1235. nextToken();
  1236. // Eat the type declaration. For braced inline object types, balance braces,
  1237. // otherwise just parse until finding an l_brace for the function body.
  1238. if (FormatTok->is(tok::l_brace))
  1239. tryToParseBracedList();
  1240. else
  1241. while (!FormatTok->isOneOf(tok::l_brace, tok::semi) && !eof())
  1242. nextToken();
  1243. }
  1244. if (FormatTok->is(tok::semi))
  1245. return;
  1246. parseChildBlock();
  1247. }
  1248. bool UnwrappedLineParser::tryToParseBracedList() {
  1249. if (FormatTok->BlockKind == BK_Unknown)
  1250. calculateBraceTypes();
  1251. assert(FormatTok->BlockKind != BK_Unknown);
  1252. if (FormatTok->BlockKind == BK_Block)
  1253. return false;
  1254. nextToken();
  1255. parseBracedList();
  1256. return true;
  1257. }
  1258. bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons,
  1259. tok::TokenKind ClosingBraceKind) {
  1260. bool HasError = false;
  1261. // FIXME: Once we have an expression parser in the UnwrappedLineParser,
  1262. // replace this by using parseAssigmentExpression() inside.
  1263. do {
  1264. if (Style.Language == FormatStyle::LK_JavaScript) {
  1265. if (FormatTok->is(Keywords.kw_function) ||
  1266. FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)) {
  1267. tryToParseJSFunction();
  1268. continue;
  1269. }
  1270. if (FormatTok->is(TT_JsFatArrow)) {
  1271. nextToken();
  1272. // Fat arrows can be followed by simple expressions or by child blocks
  1273. // in curly braces.
  1274. if (FormatTok->is(tok::l_brace)) {
  1275. parseChildBlock();
  1276. continue;
  1277. }
  1278. }
  1279. if (FormatTok->is(tok::l_brace)) {
  1280. // Could be a method inside of a braced list `{a() { return 1; }}`.
  1281. if (tryToParseBracedList())
  1282. continue;
  1283. parseChildBlock();
  1284. }
  1285. }
  1286. if (FormatTok->Tok.getKind() == ClosingBraceKind) {
  1287. nextToken();
  1288. return !HasError;
  1289. }
  1290. switch (FormatTok->Tok.getKind()) {
  1291. case tok::caret:
  1292. nextToken();
  1293. if (FormatTok->is(tok::l_brace)) {
  1294. parseChildBlock();
  1295. }
  1296. break;
  1297. case tok::l_square:
  1298. tryToParseLambda();
  1299. break;
  1300. case tok::l_paren:
  1301. parseParens();
  1302. // JavaScript can just have free standing methods and getters/setters in
  1303. // object literals. Detect them by a "{" following ")".
  1304. if (Style.Language == FormatStyle::LK_JavaScript) {
  1305. if (FormatTok->is(tok::l_brace))
  1306. parseChildBlock();
  1307. break;
  1308. }
  1309. break;
  1310. case tok::l_brace:
  1311. // Assume there are no blocks inside a braced init list apart
  1312. // from the ones we explicitly parse out (like lambdas).
  1313. FormatTok->BlockKind = BK_BracedInit;
  1314. nextToken();
  1315. parseBracedList();
  1316. break;
  1317. case tok::semi:
  1318. // JavaScript (or more precisely TypeScript) can have semicolons in braced
  1319. // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be
  1320. // used for error recovery if we have otherwise determined that this is
  1321. // a braced list.
  1322. if (Style.Language == FormatStyle::LK_JavaScript) {
  1323. nextToken();
  1324. break;
  1325. }
  1326. HasError = true;
  1327. if (!ContinueOnSemicolons)
  1328. return !HasError;
  1329. nextToken();
  1330. break;
  1331. case tok::comma:
  1332. nextToken();
  1333. break;
  1334. default:
  1335. nextToken();
  1336. break;
  1337. }
  1338. } while (!eof());
  1339. return false;
  1340. }
  1341. void UnwrappedLineParser::parseParens() {
  1342. assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
  1343. nextToken();
  1344. do {
  1345. switch (FormatTok->Tok.getKind()) {
  1346. case tok::l_paren:
  1347. parseParens();
  1348. if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
  1349. parseChildBlock();
  1350. break;
  1351. case tok::r_paren:
  1352. nextToken();
  1353. return;
  1354. case tok::r_brace:
  1355. // A "}" inside parenthesis is an error if there wasn't a matching "{".
  1356. return;
  1357. case tok::l_square:
  1358. tryToParseLambda();
  1359. break;
  1360. case tok::l_brace:
  1361. if (!tryToParseBracedList())
  1362. parseChildBlock();
  1363. break;
  1364. case tok::at:
  1365. nextToken();
  1366. if (FormatTok->Tok.is(tok::l_brace)) {
  1367. nextToken();
  1368. parseBracedList();
  1369. }
  1370. break;
  1371. case tok::kw_class:
  1372. if (Style.Language == FormatStyle::LK_JavaScript)
  1373. parseRecord(/*ParseAsExpr=*/true);
  1374. else
  1375. nextToken();
  1376. break;
  1377. case tok::identifier:
  1378. if (Style.Language == FormatStyle::LK_JavaScript &&
  1379. (FormatTok->is(Keywords.kw_function) ||
  1380. FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)))
  1381. tryToParseJSFunction();
  1382. else
  1383. nextToken();
  1384. break;
  1385. default:
  1386. nextToken();
  1387. break;
  1388. }
  1389. } while (!eof());
  1390. }
  1391. void UnwrappedLineParser::parseSquare() {
  1392. assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
  1393. if (tryToParseLambda())
  1394. return;
  1395. do {
  1396. switch (FormatTok->Tok.getKind()) {
  1397. case tok::l_paren:
  1398. parseParens();
  1399. break;
  1400. case tok::r_square:
  1401. nextToken();
  1402. return;
  1403. case tok::r_brace:
  1404. // A "}" inside parenthesis is an error if there wasn't a matching "{".
  1405. return;
  1406. case tok::l_square:
  1407. parseSquare();
  1408. break;
  1409. case tok::l_brace: {
  1410. if (!tryToParseBracedList())
  1411. parseChildBlock();
  1412. break;
  1413. }
  1414. case tok::at:
  1415. nextToken();
  1416. if (FormatTok->Tok.is(tok::l_brace)) {
  1417. nextToken();
  1418. parseBracedList();
  1419. }
  1420. break;
  1421. default:
  1422. nextToken();
  1423. break;
  1424. }
  1425. } while (!eof());
  1426. }
  1427. void UnwrappedLineParser::parseIfThenElse() {
  1428. assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
  1429. nextToken();
  1430. if (FormatTok->Tok.is(tok::kw_constexpr))
  1431. nextToken();
  1432. if (FormatTok->Tok.is(tok::l_paren))
  1433. parseParens();
  1434. bool NeedsUnwrappedLine = false;
  1435. if (FormatTok->Tok.is(tok::l_brace)) {
  1436. CompoundStatementIndenter Indenter(this, Style, Line->Level);
  1437. parseBlock(/*MustBeDeclaration=*/false);
  1438. if (Style.BraceWrapping.BeforeElse)
  1439. addUnwrappedLine();
  1440. else
  1441. NeedsUnwrappedLine = true;
  1442. } else {
  1443. addUnwrappedLine();
  1444. ++Line->Level;
  1445. parseStructuralElement();
  1446. --Line->Level;
  1447. }
  1448. if (FormatTok->Tok.is(tok::kw_else)) {
  1449. nextToken();
  1450. if (FormatTok->Tok.is(tok::l_brace)) {
  1451. CompoundStatementIndenter Indenter(this, Style, Line->Level);
  1452. parseBlock(/*MustBeDeclaration=*/false);
  1453. addUnwrappedLine();
  1454. } else if (FormatTok->Tok.is(tok::kw_if)) {
  1455. parseIfThenElse();
  1456. } else {
  1457. addUnwrappedLine();
  1458. ++Line->Level;
  1459. parseStructuralElement();
  1460. if (FormatTok->is(tok::eof))
  1461. addUnwrappedLine();
  1462. --Line->Level;
  1463. }
  1464. } else if (NeedsUnwrappedLine) {
  1465. addUnwrappedLine();
  1466. }
  1467. }
  1468. void UnwrappedLineParser::parseTryCatch() {
  1469. assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
  1470. nextToken();
  1471. bool NeedsUnwrappedLine = false;
  1472. if (FormatTok->is(tok::colon)) {
  1473. // We are in a function try block, what comes is an initializer list.
  1474. nextToken();
  1475. while (FormatTok->is(tok::identifier)) {
  1476. nextToken();
  1477. if (FormatTok->is(tok::l_paren))
  1478. parseParens();
  1479. if (FormatTok->is(tok::comma))
  1480. nextToken();
  1481. }
  1482. }
  1483. // Parse try with resource.
  1484. if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
  1485. parseParens();
  1486. }
  1487. if (FormatTok->is(tok::l_brace)) {
  1488. CompoundStatementIndenter Indenter(this, Style, Line->Level);
  1489. parseBlock(/*MustBeDeclaration=*/false);
  1490. if (Style.BraceWrapping.BeforeCatch) {
  1491. addUnwrappedLine();
  1492. } else {
  1493. NeedsUnwrappedLine = true;
  1494. }
  1495. } else if (!FormatTok->is(tok::kw_catch)) {
  1496. // The C++ standard requires a compound-statement after a try.
  1497. // If there's none, we try to assume there's a structuralElement
  1498. // and try to continue.
  1499. addUnwrappedLine();
  1500. ++Line->Level;
  1501. parseStructuralElement();
  1502. --Line->Level;
  1503. }
  1504. while (1) {
  1505. if (FormatTok->is(tok::at))
  1506. nextToken();
  1507. if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
  1508. tok::kw___finally) ||
  1509. ((Style.Language == FormatStyle::LK_Java ||
  1510. Style.Language == FormatStyle::LK_JavaScript) &&
  1511. FormatTok->is(Keywords.kw_finally)) ||
  1512. (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
  1513. FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
  1514. break;
  1515. nextToken();
  1516. while (FormatTok->isNot(tok::l_brace)) {
  1517. if (FormatTok->is(tok::l_paren)) {
  1518. parseParens();
  1519. continue;
  1520. }
  1521. if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
  1522. return;
  1523. nextToken();
  1524. }
  1525. NeedsUnwrappedLine = false;
  1526. CompoundStatementIndenter Indenter(this, Style, Line->Level);
  1527. parseBlock(/*MustBeDeclaration=*/false);
  1528. if (Style.BraceWrapping.BeforeCatch)
  1529. addUnwrappedLine();
  1530. else
  1531. NeedsUnwrappedLine = true;
  1532. }
  1533. if (NeedsUnwrappedLine)
  1534. addUnwrappedLine();
  1535. }
  1536. void UnwrappedLineParser::parseNamespace() {
  1537. assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
  1538. const FormatToken &InitialToken = *FormatTok;
  1539. nextToken();
  1540. while (FormatTok->isOneOf(tok::identifier, tok::coloncolon))
  1541. nextToken();
  1542. if (FormatTok->Tok.is(tok::l_brace)) {
  1543. if (ShouldBreakBeforeBrace(Style, InitialToken))
  1544. addUnwrappedLine();
  1545. bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
  1546. (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
  1547. DeclarationScopeStack.size() > 1);
  1548. parseBlock(/*MustBeDeclaration=*/true, AddLevel);
  1549. // Munch the semicolon after a namespace. This is more common than one would
  1550. // think. Puttin the semicolon into its own line is very ugly.
  1551. if (FormatTok->Tok.is(tok::semi))
  1552. nextToken();
  1553. addUnwrappedLine();
  1554. }
  1555. // FIXME: Add error handling.
  1556. }
  1557. void UnwrappedLineParser::parseNew() {
  1558. assert(FormatTok->is(tok::kw_new) && "'new' expected");
  1559. nextToken();
  1560. if (Style.Language != FormatStyle::LK_Java)
  1561. return;
  1562. // In Java, we can parse everything up to the parens, which aren't optional.
  1563. do {
  1564. // There should not be a ;, { or } before the new's open paren.
  1565. if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
  1566. return;
  1567. // Consume the parens.
  1568. if (FormatTok->is(tok::l_paren)) {
  1569. parseParens();
  1570. // If there is a class body of an anonymous class, consume that as child.
  1571. if (FormatTok->is(tok::l_brace))
  1572. parseChildBlock();
  1573. return;
  1574. }
  1575. nextToken();
  1576. } while (!eof());
  1577. }
  1578. void UnwrappedLineParser::parseForOrWhileLoop() {
  1579. assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
  1580. "'for', 'while' or foreach macro expected");
  1581. nextToken();
  1582. // JS' for await ( ...
  1583. if (Style.Language == FormatStyle::LK_JavaScript &&
  1584. FormatTok->is(Keywords.kw_await))
  1585. nextToken();
  1586. if (FormatTok->Tok.is(tok::l_paren))
  1587. parseParens();
  1588. if (FormatTok->Tok.is(tok::l_brace)) {
  1589. CompoundStatementIndenter Indenter(this, Style, Line->Level);
  1590. parseBlock(/*MustBeDeclaration=*/false);
  1591. addUnwrappedLine();
  1592. } else {
  1593. addUnwrappedLine();
  1594. ++Line->Level;
  1595. parseStructuralElement();
  1596. --Line->Level;
  1597. }
  1598. }
  1599. void UnwrappedLineParser::parseDoWhile() {
  1600. assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
  1601. nextToken();
  1602. if (FormatTok->Tok.is(tok::l_brace)) {
  1603. CompoundStatementIndenter Indenter(this, Style, Line->Level);
  1604. parseBlock(/*MustBeDeclaration=*/false);
  1605. if (Style.BraceWrapping.IndentBraces)
  1606. addUnwrappedLine();
  1607. } else {
  1608. addUnwrappedLine();
  1609. ++Line->Level;
  1610. parseStructuralElement();
  1611. --Line->Level;
  1612. }
  1613. // FIXME: Add error handling.
  1614. if (!FormatTok->Tok.is(tok::kw_while)) {
  1615. addUnwrappedLine();
  1616. return;
  1617. }
  1618. nextToken();
  1619. parseStructuralElement();
  1620. }
  1621. void UnwrappedLineParser::parseLabel() {
  1622. nextToken();
  1623. unsigned OldLineLevel = Line->Level;
  1624. if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
  1625. --Line->Level;
  1626. if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
  1627. CompoundStatementIndenter Indenter(this, Style, Line->Level);
  1628. parseBlock(/*MustBeDeclaration=*/false);
  1629. if (FormatTok->Tok.is(tok::kw_break)) {
  1630. if (Style.BraceWrapping.AfterControlStatement)
  1631. addUnwrappedLine();
  1632. parseStructuralElement();
  1633. }
  1634. addUnwrappedLine();
  1635. } else {
  1636. if (FormatTok->is(tok::semi))
  1637. nextToken();
  1638. addUnwrappedLine();
  1639. }
  1640. Line->Level = OldLineLevel;
  1641. if (FormatTok->isNot(tok::l_brace)) {
  1642. parseStructuralElement();
  1643. addUnwrappedLine();
  1644. }
  1645. }
  1646. void UnwrappedLineParser::parseCaseLabel() {
  1647. assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
  1648. // FIXME: fix handling of complex expressions here.
  1649. do {
  1650. nextToken();
  1651. } while (!eof() && !FormatTok->Tok.is(tok::colon));
  1652. parseLabel();
  1653. }
  1654. void UnwrappedLineParser::parseSwitch() {
  1655. assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
  1656. nextToken();
  1657. if (FormatTok->Tok.is(tok::l_paren))
  1658. parseParens();
  1659. if (FormatTok->Tok.is(tok::l_brace)) {
  1660. CompoundStatementIndenter Indenter(this, Style, Line->Level);
  1661. parseBlock(/*MustBeDeclaration=*/false);
  1662. addUnwrappedLine();
  1663. } else {
  1664. addUnwrappedLine();
  1665. ++Line->Level;
  1666. parseStructuralElement();
  1667. --Line->Level;
  1668. }
  1669. }
  1670. void UnwrappedLineParser::parseAccessSpecifier() {
  1671. nextToken();
  1672. // Understand Qt's slots.
  1673. if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
  1674. nextToken();
  1675. // Otherwise, we don't know what it is, and we'd better keep the next token.
  1676. if (FormatTok->Tok.is(tok::colon))
  1677. nextToken();
  1678. addUnwrappedLine();
  1679. }
  1680. bool UnwrappedLineParser::parseEnum() {
  1681. // Won't be 'enum' for NS_ENUMs.
  1682. if (FormatTok->Tok.is(tok::kw_enum))
  1683. nextToken();
  1684. // In TypeScript, "enum" can also be used as property name, e.g. in interface
  1685. // declarations. An "enum" keyword followed by a colon would be a syntax
  1686. // error and thus assume it is just an identifier.
  1687. if (Style.Language == FormatStyle::LK_JavaScript &&
  1688. FormatTok->isOneOf(tok::colon, tok::question))
  1689. return false;
  1690. // Eat up enum class ...
  1691. if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
  1692. nextToken();
  1693. while (FormatTok->Tok.getIdentifierInfo() ||
  1694. FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
  1695. tok::greater, tok::comma, tok::question)) {
  1696. nextToken();
  1697. // We can have macros or attributes in between 'enum' and the enum name.
  1698. if (FormatTok->is(tok::l_paren))
  1699. parseParens();
  1700. if (FormatTok->is(tok::identifier)) {
  1701. nextToken();
  1702. // If there are two identifiers in a row, this is likely an elaborate
  1703. // return type. In Java, this can be "implements", etc.
  1704. if (Style.isCpp() && FormatTok->is(tok::identifier))
  1705. return false;
  1706. }
  1707. }
  1708. // Just a declaration or something is wrong.
  1709. if (FormatTok->isNot(tok::l_brace))
  1710. return true;
  1711. FormatTok->BlockKind = BK_Block;
  1712. if (Style.Language == FormatStyle::LK_Java) {
  1713. // Java enums are different.
  1714. parseJavaEnumBody();
  1715. return true;
  1716. }
  1717. if (Style.Language == FormatStyle::LK_Proto) {
  1718. parseBlock(/*MustBeDeclaration=*/true);
  1719. return true;
  1720. }
  1721. // Parse enum body.
  1722. nextToken();
  1723. bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
  1724. if (HasError) {
  1725. if (FormatTok->is(tok::semi))
  1726. nextToken();
  1727. addUnwrappedLine();
  1728. }
  1729. return true;
  1730. // There is no addUnwrappedLine() here so that we fall through to parsing a
  1731. // structural element afterwards. Thus, in "enum A {} n, m;",
  1732. // "} n, m;" will end up in one unwrapped line.
  1733. }
  1734. void UnwrappedLineParser::parseJavaEnumBody() {
  1735. // Determine whether the enum is simple, i.e. does not have a semicolon or
  1736. // constants with class bodies. Simple enums can be formatted like braced
  1737. // lists, contracted to a single line, etc.
  1738. unsigned StoredPosition = Tokens->getPosition();
  1739. bool IsSimple = true;
  1740. FormatToken *Tok = Tokens->getNextToken();
  1741. while (Tok) {
  1742. if (Tok->is(tok::r_brace))
  1743. break;
  1744. if (Tok->isOneOf(tok::l_brace, tok::semi)) {
  1745. IsSimple = false;
  1746. break;
  1747. }
  1748. // FIXME: This will also mark enums with braces in the arguments to enum
  1749. // constants as "not simple". This is probably fine in practice, though.
  1750. Tok = Tokens->getNextToken();
  1751. }
  1752. FormatTok = Tokens->setPosition(StoredPosition);
  1753. if (IsSimple) {
  1754. nextToken();
  1755. parseBracedList();
  1756. addUnwrappedLine();
  1757. return;
  1758. }
  1759. // Parse the body of a more complex enum.
  1760. // First add a line for everything up to the "{".
  1761. nextToken();
  1762. addUnwrappedLine();
  1763. ++Line->Level;
  1764. // Parse the enum constants.
  1765. while (FormatTok) {
  1766. if (FormatTok->is(tok::l_brace)) {
  1767. // Parse the constant's class body.
  1768. parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
  1769. /*MunchSemi=*/false);
  1770. } else if (FormatTok->is(tok::l_paren)) {
  1771. parseParens();
  1772. } else if (FormatTok->is(tok::comma)) {
  1773. nextToken();
  1774. addUnwrappedLine();
  1775. } else if (FormatTok->is(tok::semi)) {
  1776. nextToken();
  1777. addUnwrappedLine();
  1778. break;
  1779. } else if (FormatTok->is(tok::r_brace)) {
  1780. addUnwrappedLine();
  1781. break;
  1782. } else {
  1783. nextToken();
  1784. }
  1785. }
  1786. // Parse the class body after the enum's ";" if any.
  1787. parseLevel(/*HasOpeningBrace=*/true);
  1788. nextToken();
  1789. --Line->Level;
  1790. addUnwrappedLine();
  1791. }
  1792. void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
  1793. const FormatToken &InitialToken = *FormatTok;
  1794. nextToken();
  1795. // The actual identifier can be a nested name specifier, and in macros
  1796. // it is often token-pasted.
  1797. while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
  1798. tok::kw___attribute, tok::kw___declspec,
  1799. tok::kw_alignas) ||
  1800. ((Style.Language == FormatStyle::LK_Java ||
  1801. Style.Language == FormatStyle::LK_JavaScript) &&
  1802. FormatTok->isOneOf(tok::period, tok::comma))) {
  1803. bool IsNonMacroIdentifier =
  1804. FormatTok->is(tok::identifier) &&
  1805. FormatTok->TokenText != FormatTok->TokenText.upper();
  1806. nextToken();
  1807. // We can have macros or attributes in between 'class' and the class name.
  1808. if (!IsNonMacroIdentifier && FormatTok->Tok.is(tok::l_paren))
  1809. parseParens();
  1810. }
  1811. // Note that parsing away template declarations here leads to incorrectly
  1812. // accepting function declarations as record declarations.
  1813. // In general, we cannot solve this problem. Consider:
  1814. // class A<int> B() {}
  1815. // which can be a function definition or a class definition when B() is a
  1816. // macro. If we find enough real-world cases where this is a problem, we
  1817. // can parse for the 'template' keyword in the beginning of the statement,
  1818. // and thus rule out the record production in case there is no template
  1819. // (this would still leave us with an ambiguity between template function
  1820. // and class declarations).
  1821. if (FormatTok->isOneOf(tok::colon, tok::less)) {
  1822. while (!eof()) {
  1823. if (FormatTok->is(tok::l_brace)) {
  1824. calculateBraceTypes(/*ExpectClassBody=*/true);
  1825. if (!tryToParseBracedList())
  1826. break;
  1827. }
  1828. if (FormatTok->Tok.is(tok::semi))
  1829. return;
  1830. nextToken();
  1831. }
  1832. }
  1833. if (FormatTok->Tok.is(tok::l_brace)) {
  1834. if (ParseAsExpr) {
  1835. parseChildBlock();
  1836. } else {
  1837. if (ShouldBreakBeforeBrace(Style, InitialToken))
  1838. addUnwrappedLine();
  1839. parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
  1840. /*MunchSemi=*/false);
  1841. }
  1842. }
  1843. // There is no addUnwrappedLine() here so that we fall through to parsing a
  1844. // structural element afterwards. Thus, in "class A {} n, m;",
  1845. // "} n, m;" will end up in one unwrapped line.
  1846. }
  1847. void UnwrappedLineParser::parseObjCProtocolList() {
  1848. assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
  1849. do
  1850. nextToken();
  1851. while (!eof() && FormatTok->Tok.isNot(tok::greater));
  1852. nextToken(); // Skip '>'.
  1853. }
  1854. void UnwrappedLineParser::parseObjCUntilAtEnd() {
  1855. do {
  1856. if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
  1857. nextToken();
  1858. addUnwrappedLine();
  1859. break;
  1860. }
  1861. if (FormatTok->is(tok::l_brace)) {
  1862. parseBlock(/*MustBeDeclaration=*/false);
  1863. // In ObjC interfaces, nothing should be following the "}".
  1864. addUnwrappedLine();
  1865. } else if (FormatTok->is(tok::r_brace)) {
  1866. // Ignore stray "}". parseStructuralElement doesn't consume them.
  1867. nextToken();
  1868. addUnwrappedLine();
  1869. } else {
  1870. parseStructuralElement();
  1871. }
  1872. } while (!eof());
  1873. }
  1874. void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
  1875. nextToken();
  1876. nextToken(); // interface name
  1877. // @interface can be followed by either a base class, or a category.
  1878. if (FormatTok->Tok.is(tok::colon)) {
  1879. nextToken();
  1880. nextToken(); // base class name
  1881. } else if (FormatTok->Tok.is(tok::l_paren))
  1882. // Skip category, if present.
  1883. parseParens();
  1884. if (FormatTok->Tok.is(tok::less))
  1885. parseObjCProtocolList();
  1886. if (FormatTok->Tok.is(tok::l_brace)) {
  1887. if (Style.BraceWrapping.AfterObjCDeclaration)
  1888. addUnwrappedLine();
  1889. parseBlock(/*MustBeDeclaration=*/true);
  1890. }
  1891. // With instance variables, this puts '}' on its own line. Without instance
  1892. // variables, this ends the @interface line.
  1893. addUnwrappedLine();
  1894. parseObjCUntilAtEnd();
  1895. }
  1896. void UnwrappedLineParser::parseObjCProtocol() {
  1897. nextToken();
  1898. nextToken(); // protocol name
  1899. if (FormatTok->Tok.is(tok::less))
  1900. parseObjCProtocolList();
  1901. // Check for protocol declaration.
  1902. if (FormatTok->Tok.is(tok::semi)) {
  1903. nextToken();
  1904. return addUnwrappedLine();
  1905. }
  1906. addUnwrappedLine();
  1907. parseObjCUntilAtEnd();
  1908. }
  1909. void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
  1910. bool IsImport = FormatTok->is(Keywords.kw_import);
  1911. assert(IsImport || FormatTok->is(tok::kw_export));
  1912. nextToken();
  1913. // Consume the "default" in "export default class/function".
  1914. if (FormatTok->is(tok::kw_default))
  1915. nextToken();
  1916. // Consume "async function", "function" and "default function", so that these
  1917. // get parsed as free-standing JS functions, i.e. do not require a trailing
  1918. // semicolon.
  1919. if (FormatTok->is(Keywords.kw_async))
  1920. nextToken();
  1921. if (FormatTok->is(Keywords.kw_function)) {
  1922. nextToken();
  1923. return;
  1924. }
  1925. // For imports, `export *`, `export {...}`, consume the rest of the line up
  1926. // to the terminating `;`. For everything else, just return and continue
  1927. // parsing the structural element, i.e. the declaration or expression for
  1928. // `export default`.
  1929. if (!IsImport && !FormatTok->isOneOf(tok::l_brace, tok::star) &&
  1930. !FormatTok->isStringLiteral())
  1931. return;
  1932. while (!eof()) {
  1933. if (FormatTok->is(tok::semi))
  1934. return;
  1935. if (Line->Tokens.size() == 0) {
  1936. // Common issue: Automatic Semicolon Insertion wrapped the line, so the
  1937. // import statement should terminate.
  1938. return;
  1939. }
  1940. if (FormatTok->is(tok::l_brace)) {
  1941. FormatTok->BlockKind = BK_Block;
  1942. nextToken();
  1943. parseBracedList();
  1944. } else {
  1945. nextToken();
  1946. }
  1947. }
  1948. }
  1949. LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
  1950. StringRef Prefix = "") {
  1951. llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
  1952. << (Line.InPPDirective ? " MACRO" : "") << ": ";
  1953. for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
  1954. E = Line.Tokens.end();
  1955. I != E; ++I) {
  1956. llvm::dbgs() << I->Tok->Tok.getName() << "["
  1957. << "T=" << I->Tok->Type
  1958. << ", OC=" << I->Tok->OriginalColumn << "] ";
  1959. }
  1960. for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
  1961. E = Line.Tokens.end();
  1962. I != E; ++I) {
  1963. const UnwrappedLineNode &Node = *I;
  1964. for (SmallVectorImpl<UnwrappedLine>::const_iterator
  1965. I = Node.Children.begin(),
  1966. E = Node.Children.end();
  1967. I != E; ++I) {
  1968. printDebugInfo(*I, "\nChild: ");
  1969. }
  1970. }
  1971. llvm::dbgs() << "\n";
  1972. }
  1973. void UnwrappedLineParser::addUnwrappedLine() {
  1974. if (Line->Tokens.empty())
  1975. return;
  1976. DEBUG({
  1977. if (CurrentLines == &Lines)
  1978. printDebugInfo(*Line);
  1979. });
  1980. CurrentLines->push_back(std::move(*Line));
  1981. Line->Tokens.clear();
  1982. Line->MatchingOpeningBlockLineIndex = UnwrappedLine::kInvalidIndex;
  1983. if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
  1984. CurrentLines->append(
  1985. std::make_move_iterator(PreprocessorDirectives.begin()),
  1986. std::make_move_iterator(PreprocessorDirectives.end()));
  1987. PreprocessorDirectives.clear();
  1988. }
  1989. }
  1990. bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
  1991. bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
  1992. return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
  1993. FormatTok.NewlinesBefore > 0;
  1994. }
  1995. // Checks if \p FormatTok is a line comment that continues the line comment
  1996. // section on \p Line.
  1997. static bool continuesLineCommentSection(const FormatToken &FormatTok,
  1998. const UnwrappedLine &Line,
  1999. llvm::Regex &CommentPragmasRegex) {
  2000. if (Line.Tokens.empty())
  2001. return false;
  2002. StringRef IndentContent = FormatTok.TokenText;
  2003. if (FormatTok.TokenText.startswith("//") ||
  2004. FormatTok.TokenText.startswith("/*"))
  2005. IndentContent = FormatTok.TokenText.substr(2);
  2006. if (CommentPragmasRegex.match(IndentContent))
  2007. return false;
  2008. // If Line starts with a line comment, then FormatTok continues the comment
  2009. // section if its original column is greater or equal to the original start
  2010. // column of the line.
  2011. //
  2012. // Define the min column token of a line as follows: if a line ends in '{' or
  2013. // contains a '{' followed by a line comment, then the min column token is
  2014. // that '{'. Otherwise, the min column token of the line is the first token of
  2015. // the line.
  2016. //
  2017. // If Line starts with a token other than a line comment, then FormatTok
  2018. // continues the comment section if its original column is greater than the
  2019. // original start column of the min column token of the line.
  2020. //
  2021. // For example, the second line comment continues the first in these cases:
  2022. //
  2023. // // first line
  2024. // // second line
  2025. //
  2026. // and:
  2027. //
  2028. // // first line
  2029. // // second line
  2030. //
  2031. // and:
  2032. //
  2033. // int i; // first line
  2034. // // second line
  2035. //
  2036. // and:
  2037. //
  2038. // do { // first line
  2039. // // second line
  2040. // int i;
  2041. // } while (true);
  2042. //
  2043. // and:
  2044. //
  2045. // enum {
  2046. // a, // first line
  2047. // // second line
  2048. // b
  2049. // };
  2050. //
  2051. // The second line comment doesn't continue the first in these cases:
  2052. //
  2053. // // first line
  2054. // // second line
  2055. //
  2056. // and:
  2057. //
  2058. // int i; // first line
  2059. // // second line
  2060. //
  2061. // and:
  2062. //
  2063. // do { // first line
  2064. // // second line
  2065. // int i;
  2066. // } while (true);
  2067. //
  2068. // and:
  2069. //
  2070. // enum {
  2071. // a, // first line
  2072. // // second line
  2073. // };
  2074. const FormatToken *MinColumnToken = Line.Tokens.front().Tok;
  2075. // Scan for '{//'. If found, use the column of '{' as a min column for line
  2076. // comment section continuation.
  2077. const FormatToken *PreviousToken = nullptr;
  2078. for (const UnwrappedLineNode &Node : Line.Tokens) {
  2079. if (PreviousToken && PreviousToken->is(tok::l_brace) &&
  2080. isLineComment(*Node.Tok)) {
  2081. MinColumnToken = PreviousToken;
  2082. break;
  2083. }
  2084. PreviousToken = Node.Tok;
  2085. // Grab the last newline preceding a token in this unwrapped line.
  2086. if (Node.Tok->NewlinesBefore > 0) {
  2087. MinColumnToken = Node.Tok;
  2088. }
  2089. }
  2090. if (PreviousToken && PreviousToken->is(tok::l_brace)) {
  2091. MinColumnToken = PreviousToken;
  2092. }
  2093. return continuesLineComment(FormatTok, /*Previous=*/Line.Tokens.back().Tok,
  2094. MinColumnToken);
  2095. }
  2096. void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
  2097. bool JustComments = Line->Tokens.empty();
  2098. for (SmallVectorImpl<FormatToken *>::const_iterator
  2099. I = CommentsBeforeNextToken.begin(),
  2100. E = CommentsBeforeNextToken.end();
  2101. I != E; ++I) {
  2102. // Line comments that belong to the same line comment section are put on the
  2103. // same line since later we might want to reflow content between them.
  2104. // Additional fine-grained breaking of line comment sections is controlled
  2105. // by the class BreakableLineCommentSection in case it is desirable to keep
  2106. // several line comment sections in the same unwrapped line.
  2107. //
  2108. // FIXME: Consider putting separate line comment sections as children to the
  2109. // unwrapped line instead.
  2110. (*I)->ContinuesLineCommentSection =
  2111. continuesLineCommentSection(**I, *Line, CommentPragmasRegex);
  2112. if (isOnNewLine(**I) && JustComments && !(*I)->ContinuesLineCommentSection)
  2113. addUnwrappedLine();
  2114. pushToken(*I);
  2115. }
  2116. if (NewlineBeforeNext && JustComments)
  2117. addUnwrappedLine();
  2118. CommentsBeforeNextToken.clear();
  2119. }
  2120. void UnwrappedLineParser::nextToken() {
  2121. if (eof())
  2122. return;
  2123. flushComments(isOnNewLine(*FormatTok));
  2124. pushToken(FormatTok);
  2125. if (Style.Language != FormatStyle::LK_JavaScript)
  2126. readToken();
  2127. else
  2128. readTokenWithJavaScriptASI();
  2129. }
  2130. const FormatToken *UnwrappedLineParser::getPreviousToken() {
  2131. // FIXME: This is a dirty way to access the previous token. Find a better
  2132. // solution.
  2133. if (!Line || Line->Tokens.empty())
  2134. return nullptr;
  2135. return Line->Tokens.back().Tok;
  2136. }
  2137. void UnwrappedLineParser::distributeComments(
  2138. const SmallVectorImpl<FormatToken *> &Comments,
  2139. const FormatToken *NextTok) {
  2140. // Whether or not a line comment token continues a line is controlled by
  2141. // the method continuesLineCommentSection, with the following caveat:
  2142. //
  2143. // Define a trail of Comments to be a nonempty proper postfix of Comments such
  2144. // that each comment line from the trail is aligned with the next token, if
  2145. // the next token exists. If a trail exists, the beginning of the maximal
  2146. // trail is marked as a start of a new comment section.
  2147. //
  2148. // For example in this code:
  2149. //
  2150. // int a; // line about a
  2151. // // line 1 about b
  2152. // // line 2 about b
  2153. // int b;
  2154. //
  2155. // the two lines about b form a maximal trail, so there are two sections, the
  2156. // first one consisting of the single comment "// line about a" and the
  2157. // second one consisting of the next two comments.
  2158. if (Comments.empty())
  2159. return;
  2160. bool ShouldPushCommentsInCurrentLine = true;
  2161. bool HasTrailAlignedWithNextToken = false;
  2162. unsigned StartOfTrailAlignedWithNextToken = 0;
  2163. if (NextTok) {
  2164. // We are skipping the first element intentionally.
  2165. for (unsigned i = Comments.size() - 1; i > 0; --i) {
  2166. if (Comments[i]->OriginalColumn == NextTok->OriginalColumn) {
  2167. HasTrailAlignedWithNextToken = true;
  2168. StartOfTrailAlignedWithNextToken = i;
  2169. }
  2170. }
  2171. }
  2172. for (unsigned i = 0, e = Comments.size(); i < e; ++i) {
  2173. FormatToken *FormatTok = Comments[i];
  2174. if (HasTrailAlignedWithNextToken &&
  2175. i == StartOfTrailAlignedWithNextToken) {
  2176. FormatTok->ContinuesLineCommentSection = false;
  2177. } else {
  2178. FormatTok->ContinuesLineCommentSection =
  2179. continuesLineCommentSection(*FormatTok, *Line, CommentPragmasRegex);
  2180. }
  2181. if (!FormatTok->ContinuesLineCommentSection &&
  2182. (isOnNewLine(*FormatTok) || FormatTok->IsFirst)) {
  2183. ShouldPushCommentsInCurrentLine = false;
  2184. }
  2185. if (ShouldPushCommentsInCurrentLine) {
  2186. pushToken(FormatTok);
  2187. } else {
  2188. CommentsBeforeNextToken.push_back(FormatTok);
  2189. }
  2190. }
  2191. }
  2192. void UnwrappedLineParser::readToken() {
  2193. SmallVector<FormatToken *, 1> Comments;
  2194. do {
  2195. FormatTok = Tokens->getNextToken();
  2196. assert(FormatTok);
  2197. while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
  2198. (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
  2199. distributeComments(Comments, FormatTok);
  2200. Comments.clear();
  2201. // If there is an unfinished unwrapped line, we flush the preprocessor
  2202. // directives only after that unwrapped line was finished later.
  2203. bool SwitchToPreprocessorLines = !Line->Tokens.empty();
  2204. ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
  2205. // Comments stored before the preprocessor directive need to be output
  2206. // before the preprocessor directive, at the same level as the
  2207. // preprocessor directive, as we consider them to apply to the directive.
  2208. flushComments(isOnNewLine(*FormatTok));
  2209. parsePPDirective();
  2210. }
  2211. while (FormatTok->Type == TT_ConflictStart ||
  2212. FormatTok->Type == TT_ConflictEnd ||
  2213. FormatTok->Type == TT_ConflictAlternative) {
  2214. if (FormatTok->Type == TT_ConflictStart) {
  2215. conditionalCompilationStart(/*Unreachable=*/false);
  2216. } else if (FormatTok->Type == TT_ConflictAlternative) {
  2217. conditionalCompilationAlternative();
  2218. } else if (FormatTok->Type == TT_ConflictEnd) {
  2219. conditionalCompilationEnd();
  2220. }
  2221. FormatTok = Tokens->getNextToken();
  2222. FormatTok->MustBreakBefore = true;
  2223. }
  2224. if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
  2225. !Line->InPPDirective) {
  2226. continue;
  2227. }
  2228. if (!FormatTok->Tok.is(tok::comment)) {
  2229. distributeComments(Comments, FormatTok);
  2230. Comments.clear();
  2231. return;
  2232. }
  2233. Comments.push_back(FormatTok);
  2234. } while (!eof());
  2235. distributeComments(Comments, nullptr);
  2236. Comments.clear();
  2237. }
  2238. void UnwrappedLineParser::pushToken(FormatToken *Tok) {
  2239. Line->Tokens.push_back(UnwrappedLineNode(Tok));
  2240. if (MustBreakBeforeNextToken) {
  2241. Line->Tokens.back().Tok->MustBreakBefore = true;
  2242. MustBreakBeforeNextToken = false;
  2243. }
  2244. }
  2245. } // end namespace format
  2246. } // end namespace clang