UnwrappedLineParser.cpp 86 KB

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