FormatTest.cpp 257 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960
  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. #define DEBUG_TYPE "format-test"
  10. #include "clang/Format/Format.h"
  11. #include "clang/Lex/Lexer.h"
  12. #include "llvm/Support/Debug.h"
  13. #include "gtest/gtest.h"
  14. namespace clang {
  15. namespace format {
  16. class FormatTest : public ::testing::Test {
  17. protected:
  18. std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length,
  19. const FormatStyle &Style) {
  20. DEBUG(llvm::errs() << "---\n");
  21. DEBUG(llvm::errs() << Code << "\n\n");
  22. std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
  23. tooling::Replacements Replaces = reformat(Style, Code, Ranges);
  24. ReplacementCount = Replaces.size();
  25. std::string Result = applyAllReplacements(Code, Replaces);
  26. EXPECT_NE("", Result);
  27. DEBUG(llvm::errs() << "\n" << Result << "\n\n");
  28. return Result;
  29. }
  30. std::string
  31. format(llvm::StringRef Code, const FormatStyle &Style = getLLVMStyle()) {
  32. return format(Code, 0, Code.size(), Style);
  33. }
  34. std::string messUp(llvm::StringRef Code) {
  35. std::string MessedUp(Code.str());
  36. bool InComment = false;
  37. bool InPreprocessorDirective = false;
  38. bool JustReplacedNewline = false;
  39. for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) {
  40. if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') {
  41. if (JustReplacedNewline)
  42. MessedUp[i - 1] = '\n';
  43. InComment = true;
  44. } else if (MessedUp[i] == '#' && (JustReplacedNewline || i == 0)) {
  45. if (i != 0)
  46. MessedUp[i - 1] = '\n';
  47. InPreprocessorDirective = true;
  48. } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') {
  49. MessedUp[i] = ' ';
  50. MessedUp[i + 1] = ' ';
  51. } else if (MessedUp[i] == '\n') {
  52. if (InComment) {
  53. InComment = false;
  54. } else if (InPreprocessorDirective) {
  55. InPreprocessorDirective = false;
  56. } else {
  57. JustReplacedNewline = true;
  58. MessedUp[i] = ' ';
  59. }
  60. } else if (MessedUp[i] != ' ') {
  61. JustReplacedNewline = false;
  62. }
  63. }
  64. std::string WithoutWhitespace;
  65. if (MessedUp[0] != ' ')
  66. WithoutWhitespace.push_back(MessedUp[0]);
  67. for (unsigned i = 1, e = MessedUp.size(); i != e; ++i) {
  68. if (MessedUp[i] != ' ' || MessedUp[i - 1] != ' ')
  69. WithoutWhitespace.push_back(MessedUp[i]);
  70. }
  71. return WithoutWhitespace;
  72. }
  73. FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
  74. FormatStyle Style = getLLVMStyle();
  75. Style.ColumnLimit = ColumnLimit;
  76. return Style;
  77. }
  78. FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
  79. FormatStyle Style = getGoogleStyle();
  80. Style.ColumnLimit = ColumnLimit;
  81. return Style;
  82. }
  83. void verifyFormat(llvm::StringRef Code,
  84. const FormatStyle &Style = getLLVMStyle()) {
  85. EXPECT_EQ(Code.str(), format(messUp(Code), Style));
  86. }
  87. void verifyGoogleFormat(llvm::StringRef Code) {
  88. verifyFormat(Code, getGoogleStyle());
  89. }
  90. void verifyIndependentOfContext(llvm::StringRef text) {
  91. verifyFormat(text);
  92. verifyFormat(llvm::Twine("void f() { " + text + " }").str());
  93. }
  94. int ReplacementCount;
  95. };
  96. TEST_F(FormatTest, MessUp) {
  97. EXPECT_EQ("1 2 3", messUp("1 2 3"));
  98. EXPECT_EQ("1 2 3\n", messUp("1\n2\n3\n"));
  99. EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc"));
  100. EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc"));
  101. EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne"));
  102. }
  103. //===----------------------------------------------------------------------===//
  104. // Basic function tests.
  105. //===----------------------------------------------------------------------===//
  106. TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
  107. EXPECT_EQ(";", format(";"));
  108. }
  109. TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
  110. EXPECT_EQ("int i;", format(" int i;"));
  111. EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;"));
  112. EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
  113. EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
  114. }
  115. TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
  116. EXPECT_EQ("int i;", format("int\ni;"));
  117. }
  118. TEST_F(FormatTest, FormatsNestedBlockStatements) {
  119. EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
  120. }
  121. TEST_F(FormatTest, FormatsNestedCall) {
  122. verifyFormat("Method(f1, f2(f3));");
  123. verifyFormat("Method(f1(f2, f3()));");
  124. verifyFormat("Method(f1(f2, (f3())));");
  125. }
  126. TEST_F(FormatTest, NestedNameSpecifiers) {
  127. verifyFormat("vector< ::Type> v;");
  128. verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
  129. verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
  130. }
  131. TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
  132. EXPECT_EQ("if (a) {\n"
  133. " f();\n"
  134. "}",
  135. format("if(a){f();}"));
  136. EXPECT_EQ(4, ReplacementCount);
  137. EXPECT_EQ("if (a) {\n"
  138. " f();\n"
  139. "}",
  140. format("if (a) {\n"
  141. " f();\n"
  142. "}"));
  143. EXPECT_EQ(0, ReplacementCount);
  144. }
  145. TEST_F(FormatTest, RemovesTrailingWhitespaceOfFormattedLine) {
  146. EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0, getLLVMStyle()));
  147. EXPECT_EQ("int a;", format("int a; "));
  148. EXPECT_EQ("int a;\n", format("int a; \n \n \n "));
  149. EXPECT_EQ("int a;\nint b; ",
  150. format("int a; \nint b; ", 0, 0, getLLVMStyle()));
  151. }
  152. TEST_F(FormatTest, FormatsCorrectRegionForLeadingWhitespace) {
  153. EXPECT_EQ("int b;\nint a;",
  154. format("int b;\n int a;", 7, 0, getLLVMStyle()));
  155. EXPECT_EQ("int b;\n int a;",
  156. format("int b;\n int a;", 6, 0, getLLVMStyle()));
  157. EXPECT_EQ("#define A \\\n"
  158. " int a; \\\n"
  159. " int b;",
  160. format("#define A \\\n"
  161. " int a; \\\n"
  162. " int b;",
  163. 26, 0, getLLVMStyleWithColumns(12)));
  164. EXPECT_EQ("#define A \\\n"
  165. " int a; \\\n"
  166. " int b;",
  167. format("#define A \\\n"
  168. " int a; \\\n"
  169. " int b;",
  170. 25, 0, getLLVMStyleWithColumns(12)));
  171. }
  172. TEST_F(FormatTest, RemovesWhitespaceWhenTriggeredOnEmptyLine) {
  173. EXPECT_EQ("int a;\n\n int b;",
  174. format("int a;\n \n\n int b;", 7, 0, getLLVMStyle()));
  175. EXPECT_EQ("int a;\n\n int b;",
  176. format("int a;\n \n\n int b;", 9, 0, getLLVMStyle()));
  177. }
  178. TEST_F(FormatTest, RemovesEmptyLines) {
  179. EXPECT_EQ("class C {\n"
  180. " int i;\n"
  181. "};",
  182. format("class C {\n"
  183. " int i;\n"
  184. "\n"
  185. "};"));
  186. // Don't remove empty lines in more complex control statements.
  187. EXPECT_EQ("void f() {\n"
  188. " if (a) {\n"
  189. " f();\n"
  190. "\n"
  191. " } else if (b) {\n"
  192. " f();\n"
  193. " }\n"
  194. "}",
  195. format("void f() {\n"
  196. " if (a) {\n"
  197. " f();\n"
  198. "\n"
  199. " } else if (b) {\n"
  200. " f();\n"
  201. "\n"
  202. " }\n"
  203. "\n"
  204. "}"));
  205. // FIXME: This is slightly inconsistent.
  206. EXPECT_EQ("namespace {\n"
  207. "int i;\n"
  208. "}",
  209. format("namespace {\n"
  210. "int i;\n"
  211. "\n"
  212. "}"));
  213. EXPECT_EQ("namespace {\n"
  214. "int i;\n"
  215. "\n"
  216. "} // namespace",
  217. format("namespace {\n"
  218. "int i;\n"
  219. "\n"
  220. "} // namespace"));
  221. }
  222. TEST_F(FormatTest, ReformatsMovedLines) {
  223. EXPECT_EQ(
  224. "template <typename T> T *getFETokenInfo() const {\n"
  225. " return static_cast<T *>(FETokenInfo);\n"
  226. "}\n"
  227. " int a; // <- Should not be formatted",
  228. format(
  229. "template<typename T>\n"
  230. "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
  231. " int a; // <- Should not be formatted",
  232. 9, 5, getLLVMStyle()));
  233. }
  234. //===----------------------------------------------------------------------===//
  235. // Tests for control statements.
  236. //===----------------------------------------------------------------------===//
  237. TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
  238. verifyFormat("if (true)\n f();\ng();");
  239. verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
  240. verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
  241. FormatStyle AllowsMergedIf = getLLVMStyle();
  242. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  243. verifyFormat("if (a)\n"
  244. " // comment\n"
  245. " f();",
  246. AllowsMergedIf);
  247. verifyFormat("if (a)\n"
  248. " ;",
  249. AllowsMergedIf);
  250. verifyFormat("if (a)\n"
  251. " if (b) return;",
  252. AllowsMergedIf);
  253. verifyFormat("if (a) // Can't merge this\n"
  254. " f();\n",
  255. AllowsMergedIf);
  256. verifyFormat("if (a) /* still don't merge */\n"
  257. " f();",
  258. AllowsMergedIf);
  259. verifyFormat("if (a) { // Never merge this\n"
  260. " f();\n"
  261. "}",
  262. AllowsMergedIf);
  263. verifyFormat("if (a) { /* Never merge this */\n"
  264. " f();\n"
  265. "}",
  266. AllowsMergedIf);
  267. EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1, AllowsMergedIf));
  268. EXPECT_EQ("if (a) return; // comment",
  269. format("if(a)\nreturn; // comment", 20, 1, AllowsMergedIf));
  270. AllowsMergedIf.ColumnLimit = 14;
  271. verifyFormat("if (a) return;", AllowsMergedIf);
  272. verifyFormat("if (aaaaaaaaa)\n"
  273. " return;",
  274. AllowsMergedIf);
  275. AllowsMergedIf.ColumnLimit = 13;
  276. verifyFormat("if (a)\n return;", AllowsMergedIf);
  277. }
  278. TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
  279. FormatStyle AllowsMergedLoops = getLLVMStyle();
  280. AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
  281. verifyFormat("while (true) continue;", AllowsMergedLoops);
  282. verifyFormat("for (;;) continue;", AllowsMergedLoops);
  283. verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
  284. verifyFormat("while (true)\n"
  285. " ;",
  286. AllowsMergedLoops);
  287. verifyFormat("for (;;)\n"
  288. " ;",
  289. AllowsMergedLoops);
  290. verifyFormat("for (;;)\n"
  291. " for (;;) continue;",
  292. AllowsMergedLoops);
  293. verifyFormat("for (;;) // Can't merge this\n"
  294. " continue;",
  295. AllowsMergedLoops);
  296. verifyFormat("for (;;) /* still don't merge */\n"
  297. " continue;",
  298. AllowsMergedLoops);
  299. }
  300. TEST_F(FormatTest, ParseIfElse) {
  301. verifyFormat("if (true)\n"
  302. " if (true)\n"
  303. " if (true)\n"
  304. " f();\n"
  305. " else\n"
  306. " g();\n"
  307. " else\n"
  308. " h();\n"
  309. "else\n"
  310. " i();");
  311. verifyFormat("if (true)\n"
  312. " if (true)\n"
  313. " if (true) {\n"
  314. " if (true)\n"
  315. " f();\n"
  316. " } else {\n"
  317. " g();\n"
  318. " }\n"
  319. " else\n"
  320. " h();\n"
  321. "else {\n"
  322. " i();\n"
  323. "}");
  324. verifyFormat("void f() {\n"
  325. " if (a) {\n"
  326. " } else {\n"
  327. " }\n"
  328. "}");
  329. }
  330. TEST_F(FormatTest, ElseIf) {
  331. verifyFormat("if (a) {\n} else if (b) {\n}");
  332. verifyFormat("if (a)\n"
  333. " f();\n"
  334. "else if (b)\n"
  335. " g();\n"
  336. "else\n"
  337. " h();");
  338. }
  339. TEST_F(FormatTest, FormatsForLoop) {
  340. verifyFormat(
  341. "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
  342. " ++VeryVeryLongLoopVariable)\n"
  343. " ;");
  344. verifyFormat("for (;;)\n"
  345. " f();");
  346. verifyFormat("for (;;) {\n}");
  347. verifyFormat("for (;;) {\n"
  348. " f();\n"
  349. "}");
  350. verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
  351. verifyFormat(
  352. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  353. " E = UnwrappedLines.end();\n"
  354. " I != E; ++I) {\n}");
  355. verifyFormat(
  356. "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
  357. " ++IIIII) {\n}");
  358. verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
  359. " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
  360. " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
  361. verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
  362. " I = FD->getDeclsInPrototypeScope().begin(),\n"
  363. " E = FD->getDeclsInPrototypeScope().end();\n"
  364. " I != E; ++I) {\n}");
  365. // FIXME: Not sure whether we want extra identation in line 3 here:
  366. verifyFormat(
  367. "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  368. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
  369. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  370. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  371. " ++aaaaaaaaaaa) {\n}");
  372. verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
  373. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  374. "}");
  375. verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
  376. " aaaaaaaaaa);\n"
  377. " iter; ++iter) {\n"
  378. "}");
  379. FormatStyle NoBinPacking = getLLVMStyle();
  380. NoBinPacking.BinPackParameters = false;
  381. verifyFormat("for (int aaaaaaaaaaa = 1;\n"
  382. " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
  383. " aaaaaaaaaaaaaaaa,\n"
  384. " aaaaaaaaaaaaaaaa,\n"
  385. " aaaaaaaaaaaaaaaa);\n"
  386. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  387. "}",
  388. NoBinPacking);
  389. verifyFormat(
  390. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  391. " E = UnwrappedLines.end();\n"
  392. " I != E;\n"
  393. " ++I) {\n}",
  394. NoBinPacking);
  395. }
  396. TEST_F(FormatTest, RangeBasedForLoops) {
  397. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  398. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  399. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
  400. " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
  401. verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
  402. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  403. }
  404. TEST_F(FormatTest, FormatsWhileLoop) {
  405. verifyFormat("while (true) {\n}");
  406. verifyFormat("while (true)\n"
  407. " f();");
  408. verifyFormat("while () {\n}");
  409. verifyFormat("while () {\n"
  410. " f();\n"
  411. "}");
  412. }
  413. TEST_F(FormatTest, FormatsDoWhile) {
  414. verifyFormat("do {\n"
  415. " do_something();\n"
  416. "} while (something());");
  417. verifyFormat("do\n"
  418. " do_something();\n"
  419. "while (something());");
  420. }
  421. TEST_F(FormatTest, FormatsSwitchStatement) {
  422. verifyFormat("switch (x) {\n"
  423. "case 1:\n"
  424. " f();\n"
  425. " break;\n"
  426. "case kFoo:\n"
  427. "case ns::kBar:\n"
  428. "case kBaz:\n"
  429. " break;\n"
  430. "default:\n"
  431. " g();\n"
  432. " break;\n"
  433. "}");
  434. verifyFormat("switch (x) {\n"
  435. "case 1: {\n"
  436. " f();\n"
  437. " break;\n"
  438. "}\n"
  439. "}");
  440. verifyFormat("switch (x) {\n"
  441. "case 1: {\n"
  442. " f();\n"
  443. " {\n"
  444. " g();\n"
  445. " h();\n"
  446. " }\n"
  447. " break;\n"
  448. "}\n"
  449. "}");
  450. verifyFormat("switch (x) {\n"
  451. "case 1: {\n"
  452. " f();\n"
  453. " if (foo) {\n"
  454. " g();\n"
  455. " h();\n"
  456. " }\n"
  457. " break;\n"
  458. "}\n"
  459. "}");
  460. verifyFormat("switch (x) {\n"
  461. "case 1: {\n"
  462. " f();\n"
  463. " g();\n"
  464. "} break;\n"
  465. "}");
  466. verifyFormat("switch (test)\n"
  467. " ;");
  468. verifyFormat("switch (x) {\n"
  469. "default: {\n"
  470. " // Do nothing.\n"
  471. "}\n"
  472. "}");
  473. verifyFormat("switch (x) {\n"
  474. "// comment\n"
  475. "// if 1, do f()\n"
  476. "case 1:\n"
  477. " f();\n"
  478. "}");
  479. verifyFormat("switch (x) {\n"
  480. "case 1:\n"
  481. " // Do amazing stuff\n"
  482. " {\n"
  483. " f();\n"
  484. " g();\n"
  485. " }\n"
  486. " break;\n"
  487. "}");
  488. verifyFormat("#define A \\\n"
  489. " switch (x) { \\\n"
  490. " case a: \\\n"
  491. " foo = b; \\\n"
  492. " }", getLLVMStyleWithColumns(20));
  493. verifyFormat("#define OPERATION_CASE(name) \\\n"
  494. " case OP_name: \\\n"
  495. " return operations::Operation##name\n",
  496. getLLVMStyleWithColumns(40));
  497. verifyGoogleFormat("switch (x) {\n"
  498. " case 1:\n"
  499. " f();\n"
  500. " break;\n"
  501. " case kFoo:\n"
  502. " case ns::kBar:\n"
  503. " case kBaz:\n"
  504. " break;\n"
  505. " default:\n"
  506. " g();\n"
  507. " break;\n"
  508. "}");
  509. verifyGoogleFormat("switch (x) {\n"
  510. " case 1: {\n"
  511. " f();\n"
  512. " break;\n"
  513. " }\n"
  514. "}");
  515. verifyGoogleFormat("switch (test)\n"
  516. " ;");
  517. verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
  518. " case OP_name: \\\n"
  519. " return operations::Operation##name\n");
  520. verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
  521. " // Get the correction operation class.\n"
  522. " switch (OpCode) {\n"
  523. " CASE(Add);\n"
  524. " CASE(Subtract);\n"
  525. " default:\n"
  526. " return operations::Unknown;\n"
  527. " }\n"
  528. "#undef OPERATION_CASE\n"
  529. "}");
  530. verifyFormat("DEBUG({\n"
  531. " switch (x) {\n"
  532. " case A:\n"
  533. " f();\n"
  534. " break;\n"
  535. " // On B:\n"
  536. " case B:\n"
  537. " g();\n"
  538. " break;\n"
  539. " }\n"
  540. "});");
  541. }
  542. TEST_F(FormatTest, CaseRanges) {
  543. verifyFormat("switch (x) {\n"
  544. "case 'A' ... 'Z':\n"
  545. "case 1 ... 5:\n"
  546. " break;\n"
  547. "}");
  548. }
  549. TEST_F(FormatTest, FormatsLabels) {
  550. verifyFormat("void f() {\n"
  551. " some_code();\n"
  552. "test_label:\n"
  553. " some_other_code();\n"
  554. " {\n"
  555. " some_more_code();\n"
  556. " another_label:\n"
  557. " some_more_code();\n"
  558. " }\n"
  559. "}");
  560. verifyFormat("some_code();\n"
  561. "test_label:\n"
  562. "some_other_code();");
  563. }
  564. //===----------------------------------------------------------------------===//
  565. // Tests for comments.
  566. //===----------------------------------------------------------------------===//
  567. TEST_F(FormatTest, UnderstandsSingleLineComments) {
  568. verifyFormat("//* */");
  569. verifyFormat("// line 1\n"
  570. "// line 2\n"
  571. "void f() {}\n");
  572. verifyFormat("void f() {\n"
  573. " // Doesn't do anything\n"
  574. "}");
  575. verifyFormat("SomeObject\n"
  576. " // Calling someFunction on SomeObject\n"
  577. " .someFunction();");
  578. verifyFormat("void f(int i, // some comment (probably for i)\n"
  579. " int j, // some comment (probably for j)\n"
  580. " int k); // some comment (probably for k)");
  581. verifyFormat("void f(int i,\n"
  582. " // some comment (probably for j)\n"
  583. " int j,\n"
  584. " // some comment (probably for k)\n"
  585. " int k);");
  586. verifyFormat("int i // This is a fancy variable\n"
  587. " = 5; // with nicely aligned comment.");
  588. verifyFormat("// Leading comment.\n"
  589. "int a; // Trailing comment.");
  590. verifyFormat("int a; // Trailing comment\n"
  591. " // on 2\n"
  592. " // or 3 lines.\n"
  593. "int b;");
  594. verifyFormat("int a; // Trailing comment\n"
  595. "\n"
  596. "// Leading comment.\n"
  597. "int b;");
  598. verifyFormat("int a; // Comment.\n"
  599. " // More details.\n"
  600. "int bbbb; // Another comment.");
  601. verifyFormat(
  602. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  603. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n"
  604. "int cccccccccccccccccccccccccccccc; // comment\n"
  605. "int ddd; // looooooooooooooooooooooooong comment\n"
  606. "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  607. "int bbbbbbbbbbbbbbbbbbbbb; // comment\n"
  608. "int ccccccccccccccccccc; // comment");
  609. verifyFormat("#include \"a\" // comment\n"
  610. "#include \"a/b/c\" // comment");
  611. verifyFormat("#include <a> // comment\n"
  612. "#include <a/b/c> // comment");
  613. EXPECT_EQ("#include \\\n"
  614. " \"a\" // comment\n"
  615. "#include \"a/b/c\" // comment",
  616. format("#include \\\n"
  617. " \"a\" // comment\n"
  618. "#include \"a/b/c\" // comment"));
  619. verifyFormat("enum E {\n"
  620. " // comment\n"
  621. " VAL_A, // comment\n"
  622. " VAL_B\n"
  623. "};");
  624. verifyFormat(
  625. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  626. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
  627. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  628. " // Comment inside a statement.\n"
  629. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  630. verifyFormat(
  631. "bool aaaaaaaaaaaaa = // comment\n"
  632. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  633. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  634. verifyFormat("int aaaa; // aaaaa\n"
  635. "int aa; // aaaaaaa",
  636. getLLVMStyleWithColumns(20));
  637. EXPECT_EQ("void f() { // This does something ..\n"
  638. "}\n"
  639. "int a; // This is unrelated",
  640. format("void f() { // This does something ..\n"
  641. " }\n"
  642. "int a; // This is unrelated"));
  643. EXPECT_EQ("class C {\n"
  644. " void f() { // This does something ..\n"
  645. " } // awesome..\n"
  646. "\n"
  647. " int a; // This is unrelated\n"
  648. "};",
  649. format("class C{void f() { // This does something ..\n"
  650. " } // awesome..\n"
  651. " \n"
  652. "int a; // This is unrelated\n"
  653. "};"));
  654. EXPECT_EQ("int i; // single line trailing comment",
  655. format("int i;\\\n// single line trailing comment"));
  656. verifyGoogleFormat("int a; // Trailing comment.");
  657. verifyFormat("someFunction(anotherFunction( // Force break.\n"
  658. " parameter));");
  659. verifyGoogleFormat("#endif // HEADER_GUARD");
  660. verifyFormat("const char *test[] = {\n"
  661. " // A\n"
  662. " \"aaaa\",\n"
  663. " // B\n"
  664. " \"aaaaa\",\n"
  665. "};");
  666. verifyGoogleFormat(
  667. "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  668. " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment");
  669. EXPECT_EQ("D(a, {\n"
  670. " // test\n"
  671. " int a;\n"
  672. "});",
  673. format("D(a, {\n"
  674. "// test\n"
  675. "int a;\n"
  676. "});"));
  677. EXPECT_EQ("lineWith(); // comment\n"
  678. "// at start\n"
  679. "otherLine();",
  680. format("lineWith(); // comment\n"
  681. "// at start\n"
  682. "otherLine();"));
  683. EXPECT_EQ("lineWith(); // comment\n"
  684. " // at start\n"
  685. "otherLine();",
  686. format("lineWith(); // comment\n"
  687. " // at start\n"
  688. "otherLine();"));
  689. EXPECT_EQ("lineWith(); // comment\n"
  690. "// at start\n"
  691. "otherLine(); // comment",
  692. format("lineWith(); // comment\n"
  693. "// at start\n"
  694. "otherLine(); // comment"));
  695. EXPECT_EQ("lineWith();\n"
  696. "// at start\n"
  697. "otherLine(); // comment",
  698. format("lineWith();\n"
  699. " // at start\n"
  700. "otherLine(); // comment"));
  701. EXPECT_EQ("// first\n"
  702. "// at start\n"
  703. "otherLine(); // comment",
  704. format("// first\n"
  705. " // at start\n"
  706. "otherLine(); // comment"));
  707. EXPECT_EQ("f();\n"
  708. "// first\n"
  709. "// at start\n"
  710. "otherLine(); // comment",
  711. format("f();\n"
  712. "// first\n"
  713. " // at start\n"
  714. "otherLine(); // comment"));
  715. }
  716. TEST_F(FormatTest, CanFormatCommentsLocally) {
  717. EXPECT_EQ("int a; // comment\n"
  718. "int b; // comment",
  719. format("int a; // comment\n"
  720. "int b; // comment",
  721. 0, 0, getLLVMStyle()));
  722. EXPECT_EQ("int a; // comment\n"
  723. " // line 2\n"
  724. "int b;",
  725. format("int a; // comment\n"
  726. " // line 2\n"
  727. "int b;",
  728. 28, 0, getLLVMStyle()));
  729. EXPECT_EQ("int aaaaaa; // comment\n"
  730. "int b;\n"
  731. "int c; // unrelated comment",
  732. format("int aaaaaa; // comment\n"
  733. "int b;\n"
  734. "int c; // unrelated comment",
  735. 31, 0, getLLVMStyle()));
  736. }
  737. TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
  738. EXPECT_EQ("// comment", format("// comment "));
  739. EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
  740. format("int aaaaaaa, bbbbbbb; // comment ",
  741. getLLVMStyleWithColumns(33)));
  742. EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f "));
  743. EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f "));
  744. }
  745. TEST_F(FormatTest, UnderstandsBlockComments) {
  746. verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
  747. EXPECT_EQ(
  748. "f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
  749. " bbbbbbbbbbbbbbbbbbbbbbbbb);",
  750. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n/* Trailing comment for aa... */\n"
  751. " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  752. EXPECT_EQ(
  753. "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  754. " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
  755. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
  756. "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  757. EXPECT_EQ(
  758. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  759. " aaaaaaaaaaaaaaaaaa,\n"
  760. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  761. "}",
  762. format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  763. " aaaaaaaaaaaaaaaaaa ,\n"
  764. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  765. "}"));
  766. FormatStyle NoBinPacking = getLLVMStyle();
  767. NoBinPacking.BinPackParameters = false;
  768. verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
  769. " /* parameter 2 */ aaaaaa,\n"
  770. " /* parameter 3 */ aaaaaa,\n"
  771. " /* parameter 4 */ aaaaaa);",
  772. NoBinPacking);
  773. }
  774. TEST_F(FormatTest, AlignsBlockComments) {
  775. EXPECT_EQ("/*\n"
  776. " * Really multi-line\n"
  777. " * comment.\n"
  778. " */\n"
  779. "void f() {}",
  780. format(" /*\n"
  781. " * Really multi-line\n"
  782. " * comment.\n"
  783. " */\n"
  784. " void f() {}"));
  785. EXPECT_EQ("class C {\n"
  786. " /*\n"
  787. " * Another multi-line\n"
  788. " * comment.\n"
  789. " */\n"
  790. " void f() {}\n"
  791. "};",
  792. format("class C {\n"
  793. "/*\n"
  794. " * Another multi-line\n"
  795. " * comment.\n"
  796. " */\n"
  797. "void f() {}\n"
  798. "};"));
  799. EXPECT_EQ("/*\n"
  800. " 1. This is a comment with non-trivial formatting.\n"
  801. " 1.1. We have to indent/outdent all lines equally\n"
  802. " 1.1.1. to keep the formatting.\n"
  803. " */",
  804. format(" /*\n"
  805. " 1. This is a comment with non-trivial formatting.\n"
  806. " 1.1. We have to indent/outdent all lines equally\n"
  807. " 1.1.1. to keep the formatting.\n"
  808. " */"));
  809. EXPECT_EQ("/*\n"
  810. "Don't try to outdent if there's not enough indentation.\n"
  811. "*/",
  812. format(" /*\n"
  813. " Don't try to outdent if there's not enough indentation.\n"
  814. " */"));
  815. EXPECT_EQ("int i; /* Comment with empty...\n"
  816. " *\n"
  817. " * line. */",
  818. format("int i; /* Comment with empty...\n"
  819. " *\n"
  820. " * line. */"));
  821. }
  822. TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
  823. EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  824. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
  825. format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  826. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
  827. EXPECT_EQ(
  828. "void ffffffffffff(\n"
  829. " int aaaaaaaa, int bbbbbbbb,\n"
  830. " int cccccccccccc) { /*\n"
  831. " aaaaaaaaaa\n"
  832. " aaaaaaaaaaaaa\n"
  833. " bbbbbbbbbbbbbb\n"
  834. " bbbbbbbbbb\n"
  835. " */\n"
  836. "}",
  837. format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
  838. "{ /*\n"
  839. " aaaaaaaaaa aaaaaaaaaaaaa\n"
  840. " bbbbbbbbbbbbbb bbbbbbbbbb\n"
  841. " */\n"
  842. "}",
  843. getLLVMStyleWithColumns(40)));
  844. }
  845. TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
  846. EXPECT_EQ("void\n"
  847. "ffffffffff(int aaaaa /* test */);",
  848. format("void ffffffffff(int aaaaa /* test */);",
  849. getLLVMStyleWithColumns(35)));
  850. }
  851. TEST_F(FormatTest, SplitsLongCxxComments) {
  852. EXPECT_EQ("// A comment that\n"
  853. "// doesn't fit on\n"
  854. "// one line",
  855. format("// A comment that doesn't fit on one line",
  856. getLLVMStyleWithColumns(20)));
  857. EXPECT_EQ("// a b c d\n"
  858. "// e f g\n"
  859. "// h i j k",
  860. format("// a b c d e f g h i j k",
  861. getLLVMStyleWithColumns(10)));
  862. EXPECT_EQ("// a b c d\n"
  863. "// e f g\n"
  864. "// h i j k",
  865. format("\\\n// a b c d e f g h i j k",
  866. getLLVMStyleWithColumns(10)));
  867. EXPECT_EQ("if (true) // A comment that\n"
  868. " // doesn't fit on\n"
  869. " // one line",
  870. format("if (true) // A comment that doesn't fit on one line ",
  871. getLLVMStyleWithColumns(30)));
  872. EXPECT_EQ("// Don't_touch_leading_whitespace",
  873. format("// Don't_touch_leading_whitespace",
  874. getLLVMStyleWithColumns(20)));
  875. EXPECT_EQ("// Add leading\n"
  876. "// whitespace",
  877. format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
  878. EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
  879. EXPECT_EQ("// Even if it makes the line exceed the column\n"
  880. "// limit",
  881. format("//Even if it makes the line exceed the column limit",
  882. getLLVMStyleWithColumns(51)));
  883. EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
  884. EXPECT_EQ("// A comment before\n"
  885. "// a macro\n"
  886. "// definition\n"
  887. "#define a b",
  888. format("// A comment before a macro definition\n"
  889. "#define a b",
  890. getLLVMStyleWithColumns(20)));
  891. EXPECT_EQ("void ffffff(int aaaaaaaaa, // wwww\n"
  892. " int a, int bbb, // xxxxxxx\n"
  893. " // yyyyyyyyy\n"
  894. " int c, int d, int e) {}",
  895. format("void ffffff(\n"
  896. " int aaaaaaaaa, // wwww\n"
  897. " int a,\n"
  898. " int bbb, // xxxxxxx yyyyyyyyy\n"
  899. " int c, int d, int e) {}",
  900. getLLVMStyleWithColumns(40)));
  901. EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  902. format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  903. getLLVMStyleWithColumns(20)));
  904. }
  905. TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
  906. EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  907. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  908. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  909. format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  910. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  911. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
  912. EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  913. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  914. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  915. format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  916. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  917. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  918. getLLVMStyleWithColumns(50)));
  919. // FIXME: One day we might want to implement adjustment of leading whitespace
  920. // of the consecutive lines in this kind of comment:
  921. EXPECT_EQ("int\n"
  922. "a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  923. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  924. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  925. format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  926. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  927. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  928. getLLVMStyleWithColumns(49)));
  929. }
  930. TEST_F(FormatTest, PriorityOfCommentBreaking) {
  931. EXPECT_EQ("if (xxx ==\n"
  932. " yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  933. " zzz)\n"
  934. " q();",
  935. format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  936. " zzz) q();",
  937. getLLVMStyleWithColumns(40)));
  938. EXPECT_EQ("if (xxxxxxxxxx ==\n"
  939. " yyy && // aaaaaa bbbbbbbb cccc\n"
  940. " zzz)\n"
  941. " q();",
  942. format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
  943. " zzz) q();",
  944. getLLVMStyleWithColumns(40)));
  945. EXPECT_EQ("if (xxxxxxxxxx &&\n"
  946. " yyy || // aaaaaa bbbbbbbb cccc\n"
  947. " zzz)\n"
  948. " q();",
  949. format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
  950. " zzz) q();",
  951. getLLVMStyleWithColumns(40)));
  952. EXPECT_EQ("fffffffff(&xxx, // aaaaaaaaaaaa\n"
  953. " // bbbbbbbbbbb\n"
  954. " zzz);",
  955. format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  956. " zzz);",
  957. getLLVMStyleWithColumns(40)));
  958. }
  959. TEST_F(FormatTest, MultiLineCommentsInDefines) {
  960. EXPECT_EQ("#define A(x) /* \\\n"
  961. " a comment \\\n"
  962. " inside */ \\\n"
  963. " f();",
  964. format("#define A(x) /* \\\n"
  965. " a comment \\\n"
  966. " inside */ \\\n"
  967. " f();",
  968. getLLVMStyleWithColumns(17)));
  969. EXPECT_EQ("#define A( \\\n"
  970. " x) /* \\\n"
  971. " a comment \\\n"
  972. " inside */ \\\n"
  973. " f();",
  974. format("#define A( \\\n"
  975. " x) /* \\\n"
  976. " a comment \\\n"
  977. " inside */ \\\n"
  978. " f();",
  979. getLLVMStyleWithColumns(17)));
  980. }
  981. TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
  982. EXPECT_EQ("namespace {}\n// Test\n#define A",
  983. format("namespace {}\n // Test\n#define A"));
  984. EXPECT_EQ("namespace {}\n/* Test */\n#define A",
  985. format("namespace {}\n /* Test */\n#define A"));
  986. EXPECT_EQ("namespace {}\n/* Test */ #define A",
  987. format("namespace {}\n /* Test */ #define A"));
  988. }
  989. TEST_F(FormatTest, SplitsLongLinesInComments) {
  990. EXPECT_EQ("/* This is a long\n"
  991. " * comment that\n"
  992. " * doesn't\n"
  993. " * fit on one line.\n"
  994. " */",
  995. format("/* "
  996. "This is a long "
  997. "comment that "
  998. "doesn't "
  999. "fit on one line. */",
  1000. getLLVMStyleWithColumns(20)));
  1001. EXPECT_EQ("/* a b c d\n"
  1002. " * e f g\n"
  1003. " * h i j k\n"
  1004. " */",
  1005. format("/* a b c d e f g h i j k */",
  1006. getLLVMStyleWithColumns(10)));
  1007. EXPECT_EQ("/* a b c d\n"
  1008. " * e f g\n"
  1009. " * h i j k\n"
  1010. " */",
  1011. format("\\\n/* a b c d e f g h i j k */",
  1012. getLLVMStyleWithColumns(10)));
  1013. EXPECT_EQ("/*\n"
  1014. "This is a long\n"
  1015. "comment that doesn't\n"
  1016. "fit on one line.\n"
  1017. "*/",
  1018. format("/*\n"
  1019. "This is a long "
  1020. "comment that doesn't "
  1021. "fit on one line. \n"
  1022. "*/", getLLVMStyleWithColumns(20)));
  1023. EXPECT_EQ("/*\n"
  1024. " * This is a long\n"
  1025. " * comment that\n"
  1026. " * doesn't fit on\n"
  1027. " * one line.\n"
  1028. " */",
  1029. format("/* \n"
  1030. " * This is a long "
  1031. " comment that "
  1032. " doesn't fit on "
  1033. " one line. \n"
  1034. " */", getLLVMStyleWithColumns(20)));
  1035. EXPECT_EQ("/*\n"
  1036. " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
  1037. " * so_it_should_be_broken\n"
  1038. " * wherever_a_space_occurs\n"
  1039. " */",
  1040. format("/*\n"
  1041. " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
  1042. " so_it_should_be_broken "
  1043. " wherever_a_space_occurs \n"
  1044. " */",
  1045. getLLVMStyleWithColumns(20)));
  1046. EXPECT_EQ("/*\n"
  1047. " * This_comment_can_not_be_broken_into_lines\n"
  1048. " */",
  1049. format("/*\n"
  1050. " * This_comment_can_not_be_broken_into_lines\n"
  1051. " */",
  1052. getLLVMStyleWithColumns(20)));
  1053. EXPECT_EQ("{\n"
  1054. " /*\n"
  1055. " This is another\n"
  1056. " long comment that\n"
  1057. " doesn't fit on one\n"
  1058. " line 1234567890\n"
  1059. " */\n"
  1060. "}",
  1061. format("{\n"
  1062. "/*\n"
  1063. "This is another "
  1064. " long comment that "
  1065. " doesn't fit on one"
  1066. " line 1234567890\n"
  1067. "*/\n"
  1068. "}", getLLVMStyleWithColumns(20)));
  1069. EXPECT_EQ("{\n"
  1070. " /*\n"
  1071. " * This i s\n"
  1072. " * another comment\n"
  1073. " * t hat doesn' t\n"
  1074. " * fit on one l i\n"
  1075. " * n e\n"
  1076. " */\n"
  1077. "}",
  1078. format("{\n"
  1079. "/*\n"
  1080. " * This i s"
  1081. " another comment"
  1082. " t hat doesn' t"
  1083. " fit on one l i"
  1084. " n e\n"
  1085. " */\n"
  1086. "}", getLLVMStyleWithColumns(20)));
  1087. EXPECT_EQ("/*\n"
  1088. " * This is a long\n"
  1089. " * comment that\n"
  1090. " * doesn't fit on\n"
  1091. " * one line\n"
  1092. " */",
  1093. format(" /*\n"
  1094. " * This is a long comment that doesn't fit on one line\n"
  1095. " */", getLLVMStyleWithColumns(20)));
  1096. EXPECT_EQ("{\n"
  1097. " if (something) /* This is a\n"
  1098. " long\n"
  1099. " comment */\n"
  1100. " ;\n"
  1101. "}",
  1102. format("{\n"
  1103. " if (something) /* This is a long comment */\n"
  1104. " ;\n"
  1105. "}",
  1106. getLLVMStyleWithColumns(30)));
  1107. EXPECT_EQ("/* A comment before\n"
  1108. " * a macro\n"
  1109. " * definition */\n"
  1110. "#define a b",
  1111. format("/* A comment before a macro definition */\n"
  1112. "#define a b",
  1113. getLLVMStyleWithColumns(20)));
  1114. EXPECT_EQ("/* some comment\n"
  1115. " * a comment\n"
  1116. "* that we break\n"
  1117. " * another comment\n"
  1118. "* we have to break\n"
  1119. "* a left comment\n"
  1120. " */",
  1121. format(" /* some comment\n"
  1122. " * a comment that we break\n"
  1123. " * another comment we have to break\n"
  1124. "* a left comment\n"
  1125. " */",
  1126. getLLVMStyleWithColumns(20)));
  1127. EXPECT_EQ("/*\n"
  1128. "\n"
  1129. "\n"
  1130. " */\n",
  1131. format(" /* \n"
  1132. " \n"
  1133. " \n"
  1134. " */\n"));
  1135. }
  1136. TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
  1137. EXPECT_EQ("#define X \\\n"
  1138. " /* \\\n"
  1139. " Test \\\n"
  1140. " Macro comment \\\n"
  1141. " with a long \\\n"
  1142. " line \\\n"
  1143. " */ \\\n"
  1144. " A + B",
  1145. format("#define X \\\n"
  1146. " /*\n"
  1147. " Test\n"
  1148. " Macro comment with a long line\n"
  1149. " */ \\\n"
  1150. " A + B",
  1151. getLLVMStyleWithColumns(20)));
  1152. EXPECT_EQ("#define X \\\n"
  1153. " /* Macro comment \\\n"
  1154. " with a long \\\n"
  1155. " line */ \\\n"
  1156. " A + B",
  1157. format("#define X \\\n"
  1158. " /* Macro comment with a long\n"
  1159. " line */ \\\n"
  1160. " A + B",
  1161. getLLVMStyleWithColumns(20)));
  1162. EXPECT_EQ("#define X \\\n"
  1163. " /* Macro comment \\\n"
  1164. " * with a long \\\n"
  1165. " * line */ \\\n"
  1166. " A + B",
  1167. format("#define X \\\n"
  1168. " /* Macro comment with a long line */ \\\n"
  1169. " A + B",
  1170. getLLVMStyleWithColumns(20)));
  1171. }
  1172. TEST_F(FormatTest, CommentsInStaticInitializers) {
  1173. EXPECT_EQ(
  1174. "static SomeType type = { aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
  1175. " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
  1176. " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
  1177. " aaaaaaaaaaaaaaaaaaaa, // comment\n"
  1178. " aaaaaaaaaaaaaaaaaaaa };",
  1179. format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
  1180. " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
  1181. " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
  1182. " aaaaaaaaaaaaaaaaaaaa , // comment\n"
  1183. " aaaaaaaaaaaaaaaaaaaa };"));
  1184. verifyFormat("static SomeType type = { aaaaaaaaaaa, // comment for aa...\n"
  1185. " bbbbbbbbbbb, ccccccccccc };");
  1186. verifyFormat("static SomeType type = { aaaaaaaaaaa,\n"
  1187. " // comment for bb....\n"
  1188. " bbbbbbbbbbb, ccccccccccc };");
  1189. verifyGoogleFormat(
  1190. "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1191. " bbbbbbbbbbb, ccccccccccc};");
  1192. verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1193. " // comment for bb....\n"
  1194. " bbbbbbbbbbb, ccccccccccc};");
  1195. verifyFormat("S s = { { a, b, c }, // Group #1\n"
  1196. " { d, e, f }, // Group #2\n"
  1197. " { g, h, i } }; // Group #3");
  1198. verifyFormat("S s = { { // Group #1\n"
  1199. " a, b, c },\n"
  1200. " { // Group #2\n"
  1201. " d, e, f },\n"
  1202. " { // Group #3\n"
  1203. " g, h, i } };");
  1204. EXPECT_EQ("S s = {\n"
  1205. " // Some comment\n"
  1206. " a,\n"
  1207. "\n"
  1208. " // Comment after empty line\n"
  1209. " b\n"
  1210. "}",
  1211. format("S s = {\n"
  1212. " // Some comment\n"
  1213. " a,\n"
  1214. " \n"
  1215. " // Comment after empty line\n"
  1216. " b\n"
  1217. "}"));
  1218. EXPECT_EQ("S s = {\n"
  1219. " /* Some comment */\n"
  1220. " a,\n"
  1221. "\n"
  1222. " /* Comment after empty line */\n"
  1223. " b\n"
  1224. "}",
  1225. format("S s = {\n"
  1226. " /* Some comment */\n"
  1227. " a,\n"
  1228. " \n"
  1229. " /* Comment after empty line */\n"
  1230. " b\n"
  1231. "}"));
  1232. verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
  1233. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1234. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1235. " 0x00, 0x00, 0x00, 0x00 // comment\n"
  1236. "};");
  1237. }
  1238. TEST_F(FormatTest, IgnoresIf0Contents) {
  1239. EXPECT_EQ("#if 0\n"
  1240. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1241. "#endif\n"
  1242. "void f() {}",
  1243. format("#if 0\n"
  1244. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1245. "#endif\n"
  1246. "void f( ) { }"));
  1247. EXPECT_EQ("#if false\n"
  1248. "void f( ) { }\n"
  1249. "#endif\n"
  1250. "void g() {}\n",
  1251. format("#if false\n"
  1252. "void f( ) { }\n"
  1253. "#endif\n"
  1254. "void g( ) { }\n"));
  1255. EXPECT_EQ("enum E {\n"
  1256. " One,\n"
  1257. " Two,\n"
  1258. "#if 0\n"
  1259. "Three,\n"
  1260. " Four,\n"
  1261. "#endif\n"
  1262. " Five\n"
  1263. "};",
  1264. format("enum E {\n"
  1265. " One,Two,\n"
  1266. "#if 0\n"
  1267. "Three,\n"
  1268. " Four,\n"
  1269. "#endif\n"
  1270. " Five};"));
  1271. EXPECT_EQ("enum F {\n"
  1272. " One,\n"
  1273. "#if 1\n"
  1274. " Two,\n"
  1275. "#if 0\n"
  1276. "Three,\n"
  1277. " Four,\n"
  1278. "#endif\n"
  1279. " Five\n"
  1280. "#endif\n"
  1281. "};",
  1282. format("enum F {\n"
  1283. "One,\n"
  1284. "#if 1\n"
  1285. "Two,\n"
  1286. "#if 0\n"
  1287. "Three,\n"
  1288. " Four,\n"
  1289. "#endif\n"
  1290. "Five\n"
  1291. "#endif\n"
  1292. "};"));
  1293. EXPECT_EQ("enum G {\n"
  1294. " One,\n"
  1295. "#if 0\n"
  1296. "Two,\n"
  1297. "#else\n"
  1298. " Three,\n"
  1299. "#endif\n"
  1300. " Four\n"
  1301. "};",
  1302. format("enum G {\n"
  1303. "One,\n"
  1304. "#if 0\n"
  1305. "Two,\n"
  1306. "#else\n"
  1307. "Three,\n"
  1308. "#endif\n"
  1309. "Four\n"
  1310. "};"));
  1311. EXPECT_EQ("enum H {\n"
  1312. " One,\n"
  1313. "#if 0\n"
  1314. "#ifdef Q\n"
  1315. "Two,\n"
  1316. "#else\n"
  1317. "Three,\n"
  1318. "#endif\n"
  1319. "#endif\n"
  1320. " Four\n"
  1321. "};",
  1322. format("enum H {\n"
  1323. "One,\n"
  1324. "#if 0\n"
  1325. "#ifdef Q\n"
  1326. "Two,\n"
  1327. "#else\n"
  1328. "Three,\n"
  1329. "#endif\n"
  1330. "#endif\n"
  1331. "Four\n"
  1332. "};"));
  1333. EXPECT_EQ("enum I {\n"
  1334. " One,\n"
  1335. "#if /* test */ 0 || 1\n"
  1336. "Two,\n"
  1337. "Three,\n"
  1338. "#endif\n"
  1339. " Four\n"
  1340. "};",
  1341. format("enum I {\n"
  1342. "One,\n"
  1343. "#if /* test */ 0 || 1\n"
  1344. "Two,\n"
  1345. "Three,\n"
  1346. "#endif\n"
  1347. "Four\n"
  1348. "};"));
  1349. EXPECT_EQ("enum J {\n"
  1350. " One,\n"
  1351. "#if 0\n"
  1352. "#if 0\n"
  1353. "Two,\n"
  1354. "#else\n"
  1355. "Three,\n"
  1356. "#endif\n"
  1357. "Four,\n"
  1358. "#endif\n"
  1359. " Five\n"
  1360. "};",
  1361. format("enum J {\n"
  1362. "One,\n"
  1363. "#if 0\n"
  1364. "#if 0\n"
  1365. "Two,\n"
  1366. "#else\n"
  1367. "Three,\n"
  1368. "#endif\n"
  1369. "Four,\n"
  1370. "#endif\n"
  1371. "Five\n"
  1372. "};"));
  1373. }
  1374. //===----------------------------------------------------------------------===//
  1375. // Tests for classes, namespaces, etc.
  1376. //===----------------------------------------------------------------------===//
  1377. TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
  1378. verifyFormat("class A {};");
  1379. }
  1380. TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
  1381. verifyFormat("class A {\n"
  1382. "public:\n"
  1383. "public: // comment\n"
  1384. "protected:\n"
  1385. "private:\n"
  1386. " void f() {}\n"
  1387. "};");
  1388. verifyGoogleFormat("class A {\n"
  1389. " public:\n"
  1390. " protected:\n"
  1391. " private:\n"
  1392. " void f() {}\n"
  1393. "};");
  1394. }
  1395. TEST_F(FormatTest, SeparatesLogicalBlocks) {
  1396. EXPECT_EQ("class A {\n"
  1397. "public:\n"
  1398. " void f();\n"
  1399. "\n"
  1400. "private:\n"
  1401. " void g() {}\n"
  1402. " // test\n"
  1403. "protected:\n"
  1404. " int h;\n"
  1405. "};",
  1406. format("class A {\n"
  1407. "public:\n"
  1408. "void f();\n"
  1409. "private:\n"
  1410. "void g() {}\n"
  1411. "// test\n"
  1412. "protected:\n"
  1413. "int h;\n"
  1414. "};"));
  1415. EXPECT_EQ("class A {\n"
  1416. "protected:\n"
  1417. "public:\n"
  1418. " void f();\n"
  1419. "};",
  1420. format("class A {\n"
  1421. "protected:\n"
  1422. "\n"
  1423. "public:\n"
  1424. "\n"
  1425. " void f();\n"
  1426. "};"));
  1427. }
  1428. TEST_F(FormatTest, FormatsClasses) {
  1429. verifyFormat("class A : public B {};");
  1430. verifyFormat("class A : public ::B {};");
  1431. verifyFormat(
  1432. "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1433. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1434. verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
  1435. " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1436. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1437. verifyFormat(
  1438. "class A : public B, public C, public D, public E, public F {};");
  1439. verifyFormat("class AAAAAAAAAAAA : public B,\n"
  1440. " public C,\n"
  1441. " public D,\n"
  1442. " public E,\n"
  1443. " public F,\n"
  1444. " public G {};");
  1445. verifyFormat("class\n"
  1446. " ReallyReallyLongClassName {\n"
  1447. " int i;\n"
  1448. "};",
  1449. getLLVMStyleWithColumns(32));
  1450. verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
  1451. " aaaaaaaaaaaaaaaa> {};");
  1452. verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
  1453. " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
  1454. " aaaaaaaaaaaaaaaaaaaaaa> {};");
  1455. }
  1456. TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
  1457. verifyFormat("class A {\n} a, b;");
  1458. verifyFormat("struct A {\n} a, b;");
  1459. verifyFormat("union A {\n} a;");
  1460. }
  1461. TEST_F(FormatTest, FormatsEnum) {
  1462. verifyFormat("enum {\n"
  1463. " Zero,\n"
  1464. " One = 1,\n"
  1465. " Two = One + 1,\n"
  1466. " Three = (One + Two),\n"
  1467. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1468. " Five = (One, Two, Three, Four, 5)\n"
  1469. "};");
  1470. verifyGoogleFormat("enum {\n"
  1471. " Zero,\n"
  1472. " One = 1,\n"
  1473. " Two = One + 1,\n"
  1474. " Three = (One + Two),\n"
  1475. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1476. " Five = (One, Two, Three, Four, 5)\n"
  1477. "};");
  1478. verifyFormat("enum Enum {};");
  1479. verifyFormat("enum {};");
  1480. verifyFormat("enum X E {} d;");
  1481. verifyFormat("enum __attribute__((...)) E {} d;");
  1482. verifyFormat("enum __declspec__((...)) E {} d;");
  1483. verifyFormat("enum X f() {\n a();\n return 42;\n}");
  1484. verifyFormat("enum {\n"
  1485. " Bar = Foo<int, int>::value\n"
  1486. "};");
  1487. }
  1488. TEST_F(FormatTest, FormatsEnumsWithErrors) {
  1489. verifyFormat("enum Type {\n"
  1490. " One = 0;\n" // These semicolons should be commas.
  1491. " Two = 1;\n"
  1492. "};");
  1493. verifyFormat("namespace n {\n"
  1494. "enum Type {\n"
  1495. " One,\n"
  1496. " Two,\n" // missing };
  1497. " int i;\n"
  1498. "}\n"
  1499. "void g() {}");
  1500. }
  1501. TEST_F(FormatTest, FormatsEnumStruct) {
  1502. verifyFormat("enum struct {\n"
  1503. " Zero,\n"
  1504. " One = 1,\n"
  1505. " Two = One + 1,\n"
  1506. " Three = (One + Two),\n"
  1507. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1508. " Five = (One, Two, Three, Four, 5)\n"
  1509. "};");
  1510. verifyFormat("enum struct Enum {};");
  1511. verifyFormat("enum struct {};");
  1512. verifyFormat("enum struct X E {} d;");
  1513. verifyFormat("enum struct __attribute__((...)) E {} d;");
  1514. verifyFormat("enum struct __declspec__((...)) E {} d;");
  1515. verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
  1516. }
  1517. TEST_F(FormatTest, FormatsEnumClass) {
  1518. verifyFormat("enum class {\n"
  1519. " Zero,\n"
  1520. " One = 1,\n"
  1521. " Two = One + 1,\n"
  1522. " Three = (One + Two),\n"
  1523. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1524. " Five = (One, Two, Three, Four, 5)\n"
  1525. "};");
  1526. verifyFormat("enum class Enum {};");
  1527. verifyFormat("enum class {};");
  1528. verifyFormat("enum class X E {} d;");
  1529. verifyFormat("enum class __attribute__((...)) E {} d;");
  1530. verifyFormat("enum class __declspec__((...)) E {} d;");
  1531. verifyFormat("enum class X f() {\n a();\n return 42;\n}");
  1532. }
  1533. TEST_F(FormatTest, FormatsEnumTypes) {
  1534. verifyFormat("enum X : int {\n"
  1535. " A,\n"
  1536. " B\n"
  1537. "};");
  1538. verifyFormat("enum X : std::uint32_t {\n"
  1539. " A,\n"
  1540. " B\n"
  1541. "};");
  1542. }
  1543. TEST_F(FormatTest, FormatsBitfields) {
  1544. verifyFormat("struct Bitfields {\n"
  1545. " unsigned sClass : 8;\n"
  1546. " unsigned ValueKind : 2;\n"
  1547. "};");
  1548. verifyFormat("struct A {\n"
  1549. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
  1550. " bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
  1551. "};");
  1552. }
  1553. TEST_F(FormatTest, FormatsNamespaces) {
  1554. verifyFormat("namespace some_namespace {\n"
  1555. "class A {};\n"
  1556. "void f() { f(); }\n"
  1557. "}");
  1558. verifyFormat("namespace {\n"
  1559. "class A {};\n"
  1560. "void f() { f(); }\n"
  1561. "}");
  1562. verifyFormat("inline namespace X {\n"
  1563. "class A {};\n"
  1564. "void f() { f(); }\n"
  1565. "}");
  1566. verifyFormat("using namespace some_namespace;\n"
  1567. "class A {};\n"
  1568. "void f() { f(); }");
  1569. // This code is more common than we thought; if we
  1570. // layout this correctly the semicolon will go into
  1571. // its own line, which is undesireable.
  1572. verifyFormat("namespace {};");
  1573. verifyFormat("namespace {\n"
  1574. "class A {};\n"
  1575. "};");
  1576. verifyFormat("namespace {\n"
  1577. "int SomeVariable = 0; // comment\n"
  1578. "} // namespace");
  1579. EXPECT_EQ("#ifndef HEADER_GUARD\n"
  1580. "#define HEADER_GUARD\n"
  1581. "namespace my_namespace {\n"
  1582. "int i;\n"
  1583. "} // my_namespace\n"
  1584. "#endif // HEADER_GUARD",
  1585. format("#ifndef HEADER_GUARD\n"
  1586. " #define HEADER_GUARD\n"
  1587. " namespace my_namespace {\n"
  1588. "int i;\n"
  1589. "} // my_namespace\n"
  1590. "#endif // HEADER_GUARD"));
  1591. FormatStyle Style = getLLVMStyle();
  1592. Style.NamespaceIndentation = FormatStyle::NI_All;
  1593. EXPECT_EQ("namespace out {\n"
  1594. " int i;\n"
  1595. " namespace in {\n"
  1596. " int i;\n"
  1597. " } // namespace\n"
  1598. "} // namespace",
  1599. format("namespace out {\n"
  1600. "int i;\n"
  1601. "namespace in {\n"
  1602. "int i;\n"
  1603. "} // namespace\n"
  1604. "} // namespace",
  1605. Style));
  1606. Style.NamespaceIndentation = FormatStyle::NI_Inner;
  1607. EXPECT_EQ("namespace out {\n"
  1608. "int i;\n"
  1609. "namespace in {\n"
  1610. " int i;\n"
  1611. "} // namespace\n"
  1612. "} // namespace",
  1613. format("namespace out {\n"
  1614. "int i;\n"
  1615. "namespace in {\n"
  1616. "int i;\n"
  1617. "} // namespace\n"
  1618. "} // namespace",
  1619. Style));
  1620. }
  1621. TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
  1622. TEST_F(FormatTest, FormatsInlineASM) {
  1623. verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
  1624. verifyFormat(
  1625. "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
  1626. " \"cpuid\\n\\t\"\n"
  1627. " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
  1628. " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
  1629. " : \"a\"(value));");
  1630. }
  1631. TEST_F(FormatTest, FormatTryCatch) {
  1632. // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll
  1633. // also not create single-line-blocks.
  1634. verifyFormat("try {\n"
  1635. " throw a * b;\n"
  1636. "}\n"
  1637. "catch (int a) {\n"
  1638. " // Do nothing.\n"
  1639. "}\n"
  1640. "catch (...) {\n"
  1641. " exit(42);\n"
  1642. "}");
  1643. // Function-level try statements.
  1644. verifyFormat("int f() try { return 4; }\n"
  1645. "catch (...) {\n"
  1646. " return 5;\n"
  1647. "}");
  1648. verifyFormat("class A {\n"
  1649. " int a;\n"
  1650. " A() try : a(0) {}\n"
  1651. " catch (...) {\n"
  1652. " throw;\n"
  1653. " }\n"
  1654. "};\n");
  1655. }
  1656. TEST_F(FormatTest, FormatObjCTryCatch) {
  1657. verifyFormat("@try {\n"
  1658. " f();\n"
  1659. "}\n"
  1660. "@catch (NSException e) {\n"
  1661. " @throw;\n"
  1662. "}\n"
  1663. "@finally {\n"
  1664. " exit(42);\n"
  1665. "}");
  1666. }
  1667. TEST_F(FormatTest, StaticInitializers) {
  1668. verifyFormat("static SomeClass SC = { 1, 'a' };");
  1669. verifyFormat(
  1670. "static SomeClass WithALoooooooooooooooooooongName = {\n"
  1671. " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
  1672. "};");
  1673. // Here, everything other than the "}" would fit on a line.
  1674. verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
  1675. " 100000000000000000000000\n"
  1676. "};");
  1677. EXPECT_EQ("S s = { a, b };", format("S s = {\n"
  1678. " a,\n"
  1679. "\n"
  1680. " b\n"
  1681. "};"));
  1682. // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
  1683. // line. However, the formatting looks a bit off and this probably doesn't
  1684. // happen often in practice.
  1685. verifyFormat("static int Variable[1] = {\n"
  1686. " { 1000000000000000000000000000000000000 }\n"
  1687. "};",
  1688. getLLVMStyleWithColumns(40));
  1689. }
  1690. TEST_F(FormatTest, DesignatedInitializers) {
  1691. verifyFormat("const struct A a = { .a = 1, .b = 2 };");
  1692. verifyFormat("const struct A a = { .aaaaaaaaaa = 1,\n"
  1693. " .bbbbbbbbbb = 2,\n"
  1694. " .cccccccccc = 3,\n"
  1695. " .dddddddddd = 4,\n"
  1696. " .eeeeeeeeee = 5 };");
  1697. verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
  1698. " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
  1699. " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
  1700. " .ccccccccccccccccccccccccccc = 3,\n"
  1701. " .ddddddddddddddddddddddddddd = 4,\n"
  1702. " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5\n"
  1703. "};");
  1704. verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
  1705. }
  1706. TEST_F(FormatTest, NestedStaticInitializers) {
  1707. verifyFormat("static A x = { { {} } };\n");
  1708. verifyFormat("static A x = { { { init1, init2, init3, init4 },\n"
  1709. " { init1, init2, init3, init4 } } };");
  1710. verifyFormat("somes Status::global_reps[3] = {\n"
  1711. " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n"
  1712. " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n"
  1713. " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n"
  1714. "};");
  1715. verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
  1716. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  1717. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  1718. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
  1719. verifyFormat(
  1720. "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n"
  1721. " { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop"
  1722. " } };");
  1723. verifyFormat(
  1724. "SomeArrayOfSomeType a = { { { 1, 2, 3 }, { 1, 2, 3 },\n"
  1725. " { 111111111111111111111111111111,\n"
  1726. " 222222222222222222222222222222,\n"
  1727. " 333333333333333333333333333333 },\n"
  1728. " { 1, 2, 3 }, { 1, 2, 3 } } };");
  1729. verifyFormat(
  1730. "SomeArrayOfSomeType a = { { { 1, 2, 3 } }, { { 1, 2, 3 } },\n"
  1731. " { { 111111111111111111111111111111,\n"
  1732. " 222222222222222222222222222222,\n"
  1733. " 333333333333333333333333333333 } },\n"
  1734. " { { 1, 2, 3 } }, { { 1, 2, 3 } } };");
  1735. verifyGoogleFormat(
  1736. "SomeArrayOfSomeType a = {\n"
  1737. " {{1, 2, 3}}, {{1, 2, 3}},\n"
  1738. " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
  1739. " 333333333333333333333333333333}},\n"
  1740. " {{1, 2, 3}}, {{1, 2, 3}}};");
  1741. // FIXME: We might at some point want to handle this similar to parameter
  1742. // lists, where we have an option to put each on a single line.
  1743. verifyFormat(
  1744. "struct {\n"
  1745. " unsigned bit;\n"
  1746. " const char *const name;\n"
  1747. "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n"
  1748. " { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };");
  1749. }
  1750. TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
  1751. verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  1752. " \\\n"
  1753. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
  1754. }
  1755. TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
  1756. verifyFormat("virtual void write(ELFWriter *writerrr,\n"
  1757. " OwningPtr<FileOutputBuffer> &buffer) = 0;");
  1758. }
  1759. TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
  1760. verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
  1761. getLLVMStyleWithColumns(40));
  1762. verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  1763. getLLVMStyleWithColumns(40));
  1764. EXPECT_EQ("#define Q \\\n"
  1765. " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n"
  1766. " \"aaaaaaaa.cpp\"",
  1767. format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  1768. getLLVMStyleWithColumns(40)));
  1769. }
  1770. TEST_F(FormatTest, UnderstandsLinePPDirective) {
  1771. EXPECT_EQ("# 123 \"A string literal\"",
  1772. format(" # 123 \"A string literal\""));
  1773. }
  1774. TEST_F(FormatTest, LayoutUnknownPPDirective) {
  1775. EXPECT_EQ("#;", format("#;"));
  1776. verifyFormat("#\n;\n;\n;");
  1777. }
  1778. TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
  1779. EXPECT_EQ("#line 42 \"test\"\n",
  1780. format("# \\\n line \\\n 42 \\\n \"test\"\n"));
  1781. EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n",
  1782. getLLVMStyleWithColumns(12)));
  1783. }
  1784. TEST_F(FormatTest, EndOfFileEndsPPDirective) {
  1785. EXPECT_EQ("#line 42 \"test\"",
  1786. format("# \\\n line \\\n 42 \\\n \"test\""));
  1787. EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B"));
  1788. }
  1789. TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
  1790. verifyFormat("#define A \\x20");
  1791. verifyFormat("#define A \\ x20");
  1792. EXPECT_EQ("#define A \\ x20", format("#define A \\ x20"));
  1793. verifyFormat("#define A ''");
  1794. verifyFormat("#define A ''qqq");
  1795. verifyFormat("#define A `qqq");
  1796. verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
  1797. }
  1798. TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
  1799. verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
  1800. verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
  1801. verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
  1802. // FIXME: We never break before the macro name.
  1803. verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
  1804. verifyFormat("#define A A\n#define A A");
  1805. verifyFormat("#define A(X) A\n#define A A");
  1806. verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
  1807. verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
  1808. }
  1809. TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
  1810. EXPECT_EQ("// somecomment\n"
  1811. "#include \"a.h\"\n"
  1812. "#define A( \\\n"
  1813. " A, B)\n"
  1814. "#include \"b.h\"\n"
  1815. "// somecomment\n",
  1816. format(" // somecomment\n"
  1817. " #include \"a.h\"\n"
  1818. "#define A(A,\\\n"
  1819. " B)\n"
  1820. " #include \"b.h\"\n"
  1821. " // somecomment\n",
  1822. getLLVMStyleWithColumns(13)));
  1823. }
  1824. TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
  1825. TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
  1826. EXPECT_EQ("#define A \\\n"
  1827. " c; \\\n"
  1828. " e;\n"
  1829. "f;",
  1830. format("#define A c; e;\n"
  1831. "f;",
  1832. getLLVMStyleWithColumns(14)));
  1833. }
  1834. TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
  1835. TEST_F(FormatTest, AlwaysFormatsEntireMacroDefinitions) {
  1836. EXPECT_EQ("int i;\n"
  1837. "#define A \\\n"
  1838. " int i; \\\n"
  1839. " int j\n"
  1840. "int k;",
  1841. format("int i;\n"
  1842. "#define A \\\n"
  1843. " int i ; \\\n"
  1844. " int j\n"
  1845. "int k;",
  1846. 8, 0, getGoogleStyle())); // 8: position of "#define".
  1847. EXPECT_EQ("int i;\n"
  1848. "#define A \\\n"
  1849. " int i; \\\n"
  1850. " int j\n"
  1851. "int k;",
  1852. format("int i;\n"
  1853. "#define A \\\n"
  1854. " int i ; \\\n"
  1855. " int j\n"
  1856. "int k;",
  1857. 45, 0, getGoogleStyle())); // 45: position of "j".
  1858. }
  1859. TEST_F(FormatTest, MacroDefinitionInsideStatement) {
  1860. EXPECT_EQ("int x,\n"
  1861. "#define A\n"
  1862. " y;",
  1863. format("int x,\n#define A\ny;"));
  1864. }
  1865. TEST_F(FormatTest, HashInMacroDefinition) {
  1866. EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
  1867. verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
  1868. verifyFormat("#define A \\\n"
  1869. " { \\\n"
  1870. " f(#c); \\\n"
  1871. " }",
  1872. getLLVMStyleWithColumns(11));
  1873. verifyFormat("#define A(X) \\\n"
  1874. " void function##X()",
  1875. getLLVMStyleWithColumns(22));
  1876. verifyFormat("#define A(a, b, c) \\\n"
  1877. " void a##b##c()",
  1878. getLLVMStyleWithColumns(22));
  1879. verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
  1880. }
  1881. TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
  1882. verifyFormat("#define A (1)");
  1883. }
  1884. TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
  1885. EXPECT_EQ("#define A b;", format("#define A \\\n"
  1886. " \\\n"
  1887. " b;",
  1888. getLLVMStyleWithColumns(25)));
  1889. EXPECT_EQ("#define A \\\n"
  1890. " \\\n"
  1891. " a; \\\n"
  1892. " b;",
  1893. format("#define A \\\n"
  1894. " \\\n"
  1895. " a; \\\n"
  1896. " b;",
  1897. getLLVMStyleWithColumns(11)));
  1898. EXPECT_EQ("#define A \\\n"
  1899. " a; \\\n"
  1900. " \\\n"
  1901. " b;",
  1902. format("#define A \\\n"
  1903. " a; \\\n"
  1904. " \\\n"
  1905. " b;",
  1906. getLLVMStyleWithColumns(11)));
  1907. }
  1908. TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
  1909. verifyFormat("#define A :");
  1910. verifyFormat("#define SOMECASES \\\n"
  1911. " case 1: \\\n"
  1912. " case 2\n",
  1913. getLLVMStyleWithColumns(20));
  1914. verifyFormat("#define A template <typename T>");
  1915. verifyFormat("#define STR(x) #x\n"
  1916. "f(STR(this_is_a_string_literal{));");
  1917. verifyFormat("#pragma omp threadprivate( \\\n"
  1918. " y)), // expected-warning",
  1919. getLLVMStyleWithColumns(28));
  1920. }
  1921. TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
  1922. verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
  1923. EXPECT_EQ("class A : public QObject {\n"
  1924. " Q_OBJECT\n"
  1925. "\n"
  1926. " A() {}\n"
  1927. "};",
  1928. format("class A : public QObject {\n"
  1929. " Q_OBJECT\n"
  1930. "\n"
  1931. " A() {\n}\n"
  1932. "} ;"));
  1933. EXPECT_EQ("SOME_MACRO\n"
  1934. "namespace {\n"
  1935. "void f();\n"
  1936. "}",
  1937. format("SOME_MACRO\n"
  1938. " namespace {\n"
  1939. "void f( );\n"
  1940. "}"));
  1941. // Only if the identifier contains at least 5 characters.
  1942. EXPECT_EQ("HTTP f();",
  1943. format("HTTP\nf();"));
  1944. EXPECT_EQ("MACRO\nf();",
  1945. format("MACRO\nf();"));
  1946. // Only if everything is upper case.
  1947. EXPECT_EQ("class A : public QObject {\n"
  1948. " Q_Object A() {}\n"
  1949. "};",
  1950. format("class A : public QObject {\n"
  1951. " Q_Object\n"
  1952. "\n"
  1953. " A() {\n}\n"
  1954. "} ;"));
  1955. }
  1956. TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
  1957. EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  1958. "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  1959. "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  1960. "class X {};\n"
  1961. "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  1962. "int *createScopDetectionPass() { return 0; }",
  1963. format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  1964. " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  1965. " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  1966. " class X {};\n"
  1967. " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  1968. " int *createScopDetectionPass() { return 0; }"));
  1969. // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
  1970. // braces, so that inner block is indented one level more.
  1971. EXPECT_EQ("int q() {\n"
  1972. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  1973. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  1974. " IPC_END_MESSAGE_MAP()\n"
  1975. "}",
  1976. format("int q() {\n"
  1977. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  1978. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  1979. " IPC_END_MESSAGE_MAP()\n"
  1980. "}"));
  1981. // These must not be recognized as macros.
  1982. EXPECT_EQ("int q() {\n"
  1983. " f(x);\n"
  1984. " f(x) {}\n"
  1985. " f(x)->g();\n"
  1986. " f(x)->*g();\n"
  1987. " f(x).g();\n"
  1988. " f(x) = x;\n"
  1989. " f(x) += x;\n"
  1990. " f(x) -= x;\n"
  1991. " f(x) *= x;\n"
  1992. " f(x) /= x;\n"
  1993. " f(x) %= x;\n"
  1994. " f(x) &= x;\n"
  1995. " f(x) |= x;\n"
  1996. " f(x) ^= x;\n"
  1997. " f(x) >>= x;\n"
  1998. " f(x) <<= x;\n"
  1999. " f(x)[y].z();\n"
  2000. " LOG(INFO) << x;\n"
  2001. " ifstream(x) >> x;\n"
  2002. "}\n",
  2003. format("int q() {\n"
  2004. " f(x)\n;\n"
  2005. " f(x)\n {}\n"
  2006. " f(x)\n->g();\n"
  2007. " f(x)\n->*g();\n"
  2008. " f(x)\n.g();\n"
  2009. " f(x)\n = x;\n"
  2010. " f(x)\n += x;\n"
  2011. " f(x)\n -= x;\n"
  2012. " f(x)\n *= x;\n"
  2013. " f(x)\n /= x;\n"
  2014. " f(x)\n %= x;\n"
  2015. " f(x)\n &= x;\n"
  2016. " f(x)\n |= x;\n"
  2017. " f(x)\n ^= x;\n"
  2018. " f(x)\n >>= x;\n"
  2019. " f(x)\n <<= x;\n"
  2020. " f(x)\n[y].z();\n"
  2021. " LOG(INFO)\n << x;\n"
  2022. " ifstream(x)\n >> x;\n"
  2023. "}\n"));
  2024. EXPECT_EQ("int q() {\n"
  2025. " f(x)\n"
  2026. " if (1) {\n"
  2027. " }\n"
  2028. " f(x)\n"
  2029. " while (1) {\n"
  2030. " }\n"
  2031. " f(x)\n"
  2032. " g(x);\n"
  2033. " f(x)\n"
  2034. " try {\n"
  2035. " q();\n"
  2036. " }\n"
  2037. " catch (...) {\n"
  2038. " }\n"
  2039. "}\n",
  2040. format("int q() {\n"
  2041. "f(x)\n"
  2042. "if (1) {}\n"
  2043. "f(x)\n"
  2044. "while (1) {}\n"
  2045. "f(x)\n"
  2046. "g(x);\n"
  2047. "f(x)\n"
  2048. "try { q(); } catch (...) {}\n"
  2049. "}\n"));
  2050. EXPECT_EQ("class A {\n"
  2051. " A() : t(0) {}\n"
  2052. " A(X x)\n" // FIXME: function-level try blocks are broken.
  2053. " try : t(0) {\n"
  2054. " }\n"
  2055. " catch (...) {\n"
  2056. " }\n"
  2057. "};",
  2058. format("class A {\n"
  2059. " A()\n : t(0) {}\n"
  2060. " A(X x)\n"
  2061. " try : t(0) {} catch (...) {}\n"
  2062. "};"));
  2063. }
  2064. TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
  2065. verifyFormat("#define A \\\n"
  2066. " f({ \\\n"
  2067. " g(); \\\n"
  2068. " });", getLLVMStyleWithColumns(11));
  2069. }
  2070. TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
  2071. EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
  2072. }
  2073. TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
  2074. verifyFormat("{\n { a #c; }\n}");
  2075. }
  2076. TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
  2077. EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
  2078. format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
  2079. EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
  2080. format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
  2081. }
  2082. TEST_F(FormatTest, EscapedNewlineAtStartOfToken) {
  2083. EXPECT_EQ(
  2084. "#define A \\\n int i; \\\n int j;",
  2085. format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
  2086. EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
  2087. }
  2088. TEST_F(FormatTest, NoEscapedNewlineHandlingInBlockComments) {
  2089. EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/"));
  2090. }
  2091. TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
  2092. verifyFormat("#define A \\\n"
  2093. " int v( \\\n"
  2094. " a); \\\n"
  2095. " int i;",
  2096. getLLVMStyleWithColumns(11));
  2097. }
  2098. TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
  2099. EXPECT_EQ(
  2100. "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2101. " \\\n"
  2102. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2103. "\n"
  2104. "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2105. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
  2106. format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2107. "\\\n"
  2108. "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2109. " \n"
  2110. " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2111. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
  2112. }
  2113. TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
  2114. EXPECT_EQ("int\n"
  2115. "#define A\n"
  2116. " a;",
  2117. format("int\n#define A\na;", getGoogleStyle()));
  2118. verifyFormat("functionCallTo(\n"
  2119. " someOtherFunction(\n"
  2120. " withSomeParameters, whichInSequence,\n"
  2121. " areLongerThanALine(andAnotherCall,\n"
  2122. "#define A B\n"
  2123. " withMoreParamters,\n"
  2124. " whichStronglyInfluenceTheLayout),\n"
  2125. " andMoreParameters),\n"
  2126. " trailing);",
  2127. getLLVMStyleWithColumns(69));
  2128. verifyFormat("Foo::Foo()\n"
  2129. "#ifdef BAR\n"
  2130. " : baz(0)\n"
  2131. "#endif\n"
  2132. "{\n"
  2133. "}");
  2134. verifyFormat("void f() {\n"
  2135. " if (true)\n"
  2136. "#ifdef A\n"
  2137. " f(42);\n"
  2138. " x();\n"
  2139. "#else\n"
  2140. " g();\n"
  2141. " x();\n"
  2142. "#endif\n"
  2143. "}");
  2144. verifyFormat("void f(param1, param2,\n"
  2145. " param3,\n"
  2146. "#ifdef A\n"
  2147. " param4(param5,\n"
  2148. "#ifdef A1\n"
  2149. " param6,\n"
  2150. "#ifdef A2\n"
  2151. " param7),\n"
  2152. "#else\n"
  2153. " param8),\n"
  2154. " param9,\n"
  2155. "#endif\n"
  2156. " param10,\n"
  2157. "#endif\n"
  2158. " param11)\n"
  2159. "#else\n"
  2160. " param12)\n"
  2161. "#endif\n"
  2162. "{\n"
  2163. " x();\n"
  2164. "}",
  2165. getLLVMStyleWithColumns(28));
  2166. verifyFormat("#if 1\n"
  2167. "int i;");
  2168. verifyFormat(
  2169. "#if 1\n"
  2170. "#endif\n"
  2171. "#if 1\n"
  2172. "#else\n"
  2173. "#endif\n");
  2174. }
  2175. TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
  2176. FormatStyle SingleLine = getLLVMStyle();
  2177. SingleLine.AllowShortIfStatementsOnASingleLine = true;
  2178. verifyFormat(
  2179. "#if 0\n"
  2180. "#elif 1\n"
  2181. "#endif\n"
  2182. "void foo() {\n"
  2183. " if (test) foo2();\n"
  2184. "}",
  2185. SingleLine);
  2186. }
  2187. TEST_F(FormatTest, LayoutBlockInsideParens) {
  2188. EXPECT_EQ("functionCall({ int i; });", format(" functionCall ( {int i;} );"));
  2189. EXPECT_EQ("functionCall({\n"
  2190. " int i;\n"
  2191. " int j;\n"
  2192. "});",
  2193. format(" functionCall ( {int i;int j;} );"));
  2194. EXPECT_EQ("functionCall({\n"
  2195. " int i;\n"
  2196. " int j;\n"
  2197. " },\n"
  2198. " aaaa, bbbb, cccc);",
  2199. format(" functionCall ( {int i;int j;}, aaaa, bbbb, cccc);"));
  2200. EXPECT_EQ("functionCall(aaaa, bbbb, { int i; });",
  2201. format(" functionCall (aaaa, bbbb, {int i;});"));
  2202. EXPECT_EQ("functionCall(aaaa, bbbb, {\n"
  2203. " int i;\n"
  2204. " int j;\n"
  2205. "});",
  2206. format(" functionCall (aaaa, bbbb, {int i;int j;});"));
  2207. EXPECT_EQ("functionCall(aaaa, bbbb, { int i; });",
  2208. format(" functionCall (aaaa, bbbb, {int i;});"));
  2209. verifyFormat(
  2210. "Aaa({\n"
  2211. " int i; // break\n"
  2212. " },\n"
  2213. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  2214. " ccccccccccccccccc));");
  2215. verifyFormat("DEBUG({\n"
  2216. " if (a)\n"
  2217. " f();\n"
  2218. "});");
  2219. }
  2220. TEST_F(FormatTest, LayoutBlockInsideStatement) {
  2221. EXPECT_EQ("SOME_MACRO { int i; }\n"
  2222. "int i;",
  2223. format(" SOME_MACRO {int i;} int i;"));
  2224. }
  2225. TEST_F(FormatTest, LayoutNestedBlocks) {
  2226. verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
  2227. " struct s {\n"
  2228. " int i;\n"
  2229. " };\n"
  2230. " s kBitsToOs[] = { { 10 } };\n"
  2231. " for (int i = 0; i < 10; ++i)\n"
  2232. " return;\n"
  2233. "}");
  2234. verifyFormat("call(parameter, {\n"
  2235. " something();\n"
  2236. " // Comment using all columns.\n"
  2237. " somethingelse();\n"
  2238. "});",
  2239. getLLVMStyleWithColumns(40));
  2240. EXPECT_EQ("call(parameter, {\n"
  2241. " something();\n"
  2242. " // Comment too\n"
  2243. " // looooooooooong.\n"
  2244. " somethingElse();\n"
  2245. "});",
  2246. format("call(parameter, {\n"
  2247. " something();\n"
  2248. " // Comment too looooooooooong.\n"
  2249. " somethingElse();\n"
  2250. "});",
  2251. getLLVMStyleWithColumns(29)));
  2252. EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });"));
  2253. EXPECT_EQ("DEBUG({ // comment\n"
  2254. " int i;\n"
  2255. "});",
  2256. format("DEBUG({ // comment\n"
  2257. "int i;\n"
  2258. "});"));
  2259. EXPECT_EQ("DEBUG({\n"
  2260. " int i;\n"
  2261. "\n"
  2262. " // comment\n"
  2263. " int j;\n"
  2264. "});",
  2265. format("DEBUG({\n"
  2266. " int i;\n"
  2267. "\n"
  2268. " // comment\n"
  2269. " int j;\n"
  2270. "});"));
  2271. }
  2272. TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
  2273. EXPECT_EQ("{}", format("{}"));
  2274. verifyFormat("enum E {};");
  2275. verifyFormat("enum E {}");
  2276. }
  2277. //===----------------------------------------------------------------------===//
  2278. // Line break tests.
  2279. //===----------------------------------------------------------------------===//
  2280. TEST_F(FormatTest, PreventConfusingIndents) {
  2281. verifyFormat(
  2282. "SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
  2283. " parameter, parameter, parameter)),\n"
  2284. " SecondLongCall(parameter));");
  2285. verifyFormat(
  2286. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2287. " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2288. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2289. " aaaaaaaaaaaaaaaaaaaaaaaa);");
  2290. verifyFormat(
  2291. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2292. " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
  2293. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  2294. " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
  2295. verifyFormat(
  2296. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  2297. " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
  2298. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
  2299. " aaaaaaaaaaaaaaaaaaaaaaaa>;");
  2300. verifyFormat("int a = bbbb && ccc && fffff(\n"
  2301. "#define A Just forcing a new line\n"
  2302. " ddd);");
  2303. }
  2304. TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
  2305. verifyFormat(
  2306. "bool aaaaaaa =\n"
  2307. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
  2308. " bbbbbbbb();");
  2309. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  2310. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
  2311. " ccccccccc == ddddddddddd;");
  2312. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  2313. " aaaaaa) &&\n"
  2314. " bbbbbb && cccccc;");
  2315. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  2316. " aaaaaa) >>\n"
  2317. " bbbbbb;");
  2318. verifyFormat("Whitespaces.addUntouchableComment(\n"
  2319. " SourceMgr.getSpellingColumnNumber(\n"
  2320. " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
  2321. " 1);");
  2322. verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2323. " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
  2324. " cccccc) {\n}");
  2325. // If the LHS of a comparison is not a binary expression itself, the
  2326. // additional linebreak confuses many people.
  2327. verifyFormat(
  2328. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2329. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
  2330. "}");
  2331. verifyFormat(
  2332. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2333. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  2334. "}");
  2335. verifyFormat(
  2336. "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
  2337. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  2338. "}");
  2339. // Even explicit parentheses stress the precedence enough to make the
  2340. // additional break unnecessary.
  2341. verifyFormat(
  2342. "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2343. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  2344. "}");
  2345. // This cases is borderline, but with the indentation it is still readable.
  2346. verifyFormat(
  2347. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2348. " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2349. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  2350. "}",
  2351. getLLVMStyleWithColumns(75));
  2352. // If the LHS is a binary expression, we should still use the additional break
  2353. // as otherwise the formatting hides the operator precedence.
  2354. verifyFormat(
  2355. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2356. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2357. " 5) {\n"
  2358. "}");
  2359. FormatStyle OnePerLine = getLLVMStyle();
  2360. OnePerLine.BinPackParameters = false;
  2361. verifyFormat(
  2362. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2363. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2364. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
  2365. OnePerLine);
  2366. }
  2367. TEST_F(FormatTest, ExpressionIndentation) {
  2368. verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2369. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2370. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2371. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2372. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
  2373. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
  2374. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2375. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
  2376. " ccccccccccccccccccccccccccccccccccccccccc;");
  2377. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2378. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2379. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2380. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  2381. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2382. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2383. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2384. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  2385. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2386. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2387. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2388. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  2389. verifyFormat("if () {\n"
  2390. "} else if (aaaaa && bbbbb > // break\n"
  2391. " ccccc) {\n"
  2392. "}");
  2393. // Presence of a trailing comment used to change indentation of b.
  2394. verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
  2395. " b;\n"
  2396. "return aaaaaaaaaaaaaaaaaaa +\n"
  2397. " b; //",
  2398. getLLVMStyleWithColumns(30));
  2399. }
  2400. TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
  2401. // Not sure what the best system is here. Like this, the LHS can be found
  2402. // immediately above an operator (everything with the same or a higher
  2403. // indent). The RHS is aligned right of the operator and so compasses
  2404. // everything until something with the same indent as the operator is found.
  2405. // FIXME: Is this a good system?
  2406. FormatStyle Style = getLLVMStyle();
  2407. Style.BreakBeforeBinaryOperators = true;
  2408. verifyFormat(
  2409. "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2410. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2411. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2412. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2413. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  2414. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  2415. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2416. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2417. " > ccccccccccccccccccccccccccccccccccccccccc;",
  2418. Style);
  2419. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2420. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2421. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2422. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  2423. Style);
  2424. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2425. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2426. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2427. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  2428. Style);
  2429. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2430. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2431. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2432. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  2433. Style);
  2434. verifyFormat("if () {\n"
  2435. "} else if (aaaaa && bbbbb // break\n"
  2436. " > ccccc) {\n"
  2437. "}",
  2438. Style);
  2439. }
  2440. TEST_F(FormatTest, ConstructorInitializers) {
  2441. verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
  2442. verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
  2443. getLLVMStyleWithColumns(45));
  2444. verifyFormat("Constructor()\n"
  2445. " : Inttializer(FitsOnTheLine) {}",
  2446. getLLVMStyleWithColumns(44));
  2447. verifyFormat("Constructor()\n"
  2448. " : Inttializer(FitsOnTheLine) {}",
  2449. getLLVMStyleWithColumns(43));
  2450. verifyFormat(
  2451. "SomeClass::Constructor()\n"
  2452. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  2453. verifyFormat(
  2454. "SomeClass::Constructor()\n"
  2455. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  2456. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
  2457. verifyFormat(
  2458. "SomeClass::Constructor()\n"
  2459. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2460. " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  2461. verifyFormat("Constructor()\n"
  2462. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2463. " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2464. " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2465. " aaaaaaaaaaaaaaaaaaaaaaa() {}");
  2466. verifyFormat("Constructor()\n"
  2467. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2468. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  2469. verifyFormat("Constructor(int Parameter = 0)\n"
  2470. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
  2471. " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
  2472. verifyFormat("Constructor()\n"
  2473. " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
  2474. "}",
  2475. getLLVMStyleWithColumns(60));
  2476. // Here a line could be saved by splitting the second initializer onto two
  2477. // lines, but that is not desireable.
  2478. verifyFormat("Constructor()\n"
  2479. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2480. " aaaaaaaaaaa(aaaaaaaaaaa),\n"
  2481. " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  2482. FormatStyle OnePerLine = getLLVMStyle();
  2483. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  2484. verifyFormat("SomeClass::Constructor()\n"
  2485. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  2486. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  2487. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  2488. OnePerLine);
  2489. verifyFormat("SomeClass::Constructor()\n"
  2490. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
  2491. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  2492. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  2493. OnePerLine);
  2494. verifyFormat("MyClass::MyClass(int var)\n"
  2495. " : some_var_(var), // 4 space indent\n"
  2496. " some_other_var_(var + 1) { // lined up\n"
  2497. "}",
  2498. OnePerLine);
  2499. verifyFormat("Constructor()\n"
  2500. " : aaaaa(aaaaaa),\n"
  2501. " aaaaa(aaaaaa),\n"
  2502. " aaaaa(aaaaaa),\n"
  2503. " aaaaa(aaaaaa),\n"
  2504. " aaaaa(aaaaaa) {}",
  2505. OnePerLine);
  2506. verifyFormat("Constructor()\n"
  2507. " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
  2508. " aaaaaaaaaaaaaaaaaaaaaa) {}",
  2509. OnePerLine);
  2510. }
  2511. TEST_F(FormatTest, MemoizationTests) {
  2512. // This breaks if the memoization lookup does not take \c Indent and
  2513. // \c LastSpace into account.
  2514. verifyFormat(
  2515. "extern CFRunLoopTimerRef\n"
  2516. "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
  2517. " CFTimeInterval interval, CFOptionFlags flags,\n"
  2518. " CFIndex order, CFRunLoopTimerCallBack callout,\n"
  2519. " CFRunLoopTimerContext *context) {}");
  2520. // Deep nesting somewhat works around our memoization.
  2521. verifyFormat(
  2522. "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  2523. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  2524. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  2525. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  2526. " aaaaa())))))))))))))))))))))))))))))))))))))));",
  2527. getLLVMStyleWithColumns(65));
  2528. verifyFormat(
  2529. "aaaaa(\n"
  2530. " aaaaa,\n"
  2531. " aaaaa(\n"
  2532. " aaaaa,\n"
  2533. " aaaaa(\n"
  2534. " aaaaa,\n"
  2535. " aaaaa(\n"
  2536. " aaaaa,\n"
  2537. " aaaaa(\n"
  2538. " aaaaa,\n"
  2539. " aaaaa(\n"
  2540. " aaaaa,\n"
  2541. " aaaaa(\n"
  2542. " aaaaa,\n"
  2543. " aaaaa(\n"
  2544. " aaaaa,\n"
  2545. " aaaaa(\n"
  2546. " aaaaa,\n"
  2547. " aaaaa(\n"
  2548. " aaaaa,\n"
  2549. " aaaaa(\n"
  2550. " aaaaa,\n"
  2551. " aaaaa(\n"
  2552. " aaaaa,\n"
  2553. " aaaaa))))))))))));",
  2554. getLLVMStyleWithColumns(65));
  2555. verifyFormat(
  2556. "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"
  2557. " a),\n"
  2558. " a),\n"
  2559. " a),\n"
  2560. " a),\n"
  2561. " a),\n"
  2562. " a),\n"
  2563. " a),\n"
  2564. " a),\n"
  2565. " a),\n"
  2566. " a),\n"
  2567. " a),\n"
  2568. " a),\n"
  2569. " a),\n"
  2570. " a),\n"
  2571. " a),\n"
  2572. " a),\n"
  2573. " a)",
  2574. getLLVMStyleWithColumns(65));
  2575. // This test takes VERY long when memoization is broken.
  2576. FormatStyle OnePerLine = getLLVMStyle();
  2577. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  2578. OnePerLine.BinPackParameters = false;
  2579. std::string input = "Constructor()\n"
  2580. " : aaaa(a,\n";
  2581. for (unsigned i = 0, e = 80; i != e; ++i) {
  2582. input += " a,\n";
  2583. }
  2584. input += " a) {}";
  2585. verifyFormat(input, OnePerLine);
  2586. }
  2587. TEST_F(FormatTest, BreaksAsHighAsPossible) {
  2588. verifyFormat(
  2589. "void f() {\n"
  2590. " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
  2591. " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
  2592. " f();\n"
  2593. "}");
  2594. verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
  2595. " Intervals[i - 1].getRange().getLast()) {\n}");
  2596. }
  2597. TEST_F(FormatTest, BreaksFunctionDeclarations) {
  2598. // Principially, we break function declarations in a certain order:
  2599. // 1) break amongst arguments.
  2600. verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
  2601. " Cccccccccccccc cccccccccccccc);");
  2602. verifyFormat(
  2603. "template <class TemplateIt>\n"
  2604. "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
  2605. " TemplateIt *stop) {}");
  2606. // 2) break after return type.
  2607. verifyFormat(
  2608. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2609. " bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
  2610. getGoogleStyle());
  2611. // 3) break after (.
  2612. verifyFormat(
  2613. "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
  2614. " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
  2615. getGoogleStyle());
  2616. // 4) break before after nested name specifiers.
  2617. verifyFormat(
  2618. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2619. " SomeClasssssssssssssssssssssssssssssssssssssss::\n"
  2620. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
  2621. getGoogleStyle());
  2622. // However, there are exceptions, if a sufficient amount of lines can be
  2623. // saved.
  2624. // FIXME: The precise cut-offs wrt. the number of saved lines might need some
  2625. // more adjusting.
  2626. verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  2627. " Cccccccccccccc cccccccccc,\n"
  2628. " Cccccccccccccc cccccccccc,\n"
  2629. " Cccccccccccccc cccccccccc,\n"
  2630. " Cccccccccccccc cccccccccc);");
  2631. verifyFormat(
  2632. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2633. " bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  2634. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  2635. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
  2636. getGoogleStyle());
  2637. verifyFormat(
  2638. "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  2639. " Cccccccccccccc cccccccccc,\n"
  2640. " Cccccccccccccc cccccccccc,\n"
  2641. " Cccccccccccccc cccccccccc,\n"
  2642. " Cccccccccccccc cccccccccc,\n"
  2643. " Cccccccccccccc cccccccccc,\n"
  2644. " Cccccccccccccc cccccccccc);");
  2645. verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  2646. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  2647. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  2648. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  2649. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
  2650. // Break after multi-line parameters.
  2651. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2652. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2653. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2654. " bbbb bbbb);");
  2655. // Treat overloaded operators like other functions.
  2656. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  2657. "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
  2658. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  2659. "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
  2660. verifyGoogleFormat(
  2661. "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
  2662. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  2663. }
  2664. TEST_F(FormatTest, TrailingReturnType) {
  2665. verifyFormat("auto foo() -> int;\n");
  2666. verifyFormat("struct S {\n"
  2667. " auto bar() const -> int;\n"
  2668. "};");
  2669. verifyFormat("template <size_t Order, typename T>\n"
  2670. "auto load_img(const std::string &filename)\n"
  2671. " -> alias::tensor<Order, T, mem::tag::cpu> {}");
  2672. // Not trailing return types.
  2673. verifyFormat("void f() { auto a = b->c(); }");
  2674. }
  2675. TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
  2676. // Avoid breaking before trailing 'const' or other trailing annotations, if
  2677. // they are not function-like.
  2678. FormatStyle Style = getGoogleStyle();
  2679. Style.ColumnLimit = 47;
  2680. verifyFormat("void\n"
  2681. "someLongFunction(int someLongParameter) const {\n}",
  2682. getLLVMStyleWithColumns(47));
  2683. verifyFormat("LoooooongReturnType\n"
  2684. "someLoooooooongFunction() const {}",
  2685. getLLVMStyleWithColumns(47));
  2686. verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
  2687. " const {}",
  2688. Style);
  2689. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  2690. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
  2691. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  2692. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
  2693. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  2694. " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
  2695. // Unless this would lead to the first parameter being broken.
  2696. verifyFormat("void someLongFunction(int someLongParameter)\n"
  2697. " const {}",
  2698. getLLVMStyleWithColumns(46));
  2699. verifyFormat("void someLongFunction(int someLongParameter)\n"
  2700. " const {}",
  2701. Style);
  2702. verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
  2703. " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2704. " LONG_AND_UGLY_ANNOTATION;");
  2705. // Breaking before function-like trailing annotations is fine to keep them
  2706. // close to their arguments.
  2707. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2708. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  2709. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  2710. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  2711. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  2712. " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
  2713. verifyFormat(
  2714. "void aaaaaaaaaaaaaaaaaa()\n"
  2715. " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
  2716. " aaaaaaaaaaaaaaaaaaaaaaaaa));");
  2717. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2718. " __attribute__((unused));");
  2719. verifyFormat(
  2720. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2721. " GUARDED_BY(aaaaaaaaaaaa);",
  2722. getGoogleStyle());
  2723. verifyFormat(
  2724. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2725. " GUARDED_BY(aaaaaaaaaaaa);",
  2726. getGoogleStyle());
  2727. }
  2728. TEST_F(FormatTest, BreaksDesireably) {
  2729. verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  2730. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  2731. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
  2732. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2733. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  2734. "}");
  2735. verifyFormat(
  2736. "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2737. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  2738. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2739. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2740. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  2741. verifyFormat(
  2742. "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2743. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  2744. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2745. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
  2746. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2747. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2748. verifyFormat(
  2749. "void f() {\n"
  2750. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
  2751. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  2752. "}");
  2753. verifyFormat(
  2754. "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2755. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  2756. verifyFormat(
  2757. "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2758. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  2759. verifyFormat(
  2760. "aaaaaaaaaaaaaaaaa(\n"
  2761. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2762. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2763. // Indent consistently indenpendent of call expression.
  2764. verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
  2765. " dddddddddddddddddddddddddddddd));\n"
  2766. "aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  2767. " dddddddddddddddddddddddddddddd));");
  2768. // This test case breaks on an incorrect memoization, i.e. an optimization not
  2769. // taking into account the StopAt value.
  2770. verifyFormat(
  2771. "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2772. " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2773. " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2774. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2775. verifyFormat("{\n {\n {\n"
  2776. " Annotation.SpaceRequiredBefore =\n"
  2777. " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
  2778. " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
  2779. " }\n }\n}");
  2780. // Break on an outer level if there was a break on an inner level.
  2781. EXPECT_EQ("f(g(h(a, // comment\n"
  2782. " b, c),\n"
  2783. " d, e),\n"
  2784. " x, y);",
  2785. format("f(g(h(a, // comment\n"
  2786. " b, c), d, e), x, y);"));
  2787. // Prefer breaking similar line breaks.
  2788. verifyFormat(
  2789. "const int kTrackingOptions = NSTrackingMouseMoved |\n"
  2790. " NSTrackingMouseEnteredAndExited |\n"
  2791. " NSTrackingActiveAlways;");
  2792. }
  2793. TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
  2794. FormatStyle NoBinPacking = getGoogleStyle();
  2795. NoBinPacking.BinPackParameters = false;
  2796. verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
  2797. " aaaaaaaaaaaaaaaaaaaa,\n"
  2798. " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
  2799. NoBinPacking);
  2800. verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
  2801. " aaaaaaaaaaaaa,\n"
  2802. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
  2803. NoBinPacking);
  2804. verifyFormat(
  2805. "aaaaaaaa(aaaaaaaaaaaaa,\n"
  2806. " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2807. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  2808. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2809. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
  2810. NoBinPacking);
  2811. verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  2812. " .aaaaaaaaaaaaaaaaaa();",
  2813. NoBinPacking);
  2814. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2815. " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);",
  2816. NoBinPacking);
  2817. verifyFormat(
  2818. "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2819. " aaaaaaaaaaaa,\n"
  2820. " aaaaaaaaaaaa);",
  2821. NoBinPacking);
  2822. verifyFormat(
  2823. "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
  2824. " ddddddddddddddddddddddddddddd),\n"
  2825. " test);",
  2826. NoBinPacking);
  2827. verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
  2828. " aaaaaaaaaaaaaaaaaaaaaaa,\n"
  2829. " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;",
  2830. NoBinPacking);
  2831. verifyFormat("a(\"a\"\n"
  2832. " \"a\",\n"
  2833. " a);");
  2834. NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
  2835. verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
  2836. " aaaaaaaaa,\n"
  2837. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  2838. NoBinPacking);
  2839. verifyFormat(
  2840. "void f() {\n"
  2841. " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  2842. " .aaaaaaa();\n"
  2843. "}",
  2844. NoBinPacking);
  2845. verifyFormat(
  2846. "template <class SomeType, class SomeOtherType>\n"
  2847. "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
  2848. NoBinPacking);
  2849. }
  2850. TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
  2851. FormatStyle Style = getLLVMStyleWithColumns(15);
  2852. Style.ExperimentalAutoDetectBinPacking = true;
  2853. EXPECT_EQ("aaa(aaaa,\n"
  2854. " aaaa,\n"
  2855. " aaaa);\n"
  2856. "aaa(aaaa,\n"
  2857. " aaaa,\n"
  2858. " aaaa);",
  2859. format("aaa(aaaa,\n" // one-per-line
  2860. " aaaa,\n"
  2861. " aaaa );\n"
  2862. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  2863. Style));
  2864. EXPECT_EQ("aaa(aaaa, aaaa,\n"
  2865. " aaaa);\n"
  2866. "aaa(aaaa, aaaa,\n"
  2867. " aaaa);",
  2868. format("aaa(aaaa, aaaa,\n" // bin-packed
  2869. " aaaa );\n"
  2870. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  2871. Style));
  2872. }
  2873. TEST_F(FormatTest, FormatsBuilderPattern) {
  2874. verifyFormat(
  2875. "return llvm::StringSwitch<Reference::Kind>(name)\n"
  2876. " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
  2877. " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
  2878. " .StartsWith(\".init\", ORDER_INIT)\n"
  2879. " .StartsWith(\".fini\", ORDER_FINI)\n"
  2880. " .StartsWith(\".hash\", ORDER_HASH)\n"
  2881. " .Default(ORDER_TEXT);\n");
  2882. verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
  2883. " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
  2884. verifyFormat(
  2885. "aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaa(\n"
  2886. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2887. " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
  2888. verifyFormat(
  2889. "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
  2890. " aaaaaaaaaaaaaa);");
  2891. verifyFormat(
  2892. "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
  2893. " aaaaaa->aaaaaaaaaaaa()\n"
  2894. " ->aaaaaaaaaaaaaaaa(\n"
  2895. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2896. " ->aaaaaaaaaaaaaaaaa();");
  2897. verifyFormat(
  2898. "someobj->Add((new util::filetools::Handler(dir))\n"
  2899. " ->OnEvent1(NewPermanentCallback(\n"
  2900. " this, &HandlerHolderClass::EventHandlerCBA))\n"
  2901. " ->OnEvent2(NewPermanentCallback(\n"
  2902. " this, &HandlerHolderClass::EventHandlerCBB))\n"
  2903. " ->OnEvent3(NewPermanentCallback(\n"
  2904. " this, &HandlerHolderClass::EventHandlerCBC))\n"
  2905. " ->OnEvent5(NewPermanentCallback(\n"
  2906. " this, &HandlerHolderClass::EventHandlerCBD))\n"
  2907. " ->OnEvent6(NewPermanentCallback(\n"
  2908. " this, &HandlerHolderClass::EventHandlerCBE)));");
  2909. verifyFormat(
  2910. "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
  2911. verifyFormat("aaaaaaaaaaaaaaa()\n"
  2912. " .aaaaaaaaaaaaaaa()\n"
  2913. " .aaaaaaaaaaaaaaa()\n"
  2914. " .aaaaaaaaaaaaaaa()\n"
  2915. " .aaaaaaaaaaaaaaa();");
  2916. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  2917. " .aaaaaaaaaaaaaaa()\n"
  2918. " .aaaaaaaaaaaaaaa()\n"
  2919. " .aaaaaaaaaaaaaaa();");
  2920. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  2921. " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  2922. " .aaaaaaaaaaaaaaa();");
  2923. verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
  2924. " ->aaaaaaaaaaaaaae(0)\n"
  2925. " ->aaaaaaaaaaaaaaa();");
  2926. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  2927. " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  2928. " .has<bbbbbbbbbbbbbbbbbbbbb>();");
  2929. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  2930. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  2931. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
  2932. // Prefer not to break after empty parentheses.
  2933. verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
  2934. " First->LastNewlineOffset);");
  2935. }
  2936. TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
  2937. verifyFormat(
  2938. "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2939. " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
  2940. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  2941. " ccccccccccccccccccccccccc) {\n}");
  2942. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  2943. " ccccccccccccccccccccccccc) {\n}");
  2944. verifyFormat(
  2945. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
  2946. " ccccccccccccccccccccccccc) {\n}");
  2947. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
  2948. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
  2949. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
  2950. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  2951. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
  2952. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
  2953. " aaaaaaaaaaaaaaa != aa) {\n}");
  2954. }
  2955. TEST_F(FormatTest, BreaksAfterAssignments) {
  2956. verifyFormat(
  2957. "unsigned Cost =\n"
  2958. " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
  2959. " SI->getPointerAddressSpaceee());\n");
  2960. verifyFormat(
  2961. "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
  2962. " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
  2963. verifyFormat(
  2964. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
  2965. " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
  2966. verifyFormat("unsigned OriginalStartColumn =\n"
  2967. " SourceMgr.getSpellingColumnNumber(\n"
  2968. " Current.FormatTok.getStartOfNonWhitespace()) -\n"
  2969. " 1;");
  2970. }
  2971. TEST_F(FormatTest, AlignsAfterAssignments) {
  2972. verifyFormat(
  2973. "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2974. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  2975. verifyFormat(
  2976. "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2977. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  2978. verifyFormat(
  2979. "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2980. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  2981. verifyFormat(
  2982. "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2983. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  2984. verifyFormat(
  2985. "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2986. " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2987. " aaaaaaaaaaaaaaaaaaaaaaaa;");
  2988. }
  2989. TEST_F(FormatTest, AlignsAfterReturn) {
  2990. verifyFormat(
  2991. "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2992. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  2993. verifyFormat(
  2994. "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2995. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  2996. verifyFormat(
  2997. "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  2998. " aaaaaaaaaaaaaaaaaaaaaa();");
  2999. verifyFormat(
  3000. "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  3001. " aaaaaaaaaaaaaaaaaaaaaa());");
  3002. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3003. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3004. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3005. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
  3006. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3007. verifyFormat("return\n"
  3008. " // true if code is one of a or b.\n"
  3009. " code == a || code == b;");
  3010. }
  3011. TEST_F(FormatTest, BreaksConditionalExpressions) {
  3012. verifyFormat(
  3013. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3014. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3015. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3016. verifyFormat(
  3017. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3018. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3019. verifyFormat(
  3020. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
  3021. " : aaaaaaaaaaaaa);");
  3022. verifyFormat(
  3023. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3024. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3025. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3026. " aaaaaaaaaaaaa);");
  3027. verifyFormat(
  3028. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3029. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3030. " aaaaaaaaaaaaa);");
  3031. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3032. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3033. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3034. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3035. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3036. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3037. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3038. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3039. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3040. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3041. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3042. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3043. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3044. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3045. " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3046. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3047. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3048. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3049. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3050. " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3051. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  3052. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3053. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3054. " : aaaaaaaaaaaaaaaa;");
  3055. verifyFormat(
  3056. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3057. " ? aaaaaaaaaaaaaaa\n"
  3058. " : aaaaaaaaaaaaaaa;");
  3059. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  3060. " aaaaaaaaa\n"
  3061. " ? b\n"
  3062. " : c);");
  3063. verifyFormat(
  3064. "unsigned Indent =\n"
  3065. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
  3066. " ? IndentForLevel[TheLine.Level]\n"
  3067. " : TheLine * 2,\n"
  3068. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  3069. getLLVMStyleWithColumns(70));
  3070. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  3071. " ? aaaaaaaaaaaaaaa\n"
  3072. " : bbbbbbbbbbbbbbb //\n"
  3073. " ? ccccccccccccccc\n"
  3074. " : ddddddddddddddd;");
  3075. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  3076. " ? aaaaaaaaaaaaaaa\n"
  3077. " : (bbbbbbbbbbbbbbb //\n"
  3078. " ? ccccccccccccccc\n"
  3079. " : ddddddddddddddd);");
  3080. FormatStyle NoBinPacking = getLLVMStyle();
  3081. NoBinPacking.BinPackParameters = false;
  3082. verifyFormat(
  3083. "void f() {\n"
  3084. " g(aaa,\n"
  3085. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  3086. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3087. " ? aaaaaaaaaaaaaaa\n"
  3088. " : aaaaaaaaaaaaaaa);\n"
  3089. "}",
  3090. NoBinPacking);
  3091. verifyFormat(
  3092. "void f() {\n"
  3093. " g(aaa,\n"
  3094. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  3095. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3096. " ?: aaaaaaaaaaaaaaa);\n"
  3097. "}",
  3098. NoBinPacking);
  3099. }
  3100. TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
  3101. verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
  3102. " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
  3103. verifyFormat("bool a = true, b = false;");
  3104. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3105. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
  3106. " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
  3107. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
  3108. verifyFormat(
  3109. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  3110. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
  3111. " d = e && f;");
  3112. verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
  3113. " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
  3114. verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  3115. " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
  3116. verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
  3117. " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
  3118. // FIXME: If multiple variables are defined, the "*" needs to move to the new
  3119. // line. Also fix indent for breaking after the type, this looks bad.
  3120. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
  3121. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
  3122. " *b = bbbbbbbbbbbbbbbbbbb;",
  3123. getGoogleStyle());
  3124. // Not ideal, but pointer-with-type does not allow much here.
  3125. verifyGoogleFormat(
  3126. "aaaaaaaaa* a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  3127. " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;");
  3128. }
  3129. TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
  3130. verifyFormat("arr[foo ? bar : baz];");
  3131. verifyFormat("f()[foo ? bar : baz];");
  3132. verifyFormat("(a + b)[foo ? bar : baz];");
  3133. verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
  3134. }
  3135. TEST_F(FormatTest, AlignsStringLiterals) {
  3136. verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
  3137. " \"short literal\");");
  3138. verifyFormat(
  3139. "looooooooooooooooooooooooongFunction(\n"
  3140. " \"short literal\"\n"
  3141. " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
  3142. verifyFormat("someFunction(\"Always break between multi-line\"\n"
  3143. " \" string literals\",\n"
  3144. " and, other, parameters);");
  3145. EXPECT_EQ("fun + \"1243\" /* comment */\n"
  3146. " \"5678\";",
  3147. format("fun + \"1243\" /* comment */\n"
  3148. " \"5678\";",
  3149. getLLVMStyleWithColumns(28)));
  3150. EXPECT_EQ(
  3151. "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  3152. " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
  3153. " \"aaaaaaaaaaaaaaaa\";",
  3154. format("aaaaaa ="
  3155. "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  3156. "aaaaaaaaaaaaaaaaaaaaa\" "
  3157. "\"aaaaaaaaaaaaaaaa\";"));
  3158. verifyFormat("a = a + \"a\"\n"
  3159. " \"a\"\n"
  3160. " \"a\";");
  3161. verifyFormat("f(\"a\", \"b\"\n"
  3162. " \"c\");");
  3163. verifyFormat(
  3164. "#define LL_FORMAT \"ll\"\n"
  3165. "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
  3166. " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
  3167. verifyFormat("#define A(X) \\\n"
  3168. " \"aaaaa\" #X \"bbbbbb\" \\\n"
  3169. " \"ccccc\"",
  3170. getLLVMStyleWithColumns(23));
  3171. verifyFormat("#define A \"def\"\n"
  3172. "f(\"abc\" A \"ghi\"\n"
  3173. " \"jkl\");");
  3174. }
  3175. TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
  3176. FormatStyle NoBreak = getLLVMStyle();
  3177. NoBreak.AlwaysBreakBeforeMultilineStrings = false;
  3178. FormatStyle Break = getLLVMStyle();
  3179. Break.AlwaysBreakBeforeMultilineStrings = true;
  3180. verifyFormat("aaaa = \"bbbb\"\n"
  3181. " \"cccc\";",
  3182. NoBreak);
  3183. verifyFormat("aaaa =\n"
  3184. " \"bbbb\"\n"
  3185. " \"cccc\";",
  3186. Break);
  3187. verifyFormat("aaaa(\"bbbb\"\n"
  3188. " \"cccc\");",
  3189. NoBreak);
  3190. verifyFormat("aaaa(\n"
  3191. " \"bbbb\"\n"
  3192. " \"cccc\");",
  3193. Break);
  3194. verifyFormat("aaaa(qqq, \"bbbb\"\n"
  3195. " \"cccc\");",
  3196. NoBreak);
  3197. verifyFormat("aaaa(qqq,\n"
  3198. " \"bbbb\"\n"
  3199. " \"cccc\");",
  3200. Break);
  3201. // Don't break if there is no column gain.
  3202. verifyFormat("f(\"aaaa\"\n"
  3203. " \"bbbb\");",
  3204. Break);
  3205. // Treat literals with escaped newlines like multi-line string literals.
  3206. EXPECT_EQ("x = \"a\\\n"
  3207. "b\\\n"
  3208. "c\";",
  3209. format("x = \"a\\\n"
  3210. "b\\\n"
  3211. "c\";",
  3212. NoBreak));
  3213. EXPECT_EQ("x =\n"
  3214. " \"a\\\n"
  3215. "b\\\n"
  3216. "c\";",
  3217. format("x = \"a\\\n"
  3218. "b\\\n"
  3219. "c\";",
  3220. Break));
  3221. }
  3222. TEST_F(FormatTest, AlignsPipes) {
  3223. verifyFormat(
  3224. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3225. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3226. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3227. verifyFormat(
  3228. "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
  3229. " << aaaaaaaaaaaaaaaaaaaa;");
  3230. verifyFormat(
  3231. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3232. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3233. verifyFormat(
  3234. "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
  3235. " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
  3236. " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
  3237. verifyFormat(
  3238. "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3239. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3240. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3241. verifyFormat("return out << \"somepacket = {\\n\"\n"
  3242. " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
  3243. " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
  3244. " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
  3245. " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
  3246. " << \"}\";");
  3247. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  3248. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  3249. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
  3250. verifyFormat(
  3251. "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
  3252. " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
  3253. " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
  3254. " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
  3255. " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
  3256. verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
  3257. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  3258. verifyFormat(
  3259. "void f() {\n"
  3260. " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
  3261. " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3262. "}");
  3263. // Breaking before the first "<<" is generally not desirable.
  3264. verifyFormat(
  3265. "llvm::errs()\n"
  3266. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3267. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3268. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3269. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  3270. getLLVMStyleWithColumns(70));
  3271. verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  3272. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3273. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  3274. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3275. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  3276. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  3277. getLLVMStyleWithColumns(70));
  3278. // But sometimes, breaking before the first "<<" is desirable.
  3279. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
  3280. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3281. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3282. verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
  3283. " << BEF << IsTemplate << Description << E->getType();");
  3284. verifyFormat(
  3285. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3286. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3287. // Incomplete string literal.
  3288. EXPECT_EQ("llvm::errs() << \"\n"
  3289. " << a;",
  3290. format("llvm::errs() << \"\n<<a;"));
  3291. }
  3292. TEST_F(FormatTest, UnderstandsEquals) {
  3293. verifyFormat(
  3294. "aaaaaaaaaaaaaaaaa =\n"
  3295. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3296. verifyFormat(
  3297. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3298. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  3299. verifyFormat(
  3300. "if (a) {\n"
  3301. " f();\n"
  3302. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3303. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  3304. "}");
  3305. verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3306. " 100000000 + 10000000) {\n}");
  3307. }
  3308. TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
  3309. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  3310. " .looooooooooooooooooooooooooooooooooooooongFunction();");
  3311. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  3312. " ->looooooooooooooooooooooooooooooooooooooongFunction();");
  3313. verifyFormat(
  3314. "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
  3315. " Parameter2);");
  3316. verifyFormat(
  3317. "ShortObject->shortFunction(\n"
  3318. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
  3319. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
  3320. verifyFormat("loooooooooooooongFunction(\n"
  3321. " LoooooooooooooongObject->looooooooooooooooongFunction());");
  3322. verifyFormat(
  3323. "function(LoooooooooooooooooooooooooooooooooooongObject\n"
  3324. " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
  3325. verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  3326. " .WillRepeatedly(Return(SomeValue));");
  3327. verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)]\n"
  3328. " .insert(ccccccccccccccccccccccc);");
  3329. verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3330. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).aaaaa(aaaaa),\n"
  3331. " aaaaaaaaaaaaaaaaaaaaa);");
  3332. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3333. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());");
  3334. verifyFormat(
  3335. "aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3336. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3337. " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3338. " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3339. " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3340. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3341. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3342. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3343. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
  3344. "}");
  3345. // Here, it is not necessary to wrap at "." or "->".
  3346. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
  3347. " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  3348. verifyFormat(
  3349. "aaaaaaaaaaa->aaaaaaaaa(\n"
  3350. " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3351. " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
  3352. verifyFormat(
  3353. "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3354. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
  3355. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
  3356. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  3357. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
  3358. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  3359. // FIXME: Should we break before .a()?
  3360. verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3361. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).a();");
  3362. FormatStyle NoBinPacking = getLLVMStyle();
  3363. NoBinPacking.BinPackParameters = false;
  3364. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  3365. " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  3366. " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
  3367. " aaaaaaaaaaaaaaaaaaa,\n"
  3368. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3369. NoBinPacking);
  3370. // If there is a subsequent call, change to hanging indentation.
  3371. verifyFormat(
  3372. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3373. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
  3374. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3375. verifyFormat(
  3376. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3377. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
  3378. }
  3379. TEST_F(FormatTest, WrapsTemplateDeclarations) {
  3380. verifyFormat("template <typename T>\n"
  3381. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  3382. verifyFormat("template <typename T>\n"
  3383. "// T should be one of {A, B}.\n"
  3384. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  3385. verifyFormat(
  3386. "template <typename T>\n"
  3387. "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
  3388. verifyFormat("template <typename T>\n"
  3389. "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
  3390. " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
  3391. verifyFormat(
  3392. "template <typename T>\n"
  3393. "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
  3394. " int Paaaaaaaaaaaaaaaaaaaaram2);");
  3395. verifyFormat(
  3396. "template <typename T>\n"
  3397. "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
  3398. " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
  3399. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3400. verifyFormat("template <typename T>\n"
  3401. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3402. " int aaaaaaaaaaaaaaaaaaaaaa);");
  3403. verifyFormat(
  3404. "template <typename T1, typename T2 = char, typename T3 = char,\n"
  3405. " typename T4 = char>\n"
  3406. "void f();");
  3407. verifyFormat(
  3408. "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
  3409. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3410. verifyFormat("a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
  3411. " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));");
  3412. verifyFormat("template <typename T> class C {};");
  3413. verifyFormat("template <typename T> void f();");
  3414. verifyFormat("template <typename T> void f() {}");
  3415. verifyFormat(
  3416. "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  3417. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3418. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
  3419. " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  3420. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3421. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
  3422. " bbbbbbbbbbbbbbbbbbbbbbbb);",
  3423. getLLVMStyleWithColumns(72));
  3424. FormatStyle AlwaysBreak = getLLVMStyle();
  3425. AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
  3426. verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
  3427. verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
  3428. verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
  3429. verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3430. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
  3431. " ccccccccccccccccccccccccccccccccccccccccccccccc);");
  3432. verifyFormat("template <template <typename> class Fooooooo,\n"
  3433. " template <typename> class Baaaaaaar>\n"
  3434. "struct C {};",
  3435. AlwaysBreak);
  3436. verifyFormat("template <typename T> // T can be A, B or C.\n"
  3437. "struct C {};",
  3438. AlwaysBreak);
  3439. }
  3440. TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
  3441. verifyFormat(
  3442. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  3443. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3444. verifyFormat(
  3445. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  3446. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3447. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  3448. // FIXME: Should we have the extra indent after the second break?
  3449. verifyFormat(
  3450. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  3451. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  3452. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3453. verifyFormat(
  3454. "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
  3455. " cccccccccccccccccccccccccccccccccccccccccccccc());");
  3456. // Breaking at nested name specifiers is generally not desirable.
  3457. verifyFormat(
  3458. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3459. " aaaaaaaaaaaaaaaaaaaaaaa);");
  3460. verifyFormat(
  3461. "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  3462. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3463. " aaaaaaaaaaaaaaaaaaaaa);",
  3464. getLLVMStyleWithColumns(74));
  3465. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  3466. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3467. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3468. }
  3469. TEST_F(FormatTest, UnderstandsTemplateParameters) {
  3470. verifyFormat("A<int> a;");
  3471. verifyFormat("A<A<A<int> > > a;");
  3472. verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
  3473. verifyFormat("bool x = a < 1 || 2 > a;");
  3474. verifyFormat("bool x = 5 < f<int>();");
  3475. verifyFormat("bool x = f<int>() > 5;");
  3476. verifyFormat("bool x = 5 < a<int>::x;");
  3477. verifyFormat("bool x = a < 4 ? a > 2 : false;");
  3478. verifyFormat("bool x = f() ? a < 2 : a > 2;");
  3479. verifyGoogleFormat("A<A<int>> a;");
  3480. verifyGoogleFormat("A<A<A<int>>> a;");
  3481. verifyGoogleFormat("A<A<A<A<int>>>> a;");
  3482. verifyGoogleFormat("A<A<int> > a;");
  3483. verifyGoogleFormat("A<A<A<int> > > a;");
  3484. verifyGoogleFormat("A<A<A<A<int> > > > a;");
  3485. verifyGoogleFormat("A<::A<int>> a;");
  3486. verifyGoogleFormat("A<::A> a;");
  3487. verifyGoogleFormat("A< ::A> a;");
  3488. verifyGoogleFormat("A< ::A<int> > a;");
  3489. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
  3490. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
  3491. EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
  3492. EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
  3493. verifyFormat("test >> a >> b;");
  3494. verifyFormat("test << a >> b;");
  3495. verifyFormat("f<int>();");
  3496. verifyFormat("template <typename T> void f() {}");
  3497. // Not template parameters.
  3498. verifyFormat("return a < b && c > d;");
  3499. verifyFormat("void f() {\n"
  3500. " while (a < b && c > d) {\n"
  3501. " }\n"
  3502. "}");
  3503. verifyFormat("template <typename... Types>\n"
  3504. "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
  3505. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3506. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
  3507. getLLVMStyleWithColumns(60));
  3508. }
  3509. TEST_F(FormatTest, UnderstandsBinaryOperators) {
  3510. verifyFormat("COMPARE(a, ==, b);");
  3511. }
  3512. TEST_F(FormatTest, UnderstandsPointersToMembers) {
  3513. verifyFormat("int A::*x;");
  3514. verifyFormat("int (S::*func)(void *);");
  3515. verifyFormat("void f() { int (S::*func)(void *); }");
  3516. verifyFormat("typedef bool *(Class::*Member)() const;");
  3517. verifyFormat("void f() {\n"
  3518. " (a->*f)();\n"
  3519. " a->*x;\n"
  3520. " (a.*f)();\n"
  3521. " ((*a).*f)();\n"
  3522. " a.*x;\n"
  3523. "}");
  3524. verifyFormat("void f() {\n"
  3525. " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  3526. " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
  3527. "}");
  3528. FormatStyle Style = getLLVMStyle();
  3529. Style.PointerBindsToType = true;
  3530. verifyFormat("typedef bool* (Class::*Member)() const;", Style);
  3531. }
  3532. TEST_F(FormatTest, UnderstandsUnaryOperators) {
  3533. verifyFormat("int a = -2;");
  3534. verifyFormat("f(-1, -2, -3);");
  3535. verifyFormat("a[-1] = 5;");
  3536. verifyFormat("int a = 5 + -2;");
  3537. verifyFormat("if (i == -1) {\n}");
  3538. verifyFormat("if (i != -1) {\n}");
  3539. verifyFormat("if (i > -1) {\n}");
  3540. verifyFormat("if (i < -1) {\n}");
  3541. verifyFormat("++(a->f());");
  3542. verifyFormat("--(a->f());");
  3543. verifyFormat("(a->f())++;");
  3544. verifyFormat("a[42]++;");
  3545. verifyFormat("if (!(a->f())) {\n}");
  3546. verifyFormat("a-- > b;");
  3547. verifyFormat("b ? -a : c;");
  3548. verifyFormat("n * sizeof char16;");
  3549. verifyFormat("n * alignof char16;", getGoogleStyle());
  3550. verifyFormat("sizeof(char);");
  3551. verifyFormat("alignof(char);", getGoogleStyle());
  3552. verifyFormat("return -1;");
  3553. verifyFormat("switch (a) {\n"
  3554. "case -1:\n"
  3555. " break;\n"
  3556. "}");
  3557. verifyFormat("#define X -1");
  3558. verifyFormat("#define X -kConstant");
  3559. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
  3560. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
  3561. verifyFormat("int a = /* confusing comment */ -1;");
  3562. // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
  3563. verifyFormat("int a = i /* confusing comment */++;");
  3564. }
  3565. TEST_F(FormatTest, IndentsRelativeToUnaryOperators) {
  3566. verifyFormat("if (!aaaaaaaaaa( // break\n"
  3567. " aaaaa)) {\n"
  3568. "}");
  3569. verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
  3570. " aaaaa));");
  3571. // Only indent relative to unary operators if the expression is nested.
  3572. verifyFormat("*aaa = aaaaaaa( // break\n"
  3573. " bbbbbb);");
  3574. }
  3575. TEST_F(FormatTest, UnderstandsOverloadedOperators) {
  3576. verifyFormat("bool operator<();");
  3577. verifyFormat("bool operator>();");
  3578. verifyFormat("bool operator=();");
  3579. verifyFormat("bool operator==();");
  3580. verifyFormat("bool operator!=();");
  3581. verifyFormat("int operator+();");
  3582. verifyFormat("int operator++();");
  3583. verifyFormat("bool operator();");
  3584. verifyFormat("bool operator()();");
  3585. verifyFormat("bool operator[]();");
  3586. verifyFormat("operator bool();");
  3587. verifyFormat("operator int();");
  3588. verifyFormat("operator void *();");
  3589. verifyFormat("operator SomeType<int>();");
  3590. verifyFormat("operator SomeType<int, int>();");
  3591. verifyFormat("operator SomeType<SomeType<int> >();");
  3592. verifyFormat("void *operator new(std::size_t size);");
  3593. verifyFormat("void *operator new[](std::size_t size);");
  3594. verifyFormat("void operator delete(void *ptr);");
  3595. verifyFormat("void operator delete[](void *ptr);");
  3596. verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
  3597. "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
  3598. verifyFormat(
  3599. "ostream &operator<<(ostream &OutputStream,\n"
  3600. " SomeReallyLongType WithSomeReallyLongValue);");
  3601. verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
  3602. " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
  3603. " return left.group < right.group;\n"
  3604. "}");
  3605. verifyFormat("SomeType &operator=(const SomeType &S);");
  3606. verifyGoogleFormat("operator void*();");
  3607. verifyGoogleFormat("operator SomeType<SomeType<int>>();");
  3608. verifyFormat("using A::operator+;");
  3609. }
  3610. TEST_F(FormatTest, UnderstandsNewAndDelete) {
  3611. verifyFormat("void f() {\n"
  3612. " A *a = new A;\n"
  3613. " A *a = new (placement) A;\n"
  3614. " delete a;\n"
  3615. " delete (A *)a;\n"
  3616. "}");
  3617. }
  3618. TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
  3619. verifyFormat("int *f(int *a) {}");
  3620. verifyFormat("int main(int argc, char **argv) {}");
  3621. verifyFormat("Test::Test(int b) : a(b * b) {}");
  3622. verifyIndependentOfContext("f(a, *a);");
  3623. verifyFormat("void g() { f(*a); }");
  3624. verifyIndependentOfContext("int a = b * 10;");
  3625. verifyIndependentOfContext("int a = 10 * b;");
  3626. verifyIndependentOfContext("int a = b * c;");
  3627. verifyIndependentOfContext("int a += b * c;");
  3628. verifyIndependentOfContext("int a -= b * c;");
  3629. verifyIndependentOfContext("int a *= b * c;");
  3630. verifyIndependentOfContext("int a /= b * c;");
  3631. verifyIndependentOfContext("int a = *b;");
  3632. verifyIndependentOfContext("int a = *b * c;");
  3633. verifyIndependentOfContext("int a = b * *c;");
  3634. verifyIndependentOfContext("return 10 * b;");
  3635. verifyIndependentOfContext("return *b * *c;");
  3636. verifyIndependentOfContext("return a & ~b;");
  3637. verifyIndependentOfContext("f(b ? *c : *d);");
  3638. verifyIndependentOfContext("int a = b ? *c : *d;");
  3639. verifyIndependentOfContext("*b = a;");
  3640. verifyIndependentOfContext("a * ~b;");
  3641. verifyIndependentOfContext("a * !b;");
  3642. verifyIndependentOfContext("a * +b;");
  3643. verifyIndependentOfContext("a * -b;");
  3644. verifyIndependentOfContext("a * ++b;");
  3645. verifyIndependentOfContext("a * --b;");
  3646. verifyIndependentOfContext("a[4] * b;");
  3647. verifyIndependentOfContext("a[a * a] = 1;");
  3648. verifyIndependentOfContext("f() * b;");
  3649. verifyIndependentOfContext("a * [self dostuff];");
  3650. verifyIndependentOfContext("int x = a * (a + b);");
  3651. verifyIndependentOfContext("(a *)(a + b);");
  3652. verifyIndependentOfContext("int *pa = (int *)&a;");
  3653. verifyIndependentOfContext("return sizeof(int **);");
  3654. verifyIndependentOfContext("return sizeof(int ******);");
  3655. verifyIndependentOfContext("return (int **&)a;");
  3656. verifyIndependentOfContext("f((*PointerToArray)[10]);");
  3657. verifyFormat("void f(Type (*parameter)[10]) {}");
  3658. verifyGoogleFormat("return sizeof(int**);");
  3659. verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
  3660. verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
  3661. verifyFormat("auto a = [](int **&, int ***) {};");
  3662. verifyFormat("auto PointerBinding = [](const char *S) {};");
  3663. verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
  3664. verifyIndependentOfContext("InvalidRegions[*R] = 0;");
  3665. verifyIndependentOfContext("A<int *> a;");
  3666. verifyIndependentOfContext("A<int **> a;");
  3667. verifyIndependentOfContext("A<int *, int *> a;");
  3668. verifyIndependentOfContext(
  3669. "const char *const p = reinterpret_cast<const char *const>(q);");
  3670. verifyIndependentOfContext("A<int **, int **> a;");
  3671. verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
  3672. verifyFormat("for (char **a = b; *a; ++a) {\n}");
  3673. verifyFormat("for (; a && b;) {\n}");
  3674. verifyFormat(
  3675. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3676. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3677. verifyGoogleFormat("int main(int argc, char** argv) {}");
  3678. verifyGoogleFormat("A<int*> a;");
  3679. verifyGoogleFormat("A<int**> a;");
  3680. verifyGoogleFormat("A<int*, int*> a;");
  3681. verifyGoogleFormat("A<int**, int**> a;");
  3682. verifyGoogleFormat("f(b ? *c : *d);");
  3683. verifyGoogleFormat("int a = b ? *c : *d;");
  3684. verifyGoogleFormat("Type* t = **x;");
  3685. verifyGoogleFormat("Type* t = *++*x;");
  3686. verifyGoogleFormat("*++*x;");
  3687. verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
  3688. verifyGoogleFormat("Type* t = x++ * y;");
  3689. verifyGoogleFormat(
  3690. "const char* const p = reinterpret_cast<const char* const>(q);");
  3691. verifyIndependentOfContext("a = *(x + y);");
  3692. verifyIndependentOfContext("a = &(x + y);");
  3693. verifyIndependentOfContext("*(x + y).call();");
  3694. verifyIndependentOfContext("&(x + y)->call();");
  3695. verifyFormat("void f() { &(*I).first; }");
  3696. verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
  3697. verifyFormat(
  3698. "int *MyValues = {\n"
  3699. " *A, // Operator detection might be confused by the '{'\n"
  3700. " *BB // Operator detection might be confused by previous comment\n"
  3701. "};");
  3702. verifyIndependentOfContext("if (int *a = &b)");
  3703. verifyIndependentOfContext("if (int &a = *b)");
  3704. verifyIndependentOfContext("if (a & b[i])");
  3705. verifyIndependentOfContext("if (a::b::c::d & b[i])");
  3706. verifyIndependentOfContext("if (*b[i])");
  3707. verifyIndependentOfContext("if (int *a = (&b))");
  3708. verifyIndependentOfContext("while (int *a = &b)");
  3709. verifyIndependentOfContext("size = sizeof *a;");
  3710. verifyFormat("void f() {\n"
  3711. " for (const int &v : Values) {\n"
  3712. " }\n"
  3713. "}");
  3714. verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
  3715. verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
  3716. verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
  3717. verifyFormat("#define MACRO \\\n"
  3718. " int *i = a * b; \\\n"
  3719. " void f(a *b);",
  3720. getLLVMStyleWithColumns(19));
  3721. verifyIndependentOfContext("A = new SomeType *[Length];");
  3722. verifyIndependentOfContext("A = new SomeType *[Length]();");
  3723. verifyIndependentOfContext("T **t = new T *;");
  3724. verifyIndependentOfContext("T **t = new T *();");
  3725. verifyGoogleFormat("A = new SomeType* [Length]();");
  3726. verifyGoogleFormat("A = new SomeType* [Length];");
  3727. verifyGoogleFormat("T** t = new T*;");
  3728. verifyGoogleFormat("T** t = new T*();");
  3729. FormatStyle PointerLeft = getLLVMStyle();
  3730. PointerLeft.PointerBindsToType = true;
  3731. verifyFormat("delete *x;", PointerLeft);
  3732. }
  3733. TEST_F(FormatTest, UnderstandsAttributes) {
  3734. verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
  3735. }
  3736. TEST_F(FormatTest, UnderstandsEllipsis) {
  3737. verifyFormat("int printf(const char *fmt, ...);");
  3738. verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
  3739. verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
  3740. FormatStyle PointersLeft = getLLVMStyle();
  3741. PointersLeft.PointerBindsToType = true;
  3742. verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
  3743. }
  3744. TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
  3745. EXPECT_EQ("int *a;\n"
  3746. "int *a;\n"
  3747. "int *a;",
  3748. format("int *a;\n"
  3749. "int* a;\n"
  3750. "int *a;",
  3751. getGoogleStyle()));
  3752. EXPECT_EQ("int* a;\n"
  3753. "int* a;\n"
  3754. "int* a;",
  3755. format("int* a;\n"
  3756. "int* a;\n"
  3757. "int *a;",
  3758. getGoogleStyle()));
  3759. EXPECT_EQ("int *a;\n"
  3760. "int *a;\n"
  3761. "int *a;",
  3762. format("int *a;\n"
  3763. "int * a;\n"
  3764. "int * a;",
  3765. getGoogleStyle()));
  3766. }
  3767. TEST_F(FormatTest, UnderstandsRvalueReferences) {
  3768. verifyFormat("int f(int &&a) {}");
  3769. verifyFormat("int f(int a, char &&b) {}");
  3770. verifyFormat("void f() { int &&a = b; }");
  3771. verifyGoogleFormat("int f(int a, char&& b) {}");
  3772. verifyGoogleFormat("void f() { int&& a = b; }");
  3773. verifyIndependentOfContext("A<int &&> a;");
  3774. verifyIndependentOfContext("A<int &&, int &&> a;");
  3775. verifyGoogleFormat("A<int&&> a;");
  3776. verifyGoogleFormat("A<int&&, int&&> a;");
  3777. // Not rvalue references:
  3778. verifyFormat("template <bool B, bool C> class A {\n"
  3779. " static_assert(B && C, \"Something is wrong\");\n"
  3780. "};");
  3781. verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
  3782. verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
  3783. }
  3784. TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
  3785. verifyFormat("void f() {\n"
  3786. " x[aaaaaaaaa -\n"
  3787. " b] = 23;\n"
  3788. "}",
  3789. getLLVMStyleWithColumns(15));
  3790. }
  3791. TEST_F(FormatTest, FormatsCasts) {
  3792. verifyFormat("Type *A = static_cast<Type *>(P);");
  3793. verifyFormat("Type *A = (Type *)P;");
  3794. verifyFormat("Type *A = (vector<Type *, int *>)P;");
  3795. verifyFormat("int a = (int)(2.0f);");
  3796. verifyFormat("int a = (int)2.0f;");
  3797. verifyFormat("x[(int32)y];");
  3798. verifyFormat("x = (int32)y;");
  3799. verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
  3800. verifyFormat("int a = (int)*b;");
  3801. verifyFormat("int a = (int)2.0f;");
  3802. verifyFormat("int a = (int)~0;");
  3803. verifyFormat("int a = (int)++a;");
  3804. verifyFormat("int a = (int)sizeof(int);");
  3805. verifyFormat("int a = (int)+2;");
  3806. verifyFormat("my_int a = (my_int)2.0f;");
  3807. verifyFormat("my_int a = (my_int)sizeof(int);");
  3808. verifyFormat("return (my_int)aaa;");
  3809. verifyFormat("#define x ((int)-1)");
  3810. verifyFormat("#define p(q) ((int *)&q)");
  3811. // FIXME: Without type knowledge, this can still fall apart miserably.
  3812. verifyFormat("void f() { my_int a = (my_int) * b; }");
  3813. verifyFormat("void f() { return P ? (my_int) * P : (my_int)0; }");
  3814. verifyFormat("my_int a = (my_int) ~0;");
  3815. verifyFormat("my_int a = (my_int)++ a;");
  3816. verifyFormat("my_int a = (my_int) + 2;");
  3817. // Don't break after a cast's
  3818. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3819. " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
  3820. " bbbbbbbbbbbbbbbbbbbbbb);");
  3821. // These are not casts.
  3822. verifyFormat("void f(int *) {}");
  3823. verifyFormat("f(foo)->b;");
  3824. verifyFormat("f(foo).b;");
  3825. verifyFormat("f(foo)(b);");
  3826. verifyFormat("f(foo)[b];");
  3827. verifyFormat("[](foo) { return 4; }(bar);");
  3828. verifyFormat("(*funptr)(foo)[4];");
  3829. verifyFormat("funptrs[4](foo)[4];");
  3830. verifyFormat("void f(int *);");
  3831. verifyFormat("void f(int *) = 0;");
  3832. verifyFormat("void f(SmallVector<int>) {}");
  3833. verifyFormat("void f(SmallVector<int>);");
  3834. verifyFormat("void f(SmallVector<int>) = 0;");
  3835. verifyFormat("void f(int i = (kValue) * kMask) {}");
  3836. verifyFormat("void f(int i = (kA * kB) & kMask) {}");
  3837. verifyFormat("int a = sizeof(int) * b;");
  3838. verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
  3839. verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
  3840. verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
  3841. verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
  3842. // These are not casts, but at some point were confused with casts.
  3843. verifyFormat("virtual void foo(int *) override;");
  3844. verifyFormat("virtual void foo(char &) const;");
  3845. verifyFormat("virtual void foo(int *a, char *) const;");
  3846. verifyFormat("int a = sizeof(int *) + b;");
  3847. verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
  3848. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
  3849. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  3850. // FIXME: The indentation here is not ideal.
  3851. verifyFormat(
  3852. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3853. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
  3854. " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
  3855. }
  3856. TEST_F(FormatTest, FormatsFunctionTypes) {
  3857. verifyFormat("A<bool()> a;");
  3858. verifyFormat("A<SomeType()> a;");
  3859. verifyFormat("A<void (*)(int, std::string)> a;");
  3860. verifyFormat("A<void *(int)>;");
  3861. verifyFormat("void *(*a)(int *, SomeType *);");
  3862. verifyFormat("int (*func)(void *);");
  3863. verifyFormat("void f() { int (*func)(void *); }");
  3864. verifyFormat("template <class CallbackClass>\n"
  3865. "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
  3866. verifyGoogleFormat("A<void*(int*, SomeType*)>;");
  3867. verifyGoogleFormat("void* (*a)(int);");
  3868. verifyGoogleFormat(
  3869. "template <class CallbackClass>\n"
  3870. "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
  3871. // Other constructs can look somewhat like function types:
  3872. verifyFormat("A<sizeof(*x)> a;");
  3873. verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
  3874. verifyFormat("some_var = function(*some_pointer_var)[0];");
  3875. verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
  3876. }
  3877. TEST_F(FormatTest, BreaksLongDeclarations) {
  3878. verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
  3879. " AnotherNameForTheLongType;",
  3880. getGoogleStyle());
  3881. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  3882. " LoooooooooooooooooooooooooooooooooooooooongVariable;",
  3883. getGoogleStyle());
  3884. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
  3885. " LoooooooooooooooooooooooooooooooooooooooongVariable;",
  3886. getGoogleStyle());
  3887. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  3888. " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
  3889. getGoogleStyle());
  3890. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  3891. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  3892. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  3893. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  3894. // FIXME: Without the comment, this breaks after "(".
  3895. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
  3896. " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
  3897. getGoogleStyle());
  3898. verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
  3899. " int LoooooooooooooooooooongParam2) {}");
  3900. verifyFormat(
  3901. "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
  3902. " SourceLocation L, IdentifierIn *II,\n"
  3903. " Type *T) {}");
  3904. verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
  3905. "ReallyReallyLongFunctionName(\n"
  3906. " const std::string &SomeParameter,\n"
  3907. " const SomeType<string, SomeOtherTemplateParameter> &\n"
  3908. " ReallyReallyLongParameterName,\n"
  3909. " const SomeType<string, SomeOtherTemplateParameter> &\n"
  3910. " AnotherLongParameterName) {}");
  3911. verifyFormat("template <typename A>\n"
  3912. "SomeLoooooooooooooooooooooongType<\n"
  3913. " typename some_namespace::SomeOtherType<A>::Type>\n"
  3914. "Function() {}");
  3915. verifyGoogleFormat(
  3916. "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
  3917. " aaaaaaaaaaaaaaaaaaaaaaa;");
  3918. verifyGoogleFormat(
  3919. "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
  3920. " SourceLocation L) {}");
  3921. verifyGoogleFormat(
  3922. "some_namespace::LongReturnType\n"
  3923. "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
  3924. " int first_long_parameter, int second_parameter) {}");
  3925. verifyGoogleFormat("template <typename T>\n"
  3926. "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  3927. "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
  3928. verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3929. " int aaaaaaaaaaaaaaaaaaaaaaa);");
  3930. }
  3931. TEST_F(FormatTest, FormatsArrays) {
  3932. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  3933. " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
  3934. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3935. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  3936. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3937. " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
  3938. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3939. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  3940. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  3941. verifyFormat(
  3942. "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
  3943. " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  3944. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  3945. }
  3946. TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
  3947. verifyFormat("(a)->b();");
  3948. verifyFormat("--a;");
  3949. }
  3950. TEST_F(FormatTest, HandlesIncludeDirectives) {
  3951. verifyFormat("#include <string>\n"
  3952. "#include <a/b/c.h>\n"
  3953. "#include \"a/b/string\"\n"
  3954. "#include \"string.h\"\n"
  3955. "#include \"string.h\"\n"
  3956. "#include <a-a>\n"
  3957. "#include < path with space >\n"
  3958. "#include \"abc.h\" // this is included for ABC\n"
  3959. "#include \"some long include\" // with a comment\n"
  3960. "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
  3961. getLLVMStyleWithColumns(35));
  3962. verifyFormat("#import <string>");
  3963. verifyFormat("#import <a/b/c.h>");
  3964. verifyFormat("#import \"a/b/string\"");
  3965. verifyFormat("#import \"string.h\"");
  3966. verifyFormat("#import \"string.h\"");
  3967. verifyFormat("#if __has_include(<strstream>)\n"
  3968. "#include <strstream>\n"
  3969. "#endif");
  3970. }
  3971. //===----------------------------------------------------------------------===//
  3972. // Error recovery tests.
  3973. //===----------------------------------------------------------------------===//
  3974. TEST_F(FormatTest, IncompleteParameterLists) {
  3975. FormatStyle NoBinPacking = getLLVMStyle();
  3976. NoBinPacking.BinPackParameters = false;
  3977. verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
  3978. " double *min_x,\n"
  3979. " double *max_x,\n"
  3980. " double *min_y,\n"
  3981. " double *max_y,\n"
  3982. " double *min_z,\n"
  3983. " double *max_z, ) {}",
  3984. NoBinPacking);
  3985. }
  3986. TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
  3987. verifyFormat("void f() { return; }\n42");
  3988. verifyFormat("void f() {\n"
  3989. " if (0)\n"
  3990. " return;\n"
  3991. "}\n"
  3992. "42");
  3993. verifyFormat("void f() { return }\n42");
  3994. verifyFormat("void f() {\n"
  3995. " if (0)\n"
  3996. " return\n"
  3997. "}\n"
  3998. "42");
  3999. }
  4000. TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
  4001. EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
  4002. EXPECT_EQ("void f() {\n"
  4003. " if (a)\n"
  4004. " return\n"
  4005. "}",
  4006. format("void f ( ) { if ( a ) return }"));
  4007. EXPECT_EQ("namespace N {\n"
  4008. "void f()\n"
  4009. "}",
  4010. format("namespace N { void f() }"));
  4011. EXPECT_EQ("namespace N {\n"
  4012. "void f() {}\n"
  4013. "void g()\n"
  4014. "}",
  4015. format("namespace N { void f( ) { } void g( ) }"));
  4016. }
  4017. TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
  4018. verifyFormat("int aaaaaaaa =\n"
  4019. " // Overlylongcomment\n"
  4020. " b;",
  4021. getLLVMStyleWithColumns(20));
  4022. verifyFormat("function(\n"
  4023. " ShortArgument,\n"
  4024. " LoooooooooooongArgument);\n",
  4025. getLLVMStyleWithColumns(20));
  4026. }
  4027. TEST_F(FormatTest, IncorrectAccessSpecifier) {
  4028. verifyFormat("public:");
  4029. verifyFormat("class A {\n"
  4030. "public\n"
  4031. " void f() {}\n"
  4032. "};");
  4033. verifyFormat("public\n"
  4034. "int qwerty;");
  4035. verifyFormat("public\n"
  4036. "B {}");
  4037. verifyFormat("public\n"
  4038. "{}");
  4039. verifyFormat("public\n"
  4040. "B { int x; }");
  4041. }
  4042. TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
  4043. verifyFormat("{");
  4044. verifyFormat("#})");
  4045. }
  4046. TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
  4047. verifyFormat("do {\n}");
  4048. verifyFormat("do {\n}\n"
  4049. "f();");
  4050. verifyFormat("do {\n}\n"
  4051. "wheeee(fun);");
  4052. verifyFormat("do {\n"
  4053. " f();\n"
  4054. "}");
  4055. }
  4056. TEST_F(FormatTest, IncorrectCodeMissingParens) {
  4057. verifyFormat("if {\n foo;\n foo();\n}");
  4058. verifyFormat("switch {\n foo;\n foo();\n}");
  4059. verifyFormat("for {\n foo;\n foo();\n}");
  4060. verifyFormat("while {\n foo;\n foo();\n}");
  4061. verifyFormat("do {\n foo;\n foo();\n} while;");
  4062. }
  4063. TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
  4064. verifyFormat("namespace {\n"
  4065. "class Foo { Foo (\n"
  4066. "};\n"
  4067. "} // comment");
  4068. }
  4069. TEST_F(FormatTest, IncorrectCodeErrorDetection) {
  4070. EXPECT_EQ("{\n {}\n", format("{\n{\n}\n"));
  4071. EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
  4072. EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
  4073. EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n"));
  4074. EXPECT_EQ("{\n"
  4075. " {\n"
  4076. " breakme(\n"
  4077. " qwe);\n"
  4078. " }\n",
  4079. format("{\n"
  4080. " {\n"
  4081. " breakme(qwe);\n"
  4082. "}\n",
  4083. getLLVMStyleWithColumns(10)));
  4084. }
  4085. TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
  4086. verifyFormat("int x = {\n"
  4087. " avariable,\n"
  4088. " b(alongervariable)\n"
  4089. "};",
  4090. getLLVMStyleWithColumns(25));
  4091. }
  4092. TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
  4093. verifyFormat("return (a)(b) { 1, 2, 3 };");
  4094. }
  4095. TEST_F(FormatTest, LayoutCxx11ConstructorBraceInitializers) {
  4096. verifyFormat("vector<int> x{ 1, 2, 3, 4 };");
  4097. verifyFormat("vector<T> x{ {}, {}, {}, {} };");
  4098. verifyFormat("f({ 1, 2 });");
  4099. verifyFormat("auto v = Foo{ 1 };");
  4100. verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });");
  4101. verifyFormat("Class::Class : member{ 1, 2, 3 } {}");
  4102. verifyFormat("new vector<int>{ 1, 2, 3 };");
  4103. verifyFormat("new int[3]{ 1, 2, 3 };");
  4104. verifyFormat("return { arg1, arg2 };");
  4105. verifyFormat("return { arg1, SomeType{ parameter } };");
  4106. verifyFormat("new T{ arg1, arg2 };");
  4107. verifyFormat("f(MyMap[{ composite, key }]);");
  4108. verifyFormat("class Class {\n"
  4109. " T member = { arg1, arg2 };\n"
  4110. "};");
  4111. verifyFormat(
  4112. "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4113. " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
  4114. " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  4115. " bbbbbbbbbbbbbbbbbbbb, bbbbb };");
  4116. verifyFormat("DoSomethingWithVector({} /* No data */);");
  4117. verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });");
  4118. verifyFormat(
  4119. "someFunction(OtherParam, BracedList{\n"
  4120. " // comment 1 (Forcing interesting break)\n"
  4121. " param1, param2,\n"
  4122. " // comment 2\n"
  4123. " param3, param4\n"
  4124. " });");
  4125. verifyFormat(
  4126. "std::this_thread::sleep_for(\n"
  4127. " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);");
  4128. FormatStyle NoSpaces = getLLVMStyle();
  4129. NoSpaces.Cpp11BracedListStyle = true;
  4130. verifyFormat("vector<int> x{1, 2, 3, 4};", NoSpaces);
  4131. verifyFormat("vector<T> x{{}, {}, {}, {}};", NoSpaces);
  4132. verifyFormat("f({1, 2});", NoSpaces);
  4133. verifyFormat("auto v = Foo{-1};", NoSpaces);
  4134. verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});", NoSpaces);
  4135. verifyFormat("Class::Class : member{1, 2, 3} {}", NoSpaces);
  4136. verifyFormat("new vector<int>{1, 2, 3};", NoSpaces);
  4137. verifyFormat("new int[3]{1, 2, 3};", NoSpaces);
  4138. verifyFormat("return {arg1, arg2};", NoSpaces);
  4139. verifyFormat("return {arg1, SomeType{parameter}};", NoSpaces);
  4140. verifyFormat("new T{arg1, arg2};", NoSpaces);
  4141. verifyFormat("f(MyMap[{composite, key}]);", NoSpaces);
  4142. verifyFormat("class Class {\n"
  4143. " T member = {arg1, arg2};\n"
  4144. "};",
  4145. NoSpaces);
  4146. }
  4147. TEST_F(FormatTest, FormatsBracedListsinColumnLayout) {
  4148. verifyFormat("vector<int> x = { 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  4149. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  4150. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  4151. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  4152. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  4153. " 1, 22, 333, 4444, 55555, 666666, 7777777 };");
  4154. verifyFormat("vector<int> x = { 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  4155. " // line comment\n"
  4156. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  4157. " 1, 22, 333, 4444, 55555,\n"
  4158. " // line comment\n"
  4159. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  4160. " 1, 22, 333, 4444, 55555, 666666, 7777777 };");
  4161. verifyFormat(
  4162. "vector<int> x = { 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  4163. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  4164. " 1, 22, 333, 4444, 55555, 666666, // comment\n"
  4165. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  4166. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  4167. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  4168. " 7777777 };");
  4169. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  4170. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  4171. " X86::R8, X86::R9, X86::R10, X86::R11, 0\n"
  4172. "};");
  4173. verifyFormat("vector<int> x = { 1, 1, 1, 1,\n"
  4174. " 1, 1, 1, 1 };",
  4175. getLLVMStyleWithColumns(39));
  4176. verifyFormat("vector<int> x = { 1, 1, 1, 1,\n"
  4177. " 1, 1, 1, 1 };",
  4178. getLLVMStyleWithColumns(38));
  4179. verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
  4180. " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1\n"
  4181. "};",
  4182. getLLVMStyleWithColumns(40));
  4183. // Trailing commas.
  4184. verifyFormat("vector<int> x = { 1, 1, 1, 1,\n"
  4185. " 1, 1, 1, 1, };",
  4186. getLLVMStyleWithColumns(39));
  4187. verifyFormat("vector<int> x = {\n"
  4188. " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
  4189. "};",
  4190. getLLVMStyleWithColumns(39));
  4191. verifyFormat("vector<int> x = { 1, 1, 1, 1,\n"
  4192. " 1, 1, 1, 1,\n"
  4193. " /**/ /**/ };",
  4194. getLLVMStyleWithColumns(39));
  4195. verifyFormat("return { { aaaaaaaaaaaaaaaaaaaaa }, { aaaaaaaaaaaaaaaaaaa },\n"
  4196. " { aaaaaaaaaaaaaaaaaaaaa }, { aaaaaaaaaaaaaaaaa } };",
  4197. getLLVMStyleWithColumns(60));
  4198. }
  4199. TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
  4200. verifyFormat("void f() { return 42; }");
  4201. verifyFormat("void f() {\n"
  4202. " // Comment\n"
  4203. "}");
  4204. verifyFormat("{\n"
  4205. "#error {\n"
  4206. " int a;\n"
  4207. "}");
  4208. verifyFormat("{\n"
  4209. " int a;\n"
  4210. "#error {\n"
  4211. "}");
  4212. verifyFormat("void f() {} // comment");
  4213. verifyFormat("void f() { int a; } // comment");
  4214. verifyFormat("void f() {\n"
  4215. "} // comment",
  4216. getLLVMStyleWithColumns(15));
  4217. verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
  4218. verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
  4219. verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
  4220. verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
  4221. }
  4222. TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
  4223. // Elaborate type variable declarations.
  4224. verifyFormat("struct foo a = { bar };\nint n;");
  4225. verifyFormat("class foo a = { bar };\nint n;");
  4226. verifyFormat("union foo a = { bar };\nint n;");
  4227. // Elaborate types inside function definitions.
  4228. verifyFormat("struct foo f() {}\nint n;");
  4229. verifyFormat("class foo f() {}\nint n;");
  4230. verifyFormat("union foo f() {}\nint n;");
  4231. // Templates.
  4232. verifyFormat("template <class X> void f() {}\nint n;");
  4233. verifyFormat("template <struct X> void f() {}\nint n;");
  4234. verifyFormat("template <union X> void f() {}\nint n;");
  4235. // Actual definitions...
  4236. verifyFormat("struct {\n} n;");
  4237. verifyFormat(
  4238. "template <template <class T, class Y>, class Z> class X {\n} n;");
  4239. verifyFormat("union Z {\n int n;\n} x;");
  4240. verifyFormat("class MACRO Z {\n} n;");
  4241. verifyFormat("class MACRO(X) Z {\n} n;");
  4242. verifyFormat("class __attribute__(X) Z {\n} n;");
  4243. verifyFormat("class __declspec(X) Z {\n} n;");
  4244. verifyFormat("class A##B##C {\n} n;");
  4245. verifyFormat("class alignas(16) Z {\n} n;");
  4246. // Redefinition from nested context:
  4247. verifyFormat("class A::B::C {\n} n;");
  4248. // Template definitions.
  4249. verifyFormat(
  4250. "template <typename F>\n"
  4251. "Matcher(const Matcher<F> &Other,\n"
  4252. " typename enable_if_c<is_base_of<F, T>::value &&\n"
  4253. " !is_same<F, T>::value>::type * = 0)\n"
  4254. " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
  4255. // FIXME: This is still incorrectly handled at the formatter side.
  4256. verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
  4257. // FIXME:
  4258. // This now gets parsed incorrectly as class definition.
  4259. // verifyFormat("class A<int> f() {\n}\nint n;");
  4260. // Elaborate types where incorrectly parsing the structural element would
  4261. // break the indent.
  4262. verifyFormat("if (true)\n"
  4263. " class X x;\n"
  4264. "else\n"
  4265. " f();\n");
  4266. // This is simply incomplete. Formatting is not important, but must not crash.
  4267. verifyFormat("class A:");
  4268. }
  4269. TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
  4270. EXPECT_EQ("#error Leave all white!!!!! space* alone!\n",
  4271. format("#error Leave all white!!!!! space* alone!\n"));
  4272. EXPECT_EQ(
  4273. "#warning Leave all white!!!!! space* alone!\n",
  4274. format("#warning Leave all white!!!!! space* alone!\n"));
  4275. EXPECT_EQ("#error 1", format(" # error 1"));
  4276. EXPECT_EQ("#warning 1", format(" # warning 1"));
  4277. }
  4278. TEST_F(FormatTest, FormatHashIfExpressions) {
  4279. // FIXME: Come up with a better indentation for #elif.
  4280. verifyFormat(
  4281. "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
  4282. " defined(BBBBBBBB)\n"
  4283. "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
  4284. " defined(BBBBBBBB)\n"
  4285. "#endif",
  4286. getLLVMStyleWithColumns(65));
  4287. }
  4288. TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
  4289. FormatStyle AllowsMergedIf = getGoogleStyle();
  4290. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  4291. verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
  4292. verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
  4293. verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
  4294. EXPECT_EQ("if (true) return 42;",
  4295. format("if (true)\nreturn 42;", AllowsMergedIf));
  4296. FormatStyle ShortMergedIf = AllowsMergedIf;
  4297. ShortMergedIf.ColumnLimit = 25;
  4298. verifyFormat("#define A \\\n"
  4299. " if (true) return 42;",
  4300. ShortMergedIf);
  4301. verifyFormat("#define A \\\n"
  4302. " f(); \\\n"
  4303. " if (true)\n"
  4304. "#define B",
  4305. ShortMergedIf);
  4306. verifyFormat("#define A \\\n"
  4307. " f(); \\\n"
  4308. " if (true)\n"
  4309. "g();",
  4310. ShortMergedIf);
  4311. verifyFormat("{\n"
  4312. "#ifdef A\n"
  4313. " // Comment\n"
  4314. " if (true) continue;\n"
  4315. "#endif\n"
  4316. " // Comment\n"
  4317. " if (true) continue;\n"
  4318. "}",
  4319. ShortMergedIf);
  4320. }
  4321. TEST_F(FormatTest, BlockCommentsInControlLoops) {
  4322. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  4323. " f();\n"
  4324. "}");
  4325. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  4326. " f();\n"
  4327. "} /* another comment */ else /* comment #3 */ {\n"
  4328. " g();\n"
  4329. "}");
  4330. verifyFormat("while (0) /* a comment in a strange place */ {\n"
  4331. " f();\n"
  4332. "}");
  4333. verifyFormat("for (;;) /* a comment in a strange place */ {\n"
  4334. " f();\n"
  4335. "}");
  4336. verifyFormat("do /* a comment in a strange place */ {\n"
  4337. " f();\n"
  4338. "} /* another comment */ while (0);");
  4339. }
  4340. TEST_F(FormatTest, BlockComments) {
  4341. EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
  4342. format("/* *//* */ /* */\n/* *//* */ /* */"));
  4343. EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
  4344. EXPECT_EQ("#define A /*123*/ \\\n"
  4345. " b\n"
  4346. "/* */\n"
  4347. "someCall(\n"
  4348. " parameter);",
  4349. format("#define A /*123*/ b\n"
  4350. "/* */\n"
  4351. "someCall(parameter);",
  4352. getLLVMStyleWithColumns(15)));
  4353. EXPECT_EQ("#define A\n"
  4354. "/* */ someCall(\n"
  4355. " parameter);",
  4356. format("#define A\n"
  4357. "/* */someCall(parameter);",
  4358. getLLVMStyleWithColumns(15)));
  4359. EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
  4360. EXPECT_EQ("/*\n"
  4361. "*\n"
  4362. " * aaaaaa\n"
  4363. "*aaaaaa\n"
  4364. "*/",
  4365. format("/*\n"
  4366. "*\n"
  4367. " * aaaaaa aaaaaa\n"
  4368. "*/",
  4369. getLLVMStyleWithColumns(10)));
  4370. EXPECT_EQ("/*\n"
  4371. "**\n"
  4372. "* aaaaaa\n"
  4373. "*aaaaaa\n"
  4374. "*/",
  4375. format("/*\n"
  4376. "**\n"
  4377. "* aaaaaa aaaaaa\n"
  4378. "*/",
  4379. getLLVMStyleWithColumns(10)));
  4380. FormatStyle NoBinPacking = getLLVMStyle();
  4381. NoBinPacking.BinPackParameters = false;
  4382. EXPECT_EQ("someFunction(1, /* comment 1 */\n"
  4383. " 2, /* comment 2 */\n"
  4384. " 3, /* comment 3 */\n"
  4385. " aaaa,\n"
  4386. " bbbb);",
  4387. format("someFunction (1, /* comment 1 */\n"
  4388. " 2, /* comment 2 */ \n"
  4389. " 3, /* comment 3 */\n"
  4390. "aaaa, bbbb );",
  4391. NoBinPacking));
  4392. verifyFormat(
  4393. "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  4394. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4395. EXPECT_EQ(
  4396. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  4397. " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  4398. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4399. format(
  4400. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  4401. " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  4402. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
  4403. EXPECT_EQ(
  4404. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  4405. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  4406. "int cccccccccccccccccccccccccccccc; /* comment */\n",
  4407. format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  4408. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  4409. "int cccccccccccccccccccccccccccccc; /* comment */\n"));
  4410. verifyFormat("void f(int * /* unused */) {}");
  4411. EXPECT_EQ("/*\n"
  4412. " **\n"
  4413. " */",
  4414. format("/*\n"
  4415. " **\n"
  4416. " */"));
  4417. EXPECT_EQ("/*\n"
  4418. " *q\n"
  4419. " */",
  4420. format("/*\n"
  4421. " *q\n"
  4422. " */"));
  4423. EXPECT_EQ("/*\n"
  4424. " * q\n"
  4425. " */",
  4426. format("/*\n"
  4427. " * q\n"
  4428. " */"));
  4429. EXPECT_EQ("/*\n"
  4430. " **/",
  4431. format("/*\n"
  4432. " **/"));
  4433. EXPECT_EQ("/*\n"
  4434. " ***/",
  4435. format("/*\n"
  4436. " ***/"));
  4437. }
  4438. TEST_F(FormatTest, BlockCommentsInMacros) {
  4439. EXPECT_EQ("#define A \\\n"
  4440. " { \\\n"
  4441. " /* one line */ \\\n"
  4442. " someCall();",
  4443. format("#define A { \\\n"
  4444. " /* one line */ \\\n"
  4445. " someCall();",
  4446. getLLVMStyleWithColumns(20)));
  4447. EXPECT_EQ("#define A \\\n"
  4448. " { \\\n"
  4449. " /* previous */ \\\n"
  4450. " /* one line */ \\\n"
  4451. " someCall();",
  4452. format("#define A { \\\n"
  4453. " /* previous */ \\\n"
  4454. " /* one line */ \\\n"
  4455. " someCall();",
  4456. getLLVMStyleWithColumns(20)));
  4457. }
  4458. TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
  4459. EXPECT_EQ("a = {\n"
  4460. " 1111 /* */\n"
  4461. "};",
  4462. format("a = {1111 /* */\n"
  4463. "};",
  4464. getLLVMStyleWithColumns(15)));
  4465. EXPECT_EQ("a = {\n"
  4466. " 1111 /* */\n"
  4467. "};",
  4468. format("a = {1111 /* */\n"
  4469. "};",
  4470. getLLVMStyleWithColumns(15)));
  4471. // FIXME: The formatting is still wrong here.
  4472. EXPECT_EQ("a = {\n"
  4473. " 1111 /* a\n"
  4474. " */\n"
  4475. "};",
  4476. format("a = {1111 /* a */\n"
  4477. "};",
  4478. getLLVMStyleWithColumns(15)));
  4479. }
  4480. TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
  4481. // FIXME: This is not what we want...
  4482. verifyFormat("{\n"
  4483. "// a"
  4484. "// b");
  4485. }
  4486. TEST_F(FormatTest, FormatStarDependingOnContext) {
  4487. verifyFormat("void f(int *a);");
  4488. verifyFormat("void f() { f(fint * b); }");
  4489. verifyFormat("class A {\n void f(int *a);\n};");
  4490. verifyFormat("class A {\n int *a;\n};");
  4491. verifyFormat("namespace a {\n"
  4492. "namespace b {\n"
  4493. "class A {\n"
  4494. " void f() {}\n"
  4495. " int *a;\n"
  4496. "};\n"
  4497. "}\n"
  4498. "}");
  4499. }
  4500. TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
  4501. verifyFormat("while");
  4502. verifyFormat("operator");
  4503. }
  4504. //===----------------------------------------------------------------------===//
  4505. // Objective-C tests.
  4506. //===----------------------------------------------------------------------===//
  4507. TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
  4508. verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
  4509. EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
  4510. format("-(NSUInteger)indexOfObject:(id)anObject;"));
  4511. EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
  4512. EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
  4513. EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
  4514. format("-(NSInteger)Method3:(id)anObject;"));
  4515. EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
  4516. format("-(NSInteger)Method4:(id)anObject;"));
  4517. EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
  4518. format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
  4519. EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
  4520. format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
  4521. EXPECT_EQ(
  4522. "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
  4523. format(
  4524. "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
  4525. // Very long objectiveC method declaration.
  4526. verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
  4527. " inRange:(NSRange)range\n"
  4528. " outRange:(NSRange)out_range\n"
  4529. " outRange1:(NSRange)out_range1\n"
  4530. " outRange2:(NSRange)out_range2\n"
  4531. " outRange3:(NSRange)out_range3\n"
  4532. " outRange4:(NSRange)out_range4\n"
  4533. " outRange5:(NSRange)out_range5\n"
  4534. " outRange6:(NSRange)out_range6\n"
  4535. " outRange7:(NSRange)out_range7\n"
  4536. " outRange8:(NSRange)out_range8\n"
  4537. " outRange9:(NSRange)out_range9;");
  4538. verifyFormat("- (int)sum:(vector<int>)numbers;");
  4539. verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
  4540. // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
  4541. // protocol lists (but not for template classes):
  4542. //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
  4543. verifyFormat("- (int (*)())foo:(int (*)())f;");
  4544. verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
  4545. // If there's no return type (very rare in practice!), LLVM and Google style
  4546. // agree.
  4547. verifyFormat("- foo;");
  4548. verifyFormat("- foo:(int)f;");
  4549. verifyGoogleFormat("- foo:(int)foo;");
  4550. }
  4551. TEST_F(FormatTest, FormatObjCBlocks) {
  4552. verifyFormat("int (^Block)(int, int);");
  4553. verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
  4554. }
  4555. TEST_F(FormatTest, FormatObjCInterface) {
  4556. verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
  4557. "@public\n"
  4558. " int field1;\n"
  4559. "@protected\n"
  4560. " int field2;\n"
  4561. "@private\n"
  4562. " int field3;\n"
  4563. "@package\n"
  4564. " int field4;\n"
  4565. "}\n"
  4566. "+ (id)init;\n"
  4567. "@end");
  4568. verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
  4569. " @public\n"
  4570. " int field1;\n"
  4571. " @protected\n"
  4572. " int field2;\n"
  4573. " @private\n"
  4574. " int field3;\n"
  4575. " @package\n"
  4576. " int field4;\n"
  4577. "}\n"
  4578. "+ (id)init;\n"
  4579. "@end");
  4580. verifyFormat("@interface /* wait for it */ Foo\n"
  4581. "+ (id)init;\n"
  4582. "// Look, a comment!\n"
  4583. "- (int)answerWith:(int)i;\n"
  4584. "@end");
  4585. verifyFormat("@interface Foo\n"
  4586. "@end\n"
  4587. "@interface Bar\n"
  4588. "@end");
  4589. verifyFormat("@interface Foo : Bar\n"
  4590. "+ (id)init;\n"
  4591. "@end");
  4592. verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
  4593. "+ (id)init;\n"
  4594. "@end");
  4595. verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
  4596. "+ (id)init;\n"
  4597. "@end");
  4598. verifyFormat("@interface Foo (HackStuff)\n"
  4599. "+ (id)init;\n"
  4600. "@end");
  4601. verifyFormat("@interface Foo ()\n"
  4602. "+ (id)init;\n"
  4603. "@end");
  4604. verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
  4605. "+ (id)init;\n"
  4606. "@end");
  4607. verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
  4608. "+ (id)init;\n"
  4609. "@end");
  4610. verifyFormat("@interface Foo {\n"
  4611. " int _i;\n"
  4612. "}\n"
  4613. "+ (id)init;\n"
  4614. "@end");
  4615. verifyFormat("@interface Foo : Bar {\n"
  4616. " int _i;\n"
  4617. "}\n"
  4618. "+ (id)init;\n"
  4619. "@end");
  4620. verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
  4621. " int _i;\n"
  4622. "}\n"
  4623. "+ (id)init;\n"
  4624. "@end");
  4625. verifyFormat("@interface Foo (HackStuff) {\n"
  4626. " int _i;\n"
  4627. "}\n"
  4628. "+ (id)init;\n"
  4629. "@end");
  4630. verifyFormat("@interface Foo () {\n"
  4631. " int _i;\n"
  4632. "}\n"
  4633. "+ (id)init;\n"
  4634. "@end");
  4635. verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
  4636. " int _i;\n"
  4637. "}\n"
  4638. "+ (id)init;\n"
  4639. "@end");
  4640. }
  4641. TEST_F(FormatTest, FormatObjCImplementation) {
  4642. verifyFormat("@implementation Foo : NSObject {\n"
  4643. "@public\n"
  4644. " int field1;\n"
  4645. "@protected\n"
  4646. " int field2;\n"
  4647. "@private\n"
  4648. " int field3;\n"
  4649. "@package\n"
  4650. " int field4;\n"
  4651. "}\n"
  4652. "+ (id)init {\n}\n"
  4653. "@end");
  4654. verifyGoogleFormat("@implementation Foo : NSObject {\n"
  4655. " @public\n"
  4656. " int field1;\n"
  4657. " @protected\n"
  4658. " int field2;\n"
  4659. " @private\n"
  4660. " int field3;\n"
  4661. " @package\n"
  4662. " int field4;\n"
  4663. "}\n"
  4664. "+ (id)init {\n}\n"
  4665. "@end");
  4666. verifyFormat("@implementation Foo\n"
  4667. "+ (id)init {\n"
  4668. " if (true)\n"
  4669. " return nil;\n"
  4670. "}\n"
  4671. "// Look, a comment!\n"
  4672. "- (int)answerWith:(int)i {\n"
  4673. " return i;\n"
  4674. "}\n"
  4675. "+ (int)answerWith:(int)i {\n"
  4676. " return i;\n"
  4677. "}\n"
  4678. "@end");
  4679. verifyFormat("@implementation Foo\n"
  4680. "@end\n"
  4681. "@implementation Bar\n"
  4682. "@end");
  4683. verifyFormat("@implementation Foo : Bar\n"
  4684. "+ (id)init {\n}\n"
  4685. "- (void)foo {\n}\n"
  4686. "@end");
  4687. verifyFormat("@implementation Foo {\n"
  4688. " int _i;\n"
  4689. "}\n"
  4690. "+ (id)init {\n}\n"
  4691. "@end");
  4692. verifyFormat("@implementation Foo : Bar {\n"
  4693. " int _i;\n"
  4694. "}\n"
  4695. "+ (id)init {\n}\n"
  4696. "@end");
  4697. verifyFormat("@implementation Foo (HackStuff)\n"
  4698. "+ (id)init {\n}\n"
  4699. "@end");
  4700. verifyFormat("@implementation ObjcClass\n"
  4701. "- (void)method;\n"
  4702. "{}\n"
  4703. "@end");
  4704. }
  4705. TEST_F(FormatTest, FormatObjCProtocol) {
  4706. verifyFormat("@protocol Foo\n"
  4707. "@property(weak) id delegate;\n"
  4708. "- (NSUInteger)numberOfThings;\n"
  4709. "@end");
  4710. verifyFormat("@protocol MyProtocol <NSObject>\n"
  4711. "- (NSUInteger)numberOfThings;\n"
  4712. "@end");
  4713. verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
  4714. "- (NSUInteger)numberOfThings;\n"
  4715. "@end");
  4716. verifyFormat("@protocol Foo;\n"
  4717. "@protocol Bar;\n");
  4718. verifyFormat("@protocol Foo\n"
  4719. "@end\n"
  4720. "@protocol Bar\n"
  4721. "@end");
  4722. verifyFormat("@protocol myProtocol\n"
  4723. "- (void)mandatoryWithInt:(int)i;\n"
  4724. "@optional\n"
  4725. "- (void)optional;\n"
  4726. "@required\n"
  4727. "- (void)required;\n"
  4728. "@optional\n"
  4729. "@property(assign) int madProp;\n"
  4730. "@end\n");
  4731. verifyFormat("@property(nonatomic, assign, readonly)\n"
  4732. " int *looooooooooooooooooooooooooooongNumber;\n"
  4733. "@property(nonatomic, assign, readonly)\n"
  4734. " NSString *looooooooooooooooooooooooooooongName;");
  4735. }
  4736. TEST_F(FormatTest, FormatObjCMethodDeclarations) {
  4737. verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
  4738. " rect:(NSRect)theRect\n"
  4739. " interval:(float)theInterval {\n"
  4740. "}");
  4741. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  4742. " longKeyword:(NSRect)theRect\n"
  4743. " evenLongerKeyword:(float)theInterval\n"
  4744. " error:(NSError **)theError {\n"
  4745. "}");
  4746. }
  4747. TEST_F(FormatTest, FormatObjCMethodExpr) {
  4748. verifyFormat("[foo bar:baz];");
  4749. verifyFormat("return [foo bar:baz];");
  4750. verifyFormat("f([foo bar:baz]);");
  4751. verifyFormat("f(2, [foo bar:baz]);");
  4752. verifyFormat("f(2, a ? b : c);");
  4753. verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
  4754. // Unary operators.
  4755. verifyFormat("int a = +[foo bar:baz];");
  4756. verifyFormat("int a = -[foo bar:baz];");
  4757. verifyFormat("int a = ![foo bar:baz];");
  4758. verifyFormat("int a = ~[foo bar:baz];");
  4759. verifyFormat("int a = ++[foo bar:baz];");
  4760. verifyFormat("int a = --[foo bar:baz];");
  4761. verifyFormat("int a = sizeof [foo bar:baz];");
  4762. verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
  4763. verifyFormat("int a = &[foo bar:baz];");
  4764. verifyFormat("int a = *[foo bar:baz];");
  4765. // FIXME: Make casts work, without breaking f()[4].
  4766. //verifyFormat("int a = (int)[foo bar:baz];");
  4767. //verifyFormat("return (int)[foo bar:baz];");
  4768. //verifyFormat("(void)[foo bar:baz];");
  4769. verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
  4770. // Binary operators.
  4771. verifyFormat("[foo bar:baz], [foo bar:baz];");
  4772. verifyFormat("[foo bar:baz] = [foo bar:baz];");
  4773. verifyFormat("[foo bar:baz] *= [foo bar:baz];");
  4774. verifyFormat("[foo bar:baz] /= [foo bar:baz];");
  4775. verifyFormat("[foo bar:baz] %= [foo bar:baz];");
  4776. verifyFormat("[foo bar:baz] += [foo bar:baz];");
  4777. verifyFormat("[foo bar:baz] -= [foo bar:baz];");
  4778. verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
  4779. verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
  4780. verifyFormat("[foo bar:baz] &= [foo bar:baz];");
  4781. verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
  4782. verifyFormat("[foo bar:baz] |= [foo bar:baz];");
  4783. verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
  4784. verifyFormat("[foo bar:baz] || [foo bar:baz];");
  4785. verifyFormat("[foo bar:baz] && [foo bar:baz];");
  4786. verifyFormat("[foo bar:baz] | [foo bar:baz];");
  4787. verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
  4788. verifyFormat("[foo bar:baz] & [foo bar:baz];");
  4789. verifyFormat("[foo bar:baz] == [foo bar:baz];");
  4790. verifyFormat("[foo bar:baz] != [foo bar:baz];");
  4791. verifyFormat("[foo bar:baz] >= [foo bar:baz];");
  4792. verifyFormat("[foo bar:baz] <= [foo bar:baz];");
  4793. verifyFormat("[foo bar:baz] > [foo bar:baz];");
  4794. verifyFormat("[foo bar:baz] < [foo bar:baz];");
  4795. verifyFormat("[foo bar:baz] >> [foo bar:baz];");
  4796. verifyFormat("[foo bar:baz] << [foo bar:baz];");
  4797. verifyFormat("[foo bar:baz] - [foo bar:baz];");
  4798. verifyFormat("[foo bar:baz] + [foo bar:baz];");
  4799. verifyFormat("[foo bar:baz] * [foo bar:baz];");
  4800. verifyFormat("[foo bar:baz] / [foo bar:baz];");
  4801. verifyFormat("[foo bar:baz] % [foo bar:baz];");
  4802. // Whew!
  4803. verifyFormat("return in[42];");
  4804. verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
  4805. "}");
  4806. verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
  4807. verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
  4808. verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
  4809. verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
  4810. verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
  4811. verifyFormat("[button setAction:@selector(zoomOut:)];");
  4812. verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
  4813. verifyFormat("arr[[self indexForFoo:a]];");
  4814. verifyFormat("throw [self errorFor:a];");
  4815. verifyFormat("@throw [self errorFor:a];");
  4816. verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
  4817. verifyFormat("[(id)foo bar:(id) ? baz : quux];");
  4818. verifyFormat("4 > 4 ? (id)a : (id)baz;");
  4819. // This tests that the formatter doesn't break after "backing" but before ":",
  4820. // which would be at 80 columns.
  4821. verifyFormat(
  4822. "void f() {\n"
  4823. " if ((self = [super initWithContentRect:contentRect\n"
  4824. " styleMask:styleMask ?: otherMask\n"
  4825. " backing:NSBackingStoreBuffered\n"
  4826. " defer:YES]))");
  4827. verifyFormat(
  4828. "[foo checkThatBreakingAfterColonWorksOk:\n"
  4829. " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
  4830. verifyFormat("[myObj short:arg1 // Force line break\n"
  4831. " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
  4832. " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
  4833. " error:arg4];");
  4834. verifyFormat(
  4835. "void f() {\n"
  4836. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  4837. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  4838. " pos.width(), pos.height())\n"
  4839. " styleMask:NSBorderlessWindowMask\n"
  4840. " backing:NSBackingStoreBuffered\n"
  4841. " defer:NO]);\n"
  4842. "}");
  4843. verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
  4844. " with:contentsNativeView];");
  4845. verifyFormat(
  4846. "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
  4847. " owner:nillllll];");
  4848. verifyFormat(
  4849. "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
  4850. " forType:kBookmarkButtonDragType];");
  4851. verifyFormat("[defaultCenter addObserver:self\n"
  4852. " selector:@selector(willEnterFullscreen)\n"
  4853. " name:kWillEnterFullscreenNotification\n"
  4854. " object:nil];");
  4855. verifyFormat("[image_rep drawInRect:drawRect\n"
  4856. " fromRect:NSZeroRect\n"
  4857. " operation:NSCompositeCopy\n"
  4858. " fraction:1.0\n"
  4859. " respectFlipped:NO\n"
  4860. " hints:nil];");
  4861. verifyFormat(
  4862. "scoped_nsobject<NSTextField> message(\n"
  4863. " // The frame will be fixed up when |-setMessageText:| is called.\n"
  4864. " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
  4865. verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
  4866. " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
  4867. " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
  4868. " aaaa:bbb];");
  4869. }
  4870. TEST_F(FormatTest, ObjCAt) {
  4871. verifyFormat("@autoreleasepool");
  4872. verifyFormat("@catch");
  4873. verifyFormat("@class");
  4874. verifyFormat("@compatibility_alias");
  4875. verifyFormat("@defs");
  4876. verifyFormat("@dynamic");
  4877. verifyFormat("@encode");
  4878. verifyFormat("@end");
  4879. verifyFormat("@finally");
  4880. verifyFormat("@implementation");
  4881. verifyFormat("@import");
  4882. verifyFormat("@interface");
  4883. verifyFormat("@optional");
  4884. verifyFormat("@package");
  4885. verifyFormat("@private");
  4886. verifyFormat("@property");
  4887. verifyFormat("@protected");
  4888. verifyFormat("@protocol");
  4889. verifyFormat("@public");
  4890. verifyFormat("@required");
  4891. verifyFormat("@selector");
  4892. verifyFormat("@synchronized");
  4893. verifyFormat("@synthesize");
  4894. verifyFormat("@throw");
  4895. verifyFormat("@try");
  4896. EXPECT_EQ("@interface", format("@ interface"));
  4897. // The precise formatting of this doesn't matter, nobody writes code like
  4898. // this.
  4899. verifyFormat("@ /*foo*/ interface");
  4900. }
  4901. TEST_F(FormatTest, ObjCSnippets) {
  4902. verifyFormat("@autoreleasepool {\n"
  4903. " foo();\n"
  4904. "}");
  4905. verifyFormat("@class Foo, Bar;");
  4906. verifyFormat("@compatibility_alias AliasName ExistingClass;");
  4907. verifyFormat("@dynamic textColor;");
  4908. verifyFormat("char *buf1 = @encode(int *);");
  4909. verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
  4910. verifyFormat("char *buf1 = @encode(int **);");
  4911. verifyFormat("Protocol *proto = @protocol(p1);");
  4912. verifyFormat("SEL s = @selector(foo:);");
  4913. verifyFormat("@synchronized(self) {\n"
  4914. " f();\n"
  4915. "}");
  4916. verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  4917. verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  4918. verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
  4919. verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
  4920. verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
  4921. verifyFormat("@import foo.bar;\n"
  4922. "@import baz;");
  4923. }
  4924. TEST_F(FormatTest, ObjCLiterals) {
  4925. verifyFormat("@\"String\"");
  4926. verifyFormat("@1");
  4927. verifyFormat("@+4.8");
  4928. verifyFormat("@-4");
  4929. verifyFormat("@1LL");
  4930. verifyFormat("@.5");
  4931. verifyFormat("@'c'");
  4932. verifyFormat("@true");
  4933. verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
  4934. verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
  4935. verifyFormat("NSNumber *favoriteColor = @(Green);");
  4936. verifyFormat("NSString *path = @(getenv(\"PATH\"));");
  4937. verifyFormat("@[");
  4938. verifyFormat("@[]");
  4939. verifyFormat(
  4940. "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
  4941. verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
  4942. verifyFormat("NSArray *array = @[ [foo description] ];");
  4943. verifyFormat("@{");
  4944. verifyFormat("@{}");
  4945. verifyFormat("@{ @\"one\" : @1 }");
  4946. verifyFormat("return @{ @\"one\" : @1 };");
  4947. verifyFormat("@{ @\"one\" : @1, }");
  4948. verifyFormat("@{ @\"one\" : @{ @2 : @1 } }");
  4949. verifyFormat("@{ @\"one\" : @{ @2 : @1 }, }");
  4950. verifyFormat("@{ 1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2 }");
  4951. verifyFormat("[self setDict:@{}");
  4952. verifyFormat("[self setDict:@{ @1 : @2 }");
  4953. verifyFormat("NSLog(@\"%@\", @{ @1 : @2, @2 : @3 }[@1]);");
  4954. verifyFormat(
  4955. "NSDictionary *masses = @{ @\"H\" : @1.0078, @\"He\" : @4.0026 };");
  4956. verifyFormat(
  4957. "NSDictionary *settings = @{ AVEncoderKey : @(AVAudioQualityMax) };");
  4958. verifyFormat(
  4959. "NSDictionary *d = @{\n"
  4960. " @\"nam\" : NSUserNam(),\n"
  4961. " @\"dte\" : [NSDate date],\n"
  4962. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  4963. "};");
  4964. verifyFormat(
  4965. "@{\n"
  4966. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  4967. "regularFont,\n"
  4968. "};");
  4969. verifyFormat(
  4970. "NSArray *arguments = @[\n"
  4971. " kind == kUserTicket ? @\"--user-store\" : @\"--system-store\",\n"
  4972. " @\"--print-tickets\",\n"
  4973. " @\"--productid\",\n"
  4974. " @\"com.google.Chrome\"\n"
  4975. "];");
  4976. }
  4977. TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
  4978. EXPECT_EQ("{\n"
  4979. "{\n"
  4980. "a;\n"
  4981. "b;\n"
  4982. "}\n"
  4983. "}",
  4984. format("{\n"
  4985. "{\n"
  4986. "a;\n"
  4987. " b;\n"
  4988. "}\n"
  4989. "}",
  4990. 13, 2, getLLVMStyle()));
  4991. EXPECT_EQ("{\n"
  4992. "{\n"
  4993. " a;\n"
  4994. "b;\n"
  4995. "}\n"
  4996. "}",
  4997. format("{\n"
  4998. "{\n"
  4999. " a;\n"
  5000. "b;\n"
  5001. "}\n"
  5002. "}",
  5003. 9, 2, getLLVMStyle()));
  5004. EXPECT_EQ("{\n"
  5005. "{\n"
  5006. "public:\n"
  5007. " b;\n"
  5008. "}\n"
  5009. "}",
  5010. format("{\n"
  5011. "{\n"
  5012. "public:\n"
  5013. " b;\n"
  5014. "}\n"
  5015. "}",
  5016. 17, 2, getLLVMStyle()));
  5017. EXPECT_EQ("{\n"
  5018. "{\n"
  5019. "a;\n"
  5020. "}\n"
  5021. "{\n"
  5022. " b; //\n"
  5023. "}\n"
  5024. "}",
  5025. format("{\n"
  5026. "{\n"
  5027. "a;\n"
  5028. "}\n"
  5029. "{\n"
  5030. " b; //\n"
  5031. "}\n"
  5032. "}",
  5033. 22, 2, getLLVMStyle()));
  5034. EXPECT_EQ(" {\n"
  5035. " a; //\n"
  5036. " }",
  5037. format(" {\n"
  5038. "a; //\n"
  5039. " }",
  5040. 4, 2, getLLVMStyle()));
  5041. EXPECT_EQ("void f() {}\n"
  5042. "void g() {}",
  5043. format("void f() {}\n"
  5044. "void g() {}",
  5045. 13, 0, getLLVMStyle()));
  5046. EXPECT_EQ("int a; // comment\n"
  5047. " // line 2\n"
  5048. "int b;",
  5049. format("int a; // comment\n"
  5050. " // line 2\n"
  5051. " int b;",
  5052. 35, 0, getLLVMStyle()));
  5053. EXPECT_EQ(" int a;\n"
  5054. " void\n"
  5055. " ffffff() {\n"
  5056. " }",
  5057. format(" int a;\n"
  5058. "void ffffff() {}",
  5059. 11, 0, getLLVMStyleWithColumns(11)));
  5060. }
  5061. TEST_F(FormatTest, BreakStringLiterals) {
  5062. EXPECT_EQ("\"some text \"\n"
  5063. "\"other\";",
  5064. format("\"some text other\";", getLLVMStyleWithColumns(12)));
  5065. EXPECT_EQ("\"some text \"\n"
  5066. "\"other\";",
  5067. format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
  5068. EXPECT_EQ(
  5069. "#define A \\\n"
  5070. " \"some \" \\\n"
  5071. " \"text \" \\\n"
  5072. " \"other\";",
  5073. format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
  5074. EXPECT_EQ(
  5075. "#define A \\\n"
  5076. " \"so \" \\\n"
  5077. " \"text \" \\\n"
  5078. " \"other\";",
  5079. format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
  5080. EXPECT_EQ("\"some text\"",
  5081. format("\"some text\"", getLLVMStyleWithColumns(1)));
  5082. EXPECT_EQ("\"some text\"",
  5083. format("\"some text\"", getLLVMStyleWithColumns(11)));
  5084. EXPECT_EQ("\"some \"\n"
  5085. "\"text\"",
  5086. format("\"some text\"", getLLVMStyleWithColumns(10)));
  5087. EXPECT_EQ("\"some \"\n"
  5088. "\"text\"",
  5089. format("\"some text\"", getLLVMStyleWithColumns(7)));
  5090. EXPECT_EQ("\"some\"\n"
  5091. "\" tex\"\n"
  5092. "\"t\"",
  5093. format("\"some text\"", getLLVMStyleWithColumns(6)));
  5094. EXPECT_EQ("\"some\"\n"
  5095. "\" tex\"\n"
  5096. "\" and\"",
  5097. format("\"some tex and\"", getLLVMStyleWithColumns(6)));
  5098. EXPECT_EQ("\"some\"\n"
  5099. "\"/tex\"\n"
  5100. "\"/and\"",
  5101. format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
  5102. EXPECT_EQ("variable =\n"
  5103. " \"long string \"\n"
  5104. " \"literal\";",
  5105. format("variable = \"long string literal\";",
  5106. getLLVMStyleWithColumns(20)));
  5107. EXPECT_EQ("variable = f(\n"
  5108. " \"long string \"\n"
  5109. " \"literal\",\n"
  5110. " short,\n"
  5111. " loooooooooooooooooooong);",
  5112. format("variable = f(\"long string literal\", short, "
  5113. "loooooooooooooooooooong);",
  5114. getLLVMStyleWithColumns(20)));
  5115. EXPECT_EQ("f(g(\"long string \"\n"
  5116. " \"literal\"),\n"
  5117. " b);",
  5118. format("f(g(\"long string literal\"), b);",
  5119. getLLVMStyleWithColumns(20)));
  5120. EXPECT_EQ("f(g(\"long string \"\n"
  5121. " \"literal\",\n"
  5122. " a),\n"
  5123. " b);",
  5124. format("f(g(\"long string literal\", a), b);",
  5125. getLLVMStyleWithColumns(20)));
  5126. EXPECT_EQ(
  5127. "f(\"one two\".split(\n"
  5128. " variable));",
  5129. format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
  5130. EXPECT_EQ("f(\"one two three four five six \"\n"
  5131. " \"seven\".split(\n"
  5132. " really_looooong_variable));",
  5133. format("f(\"one two three four five six seven\"."
  5134. "split(really_looooong_variable));",
  5135. getLLVMStyleWithColumns(33)));
  5136. EXPECT_EQ("f(\"some \"\n"
  5137. " \"text\",\n"
  5138. " other);",
  5139. format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
  5140. // Only break as a last resort.
  5141. verifyFormat(
  5142. "aaaaaaaaaaaaaaaaaaaa(\n"
  5143. " aaaaaaaaaaaaaaaaaaaa,\n"
  5144. " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
  5145. EXPECT_EQ(
  5146. "\"splitmea\"\n"
  5147. "\"trandomp\"\n"
  5148. "\"oint\"",
  5149. format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
  5150. EXPECT_EQ(
  5151. "\"split/\"\n"
  5152. "\"pathat/\"\n"
  5153. "\"slashes\"",
  5154. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  5155. EXPECT_EQ(
  5156. "\"split/\"\n"
  5157. "\"pathat/\"\n"
  5158. "\"slashes\"",
  5159. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  5160. EXPECT_EQ("\"split at \"\n"
  5161. "\"spaces/at/\"\n"
  5162. "\"slashes.at.any$\"\n"
  5163. "\"non-alphanumeric%\"\n"
  5164. "\"1111111111characte\"\n"
  5165. "\"rs\"",
  5166. format("\"split at "
  5167. "spaces/at/"
  5168. "slashes.at."
  5169. "any$non-"
  5170. "alphanumeric%"
  5171. "1111111111characte"
  5172. "rs\"",
  5173. getLLVMStyleWithColumns(20)));
  5174. // Verify that splitting the strings understands
  5175. // Style::AlwaysBreakBeforeMultilineStrings.
  5176. EXPECT_EQ("aaaaaaaaaaaa(aaaaaaaaaaaaa,\n"
  5177. " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
  5178. " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
  5179. format("aaaaaaaaaaaa(aaaaaaaaaaaaa, \"aaaaaaaaaaaaaaaaaaaaaa "
  5180. "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa "
  5181. "aaaaaaaaaaaaaaaaaaaaaa\");",
  5182. getGoogleStyle()));
  5183. EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  5184. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
  5185. format("return \"aaaaaaaaaaaaaaaaaaaaaa "
  5186. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
  5187. "aaaaaaaaaaaaaaaaaaaaaa\";",
  5188. getGoogleStyle()));
  5189. EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  5190. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  5191. format("llvm::outs() << "
  5192. "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
  5193. "aaaaaaaaaaaaaaaaaaa\";"));
  5194. EXPECT_EQ("ffff(\n"
  5195. " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  5196. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  5197. format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  5198. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  5199. getGoogleStyle()));
  5200. FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
  5201. AlignLeft.AlignEscapedNewlinesLeft = true;
  5202. EXPECT_EQ(
  5203. "#define A \\\n"
  5204. " \"some \" \\\n"
  5205. " \"text \" \\\n"
  5206. " \"other\";",
  5207. format("#define A \"some text other\";", AlignLeft));
  5208. }
  5209. TEST_F(FormatTest, BreaksWideStringLiterals) {
  5210. EXPECT_EQ(
  5211. "u8\"utf8 string \"\n"
  5212. "u8\"literal\";",
  5213. format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
  5214. EXPECT_EQ(
  5215. "u\"utf16 string \"\n"
  5216. "u\"literal\";",
  5217. format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
  5218. EXPECT_EQ(
  5219. "U\"utf32 string \"\n"
  5220. "U\"literal\";",
  5221. format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
  5222. EXPECT_EQ("L\"wide string \"\n"
  5223. "L\"literal\";",
  5224. format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
  5225. }
  5226. TEST_F(FormatTest, BreaksRawStringLiterals) {
  5227. EXPECT_EQ("R\"x(raw )x\"\n"
  5228. "R\"x(literal)x\";",
  5229. format("R\"x(raw literal)x\";", getGoogleStyleWithColumns(15)));
  5230. EXPECT_EQ("uR\"x(raw )x\"\n"
  5231. "uR\"x(literal)x\";",
  5232. format("uR\"x(raw literal)x\";", getGoogleStyleWithColumns(16)));
  5233. EXPECT_EQ("u8R\"x(raw )x\"\n"
  5234. "u8R\"x(literal)x\";",
  5235. format("u8R\"x(raw literal)x\";", getGoogleStyleWithColumns(17)));
  5236. EXPECT_EQ("LR\"x(raw )x\"\n"
  5237. "LR\"x(literal)x\";",
  5238. format("LR\"x(raw literal)x\";", getGoogleStyleWithColumns(16)));
  5239. EXPECT_EQ("UR\"x(raw )x\"\n"
  5240. "UR\"x(literal)x\";",
  5241. format("UR\"x(raw literal)x\";", getGoogleStyleWithColumns(16)));
  5242. }
  5243. TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
  5244. FormatStyle Style = getLLVMStyleWithColumns(20);
  5245. EXPECT_EQ(
  5246. "_T(\"aaaaaaaaaaaaaa\")\n"
  5247. "_T(\"aaaaaaaaaaaaaa\")\n"
  5248. "_T(\"aaaaaaaaaaaa\")",
  5249. format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
  5250. EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
  5251. " _T(\"aaaaaa\"),\n"
  5252. " z);",
  5253. format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
  5254. // FIXME: Handle embedded spaces in one iteration.
  5255. // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
  5256. // "_T(\"aaaaaaaaaaaaa\")\n"
  5257. // "_T(\"aaaaaaaaaaaaa\")\n"
  5258. // "_T(\"a\")",
  5259. // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  5260. // getLLVMStyleWithColumns(20)));
  5261. EXPECT_EQ(
  5262. "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  5263. format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
  5264. }
  5265. TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
  5266. EXPECT_EQ(
  5267. "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  5268. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  5269. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  5270. format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  5271. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  5272. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
  5273. }
  5274. TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
  5275. EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
  5276. format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle()));
  5277. EXPECT_EQ("fffffffffff(g(R\"x(\n"
  5278. "multiline raw string literal xxxxxxxxxxxxxx\n"
  5279. ")x\",\n"
  5280. " a),\n"
  5281. " b);",
  5282. format("fffffffffff(g(R\"x(\n"
  5283. "multiline raw string literal xxxxxxxxxxxxxx\n"
  5284. ")x\", a), b);",
  5285. getGoogleStyleWithColumns(20)));
  5286. EXPECT_EQ("fffffffffff(\n"
  5287. " g(R\"x(qqq\n"
  5288. "multiline raw string literal xxxxxxxxxxxxxx\n"
  5289. ")x\",\n"
  5290. " a),\n"
  5291. " b);",
  5292. format("fffffffffff(g(R\"x(qqq\n"
  5293. "multiline raw string literal xxxxxxxxxxxxxx\n"
  5294. ")x\", a), b);",
  5295. getGoogleStyleWithColumns(20)));
  5296. EXPECT_EQ("fffffffffff(R\"x(\n"
  5297. "multiline raw string literal xxxxxxxxxxxxxx\n"
  5298. ")x\");",
  5299. format("fffffffffff(R\"x(\n"
  5300. "multiline raw string literal xxxxxxxxxxxxxx\n"
  5301. ")x\");",
  5302. getGoogleStyleWithColumns(20)));
  5303. EXPECT_EQ("fffffffffff(R\"x(\n"
  5304. "multiline raw string literal xxxxxxxxxxxxxx\n"
  5305. ")x\" +\n"
  5306. " bbbbbb);",
  5307. format("fffffffffff(R\"x(\n"
  5308. "multiline raw string literal xxxxxxxxxxxxxx\n"
  5309. ")x\" + bbbbbb);",
  5310. getGoogleStyleWithColumns(20)));
  5311. }
  5312. TEST_F(FormatTest, SkipsUnknownStringLiterals) {
  5313. verifyFormat("string a = \"unterminated;");
  5314. EXPECT_EQ("function(\"unterminated,\n"
  5315. " OtherParameter);",
  5316. format("function( \"unterminated,\n"
  5317. " OtherParameter);"));
  5318. }
  5319. TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
  5320. EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
  5321. format("#define x(_a) printf(\"foo\"_a);", getLLVMStyle()));
  5322. }
  5323. TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
  5324. EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
  5325. " \"ddeeefff\");",
  5326. format("someFunction(\"aaabbbcccdddeeefff\");",
  5327. getLLVMStyleWithColumns(25)));
  5328. EXPECT_EQ("someFunction1234567890(\n"
  5329. " \"aaabbbcccdddeeefff\");",
  5330. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  5331. getLLVMStyleWithColumns(26)));
  5332. EXPECT_EQ("someFunction1234567890(\n"
  5333. " \"aaabbbcccdddeeeff\"\n"
  5334. " \"f\");",
  5335. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  5336. getLLVMStyleWithColumns(25)));
  5337. EXPECT_EQ("someFunction1234567890(\n"
  5338. " \"aaabbbcccdddeeeff\"\n"
  5339. " \"f\");",
  5340. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  5341. getLLVMStyleWithColumns(24)));
  5342. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  5343. " \"ddde \"\n"
  5344. " \"efff\");",
  5345. format("someFunction(\"aaabbbcc ddde efff\");",
  5346. getLLVMStyleWithColumns(25)));
  5347. EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
  5348. " \"ddeeefff\");",
  5349. format("someFunction(\"aaabbbccc ddeeefff\");",
  5350. getLLVMStyleWithColumns(25)));
  5351. EXPECT_EQ("someFunction1234567890(\n"
  5352. " \"aaabb \"\n"
  5353. " \"cccdddeeefff\");",
  5354. format("someFunction1234567890(\"aaabb cccdddeeefff\");",
  5355. getLLVMStyleWithColumns(25)));
  5356. EXPECT_EQ("#define A \\\n"
  5357. " string s = \\\n"
  5358. " \"123456789\" \\\n"
  5359. " \"0\"; \\\n"
  5360. " int i;",
  5361. format("#define A string s = \"1234567890\"; int i;",
  5362. getLLVMStyleWithColumns(20)));
  5363. // FIXME: Put additional penalties on breaking at non-whitespace locations.
  5364. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  5365. " \"dddeeeff\"\n"
  5366. " \"f\");",
  5367. format("someFunction(\"aaabbbcc dddeeefff\");",
  5368. getLLVMStyleWithColumns(25)));
  5369. }
  5370. TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
  5371. EXPECT_EQ("\"\\a\"",
  5372. format("\"\\a\"", getLLVMStyleWithColumns(3)));
  5373. EXPECT_EQ("\"\\\"",
  5374. format("\"\\\"", getLLVMStyleWithColumns(2)));
  5375. EXPECT_EQ("\"test\"\n"
  5376. "\"\\n\"",
  5377. format("\"test\\n\"", getLLVMStyleWithColumns(7)));
  5378. EXPECT_EQ("\"tes\\\\\"\n"
  5379. "\"n\"",
  5380. format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
  5381. EXPECT_EQ("\"\\\\\\\\\"\n"
  5382. "\"\\n\"",
  5383. format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
  5384. EXPECT_EQ("\"\\uff01\"",
  5385. format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
  5386. EXPECT_EQ("\"\\uff01\"\n"
  5387. "\"test\"",
  5388. format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
  5389. EXPECT_EQ("\"\\Uff01ff02\"",
  5390. format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
  5391. EXPECT_EQ("\"\\x000000000001\"\n"
  5392. "\"next\"",
  5393. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
  5394. EXPECT_EQ("\"\\x000000000001next\"",
  5395. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
  5396. EXPECT_EQ("\"\\x000000000001\"",
  5397. format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
  5398. EXPECT_EQ("\"test\"\n"
  5399. "\"\\000000\"\n"
  5400. "\"000001\"",
  5401. format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
  5402. EXPECT_EQ("\"test\\000\"\n"
  5403. "\"00000000\"\n"
  5404. "\"1\"",
  5405. format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
  5406. // FIXME: We probably don't need to care about escape sequences in raw
  5407. // literals.
  5408. EXPECT_EQ("R\"(\\x)\"\n"
  5409. "R\"(\\x00)\"\n",
  5410. format("R\"(\\x\\x00)\"\n", getGoogleStyleWithColumns(7)));
  5411. }
  5412. TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
  5413. verifyFormat("void f() {\n"
  5414. " return g() {}\n"
  5415. " void h() {}");
  5416. verifyFormat("if (foo)\n"
  5417. " return { forgot_closing_brace();\n"
  5418. "test();");
  5419. verifyFormat("int a[] = { void forgot_closing_brace() { f();\n"
  5420. "g();\n"
  5421. "}");
  5422. }
  5423. TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
  5424. verifyFormat("class X {\n"
  5425. " void f() {\n"
  5426. " }\n"
  5427. "};",
  5428. getLLVMStyleWithColumns(12));
  5429. }
  5430. TEST_F(FormatTest, ConfigurableIndentWidth) {
  5431. FormatStyle EightIndent = getLLVMStyleWithColumns(18);
  5432. EightIndent.IndentWidth = 8;
  5433. verifyFormat("void f() {\n"
  5434. " someFunction();\n"
  5435. " if (true) {\n"
  5436. " f();\n"
  5437. " }\n"
  5438. "}",
  5439. EightIndent);
  5440. verifyFormat("class X {\n"
  5441. " void f() {\n"
  5442. " }\n"
  5443. "};",
  5444. EightIndent);
  5445. verifyFormat("int x[] = {\n"
  5446. " call(),\n"
  5447. " call(),\n"
  5448. "};",
  5449. EightIndent);
  5450. }
  5451. TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
  5452. verifyFormat("void\n"
  5453. "f();",
  5454. getLLVMStyleWithColumns(8));
  5455. }
  5456. TEST_F(FormatTest, ConfigurableUseOfTab) {
  5457. FormatStyle Tab = getLLVMStyleWithColumns(42);
  5458. Tab.IndentWidth = 8;
  5459. Tab.UseTab = FormatStyle::UT_Always;
  5460. Tab.AlignEscapedNewlinesLeft = true;
  5461. EXPECT_EQ("if (aaaaaaaa && // q\n"
  5462. " bb)\t\t// w\n"
  5463. "\t;",
  5464. format("if (aaaaaaaa &&// q\n"
  5465. "bb)// w\n"
  5466. ";",
  5467. Tab));
  5468. EXPECT_EQ("if (aaa && bbb) // w\n"
  5469. "\t;",
  5470. format("if(aaa&&bbb)// w\n"
  5471. ";",
  5472. Tab));
  5473. verifyFormat("class X {\n"
  5474. "\tvoid f() {\n"
  5475. "\t\tsomeFunction(parameter1,\n"
  5476. "\t\t\t parameter2);\n"
  5477. "\t}\n"
  5478. "};",
  5479. Tab);
  5480. verifyFormat("#define A \\\n"
  5481. "\tvoid f() { \\\n"
  5482. "\t\tsomeFunction( \\\n"
  5483. "\t\t parameter1, \\\n"
  5484. "\t\t parameter2); \\\n"
  5485. "\t}",
  5486. Tab);
  5487. EXPECT_EQ("void f() {\n"
  5488. "\tf();\n"
  5489. "\tg();\n"
  5490. "}",
  5491. format("void f() {\n"
  5492. "\tf();\n"
  5493. "\tg();\n"
  5494. "}",
  5495. 0, 0, Tab));
  5496. EXPECT_EQ("void f() {\n"
  5497. "\tf();\n"
  5498. "\tg();\n"
  5499. "}",
  5500. format("void f() {\n"
  5501. "\tf();\n"
  5502. "\tg();\n"
  5503. "}",
  5504. 16, 0, Tab));
  5505. EXPECT_EQ("void f() {\n"
  5506. " \tf();\n"
  5507. "\tg();\n"
  5508. "}",
  5509. format("void f() {\n"
  5510. " \tf();\n"
  5511. " \tg();\n"
  5512. "}",
  5513. 21, 0, Tab));
  5514. Tab.TabWidth = 4;
  5515. Tab.IndentWidth = 8;
  5516. verifyFormat("class TabWidth4Indent8 {\n"
  5517. "\t\tvoid f() {\n"
  5518. "\t\t\t\tsomeFunction(parameter1,\n"
  5519. "\t\t\t\t\t\t\t parameter2);\n"
  5520. "\t\t}\n"
  5521. "};",
  5522. Tab);
  5523. Tab.TabWidth = 4;
  5524. Tab.IndentWidth = 4;
  5525. verifyFormat("class TabWidth4Indent4 {\n"
  5526. "\tvoid f() {\n"
  5527. "\t\tsomeFunction(parameter1,\n"
  5528. "\t\t\t\t\t parameter2);\n"
  5529. "\t}\n"
  5530. "};",
  5531. Tab);
  5532. Tab.TabWidth = 8;
  5533. Tab.IndentWidth = 4;
  5534. verifyFormat("class TabWidth8Indent4 {\n"
  5535. " void f() {\n"
  5536. "\tsomeFunction(parameter1,\n"
  5537. "\t\t parameter2);\n"
  5538. " }\n"
  5539. "};",
  5540. Tab);
  5541. Tab.TabWidth = 8;
  5542. Tab.IndentWidth = 8;
  5543. EXPECT_EQ("/*\n"
  5544. "\t a\t\tcomment\n"
  5545. "\t in multiple lines\n"
  5546. " */",
  5547. format(" /*\t \t \n"
  5548. " \t \t a\t\tcomment\t \t\n"
  5549. " \t \t in multiple lines\t\n"
  5550. " \t */",
  5551. Tab));
  5552. Tab.UseTab = FormatStyle::UT_ForIndentation;
  5553. verifyFormat("T t[] = {\n"
  5554. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5555. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5556. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5557. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5558. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5559. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5560. "};",
  5561. Tab);
  5562. verifyFormat("enum A {\n"
  5563. "\ta1,\n"
  5564. "\ta2,\n"
  5565. "\ta3\n"
  5566. "};",
  5567. Tab);
  5568. EXPECT_EQ("if (aaaaaaaa && // q\n"
  5569. " bb) // w\n"
  5570. "\t;",
  5571. format("if (aaaaaaaa &&// q\n"
  5572. "bb)// w\n"
  5573. ";",
  5574. Tab));
  5575. verifyFormat("class X {\n"
  5576. "\tvoid f() {\n"
  5577. "\t\tsomeFunction(parameter1,\n"
  5578. "\t\t parameter2);\n"
  5579. "\t}\n"
  5580. "};",
  5581. Tab);
  5582. verifyFormat("{\n"
  5583. "\tQ({\n"
  5584. "\t\t int a;\n"
  5585. "\t\t someFunction(aaaaaaaaaa,\n"
  5586. "\t\t bbbbbbbbb);\n"
  5587. "\t },\n"
  5588. "\t p);\n"
  5589. "}",
  5590. Tab);
  5591. EXPECT_EQ("{\n"
  5592. "\t/* aaaa\n"
  5593. "\t bbbb */\n"
  5594. "}",
  5595. format("{\n"
  5596. "/* aaaa\n"
  5597. " bbbb */\n"
  5598. "}",
  5599. Tab));
  5600. EXPECT_EQ("{\n"
  5601. "\t/*\n"
  5602. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5603. "\t bbbbbbbbbbbbb\n"
  5604. "\t*/\n"
  5605. "}",
  5606. format("{\n"
  5607. "/*\n"
  5608. " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  5609. "*/\n"
  5610. "}",
  5611. Tab));
  5612. EXPECT_EQ("{\n"
  5613. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5614. "\t// bbbbbbbbbbbbb\n"
  5615. "}",
  5616. format("{\n"
  5617. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  5618. "}",
  5619. Tab));
  5620. EXPECT_EQ("{\n"
  5621. "\t/*\n"
  5622. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5623. "\t bbbbbbbbbbbbb\n"
  5624. "\t*/\n"
  5625. "}",
  5626. format("{\n"
  5627. "\t/*\n"
  5628. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  5629. "\t*/\n"
  5630. "}",
  5631. Tab));
  5632. EXPECT_EQ("{\n"
  5633. "\t/*\n"
  5634. "\n"
  5635. "\t*/\n"
  5636. "}",
  5637. format("{\n"
  5638. "\t/*\n"
  5639. "\n"
  5640. "\t*/\n"
  5641. "}",
  5642. Tab));
  5643. EXPECT_EQ("{\n"
  5644. "\t/*\n"
  5645. " asdf\n"
  5646. "\t*/\n"
  5647. "}",
  5648. format("{\n"
  5649. "\t/*\n"
  5650. " asdf\n"
  5651. "\t*/\n"
  5652. "}",
  5653. Tab));
  5654. Tab.UseTab = FormatStyle::UT_Never;
  5655. EXPECT_EQ("/*\n"
  5656. " a\t\tcomment\n"
  5657. " in multiple lines\n"
  5658. " */",
  5659. format(" /*\t \t \n"
  5660. " \t \t a\t\tcomment\t \t\n"
  5661. " \t \t in multiple lines\t\n"
  5662. " \t */",
  5663. Tab));
  5664. EXPECT_EQ("/* some\n"
  5665. " comment */",
  5666. format(" \t \t /* some\n"
  5667. " \t \t comment */",
  5668. Tab));
  5669. EXPECT_EQ("int a; /* some\n"
  5670. " comment */",
  5671. format(" \t \t int a; /* some\n"
  5672. " \t \t comment */",
  5673. Tab));
  5674. EXPECT_EQ("int a; /* some\n"
  5675. "comment */",
  5676. format(" \t \t int\ta; /* some\n"
  5677. " \t \t comment */",
  5678. Tab));
  5679. EXPECT_EQ("f(\"\t\t\"); /* some\n"
  5680. " comment */",
  5681. format(" \t \t f(\"\t\t\"); /* some\n"
  5682. " \t \t comment */",
  5683. Tab));
  5684. EXPECT_EQ("{\n"
  5685. " /*\n"
  5686. " * Comment\n"
  5687. " */\n"
  5688. " int i;\n"
  5689. "}",
  5690. format("{\n"
  5691. "\t/*\n"
  5692. "\t * Comment\n"
  5693. "\t */\n"
  5694. "\t int i;\n"
  5695. "}"));
  5696. }
  5697. TEST_F(FormatTest, CalculatesOriginalColumn) {
  5698. EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  5699. "q\"; /* some\n"
  5700. " comment */",
  5701. format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  5702. "q\"; /* some\n"
  5703. " comment */",
  5704. getLLVMStyle()));
  5705. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  5706. "/* some\n"
  5707. " comment */",
  5708. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  5709. " /* some\n"
  5710. " comment */",
  5711. getLLVMStyle()));
  5712. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  5713. "qqq\n"
  5714. "/* some\n"
  5715. " comment */",
  5716. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  5717. "qqq\n"
  5718. " /* some\n"
  5719. " comment */",
  5720. getLLVMStyle()));
  5721. EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  5722. "wwww; /* some\n"
  5723. " comment */",
  5724. format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  5725. "wwww; /* some\n"
  5726. " comment */",
  5727. getLLVMStyle()));
  5728. }
  5729. TEST_F(FormatTest, ConfigurableSpaceAfterControlStatementKeyword) {
  5730. FormatStyle NoSpace = getLLVMStyle();
  5731. NoSpace.SpaceAfterControlStatementKeyword = false;
  5732. verifyFormat("while(true)\n"
  5733. " continue;", NoSpace);
  5734. verifyFormat("for(;;)\n"
  5735. " continue;", NoSpace);
  5736. verifyFormat("if(true)\n"
  5737. " f();\n"
  5738. "else if(true)\n"
  5739. " f();", NoSpace);
  5740. verifyFormat("do {\n"
  5741. " do_something();\n"
  5742. "} while(something());", NoSpace);
  5743. verifyFormat("switch(x) {\n"
  5744. "default:\n"
  5745. " break;\n"
  5746. "}", NoSpace);
  5747. }
  5748. TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
  5749. FormatStyle Spaces = getLLVMStyle();
  5750. Spaces.SpacesInParentheses = true;
  5751. verifyFormat("call( x, y, z );", Spaces);
  5752. verifyFormat("while ( (bool)1 )\n"
  5753. " continue;", Spaces);
  5754. verifyFormat("for ( ;; )\n"
  5755. " continue;", Spaces);
  5756. verifyFormat("if ( true )\n"
  5757. " f();\n"
  5758. "else if ( true )\n"
  5759. " f();", Spaces);
  5760. verifyFormat("do {\n"
  5761. " do_something( (int)i );\n"
  5762. "} while ( something() );", Spaces);
  5763. verifyFormat("switch ( x ) {\n"
  5764. "default:\n"
  5765. " break;\n"
  5766. "}", Spaces);
  5767. Spaces.SpacesInParentheses = false;
  5768. Spaces.SpacesInCStyleCastParentheses = true;
  5769. verifyFormat("Type *A = ( Type * )P;", Spaces);
  5770. verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
  5771. verifyFormat("x = ( int32 )y;", Spaces);
  5772. verifyFormat("int a = ( int )(2.0f);", Spaces);
  5773. verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
  5774. verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
  5775. verifyFormat("#define x (( int )-1)", Spaces);
  5776. Spaces.SpacesInParentheses = false;
  5777. Spaces.SpaceInEmptyParentheses = true;
  5778. verifyFormat("call(x, y, z);", Spaces);
  5779. verifyFormat("call( )", Spaces);
  5780. // Run the first set of tests again with
  5781. // Spaces.SpacesInParentheses = false,
  5782. // Spaces.SpaceInEmptyParentheses = true and
  5783. // Spaces.SpacesInCStyleCastParentheses = true
  5784. Spaces.SpacesInParentheses = false,
  5785. Spaces.SpaceInEmptyParentheses = true;
  5786. Spaces.SpacesInCStyleCastParentheses = true;
  5787. verifyFormat("call(x, y, z);", Spaces);
  5788. verifyFormat("while (( bool )1)\n"
  5789. " continue;", Spaces);
  5790. verifyFormat("for (;;)\n"
  5791. " continue;", Spaces);
  5792. verifyFormat("if (true)\n"
  5793. " f( );\n"
  5794. "else if (true)\n"
  5795. " f( );", Spaces);
  5796. verifyFormat("do {\n"
  5797. " do_something(( int )i);\n"
  5798. "} while (something( ));", Spaces);
  5799. verifyFormat("switch (x) {\n"
  5800. "default:\n"
  5801. " break;\n"
  5802. "}", Spaces);
  5803. }
  5804. TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
  5805. verifyFormat("int a = 5;");
  5806. verifyFormat("a += 42;");
  5807. verifyFormat("a or_eq 8;");
  5808. FormatStyle Spaces = getLLVMStyle();
  5809. Spaces.SpaceBeforeAssignmentOperators = false;
  5810. verifyFormat("int a= 5;", Spaces);
  5811. verifyFormat("a+= 42;", Spaces);
  5812. verifyFormat("a or_eq 8;", Spaces);
  5813. }
  5814. TEST_F(FormatTest, LinuxBraceBreaking) {
  5815. FormatStyle BreakBeforeBrace = getLLVMStyle();
  5816. BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Linux;
  5817. verifyFormat("namespace a\n"
  5818. "{\n"
  5819. "class A\n"
  5820. "{\n"
  5821. " void f()\n"
  5822. " {\n"
  5823. " if (true) {\n"
  5824. " a();\n"
  5825. " b();\n"
  5826. " }\n"
  5827. " }\n"
  5828. " void g()\n"
  5829. " {\n"
  5830. " return;\n"
  5831. " }\n"
  5832. "}\n"
  5833. "}",
  5834. BreakBeforeBrace);
  5835. }
  5836. TEST_F(FormatTest, StroustrupBraceBreaking) {
  5837. FormatStyle BreakBeforeBrace = getLLVMStyle();
  5838. BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  5839. verifyFormat("namespace a {\n"
  5840. "class A {\n"
  5841. " void f()\n"
  5842. " {\n"
  5843. " if (true) {\n"
  5844. " a();\n"
  5845. " b();\n"
  5846. " }\n"
  5847. " }\n"
  5848. " void g()\n"
  5849. " {\n"
  5850. " return;\n"
  5851. " }\n"
  5852. "}\n"
  5853. "}",
  5854. BreakBeforeBrace);
  5855. }
  5856. TEST_F(FormatTest, AllmanBraceBreaking) {
  5857. FormatStyle BreakBeforeBrace = getLLVMStyle();
  5858. BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Allman;
  5859. verifyFormat("namespace a\n"
  5860. "{\n"
  5861. "class A\n"
  5862. "{\n"
  5863. " void f()\n"
  5864. " {\n"
  5865. " if (true)\n"
  5866. " {\n"
  5867. " a();\n"
  5868. " b();\n"
  5869. " }\n"
  5870. " }\n"
  5871. " void g()\n"
  5872. " {\n"
  5873. " return;\n"
  5874. " }\n"
  5875. "}\n"
  5876. "}",
  5877. BreakBeforeBrace);
  5878. verifyFormat("void f()\n"
  5879. "{\n"
  5880. " if (true)\n"
  5881. " {\n"
  5882. " a();\n"
  5883. " }\n"
  5884. " else if (false)\n"
  5885. " {\n"
  5886. " b();\n"
  5887. " }\n"
  5888. " else\n"
  5889. " {\n"
  5890. " c();\n"
  5891. " }\n"
  5892. "}\n",
  5893. BreakBeforeBrace);
  5894. verifyFormat("void f()\n"
  5895. "{\n"
  5896. " for (int i = 0; i < 10; ++i)\n"
  5897. " {\n"
  5898. " a();\n"
  5899. " }\n"
  5900. " while (false)\n"
  5901. " {\n"
  5902. " b();\n"
  5903. " }\n"
  5904. " do\n"
  5905. " {\n"
  5906. " c();\n"
  5907. " } while (false)\n"
  5908. "}\n",
  5909. BreakBeforeBrace);
  5910. verifyFormat("void f(int a)\n"
  5911. "{\n"
  5912. " switch (a)\n"
  5913. " {\n"
  5914. " case 0:\n"
  5915. " break;\n"
  5916. " case 1:\n"
  5917. " {\n"
  5918. " break;\n"
  5919. " }\n"
  5920. " case 2:\n"
  5921. " {\n"
  5922. " }\n"
  5923. " break;\n"
  5924. " default:\n"
  5925. " break;\n"
  5926. " }\n"
  5927. "}\n",
  5928. BreakBeforeBrace);
  5929. verifyFormat("enum X\n"
  5930. "{\n"
  5931. " Y = 0,\n"
  5932. "}\n",
  5933. BreakBeforeBrace);
  5934. FormatStyle BreakBeforeBraceShortIfs = BreakBeforeBrace;
  5935. BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
  5936. BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
  5937. verifyFormat("void f(bool b)\n"
  5938. "{\n"
  5939. " if (b)\n"
  5940. " {\n"
  5941. " return;\n"
  5942. " }\n"
  5943. "}\n",
  5944. BreakBeforeBraceShortIfs);
  5945. verifyFormat("void f(bool b)\n"
  5946. "{\n"
  5947. " if (b) return;\n"
  5948. "}\n",
  5949. BreakBeforeBraceShortIfs);
  5950. verifyFormat("void f(bool b)\n"
  5951. "{\n"
  5952. " while (b)\n"
  5953. " {\n"
  5954. " return;\n"
  5955. " }\n"
  5956. "}\n",
  5957. BreakBeforeBraceShortIfs);
  5958. }
  5959. TEST_F(FormatTest, CatchExceptionReferenceBinding) {
  5960. verifyFormat("void f() {\n"
  5961. " try {\n"
  5962. " }\n"
  5963. " catch (const Exception &e) {\n"
  5964. " }\n"
  5965. "}\n",
  5966. getLLVMStyle());
  5967. }
  5968. TEST_F(FormatTest, UnderstandsPragmas) {
  5969. verifyFormat("#pragma omp reduction(| : var)");
  5970. verifyFormat("#pragma omp reduction(+ : var)");
  5971. }
  5972. bool allStylesEqual(ArrayRef<FormatStyle> Styles) {
  5973. for (size_t i = 1; i < Styles.size(); ++i)
  5974. if (!(Styles[0] == Styles[i]))
  5975. return false;
  5976. return true;
  5977. }
  5978. TEST_F(FormatTest, GetsPredefinedStyleByName) {
  5979. FormatStyle Styles[3];
  5980. Styles[0] = getLLVMStyle();
  5981. EXPECT_TRUE(getPredefinedStyle("LLVM", &Styles[1]));
  5982. EXPECT_TRUE(getPredefinedStyle("lLvM", &Styles[2]));
  5983. EXPECT_TRUE(allStylesEqual(Styles));
  5984. Styles[0] = getGoogleStyle();
  5985. EXPECT_TRUE(getPredefinedStyle("Google", &Styles[1]));
  5986. EXPECT_TRUE(getPredefinedStyle("gOOgle", &Styles[2]));
  5987. EXPECT_TRUE(allStylesEqual(Styles));
  5988. Styles[0] = getChromiumStyle();
  5989. EXPECT_TRUE(getPredefinedStyle("Chromium", &Styles[1]));
  5990. EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", &Styles[2]));
  5991. EXPECT_TRUE(allStylesEqual(Styles));
  5992. Styles[0] = getMozillaStyle();
  5993. EXPECT_TRUE(getPredefinedStyle("Mozilla", &Styles[1]));
  5994. EXPECT_TRUE(getPredefinedStyle("moZILla", &Styles[2]));
  5995. EXPECT_TRUE(allStylesEqual(Styles));
  5996. Styles[0] = getWebKitStyle();
  5997. EXPECT_TRUE(getPredefinedStyle("WebKit", &Styles[1]));
  5998. EXPECT_TRUE(getPredefinedStyle("wEbKit", &Styles[2]));
  5999. EXPECT_TRUE(allStylesEqual(Styles));
  6000. EXPECT_FALSE(getPredefinedStyle("qwerty", &Styles[0]));
  6001. }
  6002. TEST_F(FormatTest, ParsesConfiguration) {
  6003. FormatStyle Style = {};
  6004. #define CHECK_PARSE(TEXT, FIELD, VALUE) \
  6005. EXPECT_NE(VALUE, Style.FIELD); \
  6006. EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \
  6007. EXPECT_EQ(VALUE, Style.FIELD)
  6008. #define CHECK_PARSE_BOOL(FIELD) \
  6009. Style.FIELD = false; \
  6010. EXPECT_EQ(0, parseConfiguration(#FIELD ": true", &Style).value()); \
  6011. EXPECT_TRUE(Style.FIELD); \
  6012. EXPECT_EQ(0, parseConfiguration(#FIELD ": false", &Style).value()); \
  6013. EXPECT_FALSE(Style.FIELD);
  6014. CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
  6015. CHECK_PARSE_BOOL(AlignTrailingComments);
  6016. CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
  6017. CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
  6018. CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
  6019. CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
  6020. CHECK_PARSE_BOOL(BinPackParameters);
  6021. CHECK_PARSE_BOOL(BreakBeforeBinaryOperators);
  6022. CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
  6023. CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
  6024. CHECK_PARSE_BOOL(DerivePointerBinding);
  6025. CHECK_PARSE_BOOL(IndentCaseLabels);
  6026. CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
  6027. CHECK_PARSE_BOOL(PointerBindsToType);
  6028. CHECK_PARSE_BOOL(Cpp11BracedListStyle);
  6029. CHECK_PARSE_BOOL(IndentFunctionDeclarationAfterType);
  6030. CHECK_PARSE_BOOL(SpacesInParentheses);
  6031. CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
  6032. CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
  6033. CHECK_PARSE_BOOL(SpaceAfterControlStatementKeyword);
  6034. CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
  6035. CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
  6036. CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
  6037. ConstructorInitializerIndentWidth, 1234u);
  6038. CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
  6039. CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
  6040. CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
  6041. CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
  6042. PenaltyReturnTypeOnItsOwnLine, 1234u);
  6043. CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
  6044. SpacesBeforeTrailingComments, 1234u);
  6045. CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
  6046. CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
  6047. Style.Standard = FormatStyle::LS_Auto;
  6048. CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
  6049. CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
  6050. CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
  6051. CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
  6052. CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
  6053. Style.UseTab = FormatStyle::UT_ForIndentation;
  6054. CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
  6055. CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
  6056. CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
  6057. CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
  6058. CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
  6059. Style.ColumnLimit = 123;
  6060. FormatStyle BaseStyle = getLLVMStyle();
  6061. CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
  6062. CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
  6063. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  6064. CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
  6065. FormatStyle::BS_Attach);
  6066. CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
  6067. FormatStyle::BS_Linux);
  6068. CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
  6069. FormatStyle::BS_Stroustrup);
  6070. Style.NamespaceIndentation = FormatStyle::NI_All;
  6071. CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
  6072. FormatStyle::NI_None);
  6073. CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
  6074. FormatStyle::NI_Inner);
  6075. CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
  6076. FormatStyle::NI_All);
  6077. #undef CHECK_PARSE
  6078. #undef CHECK_PARSE_BOOL
  6079. }
  6080. TEST_F(FormatTest, ConfigurationRoundTripTest) {
  6081. FormatStyle Style = getLLVMStyle();
  6082. std::string YAML = configurationAsText(Style);
  6083. FormatStyle ParsedStyle = {};
  6084. EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
  6085. EXPECT_EQ(Style, ParsedStyle);
  6086. }
  6087. TEST_F(FormatTest, WorksFor8bitEncodings) {
  6088. EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
  6089. "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
  6090. "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
  6091. "\"\xef\xee\xf0\xf3...\"",
  6092. format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
  6093. "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
  6094. "\xef\xee\xf0\xf3...\"",
  6095. getLLVMStyleWithColumns(12)));
  6096. }
  6097. // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
  6098. #if !defined(_MSC_VER)
  6099. TEST_F(FormatTest, CountsUTF8CharactersProperly) {
  6100. verifyFormat("\"Однажды в студёную зимнюю пору...\"",
  6101. getLLVMStyleWithColumns(35));
  6102. verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
  6103. getLLVMStyleWithColumns(31));
  6104. verifyFormat("// Однажды в студёную зимнюю пору...",
  6105. getLLVMStyleWithColumns(36));
  6106. verifyFormat("// 一 二 三 四 五 六 七 八 九 十",
  6107. getLLVMStyleWithColumns(32));
  6108. verifyFormat("/* Однажды в студёную зимнюю пору... */",
  6109. getLLVMStyleWithColumns(39));
  6110. verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
  6111. getLLVMStyleWithColumns(35));
  6112. }
  6113. TEST_F(FormatTest, SplitsUTF8Strings) {
  6114. EXPECT_EQ(
  6115. "\"Однажды, в \"\n"
  6116. "\"студёную \"\n"
  6117. "\"зимнюю \"\n"
  6118. "\"пору,\"",
  6119. format("\"Однажды, в студёную зимнюю пору,\"",
  6120. getLLVMStyleWithColumns(13)));
  6121. EXPECT_EQ("\"一 二 三 \"\n"
  6122. "\"四 五六 \"\n"
  6123. "\"七 八 九 \"\n"
  6124. "\"十\"",
  6125. format("\"一 二 三 四 五六 七 八 九 十\"",
  6126. getLLVMStyleWithColumns(11)));
  6127. EXPECT_EQ("\"一\t二 \"\n"
  6128. "\"\t三 \"\n"
  6129. "\"四 五\t六 \"\n"
  6130. "\"\t七 \"\n"
  6131. "\"八九十\tqq\"",
  6132. format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
  6133. getLLVMStyleWithColumns(11)));
  6134. }
  6135. TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
  6136. EXPECT_EQ("const char *sssss =\n"
  6137. " \"一二三四五六七八\\\n"
  6138. " 九 十\";",
  6139. format("const char *sssss = \"一二三四五六七八\\\n"
  6140. " 九 十\";",
  6141. getLLVMStyleWithColumns(30)));
  6142. }
  6143. TEST_F(FormatTest, SplitsUTF8LineComments) {
  6144. EXPECT_EQ("// Я из лесу\n"
  6145. "// вышел; был\n"
  6146. "// сильный\n"
  6147. "// мороз.",
  6148. format("// Я из лесу вышел; был сильный мороз.",
  6149. getLLVMStyleWithColumns(13)));
  6150. EXPECT_EQ("// 一二三\n"
  6151. "// 四五六七\n"
  6152. "// 八 九\n"
  6153. "// 十",
  6154. format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9)));
  6155. }
  6156. TEST_F(FormatTest, SplitsUTF8BlockComments) {
  6157. EXPECT_EQ("/* Гляжу,\n"
  6158. " * поднимается\n"
  6159. " * медленно в\n"
  6160. " * гору\n"
  6161. " * Лошадка,\n"
  6162. " * везущая\n"
  6163. " * хворосту\n"
  6164. " * воз. */",
  6165. format("/* Гляжу, поднимается медленно в гору\n"
  6166. " * Лошадка, везущая хворосту воз. */",
  6167. getLLVMStyleWithColumns(13)));
  6168. EXPECT_EQ(
  6169. "/* 一二三\n"
  6170. " * 四五六七\n"
  6171. " * 八 九\n"
  6172. " * 十 */",
  6173. format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9)));
  6174. EXPECT_EQ("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
  6175. " * 𝕓𝕪𝕥𝕖\n"
  6176. " * 𝖀𝕿𝕱-𝟠 */",
  6177. format("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12)));
  6178. }
  6179. #endif // _MSC_VER
  6180. TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
  6181. FormatStyle Style = getLLVMStyle();
  6182. Style.ConstructorInitializerIndentWidth = 4;
  6183. verifyFormat(
  6184. "SomeClass::Constructor()\n"
  6185. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  6186. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  6187. Style);
  6188. Style.ConstructorInitializerIndentWidth = 2;
  6189. verifyFormat(
  6190. "SomeClass::Constructor()\n"
  6191. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  6192. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  6193. Style);
  6194. Style.ConstructorInitializerIndentWidth = 0;
  6195. verifyFormat(
  6196. "SomeClass::Constructor()\n"
  6197. ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  6198. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  6199. Style);
  6200. Style.BreakConstructorInitializersBeforeComma = true;
  6201. Style.ConstructorInitializerIndentWidth = 4;
  6202. verifyFormat("SomeClass::Constructor()\n"
  6203. " : a(a)\n"
  6204. " , b(b)\n"
  6205. " , c(c) {}",
  6206. Style);
  6207. Style.ConstructorInitializerIndentWidth = 2;
  6208. verifyFormat("SomeClass::Constructor()\n"
  6209. " : a(a)\n"
  6210. " , b(b)\n"
  6211. " , c(c) {}",
  6212. Style);
  6213. Style.ConstructorInitializerIndentWidth = 0;
  6214. verifyFormat("SomeClass::Constructor()\n"
  6215. ": a(a)\n"
  6216. ", b(b)\n"
  6217. ", c(c) {}",
  6218. Style);
  6219. Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  6220. Style.ConstructorInitializerIndentWidth = 4;
  6221. verifyFormat(
  6222. "SomeClass::Constructor()\n"
  6223. " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
  6224. Style);
  6225. Style.ConstructorInitializerIndentWidth = 4;
  6226. Style.ColumnLimit = 60;
  6227. verifyFormat("SomeClass::Constructor()\n"
  6228. " : aaaaaaaa(aaaaaaaa)\n"
  6229. " , aaaaaaaa(aaaaaaaa)\n"
  6230. " , aaaaaaaa(aaaaaaaa) {}",
  6231. Style);
  6232. }
  6233. TEST_F(FormatTest, FormatsWithWebKitStyle) {
  6234. FormatStyle Style = getWebKitStyle();
  6235. // Don't indent in outer namespaces.
  6236. verifyFormat("namespace outer {\n"
  6237. "int i;\n"
  6238. "namespace inner {\n"
  6239. " int i;\n"
  6240. "} // namespace inner\n"
  6241. "} // namespace outer\n"
  6242. "namespace other_outer {\n"
  6243. "int i;\n"
  6244. "}",
  6245. Style);
  6246. // Don't indent case labels.
  6247. verifyFormat("switch (variable) {\n"
  6248. "case 1:\n"
  6249. "case 2:\n"
  6250. " doSomething();\n"
  6251. " break;\n"
  6252. "default:\n"
  6253. " ++variable;\n"
  6254. "}",
  6255. Style);
  6256. // Wrap before binary operators.
  6257. EXPECT_EQ(
  6258. "void f()\n"
  6259. "{\n"
  6260. " if (aaaaaaaaaaaaaaaa\n"
  6261. " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
  6262. " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  6263. " return;\n"
  6264. "}",
  6265. format(
  6266. "void f() {\n"
  6267. "if (aaaaaaaaaaaaaaaa\n"
  6268. "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
  6269. "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  6270. "return;\n"
  6271. "}",
  6272. Style));
  6273. // Constructor initializers are formatted one per line with the "," on the
  6274. // new line.
  6275. verifyFormat("Constructor()\n"
  6276. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  6277. " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
  6278. " aaaaaaaaaaaaaa)\n"
  6279. " , aaaaaaaaaaaaaaaaaaaaaaa()\n{\n}",
  6280. Style);
  6281. // Access specifiers should be aligned left.
  6282. verifyFormat("class C {\n"
  6283. "public:\n"
  6284. " int i;\n"
  6285. "};",
  6286. Style);
  6287. // Do not align comments.
  6288. verifyFormat("int a; // Do not\n"
  6289. "double b; // align comments.",
  6290. Style);
  6291. // Accept input's line breaks.
  6292. EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
  6293. " || bbbbbbbbbbbbbbb) {\n"
  6294. " i++;\n"
  6295. "}",
  6296. format("if (aaaaaaaaaaaaaaa\n"
  6297. "|| bbbbbbbbbbbbbbb) { i++; }",
  6298. Style));
  6299. EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
  6300. " i++;\n"
  6301. "}",
  6302. format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
  6303. }
  6304. TEST_F(FormatTest, FormatsProtocolBufferDefinitions) {
  6305. // It seems that clang-format can format protocol buffer definitions
  6306. // (see https://code.google.com/p/protobuf/).
  6307. verifyFormat("message SomeMessage {\n"
  6308. " required int32 field1 = 1;\n"
  6309. " optional string field2 = 2 [default = \"2\"]\n"
  6310. "}");
  6311. }
  6312. TEST_F(FormatTest, FormatsLambdas) {
  6313. verifyFormat("int c = [b]() mutable {\n"
  6314. " return [&b] { return b++; }();\n"
  6315. "}();\n");
  6316. verifyFormat("int c = [&] {\n"
  6317. " [=] { return b++; }();\n"
  6318. "}();\n");
  6319. verifyFormat("int c = [&, &a, a] {\n"
  6320. " [=, c, &d] { return b++; }();\n"
  6321. "}();\n");
  6322. verifyFormat("int c = [&a, &a, a] {\n"
  6323. " [=, a, b, &c] { return b++; }();\n"
  6324. "}();\n");
  6325. verifyFormat("auto c = { [&a, &a, a] {\n"
  6326. " [=, a, b, &c] { return b++; }();\n"
  6327. "} }\n");
  6328. verifyFormat("auto c = { [&a, &a, a] { [=, a, b, &c] {}(); } }\n");
  6329. verifyFormat("void f() {\n"
  6330. " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
  6331. "}\n");
  6332. verifyFormat("void f() {\n"
  6333. " other(x.begin(), //\n"
  6334. " x.end(), //\n"
  6335. " [&](int, int) { return 1; });\n"
  6336. "}\n");
  6337. // Not lambdas.
  6338. verifyFormat("constexpr char hello[]{ \"hello\" };");
  6339. verifyFormat("double &operator[](int i) { return 0; }\n"
  6340. "int i;");
  6341. }
  6342. TEST_F(FormatTest, FormatsBlocks) {
  6343. // FIXME: Make whitespace formatting consistent. Ask a ObjC dev how
  6344. // it would ideally look.
  6345. verifyFormat("[operation setCompletionBlock:^{ [self onOperationDone]; }];");
  6346. verifyFormat("int i = {[operation setCompletionBlock : ^{ [self "
  6347. "onOperationDone]; }] };");
  6348. }
  6349. TEST_F(FormatTest, SupportsCRLF) {
  6350. EXPECT_EQ("int a;\r\n"
  6351. "int b;\r\n"
  6352. "int c;\r\n",
  6353. format("int a;\r\n"
  6354. " int b;\r\n"
  6355. " int c;\r\n",
  6356. getLLVMStyle()));
  6357. EXPECT_EQ("int a;\r\n"
  6358. "int b;\r\n"
  6359. "int c;\r\n",
  6360. format("int a;\r\n"
  6361. " int b;\n"
  6362. " int c;\r\n",
  6363. getLLVMStyle()));
  6364. EXPECT_EQ("int a;\n"
  6365. "int b;\n"
  6366. "int c;\n",
  6367. format("int a;\r\n"
  6368. " int b;\n"
  6369. " int c;\n",
  6370. getLLVMStyle()));
  6371. EXPECT_EQ("\"aaaaaaa \"\r\n"
  6372. "\"bbbbbbb\";\r\n",
  6373. format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
  6374. EXPECT_EQ("#define A \\\r\n"
  6375. " b; \\\r\n"
  6376. " c; \\\r\n"
  6377. " d;\r\n",
  6378. format("#define A \\\r\n"
  6379. " b; \\\r\n"
  6380. " c; d; \r\n",
  6381. getGoogleStyle()));
  6382. }
  6383. TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
  6384. verifyFormat("MY_CLASS(C) {\n"
  6385. " int i;\n"
  6386. " int j;\n"
  6387. "};");
  6388. }
  6389. TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
  6390. FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
  6391. TwoIndent.ContinuationIndentWidth = 2;
  6392. EXPECT_EQ("int i =\n"
  6393. " longFunction(\n"
  6394. " arg);",
  6395. format("int i = longFunction(arg);", TwoIndent));
  6396. FormatStyle SixIndent = getLLVMStyleWithColumns(20);
  6397. SixIndent.ContinuationIndentWidth = 6;
  6398. EXPECT_EQ("int i =\n"
  6399. " longFunction(\n"
  6400. " arg);",
  6401. format("int i = longFunction(arg);", SixIndent));
  6402. }
  6403. } // end namespace tooling
  6404. } // end namespace clang