UnwrappedLineParser.cpp 49 KB

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