FormatTest.cpp 363 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822
  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 "FormatTestUtils.h"
  10. #include "clang/Format/Format.h"
  11. #include "llvm/Support/Debug.h"
  12. #include "gtest/gtest.h"
  13. #define DEBUG_TYPE "format-test"
  14. namespace clang {
  15. namespace format {
  16. FormatStyle getGoogleStyle() {
  17. return getGoogleStyle(FormatStyle::LK_Cpp);
  18. }
  19. class FormatTest : public ::testing::Test {
  20. protected:
  21. std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length,
  22. const FormatStyle &Style) {
  23. DEBUG(llvm::errs() << "---\n");
  24. DEBUG(llvm::errs() << Code << "\n\n");
  25. std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
  26. tooling::Replacements Replaces = reformat(Style, Code, Ranges);
  27. ReplacementCount = Replaces.size();
  28. std::string Result = applyAllReplacements(Code, Replaces);
  29. EXPECT_NE("", Result);
  30. DEBUG(llvm::errs() << "\n" << Result << "\n\n");
  31. return Result;
  32. }
  33. std::string
  34. format(llvm::StringRef Code, const FormatStyle &Style = getLLVMStyle()) {
  35. return format(Code, 0, Code.size(), Style);
  36. }
  37. FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
  38. FormatStyle Style = getLLVMStyle();
  39. Style.ColumnLimit = ColumnLimit;
  40. return Style;
  41. }
  42. FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
  43. FormatStyle Style = getGoogleStyle();
  44. Style.ColumnLimit = ColumnLimit;
  45. return Style;
  46. }
  47. void verifyFormat(llvm::StringRef Code,
  48. const FormatStyle &Style = getLLVMStyle()) {
  49. EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
  50. }
  51. void verifyGoogleFormat(llvm::StringRef Code) {
  52. verifyFormat(Code, getGoogleStyle());
  53. }
  54. void verifyIndependentOfContext(llvm::StringRef text) {
  55. verifyFormat(text);
  56. verifyFormat(llvm::Twine("void f() { " + text + " }").str());
  57. }
  58. /// \brief Verify that clang-format does not crash on the given input.
  59. void verifyNoCrash(llvm::StringRef Code,
  60. const FormatStyle &Style = getLLVMStyle()) {
  61. format(Code, Style);
  62. }
  63. int ReplacementCount;
  64. };
  65. TEST_F(FormatTest, MessUp) {
  66. EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
  67. EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
  68. EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
  69. EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
  70. EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
  71. }
  72. //===----------------------------------------------------------------------===//
  73. // Basic function tests.
  74. //===----------------------------------------------------------------------===//
  75. TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
  76. EXPECT_EQ(";", format(";"));
  77. }
  78. TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
  79. EXPECT_EQ("int i;", format(" int i;"));
  80. EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;"));
  81. EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
  82. EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
  83. }
  84. TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
  85. EXPECT_EQ("int i;", format("int\ni;"));
  86. }
  87. TEST_F(FormatTest, FormatsNestedBlockStatements) {
  88. EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
  89. }
  90. TEST_F(FormatTest, FormatsNestedCall) {
  91. verifyFormat("Method(f1, f2(f3));");
  92. verifyFormat("Method(f1(f2, f3()));");
  93. verifyFormat("Method(f1(f2, (f3())));");
  94. }
  95. TEST_F(FormatTest, NestedNameSpecifiers) {
  96. verifyFormat("vector<::Type> v;");
  97. verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
  98. verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
  99. verifyFormat("bool a = 2 < ::SomeFunction();");
  100. }
  101. TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
  102. EXPECT_EQ("if (a) {\n"
  103. " f();\n"
  104. "}",
  105. format("if(a){f();}"));
  106. EXPECT_EQ(4, ReplacementCount);
  107. EXPECT_EQ("if (a) {\n"
  108. " f();\n"
  109. "}",
  110. format("if (a) {\n"
  111. " f();\n"
  112. "}"));
  113. EXPECT_EQ(0, ReplacementCount);
  114. }
  115. TEST_F(FormatTest, RemovesTrailingWhitespaceOfFormattedLine) {
  116. EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0, getLLVMStyle()));
  117. EXPECT_EQ("int a;", format("int a; "));
  118. EXPECT_EQ("int a;\n", format("int a; \n \n \n "));
  119. EXPECT_EQ("int a;\nint b; ",
  120. format("int a; \nint b; ", 0, 0, getLLVMStyle()));
  121. }
  122. TEST_F(FormatTest, FormatsCorrectRegionForLeadingWhitespace) {
  123. EXPECT_EQ("int b;\nint a;",
  124. format("int b;\n int a;", 7, 0, getLLVMStyle()));
  125. EXPECT_EQ("int b;\n int a;",
  126. format("int b;\n int a;", 6, 0, getLLVMStyle()));
  127. EXPECT_EQ("#define A \\\n"
  128. " int a; \\\n"
  129. " int b;",
  130. format("#define A \\\n"
  131. " int a; \\\n"
  132. " int b;",
  133. 26, 0, getLLVMStyleWithColumns(12)));
  134. EXPECT_EQ("#define A \\\n"
  135. " int a; \\\n"
  136. " int b;",
  137. format("#define A \\\n"
  138. " int a; \\\n"
  139. " int b;",
  140. 25, 0, getLLVMStyleWithColumns(12)));
  141. }
  142. TEST_F(FormatTest, FormatLineWhenInvokedOnTrailingNewline) {
  143. EXPECT_EQ("int b;\n\nint a;",
  144. format("int b;\n\nint a;", 8, 0, getLLVMStyle()));
  145. EXPECT_EQ("int b;\n\nint a;",
  146. format("int b;\n\nint a;", 7, 0, getLLVMStyle()));
  147. // This might not strictly be correct, but is likely good in all practical
  148. // cases.
  149. EXPECT_EQ("int b;\nint a;",
  150. format("int b;int a;", 7, 0, getLLVMStyle()));
  151. }
  152. TEST_F(FormatTest, RemovesWhitespaceWhenTriggeredOnEmptyLine) {
  153. EXPECT_EQ("int a;\n\n int b;",
  154. format("int a;\n \n\n int b;", 8, 0, getLLVMStyle()));
  155. EXPECT_EQ("int a;\n\n int b;",
  156. format("int a;\n \n\n int b;", 9, 0, getLLVMStyle()));
  157. }
  158. TEST_F(FormatTest, RemovesEmptyLines) {
  159. EXPECT_EQ("class C {\n"
  160. " int i;\n"
  161. "};",
  162. format("class C {\n"
  163. " int i;\n"
  164. "\n"
  165. "};"));
  166. // Don't remove empty lines at the start of namespaces or extern "C" blocks.
  167. EXPECT_EQ("namespace N {\n"
  168. "\n"
  169. "int i;\n"
  170. "}",
  171. format("namespace N {\n"
  172. "\n"
  173. "int i;\n"
  174. "}",
  175. getGoogleStyle()));
  176. EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
  177. "\n"
  178. "int i;\n"
  179. "}",
  180. format("extern /**/ \"C\" /**/ {\n"
  181. "\n"
  182. "int i;\n"
  183. "}",
  184. getGoogleStyle()));
  185. // ...but do keep inlining and removing empty lines for non-block extern "C"
  186. // functions.
  187. verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
  188. EXPECT_EQ("extern \"C\" int f() {\n"
  189. " int i = 42;\n"
  190. " return i;\n"
  191. "}",
  192. format("extern \"C\" int f() {\n"
  193. "\n"
  194. " int i = 42;\n"
  195. " return i;\n"
  196. "}",
  197. getGoogleStyle()));
  198. // Remove empty lines at the beginning and end of blocks.
  199. EXPECT_EQ("void f() {\n"
  200. "\n"
  201. " if (a) {\n"
  202. "\n"
  203. " f();\n"
  204. " }\n"
  205. "}",
  206. format("void f() {\n"
  207. "\n"
  208. " if (a) {\n"
  209. "\n"
  210. " f();\n"
  211. "\n"
  212. " }\n"
  213. "\n"
  214. "}",
  215. getLLVMStyle()));
  216. EXPECT_EQ("void f() {\n"
  217. " if (a) {\n"
  218. " f();\n"
  219. " }\n"
  220. "}",
  221. format("void f() {\n"
  222. "\n"
  223. " if (a) {\n"
  224. "\n"
  225. " f();\n"
  226. "\n"
  227. " }\n"
  228. "\n"
  229. "}",
  230. getGoogleStyle()));
  231. // Don't remove empty lines in more complex control statements.
  232. EXPECT_EQ("void f() {\n"
  233. " if (a) {\n"
  234. " f();\n"
  235. "\n"
  236. " } else if (b) {\n"
  237. " f();\n"
  238. " }\n"
  239. "}",
  240. format("void f() {\n"
  241. " if (a) {\n"
  242. " f();\n"
  243. "\n"
  244. " } else if (b) {\n"
  245. " f();\n"
  246. "\n"
  247. " }\n"
  248. "\n"
  249. "}"));
  250. // FIXME: This is slightly inconsistent.
  251. EXPECT_EQ("namespace {\n"
  252. "int i;\n"
  253. "}",
  254. format("namespace {\n"
  255. "int i;\n"
  256. "\n"
  257. "}"));
  258. EXPECT_EQ("namespace {\n"
  259. "int i;\n"
  260. "\n"
  261. "} // namespace",
  262. format("namespace {\n"
  263. "int i;\n"
  264. "\n"
  265. "} // namespace"));
  266. }
  267. TEST_F(FormatTest, ReformatsMovedLines) {
  268. EXPECT_EQ(
  269. "template <typename T> T *getFETokenInfo() const {\n"
  270. " return static_cast<T *>(FETokenInfo);\n"
  271. "}\n"
  272. " int a; // <- Should not be formatted",
  273. format(
  274. "template<typename T>\n"
  275. "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
  276. " int a; // <- Should not be formatted",
  277. 9, 5, getLLVMStyle()));
  278. }
  279. TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
  280. verifyFormat("x = (a) and (b);");
  281. verifyFormat("x = (a) or (b);");
  282. verifyFormat("x = (a) bitand (b);");
  283. verifyFormat("x = (a) bitor (b);");
  284. verifyFormat("x = (a) not_eq (b);");
  285. verifyFormat("x = (a) and_eq (b);");
  286. verifyFormat("x = (a) or_eq (b);");
  287. verifyFormat("x = (a) xor (b);");
  288. }
  289. //===----------------------------------------------------------------------===//
  290. // Tests for control statements.
  291. //===----------------------------------------------------------------------===//
  292. TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
  293. verifyFormat("if (true)\n f();\ng();");
  294. verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
  295. verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
  296. FormatStyle AllowsMergedIf = getLLVMStyle();
  297. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  298. verifyFormat("if (a)\n"
  299. " // comment\n"
  300. " f();",
  301. AllowsMergedIf);
  302. verifyFormat("if (a)\n"
  303. " ;",
  304. AllowsMergedIf);
  305. verifyFormat("if (a)\n"
  306. " if (b) return;",
  307. AllowsMergedIf);
  308. verifyFormat("if (a) // Can't merge this\n"
  309. " f();\n",
  310. AllowsMergedIf);
  311. verifyFormat("if (a) /* still don't merge */\n"
  312. " f();",
  313. AllowsMergedIf);
  314. verifyFormat("if (a) { // Never merge this\n"
  315. " f();\n"
  316. "}",
  317. AllowsMergedIf);
  318. verifyFormat("if (a) {/* Never merge this */\n"
  319. " f();\n"
  320. "}",
  321. AllowsMergedIf);
  322. EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1, AllowsMergedIf));
  323. EXPECT_EQ("if (a) return; // comment",
  324. format("if(a)\nreturn; // comment", 20, 1, AllowsMergedIf));
  325. AllowsMergedIf.ColumnLimit = 14;
  326. verifyFormat("if (a) return;", AllowsMergedIf);
  327. verifyFormat("if (aaaaaaaaa)\n"
  328. " return;",
  329. AllowsMergedIf);
  330. AllowsMergedIf.ColumnLimit = 13;
  331. verifyFormat("if (a)\n return;", AllowsMergedIf);
  332. }
  333. TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
  334. FormatStyle AllowsMergedLoops = getLLVMStyle();
  335. AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
  336. verifyFormat("while (true) continue;", AllowsMergedLoops);
  337. verifyFormat("for (;;) continue;", AllowsMergedLoops);
  338. verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
  339. verifyFormat("while (true)\n"
  340. " ;",
  341. AllowsMergedLoops);
  342. verifyFormat("for (;;)\n"
  343. " ;",
  344. AllowsMergedLoops);
  345. verifyFormat("for (;;)\n"
  346. " for (;;) continue;",
  347. AllowsMergedLoops);
  348. verifyFormat("for (;;) // Can't merge this\n"
  349. " continue;",
  350. AllowsMergedLoops);
  351. verifyFormat("for (;;) /* still don't merge */\n"
  352. " continue;",
  353. AllowsMergedLoops);
  354. }
  355. TEST_F(FormatTest, FormatShortBracedStatements) {
  356. FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
  357. AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
  358. AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
  359. AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
  360. verifyFormat("if (true) {}", AllowSimpleBracedStatements);
  361. verifyFormat("while (true) {}", AllowSimpleBracedStatements);
  362. verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
  363. verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
  364. verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
  365. verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
  366. verifyFormat("if (true) { //\n"
  367. " f();\n"
  368. "}",
  369. AllowSimpleBracedStatements);
  370. verifyFormat("if (true) {\n"
  371. " f();\n"
  372. " f();\n"
  373. "}",
  374. AllowSimpleBracedStatements);
  375. verifyFormat("template <int> struct A2 {\n"
  376. " struct B {};\n"
  377. "};",
  378. AllowSimpleBracedStatements);
  379. AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
  380. verifyFormat("if (true) {\n"
  381. " f();\n"
  382. "}",
  383. AllowSimpleBracedStatements);
  384. AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
  385. verifyFormat("while (true) {\n"
  386. " f();\n"
  387. "}",
  388. AllowSimpleBracedStatements);
  389. verifyFormat("for (;;) {\n"
  390. " f();\n"
  391. "}",
  392. AllowSimpleBracedStatements);
  393. }
  394. TEST_F(FormatTest, ParseIfElse) {
  395. verifyFormat("if (true)\n"
  396. " if (true)\n"
  397. " if (true)\n"
  398. " f();\n"
  399. " else\n"
  400. " g();\n"
  401. " else\n"
  402. " h();\n"
  403. "else\n"
  404. " i();");
  405. verifyFormat("if (true)\n"
  406. " if (true)\n"
  407. " if (true) {\n"
  408. " if (true)\n"
  409. " f();\n"
  410. " } else {\n"
  411. " g();\n"
  412. " }\n"
  413. " else\n"
  414. " h();\n"
  415. "else {\n"
  416. " i();\n"
  417. "}");
  418. verifyFormat("void f() {\n"
  419. " if (a) {\n"
  420. " } else {\n"
  421. " }\n"
  422. "}");
  423. }
  424. TEST_F(FormatTest, ElseIf) {
  425. verifyFormat("if (a) {\n} else if (b) {\n}");
  426. verifyFormat("if (a)\n"
  427. " f();\n"
  428. "else if (b)\n"
  429. " g();\n"
  430. "else\n"
  431. " h();");
  432. verifyFormat("if (a) {\n"
  433. " f();\n"
  434. "}\n"
  435. "// or else ..\n"
  436. "else {\n"
  437. " g()\n"
  438. "}");
  439. verifyFormat("if (a) {\n"
  440. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  441. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  442. "}");
  443. }
  444. TEST_F(FormatTest, FormatsForLoop) {
  445. verifyFormat(
  446. "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
  447. " ++VeryVeryLongLoopVariable)\n"
  448. " ;");
  449. verifyFormat("for (;;)\n"
  450. " f();");
  451. verifyFormat("for (;;) {\n}");
  452. verifyFormat("for (;;) {\n"
  453. " f();\n"
  454. "}");
  455. verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
  456. verifyFormat(
  457. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  458. " E = UnwrappedLines.end();\n"
  459. " I != E; ++I) {\n}");
  460. verifyFormat(
  461. "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
  462. " ++IIIII) {\n}");
  463. verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
  464. " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
  465. " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
  466. verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
  467. " I = FD->getDeclsInPrototypeScope().begin(),\n"
  468. " E = FD->getDeclsInPrototypeScope().end();\n"
  469. " I != E; ++I) {\n}");
  470. // FIXME: Not sure whether we want extra identation in line 3 here:
  471. verifyFormat(
  472. "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  473. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
  474. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  475. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  476. " ++aaaaaaaaaaa) {\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. FormatStyle NoBinPacking = getLLVMStyle();
  485. NoBinPacking.BinPackParameters = false;
  486. verifyFormat("for (int aaaaaaaaaaa = 1;\n"
  487. " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
  488. " aaaaaaaaaaaaaaaa,\n"
  489. " aaaaaaaaaaaaaaaa,\n"
  490. " aaaaaaaaaaaaaaaa);\n"
  491. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  492. "}",
  493. NoBinPacking);
  494. verifyFormat(
  495. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  496. " E = UnwrappedLines.end();\n"
  497. " I != E;\n"
  498. " ++I) {\n}",
  499. NoBinPacking);
  500. }
  501. TEST_F(FormatTest, RangeBasedForLoops) {
  502. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  503. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  504. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
  505. " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
  506. verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
  507. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  508. verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
  509. " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
  510. }
  511. TEST_F(FormatTest, ForEachLoops) {
  512. verifyFormat("void f() {\n"
  513. " foreach (Item *item, itemlist) {}\n"
  514. " Q_FOREACH (Item *item, itemlist) {}\n"
  515. " BOOST_FOREACH (Item *item, itemlist) {}\n"
  516. " UNKNOWN_FORACH(Item * item, itemlist) {}\n"
  517. "}");
  518. }
  519. TEST_F(FormatTest, FormatsWhileLoop) {
  520. verifyFormat("while (true) {\n}");
  521. verifyFormat("while (true)\n"
  522. " f();");
  523. verifyFormat("while () {\n}");
  524. verifyFormat("while () {\n"
  525. " f();\n"
  526. "}");
  527. }
  528. TEST_F(FormatTest, FormatsDoWhile) {
  529. verifyFormat("do {\n"
  530. " do_something();\n"
  531. "} while (something());");
  532. verifyFormat("do\n"
  533. " do_something();\n"
  534. "while (something());");
  535. }
  536. TEST_F(FormatTest, FormatsSwitchStatement) {
  537. verifyFormat("switch (x) {\n"
  538. "case 1:\n"
  539. " f();\n"
  540. " break;\n"
  541. "case kFoo:\n"
  542. "case ns::kBar:\n"
  543. "case kBaz:\n"
  544. " break;\n"
  545. "default:\n"
  546. " g();\n"
  547. " break;\n"
  548. "}");
  549. verifyFormat("switch (x) {\n"
  550. "case 1: {\n"
  551. " f();\n"
  552. " break;\n"
  553. "}\n"
  554. "case 2: {\n"
  555. " break;\n"
  556. "}\n"
  557. "}");
  558. verifyFormat("switch (x) {\n"
  559. "case 1: {\n"
  560. " f();\n"
  561. " {\n"
  562. " g();\n"
  563. " h();\n"
  564. " }\n"
  565. " break;\n"
  566. "}\n"
  567. "}");
  568. verifyFormat("switch (x) {\n"
  569. "case 1: {\n"
  570. " f();\n"
  571. " if (foo) {\n"
  572. " g();\n"
  573. " h();\n"
  574. " }\n"
  575. " break;\n"
  576. "}\n"
  577. "}");
  578. verifyFormat("switch (x) {\n"
  579. "case 1: {\n"
  580. " f();\n"
  581. " g();\n"
  582. "} break;\n"
  583. "}");
  584. verifyFormat("switch (test)\n"
  585. " ;");
  586. verifyFormat("switch (x) {\n"
  587. "default: {\n"
  588. " // Do nothing.\n"
  589. "}\n"
  590. "}");
  591. verifyFormat("switch (x) {\n"
  592. "// comment\n"
  593. "// if 1, do f()\n"
  594. "case 1:\n"
  595. " f();\n"
  596. "}");
  597. verifyFormat("switch (x) {\n"
  598. "case 1:\n"
  599. " // Do amazing stuff\n"
  600. " {\n"
  601. " f();\n"
  602. " g();\n"
  603. " }\n"
  604. " break;\n"
  605. "}");
  606. verifyFormat("#define A \\\n"
  607. " switch (x) { \\\n"
  608. " case a: \\\n"
  609. " foo = b; \\\n"
  610. " }", getLLVMStyleWithColumns(20));
  611. verifyFormat("#define OPERATION_CASE(name) \\\n"
  612. " case OP_name: \\\n"
  613. " return operations::Operation##name\n",
  614. getLLVMStyleWithColumns(40));
  615. verifyGoogleFormat("switch (x) {\n"
  616. " case 1:\n"
  617. " f();\n"
  618. " break;\n"
  619. " case kFoo:\n"
  620. " case ns::kBar:\n"
  621. " case kBaz:\n"
  622. " break;\n"
  623. " default:\n"
  624. " g();\n"
  625. " break;\n"
  626. "}");
  627. verifyGoogleFormat("switch (x) {\n"
  628. " case 1: {\n"
  629. " f();\n"
  630. " break;\n"
  631. " }\n"
  632. "}");
  633. verifyGoogleFormat("switch (test)\n"
  634. " ;");
  635. verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
  636. " case OP_name: \\\n"
  637. " return operations::Operation##name\n");
  638. verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
  639. " // Get the correction operation class.\n"
  640. " switch (OpCode) {\n"
  641. " CASE(Add);\n"
  642. " CASE(Subtract);\n"
  643. " default:\n"
  644. " return operations::Unknown;\n"
  645. " }\n"
  646. "#undef OPERATION_CASE\n"
  647. "}");
  648. verifyFormat("DEBUG({\n"
  649. " switch (x) {\n"
  650. " case A:\n"
  651. " f();\n"
  652. " break;\n"
  653. " // On B:\n"
  654. " case B:\n"
  655. " g();\n"
  656. " break;\n"
  657. " }\n"
  658. "});");
  659. verifyFormat("switch (a) {\n"
  660. "case (b):\n"
  661. " return;\n"
  662. "}");
  663. verifyFormat("switch (a) {\n"
  664. "case some_namespace::\n"
  665. " some_constant:\n"
  666. " return;\n"
  667. "}",
  668. getLLVMStyleWithColumns(34));
  669. }
  670. TEST_F(FormatTest, CaseRanges) {
  671. verifyFormat("switch (x) {\n"
  672. "case 'A' ... 'Z':\n"
  673. "case 1 ... 5:\n"
  674. " break;\n"
  675. "}");
  676. }
  677. TEST_F(FormatTest, ShortCaseLabels) {
  678. FormatStyle Style = getLLVMStyle();
  679. Style.AllowShortCaseLabelsOnASingleLine = true;
  680. verifyFormat("switch (a) {\n"
  681. "case 1: x = 1; break;\n"
  682. "case 2: return;\n"
  683. "case 3:\n"
  684. "case 4:\n"
  685. "case 5: return;\n"
  686. "case 6: // comment\n"
  687. " return;\n"
  688. "case 7:\n"
  689. " // comment\n"
  690. " return;\n"
  691. "default: y = 1; break;\n"
  692. "}",
  693. Style);
  694. verifyFormat("switch (a) {\n"
  695. "#if FOO\n"
  696. "case 0: return 0;\n"
  697. "#endif\n"
  698. "}",
  699. Style);
  700. verifyFormat("switch (a) {\n"
  701. "case 1: {\n"
  702. "}\n"
  703. "case 2: {\n"
  704. " return;\n"
  705. "}\n"
  706. "case 3: {\n"
  707. " x = 1;\n"
  708. " return;\n"
  709. "}\n"
  710. "case 4:\n"
  711. " if (x)\n"
  712. " return;\n"
  713. "}",
  714. Style);
  715. Style.ColumnLimit = 21;
  716. verifyFormat("switch (a) {\n"
  717. "case 1: x = 1; break;\n"
  718. "case 2: return;\n"
  719. "case 3:\n"
  720. "case 4:\n"
  721. "case 5: return;\n"
  722. "default:\n"
  723. " y = 1;\n"
  724. " break;\n"
  725. "}",
  726. Style);
  727. }
  728. TEST_F(FormatTest, FormatsLabels) {
  729. verifyFormat("void f() {\n"
  730. " some_code();\n"
  731. "test_label:\n"
  732. " some_other_code();\n"
  733. " {\n"
  734. " some_more_code();\n"
  735. " another_label:\n"
  736. " some_more_code();\n"
  737. " }\n"
  738. "}");
  739. verifyFormat("some_code();\n"
  740. "test_label:\n"
  741. "some_other_code();");
  742. }
  743. //===----------------------------------------------------------------------===//
  744. // Tests for comments.
  745. //===----------------------------------------------------------------------===//
  746. TEST_F(FormatTest, UnderstandsSingleLineComments) {
  747. verifyFormat("//* */");
  748. verifyFormat("// line 1\n"
  749. "// line 2\n"
  750. "void f() {}\n");
  751. verifyFormat("void f() {\n"
  752. " // Doesn't do anything\n"
  753. "}");
  754. verifyFormat("SomeObject\n"
  755. " // Calling someFunction on SomeObject\n"
  756. " .someFunction();");
  757. verifyFormat("auto result = SomeObject\n"
  758. " // Calling someFunction on SomeObject\n"
  759. " .someFunction();");
  760. verifyFormat("void f(int i, // some comment (probably for i)\n"
  761. " int j, // some comment (probably for j)\n"
  762. " int k); // some comment (probably for k)");
  763. verifyFormat("void f(int i,\n"
  764. " // some comment (probably for j)\n"
  765. " int j,\n"
  766. " // some comment (probably for k)\n"
  767. " int k);");
  768. verifyFormat("int i // This is a fancy variable\n"
  769. " = 5; // with nicely aligned comment.");
  770. verifyFormat("// Leading comment.\n"
  771. "int a; // Trailing comment.");
  772. verifyFormat("int a; // Trailing comment\n"
  773. " // on 2\n"
  774. " // or 3 lines.\n"
  775. "int b;");
  776. verifyFormat("int a; // Trailing comment\n"
  777. "\n"
  778. "// Leading comment.\n"
  779. "int b;");
  780. verifyFormat("int a; // Comment.\n"
  781. " // More details.\n"
  782. "int bbbb; // Another comment.");
  783. verifyFormat(
  784. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  785. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n"
  786. "int cccccccccccccccccccccccccccccc; // comment\n"
  787. "int ddd; // looooooooooooooooooooooooong comment\n"
  788. "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  789. "int bbbbbbbbbbbbbbbbbbbbb; // comment\n"
  790. "int ccccccccccccccccccc; // comment");
  791. verifyFormat("#include \"a\" // comment\n"
  792. "#include \"a/b/c\" // comment");
  793. verifyFormat("#include <a> // comment\n"
  794. "#include <a/b/c> // comment");
  795. EXPECT_EQ("#include \"a\" // comment\n"
  796. "#include \"a/b/c\" // comment",
  797. format("#include \\\n"
  798. " \"a\" // comment\n"
  799. "#include \"a/b/c\" // comment"));
  800. verifyFormat("enum E {\n"
  801. " // comment\n"
  802. " VAL_A, // comment\n"
  803. " VAL_B\n"
  804. "};");
  805. verifyFormat(
  806. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  807. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
  808. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  809. " // Comment inside a statement.\n"
  810. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  811. verifyFormat("SomeFunction(a,\n"
  812. " // comment\n"
  813. " b + x);");
  814. verifyFormat("SomeFunction(a, a,\n"
  815. " // comment\n"
  816. " b + x);");
  817. verifyFormat(
  818. "bool aaaaaaaaaaaaa = // comment\n"
  819. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  820. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  821. verifyFormat("int aaaa; // aaaaa\n"
  822. "int aa; // aaaaaaa",
  823. getLLVMStyleWithColumns(20));
  824. EXPECT_EQ("void f() { // This does something ..\n"
  825. "}\n"
  826. "int a; // This is unrelated",
  827. format("void f() { // This does something ..\n"
  828. " }\n"
  829. "int a; // This is unrelated"));
  830. EXPECT_EQ("class C {\n"
  831. " void f() { // This does something ..\n"
  832. " } // awesome..\n"
  833. "\n"
  834. " int a; // This is unrelated\n"
  835. "};",
  836. format("class C{void f() { // This does something ..\n"
  837. " } // awesome..\n"
  838. " \n"
  839. "int a; // This is unrelated\n"
  840. "};"));
  841. EXPECT_EQ("int i; // single line trailing comment",
  842. format("int i;\\\n// single line trailing comment"));
  843. verifyGoogleFormat("int a; // Trailing comment.");
  844. verifyFormat("someFunction(anotherFunction( // Force break.\n"
  845. " parameter));");
  846. verifyGoogleFormat("#endif // HEADER_GUARD");
  847. verifyFormat("const char *test[] = {\n"
  848. " // A\n"
  849. " \"aaaa\",\n"
  850. " // B\n"
  851. " \"aaaaa\"};");
  852. verifyGoogleFormat(
  853. "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  854. " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment");
  855. EXPECT_EQ("D(a, {\n"
  856. " // test\n"
  857. " int a;\n"
  858. "});",
  859. format("D(a, {\n"
  860. "// test\n"
  861. "int a;\n"
  862. "});"));
  863. EXPECT_EQ("lineWith(); // comment\n"
  864. "// at start\n"
  865. "otherLine();",
  866. format("lineWith(); // comment\n"
  867. "// at start\n"
  868. "otherLine();"));
  869. EXPECT_EQ("lineWith(); // comment\n"
  870. " // at start\n"
  871. "otherLine();",
  872. format("lineWith(); // comment\n"
  873. " // at start\n"
  874. "otherLine();"));
  875. EXPECT_EQ("lineWith(); // comment\n"
  876. "// at start\n"
  877. "otherLine(); // comment",
  878. format("lineWith(); // comment\n"
  879. "// at start\n"
  880. "otherLine(); // comment"));
  881. EXPECT_EQ("lineWith();\n"
  882. "// at start\n"
  883. "otherLine(); // comment",
  884. format("lineWith();\n"
  885. " // at start\n"
  886. "otherLine(); // comment"));
  887. EXPECT_EQ("// first\n"
  888. "// at start\n"
  889. "otherLine(); // comment",
  890. format("// first\n"
  891. " // at start\n"
  892. "otherLine(); // comment"));
  893. EXPECT_EQ("f();\n"
  894. "// first\n"
  895. "// at start\n"
  896. "otherLine(); // comment",
  897. format("f();\n"
  898. "// first\n"
  899. " // at start\n"
  900. "otherLine(); // comment"));
  901. verifyFormat("f(); // comment\n"
  902. "// first\n"
  903. "// at start\n"
  904. "otherLine();");
  905. EXPECT_EQ("f(); // comment\n"
  906. "// first\n"
  907. "// at start\n"
  908. "otherLine();",
  909. format("f(); // comment\n"
  910. "// first\n"
  911. " // at start\n"
  912. "otherLine();"));
  913. EXPECT_EQ("f(); // comment\n"
  914. " // first\n"
  915. "// at start\n"
  916. "otherLine();",
  917. format("f(); // comment\n"
  918. " // first\n"
  919. "// at start\n"
  920. "otherLine();"));
  921. EXPECT_EQ("void f() {\n"
  922. " lineWith(); // comment\n"
  923. " // at start\n"
  924. "}",
  925. format("void f() {\n"
  926. " lineWith(); // comment\n"
  927. " // at start\n"
  928. "}"));
  929. verifyFormat(
  930. "#define A \\\n"
  931. " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
  932. " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
  933. getLLVMStyleWithColumns(60));
  934. verifyFormat(
  935. "#define A \\\n"
  936. " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
  937. " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
  938. getLLVMStyleWithColumns(61));
  939. verifyFormat("if ( // This is some comment\n"
  940. " x + 3) {\n"
  941. "}");
  942. EXPECT_EQ("if ( // This is some comment\n"
  943. " // spanning two lines\n"
  944. " x + 3) {\n"
  945. "}",
  946. format("if( // This is some comment\n"
  947. " // spanning two lines\n"
  948. " x + 3) {\n"
  949. "}"));
  950. verifyNoCrash("/\\\n/");
  951. verifyNoCrash("/\\\n* */");
  952. }
  953. TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
  954. EXPECT_EQ("SomeFunction(a,\n"
  955. " b, // comment\n"
  956. " c);",
  957. format("SomeFunction(a,\n"
  958. " b, // comment\n"
  959. " c);"));
  960. EXPECT_EQ("SomeFunction(a, b,\n"
  961. " // comment\n"
  962. " c);",
  963. format("SomeFunction(a,\n"
  964. " b,\n"
  965. " // comment\n"
  966. " c);"));
  967. EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
  968. " c);",
  969. format("SomeFunction(a, b, // comment (unclear relation)\n"
  970. " c);"));
  971. EXPECT_EQ("SomeFunction(a, // comment\n"
  972. " b,\n"
  973. " c); // comment",
  974. format("SomeFunction(a, // comment\n"
  975. " b,\n"
  976. " c); // comment"));
  977. }
  978. TEST_F(FormatTest, CanFormatCommentsLocally) {
  979. EXPECT_EQ("int a; // comment\n"
  980. "int b; // comment",
  981. format("int a; // comment\n"
  982. "int b; // comment",
  983. 0, 0, getLLVMStyle()));
  984. EXPECT_EQ("int a; // comment\n"
  985. " // line 2\n"
  986. "int b;",
  987. format("int a; // comment\n"
  988. " // line 2\n"
  989. "int b;",
  990. 28, 0, getLLVMStyle()));
  991. EXPECT_EQ("int aaaaaa; // comment\n"
  992. "int b;\n"
  993. "int c; // unrelated comment",
  994. format("int aaaaaa; // comment\n"
  995. "int b;\n"
  996. "int c; // unrelated comment",
  997. 31, 0, getLLVMStyle()));
  998. EXPECT_EQ("int a; // This\n"
  999. " // is\n"
  1000. " // a",
  1001. format("int a; // This\n"
  1002. " // is\n"
  1003. " // a",
  1004. 0, 0, getLLVMStyle()));
  1005. EXPECT_EQ("int a; // This\n"
  1006. " // is\n"
  1007. " // a\n"
  1008. "// This is b\n"
  1009. "int b;",
  1010. format("int a; // This\n"
  1011. " // is\n"
  1012. " // a\n"
  1013. "// This is b\n"
  1014. "int b;",
  1015. 0, 0, getLLVMStyle()));
  1016. EXPECT_EQ("int a; // This\n"
  1017. " // is\n"
  1018. " // a\n"
  1019. "\n"
  1020. " // This is unrelated",
  1021. format("int a; // This\n"
  1022. " // is\n"
  1023. " // a\n"
  1024. "\n"
  1025. " // This is unrelated",
  1026. 0, 0, getLLVMStyle()));
  1027. EXPECT_EQ("int a;\n"
  1028. "// This is\n"
  1029. "// not formatted. ",
  1030. format("int a;\n"
  1031. "// This is\n"
  1032. "// not formatted. ",
  1033. 0, 0, getLLVMStyle()));
  1034. }
  1035. TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
  1036. EXPECT_EQ("// comment", format("// comment "));
  1037. EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
  1038. format("int aaaaaaa, bbbbbbb; // comment ",
  1039. getLLVMStyleWithColumns(33)));
  1040. EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f "));
  1041. EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f "));
  1042. }
  1043. TEST_F(FormatTest, UnderstandsBlockComments) {
  1044. verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
  1045. verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }");
  1046. EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
  1047. " bbbbbbbbbbbbbbbbbbbbbbbbb);",
  1048. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n"
  1049. "/* Trailing comment for aa... */\n"
  1050. " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  1051. EXPECT_EQ(
  1052. "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  1053. " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
  1054. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
  1055. "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  1056. EXPECT_EQ(
  1057. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1058. " aaaaaaaaaaaaaaaaaa,\n"
  1059. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  1060. "}",
  1061. format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1062. " aaaaaaaaaaaaaaaaaa ,\n"
  1063. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  1064. "}"));
  1065. FormatStyle NoBinPacking = getLLVMStyle();
  1066. NoBinPacking.BinPackParameters = false;
  1067. verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
  1068. " /* parameter 2 */ aaaaaa,\n"
  1069. " /* parameter 3 */ aaaaaa,\n"
  1070. " /* parameter 4 */ aaaaaa);",
  1071. NoBinPacking);
  1072. // Aligning block comments in macros.
  1073. verifyGoogleFormat("#define A \\\n"
  1074. " int i; /*a*/ \\\n"
  1075. " int jjj; /*b*/");
  1076. }
  1077. TEST_F(FormatTest, AlignsBlockComments) {
  1078. EXPECT_EQ("/*\n"
  1079. " * Really multi-line\n"
  1080. " * comment.\n"
  1081. " */\n"
  1082. "void f() {}",
  1083. format(" /*\n"
  1084. " * Really multi-line\n"
  1085. " * comment.\n"
  1086. " */\n"
  1087. " void f() {}"));
  1088. EXPECT_EQ("class C {\n"
  1089. " /*\n"
  1090. " * Another multi-line\n"
  1091. " * comment.\n"
  1092. " */\n"
  1093. " void f() {}\n"
  1094. "};",
  1095. format("class C {\n"
  1096. "/*\n"
  1097. " * Another multi-line\n"
  1098. " * comment.\n"
  1099. " */\n"
  1100. "void f() {}\n"
  1101. "};"));
  1102. EXPECT_EQ("/*\n"
  1103. " 1. This is a comment with non-trivial formatting.\n"
  1104. " 1.1. We have to indent/outdent all lines equally\n"
  1105. " 1.1.1. to keep the formatting.\n"
  1106. " */",
  1107. format(" /*\n"
  1108. " 1. This is a comment with non-trivial formatting.\n"
  1109. " 1.1. We have to indent/outdent all lines equally\n"
  1110. " 1.1.1. to keep the formatting.\n"
  1111. " */"));
  1112. EXPECT_EQ("/*\n"
  1113. "Don't try to outdent if there's not enough indentation.\n"
  1114. "*/",
  1115. format(" /*\n"
  1116. " Don't try to outdent if there's not enough indentation.\n"
  1117. " */"));
  1118. EXPECT_EQ("int i; /* Comment with empty...\n"
  1119. " *\n"
  1120. " * line. */",
  1121. format("int i; /* Comment with empty...\n"
  1122. " *\n"
  1123. " * line. */"));
  1124. EXPECT_EQ("int foobar = 0; /* comment */\n"
  1125. "int bar = 0; /* multiline\n"
  1126. " comment 1 */\n"
  1127. "int baz = 0; /* multiline\n"
  1128. " comment 2 */\n"
  1129. "int bzz = 0; /* multiline\n"
  1130. " comment 3 */",
  1131. format("int foobar = 0; /* comment */\n"
  1132. "int bar = 0; /* multiline\n"
  1133. " comment 1 */\n"
  1134. "int baz = 0; /* multiline\n"
  1135. " comment 2 */\n"
  1136. "int bzz = 0; /* multiline\n"
  1137. " comment 3 */"));
  1138. EXPECT_EQ("int foobar = 0; /* comment */\n"
  1139. "int bar = 0; /* multiline\n"
  1140. " comment */\n"
  1141. "int baz = 0; /* multiline\n"
  1142. "comment */",
  1143. format("int foobar = 0; /* comment */\n"
  1144. "int bar = 0; /* multiline\n"
  1145. "comment */\n"
  1146. "int baz = 0; /* multiline\n"
  1147. "comment */"));
  1148. }
  1149. TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
  1150. EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1151. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
  1152. format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1153. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
  1154. EXPECT_EQ(
  1155. "void ffffffffffff(\n"
  1156. " int aaaaaaaa, int bbbbbbbb,\n"
  1157. " int cccccccccccc) { /*\n"
  1158. " aaaaaaaaaa\n"
  1159. " aaaaaaaaaaaaa\n"
  1160. " bbbbbbbbbbbbbb\n"
  1161. " bbbbbbbbbb\n"
  1162. " */\n"
  1163. "}",
  1164. format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
  1165. "{ /*\n"
  1166. " aaaaaaaaaa aaaaaaaaaaaaa\n"
  1167. " bbbbbbbbbbbbbb bbbbbbbbbb\n"
  1168. " */\n"
  1169. "}",
  1170. getLLVMStyleWithColumns(40)));
  1171. }
  1172. TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
  1173. EXPECT_EQ("void ffffffffff(\n"
  1174. " int aaaaa /* test */);",
  1175. format("void ffffffffff(int aaaaa /* test */);",
  1176. getLLVMStyleWithColumns(35)));
  1177. }
  1178. TEST_F(FormatTest, SplitsLongCxxComments) {
  1179. EXPECT_EQ("// A comment that\n"
  1180. "// doesn't fit on\n"
  1181. "// one line",
  1182. format("// A comment that doesn't fit on one line",
  1183. getLLVMStyleWithColumns(20)));
  1184. EXPECT_EQ("// a b c d\n"
  1185. "// e f g\n"
  1186. "// h i j k",
  1187. format("// a b c d e f g h i j k",
  1188. getLLVMStyleWithColumns(10)));
  1189. EXPECT_EQ("// a b c d\n"
  1190. "// e f g\n"
  1191. "// h i j k",
  1192. format("\\\n// a b c d e f g h i j k",
  1193. getLLVMStyleWithColumns(10)));
  1194. EXPECT_EQ("if (true) // A comment that\n"
  1195. " // doesn't fit on\n"
  1196. " // one line",
  1197. format("if (true) // A comment that doesn't fit on one line ",
  1198. getLLVMStyleWithColumns(30)));
  1199. EXPECT_EQ("// Don't_touch_leading_whitespace",
  1200. format("// Don't_touch_leading_whitespace",
  1201. getLLVMStyleWithColumns(20)));
  1202. EXPECT_EQ("// Add leading\n"
  1203. "// whitespace",
  1204. format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
  1205. EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
  1206. EXPECT_EQ("// Even if it makes the line exceed the column\n"
  1207. "// limit",
  1208. format("//Even if it makes the line exceed the column limit",
  1209. getLLVMStyleWithColumns(51)));
  1210. EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
  1211. EXPECT_EQ("// aa bb cc dd",
  1212. format("// aa bb cc dd ",
  1213. getLLVMStyleWithColumns(15)));
  1214. EXPECT_EQ("// A comment before\n"
  1215. "// a macro\n"
  1216. "// definition\n"
  1217. "#define a b",
  1218. format("// A comment before a macro definition\n"
  1219. "#define a b",
  1220. getLLVMStyleWithColumns(20)));
  1221. EXPECT_EQ("void ffffff(\n"
  1222. " int aaaaaaaaa, // wwww\n"
  1223. " int bbbbbbbbbb, // xxxxxxx\n"
  1224. " // yyyyyyyyyy\n"
  1225. " int c, int d, int e) {}",
  1226. format("void ffffff(\n"
  1227. " int aaaaaaaaa, // wwww\n"
  1228. " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
  1229. " int c, int d, int e) {}",
  1230. getLLVMStyleWithColumns(40)));
  1231. EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1232. format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1233. getLLVMStyleWithColumns(20)));
  1234. EXPECT_EQ(
  1235. "#define XXX // a b c d\n"
  1236. " // e f g h",
  1237. format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
  1238. EXPECT_EQ(
  1239. "#define XXX // q w e r\n"
  1240. " // t y u i",
  1241. format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
  1242. }
  1243. TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {
  1244. EXPECT_EQ("// A comment\n"
  1245. "// that doesn't\n"
  1246. "// fit on one\n"
  1247. "// line",
  1248. format("// A comment that doesn't fit on one line",
  1249. getLLVMStyleWithColumns(20)));
  1250. EXPECT_EQ("/// A comment\n"
  1251. "/// that doesn't\n"
  1252. "/// fit on one\n"
  1253. "/// line",
  1254. format("/// A comment that doesn't fit on one line",
  1255. getLLVMStyleWithColumns(20)));
  1256. }
  1257. TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
  1258. EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1259. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1260. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1261. format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1262. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1263. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
  1264. EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1265. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1266. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1267. format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1268. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1269. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1270. getLLVMStyleWithColumns(50)));
  1271. // FIXME: One day we might want to implement adjustment of leading whitespace
  1272. // of the consecutive lines in this kind of comment:
  1273. EXPECT_EQ("double\n"
  1274. " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1275. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1276. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1277. format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1278. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1279. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1280. getLLVMStyleWithColumns(49)));
  1281. }
  1282. TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) {
  1283. FormatStyle Pragmas = getLLVMStyleWithColumns(30);
  1284. Pragmas.CommentPragmas = "^ IWYU pragma:";
  1285. EXPECT_EQ(
  1286. "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
  1287. format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
  1288. EXPECT_EQ(
  1289. "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
  1290. format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
  1291. }
  1292. TEST_F(FormatTest, PriorityOfCommentBreaking) {
  1293. EXPECT_EQ("if (xxx ==\n"
  1294. " yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  1295. " zzz)\n"
  1296. " q();",
  1297. format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  1298. " zzz) q();",
  1299. getLLVMStyleWithColumns(40)));
  1300. EXPECT_EQ("if (xxxxxxxxxx ==\n"
  1301. " yyy && // aaaaaa bbbbbbbb cccc\n"
  1302. " zzz)\n"
  1303. " q();",
  1304. format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
  1305. " zzz) q();",
  1306. getLLVMStyleWithColumns(40)));
  1307. EXPECT_EQ("if (xxxxxxxxxx &&\n"
  1308. " yyy || // aaaaaa bbbbbbbb cccc\n"
  1309. " zzz)\n"
  1310. " q();",
  1311. format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
  1312. " zzz) q();",
  1313. getLLVMStyleWithColumns(40)));
  1314. EXPECT_EQ("fffffffff(\n"
  1315. " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  1316. " zzz);",
  1317. format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  1318. " zzz);",
  1319. getLLVMStyleWithColumns(40)));
  1320. }
  1321. TEST_F(FormatTest, MultiLineCommentsInDefines) {
  1322. EXPECT_EQ("#define A(x) /* \\\n"
  1323. " a comment \\\n"
  1324. " inside */ \\\n"
  1325. " f();",
  1326. format("#define A(x) /* \\\n"
  1327. " a comment \\\n"
  1328. " inside */ \\\n"
  1329. " f();",
  1330. getLLVMStyleWithColumns(17)));
  1331. EXPECT_EQ("#define A( \\\n"
  1332. " x) /* \\\n"
  1333. " a comment \\\n"
  1334. " inside */ \\\n"
  1335. " f();",
  1336. format("#define A( \\\n"
  1337. " x) /* \\\n"
  1338. " a comment \\\n"
  1339. " inside */ \\\n"
  1340. " f();",
  1341. getLLVMStyleWithColumns(17)));
  1342. }
  1343. TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
  1344. EXPECT_EQ("namespace {}\n// Test\n#define A",
  1345. format("namespace {}\n // Test\n#define A"));
  1346. EXPECT_EQ("namespace {}\n/* Test */\n#define A",
  1347. format("namespace {}\n /* Test */\n#define A"));
  1348. EXPECT_EQ("namespace {}\n/* Test */ #define A",
  1349. format("namespace {}\n /* Test */ #define A"));
  1350. }
  1351. TEST_F(FormatTest, SplitsLongLinesInComments) {
  1352. EXPECT_EQ("/* This is a long\n"
  1353. " * comment that\n"
  1354. " * doesn't\n"
  1355. " * fit on one line.\n"
  1356. " */",
  1357. format("/* "
  1358. "This is a long "
  1359. "comment that "
  1360. "doesn't "
  1361. "fit on one line. */",
  1362. getLLVMStyleWithColumns(20)));
  1363. EXPECT_EQ("/* a b c d\n"
  1364. " * e f g\n"
  1365. " * h i j k\n"
  1366. " */",
  1367. format("/* a b c d e f g h i j k */",
  1368. getLLVMStyleWithColumns(10)));
  1369. EXPECT_EQ("/* a b c d\n"
  1370. " * e f g\n"
  1371. " * h i j k\n"
  1372. " */",
  1373. format("\\\n/* a b c d e f g h i j k */",
  1374. getLLVMStyleWithColumns(10)));
  1375. EXPECT_EQ("/*\n"
  1376. "This is a long\n"
  1377. "comment that doesn't\n"
  1378. "fit on one line.\n"
  1379. "*/",
  1380. format("/*\n"
  1381. "This is a long "
  1382. "comment that doesn't "
  1383. "fit on one line. \n"
  1384. "*/", getLLVMStyleWithColumns(20)));
  1385. EXPECT_EQ("/*\n"
  1386. " * This is a long\n"
  1387. " * comment that\n"
  1388. " * doesn't fit on\n"
  1389. " * one line.\n"
  1390. " */",
  1391. format("/* \n"
  1392. " * This is a long "
  1393. " comment that "
  1394. " doesn't fit on "
  1395. " one line. \n"
  1396. " */", getLLVMStyleWithColumns(20)));
  1397. EXPECT_EQ("/*\n"
  1398. " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
  1399. " * so_it_should_be_broken\n"
  1400. " * wherever_a_space_occurs\n"
  1401. " */",
  1402. format("/*\n"
  1403. " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
  1404. " so_it_should_be_broken "
  1405. " wherever_a_space_occurs \n"
  1406. " */",
  1407. getLLVMStyleWithColumns(20)));
  1408. EXPECT_EQ("/*\n"
  1409. " * This_comment_can_not_be_broken_into_lines\n"
  1410. " */",
  1411. format("/*\n"
  1412. " * This_comment_can_not_be_broken_into_lines\n"
  1413. " */",
  1414. getLLVMStyleWithColumns(20)));
  1415. EXPECT_EQ("{\n"
  1416. " /*\n"
  1417. " This is another\n"
  1418. " long comment that\n"
  1419. " doesn't fit on one\n"
  1420. " line 1234567890\n"
  1421. " */\n"
  1422. "}",
  1423. format("{\n"
  1424. "/*\n"
  1425. "This is another "
  1426. " long comment that "
  1427. " doesn't fit on one"
  1428. " line 1234567890\n"
  1429. "*/\n"
  1430. "}", getLLVMStyleWithColumns(20)));
  1431. EXPECT_EQ("{\n"
  1432. " /*\n"
  1433. " * This i s\n"
  1434. " * another comment\n"
  1435. " * t hat doesn' t\n"
  1436. " * fit on one l i\n"
  1437. " * n e\n"
  1438. " */\n"
  1439. "}",
  1440. format("{\n"
  1441. "/*\n"
  1442. " * This i s"
  1443. " another comment"
  1444. " t hat doesn' t"
  1445. " fit on one l i"
  1446. " n e\n"
  1447. " */\n"
  1448. "}", getLLVMStyleWithColumns(20)));
  1449. EXPECT_EQ("/*\n"
  1450. " * This is a long\n"
  1451. " * comment that\n"
  1452. " * doesn't fit on\n"
  1453. " * one line\n"
  1454. " */",
  1455. format(" /*\n"
  1456. " * This is a long comment that doesn't fit on one line\n"
  1457. " */", getLLVMStyleWithColumns(20)));
  1458. EXPECT_EQ("{\n"
  1459. " if (something) /* This is a\n"
  1460. " long\n"
  1461. " comment */\n"
  1462. " ;\n"
  1463. "}",
  1464. format("{\n"
  1465. " if (something) /* This is a long comment */\n"
  1466. " ;\n"
  1467. "}",
  1468. getLLVMStyleWithColumns(30)));
  1469. EXPECT_EQ("/* A comment before\n"
  1470. " * a macro\n"
  1471. " * definition */\n"
  1472. "#define a b",
  1473. format("/* A comment before a macro definition */\n"
  1474. "#define a b",
  1475. getLLVMStyleWithColumns(20)));
  1476. EXPECT_EQ("/* some comment\n"
  1477. " * a comment\n"
  1478. "* that we break\n"
  1479. " * another comment\n"
  1480. "* we have to break\n"
  1481. "* a left comment\n"
  1482. " */",
  1483. format(" /* some comment\n"
  1484. " * a comment that we break\n"
  1485. " * another comment we have to break\n"
  1486. "* a left comment\n"
  1487. " */",
  1488. getLLVMStyleWithColumns(20)));
  1489. EXPECT_EQ("/*\n"
  1490. "\n"
  1491. "\n"
  1492. " */\n",
  1493. format(" /* \n"
  1494. " \n"
  1495. " \n"
  1496. " */\n"));
  1497. EXPECT_EQ("/* a a */",
  1498. format("/* a a */", getLLVMStyleWithColumns(15)));
  1499. EXPECT_EQ("/* a a bc */",
  1500. format("/* a a bc */", getLLVMStyleWithColumns(15)));
  1501. EXPECT_EQ("/* aaa aaa\n"
  1502. " * aaaaa */",
  1503. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1504. EXPECT_EQ("/* aaa aaa\n"
  1505. " * aaaaa */",
  1506. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1507. }
  1508. TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
  1509. EXPECT_EQ("#define X \\\n"
  1510. " /* \\\n"
  1511. " Test \\\n"
  1512. " Macro comment \\\n"
  1513. " with a long \\\n"
  1514. " line \\\n"
  1515. " */ \\\n"
  1516. " A + B",
  1517. format("#define X \\\n"
  1518. " /*\n"
  1519. " Test\n"
  1520. " Macro comment with a long line\n"
  1521. " */ \\\n"
  1522. " A + B",
  1523. getLLVMStyleWithColumns(20)));
  1524. EXPECT_EQ("#define X \\\n"
  1525. " /* Macro comment \\\n"
  1526. " with a long \\\n"
  1527. " line */ \\\n"
  1528. " A + B",
  1529. format("#define X \\\n"
  1530. " /* Macro comment with a long\n"
  1531. " line */ \\\n"
  1532. " A + B",
  1533. getLLVMStyleWithColumns(20)));
  1534. EXPECT_EQ("#define X \\\n"
  1535. " /* Macro comment \\\n"
  1536. " * with a long \\\n"
  1537. " * line */ \\\n"
  1538. " A + B",
  1539. format("#define X \\\n"
  1540. " /* Macro comment with a long line */ \\\n"
  1541. " A + B",
  1542. getLLVMStyleWithColumns(20)));
  1543. }
  1544. TEST_F(FormatTest, CommentsInStaticInitializers) {
  1545. EXPECT_EQ(
  1546. "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
  1547. " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
  1548. " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
  1549. " aaaaaaaaaaaaaaaaaaaa, // comment\n"
  1550. " aaaaaaaaaaaaaaaaaaaa};",
  1551. format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
  1552. " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
  1553. " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
  1554. " aaaaaaaaaaaaaaaaaaaa , // comment\n"
  1555. " aaaaaaaaaaaaaaaaaaaa };"));
  1556. verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1557. " bbbbbbbbbbb, ccccccccccc};");
  1558. verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1559. " // comment for bb....\n"
  1560. " bbbbbbbbbbb, ccccccccccc};");
  1561. verifyGoogleFormat(
  1562. "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1563. " bbbbbbbbbbb, ccccccccccc};");
  1564. verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1565. " // comment for bb....\n"
  1566. " bbbbbbbbbbb, ccccccccccc};");
  1567. verifyFormat("S s = {{a, b, c}, // Group #1\n"
  1568. " {d, e, f}, // Group #2\n"
  1569. " {g, h, i}}; // Group #3");
  1570. verifyFormat("S s = {{// Group #1\n"
  1571. " a, b, c},\n"
  1572. " {// Group #2\n"
  1573. " d, e, f},\n"
  1574. " {// Group #3\n"
  1575. " g, h, i}};");
  1576. EXPECT_EQ("S s = {\n"
  1577. " // Some comment\n"
  1578. " a,\n"
  1579. "\n"
  1580. " // Comment after empty line\n"
  1581. " b}",
  1582. format("S s = {\n"
  1583. " // Some comment\n"
  1584. " a,\n"
  1585. " \n"
  1586. " // Comment after empty line\n"
  1587. " b\n"
  1588. "}"));
  1589. EXPECT_EQ("S s = {\n"
  1590. " /* Some comment */\n"
  1591. " a,\n"
  1592. "\n"
  1593. " /* Comment after empty line */\n"
  1594. " b}",
  1595. format("S s = {\n"
  1596. " /* Some comment */\n"
  1597. " a,\n"
  1598. " \n"
  1599. " /* Comment after empty line */\n"
  1600. " b\n"
  1601. "}"));
  1602. verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
  1603. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1604. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1605. " 0x00, 0x00, 0x00, 0x00}; // comment\n");
  1606. }
  1607. TEST_F(FormatTest, IgnoresIf0Contents) {
  1608. EXPECT_EQ("#if 0\n"
  1609. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1610. "#endif\n"
  1611. "void f() {}",
  1612. format("#if 0\n"
  1613. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1614. "#endif\n"
  1615. "void f( ) { }"));
  1616. EXPECT_EQ("#if false\n"
  1617. "void f( ) { }\n"
  1618. "#endif\n"
  1619. "void g() {}\n",
  1620. format("#if false\n"
  1621. "void f( ) { }\n"
  1622. "#endif\n"
  1623. "void g( ) { }\n"));
  1624. EXPECT_EQ("enum E {\n"
  1625. " One,\n"
  1626. " Two,\n"
  1627. "#if 0\n"
  1628. "Three,\n"
  1629. " Four,\n"
  1630. "#endif\n"
  1631. " Five\n"
  1632. "};",
  1633. format("enum E {\n"
  1634. " One,Two,\n"
  1635. "#if 0\n"
  1636. "Three,\n"
  1637. " Four,\n"
  1638. "#endif\n"
  1639. " Five};"));
  1640. EXPECT_EQ("enum F {\n"
  1641. " One,\n"
  1642. "#if 1\n"
  1643. " Two,\n"
  1644. "#if 0\n"
  1645. "Three,\n"
  1646. " Four,\n"
  1647. "#endif\n"
  1648. " Five\n"
  1649. "#endif\n"
  1650. "};",
  1651. format("enum F {\n"
  1652. "One,\n"
  1653. "#if 1\n"
  1654. "Two,\n"
  1655. "#if 0\n"
  1656. "Three,\n"
  1657. " Four,\n"
  1658. "#endif\n"
  1659. "Five\n"
  1660. "#endif\n"
  1661. "};"));
  1662. EXPECT_EQ("enum G {\n"
  1663. " One,\n"
  1664. "#if 0\n"
  1665. "Two,\n"
  1666. "#else\n"
  1667. " Three,\n"
  1668. "#endif\n"
  1669. " Four\n"
  1670. "};",
  1671. format("enum G {\n"
  1672. "One,\n"
  1673. "#if 0\n"
  1674. "Two,\n"
  1675. "#else\n"
  1676. "Three,\n"
  1677. "#endif\n"
  1678. "Four\n"
  1679. "};"));
  1680. EXPECT_EQ("enum H {\n"
  1681. " One,\n"
  1682. "#if 0\n"
  1683. "#ifdef Q\n"
  1684. "Two,\n"
  1685. "#else\n"
  1686. "Three,\n"
  1687. "#endif\n"
  1688. "#endif\n"
  1689. " Four\n"
  1690. "};",
  1691. format("enum H {\n"
  1692. "One,\n"
  1693. "#if 0\n"
  1694. "#ifdef Q\n"
  1695. "Two,\n"
  1696. "#else\n"
  1697. "Three,\n"
  1698. "#endif\n"
  1699. "#endif\n"
  1700. "Four\n"
  1701. "};"));
  1702. EXPECT_EQ("enum I {\n"
  1703. " One,\n"
  1704. "#if /* test */ 0 || 1\n"
  1705. "Two,\n"
  1706. "Three,\n"
  1707. "#endif\n"
  1708. " Four\n"
  1709. "};",
  1710. format("enum I {\n"
  1711. "One,\n"
  1712. "#if /* test */ 0 || 1\n"
  1713. "Two,\n"
  1714. "Three,\n"
  1715. "#endif\n"
  1716. "Four\n"
  1717. "};"));
  1718. EXPECT_EQ("enum J {\n"
  1719. " One,\n"
  1720. "#if 0\n"
  1721. "#if 0\n"
  1722. "Two,\n"
  1723. "#else\n"
  1724. "Three,\n"
  1725. "#endif\n"
  1726. "Four,\n"
  1727. "#endif\n"
  1728. " Five\n"
  1729. "};",
  1730. format("enum J {\n"
  1731. "One,\n"
  1732. "#if 0\n"
  1733. "#if 0\n"
  1734. "Two,\n"
  1735. "#else\n"
  1736. "Three,\n"
  1737. "#endif\n"
  1738. "Four,\n"
  1739. "#endif\n"
  1740. "Five\n"
  1741. "};"));
  1742. }
  1743. //===----------------------------------------------------------------------===//
  1744. // Tests for classes, namespaces, etc.
  1745. //===----------------------------------------------------------------------===//
  1746. TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
  1747. verifyFormat("class A {};");
  1748. }
  1749. TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
  1750. verifyFormat("class A {\n"
  1751. "public:\n"
  1752. "public: // comment\n"
  1753. "protected:\n"
  1754. "private:\n"
  1755. " void f() {}\n"
  1756. "};");
  1757. verifyGoogleFormat("class A {\n"
  1758. " public:\n"
  1759. " protected:\n"
  1760. " private:\n"
  1761. " void f() {}\n"
  1762. "};");
  1763. verifyFormat("class A {\n"
  1764. "public slots:\n"
  1765. " void f() {}\n"
  1766. "public Q_SLOTS:\n"
  1767. " void f() {}\n"
  1768. "};");
  1769. }
  1770. TEST_F(FormatTest, SeparatesLogicalBlocks) {
  1771. EXPECT_EQ("class A {\n"
  1772. "public:\n"
  1773. " void f();\n"
  1774. "\n"
  1775. "private:\n"
  1776. " void g() {}\n"
  1777. " // test\n"
  1778. "protected:\n"
  1779. " int h;\n"
  1780. "};",
  1781. format("class A {\n"
  1782. "public:\n"
  1783. "void f();\n"
  1784. "private:\n"
  1785. "void g() {}\n"
  1786. "// test\n"
  1787. "protected:\n"
  1788. "int h;\n"
  1789. "};"));
  1790. EXPECT_EQ("class A {\n"
  1791. "protected:\n"
  1792. "public:\n"
  1793. " void f();\n"
  1794. "};",
  1795. format("class A {\n"
  1796. "protected:\n"
  1797. "\n"
  1798. "public:\n"
  1799. "\n"
  1800. " void f();\n"
  1801. "};"));
  1802. }
  1803. TEST_F(FormatTest, FormatsClasses) {
  1804. verifyFormat("class A : public B {};");
  1805. verifyFormat("class A : public ::B {};");
  1806. verifyFormat(
  1807. "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1808. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1809. verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
  1810. " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1811. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1812. verifyFormat(
  1813. "class A : public B, public C, public D, public E, public F {};");
  1814. verifyFormat("class AAAAAAAAAAAA : public B,\n"
  1815. " public C,\n"
  1816. " public D,\n"
  1817. " public E,\n"
  1818. " public F,\n"
  1819. " public G {};");
  1820. verifyFormat("class\n"
  1821. " ReallyReallyLongClassName {\n"
  1822. " int i;\n"
  1823. "};",
  1824. getLLVMStyleWithColumns(32));
  1825. verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
  1826. " aaaaaaaaaaaaaaaa> {};");
  1827. verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
  1828. " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
  1829. " aaaaaaaaaaaaaaaaaaaaaa> {};");
  1830. verifyFormat("template <class R, class C>\n"
  1831. "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
  1832. " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
  1833. verifyFormat("class ::A::B {};");
  1834. }
  1835. TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
  1836. verifyFormat("class A {\n} a, b;");
  1837. verifyFormat("struct A {\n} a, b;");
  1838. verifyFormat("union A {\n} a;");
  1839. }
  1840. TEST_F(FormatTest, FormatsEnum) {
  1841. verifyFormat("enum {\n"
  1842. " Zero,\n"
  1843. " One = 1,\n"
  1844. " Two = One + 1,\n"
  1845. " Three = (One + Two),\n"
  1846. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1847. " Five = (One, Two, Three, Four, 5)\n"
  1848. "};");
  1849. verifyGoogleFormat("enum {\n"
  1850. " Zero,\n"
  1851. " One = 1,\n"
  1852. " Two = One + 1,\n"
  1853. " Three = (One + Two),\n"
  1854. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1855. " Five = (One, Two, Three, Four, 5)\n"
  1856. "};");
  1857. verifyFormat("enum Enum {};");
  1858. verifyFormat("enum {};");
  1859. verifyFormat("enum X E {} d;");
  1860. verifyFormat("enum __attribute__((...)) E {} d;");
  1861. verifyFormat("enum __declspec__((...)) E {} d;");
  1862. verifyFormat("enum X f() {\n a();\n return 42;\n}");
  1863. verifyFormat("enum {\n"
  1864. " Bar = Foo<int, int>::value\n"
  1865. "};",
  1866. getLLVMStyleWithColumns(30));
  1867. verifyFormat("enum ShortEnum { A, B, C };");
  1868. verifyGoogleFormat("enum ShortEnum { A, B, C };");
  1869. EXPECT_EQ("enum KeepEmptyLines {\n"
  1870. " ONE,\n"
  1871. "\n"
  1872. " TWO,\n"
  1873. "\n"
  1874. " THREE\n"
  1875. "}",
  1876. format("enum KeepEmptyLines {\n"
  1877. " ONE,\n"
  1878. "\n"
  1879. " TWO,\n"
  1880. "\n"
  1881. "\n"
  1882. " THREE\n"
  1883. "}"));
  1884. verifyFormat("enum E { // comment\n"
  1885. " ONE,\n"
  1886. " TWO\n"
  1887. "};\n"
  1888. "int i;");
  1889. }
  1890. TEST_F(FormatTest, FormatsEnumsWithErrors) {
  1891. verifyFormat("enum Type {\n"
  1892. " One = 0; // These semicolons should be commas.\n"
  1893. " Two = 1;\n"
  1894. "};");
  1895. verifyFormat("namespace n {\n"
  1896. "enum Type {\n"
  1897. " One,\n"
  1898. " Two, // missing };\n"
  1899. " int i;\n"
  1900. "}\n"
  1901. "void g() {}");
  1902. }
  1903. TEST_F(FormatTest, FormatsEnumStruct) {
  1904. verifyFormat("enum struct {\n"
  1905. " Zero,\n"
  1906. " One = 1,\n"
  1907. " Two = One + 1,\n"
  1908. " Three = (One + Two),\n"
  1909. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1910. " Five = (One, Two, Three, Four, 5)\n"
  1911. "};");
  1912. verifyFormat("enum struct Enum {};");
  1913. verifyFormat("enum struct {};");
  1914. verifyFormat("enum struct X E {} d;");
  1915. verifyFormat("enum struct __attribute__((...)) E {} d;");
  1916. verifyFormat("enum struct __declspec__((...)) E {} d;");
  1917. verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
  1918. }
  1919. TEST_F(FormatTest, FormatsEnumClass) {
  1920. verifyFormat("enum class {\n"
  1921. " Zero,\n"
  1922. " One = 1,\n"
  1923. " Two = One + 1,\n"
  1924. " Three = (One + Two),\n"
  1925. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1926. " Five = (One, Two, Three, Four, 5)\n"
  1927. "};");
  1928. verifyFormat("enum class Enum {};");
  1929. verifyFormat("enum class {};");
  1930. verifyFormat("enum class X E {} d;");
  1931. verifyFormat("enum class __attribute__((...)) E {} d;");
  1932. verifyFormat("enum class __declspec__((...)) E {} d;");
  1933. verifyFormat("enum class X f() {\n a();\n return 42;\n}");
  1934. }
  1935. TEST_F(FormatTest, FormatsEnumTypes) {
  1936. verifyFormat("enum X : int {\n"
  1937. " A, // Force multiple lines.\n"
  1938. " B\n"
  1939. "};");
  1940. verifyFormat("enum X : int { A, B };");
  1941. verifyFormat("enum X : std::uint32_t { A, B };");
  1942. }
  1943. TEST_F(FormatTest, FormatsNSEnums) {
  1944. verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
  1945. verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
  1946. " // Information about someDecentlyLongValue.\n"
  1947. " someDecentlyLongValue,\n"
  1948. " // Information about anotherDecentlyLongValue.\n"
  1949. " anotherDecentlyLongValue,\n"
  1950. " // Information about aThirdDecentlyLongValue.\n"
  1951. " aThirdDecentlyLongValue\n"
  1952. "};");
  1953. verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
  1954. " a = 1,\n"
  1955. " b = 2,\n"
  1956. " c = 3,\n"
  1957. "};");
  1958. verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
  1959. " a = 1,\n"
  1960. " b = 2,\n"
  1961. " c = 3,\n"
  1962. "};");
  1963. verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
  1964. " a = 1,\n"
  1965. " b = 2,\n"
  1966. " c = 3,\n"
  1967. "};");
  1968. }
  1969. TEST_F(FormatTest, FormatsBitfields) {
  1970. verifyFormat("struct Bitfields {\n"
  1971. " unsigned sClass : 8;\n"
  1972. " unsigned ValueKind : 2;\n"
  1973. "};");
  1974. verifyFormat("struct A {\n"
  1975. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
  1976. " bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
  1977. "};");
  1978. }
  1979. TEST_F(FormatTest, FormatsNamespaces) {
  1980. verifyFormat("namespace some_namespace {\n"
  1981. "class A {};\n"
  1982. "void f() { f(); }\n"
  1983. "}");
  1984. verifyFormat("namespace {\n"
  1985. "class A {};\n"
  1986. "void f() { f(); }\n"
  1987. "}");
  1988. verifyFormat("inline namespace X {\n"
  1989. "class A {};\n"
  1990. "void f() { f(); }\n"
  1991. "}");
  1992. verifyFormat("using namespace some_namespace;\n"
  1993. "class A {};\n"
  1994. "void f() { f(); }");
  1995. // This code is more common than we thought; if we
  1996. // layout this correctly the semicolon will go into
  1997. // its own line, which is undesirable.
  1998. verifyFormat("namespace {};");
  1999. verifyFormat("namespace {\n"
  2000. "class A {};\n"
  2001. "};");
  2002. verifyFormat("namespace {\n"
  2003. "int SomeVariable = 0; // comment\n"
  2004. "} // namespace");
  2005. EXPECT_EQ("#ifndef HEADER_GUARD\n"
  2006. "#define HEADER_GUARD\n"
  2007. "namespace my_namespace {\n"
  2008. "int i;\n"
  2009. "} // my_namespace\n"
  2010. "#endif // HEADER_GUARD",
  2011. format("#ifndef HEADER_GUARD\n"
  2012. " #define HEADER_GUARD\n"
  2013. " namespace my_namespace {\n"
  2014. "int i;\n"
  2015. "} // my_namespace\n"
  2016. "#endif // HEADER_GUARD"));
  2017. FormatStyle Style = getLLVMStyle();
  2018. Style.NamespaceIndentation = FormatStyle::NI_All;
  2019. EXPECT_EQ("namespace out {\n"
  2020. " int i;\n"
  2021. " namespace in {\n"
  2022. " int i;\n"
  2023. " } // namespace\n"
  2024. "} // namespace",
  2025. format("namespace out {\n"
  2026. "int i;\n"
  2027. "namespace in {\n"
  2028. "int i;\n"
  2029. "} // namespace\n"
  2030. "} // namespace",
  2031. Style));
  2032. Style.NamespaceIndentation = FormatStyle::NI_Inner;
  2033. EXPECT_EQ("namespace out {\n"
  2034. "int i;\n"
  2035. "namespace in {\n"
  2036. " int i;\n"
  2037. "} // namespace\n"
  2038. "} // namespace",
  2039. format("namespace out {\n"
  2040. "int i;\n"
  2041. "namespace in {\n"
  2042. "int i;\n"
  2043. "} // namespace\n"
  2044. "} // namespace",
  2045. Style));
  2046. }
  2047. TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
  2048. TEST_F(FormatTest, FormatsInlineASM) {
  2049. verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
  2050. verifyFormat("asm(\"nop\" ::: \"memory\");");
  2051. verifyFormat(
  2052. "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
  2053. " \"cpuid\\n\\t\"\n"
  2054. " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
  2055. " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
  2056. " : \"a\"(value));");
  2057. EXPECT_EQ(
  2058. "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
  2059. " __asm {\n"
  2060. " mov edx,[that] // vtable in edx\n"
  2061. " mov eax,methodIndex\n"
  2062. " call [edx][eax*4] // stdcall\n"
  2063. " }\n"
  2064. "}",
  2065. format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
  2066. " __asm {\n"
  2067. " mov edx,[that] // vtable in edx\n"
  2068. " mov eax,methodIndex\n"
  2069. " call [edx][eax*4] // stdcall\n"
  2070. " }\n"
  2071. "}"));
  2072. verifyFormat("void function() {\n"
  2073. " // comment\n"
  2074. " asm(\"\");\n"
  2075. "}");
  2076. }
  2077. TEST_F(FormatTest, FormatTryCatch) {
  2078. verifyFormat("try {\n"
  2079. " throw a * b;\n"
  2080. "} catch (int a) {\n"
  2081. " // Do nothing.\n"
  2082. "} catch (...) {\n"
  2083. " exit(42);\n"
  2084. "}");
  2085. // Function-level try statements.
  2086. verifyFormat("int f() try { return 4; } catch (...) {\n"
  2087. " return 5;\n"
  2088. "}");
  2089. verifyFormat("class A {\n"
  2090. " int a;\n"
  2091. " A() try : a(0) {\n"
  2092. " } catch (...) {\n"
  2093. " throw;\n"
  2094. " }\n"
  2095. "};\n");
  2096. // Incomplete try-catch blocks.
  2097. verifyFormat("try {} catch (");
  2098. }
  2099. TEST_F(FormatTest, IncompleteTryCatchBlocks) {
  2100. verifyFormat("try {\n"
  2101. " f();\n"
  2102. "} catch {\n"
  2103. " g();\n"
  2104. "}");
  2105. verifyFormat("try {\n"
  2106. " f();\n"
  2107. "} catch (A a) MACRO(x) {\n"
  2108. " g();\n"
  2109. "} catch (B b) MACRO(x) {\n"
  2110. " g();\n"
  2111. "}");
  2112. }
  2113. TEST_F(FormatTest, FormatTryCatchBraceStyles) {
  2114. FormatStyle Style = getLLVMStyle();
  2115. Style.BreakBeforeBraces = FormatStyle::BS_Attach;
  2116. verifyFormat("try {\n"
  2117. " // something\n"
  2118. "} catch (...) {\n"
  2119. " // something\n"
  2120. "}",
  2121. Style);
  2122. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  2123. verifyFormat("try {\n"
  2124. " // something\n"
  2125. "}\n"
  2126. "catch (...) {\n"
  2127. " // something\n"
  2128. "}",
  2129. Style);
  2130. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  2131. verifyFormat("try\n"
  2132. "{\n"
  2133. " // something\n"
  2134. "}\n"
  2135. "catch (...)\n"
  2136. "{\n"
  2137. " // something\n"
  2138. "}",
  2139. Style);
  2140. Style.BreakBeforeBraces = FormatStyle::BS_GNU;
  2141. verifyFormat("try\n"
  2142. " {\n"
  2143. " // something\n"
  2144. " }\n"
  2145. "catch (...)\n"
  2146. " {\n"
  2147. " // something\n"
  2148. " }",
  2149. Style);
  2150. }
  2151. TEST_F(FormatTest, FormatObjCTryCatch) {
  2152. verifyFormat("@try {\n"
  2153. " f();\n"
  2154. "}\n"
  2155. "@catch (NSException e) {\n"
  2156. " @throw;\n"
  2157. "}\n"
  2158. "@finally {\n"
  2159. " exit(42);\n"
  2160. "}");
  2161. }
  2162. TEST_F(FormatTest, StaticInitializers) {
  2163. verifyFormat("static SomeClass SC = {1, 'a'};");
  2164. verifyFormat(
  2165. "static SomeClass WithALoooooooooooooooooooongName = {\n"
  2166. " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
  2167. // Here, everything other than the "}" would fit on a line.
  2168. verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
  2169. " 10000000000000000000000000};");
  2170. EXPECT_EQ("S s = {a,\n"
  2171. "\n"
  2172. " b};",
  2173. format("S s = {\n"
  2174. " a,\n"
  2175. "\n"
  2176. " b\n"
  2177. "};"));
  2178. // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
  2179. // line. However, the formatting looks a bit off and this probably doesn't
  2180. // happen often in practice.
  2181. verifyFormat("static int Variable[1] = {\n"
  2182. " {1000000000000000000000000000000000000}};",
  2183. getLLVMStyleWithColumns(40));
  2184. }
  2185. TEST_F(FormatTest, DesignatedInitializers) {
  2186. verifyFormat("const struct A a = {.a = 1, .b = 2};");
  2187. verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
  2188. " .bbbbbbbbbb = 2,\n"
  2189. " .cccccccccc = 3,\n"
  2190. " .dddddddddd = 4,\n"
  2191. " .eeeeeeeeee = 5};");
  2192. verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
  2193. " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
  2194. " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
  2195. " .ccccccccccccccccccccccccccc = 3,\n"
  2196. " .ddddddddddddddddddddddddddd = 4,\n"
  2197. " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
  2198. verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
  2199. }
  2200. TEST_F(FormatTest, NestedStaticInitializers) {
  2201. verifyFormat("static A x = {{{}}};\n");
  2202. verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
  2203. " {init1, init2, init3, init4}}};",
  2204. getLLVMStyleWithColumns(50));
  2205. verifyFormat("somes Status::global_reps[3] = {\n"
  2206. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  2207. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  2208. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
  2209. getLLVMStyleWithColumns(60));
  2210. verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
  2211. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  2212. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  2213. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
  2214. verifyFormat(
  2215. "CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
  2216. " {rect.fRight - rect.fLeft, rect.fBottom - rect.fTop}};");
  2217. verifyFormat(
  2218. "SomeArrayOfSomeType a = {\n"
  2219. " {{1, 2, 3},\n"
  2220. " {1, 2, 3},\n"
  2221. " {111111111111111111111111111111, 222222222222222222222222222222,\n"
  2222. " 333333333333333333333333333333},\n"
  2223. " {1, 2, 3},\n"
  2224. " {1, 2, 3}}};");
  2225. verifyFormat(
  2226. "SomeArrayOfSomeType a = {\n"
  2227. " {{1, 2, 3}},\n"
  2228. " {{1, 2, 3}},\n"
  2229. " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
  2230. " 333333333333333333333333333333}},\n"
  2231. " {{1, 2, 3}},\n"
  2232. " {{1, 2, 3}}};");
  2233. verifyFormat(
  2234. "struct {\n"
  2235. " unsigned bit;\n"
  2236. " const char *const name;\n"
  2237. "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
  2238. " {kOsWin, \"Windows\"},\n"
  2239. " {kOsLinux, \"Linux\"},\n"
  2240. " {kOsCrOS, \"Chrome OS\"}};");
  2241. verifyFormat(
  2242. "struct {\n"
  2243. " unsigned bit;\n"
  2244. " const char *const name;\n"
  2245. "} kBitsToOs[] = {\n"
  2246. " {kOsMac, \"Mac\"},\n"
  2247. " {kOsWin, \"Windows\"},\n"
  2248. " {kOsLinux, \"Linux\"},\n"
  2249. " {kOsCrOS, \"Chrome OS\"},\n"
  2250. "};");
  2251. }
  2252. TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
  2253. verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2254. " \\\n"
  2255. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
  2256. }
  2257. TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
  2258. verifyFormat("virtual void write(ELFWriter *writerrr,\n"
  2259. " OwningPtr<FileOutputBuffer> &buffer) = 0;");
  2260. }
  2261. TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
  2262. verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
  2263. getLLVMStyleWithColumns(40));
  2264. verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  2265. getLLVMStyleWithColumns(40));
  2266. EXPECT_EQ("#define Q \\\n"
  2267. " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n"
  2268. " \"aaaaaaaa.cpp\"",
  2269. format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  2270. getLLVMStyleWithColumns(40)));
  2271. }
  2272. TEST_F(FormatTest, UnderstandsLinePPDirective) {
  2273. EXPECT_EQ("# 123 \"A string literal\"",
  2274. format(" # 123 \"A string literal\""));
  2275. }
  2276. TEST_F(FormatTest, LayoutUnknownPPDirective) {
  2277. EXPECT_EQ("#;", format("#;"));
  2278. verifyFormat("#\n;\n;\n;");
  2279. }
  2280. TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
  2281. EXPECT_EQ("#line 42 \"test\"\n",
  2282. format("# \\\n line \\\n 42 \\\n \"test\"\n"));
  2283. EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n",
  2284. getLLVMStyleWithColumns(12)));
  2285. }
  2286. TEST_F(FormatTest, EndOfFileEndsPPDirective) {
  2287. EXPECT_EQ("#line 42 \"test\"",
  2288. format("# \\\n line \\\n 42 \\\n \"test\""));
  2289. EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B"));
  2290. }
  2291. TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
  2292. verifyFormat("#define A \\x20");
  2293. verifyFormat("#define A \\ x20");
  2294. EXPECT_EQ("#define A \\ x20", format("#define A \\ x20"));
  2295. verifyFormat("#define A ''");
  2296. verifyFormat("#define A ''qqq");
  2297. verifyFormat("#define A `qqq");
  2298. verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
  2299. EXPECT_EQ("const char *c = STRINGIFY(\n"
  2300. "\\na : b);",
  2301. format("const char * c = STRINGIFY(\n"
  2302. "\\na : b);"));
  2303. }
  2304. TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
  2305. verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
  2306. verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
  2307. verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
  2308. // FIXME: We never break before the macro name.
  2309. verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
  2310. verifyFormat("#define A A\n#define A A");
  2311. verifyFormat("#define A(X) A\n#define A A");
  2312. verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
  2313. verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
  2314. }
  2315. TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
  2316. EXPECT_EQ("// somecomment\n"
  2317. "#include \"a.h\"\n"
  2318. "#define A( \\\n"
  2319. " A, B)\n"
  2320. "#include \"b.h\"\n"
  2321. "// somecomment\n",
  2322. format(" // somecomment\n"
  2323. " #include \"a.h\"\n"
  2324. "#define A(A,\\\n"
  2325. " B)\n"
  2326. " #include \"b.h\"\n"
  2327. " // somecomment\n",
  2328. getLLVMStyleWithColumns(13)));
  2329. }
  2330. TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
  2331. TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
  2332. EXPECT_EQ("#define A \\\n"
  2333. " c; \\\n"
  2334. " e;\n"
  2335. "f;",
  2336. format("#define A c; e;\n"
  2337. "f;",
  2338. getLLVMStyleWithColumns(14)));
  2339. }
  2340. TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
  2341. TEST_F(FormatTest, AlwaysFormatsEntireMacroDefinitions) {
  2342. EXPECT_EQ("int i;\n"
  2343. "#define A \\\n"
  2344. " int i; \\\n"
  2345. " int j\n"
  2346. "int k;",
  2347. format("int i;\n"
  2348. "#define A \\\n"
  2349. " int i ; \\\n"
  2350. " int j\n"
  2351. "int k;",
  2352. 8, 0, getGoogleStyle())); // 8: position of "#define".
  2353. EXPECT_EQ("int i;\n"
  2354. "#define A \\\n"
  2355. " int i; \\\n"
  2356. " int j\n"
  2357. "int k;",
  2358. format("int i;\n"
  2359. "#define A \\\n"
  2360. " int i ; \\\n"
  2361. " int j\n"
  2362. "int k;",
  2363. 45, 0, getGoogleStyle())); // 45: position of "j".
  2364. }
  2365. TEST_F(FormatTest, MacroDefinitionInsideStatement) {
  2366. EXPECT_EQ("int x,\n"
  2367. "#define A\n"
  2368. " y;",
  2369. format("int x,\n#define A\ny;"));
  2370. }
  2371. TEST_F(FormatTest, HashInMacroDefinition) {
  2372. EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
  2373. verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
  2374. verifyFormat("#define A \\\n"
  2375. " { \\\n"
  2376. " f(#c); \\\n"
  2377. " }",
  2378. getLLVMStyleWithColumns(11));
  2379. verifyFormat("#define A(X) \\\n"
  2380. " void function##X()",
  2381. getLLVMStyleWithColumns(22));
  2382. verifyFormat("#define A(a, b, c) \\\n"
  2383. " void a##b##c()",
  2384. getLLVMStyleWithColumns(22));
  2385. verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
  2386. }
  2387. TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
  2388. EXPECT_EQ("#define A (x)", format("#define A (x)"));
  2389. EXPECT_EQ("#define A(x)", format("#define A(x)"));
  2390. }
  2391. TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
  2392. EXPECT_EQ("#define A b;", format("#define A \\\n"
  2393. " \\\n"
  2394. " b;",
  2395. getLLVMStyleWithColumns(25)));
  2396. EXPECT_EQ("#define A \\\n"
  2397. " \\\n"
  2398. " a; \\\n"
  2399. " b;",
  2400. format("#define A \\\n"
  2401. " \\\n"
  2402. " a; \\\n"
  2403. " b;",
  2404. getLLVMStyleWithColumns(11)));
  2405. EXPECT_EQ("#define A \\\n"
  2406. " a; \\\n"
  2407. " \\\n"
  2408. " b;",
  2409. format("#define A \\\n"
  2410. " a; \\\n"
  2411. " \\\n"
  2412. " b;",
  2413. getLLVMStyleWithColumns(11)));
  2414. }
  2415. TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
  2416. verifyFormat("#define A :");
  2417. verifyFormat("#define SOMECASES \\\n"
  2418. " case 1: \\\n"
  2419. " case 2\n",
  2420. getLLVMStyleWithColumns(20));
  2421. verifyFormat("#define A template <typename T>");
  2422. verifyFormat("#define STR(x) #x\n"
  2423. "f(STR(this_is_a_string_literal{));");
  2424. verifyFormat("#pragma omp threadprivate( \\\n"
  2425. " y)), // expected-warning",
  2426. getLLVMStyleWithColumns(28));
  2427. verifyFormat("#d, = };");
  2428. verifyFormat("#if \"a");
  2429. verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
  2430. verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
  2431. verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
  2432. verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}");
  2433. }
  2434. TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
  2435. verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
  2436. EXPECT_EQ("class A : public QObject {\n"
  2437. " Q_OBJECT\n"
  2438. "\n"
  2439. " A() {}\n"
  2440. "};",
  2441. format("class A : public QObject {\n"
  2442. " Q_OBJECT\n"
  2443. "\n"
  2444. " A() {\n}\n"
  2445. "} ;"));
  2446. EXPECT_EQ("SOME_MACRO\n"
  2447. "namespace {\n"
  2448. "void f();\n"
  2449. "}",
  2450. format("SOME_MACRO\n"
  2451. " namespace {\n"
  2452. "void f( );\n"
  2453. "}"));
  2454. // Only if the identifier contains at least 5 characters.
  2455. EXPECT_EQ("HTTP f();",
  2456. format("HTTP\nf();"));
  2457. EXPECT_EQ("MACRO\nf();",
  2458. format("MACRO\nf();"));
  2459. // Only if everything is upper case.
  2460. EXPECT_EQ("class A : public QObject {\n"
  2461. " Q_Object A() {}\n"
  2462. "};",
  2463. format("class A : public QObject {\n"
  2464. " Q_Object\n"
  2465. " A() {\n}\n"
  2466. "} ;"));
  2467. // Only if the next line can actually start an unwrapped line.
  2468. EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
  2469. format("SOME_WEIRD_LOG_MACRO\n"
  2470. "<< SomeThing;"));
  2471. verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
  2472. "(n, buffers))\n", getChromiumStyle(FormatStyle::LK_Cpp));
  2473. }
  2474. TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
  2475. EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  2476. "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  2477. "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  2478. "class X {};\n"
  2479. "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  2480. "int *createScopDetectionPass() { return 0; }",
  2481. format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  2482. " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  2483. " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  2484. " class X {};\n"
  2485. " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  2486. " int *createScopDetectionPass() { return 0; }"));
  2487. // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
  2488. // braces, so that inner block is indented one level more.
  2489. EXPECT_EQ("int q() {\n"
  2490. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  2491. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  2492. " IPC_END_MESSAGE_MAP()\n"
  2493. "}",
  2494. format("int q() {\n"
  2495. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  2496. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  2497. " IPC_END_MESSAGE_MAP()\n"
  2498. "}"));
  2499. // Same inside macros.
  2500. EXPECT_EQ("#define LIST(L) \\\n"
  2501. " L(A) \\\n"
  2502. " L(B) \\\n"
  2503. " L(C)",
  2504. format("#define LIST(L) \\\n"
  2505. " L(A) \\\n"
  2506. " L(B) \\\n"
  2507. " L(C)",
  2508. getGoogleStyle()));
  2509. // These must not be recognized as macros.
  2510. EXPECT_EQ("int q() {\n"
  2511. " f(x);\n"
  2512. " f(x) {}\n"
  2513. " f(x)->g();\n"
  2514. " f(x)->*g();\n"
  2515. " f(x).g();\n"
  2516. " f(x) = x;\n"
  2517. " f(x) += x;\n"
  2518. " f(x) -= x;\n"
  2519. " f(x) *= x;\n"
  2520. " f(x) /= x;\n"
  2521. " f(x) %= x;\n"
  2522. " f(x) &= x;\n"
  2523. " f(x) |= x;\n"
  2524. " f(x) ^= x;\n"
  2525. " f(x) >>= x;\n"
  2526. " f(x) <<= x;\n"
  2527. " f(x)[y].z();\n"
  2528. " LOG(INFO) << x;\n"
  2529. " ifstream(x) >> x;\n"
  2530. "}\n",
  2531. format("int q() {\n"
  2532. " f(x)\n;\n"
  2533. " f(x)\n {}\n"
  2534. " f(x)\n->g();\n"
  2535. " f(x)\n->*g();\n"
  2536. " f(x)\n.g();\n"
  2537. " f(x)\n = x;\n"
  2538. " f(x)\n += x;\n"
  2539. " f(x)\n -= x;\n"
  2540. " f(x)\n *= x;\n"
  2541. " f(x)\n /= x;\n"
  2542. " f(x)\n %= x;\n"
  2543. " f(x)\n &= x;\n"
  2544. " f(x)\n |= x;\n"
  2545. " f(x)\n ^= x;\n"
  2546. " f(x)\n >>= x;\n"
  2547. " f(x)\n <<= x;\n"
  2548. " f(x)\n[y].z();\n"
  2549. " LOG(INFO)\n << x;\n"
  2550. " ifstream(x)\n >> x;\n"
  2551. "}\n"));
  2552. EXPECT_EQ("int q() {\n"
  2553. " F(x)\n"
  2554. " if (1) {\n"
  2555. " }\n"
  2556. " F(x)\n"
  2557. " while (1) {\n"
  2558. " }\n"
  2559. " F(x)\n"
  2560. " G(x);\n"
  2561. " F(x)\n"
  2562. " try {\n"
  2563. " Q();\n"
  2564. " } catch (...) {\n"
  2565. " }\n"
  2566. "}\n",
  2567. format("int q() {\n"
  2568. "F(x)\n"
  2569. "if (1) {}\n"
  2570. "F(x)\n"
  2571. "while (1) {}\n"
  2572. "F(x)\n"
  2573. "G(x);\n"
  2574. "F(x)\n"
  2575. "try { Q(); } catch (...) {}\n"
  2576. "}\n"));
  2577. EXPECT_EQ("class A {\n"
  2578. " A() : t(0) {}\n"
  2579. " A(int i) noexcept() : {}\n"
  2580. " A(X x)\n" // FIXME: function-level try blocks are broken.
  2581. " try : t(0) {\n"
  2582. " } catch (...) {\n"
  2583. " }\n"
  2584. "};",
  2585. format("class A {\n"
  2586. " A()\n : t(0) {}\n"
  2587. " A(int i)\n noexcept() : {}\n"
  2588. " A(X x)\n"
  2589. " try : t(0) {} catch (...) {}\n"
  2590. "};"));
  2591. EXPECT_EQ(
  2592. "class SomeClass {\n"
  2593. "public:\n"
  2594. " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2595. "};",
  2596. format("class SomeClass {\n"
  2597. "public:\n"
  2598. " SomeClass()\n"
  2599. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2600. "};"));
  2601. EXPECT_EQ(
  2602. "class SomeClass {\n"
  2603. "public:\n"
  2604. " SomeClass()\n"
  2605. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2606. "};",
  2607. format("class SomeClass {\n"
  2608. "public:\n"
  2609. " SomeClass()\n"
  2610. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2611. "};", getLLVMStyleWithColumns(40)));
  2612. }
  2613. TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
  2614. verifyFormat("#define A \\\n"
  2615. " f({ \\\n"
  2616. " g(); \\\n"
  2617. " });", getLLVMStyleWithColumns(11));
  2618. }
  2619. TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
  2620. EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
  2621. }
  2622. TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
  2623. verifyFormat("{\n { a #c; }\n}");
  2624. }
  2625. TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
  2626. EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
  2627. format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
  2628. EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
  2629. format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
  2630. }
  2631. TEST_F(FormatTest, EscapedNewlineAtStartOfToken) {
  2632. EXPECT_EQ(
  2633. "#define A \\\n int i; \\\n int j;",
  2634. format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
  2635. EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
  2636. }
  2637. TEST_F(FormatTest, NoEscapedNewlineHandlingInBlockComments) {
  2638. EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/"));
  2639. }
  2640. TEST_F(FormatTest, DontCrashOnBlockComments) {
  2641. EXPECT_EQ(
  2642. "int xxxxxxxxx; /* "
  2643. "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
  2644. "zzzzzz\n"
  2645. "0*/",
  2646. format("int xxxxxxxxx; /* "
  2647. "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
  2648. "0*/"));
  2649. }
  2650. TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
  2651. verifyFormat("#define A \\\n"
  2652. " int v( \\\n"
  2653. " a); \\\n"
  2654. " int i;",
  2655. getLLVMStyleWithColumns(11));
  2656. }
  2657. TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
  2658. EXPECT_EQ(
  2659. "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2660. " \\\n"
  2661. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2662. "\n"
  2663. "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2664. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
  2665. format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2666. "\\\n"
  2667. "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2668. " \n"
  2669. " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2670. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
  2671. }
  2672. TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
  2673. EXPECT_EQ("int\n"
  2674. "#define A\n"
  2675. " a;",
  2676. format("int\n#define A\na;"));
  2677. verifyFormat("functionCallTo(\n"
  2678. " someOtherFunction(\n"
  2679. " withSomeParameters, whichInSequence,\n"
  2680. " areLongerThanALine(andAnotherCall,\n"
  2681. "#define A B\n"
  2682. " withMoreParamters,\n"
  2683. " whichStronglyInfluenceTheLayout),\n"
  2684. " andMoreParameters),\n"
  2685. " trailing);",
  2686. getLLVMStyleWithColumns(69));
  2687. verifyFormat("Foo::Foo()\n"
  2688. "#ifdef BAR\n"
  2689. " : baz(0)\n"
  2690. "#endif\n"
  2691. "{\n"
  2692. "}");
  2693. verifyFormat("void f() {\n"
  2694. " if (true)\n"
  2695. "#ifdef A\n"
  2696. " f(42);\n"
  2697. " x();\n"
  2698. "#else\n"
  2699. " g();\n"
  2700. " x();\n"
  2701. "#endif\n"
  2702. "}");
  2703. verifyFormat("void f(param1, param2,\n"
  2704. " param3,\n"
  2705. "#ifdef A\n"
  2706. " param4(param5,\n"
  2707. "#ifdef A1\n"
  2708. " param6,\n"
  2709. "#ifdef A2\n"
  2710. " param7),\n"
  2711. "#else\n"
  2712. " param8),\n"
  2713. " param9,\n"
  2714. "#endif\n"
  2715. " param10,\n"
  2716. "#endif\n"
  2717. " param11)\n"
  2718. "#else\n"
  2719. " param12)\n"
  2720. "#endif\n"
  2721. "{\n"
  2722. " x();\n"
  2723. "}",
  2724. getLLVMStyleWithColumns(28));
  2725. verifyFormat("#if 1\n"
  2726. "int i;");
  2727. verifyFormat(
  2728. "#if 1\n"
  2729. "#endif\n"
  2730. "#if 1\n"
  2731. "#else\n"
  2732. "#endif\n");
  2733. verifyFormat("DEBUG({\n"
  2734. " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2735. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  2736. "});\n"
  2737. "#if a\n"
  2738. "#else\n"
  2739. "#endif");
  2740. verifyFormat("void f(\n"
  2741. "#if A\n"
  2742. " );\n"
  2743. "#else\n"
  2744. "#endif");
  2745. }
  2746. TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
  2747. verifyFormat("#endif\n"
  2748. "#if B");
  2749. }
  2750. TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
  2751. FormatStyle SingleLine = getLLVMStyle();
  2752. SingleLine.AllowShortIfStatementsOnASingleLine = true;
  2753. verifyFormat(
  2754. "#if 0\n"
  2755. "#elif 1\n"
  2756. "#endif\n"
  2757. "void foo() {\n"
  2758. " if (test) foo2();\n"
  2759. "}",
  2760. SingleLine);
  2761. }
  2762. TEST_F(FormatTest, LayoutBlockInsideParens) {
  2763. EXPECT_EQ("functionCall({ int i; });", format(" functionCall ( {int i;} );"));
  2764. EXPECT_EQ("functionCall({\n"
  2765. " int i;\n"
  2766. " int j;\n"
  2767. "});",
  2768. format(" functionCall ( {int i;int j;} );"));
  2769. EXPECT_EQ("functionCall({\n"
  2770. " int i;\n"
  2771. " int j;\n"
  2772. "}, aaaa, bbbb, cccc);",
  2773. format(" functionCall ( {int i;int j;}, aaaa, bbbb, cccc);"));
  2774. EXPECT_EQ("functionCall(\n"
  2775. " {\n"
  2776. " int i;\n"
  2777. " int j;\n"
  2778. " },\n"
  2779. " aaaa, bbbb, // comment\n"
  2780. " cccc);",
  2781. format(" functionCall ( {int i;int j;}, aaaa, bbbb, // comment\n"
  2782. "cccc);"));
  2783. EXPECT_EQ("functionCall(aaaa, bbbb, { int i; });",
  2784. format(" functionCall (aaaa, bbbb, {int i;});"));
  2785. EXPECT_EQ("functionCall(aaaa, bbbb, {\n"
  2786. " int i;\n"
  2787. " int j;\n"
  2788. "});",
  2789. format(" functionCall (aaaa, bbbb, {int i;int j;});"));
  2790. EXPECT_EQ("functionCall(aaaa, bbbb, { int i; });",
  2791. format(" functionCall (aaaa, bbbb, {int i;});"));
  2792. verifyFormat(
  2793. "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
  2794. " {\n"
  2795. " int i; // break\n"
  2796. " },\n"
  2797. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  2798. " ccccccccccccccccc));");
  2799. verifyFormat("DEBUG({\n"
  2800. " if (a)\n"
  2801. " f();\n"
  2802. "});");
  2803. }
  2804. TEST_F(FormatTest, LayoutBlockInsideStatement) {
  2805. EXPECT_EQ("SOME_MACRO { int i; }\n"
  2806. "int i;",
  2807. format(" SOME_MACRO {int i;} int i;"));
  2808. }
  2809. TEST_F(FormatTest, LayoutNestedBlocks) {
  2810. verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
  2811. " struct s {\n"
  2812. " int i;\n"
  2813. " };\n"
  2814. " s kBitsToOs[] = {{10}};\n"
  2815. " for (int i = 0; i < 10; ++i)\n"
  2816. " return;\n"
  2817. "}");
  2818. verifyFormat("call(parameter, {\n"
  2819. " something();\n"
  2820. " // Comment using all columns.\n"
  2821. " somethingelse();\n"
  2822. "});",
  2823. getLLVMStyleWithColumns(40));
  2824. verifyFormat("DEBUG( //\n"
  2825. " { f(); }, a);");
  2826. verifyFormat("DEBUG( //\n"
  2827. " {\n"
  2828. " f(); //\n"
  2829. " },\n"
  2830. " a);");
  2831. EXPECT_EQ("call(parameter, {\n"
  2832. " something();\n"
  2833. " // Comment too\n"
  2834. " // looooooooooong.\n"
  2835. " somethingElse();\n"
  2836. "});",
  2837. format("call(parameter, {\n"
  2838. " something();\n"
  2839. " // Comment too looooooooooong.\n"
  2840. " somethingElse();\n"
  2841. "});",
  2842. getLLVMStyleWithColumns(29)));
  2843. EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });"));
  2844. EXPECT_EQ("DEBUG({ // comment\n"
  2845. " int i;\n"
  2846. "});",
  2847. format("DEBUG({ // comment\n"
  2848. "int i;\n"
  2849. "});"));
  2850. EXPECT_EQ("DEBUG({\n"
  2851. " int i;\n"
  2852. "\n"
  2853. " // comment\n"
  2854. " int j;\n"
  2855. "});",
  2856. format("DEBUG({\n"
  2857. " int i;\n"
  2858. "\n"
  2859. " // comment\n"
  2860. " int j;\n"
  2861. "});"));
  2862. verifyFormat("DEBUG({\n"
  2863. " if (a)\n"
  2864. " return;\n"
  2865. "});");
  2866. verifyGoogleFormat("DEBUG({\n"
  2867. " if (a) return;\n"
  2868. "});");
  2869. FormatStyle Style = getGoogleStyle();
  2870. Style.ColumnLimit = 45;
  2871. verifyFormat("Debug(aaaaa, {\n"
  2872. " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
  2873. "}, a);",
  2874. Style);
  2875. }
  2876. TEST_F(FormatTest, IndividualStatementsOfNestedBlocks) {
  2877. EXPECT_EQ("DEBUG({\n"
  2878. " int i;\n"
  2879. " int j;\n"
  2880. "});",
  2881. format("DEBUG( {\n"
  2882. " int i;\n"
  2883. " int j;\n"
  2884. "} ) ;",
  2885. 20, 1, getLLVMStyle()));
  2886. EXPECT_EQ("DEBUG( {\n"
  2887. " int i;\n"
  2888. " int j;\n"
  2889. "} ) ;",
  2890. format("DEBUG( {\n"
  2891. " int i;\n"
  2892. " int j;\n"
  2893. "} ) ;",
  2894. 41, 1, getLLVMStyle()));
  2895. EXPECT_EQ("DEBUG( {\n"
  2896. " int i;\n"
  2897. " int j;\n"
  2898. "} ) ;",
  2899. format("DEBUG( {\n"
  2900. " int i;\n"
  2901. " int j;\n"
  2902. "} ) ;",
  2903. 41, 1, getLLVMStyle()));
  2904. EXPECT_EQ("DEBUG({\n"
  2905. " int i;\n"
  2906. " int j;\n"
  2907. "});",
  2908. format("DEBUG( {\n"
  2909. " int i;\n"
  2910. " int j;\n"
  2911. "} ) ;",
  2912. 20, 1, getLLVMStyle()));
  2913. EXPECT_EQ("Debug({\n"
  2914. " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2915. " return;\n"
  2916. " },\n"
  2917. " a);",
  2918. format("Debug({\n"
  2919. " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2920. " return;\n"
  2921. " },\n"
  2922. " a);",
  2923. 50, 1, getLLVMStyle()));
  2924. EXPECT_EQ("DEBUG({\n"
  2925. " DEBUG({\n"
  2926. " int a;\n"
  2927. " int b;\n"
  2928. " }) ;\n"
  2929. "});",
  2930. format("DEBUG({\n"
  2931. " DEBUG({\n"
  2932. " int a;\n"
  2933. " int b;\n" // Format this line only.
  2934. " }) ;\n" // Don't touch this line.
  2935. "});",
  2936. 35, 0, getLLVMStyle()));
  2937. EXPECT_EQ("DEBUG({\n"
  2938. " int a; //\n"
  2939. "});",
  2940. format("DEBUG({\n"
  2941. " int a; //\n"
  2942. "});",
  2943. 0, 0, getLLVMStyle()));
  2944. }
  2945. TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
  2946. EXPECT_EQ("{}", format("{}"));
  2947. verifyFormat("enum E {};");
  2948. verifyFormat("enum E {}");
  2949. }
  2950. //===----------------------------------------------------------------------===//
  2951. // Line break tests.
  2952. //===----------------------------------------------------------------------===//
  2953. TEST_F(FormatTest, PreventConfusingIndents) {
  2954. verifyFormat(
  2955. "void f() {\n"
  2956. " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
  2957. " parameter, parameter, parameter)),\n"
  2958. " SecondLongCall(parameter));\n"
  2959. "}");
  2960. verifyFormat(
  2961. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2962. " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2963. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2964. " aaaaaaaaaaaaaaaaaaaaaaaa);");
  2965. verifyFormat(
  2966. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2967. " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
  2968. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  2969. " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
  2970. verifyFormat(
  2971. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  2972. " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
  2973. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
  2974. " aaaaaaaaaaaaaaaaaaaaaaaa>;");
  2975. verifyFormat("int a = bbbb && ccc && fffff(\n"
  2976. "#define A Just forcing a new line\n"
  2977. " ddd);");
  2978. }
  2979. TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
  2980. verifyFormat(
  2981. "bool aaaaaaa =\n"
  2982. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
  2983. " bbbbbbbb();");
  2984. verifyFormat(
  2985. "bool aaaaaaa =\n"
  2986. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
  2987. " bbbbbbbb();");
  2988. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  2989. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
  2990. " ccccccccc == ddddddddddd;");
  2991. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  2992. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
  2993. " ccccccccc == ddddddddddd;");
  2994. verifyFormat(
  2995. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  2996. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
  2997. " ccccccccc == ddddddddddd;");
  2998. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  2999. " aaaaaa) &&\n"
  3000. " bbbbbb && cccccc;");
  3001. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  3002. " aaaaaa) >>\n"
  3003. " bbbbbb;");
  3004. verifyFormat("Whitespaces.addUntouchableComment(\n"
  3005. " SourceMgr.getSpellingColumnNumber(\n"
  3006. " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
  3007. " 1);");
  3008. verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3009. " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
  3010. " cccccc) {\n}");
  3011. verifyFormat("b = a &&\n"
  3012. " // Comment\n"
  3013. " b.c && d;");
  3014. // If the LHS of a comparison is not a binary expression itself, the
  3015. // additional linebreak confuses many people.
  3016. verifyFormat(
  3017. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3018. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
  3019. "}");
  3020. verifyFormat(
  3021. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3022. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  3023. "}");
  3024. verifyFormat(
  3025. "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
  3026. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  3027. "}");
  3028. // Even explicit parentheses stress the precedence enough to make the
  3029. // additional break unnecessary.
  3030. verifyFormat(
  3031. "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3032. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  3033. "}");
  3034. // This cases is borderline, but with the indentation it is still readable.
  3035. verifyFormat(
  3036. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3037. " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3038. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  3039. "}",
  3040. getLLVMStyleWithColumns(75));
  3041. // If the LHS is a binary expression, we should still use the additional break
  3042. // as otherwise the formatting hides the operator precedence.
  3043. verifyFormat(
  3044. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3045. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3046. " 5) {\n"
  3047. "}");
  3048. FormatStyle OnePerLine = getLLVMStyle();
  3049. OnePerLine.BinPackParameters = false;
  3050. verifyFormat(
  3051. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3052. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3053. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
  3054. OnePerLine);
  3055. }
  3056. TEST_F(FormatTest, ExpressionIndentation) {
  3057. verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3058. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3059. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3060. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3061. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
  3062. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
  3063. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3064. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
  3065. " ccccccccccccccccccccccccccccccccccccccccc;");
  3066. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3067. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3068. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3069. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3070. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3071. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3072. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3073. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3074. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3075. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3076. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3077. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3078. verifyFormat("if () {\n"
  3079. "} else if (aaaaa &&\n"
  3080. " bbbbb > // break\n"
  3081. " ccccc) {\n"
  3082. "}");
  3083. // Presence of a trailing comment used to change indentation of b.
  3084. verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
  3085. " b;\n"
  3086. "return aaaaaaaaaaaaaaaaaaa +\n"
  3087. " b; //",
  3088. getLLVMStyleWithColumns(30));
  3089. }
  3090. TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
  3091. // Not sure what the best system is here. Like this, the LHS can be found
  3092. // immediately above an operator (everything with the same or a higher
  3093. // indent). The RHS is aligned right of the operator and so compasses
  3094. // everything until something with the same indent as the operator is found.
  3095. // FIXME: Is this a good system?
  3096. FormatStyle Style = getLLVMStyle();
  3097. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  3098. verifyFormat(
  3099. "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3100. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3101. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3102. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3103. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3104. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3105. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3106. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3107. " > ccccccccccccccccccccccccccccccccccccccccc;",
  3108. Style);
  3109. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3110. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3111. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3112. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3113. Style);
  3114. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3115. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3116. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3117. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3118. Style);
  3119. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3120. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3121. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3122. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3123. Style);
  3124. verifyFormat("if () {\n"
  3125. "} else if (aaaaa\n"
  3126. " && bbbbb // break\n"
  3127. " > ccccc) {\n"
  3128. "}",
  3129. Style);
  3130. verifyFormat("return (a)\n"
  3131. " // comment\n"
  3132. " + b;",
  3133. Style);
  3134. verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3135. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3136. " + cc;",
  3137. Style);
  3138. // Forced by comments.
  3139. verifyFormat(
  3140. "unsigned ContentSize =\n"
  3141. " sizeof(int16_t) // DWARF ARange version number\n"
  3142. " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
  3143. " + sizeof(int8_t) // Pointer Size (in bytes)\n"
  3144. " + sizeof(int8_t); // Segment Size (in bytes)");
  3145. verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
  3146. " == boost::fusion::at_c<1>(iiii).second;",
  3147. Style);
  3148. Style.ColumnLimit = 60;
  3149. verifyFormat("zzzzzzzzzz\n"
  3150. " = bbbbbbbbbbbbbbbbb\n"
  3151. " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3152. Style);
  3153. }
  3154. TEST_F(FormatTest, NoOperandAlignment) {
  3155. FormatStyle Style = getLLVMStyle();
  3156. Style.AlignOperands = false;
  3157. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
  3158. verifyFormat(
  3159. "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3160. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3161. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3162. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3163. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3164. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3165. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3166. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3167. " > ccccccccccccccccccccccccccccccccccccccccc;",
  3168. Style);
  3169. verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3170. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3171. " + cc;",
  3172. Style);
  3173. verifyFormat("int a = aa\n"
  3174. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3175. " * cccccccccccccccccccccccccccccccccccc;",
  3176. Style);
  3177. Style.AlignAfterOpenBracket = false;
  3178. verifyFormat("return (a > b\n"
  3179. " // comment1\n"
  3180. " // comment2\n"
  3181. " || c);",
  3182. Style);
  3183. }
  3184. TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
  3185. FormatStyle Style = getLLVMStyle();
  3186. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
  3187. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3188. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3189. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
  3190. Style);
  3191. }
  3192. TEST_F(FormatTest, ConstructorInitializers) {
  3193. verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
  3194. verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
  3195. getLLVMStyleWithColumns(45));
  3196. verifyFormat("Constructor()\n"
  3197. " : Inttializer(FitsOnTheLine) {}",
  3198. getLLVMStyleWithColumns(44));
  3199. verifyFormat("Constructor()\n"
  3200. " : Inttializer(FitsOnTheLine) {}",
  3201. getLLVMStyleWithColumns(43));
  3202. verifyFormat(
  3203. "SomeClass::Constructor()\n"
  3204. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  3205. verifyFormat(
  3206. "SomeClass::Constructor()\n"
  3207. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3208. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
  3209. verifyFormat(
  3210. "SomeClass::Constructor()\n"
  3211. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3212. " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  3213. verifyFormat("Constructor()\n"
  3214. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3215. " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3216. " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3217. " aaaaaaaaaaaaaaaaaaaaaaa() {}");
  3218. verifyFormat("Constructor()\n"
  3219. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3220. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3221. verifyFormat("Constructor(int Parameter = 0)\n"
  3222. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
  3223. " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
  3224. verifyFormat("Constructor()\n"
  3225. " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
  3226. "}",
  3227. getLLVMStyleWithColumns(60));
  3228. verifyFormat("Constructor()\n"
  3229. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3230. " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
  3231. // Here a line could be saved by splitting the second initializer onto two
  3232. // lines, but that is not desirable.
  3233. verifyFormat("Constructor()\n"
  3234. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3235. " aaaaaaaaaaa(aaaaaaaaaaa),\n"
  3236. " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3237. FormatStyle OnePerLine = getLLVMStyle();
  3238. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  3239. verifyFormat("SomeClass::Constructor()\n"
  3240. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3241. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3242. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  3243. OnePerLine);
  3244. verifyFormat("SomeClass::Constructor()\n"
  3245. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
  3246. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3247. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  3248. OnePerLine);
  3249. verifyFormat("MyClass::MyClass(int var)\n"
  3250. " : some_var_(var), // 4 space indent\n"
  3251. " some_other_var_(var + 1) { // lined up\n"
  3252. "}",
  3253. OnePerLine);
  3254. verifyFormat("Constructor()\n"
  3255. " : aaaaa(aaaaaa),\n"
  3256. " aaaaa(aaaaaa),\n"
  3257. " aaaaa(aaaaaa),\n"
  3258. " aaaaa(aaaaaa),\n"
  3259. " aaaaa(aaaaaa) {}",
  3260. OnePerLine);
  3261. verifyFormat("Constructor()\n"
  3262. " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
  3263. " aaaaaaaaaaaaaaaaaaaaaa) {}",
  3264. OnePerLine);
  3265. EXPECT_EQ("Constructor()\n"
  3266. " : // Comment forcing unwanted break.\n"
  3267. " aaaa(aaaa) {}",
  3268. format("Constructor() :\n"
  3269. " // Comment forcing unwanted break.\n"
  3270. " aaaa(aaaa) {}"));
  3271. }
  3272. TEST_F(FormatTest, MemoizationTests) {
  3273. // This breaks if the memoization lookup does not take \c Indent and
  3274. // \c LastSpace into account.
  3275. verifyFormat(
  3276. "extern CFRunLoopTimerRef\n"
  3277. "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
  3278. " CFTimeInterval interval, CFOptionFlags flags,\n"
  3279. " CFIndex order, CFRunLoopTimerCallBack callout,\n"
  3280. " CFRunLoopTimerContext *context) {}");
  3281. // Deep nesting somewhat works around our memoization.
  3282. verifyFormat(
  3283. "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3284. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3285. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3286. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3287. " aaaaa())))))))))))))))))))))))))))))))))))))));",
  3288. getLLVMStyleWithColumns(65));
  3289. verifyFormat(
  3290. "aaaaa(\n"
  3291. " aaaaa,\n"
  3292. " aaaaa(\n"
  3293. " aaaaa,\n"
  3294. " aaaaa(\n"
  3295. " aaaaa,\n"
  3296. " aaaaa(\n"
  3297. " aaaaa,\n"
  3298. " aaaaa(\n"
  3299. " aaaaa,\n"
  3300. " aaaaa(\n"
  3301. " aaaaa,\n"
  3302. " aaaaa(\n"
  3303. " aaaaa,\n"
  3304. " aaaaa(\n"
  3305. " aaaaa,\n"
  3306. " aaaaa(\n"
  3307. " aaaaa,\n"
  3308. " aaaaa(\n"
  3309. " aaaaa,\n"
  3310. " aaaaa(\n"
  3311. " aaaaa,\n"
  3312. " aaaaa(\n"
  3313. " aaaaa,\n"
  3314. " aaaaa))))))))))));",
  3315. getLLVMStyleWithColumns(65));
  3316. verifyFormat(
  3317. "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"
  3318. " a),\n"
  3319. " a),\n"
  3320. " a),\n"
  3321. " a),\n"
  3322. " a),\n"
  3323. " a),\n"
  3324. " a),\n"
  3325. " a),\n"
  3326. " a),\n"
  3327. " a),\n"
  3328. " a),\n"
  3329. " a),\n"
  3330. " a),\n"
  3331. " a),\n"
  3332. " a),\n"
  3333. " a),\n"
  3334. " a)",
  3335. getLLVMStyleWithColumns(65));
  3336. // This test takes VERY long when memoization is broken.
  3337. FormatStyle OnePerLine = getLLVMStyle();
  3338. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  3339. OnePerLine.BinPackParameters = false;
  3340. std::string input = "Constructor()\n"
  3341. " : aaaa(a,\n";
  3342. for (unsigned i = 0, e = 80; i != e; ++i) {
  3343. input += " a,\n";
  3344. }
  3345. input += " a) {}";
  3346. verifyFormat(input, OnePerLine);
  3347. }
  3348. TEST_F(FormatTest, BreaksAsHighAsPossible) {
  3349. verifyFormat(
  3350. "void f() {\n"
  3351. " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
  3352. " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
  3353. " f();\n"
  3354. "}");
  3355. verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
  3356. " Intervals[i - 1].getRange().getLast()) {\n}");
  3357. }
  3358. TEST_F(FormatTest, BreaksFunctionDeclarations) {
  3359. // Principially, we break function declarations in a certain order:
  3360. // 1) break amongst arguments.
  3361. verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
  3362. " Cccccccccccccc cccccccccccccc);");
  3363. verifyFormat(
  3364. "template <class TemplateIt>\n"
  3365. "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
  3366. " TemplateIt *stop) {}");
  3367. // 2) break after return type.
  3368. verifyFormat(
  3369. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3370. "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
  3371. getGoogleStyle());
  3372. // 3) break after (.
  3373. verifyFormat(
  3374. "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
  3375. " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
  3376. getGoogleStyle());
  3377. // 4) break before after nested name specifiers.
  3378. verifyFormat(
  3379. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3380. "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
  3381. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
  3382. getGoogleStyle());
  3383. // However, there are exceptions, if a sufficient amount of lines can be
  3384. // saved.
  3385. // FIXME: The precise cut-offs wrt. the number of saved lines might need some
  3386. // more adjusting.
  3387. verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  3388. " Cccccccccccccc cccccccccc,\n"
  3389. " Cccccccccccccc cccccccccc,\n"
  3390. " Cccccccccccccc cccccccccc,\n"
  3391. " Cccccccccccccc cccccccccc);");
  3392. verifyFormat(
  3393. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3394. "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3395. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3396. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
  3397. getGoogleStyle());
  3398. verifyFormat(
  3399. "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  3400. " Cccccccccccccc cccccccccc,\n"
  3401. " Cccccccccccccc cccccccccc,\n"
  3402. " Cccccccccccccc cccccccccc,\n"
  3403. " Cccccccccccccc cccccccccc,\n"
  3404. " Cccccccccccccc cccccccccc,\n"
  3405. " Cccccccccccccc cccccccccc);");
  3406. verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3407. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3408. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3409. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3410. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
  3411. // Break after multi-line parameters.
  3412. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3413. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3414. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3415. " bbbb bbbb);");
  3416. verifyFormat("void SomeLoooooooooooongFunction(\n"
  3417. " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  3418. " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3419. " int bbbbbbbbbbbbb);");
  3420. // Treat overloaded operators like other functions.
  3421. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3422. "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
  3423. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3424. "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
  3425. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3426. "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
  3427. verifyGoogleFormat(
  3428. "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
  3429. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  3430. verifyGoogleFormat(
  3431. "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
  3432. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  3433. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3434. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  3435. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
  3436. "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  3437. verifyGoogleFormat(
  3438. "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
  3439. "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3440. " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
  3441. }
  3442. TEST_F(FormatTest, TrailingReturnType) {
  3443. verifyFormat("auto foo() -> int;\n");
  3444. verifyFormat("struct S {\n"
  3445. " auto bar() const -> int;\n"
  3446. "};");
  3447. verifyFormat("template <size_t Order, typename T>\n"
  3448. "auto load_img(const std::string &filename)\n"
  3449. " -> alias::tensor<Order, T, mem::tag::cpu> {}");
  3450. verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
  3451. " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
  3452. verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
  3453. // Not trailing return types.
  3454. verifyFormat("void f() { auto a = b->c(); }");
  3455. }
  3456. TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
  3457. // Avoid breaking before trailing 'const' or other trailing annotations, if
  3458. // they are not function-like.
  3459. FormatStyle Style = getGoogleStyle();
  3460. Style.ColumnLimit = 47;
  3461. verifyFormat("void someLongFunction(\n"
  3462. " int someLoooooooooooooongParameter) const {\n}",
  3463. getLLVMStyleWithColumns(47));
  3464. verifyFormat("LoooooongReturnType\n"
  3465. "someLoooooooongFunction() const {}",
  3466. getLLVMStyleWithColumns(47));
  3467. verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
  3468. " const {}",
  3469. Style);
  3470. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3471. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
  3472. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3473. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
  3474. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3475. " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
  3476. verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
  3477. " aaaaaaaaaaa aaaaa) const override;");
  3478. verifyGoogleFormat(
  3479. "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3480. " const override;");
  3481. // Even if the first parameter has to be wrapped.
  3482. verifyFormat("void someLongFunction(\n"
  3483. " int someLongParameter) const {}",
  3484. getLLVMStyleWithColumns(46));
  3485. verifyFormat("void someLongFunction(\n"
  3486. " int someLongParameter) const {}",
  3487. Style);
  3488. verifyFormat("void someLongFunction(\n"
  3489. " int someLongParameter) override {}",
  3490. Style);
  3491. verifyFormat("void someLongFunction(\n"
  3492. " int someLongParameter) OVERRIDE {}",
  3493. Style);
  3494. verifyFormat("void someLongFunction(\n"
  3495. " int someLongParameter) final {}",
  3496. Style);
  3497. verifyFormat("void someLongFunction(\n"
  3498. " int someLongParameter) FINAL {}",
  3499. Style);
  3500. verifyFormat("void someLongFunction(\n"
  3501. " int parameter) const override {}",
  3502. Style);
  3503. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  3504. verifyFormat("void someLongFunction(\n"
  3505. " int someLongParameter) const\n"
  3506. "{\n"
  3507. "}",
  3508. Style);
  3509. // Unless these are unknown annotations.
  3510. verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
  3511. " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3512. " LONG_AND_UGLY_ANNOTATION;");
  3513. // Breaking before function-like trailing annotations is fine to keep them
  3514. // close to their arguments.
  3515. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3516. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  3517. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  3518. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  3519. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  3520. " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
  3521. verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
  3522. " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
  3523. verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
  3524. verifyFormat(
  3525. "void aaaaaaaaaaaaaaaaaa()\n"
  3526. " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3527. " aaaaaaaaaaaaaaaaaaaaaaaaa));");
  3528. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3529. " __attribute__((unused));");
  3530. verifyGoogleFormat(
  3531. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3532. " GUARDED_BY(aaaaaaaaaaaa);");
  3533. verifyGoogleFormat(
  3534. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3535. " GUARDED_BY(aaaaaaaaaaaa);");
  3536. verifyGoogleFormat(
  3537. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
  3538. " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3539. }
  3540. TEST_F(FormatTest, BreaksDesireably) {
  3541. verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  3542. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  3543. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
  3544. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3545. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  3546. "}");
  3547. verifyFormat(
  3548. "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3549. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3550. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3551. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3552. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3553. verifyFormat(
  3554. "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3555. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  3556. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3557. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
  3558. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3559. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3560. verifyFormat(
  3561. "void f() {\n"
  3562. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
  3563. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3564. "}");
  3565. verifyFormat(
  3566. "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3567. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3568. verifyFormat(
  3569. "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3570. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3571. verifyFormat(
  3572. "aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3573. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3574. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3575. // Indent consistently independent of call expression.
  3576. verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
  3577. " dddddddddddddddddddddddddddddd));\n"
  3578. "aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3579. " dddddddddddddddddddddddddddddd));");
  3580. // This test case breaks on an incorrect memoization, i.e. an optimization not
  3581. // taking into account the StopAt value.
  3582. verifyFormat(
  3583. "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3584. " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3585. " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3586. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3587. verifyFormat("{\n {\n {\n"
  3588. " Annotation.SpaceRequiredBefore =\n"
  3589. " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
  3590. " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
  3591. " }\n }\n}");
  3592. // Break on an outer level if there was a break on an inner level.
  3593. EXPECT_EQ("f(g(h(a, // comment\n"
  3594. " b, c),\n"
  3595. " d, e),\n"
  3596. " x, y);",
  3597. format("f(g(h(a, // comment\n"
  3598. " b, c), d, e), x, y);"));
  3599. // Prefer breaking similar line breaks.
  3600. verifyFormat(
  3601. "const int kTrackingOptions = NSTrackingMouseMoved |\n"
  3602. " NSTrackingMouseEnteredAndExited |\n"
  3603. " NSTrackingActiveAlways;");
  3604. }
  3605. TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
  3606. FormatStyle NoBinPacking = getGoogleStyle();
  3607. NoBinPacking.BinPackParameters = false;
  3608. NoBinPacking.BinPackArguments = true;
  3609. verifyFormat("void f() {\n"
  3610. " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
  3611. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3612. "}",
  3613. NoBinPacking);
  3614. verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
  3615. " int aaaaaaaaaaaaaaaaaaaa,\n"
  3616. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3617. NoBinPacking);
  3618. }
  3619. TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
  3620. FormatStyle NoBinPacking = getGoogleStyle();
  3621. NoBinPacking.BinPackParameters = false;
  3622. NoBinPacking.BinPackArguments = false;
  3623. verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
  3624. " aaaaaaaaaaaaaaaaaaaa,\n"
  3625. " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
  3626. NoBinPacking);
  3627. verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
  3628. " aaaaaaaaaaaaa,\n"
  3629. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
  3630. NoBinPacking);
  3631. verifyFormat(
  3632. "aaaaaaaa(aaaaaaaaaaaaa,\n"
  3633. " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3634. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  3635. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3636. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
  3637. NoBinPacking);
  3638. verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  3639. " .aaaaaaaaaaaaaaaaaa();",
  3640. NoBinPacking);
  3641. verifyFormat("void f() {\n"
  3642. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3643. " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
  3644. "}",
  3645. NoBinPacking);
  3646. verifyFormat(
  3647. "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3648. " aaaaaaaaaaaa,\n"
  3649. " aaaaaaaaaaaa);",
  3650. NoBinPacking);
  3651. verifyFormat(
  3652. "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
  3653. " ddddddddddddddddddddddddddddd),\n"
  3654. " test);",
  3655. NoBinPacking);
  3656. verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3657. " aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3658. " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;",
  3659. NoBinPacking);
  3660. verifyFormat("a(\"a\"\n"
  3661. " \"a\",\n"
  3662. " a);");
  3663. NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
  3664. verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
  3665. " aaaaaaaaa,\n"
  3666. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3667. NoBinPacking);
  3668. verifyFormat(
  3669. "void f() {\n"
  3670. " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  3671. " .aaaaaaa();\n"
  3672. "}",
  3673. NoBinPacking);
  3674. verifyFormat(
  3675. "template <class SomeType, class SomeOtherType>\n"
  3676. "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
  3677. NoBinPacking);
  3678. }
  3679. TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
  3680. FormatStyle Style = getLLVMStyleWithColumns(15);
  3681. Style.ExperimentalAutoDetectBinPacking = true;
  3682. EXPECT_EQ("aaa(aaaa,\n"
  3683. " aaaa,\n"
  3684. " aaaa);\n"
  3685. "aaa(aaaa,\n"
  3686. " aaaa,\n"
  3687. " aaaa);",
  3688. format("aaa(aaaa,\n" // one-per-line
  3689. " aaaa,\n"
  3690. " aaaa );\n"
  3691. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  3692. Style));
  3693. EXPECT_EQ("aaa(aaaa, aaaa,\n"
  3694. " aaaa);\n"
  3695. "aaa(aaaa, aaaa,\n"
  3696. " aaaa);",
  3697. format("aaa(aaaa, aaaa,\n" // bin-packed
  3698. " aaaa );\n"
  3699. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  3700. Style));
  3701. }
  3702. TEST_F(FormatTest, FormatsBuilderPattern) {
  3703. verifyFormat(
  3704. "return llvm::StringSwitch<Reference::Kind>(name)\n"
  3705. " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
  3706. " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
  3707. " .StartsWith(\".init\", ORDER_INIT)\n"
  3708. " .StartsWith(\".fini\", ORDER_FINI)\n"
  3709. " .StartsWith(\".hash\", ORDER_HASH)\n"
  3710. " .Default(ORDER_TEXT);\n");
  3711. verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
  3712. " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
  3713. verifyFormat(
  3714. "aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaa(\n"
  3715. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3716. " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
  3717. verifyFormat(
  3718. "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
  3719. " aaaaaaaaaaaaaa);");
  3720. verifyFormat(
  3721. "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
  3722. " aaaaaa->aaaaaaaaaaaa()\n"
  3723. " ->aaaaaaaaaaaaaaaa(\n"
  3724. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3725. " ->aaaaaaaaaaaaaaaaa();");
  3726. verifyGoogleFormat(
  3727. "void f() {\n"
  3728. " someo->Add((new util::filetools::Handler(dir))\n"
  3729. " ->OnEvent1(NewPermanentCallback(\n"
  3730. " this, &HandlerHolderClass::EventHandlerCBA))\n"
  3731. " ->OnEvent2(NewPermanentCallback(\n"
  3732. " this, &HandlerHolderClass::EventHandlerCBB))\n"
  3733. " ->OnEvent3(NewPermanentCallback(\n"
  3734. " this, &HandlerHolderClass::EventHandlerCBC))\n"
  3735. " ->OnEvent5(NewPermanentCallback(\n"
  3736. " this, &HandlerHolderClass::EventHandlerCBD))\n"
  3737. " ->OnEvent6(NewPermanentCallback(\n"
  3738. " this, &HandlerHolderClass::EventHandlerCBE)));\n"
  3739. "}");
  3740. verifyFormat(
  3741. "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
  3742. verifyFormat("aaaaaaaaaaaaaaa()\n"
  3743. " .aaaaaaaaaaaaaaa()\n"
  3744. " .aaaaaaaaaaaaaaa()\n"
  3745. " .aaaaaaaaaaaaaaa()\n"
  3746. " .aaaaaaaaaaaaaaa();");
  3747. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3748. " .aaaaaaaaaaaaaaa()\n"
  3749. " .aaaaaaaaaaaaaaa()\n"
  3750. " .aaaaaaaaaaaaaaa();");
  3751. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3752. " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3753. " .aaaaaaaaaaaaaaa();");
  3754. verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3755. " ->aaaaaaaaaaaaaae(0)\n"
  3756. " ->aaaaaaaaaaaaaaa();");
  3757. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  3758. " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3759. " .has<bbbbbbbbbbbbbbbbbbbbb>();");
  3760. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  3761. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  3762. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
  3763. // Prefer not to break after empty parentheses.
  3764. verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
  3765. " First->LastNewlineOffset);");
  3766. }
  3767. TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
  3768. verifyFormat(
  3769. "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3770. " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
  3771. verifyFormat(
  3772. "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
  3773. " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
  3774. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  3775. " ccccccccccccccccccccccccc) {\n}");
  3776. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
  3777. " ccccccccccccccccccccccccc) {\n}");
  3778. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  3779. " ccccccccccccccccccccccccc) {\n}");
  3780. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
  3781. " ccccccccccccccccccccccccc) {\n}");
  3782. verifyFormat(
  3783. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
  3784. " ccccccccccccccccccccccccc) {\n}");
  3785. verifyFormat(
  3786. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
  3787. " ccccccccccccccccccccccccc) {\n}");
  3788. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
  3789. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
  3790. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
  3791. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  3792. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
  3793. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
  3794. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
  3795. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  3796. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
  3797. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
  3798. " aaaaaaaaaaaaaaa != aa) {\n}");
  3799. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
  3800. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
  3801. " aaaaaaaaaaaaaaa != aa) {\n}");
  3802. }
  3803. TEST_F(FormatTest, BreaksAfterAssignments) {
  3804. verifyFormat(
  3805. "unsigned Cost =\n"
  3806. " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
  3807. " SI->getPointerAddressSpaceee());\n");
  3808. verifyFormat(
  3809. "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
  3810. " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
  3811. verifyFormat(
  3812. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
  3813. " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
  3814. verifyFormat("unsigned OriginalStartColumn =\n"
  3815. " SourceMgr.getSpellingColumnNumber(\n"
  3816. " Current.FormatTok.getStartOfNonWhitespace()) -\n"
  3817. " 1;");
  3818. }
  3819. TEST_F(FormatTest, AlignsAfterAssignments) {
  3820. verifyFormat(
  3821. "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3822. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3823. verifyFormat(
  3824. "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3825. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3826. verifyFormat(
  3827. "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3828. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3829. verifyFormat(
  3830. "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3831. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  3832. verifyFormat(
  3833. "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3834. " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3835. " aaaaaaaaaaaaaaaaaaaaaaaa;");
  3836. }
  3837. TEST_F(FormatTest, AlignsAfterReturn) {
  3838. verifyFormat(
  3839. "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3840. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3841. verifyFormat(
  3842. "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3843. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  3844. verifyFormat(
  3845. "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  3846. " aaaaaaaaaaaaaaaaaaaaaa();");
  3847. verifyFormat(
  3848. "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  3849. " aaaaaaaaaaaaaaaaaaaaaa());");
  3850. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3851. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3852. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3853. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
  3854. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3855. verifyFormat("return\n"
  3856. " // true if code is one of a or b.\n"
  3857. " code == a || code == b;");
  3858. }
  3859. TEST_F(FormatTest, AlignsAfterOpenBracket) {
  3860. verifyFormat(
  3861. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
  3862. " aaaaaaaaa aaaaaaa) {}");
  3863. verifyFormat(
  3864. "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
  3865. " aaaaaaaaaaa aaaaaaaaa);");
  3866. verifyFormat(
  3867. "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
  3868. " aaaaaaaaaaaaaaaaaaaaa));");
  3869. FormatStyle Style = getLLVMStyle();
  3870. Style.AlignAfterOpenBracket = false;
  3871. verifyFormat(
  3872. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3873. " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
  3874. Style);
  3875. verifyFormat(
  3876. "SomeLongVariableName->someVeryLongFunctionName(\n"
  3877. " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
  3878. Style);
  3879. verifyFormat(
  3880. "SomeLongVariableName->someFunction(\n"
  3881. " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
  3882. Style);
  3883. verifyFormat(
  3884. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
  3885. " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3886. Style);
  3887. verifyFormat(
  3888. "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
  3889. " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3890. Style);
  3891. verifyFormat(
  3892. "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
  3893. " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
  3894. Style);
  3895. }
  3896. TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
  3897. FormatStyle Style = getLLVMStyleWithColumns(40);
  3898. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  3899. " bbbbbbbbbbbbbbbbbbbbbb);",
  3900. Style);
  3901. Style.AlignAfterOpenBracket = true;
  3902. Style.AlignOperands = false;
  3903. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  3904. " bbbbbbbbbbbbbbbbbbbbbb);",
  3905. Style);
  3906. Style.AlignAfterOpenBracket = false;
  3907. Style.AlignOperands = true;
  3908. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  3909. " bbbbbbbbbbbbbbbbbbbbbb);",
  3910. Style);
  3911. Style.AlignAfterOpenBracket = false;
  3912. Style.AlignOperands = false;
  3913. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  3914. " bbbbbbbbbbbbbbbbbbbbbb);",
  3915. Style);
  3916. }
  3917. TEST_F(FormatTest, BreaksConditionalExpressions) {
  3918. verifyFormat(
  3919. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3920. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3921. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3922. verifyFormat(
  3923. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3924. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3925. verifyFormat(
  3926. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
  3927. " : aaaaaaaaaaaaa);");
  3928. verifyFormat(
  3929. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3930. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3931. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3932. " aaaaaaaaaaaaa);");
  3933. verifyFormat(
  3934. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3935. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3936. " aaaaaaaaaaaaa);");
  3937. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3938. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3939. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3940. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3941. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3942. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3943. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3944. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3945. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3946. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3947. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3948. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3949. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3950. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3951. " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3952. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3953. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3954. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3955. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3956. " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3957. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  3958. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3959. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3960. " : aaaaaaaaaaaaaaaa;");
  3961. verifyFormat(
  3962. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3963. " ? aaaaaaaaaaaaaaa\n"
  3964. " : aaaaaaaaaaaaaaa;");
  3965. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  3966. " aaaaaaaaa\n"
  3967. " ? b\n"
  3968. " : c);");
  3969. verifyFormat("return aaaa == bbbb\n"
  3970. " // comment\n"
  3971. " ? aaaa\n"
  3972. " : bbbb;");
  3973. verifyFormat(
  3974. "unsigned Indent =\n"
  3975. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
  3976. " ? IndentForLevel[TheLine.Level]\n"
  3977. " : TheLine * 2,\n"
  3978. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  3979. getLLVMStyleWithColumns(70));
  3980. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  3981. " ? aaaaaaaaaaaaaaa\n"
  3982. " : bbbbbbbbbbbbbbb //\n"
  3983. " ? ccccccccccccccc\n"
  3984. " : ddddddddddddddd;");
  3985. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  3986. " ? aaaaaaaaaaaaaaa\n"
  3987. " : (bbbbbbbbbbbbbbb //\n"
  3988. " ? ccccccccccccccc\n"
  3989. " : ddddddddddddddd);");
  3990. verifyFormat(
  3991. "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3992. " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3993. " aaaaaaaaaaaaaaaaaaaaa +\n"
  3994. " aaaaaaaaaaaaaaaaaaaaa\n"
  3995. " : aaaaaaaaaa;");
  3996. verifyFormat(
  3997. "aaaaaa = aaaaaaaaaaaa\n"
  3998. " ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3999. " : aaaaaaaaaaaaaaaaaaaaaa\n"
  4000. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4001. FormatStyle NoBinPacking = getLLVMStyle();
  4002. NoBinPacking.BinPackArguments = false;
  4003. verifyFormat(
  4004. "void f() {\n"
  4005. " g(aaa,\n"
  4006. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  4007. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4008. " ? aaaaaaaaaaaaaaa\n"
  4009. " : aaaaaaaaaaaaaaa);\n"
  4010. "}",
  4011. NoBinPacking);
  4012. verifyFormat(
  4013. "void f() {\n"
  4014. " g(aaa,\n"
  4015. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  4016. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4017. " ?: aaaaaaaaaaaaaaa);\n"
  4018. "}",
  4019. NoBinPacking);
  4020. verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
  4021. " // comment.\n"
  4022. " ccccccccccccccccccccccccccccccccccccccc\n"
  4023. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4024. " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
  4025. // Assignments in conditional expressions. Apparently not uncommon :-(.
  4026. verifyFormat("return a != b\n"
  4027. " // comment\n"
  4028. " ? a = b\n"
  4029. " : a = b;");
  4030. verifyFormat("return a != b\n"
  4031. " // comment\n"
  4032. " ? a = a != b\n"
  4033. " // comment\n"
  4034. " ? a = b\n"
  4035. " : a\n"
  4036. " : a;\n");
  4037. verifyFormat("return a != b\n"
  4038. " // comment\n"
  4039. " ? a\n"
  4040. " : a = a != b\n"
  4041. " // comment\n"
  4042. " ? a = b\n"
  4043. " : a;");
  4044. }
  4045. TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
  4046. FormatStyle Style = getLLVMStyle();
  4047. Style.BreakBeforeTernaryOperators = false;
  4048. Style.ColumnLimit = 70;
  4049. verifyFormat(
  4050. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4051. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4052. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4053. Style);
  4054. verifyFormat(
  4055. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4056. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4057. Style);
  4058. verifyFormat(
  4059. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
  4060. " aaaaaaaaaaaaa);",
  4061. Style);
  4062. verifyFormat(
  4063. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4064. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4065. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4066. " aaaaaaaaaaaaa);",
  4067. Style);
  4068. verifyFormat(
  4069. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4070. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4071. " aaaaaaaaaaaaa);",
  4072. Style);
  4073. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4074. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4075. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  4076. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4077. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4078. Style);
  4079. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4080. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4081. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4082. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  4083. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4084. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4085. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4086. Style);
  4087. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4088. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
  4089. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4090. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4091. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4092. Style);
  4093. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4094. " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4095. " aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4096. Style);
  4097. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  4098. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4099. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4100. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4101. Style);
  4102. verifyFormat(
  4103. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4104. " aaaaaaaaaaaaaaa :\n"
  4105. " aaaaaaaaaaaaaaa;",
  4106. Style);
  4107. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  4108. " aaaaaaaaa ?\n"
  4109. " b :\n"
  4110. " c);",
  4111. Style);
  4112. verifyFormat(
  4113. "unsigned Indent =\n"
  4114. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n"
  4115. " IndentForLevel[TheLine.Level] :\n"
  4116. " TheLine * 2,\n"
  4117. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  4118. Style);
  4119. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  4120. " aaaaaaaaaaaaaaa :\n"
  4121. " bbbbbbbbbbbbbbb ? //\n"
  4122. " ccccccccccccccc :\n"
  4123. " ddddddddddddddd;",
  4124. Style);
  4125. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  4126. " aaaaaaaaaaaaaaa :\n"
  4127. " (bbbbbbbbbbbbbbb ? //\n"
  4128. " ccccccccccccccc :\n"
  4129. " ddddddddddddddd);",
  4130. Style);
  4131. }
  4132. TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
  4133. verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
  4134. " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
  4135. verifyFormat("bool a = true, b = false;");
  4136. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4137. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
  4138. " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
  4139. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
  4140. verifyFormat(
  4141. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  4142. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
  4143. " d = e && f;");
  4144. verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
  4145. " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
  4146. verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  4147. " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
  4148. verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
  4149. " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
  4150. // FIXME: If multiple variables are defined, the "*" needs to move to the new
  4151. // line. Also fix indent for breaking after the type, this looks bad.
  4152. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
  4153. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
  4154. " * b = bbbbbbbbbbbbbbbbbbb;",
  4155. getGoogleStyle());
  4156. // Not ideal, but pointer-with-type does not allow much here.
  4157. verifyGoogleFormat(
  4158. "aaaaaaaaa* a = aaaaaaaaaaaaaaaaaaa, * b = bbbbbbbbbbbbbbbbbbb,\n"
  4159. " * b = bbbbbbbbbbbbbbbbbbb, * d = ddddddddddddddddddd;");
  4160. }
  4161. TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
  4162. verifyFormat("arr[foo ? bar : baz];");
  4163. verifyFormat("f()[foo ? bar : baz];");
  4164. verifyFormat("(a + b)[foo ? bar : baz];");
  4165. verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
  4166. }
  4167. TEST_F(FormatTest, AlignsStringLiterals) {
  4168. verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
  4169. " \"short literal\");");
  4170. verifyFormat(
  4171. "looooooooooooooooooooooooongFunction(\n"
  4172. " \"short literal\"\n"
  4173. " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
  4174. verifyFormat("someFunction(\"Always break between multi-line\"\n"
  4175. " \" string literals\",\n"
  4176. " and, other, parameters);");
  4177. EXPECT_EQ("fun + \"1243\" /* comment */\n"
  4178. " \"5678\";",
  4179. format("fun + \"1243\" /* comment */\n"
  4180. " \"5678\";",
  4181. getLLVMStyleWithColumns(28)));
  4182. EXPECT_EQ(
  4183. "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  4184. " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
  4185. " \"aaaaaaaaaaaaaaaa\";",
  4186. format("aaaaaa ="
  4187. "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  4188. "aaaaaaaaaaaaaaaaaaaaa\" "
  4189. "\"aaaaaaaaaaaaaaaa\";"));
  4190. verifyFormat("a = a + \"a\"\n"
  4191. " \"a\"\n"
  4192. " \"a\";");
  4193. verifyFormat("f(\"a\", \"b\"\n"
  4194. " \"c\");");
  4195. verifyFormat(
  4196. "#define LL_FORMAT \"ll\"\n"
  4197. "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
  4198. " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
  4199. verifyFormat("#define A(X) \\\n"
  4200. " \"aaaaa\" #X \"bbbbbb\" \\\n"
  4201. " \"ccccc\"",
  4202. getLLVMStyleWithColumns(23));
  4203. verifyFormat("#define A \"def\"\n"
  4204. "f(\"abc\" A \"ghi\"\n"
  4205. " \"jkl\");");
  4206. verifyFormat("f(L\"a\"\n"
  4207. " L\"b\")");
  4208. verifyFormat("#define A(X) \\\n"
  4209. " L\"aaaaa\" #X L\"bbbbbb\" \\\n"
  4210. " L\"ccccc\"",
  4211. getLLVMStyleWithColumns(25));
  4212. }
  4213. TEST_F(FormatTest, AlwaysBreakAfterDefinitionReturnType) {
  4214. FormatStyle AfterType = getLLVMStyle();
  4215. AfterType.AlwaysBreakAfterDefinitionReturnType = true;
  4216. verifyFormat("const char *\n"
  4217. "f(void) {\n" // Break here.
  4218. " return \"\";\n"
  4219. "}\n"
  4220. "const char *bar(void);\n", // No break here.
  4221. AfterType);
  4222. verifyFormat("template <class T>\n"
  4223. "T *\n"
  4224. "f(T &c) {\n" // Break here.
  4225. " return NULL;\n"
  4226. "}\n"
  4227. "template <class T> T *f(T &c);\n", // No break here.
  4228. AfterType);
  4229. AfterType.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  4230. verifyFormat("const char *\n"
  4231. "f(void)\n" // Break here.
  4232. "{\n"
  4233. " return \"\";\n"
  4234. "}\n"
  4235. "const char *bar(void);\n", // No break here.
  4236. AfterType);
  4237. verifyFormat("template <class T>\n"
  4238. "T *\n" // Problem here: no line break
  4239. "f(T &c)\n" // Break here.
  4240. "{\n"
  4241. " return NULL;\n"
  4242. "}\n"
  4243. "template <class T> T *f(T &c);\n", // No break here.
  4244. AfterType);
  4245. }
  4246. TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
  4247. FormatStyle NoBreak = getLLVMStyle();
  4248. NoBreak.AlwaysBreakBeforeMultilineStrings = false;
  4249. FormatStyle Break = getLLVMStyle();
  4250. Break.AlwaysBreakBeforeMultilineStrings = true;
  4251. verifyFormat("aaaa = \"bbbb\"\n"
  4252. " \"cccc\";",
  4253. NoBreak);
  4254. verifyFormat("aaaa =\n"
  4255. " \"bbbb\"\n"
  4256. " \"cccc\";",
  4257. Break);
  4258. verifyFormat("aaaa(\"bbbb\"\n"
  4259. " \"cccc\");",
  4260. NoBreak);
  4261. verifyFormat("aaaa(\n"
  4262. " \"bbbb\"\n"
  4263. " \"cccc\");",
  4264. Break);
  4265. verifyFormat("aaaa(qqq, \"bbbb\"\n"
  4266. " \"cccc\");",
  4267. NoBreak);
  4268. verifyFormat("aaaa(qqq,\n"
  4269. " \"bbbb\"\n"
  4270. " \"cccc\");",
  4271. Break);
  4272. verifyFormat("aaaa(qqq,\n"
  4273. " L\"bbbb\"\n"
  4274. " L\"cccc\");",
  4275. Break);
  4276. // As we break before unary operators, breaking right after them is bad.
  4277. verifyFormat("string foo = abc ? \"x\"\n"
  4278. " \"blah blah blah blah blah blah\"\n"
  4279. " : \"y\";",
  4280. Break);
  4281. // Don't break if there is no column gain.
  4282. verifyFormat("f(\"aaaa\"\n"
  4283. " \"bbbb\");",
  4284. Break);
  4285. // Treat literals with escaped newlines like multi-line string literals.
  4286. EXPECT_EQ("x = \"a\\\n"
  4287. "b\\\n"
  4288. "c\";",
  4289. format("x = \"a\\\n"
  4290. "b\\\n"
  4291. "c\";",
  4292. NoBreak));
  4293. EXPECT_EQ("x =\n"
  4294. " \"a\\\n"
  4295. "b\\\n"
  4296. "c\";",
  4297. format("x = \"a\\\n"
  4298. "b\\\n"
  4299. "c\";",
  4300. Break));
  4301. // Exempt ObjC strings for now.
  4302. EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
  4303. " \"bbbb\";",
  4304. format("NSString *const kString = @\"aaaa\"\n"
  4305. "\"bbbb\";",
  4306. Break));
  4307. Break.ColumnLimit = 0;
  4308. verifyFormat("const char *hello = \"hello llvm\";", Break);
  4309. }
  4310. TEST_F(FormatTest, AlignsPipes) {
  4311. verifyFormat(
  4312. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4313. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4314. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4315. verifyFormat(
  4316. "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
  4317. " << aaaaaaaaaaaaaaaaaaaa;");
  4318. verifyFormat(
  4319. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4320. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4321. verifyFormat(
  4322. "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
  4323. " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
  4324. " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
  4325. verifyFormat(
  4326. "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4327. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4328. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4329. verifyFormat(
  4330. "llvm::errs() << \"a: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4331. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4332. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4333. verifyFormat(
  4334. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4335. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4336. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4337. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4338. verifyFormat(
  4339. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4340. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4341. verifyFormat("return out << \"somepacket = {\\n\"\n"
  4342. " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
  4343. " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
  4344. " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
  4345. " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
  4346. " << \"}\";");
  4347. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  4348. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  4349. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
  4350. verifyFormat(
  4351. "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
  4352. " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
  4353. " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
  4354. " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
  4355. " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
  4356. verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
  4357. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4358. verifyFormat(
  4359. "void f() {\n"
  4360. " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
  4361. " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  4362. "}");
  4363. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
  4364. " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
  4365. // Breaking before the first "<<" is generally not desirable.
  4366. verifyFormat(
  4367. "llvm::errs()\n"
  4368. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4369. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4370. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4371. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4372. getLLVMStyleWithColumns(70));
  4373. verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4374. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4375. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4376. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4377. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4378. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4379. getLLVMStyleWithColumns(70));
  4380. // But sometimes, breaking before the first "<<" is desirable.
  4381. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
  4382. " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
  4383. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
  4384. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4385. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4386. verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
  4387. " << BEF << IsTemplate << Description << E->getType();");
  4388. verifyFormat(
  4389. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4390. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4391. // Incomplete string literal.
  4392. EXPECT_EQ("llvm::errs() << \"\n"
  4393. " << a;",
  4394. format("llvm::errs() << \"\n<<a;"));
  4395. verifyFormat("void f() {\n"
  4396. " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
  4397. " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
  4398. "}");
  4399. // Handle 'endl'.
  4400. verifyFormat("llvm::errs() << aaaa << endl\n"
  4401. " << bbbb << endl;");
  4402. }
  4403. TEST_F(FormatTest, UnderstandsEquals) {
  4404. verifyFormat(
  4405. "aaaaaaaaaaaaaaaaa =\n"
  4406. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4407. verifyFormat(
  4408. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4409. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  4410. verifyFormat(
  4411. "if (a) {\n"
  4412. " f();\n"
  4413. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4414. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  4415. "}");
  4416. verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4417. " 100000000 + 10000000) {\n}");
  4418. }
  4419. TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
  4420. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  4421. " .looooooooooooooooooooooooooooooooooooooongFunction();");
  4422. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  4423. " ->looooooooooooooooooooooooooooooooooooooongFunction();");
  4424. verifyFormat(
  4425. "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
  4426. " Parameter2);");
  4427. verifyFormat(
  4428. "ShortObject->shortFunction(\n"
  4429. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
  4430. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
  4431. verifyFormat("loooooooooooooongFunction(\n"
  4432. " LoooooooooooooongObject->looooooooooooooooongFunction());");
  4433. verifyFormat(
  4434. "function(LoooooooooooooooooooooooooooooooooooongObject\n"
  4435. " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
  4436. verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  4437. " .WillRepeatedly(Return(SomeValue));");
  4438. verifyFormat("void f() {\n"
  4439. " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  4440. " .Times(2)\n"
  4441. " .WillRepeatedly(Return(SomeValue));\n"
  4442. "}");
  4443. verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
  4444. " ccccccccccccccccccccccc);");
  4445. verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4446. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).aaaaa(aaaaa),\n"
  4447. " aaaaaaaaaaaaaaaaaaaaa);");
  4448. verifyFormat("void f() {\n"
  4449. " aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4450. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
  4451. "}");
  4452. verifyFormat(
  4453. "aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4454. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4455. " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4456. " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4457. " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  4458. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4459. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4460. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4461. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
  4462. "}");
  4463. // Here, it is not necessary to wrap at "." or "->".
  4464. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
  4465. " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  4466. verifyFormat(
  4467. "aaaaaaaaaaa->aaaaaaaaa(\n"
  4468. " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4469. " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
  4470. verifyFormat(
  4471. "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4472. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
  4473. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
  4474. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  4475. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
  4476. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  4477. // FIXME: Should we break before .a()?
  4478. verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4479. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).a();");
  4480. FormatStyle NoBinPacking = getLLVMStyle();
  4481. NoBinPacking.BinPackParameters = false;
  4482. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  4483. " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  4484. " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
  4485. " aaaaaaaaaaaaaaaaaaa,\n"
  4486. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4487. NoBinPacking);
  4488. // If there is a subsequent call, change to hanging indentation.
  4489. verifyFormat(
  4490. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4491. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
  4492. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4493. verifyFormat(
  4494. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4495. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
  4496. verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4497. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4498. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4499. verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4500. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4501. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  4502. }
  4503. TEST_F(FormatTest, WrapsTemplateDeclarations) {
  4504. verifyFormat("template <typename T>\n"
  4505. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  4506. verifyFormat("template <typename T>\n"
  4507. "// T should be one of {A, B}.\n"
  4508. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  4509. verifyFormat(
  4510. "template <typename T>\n"
  4511. "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
  4512. verifyFormat("template <typename T>\n"
  4513. "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
  4514. " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
  4515. verifyFormat(
  4516. "template <typename T>\n"
  4517. "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
  4518. " int Paaaaaaaaaaaaaaaaaaaaram2);");
  4519. verifyFormat(
  4520. "template <typename T>\n"
  4521. "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
  4522. " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
  4523. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4524. verifyFormat("template <typename T>\n"
  4525. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4526. " int aaaaaaaaaaaaaaaaaaaaaa);");
  4527. verifyFormat(
  4528. "template <typename T1, typename T2 = char, typename T3 = char,\n"
  4529. " typename T4 = char>\n"
  4530. "void f();");
  4531. verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
  4532. " template <typename> class cccccccccccccccccccccc,\n"
  4533. " typename ddddddddddddd>\n"
  4534. "class C {};");
  4535. verifyFormat(
  4536. "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
  4537. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4538. verifyFormat("void f() {\n"
  4539. " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
  4540. " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
  4541. "}");
  4542. verifyFormat("template <typename T> class C {};");
  4543. verifyFormat("template <typename T> void f();");
  4544. verifyFormat("template <typename T> void f() {}");
  4545. verifyFormat(
  4546. "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  4547. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4548. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
  4549. " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  4550. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4551. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
  4552. " bbbbbbbbbbbbbbbbbbbbbbbb);",
  4553. getLLVMStyleWithColumns(72));
  4554. EXPECT_EQ("static_cast<A< //\n"
  4555. " B> *>(\n"
  4556. "\n"
  4557. " );",
  4558. format("static_cast<A<//\n"
  4559. " B>*>(\n"
  4560. "\n"
  4561. " );"));
  4562. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4563. " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
  4564. FormatStyle AlwaysBreak = getLLVMStyle();
  4565. AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
  4566. verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
  4567. verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
  4568. verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
  4569. verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4570. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
  4571. " ccccccccccccccccccccccccccccccccccccccccccccccc);");
  4572. verifyFormat("template <template <typename> class Fooooooo,\n"
  4573. " template <typename> class Baaaaaaar>\n"
  4574. "struct C {};",
  4575. AlwaysBreak);
  4576. verifyFormat("template <typename T> // T can be A, B or C.\n"
  4577. "struct C {};",
  4578. AlwaysBreak);
  4579. }
  4580. TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
  4581. verifyFormat(
  4582. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4583. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4584. verifyFormat(
  4585. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4586. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4587. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  4588. // FIXME: Should we have the extra indent after the second break?
  4589. verifyFormat(
  4590. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4591. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4592. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4593. verifyFormat(
  4594. "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
  4595. " cccccccccccccccccccccccccccccccccccccccccccccc());");
  4596. // Breaking at nested name specifiers is generally not desirable.
  4597. verifyFormat(
  4598. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4599. " aaaaaaaaaaaaaaaaaaaaaaa);");
  4600. verifyFormat(
  4601. "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4602. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4603. " aaaaaaaaaaaaaaaaaaaaa);",
  4604. getLLVMStyleWithColumns(74));
  4605. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4606. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4607. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4608. }
  4609. TEST_F(FormatTest, UnderstandsTemplateParameters) {
  4610. verifyFormat("A<int> a;");
  4611. verifyFormat("A<A<A<int>>> a;");
  4612. verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
  4613. verifyFormat("bool x = a < 1 || 2 > a;");
  4614. verifyFormat("bool x = 5 < f<int>();");
  4615. verifyFormat("bool x = f<int>() > 5;");
  4616. verifyFormat("bool x = 5 < a<int>::x;");
  4617. verifyFormat("bool x = a < 4 ? a > 2 : false;");
  4618. verifyFormat("bool x = f() ? a < 2 : a > 2;");
  4619. verifyGoogleFormat("A<A<int>> a;");
  4620. verifyGoogleFormat("A<A<A<int>>> a;");
  4621. verifyGoogleFormat("A<A<A<A<int>>>> a;");
  4622. verifyGoogleFormat("A<A<int> > a;");
  4623. verifyGoogleFormat("A<A<A<int> > > a;");
  4624. verifyGoogleFormat("A<A<A<A<int> > > > a;");
  4625. verifyGoogleFormat("A<::A<int>> a;");
  4626. verifyGoogleFormat("A<::A> a;");
  4627. verifyGoogleFormat("A< ::A> a;");
  4628. verifyGoogleFormat("A< ::A<int> > a;");
  4629. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
  4630. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
  4631. EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
  4632. EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
  4633. verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
  4634. verifyFormat("test >> a >> b;");
  4635. verifyFormat("test << a >> b;");
  4636. verifyFormat("f<int>();");
  4637. verifyFormat("template <typename T> void f() {}");
  4638. verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
  4639. // Not template parameters.
  4640. verifyFormat("return a < b && c > d;");
  4641. verifyFormat("void f() {\n"
  4642. " while (a < b && c > d) {\n"
  4643. " }\n"
  4644. "}");
  4645. verifyFormat("template <typename... Types>\n"
  4646. "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
  4647. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4648. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
  4649. getLLVMStyleWithColumns(60));
  4650. verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
  4651. verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
  4652. }
  4653. TEST_F(FormatTest, UnderstandsBinaryOperators) {
  4654. verifyFormat("COMPARE(a, ==, b);");
  4655. }
  4656. TEST_F(FormatTest, UnderstandsPointersToMembers) {
  4657. verifyFormat("int A::*x;");
  4658. verifyFormat("int (S::*func)(void *);");
  4659. verifyFormat("void f() { int (S::*func)(void *); }");
  4660. verifyFormat("typedef bool *(Class::*Member)() const;");
  4661. verifyFormat("void f() {\n"
  4662. " (a->*f)();\n"
  4663. " a->*x;\n"
  4664. " (a.*f)();\n"
  4665. " ((*a).*f)();\n"
  4666. " a.*x;\n"
  4667. "}");
  4668. verifyFormat("void f() {\n"
  4669. " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  4670. " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
  4671. "}");
  4672. verifyFormat(
  4673. "(aaaaaaaaaa->*bbbbbbb)(\n"
  4674. " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  4675. FormatStyle Style = getLLVMStyle();
  4676. Style.PointerAlignment = FormatStyle::PAS_Left;
  4677. verifyFormat("typedef bool* (Class::*Member)() const;", Style);
  4678. }
  4679. TEST_F(FormatTest, UnderstandsUnaryOperators) {
  4680. verifyFormat("int a = -2;");
  4681. verifyFormat("f(-1, -2, -3);");
  4682. verifyFormat("a[-1] = 5;");
  4683. verifyFormat("int a = 5 + -2;");
  4684. verifyFormat("if (i == -1) {\n}");
  4685. verifyFormat("if (i != -1) {\n}");
  4686. verifyFormat("if (i > -1) {\n}");
  4687. verifyFormat("if (i < -1) {\n}");
  4688. verifyFormat("++(a->f());");
  4689. verifyFormat("--(a->f());");
  4690. verifyFormat("(a->f())++;");
  4691. verifyFormat("a[42]++;");
  4692. verifyFormat("if (!(a->f())) {\n}");
  4693. verifyFormat("a-- > b;");
  4694. verifyFormat("b ? -a : c;");
  4695. verifyFormat("n * sizeof char16;");
  4696. verifyFormat("n * alignof char16;", getGoogleStyle());
  4697. verifyFormat("sizeof(char);");
  4698. verifyFormat("alignof(char);", getGoogleStyle());
  4699. verifyFormat("return -1;");
  4700. verifyFormat("switch (a) {\n"
  4701. "case -1:\n"
  4702. " break;\n"
  4703. "}");
  4704. verifyFormat("#define X -1");
  4705. verifyFormat("#define X -kConstant");
  4706. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
  4707. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
  4708. verifyFormat("int a = /* confusing comment */ -1;");
  4709. // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
  4710. verifyFormat("int a = i /* confusing comment */++;");
  4711. }
  4712. TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
  4713. verifyFormat("if (!aaaaaaaaaa( // break\n"
  4714. " aaaaa)) {\n"
  4715. "}");
  4716. verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
  4717. " aaaaa));");
  4718. verifyFormat("*aaa = aaaaaaa( // break\n"
  4719. " bbbbbb);");
  4720. }
  4721. TEST_F(FormatTest, UnderstandsOverloadedOperators) {
  4722. verifyFormat("bool operator<();");
  4723. verifyFormat("bool operator>();");
  4724. verifyFormat("bool operator=();");
  4725. verifyFormat("bool operator==();");
  4726. verifyFormat("bool operator!=();");
  4727. verifyFormat("int operator+();");
  4728. verifyFormat("int operator++();");
  4729. verifyFormat("bool operator();");
  4730. verifyFormat("bool operator()();");
  4731. verifyFormat("bool operator[]();");
  4732. verifyFormat("operator bool();");
  4733. verifyFormat("operator int();");
  4734. verifyFormat("operator void *();");
  4735. verifyFormat("operator SomeType<int>();");
  4736. verifyFormat("operator SomeType<int, int>();");
  4737. verifyFormat("operator SomeType<SomeType<int>>();");
  4738. verifyFormat("void *operator new(std::size_t size);");
  4739. verifyFormat("void *operator new[](std::size_t size);");
  4740. verifyFormat("void operator delete(void *ptr);");
  4741. verifyFormat("void operator delete[](void *ptr);");
  4742. verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
  4743. "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
  4744. verifyFormat(
  4745. "ostream &operator<<(ostream &OutputStream,\n"
  4746. " SomeReallyLongType WithSomeReallyLongValue);");
  4747. verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
  4748. " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
  4749. " return left.group < right.group;\n"
  4750. "}");
  4751. verifyFormat("SomeType &operator=(const SomeType &S);");
  4752. verifyFormat("f.template operator()<int>();");
  4753. verifyGoogleFormat("operator void*();");
  4754. verifyGoogleFormat("operator SomeType<SomeType<int>>();");
  4755. verifyGoogleFormat("operator ::A();");
  4756. verifyFormat("using A::operator+;");
  4757. verifyFormat("Deleted &operator=(const Deleted &)& = default;");
  4758. verifyFormat("Deleted &operator=(const Deleted &)&& = delete;");
  4759. verifyGoogleFormat("Deleted& operator=(const Deleted&)& = default;");
  4760. verifyGoogleFormat("Deleted& operator=(const Deleted&)&& = delete;");
  4761. verifyFormat("string // break\n"
  4762. "operator()() & {}");
  4763. verifyFormat("string // break\n"
  4764. "operator()() && {}");
  4765. }
  4766. TEST_F(FormatTest, UnderstandsNewAndDelete) {
  4767. verifyFormat("void f() {\n"
  4768. " A *a = new A;\n"
  4769. " A *a = new (placement) A;\n"
  4770. " delete a;\n"
  4771. " delete (A *)a;\n"
  4772. "}");
  4773. verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
  4774. " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
  4775. verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4776. " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
  4777. " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
  4778. verifyFormat("delete[] h->p;");
  4779. }
  4780. TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
  4781. verifyFormat("int *f(int *a) {}");
  4782. verifyFormat("int main(int argc, char **argv) {}");
  4783. verifyFormat("Test::Test(int b) : a(b * b) {}");
  4784. verifyIndependentOfContext("f(a, *a);");
  4785. verifyFormat("void g() { f(*a); }");
  4786. verifyIndependentOfContext("int a = b * 10;");
  4787. verifyIndependentOfContext("int a = 10 * b;");
  4788. verifyIndependentOfContext("int a = b * c;");
  4789. verifyIndependentOfContext("int a += b * c;");
  4790. verifyIndependentOfContext("int a -= b * c;");
  4791. verifyIndependentOfContext("int a *= b * c;");
  4792. verifyIndependentOfContext("int a /= b * c;");
  4793. verifyIndependentOfContext("int a = *b;");
  4794. verifyIndependentOfContext("int a = *b * c;");
  4795. verifyIndependentOfContext("int a = b * *c;");
  4796. verifyIndependentOfContext("return 10 * b;");
  4797. verifyIndependentOfContext("return *b * *c;");
  4798. verifyIndependentOfContext("return a & ~b;");
  4799. verifyIndependentOfContext("f(b ? *c : *d);");
  4800. verifyIndependentOfContext("int a = b ? *c : *d;");
  4801. verifyIndependentOfContext("*b = a;");
  4802. verifyIndependentOfContext("a * ~b;");
  4803. verifyIndependentOfContext("a * !b;");
  4804. verifyIndependentOfContext("a * +b;");
  4805. verifyIndependentOfContext("a * -b;");
  4806. verifyIndependentOfContext("a * ++b;");
  4807. verifyIndependentOfContext("a * --b;");
  4808. verifyIndependentOfContext("a[4] * b;");
  4809. verifyIndependentOfContext("a[a * a] = 1;");
  4810. verifyIndependentOfContext("f() * b;");
  4811. verifyIndependentOfContext("a * [self dostuff];");
  4812. verifyIndependentOfContext("int x = a * (a + b);");
  4813. verifyIndependentOfContext("(a *)(a + b);");
  4814. verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
  4815. verifyIndependentOfContext("int *pa = (int *)&a;");
  4816. verifyIndependentOfContext("return sizeof(int **);");
  4817. verifyIndependentOfContext("return sizeof(int ******);");
  4818. verifyIndependentOfContext("return (int **&)a;");
  4819. verifyIndependentOfContext("f((*PointerToArray)[10]);");
  4820. verifyFormat("void f(Type (*parameter)[10]) {}");
  4821. verifyGoogleFormat("return sizeof(int**);");
  4822. verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
  4823. verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
  4824. verifyFormat("auto a = [](int **&, int ***) {};");
  4825. verifyFormat("auto PointerBinding = [](const char *S) {};");
  4826. verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
  4827. verifyFormat("[](const decltype(*a) &value) {}");
  4828. verifyIndependentOfContext("typedef void (*f)(int *a);");
  4829. verifyIndependentOfContext("int i{a * b};");
  4830. verifyIndependentOfContext("aaa && aaa->f();");
  4831. verifyIndependentOfContext("int x = ~*p;");
  4832. verifyFormat("Constructor() : a(a), area(width * height) {}");
  4833. verifyFormat("Constructor() : a(a), area(a, width * height) {}");
  4834. verifyFormat("void f() { f(a, c * d); }");
  4835. verifyIndependentOfContext("InvalidRegions[*R] = 0;");
  4836. verifyIndependentOfContext("A<int *> a;");
  4837. verifyIndependentOfContext("A<int **> a;");
  4838. verifyIndependentOfContext("A<int *, int *> a;");
  4839. verifyIndependentOfContext("A<int *[]> a;");
  4840. verifyIndependentOfContext(
  4841. "const char *const p = reinterpret_cast<const char *const>(q);");
  4842. verifyIndependentOfContext("A<int **, int **> a;");
  4843. verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
  4844. verifyFormat("for (char **a = b; *a; ++a) {\n}");
  4845. verifyFormat("for (; a && b;) {\n}");
  4846. verifyFormat("bool foo = true && [] { return false; }();");
  4847. verifyFormat(
  4848. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4849. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4850. verifyGoogleFormat("**outparam = 1;");
  4851. verifyGoogleFormat("*outparam = a * b;");
  4852. verifyGoogleFormat("int main(int argc, char** argv) {}");
  4853. verifyGoogleFormat("A<int*> a;");
  4854. verifyGoogleFormat("A<int**> a;");
  4855. verifyGoogleFormat("A<int*, int*> a;");
  4856. verifyGoogleFormat("A<int**, int**> a;");
  4857. verifyGoogleFormat("f(b ? *c : *d);");
  4858. verifyGoogleFormat("int a = b ? *c : *d;");
  4859. verifyGoogleFormat("Type* t = **x;");
  4860. verifyGoogleFormat("Type* t = *++*x;");
  4861. verifyGoogleFormat("*++*x;");
  4862. verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
  4863. verifyGoogleFormat("Type* t = x++ * y;");
  4864. verifyGoogleFormat(
  4865. "const char* const p = reinterpret_cast<const char* const>(q);");
  4866. verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
  4867. verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
  4868. verifyGoogleFormat("template <typename T>\n"
  4869. "void f(int i = 0, SomeType** temps = NULL);");
  4870. FormatStyle Left = getLLVMStyle();
  4871. Left.PointerAlignment = FormatStyle::PAS_Left;
  4872. verifyFormat("x = *a(x) = *a(y);", Left);
  4873. verifyIndependentOfContext("a = *(x + y);");
  4874. verifyIndependentOfContext("a = &(x + y);");
  4875. verifyIndependentOfContext("*(x + y).call();");
  4876. verifyIndependentOfContext("&(x + y)->call();");
  4877. verifyFormat("void f() { &(*I).first; }");
  4878. verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
  4879. verifyFormat(
  4880. "int *MyValues = {\n"
  4881. " *A, // Operator detection might be confused by the '{'\n"
  4882. " *BB // Operator detection might be confused by previous comment\n"
  4883. "};");
  4884. verifyIndependentOfContext("if (int *a = &b)");
  4885. verifyIndependentOfContext("if (int &a = *b)");
  4886. verifyIndependentOfContext("if (a & b[i])");
  4887. verifyIndependentOfContext("if (a::b::c::d & b[i])");
  4888. verifyIndependentOfContext("if (*b[i])");
  4889. verifyIndependentOfContext("if (int *a = (&b))");
  4890. verifyIndependentOfContext("while (int *a = &b)");
  4891. verifyIndependentOfContext("size = sizeof *a;");
  4892. verifyFormat("void f() {\n"
  4893. " for (const int &v : Values) {\n"
  4894. " }\n"
  4895. "}");
  4896. verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
  4897. verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
  4898. verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
  4899. verifyFormat("#define A (!a * b)");
  4900. verifyFormat("#define MACRO \\\n"
  4901. " int *i = a * b; \\\n"
  4902. " void f(a *b);",
  4903. getLLVMStyleWithColumns(19));
  4904. verifyIndependentOfContext("A = new SomeType *[Length];");
  4905. verifyIndependentOfContext("A = new SomeType *[Length]();");
  4906. verifyIndependentOfContext("T **t = new T *;");
  4907. verifyIndependentOfContext("T **t = new T *();");
  4908. verifyGoogleFormat("A = new SomeType* [Length]();");
  4909. verifyGoogleFormat("A = new SomeType* [Length];");
  4910. verifyGoogleFormat("T** t = new T*;");
  4911. verifyGoogleFormat("T** t = new T*();");
  4912. FormatStyle PointerLeft = getLLVMStyle();
  4913. PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
  4914. verifyFormat("delete *x;", PointerLeft);
  4915. verifyFormat("STATIC_ASSERT((a & b) == 0);");
  4916. verifyFormat("STATIC_ASSERT(0 == (a & b));");
  4917. verifyFormat("template <bool a, bool b> "
  4918. "typename t::if<x && y>::type f() {}");
  4919. verifyFormat("template <int *y> f() {}");
  4920. verifyFormat("vector<int *> v;");
  4921. verifyFormat("vector<int *const> v;");
  4922. verifyFormat("vector<int *const **const *> v;");
  4923. verifyFormat("vector<int *volatile> v;");
  4924. verifyFormat("vector<a * b> v;");
  4925. verifyFormat("foo<b && false>();");
  4926. verifyFormat("foo<b & 1>();");
  4927. verifyFormat("decltype(*::std::declval<const T &>()) void F();");
  4928. verifyFormat(
  4929. "template <class T, class = typename std::enable_if<\n"
  4930. " std::is_integral<T>::value &&\n"
  4931. " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
  4932. "void F();",
  4933. getLLVMStyleWithColumns(76));
  4934. verifyFormat(
  4935. "template <class T,\n"
  4936. " class = typename ::std::enable_if<\n"
  4937. " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
  4938. "void F();",
  4939. getGoogleStyleWithColumns(68));
  4940. verifyIndependentOfContext("MACRO(int *i);");
  4941. verifyIndependentOfContext("MACRO(auto *a);");
  4942. verifyIndependentOfContext("MACRO(const A *a);");
  4943. verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
  4944. // FIXME: Is there a way to make this work?
  4945. // verifyIndependentOfContext("MACRO(A *a);");
  4946. verifyFormat("DatumHandle const *operator->() const { return input_; }");
  4947. EXPECT_EQ("#define OP(x) \\\n"
  4948. " ostream &operator<<(ostream &s, const A &a) { \\\n"
  4949. " return s << a.DebugString(); \\\n"
  4950. " }",
  4951. format("#define OP(x) \\\n"
  4952. " ostream &operator<<(ostream &s, const A &a) { \\\n"
  4953. " return s << a.DebugString(); \\\n"
  4954. " }",
  4955. getLLVMStyleWithColumns(50)));
  4956. // FIXME: We cannot handle this case yet; we might be able to figure out that
  4957. // foo<x> d > v; doesn't make sense.
  4958. verifyFormat("foo<a<b && c> d> v;");
  4959. FormatStyle PointerMiddle = getLLVMStyle();
  4960. PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
  4961. verifyFormat("delete *x;", PointerMiddle);
  4962. verifyFormat("int * x;", PointerMiddle);
  4963. verifyFormat("template <int * y> f() {}", PointerMiddle);
  4964. verifyFormat("int * f(int * a) {}", PointerMiddle);
  4965. verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
  4966. verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
  4967. verifyFormat("A<int *> a;", PointerMiddle);
  4968. verifyFormat("A<int **> a;", PointerMiddle);
  4969. verifyFormat("A<int *, int *> a;", PointerMiddle);
  4970. verifyFormat("A<int * []> a;", PointerMiddle);
  4971. verifyFormat("A = new SomeType * [Length]();", PointerMiddle);
  4972. verifyFormat("A = new SomeType * [Length];", PointerMiddle);
  4973. verifyFormat("T ** t = new T *;", PointerMiddle);
  4974. }
  4975. TEST_F(FormatTest, UnderstandsAttributes) {
  4976. verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
  4977. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
  4978. "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
  4979. }
  4980. TEST_F(FormatTest, UnderstandsEllipsis) {
  4981. verifyFormat("int printf(const char *fmt, ...);");
  4982. verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
  4983. verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
  4984. FormatStyle PointersLeft = getLLVMStyle();
  4985. PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
  4986. verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
  4987. }
  4988. TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
  4989. EXPECT_EQ("int *a;\n"
  4990. "int *a;\n"
  4991. "int *a;",
  4992. format("int *a;\n"
  4993. "int* a;\n"
  4994. "int *a;",
  4995. getGoogleStyle()));
  4996. EXPECT_EQ("int* a;\n"
  4997. "int* a;\n"
  4998. "int* a;",
  4999. format("int* a;\n"
  5000. "int* a;\n"
  5001. "int *a;",
  5002. getGoogleStyle()));
  5003. EXPECT_EQ("int *a;\n"
  5004. "int *a;\n"
  5005. "int *a;",
  5006. format("int *a;\n"
  5007. "int * a;\n"
  5008. "int * a;",
  5009. getGoogleStyle()));
  5010. }
  5011. TEST_F(FormatTest, UnderstandsRvalueReferences) {
  5012. verifyFormat("int f(int &&a) {}");
  5013. verifyFormat("int f(int a, char &&b) {}");
  5014. verifyFormat("void f() { int &&a = b; }");
  5015. verifyGoogleFormat("int f(int a, char&& b) {}");
  5016. verifyGoogleFormat("void f() { int&& a = b; }");
  5017. verifyIndependentOfContext("A<int &&> a;");
  5018. verifyIndependentOfContext("A<int &&, int &&> a;");
  5019. verifyGoogleFormat("A<int&&> a;");
  5020. verifyGoogleFormat("A<int&&, int&&> a;");
  5021. // Not rvalue references:
  5022. verifyFormat("template <bool B, bool C> class A {\n"
  5023. " static_assert(B && C, \"Something is wrong\");\n"
  5024. "};");
  5025. verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
  5026. verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
  5027. verifyFormat("#define A(a, b) (a && b)");
  5028. }
  5029. TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
  5030. verifyFormat("void f() {\n"
  5031. " x[aaaaaaaaa -\n"
  5032. " b] = 23;\n"
  5033. "}",
  5034. getLLVMStyleWithColumns(15));
  5035. }
  5036. TEST_F(FormatTest, FormatsCasts) {
  5037. verifyFormat("Type *A = static_cast<Type *>(P);");
  5038. verifyFormat("Type *A = (Type *)P;");
  5039. verifyFormat("Type *A = (vector<Type *, int *>)P;");
  5040. verifyFormat("int a = (int)(2.0f);");
  5041. verifyFormat("int a = (int)2.0f;");
  5042. verifyFormat("x[(int32)y];");
  5043. verifyFormat("x = (int32)y;");
  5044. verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
  5045. verifyFormat("int a = (int)*b;");
  5046. verifyFormat("int a = (int)2.0f;");
  5047. verifyFormat("int a = (int)~0;");
  5048. verifyFormat("int a = (int)++a;");
  5049. verifyFormat("int a = (int)sizeof(int);");
  5050. verifyFormat("int a = (int)+2;");
  5051. verifyFormat("my_int a = (my_int)2.0f;");
  5052. verifyFormat("my_int a = (my_int)sizeof(int);");
  5053. verifyFormat("return (my_int)aaa;");
  5054. verifyFormat("#define x ((int)-1)");
  5055. verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
  5056. verifyFormat("#define p(q) ((int *)&q)");
  5057. verifyFormat("fn(a)(b) + 1;");
  5058. verifyFormat("void f() { my_int a = (my_int)*b; }");
  5059. verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
  5060. verifyFormat("my_int a = (my_int)~0;");
  5061. verifyFormat("my_int a = (my_int)++a;");
  5062. verifyFormat("my_int a = (my_int)-2;");
  5063. verifyFormat("my_int a = (my_int)1;");
  5064. verifyFormat("my_int a = (my_int *)1;");
  5065. verifyFormat("my_int a = (const my_int)-1;");
  5066. verifyFormat("my_int a = (const my_int *)-1;");
  5067. verifyFormat("my_int a = (my_int)(my_int)-1;");
  5068. // FIXME: single value wrapped with paren will be treated as cast.
  5069. verifyFormat("void f(int i = (kValue)*kMask) {}");
  5070. verifyFormat("{ (void)F; }");
  5071. // Don't break after a cast's
  5072. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  5073. " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
  5074. " bbbbbbbbbbbbbbbbbbbbbb);");
  5075. // These are not casts.
  5076. verifyFormat("void f(int *) {}");
  5077. verifyFormat("f(foo)->b;");
  5078. verifyFormat("f(foo).b;");
  5079. verifyFormat("f(foo)(b);");
  5080. verifyFormat("f(foo)[b];");
  5081. verifyFormat("[](foo) { return 4; }(bar);");
  5082. verifyFormat("(*funptr)(foo)[4];");
  5083. verifyFormat("funptrs[4](foo)[4];");
  5084. verifyFormat("void f(int *);");
  5085. verifyFormat("void f(int *) = 0;");
  5086. verifyFormat("void f(SmallVector<int>) {}");
  5087. verifyFormat("void f(SmallVector<int>);");
  5088. verifyFormat("void f(SmallVector<int>) = 0;");
  5089. verifyFormat("void f(int i = (kA * kB) & kMask) {}");
  5090. verifyFormat("int a = sizeof(int) * b;");
  5091. verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
  5092. verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
  5093. verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
  5094. verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
  5095. // These are not casts, but at some point were confused with casts.
  5096. verifyFormat("virtual void foo(int *) override;");
  5097. verifyFormat("virtual void foo(char &) const;");
  5098. verifyFormat("virtual void foo(int *a, char *) const;");
  5099. verifyFormat("int a = sizeof(int *) + b;");
  5100. verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
  5101. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
  5102. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  5103. // FIXME: The indentation here is not ideal.
  5104. verifyFormat(
  5105. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5106. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
  5107. " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
  5108. }
  5109. TEST_F(FormatTest, FormatsFunctionTypes) {
  5110. verifyFormat("A<bool()> a;");
  5111. verifyFormat("A<SomeType()> a;");
  5112. verifyFormat("A<void (*)(int, std::string)> a;");
  5113. verifyFormat("A<void *(int)>;");
  5114. verifyFormat("void *(*a)(int *, SomeType *);");
  5115. verifyFormat("int (*func)(void *);");
  5116. verifyFormat("void f() { int (*func)(void *); }");
  5117. verifyFormat("template <class CallbackClass>\n"
  5118. "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
  5119. verifyGoogleFormat("A<void*(int*, SomeType*)>;");
  5120. verifyGoogleFormat("void* (*a)(int);");
  5121. verifyGoogleFormat(
  5122. "template <class CallbackClass>\n"
  5123. "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
  5124. // Other constructs can look somewhat like function types:
  5125. verifyFormat("A<sizeof(*x)> a;");
  5126. verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
  5127. verifyFormat("some_var = function(*some_pointer_var)[0];");
  5128. verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
  5129. }
  5130. TEST_F(FormatTest, BreaksLongVariableDeclarations) {
  5131. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5132. " LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5133. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
  5134. " LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5135. // Different ways of ()-initializiation.
  5136. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5137. " LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
  5138. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5139. " LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
  5140. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5141. " LoooooooooooooooooooooooooooooooooooooooongVariable({});");
  5142. }
  5143. TEST_F(FormatTest, BreaksLongDeclarations) {
  5144. verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
  5145. " AnotherNameForTheLongType;");
  5146. verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
  5147. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  5148. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5149. "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
  5150. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5151. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5152. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
  5153. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5154. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  5155. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5156. verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
  5157. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5158. FormatStyle Indented = getLLVMStyle();
  5159. Indented.IndentWrappedFunctionNames = true;
  5160. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5161. " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
  5162. Indented);
  5163. verifyFormat(
  5164. "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5165. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5166. Indented);
  5167. verifyFormat(
  5168. "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  5169. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5170. Indented);
  5171. verifyFormat(
  5172. "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
  5173. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5174. Indented);
  5175. // FIXME: Without the comment, this breaks after "(".
  5176. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
  5177. " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
  5178. getGoogleStyle());
  5179. verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
  5180. " int LoooooooooooooooooooongParam2) {}");
  5181. verifyFormat(
  5182. "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
  5183. " SourceLocation L, IdentifierIn *II,\n"
  5184. " Type *T) {}");
  5185. verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
  5186. "ReallyReallyLongFunctionName(\n"
  5187. " const std::string &SomeParameter,\n"
  5188. " const SomeType<string, SomeOtherTemplateParameter> &\n"
  5189. " ReallyReallyLongParameterName,\n"
  5190. " const SomeType<string, SomeOtherTemplateParameter> &\n"
  5191. " AnotherLongParameterName) {}");
  5192. verifyFormat("template <typename A>\n"
  5193. "SomeLoooooooooooooooooooooongType<\n"
  5194. " typename some_namespace::SomeOtherType<A>::Type>\n"
  5195. "Function() {}");
  5196. verifyGoogleFormat(
  5197. "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
  5198. " aaaaaaaaaaaaaaaaaaaaaaa;");
  5199. verifyGoogleFormat(
  5200. "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
  5201. " SourceLocation L) {}");
  5202. verifyGoogleFormat(
  5203. "some_namespace::LongReturnType\n"
  5204. "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
  5205. " int first_long_parameter, int second_parameter) {}");
  5206. verifyGoogleFormat("template <typename T>\n"
  5207. "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  5208. "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
  5209. verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5210. " int aaaaaaaaaaaaaaaaaaaaaaa);");
  5211. verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  5212. " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  5213. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5214. }
  5215. TEST_F(FormatTest, FormatsArrays) {
  5216. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5217. " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
  5218. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5219. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  5220. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5221. " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
  5222. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5223. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5224. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  5225. verifyFormat(
  5226. "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
  5227. " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5228. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  5229. verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
  5230. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
  5231. verifyFormat(
  5232. "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
  5233. " .aaaaaaa[0]\n"
  5234. " .aaaaaaaaaaaaaaaaaaaaaa();");
  5235. verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
  5236. }
  5237. TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
  5238. verifyFormat("(a)->b();");
  5239. verifyFormat("--a;");
  5240. }
  5241. TEST_F(FormatTest, HandlesIncludeDirectives) {
  5242. verifyFormat("#include <string>\n"
  5243. "#include <a/b/c.h>\n"
  5244. "#include \"a/b/string\"\n"
  5245. "#include \"string.h\"\n"
  5246. "#include \"string.h\"\n"
  5247. "#include <a-a>\n"
  5248. "#include < path with space >\n"
  5249. "#include \"abc.h\" // this is included for ABC\n"
  5250. "#include \"some long include\" // with a comment\n"
  5251. "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
  5252. getLLVMStyleWithColumns(35));
  5253. EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\""));
  5254. EXPECT_EQ("#include <a>", format("#include<a>"));
  5255. verifyFormat("#import <string>");
  5256. verifyFormat("#import <a/b/c.h>");
  5257. verifyFormat("#import \"a/b/string\"");
  5258. verifyFormat("#import \"string.h\"");
  5259. verifyFormat("#import \"string.h\"");
  5260. verifyFormat("#if __has_include(<strstream>)\n"
  5261. "#include <strstream>\n"
  5262. "#endif");
  5263. verifyFormat("#define MY_IMPORT <a/b>");
  5264. // Protocol buffer definition or missing "#".
  5265. verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
  5266. getLLVMStyleWithColumns(30));
  5267. FormatStyle Style = getLLVMStyle();
  5268. Style.AlwaysBreakBeforeMultilineStrings = true;
  5269. Style.ColumnLimit = 0;
  5270. verifyFormat("#import \"abc.h\"", Style);
  5271. // But 'import' might also be a regular C++ namespace.
  5272. verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5273. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5274. }
  5275. //===----------------------------------------------------------------------===//
  5276. // Error recovery tests.
  5277. //===----------------------------------------------------------------------===//
  5278. TEST_F(FormatTest, IncompleteParameterLists) {
  5279. FormatStyle NoBinPacking = getLLVMStyle();
  5280. NoBinPacking.BinPackParameters = false;
  5281. verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
  5282. " double *min_x,\n"
  5283. " double *max_x,\n"
  5284. " double *min_y,\n"
  5285. " double *max_y,\n"
  5286. " double *min_z,\n"
  5287. " double *max_z, ) {}",
  5288. NoBinPacking);
  5289. }
  5290. TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
  5291. verifyFormat("void f() { return; }\n42");
  5292. verifyFormat("void f() {\n"
  5293. " if (0)\n"
  5294. " return;\n"
  5295. "}\n"
  5296. "42");
  5297. verifyFormat("void f() { return }\n42");
  5298. verifyFormat("void f() {\n"
  5299. " if (0)\n"
  5300. " return\n"
  5301. "}\n"
  5302. "42");
  5303. }
  5304. TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
  5305. EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
  5306. EXPECT_EQ("void f() {\n"
  5307. " if (a)\n"
  5308. " return\n"
  5309. "}",
  5310. format("void f ( ) { if ( a ) return }"));
  5311. EXPECT_EQ("namespace N {\n"
  5312. "void f()\n"
  5313. "}",
  5314. format("namespace N { void f() }"));
  5315. EXPECT_EQ("namespace N {\n"
  5316. "void f() {}\n"
  5317. "void g()\n"
  5318. "}",
  5319. format("namespace N { void f( ) { } void g( ) }"));
  5320. }
  5321. TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
  5322. verifyFormat("int aaaaaaaa =\n"
  5323. " // Overlylongcomment\n"
  5324. " b;",
  5325. getLLVMStyleWithColumns(20));
  5326. verifyFormat("function(\n"
  5327. " ShortArgument,\n"
  5328. " LoooooooooooongArgument);\n",
  5329. getLLVMStyleWithColumns(20));
  5330. }
  5331. TEST_F(FormatTest, IncorrectAccessSpecifier) {
  5332. verifyFormat("public:");
  5333. verifyFormat("class A {\n"
  5334. "public\n"
  5335. " void f() {}\n"
  5336. "};");
  5337. verifyFormat("public\n"
  5338. "int qwerty;");
  5339. verifyFormat("public\n"
  5340. "B {}");
  5341. verifyFormat("public\n"
  5342. "{}");
  5343. verifyFormat("public\n"
  5344. "B { int x; }");
  5345. }
  5346. TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
  5347. verifyFormat("{");
  5348. verifyFormat("#})");
  5349. }
  5350. TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
  5351. verifyFormat("do {\n}");
  5352. verifyFormat("do {\n}\n"
  5353. "f();");
  5354. verifyFormat("do {\n}\n"
  5355. "wheeee(fun);");
  5356. verifyFormat("do {\n"
  5357. " f();\n"
  5358. "}");
  5359. }
  5360. TEST_F(FormatTest, IncorrectCodeMissingParens) {
  5361. verifyFormat("if {\n foo;\n foo();\n}");
  5362. verifyFormat("switch {\n foo;\n foo();\n}");
  5363. verifyFormat("for {\n foo;\n foo();\n}");
  5364. verifyFormat("while {\n foo;\n foo();\n}");
  5365. verifyFormat("do {\n foo;\n foo();\n} while;");
  5366. }
  5367. TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
  5368. verifyFormat("namespace {\n"
  5369. "class Foo { Foo (\n"
  5370. "};\n"
  5371. "} // comment");
  5372. }
  5373. TEST_F(FormatTest, IncorrectCodeErrorDetection) {
  5374. EXPECT_EQ("{\n {}\n", format("{\n{\n}\n"));
  5375. EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
  5376. EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
  5377. EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n"));
  5378. EXPECT_EQ("{\n"
  5379. " {\n"
  5380. " breakme(\n"
  5381. " qwe);\n"
  5382. " }\n",
  5383. format("{\n"
  5384. " {\n"
  5385. " breakme(qwe);\n"
  5386. "}\n",
  5387. getLLVMStyleWithColumns(10)));
  5388. }
  5389. TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
  5390. verifyFormat("int x = {\n"
  5391. " avariable,\n"
  5392. " b(alongervariable)};",
  5393. getLLVMStyleWithColumns(25));
  5394. }
  5395. TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
  5396. verifyFormat("return (a)(b){1, 2, 3};");
  5397. }
  5398. TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
  5399. verifyFormat("vector<int> x{1, 2, 3, 4};");
  5400. verifyFormat("vector<int> x{\n"
  5401. " 1, 2, 3, 4,\n"
  5402. "};");
  5403. verifyFormat("vector<T> x{{}, {}, {}, {}};");
  5404. verifyFormat("f({1, 2});");
  5405. verifyFormat("auto v = Foo{-1};");
  5406. verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
  5407. verifyFormat("Class::Class : member{1, 2, 3} {}");
  5408. verifyFormat("new vector<int>{1, 2, 3};");
  5409. verifyFormat("new int[3]{1, 2, 3};");
  5410. verifyFormat("new int{1};");
  5411. verifyFormat("return {arg1, arg2};");
  5412. verifyFormat("return {arg1, SomeType{parameter}};");
  5413. verifyFormat("int count = set<int>{f(), g(), h()}.size();");
  5414. verifyFormat("new T{arg1, arg2};");
  5415. verifyFormat("f(MyMap[{composite, key}]);");
  5416. verifyFormat("class Class {\n"
  5417. " T member = {arg1, arg2};\n"
  5418. "};");
  5419. verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
  5420. verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
  5421. verifyFormat("int a = std::is_integral<int>{} + 0;");
  5422. verifyFormat("int foo(int i) { return fo1{}(i); }");
  5423. verifyFormat("int foo(int i) { return fo1{}(i); }");
  5424. verifyFormat("auto i = decltype(x){};");
  5425. verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
  5426. verifyFormat("Node n{1, Node{1000}, //\n"
  5427. " 2};");
  5428. // In combination with BinPackParameters = false.
  5429. FormatStyle NoBinPacking = getLLVMStyle();
  5430. NoBinPacking.BinPackParameters = false;
  5431. verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
  5432. " bbbbb,\n"
  5433. " ccccc,\n"
  5434. " ddddd,\n"
  5435. " eeeee,\n"
  5436. " ffffff,\n"
  5437. " ggggg,\n"
  5438. " hhhhhh,\n"
  5439. " iiiiii,\n"
  5440. " jjjjjj,\n"
  5441. " kkkkkk};",
  5442. NoBinPacking);
  5443. verifyFormat("const Aaaaaa aaaaa = {\n"
  5444. " aaaaa,\n"
  5445. " bbbbb,\n"
  5446. " ccccc,\n"
  5447. " ddddd,\n"
  5448. " eeeee,\n"
  5449. " ffffff,\n"
  5450. " ggggg,\n"
  5451. " hhhhhh,\n"
  5452. " iiiiii,\n"
  5453. " jjjjjj,\n"
  5454. " kkkkkk,\n"
  5455. "};",
  5456. NoBinPacking);
  5457. verifyFormat(
  5458. "const Aaaaaa aaaaa = {\n"
  5459. " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n"
  5460. " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n"
  5461. " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
  5462. "};",
  5463. NoBinPacking);
  5464. // FIXME: The alignment of these trailing comments might be bad. Then again,
  5465. // this might be utterly useless in real code.
  5466. verifyFormat("Constructor::Constructor()\n"
  5467. " : some_value{ //\n"
  5468. " aaaaaaa, //\n"
  5469. " bbbbbbb} {}");
  5470. // In braced lists, the first comment is always assumed to belong to the
  5471. // first element. Thus, it can be moved to the next or previous line as
  5472. // appropriate.
  5473. EXPECT_EQ("function({// First element:\n"
  5474. " 1,\n"
  5475. " // Second element:\n"
  5476. " 2});",
  5477. format("function({\n"
  5478. " // First element:\n"
  5479. " 1,\n"
  5480. " // Second element:\n"
  5481. " 2});"));
  5482. EXPECT_EQ("std::vector<int> MyNumbers{\n"
  5483. " // First element:\n"
  5484. " 1,\n"
  5485. " // Second element:\n"
  5486. " 2};",
  5487. format("std::vector<int> MyNumbers{// First element:\n"
  5488. " 1,\n"
  5489. " // Second element:\n"
  5490. " 2};",
  5491. getLLVMStyleWithColumns(30)));
  5492. // A trailing comma should still lead to an enforced line break.
  5493. EXPECT_EQ("vector<int> SomeVector = {\n"
  5494. " // aaa\n"
  5495. " 1, 2,\n"
  5496. "};",
  5497. format("vector<int> SomeVector = { // aaa\n"
  5498. " 1, 2, };"));
  5499. FormatStyle ExtraSpaces = getLLVMStyle();
  5500. ExtraSpaces.Cpp11BracedListStyle = false;
  5501. ExtraSpaces.ColumnLimit = 75;
  5502. verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
  5503. verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
  5504. verifyFormat("f({ 1, 2 });", ExtraSpaces);
  5505. verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
  5506. verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
  5507. verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
  5508. verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
  5509. verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
  5510. verifyFormat("return { arg1, arg2 };", ExtraSpaces);
  5511. verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
  5512. verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
  5513. verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
  5514. verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
  5515. verifyFormat("class Class {\n"
  5516. " T member = { arg1, arg2 };\n"
  5517. "};",
  5518. ExtraSpaces);
  5519. verifyFormat(
  5520. "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5521. " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
  5522. " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  5523. " bbbbbbbbbbbbbbbbbbbb, bbbbb };",
  5524. ExtraSpaces);
  5525. verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
  5526. verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
  5527. ExtraSpaces);
  5528. verifyFormat(
  5529. "someFunction(OtherParam,\n"
  5530. " BracedList{ // comment 1 (Forcing interesting break)\n"
  5531. " param1, param2,\n"
  5532. " // comment 2\n"
  5533. " param3, param4 });",
  5534. ExtraSpaces);
  5535. verifyFormat(
  5536. "std::this_thread::sleep_for(\n"
  5537. " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
  5538. ExtraSpaces);
  5539. verifyFormat(
  5540. "std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
  5541. " aaaaaaa, aaaaaaaaaa, aaaaa, aaaaaaaaaaaaaaa, aaa, aaaaaaaaaa, a,\n"
  5542. " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaa,\n"
  5543. " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa, aaaaaaa, a};");
  5544. verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
  5545. }
  5546. TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
  5547. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5548. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5549. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5550. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5551. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5552. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  5553. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5554. " // line comment\n"
  5555. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5556. " 1, 22, 333, 4444, 55555,\n"
  5557. " // line comment\n"
  5558. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5559. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  5560. verifyFormat(
  5561. "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5562. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5563. " 1, 22, 333, 4444, 55555, 666666, // comment\n"
  5564. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  5565. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  5566. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  5567. " 7777777};");
  5568. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  5569. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  5570. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  5571. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  5572. " 1, 1, 1, 1};",
  5573. getLLVMStyleWithColumns(39));
  5574. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  5575. " 1, 1, 1, 1};",
  5576. getLLVMStyleWithColumns(38));
  5577. verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
  5578. " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
  5579. getLLVMStyleWithColumns(43));
  5580. // Trailing commas.
  5581. verifyFormat("vector<int> x = {\n"
  5582. " 1, 1, 1, 1, 1, 1, 1, 1,\n"
  5583. "};",
  5584. getLLVMStyleWithColumns(39));
  5585. verifyFormat("vector<int> x = {\n"
  5586. " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
  5587. "};",
  5588. getLLVMStyleWithColumns(39));
  5589. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  5590. " 1, 1, 1, 1,\n"
  5591. " /**/ /**/};",
  5592. getLLVMStyleWithColumns(39));
  5593. verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
  5594. " {aaaaaaaaaaaaaaaaaaa},\n"
  5595. " {aaaaaaaaaaaaaaaaaaaaa},\n"
  5596. " {aaaaaaaaaaaaaaaaa}};",
  5597. getLLVMStyleWithColumns(60));
  5598. // With nested lists, we should either format one item per line or all nested
  5599. // lists one one line.
  5600. // FIXME: For some nested lists, we can do better.
  5601. verifyFormat(
  5602. "SomeStruct my_struct_array = {\n"
  5603. " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
  5604. " aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
  5605. " {aaa, aaa},\n"
  5606. " {aaa, aaa},\n"
  5607. " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
  5608. " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5609. " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
  5610. // No column layout should be used here.
  5611. verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
  5612. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
  5613. verifyNoCrash("a<,");
  5614. }
  5615. TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
  5616. FormatStyle DoNotMerge = getLLVMStyle();
  5617. DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  5618. verifyFormat("void f() { return 42; }");
  5619. verifyFormat("void f() {\n"
  5620. " return 42;\n"
  5621. "}",
  5622. DoNotMerge);
  5623. verifyFormat("void f() {\n"
  5624. " // Comment\n"
  5625. "}");
  5626. verifyFormat("{\n"
  5627. "#error {\n"
  5628. " int a;\n"
  5629. "}");
  5630. verifyFormat("{\n"
  5631. " int a;\n"
  5632. "#error {\n"
  5633. "}");
  5634. verifyFormat("void f() {} // comment");
  5635. verifyFormat("void f() { int a; } // comment");
  5636. verifyFormat("void f() {\n"
  5637. "} // comment",
  5638. DoNotMerge);
  5639. verifyFormat("void f() {\n"
  5640. " int a;\n"
  5641. "} // comment",
  5642. DoNotMerge);
  5643. verifyFormat("void f() {\n"
  5644. "} // comment",
  5645. getLLVMStyleWithColumns(15));
  5646. verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
  5647. verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
  5648. verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
  5649. verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
  5650. verifyFormat("class C {\n"
  5651. " C()\n"
  5652. " : iiiiiiii(nullptr),\n"
  5653. " kkkkkkk(nullptr),\n"
  5654. " mmmmmmm(nullptr),\n"
  5655. " nnnnnnn(nullptr) {}\n"
  5656. "};",
  5657. getGoogleStyle());
  5658. FormatStyle NoColumnLimit = getLLVMStyle();
  5659. NoColumnLimit.ColumnLimit = 0;
  5660. EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
  5661. EXPECT_EQ("class C {\n"
  5662. " A() : b(0) {}\n"
  5663. "};", format("class C{A():b(0){}};", NoColumnLimit));
  5664. EXPECT_EQ("A()\n"
  5665. " : b(0) {\n"
  5666. "}",
  5667. format("A()\n:b(0)\n{\n}", NoColumnLimit));
  5668. FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
  5669. DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
  5670. FormatStyle::SFS_None;
  5671. EXPECT_EQ("A()\n"
  5672. " : b(0) {\n"
  5673. "}",
  5674. format("A():b(0){}", DoNotMergeNoColumnLimit));
  5675. EXPECT_EQ("A()\n"
  5676. " : b(0) {\n"
  5677. "}",
  5678. format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
  5679. verifyFormat("#define A \\\n"
  5680. " void f() { \\\n"
  5681. " int i; \\\n"
  5682. " }",
  5683. getLLVMStyleWithColumns(20));
  5684. verifyFormat("#define A \\\n"
  5685. " void f() { int i; }",
  5686. getLLVMStyleWithColumns(21));
  5687. verifyFormat("#define A \\\n"
  5688. " void f() { \\\n"
  5689. " int i; \\\n"
  5690. " } \\\n"
  5691. " int j;",
  5692. getLLVMStyleWithColumns(22));
  5693. verifyFormat("#define A \\\n"
  5694. " void f() { int i; } \\\n"
  5695. " int j;",
  5696. getLLVMStyleWithColumns(23));
  5697. }
  5698. TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
  5699. FormatStyle MergeInlineOnly = getLLVMStyle();
  5700. MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  5701. verifyFormat("class C {\n"
  5702. " int f() { return 42; }\n"
  5703. "};",
  5704. MergeInlineOnly);
  5705. verifyFormat("int f() {\n"
  5706. " return 42;\n"
  5707. "}",
  5708. MergeInlineOnly);
  5709. }
  5710. TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
  5711. // Elaborate type variable declarations.
  5712. verifyFormat("struct foo a = {bar};\nint n;");
  5713. verifyFormat("class foo a = {bar};\nint n;");
  5714. verifyFormat("union foo a = {bar};\nint n;");
  5715. // Elaborate types inside function definitions.
  5716. verifyFormat("struct foo f() {}\nint n;");
  5717. verifyFormat("class foo f() {}\nint n;");
  5718. verifyFormat("union foo f() {}\nint n;");
  5719. // Templates.
  5720. verifyFormat("template <class X> void f() {}\nint n;");
  5721. verifyFormat("template <struct X> void f() {}\nint n;");
  5722. verifyFormat("template <union X> void f() {}\nint n;");
  5723. // Actual definitions...
  5724. verifyFormat("struct {\n} n;");
  5725. verifyFormat(
  5726. "template <template <class T, class Y>, class Z> class X {\n} n;");
  5727. verifyFormat("union Z {\n int n;\n} x;");
  5728. verifyFormat("class MACRO Z {\n} n;");
  5729. verifyFormat("class MACRO(X) Z {\n} n;");
  5730. verifyFormat("class __attribute__(X) Z {\n} n;");
  5731. verifyFormat("class __declspec(X) Z {\n} n;");
  5732. verifyFormat("class A##B##C {\n} n;");
  5733. verifyFormat("class alignas(16) Z {\n} n;");
  5734. // Redefinition from nested context:
  5735. verifyFormat("class A::B::C {\n} n;");
  5736. // Template definitions.
  5737. verifyFormat(
  5738. "template <typename F>\n"
  5739. "Matcher(const Matcher<F> &Other,\n"
  5740. " typename enable_if_c<is_base_of<F, T>::value &&\n"
  5741. " !is_same<F, T>::value>::type * = 0)\n"
  5742. " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
  5743. // FIXME: This is still incorrectly handled at the formatter side.
  5744. verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
  5745. // FIXME:
  5746. // This now gets parsed incorrectly as class definition.
  5747. // verifyFormat("class A<int> f() {\n}\nint n;");
  5748. // Elaborate types where incorrectly parsing the structural element would
  5749. // break the indent.
  5750. verifyFormat("if (true)\n"
  5751. " class X x;\n"
  5752. "else\n"
  5753. " f();\n");
  5754. // This is simply incomplete. Formatting is not important, but must not crash.
  5755. verifyFormat("class A:");
  5756. }
  5757. TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
  5758. EXPECT_EQ("#error Leave all white!!!!! space* alone!\n",
  5759. format("#error Leave all white!!!!! space* alone!\n"));
  5760. EXPECT_EQ(
  5761. "#warning Leave all white!!!!! space* alone!\n",
  5762. format("#warning Leave all white!!!!! space* alone!\n"));
  5763. EXPECT_EQ("#error 1", format(" # error 1"));
  5764. EXPECT_EQ("#warning 1", format(" # warning 1"));
  5765. }
  5766. TEST_F(FormatTest, FormatHashIfExpressions) {
  5767. verifyFormat("#if AAAA && BBBB");
  5768. // FIXME: Come up with a better indentation for #elif.
  5769. verifyFormat(
  5770. "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
  5771. " defined(BBBBBBBB)\n"
  5772. "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
  5773. " defined(BBBBBBBB)\n"
  5774. "#endif",
  5775. getLLVMStyleWithColumns(65));
  5776. }
  5777. TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
  5778. FormatStyle AllowsMergedIf = getGoogleStyle();
  5779. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  5780. verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
  5781. verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
  5782. verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
  5783. EXPECT_EQ("if (true) return 42;",
  5784. format("if (true)\nreturn 42;", AllowsMergedIf));
  5785. FormatStyle ShortMergedIf = AllowsMergedIf;
  5786. ShortMergedIf.ColumnLimit = 25;
  5787. verifyFormat("#define A \\\n"
  5788. " if (true) return 42;",
  5789. ShortMergedIf);
  5790. verifyFormat("#define A \\\n"
  5791. " f(); \\\n"
  5792. " if (true)\n"
  5793. "#define B",
  5794. ShortMergedIf);
  5795. verifyFormat("#define A \\\n"
  5796. " f(); \\\n"
  5797. " if (true)\n"
  5798. "g();",
  5799. ShortMergedIf);
  5800. verifyFormat("{\n"
  5801. "#ifdef A\n"
  5802. " // Comment\n"
  5803. " if (true) continue;\n"
  5804. "#endif\n"
  5805. " // Comment\n"
  5806. " if (true) continue;\n"
  5807. "}",
  5808. ShortMergedIf);
  5809. ShortMergedIf.ColumnLimit = 29;
  5810. verifyFormat("#define A \\\n"
  5811. " if (aaaaaaaaaa) return 1; \\\n"
  5812. " return 2;",
  5813. ShortMergedIf);
  5814. ShortMergedIf.ColumnLimit = 28;
  5815. verifyFormat("#define A \\\n"
  5816. " if (aaaaaaaaaa) \\\n"
  5817. " return 1; \\\n"
  5818. " return 2;",
  5819. ShortMergedIf);
  5820. }
  5821. TEST_F(FormatTest, BlockCommentsInControlLoops) {
  5822. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  5823. " f();\n"
  5824. "}");
  5825. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  5826. " f();\n"
  5827. "} /* another comment */ else /* comment #3 */ {\n"
  5828. " g();\n"
  5829. "}");
  5830. verifyFormat("while (0) /* a comment in a strange place */ {\n"
  5831. " f();\n"
  5832. "}");
  5833. verifyFormat("for (;;) /* a comment in a strange place */ {\n"
  5834. " f();\n"
  5835. "}");
  5836. verifyFormat("do /* a comment in a strange place */ {\n"
  5837. " f();\n"
  5838. "} /* another comment */ while (0);");
  5839. }
  5840. TEST_F(FormatTest, BlockComments) {
  5841. EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
  5842. format("/* *//* */ /* */\n/* *//* */ /* */"));
  5843. EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
  5844. EXPECT_EQ("#define A /*123*/ \\\n"
  5845. " b\n"
  5846. "/* */\n"
  5847. "someCall(\n"
  5848. " parameter);",
  5849. format("#define A /*123*/ b\n"
  5850. "/* */\n"
  5851. "someCall(parameter);",
  5852. getLLVMStyleWithColumns(15)));
  5853. EXPECT_EQ("#define A\n"
  5854. "/* */ someCall(\n"
  5855. " parameter);",
  5856. format("#define A\n"
  5857. "/* */someCall(parameter);",
  5858. getLLVMStyleWithColumns(15)));
  5859. EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
  5860. EXPECT_EQ("/*\n"
  5861. "*\n"
  5862. " * aaaaaa\n"
  5863. "*aaaaaa\n"
  5864. "*/",
  5865. format("/*\n"
  5866. "*\n"
  5867. " * aaaaaa aaaaaa\n"
  5868. "*/",
  5869. getLLVMStyleWithColumns(10)));
  5870. EXPECT_EQ("/*\n"
  5871. "**\n"
  5872. "* aaaaaa\n"
  5873. "*aaaaaa\n"
  5874. "*/",
  5875. format("/*\n"
  5876. "**\n"
  5877. "* aaaaaa aaaaaa\n"
  5878. "*/",
  5879. getLLVMStyleWithColumns(10)));
  5880. FormatStyle NoBinPacking = getLLVMStyle();
  5881. NoBinPacking.BinPackParameters = false;
  5882. EXPECT_EQ("someFunction(1, /* comment 1 */\n"
  5883. " 2, /* comment 2 */\n"
  5884. " 3, /* comment 3 */\n"
  5885. " aaaa,\n"
  5886. " bbbb);",
  5887. format("someFunction (1, /* comment 1 */\n"
  5888. " 2, /* comment 2 */ \n"
  5889. " 3, /* comment 3 */\n"
  5890. "aaaa, bbbb );",
  5891. NoBinPacking));
  5892. verifyFormat(
  5893. "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  5894. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  5895. EXPECT_EQ(
  5896. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  5897. " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  5898. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
  5899. format(
  5900. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  5901. " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  5902. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
  5903. EXPECT_EQ(
  5904. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  5905. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  5906. "int cccccccccccccccccccccccccccccc; /* comment */\n",
  5907. format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  5908. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  5909. "int cccccccccccccccccccccccccccccc; /* comment */\n"));
  5910. verifyFormat("void f(int * /* unused */) {}");
  5911. EXPECT_EQ("/*\n"
  5912. " **\n"
  5913. " */",
  5914. format("/*\n"
  5915. " **\n"
  5916. " */"));
  5917. EXPECT_EQ("/*\n"
  5918. " *q\n"
  5919. " */",
  5920. format("/*\n"
  5921. " *q\n"
  5922. " */"));
  5923. EXPECT_EQ("/*\n"
  5924. " * q\n"
  5925. " */",
  5926. format("/*\n"
  5927. " * q\n"
  5928. " */"));
  5929. EXPECT_EQ("/*\n"
  5930. " **/",
  5931. format("/*\n"
  5932. " **/"));
  5933. EXPECT_EQ("/*\n"
  5934. " ***/",
  5935. format("/*\n"
  5936. " ***/"));
  5937. }
  5938. TEST_F(FormatTest, BlockCommentsInMacros) {
  5939. EXPECT_EQ("#define A \\\n"
  5940. " { \\\n"
  5941. " /* one line */ \\\n"
  5942. " someCall();",
  5943. format("#define A { \\\n"
  5944. " /* one line */ \\\n"
  5945. " someCall();",
  5946. getLLVMStyleWithColumns(20)));
  5947. EXPECT_EQ("#define A \\\n"
  5948. " { \\\n"
  5949. " /* previous */ \\\n"
  5950. " /* one line */ \\\n"
  5951. " someCall();",
  5952. format("#define A { \\\n"
  5953. " /* previous */ \\\n"
  5954. " /* one line */ \\\n"
  5955. " someCall();",
  5956. getLLVMStyleWithColumns(20)));
  5957. }
  5958. TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
  5959. EXPECT_EQ("a = {\n"
  5960. " 1111 /* */\n"
  5961. "};",
  5962. format("a = {1111 /* */\n"
  5963. "};",
  5964. getLLVMStyleWithColumns(15)));
  5965. EXPECT_EQ("a = {\n"
  5966. " 1111 /* */\n"
  5967. "};",
  5968. format("a = {1111 /* */\n"
  5969. "};",
  5970. getLLVMStyleWithColumns(15)));
  5971. // FIXME: The formatting is still wrong here.
  5972. EXPECT_EQ("a = {\n"
  5973. " 1111 /* a\n"
  5974. " */\n"
  5975. "};",
  5976. format("a = {1111 /* a */\n"
  5977. "};",
  5978. getLLVMStyleWithColumns(15)));
  5979. }
  5980. TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
  5981. // FIXME: This is not what we want...
  5982. verifyFormat("{\n"
  5983. "// a"
  5984. "// b");
  5985. }
  5986. TEST_F(FormatTest, FormatStarDependingOnContext) {
  5987. verifyFormat("void f(int *a);");
  5988. verifyFormat("void f() { f(fint * b); }");
  5989. verifyFormat("class A {\n void f(int *a);\n};");
  5990. verifyFormat("class A {\n int *a;\n};");
  5991. verifyFormat("namespace a {\n"
  5992. "namespace b {\n"
  5993. "class A {\n"
  5994. " void f() {}\n"
  5995. " int *a;\n"
  5996. "};\n"
  5997. "}\n"
  5998. "}");
  5999. }
  6000. TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
  6001. verifyFormat("while");
  6002. verifyFormat("operator");
  6003. }
  6004. //===----------------------------------------------------------------------===//
  6005. // Objective-C tests.
  6006. //===----------------------------------------------------------------------===//
  6007. TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
  6008. verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
  6009. EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
  6010. format("-(NSUInteger)indexOfObject:(id)anObject;"));
  6011. EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
  6012. EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
  6013. EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
  6014. format("-(NSInteger)Method3:(id)anObject;"));
  6015. EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
  6016. format("-(NSInteger)Method4:(id)anObject;"));
  6017. EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
  6018. format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
  6019. EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
  6020. format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
  6021. EXPECT_EQ(
  6022. "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
  6023. format(
  6024. "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
  6025. // Very long objectiveC method declaration.
  6026. verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
  6027. " inRange:(NSRange)range\n"
  6028. " outRange:(NSRange)out_range\n"
  6029. " outRange1:(NSRange)out_range1\n"
  6030. " outRange2:(NSRange)out_range2\n"
  6031. " outRange3:(NSRange)out_range3\n"
  6032. " outRange4:(NSRange)out_range4\n"
  6033. " outRange5:(NSRange)out_range5\n"
  6034. " outRange6:(NSRange)out_range6\n"
  6035. " outRange7:(NSRange)out_range7\n"
  6036. " outRange8:(NSRange)out_range8\n"
  6037. " outRange9:(NSRange)out_range9;");
  6038. verifyFormat("- (int)sum:(vector<int>)numbers;");
  6039. verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
  6040. // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
  6041. // protocol lists (but not for template classes):
  6042. //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
  6043. verifyFormat("- (int (*)())foo:(int (*)())f;");
  6044. verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
  6045. // If there's no return type (very rare in practice!), LLVM and Google style
  6046. // agree.
  6047. verifyFormat("- foo;");
  6048. verifyFormat("- foo:(int)f;");
  6049. verifyGoogleFormat("- foo:(int)foo;");
  6050. }
  6051. TEST_F(FormatTest, FormatObjCInterface) {
  6052. verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
  6053. "@public\n"
  6054. " int field1;\n"
  6055. "@protected\n"
  6056. " int field2;\n"
  6057. "@private\n"
  6058. " int field3;\n"
  6059. "@package\n"
  6060. " int field4;\n"
  6061. "}\n"
  6062. "+ (id)init;\n"
  6063. "@end");
  6064. verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
  6065. " @public\n"
  6066. " int field1;\n"
  6067. " @protected\n"
  6068. " int field2;\n"
  6069. " @private\n"
  6070. " int field3;\n"
  6071. " @package\n"
  6072. " int field4;\n"
  6073. "}\n"
  6074. "+ (id)init;\n"
  6075. "@end");
  6076. verifyFormat("@interface /* wait for it */ Foo\n"
  6077. "+ (id)init;\n"
  6078. "// Look, a comment!\n"
  6079. "- (int)answerWith:(int)i;\n"
  6080. "@end");
  6081. verifyFormat("@interface Foo\n"
  6082. "@end\n"
  6083. "@interface Bar\n"
  6084. "@end");
  6085. verifyFormat("@interface Foo : Bar\n"
  6086. "+ (id)init;\n"
  6087. "@end");
  6088. verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
  6089. "+ (id)init;\n"
  6090. "@end");
  6091. verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
  6092. "+ (id)init;\n"
  6093. "@end");
  6094. verifyFormat("@interface Foo (HackStuff)\n"
  6095. "+ (id)init;\n"
  6096. "@end");
  6097. verifyFormat("@interface Foo ()\n"
  6098. "+ (id)init;\n"
  6099. "@end");
  6100. verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
  6101. "+ (id)init;\n"
  6102. "@end");
  6103. verifyGoogleFormat("@interface Foo (HackStuff) <MyProtocol>\n"
  6104. "+ (id)init;\n"
  6105. "@end");
  6106. verifyFormat("@interface Foo {\n"
  6107. " int _i;\n"
  6108. "}\n"
  6109. "+ (id)init;\n"
  6110. "@end");
  6111. verifyFormat("@interface Foo : Bar {\n"
  6112. " int _i;\n"
  6113. "}\n"
  6114. "+ (id)init;\n"
  6115. "@end");
  6116. verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
  6117. " int _i;\n"
  6118. "}\n"
  6119. "+ (id)init;\n"
  6120. "@end");
  6121. verifyFormat("@interface Foo (HackStuff) {\n"
  6122. " int _i;\n"
  6123. "}\n"
  6124. "+ (id)init;\n"
  6125. "@end");
  6126. verifyFormat("@interface Foo () {\n"
  6127. " int _i;\n"
  6128. "}\n"
  6129. "+ (id)init;\n"
  6130. "@end");
  6131. verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
  6132. " int _i;\n"
  6133. "}\n"
  6134. "+ (id)init;\n"
  6135. "@end");
  6136. FormatStyle OnePerLine = getGoogleStyle();
  6137. OnePerLine.BinPackParameters = false;
  6138. verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa () <\n"
  6139. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6140. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6141. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6142. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
  6143. "}",
  6144. OnePerLine);
  6145. }
  6146. TEST_F(FormatTest, FormatObjCImplementation) {
  6147. verifyFormat("@implementation Foo : NSObject {\n"
  6148. "@public\n"
  6149. " int field1;\n"
  6150. "@protected\n"
  6151. " int field2;\n"
  6152. "@private\n"
  6153. " int field3;\n"
  6154. "@package\n"
  6155. " int field4;\n"
  6156. "}\n"
  6157. "+ (id)init {\n}\n"
  6158. "@end");
  6159. verifyGoogleFormat("@implementation Foo : NSObject {\n"
  6160. " @public\n"
  6161. " int field1;\n"
  6162. " @protected\n"
  6163. " int field2;\n"
  6164. " @private\n"
  6165. " int field3;\n"
  6166. " @package\n"
  6167. " int field4;\n"
  6168. "}\n"
  6169. "+ (id)init {\n}\n"
  6170. "@end");
  6171. verifyFormat("@implementation Foo\n"
  6172. "+ (id)init {\n"
  6173. " if (true)\n"
  6174. " return nil;\n"
  6175. "}\n"
  6176. "// Look, a comment!\n"
  6177. "- (int)answerWith:(int)i {\n"
  6178. " return i;\n"
  6179. "}\n"
  6180. "+ (int)answerWith:(int)i {\n"
  6181. " return i;\n"
  6182. "}\n"
  6183. "@end");
  6184. verifyFormat("@implementation Foo\n"
  6185. "@end\n"
  6186. "@implementation Bar\n"
  6187. "@end");
  6188. EXPECT_EQ("@implementation Foo : Bar\n"
  6189. "+ (id)init {\n}\n"
  6190. "- (void)foo {\n}\n"
  6191. "@end",
  6192. format("@implementation Foo : Bar\n"
  6193. "+(id)init{}\n"
  6194. "-(void)foo{}\n"
  6195. "@end"));
  6196. verifyFormat("@implementation Foo {\n"
  6197. " int _i;\n"
  6198. "}\n"
  6199. "+ (id)init {\n}\n"
  6200. "@end");
  6201. verifyFormat("@implementation Foo : Bar {\n"
  6202. " int _i;\n"
  6203. "}\n"
  6204. "+ (id)init {\n}\n"
  6205. "@end");
  6206. verifyFormat("@implementation Foo (HackStuff)\n"
  6207. "+ (id)init {\n}\n"
  6208. "@end");
  6209. verifyFormat("@implementation ObjcClass\n"
  6210. "- (void)method;\n"
  6211. "{}\n"
  6212. "@end");
  6213. }
  6214. TEST_F(FormatTest, FormatObjCProtocol) {
  6215. verifyFormat("@protocol Foo\n"
  6216. "@property(weak) id delegate;\n"
  6217. "- (NSUInteger)numberOfThings;\n"
  6218. "@end");
  6219. verifyFormat("@protocol MyProtocol <NSObject>\n"
  6220. "- (NSUInteger)numberOfThings;\n"
  6221. "@end");
  6222. verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
  6223. "- (NSUInteger)numberOfThings;\n"
  6224. "@end");
  6225. verifyFormat("@protocol Foo;\n"
  6226. "@protocol Bar;\n");
  6227. verifyFormat("@protocol Foo\n"
  6228. "@end\n"
  6229. "@protocol Bar\n"
  6230. "@end");
  6231. verifyFormat("@protocol myProtocol\n"
  6232. "- (void)mandatoryWithInt:(int)i;\n"
  6233. "@optional\n"
  6234. "- (void)optional;\n"
  6235. "@required\n"
  6236. "- (void)required;\n"
  6237. "@optional\n"
  6238. "@property(assign) int madProp;\n"
  6239. "@end\n");
  6240. verifyFormat("@property(nonatomic, assign, readonly)\n"
  6241. " int *looooooooooooooooooooooooooooongNumber;\n"
  6242. "@property(nonatomic, assign, readonly)\n"
  6243. " NSString *looooooooooooooooooooooooooooongName;");
  6244. verifyFormat("@implementation PR18406\n"
  6245. "}\n"
  6246. "@end");
  6247. }
  6248. TEST_F(FormatTest, FormatObjCMethodDeclarations) {
  6249. verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
  6250. " rect:(NSRect)theRect\n"
  6251. " interval:(float)theInterval {\n"
  6252. "}");
  6253. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6254. " longKeyword:(NSRect)theRect\n"
  6255. " evenLongerKeyword:(float)theInterval\n"
  6256. " error:(NSError **)theError {\n"
  6257. "}");
  6258. verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
  6259. " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
  6260. " NS_DESIGNATED_INITIALIZER;",
  6261. getLLVMStyleWithColumns(60));
  6262. }
  6263. TEST_F(FormatTest, FormatObjCMethodExpr) {
  6264. verifyFormat("[foo bar:baz];");
  6265. verifyFormat("return [foo bar:baz];");
  6266. verifyFormat("return (a)[foo bar:baz];");
  6267. verifyFormat("f([foo bar:baz]);");
  6268. verifyFormat("f(2, [foo bar:baz]);");
  6269. verifyFormat("f(2, a ? b : c);");
  6270. verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
  6271. // Unary operators.
  6272. verifyFormat("int a = +[foo bar:baz];");
  6273. verifyFormat("int a = -[foo bar:baz];");
  6274. verifyFormat("int a = ![foo bar:baz];");
  6275. verifyFormat("int a = ~[foo bar:baz];");
  6276. verifyFormat("int a = ++[foo bar:baz];");
  6277. verifyFormat("int a = --[foo bar:baz];");
  6278. verifyFormat("int a = sizeof [foo bar:baz];");
  6279. verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
  6280. verifyFormat("int a = &[foo bar:baz];");
  6281. verifyFormat("int a = *[foo bar:baz];");
  6282. // FIXME: Make casts work, without breaking f()[4].
  6283. //verifyFormat("int a = (int)[foo bar:baz];");
  6284. //verifyFormat("return (int)[foo bar:baz];");
  6285. //verifyFormat("(void)[foo bar:baz];");
  6286. verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
  6287. // Binary operators.
  6288. verifyFormat("[foo bar:baz], [foo bar:baz];");
  6289. verifyFormat("[foo bar:baz] = [foo bar:baz];");
  6290. verifyFormat("[foo bar:baz] *= [foo bar:baz];");
  6291. verifyFormat("[foo bar:baz] /= [foo bar:baz];");
  6292. verifyFormat("[foo bar:baz] %= [foo bar:baz];");
  6293. verifyFormat("[foo bar:baz] += [foo bar:baz];");
  6294. verifyFormat("[foo bar:baz] -= [foo bar:baz];");
  6295. verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
  6296. verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
  6297. verifyFormat("[foo bar:baz] &= [foo bar:baz];");
  6298. verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
  6299. verifyFormat("[foo bar:baz] |= [foo bar:baz];");
  6300. verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
  6301. verifyFormat("[foo bar:baz] || [foo bar:baz];");
  6302. verifyFormat("[foo bar:baz] && [foo bar:baz];");
  6303. verifyFormat("[foo bar:baz] | [foo bar:baz];");
  6304. verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
  6305. verifyFormat("[foo bar:baz] & [foo bar:baz];");
  6306. verifyFormat("[foo bar:baz] == [foo bar:baz];");
  6307. verifyFormat("[foo bar:baz] != [foo bar:baz];");
  6308. verifyFormat("[foo bar:baz] >= [foo bar:baz];");
  6309. verifyFormat("[foo bar:baz] <= [foo bar:baz];");
  6310. verifyFormat("[foo bar:baz] > [foo bar:baz];");
  6311. verifyFormat("[foo bar:baz] < [foo bar:baz];");
  6312. verifyFormat("[foo bar:baz] >> [foo bar:baz];");
  6313. verifyFormat("[foo bar:baz] << [foo bar:baz];");
  6314. verifyFormat("[foo bar:baz] - [foo bar:baz];");
  6315. verifyFormat("[foo bar:baz] + [foo bar:baz];");
  6316. verifyFormat("[foo bar:baz] * [foo bar:baz];");
  6317. verifyFormat("[foo bar:baz] / [foo bar:baz];");
  6318. verifyFormat("[foo bar:baz] % [foo bar:baz];");
  6319. // Whew!
  6320. verifyFormat("return in[42];");
  6321. verifyFormat("for (auto v : in[1]) {\n}");
  6322. verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
  6323. "}");
  6324. verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
  6325. verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
  6326. verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
  6327. verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
  6328. verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
  6329. verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
  6330. verifyFormat("[button setAction:@selector(zoomOut:)];");
  6331. verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
  6332. verifyFormat("arr[[self indexForFoo:a]];");
  6333. verifyFormat("throw [self errorFor:a];");
  6334. verifyFormat("@throw [self errorFor:a];");
  6335. verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
  6336. verifyFormat("[(id)foo bar:(id) ? baz : quux];");
  6337. verifyFormat("4 > 4 ? (id)a : (id)baz;");
  6338. // This tests that the formatter doesn't break after "backing" but before ":",
  6339. // which would be at 80 columns.
  6340. verifyFormat(
  6341. "void f() {\n"
  6342. " if ((self = [super initWithContentRect:contentRect\n"
  6343. " styleMask:styleMask ?: otherMask\n"
  6344. " backing:NSBackingStoreBuffered\n"
  6345. " defer:YES]))");
  6346. verifyFormat(
  6347. "[foo checkThatBreakingAfterColonWorksOk:\n"
  6348. " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
  6349. verifyFormat("[myObj short:arg1 // Force line break\n"
  6350. " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
  6351. " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
  6352. " error:arg4];");
  6353. verifyFormat(
  6354. "void f() {\n"
  6355. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  6356. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  6357. " pos.width(), pos.height())\n"
  6358. " styleMask:NSBorderlessWindowMask\n"
  6359. " backing:NSBackingStoreBuffered\n"
  6360. " defer:NO]);\n"
  6361. "}");
  6362. verifyFormat(
  6363. "void f() {\n"
  6364. " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
  6365. " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
  6366. " pos.width(), pos.height())\n"
  6367. " syeMask:NSBorderlessWindowMask\n"
  6368. " bking:NSBackingStoreBuffered\n"
  6369. " der:NO]);\n"
  6370. "}",
  6371. getLLVMStyleWithColumns(70));
  6372. verifyFormat(
  6373. "void f() {\n"
  6374. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  6375. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  6376. " pos.width(), pos.height())\n"
  6377. " styleMask:NSBorderlessWindowMask\n"
  6378. " backing:NSBackingStoreBuffered\n"
  6379. " defer:NO]);\n"
  6380. "}",
  6381. getChromiumStyle(FormatStyle::LK_Cpp));
  6382. verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
  6383. " with:contentsNativeView];");
  6384. verifyFormat(
  6385. "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
  6386. " owner:nillllll];");
  6387. verifyFormat(
  6388. "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
  6389. " forType:kBookmarkButtonDragType];");
  6390. verifyFormat("[defaultCenter addObserver:self\n"
  6391. " selector:@selector(willEnterFullscreen)\n"
  6392. " name:kWillEnterFullscreenNotification\n"
  6393. " object:nil];");
  6394. verifyFormat("[image_rep drawInRect:drawRect\n"
  6395. " fromRect:NSZeroRect\n"
  6396. " operation:NSCompositeCopy\n"
  6397. " fraction:1.0\n"
  6398. " respectFlipped:NO\n"
  6399. " hints:nil];");
  6400. verifyFormat(
  6401. "scoped_nsobject<NSTextField> message(\n"
  6402. " // The frame will be fixed up when |-setMessageText:| is called.\n"
  6403. " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
  6404. verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
  6405. " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
  6406. " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
  6407. " aaaa:bbb];");
  6408. verifyFormat("[self param:function( //\n"
  6409. " parameter)]");
  6410. verifyFormat(
  6411. "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  6412. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  6413. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
  6414. // Variadic parameters.
  6415. verifyFormat(
  6416. "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
  6417. verifyFormat(
  6418. "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  6419. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  6420. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
  6421. verifyFormat("[self // break\n"
  6422. " a:a\n"
  6423. " aaa:aaa];");
  6424. verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
  6425. " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
  6426. }
  6427. TEST_F(FormatTest, ObjCAt) {
  6428. verifyFormat("@autoreleasepool");
  6429. verifyFormat("@catch");
  6430. verifyFormat("@class");
  6431. verifyFormat("@compatibility_alias");
  6432. verifyFormat("@defs");
  6433. verifyFormat("@dynamic");
  6434. verifyFormat("@encode");
  6435. verifyFormat("@end");
  6436. verifyFormat("@finally");
  6437. verifyFormat("@implementation");
  6438. verifyFormat("@import");
  6439. verifyFormat("@interface");
  6440. verifyFormat("@optional");
  6441. verifyFormat("@package");
  6442. verifyFormat("@private");
  6443. verifyFormat("@property");
  6444. verifyFormat("@protected");
  6445. verifyFormat("@protocol");
  6446. verifyFormat("@public");
  6447. verifyFormat("@required");
  6448. verifyFormat("@selector");
  6449. verifyFormat("@synchronized");
  6450. verifyFormat("@synthesize");
  6451. verifyFormat("@throw");
  6452. verifyFormat("@try");
  6453. EXPECT_EQ("@interface", format("@ interface"));
  6454. // The precise formatting of this doesn't matter, nobody writes code like
  6455. // this.
  6456. verifyFormat("@ /*foo*/ interface");
  6457. }
  6458. TEST_F(FormatTest, ObjCSnippets) {
  6459. verifyFormat("@autoreleasepool {\n"
  6460. " foo();\n"
  6461. "}");
  6462. verifyFormat("@class Foo, Bar;");
  6463. verifyFormat("@compatibility_alias AliasName ExistingClass;");
  6464. verifyFormat("@dynamic textColor;");
  6465. verifyFormat("char *buf1 = @encode(int *);");
  6466. verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
  6467. verifyFormat("char *buf1 = @encode(int **);");
  6468. verifyFormat("Protocol *proto = @protocol(p1);");
  6469. verifyFormat("SEL s = @selector(foo:);");
  6470. verifyFormat("@synchronized(self) {\n"
  6471. " f();\n"
  6472. "}");
  6473. verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  6474. verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  6475. verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
  6476. verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
  6477. verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
  6478. verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
  6479. getMozillaStyle());
  6480. verifyFormat("@property BOOL editable;", getMozillaStyle());
  6481. verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
  6482. getWebKitStyle());
  6483. verifyFormat("@property BOOL editable;", getWebKitStyle());
  6484. verifyFormat("@import foo.bar;\n"
  6485. "@import baz;");
  6486. }
  6487. TEST_F(FormatTest, ObjCLiterals) {
  6488. verifyFormat("@\"String\"");
  6489. verifyFormat("@1");
  6490. verifyFormat("@+4.8");
  6491. verifyFormat("@-4");
  6492. verifyFormat("@1LL");
  6493. verifyFormat("@.5");
  6494. verifyFormat("@'c'");
  6495. verifyFormat("@true");
  6496. verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
  6497. verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
  6498. verifyFormat("NSNumber *favoriteColor = @(Green);");
  6499. verifyFormat("NSString *path = @(getenv(\"PATH\"));");
  6500. verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
  6501. }
  6502. TEST_F(FormatTest, ObjCDictLiterals) {
  6503. verifyFormat("@{");
  6504. verifyFormat("@{}");
  6505. verifyFormat("@{@\"one\" : @1}");
  6506. verifyFormat("return @{@\"one\" : @1;");
  6507. verifyFormat("@{@\"one\" : @1}");
  6508. verifyFormat("@{@\"one\" : @{@2 : @1}}");
  6509. verifyFormat("@{\n"
  6510. " @\"one\" : @{@2 : @1},\n"
  6511. "}");
  6512. verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
  6513. verifyFormat("[self setDict:@{}");
  6514. verifyFormat("[self setDict:@{@1 : @2}");
  6515. verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
  6516. verifyFormat(
  6517. "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
  6518. verifyFormat(
  6519. "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
  6520. verifyFormat(
  6521. "NSDictionary *d = @{\n"
  6522. " @\"nam\" : NSUserNam(),\n"
  6523. " @\"dte\" : [NSDate date],\n"
  6524. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  6525. "};");
  6526. verifyFormat(
  6527. "@{\n"
  6528. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  6529. "regularFont,\n"
  6530. "};");
  6531. verifyGoogleFormat(
  6532. "@{\n"
  6533. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  6534. "regularFont,\n"
  6535. "};");
  6536. verifyFormat(
  6537. "@{\n"
  6538. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
  6539. " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
  6540. "};");
  6541. // We should try to be robust in case someone forgets the "@".
  6542. verifyFormat(
  6543. "NSDictionary *d = {\n"
  6544. " @\"nam\" : NSUserNam(),\n"
  6545. " @\"dte\" : [NSDate date],\n"
  6546. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  6547. "};");
  6548. verifyFormat("NSMutableDictionary *dictionary =\n"
  6549. " [NSMutableDictionary dictionaryWithDictionary:@{\n"
  6550. " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
  6551. " bbbbbbbbbbbbbbbbbb : bbbbb,\n"
  6552. " cccccccccccccccc : ccccccccccccccc\n"
  6553. " }];");
  6554. }
  6555. TEST_F(FormatTest, ObjCArrayLiterals) {
  6556. verifyFormat("@[");
  6557. verifyFormat("@[]");
  6558. verifyFormat(
  6559. "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
  6560. verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
  6561. verifyFormat("NSArray *array = @[ [foo description] ];");
  6562. verifyFormat(
  6563. "NSArray *some_variable = @[\n"
  6564. " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
  6565. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6566. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6567. " @\"aaaaaaaaaaaaaaaaa\"\n"
  6568. "];");
  6569. verifyFormat("NSArray *some_variable = @[\n"
  6570. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6571. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6572. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6573. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6574. "];");
  6575. verifyGoogleFormat("NSArray *some_variable = @[\n"
  6576. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6577. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6578. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6579. " @\"aaaaaaaaaaaaaaaaa\"\n"
  6580. "];");
  6581. // We should try to be robust in case someone forgets the "@".
  6582. verifyFormat("NSArray *some_variable = [\n"
  6583. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6584. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6585. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6586. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6587. "];");
  6588. verifyFormat(
  6589. "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
  6590. " index:(NSUInteger)index\n"
  6591. " nonDigitAttributes:\n"
  6592. " (NSDictionary *)noDigitAttributes;");
  6593. verifyFormat(
  6594. "[someFunction someLooooooooooooongParameter:\n"
  6595. " @[ NSBundle.mainBundle.infoDictionary[@\"a\"] ]];");
  6596. }
  6597. TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
  6598. EXPECT_EQ("{\n"
  6599. "{\n"
  6600. "a;\n"
  6601. "b;\n"
  6602. "}\n"
  6603. "}",
  6604. format("{\n"
  6605. "{\n"
  6606. "a;\n"
  6607. " b;\n"
  6608. "}\n"
  6609. "}",
  6610. 13, 2, getLLVMStyle()));
  6611. EXPECT_EQ("{\n"
  6612. "{\n"
  6613. " a;\n"
  6614. "b;\n"
  6615. "}\n"
  6616. "}",
  6617. format("{\n"
  6618. "{\n"
  6619. " a;\n"
  6620. "b;\n"
  6621. "}\n"
  6622. "}",
  6623. 9, 2, getLLVMStyle()));
  6624. EXPECT_EQ("{\n"
  6625. "{\n"
  6626. "public:\n"
  6627. " b;\n"
  6628. "}\n"
  6629. "}",
  6630. format("{\n"
  6631. "{\n"
  6632. "public:\n"
  6633. " b;\n"
  6634. "}\n"
  6635. "}",
  6636. 17, 2, getLLVMStyle()));
  6637. EXPECT_EQ("{\n"
  6638. "{\n"
  6639. "a;\n"
  6640. "}\n"
  6641. "{\n"
  6642. " b; //\n"
  6643. "}\n"
  6644. "}",
  6645. format("{\n"
  6646. "{\n"
  6647. "a;\n"
  6648. "}\n"
  6649. "{\n"
  6650. " b; //\n"
  6651. "}\n"
  6652. "}",
  6653. 22, 2, getLLVMStyle()));
  6654. EXPECT_EQ(" {\n"
  6655. " a; //\n"
  6656. " }",
  6657. format(" {\n"
  6658. "a; //\n"
  6659. " }",
  6660. 4, 2, getLLVMStyle()));
  6661. EXPECT_EQ("void f() {}\n"
  6662. "void g() {}",
  6663. format("void f() {}\n"
  6664. "void g() {}",
  6665. 13, 0, getLLVMStyle()));
  6666. EXPECT_EQ("int a; // comment\n"
  6667. " // line 2\n"
  6668. "int b;",
  6669. format("int a; // comment\n"
  6670. " // line 2\n"
  6671. " int b;",
  6672. 35, 0, getLLVMStyle()));
  6673. EXPECT_EQ(" int a;\n"
  6674. " void\n"
  6675. " ffffff() {\n"
  6676. " }",
  6677. format(" int a;\n"
  6678. "void ffffff() {}",
  6679. 11, 0, getLLVMStyleWithColumns(11)));
  6680. EXPECT_EQ(" void f() {\n"
  6681. "#define A 1\n"
  6682. " }",
  6683. format(" void f() {\n"
  6684. " #define A 1\n" // Format this line.
  6685. " }",
  6686. 20, 0, getLLVMStyle()));
  6687. EXPECT_EQ(" void f() {\n"
  6688. " int i;\n"
  6689. "#define A \\\n"
  6690. " int i; \\\n"
  6691. " int j;\n"
  6692. " int k;\n"
  6693. " }",
  6694. format(" void f() {\n"
  6695. " int i;\n"
  6696. "#define A \\\n"
  6697. " int i; \\\n"
  6698. " int j;\n"
  6699. " int k;\n" // Format this line.
  6700. " }",
  6701. 67, 0, getLLVMStyle()));
  6702. }
  6703. TEST_F(FormatTest, BreaksStringLiterals) {
  6704. EXPECT_EQ("\"some text \"\n"
  6705. "\"other\";",
  6706. format("\"some text other\";", getLLVMStyleWithColumns(12)));
  6707. EXPECT_EQ("\"some text \"\n"
  6708. "\"other\";",
  6709. format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
  6710. EXPECT_EQ(
  6711. "#define A \\\n"
  6712. " \"some \" \\\n"
  6713. " \"text \" \\\n"
  6714. " \"other\";",
  6715. format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
  6716. EXPECT_EQ(
  6717. "#define A \\\n"
  6718. " \"so \" \\\n"
  6719. " \"text \" \\\n"
  6720. " \"other\";",
  6721. format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
  6722. EXPECT_EQ("\"some text\"",
  6723. format("\"some text\"", getLLVMStyleWithColumns(1)));
  6724. EXPECT_EQ("\"some text\"",
  6725. format("\"some text\"", getLLVMStyleWithColumns(11)));
  6726. EXPECT_EQ("\"some \"\n"
  6727. "\"text\"",
  6728. format("\"some text\"", getLLVMStyleWithColumns(10)));
  6729. EXPECT_EQ("\"some \"\n"
  6730. "\"text\"",
  6731. format("\"some text\"", getLLVMStyleWithColumns(7)));
  6732. EXPECT_EQ("\"some\"\n"
  6733. "\" tex\"\n"
  6734. "\"t\"",
  6735. format("\"some text\"", getLLVMStyleWithColumns(6)));
  6736. EXPECT_EQ("\"some\"\n"
  6737. "\" tex\"\n"
  6738. "\" and\"",
  6739. format("\"some tex and\"", getLLVMStyleWithColumns(6)));
  6740. EXPECT_EQ("\"some\"\n"
  6741. "\"/tex\"\n"
  6742. "\"/and\"",
  6743. format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
  6744. EXPECT_EQ("variable =\n"
  6745. " \"long string \"\n"
  6746. " \"literal\";",
  6747. format("variable = \"long string literal\";",
  6748. getLLVMStyleWithColumns(20)));
  6749. EXPECT_EQ("variable = f(\n"
  6750. " \"long string \"\n"
  6751. " \"literal\",\n"
  6752. " short,\n"
  6753. " loooooooooooooooooooong);",
  6754. format("variable = f(\"long string literal\", short, "
  6755. "loooooooooooooooooooong);",
  6756. getLLVMStyleWithColumns(20)));
  6757. EXPECT_EQ("f(g(\"long string \"\n"
  6758. " \"literal\"),\n"
  6759. " b);",
  6760. format("f(g(\"long string literal\"), b);",
  6761. getLLVMStyleWithColumns(20)));
  6762. EXPECT_EQ("f(g(\"long string \"\n"
  6763. " \"literal\",\n"
  6764. " a),\n"
  6765. " b);",
  6766. format("f(g(\"long string literal\", a), b);",
  6767. getLLVMStyleWithColumns(20)));
  6768. EXPECT_EQ(
  6769. "f(\"one two\".split(\n"
  6770. " variable));",
  6771. format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
  6772. EXPECT_EQ("f(\"one two three four five six \"\n"
  6773. " \"seven\".split(\n"
  6774. " really_looooong_variable));",
  6775. format("f(\"one two three four five six seven\"."
  6776. "split(really_looooong_variable));",
  6777. getLLVMStyleWithColumns(33)));
  6778. EXPECT_EQ("f(\"some \"\n"
  6779. " \"text\",\n"
  6780. " other);",
  6781. format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
  6782. // Only break as a last resort.
  6783. verifyFormat(
  6784. "aaaaaaaaaaaaaaaaaaaa(\n"
  6785. " aaaaaaaaaaaaaaaaaaaa,\n"
  6786. " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
  6787. EXPECT_EQ(
  6788. "\"splitmea\"\n"
  6789. "\"trandomp\"\n"
  6790. "\"oint\"",
  6791. format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
  6792. EXPECT_EQ(
  6793. "\"split/\"\n"
  6794. "\"pathat/\"\n"
  6795. "\"slashes\"",
  6796. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  6797. EXPECT_EQ(
  6798. "\"split/\"\n"
  6799. "\"pathat/\"\n"
  6800. "\"slashes\"",
  6801. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  6802. EXPECT_EQ("\"split at \"\n"
  6803. "\"spaces/at/\"\n"
  6804. "\"slashes.at.any$\"\n"
  6805. "\"non-alphanumeric%\"\n"
  6806. "\"1111111111characte\"\n"
  6807. "\"rs\"",
  6808. format("\"split at "
  6809. "spaces/at/"
  6810. "slashes.at."
  6811. "any$non-"
  6812. "alphanumeric%"
  6813. "1111111111characte"
  6814. "rs\"",
  6815. getLLVMStyleWithColumns(20)));
  6816. // Verify that splitting the strings understands
  6817. // Style::AlwaysBreakBeforeMultilineStrings.
  6818. EXPECT_EQ("aaaaaaaaaaaa(aaaaaaaaaaaaa,\n"
  6819. " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
  6820. " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
  6821. format("aaaaaaaaaaaa(aaaaaaaaaaaaa, \"aaaaaaaaaaaaaaaaaaaaaa "
  6822. "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa "
  6823. "aaaaaaaaaaaaaaaaaaaaaa\");",
  6824. getGoogleStyle()));
  6825. EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  6826. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
  6827. format("return \"aaaaaaaaaaaaaaaaaaaaaa "
  6828. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
  6829. "aaaaaaaaaaaaaaaaaaaaaa\";",
  6830. getGoogleStyle()));
  6831. EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  6832. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  6833. format("llvm::outs() << "
  6834. "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
  6835. "aaaaaaaaaaaaaaaaaaa\";"));
  6836. EXPECT_EQ("ffff(\n"
  6837. " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  6838. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  6839. format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  6840. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  6841. getGoogleStyle()));
  6842. FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
  6843. AlignLeft.AlignEscapedNewlinesLeft = true;
  6844. EXPECT_EQ(
  6845. "#define A \\\n"
  6846. " \"some \" \\\n"
  6847. " \"text \" \\\n"
  6848. " \"other\";",
  6849. format("#define A \"some text other\";", AlignLeft));
  6850. }
  6851. TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
  6852. EXPECT_EQ(
  6853. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  6854. "(\n"
  6855. " \"x\t\");",
  6856. format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  6857. "aaaaaaa("
  6858. "\"x\t\");"));
  6859. }
  6860. TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
  6861. EXPECT_EQ(
  6862. "u8\"utf8 string \"\n"
  6863. "u8\"literal\";",
  6864. format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
  6865. EXPECT_EQ(
  6866. "u\"utf16 string \"\n"
  6867. "u\"literal\";",
  6868. format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
  6869. EXPECT_EQ(
  6870. "U\"utf32 string \"\n"
  6871. "U\"literal\";",
  6872. format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
  6873. EXPECT_EQ("L\"wide string \"\n"
  6874. "L\"literal\";",
  6875. format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
  6876. EXPECT_EQ("@\"NSString \"\n"
  6877. "@\"literal\";",
  6878. format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
  6879. // This input makes clang-format try to split the incomplete unicode escape
  6880. // sequence, which used to lead to a crasher.
  6881. verifyNoCrash(
  6882. "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  6883. getLLVMStyleWithColumns(60));
  6884. }
  6885. TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
  6886. FormatStyle Style = getGoogleStyleWithColumns(15);
  6887. EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
  6888. EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
  6889. EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
  6890. EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
  6891. EXPECT_EQ("u8R\"x(raw literal)x\";",
  6892. format("u8R\"x(raw literal)x\";", Style));
  6893. }
  6894. TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
  6895. FormatStyle Style = getLLVMStyleWithColumns(20);
  6896. EXPECT_EQ(
  6897. "_T(\"aaaaaaaaaaaaaa\")\n"
  6898. "_T(\"aaaaaaaaaaaaaa\")\n"
  6899. "_T(\"aaaaaaaaaaaa\")",
  6900. format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
  6901. EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
  6902. " _T(\"aaaaaa\"),\n"
  6903. " z);",
  6904. format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
  6905. // FIXME: Handle embedded spaces in one iteration.
  6906. // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
  6907. // "_T(\"aaaaaaaaaaaaa\")\n"
  6908. // "_T(\"aaaaaaaaaaaaa\")\n"
  6909. // "_T(\"a\")",
  6910. // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  6911. // getLLVMStyleWithColumns(20)));
  6912. EXPECT_EQ(
  6913. "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  6914. format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
  6915. }
  6916. TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
  6917. EXPECT_EQ(
  6918. "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  6919. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  6920. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  6921. format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  6922. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  6923. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
  6924. }
  6925. TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
  6926. EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
  6927. format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle()));
  6928. EXPECT_EQ("fffffffffff(g(R\"x(\n"
  6929. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6930. ")x\",\n"
  6931. " a),\n"
  6932. " b);",
  6933. format("fffffffffff(g(R\"x(\n"
  6934. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6935. ")x\", a), b);",
  6936. getGoogleStyleWithColumns(20)));
  6937. EXPECT_EQ("fffffffffff(\n"
  6938. " g(R\"x(qqq\n"
  6939. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6940. ")x\",\n"
  6941. " a),\n"
  6942. " b);",
  6943. format("fffffffffff(g(R\"x(qqq\n"
  6944. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6945. ")x\", a), b);",
  6946. getGoogleStyleWithColumns(20)));
  6947. EXPECT_EQ("fffffffffff(R\"x(\n"
  6948. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6949. ")x\");",
  6950. format("fffffffffff(R\"x(\n"
  6951. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6952. ")x\");",
  6953. getGoogleStyleWithColumns(20)));
  6954. EXPECT_EQ("fffffffffff(R\"x(\n"
  6955. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6956. ")x\" + bbbbbb);",
  6957. format("fffffffffff(R\"x(\n"
  6958. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6959. ")x\" + bbbbbb);",
  6960. getGoogleStyleWithColumns(20)));
  6961. EXPECT_EQ("fffffffffff(\n"
  6962. " R\"x(\n"
  6963. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6964. ")x\" +\n"
  6965. " bbbbbb);",
  6966. format("fffffffffff(\n"
  6967. " R\"x(\n"
  6968. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6969. ")x\" + bbbbbb);",
  6970. getGoogleStyleWithColumns(20)));
  6971. }
  6972. TEST_F(FormatTest, SkipsUnknownStringLiterals) {
  6973. verifyFormat("string a = \"unterminated;");
  6974. EXPECT_EQ("function(\"unterminated,\n"
  6975. " OtherParameter);",
  6976. format("function( \"unterminated,\n"
  6977. " OtherParameter);"));
  6978. }
  6979. TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
  6980. FormatStyle Style = getLLVMStyle();
  6981. Style.Standard = FormatStyle::LS_Cpp03;
  6982. EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
  6983. format("#define x(_a) printf(\"foo\"_a);", Style));
  6984. }
  6985. TEST_F(FormatTest, UnderstandsCpp1y) {
  6986. verifyFormat("int bi{1'000'000};");
  6987. }
  6988. TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
  6989. EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
  6990. " \"ddeeefff\");",
  6991. format("someFunction(\"aaabbbcccdddeeefff\");",
  6992. getLLVMStyleWithColumns(25)));
  6993. EXPECT_EQ("someFunction1234567890(\n"
  6994. " \"aaabbbcccdddeeefff\");",
  6995. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  6996. getLLVMStyleWithColumns(26)));
  6997. EXPECT_EQ("someFunction1234567890(\n"
  6998. " \"aaabbbcccdddeeeff\"\n"
  6999. " \"f\");",
  7000. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  7001. getLLVMStyleWithColumns(25)));
  7002. EXPECT_EQ("someFunction1234567890(\n"
  7003. " \"aaabbbcccdddeeeff\"\n"
  7004. " \"f\");",
  7005. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  7006. getLLVMStyleWithColumns(24)));
  7007. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  7008. " \"ddde \"\n"
  7009. " \"efff\");",
  7010. format("someFunction(\"aaabbbcc ddde efff\");",
  7011. getLLVMStyleWithColumns(25)));
  7012. EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
  7013. " \"ddeeefff\");",
  7014. format("someFunction(\"aaabbbccc ddeeefff\");",
  7015. getLLVMStyleWithColumns(25)));
  7016. EXPECT_EQ("someFunction1234567890(\n"
  7017. " \"aaabb \"\n"
  7018. " \"cccdddeeefff\");",
  7019. format("someFunction1234567890(\"aaabb cccdddeeefff\");",
  7020. getLLVMStyleWithColumns(25)));
  7021. EXPECT_EQ("#define A \\\n"
  7022. " string s = \\\n"
  7023. " \"123456789\" \\\n"
  7024. " \"0\"; \\\n"
  7025. " int i;",
  7026. format("#define A string s = \"1234567890\"; int i;",
  7027. getLLVMStyleWithColumns(20)));
  7028. // FIXME: Put additional penalties on breaking at non-whitespace locations.
  7029. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  7030. " \"dddeeeff\"\n"
  7031. " \"f\");",
  7032. format("someFunction(\"aaabbbcc dddeeefff\");",
  7033. getLLVMStyleWithColumns(25)));
  7034. }
  7035. TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
  7036. EXPECT_EQ("\"\\a\"",
  7037. format("\"\\a\"", getLLVMStyleWithColumns(3)));
  7038. EXPECT_EQ("\"\\\"",
  7039. format("\"\\\"", getLLVMStyleWithColumns(2)));
  7040. EXPECT_EQ("\"test\"\n"
  7041. "\"\\n\"",
  7042. format("\"test\\n\"", getLLVMStyleWithColumns(7)));
  7043. EXPECT_EQ("\"tes\\\\\"\n"
  7044. "\"n\"",
  7045. format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
  7046. EXPECT_EQ("\"\\\\\\\\\"\n"
  7047. "\"\\n\"",
  7048. format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
  7049. EXPECT_EQ("\"\\uff01\"",
  7050. format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
  7051. EXPECT_EQ("\"\\uff01\"\n"
  7052. "\"test\"",
  7053. format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
  7054. EXPECT_EQ("\"\\Uff01ff02\"",
  7055. format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
  7056. EXPECT_EQ("\"\\x000000000001\"\n"
  7057. "\"next\"",
  7058. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
  7059. EXPECT_EQ("\"\\x000000000001next\"",
  7060. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
  7061. EXPECT_EQ("\"\\x000000000001\"",
  7062. format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
  7063. EXPECT_EQ("\"test\"\n"
  7064. "\"\\000000\"\n"
  7065. "\"000001\"",
  7066. format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
  7067. EXPECT_EQ("\"test\\000\"\n"
  7068. "\"00000000\"\n"
  7069. "\"1\"",
  7070. format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
  7071. }
  7072. TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
  7073. verifyFormat("void f() {\n"
  7074. " return g() {}\n"
  7075. " void h() {}");
  7076. verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
  7077. "g();\n"
  7078. "}");
  7079. }
  7080. TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
  7081. verifyFormat(
  7082. "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
  7083. }
  7084. TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
  7085. verifyFormat("class X {\n"
  7086. " void f() {\n"
  7087. " }\n"
  7088. "};",
  7089. getLLVMStyleWithColumns(12));
  7090. }
  7091. TEST_F(FormatTest, ConfigurableIndentWidth) {
  7092. FormatStyle EightIndent = getLLVMStyleWithColumns(18);
  7093. EightIndent.IndentWidth = 8;
  7094. EightIndent.ContinuationIndentWidth = 8;
  7095. verifyFormat("void f() {\n"
  7096. " someFunction();\n"
  7097. " if (true) {\n"
  7098. " f();\n"
  7099. " }\n"
  7100. "}",
  7101. EightIndent);
  7102. verifyFormat("class X {\n"
  7103. " void f() {\n"
  7104. " }\n"
  7105. "};",
  7106. EightIndent);
  7107. verifyFormat("int x[] = {\n"
  7108. " call(),\n"
  7109. " call()};",
  7110. EightIndent);
  7111. }
  7112. TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
  7113. verifyFormat("double\n"
  7114. "f();",
  7115. getLLVMStyleWithColumns(8));
  7116. }
  7117. TEST_F(FormatTest, ConfigurableUseOfTab) {
  7118. FormatStyle Tab = getLLVMStyleWithColumns(42);
  7119. Tab.IndentWidth = 8;
  7120. Tab.UseTab = FormatStyle::UT_Always;
  7121. Tab.AlignEscapedNewlinesLeft = true;
  7122. EXPECT_EQ("if (aaaaaaaa && // q\n"
  7123. " bb)\t\t// w\n"
  7124. "\t;",
  7125. format("if (aaaaaaaa &&// q\n"
  7126. "bb)// w\n"
  7127. ";",
  7128. Tab));
  7129. EXPECT_EQ("if (aaa && bbb) // w\n"
  7130. "\t;",
  7131. format("if(aaa&&bbb)// w\n"
  7132. ";",
  7133. Tab));
  7134. verifyFormat("class X {\n"
  7135. "\tvoid f() {\n"
  7136. "\t\tsomeFunction(parameter1,\n"
  7137. "\t\t\t parameter2);\n"
  7138. "\t}\n"
  7139. "};",
  7140. Tab);
  7141. verifyFormat("#define A \\\n"
  7142. "\tvoid f() { \\\n"
  7143. "\t\tsomeFunction( \\\n"
  7144. "\t\t parameter1, \\\n"
  7145. "\t\t parameter2); \\\n"
  7146. "\t}",
  7147. Tab);
  7148. EXPECT_EQ("void f() {\n"
  7149. "\tf();\n"
  7150. "\tg();\n"
  7151. "}",
  7152. format("void f() {\n"
  7153. "\tf();\n"
  7154. "\tg();\n"
  7155. "}",
  7156. 0, 0, Tab));
  7157. EXPECT_EQ("void f() {\n"
  7158. "\tf();\n"
  7159. "\tg();\n"
  7160. "}",
  7161. format("void f() {\n"
  7162. "\tf();\n"
  7163. "\tg();\n"
  7164. "}",
  7165. 16, 0, Tab));
  7166. EXPECT_EQ("void f() {\n"
  7167. " \tf();\n"
  7168. "\tg();\n"
  7169. "}",
  7170. format("void f() {\n"
  7171. " \tf();\n"
  7172. " \tg();\n"
  7173. "}",
  7174. 21, 0, Tab));
  7175. Tab.TabWidth = 4;
  7176. Tab.IndentWidth = 8;
  7177. verifyFormat("class TabWidth4Indent8 {\n"
  7178. "\t\tvoid f() {\n"
  7179. "\t\t\t\tsomeFunction(parameter1,\n"
  7180. "\t\t\t\t\t\t\t parameter2);\n"
  7181. "\t\t}\n"
  7182. "};",
  7183. Tab);
  7184. Tab.TabWidth = 4;
  7185. Tab.IndentWidth = 4;
  7186. verifyFormat("class TabWidth4Indent4 {\n"
  7187. "\tvoid f() {\n"
  7188. "\t\tsomeFunction(parameter1,\n"
  7189. "\t\t\t\t\t parameter2);\n"
  7190. "\t}\n"
  7191. "};",
  7192. Tab);
  7193. Tab.TabWidth = 8;
  7194. Tab.IndentWidth = 4;
  7195. verifyFormat("class TabWidth8Indent4 {\n"
  7196. " void f() {\n"
  7197. "\tsomeFunction(parameter1,\n"
  7198. "\t\t parameter2);\n"
  7199. " }\n"
  7200. "};",
  7201. Tab);
  7202. Tab.TabWidth = 8;
  7203. Tab.IndentWidth = 8;
  7204. EXPECT_EQ("/*\n"
  7205. "\t a\t\tcomment\n"
  7206. "\t in multiple lines\n"
  7207. " */",
  7208. format(" /*\t \t \n"
  7209. " \t \t a\t\tcomment\t \t\n"
  7210. " \t \t in multiple lines\t\n"
  7211. " \t */",
  7212. Tab));
  7213. Tab.UseTab = FormatStyle::UT_ForIndentation;
  7214. verifyFormat("{\n"
  7215. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7216. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7217. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7218. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7219. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7220. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7221. "};",
  7222. Tab);
  7223. verifyFormat("enum A {\n"
  7224. "\ta1, // Force multiple lines\n"
  7225. "\ta2,\n"
  7226. "\ta3\n"
  7227. "};",
  7228. Tab);
  7229. EXPECT_EQ("if (aaaaaaaa && // q\n"
  7230. " bb) // w\n"
  7231. "\t;",
  7232. format("if (aaaaaaaa &&// q\n"
  7233. "bb)// w\n"
  7234. ";",
  7235. Tab));
  7236. verifyFormat("class X {\n"
  7237. "\tvoid f() {\n"
  7238. "\t\tsomeFunction(parameter1,\n"
  7239. "\t\t parameter2);\n"
  7240. "\t}\n"
  7241. "};",
  7242. Tab);
  7243. verifyFormat("{\n"
  7244. "\tQ({\n"
  7245. "\t\tint a;\n"
  7246. "\t\tsomeFunction(aaaaaaaa,\n"
  7247. "\t\t bbbbbbb);\n"
  7248. "\t}, p);\n"
  7249. "}",
  7250. Tab);
  7251. EXPECT_EQ("{\n"
  7252. "\t/* aaaa\n"
  7253. "\t bbbb */\n"
  7254. "}",
  7255. format("{\n"
  7256. "/* aaaa\n"
  7257. " bbbb */\n"
  7258. "}",
  7259. Tab));
  7260. EXPECT_EQ("{\n"
  7261. "\t/*\n"
  7262. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7263. "\t bbbbbbbbbbbbb\n"
  7264. "\t*/\n"
  7265. "}",
  7266. format("{\n"
  7267. "/*\n"
  7268. " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7269. "*/\n"
  7270. "}",
  7271. Tab));
  7272. EXPECT_EQ("{\n"
  7273. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7274. "\t// bbbbbbbbbbbbb\n"
  7275. "}",
  7276. format("{\n"
  7277. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7278. "}",
  7279. Tab));
  7280. EXPECT_EQ("{\n"
  7281. "\t/*\n"
  7282. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7283. "\t bbbbbbbbbbbbb\n"
  7284. "\t*/\n"
  7285. "}",
  7286. format("{\n"
  7287. "\t/*\n"
  7288. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7289. "\t*/\n"
  7290. "}",
  7291. Tab));
  7292. EXPECT_EQ("{\n"
  7293. "\t/*\n"
  7294. "\n"
  7295. "\t*/\n"
  7296. "}",
  7297. format("{\n"
  7298. "\t/*\n"
  7299. "\n"
  7300. "\t*/\n"
  7301. "}",
  7302. Tab));
  7303. EXPECT_EQ("{\n"
  7304. "\t/*\n"
  7305. " asdf\n"
  7306. "\t*/\n"
  7307. "}",
  7308. format("{\n"
  7309. "\t/*\n"
  7310. " asdf\n"
  7311. "\t*/\n"
  7312. "}",
  7313. Tab));
  7314. Tab.UseTab = FormatStyle::UT_Never;
  7315. EXPECT_EQ("/*\n"
  7316. " a\t\tcomment\n"
  7317. " in multiple lines\n"
  7318. " */",
  7319. format(" /*\t \t \n"
  7320. " \t \t a\t\tcomment\t \t\n"
  7321. " \t \t in multiple lines\t\n"
  7322. " \t */",
  7323. Tab));
  7324. EXPECT_EQ("/* some\n"
  7325. " comment */",
  7326. format(" \t \t /* some\n"
  7327. " \t \t comment */",
  7328. Tab));
  7329. EXPECT_EQ("int a; /* some\n"
  7330. " comment */",
  7331. format(" \t \t int a; /* some\n"
  7332. " \t \t comment */",
  7333. Tab));
  7334. EXPECT_EQ("int a; /* some\n"
  7335. "comment */",
  7336. format(" \t \t int\ta; /* some\n"
  7337. " \t \t comment */",
  7338. Tab));
  7339. EXPECT_EQ("f(\"\t\t\"); /* some\n"
  7340. " comment */",
  7341. format(" \t \t f(\"\t\t\"); /* some\n"
  7342. " \t \t comment */",
  7343. Tab));
  7344. EXPECT_EQ("{\n"
  7345. " /*\n"
  7346. " * Comment\n"
  7347. " */\n"
  7348. " int i;\n"
  7349. "}",
  7350. format("{\n"
  7351. "\t/*\n"
  7352. "\t * Comment\n"
  7353. "\t */\n"
  7354. "\t int i;\n"
  7355. "}"));
  7356. }
  7357. TEST_F(FormatTest, CalculatesOriginalColumn) {
  7358. EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7359. "q\"; /* some\n"
  7360. " comment */",
  7361. format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7362. "q\"; /* some\n"
  7363. " comment */",
  7364. getLLVMStyle()));
  7365. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  7366. "/* some\n"
  7367. " comment */",
  7368. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  7369. " /* some\n"
  7370. " comment */",
  7371. getLLVMStyle()));
  7372. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7373. "qqq\n"
  7374. "/* some\n"
  7375. " comment */",
  7376. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7377. "qqq\n"
  7378. " /* some\n"
  7379. " comment */",
  7380. getLLVMStyle()));
  7381. EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7382. "wwww; /* some\n"
  7383. " comment */",
  7384. format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7385. "wwww; /* some\n"
  7386. " comment */",
  7387. getLLVMStyle()));
  7388. }
  7389. TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
  7390. FormatStyle NoSpace = getLLVMStyle();
  7391. NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
  7392. verifyFormat("while(true)\n"
  7393. " continue;", NoSpace);
  7394. verifyFormat("for(;;)\n"
  7395. " continue;", NoSpace);
  7396. verifyFormat("if(true)\n"
  7397. " f();\n"
  7398. "else if(true)\n"
  7399. " f();", NoSpace);
  7400. verifyFormat("do {\n"
  7401. " do_something();\n"
  7402. "} while(something());", NoSpace);
  7403. verifyFormat("switch(x) {\n"
  7404. "default:\n"
  7405. " break;\n"
  7406. "}", NoSpace);
  7407. verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
  7408. verifyFormat("size_t x = sizeof(x);", NoSpace);
  7409. verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
  7410. verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
  7411. verifyFormat("alignas(128) char a[128];", NoSpace);
  7412. verifyFormat("size_t x = alignof(MyType);", NoSpace);
  7413. verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
  7414. verifyFormat("int f() throw(Deprecated);", NoSpace);
  7415. FormatStyle Space = getLLVMStyle();
  7416. Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
  7417. verifyFormat("int f ();", Space);
  7418. verifyFormat("void f (int a, T b) {\n"
  7419. " while (true)\n"
  7420. " continue;\n"
  7421. "}",
  7422. Space);
  7423. verifyFormat("if (true)\n"
  7424. " f ();\n"
  7425. "else if (true)\n"
  7426. " f ();",
  7427. Space);
  7428. verifyFormat("do {\n"
  7429. " do_something ();\n"
  7430. "} while (something ());",
  7431. Space);
  7432. verifyFormat("switch (x) {\n"
  7433. "default:\n"
  7434. " break;\n"
  7435. "}",
  7436. Space);
  7437. verifyFormat("A::A () : a (1) {}", Space);
  7438. verifyFormat("void f () __attribute__ ((asdf));", Space);
  7439. verifyFormat("*(&a + 1);\n"
  7440. "&((&a)[1]);\n"
  7441. "a[(b + c) * d];\n"
  7442. "(((a + 1) * 2) + 3) * 4;",
  7443. Space);
  7444. verifyFormat("#define A(x) x", Space);
  7445. verifyFormat("#define A (x) x", Space);
  7446. verifyFormat("#if defined(x)\n"
  7447. "#endif",
  7448. Space);
  7449. verifyFormat("auto i = std::make_unique<int> (5);", Space);
  7450. verifyFormat("size_t x = sizeof (x);", Space);
  7451. verifyFormat("auto f (int x) -> decltype (x);", Space);
  7452. verifyFormat("int f (T x) noexcept (x.create ());", Space);
  7453. verifyFormat("alignas (128) char a[128];", Space);
  7454. verifyFormat("size_t x = alignof (MyType);", Space);
  7455. verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
  7456. verifyFormat("int f () throw (Deprecated);", Space);
  7457. }
  7458. TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
  7459. FormatStyle Spaces = getLLVMStyle();
  7460. Spaces.SpacesInParentheses = true;
  7461. verifyFormat("call( x, y, z );", Spaces);
  7462. verifyFormat("while ( (bool)1 )\n"
  7463. " continue;", Spaces);
  7464. verifyFormat("for ( ;; )\n"
  7465. " continue;", Spaces);
  7466. verifyFormat("if ( true )\n"
  7467. " f();\n"
  7468. "else if ( true )\n"
  7469. " f();", Spaces);
  7470. verifyFormat("do {\n"
  7471. " do_something( (int)i );\n"
  7472. "} while ( something() );", Spaces);
  7473. verifyFormat("switch ( x ) {\n"
  7474. "default:\n"
  7475. " break;\n"
  7476. "}", Spaces);
  7477. Spaces.SpacesInParentheses = false;
  7478. Spaces.SpacesInCStyleCastParentheses = true;
  7479. verifyFormat("Type *A = ( Type * )P;", Spaces);
  7480. verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
  7481. verifyFormat("x = ( int32 )y;", Spaces);
  7482. verifyFormat("int a = ( int )(2.0f);", Spaces);
  7483. verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
  7484. verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
  7485. verifyFormat("#define x (( int )-1)", Spaces);
  7486. Spaces.SpacesInParentheses = false;
  7487. Spaces.SpaceInEmptyParentheses = true;
  7488. verifyFormat("call(x, y, z);", Spaces);
  7489. verifyFormat("call( )", Spaces);
  7490. // Run the first set of tests again with
  7491. // Spaces.SpacesInParentheses = false,
  7492. // Spaces.SpaceInEmptyParentheses = true and
  7493. // Spaces.SpacesInCStyleCastParentheses = true
  7494. Spaces.SpacesInParentheses = false,
  7495. Spaces.SpaceInEmptyParentheses = true;
  7496. Spaces.SpacesInCStyleCastParentheses = true;
  7497. verifyFormat("call(x, y, z);", Spaces);
  7498. verifyFormat("while (( bool )1)\n"
  7499. " continue;", Spaces);
  7500. verifyFormat("for (;;)\n"
  7501. " continue;", Spaces);
  7502. verifyFormat("if (true)\n"
  7503. " f( );\n"
  7504. "else if (true)\n"
  7505. " f( );", Spaces);
  7506. verifyFormat("do {\n"
  7507. " do_something(( int )i);\n"
  7508. "} while (something( ));", Spaces);
  7509. verifyFormat("switch (x) {\n"
  7510. "default:\n"
  7511. " break;\n"
  7512. "}", Spaces);
  7513. Spaces.SpaceAfterCStyleCast = true;
  7514. verifyFormat("call(x, y, z);", Spaces);
  7515. verifyFormat("while (( bool ) 1)\n"
  7516. " continue;",
  7517. Spaces);
  7518. verifyFormat("for (;;)\n"
  7519. " continue;",
  7520. Spaces);
  7521. verifyFormat("if (true)\n"
  7522. " f( );\n"
  7523. "else if (true)\n"
  7524. " f( );",
  7525. Spaces);
  7526. verifyFormat("do {\n"
  7527. " do_something(( int ) i);\n"
  7528. "} while (something( ));",
  7529. Spaces);
  7530. verifyFormat("switch (x) {\n"
  7531. "default:\n"
  7532. " break;\n"
  7533. "}",
  7534. Spaces);
  7535. Spaces.SpacesInCStyleCastParentheses = false;
  7536. Spaces.SpaceAfterCStyleCast = true;
  7537. verifyFormat("while ((bool) 1)\n"
  7538. " continue;",
  7539. Spaces);
  7540. verifyFormat("do {\n"
  7541. " do_something((int) i);\n"
  7542. "} while (something( ));",
  7543. Spaces);
  7544. }
  7545. TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
  7546. verifyFormat("int a[5];");
  7547. verifyFormat("a[3] += 42;");
  7548. FormatStyle Spaces = getLLVMStyle();
  7549. Spaces.SpacesInSquareBrackets = true;
  7550. // Lambdas unchanged.
  7551. verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
  7552. verifyFormat("return [i, args...] {};", Spaces);
  7553. // Not lambdas.
  7554. verifyFormat("int a[ 5 ];", Spaces);
  7555. verifyFormat("a[ 3 ] += 42;", Spaces);
  7556. verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
  7557. verifyFormat("double &operator[](int i) { return 0; }\n"
  7558. "int i;",
  7559. Spaces);
  7560. verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
  7561. verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
  7562. verifyFormat("int i = (*b)[ a ]->f();", Spaces);
  7563. }
  7564. TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
  7565. verifyFormat("int a = 5;");
  7566. verifyFormat("a += 42;");
  7567. verifyFormat("a or_eq 8;");
  7568. FormatStyle Spaces = getLLVMStyle();
  7569. Spaces.SpaceBeforeAssignmentOperators = false;
  7570. verifyFormat("int a= 5;", Spaces);
  7571. verifyFormat("a+= 42;", Spaces);
  7572. verifyFormat("a or_eq 8;", Spaces);
  7573. }
  7574. TEST_F(FormatTest, LinuxBraceBreaking) {
  7575. FormatStyle LinuxBraceStyle = getLLVMStyle();
  7576. LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
  7577. verifyFormat("namespace a\n"
  7578. "{\n"
  7579. "class A\n"
  7580. "{\n"
  7581. " void f()\n"
  7582. " {\n"
  7583. " if (true) {\n"
  7584. " a();\n"
  7585. " b();\n"
  7586. " }\n"
  7587. " }\n"
  7588. " void g() { return; }\n"
  7589. "};\n"
  7590. "struct B {\n"
  7591. " int x;\n"
  7592. "};\n"
  7593. "}\n",
  7594. LinuxBraceStyle);
  7595. verifyFormat("enum X {\n"
  7596. " Y = 0,\n"
  7597. "}\n",
  7598. LinuxBraceStyle);
  7599. verifyFormat("struct S {\n"
  7600. " int Type;\n"
  7601. " union {\n"
  7602. " int x;\n"
  7603. " double y;\n"
  7604. " } Value;\n"
  7605. " class C\n"
  7606. " {\n"
  7607. " MyFavoriteType Value;\n"
  7608. " } Class;\n"
  7609. "}\n",
  7610. LinuxBraceStyle);
  7611. }
  7612. TEST_F(FormatTest, StroustrupBraceBreaking) {
  7613. FormatStyle StroustrupBraceStyle = getLLVMStyle();
  7614. StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  7615. verifyFormat("namespace a {\n"
  7616. "class A {\n"
  7617. " void f()\n"
  7618. " {\n"
  7619. " if (true) {\n"
  7620. " a();\n"
  7621. " b();\n"
  7622. " }\n"
  7623. " }\n"
  7624. " void g() { return; }\n"
  7625. "};\n"
  7626. "struct B {\n"
  7627. " int x;\n"
  7628. "};\n"
  7629. "}\n",
  7630. StroustrupBraceStyle);
  7631. verifyFormat("void foo()\n"
  7632. "{\n"
  7633. " if (a) {\n"
  7634. " a();\n"
  7635. " }\n"
  7636. " else {\n"
  7637. " b();\n"
  7638. " }\n"
  7639. "}\n",
  7640. StroustrupBraceStyle);
  7641. verifyFormat("#ifdef _DEBUG\n"
  7642. "int foo(int i = 0)\n"
  7643. "#else\n"
  7644. "int foo(int i = 5)\n"
  7645. "#endif\n"
  7646. "{\n"
  7647. " return i;\n"
  7648. "}",
  7649. StroustrupBraceStyle);
  7650. verifyFormat("void foo() {}\n"
  7651. "void bar()\n"
  7652. "#ifdef _DEBUG\n"
  7653. "{\n"
  7654. " foo();\n"
  7655. "}\n"
  7656. "#else\n"
  7657. "{\n"
  7658. "}\n"
  7659. "#endif",
  7660. StroustrupBraceStyle);
  7661. verifyFormat("void foobar() { int i = 5; }\n"
  7662. "#ifdef _DEBUG\n"
  7663. "void bar() {}\n"
  7664. "#else\n"
  7665. "void bar() { foobar(); }\n"
  7666. "#endif",
  7667. StroustrupBraceStyle);
  7668. }
  7669. TEST_F(FormatTest, AllmanBraceBreaking) {
  7670. FormatStyle AllmanBraceStyle = getLLVMStyle();
  7671. AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
  7672. verifyFormat("namespace a\n"
  7673. "{\n"
  7674. "class A\n"
  7675. "{\n"
  7676. " void f()\n"
  7677. " {\n"
  7678. " if (true)\n"
  7679. " {\n"
  7680. " a();\n"
  7681. " b();\n"
  7682. " }\n"
  7683. " }\n"
  7684. " void g() { return; }\n"
  7685. "};\n"
  7686. "struct B\n"
  7687. "{\n"
  7688. " int x;\n"
  7689. "};\n"
  7690. "}",
  7691. AllmanBraceStyle);
  7692. verifyFormat("void f()\n"
  7693. "{\n"
  7694. " if (true)\n"
  7695. " {\n"
  7696. " a();\n"
  7697. " }\n"
  7698. " else if (false)\n"
  7699. " {\n"
  7700. " b();\n"
  7701. " }\n"
  7702. " else\n"
  7703. " {\n"
  7704. " c();\n"
  7705. " }\n"
  7706. "}\n",
  7707. AllmanBraceStyle);
  7708. verifyFormat("void f()\n"
  7709. "{\n"
  7710. " for (int i = 0; i < 10; ++i)\n"
  7711. " {\n"
  7712. " a();\n"
  7713. " }\n"
  7714. " while (false)\n"
  7715. " {\n"
  7716. " b();\n"
  7717. " }\n"
  7718. " do\n"
  7719. " {\n"
  7720. " c();\n"
  7721. " } while (false)\n"
  7722. "}\n",
  7723. AllmanBraceStyle);
  7724. verifyFormat("void f(int a)\n"
  7725. "{\n"
  7726. " switch (a)\n"
  7727. " {\n"
  7728. " case 0:\n"
  7729. " break;\n"
  7730. " case 1:\n"
  7731. " {\n"
  7732. " break;\n"
  7733. " }\n"
  7734. " case 2:\n"
  7735. " {\n"
  7736. " }\n"
  7737. " break;\n"
  7738. " default:\n"
  7739. " break;\n"
  7740. " }\n"
  7741. "}\n",
  7742. AllmanBraceStyle);
  7743. verifyFormat("enum X\n"
  7744. "{\n"
  7745. " Y = 0,\n"
  7746. "}\n",
  7747. AllmanBraceStyle);
  7748. verifyFormat("enum X\n"
  7749. "{\n"
  7750. " Y = 0\n"
  7751. "}\n",
  7752. AllmanBraceStyle);
  7753. verifyFormat("@interface BSApplicationController ()\n"
  7754. "{\n"
  7755. "@private\n"
  7756. " id _extraIvar;\n"
  7757. "}\n"
  7758. "@end\n",
  7759. AllmanBraceStyle);
  7760. verifyFormat("#ifdef _DEBUG\n"
  7761. "int foo(int i = 0)\n"
  7762. "#else\n"
  7763. "int foo(int i = 5)\n"
  7764. "#endif\n"
  7765. "{\n"
  7766. " return i;\n"
  7767. "}",
  7768. AllmanBraceStyle);
  7769. verifyFormat("void foo() {}\n"
  7770. "void bar()\n"
  7771. "#ifdef _DEBUG\n"
  7772. "{\n"
  7773. " foo();\n"
  7774. "}\n"
  7775. "#else\n"
  7776. "{\n"
  7777. "}\n"
  7778. "#endif",
  7779. AllmanBraceStyle);
  7780. verifyFormat("void foobar() { int i = 5; }\n"
  7781. "#ifdef _DEBUG\n"
  7782. "void bar() {}\n"
  7783. "#else\n"
  7784. "void bar() { foobar(); }\n"
  7785. "#endif",
  7786. AllmanBraceStyle);
  7787. // This shouldn't affect ObjC blocks..
  7788. verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
  7789. " // ...\n"
  7790. " int i;\n"
  7791. "}];",
  7792. AllmanBraceStyle);
  7793. verifyFormat("void (^block)(void) = ^{\n"
  7794. " // ...\n"
  7795. " int i;\n"
  7796. "};",
  7797. AllmanBraceStyle);
  7798. // .. or dict literals.
  7799. verifyFormat("void f()\n"
  7800. "{\n"
  7801. " [object someMethod:@{ @\"a\" : @\"b\" }];\n"
  7802. "}",
  7803. AllmanBraceStyle);
  7804. verifyFormat("int f()\n"
  7805. "{ // comment\n"
  7806. " return 42;\n"
  7807. "}",
  7808. AllmanBraceStyle);
  7809. AllmanBraceStyle.ColumnLimit = 19;
  7810. verifyFormat("void f() { int i; }", AllmanBraceStyle);
  7811. AllmanBraceStyle.ColumnLimit = 18;
  7812. verifyFormat("void f()\n"
  7813. "{\n"
  7814. " int i;\n"
  7815. "}",
  7816. AllmanBraceStyle);
  7817. AllmanBraceStyle.ColumnLimit = 80;
  7818. FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
  7819. BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
  7820. BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
  7821. verifyFormat("void f(bool b)\n"
  7822. "{\n"
  7823. " if (b)\n"
  7824. " {\n"
  7825. " return;\n"
  7826. " }\n"
  7827. "}\n",
  7828. BreakBeforeBraceShortIfs);
  7829. verifyFormat("void f(bool b)\n"
  7830. "{\n"
  7831. " if (b) return;\n"
  7832. "}\n",
  7833. BreakBeforeBraceShortIfs);
  7834. verifyFormat("void f(bool b)\n"
  7835. "{\n"
  7836. " while (b)\n"
  7837. " {\n"
  7838. " return;\n"
  7839. " }\n"
  7840. "}\n",
  7841. BreakBeforeBraceShortIfs);
  7842. }
  7843. TEST_F(FormatTest, GNUBraceBreaking) {
  7844. FormatStyle GNUBraceStyle = getLLVMStyle();
  7845. GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
  7846. verifyFormat("namespace a\n"
  7847. "{\n"
  7848. "class A\n"
  7849. "{\n"
  7850. " void f()\n"
  7851. " {\n"
  7852. " int a;\n"
  7853. " {\n"
  7854. " int b;\n"
  7855. " }\n"
  7856. " if (true)\n"
  7857. " {\n"
  7858. " a();\n"
  7859. " b();\n"
  7860. " }\n"
  7861. " }\n"
  7862. " void g() { return; }\n"
  7863. "}\n"
  7864. "}",
  7865. GNUBraceStyle);
  7866. verifyFormat("void f()\n"
  7867. "{\n"
  7868. " if (true)\n"
  7869. " {\n"
  7870. " a();\n"
  7871. " }\n"
  7872. " else if (false)\n"
  7873. " {\n"
  7874. " b();\n"
  7875. " }\n"
  7876. " else\n"
  7877. " {\n"
  7878. " c();\n"
  7879. " }\n"
  7880. "}\n",
  7881. GNUBraceStyle);
  7882. verifyFormat("void f()\n"
  7883. "{\n"
  7884. " for (int i = 0; i < 10; ++i)\n"
  7885. " {\n"
  7886. " a();\n"
  7887. " }\n"
  7888. " while (false)\n"
  7889. " {\n"
  7890. " b();\n"
  7891. " }\n"
  7892. " do\n"
  7893. " {\n"
  7894. " c();\n"
  7895. " }\n"
  7896. " while (false);\n"
  7897. "}\n",
  7898. GNUBraceStyle);
  7899. verifyFormat("void f(int a)\n"
  7900. "{\n"
  7901. " switch (a)\n"
  7902. " {\n"
  7903. " case 0:\n"
  7904. " break;\n"
  7905. " case 1:\n"
  7906. " {\n"
  7907. " break;\n"
  7908. " }\n"
  7909. " case 2:\n"
  7910. " {\n"
  7911. " }\n"
  7912. " break;\n"
  7913. " default:\n"
  7914. " break;\n"
  7915. " }\n"
  7916. "}\n",
  7917. GNUBraceStyle);
  7918. verifyFormat("enum X\n"
  7919. "{\n"
  7920. " Y = 0,\n"
  7921. "}\n",
  7922. GNUBraceStyle);
  7923. verifyFormat("@interface BSApplicationController ()\n"
  7924. "{\n"
  7925. "@private\n"
  7926. " id _extraIvar;\n"
  7927. "}\n"
  7928. "@end\n",
  7929. GNUBraceStyle);
  7930. verifyFormat("#ifdef _DEBUG\n"
  7931. "int foo(int i = 0)\n"
  7932. "#else\n"
  7933. "int foo(int i = 5)\n"
  7934. "#endif\n"
  7935. "{\n"
  7936. " return i;\n"
  7937. "}",
  7938. GNUBraceStyle);
  7939. verifyFormat("void foo() {}\n"
  7940. "void bar()\n"
  7941. "#ifdef _DEBUG\n"
  7942. "{\n"
  7943. " foo();\n"
  7944. "}\n"
  7945. "#else\n"
  7946. "{\n"
  7947. "}\n"
  7948. "#endif",
  7949. GNUBraceStyle);
  7950. verifyFormat("void foobar() { int i = 5; }\n"
  7951. "#ifdef _DEBUG\n"
  7952. "void bar() {}\n"
  7953. "#else\n"
  7954. "void bar() { foobar(); }\n"
  7955. "#endif",
  7956. GNUBraceStyle);
  7957. }
  7958. TEST_F(FormatTest, CatchExceptionReferenceBinding) {
  7959. verifyFormat("void f() {\n"
  7960. " try {\n"
  7961. " } catch (const Exception &e) {\n"
  7962. " }\n"
  7963. "}\n",
  7964. getLLVMStyle());
  7965. }
  7966. TEST_F(FormatTest, UnderstandsPragmas) {
  7967. verifyFormat("#pragma omp reduction(| : var)");
  7968. verifyFormat("#pragma omp reduction(+ : var)");
  7969. EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
  7970. "(including parentheses).",
  7971. format("#pragma mark Any non-hyphenated or hyphenated string "
  7972. "(including parentheses)."));
  7973. }
  7974. #define EXPECT_ALL_STYLES_EQUAL(Styles) \
  7975. for (size_t i = 1; i < Styles.size(); ++i) \
  7976. EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " \
  7977. << Styles.size() \
  7978. << " differs from Style #0"
  7979. TEST_F(FormatTest, GetsPredefinedStyleByName) {
  7980. SmallVector<FormatStyle, 3> Styles;
  7981. Styles.resize(3);
  7982. Styles[0] = getLLVMStyle();
  7983. EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
  7984. EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
  7985. EXPECT_ALL_STYLES_EQUAL(Styles);
  7986. Styles[0] = getGoogleStyle();
  7987. EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
  7988. EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
  7989. EXPECT_ALL_STYLES_EQUAL(Styles);
  7990. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  7991. EXPECT_TRUE(
  7992. getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
  7993. EXPECT_TRUE(
  7994. getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
  7995. EXPECT_ALL_STYLES_EQUAL(Styles);
  7996. Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
  7997. EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
  7998. EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
  7999. EXPECT_ALL_STYLES_EQUAL(Styles);
  8000. Styles[0] = getMozillaStyle();
  8001. EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
  8002. EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
  8003. EXPECT_ALL_STYLES_EQUAL(Styles);
  8004. Styles[0] = getWebKitStyle();
  8005. EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
  8006. EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
  8007. EXPECT_ALL_STYLES_EQUAL(Styles);
  8008. Styles[0] = getGNUStyle();
  8009. EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
  8010. EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
  8011. EXPECT_ALL_STYLES_EQUAL(Styles);
  8012. EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
  8013. }
  8014. TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
  8015. SmallVector<FormatStyle, 8> Styles;
  8016. Styles.resize(2);
  8017. Styles[0] = getGoogleStyle();
  8018. Styles[1] = getLLVMStyle();
  8019. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  8020. EXPECT_ALL_STYLES_EQUAL(Styles);
  8021. Styles.resize(5);
  8022. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  8023. Styles[1] = getLLVMStyle();
  8024. Styles[1].Language = FormatStyle::LK_JavaScript;
  8025. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  8026. Styles[2] = getLLVMStyle();
  8027. Styles[2].Language = FormatStyle::LK_JavaScript;
  8028. EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
  8029. "BasedOnStyle: Google",
  8030. &Styles[2]).value());
  8031. Styles[3] = getLLVMStyle();
  8032. Styles[3].Language = FormatStyle::LK_JavaScript;
  8033. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
  8034. "Language: JavaScript",
  8035. &Styles[3]).value());
  8036. Styles[4] = getLLVMStyle();
  8037. Styles[4].Language = FormatStyle::LK_JavaScript;
  8038. EXPECT_EQ(0, parseConfiguration("---\n"
  8039. "BasedOnStyle: LLVM\n"
  8040. "IndentWidth: 123\n"
  8041. "---\n"
  8042. "BasedOnStyle: Google\n"
  8043. "Language: JavaScript",
  8044. &Styles[4]).value());
  8045. EXPECT_ALL_STYLES_EQUAL(Styles);
  8046. }
  8047. #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \
  8048. Style.FIELD = false; \
  8049. EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \
  8050. EXPECT_TRUE(Style.FIELD); \
  8051. EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \
  8052. EXPECT_FALSE(Style.FIELD);
  8053. #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
  8054. #define CHECK_PARSE(TEXT, FIELD, VALUE) \
  8055. EXPECT_NE(VALUE, Style.FIELD); \
  8056. EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \
  8057. EXPECT_EQ(VALUE, Style.FIELD)
  8058. TEST_F(FormatTest, ParsesConfigurationBools) {
  8059. FormatStyle Style = {};
  8060. Style.Language = FormatStyle::LK_Cpp;
  8061. CHECK_PARSE_BOOL(AlignAfterOpenBracket);
  8062. CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
  8063. CHECK_PARSE_BOOL(AlignOperands);
  8064. CHECK_PARSE_BOOL(AlignTrailingComments);
  8065. CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
  8066. CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
  8067. CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
  8068. CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
  8069. CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
  8070. CHECK_PARSE_BOOL(AlwaysBreakAfterDefinitionReturnType);
  8071. CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
  8072. CHECK_PARSE_BOOL(BinPackParameters);
  8073. CHECK_PARSE_BOOL(BinPackArguments);
  8074. CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
  8075. CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
  8076. CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
  8077. CHECK_PARSE_BOOL(DerivePointerAlignment);
  8078. CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
  8079. CHECK_PARSE_BOOL(IndentCaseLabels);
  8080. CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
  8081. CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
  8082. CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
  8083. CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
  8084. CHECK_PARSE_BOOL(Cpp11BracedListStyle);
  8085. CHECK_PARSE_BOOL(SpacesInParentheses);
  8086. CHECK_PARSE_BOOL(SpacesInSquareBrackets);
  8087. CHECK_PARSE_BOOL(SpacesInAngles);
  8088. CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
  8089. CHECK_PARSE_BOOL(SpacesInContainerLiterals);
  8090. CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
  8091. CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
  8092. CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
  8093. }
  8094. #undef CHECK_PARSE_BOOL
  8095. TEST_F(FormatTest, ParsesConfiguration) {
  8096. FormatStyle Style = {};
  8097. Style.Language = FormatStyle::LK_Cpp;
  8098. CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
  8099. CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
  8100. ConstructorInitializerIndentWidth, 1234u);
  8101. CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
  8102. CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
  8103. CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
  8104. CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
  8105. PenaltyBreakBeforeFirstCallParameter, 1234u);
  8106. CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
  8107. CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
  8108. PenaltyReturnTypeOnItsOwnLine, 1234u);
  8109. CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
  8110. SpacesBeforeTrailingComments, 1234u);
  8111. CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
  8112. CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
  8113. Style.PointerAlignment = FormatStyle::PAS_Middle;
  8114. CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
  8115. FormatStyle::PAS_Left);
  8116. CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
  8117. FormatStyle::PAS_Right);
  8118. CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
  8119. FormatStyle::PAS_Middle);
  8120. // For backward compatibility:
  8121. CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
  8122. FormatStyle::PAS_Left);
  8123. CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
  8124. FormatStyle::PAS_Right);
  8125. CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
  8126. FormatStyle::PAS_Middle);
  8127. Style.Standard = FormatStyle::LS_Auto;
  8128. CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
  8129. CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
  8130. CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
  8131. CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
  8132. CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
  8133. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  8134. CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
  8135. BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
  8136. CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
  8137. FormatStyle::BOS_None);
  8138. CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
  8139. FormatStyle::BOS_All);
  8140. // For backward compatibility:
  8141. CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
  8142. FormatStyle::BOS_None);
  8143. CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
  8144. FormatStyle::BOS_All);
  8145. Style.UseTab = FormatStyle::UT_ForIndentation;
  8146. CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
  8147. CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
  8148. CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
  8149. // For backward compatibility:
  8150. CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
  8151. CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
  8152. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  8153. CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
  8154. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
  8155. CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
  8156. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
  8157. CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
  8158. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
  8159. CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
  8160. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
  8161. // For backward compatibility:
  8162. CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
  8163. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
  8164. CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
  8165. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
  8166. Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
  8167. CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
  8168. FormatStyle::SBPO_Never);
  8169. CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
  8170. FormatStyle::SBPO_Always);
  8171. CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
  8172. FormatStyle::SBPO_ControlStatements);
  8173. // For backward compatibility:
  8174. CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
  8175. FormatStyle::SBPO_Never);
  8176. CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
  8177. FormatStyle::SBPO_ControlStatements);
  8178. Style.ColumnLimit = 123;
  8179. FormatStyle BaseStyle = getLLVMStyle();
  8180. CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
  8181. CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
  8182. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  8183. CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
  8184. FormatStyle::BS_Attach);
  8185. CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
  8186. FormatStyle::BS_Linux);
  8187. CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
  8188. FormatStyle::BS_Stroustrup);
  8189. CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
  8190. FormatStyle::BS_Allman);
  8191. CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
  8192. Style.NamespaceIndentation = FormatStyle::NI_All;
  8193. CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
  8194. FormatStyle::NI_None);
  8195. CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
  8196. FormatStyle::NI_Inner);
  8197. CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
  8198. FormatStyle::NI_All);
  8199. Style.ForEachMacros.clear();
  8200. std::vector<std::string> BoostForeach;
  8201. BoostForeach.push_back("BOOST_FOREACH");
  8202. CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
  8203. std::vector<std::string> BoostAndQForeach;
  8204. BoostAndQForeach.push_back("BOOST_FOREACH");
  8205. BoostAndQForeach.push_back("Q_FOREACH");
  8206. CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
  8207. BoostAndQForeach);
  8208. }
  8209. TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
  8210. FormatStyle Style = {};
  8211. Style.Language = FormatStyle::LK_Cpp;
  8212. CHECK_PARSE("Language: Cpp\n"
  8213. "IndentWidth: 12",
  8214. IndentWidth, 12u);
  8215. EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
  8216. "IndentWidth: 34",
  8217. &Style),
  8218. ParseError::Unsuitable);
  8219. EXPECT_EQ(12u, Style.IndentWidth);
  8220. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  8221. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  8222. Style.Language = FormatStyle::LK_JavaScript;
  8223. CHECK_PARSE("Language: JavaScript\n"
  8224. "IndentWidth: 12",
  8225. IndentWidth, 12u);
  8226. CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
  8227. EXPECT_EQ(parseConfiguration("Language: Cpp\n"
  8228. "IndentWidth: 34",
  8229. &Style),
  8230. ParseError::Unsuitable);
  8231. EXPECT_EQ(23u, Style.IndentWidth);
  8232. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  8233. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  8234. CHECK_PARSE("BasedOnStyle: LLVM\n"
  8235. "IndentWidth: 67",
  8236. IndentWidth, 67u);
  8237. CHECK_PARSE("---\n"
  8238. "Language: JavaScript\n"
  8239. "IndentWidth: 12\n"
  8240. "---\n"
  8241. "Language: Cpp\n"
  8242. "IndentWidth: 34\n"
  8243. "...\n",
  8244. IndentWidth, 12u);
  8245. Style.Language = FormatStyle::LK_Cpp;
  8246. CHECK_PARSE("---\n"
  8247. "Language: JavaScript\n"
  8248. "IndentWidth: 12\n"
  8249. "---\n"
  8250. "Language: Cpp\n"
  8251. "IndentWidth: 34\n"
  8252. "...\n",
  8253. IndentWidth, 34u);
  8254. CHECK_PARSE("---\n"
  8255. "IndentWidth: 78\n"
  8256. "---\n"
  8257. "Language: JavaScript\n"
  8258. "IndentWidth: 56\n"
  8259. "...\n",
  8260. IndentWidth, 78u);
  8261. Style.ColumnLimit = 123;
  8262. Style.IndentWidth = 234;
  8263. Style.BreakBeforeBraces = FormatStyle::BS_Linux;
  8264. Style.TabWidth = 345;
  8265. EXPECT_FALSE(parseConfiguration("---\n"
  8266. "IndentWidth: 456\n"
  8267. "BreakBeforeBraces: Allman\n"
  8268. "---\n"
  8269. "Language: JavaScript\n"
  8270. "IndentWidth: 111\n"
  8271. "TabWidth: 111\n"
  8272. "---\n"
  8273. "Language: Cpp\n"
  8274. "BreakBeforeBraces: Stroustrup\n"
  8275. "TabWidth: 789\n"
  8276. "...\n",
  8277. &Style));
  8278. EXPECT_EQ(123u, Style.ColumnLimit);
  8279. EXPECT_EQ(456u, Style.IndentWidth);
  8280. EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
  8281. EXPECT_EQ(789u, Style.TabWidth);
  8282. EXPECT_EQ(parseConfiguration("---\n"
  8283. "Language: JavaScript\n"
  8284. "IndentWidth: 56\n"
  8285. "---\n"
  8286. "IndentWidth: 78\n"
  8287. "...\n",
  8288. &Style),
  8289. ParseError::Error);
  8290. EXPECT_EQ(parseConfiguration("---\n"
  8291. "Language: JavaScript\n"
  8292. "IndentWidth: 56\n"
  8293. "---\n"
  8294. "Language: JavaScript\n"
  8295. "IndentWidth: 78\n"
  8296. "...\n",
  8297. &Style),
  8298. ParseError::Error);
  8299. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  8300. }
  8301. #undef CHECK_PARSE
  8302. TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
  8303. FormatStyle Style = {};
  8304. Style.Language = FormatStyle::LK_JavaScript;
  8305. Style.BreakBeforeTernaryOperators = true;
  8306. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
  8307. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  8308. Style.BreakBeforeTernaryOperators = true;
  8309. EXPECT_EQ(0, parseConfiguration("---\n"
  8310. "BasedOnStyle: Google\n"
  8311. "---\n"
  8312. "Language: JavaScript\n"
  8313. "IndentWidth: 76\n"
  8314. "...\n", &Style).value());
  8315. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  8316. EXPECT_EQ(76u, Style.IndentWidth);
  8317. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  8318. }
  8319. TEST_F(FormatTest, ConfigurationRoundTripTest) {
  8320. FormatStyle Style = getLLVMStyle();
  8321. std::string YAML = configurationAsText(Style);
  8322. FormatStyle ParsedStyle = {};
  8323. ParsedStyle.Language = FormatStyle::LK_Cpp;
  8324. EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
  8325. EXPECT_EQ(Style, ParsedStyle);
  8326. }
  8327. TEST_F(FormatTest, WorksFor8bitEncodings) {
  8328. EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
  8329. "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
  8330. "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
  8331. "\"\xef\xee\xf0\xf3...\"",
  8332. format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
  8333. "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
  8334. "\xef\xee\xf0\xf3...\"",
  8335. getLLVMStyleWithColumns(12)));
  8336. }
  8337. TEST_F(FormatTest, HandlesUTF8BOM) {
  8338. EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
  8339. EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
  8340. format("\xef\xbb\xbf#include <iostream>"));
  8341. EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
  8342. format("\xef\xbb\xbf\n#include <iostream>"));
  8343. }
  8344. // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
  8345. #if !defined(_MSC_VER)
  8346. TEST_F(FormatTest, CountsUTF8CharactersProperly) {
  8347. verifyFormat("\"Однажды в студёную зимнюю пору...\"",
  8348. getLLVMStyleWithColumns(35));
  8349. verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
  8350. getLLVMStyleWithColumns(31));
  8351. verifyFormat("// Однажды в студёную зимнюю пору...",
  8352. getLLVMStyleWithColumns(36));
  8353. verifyFormat("// 一 二 三 四 五 六 七 八 九 十",
  8354. getLLVMStyleWithColumns(32));
  8355. verifyFormat("/* Однажды в студёную зимнюю пору... */",
  8356. getLLVMStyleWithColumns(39));
  8357. verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
  8358. getLLVMStyleWithColumns(35));
  8359. }
  8360. TEST_F(FormatTest, SplitsUTF8Strings) {
  8361. // Non-printable characters' width is currently considered to be the length in
  8362. // bytes in UTF8. The characters can be displayed in very different manner
  8363. // (zero-width, single width with a substitution glyph, expanded to their code
  8364. // (e.g. "<8d>"), so there's no single correct way to handle them.
  8365. EXPECT_EQ("\"aaaaÄ\"\n"
  8366. "\"\xc2\x8d\";",
  8367. format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  8368. EXPECT_EQ("\"aaaaaaaÄ\"\n"
  8369. "\"\xc2\x8d\";",
  8370. format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  8371. EXPECT_EQ(
  8372. "\"Однажды, в \"\n"
  8373. "\"студёную \"\n"
  8374. "\"зимнюю \"\n"
  8375. "\"пору,\"",
  8376. format("\"Однажды, в студёную зимнюю пору,\"",
  8377. getLLVMStyleWithColumns(13)));
  8378. EXPECT_EQ("\"一 二 三 \"\n"
  8379. "\"四 五六 \"\n"
  8380. "\"七 八 九 \"\n"
  8381. "\"十\"",
  8382. format("\"一 二 三 四 五六 七 八 九 十\"",
  8383. getLLVMStyleWithColumns(11)));
  8384. EXPECT_EQ("\"一\t二 \"\n"
  8385. "\"\t三 \"\n"
  8386. "\"四 五\t六 \"\n"
  8387. "\"\t七 \"\n"
  8388. "\"八九十\tqq\"",
  8389. format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
  8390. getLLVMStyleWithColumns(11)));
  8391. }
  8392. TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
  8393. EXPECT_EQ("const char *sssss =\n"
  8394. " \"一二三四五六七八\\\n"
  8395. " 九 十\";",
  8396. format("const char *sssss = \"一二三四五六七八\\\n"
  8397. " 九 十\";",
  8398. getLLVMStyleWithColumns(30)));
  8399. }
  8400. TEST_F(FormatTest, SplitsUTF8LineComments) {
  8401. EXPECT_EQ("// aaaaÄ\xc2\x8d",
  8402. format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
  8403. EXPECT_EQ("// Я из лесу\n"
  8404. "// вышел; был\n"
  8405. "// сильный\n"
  8406. "// мороз.",
  8407. format("// Я из лесу вышел; был сильный мороз.",
  8408. getLLVMStyleWithColumns(13)));
  8409. EXPECT_EQ("// 一二三\n"
  8410. "// 四五六七\n"
  8411. "// 八 九\n"
  8412. "// 十",
  8413. format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9)));
  8414. }
  8415. TEST_F(FormatTest, SplitsUTF8BlockComments) {
  8416. EXPECT_EQ("/* Гляжу,\n"
  8417. " * поднимается\n"
  8418. " * медленно в\n"
  8419. " * гору\n"
  8420. " * Лошадка,\n"
  8421. " * везущая\n"
  8422. " * хворосту\n"
  8423. " * воз. */",
  8424. format("/* Гляжу, поднимается медленно в гору\n"
  8425. " * Лошадка, везущая хворосту воз. */",
  8426. getLLVMStyleWithColumns(13)));
  8427. EXPECT_EQ(
  8428. "/* 一二三\n"
  8429. " * 四五六七\n"
  8430. " * 八 九\n"
  8431. " * 十 */",
  8432. format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9)));
  8433. EXPECT_EQ("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
  8434. " * 𝕓𝕪𝕥𝕖\n"
  8435. " * 𝖀𝕿𝕱-𝟠 */",
  8436. format("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12)));
  8437. }
  8438. #endif // _MSC_VER
  8439. TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
  8440. FormatStyle Style = getLLVMStyle();
  8441. Style.ConstructorInitializerIndentWidth = 4;
  8442. verifyFormat(
  8443. "SomeClass::Constructor()\n"
  8444. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  8445. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  8446. Style);
  8447. Style.ConstructorInitializerIndentWidth = 2;
  8448. verifyFormat(
  8449. "SomeClass::Constructor()\n"
  8450. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  8451. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  8452. Style);
  8453. Style.ConstructorInitializerIndentWidth = 0;
  8454. verifyFormat(
  8455. "SomeClass::Constructor()\n"
  8456. ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  8457. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  8458. Style);
  8459. }
  8460. TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
  8461. FormatStyle Style = getLLVMStyle();
  8462. Style.BreakConstructorInitializersBeforeComma = true;
  8463. Style.ConstructorInitializerIndentWidth = 4;
  8464. verifyFormat("SomeClass::Constructor()\n"
  8465. " : a(a)\n"
  8466. " , b(b)\n"
  8467. " , c(c) {}",
  8468. Style);
  8469. verifyFormat("SomeClass::Constructor()\n"
  8470. " : a(a) {}",
  8471. Style);
  8472. Style.ColumnLimit = 0;
  8473. verifyFormat("SomeClass::Constructor()\n"
  8474. " : a(a) {}",
  8475. Style);
  8476. verifyFormat("SomeClass::Constructor()\n"
  8477. " : a(a)\n"
  8478. " , b(b)\n"
  8479. " , c(c) {}",
  8480. Style);
  8481. verifyFormat("SomeClass::Constructor()\n"
  8482. " : a(a) {\n"
  8483. " foo();\n"
  8484. " bar();\n"
  8485. "}",
  8486. Style);
  8487. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  8488. verifyFormat("SomeClass::Constructor()\n"
  8489. " : a(a)\n"
  8490. " , b(b)\n"
  8491. " , c(c) {\n}",
  8492. Style);
  8493. verifyFormat("SomeClass::Constructor()\n"
  8494. " : a(a) {\n}",
  8495. Style);
  8496. Style.ColumnLimit = 80;
  8497. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
  8498. Style.ConstructorInitializerIndentWidth = 2;
  8499. verifyFormat("SomeClass::Constructor()\n"
  8500. " : a(a)\n"
  8501. " , b(b)\n"
  8502. " , c(c) {}",
  8503. Style);
  8504. Style.ConstructorInitializerIndentWidth = 0;
  8505. verifyFormat("SomeClass::Constructor()\n"
  8506. ": a(a)\n"
  8507. ", b(b)\n"
  8508. ", c(c) {}",
  8509. Style);
  8510. Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  8511. Style.ConstructorInitializerIndentWidth = 4;
  8512. verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
  8513. verifyFormat(
  8514. "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
  8515. Style);
  8516. verifyFormat(
  8517. "SomeClass::Constructor()\n"
  8518. " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
  8519. Style);
  8520. Style.ConstructorInitializerIndentWidth = 4;
  8521. Style.ColumnLimit = 60;
  8522. verifyFormat("SomeClass::Constructor()\n"
  8523. " : aaaaaaaa(aaaaaaaa)\n"
  8524. " , aaaaaaaa(aaaaaaaa)\n"
  8525. " , aaaaaaaa(aaaaaaaa) {}",
  8526. Style);
  8527. }
  8528. TEST_F(FormatTest, Destructors) {
  8529. verifyFormat("void F(int &i) { i.~int(); }");
  8530. verifyFormat("void F(int &i) { i->~int(); }");
  8531. }
  8532. TEST_F(FormatTest, FormatsWithWebKitStyle) {
  8533. FormatStyle Style = getWebKitStyle();
  8534. // Don't indent in outer namespaces.
  8535. verifyFormat("namespace outer {\n"
  8536. "int i;\n"
  8537. "namespace inner {\n"
  8538. " int i;\n"
  8539. "} // namespace inner\n"
  8540. "} // namespace outer\n"
  8541. "namespace other_outer {\n"
  8542. "int i;\n"
  8543. "}",
  8544. Style);
  8545. // Don't indent case labels.
  8546. verifyFormat("switch (variable) {\n"
  8547. "case 1:\n"
  8548. "case 2:\n"
  8549. " doSomething();\n"
  8550. " break;\n"
  8551. "default:\n"
  8552. " ++variable;\n"
  8553. "}",
  8554. Style);
  8555. // Wrap before binary operators.
  8556. EXPECT_EQ(
  8557. "void f()\n"
  8558. "{\n"
  8559. " if (aaaaaaaaaaaaaaaa\n"
  8560. " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
  8561. " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  8562. " return;\n"
  8563. "}",
  8564. format(
  8565. "void f() {\n"
  8566. "if (aaaaaaaaaaaaaaaa\n"
  8567. "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
  8568. "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  8569. "return;\n"
  8570. "}",
  8571. Style));
  8572. // Allow functions on a single line.
  8573. verifyFormat("void f() { return; }", Style);
  8574. // Constructor initializers are formatted one per line with the "," on the
  8575. // new line.
  8576. verifyFormat("Constructor()\n"
  8577. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  8578. " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
  8579. " aaaaaaaaaaaaaa)\n"
  8580. " , aaaaaaaaaaaaaaaaaaaaaaa()\n"
  8581. "{\n"
  8582. "}",
  8583. Style);
  8584. verifyFormat("SomeClass::Constructor()\n"
  8585. " : a(a)\n"
  8586. "{\n"
  8587. "}",
  8588. Style);
  8589. EXPECT_EQ("SomeClass::Constructor()\n"
  8590. " : a(a)\n"
  8591. "{\n"
  8592. "}",
  8593. format("SomeClass::Constructor():a(a){}", Style));
  8594. verifyFormat("SomeClass::Constructor()\n"
  8595. " : a(a)\n"
  8596. " , b(b)\n"
  8597. " , c(c)\n"
  8598. "{\n"
  8599. "}", Style);
  8600. verifyFormat("SomeClass::Constructor()\n"
  8601. " : a(a)\n"
  8602. "{\n"
  8603. " foo();\n"
  8604. " bar();\n"
  8605. "}",
  8606. Style);
  8607. // Access specifiers should be aligned left.
  8608. verifyFormat("class C {\n"
  8609. "public:\n"
  8610. " int i;\n"
  8611. "};",
  8612. Style);
  8613. // Do not align comments.
  8614. verifyFormat("int a; // Do not\n"
  8615. "double b; // align comments.",
  8616. Style);
  8617. // Do not align operands.
  8618. EXPECT_EQ("ASSERT(aaaa\n"
  8619. " || bbbb);",
  8620. format("ASSERT ( aaaa\n||bbbb);", Style));
  8621. // Accept input's line breaks.
  8622. EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
  8623. " || bbbbbbbbbbbbbbb) {\n"
  8624. " i++;\n"
  8625. "}",
  8626. format("if (aaaaaaaaaaaaaaa\n"
  8627. "|| bbbbbbbbbbbbbbb) { i++; }",
  8628. Style));
  8629. EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
  8630. " i++;\n"
  8631. "}",
  8632. format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
  8633. // Don't automatically break all macro definitions (llvm.org/PR17842).
  8634. verifyFormat("#define aNumber 10", Style);
  8635. // However, generally keep the line breaks that the user authored.
  8636. EXPECT_EQ("#define aNumber \\\n"
  8637. " 10",
  8638. format("#define aNumber \\\n"
  8639. " 10",
  8640. Style));
  8641. // Keep empty and one-element array literals on a single line.
  8642. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
  8643. " copyItems:YES];",
  8644. format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
  8645. "copyItems:YES];",
  8646. Style));
  8647. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
  8648. " copyItems:YES];",
  8649. format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
  8650. " copyItems:YES];",
  8651. Style));
  8652. // FIXME: This does not seem right, there should be more indentation before
  8653. // the array literal's entries. Nested blocks have the same problem.
  8654. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
  8655. " @\"a\",\n"
  8656. " @\"a\"\n"
  8657. "]\n"
  8658. " copyItems:YES];",
  8659. format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
  8660. " @\"a\",\n"
  8661. " @\"a\"\n"
  8662. " ]\n"
  8663. " copyItems:YES];",
  8664. Style));
  8665. EXPECT_EQ(
  8666. "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
  8667. " copyItems:YES];",
  8668. format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
  8669. " copyItems:YES];",
  8670. Style));
  8671. verifyFormat("[self.a b:c c:d];", Style);
  8672. EXPECT_EQ("[self.a b:c\n"
  8673. " c:d];",
  8674. format("[self.a b:c\n"
  8675. "c:d];",
  8676. Style));
  8677. }
  8678. TEST_F(FormatTest, FormatsLambdas) {
  8679. verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
  8680. verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
  8681. verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
  8682. verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
  8683. verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
  8684. verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
  8685. verifyFormat("void f() {\n"
  8686. " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
  8687. "}\n");
  8688. verifyFormat("void f() {\n"
  8689. " other(x.begin(), //\n"
  8690. " x.end(), //\n"
  8691. " [&](int, int) { return 1; });\n"
  8692. "}\n");
  8693. verifyFormat("SomeFunction([]() { // A cool function...\n"
  8694. " return 43;\n"
  8695. "});");
  8696. EXPECT_EQ("SomeFunction([]() {\n"
  8697. "#define A a\n"
  8698. " return 43;\n"
  8699. "});",
  8700. format("SomeFunction([](){\n"
  8701. "#define A a\n"
  8702. "return 43;\n"
  8703. "});"));
  8704. verifyFormat("void f() {\n"
  8705. " SomeFunction([](decltype(x), A *a) {});\n"
  8706. "}");
  8707. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  8708. " [](const aaaaaaaaaa &a) { return a; });");
  8709. verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
  8710. " SomeOtherFunctioooooooooooooooooooooooooon();\n"
  8711. "});");
  8712. verifyFormat("Constructor()\n"
  8713. " : Field([] { // comment\n"
  8714. " int i;\n"
  8715. " }) {}");
  8716. // Lambdas with return types.
  8717. verifyFormat("int c = []() -> int { return 2; }();\n");
  8718. verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
  8719. verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
  8720. verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
  8721. verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
  8722. verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
  8723. verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
  8724. verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
  8725. " int j) -> int {\n"
  8726. " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
  8727. "};");
  8728. // Multiple lambdas in the same parentheses change indentation rules.
  8729. verifyFormat("SomeFunction(\n"
  8730. " []() {\n"
  8731. " int i = 42;\n"
  8732. " return i;\n"
  8733. " },\n"
  8734. " []() {\n"
  8735. " int j = 43;\n"
  8736. " return j;\n"
  8737. " });");
  8738. // More complex introducers.
  8739. verifyFormat("return [i, args...] {};");
  8740. // Not lambdas.
  8741. verifyFormat("constexpr char hello[]{\"hello\"};");
  8742. verifyFormat("double &operator[](int i) { return 0; }\n"
  8743. "int i;");
  8744. verifyFormat("std::unique_ptr<int[]> foo() {}");
  8745. verifyFormat("int i = a[a][a]->f();");
  8746. verifyFormat("int i = (*b)[a]->f();");
  8747. // Other corner cases.
  8748. verifyFormat("void f() {\n"
  8749. " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
  8750. " );\n"
  8751. "}");
  8752. // Lambdas created through weird macros.
  8753. verifyFormat("void f() {\n"
  8754. " MACRO((const AA &a) { return 1; });\n"
  8755. "}");
  8756. verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
  8757. " doo_dah();\n"
  8758. " doo_dah();\n"
  8759. " })) {\n"
  8760. "}");
  8761. }
  8762. TEST_F(FormatTest, FormatsBlocks) {
  8763. FormatStyle ShortBlocks = getLLVMStyle();
  8764. ShortBlocks.AllowShortBlocksOnASingleLine = true;
  8765. verifyFormat("int (^Block)(int, int);", ShortBlocks);
  8766. verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
  8767. verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
  8768. verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
  8769. verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
  8770. verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
  8771. verifyFormat("foo(^{ bar(); });", ShortBlocks);
  8772. verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
  8773. verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
  8774. verifyFormat("[operation setCompletionBlock:^{\n"
  8775. " [self onOperationDone];\n"
  8776. "}];");
  8777. verifyFormat("int i = {[operation setCompletionBlock:^{\n"
  8778. " [self onOperationDone];\n"
  8779. "}]};");
  8780. verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
  8781. " f();\n"
  8782. "}];");
  8783. verifyFormat("int a = [operation block:^int(int *i) {\n"
  8784. " return 1;\n"
  8785. "}];");
  8786. verifyFormat("[myObject doSomethingWith:arg1\n"
  8787. " aaa:^int(int *a) {\n"
  8788. " return 1;\n"
  8789. " }\n"
  8790. " bbb:f(a * bbbbbbbb)];");
  8791. verifyFormat("[operation setCompletionBlock:^{\n"
  8792. " [self.delegate newDataAvailable];\n"
  8793. "}];",
  8794. getLLVMStyleWithColumns(60));
  8795. verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
  8796. " NSString *path = [self sessionFilePath];\n"
  8797. " if (path) {\n"
  8798. " // ...\n"
  8799. " }\n"
  8800. "});");
  8801. verifyFormat("[[SessionService sharedService]\n"
  8802. " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  8803. " if (window) {\n"
  8804. " [self windowDidLoad:window];\n"
  8805. " } else {\n"
  8806. " [self errorLoadingWindow];\n"
  8807. " }\n"
  8808. " }];");
  8809. verifyFormat("void (^largeBlock)(void) = ^{\n"
  8810. " // ...\n"
  8811. "};\n",
  8812. getLLVMStyleWithColumns(40));
  8813. verifyFormat("[[SessionService sharedService]\n"
  8814. " loadWindowWithCompletionBlock: //\n"
  8815. " ^(SessionWindow *window) {\n"
  8816. " if (window) {\n"
  8817. " [self windowDidLoad:window];\n"
  8818. " } else {\n"
  8819. " [self errorLoadingWindow];\n"
  8820. " }\n"
  8821. " }];",
  8822. getLLVMStyleWithColumns(60));
  8823. verifyFormat("[myObject doSomethingWith:arg1\n"
  8824. " firstBlock:^(Foo *a) {\n"
  8825. " // ...\n"
  8826. " int i;\n"
  8827. " }\n"
  8828. " secondBlock:^(Bar *b) {\n"
  8829. " // ...\n"
  8830. " int i;\n"
  8831. " }\n"
  8832. " thirdBlock:^Foo(Bar *b) {\n"
  8833. " // ...\n"
  8834. " int i;\n"
  8835. " }];");
  8836. verifyFormat("[myObject doSomethingWith:arg1\n"
  8837. " firstBlock:-1\n"
  8838. " secondBlock:^(Bar *b) {\n"
  8839. " // ...\n"
  8840. " int i;\n"
  8841. " }];");
  8842. verifyFormat("f(^{\n"
  8843. " @autoreleasepool {\n"
  8844. " if (a) {\n"
  8845. " g();\n"
  8846. " }\n"
  8847. " }\n"
  8848. "});");
  8849. verifyFormat("Block b = ^int *(A *a, B *b) {}");
  8850. FormatStyle FourIndent = getLLVMStyle();
  8851. FourIndent.ObjCBlockIndentWidth = 4;
  8852. verifyFormat("[operation setCompletionBlock:^{\n"
  8853. " [self onOperationDone];\n"
  8854. "}];",
  8855. FourIndent);
  8856. }
  8857. TEST_F(FormatTest, SupportsCRLF) {
  8858. EXPECT_EQ("int a;\r\n"
  8859. "int b;\r\n"
  8860. "int c;\r\n",
  8861. format("int a;\r\n"
  8862. " int b;\r\n"
  8863. " int c;\r\n",
  8864. getLLVMStyle()));
  8865. EXPECT_EQ("int a;\r\n"
  8866. "int b;\r\n"
  8867. "int c;\r\n",
  8868. format("int a;\r\n"
  8869. " int b;\n"
  8870. " int c;\r\n",
  8871. getLLVMStyle()));
  8872. EXPECT_EQ("int a;\n"
  8873. "int b;\n"
  8874. "int c;\n",
  8875. format("int a;\r\n"
  8876. " int b;\n"
  8877. " int c;\n",
  8878. getLLVMStyle()));
  8879. EXPECT_EQ("\"aaaaaaa \"\r\n"
  8880. "\"bbbbbbb\";\r\n",
  8881. format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
  8882. EXPECT_EQ("#define A \\\r\n"
  8883. " b; \\\r\n"
  8884. " c; \\\r\n"
  8885. " d;\r\n",
  8886. format("#define A \\\r\n"
  8887. " b; \\\r\n"
  8888. " c; d; \r\n",
  8889. getGoogleStyle()));
  8890. EXPECT_EQ("/*\r\n"
  8891. "multi line block comments\r\n"
  8892. "should not introduce\r\n"
  8893. "an extra carriage return\r\n"
  8894. "*/\r\n",
  8895. format("/*\r\n"
  8896. "multi line block comments\r\n"
  8897. "should not introduce\r\n"
  8898. "an extra carriage return\r\n"
  8899. "*/\r\n"));
  8900. }
  8901. TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
  8902. verifyFormat("MY_CLASS(C) {\n"
  8903. " int i;\n"
  8904. " int j;\n"
  8905. "};");
  8906. }
  8907. TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
  8908. FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
  8909. TwoIndent.ContinuationIndentWidth = 2;
  8910. EXPECT_EQ("int i =\n"
  8911. " longFunction(\n"
  8912. " arg);",
  8913. format("int i = longFunction(arg);", TwoIndent));
  8914. FormatStyle SixIndent = getLLVMStyleWithColumns(20);
  8915. SixIndent.ContinuationIndentWidth = 6;
  8916. EXPECT_EQ("int i =\n"
  8917. " longFunction(\n"
  8918. " arg);",
  8919. format("int i = longFunction(arg);", SixIndent));
  8920. }
  8921. TEST_F(FormatTest, SpacesInAngles) {
  8922. FormatStyle Spaces = getLLVMStyle();
  8923. Spaces.SpacesInAngles = true;
  8924. verifyFormat("static_cast< int >(arg);", Spaces);
  8925. verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
  8926. verifyFormat("f< int, float >();", Spaces);
  8927. verifyFormat("template <> g() {}", Spaces);
  8928. verifyFormat("template < std::vector< int > > f() {}", Spaces);
  8929. Spaces.Standard = FormatStyle::LS_Cpp03;
  8930. Spaces.SpacesInAngles = true;
  8931. verifyFormat("A< A< int > >();", Spaces);
  8932. Spaces.SpacesInAngles = false;
  8933. verifyFormat("A<A<int> >();", Spaces);
  8934. Spaces.Standard = FormatStyle::LS_Cpp11;
  8935. Spaces.SpacesInAngles = true;
  8936. verifyFormat("A< A< int > >();", Spaces);
  8937. Spaces.SpacesInAngles = false;
  8938. verifyFormat("A<A<int>>();", Spaces);
  8939. }
  8940. TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
  8941. std::string code = "#if A\n"
  8942. "#if B\n"
  8943. "a.\n"
  8944. "#endif\n"
  8945. " a = 1;\n"
  8946. "#else\n"
  8947. "#endif\n"
  8948. "#if C\n"
  8949. "#else\n"
  8950. "#endif\n";
  8951. EXPECT_EQ(code, format(code));
  8952. }
  8953. TEST_F(FormatTest, HandleConflictMarkers) {
  8954. // Git/SVN conflict markers.
  8955. EXPECT_EQ("int a;\n"
  8956. "void f() {\n"
  8957. " callme(some(parameter1,\n"
  8958. "<<<<<<< text by the vcs\n"
  8959. " parameter2),\n"
  8960. "||||||| text by the vcs\n"
  8961. " parameter2),\n"
  8962. " parameter3,\n"
  8963. "======= text by the vcs\n"
  8964. " parameter2, parameter3),\n"
  8965. ">>>>>>> text by the vcs\n"
  8966. " otherparameter);\n",
  8967. format("int a;\n"
  8968. "void f() {\n"
  8969. " callme(some(parameter1,\n"
  8970. "<<<<<<< text by the vcs\n"
  8971. " parameter2),\n"
  8972. "||||||| text by the vcs\n"
  8973. " parameter2),\n"
  8974. " parameter3,\n"
  8975. "======= text by the vcs\n"
  8976. " parameter2,\n"
  8977. " parameter3),\n"
  8978. ">>>>>>> text by the vcs\n"
  8979. " otherparameter);\n"));
  8980. // Perforce markers.
  8981. EXPECT_EQ("void f() {\n"
  8982. " function(\n"
  8983. ">>>> text by the vcs\n"
  8984. " parameter,\n"
  8985. "==== text by the vcs\n"
  8986. " parameter,\n"
  8987. "==== text by the vcs\n"
  8988. " parameter,\n"
  8989. "<<<< text by the vcs\n"
  8990. " parameter);\n",
  8991. format("void f() {\n"
  8992. " function(\n"
  8993. ">>>> text by the vcs\n"
  8994. " parameter,\n"
  8995. "==== text by the vcs\n"
  8996. " parameter,\n"
  8997. "==== text by the vcs\n"
  8998. " parameter,\n"
  8999. "<<<< text by the vcs\n"
  9000. " parameter);\n"));
  9001. EXPECT_EQ("<<<<<<<\n"
  9002. "|||||||\n"
  9003. "=======\n"
  9004. ">>>>>>>",
  9005. format("<<<<<<<\n"
  9006. "|||||||\n"
  9007. "=======\n"
  9008. ">>>>>>>"));
  9009. EXPECT_EQ("<<<<<<<\n"
  9010. "|||||||\n"
  9011. "int i;\n"
  9012. "=======\n"
  9013. ">>>>>>>",
  9014. format("<<<<<<<\n"
  9015. "|||||||\n"
  9016. "int i;\n"
  9017. "=======\n"
  9018. ">>>>>>>"));
  9019. // FIXME: Handle parsing of macros around conflict markers correctly:
  9020. EXPECT_EQ("#define Macro \\\n"
  9021. "<<<<<<<\n"
  9022. "Something \\\n"
  9023. "|||||||\n"
  9024. "Else \\\n"
  9025. "=======\n"
  9026. "Other \\\n"
  9027. ">>>>>>>\n"
  9028. " End int i;\n",
  9029. format("#define Macro \\\n"
  9030. "<<<<<<<\n"
  9031. " Something \\\n"
  9032. "|||||||\n"
  9033. " Else \\\n"
  9034. "=======\n"
  9035. " Other \\\n"
  9036. ">>>>>>>\n"
  9037. " End\n"
  9038. "int i;\n"));
  9039. }
  9040. TEST_F(FormatTest, DisableRegions) {
  9041. EXPECT_EQ("int i;\n"
  9042. "// clang-format off\n"
  9043. " int j;\n"
  9044. "// clang-format on\n"
  9045. "int k;",
  9046. format(" int i;\n"
  9047. " // clang-format off\n"
  9048. " int j;\n"
  9049. " // clang-format on\n"
  9050. " int k;"));
  9051. EXPECT_EQ("int i;\n"
  9052. "/* clang-format off */\n"
  9053. " int j;\n"
  9054. "/* clang-format on */\n"
  9055. "int k;",
  9056. format(" int i;\n"
  9057. " /* clang-format off */\n"
  9058. " int j;\n"
  9059. " /* clang-format on */\n"
  9060. " int k;"));
  9061. }
  9062. TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
  9063. format("? ) =");
  9064. }
  9065. } // end namespace tooling
  9066. } // end namespace clang