UnwrappedLineParser.cpp 64 KB

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