FormatTest.cpp 357 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654
  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. int ReplacementCount;
  59. };
  60. TEST_F(FormatTest, MessUp) {
  61. EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
  62. EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
  63. EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
  64. EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
  65. EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
  66. }
  67. //===----------------------------------------------------------------------===//
  68. // Basic function tests.
  69. //===----------------------------------------------------------------------===//
  70. TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
  71. EXPECT_EQ(";", format(";"));
  72. }
  73. TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
  74. EXPECT_EQ("int i;", format(" int i;"));
  75. EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;"));
  76. EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
  77. EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
  78. }
  79. TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
  80. EXPECT_EQ("int i;", format("int\ni;"));
  81. }
  82. TEST_F(FormatTest, FormatsNestedBlockStatements) {
  83. EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
  84. }
  85. TEST_F(FormatTest, FormatsNestedCall) {
  86. verifyFormat("Method(f1, f2(f3));");
  87. verifyFormat("Method(f1(f2, f3()));");
  88. verifyFormat("Method(f1(f2, (f3())));");
  89. }
  90. TEST_F(FormatTest, NestedNameSpecifiers) {
  91. verifyFormat("vector<::Type> v;");
  92. verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
  93. verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
  94. verifyFormat("bool a = 2 < ::SomeFunction();");
  95. }
  96. TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
  97. EXPECT_EQ("if (a) {\n"
  98. " f();\n"
  99. "}",
  100. format("if(a){f();}"));
  101. EXPECT_EQ(4, ReplacementCount);
  102. EXPECT_EQ("if (a) {\n"
  103. " f();\n"
  104. "}",
  105. format("if (a) {\n"
  106. " f();\n"
  107. "}"));
  108. EXPECT_EQ(0, ReplacementCount);
  109. }
  110. TEST_F(FormatTest, RemovesTrailingWhitespaceOfFormattedLine) {
  111. EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0, getLLVMStyle()));
  112. EXPECT_EQ("int a;", format("int a; "));
  113. EXPECT_EQ("int a;\n", format("int a; \n \n \n "));
  114. EXPECT_EQ("int a;\nint b; ",
  115. format("int a; \nint b; ", 0, 0, getLLVMStyle()));
  116. }
  117. TEST_F(FormatTest, FormatsCorrectRegionForLeadingWhitespace) {
  118. EXPECT_EQ("int b;\nint a;",
  119. format("int b;\n int a;", 7, 0, getLLVMStyle()));
  120. EXPECT_EQ("int b;\n int a;",
  121. format("int b;\n int a;", 6, 0, getLLVMStyle()));
  122. EXPECT_EQ("#define A \\\n"
  123. " int a; \\\n"
  124. " int b;",
  125. format("#define A \\\n"
  126. " int a; \\\n"
  127. " int b;",
  128. 26, 0, getLLVMStyleWithColumns(12)));
  129. EXPECT_EQ("#define A \\\n"
  130. " int a; \\\n"
  131. " int b;",
  132. format("#define A \\\n"
  133. " int a; \\\n"
  134. " int b;",
  135. 25, 0, getLLVMStyleWithColumns(12)));
  136. }
  137. TEST_F(FormatTest, FormatLineWhenInvokedOnTrailingNewline) {
  138. EXPECT_EQ("int b;\n\nint a;",
  139. format("int b;\n\nint a;", 8, 0, getLLVMStyle()));
  140. EXPECT_EQ("int b;\n\nint a;",
  141. format("int b;\n\nint a;", 7, 0, getLLVMStyle()));
  142. // This might not strictly be correct, but is likely good in all practical
  143. // cases.
  144. EXPECT_EQ("int b;\nint a;",
  145. format("int b;int a;", 7, 0, getLLVMStyle()));
  146. }
  147. TEST_F(FormatTest, RemovesWhitespaceWhenTriggeredOnEmptyLine) {
  148. EXPECT_EQ("int a;\n\n int b;",
  149. format("int a;\n \n\n int b;", 8, 0, getLLVMStyle()));
  150. EXPECT_EQ("int a;\n\n int b;",
  151. format("int a;\n \n\n int b;", 9, 0, getLLVMStyle()));
  152. }
  153. TEST_F(FormatTest, RemovesEmptyLines) {
  154. EXPECT_EQ("class C {\n"
  155. " int i;\n"
  156. "};",
  157. format("class C {\n"
  158. " int i;\n"
  159. "\n"
  160. "};"));
  161. // Don't remove empty lines at the start of namespaces or extern "C" blocks.
  162. EXPECT_EQ("namespace N {\n"
  163. "\n"
  164. "int i;\n"
  165. "}",
  166. format("namespace N {\n"
  167. "\n"
  168. "int i;\n"
  169. "}",
  170. getGoogleStyle()));
  171. EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
  172. "\n"
  173. "int i;\n"
  174. "}",
  175. format("extern /**/ \"C\" /**/ {\n"
  176. "\n"
  177. "int i;\n"
  178. "}",
  179. getGoogleStyle()));
  180. // ...but do keep inlining and removing empty lines for non-block extern "C"
  181. // functions.
  182. verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
  183. EXPECT_EQ("extern \"C\" int f() {\n"
  184. " int i = 42;\n"
  185. " return i;\n"
  186. "}",
  187. format("extern \"C\" int f() {\n"
  188. "\n"
  189. " int i = 42;\n"
  190. " return i;\n"
  191. "}",
  192. getGoogleStyle()));
  193. // Remove empty lines at the beginning and end of blocks.
  194. EXPECT_EQ("void f() {\n"
  195. "\n"
  196. " if (a) {\n"
  197. "\n"
  198. " f();\n"
  199. " }\n"
  200. "}",
  201. format("void f() {\n"
  202. "\n"
  203. " if (a) {\n"
  204. "\n"
  205. " f();\n"
  206. "\n"
  207. " }\n"
  208. "\n"
  209. "}",
  210. getLLVMStyle()));
  211. EXPECT_EQ("void f() {\n"
  212. " if (a) {\n"
  213. " f();\n"
  214. " }\n"
  215. "}",
  216. format("void f() {\n"
  217. "\n"
  218. " if (a) {\n"
  219. "\n"
  220. " f();\n"
  221. "\n"
  222. " }\n"
  223. "\n"
  224. "}",
  225. getGoogleStyle()));
  226. // Don't remove empty lines in more complex control statements.
  227. EXPECT_EQ("void f() {\n"
  228. " if (a) {\n"
  229. " f();\n"
  230. "\n"
  231. " } else if (b) {\n"
  232. " f();\n"
  233. " }\n"
  234. "}",
  235. format("void f() {\n"
  236. " if (a) {\n"
  237. " f();\n"
  238. "\n"
  239. " } else if (b) {\n"
  240. " f();\n"
  241. "\n"
  242. " }\n"
  243. "\n"
  244. "}"));
  245. // FIXME: This is slightly inconsistent.
  246. EXPECT_EQ("namespace {\n"
  247. "int i;\n"
  248. "}",
  249. format("namespace {\n"
  250. "int i;\n"
  251. "\n"
  252. "}"));
  253. EXPECT_EQ("namespace {\n"
  254. "int i;\n"
  255. "\n"
  256. "} // namespace",
  257. format("namespace {\n"
  258. "int i;\n"
  259. "\n"
  260. "} // namespace"));
  261. }
  262. TEST_F(FormatTest, ReformatsMovedLines) {
  263. EXPECT_EQ(
  264. "template <typename T> T *getFETokenInfo() const {\n"
  265. " return static_cast<T *>(FETokenInfo);\n"
  266. "}\n"
  267. " int a; // <- Should not be formatted",
  268. format(
  269. "template<typename T>\n"
  270. "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
  271. " int a; // <- Should not be formatted",
  272. 9, 5, getLLVMStyle()));
  273. }
  274. TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
  275. verifyFormat("x = (a) and (b);");
  276. verifyFormat("x = (a) or (b);");
  277. verifyFormat("x = (a) bitand (b);");
  278. verifyFormat("x = (a) bitor (b);");
  279. verifyFormat("x = (a) not_eq (b);");
  280. verifyFormat("x = (a) and_eq (b);");
  281. verifyFormat("x = (a) or_eq (b);");
  282. verifyFormat("x = (a) xor (b);");
  283. }
  284. //===----------------------------------------------------------------------===//
  285. // Tests for control statements.
  286. //===----------------------------------------------------------------------===//
  287. TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
  288. verifyFormat("if (true)\n f();\ng();");
  289. verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
  290. verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
  291. FormatStyle AllowsMergedIf = getLLVMStyle();
  292. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  293. verifyFormat("if (a)\n"
  294. " // comment\n"
  295. " f();",
  296. AllowsMergedIf);
  297. verifyFormat("if (a)\n"
  298. " ;",
  299. AllowsMergedIf);
  300. verifyFormat("if (a)\n"
  301. " if (b) return;",
  302. AllowsMergedIf);
  303. verifyFormat("if (a) // Can't merge this\n"
  304. " f();\n",
  305. AllowsMergedIf);
  306. verifyFormat("if (a) /* still don't merge */\n"
  307. " f();",
  308. AllowsMergedIf);
  309. verifyFormat("if (a) { // Never merge this\n"
  310. " f();\n"
  311. "}",
  312. AllowsMergedIf);
  313. verifyFormat("if (a) {/* Never merge this */\n"
  314. " f();\n"
  315. "}",
  316. AllowsMergedIf);
  317. EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1, AllowsMergedIf));
  318. EXPECT_EQ("if (a) return; // comment",
  319. format("if(a)\nreturn; // comment", 20, 1, AllowsMergedIf));
  320. AllowsMergedIf.ColumnLimit = 14;
  321. verifyFormat("if (a) return;", AllowsMergedIf);
  322. verifyFormat("if (aaaaaaaaa)\n"
  323. " return;",
  324. AllowsMergedIf);
  325. AllowsMergedIf.ColumnLimit = 13;
  326. verifyFormat("if (a)\n return;", AllowsMergedIf);
  327. }
  328. TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
  329. FormatStyle AllowsMergedLoops = getLLVMStyle();
  330. AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
  331. verifyFormat("while (true) continue;", AllowsMergedLoops);
  332. verifyFormat("for (;;) continue;", AllowsMergedLoops);
  333. verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
  334. verifyFormat("while (true)\n"
  335. " ;",
  336. AllowsMergedLoops);
  337. verifyFormat("for (;;)\n"
  338. " ;",
  339. AllowsMergedLoops);
  340. verifyFormat("for (;;)\n"
  341. " for (;;) continue;",
  342. AllowsMergedLoops);
  343. verifyFormat("for (;;) // Can't merge this\n"
  344. " continue;",
  345. AllowsMergedLoops);
  346. verifyFormat("for (;;) /* still don't merge */\n"
  347. " continue;",
  348. AllowsMergedLoops);
  349. }
  350. TEST_F(FormatTest, FormatShortBracedStatements) {
  351. FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
  352. AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
  353. AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
  354. AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
  355. verifyFormat("if (true) {}", AllowSimpleBracedStatements);
  356. verifyFormat("while (true) {}", AllowSimpleBracedStatements);
  357. verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
  358. verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
  359. verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
  360. verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
  361. verifyFormat("if (true) { //\n"
  362. " f();\n"
  363. "}",
  364. AllowSimpleBracedStatements);
  365. verifyFormat("if (true) {\n"
  366. " f();\n"
  367. " f();\n"
  368. "}",
  369. AllowSimpleBracedStatements);
  370. verifyFormat("template <int> struct A2 {\n"
  371. " struct B {};\n"
  372. "};",
  373. AllowSimpleBracedStatements);
  374. AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
  375. verifyFormat("if (true) {\n"
  376. " f();\n"
  377. "}",
  378. AllowSimpleBracedStatements);
  379. AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
  380. verifyFormat("while (true) {\n"
  381. " f();\n"
  382. "}",
  383. AllowSimpleBracedStatements);
  384. verifyFormat("for (;;) {\n"
  385. " f();\n"
  386. "}",
  387. AllowSimpleBracedStatements);
  388. }
  389. TEST_F(FormatTest, ParseIfElse) {
  390. verifyFormat("if (true)\n"
  391. " if (true)\n"
  392. " if (true)\n"
  393. " f();\n"
  394. " else\n"
  395. " g();\n"
  396. " else\n"
  397. " h();\n"
  398. "else\n"
  399. " i();");
  400. verifyFormat("if (true)\n"
  401. " if (true)\n"
  402. " if (true) {\n"
  403. " if (true)\n"
  404. " f();\n"
  405. " } else {\n"
  406. " g();\n"
  407. " }\n"
  408. " else\n"
  409. " h();\n"
  410. "else {\n"
  411. " i();\n"
  412. "}");
  413. verifyFormat("void f() {\n"
  414. " if (a) {\n"
  415. " } else {\n"
  416. " }\n"
  417. "}");
  418. }
  419. TEST_F(FormatTest, ElseIf) {
  420. verifyFormat("if (a) {\n} else if (b) {\n}");
  421. verifyFormat("if (a)\n"
  422. " f();\n"
  423. "else if (b)\n"
  424. " g();\n"
  425. "else\n"
  426. " h();");
  427. verifyFormat("if (a) {\n"
  428. " f();\n"
  429. "}\n"
  430. "// or else ..\n"
  431. "else {\n"
  432. " g()\n"
  433. "}");
  434. verifyFormat("if (a) {\n"
  435. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  436. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  437. "}");
  438. }
  439. TEST_F(FormatTest, FormatsForLoop) {
  440. verifyFormat(
  441. "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
  442. " ++VeryVeryLongLoopVariable)\n"
  443. " ;");
  444. verifyFormat("for (;;)\n"
  445. " f();");
  446. verifyFormat("for (;;) {\n}");
  447. verifyFormat("for (;;) {\n"
  448. " f();\n"
  449. "}");
  450. verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
  451. verifyFormat(
  452. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  453. " E = UnwrappedLines.end();\n"
  454. " I != E; ++I) {\n}");
  455. verifyFormat(
  456. "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
  457. " ++IIIII) {\n}");
  458. verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
  459. " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
  460. " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
  461. verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
  462. " I = FD->getDeclsInPrototypeScope().begin(),\n"
  463. " E = FD->getDeclsInPrototypeScope().end();\n"
  464. " I != E; ++I) {\n}");
  465. // FIXME: Not sure whether we want extra identation in line 3 here:
  466. verifyFormat(
  467. "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  468. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
  469. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  470. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  471. " ++aaaaaaaaaaa) {\n}");
  472. verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
  473. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  474. "}");
  475. verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
  476. " aaaaaaaaaa);\n"
  477. " iter; ++iter) {\n"
  478. "}");
  479. FormatStyle NoBinPacking = getLLVMStyle();
  480. NoBinPacking.BinPackParameters = false;
  481. verifyFormat("for (int aaaaaaaaaaa = 1;\n"
  482. " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
  483. " aaaaaaaaaaaaaaaa,\n"
  484. " aaaaaaaaaaaaaaaa,\n"
  485. " aaaaaaaaaaaaaaaa);\n"
  486. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  487. "}",
  488. NoBinPacking);
  489. verifyFormat(
  490. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  491. " E = UnwrappedLines.end();\n"
  492. " I != E;\n"
  493. " ++I) {\n}",
  494. NoBinPacking);
  495. }
  496. TEST_F(FormatTest, RangeBasedForLoops) {
  497. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  498. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  499. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
  500. " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
  501. verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
  502. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  503. verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
  504. " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
  505. }
  506. TEST_F(FormatTest, ForEachLoops) {
  507. verifyFormat("void f() {\n"
  508. " foreach (Item *item, itemlist) {}\n"
  509. " Q_FOREACH (Item *item, itemlist) {}\n"
  510. " BOOST_FOREACH (Item *item, itemlist) {}\n"
  511. " UNKNOWN_FORACH(Item * item, itemlist) {}\n"
  512. "}");
  513. }
  514. TEST_F(FormatTest, FormatsWhileLoop) {
  515. verifyFormat("while (true) {\n}");
  516. verifyFormat("while (true)\n"
  517. " f();");
  518. verifyFormat("while () {\n}");
  519. verifyFormat("while () {\n"
  520. " f();\n"
  521. "}");
  522. }
  523. TEST_F(FormatTest, FormatsDoWhile) {
  524. verifyFormat("do {\n"
  525. " do_something();\n"
  526. "} while (something());");
  527. verifyFormat("do\n"
  528. " do_something();\n"
  529. "while (something());");
  530. }
  531. TEST_F(FormatTest, FormatsSwitchStatement) {
  532. verifyFormat("switch (x) {\n"
  533. "case 1:\n"
  534. " f();\n"
  535. " break;\n"
  536. "case kFoo:\n"
  537. "case ns::kBar:\n"
  538. "case kBaz:\n"
  539. " break;\n"
  540. "default:\n"
  541. " g();\n"
  542. " break;\n"
  543. "}");
  544. verifyFormat("switch (x) {\n"
  545. "case 1: {\n"
  546. " f();\n"
  547. " break;\n"
  548. "}\n"
  549. "case 2: {\n"
  550. " break;\n"
  551. "}\n"
  552. "}");
  553. verifyFormat("switch (x) {\n"
  554. "case 1: {\n"
  555. " f();\n"
  556. " {\n"
  557. " g();\n"
  558. " h();\n"
  559. " }\n"
  560. " break;\n"
  561. "}\n"
  562. "}");
  563. verifyFormat("switch (x) {\n"
  564. "case 1: {\n"
  565. " f();\n"
  566. " if (foo) {\n"
  567. " g();\n"
  568. " h();\n"
  569. " }\n"
  570. " break;\n"
  571. "}\n"
  572. "}");
  573. verifyFormat("switch (x) {\n"
  574. "case 1: {\n"
  575. " f();\n"
  576. " g();\n"
  577. "} break;\n"
  578. "}");
  579. verifyFormat("switch (test)\n"
  580. " ;");
  581. verifyFormat("switch (x) {\n"
  582. "default: {\n"
  583. " // Do nothing.\n"
  584. "}\n"
  585. "}");
  586. verifyFormat("switch (x) {\n"
  587. "// comment\n"
  588. "// if 1, do f()\n"
  589. "case 1:\n"
  590. " f();\n"
  591. "}");
  592. verifyFormat("switch (x) {\n"
  593. "case 1:\n"
  594. " // Do amazing stuff\n"
  595. " {\n"
  596. " f();\n"
  597. " g();\n"
  598. " }\n"
  599. " break;\n"
  600. "}");
  601. verifyFormat("#define A \\\n"
  602. " switch (x) { \\\n"
  603. " case a: \\\n"
  604. " foo = b; \\\n"
  605. " }", getLLVMStyleWithColumns(20));
  606. verifyFormat("#define OPERATION_CASE(name) \\\n"
  607. " case OP_name: \\\n"
  608. " return operations::Operation##name\n",
  609. getLLVMStyleWithColumns(40));
  610. verifyGoogleFormat("switch (x) {\n"
  611. " case 1:\n"
  612. " f();\n"
  613. " break;\n"
  614. " case kFoo:\n"
  615. " case ns::kBar:\n"
  616. " case kBaz:\n"
  617. " break;\n"
  618. " default:\n"
  619. " g();\n"
  620. " break;\n"
  621. "}");
  622. verifyGoogleFormat("switch (x) {\n"
  623. " case 1: {\n"
  624. " f();\n"
  625. " break;\n"
  626. " }\n"
  627. "}");
  628. verifyGoogleFormat("switch (test)\n"
  629. " ;");
  630. verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
  631. " case OP_name: \\\n"
  632. " return operations::Operation##name\n");
  633. verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
  634. " // Get the correction operation class.\n"
  635. " switch (OpCode) {\n"
  636. " CASE(Add);\n"
  637. " CASE(Subtract);\n"
  638. " default:\n"
  639. " return operations::Unknown;\n"
  640. " }\n"
  641. "#undef OPERATION_CASE\n"
  642. "}");
  643. verifyFormat("DEBUG({\n"
  644. " switch (x) {\n"
  645. " case A:\n"
  646. " f();\n"
  647. " break;\n"
  648. " // On B:\n"
  649. " case B:\n"
  650. " g();\n"
  651. " break;\n"
  652. " }\n"
  653. "});");
  654. verifyFormat("switch (a) {\n"
  655. "case (b):\n"
  656. " return;\n"
  657. "}");
  658. verifyFormat("switch (a) {\n"
  659. "case some_namespace::\n"
  660. " some_constant:\n"
  661. " return;\n"
  662. "}",
  663. getLLVMStyleWithColumns(34));
  664. }
  665. TEST_F(FormatTest, CaseRanges) {
  666. verifyFormat("switch (x) {\n"
  667. "case 'A' ... 'Z':\n"
  668. "case 1 ... 5:\n"
  669. " break;\n"
  670. "}");
  671. }
  672. TEST_F(FormatTest, ShortCaseLabels) {
  673. FormatStyle Style = getLLVMStyle();
  674. Style.AllowShortCaseLabelsOnASingleLine = true;
  675. verifyFormat("switch (a) {\n"
  676. "case 1: x = 1; break;\n"
  677. "case 2: return;\n"
  678. "case 3:\n"
  679. "case 4:\n"
  680. "case 5: return;\n"
  681. "case 6: // comment\n"
  682. " return;\n"
  683. "case 7:\n"
  684. " // comment\n"
  685. " return;\n"
  686. "default: y = 1; break;\n"
  687. "}",
  688. Style);
  689. verifyFormat("switch (a) {\n"
  690. "case 1: {\n"
  691. "}\n"
  692. "case 2: {\n"
  693. " return;\n"
  694. "}\n"
  695. "case 3: {\n"
  696. " x = 1;\n"
  697. " return;\n"
  698. "}\n"
  699. "case 4:\n"
  700. " if (x)\n"
  701. " return;\n"
  702. "}",
  703. Style);
  704. Style.ColumnLimit = 21;
  705. verifyFormat("switch (a) {\n"
  706. "case 1: x = 1; break;\n"
  707. "case 2: return;\n"
  708. "case 3:\n"
  709. "case 4:\n"
  710. "case 5: return;\n"
  711. "default:\n"
  712. " y = 1;\n"
  713. " break;\n"
  714. "}",
  715. Style);
  716. }
  717. TEST_F(FormatTest, FormatsLabels) {
  718. verifyFormat("void f() {\n"
  719. " some_code();\n"
  720. "test_label:\n"
  721. " some_other_code();\n"
  722. " {\n"
  723. " some_more_code();\n"
  724. " another_label:\n"
  725. " some_more_code();\n"
  726. " }\n"
  727. "}");
  728. verifyFormat("some_code();\n"
  729. "test_label:\n"
  730. "some_other_code();");
  731. }
  732. //===----------------------------------------------------------------------===//
  733. // Tests for comments.
  734. //===----------------------------------------------------------------------===//
  735. TEST_F(FormatTest, UnderstandsSingleLineComments) {
  736. verifyFormat("//* */");
  737. verifyFormat("// line 1\n"
  738. "// line 2\n"
  739. "void f() {}\n");
  740. verifyFormat("void f() {\n"
  741. " // Doesn't do anything\n"
  742. "}");
  743. verifyFormat("SomeObject\n"
  744. " // Calling someFunction on SomeObject\n"
  745. " .someFunction();");
  746. verifyFormat("auto result = SomeObject\n"
  747. " // Calling someFunction on SomeObject\n"
  748. " .someFunction();");
  749. verifyFormat("void f(int i, // some comment (probably for i)\n"
  750. " int j, // some comment (probably for j)\n"
  751. " int k); // some comment (probably for k)");
  752. verifyFormat("void f(int i,\n"
  753. " // some comment (probably for j)\n"
  754. " int j,\n"
  755. " // some comment (probably for k)\n"
  756. " int k);");
  757. verifyFormat("int i // This is a fancy variable\n"
  758. " = 5; // with nicely aligned comment.");
  759. verifyFormat("// Leading comment.\n"
  760. "int a; // Trailing comment.");
  761. verifyFormat("int a; // Trailing comment\n"
  762. " // on 2\n"
  763. " // or 3 lines.\n"
  764. "int b;");
  765. verifyFormat("int a; // Trailing comment\n"
  766. "\n"
  767. "// Leading comment.\n"
  768. "int b;");
  769. verifyFormat("int a; // Comment.\n"
  770. " // More details.\n"
  771. "int bbbb; // Another comment.");
  772. verifyFormat(
  773. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  774. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n"
  775. "int cccccccccccccccccccccccccccccc; // comment\n"
  776. "int ddd; // looooooooooooooooooooooooong comment\n"
  777. "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  778. "int bbbbbbbbbbbbbbbbbbbbb; // comment\n"
  779. "int ccccccccccccccccccc; // comment");
  780. verifyFormat("#include \"a\" // comment\n"
  781. "#include \"a/b/c\" // comment");
  782. verifyFormat("#include <a> // comment\n"
  783. "#include <a/b/c> // comment");
  784. EXPECT_EQ("#include \"a\" // comment\n"
  785. "#include \"a/b/c\" // comment",
  786. format("#include \\\n"
  787. " \"a\" // comment\n"
  788. "#include \"a/b/c\" // comment"));
  789. verifyFormat("enum E {\n"
  790. " // comment\n"
  791. " VAL_A, // comment\n"
  792. " VAL_B\n"
  793. "};");
  794. verifyFormat(
  795. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  796. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
  797. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  798. " // Comment inside a statement.\n"
  799. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  800. verifyFormat("SomeFunction(a,\n"
  801. " // comment\n"
  802. " b + x);");
  803. verifyFormat("SomeFunction(a, a,\n"
  804. " // comment\n"
  805. " b + x);");
  806. verifyFormat(
  807. "bool aaaaaaaaaaaaa = // comment\n"
  808. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  809. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  810. verifyFormat("int aaaa; // aaaaa\n"
  811. "int aa; // aaaaaaa",
  812. getLLVMStyleWithColumns(20));
  813. EXPECT_EQ("void f() { // This does something ..\n"
  814. "}\n"
  815. "int a; // This is unrelated",
  816. format("void f() { // This does something ..\n"
  817. " }\n"
  818. "int a; // This is unrelated"));
  819. EXPECT_EQ("class C {\n"
  820. " void f() { // This does something ..\n"
  821. " } // awesome..\n"
  822. "\n"
  823. " int a; // This is unrelated\n"
  824. "};",
  825. format("class C{void f() { // This does something ..\n"
  826. " } // awesome..\n"
  827. " \n"
  828. "int a; // This is unrelated\n"
  829. "};"));
  830. EXPECT_EQ("int i; // single line trailing comment",
  831. format("int i;\\\n// single line trailing comment"));
  832. verifyGoogleFormat("int a; // Trailing comment.");
  833. verifyFormat("someFunction(anotherFunction( // Force break.\n"
  834. " parameter));");
  835. verifyGoogleFormat("#endif // HEADER_GUARD");
  836. verifyFormat("const char *test[] = {\n"
  837. " // A\n"
  838. " \"aaaa\",\n"
  839. " // B\n"
  840. " \"aaaaa\"};");
  841. verifyGoogleFormat(
  842. "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  843. " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment");
  844. EXPECT_EQ("D(a, {\n"
  845. " // test\n"
  846. " int a;\n"
  847. "});",
  848. format("D(a, {\n"
  849. "// test\n"
  850. "int a;\n"
  851. "});"));
  852. EXPECT_EQ("lineWith(); // comment\n"
  853. "// at start\n"
  854. "otherLine();",
  855. format("lineWith(); // comment\n"
  856. "// at start\n"
  857. "otherLine();"));
  858. EXPECT_EQ("lineWith(); // comment\n"
  859. " // at start\n"
  860. "otherLine();",
  861. format("lineWith(); // comment\n"
  862. " // at start\n"
  863. "otherLine();"));
  864. EXPECT_EQ("lineWith(); // comment\n"
  865. "// at start\n"
  866. "otherLine(); // comment",
  867. format("lineWith(); // comment\n"
  868. "// at start\n"
  869. "otherLine(); // comment"));
  870. EXPECT_EQ("lineWith();\n"
  871. "// at start\n"
  872. "otherLine(); // comment",
  873. format("lineWith();\n"
  874. " // at start\n"
  875. "otherLine(); // comment"));
  876. EXPECT_EQ("// first\n"
  877. "// at start\n"
  878. "otherLine(); // comment",
  879. format("// first\n"
  880. " // at start\n"
  881. "otherLine(); // comment"));
  882. EXPECT_EQ("f();\n"
  883. "// first\n"
  884. "// at start\n"
  885. "otherLine(); // comment",
  886. format("f();\n"
  887. "// first\n"
  888. " // at start\n"
  889. "otherLine(); // comment"));
  890. verifyFormat("f(); // comment\n"
  891. "// first\n"
  892. "// at start\n"
  893. "otherLine();");
  894. EXPECT_EQ("f(); // comment\n"
  895. "// first\n"
  896. "// at start\n"
  897. "otherLine();",
  898. format("f(); // comment\n"
  899. "// first\n"
  900. " // at start\n"
  901. "otherLine();"));
  902. EXPECT_EQ("f(); // comment\n"
  903. " // first\n"
  904. "// at start\n"
  905. "otherLine();",
  906. format("f(); // comment\n"
  907. " // first\n"
  908. "// at start\n"
  909. "otherLine();"));
  910. verifyFormat(
  911. "#define A \\\n"
  912. " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
  913. " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
  914. getLLVMStyleWithColumns(60));
  915. verifyFormat(
  916. "#define A \\\n"
  917. " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
  918. " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
  919. getLLVMStyleWithColumns(61));
  920. verifyFormat("if ( // This is some comment\n"
  921. " x + 3) {\n"
  922. "}");
  923. EXPECT_EQ("if ( // This is some comment\n"
  924. " // spanning two lines\n"
  925. " x + 3) {\n"
  926. "}",
  927. format("if( // This is some comment\n"
  928. " // spanning two lines\n"
  929. " x + 3) {\n"
  930. "}"));
  931. }
  932. TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
  933. EXPECT_EQ("SomeFunction(a,\n"
  934. " b, // comment\n"
  935. " c);",
  936. format("SomeFunction(a,\n"
  937. " b, // comment\n"
  938. " c);"));
  939. EXPECT_EQ("SomeFunction(a, b,\n"
  940. " // comment\n"
  941. " c);",
  942. format("SomeFunction(a,\n"
  943. " b,\n"
  944. " // comment\n"
  945. " c);"));
  946. EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
  947. " c);",
  948. format("SomeFunction(a, b, // comment (unclear relation)\n"
  949. " c);"));
  950. EXPECT_EQ("SomeFunction(a, // comment\n"
  951. " b,\n"
  952. " c); // comment",
  953. format("SomeFunction(a, // comment\n"
  954. " b,\n"
  955. " c); // comment"));
  956. }
  957. TEST_F(FormatTest, CanFormatCommentsLocally) {
  958. EXPECT_EQ("int a; // comment\n"
  959. "int b; // comment",
  960. format("int a; // comment\n"
  961. "int b; // comment",
  962. 0, 0, getLLVMStyle()));
  963. EXPECT_EQ("int a; // comment\n"
  964. " // line 2\n"
  965. "int b;",
  966. format("int a; // comment\n"
  967. " // line 2\n"
  968. "int b;",
  969. 28, 0, getLLVMStyle()));
  970. EXPECT_EQ("int aaaaaa; // comment\n"
  971. "int b;\n"
  972. "int c; // unrelated comment",
  973. format("int aaaaaa; // comment\n"
  974. "int b;\n"
  975. "int c; // unrelated comment",
  976. 31, 0, getLLVMStyle()));
  977. EXPECT_EQ("int a; // This\n"
  978. " // is\n"
  979. " // a",
  980. format("int a; // This\n"
  981. " // is\n"
  982. " // a",
  983. 0, 0, getLLVMStyle()));
  984. EXPECT_EQ("int a; // This\n"
  985. " // is\n"
  986. " // a\n"
  987. "// This is b\n"
  988. "int b;",
  989. format("int a; // This\n"
  990. " // is\n"
  991. " // a\n"
  992. "// This is b\n"
  993. "int b;",
  994. 0, 0, getLLVMStyle()));
  995. EXPECT_EQ("int a; // This\n"
  996. " // is\n"
  997. " // a\n"
  998. "\n"
  999. " // This is unrelated",
  1000. format("int a; // This\n"
  1001. " // is\n"
  1002. " // a\n"
  1003. "\n"
  1004. " // This is unrelated",
  1005. 0, 0, getLLVMStyle()));
  1006. EXPECT_EQ("int a;\n"
  1007. "// This is\n"
  1008. "// not formatted. ",
  1009. format("int a;\n"
  1010. "// This is\n"
  1011. "// not formatted. ",
  1012. 0, 0, getLLVMStyle()));
  1013. }
  1014. TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
  1015. EXPECT_EQ("// comment", format("// comment "));
  1016. EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
  1017. format("int aaaaaaa, bbbbbbb; // comment ",
  1018. getLLVMStyleWithColumns(33)));
  1019. EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f "));
  1020. EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f "));
  1021. }
  1022. TEST_F(FormatTest, UnderstandsBlockComments) {
  1023. verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
  1024. verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }");
  1025. EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
  1026. " bbbbbbbbbbbbbbbbbbbbbbbbb);",
  1027. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n"
  1028. "/* Trailing comment for aa... */\n"
  1029. " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  1030. EXPECT_EQ(
  1031. "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  1032. " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
  1033. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
  1034. "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  1035. EXPECT_EQ(
  1036. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1037. " aaaaaaaaaaaaaaaaaa,\n"
  1038. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  1039. "}",
  1040. format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1041. " aaaaaaaaaaaaaaaaaa ,\n"
  1042. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  1043. "}"));
  1044. FormatStyle NoBinPacking = getLLVMStyle();
  1045. NoBinPacking.BinPackParameters = false;
  1046. verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
  1047. " /* parameter 2 */ aaaaaa,\n"
  1048. " /* parameter 3 */ aaaaaa,\n"
  1049. " /* parameter 4 */ aaaaaa);",
  1050. NoBinPacking);
  1051. // Aligning block comments in macros.
  1052. verifyGoogleFormat("#define A \\\n"
  1053. " int i; /*a*/ \\\n"
  1054. " int jjj; /*b*/");
  1055. }
  1056. TEST_F(FormatTest, AlignsBlockComments) {
  1057. EXPECT_EQ("/*\n"
  1058. " * Really multi-line\n"
  1059. " * comment.\n"
  1060. " */\n"
  1061. "void f() {}",
  1062. format(" /*\n"
  1063. " * Really multi-line\n"
  1064. " * comment.\n"
  1065. " */\n"
  1066. " void f() {}"));
  1067. EXPECT_EQ("class C {\n"
  1068. " /*\n"
  1069. " * Another multi-line\n"
  1070. " * comment.\n"
  1071. " */\n"
  1072. " void f() {}\n"
  1073. "};",
  1074. format("class C {\n"
  1075. "/*\n"
  1076. " * Another multi-line\n"
  1077. " * comment.\n"
  1078. " */\n"
  1079. "void f() {}\n"
  1080. "};"));
  1081. EXPECT_EQ("/*\n"
  1082. " 1. This is a comment with non-trivial formatting.\n"
  1083. " 1.1. We have to indent/outdent all lines equally\n"
  1084. " 1.1.1. to keep the formatting.\n"
  1085. " */",
  1086. format(" /*\n"
  1087. " 1. This is a comment with non-trivial formatting.\n"
  1088. " 1.1. We have to indent/outdent all lines equally\n"
  1089. " 1.1.1. to keep the formatting.\n"
  1090. " */"));
  1091. EXPECT_EQ("/*\n"
  1092. "Don't try to outdent if there's not enough indentation.\n"
  1093. "*/",
  1094. format(" /*\n"
  1095. " Don't try to outdent if there's not enough indentation.\n"
  1096. " */"));
  1097. EXPECT_EQ("int i; /* Comment with empty...\n"
  1098. " *\n"
  1099. " * line. */",
  1100. format("int i; /* Comment with empty...\n"
  1101. " *\n"
  1102. " * line. */"));
  1103. EXPECT_EQ("int foobar = 0; /* comment */\n"
  1104. "int bar = 0; /* multiline\n"
  1105. " comment 1 */\n"
  1106. "int baz = 0; /* multiline\n"
  1107. " comment 2 */\n"
  1108. "int bzz = 0; /* multiline\n"
  1109. " comment 3 */",
  1110. format("int foobar = 0; /* comment */\n"
  1111. "int bar = 0; /* multiline\n"
  1112. " comment 1 */\n"
  1113. "int baz = 0; /* multiline\n"
  1114. " comment 2 */\n"
  1115. "int bzz = 0; /* multiline\n"
  1116. " comment 3 */"));
  1117. EXPECT_EQ("int foobar = 0; /* comment */\n"
  1118. "int bar = 0; /* multiline\n"
  1119. " comment */\n"
  1120. "int baz = 0; /* multiline\n"
  1121. "comment */",
  1122. format("int foobar = 0; /* comment */\n"
  1123. "int bar = 0; /* multiline\n"
  1124. "comment */\n"
  1125. "int baz = 0; /* multiline\n"
  1126. "comment */"));
  1127. }
  1128. TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
  1129. EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1130. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
  1131. format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1132. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
  1133. EXPECT_EQ(
  1134. "void ffffffffffff(\n"
  1135. " int aaaaaaaa, int bbbbbbbb,\n"
  1136. " int cccccccccccc) { /*\n"
  1137. " aaaaaaaaaa\n"
  1138. " aaaaaaaaaaaaa\n"
  1139. " bbbbbbbbbbbbbb\n"
  1140. " bbbbbbbbbb\n"
  1141. " */\n"
  1142. "}",
  1143. format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
  1144. "{ /*\n"
  1145. " aaaaaaaaaa aaaaaaaaaaaaa\n"
  1146. " bbbbbbbbbbbbbb bbbbbbbbbb\n"
  1147. " */\n"
  1148. "}",
  1149. getLLVMStyleWithColumns(40)));
  1150. }
  1151. TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
  1152. EXPECT_EQ("void ffffffffff(\n"
  1153. " int aaaaa /* test */);",
  1154. format("void ffffffffff(int aaaaa /* test */);",
  1155. getLLVMStyleWithColumns(35)));
  1156. }
  1157. TEST_F(FormatTest, SplitsLongCxxComments) {
  1158. EXPECT_EQ("// A comment that\n"
  1159. "// doesn't fit on\n"
  1160. "// one line",
  1161. format("// A comment that doesn't fit on one line",
  1162. getLLVMStyleWithColumns(20)));
  1163. EXPECT_EQ("// a b c d\n"
  1164. "// e f g\n"
  1165. "// h i j k",
  1166. format("// a b c d e f g h i j k",
  1167. getLLVMStyleWithColumns(10)));
  1168. EXPECT_EQ("// a b c d\n"
  1169. "// e f g\n"
  1170. "// h i j k",
  1171. format("\\\n// a b c d e f g h i j k",
  1172. getLLVMStyleWithColumns(10)));
  1173. EXPECT_EQ("if (true) // A comment that\n"
  1174. " // doesn't fit on\n"
  1175. " // one line",
  1176. format("if (true) // A comment that doesn't fit on one line ",
  1177. getLLVMStyleWithColumns(30)));
  1178. EXPECT_EQ("// Don't_touch_leading_whitespace",
  1179. format("// Don't_touch_leading_whitespace",
  1180. getLLVMStyleWithColumns(20)));
  1181. EXPECT_EQ("// Add leading\n"
  1182. "// whitespace",
  1183. format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
  1184. EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
  1185. EXPECT_EQ("// Even if it makes the line exceed the column\n"
  1186. "// limit",
  1187. format("//Even if it makes the line exceed the column limit",
  1188. getLLVMStyleWithColumns(51)));
  1189. EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
  1190. EXPECT_EQ("// aa bb cc dd",
  1191. format("// aa bb cc dd ",
  1192. getLLVMStyleWithColumns(15)));
  1193. EXPECT_EQ("// A comment before\n"
  1194. "// a macro\n"
  1195. "// definition\n"
  1196. "#define a b",
  1197. format("// A comment before a macro definition\n"
  1198. "#define a b",
  1199. getLLVMStyleWithColumns(20)));
  1200. EXPECT_EQ("void ffffff(\n"
  1201. " int aaaaaaaaa, // wwww\n"
  1202. " int bbbbbbbbbb, // xxxxxxx\n"
  1203. " // yyyyyyyyyy\n"
  1204. " int c, int d, int e) {}",
  1205. format("void ffffff(\n"
  1206. " int aaaaaaaaa, // wwww\n"
  1207. " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
  1208. " int c, int d, int e) {}",
  1209. getLLVMStyleWithColumns(40)));
  1210. EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1211. format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1212. getLLVMStyleWithColumns(20)));
  1213. EXPECT_EQ(
  1214. "#define XXX // a b c d\n"
  1215. " // e f g h",
  1216. format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
  1217. EXPECT_EQ(
  1218. "#define XXX // q w e r\n"
  1219. " // t y u i",
  1220. format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
  1221. }
  1222. TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {
  1223. EXPECT_EQ("// A comment\n"
  1224. "// that doesn't\n"
  1225. "// fit on one\n"
  1226. "// line",
  1227. format("// A comment that doesn't fit on one line",
  1228. getLLVMStyleWithColumns(20)));
  1229. EXPECT_EQ("/// A comment\n"
  1230. "/// that doesn't\n"
  1231. "/// fit on one\n"
  1232. "/// line",
  1233. format("/// A comment that doesn't fit on one line",
  1234. getLLVMStyleWithColumns(20)));
  1235. }
  1236. TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
  1237. EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1238. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1239. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1240. format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1241. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1242. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
  1243. EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1244. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1245. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1246. format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1247. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1248. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1249. getLLVMStyleWithColumns(50)));
  1250. // FIXME: One day we might want to implement adjustment of leading whitespace
  1251. // of the consecutive lines in this kind of comment:
  1252. EXPECT_EQ("double\n"
  1253. " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1254. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1255. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1256. format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1257. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1258. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1259. getLLVMStyleWithColumns(49)));
  1260. }
  1261. TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) {
  1262. FormatStyle Pragmas = getLLVMStyleWithColumns(30);
  1263. Pragmas.CommentPragmas = "^ IWYU pragma:";
  1264. EXPECT_EQ(
  1265. "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
  1266. format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
  1267. EXPECT_EQ(
  1268. "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
  1269. format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
  1270. }
  1271. TEST_F(FormatTest, PriorityOfCommentBreaking) {
  1272. EXPECT_EQ("if (xxx ==\n"
  1273. " yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  1274. " zzz)\n"
  1275. " q();",
  1276. format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  1277. " zzz) q();",
  1278. getLLVMStyleWithColumns(40)));
  1279. EXPECT_EQ("if (xxxxxxxxxx ==\n"
  1280. " yyy && // aaaaaa bbbbbbbb cccc\n"
  1281. " zzz)\n"
  1282. " q();",
  1283. format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
  1284. " zzz) q();",
  1285. getLLVMStyleWithColumns(40)));
  1286. EXPECT_EQ("if (xxxxxxxxxx &&\n"
  1287. " yyy || // aaaaaa bbbbbbbb cccc\n"
  1288. " zzz)\n"
  1289. " q();",
  1290. format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
  1291. " zzz) q();",
  1292. getLLVMStyleWithColumns(40)));
  1293. EXPECT_EQ("fffffffff(\n"
  1294. " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  1295. " zzz);",
  1296. format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  1297. " zzz);",
  1298. getLLVMStyleWithColumns(40)));
  1299. }
  1300. TEST_F(FormatTest, MultiLineCommentsInDefines) {
  1301. EXPECT_EQ("#define A(x) /* \\\n"
  1302. " a comment \\\n"
  1303. " inside */ \\\n"
  1304. " f();",
  1305. format("#define A(x) /* \\\n"
  1306. " a comment \\\n"
  1307. " inside */ \\\n"
  1308. " f();",
  1309. getLLVMStyleWithColumns(17)));
  1310. EXPECT_EQ("#define A( \\\n"
  1311. " x) /* \\\n"
  1312. " a comment \\\n"
  1313. " inside */ \\\n"
  1314. " f();",
  1315. format("#define A( \\\n"
  1316. " x) /* \\\n"
  1317. " a comment \\\n"
  1318. " inside */ \\\n"
  1319. " f();",
  1320. getLLVMStyleWithColumns(17)));
  1321. }
  1322. TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
  1323. EXPECT_EQ("namespace {}\n// Test\n#define A",
  1324. format("namespace {}\n // Test\n#define A"));
  1325. EXPECT_EQ("namespace {}\n/* Test */\n#define A",
  1326. format("namespace {}\n /* Test */\n#define A"));
  1327. EXPECT_EQ("namespace {}\n/* Test */ #define A",
  1328. format("namespace {}\n /* Test */ #define A"));
  1329. }
  1330. TEST_F(FormatTest, SplitsLongLinesInComments) {
  1331. EXPECT_EQ("/* This is a long\n"
  1332. " * comment that\n"
  1333. " * doesn't\n"
  1334. " * fit on one line.\n"
  1335. " */",
  1336. format("/* "
  1337. "This is a long "
  1338. "comment that "
  1339. "doesn't "
  1340. "fit on one line. */",
  1341. getLLVMStyleWithColumns(20)));
  1342. EXPECT_EQ("/* a b c d\n"
  1343. " * e f g\n"
  1344. " * h i j k\n"
  1345. " */",
  1346. format("/* a b c d e f g h i j k */",
  1347. getLLVMStyleWithColumns(10)));
  1348. EXPECT_EQ("/* a b c d\n"
  1349. " * e f g\n"
  1350. " * h i j k\n"
  1351. " */",
  1352. format("\\\n/* a b c d e f g h i j k */",
  1353. getLLVMStyleWithColumns(10)));
  1354. EXPECT_EQ("/*\n"
  1355. "This is a long\n"
  1356. "comment that doesn't\n"
  1357. "fit on one line.\n"
  1358. "*/",
  1359. format("/*\n"
  1360. "This is a long "
  1361. "comment that doesn't "
  1362. "fit on one line. \n"
  1363. "*/", getLLVMStyleWithColumns(20)));
  1364. EXPECT_EQ("/*\n"
  1365. " * This is a long\n"
  1366. " * comment that\n"
  1367. " * doesn't fit on\n"
  1368. " * one line.\n"
  1369. " */",
  1370. format("/* \n"
  1371. " * This is a long "
  1372. " comment that "
  1373. " doesn't fit on "
  1374. " one line. \n"
  1375. " */", getLLVMStyleWithColumns(20)));
  1376. EXPECT_EQ("/*\n"
  1377. " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
  1378. " * so_it_should_be_broken\n"
  1379. " * wherever_a_space_occurs\n"
  1380. " */",
  1381. format("/*\n"
  1382. " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
  1383. " so_it_should_be_broken "
  1384. " wherever_a_space_occurs \n"
  1385. " */",
  1386. getLLVMStyleWithColumns(20)));
  1387. EXPECT_EQ("/*\n"
  1388. " * This_comment_can_not_be_broken_into_lines\n"
  1389. " */",
  1390. format("/*\n"
  1391. " * This_comment_can_not_be_broken_into_lines\n"
  1392. " */",
  1393. getLLVMStyleWithColumns(20)));
  1394. EXPECT_EQ("{\n"
  1395. " /*\n"
  1396. " This is another\n"
  1397. " long comment that\n"
  1398. " doesn't fit on one\n"
  1399. " line 1234567890\n"
  1400. " */\n"
  1401. "}",
  1402. format("{\n"
  1403. "/*\n"
  1404. "This is another "
  1405. " long comment that "
  1406. " doesn't fit on one"
  1407. " line 1234567890\n"
  1408. "*/\n"
  1409. "}", getLLVMStyleWithColumns(20)));
  1410. EXPECT_EQ("{\n"
  1411. " /*\n"
  1412. " * This i s\n"
  1413. " * another comment\n"
  1414. " * t hat doesn' t\n"
  1415. " * fit on one l i\n"
  1416. " * n e\n"
  1417. " */\n"
  1418. "}",
  1419. format("{\n"
  1420. "/*\n"
  1421. " * This i s"
  1422. " another comment"
  1423. " t hat doesn' t"
  1424. " fit on one l i"
  1425. " n e\n"
  1426. " */\n"
  1427. "}", getLLVMStyleWithColumns(20)));
  1428. EXPECT_EQ("/*\n"
  1429. " * This is a long\n"
  1430. " * comment that\n"
  1431. " * doesn't fit on\n"
  1432. " * one line\n"
  1433. " */",
  1434. format(" /*\n"
  1435. " * This is a long comment that doesn't fit on one line\n"
  1436. " */", getLLVMStyleWithColumns(20)));
  1437. EXPECT_EQ("{\n"
  1438. " if (something) /* This is a\n"
  1439. " long\n"
  1440. " comment */\n"
  1441. " ;\n"
  1442. "}",
  1443. format("{\n"
  1444. " if (something) /* This is a long comment */\n"
  1445. " ;\n"
  1446. "}",
  1447. getLLVMStyleWithColumns(30)));
  1448. EXPECT_EQ("/* A comment before\n"
  1449. " * a macro\n"
  1450. " * definition */\n"
  1451. "#define a b",
  1452. format("/* A comment before a macro definition */\n"
  1453. "#define a b",
  1454. getLLVMStyleWithColumns(20)));
  1455. EXPECT_EQ("/* some comment\n"
  1456. " * a comment\n"
  1457. "* that we break\n"
  1458. " * another comment\n"
  1459. "* we have to break\n"
  1460. "* a left comment\n"
  1461. " */",
  1462. format(" /* some comment\n"
  1463. " * a comment that we break\n"
  1464. " * another comment we have to break\n"
  1465. "* a left comment\n"
  1466. " */",
  1467. getLLVMStyleWithColumns(20)));
  1468. EXPECT_EQ("/*\n"
  1469. "\n"
  1470. "\n"
  1471. " */\n",
  1472. format(" /* \n"
  1473. " \n"
  1474. " \n"
  1475. " */\n"));
  1476. EXPECT_EQ("/* a a */",
  1477. format("/* a a */", getLLVMStyleWithColumns(15)));
  1478. EXPECT_EQ("/* a a bc */",
  1479. format("/* a a bc */", getLLVMStyleWithColumns(15)));
  1480. EXPECT_EQ("/* aaa aaa\n"
  1481. " * aaaaa */",
  1482. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1483. EXPECT_EQ("/* aaa aaa\n"
  1484. " * aaaaa */",
  1485. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1486. }
  1487. TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
  1488. EXPECT_EQ("#define X \\\n"
  1489. " /* \\\n"
  1490. " Test \\\n"
  1491. " Macro comment \\\n"
  1492. " with a long \\\n"
  1493. " line \\\n"
  1494. " */ \\\n"
  1495. " A + B",
  1496. format("#define X \\\n"
  1497. " /*\n"
  1498. " Test\n"
  1499. " Macro comment with a long line\n"
  1500. " */ \\\n"
  1501. " A + B",
  1502. getLLVMStyleWithColumns(20)));
  1503. EXPECT_EQ("#define X \\\n"
  1504. " /* Macro comment \\\n"
  1505. " with a long \\\n"
  1506. " line */ \\\n"
  1507. " A + B",
  1508. format("#define X \\\n"
  1509. " /* Macro comment with a long\n"
  1510. " line */ \\\n"
  1511. " A + B",
  1512. getLLVMStyleWithColumns(20)));
  1513. EXPECT_EQ("#define X \\\n"
  1514. " /* Macro comment \\\n"
  1515. " * with a long \\\n"
  1516. " * line */ \\\n"
  1517. " A + B",
  1518. format("#define X \\\n"
  1519. " /* Macro comment with a long line */ \\\n"
  1520. " A + B",
  1521. getLLVMStyleWithColumns(20)));
  1522. }
  1523. TEST_F(FormatTest, CommentsInStaticInitializers) {
  1524. EXPECT_EQ(
  1525. "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
  1526. " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
  1527. " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
  1528. " aaaaaaaaaaaaaaaaaaaa, // comment\n"
  1529. " aaaaaaaaaaaaaaaaaaaa};",
  1530. format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
  1531. " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
  1532. " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
  1533. " aaaaaaaaaaaaaaaaaaaa , // comment\n"
  1534. " aaaaaaaaaaaaaaaaaaaa };"));
  1535. verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1536. " bbbbbbbbbbb, ccccccccccc};");
  1537. verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1538. " // comment for bb....\n"
  1539. " bbbbbbbbbbb, ccccccccccc};");
  1540. verifyGoogleFormat(
  1541. "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1542. " bbbbbbbbbbb, ccccccccccc};");
  1543. verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1544. " // comment for bb....\n"
  1545. " bbbbbbbbbbb, ccccccccccc};");
  1546. verifyFormat("S s = {{a, b, c}, // Group #1\n"
  1547. " {d, e, f}, // Group #2\n"
  1548. " {g, h, i}}; // Group #3");
  1549. verifyFormat("S s = {{// Group #1\n"
  1550. " a, b, c},\n"
  1551. " {// Group #2\n"
  1552. " d, e, f},\n"
  1553. " {// Group #3\n"
  1554. " g, h, i}};");
  1555. EXPECT_EQ("S s = {\n"
  1556. " // Some comment\n"
  1557. " a,\n"
  1558. "\n"
  1559. " // Comment after empty line\n"
  1560. " b}",
  1561. format("S s = {\n"
  1562. " // Some comment\n"
  1563. " a,\n"
  1564. " \n"
  1565. " // Comment after empty line\n"
  1566. " b\n"
  1567. "}"));
  1568. EXPECT_EQ("S s = {\n"
  1569. " /* Some comment */\n"
  1570. " a,\n"
  1571. "\n"
  1572. " /* Comment after empty line */\n"
  1573. " b}",
  1574. format("S s = {\n"
  1575. " /* Some comment */\n"
  1576. " a,\n"
  1577. " \n"
  1578. " /* Comment after empty line */\n"
  1579. " b\n"
  1580. "}"));
  1581. verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
  1582. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1583. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1584. " 0x00, 0x00, 0x00, 0x00}; // comment\n");
  1585. }
  1586. TEST_F(FormatTest, IgnoresIf0Contents) {
  1587. EXPECT_EQ("#if 0\n"
  1588. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1589. "#endif\n"
  1590. "void f() {}",
  1591. format("#if 0\n"
  1592. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1593. "#endif\n"
  1594. "void f( ) { }"));
  1595. EXPECT_EQ("#if false\n"
  1596. "void f( ) { }\n"
  1597. "#endif\n"
  1598. "void g() {}\n",
  1599. format("#if false\n"
  1600. "void f( ) { }\n"
  1601. "#endif\n"
  1602. "void g( ) { }\n"));
  1603. EXPECT_EQ("enum E {\n"
  1604. " One,\n"
  1605. " Two,\n"
  1606. "#if 0\n"
  1607. "Three,\n"
  1608. " Four,\n"
  1609. "#endif\n"
  1610. " Five\n"
  1611. "};",
  1612. format("enum E {\n"
  1613. " One,Two,\n"
  1614. "#if 0\n"
  1615. "Three,\n"
  1616. " Four,\n"
  1617. "#endif\n"
  1618. " Five};"));
  1619. EXPECT_EQ("enum F {\n"
  1620. " One,\n"
  1621. "#if 1\n"
  1622. " Two,\n"
  1623. "#if 0\n"
  1624. "Three,\n"
  1625. " Four,\n"
  1626. "#endif\n"
  1627. " Five\n"
  1628. "#endif\n"
  1629. "};",
  1630. format("enum F {\n"
  1631. "One,\n"
  1632. "#if 1\n"
  1633. "Two,\n"
  1634. "#if 0\n"
  1635. "Three,\n"
  1636. " Four,\n"
  1637. "#endif\n"
  1638. "Five\n"
  1639. "#endif\n"
  1640. "};"));
  1641. EXPECT_EQ("enum G {\n"
  1642. " One,\n"
  1643. "#if 0\n"
  1644. "Two,\n"
  1645. "#else\n"
  1646. " Three,\n"
  1647. "#endif\n"
  1648. " Four\n"
  1649. "};",
  1650. format("enum G {\n"
  1651. "One,\n"
  1652. "#if 0\n"
  1653. "Two,\n"
  1654. "#else\n"
  1655. "Three,\n"
  1656. "#endif\n"
  1657. "Four\n"
  1658. "};"));
  1659. EXPECT_EQ("enum H {\n"
  1660. " One,\n"
  1661. "#if 0\n"
  1662. "#ifdef Q\n"
  1663. "Two,\n"
  1664. "#else\n"
  1665. "Three,\n"
  1666. "#endif\n"
  1667. "#endif\n"
  1668. " Four\n"
  1669. "};",
  1670. format("enum H {\n"
  1671. "One,\n"
  1672. "#if 0\n"
  1673. "#ifdef Q\n"
  1674. "Two,\n"
  1675. "#else\n"
  1676. "Three,\n"
  1677. "#endif\n"
  1678. "#endif\n"
  1679. "Four\n"
  1680. "};"));
  1681. EXPECT_EQ("enum I {\n"
  1682. " One,\n"
  1683. "#if /* test */ 0 || 1\n"
  1684. "Two,\n"
  1685. "Three,\n"
  1686. "#endif\n"
  1687. " Four\n"
  1688. "};",
  1689. format("enum I {\n"
  1690. "One,\n"
  1691. "#if /* test */ 0 || 1\n"
  1692. "Two,\n"
  1693. "Three,\n"
  1694. "#endif\n"
  1695. "Four\n"
  1696. "};"));
  1697. EXPECT_EQ("enum J {\n"
  1698. " One,\n"
  1699. "#if 0\n"
  1700. "#if 0\n"
  1701. "Two,\n"
  1702. "#else\n"
  1703. "Three,\n"
  1704. "#endif\n"
  1705. "Four,\n"
  1706. "#endif\n"
  1707. " Five\n"
  1708. "};",
  1709. format("enum J {\n"
  1710. "One,\n"
  1711. "#if 0\n"
  1712. "#if 0\n"
  1713. "Two,\n"
  1714. "#else\n"
  1715. "Three,\n"
  1716. "#endif\n"
  1717. "Four,\n"
  1718. "#endif\n"
  1719. "Five\n"
  1720. "};"));
  1721. }
  1722. //===----------------------------------------------------------------------===//
  1723. // Tests for classes, namespaces, etc.
  1724. //===----------------------------------------------------------------------===//
  1725. TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
  1726. verifyFormat("class A {};");
  1727. }
  1728. TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
  1729. verifyFormat("class A {\n"
  1730. "public:\n"
  1731. "public: // comment\n"
  1732. "protected:\n"
  1733. "private:\n"
  1734. " void f() {}\n"
  1735. "};");
  1736. verifyGoogleFormat("class A {\n"
  1737. " public:\n"
  1738. " protected:\n"
  1739. " private:\n"
  1740. " void f() {}\n"
  1741. "};");
  1742. verifyFormat("class A {\n"
  1743. "public slots:\n"
  1744. " void f() {}\n"
  1745. "public Q_SLOTS:\n"
  1746. " void f() {}\n"
  1747. "};");
  1748. }
  1749. TEST_F(FormatTest, SeparatesLogicalBlocks) {
  1750. EXPECT_EQ("class A {\n"
  1751. "public:\n"
  1752. " void f();\n"
  1753. "\n"
  1754. "private:\n"
  1755. " void g() {}\n"
  1756. " // test\n"
  1757. "protected:\n"
  1758. " int h;\n"
  1759. "};",
  1760. format("class A {\n"
  1761. "public:\n"
  1762. "void f();\n"
  1763. "private:\n"
  1764. "void g() {}\n"
  1765. "// test\n"
  1766. "protected:\n"
  1767. "int h;\n"
  1768. "};"));
  1769. EXPECT_EQ("class A {\n"
  1770. "protected:\n"
  1771. "public:\n"
  1772. " void f();\n"
  1773. "};",
  1774. format("class A {\n"
  1775. "protected:\n"
  1776. "\n"
  1777. "public:\n"
  1778. "\n"
  1779. " void f();\n"
  1780. "};"));
  1781. }
  1782. TEST_F(FormatTest, FormatsClasses) {
  1783. verifyFormat("class A : public B {};");
  1784. verifyFormat("class A : public ::B {};");
  1785. verifyFormat(
  1786. "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1787. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1788. verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
  1789. " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1790. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1791. verifyFormat(
  1792. "class A : public B, public C, public D, public E, public F {};");
  1793. verifyFormat("class AAAAAAAAAAAA : public B,\n"
  1794. " public C,\n"
  1795. " public D,\n"
  1796. " public E,\n"
  1797. " public F,\n"
  1798. " public G {};");
  1799. verifyFormat("class\n"
  1800. " ReallyReallyLongClassName {\n"
  1801. " int i;\n"
  1802. "};",
  1803. getLLVMStyleWithColumns(32));
  1804. verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
  1805. " aaaaaaaaaaaaaaaa> {};");
  1806. verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
  1807. " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
  1808. " aaaaaaaaaaaaaaaaaaaaaa> {};");
  1809. verifyFormat("template <class R, class C>\n"
  1810. "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
  1811. " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
  1812. verifyFormat("class ::A::B {};");
  1813. }
  1814. TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
  1815. verifyFormat("class A {\n} a, b;");
  1816. verifyFormat("struct A {\n} a, b;");
  1817. verifyFormat("union A {\n} a;");
  1818. }
  1819. TEST_F(FormatTest, FormatsEnum) {
  1820. verifyFormat("enum {\n"
  1821. " Zero,\n"
  1822. " One = 1,\n"
  1823. " Two = One + 1,\n"
  1824. " Three = (One + Two),\n"
  1825. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1826. " Five = (One, Two, Three, Four, 5)\n"
  1827. "};");
  1828. verifyGoogleFormat("enum {\n"
  1829. " Zero,\n"
  1830. " One = 1,\n"
  1831. " Two = One + 1,\n"
  1832. " Three = (One + Two),\n"
  1833. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1834. " Five = (One, Two, Three, Four, 5)\n"
  1835. "};");
  1836. verifyFormat("enum Enum {};");
  1837. verifyFormat("enum {};");
  1838. verifyFormat("enum X E {} d;");
  1839. verifyFormat("enum __attribute__((...)) E {} d;");
  1840. verifyFormat("enum __declspec__((...)) E {} d;");
  1841. verifyFormat("enum X f() {\n a();\n return 42;\n}");
  1842. verifyFormat("enum {\n"
  1843. " Bar = Foo<int, int>::value\n"
  1844. "};",
  1845. getLLVMStyleWithColumns(30));
  1846. verifyFormat("enum ShortEnum { A, B, C };");
  1847. verifyGoogleFormat("enum ShortEnum { A, B, C };");
  1848. EXPECT_EQ("enum KeepEmptyLines {\n"
  1849. " ONE,\n"
  1850. "\n"
  1851. " TWO,\n"
  1852. "\n"
  1853. " THREE\n"
  1854. "}",
  1855. format("enum KeepEmptyLines {\n"
  1856. " ONE,\n"
  1857. "\n"
  1858. " TWO,\n"
  1859. "\n"
  1860. "\n"
  1861. " THREE\n"
  1862. "}"));
  1863. verifyFormat("enum E { // comment\n"
  1864. " ONE,\n"
  1865. " TWO\n"
  1866. "};\n"
  1867. "int i;");
  1868. }
  1869. TEST_F(FormatTest, FormatsEnumsWithErrors) {
  1870. verifyFormat("enum Type {\n"
  1871. " One = 0; // These semicolons should be commas.\n"
  1872. " Two = 1;\n"
  1873. "};");
  1874. verifyFormat("namespace n {\n"
  1875. "enum Type {\n"
  1876. " One,\n"
  1877. " Two, // missing };\n"
  1878. " int i;\n"
  1879. "}\n"
  1880. "void g() {}");
  1881. }
  1882. TEST_F(FormatTest, FormatsEnumStruct) {
  1883. verifyFormat("enum struct {\n"
  1884. " Zero,\n"
  1885. " One = 1,\n"
  1886. " Two = One + 1,\n"
  1887. " Three = (One + Two),\n"
  1888. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1889. " Five = (One, Two, Three, Four, 5)\n"
  1890. "};");
  1891. verifyFormat("enum struct Enum {};");
  1892. verifyFormat("enum struct {};");
  1893. verifyFormat("enum struct X E {} d;");
  1894. verifyFormat("enum struct __attribute__((...)) E {} d;");
  1895. verifyFormat("enum struct __declspec__((...)) E {} d;");
  1896. verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
  1897. }
  1898. TEST_F(FormatTest, FormatsEnumClass) {
  1899. verifyFormat("enum class {\n"
  1900. " Zero,\n"
  1901. " One = 1,\n"
  1902. " Two = One + 1,\n"
  1903. " Three = (One + Two),\n"
  1904. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1905. " Five = (One, Two, Three, Four, 5)\n"
  1906. "};");
  1907. verifyFormat("enum class Enum {};");
  1908. verifyFormat("enum class {};");
  1909. verifyFormat("enum class X E {} d;");
  1910. verifyFormat("enum class __attribute__((...)) E {} d;");
  1911. verifyFormat("enum class __declspec__((...)) E {} d;");
  1912. verifyFormat("enum class X f() {\n a();\n return 42;\n}");
  1913. }
  1914. TEST_F(FormatTest, FormatsEnumTypes) {
  1915. verifyFormat("enum X : int {\n"
  1916. " A, // Force multiple lines.\n"
  1917. " B\n"
  1918. "};");
  1919. verifyFormat("enum X : int { A, B };");
  1920. verifyFormat("enum X : std::uint32_t { A, B };");
  1921. }
  1922. TEST_F(FormatTest, FormatsNSEnums) {
  1923. verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
  1924. verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
  1925. " // Information about someDecentlyLongValue.\n"
  1926. " someDecentlyLongValue,\n"
  1927. " // Information about anotherDecentlyLongValue.\n"
  1928. " anotherDecentlyLongValue,\n"
  1929. " // Information about aThirdDecentlyLongValue.\n"
  1930. " aThirdDecentlyLongValue\n"
  1931. "};");
  1932. }
  1933. TEST_F(FormatTest, FormatsBitfields) {
  1934. verifyFormat("struct Bitfields {\n"
  1935. " unsigned sClass : 8;\n"
  1936. " unsigned ValueKind : 2;\n"
  1937. "};");
  1938. verifyFormat("struct A {\n"
  1939. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
  1940. " bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
  1941. "};");
  1942. }
  1943. TEST_F(FormatTest, FormatsNamespaces) {
  1944. verifyFormat("namespace some_namespace {\n"
  1945. "class A {};\n"
  1946. "void f() { f(); }\n"
  1947. "}");
  1948. verifyFormat("namespace {\n"
  1949. "class A {};\n"
  1950. "void f() { f(); }\n"
  1951. "}");
  1952. verifyFormat("inline namespace X {\n"
  1953. "class A {};\n"
  1954. "void f() { f(); }\n"
  1955. "}");
  1956. verifyFormat("using namespace some_namespace;\n"
  1957. "class A {};\n"
  1958. "void f() { f(); }");
  1959. // This code is more common than we thought; if we
  1960. // layout this correctly the semicolon will go into
  1961. // its own line, which is undesirable.
  1962. verifyFormat("namespace {};");
  1963. verifyFormat("namespace {\n"
  1964. "class A {};\n"
  1965. "};");
  1966. verifyFormat("namespace {\n"
  1967. "int SomeVariable = 0; // comment\n"
  1968. "} // namespace");
  1969. EXPECT_EQ("#ifndef HEADER_GUARD\n"
  1970. "#define HEADER_GUARD\n"
  1971. "namespace my_namespace {\n"
  1972. "int i;\n"
  1973. "} // my_namespace\n"
  1974. "#endif // HEADER_GUARD",
  1975. format("#ifndef HEADER_GUARD\n"
  1976. " #define HEADER_GUARD\n"
  1977. " namespace my_namespace {\n"
  1978. "int i;\n"
  1979. "} // my_namespace\n"
  1980. "#endif // HEADER_GUARD"));
  1981. FormatStyle Style = getLLVMStyle();
  1982. Style.NamespaceIndentation = FormatStyle::NI_All;
  1983. EXPECT_EQ("namespace out {\n"
  1984. " int i;\n"
  1985. " namespace in {\n"
  1986. " int i;\n"
  1987. " } // namespace\n"
  1988. "} // namespace",
  1989. format("namespace out {\n"
  1990. "int i;\n"
  1991. "namespace in {\n"
  1992. "int i;\n"
  1993. "} // namespace\n"
  1994. "} // namespace",
  1995. Style));
  1996. Style.NamespaceIndentation = FormatStyle::NI_Inner;
  1997. EXPECT_EQ("namespace out {\n"
  1998. "int i;\n"
  1999. "namespace in {\n"
  2000. " int i;\n"
  2001. "} // namespace\n"
  2002. "} // namespace",
  2003. format("namespace out {\n"
  2004. "int i;\n"
  2005. "namespace in {\n"
  2006. "int i;\n"
  2007. "} // namespace\n"
  2008. "} // namespace",
  2009. Style));
  2010. }
  2011. TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
  2012. TEST_F(FormatTest, FormatsInlineASM) {
  2013. verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
  2014. verifyFormat("asm(\"nop\" ::: \"memory\");");
  2015. verifyFormat(
  2016. "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
  2017. " \"cpuid\\n\\t\"\n"
  2018. " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
  2019. " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
  2020. " : \"a\"(value));");
  2021. EXPECT_EQ(
  2022. "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
  2023. " __asm {\n"
  2024. " mov edx,[that] // vtable in edx\n"
  2025. " mov eax,methodIndex\n"
  2026. " call [edx][eax*4] // stdcall\n"
  2027. " }\n"
  2028. "}",
  2029. format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
  2030. " __asm {\n"
  2031. " mov edx,[that] // vtable in edx\n"
  2032. " mov eax,methodIndex\n"
  2033. " call [edx][eax*4] // stdcall\n"
  2034. " }\n"
  2035. "}"));
  2036. }
  2037. TEST_F(FormatTest, FormatTryCatch) {
  2038. verifyFormat("try {\n"
  2039. " throw a * b;\n"
  2040. "} catch (int a) {\n"
  2041. " // Do nothing.\n"
  2042. "} catch (...) {\n"
  2043. " exit(42);\n"
  2044. "}");
  2045. // Function-level try statements.
  2046. verifyFormat("int f() try { return 4; } catch (...) {\n"
  2047. " return 5;\n"
  2048. "}");
  2049. verifyFormat("class A {\n"
  2050. " int a;\n"
  2051. " A() try : a(0) {\n"
  2052. " } catch (...) {\n"
  2053. " throw;\n"
  2054. " }\n"
  2055. "};\n");
  2056. }
  2057. TEST_F(FormatTest, IncompleteTryCatchBlocks) {
  2058. verifyFormat("try {\n"
  2059. " f();\n"
  2060. "} catch {\n"
  2061. " g();\n"
  2062. "}");
  2063. verifyFormat("try {\n"
  2064. " f();\n"
  2065. "} catch (A a) MACRO(x) {\n"
  2066. " g();\n"
  2067. "} catch (B b) MACRO(x) {\n"
  2068. " g();\n"
  2069. "}");
  2070. }
  2071. TEST_F(FormatTest, FormatTryCatchBraceStyles) {
  2072. FormatStyle Style = getLLVMStyle();
  2073. Style.BreakBeforeBraces = FormatStyle::BS_Attach;
  2074. verifyFormat("try {\n"
  2075. " // something\n"
  2076. "} catch (...) {\n"
  2077. " // something\n"
  2078. "}",
  2079. Style);
  2080. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  2081. verifyFormat("try {\n"
  2082. " // something\n"
  2083. "}\n"
  2084. "catch (...) {\n"
  2085. " // something\n"
  2086. "}",
  2087. Style);
  2088. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  2089. verifyFormat("try\n"
  2090. "{\n"
  2091. " // something\n"
  2092. "}\n"
  2093. "catch (...)\n"
  2094. "{\n"
  2095. " // something\n"
  2096. "}",
  2097. Style);
  2098. Style.BreakBeforeBraces = FormatStyle::BS_GNU;
  2099. verifyFormat("try\n"
  2100. " {\n"
  2101. " // something\n"
  2102. " }\n"
  2103. "catch (...)\n"
  2104. " {\n"
  2105. " // something\n"
  2106. " }",
  2107. Style);
  2108. }
  2109. TEST_F(FormatTest, FormatObjCTryCatch) {
  2110. verifyFormat("@try {\n"
  2111. " f();\n"
  2112. "}\n"
  2113. "@catch (NSException e) {\n"
  2114. " @throw;\n"
  2115. "}\n"
  2116. "@finally {\n"
  2117. " exit(42);\n"
  2118. "}");
  2119. }
  2120. TEST_F(FormatTest, StaticInitializers) {
  2121. verifyFormat("static SomeClass SC = {1, 'a'};");
  2122. verifyFormat(
  2123. "static SomeClass WithALoooooooooooooooooooongName = {\n"
  2124. " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
  2125. // Here, everything other than the "}" would fit on a line.
  2126. verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
  2127. " 10000000000000000000000000};");
  2128. EXPECT_EQ("S s = {a,\n"
  2129. "\n"
  2130. " b};",
  2131. format("S s = {\n"
  2132. " a,\n"
  2133. "\n"
  2134. " b\n"
  2135. "};"));
  2136. // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
  2137. // line. However, the formatting looks a bit off and this probably doesn't
  2138. // happen often in practice.
  2139. verifyFormat("static int Variable[1] = {\n"
  2140. " {1000000000000000000000000000000000000}};",
  2141. getLLVMStyleWithColumns(40));
  2142. }
  2143. TEST_F(FormatTest, DesignatedInitializers) {
  2144. verifyFormat("const struct A a = {.a = 1, .b = 2};");
  2145. verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
  2146. " .bbbbbbbbbb = 2,\n"
  2147. " .cccccccccc = 3,\n"
  2148. " .dddddddddd = 4,\n"
  2149. " .eeeeeeeeee = 5};");
  2150. verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
  2151. " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
  2152. " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
  2153. " .ccccccccccccccccccccccccccc = 3,\n"
  2154. " .ddddddddddddddddddddddddddd = 4,\n"
  2155. " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
  2156. verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
  2157. }
  2158. TEST_F(FormatTest, NestedStaticInitializers) {
  2159. verifyFormat("static A x = {{{}}};\n");
  2160. verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
  2161. " {init1, init2, init3, init4}}};",
  2162. getLLVMStyleWithColumns(50));
  2163. verifyFormat("somes Status::global_reps[3] = {\n"
  2164. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  2165. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  2166. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
  2167. getLLVMStyleWithColumns(60));
  2168. verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
  2169. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  2170. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  2171. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
  2172. verifyFormat(
  2173. "CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
  2174. " {rect.fRight - rect.fLeft, rect.fBottom - rect.fTop}};");
  2175. verifyFormat(
  2176. "SomeArrayOfSomeType a = {\n"
  2177. " {{1, 2, 3},\n"
  2178. " {1, 2, 3},\n"
  2179. " {111111111111111111111111111111, 222222222222222222222222222222,\n"
  2180. " 333333333333333333333333333333},\n"
  2181. " {1, 2, 3},\n"
  2182. " {1, 2, 3}}};");
  2183. verifyFormat(
  2184. "SomeArrayOfSomeType a = {\n"
  2185. " {{1, 2, 3}},\n"
  2186. " {{1, 2, 3}},\n"
  2187. " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
  2188. " 333333333333333333333333333333}},\n"
  2189. " {{1, 2, 3}},\n"
  2190. " {{1, 2, 3}}};");
  2191. verifyFormat(
  2192. "struct {\n"
  2193. " unsigned bit;\n"
  2194. " const char *const name;\n"
  2195. "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
  2196. " {kOsWin, \"Windows\"},\n"
  2197. " {kOsLinux, \"Linux\"},\n"
  2198. " {kOsCrOS, \"Chrome OS\"}};");
  2199. verifyFormat(
  2200. "struct {\n"
  2201. " unsigned bit;\n"
  2202. " const char *const name;\n"
  2203. "} kBitsToOs[] = {\n"
  2204. " {kOsMac, \"Mac\"},\n"
  2205. " {kOsWin, \"Windows\"},\n"
  2206. " {kOsLinux, \"Linux\"},\n"
  2207. " {kOsCrOS, \"Chrome OS\"},\n"
  2208. "};");
  2209. }
  2210. TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
  2211. verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2212. " \\\n"
  2213. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
  2214. }
  2215. TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
  2216. verifyFormat("virtual void write(ELFWriter *writerrr,\n"
  2217. " OwningPtr<FileOutputBuffer> &buffer) = 0;");
  2218. }
  2219. TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
  2220. verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
  2221. getLLVMStyleWithColumns(40));
  2222. verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  2223. getLLVMStyleWithColumns(40));
  2224. EXPECT_EQ("#define Q \\\n"
  2225. " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n"
  2226. " \"aaaaaaaa.cpp\"",
  2227. format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  2228. getLLVMStyleWithColumns(40)));
  2229. }
  2230. TEST_F(FormatTest, UnderstandsLinePPDirective) {
  2231. EXPECT_EQ("# 123 \"A string literal\"",
  2232. format(" # 123 \"A string literal\""));
  2233. }
  2234. TEST_F(FormatTest, LayoutUnknownPPDirective) {
  2235. EXPECT_EQ("#;", format("#;"));
  2236. verifyFormat("#\n;\n;\n;");
  2237. }
  2238. TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
  2239. EXPECT_EQ("#line 42 \"test\"\n",
  2240. format("# \\\n line \\\n 42 \\\n \"test\"\n"));
  2241. EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n",
  2242. getLLVMStyleWithColumns(12)));
  2243. }
  2244. TEST_F(FormatTest, EndOfFileEndsPPDirective) {
  2245. EXPECT_EQ("#line 42 \"test\"",
  2246. format("# \\\n line \\\n 42 \\\n \"test\""));
  2247. EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B"));
  2248. }
  2249. TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
  2250. verifyFormat("#define A \\x20");
  2251. verifyFormat("#define A \\ x20");
  2252. EXPECT_EQ("#define A \\ x20", format("#define A \\ x20"));
  2253. verifyFormat("#define A ''");
  2254. verifyFormat("#define A ''qqq");
  2255. verifyFormat("#define A `qqq");
  2256. verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
  2257. EXPECT_EQ("const char *c = STRINGIFY(\n"
  2258. "\\na : b);",
  2259. format("const char * c = STRINGIFY(\n"
  2260. "\\na : b);"));
  2261. }
  2262. TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
  2263. verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
  2264. verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
  2265. verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
  2266. // FIXME: We never break before the macro name.
  2267. verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
  2268. verifyFormat("#define A A\n#define A A");
  2269. verifyFormat("#define A(X) A\n#define A A");
  2270. verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
  2271. verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
  2272. }
  2273. TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
  2274. EXPECT_EQ("// somecomment\n"
  2275. "#include \"a.h\"\n"
  2276. "#define A( \\\n"
  2277. " A, B)\n"
  2278. "#include \"b.h\"\n"
  2279. "// somecomment\n",
  2280. format(" // somecomment\n"
  2281. " #include \"a.h\"\n"
  2282. "#define A(A,\\\n"
  2283. " B)\n"
  2284. " #include \"b.h\"\n"
  2285. " // somecomment\n",
  2286. getLLVMStyleWithColumns(13)));
  2287. }
  2288. TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
  2289. TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
  2290. EXPECT_EQ("#define A \\\n"
  2291. " c; \\\n"
  2292. " e;\n"
  2293. "f;",
  2294. format("#define A c; e;\n"
  2295. "f;",
  2296. getLLVMStyleWithColumns(14)));
  2297. }
  2298. TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
  2299. TEST_F(FormatTest, AlwaysFormatsEntireMacroDefinitions) {
  2300. EXPECT_EQ("int i;\n"
  2301. "#define A \\\n"
  2302. " int i; \\\n"
  2303. " int j\n"
  2304. "int k;",
  2305. format("int i;\n"
  2306. "#define A \\\n"
  2307. " int i ; \\\n"
  2308. " int j\n"
  2309. "int k;",
  2310. 8, 0, getGoogleStyle())); // 8: position of "#define".
  2311. EXPECT_EQ("int i;\n"
  2312. "#define A \\\n"
  2313. " int i; \\\n"
  2314. " int j\n"
  2315. "int k;",
  2316. format("int i;\n"
  2317. "#define A \\\n"
  2318. " int i ; \\\n"
  2319. " int j\n"
  2320. "int k;",
  2321. 45, 0, getGoogleStyle())); // 45: position of "j".
  2322. }
  2323. TEST_F(FormatTest, MacroDefinitionInsideStatement) {
  2324. EXPECT_EQ("int x,\n"
  2325. "#define A\n"
  2326. " y;",
  2327. format("int x,\n#define A\ny;"));
  2328. }
  2329. TEST_F(FormatTest, HashInMacroDefinition) {
  2330. EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
  2331. verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
  2332. verifyFormat("#define A \\\n"
  2333. " { \\\n"
  2334. " f(#c); \\\n"
  2335. " }",
  2336. getLLVMStyleWithColumns(11));
  2337. verifyFormat("#define A(X) \\\n"
  2338. " void function##X()",
  2339. getLLVMStyleWithColumns(22));
  2340. verifyFormat("#define A(a, b, c) \\\n"
  2341. " void a##b##c()",
  2342. getLLVMStyleWithColumns(22));
  2343. verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
  2344. }
  2345. TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
  2346. EXPECT_EQ("#define A (x)", format("#define A (x)"));
  2347. EXPECT_EQ("#define A(x)", format("#define A(x)"));
  2348. }
  2349. TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
  2350. EXPECT_EQ("#define A b;", format("#define A \\\n"
  2351. " \\\n"
  2352. " b;",
  2353. getLLVMStyleWithColumns(25)));
  2354. EXPECT_EQ("#define A \\\n"
  2355. " \\\n"
  2356. " a; \\\n"
  2357. " b;",
  2358. format("#define A \\\n"
  2359. " \\\n"
  2360. " a; \\\n"
  2361. " b;",
  2362. getLLVMStyleWithColumns(11)));
  2363. EXPECT_EQ("#define A \\\n"
  2364. " a; \\\n"
  2365. " \\\n"
  2366. " b;",
  2367. format("#define A \\\n"
  2368. " a; \\\n"
  2369. " \\\n"
  2370. " b;",
  2371. getLLVMStyleWithColumns(11)));
  2372. }
  2373. TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
  2374. verifyFormat("#define A :");
  2375. verifyFormat("#define SOMECASES \\\n"
  2376. " case 1: \\\n"
  2377. " case 2\n",
  2378. getLLVMStyleWithColumns(20));
  2379. verifyFormat("#define A template <typename T>");
  2380. verifyFormat("#define STR(x) #x\n"
  2381. "f(STR(this_is_a_string_literal{));");
  2382. verifyFormat("#pragma omp threadprivate( \\\n"
  2383. " y)), // expected-warning",
  2384. getLLVMStyleWithColumns(28));
  2385. }
  2386. TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
  2387. verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
  2388. EXPECT_EQ("class A : public QObject {\n"
  2389. " Q_OBJECT\n"
  2390. "\n"
  2391. " A() {}\n"
  2392. "};",
  2393. format("class A : public QObject {\n"
  2394. " Q_OBJECT\n"
  2395. "\n"
  2396. " A() {\n}\n"
  2397. "} ;"));
  2398. EXPECT_EQ("SOME_MACRO\n"
  2399. "namespace {\n"
  2400. "void f();\n"
  2401. "}",
  2402. format("SOME_MACRO\n"
  2403. " namespace {\n"
  2404. "void f( );\n"
  2405. "}"));
  2406. // Only if the identifier contains at least 5 characters.
  2407. EXPECT_EQ("HTTP f();",
  2408. format("HTTP\nf();"));
  2409. EXPECT_EQ("MACRO\nf();",
  2410. format("MACRO\nf();"));
  2411. // Only if everything is upper case.
  2412. EXPECT_EQ("class A : public QObject {\n"
  2413. " Q_Object A() {}\n"
  2414. "};",
  2415. format("class A : public QObject {\n"
  2416. " Q_Object\n"
  2417. " A() {\n}\n"
  2418. "} ;"));
  2419. // Only if the next line can actually start an unwrapped line.
  2420. EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
  2421. format("SOME_WEIRD_LOG_MACRO\n"
  2422. "<< SomeThing;"));
  2423. }
  2424. TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
  2425. EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  2426. "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  2427. "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  2428. "class X {};\n"
  2429. "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  2430. "int *createScopDetectionPass() { return 0; }",
  2431. format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  2432. " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  2433. " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  2434. " class X {};\n"
  2435. " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  2436. " int *createScopDetectionPass() { return 0; }"));
  2437. // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
  2438. // braces, so that inner block is indented one level more.
  2439. EXPECT_EQ("int q() {\n"
  2440. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  2441. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  2442. " IPC_END_MESSAGE_MAP()\n"
  2443. "}",
  2444. format("int q() {\n"
  2445. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  2446. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  2447. " IPC_END_MESSAGE_MAP()\n"
  2448. "}"));
  2449. // Same inside macros.
  2450. EXPECT_EQ("#define LIST(L) \\\n"
  2451. " L(A) \\\n"
  2452. " L(B) \\\n"
  2453. " L(C)",
  2454. format("#define LIST(L) \\\n"
  2455. " L(A) \\\n"
  2456. " L(B) \\\n"
  2457. " L(C)",
  2458. getGoogleStyle()));
  2459. // These must not be recognized as macros.
  2460. EXPECT_EQ("int q() {\n"
  2461. " f(x);\n"
  2462. " f(x) {}\n"
  2463. " f(x)->g();\n"
  2464. " f(x)->*g();\n"
  2465. " f(x).g();\n"
  2466. " f(x) = x;\n"
  2467. " f(x) += x;\n"
  2468. " f(x) -= x;\n"
  2469. " f(x) *= x;\n"
  2470. " f(x) /= x;\n"
  2471. " f(x) %= x;\n"
  2472. " f(x) &= x;\n"
  2473. " f(x) |= x;\n"
  2474. " f(x) ^= x;\n"
  2475. " f(x) >>= x;\n"
  2476. " f(x) <<= x;\n"
  2477. " f(x)[y].z();\n"
  2478. " LOG(INFO) << x;\n"
  2479. " ifstream(x) >> x;\n"
  2480. "}\n",
  2481. format("int q() {\n"
  2482. " f(x)\n;\n"
  2483. " f(x)\n {}\n"
  2484. " f(x)\n->g();\n"
  2485. " f(x)\n->*g();\n"
  2486. " f(x)\n.g();\n"
  2487. " f(x)\n = x;\n"
  2488. " f(x)\n += x;\n"
  2489. " f(x)\n -= x;\n"
  2490. " f(x)\n *= x;\n"
  2491. " f(x)\n /= x;\n"
  2492. " f(x)\n %= x;\n"
  2493. " f(x)\n &= x;\n"
  2494. " f(x)\n |= x;\n"
  2495. " f(x)\n ^= x;\n"
  2496. " f(x)\n >>= x;\n"
  2497. " f(x)\n <<= x;\n"
  2498. " f(x)\n[y].z();\n"
  2499. " LOG(INFO)\n << x;\n"
  2500. " ifstream(x)\n >> x;\n"
  2501. "}\n"));
  2502. EXPECT_EQ("int q() {\n"
  2503. " F(x)\n"
  2504. " if (1) {\n"
  2505. " }\n"
  2506. " F(x)\n"
  2507. " while (1) {\n"
  2508. " }\n"
  2509. " F(x)\n"
  2510. " G(x);\n"
  2511. " F(x)\n"
  2512. " try {\n"
  2513. " Q();\n"
  2514. " } catch (...) {\n"
  2515. " }\n"
  2516. "}\n",
  2517. format("int q() {\n"
  2518. "F(x)\n"
  2519. "if (1) {}\n"
  2520. "F(x)\n"
  2521. "while (1) {}\n"
  2522. "F(x)\n"
  2523. "G(x);\n"
  2524. "F(x)\n"
  2525. "try { Q(); } catch (...) {}\n"
  2526. "}\n"));
  2527. EXPECT_EQ("class A {\n"
  2528. " A() : t(0) {}\n"
  2529. " A(int i) noexcept() : {}\n"
  2530. " A(X x)\n" // FIXME: function-level try blocks are broken.
  2531. " try : t(0) {\n"
  2532. " } catch (...) {\n"
  2533. " }\n"
  2534. "};",
  2535. format("class A {\n"
  2536. " A()\n : t(0) {}\n"
  2537. " A(int i)\n noexcept() : {}\n"
  2538. " A(X x)\n"
  2539. " try : t(0) {} catch (...) {}\n"
  2540. "};"));
  2541. EXPECT_EQ(
  2542. "class SomeClass {\n"
  2543. "public:\n"
  2544. " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2545. "};",
  2546. format("class SomeClass {\n"
  2547. "public:\n"
  2548. " SomeClass()\n"
  2549. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2550. "};"));
  2551. EXPECT_EQ(
  2552. "class SomeClass {\n"
  2553. "public:\n"
  2554. " SomeClass()\n"
  2555. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2556. "};",
  2557. format("class SomeClass {\n"
  2558. "public:\n"
  2559. " SomeClass()\n"
  2560. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2561. "};", getLLVMStyleWithColumns(40)));
  2562. }
  2563. TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
  2564. verifyFormat("#define A \\\n"
  2565. " f({ \\\n"
  2566. " g(); \\\n"
  2567. " });", getLLVMStyleWithColumns(11));
  2568. }
  2569. TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
  2570. EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
  2571. }
  2572. TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
  2573. verifyFormat("{\n { a #c; }\n}");
  2574. }
  2575. TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
  2576. EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
  2577. format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
  2578. EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
  2579. format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
  2580. }
  2581. TEST_F(FormatTest, EscapedNewlineAtStartOfToken) {
  2582. EXPECT_EQ(
  2583. "#define A \\\n int i; \\\n int j;",
  2584. format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
  2585. EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
  2586. }
  2587. TEST_F(FormatTest, NoEscapedNewlineHandlingInBlockComments) {
  2588. EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/"));
  2589. }
  2590. TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
  2591. verifyFormat("#define A \\\n"
  2592. " int v( \\\n"
  2593. " a); \\\n"
  2594. " int i;",
  2595. getLLVMStyleWithColumns(11));
  2596. }
  2597. TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
  2598. EXPECT_EQ(
  2599. "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2600. " \\\n"
  2601. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2602. "\n"
  2603. "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2604. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
  2605. format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2606. "\\\n"
  2607. "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2608. " \n"
  2609. " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2610. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
  2611. }
  2612. TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
  2613. EXPECT_EQ("int\n"
  2614. "#define A\n"
  2615. " a;",
  2616. format("int\n#define A\na;"));
  2617. verifyFormat("functionCallTo(\n"
  2618. " someOtherFunction(\n"
  2619. " withSomeParameters, whichInSequence,\n"
  2620. " areLongerThanALine(andAnotherCall,\n"
  2621. "#define A B\n"
  2622. " withMoreParamters,\n"
  2623. " whichStronglyInfluenceTheLayout),\n"
  2624. " andMoreParameters),\n"
  2625. " trailing);",
  2626. getLLVMStyleWithColumns(69));
  2627. verifyFormat("Foo::Foo()\n"
  2628. "#ifdef BAR\n"
  2629. " : baz(0)\n"
  2630. "#endif\n"
  2631. "{\n"
  2632. "}");
  2633. verifyFormat("void f() {\n"
  2634. " if (true)\n"
  2635. "#ifdef A\n"
  2636. " f(42);\n"
  2637. " x();\n"
  2638. "#else\n"
  2639. " g();\n"
  2640. " x();\n"
  2641. "#endif\n"
  2642. "}");
  2643. verifyFormat("void f(param1, param2,\n"
  2644. " param3,\n"
  2645. "#ifdef A\n"
  2646. " param4(param5,\n"
  2647. "#ifdef A1\n"
  2648. " param6,\n"
  2649. "#ifdef A2\n"
  2650. " param7),\n"
  2651. "#else\n"
  2652. " param8),\n"
  2653. " param9,\n"
  2654. "#endif\n"
  2655. " param10,\n"
  2656. "#endif\n"
  2657. " param11)\n"
  2658. "#else\n"
  2659. " param12)\n"
  2660. "#endif\n"
  2661. "{\n"
  2662. " x();\n"
  2663. "}",
  2664. getLLVMStyleWithColumns(28));
  2665. verifyFormat("#if 1\n"
  2666. "int i;");
  2667. verifyFormat(
  2668. "#if 1\n"
  2669. "#endif\n"
  2670. "#if 1\n"
  2671. "#else\n"
  2672. "#endif\n");
  2673. verifyFormat("DEBUG({\n"
  2674. " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2675. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  2676. "});\n"
  2677. "#if a\n"
  2678. "#else\n"
  2679. "#endif");
  2680. }
  2681. TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
  2682. verifyFormat("#endif\n"
  2683. "#if B");
  2684. }
  2685. TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
  2686. FormatStyle SingleLine = getLLVMStyle();
  2687. SingleLine.AllowShortIfStatementsOnASingleLine = true;
  2688. verifyFormat(
  2689. "#if 0\n"
  2690. "#elif 1\n"
  2691. "#endif\n"
  2692. "void foo() {\n"
  2693. " if (test) foo2();\n"
  2694. "}",
  2695. SingleLine);
  2696. }
  2697. TEST_F(FormatTest, LayoutBlockInsideParens) {
  2698. EXPECT_EQ("functionCall({ int i; });", format(" functionCall ( {int i;} );"));
  2699. EXPECT_EQ("functionCall({\n"
  2700. " int i;\n"
  2701. " int j;\n"
  2702. "});",
  2703. format(" functionCall ( {int i;int j;} );"));
  2704. EXPECT_EQ("functionCall({\n"
  2705. " int i;\n"
  2706. " int j;\n"
  2707. "}, aaaa, bbbb, cccc);",
  2708. format(" functionCall ( {int i;int j;}, aaaa, bbbb, cccc);"));
  2709. EXPECT_EQ("functionCall(\n"
  2710. " {\n"
  2711. " int i;\n"
  2712. " int j;\n"
  2713. " },\n"
  2714. " aaaa, bbbb, // comment\n"
  2715. " cccc);",
  2716. format(" functionCall ( {int i;int j;}, aaaa, bbbb, // comment\n"
  2717. "cccc);"));
  2718. EXPECT_EQ("functionCall(aaaa, bbbb, { int i; });",
  2719. format(" functionCall (aaaa, bbbb, {int i;});"));
  2720. EXPECT_EQ("functionCall(aaaa, bbbb, {\n"
  2721. " int i;\n"
  2722. " int j;\n"
  2723. "});",
  2724. format(" functionCall (aaaa, bbbb, {int i;int j;});"));
  2725. EXPECT_EQ("functionCall(aaaa, bbbb, { int i; });",
  2726. format(" functionCall (aaaa, bbbb, {int i;});"));
  2727. verifyFormat(
  2728. "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
  2729. " {\n"
  2730. " int i; // break\n"
  2731. " },\n"
  2732. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  2733. " ccccccccccccccccc));");
  2734. verifyFormat("DEBUG({\n"
  2735. " if (a)\n"
  2736. " f();\n"
  2737. "});");
  2738. }
  2739. TEST_F(FormatTest, LayoutBlockInsideStatement) {
  2740. EXPECT_EQ("SOME_MACRO { int i; }\n"
  2741. "int i;",
  2742. format(" SOME_MACRO {int i;} int i;"));
  2743. }
  2744. TEST_F(FormatTest, LayoutNestedBlocks) {
  2745. verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
  2746. " struct s {\n"
  2747. " int i;\n"
  2748. " };\n"
  2749. " s kBitsToOs[] = {{10}};\n"
  2750. " for (int i = 0; i < 10; ++i)\n"
  2751. " return;\n"
  2752. "}");
  2753. verifyFormat("call(parameter, {\n"
  2754. " something();\n"
  2755. " // Comment using all columns.\n"
  2756. " somethingelse();\n"
  2757. "});",
  2758. getLLVMStyleWithColumns(40));
  2759. verifyFormat("DEBUG( //\n"
  2760. " { f(); }, a);");
  2761. verifyFormat("DEBUG( //\n"
  2762. " {\n"
  2763. " f(); //\n"
  2764. " },\n"
  2765. " a);");
  2766. EXPECT_EQ("call(parameter, {\n"
  2767. " something();\n"
  2768. " // Comment too\n"
  2769. " // looooooooooong.\n"
  2770. " somethingElse();\n"
  2771. "});",
  2772. format("call(parameter, {\n"
  2773. " something();\n"
  2774. " // Comment too looooooooooong.\n"
  2775. " somethingElse();\n"
  2776. "});",
  2777. getLLVMStyleWithColumns(29)));
  2778. EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });"));
  2779. EXPECT_EQ("DEBUG({ // comment\n"
  2780. " int i;\n"
  2781. "});",
  2782. format("DEBUG({ // comment\n"
  2783. "int i;\n"
  2784. "});"));
  2785. EXPECT_EQ("DEBUG({\n"
  2786. " int i;\n"
  2787. "\n"
  2788. " // comment\n"
  2789. " int j;\n"
  2790. "});",
  2791. format("DEBUG({\n"
  2792. " int i;\n"
  2793. "\n"
  2794. " // comment\n"
  2795. " int j;\n"
  2796. "});"));
  2797. verifyFormat("DEBUG({\n"
  2798. " if (a)\n"
  2799. " return;\n"
  2800. "});");
  2801. verifyGoogleFormat("DEBUG({\n"
  2802. " if (a) return;\n"
  2803. "});");
  2804. FormatStyle Style = getGoogleStyle();
  2805. Style.ColumnLimit = 45;
  2806. verifyFormat("Debug(aaaaa, {\n"
  2807. " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
  2808. "}, a);",
  2809. Style);
  2810. }
  2811. TEST_F(FormatTest, IndividualStatementsOfNestedBlocks) {
  2812. EXPECT_EQ("DEBUG({\n"
  2813. " int i;\n"
  2814. " int j;\n"
  2815. "});",
  2816. format("DEBUG( {\n"
  2817. " int i;\n"
  2818. " int j;\n"
  2819. "} ) ;",
  2820. 20, 1, getLLVMStyle()));
  2821. EXPECT_EQ("DEBUG( {\n"
  2822. " int i;\n"
  2823. " int j;\n"
  2824. "} ) ;",
  2825. format("DEBUG( {\n"
  2826. " int i;\n"
  2827. " int j;\n"
  2828. "} ) ;",
  2829. 41, 1, getLLVMStyle()));
  2830. EXPECT_EQ("DEBUG( {\n"
  2831. " int i;\n"
  2832. " int j;\n"
  2833. "} ) ;",
  2834. format("DEBUG( {\n"
  2835. " int i;\n"
  2836. " int j;\n"
  2837. "} ) ;",
  2838. 41, 1, getLLVMStyle()));
  2839. EXPECT_EQ("DEBUG({\n"
  2840. " int i;\n"
  2841. " int j;\n"
  2842. "});",
  2843. format("DEBUG( {\n"
  2844. " int i;\n"
  2845. " int j;\n"
  2846. "} ) ;",
  2847. 20, 1, getLLVMStyle()));
  2848. EXPECT_EQ("Debug({\n"
  2849. " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2850. " return;\n"
  2851. " },\n"
  2852. " a);",
  2853. format("Debug({\n"
  2854. " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2855. " return;\n"
  2856. " },\n"
  2857. " a);",
  2858. 50, 1, getLLVMStyle()));
  2859. EXPECT_EQ("DEBUG({\n"
  2860. " DEBUG({\n"
  2861. " int a;\n"
  2862. " int b;\n"
  2863. " }) ;\n"
  2864. "});",
  2865. format("DEBUG({\n"
  2866. " DEBUG({\n"
  2867. " int a;\n"
  2868. " int b;\n" // Format this line only.
  2869. " }) ;\n" // Don't touch this line.
  2870. "});",
  2871. 35, 0, getLLVMStyle()));
  2872. EXPECT_EQ("DEBUG({\n"
  2873. " int a; //\n"
  2874. "});",
  2875. format("DEBUG({\n"
  2876. " int a; //\n"
  2877. "});",
  2878. 0, 0, getLLVMStyle()));
  2879. }
  2880. TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
  2881. EXPECT_EQ("{}", format("{}"));
  2882. verifyFormat("enum E {};");
  2883. verifyFormat("enum E {}");
  2884. }
  2885. //===----------------------------------------------------------------------===//
  2886. // Line break tests.
  2887. //===----------------------------------------------------------------------===//
  2888. TEST_F(FormatTest, PreventConfusingIndents) {
  2889. verifyFormat(
  2890. "void f() {\n"
  2891. " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
  2892. " parameter, parameter, parameter)),\n"
  2893. " SecondLongCall(parameter));\n"
  2894. "}");
  2895. verifyFormat(
  2896. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2897. " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2898. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2899. " aaaaaaaaaaaaaaaaaaaaaaaa);");
  2900. verifyFormat(
  2901. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2902. " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
  2903. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  2904. " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
  2905. verifyFormat(
  2906. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  2907. " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
  2908. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
  2909. " aaaaaaaaaaaaaaaaaaaaaaaa>;");
  2910. verifyFormat("int a = bbbb && ccc && fffff(\n"
  2911. "#define A Just forcing a new line\n"
  2912. " ddd);");
  2913. }
  2914. TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
  2915. verifyFormat(
  2916. "bool aaaaaaa =\n"
  2917. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
  2918. " bbbbbbbb();");
  2919. verifyFormat(
  2920. "bool aaaaaaa =\n"
  2921. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
  2922. " bbbbbbbb();");
  2923. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  2924. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
  2925. " ccccccccc == ddddddddddd;");
  2926. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  2927. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
  2928. " ccccccccc == ddddddddddd;");
  2929. verifyFormat(
  2930. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  2931. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
  2932. " ccccccccc == ddddddddddd;");
  2933. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  2934. " aaaaaa) &&\n"
  2935. " bbbbbb && cccccc;");
  2936. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  2937. " aaaaaa) >>\n"
  2938. " bbbbbb;");
  2939. verifyFormat("Whitespaces.addUntouchableComment(\n"
  2940. " SourceMgr.getSpellingColumnNumber(\n"
  2941. " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
  2942. " 1);");
  2943. verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2944. " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
  2945. " cccccc) {\n}");
  2946. verifyFormat("b = a &&\n"
  2947. " // Comment\n"
  2948. " b.c && d;");
  2949. // If the LHS of a comparison is not a binary expression itself, the
  2950. // additional linebreak confuses many people.
  2951. verifyFormat(
  2952. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2953. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
  2954. "}");
  2955. verifyFormat(
  2956. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2957. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  2958. "}");
  2959. verifyFormat(
  2960. "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
  2961. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  2962. "}");
  2963. // Even explicit parentheses stress the precedence enough to make the
  2964. // additional break unnecessary.
  2965. verifyFormat(
  2966. "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2967. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  2968. "}");
  2969. // This cases is borderline, but with the indentation it is still readable.
  2970. verifyFormat(
  2971. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2972. " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2973. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  2974. "}",
  2975. getLLVMStyleWithColumns(75));
  2976. // If the LHS is a binary expression, we should still use the additional break
  2977. // as otherwise the formatting hides the operator precedence.
  2978. verifyFormat(
  2979. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2980. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2981. " 5) {\n"
  2982. "}");
  2983. FormatStyle OnePerLine = getLLVMStyle();
  2984. OnePerLine.BinPackParameters = false;
  2985. verifyFormat(
  2986. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2987. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2988. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
  2989. OnePerLine);
  2990. }
  2991. TEST_F(FormatTest, ExpressionIndentation) {
  2992. verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2993. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2994. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2995. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2996. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
  2997. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
  2998. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2999. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
  3000. " ccccccccccccccccccccccccccccccccccccccccc;");
  3001. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3002. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3003. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3004. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3005. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3006. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3007. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3008. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3009. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3010. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3011. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3012. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3013. verifyFormat("if () {\n"
  3014. "} else if (aaaaa &&\n"
  3015. " bbbbb > // break\n"
  3016. " ccccc) {\n"
  3017. "}");
  3018. // Presence of a trailing comment used to change indentation of b.
  3019. verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
  3020. " b;\n"
  3021. "return aaaaaaaaaaaaaaaaaaa +\n"
  3022. " b; //",
  3023. getLLVMStyleWithColumns(30));
  3024. }
  3025. TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
  3026. // Not sure what the best system is here. Like this, the LHS can be found
  3027. // immediately above an operator (everything with the same or a higher
  3028. // indent). The RHS is aligned right of the operator and so compasses
  3029. // everything until something with the same indent as the operator is found.
  3030. // FIXME: Is this a good system?
  3031. FormatStyle Style = getLLVMStyle();
  3032. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  3033. verifyFormat(
  3034. "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3035. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3036. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3037. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3038. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3039. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3040. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3041. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3042. " > ccccccccccccccccccccccccccccccccccccccccc;",
  3043. Style);
  3044. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3045. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3046. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3047. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3048. Style);
  3049. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3050. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3051. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3052. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3053. Style);
  3054. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3055. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3056. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3057. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3058. Style);
  3059. verifyFormat("if () {\n"
  3060. "} else if (aaaaa\n"
  3061. " && bbbbb // break\n"
  3062. " > ccccc) {\n"
  3063. "}",
  3064. Style);
  3065. verifyFormat("return (a)\n"
  3066. " // comment\n"
  3067. " + b;",
  3068. Style);
  3069. // Forced by comments.
  3070. verifyFormat(
  3071. "unsigned ContentSize =\n"
  3072. " sizeof(int16_t) // DWARF ARange version number\n"
  3073. " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
  3074. " + sizeof(int8_t) // Pointer Size (in bytes)\n"
  3075. " + sizeof(int8_t); // Segment Size (in bytes)");
  3076. verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
  3077. " == boost::fusion::at_c<1>(iiii).second;",
  3078. Style);
  3079. Style.ColumnLimit = 60;
  3080. verifyFormat("zzzzzzzzzz\n"
  3081. " = bbbbbbbbbbbbbbbbb\n"
  3082. " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3083. Style);
  3084. }
  3085. TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
  3086. FormatStyle Style = getLLVMStyle();
  3087. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
  3088. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3089. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3090. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", Style);
  3091. }
  3092. TEST_F(FormatTest, ConstructorInitializers) {
  3093. verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
  3094. verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
  3095. getLLVMStyleWithColumns(45));
  3096. verifyFormat("Constructor()\n"
  3097. " : Inttializer(FitsOnTheLine) {}",
  3098. getLLVMStyleWithColumns(44));
  3099. verifyFormat("Constructor()\n"
  3100. " : Inttializer(FitsOnTheLine) {}",
  3101. getLLVMStyleWithColumns(43));
  3102. verifyFormat(
  3103. "SomeClass::Constructor()\n"
  3104. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  3105. verifyFormat(
  3106. "SomeClass::Constructor()\n"
  3107. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3108. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
  3109. verifyFormat(
  3110. "SomeClass::Constructor()\n"
  3111. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3112. " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  3113. verifyFormat("Constructor()\n"
  3114. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3115. " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3116. " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3117. " aaaaaaaaaaaaaaaaaaaaaaa() {}");
  3118. verifyFormat("Constructor()\n"
  3119. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3120. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3121. verifyFormat("Constructor(int Parameter = 0)\n"
  3122. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
  3123. " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
  3124. verifyFormat("Constructor()\n"
  3125. " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
  3126. "}",
  3127. getLLVMStyleWithColumns(60));
  3128. verifyFormat("Constructor()\n"
  3129. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3130. " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
  3131. // Here a line could be saved by splitting the second initializer onto two
  3132. // lines, but that is not desirable.
  3133. verifyFormat("Constructor()\n"
  3134. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3135. " aaaaaaaaaaa(aaaaaaaaaaa),\n"
  3136. " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3137. FormatStyle OnePerLine = getLLVMStyle();
  3138. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  3139. verifyFormat("SomeClass::Constructor()\n"
  3140. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3141. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3142. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  3143. OnePerLine);
  3144. verifyFormat("SomeClass::Constructor()\n"
  3145. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
  3146. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3147. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  3148. OnePerLine);
  3149. verifyFormat("MyClass::MyClass(int var)\n"
  3150. " : some_var_(var), // 4 space indent\n"
  3151. " some_other_var_(var + 1) { // lined up\n"
  3152. "}",
  3153. OnePerLine);
  3154. verifyFormat("Constructor()\n"
  3155. " : aaaaa(aaaaaa),\n"
  3156. " aaaaa(aaaaaa),\n"
  3157. " aaaaa(aaaaaa),\n"
  3158. " aaaaa(aaaaaa),\n"
  3159. " aaaaa(aaaaaa) {}",
  3160. OnePerLine);
  3161. verifyFormat("Constructor()\n"
  3162. " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
  3163. " aaaaaaaaaaaaaaaaaaaaaa) {}",
  3164. OnePerLine);
  3165. EXPECT_EQ("Constructor()\n"
  3166. " : // Comment forcing unwanted break.\n"
  3167. " aaaa(aaaa) {}",
  3168. format("Constructor() :\n"
  3169. " // Comment forcing unwanted break.\n"
  3170. " aaaa(aaaa) {}"));
  3171. }
  3172. TEST_F(FormatTest, MemoizationTests) {
  3173. // This breaks if the memoization lookup does not take \c Indent and
  3174. // \c LastSpace into account.
  3175. verifyFormat(
  3176. "extern CFRunLoopTimerRef\n"
  3177. "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
  3178. " CFTimeInterval interval, CFOptionFlags flags,\n"
  3179. " CFIndex order, CFRunLoopTimerCallBack callout,\n"
  3180. " CFRunLoopTimerContext *context) {}");
  3181. // Deep nesting somewhat works around our memoization.
  3182. verifyFormat(
  3183. "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3184. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3185. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3186. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3187. " aaaaa())))))))))))))))))))))))))))))))))))))));",
  3188. getLLVMStyleWithColumns(65));
  3189. verifyFormat(
  3190. "aaaaa(\n"
  3191. " aaaaa,\n"
  3192. " aaaaa(\n"
  3193. " aaaaa,\n"
  3194. " aaaaa(\n"
  3195. " aaaaa,\n"
  3196. " aaaaa(\n"
  3197. " aaaaa,\n"
  3198. " aaaaa(\n"
  3199. " aaaaa,\n"
  3200. " aaaaa(\n"
  3201. " aaaaa,\n"
  3202. " aaaaa(\n"
  3203. " aaaaa,\n"
  3204. " aaaaa(\n"
  3205. " aaaaa,\n"
  3206. " aaaaa(\n"
  3207. " aaaaa,\n"
  3208. " aaaaa(\n"
  3209. " aaaaa,\n"
  3210. " aaaaa(\n"
  3211. " aaaaa,\n"
  3212. " aaaaa(\n"
  3213. " aaaaa,\n"
  3214. " aaaaa))))))))))));",
  3215. getLLVMStyleWithColumns(65));
  3216. verifyFormat(
  3217. "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"
  3218. " a),\n"
  3219. " a),\n"
  3220. " a),\n"
  3221. " a),\n"
  3222. " a),\n"
  3223. " a),\n"
  3224. " a),\n"
  3225. " a),\n"
  3226. " a),\n"
  3227. " a),\n"
  3228. " a),\n"
  3229. " a),\n"
  3230. " a),\n"
  3231. " a),\n"
  3232. " a),\n"
  3233. " a),\n"
  3234. " a)",
  3235. getLLVMStyleWithColumns(65));
  3236. // This test takes VERY long when memoization is broken.
  3237. FormatStyle OnePerLine = getLLVMStyle();
  3238. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  3239. OnePerLine.BinPackParameters = false;
  3240. std::string input = "Constructor()\n"
  3241. " : aaaa(a,\n";
  3242. for (unsigned i = 0, e = 80; i != e; ++i) {
  3243. input += " a,\n";
  3244. }
  3245. input += " a) {}";
  3246. verifyFormat(input, OnePerLine);
  3247. }
  3248. TEST_F(FormatTest, BreaksAsHighAsPossible) {
  3249. verifyFormat(
  3250. "void f() {\n"
  3251. " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
  3252. " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
  3253. " f();\n"
  3254. "}");
  3255. verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
  3256. " Intervals[i - 1].getRange().getLast()) {\n}");
  3257. }
  3258. TEST_F(FormatTest, BreaksFunctionDeclarations) {
  3259. // Principially, we break function declarations in a certain order:
  3260. // 1) break amongst arguments.
  3261. verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
  3262. " Cccccccccccccc cccccccccccccc);");
  3263. verifyFormat(
  3264. "template <class TemplateIt>\n"
  3265. "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
  3266. " TemplateIt *stop) {}");
  3267. // 2) break after return type.
  3268. verifyFormat(
  3269. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3270. "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
  3271. getGoogleStyle());
  3272. // 3) break after (.
  3273. verifyFormat(
  3274. "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
  3275. " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
  3276. getGoogleStyle());
  3277. // 4) break before after nested name specifiers.
  3278. verifyFormat(
  3279. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3280. "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
  3281. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
  3282. getGoogleStyle());
  3283. // However, there are exceptions, if a sufficient amount of lines can be
  3284. // saved.
  3285. // FIXME: The precise cut-offs wrt. the number of saved lines might need some
  3286. // more adjusting.
  3287. verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  3288. " Cccccccccccccc cccccccccc,\n"
  3289. " Cccccccccccccc cccccccccc,\n"
  3290. " Cccccccccccccc cccccccccc,\n"
  3291. " Cccccccccccccc cccccccccc);");
  3292. verifyFormat(
  3293. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3294. "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3295. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3296. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
  3297. getGoogleStyle());
  3298. verifyFormat(
  3299. "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  3300. " Cccccccccccccc cccccccccc,\n"
  3301. " Cccccccccccccc cccccccccc,\n"
  3302. " Cccccccccccccc cccccccccc,\n"
  3303. " Cccccccccccccc cccccccccc,\n"
  3304. " Cccccccccccccc cccccccccc,\n"
  3305. " Cccccccccccccc cccccccccc);");
  3306. verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3307. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3308. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3309. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3310. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
  3311. // Break after multi-line parameters.
  3312. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3313. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3314. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3315. " bbbb bbbb);");
  3316. verifyFormat("void SomeLoooooooooooongFunction(\n"
  3317. " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  3318. " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3319. " int bbbbbbbbbbbbb);");
  3320. // Treat overloaded operators like other functions.
  3321. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3322. "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
  3323. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3324. "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
  3325. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3326. "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
  3327. verifyGoogleFormat(
  3328. "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
  3329. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  3330. verifyGoogleFormat(
  3331. "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
  3332. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  3333. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3334. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  3335. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
  3336. "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  3337. verifyGoogleFormat(
  3338. "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
  3339. "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3340. " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
  3341. }
  3342. TEST_F(FormatTest, TrailingReturnType) {
  3343. verifyFormat("auto foo() -> int;\n");
  3344. verifyFormat("struct S {\n"
  3345. " auto bar() const -> int;\n"
  3346. "};");
  3347. verifyFormat("template <size_t Order, typename T>\n"
  3348. "auto load_img(const std::string &filename)\n"
  3349. " -> alias::tensor<Order, T, mem::tag::cpu> {}");
  3350. verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
  3351. " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
  3352. verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
  3353. // Not trailing return types.
  3354. verifyFormat("void f() { auto a = b->c(); }");
  3355. }
  3356. TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
  3357. // Avoid breaking before trailing 'const' or other trailing annotations, if
  3358. // they are not function-like.
  3359. FormatStyle Style = getGoogleStyle();
  3360. Style.ColumnLimit = 47;
  3361. verifyFormat("void someLongFunction(\n"
  3362. " int someLoooooooooooooongParameter) const {\n}",
  3363. getLLVMStyleWithColumns(47));
  3364. verifyFormat("LoooooongReturnType\n"
  3365. "someLoooooooongFunction() const {}",
  3366. getLLVMStyleWithColumns(47));
  3367. verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
  3368. " const {}",
  3369. Style);
  3370. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3371. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
  3372. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3373. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
  3374. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3375. " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
  3376. verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
  3377. " aaaaaaaaaaa aaaaa) const override;");
  3378. verifyGoogleFormat(
  3379. "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3380. " const override;");
  3381. // Even if the first parameter has to be wrapped.
  3382. verifyFormat("void someLongFunction(\n"
  3383. " int someLongParameter) const {}",
  3384. getLLVMStyleWithColumns(46));
  3385. verifyFormat("void someLongFunction(\n"
  3386. " int someLongParameter) const {}",
  3387. Style);
  3388. verifyFormat("void someLongFunction(\n"
  3389. " int someLongParameter) override {}",
  3390. Style);
  3391. verifyFormat("void someLongFunction(\n"
  3392. " int someLongParameter) OVERRIDE {}",
  3393. Style);
  3394. verifyFormat("void someLongFunction(\n"
  3395. " int someLongParameter) final {}",
  3396. Style);
  3397. verifyFormat("void someLongFunction(\n"
  3398. " int someLongParameter) FINAL {}",
  3399. Style);
  3400. verifyFormat("void someLongFunction(\n"
  3401. " int parameter) const override {}",
  3402. Style);
  3403. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  3404. verifyFormat("void someLongFunction(\n"
  3405. " int someLongParameter) const\n"
  3406. "{\n"
  3407. "}",
  3408. Style);
  3409. // Unless these are unknown annotations.
  3410. verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
  3411. " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3412. " LONG_AND_UGLY_ANNOTATION;");
  3413. // Breaking before function-like trailing annotations is fine to keep them
  3414. // close to their arguments.
  3415. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3416. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  3417. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  3418. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  3419. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  3420. " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
  3421. verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
  3422. " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
  3423. verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
  3424. verifyFormat(
  3425. "void aaaaaaaaaaaaaaaaaa()\n"
  3426. " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3427. " aaaaaaaaaaaaaaaaaaaaaaaaa));");
  3428. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3429. " __attribute__((unused));");
  3430. verifyGoogleFormat(
  3431. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3432. " GUARDED_BY(aaaaaaaaaaaa);");
  3433. verifyGoogleFormat(
  3434. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3435. " GUARDED_BY(aaaaaaaaaaaa);");
  3436. verifyGoogleFormat(
  3437. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
  3438. " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3439. }
  3440. TEST_F(FormatTest, BreaksDesireably) {
  3441. verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  3442. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  3443. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
  3444. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3445. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  3446. "}");
  3447. verifyFormat(
  3448. "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3449. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3450. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3451. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3452. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3453. verifyFormat(
  3454. "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3455. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  3456. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3457. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
  3458. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3459. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3460. verifyFormat(
  3461. "void f() {\n"
  3462. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
  3463. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3464. "}");
  3465. verifyFormat(
  3466. "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3467. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3468. verifyFormat(
  3469. "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3470. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3471. verifyFormat(
  3472. "aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3473. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3474. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3475. // Indent consistently independent of call expression.
  3476. verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
  3477. " dddddddddddddddddddddddddddddd));\n"
  3478. "aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3479. " dddddddddddddddddddddddddddddd));");
  3480. // This test case breaks on an incorrect memoization, i.e. an optimization not
  3481. // taking into account the StopAt value.
  3482. verifyFormat(
  3483. "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3484. " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3485. " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3486. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3487. verifyFormat("{\n {\n {\n"
  3488. " Annotation.SpaceRequiredBefore =\n"
  3489. " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
  3490. " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
  3491. " }\n }\n}");
  3492. // Break on an outer level if there was a break on an inner level.
  3493. EXPECT_EQ("f(g(h(a, // comment\n"
  3494. " b, c),\n"
  3495. " d, e),\n"
  3496. " x, y);",
  3497. format("f(g(h(a, // comment\n"
  3498. " b, c), d, e), x, y);"));
  3499. // Prefer breaking similar line breaks.
  3500. verifyFormat(
  3501. "const int kTrackingOptions = NSTrackingMouseMoved |\n"
  3502. " NSTrackingMouseEnteredAndExited |\n"
  3503. " NSTrackingActiveAlways;");
  3504. }
  3505. TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
  3506. FormatStyle NoBinPacking = getGoogleStyle();
  3507. NoBinPacking.BinPackParameters = false;
  3508. NoBinPacking.BinPackArguments = true;
  3509. verifyFormat("void f() {\n"
  3510. " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
  3511. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3512. "}",
  3513. NoBinPacking);
  3514. verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
  3515. " int aaaaaaaaaaaaaaaaaaaa,\n"
  3516. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3517. NoBinPacking);
  3518. }
  3519. TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
  3520. FormatStyle NoBinPacking = getGoogleStyle();
  3521. NoBinPacking.BinPackParameters = false;
  3522. NoBinPacking.BinPackArguments = false;
  3523. verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
  3524. " aaaaaaaaaaaaaaaaaaaa,\n"
  3525. " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
  3526. NoBinPacking);
  3527. verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
  3528. " aaaaaaaaaaaaa,\n"
  3529. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
  3530. NoBinPacking);
  3531. verifyFormat(
  3532. "aaaaaaaa(aaaaaaaaaaaaa,\n"
  3533. " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3534. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  3535. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3536. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
  3537. NoBinPacking);
  3538. verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  3539. " .aaaaaaaaaaaaaaaaaa();",
  3540. NoBinPacking);
  3541. verifyFormat("void f() {\n"
  3542. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3543. " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
  3544. "}",
  3545. NoBinPacking);
  3546. verifyFormat(
  3547. "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3548. " aaaaaaaaaaaa,\n"
  3549. " aaaaaaaaaaaa);",
  3550. NoBinPacking);
  3551. verifyFormat(
  3552. "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
  3553. " ddddddddddddddddddddddddddddd),\n"
  3554. " test);",
  3555. NoBinPacking);
  3556. verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3557. " aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3558. " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;",
  3559. NoBinPacking);
  3560. verifyFormat("a(\"a\"\n"
  3561. " \"a\",\n"
  3562. " a);");
  3563. NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
  3564. verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
  3565. " aaaaaaaaa,\n"
  3566. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3567. NoBinPacking);
  3568. verifyFormat(
  3569. "void f() {\n"
  3570. " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  3571. " .aaaaaaa();\n"
  3572. "}",
  3573. NoBinPacking);
  3574. verifyFormat(
  3575. "template <class SomeType, class SomeOtherType>\n"
  3576. "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
  3577. NoBinPacking);
  3578. }
  3579. TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
  3580. FormatStyle Style = getLLVMStyleWithColumns(15);
  3581. Style.ExperimentalAutoDetectBinPacking = true;
  3582. EXPECT_EQ("aaa(aaaa,\n"
  3583. " aaaa,\n"
  3584. " aaaa);\n"
  3585. "aaa(aaaa,\n"
  3586. " aaaa,\n"
  3587. " aaaa);",
  3588. format("aaa(aaaa,\n" // one-per-line
  3589. " aaaa,\n"
  3590. " aaaa );\n"
  3591. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  3592. Style));
  3593. EXPECT_EQ("aaa(aaaa, aaaa,\n"
  3594. " aaaa);\n"
  3595. "aaa(aaaa, aaaa,\n"
  3596. " aaaa);",
  3597. format("aaa(aaaa, aaaa,\n" // bin-packed
  3598. " aaaa );\n"
  3599. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  3600. Style));
  3601. }
  3602. TEST_F(FormatTest, FormatsBuilderPattern) {
  3603. verifyFormat(
  3604. "return llvm::StringSwitch<Reference::Kind>(name)\n"
  3605. " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
  3606. " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
  3607. " .StartsWith(\".init\", ORDER_INIT)\n"
  3608. " .StartsWith(\".fini\", ORDER_FINI)\n"
  3609. " .StartsWith(\".hash\", ORDER_HASH)\n"
  3610. " .Default(ORDER_TEXT);\n");
  3611. verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
  3612. " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
  3613. verifyFormat(
  3614. "aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaa(\n"
  3615. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3616. " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
  3617. verifyFormat(
  3618. "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
  3619. " aaaaaaaaaaaaaa);");
  3620. verifyFormat(
  3621. "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
  3622. " aaaaaa->aaaaaaaaaaaa()\n"
  3623. " ->aaaaaaaaaaaaaaaa(\n"
  3624. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3625. " ->aaaaaaaaaaaaaaaaa();");
  3626. verifyGoogleFormat(
  3627. "void f() {\n"
  3628. " someo->Add((new util::filetools::Handler(dir))\n"
  3629. " ->OnEvent1(NewPermanentCallback(\n"
  3630. " this, &HandlerHolderClass::EventHandlerCBA))\n"
  3631. " ->OnEvent2(NewPermanentCallback(\n"
  3632. " this, &HandlerHolderClass::EventHandlerCBB))\n"
  3633. " ->OnEvent3(NewPermanentCallback(\n"
  3634. " this, &HandlerHolderClass::EventHandlerCBC))\n"
  3635. " ->OnEvent5(NewPermanentCallback(\n"
  3636. " this, &HandlerHolderClass::EventHandlerCBD))\n"
  3637. " ->OnEvent6(NewPermanentCallback(\n"
  3638. " this, &HandlerHolderClass::EventHandlerCBE)));\n"
  3639. "}");
  3640. verifyFormat(
  3641. "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
  3642. verifyFormat("aaaaaaaaaaaaaaa()\n"
  3643. " .aaaaaaaaaaaaaaa()\n"
  3644. " .aaaaaaaaaaaaaaa()\n"
  3645. " .aaaaaaaaaaaaaaa()\n"
  3646. " .aaaaaaaaaaaaaaa();");
  3647. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3648. " .aaaaaaaaaaaaaaa()\n"
  3649. " .aaaaaaaaaaaaaaa()\n"
  3650. " .aaaaaaaaaaaaaaa();");
  3651. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3652. " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3653. " .aaaaaaaaaaaaaaa();");
  3654. verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3655. " ->aaaaaaaaaaaaaae(0)\n"
  3656. " ->aaaaaaaaaaaaaaa();");
  3657. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  3658. " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3659. " .has<bbbbbbbbbbbbbbbbbbbbb>();");
  3660. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  3661. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  3662. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
  3663. // Prefer not to break after empty parentheses.
  3664. verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
  3665. " First->LastNewlineOffset);");
  3666. }
  3667. TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
  3668. verifyFormat(
  3669. "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3670. " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
  3671. verifyFormat(
  3672. "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
  3673. " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
  3674. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  3675. " ccccccccccccccccccccccccc) {\n}");
  3676. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
  3677. " ccccccccccccccccccccccccc) {\n}");
  3678. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  3679. " ccccccccccccccccccccccccc) {\n}");
  3680. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
  3681. " ccccccccccccccccccccccccc) {\n}");
  3682. verifyFormat(
  3683. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
  3684. " ccccccccccccccccccccccccc) {\n}");
  3685. verifyFormat(
  3686. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
  3687. " ccccccccccccccccccccccccc) {\n}");
  3688. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
  3689. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
  3690. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
  3691. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  3692. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
  3693. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
  3694. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
  3695. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  3696. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
  3697. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
  3698. " aaaaaaaaaaaaaaa != aa) {\n}");
  3699. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
  3700. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
  3701. " aaaaaaaaaaaaaaa != aa) {\n}");
  3702. }
  3703. TEST_F(FormatTest, BreaksAfterAssignments) {
  3704. verifyFormat(
  3705. "unsigned Cost =\n"
  3706. " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
  3707. " SI->getPointerAddressSpaceee());\n");
  3708. verifyFormat(
  3709. "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
  3710. " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
  3711. verifyFormat(
  3712. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
  3713. " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
  3714. verifyFormat("unsigned OriginalStartColumn =\n"
  3715. " SourceMgr.getSpellingColumnNumber(\n"
  3716. " Current.FormatTok.getStartOfNonWhitespace()) -\n"
  3717. " 1;");
  3718. }
  3719. TEST_F(FormatTest, AlignsAfterAssignments) {
  3720. verifyFormat(
  3721. "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3722. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3723. verifyFormat(
  3724. "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3725. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3726. verifyFormat(
  3727. "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3728. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3729. verifyFormat(
  3730. "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3731. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  3732. verifyFormat(
  3733. "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3734. " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3735. " aaaaaaaaaaaaaaaaaaaaaaaa;");
  3736. }
  3737. TEST_F(FormatTest, AlignsAfterReturn) {
  3738. verifyFormat(
  3739. "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3740. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3741. verifyFormat(
  3742. "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3743. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  3744. verifyFormat(
  3745. "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  3746. " aaaaaaaaaaaaaaaaaaaaaa();");
  3747. verifyFormat(
  3748. "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  3749. " aaaaaaaaaaaaaaaaaaaaaa());");
  3750. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3751. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3752. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3753. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
  3754. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3755. verifyFormat("return\n"
  3756. " // true if code is one of a or b.\n"
  3757. " code == a || code == b;");
  3758. }
  3759. TEST_F(FormatTest, AlignsAfterOpenBracket) {
  3760. verifyFormat(
  3761. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
  3762. " aaaaaaaaa aaaaaaa) {}");
  3763. verifyFormat(
  3764. "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
  3765. " aaaaaaaaaaa aaaaaaaaa);");
  3766. verifyFormat(
  3767. "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
  3768. " aaaaaaaaaaaaaaaaaaaaa));");
  3769. FormatStyle Style = getLLVMStyle();
  3770. Style.AlignAfterOpenBracket = false;
  3771. verifyFormat(
  3772. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3773. " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
  3774. Style);
  3775. verifyFormat(
  3776. "SomeLongVariableName->someVeryLongFunctionName(\n"
  3777. " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
  3778. Style);
  3779. verifyFormat(
  3780. "SomeLongVariableName->someFunction(\n"
  3781. " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
  3782. Style);
  3783. verifyFormat(
  3784. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
  3785. " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3786. Style);
  3787. verifyFormat(
  3788. "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
  3789. " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3790. Style);
  3791. verifyFormat(
  3792. "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
  3793. " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
  3794. Style);
  3795. }
  3796. TEST_F(FormatTest, BreaksConditionalExpressions) {
  3797. verifyFormat(
  3798. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3799. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3800. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3801. verifyFormat(
  3802. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3803. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3804. verifyFormat(
  3805. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
  3806. " : aaaaaaaaaaaaa);");
  3807. verifyFormat(
  3808. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3809. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3810. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3811. " aaaaaaaaaaaaa);");
  3812. verifyFormat(
  3813. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3814. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3815. " aaaaaaaaaaaaa);");
  3816. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3817. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3818. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3819. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3820. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3821. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3822. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3823. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3824. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3825. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3826. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3827. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3828. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3829. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3830. " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3831. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3832. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3833. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3834. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3835. " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3836. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  3837. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3838. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3839. " : aaaaaaaaaaaaaaaa;");
  3840. verifyFormat(
  3841. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3842. " ? aaaaaaaaaaaaaaa\n"
  3843. " : aaaaaaaaaaaaaaa;");
  3844. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  3845. " aaaaaaaaa\n"
  3846. " ? b\n"
  3847. " : c);");
  3848. verifyFormat("return aaaa == bbbb\n"
  3849. " // comment\n"
  3850. " ? aaaa\n"
  3851. " : bbbb;");
  3852. verifyFormat(
  3853. "unsigned Indent =\n"
  3854. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
  3855. " ? IndentForLevel[TheLine.Level]\n"
  3856. " : TheLine * 2,\n"
  3857. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  3858. getLLVMStyleWithColumns(70));
  3859. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  3860. " ? aaaaaaaaaaaaaaa\n"
  3861. " : bbbbbbbbbbbbbbb //\n"
  3862. " ? ccccccccccccccc\n"
  3863. " : ddddddddddddddd;");
  3864. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  3865. " ? aaaaaaaaaaaaaaa\n"
  3866. " : (bbbbbbbbbbbbbbb //\n"
  3867. " ? ccccccccccccccc\n"
  3868. " : ddddddddddddddd);");
  3869. verifyFormat(
  3870. "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3871. " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3872. " aaaaaaaaaaaaaaaaaaaaa +\n"
  3873. " aaaaaaaaaaaaaaaaaaaaa\n"
  3874. " : aaaaaaaaaa;");
  3875. verifyFormat(
  3876. "aaaaaa = aaaaaaaaaaaa\n"
  3877. " ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3878. " : aaaaaaaaaaaaaaaaaaaaaa\n"
  3879. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3880. FormatStyle NoBinPacking = getLLVMStyle();
  3881. NoBinPacking.BinPackArguments = false;
  3882. verifyFormat(
  3883. "void f() {\n"
  3884. " g(aaa,\n"
  3885. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  3886. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3887. " ? aaaaaaaaaaaaaaa\n"
  3888. " : aaaaaaaaaaaaaaa);\n"
  3889. "}",
  3890. NoBinPacking);
  3891. verifyFormat(
  3892. "void f() {\n"
  3893. " g(aaa,\n"
  3894. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  3895. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3896. " ?: aaaaaaaaaaaaaaa);\n"
  3897. "}",
  3898. NoBinPacking);
  3899. verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
  3900. " // comment.\n"
  3901. " ccccccccccccccccccccccccccccccccccccccc\n"
  3902. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3903. " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
  3904. // Assignments in conditional expressions. Apparently not uncommon :-(.
  3905. verifyFormat("return a != b\n"
  3906. " // comment\n"
  3907. " ? a = b\n"
  3908. " : a = b;");
  3909. verifyFormat("return a != b\n"
  3910. " // comment\n"
  3911. " ? a = a != b\n"
  3912. " // comment\n"
  3913. " ? a = b\n"
  3914. " : a\n"
  3915. " : a;\n");
  3916. verifyFormat("return a != b\n"
  3917. " // comment\n"
  3918. " ? a\n"
  3919. " : a = a != b\n"
  3920. " // comment\n"
  3921. " ? a = b\n"
  3922. " : a;");
  3923. }
  3924. TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
  3925. FormatStyle Style = getLLVMStyle();
  3926. Style.BreakBeforeTernaryOperators = false;
  3927. Style.ColumnLimit = 70;
  3928. verifyFormat(
  3929. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  3930. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  3931. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3932. Style);
  3933. verifyFormat(
  3934. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  3935. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3936. Style);
  3937. verifyFormat(
  3938. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
  3939. " aaaaaaaaaaaaa);",
  3940. Style);
  3941. verifyFormat(
  3942. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3943. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  3944. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3945. " aaaaaaaaaaaaa);",
  3946. Style);
  3947. verifyFormat(
  3948. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3949. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3950. " aaaaaaaaaaaaa);",
  3951. Style);
  3952. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  3953. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3954. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  3955. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3956. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3957. Style);
  3958. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3959. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  3960. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3961. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  3962. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3963. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3964. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3965. Style);
  3966. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3967. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
  3968. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3969. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3970. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3971. Style);
  3972. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  3973. " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  3974. " aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  3975. Style);
  3976. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  3977. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  3978. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  3979. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  3980. Style);
  3981. verifyFormat(
  3982. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  3983. " aaaaaaaaaaaaaaa :\n"
  3984. " aaaaaaaaaaaaaaa;",
  3985. Style);
  3986. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  3987. " aaaaaaaaa ?\n"
  3988. " b :\n"
  3989. " c);",
  3990. Style);
  3991. verifyFormat(
  3992. "unsigned Indent =\n"
  3993. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n"
  3994. " IndentForLevel[TheLine.Level] :\n"
  3995. " TheLine * 2,\n"
  3996. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  3997. Style);
  3998. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  3999. " aaaaaaaaaaaaaaa :\n"
  4000. " bbbbbbbbbbbbbbb ? //\n"
  4001. " ccccccccccccccc :\n"
  4002. " ddddddddddddddd;",
  4003. Style);
  4004. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  4005. " aaaaaaaaaaaaaaa :\n"
  4006. " (bbbbbbbbbbbbbbb ? //\n"
  4007. " ccccccccccccccc :\n"
  4008. " ddddddddddddddd);",
  4009. Style);
  4010. }
  4011. TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
  4012. verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
  4013. " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
  4014. verifyFormat("bool a = true, b = false;");
  4015. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4016. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
  4017. " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
  4018. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
  4019. verifyFormat(
  4020. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  4021. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
  4022. " d = e && f;");
  4023. verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
  4024. " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
  4025. verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  4026. " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
  4027. verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
  4028. " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
  4029. // FIXME: If multiple variables are defined, the "*" needs to move to the new
  4030. // line. Also fix indent for breaking after the type, this looks bad.
  4031. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
  4032. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
  4033. " * b = bbbbbbbbbbbbbbbbbbb;",
  4034. getGoogleStyle());
  4035. // Not ideal, but pointer-with-type does not allow much here.
  4036. verifyGoogleFormat(
  4037. "aaaaaaaaa* a = aaaaaaaaaaaaaaaaaaa, * b = bbbbbbbbbbbbbbbbbbb,\n"
  4038. " * b = bbbbbbbbbbbbbbbbbbb, * d = ddddddddddddddddddd;");
  4039. }
  4040. TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
  4041. verifyFormat("arr[foo ? bar : baz];");
  4042. verifyFormat("f()[foo ? bar : baz];");
  4043. verifyFormat("(a + b)[foo ? bar : baz];");
  4044. verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
  4045. }
  4046. TEST_F(FormatTest, AlignsStringLiterals) {
  4047. verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
  4048. " \"short literal\");");
  4049. verifyFormat(
  4050. "looooooooooooooooooooooooongFunction(\n"
  4051. " \"short literal\"\n"
  4052. " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
  4053. verifyFormat("someFunction(\"Always break between multi-line\"\n"
  4054. " \" string literals\",\n"
  4055. " and, other, parameters);");
  4056. EXPECT_EQ("fun + \"1243\" /* comment */\n"
  4057. " \"5678\";",
  4058. format("fun + \"1243\" /* comment */\n"
  4059. " \"5678\";",
  4060. getLLVMStyleWithColumns(28)));
  4061. EXPECT_EQ(
  4062. "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  4063. " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
  4064. " \"aaaaaaaaaaaaaaaa\";",
  4065. format("aaaaaa ="
  4066. "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  4067. "aaaaaaaaaaaaaaaaaaaaa\" "
  4068. "\"aaaaaaaaaaaaaaaa\";"));
  4069. verifyFormat("a = a + \"a\"\n"
  4070. " \"a\"\n"
  4071. " \"a\";");
  4072. verifyFormat("f(\"a\", \"b\"\n"
  4073. " \"c\");");
  4074. verifyFormat(
  4075. "#define LL_FORMAT \"ll\"\n"
  4076. "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
  4077. " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
  4078. verifyFormat("#define A(X) \\\n"
  4079. " \"aaaaa\" #X \"bbbbbb\" \\\n"
  4080. " \"ccccc\"",
  4081. getLLVMStyleWithColumns(23));
  4082. verifyFormat("#define A \"def\"\n"
  4083. "f(\"abc\" A \"ghi\"\n"
  4084. " \"jkl\");");
  4085. verifyFormat("f(L\"a\"\n"
  4086. " L\"b\")");
  4087. verifyFormat("#define A(X) \\\n"
  4088. " L\"aaaaa\" #X L\"bbbbbb\" \\\n"
  4089. " L\"ccccc\"",
  4090. getLLVMStyleWithColumns(25));
  4091. }
  4092. TEST_F(FormatTest, AlwaysBreakAfterDefinitionReturnType) {
  4093. FormatStyle AfterType = getLLVMStyle();
  4094. AfterType.AlwaysBreakAfterDefinitionReturnType = true;
  4095. verifyFormat("const char *\n"
  4096. "f(void) {\n" // Break here.
  4097. " return \"\";\n"
  4098. "}\n"
  4099. "const char *bar(void);\n", // No break here.
  4100. AfterType);
  4101. verifyFormat("template <class T>\n"
  4102. "T *\n"
  4103. "f(T &c) {\n" // Break here.
  4104. " return NULL;\n"
  4105. "}\n"
  4106. "template <class T> T *f(T &c);\n", // No break here.
  4107. AfterType);
  4108. AfterType.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  4109. verifyFormat("const char *\n"
  4110. "f(void)\n" // Break here.
  4111. "{\n"
  4112. " return \"\";\n"
  4113. "}\n"
  4114. "const char *bar(void);\n", // No break here.
  4115. AfterType);
  4116. verifyFormat("template <class T>\n"
  4117. "T *\n" // Problem here: no line break
  4118. "f(T &c)\n" // Break here.
  4119. "{\n"
  4120. " return NULL;\n"
  4121. "}\n"
  4122. "template <class T> T *f(T &c);\n", // No break here.
  4123. AfterType);
  4124. }
  4125. TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
  4126. FormatStyle NoBreak = getLLVMStyle();
  4127. NoBreak.AlwaysBreakBeforeMultilineStrings = false;
  4128. FormatStyle Break = getLLVMStyle();
  4129. Break.AlwaysBreakBeforeMultilineStrings = true;
  4130. verifyFormat("aaaa = \"bbbb\"\n"
  4131. " \"cccc\";",
  4132. NoBreak);
  4133. verifyFormat("aaaa =\n"
  4134. " \"bbbb\"\n"
  4135. " \"cccc\";",
  4136. Break);
  4137. verifyFormat("aaaa(\"bbbb\"\n"
  4138. " \"cccc\");",
  4139. NoBreak);
  4140. verifyFormat("aaaa(\n"
  4141. " \"bbbb\"\n"
  4142. " \"cccc\");",
  4143. Break);
  4144. verifyFormat("aaaa(qqq, \"bbbb\"\n"
  4145. " \"cccc\");",
  4146. NoBreak);
  4147. verifyFormat("aaaa(qqq,\n"
  4148. " \"bbbb\"\n"
  4149. " \"cccc\");",
  4150. Break);
  4151. verifyFormat("aaaa(qqq,\n"
  4152. " L\"bbbb\"\n"
  4153. " L\"cccc\");",
  4154. Break);
  4155. // As we break before unary operators, breaking right after them is bad.
  4156. verifyFormat("string foo = abc ? \"x\"\n"
  4157. " \"blah blah blah blah blah blah\"\n"
  4158. " : \"y\";",
  4159. Break);
  4160. // Don't break if there is no column gain.
  4161. verifyFormat("f(\"aaaa\"\n"
  4162. " \"bbbb\");",
  4163. Break);
  4164. // Treat literals with escaped newlines like multi-line string literals.
  4165. EXPECT_EQ("x = \"a\\\n"
  4166. "b\\\n"
  4167. "c\";",
  4168. format("x = \"a\\\n"
  4169. "b\\\n"
  4170. "c\";",
  4171. NoBreak));
  4172. EXPECT_EQ("x =\n"
  4173. " \"a\\\n"
  4174. "b\\\n"
  4175. "c\";",
  4176. format("x = \"a\\\n"
  4177. "b\\\n"
  4178. "c\";",
  4179. Break));
  4180. // Exempt ObjC strings for now.
  4181. EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
  4182. " \"bbbb\";",
  4183. format("NSString *const kString = @\"aaaa\"\n"
  4184. "\"bbbb\";",
  4185. Break));
  4186. }
  4187. TEST_F(FormatTest, AlignsPipes) {
  4188. verifyFormat(
  4189. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4190. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4191. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4192. verifyFormat(
  4193. "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
  4194. " << aaaaaaaaaaaaaaaaaaaa;");
  4195. verifyFormat(
  4196. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4197. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4198. verifyFormat(
  4199. "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
  4200. " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
  4201. " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
  4202. verifyFormat(
  4203. "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4204. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4205. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4206. verifyFormat(
  4207. "llvm::errs() << \"a: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4208. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4209. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4210. verifyFormat(
  4211. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4212. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4213. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4214. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4215. verifyFormat(
  4216. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4217. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4218. verifyFormat("return out << \"somepacket = {\\n\"\n"
  4219. " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
  4220. " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
  4221. " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
  4222. " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
  4223. " << \"}\";");
  4224. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  4225. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  4226. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
  4227. verifyFormat(
  4228. "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
  4229. " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
  4230. " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
  4231. " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
  4232. " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
  4233. verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
  4234. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4235. verifyFormat(
  4236. "void f() {\n"
  4237. " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
  4238. " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  4239. "}");
  4240. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
  4241. " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
  4242. // Breaking before the first "<<" is generally not desirable.
  4243. verifyFormat(
  4244. "llvm::errs()\n"
  4245. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4246. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4247. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4248. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4249. getLLVMStyleWithColumns(70));
  4250. verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4251. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4252. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4253. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4254. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4255. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4256. getLLVMStyleWithColumns(70));
  4257. // But sometimes, breaking before the first "<<" is desirable.
  4258. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
  4259. " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
  4260. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
  4261. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4262. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4263. verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
  4264. " << BEF << IsTemplate << Description << E->getType();");
  4265. verifyFormat(
  4266. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4267. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4268. // Incomplete string literal.
  4269. EXPECT_EQ("llvm::errs() << \"\n"
  4270. " << a;",
  4271. format("llvm::errs() << \"\n<<a;"));
  4272. verifyFormat("void f() {\n"
  4273. " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
  4274. " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
  4275. "}");
  4276. }
  4277. TEST_F(FormatTest, UnderstandsEquals) {
  4278. verifyFormat(
  4279. "aaaaaaaaaaaaaaaaa =\n"
  4280. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4281. verifyFormat(
  4282. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4283. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  4284. verifyFormat(
  4285. "if (a) {\n"
  4286. " f();\n"
  4287. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4288. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  4289. "}");
  4290. verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4291. " 100000000 + 10000000) {\n}");
  4292. }
  4293. TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
  4294. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  4295. " .looooooooooooooooooooooooooooooooooooooongFunction();");
  4296. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  4297. " ->looooooooooooooooooooooooooooooooooooooongFunction();");
  4298. verifyFormat(
  4299. "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
  4300. " Parameter2);");
  4301. verifyFormat(
  4302. "ShortObject->shortFunction(\n"
  4303. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
  4304. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
  4305. verifyFormat("loooooooooooooongFunction(\n"
  4306. " LoooooooooooooongObject->looooooooooooooooongFunction());");
  4307. verifyFormat(
  4308. "function(LoooooooooooooooooooooooooooooooooooongObject\n"
  4309. " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
  4310. verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  4311. " .WillRepeatedly(Return(SomeValue));");
  4312. verifyFormat("void f() {\n"
  4313. " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  4314. " .Times(2)\n"
  4315. " .WillRepeatedly(Return(SomeValue));\n"
  4316. "}");
  4317. verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
  4318. " ccccccccccccccccccccccc);");
  4319. verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4320. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).aaaaa(aaaaa),\n"
  4321. " aaaaaaaaaaaaaaaaaaaaa);");
  4322. verifyFormat("void f() {\n"
  4323. " aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4324. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
  4325. "}");
  4326. verifyFormat(
  4327. "aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4328. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4329. " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4330. " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4331. " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  4332. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4333. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4334. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4335. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
  4336. "}");
  4337. // Here, it is not necessary to wrap at "." or "->".
  4338. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
  4339. " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  4340. verifyFormat(
  4341. "aaaaaaaaaaa->aaaaaaaaa(\n"
  4342. " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4343. " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
  4344. verifyFormat(
  4345. "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4346. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
  4347. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
  4348. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  4349. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
  4350. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  4351. // FIXME: Should we break before .a()?
  4352. verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4353. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).a();");
  4354. FormatStyle NoBinPacking = getLLVMStyle();
  4355. NoBinPacking.BinPackParameters = false;
  4356. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  4357. " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  4358. " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
  4359. " aaaaaaaaaaaaaaaaaaa,\n"
  4360. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4361. NoBinPacking);
  4362. // If there is a subsequent call, change to hanging indentation.
  4363. verifyFormat(
  4364. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4365. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
  4366. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4367. verifyFormat(
  4368. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4369. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
  4370. verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4371. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4372. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4373. verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4374. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4375. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  4376. }
  4377. TEST_F(FormatTest, WrapsTemplateDeclarations) {
  4378. verifyFormat("template <typename T>\n"
  4379. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  4380. verifyFormat("template <typename T>\n"
  4381. "// T should be one of {A, B}.\n"
  4382. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  4383. verifyFormat(
  4384. "template <typename T>\n"
  4385. "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
  4386. verifyFormat("template <typename T>\n"
  4387. "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
  4388. " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
  4389. verifyFormat(
  4390. "template <typename T>\n"
  4391. "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
  4392. " int Paaaaaaaaaaaaaaaaaaaaram2);");
  4393. verifyFormat(
  4394. "template <typename T>\n"
  4395. "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
  4396. " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
  4397. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4398. verifyFormat("template <typename T>\n"
  4399. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4400. " int aaaaaaaaaaaaaaaaaaaaaa);");
  4401. verifyFormat(
  4402. "template <typename T1, typename T2 = char, typename T3 = char,\n"
  4403. " typename T4 = char>\n"
  4404. "void f();");
  4405. verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
  4406. " template <typename> class cccccccccccccccccccccc,\n"
  4407. " typename ddddddddddddd>\n"
  4408. "class C {};");
  4409. verifyFormat(
  4410. "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
  4411. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4412. verifyFormat("void f() {\n"
  4413. " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
  4414. " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
  4415. "}");
  4416. verifyFormat("template <typename T> class C {};");
  4417. verifyFormat("template <typename T> void f();");
  4418. verifyFormat("template <typename T> void f() {}");
  4419. verifyFormat(
  4420. "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  4421. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4422. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
  4423. " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  4424. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4425. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
  4426. " bbbbbbbbbbbbbbbbbbbbbbbb);",
  4427. getLLVMStyleWithColumns(72));
  4428. EXPECT_EQ("static_cast<A< //\n"
  4429. " B> *>(\n"
  4430. "\n"
  4431. " );",
  4432. format("static_cast<A<//\n"
  4433. " B>*>(\n"
  4434. "\n"
  4435. " );"));
  4436. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4437. " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
  4438. FormatStyle AlwaysBreak = getLLVMStyle();
  4439. AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
  4440. verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
  4441. verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
  4442. verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
  4443. verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4444. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
  4445. " ccccccccccccccccccccccccccccccccccccccccccccccc);");
  4446. verifyFormat("template <template <typename> class Fooooooo,\n"
  4447. " template <typename> class Baaaaaaar>\n"
  4448. "struct C {};",
  4449. AlwaysBreak);
  4450. verifyFormat("template <typename T> // T can be A, B or C.\n"
  4451. "struct C {};",
  4452. AlwaysBreak);
  4453. }
  4454. TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
  4455. verifyFormat(
  4456. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4457. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4458. verifyFormat(
  4459. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4460. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4461. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  4462. // FIXME: Should we have the extra indent after the second break?
  4463. verifyFormat(
  4464. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4465. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4466. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4467. verifyFormat(
  4468. "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
  4469. " cccccccccccccccccccccccccccccccccccccccccccccc());");
  4470. // Breaking at nested name specifiers is generally not desirable.
  4471. verifyFormat(
  4472. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4473. " aaaaaaaaaaaaaaaaaaaaaaa);");
  4474. verifyFormat(
  4475. "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4476. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4477. " aaaaaaaaaaaaaaaaaaaaa);",
  4478. getLLVMStyleWithColumns(74));
  4479. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4480. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4481. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4482. }
  4483. TEST_F(FormatTest, UnderstandsTemplateParameters) {
  4484. verifyFormat("A<int> a;");
  4485. verifyFormat("A<A<A<int>>> a;");
  4486. verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
  4487. verifyFormat("bool x = a < 1 || 2 > a;");
  4488. verifyFormat("bool x = 5 < f<int>();");
  4489. verifyFormat("bool x = f<int>() > 5;");
  4490. verifyFormat("bool x = 5 < a<int>::x;");
  4491. verifyFormat("bool x = a < 4 ? a > 2 : false;");
  4492. verifyFormat("bool x = f() ? a < 2 : a > 2;");
  4493. verifyGoogleFormat("A<A<int>> a;");
  4494. verifyGoogleFormat("A<A<A<int>>> a;");
  4495. verifyGoogleFormat("A<A<A<A<int>>>> a;");
  4496. verifyGoogleFormat("A<A<int> > a;");
  4497. verifyGoogleFormat("A<A<A<int> > > a;");
  4498. verifyGoogleFormat("A<A<A<A<int> > > > a;");
  4499. verifyGoogleFormat("A<::A<int>> a;");
  4500. verifyGoogleFormat("A<::A> a;");
  4501. verifyGoogleFormat("A< ::A> a;");
  4502. verifyGoogleFormat("A< ::A<int> > a;");
  4503. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
  4504. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
  4505. EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
  4506. EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
  4507. verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
  4508. verifyFormat("test >> a >> b;");
  4509. verifyFormat("test << a >> b;");
  4510. verifyFormat("f<int>();");
  4511. verifyFormat("template <typename T> void f() {}");
  4512. // Not template parameters.
  4513. verifyFormat("return a < b && c > d;");
  4514. verifyFormat("void f() {\n"
  4515. " while (a < b && c > d) {\n"
  4516. " }\n"
  4517. "}");
  4518. verifyFormat("template <typename... Types>\n"
  4519. "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
  4520. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4521. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
  4522. getLLVMStyleWithColumns(60));
  4523. verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
  4524. verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
  4525. }
  4526. TEST_F(FormatTest, UnderstandsBinaryOperators) {
  4527. verifyFormat("COMPARE(a, ==, b);");
  4528. }
  4529. TEST_F(FormatTest, UnderstandsPointersToMembers) {
  4530. verifyFormat("int A::*x;");
  4531. verifyFormat("int (S::*func)(void *);");
  4532. verifyFormat("void f() { int (S::*func)(void *); }");
  4533. verifyFormat("typedef bool *(Class::*Member)() const;");
  4534. verifyFormat("void f() {\n"
  4535. " (a->*f)();\n"
  4536. " a->*x;\n"
  4537. " (a.*f)();\n"
  4538. " ((*a).*f)();\n"
  4539. " a.*x;\n"
  4540. "}");
  4541. verifyFormat("void f() {\n"
  4542. " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  4543. " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
  4544. "}");
  4545. verifyFormat(
  4546. "(aaaaaaaaaa->*bbbbbbb)(\n"
  4547. " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  4548. FormatStyle Style = getLLVMStyle();
  4549. Style.PointerAlignment = FormatStyle::PAS_Left;
  4550. verifyFormat("typedef bool* (Class::*Member)() const;", Style);
  4551. }
  4552. TEST_F(FormatTest, UnderstandsUnaryOperators) {
  4553. verifyFormat("int a = -2;");
  4554. verifyFormat("f(-1, -2, -3);");
  4555. verifyFormat("a[-1] = 5;");
  4556. verifyFormat("int a = 5 + -2;");
  4557. verifyFormat("if (i == -1) {\n}");
  4558. verifyFormat("if (i != -1) {\n}");
  4559. verifyFormat("if (i > -1) {\n}");
  4560. verifyFormat("if (i < -1) {\n}");
  4561. verifyFormat("++(a->f());");
  4562. verifyFormat("--(a->f());");
  4563. verifyFormat("(a->f())++;");
  4564. verifyFormat("a[42]++;");
  4565. verifyFormat("if (!(a->f())) {\n}");
  4566. verifyFormat("a-- > b;");
  4567. verifyFormat("b ? -a : c;");
  4568. verifyFormat("n * sizeof char16;");
  4569. verifyFormat("n * alignof char16;", getGoogleStyle());
  4570. verifyFormat("sizeof(char);");
  4571. verifyFormat("alignof(char);", getGoogleStyle());
  4572. verifyFormat("return -1;");
  4573. verifyFormat("switch (a) {\n"
  4574. "case -1:\n"
  4575. " break;\n"
  4576. "}");
  4577. verifyFormat("#define X -1");
  4578. verifyFormat("#define X -kConstant");
  4579. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
  4580. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
  4581. verifyFormat("int a = /* confusing comment */ -1;");
  4582. // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
  4583. verifyFormat("int a = i /* confusing comment */++;");
  4584. }
  4585. TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
  4586. verifyFormat("if (!aaaaaaaaaa( // break\n"
  4587. " aaaaa)) {\n"
  4588. "}");
  4589. verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
  4590. " aaaaa));");
  4591. verifyFormat("*aaa = aaaaaaa( // break\n"
  4592. " bbbbbb);");
  4593. }
  4594. TEST_F(FormatTest, UnderstandsOverloadedOperators) {
  4595. verifyFormat("bool operator<();");
  4596. verifyFormat("bool operator>();");
  4597. verifyFormat("bool operator=();");
  4598. verifyFormat("bool operator==();");
  4599. verifyFormat("bool operator!=();");
  4600. verifyFormat("int operator+();");
  4601. verifyFormat("int operator++();");
  4602. verifyFormat("bool operator();");
  4603. verifyFormat("bool operator()();");
  4604. verifyFormat("bool operator[]();");
  4605. verifyFormat("operator bool();");
  4606. verifyFormat("operator int();");
  4607. verifyFormat("operator void *();");
  4608. verifyFormat("operator SomeType<int>();");
  4609. verifyFormat("operator SomeType<int, int>();");
  4610. verifyFormat("operator SomeType<SomeType<int>>();");
  4611. verifyFormat("void *operator new(std::size_t size);");
  4612. verifyFormat("void *operator new[](std::size_t size);");
  4613. verifyFormat("void operator delete(void *ptr);");
  4614. verifyFormat("void operator delete[](void *ptr);");
  4615. verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
  4616. "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
  4617. verifyFormat(
  4618. "ostream &operator<<(ostream &OutputStream,\n"
  4619. " SomeReallyLongType WithSomeReallyLongValue);");
  4620. verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
  4621. " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
  4622. " return left.group < right.group;\n"
  4623. "}");
  4624. verifyFormat("SomeType &operator=(const SomeType &S);");
  4625. verifyFormat("f.template operator()<int>();");
  4626. verifyGoogleFormat("operator void*();");
  4627. verifyGoogleFormat("operator SomeType<SomeType<int>>();");
  4628. verifyGoogleFormat("operator ::A();");
  4629. verifyFormat("using A::operator+;");
  4630. verifyFormat("Deleted &operator=(const Deleted &)& = default;");
  4631. verifyFormat("Deleted &operator=(const Deleted &)&& = delete;");
  4632. verifyGoogleFormat("Deleted& operator=(const Deleted&)& = default;");
  4633. verifyGoogleFormat("Deleted& operator=(const Deleted&)&& = delete;");
  4634. verifyFormat("string // break\n"
  4635. "operator()() & {}");
  4636. verifyFormat("string // break\n"
  4637. "operator()() && {}");
  4638. }
  4639. TEST_F(FormatTest, UnderstandsNewAndDelete) {
  4640. verifyFormat("void f() {\n"
  4641. " A *a = new A;\n"
  4642. " A *a = new (placement) A;\n"
  4643. " delete a;\n"
  4644. " delete (A *)a;\n"
  4645. "}");
  4646. verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
  4647. " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
  4648. verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4649. " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
  4650. " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
  4651. verifyFormat("delete[] h->p;");
  4652. }
  4653. TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
  4654. verifyFormat("int *f(int *a) {}");
  4655. verifyFormat("int main(int argc, char **argv) {}");
  4656. verifyFormat("Test::Test(int b) : a(b * b) {}");
  4657. verifyIndependentOfContext("f(a, *a);");
  4658. verifyFormat("void g() { f(*a); }");
  4659. verifyIndependentOfContext("int a = b * 10;");
  4660. verifyIndependentOfContext("int a = 10 * b;");
  4661. verifyIndependentOfContext("int a = b * c;");
  4662. verifyIndependentOfContext("int a += b * c;");
  4663. verifyIndependentOfContext("int a -= b * c;");
  4664. verifyIndependentOfContext("int a *= b * c;");
  4665. verifyIndependentOfContext("int a /= b * c;");
  4666. verifyIndependentOfContext("int a = *b;");
  4667. verifyIndependentOfContext("int a = *b * c;");
  4668. verifyIndependentOfContext("int a = b * *c;");
  4669. verifyIndependentOfContext("return 10 * b;");
  4670. verifyIndependentOfContext("return *b * *c;");
  4671. verifyIndependentOfContext("return a & ~b;");
  4672. verifyIndependentOfContext("f(b ? *c : *d);");
  4673. verifyIndependentOfContext("int a = b ? *c : *d;");
  4674. verifyIndependentOfContext("*b = a;");
  4675. verifyIndependentOfContext("a * ~b;");
  4676. verifyIndependentOfContext("a * !b;");
  4677. verifyIndependentOfContext("a * +b;");
  4678. verifyIndependentOfContext("a * -b;");
  4679. verifyIndependentOfContext("a * ++b;");
  4680. verifyIndependentOfContext("a * --b;");
  4681. verifyIndependentOfContext("a[4] * b;");
  4682. verifyIndependentOfContext("a[a * a] = 1;");
  4683. verifyIndependentOfContext("f() * b;");
  4684. verifyIndependentOfContext("a * [self dostuff];");
  4685. verifyIndependentOfContext("int x = a * (a + b);");
  4686. verifyIndependentOfContext("(a *)(a + b);");
  4687. verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
  4688. verifyIndependentOfContext("int *pa = (int *)&a;");
  4689. verifyIndependentOfContext("return sizeof(int **);");
  4690. verifyIndependentOfContext("return sizeof(int ******);");
  4691. verifyIndependentOfContext("return (int **&)a;");
  4692. verifyIndependentOfContext("f((*PointerToArray)[10]);");
  4693. verifyFormat("void f(Type (*parameter)[10]) {}");
  4694. verifyGoogleFormat("return sizeof(int**);");
  4695. verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
  4696. verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
  4697. verifyFormat("auto a = [](int **&, int ***) {};");
  4698. verifyFormat("auto PointerBinding = [](const char *S) {};");
  4699. verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
  4700. verifyFormat("[](const decltype(*a) &value) {}");
  4701. verifyIndependentOfContext("typedef void (*f)(int *a);");
  4702. verifyIndependentOfContext("int i{a * b};");
  4703. verifyIndependentOfContext("aaa && aaa->f();");
  4704. verifyIndependentOfContext("int x = ~*p;");
  4705. verifyFormat("Constructor() : a(a), area(width * height) {}");
  4706. verifyFormat("Constructor() : a(a), area(a, width * height) {}");
  4707. verifyFormat("void f() { f(a, c * d); }");
  4708. verifyIndependentOfContext("InvalidRegions[*R] = 0;");
  4709. verifyIndependentOfContext("A<int *> a;");
  4710. verifyIndependentOfContext("A<int **> a;");
  4711. verifyIndependentOfContext("A<int *, int *> a;");
  4712. verifyIndependentOfContext("A<int *[]> a;");
  4713. verifyIndependentOfContext(
  4714. "const char *const p = reinterpret_cast<const char *const>(q);");
  4715. verifyIndependentOfContext("A<int **, int **> a;");
  4716. verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
  4717. verifyFormat("for (char **a = b; *a; ++a) {\n}");
  4718. verifyFormat("for (; a && b;) {\n}");
  4719. verifyFormat("bool foo = true && [] { return false; }();");
  4720. verifyFormat(
  4721. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4722. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4723. verifyGoogleFormat("int main(int argc, char** argv) {}");
  4724. verifyGoogleFormat("A<int*> a;");
  4725. verifyGoogleFormat("A<int**> a;");
  4726. verifyGoogleFormat("A<int*, int*> a;");
  4727. verifyGoogleFormat("A<int**, int**> a;");
  4728. verifyGoogleFormat("f(b ? *c : *d);");
  4729. verifyGoogleFormat("int a = b ? *c : *d;");
  4730. verifyGoogleFormat("Type* t = **x;");
  4731. verifyGoogleFormat("Type* t = *++*x;");
  4732. verifyGoogleFormat("*++*x;");
  4733. verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
  4734. verifyGoogleFormat("Type* t = x++ * y;");
  4735. verifyGoogleFormat(
  4736. "const char* const p = reinterpret_cast<const char* const>(q);");
  4737. verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
  4738. verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
  4739. verifyGoogleFormat("template <typename T>\n"
  4740. "void f(int i = 0, SomeType** temps = NULL);");
  4741. FormatStyle Left = getLLVMStyle();
  4742. Left.PointerAlignment = FormatStyle::PAS_Left;
  4743. verifyFormat("x = *a(x) = *a(y);", Left);
  4744. verifyIndependentOfContext("a = *(x + y);");
  4745. verifyIndependentOfContext("a = &(x + y);");
  4746. verifyIndependentOfContext("*(x + y).call();");
  4747. verifyIndependentOfContext("&(x + y)->call();");
  4748. verifyFormat("void f() { &(*I).first; }");
  4749. verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
  4750. verifyFormat(
  4751. "int *MyValues = {\n"
  4752. " *A, // Operator detection might be confused by the '{'\n"
  4753. " *BB // Operator detection might be confused by previous comment\n"
  4754. "};");
  4755. verifyIndependentOfContext("if (int *a = &b)");
  4756. verifyIndependentOfContext("if (int &a = *b)");
  4757. verifyIndependentOfContext("if (a & b[i])");
  4758. verifyIndependentOfContext("if (a::b::c::d & b[i])");
  4759. verifyIndependentOfContext("if (*b[i])");
  4760. verifyIndependentOfContext("if (int *a = (&b))");
  4761. verifyIndependentOfContext("while (int *a = &b)");
  4762. verifyIndependentOfContext("size = sizeof *a;");
  4763. verifyFormat("void f() {\n"
  4764. " for (const int &v : Values) {\n"
  4765. " }\n"
  4766. "}");
  4767. verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
  4768. verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
  4769. verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
  4770. verifyFormat("#define A (!a * b)");
  4771. verifyFormat("#define MACRO \\\n"
  4772. " int *i = a * b; \\\n"
  4773. " void f(a *b);",
  4774. getLLVMStyleWithColumns(19));
  4775. verifyIndependentOfContext("A = new SomeType *[Length];");
  4776. verifyIndependentOfContext("A = new SomeType *[Length]();");
  4777. verifyIndependentOfContext("T **t = new T *;");
  4778. verifyIndependentOfContext("T **t = new T *();");
  4779. verifyGoogleFormat("A = new SomeType* [Length]();");
  4780. verifyGoogleFormat("A = new SomeType* [Length];");
  4781. verifyGoogleFormat("T** t = new T*;");
  4782. verifyGoogleFormat("T** t = new T*();");
  4783. FormatStyle PointerLeft = getLLVMStyle();
  4784. PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
  4785. verifyFormat("delete *x;", PointerLeft);
  4786. verifyFormat("STATIC_ASSERT((a & b) == 0);");
  4787. verifyFormat("STATIC_ASSERT(0 == (a & b));");
  4788. verifyFormat("template <bool a, bool b> "
  4789. "typename t::if<x && y>::type f() {}");
  4790. verifyFormat("template <int *y> f() {}");
  4791. verifyFormat("vector<int *> v;");
  4792. verifyFormat("vector<int *const> v;");
  4793. verifyFormat("vector<int *const **const *> v;");
  4794. verifyFormat("vector<int *volatile> v;");
  4795. verifyFormat("vector<a * b> v;");
  4796. verifyFormat("foo<b && false>();");
  4797. verifyFormat("foo<b & 1>();");
  4798. verifyFormat("decltype(*::std::declval<const T &>()) void F();");
  4799. verifyFormat(
  4800. "template <class T, class = typename std::enable_if<\n"
  4801. " std::is_integral<T>::value &&\n"
  4802. " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
  4803. "void F();",
  4804. getLLVMStyleWithColumns(76));
  4805. verifyFormat(
  4806. "template <class T,\n"
  4807. " class = typename ::std::enable_if<\n"
  4808. " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
  4809. "void F();",
  4810. getGoogleStyleWithColumns(68));
  4811. verifyIndependentOfContext("MACRO(int *i);");
  4812. verifyIndependentOfContext("MACRO(auto *a);");
  4813. verifyIndependentOfContext("MACRO(const A *a);");
  4814. verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
  4815. // FIXME: Is there a way to make this work?
  4816. // verifyIndependentOfContext("MACRO(A *a);");
  4817. verifyFormat("DatumHandle const *operator->() const { return input_; }");
  4818. EXPECT_EQ("#define OP(x) \\\n"
  4819. " ostream &operator<<(ostream &s, const A &a) { \\\n"
  4820. " return s << a.DebugString(); \\\n"
  4821. " }",
  4822. format("#define OP(x) \\\n"
  4823. " ostream &operator<<(ostream &s, const A &a) { \\\n"
  4824. " return s << a.DebugString(); \\\n"
  4825. " }",
  4826. getLLVMStyleWithColumns(50)));
  4827. // FIXME: We cannot handle this case yet; we might be able to figure out that
  4828. // foo<x> d > v; doesn't make sense.
  4829. verifyFormat("foo<a<b && c> d> v;");
  4830. FormatStyle PointerMiddle = getLLVMStyle();
  4831. PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
  4832. verifyFormat("delete *x;", PointerMiddle);
  4833. verifyFormat("int * x;", PointerMiddle);
  4834. verifyFormat("template <int * y> f() {}", PointerMiddle);
  4835. verifyFormat("int * f(int * a) {}", PointerMiddle);
  4836. verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
  4837. verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
  4838. verifyFormat("A<int *> a;", PointerMiddle);
  4839. verifyFormat("A<int **> a;", PointerMiddle);
  4840. verifyFormat("A<int *, int *> a;", PointerMiddle);
  4841. verifyFormat("A<int * []> a;", PointerMiddle);
  4842. verifyFormat("A = new SomeType * [Length]();", PointerMiddle);
  4843. verifyFormat("A = new SomeType * [Length];", PointerMiddle);
  4844. verifyFormat("T ** t = new T *;", PointerMiddle);
  4845. }
  4846. TEST_F(FormatTest, UnderstandsAttributes) {
  4847. verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
  4848. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
  4849. "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
  4850. }
  4851. TEST_F(FormatTest, UnderstandsEllipsis) {
  4852. verifyFormat("int printf(const char *fmt, ...);");
  4853. verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
  4854. verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
  4855. FormatStyle PointersLeft = getLLVMStyle();
  4856. PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
  4857. verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
  4858. }
  4859. TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
  4860. EXPECT_EQ("int *a;\n"
  4861. "int *a;\n"
  4862. "int *a;",
  4863. format("int *a;\n"
  4864. "int* a;\n"
  4865. "int *a;",
  4866. getGoogleStyle()));
  4867. EXPECT_EQ("int* a;\n"
  4868. "int* a;\n"
  4869. "int* a;",
  4870. format("int* a;\n"
  4871. "int* a;\n"
  4872. "int *a;",
  4873. getGoogleStyle()));
  4874. EXPECT_EQ("int *a;\n"
  4875. "int *a;\n"
  4876. "int *a;",
  4877. format("int *a;\n"
  4878. "int * a;\n"
  4879. "int * a;",
  4880. getGoogleStyle()));
  4881. }
  4882. TEST_F(FormatTest, UnderstandsRvalueReferences) {
  4883. verifyFormat("int f(int &&a) {}");
  4884. verifyFormat("int f(int a, char &&b) {}");
  4885. verifyFormat("void f() { int &&a = b; }");
  4886. verifyGoogleFormat("int f(int a, char&& b) {}");
  4887. verifyGoogleFormat("void f() { int&& a = b; }");
  4888. verifyIndependentOfContext("A<int &&> a;");
  4889. verifyIndependentOfContext("A<int &&, int &&> a;");
  4890. verifyGoogleFormat("A<int&&> a;");
  4891. verifyGoogleFormat("A<int&&, int&&> a;");
  4892. // Not rvalue references:
  4893. verifyFormat("template <bool B, bool C> class A {\n"
  4894. " static_assert(B && C, \"Something is wrong\");\n"
  4895. "};");
  4896. verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
  4897. verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
  4898. verifyFormat("#define A(a, b) (a && b)");
  4899. }
  4900. TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
  4901. verifyFormat("void f() {\n"
  4902. " x[aaaaaaaaa -\n"
  4903. " b] = 23;\n"
  4904. "}",
  4905. getLLVMStyleWithColumns(15));
  4906. }
  4907. TEST_F(FormatTest, FormatsCasts) {
  4908. verifyFormat("Type *A = static_cast<Type *>(P);");
  4909. verifyFormat("Type *A = (Type *)P;");
  4910. verifyFormat("Type *A = (vector<Type *, int *>)P;");
  4911. verifyFormat("int a = (int)(2.0f);");
  4912. verifyFormat("int a = (int)2.0f;");
  4913. verifyFormat("x[(int32)y];");
  4914. verifyFormat("x = (int32)y;");
  4915. verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
  4916. verifyFormat("int a = (int)*b;");
  4917. verifyFormat("int a = (int)2.0f;");
  4918. verifyFormat("int a = (int)~0;");
  4919. verifyFormat("int a = (int)++a;");
  4920. verifyFormat("int a = (int)sizeof(int);");
  4921. verifyFormat("int a = (int)+2;");
  4922. verifyFormat("my_int a = (my_int)2.0f;");
  4923. verifyFormat("my_int a = (my_int)sizeof(int);");
  4924. verifyFormat("return (my_int)aaa;");
  4925. verifyFormat("#define x ((int)-1)");
  4926. verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
  4927. verifyFormat("#define p(q) ((int *)&q)");
  4928. verifyFormat("fn(a)(b) + 1;");
  4929. verifyFormat("void f() { my_int a = (my_int)*b; }");
  4930. verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
  4931. verifyFormat("my_int a = (my_int)~0;");
  4932. verifyFormat("my_int a = (my_int)++a;");
  4933. verifyFormat("my_int a = (my_int)-2;");
  4934. verifyFormat("my_int a = (my_int)1;");
  4935. verifyFormat("my_int a = (my_int *)1;");
  4936. verifyFormat("my_int a = (const my_int)-1;");
  4937. verifyFormat("my_int a = (const my_int *)-1;");
  4938. verifyFormat("my_int a = (my_int)(my_int)-1;");
  4939. // FIXME: single value wrapped with paren will be treated as cast.
  4940. verifyFormat("void f(int i = (kValue)*kMask) {}");
  4941. verifyFormat("{ (void)F; }");
  4942. // Don't break after a cast's
  4943. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4944. " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
  4945. " bbbbbbbbbbbbbbbbbbbbbb);");
  4946. // These are not casts.
  4947. verifyFormat("void f(int *) {}");
  4948. verifyFormat("f(foo)->b;");
  4949. verifyFormat("f(foo).b;");
  4950. verifyFormat("f(foo)(b);");
  4951. verifyFormat("f(foo)[b];");
  4952. verifyFormat("[](foo) { return 4; }(bar);");
  4953. verifyFormat("(*funptr)(foo)[4];");
  4954. verifyFormat("funptrs[4](foo)[4];");
  4955. verifyFormat("void f(int *);");
  4956. verifyFormat("void f(int *) = 0;");
  4957. verifyFormat("void f(SmallVector<int>) {}");
  4958. verifyFormat("void f(SmallVector<int>);");
  4959. verifyFormat("void f(SmallVector<int>) = 0;");
  4960. verifyFormat("void f(int i = (kA * kB) & kMask) {}");
  4961. verifyFormat("int a = sizeof(int) * b;");
  4962. verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
  4963. verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
  4964. verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
  4965. verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
  4966. // These are not casts, but at some point were confused with casts.
  4967. verifyFormat("virtual void foo(int *) override;");
  4968. verifyFormat("virtual void foo(char &) const;");
  4969. verifyFormat("virtual void foo(int *a, char *) const;");
  4970. verifyFormat("int a = sizeof(int *) + b;");
  4971. verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
  4972. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
  4973. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4974. // FIXME: The indentation here is not ideal.
  4975. verifyFormat(
  4976. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4977. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
  4978. " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
  4979. }
  4980. TEST_F(FormatTest, FormatsFunctionTypes) {
  4981. verifyFormat("A<bool()> a;");
  4982. verifyFormat("A<SomeType()> a;");
  4983. verifyFormat("A<void (*)(int, std::string)> a;");
  4984. verifyFormat("A<void *(int)>;");
  4985. verifyFormat("void *(*a)(int *, SomeType *);");
  4986. verifyFormat("int (*func)(void *);");
  4987. verifyFormat("void f() { int (*func)(void *); }");
  4988. verifyFormat("template <class CallbackClass>\n"
  4989. "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
  4990. verifyGoogleFormat("A<void*(int*, SomeType*)>;");
  4991. verifyGoogleFormat("void* (*a)(int);");
  4992. verifyGoogleFormat(
  4993. "template <class CallbackClass>\n"
  4994. "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
  4995. // Other constructs can look somewhat like function types:
  4996. verifyFormat("A<sizeof(*x)> a;");
  4997. verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
  4998. verifyFormat("some_var = function(*some_pointer_var)[0];");
  4999. verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
  5000. }
  5001. TEST_F(FormatTest, BreaksLongVariableDeclarations) {
  5002. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5003. " LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5004. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
  5005. " LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5006. // Different ways of ()-initializiation.
  5007. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5008. " LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
  5009. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5010. " LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
  5011. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5012. " LoooooooooooooooooooooooooooooooooooooooongVariable({});");
  5013. }
  5014. TEST_F(FormatTest, BreaksLongDeclarations) {
  5015. verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
  5016. " AnotherNameForTheLongType;");
  5017. verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
  5018. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  5019. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5020. "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
  5021. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5022. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5023. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
  5024. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5025. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  5026. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5027. verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
  5028. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5029. FormatStyle Indented = getLLVMStyle();
  5030. Indented.IndentWrappedFunctionNames = true;
  5031. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5032. " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
  5033. Indented);
  5034. verifyFormat(
  5035. "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5036. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5037. Indented);
  5038. verifyFormat(
  5039. "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  5040. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5041. Indented);
  5042. verifyFormat(
  5043. "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
  5044. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5045. Indented);
  5046. // FIXME: Without the comment, this breaks after "(".
  5047. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
  5048. " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
  5049. getGoogleStyle());
  5050. verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
  5051. " int LoooooooooooooooooooongParam2) {}");
  5052. verifyFormat(
  5053. "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
  5054. " SourceLocation L, IdentifierIn *II,\n"
  5055. " Type *T) {}");
  5056. verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
  5057. "ReallyReallyLongFunctionName(\n"
  5058. " const std::string &SomeParameter,\n"
  5059. " const SomeType<string, SomeOtherTemplateParameter> &\n"
  5060. " ReallyReallyLongParameterName,\n"
  5061. " const SomeType<string, SomeOtherTemplateParameter> &\n"
  5062. " AnotherLongParameterName) {}");
  5063. verifyFormat("template <typename A>\n"
  5064. "SomeLoooooooooooooooooooooongType<\n"
  5065. " typename some_namespace::SomeOtherType<A>::Type>\n"
  5066. "Function() {}");
  5067. verifyGoogleFormat(
  5068. "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
  5069. " aaaaaaaaaaaaaaaaaaaaaaa;");
  5070. verifyGoogleFormat(
  5071. "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
  5072. " SourceLocation L) {}");
  5073. verifyGoogleFormat(
  5074. "some_namespace::LongReturnType\n"
  5075. "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
  5076. " int first_long_parameter, int second_parameter) {}");
  5077. verifyGoogleFormat("template <typename T>\n"
  5078. "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  5079. "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
  5080. verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5081. " int aaaaaaaaaaaaaaaaaaaaaaa);");
  5082. verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  5083. " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  5084. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5085. }
  5086. TEST_F(FormatTest, FormatsArrays) {
  5087. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5088. " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
  5089. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5090. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  5091. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5092. " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
  5093. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5094. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5095. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  5096. verifyFormat(
  5097. "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
  5098. " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5099. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  5100. verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
  5101. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
  5102. verifyFormat(
  5103. "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
  5104. " .aaaaaaa[0]\n"
  5105. " .aaaaaaaaaaaaaaaaaaaaaa();");
  5106. }
  5107. TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
  5108. verifyFormat("(a)->b();");
  5109. verifyFormat("--a;");
  5110. }
  5111. TEST_F(FormatTest, HandlesIncludeDirectives) {
  5112. verifyFormat("#include <string>\n"
  5113. "#include <a/b/c.h>\n"
  5114. "#include \"a/b/string\"\n"
  5115. "#include \"string.h\"\n"
  5116. "#include \"string.h\"\n"
  5117. "#include <a-a>\n"
  5118. "#include < path with space >\n"
  5119. "#include \"abc.h\" // this is included for ABC\n"
  5120. "#include \"some long include\" // with a comment\n"
  5121. "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
  5122. getLLVMStyleWithColumns(35));
  5123. EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\""));
  5124. EXPECT_EQ("#include <a>", format("#include<a>"));
  5125. verifyFormat("#import <string>");
  5126. verifyFormat("#import <a/b/c.h>");
  5127. verifyFormat("#import \"a/b/string\"");
  5128. verifyFormat("#import \"string.h\"");
  5129. verifyFormat("#import \"string.h\"");
  5130. verifyFormat("#if __has_include(<strstream>)\n"
  5131. "#include <strstream>\n"
  5132. "#endif");
  5133. verifyFormat("#define MY_IMPORT <a/b>");
  5134. // Protocol buffer definition or missing "#".
  5135. verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
  5136. getLLVMStyleWithColumns(30));
  5137. FormatStyle Style = getLLVMStyle();
  5138. Style.AlwaysBreakBeforeMultilineStrings = true;
  5139. Style.ColumnLimit = 0;
  5140. verifyFormat("#import \"abc.h\"", Style);
  5141. }
  5142. //===----------------------------------------------------------------------===//
  5143. // Error recovery tests.
  5144. //===----------------------------------------------------------------------===//
  5145. TEST_F(FormatTest, IncompleteParameterLists) {
  5146. FormatStyle NoBinPacking = getLLVMStyle();
  5147. NoBinPacking.BinPackParameters = false;
  5148. verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
  5149. " double *min_x,\n"
  5150. " double *max_x,\n"
  5151. " double *min_y,\n"
  5152. " double *max_y,\n"
  5153. " double *min_z,\n"
  5154. " double *max_z, ) {}",
  5155. NoBinPacking);
  5156. }
  5157. TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
  5158. verifyFormat("void f() { return; }\n42");
  5159. verifyFormat("void f() {\n"
  5160. " if (0)\n"
  5161. " return;\n"
  5162. "}\n"
  5163. "42");
  5164. verifyFormat("void f() { return }\n42");
  5165. verifyFormat("void f() {\n"
  5166. " if (0)\n"
  5167. " return\n"
  5168. "}\n"
  5169. "42");
  5170. }
  5171. TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
  5172. EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
  5173. EXPECT_EQ("void f() {\n"
  5174. " if (a)\n"
  5175. " return\n"
  5176. "}",
  5177. format("void f ( ) { if ( a ) return }"));
  5178. EXPECT_EQ("namespace N {\n"
  5179. "void f()\n"
  5180. "}",
  5181. format("namespace N { void f() }"));
  5182. EXPECT_EQ("namespace N {\n"
  5183. "void f() {}\n"
  5184. "void g()\n"
  5185. "}",
  5186. format("namespace N { void f( ) { } void g( ) }"));
  5187. }
  5188. TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
  5189. verifyFormat("int aaaaaaaa =\n"
  5190. " // Overlylongcomment\n"
  5191. " b;",
  5192. getLLVMStyleWithColumns(20));
  5193. verifyFormat("function(\n"
  5194. " ShortArgument,\n"
  5195. " LoooooooooooongArgument);\n",
  5196. getLLVMStyleWithColumns(20));
  5197. }
  5198. TEST_F(FormatTest, IncorrectAccessSpecifier) {
  5199. verifyFormat("public:");
  5200. verifyFormat("class A {\n"
  5201. "public\n"
  5202. " void f() {}\n"
  5203. "};");
  5204. verifyFormat("public\n"
  5205. "int qwerty;");
  5206. verifyFormat("public\n"
  5207. "B {}");
  5208. verifyFormat("public\n"
  5209. "{}");
  5210. verifyFormat("public\n"
  5211. "B { int x; }");
  5212. }
  5213. TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
  5214. verifyFormat("{");
  5215. verifyFormat("#})");
  5216. }
  5217. TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
  5218. verifyFormat("do {\n}");
  5219. verifyFormat("do {\n}\n"
  5220. "f();");
  5221. verifyFormat("do {\n}\n"
  5222. "wheeee(fun);");
  5223. verifyFormat("do {\n"
  5224. " f();\n"
  5225. "}");
  5226. }
  5227. TEST_F(FormatTest, IncorrectCodeMissingParens) {
  5228. verifyFormat("if {\n foo;\n foo();\n}");
  5229. verifyFormat("switch {\n foo;\n foo();\n}");
  5230. verifyFormat("for {\n foo;\n foo();\n}");
  5231. verifyFormat("while {\n foo;\n foo();\n}");
  5232. verifyFormat("do {\n foo;\n foo();\n} while;");
  5233. }
  5234. TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
  5235. verifyFormat("namespace {\n"
  5236. "class Foo { Foo (\n"
  5237. "};\n"
  5238. "} // comment");
  5239. }
  5240. TEST_F(FormatTest, IncorrectCodeErrorDetection) {
  5241. EXPECT_EQ("{\n {}\n", format("{\n{\n}\n"));
  5242. EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
  5243. EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
  5244. EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n"));
  5245. EXPECT_EQ("{\n"
  5246. " {\n"
  5247. " breakme(\n"
  5248. " qwe);\n"
  5249. " }\n",
  5250. format("{\n"
  5251. " {\n"
  5252. " breakme(qwe);\n"
  5253. "}\n",
  5254. getLLVMStyleWithColumns(10)));
  5255. }
  5256. TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
  5257. verifyFormat("int x = {\n"
  5258. " avariable,\n"
  5259. " b(alongervariable)};",
  5260. getLLVMStyleWithColumns(25));
  5261. }
  5262. TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
  5263. verifyFormat("return (a)(b){1, 2, 3};");
  5264. }
  5265. TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
  5266. verifyFormat("vector<int> x{1, 2, 3, 4};");
  5267. verifyFormat("vector<int> x{\n"
  5268. " 1, 2, 3, 4,\n"
  5269. "};");
  5270. verifyFormat("vector<T> x{{}, {}, {}, {}};");
  5271. verifyFormat("f({1, 2});");
  5272. verifyFormat("auto v = Foo{-1};");
  5273. verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
  5274. verifyFormat("Class::Class : member{1, 2, 3} {}");
  5275. verifyFormat("new vector<int>{1, 2, 3};");
  5276. verifyFormat("new int[3]{1, 2, 3};");
  5277. verifyFormat("new int{1};");
  5278. verifyFormat("return {arg1, arg2};");
  5279. verifyFormat("return {arg1, SomeType{parameter}};");
  5280. verifyFormat("int count = set<int>{f(), g(), h()}.size();");
  5281. verifyFormat("new T{arg1, arg2};");
  5282. verifyFormat("f(MyMap[{composite, key}]);");
  5283. verifyFormat("class Class {\n"
  5284. " T member = {arg1, arg2};\n"
  5285. "};");
  5286. verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
  5287. verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
  5288. verifyFormat("int a = std::is_integral<int>{} + 0;");
  5289. verifyFormat("int foo(int i) { return fo1{}(i); }");
  5290. verifyFormat("int foo(int i) { return fo1{}(i); }");
  5291. verifyFormat("auto i = decltype(x){};");
  5292. verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
  5293. verifyFormat("Node n{1, Node{1000}, //\n"
  5294. " 2};");
  5295. // In combination with BinPackParameters = false.
  5296. FormatStyle NoBinPacking = getLLVMStyle();
  5297. NoBinPacking.BinPackParameters = false;
  5298. verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
  5299. " bbbbb,\n"
  5300. " ccccc,\n"
  5301. " ddddd,\n"
  5302. " eeeee,\n"
  5303. " ffffff,\n"
  5304. " ggggg,\n"
  5305. " hhhhhh,\n"
  5306. " iiiiii,\n"
  5307. " jjjjjj,\n"
  5308. " kkkkkk};",
  5309. NoBinPacking);
  5310. verifyFormat("const Aaaaaa aaaaa = {\n"
  5311. " aaaaa,\n"
  5312. " bbbbb,\n"
  5313. " ccccc,\n"
  5314. " ddddd,\n"
  5315. " eeeee,\n"
  5316. " ffffff,\n"
  5317. " ggggg,\n"
  5318. " hhhhhh,\n"
  5319. " iiiiii,\n"
  5320. " jjjjjj,\n"
  5321. " kkkkkk,\n"
  5322. "};",
  5323. NoBinPacking);
  5324. verifyFormat(
  5325. "const Aaaaaa aaaaa = {\n"
  5326. " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n"
  5327. " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n"
  5328. " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
  5329. "};",
  5330. NoBinPacking);
  5331. // FIXME: The alignment of these trailing comments might be bad. Then again,
  5332. // this might be utterly useless in real code.
  5333. verifyFormat("Constructor::Constructor()\n"
  5334. " : some_value{ //\n"
  5335. " aaaaaaa, //\n"
  5336. " bbbbbbb} {}");
  5337. // In braced lists, the first comment is always assumed to belong to the
  5338. // first element. Thus, it can be moved to the next or previous line as
  5339. // appropriate.
  5340. EXPECT_EQ("function({// First element:\n"
  5341. " 1,\n"
  5342. " // Second element:\n"
  5343. " 2});",
  5344. format("function({\n"
  5345. " // First element:\n"
  5346. " 1,\n"
  5347. " // Second element:\n"
  5348. " 2});"));
  5349. EXPECT_EQ("std::vector<int> MyNumbers{\n"
  5350. " // First element:\n"
  5351. " 1,\n"
  5352. " // Second element:\n"
  5353. " 2};",
  5354. format("std::vector<int> MyNumbers{// First element:\n"
  5355. " 1,\n"
  5356. " // Second element:\n"
  5357. " 2};",
  5358. getLLVMStyleWithColumns(30)));
  5359. // A trailing comma should still lead to an enforced line break.
  5360. EXPECT_EQ("vector<int> SomeVector = {\n"
  5361. " // aaa\n"
  5362. " 1, 2,\n"
  5363. "};",
  5364. format("vector<int> SomeVector = { // aaa\n"
  5365. " 1, 2, };"));
  5366. FormatStyle ExtraSpaces = getLLVMStyle();
  5367. ExtraSpaces.Cpp11BracedListStyle = false;
  5368. ExtraSpaces.ColumnLimit = 75;
  5369. verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
  5370. verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
  5371. verifyFormat("f({ 1, 2 });", ExtraSpaces);
  5372. verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
  5373. verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
  5374. verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
  5375. verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
  5376. verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
  5377. verifyFormat("return { arg1, arg2 };", ExtraSpaces);
  5378. verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
  5379. verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
  5380. verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
  5381. verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
  5382. verifyFormat("class Class {\n"
  5383. " T member = { arg1, arg2 };\n"
  5384. "};",
  5385. ExtraSpaces);
  5386. verifyFormat(
  5387. "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5388. " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
  5389. " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  5390. " bbbbbbbbbbbbbbbbbbbb, bbbbb };",
  5391. ExtraSpaces);
  5392. verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
  5393. verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
  5394. ExtraSpaces);
  5395. verifyFormat(
  5396. "someFunction(OtherParam,\n"
  5397. " BracedList{ // comment 1 (Forcing interesting break)\n"
  5398. " param1, param2,\n"
  5399. " // comment 2\n"
  5400. " param3, param4\n"
  5401. " });",
  5402. ExtraSpaces);
  5403. verifyFormat(
  5404. "std::this_thread::sleep_for(\n"
  5405. " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
  5406. ExtraSpaces);
  5407. verifyFormat(
  5408. "std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
  5409. " aaaaaaa, aaaaaaaaaa, aaaaa, aaaaaaaaaaaaaaa, aaa, aaaaaaaaaa, a,\n"
  5410. " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaa,\n"
  5411. " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa, aaaaaaa, a};");
  5412. verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
  5413. }
  5414. TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
  5415. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5416. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5417. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5418. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5419. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5420. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  5421. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5422. " // line comment\n"
  5423. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5424. " 1, 22, 333, 4444, 55555,\n"
  5425. " // line comment\n"
  5426. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5427. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  5428. verifyFormat(
  5429. "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5430. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5431. " 1, 22, 333, 4444, 55555, 666666, // comment\n"
  5432. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  5433. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  5434. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  5435. " 7777777};");
  5436. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  5437. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  5438. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  5439. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  5440. " 1, 1, 1, 1};",
  5441. getLLVMStyleWithColumns(39));
  5442. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  5443. " 1, 1, 1, 1};",
  5444. getLLVMStyleWithColumns(38));
  5445. verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
  5446. " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
  5447. getLLVMStyleWithColumns(43));
  5448. // Trailing commas.
  5449. verifyFormat("vector<int> x = {\n"
  5450. " 1, 1, 1, 1, 1, 1, 1, 1,\n"
  5451. "};",
  5452. getLLVMStyleWithColumns(39));
  5453. verifyFormat("vector<int> x = {\n"
  5454. " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
  5455. "};",
  5456. getLLVMStyleWithColumns(39));
  5457. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  5458. " 1, 1, 1, 1,\n"
  5459. " /**/ /**/};",
  5460. getLLVMStyleWithColumns(39));
  5461. verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
  5462. " {aaaaaaaaaaaaaaaaaaa},\n"
  5463. " {aaaaaaaaaaaaaaaaaaaaa},\n"
  5464. " {aaaaaaaaaaaaaaaaa}};",
  5465. getLLVMStyleWithColumns(60));
  5466. // With nested lists, we should either format one item per line or all nested
  5467. // lists one one line.
  5468. // FIXME: For some nested lists, we can do better.
  5469. verifyFormat(
  5470. "SomeStruct my_struct_array = {\n"
  5471. " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
  5472. " aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
  5473. " {aaa, aaa},\n"
  5474. " {aaa, aaa},\n"
  5475. " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
  5476. " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5477. " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
  5478. // No column layout should be used here.
  5479. verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
  5480. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
  5481. }
  5482. TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
  5483. FormatStyle DoNotMerge = getLLVMStyle();
  5484. DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  5485. verifyFormat("void f() { return 42; }");
  5486. verifyFormat("void f() {\n"
  5487. " return 42;\n"
  5488. "}",
  5489. DoNotMerge);
  5490. verifyFormat("void f() {\n"
  5491. " // Comment\n"
  5492. "}");
  5493. verifyFormat("{\n"
  5494. "#error {\n"
  5495. " int a;\n"
  5496. "}");
  5497. verifyFormat("{\n"
  5498. " int a;\n"
  5499. "#error {\n"
  5500. "}");
  5501. verifyFormat("void f() {} // comment");
  5502. verifyFormat("void f() { int a; } // comment");
  5503. verifyFormat("void f() {\n"
  5504. "} // comment",
  5505. DoNotMerge);
  5506. verifyFormat("void f() {\n"
  5507. " int a;\n"
  5508. "} // comment",
  5509. DoNotMerge);
  5510. verifyFormat("void f() {\n"
  5511. "} // comment",
  5512. getLLVMStyleWithColumns(15));
  5513. verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
  5514. verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
  5515. verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
  5516. verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
  5517. verifyFormat("class C {\n"
  5518. " C()\n"
  5519. " : iiiiiiii(nullptr),\n"
  5520. " kkkkkkk(nullptr),\n"
  5521. " mmmmmmm(nullptr),\n"
  5522. " nnnnnnn(nullptr) {}\n"
  5523. "};",
  5524. getGoogleStyle());
  5525. FormatStyle NoColumnLimit = getLLVMStyle();
  5526. NoColumnLimit.ColumnLimit = 0;
  5527. EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
  5528. EXPECT_EQ("class C {\n"
  5529. " A() : b(0) {}\n"
  5530. "};", format("class C{A():b(0){}};", NoColumnLimit));
  5531. EXPECT_EQ("A()\n"
  5532. " : b(0) {\n"
  5533. "}",
  5534. format("A()\n:b(0)\n{\n}", NoColumnLimit));
  5535. FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
  5536. DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
  5537. FormatStyle::SFS_None;
  5538. EXPECT_EQ("A()\n"
  5539. " : b(0) {\n"
  5540. "}",
  5541. format("A():b(0){}", DoNotMergeNoColumnLimit));
  5542. EXPECT_EQ("A()\n"
  5543. " : b(0) {\n"
  5544. "}",
  5545. format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
  5546. verifyFormat("#define A \\\n"
  5547. " void f() { \\\n"
  5548. " int i; \\\n"
  5549. " }",
  5550. getLLVMStyleWithColumns(20));
  5551. verifyFormat("#define A \\\n"
  5552. " void f() { int i; }",
  5553. getLLVMStyleWithColumns(21));
  5554. verifyFormat("#define A \\\n"
  5555. " void f() { \\\n"
  5556. " int i; \\\n"
  5557. " } \\\n"
  5558. " int j;",
  5559. getLLVMStyleWithColumns(22));
  5560. verifyFormat("#define A \\\n"
  5561. " void f() { int i; } \\\n"
  5562. " int j;",
  5563. getLLVMStyleWithColumns(23));
  5564. }
  5565. TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
  5566. FormatStyle MergeInlineOnly = getLLVMStyle();
  5567. MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  5568. verifyFormat("class C {\n"
  5569. " int f() { return 42; }\n"
  5570. "};",
  5571. MergeInlineOnly);
  5572. verifyFormat("int f() {\n"
  5573. " return 42;\n"
  5574. "}",
  5575. MergeInlineOnly);
  5576. }
  5577. TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
  5578. // Elaborate type variable declarations.
  5579. verifyFormat("struct foo a = {bar};\nint n;");
  5580. verifyFormat("class foo a = {bar};\nint n;");
  5581. verifyFormat("union foo a = {bar};\nint n;");
  5582. // Elaborate types inside function definitions.
  5583. verifyFormat("struct foo f() {}\nint n;");
  5584. verifyFormat("class foo f() {}\nint n;");
  5585. verifyFormat("union foo f() {}\nint n;");
  5586. // Templates.
  5587. verifyFormat("template <class X> void f() {}\nint n;");
  5588. verifyFormat("template <struct X> void f() {}\nint n;");
  5589. verifyFormat("template <union X> void f() {}\nint n;");
  5590. // Actual definitions...
  5591. verifyFormat("struct {\n} n;");
  5592. verifyFormat(
  5593. "template <template <class T, class Y>, class Z> class X {\n} n;");
  5594. verifyFormat("union Z {\n int n;\n} x;");
  5595. verifyFormat("class MACRO Z {\n} n;");
  5596. verifyFormat("class MACRO(X) Z {\n} n;");
  5597. verifyFormat("class __attribute__(X) Z {\n} n;");
  5598. verifyFormat("class __declspec(X) Z {\n} n;");
  5599. verifyFormat("class A##B##C {\n} n;");
  5600. verifyFormat("class alignas(16) Z {\n} n;");
  5601. // Redefinition from nested context:
  5602. verifyFormat("class A::B::C {\n} n;");
  5603. // Template definitions.
  5604. verifyFormat(
  5605. "template <typename F>\n"
  5606. "Matcher(const Matcher<F> &Other,\n"
  5607. " typename enable_if_c<is_base_of<F, T>::value &&\n"
  5608. " !is_same<F, T>::value>::type * = 0)\n"
  5609. " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
  5610. // FIXME: This is still incorrectly handled at the formatter side.
  5611. verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
  5612. // FIXME:
  5613. // This now gets parsed incorrectly as class definition.
  5614. // verifyFormat("class A<int> f() {\n}\nint n;");
  5615. // Elaborate types where incorrectly parsing the structural element would
  5616. // break the indent.
  5617. verifyFormat("if (true)\n"
  5618. " class X x;\n"
  5619. "else\n"
  5620. " f();\n");
  5621. // This is simply incomplete. Formatting is not important, but must not crash.
  5622. verifyFormat("class A:");
  5623. }
  5624. TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
  5625. EXPECT_EQ("#error Leave all white!!!!! space* alone!\n",
  5626. format("#error Leave all white!!!!! space* alone!\n"));
  5627. EXPECT_EQ(
  5628. "#warning Leave all white!!!!! space* alone!\n",
  5629. format("#warning Leave all white!!!!! space* alone!\n"));
  5630. EXPECT_EQ("#error 1", format(" # error 1"));
  5631. EXPECT_EQ("#warning 1", format(" # warning 1"));
  5632. }
  5633. TEST_F(FormatTest, FormatHashIfExpressions) {
  5634. verifyFormat("#if AAAA && BBBB");
  5635. // FIXME: Come up with a better indentation for #elif.
  5636. verifyFormat(
  5637. "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
  5638. " defined(BBBBBBBB)\n"
  5639. "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
  5640. " defined(BBBBBBBB)\n"
  5641. "#endif",
  5642. getLLVMStyleWithColumns(65));
  5643. }
  5644. TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
  5645. FormatStyle AllowsMergedIf = getGoogleStyle();
  5646. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  5647. verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
  5648. verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
  5649. verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
  5650. EXPECT_EQ("if (true) return 42;",
  5651. format("if (true)\nreturn 42;", AllowsMergedIf));
  5652. FormatStyle ShortMergedIf = AllowsMergedIf;
  5653. ShortMergedIf.ColumnLimit = 25;
  5654. verifyFormat("#define A \\\n"
  5655. " if (true) return 42;",
  5656. ShortMergedIf);
  5657. verifyFormat("#define A \\\n"
  5658. " f(); \\\n"
  5659. " if (true)\n"
  5660. "#define B",
  5661. ShortMergedIf);
  5662. verifyFormat("#define A \\\n"
  5663. " f(); \\\n"
  5664. " if (true)\n"
  5665. "g();",
  5666. ShortMergedIf);
  5667. verifyFormat("{\n"
  5668. "#ifdef A\n"
  5669. " // Comment\n"
  5670. " if (true) continue;\n"
  5671. "#endif\n"
  5672. " // Comment\n"
  5673. " if (true) continue;\n"
  5674. "}",
  5675. ShortMergedIf);
  5676. ShortMergedIf.ColumnLimit = 29;
  5677. verifyFormat("#define A \\\n"
  5678. " if (aaaaaaaaaa) return 1; \\\n"
  5679. " return 2;",
  5680. ShortMergedIf);
  5681. ShortMergedIf.ColumnLimit = 28;
  5682. verifyFormat("#define A \\\n"
  5683. " if (aaaaaaaaaa) \\\n"
  5684. " return 1; \\\n"
  5685. " return 2;",
  5686. ShortMergedIf);
  5687. }
  5688. TEST_F(FormatTest, BlockCommentsInControlLoops) {
  5689. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  5690. " f();\n"
  5691. "}");
  5692. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  5693. " f();\n"
  5694. "} /* another comment */ else /* comment #3 */ {\n"
  5695. " g();\n"
  5696. "}");
  5697. verifyFormat("while (0) /* a comment in a strange place */ {\n"
  5698. " f();\n"
  5699. "}");
  5700. verifyFormat("for (;;) /* a comment in a strange place */ {\n"
  5701. " f();\n"
  5702. "}");
  5703. verifyFormat("do /* a comment in a strange place */ {\n"
  5704. " f();\n"
  5705. "} /* another comment */ while (0);");
  5706. }
  5707. TEST_F(FormatTest, BlockComments) {
  5708. EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
  5709. format("/* *//* */ /* */\n/* *//* */ /* */"));
  5710. EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
  5711. EXPECT_EQ("#define A /*123*/ \\\n"
  5712. " b\n"
  5713. "/* */\n"
  5714. "someCall(\n"
  5715. " parameter);",
  5716. format("#define A /*123*/ b\n"
  5717. "/* */\n"
  5718. "someCall(parameter);",
  5719. getLLVMStyleWithColumns(15)));
  5720. EXPECT_EQ("#define A\n"
  5721. "/* */ someCall(\n"
  5722. " parameter);",
  5723. format("#define A\n"
  5724. "/* */someCall(parameter);",
  5725. getLLVMStyleWithColumns(15)));
  5726. EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
  5727. EXPECT_EQ("/*\n"
  5728. "*\n"
  5729. " * aaaaaa\n"
  5730. "*aaaaaa\n"
  5731. "*/",
  5732. format("/*\n"
  5733. "*\n"
  5734. " * aaaaaa aaaaaa\n"
  5735. "*/",
  5736. getLLVMStyleWithColumns(10)));
  5737. EXPECT_EQ("/*\n"
  5738. "**\n"
  5739. "* aaaaaa\n"
  5740. "*aaaaaa\n"
  5741. "*/",
  5742. format("/*\n"
  5743. "**\n"
  5744. "* aaaaaa aaaaaa\n"
  5745. "*/",
  5746. getLLVMStyleWithColumns(10)));
  5747. FormatStyle NoBinPacking = getLLVMStyle();
  5748. NoBinPacking.BinPackParameters = false;
  5749. EXPECT_EQ("someFunction(1, /* comment 1 */\n"
  5750. " 2, /* comment 2 */\n"
  5751. " 3, /* comment 3 */\n"
  5752. " aaaa,\n"
  5753. " bbbb);",
  5754. format("someFunction (1, /* comment 1 */\n"
  5755. " 2, /* comment 2 */ \n"
  5756. " 3, /* comment 3 */\n"
  5757. "aaaa, bbbb );",
  5758. NoBinPacking));
  5759. verifyFormat(
  5760. "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  5761. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  5762. EXPECT_EQ(
  5763. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  5764. " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  5765. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
  5766. format(
  5767. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  5768. " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  5769. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
  5770. EXPECT_EQ(
  5771. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  5772. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  5773. "int cccccccccccccccccccccccccccccc; /* comment */\n",
  5774. format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  5775. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  5776. "int cccccccccccccccccccccccccccccc; /* comment */\n"));
  5777. verifyFormat("void f(int * /* unused */) {}");
  5778. EXPECT_EQ("/*\n"
  5779. " **\n"
  5780. " */",
  5781. format("/*\n"
  5782. " **\n"
  5783. " */"));
  5784. EXPECT_EQ("/*\n"
  5785. " *q\n"
  5786. " */",
  5787. format("/*\n"
  5788. " *q\n"
  5789. " */"));
  5790. EXPECT_EQ("/*\n"
  5791. " * q\n"
  5792. " */",
  5793. format("/*\n"
  5794. " * q\n"
  5795. " */"));
  5796. EXPECT_EQ("/*\n"
  5797. " **/",
  5798. format("/*\n"
  5799. " **/"));
  5800. EXPECT_EQ("/*\n"
  5801. " ***/",
  5802. format("/*\n"
  5803. " ***/"));
  5804. }
  5805. TEST_F(FormatTest, BlockCommentsInMacros) {
  5806. EXPECT_EQ("#define A \\\n"
  5807. " { \\\n"
  5808. " /* one line */ \\\n"
  5809. " someCall();",
  5810. format("#define A { \\\n"
  5811. " /* one line */ \\\n"
  5812. " someCall();",
  5813. getLLVMStyleWithColumns(20)));
  5814. EXPECT_EQ("#define A \\\n"
  5815. " { \\\n"
  5816. " /* previous */ \\\n"
  5817. " /* one line */ \\\n"
  5818. " someCall();",
  5819. format("#define A { \\\n"
  5820. " /* previous */ \\\n"
  5821. " /* one line */ \\\n"
  5822. " someCall();",
  5823. getLLVMStyleWithColumns(20)));
  5824. }
  5825. TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
  5826. EXPECT_EQ("a = {\n"
  5827. " 1111 /* */\n"
  5828. "};",
  5829. format("a = {1111 /* */\n"
  5830. "};",
  5831. getLLVMStyleWithColumns(15)));
  5832. EXPECT_EQ("a = {\n"
  5833. " 1111 /* */\n"
  5834. "};",
  5835. format("a = {1111 /* */\n"
  5836. "};",
  5837. getLLVMStyleWithColumns(15)));
  5838. // FIXME: The formatting is still wrong here.
  5839. EXPECT_EQ("a = {\n"
  5840. " 1111 /* a\n"
  5841. " */\n"
  5842. "};",
  5843. format("a = {1111 /* a */\n"
  5844. "};",
  5845. getLLVMStyleWithColumns(15)));
  5846. }
  5847. TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
  5848. // FIXME: This is not what we want...
  5849. verifyFormat("{\n"
  5850. "// a"
  5851. "// b");
  5852. }
  5853. TEST_F(FormatTest, FormatStarDependingOnContext) {
  5854. verifyFormat("void f(int *a);");
  5855. verifyFormat("void f() { f(fint * b); }");
  5856. verifyFormat("class A {\n void f(int *a);\n};");
  5857. verifyFormat("class A {\n int *a;\n};");
  5858. verifyFormat("namespace a {\n"
  5859. "namespace b {\n"
  5860. "class A {\n"
  5861. " void f() {}\n"
  5862. " int *a;\n"
  5863. "};\n"
  5864. "}\n"
  5865. "}");
  5866. }
  5867. TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
  5868. verifyFormat("while");
  5869. verifyFormat("operator");
  5870. }
  5871. //===----------------------------------------------------------------------===//
  5872. // Objective-C tests.
  5873. //===----------------------------------------------------------------------===//
  5874. TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
  5875. verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
  5876. EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
  5877. format("-(NSUInteger)indexOfObject:(id)anObject;"));
  5878. EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
  5879. EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
  5880. EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
  5881. format("-(NSInteger)Method3:(id)anObject;"));
  5882. EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
  5883. format("-(NSInteger)Method4:(id)anObject;"));
  5884. EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
  5885. format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
  5886. EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
  5887. format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
  5888. EXPECT_EQ(
  5889. "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
  5890. format(
  5891. "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
  5892. // Very long objectiveC method declaration.
  5893. verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
  5894. " inRange:(NSRange)range\n"
  5895. " outRange:(NSRange)out_range\n"
  5896. " outRange1:(NSRange)out_range1\n"
  5897. " outRange2:(NSRange)out_range2\n"
  5898. " outRange3:(NSRange)out_range3\n"
  5899. " outRange4:(NSRange)out_range4\n"
  5900. " outRange5:(NSRange)out_range5\n"
  5901. " outRange6:(NSRange)out_range6\n"
  5902. " outRange7:(NSRange)out_range7\n"
  5903. " outRange8:(NSRange)out_range8\n"
  5904. " outRange9:(NSRange)out_range9;");
  5905. verifyFormat("- (int)sum:(vector<int>)numbers;");
  5906. verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
  5907. // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
  5908. // protocol lists (but not for template classes):
  5909. //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
  5910. verifyFormat("- (int (*)())foo:(int (*)())f;");
  5911. verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
  5912. // If there's no return type (very rare in practice!), LLVM and Google style
  5913. // agree.
  5914. verifyFormat("- foo;");
  5915. verifyFormat("- foo:(int)f;");
  5916. verifyGoogleFormat("- foo:(int)foo;");
  5917. }
  5918. TEST_F(FormatTest, FormatObjCInterface) {
  5919. verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
  5920. "@public\n"
  5921. " int field1;\n"
  5922. "@protected\n"
  5923. " int field2;\n"
  5924. "@private\n"
  5925. " int field3;\n"
  5926. "@package\n"
  5927. " int field4;\n"
  5928. "}\n"
  5929. "+ (id)init;\n"
  5930. "@end");
  5931. verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
  5932. " @public\n"
  5933. " int field1;\n"
  5934. " @protected\n"
  5935. " int field2;\n"
  5936. " @private\n"
  5937. " int field3;\n"
  5938. " @package\n"
  5939. " int field4;\n"
  5940. "}\n"
  5941. "+ (id)init;\n"
  5942. "@end");
  5943. verifyFormat("@interface /* wait for it */ Foo\n"
  5944. "+ (id)init;\n"
  5945. "// Look, a comment!\n"
  5946. "- (int)answerWith:(int)i;\n"
  5947. "@end");
  5948. verifyFormat("@interface Foo\n"
  5949. "@end\n"
  5950. "@interface Bar\n"
  5951. "@end");
  5952. verifyFormat("@interface Foo : Bar\n"
  5953. "+ (id)init;\n"
  5954. "@end");
  5955. verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
  5956. "+ (id)init;\n"
  5957. "@end");
  5958. verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
  5959. "+ (id)init;\n"
  5960. "@end");
  5961. verifyFormat("@interface Foo (HackStuff)\n"
  5962. "+ (id)init;\n"
  5963. "@end");
  5964. verifyFormat("@interface Foo ()\n"
  5965. "+ (id)init;\n"
  5966. "@end");
  5967. verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
  5968. "+ (id)init;\n"
  5969. "@end");
  5970. verifyGoogleFormat("@interface Foo (HackStuff) <MyProtocol>\n"
  5971. "+ (id)init;\n"
  5972. "@end");
  5973. verifyFormat("@interface Foo {\n"
  5974. " int _i;\n"
  5975. "}\n"
  5976. "+ (id)init;\n"
  5977. "@end");
  5978. verifyFormat("@interface Foo : Bar {\n"
  5979. " int _i;\n"
  5980. "}\n"
  5981. "+ (id)init;\n"
  5982. "@end");
  5983. verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
  5984. " int _i;\n"
  5985. "}\n"
  5986. "+ (id)init;\n"
  5987. "@end");
  5988. verifyFormat("@interface Foo (HackStuff) {\n"
  5989. " int _i;\n"
  5990. "}\n"
  5991. "+ (id)init;\n"
  5992. "@end");
  5993. verifyFormat("@interface Foo () {\n"
  5994. " int _i;\n"
  5995. "}\n"
  5996. "+ (id)init;\n"
  5997. "@end");
  5998. verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
  5999. " int _i;\n"
  6000. "}\n"
  6001. "+ (id)init;\n"
  6002. "@end");
  6003. FormatStyle OnePerLine = getGoogleStyle();
  6004. OnePerLine.BinPackParameters = false;
  6005. verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa () <\n"
  6006. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6007. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6008. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6009. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
  6010. "}",
  6011. OnePerLine);
  6012. }
  6013. TEST_F(FormatTest, FormatObjCImplementation) {
  6014. verifyFormat("@implementation Foo : NSObject {\n"
  6015. "@public\n"
  6016. " int field1;\n"
  6017. "@protected\n"
  6018. " int field2;\n"
  6019. "@private\n"
  6020. " int field3;\n"
  6021. "@package\n"
  6022. " int field4;\n"
  6023. "}\n"
  6024. "+ (id)init {\n}\n"
  6025. "@end");
  6026. verifyGoogleFormat("@implementation Foo : NSObject {\n"
  6027. " @public\n"
  6028. " int field1;\n"
  6029. " @protected\n"
  6030. " int field2;\n"
  6031. " @private\n"
  6032. " int field3;\n"
  6033. " @package\n"
  6034. " int field4;\n"
  6035. "}\n"
  6036. "+ (id)init {\n}\n"
  6037. "@end");
  6038. verifyFormat("@implementation Foo\n"
  6039. "+ (id)init {\n"
  6040. " if (true)\n"
  6041. " return nil;\n"
  6042. "}\n"
  6043. "// Look, a comment!\n"
  6044. "- (int)answerWith:(int)i {\n"
  6045. " return i;\n"
  6046. "}\n"
  6047. "+ (int)answerWith:(int)i {\n"
  6048. " return i;\n"
  6049. "}\n"
  6050. "@end");
  6051. verifyFormat("@implementation Foo\n"
  6052. "@end\n"
  6053. "@implementation Bar\n"
  6054. "@end");
  6055. EXPECT_EQ("@implementation Foo : Bar\n"
  6056. "+ (id)init {\n}\n"
  6057. "- (void)foo {\n}\n"
  6058. "@end",
  6059. format("@implementation Foo : Bar\n"
  6060. "+(id)init{}\n"
  6061. "-(void)foo{}\n"
  6062. "@end"));
  6063. verifyFormat("@implementation Foo {\n"
  6064. " int _i;\n"
  6065. "}\n"
  6066. "+ (id)init {\n}\n"
  6067. "@end");
  6068. verifyFormat("@implementation Foo : Bar {\n"
  6069. " int _i;\n"
  6070. "}\n"
  6071. "+ (id)init {\n}\n"
  6072. "@end");
  6073. verifyFormat("@implementation Foo (HackStuff)\n"
  6074. "+ (id)init {\n}\n"
  6075. "@end");
  6076. verifyFormat("@implementation ObjcClass\n"
  6077. "- (void)method;\n"
  6078. "{}\n"
  6079. "@end");
  6080. }
  6081. TEST_F(FormatTest, FormatObjCProtocol) {
  6082. verifyFormat("@protocol Foo\n"
  6083. "@property(weak) id delegate;\n"
  6084. "- (NSUInteger)numberOfThings;\n"
  6085. "@end");
  6086. verifyFormat("@protocol MyProtocol <NSObject>\n"
  6087. "- (NSUInteger)numberOfThings;\n"
  6088. "@end");
  6089. verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
  6090. "- (NSUInteger)numberOfThings;\n"
  6091. "@end");
  6092. verifyFormat("@protocol Foo;\n"
  6093. "@protocol Bar;\n");
  6094. verifyFormat("@protocol Foo\n"
  6095. "@end\n"
  6096. "@protocol Bar\n"
  6097. "@end");
  6098. verifyFormat("@protocol myProtocol\n"
  6099. "- (void)mandatoryWithInt:(int)i;\n"
  6100. "@optional\n"
  6101. "- (void)optional;\n"
  6102. "@required\n"
  6103. "- (void)required;\n"
  6104. "@optional\n"
  6105. "@property(assign) int madProp;\n"
  6106. "@end\n");
  6107. verifyFormat("@property(nonatomic, assign, readonly)\n"
  6108. " int *looooooooooooooooooooooooooooongNumber;\n"
  6109. "@property(nonatomic, assign, readonly)\n"
  6110. " NSString *looooooooooooooooooooooooooooongName;");
  6111. verifyFormat("@implementation PR18406\n"
  6112. "}\n"
  6113. "@end");
  6114. }
  6115. TEST_F(FormatTest, FormatObjCMethodDeclarations) {
  6116. verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
  6117. " rect:(NSRect)theRect\n"
  6118. " interval:(float)theInterval {\n"
  6119. "}");
  6120. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6121. " longKeyword:(NSRect)theRect\n"
  6122. " evenLongerKeyword:(float)theInterval\n"
  6123. " error:(NSError **)theError {\n"
  6124. "}");
  6125. verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
  6126. " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
  6127. " NS_DESIGNATED_INITIALIZER;",
  6128. getLLVMStyleWithColumns(60));
  6129. }
  6130. TEST_F(FormatTest, FormatObjCMethodExpr) {
  6131. verifyFormat("[foo bar:baz];");
  6132. verifyFormat("return [foo bar:baz];");
  6133. verifyFormat("return (a)[foo bar:baz];");
  6134. verifyFormat("f([foo bar:baz]);");
  6135. verifyFormat("f(2, [foo bar:baz]);");
  6136. verifyFormat("f(2, a ? b : c);");
  6137. verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
  6138. // Unary operators.
  6139. verifyFormat("int a = +[foo bar:baz];");
  6140. verifyFormat("int a = -[foo bar:baz];");
  6141. verifyFormat("int a = ![foo bar:baz];");
  6142. verifyFormat("int a = ~[foo bar:baz];");
  6143. verifyFormat("int a = ++[foo bar:baz];");
  6144. verifyFormat("int a = --[foo bar:baz];");
  6145. verifyFormat("int a = sizeof [foo bar:baz];");
  6146. verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
  6147. verifyFormat("int a = &[foo bar:baz];");
  6148. verifyFormat("int a = *[foo bar:baz];");
  6149. // FIXME: Make casts work, without breaking f()[4].
  6150. //verifyFormat("int a = (int)[foo bar:baz];");
  6151. //verifyFormat("return (int)[foo bar:baz];");
  6152. //verifyFormat("(void)[foo bar:baz];");
  6153. verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
  6154. // Binary operators.
  6155. verifyFormat("[foo bar:baz], [foo bar:baz];");
  6156. verifyFormat("[foo bar:baz] = [foo bar:baz];");
  6157. verifyFormat("[foo bar:baz] *= [foo bar:baz];");
  6158. verifyFormat("[foo bar:baz] /= [foo bar:baz];");
  6159. verifyFormat("[foo bar:baz] %= [foo bar:baz];");
  6160. verifyFormat("[foo bar:baz] += [foo bar:baz];");
  6161. verifyFormat("[foo bar:baz] -= [foo bar:baz];");
  6162. verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
  6163. verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
  6164. verifyFormat("[foo bar:baz] &= [foo bar:baz];");
  6165. verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
  6166. verifyFormat("[foo bar:baz] |= [foo bar:baz];");
  6167. verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
  6168. verifyFormat("[foo bar:baz] || [foo bar:baz];");
  6169. verifyFormat("[foo bar:baz] && [foo bar:baz];");
  6170. verifyFormat("[foo bar:baz] | [foo bar:baz];");
  6171. verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
  6172. verifyFormat("[foo bar:baz] & [foo bar:baz];");
  6173. verifyFormat("[foo bar:baz] == [foo bar:baz];");
  6174. verifyFormat("[foo bar:baz] != [foo bar:baz];");
  6175. verifyFormat("[foo bar:baz] >= [foo bar:baz];");
  6176. verifyFormat("[foo bar:baz] <= [foo bar:baz];");
  6177. verifyFormat("[foo bar:baz] > [foo bar:baz];");
  6178. verifyFormat("[foo bar:baz] < [foo bar:baz];");
  6179. verifyFormat("[foo bar:baz] >> [foo bar:baz];");
  6180. verifyFormat("[foo bar:baz] << [foo bar:baz];");
  6181. verifyFormat("[foo bar:baz] - [foo bar:baz];");
  6182. verifyFormat("[foo bar:baz] + [foo bar:baz];");
  6183. verifyFormat("[foo bar:baz] * [foo bar:baz];");
  6184. verifyFormat("[foo bar:baz] / [foo bar:baz];");
  6185. verifyFormat("[foo bar:baz] % [foo bar:baz];");
  6186. // Whew!
  6187. verifyFormat("return in[42];");
  6188. verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
  6189. "}");
  6190. verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
  6191. verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
  6192. verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
  6193. verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
  6194. verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
  6195. verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
  6196. verifyFormat("[button setAction:@selector(zoomOut:)];");
  6197. verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
  6198. verifyFormat("arr[[self indexForFoo:a]];");
  6199. verifyFormat("throw [self errorFor:a];");
  6200. verifyFormat("@throw [self errorFor:a];");
  6201. verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
  6202. verifyFormat("[(id)foo bar:(id) ? baz : quux];");
  6203. verifyFormat("4 > 4 ? (id)a : (id)baz;");
  6204. // This tests that the formatter doesn't break after "backing" but before ":",
  6205. // which would be at 80 columns.
  6206. verifyFormat(
  6207. "void f() {\n"
  6208. " if ((self = [super initWithContentRect:contentRect\n"
  6209. " styleMask:styleMask ?: otherMask\n"
  6210. " backing:NSBackingStoreBuffered\n"
  6211. " defer:YES]))");
  6212. verifyFormat(
  6213. "[foo checkThatBreakingAfterColonWorksOk:\n"
  6214. " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
  6215. verifyFormat("[myObj short:arg1 // Force line break\n"
  6216. " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
  6217. " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
  6218. " error:arg4];");
  6219. verifyFormat(
  6220. "void f() {\n"
  6221. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  6222. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  6223. " pos.width(), pos.height())\n"
  6224. " styleMask:NSBorderlessWindowMask\n"
  6225. " backing:NSBackingStoreBuffered\n"
  6226. " defer:NO]);\n"
  6227. "}");
  6228. verifyFormat(
  6229. "void f() {\n"
  6230. " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
  6231. " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
  6232. " pos.width(), pos.height())\n"
  6233. " syeMask:NSBorderlessWindowMask\n"
  6234. " bking:NSBackingStoreBuffered\n"
  6235. " der:NO]);\n"
  6236. "}",
  6237. getLLVMStyleWithColumns(70));
  6238. verifyFormat(
  6239. "void f() {\n"
  6240. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  6241. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  6242. " pos.width(), pos.height())\n"
  6243. " styleMask:NSBorderlessWindowMask\n"
  6244. " backing:NSBackingStoreBuffered\n"
  6245. " defer:NO]);\n"
  6246. "}",
  6247. getChromiumStyle(FormatStyle::LK_Cpp));
  6248. verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
  6249. " with:contentsNativeView];");
  6250. verifyFormat(
  6251. "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
  6252. " owner:nillllll];");
  6253. verifyFormat(
  6254. "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
  6255. " forType:kBookmarkButtonDragType];");
  6256. verifyFormat("[defaultCenter addObserver:self\n"
  6257. " selector:@selector(willEnterFullscreen)\n"
  6258. " name:kWillEnterFullscreenNotification\n"
  6259. " object:nil];");
  6260. verifyFormat("[image_rep drawInRect:drawRect\n"
  6261. " fromRect:NSZeroRect\n"
  6262. " operation:NSCompositeCopy\n"
  6263. " fraction:1.0\n"
  6264. " respectFlipped:NO\n"
  6265. " hints:nil];");
  6266. verifyFormat(
  6267. "scoped_nsobject<NSTextField> message(\n"
  6268. " // The frame will be fixed up when |-setMessageText:| is called.\n"
  6269. " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
  6270. verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
  6271. " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
  6272. " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
  6273. " aaaa:bbb];");
  6274. verifyFormat("[self param:function( //\n"
  6275. " parameter)]");
  6276. verifyFormat(
  6277. "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  6278. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  6279. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
  6280. // Variadic parameters.
  6281. verifyFormat(
  6282. "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
  6283. verifyFormat(
  6284. "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  6285. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  6286. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
  6287. verifyFormat("[self // break\n"
  6288. " a:a\n"
  6289. " aaa:aaa];");
  6290. verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
  6291. " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
  6292. }
  6293. TEST_F(FormatTest, ObjCAt) {
  6294. verifyFormat("@autoreleasepool");
  6295. verifyFormat("@catch");
  6296. verifyFormat("@class");
  6297. verifyFormat("@compatibility_alias");
  6298. verifyFormat("@defs");
  6299. verifyFormat("@dynamic");
  6300. verifyFormat("@encode");
  6301. verifyFormat("@end");
  6302. verifyFormat("@finally");
  6303. verifyFormat("@implementation");
  6304. verifyFormat("@import");
  6305. verifyFormat("@interface");
  6306. verifyFormat("@optional");
  6307. verifyFormat("@package");
  6308. verifyFormat("@private");
  6309. verifyFormat("@property");
  6310. verifyFormat("@protected");
  6311. verifyFormat("@protocol");
  6312. verifyFormat("@public");
  6313. verifyFormat("@required");
  6314. verifyFormat("@selector");
  6315. verifyFormat("@synchronized");
  6316. verifyFormat("@synthesize");
  6317. verifyFormat("@throw");
  6318. verifyFormat("@try");
  6319. EXPECT_EQ("@interface", format("@ interface"));
  6320. // The precise formatting of this doesn't matter, nobody writes code like
  6321. // this.
  6322. verifyFormat("@ /*foo*/ interface");
  6323. }
  6324. TEST_F(FormatTest, ObjCSnippets) {
  6325. verifyFormat("@autoreleasepool {\n"
  6326. " foo();\n"
  6327. "}");
  6328. verifyFormat("@class Foo, Bar;");
  6329. verifyFormat("@compatibility_alias AliasName ExistingClass;");
  6330. verifyFormat("@dynamic textColor;");
  6331. verifyFormat("char *buf1 = @encode(int *);");
  6332. verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
  6333. verifyFormat("char *buf1 = @encode(int **);");
  6334. verifyFormat("Protocol *proto = @protocol(p1);");
  6335. verifyFormat("SEL s = @selector(foo:);");
  6336. verifyFormat("@synchronized(self) {\n"
  6337. " f();\n"
  6338. "}");
  6339. verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  6340. verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  6341. verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
  6342. verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
  6343. verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
  6344. verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
  6345. getMozillaStyle());
  6346. verifyFormat("@property BOOL editable;", getMozillaStyle());
  6347. verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
  6348. getWebKitStyle());
  6349. verifyFormat("@property BOOL editable;", getWebKitStyle());
  6350. verifyFormat("@import foo.bar;\n"
  6351. "@import baz;");
  6352. }
  6353. TEST_F(FormatTest, ObjCLiterals) {
  6354. verifyFormat("@\"String\"");
  6355. verifyFormat("@1");
  6356. verifyFormat("@+4.8");
  6357. verifyFormat("@-4");
  6358. verifyFormat("@1LL");
  6359. verifyFormat("@.5");
  6360. verifyFormat("@'c'");
  6361. verifyFormat("@true");
  6362. verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
  6363. verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
  6364. verifyFormat("NSNumber *favoriteColor = @(Green);");
  6365. verifyFormat("NSString *path = @(getenv(\"PATH\"));");
  6366. verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
  6367. }
  6368. TEST_F(FormatTest, ObjCDictLiterals) {
  6369. verifyFormat("@{");
  6370. verifyFormat("@{}");
  6371. verifyFormat("@{@\"one\" : @1}");
  6372. verifyFormat("return @{@\"one\" : @1;");
  6373. verifyFormat("@{@\"one\" : @1}");
  6374. verifyFormat("@{@\"one\" : @{@2 : @1}}");
  6375. verifyFormat("@{\n"
  6376. " @\"one\" : @{@2 : @1},\n"
  6377. "}");
  6378. verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
  6379. verifyFormat("[self setDict:@{}");
  6380. verifyFormat("[self setDict:@{@1 : @2}");
  6381. verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
  6382. verifyFormat(
  6383. "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
  6384. verifyFormat(
  6385. "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
  6386. verifyFormat(
  6387. "NSDictionary *d = @{\n"
  6388. " @\"nam\" : NSUserNam(),\n"
  6389. " @\"dte\" : [NSDate date],\n"
  6390. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  6391. "};");
  6392. verifyFormat(
  6393. "@{\n"
  6394. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  6395. "regularFont,\n"
  6396. "};");
  6397. verifyGoogleFormat(
  6398. "@{\n"
  6399. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  6400. "regularFont,\n"
  6401. "};");
  6402. verifyFormat(
  6403. "@{\n"
  6404. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
  6405. " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
  6406. "};");
  6407. // We should try to be robust in case someone forgets the "@".
  6408. verifyFormat(
  6409. "NSDictionary *d = {\n"
  6410. " @\"nam\" : NSUserNam(),\n"
  6411. " @\"dte\" : [NSDate date],\n"
  6412. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  6413. "};");
  6414. verifyFormat("NSMutableDictionary *dictionary =\n"
  6415. " [NSMutableDictionary dictionaryWithDictionary:@{\n"
  6416. " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
  6417. " bbbbbbbbbbbbbbbbbb : bbbbb,\n"
  6418. " cccccccccccccccc : ccccccccccccccc\n"
  6419. " }];");
  6420. }
  6421. TEST_F(FormatTest, ObjCArrayLiterals) {
  6422. verifyFormat("@[");
  6423. verifyFormat("@[]");
  6424. verifyFormat(
  6425. "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
  6426. verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
  6427. verifyFormat("NSArray *array = @[ [foo description] ];");
  6428. verifyFormat(
  6429. "NSArray *some_variable = @[\n"
  6430. " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
  6431. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6432. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6433. " @\"aaaaaaaaaaaaaaaaa\"\n"
  6434. "];");
  6435. verifyFormat("NSArray *some_variable = @[\n"
  6436. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6437. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6438. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6439. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6440. "];");
  6441. verifyGoogleFormat("NSArray *some_variable = @[\n"
  6442. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6443. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6444. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6445. " @\"aaaaaaaaaaaaaaaaa\"\n"
  6446. "];");
  6447. // We should try to be robust in case someone forgets the "@".
  6448. verifyFormat("NSArray *some_variable = [\n"
  6449. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6450. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6451. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6452. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6453. "];");
  6454. verifyFormat(
  6455. "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
  6456. " index:(NSUInteger)index\n"
  6457. " nonDigitAttributes:\n"
  6458. " (NSDictionary *)noDigitAttributes;");
  6459. verifyFormat(
  6460. "[someFunction someLooooooooooooongParameter:\n"
  6461. " @[ NSBundle.mainBundle.infoDictionary[@\"a\"] ]];");
  6462. }
  6463. TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
  6464. EXPECT_EQ("{\n"
  6465. "{\n"
  6466. "a;\n"
  6467. "b;\n"
  6468. "}\n"
  6469. "}",
  6470. format("{\n"
  6471. "{\n"
  6472. "a;\n"
  6473. " b;\n"
  6474. "}\n"
  6475. "}",
  6476. 13, 2, getLLVMStyle()));
  6477. EXPECT_EQ("{\n"
  6478. "{\n"
  6479. " a;\n"
  6480. "b;\n"
  6481. "}\n"
  6482. "}",
  6483. format("{\n"
  6484. "{\n"
  6485. " a;\n"
  6486. "b;\n"
  6487. "}\n"
  6488. "}",
  6489. 9, 2, getLLVMStyle()));
  6490. EXPECT_EQ("{\n"
  6491. "{\n"
  6492. "public:\n"
  6493. " b;\n"
  6494. "}\n"
  6495. "}",
  6496. format("{\n"
  6497. "{\n"
  6498. "public:\n"
  6499. " b;\n"
  6500. "}\n"
  6501. "}",
  6502. 17, 2, getLLVMStyle()));
  6503. EXPECT_EQ("{\n"
  6504. "{\n"
  6505. "a;\n"
  6506. "}\n"
  6507. "{\n"
  6508. " b; //\n"
  6509. "}\n"
  6510. "}",
  6511. format("{\n"
  6512. "{\n"
  6513. "a;\n"
  6514. "}\n"
  6515. "{\n"
  6516. " b; //\n"
  6517. "}\n"
  6518. "}",
  6519. 22, 2, getLLVMStyle()));
  6520. EXPECT_EQ(" {\n"
  6521. " a; //\n"
  6522. " }",
  6523. format(" {\n"
  6524. "a; //\n"
  6525. " }",
  6526. 4, 2, getLLVMStyle()));
  6527. EXPECT_EQ("void f() {}\n"
  6528. "void g() {}",
  6529. format("void f() {}\n"
  6530. "void g() {}",
  6531. 13, 0, getLLVMStyle()));
  6532. EXPECT_EQ("int a; // comment\n"
  6533. " // line 2\n"
  6534. "int b;",
  6535. format("int a; // comment\n"
  6536. " // line 2\n"
  6537. " int b;",
  6538. 35, 0, getLLVMStyle()));
  6539. EXPECT_EQ(" int a;\n"
  6540. " void\n"
  6541. " ffffff() {\n"
  6542. " }",
  6543. format(" int a;\n"
  6544. "void ffffff() {}",
  6545. 11, 0, getLLVMStyleWithColumns(11)));
  6546. EXPECT_EQ(" void f() {\n"
  6547. "#define A 1\n"
  6548. " }",
  6549. format(" void f() {\n"
  6550. " #define A 1\n" // Format this line.
  6551. " }",
  6552. 20, 0, getLLVMStyle()));
  6553. EXPECT_EQ(" void f() {\n"
  6554. " int i;\n"
  6555. "#define A \\\n"
  6556. " int i; \\\n"
  6557. " int j;\n"
  6558. " int k;\n"
  6559. " }",
  6560. format(" void f() {\n"
  6561. " int i;\n"
  6562. "#define A \\\n"
  6563. " int i; \\\n"
  6564. " int j;\n"
  6565. " int k;\n" // Format this line.
  6566. " }",
  6567. 67, 0, getLLVMStyle()));
  6568. }
  6569. TEST_F(FormatTest, BreaksStringLiterals) {
  6570. EXPECT_EQ("\"some text \"\n"
  6571. "\"other\";",
  6572. format("\"some text other\";", getLLVMStyleWithColumns(12)));
  6573. EXPECT_EQ("\"some text \"\n"
  6574. "\"other\";",
  6575. format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
  6576. EXPECT_EQ(
  6577. "#define A \\\n"
  6578. " \"some \" \\\n"
  6579. " \"text \" \\\n"
  6580. " \"other\";",
  6581. format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
  6582. EXPECT_EQ(
  6583. "#define A \\\n"
  6584. " \"so \" \\\n"
  6585. " \"text \" \\\n"
  6586. " \"other\";",
  6587. format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
  6588. EXPECT_EQ("\"some text\"",
  6589. format("\"some text\"", getLLVMStyleWithColumns(1)));
  6590. EXPECT_EQ("\"some text\"",
  6591. format("\"some text\"", getLLVMStyleWithColumns(11)));
  6592. EXPECT_EQ("\"some \"\n"
  6593. "\"text\"",
  6594. format("\"some text\"", getLLVMStyleWithColumns(10)));
  6595. EXPECT_EQ("\"some \"\n"
  6596. "\"text\"",
  6597. format("\"some text\"", getLLVMStyleWithColumns(7)));
  6598. EXPECT_EQ("\"some\"\n"
  6599. "\" tex\"\n"
  6600. "\"t\"",
  6601. format("\"some text\"", getLLVMStyleWithColumns(6)));
  6602. EXPECT_EQ("\"some\"\n"
  6603. "\" tex\"\n"
  6604. "\" and\"",
  6605. format("\"some tex and\"", getLLVMStyleWithColumns(6)));
  6606. EXPECT_EQ("\"some\"\n"
  6607. "\"/tex\"\n"
  6608. "\"/and\"",
  6609. format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
  6610. EXPECT_EQ("variable =\n"
  6611. " \"long string \"\n"
  6612. " \"literal\";",
  6613. format("variable = \"long string literal\";",
  6614. getLLVMStyleWithColumns(20)));
  6615. EXPECT_EQ("variable = f(\n"
  6616. " \"long string \"\n"
  6617. " \"literal\",\n"
  6618. " short,\n"
  6619. " loooooooooooooooooooong);",
  6620. format("variable = f(\"long string literal\", short, "
  6621. "loooooooooooooooooooong);",
  6622. getLLVMStyleWithColumns(20)));
  6623. EXPECT_EQ("f(g(\"long string \"\n"
  6624. " \"literal\"),\n"
  6625. " b);",
  6626. format("f(g(\"long string literal\"), b);",
  6627. getLLVMStyleWithColumns(20)));
  6628. EXPECT_EQ("f(g(\"long string \"\n"
  6629. " \"literal\",\n"
  6630. " a),\n"
  6631. " b);",
  6632. format("f(g(\"long string literal\", a), b);",
  6633. getLLVMStyleWithColumns(20)));
  6634. EXPECT_EQ(
  6635. "f(\"one two\".split(\n"
  6636. " variable));",
  6637. format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
  6638. EXPECT_EQ("f(\"one two three four five six \"\n"
  6639. " \"seven\".split(\n"
  6640. " really_looooong_variable));",
  6641. format("f(\"one two three four five six seven\"."
  6642. "split(really_looooong_variable));",
  6643. getLLVMStyleWithColumns(33)));
  6644. EXPECT_EQ("f(\"some \"\n"
  6645. " \"text\",\n"
  6646. " other);",
  6647. format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
  6648. // Only break as a last resort.
  6649. verifyFormat(
  6650. "aaaaaaaaaaaaaaaaaaaa(\n"
  6651. " aaaaaaaaaaaaaaaaaaaa,\n"
  6652. " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
  6653. EXPECT_EQ(
  6654. "\"splitmea\"\n"
  6655. "\"trandomp\"\n"
  6656. "\"oint\"",
  6657. format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
  6658. EXPECT_EQ(
  6659. "\"split/\"\n"
  6660. "\"pathat/\"\n"
  6661. "\"slashes\"",
  6662. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  6663. EXPECT_EQ(
  6664. "\"split/\"\n"
  6665. "\"pathat/\"\n"
  6666. "\"slashes\"",
  6667. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  6668. EXPECT_EQ("\"split at \"\n"
  6669. "\"spaces/at/\"\n"
  6670. "\"slashes.at.any$\"\n"
  6671. "\"non-alphanumeric%\"\n"
  6672. "\"1111111111characte\"\n"
  6673. "\"rs\"",
  6674. format("\"split at "
  6675. "spaces/at/"
  6676. "slashes.at."
  6677. "any$non-"
  6678. "alphanumeric%"
  6679. "1111111111characte"
  6680. "rs\"",
  6681. getLLVMStyleWithColumns(20)));
  6682. // Verify that splitting the strings understands
  6683. // Style::AlwaysBreakBeforeMultilineStrings.
  6684. EXPECT_EQ("aaaaaaaaaaaa(aaaaaaaaaaaaa,\n"
  6685. " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
  6686. " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
  6687. format("aaaaaaaaaaaa(aaaaaaaaaaaaa, \"aaaaaaaaaaaaaaaaaaaaaa "
  6688. "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa "
  6689. "aaaaaaaaaaaaaaaaaaaaaa\");",
  6690. getGoogleStyle()));
  6691. EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  6692. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
  6693. format("return \"aaaaaaaaaaaaaaaaaaaaaa "
  6694. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
  6695. "aaaaaaaaaaaaaaaaaaaaaa\";",
  6696. getGoogleStyle()));
  6697. EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  6698. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  6699. format("llvm::outs() << "
  6700. "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
  6701. "aaaaaaaaaaaaaaaaaaa\";"));
  6702. EXPECT_EQ("ffff(\n"
  6703. " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  6704. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  6705. format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  6706. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  6707. getGoogleStyle()));
  6708. FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
  6709. AlignLeft.AlignEscapedNewlinesLeft = true;
  6710. EXPECT_EQ(
  6711. "#define A \\\n"
  6712. " \"some \" \\\n"
  6713. " \"text \" \\\n"
  6714. " \"other\";",
  6715. format("#define A \"some text other\";", AlignLeft));
  6716. }
  6717. TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
  6718. EXPECT_EQ(
  6719. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  6720. "(\n"
  6721. " \"x\t\");",
  6722. format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  6723. "aaaaaaa("
  6724. "\"x\t\");"));
  6725. }
  6726. TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
  6727. EXPECT_EQ(
  6728. "u8\"utf8 string \"\n"
  6729. "u8\"literal\";",
  6730. format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
  6731. EXPECT_EQ(
  6732. "u\"utf16 string \"\n"
  6733. "u\"literal\";",
  6734. format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
  6735. EXPECT_EQ(
  6736. "U\"utf32 string \"\n"
  6737. "U\"literal\";",
  6738. format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
  6739. EXPECT_EQ("L\"wide string \"\n"
  6740. "L\"literal\";",
  6741. format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
  6742. EXPECT_EQ("@\"NSString \"\n"
  6743. "@\"literal\";",
  6744. format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
  6745. }
  6746. TEST_F(FormatTest, BreaksRawStringLiterals) {
  6747. EXPECT_EQ("R\"x(raw )x\"\n"
  6748. "R\"x(literal)x\";",
  6749. format("R\"x(raw literal)x\";", getGoogleStyleWithColumns(15)));
  6750. EXPECT_EQ("uR\"x(raw )x\"\n"
  6751. "uR\"x(literal)x\";",
  6752. format("uR\"x(raw literal)x\";", getGoogleStyleWithColumns(16)));
  6753. EXPECT_EQ("u8R\"x(raw )x\"\n"
  6754. "u8R\"x(literal)x\";",
  6755. format("u8R\"x(raw literal)x\";", getGoogleStyleWithColumns(17)));
  6756. EXPECT_EQ("LR\"x(raw )x\"\n"
  6757. "LR\"x(literal)x\";",
  6758. format("LR\"x(raw literal)x\";", getGoogleStyleWithColumns(16)));
  6759. EXPECT_EQ("UR\"x(raw )x\"\n"
  6760. "UR\"x(literal)x\";",
  6761. format("UR\"x(raw literal)x\";", getGoogleStyleWithColumns(16)));
  6762. }
  6763. TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
  6764. FormatStyle Style = getLLVMStyleWithColumns(20);
  6765. EXPECT_EQ(
  6766. "_T(\"aaaaaaaaaaaaaa\")\n"
  6767. "_T(\"aaaaaaaaaaaaaa\")\n"
  6768. "_T(\"aaaaaaaaaaaa\")",
  6769. format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
  6770. EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
  6771. " _T(\"aaaaaa\"),\n"
  6772. " z);",
  6773. format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
  6774. // FIXME: Handle embedded spaces in one iteration.
  6775. // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
  6776. // "_T(\"aaaaaaaaaaaaa\")\n"
  6777. // "_T(\"aaaaaaaaaaaaa\")\n"
  6778. // "_T(\"a\")",
  6779. // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  6780. // getLLVMStyleWithColumns(20)));
  6781. EXPECT_EQ(
  6782. "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  6783. format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
  6784. }
  6785. TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
  6786. EXPECT_EQ(
  6787. "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  6788. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  6789. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  6790. format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  6791. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  6792. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
  6793. }
  6794. TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
  6795. EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
  6796. format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle()));
  6797. EXPECT_EQ("fffffffffff(g(R\"x(\n"
  6798. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6799. ")x\",\n"
  6800. " a),\n"
  6801. " b);",
  6802. format("fffffffffff(g(R\"x(\n"
  6803. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6804. ")x\", a), b);",
  6805. getGoogleStyleWithColumns(20)));
  6806. EXPECT_EQ("fffffffffff(\n"
  6807. " g(R\"x(qqq\n"
  6808. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6809. ")x\",\n"
  6810. " a),\n"
  6811. " b);",
  6812. format("fffffffffff(g(R\"x(qqq\n"
  6813. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6814. ")x\", a), b);",
  6815. getGoogleStyleWithColumns(20)));
  6816. EXPECT_EQ("fffffffffff(R\"x(\n"
  6817. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6818. ")x\");",
  6819. format("fffffffffff(R\"x(\n"
  6820. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6821. ")x\");",
  6822. getGoogleStyleWithColumns(20)));
  6823. EXPECT_EQ("fffffffffff(R\"x(\n"
  6824. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6825. ")x\" + bbbbbb);",
  6826. format("fffffffffff(R\"x(\n"
  6827. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6828. ")x\" + bbbbbb);",
  6829. getGoogleStyleWithColumns(20)));
  6830. EXPECT_EQ("fffffffffff(\n"
  6831. " R\"x(\n"
  6832. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6833. ")x\" +\n"
  6834. " bbbbbb);",
  6835. format("fffffffffff(\n"
  6836. " R\"x(\n"
  6837. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6838. ")x\" + bbbbbb);",
  6839. getGoogleStyleWithColumns(20)));
  6840. }
  6841. TEST_F(FormatTest, SkipsUnknownStringLiterals) {
  6842. verifyFormat("string a = \"unterminated;");
  6843. EXPECT_EQ("function(\"unterminated,\n"
  6844. " OtherParameter);",
  6845. format("function( \"unterminated,\n"
  6846. " OtherParameter);"));
  6847. }
  6848. TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
  6849. FormatStyle Style = getLLVMStyle();
  6850. Style.Standard = FormatStyle::LS_Cpp03;
  6851. EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
  6852. format("#define x(_a) printf(\"foo\"_a);", Style));
  6853. }
  6854. TEST_F(FormatTest, UnderstandsCpp1y) {
  6855. verifyFormat("int bi{1'000'000};");
  6856. }
  6857. TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
  6858. EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
  6859. " \"ddeeefff\");",
  6860. format("someFunction(\"aaabbbcccdddeeefff\");",
  6861. getLLVMStyleWithColumns(25)));
  6862. EXPECT_EQ("someFunction1234567890(\n"
  6863. " \"aaabbbcccdddeeefff\");",
  6864. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  6865. getLLVMStyleWithColumns(26)));
  6866. EXPECT_EQ("someFunction1234567890(\n"
  6867. " \"aaabbbcccdddeeeff\"\n"
  6868. " \"f\");",
  6869. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  6870. getLLVMStyleWithColumns(25)));
  6871. EXPECT_EQ("someFunction1234567890(\n"
  6872. " \"aaabbbcccdddeeeff\"\n"
  6873. " \"f\");",
  6874. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  6875. getLLVMStyleWithColumns(24)));
  6876. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  6877. " \"ddde \"\n"
  6878. " \"efff\");",
  6879. format("someFunction(\"aaabbbcc ddde efff\");",
  6880. getLLVMStyleWithColumns(25)));
  6881. EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
  6882. " \"ddeeefff\");",
  6883. format("someFunction(\"aaabbbccc ddeeefff\");",
  6884. getLLVMStyleWithColumns(25)));
  6885. EXPECT_EQ("someFunction1234567890(\n"
  6886. " \"aaabb \"\n"
  6887. " \"cccdddeeefff\");",
  6888. format("someFunction1234567890(\"aaabb cccdddeeefff\");",
  6889. getLLVMStyleWithColumns(25)));
  6890. EXPECT_EQ("#define A \\\n"
  6891. " string s = \\\n"
  6892. " \"123456789\" \\\n"
  6893. " \"0\"; \\\n"
  6894. " int i;",
  6895. format("#define A string s = \"1234567890\"; int i;",
  6896. getLLVMStyleWithColumns(20)));
  6897. // FIXME: Put additional penalties on breaking at non-whitespace locations.
  6898. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  6899. " \"dddeeeff\"\n"
  6900. " \"f\");",
  6901. format("someFunction(\"aaabbbcc dddeeefff\");",
  6902. getLLVMStyleWithColumns(25)));
  6903. }
  6904. TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
  6905. EXPECT_EQ("\"\\a\"",
  6906. format("\"\\a\"", getLLVMStyleWithColumns(3)));
  6907. EXPECT_EQ("\"\\\"",
  6908. format("\"\\\"", getLLVMStyleWithColumns(2)));
  6909. EXPECT_EQ("\"test\"\n"
  6910. "\"\\n\"",
  6911. format("\"test\\n\"", getLLVMStyleWithColumns(7)));
  6912. EXPECT_EQ("\"tes\\\\\"\n"
  6913. "\"n\"",
  6914. format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
  6915. EXPECT_EQ("\"\\\\\\\\\"\n"
  6916. "\"\\n\"",
  6917. format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
  6918. EXPECT_EQ("\"\\uff01\"",
  6919. format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
  6920. EXPECT_EQ("\"\\uff01\"\n"
  6921. "\"test\"",
  6922. format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
  6923. EXPECT_EQ("\"\\Uff01ff02\"",
  6924. format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
  6925. EXPECT_EQ("\"\\x000000000001\"\n"
  6926. "\"next\"",
  6927. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
  6928. EXPECT_EQ("\"\\x000000000001next\"",
  6929. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
  6930. EXPECT_EQ("\"\\x000000000001\"",
  6931. format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
  6932. EXPECT_EQ("\"test\"\n"
  6933. "\"\\000000\"\n"
  6934. "\"000001\"",
  6935. format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
  6936. EXPECT_EQ("\"test\\000\"\n"
  6937. "\"00000000\"\n"
  6938. "\"1\"",
  6939. format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
  6940. // FIXME: We probably don't need to care about escape sequences in raw
  6941. // literals.
  6942. EXPECT_EQ("R\"(\\x)\"\n"
  6943. "R\"(\\x00)\"\n",
  6944. format("R\"(\\x\\x00)\"\n", getGoogleStyleWithColumns(7)));
  6945. }
  6946. TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
  6947. verifyFormat("void f() {\n"
  6948. " return g() {}\n"
  6949. " void h() {}");
  6950. verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
  6951. "g();\n"
  6952. "}");
  6953. }
  6954. TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
  6955. verifyFormat(
  6956. "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
  6957. }
  6958. TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
  6959. verifyFormat("class X {\n"
  6960. " void f() {\n"
  6961. " }\n"
  6962. "};",
  6963. getLLVMStyleWithColumns(12));
  6964. }
  6965. TEST_F(FormatTest, ConfigurableIndentWidth) {
  6966. FormatStyle EightIndent = getLLVMStyleWithColumns(18);
  6967. EightIndent.IndentWidth = 8;
  6968. EightIndent.ContinuationIndentWidth = 8;
  6969. verifyFormat("void f() {\n"
  6970. " someFunction();\n"
  6971. " if (true) {\n"
  6972. " f();\n"
  6973. " }\n"
  6974. "}",
  6975. EightIndent);
  6976. verifyFormat("class X {\n"
  6977. " void f() {\n"
  6978. " }\n"
  6979. "};",
  6980. EightIndent);
  6981. verifyFormat("int x[] = {\n"
  6982. " call(),\n"
  6983. " call()};",
  6984. EightIndent);
  6985. }
  6986. TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
  6987. verifyFormat("double\n"
  6988. "f();",
  6989. getLLVMStyleWithColumns(8));
  6990. }
  6991. TEST_F(FormatTest, ConfigurableUseOfTab) {
  6992. FormatStyle Tab = getLLVMStyleWithColumns(42);
  6993. Tab.IndentWidth = 8;
  6994. Tab.UseTab = FormatStyle::UT_Always;
  6995. Tab.AlignEscapedNewlinesLeft = true;
  6996. EXPECT_EQ("if (aaaaaaaa && // q\n"
  6997. " bb)\t\t// w\n"
  6998. "\t;",
  6999. format("if (aaaaaaaa &&// q\n"
  7000. "bb)// w\n"
  7001. ";",
  7002. Tab));
  7003. EXPECT_EQ("if (aaa && bbb) // w\n"
  7004. "\t;",
  7005. format("if(aaa&&bbb)// w\n"
  7006. ";",
  7007. Tab));
  7008. verifyFormat("class X {\n"
  7009. "\tvoid f() {\n"
  7010. "\t\tsomeFunction(parameter1,\n"
  7011. "\t\t\t parameter2);\n"
  7012. "\t}\n"
  7013. "};",
  7014. Tab);
  7015. verifyFormat("#define A \\\n"
  7016. "\tvoid f() { \\\n"
  7017. "\t\tsomeFunction( \\\n"
  7018. "\t\t parameter1, \\\n"
  7019. "\t\t parameter2); \\\n"
  7020. "\t}",
  7021. Tab);
  7022. EXPECT_EQ("void f() {\n"
  7023. "\tf();\n"
  7024. "\tg();\n"
  7025. "}",
  7026. format("void f() {\n"
  7027. "\tf();\n"
  7028. "\tg();\n"
  7029. "}",
  7030. 0, 0, Tab));
  7031. EXPECT_EQ("void f() {\n"
  7032. "\tf();\n"
  7033. "\tg();\n"
  7034. "}",
  7035. format("void f() {\n"
  7036. "\tf();\n"
  7037. "\tg();\n"
  7038. "}",
  7039. 16, 0, Tab));
  7040. EXPECT_EQ("void f() {\n"
  7041. " \tf();\n"
  7042. "\tg();\n"
  7043. "}",
  7044. format("void f() {\n"
  7045. " \tf();\n"
  7046. " \tg();\n"
  7047. "}",
  7048. 21, 0, Tab));
  7049. Tab.TabWidth = 4;
  7050. Tab.IndentWidth = 8;
  7051. verifyFormat("class TabWidth4Indent8 {\n"
  7052. "\t\tvoid f() {\n"
  7053. "\t\t\t\tsomeFunction(parameter1,\n"
  7054. "\t\t\t\t\t\t\t parameter2);\n"
  7055. "\t\t}\n"
  7056. "};",
  7057. Tab);
  7058. Tab.TabWidth = 4;
  7059. Tab.IndentWidth = 4;
  7060. verifyFormat("class TabWidth4Indent4 {\n"
  7061. "\tvoid f() {\n"
  7062. "\t\tsomeFunction(parameter1,\n"
  7063. "\t\t\t\t\t parameter2);\n"
  7064. "\t}\n"
  7065. "};",
  7066. Tab);
  7067. Tab.TabWidth = 8;
  7068. Tab.IndentWidth = 4;
  7069. verifyFormat("class TabWidth8Indent4 {\n"
  7070. " void f() {\n"
  7071. "\tsomeFunction(parameter1,\n"
  7072. "\t\t parameter2);\n"
  7073. " }\n"
  7074. "};",
  7075. Tab);
  7076. Tab.TabWidth = 8;
  7077. Tab.IndentWidth = 8;
  7078. EXPECT_EQ("/*\n"
  7079. "\t a\t\tcomment\n"
  7080. "\t in multiple lines\n"
  7081. " */",
  7082. format(" /*\t \t \n"
  7083. " \t \t a\t\tcomment\t \t\n"
  7084. " \t \t in multiple lines\t\n"
  7085. " \t */",
  7086. Tab));
  7087. Tab.UseTab = FormatStyle::UT_ForIndentation;
  7088. verifyFormat("{\n"
  7089. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7090. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7091. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7092. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7093. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7094. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7095. "};",
  7096. Tab);
  7097. verifyFormat("enum A {\n"
  7098. "\ta1, // Force multiple lines\n"
  7099. "\ta2,\n"
  7100. "\ta3\n"
  7101. "};",
  7102. Tab);
  7103. EXPECT_EQ("if (aaaaaaaa && // q\n"
  7104. " bb) // w\n"
  7105. "\t;",
  7106. format("if (aaaaaaaa &&// q\n"
  7107. "bb)// w\n"
  7108. ";",
  7109. Tab));
  7110. verifyFormat("class X {\n"
  7111. "\tvoid f() {\n"
  7112. "\t\tsomeFunction(parameter1,\n"
  7113. "\t\t parameter2);\n"
  7114. "\t}\n"
  7115. "};",
  7116. Tab);
  7117. verifyFormat("{\n"
  7118. "\tQ({\n"
  7119. "\t\tint a;\n"
  7120. "\t\tsomeFunction(aaaaaaaa,\n"
  7121. "\t\t bbbbbbb);\n"
  7122. "\t}, p);\n"
  7123. "}",
  7124. Tab);
  7125. EXPECT_EQ("{\n"
  7126. "\t/* aaaa\n"
  7127. "\t bbbb */\n"
  7128. "}",
  7129. format("{\n"
  7130. "/* aaaa\n"
  7131. " bbbb */\n"
  7132. "}",
  7133. Tab));
  7134. EXPECT_EQ("{\n"
  7135. "\t/*\n"
  7136. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7137. "\t bbbbbbbbbbbbb\n"
  7138. "\t*/\n"
  7139. "}",
  7140. format("{\n"
  7141. "/*\n"
  7142. " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7143. "*/\n"
  7144. "}",
  7145. Tab));
  7146. EXPECT_EQ("{\n"
  7147. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7148. "\t// bbbbbbbbbbbbb\n"
  7149. "}",
  7150. format("{\n"
  7151. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7152. "}",
  7153. Tab));
  7154. EXPECT_EQ("{\n"
  7155. "\t/*\n"
  7156. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7157. "\t bbbbbbbbbbbbb\n"
  7158. "\t*/\n"
  7159. "}",
  7160. format("{\n"
  7161. "\t/*\n"
  7162. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7163. "\t*/\n"
  7164. "}",
  7165. Tab));
  7166. EXPECT_EQ("{\n"
  7167. "\t/*\n"
  7168. "\n"
  7169. "\t*/\n"
  7170. "}",
  7171. format("{\n"
  7172. "\t/*\n"
  7173. "\n"
  7174. "\t*/\n"
  7175. "}",
  7176. Tab));
  7177. EXPECT_EQ("{\n"
  7178. "\t/*\n"
  7179. " asdf\n"
  7180. "\t*/\n"
  7181. "}",
  7182. format("{\n"
  7183. "\t/*\n"
  7184. " asdf\n"
  7185. "\t*/\n"
  7186. "}",
  7187. Tab));
  7188. Tab.UseTab = FormatStyle::UT_Never;
  7189. EXPECT_EQ("/*\n"
  7190. " a\t\tcomment\n"
  7191. " in multiple lines\n"
  7192. " */",
  7193. format(" /*\t \t \n"
  7194. " \t \t a\t\tcomment\t \t\n"
  7195. " \t \t in multiple lines\t\n"
  7196. " \t */",
  7197. Tab));
  7198. EXPECT_EQ("/* some\n"
  7199. " comment */",
  7200. format(" \t \t /* some\n"
  7201. " \t \t comment */",
  7202. Tab));
  7203. EXPECT_EQ("int a; /* some\n"
  7204. " comment */",
  7205. format(" \t \t int a; /* some\n"
  7206. " \t \t comment */",
  7207. Tab));
  7208. EXPECT_EQ("int a; /* some\n"
  7209. "comment */",
  7210. format(" \t \t int\ta; /* some\n"
  7211. " \t \t comment */",
  7212. Tab));
  7213. EXPECT_EQ("f(\"\t\t\"); /* some\n"
  7214. " comment */",
  7215. format(" \t \t f(\"\t\t\"); /* some\n"
  7216. " \t \t comment */",
  7217. Tab));
  7218. EXPECT_EQ("{\n"
  7219. " /*\n"
  7220. " * Comment\n"
  7221. " */\n"
  7222. " int i;\n"
  7223. "}",
  7224. format("{\n"
  7225. "\t/*\n"
  7226. "\t * Comment\n"
  7227. "\t */\n"
  7228. "\t int i;\n"
  7229. "}"));
  7230. }
  7231. TEST_F(FormatTest, CalculatesOriginalColumn) {
  7232. EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7233. "q\"; /* some\n"
  7234. " comment */",
  7235. format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7236. "q\"; /* some\n"
  7237. " comment */",
  7238. getLLVMStyle()));
  7239. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  7240. "/* some\n"
  7241. " comment */",
  7242. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  7243. " /* some\n"
  7244. " comment */",
  7245. getLLVMStyle()));
  7246. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7247. "qqq\n"
  7248. "/* some\n"
  7249. " comment */",
  7250. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7251. "qqq\n"
  7252. " /* some\n"
  7253. " comment */",
  7254. getLLVMStyle()));
  7255. EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7256. "wwww; /* some\n"
  7257. " comment */",
  7258. format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7259. "wwww; /* some\n"
  7260. " comment */",
  7261. getLLVMStyle()));
  7262. }
  7263. TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
  7264. FormatStyle NoSpace = getLLVMStyle();
  7265. NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
  7266. verifyFormat("while(true)\n"
  7267. " continue;", NoSpace);
  7268. verifyFormat("for(;;)\n"
  7269. " continue;", NoSpace);
  7270. verifyFormat("if(true)\n"
  7271. " f();\n"
  7272. "else if(true)\n"
  7273. " f();", NoSpace);
  7274. verifyFormat("do {\n"
  7275. " do_something();\n"
  7276. "} while(something());", NoSpace);
  7277. verifyFormat("switch(x) {\n"
  7278. "default:\n"
  7279. " break;\n"
  7280. "}", NoSpace);
  7281. verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
  7282. verifyFormat("size_t x = sizeof(x);", NoSpace);
  7283. verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
  7284. verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
  7285. verifyFormat("alignas(128) char a[128];", NoSpace);
  7286. verifyFormat("size_t x = alignof(MyType);", NoSpace);
  7287. verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
  7288. verifyFormat("int f() throw(Deprecated);", NoSpace);
  7289. FormatStyle Space = getLLVMStyle();
  7290. Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
  7291. verifyFormat("int f ();", Space);
  7292. verifyFormat("void f (int a, T b) {\n"
  7293. " while (true)\n"
  7294. " continue;\n"
  7295. "}",
  7296. Space);
  7297. verifyFormat("if (true)\n"
  7298. " f ();\n"
  7299. "else if (true)\n"
  7300. " f ();",
  7301. Space);
  7302. verifyFormat("do {\n"
  7303. " do_something ();\n"
  7304. "} while (something ());",
  7305. Space);
  7306. verifyFormat("switch (x) {\n"
  7307. "default:\n"
  7308. " break;\n"
  7309. "}",
  7310. Space);
  7311. verifyFormat("A::A () : a (1) {}", Space);
  7312. verifyFormat("void f () __attribute__ ((asdf));", Space);
  7313. verifyFormat("*(&a + 1);\n"
  7314. "&((&a)[1]);\n"
  7315. "a[(b + c) * d];\n"
  7316. "(((a + 1) * 2) + 3) * 4;",
  7317. Space);
  7318. verifyFormat("#define A(x) x", Space);
  7319. verifyFormat("#define A (x) x", Space);
  7320. verifyFormat("#if defined(x)\n"
  7321. "#endif",
  7322. Space);
  7323. verifyFormat("auto i = std::make_unique<int> (5);", Space);
  7324. verifyFormat("size_t x = sizeof (x);", Space);
  7325. verifyFormat("auto f (int x) -> decltype (x);", Space);
  7326. verifyFormat("int f (T x) noexcept (x.create ());", Space);
  7327. verifyFormat("alignas (128) char a[128];", Space);
  7328. verifyFormat("size_t x = alignof (MyType);", Space);
  7329. verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
  7330. verifyFormat("int f () throw (Deprecated);", Space);
  7331. }
  7332. TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
  7333. FormatStyle Spaces = getLLVMStyle();
  7334. Spaces.SpacesInParentheses = true;
  7335. verifyFormat("call( x, y, z );", Spaces);
  7336. verifyFormat("while ( (bool)1 )\n"
  7337. " continue;", Spaces);
  7338. verifyFormat("for ( ;; )\n"
  7339. " continue;", Spaces);
  7340. verifyFormat("if ( true )\n"
  7341. " f();\n"
  7342. "else if ( true )\n"
  7343. " f();", Spaces);
  7344. verifyFormat("do {\n"
  7345. " do_something( (int)i );\n"
  7346. "} while ( something() );", Spaces);
  7347. verifyFormat("switch ( x ) {\n"
  7348. "default:\n"
  7349. " break;\n"
  7350. "}", Spaces);
  7351. Spaces.SpacesInParentheses = false;
  7352. Spaces.SpacesInCStyleCastParentheses = true;
  7353. verifyFormat("Type *A = ( Type * )P;", Spaces);
  7354. verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
  7355. verifyFormat("x = ( int32 )y;", Spaces);
  7356. verifyFormat("int a = ( int )(2.0f);", Spaces);
  7357. verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
  7358. verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
  7359. verifyFormat("#define x (( int )-1)", Spaces);
  7360. Spaces.SpacesInParentheses = false;
  7361. Spaces.SpaceInEmptyParentheses = true;
  7362. verifyFormat("call(x, y, z);", Spaces);
  7363. verifyFormat("call( )", Spaces);
  7364. // Run the first set of tests again with
  7365. // Spaces.SpacesInParentheses = false,
  7366. // Spaces.SpaceInEmptyParentheses = true and
  7367. // Spaces.SpacesInCStyleCastParentheses = true
  7368. Spaces.SpacesInParentheses = false,
  7369. Spaces.SpaceInEmptyParentheses = true;
  7370. Spaces.SpacesInCStyleCastParentheses = true;
  7371. verifyFormat("call(x, y, z);", Spaces);
  7372. verifyFormat("while (( bool )1)\n"
  7373. " continue;", Spaces);
  7374. verifyFormat("for (;;)\n"
  7375. " continue;", Spaces);
  7376. verifyFormat("if (true)\n"
  7377. " f( );\n"
  7378. "else if (true)\n"
  7379. " f( );", Spaces);
  7380. verifyFormat("do {\n"
  7381. " do_something(( int )i);\n"
  7382. "} while (something( ));", Spaces);
  7383. verifyFormat("switch (x) {\n"
  7384. "default:\n"
  7385. " break;\n"
  7386. "}", Spaces);
  7387. Spaces.SpaceAfterCStyleCast = true;
  7388. verifyFormat("call(x, y, z);", Spaces);
  7389. verifyFormat("while (( bool ) 1)\n"
  7390. " continue;",
  7391. Spaces);
  7392. verifyFormat("for (;;)\n"
  7393. " continue;",
  7394. Spaces);
  7395. verifyFormat("if (true)\n"
  7396. " f( );\n"
  7397. "else if (true)\n"
  7398. " f( );",
  7399. Spaces);
  7400. verifyFormat("do {\n"
  7401. " do_something(( int ) i);\n"
  7402. "} while (something( ));",
  7403. Spaces);
  7404. verifyFormat("switch (x) {\n"
  7405. "default:\n"
  7406. " break;\n"
  7407. "}",
  7408. Spaces);
  7409. Spaces.SpacesInCStyleCastParentheses = false;
  7410. Spaces.SpaceAfterCStyleCast = true;
  7411. verifyFormat("while ((bool) 1)\n"
  7412. " continue;",
  7413. Spaces);
  7414. verifyFormat("do {\n"
  7415. " do_something((int) i);\n"
  7416. "} while (something( ));",
  7417. Spaces);
  7418. }
  7419. TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
  7420. verifyFormat("int a[5];");
  7421. verifyFormat("a[3] += 42;");
  7422. FormatStyle Spaces = getLLVMStyle();
  7423. Spaces.SpacesInSquareBrackets = true;
  7424. // Lambdas unchanged.
  7425. verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
  7426. verifyFormat("return [i, args...] {};", Spaces);
  7427. // Not lambdas.
  7428. verifyFormat("int a[ 5 ];", Spaces);
  7429. verifyFormat("a[ 3 ] += 42;", Spaces);
  7430. verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
  7431. verifyFormat("double &operator[](int i) { return 0; }\n"
  7432. "int i;",
  7433. Spaces);
  7434. verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
  7435. verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
  7436. verifyFormat("int i = (*b)[ a ]->f();", Spaces);
  7437. }
  7438. TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
  7439. verifyFormat("int a = 5;");
  7440. verifyFormat("a += 42;");
  7441. verifyFormat("a or_eq 8;");
  7442. FormatStyle Spaces = getLLVMStyle();
  7443. Spaces.SpaceBeforeAssignmentOperators = false;
  7444. verifyFormat("int a= 5;", Spaces);
  7445. verifyFormat("a+= 42;", Spaces);
  7446. verifyFormat("a or_eq 8;", Spaces);
  7447. }
  7448. TEST_F(FormatTest, LinuxBraceBreaking) {
  7449. FormatStyle LinuxBraceStyle = getLLVMStyle();
  7450. LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
  7451. verifyFormat("namespace a\n"
  7452. "{\n"
  7453. "class A\n"
  7454. "{\n"
  7455. " void f()\n"
  7456. " {\n"
  7457. " if (true) {\n"
  7458. " a();\n"
  7459. " b();\n"
  7460. " }\n"
  7461. " }\n"
  7462. " void g() { return; }\n"
  7463. "};\n"
  7464. "struct B {\n"
  7465. " int x;\n"
  7466. "};\n"
  7467. "}\n",
  7468. LinuxBraceStyle);
  7469. verifyFormat("enum X {\n"
  7470. " Y = 0,\n"
  7471. "}\n",
  7472. LinuxBraceStyle);
  7473. verifyFormat("struct S {\n"
  7474. " int Type;\n"
  7475. " union {\n"
  7476. " int x;\n"
  7477. " double y;\n"
  7478. " } Value;\n"
  7479. " class C\n"
  7480. " {\n"
  7481. " MyFavoriteType Value;\n"
  7482. " } Class;\n"
  7483. "}\n",
  7484. LinuxBraceStyle);
  7485. }
  7486. TEST_F(FormatTest, StroustrupBraceBreaking) {
  7487. FormatStyle StroustrupBraceStyle = getLLVMStyle();
  7488. StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  7489. verifyFormat("namespace a {\n"
  7490. "class A {\n"
  7491. " void f()\n"
  7492. " {\n"
  7493. " if (true) {\n"
  7494. " a();\n"
  7495. " b();\n"
  7496. " }\n"
  7497. " }\n"
  7498. " void g() { return; }\n"
  7499. "};\n"
  7500. "struct B {\n"
  7501. " int x;\n"
  7502. "};\n"
  7503. "}\n",
  7504. StroustrupBraceStyle);
  7505. verifyFormat("void foo()\n"
  7506. "{\n"
  7507. " if (a) {\n"
  7508. " a();\n"
  7509. " }\n"
  7510. " else {\n"
  7511. " b();\n"
  7512. " }\n"
  7513. "}\n",
  7514. StroustrupBraceStyle);
  7515. verifyFormat("#ifdef _DEBUG\n"
  7516. "int foo(int i = 0)\n"
  7517. "#else\n"
  7518. "int foo(int i = 5)\n"
  7519. "#endif\n"
  7520. "{\n"
  7521. " return i;\n"
  7522. "}",
  7523. StroustrupBraceStyle);
  7524. verifyFormat("void foo() {}\n"
  7525. "void bar()\n"
  7526. "#ifdef _DEBUG\n"
  7527. "{\n"
  7528. " foo();\n"
  7529. "}\n"
  7530. "#else\n"
  7531. "{\n"
  7532. "}\n"
  7533. "#endif",
  7534. StroustrupBraceStyle);
  7535. verifyFormat("void foobar() { int i = 5; }\n"
  7536. "#ifdef _DEBUG\n"
  7537. "void bar() {}\n"
  7538. "#else\n"
  7539. "void bar() { foobar(); }\n"
  7540. "#endif",
  7541. StroustrupBraceStyle);
  7542. }
  7543. TEST_F(FormatTest, AllmanBraceBreaking) {
  7544. FormatStyle AllmanBraceStyle = getLLVMStyle();
  7545. AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
  7546. verifyFormat("namespace a\n"
  7547. "{\n"
  7548. "class A\n"
  7549. "{\n"
  7550. " void f()\n"
  7551. " {\n"
  7552. " if (true)\n"
  7553. " {\n"
  7554. " a();\n"
  7555. " b();\n"
  7556. " }\n"
  7557. " }\n"
  7558. " void g() { return; }\n"
  7559. "};\n"
  7560. "struct B\n"
  7561. "{\n"
  7562. " int x;\n"
  7563. "};\n"
  7564. "}",
  7565. AllmanBraceStyle);
  7566. verifyFormat("void f()\n"
  7567. "{\n"
  7568. " if (true)\n"
  7569. " {\n"
  7570. " a();\n"
  7571. " }\n"
  7572. " else if (false)\n"
  7573. " {\n"
  7574. " b();\n"
  7575. " }\n"
  7576. " else\n"
  7577. " {\n"
  7578. " c();\n"
  7579. " }\n"
  7580. "}\n",
  7581. AllmanBraceStyle);
  7582. verifyFormat("void f()\n"
  7583. "{\n"
  7584. " for (int i = 0; i < 10; ++i)\n"
  7585. " {\n"
  7586. " a();\n"
  7587. " }\n"
  7588. " while (false)\n"
  7589. " {\n"
  7590. " b();\n"
  7591. " }\n"
  7592. " do\n"
  7593. " {\n"
  7594. " c();\n"
  7595. " } while (false)\n"
  7596. "}\n",
  7597. AllmanBraceStyle);
  7598. verifyFormat("void f(int a)\n"
  7599. "{\n"
  7600. " switch (a)\n"
  7601. " {\n"
  7602. " case 0:\n"
  7603. " break;\n"
  7604. " case 1:\n"
  7605. " {\n"
  7606. " break;\n"
  7607. " }\n"
  7608. " case 2:\n"
  7609. " {\n"
  7610. " }\n"
  7611. " break;\n"
  7612. " default:\n"
  7613. " break;\n"
  7614. " }\n"
  7615. "}\n",
  7616. AllmanBraceStyle);
  7617. verifyFormat("enum X\n"
  7618. "{\n"
  7619. " Y = 0,\n"
  7620. "}\n",
  7621. AllmanBraceStyle);
  7622. verifyFormat("enum X\n"
  7623. "{\n"
  7624. " Y = 0\n"
  7625. "}\n",
  7626. AllmanBraceStyle);
  7627. verifyFormat("@interface BSApplicationController ()\n"
  7628. "{\n"
  7629. "@private\n"
  7630. " id _extraIvar;\n"
  7631. "}\n"
  7632. "@end\n",
  7633. AllmanBraceStyle);
  7634. verifyFormat("#ifdef _DEBUG\n"
  7635. "int foo(int i = 0)\n"
  7636. "#else\n"
  7637. "int foo(int i = 5)\n"
  7638. "#endif\n"
  7639. "{\n"
  7640. " return i;\n"
  7641. "}",
  7642. AllmanBraceStyle);
  7643. verifyFormat("void foo() {}\n"
  7644. "void bar()\n"
  7645. "#ifdef _DEBUG\n"
  7646. "{\n"
  7647. " foo();\n"
  7648. "}\n"
  7649. "#else\n"
  7650. "{\n"
  7651. "}\n"
  7652. "#endif",
  7653. AllmanBraceStyle);
  7654. verifyFormat("void foobar() { int i = 5; }\n"
  7655. "#ifdef _DEBUG\n"
  7656. "void bar() {}\n"
  7657. "#else\n"
  7658. "void bar() { foobar(); }\n"
  7659. "#endif",
  7660. AllmanBraceStyle);
  7661. // This shouldn't affect ObjC blocks..
  7662. verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
  7663. " // ...\n"
  7664. " int i;\n"
  7665. "}];",
  7666. AllmanBraceStyle);
  7667. verifyFormat("void (^block)(void) = ^{\n"
  7668. " // ...\n"
  7669. " int i;\n"
  7670. "};",
  7671. AllmanBraceStyle);
  7672. // .. or dict literals.
  7673. verifyFormat("void f()\n"
  7674. "{\n"
  7675. " [object someMethod:@{ @\"a\" : @\"b\" }];\n"
  7676. "}",
  7677. AllmanBraceStyle);
  7678. AllmanBraceStyle.ColumnLimit = 19;
  7679. verifyFormat("void f() { int i; }", AllmanBraceStyle);
  7680. AllmanBraceStyle.ColumnLimit = 18;
  7681. verifyFormat("void f()\n"
  7682. "{\n"
  7683. " int i;\n"
  7684. "}",
  7685. AllmanBraceStyle);
  7686. AllmanBraceStyle.ColumnLimit = 80;
  7687. FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
  7688. BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
  7689. BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
  7690. verifyFormat("void f(bool b)\n"
  7691. "{\n"
  7692. " if (b)\n"
  7693. " {\n"
  7694. " return;\n"
  7695. " }\n"
  7696. "}\n",
  7697. BreakBeforeBraceShortIfs);
  7698. verifyFormat("void f(bool b)\n"
  7699. "{\n"
  7700. " if (b) return;\n"
  7701. "}\n",
  7702. BreakBeforeBraceShortIfs);
  7703. verifyFormat("void f(bool b)\n"
  7704. "{\n"
  7705. " while (b)\n"
  7706. " {\n"
  7707. " return;\n"
  7708. " }\n"
  7709. "}\n",
  7710. BreakBeforeBraceShortIfs);
  7711. }
  7712. TEST_F(FormatTest, GNUBraceBreaking) {
  7713. FormatStyle GNUBraceStyle = getLLVMStyle();
  7714. GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
  7715. verifyFormat("namespace a\n"
  7716. "{\n"
  7717. "class A\n"
  7718. "{\n"
  7719. " void f()\n"
  7720. " {\n"
  7721. " int a;\n"
  7722. " {\n"
  7723. " int b;\n"
  7724. " }\n"
  7725. " if (true)\n"
  7726. " {\n"
  7727. " a();\n"
  7728. " b();\n"
  7729. " }\n"
  7730. " }\n"
  7731. " void g() { return; }\n"
  7732. "}\n"
  7733. "}",
  7734. GNUBraceStyle);
  7735. verifyFormat("void f()\n"
  7736. "{\n"
  7737. " if (true)\n"
  7738. " {\n"
  7739. " a();\n"
  7740. " }\n"
  7741. " else if (false)\n"
  7742. " {\n"
  7743. " b();\n"
  7744. " }\n"
  7745. " else\n"
  7746. " {\n"
  7747. " c();\n"
  7748. " }\n"
  7749. "}\n",
  7750. GNUBraceStyle);
  7751. verifyFormat("void f()\n"
  7752. "{\n"
  7753. " for (int i = 0; i < 10; ++i)\n"
  7754. " {\n"
  7755. " a();\n"
  7756. " }\n"
  7757. " while (false)\n"
  7758. " {\n"
  7759. " b();\n"
  7760. " }\n"
  7761. " do\n"
  7762. " {\n"
  7763. " c();\n"
  7764. " }\n"
  7765. " while (false);\n"
  7766. "}\n",
  7767. GNUBraceStyle);
  7768. verifyFormat("void f(int a)\n"
  7769. "{\n"
  7770. " switch (a)\n"
  7771. " {\n"
  7772. " case 0:\n"
  7773. " break;\n"
  7774. " case 1:\n"
  7775. " {\n"
  7776. " break;\n"
  7777. " }\n"
  7778. " case 2:\n"
  7779. " {\n"
  7780. " }\n"
  7781. " break;\n"
  7782. " default:\n"
  7783. " break;\n"
  7784. " }\n"
  7785. "}\n",
  7786. GNUBraceStyle);
  7787. verifyFormat("enum X\n"
  7788. "{\n"
  7789. " Y = 0,\n"
  7790. "}\n",
  7791. GNUBraceStyle);
  7792. verifyFormat("@interface BSApplicationController ()\n"
  7793. "{\n"
  7794. "@private\n"
  7795. " id _extraIvar;\n"
  7796. "}\n"
  7797. "@end\n",
  7798. GNUBraceStyle);
  7799. verifyFormat("#ifdef _DEBUG\n"
  7800. "int foo(int i = 0)\n"
  7801. "#else\n"
  7802. "int foo(int i = 5)\n"
  7803. "#endif\n"
  7804. "{\n"
  7805. " return i;\n"
  7806. "}",
  7807. GNUBraceStyle);
  7808. verifyFormat("void foo() {}\n"
  7809. "void bar()\n"
  7810. "#ifdef _DEBUG\n"
  7811. "{\n"
  7812. " foo();\n"
  7813. "}\n"
  7814. "#else\n"
  7815. "{\n"
  7816. "}\n"
  7817. "#endif",
  7818. GNUBraceStyle);
  7819. verifyFormat("void foobar() { int i = 5; }\n"
  7820. "#ifdef _DEBUG\n"
  7821. "void bar() {}\n"
  7822. "#else\n"
  7823. "void bar() { foobar(); }\n"
  7824. "#endif",
  7825. GNUBraceStyle);
  7826. }
  7827. TEST_F(FormatTest, CatchExceptionReferenceBinding) {
  7828. verifyFormat("void f() {\n"
  7829. " try {\n"
  7830. " } catch (const Exception &e) {\n"
  7831. " }\n"
  7832. "}\n",
  7833. getLLVMStyle());
  7834. }
  7835. TEST_F(FormatTest, UnderstandsPragmas) {
  7836. verifyFormat("#pragma omp reduction(| : var)");
  7837. verifyFormat("#pragma omp reduction(+ : var)");
  7838. EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
  7839. "(including parentheses).",
  7840. format("#pragma mark Any non-hyphenated or hyphenated string "
  7841. "(including parentheses)."));
  7842. }
  7843. #define EXPECT_ALL_STYLES_EQUAL(Styles) \
  7844. for (size_t i = 1; i < Styles.size(); ++i) \
  7845. EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " \
  7846. << Styles.size() \
  7847. << " differs from Style #0"
  7848. TEST_F(FormatTest, GetsPredefinedStyleByName) {
  7849. SmallVector<FormatStyle, 3> Styles;
  7850. Styles.resize(3);
  7851. Styles[0] = getLLVMStyle();
  7852. EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
  7853. EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
  7854. EXPECT_ALL_STYLES_EQUAL(Styles);
  7855. Styles[0] = getGoogleStyle();
  7856. EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
  7857. EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
  7858. EXPECT_ALL_STYLES_EQUAL(Styles);
  7859. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  7860. EXPECT_TRUE(
  7861. getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
  7862. EXPECT_TRUE(
  7863. getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
  7864. EXPECT_ALL_STYLES_EQUAL(Styles);
  7865. Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
  7866. EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
  7867. EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
  7868. EXPECT_ALL_STYLES_EQUAL(Styles);
  7869. Styles[0] = getMozillaStyle();
  7870. EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
  7871. EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
  7872. EXPECT_ALL_STYLES_EQUAL(Styles);
  7873. Styles[0] = getWebKitStyle();
  7874. EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
  7875. EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
  7876. EXPECT_ALL_STYLES_EQUAL(Styles);
  7877. Styles[0] = getGNUStyle();
  7878. EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
  7879. EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
  7880. EXPECT_ALL_STYLES_EQUAL(Styles);
  7881. EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
  7882. }
  7883. TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
  7884. SmallVector<FormatStyle, 8> Styles;
  7885. Styles.resize(2);
  7886. Styles[0] = getGoogleStyle();
  7887. Styles[1] = getLLVMStyle();
  7888. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  7889. EXPECT_ALL_STYLES_EQUAL(Styles);
  7890. Styles.resize(5);
  7891. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  7892. Styles[1] = getLLVMStyle();
  7893. Styles[1].Language = FormatStyle::LK_JavaScript;
  7894. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  7895. Styles[2] = getLLVMStyle();
  7896. Styles[2].Language = FormatStyle::LK_JavaScript;
  7897. EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
  7898. "BasedOnStyle: Google",
  7899. &Styles[2]).value());
  7900. Styles[3] = getLLVMStyle();
  7901. Styles[3].Language = FormatStyle::LK_JavaScript;
  7902. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
  7903. "Language: JavaScript",
  7904. &Styles[3]).value());
  7905. Styles[4] = getLLVMStyle();
  7906. Styles[4].Language = FormatStyle::LK_JavaScript;
  7907. EXPECT_EQ(0, parseConfiguration("---\n"
  7908. "BasedOnStyle: LLVM\n"
  7909. "IndentWidth: 123\n"
  7910. "---\n"
  7911. "BasedOnStyle: Google\n"
  7912. "Language: JavaScript",
  7913. &Styles[4]).value());
  7914. EXPECT_ALL_STYLES_EQUAL(Styles);
  7915. }
  7916. #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \
  7917. Style.FIELD = false; \
  7918. EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \
  7919. EXPECT_TRUE(Style.FIELD); \
  7920. EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \
  7921. EXPECT_FALSE(Style.FIELD);
  7922. #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
  7923. #define CHECK_PARSE(TEXT, FIELD, VALUE) \
  7924. EXPECT_NE(VALUE, Style.FIELD); \
  7925. EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \
  7926. EXPECT_EQ(VALUE, Style.FIELD)
  7927. TEST_F(FormatTest, ParsesConfigurationBools) {
  7928. FormatStyle Style = {};
  7929. Style.Language = FormatStyle::LK_Cpp;
  7930. CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
  7931. CHECK_PARSE_BOOL(AlignTrailingComments);
  7932. CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
  7933. CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
  7934. CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
  7935. CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
  7936. CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
  7937. CHECK_PARSE_BOOL(AlwaysBreakAfterDefinitionReturnType);
  7938. CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
  7939. CHECK_PARSE_BOOL(BinPackParameters);
  7940. CHECK_PARSE_BOOL(BinPackArguments);
  7941. CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
  7942. CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
  7943. CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
  7944. CHECK_PARSE_BOOL(DerivePointerAlignment);
  7945. CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
  7946. CHECK_PARSE_BOOL(IndentCaseLabels);
  7947. CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
  7948. CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
  7949. CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
  7950. CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
  7951. CHECK_PARSE_BOOL(Cpp11BracedListStyle);
  7952. CHECK_PARSE_BOOL(SpacesInParentheses);
  7953. CHECK_PARSE_BOOL(SpacesInSquareBrackets);
  7954. CHECK_PARSE_BOOL(SpacesInAngles);
  7955. CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
  7956. CHECK_PARSE_BOOL(SpacesInContainerLiterals);
  7957. CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
  7958. CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
  7959. CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
  7960. }
  7961. #undef CHECK_PARSE_BOOL
  7962. TEST_F(FormatTest, ParsesConfiguration) {
  7963. FormatStyle Style = {};
  7964. Style.Language = FormatStyle::LK_Cpp;
  7965. CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
  7966. CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
  7967. ConstructorInitializerIndentWidth, 1234u);
  7968. CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
  7969. CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
  7970. CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
  7971. CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
  7972. PenaltyBreakBeforeFirstCallParameter, 1234u);
  7973. CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
  7974. CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
  7975. PenaltyReturnTypeOnItsOwnLine, 1234u);
  7976. CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
  7977. SpacesBeforeTrailingComments, 1234u);
  7978. CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
  7979. CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
  7980. Style.PointerAlignment = FormatStyle::PAS_Middle;
  7981. CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
  7982. FormatStyle::PAS_Left);
  7983. CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
  7984. FormatStyle::PAS_Right);
  7985. CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
  7986. FormatStyle::PAS_Middle);
  7987. // For backward compatibility:
  7988. CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
  7989. FormatStyle::PAS_Left);
  7990. CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
  7991. FormatStyle::PAS_Right);
  7992. CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
  7993. FormatStyle::PAS_Middle);
  7994. Style.Standard = FormatStyle::LS_Auto;
  7995. CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
  7996. CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
  7997. CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
  7998. CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
  7999. CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
  8000. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  8001. CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
  8002. BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
  8003. CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
  8004. FormatStyle::BOS_None);
  8005. CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
  8006. FormatStyle::BOS_All);
  8007. // For backward compatibility:
  8008. CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
  8009. FormatStyle::BOS_None);
  8010. CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
  8011. FormatStyle::BOS_All);
  8012. Style.UseTab = FormatStyle::UT_ForIndentation;
  8013. CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
  8014. CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
  8015. CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
  8016. // For backward compatibility:
  8017. CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
  8018. CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
  8019. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  8020. CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
  8021. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
  8022. CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
  8023. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
  8024. CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
  8025. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
  8026. // For backward compatibility:
  8027. CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
  8028. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
  8029. CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
  8030. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
  8031. Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
  8032. CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
  8033. FormatStyle::SBPO_Never);
  8034. CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
  8035. FormatStyle::SBPO_Always);
  8036. CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
  8037. FormatStyle::SBPO_ControlStatements);
  8038. // For backward compatibility:
  8039. CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
  8040. FormatStyle::SBPO_Never);
  8041. CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
  8042. FormatStyle::SBPO_ControlStatements);
  8043. Style.ColumnLimit = 123;
  8044. FormatStyle BaseStyle = getLLVMStyle();
  8045. CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
  8046. CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
  8047. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  8048. CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
  8049. FormatStyle::BS_Attach);
  8050. CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
  8051. FormatStyle::BS_Linux);
  8052. CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
  8053. FormatStyle::BS_Stroustrup);
  8054. CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
  8055. FormatStyle::BS_Allman);
  8056. CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
  8057. Style.NamespaceIndentation = FormatStyle::NI_All;
  8058. CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
  8059. FormatStyle::NI_None);
  8060. CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
  8061. FormatStyle::NI_Inner);
  8062. CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
  8063. FormatStyle::NI_All);
  8064. Style.ForEachMacros.clear();
  8065. std::vector<std::string> BoostForeach;
  8066. BoostForeach.push_back("BOOST_FOREACH");
  8067. CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
  8068. std::vector<std::string> BoostAndQForeach;
  8069. BoostAndQForeach.push_back("BOOST_FOREACH");
  8070. BoostAndQForeach.push_back("Q_FOREACH");
  8071. CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
  8072. BoostAndQForeach);
  8073. }
  8074. TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
  8075. FormatStyle Style = {};
  8076. Style.Language = FormatStyle::LK_Cpp;
  8077. CHECK_PARSE("Language: Cpp\n"
  8078. "IndentWidth: 12",
  8079. IndentWidth, 12u);
  8080. EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
  8081. "IndentWidth: 34",
  8082. &Style),
  8083. ParseError::Unsuitable);
  8084. EXPECT_EQ(12u, Style.IndentWidth);
  8085. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  8086. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  8087. Style.Language = FormatStyle::LK_JavaScript;
  8088. CHECK_PARSE("Language: JavaScript\n"
  8089. "IndentWidth: 12",
  8090. IndentWidth, 12u);
  8091. CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
  8092. EXPECT_EQ(parseConfiguration("Language: Cpp\n"
  8093. "IndentWidth: 34",
  8094. &Style),
  8095. ParseError::Unsuitable);
  8096. EXPECT_EQ(23u, Style.IndentWidth);
  8097. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  8098. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  8099. CHECK_PARSE("BasedOnStyle: LLVM\n"
  8100. "IndentWidth: 67",
  8101. IndentWidth, 67u);
  8102. CHECK_PARSE("---\n"
  8103. "Language: JavaScript\n"
  8104. "IndentWidth: 12\n"
  8105. "---\n"
  8106. "Language: Cpp\n"
  8107. "IndentWidth: 34\n"
  8108. "...\n",
  8109. IndentWidth, 12u);
  8110. Style.Language = FormatStyle::LK_Cpp;
  8111. CHECK_PARSE("---\n"
  8112. "Language: JavaScript\n"
  8113. "IndentWidth: 12\n"
  8114. "---\n"
  8115. "Language: Cpp\n"
  8116. "IndentWidth: 34\n"
  8117. "...\n",
  8118. IndentWidth, 34u);
  8119. CHECK_PARSE("---\n"
  8120. "IndentWidth: 78\n"
  8121. "---\n"
  8122. "Language: JavaScript\n"
  8123. "IndentWidth: 56\n"
  8124. "...\n",
  8125. IndentWidth, 78u);
  8126. Style.ColumnLimit = 123;
  8127. Style.IndentWidth = 234;
  8128. Style.BreakBeforeBraces = FormatStyle::BS_Linux;
  8129. Style.TabWidth = 345;
  8130. EXPECT_FALSE(parseConfiguration("---\n"
  8131. "IndentWidth: 456\n"
  8132. "BreakBeforeBraces: Allman\n"
  8133. "---\n"
  8134. "Language: JavaScript\n"
  8135. "IndentWidth: 111\n"
  8136. "TabWidth: 111\n"
  8137. "---\n"
  8138. "Language: Cpp\n"
  8139. "BreakBeforeBraces: Stroustrup\n"
  8140. "TabWidth: 789\n"
  8141. "...\n",
  8142. &Style));
  8143. EXPECT_EQ(123u, Style.ColumnLimit);
  8144. EXPECT_EQ(456u, Style.IndentWidth);
  8145. EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
  8146. EXPECT_EQ(789u, Style.TabWidth);
  8147. EXPECT_EQ(parseConfiguration("---\n"
  8148. "Language: JavaScript\n"
  8149. "IndentWidth: 56\n"
  8150. "---\n"
  8151. "IndentWidth: 78\n"
  8152. "...\n",
  8153. &Style),
  8154. ParseError::Error);
  8155. EXPECT_EQ(parseConfiguration("---\n"
  8156. "Language: JavaScript\n"
  8157. "IndentWidth: 56\n"
  8158. "---\n"
  8159. "Language: JavaScript\n"
  8160. "IndentWidth: 78\n"
  8161. "...\n",
  8162. &Style),
  8163. ParseError::Error);
  8164. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  8165. }
  8166. #undef CHECK_PARSE
  8167. TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
  8168. FormatStyle Style = {};
  8169. Style.Language = FormatStyle::LK_JavaScript;
  8170. Style.BreakBeforeTernaryOperators = true;
  8171. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
  8172. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  8173. Style.BreakBeforeTernaryOperators = true;
  8174. EXPECT_EQ(0, parseConfiguration("---\n"
  8175. "BasedOnStyle: Google\n"
  8176. "---\n"
  8177. "Language: JavaScript\n"
  8178. "IndentWidth: 76\n"
  8179. "...\n", &Style).value());
  8180. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  8181. EXPECT_EQ(76u, Style.IndentWidth);
  8182. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  8183. }
  8184. TEST_F(FormatTest, ConfigurationRoundTripTest) {
  8185. FormatStyle Style = getLLVMStyle();
  8186. std::string YAML = configurationAsText(Style);
  8187. FormatStyle ParsedStyle = {};
  8188. ParsedStyle.Language = FormatStyle::LK_Cpp;
  8189. EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
  8190. EXPECT_EQ(Style, ParsedStyle);
  8191. }
  8192. TEST_F(FormatTest, WorksFor8bitEncodings) {
  8193. EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
  8194. "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
  8195. "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
  8196. "\"\xef\xee\xf0\xf3...\"",
  8197. format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
  8198. "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
  8199. "\xef\xee\xf0\xf3...\"",
  8200. getLLVMStyleWithColumns(12)));
  8201. }
  8202. TEST_F(FormatTest, HandlesUTF8BOM) {
  8203. EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
  8204. EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
  8205. format("\xef\xbb\xbf#include <iostream>"));
  8206. EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
  8207. format("\xef\xbb\xbf\n#include <iostream>"));
  8208. }
  8209. // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
  8210. #if !defined(_MSC_VER)
  8211. TEST_F(FormatTest, CountsUTF8CharactersProperly) {
  8212. verifyFormat("\"Однажды в студёную зимнюю пору...\"",
  8213. getLLVMStyleWithColumns(35));
  8214. verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
  8215. getLLVMStyleWithColumns(31));
  8216. verifyFormat("// Однажды в студёную зимнюю пору...",
  8217. getLLVMStyleWithColumns(36));
  8218. verifyFormat("// 一 二 三 四 五 六 七 八 九 十",
  8219. getLLVMStyleWithColumns(32));
  8220. verifyFormat("/* Однажды в студёную зимнюю пору... */",
  8221. getLLVMStyleWithColumns(39));
  8222. verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
  8223. getLLVMStyleWithColumns(35));
  8224. }
  8225. TEST_F(FormatTest, SplitsUTF8Strings) {
  8226. // Non-printable characters' width is currently considered to be the length in
  8227. // bytes in UTF8. The characters can be displayed in very different manner
  8228. // (zero-width, single width with a substitution glyph, expanded to their code
  8229. // (e.g. "<8d>"), so there's no single correct way to handle them.
  8230. EXPECT_EQ("\"aaaaÄ\"\n"
  8231. "\"\xc2\x8d\";",
  8232. format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  8233. EXPECT_EQ("\"aaaaaaaÄ\"\n"
  8234. "\"\xc2\x8d\";",
  8235. format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  8236. EXPECT_EQ(
  8237. "\"Однажды, в \"\n"
  8238. "\"студёную \"\n"
  8239. "\"зимнюю \"\n"
  8240. "\"пору,\"",
  8241. format("\"Однажды, в студёную зимнюю пору,\"",
  8242. getLLVMStyleWithColumns(13)));
  8243. EXPECT_EQ("\"一 二 三 \"\n"
  8244. "\"四 五六 \"\n"
  8245. "\"七 八 九 \"\n"
  8246. "\"十\"",
  8247. format("\"一 二 三 四 五六 七 八 九 十\"",
  8248. getLLVMStyleWithColumns(11)));
  8249. EXPECT_EQ("\"一\t二 \"\n"
  8250. "\"\t三 \"\n"
  8251. "\"四 五\t六 \"\n"
  8252. "\"\t七 \"\n"
  8253. "\"八九十\tqq\"",
  8254. format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
  8255. getLLVMStyleWithColumns(11)));
  8256. }
  8257. TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
  8258. EXPECT_EQ("const char *sssss =\n"
  8259. " \"一二三四五六七八\\\n"
  8260. " 九 十\";",
  8261. format("const char *sssss = \"一二三四五六七八\\\n"
  8262. " 九 十\";",
  8263. getLLVMStyleWithColumns(30)));
  8264. }
  8265. TEST_F(FormatTest, SplitsUTF8LineComments) {
  8266. EXPECT_EQ("// aaaaÄ\xc2\x8d",
  8267. format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
  8268. EXPECT_EQ("// Я из лесу\n"
  8269. "// вышел; был\n"
  8270. "// сильный\n"
  8271. "// мороз.",
  8272. format("// Я из лесу вышел; был сильный мороз.",
  8273. getLLVMStyleWithColumns(13)));
  8274. EXPECT_EQ("// 一二三\n"
  8275. "// 四五六七\n"
  8276. "// 八 九\n"
  8277. "// 十",
  8278. format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9)));
  8279. }
  8280. TEST_F(FormatTest, SplitsUTF8BlockComments) {
  8281. EXPECT_EQ("/* Гляжу,\n"
  8282. " * поднимается\n"
  8283. " * медленно в\n"
  8284. " * гору\n"
  8285. " * Лошадка,\n"
  8286. " * везущая\n"
  8287. " * хворосту\n"
  8288. " * воз. */",
  8289. format("/* Гляжу, поднимается медленно в гору\n"
  8290. " * Лошадка, везущая хворосту воз. */",
  8291. getLLVMStyleWithColumns(13)));
  8292. EXPECT_EQ(
  8293. "/* 一二三\n"
  8294. " * 四五六七\n"
  8295. " * 八 九\n"
  8296. " * 十 */",
  8297. format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9)));
  8298. EXPECT_EQ("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
  8299. " * 𝕓𝕪𝕥𝕖\n"
  8300. " * 𝖀𝕿𝕱-𝟠 */",
  8301. format("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12)));
  8302. }
  8303. #endif // _MSC_VER
  8304. TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
  8305. FormatStyle Style = getLLVMStyle();
  8306. Style.ConstructorInitializerIndentWidth = 4;
  8307. verifyFormat(
  8308. "SomeClass::Constructor()\n"
  8309. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  8310. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  8311. Style);
  8312. Style.ConstructorInitializerIndentWidth = 2;
  8313. verifyFormat(
  8314. "SomeClass::Constructor()\n"
  8315. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  8316. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  8317. Style);
  8318. Style.ConstructorInitializerIndentWidth = 0;
  8319. verifyFormat(
  8320. "SomeClass::Constructor()\n"
  8321. ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  8322. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  8323. Style);
  8324. }
  8325. TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
  8326. FormatStyle Style = getLLVMStyle();
  8327. Style.BreakConstructorInitializersBeforeComma = true;
  8328. Style.ConstructorInitializerIndentWidth = 4;
  8329. verifyFormat("SomeClass::Constructor()\n"
  8330. " : a(a)\n"
  8331. " , b(b)\n"
  8332. " , c(c) {}",
  8333. Style);
  8334. verifyFormat("SomeClass::Constructor()\n"
  8335. " : a(a) {}",
  8336. Style);
  8337. Style.ColumnLimit = 0;
  8338. verifyFormat("SomeClass::Constructor()\n"
  8339. " : a(a) {}",
  8340. Style);
  8341. verifyFormat("SomeClass::Constructor()\n"
  8342. " : a(a)\n"
  8343. " , b(b)\n"
  8344. " , c(c) {}",
  8345. Style);
  8346. verifyFormat("SomeClass::Constructor()\n"
  8347. " : a(a) {\n"
  8348. " foo();\n"
  8349. " bar();\n"
  8350. "}",
  8351. Style);
  8352. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  8353. verifyFormat("SomeClass::Constructor()\n"
  8354. " : a(a)\n"
  8355. " , b(b)\n"
  8356. " , c(c) {\n}",
  8357. Style);
  8358. verifyFormat("SomeClass::Constructor()\n"
  8359. " : a(a) {\n}",
  8360. Style);
  8361. Style.ColumnLimit = 80;
  8362. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
  8363. Style.ConstructorInitializerIndentWidth = 2;
  8364. verifyFormat("SomeClass::Constructor()\n"
  8365. " : a(a)\n"
  8366. " , b(b)\n"
  8367. " , c(c) {}",
  8368. Style);
  8369. Style.ConstructorInitializerIndentWidth = 0;
  8370. verifyFormat("SomeClass::Constructor()\n"
  8371. ": a(a)\n"
  8372. ", b(b)\n"
  8373. ", c(c) {}",
  8374. Style);
  8375. Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  8376. Style.ConstructorInitializerIndentWidth = 4;
  8377. verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
  8378. verifyFormat(
  8379. "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
  8380. Style);
  8381. verifyFormat(
  8382. "SomeClass::Constructor()\n"
  8383. " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
  8384. Style);
  8385. Style.ConstructorInitializerIndentWidth = 4;
  8386. Style.ColumnLimit = 60;
  8387. verifyFormat("SomeClass::Constructor()\n"
  8388. " : aaaaaaaa(aaaaaaaa)\n"
  8389. " , aaaaaaaa(aaaaaaaa)\n"
  8390. " , aaaaaaaa(aaaaaaaa) {}",
  8391. Style);
  8392. }
  8393. TEST_F(FormatTest, Destructors) {
  8394. verifyFormat("void F(int &i) { i.~int(); }");
  8395. verifyFormat("void F(int &i) { i->~int(); }");
  8396. }
  8397. TEST_F(FormatTest, FormatsWithWebKitStyle) {
  8398. FormatStyle Style = getWebKitStyle();
  8399. // Don't indent in outer namespaces.
  8400. verifyFormat("namespace outer {\n"
  8401. "int i;\n"
  8402. "namespace inner {\n"
  8403. " int i;\n"
  8404. "} // namespace inner\n"
  8405. "} // namespace outer\n"
  8406. "namespace other_outer {\n"
  8407. "int i;\n"
  8408. "}",
  8409. Style);
  8410. // Don't indent case labels.
  8411. verifyFormat("switch (variable) {\n"
  8412. "case 1:\n"
  8413. "case 2:\n"
  8414. " doSomething();\n"
  8415. " break;\n"
  8416. "default:\n"
  8417. " ++variable;\n"
  8418. "}",
  8419. Style);
  8420. // Wrap before binary operators.
  8421. EXPECT_EQ(
  8422. "void f()\n"
  8423. "{\n"
  8424. " if (aaaaaaaaaaaaaaaa\n"
  8425. " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
  8426. " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  8427. " return;\n"
  8428. "}",
  8429. format(
  8430. "void f() {\n"
  8431. "if (aaaaaaaaaaaaaaaa\n"
  8432. "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
  8433. "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  8434. "return;\n"
  8435. "}",
  8436. Style));
  8437. // Allow functions on a single line.
  8438. verifyFormat("void f() { return; }", Style);
  8439. // Constructor initializers are formatted one per line with the "," on the
  8440. // new line.
  8441. verifyFormat("Constructor()\n"
  8442. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  8443. " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
  8444. " aaaaaaaaaaaaaa)\n"
  8445. " , aaaaaaaaaaaaaaaaaaaaaaa()\n"
  8446. "{\n"
  8447. "}",
  8448. Style);
  8449. verifyFormat("SomeClass::Constructor()\n"
  8450. " : a(a)\n"
  8451. "{\n"
  8452. "}",
  8453. Style);
  8454. EXPECT_EQ("SomeClass::Constructor()\n"
  8455. " : a(a)\n"
  8456. "{\n"
  8457. "}",
  8458. format("SomeClass::Constructor():a(a){}", Style));
  8459. verifyFormat("SomeClass::Constructor()\n"
  8460. " : a(a)\n"
  8461. " , b(b)\n"
  8462. " , c(c)\n"
  8463. "{\n"
  8464. "}", Style);
  8465. verifyFormat("SomeClass::Constructor()\n"
  8466. " : a(a)\n"
  8467. "{\n"
  8468. " foo();\n"
  8469. " bar();\n"
  8470. "}",
  8471. Style);
  8472. // Access specifiers should be aligned left.
  8473. verifyFormat("class C {\n"
  8474. "public:\n"
  8475. " int i;\n"
  8476. "};",
  8477. Style);
  8478. // Do not align comments.
  8479. verifyFormat("int a; // Do not\n"
  8480. "double b; // align comments.",
  8481. Style);
  8482. // Accept input's line breaks.
  8483. EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
  8484. " || bbbbbbbbbbbbbbb) {\n"
  8485. " i++;\n"
  8486. "}",
  8487. format("if (aaaaaaaaaaaaaaa\n"
  8488. "|| bbbbbbbbbbbbbbb) { i++; }",
  8489. Style));
  8490. EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
  8491. " i++;\n"
  8492. "}",
  8493. format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
  8494. // Don't automatically break all macro definitions (llvm.org/PR17842).
  8495. verifyFormat("#define aNumber 10", Style);
  8496. // However, generally keep the line breaks that the user authored.
  8497. EXPECT_EQ("#define aNumber \\\n"
  8498. " 10",
  8499. format("#define aNumber \\\n"
  8500. " 10",
  8501. Style));
  8502. // Keep empty and one-element array literals on a single line.
  8503. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
  8504. " copyItems:YES];",
  8505. format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
  8506. "copyItems:YES];",
  8507. Style));
  8508. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
  8509. " copyItems:YES];",
  8510. format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
  8511. " copyItems:YES];",
  8512. Style));
  8513. // FIXME: This does not seem right, there should be more indentation before
  8514. // the array literal's entries. Nested blocks have the same problem.
  8515. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
  8516. " @\"a\",\n"
  8517. " @\"a\"\n"
  8518. "]\n"
  8519. " copyItems:YES];",
  8520. format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
  8521. " @\"a\",\n"
  8522. " @\"a\"\n"
  8523. " ]\n"
  8524. " copyItems:YES];",
  8525. Style));
  8526. EXPECT_EQ(
  8527. "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
  8528. " copyItems:YES];",
  8529. format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
  8530. " copyItems:YES];",
  8531. Style));
  8532. verifyFormat("[self.a b:c c:d];", Style);
  8533. EXPECT_EQ("[self.a b:c\n"
  8534. " c:d];",
  8535. format("[self.a b:c\n"
  8536. "c:d];",
  8537. Style));
  8538. }
  8539. TEST_F(FormatTest, FormatsLambdas) {
  8540. verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
  8541. verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
  8542. verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
  8543. verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
  8544. verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
  8545. verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
  8546. verifyFormat("void f() {\n"
  8547. " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
  8548. "}\n");
  8549. verifyFormat("void f() {\n"
  8550. " other(x.begin(), //\n"
  8551. " x.end(), //\n"
  8552. " [&](int, int) { return 1; });\n"
  8553. "}\n");
  8554. verifyFormat("SomeFunction([]() { // A cool function...\n"
  8555. " return 43;\n"
  8556. "});");
  8557. EXPECT_EQ("SomeFunction([]() {\n"
  8558. "#define A a\n"
  8559. " return 43;\n"
  8560. "});",
  8561. format("SomeFunction([](){\n"
  8562. "#define A a\n"
  8563. "return 43;\n"
  8564. "});"));
  8565. verifyFormat("void f() {\n"
  8566. " SomeFunction([](decltype(x), A *a) {});\n"
  8567. "}");
  8568. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  8569. " [](const aaaaaaaaaa &a) { return a; });");
  8570. verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
  8571. " SomeOtherFunctioooooooooooooooooooooooooon();\n"
  8572. "});");
  8573. // Lambdas with return types.
  8574. verifyFormat("int c = []() -> int { return 2; }();\n");
  8575. verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
  8576. verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
  8577. verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
  8578. verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
  8579. verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
  8580. verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
  8581. " int j) -> int {\n"
  8582. " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
  8583. "};");
  8584. // Multiple lambdas in the same parentheses change indentation rules.
  8585. verifyFormat("SomeFunction(\n"
  8586. " []() {\n"
  8587. " int i = 42;\n"
  8588. " return i;\n"
  8589. " },\n"
  8590. " []() {\n"
  8591. " int j = 43;\n"
  8592. " return j;\n"
  8593. " });");
  8594. // More complex introducers.
  8595. verifyFormat("return [i, args...] {};");
  8596. // Not lambdas.
  8597. verifyFormat("constexpr char hello[]{\"hello\"};");
  8598. verifyFormat("double &operator[](int i) { return 0; }\n"
  8599. "int i;");
  8600. verifyFormat("std::unique_ptr<int[]> foo() {}");
  8601. verifyFormat("int i = a[a][a]->f();");
  8602. verifyFormat("int i = (*b)[a]->f();");
  8603. // Other corner cases.
  8604. verifyFormat("void f() {\n"
  8605. " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
  8606. " );\n"
  8607. "}");
  8608. // Lambdas created through weird macros.
  8609. verifyFormat("void f() {\n"
  8610. " MACRO((const AA &a) { return 1; });\n"
  8611. "}");
  8612. }
  8613. TEST_F(FormatTest, FormatsBlocks) {
  8614. FormatStyle ShortBlocks = getLLVMStyle();
  8615. ShortBlocks.AllowShortBlocksOnASingleLine = true;
  8616. verifyFormat("int (^Block)(int, int);", ShortBlocks);
  8617. verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
  8618. verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
  8619. verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
  8620. verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
  8621. verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
  8622. verifyFormat("foo(^{ bar(); });", ShortBlocks);
  8623. verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
  8624. verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
  8625. verifyFormat("[operation setCompletionBlock:^{\n"
  8626. " [self onOperationDone];\n"
  8627. "}];");
  8628. verifyFormat("int i = {[operation setCompletionBlock:^{\n"
  8629. " [self onOperationDone];\n"
  8630. "}]};");
  8631. verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
  8632. " f();\n"
  8633. "}];");
  8634. verifyFormat("int a = [operation block:^int(int *i) {\n"
  8635. " return 1;\n"
  8636. "}];");
  8637. verifyFormat("[myObject doSomethingWith:arg1\n"
  8638. " aaa:^int(int *a) {\n"
  8639. " return 1;\n"
  8640. " }\n"
  8641. " bbb:f(a * bbbbbbbb)];");
  8642. verifyFormat("[operation setCompletionBlock:^{\n"
  8643. " [self.delegate newDataAvailable];\n"
  8644. "}];",
  8645. getLLVMStyleWithColumns(60));
  8646. verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
  8647. " NSString *path = [self sessionFilePath];\n"
  8648. " if (path) {\n"
  8649. " // ...\n"
  8650. " }\n"
  8651. "});");
  8652. verifyFormat("[[SessionService sharedService]\n"
  8653. " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  8654. " if (window) {\n"
  8655. " [self windowDidLoad:window];\n"
  8656. " } else {\n"
  8657. " [self errorLoadingWindow];\n"
  8658. " }\n"
  8659. " }];");
  8660. verifyFormat("void (^largeBlock)(void) = ^{\n"
  8661. " // ...\n"
  8662. "};\n",
  8663. getLLVMStyleWithColumns(40));
  8664. verifyFormat("[[SessionService sharedService]\n"
  8665. " loadWindowWithCompletionBlock: //\n"
  8666. " ^(SessionWindow *window) {\n"
  8667. " if (window) {\n"
  8668. " [self windowDidLoad:window];\n"
  8669. " } else {\n"
  8670. " [self errorLoadingWindow];\n"
  8671. " }\n"
  8672. " }];",
  8673. getLLVMStyleWithColumns(60));
  8674. verifyFormat("[myObject doSomethingWith:arg1\n"
  8675. " firstBlock:^(Foo *a) {\n"
  8676. " // ...\n"
  8677. " int i;\n"
  8678. " }\n"
  8679. " secondBlock:^(Bar *b) {\n"
  8680. " // ...\n"
  8681. " int i;\n"
  8682. " }\n"
  8683. " thirdBlock:^Foo(Bar *b) {\n"
  8684. " // ...\n"
  8685. " int i;\n"
  8686. " }];");
  8687. verifyFormat("[myObject doSomethingWith:arg1\n"
  8688. " firstBlock:-1\n"
  8689. " secondBlock:^(Bar *b) {\n"
  8690. " // ...\n"
  8691. " int i;\n"
  8692. " }];");
  8693. verifyFormat("f(^{\n"
  8694. " @autoreleasepool {\n"
  8695. " if (a) {\n"
  8696. " g();\n"
  8697. " }\n"
  8698. " }\n"
  8699. "});");
  8700. FormatStyle FourIndent = getLLVMStyle();
  8701. FourIndent.ObjCBlockIndentWidth = 4;
  8702. verifyFormat("[operation setCompletionBlock:^{\n"
  8703. " [self onOperationDone];\n"
  8704. "}];",
  8705. FourIndent);
  8706. }
  8707. TEST_F(FormatTest, SupportsCRLF) {
  8708. EXPECT_EQ("int a;\r\n"
  8709. "int b;\r\n"
  8710. "int c;\r\n",
  8711. format("int a;\r\n"
  8712. " int b;\r\n"
  8713. " int c;\r\n",
  8714. getLLVMStyle()));
  8715. EXPECT_EQ("int a;\r\n"
  8716. "int b;\r\n"
  8717. "int c;\r\n",
  8718. format("int a;\r\n"
  8719. " int b;\n"
  8720. " int c;\r\n",
  8721. getLLVMStyle()));
  8722. EXPECT_EQ("int a;\n"
  8723. "int b;\n"
  8724. "int c;\n",
  8725. format("int a;\r\n"
  8726. " int b;\n"
  8727. " int c;\n",
  8728. getLLVMStyle()));
  8729. EXPECT_EQ("\"aaaaaaa \"\r\n"
  8730. "\"bbbbbbb\";\r\n",
  8731. format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
  8732. EXPECT_EQ("#define A \\\r\n"
  8733. " b; \\\r\n"
  8734. " c; \\\r\n"
  8735. " d;\r\n",
  8736. format("#define A \\\r\n"
  8737. " b; \\\r\n"
  8738. " c; d; \r\n",
  8739. getGoogleStyle()));
  8740. EXPECT_EQ("/*\r\n"
  8741. "multi line block comments\r\n"
  8742. "should not introduce\r\n"
  8743. "an extra carriage return\r\n"
  8744. "*/\r\n",
  8745. format("/*\r\n"
  8746. "multi line block comments\r\n"
  8747. "should not introduce\r\n"
  8748. "an extra carriage return\r\n"
  8749. "*/\r\n"));
  8750. }
  8751. TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
  8752. verifyFormat("MY_CLASS(C) {\n"
  8753. " int i;\n"
  8754. " int j;\n"
  8755. "};");
  8756. }
  8757. TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
  8758. FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
  8759. TwoIndent.ContinuationIndentWidth = 2;
  8760. EXPECT_EQ("int i =\n"
  8761. " longFunction(\n"
  8762. " arg);",
  8763. format("int i = longFunction(arg);", TwoIndent));
  8764. FormatStyle SixIndent = getLLVMStyleWithColumns(20);
  8765. SixIndent.ContinuationIndentWidth = 6;
  8766. EXPECT_EQ("int i =\n"
  8767. " longFunction(\n"
  8768. " arg);",
  8769. format("int i = longFunction(arg);", SixIndent));
  8770. }
  8771. TEST_F(FormatTest, SpacesInAngles) {
  8772. FormatStyle Spaces = getLLVMStyle();
  8773. Spaces.SpacesInAngles = true;
  8774. verifyFormat("static_cast< int >(arg);", Spaces);
  8775. verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
  8776. verifyFormat("f< int, float >();", Spaces);
  8777. verifyFormat("template <> g() {}", Spaces);
  8778. verifyFormat("template < std::vector< int > > f() {}", Spaces);
  8779. Spaces.Standard = FormatStyle::LS_Cpp03;
  8780. Spaces.SpacesInAngles = true;
  8781. verifyFormat("A< A< int > >();", Spaces);
  8782. Spaces.SpacesInAngles = false;
  8783. verifyFormat("A<A<int> >();", Spaces);
  8784. Spaces.Standard = FormatStyle::LS_Cpp11;
  8785. Spaces.SpacesInAngles = true;
  8786. verifyFormat("A< A< int > >();", Spaces);
  8787. Spaces.SpacesInAngles = false;
  8788. verifyFormat("A<A<int>>();", Spaces);
  8789. }
  8790. TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
  8791. std::string code = "#if A\n"
  8792. "#if B\n"
  8793. "a.\n"
  8794. "#endif\n"
  8795. " a = 1;\n"
  8796. "#else\n"
  8797. "#endif\n"
  8798. "#if C\n"
  8799. "#else\n"
  8800. "#endif\n";
  8801. EXPECT_EQ(code, format(code));
  8802. }
  8803. TEST_F(FormatTest, HandleConflictMarkers) {
  8804. // Git/SVN conflict markers.
  8805. EXPECT_EQ("int a;\n"
  8806. "void f() {\n"
  8807. " callme(some(parameter1,\n"
  8808. "<<<<<<< text by the vcs\n"
  8809. " parameter2),\n"
  8810. "||||||| text by the vcs\n"
  8811. " parameter2),\n"
  8812. " parameter3,\n"
  8813. "======= text by the vcs\n"
  8814. " parameter2, parameter3),\n"
  8815. ">>>>>>> text by the vcs\n"
  8816. " otherparameter);\n",
  8817. format("int a;\n"
  8818. "void f() {\n"
  8819. " callme(some(parameter1,\n"
  8820. "<<<<<<< text by the vcs\n"
  8821. " parameter2),\n"
  8822. "||||||| text by the vcs\n"
  8823. " parameter2),\n"
  8824. " parameter3,\n"
  8825. "======= text by the vcs\n"
  8826. " parameter2,\n"
  8827. " parameter3),\n"
  8828. ">>>>>>> text by the vcs\n"
  8829. " otherparameter);\n"));
  8830. // Perforce markers.
  8831. EXPECT_EQ("void f() {\n"
  8832. " function(\n"
  8833. ">>>> text by the vcs\n"
  8834. " parameter,\n"
  8835. "==== text by the vcs\n"
  8836. " parameter,\n"
  8837. "==== text by the vcs\n"
  8838. " parameter,\n"
  8839. "<<<< text by the vcs\n"
  8840. " parameter);\n",
  8841. format("void f() {\n"
  8842. " function(\n"
  8843. ">>>> text by the vcs\n"
  8844. " parameter,\n"
  8845. "==== text by the vcs\n"
  8846. " parameter,\n"
  8847. "==== text by the vcs\n"
  8848. " parameter,\n"
  8849. "<<<< text by the vcs\n"
  8850. " parameter);\n"));
  8851. EXPECT_EQ("<<<<<<<\n"
  8852. "|||||||\n"
  8853. "=======\n"
  8854. ">>>>>>>",
  8855. format("<<<<<<<\n"
  8856. "|||||||\n"
  8857. "=======\n"
  8858. ">>>>>>>"));
  8859. EXPECT_EQ("<<<<<<<\n"
  8860. "|||||||\n"
  8861. "int i;\n"
  8862. "=======\n"
  8863. ">>>>>>>",
  8864. format("<<<<<<<\n"
  8865. "|||||||\n"
  8866. "int i;\n"
  8867. "=======\n"
  8868. ">>>>>>>"));
  8869. // FIXME: Handle parsing of macros around conflict markers correctly:
  8870. EXPECT_EQ("#define Macro \\\n"
  8871. "<<<<<<<\n"
  8872. "Something \\\n"
  8873. "|||||||\n"
  8874. "Else \\\n"
  8875. "=======\n"
  8876. "Other \\\n"
  8877. ">>>>>>>\n"
  8878. " End int i;\n",
  8879. format("#define Macro \\\n"
  8880. "<<<<<<<\n"
  8881. " Something \\\n"
  8882. "|||||||\n"
  8883. " Else \\\n"
  8884. "=======\n"
  8885. " Other \\\n"
  8886. ">>>>>>>\n"
  8887. " End\n"
  8888. "int i;\n"));
  8889. }
  8890. TEST_F(FormatTest, DisableRegions) {
  8891. EXPECT_EQ("int i;\n"
  8892. "// clang-format off\n"
  8893. " int j;\n"
  8894. "// clang-format on\n"
  8895. "int k;",
  8896. format(" int i;\n"
  8897. " // clang-format off\n"
  8898. " int j;\n"
  8899. " // clang-format on\n"
  8900. " int k;"));
  8901. EXPECT_EQ("int i;\n"
  8902. "/* clang-format off */\n"
  8903. " int j;\n"
  8904. "/* clang-format on */\n"
  8905. "int k;",
  8906. format(" int i;\n"
  8907. " /* clang-format off */\n"
  8908. " int j;\n"
  8909. " /* clang-format on */\n"
  8910. " int k;"));
  8911. }
  8912. TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
  8913. format("? ) =");
  8914. }
  8915. } // end namespace tooling
  8916. } // end namespace clang