FormatTest.cpp 227 KB

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