FormatTest.cpp 424 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298
  1. //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "clang/Format/Format.h"
  10. #include "../Tooling/RewriterTestContext.h"
  11. #include "FormatTestUtils.h"
  12. #include "clang/Frontend/TextDiagnosticPrinter.h"
  13. #include "llvm/Support/Debug.h"
  14. #include "llvm/Support/MemoryBuffer.h"
  15. #include "gtest/gtest.h"
  16. #define DEBUG_TYPE "format-test"
  17. namespace clang {
  18. namespace format {
  19. namespace {
  20. FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
  21. class FormatTest : public ::testing::Test {
  22. protected:
  23. enum IncompleteCheck {
  24. IC_ExpectComplete,
  25. IC_ExpectIncomplete,
  26. IC_DoNotCheck
  27. };
  28. std::string format(llvm::StringRef Code,
  29. const FormatStyle &Style = getLLVMStyle(),
  30. IncompleteCheck CheckIncomplete = IC_ExpectComplete) {
  31. DEBUG(llvm::errs() << "---\n");
  32. DEBUG(llvm::errs() << Code << "\n\n");
  33. std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
  34. bool IncompleteFormat = false;
  35. tooling::Replacements Replaces =
  36. reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
  37. if (CheckIncomplete != IC_DoNotCheck) {
  38. bool ExpectedIncompleteFormat = CheckIncomplete == IC_ExpectIncomplete;
  39. EXPECT_EQ(ExpectedIncompleteFormat, IncompleteFormat) << Code << "\n\n";
  40. }
  41. ReplacementCount = Replaces.size();
  42. std::string Result = applyAllReplacements(Code, Replaces);
  43. EXPECT_NE("", Result);
  44. DEBUG(llvm::errs() << "\n" << Result << "\n\n");
  45. return Result;
  46. }
  47. FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
  48. FormatStyle Style = getLLVMStyle();
  49. Style.ColumnLimit = ColumnLimit;
  50. return Style;
  51. }
  52. FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
  53. FormatStyle Style = getGoogleStyle();
  54. Style.ColumnLimit = ColumnLimit;
  55. return Style;
  56. }
  57. void verifyFormat(llvm::StringRef Code,
  58. const FormatStyle &Style = getLLVMStyle()) {
  59. EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
  60. }
  61. void verifyIncompleteFormat(llvm::StringRef Code,
  62. const FormatStyle &Style = getLLVMStyle()) {
  63. EXPECT_EQ(Code.str(),
  64. format(test::messUp(Code), Style, IC_ExpectIncomplete));
  65. }
  66. void verifyGoogleFormat(llvm::StringRef Code) {
  67. verifyFormat(Code, getGoogleStyle());
  68. }
  69. void verifyIndependentOfContext(llvm::StringRef text) {
  70. verifyFormat(text);
  71. verifyFormat(llvm::Twine("void f() { " + text + " }").str());
  72. }
  73. /// \brief Verify that clang-format does not crash on the given input.
  74. void verifyNoCrash(llvm::StringRef Code,
  75. const FormatStyle &Style = getLLVMStyle()) {
  76. format(Code, Style, IC_DoNotCheck);
  77. }
  78. int ReplacementCount;
  79. };
  80. TEST_F(FormatTest, MessUp) {
  81. EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
  82. EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
  83. EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
  84. EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
  85. EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
  86. }
  87. //===----------------------------------------------------------------------===//
  88. // Basic function tests.
  89. //===----------------------------------------------------------------------===//
  90. TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
  91. EXPECT_EQ(";", format(";"));
  92. }
  93. TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
  94. EXPECT_EQ("int i;", format(" int i;"));
  95. EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;"));
  96. EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
  97. EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
  98. }
  99. TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
  100. EXPECT_EQ("int i;", format("int\ni;"));
  101. }
  102. TEST_F(FormatTest, FormatsNestedBlockStatements) {
  103. EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
  104. }
  105. TEST_F(FormatTest, FormatsNestedCall) {
  106. verifyFormat("Method(f1, f2(f3));");
  107. verifyFormat("Method(f1(f2, f3()));");
  108. verifyFormat("Method(f1(f2, (f3())));");
  109. }
  110. TEST_F(FormatTest, NestedNameSpecifiers) {
  111. verifyFormat("vector<::Type> v;");
  112. verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
  113. verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
  114. verifyFormat("bool a = 2 < ::SomeFunction();");
  115. }
  116. TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
  117. EXPECT_EQ("if (a) {\n"
  118. " f();\n"
  119. "}",
  120. format("if(a){f();}"));
  121. EXPECT_EQ(4, ReplacementCount);
  122. EXPECT_EQ("if (a) {\n"
  123. " f();\n"
  124. "}",
  125. format("if (a) {\n"
  126. " f();\n"
  127. "}"));
  128. EXPECT_EQ(0, ReplacementCount);
  129. EXPECT_EQ("/*\r\n"
  130. "\r\n"
  131. "*/\r\n",
  132. format("/*\r\n"
  133. "\r\n"
  134. "*/\r\n"));
  135. EXPECT_EQ(0, ReplacementCount);
  136. }
  137. TEST_F(FormatTest, RemovesEmptyLines) {
  138. EXPECT_EQ("class C {\n"
  139. " int i;\n"
  140. "};",
  141. format("class C {\n"
  142. " int i;\n"
  143. "\n"
  144. "};"));
  145. // Don't remove empty lines at the start of namespaces or extern "C" blocks.
  146. EXPECT_EQ("namespace N {\n"
  147. "\n"
  148. "int i;\n"
  149. "}",
  150. format("namespace N {\n"
  151. "\n"
  152. "int i;\n"
  153. "}",
  154. getGoogleStyle()));
  155. EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
  156. "\n"
  157. "int i;\n"
  158. "}",
  159. format("extern /**/ \"C\" /**/ {\n"
  160. "\n"
  161. "int i;\n"
  162. "}",
  163. getGoogleStyle()));
  164. // ...but do keep inlining and removing empty lines for non-block extern "C"
  165. // functions.
  166. verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
  167. EXPECT_EQ("extern \"C\" int f() {\n"
  168. " int i = 42;\n"
  169. " return i;\n"
  170. "}",
  171. format("extern \"C\" int f() {\n"
  172. "\n"
  173. " int i = 42;\n"
  174. " return i;\n"
  175. "}",
  176. getGoogleStyle()));
  177. // Remove empty lines at the beginning and end of blocks.
  178. EXPECT_EQ("void f() {\n"
  179. "\n"
  180. " if (a) {\n"
  181. "\n"
  182. " f();\n"
  183. " }\n"
  184. "}",
  185. format("void f() {\n"
  186. "\n"
  187. " if (a) {\n"
  188. "\n"
  189. " f();\n"
  190. "\n"
  191. " }\n"
  192. "\n"
  193. "}",
  194. getLLVMStyle()));
  195. EXPECT_EQ("void f() {\n"
  196. " if (a) {\n"
  197. " f();\n"
  198. " }\n"
  199. "}",
  200. format("void f() {\n"
  201. "\n"
  202. " if (a) {\n"
  203. "\n"
  204. " f();\n"
  205. "\n"
  206. " }\n"
  207. "\n"
  208. "}",
  209. getGoogleStyle()));
  210. // Don't remove empty lines in more complex control statements.
  211. EXPECT_EQ("void f() {\n"
  212. " if (a) {\n"
  213. " f();\n"
  214. "\n"
  215. " } else if (b) {\n"
  216. " f();\n"
  217. " }\n"
  218. "}",
  219. format("void f() {\n"
  220. " if (a) {\n"
  221. " f();\n"
  222. "\n"
  223. " } else if (b) {\n"
  224. " f();\n"
  225. "\n"
  226. " }\n"
  227. "\n"
  228. "}"));
  229. // FIXME: This is slightly inconsistent.
  230. EXPECT_EQ("namespace {\n"
  231. "int i;\n"
  232. "}",
  233. format("namespace {\n"
  234. "int i;\n"
  235. "\n"
  236. "}"));
  237. EXPECT_EQ("namespace {\n"
  238. "int i;\n"
  239. "\n"
  240. "} // namespace",
  241. format("namespace {\n"
  242. "int i;\n"
  243. "\n"
  244. "} // namespace"));
  245. }
  246. TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
  247. verifyFormat("x = (a) and (b);");
  248. verifyFormat("x = (a) or (b);");
  249. verifyFormat("x = (a) bitand (b);");
  250. verifyFormat("x = (a) bitor (b);");
  251. verifyFormat("x = (a) not_eq (b);");
  252. verifyFormat("x = (a) and_eq (b);");
  253. verifyFormat("x = (a) or_eq (b);");
  254. verifyFormat("x = (a) xor (b);");
  255. }
  256. //===----------------------------------------------------------------------===//
  257. // Tests for control statements.
  258. //===----------------------------------------------------------------------===//
  259. TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
  260. verifyFormat("if (true)\n f();\ng();");
  261. verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
  262. verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
  263. FormatStyle AllowsMergedIf = getLLVMStyle();
  264. AllowsMergedIf.AlignEscapedNewlinesLeft = true;
  265. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  266. verifyFormat("if (a)\n"
  267. " // comment\n"
  268. " f();",
  269. AllowsMergedIf);
  270. verifyFormat("{\n"
  271. " if (a)\n"
  272. " label:\n"
  273. " f();\n"
  274. "}",
  275. AllowsMergedIf);
  276. verifyFormat("#define A \\\n"
  277. " if (a) \\\n"
  278. " label: \\\n"
  279. " f()",
  280. AllowsMergedIf);
  281. verifyFormat("if (a)\n"
  282. " ;",
  283. AllowsMergedIf);
  284. verifyFormat("if (a)\n"
  285. " if (b) return;",
  286. AllowsMergedIf);
  287. verifyFormat("if (a) // Can't merge this\n"
  288. " f();\n",
  289. AllowsMergedIf);
  290. verifyFormat("if (a) /* still don't merge */\n"
  291. " f();",
  292. AllowsMergedIf);
  293. verifyFormat("if (a) { // Never merge this\n"
  294. " f();\n"
  295. "}",
  296. AllowsMergedIf);
  297. verifyFormat("if (a) { /* Never merge this */\n"
  298. " f();\n"
  299. "}",
  300. AllowsMergedIf);
  301. AllowsMergedIf.ColumnLimit = 14;
  302. verifyFormat("if (a) return;", AllowsMergedIf);
  303. verifyFormat("if (aaaaaaaaa)\n"
  304. " return;",
  305. AllowsMergedIf);
  306. AllowsMergedIf.ColumnLimit = 13;
  307. verifyFormat("if (a)\n return;", AllowsMergedIf);
  308. }
  309. TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
  310. FormatStyle AllowsMergedLoops = getLLVMStyle();
  311. AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
  312. verifyFormat("while (true) continue;", AllowsMergedLoops);
  313. verifyFormat("for (;;) continue;", AllowsMergedLoops);
  314. verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
  315. verifyFormat("while (true)\n"
  316. " ;",
  317. AllowsMergedLoops);
  318. verifyFormat("for (;;)\n"
  319. " ;",
  320. AllowsMergedLoops);
  321. verifyFormat("for (;;)\n"
  322. " for (;;) continue;",
  323. AllowsMergedLoops);
  324. verifyFormat("for (;;) // Can't merge this\n"
  325. " continue;",
  326. AllowsMergedLoops);
  327. verifyFormat("for (;;) /* still don't merge */\n"
  328. " continue;",
  329. AllowsMergedLoops);
  330. }
  331. TEST_F(FormatTest, FormatShortBracedStatements) {
  332. FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
  333. AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
  334. AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
  335. AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
  336. verifyFormat("if (true) {}", AllowSimpleBracedStatements);
  337. verifyFormat("while (true) {}", AllowSimpleBracedStatements);
  338. verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
  339. verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
  340. verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
  341. verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
  342. verifyFormat("if (true) { //\n"
  343. " f();\n"
  344. "}",
  345. AllowSimpleBracedStatements);
  346. verifyFormat("if (true) {\n"
  347. " f();\n"
  348. " f();\n"
  349. "}",
  350. AllowSimpleBracedStatements);
  351. verifyFormat("if (true) {\n"
  352. " f();\n"
  353. "} else {\n"
  354. " f();\n"
  355. "}",
  356. AllowSimpleBracedStatements);
  357. verifyFormat("template <int> struct A2 {\n"
  358. " struct B {};\n"
  359. "};",
  360. AllowSimpleBracedStatements);
  361. AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
  362. verifyFormat("if (true) {\n"
  363. " f();\n"
  364. "}",
  365. AllowSimpleBracedStatements);
  366. verifyFormat("if (true) {\n"
  367. " f();\n"
  368. "} else {\n"
  369. " f();\n"
  370. "}",
  371. AllowSimpleBracedStatements);
  372. AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
  373. verifyFormat("while (true) {\n"
  374. " f();\n"
  375. "}",
  376. AllowSimpleBracedStatements);
  377. verifyFormat("for (;;) {\n"
  378. " f();\n"
  379. "}",
  380. AllowSimpleBracedStatements);
  381. }
  382. TEST_F(FormatTest, ParseIfElse) {
  383. verifyFormat("if (true)\n"
  384. " if (true)\n"
  385. " if (true)\n"
  386. " f();\n"
  387. " else\n"
  388. " g();\n"
  389. " else\n"
  390. " h();\n"
  391. "else\n"
  392. " i();");
  393. verifyFormat("if (true)\n"
  394. " if (true)\n"
  395. " if (true) {\n"
  396. " if (true)\n"
  397. " f();\n"
  398. " } else {\n"
  399. " g();\n"
  400. " }\n"
  401. " else\n"
  402. " h();\n"
  403. "else {\n"
  404. " i();\n"
  405. "}");
  406. verifyFormat("void f() {\n"
  407. " if (a) {\n"
  408. " } else {\n"
  409. " }\n"
  410. "}");
  411. }
  412. TEST_F(FormatTest, ElseIf) {
  413. verifyFormat("if (a) {\n} else if (b) {\n}");
  414. verifyFormat("if (a)\n"
  415. " f();\n"
  416. "else if (b)\n"
  417. " g();\n"
  418. "else\n"
  419. " h();");
  420. verifyFormat("if (a) {\n"
  421. " f();\n"
  422. "}\n"
  423. "// or else ..\n"
  424. "else {\n"
  425. " g()\n"
  426. "}");
  427. verifyFormat("if (a) {\n"
  428. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  429. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  430. "}");
  431. verifyFormat("if (a) {\n"
  432. "} else if (\n"
  433. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  434. "}",
  435. getLLVMStyleWithColumns(62));
  436. }
  437. TEST_F(FormatTest, FormatsForLoop) {
  438. verifyFormat(
  439. "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
  440. " ++VeryVeryLongLoopVariable)\n"
  441. " ;");
  442. verifyFormat("for (;;)\n"
  443. " f();");
  444. verifyFormat("for (;;) {\n}");
  445. verifyFormat("for (;;) {\n"
  446. " f();\n"
  447. "}");
  448. verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
  449. verifyFormat(
  450. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  451. " E = UnwrappedLines.end();\n"
  452. " I != E; ++I) {\n}");
  453. verifyFormat(
  454. "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
  455. " ++IIIII) {\n}");
  456. verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
  457. " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
  458. " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
  459. verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
  460. " I = FD->getDeclsInPrototypeScope().begin(),\n"
  461. " E = FD->getDeclsInPrototypeScope().end();\n"
  462. " I != E; ++I) {\n}");
  463. verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
  464. " I = Container.begin(),\n"
  465. " E = Container.end();\n"
  466. " I != E; ++I) {\n}",
  467. getLLVMStyleWithColumns(76));
  468. verifyFormat(
  469. "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  470. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
  471. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  472. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  473. " ++aaaaaaaaaaa) {\n}");
  474. verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  475. " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
  476. " ++i) {\n}");
  477. verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
  478. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  479. "}");
  480. verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
  481. " aaaaaaaaaa);\n"
  482. " iter; ++iter) {\n"
  483. "}");
  484. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  485. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  486. " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
  487. " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
  488. FormatStyle NoBinPacking = getLLVMStyle();
  489. NoBinPacking.BinPackParameters = false;
  490. verifyFormat("for (int aaaaaaaaaaa = 1;\n"
  491. " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
  492. " aaaaaaaaaaaaaaaa,\n"
  493. " aaaaaaaaaaaaaaaa,\n"
  494. " aaaaaaaaaaaaaaaa);\n"
  495. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  496. "}",
  497. NoBinPacking);
  498. verifyFormat(
  499. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  500. " E = UnwrappedLines.end();\n"
  501. " I != E;\n"
  502. " ++I) {\n}",
  503. NoBinPacking);
  504. }
  505. TEST_F(FormatTest, RangeBasedForLoops) {
  506. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  507. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  508. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
  509. " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
  510. verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
  511. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  512. verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
  513. " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
  514. }
  515. TEST_F(FormatTest, ForEachLoops) {
  516. verifyFormat("void f() {\n"
  517. " foreach (Item *item, itemlist) {}\n"
  518. " Q_FOREACH (Item *item, itemlist) {}\n"
  519. " BOOST_FOREACH (Item *item, itemlist) {}\n"
  520. " UNKNOWN_FORACH(Item * item, itemlist) {}\n"
  521. "}");
  522. // As function-like macros.
  523. verifyFormat("#define foreach(x, y)\n"
  524. "#define Q_FOREACH(x, y)\n"
  525. "#define BOOST_FOREACH(x, y)\n"
  526. "#define UNKNOWN_FOREACH(x, y)\n");
  527. // Not as function-like macros.
  528. verifyFormat("#define foreach (x, y)\n"
  529. "#define Q_FOREACH (x, y)\n"
  530. "#define BOOST_FOREACH (x, y)\n"
  531. "#define UNKNOWN_FOREACH (x, y)\n");
  532. }
  533. TEST_F(FormatTest, FormatsWhileLoop) {
  534. verifyFormat("while (true) {\n}");
  535. verifyFormat("while (true)\n"
  536. " f();");
  537. verifyFormat("while () {\n}");
  538. verifyFormat("while () {\n"
  539. " f();\n"
  540. "}");
  541. }
  542. TEST_F(FormatTest, FormatsDoWhile) {
  543. verifyFormat("do {\n"
  544. " do_something();\n"
  545. "} while (something());");
  546. verifyFormat("do\n"
  547. " do_something();\n"
  548. "while (something());");
  549. }
  550. TEST_F(FormatTest, FormatsSwitchStatement) {
  551. verifyFormat("switch (x) {\n"
  552. "case 1:\n"
  553. " f();\n"
  554. " break;\n"
  555. "case kFoo:\n"
  556. "case ns::kBar:\n"
  557. "case kBaz:\n"
  558. " break;\n"
  559. "default:\n"
  560. " g();\n"
  561. " break;\n"
  562. "}");
  563. verifyFormat("switch (x) {\n"
  564. "case 1: {\n"
  565. " f();\n"
  566. " break;\n"
  567. "}\n"
  568. "case 2: {\n"
  569. " break;\n"
  570. "}\n"
  571. "}");
  572. verifyFormat("switch (x) {\n"
  573. "case 1: {\n"
  574. " f();\n"
  575. " {\n"
  576. " g();\n"
  577. " h();\n"
  578. " }\n"
  579. " break;\n"
  580. "}\n"
  581. "}");
  582. verifyFormat("switch (x) {\n"
  583. "case 1: {\n"
  584. " f();\n"
  585. " if (foo) {\n"
  586. " g();\n"
  587. " h();\n"
  588. " }\n"
  589. " break;\n"
  590. "}\n"
  591. "}");
  592. verifyFormat("switch (x) {\n"
  593. "case 1: {\n"
  594. " f();\n"
  595. " g();\n"
  596. "} break;\n"
  597. "}");
  598. verifyFormat("switch (test)\n"
  599. " ;");
  600. verifyFormat("switch (x) {\n"
  601. "default: {\n"
  602. " // Do nothing.\n"
  603. "}\n"
  604. "}");
  605. verifyFormat("switch (x) {\n"
  606. "// comment\n"
  607. "// if 1, do f()\n"
  608. "case 1:\n"
  609. " f();\n"
  610. "}");
  611. verifyFormat("switch (x) {\n"
  612. "case 1:\n"
  613. " // Do amazing stuff\n"
  614. " {\n"
  615. " f();\n"
  616. " g();\n"
  617. " }\n"
  618. " break;\n"
  619. "}");
  620. verifyFormat("#define A \\\n"
  621. " switch (x) { \\\n"
  622. " case a: \\\n"
  623. " foo = b; \\\n"
  624. " }",
  625. getLLVMStyleWithColumns(20));
  626. verifyFormat("#define OPERATION_CASE(name) \\\n"
  627. " case OP_name: \\\n"
  628. " return operations::Operation##name\n",
  629. getLLVMStyleWithColumns(40));
  630. verifyFormat("switch (x) {\n"
  631. "case 1:;\n"
  632. "default:;\n"
  633. " int i;\n"
  634. "}");
  635. verifyGoogleFormat("switch (x) {\n"
  636. " case 1:\n"
  637. " f();\n"
  638. " break;\n"
  639. " case kFoo:\n"
  640. " case ns::kBar:\n"
  641. " case kBaz:\n"
  642. " break;\n"
  643. " default:\n"
  644. " g();\n"
  645. " break;\n"
  646. "}");
  647. verifyGoogleFormat("switch (x) {\n"
  648. " case 1: {\n"
  649. " f();\n"
  650. " break;\n"
  651. " }\n"
  652. "}");
  653. verifyGoogleFormat("switch (test)\n"
  654. " ;");
  655. verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
  656. " case OP_name: \\\n"
  657. " return operations::Operation##name\n");
  658. verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
  659. " // Get the correction operation class.\n"
  660. " switch (OpCode) {\n"
  661. " CASE(Add);\n"
  662. " CASE(Subtract);\n"
  663. " default:\n"
  664. " return operations::Unknown;\n"
  665. " }\n"
  666. "#undef OPERATION_CASE\n"
  667. "}");
  668. verifyFormat("DEBUG({\n"
  669. " switch (x) {\n"
  670. " case A:\n"
  671. " f();\n"
  672. " break;\n"
  673. " // On B:\n"
  674. " case B:\n"
  675. " g();\n"
  676. " break;\n"
  677. " }\n"
  678. "});");
  679. verifyFormat("switch (a) {\n"
  680. "case (b):\n"
  681. " return;\n"
  682. "}");
  683. verifyFormat("switch (a) {\n"
  684. "case some_namespace::\n"
  685. " some_constant:\n"
  686. " return;\n"
  687. "}",
  688. getLLVMStyleWithColumns(34));
  689. }
  690. TEST_F(FormatTest, CaseRanges) {
  691. verifyFormat("switch (x) {\n"
  692. "case 'A' ... 'Z':\n"
  693. "case 1 ... 5:\n"
  694. " break;\n"
  695. "}");
  696. }
  697. TEST_F(FormatTest, ShortCaseLabels) {
  698. FormatStyle Style = getLLVMStyle();
  699. Style.AllowShortCaseLabelsOnASingleLine = true;
  700. verifyFormat("switch (a) {\n"
  701. "case 1: x = 1; break;\n"
  702. "case 2: return;\n"
  703. "case 3:\n"
  704. "case 4:\n"
  705. "case 5: return;\n"
  706. "case 6: // comment\n"
  707. " return;\n"
  708. "case 7:\n"
  709. " // comment\n"
  710. " return;\n"
  711. "case 8:\n"
  712. " x = 8; // comment\n"
  713. " break;\n"
  714. "default: y = 1; break;\n"
  715. "}",
  716. Style);
  717. verifyFormat("switch (a) {\n"
  718. "#if FOO\n"
  719. "case 0: return 0;\n"
  720. "#endif\n"
  721. "}",
  722. Style);
  723. verifyFormat("switch (a) {\n"
  724. "case 1: {\n"
  725. "}\n"
  726. "case 2: {\n"
  727. " return;\n"
  728. "}\n"
  729. "case 3: {\n"
  730. " x = 1;\n"
  731. " return;\n"
  732. "}\n"
  733. "case 4:\n"
  734. " if (x)\n"
  735. " return;\n"
  736. "}",
  737. Style);
  738. Style.ColumnLimit = 21;
  739. verifyFormat("switch (a) {\n"
  740. "case 1: x = 1; break;\n"
  741. "case 2: return;\n"
  742. "case 3:\n"
  743. "case 4:\n"
  744. "case 5: return;\n"
  745. "default:\n"
  746. " y = 1;\n"
  747. " break;\n"
  748. "}",
  749. Style);
  750. }
  751. TEST_F(FormatTest, FormatsLabels) {
  752. verifyFormat("void f() {\n"
  753. " some_code();\n"
  754. "test_label:\n"
  755. " some_other_code();\n"
  756. " {\n"
  757. " some_more_code();\n"
  758. " another_label:\n"
  759. " some_more_code();\n"
  760. " }\n"
  761. "}");
  762. verifyFormat("{\n"
  763. " some_code();\n"
  764. "test_label:\n"
  765. " some_other_code();\n"
  766. "}");
  767. verifyFormat("{\n"
  768. " some_code();\n"
  769. "test_label:;\n"
  770. " int i = 0;\n"
  771. "}");
  772. }
  773. //===----------------------------------------------------------------------===//
  774. // Tests for comments.
  775. //===----------------------------------------------------------------------===//
  776. TEST_F(FormatTest, UnderstandsSingleLineComments) {
  777. verifyFormat("//* */");
  778. verifyFormat("// line 1\n"
  779. "// line 2\n"
  780. "void f() {}\n");
  781. verifyFormat("void f() {\n"
  782. " // Doesn't do anything\n"
  783. "}");
  784. verifyFormat("SomeObject\n"
  785. " // Calling someFunction on SomeObject\n"
  786. " .someFunction();");
  787. verifyFormat("auto result = SomeObject\n"
  788. " // Calling someFunction on SomeObject\n"
  789. " .someFunction();");
  790. verifyFormat("void f(int i, // some comment (probably for i)\n"
  791. " int j, // some comment (probably for j)\n"
  792. " int k); // some comment (probably for k)");
  793. verifyFormat("void f(int i,\n"
  794. " // some comment (probably for j)\n"
  795. " int j,\n"
  796. " // some comment (probably for k)\n"
  797. " int k);");
  798. verifyFormat("int i // This is a fancy variable\n"
  799. " = 5; // with nicely aligned comment.");
  800. verifyFormat("// Leading comment.\n"
  801. "int a; // Trailing comment.");
  802. verifyFormat("int a; // Trailing comment\n"
  803. " // on 2\n"
  804. " // or 3 lines.\n"
  805. "int b;");
  806. verifyFormat("int a; // Trailing comment\n"
  807. "\n"
  808. "// Leading comment.\n"
  809. "int b;");
  810. verifyFormat("int a; // Comment.\n"
  811. " // More details.\n"
  812. "int bbbb; // Another comment.");
  813. verifyFormat(
  814. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  815. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n"
  816. "int cccccccccccccccccccccccccccccc; // comment\n"
  817. "int ddd; // looooooooooooooooooooooooong comment\n"
  818. "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  819. "int bbbbbbbbbbbbbbbbbbbbb; // comment\n"
  820. "int ccccccccccccccccccc; // comment");
  821. verifyFormat("#include \"a\" // comment\n"
  822. "#include \"a/b/c\" // comment");
  823. verifyFormat("#include <a> // comment\n"
  824. "#include <a/b/c> // comment");
  825. EXPECT_EQ("#include \"a\" // comment\n"
  826. "#include \"a/b/c\" // comment",
  827. format("#include \\\n"
  828. " \"a\" // comment\n"
  829. "#include \"a/b/c\" // comment"));
  830. verifyFormat("enum E {\n"
  831. " // comment\n"
  832. " VAL_A, // comment\n"
  833. " VAL_B\n"
  834. "};");
  835. verifyFormat(
  836. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  837. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
  838. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  839. " // Comment inside a statement.\n"
  840. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  841. verifyFormat("SomeFunction(a,\n"
  842. " // comment\n"
  843. " b + x);");
  844. verifyFormat("SomeFunction(a, a,\n"
  845. " // comment\n"
  846. " b + x);");
  847. verifyFormat(
  848. "bool aaaaaaaaaaaaa = // comment\n"
  849. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  850. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  851. verifyFormat("int aaaa; // aaaaa\n"
  852. "int aa; // aaaaaaa",
  853. getLLVMStyleWithColumns(20));
  854. EXPECT_EQ("void f() { // This does something ..\n"
  855. "}\n"
  856. "int a; // This is unrelated",
  857. format("void f() { // This does something ..\n"
  858. " }\n"
  859. "int a; // This is unrelated"));
  860. EXPECT_EQ("class C {\n"
  861. " void f() { // This does something ..\n"
  862. " } // awesome..\n"
  863. "\n"
  864. " int a; // This is unrelated\n"
  865. "};",
  866. format("class C{void f() { // This does something ..\n"
  867. " } // awesome..\n"
  868. " \n"
  869. "int a; // This is unrelated\n"
  870. "};"));
  871. EXPECT_EQ("int i; // single line trailing comment",
  872. format("int i;\\\n// single line trailing comment"));
  873. verifyGoogleFormat("int a; // Trailing comment.");
  874. verifyFormat("someFunction(anotherFunction( // Force break.\n"
  875. " parameter));");
  876. verifyGoogleFormat("#endif // HEADER_GUARD");
  877. verifyFormat("const char *test[] = {\n"
  878. " // A\n"
  879. " \"aaaa\",\n"
  880. " // B\n"
  881. " \"aaaaa\"};");
  882. verifyGoogleFormat(
  883. "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  884. " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment");
  885. EXPECT_EQ("D(a, {\n"
  886. " // test\n"
  887. " int a;\n"
  888. "});",
  889. format("D(a, {\n"
  890. "// test\n"
  891. "int a;\n"
  892. "});"));
  893. EXPECT_EQ("lineWith(); // comment\n"
  894. "// at start\n"
  895. "otherLine();",
  896. format("lineWith(); // comment\n"
  897. "// at start\n"
  898. "otherLine();"));
  899. EXPECT_EQ("lineWith(); // comment\n"
  900. "/*\n"
  901. " * at start */\n"
  902. "otherLine();",
  903. format("lineWith(); // comment\n"
  904. "/*\n"
  905. " * at start */\n"
  906. "otherLine();"));
  907. EXPECT_EQ("lineWith(); // comment\n"
  908. " // at start\n"
  909. "otherLine();",
  910. format("lineWith(); // comment\n"
  911. " // at start\n"
  912. "otherLine();"));
  913. EXPECT_EQ("lineWith(); // comment\n"
  914. "// at start\n"
  915. "otherLine(); // comment",
  916. format("lineWith(); // comment\n"
  917. "// at start\n"
  918. "otherLine(); // comment"));
  919. EXPECT_EQ("lineWith();\n"
  920. "// at start\n"
  921. "otherLine(); // comment",
  922. format("lineWith();\n"
  923. " // at start\n"
  924. "otherLine(); // comment"));
  925. EXPECT_EQ("// first\n"
  926. "// at start\n"
  927. "otherLine(); // comment",
  928. format("// first\n"
  929. " // at start\n"
  930. "otherLine(); // comment"));
  931. EXPECT_EQ("f();\n"
  932. "// first\n"
  933. "// at start\n"
  934. "otherLine(); // comment",
  935. format("f();\n"
  936. "// first\n"
  937. " // at start\n"
  938. "otherLine(); // comment"));
  939. verifyFormat("f(); // comment\n"
  940. "// first\n"
  941. "// at start\n"
  942. "otherLine();");
  943. EXPECT_EQ("f(); // comment\n"
  944. "// first\n"
  945. "// at start\n"
  946. "otherLine();",
  947. format("f(); // comment\n"
  948. "// first\n"
  949. " // at start\n"
  950. "otherLine();"));
  951. EXPECT_EQ("f(); // comment\n"
  952. " // first\n"
  953. "// at start\n"
  954. "otherLine();",
  955. format("f(); // comment\n"
  956. " // first\n"
  957. "// at start\n"
  958. "otherLine();"));
  959. EXPECT_EQ("void f() {\n"
  960. " lineWith(); // comment\n"
  961. " // at start\n"
  962. "}",
  963. format("void f() {\n"
  964. " lineWith(); // comment\n"
  965. " // at start\n"
  966. "}"));
  967. EXPECT_EQ("int xy; // a\n"
  968. "int z; // b",
  969. format("int xy; // a\n"
  970. "int z; //b"));
  971. EXPECT_EQ("int xy; // a\n"
  972. "int z; // bb",
  973. format("int xy; // a\n"
  974. "int z; //bb",
  975. getLLVMStyleWithColumns(12)));
  976. verifyFormat("#define A \\\n"
  977. " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
  978. " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
  979. getLLVMStyleWithColumns(60));
  980. verifyFormat(
  981. "#define A \\\n"
  982. " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
  983. " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
  984. getLLVMStyleWithColumns(61));
  985. verifyFormat("if ( // This is some comment\n"
  986. " x + 3) {\n"
  987. "}");
  988. EXPECT_EQ("if ( // This is some comment\n"
  989. " // spanning two lines\n"
  990. " x + 3) {\n"
  991. "}",
  992. format("if( // This is some comment\n"
  993. " // spanning two lines\n"
  994. " x + 3) {\n"
  995. "}"));
  996. verifyNoCrash("/\\\n/");
  997. verifyNoCrash("/\\\n* */");
  998. // The 0-character somehow makes the lexer return a proper comment.
  999. verifyNoCrash(StringRef("/*\\\0\n/", 6));
  1000. }
  1001. TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
  1002. EXPECT_EQ("SomeFunction(a,\n"
  1003. " b, // comment\n"
  1004. " c);",
  1005. format("SomeFunction(a,\n"
  1006. " b, // comment\n"
  1007. " c);"));
  1008. EXPECT_EQ("SomeFunction(a, b,\n"
  1009. " // comment\n"
  1010. " c);",
  1011. format("SomeFunction(a,\n"
  1012. " b,\n"
  1013. " // comment\n"
  1014. " c);"));
  1015. EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
  1016. " c);",
  1017. format("SomeFunction(a, b, // comment (unclear relation)\n"
  1018. " c);"));
  1019. EXPECT_EQ("SomeFunction(a, // comment\n"
  1020. " b,\n"
  1021. " c); // comment",
  1022. format("SomeFunction(a, // comment\n"
  1023. " b,\n"
  1024. " c); // comment"));
  1025. }
  1026. TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
  1027. EXPECT_EQ("// comment", format("// comment "));
  1028. EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
  1029. format("int aaaaaaa, bbbbbbb; // comment ",
  1030. getLLVMStyleWithColumns(33)));
  1031. EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f "));
  1032. EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f "));
  1033. }
  1034. TEST_F(FormatTest, UnderstandsBlockComments) {
  1035. verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
  1036. verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }");
  1037. EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
  1038. " bbbbbbbbbbbbbbbbbbbbbbbbb);",
  1039. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n"
  1040. "/* Trailing comment for aa... */\n"
  1041. " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  1042. EXPECT_EQ(
  1043. "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  1044. " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
  1045. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
  1046. "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  1047. EXPECT_EQ(
  1048. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1049. " aaaaaaaaaaaaaaaaaa,\n"
  1050. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  1051. "}",
  1052. format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1053. " aaaaaaaaaaaaaaaaaa ,\n"
  1054. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  1055. "}"));
  1056. FormatStyle NoBinPacking = getLLVMStyle();
  1057. NoBinPacking.BinPackParameters = false;
  1058. verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
  1059. " /* parameter 2 */ aaaaaa,\n"
  1060. " /* parameter 3 */ aaaaaa,\n"
  1061. " /* parameter 4 */ aaaaaa);",
  1062. NoBinPacking);
  1063. // Aligning block comments in macros.
  1064. verifyGoogleFormat("#define A \\\n"
  1065. " int i; /*a*/ \\\n"
  1066. " int jjj; /*b*/");
  1067. }
  1068. TEST_F(FormatTest, AlignsBlockComments) {
  1069. EXPECT_EQ("/*\n"
  1070. " * Really multi-line\n"
  1071. " * comment.\n"
  1072. " */\n"
  1073. "void f() {}",
  1074. format(" /*\n"
  1075. " * Really multi-line\n"
  1076. " * comment.\n"
  1077. " */\n"
  1078. " void f() {}"));
  1079. EXPECT_EQ("class C {\n"
  1080. " /*\n"
  1081. " * Another multi-line\n"
  1082. " * comment.\n"
  1083. " */\n"
  1084. " void f() {}\n"
  1085. "};",
  1086. format("class C {\n"
  1087. "/*\n"
  1088. " * Another multi-line\n"
  1089. " * comment.\n"
  1090. " */\n"
  1091. "void f() {}\n"
  1092. "};"));
  1093. EXPECT_EQ("/*\n"
  1094. " 1. This is a comment with non-trivial formatting.\n"
  1095. " 1.1. We have to indent/outdent all lines equally\n"
  1096. " 1.1.1. to keep the formatting.\n"
  1097. " */",
  1098. format(" /*\n"
  1099. " 1. This is a comment with non-trivial formatting.\n"
  1100. " 1.1. We have to indent/outdent all lines equally\n"
  1101. " 1.1.1. to keep the formatting.\n"
  1102. " */"));
  1103. EXPECT_EQ("/*\n"
  1104. "Don't try to outdent if there's not enough indentation.\n"
  1105. "*/",
  1106. format(" /*\n"
  1107. " Don't try to outdent if there's not enough indentation.\n"
  1108. " */"));
  1109. EXPECT_EQ("int i; /* Comment with empty...\n"
  1110. " *\n"
  1111. " * line. */",
  1112. format("int i; /* Comment with empty...\n"
  1113. " *\n"
  1114. " * line. */"));
  1115. EXPECT_EQ("int foobar = 0; /* comment */\n"
  1116. "int bar = 0; /* multiline\n"
  1117. " comment 1 */\n"
  1118. "int baz = 0; /* multiline\n"
  1119. " comment 2 */\n"
  1120. "int bzz = 0; /* multiline\n"
  1121. " comment 3 */",
  1122. format("int foobar = 0; /* comment */\n"
  1123. "int bar = 0; /* multiline\n"
  1124. " comment 1 */\n"
  1125. "int baz = 0; /* multiline\n"
  1126. " comment 2 */\n"
  1127. "int bzz = 0; /* multiline\n"
  1128. " comment 3 */"));
  1129. EXPECT_EQ("int foobar = 0; /* comment */\n"
  1130. "int bar = 0; /* multiline\n"
  1131. " comment */\n"
  1132. "int baz = 0; /* multiline\n"
  1133. "comment */",
  1134. format("int foobar = 0; /* comment */\n"
  1135. "int bar = 0; /* multiline\n"
  1136. "comment */\n"
  1137. "int baz = 0; /* multiline\n"
  1138. "comment */"));
  1139. }
  1140. TEST_F(FormatTest, CommentReflowingCanBeTurnedOff) {
  1141. FormatStyle Style = getLLVMStyleWithColumns(20);
  1142. Style.ReflowComments = false;
  1143. verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style);
  1144. verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style);
  1145. }
  1146. TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
  1147. EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1148. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
  1149. format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1150. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
  1151. EXPECT_EQ(
  1152. "void ffffffffffff(\n"
  1153. " int aaaaaaaa, int bbbbbbbb,\n"
  1154. " int cccccccccccc) { /*\n"
  1155. " aaaaaaaaaa\n"
  1156. " aaaaaaaaaaaaa\n"
  1157. " bbbbbbbbbbbbbb\n"
  1158. " bbbbbbbbbb\n"
  1159. " */\n"
  1160. "}",
  1161. format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
  1162. "{ /*\n"
  1163. " aaaaaaaaaa aaaaaaaaaaaaa\n"
  1164. " bbbbbbbbbbbbbb bbbbbbbbbb\n"
  1165. " */\n"
  1166. "}",
  1167. getLLVMStyleWithColumns(40)));
  1168. }
  1169. TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
  1170. EXPECT_EQ("void ffffffffff(\n"
  1171. " int aaaaa /* test */);",
  1172. format("void ffffffffff(int aaaaa /* test */);",
  1173. getLLVMStyleWithColumns(35)));
  1174. }
  1175. TEST_F(FormatTest, SplitsLongCxxComments) {
  1176. EXPECT_EQ("// A comment that\n"
  1177. "// doesn't fit on\n"
  1178. "// one line",
  1179. format("// A comment that doesn't fit on one line",
  1180. getLLVMStyleWithColumns(20)));
  1181. EXPECT_EQ("/// A comment that\n"
  1182. "/// doesn't fit on\n"
  1183. "/// one line",
  1184. format("/// A comment that doesn't fit on one line",
  1185. getLLVMStyleWithColumns(20)));
  1186. EXPECT_EQ("//! A comment that\n"
  1187. "//! doesn't fit on\n"
  1188. "//! one line",
  1189. format("//! A comment that doesn't fit on one line",
  1190. getLLVMStyleWithColumns(20)));
  1191. EXPECT_EQ("// a b c d\n"
  1192. "// e f g\n"
  1193. "// h i j k",
  1194. format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10)));
  1195. EXPECT_EQ(
  1196. "// a b c d\n"
  1197. "// e f g\n"
  1198. "// h i j k",
  1199. format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10)));
  1200. EXPECT_EQ("if (true) // A comment that\n"
  1201. " // doesn't fit on\n"
  1202. " // one line",
  1203. format("if (true) // A comment that doesn't fit on one line ",
  1204. getLLVMStyleWithColumns(30)));
  1205. EXPECT_EQ("// Don't_touch_leading_whitespace",
  1206. format("// Don't_touch_leading_whitespace",
  1207. getLLVMStyleWithColumns(20)));
  1208. EXPECT_EQ("// Add leading\n"
  1209. "// whitespace",
  1210. format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
  1211. EXPECT_EQ("/// Add leading\n"
  1212. "/// whitespace",
  1213. format("///Add leading whitespace", getLLVMStyleWithColumns(20)));
  1214. EXPECT_EQ("//! Add leading\n"
  1215. "//! whitespace",
  1216. format("//!Add leading whitespace", getLLVMStyleWithColumns(20)));
  1217. EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
  1218. EXPECT_EQ("// Even if it makes the line exceed the column\n"
  1219. "// limit",
  1220. format("//Even if it makes the line exceed the column limit",
  1221. getLLVMStyleWithColumns(51)));
  1222. EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
  1223. EXPECT_EQ("// aa bb cc dd",
  1224. format("// aa bb cc dd ",
  1225. getLLVMStyleWithColumns(15)));
  1226. EXPECT_EQ("// A comment before\n"
  1227. "// a macro\n"
  1228. "// definition\n"
  1229. "#define a b",
  1230. format("// A comment before a macro definition\n"
  1231. "#define a b",
  1232. getLLVMStyleWithColumns(20)));
  1233. EXPECT_EQ("void ffffff(\n"
  1234. " int aaaaaaaaa, // wwww\n"
  1235. " int bbbbbbbbbb, // xxxxxxx\n"
  1236. " // yyyyyyyyyy\n"
  1237. " int c, int d, int e) {}",
  1238. format("void ffffff(\n"
  1239. " int aaaaaaaaa, // wwww\n"
  1240. " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
  1241. " int c, int d, int e) {}",
  1242. getLLVMStyleWithColumns(40)));
  1243. EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1244. format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1245. getLLVMStyleWithColumns(20)));
  1246. EXPECT_EQ(
  1247. "#define XXX // a b c d\n"
  1248. " // e f g h",
  1249. format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
  1250. EXPECT_EQ(
  1251. "#define XXX // q w e r\n"
  1252. " // t y u i",
  1253. format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
  1254. }
  1255. TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {
  1256. EXPECT_EQ("// A comment\n"
  1257. "// that doesn't\n"
  1258. "// fit on one\n"
  1259. "// line",
  1260. format("// A comment that doesn't fit on one line",
  1261. getLLVMStyleWithColumns(20)));
  1262. EXPECT_EQ("/// A comment\n"
  1263. "/// that doesn't\n"
  1264. "/// fit on one\n"
  1265. "/// line",
  1266. format("/// A comment that doesn't fit on one line",
  1267. getLLVMStyleWithColumns(20)));
  1268. }
  1269. TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
  1270. EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1271. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1272. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1273. format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1274. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1275. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
  1276. EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1277. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1278. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1279. format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1280. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1281. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1282. getLLVMStyleWithColumns(50)));
  1283. // FIXME: One day we might want to implement adjustment of leading whitespace
  1284. // of the consecutive lines in this kind of comment:
  1285. EXPECT_EQ("double\n"
  1286. " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1287. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1288. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1289. format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1290. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1291. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1292. getLLVMStyleWithColumns(49)));
  1293. }
  1294. TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) {
  1295. FormatStyle Pragmas = getLLVMStyleWithColumns(30);
  1296. Pragmas.CommentPragmas = "^ IWYU pragma:";
  1297. EXPECT_EQ(
  1298. "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
  1299. format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
  1300. EXPECT_EQ(
  1301. "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
  1302. format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
  1303. }
  1304. TEST_F(FormatTest, PriorityOfCommentBreaking) {
  1305. EXPECT_EQ("if (xxx ==\n"
  1306. " yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  1307. " zzz)\n"
  1308. " q();",
  1309. format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  1310. " zzz) q();",
  1311. getLLVMStyleWithColumns(40)));
  1312. EXPECT_EQ("if (xxxxxxxxxx ==\n"
  1313. " yyy && // aaaaaa bbbbbbbb cccc\n"
  1314. " zzz)\n"
  1315. " q();",
  1316. format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
  1317. " zzz) q();",
  1318. getLLVMStyleWithColumns(40)));
  1319. EXPECT_EQ("if (xxxxxxxxxx &&\n"
  1320. " yyy || // aaaaaa bbbbbbbb cccc\n"
  1321. " zzz)\n"
  1322. " q();",
  1323. format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
  1324. " zzz) q();",
  1325. getLLVMStyleWithColumns(40)));
  1326. EXPECT_EQ("fffffffff(\n"
  1327. " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  1328. " zzz);",
  1329. format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  1330. " zzz);",
  1331. getLLVMStyleWithColumns(40)));
  1332. }
  1333. TEST_F(FormatTest, MultiLineCommentsInDefines) {
  1334. EXPECT_EQ("#define A(x) /* \\\n"
  1335. " a comment \\\n"
  1336. " inside */ \\\n"
  1337. " f();",
  1338. format("#define A(x) /* \\\n"
  1339. " a comment \\\n"
  1340. " inside */ \\\n"
  1341. " f();",
  1342. getLLVMStyleWithColumns(17)));
  1343. EXPECT_EQ("#define A( \\\n"
  1344. " x) /* \\\n"
  1345. " a comment \\\n"
  1346. " inside */ \\\n"
  1347. " f();",
  1348. format("#define A( \\\n"
  1349. " x) /* \\\n"
  1350. " a comment \\\n"
  1351. " inside */ \\\n"
  1352. " f();",
  1353. getLLVMStyleWithColumns(17)));
  1354. }
  1355. TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
  1356. EXPECT_EQ("namespace {}\n// Test\n#define A",
  1357. format("namespace {}\n // Test\n#define A"));
  1358. EXPECT_EQ("namespace {}\n/* Test */\n#define A",
  1359. format("namespace {}\n /* Test */\n#define A"));
  1360. EXPECT_EQ("namespace {}\n/* Test */ #define A",
  1361. format("namespace {}\n /* Test */ #define A"));
  1362. }
  1363. TEST_F(FormatTest, SplitsLongLinesInComments) {
  1364. EXPECT_EQ("/* This is a long\n"
  1365. " * comment that\n"
  1366. " * doesn't\n"
  1367. " * fit on one line.\n"
  1368. " */",
  1369. format("/* "
  1370. "This is a long "
  1371. "comment that "
  1372. "doesn't "
  1373. "fit on one line. */",
  1374. getLLVMStyleWithColumns(20)));
  1375. EXPECT_EQ(
  1376. "/* a b c d\n"
  1377. " * e f g\n"
  1378. " * h i j k\n"
  1379. " */",
  1380. format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
  1381. EXPECT_EQ(
  1382. "/* a b c d\n"
  1383. " * e f g\n"
  1384. " * h i j k\n"
  1385. " */",
  1386. format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
  1387. EXPECT_EQ("/*\n"
  1388. "This is a long\n"
  1389. "comment that doesn't\n"
  1390. "fit on one line.\n"
  1391. "*/",
  1392. format("/*\n"
  1393. "This is a long "
  1394. "comment that doesn't "
  1395. "fit on one line. \n"
  1396. "*/",
  1397. getLLVMStyleWithColumns(20)));
  1398. EXPECT_EQ("/*\n"
  1399. " * This is a long\n"
  1400. " * comment that\n"
  1401. " * doesn't fit on\n"
  1402. " * one line.\n"
  1403. " */",
  1404. format("/* \n"
  1405. " * This is a long "
  1406. " comment that "
  1407. " doesn't fit on "
  1408. " one line. \n"
  1409. " */",
  1410. getLLVMStyleWithColumns(20)));
  1411. EXPECT_EQ("/*\n"
  1412. " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
  1413. " * so_it_should_be_broken\n"
  1414. " * wherever_a_space_occurs\n"
  1415. " */",
  1416. format("/*\n"
  1417. " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
  1418. " so_it_should_be_broken "
  1419. " wherever_a_space_occurs \n"
  1420. " */",
  1421. getLLVMStyleWithColumns(20)));
  1422. EXPECT_EQ("/*\n"
  1423. " * This_comment_can_not_be_broken_into_lines\n"
  1424. " */",
  1425. format("/*\n"
  1426. " * This_comment_can_not_be_broken_into_lines\n"
  1427. " */",
  1428. getLLVMStyleWithColumns(20)));
  1429. EXPECT_EQ("{\n"
  1430. " /*\n"
  1431. " This is another\n"
  1432. " long comment that\n"
  1433. " doesn't fit on one\n"
  1434. " line 1234567890\n"
  1435. " */\n"
  1436. "}",
  1437. format("{\n"
  1438. "/*\n"
  1439. "This is another "
  1440. " long comment that "
  1441. " doesn't fit on one"
  1442. " line 1234567890\n"
  1443. "*/\n"
  1444. "}",
  1445. getLLVMStyleWithColumns(20)));
  1446. EXPECT_EQ("{\n"
  1447. " /*\n"
  1448. " * This i s\n"
  1449. " * another comment\n"
  1450. " * t hat doesn' t\n"
  1451. " * fit on one l i\n"
  1452. " * n e\n"
  1453. " */\n"
  1454. "}",
  1455. format("{\n"
  1456. "/*\n"
  1457. " * This i s"
  1458. " another comment"
  1459. " t hat doesn' t"
  1460. " fit on one l i"
  1461. " n e\n"
  1462. " */\n"
  1463. "}",
  1464. getLLVMStyleWithColumns(20)));
  1465. EXPECT_EQ("/*\n"
  1466. " * This is a long\n"
  1467. " * comment that\n"
  1468. " * doesn't fit on\n"
  1469. " * one line\n"
  1470. " */",
  1471. format(" /*\n"
  1472. " * This is a long comment that doesn't fit on one line\n"
  1473. " */",
  1474. getLLVMStyleWithColumns(20)));
  1475. EXPECT_EQ("{\n"
  1476. " if (something) /* This is a\n"
  1477. " long\n"
  1478. " comment */\n"
  1479. " ;\n"
  1480. "}",
  1481. format("{\n"
  1482. " if (something) /* This is a long comment */\n"
  1483. " ;\n"
  1484. "}",
  1485. getLLVMStyleWithColumns(30)));
  1486. EXPECT_EQ("/* A comment before\n"
  1487. " * a macro\n"
  1488. " * definition */\n"
  1489. "#define a b",
  1490. format("/* A comment before a macro definition */\n"
  1491. "#define a b",
  1492. getLLVMStyleWithColumns(20)));
  1493. EXPECT_EQ("/* some comment\n"
  1494. " * a comment\n"
  1495. "* that we break\n"
  1496. " * another comment\n"
  1497. "* we have to break\n"
  1498. "* a left comment\n"
  1499. " */",
  1500. format(" /* some comment\n"
  1501. " * a comment that we break\n"
  1502. " * another comment we have to break\n"
  1503. "* a left comment\n"
  1504. " */",
  1505. getLLVMStyleWithColumns(20)));
  1506. EXPECT_EQ("/**\n"
  1507. " * multiline block\n"
  1508. " * comment\n"
  1509. " *\n"
  1510. " */",
  1511. format("/**\n"
  1512. " * multiline block comment\n"
  1513. " *\n"
  1514. " */",
  1515. getLLVMStyleWithColumns(20)));
  1516. EXPECT_EQ("/*\n"
  1517. "\n"
  1518. "\n"
  1519. " */\n",
  1520. format(" /* \n"
  1521. " \n"
  1522. " \n"
  1523. " */\n"));
  1524. EXPECT_EQ("/* a a */",
  1525. format("/* a a */", getLLVMStyleWithColumns(15)));
  1526. EXPECT_EQ("/* a a bc */",
  1527. format("/* a a bc */", getLLVMStyleWithColumns(15)));
  1528. EXPECT_EQ("/* aaa aaa\n"
  1529. " * aaaaa */",
  1530. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1531. EXPECT_EQ("/* aaa aaa\n"
  1532. " * aaaaa */",
  1533. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1534. }
  1535. TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
  1536. EXPECT_EQ("#define X \\\n"
  1537. " /* \\\n"
  1538. " Test \\\n"
  1539. " Macro comment \\\n"
  1540. " with a long \\\n"
  1541. " line \\\n"
  1542. " */ \\\n"
  1543. " A + B",
  1544. format("#define X \\\n"
  1545. " /*\n"
  1546. " Test\n"
  1547. " Macro comment with a long line\n"
  1548. " */ \\\n"
  1549. " A + B",
  1550. getLLVMStyleWithColumns(20)));
  1551. EXPECT_EQ("#define X \\\n"
  1552. " /* Macro comment \\\n"
  1553. " with a long \\\n"
  1554. " line */ \\\n"
  1555. " A + B",
  1556. format("#define X \\\n"
  1557. " /* Macro comment with a long\n"
  1558. " line */ \\\n"
  1559. " A + B",
  1560. getLLVMStyleWithColumns(20)));
  1561. EXPECT_EQ("#define X \\\n"
  1562. " /* Macro comment \\\n"
  1563. " * with a long \\\n"
  1564. " * line */ \\\n"
  1565. " A + B",
  1566. format("#define X \\\n"
  1567. " /* Macro comment with a long line */ \\\n"
  1568. " A + B",
  1569. getLLVMStyleWithColumns(20)));
  1570. }
  1571. TEST_F(FormatTest, CommentsInStaticInitializers) {
  1572. EXPECT_EQ(
  1573. "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
  1574. " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
  1575. " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
  1576. " aaaaaaaaaaaaaaaaaaaa, // comment\n"
  1577. " aaaaaaaaaaaaaaaaaaaa};",
  1578. format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
  1579. " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
  1580. " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
  1581. " aaaaaaaaaaaaaaaaaaaa , // comment\n"
  1582. " aaaaaaaaaaaaaaaaaaaa };"));
  1583. verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1584. " bbbbbbbbbbb, ccccccccccc};");
  1585. verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1586. " // comment for bb....\n"
  1587. " bbbbbbbbbbb, ccccccccccc};");
  1588. verifyGoogleFormat(
  1589. "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1590. " bbbbbbbbbbb, ccccccccccc};");
  1591. verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1592. " // comment for bb....\n"
  1593. " bbbbbbbbbbb, ccccccccccc};");
  1594. verifyFormat("S s = {{a, b, c}, // Group #1\n"
  1595. " {d, e, f}, // Group #2\n"
  1596. " {g, h, i}}; // Group #3");
  1597. verifyFormat("S s = {{// Group #1\n"
  1598. " a, b, c},\n"
  1599. " {// Group #2\n"
  1600. " d, e, f},\n"
  1601. " {// Group #3\n"
  1602. " g, h, i}};");
  1603. EXPECT_EQ("S s = {\n"
  1604. " // Some comment\n"
  1605. " a,\n"
  1606. "\n"
  1607. " // Comment after empty line\n"
  1608. " b}",
  1609. format("S s = {\n"
  1610. " // Some comment\n"
  1611. " a,\n"
  1612. " \n"
  1613. " // Comment after empty line\n"
  1614. " b\n"
  1615. "}"));
  1616. EXPECT_EQ("S s = {\n"
  1617. " /* Some comment */\n"
  1618. " a,\n"
  1619. "\n"
  1620. " /* Comment after empty line */\n"
  1621. " b}",
  1622. format("S s = {\n"
  1623. " /* Some comment */\n"
  1624. " a,\n"
  1625. " \n"
  1626. " /* Comment after empty line */\n"
  1627. " b\n"
  1628. "}"));
  1629. verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
  1630. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1631. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1632. " 0x00, 0x00, 0x00, 0x00}; // comment\n");
  1633. }
  1634. TEST_F(FormatTest, IgnoresIf0Contents) {
  1635. EXPECT_EQ("#if 0\n"
  1636. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1637. "#endif\n"
  1638. "void f() {}",
  1639. format("#if 0\n"
  1640. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1641. "#endif\n"
  1642. "void f( ) { }"));
  1643. EXPECT_EQ("#if false\n"
  1644. "void f( ) { }\n"
  1645. "#endif\n"
  1646. "void g() {}\n",
  1647. format("#if false\n"
  1648. "void f( ) { }\n"
  1649. "#endif\n"
  1650. "void g( ) { }\n"));
  1651. EXPECT_EQ("enum E {\n"
  1652. " One,\n"
  1653. " Two,\n"
  1654. "#if 0\n"
  1655. "Three,\n"
  1656. " Four,\n"
  1657. "#endif\n"
  1658. " Five\n"
  1659. "};",
  1660. format("enum E {\n"
  1661. " One,Two,\n"
  1662. "#if 0\n"
  1663. "Three,\n"
  1664. " Four,\n"
  1665. "#endif\n"
  1666. " Five};"));
  1667. EXPECT_EQ("enum F {\n"
  1668. " One,\n"
  1669. "#if 1\n"
  1670. " Two,\n"
  1671. "#if 0\n"
  1672. "Three,\n"
  1673. " Four,\n"
  1674. "#endif\n"
  1675. " Five\n"
  1676. "#endif\n"
  1677. "};",
  1678. format("enum F {\n"
  1679. "One,\n"
  1680. "#if 1\n"
  1681. "Two,\n"
  1682. "#if 0\n"
  1683. "Three,\n"
  1684. " Four,\n"
  1685. "#endif\n"
  1686. "Five\n"
  1687. "#endif\n"
  1688. "};"));
  1689. EXPECT_EQ("enum G {\n"
  1690. " One,\n"
  1691. "#if 0\n"
  1692. "Two,\n"
  1693. "#else\n"
  1694. " Three,\n"
  1695. "#endif\n"
  1696. " Four\n"
  1697. "};",
  1698. format("enum G {\n"
  1699. "One,\n"
  1700. "#if 0\n"
  1701. "Two,\n"
  1702. "#else\n"
  1703. "Three,\n"
  1704. "#endif\n"
  1705. "Four\n"
  1706. "};"));
  1707. EXPECT_EQ("enum H {\n"
  1708. " One,\n"
  1709. "#if 0\n"
  1710. "#ifdef Q\n"
  1711. "Two,\n"
  1712. "#else\n"
  1713. "Three,\n"
  1714. "#endif\n"
  1715. "#endif\n"
  1716. " Four\n"
  1717. "};",
  1718. format("enum H {\n"
  1719. "One,\n"
  1720. "#if 0\n"
  1721. "#ifdef Q\n"
  1722. "Two,\n"
  1723. "#else\n"
  1724. "Three,\n"
  1725. "#endif\n"
  1726. "#endif\n"
  1727. "Four\n"
  1728. "};"));
  1729. EXPECT_EQ("enum I {\n"
  1730. " One,\n"
  1731. "#if /* test */ 0 || 1\n"
  1732. "Two,\n"
  1733. "Three,\n"
  1734. "#endif\n"
  1735. " Four\n"
  1736. "};",
  1737. format("enum I {\n"
  1738. "One,\n"
  1739. "#if /* test */ 0 || 1\n"
  1740. "Two,\n"
  1741. "Three,\n"
  1742. "#endif\n"
  1743. "Four\n"
  1744. "};"));
  1745. EXPECT_EQ("enum J {\n"
  1746. " One,\n"
  1747. "#if 0\n"
  1748. "#if 0\n"
  1749. "Two,\n"
  1750. "#else\n"
  1751. "Three,\n"
  1752. "#endif\n"
  1753. "Four,\n"
  1754. "#endif\n"
  1755. " Five\n"
  1756. "};",
  1757. format("enum J {\n"
  1758. "One,\n"
  1759. "#if 0\n"
  1760. "#if 0\n"
  1761. "Two,\n"
  1762. "#else\n"
  1763. "Three,\n"
  1764. "#endif\n"
  1765. "Four,\n"
  1766. "#endif\n"
  1767. "Five\n"
  1768. "};"));
  1769. }
  1770. //===----------------------------------------------------------------------===//
  1771. // Tests for classes, namespaces, etc.
  1772. //===----------------------------------------------------------------------===//
  1773. TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
  1774. verifyFormat("class A {};");
  1775. }
  1776. TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
  1777. verifyFormat("class A {\n"
  1778. "public:\n"
  1779. "public: // comment\n"
  1780. "protected:\n"
  1781. "private:\n"
  1782. " void f() {}\n"
  1783. "};");
  1784. verifyGoogleFormat("class A {\n"
  1785. " public:\n"
  1786. " protected:\n"
  1787. " private:\n"
  1788. " void f() {}\n"
  1789. "};");
  1790. verifyFormat("class A {\n"
  1791. "public slots:\n"
  1792. " void f1() {}\n"
  1793. "public Q_SLOTS:\n"
  1794. " void f2() {}\n"
  1795. "protected slots:\n"
  1796. " void f3() {}\n"
  1797. "protected Q_SLOTS:\n"
  1798. " void f4() {}\n"
  1799. "private slots:\n"
  1800. " void f5() {}\n"
  1801. "private Q_SLOTS:\n"
  1802. " void f6() {}\n"
  1803. "signals:\n"
  1804. " void g1();\n"
  1805. "Q_SIGNALS:\n"
  1806. " void g2();\n"
  1807. "};");
  1808. // Don't interpret 'signals' the wrong way.
  1809. verifyFormat("signals.set();");
  1810. verifyFormat("for (Signals signals : f()) {\n}");
  1811. verifyFormat("{\n"
  1812. " signals.set(); // This needs indentation.\n"
  1813. "}");
  1814. }
  1815. TEST_F(FormatTest, SeparatesLogicalBlocks) {
  1816. EXPECT_EQ("class A {\n"
  1817. "public:\n"
  1818. " void f();\n"
  1819. "\n"
  1820. "private:\n"
  1821. " void g() {}\n"
  1822. " // test\n"
  1823. "protected:\n"
  1824. " int h;\n"
  1825. "};",
  1826. format("class A {\n"
  1827. "public:\n"
  1828. "void f();\n"
  1829. "private:\n"
  1830. "void g() {}\n"
  1831. "// test\n"
  1832. "protected:\n"
  1833. "int h;\n"
  1834. "};"));
  1835. EXPECT_EQ("class A {\n"
  1836. "protected:\n"
  1837. "public:\n"
  1838. " void f();\n"
  1839. "};",
  1840. format("class A {\n"
  1841. "protected:\n"
  1842. "\n"
  1843. "public:\n"
  1844. "\n"
  1845. " void f();\n"
  1846. "};"));
  1847. // Even ensure proper spacing inside macros.
  1848. EXPECT_EQ("#define B \\\n"
  1849. " class A { \\\n"
  1850. " protected: \\\n"
  1851. " public: \\\n"
  1852. " void f(); \\\n"
  1853. " };",
  1854. format("#define B \\\n"
  1855. " class A { \\\n"
  1856. " protected: \\\n"
  1857. " \\\n"
  1858. " public: \\\n"
  1859. " \\\n"
  1860. " void f(); \\\n"
  1861. " };",
  1862. getGoogleStyle()));
  1863. // But don't remove empty lines after macros ending in access specifiers.
  1864. EXPECT_EQ("#define A private:\n"
  1865. "\n"
  1866. "int i;",
  1867. format("#define A private:\n"
  1868. "\n"
  1869. "int i;"));
  1870. }
  1871. TEST_F(FormatTest, FormatsClasses) {
  1872. verifyFormat("class A : public B {};");
  1873. verifyFormat("class A : public ::B {};");
  1874. verifyFormat(
  1875. "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1876. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1877. verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
  1878. " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1879. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1880. verifyFormat(
  1881. "class A : public B, public C, public D, public E, public F {};");
  1882. verifyFormat("class AAAAAAAAAAAA : public B,\n"
  1883. " public C,\n"
  1884. " public D,\n"
  1885. " public E,\n"
  1886. " public F,\n"
  1887. " public G {};");
  1888. verifyFormat("class\n"
  1889. " ReallyReallyLongClassName {\n"
  1890. " int i;\n"
  1891. "};",
  1892. getLLVMStyleWithColumns(32));
  1893. verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
  1894. " aaaaaaaaaaaaaaaa> {};");
  1895. verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
  1896. " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
  1897. " aaaaaaaaaaaaaaaaaaaaaa> {};");
  1898. verifyFormat("template <class R, class C>\n"
  1899. "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
  1900. " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
  1901. verifyFormat("class ::A::B {};");
  1902. }
  1903. TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
  1904. verifyFormat("class A {\n} a, b;");
  1905. verifyFormat("struct A {\n} a, b;");
  1906. verifyFormat("union A {\n} a;");
  1907. }
  1908. TEST_F(FormatTest, FormatsEnum) {
  1909. verifyFormat("enum {\n"
  1910. " Zero,\n"
  1911. " One = 1,\n"
  1912. " Two = One + 1,\n"
  1913. " Three = (One + Two),\n"
  1914. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1915. " Five = (One, Two, Three, Four, 5)\n"
  1916. "};");
  1917. verifyGoogleFormat("enum {\n"
  1918. " Zero,\n"
  1919. " One = 1,\n"
  1920. " Two = One + 1,\n"
  1921. " Three = (One + Two),\n"
  1922. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1923. " Five = (One, Two, Three, Four, 5)\n"
  1924. "};");
  1925. verifyFormat("enum Enum {};");
  1926. verifyFormat("enum {};");
  1927. verifyFormat("enum X E {} d;");
  1928. verifyFormat("enum __attribute__((...)) E {} d;");
  1929. verifyFormat("enum __declspec__((...)) E {} d;");
  1930. verifyFormat("enum {\n"
  1931. " Bar = Foo<int, int>::value\n"
  1932. "};",
  1933. getLLVMStyleWithColumns(30));
  1934. verifyFormat("enum ShortEnum { A, B, C };");
  1935. verifyGoogleFormat("enum ShortEnum { A, B, C };");
  1936. EXPECT_EQ("enum KeepEmptyLines {\n"
  1937. " ONE,\n"
  1938. "\n"
  1939. " TWO,\n"
  1940. "\n"
  1941. " THREE\n"
  1942. "}",
  1943. format("enum KeepEmptyLines {\n"
  1944. " ONE,\n"
  1945. "\n"
  1946. " TWO,\n"
  1947. "\n"
  1948. "\n"
  1949. " THREE\n"
  1950. "}"));
  1951. verifyFormat("enum E { // comment\n"
  1952. " ONE,\n"
  1953. " TWO\n"
  1954. "};\n"
  1955. "int i;");
  1956. // Not enums.
  1957. verifyFormat("enum X f() {\n"
  1958. " a();\n"
  1959. " return 42;\n"
  1960. "}");
  1961. verifyFormat("enum X Type::f() {\n"
  1962. " a();\n"
  1963. " return 42;\n"
  1964. "}");
  1965. verifyFormat("enum ::X f() {\n"
  1966. " a();\n"
  1967. " return 42;\n"
  1968. "}");
  1969. verifyFormat("enum ns::X f() {\n"
  1970. " a();\n"
  1971. " return 42;\n"
  1972. "}");
  1973. }
  1974. TEST_F(FormatTest, FormatsEnumsWithErrors) {
  1975. verifyFormat("enum Type {\n"
  1976. " One = 0; // These semicolons should be commas.\n"
  1977. " Two = 1;\n"
  1978. "};");
  1979. verifyFormat("namespace n {\n"
  1980. "enum Type {\n"
  1981. " One,\n"
  1982. " Two, // missing };\n"
  1983. " int i;\n"
  1984. "}\n"
  1985. "void g() {}");
  1986. }
  1987. TEST_F(FormatTest, FormatsEnumStruct) {
  1988. verifyFormat("enum struct {\n"
  1989. " Zero,\n"
  1990. " One = 1,\n"
  1991. " Two = One + 1,\n"
  1992. " Three = (One + Two),\n"
  1993. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1994. " Five = (One, Two, Three, Four, 5)\n"
  1995. "};");
  1996. verifyFormat("enum struct Enum {};");
  1997. verifyFormat("enum struct {};");
  1998. verifyFormat("enum struct X E {} d;");
  1999. verifyFormat("enum struct __attribute__((...)) E {} d;");
  2000. verifyFormat("enum struct __declspec__((...)) E {} d;");
  2001. verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
  2002. }
  2003. TEST_F(FormatTest, FormatsEnumClass) {
  2004. verifyFormat("enum class {\n"
  2005. " Zero,\n"
  2006. " One = 1,\n"
  2007. " Two = One + 1,\n"
  2008. " Three = (One + Two),\n"
  2009. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  2010. " Five = (One, Two, Three, Four, 5)\n"
  2011. "};");
  2012. verifyFormat("enum class Enum {};");
  2013. verifyFormat("enum class {};");
  2014. verifyFormat("enum class X E {} d;");
  2015. verifyFormat("enum class __attribute__((...)) E {} d;");
  2016. verifyFormat("enum class __declspec__((...)) E {} d;");
  2017. verifyFormat("enum class X f() {\n a();\n return 42;\n}");
  2018. }
  2019. TEST_F(FormatTest, FormatsEnumTypes) {
  2020. verifyFormat("enum X : int {\n"
  2021. " A, // Force multiple lines.\n"
  2022. " B\n"
  2023. "};");
  2024. verifyFormat("enum X : int { A, B };");
  2025. verifyFormat("enum X : std::uint32_t { A, B };");
  2026. }
  2027. TEST_F(FormatTest, FormatsNSEnums) {
  2028. verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
  2029. verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
  2030. " // Information about someDecentlyLongValue.\n"
  2031. " someDecentlyLongValue,\n"
  2032. " // Information about anotherDecentlyLongValue.\n"
  2033. " anotherDecentlyLongValue,\n"
  2034. " // Information about aThirdDecentlyLongValue.\n"
  2035. " aThirdDecentlyLongValue\n"
  2036. "};");
  2037. verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
  2038. " a = 1,\n"
  2039. " b = 2,\n"
  2040. " c = 3,\n"
  2041. "};");
  2042. verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
  2043. " a = 1,\n"
  2044. " b = 2,\n"
  2045. " c = 3,\n"
  2046. "};");
  2047. verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
  2048. " a = 1,\n"
  2049. " b = 2,\n"
  2050. " c = 3,\n"
  2051. "};");
  2052. }
  2053. TEST_F(FormatTest, FormatsBitfields) {
  2054. verifyFormat("struct Bitfields {\n"
  2055. " unsigned sClass : 8;\n"
  2056. " unsigned ValueKind : 2;\n"
  2057. "};");
  2058. verifyFormat("struct A {\n"
  2059. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
  2060. " bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
  2061. "};");
  2062. verifyFormat("struct MyStruct {\n"
  2063. " uchar data;\n"
  2064. " uchar : 8;\n"
  2065. " uchar : 8;\n"
  2066. " uchar other;\n"
  2067. "};");
  2068. }
  2069. TEST_F(FormatTest, FormatsNamespaces) {
  2070. verifyFormat("namespace some_namespace {\n"
  2071. "class A {};\n"
  2072. "void f() { f(); }\n"
  2073. "}");
  2074. verifyFormat("namespace {\n"
  2075. "class A {};\n"
  2076. "void f() { f(); }\n"
  2077. "}");
  2078. verifyFormat("inline namespace X {\n"
  2079. "class A {};\n"
  2080. "void f() { f(); }\n"
  2081. "}");
  2082. verifyFormat("using namespace some_namespace;\n"
  2083. "class A {};\n"
  2084. "void f() { f(); }");
  2085. // This code is more common than we thought; if we
  2086. // layout this correctly the semicolon will go into
  2087. // its own line, which is undesirable.
  2088. verifyFormat("namespace {};");
  2089. verifyFormat("namespace {\n"
  2090. "class A {};\n"
  2091. "};");
  2092. verifyFormat("namespace {\n"
  2093. "int SomeVariable = 0; // comment\n"
  2094. "} // namespace");
  2095. EXPECT_EQ("#ifndef HEADER_GUARD\n"
  2096. "#define HEADER_GUARD\n"
  2097. "namespace my_namespace {\n"
  2098. "int i;\n"
  2099. "} // my_namespace\n"
  2100. "#endif // HEADER_GUARD",
  2101. format("#ifndef HEADER_GUARD\n"
  2102. " #define HEADER_GUARD\n"
  2103. " namespace my_namespace {\n"
  2104. "int i;\n"
  2105. "} // my_namespace\n"
  2106. "#endif // HEADER_GUARD"));
  2107. EXPECT_EQ("namespace A::B {\n"
  2108. "class C {};\n"
  2109. "}",
  2110. format("namespace A::B {\n"
  2111. "class C {};\n"
  2112. "}"));
  2113. FormatStyle Style = getLLVMStyle();
  2114. Style.NamespaceIndentation = FormatStyle::NI_All;
  2115. EXPECT_EQ("namespace out {\n"
  2116. " int i;\n"
  2117. " namespace in {\n"
  2118. " int i;\n"
  2119. " } // namespace\n"
  2120. "} // namespace",
  2121. format("namespace out {\n"
  2122. "int i;\n"
  2123. "namespace in {\n"
  2124. "int i;\n"
  2125. "} // namespace\n"
  2126. "} // namespace",
  2127. Style));
  2128. Style.NamespaceIndentation = FormatStyle::NI_Inner;
  2129. EXPECT_EQ("namespace out {\n"
  2130. "int i;\n"
  2131. "namespace in {\n"
  2132. " int i;\n"
  2133. "} // namespace\n"
  2134. "} // namespace",
  2135. format("namespace out {\n"
  2136. "int i;\n"
  2137. "namespace in {\n"
  2138. "int i;\n"
  2139. "} // namespace\n"
  2140. "} // namespace",
  2141. Style));
  2142. }
  2143. TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
  2144. TEST_F(FormatTest, FormatsInlineASM) {
  2145. verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
  2146. verifyFormat("asm(\"nop\" ::: \"memory\");");
  2147. verifyFormat(
  2148. "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
  2149. " \"cpuid\\n\\t\"\n"
  2150. " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
  2151. " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
  2152. " : \"a\"(value));");
  2153. EXPECT_EQ(
  2154. "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
  2155. " __asm {\n"
  2156. " mov edx,[that] // vtable in edx\n"
  2157. " mov eax,methodIndex\n"
  2158. " call [edx][eax*4] // stdcall\n"
  2159. " }\n"
  2160. "}",
  2161. format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
  2162. " __asm {\n"
  2163. " mov edx,[that] // vtable in edx\n"
  2164. " mov eax,methodIndex\n"
  2165. " call [edx][eax*4] // stdcall\n"
  2166. " }\n"
  2167. "}"));
  2168. EXPECT_EQ("_asm {\n"
  2169. " xor eax, eax;\n"
  2170. " cpuid;\n"
  2171. "}",
  2172. format("_asm {\n"
  2173. " xor eax, eax;\n"
  2174. " cpuid;\n"
  2175. "}"));
  2176. verifyFormat("void function() {\n"
  2177. " // comment\n"
  2178. " asm(\"\");\n"
  2179. "}");
  2180. EXPECT_EQ("__asm {\n"
  2181. "}\n"
  2182. "int i;",
  2183. format("__asm {\n"
  2184. "}\n"
  2185. "int i;"));
  2186. }
  2187. TEST_F(FormatTest, FormatTryCatch) {
  2188. verifyFormat("try {\n"
  2189. " throw a * b;\n"
  2190. "} catch (int a) {\n"
  2191. " // Do nothing.\n"
  2192. "} catch (...) {\n"
  2193. " exit(42);\n"
  2194. "}");
  2195. // Function-level try statements.
  2196. verifyFormat("int f() try { return 4; } catch (...) {\n"
  2197. " return 5;\n"
  2198. "}");
  2199. verifyFormat("class A {\n"
  2200. " int a;\n"
  2201. " A() try : a(0) {\n"
  2202. " } catch (...) {\n"
  2203. " throw;\n"
  2204. " }\n"
  2205. "};\n");
  2206. // Incomplete try-catch blocks.
  2207. verifyIncompleteFormat("try {} catch (");
  2208. }
  2209. TEST_F(FormatTest, FormatSEHTryCatch) {
  2210. verifyFormat("__try {\n"
  2211. " int a = b * c;\n"
  2212. "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
  2213. " // Do nothing.\n"
  2214. "}");
  2215. verifyFormat("__try {\n"
  2216. " int a = b * c;\n"
  2217. "} __finally {\n"
  2218. " // Do nothing.\n"
  2219. "}");
  2220. verifyFormat("DEBUG({\n"
  2221. " __try {\n"
  2222. " } __finally {\n"
  2223. " }\n"
  2224. "});\n");
  2225. }
  2226. TEST_F(FormatTest, IncompleteTryCatchBlocks) {
  2227. verifyFormat("try {\n"
  2228. " f();\n"
  2229. "} catch {\n"
  2230. " g();\n"
  2231. "}");
  2232. verifyFormat("try {\n"
  2233. " f();\n"
  2234. "} catch (A a) MACRO(x) {\n"
  2235. " g();\n"
  2236. "} catch (B b) MACRO(x) {\n"
  2237. " g();\n"
  2238. "}");
  2239. }
  2240. TEST_F(FormatTest, FormatTryCatchBraceStyles) {
  2241. FormatStyle Style = getLLVMStyle();
  2242. for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
  2243. FormatStyle::BS_WebKit}) {
  2244. Style.BreakBeforeBraces = BraceStyle;
  2245. verifyFormat("try {\n"
  2246. " // something\n"
  2247. "} catch (...) {\n"
  2248. " // something\n"
  2249. "}",
  2250. Style);
  2251. }
  2252. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  2253. verifyFormat("try {\n"
  2254. " // something\n"
  2255. "}\n"
  2256. "catch (...) {\n"
  2257. " // something\n"
  2258. "}",
  2259. Style);
  2260. verifyFormat("__try {\n"
  2261. " // something\n"
  2262. "}\n"
  2263. "__finally {\n"
  2264. " // something\n"
  2265. "}",
  2266. Style);
  2267. verifyFormat("@try {\n"
  2268. " // something\n"
  2269. "}\n"
  2270. "@finally {\n"
  2271. " // something\n"
  2272. "}",
  2273. Style);
  2274. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  2275. verifyFormat("try\n"
  2276. "{\n"
  2277. " // something\n"
  2278. "}\n"
  2279. "catch (...)\n"
  2280. "{\n"
  2281. " // something\n"
  2282. "}",
  2283. Style);
  2284. Style.BreakBeforeBraces = FormatStyle::BS_GNU;
  2285. verifyFormat("try\n"
  2286. " {\n"
  2287. " // something\n"
  2288. " }\n"
  2289. "catch (...)\n"
  2290. " {\n"
  2291. " // something\n"
  2292. " }",
  2293. Style);
  2294. Style.BreakBeforeBraces = FormatStyle::BS_Custom;
  2295. Style.BraceWrapping.BeforeCatch = true;
  2296. verifyFormat("try {\n"
  2297. " // something\n"
  2298. "}\n"
  2299. "catch (...) {\n"
  2300. " // something\n"
  2301. "}",
  2302. Style);
  2303. }
  2304. TEST_F(FormatTest, FormatObjCTryCatch) {
  2305. verifyFormat("@try {\n"
  2306. " f();\n"
  2307. "} @catch (NSException e) {\n"
  2308. " @throw;\n"
  2309. "} @finally {\n"
  2310. " exit(42);\n"
  2311. "}");
  2312. verifyFormat("DEBUG({\n"
  2313. " @try {\n"
  2314. " } @finally {\n"
  2315. " }\n"
  2316. "});\n");
  2317. }
  2318. TEST_F(FormatTest, FormatObjCAutoreleasepool) {
  2319. FormatStyle Style = getLLVMStyle();
  2320. verifyFormat("@autoreleasepool {\n"
  2321. " f();\n"
  2322. "}\n"
  2323. "@autoreleasepool {\n"
  2324. " f();\n"
  2325. "}\n",
  2326. Style);
  2327. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  2328. verifyFormat("@autoreleasepool\n"
  2329. "{\n"
  2330. " f();\n"
  2331. "}\n"
  2332. "@autoreleasepool\n"
  2333. "{\n"
  2334. " f();\n"
  2335. "}\n",
  2336. Style);
  2337. }
  2338. TEST_F(FormatTest, StaticInitializers) {
  2339. verifyFormat("static SomeClass SC = {1, 'a'};");
  2340. verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
  2341. " 100000000, "
  2342. "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
  2343. // Here, everything other than the "}" would fit on a line.
  2344. verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
  2345. " 10000000000000000000000000};");
  2346. EXPECT_EQ("S s = {a,\n"
  2347. "\n"
  2348. " b};",
  2349. format("S s = {\n"
  2350. " a,\n"
  2351. "\n"
  2352. " b\n"
  2353. "};"));
  2354. // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
  2355. // line. However, the formatting looks a bit off and this probably doesn't
  2356. // happen often in practice.
  2357. verifyFormat("static int Variable[1] = {\n"
  2358. " {1000000000000000000000000000000000000}};",
  2359. getLLVMStyleWithColumns(40));
  2360. }
  2361. TEST_F(FormatTest, DesignatedInitializers) {
  2362. verifyFormat("const struct A a = {.a = 1, .b = 2};");
  2363. verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
  2364. " .bbbbbbbbbb = 2,\n"
  2365. " .cccccccccc = 3,\n"
  2366. " .dddddddddd = 4,\n"
  2367. " .eeeeeeeeee = 5};");
  2368. verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
  2369. " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
  2370. " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
  2371. " .ccccccccccccccccccccccccccc = 3,\n"
  2372. " .ddddddddddddddddddddddddddd = 4,\n"
  2373. " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
  2374. verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
  2375. }
  2376. TEST_F(FormatTest, NestedStaticInitializers) {
  2377. verifyFormat("static A x = {{{}}};\n");
  2378. verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
  2379. " {init1, init2, init3, init4}}};",
  2380. getLLVMStyleWithColumns(50));
  2381. verifyFormat("somes Status::global_reps[3] = {\n"
  2382. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  2383. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  2384. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
  2385. getLLVMStyleWithColumns(60));
  2386. verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
  2387. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  2388. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  2389. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
  2390. verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
  2391. " {rect.fRight - rect.fLeft, rect.fBottom - "
  2392. "rect.fTop}};");
  2393. verifyFormat(
  2394. "SomeArrayOfSomeType a = {\n"
  2395. " {{1, 2, 3},\n"
  2396. " {1, 2, 3},\n"
  2397. " {111111111111111111111111111111, 222222222222222222222222222222,\n"
  2398. " 333333333333333333333333333333},\n"
  2399. " {1, 2, 3},\n"
  2400. " {1, 2, 3}}};");
  2401. verifyFormat(
  2402. "SomeArrayOfSomeType a = {\n"
  2403. " {{1, 2, 3}},\n"
  2404. " {{1, 2, 3}},\n"
  2405. " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
  2406. " 333333333333333333333333333333}},\n"
  2407. " {{1, 2, 3}},\n"
  2408. " {{1, 2, 3}}};");
  2409. verifyFormat("struct {\n"
  2410. " unsigned bit;\n"
  2411. " const char *const name;\n"
  2412. "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
  2413. " {kOsWin, \"Windows\"},\n"
  2414. " {kOsLinux, \"Linux\"},\n"
  2415. " {kOsCrOS, \"Chrome OS\"}};");
  2416. verifyFormat("struct {\n"
  2417. " unsigned bit;\n"
  2418. " const char *const name;\n"
  2419. "} kBitsToOs[] = {\n"
  2420. " {kOsMac, \"Mac\"},\n"
  2421. " {kOsWin, \"Windows\"},\n"
  2422. " {kOsLinux, \"Linux\"},\n"
  2423. " {kOsCrOS, \"Chrome OS\"},\n"
  2424. "};");
  2425. }
  2426. TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
  2427. verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2428. " \\\n"
  2429. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
  2430. }
  2431. TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
  2432. verifyFormat("virtual void write(ELFWriter *writerrr,\n"
  2433. " OwningPtr<FileOutputBuffer> &buffer) = 0;");
  2434. // Do break defaulted and deleted functions.
  2435. verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
  2436. " default;",
  2437. getLLVMStyleWithColumns(40));
  2438. verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
  2439. " delete;",
  2440. getLLVMStyleWithColumns(40));
  2441. }
  2442. TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
  2443. verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
  2444. getLLVMStyleWithColumns(40));
  2445. verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  2446. getLLVMStyleWithColumns(40));
  2447. EXPECT_EQ("#define Q \\\n"
  2448. " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n"
  2449. " \"aaaaaaaa.cpp\"",
  2450. format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  2451. getLLVMStyleWithColumns(40)));
  2452. }
  2453. TEST_F(FormatTest, UnderstandsLinePPDirective) {
  2454. EXPECT_EQ("# 123 \"A string literal\"",
  2455. format(" # 123 \"A string literal\""));
  2456. }
  2457. TEST_F(FormatTest, LayoutUnknownPPDirective) {
  2458. EXPECT_EQ("#;", format("#;"));
  2459. verifyFormat("#\n;\n;\n;");
  2460. }
  2461. TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
  2462. EXPECT_EQ("#line 42 \"test\"\n",
  2463. format("# \\\n line \\\n 42 \\\n \"test\"\n"));
  2464. EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n",
  2465. getLLVMStyleWithColumns(12)));
  2466. }
  2467. TEST_F(FormatTest, EndOfFileEndsPPDirective) {
  2468. EXPECT_EQ("#line 42 \"test\"",
  2469. format("# \\\n line \\\n 42 \\\n \"test\""));
  2470. EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B"));
  2471. }
  2472. TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
  2473. verifyFormat("#define A \\x20");
  2474. verifyFormat("#define A \\ x20");
  2475. EXPECT_EQ("#define A \\ x20", format("#define A \\ x20"));
  2476. verifyFormat("#define A ''");
  2477. verifyFormat("#define A ''qqq");
  2478. verifyFormat("#define A `qqq");
  2479. verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
  2480. EXPECT_EQ("const char *c = STRINGIFY(\n"
  2481. "\\na : b);",
  2482. format("const char * c = STRINGIFY(\n"
  2483. "\\na : b);"));
  2484. verifyFormat("a\r\\");
  2485. verifyFormat("a\v\\");
  2486. verifyFormat("a\f\\");
  2487. }
  2488. TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
  2489. verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
  2490. verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
  2491. verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
  2492. // FIXME: We never break before the macro name.
  2493. verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
  2494. verifyFormat("#define A A\n#define A A");
  2495. verifyFormat("#define A(X) A\n#define A A");
  2496. verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
  2497. verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
  2498. }
  2499. TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
  2500. EXPECT_EQ("// somecomment\n"
  2501. "#include \"a.h\"\n"
  2502. "#define A( \\\n"
  2503. " A, B)\n"
  2504. "#include \"b.h\"\n"
  2505. "// somecomment\n",
  2506. format(" // somecomment\n"
  2507. " #include \"a.h\"\n"
  2508. "#define A(A,\\\n"
  2509. " B)\n"
  2510. " #include \"b.h\"\n"
  2511. " // somecomment\n",
  2512. getLLVMStyleWithColumns(13)));
  2513. }
  2514. TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
  2515. TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
  2516. EXPECT_EQ("#define A \\\n"
  2517. " c; \\\n"
  2518. " e;\n"
  2519. "f;",
  2520. format("#define A c; e;\n"
  2521. "f;",
  2522. getLLVMStyleWithColumns(14)));
  2523. }
  2524. TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
  2525. TEST_F(FormatTest, MacroDefinitionInsideStatement) {
  2526. EXPECT_EQ("int x,\n"
  2527. "#define A\n"
  2528. " y;",
  2529. format("int x,\n#define A\ny;"));
  2530. }
  2531. TEST_F(FormatTest, HashInMacroDefinition) {
  2532. EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
  2533. verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
  2534. verifyFormat("#define A \\\n"
  2535. " { \\\n"
  2536. " f(#c); \\\n"
  2537. " }",
  2538. getLLVMStyleWithColumns(11));
  2539. verifyFormat("#define A(X) \\\n"
  2540. " void function##X()",
  2541. getLLVMStyleWithColumns(22));
  2542. verifyFormat("#define A(a, b, c) \\\n"
  2543. " void a##b##c()",
  2544. getLLVMStyleWithColumns(22));
  2545. verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
  2546. }
  2547. TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
  2548. EXPECT_EQ("#define A (x)", format("#define A (x)"));
  2549. EXPECT_EQ("#define A(x)", format("#define A(x)"));
  2550. }
  2551. TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
  2552. EXPECT_EQ("#define A b;", format("#define A \\\n"
  2553. " \\\n"
  2554. " b;",
  2555. getLLVMStyleWithColumns(25)));
  2556. EXPECT_EQ("#define A \\\n"
  2557. " \\\n"
  2558. " a; \\\n"
  2559. " b;",
  2560. format("#define A \\\n"
  2561. " \\\n"
  2562. " a; \\\n"
  2563. " b;",
  2564. getLLVMStyleWithColumns(11)));
  2565. EXPECT_EQ("#define A \\\n"
  2566. " a; \\\n"
  2567. " \\\n"
  2568. " b;",
  2569. format("#define A \\\n"
  2570. " a; \\\n"
  2571. " \\\n"
  2572. " b;",
  2573. getLLVMStyleWithColumns(11)));
  2574. }
  2575. TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
  2576. verifyIncompleteFormat("#define A :");
  2577. verifyFormat("#define SOMECASES \\\n"
  2578. " case 1: \\\n"
  2579. " case 2\n",
  2580. getLLVMStyleWithColumns(20));
  2581. verifyFormat("#define A template <typename T>");
  2582. verifyIncompleteFormat("#define STR(x) #x\n"
  2583. "f(STR(this_is_a_string_literal{));");
  2584. verifyFormat("#pragma omp threadprivate( \\\n"
  2585. " y)), // expected-warning",
  2586. getLLVMStyleWithColumns(28));
  2587. verifyFormat("#d, = };");
  2588. verifyFormat("#if \"a");
  2589. verifyIncompleteFormat("({\n"
  2590. "#define b \\\n"
  2591. " } \\\n"
  2592. " a\n"
  2593. "a",
  2594. getLLVMStyleWithColumns(15));
  2595. verifyFormat("#define A \\\n"
  2596. " { \\\n"
  2597. " {\n"
  2598. "#define B \\\n"
  2599. " } \\\n"
  2600. " }",
  2601. getLLVMStyleWithColumns(15));
  2602. verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
  2603. verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
  2604. verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
  2605. verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}");
  2606. }
  2607. TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
  2608. verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
  2609. EXPECT_EQ("class A : public QObject {\n"
  2610. " Q_OBJECT\n"
  2611. "\n"
  2612. " A() {}\n"
  2613. "};",
  2614. format("class A : public QObject {\n"
  2615. " Q_OBJECT\n"
  2616. "\n"
  2617. " A() {\n}\n"
  2618. "} ;"));
  2619. EXPECT_EQ("MACRO\n"
  2620. "/*static*/ int i;",
  2621. format("MACRO\n"
  2622. " /*static*/ int i;"));
  2623. EXPECT_EQ("SOME_MACRO\n"
  2624. "namespace {\n"
  2625. "void f();\n"
  2626. "}",
  2627. format("SOME_MACRO\n"
  2628. " namespace {\n"
  2629. "void f( );\n"
  2630. "}"));
  2631. // Only if the identifier contains at least 5 characters.
  2632. EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
  2633. EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
  2634. // Only if everything is upper case.
  2635. EXPECT_EQ("class A : public QObject {\n"
  2636. " Q_Object A() {}\n"
  2637. "};",
  2638. format("class A : public QObject {\n"
  2639. " Q_Object\n"
  2640. " A() {\n}\n"
  2641. "} ;"));
  2642. // Only if the next line can actually start an unwrapped line.
  2643. EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
  2644. format("SOME_WEIRD_LOG_MACRO\n"
  2645. "<< SomeThing;"));
  2646. verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
  2647. "(n, buffers))\n",
  2648. getChromiumStyle(FormatStyle::LK_Cpp));
  2649. }
  2650. TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
  2651. EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  2652. "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  2653. "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  2654. "class X {};\n"
  2655. "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  2656. "int *createScopDetectionPass() { return 0; }",
  2657. format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  2658. " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  2659. " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  2660. " class X {};\n"
  2661. " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  2662. " int *createScopDetectionPass() { return 0; }"));
  2663. // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
  2664. // braces, so that inner block is indented one level more.
  2665. EXPECT_EQ("int q() {\n"
  2666. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  2667. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  2668. " IPC_END_MESSAGE_MAP()\n"
  2669. "}",
  2670. format("int q() {\n"
  2671. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  2672. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  2673. " IPC_END_MESSAGE_MAP()\n"
  2674. "}"));
  2675. // Same inside macros.
  2676. EXPECT_EQ("#define LIST(L) \\\n"
  2677. " L(A) \\\n"
  2678. " L(B) \\\n"
  2679. " L(C)",
  2680. format("#define LIST(L) \\\n"
  2681. " L(A) \\\n"
  2682. " L(B) \\\n"
  2683. " L(C)",
  2684. getGoogleStyle()));
  2685. // These must not be recognized as macros.
  2686. EXPECT_EQ("int q() {\n"
  2687. " f(x);\n"
  2688. " f(x) {}\n"
  2689. " f(x)->g();\n"
  2690. " f(x)->*g();\n"
  2691. " f(x).g();\n"
  2692. " f(x) = x;\n"
  2693. " f(x) += x;\n"
  2694. " f(x) -= x;\n"
  2695. " f(x) *= x;\n"
  2696. " f(x) /= x;\n"
  2697. " f(x) %= x;\n"
  2698. " f(x) &= x;\n"
  2699. " f(x) |= x;\n"
  2700. " f(x) ^= x;\n"
  2701. " f(x) >>= x;\n"
  2702. " f(x) <<= x;\n"
  2703. " f(x)[y].z();\n"
  2704. " LOG(INFO) << x;\n"
  2705. " ifstream(x) >> x;\n"
  2706. "}\n",
  2707. format("int q() {\n"
  2708. " f(x)\n;\n"
  2709. " f(x)\n {}\n"
  2710. " f(x)\n->g();\n"
  2711. " f(x)\n->*g();\n"
  2712. " f(x)\n.g();\n"
  2713. " f(x)\n = x;\n"
  2714. " f(x)\n += x;\n"
  2715. " f(x)\n -= x;\n"
  2716. " f(x)\n *= x;\n"
  2717. " f(x)\n /= x;\n"
  2718. " f(x)\n %= x;\n"
  2719. " f(x)\n &= x;\n"
  2720. " f(x)\n |= x;\n"
  2721. " f(x)\n ^= x;\n"
  2722. " f(x)\n >>= x;\n"
  2723. " f(x)\n <<= x;\n"
  2724. " f(x)\n[y].z();\n"
  2725. " LOG(INFO)\n << x;\n"
  2726. " ifstream(x)\n >> x;\n"
  2727. "}\n"));
  2728. EXPECT_EQ("int q() {\n"
  2729. " F(x)\n"
  2730. " if (1) {\n"
  2731. " }\n"
  2732. " F(x)\n"
  2733. " while (1) {\n"
  2734. " }\n"
  2735. " F(x)\n"
  2736. " G(x);\n"
  2737. " F(x)\n"
  2738. " try {\n"
  2739. " Q();\n"
  2740. " } catch (...) {\n"
  2741. " }\n"
  2742. "}\n",
  2743. format("int q() {\n"
  2744. "F(x)\n"
  2745. "if (1) {}\n"
  2746. "F(x)\n"
  2747. "while (1) {}\n"
  2748. "F(x)\n"
  2749. "G(x);\n"
  2750. "F(x)\n"
  2751. "try { Q(); } catch (...) {}\n"
  2752. "}\n"));
  2753. EXPECT_EQ("class A {\n"
  2754. " A() : t(0) {}\n"
  2755. " A(int i) noexcept() : {}\n"
  2756. " A(X x)\n" // FIXME: function-level try blocks are broken.
  2757. " try : t(0) {\n"
  2758. " } catch (...) {\n"
  2759. " }\n"
  2760. "};",
  2761. format("class A {\n"
  2762. " A()\n : t(0) {}\n"
  2763. " A(int i)\n noexcept() : {}\n"
  2764. " A(X x)\n"
  2765. " try : t(0) {} catch (...) {}\n"
  2766. "};"));
  2767. EXPECT_EQ("class SomeClass {\n"
  2768. "public:\n"
  2769. " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2770. "};",
  2771. format("class SomeClass {\n"
  2772. "public:\n"
  2773. " SomeClass()\n"
  2774. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2775. "};"));
  2776. EXPECT_EQ("class SomeClass {\n"
  2777. "public:\n"
  2778. " SomeClass()\n"
  2779. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2780. "};",
  2781. format("class SomeClass {\n"
  2782. "public:\n"
  2783. " SomeClass()\n"
  2784. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2785. "};",
  2786. getLLVMStyleWithColumns(40)));
  2787. verifyFormat("MACRO(>)");
  2788. }
  2789. TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
  2790. verifyFormat("#define A \\\n"
  2791. " f({ \\\n"
  2792. " g(); \\\n"
  2793. " });",
  2794. getLLVMStyleWithColumns(11));
  2795. }
  2796. TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
  2797. EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
  2798. }
  2799. TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
  2800. verifyFormat("{\n { a #c; }\n}");
  2801. }
  2802. TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
  2803. EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
  2804. format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
  2805. EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
  2806. format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
  2807. }
  2808. TEST_F(FormatTest, EscapedNewlines) {
  2809. EXPECT_EQ(
  2810. "#define A \\\n int i; \\\n int j;",
  2811. format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
  2812. EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
  2813. EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
  2814. EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/"));
  2815. EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
  2816. }
  2817. TEST_F(FormatTest, DontCrashOnBlockComments) {
  2818. EXPECT_EQ(
  2819. "int xxxxxxxxx; /* "
  2820. "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
  2821. "zzzzzz\n"
  2822. "0*/",
  2823. format("int xxxxxxxxx; /* "
  2824. "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
  2825. "0*/"));
  2826. }
  2827. TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
  2828. verifyFormat("#define A \\\n"
  2829. " int v( \\\n"
  2830. " a); \\\n"
  2831. " int i;",
  2832. getLLVMStyleWithColumns(11));
  2833. }
  2834. TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
  2835. EXPECT_EQ(
  2836. "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2837. " \\\n"
  2838. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2839. "\n"
  2840. "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2841. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
  2842. format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2843. "\\\n"
  2844. "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2845. " \n"
  2846. " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2847. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
  2848. }
  2849. TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
  2850. EXPECT_EQ("int\n"
  2851. "#define A\n"
  2852. " a;",
  2853. format("int\n#define A\na;"));
  2854. verifyFormat("functionCallTo(\n"
  2855. " someOtherFunction(\n"
  2856. " withSomeParameters, whichInSequence,\n"
  2857. " areLongerThanALine(andAnotherCall,\n"
  2858. "#define A B\n"
  2859. " withMoreParamters,\n"
  2860. " whichStronglyInfluenceTheLayout),\n"
  2861. " andMoreParameters),\n"
  2862. " trailing);",
  2863. getLLVMStyleWithColumns(69));
  2864. verifyFormat("Foo::Foo()\n"
  2865. "#ifdef BAR\n"
  2866. " : baz(0)\n"
  2867. "#endif\n"
  2868. "{\n"
  2869. "}");
  2870. verifyFormat("void f() {\n"
  2871. " if (true)\n"
  2872. "#ifdef A\n"
  2873. " f(42);\n"
  2874. " x();\n"
  2875. "#else\n"
  2876. " g();\n"
  2877. " x();\n"
  2878. "#endif\n"
  2879. "}");
  2880. verifyFormat("void f(param1, param2,\n"
  2881. " param3,\n"
  2882. "#ifdef A\n"
  2883. " param4(param5,\n"
  2884. "#ifdef A1\n"
  2885. " param6,\n"
  2886. "#ifdef A2\n"
  2887. " param7),\n"
  2888. "#else\n"
  2889. " param8),\n"
  2890. " param9,\n"
  2891. "#endif\n"
  2892. " param10,\n"
  2893. "#endif\n"
  2894. " param11)\n"
  2895. "#else\n"
  2896. " param12)\n"
  2897. "#endif\n"
  2898. "{\n"
  2899. " x();\n"
  2900. "}",
  2901. getLLVMStyleWithColumns(28));
  2902. verifyFormat("#if 1\n"
  2903. "int i;");
  2904. verifyFormat("#if 1\n"
  2905. "#endif\n"
  2906. "#if 1\n"
  2907. "#else\n"
  2908. "#endif\n");
  2909. verifyFormat("DEBUG({\n"
  2910. " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2911. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  2912. "});\n"
  2913. "#if a\n"
  2914. "#else\n"
  2915. "#endif");
  2916. verifyIncompleteFormat("void f(\n"
  2917. "#if A\n"
  2918. " );\n"
  2919. "#else\n"
  2920. "#endif");
  2921. }
  2922. TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
  2923. verifyFormat("#endif\n"
  2924. "#if B");
  2925. }
  2926. TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
  2927. FormatStyle SingleLine = getLLVMStyle();
  2928. SingleLine.AllowShortIfStatementsOnASingleLine = true;
  2929. verifyFormat("#if 0\n"
  2930. "#elif 1\n"
  2931. "#endif\n"
  2932. "void foo() {\n"
  2933. " if (test) foo2();\n"
  2934. "}",
  2935. SingleLine);
  2936. }
  2937. TEST_F(FormatTest, LayoutBlockInsideParens) {
  2938. verifyFormat("functionCall({ int i; });");
  2939. verifyFormat("functionCall({\n"
  2940. " int i;\n"
  2941. " int j;\n"
  2942. "});");
  2943. verifyFormat("functionCall(\n"
  2944. " {\n"
  2945. " int i;\n"
  2946. " int j;\n"
  2947. " },\n"
  2948. " aaaa, bbbb, cccc);");
  2949. verifyFormat("functionA(functionB({\n"
  2950. " int i;\n"
  2951. " int j;\n"
  2952. " }),\n"
  2953. " aaaa, bbbb, cccc);");
  2954. verifyFormat("functionCall(\n"
  2955. " {\n"
  2956. " int i;\n"
  2957. " int j;\n"
  2958. " },\n"
  2959. " aaaa, bbbb, // comment\n"
  2960. " cccc);");
  2961. verifyFormat("functionA(functionB({\n"
  2962. " int i;\n"
  2963. " int j;\n"
  2964. " }),\n"
  2965. " aaaa, bbbb, // comment\n"
  2966. " cccc);");
  2967. verifyFormat("functionCall(aaaa, bbbb, { int i; });");
  2968. verifyFormat("functionCall(aaaa, bbbb, {\n"
  2969. " int i;\n"
  2970. " int j;\n"
  2971. "});");
  2972. verifyFormat(
  2973. "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
  2974. " {\n"
  2975. " int i; // break\n"
  2976. " },\n"
  2977. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  2978. " ccccccccccccccccc));");
  2979. verifyFormat("DEBUG({\n"
  2980. " if (a)\n"
  2981. " f();\n"
  2982. "});");
  2983. }
  2984. TEST_F(FormatTest, LayoutBlockInsideStatement) {
  2985. EXPECT_EQ("SOME_MACRO { int i; }\n"
  2986. "int i;",
  2987. format(" SOME_MACRO {int i;} int i;"));
  2988. }
  2989. TEST_F(FormatTest, LayoutNestedBlocks) {
  2990. verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
  2991. " struct s {\n"
  2992. " int i;\n"
  2993. " };\n"
  2994. " s kBitsToOs[] = {{10}};\n"
  2995. " for (int i = 0; i < 10; ++i)\n"
  2996. " return;\n"
  2997. "}");
  2998. verifyFormat("call(parameter, {\n"
  2999. " something();\n"
  3000. " // Comment using all columns.\n"
  3001. " somethingelse();\n"
  3002. "});",
  3003. getLLVMStyleWithColumns(40));
  3004. verifyFormat("DEBUG( //\n"
  3005. " { f(); }, a);");
  3006. verifyFormat("DEBUG( //\n"
  3007. " {\n"
  3008. " f(); //\n"
  3009. " },\n"
  3010. " a);");
  3011. EXPECT_EQ("call(parameter, {\n"
  3012. " something();\n"
  3013. " // Comment too\n"
  3014. " // looooooooooong.\n"
  3015. " somethingElse();\n"
  3016. "});",
  3017. format("call(parameter, {\n"
  3018. " something();\n"
  3019. " // Comment too looooooooooong.\n"
  3020. " somethingElse();\n"
  3021. "});",
  3022. getLLVMStyleWithColumns(29)));
  3023. EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });"));
  3024. EXPECT_EQ("DEBUG({ // comment\n"
  3025. " int i;\n"
  3026. "});",
  3027. format("DEBUG({ // comment\n"
  3028. "int i;\n"
  3029. "});"));
  3030. EXPECT_EQ("DEBUG({\n"
  3031. " int i;\n"
  3032. "\n"
  3033. " // comment\n"
  3034. " int j;\n"
  3035. "});",
  3036. format("DEBUG({\n"
  3037. " int i;\n"
  3038. "\n"
  3039. " // comment\n"
  3040. " int j;\n"
  3041. "});"));
  3042. verifyFormat("DEBUG({\n"
  3043. " if (a)\n"
  3044. " return;\n"
  3045. "});");
  3046. verifyGoogleFormat("DEBUG({\n"
  3047. " if (a) return;\n"
  3048. "});");
  3049. FormatStyle Style = getGoogleStyle();
  3050. Style.ColumnLimit = 45;
  3051. verifyFormat("Debug(aaaaa,\n"
  3052. " {\n"
  3053. " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
  3054. " },\n"
  3055. " a);",
  3056. Style);
  3057. verifyFormat("SomeFunction({MACRO({ return output; }), b});");
  3058. verifyNoCrash("^{v^{a}}");
  3059. }
  3060. TEST_F(FormatTest, FormatNestedBlocksInMacros) {
  3061. EXPECT_EQ("#define MACRO() \\\n"
  3062. " Debug(aaa, /* force line break */ \\\n"
  3063. " { \\\n"
  3064. " int i; \\\n"
  3065. " int j; \\\n"
  3066. " })",
  3067. format("#define MACRO() Debug(aaa, /* force line break */ \\\n"
  3068. " { int i; int j; })",
  3069. getGoogleStyle()));
  3070. EXPECT_EQ("#define A \\\n"
  3071. " [] { \\\n"
  3072. " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
  3073. " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
  3074. " }",
  3075. format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
  3076. "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
  3077. getGoogleStyle()));
  3078. }
  3079. TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
  3080. EXPECT_EQ("{}", format("{}"));
  3081. verifyFormat("enum E {};");
  3082. verifyFormat("enum E {}");
  3083. }
  3084. TEST_F(FormatTest, FormatBeginBlockEndMacros) {
  3085. FormatStyle Style = getLLVMStyle();
  3086. Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
  3087. Style.MacroBlockEnd = "^[A-Z_]+_END$";
  3088. verifyFormat("FOO_BEGIN\n"
  3089. " FOO_ENTRY\n"
  3090. "FOO_END", Style);
  3091. verifyFormat("FOO_BEGIN\n"
  3092. " NESTED_FOO_BEGIN\n"
  3093. " NESTED_FOO_ENTRY\n"
  3094. " NESTED_FOO_END\n"
  3095. "FOO_END", Style);
  3096. verifyFormat("FOO_BEGIN(Foo, Bar)\n"
  3097. " int x;\n"
  3098. " x = 1;\n"
  3099. "FOO_END(Baz)", Style);
  3100. }
  3101. //===----------------------------------------------------------------------===//
  3102. // Line break tests.
  3103. //===----------------------------------------------------------------------===//
  3104. TEST_F(FormatTest, PreventConfusingIndents) {
  3105. verifyFormat(
  3106. "void f() {\n"
  3107. " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
  3108. " parameter, parameter, parameter)),\n"
  3109. " SecondLongCall(parameter));\n"
  3110. "}");
  3111. verifyFormat(
  3112. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3113. " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3114. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3115. " aaaaaaaaaaaaaaaaaaaaaaaa);");
  3116. verifyFormat(
  3117. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3118. " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
  3119. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  3120. " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
  3121. verifyFormat(
  3122. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  3123. " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
  3124. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
  3125. " aaaaaaaaaaaaaaaaaaaaaaaa>;");
  3126. verifyFormat("int a = bbbb && ccc && fffff(\n"
  3127. "#define A Just forcing a new line\n"
  3128. " ddd);");
  3129. }
  3130. TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
  3131. verifyFormat(
  3132. "bool aaaaaaa =\n"
  3133. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
  3134. " bbbbbbbb();");
  3135. verifyFormat(
  3136. "bool aaaaaaa =\n"
  3137. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
  3138. " bbbbbbbb();");
  3139. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  3140. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
  3141. " ccccccccc == ddddddddddd;");
  3142. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  3143. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
  3144. " ccccccccc == ddddddddddd;");
  3145. verifyFormat(
  3146. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  3147. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
  3148. " ccccccccc == ddddddddddd;");
  3149. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  3150. " aaaaaa) &&\n"
  3151. " bbbbbb && cccccc;");
  3152. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  3153. " aaaaaa) >>\n"
  3154. " bbbbbb;");
  3155. verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
  3156. " SourceMgr.getSpellingColumnNumber(\n"
  3157. " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
  3158. " 1);");
  3159. verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3160. " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
  3161. " cccccc) {\n}");
  3162. verifyFormat("b = a &&\n"
  3163. " // Comment\n"
  3164. " b.c && d;");
  3165. // If the LHS of a comparison is not a binary expression itself, the
  3166. // additional linebreak confuses many people.
  3167. verifyFormat(
  3168. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3169. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
  3170. "}");
  3171. verifyFormat(
  3172. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3173. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  3174. "}");
  3175. verifyFormat(
  3176. "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
  3177. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  3178. "}");
  3179. // Even explicit parentheses stress the precedence enough to make the
  3180. // additional break unnecessary.
  3181. verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3182. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  3183. "}");
  3184. // This cases is borderline, but with the indentation it is still readable.
  3185. verifyFormat(
  3186. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3187. " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3188. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  3189. "}",
  3190. getLLVMStyleWithColumns(75));
  3191. // If the LHS is a binary expression, we should still use the additional break
  3192. // as otherwise the formatting hides the operator precedence.
  3193. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3194. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3195. " 5) {\n"
  3196. "}");
  3197. FormatStyle OnePerLine = getLLVMStyle();
  3198. OnePerLine.BinPackParameters = false;
  3199. verifyFormat(
  3200. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3201. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3202. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
  3203. OnePerLine);
  3204. }
  3205. TEST_F(FormatTest, ExpressionIndentation) {
  3206. verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3207. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3208. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3209. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3210. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
  3211. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
  3212. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3213. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
  3214. " ccccccccccccccccccccccccccccccccccccccccc;");
  3215. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3216. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3217. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3218. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3219. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3220. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3221. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3222. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3223. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3224. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3225. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3226. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3227. verifyFormat("if () {\n"
  3228. "} else if (aaaaa &&\n"
  3229. " bbbbb > // break\n"
  3230. " ccccc) {\n"
  3231. "}");
  3232. // Presence of a trailing comment used to change indentation of b.
  3233. verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
  3234. " b;\n"
  3235. "return aaaaaaaaaaaaaaaaaaa +\n"
  3236. " b; //",
  3237. getLLVMStyleWithColumns(30));
  3238. }
  3239. TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
  3240. // Not sure what the best system is here. Like this, the LHS can be found
  3241. // immediately above an operator (everything with the same or a higher
  3242. // indent). The RHS is aligned right of the operator and so compasses
  3243. // everything until something with the same indent as the operator is found.
  3244. // FIXME: Is this a good system?
  3245. FormatStyle Style = getLLVMStyle();
  3246. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  3247. verifyFormat(
  3248. "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3249. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3250. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3251. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3252. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3253. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3254. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3255. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3256. " > ccccccccccccccccccccccccccccccccccccccccc;",
  3257. Style);
  3258. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3259. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3260. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3261. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3262. Style);
  3263. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3264. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3265. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3266. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3267. Style);
  3268. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3269. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3270. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3271. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3272. Style);
  3273. verifyFormat("if () {\n"
  3274. "} else if (aaaaa\n"
  3275. " && bbbbb // break\n"
  3276. " > ccccc) {\n"
  3277. "}",
  3278. Style);
  3279. verifyFormat("return (a)\n"
  3280. " // comment\n"
  3281. " + b;",
  3282. Style);
  3283. verifyFormat(
  3284. "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3285. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3286. " + cc;",
  3287. Style);
  3288. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3289. " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  3290. Style);
  3291. // Forced by comments.
  3292. verifyFormat(
  3293. "unsigned ContentSize =\n"
  3294. " sizeof(int16_t) // DWARF ARange version number\n"
  3295. " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
  3296. " + sizeof(int8_t) // Pointer Size (in bytes)\n"
  3297. " + sizeof(int8_t); // Segment Size (in bytes)");
  3298. verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
  3299. " == boost::fusion::at_c<1>(iiii).second;",
  3300. Style);
  3301. Style.ColumnLimit = 60;
  3302. verifyFormat("zzzzzzzzzz\n"
  3303. " = bbbbbbbbbbbbbbbbb\n"
  3304. " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3305. Style);
  3306. }
  3307. TEST_F(FormatTest, NoOperandAlignment) {
  3308. FormatStyle Style = getLLVMStyle();
  3309. Style.AlignOperands = false;
  3310. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
  3311. verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3312. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3313. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3314. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3315. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3316. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3317. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3318. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3319. " > ccccccccccccccccccccccccccccccccccccccccc;",
  3320. Style);
  3321. verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3322. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3323. " + cc;",
  3324. Style);
  3325. verifyFormat("int a = aa\n"
  3326. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3327. " * cccccccccccccccccccccccccccccccccccc;",
  3328. Style);
  3329. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  3330. verifyFormat("return (a > b\n"
  3331. " // comment1\n"
  3332. " // comment2\n"
  3333. " || c);",
  3334. Style);
  3335. }
  3336. TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
  3337. FormatStyle Style = getLLVMStyle();
  3338. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
  3339. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3340. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3341. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
  3342. Style);
  3343. }
  3344. TEST_F(FormatTest, ConstructorInitializers) {
  3345. verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
  3346. verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
  3347. getLLVMStyleWithColumns(45));
  3348. verifyFormat("Constructor()\n"
  3349. " : Inttializer(FitsOnTheLine) {}",
  3350. getLLVMStyleWithColumns(44));
  3351. verifyFormat("Constructor()\n"
  3352. " : Inttializer(FitsOnTheLine) {}",
  3353. getLLVMStyleWithColumns(43));
  3354. verifyFormat("template <typename T>\n"
  3355. "Constructor() : Initializer(FitsOnTheLine) {}",
  3356. getLLVMStyleWithColumns(45));
  3357. verifyFormat(
  3358. "SomeClass::Constructor()\n"
  3359. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  3360. verifyFormat(
  3361. "SomeClass::Constructor()\n"
  3362. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3363. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
  3364. verifyFormat(
  3365. "SomeClass::Constructor()\n"
  3366. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3367. " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  3368. verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3369. " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3370. " : aaaaaaaaaa(aaaaaa) {}");
  3371. verifyFormat("Constructor()\n"
  3372. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3373. " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3374. " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3375. " aaaaaaaaaaaaaaaaaaaaaaa() {}");
  3376. verifyFormat("Constructor()\n"
  3377. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3378. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3379. verifyFormat("Constructor(int Parameter = 0)\n"
  3380. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
  3381. " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
  3382. verifyFormat("Constructor()\n"
  3383. " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
  3384. "}",
  3385. getLLVMStyleWithColumns(60));
  3386. verifyFormat("Constructor()\n"
  3387. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3388. " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
  3389. // Here a line could be saved by splitting the second initializer onto two
  3390. // lines, but that is not desirable.
  3391. verifyFormat("Constructor()\n"
  3392. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3393. " aaaaaaaaaaa(aaaaaaaaaaa),\n"
  3394. " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3395. FormatStyle OnePerLine = getLLVMStyle();
  3396. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  3397. OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
  3398. verifyFormat("SomeClass::Constructor()\n"
  3399. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3400. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3401. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  3402. OnePerLine);
  3403. verifyFormat("SomeClass::Constructor()\n"
  3404. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
  3405. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3406. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  3407. OnePerLine);
  3408. verifyFormat("MyClass::MyClass(int var)\n"
  3409. " : some_var_(var), // 4 space indent\n"
  3410. " some_other_var_(var + 1) { // lined up\n"
  3411. "}",
  3412. OnePerLine);
  3413. verifyFormat("Constructor()\n"
  3414. " : aaaaa(aaaaaa),\n"
  3415. " aaaaa(aaaaaa),\n"
  3416. " aaaaa(aaaaaa),\n"
  3417. " aaaaa(aaaaaa),\n"
  3418. " aaaaa(aaaaaa) {}",
  3419. OnePerLine);
  3420. verifyFormat("Constructor()\n"
  3421. " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
  3422. " aaaaaaaaaaaaaaaaaaaaaa) {}",
  3423. OnePerLine);
  3424. OnePerLine.BinPackParameters = false;
  3425. verifyFormat(
  3426. "Constructor()\n"
  3427. " : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3428. " aaaaaaaaaaa().aaa(),\n"
  3429. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3430. OnePerLine);
  3431. OnePerLine.ColumnLimit = 60;
  3432. verifyFormat("Constructor()\n"
  3433. " : aaaaaaaaaaaaaaaaaaaa(a),\n"
  3434. " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
  3435. OnePerLine);
  3436. EXPECT_EQ("Constructor()\n"
  3437. " : // Comment forcing unwanted break.\n"
  3438. " aaaa(aaaa) {}",
  3439. format("Constructor() :\n"
  3440. " // Comment forcing unwanted break.\n"
  3441. " aaaa(aaaa) {}"));
  3442. }
  3443. TEST_F(FormatTest, MemoizationTests) {
  3444. // This breaks if the memoization lookup does not take \c Indent and
  3445. // \c LastSpace into account.
  3446. verifyFormat(
  3447. "extern CFRunLoopTimerRef\n"
  3448. "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
  3449. " CFTimeInterval interval, CFOptionFlags flags,\n"
  3450. " CFIndex order, CFRunLoopTimerCallBack callout,\n"
  3451. " CFRunLoopTimerContext *context) {}");
  3452. // Deep nesting somewhat works around our memoization.
  3453. verifyFormat(
  3454. "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3455. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3456. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3457. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3458. " aaaaa())))))))))))))))))))))))))))))))))))))));",
  3459. getLLVMStyleWithColumns(65));
  3460. verifyFormat(
  3461. "aaaaa(\n"
  3462. " aaaaa,\n"
  3463. " aaaaa(\n"
  3464. " aaaaa,\n"
  3465. " aaaaa(\n"
  3466. " aaaaa,\n"
  3467. " aaaaa(\n"
  3468. " aaaaa,\n"
  3469. " aaaaa(\n"
  3470. " aaaaa,\n"
  3471. " aaaaa(\n"
  3472. " aaaaa,\n"
  3473. " aaaaa(\n"
  3474. " aaaaa,\n"
  3475. " aaaaa(\n"
  3476. " aaaaa,\n"
  3477. " aaaaa(\n"
  3478. " aaaaa,\n"
  3479. " aaaaa(\n"
  3480. " aaaaa,\n"
  3481. " aaaaa(\n"
  3482. " aaaaa,\n"
  3483. " aaaaa(\n"
  3484. " aaaaa,\n"
  3485. " aaaaa))))))))))));",
  3486. getLLVMStyleWithColumns(65));
  3487. verifyFormat(
  3488. "a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(), a), a), a), a),\n"
  3489. " a),\n"
  3490. " a),\n"
  3491. " a),\n"
  3492. " a),\n"
  3493. " a),\n"
  3494. " a),\n"
  3495. " a),\n"
  3496. " a),\n"
  3497. " a),\n"
  3498. " a),\n"
  3499. " a),\n"
  3500. " a),\n"
  3501. " a),\n"
  3502. " a),\n"
  3503. " a),\n"
  3504. " a),\n"
  3505. " a)",
  3506. getLLVMStyleWithColumns(65));
  3507. // This test takes VERY long when memoization is broken.
  3508. FormatStyle OnePerLine = getLLVMStyle();
  3509. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  3510. OnePerLine.BinPackParameters = false;
  3511. std::string input = "Constructor()\n"
  3512. " : aaaa(a,\n";
  3513. for (unsigned i = 0, e = 80; i != e; ++i) {
  3514. input += " a,\n";
  3515. }
  3516. input += " a) {}";
  3517. verifyFormat(input, OnePerLine);
  3518. }
  3519. TEST_F(FormatTest, BreaksAsHighAsPossible) {
  3520. verifyFormat(
  3521. "void f() {\n"
  3522. " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
  3523. " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
  3524. " f();\n"
  3525. "}");
  3526. verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
  3527. " Intervals[i - 1].getRange().getLast()) {\n}");
  3528. }
  3529. TEST_F(FormatTest, BreaksFunctionDeclarations) {
  3530. // Principially, we break function declarations in a certain order:
  3531. // 1) break amongst arguments.
  3532. verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
  3533. " Cccccccccccccc cccccccccccccc);");
  3534. verifyFormat("template <class TemplateIt>\n"
  3535. "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
  3536. " TemplateIt *stop) {}");
  3537. // 2) break after return type.
  3538. verifyFormat(
  3539. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3540. "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
  3541. getGoogleStyle());
  3542. // 3) break after (.
  3543. verifyFormat(
  3544. "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
  3545. " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
  3546. getGoogleStyle());
  3547. // 4) break before after nested name specifiers.
  3548. verifyFormat(
  3549. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3550. "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
  3551. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
  3552. getGoogleStyle());
  3553. // However, there are exceptions, if a sufficient amount of lines can be
  3554. // saved.
  3555. // FIXME: The precise cut-offs wrt. the number of saved lines might need some
  3556. // more adjusting.
  3557. verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  3558. " Cccccccccccccc cccccccccc,\n"
  3559. " Cccccccccccccc cccccccccc,\n"
  3560. " Cccccccccccccc cccccccccc,\n"
  3561. " Cccccccccccccc cccccccccc);");
  3562. verifyFormat(
  3563. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3564. "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3565. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3566. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
  3567. getGoogleStyle());
  3568. verifyFormat(
  3569. "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  3570. " Cccccccccccccc cccccccccc,\n"
  3571. " Cccccccccccccc cccccccccc,\n"
  3572. " Cccccccccccccc cccccccccc,\n"
  3573. " Cccccccccccccc cccccccccc,\n"
  3574. " Cccccccccccccc cccccccccc,\n"
  3575. " Cccccccccccccc cccccccccc);");
  3576. verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3577. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3578. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3579. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3580. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
  3581. // Break after multi-line parameters.
  3582. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3583. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3584. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3585. " bbbb bbbb);");
  3586. verifyFormat("void SomeLoooooooooooongFunction(\n"
  3587. " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  3588. " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3589. " int bbbbbbbbbbbbb);");
  3590. // Treat overloaded operators like other functions.
  3591. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3592. "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
  3593. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3594. "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
  3595. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3596. "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
  3597. verifyGoogleFormat(
  3598. "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
  3599. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  3600. verifyGoogleFormat(
  3601. "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
  3602. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  3603. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3604. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  3605. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
  3606. "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  3607. verifyGoogleFormat(
  3608. "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
  3609. "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3610. " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
  3611. verifyGoogleFormat(
  3612. "template <typename T>\n"
  3613. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3614. "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
  3615. " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
  3616. FormatStyle Style = getLLVMStyle();
  3617. Style.PointerAlignment = FormatStyle::PAS_Left;
  3618. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3619. " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
  3620. Style);
  3621. verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
  3622. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3623. Style);
  3624. }
  3625. TEST_F(FormatTest, TrailingReturnType) {
  3626. verifyFormat("auto foo() -> int;\n");
  3627. verifyFormat("struct S {\n"
  3628. " auto bar() const -> int;\n"
  3629. "};");
  3630. verifyFormat("template <size_t Order, typename T>\n"
  3631. "auto load_img(const std::string &filename)\n"
  3632. " -> alias::tensor<Order, T, mem::tag::cpu> {}");
  3633. verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
  3634. " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
  3635. verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
  3636. verifyFormat("template <typename T>\n"
  3637. "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
  3638. " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
  3639. // Not trailing return types.
  3640. verifyFormat("void f() { auto a = b->c(); }");
  3641. }
  3642. TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
  3643. // Avoid breaking before trailing 'const' or other trailing annotations, if
  3644. // they are not function-like.
  3645. FormatStyle Style = getGoogleStyle();
  3646. Style.ColumnLimit = 47;
  3647. verifyFormat("void someLongFunction(\n"
  3648. " int someLoooooooooooooongParameter) const {\n}",
  3649. getLLVMStyleWithColumns(47));
  3650. verifyFormat("LoooooongReturnType\n"
  3651. "someLoooooooongFunction() const {}",
  3652. getLLVMStyleWithColumns(47));
  3653. verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
  3654. " const {}",
  3655. Style);
  3656. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3657. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
  3658. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3659. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
  3660. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3661. " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
  3662. verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
  3663. " aaaaaaaaaaa aaaaa) const override;");
  3664. verifyGoogleFormat(
  3665. "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3666. " const override;");
  3667. // Even if the first parameter has to be wrapped.
  3668. verifyFormat("void someLongFunction(\n"
  3669. " int someLongParameter) const {}",
  3670. getLLVMStyleWithColumns(46));
  3671. verifyFormat("void someLongFunction(\n"
  3672. " int someLongParameter) const {}",
  3673. Style);
  3674. verifyFormat("void someLongFunction(\n"
  3675. " int someLongParameter) override {}",
  3676. Style);
  3677. verifyFormat("void someLongFunction(\n"
  3678. " int someLongParameter) OVERRIDE {}",
  3679. Style);
  3680. verifyFormat("void someLongFunction(\n"
  3681. " int someLongParameter) final {}",
  3682. Style);
  3683. verifyFormat("void someLongFunction(\n"
  3684. " int someLongParameter) FINAL {}",
  3685. Style);
  3686. verifyFormat("void someLongFunction(\n"
  3687. " int parameter) const override {}",
  3688. Style);
  3689. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  3690. verifyFormat("void someLongFunction(\n"
  3691. " int someLongParameter) const\n"
  3692. "{\n"
  3693. "}",
  3694. Style);
  3695. // Unless these are unknown annotations.
  3696. verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
  3697. " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3698. " LONG_AND_UGLY_ANNOTATION;");
  3699. // Breaking before function-like trailing annotations is fine to keep them
  3700. // close to their arguments.
  3701. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3702. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  3703. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  3704. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  3705. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  3706. " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
  3707. verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
  3708. " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
  3709. verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
  3710. verifyFormat(
  3711. "void aaaaaaaaaaaaaaaaaa()\n"
  3712. " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3713. " aaaaaaaaaaaaaaaaaaaaaaaaa));");
  3714. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3715. " __attribute__((unused));");
  3716. verifyGoogleFormat(
  3717. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3718. " GUARDED_BY(aaaaaaaaaaaa);");
  3719. verifyGoogleFormat(
  3720. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3721. " GUARDED_BY(aaaaaaaaaaaa);");
  3722. verifyGoogleFormat(
  3723. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
  3724. " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3725. verifyGoogleFormat(
  3726. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
  3727. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3728. }
  3729. TEST_F(FormatTest, FunctionAnnotations) {
  3730. verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
  3731. "int OldFunction(const string &parameter) {}");
  3732. verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
  3733. "string OldFunction(const string &parameter) {}");
  3734. verifyFormat("template <typename T>\n"
  3735. "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
  3736. "string OldFunction(const string &parameter) {}");
  3737. // Not function annotations.
  3738. verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3739. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
  3740. verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
  3741. " ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
  3742. verifyFormat("MACRO(abc).function() // wrap\n"
  3743. " << abc;");
  3744. verifyFormat("MACRO(abc)->function() // wrap\n"
  3745. " << abc;");
  3746. verifyFormat("MACRO(abc)::function() // wrap\n"
  3747. " << abc;");
  3748. }
  3749. TEST_F(FormatTest, BreaksDesireably) {
  3750. verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  3751. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  3752. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
  3753. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3754. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  3755. "}");
  3756. verifyFormat(
  3757. "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3758. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3759. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3760. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3761. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3762. verifyFormat(
  3763. "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3764. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  3765. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3766. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
  3767. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3768. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3769. verifyFormat(
  3770. "void f() {\n"
  3771. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
  3772. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3773. "}");
  3774. verifyFormat(
  3775. "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3776. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3777. verifyFormat(
  3778. "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3779. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3780. verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3781. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3782. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3783. // Indent consistently independent of call expression and unary operator.
  3784. verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3785. " dddddddddddddddddddddddddddddd));");
  3786. verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3787. " dddddddddddddddddddddddddddddd));");
  3788. verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
  3789. " dddddddddddddddddddddddddddddd));");
  3790. // This test case breaks on an incorrect memoization, i.e. an optimization not
  3791. // taking into account the StopAt value.
  3792. verifyFormat(
  3793. "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3794. " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3795. " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3796. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3797. verifyFormat("{\n {\n {\n"
  3798. " Annotation.SpaceRequiredBefore =\n"
  3799. " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
  3800. " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
  3801. " }\n }\n}");
  3802. // Break on an outer level if there was a break on an inner level.
  3803. EXPECT_EQ("f(g(h(a, // comment\n"
  3804. " b, c),\n"
  3805. " d, e),\n"
  3806. " x, y);",
  3807. format("f(g(h(a, // comment\n"
  3808. " b, c), d, e), x, y);"));
  3809. // Prefer breaking similar line breaks.
  3810. verifyFormat(
  3811. "const int kTrackingOptions = NSTrackingMouseMoved |\n"
  3812. " NSTrackingMouseEnteredAndExited |\n"
  3813. " NSTrackingActiveAlways;");
  3814. }
  3815. TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
  3816. FormatStyle NoBinPacking = getGoogleStyle();
  3817. NoBinPacking.BinPackParameters = false;
  3818. NoBinPacking.BinPackArguments = true;
  3819. verifyFormat("void f() {\n"
  3820. " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
  3821. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3822. "}",
  3823. NoBinPacking);
  3824. verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
  3825. " int aaaaaaaaaaaaaaaaaaaa,\n"
  3826. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3827. NoBinPacking);
  3828. NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
  3829. verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3830. " vector<int> bbbbbbbbbbbbbbb);",
  3831. NoBinPacking);
  3832. // FIXME: This behavior difference is probably not wanted. However, currently
  3833. // we cannot distinguish BreakBeforeParameter being set because of the wrapped
  3834. // template arguments from BreakBeforeParameter being set because of the
  3835. // one-per-line formatting.
  3836. verifyFormat(
  3837. "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3838. " aaaaaaaaaa> aaaaaaaaaa);",
  3839. NoBinPacking);
  3840. verifyFormat(
  3841. "void fffffffffff(\n"
  3842. " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
  3843. " aaaaaaaaaa);");
  3844. }
  3845. TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
  3846. FormatStyle NoBinPacking = getGoogleStyle();
  3847. NoBinPacking.BinPackParameters = false;
  3848. NoBinPacking.BinPackArguments = false;
  3849. verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
  3850. " aaaaaaaaaaaaaaaaaaaa,\n"
  3851. " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
  3852. NoBinPacking);
  3853. verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
  3854. " aaaaaaaaaaaaa,\n"
  3855. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
  3856. NoBinPacking);
  3857. verifyFormat(
  3858. "aaaaaaaa(aaaaaaaaaaaaa,\n"
  3859. " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3860. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  3861. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3862. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
  3863. NoBinPacking);
  3864. verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  3865. " .aaaaaaaaaaaaaaaaaa();",
  3866. NoBinPacking);
  3867. verifyFormat("void f() {\n"
  3868. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3869. " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
  3870. "}",
  3871. NoBinPacking);
  3872. verifyFormat(
  3873. "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3874. " aaaaaaaaaaaa,\n"
  3875. " aaaaaaaaaaaa);",
  3876. NoBinPacking);
  3877. verifyFormat(
  3878. "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
  3879. " ddddddddddddddddddddddddddddd),\n"
  3880. " test);",
  3881. NoBinPacking);
  3882. verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3883. " aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3884. " aaaaaaaaaaaaaaaaaaaaaaa>\n"
  3885. " aaaaaaaaaaaaaaaaaa;",
  3886. NoBinPacking);
  3887. verifyFormat("a(\"a\"\n"
  3888. " \"a\",\n"
  3889. " a);");
  3890. NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
  3891. verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
  3892. " aaaaaaaaa,\n"
  3893. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3894. NoBinPacking);
  3895. verifyFormat(
  3896. "void f() {\n"
  3897. " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  3898. " .aaaaaaa();\n"
  3899. "}",
  3900. NoBinPacking);
  3901. verifyFormat(
  3902. "template <class SomeType, class SomeOtherType>\n"
  3903. "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
  3904. NoBinPacking);
  3905. }
  3906. TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
  3907. FormatStyle Style = getLLVMStyleWithColumns(15);
  3908. Style.ExperimentalAutoDetectBinPacking = true;
  3909. EXPECT_EQ("aaa(aaaa,\n"
  3910. " aaaa,\n"
  3911. " aaaa);\n"
  3912. "aaa(aaaa,\n"
  3913. " aaaa,\n"
  3914. " aaaa);",
  3915. format("aaa(aaaa,\n" // one-per-line
  3916. " aaaa,\n"
  3917. " aaaa );\n"
  3918. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  3919. Style));
  3920. EXPECT_EQ("aaa(aaaa, aaaa,\n"
  3921. " aaaa);\n"
  3922. "aaa(aaaa, aaaa,\n"
  3923. " aaaa);",
  3924. format("aaa(aaaa, aaaa,\n" // bin-packed
  3925. " aaaa );\n"
  3926. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  3927. Style));
  3928. }
  3929. TEST_F(FormatTest, FormatsBuilderPattern) {
  3930. verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
  3931. " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
  3932. " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
  3933. " .StartsWith(\".init\", ORDER_INIT)\n"
  3934. " .StartsWith(\".fini\", ORDER_FINI)\n"
  3935. " .StartsWith(\".hash\", ORDER_HASH)\n"
  3936. " .Default(ORDER_TEXT);\n");
  3937. verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
  3938. " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
  3939. verifyFormat(
  3940. "aaaaaaa->aaaaaaa\n"
  3941. " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3942. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3943. " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
  3944. verifyFormat(
  3945. "aaaaaaa->aaaaaaa\n"
  3946. " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3947. " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
  3948. verifyFormat(
  3949. "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
  3950. " aaaaaaaaaaaaaa);");
  3951. verifyFormat(
  3952. "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
  3953. " aaaaaa->aaaaaaaaaaaa()\n"
  3954. " ->aaaaaaaaaaaaaaaa(\n"
  3955. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3956. " ->aaaaaaaaaaaaaaaaa();");
  3957. verifyGoogleFormat(
  3958. "void f() {\n"
  3959. " someo->Add((new util::filetools::Handler(dir))\n"
  3960. " ->OnEvent1(NewPermanentCallback(\n"
  3961. " this, &HandlerHolderClass::EventHandlerCBA))\n"
  3962. " ->OnEvent2(NewPermanentCallback(\n"
  3963. " this, &HandlerHolderClass::EventHandlerCBB))\n"
  3964. " ->OnEvent3(NewPermanentCallback(\n"
  3965. " this, &HandlerHolderClass::EventHandlerCBC))\n"
  3966. " ->OnEvent5(NewPermanentCallback(\n"
  3967. " this, &HandlerHolderClass::EventHandlerCBD))\n"
  3968. " ->OnEvent6(NewPermanentCallback(\n"
  3969. " this, &HandlerHolderClass::EventHandlerCBE)));\n"
  3970. "}");
  3971. verifyFormat(
  3972. "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
  3973. verifyFormat("aaaaaaaaaaaaaaa()\n"
  3974. " .aaaaaaaaaaaaaaa()\n"
  3975. " .aaaaaaaaaaaaaaa()\n"
  3976. " .aaaaaaaaaaaaaaa()\n"
  3977. " .aaaaaaaaaaaaaaa();");
  3978. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3979. " .aaaaaaaaaaaaaaa()\n"
  3980. " .aaaaaaaaaaaaaaa()\n"
  3981. " .aaaaaaaaaaaaaaa();");
  3982. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3983. " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3984. " .aaaaaaaaaaaaaaa();");
  3985. verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3986. " ->aaaaaaaaaaaaaae(0)\n"
  3987. " ->aaaaaaaaaaaaaaa();");
  3988. // Don't linewrap after very short segments.
  3989. verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3990. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3991. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3992. verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3993. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3994. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3995. verifyFormat("aaa()\n"
  3996. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3997. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3998. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3999. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  4000. " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  4001. " .has<bbbbbbbbbbbbbbbbbbbbb>();");
  4002. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  4003. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  4004. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
  4005. // Prefer not to break after empty parentheses.
  4006. verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
  4007. " First->LastNewlineOffset);");
  4008. // Prefer not to create "hanging" indents.
  4009. verifyFormat(
  4010. "return !soooooooooooooome_map\n"
  4011. " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4012. " .second;");
  4013. verifyFormat(
  4014. "return aaaaaaaaaaaaaaaa\n"
  4015. " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
  4016. " .aaaa(aaaaaaaaaaaaaa);");
  4017. // No hanging indent here.
  4018. verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
  4019. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4020. verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
  4021. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4022. verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
  4023. " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4024. getLLVMStyleWithColumns(60));
  4025. verifyFormat("aaaaaaaaaaaaaaaaaa\n"
  4026. " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
  4027. " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4028. getLLVMStyleWithColumns(59));
  4029. verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4030. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4031. " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4032. }
  4033. TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
  4034. verifyFormat(
  4035. "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  4036. " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
  4037. verifyFormat(
  4038. "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
  4039. " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
  4040. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  4041. " ccccccccccccccccccccccccc) {\n}");
  4042. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
  4043. " ccccccccccccccccccccccccc) {\n}");
  4044. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  4045. " ccccccccccccccccccccccccc) {\n}");
  4046. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
  4047. " ccccccccccccccccccccccccc) {\n}");
  4048. verifyFormat(
  4049. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
  4050. " ccccccccccccccccccccccccc) {\n}");
  4051. verifyFormat(
  4052. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
  4053. " ccccccccccccccccccccccccc) {\n}");
  4054. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
  4055. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
  4056. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
  4057. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  4058. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
  4059. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
  4060. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
  4061. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  4062. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
  4063. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
  4064. " aaaaaaaaaaaaaaa != aa) {\n}");
  4065. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
  4066. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
  4067. " aaaaaaaaaaaaaaa != aa) {\n}");
  4068. }
  4069. TEST_F(FormatTest, BreaksAfterAssignments) {
  4070. verifyFormat(
  4071. "unsigned Cost =\n"
  4072. " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
  4073. " SI->getPointerAddressSpaceee());\n");
  4074. verifyFormat(
  4075. "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
  4076. " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
  4077. verifyFormat(
  4078. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
  4079. " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
  4080. verifyFormat("unsigned OriginalStartColumn =\n"
  4081. " SourceMgr.getSpellingColumnNumber(\n"
  4082. " Current.FormatTok.getStartOfNonWhitespace()) -\n"
  4083. " 1;");
  4084. }
  4085. TEST_F(FormatTest, AlignsAfterAssignments) {
  4086. verifyFormat(
  4087. "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4088. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  4089. verifyFormat(
  4090. "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4091. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  4092. verifyFormat(
  4093. "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4094. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  4095. verifyFormat(
  4096. "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4097. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  4098. verifyFormat(
  4099. "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4100. " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4101. " aaaaaaaaaaaaaaaaaaaaaaaa;");
  4102. }
  4103. TEST_F(FormatTest, AlignsAfterReturn) {
  4104. verifyFormat(
  4105. "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4106. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  4107. verifyFormat(
  4108. "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4109. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  4110. verifyFormat(
  4111. "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  4112. " aaaaaaaaaaaaaaaaaaaaaa();");
  4113. verifyFormat(
  4114. "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  4115. " aaaaaaaaaaaaaaaaaaaaaa());");
  4116. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4117. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4118. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4119. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
  4120. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4121. verifyFormat("return\n"
  4122. " // true if code is one of a or b.\n"
  4123. " code == a || code == b;");
  4124. }
  4125. TEST_F(FormatTest, AlignsAfterOpenBracket) {
  4126. verifyFormat(
  4127. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
  4128. " aaaaaaaaa aaaaaaa) {}");
  4129. verifyFormat(
  4130. "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
  4131. " aaaaaaaaaaa aaaaaaaaa);");
  4132. verifyFormat(
  4133. "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
  4134. " aaaaaaaaaaaaaaaaaaaaa));");
  4135. FormatStyle Style = getLLVMStyle();
  4136. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  4137. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4138. " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
  4139. Style);
  4140. verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
  4141. " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
  4142. Style);
  4143. verifyFormat("SomeLongVariableName->someFunction(\n"
  4144. " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
  4145. Style);
  4146. verifyFormat(
  4147. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
  4148. " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  4149. Style);
  4150. verifyFormat(
  4151. "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
  4152. " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4153. Style);
  4154. verifyFormat(
  4155. "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
  4156. " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
  4157. Style);
  4158. verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
  4159. " ccccccc(aaaaaaaaaaaaaaaaa, //\n"
  4160. " b));",
  4161. Style);
  4162. Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
  4163. Style.BinPackArguments = false;
  4164. Style.BinPackParameters = false;
  4165. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4166. " aaaaaaaaaaa aaaaaaaa,\n"
  4167. " aaaaaaaaa aaaaaaa,\n"
  4168. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  4169. Style);
  4170. verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
  4171. " aaaaaaaaaaa aaaaaaaaa,\n"
  4172. " aaaaaaaaaaa aaaaaaaaa,\n"
  4173. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4174. Style);
  4175. verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
  4176. " aaaaaaaaaaaaaaa,\n"
  4177. " aaaaaaaaaaaaaaaaaaaaa,\n"
  4178. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
  4179. Style);
  4180. verifyFormat(
  4181. "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
  4182. " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
  4183. Style);
  4184. verifyFormat(
  4185. "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
  4186. " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
  4187. Style);
  4188. verifyFormat(
  4189. "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4190. " aaaaaaaaaaaaaaaaaaaaa(\n"
  4191. " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
  4192. " aaaaaaaaaaaaaaaa);",
  4193. Style);
  4194. verifyFormat(
  4195. "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4196. " aaaaaaaaaaaaaaaaaaaaa(\n"
  4197. " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
  4198. " aaaaaaaaaaaaaaaa);",
  4199. Style);
  4200. }
  4201. TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
  4202. FormatStyle Style = getLLVMStyleWithColumns(40);
  4203. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4204. " bbbbbbbbbbbbbbbbbbbbbb);",
  4205. Style);
  4206. Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
  4207. Style.AlignOperands = false;
  4208. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4209. " bbbbbbbbbbbbbbbbbbbbbb);",
  4210. Style);
  4211. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  4212. Style.AlignOperands = true;
  4213. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4214. " bbbbbbbbbbbbbbbbbbbbbb);",
  4215. Style);
  4216. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  4217. Style.AlignOperands = false;
  4218. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4219. " bbbbbbbbbbbbbbbbbbbbbb);",
  4220. Style);
  4221. }
  4222. TEST_F(FormatTest, BreaksConditionalExpressions) {
  4223. verifyFormat(
  4224. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4225. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4226. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4227. verifyFormat(
  4228. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4229. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4230. verifyFormat(
  4231. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
  4232. " : aaaaaaaaaaaaa);");
  4233. verifyFormat(
  4234. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4235. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4236. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4237. " aaaaaaaaaaaaa);");
  4238. verifyFormat(
  4239. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4240. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4241. " aaaaaaaaaaaaa);");
  4242. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4243. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4244. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4245. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4246. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4247. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4248. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4249. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4250. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4251. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4252. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4253. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4254. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4255. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4256. " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4257. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4258. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4259. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4260. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4261. " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4262. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  4263. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4264. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4265. " : aaaaaaaaaaaaaaaa;");
  4266. verifyFormat(
  4267. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4268. " ? aaaaaaaaaaaaaaa\n"
  4269. " : aaaaaaaaaaaaaaa;");
  4270. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  4271. " aaaaaaaaa\n"
  4272. " ? b\n"
  4273. " : c);");
  4274. verifyFormat("return aaaa == bbbb\n"
  4275. " // comment\n"
  4276. " ? aaaa\n"
  4277. " : bbbb;");
  4278. verifyFormat("unsigned Indent =\n"
  4279. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
  4280. " ? IndentForLevel[TheLine.Level]\n"
  4281. " : TheLine * 2,\n"
  4282. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  4283. getLLVMStyleWithColumns(70));
  4284. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  4285. " ? aaaaaaaaaaaaaaa\n"
  4286. " : bbbbbbbbbbbbbbb //\n"
  4287. " ? ccccccccccccccc\n"
  4288. " : ddddddddddddddd;");
  4289. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  4290. " ? aaaaaaaaaaaaaaa\n"
  4291. " : (bbbbbbbbbbbbbbb //\n"
  4292. " ? ccccccccccccccc\n"
  4293. " : ddddddddddddddd);");
  4294. verifyFormat(
  4295. "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4296. " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4297. " aaaaaaaaaaaaaaaaaaaaa +\n"
  4298. " aaaaaaaaaaaaaaaaaaaaa\n"
  4299. " : aaaaaaaaaa;");
  4300. verifyFormat(
  4301. "aaaaaa = aaaaaaaaaaaa\n"
  4302. " ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4303. " : aaaaaaaaaaaaaaaaaaaaaa\n"
  4304. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4305. FormatStyle NoBinPacking = getLLVMStyle();
  4306. NoBinPacking.BinPackArguments = false;
  4307. verifyFormat(
  4308. "void f() {\n"
  4309. " g(aaa,\n"
  4310. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  4311. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4312. " ? aaaaaaaaaaaaaaa\n"
  4313. " : aaaaaaaaaaaaaaa);\n"
  4314. "}",
  4315. NoBinPacking);
  4316. verifyFormat(
  4317. "void f() {\n"
  4318. " g(aaa,\n"
  4319. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  4320. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4321. " ?: aaaaaaaaaaaaaaa);\n"
  4322. "}",
  4323. NoBinPacking);
  4324. verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
  4325. " // comment.\n"
  4326. " ccccccccccccccccccccccccccccccccccccccc\n"
  4327. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4328. " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
  4329. // Assignments in conditional expressions. Apparently not uncommon :-(.
  4330. verifyFormat("return a != b\n"
  4331. " // comment\n"
  4332. " ? a = b\n"
  4333. " : a = b;");
  4334. verifyFormat("return a != b\n"
  4335. " // comment\n"
  4336. " ? a = a != b\n"
  4337. " // comment\n"
  4338. " ? a = b\n"
  4339. " : a\n"
  4340. " : a;\n");
  4341. verifyFormat("return a != b\n"
  4342. " // comment\n"
  4343. " ? a\n"
  4344. " : a = a != b\n"
  4345. " // comment\n"
  4346. " ? a = b\n"
  4347. " : a;");
  4348. }
  4349. TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
  4350. FormatStyle Style = getLLVMStyle();
  4351. Style.BreakBeforeTernaryOperators = false;
  4352. Style.ColumnLimit = 70;
  4353. verifyFormat(
  4354. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4355. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4356. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4357. Style);
  4358. verifyFormat(
  4359. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4360. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4361. Style);
  4362. verifyFormat(
  4363. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
  4364. " aaaaaaaaaaaaa);",
  4365. Style);
  4366. verifyFormat(
  4367. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4368. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4369. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4370. " aaaaaaaaaaaaa);",
  4371. Style);
  4372. verifyFormat(
  4373. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4374. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4375. " aaaaaaaaaaaaa);",
  4376. Style);
  4377. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4378. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4379. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  4380. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4381. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4382. Style);
  4383. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4384. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4385. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4386. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  4387. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4388. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4389. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4390. Style);
  4391. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4392. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
  4393. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4394. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4395. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4396. Style);
  4397. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4398. " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4399. " aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4400. Style);
  4401. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  4402. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4403. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4404. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4405. Style);
  4406. verifyFormat(
  4407. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4408. " aaaaaaaaaaaaaaa :\n"
  4409. " aaaaaaaaaaaaaaa;",
  4410. Style);
  4411. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  4412. " aaaaaaaaa ?\n"
  4413. " b :\n"
  4414. " c);",
  4415. Style);
  4416. verifyFormat(
  4417. "unsigned Indent =\n"
  4418. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n"
  4419. " IndentForLevel[TheLine.Level] :\n"
  4420. " TheLine * 2,\n"
  4421. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  4422. Style);
  4423. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  4424. " aaaaaaaaaaaaaaa :\n"
  4425. " bbbbbbbbbbbbbbb ? //\n"
  4426. " ccccccccccccccc :\n"
  4427. " ddddddddddddddd;",
  4428. Style);
  4429. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  4430. " aaaaaaaaaaaaaaa :\n"
  4431. " (bbbbbbbbbbbbbbb ? //\n"
  4432. " ccccccccccccccc :\n"
  4433. " ddddddddddddddd);",
  4434. Style);
  4435. verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4436. " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
  4437. " ccccccccccccccccccccccccccc;",
  4438. Style);
  4439. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4440. " aaaaa :\n"
  4441. " bbbbbbbbbbbbbbb + cccccccccccccccc;",
  4442. Style);
  4443. }
  4444. TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
  4445. verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
  4446. " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
  4447. verifyFormat("bool a = true, b = false;");
  4448. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4449. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
  4450. " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
  4451. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
  4452. verifyFormat(
  4453. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  4454. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
  4455. " d = e && f;");
  4456. verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
  4457. " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
  4458. verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  4459. " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
  4460. verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
  4461. " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
  4462. FormatStyle Style = getGoogleStyle();
  4463. Style.PointerAlignment = FormatStyle::PAS_Left;
  4464. Style.DerivePointerAlignment = false;
  4465. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4466. " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
  4467. " *b = bbbbbbbbbbbbbbbbbbb;",
  4468. Style);
  4469. verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  4470. " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
  4471. Style);
  4472. }
  4473. TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
  4474. verifyFormat("arr[foo ? bar : baz];");
  4475. verifyFormat("f()[foo ? bar : baz];");
  4476. verifyFormat("(a + b)[foo ? bar : baz];");
  4477. verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
  4478. }
  4479. TEST_F(FormatTest, AlignsStringLiterals) {
  4480. verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
  4481. " \"short literal\");");
  4482. verifyFormat(
  4483. "looooooooooooooooooooooooongFunction(\n"
  4484. " \"short literal\"\n"
  4485. " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
  4486. verifyFormat("someFunction(\"Always break between multi-line\"\n"
  4487. " \" string literals\",\n"
  4488. " and, other, parameters);");
  4489. EXPECT_EQ("fun + \"1243\" /* comment */\n"
  4490. " \"5678\";",
  4491. format("fun + \"1243\" /* comment */\n"
  4492. " \"5678\";",
  4493. getLLVMStyleWithColumns(28)));
  4494. EXPECT_EQ(
  4495. "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  4496. " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
  4497. " \"aaaaaaaaaaaaaaaa\";",
  4498. format("aaaaaa ="
  4499. "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  4500. "aaaaaaaaaaaaaaaaaaaaa\" "
  4501. "\"aaaaaaaaaaaaaaaa\";"));
  4502. verifyFormat("a = a + \"a\"\n"
  4503. " \"a\"\n"
  4504. " \"a\";");
  4505. verifyFormat("f(\"a\", \"b\"\n"
  4506. " \"c\");");
  4507. verifyFormat(
  4508. "#define LL_FORMAT \"ll\"\n"
  4509. "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
  4510. " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
  4511. verifyFormat("#define A(X) \\\n"
  4512. " \"aaaaa\" #X \"bbbbbb\" \\\n"
  4513. " \"ccccc\"",
  4514. getLLVMStyleWithColumns(23));
  4515. verifyFormat("#define A \"def\"\n"
  4516. "f(\"abc\" A \"ghi\"\n"
  4517. " \"jkl\");");
  4518. verifyFormat("f(L\"a\"\n"
  4519. " L\"b\");");
  4520. verifyFormat("#define A(X) \\\n"
  4521. " L\"aaaaa\" #X L\"bbbbbb\" \\\n"
  4522. " L\"ccccc\"",
  4523. getLLVMStyleWithColumns(25));
  4524. verifyFormat("f(@\"a\"\n"
  4525. " @\"b\");");
  4526. verifyFormat("NSString s = @\"a\"\n"
  4527. " @\"b\"\n"
  4528. " @\"c\";");
  4529. verifyFormat("NSString s = @\"a\"\n"
  4530. " \"b\"\n"
  4531. " \"c\";");
  4532. }
  4533. TEST_F(FormatTest, ReturnTypeBreakingStyle) {
  4534. FormatStyle Style = getLLVMStyle();
  4535. // No declarations or definitions should be moved to own line.
  4536. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
  4537. verifyFormat("class A {\n"
  4538. " int f() { return 1; }\n"
  4539. " int g();\n"
  4540. "};\n"
  4541. "int f() { return 1; }\n"
  4542. "int g();\n",
  4543. Style);
  4544. // All declarations and definitions should have the return type moved to its
  4545. // own
  4546. // line.
  4547. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
  4548. verifyFormat("class E {\n"
  4549. " int\n"
  4550. " f() {\n"
  4551. " return 1;\n"
  4552. " }\n"
  4553. " int\n"
  4554. " g();\n"
  4555. "};\n"
  4556. "int\n"
  4557. "f() {\n"
  4558. " return 1;\n"
  4559. "}\n"
  4560. "int\n"
  4561. "g();\n",
  4562. Style);
  4563. // Top-level definitions, and no kinds of declarations should have the
  4564. // return type moved to its own line.
  4565. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
  4566. verifyFormat("class B {\n"
  4567. " int f() { return 1; }\n"
  4568. " int g();\n"
  4569. "};\n"
  4570. "int\n"
  4571. "f() {\n"
  4572. " return 1;\n"
  4573. "}\n"
  4574. "int g();\n",
  4575. Style);
  4576. // Top-level definitions and declarations should have the return type moved
  4577. // to its own line.
  4578. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
  4579. verifyFormat("class C {\n"
  4580. " int f() { return 1; }\n"
  4581. " int g();\n"
  4582. "};\n"
  4583. "int\n"
  4584. "f() {\n"
  4585. " return 1;\n"
  4586. "}\n"
  4587. "int\n"
  4588. "g();\n",
  4589. Style);
  4590. // All definitions should have the return type moved to its own line, but no
  4591. // kinds of declarations.
  4592. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
  4593. verifyFormat("class D {\n"
  4594. " int\n"
  4595. " f() {\n"
  4596. " return 1;\n"
  4597. " }\n"
  4598. " int g();\n"
  4599. "};\n"
  4600. "int\n"
  4601. "f() {\n"
  4602. " return 1;\n"
  4603. "}\n"
  4604. "int g();\n",
  4605. Style);
  4606. verifyFormat("const char *\n"
  4607. "f(void) {\n" // Break here.
  4608. " return \"\";\n"
  4609. "}\n"
  4610. "const char *bar(void);\n", // No break here.
  4611. Style);
  4612. verifyFormat("template <class T>\n"
  4613. "T *\n"
  4614. "f(T &c) {\n" // Break here.
  4615. " return NULL;\n"
  4616. "}\n"
  4617. "template <class T> T *f(T &c);\n", // No break here.
  4618. Style);
  4619. verifyFormat("class C {\n"
  4620. " int\n"
  4621. " operator+() {\n"
  4622. " return 1;\n"
  4623. " }\n"
  4624. " int\n"
  4625. " operator()() {\n"
  4626. " return 1;\n"
  4627. " }\n"
  4628. "};\n",
  4629. Style);
  4630. verifyFormat("void\n"
  4631. "A::operator()() {}\n"
  4632. "void\n"
  4633. "A::operator>>() {}\n"
  4634. "void\n"
  4635. "A::operator+() {}\n",
  4636. Style);
  4637. verifyFormat("void *operator new(std::size_t s);", // No break here.
  4638. Style);
  4639. verifyFormat("void *\n"
  4640. "operator new(std::size_t s) {}",
  4641. Style);
  4642. verifyFormat("void *\n"
  4643. "operator delete[](void *ptr) {}",
  4644. Style);
  4645. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  4646. verifyFormat("const char *\n"
  4647. "f(void)\n" // Break here.
  4648. "{\n"
  4649. " return \"\";\n"
  4650. "}\n"
  4651. "const char *bar(void);\n", // No break here.
  4652. Style);
  4653. verifyFormat("template <class T>\n"
  4654. "T *\n" // Problem here: no line break
  4655. "f(T &c)\n" // Break here.
  4656. "{\n"
  4657. " return NULL;\n"
  4658. "}\n"
  4659. "template <class T> T *f(T &c);\n", // No break here.
  4660. Style);
  4661. }
  4662. TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
  4663. FormatStyle NoBreak = getLLVMStyle();
  4664. NoBreak.AlwaysBreakBeforeMultilineStrings = false;
  4665. FormatStyle Break = getLLVMStyle();
  4666. Break.AlwaysBreakBeforeMultilineStrings = true;
  4667. verifyFormat("aaaa = \"bbbb\"\n"
  4668. " \"cccc\";",
  4669. NoBreak);
  4670. verifyFormat("aaaa =\n"
  4671. " \"bbbb\"\n"
  4672. " \"cccc\";",
  4673. Break);
  4674. verifyFormat("aaaa(\"bbbb\"\n"
  4675. " \"cccc\");",
  4676. NoBreak);
  4677. verifyFormat("aaaa(\n"
  4678. " \"bbbb\"\n"
  4679. " \"cccc\");",
  4680. Break);
  4681. verifyFormat("aaaa(qqq, \"bbbb\"\n"
  4682. " \"cccc\");",
  4683. NoBreak);
  4684. verifyFormat("aaaa(qqq,\n"
  4685. " \"bbbb\"\n"
  4686. " \"cccc\");",
  4687. Break);
  4688. verifyFormat("aaaa(qqq,\n"
  4689. " L\"bbbb\"\n"
  4690. " L\"cccc\");",
  4691. Break);
  4692. verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
  4693. " \"bbbb\"));",
  4694. Break);
  4695. verifyFormat("string s = someFunction(\n"
  4696. " \"abc\"\n"
  4697. " \"abc\");",
  4698. Break);
  4699. // As we break before unary operators, breaking right after them is bad.
  4700. verifyFormat("string foo = abc ? \"x\"\n"
  4701. " \"blah blah blah blah blah blah\"\n"
  4702. " : \"y\";",
  4703. Break);
  4704. // Don't break if there is no column gain.
  4705. verifyFormat("f(\"aaaa\"\n"
  4706. " \"bbbb\");",
  4707. Break);
  4708. // Treat literals with escaped newlines like multi-line string literals.
  4709. EXPECT_EQ("x = \"a\\\n"
  4710. "b\\\n"
  4711. "c\";",
  4712. format("x = \"a\\\n"
  4713. "b\\\n"
  4714. "c\";",
  4715. NoBreak));
  4716. EXPECT_EQ("xxxx =\n"
  4717. " \"a\\\n"
  4718. "b\\\n"
  4719. "c\";",
  4720. format("xxxx = \"a\\\n"
  4721. "b\\\n"
  4722. "c\";",
  4723. Break));
  4724. // Exempt ObjC strings for now.
  4725. EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
  4726. " @\"bbbb\";",
  4727. format("NSString *const kString = @\"aaaa\"\n"
  4728. "@\"bbbb\";",
  4729. Break));
  4730. Break.ColumnLimit = 0;
  4731. verifyFormat("const char *hello = \"hello llvm\";", Break);
  4732. }
  4733. TEST_F(FormatTest, AlignsPipes) {
  4734. verifyFormat(
  4735. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4736. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4737. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4738. verifyFormat(
  4739. "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
  4740. " << aaaaaaaaaaaaaaaaaaaa;");
  4741. verifyFormat(
  4742. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4743. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4744. verifyFormat(
  4745. "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
  4746. " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
  4747. " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
  4748. verifyFormat(
  4749. "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4750. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4751. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4752. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4753. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4754. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4755. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4756. verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
  4757. " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
  4758. verifyFormat(
  4759. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4760. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4761. verifyFormat("return out << \"somepacket = {\\n\"\n"
  4762. " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
  4763. " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
  4764. " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
  4765. " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
  4766. " << \"}\";");
  4767. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  4768. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  4769. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
  4770. verifyFormat(
  4771. "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
  4772. " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
  4773. " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
  4774. " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
  4775. " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
  4776. verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
  4777. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4778. verifyFormat(
  4779. "void f() {\n"
  4780. " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
  4781. " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  4782. "}");
  4783. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
  4784. " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
  4785. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4786. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4787. " aaaaaaaaaaaaaaaaaaaaa)\n"
  4788. " << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4789. verifyFormat("LOG_IF(aaa == //\n"
  4790. " bbb)\n"
  4791. " << a << b;");
  4792. // Breaking before the first "<<" is generally not desirable.
  4793. verifyFormat(
  4794. "llvm::errs()\n"
  4795. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4796. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4797. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4798. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4799. getLLVMStyleWithColumns(70));
  4800. verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4801. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4802. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4803. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4804. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4805. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4806. getLLVMStyleWithColumns(70));
  4807. // But sometimes, breaking before the first "<<" is desirable.
  4808. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
  4809. " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
  4810. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
  4811. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4812. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4813. verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
  4814. " << BEF << IsTemplate << Description << E->getType();");
  4815. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
  4816. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4817. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4818. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
  4819. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4820. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4821. " << aaa;");
  4822. verifyFormat(
  4823. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4824. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4825. // Incomplete string literal.
  4826. EXPECT_EQ("llvm::errs() << \"\n"
  4827. " << a;",
  4828. format("llvm::errs() << \"\n<<a;"));
  4829. verifyFormat("void f() {\n"
  4830. " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
  4831. " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
  4832. "}");
  4833. // Handle 'endl'.
  4834. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
  4835. " << bbbbbbbbbbbbbbbbbbbbbb << endl;");
  4836. verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
  4837. // Handle '\n'.
  4838. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
  4839. " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
  4840. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
  4841. " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
  4842. verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
  4843. " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
  4844. verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
  4845. }
  4846. TEST_F(FormatTest, UnderstandsEquals) {
  4847. verifyFormat(
  4848. "aaaaaaaaaaaaaaaaa =\n"
  4849. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4850. verifyFormat(
  4851. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4852. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  4853. verifyFormat(
  4854. "if (a) {\n"
  4855. " f();\n"
  4856. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4857. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  4858. "}");
  4859. verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4860. " 100000000 + 10000000) {\n}");
  4861. }
  4862. TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
  4863. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  4864. " .looooooooooooooooooooooooooooooooooooooongFunction();");
  4865. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  4866. " ->looooooooooooooooooooooooooooooooooooooongFunction();");
  4867. verifyFormat(
  4868. "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
  4869. " Parameter2);");
  4870. verifyFormat(
  4871. "ShortObject->shortFunction(\n"
  4872. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
  4873. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
  4874. verifyFormat("loooooooooooooongFunction(\n"
  4875. " LoooooooooooooongObject->looooooooooooooooongFunction());");
  4876. verifyFormat(
  4877. "function(LoooooooooooooooooooooooooooooooooooongObject\n"
  4878. " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
  4879. verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  4880. " .WillRepeatedly(Return(SomeValue));");
  4881. verifyFormat("void f() {\n"
  4882. " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  4883. " .Times(2)\n"
  4884. " .WillRepeatedly(Return(SomeValue));\n"
  4885. "}");
  4886. verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
  4887. " ccccccccccccccccccccccc);");
  4888. verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4889. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4890. " .aaaaa(aaaaa),\n"
  4891. " aaaaaaaaaaaaaaaaaaaaa);");
  4892. verifyFormat("void f() {\n"
  4893. " aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4894. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
  4895. "}");
  4896. verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4897. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4898. " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4899. " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4900. " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  4901. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4902. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4903. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4904. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
  4905. "}");
  4906. // Here, it is not necessary to wrap at "." or "->".
  4907. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
  4908. " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  4909. verifyFormat(
  4910. "aaaaaaaaaaa->aaaaaaaaa(\n"
  4911. " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4912. " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
  4913. verifyFormat(
  4914. "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4915. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
  4916. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
  4917. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  4918. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
  4919. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  4920. verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4921. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4922. " .a();");
  4923. FormatStyle NoBinPacking = getLLVMStyle();
  4924. NoBinPacking.BinPackParameters = false;
  4925. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  4926. " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  4927. " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
  4928. " aaaaaaaaaaaaaaaaaaa,\n"
  4929. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4930. NoBinPacking);
  4931. // If there is a subsequent call, change to hanging indentation.
  4932. verifyFormat(
  4933. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4934. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
  4935. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4936. verifyFormat(
  4937. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4938. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
  4939. verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4940. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4941. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4942. verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4943. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4944. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  4945. }
  4946. TEST_F(FormatTest, WrapsTemplateDeclarations) {
  4947. verifyFormat("template <typename T>\n"
  4948. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  4949. verifyFormat("template <typename T>\n"
  4950. "// T should be one of {A, B}.\n"
  4951. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  4952. verifyFormat(
  4953. "template <typename T>\n"
  4954. "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
  4955. verifyFormat("template <typename T>\n"
  4956. "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
  4957. " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
  4958. verifyFormat(
  4959. "template <typename T>\n"
  4960. "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
  4961. " int Paaaaaaaaaaaaaaaaaaaaram2);");
  4962. verifyFormat(
  4963. "template <typename T>\n"
  4964. "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
  4965. " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
  4966. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4967. verifyFormat("template <typename T>\n"
  4968. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4969. " int aaaaaaaaaaaaaaaaaaaaaa);");
  4970. verifyFormat(
  4971. "template <typename T1, typename T2 = char, typename T3 = char,\n"
  4972. " typename T4 = char>\n"
  4973. "void f();");
  4974. verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
  4975. " template <typename> class cccccccccccccccccccccc,\n"
  4976. " typename ddddddddddddd>\n"
  4977. "class C {};");
  4978. verifyFormat(
  4979. "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
  4980. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4981. verifyFormat("void f() {\n"
  4982. " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
  4983. " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
  4984. "}");
  4985. verifyFormat("template <typename T> class C {};");
  4986. verifyFormat("template <typename T> void f();");
  4987. verifyFormat("template <typename T> void f() {}");
  4988. verifyFormat(
  4989. "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  4990. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4991. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
  4992. " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  4993. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4994. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
  4995. " bbbbbbbbbbbbbbbbbbbbbbbb);",
  4996. getLLVMStyleWithColumns(72));
  4997. EXPECT_EQ("static_cast<A< //\n"
  4998. " B> *>(\n"
  4999. "\n"
  5000. " );",
  5001. format("static_cast<A<//\n"
  5002. " B>*>(\n"
  5003. "\n"
  5004. " );"));
  5005. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5006. " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
  5007. FormatStyle AlwaysBreak = getLLVMStyle();
  5008. AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
  5009. verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
  5010. verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
  5011. verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
  5012. verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5013. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
  5014. " ccccccccccccccccccccccccccccccccccccccccccccccc);");
  5015. verifyFormat("template <template <typename> class Fooooooo,\n"
  5016. " template <typename> class Baaaaaaar>\n"
  5017. "struct C {};",
  5018. AlwaysBreak);
  5019. verifyFormat("template <typename T> // T can be A, B or C.\n"
  5020. "struct C {};",
  5021. AlwaysBreak);
  5022. }
  5023. TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
  5024. verifyFormat(
  5025. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  5026. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  5027. verifyFormat(
  5028. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  5029. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5030. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  5031. // FIXME: Should we have the extra indent after the second break?
  5032. verifyFormat(
  5033. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  5034. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  5035. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  5036. verifyFormat(
  5037. "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
  5038. " cccccccccccccccccccccccccccccccccccccccccccccc());");
  5039. // Breaking at nested name specifiers is generally not desirable.
  5040. verifyFormat(
  5041. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5042. " aaaaaaaaaaaaaaaaaaaaaaa);");
  5043. verifyFormat(
  5044. "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  5045. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5046. " aaaaaaaaaaaaaaaaaaaaa);",
  5047. getLLVMStyleWithColumns(74));
  5048. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  5049. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5050. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  5051. }
  5052. TEST_F(FormatTest, UnderstandsTemplateParameters) {
  5053. verifyFormat("A<int> a;");
  5054. verifyFormat("A<A<A<int>>> a;");
  5055. verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
  5056. verifyFormat("bool x = a < 1 || 2 > a;");
  5057. verifyFormat("bool x = 5 < f<int>();");
  5058. verifyFormat("bool x = f<int>() > 5;");
  5059. verifyFormat("bool x = 5 < a<int>::x;");
  5060. verifyFormat("bool x = a < 4 ? a > 2 : false;");
  5061. verifyFormat("bool x = f() ? a < 2 : a > 2;");
  5062. verifyGoogleFormat("A<A<int>> a;");
  5063. verifyGoogleFormat("A<A<A<int>>> a;");
  5064. verifyGoogleFormat("A<A<A<A<int>>>> a;");
  5065. verifyGoogleFormat("A<A<int> > a;");
  5066. verifyGoogleFormat("A<A<A<int> > > a;");
  5067. verifyGoogleFormat("A<A<A<A<int> > > > a;");
  5068. verifyGoogleFormat("A<::A<int>> a;");
  5069. verifyGoogleFormat("A<::A> a;");
  5070. verifyGoogleFormat("A< ::A> a;");
  5071. verifyGoogleFormat("A< ::A<int> > a;");
  5072. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
  5073. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
  5074. EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
  5075. EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
  5076. EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
  5077. format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
  5078. verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
  5079. verifyFormat("test >> a >> b;");
  5080. verifyFormat("test << a >> b;");
  5081. verifyFormat("f<int>();");
  5082. verifyFormat("template <typename T> void f() {}");
  5083. verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
  5084. verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
  5085. "sizeof(char)>::type>;");
  5086. verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
  5087. verifyFormat("f(a.operator()<A>());");
  5088. verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5089. " .template operator()<A>());",
  5090. getLLVMStyleWithColumns(35));
  5091. // Not template parameters.
  5092. verifyFormat("return a < b && c > d;");
  5093. verifyFormat("void f() {\n"
  5094. " while (a < b && c > d) {\n"
  5095. " }\n"
  5096. "}");
  5097. verifyFormat("template <typename... Types>\n"
  5098. "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
  5099. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5100. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
  5101. getLLVMStyleWithColumns(60));
  5102. verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
  5103. verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
  5104. verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
  5105. }
  5106. TEST_F(FormatTest, UnderstandsBinaryOperators) {
  5107. verifyFormat("COMPARE(a, ==, b);");
  5108. }
  5109. TEST_F(FormatTest, UnderstandsPointersToMembers) {
  5110. verifyFormat("int A::*x;");
  5111. verifyFormat("int (S::*func)(void *);");
  5112. verifyFormat("void f() { int (S::*func)(void *); }");
  5113. verifyFormat("typedef bool *(Class::*Member)() const;");
  5114. verifyFormat("void f() {\n"
  5115. " (a->*f)();\n"
  5116. " a->*x;\n"
  5117. " (a.*f)();\n"
  5118. " ((*a).*f)();\n"
  5119. " a.*x;\n"
  5120. "}");
  5121. verifyFormat("void f() {\n"
  5122. " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  5123. " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
  5124. "}");
  5125. verifyFormat(
  5126. "(aaaaaaaaaa->*bbbbbbb)(\n"
  5127. " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  5128. FormatStyle Style = getLLVMStyle();
  5129. Style.PointerAlignment = FormatStyle::PAS_Left;
  5130. verifyFormat("typedef bool* (Class::*Member)() const;", Style);
  5131. }
  5132. TEST_F(FormatTest, UnderstandsUnaryOperators) {
  5133. verifyFormat("int a = -2;");
  5134. verifyFormat("f(-1, -2, -3);");
  5135. verifyFormat("a[-1] = 5;");
  5136. verifyFormat("int a = 5 + -2;");
  5137. verifyFormat("if (i == -1) {\n}");
  5138. verifyFormat("if (i != -1) {\n}");
  5139. verifyFormat("if (i > -1) {\n}");
  5140. verifyFormat("if (i < -1) {\n}");
  5141. verifyFormat("++(a->f());");
  5142. verifyFormat("--(a->f());");
  5143. verifyFormat("(a->f())++;");
  5144. verifyFormat("a[42]++;");
  5145. verifyFormat("if (!(a->f())) {\n}");
  5146. verifyFormat("a-- > b;");
  5147. verifyFormat("b ? -a : c;");
  5148. verifyFormat("n * sizeof char16;");
  5149. verifyFormat("n * alignof char16;", getGoogleStyle());
  5150. verifyFormat("sizeof(char);");
  5151. verifyFormat("alignof(char);", getGoogleStyle());
  5152. verifyFormat("return -1;");
  5153. verifyFormat("switch (a) {\n"
  5154. "case -1:\n"
  5155. " break;\n"
  5156. "}");
  5157. verifyFormat("#define X -1");
  5158. verifyFormat("#define X -kConstant");
  5159. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
  5160. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
  5161. verifyFormat("int a = /* confusing comment */ -1;");
  5162. // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
  5163. verifyFormat("int a = i /* confusing comment */++;");
  5164. }
  5165. TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
  5166. verifyFormat("if (!aaaaaaaaaa( // break\n"
  5167. " aaaaa)) {\n"
  5168. "}");
  5169. verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
  5170. " aaaaa));");
  5171. verifyFormat("*aaa = aaaaaaa( // break\n"
  5172. " bbbbbb);");
  5173. }
  5174. TEST_F(FormatTest, UnderstandsOverloadedOperators) {
  5175. verifyFormat("bool operator<();");
  5176. verifyFormat("bool operator>();");
  5177. verifyFormat("bool operator=();");
  5178. verifyFormat("bool operator==();");
  5179. verifyFormat("bool operator!=();");
  5180. verifyFormat("int operator+();");
  5181. verifyFormat("int operator++();");
  5182. verifyFormat("bool operator,();");
  5183. verifyFormat("bool operator();");
  5184. verifyFormat("bool operator()();");
  5185. verifyFormat("bool operator[]();");
  5186. verifyFormat("operator bool();");
  5187. verifyFormat("operator int();");
  5188. verifyFormat("operator void *();");
  5189. verifyFormat("operator SomeType<int>();");
  5190. verifyFormat("operator SomeType<int, int>();");
  5191. verifyFormat("operator SomeType<SomeType<int>>();");
  5192. verifyFormat("void *operator new(std::size_t size);");
  5193. verifyFormat("void *operator new[](std::size_t size);");
  5194. verifyFormat("void operator delete(void *ptr);");
  5195. verifyFormat("void operator delete[](void *ptr);");
  5196. verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
  5197. "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
  5198. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
  5199. " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
  5200. verifyFormat(
  5201. "ostream &operator<<(ostream &OutputStream,\n"
  5202. " SomeReallyLongType WithSomeReallyLongValue);");
  5203. verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
  5204. " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
  5205. " return left.group < right.group;\n"
  5206. "}");
  5207. verifyFormat("SomeType &operator=(const SomeType &S);");
  5208. verifyFormat("f.template operator()<int>();");
  5209. verifyGoogleFormat("operator void*();");
  5210. verifyGoogleFormat("operator SomeType<SomeType<int>>();");
  5211. verifyGoogleFormat("operator ::A();");
  5212. verifyFormat("using A::operator+;");
  5213. verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
  5214. "int i;");
  5215. }
  5216. TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
  5217. verifyFormat("Deleted &operator=(const Deleted &) & = default;");
  5218. verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
  5219. verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
  5220. verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
  5221. verifyFormat("Deleted &operator=(const Deleted &) &;");
  5222. verifyFormat("Deleted &operator=(const Deleted &) &&;");
  5223. verifyFormat("SomeType MemberFunction(const Deleted &) &;");
  5224. verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
  5225. verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
  5226. verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
  5227. verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
  5228. FormatStyle AlignLeft = getLLVMStyle();
  5229. AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
  5230. verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
  5231. verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
  5232. AlignLeft);
  5233. verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
  5234. verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
  5235. FormatStyle Spaces = getLLVMStyle();
  5236. Spaces.SpacesInCStyleCastParentheses = true;
  5237. verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
  5238. verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
  5239. verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
  5240. verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
  5241. Spaces.SpacesInCStyleCastParentheses = false;
  5242. Spaces.SpacesInParentheses = true;
  5243. verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
  5244. verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
  5245. verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
  5246. verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
  5247. }
  5248. TEST_F(FormatTest, UnderstandsNewAndDelete) {
  5249. verifyFormat("void f() {\n"
  5250. " A *a = new A;\n"
  5251. " A *a = new (placement) A;\n"
  5252. " delete a;\n"
  5253. " delete (A *)a;\n"
  5254. "}");
  5255. verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
  5256. " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
  5257. verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  5258. " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
  5259. " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
  5260. verifyFormat("delete[] h->p;");
  5261. }
  5262. TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
  5263. verifyFormat("int *f(int *a) {}");
  5264. verifyFormat("int main(int argc, char **argv) {}");
  5265. verifyFormat("Test::Test(int b) : a(b * b) {}");
  5266. verifyIndependentOfContext("f(a, *a);");
  5267. verifyFormat("void g() { f(*a); }");
  5268. verifyIndependentOfContext("int a = b * 10;");
  5269. verifyIndependentOfContext("int a = 10 * b;");
  5270. verifyIndependentOfContext("int a = b * c;");
  5271. verifyIndependentOfContext("int a += b * c;");
  5272. verifyIndependentOfContext("int a -= b * c;");
  5273. verifyIndependentOfContext("int a *= b * c;");
  5274. verifyIndependentOfContext("int a /= b * c;");
  5275. verifyIndependentOfContext("int a = *b;");
  5276. verifyIndependentOfContext("int a = *b * c;");
  5277. verifyIndependentOfContext("int a = b * *c;");
  5278. verifyIndependentOfContext("int a = b * (10);");
  5279. verifyIndependentOfContext("S << b * (10);");
  5280. verifyIndependentOfContext("return 10 * b;");
  5281. verifyIndependentOfContext("return *b * *c;");
  5282. verifyIndependentOfContext("return a & ~b;");
  5283. verifyIndependentOfContext("f(b ? *c : *d);");
  5284. verifyIndependentOfContext("int a = b ? *c : *d;");
  5285. verifyIndependentOfContext("*b = a;");
  5286. verifyIndependentOfContext("a * ~b;");
  5287. verifyIndependentOfContext("a * !b;");
  5288. verifyIndependentOfContext("a * +b;");
  5289. verifyIndependentOfContext("a * -b;");
  5290. verifyIndependentOfContext("a * ++b;");
  5291. verifyIndependentOfContext("a * --b;");
  5292. verifyIndependentOfContext("a[4] * b;");
  5293. verifyIndependentOfContext("a[a * a] = 1;");
  5294. verifyIndependentOfContext("f() * b;");
  5295. verifyIndependentOfContext("a * [self dostuff];");
  5296. verifyIndependentOfContext("int x = a * (a + b);");
  5297. verifyIndependentOfContext("(a *)(a + b);");
  5298. verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
  5299. verifyIndependentOfContext("int *pa = (int *)&a;");
  5300. verifyIndependentOfContext("return sizeof(int **);");
  5301. verifyIndependentOfContext("return sizeof(int ******);");
  5302. verifyIndependentOfContext("return (int **&)a;");
  5303. verifyIndependentOfContext("f((*PointerToArray)[10]);");
  5304. verifyFormat("void f(Type (*parameter)[10]) {}");
  5305. verifyFormat("void f(Type (&parameter)[10]) {}");
  5306. verifyGoogleFormat("return sizeof(int**);");
  5307. verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
  5308. verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
  5309. verifyFormat("auto a = [](int **&, int ***) {};");
  5310. verifyFormat("auto PointerBinding = [](const char *S) {};");
  5311. verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
  5312. verifyFormat("[](const decltype(*a) &value) {}");
  5313. verifyFormat("decltype(a * b) F();");
  5314. verifyFormat("#define MACRO() [](A *a) { return 1; }");
  5315. verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
  5316. verifyIndependentOfContext("typedef void (*f)(int *a);");
  5317. verifyIndependentOfContext("int i{a * b};");
  5318. verifyIndependentOfContext("aaa && aaa->f();");
  5319. verifyIndependentOfContext("int x = ~*p;");
  5320. verifyFormat("Constructor() : a(a), area(width * height) {}");
  5321. verifyFormat("Constructor() : a(a), area(a, width * height) {}");
  5322. verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
  5323. verifyFormat("void f() { f(a, c * d); }");
  5324. verifyFormat("void f() { f(new a(), c * d); }");
  5325. verifyIndependentOfContext("InvalidRegions[*R] = 0;");
  5326. verifyIndependentOfContext("A<int *> a;");
  5327. verifyIndependentOfContext("A<int **> a;");
  5328. verifyIndependentOfContext("A<int *, int *> a;");
  5329. verifyIndependentOfContext("A<int *[]> a;");
  5330. verifyIndependentOfContext(
  5331. "const char *const p = reinterpret_cast<const char *const>(q);");
  5332. verifyIndependentOfContext("A<int **, int **> a;");
  5333. verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
  5334. verifyFormat("for (char **a = b; *a; ++a) {\n}");
  5335. verifyFormat("for (; a && b;) {\n}");
  5336. verifyFormat("bool foo = true && [] { return false; }();");
  5337. verifyFormat(
  5338. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5339. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5340. verifyGoogleFormat("**outparam = 1;");
  5341. verifyGoogleFormat("*outparam = a * b;");
  5342. verifyGoogleFormat("int main(int argc, char** argv) {}");
  5343. verifyGoogleFormat("A<int*> a;");
  5344. verifyGoogleFormat("A<int**> a;");
  5345. verifyGoogleFormat("A<int*, int*> a;");
  5346. verifyGoogleFormat("A<int**, int**> a;");
  5347. verifyGoogleFormat("f(b ? *c : *d);");
  5348. verifyGoogleFormat("int a = b ? *c : *d;");
  5349. verifyGoogleFormat("Type* t = **x;");
  5350. verifyGoogleFormat("Type* t = *++*x;");
  5351. verifyGoogleFormat("*++*x;");
  5352. verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
  5353. verifyGoogleFormat("Type* t = x++ * y;");
  5354. verifyGoogleFormat(
  5355. "const char* const p = reinterpret_cast<const char* const>(q);");
  5356. verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
  5357. verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
  5358. verifyGoogleFormat("template <typename T>\n"
  5359. "void f(int i = 0, SomeType** temps = NULL);");
  5360. FormatStyle Left = getLLVMStyle();
  5361. Left.PointerAlignment = FormatStyle::PAS_Left;
  5362. verifyFormat("x = *a(x) = *a(y);", Left);
  5363. verifyFormat("for (;; * = b) {\n}", Left);
  5364. verifyFormat("return *this += 1;", Left);
  5365. verifyIndependentOfContext("a = *(x + y);");
  5366. verifyIndependentOfContext("a = &(x + y);");
  5367. verifyIndependentOfContext("*(x + y).call();");
  5368. verifyIndependentOfContext("&(x + y)->call();");
  5369. verifyFormat("void f() { &(*I).first; }");
  5370. verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
  5371. verifyFormat(
  5372. "int *MyValues = {\n"
  5373. " *A, // Operator detection might be confused by the '{'\n"
  5374. " *BB // Operator detection might be confused by previous comment\n"
  5375. "};");
  5376. verifyIndependentOfContext("if (int *a = &b)");
  5377. verifyIndependentOfContext("if (int &a = *b)");
  5378. verifyIndependentOfContext("if (a & b[i])");
  5379. verifyIndependentOfContext("if (a::b::c::d & b[i])");
  5380. verifyIndependentOfContext("if (*b[i])");
  5381. verifyIndependentOfContext("if (int *a = (&b))");
  5382. verifyIndependentOfContext("while (int *a = &b)");
  5383. verifyIndependentOfContext("size = sizeof *a;");
  5384. verifyIndependentOfContext("if (a && (b = c))");
  5385. verifyFormat("void f() {\n"
  5386. " for (const int &v : Values) {\n"
  5387. " }\n"
  5388. "}");
  5389. verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
  5390. verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
  5391. verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
  5392. verifyFormat("#define A (!a * b)");
  5393. verifyFormat("#define MACRO \\\n"
  5394. " int *i = a * b; \\\n"
  5395. " void f(a *b);",
  5396. getLLVMStyleWithColumns(19));
  5397. verifyIndependentOfContext("A = new SomeType *[Length];");
  5398. verifyIndependentOfContext("A = new SomeType *[Length]();");
  5399. verifyIndependentOfContext("T **t = new T *;");
  5400. verifyIndependentOfContext("T **t = new T *();");
  5401. verifyGoogleFormat("A = new SomeType*[Length]();");
  5402. verifyGoogleFormat("A = new SomeType*[Length];");
  5403. verifyGoogleFormat("T** t = new T*;");
  5404. verifyGoogleFormat("T** t = new T*();");
  5405. FormatStyle PointerLeft = getLLVMStyle();
  5406. PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
  5407. verifyFormat("delete *x;", PointerLeft);
  5408. verifyFormat("STATIC_ASSERT((a & b) == 0);");
  5409. verifyFormat("STATIC_ASSERT(0 == (a & b));");
  5410. verifyFormat("template <bool a, bool b> "
  5411. "typename t::if<x && y>::type f() {}");
  5412. verifyFormat("template <int *y> f() {}");
  5413. verifyFormat("vector<int *> v;");
  5414. verifyFormat("vector<int *const> v;");
  5415. verifyFormat("vector<int *const **const *> v;");
  5416. verifyFormat("vector<int *volatile> v;");
  5417. verifyFormat("vector<a * b> v;");
  5418. verifyFormat("foo<b && false>();");
  5419. verifyFormat("foo<b & 1>();");
  5420. verifyFormat("decltype(*::std::declval<const T &>()) void F();");
  5421. verifyFormat(
  5422. "template <class T, class = typename std::enable_if<\n"
  5423. " std::is_integral<T>::value &&\n"
  5424. " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
  5425. "void F();",
  5426. getLLVMStyleWithColumns(76));
  5427. verifyFormat(
  5428. "template <class T,\n"
  5429. " class = typename ::std::enable_if<\n"
  5430. " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
  5431. "void F();",
  5432. getGoogleStyleWithColumns(68));
  5433. verifyIndependentOfContext("MACRO(int *i);");
  5434. verifyIndependentOfContext("MACRO(auto *a);");
  5435. verifyIndependentOfContext("MACRO(const A *a);");
  5436. verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
  5437. // FIXME: Is there a way to make this work?
  5438. // verifyIndependentOfContext("MACRO(A *a);");
  5439. verifyFormat("DatumHandle const *operator->() const { return input_; }");
  5440. verifyFormat("return options != nullptr && operator==(*options);");
  5441. EXPECT_EQ("#define OP(x) \\\n"
  5442. " ostream &operator<<(ostream &s, const A &a) { \\\n"
  5443. " return s << a.DebugString(); \\\n"
  5444. " }",
  5445. format("#define OP(x) \\\n"
  5446. " ostream &operator<<(ostream &s, const A &a) { \\\n"
  5447. " return s << a.DebugString(); \\\n"
  5448. " }",
  5449. getLLVMStyleWithColumns(50)));
  5450. // FIXME: We cannot handle this case yet; we might be able to figure out that
  5451. // foo<x> d > v; doesn't make sense.
  5452. verifyFormat("foo<a<b && c> d> v;");
  5453. FormatStyle PointerMiddle = getLLVMStyle();
  5454. PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
  5455. verifyFormat("delete *x;", PointerMiddle);
  5456. verifyFormat("int * x;", PointerMiddle);
  5457. verifyFormat("template <int * y> f() {}", PointerMiddle);
  5458. verifyFormat("int * f(int * a) {}", PointerMiddle);
  5459. verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
  5460. verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
  5461. verifyFormat("A<int *> a;", PointerMiddle);
  5462. verifyFormat("A<int **> a;", PointerMiddle);
  5463. verifyFormat("A<int *, int *> a;", PointerMiddle);
  5464. verifyFormat("A<int * []> a;", PointerMiddle);
  5465. verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
  5466. verifyFormat("A = new SomeType *[Length];", PointerMiddle);
  5467. verifyFormat("T ** t = new T *;", PointerMiddle);
  5468. // Member function reference qualifiers aren't binary operators.
  5469. verifyFormat("string // break\n"
  5470. "operator()() & {}");
  5471. verifyFormat("string // break\n"
  5472. "operator()() && {}");
  5473. verifyGoogleFormat("template <typename T>\n"
  5474. "auto x() & -> int {}");
  5475. }
  5476. TEST_F(FormatTest, UnderstandsAttributes) {
  5477. verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
  5478. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
  5479. "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
  5480. FormatStyle AfterType = getLLVMStyle();
  5481. AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
  5482. verifyFormat("__attribute__((nodebug)) void\n"
  5483. "foo() {}\n",
  5484. AfterType);
  5485. }
  5486. TEST_F(FormatTest, UnderstandsEllipsis) {
  5487. verifyFormat("int printf(const char *fmt, ...);");
  5488. verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
  5489. verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
  5490. FormatStyle PointersLeft = getLLVMStyle();
  5491. PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
  5492. verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
  5493. }
  5494. TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
  5495. EXPECT_EQ("int *a;\n"
  5496. "int *a;\n"
  5497. "int *a;",
  5498. format("int *a;\n"
  5499. "int* a;\n"
  5500. "int *a;",
  5501. getGoogleStyle()));
  5502. EXPECT_EQ("int* a;\n"
  5503. "int* a;\n"
  5504. "int* a;",
  5505. format("int* a;\n"
  5506. "int* a;\n"
  5507. "int *a;",
  5508. getGoogleStyle()));
  5509. EXPECT_EQ("int *a;\n"
  5510. "int *a;\n"
  5511. "int *a;",
  5512. format("int *a;\n"
  5513. "int * a;\n"
  5514. "int * a;",
  5515. getGoogleStyle()));
  5516. EXPECT_EQ("auto x = [] {\n"
  5517. " int *a;\n"
  5518. " int *a;\n"
  5519. " int *a;\n"
  5520. "};",
  5521. format("auto x=[]{int *a;\n"
  5522. "int * a;\n"
  5523. "int * a;};",
  5524. getGoogleStyle()));
  5525. }
  5526. TEST_F(FormatTest, UnderstandsRvalueReferences) {
  5527. verifyFormat("int f(int &&a) {}");
  5528. verifyFormat("int f(int a, char &&b) {}");
  5529. verifyFormat("void f() { int &&a = b; }");
  5530. verifyGoogleFormat("int f(int a, char&& b) {}");
  5531. verifyGoogleFormat("void f() { int&& a = b; }");
  5532. verifyIndependentOfContext("A<int &&> a;");
  5533. verifyIndependentOfContext("A<int &&, int &&> a;");
  5534. verifyGoogleFormat("A<int&&> a;");
  5535. verifyGoogleFormat("A<int&&, int&&> a;");
  5536. // Not rvalue references:
  5537. verifyFormat("template <bool B, bool C> class A {\n"
  5538. " static_assert(B && C, \"Something is wrong\");\n"
  5539. "};");
  5540. verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
  5541. verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
  5542. verifyFormat("#define A(a, b) (a && b)");
  5543. }
  5544. TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
  5545. verifyFormat("void f() {\n"
  5546. " x[aaaaaaaaa -\n"
  5547. " b] = 23;\n"
  5548. "}",
  5549. getLLVMStyleWithColumns(15));
  5550. }
  5551. TEST_F(FormatTest, FormatsCasts) {
  5552. verifyFormat("Type *A = static_cast<Type *>(P);");
  5553. verifyFormat("Type *A = (Type *)P;");
  5554. verifyFormat("Type *A = (vector<Type *, int *>)P;");
  5555. verifyFormat("int a = (int)(2.0f);");
  5556. verifyFormat("int a = (int)2.0f;");
  5557. verifyFormat("x[(int32)y];");
  5558. verifyFormat("x = (int32)y;");
  5559. verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
  5560. verifyFormat("int a = (int)*b;");
  5561. verifyFormat("int a = (int)2.0f;");
  5562. verifyFormat("int a = (int)~0;");
  5563. verifyFormat("int a = (int)++a;");
  5564. verifyFormat("int a = (int)sizeof(int);");
  5565. verifyFormat("int a = (int)+2;");
  5566. verifyFormat("my_int a = (my_int)2.0f;");
  5567. verifyFormat("my_int a = (my_int)sizeof(int);");
  5568. verifyFormat("return (my_int)aaa;");
  5569. verifyFormat("#define x ((int)-1)");
  5570. verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
  5571. verifyFormat("#define p(q) ((int *)&q)");
  5572. verifyFormat("fn(a)(b) + 1;");
  5573. verifyFormat("void f() { my_int a = (my_int)*b; }");
  5574. verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
  5575. verifyFormat("my_int a = (my_int)~0;");
  5576. verifyFormat("my_int a = (my_int)++a;");
  5577. verifyFormat("my_int a = (my_int)-2;");
  5578. verifyFormat("my_int a = (my_int)1;");
  5579. verifyFormat("my_int a = (my_int *)1;");
  5580. verifyFormat("my_int a = (const my_int)-1;");
  5581. verifyFormat("my_int a = (const my_int *)-1;");
  5582. verifyFormat("my_int a = (my_int)(my_int)-1;");
  5583. verifyFormat("my_int a = (ns::my_int)-2;");
  5584. verifyFormat("case (my_int)ONE:");
  5585. verifyFormat("auto x = (X)this;");
  5586. // FIXME: single value wrapped with paren will be treated as cast.
  5587. verifyFormat("void f(int i = (kValue)*kMask) {}");
  5588. verifyFormat("{ (void)F; }");
  5589. // Don't break after a cast's
  5590. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  5591. " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
  5592. " bbbbbbbbbbbbbbbbbbbbbb);");
  5593. // These are not casts.
  5594. verifyFormat("void f(int *) {}");
  5595. verifyFormat("f(foo)->b;");
  5596. verifyFormat("f(foo).b;");
  5597. verifyFormat("f(foo)(b);");
  5598. verifyFormat("f(foo)[b];");
  5599. verifyFormat("[](foo) { return 4; }(bar);");
  5600. verifyFormat("(*funptr)(foo)[4];");
  5601. verifyFormat("funptrs[4](foo)[4];");
  5602. verifyFormat("void f(int *);");
  5603. verifyFormat("void f(int *) = 0;");
  5604. verifyFormat("void f(SmallVector<int>) {}");
  5605. verifyFormat("void f(SmallVector<int>);");
  5606. verifyFormat("void f(SmallVector<int>) = 0;");
  5607. verifyFormat("void f(int i = (kA * kB) & kMask) {}");
  5608. verifyFormat("int a = sizeof(int) * b;");
  5609. verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
  5610. verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
  5611. verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
  5612. verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
  5613. // These are not casts, but at some point were confused with casts.
  5614. verifyFormat("virtual void foo(int *) override;");
  5615. verifyFormat("virtual void foo(char &) const;");
  5616. verifyFormat("virtual void foo(int *a, char *) const;");
  5617. verifyFormat("int a = sizeof(int *) + b;");
  5618. verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
  5619. verifyFormat("bool b = f(g<int>) && c;");
  5620. verifyFormat("typedef void (*f)(int i) func;");
  5621. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
  5622. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  5623. // FIXME: The indentation here is not ideal.
  5624. verifyFormat(
  5625. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5626. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
  5627. " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
  5628. }
  5629. TEST_F(FormatTest, FormatsFunctionTypes) {
  5630. verifyFormat("A<bool()> a;");
  5631. verifyFormat("A<SomeType()> a;");
  5632. verifyFormat("A<void (*)(int, std::string)> a;");
  5633. verifyFormat("A<void *(int)>;");
  5634. verifyFormat("void *(*a)(int *, SomeType *);");
  5635. verifyFormat("int (*func)(void *);");
  5636. verifyFormat("void f() { int (*func)(void *); }");
  5637. verifyFormat("template <class CallbackClass>\n"
  5638. "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
  5639. verifyGoogleFormat("A<void*(int*, SomeType*)>;");
  5640. verifyGoogleFormat("void* (*a)(int);");
  5641. verifyGoogleFormat(
  5642. "template <class CallbackClass>\n"
  5643. "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
  5644. // Other constructs can look somewhat like function types:
  5645. verifyFormat("A<sizeof(*x)> a;");
  5646. verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
  5647. verifyFormat("some_var = function(*some_pointer_var)[0];");
  5648. verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
  5649. verifyFormat("int x = f(&h)();");
  5650. }
  5651. TEST_F(FormatTest, FormatsPointersToArrayTypes) {
  5652. verifyFormat("A (*foo_)[6];");
  5653. verifyFormat("vector<int> (*foo_)[6];");
  5654. }
  5655. TEST_F(FormatTest, BreaksLongVariableDeclarations) {
  5656. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5657. " LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5658. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
  5659. " LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5660. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5661. " *LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5662. // Different ways of ()-initializiation.
  5663. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5664. " LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
  5665. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5666. " LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
  5667. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5668. " LoooooooooooooooooooooooooooooooooooooooongVariable({});");
  5669. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5670. " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
  5671. }
  5672. TEST_F(FormatTest, BreaksLongDeclarations) {
  5673. verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
  5674. " AnotherNameForTheLongType;");
  5675. verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
  5676. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  5677. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5678. "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
  5679. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
  5680. "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
  5681. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5682. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5683. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
  5684. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5685. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  5686. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5687. verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
  5688. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5689. FormatStyle Indented = getLLVMStyle();
  5690. Indented.IndentWrappedFunctionNames = true;
  5691. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5692. " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
  5693. Indented);
  5694. verifyFormat(
  5695. "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5696. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5697. Indented);
  5698. verifyFormat(
  5699. "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  5700. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5701. Indented);
  5702. verifyFormat(
  5703. "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
  5704. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5705. Indented);
  5706. // FIXME: Without the comment, this breaks after "(".
  5707. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
  5708. " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
  5709. getGoogleStyle());
  5710. verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
  5711. " int LoooooooooooooooooooongParam2) {}");
  5712. verifyFormat(
  5713. "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
  5714. " SourceLocation L, IdentifierIn *II,\n"
  5715. " Type *T) {}");
  5716. verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
  5717. "ReallyReaaallyLongFunctionName(\n"
  5718. " const std::string &SomeParameter,\n"
  5719. " const SomeType<string, SomeOtherTemplateParameter>\n"
  5720. " &ReallyReallyLongParameterName,\n"
  5721. " const SomeType<string, SomeOtherTemplateParameter>\n"
  5722. " &AnotherLongParameterName) {}");
  5723. verifyFormat("template <typename A>\n"
  5724. "SomeLoooooooooooooooooooooongType<\n"
  5725. " typename some_namespace::SomeOtherType<A>::Type>\n"
  5726. "Function() {}");
  5727. verifyGoogleFormat(
  5728. "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
  5729. " aaaaaaaaaaaaaaaaaaaaaaa;");
  5730. verifyGoogleFormat(
  5731. "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
  5732. " SourceLocation L) {}");
  5733. verifyGoogleFormat(
  5734. "some_namespace::LongReturnType\n"
  5735. "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
  5736. " int first_long_parameter, int second_parameter) {}");
  5737. verifyGoogleFormat("template <typename T>\n"
  5738. "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  5739. "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
  5740. verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5741. " int aaaaaaaaaaaaaaaaaaaaaaa);");
  5742. verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  5743. " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5744. " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5745. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5746. " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  5747. " aaaaaaaaaaaaaaaaaaaaaaaa);");
  5748. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5749. " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  5750. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
  5751. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5752. }
  5753. TEST_F(FormatTest, FormatsArrays) {
  5754. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5755. " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
  5756. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
  5757. " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
  5758. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
  5759. " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
  5760. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5761. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  5762. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5763. " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
  5764. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5765. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5766. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  5767. verifyFormat(
  5768. "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
  5769. " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5770. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  5771. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
  5772. " .aaaaaaaaaaaaaaaaaaaaaa();");
  5773. verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
  5774. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
  5775. verifyFormat(
  5776. "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
  5777. " .aaaaaaa[0]\n"
  5778. " .aaaaaaaaaaaaaaaaaaaaaa();");
  5779. verifyFormat("a[::b::c];");
  5780. verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
  5781. FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
  5782. verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
  5783. }
  5784. TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
  5785. verifyFormat("(a)->b();");
  5786. verifyFormat("--a;");
  5787. }
  5788. TEST_F(FormatTest, HandlesIncludeDirectives) {
  5789. verifyFormat("#include <string>\n"
  5790. "#include <a/b/c.h>\n"
  5791. "#include \"a/b/string\"\n"
  5792. "#include \"string.h\"\n"
  5793. "#include \"string.h\"\n"
  5794. "#include <a-a>\n"
  5795. "#include < path with space >\n"
  5796. "#include_next <test.h>"
  5797. "#include \"abc.h\" // this is included for ABC\n"
  5798. "#include \"some long include\" // with a comment\n"
  5799. "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
  5800. getLLVMStyleWithColumns(35));
  5801. EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\""));
  5802. EXPECT_EQ("#include <a>", format("#include<a>"));
  5803. verifyFormat("#import <string>");
  5804. verifyFormat("#import <a/b/c.h>");
  5805. verifyFormat("#import \"a/b/string\"");
  5806. verifyFormat("#import \"string.h\"");
  5807. verifyFormat("#import \"string.h\"");
  5808. verifyFormat("#if __has_include(<strstream>)\n"
  5809. "#include <strstream>\n"
  5810. "#endif");
  5811. verifyFormat("#define MY_IMPORT <a/b>");
  5812. // Protocol buffer definition or missing "#".
  5813. verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
  5814. getLLVMStyleWithColumns(30));
  5815. FormatStyle Style = getLLVMStyle();
  5816. Style.AlwaysBreakBeforeMultilineStrings = true;
  5817. Style.ColumnLimit = 0;
  5818. verifyFormat("#import \"abc.h\"", Style);
  5819. // But 'import' might also be a regular C++ namespace.
  5820. verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5821. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5822. }
  5823. //===----------------------------------------------------------------------===//
  5824. // Error recovery tests.
  5825. //===----------------------------------------------------------------------===//
  5826. TEST_F(FormatTest, IncompleteParameterLists) {
  5827. FormatStyle NoBinPacking = getLLVMStyle();
  5828. NoBinPacking.BinPackParameters = false;
  5829. verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
  5830. " double *min_x,\n"
  5831. " double *max_x,\n"
  5832. " double *min_y,\n"
  5833. " double *max_y,\n"
  5834. " double *min_z,\n"
  5835. " double *max_z, ) {}",
  5836. NoBinPacking);
  5837. }
  5838. TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
  5839. verifyFormat("void f() { return; }\n42");
  5840. verifyFormat("void f() {\n"
  5841. " if (0)\n"
  5842. " return;\n"
  5843. "}\n"
  5844. "42");
  5845. verifyFormat("void f() { return }\n42");
  5846. verifyFormat("void f() {\n"
  5847. " if (0)\n"
  5848. " return\n"
  5849. "}\n"
  5850. "42");
  5851. }
  5852. TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
  5853. EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
  5854. EXPECT_EQ("void f() {\n"
  5855. " if (a)\n"
  5856. " return\n"
  5857. "}",
  5858. format("void f ( ) { if ( a ) return }"));
  5859. EXPECT_EQ("namespace N {\n"
  5860. "void f()\n"
  5861. "}",
  5862. format("namespace N { void f() }"));
  5863. EXPECT_EQ("namespace N {\n"
  5864. "void f() {}\n"
  5865. "void g()\n"
  5866. "}",
  5867. format("namespace N { void f( ) { } void g( ) }"));
  5868. }
  5869. TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
  5870. verifyFormat("int aaaaaaaa =\n"
  5871. " // Overlylongcomment\n"
  5872. " b;",
  5873. getLLVMStyleWithColumns(20));
  5874. verifyFormat("function(\n"
  5875. " ShortArgument,\n"
  5876. " LoooooooooooongArgument);\n",
  5877. getLLVMStyleWithColumns(20));
  5878. }
  5879. TEST_F(FormatTest, IncorrectAccessSpecifier) {
  5880. verifyFormat("public:");
  5881. verifyFormat("class A {\n"
  5882. "public\n"
  5883. " void f() {}\n"
  5884. "};");
  5885. verifyFormat("public\n"
  5886. "int qwerty;");
  5887. verifyFormat("public\n"
  5888. "B {}");
  5889. verifyFormat("public\n"
  5890. "{}");
  5891. verifyFormat("public\n"
  5892. "B { int x; }");
  5893. }
  5894. TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
  5895. verifyFormat("{");
  5896. verifyFormat("#})");
  5897. verifyNoCrash("(/**/[:!] ?[).");
  5898. }
  5899. TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
  5900. verifyFormat("do {\n}");
  5901. verifyFormat("do {\n}\n"
  5902. "f();");
  5903. verifyFormat("do {\n}\n"
  5904. "wheeee(fun);");
  5905. verifyFormat("do {\n"
  5906. " f();\n"
  5907. "}");
  5908. }
  5909. TEST_F(FormatTest, IncorrectCodeMissingParens) {
  5910. verifyFormat("if {\n foo;\n foo();\n}");
  5911. verifyFormat("switch {\n foo;\n foo();\n}");
  5912. verifyIncompleteFormat("for {\n foo;\n foo();\n}");
  5913. verifyFormat("while {\n foo;\n foo();\n}");
  5914. verifyFormat("do {\n foo;\n foo();\n} while;");
  5915. }
  5916. TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
  5917. verifyIncompleteFormat("namespace {\n"
  5918. "class Foo { Foo (\n"
  5919. "};\n"
  5920. "} // comment");
  5921. }
  5922. TEST_F(FormatTest, IncorrectCodeErrorDetection) {
  5923. EXPECT_EQ("{\n {}\n", format("{\n{\n}\n"));
  5924. EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
  5925. EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
  5926. EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n"));
  5927. EXPECT_EQ("{\n"
  5928. " {\n"
  5929. " breakme(\n"
  5930. " qwe);\n"
  5931. " }\n",
  5932. format("{\n"
  5933. " {\n"
  5934. " breakme(qwe);\n"
  5935. "}\n",
  5936. getLLVMStyleWithColumns(10)));
  5937. }
  5938. TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
  5939. verifyFormat("int x = {\n"
  5940. " avariable,\n"
  5941. " b(alongervariable)};",
  5942. getLLVMStyleWithColumns(25));
  5943. }
  5944. TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
  5945. verifyFormat("return (a)(b){1, 2, 3};");
  5946. }
  5947. TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
  5948. verifyFormat("vector<int> x{1, 2, 3, 4};");
  5949. verifyFormat("vector<int> x{\n"
  5950. " 1, 2, 3, 4,\n"
  5951. "};");
  5952. verifyFormat("vector<T> x{{}, {}, {}, {}};");
  5953. verifyFormat("f({1, 2});");
  5954. verifyFormat("auto v = Foo{-1};");
  5955. verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
  5956. verifyFormat("Class::Class : member{1, 2, 3} {}");
  5957. verifyFormat("new vector<int>{1, 2, 3};");
  5958. verifyFormat("new int[3]{1, 2, 3};");
  5959. verifyFormat("new int{1};");
  5960. verifyFormat("return {arg1, arg2};");
  5961. verifyFormat("return {arg1, SomeType{parameter}};");
  5962. verifyFormat("int count = set<int>{f(), g(), h()}.size();");
  5963. verifyFormat("new T{arg1, arg2};");
  5964. verifyFormat("f(MyMap[{composite, key}]);");
  5965. verifyFormat("class Class {\n"
  5966. " T member = {arg1, arg2};\n"
  5967. "};");
  5968. verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
  5969. verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
  5970. verifyFormat("int a = std::is_integral<int>{} + 0;");
  5971. verifyFormat("int foo(int i) { return fo1{}(i); }");
  5972. verifyFormat("int foo(int i) { return fo1{}(i); }");
  5973. verifyFormat("auto i = decltype(x){};");
  5974. verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
  5975. verifyFormat("Node n{1, Node{1000}, //\n"
  5976. " 2};");
  5977. verifyFormat("Aaaa aaaaaaa{\n"
  5978. " {\n"
  5979. " aaaa,\n"
  5980. " },\n"
  5981. "};");
  5982. verifyFormat("class C : public D {\n"
  5983. " SomeClass SC{2};\n"
  5984. "};");
  5985. verifyFormat("class C : public A {\n"
  5986. " class D : public B {\n"
  5987. " void f() { int i{2}; }\n"
  5988. " };\n"
  5989. "};");
  5990. verifyFormat("#define A {a, a},");
  5991. // In combination with BinPackArguments = false.
  5992. FormatStyle NoBinPacking = getLLVMStyle();
  5993. NoBinPacking.BinPackArguments = false;
  5994. verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
  5995. " bbbbb,\n"
  5996. " ccccc,\n"
  5997. " ddddd,\n"
  5998. " eeeee,\n"
  5999. " ffffff,\n"
  6000. " ggggg,\n"
  6001. " hhhhhh,\n"
  6002. " iiiiii,\n"
  6003. " jjjjjj,\n"
  6004. " kkkkkk};",
  6005. NoBinPacking);
  6006. verifyFormat("const Aaaaaa aaaaa = {\n"
  6007. " aaaaa,\n"
  6008. " bbbbb,\n"
  6009. " ccccc,\n"
  6010. " ddddd,\n"
  6011. " eeeee,\n"
  6012. " ffffff,\n"
  6013. " ggggg,\n"
  6014. " hhhhhh,\n"
  6015. " iiiiii,\n"
  6016. " jjjjjj,\n"
  6017. " kkkkkk,\n"
  6018. "};",
  6019. NoBinPacking);
  6020. verifyFormat(
  6021. "const Aaaaaa aaaaa = {\n"
  6022. " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n"
  6023. " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n"
  6024. " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
  6025. "};",
  6026. NoBinPacking);
  6027. // FIXME: The alignment of these trailing comments might be bad. Then again,
  6028. // this might be utterly useless in real code.
  6029. verifyFormat("Constructor::Constructor()\n"
  6030. " : some_value{ //\n"
  6031. " aaaaaaa, //\n"
  6032. " bbbbbbb} {}");
  6033. // In braced lists, the first comment is always assumed to belong to the
  6034. // first element. Thus, it can be moved to the next or previous line as
  6035. // appropriate.
  6036. EXPECT_EQ("function({// First element:\n"
  6037. " 1,\n"
  6038. " // Second element:\n"
  6039. " 2});",
  6040. format("function({\n"
  6041. " // First element:\n"
  6042. " 1,\n"
  6043. " // Second element:\n"
  6044. " 2});"));
  6045. EXPECT_EQ("std::vector<int> MyNumbers{\n"
  6046. " // First element:\n"
  6047. " 1,\n"
  6048. " // Second element:\n"
  6049. " 2};",
  6050. format("std::vector<int> MyNumbers{// First element:\n"
  6051. " 1,\n"
  6052. " // Second element:\n"
  6053. " 2};",
  6054. getLLVMStyleWithColumns(30)));
  6055. // A trailing comma should still lead to an enforced line break.
  6056. EXPECT_EQ("vector<int> SomeVector = {\n"
  6057. " // aaa\n"
  6058. " 1, 2,\n"
  6059. "};",
  6060. format("vector<int> SomeVector = { // aaa\n"
  6061. " 1, 2, };"));
  6062. FormatStyle ExtraSpaces = getLLVMStyle();
  6063. ExtraSpaces.Cpp11BracedListStyle = false;
  6064. ExtraSpaces.ColumnLimit = 75;
  6065. verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
  6066. verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
  6067. verifyFormat("f({ 1, 2 });", ExtraSpaces);
  6068. verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
  6069. verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
  6070. verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
  6071. verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
  6072. verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
  6073. verifyFormat("return { arg1, arg2 };", ExtraSpaces);
  6074. verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
  6075. verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
  6076. verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
  6077. verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
  6078. verifyFormat("class Class {\n"
  6079. " T member = { arg1, arg2 };\n"
  6080. "};",
  6081. ExtraSpaces);
  6082. verifyFormat(
  6083. "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6084. " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
  6085. " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  6086. " bbbbbbbbbbbbbbbbbbbb, bbbbb };",
  6087. ExtraSpaces);
  6088. verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
  6089. verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
  6090. ExtraSpaces);
  6091. verifyFormat(
  6092. "someFunction(OtherParam,\n"
  6093. " BracedList{ // comment 1 (Forcing interesting break)\n"
  6094. " param1, param2,\n"
  6095. " // comment 2\n"
  6096. " param3, param4 });",
  6097. ExtraSpaces);
  6098. verifyFormat(
  6099. "std::this_thread::sleep_for(\n"
  6100. " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
  6101. ExtraSpaces);
  6102. verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
  6103. " aaaaaaa,\n"
  6104. " aaaaaaaaaa,\n"
  6105. " aaaaa,\n"
  6106. " aaaaaaaaaaaaaaa,\n"
  6107. " aaa,\n"
  6108. " aaaaaaaaaa,\n"
  6109. " a,\n"
  6110. " aaaaaaaaaaaaaaaaaaaaa,\n"
  6111. " aaaaaaaaaaaa,\n"
  6112. " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
  6113. " aaaaaaa,\n"
  6114. " a};");
  6115. verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
  6116. }
  6117. TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
  6118. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6119. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6120. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6121. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6122. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6123. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  6124. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
  6125. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6126. " 1, 22, 333, 4444, 55555, //\n"
  6127. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6128. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  6129. verifyFormat(
  6130. "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6131. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6132. " 1, 22, 333, 4444, 55555, 666666, // comment\n"
  6133. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  6134. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  6135. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  6136. " 7777777};");
  6137. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  6138. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  6139. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  6140. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  6141. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  6142. " // Separating comment.\n"
  6143. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  6144. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  6145. " // Leading comment\n"
  6146. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  6147. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  6148. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  6149. " 1, 1, 1, 1};",
  6150. getLLVMStyleWithColumns(39));
  6151. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  6152. " 1, 1, 1, 1};",
  6153. getLLVMStyleWithColumns(38));
  6154. verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
  6155. " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
  6156. getLLVMStyleWithColumns(43));
  6157. verifyFormat(
  6158. "static unsigned SomeValues[10][3] = {\n"
  6159. " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n"
  6160. " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
  6161. verifyFormat("static auto fields = new vector<string>{\n"
  6162. " \"aaaaaaaaaaaaa\",\n"
  6163. " \"aaaaaaaaaaaaa\",\n"
  6164. " \"aaaaaaaaaaaa\",\n"
  6165. " \"aaaaaaaaaaaaaa\",\n"
  6166. " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
  6167. " \"aaaaaaaaaaaa\",\n"
  6168. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
  6169. "};");
  6170. verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
  6171. verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
  6172. " 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
  6173. " 3, cccccccccccccccccccccc};",
  6174. getLLVMStyleWithColumns(60));
  6175. // Trailing commas.
  6176. verifyFormat("vector<int> x = {\n"
  6177. " 1, 1, 1, 1, 1, 1, 1, 1,\n"
  6178. "};",
  6179. getLLVMStyleWithColumns(39));
  6180. verifyFormat("vector<int> x = {\n"
  6181. " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
  6182. "};",
  6183. getLLVMStyleWithColumns(39));
  6184. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  6185. " 1, 1, 1, 1,\n"
  6186. " /**/ /**/};",
  6187. getLLVMStyleWithColumns(39));
  6188. // Trailing comment in the first line.
  6189. verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n"
  6190. " 1111111111, 2222222222, 33333333333, 4444444444, //\n"
  6191. " 111111111, 222222222, 3333333333, 444444444, //\n"
  6192. " 11111111, 22222222, 333333333, 44444444};");
  6193. // Trailing comment in the last line.
  6194. verifyFormat("int aaaaa[] = {\n"
  6195. " 1, 2, 3, // comment\n"
  6196. " 4, 5, 6 // comment\n"
  6197. "};");
  6198. // With nested lists, we should either format one item per line or all nested
  6199. // lists one on line.
  6200. // FIXME: For some nested lists, we can do better.
  6201. verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
  6202. " {aaaaaaaaaaaaaaaaaaa},\n"
  6203. " {aaaaaaaaaaaaaaaaaaaaa},\n"
  6204. " {aaaaaaaaaaaaaaaaa}};",
  6205. getLLVMStyleWithColumns(60));
  6206. verifyFormat(
  6207. "SomeStruct my_struct_array = {\n"
  6208. " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
  6209. " aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
  6210. " {aaa, aaa},\n"
  6211. " {aaa, aaa},\n"
  6212. " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
  6213. " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6214. " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
  6215. // No column layout should be used here.
  6216. verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
  6217. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
  6218. verifyNoCrash("a<,");
  6219. // No braced initializer here.
  6220. verifyFormat("void f() {\n"
  6221. " struct Dummy {};\n"
  6222. " f(v);\n"
  6223. "}");
  6224. // Long lists should be formatted in columns even if they are nested.
  6225. verifyFormat(
  6226. "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6227. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6228. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6229. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6230. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6231. " 1, 22, 333, 4444, 55555, 666666, 7777777});");
  6232. }
  6233. TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
  6234. FormatStyle DoNotMerge = getLLVMStyle();
  6235. DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  6236. verifyFormat("void f() { return 42; }");
  6237. verifyFormat("void f() {\n"
  6238. " return 42;\n"
  6239. "}",
  6240. DoNotMerge);
  6241. verifyFormat("void f() {\n"
  6242. " // Comment\n"
  6243. "}");
  6244. verifyFormat("{\n"
  6245. "#error {\n"
  6246. " int a;\n"
  6247. "}");
  6248. verifyFormat("{\n"
  6249. " int a;\n"
  6250. "#error {\n"
  6251. "}");
  6252. verifyFormat("void f() {} // comment");
  6253. verifyFormat("void f() { int a; } // comment");
  6254. verifyFormat("void f() {\n"
  6255. "} // comment",
  6256. DoNotMerge);
  6257. verifyFormat("void f() {\n"
  6258. " int a;\n"
  6259. "} // comment",
  6260. DoNotMerge);
  6261. verifyFormat("void f() {\n"
  6262. "} // comment",
  6263. getLLVMStyleWithColumns(15));
  6264. verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
  6265. verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
  6266. verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
  6267. verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
  6268. verifyFormat("class C {\n"
  6269. " C()\n"
  6270. " : iiiiiiii(nullptr),\n"
  6271. " kkkkkkk(nullptr),\n"
  6272. " mmmmmmm(nullptr),\n"
  6273. " nnnnnnn(nullptr) {}\n"
  6274. "};",
  6275. getGoogleStyle());
  6276. FormatStyle NoColumnLimit = getLLVMStyle();
  6277. NoColumnLimit.ColumnLimit = 0;
  6278. EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
  6279. EXPECT_EQ("class C {\n"
  6280. " A() : b(0) {}\n"
  6281. "};",
  6282. format("class C{A():b(0){}};", NoColumnLimit));
  6283. EXPECT_EQ("A()\n"
  6284. " : b(0) {\n"
  6285. "}",
  6286. format("A()\n:b(0)\n{\n}", NoColumnLimit));
  6287. FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
  6288. DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
  6289. FormatStyle::SFS_None;
  6290. EXPECT_EQ("A()\n"
  6291. " : b(0) {\n"
  6292. "}",
  6293. format("A():b(0){}", DoNotMergeNoColumnLimit));
  6294. EXPECT_EQ("A()\n"
  6295. " : b(0) {\n"
  6296. "}",
  6297. format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
  6298. verifyFormat("#define A \\\n"
  6299. " void f() { \\\n"
  6300. " int i; \\\n"
  6301. " }",
  6302. getLLVMStyleWithColumns(20));
  6303. verifyFormat("#define A \\\n"
  6304. " void f() { int i; }",
  6305. getLLVMStyleWithColumns(21));
  6306. verifyFormat("#define A \\\n"
  6307. " void f() { \\\n"
  6308. " int i; \\\n"
  6309. " } \\\n"
  6310. " int j;",
  6311. getLLVMStyleWithColumns(22));
  6312. verifyFormat("#define A \\\n"
  6313. " void f() { int i; } \\\n"
  6314. " int j;",
  6315. getLLVMStyleWithColumns(23));
  6316. }
  6317. TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
  6318. FormatStyle MergeInlineOnly = getLLVMStyle();
  6319. MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  6320. verifyFormat("class C {\n"
  6321. " int f() { return 42; }\n"
  6322. "};",
  6323. MergeInlineOnly);
  6324. verifyFormat("int f() {\n"
  6325. " return 42;\n"
  6326. "}",
  6327. MergeInlineOnly);
  6328. }
  6329. TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
  6330. // Elaborate type variable declarations.
  6331. verifyFormat("struct foo a = {bar};\nint n;");
  6332. verifyFormat("class foo a = {bar};\nint n;");
  6333. verifyFormat("union foo a = {bar};\nint n;");
  6334. // Elaborate types inside function definitions.
  6335. verifyFormat("struct foo f() {}\nint n;");
  6336. verifyFormat("class foo f() {}\nint n;");
  6337. verifyFormat("union foo f() {}\nint n;");
  6338. // Templates.
  6339. verifyFormat("template <class X> void f() {}\nint n;");
  6340. verifyFormat("template <struct X> void f() {}\nint n;");
  6341. verifyFormat("template <union X> void f() {}\nint n;");
  6342. // Actual definitions...
  6343. verifyFormat("struct {\n} n;");
  6344. verifyFormat(
  6345. "template <template <class T, class Y>, class Z> class X {\n} n;");
  6346. verifyFormat("union Z {\n int n;\n} x;");
  6347. verifyFormat("class MACRO Z {\n} n;");
  6348. verifyFormat("class MACRO(X) Z {\n} n;");
  6349. verifyFormat("class __attribute__(X) Z {\n} n;");
  6350. verifyFormat("class __declspec(X) Z {\n} n;");
  6351. verifyFormat("class A##B##C {\n} n;");
  6352. verifyFormat("class alignas(16) Z {\n} n;");
  6353. verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
  6354. verifyFormat("class MACROA MACRO(X) Z {\n} n;");
  6355. // Redefinition from nested context:
  6356. verifyFormat("class A::B::C {\n} n;");
  6357. // Template definitions.
  6358. verifyFormat(
  6359. "template <typename F>\n"
  6360. "Matcher(const Matcher<F> &Other,\n"
  6361. " typename enable_if_c<is_base_of<F, T>::value &&\n"
  6362. " !is_same<F, T>::value>::type * = 0)\n"
  6363. " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
  6364. // FIXME: This is still incorrectly handled at the formatter side.
  6365. verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
  6366. verifyFormat("int i = SomeFunction(a<b, a> b);");
  6367. // FIXME:
  6368. // This now gets parsed incorrectly as class definition.
  6369. // verifyFormat("class A<int> f() {\n}\nint n;");
  6370. // Elaborate types where incorrectly parsing the structural element would
  6371. // break the indent.
  6372. verifyFormat("if (true)\n"
  6373. " class X x;\n"
  6374. "else\n"
  6375. " f();\n");
  6376. // This is simply incomplete. Formatting is not important, but must not crash.
  6377. verifyFormat("class A:");
  6378. }
  6379. TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
  6380. EXPECT_EQ("#error Leave all white!!!!! space* alone!\n",
  6381. format("#error Leave all white!!!!! space* alone!\n"));
  6382. EXPECT_EQ(
  6383. "#warning Leave all white!!!!! space* alone!\n",
  6384. format("#warning Leave all white!!!!! space* alone!\n"));
  6385. EXPECT_EQ("#error 1", format(" # error 1"));
  6386. EXPECT_EQ("#warning 1", format(" # warning 1"));
  6387. }
  6388. TEST_F(FormatTest, FormatHashIfExpressions) {
  6389. verifyFormat("#if AAAA && BBBB");
  6390. verifyFormat("#if (AAAA && BBBB)");
  6391. verifyFormat("#elif (AAAA && BBBB)");
  6392. // FIXME: Come up with a better indentation for #elif.
  6393. verifyFormat(
  6394. "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
  6395. " defined(BBBBBBBB)\n"
  6396. "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
  6397. " defined(BBBBBBBB)\n"
  6398. "#endif",
  6399. getLLVMStyleWithColumns(65));
  6400. }
  6401. TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
  6402. FormatStyle AllowsMergedIf = getGoogleStyle();
  6403. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  6404. verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
  6405. verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
  6406. verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
  6407. EXPECT_EQ("if (true) return 42;",
  6408. format("if (true)\nreturn 42;", AllowsMergedIf));
  6409. FormatStyle ShortMergedIf = AllowsMergedIf;
  6410. ShortMergedIf.ColumnLimit = 25;
  6411. verifyFormat("#define A \\\n"
  6412. " if (true) return 42;",
  6413. ShortMergedIf);
  6414. verifyFormat("#define A \\\n"
  6415. " f(); \\\n"
  6416. " if (true)\n"
  6417. "#define B",
  6418. ShortMergedIf);
  6419. verifyFormat("#define A \\\n"
  6420. " f(); \\\n"
  6421. " if (true)\n"
  6422. "g();",
  6423. ShortMergedIf);
  6424. verifyFormat("{\n"
  6425. "#ifdef A\n"
  6426. " // Comment\n"
  6427. " if (true) continue;\n"
  6428. "#endif\n"
  6429. " // Comment\n"
  6430. " if (true) continue;\n"
  6431. "}",
  6432. ShortMergedIf);
  6433. ShortMergedIf.ColumnLimit = 29;
  6434. verifyFormat("#define A \\\n"
  6435. " if (aaaaaaaaaa) return 1; \\\n"
  6436. " return 2;",
  6437. ShortMergedIf);
  6438. ShortMergedIf.ColumnLimit = 28;
  6439. verifyFormat("#define A \\\n"
  6440. " if (aaaaaaaaaa) \\\n"
  6441. " return 1; \\\n"
  6442. " return 2;",
  6443. ShortMergedIf);
  6444. }
  6445. TEST_F(FormatTest, BlockCommentsInControlLoops) {
  6446. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  6447. " f();\n"
  6448. "}");
  6449. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  6450. " f();\n"
  6451. "} /* another comment */ else /* comment #3 */ {\n"
  6452. " g();\n"
  6453. "}");
  6454. verifyFormat("while (0) /* a comment in a strange place */ {\n"
  6455. " f();\n"
  6456. "}");
  6457. verifyFormat("for (;;) /* a comment in a strange place */ {\n"
  6458. " f();\n"
  6459. "}");
  6460. verifyFormat("do /* a comment in a strange place */ {\n"
  6461. " f();\n"
  6462. "} /* another comment */ while (0);");
  6463. }
  6464. TEST_F(FormatTest, BlockComments) {
  6465. EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
  6466. format("/* *//* */ /* */\n/* *//* */ /* */"));
  6467. EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
  6468. EXPECT_EQ("#define A /*123*/ \\\n"
  6469. " b\n"
  6470. "/* */\n"
  6471. "someCall(\n"
  6472. " parameter);",
  6473. format("#define A /*123*/ b\n"
  6474. "/* */\n"
  6475. "someCall(parameter);",
  6476. getLLVMStyleWithColumns(15)));
  6477. EXPECT_EQ("#define A\n"
  6478. "/* */ someCall(\n"
  6479. " parameter);",
  6480. format("#define A\n"
  6481. "/* */someCall(parameter);",
  6482. getLLVMStyleWithColumns(15)));
  6483. EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
  6484. EXPECT_EQ("/*\n"
  6485. "*\n"
  6486. " * aaaaaa\n"
  6487. " * aaaaaa\n"
  6488. "*/",
  6489. format("/*\n"
  6490. "*\n"
  6491. " * aaaaaa aaaaaa\n"
  6492. "*/",
  6493. getLLVMStyleWithColumns(10)));
  6494. EXPECT_EQ("/*\n"
  6495. "**\n"
  6496. "* aaaaaa\n"
  6497. "*aaaaaa\n"
  6498. "*/",
  6499. format("/*\n"
  6500. "**\n"
  6501. "* aaaaaa aaaaaa\n"
  6502. "*/",
  6503. getLLVMStyleWithColumns(10)));
  6504. FormatStyle NoBinPacking = getLLVMStyle();
  6505. NoBinPacking.BinPackParameters = false;
  6506. EXPECT_EQ("someFunction(1, /* comment 1 */\n"
  6507. " 2, /* comment 2 */\n"
  6508. " 3, /* comment 3 */\n"
  6509. " aaaa,\n"
  6510. " bbbb);",
  6511. format("someFunction (1, /* comment 1 */\n"
  6512. " 2, /* comment 2 */ \n"
  6513. " 3, /* comment 3 */\n"
  6514. "aaaa, bbbb );",
  6515. NoBinPacking));
  6516. verifyFormat(
  6517. "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  6518. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  6519. EXPECT_EQ(
  6520. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  6521. " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  6522. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
  6523. format(
  6524. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  6525. " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  6526. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
  6527. EXPECT_EQ(
  6528. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  6529. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  6530. "int cccccccccccccccccccccccccccccc; /* comment */\n",
  6531. format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  6532. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  6533. "int cccccccccccccccccccccccccccccc; /* comment */\n"));
  6534. verifyFormat("void f(int * /* unused */) {}");
  6535. EXPECT_EQ("/*\n"
  6536. " **\n"
  6537. " */",
  6538. format("/*\n"
  6539. " **\n"
  6540. " */"));
  6541. EXPECT_EQ("/*\n"
  6542. " *q\n"
  6543. " */",
  6544. format("/*\n"
  6545. " *q\n"
  6546. " */"));
  6547. EXPECT_EQ("/*\n"
  6548. " * q\n"
  6549. " */",
  6550. format("/*\n"
  6551. " * q\n"
  6552. " */"));
  6553. EXPECT_EQ("/*\n"
  6554. " **/",
  6555. format("/*\n"
  6556. " **/"));
  6557. EXPECT_EQ("/*\n"
  6558. " ***/",
  6559. format("/*\n"
  6560. " ***/"));
  6561. }
  6562. TEST_F(FormatTest, BlockCommentsInMacros) {
  6563. EXPECT_EQ("#define A \\\n"
  6564. " { \\\n"
  6565. " /* one line */ \\\n"
  6566. " someCall();",
  6567. format("#define A { \\\n"
  6568. " /* one line */ \\\n"
  6569. " someCall();",
  6570. getLLVMStyleWithColumns(20)));
  6571. EXPECT_EQ("#define A \\\n"
  6572. " { \\\n"
  6573. " /* previous */ \\\n"
  6574. " /* one line */ \\\n"
  6575. " someCall();",
  6576. format("#define A { \\\n"
  6577. " /* previous */ \\\n"
  6578. " /* one line */ \\\n"
  6579. " someCall();",
  6580. getLLVMStyleWithColumns(20)));
  6581. }
  6582. TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
  6583. EXPECT_EQ("a = {\n"
  6584. " 1111 /* */\n"
  6585. "};",
  6586. format("a = {1111 /* */\n"
  6587. "};",
  6588. getLLVMStyleWithColumns(15)));
  6589. EXPECT_EQ("a = {\n"
  6590. " 1111 /* */\n"
  6591. "};",
  6592. format("a = {1111 /* */\n"
  6593. "};",
  6594. getLLVMStyleWithColumns(15)));
  6595. // FIXME: The formatting is still wrong here.
  6596. EXPECT_EQ("a = {\n"
  6597. " 1111 /* a\n"
  6598. " */\n"
  6599. "};",
  6600. format("a = {1111 /* a */\n"
  6601. "};",
  6602. getLLVMStyleWithColumns(15)));
  6603. }
  6604. TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
  6605. // FIXME: This is not what we want...
  6606. verifyFormat("{\n"
  6607. "// a"
  6608. "// b");
  6609. }
  6610. TEST_F(FormatTest, FormatStarDependingOnContext) {
  6611. verifyFormat("void f(int *a);");
  6612. verifyFormat("void f() { f(fint * b); }");
  6613. verifyFormat("class A {\n void f(int *a);\n};");
  6614. verifyFormat("class A {\n int *a;\n};");
  6615. verifyFormat("namespace a {\n"
  6616. "namespace b {\n"
  6617. "class A {\n"
  6618. " void f() {}\n"
  6619. " int *a;\n"
  6620. "};\n"
  6621. "}\n"
  6622. "}");
  6623. }
  6624. TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
  6625. verifyFormat("while");
  6626. verifyFormat("operator");
  6627. }
  6628. //===----------------------------------------------------------------------===//
  6629. // Objective-C tests.
  6630. //===----------------------------------------------------------------------===//
  6631. TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
  6632. verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
  6633. EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
  6634. format("-(NSUInteger)indexOfObject:(id)anObject;"));
  6635. EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
  6636. EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
  6637. EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
  6638. format("-(NSInteger)Method3:(id)anObject;"));
  6639. EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
  6640. format("-(NSInteger)Method4:(id)anObject;"));
  6641. EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
  6642. format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
  6643. EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
  6644. format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
  6645. EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
  6646. "forAllCells:(BOOL)flag;",
  6647. format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
  6648. "forAllCells:(BOOL)flag;"));
  6649. // Very long objectiveC method declaration.
  6650. verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
  6651. " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
  6652. verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
  6653. " inRange:(NSRange)range\n"
  6654. " outRange:(NSRange)out_range\n"
  6655. " outRange1:(NSRange)out_range1\n"
  6656. " outRange2:(NSRange)out_range2\n"
  6657. " outRange3:(NSRange)out_range3\n"
  6658. " outRange4:(NSRange)out_range4\n"
  6659. " outRange5:(NSRange)out_range5\n"
  6660. " outRange6:(NSRange)out_range6\n"
  6661. " outRange7:(NSRange)out_range7\n"
  6662. " outRange8:(NSRange)out_range8\n"
  6663. " outRange9:(NSRange)out_range9;");
  6664. // When the function name has to be wrapped.
  6665. FormatStyle Style = getLLVMStyle();
  6666. Style.IndentWrappedFunctionNames = false;
  6667. verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
  6668. "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
  6669. " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
  6670. "}",
  6671. Style);
  6672. Style.IndentWrappedFunctionNames = true;
  6673. verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
  6674. " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
  6675. " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
  6676. "}",
  6677. Style);
  6678. verifyFormat("- (int)sum:(vector<int>)numbers;");
  6679. verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
  6680. // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
  6681. // protocol lists (but not for template classes):
  6682. // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
  6683. verifyFormat("- (int (*)())foo:(int (*)())f;");
  6684. verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
  6685. // If there's no return type (very rare in practice!), LLVM and Google style
  6686. // agree.
  6687. verifyFormat("- foo;");
  6688. verifyFormat("- foo:(int)f;");
  6689. verifyGoogleFormat("- foo:(int)foo;");
  6690. }
  6691. TEST_F(FormatTest, FormatObjCInterface) {
  6692. verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
  6693. "@public\n"
  6694. " int field1;\n"
  6695. "@protected\n"
  6696. " int field2;\n"
  6697. "@private\n"
  6698. " int field3;\n"
  6699. "@package\n"
  6700. " int field4;\n"
  6701. "}\n"
  6702. "+ (id)init;\n"
  6703. "@end");
  6704. verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
  6705. " @public\n"
  6706. " int field1;\n"
  6707. " @protected\n"
  6708. " int field2;\n"
  6709. " @private\n"
  6710. " int field3;\n"
  6711. " @package\n"
  6712. " int field4;\n"
  6713. "}\n"
  6714. "+ (id)init;\n"
  6715. "@end");
  6716. verifyFormat("@interface /* wait for it */ Foo\n"
  6717. "+ (id)init;\n"
  6718. "// Look, a comment!\n"
  6719. "- (int)answerWith:(int)i;\n"
  6720. "@end");
  6721. verifyFormat("@interface Foo\n"
  6722. "@end\n"
  6723. "@interface Bar\n"
  6724. "@end");
  6725. verifyFormat("@interface Foo : Bar\n"
  6726. "+ (id)init;\n"
  6727. "@end");
  6728. verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
  6729. "+ (id)init;\n"
  6730. "@end");
  6731. verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
  6732. "+ (id)init;\n"
  6733. "@end");
  6734. verifyFormat("@interface Foo (HackStuff)\n"
  6735. "+ (id)init;\n"
  6736. "@end");
  6737. verifyFormat("@interface Foo ()\n"
  6738. "+ (id)init;\n"
  6739. "@end");
  6740. verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
  6741. "+ (id)init;\n"
  6742. "@end");
  6743. verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
  6744. "+ (id)init;\n"
  6745. "@end");
  6746. verifyFormat("@interface Foo {\n"
  6747. " int _i;\n"
  6748. "}\n"
  6749. "+ (id)init;\n"
  6750. "@end");
  6751. verifyFormat("@interface Foo : Bar {\n"
  6752. " int _i;\n"
  6753. "}\n"
  6754. "+ (id)init;\n"
  6755. "@end");
  6756. verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
  6757. " int _i;\n"
  6758. "}\n"
  6759. "+ (id)init;\n"
  6760. "@end");
  6761. verifyFormat("@interface Foo (HackStuff) {\n"
  6762. " int _i;\n"
  6763. "}\n"
  6764. "+ (id)init;\n"
  6765. "@end");
  6766. verifyFormat("@interface Foo () {\n"
  6767. " int _i;\n"
  6768. "}\n"
  6769. "+ (id)init;\n"
  6770. "@end");
  6771. verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
  6772. " int _i;\n"
  6773. "}\n"
  6774. "+ (id)init;\n"
  6775. "@end");
  6776. FormatStyle OnePerLine = getGoogleStyle();
  6777. OnePerLine.BinPackParameters = false;
  6778. verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n"
  6779. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6780. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6781. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6782. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
  6783. "}",
  6784. OnePerLine);
  6785. }
  6786. TEST_F(FormatTest, FormatObjCImplementation) {
  6787. verifyFormat("@implementation Foo : NSObject {\n"
  6788. "@public\n"
  6789. " int field1;\n"
  6790. "@protected\n"
  6791. " int field2;\n"
  6792. "@private\n"
  6793. " int field3;\n"
  6794. "@package\n"
  6795. " int field4;\n"
  6796. "}\n"
  6797. "+ (id)init {\n}\n"
  6798. "@end");
  6799. verifyGoogleFormat("@implementation Foo : NSObject {\n"
  6800. " @public\n"
  6801. " int field1;\n"
  6802. " @protected\n"
  6803. " int field2;\n"
  6804. " @private\n"
  6805. " int field3;\n"
  6806. " @package\n"
  6807. " int field4;\n"
  6808. "}\n"
  6809. "+ (id)init {\n}\n"
  6810. "@end");
  6811. verifyFormat("@implementation Foo\n"
  6812. "+ (id)init {\n"
  6813. " if (true)\n"
  6814. " return nil;\n"
  6815. "}\n"
  6816. "// Look, a comment!\n"
  6817. "- (int)answerWith:(int)i {\n"
  6818. " return i;\n"
  6819. "}\n"
  6820. "+ (int)answerWith:(int)i {\n"
  6821. " return i;\n"
  6822. "}\n"
  6823. "@end");
  6824. verifyFormat("@implementation Foo\n"
  6825. "@end\n"
  6826. "@implementation Bar\n"
  6827. "@end");
  6828. EXPECT_EQ("@implementation Foo : Bar\n"
  6829. "+ (id)init {\n}\n"
  6830. "- (void)foo {\n}\n"
  6831. "@end",
  6832. format("@implementation Foo : Bar\n"
  6833. "+(id)init{}\n"
  6834. "-(void)foo{}\n"
  6835. "@end"));
  6836. verifyFormat("@implementation Foo {\n"
  6837. " int _i;\n"
  6838. "}\n"
  6839. "+ (id)init {\n}\n"
  6840. "@end");
  6841. verifyFormat("@implementation Foo : Bar {\n"
  6842. " int _i;\n"
  6843. "}\n"
  6844. "+ (id)init {\n}\n"
  6845. "@end");
  6846. verifyFormat("@implementation Foo (HackStuff)\n"
  6847. "+ (id)init {\n}\n"
  6848. "@end");
  6849. verifyFormat("@implementation ObjcClass\n"
  6850. "- (void)method;\n"
  6851. "{}\n"
  6852. "@end");
  6853. }
  6854. TEST_F(FormatTest, FormatObjCProtocol) {
  6855. verifyFormat("@protocol Foo\n"
  6856. "@property(weak) id delegate;\n"
  6857. "- (NSUInteger)numberOfThings;\n"
  6858. "@end");
  6859. verifyFormat("@protocol MyProtocol <NSObject>\n"
  6860. "- (NSUInteger)numberOfThings;\n"
  6861. "@end");
  6862. verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
  6863. "- (NSUInteger)numberOfThings;\n"
  6864. "@end");
  6865. verifyFormat("@protocol Foo;\n"
  6866. "@protocol Bar;\n");
  6867. verifyFormat("@protocol Foo\n"
  6868. "@end\n"
  6869. "@protocol Bar\n"
  6870. "@end");
  6871. verifyFormat("@protocol myProtocol\n"
  6872. "- (void)mandatoryWithInt:(int)i;\n"
  6873. "@optional\n"
  6874. "- (void)optional;\n"
  6875. "@required\n"
  6876. "- (void)required;\n"
  6877. "@optional\n"
  6878. "@property(assign) int madProp;\n"
  6879. "@end\n");
  6880. verifyFormat("@property(nonatomic, assign, readonly)\n"
  6881. " int *looooooooooooooooooooooooooooongNumber;\n"
  6882. "@property(nonatomic, assign, readonly)\n"
  6883. " NSString *looooooooooooooooooooooooooooongName;");
  6884. verifyFormat("@implementation PR18406\n"
  6885. "}\n"
  6886. "@end");
  6887. }
  6888. TEST_F(FormatTest, FormatObjCMethodDeclarations) {
  6889. verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
  6890. " rect:(NSRect)theRect\n"
  6891. " interval:(float)theInterval {\n"
  6892. "}");
  6893. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6894. " longKeyword:(NSRect)theRect\n"
  6895. " longerKeyword:(float)theInterval\n"
  6896. " error:(NSError **)theError {\n"
  6897. "}");
  6898. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6899. " longKeyword:(NSRect)theRect\n"
  6900. " evenLongerKeyword:(float)theInterval\n"
  6901. " error:(NSError **)theError {\n"
  6902. "}");
  6903. verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
  6904. " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
  6905. " NS_DESIGNATED_INITIALIZER;",
  6906. getLLVMStyleWithColumns(60));
  6907. // Continuation indent width should win over aligning colons if the function
  6908. // name is long.
  6909. FormatStyle continuationStyle = getGoogleStyle();
  6910. continuationStyle.ColumnLimit = 40;
  6911. continuationStyle.IndentWrappedFunctionNames = true;
  6912. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6913. " dontAlignNamef:(NSRect)theRect {\n"
  6914. "}",
  6915. continuationStyle);
  6916. // Make sure we don't break aligning for short parameter names.
  6917. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6918. " aShortf:(NSRect)theRect {\n"
  6919. "}",
  6920. continuationStyle);
  6921. }
  6922. TEST_F(FormatTest, FormatObjCMethodExpr) {
  6923. verifyFormat("[foo bar:baz];");
  6924. verifyFormat("return [foo bar:baz];");
  6925. verifyFormat("return (a)[foo bar:baz];");
  6926. verifyFormat("f([foo bar:baz]);");
  6927. verifyFormat("f(2, [foo bar:baz]);");
  6928. verifyFormat("f(2, a ? b : c);");
  6929. verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
  6930. // Unary operators.
  6931. verifyFormat("int a = +[foo bar:baz];");
  6932. verifyFormat("int a = -[foo bar:baz];");
  6933. verifyFormat("int a = ![foo bar:baz];");
  6934. verifyFormat("int a = ~[foo bar:baz];");
  6935. verifyFormat("int a = ++[foo bar:baz];");
  6936. verifyFormat("int a = --[foo bar:baz];");
  6937. verifyFormat("int a = sizeof [foo bar:baz];");
  6938. verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
  6939. verifyFormat("int a = &[foo bar:baz];");
  6940. verifyFormat("int a = *[foo bar:baz];");
  6941. // FIXME: Make casts work, without breaking f()[4].
  6942. // verifyFormat("int a = (int)[foo bar:baz];");
  6943. // verifyFormat("return (int)[foo bar:baz];");
  6944. // verifyFormat("(void)[foo bar:baz];");
  6945. verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
  6946. // Binary operators.
  6947. verifyFormat("[foo bar:baz], [foo bar:baz];");
  6948. verifyFormat("[foo bar:baz] = [foo bar:baz];");
  6949. verifyFormat("[foo bar:baz] *= [foo bar:baz];");
  6950. verifyFormat("[foo bar:baz] /= [foo bar:baz];");
  6951. verifyFormat("[foo bar:baz] %= [foo bar:baz];");
  6952. verifyFormat("[foo bar:baz] += [foo bar:baz];");
  6953. verifyFormat("[foo bar:baz] -= [foo bar:baz];");
  6954. verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
  6955. verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
  6956. verifyFormat("[foo bar:baz] &= [foo bar:baz];");
  6957. verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
  6958. verifyFormat("[foo bar:baz] |= [foo bar:baz];");
  6959. verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
  6960. verifyFormat("[foo bar:baz] || [foo bar:baz];");
  6961. verifyFormat("[foo bar:baz] && [foo bar:baz];");
  6962. verifyFormat("[foo bar:baz] | [foo bar:baz];");
  6963. verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
  6964. verifyFormat("[foo bar:baz] & [foo bar:baz];");
  6965. verifyFormat("[foo bar:baz] == [foo bar:baz];");
  6966. verifyFormat("[foo bar:baz] != [foo bar:baz];");
  6967. verifyFormat("[foo bar:baz] >= [foo bar:baz];");
  6968. verifyFormat("[foo bar:baz] <= [foo bar:baz];");
  6969. verifyFormat("[foo bar:baz] > [foo bar:baz];");
  6970. verifyFormat("[foo bar:baz] < [foo bar:baz];");
  6971. verifyFormat("[foo bar:baz] >> [foo bar:baz];");
  6972. verifyFormat("[foo bar:baz] << [foo bar:baz];");
  6973. verifyFormat("[foo bar:baz] - [foo bar:baz];");
  6974. verifyFormat("[foo bar:baz] + [foo bar:baz];");
  6975. verifyFormat("[foo bar:baz] * [foo bar:baz];");
  6976. verifyFormat("[foo bar:baz] / [foo bar:baz];");
  6977. verifyFormat("[foo bar:baz] % [foo bar:baz];");
  6978. // Whew!
  6979. verifyFormat("return in[42];");
  6980. verifyFormat("for (auto v : in[1]) {\n}");
  6981. verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
  6982. verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
  6983. verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
  6984. verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
  6985. verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
  6986. verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
  6987. "}");
  6988. verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
  6989. verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
  6990. verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
  6991. verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
  6992. verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
  6993. verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
  6994. verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
  6995. verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
  6996. verifyFormat("[button setAction:@selector(zoomOut:)];");
  6997. verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
  6998. verifyFormat("arr[[self indexForFoo:a]];");
  6999. verifyFormat("throw [self errorFor:a];");
  7000. verifyFormat("@throw [self errorFor:a];");
  7001. verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
  7002. verifyFormat("[(id)foo bar:(id) ? baz : quux];");
  7003. verifyFormat("4 > 4 ? (id)a : (id)baz;");
  7004. // This tests that the formatter doesn't break after "backing" but before ":",
  7005. // which would be at 80 columns.
  7006. verifyFormat(
  7007. "void f() {\n"
  7008. " if ((self = [super initWithContentRect:contentRect\n"
  7009. " styleMask:styleMask ?: otherMask\n"
  7010. " backing:NSBackingStoreBuffered\n"
  7011. " defer:YES]))");
  7012. verifyFormat(
  7013. "[foo checkThatBreakingAfterColonWorksOk:\n"
  7014. " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
  7015. verifyFormat("[myObj short:arg1 // Force line break\n"
  7016. " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
  7017. " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
  7018. " error:arg4];");
  7019. verifyFormat(
  7020. "void f() {\n"
  7021. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  7022. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  7023. " pos.width(), pos.height())\n"
  7024. " styleMask:NSBorderlessWindowMask\n"
  7025. " backing:NSBackingStoreBuffered\n"
  7026. " defer:NO]);\n"
  7027. "}");
  7028. verifyFormat(
  7029. "void f() {\n"
  7030. " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
  7031. " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
  7032. " pos.width(), pos.height())\n"
  7033. " syeMask:NSBorderlessWindowMask\n"
  7034. " bking:NSBackingStoreBuffered\n"
  7035. " der:NO]);\n"
  7036. "}",
  7037. getLLVMStyleWithColumns(70));
  7038. verifyFormat(
  7039. "void f() {\n"
  7040. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  7041. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  7042. " pos.width(), pos.height())\n"
  7043. " styleMask:NSBorderlessWindowMask\n"
  7044. " backing:NSBackingStoreBuffered\n"
  7045. " defer:NO]);\n"
  7046. "}",
  7047. getChromiumStyle(FormatStyle::LK_Cpp));
  7048. verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
  7049. " with:contentsNativeView];");
  7050. verifyFormat(
  7051. "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
  7052. " owner:nillllll];");
  7053. verifyFormat(
  7054. "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
  7055. " forType:kBookmarkButtonDragType];");
  7056. verifyFormat("[defaultCenter addObserver:self\n"
  7057. " selector:@selector(willEnterFullscreen)\n"
  7058. " name:kWillEnterFullscreenNotification\n"
  7059. " object:nil];");
  7060. verifyFormat("[image_rep drawInRect:drawRect\n"
  7061. " fromRect:NSZeroRect\n"
  7062. " operation:NSCompositeCopy\n"
  7063. " fraction:1.0\n"
  7064. " respectFlipped:NO\n"
  7065. " hints:nil];");
  7066. verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7067. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  7068. verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  7069. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  7070. verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
  7071. " aaaaaaaaaaaaaaaaaaaaaa];");
  7072. verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
  7073. " .aaaaaaaa];", // FIXME: Indentation seems off.
  7074. getLLVMStyleWithColumns(60));
  7075. verifyFormat(
  7076. "scoped_nsobject<NSTextField> message(\n"
  7077. " // The frame will be fixed up when |-setMessageText:| is called.\n"
  7078. " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
  7079. verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
  7080. " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
  7081. " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
  7082. " aaaa:bbb];");
  7083. verifyFormat("[self param:function( //\n"
  7084. " parameter)]");
  7085. verifyFormat(
  7086. "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  7087. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  7088. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
  7089. // FIXME: This violates the column limit.
  7090. verifyFormat(
  7091. "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7092. " aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
  7093. " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];",
  7094. getLLVMStyleWithColumns(60));
  7095. // Variadic parameters.
  7096. verifyFormat(
  7097. "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
  7098. verifyFormat(
  7099. "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  7100. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  7101. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
  7102. verifyFormat("[self // break\n"
  7103. " a:a\n"
  7104. " aaa:aaa];");
  7105. verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
  7106. " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
  7107. }
  7108. TEST_F(FormatTest, ObjCAt) {
  7109. verifyFormat("@autoreleasepool");
  7110. verifyFormat("@catch");
  7111. verifyFormat("@class");
  7112. verifyFormat("@compatibility_alias");
  7113. verifyFormat("@defs");
  7114. verifyFormat("@dynamic");
  7115. verifyFormat("@encode");
  7116. verifyFormat("@end");
  7117. verifyFormat("@finally");
  7118. verifyFormat("@implementation");
  7119. verifyFormat("@import");
  7120. verifyFormat("@interface");
  7121. verifyFormat("@optional");
  7122. verifyFormat("@package");
  7123. verifyFormat("@private");
  7124. verifyFormat("@property");
  7125. verifyFormat("@protected");
  7126. verifyFormat("@protocol");
  7127. verifyFormat("@public");
  7128. verifyFormat("@required");
  7129. verifyFormat("@selector");
  7130. verifyFormat("@synchronized");
  7131. verifyFormat("@synthesize");
  7132. verifyFormat("@throw");
  7133. verifyFormat("@try");
  7134. EXPECT_EQ("@interface", format("@ interface"));
  7135. // The precise formatting of this doesn't matter, nobody writes code like
  7136. // this.
  7137. verifyFormat("@ /*foo*/ interface");
  7138. }
  7139. TEST_F(FormatTest, ObjCSnippets) {
  7140. verifyFormat("@autoreleasepool {\n"
  7141. " foo();\n"
  7142. "}");
  7143. verifyFormat("@class Foo, Bar;");
  7144. verifyFormat("@compatibility_alias AliasName ExistingClass;");
  7145. verifyFormat("@dynamic textColor;");
  7146. verifyFormat("char *buf1 = @encode(int *);");
  7147. verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
  7148. verifyFormat("char *buf1 = @encode(int **);");
  7149. verifyFormat("Protocol *proto = @protocol(p1);");
  7150. verifyFormat("SEL s = @selector(foo:);");
  7151. verifyFormat("@synchronized(self) {\n"
  7152. " f();\n"
  7153. "}");
  7154. verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  7155. verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  7156. verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
  7157. verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
  7158. verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
  7159. verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
  7160. getMozillaStyle());
  7161. verifyFormat("@property BOOL editable;", getMozillaStyle());
  7162. verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
  7163. getWebKitStyle());
  7164. verifyFormat("@property BOOL editable;", getWebKitStyle());
  7165. verifyFormat("@import foo.bar;\n"
  7166. "@import baz;");
  7167. }
  7168. TEST_F(FormatTest, ObjCForIn) {
  7169. verifyFormat("- (void)test {\n"
  7170. " for (NSString *n in arrayOfStrings) {\n"
  7171. " foo(n);\n"
  7172. " }\n"
  7173. "}");
  7174. verifyFormat("- (void)test {\n"
  7175. " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n"
  7176. " foo(n);\n"
  7177. " }\n"
  7178. "}");
  7179. }
  7180. TEST_F(FormatTest, ObjCLiterals) {
  7181. verifyFormat("@\"String\"");
  7182. verifyFormat("@1");
  7183. verifyFormat("@+4.8");
  7184. verifyFormat("@-4");
  7185. verifyFormat("@1LL");
  7186. verifyFormat("@.5");
  7187. verifyFormat("@'c'");
  7188. verifyFormat("@true");
  7189. verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
  7190. verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
  7191. verifyFormat("NSNumber *favoriteColor = @(Green);");
  7192. verifyFormat("NSString *path = @(getenv(\"PATH\"));");
  7193. verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
  7194. }
  7195. TEST_F(FormatTest, ObjCDictLiterals) {
  7196. verifyFormat("@{");
  7197. verifyFormat("@{}");
  7198. verifyFormat("@{@\"one\" : @1}");
  7199. verifyFormat("return @{@\"one\" : @1;");
  7200. verifyFormat("@{@\"one\" : @1}");
  7201. verifyFormat("@{@\"one\" : @{@2 : @1}}");
  7202. verifyFormat("@{\n"
  7203. " @\"one\" : @{@2 : @1},\n"
  7204. "}");
  7205. verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
  7206. verifyIncompleteFormat("[self setDict:@{}");
  7207. verifyIncompleteFormat("[self setDict:@{@1 : @2}");
  7208. verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
  7209. verifyFormat(
  7210. "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
  7211. verifyFormat(
  7212. "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
  7213. verifyFormat("NSDictionary *d = @{\n"
  7214. " @\"nam\" : NSUserNam(),\n"
  7215. " @\"dte\" : [NSDate date],\n"
  7216. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  7217. "};");
  7218. verifyFormat(
  7219. "@{\n"
  7220. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  7221. "regularFont,\n"
  7222. "};");
  7223. verifyGoogleFormat(
  7224. "@{\n"
  7225. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  7226. "regularFont,\n"
  7227. "};");
  7228. verifyFormat(
  7229. "@{\n"
  7230. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
  7231. " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
  7232. "};");
  7233. // We should try to be robust in case someone forgets the "@".
  7234. verifyFormat("NSDictionary *d = {\n"
  7235. " @\"nam\" : NSUserNam(),\n"
  7236. " @\"dte\" : [NSDate date],\n"
  7237. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  7238. "};");
  7239. verifyFormat("NSMutableDictionary *dictionary =\n"
  7240. " [NSMutableDictionary dictionaryWithDictionary:@{\n"
  7241. " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
  7242. " bbbbbbbbbbbbbbbbbb : bbbbb,\n"
  7243. " cccccccccccccccc : ccccccccccccccc\n"
  7244. " }];");
  7245. // Ensure that casts before the key are kept on the same line as the key.
  7246. verifyFormat(
  7247. "NSDictionary *d = @{\n"
  7248. " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n"
  7249. " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n"
  7250. "};");
  7251. }
  7252. TEST_F(FormatTest, ObjCArrayLiterals) {
  7253. verifyIncompleteFormat("@[");
  7254. verifyFormat("@[]");
  7255. verifyFormat(
  7256. "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
  7257. verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
  7258. verifyFormat("NSArray *array = @[ [foo description] ];");
  7259. verifyFormat(
  7260. "NSArray *some_variable = @[\n"
  7261. " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
  7262. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7263. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7264. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7265. "];");
  7266. verifyFormat(
  7267. "NSArray *some_variable = @[\n"
  7268. " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
  7269. " @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n"
  7270. "];");
  7271. verifyFormat("NSArray *some_variable = @[\n"
  7272. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7273. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7274. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7275. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7276. "];");
  7277. verifyFormat("NSArray *array = @[\n"
  7278. " @\"a\",\n"
  7279. " @\"a\",\n" // Trailing comma -> one per line.
  7280. "];");
  7281. // We should try to be robust in case someone forgets the "@".
  7282. verifyFormat("NSArray *some_variable = [\n"
  7283. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7284. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7285. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7286. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7287. "];");
  7288. verifyFormat(
  7289. "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
  7290. " index:(NSUInteger)index\n"
  7291. " nonDigitAttributes:\n"
  7292. " (NSDictionary *)noDigitAttributes;");
  7293. verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
  7294. " NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
  7295. "]];");
  7296. }
  7297. TEST_F(FormatTest, BreaksStringLiterals) {
  7298. EXPECT_EQ("\"some text \"\n"
  7299. "\"other\";",
  7300. format("\"some text other\";", getLLVMStyleWithColumns(12)));
  7301. EXPECT_EQ("\"some text \"\n"
  7302. "\"other\";",
  7303. format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
  7304. EXPECT_EQ(
  7305. "#define A \\\n"
  7306. " \"some \" \\\n"
  7307. " \"text \" \\\n"
  7308. " \"other\";",
  7309. format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
  7310. EXPECT_EQ(
  7311. "#define A \\\n"
  7312. " \"so \" \\\n"
  7313. " \"text \" \\\n"
  7314. " \"other\";",
  7315. format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
  7316. EXPECT_EQ("\"some text\"",
  7317. format("\"some text\"", getLLVMStyleWithColumns(1)));
  7318. EXPECT_EQ("\"some text\"",
  7319. format("\"some text\"", getLLVMStyleWithColumns(11)));
  7320. EXPECT_EQ("\"some \"\n"
  7321. "\"text\"",
  7322. format("\"some text\"", getLLVMStyleWithColumns(10)));
  7323. EXPECT_EQ("\"some \"\n"
  7324. "\"text\"",
  7325. format("\"some text\"", getLLVMStyleWithColumns(7)));
  7326. EXPECT_EQ("\"some\"\n"
  7327. "\" tex\"\n"
  7328. "\"t\"",
  7329. format("\"some text\"", getLLVMStyleWithColumns(6)));
  7330. EXPECT_EQ("\"some\"\n"
  7331. "\" tex\"\n"
  7332. "\" and\"",
  7333. format("\"some tex and\"", getLLVMStyleWithColumns(6)));
  7334. EXPECT_EQ("\"some\"\n"
  7335. "\"/tex\"\n"
  7336. "\"/and\"",
  7337. format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
  7338. EXPECT_EQ("variable =\n"
  7339. " \"long string \"\n"
  7340. " \"literal\";",
  7341. format("variable = \"long string literal\";",
  7342. getLLVMStyleWithColumns(20)));
  7343. EXPECT_EQ("variable = f(\n"
  7344. " \"long string \"\n"
  7345. " \"literal\",\n"
  7346. " short,\n"
  7347. " loooooooooooooooooooong);",
  7348. format("variable = f(\"long string literal\", short, "
  7349. "loooooooooooooooooooong);",
  7350. getLLVMStyleWithColumns(20)));
  7351. EXPECT_EQ(
  7352. "f(g(\"long string \"\n"
  7353. " \"literal\"),\n"
  7354. " b);",
  7355. format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
  7356. EXPECT_EQ("f(g(\"long string \"\n"
  7357. " \"literal\",\n"
  7358. " a),\n"
  7359. " b);",
  7360. format("f(g(\"long string literal\", a), b);",
  7361. getLLVMStyleWithColumns(20)));
  7362. EXPECT_EQ(
  7363. "f(\"one two\".split(\n"
  7364. " variable));",
  7365. format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
  7366. EXPECT_EQ("f(\"one two three four five six \"\n"
  7367. " \"seven\".split(\n"
  7368. " really_looooong_variable));",
  7369. format("f(\"one two three four five six seven\"."
  7370. "split(really_looooong_variable));",
  7371. getLLVMStyleWithColumns(33)));
  7372. EXPECT_EQ("f(\"some \"\n"
  7373. " \"text\",\n"
  7374. " other);",
  7375. format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
  7376. // Only break as a last resort.
  7377. verifyFormat(
  7378. "aaaaaaaaaaaaaaaaaaaa(\n"
  7379. " aaaaaaaaaaaaaaaaaaaa,\n"
  7380. " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
  7381. EXPECT_EQ("\"splitmea\"\n"
  7382. "\"trandomp\"\n"
  7383. "\"oint\"",
  7384. format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
  7385. EXPECT_EQ("\"split/\"\n"
  7386. "\"pathat/\"\n"
  7387. "\"slashes\"",
  7388. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  7389. EXPECT_EQ("\"split/\"\n"
  7390. "\"pathat/\"\n"
  7391. "\"slashes\"",
  7392. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  7393. EXPECT_EQ("\"split at \"\n"
  7394. "\"spaces/at/\"\n"
  7395. "\"slashes.at.any$\"\n"
  7396. "\"non-alphanumeric%\"\n"
  7397. "\"1111111111characte\"\n"
  7398. "\"rs\"",
  7399. format("\"split at "
  7400. "spaces/at/"
  7401. "slashes.at."
  7402. "any$non-"
  7403. "alphanumeric%"
  7404. "1111111111characte"
  7405. "rs\"",
  7406. getLLVMStyleWithColumns(20)));
  7407. // Verify that splitting the strings understands
  7408. // Style::AlwaysBreakBeforeMultilineStrings.
  7409. EXPECT_EQ(
  7410. "aaaaaaaaaaaa(\n"
  7411. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
  7412. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
  7413. format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
  7414. "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
  7415. "aaaaaaaaaaaaaaaaaaaaaa\");",
  7416. getGoogleStyle()));
  7417. EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  7418. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
  7419. format("return \"aaaaaaaaaaaaaaaaaaaaaa "
  7420. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
  7421. "aaaaaaaaaaaaaaaaaaaaaa\";",
  7422. getGoogleStyle()));
  7423. EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  7424. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  7425. format("llvm::outs() << "
  7426. "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
  7427. "aaaaaaaaaaaaaaaaaaa\";"));
  7428. EXPECT_EQ("ffff(\n"
  7429. " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  7430. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  7431. format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  7432. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  7433. getGoogleStyle()));
  7434. FormatStyle Style = getLLVMStyleWithColumns(12);
  7435. Style.BreakStringLiterals = false;
  7436. EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
  7437. FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
  7438. AlignLeft.AlignEscapedNewlinesLeft = true;
  7439. EXPECT_EQ("#define A \\\n"
  7440. " \"some \" \\\n"
  7441. " \"text \" \\\n"
  7442. " \"other\";",
  7443. format("#define A \"some text other\";", AlignLeft));
  7444. }
  7445. TEST_F(FormatTest, FullyRemoveEmptyLines) {
  7446. FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
  7447. NoEmptyLines.MaxEmptyLinesToKeep = 0;
  7448. EXPECT_EQ("int i = a(b());",
  7449. format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
  7450. }
  7451. TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
  7452. EXPECT_EQ(
  7453. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  7454. "(\n"
  7455. " \"x\t\");",
  7456. format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  7457. "aaaaaaa("
  7458. "\"x\t\");"));
  7459. }
  7460. TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
  7461. EXPECT_EQ(
  7462. "u8\"utf8 string \"\n"
  7463. "u8\"literal\";",
  7464. format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
  7465. EXPECT_EQ(
  7466. "u\"utf16 string \"\n"
  7467. "u\"literal\";",
  7468. format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
  7469. EXPECT_EQ(
  7470. "U\"utf32 string \"\n"
  7471. "U\"literal\";",
  7472. format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
  7473. EXPECT_EQ("L\"wide string \"\n"
  7474. "L\"literal\";",
  7475. format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
  7476. EXPECT_EQ("@\"NSString \"\n"
  7477. "@\"literal\";",
  7478. format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
  7479. // This input makes clang-format try to split the incomplete unicode escape
  7480. // sequence, which used to lead to a crasher.
  7481. verifyNoCrash(
  7482. "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  7483. getLLVMStyleWithColumns(60));
  7484. }
  7485. TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
  7486. FormatStyle Style = getGoogleStyleWithColumns(15);
  7487. EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
  7488. EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
  7489. EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
  7490. EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
  7491. EXPECT_EQ("u8R\"x(raw literal)x\";",
  7492. format("u8R\"x(raw literal)x\";", Style));
  7493. }
  7494. TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
  7495. FormatStyle Style = getLLVMStyleWithColumns(20);
  7496. EXPECT_EQ(
  7497. "_T(\"aaaaaaaaaaaaaa\")\n"
  7498. "_T(\"aaaaaaaaaaaaaa\")\n"
  7499. "_T(\"aaaaaaaaaaaa\")",
  7500. format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
  7501. EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
  7502. " _T(\"aaaaaa\"),\n"
  7503. " z);",
  7504. format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
  7505. // FIXME: Handle embedded spaces in one iteration.
  7506. // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
  7507. // "_T(\"aaaaaaaaaaaaa\")\n"
  7508. // "_T(\"aaaaaaaaaaaaa\")\n"
  7509. // "_T(\"a\")",
  7510. // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  7511. // getLLVMStyleWithColumns(20)));
  7512. EXPECT_EQ(
  7513. "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  7514. format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
  7515. EXPECT_EQ("f(\n"
  7516. "#if !TEST\n"
  7517. " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
  7518. "#endif\n"
  7519. " );",
  7520. format("f(\n"
  7521. "#if !TEST\n"
  7522. "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
  7523. "#endif\n"
  7524. ");"));
  7525. EXPECT_EQ("f(\n"
  7526. "\n"
  7527. " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
  7528. format("f(\n"
  7529. "\n"
  7530. "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
  7531. }
  7532. TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
  7533. EXPECT_EQ(
  7534. "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7535. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7536. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  7537. format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7538. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7539. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
  7540. }
  7541. TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
  7542. EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
  7543. format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle()));
  7544. EXPECT_EQ("fffffffffff(g(R\"x(\n"
  7545. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7546. ")x\",\n"
  7547. " a),\n"
  7548. " b);",
  7549. format("fffffffffff(g(R\"x(\n"
  7550. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7551. ")x\", a), b);",
  7552. getGoogleStyleWithColumns(20)));
  7553. EXPECT_EQ("fffffffffff(\n"
  7554. " g(R\"x(qqq\n"
  7555. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7556. ")x\",\n"
  7557. " a),\n"
  7558. " b);",
  7559. format("fffffffffff(g(R\"x(qqq\n"
  7560. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7561. ")x\", a), b);",
  7562. getGoogleStyleWithColumns(20)));
  7563. EXPECT_EQ("fffffffffff(R\"x(\n"
  7564. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7565. ")x\");",
  7566. format("fffffffffff(R\"x(\n"
  7567. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7568. ")x\");",
  7569. getGoogleStyleWithColumns(20)));
  7570. EXPECT_EQ("fffffffffff(R\"x(\n"
  7571. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7572. ")x\" + bbbbbb);",
  7573. format("fffffffffff(R\"x(\n"
  7574. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7575. ")x\" + bbbbbb);",
  7576. getGoogleStyleWithColumns(20)));
  7577. EXPECT_EQ("fffffffffff(\n"
  7578. " R\"x(\n"
  7579. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7580. ")x\" +\n"
  7581. " bbbbbb);",
  7582. format("fffffffffff(\n"
  7583. " R\"x(\n"
  7584. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7585. ")x\" + bbbbbb);",
  7586. getGoogleStyleWithColumns(20)));
  7587. }
  7588. TEST_F(FormatTest, SkipsUnknownStringLiterals) {
  7589. verifyFormat("string a = \"unterminated;");
  7590. EXPECT_EQ("function(\"unterminated,\n"
  7591. " OtherParameter);",
  7592. format("function( \"unterminated,\n"
  7593. " OtherParameter);"));
  7594. }
  7595. TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
  7596. FormatStyle Style = getLLVMStyle();
  7597. Style.Standard = FormatStyle::LS_Cpp03;
  7598. EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
  7599. format("#define x(_a) printf(\"foo\"_a);", Style));
  7600. }
  7601. TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
  7602. TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
  7603. EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
  7604. " \"ddeeefff\");",
  7605. format("someFunction(\"aaabbbcccdddeeefff\");",
  7606. getLLVMStyleWithColumns(25)));
  7607. EXPECT_EQ("someFunction1234567890(\n"
  7608. " \"aaabbbcccdddeeefff\");",
  7609. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  7610. getLLVMStyleWithColumns(26)));
  7611. EXPECT_EQ("someFunction1234567890(\n"
  7612. " \"aaabbbcccdddeeeff\"\n"
  7613. " \"f\");",
  7614. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  7615. getLLVMStyleWithColumns(25)));
  7616. EXPECT_EQ("someFunction1234567890(\n"
  7617. " \"aaabbbcccdddeeeff\"\n"
  7618. " \"f\");",
  7619. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  7620. getLLVMStyleWithColumns(24)));
  7621. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  7622. " \"ddde \"\n"
  7623. " \"efff\");",
  7624. format("someFunction(\"aaabbbcc ddde efff\");",
  7625. getLLVMStyleWithColumns(25)));
  7626. EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
  7627. " \"ddeeefff\");",
  7628. format("someFunction(\"aaabbbccc ddeeefff\");",
  7629. getLLVMStyleWithColumns(25)));
  7630. EXPECT_EQ("someFunction1234567890(\n"
  7631. " \"aaabb \"\n"
  7632. " \"cccdddeeefff\");",
  7633. format("someFunction1234567890(\"aaabb cccdddeeefff\");",
  7634. getLLVMStyleWithColumns(25)));
  7635. EXPECT_EQ("#define A \\\n"
  7636. " string s = \\\n"
  7637. " \"123456789\" \\\n"
  7638. " \"0\"; \\\n"
  7639. " int i;",
  7640. format("#define A string s = \"1234567890\"; int i;",
  7641. getLLVMStyleWithColumns(20)));
  7642. // FIXME: Put additional penalties on breaking at non-whitespace locations.
  7643. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  7644. " \"dddeeeff\"\n"
  7645. " \"f\");",
  7646. format("someFunction(\"aaabbbcc dddeeefff\");",
  7647. getLLVMStyleWithColumns(25)));
  7648. }
  7649. TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
  7650. EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
  7651. EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
  7652. EXPECT_EQ("\"test\"\n"
  7653. "\"\\n\"",
  7654. format("\"test\\n\"", getLLVMStyleWithColumns(7)));
  7655. EXPECT_EQ("\"tes\\\\\"\n"
  7656. "\"n\"",
  7657. format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
  7658. EXPECT_EQ("\"\\\\\\\\\"\n"
  7659. "\"\\n\"",
  7660. format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
  7661. EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
  7662. EXPECT_EQ("\"\\uff01\"\n"
  7663. "\"test\"",
  7664. format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
  7665. EXPECT_EQ("\"\\Uff01ff02\"",
  7666. format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
  7667. EXPECT_EQ("\"\\x000000000001\"\n"
  7668. "\"next\"",
  7669. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
  7670. EXPECT_EQ("\"\\x000000000001next\"",
  7671. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
  7672. EXPECT_EQ("\"\\x000000000001\"",
  7673. format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
  7674. EXPECT_EQ("\"test\"\n"
  7675. "\"\\000000\"\n"
  7676. "\"000001\"",
  7677. format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
  7678. EXPECT_EQ("\"test\\000\"\n"
  7679. "\"00000000\"\n"
  7680. "\"1\"",
  7681. format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
  7682. }
  7683. TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
  7684. verifyFormat("void f() {\n"
  7685. " return g() {}\n"
  7686. " void h() {}");
  7687. verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
  7688. "g();\n"
  7689. "}");
  7690. }
  7691. TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
  7692. verifyFormat(
  7693. "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
  7694. }
  7695. TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
  7696. verifyFormat("class X {\n"
  7697. " void f() {\n"
  7698. " }\n"
  7699. "};",
  7700. getLLVMStyleWithColumns(12));
  7701. }
  7702. TEST_F(FormatTest, ConfigurableIndentWidth) {
  7703. FormatStyle EightIndent = getLLVMStyleWithColumns(18);
  7704. EightIndent.IndentWidth = 8;
  7705. EightIndent.ContinuationIndentWidth = 8;
  7706. verifyFormat("void f() {\n"
  7707. " someFunction();\n"
  7708. " if (true) {\n"
  7709. " f();\n"
  7710. " }\n"
  7711. "}",
  7712. EightIndent);
  7713. verifyFormat("class X {\n"
  7714. " void f() {\n"
  7715. " }\n"
  7716. "};",
  7717. EightIndent);
  7718. verifyFormat("int x[] = {\n"
  7719. " call(),\n"
  7720. " call()};",
  7721. EightIndent);
  7722. }
  7723. TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
  7724. verifyFormat("double\n"
  7725. "f();",
  7726. getLLVMStyleWithColumns(8));
  7727. }
  7728. TEST_F(FormatTest, ConfigurableUseOfTab) {
  7729. FormatStyle Tab = getLLVMStyleWithColumns(42);
  7730. Tab.IndentWidth = 8;
  7731. Tab.UseTab = FormatStyle::UT_Always;
  7732. Tab.AlignEscapedNewlinesLeft = true;
  7733. EXPECT_EQ("if (aaaaaaaa && // q\n"
  7734. " bb)\t\t// w\n"
  7735. "\t;",
  7736. format("if (aaaaaaaa &&// q\n"
  7737. "bb)// w\n"
  7738. ";",
  7739. Tab));
  7740. EXPECT_EQ("if (aaa && bbb) // w\n"
  7741. "\t;",
  7742. format("if(aaa&&bbb)// w\n"
  7743. ";",
  7744. Tab));
  7745. verifyFormat("class X {\n"
  7746. "\tvoid f() {\n"
  7747. "\t\tsomeFunction(parameter1,\n"
  7748. "\t\t\t parameter2);\n"
  7749. "\t}\n"
  7750. "};",
  7751. Tab);
  7752. verifyFormat("#define A \\\n"
  7753. "\tvoid f() { \\\n"
  7754. "\t\tsomeFunction( \\\n"
  7755. "\t\t parameter1, \\\n"
  7756. "\t\t parameter2); \\\n"
  7757. "\t}",
  7758. Tab);
  7759. Tab.TabWidth = 4;
  7760. Tab.IndentWidth = 8;
  7761. verifyFormat("class TabWidth4Indent8 {\n"
  7762. "\t\tvoid f() {\n"
  7763. "\t\t\t\tsomeFunction(parameter1,\n"
  7764. "\t\t\t\t\t\t\t parameter2);\n"
  7765. "\t\t}\n"
  7766. "};",
  7767. Tab);
  7768. Tab.TabWidth = 4;
  7769. Tab.IndentWidth = 4;
  7770. verifyFormat("class TabWidth4Indent4 {\n"
  7771. "\tvoid f() {\n"
  7772. "\t\tsomeFunction(parameter1,\n"
  7773. "\t\t\t\t\t parameter2);\n"
  7774. "\t}\n"
  7775. "};",
  7776. Tab);
  7777. Tab.TabWidth = 8;
  7778. Tab.IndentWidth = 4;
  7779. verifyFormat("class TabWidth8Indent4 {\n"
  7780. " void f() {\n"
  7781. "\tsomeFunction(parameter1,\n"
  7782. "\t\t parameter2);\n"
  7783. " }\n"
  7784. "};",
  7785. Tab);
  7786. Tab.TabWidth = 8;
  7787. Tab.IndentWidth = 8;
  7788. EXPECT_EQ("/*\n"
  7789. "\t a\t\tcomment\n"
  7790. "\t in multiple lines\n"
  7791. " */",
  7792. format(" /*\t \t \n"
  7793. " \t \t a\t\tcomment\t \t\n"
  7794. " \t \t in multiple lines\t\n"
  7795. " \t */",
  7796. Tab));
  7797. Tab.UseTab = FormatStyle::UT_ForIndentation;
  7798. verifyFormat("{\n"
  7799. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7800. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7801. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7802. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7803. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7804. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7805. "};",
  7806. Tab);
  7807. verifyFormat("enum AA {\n"
  7808. "\ta1, // Force multiple lines\n"
  7809. "\ta2,\n"
  7810. "\ta3\n"
  7811. "};",
  7812. Tab);
  7813. EXPECT_EQ("if (aaaaaaaa && // q\n"
  7814. " bb) // w\n"
  7815. "\t;",
  7816. format("if (aaaaaaaa &&// q\n"
  7817. "bb)// w\n"
  7818. ";",
  7819. Tab));
  7820. verifyFormat("class X {\n"
  7821. "\tvoid f() {\n"
  7822. "\t\tsomeFunction(parameter1,\n"
  7823. "\t\t parameter2);\n"
  7824. "\t}\n"
  7825. "};",
  7826. Tab);
  7827. verifyFormat("{\n"
  7828. "\tQ(\n"
  7829. "\t {\n"
  7830. "\t\t int a;\n"
  7831. "\t\t someFunction(aaaaaaaa,\n"
  7832. "\t\t bbbbbbb);\n"
  7833. "\t },\n"
  7834. "\t p);\n"
  7835. "}",
  7836. Tab);
  7837. EXPECT_EQ("{\n"
  7838. "\t/* aaaa\n"
  7839. "\t bbbb */\n"
  7840. "}",
  7841. format("{\n"
  7842. "/* aaaa\n"
  7843. " bbbb */\n"
  7844. "}",
  7845. Tab));
  7846. EXPECT_EQ("{\n"
  7847. "\t/*\n"
  7848. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7849. "\t bbbbbbbbbbbbb\n"
  7850. "\t*/\n"
  7851. "}",
  7852. format("{\n"
  7853. "/*\n"
  7854. " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7855. "*/\n"
  7856. "}",
  7857. Tab));
  7858. EXPECT_EQ("{\n"
  7859. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7860. "\t// bbbbbbbbbbbbb\n"
  7861. "}",
  7862. format("{\n"
  7863. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7864. "}",
  7865. Tab));
  7866. EXPECT_EQ("{\n"
  7867. "\t/*\n"
  7868. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7869. "\t bbbbbbbbbbbbb\n"
  7870. "\t*/\n"
  7871. "}",
  7872. format("{\n"
  7873. "\t/*\n"
  7874. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7875. "\t*/\n"
  7876. "}",
  7877. Tab));
  7878. EXPECT_EQ("{\n"
  7879. "\t/*\n"
  7880. "\n"
  7881. "\t*/\n"
  7882. "}",
  7883. format("{\n"
  7884. "\t/*\n"
  7885. "\n"
  7886. "\t*/\n"
  7887. "}",
  7888. Tab));
  7889. EXPECT_EQ("{\n"
  7890. "\t/*\n"
  7891. " asdf\n"
  7892. "\t*/\n"
  7893. "}",
  7894. format("{\n"
  7895. "\t/*\n"
  7896. " asdf\n"
  7897. "\t*/\n"
  7898. "}",
  7899. Tab));
  7900. Tab.UseTab = FormatStyle::UT_Never;
  7901. EXPECT_EQ("/*\n"
  7902. " a\t\tcomment\n"
  7903. " in multiple lines\n"
  7904. " */",
  7905. format(" /*\t \t \n"
  7906. " \t \t a\t\tcomment\t \t\n"
  7907. " \t \t in multiple lines\t\n"
  7908. " \t */",
  7909. Tab));
  7910. EXPECT_EQ("/* some\n"
  7911. " comment */",
  7912. format(" \t \t /* some\n"
  7913. " \t \t comment */",
  7914. Tab));
  7915. EXPECT_EQ("int a; /* some\n"
  7916. " comment */",
  7917. format(" \t \t int a; /* some\n"
  7918. " \t \t comment */",
  7919. Tab));
  7920. EXPECT_EQ("int a; /* some\n"
  7921. "comment */",
  7922. format(" \t \t int\ta; /* some\n"
  7923. " \t \t comment */",
  7924. Tab));
  7925. EXPECT_EQ("f(\"\t\t\"); /* some\n"
  7926. " comment */",
  7927. format(" \t \t f(\"\t\t\"); /* some\n"
  7928. " \t \t comment */",
  7929. Tab));
  7930. EXPECT_EQ("{\n"
  7931. " /*\n"
  7932. " * Comment\n"
  7933. " */\n"
  7934. " int i;\n"
  7935. "}",
  7936. format("{\n"
  7937. "\t/*\n"
  7938. "\t * Comment\n"
  7939. "\t */\n"
  7940. "\t int i;\n"
  7941. "}"));
  7942. }
  7943. TEST_F(FormatTest, CalculatesOriginalColumn) {
  7944. EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7945. "q\"; /* some\n"
  7946. " comment */",
  7947. format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7948. "q\"; /* some\n"
  7949. " comment */",
  7950. getLLVMStyle()));
  7951. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  7952. "/* some\n"
  7953. " comment */",
  7954. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  7955. " /* some\n"
  7956. " comment */",
  7957. getLLVMStyle()));
  7958. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7959. "qqq\n"
  7960. "/* some\n"
  7961. " comment */",
  7962. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7963. "qqq\n"
  7964. " /* some\n"
  7965. " comment */",
  7966. getLLVMStyle()));
  7967. EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7968. "wwww; /* some\n"
  7969. " comment */",
  7970. format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7971. "wwww; /* some\n"
  7972. " comment */",
  7973. getLLVMStyle()));
  7974. }
  7975. TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
  7976. FormatStyle NoSpace = getLLVMStyle();
  7977. NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
  7978. verifyFormat("while(true)\n"
  7979. " continue;",
  7980. NoSpace);
  7981. verifyFormat("for(;;)\n"
  7982. " continue;",
  7983. NoSpace);
  7984. verifyFormat("if(true)\n"
  7985. " f();\n"
  7986. "else if(true)\n"
  7987. " f();",
  7988. NoSpace);
  7989. verifyFormat("do {\n"
  7990. " do_something();\n"
  7991. "} while(something());",
  7992. NoSpace);
  7993. verifyFormat("switch(x) {\n"
  7994. "default:\n"
  7995. " break;\n"
  7996. "}",
  7997. NoSpace);
  7998. verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
  7999. verifyFormat("size_t x = sizeof(x);", NoSpace);
  8000. verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
  8001. verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
  8002. verifyFormat("alignas(128) char a[128];", NoSpace);
  8003. verifyFormat("size_t x = alignof(MyType);", NoSpace);
  8004. verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
  8005. verifyFormat("int f() throw(Deprecated);", NoSpace);
  8006. verifyFormat("typedef void (*cb)(int);", NoSpace);
  8007. verifyFormat("T A::operator()();", NoSpace);
  8008. verifyFormat("X A::operator++(T);", NoSpace);
  8009. FormatStyle Space = getLLVMStyle();
  8010. Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
  8011. verifyFormat("int f ();", Space);
  8012. verifyFormat("void f (int a, T b) {\n"
  8013. " while (true)\n"
  8014. " continue;\n"
  8015. "}",
  8016. Space);
  8017. verifyFormat("if (true)\n"
  8018. " f ();\n"
  8019. "else if (true)\n"
  8020. " f ();",
  8021. Space);
  8022. verifyFormat("do {\n"
  8023. " do_something ();\n"
  8024. "} while (something ());",
  8025. Space);
  8026. verifyFormat("switch (x) {\n"
  8027. "default:\n"
  8028. " break;\n"
  8029. "}",
  8030. Space);
  8031. verifyFormat("A::A () : a (1) {}", Space);
  8032. verifyFormat("void f () __attribute__ ((asdf));", Space);
  8033. verifyFormat("*(&a + 1);\n"
  8034. "&((&a)[1]);\n"
  8035. "a[(b + c) * d];\n"
  8036. "(((a + 1) * 2) + 3) * 4;",
  8037. Space);
  8038. verifyFormat("#define A(x) x", Space);
  8039. verifyFormat("#define A (x) x", Space);
  8040. verifyFormat("#if defined(x)\n"
  8041. "#endif",
  8042. Space);
  8043. verifyFormat("auto i = std::make_unique<int> (5);", Space);
  8044. verifyFormat("size_t x = sizeof (x);", Space);
  8045. verifyFormat("auto f (int x) -> decltype (x);", Space);
  8046. verifyFormat("int f (T x) noexcept (x.create ());", Space);
  8047. verifyFormat("alignas (128) char a[128];", Space);
  8048. verifyFormat("size_t x = alignof (MyType);", Space);
  8049. verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
  8050. verifyFormat("int f () throw (Deprecated);", Space);
  8051. verifyFormat("typedef void (*cb) (int);", Space);
  8052. verifyFormat("T A::operator() ();", Space);
  8053. verifyFormat("X A::operator++ (T);", Space);
  8054. }
  8055. TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
  8056. FormatStyle Spaces = getLLVMStyle();
  8057. Spaces.SpacesInParentheses = true;
  8058. verifyFormat("call( x, y, z );", Spaces);
  8059. verifyFormat("call();", Spaces);
  8060. verifyFormat("std::function<void( int, int )> callback;", Spaces);
  8061. verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
  8062. Spaces);
  8063. verifyFormat("while ( (bool)1 )\n"
  8064. " continue;",
  8065. Spaces);
  8066. verifyFormat("for ( ;; )\n"
  8067. " continue;",
  8068. Spaces);
  8069. verifyFormat("if ( true )\n"
  8070. " f();\n"
  8071. "else if ( true )\n"
  8072. " f();",
  8073. Spaces);
  8074. verifyFormat("do {\n"
  8075. " do_something( (int)i );\n"
  8076. "} while ( something() );",
  8077. Spaces);
  8078. verifyFormat("switch ( x ) {\n"
  8079. "default:\n"
  8080. " break;\n"
  8081. "}",
  8082. Spaces);
  8083. Spaces.SpacesInParentheses = false;
  8084. Spaces.SpacesInCStyleCastParentheses = true;
  8085. verifyFormat("Type *A = ( Type * )P;", Spaces);
  8086. verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
  8087. verifyFormat("x = ( int32 )y;", Spaces);
  8088. verifyFormat("int a = ( int )(2.0f);", Spaces);
  8089. verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
  8090. verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
  8091. verifyFormat("#define x (( int )-1)", Spaces);
  8092. // Run the first set of tests again with:
  8093. Spaces.SpacesInParentheses = false;
  8094. Spaces.SpaceInEmptyParentheses = true;
  8095. Spaces.SpacesInCStyleCastParentheses = true;
  8096. verifyFormat("call(x, y, z);", Spaces);
  8097. verifyFormat("call( );", Spaces);
  8098. verifyFormat("std::function<void(int, int)> callback;", Spaces);
  8099. verifyFormat("while (( bool )1)\n"
  8100. " continue;",
  8101. Spaces);
  8102. verifyFormat("for (;;)\n"
  8103. " continue;",
  8104. Spaces);
  8105. verifyFormat("if (true)\n"
  8106. " f( );\n"
  8107. "else if (true)\n"
  8108. " f( );",
  8109. Spaces);
  8110. verifyFormat("do {\n"
  8111. " do_something(( int )i);\n"
  8112. "} while (something( ));",
  8113. Spaces);
  8114. verifyFormat("switch (x) {\n"
  8115. "default:\n"
  8116. " break;\n"
  8117. "}",
  8118. Spaces);
  8119. // Run the first set of tests again with:
  8120. Spaces.SpaceAfterCStyleCast = true;
  8121. verifyFormat("call(x, y, z);", Spaces);
  8122. verifyFormat("call( );", Spaces);
  8123. verifyFormat("std::function<void(int, int)> callback;", Spaces);
  8124. verifyFormat("while (( bool ) 1)\n"
  8125. " continue;",
  8126. Spaces);
  8127. verifyFormat("for (;;)\n"
  8128. " continue;",
  8129. Spaces);
  8130. verifyFormat("if (true)\n"
  8131. " f( );\n"
  8132. "else if (true)\n"
  8133. " f( );",
  8134. Spaces);
  8135. verifyFormat("do {\n"
  8136. " do_something(( int ) i);\n"
  8137. "} while (something( ));",
  8138. Spaces);
  8139. verifyFormat("switch (x) {\n"
  8140. "default:\n"
  8141. " break;\n"
  8142. "}",
  8143. Spaces);
  8144. // Run subset of tests again with:
  8145. Spaces.SpacesInCStyleCastParentheses = false;
  8146. Spaces.SpaceAfterCStyleCast = true;
  8147. verifyFormat("while ((bool) 1)\n"
  8148. " continue;",
  8149. Spaces);
  8150. verifyFormat("do {\n"
  8151. " do_something((int) i);\n"
  8152. "} while (something( ));",
  8153. Spaces);
  8154. }
  8155. TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
  8156. verifyFormat("int a[5];");
  8157. verifyFormat("a[3] += 42;");
  8158. FormatStyle Spaces = getLLVMStyle();
  8159. Spaces.SpacesInSquareBrackets = true;
  8160. // Lambdas unchanged.
  8161. verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
  8162. verifyFormat("return [i, args...] {};", Spaces);
  8163. // Not lambdas.
  8164. verifyFormat("int a[ 5 ];", Spaces);
  8165. verifyFormat("a[ 3 ] += 42;", Spaces);
  8166. verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
  8167. verifyFormat("double &operator[](int i) { return 0; }\n"
  8168. "int i;",
  8169. Spaces);
  8170. verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
  8171. verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
  8172. verifyFormat("int i = (*b)[ a ]->f();", Spaces);
  8173. }
  8174. TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
  8175. verifyFormat("int a = 5;");
  8176. verifyFormat("a += 42;");
  8177. verifyFormat("a or_eq 8;");
  8178. FormatStyle Spaces = getLLVMStyle();
  8179. Spaces.SpaceBeforeAssignmentOperators = false;
  8180. verifyFormat("int a= 5;", Spaces);
  8181. verifyFormat("a+= 42;", Spaces);
  8182. verifyFormat("a or_eq 8;", Spaces);
  8183. }
  8184. TEST_F(FormatTest, AlignConsecutiveAssignments) {
  8185. FormatStyle Alignment = getLLVMStyle();
  8186. Alignment.AlignConsecutiveAssignments = false;
  8187. verifyFormat("int a = 5;\n"
  8188. "int oneTwoThree = 123;",
  8189. Alignment);
  8190. verifyFormat("int a = 5;\n"
  8191. "int oneTwoThree = 123;",
  8192. Alignment);
  8193. Alignment.AlignConsecutiveAssignments = true;
  8194. verifyFormat("int a = 5;\n"
  8195. "int oneTwoThree = 123;",
  8196. Alignment);
  8197. verifyFormat("int a = method();\n"
  8198. "int oneTwoThree = 133;",
  8199. Alignment);
  8200. verifyFormat("a &= 5;\n"
  8201. "bcd *= 5;\n"
  8202. "ghtyf += 5;\n"
  8203. "dvfvdb -= 5;\n"
  8204. "a /= 5;\n"
  8205. "vdsvsv %= 5;\n"
  8206. "sfdbddfbdfbb ^= 5;\n"
  8207. "dvsdsv |= 5;\n"
  8208. "int dsvvdvsdvvv = 123;",
  8209. Alignment);
  8210. verifyFormat("int i = 1, j = 10;\n"
  8211. "something = 2000;",
  8212. Alignment);
  8213. verifyFormat("something = 2000;\n"
  8214. "int i = 1, j = 10;\n",
  8215. Alignment);
  8216. verifyFormat("something = 2000;\n"
  8217. "another = 911;\n"
  8218. "int i = 1, j = 10;\n"
  8219. "oneMore = 1;\n"
  8220. "i = 2;",
  8221. Alignment);
  8222. verifyFormat("int a = 5;\n"
  8223. "int one = 1;\n"
  8224. "method();\n"
  8225. "int oneTwoThree = 123;\n"
  8226. "int oneTwo = 12;",
  8227. Alignment);
  8228. verifyFormat("int oneTwoThree = 123;\n"
  8229. "int oneTwo = 12;\n"
  8230. "method();\n",
  8231. Alignment);
  8232. verifyFormat("int oneTwoThree = 123; // comment\n"
  8233. "int oneTwo = 12; // comment",
  8234. Alignment);
  8235. EXPECT_EQ("int a = 5;\n"
  8236. "\n"
  8237. "int oneTwoThree = 123;",
  8238. format("int a = 5;\n"
  8239. "\n"
  8240. "int oneTwoThree= 123;",
  8241. Alignment));
  8242. EXPECT_EQ("int a = 5;\n"
  8243. "int one = 1;\n"
  8244. "\n"
  8245. "int oneTwoThree = 123;",
  8246. format("int a = 5;\n"
  8247. "int one = 1;\n"
  8248. "\n"
  8249. "int oneTwoThree = 123;",
  8250. Alignment));
  8251. EXPECT_EQ("int a = 5;\n"
  8252. "int one = 1;\n"
  8253. "\n"
  8254. "int oneTwoThree = 123;\n"
  8255. "int oneTwo = 12;",
  8256. format("int a = 5;\n"
  8257. "int one = 1;\n"
  8258. "\n"
  8259. "int oneTwoThree = 123;\n"
  8260. "int oneTwo = 12;",
  8261. Alignment));
  8262. Alignment.AlignEscapedNewlinesLeft = true;
  8263. verifyFormat("#define A \\\n"
  8264. " int aaaa = 12; \\\n"
  8265. " int b = 23; \\\n"
  8266. " int ccc = 234; \\\n"
  8267. " int dddddddddd = 2345;",
  8268. Alignment);
  8269. Alignment.AlignEscapedNewlinesLeft = false;
  8270. verifyFormat("#define A "
  8271. " \\\n"
  8272. " int aaaa = 12; "
  8273. " \\\n"
  8274. " int b = 23; "
  8275. " \\\n"
  8276. " int ccc = 234; "
  8277. " \\\n"
  8278. " int dddddddddd = 2345;",
  8279. Alignment);
  8280. verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
  8281. "k = 4, int l = 5,\n"
  8282. " int m = 6) {\n"
  8283. " int j = 10;\n"
  8284. " otherThing = 1;\n"
  8285. "}",
  8286. Alignment);
  8287. verifyFormat("void SomeFunction(int parameter = 0) {\n"
  8288. " int i = 1;\n"
  8289. " int j = 2;\n"
  8290. " int big = 10000;\n"
  8291. "}",
  8292. Alignment);
  8293. verifyFormat("class C {\n"
  8294. "public:\n"
  8295. " int i = 1;\n"
  8296. " virtual void f() = 0;\n"
  8297. "};",
  8298. Alignment);
  8299. verifyFormat("int i = 1;\n"
  8300. "if (SomeType t = getSomething()) {\n"
  8301. "}\n"
  8302. "int j = 2;\n"
  8303. "int big = 10000;",
  8304. Alignment);
  8305. verifyFormat("int j = 7;\n"
  8306. "for (int k = 0; k < N; ++k) {\n"
  8307. "}\n"
  8308. "int j = 2;\n"
  8309. "int big = 10000;\n"
  8310. "}",
  8311. Alignment);
  8312. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  8313. verifyFormat("int i = 1;\n"
  8314. "LooooooooooongType loooooooooooooooooooooongVariable\n"
  8315. " = someLooooooooooooooooongFunction();\n"
  8316. "int j = 2;",
  8317. Alignment);
  8318. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
  8319. verifyFormat("int i = 1;\n"
  8320. "LooooooooooongType loooooooooooooooooooooongVariable =\n"
  8321. " someLooooooooooooooooongFunction();\n"
  8322. "int j = 2;",
  8323. Alignment);
  8324. verifyFormat("auto lambda = []() {\n"
  8325. " auto i = 0;\n"
  8326. " return 0;\n"
  8327. "};\n"
  8328. "int i = 0;\n"
  8329. "auto v = type{\n"
  8330. " i = 1, //\n"
  8331. " (i = 2), //\n"
  8332. " i = 3 //\n"
  8333. "};",
  8334. Alignment);
  8335. // FIXME: Should align all three assignments
  8336. verifyFormat(
  8337. "int i = 1;\n"
  8338. "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
  8339. " loooooooooooooooooooooongParameterB);\n"
  8340. "int j = 2;",
  8341. Alignment);
  8342. verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
  8343. " typename B = very_long_type_name_1,\n"
  8344. " typename T_2 = very_long_type_name_2>\n"
  8345. "auto foo() {}\n",
  8346. Alignment);
  8347. verifyFormat("int a, b = 1;\n"
  8348. "int c = 2;\n"
  8349. "int dd = 3;\n",
  8350. Alignment);
  8351. verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
  8352. "float b[1][] = {{3.f}};\n",
  8353. Alignment);
  8354. }
  8355. TEST_F(FormatTest, AlignConsecutiveDeclarations) {
  8356. FormatStyle Alignment = getLLVMStyle();
  8357. Alignment.AlignConsecutiveDeclarations = false;
  8358. verifyFormat("float const a = 5;\n"
  8359. "int oneTwoThree = 123;",
  8360. Alignment);
  8361. verifyFormat("int a = 5;\n"
  8362. "float const oneTwoThree = 123;",
  8363. Alignment);
  8364. Alignment.AlignConsecutiveDeclarations = true;
  8365. verifyFormat("float const a = 5;\n"
  8366. "int oneTwoThree = 123;",
  8367. Alignment);
  8368. verifyFormat("int a = method();\n"
  8369. "float const oneTwoThree = 133;",
  8370. Alignment);
  8371. verifyFormat("int i = 1, j = 10;\n"
  8372. "something = 2000;",
  8373. Alignment);
  8374. verifyFormat("something = 2000;\n"
  8375. "int i = 1, j = 10;\n",
  8376. Alignment);
  8377. verifyFormat("float something = 2000;\n"
  8378. "double another = 911;\n"
  8379. "int i = 1, j = 10;\n"
  8380. "const int *oneMore = 1;\n"
  8381. "unsigned i = 2;",
  8382. Alignment);
  8383. verifyFormat("float a = 5;\n"
  8384. "int one = 1;\n"
  8385. "method();\n"
  8386. "const double oneTwoThree = 123;\n"
  8387. "const unsigned int oneTwo = 12;",
  8388. Alignment);
  8389. verifyFormat("int oneTwoThree{0}; // comment\n"
  8390. "unsigned oneTwo; // comment",
  8391. Alignment);
  8392. EXPECT_EQ("float const a = 5;\n"
  8393. "\n"
  8394. "int oneTwoThree = 123;",
  8395. format("float const a = 5;\n"
  8396. "\n"
  8397. "int oneTwoThree= 123;",
  8398. Alignment));
  8399. EXPECT_EQ("float a = 5;\n"
  8400. "int one = 1;\n"
  8401. "\n"
  8402. "unsigned oneTwoThree = 123;",
  8403. format("float a = 5;\n"
  8404. "int one = 1;\n"
  8405. "\n"
  8406. "unsigned oneTwoThree = 123;",
  8407. Alignment));
  8408. EXPECT_EQ("float a = 5;\n"
  8409. "int one = 1;\n"
  8410. "\n"
  8411. "unsigned oneTwoThree = 123;\n"
  8412. "int oneTwo = 12;",
  8413. format("float a = 5;\n"
  8414. "int one = 1;\n"
  8415. "\n"
  8416. "unsigned oneTwoThree = 123;\n"
  8417. "int oneTwo = 12;",
  8418. Alignment));
  8419. Alignment.AlignConsecutiveAssignments = true;
  8420. verifyFormat("float something = 2000;\n"
  8421. "double another = 911;\n"
  8422. "int i = 1, j = 10;\n"
  8423. "const int *oneMore = 1;\n"
  8424. "unsigned i = 2;",
  8425. Alignment);
  8426. verifyFormat("int oneTwoThree = {0}; // comment\n"
  8427. "unsigned oneTwo = 0; // comment",
  8428. Alignment);
  8429. EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
  8430. " int const i = 1;\n"
  8431. " int * j = 2;\n"
  8432. " int big = 10000;\n"
  8433. "\n"
  8434. " unsigned oneTwoThree = 123;\n"
  8435. " int oneTwo = 12;\n"
  8436. " method();\n"
  8437. " float k = 2;\n"
  8438. " int ll = 10000;\n"
  8439. "}",
  8440. format("void SomeFunction(int parameter= 0) {\n"
  8441. " int const i= 1;\n"
  8442. " int *j=2;\n"
  8443. " int big = 10000;\n"
  8444. "\n"
  8445. "unsigned oneTwoThree =123;\n"
  8446. "int oneTwo = 12;\n"
  8447. " method();\n"
  8448. "float k= 2;\n"
  8449. "int ll=10000;\n"
  8450. "}",
  8451. Alignment));
  8452. Alignment.AlignConsecutiveAssignments = false;
  8453. Alignment.AlignEscapedNewlinesLeft = true;
  8454. verifyFormat("#define A \\\n"
  8455. " int aaaa = 12; \\\n"
  8456. " float b = 23; \\\n"
  8457. " const int ccc = 234; \\\n"
  8458. " unsigned dddddddddd = 2345;",
  8459. Alignment);
  8460. Alignment.AlignEscapedNewlinesLeft = false;
  8461. Alignment.ColumnLimit = 30;
  8462. verifyFormat("#define A \\\n"
  8463. " int aaaa = 12; \\\n"
  8464. " float b = 23; \\\n"
  8465. " const int ccc = 234; \\\n"
  8466. " int dddddddddd = 2345;",
  8467. Alignment);
  8468. Alignment.ColumnLimit = 80;
  8469. verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
  8470. "k = 4, int l = 5,\n"
  8471. " int m = 6) {\n"
  8472. " const int j = 10;\n"
  8473. " otherThing = 1;\n"
  8474. "}",
  8475. Alignment);
  8476. verifyFormat("void SomeFunction(int parameter = 0) {\n"
  8477. " int const i = 1;\n"
  8478. " int * j = 2;\n"
  8479. " int big = 10000;\n"
  8480. "}",
  8481. Alignment);
  8482. verifyFormat("class C {\n"
  8483. "public:\n"
  8484. " int i = 1;\n"
  8485. " virtual void f() = 0;\n"
  8486. "};",
  8487. Alignment);
  8488. verifyFormat("float i = 1;\n"
  8489. "if (SomeType t = getSomething()) {\n"
  8490. "}\n"
  8491. "const unsigned j = 2;\n"
  8492. "int big = 10000;",
  8493. Alignment);
  8494. verifyFormat("float j = 7;\n"
  8495. "for (int k = 0; k < N; ++k) {\n"
  8496. "}\n"
  8497. "unsigned j = 2;\n"
  8498. "int big = 10000;\n"
  8499. "}",
  8500. Alignment);
  8501. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  8502. verifyFormat("float i = 1;\n"
  8503. "LooooooooooongType loooooooooooooooooooooongVariable\n"
  8504. " = someLooooooooooooooooongFunction();\n"
  8505. "int j = 2;",
  8506. Alignment);
  8507. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
  8508. verifyFormat("int i = 1;\n"
  8509. "LooooooooooongType loooooooooooooooooooooongVariable =\n"
  8510. " someLooooooooooooooooongFunction();\n"
  8511. "int j = 2;",
  8512. Alignment);
  8513. Alignment.AlignConsecutiveAssignments = true;
  8514. verifyFormat("auto lambda = []() {\n"
  8515. " auto ii = 0;\n"
  8516. " float j = 0;\n"
  8517. " return 0;\n"
  8518. "};\n"
  8519. "int i = 0;\n"
  8520. "float i2 = 0;\n"
  8521. "auto v = type{\n"
  8522. " i = 1, //\n"
  8523. " (i = 2), //\n"
  8524. " i = 3 //\n"
  8525. "};",
  8526. Alignment);
  8527. Alignment.AlignConsecutiveAssignments = false;
  8528. // FIXME: Should align all three declarations
  8529. verifyFormat(
  8530. "int i = 1;\n"
  8531. "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
  8532. " loooooooooooooooooooooongParameterB);\n"
  8533. "int j = 2;",
  8534. Alignment);
  8535. // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
  8536. // We expect declarations and assignments to align, as long as it doesn't
  8537. // exceed the column limit, starting a new alignemnt sequence whenever it
  8538. // happens.
  8539. Alignment.AlignConsecutiveAssignments = true;
  8540. Alignment.ColumnLimit = 30;
  8541. verifyFormat("float ii = 1;\n"
  8542. "unsigned j = 2;\n"
  8543. "int someVerylongVariable = 1;\n"
  8544. "AnotherLongType ll = 123456;\n"
  8545. "VeryVeryLongType k = 2;\n"
  8546. "int myvar = 1;",
  8547. Alignment);
  8548. Alignment.ColumnLimit = 80;
  8549. Alignment.AlignConsecutiveAssignments = false;
  8550. verifyFormat(
  8551. "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
  8552. " typename LongType, typename B>\n"
  8553. "auto foo() {}\n",
  8554. Alignment);
  8555. verifyFormat("float a, b = 1;\n"
  8556. "int c = 2;\n"
  8557. "int dd = 3;\n",
  8558. Alignment);
  8559. verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
  8560. "float b[1][] = {{3.f}};\n",
  8561. Alignment);
  8562. Alignment.AlignConsecutiveAssignments = true;
  8563. verifyFormat("float a, b = 1;\n"
  8564. "int c = 2;\n"
  8565. "int dd = 3;\n",
  8566. Alignment);
  8567. verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
  8568. "float b[1][] = {{3.f}};\n",
  8569. Alignment);
  8570. Alignment.AlignConsecutiveAssignments = false;
  8571. Alignment.ColumnLimit = 30;
  8572. Alignment.BinPackParameters = false;
  8573. verifyFormat("void foo(float a,\n"
  8574. " float b,\n"
  8575. " int c,\n"
  8576. " uint32_t *d) {\n"
  8577. " int * e = 0;\n"
  8578. " float f = 0;\n"
  8579. " double g = 0;\n"
  8580. "}\n"
  8581. "void bar(ino_t a,\n"
  8582. " int b,\n"
  8583. " uint32_t *c,\n"
  8584. " bool d) {}\n",
  8585. Alignment);
  8586. Alignment.BinPackParameters = true;
  8587. Alignment.ColumnLimit = 80;
  8588. }
  8589. TEST_F(FormatTest, LinuxBraceBreaking) {
  8590. FormatStyle LinuxBraceStyle = getLLVMStyle();
  8591. LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
  8592. verifyFormat("namespace a\n"
  8593. "{\n"
  8594. "class A\n"
  8595. "{\n"
  8596. " void f()\n"
  8597. " {\n"
  8598. " if (true) {\n"
  8599. " a();\n"
  8600. " b();\n"
  8601. " } else {\n"
  8602. " a();\n"
  8603. " }\n"
  8604. " }\n"
  8605. " void g() { return; }\n"
  8606. "};\n"
  8607. "struct B {\n"
  8608. " int x;\n"
  8609. "};\n"
  8610. "}\n",
  8611. LinuxBraceStyle);
  8612. verifyFormat("enum X {\n"
  8613. " Y = 0,\n"
  8614. "}\n",
  8615. LinuxBraceStyle);
  8616. verifyFormat("struct S {\n"
  8617. " int Type;\n"
  8618. " union {\n"
  8619. " int x;\n"
  8620. " double y;\n"
  8621. " } Value;\n"
  8622. " class C\n"
  8623. " {\n"
  8624. " MyFavoriteType Value;\n"
  8625. " } Class;\n"
  8626. "}\n",
  8627. LinuxBraceStyle);
  8628. }
  8629. TEST_F(FormatTest, MozillaBraceBreaking) {
  8630. FormatStyle MozillaBraceStyle = getLLVMStyle();
  8631. MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
  8632. verifyFormat("namespace a {\n"
  8633. "class A\n"
  8634. "{\n"
  8635. " void f()\n"
  8636. " {\n"
  8637. " if (true) {\n"
  8638. " a();\n"
  8639. " b();\n"
  8640. " }\n"
  8641. " }\n"
  8642. " void g() { return; }\n"
  8643. "};\n"
  8644. "enum E\n"
  8645. "{\n"
  8646. " A,\n"
  8647. " // foo\n"
  8648. " B,\n"
  8649. " C\n"
  8650. "};\n"
  8651. "struct B\n"
  8652. "{\n"
  8653. " int x;\n"
  8654. "};\n"
  8655. "}\n",
  8656. MozillaBraceStyle);
  8657. verifyFormat("struct S\n"
  8658. "{\n"
  8659. " int Type;\n"
  8660. " union\n"
  8661. " {\n"
  8662. " int x;\n"
  8663. " double y;\n"
  8664. " } Value;\n"
  8665. " class C\n"
  8666. " {\n"
  8667. " MyFavoriteType Value;\n"
  8668. " } Class;\n"
  8669. "}\n",
  8670. MozillaBraceStyle);
  8671. }
  8672. TEST_F(FormatTest, StroustrupBraceBreaking) {
  8673. FormatStyle StroustrupBraceStyle = getLLVMStyle();
  8674. StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  8675. verifyFormat("namespace a {\n"
  8676. "class A {\n"
  8677. " void f()\n"
  8678. " {\n"
  8679. " if (true) {\n"
  8680. " a();\n"
  8681. " b();\n"
  8682. " }\n"
  8683. " }\n"
  8684. " void g() { return; }\n"
  8685. "};\n"
  8686. "struct B {\n"
  8687. " int x;\n"
  8688. "};\n"
  8689. "}\n",
  8690. StroustrupBraceStyle);
  8691. verifyFormat("void foo()\n"
  8692. "{\n"
  8693. " if (a) {\n"
  8694. " a();\n"
  8695. " }\n"
  8696. " else {\n"
  8697. " b();\n"
  8698. " }\n"
  8699. "}\n",
  8700. StroustrupBraceStyle);
  8701. verifyFormat("#ifdef _DEBUG\n"
  8702. "int foo(int i = 0)\n"
  8703. "#else\n"
  8704. "int foo(int i = 5)\n"
  8705. "#endif\n"
  8706. "{\n"
  8707. " return i;\n"
  8708. "}",
  8709. StroustrupBraceStyle);
  8710. verifyFormat("void foo() {}\n"
  8711. "void bar()\n"
  8712. "#ifdef _DEBUG\n"
  8713. "{\n"
  8714. " foo();\n"
  8715. "}\n"
  8716. "#else\n"
  8717. "{\n"
  8718. "}\n"
  8719. "#endif",
  8720. StroustrupBraceStyle);
  8721. verifyFormat("void foobar() { int i = 5; }\n"
  8722. "#ifdef _DEBUG\n"
  8723. "void bar() {}\n"
  8724. "#else\n"
  8725. "void bar() { foobar(); }\n"
  8726. "#endif",
  8727. StroustrupBraceStyle);
  8728. }
  8729. TEST_F(FormatTest, AllmanBraceBreaking) {
  8730. FormatStyle AllmanBraceStyle = getLLVMStyle();
  8731. AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
  8732. verifyFormat("namespace a\n"
  8733. "{\n"
  8734. "class A\n"
  8735. "{\n"
  8736. " void f()\n"
  8737. " {\n"
  8738. " if (true)\n"
  8739. " {\n"
  8740. " a();\n"
  8741. " b();\n"
  8742. " }\n"
  8743. " }\n"
  8744. " void g() { return; }\n"
  8745. "};\n"
  8746. "struct B\n"
  8747. "{\n"
  8748. " int x;\n"
  8749. "};\n"
  8750. "}",
  8751. AllmanBraceStyle);
  8752. verifyFormat("void f()\n"
  8753. "{\n"
  8754. " if (true)\n"
  8755. " {\n"
  8756. " a();\n"
  8757. " }\n"
  8758. " else if (false)\n"
  8759. " {\n"
  8760. " b();\n"
  8761. " }\n"
  8762. " else\n"
  8763. " {\n"
  8764. " c();\n"
  8765. " }\n"
  8766. "}\n",
  8767. AllmanBraceStyle);
  8768. verifyFormat("void f()\n"
  8769. "{\n"
  8770. " for (int i = 0; i < 10; ++i)\n"
  8771. " {\n"
  8772. " a();\n"
  8773. " }\n"
  8774. " while (false)\n"
  8775. " {\n"
  8776. " b();\n"
  8777. " }\n"
  8778. " do\n"
  8779. " {\n"
  8780. " c();\n"
  8781. " } while (false)\n"
  8782. "}\n",
  8783. AllmanBraceStyle);
  8784. verifyFormat("void f(int a)\n"
  8785. "{\n"
  8786. " switch (a)\n"
  8787. " {\n"
  8788. " case 0:\n"
  8789. " break;\n"
  8790. " case 1:\n"
  8791. " {\n"
  8792. " break;\n"
  8793. " }\n"
  8794. " case 2:\n"
  8795. " {\n"
  8796. " }\n"
  8797. " break;\n"
  8798. " default:\n"
  8799. " break;\n"
  8800. " }\n"
  8801. "}\n",
  8802. AllmanBraceStyle);
  8803. verifyFormat("enum X\n"
  8804. "{\n"
  8805. " Y = 0,\n"
  8806. "}\n",
  8807. AllmanBraceStyle);
  8808. verifyFormat("enum X\n"
  8809. "{\n"
  8810. " Y = 0\n"
  8811. "}\n",
  8812. AllmanBraceStyle);
  8813. verifyFormat("@interface BSApplicationController ()\n"
  8814. "{\n"
  8815. "@private\n"
  8816. " id _extraIvar;\n"
  8817. "}\n"
  8818. "@end\n",
  8819. AllmanBraceStyle);
  8820. verifyFormat("#ifdef _DEBUG\n"
  8821. "int foo(int i = 0)\n"
  8822. "#else\n"
  8823. "int foo(int i = 5)\n"
  8824. "#endif\n"
  8825. "{\n"
  8826. " return i;\n"
  8827. "}",
  8828. AllmanBraceStyle);
  8829. verifyFormat("void foo() {}\n"
  8830. "void bar()\n"
  8831. "#ifdef _DEBUG\n"
  8832. "{\n"
  8833. " foo();\n"
  8834. "}\n"
  8835. "#else\n"
  8836. "{\n"
  8837. "}\n"
  8838. "#endif",
  8839. AllmanBraceStyle);
  8840. verifyFormat("void foobar() { int i = 5; }\n"
  8841. "#ifdef _DEBUG\n"
  8842. "void bar() {}\n"
  8843. "#else\n"
  8844. "void bar() { foobar(); }\n"
  8845. "#endif",
  8846. AllmanBraceStyle);
  8847. // This shouldn't affect ObjC blocks..
  8848. verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
  8849. " // ...\n"
  8850. " int i;\n"
  8851. "}];",
  8852. AllmanBraceStyle);
  8853. verifyFormat("void (^block)(void) = ^{\n"
  8854. " // ...\n"
  8855. " int i;\n"
  8856. "};",
  8857. AllmanBraceStyle);
  8858. // .. or dict literals.
  8859. verifyFormat("void f()\n"
  8860. "{\n"
  8861. " [object someMethod:@{ @\"a\" : @\"b\" }];\n"
  8862. "}",
  8863. AllmanBraceStyle);
  8864. verifyFormat("int f()\n"
  8865. "{ // comment\n"
  8866. " return 42;\n"
  8867. "}",
  8868. AllmanBraceStyle);
  8869. AllmanBraceStyle.ColumnLimit = 19;
  8870. verifyFormat("void f() { int i; }", AllmanBraceStyle);
  8871. AllmanBraceStyle.ColumnLimit = 18;
  8872. verifyFormat("void f()\n"
  8873. "{\n"
  8874. " int i;\n"
  8875. "}",
  8876. AllmanBraceStyle);
  8877. AllmanBraceStyle.ColumnLimit = 80;
  8878. FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
  8879. BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
  8880. BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
  8881. verifyFormat("void f(bool b)\n"
  8882. "{\n"
  8883. " if (b)\n"
  8884. " {\n"
  8885. " return;\n"
  8886. " }\n"
  8887. "}\n",
  8888. BreakBeforeBraceShortIfs);
  8889. verifyFormat("void f(bool b)\n"
  8890. "{\n"
  8891. " if (b) return;\n"
  8892. "}\n",
  8893. BreakBeforeBraceShortIfs);
  8894. verifyFormat("void f(bool b)\n"
  8895. "{\n"
  8896. " while (b)\n"
  8897. " {\n"
  8898. " return;\n"
  8899. " }\n"
  8900. "}\n",
  8901. BreakBeforeBraceShortIfs);
  8902. }
  8903. TEST_F(FormatTest, GNUBraceBreaking) {
  8904. FormatStyle GNUBraceStyle = getLLVMStyle();
  8905. GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
  8906. verifyFormat("namespace a\n"
  8907. "{\n"
  8908. "class A\n"
  8909. "{\n"
  8910. " void f()\n"
  8911. " {\n"
  8912. " int a;\n"
  8913. " {\n"
  8914. " int b;\n"
  8915. " }\n"
  8916. " if (true)\n"
  8917. " {\n"
  8918. " a();\n"
  8919. " b();\n"
  8920. " }\n"
  8921. " }\n"
  8922. " void g() { return; }\n"
  8923. "}\n"
  8924. "}",
  8925. GNUBraceStyle);
  8926. verifyFormat("void f()\n"
  8927. "{\n"
  8928. " if (true)\n"
  8929. " {\n"
  8930. " a();\n"
  8931. " }\n"
  8932. " else if (false)\n"
  8933. " {\n"
  8934. " b();\n"
  8935. " }\n"
  8936. " else\n"
  8937. " {\n"
  8938. " c();\n"
  8939. " }\n"
  8940. "}\n",
  8941. GNUBraceStyle);
  8942. verifyFormat("void f()\n"
  8943. "{\n"
  8944. " for (int i = 0; i < 10; ++i)\n"
  8945. " {\n"
  8946. " a();\n"
  8947. " }\n"
  8948. " while (false)\n"
  8949. " {\n"
  8950. " b();\n"
  8951. " }\n"
  8952. " do\n"
  8953. " {\n"
  8954. " c();\n"
  8955. " }\n"
  8956. " while (false);\n"
  8957. "}\n",
  8958. GNUBraceStyle);
  8959. verifyFormat("void f(int a)\n"
  8960. "{\n"
  8961. " switch (a)\n"
  8962. " {\n"
  8963. " case 0:\n"
  8964. " break;\n"
  8965. " case 1:\n"
  8966. " {\n"
  8967. " break;\n"
  8968. " }\n"
  8969. " case 2:\n"
  8970. " {\n"
  8971. " }\n"
  8972. " break;\n"
  8973. " default:\n"
  8974. " break;\n"
  8975. " }\n"
  8976. "}\n",
  8977. GNUBraceStyle);
  8978. verifyFormat("enum X\n"
  8979. "{\n"
  8980. " Y = 0,\n"
  8981. "}\n",
  8982. GNUBraceStyle);
  8983. verifyFormat("@interface BSApplicationController ()\n"
  8984. "{\n"
  8985. "@private\n"
  8986. " id _extraIvar;\n"
  8987. "}\n"
  8988. "@end\n",
  8989. GNUBraceStyle);
  8990. verifyFormat("#ifdef _DEBUG\n"
  8991. "int foo(int i = 0)\n"
  8992. "#else\n"
  8993. "int foo(int i = 5)\n"
  8994. "#endif\n"
  8995. "{\n"
  8996. " return i;\n"
  8997. "}",
  8998. GNUBraceStyle);
  8999. verifyFormat("void foo() {}\n"
  9000. "void bar()\n"
  9001. "#ifdef _DEBUG\n"
  9002. "{\n"
  9003. " foo();\n"
  9004. "}\n"
  9005. "#else\n"
  9006. "{\n"
  9007. "}\n"
  9008. "#endif",
  9009. GNUBraceStyle);
  9010. verifyFormat("void foobar() { int i = 5; }\n"
  9011. "#ifdef _DEBUG\n"
  9012. "void bar() {}\n"
  9013. "#else\n"
  9014. "void bar() { foobar(); }\n"
  9015. "#endif",
  9016. GNUBraceStyle);
  9017. }
  9018. TEST_F(FormatTest, WebKitBraceBreaking) {
  9019. FormatStyle WebKitBraceStyle = getLLVMStyle();
  9020. WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
  9021. verifyFormat("namespace a {\n"
  9022. "class A {\n"
  9023. " void f()\n"
  9024. " {\n"
  9025. " if (true) {\n"
  9026. " a();\n"
  9027. " b();\n"
  9028. " }\n"
  9029. " }\n"
  9030. " void g() { return; }\n"
  9031. "};\n"
  9032. "enum E {\n"
  9033. " A,\n"
  9034. " // foo\n"
  9035. " B,\n"
  9036. " C\n"
  9037. "};\n"
  9038. "struct B {\n"
  9039. " int x;\n"
  9040. "};\n"
  9041. "}\n",
  9042. WebKitBraceStyle);
  9043. verifyFormat("struct S {\n"
  9044. " int Type;\n"
  9045. " union {\n"
  9046. " int x;\n"
  9047. " double y;\n"
  9048. " } Value;\n"
  9049. " class C {\n"
  9050. " MyFavoriteType Value;\n"
  9051. " } Class;\n"
  9052. "};\n",
  9053. WebKitBraceStyle);
  9054. }
  9055. TEST_F(FormatTest, CatchExceptionReferenceBinding) {
  9056. verifyFormat("void f() {\n"
  9057. " try {\n"
  9058. " } catch (const Exception &e) {\n"
  9059. " }\n"
  9060. "}\n",
  9061. getLLVMStyle());
  9062. }
  9063. TEST_F(FormatTest, UnderstandsPragmas) {
  9064. verifyFormat("#pragma omp reduction(| : var)");
  9065. verifyFormat("#pragma omp reduction(+ : var)");
  9066. EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
  9067. "(including parentheses).",
  9068. format("#pragma mark Any non-hyphenated or hyphenated string "
  9069. "(including parentheses)."));
  9070. }
  9071. TEST_F(FormatTest, UnderstandPragmaOption) {
  9072. verifyFormat("#pragma option -C -A");
  9073. EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A"));
  9074. }
  9075. #define EXPECT_ALL_STYLES_EQUAL(Styles) \
  9076. for (size_t i = 1; i < Styles.size(); ++i) \
  9077. EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
  9078. << " differs from Style #0"
  9079. TEST_F(FormatTest, GetsPredefinedStyleByName) {
  9080. SmallVector<FormatStyle, 3> Styles;
  9081. Styles.resize(3);
  9082. Styles[0] = getLLVMStyle();
  9083. EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
  9084. EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
  9085. EXPECT_ALL_STYLES_EQUAL(Styles);
  9086. Styles[0] = getGoogleStyle();
  9087. EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
  9088. EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
  9089. EXPECT_ALL_STYLES_EQUAL(Styles);
  9090. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  9091. EXPECT_TRUE(
  9092. getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
  9093. EXPECT_TRUE(
  9094. getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
  9095. EXPECT_ALL_STYLES_EQUAL(Styles);
  9096. Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
  9097. EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
  9098. EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
  9099. EXPECT_ALL_STYLES_EQUAL(Styles);
  9100. Styles[0] = getMozillaStyle();
  9101. EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
  9102. EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
  9103. EXPECT_ALL_STYLES_EQUAL(Styles);
  9104. Styles[0] = getWebKitStyle();
  9105. EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
  9106. EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
  9107. EXPECT_ALL_STYLES_EQUAL(Styles);
  9108. Styles[0] = getGNUStyle();
  9109. EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
  9110. EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
  9111. EXPECT_ALL_STYLES_EQUAL(Styles);
  9112. EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
  9113. }
  9114. TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
  9115. SmallVector<FormatStyle, 8> Styles;
  9116. Styles.resize(2);
  9117. Styles[0] = getGoogleStyle();
  9118. Styles[1] = getLLVMStyle();
  9119. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  9120. EXPECT_ALL_STYLES_EQUAL(Styles);
  9121. Styles.resize(5);
  9122. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  9123. Styles[1] = getLLVMStyle();
  9124. Styles[1].Language = FormatStyle::LK_JavaScript;
  9125. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  9126. Styles[2] = getLLVMStyle();
  9127. Styles[2].Language = FormatStyle::LK_JavaScript;
  9128. EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
  9129. "BasedOnStyle: Google",
  9130. &Styles[2])
  9131. .value());
  9132. Styles[3] = getLLVMStyle();
  9133. Styles[3].Language = FormatStyle::LK_JavaScript;
  9134. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
  9135. "Language: JavaScript",
  9136. &Styles[3])
  9137. .value());
  9138. Styles[4] = getLLVMStyle();
  9139. Styles[4].Language = FormatStyle::LK_JavaScript;
  9140. EXPECT_EQ(0, parseConfiguration("---\n"
  9141. "BasedOnStyle: LLVM\n"
  9142. "IndentWidth: 123\n"
  9143. "---\n"
  9144. "BasedOnStyle: Google\n"
  9145. "Language: JavaScript",
  9146. &Styles[4])
  9147. .value());
  9148. EXPECT_ALL_STYLES_EQUAL(Styles);
  9149. }
  9150. #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \
  9151. Style.FIELD = false; \
  9152. EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \
  9153. EXPECT_TRUE(Style.FIELD); \
  9154. EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \
  9155. EXPECT_FALSE(Style.FIELD);
  9156. #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
  9157. #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \
  9158. Style.STRUCT.FIELD = false; \
  9159. EXPECT_EQ(0, \
  9160. parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \
  9161. .value()); \
  9162. EXPECT_TRUE(Style.STRUCT.FIELD); \
  9163. EXPECT_EQ(0, \
  9164. parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \
  9165. .value()); \
  9166. EXPECT_FALSE(Style.STRUCT.FIELD);
  9167. #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \
  9168. CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
  9169. #define CHECK_PARSE(TEXT, FIELD, VALUE) \
  9170. EXPECT_NE(VALUE, Style.FIELD); \
  9171. EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \
  9172. EXPECT_EQ(VALUE, Style.FIELD)
  9173. TEST_F(FormatTest, ParsesConfigurationBools) {
  9174. FormatStyle Style = {};
  9175. Style.Language = FormatStyle::LK_Cpp;
  9176. CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
  9177. CHECK_PARSE_BOOL(AlignOperands);
  9178. CHECK_PARSE_BOOL(AlignTrailingComments);
  9179. CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
  9180. CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
  9181. CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
  9182. CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
  9183. CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
  9184. CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
  9185. CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
  9186. CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
  9187. CHECK_PARSE_BOOL(BinPackArguments);
  9188. CHECK_PARSE_BOOL(BinPackParameters);
  9189. CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
  9190. CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
  9191. CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
  9192. CHECK_PARSE_BOOL(BreakStringLiterals);
  9193. CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
  9194. CHECK_PARSE_BOOL(DerivePointerAlignment);
  9195. CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
  9196. CHECK_PARSE_BOOL(DisableFormat);
  9197. CHECK_PARSE_BOOL(IndentCaseLabels);
  9198. CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
  9199. CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
  9200. CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
  9201. CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
  9202. CHECK_PARSE_BOOL(Cpp11BracedListStyle);
  9203. CHECK_PARSE_BOOL(ReflowComments);
  9204. CHECK_PARSE_BOOL(SortIncludes);
  9205. CHECK_PARSE_BOOL(SpacesInParentheses);
  9206. CHECK_PARSE_BOOL(SpacesInSquareBrackets);
  9207. CHECK_PARSE_BOOL(SpacesInAngles);
  9208. CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
  9209. CHECK_PARSE_BOOL(SpacesInContainerLiterals);
  9210. CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
  9211. CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
  9212. CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
  9213. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
  9214. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
  9215. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
  9216. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
  9217. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
  9218. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
  9219. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
  9220. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
  9221. CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
  9222. CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
  9223. CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
  9224. }
  9225. #undef CHECK_PARSE_BOOL
  9226. TEST_F(FormatTest, ParsesConfiguration) {
  9227. FormatStyle Style = {};
  9228. Style.Language = FormatStyle::LK_Cpp;
  9229. CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
  9230. CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
  9231. ConstructorInitializerIndentWidth, 1234u);
  9232. CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
  9233. CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
  9234. CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
  9235. CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
  9236. PenaltyBreakBeforeFirstCallParameter, 1234u);
  9237. CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
  9238. CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
  9239. PenaltyReturnTypeOnItsOwnLine, 1234u);
  9240. CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
  9241. SpacesBeforeTrailingComments, 1234u);
  9242. CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
  9243. CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
  9244. CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
  9245. Style.PointerAlignment = FormatStyle::PAS_Middle;
  9246. CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
  9247. FormatStyle::PAS_Left);
  9248. CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
  9249. FormatStyle::PAS_Right);
  9250. CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
  9251. FormatStyle::PAS_Middle);
  9252. // For backward compatibility:
  9253. CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
  9254. FormatStyle::PAS_Left);
  9255. CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
  9256. FormatStyle::PAS_Right);
  9257. CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
  9258. FormatStyle::PAS_Middle);
  9259. Style.Standard = FormatStyle::LS_Auto;
  9260. CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
  9261. CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
  9262. CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
  9263. CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
  9264. CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
  9265. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  9266. CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
  9267. BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
  9268. CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
  9269. FormatStyle::BOS_None);
  9270. CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
  9271. FormatStyle::BOS_All);
  9272. // For backward compatibility:
  9273. CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
  9274. FormatStyle::BOS_None);
  9275. CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
  9276. FormatStyle::BOS_All);
  9277. Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
  9278. CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
  9279. FormatStyle::BAS_Align);
  9280. CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
  9281. FormatStyle::BAS_DontAlign);
  9282. CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
  9283. FormatStyle::BAS_AlwaysBreak);
  9284. // For backward compatibility:
  9285. CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
  9286. FormatStyle::BAS_DontAlign);
  9287. CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
  9288. FormatStyle::BAS_Align);
  9289. Style.UseTab = FormatStyle::UT_ForIndentation;
  9290. CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
  9291. CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
  9292. CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
  9293. // For backward compatibility:
  9294. CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
  9295. CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
  9296. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  9297. CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
  9298. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
  9299. CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
  9300. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
  9301. CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
  9302. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
  9303. CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
  9304. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
  9305. // For backward compatibility:
  9306. CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
  9307. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
  9308. CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
  9309. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
  9310. Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
  9311. CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
  9312. FormatStyle::SBPO_Never);
  9313. CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
  9314. FormatStyle::SBPO_Always);
  9315. CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
  9316. FormatStyle::SBPO_ControlStatements);
  9317. // For backward compatibility:
  9318. CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
  9319. FormatStyle::SBPO_Never);
  9320. CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
  9321. FormatStyle::SBPO_ControlStatements);
  9322. Style.ColumnLimit = 123;
  9323. FormatStyle BaseStyle = getLLVMStyle();
  9324. CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
  9325. CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
  9326. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  9327. CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
  9328. FormatStyle::BS_Attach);
  9329. CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
  9330. FormatStyle::BS_Linux);
  9331. CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
  9332. FormatStyle::BS_Mozilla);
  9333. CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
  9334. FormatStyle::BS_Stroustrup);
  9335. CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
  9336. FormatStyle::BS_Allman);
  9337. CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
  9338. CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
  9339. FormatStyle::BS_WebKit);
  9340. CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
  9341. FormatStyle::BS_Custom);
  9342. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
  9343. CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
  9344. FormatStyle::RTBS_None);
  9345. CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
  9346. FormatStyle::RTBS_All);
  9347. CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
  9348. AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
  9349. CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
  9350. AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
  9351. CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
  9352. AlwaysBreakAfterReturnType,
  9353. FormatStyle::RTBS_TopLevelDefinitions);
  9354. Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
  9355. CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
  9356. AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
  9357. CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
  9358. AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
  9359. CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
  9360. AlwaysBreakAfterDefinitionReturnType,
  9361. FormatStyle::DRTBS_TopLevel);
  9362. Style.NamespaceIndentation = FormatStyle::NI_All;
  9363. CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
  9364. FormatStyle::NI_None);
  9365. CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
  9366. FormatStyle::NI_Inner);
  9367. CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
  9368. FormatStyle::NI_All);
  9369. // FIXME: This is required because parsing a configuration simply overwrites
  9370. // the first N elements of the list instead of resetting it.
  9371. Style.ForEachMacros.clear();
  9372. std::vector<std::string> BoostForeach;
  9373. BoostForeach.push_back("BOOST_FOREACH");
  9374. CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
  9375. std::vector<std::string> BoostAndQForeach;
  9376. BoostAndQForeach.push_back("BOOST_FOREACH");
  9377. BoostAndQForeach.push_back("Q_FOREACH");
  9378. CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
  9379. BoostAndQForeach);
  9380. Style.IncludeCategories.clear();
  9381. std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2},
  9382. {".*", 1}};
  9383. CHECK_PARSE("IncludeCategories:\n"
  9384. " - Regex: abc/.*\n"
  9385. " Priority: 2\n"
  9386. " - Regex: .*\n"
  9387. " Priority: 1",
  9388. IncludeCategories, ExpectedCategories);
  9389. CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$");
  9390. }
  9391. TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
  9392. FormatStyle Style = {};
  9393. Style.Language = FormatStyle::LK_Cpp;
  9394. CHECK_PARSE("Language: Cpp\n"
  9395. "IndentWidth: 12",
  9396. IndentWidth, 12u);
  9397. EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
  9398. "IndentWidth: 34",
  9399. &Style),
  9400. ParseError::Unsuitable);
  9401. EXPECT_EQ(12u, Style.IndentWidth);
  9402. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  9403. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  9404. Style.Language = FormatStyle::LK_JavaScript;
  9405. CHECK_PARSE("Language: JavaScript\n"
  9406. "IndentWidth: 12",
  9407. IndentWidth, 12u);
  9408. CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
  9409. EXPECT_EQ(parseConfiguration("Language: Cpp\n"
  9410. "IndentWidth: 34",
  9411. &Style),
  9412. ParseError::Unsuitable);
  9413. EXPECT_EQ(23u, Style.IndentWidth);
  9414. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  9415. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  9416. CHECK_PARSE("BasedOnStyle: LLVM\n"
  9417. "IndentWidth: 67",
  9418. IndentWidth, 67u);
  9419. CHECK_PARSE("---\n"
  9420. "Language: JavaScript\n"
  9421. "IndentWidth: 12\n"
  9422. "---\n"
  9423. "Language: Cpp\n"
  9424. "IndentWidth: 34\n"
  9425. "...\n",
  9426. IndentWidth, 12u);
  9427. Style.Language = FormatStyle::LK_Cpp;
  9428. CHECK_PARSE("---\n"
  9429. "Language: JavaScript\n"
  9430. "IndentWidth: 12\n"
  9431. "---\n"
  9432. "Language: Cpp\n"
  9433. "IndentWidth: 34\n"
  9434. "...\n",
  9435. IndentWidth, 34u);
  9436. CHECK_PARSE("---\n"
  9437. "IndentWidth: 78\n"
  9438. "---\n"
  9439. "Language: JavaScript\n"
  9440. "IndentWidth: 56\n"
  9441. "...\n",
  9442. IndentWidth, 78u);
  9443. Style.ColumnLimit = 123;
  9444. Style.IndentWidth = 234;
  9445. Style.BreakBeforeBraces = FormatStyle::BS_Linux;
  9446. Style.TabWidth = 345;
  9447. EXPECT_FALSE(parseConfiguration("---\n"
  9448. "IndentWidth: 456\n"
  9449. "BreakBeforeBraces: Allman\n"
  9450. "---\n"
  9451. "Language: JavaScript\n"
  9452. "IndentWidth: 111\n"
  9453. "TabWidth: 111\n"
  9454. "---\n"
  9455. "Language: Cpp\n"
  9456. "BreakBeforeBraces: Stroustrup\n"
  9457. "TabWidth: 789\n"
  9458. "...\n",
  9459. &Style));
  9460. EXPECT_EQ(123u, Style.ColumnLimit);
  9461. EXPECT_EQ(456u, Style.IndentWidth);
  9462. EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
  9463. EXPECT_EQ(789u, Style.TabWidth);
  9464. EXPECT_EQ(parseConfiguration("---\n"
  9465. "Language: JavaScript\n"
  9466. "IndentWidth: 56\n"
  9467. "---\n"
  9468. "IndentWidth: 78\n"
  9469. "...\n",
  9470. &Style),
  9471. ParseError::Error);
  9472. EXPECT_EQ(parseConfiguration("---\n"
  9473. "Language: JavaScript\n"
  9474. "IndentWidth: 56\n"
  9475. "---\n"
  9476. "Language: JavaScript\n"
  9477. "IndentWidth: 78\n"
  9478. "...\n",
  9479. &Style),
  9480. ParseError::Error);
  9481. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  9482. }
  9483. #undef CHECK_PARSE
  9484. TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
  9485. FormatStyle Style = {};
  9486. Style.Language = FormatStyle::LK_JavaScript;
  9487. Style.BreakBeforeTernaryOperators = true;
  9488. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
  9489. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  9490. Style.BreakBeforeTernaryOperators = true;
  9491. EXPECT_EQ(0, parseConfiguration("---\n"
  9492. "BasedOnStyle: Google\n"
  9493. "---\n"
  9494. "Language: JavaScript\n"
  9495. "IndentWidth: 76\n"
  9496. "...\n",
  9497. &Style)
  9498. .value());
  9499. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  9500. EXPECT_EQ(76u, Style.IndentWidth);
  9501. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  9502. }
  9503. TEST_F(FormatTest, ConfigurationRoundTripTest) {
  9504. FormatStyle Style = getLLVMStyle();
  9505. std::string YAML = configurationAsText(Style);
  9506. FormatStyle ParsedStyle = {};
  9507. ParsedStyle.Language = FormatStyle::LK_Cpp;
  9508. EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
  9509. EXPECT_EQ(Style, ParsedStyle);
  9510. }
  9511. TEST_F(FormatTest, WorksFor8bitEncodings) {
  9512. EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
  9513. "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
  9514. "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
  9515. "\"\xef\xee\xf0\xf3...\"",
  9516. format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
  9517. "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
  9518. "\xef\xee\xf0\xf3...\"",
  9519. getLLVMStyleWithColumns(12)));
  9520. }
  9521. TEST_F(FormatTest, HandlesUTF8BOM) {
  9522. EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
  9523. EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
  9524. format("\xef\xbb\xbf#include <iostream>"));
  9525. EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
  9526. format("\xef\xbb\xbf\n#include <iostream>"));
  9527. }
  9528. // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
  9529. #if !defined(_MSC_VER)
  9530. TEST_F(FormatTest, CountsUTF8CharactersProperly) {
  9531. verifyFormat("\"Однажды в студёную зимнюю пору...\"",
  9532. getLLVMStyleWithColumns(35));
  9533. verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
  9534. getLLVMStyleWithColumns(31));
  9535. verifyFormat("// Однажды в студёную зимнюю пору...",
  9536. getLLVMStyleWithColumns(36));
  9537. verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
  9538. verifyFormat("/* Однажды в студёную зимнюю пору... */",
  9539. getLLVMStyleWithColumns(39));
  9540. verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
  9541. getLLVMStyleWithColumns(35));
  9542. }
  9543. TEST_F(FormatTest, SplitsUTF8Strings) {
  9544. // Non-printable characters' width is currently considered to be the length in
  9545. // bytes in UTF8. The characters can be displayed in very different manner
  9546. // (zero-width, single width with a substitution glyph, expanded to their code
  9547. // (e.g. "<8d>"), so there's no single correct way to handle them.
  9548. EXPECT_EQ("\"aaaaÄ\"\n"
  9549. "\"\xc2\x8d\";",
  9550. format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  9551. EXPECT_EQ("\"aaaaaaaÄ\"\n"
  9552. "\"\xc2\x8d\";",
  9553. format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  9554. EXPECT_EQ("\"Однажды, в \"\n"
  9555. "\"студёную \"\n"
  9556. "\"зимнюю \"\n"
  9557. "\"пору,\"",
  9558. format("\"Однажды, в студёную зимнюю пору,\"",
  9559. getLLVMStyleWithColumns(13)));
  9560. EXPECT_EQ(
  9561. "\"一 二 三 \"\n"
  9562. "\"四 五六 \"\n"
  9563. "\"七 八 九 \"\n"
  9564. "\"十\"",
  9565. format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
  9566. EXPECT_EQ("\"一\t二 \"\n"
  9567. "\"\t三 \"\n"
  9568. "\"四 五\t六 \"\n"
  9569. "\"\t七 \"\n"
  9570. "\"八九十\tqq\"",
  9571. format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
  9572. getLLVMStyleWithColumns(11)));
  9573. // UTF8 character in an escape sequence.
  9574. EXPECT_EQ("\"aaaaaa\"\n"
  9575. "\"\\\xC2\x8D\"",
  9576. format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
  9577. }
  9578. TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
  9579. EXPECT_EQ("const char *sssss =\n"
  9580. " \"一二三四五六七八\\\n"
  9581. " 九 十\";",
  9582. format("const char *sssss = \"一二三四五六七八\\\n"
  9583. " 九 十\";",
  9584. getLLVMStyleWithColumns(30)));
  9585. }
  9586. TEST_F(FormatTest, SplitsUTF8LineComments) {
  9587. EXPECT_EQ("// aaaaÄ\xc2\x8d",
  9588. format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
  9589. EXPECT_EQ("// Я из лесу\n"
  9590. "// вышел; был\n"
  9591. "// сильный\n"
  9592. "// мороз.",
  9593. format("// Я из лесу вышел; был сильный мороз.",
  9594. getLLVMStyleWithColumns(13)));
  9595. EXPECT_EQ("// 一二三\n"
  9596. "// 四五六七\n"
  9597. "// 八 九\n"
  9598. "// 十",
  9599. format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9)));
  9600. }
  9601. TEST_F(FormatTest, SplitsUTF8BlockComments) {
  9602. EXPECT_EQ("/* Гляжу,\n"
  9603. " * поднимается\n"
  9604. " * медленно в\n"
  9605. " * гору\n"
  9606. " * Лошадка,\n"
  9607. " * везущая\n"
  9608. " * хворосту\n"
  9609. " * воз. */",
  9610. format("/* Гляжу, поднимается медленно в гору\n"
  9611. " * Лошадка, везущая хворосту воз. */",
  9612. getLLVMStyleWithColumns(13)));
  9613. EXPECT_EQ(
  9614. "/* 一二三\n"
  9615. " * 四五六七\n"
  9616. " * 八 九\n"
  9617. " * 十 */",
  9618. format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9)));
  9619. EXPECT_EQ("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
  9620. " * 𝕓𝕪𝕥𝕖\n"
  9621. " * 𝖀𝕿𝕱-𝟠 */",
  9622. format("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12)));
  9623. }
  9624. #endif // _MSC_VER
  9625. TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
  9626. FormatStyle Style = getLLVMStyle();
  9627. Style.ConstructorInitializerIndentWidth = 4;
  9628. verifyFormat(
  9629. "SomeClass::Constructor()\n"
  9630. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  9631. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  9632. Style);
  9633. Style.ConstructorInitializerIndentWidth = 2;
  9634. verifyFormat(
  9635. "SomeClass::Constructor()\n"
  9636. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  9637. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  9638. Style);
  9639. Style.ConstructorInitializerIndentWidth = 0;
  9640. verifyFormat(
  9641. "SomeClass::Constructor()\n"
  9642. ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  9643. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  9644. Style);
  9645. Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
  9646. verifyFormat(
  9647. "SomeLongTemplateVariableName<\n"
  9648. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
  9649. Style);
  9650. verifyFormat(
  9651. "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  9652. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  9653. Style);
  9654. }
  9655. TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
  9656. FormatStyle Style = getLLVMStyle();
  9657. Style.BreakConstructorInitializersBeforeComma = true;
  9658. Style.ConstructorInitializerIndentWidth = 4;
  9659. verifyFormat("SomeClass::Constructor()\n"
  9660. " : a(a)\n"
  9661. " , b(b)\n"
  9662. " , c(c) {}",
  9663. Style);
  9664. verifyFormat("SomeClass::Constructor()\n"
  9665. " : a(a) {}",
  9666. Style);
  9667. Style.ColumnLimit = 0;
  9668. verifyFormat("SomeClass::Constructor()\n"
  9669. " : a(a) {}",
  9670. Style);
  9671. verifyFormat("SomeClass::Constructor() noexcept\n"
  9672. " : a(a) {}",
  9673. Style);
  9674. verifyFormat("SomeClass::Constructor()\n"
  9675. " : a(a)\n"
  9676. " , b(b)\n"
  9677. " , c(c) {}",
  9678. Style);
  9679. verifyFormat("SomeClass::Constructor()\n"
  9680. " : a(a) {\n"
  9681. " foo();\n"
  9682. " bar();\n"
  9683. "}",
  9684. Style);
  9685. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  9686. verifyFormat("SomeClass::Constructor()\n"
  9687. " : a(a)\n"
  9688. " , b(b)\n"
  9689. " , c(c) {\n}",
  9690. Style);
  9691. verifyFormat("SomeClass::Constructor()\n"
  9692. " : a(a) {\n}",
  9693. Style);
  9694. Style.ColumnLimit = 80;
  9695. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
  9696. Style.ConstructorInitializerIndentWidth = 2;
  9697. verifyFormat("SomeClass::Constructor()\n"
  9698. " : a(a)\n"
  9699. " , b(b)\n"
  9700. " , c(c) {}",
  9701. Style);
  9702. Style.ConstructorInitializerIndentWidth = 0;
  9703. verifyFormat("SomeClass::Constructor()\n"
  9704. ": a(a)\n"
  9705. ", b(b)\n"
  9706. ", c(c) {}",
  9707. Style);
  9708. Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  9709. Style.ConstructorInitializerIndentWidth = 4;
  9710. verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
  9711. verifyFormat(
  9712. "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
  9713. Style);
  9714. verifyFormat(
  9715. "SomeClass::Constructor()\n"
  9716. " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
  9717. Style);
  9718. Style.ConstructorInitializerIndentWidth = 4;
  9719. Style.ColumnLimit = 60;
  9720. verifyFormat("SomeClass::Constructor()\n"
  9721. " : aaaaaaaa(aaaaaaaa)\n"
  9722. " , aaaaaaaa(aaaaaaaa)\n"
  9723. " , aaaaaaaa(aaaaaaaa) {}",
  9724. Style);
  9725. }
  9726. TEST_F(FormatTest, Destructors) {
  9727. verifyFormat("void F(int &i) { i.~int(); }");
  9728. verifyFormat("void F(int &i) { i->~int(); }");
  9729. }
  9730. TEST_F(FormatTest, FormatsWithWebKitStyle) {
  9731. FormatStyle Style = getWebKitStyle();
  9732. // Don't indent in outer namespaces.
  9733. verifyFormat("namespace outer {\n"
  9734. "int i;\n"
  9735. "namespace inner {\n"
  9736. " int i;\n"
  9737. "} // namespace inner\n"
  9738. "} // namespace outer\n"
  9739. "namespace other_outer {\n"
  9740. "int i;\n"
  9741. "}",
  9742. Style);
  9743. // Don't indent case labels.
  9744. verifyFormat("switch (variable) {\n"
  9745. "case 1:\n"
  9746. "case 2:\n"
  9747. " doSomething();\n"
  9748. " break;\n"
  9749. "default:\n"
  9750. " ++variable;\n"
  9751. "}",
  9752. Style);
  9753. // Wrap before binary operators.
  9754. EXPECT_EQ("void f()\n"
  9755. "{\n"
  9756. " if (aaaaaaaaaaaaaaaa\n"
  9757. " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
  9758. " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  9759. " return;\n"
  9760. "}",
  9761. format("void f() {\n"
  9762. "if (aaaaaaaaaaaaaaaa\n"
  9763. "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
  9764. "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  9765. "return;\n"
  9766. "}",
  9767. Style));
  9768. // Allow functions on a single line.
  9769. verifyFormat("void f() { return; }", Style);
  9770. // Constructor initializers are formatted one per line with the "," on the
  9771. // new line.
  9772. verifyFormat("Constructor()\n"
  9773. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  9774. " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
  9775. " aaaaaaaaaaaaaa)\n"
  9776. " , aaaaaaaaaaaaaaaaaaaaaaa()\n"
  9777. "{\n"
  9778. "}",
  9779. Style);
  9780. verifyFormat("SomeClass::Constructor()\n"
  9781. " : a(a)\n"
  9782. "{\n"
  9783. "}",
  9784. Style);
  9785. EXPECT_EQ("SomeClass::Constructor()\n"
  9786. " : a(a)\n"
  9787. "{\n"
  9788. "}",
  9789. format("SomeClass::Constructor():a(a){}", Style));
  9790. verifyFormat("SomeClass::Constructor()\n"
  9791. " : a(a)\n"
  9792. " , b(b)\n"
  9793. " , c(c)\n"
  9794. "{\n"
  9795. "}",
  9796. Style);
  9797. verifyFormat("SomeClass::Constructor()\n"
  9798. " : a(a)\n"
  9799. "{\n"
  9800. " foo();\n"
  9801. " bar();\n"
  9802. "}",
  9803. Style);
  9804. // Access specifiers should be aligned left.
  9805. verifyFormat("class C {\n"
  9806. "public:\n"
  9807. " int i;\n"
  9808. "};",
  9809. Style);
  9810. // Do not align comments.
  9811. verifyFormat("int a; // Do not\n"
  9812. "double b; // align comments.",
  9813. Style);
  9814. // Do not align operands.
  9815. EXPECT_EQ("ASSERT(aaaa\n"
  9816. " || bbbb);",
  9817. format("ASSERT ( aaaa\n||bbbb);", Style));
  9818. // Accept input's line breaks.
  9819. EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
  9820. " || bbbbbbbbbbbbbbb) {\n"
  9821. " i++;\n"
  9822. "}",
  9823. format("if (aaaaaaaaaaaaaaa\n"
  9824. "|| bbbbbbbbbbbbbbb) { i++; }",
  9825. Style));
  9826. EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
  9827. " i++;\n"
  9828. "}",
  9829. format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
  9830. // Don't automatically break all macro definitions (llvm.org/PR17842).
  9831. verifyFormat("#define aNumber 10", Style);
  9832. // However, generally keep the line breaks that the user authored.
  9833. EXPECT_EQ("#define aNumber \\\n"
  9834. " 10",
  9835. format("#define aNumber \\\n"
  9836. " 10",
  9837. Style));
  9838. // Keep empty and one-element array literals on a single line.
  9839. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
  9840. " copyItems:YES];",
  9841. format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
  9842. "copyItems:YES];",
  9843. Style));
  9844. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
  9845. " copyItems:YES];",
  9846. format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
  9847. " copyItems:YES];",
  9848. Style));
  9849. // FIXME: This does not seem right, there should be more indentation before
  9850. // the array literal's entries. Nested blocks have the same problem.
  9851. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
  9852. " @\"a\",\n"
  9853. " @\"a\"\n"
  9854. "]\n"
  9855. " copyItems:YES];",
  9856. format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
  9857. " @\"a\",\n"
  9858. " @\"a\"\n"
  9859. " ]\n"
  9860. " copyItems:YES];",
  9861. Style));
  9862. EXPECT_EQ(
  9863. "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
  9864. " copyItems:YES];",
  9865. format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
  9866. " copyItems:YES];",
  9867. Style));
  9868. verifyFormat("[self.a b:c c:d];", Style);
  9869. EXPECT_EQ("[self.a b:c\n"
  9870. " c:d];",
  9871. format("[self.a b:c\n"
  9872. "c:d];",
  9873. Style));
  9874. }
  9875. TEST_F(FormatTest, FormatsLambdas) {
  9876. verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
  9877. verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
  9878. verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
  9879. verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
  9880. verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
  9881. verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
  9882. verifyFormat("void f() {\n"
  9883. " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
  9884. "}\n");
  9885. verifyFormat("void f() {\n"
  9886. " other(x.begin(), //\n"
  9887. " x.end(), //\n"
  9888. " [&](int, int) { return 1; });\n"
  9889. "}\n");
  9890. verifyFormat("SomeFunction([]() { // A cool function...\n"
  9891. " return 43;\n"
  9892. "});");
  9893. EXPECT_EQ("SomeFunction([]() {\n"
  9894. "#define A a\n"
  9895. " return 43;\n"
  9896. "});",
  9897. format("SomeFunction([](){\n"
  9898. "#define A a\n"
  9899. "return 43;\n"
  9900. "});"));
  9901. verifyFormat("void f() {\n"
  9902. " SomeFunction([](decltype(x), A *a) {});\n"
  9903. "}");
  9904. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  9905. " [](const aaaaaaaaaa &a) { return a; });");
  9906. verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
  9907. " SomeOtherFunctioooooooooooooooooooooooooon();\n"
  9908. "});");
  9909. verifyFormat("Constructor()\n"
  9910. " : Field([] { // comment\n"
  9911. " int i;\n"
  9912. " }) {}");
  9913. verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
  9914. " return some_parameter.size();\n"
  9915. "};");
  9916. verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
  9917. " [](const string &s) { return s; };");
  9918. verifyFormat("int i = aaaaaa ? 1 //\n"
  9919. " : [] {\n"
  9920. " return 2; //\n"
  9921. " }();");
  9922. verifyFormat("llvm::errs() << \"number of twos is \"\n"
  9923. " << std::count_if(v.begin(), v.end(), [](int x) {\n"
  9924. " return x == 2; // force break\n"
  9925. " });");
  9926. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n"
  9927. " int iiiiiiiiiiii) {\n"
  9928. " return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n"
  9929. "});",
  9930. getLLVMStyleWithColumns(60));
  9931. verifyFormat("SomeFunction({[&] {\n"
  9932. " // comment\n"
  9933. " },\n"
  9934. " [&] {\n"
  9935. " // comment\n"
  9936. " }});");
  9937. verifyFormat("SomeFunction({[&] {\n"
  9938. " // comment\n"
  9939. "}});");
  9940. verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n"
  9941. " [&]() { return true; },\n"
  9942. " aaaaa aaaaaaaaa);");
  9943. // Lambdas with return types.
  9944. verifyFormat("int c = []() -> int { return 2; }();\n");
  9945. verifyFormat("int c = []() -> int * { return 2; }();\n");
  9946. verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
  9947. verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
  9948. verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
  9949. verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
  9950. verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
  9951. verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
  9952. verifyFormat("[a, a]() -> a<1> {};");
  9953. verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
  9954. " int j) -> int {\n"
  9955. " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
  9956. "};");
  9957. verifyFormat(
  9958. "aaaaaaaaaaaaaaaaaaaaaa(\n"
  9959. " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
  9960. " return aaaaaaaaaaaaaaaaa;\n"
  9961. " });",
  9962. getLLVMStyleWithColumns(70));
  9963. // Multiple lambdas in the same parentheses change indentation rules.
  9964. verifyFormat("SomeFunction(\n"
  9965. " []() {\n"
  9966. " int i = 42;\n"
  9967. " return i;\n"
  9968. " },\n"
  9969. " []() {\n"
  9970. " int j = 43;\n"
  9971. " return j;\n"
  9972. " });");
  9973. // More complex introducers.
  9974. verifyFormat("return [i, args...] {};");
  9975. // Not lambdas.
  9976. verifyFormat("constexpr char hello[]{\"hello\"};");
  9977. verifyFormat("double &operator[](int i) { return 0; }\n"
  9978. "int i;");
  9979. verifyFormat("std::unique_ptr<int[]> foo() {}");
  9980. verifyFormat("int i = a[a][a]->f();");
  9981. verifyFormat("int i = (*b)[a]->f();");
  9982. // Other corner cases.
  9983. verifyFormat("void f() {\n"
  9984. " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
  9985. " );\n"
  9986. "}");
  9987. // Lambdas created through weird macros.
  9988. verifyFormat("void f() {\n"
  9989. " MACRO((const AA &a) { return 1; });\n"
  9990. " MACRO((AA &a) { return 1; });\n"
  9991. "}");
  9992. verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
  9993. " doo_dah();\n"
  9994. " doo_dah();\n"
  9995. " })) {\n"
  9996. "}");
  9997. verifyFormat("auto lambda = []() {\n"
  9998. " int a = 2\n"
  9999. "#if A\n"
  10000. " + 2\n"
  10001. "#endif\n"
  10002. " ;\n"
  10003. "};");
  10004. }
  10005. TEST_F(FormatTest, FormatsBlocks) {
  10006. FormatStyle ShortBlocks = getLLVMStyle();
  10007. ShortBlocks.AllowShortBlocksOnASingleLine = true;
  10008. verifyFormat("int (^Block)(int, int);", ShortBlocks);
  10009. verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
  10010. verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
  10011. verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
  10012. verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
  10013. verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
  10014. verifyFormat("foo(^{ bar(); });", ShortBlocks);
  10015. verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
  10016. verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
  10017. verifyFormat("[operation setCompletionBlock:^{\n"
  10018. " [self onOperationDone];\n"
  10019. "}];");
  10020. verifyFormat("int i = {[operation setCompletionBlock:^{\n"
  10021. " [self onOperationDone];\n"
  10022. "}]};");
  10023. verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
  10024. " f();\n"
  10025. "}];");
  10026. verifyFormat("int a = [operation block:^int(int *i) {\n"
  10027. " return 1;\n"
  10028. "}];");
  10029. verifyFormat("[myObject doSomethingWith:arg1\n"
  10030. " aaa:^int(int *a) {\n"
  10031. " return 1;\n"
  10032. " }\n"
  10033. " bbb:f(a * bbbbbbbb)];");
  10034. verifyFormat("[operation setCompletionBlock:^{\n"
  10035. " [self.delegate newDataAvailable];\n"
  10036. "}];",
  10037. getLLVMStyleWithColumns(60));
  10038. verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
  10039. " NSString *path = [self sessionFilePath];\n"
  10040. " if (path) {\n"
  10041. " // ...\n"
  10042. " }\n"
  10043. "});");
  10044. verifyFormat("[[SessionService sharedService]\n"
  10045. " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  10046. " if (window) {\n"
  10047. " [self windowDidLoad:window];\n"
  10048. " } else {\n"
  10049. " [self errorLoadingWindow];\n"
  10050. " }\n"
  10051. " }];");
  10052. verifyFormat("void (^largeBlock)(void) = ^{\n"
  10053. " // ...\n"
  10054. "};\n",
  10055. getLLVMStyleWithColumns(40));
  10056. verifyFormat("[[SessionService sharedService]\n"
  10057. " loadWindowWithCompletionBlock: //\n"
  10058. " ^(SessionWindow *window) {\n"
  10059. " if (window) {\n"
  10060. " [self windowDidLoad:window];\n"
  10061. " } else {\n"
  10062. " [self errorLoadingWindow];\n"
  10063. " }\n"
  10064. " }];",
  10065. getLLVMStyleWithColumns(60));
  10066. verifyFormat("[myObject doSomethingWith:arg1\n"
  10067. " firstBlock:^(Foo *a) {\n"
  10068. " // ...\n"
  10069. " int i;\n"
  10070. " }\n"
  10071. " secondBlock:^(Bar *b) {\n"
  10072. " // ...\n"
  10073. " int i;\n"
  10074. " }\n"
  10075. " thirdBlock:^Foo(Bar *b) {\n"
  10076. " // ...\n"
  10077. " int i;\n"
  10078. " }];");
  10079. verifyFormat("[myObject doSomethingWith:arg1\n"
  10080. " firstBlock:-1\n"
  10081. " secondBlock:^(Bar *b) {\n"
  10082. " // ...\n"
  10083. " int i;\n"
  10084. " }];");
  10085. verifyFormat("f(^{\n"
  10086. " @autoreleasepool {\n"
  10087. " if (a) {\n"
  10088. " g();\n"
  10089. " }\n"
  10090. " }\n"
  10091. "});");
  10092. verifyFormat("Block b = ^int *(A *a, B *b) {}");
  10093. FormatStyle FourIndent = getLLVMStyle();
  10094. FourIndent.ObjCBlockIndentWidth = 4;
  10095. verifyFormat("[operation setCompletionBlock:^{\n"
  10096. " [self onOperationDone];\n"
  10097. "}];",
  10098. FourIndent);
  10099. }
  10100. TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
  10101. FormatStyle ZeroColumn = getLLVMStyle();
  10102. ZeroColumn.ColumnLimit = 0;
  10103. verifyFormat("[[SessionService sharedService] "
  10104. "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  10105. " if (window) {\n"
  10106. " [self windowDidLoad:window];\n"
  10107. " } else {\n"
  10108. " [self errorLoadingWindow];\n"
  10109. " }\n"
  10110. "}];",
  10111. ZeroColumn);
  10112. EXPECT_EQ("[[SessionService sharedService]\n"
  10113. " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  10114. " if (window) {\n"
  10115. " [self windowDidLoad:window];\n"
  10116. " } else {\n"
  10117. " [self errorLoadingWindow];\n"
  10118. " }\n"
  10119. " }];",
  10120. format("[[SessionService sharedService]\n"
  10121. "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  10122. " if (window) {\n"
  10123. " [self windowDidLoad:window];\n"
  10124. " } else {\n"
  10125. " [self errorLoadingWindow];\n"
  10126. " }\n"
  10127. "}];",
  10128. ZeroColumn));
  10129. verifyFormat("[myObject doSomethingWith:arg1\n"
  10130. " firstBlock:^(Foo *a) {\n"
  10131. " // ...\n"
  10132. " int i;\n"
  10133. " }\n"
  10134. " secondBlock:^(Bar *b) {\n"
  10135. " // ...\n"
  10136. " int i;\n"
  10137. " }\n"
  10138. " thirdBlock:^Foo(Bar *b) {\n"
  10139. " // ...\n"
  10140. " int i;\n"
  10141. " }];",
  10142. ZeroColumn);
  10143. verifyFormat("f(^{\n"
  10144. " @autoreleasepool {\n"
  10145. " if (a) {\n"
  10146. " g();\n"
  10147. " }\n"
  10148. " }\n"
  10149. "});",
  10150. ZeroColumn);
  10151. verifyFormat("void (^largeBlock)(void) = ^{\n"
  10152. " // ...\n"
  10153. "};",
  10154. ZeroColumn);
  10155. ZeroColumn.AllowShortBlocksOnASingleLine = true;
  10156. EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
  10157. format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn));
  10158. ZeroColumn.AllowShortBlocksOnASingleLine = false;
  10159. EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
  10160. " int i;\n"
  10161. "};",
  10162. format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn));
  10163. }
  10164. TEST_F(FormatTest, SupportsCRLF) {
  10165. EXPECT_EQ("int a;\r\n"
  10166. "int b;\r\n"
  10167. "int c;\r\n",
  10168. format("int a;\r\n"
  10169. " int b;\r\n"
  10170. " int c;\r\n",
  10171. getLLVMStyle()));
  10172. EXPECT_EQ("int a;\r\n"
  10173. "int b;\r\n"
  10174. "int c;\r\n",
  10175. format("int a;\r\n"
  10176. " int b;\n"
  10177. " int c;\r\n",
  10178. getLLVMStyle()));
  10179. EXPECT_EQ("int a;\n"
  10180. "int b;\n"
  10181. "int c;\n",
  10182. format("int a;\r\n"
  10183. " int b;\n"
  10184. " int c;\n",
  10185. getLLVMStyle()));
  10186. EXPECT_EQ("\"aaaaaaa \"\r\n"
  10187. "\"bbbbbbb\";\r\n",
  10188. format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
  10189. EXPECT_EQ("#define A \\\r\n"
  10190. " b; \\\r\n"
  10191. " c; \\\r\n"
  10192. " d;\r\n",
  10193. format("#define A \\\r\n"
  10194. " b; \\\r\n"
  10195. " c; d; \r\n",
  10196. getGoogleStyle()));
  10197. EXPECT_EQ("/*\r\n"
  10198. "multi line block comments\r\n"
  10199. "should not introduce\r\n"
  10200. "an extra carriage return\r\n"
  10201. "*/\r\n",
  10202. format("/*\r\n"
  10203. "multi line block comments\r\n"
  10204. "should not introduce\r\n"
  10205. "an extra carriage return\r\n"
  10206. "*/\r\n"));
  10207. }
  10208. TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
  10209. verifyFormat("MY_CLASS(C) {\n"
  10210. " int i;\n"
  10211. " int j;\n"
  10212. "};");
  10213. }
  10214. TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
  10215. FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
  10216. TwoIndent.ContinuationIndentWidth = 2;
  10217. EXPECT_EQ("int i =\n"
  10218. " longFunction(\n"
  10219. " arg);",
  10220. format("int i = longFunction(arg);", TwoIndent));
  10221. FormatStyle SixIndent = getLLVMStyleWithColumns(20);
  10222. SixIndent.ContinuationIndentWidth = 6;
  10223. EXPECT_EQ("int i =\n"
  10224. " longFunction(\n"
  10225. " arg);",
  10226. format("int i = longFunction(arg);", SixIndent));
  10227. }
  10228. TEST_F(FormatTest, SpacesInAngles) {
  10229. FormatStyle Spaces = getLLVMStyle();
  10230. Spaces.SpacesInAngles = true;
  10231. verifyFormat("static_cast< int >(arg);", Spaces);
  10232. verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
  10233. verifyFormat("f< int, float >();", Spaces);
  10234. verifyFormat("template <> g() {}", Spaces);
  10235. verifyFormat("template < std::vector< int > > f() {}", Spaces);
  10236. verifyFormat("std::function< void(int, int) > fct;", Spaces);
  10237. verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
  10238. Spaces);
  10239. Spaces.Standard = FormatStyle::LS_Cpp03;
  10240. Spaces.SpacesInAngles = true;
  10241. verifyFormat("A< A< int > >();", Spaces);
  10242. Spaces.SpacesInAngles = false;
  10243. verifyFormat("A<A<int> >();", Spaces);
  10244. Spaces.Standard = FormatStyle::LS_Cpp11;
  10245. Spaces.SpacesInAngles = true;
  10246. verifyFormat("A< A< int > >();", Spaces);
  10247. Spaces.SpacesInAngles = false;
  10248. verifyFormat("A<A<int>>();", Spaces);
  10249. }
  10250. TEST_F(FormatTest, TripleAngleBrackets) {
  10251. verifyFormat("f<<<1, 1>>>();");
  10252. verifyFormat("f<<<1, 1, 1, s>>>();");
  10253. verifyFormat("f<<<a, b, c, d>>>();");
  10254. EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
  10255. verifyFormat("f<param><<<1, 1>>>();");
  10256. verifyFormat("f<1><<<1, 1>>>();");
  10257. EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
  10258. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  10259. "aaaaaaaaaaa<<<\n 1, 1>>>();");
  10260. }
  10261. TEST_F(FormatTest, MergeLessLessAtEnd) {
  10262. verifyFormat("<<");
  10263. EXPECT_EQ("< < <", format("\\\n<<<"));
  10264. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  10265. "aaallvm::outs() <<");
  10266. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  10267. "aaaallvm::outs()\n <<");
  10268. }
  10269. TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
  10270. std::string code = "#if A\n"
  10271. "#if B\n"
  10272. "a.\n"
  10273. "#endif\n"
  10274. " a = 1;\n"
  10275. "#else\n"
  10276. "#endif\n"
  10277. "#if C\n"
  10278. "#else\n"
  10279. "#endif\n";
  10280. EXPECT_EQ(code, format(code));
  10281. }
  10282. TEST_F(FormatTest, HandleConflictMarkers) {
  10283. // Git/SVN conflict markers.
  10284. EXPECT_EQ("int a;\n"
  10285. "void f() {\n"
  10286. " callme(some(parameter1,\n"
  10287. "<<<<<<< text by the vcs\n"
  10288. " parameter2),\n"
  10289. "||||||| text by the vcs\n"
  10290. " parameter2),\n"
  10291. " parameter3,\n"
  10292. "======= text by the vcs\n"
  10293. " parameter2, parameter3),\n"
  10294. ">>>>>>> text by the vcs\n"
  10295. " otherparameter);\n",
  10296. format("int a;\n"
  10297. "void f() {\n"
  10298. " callme(some(parameter1,\n"
  10299. "<<<<<<< text by the vcs\n"
  10300. " parameter2),\n"
  10301. "||||||| text by the vcs\n"
  10302. " parameter2),\n"
  10303. " parameter3,\n"
  10304. "======= text by the vcs\n"
  10305. " parameter2,\n"
  10306. " parameter3),\n"
  10307. ">>>>>>> text by the vcs\n"
  10308. " otherparameter);\n"));
  10309. // Perforce markers.
  10310. EXPECT_EQ("void f() {\n"
  10311. " function(\n"
  10312. ">>>> text by the vcs\n"
  10313. " parameter,\n"
  10314. "==== text by the vcs\n"
  10315. " parameter,\n"
  10316. "==== text by the vcs\n"
  10317. " parameter,\n"
  10318. "<<<< text by the vcs\n"
  10319. " parameter);\n",
  10320. format("void f() {\n"
  10321. " function(\n"
  10322. ">>>> text by the vcs\n"
  10323. " parameter,\n"
  10324. "==== text by the vcs\n"
  10325. " parameter,\n"
  10326. "==== text by the vcs\n"
  10327. " parameter,\n"
  10328. "<<<< text by the vcs\n"
  10329. " parameter);\n"));
  10330. EXPECT_EQ("<<<<<<<\n"
  10331. "|||||||\n"
  10332. "=======\n"
  10333. ">>>>>>>",
  10334. format("<<<<<<<\n"
  10335. "|||||||\n"
  10336. "=======\n"
  10337. ">>>>>>>"));
  10338. EXPECT_EQ("<<<<<<<\n"
  10339. "|||||||\n"
  10340. "int i;\n"
  10341. "=======\n"
  10342. ">>>>>>>",
  10343. format("<<<<<<<\n"
  10344. "|||||||\n"
  10345. "int i;\n"
  10346. "=======\n"
  10347. ">>>>>>>"));
  10348. // FIXME: Handle parsing of macros around conflict markers correctly:
  10349. EXPECT_EQ("#define Macro \\\n"
  10350. "<<<<<<<\n"
  10351. "Something \\\n"
  10352. "|||||||\n"
  10353. "Else \\\n"
  10354. "=======\n"
  10355. "Other \\\n"
  10356. ">>>>>>>\n"
  10357. " End int i;\n",
  10358. format("#define Macro \\\n"
  10359. "<<<<<<<\n"
  10360. " Something \\\n"
  10361. "|||||||\n"
  10362. " Else \\\n"
  10363. "=======\n"
  10364. " Other \\\n"
  10365. ">>>>>>>\n"
  10366. " End\n"
  10367. "int i;\n"));
  10368. }
  10369. TEST_F(FormatTest, DisableRegions) {
  10370. EXPECT_EQ("int i;\n"
  10371. "// clang-format off\n"
  10372. " int j;\n"
  10373. "// clang-format on\n"
  10374. "int k;",
  10375. format(" int i;\n"
  10376. " // clang-format off\n"
  10377. " int j;\n"
  10378. " // clang-format on\n"
  10379. " int k;"));
  10380. EXPECT_EQ("int i;\n"
  10381. "/* clang-format off */\n"
  10382. " int j;\n"
  10383. "/* clang-format on */\n"
  10384. "int k;",
  10385. format(" int i;\n"
  10386. " /* clang-format off */\n"
  10387. " int j;\n"
  10388. " /* clang-format on */\n"
  10389. " int k;"));
  10390. }
  10391. TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
  10392. format("? ) =");
  10393. verifyNoCrash("#define a\\\n /**/}");
  10394. }
  10395. TEST_F(FormatTest, FormatsTableGenCode) {
  10396. FormatStyle Style = getLLVMStyle();
  10397. Style.Language = FormatStyle::LK_TableGen;
  10398. verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
  10399. }
  10400. // Since this test case uses UNIX-style file path. We disable it for MS
  10401. // compiler.
  10402. #if !defined(_MSC_VER) && !defined(__MINGW32__)
  10403. TEST(FormatStyle, GetStyleOfFile) {
  10404. vfs::InMemoryFileSystem FS;
  10405. // Test 1: format file in the same directory.
  10406. ASSERT_TRUE(
  10407. FS.addFile("/a/.clang-format", 0,
  10408. llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
  10409. ASSERT_TRUE(
  10410. FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
  10411. auto Style1 = getStyle("file", "/a/.clang-format", "Google", &FS);
  10412. ASSERT_EQ(Style1, getLLVMStyle());
  10413. // Test 2: fallback to default.
  10414. ASSERT_TRUE(
  10415. FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
  10416. auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", &FS);
  10417. ASSERT_EQ(Style2, getMozillaStyle());
  10418. // Test 3: format file in parent directory.
  10419. ASSERT_TRUE(
  10420. FS.addFile("/c/.clang-format", 0,
  10421. llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
  10422. ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
  10423. llvm::MemoryBuffer::getMemBuffer("int i;")));
  10424. auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", &FS);
  10425. ASSERT_EQ(Style3, getGoogleStyle());
  10426. }
  10427. #endif // _MSC_VER
  10428. class ReplacementTest : public ::testing::Test {
  10429. protected:
  10430. tooling::Replacement createReplacement(SourceLocation Start, unsigned Length,
  10431. llvm::StringRef ReplacementText) {
  10432. return tooling::Replacement(Context.Sources, Start, Length,
  10433. ReplacementText);
  10434. }
  10435. RewriterTestContext Context;
  10436. };
  10437. TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
  10438. // Column limit is 20.
  10439. std::string Code = "Type *a =\n"
  10440. " new Type();\n"
  10441. "g(iiiii, 0, jjjjj,\n"
  10442. " 0, kkkkk, 0, mm);\n"
  10443. "int bad = format ;";
  10444. std::string Expected = "auto a = new Type();\n"
  10445. "g(iiiii, nullptr,\n"
  10446. " jjjjj, nullptr,\n"
  10447. " kkkkk, nullptr,\n"
  10448. " mm);\n"
  10449. "int bad = format ;";
  10450. FileID ID = Context.createInMemoryFile("format.cpp", Code);
  10451. tooling::Replacements Replaces;
  10452. Replaces.insert(tooling::Replacement(
  10453. Context.Sources, Context.getLocation(ID, 1, 1), 6, "auto "));
  10454. Replaces.insert(tooling::Replacement(
  10455. Context.Sources, Context.getLocation(ID, 3, 10), 1, "nullptr"));
  10456. Replaces.insert(tooling::Replacement(
  10457. Context.Sources, Context.getLocation(ID, 4, 3), 1, "nullptr"));
  10458. Replaces.insert(tooling::Replacement(
  10459. Context.Sources, Context.getLocation(ID, 4, 13), 1, "nullptr"));
  10460. format::FormatStyle Style = format::getLLVMStyle();
  10461. Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
  10462. EXPECT_EQ(Expected, applyAllReplacements(
  10463. Code, formatReplacements(Code, Replaces, Style)));
  10464. }
  10465. } // end namespace
  10466. } // end namespace format
  10467. } // end namespace clang