TokenAnnotator.cpp 200 KB

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