UnwrappedLineParser.cpp 59 KB

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