UnwrappedLineParser.cpp 53 KB

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