FormatTest.cpp 434 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595
  1. //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "clang/Format/Format.h"
  10. #include "../Tooling/RewriterTestContext.h"
  11. #include "FormatTestUtils.h"
  12. #include "clang/Frontend/TextDiagnosticPrinter.h"
  13. #include "llvm/Support/Debug.h"
  14. #include "llvm/Support/MemoryBuffer.h"
  15. #include "gtest/gtest.h"
  16. #define DEBUG_TYPE "format-test"
  17. namespace clang {
  18. namespace format {
  19. namespace {
  20. FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
  21. class FormatTest : public ::testing::Test {
  22. protected:
  23. enum IncompleteCheck {
  24. IC_ExpectComplete,
  25. IC_ExpectIncomplete,
  26. IC_DoNotCheck
  27. };
  28. std::string format(llvm::StringRef Code,
  29. const FormatStyle &Style = getLLVMStyle(),
  30. IncompleteCheck CheckIncomplete = IC_ExpectComplete) {
  31. DEBUG(llvm::errs() << "---\n");
  32. DEBUG(llvm::errs() << Code << "\n\n");
  33. std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
  34. bool IncompleteFormat = false;
  35. tooling::Replacements Replaces =
  36. reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
  37. if (CheckIncomplete != IC_DoNotCheck) {
  38. bool ExpectedIncompleteFormat = CheckIncomplete == IC_ExpectIncomplete;
  39. EXPECT_EQ(ExpectedIncompleteFormat, IncompleteFormat) << Code << "\n\n";
  40. }
  41. ReplacementCount = Replaces.size();
  42. auto Result = applyAllReplacements(Code, Replaces);
  43. EXPECT_TRUE(static_cast<bool>(Result));
  44. DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
  45. return *Result;
  46. }
  47. FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
  48. FormatStyle Style = getLLVMStyle();
  49. Style.ColumnLimit = ColumnLimit;
  50. return Style;
  51. }
  52. FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
  53. FormatStyle Style = getGoogleStyle();
  54. Style.ColumnLimit = ColumnLimit;
  55. return Style;
  56. }
  57. void verifyFormat(llvm::StringRef Code,
  58. const FormatStyle &Style = getLLVMStyle()) {
  59. EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
  60. }
  61. void verifyIncompleteFormat(llvm::StringRef Code,
  62. const FormatStyle &Style = getLLVMStyle()) {
  63. EXPECT_EQ(Code.str(),
  64. format(test::messUp(Code), Style, IC_ExpectIncomplete));
  65. }
  66. void verifyGoogleFormat(llvm::StringRef Code) {
  67. verifyFormat(Code, getGoogleStyle());
  68. }
  69. void verifyIndependentOfContext(llvm::StringRef text) {
  70. verifyFormat(text);
  71. verifyFormat(llvm::Twine("void f() { " + text + " }").str());
  72. }
  73. /// \brief Verify that clang-format does not crash on the given input.
  74. void verifyNoCrash(llvm::StringRef Code,
  75. const FormatStyle &Style = getLLVMStyle()) {
  76. format(Code, Style, IC_DoNotCheck);
  77. }
  78. int ReplacementCount;
  79. };
  80. TEST_F(FormatTest, MessUp) {
  81. EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
  82. EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
  83. EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
  84. EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
  85. EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
  86. }
  87. //===----------------------------------------------------------------------===//
  88. // Basic function tests.
  89. //===----------------------------------------------------------------------===//
  90. TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
  91. EXPECT_EQ(";", format(";"));
  92. }
  93. TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
  94. EXPECT_EQ("int i;", format(" int i;"));
  95. EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;"));
  96. EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
  97. EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
  98. }
  99. TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
  100. EXPECT_EQ("int i;", format("int\ni;"));
  101. }
  102. TEST_F(FormatTest, FormatsNestedBlockStatements) {
  103. EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
  104. }
  105. TEST_F(FormatTest, FormatsNestedCall) {
  106. verifyFormat("Method(f1, f2(f3));");
  107. verifyFormat("Method(f1(f2, f3()));");
  108. verifyFormat("Method(f1(f2, (f3())));");
  109. }
  110. TEST_F(FormatTest, NestedNameSpecifiers) {
  111. verifyFormat("vector<::Type> v;");
  112. verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
  113. verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
  114. verifyFormat("bool a = 2 < ::SomeFunction();");
  115. }
  116. TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
  117. EXPECT_EQ("if (a) {\n"
  118. " f();\n"
  119. "}",
  120. format("if(a){f();}"));
  121. EXPECT_EQ(4, ReplacementCount);
  122. EXPECT_EQ("if (a) {\n"
  123. " f();\n"
  124. "}",
  125. format("if (a) {\n"
  126. " f();\n"
  127. "}"));
  128. EXPECT_EQ(0, ReplacementCount);
  129. EXPECT_EQ("/*\r\n"
  130. "\r\n"
  131. "*/\r\n",
  132. format("/*\r\n"
  133. "\r\n"
  134. "*/\r\n"));
  135. EXPECT_EQ(0, ReplacementCount);
  136. }
  137. TEST_F(FormatTest, RemovesEmptyLines) {
  138. EXPECT_EQ("class C {\n"
  139. " int i;\n"
  140. "};",
  141. format("class C {\n"
  142. " int i;\n"
  143. "\n"
  144. "};"));
  145. // Don't remove empty lines at the start of namespaces or extern "C" blocks.
  146. EXPECT_EQ("namespace N {\n"
  147. "\n"
  148. "int i;\n"
  149. "}",
  150. format("namespace N {\n"
  151. "\n"
  152. "int i;\n"
  153. "}",
  154. getGoogleStyle()));
  155. EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
  156. "\n"
  157. "int i;\n"
  158. "}",
  159. format("extern /**/ \"C\" /**/ {\n"
  160. "\n"
  161. "int i;\n"
  162. "}",
  163. getGoogleStyle()));
  164. // ...but do keep inlining and removing empty lines for non-block extern "C"
  165. // functions.
  166. verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
  167. EXPECT_EQ("extern \"C\" int f() {\n"
  168. " int i = 42;\n"
  169. " return i;\n"
  170. "}",
  171. format("extern \"C\" int f() {\n"
  172. "\n"
  173. " int i = 42;\n"
  174. " return i;\n"
  175. "}",
  176. getGoogleStyle()));
  177. // Remove empty lines at the beginning and end of blocks.
  178. EXPECT_EQ("void f() {\n"
  179. "\n"
  180. " if (a) {\n"
  181. "\n"
  182. " f();\n"
  183. " }\n"
  184. "}",
  185. format("void f() {\n"
  186. "\n"
  187. " if (a) {\n"
  188. "\n"
  189. " f();\n"
  190. "\n"
  191. " }\n"
  192. "\n"
  193. "}",
  194. getLLVMStyle()));
  195. EXPECT_EQ("void f() {\n"
  196. " if (a) {\n"
  197. " f();\n"
  198. " }\n"
  199. "}",
  200. format("void f() {\n"
  201. "\n"
  202. " if (a) {\n"
  203. "\n"
  204. " f();\n"
  205. "\n"
  206. " }\n"
  207. "\n"
  208. "}",
  209. getGoogleStyle()));
  210. // Don't remove empty lines in more complex control statements.
  211. EXPECT_EQ("void f() {\n"
  212. " if (a) {\n"
  213. " f();\n"
  214. "\n"
  215. " } else if (b) {\n"
  216. " f();\n"
  217. " }\n"
  218. "}",
  219. format("void f() {\n"
  220. " if (a) {\n"
  221. " f();\n"
  222. "\n"
  223. " } else if (b) {\n"
  224. " f();\n"
  225. "\n"
  226. " }\n"
  227. "\n"
  228. "}"));
  229. // FIXME: This is slightly inconsistent.
  230. EXPECT_EQ("namespace {\n"
  231. "int i;\n"
  232. "}",
  233. format("namespace {\n"
  234. "int i;\n"
  235. "\n"
  236. "}"));
  237. EXPECT_EQ("namespace {\n"
  238. "int i;\n"
  239. "\n"
  240. "} // namespace",
  241. format("namespace {\n"
  242. "int i;\n"
  243. "\n"
  244. "} // namespace"));
  245. }
  246. TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
  247. verifyFormat("x = (a) and (b);");
  248. verifyFormat("x = (a) or (b);");
  249. verifyFormat("x = (a) bitand (b);");
  250. verifyFormat("x = (a) bitor (b);");
  251. verifyFormat("x = (a) not_eq (b);");
  252. verifyFormat("x = (a) and_eq (b);");
  253. verifyFormat("x = (a) or_eq (b);");
  254. verifyFormat("x = (a) xor (b);");
  255. }
  256. //===----------------------------------------------------------------------===//
  257. // Tests for control statements.
  258. //===----------------------------------------------------------------------===//
  259. TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
  260. verifyFormat("if (true)\n f();\ng();");
  261. verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
  262. verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
  263. FormatStyle AllowsMergedIf = getLLVMStyle();
  264. AllowsMergedIf.AlignEscapedNewlinesLeft = true;
  265. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  266. verifyFormat("if (a)\n"
  267. " // comment\n"
  268. " f();",
  269. AllowsMergedIf);
  270. verifyFormat("{\n"
  271. " if (a)\n"
  272. " label:\n"
  273. " f();\n"
  274. "}",
  275. AllowsMergedIf);
  276. verifyFormat("#define A \\\n"
  277. " if (a) \\\n"
  278. " label: \\\n"
  279. " f()",
  280. AllowsMergedIf);
  281. verifyFormat("if (a)\n"
  282. " ;",
  283. AllowsMergedIf);
  284. verifyFormat("if (a)\n"
  285. " if (b) return;",
  286. AllowsMergedIf);
  287. verifyFormat("if (a) // Can't merge this\n"
  288. " f();\n",
  289. AllowsMergedIf);
  290. verifyFormat("if (a) /* still don't merge */\n"
  291. " f();",
  292. AllowsMergedIf);
  293. verifyFormat("if (a) { // Never merge this\n"
  294. " f();\n"
  295. "}",
  296. AllowsMergedIf);
  297. verifyFormat("if (a) { /* Never merge this */\n"
  298. " f();\n"
  299. "}",
  300. AllowsMergedIf);
  301. AllowsMergedIf.ColumnLimit = 14;
  302. verifyFormat("if (a) return;", AllowsMergedIf);
  303. verifyFormat("if (aaaaaaaaa)\n"
  304. " return;",
  305. AllowsMergedIf);
  306. AllowsMergedIf.ColumnLimit = 13;
  307. verifyFormat("if (a)\n return;", AllowsMergedIf);
  308. }
  309. TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
  310. FormatStyle AllowsMergedLoops = getLLVMStyle();
  311. AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
  312. verifyFormat("while (true) continue;", AllowsMergedLoops);
  313. verifyFormat("for (;;) continue;", AllowsMergedLoops);
  314. verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
  315. verifyFormat("while (true)\n"
  316. " ;",
  317. AllowsMergedLoops);
  318. verifyFormat("for (;;)\n"
  319. " ;",
  320. AllowsMergedLoops);
  321. verifyFormat("for (;;)\n"
  322. " for (;;) continue;",
  323. AllowsMergedLoops);
  324. verifyFormat("for (;;) // Can't merge this\n"
  325. " continue;",
  326. AllowsMergedLoops);
  327. verifyFormat("for (;;) /* still don't merge */\n"
  328. " continue;",
  329. AllowsMergedLoops);
  330. }
  331. TEST_F(FormatTest, FormatShortBracedStatements) {
  332. FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
  333. AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
  334. AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
  335. AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
  336. verifyFormat("if (true) {}", AllowSimpleBracedStatements);
  337. verifyFormat("while (true) {}", AllowSimpleBracedStatements);
  338. verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
  339. verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
  340. verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
  341. verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
  342. verifyFormat("if (true) { //\n"
  343. " f();\n"
  344. "}",
  345. AllowSimpleBracedStatements);
  346. verifyFormat("if (true) {\n"
  347. " f();\n"
  348. " f();\n"
  349. "}",
  350. AllowSimpleBracedStatements);
  351. verifyFormat("if (true) {\n"
  352. " f();\n"
  353. "} else {\n"
  354. " f();\n"
  355. "}",
  356. AllowSimpleBracedStatements);
  357. verifyFormat("template <int> struct A2 {\n"
  358. " struct B {};\n"
  359. "};",
  360. AllowSimpleBracedStatements);
  361. AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
  362. verifyFormat("if (true) {\n"
  363. " f();\n"
  364. "}",
  365. AllowSimpleBracedStatements);
  366. verifyFormat("if (true) {\n"
  367. " f();\n"
  368. "} else {\n"
  369. " f();\n"
  370. "}",
  371. AllowSimpleBracedStatements);
  372. AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
  373. verifyFormat("while (true) {\n"
  374. " f();\n"
  375. "}",
  376. AllowSimpleBracedStatements);
  377. verifyFormat("for (;;) {\n"
  378. " f();\n"
  379. "}",
  380. AllowSimpleBracedStatements);
  381. }
  382. TEST_F(FormatTest, ParseIfElse) {
  383. verifyFormat("if (true)\n"
  384. " if (true)\n"
  385. " if (true)\n"
  386. " f();\n"
  387. " else\n"
  388. " g();\n"
  389. " else\n"
  390. " h();\n"
  391. "else\n"
  392. " i();");
  393. verifyFormat("if (true)\n"
  394. " if (true)\n"
  395. " if (true) {\n"
  396. " if (true)\n"
  397. " f();\n"
  398. " } else {\n"
  399. " g();\n"
  400. " }\n"
  401. " else\n"
  402. " h();\n"
  403. "else {\n"
  404. " i();\n"
  405. "}");
  406. verifyFormat("void f() {\n"
  407. " if (a) {\n"
  408. " } else {\n"
  409. " }\n"
  410. "}");
  411. }
  412. TEST_F(FormatTest, ElseIf) {
  413. verifyFormat("if (a) {\n} else if (b) {\n}");
  414. verifyFormat("if (a)\n"
  415. " f();\n"
  416. "else if (b)\n"
  417. " g();\n"
  418. "else\n"
  419. " h();");
  420. verifyFormat("if (a) {\n"
  421. " f();\n"
  422. "}\n"
  423. "// or else ..\n"
  424. "else {\n"
  425. " g()\n"
  426. "}");
  427. verifyFormat("if (a) {\n"
  428. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  429. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  430. "}");
  431. verifyFormat("if (a) {\n"
  432. "} else if (\n"
  433. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  434. "}",
  435. getLLVMStyleWithColumns(62));
  436. }
  437. TEST_F(FormatTest, FormatsForLoop) {
  438. verifyFormat(
  439. "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
  440. " ++VeryVeryLongLoopVariable)\n"
  441. " ;");
  442. verifyFormat("for (;;)\n"
  443. " f();");
  444. verifyFormat("for (;;) {\n}");
  445. verifyFormat("for (;;) {\n"
  446. " f();\n"
  447. "}");
  448. verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
  449. verifyFormat(
  450. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  451. " E = UnwrappedLines.end();\n"
  452. " I != E; ++I) {\n}");
  453. verifyFormat(
  454. "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
  455. " ++IIIII) {\n}");
  456. verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
  457. " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
  458. " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
  459. verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
  460. " I = FD->getDeclsInPrototypeScope().begin(),\n"
  461. " E = FD->getDeclsInPrototypeScope().end();\n"
  462. " I != E; ++I) {\n}");
  463. verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
  464. " I = Container.begin(),\n"
  465. " E = Container.end();\n"
  466. " I != E; ++I) {\n}",
  467. getLLVMStyleWithColumns(76));
  468. verifyFormat(
  469. "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  470. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
  471. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  472. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  473. " ++aaaaaaaaaaa) {\n}");
  474. verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  475. " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
  476. " ++i) {\n}");
  477. verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
  478. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  479. "}");
  480. verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
  481. " aaaaaaaaaa);\n"
  482. " iter; ++iter) {\n"
  483. "}");
  484. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  485. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  486. " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
  487. " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
  488. FormatStyle NoBinPacking = getLLVMStyle();
  489. NoBinPacking.BinPackParameters = false;
  490. verifyFormat("for (int aaaaaaaaaaa = 1;\n"
  491. " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
  492. " aaaaaaaaaaaaaaaa,\n"
  493. " aaaaaaaaaaaaaaaa,\n"
  494. " aaaaaaaaaaaaaaaa);\n"
  495. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  496. "}",
  497. NoBinPacking);
  498. verifyFormat(
  499. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  500. " E = UnwrappedLines.end();\n"
  501. " I != E;\n"
  502. " ++I) {\n}",
  503. NoBinPacking);
  504. }
  505. TEST_F(FormatTest, RangeBasedForLoops) {
  506. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  507. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  508. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
  509. " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
  510. verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
  511. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  512. verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
  513. " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
  514. }
  515. TEST_F(FormatTest, ForEachLoops) {
  516. verifyFormat("void f() {\n"
  517. " foreach (Item *item, itemlist) {}\n"
  518. " Q_FOREACH (Item *item, itemlist) {}\n"
  519. " BOOST_FOREACH (Item *item, itemlist) {}\n"
  520. " UNKNOWN_FORACH(Item * item, itemlist) {}\n"
  521. "}");
  522. // As function-like macros.
  523. verifyFormat("#define foreach(x, y)\n"
  524. "#define Q_FOREACH(x, y)\n"
  525. "#define BOOST_FOREACH(x, y)\n"
  526. "#define UNKNOWN_FOREACH(x, y)\n");
  527. // Not as function-like macros.
  528. verifyFormat("#define foreach (x, y)\n"
  529. "#define Q_FOREACH (x, y)\n"
  530. "#define BOOST_FOREACH (x, y)\n"
  531. "#define UNKNOWN_FOREACH (x, y)\n");
  532. }
  533. TEST_F(FormatTest, FormatsWhileLoop) {
  534. verifyFormat("while (true) {\n}");
  535. verifyFormat("while (true)\n"
  536. " f();");
  537. verifyFormat("while () {\n}");
  538. verifyFormat("while () {\n"
  539. " f();\n"
  540. "}");
  541. }
  542. TEST_F(FormatTest, FormatsDoWhile) {
  543. verifyFormat("do {\n"
  544. " do_something();\n"
  545. "} while (something());");
  546. verifyFormat("do\n"
  547. " do_something();\n"
  548. "while (something());");
  549. }
  550. TEST_F(FormatTest, FormatsSwitchStatement) {
  551. verifyFormat("switch (x) {\n"
  552. "case 1:\n"
  553. " f();\n"
  554. " break;\n"
  555. "case kFoo:\n"
  556. "case ns::kBar:\n"
  557. "case kBaz:\n"
  558. " break;\n"
  559. "default:\n"
  560. " g();\n"
  561. " break;\n"
  562. "}");
  563. verifyFormat("switch (x) {\n"
  564. "case 1: {\n"
  565. " f();\n"
  566. " break;\n"
  567. "}\n"
  568. "case 2: {\n"
  569. " break;\n"
  570. "}\n"
  571. "}");
  572. verifyFormat("switch (x) {\n"
  573. "case 1: {\n"
  574. " f();\n"
  575. " {\n"
  576. " g();\n"
  577. " h();\n"
  578. " }\n"
  579. " break;\n"
  580. "}\n"
  581. "}");
  582. verifyFormat("switch (x) {\n"
  583. "case 1: {\n"
  584. " f();\n"
  585. " if (foo) {\n"
  586. " g();\n"
  587. " h();\n"
  588. " }\n"
  589. " break;\n"
  590. "}\n"
  591. "}");
  592. verifyFormat("switch (x) {\n"
  593. "case 1: {\n"
  594. " f();\n"
  595. " g();\n"
  596. "} break;\n"
  597. "}");
  598. verifyFormat("switch (test)\n"
  599. " ;");
  600. verifyFormat("switch (x) {\n"
  601. "default: {\n"
  602. " // Do nothing.\n"
  603. "}\n"
  604. "}");
  605. verifyFormat("switch (x) {\n"
  606. "// comment\n"
  607. "// if 1, do f()\n"
  608. "case 1:\n"
  609. " f();\n"
  610. "}");
  611. verifyFormat("switch (x) {\n"
  612. "case 1:\n"
  613. " // Do amazing stuff\n"
  614. " {\n"
  615. " f();\n"
  616. " g();\n"
  617. " }\n"
  618. " break;\n"
  619. "}");
  620. verifyFormat("#define A \\\n"
  621. " switch (x) { \\\n"
  622. " case a: \\\n"
  623. " foo = b; \\\n"
  624. " }",
  625. getLLVMStyleWithColumns(20));
  626. verifyFormat("#define OPERATION_CASE(name) \\\n"
  627. " case OP_name: \\\n"
  628. " return operations::Operation##name\n",
  629. getLLVMStyleWithColumns(40));
  630. verifyFormat("switch (x) {\n"
  631. "case 1:;\n"
  632. "default:;\n"
  633. " int i;\n"
  634. "}");
  635. verifyGoogleFormat("switch (x) {\n"
  636. " case 1:\n"
  637. " f();\n"
  638. " break;\n"
  639. " case kFoo:\n"
  640. " case ns::kBar:\n"
  641. " case kBaz:\n"
  642. " break;\n"
  643. " default:\n"
  644. " g();\n"
  645. " break;\n"
  646. "}");
  647. verifyGoogleFormat("switch (x) {\n"
  648. " case 1: {\n"
  649. " f();\n"
  650. " break;\n"
  651. " }\n"
  652. "}");
  653. verifyGoogleFormat("switch (test)\n"
  654. " ;");
  655. verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
  656. " case OP_name: \\\n"
  657. " return operations::Operation##name\n");
  658. verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
  659. " // Get the correction operation class.\n"
  660. " switch (OpCode) {\n"
  661. " CASE(Add);\n"
  662. " CASE(Subtract);\n"
  663. " default:\n"
  664. " return operations::Unknown;\n"
  665. " }\n"
  666. "#undef OPERATION_CASE\n"
  667. "}");
  668. verifyFormat("DEBUG({\n"
  669. " switch (x) {\n"
  670. " case A:\n"
  671. " f();\n"
  672. " break;\n"
  673. " // On B:\n"
  674. " case B:\n"
  675. " g();\n"
  676. " break;\n"
  677. " }\n"
  678. "});");
  679. verifyFormat("switch (a) {\n"
  680. "case (b):\n"
  681. " return;\n"
  682. "}");
  683. verifyFormat("switch (a) {\n"
  684. "case some_namespace::\n"
  685. " some_constant:\n"
  686. " return;\n"
  687. "}",
  688. getLLVMStyleWithColumns(34));
  689. }
  690. TEST_F(FormatTest, CaseRanges) {
  691. verifyFormat("switch (x) {\n"
  692. "case 'A' ... 'Z':\n"
  693. "case 1 ... 5:\n"
  694. "case a ... b:\n"
  695. " break;\n"
  696. "}");
  697. }
  698. TEST_F(FormatTest, ShortCaseLabels) {
  699. FormatStyle Style = getLLVMStyle();
  700. Style.AllowShortCaseLabelsOnASingleLine = true;
  701. verifyFormat("switch (a) {\n"
  702. "case 1: x = 1; break;\n"
  703. "case 2: return;\n"
  704. "case 3:\n"
  705. "case 4:\n"
  706. "case 5: return;\n"
  707. "case 6: // comment\n"
  708. " return;\n"
  709. "case 7:\n"
  710. " // comment\n"
  711. " return;\n"
  712. "case 8:\n"
  713. " x = 8; // comment\n"
  714. " break;\n"
  715. "default: y = 1; break;\n"
  716. "}",
  717. Style);
  718. verifyFormat("switch (a) {\n"
  719. "#if FOO\n"
  720. "case 0: return 0;\n"
  721. "#endif\n"
  722. "}",
  723. Style);
  724. verifyFormat("switch (a) {\n"
  725. "case 1: {\n"
  726. "}\n"
  727. "case 2: {\n"
  728. " return;\n"
  729. "}\n"
  730. "case 3: {\n"
  731. " x = 1;\n"
  732. " return;\n"
  733. "}\n"
  734. "case 4:\n"
  735. " if (x)\n"
  736. " return;\n"
  737. "}",
  738. Style);
  739. Style.ColumnLimit = 21;
  740. verifyFormat("switch (a) {\n"
  741. "case 1: x = 1; break;\n"
  742. "case 2: return;\n"
  743. "case 3:\n"
  744. "case 4:\n"
  745. "case 5: return;\n"
  746. "default:\n"
  747. " y = 1;\n"
  748. " break;\n"
  749. "}",
  750. Style);
  751. }
  752. TEST_F(FormatTest, FormatsLabels) {
  753. verifyFormat("void f() {\n"
  754. " some_code();\n"
  755. "test_label:\n"
  756. " some_other_code();\n"
  757. " {\n"
  758. " some_more_code();\n"
  759. " another_label:\n"
  760. " some_more_code();\n"
  761. " }\n"
  762. "}");
  763. verifyFormat("{\n"
  764. " some_code();\n"
  765. "test_label:\n"
  766. " some_other_code();\n"
  767. "}");
  768. verifyFormat("{\n"
  769. " some_code();\n"
  770. "test_label:;\n"
  771. " int i = 0;\n"
  772. "}");
  773. }
  774. //===----------------------------------------------------------------------===//
  775. // Tests for comments.
  776. //===----------------------------------------------------------------------===//
  777. TEST_F(FormatTest, UnderstandsSingleLineComments) {
  778. verifyFormat("//* */");
  779. verifyFormat("// line 1\n"
  780. "// line 2\n"
  781. "void f() {}\n");
  782. verifyFormat("void f() {\n"
  783. " // Doesn't do anything\n"
  784. "}");
  785. verifyFormat("SomeObject\n"
  786. " // Calling someFunction on SomeObject\n"
  787. " .someFunction();");
  788. verifyFormat("auto result = SomeObject\n"
  789. " // Calling someFunction on SomeObject\n"
  790. " .someFunction();");
  791. verifyFormat("void f(int i, // some comment (probably for i)\n"
  792. " int j, // some comment (probably for j)\n"
  793. " int k); // some comment (probably for k)");
  794. verifyFormat("void f(int i,\n"
  795. " // some comment (probably for j)\n"
  796. " int j,\n"
  797. " // some comment (probably for k)\n"
  798. " int k);");
  799. verifyFormat("int i // This is a fancy variable\n"
  800. " = 5; // with nicely aligned comment.");
  801. verifyFormat("// Leading comment.\n"
  802. "int a; // Trailing comment.");
  803. verifyFormat("int a; // Trailing comment\n"
  804. " // on 2\n"
  805. " // or 3 lines.\n"
  806. "int b;");
  807. verifyFormat("int a; // Trailing comment\n"
  808. "\n"
  809. "// Leading comment.\n"
  810. "int b;");
  811. verifyFormat("int a; // Comment.\n"
  812. " // More details.\n"
  813. "int bbbb; // Another comment.");
  814. verifyFormat(
  815. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  816. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n"
  817. "int cccccccccccccccccccccccccccccc; // comment\n"
  818. "int ddd; // looooooooooooooooooooooooong comment\n"
  819. "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  820. "int bbbbbbbbbbbbbbbbbbbbb; // comment\n"
  821. "int ccccccccccccccccccc; // comment");
  822. verifyFormat("#include \"a\" // comment\n"
  823. "#include \"a/b/c\" // comment");
  824. verifyFormat("#include <a> // comment\n"
  825. "#include <a/b/c> // comment");
  826. EXPECT_EQ("#include \"a\" // comment\n"
  827. "#include \"a/b/c\" // comment",
  828. format("#include \\\n"
  829. " \"a\" // comment\n"
  830. "#include \"a/b/c\" // comment"));
  831. verifyFormat("enum E {\n"
  832. " // comment\n"
  833. " VAL_A, // comment\n"
  834. " VAL_B\n"
  835. "};");
  836. verifyFormat(
  837. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  838. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
  839. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  840. " // Comment inside a statement.\n"
  841. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  842. verifyFormat("SomeFunction(a,\n"
  843. " // comment\n"
  844. " b + x);");
  845. verifyFormat("SomeFunction(a, a,\n"
  846. " // comment\n"
  847. " b + x);");
  848. verifyFormat(
  849. "bool aaaaaaaaaaaaa = // comment\n"
  850. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  851. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  852. verifyFormat("int aaaa; // aaaaa\n"
  853. "int aa; // aaaaaaa",
  854. getLLVMStyleWithColumns(20));
  855. EXPECT_EQ("void f() { // This does something ..\n"
  856. "}\n"
  857. "int a; // This is unrelated",
  858. format("void f() { // This does something ..\n"
  859. " }\n"
  860. "int a; // This is unrelated"));
  861. EXPECT_EQ("class C {\n"
  862. " void f() { // This does something ..\n"
  863. " } // awesome..\n"
  864. "\n"
  865. " int a; // This is unrelated\n"
  866. "};",
  867. format("class C{void f() { // This does something ..\n"
  868. " } // awesome..\n"
  869. " \n"
  870. "int a; // This is unrelated\n"
  871. "};"));
  872. EXPECT_EQ("int i; // single line trailing comment",
  873. format("int i;\\\n// single line trailing comment"));
  874. verifyGoogleFormat("int a; // Trailing comment.");
  875. verifyFormat("someFunction(anotherFunction( // Force break.\n"
  876. " parameter));");
  877. verifyGoogleFormat("#endif // HEADER_GUARD");
  878. verifyFormat("const char *test[] = {\n"
  879. " // A\n"
  880. " \"aaaa\",\n"
  881. " // B\n"
  882. " \"aaaaa\"};");
  883. verifyGoogleFormat(
  884. "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  885. " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment");
  886. EXPECT_EQ("D(a, {\n"
  887. " // test\n"
  888. " int a;\n"
  889. "});",
  890. format("D(a, {\n"
  891. "// test\n"
  892. "int a;\n"
  893. "});"));
  894. EXPECT_EQ("lineWith(); // comment\n"
  895. "// at start\n"
  896. "otherLine();",
  897. format("lineWith(); // comment\n"
  898. "// at start\n"
  899. "otherLine();"));
  900. EXPECT_EQ("lineWith(); // comment\n"
  901. "/*\n"
  902. " * at start */\n"
  903. "otherLine();",
  904. format("lineWith(); // comment\n"
  905. "/*\n"
  906. " * at start */\n"
  907. "otherLine();"));
  908. EXPECT_EQ("lineWith(); // comment\n"
  909. " // at start\n"
  910. "otherLine();",
  911. format("lineWith(); // comment\n"
  912. " // at start\n"
  913. "otherLine();"));
  914. EXPECT_EQ("lineWith(); // comment\n"
  915. "// at start\n"
  916. "otherLine(); // comment",
  917. format("lineWith(); // comment\n"
  918. "// at start\n"
  919. "otherLine(); // comment"));
  920. EXPECT_EQ("lineWith();\n"
  921. "// at start\n"
  922. "otherLine(); // comment",
  923. format("lineWith();\n"
  924. " // at start\n"
  925. "otherLine(); // comment"));
  926. EXPECT_EQ("// first\n"
  927. "// at start\n"
  928. "otherLine(); // comment",
  929. format("// first\n"
  930. " // at start\n"
  931. "otherLine(); // comment"));
  932. EXPECT_EQ("f();\n"
  933. "// first\n"
  934. "// at start\n"
  935. "otherLine(); // comment",
  936. format("f();\n"
  937. "// first\n"
  938. " // at start\n"
  939. "otherLine(); // comment"));
  940. verifyFormat("f(); // comment\n"
  941. "// first\n"
  942. "// at start\n"
  943. "otherLine();");
  944. EXPECT_EQ("f(); // comment\n"
  945. "// first\n"
  946. "// at start\n"
  947. "otherLine();",
  948. format("f(); // comment\n"
  949. "// first\n"
  950. " // at start\n"
  951. "otherLine();"));
  952. EXPECT_EQ("f(); // comment\n"
  953. " // first\n"
  954. "// at start\n"
  955. "otherLine();",
  956. format("f(); // comment\n"
  957. " // first\n"
  958. "// at start\n"
  959. "otherLine();"));
  960. EXPECT_EQ("void f() {\n"
  961. " lineWith(); // comment\n"
  962. " // at start\n"
  963. "}",
  964. format("void f() {\n"
  965. " lineWith(); // comment\n"
  966. " // at start\n"
  967. "}"));
  968. EXPECT_EQ("int xy; // a\n"
  969. "int z; // b",
  970. format("int xy; // a\n"
  971. "int z; //b"));
  972. EXPECT_EQ("int xy; // a\n"
  973. "int z; // bb",
  974. format("int xy; // a\n"
  975. "int z; //bb",
  976. getLLVMStyleWithColumns(12)));
  977. verifyFormat("#define A \\\n"
  978. " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
  979. " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
  980. getLLVMStyleWithColumns(60));
  981. verifyFormat(
  982. "#define A \\\n"
  983. " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
  984. " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
  985. getLLVMStyleWithColumns(61));
  986. verifyFormat("if ( // This is some comment\n"
  987. " x + 3) {\n"
  988. "}");
  989. EXPECT_EQ("if ( // This is some comment\n"
  990. " // spanning two lines\n"
  991. " x + 3) {\n"
  992. "}",
  993. format("if( // This is some comment\n"
  994. " // spanning two lines\n"
  995. " x + 3) {\n"
  996. "}"));
  997. verifyNoCrash("/\\\n/");
  998. verifyNoCrash("/\\\n* */");
  999. // The 0-character somehow makes the lexer return a proper comment.
  1000. verifyNoCrash(StringRef("/*\\\0\n/", 6));
  1001. }
  1002. TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
  1003. EXPECT_EQ("SomeFunction(a,\n"
  1004. " b, // comment\n"
  1005. " c);",
  1006. format("SomeFunction(a,\n"
  1007. " b, // comment\n"
  1008. " c);"));
  1009. EXPECT_EQ("SomeFunction(a, b,\n"
  1010. " // comment\n"
  1011. " c);",
  1012. format("SomeFunction(a,\n"
  1013. " b,\n"
  1014. " // comment\n"
  1015. " c);"));
  1016. EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
  1017. " c);",
  1018. format("SomeFunction(a, b, // comment (unclear relation)\n"
  1019. " c);"));
  1020. EXPECT_EQ("SomeFunction(a, // comment\n"
  1021. " b,\n"
  1022. " c); // comment",
  1023. format("SomeFunction(a, // comment\n"
  1024. " b,\n"
  1025. " c); // comment"));
  1026. }
  1027. TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
  1028. EXPECT_EQ("// comment", format("// comment "));
  1029. EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
  1030. format("int aaaaaaa, bbbbbbb; // comment ",
  1031. getLLVMStyleWithColumns(33)));
  1032. EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f "));
  1033. EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f "));
  1034. }
  1035. TEST_F(FormatTest, UnderstandsBlockComments) {
  1036. verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
  1037. verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y, /*c=*/::c); }");
  1038. EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
  1039. " bbbbbbbbbbbbbbbbbbbbbbbbb);",
  1040. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n"
  1041. "/* Trailing comment for aa... */\n"
  1042. " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  1043. EXPECT_EQ(
  1044. "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  1045. " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
  1046. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
  1047. "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  1048. EXPECT_EQ(
  1049. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1050. " aaaaaaaaaaaaaaaaaa,\n"
  1051. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  1052. "}",
  1053. format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1054. " aaaaaaaaaaaaaaaaaa ,\n"
  1055. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  1056. "}"));
  1057. verifyFormat("f(/* aaaaaaaaaaaaaaaaaa = */\n"
  1058. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  1059. FormatStyle NoBinPacking = getLLVMStyle();
  1060. NoBinPacking.BinPackParameters = false;
  1061. verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
  1062. " /* parameter 2 */ aaaaaa,\n"
  1063. " /* parameter 3 */ aaaaaa,\n"
  1064. " /* parameter 4 */ aaaaaa);",
  1065. NoBinPacking);
  1066. // Aligning block comments in macros.
  1067. verifyGoogleFormat("#define A \\\n"
  1068. " int i; /*a*/ \\\n"
  1069. " int jjj; /*b*/");
  1070. }
  1071. TEST_F(FormatTest, AlignsBlockComments) {
  1072. EXPECT_EQ("/*\n"
  1073. " * Really multi-line\n"
  1074. " * comment.\n"
  1075. " */\n"
  1076. "void f() {}",
  1077. format(" /*\n"
  1078. " * Really multi-line\n"
  1079. " * comment.\n"
  1080. " */\n"
  1081. " void f() {}"));
  1082. EXPECT_EQ("class C {\n"
  1083. " /*\n"
  1084. " * Another multi-line\n"
  1085. " * comment.\n"
  1086. " */\n"
  1087. " void f() {}\n"
  1088. "};",
  1089. format("class C {\n"
  1090. "/*\n"
  1091. " * Another multi-line\n"
  1092. " * comment.\n"
  1093. " */\n"
  1094. "void f() {}\n"
  1095. "};"));
  1096. EXPECT_EQ("/*\n"
  1097. " 1. This is a comment with non-trivial formatting.\n"
  1098. " 1.1. We have to indent/outdent all lines equally\n"
  1099. " 1.1.1. to keep the formatting.\n"
  1100. " */",
  1101. format(" /*\n"
  1102. " 1. This is a comment with non-trivial formatting.\n"
  1103. " 1.1. We have to indent/outdent all lines equally\n"
  1104. " 1.1.1. to keep the formatting.\n"
  1105. " */"));
  1106. EXPECT_EQ("/*\n"
  1107. "Don't try to outdent if there's not enough indentation.\n"
  1108. "*/",
  1109. format(" /*\n"
  1110. " Don't try to outdent if there's not enough indentation.\n"
  1111. " */"));
  1112. EXPECT_EQ("int i; /* Comment with empty...\n"
  1113. " *\n"
  1114. " * line. */",
  1115. format("int i; /* Comment with empty...\n"
  1116. " *\n"
  1117. " * line. */"));
  1118. EXPECT_EQ("int foobar = 0; /* comment */\n"
  1119. "int bar = 0; /* multiline\n"
  1120. " comment 1 */\n"
  1121. "int baz = 0; /* multiline\n"
  1122. " comment 2 */\n"
  1123. "int bzz = 0; /* multiline\n"
  1124. " comment 3 */",
  1125. format("int foobar = 0; /* comment */\n"
  1126. "int bar = 0; /* multiline\n"
  1127. " comment 1 */\n"
  1128. "int baz = 0; /* multiline\n"
  1129. " comment 2 */\n"
  1130. "int bzz = 0; /* multiline\n"
  1131. " comment 3 */"));
  1132. EXPECT_EQ("int foobar = 0; /* comment */\n"
  1133. "int bar = 0; /* multiline\n"
  1134. " comment */\n"
  1135. "int baz = 0; /* multiline\n"
  1136. "comment */",
  1137. format("int foobar = 0; /* comment */\n"
  1138. "int bar = 0; /* multiline\n"
  1139. "comment */\n"
  1140. "int baz = 0; /* multiline\n"
  1141. "comment */"));
  1142. }
  1143. TEST_F(FormatTest, CommentReflowingCanBeTurnedOff) {
  1144. FormatStyle Style = getLLVMStyleWithColumns(20);
  1145. Style.ReflowComments = false;
  1146. verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style);
  1147. verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style);
  1148. }
  1149. TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
  1150. EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1151. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
  1152. format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1153. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
  1154. EXPECT_EQ(
  1155. "void ffffffffffff(\n"
  1156. " int aaaaaaaa, int bbbbbbbb,\n"
  1157. " int cccccccccccc) { /*\n"
  1158. " aaaaaaaaaa\n"
  1159. " aaaaaaaaaaaaa\n"
  1160. " bbbbbbbbbbbbbb\n"
  1161. " bbbbbbbbbb\n"
  1162. " */\n"
  1163. "}",
  1164. format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
  1165. "{ /*\n"
  1166. " aaaaaaaaaa aaaaaaaaaaaaa\n"
  1167. " bbbbbbbbbbbbbb bbbbbbbbbb\n"
  1168. " */\n"
  1169. "}",
  1170. getLLVMStyleWithColumns(40)));
  1171. }
  1172. TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
  1173. EXPECT_EQ("void ffffffffff(\n"
  1174. " int aaaaa /* test */);",
  1175. format("void ffffffffff(int aaaaa /* test */);",
  1176. getLLVMStyleWithColumns(35)));
  1177. }
  1178. TEST_F(FormatTest, SplitsLongCxxComments) {
  1179. EXPECT_EQ("// A comment that\n"
  1180. "// doesn't fit on\n"
  1181. "// one line",
  1182. format("// A comment that doesn't fit on one line",
  1183. getLLVMStyleWithColumns(20)));
  1184. EXPECT_EQ("/// A comment that\n"
  1185. "/// doesn't fit on\n"
  1186. "/// one line",
  1187. format("/// A comment that doesn't fit on one line",
  1188. getLLVMStyleWithColumns(20)));
  1189. EXPECT_EQ("//! A comment that\n"
  1190. "//! doesn't fit on\n"
  1191. "//! one line",
  1192. format("//! A comment that doesn't fit on one line",
  1193. getLLVMStyleWithColumns(20)));
  1194. EXPECT_EQ("// a b c d\n"
  1195. "// e f g\n"
  1196. "// h i j k",
  1197. format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10)));
  1198. EXPECT_EQ(
  1199. "// a b c d\n"
  1200. "// e f g\n"
  1201. "// h i j k",
  1202. format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10)));
  1203. EXPECT_EQ("if (true) // A comment that\n"
  1204. " // doesn't fit on\n"
  1205. " // one line",
  1206. format("if (true) // A comment that doesn't fit on one line ",
  1207. getLLVMStyleWithColumns(30)));
  1208. EXPECT_EQ("// Don't_touch_leading_whitespace",
  1209. format("// Don't_touch_leading_whitespace",
  1210. getLLVMStyleWithColumns(20)));
  1211. EXPECT_EQ("// Add leading\n"
  1212. "// whitespace",
  1213. format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
  1214. EXPECT_EQ("/// Add leading\n"
  1215. "/// whitespace",
  1216. format("///Add leading whitespace", getLLVMStyleWithColumns(20)));
  1217. EXPECT_EQ("//! Add leading\n"
  1218. "//! whitespace",
  1219. format("//!Add leading whitespace", getLLVMStyleWithColumns(20)));
  1220. EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
  1221. EXPECT_EQ("// Even if it makes the line exceed the column\n"
  1222. "// limit",
  1223. format("//Even if it makes the line exceed the column limit",
  1224. getLLVMStyleWithColumns(51)));
  1225. EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
  1226. EXPECT_EQ("// aa bb cc dd",
  1227. format("// aa bb cc dd ",
  1228. getLLVMStyleWithColumns(15)));
  1229. EXPECT_EQ("// A comment before\n"
  1230. "// a macro\n"
  1231. "// definition\n"
  1232. "#define a b",
  1233. format("// A comment before a macro definition\n"
  1234. "#define a b",
  1235. getLLVMStyleWithColumns(20)));
  1236. EXPECT_EQ("void ffffff(\n"
  1237. " int aaaaaaaaa, // wwww\n"
  1238. " int bbbbbbbbbb, // xxxxxxx\n"
  1239. " // yyyyyyyyyy\n"
  1240. " int c, int d, int e) {}",
  1241. format("void ffffff(\n"
  1242. " int aaaaaaaaa, // wwww\n"
  1243. " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
  1244. " int c, int d, int e) {}",
  1245. getLLVMStyleWithColumns(40)));
  1246. EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1247. format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1248. getLLVMStyleWithColumns(20)));
  1249. EXPECT_EQ(
  1250. "#define XXX // a b c d\n"
  1251. " // e f g h",
  1252. format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
  1253. EXPECT_EQ(
  1254. "#define XXX // q w e r\n"
  1255. " // t y u i",
  1256. format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
  1257. }
  1258. TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {
  1259. EXPECT_EQ("// A comment\n"
  1260. "// that doesn't\n"
  1261. "// fit on one\n"
  1262. "// line",
  1263. format("// A comment that doesn't fit on one line",
  1264. getLLVMStyleWithColumns(20)));
  1265. EXPECT_EQ("/// A comment\n"
  1266. "/// that doesn't\n"
  1267. "/// fit on one\n"
  1268. "/// line",
  1269. format("/// A comment that doesn't fit on one line",
  1270. getLLVMStyleWithColumns(20)));
  1271. }
  1272. TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
  1273. EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1274. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1275. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1276. format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1277. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1278. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
  1279. EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1280. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1281. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1282. format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1283. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1284. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1285. getLLVMStyleWithColumns(50)));
  1286. // FIXME: One day we might want to implement adjustment of leading whitespace
  1287. // of the consecutive lines in this kind of comment:
  1288. EXPECT_EQ("double\n"
  1289. " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1290. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1291. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1292. format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1293. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1294. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1295. getLLVMStyleWithColumns(49)));
  1296. }
  1297. TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) {
  1298. FormatStyle Pragmas = getLLVMStyleWithColumns(30);
  1299. Pragmas.CommentPragmas = "^ IWYU pragma:";
  1300. EXPECT_EQ(
  1301. "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
  1302. format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
  1303. EXPECT_EQ(
  1304. "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
  1305. format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
  1306. }
  1307. TEST_F(FormatTest, PriorityOfCommentBreaking) {
  1308. EXPECT_EQ("if (xxx ==\n"
  1309. " yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  1310. " zzz)\n"
  1311. " q();",
  1312. format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  1313. " zzz) q();",
  1314. getLLVMStyleWithColumns(40)));
  1315. EXPECT_EQ("if (xxxxxxxxxx ==\n"
  1316. " yyy && // aaaaaa bbbbbbbb cccc\n"
  1317. " zzz)\n"
  1318. " q();",
  1319. format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
  1320. " zzz) q();",
  1321. getLLVMStyleWithColumns(40)));
  1322. EXPECT_EQ("if (xxxxxxxxxx &&\n"
  1323. " yyy || // aaaaaa bbbbbbbb cccc\n"
  1324. " zzz)\n"
  1325. " q();",
  1326. format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
  1327. " zzz) q();",
  1328. getLLVMStyleWithColumns(40)));
  1329. EXPECT_EQ("fffffffff(\n"
  1330. " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  1331. " zzz);",
  1332. format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  1333. " zzz);",
  1334. getLLVMStyleWithColumns(40)));
  1335. }
  1336. TEST_F(FormatTest, MultiLineCommentsInDefines) {
  1337. EXPECT_EQ("#define A(x) /* \\\n"
  1338. " a comment \\\n"
  1339. " inside */ \\\n"
  1340. " f();",
  1341. format("#define A(x) /* \\\n"
  1342. " a comment \\\n"
  1343. " inside */ \\\n"
  1344. " f();",
  1345. getLLVMStyleWithColumns(17)));
  1346. EXPECT_EQ("#define A( \\\n"
  1347. " x) /* \\\n"
  1348. " a comment \\\n"
  1349. " inside */ \\\n"
  1350. " f();",
  1351. format("#define A( \\\n"
  1352. " x) /* \\\n"
  1353. " a comment \\\n"
  1354. " inside */ \\\n"
  1355. " f();",
  1356. getLLVMStyleWithColumns(17)));
  1357. }
  1358. TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
  1359. EXPECT_EQ("namespace {}\n// Test\n#define A",
  1360. format("namespace {}\n // Test\n#define A"));
  1361. EXPECT_EQ("namespace {}\n/* Test */\n#define A",
  1362. format("namespace {}\n /* Test */\n#define A"));
  1363. EXPECT_EQ("namespace {}\n/* Test */ #define A",
  1364. format("namespace {}\n /* Test */ #define A"));
  1365. }
  1366. TEST_F(FormatTest, SplitsLongLinesInComments) {
  1367. EXPECT_EQ("/* This is a long\n"
  1368. " * comment that\n"
  1369. " * doesn't\n"
  1370. " * fit on one line.\n"
  1371. " */",
  1372. format("/* "
  1373. "This is a long "
  1374. "comment that "
  1375. "doesn't "
  1376. "fit on one line. */",
  1377. getLLVMStyleWithColumns(20)));
  1378. EXPECT_EQ(
  1379. "/* a b c d\n"
  1380. " * e f g\n"
  1381. " * h i j k\n"
  1382. " */",
  1383. format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
  1384. EXPECT_EQ(
  1385. "/* a b c d\n"
  1386. " * e f g\n"
  1387. " * h i j k\n"
  1388. " */",
  1389. format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
  1390. EXPECT_EQ("/*\n"
  1391. "This is a long\n"
  1392. "comment that doesn't\n"
  1393. "fit on one line.\n"
  1394. "*/",
  1395. format("/*\n"
  1396. "This is a long "
  1397. "comment that doesn't "
  1398. "fit on one line. \n"
  1399. "*/",
  1400. getLLVMStyleWithColumns(20)));
  1401. EXPECT_EQ("/*\n"
  1402. " * This is a long\n"
  1403. " * comment that\n"
  1404. " * doesn't fit on\n"
  1405. " * one line.\n"
  1406. " */",
  1407. format("/* \n"
  1408. " * This is a long "
  1409. " comment that "
  1410. " doesn't fit on "
  1411. " one line. \n"
  1412. " */",
  1413. getLLVMStyleWithColumns(20)));
  1414. EXPECT_EQ("/*\n"
  1415. " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
  1416. " * so_it_should_be_broken\n"
  1417. " * wherever_a_space_occurs\n"
  1418. " */",
  1419. format("/*\n"
  1420. " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
  1421. " so_it_should_be_broken "
  1422. " wherever_a_space_occurs \n"
  1423. " */",
  1424. getLLVMStyleWithColumns(20)));
  1425. EXPECT_EQ("/*\n"
  1426. " * This_comment_can_not_be_broken_into_lines\n"
  1427. " */",
  1428. format("/*\n"
  1429. " * This_comment_can_not_be_broken_into_lines\n"
  1430. " */",
  1431. getLLVMStyleWithColumns(20)));
  1432. EXPECT_EQ("{\n"
  1433. " /*\n"
  1434. " This is another\n"
  1435. " long comment that\n"
  1436. " doesn't fit on one\n"
  1437. " line 1234567890\n"
  1438. " */\n"
  1439. "}",
  1440. format("{\n"
  1441. "/*\n"
  1442. "This is another "
  1443. " long comment that "
  1444. " doesn't fit on one"
  1445. " line 1234567890\n"
  1446. "*/\n"
  1447. "}",
  1448. getLLVMStyleWithColumns(20)));
  1449. EXPECT_EQ("{\n"
  1450. " /*\n"
  1451. " * This i s\n"
  1452. " * another comment\n"
  1453. " * t hat doesn' t\n"
  1454. " * fit on one l i\n"
  1455. " * n e\n"
  1456. " */\n"
  1457. "}",
  1458. format("{\n"
  1459. "/*\n"
  1460. " * This i s"
  1461. " another comment"
  1462. " t hat doesn' t"
  1463. " fit on one l i"
  1464. " n e\n"
  1465. " */\n"
  1466. "}",
  1467. getLLVMStyleWithColumns(20)));
  1468. EXPECT_EQ("/*\n"
  1469. " * This is a long\n"
  1470. " * comment that\n"
  1471. " * doesn't fit on\n"
  1472. " * one line\n"
  1473. " */",
  1474. format(" /*\n"
  1475. " * This is a long comment that doesn't fit on one line\n"
  1476. " */",
  1477. getLLVMStyleWithColumns(20)));
  1478. EXPECT_EQ("{\n"
  1479. " if (something) /* This is a\n"
  1480. " long\n"
  1481. " comment */\n"
  1482. " ;\n"
  1483. "}",
  1484. format("{\n"
  1485. " if (something) /* This is a long comment */\n"
  1486. " ;\n"
  1487. "}",
  1488. getLLVMStyleWithColumns(30)));
  1489. EXPECT_EQ("/* A comment before\n"
  1490. " * a macro\n"
  1491. " * definition */\n"
  1492. "#define a b",
  1493. format("/* A comment before a macro definition */\n"
  1494. "#define a b",
  1495. getLLVMStyleWithColumns(20)));
  1496. EXPECT_EQ("/* some comment\n"
  1497. " * a comment\n"
  1498. "* that we break\n"
  1499. " * another comment\n"
  1500. "* we have to break\n"
  1501. "* a left comment\n"
  1502. " */",
  1503. format(" /* some comment\n"
  1504. " * a comment that we break\n"
  1505. " * another comment we have to break\n"
  1506. "* a left comment\n"
  1507. " */",
  1508. getLLVMStyleWithColumns(20)));
  1509. EXPECT_EQ("/**\n"
  1510. " * multiline block\n"
  1511. " * comment\n"
  1512. " *\n"
  1513. " */",
  1514. format("/**\n"
  1515. " * multiline block comment\n"
  1516. " *\n"
  1517. " */",
  1518. getLLVMStyleWithColumns(20)));
  1519. EXPECT_EQ("/*\n"
  1520. "\n"
  1521. "\n"
  1522. " */\n",
  1523. format(" /* \n"
  1524. " \n"
  1525. " \n"
  1526. " */\n"));
  1527. EXPECT_EQ("/* a a */",
  1528. format("/* a a */", getLLVMStyleWithColumns(15)));
  1529. EXPECT_EQ("/* a a bc */",
  1530. format("/* a a bc */", getLLVMStyleWithColumns(15)));
  1531. EXPECT_EQ("/* aaa aaa\n"
  1532. " * aaaaa */",
  1533. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1534. EXPECT_EQ("/* aaa aaa\n"
  1535. " * aaaaa */",
  1536. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1537. }
  1538. TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
  1539. EXPECT_EQ("#define X \\\n"
  1540. " /* \\\n"
  1541. " Test \\\n"
  1542. " Macro comment \\\n"
  1543. " with a long \\\n"
  1544. " line \\\n"
  1545. " */ \\\n"
  1546. " A + B",
  1547. format("#define X \\\n"
  1548. " /*\n"
  1549. " Test\n"
  1550. " Macro comment with a long line\n"
  1551. " */ \\\n"
  1552. " A + B",
  1553. getLLVMStyleWithColumns(20)));
  1554. EXPECT_EQ("#define X \\\n"
  1555. " /* Macro comment \\\n"
  1556. " with a long \\\n"
  1557. " line */ \\\n"
  1558. " A + B",
  1559. format("#define X \\\n"
  1560. " /* Macro comment with a long\n"
  1561. " line */ \\\n"
  1562. " A + B",
  1563. getLLVMStyleWithColumns(20)));
  1564. EXPECT_EQ("#define X \\\n"
  1565. " /* Macro comment \\\n"
  1566. " * with a long \\\n"
  1567. " * line */ \\\n"
  1568. " A + B",
  1569. format("#define X \\\n"
  1570. " /* Macro comment with a long line */ \\\n"
  1571. " A + B",
  1572. getLLVMStyleWithColumns(20)));
  1573. }
  1574. TEST_F(FormatTest, CommentsInStaticInitializers) {
  1575. EXPECT_EQ(
  1576. "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
  1577. " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
  1578. " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
  1579. " aaaaaaaaaaaaaaaaaaaa, // comment\n"
  1580. " aaaaaaaaaaaaaaaaaaaa};",
  1581. format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
  1582. " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
  1583. " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
  1584. " aaaaaaaaaaaaaaaaaaaa , // comment\n"
  1585. " aaaaaaaaaaaaaaaaaaaa };"));
  1586. verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1587. " bbbbbbbbbbb, ccccccccccc};");
  1588. verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1589. " // comment for bb....\n"
  1590. " bbbbbbbbbbb, ccccccccccc};");
  1591. verifyGoogleFormat(
  1592. "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1593. " bbbbbbbbbbb, ccccccccccc};");
  1594. verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1595. " // comment for bb....\n"
  1596. " bbbbbbbbbbb, ccccccccccc};");
  1597. verifyFormat("S s = {{a, b, c}, // Group #1\n"
  1598. " {d, e, f}, // Group #2\n"
  1599. " {g, h, i}}; // Group #3");
  1600. verifyFormat("S s = {{// Group #1\n"
  1601. " a, b, c},\n"
  1602. " {// Group #2\n"
  1603. " d, e, f},\n"
  1604. " {// Group #3\n"
  1605. " g, h, i}};");
  1606. EXPECT_EQ("S s = {\n"
  1607. " // Some comment\n"
  1608. " a,\n"
  1609. "\n"
  1610. " // Comment after empty line\n"
  1611. " b}",
  1612. format("S s = {\n"
  1613. " // Some comment\n"
  1614. " a,\n"
  1615. " \n"
  1616. " // Comment after empty line\n"
  1617. " b\n"
  1618. "}"));
  1619. EXPECT_EQ("S s = {\n"
  1620. " /* Some comment */\n"
  1621. " a,\n"
  1622. "\n"
  1623. " /* Comment after empty line */\n"
  1624. " b}",
  1625. format("S s = {\n"
  1626. " /* Some comment */\n"
  1627. " a,\n"
  1628. " \n"
  1629. " /* Comment after empty line */\n"
  1630. " b\n"
  1631. "}"));
  1632. verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
  1633. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1634. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1635. " 0x00, 0x00, 0x00, 0x00}; // comment\n");
  1636. }
  1637. TEST_F(FormatTest, IgnoresIf0Contents) {
  1638. EXPECT_EQ("#if 0\n"
  1639. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1640. "#endif\n"
  1641. "void f() {}",
  1642. format("#if 0\n"
  1643. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1644. "#endif\n"
  1645. "void f( ) { }"));
  1646. EXPECT_EQ("#if false\n"
  1647. "void f( ) { }\n"
  1648. "#endif\n"
  1649. "void g() {}\n",
  1650. format("#if false\n"
  1651. "void f( ) { }\n"
  1652. "#endif\n"
  1653. "void g( ) { }\n"));
  1654. EXPECT_EQ("enum E {\n"
  1655. " One,\n"
  1656. " Two,\n"
  1657. "#if 0\n"
  1658. "Three,\n"
  1659. " Four,\n"
  1660. "#endif\n"
  1661. " Five\n"
  1662. "};",
  1663. format("enum E {\n"
  1664. " One,Two,\n"
  1665. "#if 0\n"
  1666. "Three,\n"
  1667. " Four,\n"
  1668. "#endif\n"
  1669. " Five};"));
  1670. EXPECT_EQ("enum F {\n"
  1671. " One,\n"
  1672. "#if 1\n"
  1673. " Two,\n"
  1674. "#if 0\n"
  1675. "Three,\n"
  1676. " Four,\n"
  1677. "#endif\n"
  1678. " Five\n"
  1679. "#endif\n"
  1680. "};",
  1681. format("enum F {\n"
  1682. "One,\n"
  1683. "#if 1\n"
  1684. "Two,\n"
  1685. "#if 0\n"
  1686. "Three,\n"
  1687. " Four,\n"
  1688. "#endif\n"
  1689. "Five\n"
  1690. "#endif\n"
  1691. "};"));
  1692. EXPECT_EQ("enum G {\n"
  1693. " One,\n"
  1694. "#if 0\n"
  1695. "Two,\n"
  1696. "#else\n"
  1697. " Three,\n"
  1698. "#endif\n"
  1699. " Four\n"
  1700. "};",
  1701. format("enum G {\n"
  1702. "One,\n"
  1703. "#if 0\n"
  1704. "Two,\n"
  1705. "#else\n"
  1706. "Three,\n"
  1707. "#endif\n"
  1708. "Four\n"
  1709. "};"));
  1710. EXPECT_EQ("enum H {\n"
  1711. " One,\n"
  1712. "#if 0\n"
  1713. "#ifdef Q\n"
  1714. "Two,\n"
  1715. "#else\n"
  1716. "Three,\n"
  1717. "#endif\n"
  1718. "#endif\n"
  1719. " Four\n"
  1720. "};",
  1721. format("enum H {\n"
  1722. "One,\n"
  1723. "#if 0\n"
  1724. "#ifdef Q\n"
  1725. "Two,\n"
  1726. "#else\n"
  1727. "Three,\n"
  1728. "#endif\n"
  1729. "#endif\n"
  1730. "Four\n"
  1731. "};"));
  1732. EXPECT_EQ("enum I {\n"
  1733. " One,\n"
  1734. "#if /* test */ 0 || 1\n"
  1735. "Two,\n"
  1736. "Three,\n"
  1737. "#endif\n"
  1738. " Four\n"
  1739. "};",
  1740. format("enum I {\n"
  1741. "One,\n"
  1742. "#if /* test */ 0 || 1\n"
  1743. "Two,\n"
  1744. "Three,\n"
  1745. "#endif\n"
  1746. "Four\n"
  1747. "};"));
  1748. EXPECT_EQ("enum J {\n"
  1749. " One,\n"
  1750. "#if 0\n"
  1751. "#if 0\n"
  1752. "Two,\n"
  1753. "#else\n"
  1754. "Three,\n"
  1755. "#endif\n"
  1756. "Four,\n"
  1757. "#endif\n"
  1758. " Five\n"
  1759. "};",
  1760. format("enum J {\n"
  1761. "One,\n"
  1762. "#if 0\n"
  1763. "#if 0\n"
  1764. "Two,\n"
  1765. "#else\n"
  1766. "Three,\n"
  1767. "#endif\n"
  1768. "Four,\n"
  1769. "#endif\n"
  1770. "Five\n"
  1771. "};"));
  1772. }
  1773. //===----------------------------------------------------------------------===//
  1774. // Tests for classes, namespaces, etc.
  1775. //===----------------------------------------------------------------------===//
  1776. TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
  1777. verifyFormat("class A {};");
  1778. }
  1779. TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
  1780. verifyFormat("class A {\n"
  1781. "public:\n"
  1782. "public: // comment\n"
  1783. "protected:\n"
  1784. "private:\n"
  1785. " void f() {}\n"
  1786. "};");
  1787. verifyGoogleFormat("class A {\n"
  1788. " public:\n"
  1789. " protected:\n"
  1790. " private:\n"
  1791. " void f() {}\n"
  1792. "};");
  1793. verifyFormat("class A {\n"
  1794. "public slots:\n"
  1795. " void f1() {}\n"
  1796. "public Q_SLOTS:\n"
  1797. " void f2() {}\n"
  1798. "protected slots:\n"
  1799. " void f3() {}\n"
  1800. "protected Q_SLOTS:\n"
  1801. " void f4() {}\n"
  1802. "private slots:\n"
  1803. " void f5() {}\n"
  1804. "private Q_SLOTS:\n"
  1805. " void f6() {}\n"
  1806. "signals:\n"
  1807. " void g1();\n"
  1808. "Q_SIGNALS:\n"
  1809. " void g2();\n"
  1810. "};");
  1811. // Don't interpret 'signals' the wrong way.
  1812. verifyFormat("signals.set();");
  1813. verifyFormat("for (Signals signals : f()) {\n}");
  1814. verifyFormat("{\n"
  1815. " signals.set(); // This needs indentation.\n"
  1816. "}");
  1817. }
  1818. TEST_F(FormatTest, SeparatesLogicalBlocks) {
  1819. EXPECT_EQ("class A {\n"
  1820. "public:\n"
  1821. " void f();\n"
  1822. "\n"
  1823. "private:\n"
  1824. " void g() {}\n"
  1825. " // test\n"
  1826. "protected:\n"
  1827. " int h;\n"
  1828. "};",
  1829. format("class A {\n"
  1830. "public:\n"
  1831. "void f();\n"
  1832. "private:\n"
  1833. "void g() {}\n"
  1834. "// test\n"
  1835. "protected:\n"
  1836. "int h;\n"
  1837. "};"));
  1838. EXPECT_EQ("class A {\n"
  1839. "protected:\n"
  1840. "public:\n"
  1841. " void f();\n"
  1842. "};",
  1843. format("class A {\n"
  1844. "protected:\n"
  1845. "\n"
  1846. "public:\n"
  1847. "\n"
  1848. " void f();\n"
  1849. "};"));
  1850. // Even ensure proper spacing inside macros.
  1851. EXPECT_EQ("#define B \\\n"
  1852. " class A { \\\n"
  1853. " protected: \\\n"
  1854. " public: \\\n"
  1855. " void f(); \\\n"
  1856. " };",
  1857. format("#define B \\\n"
  1858. " class A { \\\n"
  1859. " protected: \\\n"
  1860. " \\\n"
  1861. " public: \\\n"
  1862. " \\\n"
  1863. " void f(); \\\n"
  1864. " };",
  1865. getGoogleStyle()));
  1866. // But don't remove empty lines after macros ending in access specifiers.
  1867. EXPECT_EQ("#define A private:\n"
  1868. "\n"
  1869. "int i;",
  1870. format("#define A private:\n"
  1871. "\n"
  1872. "int i;"));
  1873. }
  1874. TEST_F(FormatTest, FormatsClasses) {
  1875. verifyFormat("class A : public B {};");
  1876. verifyFormat("class A : public ::B {};");
  1877. verifyFormat(
  1878. "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1879. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1880. verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
  1881. " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1882. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1883. verifyFormat(
  1884. "class A : public B, public C, public D, public E, public F {};");
  1885. verifyFormat("class AAAAAAAAAAAA : public B,\n"
  1886. " public C,\n"
  1887. " public D,\n"
  1888. " public E,\n"
  1889. " public F,\n"
  1890. " public G {};");
  1891. verifyFormat("class\n"
  1892. " ReallyReallyLongClassName {\n"
  1893. " int i;\n"
  1894. "};",
  1895. getLLVMStyleWithColumns(32));
  1896. verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
  1897. " aaaaaaaaaaaaaaaa> {};");
  1898. verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
  1899. " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
  1900. " aaaaaaaaaaaaaaaaaaaaaa> {};");
  1901. verifyFormat("template <class R, class C>\n"
  1902. "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
  1903. " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
  1904. verifyFormat("class ::A::B {};");
  1905. }
  1906. TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
  1907. verifyFormat("class A {\n} a, b;");
  1908. verifyFormat("struct A {\n} a, b;");
  1909. verifyFormat("union A {\n} a;");
  1910. }
  1911. TEST_F(FormatTest, FormatsEnum) {
  1912. verifyFormat("enum {\n"
  1913. " Zero,\n"
  1914. " One = 1,\n"
  1915. " Two = One + 1,\n"
  1916. " Three = (One + Two),\n"
  1917. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1918. " Five = (One, Two, Three, Four, 5)\n"
  1919. "};");
  1920. verifyGoogleFormat("enum {\n"
  1921. " Zero,\n"
  1922. " One = 1,\n"
  1923. " Two = One + 1,\n"
  1924. " Three = (One + Two),\n"
  1925. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1926. " Five = (One, Two, Three, Four, 5)\n"
  1927. "};");
  1928. verifyFormat("enum Enum {};");
  1929. verifyFormat("enum {};");
  1930. verifyFormat("enum X E {} d;");
  1931. verifyFormat("enum __attribute__((...)) E {} d;");
  1932. verifyFormat("enum __declspec__((...)) E {} d;");
  1933. verifyFormat("enum {\n"
  1934. " Bar = Foo<int, int>::value\n"
  1935. "};",
  1936. getLLVMStyleWithColumns(30));
  1937. verifyFormat("enum ShortEnum { A, B, C };");
  1938. verifyGoogleFormat("enum ShortEnum { A, B, C };");
  1939. EXPECT_EQ("enum KeepEmptyLines {\n"
  1940. " ONE,\n"
  1941. "\n"
  1942. " TWO,\n"
  1943. "\n"
  1944. " THREE\n"
  1945. "}",
  1946. format("enum KeepEmptyLines {\n"
  1947. " ONE,\n"
  1948. "\n"
  1949. " TWO,\n"
  1950. "\n"
  1951. "\n"
  1952. " THREE\n"
  1953. "}"));
  1954. verifyFormat("enum E { // comment\n"
  1955. " ONE,\n"
  1956. " TWO\n"
  1957. "};\n"
  1958. "int i;");
  1959. // Not enums.
  1960. verifyFormat("enum X f() {\n"
  1961. " a();\n"
  1962. " return 42;\n"
  1963. "}");
  1964. verifyFormat("enum X Type::f() {\n"
  1965. " a();\n"
  1966. " return 42;\n"
  1967. "}");
  1968. verifyFormat("enum ::X f() {\n"
  1969. " a();\n"
  1970. " return 42;\n"
  1971. "}");
  1972. verifyFormat("enum ns::X f() {\n"
  1973. " a();\n"
  1974. " return 42;\n"
  1975. "}");
  1976. }
  1977. TEST_F(FormatTest, FormatsEnumsWithErrors) {
  1978. verifyFormat("enum Type {\n"
  1979. " One = 0; // These semicolons should be commas.\n"
  1980. " Two = 1;\n"
  1981. "};");
  1982. verifyFormat("namespace n {\n"
  1983. "enum Type {\n"
  1984. " One,\n"
  1985. " Two, // missing };\n"
  1986. " int i;\n"
  1987. "}\n"
  1988. "void g() {}");
  1989. }
  1990. TEST_F(FormatTest, FormatsEnumStruct) {
  1991. verifyFormat("enum struct {\n"
  1992. " Zero,\n"
  1993. " One = 1,\n"
  1994. " Two = One + 1,\n"
  1995. " Three = (One + Two),\n"
  1996. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1997. " Five = (One, Two, Three, Four, 5)\n"
  1998. "};");
  1999. verifyFormat("enum struct Enum {};");
  2000. verifyFormat("enum struct {};");
  2001. verifyFormat("enum struct X E {} d;");
  2002. verifyFormat("enum struct __attribute__((...)) E {} d;");
  2003. verifyFormat("enum struct __declspec__((...)) E {} d;");
  2004. verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
  2005. }
  2006. TEST_F(FormatTest, FormatsEnumClass) {
  2007. verifyFormat("enum class {\n"
  2008. " Zero,\n"
  2009. " One = 1,\n"
  2010. " Two = One + 1,\n"
  2011. " Three = (One + Two),\n"
  2012. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  2013. " Five = (One, Two, Three, Four, 5)\n"
  2014. "};");
  2015. verifyFormat("enum class Enum {};");
  2016. verifyFormat("enum class {};");
  2017. verifyFormat("enum class X E {} d;");
  2018. verifyFormat("enum class __attribute__((...)) E {} d;");
  2019. verifyFormat("enum class __declspec__((...)) E {} d;");
  2020. verifyFormat("enum class X f() {\n a();\n return 42;\n}");
  2021. }
  2022. TEST_F(FormatTest, FormatsEnumTypes) {
  2023. verifyFormat("enum X : int {\n"
  2024. " A, // Force multiple lines.\n"
  2025. " B\n"
  2026. "};");
  2027. verifyFormat("enum X : int { A, B };");
  2028. verifyFormat("enum X : std::uint32_t { A, B };");
  2029. }
  2030. TEST_F(FormatTest, FormatsNSEnums) {
  2031. verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
  2032. verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
  2033. " // Information about someDecentlyLongValue.\n"
  2034. " someDecentlyLongValue,\n"
  2035. " // Information about anotherDecentlyLongValue.\n"
  2036. " anotherDecentlyLongValue,\n"
  2037. " // Information about aThirdDecentlyLongValue.\n"
  2038. " aThirdDecentlyLongValue\n"
  2039. "};");
  2040. verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
  2041. " a = 1,\n"
  2042. " b = 2,\n"
  2043. " c = 3,\n"
  2044. "};");
  2045. verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
  2046. " a = 1,\n"
  2047. " b = 2,\n"
  2048. " c = 3,\n"
  2049. "};");
  2050. verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
  2051. " a = 1,\n"
  2052. " b = 2,\n"
  2053. " c = 3,\n"
  2054. "};");
  2055. }
  2056. TEST_F(FormatTest, FormatsBitfields) {
  2057. verifyFormat("struct Bitfields {\n"
  2058. " unsigned sClass : 8;\n"
  2059. " unsigned ValueKind : 2;\n"
  2060. "};");
  2061. verifyFormat("struct A {\n"
  2062. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
  2063. " bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
  2064. "};");
  2065. verifyFormat("struct MyStruct {\n"
  2066. " uchar data;\n"
  2067. " uchar : 8;\n"
  2068. " uchar : 8;\n"
  2069. " uchar other;\n"
  2070. "};");
  2071. }
  2072. TEST_F(FormatTest, FormatsNamespaces) {
  2073. verifyFormat("namespace some_namespace {\n"
  2074. "class A {};\n"
  2075. "void f() { f(); }\n"
  2076. "}");
  2077. verifyFormat("namespace {\n"
  2078. "class A {};\n"
  2079. "void f() { f(); }\n"
  2080. "}");
  2081. verifyFormat("inline namespace X {\n"
  2082. "class A {};\n"
  2083. "void f() { f(); }\n"
  2084. "}");
  2085. verifyFormat("using namespace some_namespace;\n"
  2086. "class A {};\n"
  2087. "void f() { f(); }");
  2088. // This code is more common than we thought; if we
  2089. // layout this correctly the semicolon will go into
  2090. // its own line, which is undesirable.
  2091. verifyFormat("namespace {};");
  2092. verifyFormat("namespace {\n"
  2093. "class A {};\n"
  2094. "};");
  2095. verifyFormat("namespace {\n"
  2096. "int SomeVariable = 0; // comment\n"
  2097. "} // namespace");
  2098. EXPECT_EQ("#ifndef HEADER_GUARD\n"
  2099. "#define HEADER_GUARD\n"
  2100. "namespace my_namespace {\n"
  2101. "int i;\n"
  2102. "} // my_namespace\n"
  2103. "#endif // HEADER_GUARD",
  2104. format("#ifndef HEADER_GUARD\n"
  2105. " #define HEADER_GUARD\n"
  2106. " namespace my_namespace {\n"
  2107. "int i;\n"
  2108. "} // my_namespace\n"
  2109. "#endif // HEADER_GUARD"));
  2110. EXPECT_EQ("namespace A::B {\n"
  2111. "class C {};\n"
  2112. "}",
  2113. format("namespace A::B {\n"
  2114. "class C {};\n"
  2115. "}"));
  2116. FormatStyle Style = getLLVMStyle();
  2117. Style.NamespaceIndentation = FormatStyle::NI_All;
  2118. EXPECT_EQ("namespace out {\n"
  2119. " int i;\n"
  2120. " namespace in {\n"
  2121. " int i;\n"
  2122. " } // namespace\n"
  2123. "} // namespace",
  2124. format("namespace out {\n"
  2125. "int i;\n"
  2126. "namespace in {\n"
  2127. "int i;\n"
  2128. "} // namespace\n"
  2129. "} // namespace",
  2130. Style));
  2131. Style.NamespaceIndentation = FormatStyle::NI_Inner;
  2132. EXPECT_EQ("namespace out {\n"
  2133. "int i;\n"
  2134. "namespace in {\n"
  2135. " int i;\n"
  2136. "} // namespace\n"
  2137. "} // namespace",
  2138. format("namespace out {\n"
  2139. "int i;\n"
  2140. "namespace in {\n"
  2141. "int i;\n"
  2142. "} // namespace\n"
  2143. "} // namespace",
  2144. Style));
  2145. }
  2146. TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
  2147. TEST_F(FormatTest, FormatsInlineASM) {
  2148. verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
  2149. verifyFormat("asm(\"nop\" ::: \"memory\");");
  2150. verifyFormat(
  2151. "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
  2152. " \"cpuid\\n\\t\"\n"
  2153. " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
  2154. " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
  2155. " : \"a\"(value));");
  2156. EXPECT_EQ(
  2157. "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
  2158. " __asm {\n"
  2159. " mov edx,[that] // vtable in edx\n"
  2160. " mov eax,methodIndex\n"
  2161. " call [edx][eax*4] // stdcall\n"
  2162. " }\n"
  2163. "}",
  2164. format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
  2165. " __asm {\n"
  2166. " mov edx,[that] // vtable in edx\n"
  2167. " mov eax,methodIndex\n"
  2168. " call [edx][eax*4] // stdcall\n"
  2169. " }\n"
  2170. "}"));
  2171. EXPECT_EQ("_asm {\n"
  2172. " xor eax, eax;\n"
  2173. " cpuid;\n"
  2174. "}",
  2175. format("_asm {\n"
  2176. " xor eax, eax;\n"
  2177. " cpuid;\n"
  2178. "}"));
  2179. verifyFormat("void function() {\n"
  2180. " // comment\n"
  2181. " asm(\"\");\n"
  2182. "}");
  2183. EXPECT_EQ("__asm {\n"
  2184. "}\n"
  2185. "int i;",
  2186. format("__asm {\n"
  2187. "}\n"
  2188. "int i;"));
  2189. }
  2190. TEST_F(FormatTest, FormatTryCatch) {
  2191. verifyFormat("try {\n"
  2192. " throw a * b;\n"
  2193. "} catch (int a) {\n"
  2194. " // Do nothing.\n"
  2195. "} catch (...) {\n"
  2196. " exit(42);\n"
  2197. "}");
  2198. // Function-level try statements.
  2199. verifyFormat("int f() try { return 4; } catch (...) {\n"
  2200. " return 5;\n"
  2201. "}");
  2202. verifyFormat("class A {\n"
  2203. " int a;\n"
  2204. " A() try : a(0) {\n"
  2205. " } catch (...) {\n"
  2206. " throw;\n"
  2207. " }\n"
  2208. "};\n");
  2209. // Incomplete try-catch blocks.
  2210. verifyIncompleteFormat("try {} catch (");
  2211. }
  2212. TEST_F(FormatTest, FormatSEHTryCatch) {
  2213. verifyFormat("__try {\n"
  2214. " int a = b * c;\n"
  2215. "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
  2216. " // Do nothing.\n"
  2217. "}");
  2218. verifyFormat("__try {\n"
  2219. " int a = b * c;\n"
  2220. "} __finally {\n"
  2221. " // Do nothing.\n"
  2222. "}");
  2223. verifyFormat("DEBUG({\n"
  2224. " __try {\n"
  2225. " } __finally {\n"
  2226. " }\n"
  2227. "});\n");
  2228. }
  2229. TEST_F(FormatTest, IncompleteTryCatchBlocks) {
  2230. verifyFormat("try {\n"
  2231. " f();\n"
  2232. "} catch {\n"
  2233. " g();\n"
  2234. "}");
  2235. verifyFormat("try {\n"
  2236. " f();\n"
  2237. "} catch (A a) MACRO(x) {\n"
  2238. " g();\n"
  2239. "} catch (B b) MACRO(x) {\n"
  2240. " g();\n"
  2241. "}");
  2242. }
  2243. TEST_F(FormatTest, FormatTryCatchBraceStyles) {
  2244. FormatStyle Style = getLLVMStyle();
  2245. for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
  2246. FormatStyle::BS_WebKit}) {
  2247. Style.BreakBeforeBraces = BraceStyle;
  2248. verifyFormat("try {\n"
  2249. " // something\n"
  2250. "} catch (...) {\n"
  2251. " // something\n"
  2252. "}",
  2253. Style);
  2254. }
  2255. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  2256. verifyFormat("try {\n"
  2257. " // something\n"
  2258. "}\n"
  2259. "catch (...) {\n"
  2260. " // something\n"
  2261. "}",
  2262. Style);
  2263. verifyFormat("__try {\n"
  2264. " // something\n"
  2265. "}\n"
  2266. "__finally {\n"
  2267. " // something\n"
  2268. "}",
  2269. Style);
  2270. verifyFormat("@try {\n"
  2271. " // something\n"
  2272. "}\n"
  2273. "@finally {\n"
  2274. " // something\n"
  2275. "}",
  2276. Style);
  2277. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  2278. verifyFormat("try\n"
  2279. "{\n"
  2280. " // something\n"
  2281. "}\n"
  2282. "catch (...)\n"
  2283. "{\n"
  2284. " // something\n"
  2285. "}",
  2286. Style);
  2287. Style.BreakBeforeBraces = FormatStyle::BS_GNU;
  2288. verifyFormat("try\n"
  2289. " {\n"
  2290. " // something\n"
  2291. " }\n"
  2292. "catch (...)\n"
  2293. " {\n"
  2294. " // something\n"
  2295. " }",
  2296. Style);
  2297. Style.BreakBeforeBraces = FormatStyle::BS_Custom;
  2298. Style.BraceWrapping.BeforeCatch = true;
  2299. verifyFormat("try {\n"
  2300. " // something\n"
  2301. "}\n"
  2302. "catch (...) {\n"
  2303. " // something\n"
  2304. "}",
  2305. Style);
  2306. }
  2307. TEST_F(FormatTest, FormatObjCTryCatch) {
  2308. verifyFormat("@try {\n"
  2309. " f();\n"
  2310. "} @catch (NSException e) {\n"
  2311. " @throw;\n"
  2312. "} @finally {\n"
  2313. " exit(42);\n"
  2314. "}");
  2315. verifyFormat("DEBUG({\n"
  2316. " @try {\n"
  2317. " } @finally {\n"
  2318. " }\n"
  2319. "});\n");
  2320. }
  2321. TEST_F(FormatTest, FormatObjCAutoreleasepool) {
  2322. FormatStyle Style = getLLVMStyle();
  2323. verifyFormat("@autoreleasepool {\n"
  2324. " f();\n"
  2325. "}\n"
  2326. "@autoreleasepool {\n"
  2327. " f();\n"
  2328. "}\n",
  2329. Style);
  2330. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  2331. verifyFormat("@autoreleasepool\n"
  2332. "{\n"
  2333. " f();\n"
  2334. "}\n"
  2335. "@autoreleasepool\n"
  2336. "{\n"
  2337. " f();\n"
  2338. "}\n",
  2339. Style);
  2340. }
  2341. TEST_F(FormatTest, StaticInitializers) {
  2342. verifyFormat("static SomeClass SC = {1, 'a'};");
  2343. verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
  2344. " 100000000, "
  2345. "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
  2346. // Here, everything other than the "}" would fit on a line.
  2347. verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
  2348. " 10000000000000000000000000};");
  2349. EXPECT_EQ("S s = {a,\n"
  2350. "\n"
  2351. " b};",
  2352. format("S s = {\n"
  2353. " a,\n"
  2354. "\n"
  2355. " b\n"
  2356. "};"));
  2357. // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
  2358. // line. However, the formatting looks a bit off and this probably doesn't
  2359. // happen often in practice.
  2360. verifyFormat("static int Variable[1] = {\n"
  2361. " {1000000000000000000000000000000000000}};",
  2362. getLLVMStyleWithColumns(40));
  2363. }
  2364. TEST_F(FormatTest, DesignatedInitializers) {
  2365. verifyFormat("const struct A a = {.a = 1, .b = 2};");
  2366. verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
  2367. " .bbbbbbbbbb = 2,\n"
  2368. " .cccccccccc = 3,\n"
  2369. " .dddddddddd = 4,\n"
  2370. " .eeeeeeeeee = 5};");
  2371. verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
  2372. " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
  2373. " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
  2374. " .ccccccccccccccccccccccccccc = 3,\n"
  2375. " .ddddddddddddddddddddddddddd = 4,\n"
  2376. " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
  2377. verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
  2378. }
  2379. TEST_F(FormatTest, NestedStaticInitializers) {
  2380. verifyFormat("static A x = {{{}}};\n");
  2381. verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
  2382. " {init1, init2, init3, init4}}};",
  2383. getLLVMStyleWithColumns(50));
  2384. verifyFormat("somes Status::global_reps[3] = {\n"
  2385. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  2386. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  2387. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
  2388. getLLVMStyleWithColumns(60));
  2389. verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
  2390. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  2391. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  2392. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
  2393. verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
  2394. " {rect.fRight - rect.fLeft, rect.fBottom - "
  2395. "rect.fTop}};");
  2396. verifyFormat(
  2397. "SomeArrayOfSomeType a = {\n"
  2398. " {{1, 2, 3},\n"
  2399. " {1, 2, 3},\n"
  2400. " {111111111111111111111111111111, 222222222222222222222222222222,\n"
  2401. " 333333333333333333333333333333},\n"
  2402. " {1, 2, 3},\n"
  2403. " {1, 2, 3}}};");
  2404. verifyFormat(
  2405. "SomeArrayOfSomeType a = {\n"
  2406. " {{1, 2, 3}},\n"
  2407. " {{1, 2, 3}},\n"
  2408. " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
  2409. " 333333333333333333333333333333}},\n"
  2410. " {{1, 2, 3}},\n"
  2411. " {{1, 2, 3}}};");
  2412. verifyFormat("struct {\n"
  2413. " unsigned bit;\n"
  2414. " const char *const name;\n"
  2415. "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
  2416. " {kOsWin, \"Windows\"},\n"
  2417. " {kOsLinux, \"Linux\"},\n"
  2418. " {kOsCrOS, \"Chrome OS\"}};");
  2419. verifyFormat("struct {\n"
  2420. " unsigned bit;\n"
  2421. " const char *const name;\n"
  2422. "} kBitsToOs[] = {\n"
  2423. " {kOsMac, \"Mac\"},\n"
  2424. " {kOsWin, \"Windows\"},\n"
  2425. " {kOsLinux, \"Linux\"},\n"
  2426. " {kOsCrOS, \"Chrome OS\"},\n"
  2427. "};");
  2428. }
  2429. TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
  2430. verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2431. " \\\n"
  2432. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
  2433. }
  2434. TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
  2435. verifyFormat("virtual void write(ELFWriter *writerrr,\n"
  2436. " OwningPtr<FileOutputBuffer> &buffer) = 0;");
  2437. // Do break defaulted and deleted functions.
  2438. verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
  2439. " default;",
  2440. getLLVMStyleWithColumns(40));
  2441. verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
  2442. " delete;",
  2443. getLLVMStyleWithColumns(40));
  2444. }
  2445. TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
  2446. verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
  2447. getLLVMStyleWithColumns(40));
  2448. verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  2449. getLLVMStyleWithColumns(40));
  2450. EXPECT_EQ("#define Q \\\n"
  2451. " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n"
  2452. " \"aaaaaaaa.cpp\"",
  2453. format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  2454. getLLVMStyleWithColumns(40)));
  2455. }
  2456. TEST_F(FormatTest, UnderstandsLinePPDirective) {
  2457. EXPECT_EQ("# 123 \"A string literal\"",
  2458. format(" # 123 \"A string literal\""));
  2459. }
  2460. TEST_F(FormatTest, LayoutUnknownPPDirective) {
  2461. EXPECT_EQ("#;", format("#;"));
  2462. verifyFormat("#\n;\n;\n;");
  2463. }
  2464. TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
  2465. EXPECT_EQ("#line 42 \"test\"\n",
  2466. format("# \\\n line \\\n 42 \\\n \"test\"\n"));
  2467. EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n",
  2468. getLLVMStyleWithColumns(12)));
  2469. }
  2470. TEST_F(FormatTest, EndOfFileEndsPPDirective) {
  2471. EXPECT_EQ("#line 42 \"test\"",
  2472. format("# \\\n line \\\n 42 \\\n \"test\""));
  2473. EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B"));
  2474. }
  2475. TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
  2476. verifyFormat("#define A \\x20");
  2477. verifyFormat("#define A \\ x20");
  2478. EXPECT_EQ("#define A \\ x20", format("#define A \\ x20"));
  2479. verifyFormat("#define A ''");
  2480. verifyFormat("#define A ''qqq");
  2481. verifyFormat("#define A `qqq");
  2482. verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
  2483. EXPECT_EQ("const char *c = STRINGIFY(\n"
  2484. "\\na : b);",
  2485. format("const char * c = STRINGIFY(\n"
  2486. "\\na : b);"));
  2487. verifyFormat("a\r\\");
  2488. verifyFormat("a\v\\");
  2489. verifyFormat("a\f\\");
  2490. }
  2491. TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
  2492. verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
  2493. verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
  2494. verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
  2495. // FIXME: We never break before the macro name.
  2496. verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
  2497. verifyFormat("#define A A\n#define A A");
  2498. verifyFormat("#define A(X) A\n#define A A");
  2499. verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
  2500. verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
  2501. }
  2502. TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
  2503. EXPECT_EQ("// somecomment\n"
  2504. "#include \"a.h\"\n"
  2505. "#define A( \\\n"
  2506. " A, B)\n"
  2507. "#include \"b.h\"\n"
  2508. "// somecomment\n",
  2509. format(" // somecomment\n"
  2510. " #include \"a.h\"\n"
  2511. "#define A(A,\\\n"
  2512. " B)\n"
  2513. " #include \"b.h\"\n"
  2514. " // somecomment\n",
  2515. getLLVMStyleWithColumns(13)));
  2516. }
  2517. TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
  2518. TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
  2519. EXPECT_EQ("#define A \\\n"
  2520. " c; \\\n"
  2521. " e;\n"
  2522. "f;",
  2523. format("#define A c; e;\n"
  2524. "f;",
  2525. getLLVMStyleWithColumns(14)));
  2526. }
  2527. TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
  2528. TEST_F(FormatTest, MacroDefinitionInsideStatement) {
  2529. EXPECT_EQ("int x,\n"
  2530. "#define A\n"
  2531. " y;",
  2532. format("int x,\n#define A\ny;"));
  2533. }
  2534. TEST_F(FormatTest, HashInMacroDefinition) {
  2535. EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
  2536. verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
  2537. verifyFormat("#define A \\\n"
  2538. " { \\\n"
  2539. " f(#c); \\\n"
  2540. " }",
  2541. getLLVMStyleWithColumns(11));
  2542. verifyFormat("#define A(X) \\\n"
  2543. " void function##X()",
  2544. getLLVMStyleWithColumns(22));
  2545. verifyFormat("#define A(a, b, c) \\\n"
  2546. " void a##b##c()",
  2547. getLLVMStyleWithColumns(22));
  2548. verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
  2549. }
  2550. TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
  2551. EXPECT_EQ("#define A (x)", format("#define A (x)"));
  2552. EXPECT_EQ("#define A(x)", format("#define A(x)"));
  2553. }
  2554. TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
  2555. EXPECT_EQ("#define A b;", format("#define A \\\n"
  2556. " \\\n"
  2557. " b;",
  2558. getLLVMStyleWithColumns(25)));
  2559. EXPECT_EQ("#define A \\\n"
  2560. " \\\n"
  2561. " a; \\\n"
  2562. " b;",
  2563. format("#define A \\\n"
  2564. " \\\n"
  2565. " a; \\\n"
  2566. " b;",
  2567. getLLVMStyleWithColumns(11)));
  2568. EXPECT_EQ("#define A \\\n"
  2569. " a; \\\n"
  2570. " \\\n"
  2571. " b;",
  2572. format("#define A \\\n"
  2573. " a; \\\n"
  2574. " \\\n"
  2575. " b;",
  2576. getLLVMStyleWithColumns(11)));
  2577. }
  2578. TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
  2579. verifyIncompleteFormat("#define A :");
  2580. verifyFormat("#define SOMECASES \\\n"
  2581. " case 1: \\\n"
  2582. " case 2\n",
  2583. getLLVMStyleWithColumns(20));
  2584. verifyFormat("#define MACRO(a) \\\n"
  2585. " if (a) \\\n"
  2586. " f(); \\\n"
  2587. " else \\\n"
  2588. " g()",
  2589. getLLVMStyleWithColumns(18));
  2590. verifyFormat("#define A template <typename T>");
  2591. verifyIncompleteFormat("#define STR(x) #x\n"
  2592. "f(STR(this_is_a_string_literal{));");
  2593. verifyFormat("#pragma omp threadprivate( \\\n"
  2594. " y)), // expected-warning",
  2595. getLLVMStyleWithColumns(28));
  2596. verifyFormat("#d, = };");
  2597. verifyFormat("#if \"a");
  2598. verifyIncompleteFormat("({\n"
  2599. "#define b \\\n"
  2600. " } \\\n"
  2601. " a\n"
  2602. "a",
  2603. getLLVMStyleWithColumns(15));
  2604. verifyFormat("#define A \\\n"
  2605. " { \\\n"
  2606. " {\n"
  2607. "#define B \\\n"
  2608. " } \\\n"
  2609. " }",
  2610. getLLVMStyleWithColumns(15));
  2611. verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
  2612. verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
  2613. verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
  2614. verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}");
  2615. }
  2616. TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
  2617. verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
  2618. EXPECT_EQ("class A : public QObject {\n"
  2619. " Q_OBJECT\n"
  2620. "\n"
  2621. " A() {}\n"
  2622. "};",
  2623. format("class A : public QObject {\n"
  2624. " Q_OBJECT\n"
  2625. "\n"
  2626. " A() {\n}\n"
  2627. "} ;"));
  2628. EXPECT_EQ("MACRO\n"
  2629. "/*static*/ int i;",
  2630. format("MACRO\n"
  2631. " /*static*/ int i;"));
  2632. EXPECT_EQ("SOME_MACRO\n"
  2633. "namespace {\n"
  2634. "void f();\n"
  2635. "}",
  2636. format("SOME_MACRO\n"
  2637. " namespace {\n"
  2638. "void f( );\n"
  2639. "}"));
  2640. // Only if the identifier contains at least 5 characters.
  2641. EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
  2642. EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
  2643. // Only if everything is upper case.
  2644. EXPECT_EQ("class A : public QObject {\n"
  2645. " Q_Object A() {}\n"
  2646. "};",
  2647. format("class A : public QObject {\n"
  2648. " Q_Object\n"
  2649. " A() {\n}\n"
  2650. "} ;"));
  2651. // Only if the next line can actually start an unwrapped line.
  2652. EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
  2653. format("SOME_WEIRD_LOG_MACRO\n"
  2654. "<< SomeThing;"));
  2655. verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
  2656. "(n, buffers))\n",
  2657. getChromiumStyle(FormatStyle::LK_Cpp));
  2658. }
  2659. TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
  2660. EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  2661. "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  2662. "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  2663. "class X {};\n"
  2664. "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  2665. "int *createScopDetectionPass() { return 0; }",
  2666. format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  2667. " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  2668. " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  2669. " class X {};\n"
  2670. " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  2671. " int *createScopDetectionPass() { return 0; }"));
  2672. // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
  2673. // braces, so that inner block is indented one level more.
  2674. EXPECT_EQ("int q() {\n"
  2675. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  2676. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  2677. " IPC_END_MESSAGE_MAP()\n"
  2678. "}",
  2679. format("int q() {\n"
  2680. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  2681. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  2682. " IPC_END_MESSAGE_MAP()\n"
  2683. "}"));
  2684. // Same inside macros.
  2685. EXPECT_EQ("#define LIST(L) \\\n"
  2686. " L(A) \\\n"
  2687. " L(B) \\\n"
  2688. " L(C)",
  2689. format("#define LIST(L) \\\n"
  2690. " L(A) \\\n"
  2691. " L(B) \\\n"
  2692. " L(C)",
  2693. getGoogleStyle()));
  2694. // These must not be recognized as macros.
  2695. EXPECT_EQ("int q() {\n"
  2696. " f(x);\n"
  2697. " f(x) {}\n"
  2698. " f(x)->g();\n"
  2699. " f(x)->*g();\n"
  2700. " f(x).g();\n"
  2701. " f(x) = x;\n"
  2702. " f(x) += x;\n"
  2703. " f(x) -= x;\n"
  2704. " f(x) *= x;\n"
  2705. " f(x) /= x;\n"
  2706. " f(x) %= x;\n"
  2707. " f(x) &= x;\n"
  2708. " f(x) |= x;\n"
  2709. " f(x) ^= x;\n"
  2710. " f(x) >>= x;\n"
  2711. " f(x) <<= x;\n"
  2712. " f(x)[y].z();\n"
  2713. " LOG(INFO) << x;\n"
  2714. " ifstream(x) >> x;\n"
  2715. "}\n",
  2716. format("int q() {\n"
  2717. " f(x)\n;\n"
  2718. " f(x)\n {}\n"
  2719. " f(x)\n->g();\n"
  2720. " f(x)\n->*g();\n"
  2721. " f(x)\n.g();\n"
  2722. " f(x)\n = x;\n"
  2723. " f(x)\n += x;\n"
  2724. " f(x)\n -= x;\n"
  2725. " f(x)\n *= x;\n"
  2726. " f(x)\n /= x;\n"
  2727. " f(x)\n %= x;\n"
  2728. " f(x)\n &= x;\n"
  2729. " f(x)\n |= x;\n"
  2730. " f(x)\n ^= x;\n"
  2731. " f(x)\n >>= x;\n"
  2732. " f(x)\n <<= x;\n"
  2733. " f(x)\n[y].z();\n"
  2734. " LOG(INFO)\n << x;\n"
  2735. " ifstream(x)\n >> x;\n"
  2736. "}\n"));
  2737. EXPECT_EQ("int q() {\n"
  2738. " F(x)\n"
  2739. " if (1) {\n"
  2740. " }\n"
  2741. " F(x)\n"
  2742. " while (1) {\n"
  2743. " }\n"
  2744. " F(x)\n"
  2745. " G(x);\n"
  2746. " F(x)\n"
  2747. " try {\n"
  2748. " Q();\n"
  2749. " } catch (...) {\n"
  2750. " }\n"
  2751. "}\n",
  2752. format("int q() {\n"
  2753. "F(x)\n"
  2754. "if (1) {}\n"
  2755. "F(x)\n"
  2756. "while (1) {}\n"
  2757. "F(x)\n"
  2758. "G(x);\n"
  2759. "F(x)\n"
  2760. "try { Q(); } catch (...) {}\n"
  2761. "}\n"));
  2762. EXPECT_EQ("class A {\n"
  2763. " A() : t(0) {}\n"
  2764. " A(int i) noexcept() : {}\n"
  2765. " A(X x)\n" // FIXME: function-level try blocks are broken.
  2766. " try : t(0) {\n"
  2767. " } catch (...) {\n"
  2768. " }\n"
  2769. "};",
  2770. format("class A {\n"
  2771. " A()\n : t(0) {}\n"
  2772. " A(int i)\n noexcept() : {}\n"
  2773. " A(X x)\n"
  2774. " try : t(0) {} catch (...) {}\n"
  2775. "};"));
  2776. EXPECT_EQ("class SomeClass {\n"
  2777. "public:\n"
  2778. " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2779. "};",
  2780. format("class SomeClass {\n"
  2781. "public:\n"
  2782. " SomeClass()\n"
  2783. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2784. "};"));
  2785. EXPECT_EQ("class SomeClass {\n"
  2786. "public:\n"
  2787. " SomeClass()\n"
  2788. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2789. "};",
  2790. format("class SomeClass {\n"
  2791. "public:\n"
  2792. " SomeClass()\n"
  2793. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2794. "};",
  2795. getLLVMStyleWithColumns(40)));
  2796. verifyFormat("MACRO(>)");
  2797. }
  2798. TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
  2799. verifyFormat("#define A \\\n"
  2800. " f({ \\\n"
  2801. " g(); \\\n"
  2802. " });",
  2803. getLLVMStyleWithColumns(11));
  2804. }
  2805. TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
  2806. EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
  2807. }
  2808. TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
  2809. verifyFormat("{\n { a #c; }\n}");
  2810. }
  2811. TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
  2812. EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
  2813. format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
  2814. EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
  2815. format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
  2816. }
  2817. TEST_F(FormatTest, EscapedNewlines) {
  2818. EXPECT_EQ(
  2819. "#define A \\\n int i; \\\n int j;",
  2820. format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
  2821. EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
  2822. EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
  2823. EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/"));
  2824. EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
  2825. }
  2826. TEST_F(FormatTest, DontCrashOnBlockComments) {
  2827. EXPECT_EQ(
  2828. "int xxxxxxxxx; /* "
  2829. "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
  2830. "zzzzzz\n"
  2831. "0*/",
  2832. format("int xxxxxxxxx; /* "
  2833. "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
  2834. "0*/"));
  2835. }
  2836. TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
  2837. verifyFormat("#define A \\\n"
  2838. " int v( \\\n"
  2839. " a); \\\n"
  2840. " int i;",
  2841. getLLVMStyleWithColumns(11));
  2842. }
  2843. TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
  2844. EXPECT_EQ(
  2845. "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2846. " \\\n"
  2847. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2848. "\n"
  2849. "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2850. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
  2851. format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2852. "\\\n"
  2853. "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2854. " \n"
  2855. " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2856. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
  2857. }
  2858. TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
  2859. EXPECT_EQ("int\n"
  2860. "#define A\n"
  2861. " a;",
  2862. format("int\n#define A\na;"));
  2863. verifyFormat("functionCallTo(\n"
  2864. " someOtherFunction(\n"
  2865. " withSomeParameters, whichInSequence,\n"
  2866. " areLongerThanALine(andAnotherCall,\n"
  2867. "#define A B\n"
  2868. " withMoreParamters,\n"
  2869. " whichStronglyInfluenceTheLayout),\n"
  2870. " andMoreParameters),\n"
  2871. " trailing);",
  2872. getLLVMStyleWithColumns(69));
  2873. verifyFormat("Foo::Foo()\n"
  2874. "#ifdef BAR\n"
  2875. " : baz(0)\n"
  2876. "#endif\n"
  2877. "{\n"
  2878. "}");
  2879. verifyFormat("void f() {\n"
  2880. " if (true)\n"
  2881. "#ifdef A\n"
  2882. " f(42);\n"
  2883. " x();\n"
  2884. "#else\n"
  2885. " g();\n"
  2886. " x();\n"
  2887. "#endif\n"
  2888. "}");
  2889. verifyFormat("void f(param1, param2,\n"
  2890. " param3,\n"
  2891. "#ifdef A\n"
  2892. " param4(param5,\n"
  2893. "#ifdef A1\n"
  2894. " param6,\n"
  2895. "#ifdef A2\n"
  2896. " param7),\n"
  2897. "#else\n"
  2898. " param8),\n"
  2899. " param9,\n"
  2900. "#endif\n"
  2901. " param10,\n"
  2902. "#endif\n"
  2903. " param11)\n"
  2904. "#else\n"
  2905. " param12)\n"
  2906. "#endif\n"
  2907. "{\n"
  2908. " x();\n"
  2909. "}",
  2910. getLLVMStyleWithColumns(28));
  2911. verifyFormat("#if 1\n"
  2912. "int i;");
  2913. verifyFormat("#if 1\n"
  2914. "#endif\n"
  2915. "#if 1\n"
  2916. "#else\n"
  2917. "#endif\n");
  2918. verifyFormat("DEBUG({\n"
  2919. " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2920. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  2921. "});\n"
  2922. "#if a\n"
  2923. "#else\n"
  2924. "#endif");
  2925. verifyIncompleteFormat("void f(\n"
  2926. "#if A\n"
  2927. " );\n"
  2928. "#else\n"
  2929. "#endif");
  2930. }
  2931. TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
  2932. verifyFormat("#endif\n"
  2933. "#if B");
  2934. }
  2935. TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
  2936. FormatStyle SingleLine = getLLVMStyle();
  2937. SingleLine.AllowShortIfStatementsOnASingleLine = true;
  2938. verifyFormat("#if 0\n"
  2939. "#elif 1\n"
  2940. "#endif\n"
  2941. "void foo() {\n"
  2942. " if (test) foo2();\n"
  2943. "}",
  2944. SingleLine);
  2945. }
  2946. TEST_F(FormatTest, LayoutBlockInsideParens) {
  2947. verifyFormat("functionCall({ int i; });");
  2948. verifyFormat("functionCall({\n"
  2949. " int i;\n"
  2950. " int j;\n"
  2951. "});");
  2952. verifyFormat("functionCall(\n"
  2953. " {\n"
  2954. " int i;\n"
  2955. " int j;\n"
  2956. " },\n"
  2957. " aaaa, bbbb, cccc);");
  2958. verifyFormat("functionA(functionB({\n"
  2959. " int i;\n"
  2960. " int j;\n"
  2961. " }),\n"
  2962. " aaaa, bbbb, cccc);");
  2963. verifyFormat("functionCall(\n"
  2964. " {\n"
  2965. " int i;\n"
  2966. " int j;\n"
  2967. " },\n"
  2968. " aaaa, bbbb, // comment\n"
  2969. " cccc);");
  2970. verifyFormat("functionA(functionB({\n"
  2971. " int i;\n"
  2972. " int j;\n"
  2973. " }),\n"
  2974. " aaaa, bbbb, // comment\n"
  2975. " cccc);");
  2976. verifyFormat("functionCall(aaaa, bbbb, { int i; });");
  2977. verifyFormat("functionCall(aaaa, bbbb, {\n"
  2978. " int i;\n"
  2979. " int j;\n"
  2980. "});");
  2981. verifyFormat(
  2982. "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
  2983. " {\n"
  2984. " int i; // break\n"
  2985. " },\n"
  2986. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  2987. " ccccccccccccccccc));");
  2988. verifyFormat("DEBUG({\n"
  2989. " if (a)\n"
  2990. " f();\n"
  2991. "});");
  2992. }
  2993. TEST_F(FormatTest, LayoutBlockInsideStatement) {
  2994. EXPECT_EQ("SOME_MACRO { int i; }\n"
  2995. "int i;",
  2996. format(" SOME_MACRO {int i;} int i;"));
  2997. }
  2998. TEST_F(FormatTest, LayoutNestedBlocks) {
  2999. verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
  3000. " struct s {\n"
  3001. " int i;\n"
  3002. " };\n"
  3003. " s kBitsToOs[] = {{10}};\n"
  3004. " for (int i = 0; i < 10; ++i)\n"
  3005. " return;\n"
  3006. "}");
  3007. verifyFormat("call(parameter, {\n"
  3008. " something();\n"
  3009. " // Comment using all columns.\n"
  3010. " somethingelse();\n"
  3011. "});",
  3012. getLLVMStyleWithColumns(40));
  3013. verifyFormat("DEBUG( //\n"
  3014. " { f(); }, a);");
  3015. verifyFormat("DEBUG( //\n"
  3016. " {\n"
  3017. " f(); //\n"
  3018. " },\n"
  3019. " a);");
  3020. EXPECT_EQ("call(parameter, {\n"
  3021. " something();\n"
  3022. " // Comment too\n"
  3023. " // looooooooooong.\n"
  3024. " somethingElse();\n"
  3025. "});",
  3026. format("call(parameter, {\n"
  3027. " something();\n"
  3028. " // Comment too looooooooooong.\n"
  3029. " somethingElse();\n"
  3030. "});",
  3031. getLLVMStyleWithColumns(29)));
  3032. EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });"));
  3033. EXPECT_EQ("DEBUG({ // comment\n"
  3034. " int i;\n"
  3035. "});",
  3036. format("DEBUG({ // comment\n"
  3037. "int i;\n"
  3038. "});"));
  3039. EXPECT_EQ("DEBUG({\n"
  3040. " int i;\n"
  3041. "\n"
  3042. " // comment\n"
  3043. " int j;\n"
  3044. "});",
  3045. format("DEBUG({\n"
  3046. " int i;\n"
  3047. "\n"
  3048. " // comment\n"
  3049. " int j;\n"
  3050. "});"));
  3051. verifyFormat("DEBUG({\n"
  3052. " if (a)\n"
  3053. " return;\n"
  3054. "});");
  3055. verifyGoogleFormat("DEBUG({\n"
  3056. " if (a) return;\n"
  3057. "});");
  3058. FormatStyle Style = getGoogleStyle();
  3059. Style.ColumnLimit = 45;
  3060. verifyFormat("Debug(aaaaa,\n"
  3061. " {\n"
  3062. " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
  3063. " },\n"
  3064. " a);",
  3065. Style);
  3066. verifyFormat("SomeFunction({MACRO({ return output; }), b});");
  3067. verifyNoCrash("^{v^{a}}");
  3068. }
  3069. TEST_F(FormatTest, FormatNestedBlocksInMacros) {
  3070. EXPECT_EQ("#define MACRO() \\\n"
  3071. " Debug(aaa, /* force line break */ \\\n"
  3072. " { \\\n"
  3073. " int i; \\\n"
  3074. " int j; \\\n"
  3075. " })",
  3076. format("#define MACRO() Debug(aaa, /* force line break */ \\\n"
  3077. " { int i; int j; })",
  3078. getGoogleStyle()));
  3079. EXPECT_EQ("#define A \\\n"
  3080. " [] { \\\n"
  3081. " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
  3082. " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
  3083. " }",
  3084. format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
  3085. "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
  3086. getGoogleStyle()));
  3087. }
  3088. TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
  3089. EXPECT_EQ("{}", format("{}"));
  3090. verifyFormat("enum E {};");
  3091. verifyFormat("enum E {}");
  3092. }
  3093. TEST_F(FormatTest, FormatBeginBlockEndMacros) {
  3094. FormatStyle Style = getLLVMStyle();
  3095. Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
  3096. Style.MacroBlockEnd = "^[A-Z_]+_END$";
  3097. verifyFormat("FOO_BEGIN\n"
  3098. " FOO_ENTRY\n"
  3099. "FOO_END", Style);
  3100. verifyFormat("FOO_BEGIN\n"
  3101. " NESTED_FOO_BEGIN\n"
  3102. " NESTED_FOO_ENTRY\n"
  3103. " NESTED_FOO_END\n"
  3104. "FOO_END", Style);
  3105. verifyFormat("FOO_BEGIN(Foo, Bar)\n"
  3106. " int x;\n"
  3107. " x = 1;\n"
  3108. "FOO_END(Baz)", Style);
  3109. }
  3110. //===----------------------------------------------------------------------===//
  3111. // Line break tests.
  3112. //===----------------------------------------------------------------------===//
  3113. TEST_F(FormatTest, PreventConfusingIndents) {
  3114. verifyFormat(
  3115. "void f() {\n"
  3116. " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
  3117. " parameter, parameter, parameter)),\n"
  3118. " SecondLongCall(parameter));\n"
  3119. "}");
  3120. verifyFormat(
  3121. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3122. " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3123. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3124. " aaaaaaaaaaaaaaaaaaaaaaaa);");
  3125. verifyFormat(
  3126. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3127. " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
  3128. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  3129. " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
  3130. verifyFormat(
  3131. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  3132. " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
  3133. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
  3134. " aaaaaaaaaaaaaaaaaaaaaaaa>;");
  3135. verifyFormat("int a = bbbb && ccc && fffff(\n"
  3136. "#define A Just forcing a new line\n"
  3137. " ddd);");
  3138. }
  3139. TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
  3140. verifyFormat(
  3141. "bool aaaaaaa =\n"
  3142. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
  3143. " bbbbbbbb();");
  3144. verifyFormat(
  3145. "bool aaaaaaa =\n"
  3146. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
  3147. " bbbbbbbb();");
  3148. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  3149. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
  3150. " ccccccccc == ddddddddddd;");
  3151. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  3152. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
  3153. " ccccccccc == ddddddddddd;");
  3154. verifyFormat(
  3155. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  3156. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
  3157. " ccccccccc == ddddddddddd;");
  3158. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  3159. " aaaaaa) &&\n"
  3160. " bbbbbb && cccccc;");
  3161. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  3162. " aaaaaa) >>\n"
  3163. " bbbbbb;");
  3164. verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
  3165. " SourceMgr.getSpellingColumnNumber(\n"
  3166. " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
  3167. " 1);");
  3168. verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3169. " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
  3170. " cccccc) {\n}");
  3171. verifyFormat("b = a &&\n"
  3172. " // Comment\n"
  3173. " b.c && d;");
  3174. // If the LHS of a comparison is not a binary expression itself, the
  3175. // additional linebreak confuses many people.
  3176. verifyFormat(
  3177. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3178. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
  3179. "}");
  3180. verifyFormat(
  3181. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3182. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  3183. "}");
  3184. verifyFormat(
  3185. "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
  3186. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  3187. "}");
  3188. // Even explicit parentheses stress the precedence enough to make the
  3189. // additional break unnecessary.
  3190. verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3191. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  3192. "}");
  3193. // This cases is borderline, but with the indentation it is still readable.
  3194. verifyFormat(
  3195. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3196. " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3197. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  3198. "}",
  3199. getLLVMStyleWithColumns(75));
  3200. // If the LHS is a binary expression, we should still use the additional break
  3201. // as otherwise the formatting hides the operator precedence.
  3202. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3203. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3204. " 5) {\n"
  3205. "}");
  3206. FormatStyle OnePerLine = getLLVMStyle();
  3207. OnePerLine.BinPackParameters = false;
  3208. verifyFormat(
  3209. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3210. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3211. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
  3212. OnePerLine);
  3213. }
  3214. TEST_F(FormatTest, ExpressionIndentation) {
  3215. verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3216. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3217. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3218. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3219. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
  3220. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
  3221. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3222. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
  3223. " ccccccccccccccccccccccccccccccccccccccccc;");
  3224. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3225. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3226. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3227. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3228. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3229. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3230. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3231. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3232. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3233. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3234. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3235. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3236. verifyFormat("if () {\n"
  3237. "} else if (aaaaa &&\n"
  3238. " bbbbb > // break\n"
  3239. " ccccc) {\n"
  3240. "}");
  3241. // Presence of a trailing comment used to change indentation of b.
  3242. verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
  3243. " b;\n"
  3244. "return aaaaaaaaaaaaaaaaaaa +\n"
  3245. " b; //",
  3246. getLLVMStyleWithColumns(30));
  3247. }
  3248. TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
  3249. // Not sure what the best system is here. Like this, the LHS can be found
  3250. // immediately above an operator (everything with the same or a higher
  3251. // indent). The RHS is aligned right of the operator and so compasses
  3252. // everything until something with the same indent as the operator is found.
  3253. // FIXME: Is this a good system?
  3254. FormatStyle Style = getLLVMStyle();
  3255. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  3256. verifyFormat(
  3257. "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3258. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3259. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3260. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3261. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3262. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3263. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3264. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3265. " > ccccccccccccccccccccccccccccccccccccccccc;",
  3266. Style);
  3267. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3268. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3269. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3270. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3271. Style);
  3272. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3273. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3274. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3275. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3276. Style);
  3277. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3278. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3279. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3280. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3281. Style);
  3282. verifyFormat("if () {\n"
  3283. "} else if (aaaaa\n"
  3284. " && bbbbb // break\n"
  3285. " > ccccc) {\n"
  3286. "}",
  3287. Style);
  3288. verifyFormat("return (a)\n"
  3289. " // comment\n"
  3290. " + b;",
  3291. Style);
  3292. verifyFormat(
  3293. "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3294. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3295. " + cc;",
  3296. Style);
  3297. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3298. " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  3299. Style);
  3300. // Forced by comments.
  3301. verifyFormat(
  3302. "unsigned ContentSize =\n"
  3303. " sizeof(int16_t) // DWARF ARange version number\n"
  3304. " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
  3305. " + sizeof(int8_t) // Pointer Size (in bytes)\n"
  3306. " + sizeof(int8_t); // Segment Size (in bytes)");
  3307. verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
  3308. " == boost::fusion::at_c<1>(iiii).second;",
  3309. Style);
  3310. Style.ColumnLimit = 60;
  3311. verifyFormat("zzzzzzzzzz\n"
  3312. " = bbbbbbbbbbbbbbbbb\n"
  3313. " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3314. Style);
  3315. }
  3316. TEST_F(FormatTest, NoOperandAlignment) {
  3317. FormatStyle Style = getLLVMStyle();
  3318. Style.AlignOperands = false;
  3319. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
  3320. verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3321. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3322. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3323. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3324. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3325. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3326. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3327. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3328. " > ccccccccccccccccccccccccccccccccccccccccc;",
  3329. Style);
  3330. verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3331. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3332. " + cc;",
  3333. Style);
  3334. verifyFormat("int a = aa\n"
  3335. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3336. " * cccccccccccccccccccccccccccccccccccc;",
  3337. Style);
  3338. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  3339. verifyFormat("return (a > b\n"
  3340. " // comment1\n"
  3341. " // comment2\n"
  3342. " || c);",
  3343. Style);
  3344. }
  3345. TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
  3346. FormatStyle Style = getLLVMStyle();
  3347. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
  3348. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3349. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3350. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
  3351. Style);
  3352. }
  3353. TEST_F(FormatTest, ConstructorInitializers) {
  3354. verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
  3355. verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
  3356. getLLVMStyleWithColumns(45));
  3357. verifyFormat("Constructor()\n"
  3358. " : Inttializer(FitsOnTheLine) {}",
  3359. getLLVMStyleWithColumns(44));
  3360. verifyFormat("Constructor()\n"
  3361. " : Inttializer(FitsOnTheLine) {}",
  3362. getLLVMStyleWithColumns(43));
  3363. verifyFormat("template <typename T>\n"
  3364. "Constructor() : Initializer(FitsOnTheLine) {}",
  3365. getLLVMStyleWithColumns(45));
  3366. verifyFormat(
  3367. "SomeClass::Constructor()\n"
  3368. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  3369. verifyFormat(
  3370. "SomeClass::Constructor()\n"
  3371. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3372. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
  3373. verifyFormat(
  3374. "SomeClass::Constructor()\n"
  3375. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3376. " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  3377. verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3378. " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3379. " : aaaaaaaaaa(aaaaaa) {}");
  3380. verifyFormat("Constructor()\n"
  3381. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3382. " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3383. " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3384. " aaaaaaaaaaaaaaaaaaaaaaa() {}");
  3385. verifyFormat("Constructor()\n"
  3386. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3387. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3388. verifyFormat("Constructor(int Parameter = 0)\n"
  3389. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
  3390. " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
  3391. verifyFormat("Constructor()\n"
  3392. " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
  3393. "}",
  3394. getLLVMStyleWithColumns(60));
  3395. verifyFormat("Constructor()\n"
  3396. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3397. " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
  3398. // Here a line could be saved by splitting the second initializer onto two
  3399. // lines, but that is not desirable.
  3400. verifyFormat("Constructor()\n"
  3401. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3402. " aaaaaaaaaaa(aaaaaaaaaaa),\n"
  3403. " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3404. FormatStyle OnePerLine = getLLVMStyle();
  3405. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  3406. OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
  3407. verifyFormat("SomeClass::Constructor()\n"
  3408. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3409. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3410. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  3411. OnePerLine);
  3412. verifyFormat("SomeClass::Constructor()\n"
  3413. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
  3414. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3415. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  3416. OnePerLine);
  3417. verifyFormat("MyClass::MyClass(int var)\n"
  3418. " : some_var_(var), // 4 space indent\n"
  3419. " some_other_var_(var + 1) { // lined up\n"
  3420. "}",
  3421. OnePerLine);
  3422. verifyFormat("Constructor()\n"
  3423. " : aaaaa(aaaaaa),\n"
  3424. " aaaaa(aaaaaa),\n"
  3425. " aaaaa(aaaaaa),\n"
  3426. " aaaaa(aaaaaa),\n"
  3427. " aaaaa(aaaaaa) {}",
  3428. OnePerLine);
  3429. verifyFormat("Constructor()\n"
  3430. " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
  3431. " aaaaaaaaaaaaaaaaaaaaaa) {}",
  3432. OnePerLine);
  3433. OnePerLine.BinPackParameters = false;
  3434. verifyFormat(
  3435. "Constructor()\n"
  3436. " : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3437. " aaaaaaaaaaa().aaa(),\n"
  3438. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3439. OnePerLine);
  3440. OnePerLine.ColumnLimit = 60;
  3441. verifyFormat("Constructor()\n"
  3442. " : aaaaaaaaaaaaaaaaaaaa(a),\n"
  3443. " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
  3444. OnePerLine);
  3445. EXPECT_EQ("Constructor()\n"
  3446. " : // Comment forcing unwanted break.\n"
  3447. " aaaa(aaaa) {}",
  3448. format("Constructor() :\n"
  3449. " // Comment forcing unwanted break.\n"
  3450. " aaaa(aaaa) {}"));
  3451. }
  3452. TEST_F(FormatTest, MemoizationTests) {
  3453. // This breaks if the memoization lookup does not take \c Indent and
  3454. // \c LastSpace into account.
  3455. verifyFormat(
  3456. "extern CFRunLoopTimerRef\n"
  3457. "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
  3458. " CFTimeInterval interval, CFOptionFlags flags,\n"
  3459. " CFIndex order, CFRunLoopTimerCallBack callout,\n"
  3460. " CFRunLoopTimerContext *context) {}");
  3461. // Deep nesting somewhat works around our memoization.
  3462. verifyFormat(
  3463. "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3464. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3465. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3466. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3467. " aaaaa())))))))))))))))))))))))))))))))))))))));",
  3468. getLLVMStyleWithColumns(65));
  3469. verifyFormat(
  3470. "aaaaa(\n"
  3471. " aaaaa,\n"
  3472. " aaaaa(\n"
  3473. " aaaaa,\n"
  3474. " aaaaa(\n"
  3475. " aaaaa,\n"
  3476. " aaaaa(\n"
  3477. " aaaaa,\n"
  3478. " aaaaa(\n"
  3479. " aaaaa,\n"
  3480. " aaaaa(\n"
  3481. " aaaaa,\n"
  3482. " aaaaa(\n"
  3483. " aaaaa,\n"
  3484. " aaaaa(\n"
  3485. " aaaaa,\n"
  3486. " aaaaa(\n"
  3487. " aaaaa,\n"
  3488. " aaaaa(\n"
  3489. " aaaaa,\n"
  3490. " aaaaa(\n"
  3491. " aaaaa,\n"
  3492. " aaaaa(\n"
  3493. " aaaaa,\n"
  3494. " aaaaa))))))))))));",
  3495. getLLVMStyleWithColumns(65));
  3496. verifyFormat(
  3497. "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"
  3498. " a),\n"
  3499. " a),\n"
  3500. " a),\n"
  3501. " a),\n"
  3502. " a),\n"
  3503. " a),\n"
  3504. " a),\n"
  3505. " a),\n"
  3506. " a),\n"
  3507. " a),\n"
  3508. " a),\n"
  3509. " a),\n"
  3510. " a),\n"
  3511. " a),\n"
  3512. " a),\n"
  3513. " a),\n"
  3514. " a)",
  3515. getLLVMStyleWithColumns(65));
  3516. // This test takes VERY long when memoization is broken.
  3517. FormatStyle OnePerLine = getLLVMStyle();
  3518. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  3519. OnePerLine.BinPackParameters = false;
  3520. std::string input = "Constructor()\n"
  3521. " : aaaa(a,\n";
  3522. for (unsigned i = 0, e = 80; i != e; ++i) {
  3523. input += " a,\n";
  3524. }
  3525. input += " a) {}";
  3526. verifyFormat(input, OnePerLine);
  3527. }
  3528. TEST_F(FormatTest, BreaksAsHighAsPossible) {
  3529. verifyFormat(
  3530. "void f() {\n"
  3531. " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
  3532. " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
  3533. " f();\n"
  3534. "}");
  3535. verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
  3536. " Intervals[i - 1].getRange().getLast()) {\n}");
  3537. }
  3538. TEST_F(FormatTest, BreaksFunctionDeclarations) {
  3539. // Principially, we break function declarations in a certain order:
  3540. // 1) break amongst arguments.
  3541. verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
  3542. " Cccccccccccccc cccccccccccccc);");
  3543. verifyFormat("template <class TemplateIt>\n"
  3544. "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
  3545. " TemplateIt *stop) {}");
  3546. // 2) break after return type.
  3547. verifyFormat(
  3548. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3549. "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
  3550. getGoogleStyle());
  3551. // 3) break after (.
  3552. verifyFormat(
  3553. "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
  3554. " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
  3555. getGoogleStyle());
  3556. // 4) break before after nested name specifiers.
  3557. verifyFormat(
  3558. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3559. "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
  3560. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
  3561. getGoogleStyle());
  3562. // However, there are exceptions, if a sufficient amount of lines can be
  3563. // saved.
  3564. // FIXME: The precise cut-offs wrt. the number of saved lines might need some
  3565. // more adjusting.
  3566. verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  3567. " Cccccccccccccc cccccccccc,\n"
  3568. " Cccccccccccccc cccccccccc,\n"
  3569. " Cccccccccccccc cccccccccc,\n"
  3570. " Cccccccccccccc cccccccccc);");
  3571. verifyFormat(
  3572. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3573. "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3574. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3575. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
  3576. getGoogleStyle());
  3577. verifyFormat(
  3578. "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  3579. " Cccccccccccccc cccccccccc,\n"
  3580. " Cccccccccccccc cccccccccc,\n"
  3581. " Cccccccccccccc cccccccccc,\n"
  3582. " Cccccccccccccc cccccccccc,\n"
  3583. " Cccccccccccccc cccccccccc,\n"
  3584. " Cccccccccccccc cccccccccc);");
  3585. verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3586. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3587. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3588. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3589. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
  3590. // Break after multi-line parameters.
  3591. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3592. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3593. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3594. " bbbb bbbb);");
  3595. verifyFormat("void SomeLoooooooooooongFunction(\n"
  3596. " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  3597. " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3598. " int bbbbbbbbbbbbb);");
  3599. // Treat overloaded operators like other functions.
  3600. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3601. "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
  3602. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3603. "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
  3604. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3605. "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
  3606. verifyGoogleFormat(
  3607. "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
  3608. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  3609. verifyGoogleFormat(
  3610. "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
  3611. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  3612. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3613. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  3614. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
  3615. "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  3616. verifyGoogleFormat(
  3617. "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
  3618. "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3619. " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
  3620. verifyGoogleFormat(
  3621. "template <typename T>\n"
  3622. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3623. "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
  3624. " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
  3625. FormatStyle Style = getLLVMStyle();
  3626. Style.PointerAlignment = FormatStyle::PAS_Left;
  3627. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3628. " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
  3629. Style);
  3630. verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
  3631. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3632. Style);
  3633. }
  3634. TEST_F(FormatTest, TrailingReturnType) {
  3635. verifyFormat("auto foo() -> int;\n");
  3636. verifyFormat("struct S {\n"
  3637. " auto bar() const -> int;\n"
  3638. "};");
  3639. verifyFormat("template <size_t Order, typename T>\n"
  3640. "auto load_img(const std::string &filename)\n"
  3641. " -> alias::tensor<Order, T, mem::tag::cpu> {}");
  3642. verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
  3643. " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
  3644. verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
  3645. verifyFormat("template <typename T>\n"
  3646. "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
  3647. " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
  3648. // Not trailing return types.
  3649. verifyFormat("void f() { auto a = b->c(); }");
  3650. }
  3651. TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
  3652. // Avoid breaking before trailing 'const' or other trailing annotations, if
  3653. // they are not function-like.
  3654. FormatStyle Style = getGoogleStyle();
  3655. Style.ColumnLimit = 47;
  3656. verifyFormat("void someLongFunction(\n"
  3657. " int someLoooooooooooooongParameter) const {\n}",
  3658. getLLVMStyleWithColumns(47));
  3659. verifyFormat("LoooooongReturnType\n"
  3660. "someLoooooooongFunction() const {}",
  3661. getLLVMStyleWithColumns(47));
  3662. verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
  3663. " const {}",
  3664. Style);
  3665. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3666. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
  3667. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3668. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
  3669. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3670. " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
  3671. verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
  3672. " aaaaaaaaaaa aaaaa) const override;");
  3673. verifyGoogleFormat(
  3674. "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3675. " const override;");
  3676. // Even if the first parameter has to be wrapped.
  3677. verifyFormat("void someLongFunction(\n"
  3678. " int someLongParameter) const {}",
  3679. getLLVMStyleWithColumns(46));
  3680. verifyFormat("void someLongFunction(\n"
  3681. " int someLongParameter) const {}",
  3682. Style);
  3683. verifyFormat("void someLongFunction(\n"
  3684. " int someLongParameter) override {}",
  3685. Style);
  3686. verifyFormat("void someLongFunction(\n"
  3687. " int someLongParameter) OVERRIDE {}",
  3688. Style);
  3689. verifyFormat("void someLongFunction(\n"
  3690. " int someLongParameter) final {}",
  3691. Style);
  3692. verifyFormat("void someLongFunction(\n"
  3693. " int someLongParameter) FINAL {}",
  3694. Style);
  3695. verifyFormat("void someLongFunction(\n"
  3696. " int parameter) const override {}",
  3697. Style);
  3698. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  3699. verifyFormat("void someLongFunction(\n"
  3700. " int someLongParameter) const\n"
  3701. "{\n"
  3702. "}",
  3703. Style);
  3704. // Unless these are unknown annotations.
  3705. verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
  3706. " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3707. " LONG_AND_UGLY_ANNOTATION;");
  3708. // Breaking before function-like trailing annotations is fine to keep them
  3709. // close to their arguments.
  3710. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3711. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  3712. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  3713. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  3714. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  3715. " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
  3716. verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
  3717. " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
  3718. verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
  3719. verifyFormat(
  3720. "void aaaaaaaaaaaaaaaaaa()\n"
  3721. " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3722. " aaaaaaaaaaaaaaaaaaaaaaaaa));");
  3723. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3724. " __attribute__((unused));");
  3725. verifyGoogleFormat(
  3726. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3727. " GUARDED_BY(aaaaaaaaaaaa);");
  3728. verifyGoogleFormat(
  3729. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3730. " GUARDED_BY(aaaaaaaaaaaa);");
  3731. verifyGoogleFormat(
  3732. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
  3733. " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3734. verifyGoogleFormat(
  3735. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
  3736. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3737. }
  3738. TEST_F(FormatTest, FunctionAnnotations) {
  3739. verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
  3740. "int OldFunction(const string &parameter) {}");
  3741. verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
  3742. "string OldFunction(const string &parameter) {}");
  3743. verifyFormat("template <typename T>\n"
  3744. "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
  3745. "string OldFunction(const string &parameter) {}");
  3746. // Not function annotations.
  3747. verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3748. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
  3749. verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
  3750. " ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
  3751. verifyFormat("MACRO(abc).function() // wrap\n"
  3752. " << abc;");
  3753. verifyFormat("MACRO(abc)->function() // wrap\n"
  3754. " << abc;");
  3755. verifyFormat("MACRO(abc)::function() // wrap\n"
  3756. " << abc;");
  3757. }
  3758. TEST_F(FormatTest, BreaksDesireably) {
  3759. verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  3760. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  3761. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
  3762. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3763. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  3764. "}");
  3765. verifyFormat(
  3766. "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3767. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3768. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3769. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3770. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3771. verifyFormat(
  3772. "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3773. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  3774. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3775. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
  3776. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3777. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3778. verifyFormat(
  3779. "void f() {\n"
  3780. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
  3781. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3782. "}");
  3783. verifyFormat(
  3784. "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3785. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3786. verifyFormat(
  3787. "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3788. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3789. verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3790. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3791. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3792. // Indent consistently independent of call expression and unary operator.
  3793. verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3794. " dddddddddddddddddddddddddddddd));");
  3795. verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3796. " dddddddddddddddddddddddddddddd));");
  3797. verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
  3798. " dddddddddddddddddddddddddddddd));");
  3799. // This test case breaks on an incorrect memoization, i.e. an optimization not
  3800. // taking into account the StopAt value.
  3801. verifyFormat(
  3802. "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3803. " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3804. " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3805. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3806. verifyFormat("{\n {\n {\n"
  3807. " Annotation.SpaceRequiredBefore =\n"
  3808. " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
  3809. " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
  3810. " }\n }\n}");
  3811. // Break on an outer level if there was a break on an inner level.
  3812. EXPECT_EQ("f(g(h(a, // comment\n"
  3813. " b, c),\n"
  3814. " d, e),\n"
  3815. " x, y);",
  3816. format("f(g(h(a, // comment\n"
  3817. " b, c), d, e), x, y);"));
  3818. // Prefer breaking similar line breaks.
  3819. verifyFormat(
  3820. "const int kTrackingOptions = NSTrackingMouseMoved |\n"
  3821. " NSTrackingMouseEnteredAndExited |\n"
  3822. " NSTrackingActiveAlways;");
  3823. }
  3824. TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
  3825. FormatStyle NoBinPacking = getGoogleStyle();
  3826. NoBinPacking.BinPackParameters = false;
  3827. NoBinPacking.BinPackArguments = true;
  3828. verifyFormat("void f() {\n"
  3829. " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
  3830. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3831. "}",
  3832. NoBinPacking);
  3833. verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
  3834. " int aaaaaaaaaaaaaaaaaaaa,\n"
  3835. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3836. NoBinPacking);
  3837. NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
  3838. verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3839. " vector<int> bbbbbbbbbbbbbbb);",
  3840. NoBinPacking);
  3841. // FIXME: This behavior difference is probably not wanted. However, currently
  3842. // we cannot distinguish BreakBeforeParameter being set because of the wrapped
  3843. // template arguments from BreakBeforeParameter being set because of the
  3844. // one-per-line formatting.
  3845. verifyFormat(
  3846. "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3847. " aaaaaaaaaa> aaaaaaaaaa);",
  3848. NoBinPacking);
  3849. verifyFormat(
  3850. "void fffffffffff(\n"
  3851. " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
  3852. " aaaaaaaaaa);");
  3853. }
  3854. TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
  3855. FormatStyle NoBinPacking = getGoogleStyle();
  3856. NoBinPacking.BinPackParameters = false;
  3857. NoBinPacking.BinPackArguments = false;
  3858. verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
  3859. " aaaaaaaaaaaaaaaaaaaa,\n"
  3860. " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
  3861. NoBinPacking);
  3862. verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
  3863. " aaaaaaaaaaaaa,\n"
  3864. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
  3865. NoBinPacking);
  3866. verifyFormat(
  3867. "aaaaaaaa(aaaaaaaaaaaaa,\n"
  3868. " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3869. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  3870. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3871. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
  3872. NoBinPacking);
  3873. verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  3874. " .aaaaaaaaaaaaaaaaaa();",
  3875. NoBinPacking);
  3876. verifyFormat("void f() {\n"
  3877. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3878. " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
  3879. "}",
  3880. NoBinPacking);
  3881. verifyFormat(
  3882. "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3883. " aaaaaaaaaaaa,\n"
  3884. " aaaaaaaaaaaa);",
  3885. NoBinPacking);
  3886. verifyFormat(
  3887. "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
  3888. " ddddddddddddddddddddddddddddd),\n"
  3889. " test);",
  3890. NoBinPacking);
  3891. verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3892. " aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3893. " aaaaaaaaaaaaaaaaaaaaaaa>\n"
  3894. " aaaaaaaaaaaaaaaaaa;",
  3895. NoBinPacking);
  3896. verifyFormat("a(\"a\"\n"
  3897. " \"a\",\n"
  3898. " a);");
  3899. NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
  3900. verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
  3901. " aaaaaaaaa,\n"
  3902. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3903. NoBinPacking);
  3904. verifyFormat(
  3905. "void f() {\n"
  3906. " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  3907. " .aaaaaaa();\n"
  3908. "}",
  3909. NoBinPacking);
  3910. verifyFormat(
  3911. "template <class SomeType, class SomeOtherType>\n"
  3912. "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
  3913. NoBinPacking);
  3914. }
  3915. TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
  3916. FormatStyle Style = getLLVMStyleWithColumns(15);
  3917. Style.ExperimentalAutoDetectBinPacking = true;
  3918. EXPECT_EQ("aaa(aaaa,\n"
  3919. " aaaa,\n"
  3920. " aaaa);\n"
  3921. "aaa(aaaa,\n"
  3922. " aaaa,\n"
  3923. " aaaa);",
  3924. format("aaa(aaaa,\n" // one-per-line
  3925. " aaaa,\n"
  3926. " aaaa );\n"
  3927. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  3928. Style));
  3929. EXPECT_EQ("aaa(aaaa, aaaa,\n"
  3930. " aaaa);\n"
  3931. "aaa(aaaa, aaaa,\n"
  3932. " aaaa);",
  3933. format("aaa(aaaa, aaaa,\n" // bin-packed
  3934. " aaaa );\n"
  3935. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  3936. Style));
  3937. }
  3938. TEST_F(FormatTest, FormatsBuilderPattern) {
  3939. verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
  3940. " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
  3941. " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
  3942. " .StartsWith(\".init\", ORDER_INIT)\n"
  3943. " .StartsWith(\".fini\", ORDER_FINI)\n"
  3944. " .StartsWith(\".hash\", ORDER_HASH)\n"
  3945. " .Default(ORDER_TEXT);\n");
  3946. verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
  3947. " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
  3948. verifyFormat(
  3949. "aaaaaaa->aaaaaaa\n"
  3950. " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3951. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3952. " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
  3953. verifyFormat(
  3954. "aaaaaaa->aaaaaaa\n"
  3955. " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3956. " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
  3957. verifyFormat(
  3958. "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
  3959. " aaaaaaaaaaaaaa);");
  3960. verifyFormat(
  3961. "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
  3962. " aaaaaa->aaaaaaaaaaaa()\n"
  3963. " ->aaaaaaaaaaaaaaaa(\n"
  3964. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3965. " ->aaaaaaaaaaaaaaaaa();");
  3966. verifyGoogleFormat(
  3967. "void f() {\n"
  3968. " someo->Add((new util::filetools::Handler(dir))\n"
  3969. " ->OnEvent1(NewPermanentCallback(\n"
  3970. " this, &HandlerHolderClass::EventHandlerCBA))\n"
  3971. " ->OnEvent2(NewPermanentCallback(\n"
  3972. " this, &HandlerHolderClass::EventHandlerCBB))\n"
  3973. " ->OnEvent3(NewPermanentCallback(\n"
  3974. " this, &HandlerHolderClass::EventHandlerCBC))\n"
  3975. " ->OnEvent5(NewPermanentCallback(\n"
  3976. " this, &HandlerHolderClass::EventHandlerCBD))\n"
  3977. " ->OnEvent6(NewPermanentCallback(\n"
  3978. " this, &HandlerHolderClass::EventHandlerCBE)));\n"
  3979. "}");
  3980. verifyFormat(
  3981. "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
  3982. verifyFormat("aaaaaaaaaaaaaaa()\n"
  3983. " .aaaaaaaaaaaaaaa()\n"
  3984. " .aaaaaaaaaaaaaaa()\n"
  3985. " .aaaaaaaaaaaaaaa()\n"
  3986. " .aaaaaaaaaaaaaaa();");
  3987. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3988. " .aaaaaaaaaaaaaaa()\n"
  3989. " .aaaaaaaaaaaaaaa()\n"
  3990. " .aaaaaaaaaaaaaaa();");
  3991. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3992. " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3993. " .aaaaaaaaaaaaaaa();");
  3994. verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3995. " ->aaaaaaaaaaaaaae(0)\n"
  3996. " ->aaaaaaaaaaaaaaa();");
  3997. // Don't linewrap after very short segments.
  3998. verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3999. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  4000. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4001. verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  4002. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  4003. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4004. verifyFormat("aaa()\n"
  4005. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  4006. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  4007. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4008. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  4009. " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  4010. " .has<bbbbbbbbbbbbbbbbbbbbb>();");
  4011. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  4012. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  4013. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
  4014. // Prefer not to break after empty parentheses.
  4015. verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
  4016. " First->LastNewlineOffset);");
  4017. // Prefer not to create "hanging" indents.
  4018. verifyFormat(
  4019. "return !soooooooooooooome_map\n"
  4020. " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4021. " .second;");
  4022. verifyFormat(
  4023. "return aaaaaaaaaaaaaaaa\n"
  4024. " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
  4025. " .aaaa(aaaaaaaaaaaaaa);");
  4026. // No hanging indent here.
  4027. verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
  4028. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4029. verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
  4030. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4031. verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
  4032. " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4033. getLLVMStyleWithColumns(60));
  4034. verifyFormat("aaaaaaaaaaaaaaaaaa\n"
  4035. " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
  4036. " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4037. getLLVMStyleWithColumns(59));
  4038. verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4039. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4040. " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4041. }
  4042. TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
  4043. verifyFormat(
  4044. "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  4045. " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
  4046. verifyFormat(
  4047. "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
  4048. " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
  4049. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  4050. " ccccccccccccccccccccccccc) {\n}");
  4051. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
  4052. " ccccccccccccccccccccccccc) {\n}");
  4053. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  4054. " ccccccccccccccccccccccccc) {\n}");
  4055. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
  4056. " ccccccccccccccccccccccccc) {\n}");
  4057. verifyFormat(
  4058. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
  4059. " ccccccccccccccccccccccccc) {\n}");
  4060. verifyFormat(
  4061. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
  4062. " ccccccccccccccccccccccccc) {\n}");
  4063. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
  4064. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
  4065. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
  4066. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  4067. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
  4068. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
  4069. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
  4070. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  4071. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
  4072. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
  4073. " aaaaaaaaaaaaaaa != aa) {\n}");
  4074. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
  4075. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
  4076. " aaaaaaaaaaaaaaa != aa) {\n}");
  4077. }
  4078. TEST_F(FormatTest, BreaksAfterAssignments) {
  4079. verifyFormat(
  4080. "unsigned Cost =\n"
  4081. " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
  4082. " SI->getPointerAddressSpaceee());\n");
  4083. verifyFormat(
  4084. "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
  4085. " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
  4086. verifyFormat(
  4087. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
  4088. " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
  4089. verifyFormat("unsigned OriginalStartColumn =\n"
  4090. " SourceMgr.getSpellingColumnNumber(\n"
  4091. " Current.FormatTok.getStartOfNonWhitespace()) -\n"
  4092. " 1;");
  4093. }
  4094. TEST_F(FormatTest, AlignsAfterAssignments) {
  4095. verifyFormat(
  4096. "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4097. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  4098. verifyFormat(
  4099. "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4100. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  4101. verifyFormat(
  4102. "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4103. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  4104. verifyFormat(
  4105. "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4106. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  4107. verifyFormat(
  4108. "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4109. " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4110. " aaaaaaaaaaaaaaaaaaaaaaaa;");
  4111. }
  4112. TEST_F(FormatTest, AlignsAfterReturn) {
  4113. verifyFormat(
  4114. "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4115. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  4116. verifyFormat(
  4117. "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4118. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  4119. verifyFormat(
  4120. "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  4121. " aaaaaaaaaaaaaaaaaaaaaa();");
  4122. verifyFormat(
  4123. "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  4124. " aaaaaaaaaaaaaaaaaaaaaa());");
  4125. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4126. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4127. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4128. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
  4129. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4130. verifyFormat("return\n"
  4131. " // true if code is one of a or b.\n"
  4132. " code == a || code == b;");
  4133. }
  4134. TEST_F(FormatTest, AlignsAfterOpenBracket) {
  4135. verifyFormat(
  4136. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
  4137. " aaaaaaaaa aaaaaaa) {}");
  4138. verifyFormat(
  4139. "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
  4140. " aaaaaaaaaaa aaaaaaaaa);");
  4141. verifyFormat(
  4142. "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
  4143. " aaaaaaaaaaaaaaaaaaaaa));");
  4144. FormatStyle Style = getLLVMStyle();
  4145. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  4146. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4147. " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
  4148. Style);
  4149. verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
  4150. " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
  4151. Style);
  4152. verifyFormat("SomeLongVariableName->someFunction(\n"
  4153. " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
  4154. Style);
  4155. verifyFormat(
  4156. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
  4157. " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  4158. Style);
  4159. verifyFormat(
  4160. "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
  4161. " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4162. Style);
  4163. verifyFormat(
  4164. "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
  4165. " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
  4166. Style);
  4167. verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
  4168. " ccccccc(aaaaaaaaaaaaaaaaa, //\n"
  4169. " b));",
  4170. Style);
  4171. Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
  4172. Style.BinPackArguments = false;
  4173. Style.BinPackParameters = false;
  4174. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4175. " aaaaaaaaaaa aaaaaaaa,\n"
  4176. " aaaaaaaaa aaaaaaa,\n"
  4177. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  4178. Style);
  4179. verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
  4180. " aaaaaaaaaaa aaaaaaaaa,\n"
  4181. " aaaaaaaaaaa aaaaaaaaa,\n"
  4182. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4183. Style);
  4184. verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
  4185. " aaaaaaaaaaaaaaa,\n"
  4186. " aaaaaaaaaaaaaaaaaaaaa,\n"
  4187. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
  4188. Style);
  4189. verifyFormat(
  4190. "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
  4191. " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
  4192. Style);
  4193. verifyFormat(
  4194. "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
  4195. " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
  4196. Style);
  4197. verifyFormat(
  4198. "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4199. " aaaaaaaaaaaaaaaaaaaaa(\n"
  4200. " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
  4201. " aaaaaaaaaaaaaaaa);",
  4202. Style);
  4203. verifyFormat(
  4204. "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4205. " aaaaaaaaaaaaaaaaaaaaa(\n"
  4206. " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
  4207. " aaaaaaaaaaaaaaaa);",
  4208. Style);
  4209. }
  4210. TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
  4211. FormatStyle Style = getLLVMStyleWithColumns(40);
  4212. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4213. " bbbbbbbbbbbbbbbbbbbbbb);",
  4214. Style);
  4215. Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
  4216. Style.AlignOperands = false;
  4217. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4218. " bbbbbbbbbbbbbbbbbbbbbb);",
  4219. Style);
  4220. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  4221. Style.AlignOperands = true;
  4222. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4223. " bbbbbbbbbbbbbbbbbbbbbb);",
  4224. Style);
  4225. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  4226. Style.AlignOperands = false;
  4227. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4228. " bbbbbbbbbbbbbbbbbbbbbb);",
  4229. Style);
  4230. }
  4231. TEST_F(FormatTest, BreaksConditionalExpressions) {
  4232. verifyFormat(
  4233. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4234. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4235. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4236. verifyFormat(
  4237. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4238. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4239. verifyFormat(
  4240. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
  4241. " : aaaaaaaaaaaaa);");
  4242. verifyFormat(
  4243. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4244. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4245. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4246. " aaaaaaaaaaaaa);");
  4247. verifyFormat(
  4248. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4249. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4250. " aaaaaaaaaaaaa);");
  4251. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4252. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4253. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4254. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4255. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4256. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4257. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4258. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4259. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4260. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4261. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4262. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4263. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4264. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4265. " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4266. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4267. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4268. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4269. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4270. " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4271. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  4272. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4273. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4274. " : aaaaaaaaaaaaaaaa;");
  4275. verifyFormat(
  4276. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4277. " ? aaaaaaaaaaaaaaa\n"
  4278. " : aaaaaaaaaaaaaaa;");
  4279. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  4280. " aaaaaaaaa\n"
  4281. " ? b\n"
  4282. " : c);");
  4283. verifyFormat("return aaaa == bbbb\n"
  4284. " // comment\n"
  4285. " ? aaaa\n"
  4286. " : bbbb;");
  4287. verifyFormat("unsigned Indent =\n"
  4288. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
  4289. " ? IndentForLevel[TheLine.Level]\n"
  4290. " : TheLine * 2,\n"
  4291. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  4292. getLLVMStyleWithColumns(70));
  4293. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  4294. " ? aaaaaaaaaaaaaaa\n"
  4295. " : bbbbbbbbbbbbbbb //\n"
  4296. " ? ccccccccccccccc\n"
  4297. " : ddddddddddddddd;");
  4298. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  4299. " ? aaaaaaaaaaaaaaa\n"
  4300. " : (bbbbbbbbbbbbbbb //\n"
  4301. " ? ccccccccccccccc\n"
  4302. " : ddddddddddddddd);");
  4303. verifyFormat(
  4304. "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4305. " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4306. " aaaaaaaaaaaaaaaaaaaaa +\n"
  4307. " aaaaaaaaaaaaaaaaaaaaa\n"
  4308. " : aaaaaaaaaa;");
  4309. verifyFormat(
  4310. "aaaaaa = aaaaaaaaaaaa\n"
  4311. " ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4312. " : aaaaaaaaaaaaaaaaaaaaaa\n"
  4313. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4314. FormatStyle NoBinPacking = getLLVMStyle();
  4315. NoBinPacking.BinPackArguments = false;
  4316. verifyFormat(
  4317. "void f() {\n"
  4318. " g(aaa,\n"
  4319. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  4320. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4321. " ? aaaaaaaaaaaaaaa\n"
  4322. " : aaaaaaaaaaaaaaa);\n"
  4323. "}",
  4324. NoBinPacking);
  4325. verifyFormat(
  4326. "void f() {\n"
  4327. " g(aaa,\n"
  4328. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  4329. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4330. " ?: aaaaaaaaaaaaaaa);\n"
  4331. "}",
  4332. NoBinPacking);
  4333. verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
  4334. " // comment.\n"
  4335. " ccccccccccccccccccccccccccccccccccccccc\n"
  4336. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4337. " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
  4338. // Assignments in conditional expressions. Apparently not uncommon :-(.
  4339. verifyFormat("return a != b\n"
  4340. " // comment\n"
  4341. " ? a = b\n"
  4342. " : a = b;");
  4343. verifyFormat("return a != b\n"
  4344. " // comment\n"
  4345. " ? a = a != b\n"
  4346. " // comment\n"
  4347. " ? a = b\n"
  4348. " : a\n"
  4349. " : a;\n");
  4350. verifyFormat("return a != b\n"
  4351. " // comment\n"
  4352. " ? a\n"
  4353. " : a = a != b\n"
  4354. " // comment\n"
  4355. " ? a = b\n"
  4356. " : a;");
  4357. }
  4358. TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
  4359. FormatStyle Style = getLLVMStyle();
  4360. Style.BreakBeforeTernaryOperators = false;
  4361. Style.ColumnLimit = 70;
  4362. verifyFormat(
  4363. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4364. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4365. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4366. Style);
  4367. verifyFormat(
  4368. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4369. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4370. Style);
  4371. verifyFormat(
  4372. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
  4373. " aaaaaaaaaaaaa);",
  4374. Style);
  4375. verifyFormat(
  4376. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4377. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4378. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4379. " aaaaaaaaaaaaa);",
  4380. Style);
  4381. verifyFormat(
  4382. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4383. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4384. " aaaaaaaaaaaaa);",
  4385. Style);
  4386. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4387. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4388. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  4389. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4390. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4391. Style);
  4392. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4393. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4394. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4395. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  4396. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4397. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4398. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4399. Style);
  4400. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4401. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
  4402. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4403. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4404. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4405. Style);
  4406. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4407. " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4408. " aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4409. Style);
  4410. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  4411. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4412. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4413. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4414. Style);
  4415. verifyFormat(
  4416. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4417. " aaaaaaaaaaaaaaa :\n"
  4418. " aaaaaaaaaaaaaaa;",
  4419. Style);
  4420. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  4421. " aaaaaaaaa ?\n"
  4422. " b :\n"
  4423. " c);",
  4424. Style);
  4425. verifyFormat(
  4426. "unsigned Indent =\n"
  4427. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n"
  4428. " IndentForLevel[TheLine.Level] :\n"
  4429. " TheLine * 2,\n"
  4430. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  4431. Style);
  4432. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  4433. " aaaaaaaaaaaaaaa :\n"
  4434. " bbbbbbbbbbbbbbb ? //\n"
  4435. " ccccccccccccccc :\n"
  4436. " ddddddddddddddd;",
  4437. Style);
  4438. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  4439. " aaaaaaaaaaaaaaa :\n"
  4440. " (bbbbbbbbbbbbbbb ? //\n"
  4441. " ccccccccccccccc :\n"
  4442. " ddddddddddddddd);",
  4443. Style);
  4444. verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4445. " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
  4446. " ccccccccccccccccccccccccccc;",
  4447. Style);
  4448. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4449. " aaaaa :\n"
  4450. " bbbbbbbbbbbbbbb + cccccccccccccccc;",
  4451. Style);
  4452. }
  4453. TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
  4454. verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
  4455. " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
  4456. verifyFormat("bool a = true, b = false;");
  4457. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4458. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
  4459. " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
  4460. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
  4461. verifyFormat(
  4462. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  4463. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
  4464. " d = e && f;");
  4465. verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
  4466. " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
  4467. verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  4468. " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
  4469. verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
  4470. " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
  4471. FormatStyle Style = getGoogleStyle();
  4472. Style.PointerAlignment = FormatStyle::PAS_Left;
  4473. Style.DerivePointerAlignment = false;
  4474. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4475. " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
  4476. " *b = bbbbbbbbbbbbbbbbbbb;",
  4477. Style);
  4478. verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  4479. " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
  4480. Style);
  4481. }
  4482. TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
  4483. verifyFormat("arr[foo ? bar : baz];");
  4484. verifyFormat("f()[foo ? bar : baz];");
  4485. verifyFormat("(a + b)[foo ? bar : baz];");
  4486. verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
  4487. }
  4488. TEST_F(FormatTest, AlignsStringLiterals) {
  4489. verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
  4490. " \"short literal\");");
  4491. verifyFormat(
  4492. "looooooooooooooooooooooooongFunction(\n"
  4493. " \"short literal\"\n"
  4494. " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
  4495. verifyFormat("someFunction(\"Always break between multi-line\"\n"
  4496. " \" string literals\",\n"
  4497. " and, other, parameters);");
  4498. EXPECT_EQ("fun + \"1243\" /* comment */\n"
  4499. " \"5678\";",
  4500. format("fun + \"1243\" /* comment */\n"
  4501. " \"5678\";",
  4502. getLLVMStyleWithColumns(28)));
  4503. EXPECT_EQ(
  4504. "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  4505. " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
  4506. " \"aaaaaaaaaaaaaaaa\";",
  4507. format("aaaaaa ="
  4508. "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  4509. "aaaaaaaaaaaaaaaaaaaaa\" "
  4510. "\"aaaaaaaaaaaaaaaa\";"));
  4511. verifyFormat("a = a + \"a\"\n"
  4512. " \"a\"\n"
  4513. " \"a\";");
  4514. verifyFormat("f(\"a\", \"b\"\n"
  4515. " \"c\");");
  4516. verifyFormat(
  4517. "#define LL_FORMAT \"ll\"\n"
  4518. "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
  4519. " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
  4520. verifyFormat("#define A(X) \\\n"
  4521. " \"aaaaa\" #X \"bbbbbb\" \\\n"
  4522. " \"ccccc\"",
  4523. getLLVMStyleWithColumns(23));
  4524. verifyFormat("#define A \"def\"\n"
  4525. "f(\"abc\" A \"ghi\"\n"
  4526. " \"jkl\");");
  4527. verifyFormat("f(L\"a\"\n"
  4528. " L\"b\");");
  4529. verifyFormat("#define A(X) \\\n"
  4530. " L\"aaaaa\" #X L\"bbbbbb\" \\\n"
  4531. " L\"ccccc\"",
  4532. getLLVMStyleWithColumns(25));
  4533. verifyFormat("f(@\"a\"\n"
  4534. " @\"b\");");
  4535. verifyFormat("NSString s = @\"a\"\n"
  4536. " @\"b\"\n"
  4537. " @\"c\";");
  4538. verifyFormat("NSString s = @\"a\"\n"
  4539. " \"b\"\n"
  4540. " \"c\";");
  4541. }
  4542. TEST_F(FormatTest, ReturnTypeBreakingStyle) {
  4543. FormatStyle Style = getLLVMStyle();
  4544. // No declarations or definitions should be moved to own line.
  4545. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
  4546. verifyFormat("class A {\n"
  4547. " int f() { return 1; }\n"
  4548. " int g();\n"
  4549. "};\n"
  4550. "int f() { return 1; }\n"
  4551. "int g();\n",
  4552. Style);
  4553. // All declarations and definitions should have the return type moved to its
  4554. // own
  4555. // line.
  4556. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
  4557. verifyFormat("class E {\n"
  4558. " int\n"
  4559. " f() {\n"
  4560. " return 1;\n"
  4561. " }\n"
  4562. " int\n"
  4563. " g();\n"
  4564. "};\n"
  4565. "int\n"
  4566. "f() {\n"
  4567. " return 1;\n"
  4568. "}\n"
  4569. "int\n"
  4570. "g();\n",
  4571. Style);
  4572. // Top-level definitions, and no kinds of declarations should have the
  4573. // return type moved to its own line.
  4574. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
  4575. verifyFormat("class B {\n"
  4576. " int f() { return 1; }\n"
  4577. " int g();\n"
  4578. "};\n"
  4579. "int\n"
  4580. "f() {\n"
  4581. " return 1;\n"
  4582. "}\n"
  4583. "int g();\n",
  4584. Style);
  4585. // Top-level definitions and declarations should have the return type moved
  4586. // to its own line.
  4587. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
  4588. verifyFormat("class C {\n"
  4589. " int f() { return 1; }\n"
  4590. " int g();\n"
  4591. "};\n"
  4592. "int\n"
  4593. "f() {\n"
  4594. " return 1;\n"
  4595. "}\n"
  4596. "int\n"
  4597. "g();\n",
  4598. Style);
  4599. // All definitions should have the return type moved to its own line, but no
  4600. // kinds of declarations.
  4601. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
  4602. verifyFormat("class D {\n"
  4603. " int\n"
  4604. " f() {\n"
  4605. " return 1;\n"
  4606. " }\n"
  4607. " int g();\n"
  4608. "};\n"
  4609. "int\n"
  4610. "f() {\n"
  4611. " return 1;\n"
  4612. "}\n"
  4613. "int g();\n",
  4614. Style);
  4615. verifyFormat("const char *\n"
  4616. "f(void) {\n" // Break here.
  4617. " return \"\";\n"
  4618. "}\n"
  4619. "const char *bar(void);\n", // No break here.
  4620. Style);
  4621. verifyFormat("template <class T>\n"
  4622. "T *\n"
  4623. "f(T &c) {\n" // Break here.
  4624. " return NULL;\n"
  4625. "}\n"
  4626. "template <class T> T *f(T &c);\n", // No break here.
  4627. Style);
  4628. verifyFormat("class C {\n"
  4629. " int\n"
  4630. " operator+() {\n"
  4631. " return 1;\n"
  4632. " }\n"
  4633. " int\n"
  4634. " operator()() {\n"
  4635. " return 1;\n"
  4636. " }\n"
  4637. "};\n",
  4638. Style);
  4639. verifyFormat("void\n"
  4640. "A::operator()() {}\n"
  4641. "void\n"
  4642. "A::operator>>() {}\n"
  4643. "void\n"
  4644. "A::operator+() {}\n",
  4645. Style);
  4646. verifyFormat("void *operator new(std::size_t s);", // No break here.
  4647. Style);
  4648. verifyFormat("void *\n"
  4649. "operator new(std::size_t s) {}",
  4650. Style);
  4651. verifyFormat("void *\n"
  4652. "operator delete[](void *ptr) {}",
  4653. Style);
  4654. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  4655. verifyFormat("const char *\n"
  4656. "f(void)\n" // Break here.
  4657. "{\n"
  4658. " return \"\";\n"
  4659. "}\n"
  4660. "const char *bar(void);\n", // No break here.
  4661. Style);
  4662. verifyFormat("template <class T>\n"
  4663. "T *\n" // Problem here: no line break
  4664. "f(T &c)\n" // Break here.
  4665. "{\n"
  4666. " return NULL;\n"
  4667. "}\n"
  4668. "template <class T> T *f(T &c);\n", // No break here.
  4669. Style);
  4670. }
  4671. TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
  4672. FormatStyle NoBreak = getLLVMStyle();
  4673. NoBreak.AlwaysBreakBeforeMultilineStrings = false;
  4674. FormatStyle Break = getLLVMStyle();
  4675. Break.AlwaysBreakBeforeMultilineStrings = true;
  4676. verifyFormat("aaaa = \"bbbb\"\n"
  4677. " \"cccc\";",
  4678. NoBreak);
  4679. verifyFormat("aaaa =\n"
  4680. " \"bbbb\"\n"
  4681. " \"cccc\";",
  4682. Break);
  4683. verifyFormat("aaaa(\"bbbb\"\n"
  4684. " \"cccc\");",
  4685. NoBreak);
  4686. verifyFormat("aaaa(\n"
  4687. " \"bbbb\"\n"
  4688. " \"cccc\");",
  4689. Break);
  4690. verifyFormat("aaaa(qqq, \"bbbb\"\n"
  4691. " \"cccc\");",
  4692. NoBreak);
  4693. verifyFormat("aaaa(qqq,\n"
  4694. " \"bbbb\"\n"
  4695. " \"cccc\");",
  4696. Break);
  4697. verifyFormat("aaaa(qqq,\n"
  4698. " L\"bbbb\"\n"
  4699. " L\"cccc\");",
  4700. Break);
  4701. verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
  4702. " \"bbbb\"));",
  4703. Break);
  4704. verifyFormat("string s = someFunction(\n"
  4705. " \"abc\"\n"
  4706. " \"abc\");",
  4707. Break);
  4708. // As we break before unary operators, breaking right after them is bad.
  4709. verifyFormat("string foo = abc ? \"x\"\n"
  4710. " \"blah blah blah blah blah blah\"\n"
  4711. " : \"y\";",
  4712. Break);
  4713. // Don't break if there is no column gain.
  4714. verifyFormat("f(\"aaaa\"\n"
  4715. " \"bbbb\");",
  4716. Break);
  4717. // Treat literals with escaped newlines like multi-line string literals.
  4718. EXPECT_EQ("x = \"a\\\n"
  4719. "b\\\n"
  4720. "c\";",
  4721. format("x = \"a\\\n"
  4722. "b\\\n"
  4723. "c\";",
  4724. NoBreak));
  4725. EXPECT_EQ("xxxx =\n"
  4726. " \"a\\\n"
  4727. "b\\\n"
  4728. "c\";",
  4729. format("xxxx = \"a\\\n"
  4730. "b\\\n"
  4731. "c\";",
  4732. Break));
  4733. // Exempt ObjC strings for now.
  4734. EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
  4735. " @\"bbbb\";",
  4736. format("NSString *const kString = @\"aaaa\"\n"
  4737. "@\"bbbb\";",
  4738. Break));
  4739. Break.ColumnLimit = 0;
  4740. verifyFormat("const char *hello = \"hello llvm\";", Break);
  4741. }
  4742. TEST_F(FormatTest, AlignsPipes) {
  4743. verifyFormat(
  4744. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4745. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4746. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4747. verifyFormat(
  4748. "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
  4749. " << aaaaaaaaaaaaaaaaaaaa;");
  4750. verifyFormat(
  4751. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4752. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4753. verifyFormat(
  4754. "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
  4755. " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
  4756. " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
  4757. verifyFormat(
  4758. "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4759. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4760. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4761. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4762. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4763. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4764. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4765. verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
  4766. " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
  4767. verifyFormat(
  4768. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4769. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4770. verifyFormat("return out << \"somepacket = {\\n\"\n"
  4771. " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
  4772. " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
  4773. " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
  4774. " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
  4775. " << \"}\";");
  4776. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  4777. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  4778. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
  4779. verifyFormat(
  4780. "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
  4781. " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
  4782. " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
  4783. " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
  4784. " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
  4785. verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
  4786. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4787. verifyFormat(
  4788. "void f() {\n"
  4789. " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
  4790. " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  4791. "}");
  4792. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
  4793. " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
  4794. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4795. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4796. " aaaaaaaaaaaaaaaaaaaaa)\n"
  4797. " << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4798. verifyFormat("LOG_IF(aaa == //\n"
  4799. " bbb)\n"
  4800. " << a << b;");
  4801. // Breaking before the first "<<" is generally not desirable.
  4802. verifyFormat(
  4803. "llvm::errs()\n"
  4804. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4805. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4806. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4807. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4808. getLLVMStyleWithColumns(70));
  4809. verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4810. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4811. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4812. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4813. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4814. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4815. getLLVMStyleWithColumns(70));
  4816. // But sometimes, breaking before the first "<<" is desirable.
  4817. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
  4818. " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
  4819. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
  4820. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4821. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4822. verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
  4823. " << BEF << IsTemplate << Description << E->getType();");
  4824. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
  4825. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4826. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4827. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
  4828. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4829. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4830. " << aaa;");
  4831. verifyFormat(
  4832. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4833. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4834. // Incomplete string literal.
  4835. EXPECT_EQ("llvm::errs() << \"\n"
  4836. " << a;",
  4837. format("llvm::errs() << \"\n<<a;"));
  4838. verifyFormat("void f() {\n"
  4839. " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
  4840. " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
  4841. "}");
  4842. // Handle 'endl'.
  4843. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
  4844. " << bbbbbbbbbbbbbbbbbbbbbb << endl;");
  4845. verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
  4846. // Handle '\n'.
  4847. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
  4848. " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
  4849. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
  4850. " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
  4851. verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
  4852. " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
  4853. verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
  4854. }
  4855. TEST_F(FormatTest, UnderstandsEquals) {
  4856. verifyFormat(
  4857. "aaaaaaaaaaaaaaaaa =\n"
  4858. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4859. verifyFormat(
  4860. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4861. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  4862. verifyFormat(
  4863. "if (a) {\n"
  4864. " f();\n"
  4865. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4866. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  4867. "}");
  4868. verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4869. " 100000000 + 10000000) {\n}");
  4870. }
  4871. TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
  4872. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  4873. " .looooooooooooooooooooooooooooooooooooooongFunction();");
  4874. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  4875. " ->looooooooooooooooooooooooooooooooooooooongFunction();");
  4876. verifyFormat(
  4877. "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
  4878. " Parameter2);");
  4879. verifyFormat(
  4880. "ShortObject->shortFunction(\n"
  4881. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
  4882. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
  4883. verifyFormat("loooooooooooooongFunction(\n"
  4884. " LoooooooooooooongObject->looooooooooooooooongFunction());");
  4885. verifyFormat(
  4886. "function(LoooooooooooooooooooooooooooooooooooongObject\n"
  4887. " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
  4888. verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  4889. " .WillRepeatedly(Return(SomeValue));");
  4890. verifyFormat("void f() {\n"
  4891. " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  4892. " .Times(2)\n"
  4893. " .WillRepeatedly(Return(SomeValue));\n"
  4894. "}");
  4895. verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
  4896. " ccccccccccccccccccccccc);");
  4897. verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4898. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4899. " .aaaaa(aaaaa),\n"
  4900. " aaaaaaaaaaaaaaaaaaaaa);");
  4901. verifyFormat("void f() {\n"
  4902. " aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4903. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
  4904. "}");
  4905. verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4906. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4907. " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4908. " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4909. " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  4910. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4911. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4912. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4913. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
  4914. "}");
  4915. // Here, it is not necessary to wrap at "." or "->".
  4916. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
  4917. " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  4918. verifyFormat(
  4919. "aaaaaaaaaaa->aaaaaaaaa(\n"
  4920. " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4921. " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
  4922. verifyFormat(
  4923. "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4924. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
  4925. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
  4926. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  4927. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
  4928. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  4929. verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4930. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4931. " .a();");
  4932. FormatStyle NoBinPacking = getLLVMStyle();
  4933. NoBinPacking.BinPackParameters = false;
  4934. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  4935. " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  4936. " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
  4937. " aaaaaaaaaaaaaaaaaaa,\n"
  4938. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4939. NoBinPacking);
  4940. // If there is a subsequent call, change to hanging indentation.
  4941. verifyFormat(
  4942. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4943. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
  4944. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4945. verifyFormat(
  4946. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4947. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
  4948. verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4949. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4950. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4951. verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4952. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4953. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  4954. }
  4955. TEST_F(FormatTest, WrapsTemplateDeclarations) {
  4956. verifyFormat("template <typename T>\n"
  4957. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  4958. verifyFormat("template <typename T>\n"
  4959. "// T should be one of {A, B}.\n"
  4960. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  4961. verifyFormat(
  4962. "template <typename T>\n"
  4963. "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
  4964. verifyFormat("template <typename T>\n"
  4965. "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
  4966. " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
  4967. verifyFormat(
  4968. "template <typename T>\n"
  4969. "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
  4970. " int Paaaaaaaaaaaaaaaaaaaaram2);");
  4971. verifyFormat(
  4972. "template <typename T>\n"
  4973. "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
  4974. " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
  4975. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4976. verifyFormat("template <typename T>\n"
  4977. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4978. " int aaaaaaaaaaaaaaaaaaaaaa);");
  4979. verifyFormat(
  4980. "template <typename T1, typename T2 = char, typename T3 = char,\n"
  4981. " typename T4 = char>\n"
  4982. "void f();");
  4983. verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
  4984. " template <typename> class cccccccccccccccccccccc,\n"
  4985. " typename ddddddddddddd>\n"
  4986. "class C {};");
  4987. verifyFormat(
  4988. "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
  4989. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4990. verifyFormat("void f() {\n"
  4991. " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
  4992. " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
  4993. "}");
  4994. verifyFormat("template <typename T> class C {};");
  4995. verifyFormat("template <typename T> void f();");
  4996. verifyFormat("template <typename T> void f() {}");
  4997. verifyFormat(
  4998. "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  4999. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5000. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
  5001. " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  5002. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5003. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
  5004. " bbbbbbbbbbbbbbbbbbbbbbbb);",
  5005. getLLVMStyleWithColumns(72));
  5006. EXPECT_EQ("static_cast<A< //\n"
  5007. " B> *>(\n"
  5008. "\n"
  5009. " );",
  5010. format("static_cast<A<//\n"
  5011. " B>*>(\n"
  5012. "\n"
  5013. " );"));
  5014. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5015. " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
  5016. FormatStyle AlwaysBreak = getLLVMStyle();
  5017. AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
  5018. verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
  5019. verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
  5020. verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
  5021. verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5022. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
  5023. " ccccccccccccccccccccccccccccccccccccccccccccccc);");
  5024. verifyFormat("template <template <typename> class Fooooooo,\n"
  5025. " template <typename> class Baaaaaaar>\n"
  5026. "struct C {};",
  5027. AlwaysBreak);
  5028. verifyFormat("template <typename T> // T can be A, B or C.\n"
  5029. "struct C {};",
  5030. AlwaysBreak);
  5031. verifyFormat("template <enum E> class A {\n"
  5032. "public:\n"
  5033. " E *f();\n"
  5034. "};");
  5035. }
  5036. TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
  5037. verifyFormat(
  5038. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  5039. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  5040. verifyFormat(
  5041. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  5042. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5043. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  5044. // FIXME: Should we have the extra indent after the second break?
  5045. verifyFormat(
  5046. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  5047. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  5048. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  5049. verifyFormat(
  5050. "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
  5051. " cccccccccccccccccccccccccccccccccccccccccccccc());");
  5052. // Breaking at nested name specifiers is generally not desirable.
  5053. verifyFormat(
  5054. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5055. " aaaaaaaaaaaaaaaaaaaaaaa);");
  5056. verifyFormat(
  5057. "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  5058. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5059. " aaaaaaaaaaaaaaaaaaaaa);",
  5060. getLLVMStyleWithColumns(74));
  5061. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  5062. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5063. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  5064. }
  5065. TEST_F(FormatTest, UnderstandsTemplateParameters) {
  5066. verifyFormat("A<int> a;");
  5067. verifyFormat("A<A<A<int>>> a;");
  5068. verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
  5069. verifyFormat("bool x = a < 1 || 2 > a;");
  5070. verifyFormat("bool x = 5 < f<int>();");
  5071. verifyFormat("bool x = f<int>() > 5;");
  5072. verifyFormat("bool x = 5 < a<int>::x;");
  5073. verifyFormat("bool x = a < 4 ? a > 2 : false;");
  5074. verifyFormat("bool x = f() ? a < 2 : a > 2;");
  5075. verifyGoogleFormat("A<A<int>> a;");
  5076. verifyGoogleFormat("A<A<A<int>>> a;");
  5077. verifyGoogleFormat("A<A<A<A<int>>>> a;");
  5078. verifyGoogleFormat("A<A<int> > a;");
  5079. verifyGoogleFormat("A<A<A<int> > > a;");
  5080. verifyGoogleFormat("A<A<A<A<int> > > > a;");
  5081. verifyGoogleFormat("A<::A<int>> a;");
  5082. verifyGoogleFormat("A<::A> a;");
  5083. verifyGoogleFormat("A< ::A> a;");
  5084. verifyGoogleFormat("A< ::A<int> > a;");
  5085. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
  5086. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
  5087. EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
  5088. EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
  5089. EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
  5090. format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
  5091. verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
  5092. verifyFormat("test >> a >> b;");
  5093. verifyFormat("test << a >> b;");
  5094. verifyFormat("f<int>();");
  5095. verifyFormat("template <typename T> void f() {}");
  5096. verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
  5097. verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
  5098. "sizeof(char)>::type>;");
  5099. verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
  5100. verifyFormat("f(a.operator()<A>());");
  5101. verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5102. " .template operator()<A>());",
  5103. getLLVMStyleWithColumns(35));
  5104. // Not template parameters.
  5105. verifyFormat("return a < b && c > d;");
  5106. verifyFormat("void f() {\n"
  5107. " while (a < b && c > d) {\n"
  5108. " }\n"
  5109. "}");
  5110. verifyFormat("template <typename... Types>\n"
  5111. "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
  5112. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5113. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
  5114. getLLVMStyleWithColumns(60));
  5115. verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
  5116. verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
  5117. verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
  5118. }
  5119. TEST_F(FormatTest, UnderstandsBinaryOperators) {
  5120. verifyFormat("COMPARE(a, ==, b);");
  5121. verifyFormat("auto s = sizeof...(Ts) - 1;");
  5122. }
  5123. TEST_F(FormatTest, UnderstandsPointersToMembers) {
  5124. verifyFormat("int A::*x;");
  5125. verifyFormat("int (S::*func)(void *);");
  5126. verifyFormat("void f() { int (S::*func)(void *); }");
  5127. verifyFormat("typedef bool *(Class::*Member)() const;");
  5128. verifyFormat("void f() {\n"
  5129. " (a->*f)();\n"
  5130. " a->*x;\n"
  5131. " (a.*f)();\n"
  5132. " ((*a).*f)();\n"
  5133. " a.*x;\n"
  5134. "}");
  5135. verifyFormat("void f() {\n"
  5136. " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  5137. " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
  5138. "}");
  5139. verifyFormat(
  5140. "(aaaaaaaaaa->*bbbbbbb)(\n"
  5141. " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  5142. FormatStyle Style = getLLVMStyle();
  5143. Style.PointerAlignment = FormatStyle::PAS_Left;
  5144. verifyFormat("typedef bool* (Class::*Member)() const;", Style);
  5145. }
  5146. TEST_F(FormatTest, UnderstandsUnaryOperators) {
  5147. verifyFormat("int a = -2;");
  5148. verifyFormat("f(-1, -2, -3);");
  5149. verifyFormat("a[-1] = 5;");
  5150. verifyFormat("int a = 5 + -2;");
  5151. verifyFormat("if (i == -1) {\n}");
  5152. verifyFormat("if (i != -1) {\n}");
  5153. verifyFormat("if (i > -1) {\n}");
  5154. verifyFormat("if (i < -1) {\n}");
  5155. verifyFormat("++(a->f());");
  5156. verifyFormat("--(a->f());");
  5157. verifyFormat("(a->f())++;");
  5158. verifyFormat("a[42]++;");
  5159. verifyFormat("if (!(a->f())) {\n}");
  5160. verifyFormat("a-- > b;");
  5161. verifyFormat("b ? -a : c;");
  5162. verifyFormat("n * sizeof char16;");
  5163. verifyFormat("n * alignof char16;", getGoogleStyle());
  5164. verifyFormat("sizeof(char);");
  5165. verifyFormat("alignof(char);", getGoogleStyle());
  5166. verifyFormat("return -1;");
  5167. verifyFormat("switch (a) {\n"
  5168. "case -1:\n"
  5169. " break;\n"
  5170. "}");
  5171. verifyFormat("#define X -1");
  5172. verifyFormat("#define X -kConstant");
  5173. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
  5174. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
  5175. verifyFormat("int a = /* confusing comment */ -1;");
  5176. // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
  5177. verifyFormat("int a = i /* confusing comment */++;");
  5178. }
  5179. TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
  5180. verifyFormat("if (!aaaaaaaaaa( // break\n"
  5181. " aaaaa)) {\n"
  5182. "}");
  5183. verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
  5184. " aaaaa));");
  5185. verifyFormat("*aaa = aaaaaaa( // break\n"
  5186. " bbbbbb);");
  5187. }
  5188. TEST_F(FormatTest, UnderstandsOverloadedOperators) {
  5189. verifyFormat("bool operator<();");
  5190. verifyFormat("bool operator>();");
  5191. verifyFormat("bool operator=();");
  5192. verifyFormat("bool operator==();");
  5193. verifyFormat("bool operator!=();");
  5194. verifyFormat("int operator+();");
  5195. verifyFormat("int operator++();");
  5196. verifyFormat("bool operator,();");
  5197. verifyFormat("bool operator();");
  5198. verifyFormat("bool operator()();");
  5199. verifyFormat("bool operator[]();");
  5200. verifyFormat("operator bool();");
  5201. verifyFormat("operator int();");
  5202. verifyFormat("operator void *();");
  5203. verifyFormat("operator SomeType<int>();");
  5204. verifyFormat("operator SomeType<int, int>();");
  5205. verifyFormat("operator SomeType<SomeType<int>>();");
  5206. verifyFormat("void *operator new(std::size_t size);");
  5207. verifyFormat("void *operator new[](std::size_t size);");
  5208. verifyFormat("void operator delete(void *ptr);");
  5209. verifyFormat("void operator delete[](void *ptr);");
  5210. verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
  5211. "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
  5212. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
  5213. " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
  5214. verifyFormat(
  5215. "ostream &operator<<(ostream &OutputStream,\n"
  5216. " SomeReallyLongType WithSomeReallyLongValue);");
  5217. verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
  5218. " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
  5219. " return left.group < right.group;\n"
  5220. "}");
  5221. verifyFormat("SomeType &operator=(const SomeType &S);");
  5222. verifyFormat("f.template operator()<int>();");
  5223. verifyGoogleFormat("operator void*();");
  5224. verifyGoogleFormat("operator SomeType<SomeType<int>>();");
  5225. verifyGoogleFormat("operator ::A();");
  5226. verifyFormat("using A::operator+;");
  5227. verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
  5228. "int i;");
  5229. }
  5230. TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
  5231. verifyFormat("Deleted &operator=(const Deleted &) & = default;");
  5232. verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
  5233. verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
  5234. verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
  5235. verifyFormat("Deleted &operator=(const Deleted &) &;");
  5236. verifyFormat("Deleted &operator=(const Deleted &) &&;");
  5237. verifyFormat("SomeType MemberFunction(const Deleted &) &;");
  5238. verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
  5239. verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
  5240. verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
  5241. verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
  5242. verifyFormat("SomeType MemberFunction(const Deleted &) const &;");
  5243. FormatStyle AlignLeft = getLLVMStyle();
  5244. AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
  5245. verifyFormat("void A::b() && {}", AlignLeft);
  5246. verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
  5247. verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
  5248. AlignLeft);
  5249. verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
  5250. verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
  5251. verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
  5252. verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
  5253. verifyFormat("auto Function(T) & -> void {}", AlignLeft);
  5254. verifyFormat("auto Function(T) & -> void;", AlignLeft);
  5255. verifyFormat("SomeType MemberFunction(const Deleted&) const &;", AlignLeft);
  5256. FormatStyle Spaces = getLLVMStyle();
  5257. Spaces.SpacesInCStyleCastParentheses = true;
  5258. verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
  5259. verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
  5260. verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
  5261. verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
  5262. Spaces.SpacesInCStyleCastParentheses = false;
  5263. Spaces.SpacesInParentheses = true;
  5264. verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
  5265. verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
  5266. verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
  5267. verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
  5268. }
  5269. TEST_F(FormatTest, UnderstandsNewAndDelete) {
  5270. verifyFormat("void f() {\n"
  5271. " A *a = new A;\n"
  5272. " A *a = new (placement) A;\n"
  5273. " delete a;\n"
  5274. " delete (A *)a;\n"
  5275. "}");
  5276. verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
  5277. " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
  5278. verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  5279. " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
  5280. " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
  5281. verifyFormat("delete[] h->p;");
  5282. }
  5283. TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
  5284. verifyFormat("int *f(int *a) {}");
  5285. verifyFormat("int main(int argc, char **argv) {}");
  5286. verifyFormat("Test::Test(int b) : a(b * b) {}");
  5287. verifyIndependentOfContext("f(a, *a);");
  5288. verifyFormat("void g() { f(*a); }");
  5289. verifyIndependentOfContext("int a = b * 10;");
  5290. verifyIndependentOfContext("int a = 10 * b;");
  5291. verifyIndependentOfContext("int a = b * c;");
  5292. verifyIndependentOfContext("int a += b * c;");
  5293. verifyIndependentOfContext("int a -= b * c;");
  5294. verifyIndependentOfContext("int a *= b * c;");
  5295. verifyIndependentOfContext("int a /= b * c;");
  5296. verifyIndependentOfContext("int a = *b;");
  5297. verifyIndependentOfContext("int a = *b * c;");
  5298. verifyIndependentOfContext("int a = b * *c;");
  5299. verifyIndependentOfContext("int a = b * (10);");
  5300. verifyIndependentOfContext("S << b * (10);");
  5301. verifyIndependentOfContext("return 10 * b;");
  5302. verifyIndependentOfContext("return *b * *c;");
  5303. verifyIndependentOfContext("return a & ~b;");
  5304. verifyIndependentOfContext("f(b ? *c : *d);");
  5305. verifyIndependentOfContext("int a = b ? *c : *d;");
  5306. verifyIndependentOfContext("*b = a;");
  5307. verifyIndependentOfContext("a * ~b;");
  5308. verifyIndependentOfContext("a * !b;");
  5309. verifyIndependentOfContext("a * +b;");
  5310. verifyIndependentOfContext("a * -b;");
  5311. verifyIndependentOfContext("a * ++b;");
  5312. verifyIndependentOfContext("a * --b;");
  5313. verifyIndependentOfContext("a[4] * b;");
  5314. verifyIndependentOfContext("a[a * a] = 1;");
  5315. verifyIndependentOfContext("f() * b;");
  5316. verifyIndependentOfContext("a * [self dostuff];");
  5317. verifyIndependentOfContext("int x = a * (a + b);");
  5318. verifyIndependentOfContext("(a *)(a + b);");
  5319. verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
  5320. verifyIndependentOfContext("int *pa = (int *)&a;");
  5321. verifyIndependentOfContext("return sizeof(int **);");
  5322. verifyIndependentOfContext("return sizeof(int ******);");
  5323. verifyIndependentOfContext("return (int **&)a;");
  5324. verifyIndependentOfContext("f((*PointerToArray)[10]);");
  5325. verifyFormat("void f(Type (*parameter)[10]) {}");
  5326. verifyFormat("void f(Type (&parameter)[10]) {}");
  5327. verifyGoogleFormat("return sizeof(int**);");
  5328. verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
  5329. verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
  5330. verifyFormat("auto a = [](int **&, int ***) {};");
  5331. verifyFormat("auto PointerBinding = [](const char *S) {};");
  5332. verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
  5333. verifyFormat("[](const decltype(*a) &value) {}");
  5334. verifyFormat("decltype(a * b) F();");
  5335. verifyFormat("#define MACRO() [](A *a) { return 1; }");
  5336. verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
  5337. verifyIndependentOfContext("typedef void (*f)(int *a);");
  5338. verifyIndependentOfContext("int i{a * b};");
  5339. verifyIndependentOfContext("aaa && aaa->f();");
  5340. verifyIndependentOfContext("int x = ~*p;");
  5341. verifyFormat("Constructor() : a(a), area(width * height) {}");
  5342. verifyFormat("Constructor() : a(a), area(a, width * height) {}");
  5343. verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
  5344. verifyFormat("void f() { f(a, c * d); }");
  5345. verifyFormat("void f() { f(new a(), c * d); }");
  5346. verifyIndependentOfContext("InvalidRegions[*R] = 0;");
  5347. verifyIndependentOfContext("A<int *> a;");
  5348. verifyIndependentOfContext("A<int **> a;");
  5349. verifyIndependentOfContext("A<int *, int *> a;");
  5350. verifyIndependentOfContext("A<int *[]> a;");
  5351. verifyIndependentOfContext(
  5352. "const char *const p = reinterpret_cast<const char *const>(q);");
  5353. verifyIndependentOfContext("A<int **, int **> a;");
  5354. verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
  5355. verifyFormat("for (char **a = b; *a; ++a) {\n}");
  5356. verifyFormat("for (; a && b;) {\n}");
  5357. verifyFormat("bool foo = true && [] { return false; }();");
  5358. verifyFormat(
  5359. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5360. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5361. verifyGoogleFormat("int const* a = &b;");
  5362. verifyGoogleFormat("**outparam = 1;");
  5363. verifyGoogleFormat("*outparam = a * b;");
  5364. verifyGoogleFormat("int main(int argc, char** argv) {}");
  5365. verifyGoogleFormat("A<int*> a;");
  5366. verifyGoogleFormat("A<int**> a;");
  5367. verifyGoogleFormat("A<int*, int*> a;");
  5368. verifyGoogleFormat("A<int**, int**> a;");
  5369. verifyGoogleFormat("f(b ? *c : *d);");
  5370. verifyGoogleFormat("int a = b ? *c : *d;");
  5371. verifyGoogleFormat("Type* t = **x;");
  5372. verifyGoogleFormat("Type* t = *++*x;");
  5373. verifyGoogleFormat("*++*x;");
  5374. verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
  5375. verifyGoogleFormat("Type* t = x++ * y;");
  5376. verifyGoogleFormat(
  5377. "const char* const p = reinterpret_cast<const char* const>(q);");
  5378. verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
  5379. verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
  5380. verifyGoogleFormat("template <typename T>\n"
  5381. "void f(int i = 0, SomeType** temps = NULL);");
  5382. FormatStyle Left = getLLVMStyle();
  5383. Left.PointerAlignment = FormatStyle::PAS_Left;
  5384. verifyFormat("x = *a(x) = *a(y);", Left);
  5385. verifyFormat("for (;; * = b) {\n}", Left);
  5386. verifyFormat("return *this += 1;", Left);
  5387. verifyIndependentOfContext("a = *(x + y);");
  5388. verifyIndependentOfContext("a = &(x + y);");
  5389. verifyIndependentOfContext("*(x + y).call();");
  5390. verifyIndependentOfContext("&(x + y)->call();");
  5391. verifyFormat("void f() { &(*I).first; }");
  5392. verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
  5393. verifyFormat(
  5394. "int *MyValues = {\n"
  5395. " *A, // Operator detection might be confused by the '{'\n"
  5396. " *BB // Operator detection might be confused by previous comment\n"
  5397. "};");
  5398. verifyIndependentOfContext("if (int *a = &b)");
  5399. verifyIndependentOfContext("if (int &a = *b)");
  5400. verifyIndependentOfContext("if (a & b[i])");
  5401. verifyIndependentOfContext("if (a::b::c::d & b[i])");
  5402. verifyIndependentOfContext("if (*b[i])");
  5403. verifyIndependentOfContext("if (int *a = (&b))");
  5404. verifyIndependentOfContext("while (int *a = &b)");
  5405. verifyIndependentOfContext("size = sizeof *a;");
  5406. verifyIndependentOfContext("if (a && (b = c))");
  5407. verifyFormat("void f() {\n"
  5408. " for (const int &v : Values) {\n"
  5409. " }\n"
  5410. "}");
  5411. verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
  5412. verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
  5413. verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
  5414. verifyFormat("#define A (!a * b)");
  5415. verifyFormat("#define MACRO \\\n"
  5416. " int *i = a * b; \\\n"
  5417. " void f(a *b);",
  5418. getLLVMStyleWithColumns(19));
  5419. verifyIndependentOfContext("A = new SomeType *[Length];");
  5420. verifyIndependentOfContext("A = new SomeType *[Length]();");
  5421. verifyIndependentOfContext("T **t = new T *;");
  5422. verifyIndependentOfContext("T **t = new T *();");
  5423. verifyGoogleFormat("A = new SomeType*[Length]();");
  5424. verifyGoogleFormat("A = new SomeType*[Length];");
  5425. verifyGoogleFormat("T** t = new T*;");
  5426. verifyGoogleFormat("T** t = new T*();");
  5427. FormatStyle PointerLeft = getLLVMStyle();
  5428. PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
  5429. verifyFormat("delete *x;", PointerLeft);
  5430. verifyFormat("STATIC_ASSERT((a & b) == 0);");
  5431. verifyFormat("STATIC_ASSERT(0 == (a & b));");
  5432. verifyFormat("template <bool a, bool b> "
  5433. "typename t::if<x && y>::type f() {}");
  5434. verifyFormat("template <int *y> f() {}");
  5435. verifyFormat("vector<int *> v;");
  5436. verifyFormat("vector<int *const> v;");
  5437. verifyFormat("vector<int *const **const *> v;");
  5438. verifyFormat("vector<int *volatile> v;");
  5439. verifyFormat("vector<a * b> v;");
  5440. verifyFormat("foo<b && false>();");
  5441. verifyFormat("foo<b & 1>();");
  5442. verifyFormat("decltype(*::std::declval<const T &>()) void F();");
  5443. verifyFormat(
  5444. "template <class T, class = typename std::enable_if<\n"
  5445. " std::is_integral<T>::value &&\n"
  5446. " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
  5447. "void F();",
  5448. getLLVMStyleWithColumns(76));
  5449. verifyFormat(
  5450. "template <class T,\n"
  5451. " class = typename ::std::enable_if<\n"
  5452. " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
  5453. "void F();",
  5454. getGoogleStyleWithColumns(68));
  5455. verifyIndependentOfContext("MACRO(int *i);");
  5456. verifyIndependentOfContext("MACRO(auto *a);");
  5457. verifyIndependentOfContext("MACRO(const A *a);");
  5458. verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
  5459. // FIXME: Is there a way to make this work?
  5460. // verifyIndependentOfContext("MACRO(A *a);");
  5461. verifyFormat("DatumHandle const *operator->() const { return input_; }");
  5462. verifyFormat("return options != nullptr && operator==(*options);");
  5463. EXPECT_EQ("#define OP(x) \\\n"
  5464. " ostream &operator<<(ostream &s, const A &a) { \\\n"
  5465. " return s << a.DebugString(); \\\n"
  5466. " }",
  5467. format("#define OP(x) \\\n"
  5468. " ostream &operator<<(ostream &s, const A &a) { \\\n"
  5469. " return s << a.DebugString(); \\\n"
  5470. " }",
  5471. getLLVMStyleWithColumns(50)));
  5472. // FIXME: We cannot handle this case yet; we might be able to figure out that
  5473. // foo<x> d > v; doesn't make sense.
  5474. verifyFormat("foo<a<b && c> d> v;");
  5475. FormatStyle PointerMiddle = getLLVMStyle();
  5476. PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
  5477. verifyFormat("delete *x;", PointerMiddle);
  5478. verifyFormat("int * x;", PointerMiddle);
  5479. verifyFormat("template <int * y> f() {}", PointerMiddle);
  5480. verifyFormat("int * f(int * a) {}", PointerMiddle);
  5481. verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
  5482. verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
  5483. verifyFormat("A<int *> a;", PointerMiddle);
  5484. verifyFormat("A<int **> a;", PointerMiddle);
  5485. verifyFormat("A<int *, int *> a;", PointerMiddle);
  5486. verifyFormat("A<int * []> a;", PointerMiddle);
  5487. verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
  5488. verifyFormat("A = new SomeType *[Length];", PointerMiddle);
  5489. verifyFormat("T ** t = new T *;", PointerMiddle);
  5490. // Member function reference qualifiers aren't binary operators.
  5491. verifyFormat("string // break\n"
  5492. "operator()() & {}");
  5493. verifyFormat("string // break\n"
  5494. "operator()() && {}");
  5495. verifyGoogleFormat("template <typename T>\n"
  5496. "auto x() & -> int {}");
  5497. }
  5498. TEST_F(FormatTest, UnderstandsAttributes) {
  5499. verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
  5500. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
  5501. "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
  5502. FormatStyle AfterType = getLLVMStyle();
  5503. AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
  5504. verifyFormat("__attribute__((nodebug)) void\n"
  5505. "foo() {}\n",
  5506. AfterType);
  5507. }
  5508. TEST_F(FormatTest, UnderstandsEllipsis) {
  5509. verifyFormat("int printf(const char *fmt, ...);");
  5510. verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
  5511. verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
  5512. FormatStyle PointersLeft = getLLVMStyle();
  5513. PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
  5514. verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
  5515. }
  5516. TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
  5517. EXPECT_EQ("int *a;\n"
  5518. "int *a;\n"
  5519. "int *a;",
  5520. format("int *a;\n"
  5521. "int* a;\n"
  5522. "int *a;",
  5523. getGoogleStyle()));
  5524. EXPECT_EQ("int* a;\n"
  5525. "int* a;\n"
  5526. "int* a;",
  5527. format("int* a;\n"
  5528. "int* a;\n"
  5529. "int *a;",
  5530. getGoogleStyle()));
  5531. EXPECT_EQ("int *a;\n"
  5532. "int *a;\n"
  5533. "int *a;",
  5534. format("int *a;\n"
  5535. "int * a;\n"
  5536. "int * a;",
  5537. getGoogleStyle()));
  5538. EXPECT_EQ("auto x = [] {\n"
  5539. " int *a;\n"
  5540. " int *a;\n"
  5541. " int *a;\n"
  5542. "};",
  5543. format("auto x=[]{int *a;\n"
  5544. "int * a;\n"
  5545. "int * a;};",
  5546. getGoogleStyle()));
  5547. }
  5548. TEST_F(FormatTest, UnderstandsRvalueReferences) {
  5549. verifyFormat("int f(int &&a) {}");
  5550. verifyFormat("int f(int a, char &&b) {}");
  5551. verifyFormat("void f() { int &&a = b; }");
  5552. verifyGoogleFormat("int f(int a, char&& b) {}");
  5553. verifyGoogleFormat("void f() { int&& a = b; }");
  5554. verifyIndependentOfContext("A<int &&> a;");
  5555. verifyIndependentOfContext("A<int &&, int &&> a;");
  5556. verifyGoogleFormat("A<int&&> a;");
  5557. verifyGoogleFormat("A<int&&, int&&> a;");
  5558. // Not rvalue references:
  5559. verifyFormat("template <bool B, bool C> class A {\n"
  5560. " static_assert(B && C, \"Something is wrong\");\n"
  5561. "};");
  5562. verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
  5563. verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
  5564. verifyFormat("#define A(a, b) (a && b)");
  5565. }
  5566. TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
  5567. verifyFormat("void f() {\n"
  5568. " x[aaaaaaaaa -\n"
  5569. " b] = 23;\n"
  5570. "}",
  5571. getLLVMStyleWithColumns(15));
  5572. }
  5573. TEST_F(FormatTest, FormatsCasts) {
  5574. verifyFormat("Type *A = static_cast<Type *>(P);");
  5575. verifyFormat("Type *A = (Type *)P;");
  5576. verifyFormat("Type *A = (vector<Type *, int *>)P;");
  5577. verifyFormat("int a = (int)(2.0f);");
  5578. verifyFormat("int a = (int)2.0f;");
  5579. verifyFormat("x[(int32)y];");
  5580. verifyFormat("x = (int32)y;");
  5581. verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
  5582. verifyFormat("int a = (int)*b;");
  5583. verifyFormat("int a = (int)2.0f;");
  5584. verifyFormat("int a = (int)~0;");
  5585. verifyFormat("int a = (int)++a;");
  5586. verifyFormat("int a = (int)sizeof(int);");
  5587. verifyFormat("int a = (int)+2;");
  5588. verifyFormat("my_int a = (my_int)2.0f;");
  5589. verifyFormat("my_int a = (my_int)sizeof(int);");
  5590. verifyFormat("return (my_int)aaa;");
  5591. verifyFormat("#define x ((int)-1)");
  5592. verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
  5593. verifyFormat("#define p(q) ((int *)&q)");
  5594. verifyFormat("fn(a)(b) + 1;");
  5595. verifyFormat("void f() { my_int a = (my_int)*b; }");
  5596. verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
  5597. verifyFormat("my_int a = (my_int)~0;");
  5598. verifyFormat("my_int a = (my_int)++a;");
  5599. verifyFormat("my_int a = (my_int)-2;");
  5600. verifyFormat("my_int a = (my_int)1;");
  5601. verifyFormat("my_int a = (my_int *)1;");
  5602. verifyFormat("my_int a = (const my_int)-1;");
  5603. verifyFormat("my_int a = (const my_int *)-1;");
  5604. verifyFormat("my_int a = (my_int)(my_int)-1;");
  5605. verifyFormat("my_int a = (ns::my_int)-2;");
  5606. verifyFormat("case (my_int)ONE:");
  5607. verifyFormat("auto x = (X)this;");
  5608. // FIXME: single value wrapped with paren will be treated as cast.
  5609. verifyFormat("void f(int i = (kValue)*kMask) {}");
  5610. verifyFormat("{ (void)F; }");
  5611. // Don't break after a cast's
  5612. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  5613. " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
  5614. " bbbbbbbbbbbbbbbbbbbbbb);");
  5615. // These are not casts.
  5616. verifyFormat("void f(int *) {}");
  5617. verifyFormat("f(foo)->b;");
  5618. verifyFormat("f(foo).b;");
  5619. verifyFormat("f(foo)(b);");
  5620. verifyFormat("f(foo)[b];");
  5621. verifyFormat("[](foo) { return 4; }(bar);");
  5622. verifyFormat("(*funptr)(foo)[4];");
  5623. verifyFormat("funptrs[4](foo)[4];");
  5624. verifyFormat("void f(int *);");
  5625. verifyFormat("void f(int *) = 0;");
  5626. verifyFormat("void f(SmallVector<int>) {}");
  5627. verifyFormat("void f(SmallVector<int>);");
  5628. verifyFormat("void f(SmallVector<int>) = 0;");
  5629. verifyFormat("void f(int i = (kA * kB) & kMask) {}");
  5630. verifyFormat("int a = sizeof(int) * b;");
  5631. verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
  5632. verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
  5633. verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
  5634. verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
  5635. // These are not casts, but at some point were confused with casts.
  5636. verifyFormat("virtual void foo(int *) override;");
  5637. verifyFormat("virtual void foo(char &) const;");
  5638. verifyFormat("virtual void foo(int *a, char *) const;");
  5639. verifyFormat("int a = sizeof(int *) + b;");
  5640. verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
  5641. verifyFormat("bool b = f(g<int>) && c;");
  5642. verifyFormat("typedef void (*f)(int i) func;");
  5643. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
  5644. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  5645. // FIXME: The indentation here is not ideal.
  5646. verifyFormat(
  5647. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5648. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
  5649. " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
  5650. }
  5651. TEST_F(FormatTest, FormatsFunctionTypes) {
  5652. verifyFormat("A<bool()> a;");
  5653. verifyFormat("A<SomeType()> a;");
  5654. verifyFormat("A<void (*)(int, std::string)> a;");
  5655. verifyFormat("A<void *(int)>;");
  5656. verifyFormat("void *(*a)(int *, SomeType *);");
  5657. verifyFormat("int (*func)(void *);");
  5658. verifyFormat("void f() { int (*func)(void *); }");
  5659. verifyFormat("template <class CallbackClass>\n"
  5660. "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
  5661. verifyGoogleFormat("A<void*(int*, SomeType*)>;");
  5662. verifyGoogleFormat("void* (*a)(int);");
  5663. verifyGoogleFormat(
  5664. "template <class CallbackClass>\n"
  5665. "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
  5666. // Other constructs can look somewhat like function types:
  5667. verifyFormat("A<sizeof(*x)> a;");
  5668. verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
  5669. verifyFormat("some_var = function(*some_pointer_var)[0];");
  5670. verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
  5671. verifyFormat("int x = f(&h)();");
  5672. verifyFormat("returnsFunction(&param1, &param2)(param);");
  5673. }
  5674. TEST_F(FormatTest, FormatsPointersToArrayTypes) {
  5675. verifyFormat("A (*foo_)[6];");
  5676. verifyFormat("vector<int> (*foo_)[6];");
  5677. }
  5678. TEST_F(FormatTest, BreaksLongVariableDeclarations) {
  5679. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5680. " LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5681. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
  5682. " LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5683. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5684. " *LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5685. // Different ways of ()-initializiation.
  5686. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5687. " LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
  5688. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5689. " LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
  5690. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5691. " LoooooooooooooooooooooooooooooooooooooooongVariable({});");
  5692. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5693. " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
  5694. }
  5695. TEST_F(FormatTest, BreaksLongDeclarations) {
  5696. verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
  5697. " AnotherNameForTheLongType;");
  5698. verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
  5699. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  5700. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5701. "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
  5702. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
  5703. "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
  5704. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5705. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5706. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
  5707. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5708. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  5709. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5710. verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
  5711. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5712. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5713. "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
  5714. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5715. "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
  5716. FormatStyle Indented = getLLVMStyle();
  5717. Indented.IndentWrappedFunctionNames = true;
  5718. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5719. " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
  5720. Indented);
  5721. verifyFormat(
  5722. "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5723. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5724. Indented);
  5725. verifyFormat(
  5726. "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  5727. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5728. Indented);
  5729. verifyFormat(
  5730. "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
  5731. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5732. Indented);
  5733. // FIXME: Without the comment, this breaks after "(".
  5734. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
  5735. " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
  5736. getGoogleStyle());
  5737. verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
  5738. " int LoooooooooooooooooooongParam2) {}");
  5739. verifyFormat(
  5740. "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
  5741. " SourceLocation L, IdentifierIn *II,\n"
  5742. " Type *T) {}");
  5743. verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
  5744. "ReallyReaaallyLongFunctionName(\n"
  5745. " const std::string &SomeParameter,\n"
  5746. " const SomeType<string, SomeOtherTemplateParameter>\n"
  5747. " &ReallyReallyLongParameterName,\n"
  5748. " const SomeType<string, SomeOtherTemplateParameter>\n"
  5749. " &AnotherLongParameterName) {}");
  5750. verifyFormat("template <typename A>\n"
  5751. "SomeLoooooooooooooooooooooongType<\n"
  5752. " typename some_namespace::SomeOtherType<A>::Type>\n"
  5753. "Function() {}");
  5754. verifyGoogleFormat(
  5755. "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
  5756. " aaaaaaaaaaaaaaaaaaaaaaa;");
  5757. verifyGoogleFormat(
  5758. "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
  5759. " SourceLocation L) {}");
  5760. verifyGoogleFormat(
  5761. "some_namespace::LongReturnType\n"
  5762. "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
  5763. " int first_long_parameter, int second_parameter) {}");
  5764. verifyGoogleFormat("template <typename T>\n"
  5765. "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  5766. "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
  5767. verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5768. " int aaaaaaaaaaaaaaaaaaaaaaa);");
  5769. verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  5770. " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5771. " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5772. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5773. " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  5774. " aaaaaaaaaaaaaaaaaaaaaaaa);");
  5775. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5776. " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  5777. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
  5778. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5779. }
  5780. TEST_F(FormatTest, FormatsArrays) {
  5781. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5782. " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
  5783. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
  5784. " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
  5785. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
  5786. " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
  5787. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5788. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  5789. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5790. " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
  5791. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5792. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5793. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  5794. verifyFormat(
  5795. "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
  5796. " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5797. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  5798. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
  5799. " .aaaaaaaaaaaaaaaaaaaaaa();");
  5800. verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
  5801. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
  5802. verifyFormat(
  5803. "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
  5804. " .aaaaaaa[0]\n"
  5805. " .aaaaaaaaaaaaaaaaaaaaaa();");
  5806. verifyFormat("a[::b::c];");
  5807. verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
  5808. FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
  5809. verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
  5810. }
  5811. TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
  5812. verifyFormat("(a)->b();");
  5813. verifyFormat("--a;");
  5814. }
  5815. TEST_F(FormatTest, HandlesIncludeDirectives) {
  5816. verifyFormat("#include <string>\n"
  5817. "#include <a/b/c.h>\n"
  5818. "#include \"a/b/string\"\n"
  5819. "#include \"string.h\"\n"
  5820. "#include \"string.h\"\n"
  5821. "#include <a-a>\n"
  5822. "#include < path with space >\n"
  5823. "#include_next <test.h>"
  5824. "#include \"abc.h\" // this is included for ABC\n"
  5825. "#include \"some long include\" // with a comment\n"
  5826. "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
  5827. getLLVMStyleWithColumns(35));
  5828. EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\""));
  5829. EXPECT_EQ("#include <a>", format("#include<a>"));
  5830. verifyFormat("#import <string>");
  5831. verifyFormat("#import <a/b/c.h>");
  5832. verifyFormat("#import \"a/b/string\"");
  5833. verifyFormat("#import \"string.h\"");
  5834. verifyFormat("#import \"string.h\"");
  5835. verifyFormat("#if __has_include(<strstream>)\n"
  5836. "#include <strstream>\n"
  5837. "#endif");
  5838. verifyFormat("#define MY_IMPORT <a/b>");
  5839. // Protocol buffer definition or missing "#".
  5840. verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
  5841. getLLVMStyleWithColumns(30));
  5842. FormatStyle Style = getLLVMStyle();
  5843. Style.AlwaysBreakBeforeMultilineStrings = true;
  5844. Style.ColumnLimit = 0;
  5845. verifyFormat("#import \"abc.h\"", Style);
  5846. // But 'import' might also be a regular C++ namespace.
  5847. verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5848. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5849. }
  5850. //===----------------------------------------------------------------------===//
  5851. // Error recovery tests.
  5852. //===----------------------------------------------------------------------===//
  5853. TEST_F(FormatTest, IncompleteParameterLists) {
  5854. FormatStyle NoBinPacking = getLLVMStyle();
  5855. NoBinPacking.BinPackParameters = false;
  5856. verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
  5857. " double *min_x,\n"
  5858. " double *max_x,\n"
  5859. " double *min_y,\n"
  5860. " double *max_y,\n"
  5861. " double *min_z,\n"
  5862. " double *max_z, ) {}",
  5863. NoBinPacking);
  5864. }
  5865. TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
  5866. verifyFormat("void f() { return; }\n42");
  5867. verifyFormat("void f() {\n"
  5868. " if (0)\n"
  5869. " return;\n"
  5870. "}\n"
  5871. "42");
  5872. verifyFormat("void f() { return }\n42");
  5873. verifyFormat("void f() {\n"
  5874. " if (0)\n"
  5875. " return\n"
  5876. "}\n"
  5877. "42");
  5878. }
  5879. TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
  5880. EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
  5881. EXPECT_EQ("void f() {\n"
  5882. " if (a)\n"
  5883. " return\n"
  5884. "}",
  5885. format("void f ( ) { if ( a ) return }"));
  5886. EXPECT_EQ("namespace N {\n"
  5887. "void f()\n"
  5888. "}",
  5889. format("namespace N { void f() }"));
  5890. EXPECT_EQ("namespace N {\n"
  5891. "void f() {}\n"
  5892. "void g()\n"
  5893. "}",
  5894. format("namespace N { void f( ) { } void g( ) }"));
  5895. }
  5896. TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
  5897. verifyFormat("int aaaaaaaa =\n"
  5898. " // Overlylongcomment\n"
  5899. " b;",
  5900. getLLVMStyleWithColumns(20));
  5901. verifyFormat("function(\n"
  5902. " ShortArgument,\n"
  5903. " LoooooooooooongArgument);\n",
  5904. getLLVMStyleWithColumns(20));
  5905. }
  5906. TEST_F(FormatTest, IncorrectAccessSpecifier) {
  5907. verifyFormat("public:");
  5908. verifyFormat("class A {\n"
  5909. "public\n"
  5910. " void f() {}\n"
  5911. "};");
  5912. verifyFormat("public\n"
  5913. "int qwerty;");
  5914. verifyFormat("public\n"
  5915. "B {}");
  5916. verifyFormat("public\n"
  5917. "{}");
  5918. verifyFormat("public\n"
  5919. "B { int x; }");
  5920. }
  5921. TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
  5922. verifyFormat("{");
  5923. verifyFormat("#})");
  5924. verifyNoCrash("(/**/[:!] ?[).");
  5925. }
  5926. TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
  5927. verifyFormat("do {\n}");
  5928. verifyFormat("do {\n}\n"
  5929. "f();");
  5930. verifyFormat("do {\n}\n"
  5931. "wheeee(fun);");
  5932. verifyFormat("do {\n"
  5933. " f();\n"
  5934. "}");
  5935. }
  5936. TEST_F(FormatTest, IncorrectCodeMissingParens) {
  5937. verifyFormat("if {\n foo;\n foo();\n}");
  5938. verifyFormat("switch {\n foo;\n foo();\n}");
  5939. verifyIncompleteFormat("for {\n foo;\n foo();\n}");
  5940. verifyFormat("while {\n foo;\n foo();\n}");
  5941. verifyFormat("do {\n foo;\n foo();\n} while;");
  5942. }
  5943. TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
  5944. verifyIncompleteFormat("namespace {\n"
  5945. "class Foo { Foo (\n"
  5946. "};\n"
  5947. "} // comment");
  5948. }
  5949. TEST_F(FormatTest, IncorrectCodeErrorDetection) {
  5950. EXPECT_EQ("{\n {}\n", format("{\n{\n}\n"));
  5951. EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
  5952. EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
  5953. EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n"));
  5954. EXPECT_EQ("{\n"
  5955. " {\n"
  5956. " breakme(\n"
  5957. " qwe);\n"
  5958. " }\n",
  5959. format("{\n"
  5960. " {\n"
  5961. " breakme(qwe);\n"
  5962. "}\n",
  5963. getLLVMStyleWithColumns(10)));
  5964. }
  5965. TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
  5966. verifyFormat("int x = {\n"
  5967. " avariable,\n"
  5968. " b(alongervariable)};",
  5969. getLLVMStyleWithColumns(25));
  5970. }
  5971. TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
  5972. verifyFormat("return (a)(b){1, 2, 3};");
  5973. }
  5974. TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
  5975. verifyFormat("vector<int> x{1, 2, 3, 4};");
  5976. verifyFormat("vector<int> x{\n"
  5977. " 1, 2, 3, 4,\n"
  5978. "};");
  5979. verifyFormat("vector<T> x{{}, {}, {}, {}};");
  5980. verifyFormat("f({1, 2});");
  5981. verifyFormat("auto v = Foo{-1};");
  5982. verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
  5983. verifyFormat("Class::Class : member{1, 2, 3} {}");
  5984. verifyFormat("new vector<int>{1, 2, 3};");
  5985. verifyFormat("new int[3]{1, 2, 3};");
  5986. verifyFormat("new int{1};");
  5987. verifyFormat("return {arg1, arg2};");
  5988. verifyFormat("return {arg1, SomeType{parameter}};");
  5989. verifyFormat("int count = set<int>{f(), g(), h()}.size();");
  5990. verifyFormat("new T{arg1, arg2};");
  5991. verifyFormat("f(MyMap[{composite, key}]);");
  5992. verifyFormat("class Class {\n"
  5993. " T member = {arg1, arg2};\n"
  5994. "};");
  5995. verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
  5996. verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
  5997. verifyFormat("int a = std::is_integral<int>{} + 0;");
  5998. verifyFormat("int foo(int i) { return fo1{}(i); }");
  5999. verifyFormat("int foo(int i) { return fo1{}(i); }");
  6000. verifyFormat("auto i = decltype(x){};");
  6001. verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
  6002. verifyFormat("Node n{1, Node{1000}, //\n"
  6003. " 2};");
  6004. verifyFormat("Aaaa aaaaaaa{\n"
  6005. " {\n"
  6006. " aaaa,\n"
  6007. " },\n"
  6008. "};");
  6009. verifyFormat("class C : public D {\n"
  6010. " SomeClass SC{2};\n"
  6011. "};");
  6012. verifyFormat("class C : public A {\n"
  6013. " class D : public B {\n"
  6014. " void f() { int i{2}; }\n"
  6015. " };\n"
  6016. "};");
  6017. verifyFormat("#define A {a, a},");
  6018. // In combination with BinPackArguments = false.
  6019. FormatStyle NoBinPacking = getLLVMStyle();
  6020. NoBinPacking.BinPackArguments = false;
  6021. verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
  6022. " bbbbb,\n"
  6023. " ccccc,\n"
  6024. " ddddd,\n"
  6025. " eeeee,\n"
  6026. " ffffff,\n"
  6027. " ggggg,\n"
  6028. " hhhhhh,\n"
  6029. " iiiiii,\n"
  6030. " jjjjjj,\n"
  6031. " kkkkkk};",
  6032. NoBinPacking);
  6033. verifyFormat("const Aaaaaa aaaaa = {\n"
  6034. " aaaaa,\n"
  6035. " bbbbb,\n"
  6036. " ccccc,\n"
  6037. " ddddd,\n"
  6038. " eeeee,\n"
  6039. " ffffff,\n"
  6040. " ggggg,\n"
  6041. " hhhhhh,\n"
  6042. " iiiiii,\n"
  6043. " jjjjjj,\n"
  6044. " kkkkkk,\n"
  6045. "};",
  6046. NoBinPacking);
  6047. verifyFormat(
  6048. "const Aaaaaa aaaaa = {\n"
  6049. " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n"
  6050. " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n"
  6051. " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
  6052. "};",
  6053. NoBinPacking);
  6054. // FIXME: The alignment of these trailing comments might be bad. Then again,
  6055. // this might be utterly useless in real code.
  6056. verifyFormat("Constructor::Constructor()\n"
  6057. " : some_value{ //\n"
  6058. " aaaaaaa, //\n"
  6059. " bbbbbbb} {}");
  6060. // In braced lists, the first comment is always assumed to belong to the
  6061. // first element. Thus, it can be moved to the next or previous line as
  6062. // appropriate.
  6063. EXPECT_EQ("function({// First element:\n"
  6064. " 1,\n"
  6065. " // Second element:\n"
  6066. " 2});",
  6067. format("function({\n"
  6068. " // First element:\n"
  6069. " 1,\n"
  6070. " // Second element:\n"
  6071. " 2});"));
  6072. EXPECT_EQ("std::vector<int> MyNumbers{\n"
  6073. " // First element:\n"
  6074. " 1,\n"
  6075. " // Second element:\n"
  6076. " 2};",
  6077. format("std::vector<int> MyNumbers{// First element:\n"
  6078. " 1,\n"
  6079. " // Second element:\n"
  6080. " 2};",
  6081. getLLVMStyleWithColumns(30)));
  6082. // A trailing comma should still lead to an enforced line break.
  6083. EXPECT_EQ("vector<int> SomeVector = {\n"
  6084. " // aaa\n"
  6085. " 1, 2,\n"
  6086. "};",
  6087. format("vector<int> SomeVector = { // aaa\n"
  6088. " 1, 2, };"));
  6089. FormatStyle ExtraSpaces = getLLVMStyle();
  6090. ExtraSpaces.Cpp11BracedListStyle = false;
  6091. ExtraSpaces.ColumnLimit = 75;
  6092. verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
  6093. verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
  6094. verifyFormat("f({ 1, 2 });", ExtraSpaces);
  6095. verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
  6096. verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
  6097. verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
  6098. verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
  6099. verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
  6100. verifyFormat("return { arg1, arg2 };", ExtraSpaces);
  6101. verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
  6102. verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
  6103. verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
  6104. verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
  6105. verifyFormat("class Class {\n"
  6106. " T member = { arg1, arg2 };\n"
  6107. "};",
  6108. ExtraSpaces);
  6109. verifyFormat(
  6110. "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6111. " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
  6112. " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  6113. " bbbbbbbbbbbbbbbbbbbb, bbbbb };",
  6114. ExtraSpaces);
  6115. verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
  6116. verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
  6117. ExtraSpaces);
  6118. verifyFormat(
  6119. "someFunction(OtherParam,\n"
  6120. " BracedList{ // comment 1 (Forcing interesting break)\n"
  6121. " param1, param2,\n"
  6122. " // comment 2\n"
  6123. " param3, param4 });",
  6124. ExtraSpaces);
  6125. verifyFormat(
  6126. "std::this_thread::sleep_for(\n"
  6127. " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
  6128. ExtraSpaces);
  6129. verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
  6130. " aaaaaaa,\n"
  6131. " aaaaaaaaaa,\n"
  6132. " aaaaa,\n"
  6133. " aaaaaaaaaaaaaaa,\n"
  6134. " aaa,\n"
  6135. " aaaaaaaaaa,\n"
  6136. " a,\n"
  6137. " aaaaaaaaaaaaaaaaaaaaa,\n"
  6138. " aaaaaaaaaaaa,\n"
  6139. " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
  6140. " aaaaaaa,\n"
  6141. " a};");
  6142. verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
  6143. }
  6144. TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
  6145. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6146. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6147. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6148. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6149. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6150. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  6151. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
  6152. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6153. " 1, 22, 333, 4444, 55555, //\n"
  6154. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6155. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  6156. verifyFormat(
  6157. "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6158. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6159. " 1, 22, 333, 4444, 55555, 666666, // comment\n"
  6160. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  6161. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  6162. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  6163. " 7777777};");
  6164. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  6165. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  6166. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  6167. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  6168. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  6169. " // Separating comment.\n"
  6170. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  6171. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  6172. " // Leading comment\n"
  6173. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  6174. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  6175. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  6176. " 1, 1, 1, 1};",
  6177. getLLVMStyleWithColumns(39));
  6178. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  6179. " 1, 1, 1, 1};",
  6180. getLLVMStyleWithColumns(38));
  6181. verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
  6182. " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
  6183. getLLVMStyleWithColumns(43));
  6184. verifyFormat(
  6185. "static unsigned SomeValues[10][3] = {\n"
  6186. " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n"
  6187. " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
  6188. verifyFormat("static auto fields = new vector<string>{\n"
  6189. " \"aaaaaaaaaaaaa\",\n"
  6190. " \"aaaaaaaaaaaaa\",\n"
  6191. " \"aaaaaaaaaaaa\",\n"
  6192. " \"aaaaaaaaaaaaaa\",\n"
  6193. " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
  6194. " \"aaaaaaaaaaaa\",\n"
  6195. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
  6196. "};");
  6197. verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
  6198. verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
  6199. " 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
  6200. " 3, cccccccccccccccccccccc};",
  6201. getLLVMStyleWithColumns(60));
  6202. // Trailing commas.
  6203. verifyFormat("vector<int> x = {\n"
  6204. " 1, 1, 1, 1, 1, 1, 1, 1,\n"
  6205. "};",
  6206. getLLVMStyleWithColumns(39));
  6207. verifyFormat("vector<int> x = {\n"
  6208. " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
  6209. "};",
  6210. getLLVMStyleWithColumns(39));
  6211. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  6212. " 1, 1, 1, 1,\n"
  6213. " /**/ /**/};",
  6214. getLLVMStyleWithColumns(39));
  6215. // Trailing comment in the first line.
  6216. verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n"
  6217. " 1111111111, 2222222222, 33333333333, 4444444444, //\n"
  6218. " 111111111, 222222222, 3333333333, 444444444, //\n"
  6219. " 11111111, 22222222, 333333333, 44444444};");
  6220. // Trailing comment in the last line.
  6221. verifyFormat("int aaaaa[] = {\n"
  6222. " 1, 2, 3, // comment\n"
  6223. " 4, 5, 6 // comment\n"
  6224. "};");
  6225. // With nested lists, we should either format one item per line or all nested
  6226. // lists one on line.
  6227. // FIXME: For some nested lists, we can do better.
  6228. verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
  6229. " {aaaaaaaaaaaaaaaaaaa},\n"
  6230. " {aaaaaaaaaaaaaaaaaaaaa},\n"
  6231. " {aaaaaaaaaaaaaaaaa}};",
  6232. getLLVMStyleWithColumns(60));
  6233. verifyFormat(
  6234. "SomeStruct my_struct_array = {\n"
  6235. " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
  6236. " aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
  6237. " {aaa, aaa},\n"
  6238. " {aaa, aaa},\n"
  6239. " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
  6240. " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6241. " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
  6242. // No column layout should be used here.
  6243. verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
  6244. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
  6245. verifyNoCrash("a<,");
  6246. // No braced initializer here.
  6247. verifyFormat("void f() {\n"
  6248. " struct Dummy {};\n"
  6249. " f(v);\n"
  6250. "}");
  6251. // Long lists should be formatted in columns even if they are nested.
  6252. verifyFormat(
  6253. "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6254. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6255. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6256. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6257. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6258. " 1, 22, 333, 4444, 55555, 666666, 7777777});");
  6259. }
  6260. TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
  6261. FormatStyle DoNotMerge = getLLVMStyle();
  6262. DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  6263. verifyFormat("void f() { return 42; }");
  6264. verifyFormat("void f() {\n"
  6265. " return 42;\n"
  6266. "}",
  6267. DoNotMerge);
  6268. verifyFormat("void f() {\n"
  6269. " // Comment\n"
  6270. "}");
  6271. verifyFormat("{\n"
  6272. "#error {\n"
  6273. " int a;\n"
  6274. "}");
  6275. verifyFormat("{\n"
  6276. " int a;\n"
  6277. "#error {\n"
  6278. "}");
  6279. verifyFormat("void f() {} // comment");
  6280. verifyFormat("void f() { int a; } // comment");
  6281. verifyFormat("void f() {\n"
  6282. "} // comment",
  6283. DoNotMerge);
  6284. verifyFormat("void f() {\n"
  6285. " int a;\n"
  6286. "} // comment",
  6287. DoNotMerge);
  6288. verifyFormat("void f() {\n"
  6289. "} // comment",
  6290. getLLVMStyleWithColumns(15));
  6291. verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
  6292. verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
  6293. verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
  6294. verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
  6295. verifyFormat("class C {\n"
  6296. " C()\n"
  6297. " : iiiiiiii(nullptr),\n"
  6298. " kkkkkkk(nullptr),\n"
  6299. " mmmmmmm(nullptr),\n"
  6300. " nnnnnnn(nullptr) {}\n"
  6301. "};",
  6302. getGoogleStyle());
  6303. FormatStyle NoColumnLimit = getLLVMStyle();
  6304. NoColumnLimit.ColumnLimit = 0;
  6305. EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
  6306. EXPECT_EQ("class C {\n"
  6307. " A() : b(0) {}\n"
  6308. "};",
  6309. format("class C{A():b(0){}};", NoColumnLimit));
  6310. EXPECT_EQ("A()\n"
  6311. " : b(0) {\n"
  6312. "}",
  6313. format("A()\n:b(0)\n{\n}", NoColumnLimit));
  6314. FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
  6315. DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
  6316. FormatStyle::SFS_None;
  6317. EXPECT_EQ("A()\n"
  6318. " : b(0) {\n"
  6319. "}",
  6320. format("A():b(0){}", DoNotMergeNoColumnLimit));
  6321. EXPECT_EQ("A()\n"
  6322. " : b(0) {\n"
  6323. "}",
  6324. format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
  6325. verifyFormat("#define A \\\n"
  6326. " void f() { \\\n"
  6327. " int i; \\\n"
  6328. " }",
  6329. getLLVMStyleWithColumns(20));
  6330. verifyFormat("#define A \\\n"
  6331. " void f() { int i; }",
  6332. getLLVMStyleWithColumns(21));
  6333. verifyFormat("#define A \\\n"
  6334. " void f() { \\\n"
  6335. " int i; \\\n"
  6336. " } \\\n"
  6337. " int j;",
  6338. getLLVMStyleWithColumns(22));
  6339. verifyFormat("#define A \\\n"
  6340. " void f() { int i; } \\\n"
  6341. " int j;",
  6342. getLLVMStyleWithColumns(23));
  6343. }
  6344. TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
  6345. FormatStyle MergeInlineOnly = getLLVMStyle();
  6346. MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  6347. verifyFormat("class C {\n"
  6348. " int f() { return 42; }\n"
  6349. "};",
  6350. MergeInlineOnly);
  6351. verifyFormat("int f() {\n"
  6352. " return 42;\n"
  6353. "}",
  6354. MergeInlineOnly);
  6355. }
  6356. TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
  6357. // Elaborate type variable declarations.
  6358. verifyFormat("struct foo a = {bar};\nint n;");
  6359. verifyFormat("class foo a = {bar};\nint n;");
  6360. verifyFormat("union foo a = {bar};\nint n;");
  6361. // Elaborate types inside function definitions.
  6362. verifyFormat("struct foo f() {}\nint n;");
  6363. verifyFormat("class foo f() {}\nint n;");
  6364. verifyFormat("union foo f() {}\nint n;");
  6365. // Templates.
  6366. verifyFormat("template <class X> void f() {}\nint n;");
  6367. verifyFormat("template <struct X> void f() {}\nint n;");
  6368. verifyFormat("template <union X> void f() {}\nint n;");
  6369. // Actual definitions...
  6370. verifyFormat("struct {\n} n;");
  6371. verifyFormat(
  6372. "template <template <class T, class Y>, class Z> class X {\n} n;");
  6373. verifyFormat("union Z {\n int n;\n} x;");
  6374. verifyFormat("class MACRO Z {\n} n;");
  6375. verifyFormat("class MACRO(X) Z {\n} n;");
  6376. verifyFormat("class __attribute__(X) Z {\n} n;");
  6377. verifyFormat("class __declspec(X) Z {\n} n;");
  6378. verifyFormat("class A##B##C {\n} n;");
  6379. verifyFormat("class alignas(16) Z {\n} n;");
  6380. verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
  6381. verifyFormat("class MACROA MACRO(X) Z {\n} n;");
  6382. // Redefinition from nested context:
  6383. verifyFormat("class A::B::C {\n} n;");
  6384. // Template definitions.
  6385. verifyFormat(
  6386. "template <typename F>\n"
  6387. "Matcher(const Matcher<F> &Other,\n"
  6388. " typename enable_if_c<is_base_of<F, T>::value &&\n"
  6389. " !is_same<F, T>::value>::type * = 0)\n"
  6390. " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
  6391. // FIXME: This is still incorrectly handled at the formatter side.
  6392. verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
  6393. verifyFormat("int i = SomeFunction(a<b, a> b);");
  6394. // FIXME:
  6395. // This now gets parsed incorrectly as class definition.
  6396. // verifyFormat("class A<int> f() {\n}\nint n;");
  6397. // Elaborate types where incorrectly parsing the structural element would
  6398. // break the indent.
  6399. verifyFormat("if (true)\n"
  6400. " class X x;\n"
  6401. "else\n"
  6402. " f();\n");
  6403. // This is simply incomplete. Formatting is not important, but must not crash.
  6404. verifyFormat("class A:");
  6405. }
  6406. TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
  6407. EXPECT_EQ("#error Leave all white!!!!! space* alone!\n",
  6408. format("#error Leave all white!!!!! space* alone!\n"));
  6409. EXPECT_EQ(
  6410. "#warning Leave all white!!!!! space* alone!\n",
  6411. format("#warning Leave all white!!!!! space* alone!\n"));
  6412. EXPECT_EQ("#error 1", format(" # error 1"));
  6413. EXPECT_EQ("#warning 1", format(" # warning 1"));
  6414. }
  6415. TEST_F(FormatTest, FormatHashIfExpressions) {
  6416. verifyFormat("#if AAAA && BBBB");
  6417. verifyFormat("#if (AAAA && BBBB)");
  6418. verifyFormat("#elif (AAAA && BBBB)");
  6419. // FIXME: Come up with a better indentation for #elif.
  6420. verifyFormat(
  6421. "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
  6422. " defined(BBBBBBBB)\n"
  6423. "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
  6424. " defined(BBBBBBBB)\n"
  6425. "#endif",
  6426. getLLVMStyleWithColumns(65));
  6427. }
  6428. TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
  6429. FormatStyle AllowsMergedIf = getGoogleStyle();
  6430. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  6431. verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
  6432. verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
  6433. verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
  6434. EXPECT_EQ("if (true) return 42;",
  6435. format("if (true)\nreturn 42;", AllowsMergedIf));
  6436. FormatStyle ShortMergedIf = AllowsMergedIf;
  6437. ShortMergedIf.ColumnLimit = 25;
  6438. verifyFormat("#define A \\\n"
  6439. " if (true) return 42;",
  6440. ShortMergedIf);
  6441. verifyFormat("#define A \\\n"
  6442. " f(); \\\n"
  6443. " if (true)\n"
  6444. "#define B",
  6445. ShortMergedIf);
  6446. verifyFormat("#define A \\\n"
  6447. " f(); \\\n"
  6448. " if (true)\n"
  6449. "g();",
  6450. ShortMergedIf);
  6451. verifyFormat("{\n"
  6452. "#ifdef A\n"
  6453. " // Comment\n"
  6454. " if (true) continue;\n"
  6455. "#endif\n"
  6456. " // Comment\n"
  6457. " if (true) continue;\n"
  6458. "}",
  6459. ShortMergedIf);
  6460. ShortMergedIf.ColumnLimit = 29;
  6461. verifyFormat("#define A \\\n"
  6462. " if (aaaaaaaaaa) return 1; \\\n"
  6463. " return 2;",
  6464. ShortMergedIf);
  6465. ShortMergedIf.ColumnLimit = 28;
  6466. verifyFormat("#define A \\\n"
  6467. " if (aaaaaaaaaa) \\\n"
  6468. " return 1; \\\n"
  6469. " return 2;",
  6470. ShortMergedIf);
  6471. }
  6472. TEST_F(FormatTest, BlockCommentsInControlLoops) {
  6473. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  6474. " f();\n"
  6475. "}");
  6476. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  6477. " f();\n"
  6478. "} /* another comment */ else /* comment #3 */ {\n"
  6479. " g();\n"
  6480. "}");
  6481. verifyFormat("while (0) /* a comment in a strange place */ {\n"
  6482. " f();\n"
  6483. "}");
  6484. verifyFormat("for (;;) /* a comment in a strange place */ {\n"
  6485. " f();\n"
  6486. "}");
  6487. verifyFormat("do /* a comment in a strange place */ {\n"
  6488. " f();\n"
  6489. "} /* another comment */ while (0);");
  6490. }
  6491. TEST_F(FormatTest, BlockComments) {
  6492. EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
  6493. format("/* *//* */ /* */\n/* *//* */ /* */"));
  6494. EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
  6495. EXPECT_EQ("#define A /*123*/ \\\n"
  6496. " b\n"
  6497. "/* */\n"
  6498. "someCall(\n"
  6499. " parameter);",
  6500. format("#define A /*123*/ b\n"
  6501. "/* */\n"
  6502. "someCall(parameter);",
  6503. getLLVMStyleWithColumns(15)));
  6504. EXPECT_EQ("#define A\n"
  6505. "/* */ someCall(\n"
  6506. " parameter);",
  6507. format("#define A\n"
  6508. "/* */someCall(parameter);",
  6509. getLLVMStyleWithColumns(15)));
  6510. EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
  6511. EXPECT_EQ("/*\n"
  6512. "*\n"
  6513. " * aaaaaa\n"
  6514. " * aaaaaa\n"
  6515. "*/",
  6516. format("/*\n"
  6517. "*\n"
  6518. " * aaaaaa aaaaaa\n"
  6519. "*/",
  6520. getLLVMStyleWithColumns(10)));
  6521. EXPECT_EQ("/*\n"
  6522. "**\n"
  6523. "* aaaaaa\n"
  6524. "*aaaaaa\n"
  6525. "*/",
  6526. format("/*\n"
  6527. "**\n"
  6528. "* aaaaaa aaaaaa\n"
  6529. "*/",
  6530. getLLVMStyleWithColumns(10)));
  6531. EXPECT_EQ("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  6532. " /* line 1\n"
  6533. " bbbbbbbbbbbb */\n"
  6534. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
  6535. format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  6536. " /* line 1\n"
  6537. " bbbbbbbbbbbb */ bbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
  6538. getLLVMStyleWithColumns(50)));
  6539. FormatStyle NoBinPacking = getLLVMStyle();
  6540. NoBinPacking.BinPackParameters = false;
  6541. EXPECT_EQ("someFunction(1, /* comment 1 */\n"
  6542. " 2, /* comment 2 */\n"
  6543. " 3, /* comment 3 */\n"
  6544. " aaaa,\n"
  6545. " bbbb);",
  6546. format("someFunction (1, /* comment 1 */\n"
  6547. " 2, /* comment 2 */ \n"
  6548. " 3, /* comment 3 */\n"
  6549. "aaaa, bbbb );",
  6550. NoBinPacking));
  6551. verifyFormat(
  6552. "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  6553. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  6554. EXPECT_EQ(
  6555. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  6556. " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  6557. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
  6558. format(
  6559. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  6560. " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  6561. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
  6562. EXPECT_EQ(
  6563. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  6564. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  6565. "int cccccccccccccccccccccccccccccc; /* comment */\n",
  6566. format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  6567. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  6568. "int cccccccccccccccccccccccccccccc; /* comment */\n"));
  6569. verifyFormat("void f(int * /* unused */) {}");
  6570. EXPECT_EQ("/*\n"
  6571. " **\n"
  6572. " */",
  6573. format("/*\n"
  6574. " **\n"
  6575. " */"));
  6576. EXPECT_EQ("/*\n"
  6577. " *q\n"
  6578. " */",
  6579. format("/*\n"
  6580. " *q\n"
  6581. " */"));
  6582. EXPECT_EQ("/*\n"
  6583. " * q\n"
  6584. " */",
  6585. format("/*\n"
  6586. " * q\n"
  6587. " */"));
  6588. EXPECT_EQ("/*\n"
  6589. " **/",
  6590. format("/*\n"
  6591. " **/"));
  6592. EXPECT_EQ("/*\n"
  6593. " ***/",
  6594. format("/*\n"
  6595. " ***/"));
  6596. }
  6597. TEST_F(FormatTest, BlockCommentsInMacros) {
  6598. EXPECT_EQ("#define A \\\n"
  6599. " { \\\n"
  6600. " /* one line */ \\\n"
  6601. " someCall();",
  6602. format("#define A { \\\n"
  6603. " /* one line */ \\\n"
  6604. " someCall();",
  6605. getLLVMStyleWithColumns(20)));
  6606. EXPECT_EQ("#define A \\\n"
  6607. " { \\\n"
  6608. " /* previous */ \\\n"
  6609. " /* one line */ \\\n"
  6610. " someCall();",
  6611. format("#define A { \\\n"
  6612. " /* previous */ \\\n"
  6613. " /* one line */ \\\n"
  6614. " someCall();",
  6615. getLLVMStyleWithColumns(20)));
  6616. }
  6617. TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
  6618. EXPECT_EQ("a = {\n"
  6619. " 1111 /* */\n"
  6620. "};",
  6621. format("a = {1111 /* */\n"
  6622. "};",
  6623. getLLVMStyleWithColumns(15)));
  6624. EXPECT_EQ("a = {\n"
  6625. " 1111 /* */\n"
  6626. "};",
  6627. format("a = {1111 /* */\n"
  6628. "};",
  6629. getLLVMStyleWithColumns(15)));
  6630. // FIXME: The formatting is still wrong here.
  6631. EXPECT_EQ("a = {\n"
  6632. " 1111 /* a\n"
  6633. " */\n"
  6634. "};",
  6635. format("a = {1111 /* a */\n"
  6636. "};",
  6637. getLLVMStyleWithColumns(15)));
  6638. }
  6639. TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
  6640. verifyFormat("{\n"
  6641. " // a\n"
  6642. " // b");
  6643. }
  6644. TEST_F(FormatTest, FormatStarDependingOnContext) {
  6645. verifyFormat("void f(int *a);");
  6646. verifyFormat("void f() { f(fint * b); }");
  6647. verifyFormat("class A {\n void f(int *a);\n};");
  6648. verifyFormat("class A {\n int *a;\n};");
  6649. verifyFormat("namespace a {\n"
  6650. "namespace b {\n"
  6651. "class A {\n"
  6652. " void f() {}\n"
  6653. " int *a;\n"
  6654. "};\n"
  6655. "}\n"
  6656. "}");
  6657. }
  6658. TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
  6659. verifyFormat("while");
  6660. verifyFormat("operator");
  6661. }
  6662. //===----------------------------------------------------------------------===//
  6663. // Objective-C tests.
  6664. //===----------------------------------------------------------------------===//
  6665. TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
  6666. verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
  6667. EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
  6668. format("-(NSUInteger)indexOfObject:(id)anObject;"));
  6669. EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
  6670. EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
  6671. EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
  6672. format("-(NSInteger)Method3:(id)anObject;"));
  6673. EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
  6674. format("-(NSInteger)Method4:(id)anObject;"));
  6675. EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
  6676. format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
  6677. EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
  6678. format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
  6679. EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
  6680. "forAllCells:(BOOL)flag;",
  6681. format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
  6682. "forAllCells:(BOOL)flag;"));
  6683. // Very long objectiveC method declaration.
  6684. verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
  6685. " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
  6686. verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
  6687. " inRange:(NSRange)range\n"
  6688. " outRange:(NSRange)out_range\n"
  6689. " outRange1:(NSRange)out_range1\n"
  6690. " outRange2:(NSRange)out_range2\n"
  6691. " outRange3:(NSRange)out_range3\n"
  6692. " outRange4:(NSRange)out_range4\n"
  6693. " outRange5:(NSRange)out_range5\n"
  6694. " outRange6:(NSRange)out_range6\n"
  6695. " outRange7:(NSRange)out_range7\n"
  6696. " outRange8:(NSRange)out_range8\n"
  6697. " outRange9:(NSRange)out_range9;");
  6698. // When the function name has to be wrapped.
  6699. FormatStyle Style = getLLVMStyle();
  6700. Style.IndentWrappedFunctionNames = false;
  6701. verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
  6702. "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
  6703. " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
  6704. "}",
  6705. Style);
  6706. Style.IndentWrappedFunctionNames = true;
  6707. verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
  6708. " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
  6709. " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
  6710. "}",
  6711. Style);
  6712. verifyFormat("- (int)sum:(vector<int>)numbers;");
  6713. verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
  6714. // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
  6715. // protocol lists (but not for template classes):
  6716. // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
  6717. verifyFormat("- (int (*)())foo:(int (*)())f;");
  6718. verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
  6719. // If there's no return type (very rare in practice!), LLVM and Google style
  6720. // agree.
  6721. verifyFormat("- foo;");
  6722. verifyFormat("- foo:(int)f;");
  6723. verifyGoogleFormat("- foo:(int)foo;");
  6724. }
  6725. TEST_F(FormatTest, FormatObjCInterface) {
  6726. verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
  6727. "@public\n"
  6728. " int field1;\n"
  6729. "@protected\n"
  6730. " int field2;\n"
  6731. "@private\n"
  6732. " int field3;\n"
  6733. "@package\n"
  6734. " int field4;\n"
  6735. "}\n"
  6736. "+ (id)init;\n"
  6737. "@end");
  6738. verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
  6739. " @public\n"
  6740. " int field1;\n"
  6741. " @protected\n"
  6742. " int field2;\n"
  6743. " @private\n"
  6744. " int field3;\n"
  6745. " @package\n"
  6746. " int field4;\n"
  6747. "}\n"
  6748. "+ (id)init;\n"
  6749. "@end");
  6750. verifyFormat("@interface /* wait for it */ Foo\n"
  6751. "+ (id)init;\n"
  6752. "// Look, a comment!\n"
  6753. "- (int)answerWith:(int)i;\n"
  6754. "@end");
  6755. verifyFormat("@interface Foo\n"
  6756. "@end\n"
  6757. "@interface Bar\n"
  6758. "@end");
  6759. verifyFormat("@interface Foo : Bar\n"
  6760. "+ (id)init;\n"
  6761. "@end");
  6762. verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
  6763. "+ (id)init;\n"
  6764. "@end");
  6765. verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
  6766. "+ (id)init;\n"
  6767. "@end");
  6768. verifyFormat("@interface Foo (HackStuff)\n"
  6769. "+ (id)init;\n"
  6770. "@end");
  6771. verifyFormat("@interface Foo ()\n"
  6772. "+ (id)init;\n"
  6773. "@end");
  6774. verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
  6775. "+ (id)init;\n"
  6776. "@end");
  6777. verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
  6778. "+ (id)init;\n"
  6779. "@end");
  6780. verifyFormat("@interface Foo {\n"
  6781. " int _i;\n"
  6782. "}\n"
  6783. "+ (id)init;\n"
  6784. "@end");
  6785. verifyFormat("@interface Foo : Bar {\n"
  6786. " int _i;\n"
  6787. "}\n"
  6788. "+ (id)init;\n"
  6789. "@end");
  6790. verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
  6791. " int _i;\n"
  6792. "}\n"
  6793. "+ (id)init;\n"
  6794. "@end");
  6795. verifyFormat("@interface Foo (HackStuff) {\n"
  6796. " int _i;\n"
  6797. "}\n"
  6798. "+ (id)init;\n"
  6799. "@end");
  6800. verifyFormat("@interface Foo () {\n"
  6801. " int _i;\n"
  6802. "}\n"
  6803. "+ (id)init;\n"
  6804. "@end");
  6805. verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
  6806. " int _i;\n"
  6807. "}\n"
  6808. "+ (id)init;\n"
  6809. "@end");
  6810. FormatStyle OnePerLine = getGoogleStyle();
  6811. OnePerLine.BinPackParameters = false;
  6812. verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n"
  6813. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6814. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6815. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6816. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
  6817. "}",
  6818. OnePerLine);
  6819. }
  6820. TEST_F(FormatTest, FormatObjCImplementation) {
  6821. verifyFormat("@implementation Foo : NSObject {\n"
  6822. "@public\n"
  6823. " int field1;\n"
  6824. "@protected\n"
  6825. " int field2;\n"
  6826. "@private\n"
  6827. " int field3;\n"
  6828. "@package\n"
  6829. " int field4;\n"
  6830. "}\n"
  6831. "+ (id)init {\n}\n"
  6832. "@end");
  6833. verifyGoogleFormat("@implementation Foo : NSObject {\n"
  6834. " @public\n"
  6835. " int field1;\n"
  6836. " @protected\n"
  6837. " int field2;\n"
  6838. " @private\n"
  6839. " int field3;\n"
  6840. " @package\n"
  6841. " int field4;\n"
  6842. "}\n"
  6843. "+ (id)init {\n}\n"
  6844. "@end");
  6845. verifyFormat("@implementation Foo\n"
  6846. "+ (id)init {\n"
  6847. " if (true)\n"
  6848. " return nil;\n"
  6849. "}\n"
  6850. "// Look, a comment!\n"
  6851. "- (int)answerWith:(int)i {\n"
  6852. " return i;\n"
  6853. "}\n"
  6854. "+ (int)answerWith:(int)i {\n"
  6855. " return i;\n"
  6856. "}\n"
  6857. "@end");
  6858. verifyFormat("@implementation Foo\n"
  6859. "@end\n"
  6860. "@implementation Bar\n"
  6861. "@end");
  6862. EXPECT_EQ("@implementation Foo : Bar\n"
  6863. "+ (id)init {\n}\n"
  6864. "- (void)foo {\n}\n"
  6865. "@end",
  6866. format("@implementation Foo : Bar\n"
  6867. "+(id)init{}\n"
  6868. "-(void)foo{}\n"
  6869. "@end"));
  6870. verifyFormat("@implementation Foo {\n"
  6871. " int _i;\n"
  6872. "}\n"
  6873. "+ (id)init {\n}\n"
  6874. "@end");
  6875. verifyFormat("@implementation Foo : Bar {\n"
  6876. " int _i;\n"
  6877. "}\n"
  6878. "+ (id)init {\n}\n"
  6879. "@end");
  6880. verifyFormat("@implementation Foo (HackStuff)\n"
  6881. "+ (id)init {\n}\n"
  6882. "@end");
  6883. verifyFormat("@implementation ObjcClass\n"
  6884. "- (void)method;\n"
  6885. "{}\n"
  6886. "@end");
  6887. }
  6888. TEST_F(FormatTest, FormatObjCProtocol) {
  6889. verifyFormat("@protocol Foo\n"
  6890. "@property(weak) id delegate;\n"
  6891. "- (NSUInteger)numberOfThings;\n"
  6892. "@end");
  6893. verifyFormat("@protocol MyProtocol <NSObject>\n"
  6894. "- (NSUInteger)numberOfThings;\n"
  6895. "@end");
  6896. verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
  6897. "- (NSUInteger)numberOfThings;\n"
  6898. "@end");
  6899. verifyFormat("@protocol Foo;\n"
  6900. "@protocol Bar;\n");
  6901. verifyFormat("@protocol Foo\n"
  6902. "@end\n"
  6903. "@protocol Bar\n"
  6904. "@end");
  6905. verifyFormat("@protocol myProtocol\n"
  6906. "- (void)mandatoryWithInt:(int)i;\n"
  6907. "@optional\n"
  6908. "- (void)optional;\n"
  6909. "@required\n"
  6910. "- (void)required;\n"
  6911. "@optional\n"
  6912. "@property(assign) int madProp;\n"
  6913. "@end\n");
  6914. verifyFormat("@property(nonatomic, assign, readonly)\n"
  6915. " int *looooooooooooooooooooooooooooongNumber;\n"
  6916. "@property(nonatomic, assign, readonly)\n"
  6917. " NSString *looooooooooooooooooooooooooooongName;");
  6918. verifyFormat("@implementation PR18406\n"
  6919. "}\n"
  6920. "@end");
  6921. }
  6922. TEST_F(FormatTest, FormatObjCMethodDeclarations) {
  6923. verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
  6924. " rect:(NSRect)theRect\n"
  6925. " interval:(float)theInterval {\n"
  6926. "}");
  6927. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6928. " longKeyword:(NSRect)theRect\n"
  6929. " longerKeyword:(float)theInterval\n"
  6930. " error:(NSError **)theError {\n"
  6931. "}");
  6932. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6933. " longKeyword:(NSRect)theRect\n"
  6934. " evenLongerKeyword:(float)theInterval\n"
  6935. " error:(NSError **)theError {\n"
  6936. "}");
  6937. verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
  6938. " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
  6939. " NS_DESIGNATED_INITIALIZER;",
  6940. getLLVMStyleWithColumns(60));
  6941. // Continuation indent width should win over aligning colons if the function
  6942. // name is long.
  6943. FormatStyle continuationStyle = getGoogleStyle();
  6944. continuationStyle.ColumnLimit = 40;
  6945. continuationStyle.IndentWrappedFunctionNames = true;
  6946. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6947. " dontAlignNamef:(NSRect)theRect {\n"
  6948. "}",
  6949. continuationStyle);
  6950. // Make sure we don't break aligning for short parameter names.
  6951. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6952. " aShortf:(NSRect)theRect {\n"
  6953. "}",
  6954. continuationStyle);
  6955. }
  6956. TEST_F(FormatTest, FormatObjCMethodExpr) {
  6957. verifyFormat("[foo bar:baz];");
  6958. verifyFormat("return [foo bar:baz];");
  6959. verifyFormat("return (a)[foo bar:baz];");
  6960. verifyFormat("f([foo bar:baz]);");
  6961. verifyFormat("f(2, [foo bar:baz]);");
  6962. verifyFormat("f(2, a ? b : c);");
  6963. verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
  6964. // Unary operators.
  6965. verifyFormat("int a = +[foo bar:baz];");
  6966. verifyFormat("int a = -[foo bar:baz];");
  6967. verifyFormat("int a = ![foo bar:baz];");
  6968. verifyFormat("int a = ~[foo bar:baz];");
  6969. verifyFormat("int a = ++[foo bar:baz];");
  6970. verifyFormat("int a = --[foo bar:baz];");
  6971. verifyFormat("int a = sizeof [foo bar:baz];");
  6972. verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
  6973. verifyFormat("int a = &[foo bar:baz];");
  6974. verifyFormat("int a = *[foo bar:baz];");
  6975. // FIXME: Make casts work, without breaking f()[4].
  6976. // verifyFormat("int a = (int)[foo bar:baz];");
  6977. // verifyFormat("return (int)[foo bar:baz];");
  6978. // verifyFormat("(void)[foo bar:baz];");
  6979. verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
  6980. // Binary operators.
  6981. verifyFormat("[foo bar:baz], [foo bar:baz];");
  6982. verifyFormat("[foo bar:baz] = [foo bar:baz];");
  6983. verifyFormat("[foo bar:baz] *= [foo bar:baz];");
  6984. verifyFormat("[foo bar:baz] /= [foo bar:baz];");
  6985. verifyFormat("[foo bar:baz] %= [foo bar:baz];");
  6986. verifyFormat("[foo bar:baz] += [foo bar:baz];");
  6987. verifyFormat("[foo bar:baz] -= [foo bar:baz];");
  6988. verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
  6989. verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
  6990. verifyFormat("[foo bar:baz] &= [foo bar:baz];");
  6991. verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
  6992. verifyFormat("[foo bar:baz] |= [foo bar:baz];");
  6993. verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
  6994. verifyFormat("[foo bar:baz] || [foo bar:baz];");
  6995. verifyFormat("[foo bar:baz] && [foo bar:baz];");
  6996. verifyFormat("[foo bar:baz] | [foo bar:baz];");
  6997. verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
  6998. verifyFormat("[foo bar:baz] & [foo bar:baz];");
  6999. verifyFormat("[foo bar:baz] == [foo bar:baz];");
  7000. verifyFormat("[foo bar:baz] != [foo bar:baz];");
  7001. verifyFormat("[foo bar:baz] >= [foo bar:baz];");
  7002. verifyFormat("[foo bar:baz] <= [foo bar:baz];");
  7003. verifyFormat("[foo bar:baz] > [foo bar:baz];");
  7004. verifyFormat("[foo bar:baz] < [foo bar:baz];");
  7005. verifyFormat("[foo bar:baz] >> [foo bar:baz];");
  7006. verifyFormat("[foo bar:baz] << [foo bar:baz];");
  7007. verifyFormat("[foo bar:baz] - [foo bar:baz];");
  7008. verifyFormat("[foo bar:baz] + [foo bar:baz];");
  7009. verifyFormat("[foo bar:baz] * [foo bar:baz];");
  7010. verifyFormat("[foo bar:baz] / [foo bar:baz];");
  7011. verifyFormat("[foo bar:baz] % [foo bar:baz];");
  7012. // Whew!
  7013. verifyFormat("return in[42];");
  7014. verifyFormat("for (auto v : in[1]) {\n}");
  7015. verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
  7016. verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
  7017. verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
  7018. verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
  7019. verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
  7020. verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
  7021. "}");
  7022. verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
  7023. verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
  7024. verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
  7025. verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
  7026. verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
  7027. verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
  7028. verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
  7029. verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
  7030. verifyFormat("[button setAction:@selector(zoomOut:)];");
  7031. verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
  7032. verifyFormat("arr[[self indexForFoo:a]];");
  7033. verifyFormat("throw [self errorFor:a];");
  7034. verifyFormat("@throw [self errorFor:a];");
  7035. verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
  7036. verifyFormat("[(id)foo bar:(id) ? baz : quux];");
  7037. verifyFormat("4 > 4 ? (id)a : (id)baz;");
  7038. // This tests that the formatter doesn't break after "backing" but before ":",
  7039. // which would be at 80 columns.
  7040. verifyFormat(
  7041. "void f() {\n"
  7042. " if ((self = [super initWithContentRect:contentRect\n"
  7043. " styleMask:styleMask ?: otherMask\n"
  7044. " backing:NSBackingStoreBuffered\n"
  7045. " defer:YES]))");
  7046. verifyFormat(
  7047. "[foo checkThatBreakingAfterColonWorksOk:\n"
  7048. " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
  7049. verifyFormat("[myObj short:arg1 // Force line break\n"
  7050. " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
  7051. " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
  7052. " error:arg4];");
  7053. verifyFormat(
  7054. "void f() {\n"
  7055. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  7056. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  7057. " pos.width(), pos.height())\n"
  7058. " styleMask:NSBorderlessWindowMask\n"
  7059. " backing:NSBackingStoreBuffered\n"
  7060. " defer:NO]);\n"
  7061. "}");
  7062. verifyFormat(
  7063. "void f() {\n"
  7064. " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
  7065. " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
  7066. " pos.width(), pos.height())\n"
  7067. " syeMask:NSBorderlessWindowMask\n"
  7068. " bking:NSBackingStoreBuffered\n"
  7069. " der:NO]);\n"
  7070. "}",
  7071. getLLVMStyleWithColumns(70));
  7072. verifyFormat(
  7073. "void f() {\n"
  7074. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  7075. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  7076. " pos.width(), pos.height())\n"
  7077. " styleMask:NSBorderlessWindowMask\n"
  7078. " backing:NSBackingStoreBuffered\n"
  7079. " defer:NO]);\n"
  7080. "}",
  7081. getChromiumStyle(FormatStyle::LK_Cpp));
  7082. verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
  7083. " with:contentsNativeView];");
  7084. verifyFormat(
  7085. "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
  7086. " owner:nillllll];");
  7087. verifyFormat(
  7088. "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
  7089. " forType:kBookmarkButtonDragType];");
  7090. verifyFormat("[defaultCenter addObserver:self\n"
  7091. " selector:@selector(willEnterFullscreen)\n"
  7092. " name:kWillEnterFullscreenNotification\n"
  7093. " object:nil];");
  7094. verifyFormat("[image_rep drawInRect:drawRect\n"
  7095. " fromRect:NSZeroRect\n"
  7096. " operation:NSCompositeCopy\n"
  7097. " fraction:1.0\n"
  7098. " respectFlipped:NO\n"
  7099. " hints:nil];");
  7100. verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7101. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  7102. verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  7103. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  7104. verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
  7105. " aaaaaaaaaaaaaaaaaaaaaa];");
  7106. verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
  7107. " .aaaaaaaa];", // FIXME: Indentation seems off.
  7108. getLLVMStyleWithColumns(60));
  7109. verifyFormat(
  7110. "scoped_nsobject<NSTextField> message(\n"
  7111. " // The frame will be fixed up when |-setMessageText:| is called.\n"
  7112. " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
  7113. verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
  7114. " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
  7115. " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
  7116. " aaaa:bbb];");
  7117. verifyFormat("[self param:function( //\n"
  7118. " parameter)]");
  7119. verifyFormat(
  7120. "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  7121. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  7122. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
  7123. // FIXME: This violates the column limit.
  7124. verifyFormat(
  7125. "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7126. " aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
  7127. " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];",
  7128. getLLVMStyleWithColumns(60));
  7129. // Variadic parameters.
  7130. verifyFormat(
  7131. "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
  7132. verifyFormat(
  7133. "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  7134. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  7135. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
  7136. verifyFormat("[self // break\n"
  7137. " a:a\n"
  7138. " aaa:aaa];");
  7139. verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
  7140. " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
  7141. }
  7142. TEST_F(FormatTest, ObjCAt) {
  7143. verifyFormat("@autoreleasepool");
  7144. verifyFormat("@catch");
  7145. verifyFormat("@class");
  7146. verifyFormat("@compatibility_alias");
  7147. verifyFormat("@defs");
  7148. verifyFormat("@dynamic");
  7149. verifyFormat("@encode");
  7150. verifyFormat("@end");
  7151. verifyFormat("@finally");
  7152. verifyFormat("@implementation");
  7153. verifyFormat("@import");
  7154. verifyFormat("@interface");
  7155. verifyFormat("@optional");
  7156. verifyFormat("@package");
  7157. verifyFormat("@private");
  7158. verifyFormat("@property");
  7159. verifyFormat("@protected");
  7160. verifyFormat("@protocol");
  7161. verifyFormat("@public");
  7162. verifyFormat("@required");
  7163. verifyFormat("@selector");
  7164. verifyFormat("@synchronized");
  7165. verifyFormat("@synthesize");
  7166. verifyFormat("@throw");
  7167. verifyFormat("@try");
  7168. EXPECT_EQ("@interface", format("@ interface"));
  7169. // The precise formatting of this doesn't matter, nobody writes code like
  7170. // this.
  7171. verifyFormat("@ /*foo*/ interface");
  7172. }
  7173. TEST_F(FormatTest, ObjCSnippets) {
  7174. verifyFormat("@autoreleasepool {\n"
  7175. " foo();\n"
  7176. "}");
  7177. verifyFormat("@class Foo, Bar;");
  7178. verifyFormat("@compatibility_alias AliasName ExistingClass;");
  7179. verifyFormat("@dynamic textColor;");
  7180. verifyFormat("char *buf1 = @encode(int *);");
  7181. verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
  7182. verifyFormat("char *buf1 = @encode(int **);");
  7183. verifyFormat("Protocol *proto = @protocol(p1);");
  7184. verifyFormat("SEL s = @selector(foo:);");
  7185. verifyFormat("@synchronized(self) {\n"
  7186. " f();\n"
  7187. "}");
  7188. verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  7189. verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  7190. verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
  7191. verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
  7192. verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
  7193. verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
  7194. getMozillaStyle());
  7195. verifyFormat("@property BOOL editable;", getMozillaStyle());
  7196. verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
  7197. getWebKitStyle());
  7198. verifyFormat("@property BOOL editable;", getWebKitStyle());
  7199. verifyFormat("@import foo.bar;\n"
  7200. "@import baz;");
  7201. }
  7202. TEST_F(FormatTest, ObjCForIn) {
  7203. verifyFormat("- (void)test {\n"
  7204. " for (NSString *n in arrayOfStrings) {\n"
  7205. " foo(n);\n"
  7206. " }\n"
  7207. "}");
  7208. verifyFormat("- (void)test {\n"
  7209. " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n"
  7210. " foo(n);\n"
  7211. " }\n"
  7212. "}");
  7213. }
  7214. TEST_F(FormatTest, ObjCLiterals) {
  7215. verifyFormat("@\"String\"");
  7216. verifyFormat("@1");
  7217. verifyFormat("@+4.8");
  7218. verifyFormat("@-4");
  7219. verifyFormat("@1LL");
  7220. verifyFormat("@.5");
  7221. verifyFormat("@'c'");
  7222. verifyFormat("@true");
  7223. verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
  7224. verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
  7225. verifyFormat("NSNumber *favoriteColor = @(Green);");
  7226. verifyFormat("NSString *path = @(getenv(\"PATH\"));");
  7227. verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
  7228. }
  7229. TEST_F(FormatTest, ObjCDictLiterals) {
  7230. verifyFormat("@{");
  7231. verifyFormat("@{}");
  7232. verifyFormat("@{@\"one\" : @1}");
  7233. verifyFormat("return @{@\"one\" : @1;");
  7234. verifyFormat("@{@\"one\" : @1}");
  7235. verifyFormat("@{@\"one\" : @{@2 : @1}}");
  7236. verifyFormat("@{\n"
  7237. " @\"one\" : @{@2 : @1},\n"
  7238. "}");
  7239. verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
  7240. verifyIncompleteFormat("[self setDict:@{}");
  7241. verifyIncompleteFormat("[self setDict:@{@1 : @2}");
  7242. verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
  7243. verifyFormat(
  7244. "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
  7245. verifyFormat(
  7246. "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
  7247. verifyFormat("NSDictionary *d = @{\n"
  7248. " @\"nam\" : NSUserNam(),\n"
  7249. " @\"dte\" : [NSDate date],\n"
  7250. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  7251. "};");
  7252. verifyFormat(
  7253. "@{\n"
  7254. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  7255. "regularFont,\n"
  7256. "};");
  7257. verifyGoogleFormat(
  7258. "@{\n"
  7259. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  7260. "regularFont,\n"
  7261. "};");
  7262. verifyFormat(
  7263. "@{\n"
  7264. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
  7265. " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
  7266. "};");
  7267. // We should try to be robust in case someone forgets the "@".
  7268. verifyFormat("NSDictionary *d = {\n"
  7269. " @\"nam\" : NSUserNam(),\n"
  7270. " @\"dte\" : [NSDate date],\n"
  7271. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  7272. "};");
  7273. verifyFormat("NSMutableDictionary *dictionary =\n"
  7274. " [NSMutableDictionary dictionaryWithDictionary:@{\n"
  7275. " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
  7276. " bbbbbbbbbbbbbbbbbb : bbbbb,\n"
  7277. " cccccccccccccccc : ccccccccccccccc\n"
  7278. " }];");
  7279. // Ensure that casts before the key are kept on the same line as the key.
  7280. verifyFormat(
  7281. "NSDictionary *d = @{\n"
  7282. " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n"
  7283. " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n"
  7284. "};");
  7285. }
  7286. TEST_F(FormatTest, ObjCArrayLiterals) {
  7287. verifyIncompleteFormat("@[");
  7288. verifyFormat("@[]");
  7289. verifyFormat(
  7290. "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
  7291. verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
  7292. verifyFormat("NSArray *array = @[ [foo description] ];");
  7293. verifyFormat(
  7294. "NSArray *some_variable = @[\n"
  7295. " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
  7296. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7297. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7298. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7299. "];");
  7300. verifyFormat(
  7301. "NSArray *some_variable = @[\n"
  7302. " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
  7303. " @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n"
  7304. "];");
  7305. verifyFormat("NSArray *some_variable = @[\n"
  7306. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7307. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7308. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7309. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7310. "];");
  7311. verifyFormat("NSArray *array = @[\n"
  7312. " @\"a\",\n"
  7313. " @\"a\",\n" // Trailing comma -> one per line.
  7314. "];");
  7315. // We should try to be robust in case someone forgets the "@".
  7316. verifyFormat("NSArray *some_variable = [\n"
  7317. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7318. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7319. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7320. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7321. "];");
  7322. verifyFormat(
  7323. "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
  7324. " index:(NSUInteger)index\n"
  7325. " nonDigitAttributes:\n"
  7326. " (NSDictionary *)noDigitAttributes;");
  7327. verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
  7328. " NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
  7329. "]];");
  7330. }
  7331. TEST_F(FormatTest, BreaksStringLiterals) {
  7332. EXPECT_EQ("\"some text \"\n"
  7333. "\"other\";",
  7334. format("\"some text other\";", getLLVMStyleWithColumns(12)));
  7335. EXPECT_EQ("\"some text \"\n"
  7336. "\"other\";",
  7337. format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
  7338. EXPECT_EQ(
  7339. "#define A \\\n"
  7340. " \"some \" \\\n"
  7341. " \"text \" \\\n"
  7342. " \"other\";",
  7343. format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
  7344. EXPECT_EQ(
  7345. "#define A \\\n"
  7346. " \"so \" \\\n"
  7347. " \"text \" \\\n"
  7348. " \"other\";",
  7349. format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
  7350. EXPECT_EQ("\"some text\"",
  7351. format("\"some text\"", getLLVMStyleWithColumns(1)));
  7352. EXPECT_EQ("\"some text\"",
  7353. format("\"some text\"", getLLVMStyleWithColumns(11)));
  7354. EXPECT_EQ("\"some \"\n"
  7355. "\"text\"",
  7356. format("\"some text\"", getLLVMStyleWithColumns(10)));
  7357. EXPECT_EQ("\"some \"\n"
  7358. "\"text\"",
  7359. format("\"some text\"", getLLVMStyleWithColumns(7)));
  7360. EXPECT_EQ("\"some\"\n"
  7361. "\" tex\"\n"
  7362. "\"t\"",
  7363. format("\"some text\"", getLLVMStyleWithColumns(6)));
  7364. EXPECT_EQ("\"some\"\n"
  7365. "\" tex\"\n"
  7366. "\" and\"",
  7367. format("\"some tex and\"", getLLVMStyleWithColumns(6)));
  7368. EXPECT_EQ("\"some\"\n"
  7369. "\"/tex\"\n"
  7370. "\"/and\"",
  7371. format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
  7372. EXPECT_EQ("variable =\n"
  7373. " \"long string \"\n"
  7374. " \"literal\";",
  7375. format("variable = \"long string literal\";",
  7376. getLLVMStyleWithColumns(20)));
  7377. EXPECT_EQ("variable = f(\n"
  7378. " \"long string \"\n"
  7379. " \"literal\",\n"
  7380. " short,\n"
  7381. " loooooooooooooooooooong);",
  7382. format("variable = f(\"long string literal\", short, "
  7383. "loooooooooooooooooooong);",
  7384. getLLVMStyleWithColumns(20)));
  7385. EXPECT_EQ(
  7386. "f(g(\"long string \"\n"
  7387. " \"literal\"),\n"
  7388. " b);",
  7389. format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
  7390. EXPECT_EQ("f(g(\"long string \"\n"
  7391. " \"literal\",\n"
  7392. " a),\n"
  7393. " b);",
  7394. format("f(g(\"long string literal\", a), b);",
  7395. getLLVMStyleWithColumns(20)));
  7396. EXPECT_EQ(
  7397. "f(\"one two\".split(\n"
  7398. " variable));",
  7399. format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
  7400. EXPECT_EQ("f(\"one two three four five six \"\n"
  7401. " \"seven\".split(\n"
  7402. " really_looooong_variable));",
  7403. format("f(\"one two three four five six seven\"."
  7404. "split(really_looooong_variable));",
  7405. getLLVMStyleWithColumns(33)));
  7406. EXPECT_EQ("f(\"some \"\n"
  7407. " \"text\",\n"
  7408. " other);",
  7409. format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
  7410. // Only break as a last resort.
  7411. verifyFormat(
  7412. "aaaaaaaaaaaaaaaaaaaa(\n"
  7413. " aaaaaaaaaaaaaaaaaaaa,\n"
  7414. " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
  7415. EXPECT_EQ("\"splitmea\"\n"
  7416. "\"trandomp\"\n"
  7417. "\"oint\"",
  7418. format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
  7419. EXPECT_EQ("\"split/\"\n"
  7420. "\"pathat/\"\n"
  7421. "\"slashes\"",
  7422. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  7423. EXPECT_EQ("\"split/\"\n"
  7424. "\"pathat/\"\n"
  7425. "\"slashes\"",
  7426. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  7427. EXPECT_EQ("\"split at \"\n"
  7428. "\"spaces/at/\"\n"
  7429. "\"slashes.at.any$\"\n"
  7430. "\"non-alphanumeric%\"\n"
  7431. "\"1111111111characte\"\n"
  7432. "\"rs\"",
  7433. format("\"split at "
  7434. "spaces/at/"
  7435. "slashes.at."
  7436. "any$non-"
  7437. "alphanumeric%"
  7438. "1111111111characte"
  7439. "rs\"",
  7440. getLLVMStyleWithColumns(20)));
  7441. // Verify that splitting the strings understands
  7442. // Style::AlwaysBreakBeforeMultilineStrings.
  7443. EXPECT_EQ(
  7444. "aaaaaaaaaaaa(\n"
  7445. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
  7446. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
  7447. format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
  7448. "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
  7449. "aaaaaaaaaaaaaaaaaaaaaa\");",
  7450. getGoogleStyle()));
  7451. EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  7452. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
  7453. format("return \"aaaaaaaaaaaaaaaaaaaaaa "
  7454. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
  7455. "aaaaaaaaaaaaaaaaaaaaaa\";",
  7456. getGoogleStyle()));
  7457. EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  7458. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  7459. format("llvm::outs() << "
  7460. "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
  7461. "aaaaaaaaaaaaaaaaaaa\";"));
  7462. EXPECT_EQ("ffff(\n"
  7463. " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  7464. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  7465. format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  7466. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  7467. getGoogleStyle()));
  7468. FormatStyle Style = getLLVMStyleWithColumns(12);
  7469. Style.BreakStringLiterals = false;
  7470. EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
  7471. FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
  7472. AlignLeft.AlignEscapedNewlinesLeft = true;
  7473. EXPECT_EQ("#define A \\\n"
  7474. " \"some \" \\\n"
  7475. " \"text \" \\\n"
  7476. " \"other\";",
  7477. format("#define A \"some text other\";", AlignLeft));
  7478. }
  7479. TEST_F(FormatTest, FullyRemoveEmptyLines) {
  7480. FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
  7481. NoEmptyLines.MaxEmptyLinesToKeep = 0;
  7482. EXPECT_EQ("int i = a(b());",
  7483. format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
  7484. }
  7485. TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
  7486. EXPECT_EQ(
  7487. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  7488. "(\n"
  7489. " \"x\t\");",
  7490. format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  7491. "aaaaaaa("
  7492. "\"x\t\");"));
  7493. }
  7494. TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
  7495. EXPECT_EQ(
  7496. "u8\"utf8 string \"\n"
  7497. "u8\"literal\";",
  7498. format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
  7499. EXPECT_EQ(
  7500. "u\"utf16 string \"\n"
  7501. "u\"literal\";",
  7502. format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
  7503. EXPECT_EQ(
  7504. "U\"utf32 string \"\n"
  7505. "U\"literal\";",
  7506. format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
  7507. EXPECT_EQ("L\"wide string \"\n"
  7508. "L\"literal\";",
  7509. format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
  7510. EXPECT_EQ("@\"NSString \"\n"
  7511. "@\"literal\";",
  7512. format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
  7513. // This input makes clang-format try to split the incomplete unicode escape
  7514. // sequence, which used to lead to a crasher.
  7515. verifyNoCrash(
  7516. "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  7517. getLLVMStyleWithColumns(60));
  7518. }
  7519. TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
  7520. FormatStyle Style = getGoogleStyleWithColumns(15);
  7521. EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
  7522. EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
  7523. EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
  7524. EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
  7525. EXPECT_EQ("u8R\"x(raw literal)x\";",
  7526. format("u8R\"x(raw literal)x\";", Style));
  7527. }
  7528. TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
  7529. FormatStyle Style = getLLVMStyleWithColumns(20);
  7530. EXPECT_EQ(
  7531. "_T(\"aaaaaaaaaaaaaa\")\n"
  7532. "_T(\"aaaaaaaaaaaaaa\")\n"
  7533. "_T(\"aaaaaaaaaaaa\")",
  7534. format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
  7535. EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
  7536. " _T(\"aaaaaa\"),\n"
  7537. " z);",
  7538. format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
  7539. // FIXME: Handle embedded spaces in one iteration.
  7540. // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
  7541. // "_T(\"aaaaaaaaaaaaa\")\n"
  7542. // "_T(\"aaaaaaaaaaaaa\")\n"
  7543. // "_T(\"a\")",
  7544. // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  7545. // getLLVMStyleWithColumns(20)));
  7546. EXPECT_EQ(
  7547. "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  7548. format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
  7549. EXPECT_EQ("f(\n"
  7550. "#if !TEST\n"
  7551. " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
  7552. "#endif\n"
  7553. " );",
  7554. format("f(\n"
  7555. "#if !TEST\n"
  7556. "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
  7557. "#endif\n"
  7558. ");"));
  7559. EXPECT_EQ("f(\n"
  7560. "\n"
  7561. " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
  7562. format("f(\n"
  7563. "\n"
  7564. "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
  7565. }
  7566. TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
  7567. EXPECT_EQ(
  7568. "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7569. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7570. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  7571. format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7572. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7573. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
  7574. }
  7575. TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
  7576. EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
  7577. format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle()));
  7578. EXPECT_EQ("fffffffffff(g(R\"x(\n"
  7579. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7580. ")x\",\n"
  7581. " a),\n"
  7582. " b);",
  7583. format("fffffffffff(g(R\"x(\n"
  7584. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7585. ")x\", a), b);",
  7586. getGoogleStyleWithColumns(20)));
  7587. EXPECT_EQ("fffffffffff(\n"
  7588. " g(R\"x(qqq\n"
  7589. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7590. ")x\",\n"
  7591. " a),\n"
  7592. " b);",
  7593. format("fffffffffff(g(R\"x(qqq\n"
  7594. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7595. ")x\", a), b);",
  7596. getGoogleStyleWithColumns(20)));
  7597. EXPECT_EQ("fffffffffff(R\"x(\n"
  7598. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7599. ")x\");",
  7600. format("fffffffffff(R\"x(\n"
  7601. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7602. ")x\");",
  7603. getGoogleStyleWithColumns(20)));
  7604. EXPECT_EQ("fffffffffff(R\"x(\n"
  7605. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7606. ")x\" + bbbbbb);",
  7607. format("fffffffffff(R\"x(\n"
  7608. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7609. ")x\" + bbbbbb);",
  7610. getGoogleStyleWithColumns(20)));
  7611. EXPECT_EQ("fffffffffff(\n"
  7612. " R\"x(\n"
  7613. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7614. ")x\" +\n"
  7615. " bbbbbb);",
  7616. format("fffffffffff(\n"
  7617. " R\"x(\n"
  7618. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7619. ")x\" + bbbbbb);",
  7620. getGoogleStyleWithColumns(20)));
  7621. }
  7622. TEST_F(FormatTest, SkipsUnknownStringLiterals) {
  7623. verifyFormat("string a = \"unterminated;");
  7624. EXPECT_EQ("function(\"unterminated,\n"
  7625. " OtherParameter);",
  7626. format("function( \"unterminated,\n"
  7627. " OtherParameter);"));
  7628. }
  7629. TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
  7630. FormatStyle Style = getLLVMStyle();
  7631. Style.Standard = FormatStyle::LS_Cpp03;
  7632. EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
  7633. format("#define x(_a) printf(\"foo\"_a);", Style));
  7634. }
  7635. TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
  7636. TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
  7637. EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
  7638. " \"ddeeefff\");",
  7639. format("someFunction(\"aaabbbcccdddeeefff\");",
  7640. getLLVMStyleWithColumns(25)));
  7641. EXPECT_EQ("someFunction1234567890(\n"
  7642. " \"aaabbbcccdddeeefff\");",
  7643. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  7644. getLLVMStyleWithColumns(26)));
  7645. EXPECT_EQ("someFunction1234567890(\n"
  7646. " \"aaabbbcccdddeeeff\"\n"
  7647. " \"f\");",
  7648. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  7649. getLLVMStyleWithColumns(25)));
  7650. EXPECT_EQ("someFunction1234567890(\n"
  7651. " \"aaabbbcccdddeeeff\"\n"
  7652. " \"f\");",
  7653. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  7654. getLLVMStyleWithColumns(24)));
  7655. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  7656. " \"ddde \"\n"
  7657. " \"efff\");",
  7658. format("someFunction(\"aaabbbcc ddde efff\");",
  7659. getLLVMStyleWithColumns(25)));
  7660. EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
  7661. " \"ddeeefff\");",
  7662. format("someFunction(\"aaabbbccc ddeeefff\");",
  7663. getLLVMStyleWithColumns(25)));
  7664. EXPECT_EQ("someFunction1234567890(\n"
  7665. " \"aaabb \"\n"
  7666. " \"cccdddeeefff\");",
  7667. format("someFunction1234567890(\"aaabb cccdddeeefff\");",
  7668. getLLVMStyleWithColumns(25)));
  7669. EXPECT_EQ("#define A \\\n"
  7670. " string s = \\\n"
  7671. " \"123456789\" \\\n"
  7672. " \"0\"; \\\n"
  7673. " int i;",
  7674. format("#define A string s = \"1234567890\"; int i;",
  7675. getLLVMStyleWithColumns(20)));
  7676. // FIXME: Put additional penalties on breaking at non-whitespace locations.
  7677. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  7678. " \"dddeeeff\"\n"
  7679. " \"f\");",
  7680. format("someFunction(\"aaabbbcc dddeeefff\");",
  7681. getLLVMStyleWithColumns(25)));
  7682. }
  7683. TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
  7684. EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
  7685. EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
  7686. EXPECT_EQ("\"test\"\n"
  7687. "\"\\n\"",
  7688. format("\"test\\n\"", getLLVMStyleWithColumns(7)));
  7689. EXPECT_EQ("\"tes\\\\\"\n"
  7690. "\"n\"",
  7691. format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
  7692. EXPECT_EQ("\"\\\\\\\\\"\n"
  7693. "\"\\n\"",
  7694. format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
  7695. EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
  7696. EXPECT_EQ("\"\\uff01\"\n"
  7697. "\"test\"",
  7698. format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
  7699. EXPECT_EQ("\"\\Uff01ff02\"",
  7700. format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
  7701. EXPECT_EQ("\"\\x000000000001\"\n"
  7702. "\"next\"",
  7703. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
  7704. EXPECT_EQ("\"\\x000000000001next\"",
  7705. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
  7706. EXPECT_EQ("\"\\x000000000001\"",
  7707. format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
  7708. EXPECT_EQ("\"test\"\n"
  7709. "\"\\000000\"\n"
  7710. "\"000001\"",
  7711. format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
  7712. EXPECT_EQ("\"test\\000\"\n"
  7713. "\"00000000\"\n"
  7714. "\"1\"",
  7715. format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
  7716. }
  7717. TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
  7718. verifyFormat("void f() {\n"
  7719. " return g() {}\n"
  7720. " void h() {}");
  7721. verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
  7722. "g();\n"
  7723. "}");
  7724. }
  7725. TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
  7726. verifyFormat(
  7727. "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
  7728. }
  7729. TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
  7730. verifyFormat("class X {\n"
  7731. " void f() {\n"
  7732. " }\n"
  7733. "};",
  7734. getLLVMStyleWithColumns(12));
  7735. }
  7736. TEST_F(FormatTest, ConfigurableIndentWidth) {
  7737. FormatStyle EightIndent = getLLVMStyleWithColumns(18);
  7738. EightIndent.IndentWidth = 8;
  7739. EightIndent.ContinuationIndentWidth = 8;
  7740. verifyFormat("void f() {\n"
  7741. " someFunction();\n"
  7742. " if (true) {\n"
  7743. " f();\n"
  7744. " }\n"
  7745. "}",
  7746. EightIndent);
  7747. verifyFormat("class X {\n"
  7748. " void f() {\n"
  7749. " }\n"
  7750. "};",
  7751. EightIndent);
  7752. verifyFormat("int x[] = {\n"
  7753. " call(),\n"
  7754. " call()};",
  7755. EightIndent);
  7756. }
  7757. TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
  7758. verifyFormat("double\n"
  7759. "f();",
  7760. getLLVMStyleWithColumns(8));
  7761. }
  7762. TEST_F(FormatTest, ConfigurableUseOfTab) {
  7763. FormatStyle Tab = getLLVMStyleWithColumns(42);
  7764. Tab.IndentWidth = 8;
  7765. Tab.UseTab = FormatStyle::UT_Always;
  7766. Tab.AlignEscapedNewlinesLeft = true;
  7767. EXPECT_EQ("if (aaaaaaaa && // q\n"
  7768. " bb)\t\t// w\n"
  7769. "\t;",
  7770. format("if (aaaaaaaa &&// q\n"
  7771. "bb)// w\n"
  7772. ";",
  7773. Tab));
  7774. EXPECT_EQ("if (aaa && bbb) // w\n"
  7775. "\t;",
  7776. format("if(aaa&&bbb)// w\n"
  7777. ";",
  7778. Tab));
  7779. verifyFormat("class X {\n"
  7780. "\tvoid f() {\n"
  7781. "\t\tsomeFunction(parameter1,\n"
  7782. "\t\t\t parameter2);\n"
  7783. "\t}\n"
  7784. "};",
  7785. Tab);
  7786. verifyFormat("#define A \\\n"
  7787. "\tvoid f() { \\\n"
  7788. "\t\tsomeFunction( \\\n"
  7789. "\t\t parameter1, \\\n"
  7790. "\t\t parameter2); \\\n"
  7791. "\t}",
  7792. Tab);
  7793. Tab.TabWidth = 4;
  7794. Tab.IndentWidth = 8;
  7795. verifyFormat("class TabWidth4Indent8 {\n"
  7796. "\t\tvoid f() {\n"
  7797. "\t\t\t\tsomeFunction(parameter1,\n"
  7798. "\t\t\t\t\t\t\t parameter2);\n"
  7799. "\t\t}\n"
  7800. "};",
  7801. Tab);
  7802. Tab.TabWidth = 4;
  7803. Tab.IndentWidth = 4;
  7804. verifyFormat("class TabWidth4Indent4 {\n"
  7805. "\tvoid f() {\n"
  7806. "\t\tsomeFunction(parameter1,\n"
  7807. "\t\t\t\t\t parameter2);\n"
  7808. "\t}\n"
  7809. "};",
  7810. Tab);
  7811. Tab.TabWidth = 8;
  7812. Tab.IndentWidth = 4;
  7813. verifyFormat("class TabWidth8Indent4 {\n"
  7814. " void f() {\n"
  7815. "\tsomeFunction(parameter1,\n"
  7816. "\t\t parameter2);\n"
  7817. " }\n"
  7818. "};",
  7819. Tab);
  7820. Tab.TabWidth = 8;
  7821. Tab.IndentWidth = 8;
  7822. EXPECT_EQ("/*\n"
  7823. "\t a\t\tcomment\n"
  7824. "\t in multiple lines\n"
  7825. " */",
  7826. format(" /*\t \t \n"
  7827. " \t \t a\t\tcomment\t \t\n"
  7828. " \t \t in multiple lines\t\n"
  7829. " \t */",
  7830. Tab));
  7831. Tab.UseTab = FormatStyle::UT_ForIndentation;
  7832. verifyFormat("{\n"
  7833. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7834. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7835. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7836. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7837. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7838. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7839. "};",
  7840. Tab);
  7841. verifyFormat("enum AA {\n"
  7842. "\ta1, // Force multiple lines\n"
  7843. "\ta2,\n"
  7844. "\ta3\n"
  7845. "};",
  7846. Tab);
  7847. EXPECT_EQ("if (aaaaaaaa && // q\n"
  7848. " bb) // w\n"
  7849. "\t;",
  7850. format("if (aaaaaaaa &&// q\n"
  7851. "bb)// w\n"
  7852. ";",
  7853. Tab));
  7854. verifyFormat("class X {\n"
  7855. "\tvoid f() {\n"
  7856. "\t\tsomeFunction(parameter1,\n"
  7857. "\t\t parameter2);\n"
  7858. "\t}\n"
  7859. "};",
  7860. Tab);
  7861. verifyFormat("{\n"
  7862. "\tQ(\n"
  7863. "\t {\n"
  7864. "\t\t int a;\n"
  7865. "\t\t someFunction(aaaaaaaa,\n"
  7866. "\t\t bbbbbbb);\n"
  7867. "\t },\n"
  7868. "\t p);\n"
  7869. "}",
  7870. Tab);
  7871. EXPECT_EQ("{\n"
  7872. "\t/* aaaa\n"
  7873. "\t bbbb */\n"
  7874. "}",
  7875. format("{\n"
  7876. "/* aaaa\n"
  7877. " bbbb */\n"
  7878. "}",
  7879. Tab));
  7880. EXPECT_EQ("{\n"
  7881. "\t/*\n"
  7882. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7883. "\t bbbbbbbbbbbbb\n"
  7884. "\t*/\n"
  7885. "}",
  7886. format("{\n"
  7887. "/*\n"
  7888. " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7889. "*/\n"
  7890. "}",
  7891. Tab));
  7892. EXPECT_EQ("{\n"
  7893. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7894. "\t// bbbbbbbbbbbbb\n"
  7895. "}",
  7896. format("{\n"
  7897. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7898. "}",
  7899. Tab));
  7900. EXPECT_EQ("{\n"
  7901. "\t/*\n"
  7902. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7903. "\t bbbbbbbbbbbbb\n"
  7904. "\t*/\n"
  7905. "}",
  7906. format("{\n"
  7907. "\t/*\n"
  7908. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7909. "\t*/\n"
  7910. "}",
  7911. Tab));
  7912. EXPECT_EQ("{\n"
  7913. "\t/*\n"
  7914. "\n"
  7915. "\t*/\n"
  7916. "}",
  7917. format("{\n"
  7918. "\t/*\n"
  7919. "\n"
  7920. "\t*/\n"
  7921. "}",
  7922. Tab));
  7923. EXPECT_EQ("{\n"
  7924. "\t/*\n"
  7925. " asdf\n"
  7926. "\t*/\n"
  7927. "}",
  7928. format("{\n"
  7929. "\t/*\n"
  7930. " asdf\n"
  7931. "\t*/\n"
  7932. "}",
  7933. Tab));
  7934. Tab.UseTab = FormatStyle::UT_Never;
  7935. EXPECT_EQ("/*\n"
  7936. " a\t\tcomment\n"
  7937. " in multiple lines\n"
  7938. " */",
  7939. format(" /*\t \t \n"
  7940. " \t \t a\t\tcomment\t \t\n"
  7941. " \t \t in multiple lines\t\n"
  7942. " \t */",
  7943. Tab));
  7944. EXPECT_EQ("/* some\n"
  7945. " comment */",
  7946. format(" \t \t /* some\n"
  7947. " \t \t comment */",
  7948. Tab));
  7949. EXPECT_EQ("int a; /* some\n"
  7950. " comment */",
  7951. format(" \t \t int a; /* some\n"
  7952. " \t \t comment */",
  7953. Tab));
  7954. EXPECT_EQ("int a; /* some\n"
  7955. "comment */",
  7956. format(" \t \t int\ta; /* some\n"
  7957. " \t \t comment */",
  7958. Tab));
  7959. EXPECT_EQ("f(\"\t\t\"); /* some\n"
  7960. " comment */",
  7961. format(" \t \t f(\"\t\t\"); /* some\n"
  7962. " \t \t comment */",
  7963. Tab));
  7964. EXPECT_EQ("{\n"
  7965. " /*\n"
  7966. " * Comment\n"
  7967. " */\n"
  7968. " int i;\n"
  7969. "}",
  7970. format("{\n"
  7971. "\t/*\n"
  7972. "\t * Comment\n"
  7973. "\t */\n"
  7974. "\t int i;\n"
  7975. "}"));
  7976. Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
  7977. Tab.TabWidth = 8;
  7978. Tab.IndentWidth = 8;
  7979. EXPECT_EQ("if (aaaaaaaa && // q\n"
  7980. " bb) // w\n"
  7981. "\t;",
  7982. format("if (aaaaaaaa &&// q\n"
  7983. "bb)// w\n"
  7984. ";",
  7985. Tab));
  7986. EXPECT_EQ("if (aaa && bbb) // w\n"
  7987. "\t;",
  7988. format("if(aaa&&bbb)// w\n"
  7989. ";",
  7990. Tab));
  7991. verifyFormat("class X {\n"
  7992. "\tvoid f() {\n"
  7993. "\t\tsomeFunction(parameter1,\n"
  7994. "\t\t\t parameter2);\n"
  7995. "\t}\n"
  7996. "};",
  7997. Tab);
  7998. verifyFormat("#define A \\\n"
  7999. "\tvoid f() { \\\n"
  8000. "\t\tsomeFunction( \\\n"
  8001. "\t\t parameter1, \\\n"
  8002. "\t\t parameter2); \\\n"
  8003. "\t}",
  8004. Tab);
  8005. Tab.TabWidth = 4;
  8006. Tab.IndentWidth = 8;
  8007. verifyFormat("class TabWidth4Indent8 {\n"
  8008. "\t\tvoid f() {\n"
  8009. "\t\t\t\tsomeFunction(parameter1,\n"
  8010. "\t\t\t\t\t\t\t parameter2);\n"
  8011. "\t\t}\n"
  8012. "};",
  8013. Tab);
  8014. Tab.TabWidth = 4;
  8015. Tab.IndentWidth = 4;
  8016. verifyFormat("class TabWidth4Indent4 {\n"
  8017. "\tvoid f() {\n"
  8018. "\t\tsomeFunction(parameter1,\n"
  8019. "\t\t\t\t\t parameter2);\n"
  8020. "\t}\n"
  8021. "};",
  8022. Tab);
  8023. Tab.TabWidth = 8;
  8024. Tab.IndentWidth = 4;
  8025. verifyFormat("class TabWidth8Indent4 {\n"
  8026. " void f() {\n"
  8027. "\tsomeFunction(parameter1,\n"
  8028. "\t\t parameter2);\n"
  8029. " }\n"
  8030. "};",
  8031. Tab);
  8032. Tab.TabWidth = 8;
  8033. Tab.IndentWidth = 8;
  8034. EXPECT_EQ("/*\n"
  8035. "\t a\t\tcomment\n"
  8036. "\t in multiple lines\n"
  8037. " */",
  8038. format(" /*\t \t \n"
  8039. " \t \t a\t\tcomment\t \t\n"
  8040. " \t \t in multiple lines\t\n"
  8041. " \t */",
  8042. Tab));
  8043. verifyFormat("{\n"
  8044. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  8045. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  8046. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  8047. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  8048. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  8049. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  8050. "};",
  8051. Tab);
  8052. verifyFormat("enum AA {\n"
  8053. "\ta1, // Force multiple lines\n"
  8054. "\ta2,\n"
  8055. "\ta3\n"
  8056. "};",
  8057. Tab);
  8058. EXPECT_EQ("if (aaaaaaaa && // q\n"
  8059. " bb) // w\n"
  8060. "\t;",
  8061. format("if (aaaaaaaa &&// q\n"
  8062. "bb)// w\n"
  8063. ";",
  8064. Tab));
  8065. verifyFormat("class X {\n"
  8066. "\tvoid f() {\n"
  8067. "\t\tsomeFunction(parameter1,\n"
  8068. "\t\t\t parameter2);\n"
  8069. "\t}\n"
  8070. "};",
  8071. Tab);
  8072. verifyFormat("{\n"
  8073. "\tQ(\n"
  8074. "\t {\n"
  8075. "\t\t int a;\n"
  8076. "\t\t someFunction(aaaaaaaa,\n"
  8077. "\t\t\t\t bbbbbbb);\n"
  8078. "\t },\n"
  8079. "\t p);\n"
  8080. "}",
  8081. Tab);
  8082. EXPECT_EQ("{\n"
  8083. "\t/* aaaa\n"
  8084. "\t bbbb */\n"
  8085. "}",
  8086. format("{\n"
  8087. "/* aaaa\n"
  8088. " bbbb */\n"
  8089. "}",
  8090. Tab));
  8091. EXPECT_EQ("{\n"
  8092. "\t/*\n"
  8093. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  8094. "\t bbbbbbbbbbbbb\n"
  8095. "\t*/\n"
  8096. "}",
  8097. format("{\n"
  8098. "/*\n"
  8099. " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  8100. "*/\n"
  8101. "}",
  8102. Tab));
  8103. EXPECT_EQ("{\n"
  8104. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  8105. "\t// bbbbbbbbbbbbb\n"
  8106. "}",
  8107. format("{\n"
  8108. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  8109. "}",
  8110. Tab));
  8111. EXPECT_EQ("{\n"
  8112. "\t/*\n"
  8113. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  8114. "\t bbbbbbbbbbbbb\n"
  8115. "\t*/\n"
  8116. "}",
  8117. format("{\n"
  8118. "\t/*\n"
  8119. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  8120. "\t*/\n"
  8121. "}",
  8122. Tab));
  8123. EXPECT_EQ("{\n"
  8124. "\t/*\n"
  8125. "\n"
  8126. "\t*/\n"
  8127. "}",
  8128. format("{\n"
  8129. "\t/*\n"
  8130. "\n"
  8131. "\t*/\n"
  8132. "}",
  8133. Tab));
  8134. EXPECT_EQ("{\n"
  8135. "\t/*\n"
  8136. " asdf\n"
  8137. "\t*/\n"
  8138. "}",
  8139. format("{\n"
  8140. "\t/*\n"
  8141. " asdf\n"
  8142. "\t*/\n"
  8143. "}",
  8144. Tab));
  8145. EXPECT_EQ("/*\n"
  8146. "\t a\t\tcomment\n"
  8147. "\t in multiple lines\n"
  8148. " */",
  8149. format(" /*\t \t \n"
  8150. " \t \t a\t\tcomment\t \t\n"
  8151. " \t \t in multiple lines\t\n"
  8152. " \t */",
  8153. Tab));
  8154. EXPECT_EQ("/* some\n"
  8155. " comment */",
  8156. format(" \t \t /* some\n"
  8157. " \t \t comment */",
  8158. Tab));
  8159. EXPECT_EQ("int a; /* some\n"
  8160. " comment */",
  8161. format(" \t \t int a; /* some\n"
  8162. " \t \t comment */",
  8163. Tab));
  8164. EXPECT_EQ("int a; /* some\n"
  8165. "comment */",
  8166. format(" \t \t int\ta; /* some\n"
  8167. " \t \t comment */",
  8168. Tab));
  8169. EXPECT_EQ("f(\"\t\t\"); /* some\n"
  8170. " comment */",
  8171. format(" \t \t f(\"\t\t\"); /* some\n"
  8172. " \t \t comment */",
  8173. Tab));
  8174. EXPECT_EQ("{\n"
  8175. " /*\n"
  8176. " * Comment\n"
  8177. " */\n"
  8178. " int i;\n"
  8179. "}",
  8180. format("{\n"
  8181. "\t/*\n"
  8182. "\t * Comment\n"
  8183. "\t */\n"
  8184. "\t int i;\n"
  8185. "}"));
  8186. Tab.AlignConsecutiveAssignments = true;
  8187. Tab.AlignConsecutiveDeclarations = true;
  8188. Tab.TabWidth = 4;
  8189. Tab.IndentWidth = 4;
  8190. verifyFormat("class Assign {\n"
  8191. "\tvoid f() {\n"
  8192. "\t\tint x = 123;\n"
  8193. "\t\tint random = 4;\n"
  8194. "\t\tstd::string alphabet =\n"
  8195. "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
  8196. "\t}\n"
  8197. "};",
  8198. Tab);
  8199. }
  8200. TEST_F(FormatTest, CalculatesOriginalColumn) {
  8201. EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  8202. "q\"; /* some\n"
  8203. " comment */",
  8204. format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  8205. "q\"; /* some\n"
  8206. " comment */",
  8207. getLLVMStyle()));
  8208. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  8209. "/* some\n"
  8210. " comment */",
  8211. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  8212. " /* some\n"
  8213. " comment */",
  8214. getLLVMStyle()));
  8215. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  8216. "qqq\n"
  8217. "/* some\n"
  8218. " comment */",
  8219. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  8220. "qqq\n"
  8221. " /* some\n"
  8222. " comment */",
  8223. getLLVMStyle()));
  8224. EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  8225. "wwww; /* some\n"
  8226. " comment */",
  8227. format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  8228. "wwww; /* some\n"
  8229. " comment */",
  8230. getLLVMStyle()));
  8231. }
  8232. TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
  8233. FormatStyle NoSpace = getLLVMStyle();
  8234. NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
  8235. verifyFormat("while(true)\n"
  8236. " continue;",
  8237. NoSpace);
  8238. verifyFormat("for(;;)\n"
  8239. " continue;",
  8240. NoSpace);
  8241. verifyFormat("if(true)\n"
  8242. " f();\n"
  8243. "else if(true)\n"
  8244. " f();",
  8245. NoSpace);
  8246. verifyFormat("do {\n"
  8247. " do_something();\n"
  8248. "} while(something());",
  8249. NoSpace);
  8250. verifyFormat("switch(x) {\n"
  8251. "default:\n"
  8252. " break;\n"
  8253. "}",
  8254. NoSpace);
  8255. verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
  8256. verifyFormat("size_t x = sizeof(x);", NoSpace);
  8257. verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
  8258. verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
  8259. verifyFormat("alignas(128) char a[128];", NoSpace);
  8260. verifyFormat("size_t x = alignof(MyType);", NoSpace);
  8261. verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
  8262. verifyFormat("int f() throw(Deprecated);", NoSpace);
  8263. verifyFormat("typedef void (*cb)(int);", NoSpace);
  8264. verifyFormat("T A::operator()();", NoSpace);
  8265. verifyFormat("X A::operator++(T);", NoSpace);
  8266. FormatStyle Space = getLLVMStyle();
  8267. Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
  8268. verifyFormat("int f ();", Space);
  8269. verifyFormat("void f (int a, T b) {\n"
  8270. " while (true)\n"
  8271. " continue;\n"
  8272. "}",
  8273. Space);
  8274. verifyFormat("if (true)\n"
  8275. " f ();\n"
  8276. "else if (true)\n"
  8277. " f ();",
  8278. Space);
  8279. verifyFormat("do {\n"
  8280. " do_something ();\n"
  8281. "} while (something ());",
  8282. Space);
  8283. verifyFormat("switch (x) {\n"
  8284. "default:\n"
  8285. " break;\n"
  8286. "}",
  8287. Space);
  8288. verifyFormat("A::A () : a (1) {}", Space);
  8289. verifyFormat("void f () __attribute__ ((asdf));", Space);
  8290. verifyFormat("*(&a + 1);\n"
  8291. "&((&a)[1]);\n"
  8292. "a[(b + c) * d];\n"
  8293. "(((a + 1) * 2) + 3) * 4;",
  8294. Space);
  8295. verifyFormat("#define A(x) x", Space);
  8296. verifyFormat("#define A (x) x", Space);
  8297. verifyFormat("#if defined(x)\n"
  8298. "#endif",
  8299. Space);
  8300. verifyFormat("auto i = std::make_unique<int> (5);", Space);
  8301. verifyFormat("size_t x = sizeof (x);", Space);
  8302. verifyFormat("auto f (int x) -> decltype (x);", Space);
  8303. verifyFormat("int f (T x) noexcept (x.create ());", Space);
  8304. verifyFormat("alignas (128) char a[128];", Space);
  8305. verifyFormat("size_t x = alignof (MyType);", Space);
  8306. verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
  8307. verifyFormat("int f () throw (Deprecated);", Space);
  8308. verifyFormat("typedef void (*cb) (int);", Space);
  8309. verifyFormat("T A::operator() ();", Space);
  8310. verifyFormat("X A::operator++ (T);", Space);
  8311. }
  8312. TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
  8313. FormatStyle Spaces = getLLVMStyle();
  8314. Spaces.SpacesInParentheses = true;
  8315. verifyFormat("call( x, y, z );", Spaces);
  8316. verifyFormat("call();", Spaces);
  8317. verifyFormat("std::function<void( int, int )> callback;", Spaces);
  8318. verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
  8319. Spaces);
  8320. verifyFormat("while ( (bool)1 )\n"
  8321. " continue;",
  8322. Spaces);
  8323. verifyFormat("for ( ;; )\n"
  8324. " continue;",
  8325. Spaces);
  8326. verifyFormat("if ( true )\n"
  8327. " f();\n"
  8328. "else if ( true )\n"
  8329. " f();",
  8330. Spaces);
  8331. verifyFormat("do {\n"
  8332. " do_something( (int)i );\n"
  8333. "} while ( something() );",
  8334. Spaces);
  8335. verifyFormat("switch ( x ) {\n"
  8336. "default:\n"
  8337. " break;\n"
  8338. "}",
  8339. Spaces);
  8340. Spaces.SpacesInParentheses = false;
  8341. Spaces.SpacesInCStyleCastParentheses = true;
  8342. verifyFormat("Type *A = ( Type * )P;", Spaces);
  8343. verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
  8344. verifyFormat("x = ( int32 )y;", Spaces);
  8345. verifyFormat("int a = ( int )(2.0f);", Spaces);
  8346. verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
  8347. verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
  8348. verifyFormat("#define x (( int )-1)", Spaces);
  8349. // Run the first set of tests again with:
  8350. Spaces.SpacesInParentheses = false;
  8351. Spaces.SpaceInEmptyParentheses = true;
  8352. Spaces.SpacesInCStyleCastParentheses = true;
  8353. verifyFormat("call(x, y, z);", Spaces);
  8354. verifyFormat("call( );", Spaces);
  8355. verifyFormat("std::function<void(int, int)> callback;", Spaces);
  8356. verifyFormat("while (( bool )1)\n"
  8357. " continue;",
  8358. Spaces);
  8359. verifyFormat("for (;;)\n"
  8360. " continue;",
  8361. Spaces);
  8362. verifyFormat("if (true)\n"
  8363. " f( );\n"
  8364. "else if (true)\n"
  8365. " f( );",
  8366. Spaces);
  8367. verifyFormat("do {\n"
  8368. " do_something(( int )i);\n"
  8369. "} while (something( ));",
  8370. Spaces);
  8371. verifyFormat("switch (x) {\n"
  8372. "default:\n"
  8373. " break;\n"
  8374. "}",
  8375. Spaces);
  8376. // Run the first set of tests again with:
  8377. Spaces.SpaceAfterCStyleCast = true;
  8378. verifyFormat("call(x, y, z);", Spaces);
  8379. verifyFormat("call( );", Spaces);
  8380. verifyFormat("std::function<void(int, int)> callback;", Spaces);
  8381. verifyFormat("while (( bool ) 1)\n"
  8382. " continue;",
  8383. Spaces);
  8384. verifyFormat("for (;;)\n"
  8385. " continue;",
  8386. Spaces);
  8387. verifyFormat("if (true)\n"
  8388. " f( );\n"
  8389. "else if (true)\n"
  8390. " f( );",
  8391. Spaces);
  8392. verifyFormat("do {\n"
  8393. " do_something(( int ) i);\n"
  8394. "} while (something( ));",
  8395. Spaces);
  8396. verifyFormat("switch (x) {\n"
  8397. "default:\n"
  8398. " break;\n"
  8399. "}",
  8400. Spaces);
  8401. // Run subset of tests again with:
  8402. Spaces.SpacesInCStyleCastParentheses = false;
  8403. Spaces.SpaceAfterCStyleCast = true;
  8404. verifyFormat("while ((bool) 1)\n"
  8405. " continue;",
  8406. Spaces);
  8407. verifyFormat("do {\n"
  8408. " do_something((int) i);\n"
  8409. "} while (something( ));",
  8410. Spaces);
  8411. }
  8412. TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
  8413. verifyFormat("int a[5];");
  8414. verifyFormat("a[3] += 42;");
  8415. FormatStyle Spaces = getLLVMStyle();
  8416. Spaces.SpacesInSquareBrackets = true;
  8417. // Lambdas unchanged.
  8418. verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
  8419. verifyFormat("return [i, args...] {};", Spaces);
  8420. // Not lambdas.
  8421. verifyFormat("int a[ 5 ];", Spaces);
  8422. verifyFormat("a[ 3 ] += 42;", Spaces);
  8423. verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
  8424. verifyFormat("double &operator[](int i) { return 0; }\n"
  8425. "int i;",
  8426. Spaces);
  8427. verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
  8428. verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
  8429. verifyFormat("int i = (*b)[ a ]->f();", Spaces);
  8430. }
  8431. TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
  8432. verifyFormat("int a = 5;");
  8433. verifyFormat("a += 42;");
  8434. verifyFormat("a or_eq 8;");
  8435. FormatStyle Spaces = getLLVMStyle();
  8436. Spaces.SpaceBeforeAssignmentOperators = false;
  8437. verifyFormat("int a= 5;", Spaces);
  8438. verifyFormat("a+= 42;", Spaces);
  8439. verifyFormat("a or_eq 8;", Spaces);
  8440. }
  8441. TEST_F(FormatTest, AlignConsecutiveAssignments) {
  8442. FormatStyle Alignment = getLLVMStyle();
  8443. Alignment.AlignConsecutiveAssignments = false;
  8444. verifyFormat("int a = 5;\n"
  8445. "int oneTwoThree = 123;",
  8446. Alignment);
  8447. verifyFormat("int a = 5;\n"
  8448. "int oneTwoThree = 123;",
  8449. Alignment);
  8450. Alignment.AlignConsecutiveAssignments = true;
  8451. verifyFormat("int a = 5;\n"
  8452. "int oneTwoThree = 123;",
  8453. Alignment);
  8454. verifyFormat("int a = method();\n"
  8455. "int oneTwoThree = 133;",
  8456. Alignment);
  8457. verifyFormat("a &= 5;\n"
  8458. "bcd *= 5;\n"
  8459. "ghtyf += 5;\n"
  8460. "dvfvdb -= 5;\n"
  8461. "a /= 5;\n"
  8462. "vdsvsv %= 5;\n"
  8463. "sfdbddfbdfbb ^= 5;\n"
  8464. "dvsdsv |= 5;\n"
  8465. "int dsvvdvsdvvv = 123;",
  8466. Alignment);
  8467. verifyFormat("int i = 1, j = 10;\n"
  8468. "something = 2000;",
  8469. Alignment);
  8470. verifyFormat("something = 2000;\n"
  8471. "int i = 1, j = 10;\n",
  8472. Alignment);
  8473. verifyFormat("something = 2000;\n"
  8474. "another = 911;\n"
  8475. "int i = 1, j = 10;\n"
  8476. "oneMore = 1;\n"
  8477. "i = 2;",
  8478. Alignment);
  8479. verifyFormat("int a = 5;\n"
  8480. "int one = 1;\n"
  8481. "method();\n"
  8482. "int oneTwoThree = 123;\n"
  8483. "int oneTwo = 12;",
  8484. Alignment);
  8485. verifyFormat("int oneTwoThree = 123;\n"
  8486. "int oneTwo = 12;\n"
  8487. "method();\n",
  8488. Alignment);
  8489. verifyFormat("int oneTwoThree = 123; // comment\n"
  8490. "int oneTwo = 12; // comment",
  8491. Alignment);
  8492. EXPECT_EQ("int a = 5;\n"
  8493. "\n"
  8494. "int oneTwoThree = 123;",
  8495. format("int a = 5;\n"
  8496. "\n"
  8497. "int oneTwoThree= 123;",
  8498. Alignment));
  8499. EXPECT_EQ("int a = 5;\n"
  8500. "int one = 1;\n"
  8501. "\n"
  8502. "int oneTwoThree = 123;",
  8503. format("int a = 5;\n"
  8504. "int one = 1;\n"
  8505. "\n"
  8506. "int oneTwoThree = 123;",
  8507. Alignment));
  8508. EXPECT_EQ("int a = 5;\n"
  8509. "int one = 1;\n"
  8510. "\n"
  8511. "int oneTwoThree = 123;\n"
  8512. "int oneTwo = 12;",
  8513. format("int a = 5;\n"
  8514. "int one = 1;\n"
  8515. "\n"
  8516. "int oneTwoThree = 123;\n"
  8517. "int oneTwo = 12;",
  8518. Alignment));
  8519. Alignment.AlignEscapedNewlinesLeft = true;
  8520. verifyFormat("#define A \\\n"
  8521. " int aaaa = 12; \\\n"
  8522. " int b = 23; \\\n"
  8523. " int ccc = 234; \\\n"
  8524. " int dddddddddd = 2345;",
  8525. Alignment);
  8526. Alignment.AlignEscapedNewlinesLeft = false;
  8527. verifyFormat("#define A "
  8528. " \\\n"
  8529. " int aaaa = 12; "
  8530. " \\\n"
  8531. " int b = 23; "
  8532. " \\\n"
  8533. " int ccc = 234; "
  8534. " \\\n"
  8535. " int dddddddddd = 2345;",
  8536. Alignment);
  8537. verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
  8538. "k = 4, int l = 5,\n"
  8539. " int m = 6) {\n"
  8540. " int j = 10;\n"
  8541. " otherThing = 1;\n"
  8542. "}",
  8543. Alignment);
  8544. verifyFormat("void SomeFunction(int parameter = 0) {\n"
  8545. " int i = 1;\n"
  8546. " int j = 2;\n"
  8547. " int big = 10000;\n"
  8548. "}",
  8549. Alignment);
  8550. verifyFormat("class C {\n"
  8551. "public:\n"
  8552. " int i = 1;\n"
  8553. " virtual void f() = 0;\n"
  8554. "};",
  8555. Alignment);
  8556. verifyFormat("int i = 1;\n"
  8557. "if (SomeType t = getSomething()) {\n"
  8558. "}\n"
  8559. "int j = 2;\n"
  8560. "int big = 10000;",
  8561. Alignment);
  8562. verifyFormat("int j = 7;\n"
  8563. "for (int k = 0; k < N; ++k) {\n"
  8564. "}\n"
  8565. "int j = 2;\n"
  8566. "int big = 10000;\n"
  8567. "}",
  8568. Alignment);
  8569. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  8570. verifyFormat("int i = 1;\n"
  8571. "LooooooooooongType loooooooooooooooooooooongVariable\n"
  8572. " = someLooooooooooooooooongFunction();\n"
  8573. "int j = 2;",
  8574. Alignment);
  8575. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
  8576. verifyFormat("int i = 1;\n"
  8577. "LooooooooooongType loooooooooooooooooooooongVariable =\n"
  8578. " someLooooooooooooooooongFunction();\n"
  8579. "int j = 2;",
  8580. Alignment);
  8581. verifyFormat("auto lambda = []() {\n"
  8582. " auto i = 0;\n"
  8583. " return 0;\n"
  8584. "};\n"
  8585. "int i = 0;\n"
  8586. "auto v = type{\n"
  8587. " i = 1, //\n"
  8588. " (i = 2), //\n"
  8589. " i = 3 //\n"
  8590. "};",
  8591. Alignment);
  8592. // FIXME: Should align all three assignments
  8593. verifyFormat(
  8594. "int i = 1;\n"
  8595. "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
  8596. " loooooooooooooooooooooongParameterB);\n"
  8597. "int j = 2;",
  8598. Alignment);
  8599. verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
  8600. " typename B = very_long_type_name_1,\n"
  8601. " typename T_2 = very_long_type_name_2>\n"
  8602. "auto foo() {}\n",
  8603. Alignment);
  8604. verifyFormat("int a, b = 1;\n"
  8605. "int c = 2;\n"
  8606. "int dd = 3;\n",
  8607. Alignment);
  8608. verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
  8609. "float b[1][] = {{3.f}};\n",
  8610. Alignment);
  8611. }
  8612. TEST_F(FormatTest, AlignConsecutiveDeclarations) {
  8613. FormatStyle Alignment = getLLVMStyle();
  8614. Alignment.AlignConsecutiveDeclarations = false;
  8615. verifyFormat("float const a = 5;\n"
  8616. "int oneTwoThree = 123;",
  8617. Alignment);
  8618. verifyFormat("int a = 5;\n"
  8619. "float const oneTwoThree = 123;",
  8620. Alignment);
  8621. Alignment.AlignConsecutiveDeclarations = true;
  8622. verifyFormat("float const a = 5;\n"
  8623. "int oneTwoThree = 123;",
  8624. Alignment);
  8625. verifyFormat("int a = method();\n"
  8626. "float const oneTwoThree = 133;",
  8627. Alignment);
  8628. verifyFormat("int i = 1, j = 10;\n"
  8629. "something = 2000;",
  8630. Alignment);
  8631. verifyFormat("something = 2000;\n"
  8632. "int i = 1, j = 10;\n",
  8633. Alignment);
  8634. verifyFormat("float something = 2000;\n"
  8635. "double another = 911;\n"
  8636. "int i = 1, j = 10;\n"
  8637. "const int *oneMore = 1;\n"
  8638. "unsigned i = 2;",
  8639. Alignment);
  8640. verifyFormat("float a = 5;\n"
  8641. "int one = 1;\n"
  8642. "method();\n"
  8643. "const double oneTwoThree = 123;\n"
  8644. "const unsigned int oneTwo = 12;",
  8645. Alignment);
  8646. verifyFormat("int oneTwoThree{0}; // comment\n"
  8647. "unsigned oneTwo; // comment",
  8648. Alignment);
  8649. EXPECT_EQ("float const a = 5;\n"
  8650. "\n"
  8651. "int oneTwoThree = 123;",
  8652. format("float const a = 5;\n"
  8653. "\n"
  8654. "int oneTwoThree= 123;",
  8655. Alignment));
  8656. EXPECT_EQ("float a = 5;\n"
  8657. "int one = 1;\n"
  8658. "\n"
  8659. "unsigned oneTwoThree = 123;",
  8660. format("float a = 5;\n"
  8661. "int one = 1;\n"
  8662. "\n"
  8663. "unsigned oneTwoThree = 123;",
  8664. Alignment));
  8665. EXPECT_EQ("float a = 5;\n"
  8666. "int one = 1;\n"
  8667. "\n"
  8668. "unsigned oneTwoThree = 123;\n"
  8669. "int oneTwo = 12;",
  8670. format("float a = 5;\n"
  8671. "int one = 1;\n"
  8672. "\n"
  8673. "unsigned oneTwoThree = 123;\n"
  8674. "int oneTwo = 12;",
  8675. Alignment));
  8676. Alignment.AlignConsecutiveAssignments = true;
  8677. verifyFormat("float something = 2000;\n"
  8678. "double another = 911;\n"
  8679. "int i = 1, j = 10;\n"
  8680. "const int *oneMore = 1;\n"
  8681. "unsigned i = 2;",
  8682. Alignment);
  8683. verifyFormat("int oneTwoThree = {0}; // comment\n"
  8684. "unsigned oneTwo = 0; // comment",
  8685. Alignment);
  8686. EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
  8687. " int const i = 1;\n"
  8688. " int * j = 2;\n"
  8689. " int big = 10000;\n"
  8690. "\n"
  8691. " unsigned oneTwoThree = 123;\n"
  8692. " int oneTwo = 12;\n"
  8693. " method();\n"
  8694. " float k = 2;\n"
  8695. " int ll = 10000;\n"
  8696. "}",
  8697. format("void SomeFunction(int parameter= 0) {\n"
  8698. " int const i= 1;\n"
  8699. " int *j=2;\n"
  8700. " int big = 10000;\n"
  8701. "\n"
  8702. "unsigned oneTwoThree =123;\n"
  8703. "int oneTwo = 12;\n"
  8704. " method();\n"
  8705. "float k= 2;\n"
  8706. "int ll=10000;\n"
  8707. "}",
  8708. Alignment));
  8709. Alignment.AlignConsecutiveAssignments = false;
  8710. Alignment.AlignEscapedNewlinesLeft = true;
  8711. verifyFormat("#define A \\\n"
  8712. " int aaaa = 12; \\\n"
  8713. " float b = 23; \\\n"
  8714. " const int ccc = 234; \\\n"
  8715. " unsigned dddddddddd = 2345;",
  8716. Alignment);
  8717. Alignment.AlignEscapedNewlinesLeft = false;
  8718. Alignment.ColumnLimit = 30;
  8719. verifyFormat("#define A \\\n"
  8720. " int aaaa = 12; \\\n"
  8721. " float b = 23; \\\n"
  8722. " const int ccc = 234; \\\n"
  8723. " int dddddddddd = 2345;",
  8724. Alignment);
  8725. Alignment.ColumnLimit = 80;
  8726. verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
  8727. "k = 4, int l = 5,\n"
  8728. " int m = 6) {\n"
  8729. " const int j = 10;\n"
  8730. " otherThing = 1;\n"
  8731. "}",
  8732. Alignment);
  8733. verifyFormat("void SomeFunction(int parameter = 0) {\n"
  8734. " int const i = 1;\n"
  8735. " int * j = 2;\n"
  8736. " int big = 10000;\n"
  8737. "}",
  8738. Alignment);
  8739. verifyFormat("class C {\n"
  8740. "public:\n"
  8741. " int i = 1;\n"
  8742. " virtual void f() = 0;\n"
  8743. "};",
  8744. Alignment);
  8745. verifyFormat("float i = 1;\n"
  8746. "if (SomeType t = getSomething()) {\n"
  8747. "}\n"
  8748. "const unsigned j = 2;\n"
  8749. "int big = 10000;",
  8750. Alignment);
  8751. verifyFormat("float j = 7;\n"
  8752. "for (int k = 0; k < N; ++k) {\n"
  8753. "}\n"
  8754. "unsigned j = 2;\n"
  8755. "int big = 10000;\n"
  8756. "}",
  8757. Alignment);
  8758. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  8759. verifyFormat("float i = 1;\n"
  8760. "LooooooooooongType loooooooooooooooooooooongVariable\n"
  8761. " = someLooooooooooooooooongFunction();\n"
  8762. "int j = 2;",
  8763. Alignment);
  8764. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
  8765. verifyFormat("int i = 1;\n"
  8766. "LooooooooooongType loooooooooooooooooooooongVariable =\n"
  8767. " someLooooooooooooooooongFunction();\n"
  8768. "int j = 2;",
  8769. Alignment);
  8770. Alignment.AlignConsecutiveAssignments = true;
  8771. verifyFormat("auto lambda = []() {\n"
  8772. " auto ii = 0;\n"
  8773. " float j = 0;\n"
  8774. " return 0;\n"
  8775. "};\n"
  8776. "int i = 0;\n"
  8777. "float i2 = 0;\n"
  8778. "auto v = type{\n"
  8779. " i = 1, //\n"
  8780. " (i = 2), //\n"
  8781. " i = 3 //\n"
  8782. "};",
  8783. Alignment);
  8784. Alignment.AlignConsecutiveAssignments = false;
  8785. // FIXME: Should align all three declarations
  8786. verifyFormat(
  8787. "int i = 1;\n"
  8788. "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
  8789. " loooooooooooooooooooooongParameterB);\n"
  8790. "int j = 2;",
  8791. Alignment);
  8792. // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
  8793. // We expect declarations and assignments to align, as long as it doesn't
  8794. // exceed the column limit, starting a new alignemnt sequence whenever it
  8795. // happens.
  8796. Alignment.AlignConsecutiveAssignments = true;
  8797. Alignment.ColumnLimit = 30;
  8798. verifyFormat("float ii = 1;\n"
  8799. "unsigned j = 2;\n"
  8800. "int someVerylongVariable = 1;\n"
  8801. "AnotherLongType ll = 123456;\n"
  8802. "VeryVeryLongType k = 2;\n"
  8803. "int myvar = 1;",
  8804. Alignment);
  8805. Alignment.ColumnLimit = 80;
  8806. Alignment.AlignConsecutiveAssignments = false;
  8807. verifyFormat(
  8808. "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
  8809. " typename LongType, typename B>\n"
  8810. "auto foo() {}\n",
  8811. Alignment);
  8812. verifyFormat("float a, b = 1;\n"
  8813. "int c = 2;\n"
  8814. "int dd = 3;\n",
  8815. Alignment);
  8816. verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
  8817. "float b[1][] = {{3.f}};\n",
  8818. Alignment);
  8819. Alignment.AlignConsecutiveAssignments = true;
  8820. verifyFormat("float a, b = 1;\n"
  8821. "int c = 2;\n"
  8822. "int dd = 3;\n",
  8823. Alignment);
  8824. verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
  8825. "float b[1][] = {{3.f}};\n",
  8826. Alignment);
  8827. Alignment.AlignConsecutiveAssignments = false;
  8828. Alignment.ColumnLimit = 30;
  8829. Alignment.BinPackParameters = false;
  8830. verifyFormat("void foo(float a,\n"
  8831. " float b,\n"
  8832. " int c,\n"
  8833. " uint32_t *d) {\n"
  8834. " int * e = 0;\n"
  8835. " float f = 0;\n"
  8836. " double g = 0;\n"
  8837. "}\n"
  8838. "void bar(ino_t a,\n"
  8839. " int b,\n"
  8840. " uint32_t *c,\n"
  8841. " bool d) {}\n",
  8842. Alignment);
  8843. Alignment.BinPackParameters = true;
  8844. Alignment.ColumnLimit = 80;
  8845. }
  8846. TEST_F(FormatTest, LinuxBraceBreaking) {
  8847. FormatStyle LinuxBraceStyle = getLLVMStyle();
  8848. LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
  8849. verifyFormat("namespace a\n"
  8850. "{\n"
  8851. "class A\n"
  8852. "{\n"
  8853. " void f()\n"
  8854. " {\n"
  8855. " if (true) {\n"
  8856. " a();\n"
  8857. " b();\n"
  8858. " } else {\n"
  8859. " a();\n"
  8860. " }\n"
  8861. " }\n"
  8862. " void g() { return; }\n"
  8863. "};\n"
  8864. "struct B {\n"
  8865. " int x;\n"
  8866. "};\n"
  8867. "}\n",
  8868. LinuxBraceStyle);
  8869. verifyFormat("enum X {\n"
  8870. " Y = 0,\n"
  8871. "}\n",
  8872. LinuxBraceStyle);
  8873. verifyFormat("struct S {\n"
  8874. " int Type;\n"
  8875. " union {\n"
  8876. " int x;\n"
  8877. " double y;\n"
  8878. " } Value;\n"
  8879. " class C\n"
  8880. " {\n"
  8881. " MyFavoriteType Value;\n"
  8882. " } Class;\n"
  8883. "}\n",
  8884. LinuxBraceStyle);
  8885. }
  8886. TEST_F(FormatTest, MozillaBraceBreaking) {
  8887. FormatStyle MozillaBraceStyle = getLLVMStyle();
  8888. MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
  8889. verifyFormat("namespace a {\n"
  8890. "class A\n"
  8891. "{\n"
  8892. " void f()\n"
  8893. " {\n"
  8894. " if (true) {\n"
  8895. " a();\n"
  8896. " b();\n"
  8897. " }\n"
  8898. " }\n"
  8899. " void g() { return; }\n"
  8900. "};\n"
  8901. "enum E\n"
  8902. "{\n"
  8903. " A,\n"
  8904. " // foo\n"
  8905. " B,\n"
  8906. " C\n"
  8907. "};\n"
  8908. "struct B\n"
  8909. "{\n"
  8910. " int x;\n"
  8911. "};\n"
  8912. "}\n",
  8913. MozillaBraceStyle);
  8914. verifyFormat("struct S\n"
  8915. "{\n"
  8916. " int Type;\n"
  8917. " union\n"
  8918. " {\n"
  8919. " int x;\n"
  8920. " double y;\n"
  8921. " } Value;\n"
  8922. " class C\n"
  8923. " {\n"
  8924. " MyFavoriteType Value;\n"
  8925. " } Class;\n"
  8926. "}\n",
  8927. MozillaBraceStyle);
  8928. }
  8929. TEST_F(FormatTest, StroustrupBraceBreaking) {
  8930. FormatStyle StroustrupBraceStyle = getLLVMStyle();
  8931. StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  8932. verifyFormat("namespace a {\n"
  8933. "class A {\n"
  8934. " void f()\n"
  8935. " {\n"
  8936. " if (true) {\n"
  8937. " a();\n"
  8938. " b();\n"
  8939. " }\n"
  8940. " }\n"
  8941. " void g() { return; }\n"
  8942. "};\n"
  8943. "struct B {\n"
  8944. " int x;\n"
  8945. "};\n"
  8946. "}\n",
  8947. StroustrupBraceStyle);
  8948. verifyFormat("void foo()\n"
  8949. "{\n"
  8950. " if (a) {\n"
  8951. " a();\n"
  8952. " }\n"
  8953. " else {\n"
  8954. " b();\n"
  8955. " }\n"
  8956. "}\n",
  8957. StroustrupBraceStyle);
  8958. verifyFormat("#ifdef _DEBUG\n"
  8959. "int foo(int i = 0)\n"
  8960. "#else\n"
  8961. "int foo(int i = 5)\n"
  8962. "#endif\n"
  8963. "{\n"
  8964. " return i;\n"
  8965. "}",
  8966. StroustrupBraceStyle);
  8967. verifyFormat("void foo() {}\n"
  8968. "void bar()\n"
  8969. "#ifdef _DEBUG\n"
  8970. "{\n"
  8971. " foo();\n"
  8972. "}\n"
  8973. "#else\n"
  8974. "{\n"
  8975. "}\n"
  8976. "#endif",
  8977. StroustrupBraceStyle);
  8978. verifyFormat("void foobar() { int i = 5; }\n"
  8979. "#ifdef _DEBUG\n"
  8980. "void bar() {}\n"
  8981. "#else\n"
  8982. "void bar() { foobar(); }\n"
  8983. "#endif",
  8984. StroustrupBraceStyle);
  8985. }
  8986. TEST_F(FormatTest, AllmanBraceBreaking) {
  8987. FormatStyle AllmanBraceStyle = getLLVMStyle();
  8988. AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
  8989. verifyFormat("namespace a\n"
  8990. "{\n"
  8991. "class A\n"
  8992. "{\n"
  8993. " void f()\n"
  8994. " {\n"
  8995. " if (true)\n"
  8996. " {\n"
  8997. " a();\n"
  8998. " b();\n"
  8999. " }\n"
  9000. " }\n"
  9001. " void g() { return; }\n"
  9002. "};\n"
  9003. "struct B\n"
  9004. "{\n"
  9005. " int x;\n"
  9006. "};\n"
  9007. "}",
  9008. AllmanBraceStyle);
  9009. verifyFormat("void f()\n"
  9010. "{\n"
  9011. " if (true)\n"
  9012. " {\n"
  9013. " a();\n"
  9014. " }\n"
  9015. " else if (false)\n"
  9016. " {\n"
  9017. " b();\n"
  9018. " }\n"
  9019. " else\n"
  9020. " {\n"
  9021. " c();\n"
  9022. " }\n"
  9023. "}\n",
  9024. AllmanBraceStyle);
  9025. verifyFormat("void f()\n"
  9026. "{\n"
  9027. " for (int i = 0; i < 10; ++i)\n"
  9028. " {\n"
  9029. " a();\n"
  9030. " }\n"
  9031. " while (false)\n"
  9032. " {\n"
  9033. " b();\n"
  9034. " }\n"
  9035. " do\n"
  9036. " {\n"
  9037. " c();\n"
  9038. " } while (false)\n"
  9039. "}\n",
  9040. AllmanBraceStyle);
  9041. verifyFormat("void f(int a)\n"
  9042. "{\n"
  9043. " switch (a)\n"
  9044. " {\n"
  9045. " case 0:\n"
  9046. " break;\n"
  9047. " case 1:\n"
  9048. " {\n"
  9049. " break;\n"
  9050. " }\n"
  9051. " case 2:\n"
  9052. " {\n"
  9053. " }\n"
  9054. " break;\n"
  9055. " default:\n"
  9056. " break;\n"
  9057. " }\n"
  9058. "}\n",
  9059. AllmanBraceStyle);
  9060. verifyFormat("enum X\n"
  9061. "{\n"
  9062. " Y = 0,\n"
  9063. "}\n",
  9064. AllmanBraceStyle);
  9065. verifyFormat("enum X\n"
  9066. "{\n"
  9067. " Y = 0\n"
  9068. "}\n",
  9069. AllmanBraceStyle);
  9070. verifyFormat("@interface BSApplicationController ()\n"
  9071. "{\n"
  9072. "@private\n"
  9073. " id _extraIvar;\n"
  9074. "}\n"
  9075. "@end\n",
  9076. AllmanBraceStyle);
  9077. verifyFormat("#ifdef _DEBUG\n"
  9078. "int foo(int i = 0)\n"
  9079. "#else\n"
  9080. "int foo(int i = 5)\n"
  9081. "#endif\n"
  9082. "{\n"
  9083. " return i;\n"
  9084. "}",
  9085. AllmanBraceStyle);
  9086. verifyFormat("void foo() {}\n"
  9087. "void bar()\n"
  9088. "#ifdef _DEBUG\n"
  9089. "{\n"
  9090. " foo();\n"
  9091. "}\n"
  9092. "#else\n"
  9093. "{\n"
  9094. "}\n"
  9095. "#endif",
  9096. AllmanBraceStyle);
  9097. verifyFormat("void foobar() { int i = 5; }\n"
  9098. "#ifdef _DEBUG\n"
  9099. "void bar() {}\n"
  9100. "#else\n"
  9101. "void bar() { foobar(); }\n"
  9102. "#endif",
  9103. AllmanBraceStyle);
  9104. // This shouldn't affect ObjC blocks..
  9105. verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
  9106. " // ...\n"
  9107. " int i;\n"
  9108. "}];",
  9109. AllmanBraceStyle);
  9110. verifyFormat("void (^block)(void) = ^{\n"
  9111. " // ...\n"
  9112. " int i;\n"
  9113. "};",
  9114. AllmanBraceStyle);
  9115. // .. or dict literals.
  9116. verifyFormat("void f()\n"
  9117. "{\n"
  9118. " [object someMethod:@{ @\"a\" : @\"b\" }];\n"
  9119. "}",
  9120. AllmanBraceStyle);
  9121. verifyFormat("int f()\n"
  9122. "{ // comment\n"
  9123. " return 42;\n"
  9124. "}",
  9125. AllmanBraceStyle);
  9126. AllmanBraceStyle.ColumnLimit = 19;
  9127. verifyFormat("void f() { int i; }", AllmanBraceStyle);
  9128. AllmanBraceStyle.ColumnLimit = 18;
  9129. verifyFormat("void f()\n"
  9130. "{\n"
  9131. " int i;\n"
  9132. "}",
  9133. AllmanBraceStyle);
  9134. AllmanBraceStyle.ColumnLimit = 80;
  9135. FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
  9136. BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
  9137. BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
  9138. verifyFormat("void f(bool b)\n"
  9139. "{\n"
  9140. " if (b)\n"
  9141. " {\n"
  9142. " return;\n"
  9143. " }\n"
  9144. "}\n",
  9145. BreakBeforeBraceShortIfs);
  9146. verifyFormat("void f(bool b)\n"
  9147. "{\n"
  9148. " if (b) return;\n"
  9149. "}\n",
  9150. BreakBeforeBraceShortIfs);
  9151. verifyFormat("void f(bool b)\n"
  9152. "{\n"
  9153. " while (b)\n"
  9154. " {\n"
  9155. " return;\n"
  9156. " }\n"
  9157. "}\n",
  9158. BreakBeforeBraceShortIfs);
  9159. }
  9160. TEST_F(FormatTest, GNUBraceBreaking) {
  9161. FormatStyle GNUBraceStyle = getLLVMStyle();
  9162. GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
  9163. verifyFormat("namespace a\n"
  9164. "{\n"
  9165. "class A\n"
  9166. "{\n"
  9167. " void f()\n"
  9168. " {\n"
  9169. " int a;\n"
  9170. " {\n"
  9171. " int b;\n"
  9172. " }\n"
  9173. " if (true)\n"
  9174. " {\n"
  9175. " a();\n"
  9176. " b();\n"
  9177. " }\n"
  9178. " }\n"
  9179. " void g() { return; }\n"
  9180. "}\n"
  9181. "}",
  9182. GNUBraceStyle);
  9183. verifyFormat("void f()\n"
  9184. "{\n"
  9185. " if (true)\n"
  9186. " {\n"
  9187. " a();\n"
  9188. " }\n"
  9189. " else if (false)\n"
  9190. " {\n"
  9191. " b();\n"
  9192. " }\n"
  9193. " else\n"
  9194. " {\n"
  9195. " c();\n"
  9196. " }\n"
  9197. "}\n",
  9198. GNUBraceStyle);
  9199. verifyFormat("void f()\n"
  9200. "{\n"
  9201. " for (int i = 0; i < 10; ++i)\n"
  9202. " {\n"
  9203. " a();\n"
  9204. " }\n"
  9205. " while (false)\n"
  9206. " {\n"
  9207. " b();\n"
  9208. " }\n"
  9209. " do\n"
  9210. " {\n"
  9211. " c();\n"
  9212. " }\n"
  9213. " while (false);\n"
  9214. "}\n",
  9215. GNUBraceStyle);
  9216. verifyFormat("void f(int a)\n"
  9217. "{\n"
  9218. " switch (a)\n"
  9219. " {\n"
  9220. " case 0:\n"
  9221. " break;\n"
  9222. " case 1:\n"
  9223. " {\n"
  9224. " break;\n"
  9225. " }\n"
  9226. " case 2:\n"
  9227. " {\n"
  9228. " }\n"
  9229. " break;\n"
  9230. " default:\n"
  9231. " break;\n"
  9232. " }\n"
  9233. "}\n",
  9234. GNUBraceStyle);
  9235. verifyFormat("enum X\n"
  9236. "{\n"
  9237. " Y = 0,\n"
  9238. "}\n",
  9239. GNUBraceStyle);
  9240. verifyFormat("@interface BSApplicationController ()\n"
  9241. "{\n"
  9242. "@private\n"
  9243. " id _extraIvar;\n"
  9244. "}\n"
  9245. "@end\n",
  9246. GNUBraceStyle);
  9247. verifyFormat("#ifdef _DEBUG\n"
  9248. "int foo(int i = 0)\n"
  9249. "#else\n"
  9250. "int foo(int i = 5)\n"
  9251. "#endif\n"
  9252. "{\n"
  9253. " return i;\n"
  9254. "}",
  9255. GNUBraceStyle);
  9256. verifyFormat("void foo() {}\n"
  9257. "void bar()\n"
  9258. "#ifdef _DEBUG\n"
  9259. "{\n"
  9260. " foo();\n"
  9261. "}\n"
  9262. "#else\n"
  9263. "{\n"
  9264. "}\n"
  9265. "#endif",
  9266. GNUBraceStyle);
  9267. verifyFormat("void foobar() { int i = 5; }\n"
  9268. "#ifdef _DEBUG\n"
  9269. "void bar() {}\n"
  9270. "#else\n"
  9271. "void bar() { foobar(); }\n"
  9272. "#endif",
  9273. GNUBraceStyle);
  9274. }
  9275. TEST_F(FormatTest, WebKitBraceBreaking) {
  9276. FormatStyle WebKitBraceStyle = getLLVMStyle();
  9277. WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
  9278. verifyFormat("namespace a {\n"
  9279. "class A {\n"
  9280. " void f()\n"
  9281. " {\n"
  9282. " if (true) {\n"
  9283. " a();\n"
  9284. " b();\n"
  9285. " }\n"
  9286. " }\n"
  9287. " void g() { return; }\n"
  9288. "};\n"
  9289. "enum E {\n"
  9290. " A,\n"
  9291. " // foo\n"
  9292. " B,\n"
  9293. " C\n"
  9294. "};\n"
  9295. "struct B {\n"
  9296. " int x;\n"
  9297. "};\n"
  9298. "}\n",
  9299. WebKitBraceStyle);
  9300. verifyFormat("struct S {\n"
  9301. " int Type;\n"
  9302. " union {\n"
  9303. " int x;\n"
  9304. " double y;\n"
  9305. " } Value;\n"
  9306. " class C {\n"
  9307. " MyFavoriteType Value;\n"
  9308. " } Class;\n"
  9309. "};\n",
  9310. WebKitBraceStyle);
  9311. }
  9312. TEST_F(FormatTest, CatchExceptionReferenceBinding) {
  9313. verifyFormat("void f() {\n"
  9314. " try {\n"
  9315. " } catch (const Exception &e) {\n"
  9316. " }\n"
  9317. "}\n",
  9318. getLLVMStyle());
  9319. }
  9320. TEST_F(FormatTest, UnderstandsPragmas) {
  9321. verifyFormat("#pragma omp reduction(| : var)");
  9322. verifyFormat("#pragma omp reduction(+ : var)");
  9323. EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
  9324. "(including parentheses).",
  9325. format("#pragma mark Any non-hyphenated or hyphenated string "
  9326. "(including parentheses)."));
  9327. }
  9328. TEST_F(FormatTest, UnderstandPragmaOption) {
  9329. verifyFormat("#pragma option -C -A");
  9330. EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A"));
  9331. }
  9332. #define EXPECT_ALL_STYLES_EQUAL(Styles) \
  9333. for (size_t i = 1; i < Styles.size(); ++i) \
  9334. EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
  9335. << " differs from Style #0"
  9336. TEST_F(FormatTest, GetsPredefinedStyleByName) {
  9337. SmallVector<FormatStyle, 3> Styles;
  9338. Styles.resize(3);
  9339. Styles[0] = getLLVMStyle();
  9340. EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
  9341. EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
  9342. EXPECT_ALL_STYLES_EQUAL(Styles);
  9343. Styles[0] = getGoogleStyle();
  9344. EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
  9345. EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
  9346. EXPECT_ALL_STYLES_EQUAL(Styles);
  9347. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  9348. EXPECT_TRUE(
  9349. getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
  9350. EXPECT_TRUE(
  9351. getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
  9352. EXPECT_ALL_STYLES_EQUAL(Styles);
  9353. Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
  9354. EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
  9355. EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
  9356. EXPECT_ALL_STYLES_EQUAL(Styles);
  9357. Styles[0] = getMozillaStyle();
  9358. EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
  9359. EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
  9360. EXPECT_ALL_STYLES_EQUAL(Styles);
  9361. Styles[0] = getWebKitStyle();
  9362. EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
  9363. EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
  9364. EXPECT_ALL_STYLES_EQUAL(Styles);
  9365. Styles[0] = getGNUStyle();
  9366. EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
  9367. EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
  9368. EXPECT_ALL_STYLES_EQUAL(Styles);
  9369. EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
  9370. }
  9371. TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
  9372. SmallVector<FormatStyle, 8> Styles;
  9373. Styles.resize(2);
  9374. Styles[0] = getGoogleStyle();
  9375. Styles[1] = getLLVMStyle();
  9376. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  9377. EXPECT_ALL_STYLES_EQUAL(Styles);
  9378. Styles.resize(5);
  9379. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  9380. Styles[1] = getLLVMStyle();
  9381. Styles[1].Language = FormatStyle::LK_JavaScript;
  9382. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  9383. Styles[2] = getLLVMStyle();
  9384. Styles[2].Language = FormatStyle::LK_JavaScript;
  9385. EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
  9386. "BasedOnStyle: Google",
  9387. &Styles[2])
  9388. .value());
  9389. Styles[3] = getLLVMStyle();
  9390. Styles[3].Language = FormatStyle::LK_JavaScript;
  9391. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
  9392. "Language: JavaScript",
  9393. &Styles[3])
  9394. .value());
  9395. Styles[4] = getLLVMStyle();
  9396. Styles[4].Language = FormatStyle::LK_JavaScript;
  9397. EXPECT_EQ(0, parseConfiguration("---\n"
  9398. "BasedOnStyle: LLVM\n"
  9399. "IndentWidth: 123\n"
  9400. "---\n"
  9401. "BasedOnStyle: Google\n"
  9402. "Language: JavaScript",
  9403. &Styles[4])
  9404. .value());
  9405. EXPECT_ALL_STYLES_EQUAL(Styles);
  9406. }
  9407. #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \
  9408. Style.FIELD = false; \
  9409. EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \
  9410. EXPECT_TRUE(Style.FIELD); \
  9411. EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \
  9412. EXPECT_FALSE(Style.FIELD);
  9413. #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
  9414. #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \
  9415. Style.STRUCT.FIELD = false; \
  9416. EXPECT_EQ(0, \
  9417. parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \
  9418. .value()); \
  9419. EXPECT_TRUE(Style.STRUCT.FIELD); \
  9420. EXPECT_EQ(0, \
  9421. parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \
  9422. .value()); \
  9423. EXPECT_FALSE(Style.STRUCT.FIELD);
  9424. #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \
  9425. CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
  9426. #define CHECK_PARSE(TEXT, FIELD, VALUE) \
  9427. EXPECT_NE(VALUE, Style.FIELD); \
  9428. EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \
  9429. EXPECT_EQ(VALUE, Style.FIELD)
  9430. TEST_F(FormatTest, ParsesConfigurationBools) {
  9431. FormatStyle Style = {};
  9432. Style.Language = FormatStyle::LK_Cpp;
  9433. CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
  9434. CHECK_PARSE_BOOL(AlignOperands);
  9435. CHECK_PARSE_BOOL(AlignTrailingComments);
  9436. CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
  9437. CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
  9438. CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
  9439. CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
  9440. CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
  9441. CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
  9442. CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
  9443. CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
  9444. CHECK_PARSE_BOOL(BinPackArguments);
  9445. CHECK_PARSE_BOOL(BinPackParameters);
  9446. CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
  9447. CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
  9448. CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
  9449. CHECK_PARSE_BOOL(BreakStringLiterals);
  9450. CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
  9451. CHECK_PARSE_BOOL(DerivePointerAlignment);
  9452. CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
  9453. CHECK_PARSE_BOOL(DisableFormat);
  9454. CHECK_PARSE_BOOL(IndentCaseLabels);
  9455. CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
  9456. CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
  9457. CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
  9458. CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
  9459. CHECK_PARSE_BOOL(Cpp11BracedListStyle);
  9460. CHECK_PARSE_BOOL(ReflowComments);
  9461. CHECK_PARSE_BOOL(SortIncludes);
  9462. CHECK_PARSE_BOOL(SpacesInParentheses);
  9463. CHECK_PARSE_BOOL(SpacesInSquareBrackets);
  9464. CHECK_PARSE_BOOL(SpacesInAngles);
  9465. CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
  9466. CHECK_PARSE_BOOL(SpacesInContainerLiterals);
  9467. CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
  9468. CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
  9469. CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
  9470. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
  9471. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
  9472. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
  9473. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
  9474. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
  9475. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
  9476. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
  9477. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
  9478. CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
  9479. CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
  9480. CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
  9481. }
  9482. #undef CHECK_PARSE_BOOL
  9483. TEST_F(FormatTest, ParsesConfiguration) {
  9484. FormatStyle Style = {};
  9485. Style.Language = FormatStyle::LK_Cpp;
  9486. CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
  9487. CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
  9488. ConstructorInitializerIndentWidth, 1234u);
  9489. CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
  9490. CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
  9491. CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
  9492. CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
  9493. PenaltyBreakBeforeFirstCallParameter, 1234u);
  9494. CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
  9495. CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
  9496. PenaltyReturnTypeOnItsOwnLine, 1234u);
  9497. CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
  9498. SpacesBeforeTrailingComments, 1234u);
  9499. CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
  9500. CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
  9501. CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
  9502. Style.PointerAlignment = FormatStyle::PAS_Middle;
  9503. CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
  9504. FormatStyle::PAS_Left);
  9505. CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
  9506. FormatStyle::PAS_Right);
  9507. CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
  9508. FormatStyle::PAS_Middle);
  9509. // For backward compatibility:
  9510. CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
  9511. FormatStyle::PAS_Left);
  9512. CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
  9513. FormatStyle::PAS_Right);
  9514. CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
  9515. FormatStyle::PAS_Middle);
  9516. Style.Standard = FormatStyle::LS_Auto;
  9517. CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
  9518. CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
  9519. CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
  9520. CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
  9521. CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
  9522. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  9523. CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
  9524. BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
  9525. CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
  9526. FormatStyle::BOS_None);
  9527. CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
  9528. FormatStyle::BOS_All);
  9529. // For backward compatibility:
  9530. CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
  9531. FormatStyle::BOS_None);
  9532. CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
  9533. FormatStyle::BOS_All);
  9534. Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
  9535. CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
  9536. FormatStyle::BAS_Align);
  9537. CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
  9538. FormatStyle::BAS_DontAlign);
  9539. CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
  9540. FormatStyle::BAS_AlwaysBreak);
  9541. // For backward compatibility:
  9542. CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
  9543. FormatStyle::BAS_DontAlign);
  9544. CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
  9545. FormatStyle::BAS_Align);
  9546. Style.UseTab = FormatStyle::UT_ForIndentation;
  9547. CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
  9548. CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
  9549. CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
  9550. CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
  9551. FormatStyle::UT_ForContinuationAndIndentation);
  9552. // For backward compatibility:
  9553. CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
  9554. CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
  9555. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  9556. CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
  9557. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
  9558. CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
  9559. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
  9560. CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
  9561. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
  9562. CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
  9563. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
  9564. // For backward compatibility:
  9565. CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
  9566. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
  9567. CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
  9568. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
  9569. Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
  9570. CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
  9571. FormatStyle::SBPO_Never);
  9572. CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
  9573. FormatStyle::SBPO_Always);
  9574. CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
  9575. FormatStyle::SBPO_ControlStatements);
  9576. // For backward compatibility:
  9577. CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
  9578. FormatStyle::SBPO_Never);
  9579. CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
  9580. FormatStyle::SBPO_ControlStatements);
  9581. Style.ColumnLimit = 123;
  9582. FormatStyle BaseStyle = getLLVMStyle();
  9583. CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
  9584. CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
  9585. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  9586. CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
  9587. FormatStyle::BS_Attach);
  9588. CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
  9589. FormatStyle::BS_Linux);
  9590. CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
  9591. FormatStyle::BS_Mozilla);
  9592. CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
  9593. FormatStyle::BS_Stroustrup);
  9594. CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
  9595. FormatStyle::BS_Allman);
  9596. CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
  9597. CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
  9598. FormatStyle::BS_WebKit);
  9599. CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
  9600. FormatStyle::BS_Custom);
  9601. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
  9602. CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
  9603. FormatStyle::RTBS_None);
  9604. CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
  9605. FormatStyle::RTBS_All);
  9606. CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
  9607. AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
  9608. CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
  9609. AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
  9610. CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
  9611. AlwaysBreakAfterReturnType,
  9612. FormatStyle::RTBS_TopLevelDefinitions);
  9613. Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
  9614. CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
  9615. AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
  9616. CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
  9617. AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
  9618. CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
  9619. AlwaysBreakAfterDefinitionReturnType,
  9620. FormatStyle::DRTBS_TopLevel);
  9621. Style.NamespaceIndentation = FormatStyle::NI_All;
  9622. CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
  9623. FormatStyle::NI_None);
  9624. CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
  9625. FormatStyle::NI_Inner);
  9626. CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
  9627. FormatStyle::NI_All);
  9628. // FIXME: This is required because parsing a configuration simply overwrites
  9629. // the first N elements of the list instead of resetting it.
  9630. Style.ForEachMacros.clear();
  9631. std::vector<std::string> BoostForeach;
  9632. BoostForeach.push_back("BOOST_FOREACH");
  9633. CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
  9634. std::vector<std::string> BoostAndQForeach;
  9635. BoostAndQForeach.push_back("BOOST_FOREACH");
  9636. BoostAndQForeach.push_back("Q_FOREACH");
  9637. CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
  9638. BoostAndQForeach);
  9639. Style.IncludeCategories.clear();
  9640. std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2},
  9641. {".*", 1}};
  9642. CHECK_PARSE("IncludeCategories:\n"
  9643. " - Regex: abc/.*\n"
  9644. " Priority: 2\n"
  9645. " - Regex: .*\n"
  9646. " Priority: 1",
  9647. IncludeCategories, ExpectedCategories);
  9648. CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$");
  9649. }
  9650. TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
  9651. FormatStyle Style = {};
  9652. Style.Language = FormatStyle::LK_Cpp;
  9653. CHECK_PARSE("Language: Cpp\n"
  9654. "IndentWidth: 12",
  9655. IndentWidth, 12u);
  9656. EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
  9657. "IndentWidth: 34",
  9658. &Style),
  9659. ParseError::Unsuitable);
  9660. EXPECT_EQ(12u, Style.IndentWidth);
  9661. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  9662. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  9663. Style.Language = FormatStyle::LK_JavaScript;
  9664. CHECK_PARSE("Language: JavaScript\n"
  9665. "IndentWidth: 12",
  9666. IndentWidth, 12u);
  9667. CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
  9668. EXPECT_EQ(parseConfiguration("Language: Cpp\n"
  9669. "IndentWidth: 34",
  9670. &Style),
  9671. ParseError::Unsuitable);
  9672. EXPECT_EQ(23u, Style.IndentWidth);
  9673. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  9674. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  9675. CHECK_PARSE("BasedOnStyle: LLVM\n"
  9676. "IndentWidth: 67",
  9677. IndentWidth, 67u);
  9678. CHECK_PARSE("---\n"
  9679. "Language: JavaScript\n"
  9680. "IndentWidth: 12\n"
  9681. "---\n"
  9682. "Language: Cpp\n"
  9683. "IndentWidth: 34\n"
  9684. "...\n",
  9685. IndentWidth, 12u);
  9686. Style.Language = FormatStyle::LK_Cpp;
  9687. CHECK_PARSE("---\n"
  9688. "Language: JavaScript\n"
  9689. "IndentWidth: 12\n"
  9690. "---\n"
  9691. "Language: Cpp\n"
  9692. "IndentWidth: 34\n"
  9693. "...\n",
  9694. IndentWidth, 34u);
  9695. CHECK_PARSE("---\n"
  9696. "IndentWidth: 78\n"
  9697. "---\n"
  9698. "Language: JavaScript\n"
  9699. "IndentWidth: 56\n"
  9700. "...\n",
  9701. IndentWidth, 78u);
  9702. Style.ColumnLimit = 123;
  9703. Style.IndentWidth = 234;
  9704. Style.BreakBeforeBraces = FormatStyle::BS_Linux;
  9705. Style.TabWidth = 345;
  9706. EXPECT_FALSE(parseConfiguration("---\n"
  9707. "IndentWidth: 456\n"
  9708. "BreakBeforeBraces: Allman\n"
  9709. "---\n"
  9710. "Language: JavaScript\n"
  9711. "IndentWidth: 111\n"
  9712. "TabWidth: 111\n"
  9713. "---\n"
  9714. "Language: Cpp\n"
  9715. "BreakBeforeBraces: Stroustrup\n"
  9716. "TabWidth: 789\n"
  9717. "...\n",
  9718. &Style));
  9719. EXPECT_EQ(123u, Style.ColumnLimit);
  9720. EXPECT_EQ(456u, Style.IndentWidth);
  9721. EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
  9722. EXPECT_EQ(789u, Style.TabWidth);
  9723. EXPECT_EQ(parseConfiguration("---\n"
  9724. "Language: JavaScript\n"
  9725. "IndentWidth: 56\n"
  9726. "---\n"
  9727. "IndentWidth: 78\n"
  9728. "...\n",
  9729. &Style),
  9730. ParseError::Error);
  9731. EXPECT_EQ(parseConfiguration("---\n"
  9732. "Language: JavaScript\n"
  9733. "IndentWidth: 56\n"
  9734. "---\n"
  9735. "Language: JavaScript\n"
  9736. "IndentWidth: 78\n"
  9737. "...\n",
  9738. &Style),
  9739. ParseError::Error);
  9740. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  9741. }
  9742. #undef CHECK_PARSE
  9743. TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
  9744. FormatStyle Style = {};
  9745. Style.Language = FormatStyle::LK_JavaScript;
  9746. Style.BreakBeforeTernaryOperators = true;
  9747. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
  9748. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  9749. Style.BreakBeforeTernaryOperators = true;
  9750. EXPECT_EQ(0, parseConfiguration("---\n"
  9751. "BasedOnStyle: Google\n"
  9752. "---\n"
  9753. "Language: JavaScript\n"
  9754. "IndentWidth: 76\n"
  9755. "...\n",
  9756. &Style)
  9757. .value());
  9758. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  9759. EXPECT_EQ(76u, Style.IndentWidth);
  9760. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  9761. }
  9762. TEST_F(FormatTest, ConfigurationRoundTripTest) {
  9763. FormatStyle Style = getLLVMStyle();
  9764. std::string YAML = configurationAsText(Style);
  9765. FormatStyle ParsedStyle = {};
  9766. ParsedStyle.Language = FormatStyle::LK_Cpp;
  9767. EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
  9768. EXPECT_EQ(Style, ParsedStyle);
  9769. }
  9770. TEST_F(FormatTest, WorksFor8bitEncodings) {
  9771. EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
  9772. "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
  9773. "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
  9774. "\"\xef\xee\xf0\xf3...\"",
  9775. format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
  9776. "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
  9777. "\xef\xee\xf0\xf3...\"",
  9778. getLLVMStyleWithColumns(12)));
  9779. }
  9780. TEST_F(FormatTest, HandlesUTF8BOM) {
  9781. EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
  9782. EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
  9783. format("\xef\xbb\xbf#include <iostream>"));
  9784. EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
  9785. format("\xef\xbb\xbf\n#include <iostream>"));
  9786. }
  9787. // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
  9788. #if !defined(_MSC_VER)
  9789. TEST_F(FormatTest, CountsUTF8CharactersProperly) {
  9790. verifyFormat("\"Однажды в студёную зимнюю пору...\"",
  9791. getLLVMStyleWithColumns(35));
  9792. verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
  9793. getLLVMStyleWithColumns(31));
  9794. verifyFormat("// Однажды в студёную зимнюю пору...",
  9795. getLLVMStyleWithColumns(36));
  9796. verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
  9797. verifyFormat("/* Однажды в студёную зимнюю пору... */",
  9798. getLLVMStyleWithColumns(39));
  9799. verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
  9800. getLLVMStyleWithColumns(35));
  9801. }
  9802. TEST_F(FormatTest, SplitsUTF8Strings) {
  9803. // Non-printable characters' width is currently considered to be the length in
  9804. // bytes in UTF8. The characters can be displayed in very different manner
  9805. // (zero-width, single width with a substitution glyph, expanded to their code
  9806. // (e.g. "<8d>"), so there's no single correct way to handle them.
  9807. EXPECT_EQ("\"aaaaÄ\"\n"
  9808. "\"\xc2\x8d\";",
  9809. format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  9810. EXPECT_EQ("\"aaaaaaaÄ\"\n"
  9811. "\"\xc2\x8d\";",
  9812. format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  9813. EXPECT_EQ("\"Однажды, в \"\n"
  9814. "\"студёную \"\n"
  9815. "\"зимнюю \"\n"
  9816. "\"пору,\"",
  9817. format("\"Однажды, в студёную зимнюю пору,\"",
  9818. getLLVMStyleWithColumns(13)));
  9819. EXPECT_EQ(
  9820. "\"一 二 三 \"\n"
  9821. "\"四 五六 \"\n"
  9822. "\"七 八 九 \"\n"
  9823. "\"十\"",
  9824. format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
  9825. EXPECT_EQ("\"一\t二 \"\n"
  9826. "\"\t三 \"\n"
  9827. "\"四 五\t六 \"\n"
  9828. "\"\t七 \"\n"
  9829. "\"八九十\tqq\"",
  9830. format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
  9831. getLLVMStyleWithColumns(11)));
  9832. // UTF8 character in an escape sequence.
  9833. EXPECT_EQ("\"aaaaaa\"\n"
  9834. "\"\\\xC2\x8D\"",
  9835. format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
  9836. }
  9837. TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
  9838. EXPECT_EQ("const char *sssss =\n"
  9839. " \"一二三四五六七八\\\n"
  9840. " 九 十\";",
  9841. format("const char *sssss = \"一二三四五六七八\\\n"
  9842. " 九 十\";",
  9843. getLLVMStyleWithColumns(30)));
  9844. }
  9845. TEST_F(FormatTest, SplitsUTF8LineComments) {
  9846. EXPECT_EQ("// aaaaÄ\xc2\x8d",
  9847. format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
  9848. EXPECT_EQ("// Я из лесу\n"
  9849. "// вышел; был\n"
  9850. "// сильный\n"
  9851. "// мороз.",
  9852. format("// Я из лесу вышел; был сильный мороз.",
  9853. getLLVMStyleWithColumns(13)));
  9854. EXPECT_EQ("// 一二三\n"
  9855. "// 四五六七\n"
  9856. "// 八 九\n"
  9857. "// 十",
  9858. format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9)));
  9859. }
  9860. TEST_F(FormatTest, SplitsUTF8BlockComments) {
  9861. EXPECT_EQ("/* Гляжу,\n"
  9862. " * поднимается\n"
  9863. " * медленно в\n"
  9864. " * гору\n"
  9865. " * Лошадка,\n"
  9866. " * везущая\n"
  9867. " * хворосту\n"
  9868. " * воз. */",
  9869. format("/* Гляжу, поднимается медленно в гору\n"
  9870. " * Лошадка, везущая хворосту воз. */",
  9871. getLLVMStyleWithColumns(13)));
  9872. EXPECT_EQ(
  9873. "/* 一二三\n"
  9874. " * 四五六七\n"
  9875. " * 八 九\n"
  9876. " * 十 */",
  9877. format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9)));
  9878. EXPECT_EQ("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
  9879. " * 𝕓𝕪𝕥𝕖\n"
  9880. " * 𝖀𝕿𝕱-𝟠 */",
  9881. format("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12)));
  9882. }
  9883. #endif // _MSC_VER
  9884. TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
  9885. FormatStyle Style = getLLVMStyle();
  9886. Style.ConstructorInitializerIndentWidth = 4;
  9887. verifyFormat(
  9888. "SomeClass::Constructor()\n"
  9889. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  9890. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  9891. Style);
  9892. Style.ConstructorInitializerIndentWidth = 2;
  9893. verifyFormat(
  9894. "SomeClass::Constructor()\n"
  9895. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  9896. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  9897. Style);
  9898. Style.ConstructorInitializerIndentWidth = 0;
  9899. verifyFormat(
  9900. "SomeClass::Constructor()\n"
  9901. ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  9902. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  9903. Style);
  9904. Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
  9905. verifyFormat(
  9906. "SomeLongTemplateVariableName<\n"
  9907. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
  9908. Style);
  9909. verifyFormat(
  9910. "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  9911. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  9912. Style);
  9913. }
  9914. TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
  9915. FormatStyle Style = getLLVMStyle();
  9916. Style.BreakConstructorInitializersBeforeComma = true;
  9917. Style.ConstructorInitializerIndentWidth = 4;
  9918. verifyFormat("SomeClass::Constructor()\n"
  9919. " : a(a)\n"
  9920. " , b(b)\n"
  9921. " , c(c) {}",
  9922. Style);
  9923. verifyFormat("SomeClass::Constructor()\n"
  9924. " : a(a) {}",
  9925. Style);
  9926. Style.ColumnLimit = 0;
  9927. verifyFormat("SomeClass::Constructor()\n"
  9928. " : a(a) {}",
  9929. Style);
  9930. verifyFormat("SomeClass::Constructor() noexcept\n"
  9931. " : a(a) {}",
  9932. Style);
  9933. verifyFormat("SomeClass::Constructor()\n"
  9934. " : a(a)\n"
  9935. " , b(b)\n"
  9936. " , c(c) {}",
  9937. Style);
  9938. verifyFormat("SomeClass::Constructor()\n"
  9939. " : a(a) {\n"
  9940. " foo();\n"
  9941. " bar();\n"
  9942. "}",
  9943. Style);
  9944. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  9945. verifyFormat("SomeClass::Constructor()\n"
  9946. " : a(a)\n"
  9947. " , b(b)\n"
  9948. " , c(c) {\n}",
  9949. Style);
  9950. verifyFormat("SomeClass::Constructor()\n"
  9951. " : a(a) {\n}",
  9952. Style);
  9953. Style.ColumnLimit = 80;
  9954. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
  9955. Style.ConstructorInitializerIndentWidth = 2;
  9956. verifyFormat("SomeClass::Constructor()\n"
  9957. " : a(a)\n"
  9958. " , b(b)\n"
  9959. " , c(c) {}",
  9960. Style);
  9961. Style.ConstructorInitializerIndentWidth = 0;
  9962. verifyFormat("SomeClass::Constructor()\n"
  9963. ": a(a)\n"
  9964. ", b(b)\n"
  9965. ", c(c) {}",
  9966. Style);
  9967. Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  9968. Style.ConstructorInitializerIndentWidth = 4;
  9969. verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
  9970. verifyFormat(
  9971. "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
  9972. Style);
  9973. verifyFormat(
  9974. "SomeClass::Constructor()\n"
  9975. " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
  9976. Style);
  9977. Style.ConstructorInitializerIndentWidth = 4;
  9978. Style.ColumnLimit = 60;
  9979. verifyFormat("SomeClass::Constructor()\n"
  9980. " : aaaaaaaa(aaaaaaaa)\n"
  9981. " , aaaaaaaa(aaaaaaaa)\n"
  9982. " , aaaaaaaa(aaaaaaaa) {}",
  9983. Style);
  9984. }
  9985. TEST_F(FormatTest, Destructors) {
  9986. verifyFormat("void F(int &i) { i.~int(); }");
  9987. verifyFormat("void F(int &i) { i->~int(); }");
  9988. }
  9989. TEST_F(FormatTest, FormatsWithWebKitStyle) {
  9990. FormatStyle Style = getWebKitStyle();
  9991. // Don't indent in outer namespaces.
  9992. verifyFormat("namespace outer {\n"
  9993. "int i;\n"
  9994. "namespace inner {\n"
  9995. " int i;\n"
  9996. "} // namespace inner\n"
  9997. "} // namespace outer\n"
  9998. "namespace other_outer {\n"
  9999. "int i;\n"
  10000. "}",
  10001. Style);
  10002. // Don't indent case labels.
  10003. verifyFormat("switch (variable) {\n"
  10004. "case 1:\n"
  10005. "case 2:\n"
  10006. " doSomething();\n"
  10007. " break;\n"
  10008. "default:\n"
  10009. " ++variable;\n"
  10010. "}",
  10011. Style);
  10012. // Wrap before binary operators.
  10013. EXPECT_EQ("void f()\n"
  10014. "{\n"
  10015. " if (aaaaaaaaaaaaaaaa\n"
  10016. " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
  10017. " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  10018. " return;\n"
  10019. "}",
  10020. format("void f() {\n"
  10021. "if (aaaaaaaaaaaaaaaa\n"
  10022. "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
  10023. "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  10024. "return;\n"
  10025. "}",
  10026. Style));
  10027. // Allow functions on a single line.
  10028. verifyFormat("void f() { return; }", Style);
  10029. // Constructor initializers are formatted one per line with the "," on the
  10030. // new line.
  10031. verifyFormat("Constructor()\n"
  10032. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  10033. " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
  10034. " aaaaaaaaaaaaaa)\n"
  10035. " , aaaaaaaaaaaaaaaaaaaaaaa()\n"
  10036. "{\n"
  10037. "}",
  10038. Style);
  10039. verifyFormat("SomeClass::Constructor()\n"
  10040. " : a(a)\n"
  10041. "{\n"
  10042. "}",
  10043. Style);
  10044. EXPECT_EQ("SomeClass::Constructor()\n"
  10045. " : a(a)\n"
  10046. "{\n"
  10047. "}",
  10048. format("SomeClass::Constructor():a(a){}", Style));
  10049. verifyFormat("SomeClass::Constructor()\n"
  10050. " : a(a)\n"
  10051. " , b(b)\n"
  10052. " , c(c)\n"
  10053. "{\n"
  10054. "}",
  10055. Style);
  10056. verifyFormat("SomeClass::Constructor()\n"
  10057. " : a(a)\n"
  10058. "{\n"
  10059. " foo();\n"
  10060. " bar();\n"
  10061. "}",
  10062. Style);
  10063. // Access specifiers should be aligned left.
  10064. verifyFormat("class C {\n"
  10065. "public:\n"
  10066. " int i;\n"
  10067. "};",
  10068. Style);
  10069. // Do not align comments.
  10070. verifyFormat("int a; // Do not\n"
  10071. "double b; // align comments.",
  10072. Style);
  10073. // Do not align operands.
  10074. EXPECT_EQ("ASSERT(aaaa\n"
  10075. " || bbbb);",
  10076. format("ASSERT ( aaaa\n||bbbb);", Style));
  10077. // Accept input's line breaks.
  10078. EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
  10079. " || bbbbbbbbbbbbbbb) {\n"
  10080. " i++;\n"
  10081. "}",
  10082. format("if (aaaaaaaaaaaaaaa\n"
  10083. "|| bbbbbbbbbbbbbbb) { i++; }",
  10084. Style));
  10085. EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
  10086. " i++;\n"
  10087. "}",
  10088. format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
  10089. // Don't automatically break all macro definitions (llvm.org/PR17842).
  10090. verifyFormat("#define aNumber 10", Style);
  10091. // However, generally keep the line breaks that the user authored.
  10092. EXPECT_EQ("#define aNumber \\\n"
  10093. " 10",
  10094. format("#define aNumber \\\n"
  10095. " 10",
  10096. Style));
  10097. // Keep empty and one-element array literals on a single line.
  10098. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
  10099. " copyItems:YES];",
  10100. format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
  10101. "copyItems:YES];",
  10102. Style));
  10103. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
  10104. " copyItems:YES];",
  10105. format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
  10106. " copyItems:YES];",
  10107. Style));
  10108. // FIXME: This does not seem right, there should be more indentation before
  10109. // the array literal's entries. Nested blocks have the same problem.
  10110. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
  10111. " @\"a\",\n"
  10112. " @\"a\"\n"
  10113. "]\n"
  10114. " copyItems:YES];",
  10115. format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
  10116. " @\"a\",\n"
  10117. " @\"a\"\n"
  10118. " ]\n"
  10119. " copyItems:YES];",
  10120. Style));
  10121. EXPECT_EQ(
  10122. "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
  10123. " copyItems:YES];",
  10124. format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
  10125. " copyItems:YES];",
  10126. Style));
  10127. verifyFormat("[self.a b:c c:d];", Style);
  10128. EXPECT_EQ("[self.a b:c\n"
  10129. " c:d];",
  10130. format("[self.a b:c\n"
  10131. "c:d];",
  10132. Style));
  10133. }
  10134. TEST_F(FormatTest, FormatsLambdas) {
  10135. verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
  10136. verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
  10137. verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
  10138. verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
  10139. verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
  10140. verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
  10141. verifyFormat("void f() {\n"
  10142. " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
  10143. "}\n");
  10144. verifyFormat("void f() {\n"
  10145. " other(x.begin(), //\n"
  10146. " x.end(), //\n"
  10147. " [&](int, int) { return 1; });\n"
  10148. "}\n");
  10149. verifyFormat("SomeFunction([]() { // A cool function...\n"
  10150. " return 43;\n"
  10151. "});");
  10152. EXPECT_EQ("SomeFunction([]() {\n"
  10153. "#define A a\n"
  10154. " return 43;\n"
  10155. "});",
  10156. format("SomeFunction([](){\n"
  10157. "#define A a\n"
  10158. "return 43;\n"
  10159. "});"));
  10160. verifyFormat("void f() {\n"
  10161. " SomeFunction([](decltype(x), A *a) {});\n"
  10162. "}");
  10163. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  10164. " [](const aaaaaaaaaa &a) { return a; });");
  10165. verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
  10166. " SomeOtherFunctioooooooooooooooooooooooooon();\n"
  10167. "});");
  10168. verifyFormat("Constructor()\n"
  10169. " : Field([] { // comment\n"
  10170. " int i;\n"
  10171. " }) {}");
  10172. verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
  10173. " return some_parameter.size();\n"
  10174. "};");
  10175. verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
  10176. " [](const string &s) { return s; };");
  10177. verifyFormat("int i = aaaaaa ? 1 //\n"
  10178. " : [] {\n"
  10179. " return 2; //\n"
  10180. " }();");
  10181. verifyFormat("llvm::errs() << \"number of twos is \"\n"
  10182. " << std::count_if(v.begin(), v.end(), [](int x) {\n"
  10183. " return x == 2; // force break\n"
  10184. " });");
  10185. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n"
  10186. " int iiiiiiiiiiii) {\n"
  10187. " return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n"
  10188. "});",
  10189. getLLVMStyleWithColumns(60));
  10190. verifyFormat("SomeFunction({[&] {\n"
  10191. " // comment\n"
  10192. " },\n"
  10193. " [&] {\n"
  10194. " // comment\n"
  10195. " }});");
  10196. verifyFormat("SomeFunction({[&] {\n"
  10197. " // comment\n"
  10198. "}});");
  10199. verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n"
  10200. " [&]() { return true; },\n"
  10201. " aaaaa aaaaaaaaa);");
  10202. // Lambdas with return types.
  10203. verifyFormat("int c = []() -> int { return 2; }();\n");
  10204. verifyFormat("int c = []() -> int * { return 2; }();\n");
  10205. verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
  10206. verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
  10207. verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
  10208. verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
  10209. verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
  10210. verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
  10211. verifyFormat("[a, a]() -> a<1> {};");
  10212. verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
  10213. " int j) -> int {\n"
  10214. " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
  10215. "};");
  10216. verifyFormat(
  10217. "aaaaaaaaaaaaaaaaaaaaaa(\n"
  10218. " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
  10219. " return aaaaaaaaaaaaaaaaa;\n"
  10220. " });",
  10221. getLLVMStyleWithColumns(70));
  10222. verifyFormat("[]() //\n"
  10223. " -> int {\n"
  10224. " return 1; //\n"
  10225. "};");
  10226. // Multiple lambdas in the same parentheses change indentation rules.
  10227. verifyFormat("SomeFunction(\n"
  10228. " []() {\n"
  10229. " int i = 42;\n"
  10230. " return i;\n"
  10231. " },\n"
  10232. " []() {\n"
  10233. " int j = 43;\n"
  10234. " return j;\n"
  10235. " });");
  10236. // More complex introducers.
  10237. verifyFormat("return [i, args...] {};");
  10238. // Not lambdas.
  10239. verifyFormat("constexpr char hello[]{\"hello\"};");
  10240. verifyFormat("double &operator[](int i) { return 0; }\n"
  10241. "int i;");
  10242. verifyFormat("std::unique_ptr<int[]> foo() {}");
  10243. verifyFormat("int i = a[a][a]->f();");
  10244. verifyFormat("int i = (*b)[a]->f();");
  10245. // Other corner cases.
  10246. verifyFormat("void f() {\n"
  10247. " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
  10248. " );\n"
  10249. "}");
  10250. // Lambdas created through weird macros.
  10251. verifyFormat("void f() {\n"
  10252. " MACRO((const AA &a) { return 1; });\n"
  10253. " MACRO((AA &a) { return 1; });\n"
  10254. "}");
  10255. verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
  10256. " doo_dah();\n"
  10257. " doo_dah();\n"
  10258. " })) {\n"
  10259. "}");
  10260. verifyFormat("auto lambda = []() {\n"
  10261. " int a = 2\n"
  10262. "#if A\n"
  10263. " + 2\n"
  10264. "#endif\n"
  10265. " ;\n"
  10266. "};");
  10267. }
  10268. TEST_F(FormatTest, FormatsBlocks) {
  10269. FormatStyle ShortBlocks = getLLVMStyle();
  10270. ShortBlocks.AllowShortBlocksOnASingleLine = true;
  10271. verifyFormat("int (^Block)(int, int);", ShortBlocks);
  10272. verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
  10273. verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
  10274. verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
  10275. verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
  10276. verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
  10277. verifyFormat("foo(^{ bar(); });", ShortBlocks);
  10278. verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
  10279. verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
  10280. verifyFormat("[operation setCompletionBlock:^{\n"
  10281. " [self onOperationDone];\n"
  10282. "}];");
  10283. verifyFormat("int i = {[operation setCompletionBlock:^{\n"
  10284. " [self onOperationDone];\n"
  10285. "}]};");
  10286. verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
  10287. " f();\n"
  10288. "}];");
  10289. verifyFormat("int a = [operation block:^int(int *i) {\n"
  10290. " return 1;\n"
  10291. "}];");
  10292. verifyFormat("[myObject doSomethingWith:arg1\n"
  10293. " aaa:^int(int *a) {\n"
  10294. " return 1;\n"
  10295. " }\n"
  10296. " bbb:f(a * bbbbbbbb)];");
  10297. verifyFormat("[operation setCompletionBlock:^{\n"
  10298. " [self.delegate newDataAvailable];\n"
  10299. "}];",
  10300. getLLVMStyleWithColumns(60));
  10301. verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
  10302. " NSString *path = [self sessionFilePath];\n"
  10303. " if (path) {\n"
  10304. " // ...\n"
  10305. " }\n"
  10306. "});");
  10307. verifyFormat("[[SessionService sharedService]\n"
  10308. " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  10309. " if (window) {\n"
  10310. " [self windowDidLoad:window];\n"
  10311. " } else {\n"
  10312. " [self errorLoadingWindow];\n"
  10313. " }\n"
  10314. " }];");
  10315. verifyFormat("void (^largeBlock)(void) = ^{\n"
  10316. " // ...\n"
  10317. "};\n",
  10318. getLLVMStyleWithColumns(40));
  10319. verifyFormat("[[SessionService sharedService]\n"
  10320. " loadWindowWithCompletionBlock: //\n"
  10321. " ^(SessionWindow *window) {\n"
  10322. " if (window) {\n"
  10323. " [self windowDidLoad:window];\n"
  10324. " } else {\n"
  10325. " [self errorLoadingWindow];\n"
  10326. " }\n"
  10327. " }];",
  10328. getLLVMStyleWithColumns(60));
  10329. verifyFormat("[myObject doSomethingWith:arg1\n"
  10330. " firstBlock:^(Foo *a) {\n"
  10331. " // ...\n"
  10332. " int i;\n"
  10333. " }\n"
  10334. " secondBlock:^(Bar *b) {\n"
  10335. " // ...\n"
  10336. " int i;\n"
  10337. " }\n"
  10338. " thirdBlock:^Foo(Bar *b) {\n"
  10339. " // ...\n"
  10340. " int i;\n"
  10341. " }];");
  10342. verifyFormat("[myObject doSomethingWith:arg1\n"
  10343. " firstBlock:-1\n"
  10344. " secondBlock:^(Bar *b) {\n"
  10345. " // ...\n"
  10346. " int i;\n"
  10347. " }];");
  10348. verifyFormat("f(^{\n"
  10349. " @autoreleasepool {\n"
  10350. " if (a) {\n"
  10351. " g();\n"
  10352. " }\n"
  10353. " }\n"
  10354. "});");
  10355. verifyFormat("Block b = ^int *(A *a, B *b) {}");
  10356. FormatStyle FourIndent = getLLVMStyle();
  10357. FourIndent.ObjCBlockIndentWidth = 4;
  10358. verifyFormat("[operation setCompletionBlock:^{\n"
  10359. " [self onOperationDone];\n"
  10360. "}];",
  10361. FourIndent);
  10362. }
  10363. TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
  10364. FormatStyle ZeroColumn = getLLVMStyle();
  10365. ZeroColumn.ColumnLimit = 0;
  10366. verifyFormat("[[SessionService sharedService] "
  10367. "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  10368. " if (window) {\n"
  10369. " [self windowDidLoad:window];\n"
  10370. " } else {\n"
  10371. " [self errorLoadingWindow];\n"
  10372. " }\n"
  10373. "}];",
  10374. ZeroColumn);
  10375. EXPECT_EQ("[[SessionService sharedService]\n"
  10376. " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  10377. " if (window) {\n"
  10378. " [self windowDidLoad:window];\n"
  10379. " } else {\n"
  10380. " [self errorLoadingWindow];\n"
  10381. " }\n"
  10382. " }];",
  10383. format("[[SessionService sharedService]\n"
  10384. "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  10385. " if (window) {\n"
  10386. " [self windowDidLoad:window];\n"
  10387. " } else {\n"
  10388. " [self errorLoadingWindow];\n"
  10389. " }\n"
  10390. "}];",
  10391. ZeroColumn));
  10392. verifyFormat("[myObject doSomethingWith:arg1\n"
  10393. " firstBlock:^(Foo *a) {\n"
  10394. " // ...\n"
  10395. " int i;\n"
  10396. " }\n"
  10397. " secondBlock:^(Bar *b) {\n"
  10398. " // ...\n"
  10399. " int i;\n"
  10400. " }\n"
  10401. " thirdBlock:^Foo(Bar *b) {\n"
  10402. " // ...\n"
  10403. " int i;\n"
  10404. " }];",
  10405. ZeroColumn);
  10406. verifyFormat("f(^{\n"
  10407. " @autoreleasepool {\n"
  10408. " if (a) {\n"
  10409. " g();\n"
  10410. " }\n"
  10411. " }\n"
  10412. "});",
  10413. ZeroColumn);
  10414. verifyFormat("void (^largeBlock)(void) = ^{\n"
  10415. " // ...\n"
  10416. "};",
  10417. ZeroColumn);
  10418. ZeroColumn.AllowShortBlocksOnASingleLine = true;
  10419. EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
  10420. format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn));
  10421. ZeroColumn.AllowShortBlocksOnASingleLine = false;
  10422. EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
  10423. " int i;\n"
  10424. "};",
  10425. format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn));
  10426. }
  10427. TEST_F(FormatTest, SupportsCRLF) {
  10428. EXPECT_EQ("int a;\r\n"
  10429. "int b;\r\n"
  10430. "int c;\r\n",
  10431. format("int a;\r\n"
  10432. " int b;\r\n"
  10433. " int c;\r\n",
  10434. getLLVMStyle()));
  10435. EXPECT_EQ("int a;\r\n"
  10436. "int b;\r\n"
  10437. "int c;\r\n",
  10438. format("int a;\r\n"
  10439. " int b;\n"
  10440. " int c;\r\n",
  10441. getLLVMStyle()));
  10442. EXPECT_EQ("int a;\n"
  10443. "int b;\n"
  10444. "int c;\n",
  10445. format("int a;\r\n"
  10446. " int b;\n"
  10447. " int c;\n",
  10448. getLLVMStyle()));
  10449. EXPECT_EQ("\"aaaaaaa \"\r\n"
  10450. "\"bbbbbbb\";\r\n",
  10451. format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
  10452. EXPECT_EQ("#define A \\\r\n"
  10453. " b; \\\r\n"
  10454. " c; \\\r\n"
  10455. " d;\r\n",
  10456. format("#define A \\\r\n"
  10457. " b; \\\r\n"
  10458. " c; d; \r\n",
  10459. getGoogleStyle()));
  10460. EXPECT_EQ("/*\r\n"
  10461. "multi line block comments\r\n"
  10462. "should not introduce\r\n"
  10463. "an extra carriage return\r\n"
  10464. "*/\r\n",
  10465. format("/*\r\n"
  10466. "multi line block comments\r\n"
  10467. "should not introduce\r\n"
  10468. "an extra carriage return\r\n"
  10469. "*/\r\n"));
  10470. }
  10471. TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
  10472. verifyFormat("MY_CLASS(C) {\n"
  10473. " int i;\n"
  10474. " int j;\n"
  10475. "};");
  10476. }
  10477. TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
  10478. FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
  10479. TwoIndent.ContinuationIndentWidth = 2;
  10480. EXPECT_EQ("int i =\n"
  10481. " longFunction(\n"
  10482. " arg);",
  10483. format("int i = longFunction(arg);", TwoIndent));
  10484. FormatStyle SixIndent = getLLVMStyleWithColumns(20);
  10485. SixIndent.ContinuationIndentWidth = 6;
  10486. EXPECT_EQ("int i =\n"
  10487. " longFunction(\n"
  10488. " arg);",
  10489. format("int i = longFunction(arg);", SixIndent));
  10490. }
  10491. TEST_F(FormatTest, SpacesInAngles) {
  10492. FormatStyle Spaces = getLLVMStyle();
  10493. Spaces.SpacesInAngles = true;
  10494. verifyFormat("static_cast< int >(arg);", Spaces);
  10495. verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
  10496. verifyFormat("f< int, float >();", Spaces);
  10497. verifyFormat("template <> g() {}", Spaces);
  10498. verifyFormat("template < std::vector< int > > f() {}", Spaces);
  10499. verifyFormat("std::function< void(int, int) > fct;", Spaces);
  10500. verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
  10501. Spaces);
  10502. Spaces.Standard = FormatStyle::LS_Cpp03;
  10503. Spaces.SpacesInAngles = true;
  10504. verifyFormat("A< A< int > >();", Spaces);
  10505. Spaces.SpacesInAngles = false;
  10506. verifyFormat("A<A<int> >();", Spaces);
  10507. Spaces.Standard = FormatStyle::LS_Cpp11;
  10508. Spaces.SpacesInAngles = true;
  10509. verifyFormat("A< A< int > >();", Spaces);
  10510. Spaces.SpacesInAngles = false;
  10511. verifyFormat("A<A<int>>();", Spaces);
  10512. }
  10513. TEST_F(FormatTest, TripleAngleBrackets) {
  10514. verifyFormat("f<<<1, 1>>>();");
  10515. verifyFormat("f<<<1, 1, 1, s>>>();");
  10516. verifyFormat("f<<<a, b, c, d>>>();");
  10517. EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
  10518. verifyFormat("f<param><<<1, 1>>>();");
  10519. verifyFormat("f<1><<<1, 1>>>();");
  10520. EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
  10521. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  10522. "aaaaaaaaaaa<<<\n 1, 1>>>();");
  10523. }
  10524. TEST_F(FormatTest, MergeLessLessAtEnd) {
  10525. verifyFormat("<<");
  10526. EXPECT_EQ("< < <", format("\\\n<<<"));
  10527. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  10528. "aaallvm::outs() <<");
  10529. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  10530. "aaaallvm::outs()\n <<");
  10531. }
  10532. TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
  10533. std::string code = "#if A\n"
  10534. "#if B\n"
  10535. "a.\n"
  10536. "#endif\n"
  10537. " a = 1;\n"
  10538. "#else\n"
  10539. "#endif\n"
  10540. "#if C\n"
  10541. "#else\n"
  10542. "#endif\n";
  10543. EXPECT_EQ(code, format(code));
  10544. }
  10545. TEST_F(FormatTest, HandleConflictMarkers) {
  10546. // Git/SVN conflict markers.
  10547. EXPECT_EQ("int a;\n"
  10548. "void f() {\n"
  10549. " callme(some(parameter1,\n"
  10550. "<<<<<<< text by the vcs\n"
  10551. " parameter2),\n"
  10552. "||||||| text by the vcs\n"
  10553. " parameter2),\n"
  10554. " parameter3,\n"
  10555. "======= text by the vcs\n"
  10556. " parameter2, parameter3),\n"
  10557. ">>>>>>> text by the vcs\n"
  10558. " otherparameter);\n",
  10559. format("int a;\n"
  10560. "void f() {\n"
  10561. " callme(some(parameter1,\n"
  10562. "<<<<<<< text by the vcs\n"
  10563. " parameter2),\n"
  10564. "||||||| text by the vcs\n"
  10565. " parameter2),\n"
  10566. " parameter3,\n"
  10567. "======= text by the vcs\n"
  10568. " parameter2,\n"
  10569. " parameter3),\n"
  10570. ">>>>>>> text by the vcs\n"
  10571. " otherparameter);\n"));
  10572. // Perforce markers.
  10573. EXPECT_EQ("void f() {\n"
  10574. " function(\n"
  10575. ">>>> text by the vcs\n"
  10576. " parameter,\n"
  10577. "==== text by the vcs\n"
  10578. " parameter,\n"
  10579. "==== text by the vcs\n"
  10580. " parameter,\n"
  10581. "<<<< text by the vcs\n"
  10582. " parameter);\n",
  10583. format("void f() {\n"
  10584. " function(\n"
  10585. ">>>> text by the vcs\n"
  10586. " parameter,\n"
  10587. "==== text by the vcs\n"
  10588. " parameter,\n"
  10589. "==== text by the vcs\n"
  10590. " parameter,\n"
  10591. "<<<< text by the vcs\n"
  10592. " parameter);\n"));
  10593. EXPECT_EQ("<<<<<<<\n"
  10594. "|||||||\n"
  10595. "=======\n"
  10596. ">>>>>>>",
  10597. format("<<<<<<<\n"
  10598. "|||||||\n"
  10599. "=======\n"
  10600. ">>>>>>>"));
  10601. EXPECT_EQ("<<<<<<<\n"
  10602. "|||||||\n"
  10603. "int i;\n"
  10604. "=======\n"
  10605. ">>>>>>>",
  10606. format("<<<<<<<\n"
  10607. "|||||||\n"
  10608. "int i;\n"
  10609. "=======\n"
  10610. ">>>>>>>"));
  10611. // FIXME: Handle parsing of macros around conflict markers correctly:
  10612. EXPECT_EQ("#define Macro \\\n"
  10613. "<<<<<<<\n"
  10614. "Something \\\n"
  10615. "|||||||\n"
  10616. "Else \\\n"
  10617. "=======\n"
  10618. "Other \\\n"
  10619. ">>>>>>>\n"
  10620. " End int i;\n",
  10621. format("#define Macro \\\n"
  10622. "<<<<<<<\n"
  10623. " Something \\\n"
  10624. "|||||||\n"
  10625. " Else \\\n"
  10626. "=======\n"
  10627. " Other \\\n"
  10628. ">>>>>>>\n"
  10629. " End\n"
  10630. "int i;\n"));
  10631. }
  10632. TEST_F(FormatTest, DisableRegions) {
  10633. EXPECT_EQ("int i;\n"
  10634. "// clang-format off\n"
  10635. " int j;\n"
  10636. "// clang-format on\n"
  10637. "int k;",
  10638. format(" int i;\n"
  10639. " // clang-format off\n"
  10640. " int j;\n"
  10641. " // clang-format on\n"
  10642. " int k;"));
  10643. EXPECT_EQ("int i;\n"
  10644. "/* clang-format off */\n"
  10645. " int j;\n"
  10646. "/* clang-format on */\n"
  10647. "int k;",
  10648. format(" int i;\n"
  10649. " /* clang-format off */\n"
  10650. " int j;\n"
  10651. " /* clang-format on */\n"
  10652. " int k;"));
  10653. }
  10654. TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
  10655. format("? ) =");
  10656. verifyNoCrash("#define a\\\n /**/}");
  10657. }
  10658. TEST_F(FormatTest, FormatsTableGenCode) {
  10659. FormatStyle Style = getLLVMStyle();
  10660. Style.Language = FormatStyle::LK_TableGen;
  10661. verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
  10662. }
  10663. // Since this test case uses UNIX-style file path. We disable it for MS
  10664. // compiler.
  10665. #if !defined(_MSC_VER) && !defined(__MINGW32__)
  10666. TEST(FormatStyle, GetStyleOfFile) {
  10667. vfs::InMemoryFileSystem FS;
  10668. // Test 1: format file in the same directory.
  10669. ASSERT_TRUE(
  10670. FS.addFile("/a/.clang-format", 0,
  10671. llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
  10672. ASSERT_TRUE(
  10673. FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
  10674. auto Style1 = getStyle("file", "/a/.clang-format", "Google", &FS);
  10675. ASSERT_EQ(Style1, getLLVMStyle());
  10676. // Test 2: fallback to default.
  10677. ASSERT_TRUE(
  10678. FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
  10679. auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", &FS);
  10680. ASSERT_EQ(Style2, getMozillaStyle());
  10681. // Test 3: format file in parent directory.
  10682. ASSERT_TRUE(
  10683. FS.addFile("/c/.clang-format", 0,
  10684. llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
  10685. ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
  10686. llvm::MemoryBuffer::getMemBuffer("int i;")));
  10687. auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", &FS);
  10688. ASSERT_EQ(Style3, getGoogleStyle());
  10689. }
  10690. #endif // _MSC_VER
  10691. class ReplacementTest : public ::testing::Test {
  10692. protected:
  10693. tooling::Replacement createReplacement(SourceLocation Start, unsigned Length,
  10694. llvm::StringRef ReplacementText) {
  10695. return tooling::Replacement(Context.Sources, Start, Length,
  10696. ReplacementText);
  10697. }
  10698. RewriterTestContext Context;
  10699. };
  10700. TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
  10701. // Column limit is 20.
  10702. std::string Code = "Type *a =\n"
  10703. " new Type();\n"
  10704. "g(iiiii, 0, jjjjj,\n"
  10705. " 0, kkkkk, 0, mm);\n"
  10706. "int bad = format ;";
  10707. std::string Expected = "auto a = new Type();\n"
  10708. "g(iiiii, nullptr,\n"
  10709. " jjjjj, nullptr,\n"
  10710. " kkkkk, nullptr,\n"
  10711. " mm);\n"
  10712. "int bad = format ;";
  10713. FileID ID = Context.createInMemoryFile("format.cpp", Code);
  10714. tooling::Replacements Replaces;
  10715. Replaces.insert(tooling::Replacement(
  10716. Context.Sources, Context.getLocation(ID, 1, 1), 6, "auto "));
  10717. Replaces.insert(tooling::Replacement(
  10718. Context.Sources, Context.getLocation(ID, 3, 10), 1, "nullptr"));
  10719. Replaces.insert(tooling::Replacement(
  10720. Context.Sources, Context.getLocation(ID, 4, 3), 1, "nullptr"));
  10721. Replaces.insert(tooling::Replacement(
  10722. Context.Sources, Context.getLocation(ID, 4, 13), 1, "nullptr"));
  10723. format::FormatStyle Style = format::getLLVMStyle();
  10724. Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
  10725. auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
  10726. EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
  10727. << llvm::toString(FormattedReplaces.takeError()) << "\n";
  10728. auto Result = applyAllReplacements(Code, *FormattedReplaces);
  10729. EXPECT_TRUE(static_cast<bool>(Result));
  10730. EXPECT_EQ(Expected, *Result);
  10731. }
  10732. TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
  10733. std::string Code = "#include \"a.h\"\n"
  10734. "#include \"c.h\"\n"
  10735. "\n"
  10736. "int main() {\n"
  10737. " return 0;\n"
  10738. "}";
  10739. std::string Expected = "#include \"a.h\"\n"
  10740. "#include \"b.h\"\n"
  10741. "#include \"c.h\"\n"
  10742. "\n"
  10743. "int main() {\n"
  10744. " return 0;\n"
  10745. "}";
  10746. FileID ID = Context.createInMemoryFile("fix.cpp", Code);
  10747. tooling::Replacements Replaces;
  10748. Replaces.insert(tooling::Replacement(
  10749. Context.Sources, Context.getLocation(ID, 1, 1), 0, "#include \"b.h\"\n"));
  10750. format::FormatStyle Style = format::getLLVMStyle();
  10751. Style.SortIncludes = true;
  10752. auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
  10753. EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
  10754. << llvm::toString(FormattedReplaces.takeError()) << "\n";
  10755. auto Result = applyAllReplacements(Code, *FormattedReplaces);
  10756. EXPECT_TRUE(static_cast<bool>(Result));
  10757. EXPECT_EQ(Expected, *Result);
  10758. }
  10759. } // end namespace
  10760. } // end namespace format
  10761. } // end namespace clang