TokenAnnotator.cpp 179 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560
  1. //===--- TokenAnnotator.cpp - Format C++ code -----------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. ///
  9. /// \file
  10. /// This file implements a token annotator, i.e. creates
  11. /// \c AnnotatedTokens out of \c FormatTokens with required extra information.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #include "TokenAnnotator.h"
  15. #include "FormatToken.h"
  16. #include "clang/Basic/SourceManager.h"
  17. #include "clang/Basic/TokenKinds.h"
  18. #include "llvm/ADT/SmallPtrSet.h"
  19. #include "llvm/Support/Debug.h"
  20. #define DEBUG_TYPE "format-token-annotator"
  21. namespace clang {
  22. namespace format {
  23. namespace {
  24. /// Returns \c true if the token can be used as an identifier in
  25. /// an Objective-C \c \@selector, \c false otherwise.
  26. ///
  27. /// Because getFormattingLangOpts() always lexes source code as
  28. /// Objective-C++, C++ keywords like \c new and \c delete are
  29. /// lexed as tok::kw_*, not tok::identifier, even for Objective-C.
  30. ///
  31. /// For Objective-C and Objective-C++, both identifiers and keywords
  32. /// are valid inside @selector(...) (or a macro which
  33. /// invokes @selector(...)). So, we allow treat any identifier or
  34. /// keyword as a potential Objective-C selector component.
  35. static bool canBeObjCSelectorComponent(const FormatToken &Tok) {
  36. return Tok.Tok.getIdentifierInfo() != nullptr;
  37. }
  38. /// With `Left` being '(', check if we're at either `[...](` or
  39. /// `[...]<...>(`, where the [ opens a lambda capture list.
  40. static bool isLambdaParameterList(const FormatToken *Left) {
  41. // Skip <...> if present.
  42. if (Left->Previous && Left->Previous->is(tok::greater) &&
  43. Left->Previous->MatchingParen &&
  44. Left->Previous->MatchingParen->is(TT_TemplateOpener))
  45. Left = Left->Previous->MatchingParen;
  46. // Check for `[...]`.
  47. return Left->Previous && Left->Previous->is(tok::r_square) &&
  48. Left->Previous->MatchingParen &&
  49. Left->Previous->MatchingParen->is(TT_LambdaLSquare);
  50. }
  51. /// Returns \c true if the token is followed by a boolean condition, \c false
  52. /// otherwise.
  53. static bool isKeywordWithCondition(const FormatToken &Tok) {
  54. return Tok.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
  55. tok::kw_constexpr, tok::kw_catch);
  56. }
  57. /// A parser that gathers additional information about tokens.
  58. ///
  59. /// The \c TokenAnnotator tries to match parenthesis and square brakets and
  60. /// store a parenthesis levels. It also tries to resolve matching "<" and ">"
  61. /// into template parameter lists.
  62. class AnnotatingParser {
  63. public:
  64. AnnotatingParser(const FormatStyle &Style, AnnotatedLine &Line,
  65. const AdditionalKeywords &Keywords)
  66. : Style(Style), Line(Line), CurrentToken(Line.First), AutoFound(false),
  67. Keywords(Keywords) {
  68. Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false));
  69. resetTokenMetadata();
  70. }
  71. private:
  72. bool parseAngle() {
  73. if (!CurrentToken || !CurrentToken->Previous)
  74. return false;
  75. if (NonTemplateLess.count(CurrentToken->Previous))
  76. return false;
  77. const FormatToken &Previous = *CurrentToken->Previous; // The '<'.
  78. if (Previous.Previous) {
  79. if (Previous.Previous->Tok.isLiteral())
  80. return false;
  81. if (Previous.Previous->is(tok::r_paren) && Contexts.size() > 1 &&
  82. (!Previous.Previous->MatchingParen ||
  83. !Previous.Previous->MatchingParen->is(TT_OverloadedOperatorLParen)))
  84. return false;
  85. }
  86. FormatToken *Left = CurrentToken->Previous;
  87. Left->ParentBracket = Contexts.back().ContextKind;
  88. ScopedContextCreator ContextCreator(*this, tok::less, 12);
  89. // If this angle is in the context of an expression, we need to be more
  90. // hesitant to detect it as opening template parameters.
  91. bool InExprContext = Contexts.back().IsExpression;
  92. Contexts.back().IsExpression = false;
  93. // If there's a template keyword before the opening angle bracket, this is a
  94. // template parameter, not an argument.
  95. Contexts.back().InTemplateArgument =
  96. Left->Previous && Left->Previous->Tok.isNot(tok::kw_template);
  97. if (Style.Language == FormatStyle::LK_Java &&
  98. CurrentToken->is(tok::question))
  99. next();
  100. while (CurrentToken) {
  101. if (CurrentToken->is(tok::greater)) {
  102. // Try to do a better job at looking for ">>" within the condition of
  103. // a statement. Conservatively insert spaces between consecutive ">"
  104. // tokens to prevent splitting right bitshift operators and potentially
  105. // altering program semantics. This check is overly conservative and
  106. // will prevent spaces from being inserted in select nested template
  107. // parameter cases, but should not alter program semantics.
  108. if (CurrentToken->Next && CurrentToken->Next->is(tok::greater) &&
  109. Left->ParentBracket != tok::less &&
  110. (isKeywordWithCondition(*Line.First) ||
  111. CurrentToken->getStartOfNonWhitespace() ==
  112. CurrentToken->Next->getStartOfNonWhitespace().getLocWithOffset(
  113. -1)))
  114. return false;
  115. Left->MatchingParen = CurrentToken;
  116. CurrentToken->MatchingParen = Left;
  117. // In TT_Proto, we must distignuish between:
  118. // map<key, value>
  119. // msg < item: data >
  120. // msg: < item: data >
  121. // In TT_TextProto, map<key, value> does not occur.
  122. if (Style.Language == FormatStyle::LK_TextProto ||
  123. (Style.Language == FormatStyle::LK_Proto && Left->Previous &&
  124. Left->Previous->isOneOf(TT_SelectorName, TT_DictLiteral)))
  125. CurrentToken->setType(TT_DictLiteral);
  126. else
  127. CurrentToken->setType(TT_TemplateCloser);
  128. next();
  129. return true;
  130. }
  131. if (CurrentToken->is(tok::question) &&
  132. Style.Language == FormatStyle::LK_Java) {
  133. next();
  134. continue;
  135. }
  136. if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace) ||
  137. (CurrentToken->isOneOf(tok::colon, tok::question) && InExprContext &&
  138. !Style.isCSharp() && Style.Language != FormatStyle::LK_Proto &&
  139. Style.Language != FormatStyle::LK_TextProto))
  140. return false;
  141. // If a && or || is found and interpreted as a binary operator, this set
  142. // of angles is likely part of something like "a < b && c > d". If the
  143. // angles are inside an expression, the ||/&& might also be a binary
  144. // operator that was misinterpreted because we are parsing template
  145. // parameters.
  146. // FIXME: This is getting out of hand, write a decent parser.
  147. if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) &&
  148. CurrentToken->Previous->is(TT_BinaryOperator) &&
  149. Contexts[Contexts.size() - 2].IsExpression &&
  150. !Line.startsWith(tok::kw_template))
  151. return false;
  152. updateParameterCount(Left, CurrentToken);
  153. if (Style.Language == FormatStyle::LK_Proto) {
  154. if (FormatToken *Previous = CurrentToken->getPreviousNonComment()) {
  155. if (CurrentToken->is(tok::colon) ||
  156. (CurrentToken->isOneOf(tok::l_brace, tok::less) &&
  157. Previous->isNot(tok::colon)))
  158. Previous->setType(TT_SelectorName);
  159. }
  160. }
  161. if (!consumeToken())
  162. return false;
  163. }
  164. return false;
  165. }
  166. bool parseUntouchableParens() {
  167. while (CurrentToken) {
  168. CurrentToken->Finalized = true;
  169. switch (CurrentToken->Tok.getKind()) {
  170. case tok::l_paren:
  171. next();
  172. if (!parseUntouchableParens())
  173. return false;
  174. continue;
  175. case tok::r_paren:
  176. next();
  177. return true;
  178. default:
  179. // no-op
  180. break;
  181. }
  182. next();
  183. }
  184. return false;
  185. }
  186. bool parseParens(bool LookForDecls = false) {
  187. if (!CurrentToken)
  188. return false;
  189. FormatToken *Left = CurrentToken->Previous;
  190. assert(Left && "Unknown previous token");
  191. FormatToken *PrevNonComment = Left->getPreviousNonComment();
  192. Left->ParentBracket = Contexts.back().ContextKind;
  193. ScopedContextCreator ContextCreator(*this, tok::l_paren, 1);
  194. // FIXME: This is a bit of a hack. Do better.
  195. Contexts.back().ColonIsForRangeExpr =
  196. Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
  197. if (Left->Previous && Left->Previous->is(TT_UntouchableMacroFunc)) {
  198. Left->Finalized = true;
  199. return parseUntouchableParens();
  200. }
  201. bool StartsObjCMethodExpr = false;
  202. if (FormatToken *MaybeSel = Left->Previous) {
  203. // @selector( starts a selector.
  204. if (MaybeSel->isObjCAtKeyword(tok::objc_selector) && MaybeSel->Previous &&
  205. MaybeSel->Previous->is(tok::at)) {
  206. StartsObjCMethodExpr = true;
  207. }
  208. }
  209. if (Left->is(TT_OverloadedOperatorLParen)) {
  210. // Find the previous kw_operator token.
  211. FormatToken *Prev = Left;
  212. while (!Prev->is(tok::kw_operator)) {
  213. Prev = Prev->Previous;
  214. assert(Prev && "Expect a kw_operator prior to the OperatorLParen!");
  215. }
  216. // If faced with "a.operator*(argument)" or "a->operator*(argument)",
  217. // i.e. the operator is called as a member function,
  218. // then the argument must be an expression.
  219. bool OperatorCalledAsMemberFunction =
  220. Prev->Previous && Prev->Previous->isOneOf(tok::period, tok::arrow);
  221. Contexts.back().IsExpression = OperatorCalledAsMemberFunction;
  222. } else if (Style.isJavaScript() &&
  223. (Line.startsWith(Keywords.kw_type, tok::identifier) ||
  224. Line.startsWith(tok::kw_export, Keywords.kw_type,
  225. tok::identifier))) {
  226. // type X = (...);
  227. // export type X = (...);
  228. Contexts.back().IsExpression = false;
  229. } else if (Left->Previous &&
  230. (Left->Previous->isOneOf(tok::kw_static_assert, tok::kw_while,
  231. tok::l_paren, tok::comma) ||
  232. Left->Previous->isIf() ||
  233. Left->Previous->is(TT_BinaryOperator))) {
  234. // static_assert, if and while usually contain expressions.
  235. Contexts.back().IsExpression = true;
  236. } else if (Style.isJavaScript() && Left->Previous &&
  237. (Left->Previous->is(Keywords.kw_function) ||
  238. (Left->Previous->endsSequence(tok::identifier,
  239. Keywords.kw_function)))) {
  240. // function(...) or function f(...)
  241. Contexts.back().IsExpression = false;
  242. } else if (Style.isJavaScript() && Left->Previous &&
  243. Left->Previous->is(TT_JsTypeColon)) {
  244. // let x: (SomeType);
  245. Contexts.back().IsExpression = false;
  246. } else if (isLambdaParameterList(Left)) {
  247. // This is a parameter list of a lambda expression.
  248. Contexts.back().IsExpression = false;
  249. } else if (Line.InPPDirective &&
  250. (!Left->Previous || !Left->Previous->is(tok::identifier))) {
  251. Contexts.back().IsExpression = true;
  252. } else if (Contexts[Contexts.size() - 2].CaretFound) {
  253. // This is the parameter list of an ObjC block.
  254. Contexts.back().IsExpression = false;
  255. } else if (Left->Previous && Left->Previous->is(TT_ForEachMacro)) {
  256. // The first argument to a foreach macro is a declaration.
  257. Contexts.back().IsForEachMacro = true;
  258. Contexts.back().IsExpression = false;
  259. } else if (Left->Previous && Left->Previous->MatchingParen &&
  260. Left->Previous->MatchingParen->is(TT_ObjCBlockLParen)) {
  261. Contexts.back().IsExpression = false;
  262. } else if (!Line.MustBeDeclaration && !Line.InPPDirective) {
  263. bool IsForOrCatch =
  264. Left->Previous && Left->Previous->isOneOf(tok::kw_for, tok::kw_catch);
  265. Contexts.back().IsExpression = !IsForOrCatch;
  266. }
  267. // Infer the role of the l_paren based on the previous token if we haven't
  268. // detected one one yet.
  269. if (PrevNonComment && Left->is(TT_Unknown)) {
  270. if (PrevNonComment->is(tok::kw___attribute)) {
  271. Left->setType(TT_AttributeParen);
  272. } else if (PrevNonComment->isOneOf(TT_TypenameMacro, tok::kw_decltype,
  273. tok::kw_typeof, tok::kw__Atomic,
  274. tok::kw___underlying_type)) {
  275. Left->setType(TT_TypeDeclarationParen);
  276. // decltype() and typeof() usually contain expressions.
  277. if (PrevNonComment->isOneOf(tok::kw_decltype, tok::kw_typeof))
  278. Contexts.back().IsExpression = true;
  279. }
  280. }
  281. if (StartsObjCMethodExpr) {
  282. Contexts.back().ColonIsObjCMethodExpr = true;
  283. Left->setType(TT_ObjCMethodExpr);
  284. }
  285. // MightBeFunctionType and ProbablyFunctionType are used for
  286. // function pointer and reference types as well as Objective-C
  287. // block types:
  288. //
  289. // void (*FunctionPointer)(void);
  290. // void (&FunctionReference)(void);
  291. // void (&&FunctionReference)(void);
  292. // void (^ObjCBlock)(void);
  293. bool MightBeFunctionType = !Contexts[Contexts.size() - 2].IsExpression;
  294. bool ProbablyFunctionType =
  295. CurrentToken->isOneOf(tok::star, tok::amp, tok::ampamp, tok::caret);
  296. bool HasMultipleLines = false;
  297. bool HasMultipleParametersOnALine = false;
  298. bool MightBeObjCForRangeLoop =
  299. Left->Previous && Left->Previous->is(tok::kw_for);
  300. FormatToken *PossibleObjCForInToken = nullptr;
  301. while (CurrentToken) {
  302. // LookForDecls is set when "if (" has been seen. Check for
  303. // 'identifier' '*' 'identifier' followed by not '=' -- this
  304. // '*' has to be a binary operator but determineStarAmpUsage() will
  305. // categorize it as an unary operator, so set the right type here.
  306. if (LookForDecls && CurrentToken->Next) {
  307. FormatToken *Prev = CurrentToken->getPreviousNonComment();
  308. if (Prev) {
  309. FormatToken *PrevPrev = Prev->getPreviousNonComment();
  310. FormatToken *Next = CurrentToken->Next;
  311. if (PrevPrev && PrevPrev->is(tok::identifier) &&
  312. Prev->isOneOf(tok::star, tok::amp, tok::ampamp) &&
  313. CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) {
  314. Prev->setType(TT_BinaryOperator);
  315. LookForDecls = false;
  316. }
  317. }
  318. }
  319. if (CurrentToken->Previous->is(TT_PointerOrReference) &&
  320. CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
  321. tok::coloncolon))
  322. ProbablyFunctionType = true;
  323. if (CurrentToken->is(tok::comma))
  324. MightBeFunctionType = false;
  325. if (CurrentToken->Previous->is(TT_BinaryOperator))
  326. Contexts.back().IsExpression = true;
  327. if (CurrentToken->is(tok::r_paren)) {
  328. if (MightBeFunctionType && ProbablyFunctionType && CurrentToken->Next &&
  329. (CurrentToken->Next->is(tok::l_paren) ||
  330. (CurrentToken->Next->is(tok::l_square) && Line.MustBeDeclaration)))
  331. Left->setType(Left->Next->is(tok::caret) ? TT_ObjCBlockLParen
  332. : TT_FunctionTypeLParen);
  333. Left->MatchingParen = CurrentToken;
  334. CurrentToken->MatchingParen = Left;
  335. if (CurrentToken->Next && CurrentToken->Next->is(tok::l_brace) &&
  336. Left->Previous && Left->Previous->is(tok::l_paren)) {
  337. // Detect the case where macros are used to generate lambdas or
  338. // function bodies, e.g.:
  339. // auto my_lambda = MACRO((Type *type, int i) { .. body .. });
  340. for (FormatToken *Tok = Left; Tok != CurrentToken; Tok = Tok->Next) {
  341. if (Tok->is(TT_BinaryOperator) &&
  342. Tok->isOneOf(tok::star, tok::amp, tok::ampamp))
  343. Tok->setType(TT_PointerOrReference);
  344. }
  345. }
  346. if (StartsObjCMethodExpr) {
  347. CurrentToken->setType(TT_ObjCMethodExpr);
  348. if (Contexts.back().FirstObjCSelectorName) {
  349. Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
  350. Contexts.back().LongestObjCSelectorName;
  351. }
  352. }
  353. if (Left->is(TT_AttributeParen))
  354. CurrentToken->setType(TT_AttributeParen);
  355. if (Left->is(TT_TypeDeclarationParen))
  356. CurrentToken->setType(TT_TypeDeclarationParen);
  357. if (Left->Previous && Left->Previous->is(TT_JavaAnnotation))
  358. CurrentToken->setType(TT_JavaAnnotation);
  359. if (Left->Previous && Left->Previous->is(TT_LeadingJavaAnnotation))
  360. CurrentToken->setType(TT_LeadingJavaAnnotation);
  361. if (Left->Previous && Left->Previous->is(TT_AttributeSquare))
  362. CurrentToken->setType(TT_AttributeSquare);
  363. if (!HasMultipleLines)
  364. Left->setPackingKind(PPK_Inconclusive);
  365. else if (HasMultipleParametersOnALine)
  366. Left->setPackingKind(PPK_BinPacked);
  367. else
  368. Left->setPackingKind(PPK_OnePerLine);
  369. next();
  370. return true;
  371. }
  372. if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
  373. return false;
  374. if (CurrentToken->is(tok::l_brace))
  375. Left->setType(TT_Unknown); // Not TT_ObjCBlockLParen
  376. if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
  377. !CurrentToken->Next->HasUnescapedNewline &&
  378. !CurrentToken->Next->isTrailingComment())
  379. HasMultipleParametersOnALine = true;
  380. bool ProbablyFunctionTypeLParen =
  381. (CurrentToken->is(tok::l_paren) && CurrentToken->Next &&
  382. CurrentToken->Next->isOneOf(tok::star, tok::amp, tok::caret));
  383. if ((CurrentToken->Previous->isOneOf(tok::kw_const, tok::kw_auto) ||
  384. CurrentToken->Previous->isSimpleTypeSpecifier()) &&
  385. !(CurrentToken->is(tok::l_brace) ||
  386. (CurrentToken->is(tok::l_paren) && !ProbablyFunctionTypeLParen)))
  387. Contexts.back().IsExpression = false;
  388. if (CurrentToken->isOneOf(tok::semi, tok::colon)) {
  389. MightBeObjCForRangeLoop = false;
  390. if (PossibleObjCForInToken) {
  391. PossibleObjCForInToken->setType(TT_Unknown);
  392. PossibleObjCForInToken = nullptr;
  393. }
  394. }
  395. if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in)) {
  396. PossibleObjCForInToken = CurrentToken;
  397. PossibleObjCForInToken->setType(TT_ObjCForIn);
  398. }
  399. // When we discover a 'new', we set CanBeExpression to 'false' in order to
  400. // parse the type correctly. Reset that after a comma.
  401. if (CurrentToken->is(tok::comma))
  402. Contexts.back().CanBeExpression = true;
  403. FormatToken *Tok = CurrentToken;
  404. if (!consumeToken())
  405. return false;
  406. updateParameterCount(Left, Tok);
  407. if (CurrentToken && CurrentToken->HasUnescapedNewline)
  408. HasMultipleLines = true;
  409. }
  410. return false;
  411. }
  412. bool isCSharpAttributeSpecifier(const FormatToken &Tok) {
  413. if (!Style.isCSharp())
  414. return false;
  415. // `identifier[i]` is not an attribute.
  416. if (Tok.Previous && Tok.Previous->is(tok::identifier))
  417. return false;
  418. // Chains of [] in `identifier[i][j][k]` are not attributes.
  419. if (Tok.Previous && Tok.Previous->is(tok::r_square)) {
  420. auto *MatchingParen = Tok.Previous->MatchingParen;
  421. if (!MatchingParen || MatchingParen->is(TT_ArraySubscriptLSquare))
  422. return false;
  423. }
  424. const FormatToken *AttrTok = Tok.Next;
  425. if (!AttrTok)
  426. return false;
  427. // Just an empty declaration e.g. string [].
  428. if (AttrTok->is(tok::r_square))
  429. return false;
  430. // Move along the tokens inbetween the '[' and ']' e.g. [STAThread].
  431. while (AttrTok && AttrTok->isNot(tok::r_square)) {
  432. AttrTok = AttrTok->Next;
  433. }
  434. if (!AttrTok)
  435. return false;
  436. // Allow an attribute to be the only content of a file.
  437. AttrTok = AttrTok->Next;
  438. if (!AttrTok)
  439. return true;
  440. // Limit this to being an access modifier that follows.
  441. if (AttrTok->isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected,
  442. tok::comment, tok::kw_class, tok::kw_static,
  443. tok::l_square, Keywords.kw_internal)) {
  444. return true;
  445. }
  446. // incase its a [XXX] retval func(....
  447. if (AttrTok->Next &&
  448. AttrTok->Next->startsSequence(tok::identifier, tok::l_paren))
  449. return true;
  450. return false;
  451. }
  452. bool isCpp11AttributeSpecifier(const FormatToken &Tok) {
  453. if (!Style.isCpp() || !Tok.startsSequence(tok::l_square, tok::l_square))
  454. return false;
  455. // The first square bracket is part of an ObjC array literal
  456. if (Tok.Previous && Tok.Previous->is(tok::at)) {
  457. return false;
  458. }
  459. const FormatToken *AttrTok = Tok.Next->Next;
  460. if (!AttrTok)
  461. return false;
  462. // C++17 '[[using ns: foo, bar(baz, blech)]]'
  463. // We assume nobody will name an ObjC variable 'using'.
  464. if (AttrTok->startsSequence(tok::kw_using, tok::identifier, tok::colon))
  465. return true;
  466. if (AttrTok->isNot(tok::identifier))
  467. return false;
  468. while (AttrTok && !AttrTok->startsSequence(tok::r_square, tok::r_square)) {
  469. // ObjC message send. We assume nobody will use : in a C++11 attribute
  470. // specifier parameter, although this is technically valid:
  471. // [[foo(:)]].
  472. if (AttrTok->is(tok::colon) ||
  473. AttrTok->startsSequence(tok::identifier, tok::identifier) ||
  474. AttrTok->startsSequence(tok::r_paren, tok::identifier))
  475. return false;
  476. if (AttrTok->is(tok::ellipsis))
  477. return true;
  478. AttrTok = AttrTok->Next;
  479. }
  480. return AttrTok && AttrTok->startsSequence(tok::r_square, tok::r_square);
  481. }
  482. bool parseSquare() {
  483. if (!CurrentToken)
  484. return false;
  485. // A '[' could be an index subscript (after an identifier or after
  486. // ')' or ']'), it could be the start of an Objective-C method
  487. // expression, it could the start of an Objective-C array literal,
  488. // or it could be a C++ attribute specifier [[foo::bar]].
  489. FormatToken *Left = CurrentToken->Previous;
  490. Left->ParentBracket = Contexts.back().ContextKind;
  491. FormatToken *Parent = Left->getPreviousNonComment();
  492. // Cases where '>' is followed by '['.
  493. // In C++, this can happen either in array of templates (foo<int>[10])
  494. // or when array is a nested template type (unique_ptr<type1<type2>[]>).
  495. bool CppArrayTemplates =
  496. Style.isCpp() && Parent && Parent->is(TT_TemplateCloser) &&
  497. (Contexts.back().CanBeExpression || Contexts.back().IsExpression ||
  498. Contexts.back().InTemplateArgument);
  499. bool IsCpp11AttributeSpecifier = isCpp11AttributeSpecifier(*Left) ||
  500. Contexts.back().InCpp11AttributeSpecifier;
  501. // Treat C# Attributes [STAThread] much like C++ attributes [[...]].
  502. bool IsCSharpAttributeSpecifier =
  503. isCSharpAttributeSpecifier(*Left) ||
  504. Contexts.back().InCSharpAttributeSpecifier;
  505. bool InsideInlineASM = Line.startsWith(tok::kw_asm);
  506. bool IsCppStructuredBinding = Left->isCppStructuredBinding(Style);
  507. bool StartsObjCMethodExpr =
  508. !IsCppStructuredBinding && !InsideInlineASM && !CppArrayTemplates &&
  509. Style.isCpp() && !IsCpp11AttributeSpecifier &&
  510. !IsCSharpAttributeSpecifier && Contexts.back().CanBeExpression &&
  511. Left->isNot(TT_LambdaLSquare) &&
  512. !CurrentToken->isOneOf(tok::l_brace, tok::r_square) &&
  513. (!Parent ||
  514. Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
  515. tok::kw_return, tok::kw_throw) ||
  516. Parent->isUnaryOperator() ||
  517. // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
  518. Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
  519. (getBinOpPrecedence(Parent->Tok.getKind(), true, true) >
  520. prec::Unknown));
  521. bool ColonFound = false;
  522. unsigned BindingIncrease = 1;
  523. if (IsCppStructuredBinding) {
  524. Left->setType(TT_StructuredBindingLSquare);
  525. } else if (Left->is(TT_Unknown)) {
  526. if (StartsObjCMethodExpr) {
  527. Left->setType(TT_ObjCMethodExpr);
  528. } else if (InsideInlineASM) {
  529. Left->setType(TT_InlineASMSymbolicNameLSquare);
  530. } else if (IsCpp11AttributeSpecifier) {
  531. Left->setType(TT_AttributeSquare);
  532. } else if (Style.isJavaScript() && Parent &&
  533. Contexts.back().ContextKind == tok::l_brace &&
  534. Parent->isOneOf(tok::l_brace, tok::comma)) {
  535. Left->setType(TT_JsComputedPropertyName);
  536. } else if (Style.isCpp() && Contexts.back().ContextKind == tok::l_brace &&
  537. Parent && Parent->isOneOf(tok::l_brace, tok::comma)) {
  538. Left->setType(TT_DesignatedInitializerLSquare);
  539. } else if (IsCSharpAttributeSpecifier) {
  540. Left->setType(TT_AttributeSquare);
  541. } else if (CurrentToken->is(tok::r_square) && Parent &&
  542. Parent->is(TT_TemplateCloser)) {
  543. Left->setType(TT_ArraySubscriptLSquare);
  544. } else if (Style.Language == FormatStyle::LK_Proto ||
  545. Style.Language == FormatStyle::LK_TextProto) {
  546. // Square braces in LK_Proto can either be message field attributes:
  547. //
  548. // optional Aaa aaa = 1 [
  549. // (aaa) = aaa
  550. // ];
  551. //
  552. // extensions 123 [
  553. // (aaa) = aaa
  554. // ];
  555. //
  556. // or text proto extensions (in options):
  557. //
  558. // option (Aaa.options) = {
  559. // [type.type/type] {
  560. // key: value
  561. // }
  562. // }
  563. //
  564. // or repeated fields (in options):
  565. //
  566. // option (Aaa.options) = {
  567. // keys: [ 1, 2, 3 ]
  568. // }
  569. //
  570. // In the first and the third case we want to spread the contents inside
  571. // the square braces; in the second we want to keep them inline.
  572. Left->setType(TT_ArrayInitializerLSquare);
  573. if (!Left->endsSequence(tok::l_square, tok::numeric_constant,
  574. tok::equal) &&
  575. !Left->endsSequence(tok::l_square, tok::numeric_constant,
  576. tok::identifier) &&
  577. !Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)) {
  578. Left->setType(TT_ProtoExtensionLSquare);
  579. BindingIncrease = 10;
  580. }
  581. } else if (!CppArrayTemplates && Parent &&
  582. Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
  583. tok::comma, tok::l_paren, tok::l_square,
  584. tok::question, tok::colon, tok::kw_return,
  585. // Should only be relevant to JavaScript:
  586. tok::kw_default)) {
  587. Left->setType(TT_ArrayInitializerLSquare);
  588. } else {
  589. BindingIncrease = 10;
  590. Left->setType(TT_ArraySubscriptLSquare);
  591. }
  592. }
  593. ScopedContextCreator ContextCreator(*this, tok::l_square, BindingIncrease);
  594. Contexts.back().IsExpression = true;
  595. if (Style.isJavaScript() && Parent && Parent->is(TT_JsTypeColon))
  596. Contexts.back().IsExpression = false;
  597. Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
  598. Contexts.back().InCpp11AttributeSpecifier = IsCpp11AttributeSpecifier;
  599. Contexts.back().InCSharpAttributeSpecifier = IsCSharpAttributeSpecifier;
  600. while (CurrentToken) {
  601. if (CurrentToken->is(tok::r_square)) {
  602. if (IsCpp11AttributeSpecifier)
  603. CurrentToken->setType(TT_AttributeSquare);
  604. if (IsCSharpAttributeSpecifier)
  605. CurrentToken->setType(TT_AttributeSquare);
  606. else if (((CurrentToken->Next &&
  607. CurrentToken->Next->is(tok::l_paren)) ||
  608. (CurrentToken->Previous &&
  609. CurrentToken->Previous->Previous == Left)) &&
  610. Left->is(TT_ObjCMethodExpr)) {
  611. // An ObjC method call is rarely followed by an open parenthesis. It
  612. // also can't be composed of just one token, unless it's a macro that
  613. // will be expanded to more tokens.
  614. // FIXME: Do we incorrectly label ":" with this?
  615. StartsObjCMethodExpr = false;
  616. Left->setType(TT_Unknown);
  617. }
  618. if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
  619. CurrentToken->setType(TT_ObjCMethodExpr);
  620. // If we haven't seen a colon yet, make sure the last identifier
  621. // before the r_square is tagged as a selector name component.
  622. if (!ColonFound && CurrentToken->Previous &&
  623. CurrentToken->Previous->is(TT_Unknown) &&
  624. canBeObjCSelectorComponent(*CurrentToken->Previous))
  625. CurrentToken->Previous->setType(TT_SelectorName);
  626. // determineStarAmpUsage() thinks that '*' '[' is allocating an
  627. // array of pointers, but if '[' starts a selector then '*' is a
  628. // binary operator.
  629. if (Parent && Parent->is(TT_PointerOrReference))
  630. Parent->setType(TT_BinaryOperator);
  631. }
  632. // An arrow after an ObjC method expression is not a lambda arrow.
  633. if (CurrentToken->getType() == TT_ObjCMethodExpr &&
  634. CurrentToken->Next && CurrentToken->Next->is(TT_LambdaArrow))
  635. CurrentToken->Next->setType(TT_Unknown);
  636. Left->MatchingParen = CurrentToken;
  637. CurrentToken->MatchingParen = Left;
  638. // FirstObjCSelectorName is set when a colon is found. This does
  639. // not work, however, when the method has no parameters.
  640. // Here, we set FirstObjCSelectorName when the end of the method call is
  641. // reached, in case it was not set already.
  642. if (!Contexts.back().FirstObjCSelectorName) {
  643. FormatToken *Previous = CurrentToken->getPreviousNonComment();
  644. if (Previous && Previous->is(TT_SelectorName)) {
  645. Previous->ObjCSelectorNameParts = 1;
  646. Contexts.back().FirstObjCSelectorName = Previous;
  647. }
  648. } else {
  649. Left->ParameterCount =
  650. Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
  651. }
  652. if (Contexts.back().FirstObjCSelectorName) {
  653. Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
  654. Contexts.back().LongestObjCSelectorName;
  655. if (Left->BlockParameterCount > 1)
  656. Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
  657. }
  658. next();
  659. return true;
  660. }
  661. if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
  662. return false;
  663. if (CurrentToken->is(tok::colon)) {
  664. if (IsCpp11AttributeSpecifier &&
  665. CurrentToken->endsSequence(tok::colon, tok::identifier,
  666. tok::kw_using)) {
  667. // Remember that this is a [[using ns: foo]] C++ attribute, so we
  668. // don't add a space before the colon (unlike other colons).
  669. CurrentToken->setType(TT_AttributeColon);
  670. } else if (Left->isOneOf(TT_ArraySubscriptLSquare,
  671. TT_DesignatedInitializerLSquare)) {
  672. Left->setType(TT_ObjCMethodExpr);
  673. StartsObjCMethodExpr = true;
  674. Contexts.back().ColonIsObjCMethodExpr = true;
  675. if (Parent && Parent->is(tok::r_paren))
  676. // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
  677. Parent->setType(TT_CastRParen);
  678. }
  679. ColonFound = true;
  680. }
  681. if (CurrentToken->is(tok::comma) && Left->is(TT_ObjCMethodExpr) &&
  682. !ColonFound)
  683. Left->setType(TT_ArrayInitializerLSquare);
  684. FormatToken *Tok = CurrentToken;
  685. if (!consumeToken())
  686. return false;
  687. updateParameterCount(Left, Tok);
  688. }
  689. return false;
  690. }
  691. bool couldBeInStructArrayInitializer() const {
  692. if (Contexts.size() < 2)
  693. return false;
  694. // We want to back up no more then 2 context levels i.e.
  695. // . { { <-
  696. const auto End = std::next(Contexts.rbegin(), 2);
  697. auto Last = Contexts.rbegin();
  698. unsigned Depth = 0;
  699. for (; Last != End; ++Last) {
  700. if (Last->ContextKind == tok::l_brace)
  701. ++Depth;
  702. }
  703. return Depth == 2 && Last->ContextKind != tok::l_brace;
  704. }
  705. bool parseBrace() {
  706. if (CurrentToken) {
  707. FormatToken *Left = CurrentToken->Previous;
  708. Left->ParentBracket = Contexts.back().ContextKind;
  709. if (Contexts.back().CaretFound)
  710. Left->setType(TT_ObjCBlockLBrace);
  711. Contexts.back().CaretFound = false;
  712. ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
  713. Contexts.back().ColonIsDictLiteral = true;
  714. if (Left->is(BK_BracedInit))
  715. Contexts.back().IsExpression = true;
  716. if (Style.isJavaScript() && Left->Previous &&
  717. Left->Previous->is(TT_JsTypeColon))
  718. Contexts.back().IsExpression = false;
  719. unsigned CommaCount = 0;
  720. while (CurrentToken) {
  721. if (CurrentToken->is(tok::r_brace)) {
  722. assert(Left->Optional == CurrentToken->Optional);
  723. Left->MatchingParen = CurrentToken;
  724. CurrentToken->MatchingParen = Left;
  725. if (Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
  726. if (Left->ParentBracket == tok::l_brace &&
  727. couldBeInStructArrayInitializer() && CommaCount > 0) {
  728. Contexts.back().InStructArrayInitializer = true;
  729. }
  730. }
  731. next();
  732. return true;
  733. }
  734. if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
  735. return false;
  736. updateParameterCount(Left, CurrentToken);
  737. if (CurrentToken->isOneOf(tok::colon, tok::l_brace, tok::less)) {
  738. FormatToken *Previous = CurrentToken->getPreviousNonComment();
  739. if (Previous->is(TT_JsTypeOptionalQuestion))
  740. Previous = Previous->getPreviousNonComment();
  741. if ((CurrentToken->is(tok::colon) &&
  742. (!Contexts.back().ColonIsDictLiteral || !Style.isCpp())) ||
  743. Style.Language == FormatStyle::LK_Proto ||
  744. Style.Language == FormatStyle::LK_TextProto) {
  745. Left->setType(TT_DictLiteral);
  746. if (Previous->Tok.getIdentifierInfo() ||
  747. Previous->is(tok::string_literal))
  748. Previous->setType(TT_SelectorName);
  749. }
  750. if (CurrentToken->is(tok::colon) || Style.isJavaScript())
  751. Left->setType(TT_DictLiteral);
  752. }
  753. if (CurrentToken->is(tok::comma)) {
  754. if (Style.isJavaScript())
  755. Left->setType(TT_DictLiteral);
  756. ++CommaCount;
  757. }
  758. if (!consumeToken())
  759. return false;
  760. }
  761. }
  762. return true;
  763. }
  764. void updateParameterCount(FormatToken *Left, FormatToken *Current) {
  765. // For ObjC methods, the number of parameters is calculated differently as
  766. // method declarations have a different structure (the parameters are not
  767. // inside a bracket scope).
  768. if (Current->is(tok::l_brace) && Current->is(BK_Block))
  769. ++Left->BlockParameterCount;
  770. if (Current->is(tok::comma)) {
  771. ++Left->ParameterCount;
  772. if (!Left->Role)
  773. Left->Role.reset(new CommaSeparatedList(Style));
  774. Left->Role->CommaFound(Current);
  775. } else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
  776. Left->ParameterCount = 1;
  777. }
  778. }
  779. bool parseConditional() {
  780. while (CurrentToken) {
  781. if (CurrentToken->is(tok::colon)) {
  782. CurrentToken->setType(TT_ConditionalExpr);
  783. next();
  784. return true;
  785. }
  786. if (!consumeToken())
  787. return false;
  788. }
  789. return false;
  790. }
  791. bool parseTemplateDeclaration() {
  792. if (CurrentToken && CurrentToken->is(tok::less)) {
  793. CurrentToken->setType(TT_TemplateOpener);
  794. next();
  795. if (!parseAngle())
  796. return false;
  797. if (CurrentToken)
  798. CurrentToken->Previous->ClosesTemplateDeclaration = true;
  799. return true;
  800. }
  801. return false;
  802. }
  803. bool consumeToken() {
  804. FormatToken *Tok = CurrentToken;
  805. next();
  806. switch (Tok->Tok.getKind()) {
  807. case tok::plus:
  808. case tok::minus:
  809. if (!Tok->Previous && Line.MustBeDeclaration)
  810. Tok->setType(TT_ObjCMethodSpecifier);
  811. break;
  812. case tok::colon:
  813. if (!Tok->Previous)
  814. return false;
  815. // Colons from ?: are handled in parseConditional().
  816. if (Style.isJavaScript()) {
  817. if (Contexts.back().ColonIsForRangeExpr || // colon in for loop
  818. (Contexts.size() == 1 && // switch/case labels
  819. !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) ||
  820. Contexts.back().ContextKind == tok::l_paren || // function params
  821. Contexts.back().ContextKind == tok::l_square || // array type
  822. (!Contexts.back().IsExpression &&
  823. Contexts.back().ContextKind == tok::l_brace) || // object type
  824. (Contexts.size() == 1 &&
  825. Line.MustBeDeclaration)) { // method/property declaration
  826. Contexts.back().IsExpression = false;
  827. Tok->setType(TT_JsTypeColon);
  828. break;
  829. }
  830. } else if (Style.isCSharp()) {
  831. if (Contexts.back().InCSharpAttributeSpecifier) {
  832. Tok->setType(TT_AttributeColon);
  833. break;
  834. }
  835. if (Contexts.back().ContextKind == tok::l_paren) {
  836. Tok->setType(TT_CSharpNamedArgumentColon);
  837. break;
  838. }
  839. }
  840. if (Line.First->isOneOf(Keywords.kw_module, Keywords.kw_import) ||
  841. Line.First->startsSequence(tok::kw_export, Keywords.kw_module) ||
  842. Line.First->startsSequence(tok::kw_export, Keywords.kw_import)) {
  843. Tok->setType(TT_ModulePartitionColon);
  844. } else if (Contexts.back().ColonIsDictLiteral ||
  845. Style.Language == FormatStyle::LK_Proto ||
  846. Style.Language == FormatStyle::LK_TextProto) {
  847. Tok->setType(TT_DictLiteral);
  848. if (Style.Language == FormatStyle::LK_TextProto) {
  849. if (FormatToken *Previous = Tok->getPreviousNonComment())
  850. Previous->setType(TT_SelectorName);
  851. }
  852. } else if (Contexts.back().ColonIsObjCMethodExpr ||
  853. Line.startsWith(TT_ObjCMethodSpecifier)) {
  854. Tok->setType(TT_ObjCMethodExpr);
  855. const FormatToken *BeforePrevious = Tok->Previous->Previous;
  856. // Ensure we tag all identifiers in method declarations as
  857. // TT_SelectorName.
  858. bool UnknownIdentifierInMethodDeclaration =
  859. Line.startsWith(TT_ObjCMethodSpecifier) &&
  860. Tok->Previous->is(tok::identifier) && Tok->Previous->is(TT_Unknown);
  861. if (!BeforePrevious ||
  862. // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
  863. !(BeforePrevious->is(TT_CastRParen) ||
  864. (BeforePrevious->is(TT_ObjCMethodExpr) &&
  865. BeforePrevious->is(tok::colon))) ||
  866. BeforePrevious->is(tok::r_square) ||
  867. Contexts.back().LongestObjCSelectorName == 0 ||
  868. UnknownIdentifierInMethodDeclaration) {
  869. Tok->Previous->setType(TT_SelectorName);
  870. if (!Contexts.back().FirstObjCSelectorName)
  871. Contexts.back().FirstObjCSelectorName = Tok->Previous;
  872. else if (Tok->Previous->ColumnWidth >
  873. Contexts.back().LongestObjCSelectorName)
  874. Contexts.back().LongestObjCSelectorName =
  875. Tok->Previous->ColumnWidth;
  876. Tok->Previous->ParameterIndex =
  877. Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
  878. ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
  879. }
  880. } else if (Contexts.back().ColonIsForRangeExpr) {
  881. Tok->setType(TT_RangeBasedForLoopColon);
  882. } else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) {
  883. Tok->setType(TT_BitFieldColon);
  884. } else if (Contexts.size() == 1 &&
  885. !Line.First->isOneOf(tok::kw_enum, tok::kw_case,
  886. tok::kw_default)) {
  887. FormatToken *Prev = Tok->getPreviousNonComment();
  888. if (!Prev)
  889. break;
  890. if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept))
  891. Tok->setType(TT_CtorInitializerColon);
  892. else if (Prev->is(tok::kw_try)) {
  893. // Member initializer list within function try block.
  894. FormatToken *PrevPrev = Prev->getPreviousNonComment();
  895. if (!PrevPrev)
  896. break;
  897. if (PrevPrev && PrevPrev->isOneOf(tok::r_paren, tok::kw_noexcept))
  898. Tok->setType(TT_CtorInitializerColon);
  899. } else
  900. Tok->setType(TT_InheritanceColon);
  901. } else if (canBeObjCSelectorComponent(*Tok->Previous) && Tok->Next &&
  902. (Tok->Next->isOneOf(tok::r_paren, tok::comma) ||
  903. (canBeObjCSelectorComponent(*Tok->Next) && Tok->Next->Next &&
  904. Tok->Next->Next->is(tok::colon)))) {
  905. // This handles a special macro in ObjC code where selectors including
  906. // the colon are passed as macro arguments.
  907. Tok->setType(TT_ObjCMethodExpr);
  908. } else if (Contexts.back().ContextKind == tok::l_paren) {
  909. Tok->setType(TT_InlineASMColon);
  910. }
  911. break;
  912. case tok::pipe:
  913. case tok::amp:
  914. // | and & in declarations/type expressions represent union and
  915. // intersection types, respectively.
  916. if (Style.isJavaScript() && !Contexts.back().IsExpression)
  917. Tok->setType(TT_JsTypeOperator);
  918. break;
  919. case tok::kw_if:
  920. case tok::kw_while:
  921. if (Tok->is(tok::kw_if) && CurrentToken &&
  922. CurrentToken->isOneOf(tok::kw_constexpr, tok::identifier))
  923. next();
  924. if (CurrentToken && CurrentToken->is(tok::l_paren)) {
  925. next();
  926. if (!parseParens(/*LookForDecls=*/true))
  927. return false;
  928. }
  929. break;
  930. case tok::kw_for:
  931. if (Style.isJavaScript()) {
  932. // x.for and {for: ...}
  933. if ((Tok->Previous && Tok->Previous->is(tok::period)) ||
  934. (Tok->Next && Tok->Next->is(tok::colon)))
  935. break;
  936. // JS' for await ( ...
  937. if (CurrentToken && CurrentToken->is(Keywords.kw_await))
  938. next();
  939. }
  940. if (Style.isCpp() && CurrentToken && CurrentToken->is(tok::kw_co_await))
  941. next();
  942. Contexts.back().ColonIsForRangeExpr = true;
  943. next();
  944. if (!parseParens())
  945. return false;
  946. break;
  947. case tok::l_paren:
  948. // When faced with 'operator()()', the kw_operator handler incorrectly
  949. // marks the first l_paren as a OverloadedOperatorLParen. Here, we make
  950. // the first two parens OverloadedOperators and the second l_paren an
  951. // OverloadedOperatorLParen.
  952. if (Tok->Previous && Tok->Previous->is(tok::r_paren) &&
  953. Tok->Previous->MatchingParen &&
  954. Tok->Previous->MatchingParen->is(TT_OverloadedOperatorLParen)) {
  955. Tok->Previous->setType(TT_OverloadedOperator);
  956. Tok->Previous->MatchingParen->setType(TT_OverloadedOperator);
  957. Tok->setType(TT_OverloadedOperatorLParen);
  958. }
  959. if (!parseParens())
  960. return false;
  961. if (Line.MustBeDeclaration && Contexts.size() == 1 &&
  962. !Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
  963. !Tok->is(TT_TypeDeclarationParen) &&
  964. (!Tok->Previous || !Tok->Previous->isOneOf(tok::kw___attribute,
  965. TT_LeadingJavaAnnotation)))
  966. Line.MightBeFunctionDecl = true;
  967. break;
  968. case tok::l_square:
  969. if (!parseSquare())
  970. return false;
  971. break;
  972. case tok::l_brace:
  973. if (Style.Language == FormatStyle::LK_TextProto) {
  974. FormatToken *Previous = Tok->getPreviousNonComment();
  975. if (Previous && Previous->getType() != TT_DictLiteral)
  976. Previous->setType(TT_SelectorName);
  977. }
  978. if (!parseBrace())
  979. return false;
  980. break;
  981. case tok::less:
  982. if (parseAngle()) {
  983. Tok->setType(TT_TemplateOpener);
  984. // In TT_Proto, we must distignuish between:
  985. // map<key, value>
  986. // msg < item: data >
  987. // msg: < item: data >
  988. // In TT_TextProto, map<key, value> does not occur.
  989. if (Style.Language == FormatStyle::LK_TextProto ||
  990. (Style.Language == FormatStyle::LK_Proto && Tok->Previous &&
  991. Tok->Previous->isOneOf(TT_SelectorName, TT_DictLiteral))) {
  992. Tok->setType(TT_DictLiteral);
  993. FormatToken *Previous = Tok->getPreviousNonComment();
  994. if (Previous && Previous->getType() != TT_DictLiteral)
  995. Previous->setType(TT_SelectorName);
  996. }
  997. } else {
  998. Tok->setType(TT_BinaryOperator);
  999. NonTemplateLess.insert(Tok);
  1000. CurrentToken = Tok;
  1001. next();
  1002. }
  1003. break;
  1004. case tok::r_paren:
  1005. case tok::r_square:
  1006. return false;
  1007. case tok::r_brace:
  1008. // Lines can start with '}'.
  1009. if (Tok->Previous)
  1010. return false;
  1011. break;
  1012. case tok::greater:
  1013. if (Style.Language != FormatStyle::LK_TextProto)
  1014. Tok->setType(TT_BinaryOperator);
  1015. if (Tok->Previous && Tok->Previous->is(TT_TemplateCloser))
  1016. Tok->SpacesRequiredBefore = 1;
  1017. break;
  1018. case tok::kw_operator:
  1019. if (Style.Language == FormatStyle::LK_TextProto ||
  1020. Style.Language == FormatStyle::LK_Proto)
  1021. break;
  1022. while (CurrentToken &&
  1023. !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
  1024. if (CurrentToken->isOneOf(tok::star, tok::amp))
  1025. CurrentToken->setType(TT_PointerOrReference);
  1026. consumeToken();
  1027. if (CurrentToken && CurrentToken->is(tok::comma) &&
  1028. CurrentToken->Previous->isNot(tok::kw_operator))
  1029. break;
  1030. if (CurrentToken && CurrentToken->Previous->isOneOf(
  1031. TT_BinaryOperator, TT_UnaryOperator, tok::comma,
  1032. tok::star, tok::arrow, tok::amp, tok::ampamp))
  1033. CurrentToken->Previous->setType(TT_OverloadedOperator);
  1034. }
  1035. if (CurrentToken && CurrentToken->is(tok::l_paren))
  1036. CurrentToken->setType(TT_OverloadedOperatorLParen);
  1037. if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator))
  1038. CurrentToken->Previous->setType(TT_OverloadedOperator);
  1039. break;
  1040. case tok::question:
  1041. if (Style.isJavaScript() && Tok->Next &&
  1042. Tok->Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
  1043. tok::r_brace)) {
  1044. // Question marks before semicolons, colons, etc. indicate optional
  1045. // types (fields, parameters), e.g.
  1046. // function(x?: string, y?) {...}
  1047. // class X { y?; }
  1048. Tok->setType(TT_JsTypeOptionalQuestion);
  1049. break;
  1050. }
  1051. // Declarations cannot be conditional expressions, this can only be part
  1052. // of a type declaration.
  1053. if (Line.MustBeDeclaration && !Contexts.back().IsExpression &&
  1054. Style.isJavaScript())
  1055. break;
  1056. if (Style.isCSharp()) {
  1057. // `Type?)`, `Type?>`, `Type? name;` and `Type? name =` can only be
  1058. // nullable types.
  1059. // Line.MustBeDeclaration will be true for `Type? name;`.
  1060. if ((!Contexts.back().IsExpression && Line.MustBeDeclaration) ||
  1061. (Tok->Next && Tok->Next->isOneOf(tok::r_paren, tok::greater)) ||
  1062. (Tok->Next && Tok->Next->is(tok::identifier) && Tok->Next->Next &&
  1063. Tok->Next->Next->is(tok::equal))) {
  1064. Tok->setType(TT_CSharpNullable);
  1065. break;
  1066. }
  1067. }
  1068. parseConditional();
  1069. break;
  1070. case tok::kw_template:
  1071. parseTemplateDeclaration();
  1072. break;
  1073. case tok::comma:
  1074. if (Contexts.back().InCtorInitializer)
  1075. Tok->setType(TT_CtorInitializerComma);
  1076. else if (Contexts.back().InInheritanceList)
  1077. Tok->setType(TT_InheritanceComma);
  1078. else if (Contexts.back().FirstStartOfName &&
  1079. (Contexts.size() == 1 || Line.startsWith(tok::kw_for))) {
  1080. Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true;
  1081. Line.IsMultiVariableDeclStmt = true;
  1082. }
  1083. if (Contexts.back().IsForEachMacro)
  1084. Contexts.back().IsExpression = true;
  1085. break;
  1086. case tok::identifier:
  1087. if (Tok->isOneOf(Keywords.kw___has_include,
  1088. Keywords.kw___has_include_next)) {
  1089. parseHasInclude();
  1090. }
  1091. if (Style.isCSharp() && Tok->is(Keywords.kw_where) && Tok->Next &&
  1092. Tok->Next->isNot(tok::l_paren)) {
  1093. Tok->setType(TT_CSharpGenericTypeConstraint);
  1094. parseCSharpGenericTypeConstraint();
  1095. }
  1096. break;
  1097. default:
  1098. break;
  1099. }
  1100. return true;
  1101. }
  1102. void parseCSharpGenericTypeConstraint() {
  1103. int OpenAngleBracketsCount = 0;
  1104. while (CurrentToken) {
  1105. if (CurrentToken->is(tok::less)) {
  1106. // parseAngle is too greedy and will consume the whole line.
  1107. CurrentToken->setType(TT_TemplateOpener);
  1108. ++OpenAngleBracketsCount;
  1109. next();
  1110. } else if (CurrentToken->is(tok::greater)) {
  1111. CurrentToken->setType(TT_TemplateCloser);
  1112. --OpenAngleBracketsCount;
  1113. next();
  1114. } else if (CurrentToken->is(tok::comma) && OpenAngleBracketsCount == 0) {
  1115. // We allow line breaks after GenericTypeConstraintComma's
  1116. // so do not flag commas in Generics as GenericTypeConstraintComma's.
  1117. CurrentToken->setType(TT_CSharpGenericTypeConstraintComma);
  1118. next();
  1119. } else if (CurrentToken->is(Keywords.kw_where)) {
  1120. CurrentToken->setType(TT_CSharpGenericTypeConstraint);
  1121. next();
  1122. } else if (CurrentToken->is(tok::colon)) {
  1123. CurrentToken->setType(TT_CSharpGenericTypeConstraintColon);
  1124. next();
  1125. } else {
  1126. next();
  1127. }
  1128. }
  1129. }
  1130. void parseIncludeDirective() {
  1131. if (CurrentToken && CurrentToken->is(tok::less)) {
  1132. next();
  1133. while (CurrentToken) {
  1134. // Mark tokens up to the trailing line comments as implicit string
  1135. // literals.
  1136. if (CurrentToken->isNot(tok::comment) &&
  1137. !CurrentToken->TokenText.startswith("//"))
  1138. CurrentToken->setType(TT_ImplicitStringLiteral);
  1139. next();
  1140. }
  1141. }
  1142. }
  1143. void parseWarningOrError() {
  1144. next();
  1145. // We still want to format the whitespace left of the first token of the
  1146. // warning or error.
  1147. next();
  1148. while (CurrentToken) {
  1149. CurrentToken->setType(TT_ImplicitStringLiteral);
  1150. next();
  1151. }
  1152. }
  1153. void parsePragma() {
  1154. next(); // Consume "pragma".
  1155. if (CurrentToken &&
  1156. CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option)) {
  1157. bool IsMark = CurrentToken->is(Keywords.kw_mark);
  1158. next(); // Consume "mark".
  1159. next(); // Consume first token (so we fix leading whitespace).
  1160. while (CurrentToken) {
  1161. if (IsMark || CurrentToken->Previous->is(TT_BinaryOperator))
  1162. CurrentToken->setType(TT_ImplicitStringLiteral);
  1163. next();
  1164. }
  1165. }
  1166. }
  1167. void parseHasInclude() {
  1168. if (!CurrentToken || !CurrentToken->is(tok::l_paren))
  1169. return;
  1170. next(); // '('
  1171. parseIncludeDirective();
  1172. next(); // ')'
  1173. }
  1174. LineType parsePreprocessorDirective() {
  1175. bool IsFirstToken = CurrentToken->IsFirst;
  1176. LineType Type = LT_PreprocessorDirective;
  1177. next();
  1178. if (!CurrentToken)
  1179. return Type;
  1180. if (Style.isJavaScript() && IsFirstToken) {
  1181. // JavaScript files can contain shebang lines of the form:
  1182. // #!/usr/bin/env node
  1183. // Treat these like C++ #include directives.
  1184. while (CurrentToken) {
  1185. // Tokens cannot be comments here.
  1186. CurrentToken->setType(TT_ImplicitStringLiteral);
  1187. next();
  1188. }
  1189. return LT_ImportStatement;
  1190. }
  1191. if (CurrentToken->Tok.is(tok::numeric_constant)) {
  1192. CurrentToken->SpacesRequiredBefore = 1;
  1193. return Type;
  1194. }
  1195. // Hashes in the middle of a line can lead to any strange token
  1196. // sequence.
  1197. if (!CurrentToken->Tok.getIdentifierInfo())
  1198. return Type;
  1199. switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
  1200. case tok::pp_include:
  1201. case tok::pp_include_next:
  1202. case tok::pp_import:
  1203. next();
  1204. parseIncludeDirective();
  1205. Type = LT_ImportStatement;
  1206. break;
  1207. case tok::pp_error:
  1208. case tok::pp_warning:
  1209. parseWarningOrError();
  1210. break;
  1211. case tok::pp_pragma:
  1212. parsePragma();
  1213. break;
  1214. case tok::pp_if:
  1215. case tok::pp_elif:
  1216. Contexts.back().IsExpression = true;
  1217. next();
  1218. parseLine();
  1219. break;
  1220. default:
  1221. break;
  1222. }
  1223. while (CurrentToken) {
  1224. FormatToken *Tok = CurrentToken;
  1225. next();
  1226. if (Tok->is(tok::l_paren))
  1227. parseParens();
  1228. else if (Tok->isOneOf(Keywords.kw___has_include,
  1229. Keywords.kw___has_include_next))
  1230. parseHasInclude();
  1231. }
  1232. return Type;
  1233. }
  1234. public:
  1235. LineType parseLine() {
  1236. if (!CurrentToken)
  1237. return LT_Invalid;
  1238. NonTemplateLess.clear();
  1239. if (CurrentToken->is(tok::hash))
  1240. return parsePreprocessorDirective();
  1241. // Directly allow to 'import <string-literal>' to support protocol buffer
  1242. // definitions (github.com/google/protobuf) or missing "#" (either way we
  1243. // should not break the line).
  1244. IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
  1245. if ((Style.Language == FormatStyle::LK_Java &&
  1246. CurrentToken->is(Keywords.kw_package)) ||
  1247. (Info && Info->getPPKeywordID() == tok::pp_import &&
  1248. CurrentToken->Next &&
  1249. CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
  1250. tok::kw_static))) {
  1251. next();
  1252. parseIncludeDirective();
  1253. return LT_ImportStatement;
  1254. }
  1255. // If this line starts and ends in '<' and '>', respectively, it is likely
  1256. // part of "#define <a/b.h>".
  1257. if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
  1258. parseIncludeDirective();
  1259. return LT_ImportStatement;
  1260. }
  1261. // In .proto files, top-level options and package statements are very
  1262. // similar to import statements and should not be line-wrapped.
  1263. if (Style.Language == FormatStyle::LK_Proto && Line.Level == 0 &&
  1264. CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)) {
  1265. next();
  1266. if (CurrentToken && CurrentToken->is(tok::identifier)) {
  1267. while (CurrentToken)
  1268. next();
  1269. return LT_ImportStatement;
  1270. }
  1271. }
  1272. bool KeywordVirtualFound = false;
  1273. bool ImportStatement = false;
  1274. // import {...} from '...';
  1275. if (Style.isJavaScript() && CurrentToken->is(Keywords.kw_import))
  1276. ImportStatement = true;
  1277. while (CurrentToken) {
  1278. if (CurrentToken->is(tok::kw_virtual))
  1279. KeywordVirtualFound = true;
  1280. if (Style.isJavaScript()) {
  1281. // export {...} from '...';
  1282. // An export followed by "from 'some string';" is a re-export from
  1283. // another module identified by a URI and is treated as a
  1284. // LT_ImportStatement (i.e. prevent wraps on it for long URIs).
  1285. // Just "export {...};" or "export class ..." should not be treated as
  1286. // an import in this sense.
  1287. if (Line.First->is(tok::kw_export) &&
  1288. CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
  1289. CurrentToken->Next->isStringLiteral())
  1290. ImportStatement = true;
  1291. if (isClosureImportStatement(*CurrentToken))
  1292. ImportStatement = true;
  1293. }
  1294. if (!consumeToken())
  1295. return LT_Invalid;
  1296. }
  1297. if (KeywordVirtualFound)
  1298. return LT_VirtualFunctionDecl;
  1299. if (ImportStatement)
  1300. return LT_ImportStatement;
  1301. if (Line.startsWith(TT_ObjCMethodSpecifier)) {
  1302. if (Contexts.back().FirstObjCSelectorName)
  1303. Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
  1304. Contexts.back().LongestObjCSelectorName;
  1305. return LT_ObjCMethodDecl;
  1306. }
  1307. for (const auto &ctx : Contexts) {
  1308. if (ctx.InStructArrayInitializer) {
  1309. return LT_ArrayOfStructInitializer;
  1310. }
  1311. }
  1312. return LT_Other;
  1313. }
  1314. private:
  1315. bool isClosureImportStatement(const FormatToken &Tok) {
  1316. // FIXME: Closure-library specific stuff should not be hard-coded but be
  1317. // configurable.
  1318. return Tok.TokenText == "goog" && Tok.Next && Tok.Next->is(tok::period) &&
  1319. Tok.Next->Next &&
  1320. (Tok.Next->Next->TokenText == "module" ||
  1321. Tok.Next->Next->TokenText == "provide" ||
  1322. Tok.Next->Next->TokenText == "require" ||
  1323. Tok.Next->Next->TokenText == "requireType" ||
  1324. Tok.Next->Next->TokenText == "forwardDeclare") &&
  1325. Tok.Next->Next->Next && Tok.Next->Next->Next->is(tok::l_paren);
  1326. }
  1327. void resetTokenMetadata() {
  1328. if (!CurrentToken)
  1329. return;
  1330. // Reset token type in case we have already looked at it and then
  1331. // recovered from an error (e.g. failure to find the matching >).
  1332. if (!CurrentToken->isOneOf(
  1333. TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
  1334. TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
  1335. TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
  1336. TT_LambdaArrow, TT_NamespaceMacro, TT_OverloadedOperator,
  1337. TT_RegexLiteral, TT_TemplateString, TT_ObjCStringLiteral,
  1338. TT_UntouchableMacroFunc, TT_ConstraintJunctions,
  1339. TT_StatementAttributeLikeMacro, TT_FunctionLikeOrFreestandingMacro,
  1340. TT_RecordLBrace))
  1341. CurrentToken->setType(TT_Unknown);
  1342. CurrentToken->Role.reset();
  1343. CurrentToken->MatchingParen = nullptr;
  1344. CurrentToken->FakeLParens.clear();
  1345. CurrentToken->FakeRParens = 0;
  1346. }
  1347. void next() {
  1348. if (!CurrentToken)
  1349. return;
  1350. CurrentToken->NestingLevel = Contexts.size() - 1;
  1351. CurrentToken->BindingStrength = Contexts.back().BindingStrength;
  1352. modifyContext(*CurrentToken);
  1353. determineTokenType(*CurrentToken);
  1354. CurrentToken = CurrentToken->Next;
  1355. resetTokenMetadata();
  1356. }
  1357. /// A struct to hold information valid in a specific context, e.g.
  1358. /// a pair of parenthesis.
  1359. struct Context {
  1360. Context(tok::TokenKind ContextKind, unsigned BindingStrength,
  1361. bool IsExpression)
  1362. : ContextKind(ContextKind), BindingStrength(BindingStrength),
  1363. IsExpression(IsExpression) {}
  1364. tok::TokenKind ContextKind;
  1365. unsigned BindingStrength;
  1366. bool IsExpression;
  1367. unsigned LongestObjCSelectorName = 0;
  1368. bool ColonIsForRangeExpr = false;
  1369. bool ColonIsDictLiteral = false;
  1370. bool ColonIsObjCMethodExpr = false;
  1371. FormatToken *FirstObjCSelectorName = nullptr;
  1372. FormatToken *FirstStartOfName = nullptr;
  1373. bool CanBeExpression = true;
  1374. bool InTemplateArgument = false;
  1375. bool InCtorInitializer = false;
  1376. bool InInheritanceList = false;
  1377. bool CaretFound = false;
  1378. bool IsForEachMacro = false;
  1379. bool InCpp11AttributeSpecifier = false;
  1380. bool InCSharpAttributeSpecifier = false;
  1381. bool InStructArrayInitializer = false;
  1382. };
  1383. /// Puts a new \c Context onto the stack \c Contexts for the lifetime
  1384. /// of each instance.
  1385. struct ScopedContextCreator {
  1386. AnnotatingParser &P;
  1387. ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind,
  1388. unsigned Increase)
  1389. : P(P) {
  1390. P.Contexts.push_back(Context(ContextKind,
  1391. P.Contexts.back().BindingStrength + Increase,
  1392. P.Contexts.back().IsExpression));
  1393. }
  1394. ~ScopedContextCreator() {
  1395. if (P.Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
  1396. if (P.Contexts.back().InStructArrayInitializer) {
  1397. P.Contexts.pop_back();
  1398. P.Contexts.back().InStructArrayInitializer = true;
  1399. return;
  1400. }
  1401. }
  1402. P.Contexts.pop_back();
  1403. }
  1404. };
  1405. void modifyContext(const FormatToken &Current) {
  1406. if (Current.getPrecedence() == prec::Assignment &&
  1407. !Line.First->isOneOf(tok::kw_template, tok::kw_using, tok::kw_return) &&
  1408. // Type aliases use `type X = ...;` in TypeScript and can be exported
  1409. // using `export type ...`.
  1410. !(Style.isJavaScript() &&
  1411. (Line.startsWith(Keywords.kw_type, tok::identifier) ||
  1412. Line.startsWith(tok::kw_export, Keywords.kw_type,
  1413. tok::identifier))) &&
  1414. (!Current.Previous || Current.Previous->isNot(tok::kw_operator))) {
  1415. Contexts.back().IsExpression = true;
  1416. if (!Line.startsWith(TT_UnaryOperator)) {
  1417. for (FormatToken *Previous = Current.Previous;
  1418. Previous && Previous->Previous &&
  1419. !Previous->Previous->isOneOf(tok::comma, tok::semi);
  1420. Previous = Previous->Previous) {
  1421. if (Previous->isOneOf(tok::r_square, tok::r_paren)) {
  1422. Previous = Previous->MatchingParen;
  1423. if (!Previous)
  1424. break;
  1425. }
  1426. if (Previous->opensScope())
  1427. break;
  1428. if (Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
  1429. Previous->isOneOf(tok::star, tok::amp, tok::ampamp) &&
  1430. Previous->Previous && Previous->Previous->isNot(tok::equal))
  1431. Previous->setType(TT_PointerOrReference);
  1432. }
  1433. }
  1434. } else if (Current.is(tok::lessless) &&
  1435. (!Current.Previous || !Current.Previous->is(tok::kw_operator))) {
  1436. Contexts.back().IsExpression = true;
  1437. } else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
  1438. Contexts.back().IsExpression = true;
  1439. } else if (Current.is(TT_TrailingReturnArrow)) {
  1440. Contexts.back().IsExpression = false;
  1441. } else if (Current.is(TT_LambdaArrow) || Current.is(Keywords.kw_assert)) {
  1442. Contexts.back().IsExpression = Style.Language == FormatStyle::LK_Java;
  1443. } else if (Current.Previous &&
  1444. Current.Previous->is(TT_CtorInitializerColon)) {
  1445. Contexts.back().IsExpression = true;
  1446. Contexts.back().InCtorInitializer = true;
  1447. } else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
  1448. Contexts.back().InInheritanceList = true;
  1449. } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
  1450. for (FormatToken *Previous = Current.Previous;
  1451. Previous && Previous->isOneOf(tok::star, tok::amp);
  1452. Previous = Previous->Previous)
  1453. Previous->setType(TT_PointerOrReference);
  1454. if (Line.MustBeDeclaration && !Contexts.front().InCtorInitializer)
  1455. Contexts.back().IsExpression = false;
  1456. } else if (Current.is(tok::kw_new)) {
  1457. Contexts.back().CanBeExpression = false;
  1458. } else if (Current.is(tok::semi) ||
  1459. (Current.is(tok::exclaim) && Current.Previous &&
  1460. !Current.Previous->is(tok::kw_operator))) {
  1461. // This should be the condition or increment in a for-loop.
  1462. // But not operator !() (can't use TT_OverloadedOperator here as its not
  1463. // been annotated yet).
  1464. Contexts.back().IsExpression = true;
  1465. }
  1466. }
  1467. static FormatToken *untilMatchingParen(FormatToken *Current) {
  1468. // Used when `MatchingParen` is not yet established.
  1469. int ParenLevel = 0;
  1470. while (Current) {
  1471. if (Current->is(tok::l_paren))
  1472. ++ParenLevel;
  1473. if (Current->is(tok::r_paren))
  1474. --ParenLevel;
  1475. if (ParenLevel < 1)
  1476. break;
  1477. Current = Current->Next;
  1478. }
  1479. return Current;
  1480. }
  1481. static bool isDeductionGuide(FormatToken &Current) {
  1482. // Look for a deduction guide template<T> A(...) -> A<...>;
  1483. if (Current.Previous && Current.Previous->is(tok::r_paren) &&
  1484. Current.startsSequence(tok::arrow, tok::identifier, tok::less)) {
  1485. // Find the TemplateCloser.
  1486. FormatToken *TemplateCloser = Current.Next->Next;
  1487. int NestingLevel = 0;
  1488. while (TemplateCloser) {
  1489. // Skip over an expressions in parens A<(3 < 2)>;
  1490. if (TemplateCloser->is(tok::l_paren)) {
  1491. // No Matching Paren yet so skip to matching paren
  1492. TemplateCloser = untilMatchingParen(TemplateCloser);
  1493. if (!TemplateCloser)
  1494. break;
  1495. }
  1496. if (TemplateCloser->is(tok::less))
  1497. ++NestingLevel;
  1498. if (TemplateCloser->is(tok::greater))
  1499. --NestingLevel;
  1500. if (NestingLevel < 1)
  1501. break;
  1502. TemplateCloser = TemplateCloser->Next;
  1503. }
  1504. // Assuming we have found the end of the template ensure its followed
  1505. // with a semi-colon.
  1506. if (TemplateCloser && TemplateCloser->Next &&
  1507. TemplateCloser->Next->is(tok::semi) &&
  1508. Current.Previous->MatchingParen) {
  1509. // Determine if the identifier `A` prior to the A<..>; is the same as
  1510. // prior to the A(..)
  1511. FormatToken *LeadingIdentifier =
  1512. Current.Previous->MatchingParen->Previous;
  1513. // Differentiate a deduction guide by seeing the
  1514. // > of the template prior to the leading identifier.
  1515. if (LeadingIdentifier) {
  1516. FormatToken *PriorLeadingIdentifier = LeadingIdentifier->Previous;
  1517. // Skip back past explicit decoration
  1518. if (PriorLeadingIdentifier &&
  1519. PriorLeadingIdentifier->is(tok::kw_explicit))
  1520. PriorLeadingIdentifier = PriorLeadingIdentifier->Previous;
  1521. return (PriorLeadingIdentifier &&
  1522. PriorLeadingIdentifier->is(TT_TemplateCloser) &&
  1523. LeadingIdentifier->TokenText == Current.Next->TokenText);
  1524. }
  1525. }
  1526. }
  1527. return false;
  1528. }
  1529. bool possibleTrailingArrowStart(FormatToken* Current) {
  1530. if (!Current) {
  1531. return false;
  1532. }
  1533. if (Current->isOneOf(tok::r_paren, tok::kw_const, tok::kw_volatile, tok::kw_mutable, tok::kw_noexcept)) {
  1534. return true;
  1535. }
  1536. if (Current->TokenText == "&") {
  1537. return true;
  1538. }
  1539. if (Current->TokenText == "&&") {
  1540. return true;
  1541. }
  1542. return false;
  1543. }
  1544. void determineTokenType(FormatToken &Current) {
  1545. if (!Current.is(TT_Unknown))
  1546. // The token type is already known.
  1547. return;
  1548. if ((Style.isJavaScript() || Style.isCSharp()) &&
  1549. Current.is(tok::exclaim)) {
  1550. if (Current.Previous) {
  1551. bool IsIdentifier =
  1552. Style.isJavaScript()
  1553. ? Keywords.IsJavaScriptIdentifier(
  1554. *Current.Previous, /* AcceptIdentifierName= */ true)
  1555. : Current.Previous->is(tok::identifier);
  1556. if (IsIdentifier ||
  1557. Current.Previous->isOneOf(
  1558. tok::kw_namespace, tok::r_paren, tok::r_square, tok::r_brace,
  1559. tok::kw_false, tok::kw_true, Keywords.kw_type, Keywords.kw_get,
  1560. Keywords.kw_set) ||
  1561. Current.Previous->Tok.isLiteral()) {
  1562. Current.setType(TT_NonNullAssertion);
  1563. return;
  1564. }
  1565. }
  1566. if (Current.Next &&
  1567. Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
  1568. Current.setType(TT_NonNullAssertion);
  1569. return;
  1570. }
  1571. }
  1572. // Line.MightBeFunctionDecl can only be true after the parentheses of a
  1573. // function declaration have been found. In this case, 'Current' is a
  1574. // trailing token of this declaration and thus cannot be a name.
  1575. if (Current.is(Keywords.kw_instanceof)) {
  1576. Current.setType(TT_BinaryOperator);
  1577. } else if (isStartOfName(Current) &&
  1578. (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
  1579. Contexts.back().FirstStartOfName = &Current;
  1580. Current.setType(TT_StartOfName);
  1581. } else if (Current.is(tok::semi)) {
  1582. // Reset FirstStartOfName after finding a semicolon so that a for loop
  1583. // with multiple increment statements is not confused with a for loop
  1584. // having multiple variable declarations.
  1585. Contexts.back().FirstStartOfName = nullptr;
  1586. } else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
  1587. AutoFound = true;
  1588. } else if (Current.is(tok::arrow) &&
  1589. Style.Language == FormatStyle::LK_Java) {
  1590. Current.setType(TT_LambdaArrow);
  1591. } else if (Current.is(tok::arrow) && AutoFound && Line.MustBeDeclaration &&
  1592. Current.NestingLevel == 0 &&
  1593. possibleTrailingArrowStart(Current.Previous) &&
  1594. !Current.Previous->isOneOf(tok::kw_operator, tok::identifier)) {
  1595. // not auto operator->() -> xxx;
  1596. Current.setType(TT_TrailingReturnArrow);
  1597. } else if (Current.is(tok::arrow) && Current.Previous &&
  1598. Current.Previous->is(tok::r_brace)) {
  1599. // Concept implicit conversion constraint needs to be treated like
  1600. // a trailing return type ... } -> <type>.
  1601. Current.setType(TT_TrailingReturnArrow);
  1602. } else if (isDeductionGuide(Current)) {
  1603. // Deduction guides trailing arrow " A(...) -> A<T>;".
  1604. Current.setType(TT_TrailingReturnArrow);
  1605. } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
  1606. Current.setType(determineStarAmpUsage(
  1607. Current,
  1608. Contexts.back().CanBeExpression && Contexts.back().IsExpression,
  1609. Contexts.back().InTemplateArgument));
  1610. } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) {
  1611. Current.setType(determinePlusMinusCaretUsage(Current));
  1612. if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
  1613. Contexts.back().CaretFound = true;
  1614. } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
  1615. Current.setType(determineIncrementUsage(Current));
  1616. } else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
  1617. Current.setType(TT_UnaryOperator);
  1618. } else if (Current.is(tok::question)) {
  1619. if (Style.isJavaScript() && Line.MustBeDeclaration &&
  1620. !Contexts.back().IsExpression) {
  1621. // In JavaScript, `interface X { foo?(): bar; }` is an optional method
  1622. // on the interface, not a ternary expression.
  1623. Current.setType(TT_JsTypeOptionalQuestion);
  1624. } else {
  1625. Current.setType(TT_ConditionalExpr);
  1626. }
  1627. } else if (Current.isBinaryOperator() &&
  1628. (!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
  1629. (!Current.is(tok::greater) &&
  1630. Style.Language != FormatStyle::LK_TextProto)) {
  1631. Current.setType(TT_BinaryOperator);
  1632. } else if (Current.is(tok::comment)) {
  1633. if (Current.TokenText.startswith("/*")) {
  1634. if (Current.TokenText.endswith("*/"))
  1635. Current.setType(TT_BlockComment);
  1636. else
  1637. // The lexer has for some reason determined a comment here. But we
  1638. // cannot really handle it, if it isn't properly terminated.
  1639. Current.Tok.setKind(tok::unknown);
  1640. } else {
  1641. Current.setType(TT_LineComment);
  1642. }
  1643. } else if (Current.is(tok::r_paren)) {
  1644. if (rParenEndsCast(Current))
  1645. Current.setType(TT_CastRParen);
  1646. if (Current.MatchingParen && Current.Next &&
  1647. !Current.Next->isBinaryOperator() &&
  1648. !Current.Next->isOneOf(tok::semi, tok::colon, tok::l_brace,
  1649. tok::comma, tok::period, tok::arrow,
  1650. tok::coloncolon))
  1651. if (FormatToken *AfterParen = Current.MatchingParen->Next) {
  1652. // Make sure this isn't the return type of an Obj-C block declaration
  1653. if (AfterParen->Tok.isNot(tok::caret)) {
  1654. if (FormatToken *BeforeParen = Current.MatchingParen->Previous)
  1655. if (BeforeParen->is(tok::identifier) &&
  1656. !BeforeParen->is(TT_TypenameMacro) &&
  1657. BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
  1658. (!BeforeParen->Previous ||
  1659. BeforeParen->Previous->ClosesTemplateDeclaration))
  1660. Current.setType(TT_FunctionAnnotationRParen);
  1661. }
  1662. }
  1663. } else if (Current.is(tok::at) && Current.Next && !Style.isJavaScript() &&
  1664. Style.Language != FormatStyle::LK_Java) {
  1665. // In Java & JavaScript, "@..." is a decorator or annotation. In ObjC, it
  1666. // marks declarations and properties that need special formatting.
  1667. switch (Current.Next->Tok.getObjCKeywordID()) {
  1668. case tok::objc_interface:
  1669. case tok::objc_implementation:
  1670. case tok::objc_protocol:
  1671. Current.setType(TT_ObjCDecl);
  1672. break;
  1673. case tok::objc_property:
  1674. Current.setType(TT_ObjCProperty);
  1675. break;
  1676. default:
  1677. break;
  1678. }
  1679. } else if (Current.is(tok::period)) {
  1680. FormatToken *PreviousNoComment = Current.getPreviousNonComment();
  1681. if (PreviousNoComment &&
  1682. PreviousNoComment->isOneOf(tok::comma, tok::l_brace))
  1683. Current.setType(TT_DesignatedInitializerPeriod);
  1684. else if (Style.Language == FormatStyle::LK_Java && Current.Previous &&
  1685. Current.Previous->isOneOf(TT_JavaAnnotation,
  1686. TT_LeadingJavaAnnotation)) {
  1687. Current.setType(Current.Previous->getType());
  1688. }
  1689. } else if (canBeObjCSelectorComponent(Current) &&
  1690. // FIXME(bug 36976): ObjC return types shouldn't use
  1691. // TT_CastRParen.
  1692. Current.Previous && Current.Previous->is(TT_CastRParen) &&
  1693. Current.Previous->MatchingParen &&
  1694. Current.Previous->MatchingParen->Previous &&
  1695. Current.Previous->MatchingParen->Previous->is(
  1696. TT_ObjCMethodSpecifier)) {
  1697. // This is the first part of an Objective-C selector name. (If there's no
  1698. // colon after this, this is the only place which annotates the identifier
  1699. // as a selector.)
  1700. Current.setType(TT_SelectorName);
  1701. } else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept,
  1702. tok::kw_requires) &&
  1703. Current.Previous &&
  1704. !Current.Previous->isOneOf(tok::equal, tok::at) &&
  1705. Line.MightBeFunctionDecl && Contexts.size() == 1) {
  1706. // Line.MightBeFunctionDecl can only be true after the parentheses of a
  1707. // function declaration have been found.
  1708. Current.setType(TT_TrailingAnnotation);
  1709. } else if ((Style.Language == FormatStyle::LK_Java ||
  1710. Style.isJavaScript()) &&
  1711. Current.Previous) {
  1712. if (Current.Previous->is(tok::at) &&
  1713. Current.isNot(Keywords.kw_interface)) {
  1714. const FormatToken &AtToken = *Current.Previous;
  1715. const FormatToken *Previous = AtToken.getPreviousNonComment();
  1716. if (!Previous || Previous->is(TT_LeadingJavaAnnotation))
  1717. Current.setType(TT_LeadingJavaAnnotation);
  1718. else
  1719. Current.setType(TT_JavaAnnotation);
  1720. } else if (Current.Previous->is(tok::period) &&
  1721. Current.Previous->isOneOf(TT_JavaAnnotation,
  1722. TT_LeadingJavaAnnotation)) {
  1723. Current.setType(Current.Previous->getType());
  1724. }
  1725. }
  1726. }
  1727. /// Take a guess at whether \p Tok starts a name of a function or
  1728. /// variable declaration.
  1729. ///
  1730. /// This is a heuristic based on whether \p Tok is an identifier following
  1731. /// something that is likely a type.
  1732. bool isStartOfName(const FormatToken &Tok) {
  1733. if (Tok.isNot(tok::identifier) || !Tok.Previous)
  1734. return false;
  1735. if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
  1736. Keywords.kw_as))
  1737. return false;
  1738. if (Style.isJavaScript() && Tok.Previous->is(Keywords.kw_in))
  1739. return false;
  1740. // Skip "const" as it does not have an influence on whether this is a name.
  1741. FormatToken *PreviousNotConst = Tok.getPreviousNonComment();
  1742. // For javascript const can be like "let" or "var"
  1743. if (!Style.isJavaScript())
  1744. while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
  1745. PreviousNotConst = PreviousNotConst->getPreviousNonComment();
  1746. if (!PreviousNotConst)
  1747. return false;
  1748. bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
  1749. PreviousNotConst->Previous &&
  1750. PreviousNotConst->Previous->is(tok::hash);
  1751. if (PreviousNotConst->is(TT_TemplateCloser))
  1752. return PreviousNotConst && PreviousNotConst->MatchingParen &&
  1753. PreviousNotConst->MatchingParen->Previous &&
  1754. PreviousNotConst->MatchingParen->Previous->isNot(tok::period) &&
  1755. PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
  1756. if (PreviousNotConst->is(tok::r_paren) &&
  1757. PreviousNotConst->is(TT_TypeDeclarationParen))
  1758. return true;
  1759. // If is a preprocess keyword like #define.
  1760. if (IsPPKeyword)
  1761. return false;
  1762. // int a or auto a.
  1763. if (PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto))
  1764. return true;
  1765. // *a or &a or &&a.
  1766. if (PreviousNotConst->is(TT_PointerOrReference))
  1767. return true;
  1768. // MyClass a;
  1769. if (PreviousNotConst->isSimpleTypeSpecifier())
  1770. return true;
  1771. // const a = in JavaScript.
  1772. return (Style.isJavaScript() && PreviousNotConst->is(tok::kw_const));
  1773. }
  1774. /// Determine whether ')' is ending a cast.
  1775. bool rParenEndsCast(const FormatToken &Tok) {
  1776. // C-style casts are only used in C++, C# and Java.
  1777. if (!Style.isCSharp() && !Style.isCpp() &&
  1778. Style.Language != FormatStyle::LK_Java)
  1779. return false;
  1780. // Empty parens aren't casts and there are no casts at the end of the line.
  1781. if (Tok.Previous == Tok.MatchingParen || !Tok.Next || !Tok.MatchingParen)
  1782. return false;
  1783. FormatToken *LeftOfParens = Tok.MatchingParen->getPreviousNonComment();
  1784. if (LeftOfParens) {
  1785. // If there is a closing parenthesis left of the current
  1786. // parentheses, look past it as these might be chained casts.
  1787. if (LeftOfParens->is(tok::r_paren) &&
  1788. LeftOfParens->isNot(TT_CastRParen)) {
  1789. if (!LeftOfParens->MatchingParen ||
  1790. !LeftOfParens->MatchingParen->Previous)
  1791. return false;
  1792. LeftOfParens = LeftOfParens->MatchingParen->Previous;
  1793. }
  1794. // The Condition directly below this one will see the operator arguments
  1795. // as a (void *foo) cast.
  1796. // void operator delete(void *foo) ATTRIB;
  1797. if (LeftOfParens->Tok.getIdentifierInfo() && LeftOfParens->Previous &&
  1798. LeftOfParens->Previous->is(tok::kw_operator))
  1799. return false;
  1800. // If there is an identifier (or with a few exceptions a keyword) right
  1801. // before the parentheses, this is unlikely to be a cast.
  1802. if (LeftOfParens->Tok.getIdentifierInfo() &&
  1803. !LeftOfParens->isOneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
  1804. tok::kw_delete))
  1805. return false;
  1806. // Certain other tokens right before the parentheses are also signals that
  1807. // this cannot be a cast.
  1808. if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
  1809. TT_TemplateCloser, tok::ellipsis))
  1810. return false;
  1811. }
  1812. if (Tok.Next->is(tok::question))
  1813. return false;
  1814. // `foreach((A a, B b) in someList)` should not be seen as a cast.
  1815. if (Tok.Next->is(Keywords.kw_in) && Style.isCSharp())
  1816. return false;
  1817. // Functions which end with decorations like volatile, noexcept are unlikely
  1818. // to be casts.
  1819. if (Tok.Next->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
  1820. tok::kw_requires, tok::kw_throw, tok::arrow,
  1821. Keywords.kw_override, Keywords.kw_final) ||
  1822. isCpp11AttributeSpecifier(*Tok.Next))
  1823. return false;
  1824. // As Java has no function types, a "(" after the ")" likely means that this
  1825. // is a cast.
  1826. if (Style.Language == FormatStyle::LK_Java && Tok.Next->is(tok::l_paren))
  1827. return true;
  1828. // If a (non-string) literal follows, this is likely a cast.
  1829. if (Tok.Next->isNot(tok::string_literal) &&
  1830. (Tok.Next->Tok.isLiteral() ||
  1831. Tok.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof)))
  1832. return true;
  1833. // Heuristically try to determine whether the parentheses contain a type.
  1834. auto IsQualifiedPointerOrReference = [](FormatToken *T) {
  1835. // This is used to handle cases such as x = (foo *const)&y;
  1836. assert(!T->isSimpleTypeSpecifier() && "Should have already been checked");
  1837. // Strip trailing qualifiers such as const or volatile when checking
  1838. // whether the parens could be a cast to a pointer/reference type.
  1839. while (T) {
  1840. if (T->is(TT_AttributeParen)) {
  1841. // Handle `x = (foo *__attribute__((foo)))&v;`:
  1842. if (T->MatchingParen && T->MatchingParen->Previous &&
  1843. T->MatchingParen->Previous->is(tok::kw___attribute)) {
  1844. T = T->MatchingParen->Previous->Previous;
  1845. continue;
  1846. }
  1847. } else if (T->is(TT_AttributeSquare)) {
  1848. // Handle `x = (foo *[[clang::foo]])&v;`:
  1849. if (T->MatchingParen && T->MatchingParen->Previous) {
  1850. T = T->MatchingParen->Previous;
  1851. continue;
  1852. }
  1853. } else if (T->canBePointerOrReferenceQualifier()) {
  1854. T = T->Previous;
  1855. continue;
  1856. }
  1857. break;
  1858. }
  1859. return T && T->is(TT_PointerOrReference);
  1860. };
  1861. bool ParensAreType =
  1862. !Tok.Previous ||
  1863. Tok.Previous->isOneOf(TT_TemplateCloser, TT_TypeDeclarationParen) ||
  1864. Tok.Previous->isSimpleTypeSpecifier() ||
  1865. IsQualifiedPointerOrReference(Tok.Previous);
  1866. bool ParensCouldEndDecl =
  1867. Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
  1868. if (ParensAreType && !ParensCouldEndDecl)
  1869. return true;
  1870. // At this point, we heuristically assume that there are no casts at the
  1871. // start of the line. We assume that we have found most cases where there
  1872. // are by the logic above, e.g. "(void)x;".
  1873. if (!LeftOfParens)
  1874. return false;
  1875. // Certain token types inside the parentheses mean that this can't be a
  1876. // cast.
  1877. for (const FormatToken *Token = Tok.MatchingParen->Next; Token != &Tok;
  1878. Token = Token->Next)
  1879. if (Token->is(TT_BinaryOperator))
  1880. return false;
  1881. // If the following token is an identifier or 'this', this is a cast. All
  1882. // cases where this can be something else are handled above.
  1883. if (Tok.Next->isOneOf(tok::identifier, tok::kw_this))
  1884. return true;
  1885. // Look for a cast `( x ) (`.
  1886. if (Tok.Next->is(tok::l_paren) && Tok.Previous && Tok.Previous->Previous) {
  1887. if (Tok.Previous->is(tok::identifier) &&
  1888. Tok.Previous->Previous->is(tok::l_paren))
  1889. return true;
  1890. }
  1891. if (!Tok.Next->Next)
  1892. return false;
  1893. // If the next token after the parenthesis is a unary operator, assume
  1894. // that this is cast, unless there are unexpected tokens inside the
  1895. // parenthesis.
  1896. bool NextIsUnary =
  1897. Tok.Next->isUnaryOperator() || Tok.Next->isOneOf(tok::amp, tok::star);
  1898. if (!NextIsUnary || Tok.Next->is(tok::plus) ||
  1899. !Tok.Next->Next->isOneOf(tok::identifier, tok::numeric_constant))
  1900. return false;
  1901. // Search for unexpected tokens.
  1902. for (FormatToken *Prev = Tok.Previous; Prev != Tok.MatchingParen;
  1903. Prev = Prev->Previous) {
  1904. if (!Prev->isOneOf(tok::kw_const, tok::identifier, tok::coloncolon))
  1905. return false;
  1906. }
  1907. return true;
  1908. }
  1909. /// Return the type of the given token assuming it is * or &.
  1910. TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression,
  1911. bool InTemplateArgument) {
  1912. if (Style.isJavaScript())
  1913. return TT_BinaryOperator;
  1914. // && in C# must be a binary operator.
  1915. if (Style.isCSharp() && Tok.is(tok::ampamp))
  1916. return TT_BinaryOperator;
  1917. const FormatToken *PrevToken = Tok.getPreviousNonComment();
  1918. if (!PrevToken)
  1919. return TT_UnaryOperator;
  1920. const FormatToken *NextToken = Tok.getNextNonComment();
  1921. if (!NextToken ||
  1922. NextToken->isOneOf(tok::arrow, tok::equal, tok::kw_noexcept) ||
  1923. NextToken->canBePointerOrReferenceQualifier() ||
  1924. (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment()))
  1925. return TT_PointerOrReference;
  1926. if (PrevToken->is(tok::coloncolon))
  1927. return TT_PointerOrReference;
  1928. if (PrevToken->is(tok::r_paren) && PrevToken->is(TT_TypeDeclarationParen))
  1929. return TT_PointerOrReference;
  1930. if (PrevToken->isOneOf(tok::l_paren, tok::l_square, tok::l_brace,
  1931. tok::comma, tok::semi, tok::kw_return, tok::colon,
  1932. tok::kw_co_return, tok::kw_co_await,
  1933. tok::kw_co_yield, tok::equal, tok::kw_delete,
  1934. tok::kw_sizeof, tok::kw_throw, TT_BinaryOperator,
  1935. TT_ConditionalExpr, TT_UnaryOperator, TT_CastRParen))
  1936. return TT_UnaryOperator;
  1937. if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
  1938. return TT_PointerOrReference;
  1939. if (NextToken->is(tok::kw_operator) && !IsExpression)
  1940. return TT_PointerOrReference;
  1941. if (NextToken->isOneOf(tok::comma, tok::semi))
  1942. return TT_PointerOrReference;
  1943. if (PrevToken->Tok.isLiteral() ||
  1944. PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
  1945. tok::kw_false, tok::r_brace) ||
  1946. NextToken->Tok.isLiteral() ||
  1947. NextToken->isOneOf(tok::kw_true, tok::kw_false) ||
  1948. NextToken->isUnaryOperator() ||
  1949. // If we know we're in a template argument, there are no named
  1950. // declarations. Thus, having an identifier on the right-hand side
  1951. // indicates a binary operator.
  1952. (InTemplateArgument && NextToken->Tok.isAnyIdentifier()))
  1953. return TT_BinaryOperator;
  1954. // "&&(" is quite unlikely to be two successive unary "&".
  1955. if (Tok.is(tok::ampamp) && NextToken->is(tok::l_paren))
  1956. return TT_BinaryOperator;
  1957. // This catches some cases where evaluation order is used as control flow:
  1958. // aaa && aaa->f();
  1959. if (NextToken->Tok.isAnyIdentifier()) {
  1960. const FormatToken *NextNextToken = NextToken->getNextNonComment();
  1961. if (NextNextToken && NextNextToken->is(tok::arrow))
  1962. return TT_BinaryOperator;
  1963. }
  1964. // It is very unlikely that we are going to find a pointer or reference type
  1965. // definition on the RHS of an assignment.
  1966. if (IsExpression && !Contexts.back().CaretFound)
  1967. return TT_BinaryOperator;
  1968. return TT_PointerOrReference;
  1969. }
  1970. TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) {
  1971. const FormatToken *PrevToken = Tok.getPreviousNonComment();
  1972. if (!PrevToken)
  1973. return TT_UnaryOperator;
  1974. if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator))
  1975. // This must be a sequence of leading unary operators.
  1976. return TT_UnaryOperator;
  1977. // Use heuristics to recognize unary operators.
  1978. if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square,
  1979. tok::question, tok::colon, tok::kw_return,
  1980. tok::kw_case, tok::at, tok::l_brace, tok::kw_throw,
  1981. tok::kw_co_return, tok::kw_co_yield))
  1982. return TT_UnaryOperator;
  1983. // There can't be two consecutive binary operators.
  1984. if (PrevToken->is(TT_BinaryOperator))
  1985. return TT_UnaryOperator;
  1986. // Fall back to marking the token as binary operator.
  1987. return TT_BinaryOperator;
  1988. }
  1989. /// Determine whether ++/-- are pre- or post-increments/-decrements.
  1990. TokenType determineIncrementUsage(const FormatToken &Tok) {
  1991. const FormatToken *PrevToken = Tok.getPreviousNonComment();
  1992. if (!PrevToken || PrevToken->is(TT_CastRParen))
  1993. return TT_UnaryOperator;
  1994. if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
  1995. return TT_TrailingUnaryOperator;
  1996. return TT_UnaryOperator;
  1997. }
  1998. SmallVector<Context, 8> Contexts;
  1999. const FormatStyle &Style;
  2000. AnnotatedLine &Line;
  2001. FormatToken *CurrentToken;
  2002. bool AutoFound;
  2003. const AdditionalKeywords &Keywords;
  2004. // Set of "<" tokens that do not open a template parameter list. If parseAngle
  2005. // determines that a specific token can't be a template opener, it will make
  2006. // same decision irrespective of the decisions for tokens leading up to it.
  2007. // Store this information to prevent this from causing exponential runtime.
  2008. llvm::SmallPtrSet<FormatToken *, 16> NonTemplateLess;
  2009. };
  2010. static const int PrecedenceUnaryOperator = prec::PointerToMember + 1;
  2011. static const int PrecedenceArrowAndPeriod = prec::PointerToMember + 2;
  2012. /// Parses binary expressions by inserting fake parenthesis based on
  2013. /// operator precedence.
  2014. class ExpressionParser {
  2015. public:
  2016. ExpressionParser(const FormatStyle &Style, const AdditionalKeywords &Keywords,
  2017. AnnotatedLine &Line)
  2018. : Style(Style), Keywords(Keywords), Current(Line.First) {}
  2019. /// Parse expressions with the given operator precedence.
  2020. void parse(int Precedence = 0) {
  2021. // Skip 'return' and ObjC selector colons as they are not part of a binary
  2022. // expression.
  2023. while (Current && (Current->is(tok::kw_return) ||
  2024. (Current->is(tok::colon) &&
  2025. Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral))))
  2026. next();
  2027. if (!Current || Precedence > PrecedenceArrowAndPeriod)
  2028. return;
  2029. // Conditional expressions need to be parsed separately for proper nesting.
  2030. if (Precedence == prec::Conditional) {
  2031. parseConditionalExpr();
  2032. return;
  2033. }
  2034. // Parse unary operators, which all have a higher precedence than binary
  2035. // operators.
  2036. if (Precedence == PrecedenceUnaryOperator) {
  2037. parseUnaryOperator();
  2038. return;
  2039. }
  2040. FormatToken *Start = Current;
  2041. FormatToken *LatestOperator = nullptr;
  2042. unsigned OperatorIndex = 0;
  2043. while (Current) {
  2044. // Consume operators with higher precedence.
  2045. parse(Precedence + 1);
  2046. int CurrentPrecedence = getCurrentPrecedence();
  2047. if (Precedence == CurrentPrecedence && Current &&
  2048. Current->is(TT_SelectorName)) {
  2049. if (LatestOperator)
  2050. addFakeParenthesis(Start, prec::Level(Precedence));
  2051. Start = Current;
  2052. }
  2053. // At the end of the line or when an operator with higher precedence is
  2054. // found, insert fake parenthesis and return.
  2055. if (!Current ||
  2056. (Current->closesScope() &&
  2057. (Current->MatchingParen || Current->is(TT_TemplateString))) ||
  2058. (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
  2059. (CurrentPrecedence == prec::Conditional &&
  2060. Precedence == prec::Assignment && Current->is(tok::colon))) {
  2061. break;
  2062. }
  2063. // Consume scopes: (), [], <> and {}
  2064. if (Current->opensScope()) {
  2065. // In fragment of a JavaScript template string can look like '}..${' and
  2066. // thus close a scope and open a new one at the same time.
  2067. while (Current && (!Current->closesScope() || Current->opensScope())) {
  2068. next();
  2069. parse();
  2070. }
  2071. next();
  2072. } else {
  2073. // Operator found.
  2074. if (CurrentPrecedence == Precedence) {
  2075. if (LatestOperator)
  2076. LatestOperator->NextOperator = Current;
  2077. LatestOperator = Current;
  2078. Current->OperatorIndex = OperatorIndex;
  2079. ++OperatorIndex;
  2080. }
  2081. next(/*SkipPastLeadingComments=*/Precedence > 0);
  2082. }
  2083. }
  2084. if (LatestOperator && (Current || Precedence > 0)) {
  2085. // LatestOperator->LastOperator = true;
  2086. if (Precedence == PrecedenceArrowAndPeriod) {
  2087. // Call expressions don't have a binary operator precedence.
  2088. addFakeParenthesis(Start, prec::Unknown);
  2089. } else {
  2090. addFakeParenthesis(Start, prec::Level(Precedence));
  2091. }
  2092. }
  2093. }
  2094. private:
  2095. /// Gets the precedence (+1) of the given token for binary operators
  2096. /// and other tokens that we treat like binary operators.
  2097. int getCurrentPrecedence() {
  2098. if (Current) {
  2099. const FormatToken *NextNonComment = Current->getNextNonComment();
  2100. if (Current->is(TT_ConditionalExpr))
  2101. return prec::Conditional;
  2102. if (NextNonComment && Current->is(TT_SelectorName) &&
  2103. (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) ||
  2104. ((Style.Language == FormatStyle::LK_Proto ||
  2105. Style.Language == FormatStyle::LK_TextProto) &&
  2106. NextNonComment->is(tok::less))))
  2107. return prec::Assignment;
  2108. if (Current->is(TT_JsComputedPropertyName))
  2109. return prec::Assignment;
  2110. if (Current->is(TT_LambdaArrow))
  2111. return prec::Comma;
  2112. if (Current->is(TT_FatArrow))
  2113. return prec::Assignment;
  2114. if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) ||
  2115. (Current->is(tok::comment) && NextNonComment &&
  2116. NextNonComment->is(TT_SelectorName)))
  2117. return 0;
  2118. if (Current->is(TT_RangeBasedForLoopColon))
  2119. return prec::Comma;
  2120. if ((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) &&
  2121. Current->is(Keywords.kw_instanceof))
  2122. return prec::Relational;
  2123. if (Style.isJavaScript() &&
  2124. Current->isOneOf(Keywords.kw_in, Keywords.kw_as))
  2125. return prec::Relational;
  2126. if (Current->is(TT_BinaryOperator) || Current->is(tok::comma))
  2127. return Current->getPrecedence();
  2128. if (Current->isOneOf(tok::period, tok::arrow))
  2129. return PrecedenceArrowAndPeriod;
  2130. if ((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) &&
  2131. Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
  2132. Keywords.kw_throws))
  2133. return 0;
  2134. }
  2135. return -1;
  2136. }
  2137. void addFakeParenthesis(FormatToken *Start, prec::Level Precedence) {
  2138. Start->FakeLParens.push_back(Precedence);
  2139. if (Precedence > prec::Unknown)
  2140. Start->StartsBinaryExpression = true;
  2141. if (Current) {
  2142. FormatToken *Previous = Current->Previous;
  2143. while (Previous->is(tok::comment) && Previous->Previous)
  2144. Previous = Previous->Previous;
  2145. ++Previous->FakeRParens;
  2146. if (Precedence > prec::Unknown)
  2147. Previous->EndsBinaryExpression = true;
  2148. }
  2149. }
  2150. /// Parse unary operator expressions and surround them with fake
  2151. /// parentheses if appropriate.
  2152. void parseUnaryOperator() {
  2153. llvm::SmallVector<FormatToken *, 2> Tokens;
  2154. while (Current && Current->is(TT_UnaryOperator)) {
  2155. Tokens.push_back(Current);
  2156. next();
  2157. }
  2158. parse(PrecedenceArrowAndPeriod);
  2159. for (FormatToken *Token : llvm::reverse(Tokens))
  2160. // The actual precedence doesn't matter.
  2161. addFakeParenthesis(Token, prec::Unknown);
  2162. }
  2163. void parseConditionalExpr() {
  2164. while (Current && Current->isTrailingComment()) {
  2165. next();
  2166. }
  2167. FormatToken *Start = Current;
  2168. parse(prec::LogicalOr);
  2169. if (!Current || !Current->is(tok::question))
  2170. return;
  2171. next();
  2172. parse(prec::Assignment);
  2173. if (!Current || Current->isNot(TT_ConditionalExpr))
  2174. return;
  2175. next();
  2176. parse(prec::Assignment);
  2177. addFakeParenthesis(Start, prec::Conditional);
  2178. }
  2179. void next(bool SkipPastLeadingComments = true) {
  2180. if (Current)
  2181. Current = Current->Next;
  2182. while (Current &&
  2183. (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
  2184. Current->isTrailingComment())
  2185. Current = Current->Next;
  2186. }
  2187. const FormatStyle &Style;
  2188. const AdditionalKeywords &Keywords;
  2189. FormatToken *Current;
  2190. };
  2191. } // end anonymous namespace
  2192. void TokenAnnotator::setCommentLineLevels(
  2193. SmallVectorImpl<AnnotatedLine *> &Lines) {
  2194. const AnnotatedLine *NextNonCommentLine = nullptr;
  2195. for (AnnotatedLine *Line : llvm::reverse(Lines)) {
  2196. assert(Line->First);
  2197. bool CommentLine = true;
  2198. for (const FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) {
  2199. if (!Tok->is(tok::comment)) {
  2200. CommentLine = false;
  2201. break;
  2202. }
  2203. }
  2204. // If the comment is currently aligned with the line immediately following
  2205. // it, that's probably intentional and we should keep it.
  2206. if (NextNonCommentLine && CommentLine &&
  2207. NextNonCommentLine->First->NewlinesBefore <= 1 &&
  2208. NextNonCommentLine->First->OriginalColumn ==
  2209. Line->First->OriginalColumn) {
  2210. // Align comments for preprocessor lines with the # in column 0 if
  2211. // preprocessor lines are not indented. Otherwise, align with the next
  2212. // line.
  2213. Line->Level =
  2214. (Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash &&
  2215. (NextNonCommentLine->Type == LT_PreprocessorDirective ||
  2216. NextNonCommentLine->Type == LT_ImportStatement))
  2217. ? 0
  2218. : NextNonCommentLine->Level;
  2219. } else {
  2220. NextNonCommentLine = Line->First->isNot(tok::r_brace) ? Line : nullptr;
  2221. }
  2222. setCommentLineLevels(Line->Children);
  2223. }
  2224. }
  2225. static unsigned maxNestingDepth(const AnnotatedLine &Line) {
  2226. unsigned Result = 0;
  2227. for (const auto *Tok = Line.First; Tok != nullptr; Tok = Tok->Next)
  2228. Result = std::max(Result, Tok->NestingLevel);
  2229. return Result;
  2230. }
  2231. void TokenAnnotator::annotate(AnnotatedLine &Line) {
  2232. for (auto &Child : Line.Children)
  2233. annotate(*Child);
  2234. AnnotatingParser Parser(Style, Line, Keywords);
  2235. Line.Type = Parser.parseLine();
  2236. // With very deep nesting, ExpressionParser uses lots of stack and the
  2237. // formatting algorithm is very slow. We're not going to do a good job here
  2238. // anyway - it's probably generated code being formatted by mistake.
  2239. // Just skip the whole line.
  2240. if (maxNestingDepth(Line) > 50)
  2241. Line.Type = LT_Invalid;
  2242. if (Line.Type == LT_Invalid)
  2243. return;
  2244. ExpressionParser ExprParser(Style, Keywords, Line);
  2245. ExprParser.parse();
  2246. if (Line.startsWith(TT_ObjCMethodSpecifier))
  2247. Line.Type = LT_ObjCMethodDecl;
  2248. else if (Line.startsWith(TT_ObjCDecl))
  2249. Line.Type = LT_ObjCDecl;
  2250. else if (Line.startsWith(TT_ObjCProperty))
  2251. Line.Type = LT_ObjCProperty;
  2252. Line.First->SpacesRequiredBefore = 1;
  2253. Line.First->CanBreakBefore = Line.First->MustBreakBefore;
  2254. }
  2255. // This function heuristically determines whether 'Current' starts the name of a
  2256. // function declaration.
  2257. static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
  2258. const AnnotatedLine &Line) {
  2259. auto skipOperatorName = [](const FormatToken *Next) -> const FormatToken * {
  2260. for (; Next; Next = Next->Next) {
  2261. if (Next->is(TT_OverloadedOperatorLParen))
  2262. return Next;
  2263. if (Next->is(TT_OverloadedOperator))
  2264. continue;
  2265. if (Next->isOneOf(tok::kw_new, tok::kw_delete)) {
  2266. // For 'new[]' and 'delete[]'.
  2267. if (Next->Next &&
  2268. Next->Next->startsSequence(tok::l_square, tok::r_square))
  2269. Next = Next->Next->Next;
  2270. continue;
  2271. }
  2272. if (Next->startsSequence(tok::l_square, tok::r_square)) {
  2273. // For operator[]().
  2274. Next = Next->Next;
  2275. continue;
  2276. }
  2277. if ((Next->isSimpleTypeSpecifier() || Next->is(tok::identifier)) &&
  2278. Next->Next && Next->Next->isOneOf(tok::star, tok::amp, tok::ampamp)) {
  2279. // For operator void*(), operator char*(), operator Foo*().
  2280. Next = Next->Next;
  2281. continue;
  2282. }
  2283. if (Next->is(TT_TemplateOpener) && Next->MatchingParen) {
  2284. Next = Next->MatchingParen;
  2285. continue;
  2286. }
  2287. break;
  2288. }
  2289. return nullptr;
  2290. };
  2291. // Find parentheses of parameter list.
  2292. const FormatToken *Next = Current.Next;
  2293. if (Current.is(tok::kw_operator)) {
  2294. if (Current.Previous && Current.Previous->is(tok::coloncolon))
  2295. return false;
  2296. Next = skipOperatorName(Next);
  2297. } else {
  2298. if (!Current.is(TT_StartOfName) || Current.NestingLevel != 0)
  2299. return false;
  2300. for (; Next; Next = Next->Next) {
  2301. if (Next->is(TT_TemplateOpener)) {
  2302. Next = Next->MatchingParen;
  2303. } else if (Next->is(tok::coloncolon)) {
  2304. Next = Next->Next;
  2305. if (!Next)
  2306. return false;
  2307. if (Next->is(tok::kw_operator)) {
  2308. Next = skipOperatorName(Next->Next);
  2309. break;
  2310. }
  2311. if (!Next->is(tok::identifier))
  2312. return false;
  2313. } else if (Next->is(tok::l_paren)) {
  2314. break;
  2315. } else {
  2316. return false;
  2317. }
  2318. }
  2319. }
  2320. // Check whether parameter list can belong to a function declaration.
  2321. if (!Next || !Next->is(tok::l_paren) || !Next->MatchingParen)
  2322. return false;
  2323. // If the lines ends with "{", this is likely a function definition.
  2324. if (Line.Last->is(tok::l_brace))
  2325. return true;
  2326. if (Next->Next == Next->MatchingParen)
  2327. return true; // Empty parentheses.
  2328. // If there is an &/&& after the r_paren, this is likely a function.
  2329. if (Next->MatchingParen->Next &&
  2330. Next->MatchingParen->Next->is(TT_PointerOrReference))
  2331. return true;
  2332. // Check for K&R C function definitions (and C++ function definitions with
  2333. // unnamed parameters), e.g.:
  2334. // int f(i)
  2335. // {
  2336. // return i + 1;
  2337. // }
  2338. // bool g(size_t = 0, bool b = false)
  2339. // {
  2340. // return !b;
  2341. // }
  2342. if (IsCpp && Next->Next && Next->Next->is(tok::identifier) &&
  2343. !Line.endsWith(tok::semi))
  2344. return true;
  2345. for (const FormatToken *Tok = Next->Next; Tok && Tok != Next->MatchingParen;
  2346. Tok = Tok->Next) {
  2347. if (Tok->is(TT_TypeDeclarationParen))
  2348. return true;
  2349. if (Tok->isOneOf(tok::l_paren, TT_TemplateOpener) && Tok->MatchingParen) {
  2350. Tok = Tok->MatchingParen;
  2351. continue;
  2352. }
  2353. if (Tok->is(tok::kw_const) || Tok->isSimpleTypeSpecifier() ||
  2354. Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis))
  2355. return true;
  2356. if (Tok->isOneOf(tok::l_brace, tok::string_literal, TT_ObjCMethodExpr) ||
  2357. Tok->Tok.isLiteral())
  2358. return false;
  2359. }
  2360. return false;
  2361. }
  2362. bool TokenAnnotator::mustBreakForReturnType(const AnnotatedLine &Line) const {
  2363. assert(Line.MightBeFunctionDecl);
  2364. if ((Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_TopLevel ||
  2365. Style.AlwaysBreakAfterReturnType ==
  2366. FormatStyle::RTBS_TopLevelDefinitions) &&
  2367. Line.Level > 0)
  2368. return false;
  2369. switch (Style.AlwaysBreakAfterReturnType) {
  2370. case FormatStyle::RTBS_None:
  2371. return false;
  2372. case FormatStyle::RTBS_All:
  2373. case FormatStyle::RTBS_TopLevel:
  2374. return true;
  2375. case FormatStyle::RTBS_AllDefinitions:
  2376. case FormatStyle::RTBS_TopLevelDefinitions:
  2377. return Line.mightBeFunctionDefinition();
  2378. }
  2379. return false;
  2380. }
  2381. void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) {
  2382. for (AnnotatedLine *ChildLine : Line.Children)
  2383. calculateFormattingInformation(*ChildLine);
  2384. Line.First->TotalLength =
  2385. Line.First->IsMultiline ? Style.ColumnLimit
  2386. : Line.FirstStartColumn + Line.First->ColumnWidth;
  2387. FormatToken *Current = Line.First->Next;
  2388. bool InFunctionDecl = Line.MightBeFunctionDecl;
  2389. bool AlignArrayOfStructures =
  2390. (Style.AlignArrayOfStructures != FormatStyle::AIAS_None &&
  2391. Line.Type == LT_ArrayOfStructInitializer);
  2392. if (AlignArrayOfStructures)
  2393. calculateArrayInitializerColumnList(Line);
  2394. while (Current) {
  2395. if (isFunctionDeclarationName(Style.isCpp(), *Current, Line))
  2396. Current->setType(TT_FunctionDeclarationName);
  2397. const FormatToken *Prev = Current->Previous;
  2398. if (Current->is(TT_LineComment)) {
  2399. if (Prev->is(BK_BracedInit) && Prev->opensScope())
  2400. Current->SpacesRequiredBefore =
  2401. (Style.Cpp11BracedListStyle && !Style.SpacesInParentheses) ? 0 : 1;
  2402. else
  2403. Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
  2404. // If we find a trailing comment, iterate backwards to determine whether
  2405. // it seems to relate to a specific parameter. If so, break before that
  2406. // parameter to avoid changing the comment's meaning. E.g. don't move 'b'
  2407. // to the previous line in:
  2408. // SomeFunction(a,
  2409. // b, // comment
  2410. // c);
  2411. if (!Current->HasUnescapedNewline) {
  2412. for (FormatToken *Parameter = Current->Previous; Parameter;
  2413. Parameter = Parameter->Previous) {
  2414. if (Parameter->isOneOf(tok::comment, tok::r_brace))
  2415. break;
  2416. if (Parameter->Previous && Parameter->Previous->is(tok::comma)) {
  2417. if (!Parameter->Previous->is(TT_CtorInitializerComma) &&
  2418. Parameter->HasUnescapedNewline)
  2419. Parameter->MustBreakBefore = true;
  2420. break;
  2421. }
  2422. }
  2423. }
  2424. } else if (Current->SpacesRequiredBefore == 0 &&
  2425. spaceRequiredBefore(Line, *Current)) {
  2426. Current->SpacesRequiredBefore = 1;
  2427. }
  2428. Current->MustBreakBefore =
  2429. Current->MustBreakBefore || mustBreakBefore(Line, *Current);
  2430. if (!Current->MustBreakBefore && InFunctionDecl &&
  2431. Current->is(TT_FunctionDeclarationName))
  2432. Current->MustBreakBefore = mustBreakForReturnType(Line);
  2433. Current->CanBreakBefore =
  2434. Current->MustBreakBefore || canBreakBefore(Line, *Current);
  2435. unsigned ChildSize = 0;
  2436. if (Prev->Children.size() == 1) {
  2437. FormatToken &LastOfChild = *Prev->Children[0]->Last;
  2438. ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit
  2439. : LastOfChild.TotalLength + 1;
  2440. }
  2441. if (Current->MustBreakBefore || Prev->Children.size() > 1 ||
  2442. (Prev->Children.size() == 1 &&
  2443. Prev->Children[0]->First->MustBreakBefore) ||
  2444. Current->IsMultiline)
  2445. Current->TotalLength = Prev->TotalLength + Style.ColumnLimit;
  2446. else
  2447. Current->TotalLength = Prev->TotalLength + Current->ColumnWidth +
  2448. ChildSize + Current->SpacesRequiredBefore;
  2449. if (Current->is(TT_CtorInitializerColon))
  2450. InFunctionDecl = false;
  2451. // FIXME: Only calculate this if CanBreakBefore is true once static
  2452. // initializers etc. are sorted out.
  2453. // FIXME: Move magic numbers to a better place.
  2454. // Reduce penalty for aligning ObjC method arguments using the colon
  2455. // alignment as this is the canonical way (still prefer fitting everything
  2456. // into one line if possible). Trying to fit a whole expression into one
  2457. // line should not force other line breaks (e.g. when ObjC method
  2458. // expression is a part of other expression).
  2459. Current->SplitPenalty = splitPenalty(Line, *Current, InFunctionDecl);
  2460. if (Style.Language == FormatStyle::LK_ObjC &&
  2461. Current->is(TT_SelectorName) && Current->ParameterIndex > 0) {
  2462. if (Current->ParameterIndex == 1)
  2463. Current->SplitPenalty += 5 * Current->BindingStrength;
  2464. } else {
  2465. Current->SplitPenalty += 20 * Current->BindingStrength;
  2466. }
  2467. Current = Current->Next;
  2468. }
  2469. calculateUnbreakableTailLengths(Line);
  2470. unsigned IndentLevel = Line.Level;
  2471. for (Current = Line.First; Current != nullptr; Current = Current->Next) {
  2472. if (Current->Role)
  2473. Current->Role->precomputeFormattingInfos(Current);
  2474. if (Current->MatchingParen &&
  2475. Current->MatchingParen->opensBlockOrBlockTypeList(Style) &&
  2476. IndentLevel > 0) {
  2477. --IndentLevel;
  2478. }
  2479. Current->IndentLevel = IndentLevel;
  2480. if (Current->opensBlockOrBlockTypeList(Style))
  2481. ++IndentLevel;
  2482. }
  2483. LLVM_DEBUG({ printDebugInfo(Line); });
  2484. }
  2485. void TokenAnnotator::calculateUnbreakableTailLengths(AnnotatedLine &Line) {
  2486. unsigned UnbreakableTailLength = 0;
  2487. FormatToken *Current = Line.Last;
  2488. while (Current) {
  2489. Current->UnbreakableTailLength = UnbreakableTailLength;
  2490. if (Current->CanBreakBefore ||
  2491. Current->isOneOf(tok::comment, tok::string_literal)) {
  2492. UnbreakableTailLength = 0;
  2493. } else {
  2494. UnbreakableTailLength +=
  2495. Current->ColumnWidth + Current->SpacesRequiredBefore;
  2496. }
  2497. Current = Current->Previous;
  2498. }
  2499. }
  2500. void TokenAnnotator::calculateArrayInitializerColumnList(AnnotatedLine &Line) {
  2501. if (Line.First == Line.Last) {
  2502. return;
  2503. }
  2504. auto *CurrentToken = Line.First;
  2505. CurrentToken->ArrayInitializerLineStart = true;
  2506. unsigned Depth = 0;
  2507. while (CurrentToken != nullptr && CurrentToken != Line.Last) {
  2508. if (CurrentToken->is(tok::l_brace)) {
  2509. CurrentToken->IsArrayInitializer = true;
  2510. if (CurrentToken->Next != nullptr)
  2511. CurrentToken->Next->MustBreakBefore = true;
  2512. CurrentToken =
  2513. calculateInitializerColumnList(Line, CurrentToken->Next, Depth + 1);
  2514. } else {
  2515. CurrentToken = CurrentToken->Next;
  2516. }
  2517. }
  2518. }
  2519. FormatToken *TokenAnnotator::calculateInitializerColumnList(
  2520. AnnotatedLine &Line, FormatToken *CurrentToken, unsigned Depth) {
  2521. while (CurrentToken != nullptr && CurrentToken != Line.Last) {
  2522. if (CurrentToken->is(tok::l_brace))
  2523. ++Depth;
  2524. else if (CurrentToken->is(tok::r_brace))
  2525. --Depth;
  2526. if (Depth == 2 && CurrentToken->isOneOf(tok::l_brace, tok::comma)) {
  2527. CurrentToken = CurrentToken->Next;
  2528. if (CurrentToken == nullptr)
  2529. break;
  2530. CurrentToken->StartsColumn = true;
  2531. CurrentToken = CurrentToken->Previous;
  2532. }
  2533. CurrentToken = CurrentToken->Next;
  2534. }
  2535. return CurrentToken;
  2536. }
  2537. unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
  2538. const FormatToken &Tok,
  2539. bool InFunctionDecl) {
  2540. const FormatToken &Left = *Tok.Previous;
  2541. const FormatToken &Right = Tok;
  2542. if (Left.is(tok::semi))
  2543. return 0;
  2544. if (Style.Language == FormatStyle::LK_Java) {
  2545. if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
  2546. return 1;
  2547. if (Right.is(Keywords.kw_implements))
  2548. return 2;
  2549. if (Left.is(tok::comma) && Left.NestingLevel == 0)
  2550. return 3;
  2551. } else if (Style.isJavaScript()) {
  2552. if (Right.is(Keywords.kw_function) && Left.isNot(tok::comma))
  2553. return 100;
  2554. if (Left.is(TT_JsTypeColon))
  2555. return 35;
  2556. if ((Left.is(TT_TemplateString) && Left.TokenText.endswith("${")) ||
  2557. (Right.is(TT_TemplateString) && Right.TokenText.startswith("}")))
  2558. return 100;
  2559. // Prefer breaking call chains (".foo") over empty "{}", "[]" or "()".
  2560. if (Left.opensScope() && Right.closesScope())
  2561. return 200;
  2562. }
  2563. if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
  2564. return 1;
  2565. if (Right.is(tok::l_square)) {
  2566. if (Style.Language == FormatStyle::LK_Proto)
  2567. return 1;
  2568. if (Left.is(tok::r_square))
  2569. return 200;
  2570. // Slightly prefer formatting local lambda definitions like functions.
  2571. if (Right.is(TT_LambdaLSquare) && Left.is(tok::equal))
  2572. return 35;
  2573. if (!Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
  2574. TT_ArrayInitializerLSquare,
  2575. TT_DesignatedInitializerLSquare, TT_AttributeSquare))
  2576. return 500;
  2577. }
  2578. if (Left.is(tok::coloncolon) ||
  2579. (Right.is(tok::period) && Style.Language == FormatStyle::LK_Proto))
  2580. return 500;
  2581. if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
  2582. Right.is(tok::kw_operator)) {
  2583. if (Line.startsWith(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
  2584. return 3;
  2585. if (Left.is(TT_StartOfName))
  2586. return 110;
  2587. if (InFunctionDecl && Right.NestingLevel == 0)
  2588. return Style.PenaltyReturnTypeOnItsOwnLine;
  2589. return 200;
  2590. }
  2591. if (Right.is(TT_PointerOrReference))
  2592. return 190;
  2593. if (Right.is(TT_LambdaArrow))
  2594. return 110;
  2595. if (Left.is(tok::equal) && Right.is(tok::l_brace))
  2596. return 160;
  2597. if (Left.is(TT_CastRParen))
  2598. return 100;
  2599. if (Left.isOneOf(tok::kw_class, tok::kw_struct))
  2600. return 5000;
  2601. if (Left.is(tok::comment))
  2602. return 1000;
  2603. if (Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
  2604. TT_CtorInitializerColon))
  2605. return 2;
  2606. if (Right.isMemberAccess()) {
  2607. // Breaking before the "./->" of a chained call/member access is reasonably
  2608. // cheap, as formatting those with one call per line is generally
  2609. // desirable. In particular, it should be cheaper to break before the call
  2610. // than it is to break inside a call's parameters, which could lead to weird
  2611. // "hanging" indents. The exception is the very last "./->" to support this
  2612. // frequent pattern:
  2613. //
  2614. // aaaaaaaa.aaaaaaaa.bbbbbbb().ccccccccccccccccccccc(
  2615. // dddddddd);
  2616. //
  2617. // which might otherwise be blown up onto many lines. Here, clang-format
  2618. // won't produce "hanging" indents anyway as there is no other trailing
  2619. // call.
  2620. //
  2621. // Also apply higher penalty is not a call as that might lead to a wrapping
  2622. // like:
  2623. //
  2624. // aaaaaaa
  2625. // .aaaaaaaaa.bbbbbbbb(cccccccc);
  2626. return !Right.NextOperator || !Right.NextOperator->Previous->closesScope()
  2627. ? 150
  2628. : 35;
  2629. }
  2630. if (Right.is(TT_TrailingAnnotation) &&
  2631. (!Right.Next || Right.Next->isNot(tok::l_paren))) {
  2632. // Moving trailing annotations to the next line is fine for ObjC method
  2633. // declarations.
  2634. if (Line.startsWith(TT_ObjCMethodSpecifier))
  2635. return 10;
  2636. // Generally, breaking before a trailing annotation is bad unless it is
  2637. // function-like. It seems to be especially preferable to keep standard
  2638. // annotations (i.e. "const", "final" and "override") on the same line.
  2639. // Use a slightly higher penalty after ")" so that annotations like
  2640. // "const override" are kept together.
  2641. bool is_short_annotation = Right.TokenText.size() < 10;
  2642. return (Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
  2643. }
  2644. // In for-loops, prefer breaking at ',' and ';'.
  2645. if (Line.startsWith(tok::kw_for) && Left.is(tok::equal))
  2646. return 4;
  2647. // In Objective-C method expressions, prefer breaking before "param:" over
  2648. // breaking after it.
  2649. if (Right.is(TT_SelectorName))
  2650. return 0;
  2651. if (Left.is(tok::colon) && Left.is(TT_ObjCMethodExpr))
  2652. return Line.MightBeFunctionDecl ? 50 : 500;
  2653. // In Objective-C type declarations, avoid breaking after the category's
  2654. // open paren (we'll prefer breaking after the protocol list's opening
  2655. // angle bracket, if present).
  2656. if (Line.Type == LT_ObjCDecl && Left.is(tok::l_paren) && Left.Previous &&
  2657. Left.Previous->isOneOf(tok::identifier, tok::greater))
  2658. return 500;
  2659. if (Left.is(tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0)
  2660. return Style.PenaltyBreakOpenParenthesis;
  2661. if (Left.is(tok::l_paren) && InFunctionDecl &&
  2662. Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign)
  2663. return 100;
  2664. if (Left.is(tok::l_paren) && Left.Previous &&
  2665. (Left.Previous->is(tok::kw_for) || Left.Previous->isIf()))
  2666. return 1000;
  2667. if (Left.is(tok::equal) && InFunctionDecl)
  2668. return 110;
  2669. if (Right.is(tok::r_brace))
  2670. return 1;
  2671. if (Left.is(TT_TemplateOpener))
  2672. return 100;
  2673. if (Left.opensScope()) {
  2674. // If we aren't aligning after opening parens/braces we can always break
  2675. // here unless the style does not want us to place all arguments on the
  2676. // next line.
  2677. if (Style.AlignAfterOpenBracket == FormatStyle::BAS_DontAlign &&
  2678. (Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine))
  2679. return 0;
  2680. if (Left.is(tok::l_brace) && !Style.Cpp11BracedListStyle)
  2681. return 19;
  2682. return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
  2683. : 19;
  2684. }
  2685. if (Left.is(TT_JavaAnnotation))
  2686. return 50;
  2687. if (Left.is(TT_UnaryOperator))
  2688. return 60;
  2689. if (Left.isOneOf(tok::plus, tok::comma) && Left.Previous &&
  2690. Left.Previous->isLabelString() &&
  2691. (Left.NextOperator || Left.OperatorIndex != 0))
  2692. return 50;
  2693. if (Right.is(tok::plus) && Left.isLabelString() &&
  2694. (Right.NextOperator || Right.OperatorIndex != 0))
  2695. return 25;
  2696. if (Left.is(tok::comma))
  2697. return 1;
  2698. if (Right.is(tok::lessless) && Left.isLabelString() &&
  2699. (Right.NextOperator || Right.OperatorIndex != 1))
  2700. return 25;
  2701. if (Right.is(tok::lessless)) {
  2702. // Breaking at a << is really cheap.
  2703. if (!Left.is(tok::r_paren) || Right.OperatorIndex > 0)
  2704. // Slightly prefer to break before the first one in log-like statements.
  2705. return 2;
  2706. return 1;
  2707. }
  2708. if (Left.ClosesTemplateDeclaration)
  2709. return Style.PenaltyBreakTemplateDeclaration;
  2710. if (Left.is(TT_ConditionalExpr))
  2711. return prec::Conditional;
  2712. prec::Level Level = Left.getPrecedence();
  2713. if (Level == prec::Unknown)
  2714. Level = Right.getPrecedence();
  2715. if (Level == prec::Assignment)
  2716. return Style.PenaltyBreakAssignment;
  2717. if (Level != prec::Unknown)
  2718. return Level;
  2719. return 3;
  2720. }
  2721. bool TokenAnnotator::spaceRequiredBeforeParens(const FormatToken &Right) const {
  2722. if (Style.SpaceBeforeParens == FormatStyle::SBPO_Always)
  2723. return true;
  2724. if (Right.is(TT_OverloadedOperatorLParen) &&
  2725. Style.SpaceBeforeParensOptions.AfterOverloadedOperator)
  2726. return true;
  2727. if (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses &&
  2728. Right.ParameterCount > 0)
  2729. return true;
  2730. return false;
  2731. }
  2732. bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
  2733. const FormatToken &Left,
  2734. const FormatToken &Right) {
  2735. if (Left.is(tok::kw_return) && Right.isNot(tok::semi))
  2736. return true;
  2737. if (Style.isJson() && Left.is(tok::string_literal) && Right.is(tok::colon))
  2738. return false;
  2739. if (Left.is(Keywords.kw_assert) && Style.Language == FormatStyle::LK_Java)
  2740. return true;
  2741. if (Style.ObjCSpaceAfterProperty && Line.Type == LT_ObjCProperty &&
  2742. Left.Tok.getObjCKeywordID() == tok::objc_property)
  2743. return true;
  2744. if (Right.is(tok::hashhash))
  2745. return Left.is(tok::hash);
  2746. if (Left.isOneOf(tok::hashhash, tok::hash))
  2747. return Right.is(tok::hash);
  2748. if ((Left.is(tok::l_paren) && Right.is(tok::r_paren)) ||
  2749. (Left.is(tok::l_brace) && Left.isNot(BK_Block) &&
  2750. Right.is(tok::r_brace) && Right.isNot(BK_Block)))
  2751. return Style.SpaceInEmptyParentheses;
  2752. if (Style.SpacesInConditionalStatement) {
  2753. if (Left.is(tok::l_paren) && Left.Previous &&
  2754. isKeywordWithCondition(*Left.Previous))
  2755. return true;
  2756. if (Right.is(tok::r_paren) && Right.MatchingParen &&
  2757. Right.MatchingParen->Previous &&
  2758. isKeywordWithCondition(*Right.MatchingParen->Previous))
  2759. return true;
  2760. }
  2761. // auto{x} auto(x)
  2762. if (Left.is(tok::kw_auto) && Right.isOneOf(tok::l_paren, tok::l_brace))
  2763. return false;
  2764. // operator co_await(x)
  2765. if (Right.is(tok::l_paren) && Left.is(tok::kw_co_await) && Left.Previous &&
  2766. Left.Previous->is(tok::kw_operator))
  2767. return false;
  2768. // co_await (x), co_yield (x), co_return (x)
  2769. if (Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
  2770. Right.isNot(tok::semi))
  2771. return true;
  2772. // requires clause Concept1<T> && Concept2<T>
  2773. if (Left.is(TT_ConstraintJunctions) && Right.is(tok::identifier))
  2774. return true;
  2775. if (Left.is(tok::l_paren) || Right.is(tok::r_paren))
  2776. return (Right.is(TT_CastRParen) ||
  2777. (Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
  2778. ? Style.SpacesInCStyleCastParentheses
  2779. : Style.SpacesInParentheses;
  2780. if (Right.isOneOf(tok::semi, tok::comma))
  2781. return false;
  2782. if (Right.is(tok::less) && Line.Type == LT_ObjCDecl) {
  2783. bool IsLightweightGeneric = Right.MatchingParen &&
  2784. Right.MatchingParen->Next &&
  2785. Right.MatchingParen->Next->is(tok::colon);
  2786. return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList;
  2787. }
  2788. if (Right.is(tok::less) && Left.is(tok::kw_template))
  2789. return Style.SpaceAfterTemplateKeyword;
  2790. if (Left.isOneOf(tok::exclaim, tok::tilde))
  2791. return false;
  2792. if (Left.is(tok::at) &&
  2793. Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
  2794. tok::numeric_constant, tok::l_paren, tok::l_brace,
  2795. tok::kw_true, tok::kw_false))
  2796. return false;
  2797. if (Left.is(tok::colon))
  2798. return !Left.is(TT_ObjCMethodExpr);
  2799. if (Left.is(tok::coloncolon))
  2800. return false;
  2801. if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less)) {
  2802. if (Style.Language == FormatStyle::LK_TextProto ||
  2803. (Style.Language == FormatStyle::LK_Proto &&
  2804. (Left.is(TT_DictLiteral) || Right.is(TT_DictLiteral)))) {
  2805. // Format empty list as `<>`.
  2806. if (Left.is(tok::less) && Right.is(tok::greater))
  2807. return false;
  2808. return !Style.Cpp11BracedListStyle;
  2809. }
  2810. return false;
  2811. }
  2812. if (Right.is(tok::ellipsis))
  2813. return Left.Tok.isLiteral() || (Left.is(tok::identifier) && Left.Previous &&
  2814. Left.Previous->is(tok::kw_case));
  2815. if (Left.is(tok::l_square) && Right.is(tok::amp))
  2816. return Style.SpacesInSquareBrackets;
  2817. if (Right.is(TT_PointerOrReference)) {
  2818. if (Left.is(tok::r_paren) && Line.MightBeFunctionDecl) {
  2819. if (!Left.MatchingParen)
  2820. return true;
  2821. FormatToken *TokenBeforeMatchingParen =
  2822. Left.MatchingParen->getPreviousNonComment();
  2823. if (!TokenBeforeMatchingParen || !Left.is(TT_TypeDeclarationParen))
  2824. return true;
  2825. }
  2826. // Add a space if the previous token is a pointer qualifier or the closing
  2827. // parenthesis of __attribute__(()) expression and the style requires spaces
  2828. // after pointer qualifiers.
  2829. if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_After ||
  2830. Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
  2831. (Left.is(TT_AttributeParen) || Left.canBePointerOrReferenceQualifier()))
  2832. return true;
  2833. if (Left.Tok.isLiteral())
  2834. return true;
  2835. // for (auto a = 0, b = 0; const auto & c : {1, 2, 3})
  2836. if (Left.isTypeOrIdentifier() && Right.Next && Right.Next->Next &&
  2837. Right.Next->Next->is(TT_RangeBasedForLoopColon))
  2838. return getTokenPointerOrReferenceAlignment(Right) !=
  2839. FormatStyle::PAS_Left;
  2840. return (
  2841. (!Left.isOneOf(TT_PointerOrReference, tok::l_paren) &&
  2842. (getTokenPointerOrReferenceAlignment(Right) != FormatStyle::PAS_Left ||
  2843. (Line.IsMultiVariableDeclStmt &&
  2844. (Left.NestingLevel == 0 ||
  2845. (Left.NestingLevel == 1 && Line.First->is(tok::kw_for)))))));
  2846. }
  2847. if (Right.is(TT_FunctionTypeLParen) && Left.isNot(tok::l_paren) &&
  2848. (!Left.is(TT_PointerOrReference) ||
  2849. (getTokenPointerOrReferenceAlignment(Left) != FormatStyle::PAS_Right &&
  2850. !Line.IsMultiVariableDeclStmt)))
  2851. return true;
  2852. if (Left.is(TT_PointerOrReference)) {
  2853. // Add a space if the next token is a pointer qualifier and the style
  2854. // requires spaces before pointer qualifiers.
  2855. if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Before ||
  2856. Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
  2857. Right.canBePointerOrReferenceQualifier())
  2858. return true;
  2859. // & 1
  2860. if (Right.Tok.isLiteral())
  2861. return true;
  2862. // & /* comment
  2863. if (Right.is(TT_BlockComment))
  2864. return true;
  2865. // foo() -> const Bar * override/final
  2866. if (Right.isOneOf(Keywords.kw_override, Keywords.kw_final) &&
  2867. !Right.is(TT_StartOfName))
  2868. return true;
  2869. // & {
  2870. if (Right.is(tok::l_brace) && Right.is(BK_Block))
  2871. return true;
  2872. // for (auto a = 0, b = 0; const auto& c : {1, 2, 3})
  2873. if (Left.Previous && Left.Previous->isTypeOrIdentifier() && Right.Next &&
  2874. Right.Next->is(TT_RangeBasedForLoopColon))
  2875. return getTokenPointerOrReferenceAlignment(Left) !=
  2876. FormatStyle::PAS_Right;
  2877. return !Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
  2878. tok::l_paren) &&
  2879. (getTokenPointerOrReferenceAlignment(Left) !=
  2880. FormatStyle::PAS_Right &&
  2881. !Line.IsMultiVariableDeclStmt) &&
  2882. Left.Previous &&
  2883. !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon,
  2884. tok::l_square);
  2885. }
  2886. // Ensure right pointer alignment with ellipsis e.g. int *...P
  2887. if (Left.is(tok::ellipsis) && Left.Previous &&
  2888. Left.Previous->isOneOf(tok::star, tok::amp, tok::ampamp))
  2889. return Style.PointerAlignment != FormatStyle::PAS_Right;
  2890. if (Right.is(tok::star) && Left.is(tok::l_paren))
  2891. return false;
  2892. if (Left.is(tok::star) && Right.isOneOf(tok::star, tok::amp, tok::ampamp))
  2893. return false;
  2894. if (Right.isOneOf(tok::star, tok::amp, tok::ampamp)) {
  2895. const FormatToken *Previous = &Left;
  2896. while (Previous && !Previous->is(tok::kw_operator)) {
  2897. if (Previous->is(tok::identifier) || Previous->isSimpleTypeSpecifier()) {
  2898. Previous = Previous->getPreviousNonComment();
  2899. continue;
  2900. }
  2901. if (Previous->is(TT_TemplateCloser) && Previous->MatchingParen) {
  2902. Previous = Previous->MatchingParen->getPreviousNonComment();
  2903. continue;
  2904. }
  2905. if (Previous->is(tok::coloncolon)) {
  2906. Previous = Previous->getPreviousNonComment();
  2907. continue;
  2908. }
  2909. break;
  2910. }
  2911. // Space between the type and the * in:
  2912. // operator void*()
  2913. // operator char*()
  2914. // operator void const*()
  2915. // operator void volatile*()
  2916. // operator /*comment*/ const char*()
  2917. // operator volatile /*comment*/ char*()
  2918. // operator Foo*()
  2919. // operator C<T>*()
  2920. // operator std::Foo*()
  2921. // operator C<T>::D<U>*()
  2922. // dependent on PointerAlignment style.
  2923. if (Previous) {
  2924. if (Previous->endsSequence(tok::kw_operator))
  2925. return (Style.PointerAlignment != FormatStyle::PAS_Left);
  2926. if (Previous->is(tok::kw_const) || Previous->is(tok::kw_volatile))
  2927. return (Style.PointerAlignment != FormatStyle::PAS_Left) ||
  2928. (Style.SpaceAroundPointerQualifiers ==
  2929. FormatStyle::SAPQ_After) ||
  2930. (Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both);
  2931. }
  2932. }
  2933. const auto SpaceRequiredForArrayInitializerLSquare =
  2934. [](const FormatToken &LSquareTok, const FormatStyle &Style) {
  2935. return Style.SpacesInContainerLiterals ||
  2936. ((Style.Language == FormatStyle::LK_Proto ||
  2937. Style.Language == FormatStyle::LK_TextProto) &&
  2938. !Style.Cpp11BracedListStyle &&
  2939. LSquareTok.endsSequence(tok::l_square, tok::colon,
  2940. TT_SelectorName));
  2941. };
  2942. if (Left.is(tok::l_square))
  2943. return (Left.is(TT_ArrayInitializerLSquare) && Right.isNot(tok::r_square) &&
  2944. SpaceRequiredForArrayInitializerLSquare(Left, Style)) ||
  2945. (Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare,
  2946. TT_LambdaLSquare) &&
  2947. Style.SpacesInSquareBrackets && Right.isNot(tok::r_square));
  2948. if (Right.is(tok::r_square))
  2949. return Right.MatchingParen &&
  2950. ((Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
  2951. SpaceRequiredForArrayInitializerLSquare(*Right.MatchingParen,
  2952. Style)) ||
  2953. (Style.SpacesInSquareBrackets &&
  2954. Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
  2955. TT_StructuredBindingLSquare,
  2956. TT_LambdaLSquare)) ||
  2957. Right.MatchingParen->is(TT_AttributeParen));
  2958. if (Right.is(tok::l_square) &&
  2959. !Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
  2960. TT_DesignatedInitializerLSquare,
  2961. TT_StructuredBindingLSquare, TT_AttributeSquare) &&
  2962. !Left.isOneOf(tok::numeric_constant, TT_DictLiteral) &&
  2963. !(!Left.is(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
  2964. Right.is(TT_ArraySubscriptLSquare)))
  2965. return false;
  2966. if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
  2967. return !Left.Children.empty(); // No spaces in "{}".
  2968. if ((Left.is(tok::l_brace) && Left.isNot(BK_Block)) ||
  2969. (Right.is(tok::r_brace) && Right.MatchingParen &&
  2970. Right.MatchingParen->isNot(BK_Block)))
  2971. return Style.Cpp11BracedListStyle ? Style.SpacesInParentheses : true;
  2972. if (Left.is(TT_BlockComment))
  2973. // No whitespace in x(/*foo=*/1), except for JavaScript.
  2974. return Style.isJavaScript() || !Left.TokenText.endswith("=*/");
  2975. // Space between template and attribute.
  2976. // e.g. template <typename T> [[nodiscard]] ...
  2977. if (Left.is(TT_TemplateCloser) && Right.is(TT_AttributeSquare))
  2978. return true;
  2979. // Space before parentheses common for all languages
  2980. if (Right.is(tok::l_paren)) {
  2981. if (Left.is(TT_TemplateCloser) && Right.isNot(TT_FunctionTypeLParen))
  2982. return spaceRequiredBeforeParens(Right);
  2983. if (Left.is(tok::kw_requires))
  2984. return spaceRequiredBeforeParens(Right);
  2985. if ((Left.is(tok::r_paren) && Left.is(TT_AttributeParen)) ||
  2986. (Left.is(tok::r_square) && Left.is(TT_AttributeSquare)))
  2987. return true;
  2988. if (Left.is(TT_ForEachMacro))
  2989. return (Style.SpaceBeforeParensOptions.AfterForeachMacros ||
  2990. spaceRequiredBeforeParens(Right));
  2991. if (Left.is(TT_IfMacro))
  2992. return (Style.SpaceBeforeParensOptions.AfterIfMacros ||
  2993. spaceRequiredBeforeParens(Right));
  2994. if (Line.Type == LT_ObjCDecl)
  2995. return true;
  2996. if (Left.is(tok::semi))
  2997. return true;
  2998. if (Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while, tok::kw_switch,
  2999. tok::kw_case, TT_ForEachMacro, TT_ObjCForIn))
  3000. return Style.SpaceBeforeParensOptions.AfterControlStatements ||
  3001. spaceRequiredBeforeParens(Right);
  3002. if (Left.isIf(Line.Type != LT_PreprocessorDirective))
  3003. return Style.SpaceBeforeParensOptions.AfterControlStatements ||
  3004. spaceRequiredBeforeParens(Right);
  3005. // TODO add Operator overloading specific Options to
  3006. // SpaceBeforeParensOptions
  3007. if (Right.is(TT_OverloadedOperatorLParen))
  3008. return spaceRequiredBeforeParens(Right);
  3009. // Function declaration or definition
  3010. if (Line.MightBeFunctionDecl && (Left.is(TT_FunctionDeclarationName))) {
  3011. if (Line.mightBeFunctionDefinition())
  3012. return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
  3013. spaceRequiredBeforeParens(Right);
  3014. else
  3015. return Style.SpaceBeforeParensOptions.AfterFunctionDeclarationName ||
  3016. spaceRequiredBeforeParens(Right);
  3017. }
  3018. // Lambda
  3019. if (Line.Type != LT_PreprocessorDirective && Left.is(tok::r_square) &&
  3020. Left.MatchingParen && Left.MatchingParen->is(TT_LambdaLSquare))
  3021. return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
  3022. spaceRequiredBeforeParens(Right);
  3023. if (!Left.Previous || Left.Previous->isNot(tok::period)) {
  3024. if (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch))
  3025. return Style.SpaceBeforeParensOptions.AfterControlStatements ||
  3026. spaceRequiredBeforeParens(Right);
  3027. if (Left.isOneOf(tok::kw_new, tok::kw_delete))
  3028. return Style.SpaceBeforeParens != FormatStyle::SBPO_Never ||
  3029. spaceRequiredBeforeParens(Right);
  3030. }
  3031. if (Line.Type != LT_PreprocessorDirective &&
  3032. (Left.is(tok::identifier) || Left.isFunctionLikeKeyword() ||
  3033. Left.is(tok::r_paren) || Left.isSimpleTypeSpecifier()))
  3034. return spaceRequiredBeforeParens(Right);
  3035. return false;
  3036. }
  3037. if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
  3038. return false;
  3039. if (Right.is(TT_UnaryOperator))
  3040. return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
  3041. (Left.isNot(tok::colon) || Left.isNot(TT_ObjCMethodExpr));
  3042. if ((Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
  3043. tok::r_paren) ||
  3044. Left.isSimpleTypeSpecifier()) &&
  3045. Right.is(tok::l_brace) && Right.getNextNonComment() &&
  3046. Right.isNot(BK_Block))
  3047. return false;
  3048. if (Left.is(tok::period) || Right.is(tok::period))
  3049. return false;
  3050. // u#str, U#str, L#str, u8#str
  3051. // uR#str, UR#str, LR#str, u8R#str
  3052. if (Right.is(tok::hash) && Left.is(tok::identifier) &&
  3053. (Left.TokenText == "L" || Left.TokenText == "u" ||
  3054. Left.TokenText == "U" || Left.TokenText == "u8" ||
  3055. Left.TokenText == "LR" || Left.TokenText == "uR" ||
  3056. Left.TokenText == "UR" || Left.TokenText == "u8R"))
  3057. return false;
  3058. if (Left.is(TT_TemplateCloser) && Left.MatchingParen &&
  3059. Left.MatchingParen->Previous &&
  3060. (Left.MatchingParen->Previous->is(tok::period) ||
  3061. Left.MatchingParen->Previous->is(tok::coloncolon)))
  3062. // Java call to generic function with explicit type:
  3063. // A.<B<C<...>>>DoSomething();
  3064. // A::<B<C<...>>>DoSomething(); // With a Java 8 method reference.
  3065. return false;
  3066. if (Left.is(TT_TemplateCloser) && Right.is(tok::l_square))
  3067. return false;
  3068. if (Left.is(tok::l_brace) && Left.endsSequence(TT_DictLiteral, tok::at))
  3069. // Objective-C dictionary literal -> no space after opening brace.
  3070. return false;
  3071. if (Right.is(tok::r_brace) && Right.MatchingParen &&
  3072. Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at))
  3073. // Objective-C dictionary literal -> no space before closing brace.
  3074. return false;
  3075. if (Right.getType() == TT_TrailingAnnotation &&
  3076. Right.isOneOf(tok::amp, tok::ampamp) &&
  3077. Left.isOneOf(tok::kw_const, tok::kw_volatile) &&
  3078. (!Right.Next || Right.Next->is(tok::semi)))
  3079. // Match const and volatile ref-qualifiers without any additional
  3080. // qualifiers such as
  3081. // void Fn() const &;
  3082. return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
  3083. return true;
  3084. }
  3085. bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
  3086. const FormatToken &Right) {
  3087. const FormatToken &Left = *Right.Previous;
  3088. if (Right.is(tok::colon)) {
  3089. auto n = Right.Next;
  3090. if (n && n->isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected)) {
  3091. return false;
  3092. }
  3093. auto p = Left.Previous;
  3094. if (p && p->isOneOf(tok::kw_class, tok::kw_struct, tok::kw_enum)) {
  3095. return false;
  3096. }
  3097. }
  3098. // If the token is finalized don't touch it (as it could be in a
  3099. // clang-format-off section).
  3100. if (Left.Finalized)
  3101. return Right.hasWhitespaceBefore();
  3102. if (Right.Tok.getIdentifierInfo() && Left.Tok.getIdentifierInfo())
  3103. return true; // Never ever merge two identifiers.
  3104. // Leave a space between * and /* to avoid C4138 `comment end` found outside
  3105. // of comment.
  3106. if (Left.is(tok::star) && Right.is(tok::comment))
  3107. return true;
  3108. if (Style.isCpp()) {
  3109. // Space between import <iostream>.
  3110. // or import .....;
  3111. if (Left.is(Keywords.kw_import) && Right.isOneOf(tok::less, tok::ellipsis))
  3112. return true;
  3113. // Space between `module :` and `import :`.
  3114. if (Left.isOneOf(Keywords.kw_module, Keywords.kw_import) &&
  3115. Right.is(TT_ModulePartitionColon))
  3116. return true;
  3117. // No space between import foo:bar but keep a space between import :bar;
  3118. if (Left.is(tok::identifier) && Right.is(TT_ModulePartitionColon))
  3119. return false;
  3120. // No space between :bar;
  3121. if (Left.is(TT_ModulePartitionColon) &&
  3122. Right.isOneOf(tok::identifier, tok::kw_private))
  3123. return false;
  3124. if (Left.is(tok::ellipsis) && Right.is(tok::identifier) &&
  3125. Line.First->is(Keywords.kw_import))
  3126. return false;
  3127. // Space in __attribute__((attr)) ::type.
  3128. if (Left.is(TT_AttributeParen) && Right.is(tok::coloncolon))
  3129. return true;
  3130. if (Left.is(tok::kw_operator))
  3131. return Right.is(tok::coloncolon);
  3132. if (Right.is(tok::l_brace) && Right.is(BK_BracedInit) &&
  3133. !Left.opensScope() && Style.SpaceBeforeCpp11BracedList)
  3134. return true;
  3135. if (Left.is(tok::less) && Left.is(TT_OverloadedOperator) &&
  3136. Right.is(TT_TemplateOpener))
  3137. return true;
  3138. } else if (Style.Language == FormatStyle::LK_Proto ||
  3139. Style.Language == FormatStyle::LK_TextProto) {
  3140. if (Right.is(tok::period) &&
  3141. Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
  3142. Keywords.kw_repeated, Keywords.kw_extend))
  3143. return true;
  3144. if (Right.is(tok::l_paren) &&
  3145. Left.isOneOf(Keywords.kw_returns, Keywords.kw_option))
  3146. return true;
  3147. if (Right.isOneOf(tok::l_brace, tok::less) && Left.is(TT_SelectorName))
  3148. return true;
  3149. // Slashes occur in text protocol extension syntax: [type/type] { ... }.
  3150. if (Left.is(tok::slash) || Right.is(tok::slash))
  3151. return false;
  3152. if (Left.MatchingParen &&
  3153. Left.MatchingParen->is(TT_ProtoExtensionLSquare) &&
  3154. Right.isOneOf(tok::l_brace, tok::less))
  3155. return !Style.Cpp11BracedListStyle;
  3156. // A percent is probably part of a formatting specification, such as %lld.
  3157. if (Left.is(tok::percent))
  3158. return false;
  3159. // Preserve the existence of a space before a percent for cases like 0x%04x
  3160. // and "%d %d"
  3161. if (Left.is(tok::numeric_constant) && Right.is(tok::percent))
  3162. return Right.hasWhitespaceBefore();
  3163. } else if (Style.isJson()) {
  3164. if (Right.is(tok::colon))
  3165. return false;
  3166. } else if (Style.isCSharp()) {
  3167. // Require spaces around '{' and before '}' unless they appear in
  3168. // interpolated strings. Interpolated strings are merged into a single token
  3169. // so cannot have spaces inserted by this function.
  3170. // No space between 'this' and '['
  3171. if (Left.is(tok::kw_this) && Right.is(tok::l_square))
  3172. return false;
  3173. // No space between 'new' and '('
  3174. if (Left.is(tok::kw_new) && Right.is(tok::l_paren))
  3175. return false;
  3176. // Space before { (including space within '{ {').
  3177. if (Right.is(tok::l_brace))
  3178. return true;
  3179. // Spaces inside braces.
  3180. if (Left.is(tok::l_brace) && Right.isNot(tok::r_brace))
  3181. return true;
  3182. if (Left.isNot(tok::l_brace) && Right.is(tok::r_brace))
  3183. return true;
  3184. // Spaces around '=>'.
  3185. if (Left.is(TT_FatArrow) || Right.is(TT_FatArrow))
  3186. return true;
  3187. // No spaces around attribute target colons
  3188. if (Left.is(TT_AttributeColon) || Right.is(TT_AttributeColon))
  3189. return false;
  3190. // space between type and variable e.g. Dictionary<string,string> foo;
  3191. if (Left.is(TT_TemplateCloser) && Right.is(TT_StartOfName))
  3192. return true;
  3193. // spaces inside square brackets.
  3194. if (Left.is(tok::l_square) || Right.is(tok::r_square))
  3195. return Style.SpacesInSquareBrackets;
  3196. // No space before ? in nullable types.
  3197. if (Right.is(TT_CSharpNullable))
  3198. return false;
  3199. // No space before null forgiving '!'.
  3200. if (Right.is(TT_NonNullAssertion))
  3201. return false;
  3202. // No space between consecutive commas '[,,]'.
  3203. if (Left.is(tok::comma) && Right.is(tok::comma))
  3204. return false;
  3205. // space after var in `var (key, value)`
  3206. if (Left.is(Keywords.kw_var) && Right.is(tok::l_paren))
  3207. return true;
  3208. // space between keywords and paren e.g. "using ("
  3209. if (Right.is(tok::l_paren))
  3210. if (Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when,
  3211. Keywords.kw_lock))
  3212. return Style.SpaceBeforeParensOptions.AfterControlStatements ||
  3213. spaceRequiredBeforeParens(Right);
  3214. // space between method modifier and opening parenthesis of a tuple return
  3215. // type
  3216. if (Left.isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected,
  3217. tok::kw_virtual, tok::kw_extern, tok::kw_static,
  3218. Keywords.kw_internal, Keywords.kw_abstract,
  3219. Keywords.kw_sealed, Keywords.kw_override,
  3220. Keywords.kw_async, Keywords.kw_unsafe) &&
  3221. Right.is(tok::l_paren))
  3222. return true;
  3223. } else if (Style.isJavaScript()) {
  3224. if (Left.is(TT_FatArrow))
  3225. return true;
  3226. // for await ( ...
  3227. if (Right.is(tok::l_paren) && Left.is(Keywords.kw_await) && Left.Previous &&
  3228. Left.Previous->is(tok::kw_for))
  3229. return true;
  3230. if (Left.is(Keywords.kw_async) && Right.is(tok::l_paren) &&
  3231. Right.MatchingParen) {
  3232. const FormatToken *Next = Right.MatchingParen->getNextNonComment();
  3233. // An async arrow function, for example: `x = async () => foo();`,
  3234. // as opposed to calling a function called async: `x = async();`
  3235. if (Next && Next->is(TT_FatArrow))
  3236. return true;
  3237. }
  3238. if ((Left.is(TT_TemplateString) && Left.TokenText.endswith("${")) ||
  3239. (Right.is(TT_TemplateString) && Right.TokenText.startswith("}")))
  3240. return false;
  3241. // In tagged template literals ("html`bar baz`"), there is no space between
  3242. // the tag identifier and the template string.
  3243. if (Keywords.IsJavaScriptIdentifier(Left,
  3244. /* AcceptIdentifierName= */ false) &&
  3245. Right.is(TT_TemplateString))
  3246. return false;
  3247. if (Right.is(tok::star) &&
  3248. Left.isOneOf(Keywords.kw_function, Keywords.kw_yield))
  3249. return false;
  3250. if (Right.isOneOf(tok::l_brace, tok::l_square) &&
  3251. Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
  3252. Keywords.kw_extends, Keywords.kw_implements))
  3253. return true;
  3254. if (Right.is(tok::l_paren)) {
  3255. // JS methods can use some keywords as names (e.g. `delete()`).
  3256. if (Line.MustBeDeclaration && Left.Tok.getIdentifierInfo())
  3257. return false;
  3258. // Valid JS method names can include keywords, e.g. `foo.delete()` or
  3259. // `bar.instanceof()`. Recognize call positions by preceding period.
  3260. if (Left.Previous && Left.Previous->is(tok::period) &&
  3261. Left.Tok.getIdentifierInfo())
  3262. return false;
  3263. // Additional unary JavaScript operators that need a space after.
  3264. if (Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
  3265. tok::kw_void))
  3266. return true;
  3267. }
  3268. // `foo as const;` casts into a const type.
  3269. if (Left.endsSequence(tok::kw_const, Keywords.kw_as)) {
  3270. return false;
  3271. }
  3272. if ((Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
  3273. tok::kw_const) ||
  3274. // "of" is only a keyword if it appears after another identifier
  3275. // (e.g. as "const x of y" in a for loop), or after a destructuring
  3276. // operation (const [x, y] of z, const {a, b} of c).
  3277. (Left.is(Keywords.kw_of) && Left.Previous &&
  3278. (Left.Previous->Tok.is(tok::identifier) ||
  3279. Left.Previous->isOneOf(tok::r_square, tok::r_brace)))) &&
  3280. (!Left.Previous || !Left.Previous->is(tok::period)))
  3281. return true;
  3282. if (Left.isOneOf(tok::kw_for, Keywords.kw_as) && Left.Previous &&
  3283. Left.Previous->is(tok::period) && Right.is(tok::l_paren))
  3284. return false;
  3285. if (Left.is(Keywords.kw_as) &&
  3286. Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren))
  3287. return true;
  3288. if (Left.is(tok::kw_default) && Left.Previous &&
  3289. Left.Previous->is(tok::kw_export))
  3290. return true;
  3291. if (Left.is(Keywords.kw_is) && Right.is(tok::l_brace))
  3292. return true;
  3293. if (Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
  3294. return false;
  3295. if (Left.is(TT_JsTypeOperator) || Right.is(TT_JsTypeOperator))
  3296. return false;
  3297. if ((Left.is(tok::l_brace) || Right.is(tok::r_brace)) &&
  3298. Line.First->isOneOf(Keywords.kw_import, tok::kw_export))
  3299. return false;
  3300. if (Left.is(tok::ellipsis))
  3301. return false;
  3302. if (Left.is(TT_TemplateCloser) &&
  3303. !Right.isOneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
  3304. Keywords.kw_implements, Keywords.kw_extends))
  3305. // Type assertions ('<type>expr') are not followed by whitespace. Other
  3306. // locations that should have whitespace following are identified by the
  3307. // above set of follower tokens.
  3308. return false;
  3309. if (Right.is(TT_NonNullAssertion))
  3310. return false;
  3311. if (Left.is(TT_NonNullAssertion) &&
  3312. Right.isOneOf(Keywords.kw_as, Keywords.kw_in))
  3313. return true; // "x! as string", "x! in y"
  3314. } else if (Style.Language == FormatStyle::LK_Java) {
  3315. if (Left.is(tok::r_square) && Right.is(tok::l_brace))
  3316. return true;
  3317. if (Left.is(Keywords.kw_synchronized) && Right.is(tok::l_paren))
  3318. return Style.SpaceBeforeParensOptions.AfterControlStatements ||
  3319. spaceRequiredBeforeParens(Right);
  3320. if ((Left.isOneOf(tok::kw_static, tok::kw_public, tok::kw_private,
  3321. tok::kw_protected) ||
  3322. Left.isOneOf(Keywords.kw_final, Keywords.kw_abstract,
  3323. Keywords.kw_native)) &&
  3324. Right.is(TT_TemplateOpener))
  3325. return true;
  3326. }
  3327. if (Left.is(TT_ImplicitStringLiteral))
  3328. return Right.hasWhitespaceBefore();
  3329. if (Line.Type == LT_ObjCMethodDecl) {
  3330. if (Left.is(TT_ObjCMethodSpecifier))
  3331. return true;
  3332. if (Left.is(tok::r_paren) && canBeObjCSelectorComponent(Right))
  3333. // Don't space between ')' and <id> or ')' and 'new'. 'new' is not a
  3334. // keyword in Objective-C, and '+ (instancetype)new;' is a standard class
  3335. // method declaration.
  3336. return false;
  3337. }
  3338. if (Line.Type == LT_ObjCProperty &&
  3339. (Right.is(tok::equal) || Left.is(tok::equal)))
  3340. return false;
  3341. if (Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) ||
  3342. Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow))
  3343. return true;
  3344. if (Left.is(tok::comma) && !Right.is(TT_OverloadedOperatorLParen))
  3345. return true;
  3346. if (Right.is(tok::comma))
  3347. return false;
  3348. if (Right.is(TT_ObjCBlockLParen))
  3349. return true;
  3350. if (Right.is(TT_CtorInitializerColon))
  3351. return Style.SpaceBeforeCtorInitializerColon;
  3352. if (Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon)
  3353. return false;
  3354. if (Right.is(TT_RangeBasedForLoopColon) &&
  3355. !Style.SpaceBeforeRangeBasedForLoopColon)
  3356. return false;
  3357. if (Left.is(TT_BitFieldColon))
  3358. return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
  3359. Style.BitFieldColonSpacing == FormatStyle::BFCS_After;
  3360. if (Right.is(tok::colon)) {
  3361. if (Line.First->isOneOf(tok::kw_default, tok::kw_case))
  3362. return Style.SpaceBeforeCaseColon;
  3363. if (!Right.getNextNonComment() || Right.getNextNonComment()->is(tok::semi))
  3364. return false;
  3365. if (Right.is(TT_ObjCMethodExpr))
  3366. return false;
  3367. if (Left.is(tok::question))
  3368. return false;
  3369. if (Right.is(TT_InlineASMColon) && Left.is(tok::coloncolon))
  3370. return false;
  3371. if (Right.is(TT_DictLiteral))
  3372. return Style.SpacesInContainerLiterals;
  3373. if (Right.is(TT_AttributeColon))
  3374. return false;
  3375. if (Right.is(TT_CSharpNamedArgumentColon))
  3376. return false;
  3377. if (Right.is(TT_BitFieldColon))
  3378. return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
  3379. Style.BitFieldColonSpacing == FormatStyle::BFCS_Before;
  3380. return true;
  3381. }
  3382. // Do not merge "- -" into "--".
  3383. if ((Left.isOneOf(tok::minus, tok::minusminus) &&
  3384. Right.isOneOf(tok::minus, tok::minusminus)) ||
  3385. (Left.isOneOf(tok::plus, tok::plusplus) &&
  3386. Right.isOneOf(tok::plus, tok::plusplus)))
  3387. return true;
  3388. if (Left.is(TT_UnaryOperator)) {
  3389. if (!Right.is(tok::l_paren)) {
  3390. // The alternative operators for ~ and ! are "compl" and "not".
  3391. // If they are used instead, we do not want to combine them with
  3392. // the token to the right, unless that is a left paren.
  3393. if (Left.is(tok::exclaim) && Left.TokenText == "not")
  3394. return true;
  3395. if (Left.is(tok::tilde) && Left.TokenText == "compl")
  3396. return true;
  3397. // Lambda captures allow for a lone &, so "&]" needs to be properly
  3398. // handled.
  3399. if (Left.is(tok::amp) && Right.is(tok::r_square))
  3400. return Style.SpacesInSquareBrackets;
  3401. }
  3402. return (Style.SpaceAfterLogicalNot && Left.is(tok::exclaim)) ||
  3403. Right.is(TT_BinaryOperator);
  3404. }
  3405. // If the next token is a binary operator or a selector name, we have
  3406. // incorrectly classified the parenthesis as a cast. FIXME: Detect correctly.
  3407. if (Left.is(TT_CastRParen))
  3408. return Style.SpaceAfterCStyleCast ||
  3409. Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
  3410. auto ShouldAddSpacesInAngles = [this, &Right]() {
  3411. if (this->Style.SpacesInAngles == FormatStyle::SIAS_Always)
  3412. return true;
  3413. if (this->Style.SpacesInAngles == FormatStyle::SIAS_Leave)
  3414. return Right.hasWhitespaceBefore();
  3415. return false;
  3416. };
  3417. if (Left.is(tok::greater) && Right.is(tok::greater)) {
  3418. if (Style.Language == FormatStyle::LK_TextProto ||
  3419. (Style.Language == FormatStyle::LK_Proto && Left.is(TT_DictLiteral)))
  3420. return !Style.Cpp11BracedListStyle;
  3421. return Right.is(TT_TemplateCloser) && Left.is(TT_TemplateCloser) &&
  3422. ((Style.Standard < FormatStyle::LS_Cpp11) ||
  3423. ShouldAddSpacesInAngles());
  3424. }
  3425. if (Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) ||
  3426. Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
  3427. (Right.is(tok::period) && Right.isNot(TT_DesignatedInitializerPeriod)))
  3428. return false;
  3429. if (!Style.SpaceBeforeAssignmentOperators && Left.isNot(TT_TemplateCloser) &&
  3430. Right.getPrecedence() == prec::Assignment)
  3431. return false;
  3432. if (Style.Language == FormatStyle::LK_Java && Right.is(tok::coloncolon) &&
  3433. (Left.is(tok::identifier) || Left.is(tok::kw_this)))
  3434. return false;
  3435. if (Right.is(tok::coloncolon) && Left.is(tok::identifier))
  3436. // Generally don't remove existing spaces between an identifier and "::".
  3437. // The identifier might actually be a macro name such as ALWAYS_INLINE. If
  3438. // this turns out to be too lenient, add analysis of the identifier itself.
  3439. return Right.hasWhitespaceBefore();
  3440. if (Right.is(tok::coloncolon) &&
  3441. !Left.isOneOf(tok::l_brace, tok::comment, tok::l_paren))
  3442. // Put a space between < and :: in vector< ::std::string >
  3443. return (Left.is(TT_TemplateOpener) &&
  3444. ((Style.Standard < FormatStyle::LS_Cpp11) ||
  3445. ShouldAddSpacesInAngles())) ||
  3446. !(Left.isOneOf(tok::l_paren, tok::r_paren, tok::l_square,
  3447. tok::kw___super, TT_TemplateOpener,
  3448. TT_TemplateCloser)) ||
  3449. (Left.is(tok::l_paren) && Style.SpacesInParentheses);
  3450. if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser)))
  3451. return ShouldAddSpacesInAngles();
  3452. // Space before TT_StructuredBindingLSquare.
  3453. if (Right.is(TT_StructuredBindingLSquare))
  3454. return !Left.isOneOf(tok::amp, tok::ampamp) ||
  3455. getTokenReferenceAlignment(Left) != FormatStyle::PAS_Right;
  3456. // Space before & or && following a TT_StructuredBindingLSquare.
  3457. if (Right.Next && Right.Next->is(TT_StructuredBindingLSquare) &&
  3458. Right.isOneOf(tok::amp, tok::ampamp))
  3459. return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
  3460. if ((Right.is(TT_BinaryOperator) && !Left.is(tok::l_paren)) ||
  3461. (Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) &&
  3462. !Right.is(tok::r_paren)))
  3463. return true;
  3464. if (Right.is(TT_TemplateOpener) && Left.is(tok::r_paren) &&
  3465. Left.MatchingParen && Left.MatchingParen->is(TT_OverloadedOperatorLParen))
  3466. return false;
  3467. if (Right.is(tok::less) && Left.isNot(tok::l_paren) &&
  3468. Line.startsWith(tok::hash))
  3469. return true;
  3470. if (Right.is(TT_TrailingUnaryOperator))
  3471. return false;
  3472. if (Left.is(TT_RegexLiteral))
  3473. return false;
  3474. return spaceRequiredBetween(Line, Left, Right);
  3475. }
  3476. // Returns 'true' if 'Tok' is a brace we'd want to break before in Allman style.
  3477. static bool isAllmanBrace(const FormatToken &Tok) {
  3478. return Tok.is(tok::l_brace) && Tok.is(BK_Block) &&
  3479. !Tok.isOneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral);
  3480. }
  3481. // Returns 'true' if 'Tok' is a function argument.
  3482. static bool IsFunctionArgument(const FormatToken &Tok) {
  3483. return Tok.MatchingParen && Tok.MatchingParen->Next &&
  3484. Tok.MatchingParen->Next->isOneOf(tok::comma, tok::r_paren);
  3485. }
  3486. static bool
  3487. isItAnEmptyLambdaAllowed(const FormatToken &Tok,
  3488. FormatStyle::ShortLambdaStyle ShortLambdaOption) {
  3489. return Tok.Children.empty() && ShortLambdaOption != FormatStyle::SLS_None;
  3490. }
  3491. static bool isAllmanLambdaBrace(const FormatToken &Tok) {
  3492. return (Tok.is(tok::l_brace) && Tok.is(BK_Block) &&
  3493. !Tok.isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral));
  3494. }
  3495. // Returns the first token on the line that is not a comment.
  3496. static const FormatToken *getFirstNonComment(const AnnotatedLine &Line) {
  3497. const FormatToken *Next = Line.First;
  3498. if (!Next)
  3499. return Next;
  3500. if (Next->is(tok::comment))
  3501. Next = Next->getNextNonComment();
  3502. return Next;
  3503. }
  3504. template <class T>
  3505. static const T* CalcPrev(const T* v, size_t n) noexcept {
  3506. while (v && n) {
  3507. v = v->Previous;
  3508. n -= 1;
  3509. }
  3510. return v;
  3511. }
  3512. bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
  3513. const FormatToken &Right) {
  3514. const FormatToken &Left = *Right.Previous;
  3515. //llvm::errs() << Left.TokenText << " " << Right.TokenText << " " << Right.LastNewlineOffset << "\n";
  3516. if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0)
  3517. return true;
  3518. if (Left.TokenText == "{" && Right.is(tok::identifier)) {
  3519. if (auto m = CalcPrev(&Left, 2); m && m->TokenText == "enum") {
  3520. return true;
  3521. }
  3522. if (auto m = CalcPrev(&Left, 4); m && m->TokenText == "enum") {
  3523. return true;
  3524. }
  3525. }
  3526. if (Left.TokenText == "}" && Right.TokenText == "{") {
  3527. return true;
  3528. }
  3529. if (Left.TokenText == ")" && Right.TokenText == "{") {
  3530. if (Right.LastNewlineOffset == 1) {
  3531. return true;
  3532. }
  3533. auto m = CalcPrev(Left.MatchingParen, 2);
  3534. if (m && m->isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) {
  3535. return true;
  3536. }
  3537. }
  3538. if (Style.isCSharp()) {
  3539. if (Left.is(TT_FatArrow) && Right.is(tok::l_brace) &&
  3540. Style.BraceWrapping.AfterFunction)
  3541. return true;
  3542. if (Right.is(TT_CSharpNamedArgumentColon) ||
  3543. Left.is(TT_CSharpNamedArgumentColon))
  3544. return false;
  3545. if (Right.is(TT_CSharpGenericTypeConstraint))
  3546. return true;
  3547. if (Right.Next && Right.Next->is(TT_FatArrow) &&
  3548. (Right.is(tok::numeric_constant) ||
  3549. (Right.is(tok::identifier) && Right.TokenText == "_")))
  3550. return true;
  3551. // Break after C# [...] and before public/protected/private/internal.
  3552. if (Left.is(TT_AttributeSquare) && Left.is(tok::r_square) &&
  3553. (Right.isAccessSpecifier(/*ColonRequired=*/false) ||
  3554. Right.is(Keywords.kw_internal)))
  3555. return true;
  3556. // Break between ] and [ but only when there are really 2 attributes.
  3557. if (Left.is(TT_AttributeSquare) && Right.is(TT_AttributeSquare) &&
  3558. Left.is(tok::r_square) && Right.is(tok::l_square))
  3559. return true;
  3560. } else if (Style.isJavaScript()) {
  3561. // FIXME: This might apply to other languages and token kinds.
  3562. if (Right.is(tok::string_literal) && Left.is(tok::plus) && Left.Previous &&
  3563. Left.Previous->is(tok::string_literal))
  3564. return true;
  3565. if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) && Line.Level == 0 &&
  3566. Left.Previous && Left.Previous->is(tok::equal) &&
  3567. Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
  3568. tok::kw_const) &&
  3569. // kw_var/kw_let are pseudo-tokens that are tok::identifier, so match
  3570. // above.
  3571. !Line.First->isOneOf(Keywords.kw_var, Keywords.kw_let))
  3572. // Object literals on the top level of a file are treated as "enum-style".
  3573. // Each key/value pair is put on a separate line, instead of bin-packing.
  3574. return true;
  3575. if (Left.is(tok::l_brace) && Line.Level == 0 &&
  3576. (Line.startsWith(tok::kw_enum) ||
  3577. Line.startsWith(tok::kw_const, tok::kw_enum) ||
  3578. Line.startsWith(tok::kw_export, tok::kw_enum) ||
  3579. Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum)))
  3580. // JavaScript top-level enum key/value pairs are put on separate lines
  3581. // instead of bin-packing.
  3582. return true;
  3583. if (Right.is(tok::r_brace) && Left.is(tok::l_brace) && Left.Previous &&
  3584. Left.Previous->is(TT_FatArrow)) {
  3585. // JS arrow function (=> {...}).
  3586. switch (Style.AllowShortLambdasOnASingleLine) {
  3587. case FormatStyle::SLS_All:
  3588. return false;
  3589. case FormatStyle::SLS_None:
  3590. return true;
  3591. case FormatStyle::SLS_Empty:
  3592. return !Left.Children.empty();
  3593. case FormatStyle::SLS_Inline:
  3594. // allow one-lining inline (e.g. in function call args) and empty arrow
  3595. // functions.
  3596. return (Left.NestingLevel == 0 && Line.Level == 0) &&
  3597. !Left.Children.empty();
  3598. }
  3599. llvm_unreachable("Unknown FormatStyle::ShortLambdaStyle enum");
  3600. }
  3601. if (Right.is(tok::r_brace) && Left.is(tok::l_brace) &&
  3602. !Left.Children.empty())
  3603. // Support AllowShortFunctionsOnASingleLine for JavaScript.
  3604. return Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None ||
  3605. Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Empty ||
  3606. (Left.NestingLevel == 0 && Line.Level == 0 &&
  3607. Style.AllowShortFunctionsOnASingleLine &
  3608. FormatStyle::SFS_InlineOnly);
  3609. } else if (Style.Language == FormatStyle::LK_Java) {
  3610. if (Right.is(tok::plus) && Left.is(tok::string_literal) && Right.Next &&
  3611. Right.Next->is(tok::string_literal))
  3612. return true;
  3613. } else if (Style.Language == FormatStyle::LK_Cpp ||
  3614. Style.Language == FormatStyle::LK_ObjC ||
  3615. Style.Language == FormatStyle::LK_Proto ||
  3616. Style.Language == FormatStyle::LK_TableGen ||
  3617. Style.Language == FormatStyle::LK_TextProto) {
  3618. if (Left.isStringLiteral() && Right.isStringLiteral())
  3619. return true;
  3620. }
  3621. // Basic JSON newline processing.
  3622. if (Style.isJson()) {
  3623. // Always break after a JSON record opener.
  3624. // {
  3625. // }
  3626. if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace))
  3627. return true;
  3628. // Always break after a JSON array opener.
  3629. // [
  3630. // ]
  3631. if (Left.is(TT_ArrayInitializerLSquare) && Left.is(tok::l_square) &&
  3632. !Right.is(tok::r_square))
  3633. return true;
  3634. // Always break after successive entries.
  3635. // 1,
  3636. // 2
  3637. if (Left.is(tok::comma))
  3638. return true;
  3639. }
  3640. // If the last token before a '}', ']', or ')' is a comma or a trailing
  3641. // comment, the intention is to insert a line break after it in order to make
  3642. // shuffling around entries easier. Import statements, especially in
  3643. // JavaScript, can be an exception to this rule.
  3644. if (Style.JavaScriptWrapImports || Line.Type != LT_ImportStatement) {
  3645. const FormatToken *BeforeClosingBrace = nullptr;
  3646. if ((Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
  3647. (Style.isJavaScript() && Left.is(tok::l_paren))) &&
  3648. Left.isNot(BK_Block) && Left.MatchingParen)
  3649. BeforeClosingBrace = Left.MatchingParen->Previous;
  3650. else if (Right.MatchingParen &&
  3651. (Right.MatchingParen->isOneOf(tok::l_brace,
  3652. TT_ArrayInitializerLSquare) ||
  3653. (Style.isJavaScript() && Right.MatchingParen->is(tok::l_paren))))
  3654. BeforeClosingBrace = &Left;
  3655. if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
  3656. BeforeClosingBrace->isTrailingComment()))
  3657. return true;
  3658. }
  3659. if (Right.is(tok::comment))
  3660. return Left.isNot(BK_BracedInit) && Left.isNot(TT_CtorInitializerColon) &&
  3661. (Right.NewlinesBefore > 0 && Right.HasUnescapedNewline);
  3662. if (Left.isTrailingComment())
  3663. return true;
  3664. if (Left.IsUnterminatedLiteral)
  3665. return true;
  3666. if (Right.is(tok::lessless) && Right.Next && Left.is(tok::string_literal) &&
  3667. Right.Next->is(tok::string_literal))
  3668. return true;
  3669. // Can break after template<> declaration
  3670. if (Left.ClosesTemplateDeclaration && Left.MatchingParen &&
  3671. Left.MatchingParen->NestingLevel == 0) {
  3672. // Put concepts on the next line e.g.
  3673. // template<typename T>
  3674. // concept ...
  3675. if (Right.is(tok::kw_concept))
  3676. return Style.BreakBeforeConceptDeclarations;
  3677. return (Style.AlwaysBreakTemplateDeclarations == FormatStyle::BTDS_Yes);
  3678. }
  3679. if (Style.PackConstructorInitializers == FormatStyle::PCIS_Never) {
  3680. if (Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon &&
  3681. (Left.is(TT_CtorInitializerComma) || Right.is(TT_CtorInitializerColon)))
  3682. return true;
  3683. if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
  3684. Left.isOneOf(TT_CtorInitializerColon, TT_CtorInitializerComma))
  3685. return true;
  3686. }
  3687. if (Style.PackConstructorInitializers < FormatStyle::PCIS_CurrentLine &&
  3688. Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma &&
  3689. Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon))
  3690. return true;
  3691. // Break only if we have multiple inheritance.
  3692. if (Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma &&
  3693. Right.is(TT_InheritanceComma))
  3694. return true;
  3695. if (Style.BreakInheritanceList == FormatStyle::BILS_AfterComma &&
  3696. Left.is(TT_InheritanceComma))
  3697. return true;
  3698. if (Right.is(tok::string_literal) && Right.TokenText.startswith("R\""))
  3699. // Multiline raw string literals are special wrt. line breaks. The author
  3700. // has made a deliberate choice and might have aligned the contents of the
  3701. // string literal accordingly. Thus, we try keep existing line breaks.
  3702. return Right.IsMultiline && Right.NewlinesBefore > 0;
  3703. if ((Left.is(tok::l_brace) || (Left.is(tok::less) && Left.Previous &&
  3704. Left.Previous->is(tok::equal))) &&
  3705. Right.NestingLevel == 1 && Style.Language == FormatStyle::LK_Proto) {
  3706. // Don't put enums or option definitions onto single lines in protocol
  3707. // buffers.
  3708. return true;
  3709. }
  3710. if (Right.is(TT_InlineASMBrace))
  3711. return Right.HasUnescapedNewline;
  3712. if (isAllmanBrace(Left) || isAllmanBrace(Right)) {
  3713. auto FirstNonComment = getFirstNonComment(Line);
  3714. bool AccessSpecifier =
  3715. FirstNonComment &&
  3716. FirstNonComment->isOneOf(Keywords.kw_internal, tok::kw_public,
  3717. tok::kw_private, tok::kw_protected);
  3718. if (Style.BraceWrapping.AfterEnum) {
  3719. if (Line.startsWith(tok::kw_enum) ||
  3720. Line.startsWith(tok::kw_typedef, tok::kw_enum))
  3721. return true;
  3722. // Ensure BraceWrapping for `public enum A {`.
  3723. if (AccessSpecifier && FirstNonComment->Next &&
  3724. FirstNonComment->Next->is(tok::kw_enum))
  3725. return true;
  3726. }
  3727. // Ensure BraceWrapping for `public interface A {`.
  3728. if (Style.BraceWrapping.AfterClass &&
  3729. ((AccessSpecifier && FirstNonComment->Next &&
  3730. FirstNonComment->Next->is(Keywords.kw_interface)) ||
  3731. Line.startsWith(Keywords.kw_interface)))
  3732. return true;
  3733. return (Line.startsWith(tok::kw_class) && Style.BraceWrapping.AfterClass) ||
  3734. (Line.startsWith(tok::kw_struct) && Style.BraceWrapping.AfterStruct);
  3735. }
  3736. if (Left.is(TT_ObjCBlockLBrace) &&
  3737. Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never)
  3738. return true;
  3739. // Ensure wrapping after __attribute__((XX)) and @interface etc.
  3740. if (Left.is(TT_AttributeParen) && Right.is(TT_ObjCDecl))
  3741. return true;
  3742. if (Left.is(TT_LambdaLBrace)) {
  3743. if (IsFunctionArgument(Left) &&
  3744. Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline)
  3745. return false;
  3746. if (Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_None ||
  3747. Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline ||
  3748. (!Left.Children.empty() &&
  3749. Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Empty))
  3750. return true;
  3751. }
  3752. if (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace) &&
  3753. Left.isOneOf(tok::star, tok::amp, tok::ampamp, TT_TemplateCloser)) {
  3754. return true;
  3755. }
  3756. // Put multiple Java annotation on a new line.
  3757. if ((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) &&
  3758. Left.is(TT_LeadingJavaAnnotation) &&
  3759. Right.isNot(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) &&
  3760. (Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations))
  3761. return true;
  3762. if (Right.is(TT_ProtoExtensionLSquare))
  3763. return true;
  3764. // In text proto instances if a submessage contains at least 2 entries and at
  3765. // least one of them is a submessage, like A { ... B { ... } ... },
  3766. // put all of the entries of A on separate lines by forcing the selector of
  3767. // the submessage B to be put on a newline.
  3768. //
  3769. // Example: these can stay on one line:
  3770. // a { scalar_1: 1 scalar_2: 2 }
  3771. // a { b { key: value } }
  3772. //
  3773. // and these entries need to be on a new line even if putting them all in one
  3774. // line is under the column limit:
  3775. // a {
  3776. // scalar: 1
  3777. // b { key: value }
  3778. // }
  3779. //
  3780. // We enforce this by breaking before a submessage field that has previous
  3781. // siblings, *and* breaking before a field that follows a submessage field.
  3782. //
  3783. // Be careful to exclude the case [proto.ext] { ... } since the `]` is
  3784. // the TT_SelectorName there, but we don't want to break inside the brackets.
  3785. //
  3786. // Another edge case is @submessage { key: value }, which is a common
  3787. // substitution placeholder. In this case we want to keep `@` and `submessage`
  3788. // together.
  3789. //
  3790. // We ensure elsewhere that extensions are always on their own line.
  3791. if ((Style.Language == FormatStyle::LK_Proto ||
  3792. Style.Language == FormatStyle::LK_TextProto) &&
  3793. Right.is(TT_SelectorName) && !Right.is(tok::r_square) && Right.Next) {
  3794. // Keep `@submessage` together in:
  3795. // @submessage { key: value }
  3796. if (Left.is(tok::at))
  3797. return false;
  3798. // Look for the scope opener after selector in cases like:
  3799. // selector { ...
  3800. // selector: { ...
  3801. // selector: @base { ...
  3802. FormatToken *LBrace = Right.Next;
  3803. if (LBrace && LBrace->is(tok::colon)) {
  3804. LBrace = LBrace->Next;
  3805. if (LBrace && LBrace->is(tok::at)) {
  3806. LBrace = LBrace->Next;
  3807. if (LBrace)
  3808. LBrace = LBrace->Next;
  3809. }
  3810. }
  3811. if (LBrace &&
  3812. // The scope opener is one of {, [, <:
  3813. // selector { ... }
  3814. // selector [ ... ]
  3815. // selector < ... >
  3816. //
  3817. // In case of selector { ... }, the l_brace is TT_DictLiteral.
  3818. // In case of an empty selector {}, the l_brace is not TT_DictLiteral,
  3819. // so we check for immediately following r_brace.
  3820. ((LBrace->is(tok::l_brace) &&
  3821. (LBrace->is(TT_DictLiteral) ||
  3822. (LBrace->Next && LBrace->Next->is(tok::r_brace)))) ||
  3823. LBrace->is(TT_ArrayInitializerLSquare) || LBrace->is(tok::less))) {
  3824. // If Left.ParameterCount is 0, then this submessage entry is not the
  3825. // first in its parent submessage, and we want to break before this entry.
  3826. // If Left.ParameterCount is greater than 0, then its parent submessage
  3827. // might contain 1 or more entries and we want to break before this entry
  3828. // if it contains at least 2 entries. We deal with this case later by
  3829. // detecting and breaking before the next entry in the parent submessage.
  3830. if (Left.ParameterCount == 0)
  3831. return true;
  3832. // However, if this submessage is the first entry in its parent
  3833. // submessage, Left.ParameterCount might be 1 in some cases.
  3834. // We deal with this case later by detecting an entry
  3835. // following a closing paren of this submessage.
  3836. }
  3837. // If this is an entry immediately following a submessage, it will be
  3838. // preceded by a closing paren of that submessage, like in:
  3839. // left---. .---right
  3840. // v v
  3841. // sub: { ... } key: value
  3842. // If there was a comment between `}` an `key` above, then `key` would be
  3843. // put on a new line anyways.
  3844. if (Left.isOneOf(tok::r_brace, tok::greater, tok::r_square))
  3845. return true;
  3846. }
  3847. // Deal with lambda arguments in C++ - we want consistent line breaks whether
  3848. // they happen to be at arg0, arg1 or argN. The selection is a bit nuanced
  3849. // as aggressive line breaks are placed when the lambda is not the last arg.
  3850. if ((Style.Language == FormatStyle::LK_Cpp ||
  3851. Style.Language == FormatStyle::LK_ObjC) &&
  3852. Left.is(tok::l_paren) && Left.BlockParameterCount > 0 &&
  3853. !Right.isOneOf(tok::l_paren, TT_LambdaLSquare)) {
  3854. // Multiple lambdas in the same function call force line breaks.
  3855. if (Left.BlockParameterCount > 1)
  3856. return true;
  3857. #if 0
  3858. // A lambda followed by another arg forces a line break.
  3859. if (!Left.Role)
  3860. return false;
  3861. auto Comma = Left.Role->lastComma();
  3862. if (!Comma)
  3863. return false;
  3864. auto Next = Comma->getNextNonComment();
  3865. if (!Next)
  3866. return false;
  3867. if (!Next->isOneOf(TT_LambdaLSquare, tok::l_brace, tok::caret))
  3868. return true;
  3869. #endif
  3870. }
  3871. return false;
  3872. }
  3873. bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
  3874. const FormatToken &Right) {
  3875. const FormatToken &Left = *Right.Previous;
  3876. if (Left.is(TT_AttributeMacro)) {
  3877. return false;
  3878. }
  3879. // Language-specific stuff.
  3880. if (Style.isCSharp()) {
  3881. if (Left.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon) ||
  3882. Right.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon))
  3883. return false;
  3884. // Only break after commas for generic type constraints.
  3885. if (Line.First->is(TT_CSharpGenericTypeConstraint))
  3886. return Left.is(TT_CSharpGenericTypeConstraintComma);
  3887. // Keep nullable operators attached to their identifiers.
  3888. if (Right.is(TT_CSharpNullable)) {
  3889. return false;
  3890. }
  3891. } else if (Style.Language == FormatStyle::LK_Java) {
  3892. if (Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
  3893. Keywords.kw_implements))
  3894. return false;
  3895. if (Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
  3896. Keywords.kw_implements))
  3897. return true;
  3898. } else if (Style.isJavaScript()) {
  3899. const FormatToken *NonComment = Right.getPreviousNonComment();
  3900. if (NonComment &&
  3901. NonComment->isOneOf(
  3902. tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
  3903. tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
  3904. tok::kw_static, tok::kw_public, tok::kw_private, tok::kw_protected,
  3905. Keywords.kw_readonly, Keywords.kw_override, Keywords.kw_abstract,
  3906. Keywords.kw_get, Keywords.kw_set, Keywords.kw_async,
  3907. Keywords.kw_await))
  3908. return false; // Otherwise automatic semicolon insertion would trigger.
  3909. if (Right.NestingLevel == 0 &&
  3910. (Left.Tok.getIdentifierInfo() ||
  3911. Left.isOneOf(tok::r_square, tok::r_paren)) &&
  3912. Right.isOneOf(tok::l_square, tok::l_paren))
  3913. return false; // Otherwise automatic semicolon insertion would trigger.
  3914. if (NonComment && NonComment->is(tok::identifier) &&
  3915. NonComment->TokenText == "asserts")
  3916. return false;
  3917. if (Left.is(TT_FatArrow) && Right.is(tok::l_brace))
  3918. return false;
  3919. if (Left.is(TT_JsTypeColon))
  3920. return true;
  3921. // Don't wrap between ":" and "!" of a strict prop init ("field!: type;").
  3922. if (Left.is(tok::exclaim) && Right.is(tok::colon))
  3923. return false;
  3924. // Look for is type annotations like:
  3925. // function f(): a is B { ... }
  3926. // Do not break before is in these cases.
  3927. if (Right.is(Keywords.kw_is)) {
  3928. const FormatToken *Next = Right.getNextNonComment();
  3929. // If `is` is followed by a colon, it's likely that it's a dict key, so
  3930. // ignore it for this check.
  3931. // For example this is common in Polymer:
  3932. // Polymer({
  3933. // is: 'name',
  3934. // ...
  3935. // });
  3936. if (!Next || !Next->is(tok::colon))
  3937. return false;
  3938. }
  3939. if (Left.is(Keywords.kw_in))
  3940. return Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None;
  3941. if (Right.is(Keywords.kw_in))
  3942. return Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None;
  3943. if (Right.is(Keywords.kw_as))
  3944. return false; // must not break before as in 'x as type' casts
  3945. if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
  3946. // extends and infer can appear as keywords in conditional types:
  3947. // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#conditional-types
  3948. // do not break before them, as the expressions are subject to ASI.
  3949. return false;
  3950. }
  3951. if (Left.is(Keywords.kw_as))
  3952. return true;
  3953. if (Left.is(TT_NonNullAssertion))
  3954. return true;
  3955. if (Left.is(Keywords.kw_declare) &&
  3956. Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
  3957. Keywords.kw_function, tok::kw_class, tok::kw_enum,
  3958. Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
  3959. Keywords.kw_let, tok::kw_const))
  3960. // See grammar for 'declare' statements at:
  3961. // https://github.com/Microsoft/TypeScript/blob/main/doc/spec-ARCHIVED.md#A.10
  3962. return false;
  3963. if (Left.isOneOf(Keywords.kw_module, tok::kw_namespace) &&
  3964. Right.isOneOf(tok::identifier, tok::string_literal))
  3965. return false; // must not break in "module foo { ...}"
  3966. if (Right.is(TT_TemplateString) && Right.closesScope())
  3967. return false;
  3968. // Don't split tagged template literal so there is a break between the tag
  3969. // identifier and template string.
  3970. if (Left.is(tok::identifier) && Right.is(TT_TemplateString)) {
  3971. return false;
  3972. }
  3973. if (Left.is(TT_TemplateString) && Left.opensScope())
  3974. return true;
  3975. }
  3976. if (Left.is(tok::at))
  3977. return false;
  3978. if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
  3979. return false;
  3980. if (Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
  3981. return !Right.is(tok::l_paren);
  3982. if (Right.is(TT_PointerOrReference))
  3983. return Line.IsMultiVariableDeclStmt ||
  3984. (getTokenPointerOrReferenceAlignment(Right) ==
  3985. FormatStyle::PAS_Right &&
  3986. (!Right.Next || Right.Next->isNot(TT_FunctionDeclarationName)));
  3987. if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
  3988. Right.is(tok::kw_operator))
  3989. return true;
  3990. if (Left.is(TT_PointerOrReference))
  3991. return false;
  3992. if (Right.isTrailingComment())
  3993. // We rely on MustBreakBefore being set correctly here as we should not
  3994. // change the "binding" behavior of a comment.
  3995. // The first comment in a braced lists is always interpreted as belonging to
  3996. // the first list element. Otherwise, it should be placed outside of the
  3997. // list.
  3998. return Left.is(BK_BracedInit) ||
  3999. (Left.is(TT_CtorInitializerColon) &&
  4000. Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon);
  4001. if (Left.is(tok::question) && Right.is(tok::colon))
  4002. return false;
  4003. if (Right.is(TT_ConditionalExpr) || Right.is(tok::question))
  4004. return Style.BreakBeforeTernaryOperators;
  4005. if (Left.is(TT_ConditionalExpr) || Left.is(tok::question))
  4006. return !Style.BreakBeforeTernaryOperators;
  4007. if (Left.is(TT_InheritanceColon))
  4008. return Style.BreakInheritanceList == FormatStyle::BILS_AfterColon;
  4009. if (Right.is(TT_InheritanceColon))
  4010. return Style.BreakInheritanceList != FormatStyle::BILS_AfterColon;
  4011. if (Right.is(TT_ObjCMethodExpr) && !Right.is(tok::r_square) &&
  4012. Left.isNot(TT_SelectorName))
  4013. return true;
  4014. if (Right.is(tok::colon) &&
  4015. !Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon))
  4016. return false;
  4017. if (Left.is(tok::colon) && Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) {
  4018. if (Style.Language == FormatStyle::LK_Proto ||
  4019. Style.Language == FormatStyle::LK_TextProto) {
  4020. if (!Style.AlwaysBreakBeforeMultilineStrings && Right.isStringLiteral())
  4021. return false;
  4022. // Prevent cases like:
  4023. //
  4024. // submessage:
  4025. // { key: valueeeeeeeeeeee }
  4026. //
  4027. // when the snippet does not fit into one line.
  4028. // Prefer:
  4029. //
  4030. // submessage: {
  4031. // key: valueeeeeeeeeeee
  4032. // }
  4033. //
  4034. // instead, even if it is longer by one line.
  4035. //
  4036. // Note that this allows allows the "{" to go over the column limit
  4037. // when the column limit is just between ":" and "{", but that does
  4038. // not happen too often and alternative formattings in this case are
  4039. // not much better.
  4040. //
  4041. // The code covers the cases:
  4042. //
  4043. // submessage: { ... }
  4044. // submessage: < ... >
  4045. // repeated: [ ... ]
  4046. if (((Right.is(tok::l_brace) || Right.is(tok::less)) &&
  4047. Right.is(TT_DictLiteral)) ||
  4048. Right.is(TT_ArrayInitializerLSquare))
  4049. return false;
  4050. }
  4051. return true;
  4052. }
  4053. if (Right.is(tok::r_square) && Right.MatchingParen &&
  4054. Right.MatchingParen->is(TT_ProtoExtensionLSquare))
  4055. return false;
  4056. if (Right.is(TT_SelectorName) || (Right.is(tok::identifier) && Right.Next &&
  4057. Right.Next->is(TT_ObjCMethodExpr)))
  4058. return Left.isNot(tok::period); // FIXME: Properly parse ObjC calls.
  4059. if (Left.is(tok::r_paren) && Line.Type == LT_ObjCProperty)
  4060. return true;
  4061. if (Left.ClosesTemplateDeclaration || Left.is(TT_FunctionAnnotationRParen))
  4062. return true;
  4063. if (Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
  4064. TT_OverloadedOperator))
  4065. return false;
  4066. if (Left.is(TT_RangeBasedForLoopColon))
  4067. return true;
  4068. if (Right.is(TT_RangeBasedForLoopColon))
  4069. return false;
  4070. if (Left.is(TT_TemplateCloser) && Right.is(TT_TemplateOpener))
  4071. return true;
  4072. if (Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator) ||
  4073. Left.is(tok::kw_operator))
  4074. return false;
  4075. if (Left.is(tok::equal) && !Right.isOneOf(tok::kw_default, tok::kw_delete) &&
  4076. Line.Type == LT_VirtualFunctionDecl && Left.NestingLevel == 0)
  4077. return false;
  4078. if (Left.is(tok::equal) && Right.is(tok::l_brace) &&
  4079. !Style.Cpp11BracedListStyle)
  4080. return false;
  4081. if (Left.is(tok::l_paren) &&
  4082. Left.isOneOf(TT_AttributeParen, TT_TypeDeclarationParen))
  4083. return false;
  4084. if (Left.is(tok::l_paren) && Left.Previous &&
  4085. (Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen)))
  4086. return false;
  4087. if (Right.is(TT_ImplicitStringLiteral))
  4088. return false;
  4089. if (Right.is(TT_TemplateCloser))
  4090. return false;
  4091. if (Right.is(tok::r_square) && Right.MatchingParen &&
  4092. Right.MatchingParen->is(TT_LambdaLSquare))
  4093. return false;
  4094. // We only break before r_brace if there was a corresponding break before
  4095. // the l_brace, which is tracked by BreakBeforeClosingBrace.
  4096. if (Right.is(tok::r_brace))
  4097. return Right.MatchingParen && Right.MatchingParen->is(BK_Block);
  4098. // We only break before r_paren if we're in a block indented context.
  4099. if (Right.is(tok::r_paren)) {
  4100. if (Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) {
  4101. return Right.MatchingParen &&
  4102. !(Right.MatchingParen->Previous &&
  4103. (Right.MatchingParen->Previous->is(tok::kw_for) ||
  4104. Right.MatchingParen->Previous->isIf()));
  4105. }
  4106. return false;
  4107. }
  4108. // Allow breaking after a trailing annotation, e.g. after a method
  4109. // declaration.
  4110. if (Left.is(TT_TrailingAnnotation))
  4111. return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
  4112. tok::less, tok::coloncolon);
  4113. if (Right.is(tok::kw___attribute) ||
  4114. (Right.is(tok::l_square) && Right.is(TT_AttributeSquare)))
  4115. return !Left.is(TT_AttributeSquare);
  4116. if (Left.is(tok::identifier) && Right.is(tok::string_literal))
  4117. return true;
  4118. if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
  4119. return true;
  4120. if (Left.is(TT_CtorInitializerColon))
  4121. return Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon;
  4122. if (Right.is(TT_CtorInitializerColon))
  4123. return Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon;
  4124. if (Left.is(TT_CtorInitializerComma) &&
  4125. Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma)
  4126. return false;
  4127. if (Right.is(TT_CtorInitializerComma) &&
  4128. Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma)
  4129. return true;
  4130. if (Left.is(TT_InheritanceComma) &&
  4131. Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma)
  4132. return false;
  4133. if (Right.is(TT_InheritanceComma) &&
  4134. Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma)
  4135. return true;
  4136. if ((Left.is(tok::greater) && Right.is(tok::greater)) ||
  4137. (Left.is(tok::less) && Right.is(tok::less)))
  4138. return false;
  4139. if (Right.is(TT_BinaryOperator) &&
  4140. Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None &&
  4141. (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_All ||
  4142. Right.getPrecedence() != prec::Assignment))
  4143. return true;
  4144. if (Left.is(TT_ArrayInitializerLSquare))
  4145. return true;
  4146. if (Right.is(tok::kw_typename) && Left.isNot(tok::kw_const))
  4147. return true;
  4148. if ((Left.isBinaryOperator() || Left.is(TT_BinaryOperator)) &&
  4149. !Left.isOneOf(tok::arrowstar, tok::lessless) &&
  4150. Style.BreakBeforeBinaryOperators != FormatStyle::BOS_All &&
  4151. (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None ||
  4152. Left.getPrecedence() == prec::Assignment))
  4153. return true;
  4154. if ((Left.is(TT_AttributeSquare) && Right.is(tok::l_square)) ||
  4155. (Left.is(tok::r_square) && Right.is(TT_AttributeSquare)))
  4156. return false;
  4157. auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
  4158. if (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace)) {
  4159. if (isAllmanLambdaBrace(Left))
  4160. return !isItAnEmptyLambdaAllowed(Left, ShortLambdaOption);
  4161. if (isAllmanLambdaBrace(Right))
  4162. return !isItAnEmptyLambdaAllowed(Right, ShortLambdaOption);
  4163. }
  4164. return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
  4165. tok::kw_class, tok::kw_struct, tok::comment) ||
  4166. Right.isMemberAccess() ||
  4167. Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
  4168. tok::colon, tok::l_square, tok::at) ||
  4169. (Left.is(tok::r_paren) &&
  4170. Right.isOneOf(tok::identifier, tok::kw_const)) ||
  4171. (Left.is(tok::l_paren) && !Right.is(tok::r_paren)) ||
  4172. (Left.is(TT_TemplateOpener) && !Right.is(TT_TemplateCloser));
  4173. }
  4174. void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) {
  4175. llvm::errs() << "AnnotatedTokens(L=" << Line.Level << "):\n";
  4176. const FormatToken *Tok = Line.First;
  4177. while (Tok) {
  4178. llvm::errs() << " M=" << Tok->MustBreakBefore
  4179. << " C=" << Tok->CanBreakBefore
  4180. << " T=" << getTokenTypeName(Tok->getType())
  4181. << " S=" << Tok->SpacesRequiredBefore
  4182. << " F=" << Tok->Finalized << " B=" << Tok->BlockParameterCount
  4183. << " BK=" << Tok->getBlockKind() << " P=" << Tok->SplitPenalty
  4184. << " Name=" << Tok->Tok.getName() << " L=" << Tok->TotalLength
  4185. << " PPK=" << Tok->getPackingKind() << " FakeLParens=";
  4186. for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i)
  4187. llvm::errs() << Tok->FakeLParens[i] << "/";
  4188. llvm::errs() << " FakeRParens=" << Tok->FakeRParens;
  4189. llvm::errs() << " II=" << Tok->Tok.getIdentifierInfo();
  4190. llvm::errs() << " Text='" << Tok->TokenText << "'\n";
  4191. if (!Tok->Next)
  4192. assert(Tok == Line.Last);
  4193. Tok = Tok->Next;
  4194. }
  4195. llvm::errs() << "----\n";
  4196. }
  4197. FormatStyle::PointerAlignmentStyle
  4198. TokenAnnotator::getTokenReferenceAlignment(const FormatToken &Reference) {
  4199. assert(Reference.isOneOf(tok::amp, tok::ampamp));
  4200. switch (Style.ReferenceAlignment) {
  4201. case FormatStyle::RAS_Pointer:
  4202. return Style.PointerAlignment;
  4203. case FormatStyle::RAS_Left:
  4204. return FormatStyle::PAS_Left;
  4205. case FormatStyle::RAS_Right:
  4206. return FormatStyle::PAS_Right;
  4207. case FormatStyle::RAS_Middle:
  4208. return FormatStyle::PAS_Middle;
  4209. }
  4210. assert(0); //"Unhandled value of ReferenceAlignment"
  4211. return Style.PointerAlignment;
  4212. }
  4213. FormatStyle::PointerAlignmentStyle
  4214. TokenAnnotator::getTokenPointerOrReferenceAlignment(
  4215. const FormatToken &PointerOrReference) {
  4216. if (PointerOrReference.isOneOf(tok::amp, tok::ampamp)) {
  4217. switch (Style.ReferenceAlignment) {
  4218. case FormatStyle::RAS_Pointer:
  4219. return Style.PointerAlignment;
  4220. case FormatStyle::RAS_Left:
  4221. return FormatStyle::PAS_Left;
  4222. case FormatStyle::RAS_Right:
  4223. return FormatStyle::PAS_Right;
  4224. case FormatStyle::RAS_Middle:
  4225. return FormatStyle::PAS_Middle;
  4226. }
  4227. }
  4228. assert(PointerOrReference.is(tok::star));
  4229. return Style.PointerAlignment;
  4230. }
  4231. } // namespace format
  4232. } // namespace clang