FormatTest.cpp 218 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853
  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, FormatsBitfields) {
  1421. verifyFormat("struct Bitfields {\n"
  1422. " unsigned sClass : 8;\n"
  1423. " unsigned ValueKind : 2;\n"
  1424. "};");
  1425. }
  1426. TEST_F(FormatTest, FormatsNamespaces) {
  1427. verifyFormat("namespace some_namespace {\n"
  1428. "class A {};\n"
  1429. "void f() { f(); }\n"
  1430. "}");
  1431. verifyFormat("namespace {\n"
  1432. "class A {};\n"
  1433. "void f() { f(); }\n"
  1434. "}");
  1435. verifyFormat("inline namespace X {\n"
  1436. "class A {};\n"
  1437. "void f() { f(); }\n"
  1438. "}");
  1439. verifyFormat("using namespace some_namespace;\n"
  1440. "class A {};\n"
  1441. "void f() { f(); }");
  1442. // This code is more common than we thought; if we
  1443. // layout this correctly the semicolon will go into
  1444. // its own line, which is undesireable.
  1445. verifyFormat("namespace {};");
  1446. verifyFormat("namespace {\n"
  1447. "class A {};\n"
  1448. "};");
  1449. verifyFormat("namespace {\n"
  1450. "int SomeVariable = 0; // comment\n"
  1451. "} // namespace");
  1452. EXPECT_EQ("#ifndef HEADER_GUARD\n"
  1453. "#define HEADER_GUARD\n"
  1454. "namespace my_namespace {\n"
  1455. "int i;\n"
  1456. "} // my_namespace\n"
  1457. "#endif // HEADER_GUARD",
  1458. format("#ifndef HEADER_GUARD\n"
  1459. " #define HEADER_GUARD\n"
  1460. " namespace my_namespace {\n"
  1461. "int i;\n"
  1462. "} // my_namespace\n"
  1463. "#endif // HEADER_GUARD"));
  1464. FormatStyle Style = getLLVMStyle();
  1465. Style.NamespaceIndentation = FormatStyle::NI_All;
  1466. EXPECT_EQ("namespace out {\n"
  1467. " int i;\n"
  1468. " namespace in {\n"
  1469. " int i;\n"
  1470. " } // namespace\n"
  1471. "} // namespace",
  1472. format("namespace out {\n"
  1473. "int i;\n"
  1474. "namespace in {\n"
  1475. "int i;\n"
  1476. "} // namespace\n"
  1477. "} // namespace",
  1478. Style));
  1479. Style.NamespaceIndentation = FormatStyle::NI_Inner;
  1480. EXPECT_EQ("namespace out {\n"
  1481. "int i;\n"
  1482. "namespace in {\n"
  1483. " int i;\n"
  1484. "} // namespace\n"
  1485. "} // namespace",
  1486. format("namespace out {\n"
  1487. "int i;\n"
  1488. "namespace in {\n"
  1489. "int i;\n"
  1490. "} // namespace\n"
  1491. "} // namespace",
  1492. Style));
  1493. }
  1494. TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
  1495. TEST_F(FormatTest, FormatsInlineASM) {
  1496. verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
  1497. verifyFormat(
  1498. "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
  1499. " \"cpuid\\n\\t\"\n"
  1500. " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
  1501. " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
  1502. " : \"a\"(value));");
  1503. }
  1504. TEST_F(FormatTest, FormatTryCatch) {
  1505. // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll
  1506. // also not create single-line-blocks.
  1507. verifyFormat("try {\n"
  1508. " throw a * b;\n"
  1509. "}\n"
  1510. "catch (int a) {\n"
  1511. " // Do nothing.\n"
  1512. "}\n"
  1513. "catch (...) {\n"
  1514. " exit(42);\n"
  1515. "}");
  1516. // Function-level try statements.
  1517. verifyFormat("int f() try { return 4; }\n"
  1518. "catch (...) {\n"
  1519. " return 5;\n"
  1520. "}");
  1521. verifyFormat("class A {\n"
  1522. " int a;\n"
  1523. " A() try : a(0) {}\n"
  1524. " catch (...) {\n"
  1525. " throw;\n"
  1526. " }\n"
  1527. "};\n");
  1528. }
  1529. TEST_F(FormatTest, FormatObjCTryCatch) {
  1530. verifyFormat("@try {\n"
  1531. " f();\n"
  1532. "}\n"
  1533. "@catch (NSException e) {\n"
  1534. " @throw;\n"
  1535. "}\n"
  1536. "@finally {\n"
  1537. " exit(42);\n"
  1538. "}");
  1539. }
  1540. TEST_F(FormatTest, StaticInitializers) {
  1541. verifyFormat("static SomeClass SC = { 1, 'a' };");
  1542. verifyFormat(
  1543. "static SomeClass WithALoooooooooooooooooooongName = {\n"
  1544. " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
  1545. "};");
  1546. verifyFormat(
  1547. "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n"
  1548. " looooooooooooooooooooooooooooooooooongname,\n"
  1549. " looooooooooooooooooooooooooooooong };");
  1550. // Allow bin-packing in static initializers as this would often lead to
  1551. // terrible results, e.g.:
  1552. verifyGoogleFormat(
  1553. "static SomeClass = {a, b, c, d, e, f, g, h, i, j,\n"
  1554. " looooooooooooooooooooooooooooooooooongname,\n"
  1555. " looooooooooooooooooooooooooooooong};");
  1556. // Here, everything other than the "}" would fit on a line.
  1557. verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
  1558. " 100000000000000000000000\n"
  1559. "};");
  1560. EXPECT_EQ("S s = { a, b };", format("S s = {\n"
  1561. " a,\n"
  1562. "\n"
  1563. " b\n"
  1564. "};"));
  1565. // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
  1566. // line. However, the formatting looks a bit off and this probably doesn't
  1567. // happen often in practice.
  1568. verifyFormat("static int Variable[1] = {\n"
  1569. " { 1000000000000000000000000000000000000 }\n"
  1570. "};",
  1571. getLLVMStyleWithColumns(40));
  1572. }
  1573. TEST_F(FormatTest, DesignatedInitializers) {
  1574. verifyFormat("const struct A a = { .a = 1, .b = 2 };");
  1575. verifyFormat("const struct A a = { .aaaaaaaaaa = 1,\n"
  1576. " .bbbbbbbbbb = 2,\n"
  1577. " .cccccccccc = 3,\n"
  1578. " .dddddddddd = 4,\n"
  1579. " .eeeeeeeeee = 5 };");
  1580. verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
  1581. " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
  1582. " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
  1583. " .ccccccccccccccccccccccccccc = 3,\n"
  1584. " .ddddddddddddddddddddddddddd = 4,\n"
  1585. " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5\n"
  1586. "};");
  1587. verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
  1588. }
  1589. TEST_F(FormatTest, NestedStaticInitializers) {
  1590. verifyFormat("static A x = { { {} } };\n");
  1591. verifyFormat("static A x = { { { init1, init2, init3, init4 },\n"
  1592. " { init1, init2, init3, init4 } } };");
  1593. verifyFormat("somes Status::global_reps[3] = {\n"
  1594. " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n"
  1595. " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n"
  1596. " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n"
  1597. "};");
  1598. verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
  1599. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  1600. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  1601. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
  1602. verifyFormat(
  1603. "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n"
  1604. " { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop"
  1605. " } };");
  1606. verifyFormat(
  1607. "SomeArrayOfSomeType a = { { { 1, 2, 3 }, { 1, 2, 3 },\n"
  1608. " { 111111111111111111111111111111,\n"
  1609. " 222222222222222222222222222222,\n"
  1610. " 333333333333333333333333333333 },\n"
  1611. " { 1, 2, 3 }, { 1, 2, 3 } } };");
  1612. verifyFormat(
  1613. "SomeArrayOfSomeType a = { { { 1, 2, 3 } }, { { 1, 2, 3 } },\n"
  1614. " { { 111111111111111111111111111111,\n"
  1615. " 222222222222222222222222222222,\n"
  1616. " 333333333333333333333333333333 } },\n"
  1617. " { { 1, 2, 3 } }, { { 1, 2, 3 } } };");
  1618. verifyGoogleFormat(
  1619. "SomeArrayOfSomeType a = {\n"
  1620. " {{1, 2, 3}}, {{1, 2, 3}},\n"
  1621. " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
  1622. " 333333333333333333333333333333}},\n"
  1623. " {{1, 2, 3}}, {{1, 2, 3}}};");
  1624. // FIXME: We might at some point want to handle this similar to parameter
  1625. // lists, where we have an option to put each on a single line.
  1626. verifyFormat(
  1627. "struct {\n"
  1628. " unsigned bit;\n"
  1629. " const char *const name;\n"
  1630. "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n"
  1631. " { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };");
  1632. }
  1633. TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
  1634. verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  1635. " \\\n"
  1636. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
  1637. }
  1638. TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
  1639. verifyFormat(
  1640. "virtual void write(ELFWriter *writerrr,\n"
  1641. " OwningPtr<FileOutputBuffer> &buffer) = 0;");
  1642. }
  1643. TEST_F(FormatTest, LayoutUnknownPPDirective) {
  1644. EXPECT_EQ("#123 \"A string literal\"",
  1645. format(" # 123 \"A string literal\""));
  1646. EXPECT_EQ("#;", format("#;"));
  1647. verifyFormat("#\n;\n;\n;");
  1648. }
  1649. TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
  1650. EXPECT_EQ("#line 42 \"test\"\n",
  1651. format("# \\\n line \\\n 42 \\\n \"test\"\n"));
  1652. EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n",
  1653. getLLVMStyleWithColumns(12)));
  1654. }
  1655. TEST_F(FormatTest, EndOfFileEndsPPDirective) {
  1656. EXPECT_EQ("#line 42 \"test\"",
  1657. format("# \\\n line \\\n 42 \\\n \"test\""));
  1658. EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B"));
  1659. }
  1660. TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
  1661. verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
  1662. verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
  1663. verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
  1664. // FIXME: We never break before the macro name.
  1665. verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12));
  1666. verifyFormat("#define A A\n#define A A");
  1667. verifyFormat("#define A(X) A\n#define A A");
  1668. verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
  1669. verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
  1670. }
  1671. TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
  1672. EXPECT_EQ("// somecomment\n"
  1673. "#include \"a.h\"\n"
  1674. "#define A( \\\n"
  1675. " A, B)\n"
  1676. "#include \"b.h\"\n"
  1677. "// somecomment\n",
  1678. format(" // somecomment\n"
  1679. " #include \"a.h\"\n"
  1680. "#define A(A,\\\n"
  1681. " B)\n"
  1682. " #include \"b.h\"\n"
  1683. " // somecomment\n",
  1684. getLLVMStyleWithColumns(13)));
  1685. }
  1686. TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
  1687. TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
  1688. EXPECT_EQ("#define A \\\n"
  1689. " c; \\\n"
  1690. " e;\n"
  1691. "f;",
  1692. format("#define A c; e;\n"
  1693. "f;",
  1694. getLLVMStyleWithColumns(14)));
  1695. }
  1696. TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
  1697. TEST_F(FormatTest, AlwaysFormatsEntireMacroDefinitions) {
  1698. EXPECT_EQ("int i;\n"
  1699. "#define A \\\n"
  1700. " int i; \\\n"
  1701. " int j\n"
  1702. "int k;",
  1703. format("int i;\n"
  1704. "#define A \\\n"
  1705. " int i ; \\\n"
  1706. " int j\n"
  1707. "int k;",
  1708. 8, 0, getGoogleStyle())); // 8: position of "#define".
  1709. EXPECT_EQ("int i;\n"
  1710. "#define A \\\n"
  1711. " int i; \\\n"
  1712. " int j\n"
  1713. "int k;",
  1714. format("int i;\n"
  1715. "#define A \\\n"
  1716. " int i ; \\\n"
  1717. " int j\n"
  1718. "int k;",
  1719. 45, 0, getGoogleStyle())); // 45: position of "j".
  1720. }
  1721. TEST_F(FormatTest, MacroDefinitionInsideStatement) {
  1722. EXPECT_EQ("int x,\n"
  1723. "#define A\n"
  1724. " y;",
  1725. format("int x,\n#define A\ny;"));
  1726. }
  1727. TEST_F(FormatTest, HashInMacroDefinition) {
  1728. verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
  1729. verifyFormat("#define A \\\n"
  1730. " { \\\n"
  1731. " f(#c);\\\n"
  1732. " }",
  1733. getLLVMStyleWithColumns(11));
  1734. verifyFormat("#define A(X) \\\n"
  1735. " void function##X()",
  1736. getLLVMStyleWithColumns(22));
  1737. verifyFormat("#define A(a, b, c) \\\n"
  1738. " void a##b##c()",
  1739. getLLVMStyleWithColumns(22));
  1740. verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
  1741. }
  1742. TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
  1743. verifyFormat("#define A (1)");
  1744. }
  1745. TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
  1746. EXPECT_EQ("#define A b;", format("#define A \\\n"
  1747. " \\\n"
  1748. " b;",
  1749. getLLVMStyleWithColumns(25)));
  1750. EXPECT_EQ("#define A \\\n"
  1751. " \\\n"
  1752. " a; \\\n"
  1753. " b;",
  1754. format("#define A \\\n"
  1755. " \\\n"
  1756. " a; \\\n"
  1757. " b;",
  1758. getLLVMStyleWithColumns(11)));
  1759. EXPECT_EQ("#define A \\\n"
  1760. " a; \\\n"
  1761. " \\\n"
  1762. " b;",
  1763. format("#define A \\\n"
  1764. " a; \\\n"
  1765. " \\\n"
  1766. " b;",
  1767. getLLVMStyleWithColumns(11)));
  1768. }
  1769. TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
  1770. verifyFormat("#define A :");
  1771. verifyFormat("#define SOMECASES \\\n"
  1772. " case 1: \\\n"
  1773. " case 2\n",
  1774. getLLVMStyleWithColumns(20));
  1775. verifyFormat("#define A template <typename T>");
  1776. verifyFormat("#define STR(x) #x\n"
  1777. "f(STR(this_is_a_string_literal{));");
  1778. }
  1779. TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
  1780. verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
  1781. EXPECT_EQ("class A : public QObject {\n"
  1782. " Q_OBJECT\n"
  1783. "\n"
  1784. " A() {}\n"
  1785. "};",
  1786. format("class A : public QObject {\n"
  1787. " Q_OBJECT\n"
  1788. "\n"
  1789. " A() {\n}\n"
  1790. "} ;"));
  1791. EXPECT_EQ("SOME_MACRO\n"
  1792. "namespace {\n"
  1793. "void f();\n"
  1794. "}",
  1795. format("SOME_MACRO\n"
  1796. " namespace {\n"
  1797. "void f( );\n"
  1798. "}"));
  1799. // Only if the identifier contains at least 5 characters.
  1800. EXPECT_EQ("HTTP f();",
  1801. format("HTTP\nf();"));
  1802. EXPECT_EQ("MACRO\nf();",
  1803. format("MACRO\nf();"));
  1804. // Only if everything is upper case.
  1805. EXPECT_EQ("class A : public QObject {\n"
  1806. " Q_Object A() {}\n"
  1807. "};",
  1808. format("class A : public QObject {\n"
  1809. " Q_Object\n"
  1810. "\n"
  1811. " A() {\n}\n"
  1812. "} ;"));
  1813. }
  1814. TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
  1815. EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  1816. "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  1817. "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  1818. "class X {};\n"
  1819. "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  1820. "int *createScopDetectionPass() { return 0; }",
  1821. format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  1822. " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  1823. " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  1824. " class X {};\n"
  1825. " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  1826. " int *createScopDetectionPass() { return 0; }"));
  1827. // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
  1828. // braces, so that inner block is indented one level more.
  1829. EXPECT_EQ("int q() {\n"
  1830. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  1831. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  1832. " IPC_END_MESSAGE_MAP()\n"
  1833. "}",
  1834. format("int q() {\n"
  1835. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  1836. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  1837. " IPC_END_MESSAGE_MAP()\n"
  1838. "}"));
  1839. EXPECT_EQ("int q() {\n"
  1840. " f(x);\n"
  1841. " f(x) {}\n"
  1842. " f(x)->g();\n"
  1843. " f(x)->*g();\n"
  1844. " f(x).g();\n"
  1845. " f(x) = x;\n"
  1846. " f(x) += x;\n"
  1847. " f(x) -= x;\n"
  1848. " f(x) *= x;\n"
  1849. " f(x) /= x;\n"
  1850. " f(x) %= x;\n"
  1851. " f(x) &= x;\n"
  1852. " f(x) |= x;\n"
  1853. " f(x) ^= x;\n"
  1854. " f(x) >>= x;\n"
  1855. " f(x) <<= x;\n"
  1856. " f(x)[y].z();\n"
  1857. " LOG(INFO) << x;\n"
  1858. " ifstream(x) >> x;\n"
  1859. "}\n",
  1860. format("int q() {\n"
  1861. " f(x)\n;\n"
  1862. " f(x)\n {}\n"
  1863. " f(x)\n->g();\n"
  1864. " f(x)\n->*g();\n"
  1865. " f(x)\n.g();\n"
  1866. " f(x)\n = x;\n"
  1867. " f(x)\n += x;\n"
  1868. " f(x)\n -= x;\n"
  1869. " f(x)\n *= x;\n"
  1870. " f(x)\n /= x;\n"
  1871. " f(x)\n %= x;\n"
  1872. " f(x)\n &= x;\n"
  1873. " f(x)\n |= x;\n"
  1874. " f(x)\n ^= x;\n"
  1875. " f(x)\n >>= x;\n"
  1876. " f(x)\n <<= x;\n"
  1877. " f(x)\n[y].z();\n"
  1878. " LOG(INFO)\n << x;\n"
  1879. " ifstream(x)\n >> x;\n"
  1880. "}\n"));
  1881. EXPECT_EQ("int q() {\n"
  1882. " f(x)\n"
  1883. " if (1) {\n"
  1884. " }\n"
  1885. " f(x)\n"
  1886. " while (1) {\n"
  1887. " }\n"
  1888. " f(x)\n"
  1889. " g(x);\n"
  1890. " f(x)\n"
  1891. " try {\n"
  1892. " q();\n"
  1893. " }\n"
  1894. " catch (...) {\n"
  1895. " }\n"
  1896. "}\n",
  1897. format("int q() {\n"
  1898. "f(x)\n"
  1899. "if (1) {}\n"
  1900. "f(x)\n"
  1901. "while (1) {}\n"
  1902. "f(x)\n"
  1903. "g(x);\n"
  1904. "f(x)\n"
  1905. "try { q(); } catch (...) {}\n"
  1906. "}\n"));
  1907. EXPECT_EQ("class A {\n"
  1908. " A() : t(0) {}\n"
  1909. " A(X x)\n" // FIXME: function-level try blocks are broken.
  1910. " try : t(0) {\n"
  1911. " }\n"
  1912. " catch (...) {\n"
  1913. " }\n"
  1914. "};",
  1915. format("class A {\n"
  1916. " A()\n : t(0) {}\n"
  1917. " A(X x)\n"
  1918. " try : t(0) {} catch (...) {}\n"
  1919. "};"));
  1920. }
  1921. TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
  1922. verifyFormat("#define A \\\n"
  1923. " f({ \\\n"
  1924. " g(); \\\n"
  1925. " });", getLLVMStyleWithColumns(11));
  1926. }
  1927. TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
  1928. EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
  1929. }
  1930. TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
  1931. verifyFormat("{\n { a #c; }\n}");
  1932. }
  1933. TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
  1934. EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
  1935. format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
  1936. EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
  1937. format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
  1938. }
  1939. TEST_F(FormatTest, EscapedNewlineAtStartOfToken) {
  1940. EXPECT_EQ(
  1941. "#define A \\\n int i; \\\n int j;",
  1942. format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
  1943. EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
  1944. }
  1945. TEST_F(FormatTest, NoEscapedNewlineHandlingInBlockComments) {
  1946. EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/"));
  1947. }
  1948. TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
  1949. verifyFormat("#define A \\\n"
  1950. " int v( \\\n"
  1951. " a); \\\n"
  1952. " int i;",
  1953. getLLVMStyleWithColumns(11));
  1954. }
  1955. TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
  1956. EXPECT_EQ(
  1957. "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  1958. " \\\n"
  1959. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  1960. "\n"
  1961. "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  1962. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
  1963. format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
  1964. "\\\n"
  1965. "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  1966. " \n"
  1967. " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  1968. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
  1969. }
  1970. TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
  1971. EXPECT_EQ("int\n"
  1972. "#define A\n"
  1973. " a;",
  1974. format("int\n#define A\na;", getGoogleStyle()));
  1975. verifyFormat("functionCallTo(\n"
  1976. " someOtherFunction(\n"
  1977. " withSomeParameters, whichInSequence,\n"
  1978. " areLongerThanALine(andAnotherCall,\n"
  1979. "#define A B\n"
  1980. " withMoreParamters,\n"
  1981. " whichStronglyInfluenceTheLayout),\n"
  1982. " andMoreParameters),\n"
  1983. " trailing);",
  1984. getLLVMStyleWithColumns(69));
  1985. }
  1986. TEST_F(FormatTest, LayoutBlockInsideParens) {
  1987. EXPECT_EQ("functionCall({\n"
  1988. " int i;\n"
  1989. "});",
  1990. format(" functionCall ( {int i;} );"));
  1991. // FIXME: This is bad, find a better and more generic solution.
  1992. EXPECT_EQ("functionCall({\n"
  1993. " int i;\n"
  1994. "},\n"
  1995. " aaaa, bbbb, cccc);",
  1996. format(" functionCall ( {int i;}, aaaa, bbbb, cccc);"));
  1997. verifyFormat(
  1998. "Aaa({\n"
  1999. " int i;\n"
  2000. "},\n"
  2001. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  2002. " ccccccccccccccccc));");
  2003. }
  2004. TEST_F(FormatTest, LayoutBlockInsideStatement) {
  2005. EXPECT_EQ("SOME_MACRO { int i; }\n"
  2006. "int i;",
  2007. format(" SOME_MACRO {int i;} int i;"));
  2008. }
  2009. TEST_F(FormatTest, LayoutNestedBlocks) {
  2010. verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
  2011. " struct s {\n"
  2012. " int i;\n"
  2013. " };\n"
  2014. " s kBitsToOs[] = { { 10 } };\n"
  2015. " for (int i = 0; i < 10; ++i)\n"
  2016. " return;\n"
  2017. "}");
  2018. }
  2019. TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
  2020. EXPECT_EQ("{}", format("{}"));
  2021. verifyFormat("enum E {};");
  2022. verifyFormat("enum E {}");
  2023. }
  2024. //===----------------------------------------------------------------------===//
  2025. // Line break tests.
  2026. //===----------------------------------------------------------------------===//
  2027. TEST_F(FormatTest, FormatsAwesomeMethodCall) {
  2028. verifyFormat(
  2029. "SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
  2030. " parameter, parameter, parameter)),\n"
  2031. " SecondLongCall(parameter));");
  2032. }
  2033. TEST_F(FormatTest, PreventConfusingIndents) {
  2034. verifyFormat(
  2035. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2036. " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2037. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2038. " aaaaaaaaaaaaaaaaaaaaaaaa);");
  2039. verifyFormat(
  2040. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2041. " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
  2042. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  2043. " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
  2044. verifyFormat(
  2045. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  2046. " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
  2047. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
  2048. " aaaaaaaaaaaaaaaaaaaaaaaa>;");
  2049. verifyFormat("int a = bbbb && ccc && fffff(\n"
  2050. "#define A Just forcing a new line\n"
  2051. " ddd);");
  2052. }
  2053. TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
  2054. verifyFormat(
  2055. "bool aaaaaaa =\n"
  2056. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
  2057. " bbbbbbbb();");
  2058. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  2059. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
  2060. " ccccccccc == ddddddddddd;");
  2061. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  2062. " aaaaaa) &&\n"
  2063. " bbbbbb && cccccc;");
  2064. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  2065. " aaaaaa) >>\n"
  2066. " bbbbbb;");
  2067. verifyFormat("Whitespaces.addUntouchableComment(\n"
  2068. " SourceMgr.getSpellingColumnNumber(\n"
  2069. " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
  2070. " 1);");
  2071. verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2072. " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
  2073. " cccccc) {\n}");
  2074. // If the LHS of a comparison is not a binary expression itself, the
  2075. // additional linebreak confuses many people.
  2076. verifyFormat(
  2077. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2078. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
  2079. "}");
  2080. verifyFormat(
  2081. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2082. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  2083. "}");
  2084. // Even explicit parentheses stress the precedence enough to make the
  2085. // additional break unnecessary.
  2086. verifyFormat(
  2087. "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2088. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  2089. "}");
  2090. // This cases is borderline, but with the indentation it is still readable.
  2091. verifyFormat(
  2092. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2093. " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2094. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  2095. "}",
  2096. getLLVMStyleWithColumns(75));
  2097. // If the LHS is a binary expression, we should still use the additional break
  2098. // as otherwise the formatting hides the operator precedence.
  2099. verifyFormat(
  2100. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2101. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2102. " 5) {\n"
  2103. "}");
  2104. FormatStyle OnePerLine = getLLVMStyle();
  2105. OnePerLine.BinPackParameters = false;
  2106. verifyFormat(
  2107. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2108. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2109. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
  2110. OnePerLine);
  2111. }
  2112. TEST_F(FormatTest, ExpressionIndentation) {
  2113. verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2114. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2115. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2116. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2117. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
  2118. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
  2119. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2120. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
  2121. " ccccccccccccccccccccccccccccccccccccccccc;");
  2122. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2123. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2124. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2125. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  2126. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2127. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2128. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2129. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  2130. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2131. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2132. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2133. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  2134. verifyFormat("if () {\n"
  2135. "} else if (aaaaa && bbbbb > // break\n"
  2136. " ccccc) {\n"
  2137. "}");
  2138. // Presence of a trailing comment used to change indentation of b.
  2139. verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
  2140. " b;\n"
  2141. "return aaaaaaaaaaaaaaaaaaa +\n"
  2142. " b; //",
  2143. getLLVMStyleWithColumns(30));
  2144. }
  2145. TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
  2146. // Not sure what the best system is here. Like this, the LHS can be found
  2147. // immediately above an operator (everything with the same or a higher
  2148. // indent). The RHS is aligned right of the operator and so compasses
  2149. // everything until something with the same indent as the operator is found.
  2150. // FIXME: Is this a good system?
  2151. FormatStyle Style = getLLVMStyle();
  2152. Style.BreakBeforeBinaryOperators = true;
  2153. verifyFormat(
  2154. "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2155. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2156. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2157. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2158. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  2159. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  2160. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2161. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2162. " > ccccccccccccccccccccccccccccccccccccccccc;",
  2163. Style);
  2164. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2165. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2166. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2167. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  2168. Style);
  2169. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2170. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2171. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2172. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  2173. Style);
  2174. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2175. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2176. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2177. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  2178. Style);
  2179. verifyFormat("if () {\n"
  2180. "} else if (aaaaa && bbbbb // break\n"
  2181. " > ccccc) {\n"
  2182. "}",
  2183. Style);
  2184. }
  2185. TEST_F(FormatTest, ConstructorInitializers) {
  2186. verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
  2187. verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
  2188. getLLVMStyleWithColumns(45));
  2189. verifyFormat("Constructor()\n"
  2190. " : Inttializer(FitsOnTheLine) {}",
  2191. getLLVMStyleWithColumns(44));
  2192. verifyFormat("Constructor()\n"
  2193. " : Inttializer(FitsOnTheLine) {}",
  2194. getLLVMStyleWithColumns(43));
  2195. verifyFormat(
  2196. "SomeClass::Constructor()\n"
  2197. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  2198. verifyFormat(
  2199. "SomeClass::Constructor()\n"
  2200. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  2201. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
  2202. verifyFormat(
  2203. "SomeClass::Constructor()\n"
  2204. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2205. " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  2206. verifyFormat("Constructor()\n"
  2207. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2208. " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2209. " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2210. " aaaaaaaaaaaaaaaaaaaaaaa() {}");
  2211. verifyFormat("Constructor()\n"
  2212. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2213. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  2214. verifyFormat("Constructor(int Parameter = 0)\n"
  2215. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
  2216. " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
  2217. // Here a line could be saved by splitting the second initializer onto two
  2218. // lines, but that is not desireable.
  2219. verifyFormat("Constructor()\n"
  2220. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2221. " aaaaaaaaaaa(aaaaaaaaaaa),\n"
  2222. " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  2223. FormatStyle OnePerLine = getLLVMStyle();
  2224. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  2225. verifyFormat("SomeClass::Constructor()\n"
  2226. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  2227. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  2228. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  2229. OnePerLine);
  2230. verifyFormat("SomeClass::Constructor()\n"
  2231. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
  2232. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  2233. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  2234. OnePerLine);
  2235. verifyFormat("MyClass::MyClass(int var)\n"
  2236. " : some_var_(var), // 4 space indent\n"
  2237. " some_other_var_(var + 1) { // lined up\n"
  2238. "}",
  2239. OnePerLine);
  2240. verifyFormat("Constructor()\n"
  2241. " : aaaaa(aaaaaa),\n"
  2242. " aaaaa(aaaaaa),\n"
  2243. " aaaaa(aaaaaa),\n"
  2244. " aaaaa(aaaaaa),\n"
  2245. " aaaaa(aaaaaa) {}",
  2246. OnePerLine);
  2247. verifyFormat("Constructor()\n"
  2248. " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
  2249. " aaaaaaaaaaaaaaaaaaaaaa) {}",
  2250. OnePerLine);
  2251. }
  2252. TEST_F(FormatTest, MemoizationTests) {
  2253. // This breaks if the memoization lookup does not take \c Indent and
  2254. // \c LastSpace into account.
  2255. verifyFormat(
  2256. "extern CFRunLoopTimerRef\n"
  2257. "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
  2258. " CFTimeInterval interval, CFOptionFlags flags,\n"
  2259. " CFIndex order, CFRunLoopTimerCallBack callout,\n"
  2260. " CFRunLoopTimerContext *context) {}");
  2261. // Deep nesting somewhat works around our memoization.
  2262. verifyFormat(
  2263. "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  2264. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  2265. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  2266. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  2267. " aaaaa())))))))))))))))))))))))))))))))))))))));",
  2268. getLLVMStyleWithColumns(65));
  2269. verifyFormat(
  2270. "aaaaa(\n"
  2271. " aaaaa,\n"
  2272. " aaaaa(\n"
  2273. " aaaaa,\n"
  2274. " aaaaa(\n"
  2275. " aaaaa,\n"
  2276. " aaaaa(\n"
  2277. " aaaaa,\n"
  2278. " aaaaa(\n"
  2279. " aaaaa,\n"
  2280. " aaaaa(\n"
  2281. " aaaaa,\n"
  2282. " aaaaa(\n"
  2283. " aaaaa,\n"
  2284. " aaaaa(\n"
  2285. " aaaaa,\n"
  2286. " aaaaa(\n"
  2287. " aaaaa,\n"
  2288. " aaaaa(\n"
  2289. " aaaaa,\n"
  2290. " aaaaa(\n"
  2291. " aaaaa,\n"
  2292. " aaaaa(\n"
  2293. " aaaaa,\n"
  2294. " aaaaa))))))))))));",
  2295. getLLVMStyleWithColumns(65));
  2296. verifyFormat(
  2297. "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"
  2298. " a),\n"
  2299. " a),\n"
  2300. " a),\n"
  2301. " a),\n"
  2302. " a),\n"
  2303. " a),\n"
  2304. " a),\n"
  2305. " a),\n"
  2306. " a),\n"
  2307. " a),\n"
  2308. " a),\n"
  2309. " a),\n"
  2310. " a),\n"
  2311. " a),\n"
  2312. " a),\n"
  2313. " a),\n"
  2314. " a)",
  2315. getLLVMStyleWithColumns(65));
  2316. // This test takes VERY long when memoization is broken.
  2317. FormatStyle OnePerLine = getLLVMStyle();
  2318. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  2319. OnePerLine.BinPackParameters = false;
  2320. std::string input = "Constructor()\n"
  2321. " : aaaa(a,\n";
  2322. for (unsigned i = 0, e = 80; i != e; ++i) {
  2323. input += " a,\n";
  2324. }
  2325. input += " a) {}";
  2326. verifyFormat(input, OnePerLine);
  2327. }
  2328. TEST_F(FormatTest, BreaksAsHighAsPossible) {
  2329. verifyFormat(
  2330. "void f() {\n"
  2331. " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
  2332. " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
  2333. " f();\n"
  2334. "}");
  2335. verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
  2336. " Intervals[i - 1].getRange().getLast()) {\n}");
  2337. }
  2338. TEST_F(FormatTest, BreaksFunctionDeclarations) {
  2339. // Principially, we break function declarations in a certain order:
  2340. // 1) break amongst arguments.
  2341. verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
  2342. " Cccccccccccccc cccccccccccccc);");
  2343. // 2) break after return type.
  2344. verifyFormat(
  2345. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2346. " bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
  2347. getGoogleStyle());
  2348. // 3) break after (.
  2349. verifyFormat(
  2350. "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
  2351. " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
  2352. getGoogleStyle());
  2353. // 4) break before after nested name specifiers.
  2354. verifyFormat(
  2355. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2356. " SomeClasssssssssssssssssssssssssssssssssssssss::\n"
  2357. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
  2358. getGoogleStyle());
  2359. // However, there are exceptions, if a sufficient amount of lines can be
  2360. // saved.
  2361. // FIXME: The precise cut-offs wrt. the number of saved lines might need some
  2362. // more adjusting.
  2363. verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  2364. " Cccccccccccccc cccccccccc,\n"
  2365. " Cccccccccccccc cccccccccc,\n"
  2366. " Cccccccccccccc cccccccccc,\n"
  2367. " Cccccccccccccc cccccccccc);");
  2368. verifyFormat(
  2369. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2370. " bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  2371. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  2372. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
  2373. getGoogleStyle());
  2374. verifyFormat(
  2375. "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  2376. " Cccccccccccccc cccccccccc,\n"
  2377. " Cccccccccccccc cccccccccc,\n"
  2378. " Cccccccccccccc cccccccccc,\n"
  2379. " Cccccccccccccc cccccccccc,\n"
  2380. " Cccccccccccccc cccccccccc,\n"
  2381. " Cccccccccccccc cccccccccc);");
  2382. verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  2383. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  2384. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  2385. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  2386. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
  2387. // Break after multi-line parameters.
  2388. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2389. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2390. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2391. " bbbb bbbb);");
  2392. // Treat overloaded operators like other functions.
  2393. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  2394. "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
  2395. verifyGoogleFormat(
  2396. "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
  2397. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  2398. }
  2399. TEST_F(FormatTest, TrailingReturnType) {
  2400. verifyFormat("auto foo() -> int;\n");
  2401. verifyFormat("struct S {\n"
  2402. " auto bar() const -> int;\n"
  2403. "};");
  2404. verifyFormat("template <size_t Order, typename T>\n"
  2405. "auto load_img(const std::string &filename)\n"
  2406. " -> alias::tensor<Order, T, mem::tag::cpu> {}");
  2407. // Not trailing return types.
  2408. verifyFormat("void f() { auto a = b->c(); }");
  2409. }
  2410. TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
  2411. // Avoid breaking before trailing 'const'.
  2412. verifyFormat("void someLongFunction(\n"
  2413. " int someLongParameter) const {}",
  2414. getLLVMStyleWithColumns(46));
  2415. FormatStyle Style = getGoogleStyle();
  2416. Style.ColumnLimit = 47;
  2417. verifyFormat("void\n"
  2418. "someLongFunction(int someLongParameter) const {\n}",
  2419. getLLVMStyleWithColumns(47));
  2420. verifyFormat("void someLongFunction(\n"
  2421. " int someLongParameter) const {}",
  2422. Style);
  2423. verifyFormat("LoooooongReturnType\n"
  2424. "someLoooooooongFunction() const {}",
  2425. getLLVMStyleWithColumns(47));
  2426. verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
  2427. " const {}",
  2428. Style);
  2429. // Avoid breaking before other trailing annotations, if they are not
  2430. // function-like.
  2431. verifyFormat("void SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2432. " aaaaaaaaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
  2433. // Breaking before function-like trailing annotations is fine to keep them
  2434. // close to their arguments.
  2435. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2436. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  2437. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  2438. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  2439. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  2440. " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
  2441. verifyFormat(
  2442. "void aaaaaaaaaaaaaaaaaa()\n"
  2443. " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
  2444. " aaaaaaaaaaaaaaaaaaaaaaaaa));");
  2445. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2446. " __attribute__((unused));");
  2447. verifyFormat(
  2448. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2449. " GUARDED_BY(aaaaaaaaaaaa);",
  2450. getGoogleStyle());
  2451. verifyFormat(
  2452. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2453. " GUARDED_BY(aaaaaaaaaaaa);",
  2454. getGoogleStyle());
  2455. }
  2456. TEST_F(FormatTest, BreaksDesireably) {
  2457. verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  2458. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  2459. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
  2460. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2461. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  2462. "}");
  2463. verifyFormat(
  2464. "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2465. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  2466. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2467. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2468. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  2469. verifyFormat(
  2470. "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2471. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  2472. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2473. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
  2474. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2475. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2476. verifyFormat(
  2477. "void f() {\n"
  2478. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
  2479. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  2480. "}");
  2481. verifyFormat(
  2482. "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2483. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  2484. verifyFormat(
  2485. "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2486. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  2487. verifyFormat(
  2488. "aaaaaaaaaaaaaaaaa(\n"
  2489. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2490. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2491. // This test case breaks on an incorrect memoization, i.e. an optimization not
  2492. // taking into account the StopAt value.
  2493. verifyFormat(
  2494. "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2495. " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2496. " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2497. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2498. verifyFormat("{\n {\n {\n"
  2499. " Annotation.SpaceRequiredBefore =\n"
  2500. " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
  2501. " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
  2502. " }\n }\n}");
  2503. // Break on an outer level if there was a break on an inner level.
  2504. EXPECT_EQ("f(g(h(a, // comment\n"
  2505. " b, c),\n"
  2506. " d, e),\n"
  2507. " x, y);",
  2508. format("f(g(h(a, // comment\n"
  2509. " b, c), d, e), x, y);"));
  2510. // Prefer breaking similar line breaks.
  2511. verifyFormat(
  2512. "const int kTrackingOptions = NSTrackingMouseMoved |\n"
  2513. " NSTrackingMouseEnteredAndExited |\n"
  2514. " NSTrackingActiveAlways;");
  2515. }
  2516. TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
  2517. FormatStyle NoBinPacking = getGoogleStyle();
  2518. NoBinPacking.BinPackParameters = false;
  2519. verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
  2520. " aaaaaaaaaaaaaaaaaaaa,\n"
  2521. " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
  2522. NoBinPacking);
  2523. verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
  2524. " aaaaaaaaaaaaa,\n"
  2525. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
  2526. NoBinPacking);
  2527. verifyFormat(
  2528. "aaaaaaaa(aaaaaaaaaaaaa,\n"
  2529. " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2530. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  2531. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2532. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
  2533. NoBinPacking);
  2534. verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  2535. " .aaaaaaaaaaaaaaaaaa();",
  2536. NoBinPacking);
  2537. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2538. " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);",
  2539. NoBinPacking);
  2540. verifyFormat(
  2541. "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2542. " aaaaaaaaaaaa,\n"
  2543. " aaaaaaaaaaaa);",
  2544. NoBinPacking);
  2545. verifyFormat(
  2546. "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
  2547. " ddddddddddddddddddddddddddddd),\n"
  2548. " test);",
  2549. NoBinPacking);
  2550. verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
  2551. " aaaaaaaaaaaaaaaaaaaaaaa,\n"
  2552. " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;",
  2553. NoBinPacking);
  2554. verifyFormat("a(\"a\"\n"
  2555. " \"a\",\n"
  2556. " a);");
  2557. NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
  2558. verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
  2559. " aaaaaaaaa,\n"
  2560. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  2561. NoBinPacking);
  2562. verifyFormat(
  2563. "void f() {\n"
  2564. " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  2565. " .aaaaaaa();\n"
  2566. "}",
  2567. NoBinPacking);
  2568. verifyFormat(
  2569. "template <class SomeType, class SomeOtherType>\n"
  2570. "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
  2571. NoBinPacking);
  2572. }
  2573. TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
  2574. FormatStyle Style = getLLVMStyleWithColumns(15);
  2575. Style.ExperimentalAutoDetectBinPacking = true;
  2576. EXPECT_EQ("aaa(aaaa,\n"
  2577. " aaaa,\n"
  2578. " aaaa);\n"
  2579. "aaa(aaaa,\n"
  2580. " aaaa,\n"
  2581. " aaaa);",
  2582. format("aaa(aaaa,\n" // one-per-line
  2583. " aaaa,\n"
  2584. " aaaa );\n"
  2585. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  2586. Style));
  2587. EXPECT_EQ("aaa(aaaa, aaaa,\n"
  2588. " aaaa);\n"
  2589. "aaa(aaaa, aaaa,\n"
  2590. " aaaa);",
  2591. format("aaa(aaaa, aaaa,\n" // bin-packed
  2592. " aaaa );\n"
  2593. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  2594. Style));
  2595. }
  2596. TEST_F(FormatTest, FormatsBuilderPattern) {
  2597. verifyFormat(
  2598. "return llvm::StringSwitch<Reference::Kind>(name)\n"
  2599. " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
  2600. " .StartsWith(\".eh_frame\", ORDER_EH_FRAME).StartsWith(\".init\", ORDER_INIT)\n"
  2601. " .StartsWith(\".fini\", ORDER_FINI).StartsWith(\".hash\", ORDER_HASH)\n"
  2602. " .Default(ORDER_TEXT);\n");
  2603. verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
  2604. " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
  2605. verifyFormat(
  2606. "aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaa(\n"
  2607. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2608. " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
  2609. verifyFormat(
  2610. "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
  2611. " aaaaaaaaaaaaaa);");
  2612. verifyFormat(
  2613. "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa = aaaaaa->aaaaaaaaaaaa()\n"
  2614. " ->aaaaaaaaaaaaaaaa(\n"
  2615. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2616. " ->aaaaaaaaaaaaaaaaa();");
  2617. }
  2618. TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
  2619. verifyFormat(
  2620. "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2621. " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
  2622. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  2623. " ccccccccccccccccccccccccc) {\n}");
  2624. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  2625. " ccccccccccccccccccccccccc) {\n}");
  2626. verifyFormat(
  2627. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
  2628. " ccccccccccccccccccccccccc) {\n}");
  2629. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
  2630. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
  2631. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
  2632. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  2633. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
  2634. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
  2635. " aaaaaaaaaaaaaaa != aa) {\n}");
  2636. }
  2637. TEST_F(FormatTest, BreaksAfterAssignments) {
  2638. verifyFormat(
  2639. "unsigned Cost =\n"
  2640. " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
  2641. " SI->getPointerAddressSpaceee());\n");
  2642. verifyFormat(
  2643. "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
  2644. " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
  2645. verifyFormat(
  2646. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0)\n"
  2647. " .aaaa().aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
  2648. verifyFormat("unsigned OriginalStartColumn =\n"
  2649. " SourceMgr.getSpellingColumnNumber(\n"
  2650. " Current.FormatTok.getStartOfNonWhitespace()) -\n"
  2651. " 1;");
  2652. }
  2653. TEST_F(FormatTest, AlignsAfterAssignments) {
  2654. verifyFormat(
  2655. "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2656. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  2657. verifyFormat(
  2658. "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2659. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  2660. verifyFormat(
  2661. "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2662. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  2663. verifyFormat(
  2664. "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2665. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  2666. verifyFormat(
  2667. "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2668. " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2669. " aaaaaaaaaaaaaaaaaaaaaaaa;");
  2670. }
  2671. TEST_F(FormatTest, AlignsAfterReturn) {
  2672. verifyFormat(
  2673. "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2674. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  2675. verifyFormat(
  2676. "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2677. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  2678. verifyFormat(
  2679. "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  2680. " aaaaaaaaaaaaaaaaaaaaaa();");
  2681. verifyFormat(
  2682. "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  2683. " aaaaaaaaaaaaaaaaaaaaaa());");
  2684. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2685. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2686. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2687. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
  2688. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  2689. }
  2690. TEST_F(FormatTest, BreaksConditionalExpressions) {
  2691. verifyFormat(
  2692. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2693. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2694. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2695. verifyFormat(
  2696. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2697. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2698. verifyFormat(
  2699. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
  2700. " : aaaaaaaaaaaaa);");
  2701. verifyFormat(
  2702. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2703. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2704. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2705. " aaaaaaaaaaaaa);");
  2706. verifyFormat(
  2707. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2708. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2709. " aaaaaaaaaaaaa);");
  2710. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2711. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2712. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2713. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2714. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2715. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2716. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2717. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2718. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2719. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2720. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2721. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2722. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2723. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2724. " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2725. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2726. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2727. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2728. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2729. " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  2730. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  2731. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2732. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2733. " : aaaaaaaaaaaaaaaa;");
  2734. verifyFormat(
  2735. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2736. " ? aaaaaaaaaaaaaaa\n"
  2737. " : aaaaaaaaaaaaaaa;");
  2738. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  2739. " aaaaaaaaa\n"
  2740. " ? b\n"
  2741. " : c);");
  2742. verifyFormat(
  2743. "unsigned Indent =\n"
  2744. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
  2745. " ? IndentForLevel[TheLine.Level]\n"
  2746. " : TheLine * 2,\n"
  2747. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  2748. getLLVMStyleWithColumns(70));
  2749. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  2750. " ? aaaaaaaaaaaaaaa\n"
  2751. " : bbbbbbbbbbbbbbb //\n"
  2752. " ? ccccccccccccccc\n"
  2753. " : ddddddddddddddd;");
  2754. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  2755. " ? aaaaaaaaaaaaaaa\n"
  2756. " : (bbbbbbbbbbbbbbb //\n"
  2757. " ? ccccccccccccccc\n"
  2758. " : ddddddddddddddd);");
  2759. FormatStyle NoBinPacking = getLLVMStyle();
  2760. NoBinPacking.BinPackParameters = false;
  2761. verifyFormat(
  2762. "void f() {\n"
  2763. " g(aaa,\n"
  2764. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  2765. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2766. " ? aaaaaaaaaaaaaaa\n"
  2767. " : aaaaaaaaaaaaaaa);\n"
  2768. "}",
  2769. NoBinPacking);
  2770. verifyFormat(
  2771. "void f() {\n"
  2772. " g(aaa,\n"
  2773. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  2774. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2775. " ?: aaaaaaaaaaaaaaa);\n"
  2776. "}",
  2777. NoBinPacking);
  2778. }
  2779. TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
  2780. verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
  2781. " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
  2782. verifyFormat("bool a = true, b = false;");
  2783. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  2784. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
  2785. " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
  2786. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
  2787. verifyFormat(
  2788. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  2789. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
  2790. " d = e && f;");
  2791. verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
  2792. " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
  2793. verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  2794. " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
  2795. verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
  2796. " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
  2797. // FIXME: If multiple variables are defined, the "*" needs to move to the new
  2798. // line. Also fix indent for breaking after the type, this looks bad.
  2799. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
  2800. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
  2801. " *b = bbbbbbbbbbbbbbbbbbb;",
  2802. getGoogleStyle());
  2803. // Not ideal, but pointer-with-type does not allow much here.
  2804. verifyGoogleFormat(
  2805. "aaaaaaaaa* a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  2806. " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;");
  2807. }
  2808. TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
  2809. verifyFormat("arr[foo ? bar : baz];");
  2810. verifyFormat("f()[foo ? bar : baz];");
  2811. verifyFormat("(a + b)[foo ? bar : baz];");
  2812. verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
  2813. }
  2814. TEST_F(FormatTest, AlignsStringLiterals) {
  2815. verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
  2816. " \"short literal\");");
  2817. verifyFormat(
  2818. "looooooooooooooooooooooooongFunction(\n"
  2819. " \"short literal\"\n"
  2820. " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
  2821. verifyFormat("someFunction(\"Always break between multi-line\"\n"
  2822. " \" string literals\",\n"
  2823. " and, other, parameters);");
  2824. EXPECT_EQ("fun + \"1243\" /* comment */\n"
  2825. " \"5678\";",
  2826. format("fun + \"1243\" /* comment */\n"
  2827. " \"5678\";",
  2828. getLLVMStyleWithColumns(28)));
  2829. EXPECT_EQ(
  2830. "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  2831. " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
  2832. " \"aaaaaaaaaaaaaaaa\";",
  2833. format("aaaaaa ="
  2834. "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  2835. "aaaaaaaaaaaaaaaaaaaaa\" "
  2836. "\"aaaaaaaaaaaaaaaa\";"));
  2837. verifyFormat("a = a + \"a\"\n"
  2838. " \"a\"\n"
  2839. " \"a\";");
  2840. verifyFormat("f(\"a\", \"b\"\n"
  2841. " \"c\");");
  2842. verifyFormat(
  2843. "#define LL_FORMAT \"ll\"\n"
  2844. "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
  2845. " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
  2846. verifyFormat("#define A(X) \\\n"
  2847. " \"aaaaa\" #X \"bbbbbb\" \\\n"
  2848. " \"ccccc\"",
  2849. getLLVMStyleWithColumns(23));
  2850. verifyFormat("#define A \"def\"\n"
  2851. "f(\"abc\" A \"ghi\"\n"
  2852. " \"jkl\");");
  2853. }
  2854. TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
  2855. FormatStyle NoBreak = getLLVMStyle();
  2856. NoBreak.AlwaysBreakBeforeMultilineStrings = false;
  2857. FormatStyle Break = getLLVMStyle();
  2858. Break.AlwaysBreakBeforeMultilineStrings = true;
  2859. verifyFormat("aaaa = \"bbbb\"\n"
  2860. " \"cccc\";",
  2861. NoBreak);
  2862. verifyFormat("aaaa =\n"
  2863. " \"bbbb\"\n"
  2864. " \"cccc\";",
  2865. Break);
  2866. verifyFormat("aaaa(\"bbbb\"\n"
  2867. " \"cccc\");",
  2868. NoBreak);
  2869. verifyFormat("aaaa(\n"
  2870. " \"bbbb\"\n"
  2871. " \"cccc\");",
  2872. Break);
  2873. verifyFormat("aaaa(qqq, \"bbbb\"\n"
  2874. " \"cccc\");",
  2875. NoBreak);
  2876. verifyFormat("aaaa(qqq,\n"
  2877. " \"bbbb\"\n"
  2878. " \"cccc\");",
  2879. Break);
  2880. // Don't break if there is no column gain.
  2881. verifyFormat("f(\"aaaa\"\n"
  2882. " \"bbbb\");",
  2883. Break);
  2884. // Treat literals with escaped newlines like multi-line string literals.
  2885. EXPECT_EQ("x = \"a\\\n"
  2886. "b\\\n"
  2887. "c\";",
  2888. format("x = \"a\\\n"
  2889. "b\\\n"
  2890. "c\";",
  2891. NoBreak));
  2892. EXPECT_EQ("x =\n"
  2893. " \"a\\\n"
  2894. "b\\\n"
  2895. "c\";",
  2896. format("x = \"a\\\n"
  2897. "b\\\n"
  2898. "c\";",
  2899. Break));
  2900. }
  2901. TEST_F(FormatTest, AlignsPipes) {
  2902. verifyFormat(
  2903. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2904. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2905. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  2906. verifyFormat(
  2907. "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
  2908. " << aaaaaaaaaaaaaaaaaaaa;");
  2909. verifyFormat(
  2910. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2911. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  2912. verifyFormat(
  2913. "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
  2914. " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
  2915. " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
  2916. verifyFormat(
  2917. "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2918. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2919. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  2920. verifyFormat("return out << \"somepacket = {\\n\"\n"
  2921. " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
  2922. " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
  2923. " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
  2924. " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
  2925. " << \"}\";");
  2926. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  2927. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  2928. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
  2929. verifyFormat(
  2930. "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
  2931. " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
  2932. " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
  2933. " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
  2934. " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
  2935. verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
  2936. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  2937. verifyFormat(
  2938. "void f() {\n"
  2939. " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
  2940. " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  2941. "}");
  2942. // Breaking before the first "<<" is generally not desirable.
  2943. verifyFormat(
  2944. "llvm::errs()\n"
  2945. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2946. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2947. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2948. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  2949. getLLVMStyleWithColumns(70));
  2950. verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  2951. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2952. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  2953. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2954. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  2955. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  2956. getLLVMStyleWithColumns(70));
  2957. // But sometimes, breaking before the first "<<" is necessary.
  2958. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
  2959. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2960. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  2961. verifyFormat(
  2962. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2963. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  2964. }
  2965. TEST_F(FormatTest, UnderstandsEquals) {
  2966. verifyFormat(
  2967. "aaaaaaaaaaaaaaaaa =\n"
  2968. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  2969. verifyFormat(
  2970. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  2971. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  2972. verifyFormat(
  2973. "if (a) {\n"
  2974. " f();\n"
  2975. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  2976. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  2977. "}");
  2978. verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  2979. " 100000000 + 10000000) {\n}");
  2980. }
  2981. TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
  2982. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  2983. " .looooooooooooooooooooooooooooooooooooooongFunction();");
  2984. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  2985. " ->looooooooooooooooooooooooooooooooooooooongFunction();");
  2986. verifyFormat(
  2987. "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
  2988. " Parameter2);");
  2989. verifyFormat(
  2990. "ShortObject->shortFunction(\n"
  2991. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
  2992. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
  2993. verifyFormat("loooooooooooooongFunction(\n"
  2994. " LoooooooooooooongObject->looooooooooooooooongFunction());");
  2995. verifyFormat(
  2996. "function(LoooooooooooooooooooooooooooooooooooongObject\n"
  2997. " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
  2998. verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  2999. " .WillRepeatedly(Return(SomeValue));");
  3000. verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)]\n"
  3001. " .insert(ccccccccccccccccccccccc);");
  3002. verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3003. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).aaaaa(aaaaa),\n"
  3004. " aaaaaaaaaaaaaaaaaaaaa);");
  3005. verifyFormat(
  3006. "aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3007. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3008. " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3009. " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3010. " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3011. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3012. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3013. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3014. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
  3015. "}");
  3016. // Here, it is not necessary to wrap at "." or "->".
  3017. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
  3018. " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  3019. verifyFormat(
  3020. "aaaaaaaaaaa->aaaaaaaaa(\n"
  3021. " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3022. " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
  3023. verifyFormat(
  3024. "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3025. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
  3026. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
  3027. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  3028. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
  3029. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  3030. // FIXME: Should we break before .a()?
  3031. verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3032. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).a();");
  3033. FormatStyle NoBinPacking = getLLVMStyle();
  3034. NoBinPacking.BinPackParameters = false;
  3035. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  3036. " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  3037. " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
  3038. " aaaaaaaaaaaaaaaaaaa,\n"
  3039. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3040. NoBinPacking);
  3041. // If there is a subsequent call, change to hanging indentation.
  3042. verifyFormat(
  3043. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3044. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
  3045. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3046. verifyFormat(
  3047. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3048. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
  3049. }
  3050. TEST_F(FormatTest, WrapsTemplateDeclarations) {
  3051. verifyFormat("template <typename T>\n"
  3052. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  3053. verifyFormat(
  3054. "template <typename T>\n"
  3055. "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
  3056. verifyFormat("template <typename T>\n"
  3057. "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
  3058. " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
  3059. verifyFormat(
  3060. "template <typename T>\n"
  3061. "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
  3062. " int Paaaaaaaaaaaaaaaaaaaaram2);");
  3063. verifyFormat(
  3064. "template <typename T>\n"
  3065. "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
  3066. " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
  3067. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3068. verifyFormat("template <typename T>\n"
  3069. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3070. " int aaaaaaaaaaaaaaaaaaaaaa);");
  3071. verifyFormat(
  3072. "template <typename T1, typename T2 = char, typename T3 = char,\n"
  3073. " typename T4 = char>\n"
  3074. "void f();");
  3075. verifyFormat(
  3076. "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
  3077. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3078. verifyFormat("a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
  3079. " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));");
  3080. verifyFormat("template <typename T> class C {};");
  3081. verifyFormat("template <typename T> void f();");
  3082. verifyFormat("template <typename T> void f() {}");
  3083. FormatStyle AlwaysBreak = getLLVMStyle();
  3084. AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
  3085. verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
  3086. verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
  3087. verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
  3088. verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3089. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
  3090. " ccccccccccccccccccccccccccccccccccccccccccccccc);");
  3091. verifyFormat(
  3092. "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  3093. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3094. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
  3095. " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  3096. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3097. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
  3098. " bbbbbbbbbbbbbbbbbbbbbbbb);",
  3099. getLLVMStyleWithColumns(72));
  3100. }
  3101. TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
  3102. verifyFormat(
  3103. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  3104. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3105. verifyFormat(
  3106. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  3107. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3108. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  3109. // FIXME: Should we have the extra indent after the second break?
  3110. verifyFormat(
  3111. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  3112. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  3113. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3114. verifyFormat(
  3115. "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
  3116. " cccccccccccccccccccccccccccccccccccccccccccccc());");
  3117. // Breaking at nested name specifiers is generally not desirable.
  3118. verifyFormat(
  3119. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3120. " aaaaaaaaaaaaaaaaaaaaaaa);");
  3121. verifyFormat(
  3122. "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  3123. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3124. " aaaaaaaaaaaaaaaaaaaaa);",
  3125. getLLVMStyleWithColumns(74));
  3126. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  3127. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3128. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3129. }
  3130. TEST_F(FormatTest, UnderstandsTemplateParameters) {
  3131. verifyFormat("A<int> a;");
  3132. verifyFormat("A<A<A<int> > > a;");
  3133. verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
  3134. verifyFormat("bool x = a < 1 || 2 > a;");
  3135. verifyFormat("bool x = 5 < f<int>();");
  3136. verifyFormat("bool x = f<int>() > 5;");
  3137. verifyFormat("bool x = 5 < a<int>::x;");
  3138. verifyFormat("bool x = a < 4 ? a > 2 : false;");
  3139. verifyFormat("bool x = f() ? a < 2 : a > 2;");
  3140. verifyGoogleFormat("A<A<int>> a;");
  3141. verifyGoogleFormat("A<A<A<int>>> a;");
  3142. verifyGoogleFormat("A<A<A<A<int>>>> a;");
  3143. verifyGoogleFormat("A<A<int> > a;");
  3144. verifyGoogleFormat("A<A<A<int> > > a;");
  3145. verifyGoogleFormat("A<A<A<A<int> > > > a;");
  3146. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
  3147. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
  3148. verifyFormat("test >> a >> b;");
  3149. verifyFormat("test << a >> b;");
  3150. verifyFormat("f<int>();");
  3151. verifyFormat("template <typename T> void f() {}");
  3152. // Not template parameters.
  3153. verifyFormat("return a < b && c > d;");
  3154. verifyFormat("void f() {\n"
  3155. " while (a < b && c > d) {\n"
  3156. " }\n"
  3157. "}");
  3158. verifyFormat("template <typename... Types>\n"
  3159. "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
  3160. }
  3161. TEST_F(FormatTest, UnderstandsBinaryOperators) {
  3162. verifyFormat("COMPARE(a, ==, b);");
  3163. }
  3164. TEST_F(FormatTest, UnderstandsPointersToMembers) {
  3165. verifyFormat("int A::*x;");
  3166. verifyFormat("int (S::*func)(void *);");
  3167. verifyFormat("void f() { int (S::*func)(void *); }");
  3168. verifyFormat("typedef bool *(Class::*Member)() const;");
  3169. verifyFormat("void f() {\n"
  3170. " (a->*f)();\n"
  3171. " a->*x;\n"
  3172. " (a.*f)();\n"
  3173. " ((*a).*f)();\n"
  3174. " a.*x;\n"
  3175. "}");
  3176. verifyFormat("void f() {\n"
  3177. " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  3178. " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
  3179. "}");
  3180. FormatStyle Style = getLLVMStyle();
  3181. Style.PointerBindsToType = true;
  3182. verifyFormat("typedef bool* (Class::*Member)() const;", Style);
  3183. }
  3184. TEST_F(FormatTest, UnderstandsUnaryOperators) {
  3185. verifyFormat("int a = -2;");
  3186. verifyFormat("f(-1, -2, -3);");
  3187. verifyFormat("a[-1] = 5;");
  3188. verifyFormat("int a = 5 + -2;");
  3189. verifyFormat("if (i == -1) {\n}");
  3190. verifyFormat("if (i != -1) {\n}");
  3191. verifyFormat("if (i > -1) {\n}");
  3192. verifyFormat("if (i < -1) {\n}");
  3193. verifyFormat("++(a->f());");
  3194. verifyFormat("--(a->f());");
  3195. verifyFormat("(a->f())++;");
  3196. verifyFormat("a[42]++;");
  3197. verifyFormat("if (!(a->f())) {\n}");
  3198. verifyFormat("a-- > b;");
  3199. verifyFormat("b ? -a : c;");
  3200. verifyFormat("n * sizeof char16;");
  3201. verifyFormat("n * alignof char16;", getGoogleStyle());
  3202. verifyFormat("sizeof(char);");
  3203. verifyFormat("alignof(char);", getGoogleStyle());
  3204. verifyFormat("return -1;");
  3205. verifyFormat("switch (a) {\n"
  3206. "case -1:\n"
  3207. " break;\n"
  3208. "}");
  3209. verifyFormat("#define X -1");
  3210. verifyFormat("#define X -kConstant");
  3211. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
  3212. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
  3213. verifyFormat("int a = /* confusing comment */ -1;");
  3214. // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
  3215. verifyFormat("int a = i /* confusing comment */++;");
  3216. }
  3217. TEST_F(FormatTest, UndestandsOverloadedOperators) {
  3218. verifyFormat("bool operator<();");
  3219. verifyFormat("bool operator>();");
  3220. verifyFormat("bool operator=();");
  3221. verifyFormat("bool operator==();");
  3222. verifyFormat("bool operator!=();");
  3223. verifyFormat("int operator+();");
  3224. verifyFormat("int operator++();");
  3225. verifyFormat("bool operator();");
  3226. verifyFormat("bool operator()();");
  3227. verifyFormat("bool operator[]();");
  3228. verifyFormat("operator bool();");
  3229. verifyFormat("operator int();");
  3230. verifyFormat("operator void *();");
  3231. verifyFormat("operator SomeType<int>();");
  3232. verifyFormat("operator SomeType<int, int>();");
  3233. verifyFormat("operator SomeType<SomeType<int> >();");
  3234. verifyFormat("void *operator new(std::size_t size);");
  3235. verifyFormat("void *operator new[](std::size_t size);");
  3236. verifyFormat("void operator delete(void *ptr);");
  3237. verifyFormat("void operator delete[](void *ptr);");
  3238. verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
  3239. "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
  3240. verifyFormat(
  3241. "ostream &operator<<(ostream &OutputStream,\n"
  3242. " SomeReallyLongType WithSomeReallyLongValue);");
  3243. verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
  3244. " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
  3245. " return left.group < right.group;\n"
  3246. "}");
  3247. verifyFormat("SomeType &operator=(const SomeType &S);");
  3248. verifyGoogleFormat("operator void*();");
  3249. verifyGoogleFormat("operator SomeType<SomeType<int>>();");
  3250. }
  3251. TEST_F(FormatTest, UnderstandsNewAndDelete) {
  3252. verifyFormat("void f() {\n"
  3253. " A *a = new A;\n"
  3254. " A *a = new (placement) A;\n"
  3255. " delete a;\n"
  3256. " delete (A *)a;\n"
  3257. "}");
  3258. }
  3259. TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
  3260. verifyFormat("int *f(int *a) {}");
  3261. verifyFormat("int main(int argc, char **argv) {}");
  3262. verifyFormat("Test::Test(int b) : a(b * b) {}");
  3263. verifyIndependentOfContext("f(a, *a);");
  3264. verifyFormat("void g() { f(*a); }");
  3265. verifyIndependentOfContext("int a = b * 10;");
  3266. verifyIndependentOfContext("int a = 10 * b;");
  3267. verifyIndependentOfContext("int a = b * c;");
  3268. verifyIndependentOfContext("int a += b * c;");
  3269. verifyIndependentOfContext("int a -= b * c;");
  3270. verifyIndependentOfContext("int a *= b * c;");
  3271. verifyIndependentOfContext("int a /= b * c;");
  3272. verifyIndependentOfContext("int a = *b;");
  3273. verifyIndependentOfContext("int a = *b * c;");
  3274. verifyIndependentOfContext("int a = b * *c;");
  3275. verifyIndependentOfContext("return 10 * b;");
  3276. verifyIndependentOfContext("return *b * *c;");
  3277. verifyIndependentOfContext("return a & ~b;");
  3278. verifyIndependentOfContext("f(b ? *c : *d);");
  3279. verifyIndependentOfContext("int a = b ? *c : *d;");
  3280. verifyIndependentOfContext("*b = a;");
  3281. verifyIndependentOfContext("a * ~b;");
  3282. verifyIndependentOfContext("a * !b;");
  3283. verifyIndependentOfContext("a * +b;");
  3284. verifyIndependentOfContext("a * -b;");
  3285. verifyIndependentOfContext("a * ++b;");
  3286. verifyIndependentOfContext("a * --b;");
  3287. verifyIndependentOfContext("a[4] * b;");
  3288. verifyIndependentOfContext("a[a * a] = 1;");
  3289. verifyIndependentOfContext("f() * b;");
  3290. verifyIndependentOfContext("a * [self dostuff];");
  3291. verifyIndependentOfContext("int x = a * (a + b);");
  3292. verifyIndependentOfContext("(a *)(a + b);");
  3293. verifyIndependentOfContext("int *pa = (int *)&a;");
  3294. verifyIndependentOfContext("return sizeof(int **);");
  3295. verifyIndependentOfContext("return sizeof(int ******);");
  3296. verifyIndependentOfContext("return (int **&)a;");
  3297. verifyIndependentOfContext("f((*PointerToArray)[10]);");
  3298. verifyFormat("void f(Type (*parameter)[10]) {}");
  3299. verifyGoogleFormat("return sizeof(int**);");
  3300. verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
  3301. verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
  3302. verifyFormat("auto a = [](int **&, int ***) {};");
  3303. verifyIndependentOfContext("InvalidRegions[*R] = 0;");
  3304. verifyIndependentOfContext("A<int *> a;");
  3305. verifyIndependentOfContext("A<int **> a;");
  3306. verifyIndependentOfContext("A<int *, int *> a;");
  3307. verifyIndependentOfContext(
  3308. "const char *const p = reinterpret_cast<const char *const>(q);");
  3309. verifyIndependentOfContext("A<int **, int **> a;");
  3310. verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
  3311. verifyFormat("for (char **a = b; *a; ++a) {\n}");
  3312. verifyFormat("for (; a && b;) {\n}");
  3313. verifyFormat(
  3314. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3315. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3316. verifyGoogleFormat("int main(int argc, char** argv) {}");
  3317. verifyGoogleFormat("A<int*> a;");
  3318. verifyGoogleFormat("A<int**> a;");
  3319. verifyGoogleFormat("A<int*, int*> a;");
  3320. verifyGoogleFormat("A<int**, int**> a;");
  3321. verifyGoogleFormat("f(b ? *c : *d);");
  3322. verifyGoogleFormat("int a = b ? *c : *d;");
  3323. verifyGoogleFormat("Type* t = **x;");
  3324. verifyGoogleFormat("Type* t = *++*x;");
  3325. verifyGoogleFormat("*++*x;");
  3326. verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
  3327. verifyGoogleFormat("Type* t = x++ * y;");
  3328. verifyGoogleFormat(
  3329. "const char* const p = reinterpret_cast<const char* const>(q);");
  3330. verifyIndependentOfContext("a = *(x + y);");
  3331. verifyIndependentOfContext("a = &(x + y);");
  3332. verifyIndependentOfContext("*(x + y).call();");
  3333. verifyIndependentOfContext("&(x + y)->call();");
  3334. verifyFormat("void f() { &(*I).first; }");
  3335. verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
  3336. verifyFormat(
  3337. "int *MyValues = {\n"
  3338. " *A, // Operator detection might be confused by the '{'\n"
  3339. " *BB // Operator detection might be confused by previous comment\n"
  3340. "};");
  3341. verifyIndependentOfContext("if (int *a = &b)");
  3342. verifyIndependentOfContext("if (int &a = *b)");
  3343. verifyIndependentOfContext("if (a & b[i])");
  3344. verifyIndependentOfContext("if (a::b::c::d & b[i])");
  3345. verifyIndependentOfContext("if (*b[i])");
  3346. verifyIndependentOfContext("if (int *a = (&b))");
  3347. verifyIndependentOfContext("while (int *a = &b)");
  3348. verifyFormat("void f() {\n"
  3349. " for (const int &v : Values) {\n"
  3350. " }\n"
  3351. "}");
  3352. verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
  3353. verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
  3354. verifyFormat("#define MACRO \\\n"
  3355. " int *i = a * b; \\\n"
  3356. " void f(a *b);",
  3357. getLLVMStyleWithColumns(19));
  3358. verifyIndependentOfContext("A = new SomeType *[Length];");
  3359. verifyIndependentOfContext("A = new SomeType *[Length]();");
  3360. verifyIndependentOfContext("T **t = new T *;");
  3361. verifyIndependentOfContext("T **t = new T *();");
  3362. verifyGoogleFormat("A = new SomeType* [Length]();");
  3363. verifyGoogleFormat("A = new SomeType* [Length];");
  3364. verifyGoogleFormat("T** t = new T*;");
  3365. verifyGoogleFormat("T** t = new T*();");
  3366. FormatStyle PointerLeft = getLLVMStyle();
  3367. PointerLeft.PointerBindsToType = true;
  3368. verifyFormat("delete *x;", PointerLeft);
  3369. }
  3370. TEST_F(FormatTest, UnderstandsAttributes) {
  3371. verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
  3372. }
  3373. TEST_F(FormatTest, UnderstandsEllipsis) {
  3374. verifyFormat("int printf(const char *fmt, ...);");
  3375. verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
  3376. verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
  3377. FormatStyle PointersLeft = getLLVMStyle();
  3378. PointersLeft.PointerBindsToType = true;
  3379. verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
  3380. }
  3381. TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
  3382. EXPECT_EQ("int *a;\n"
  3383. "int *a;\n"
  3384. "int *a;",
  3385. format("int *a;\n"
  3386. "int* a;\n"
  3387. "int *a;",
  3388. getGoogleStyle()));
  3389. EXPECT_EQ("int* a;\n"
  3390. "int* a;\n"
  3391. "int* a;",
  3392. format("int* a;\n"
  3393. "int* a;\n"
  3394. "int *a;",
  3395. getGoogleStyle()));
  3396. EXPECT_EQ("int *a;\n"
  3397. "int *a;\n"
  3398. "int *a;",
  3399. format("int *a;\n"
  3400. "int * a;\n"
  3401. "int * a;",
  3402. getGoogleStyle()));
  3403. }
  3404. TEST_F(FormatTest, UnderstandsRvalueReferences) {
  3405. verifyFormat("int f(int &&a) {}");
  3406. verifyFormat("int f(int a, char &&b) {}");
  3407. verifyFormat("void f() { int &&a = b; }");
  3408. verifyGoogleFormat("int f(int a, char&& b) {}");
  3409. verifyGoogleFormat("void f() { int&& a = b; }");
  3410. verifyIndependentOfContext("A<int &&> a;");
  3411. verifyIndependentOfContext("A<int &&, int &&> a;");
  3412. verifyGoogleFormat("A<int&&> a;");
  3413. verifyGoogleFormat("A<int&&, int&&> a;");
  3414. // Not rvalue references:
  3415. verifyFormat("template <bool B, bool C> class A {\n"
  3416. " static_assert(B && C, \"Something is wrong\");\n"
  3417. "};");
  3418. }
  3419. TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
  3420. verifyFormat("void f() {\n"
  3421. " x[aaaaaaaaa -\n"
  3422. " b] = 23;\n"
  3423. "}",
  3424. getLLVMStyleWithColumns(15));
  3425. }
  3426. TEST_F(FormatTest, FormatsCasts) {
  3427. verifyFormat("Type *A = static_cast<Type *>(P);");
  3428. verifyFormat("Type *A = (Type *)P;");
  3429. verifyFormat("Type *A = (vector<Type *, int *>)P;");
  3430. verifyFormat("int a = (int)(2.0f);");
  3431. verifyFormat("int a = (int)2.0f;");
  3432. verifyFormat("x[(int32)y];");
  3433. verifyFormat("x = (int32)y;");
  3434. verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
  3435. verifyFormat("int a = (int)*b;");
  3436. verifyFormat("int a = (int)2.0f;");
  3437. verifyFormat("int a = (int)~0;");
  3438. verifyFormat("int a = (int)++a;");
  3439. verifyFormat("int a = (int)sizeof(int);");
  3440. verifyFormat("int a = (int)+2;");
  3441. verifyFormat("my_int a = (my_int)2.0f;");
  3442. verifyFormat("my_int a = (my_int)sizeof(int);");
  3443. verifyFormat("return (my_int)aaa;");
  3444. verifyFormat("#define x ((int)-1)");
  3445. verifyFormat("#define p(q) ((int *)&q)");
  3446. // FIXME: Without type knowledge, this can still fall apart miserably.
  3447. verifyFormat("void f() { my_int a = (my_int) * b; }");
  3448. verifyFormat("void f() { return P ? (my_int) * P : (my_int)0; }");
  3449. verifyFormat("my_int a = (my_int) ~0;");
  3450. verifyFormat("my_int a = (my_int)++ a;");
  3451. verifyFormat("my_int a = (my_int) + 2;");
  3452. // Don't break after a cast's
  3453. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3454. " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
  3455. " bbbbbbbbbbbbbbbbbbbbbb);");
  3456. // These are not casts.
  3457. verifyFormat("void f(int *) {}");
  3458. verifyFormat("f(foo)->b;");
  3459. verifyFormat("f(foo).b;");
  3460. verifyFormat("f(foo)(b);");
  3461. verifyFormat("f(foo)[b];");
  3462. verifyFormat("[](foo) { return 4; }(bar)];");
  3463. verifyFormat("(*funptr)(foo)[4];");
  3464. verifyFormat("funptrs[4](foo)[4];");
  3465. verifyFormat("void f(int *);");
  3466. verifyFormat("void f(int *) = 0;");
  3467. verifyFormat("void f(SmallVector<int>) {}");
  3468. verifyFormat("void f(SmallVector<int>);");
  3469. verifyFormat("void f(SmallVector<int>) = 0;");
  3470. verifyFormat("void f(int i = (kValue) * kMask) {}");
  3471. verifyFormat("void f(int i = (kA * kB) & kMask) {}");
  3472. verifyFormat("int a = sizeof(int) * b;");
  3473. verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
  3474. verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
  3475. verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
  3476. verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
  3477. // These are not casts, but at some point were confused with casts.
  3478. verifyFormat("virtual void foo(int *) override;");
  3479. verifyFormat("virtual void foo(char &) const;");
  3480. verifyFormat("virtual void foo(int *a, char *) const;");
  3481. verifyFormat("int a = sizeof(int *) + b;");
  3482. verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
  3483. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
  3484. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  3485. // FIXME: The indentation here is not ideal.
  3486. verifyFormat(
  3487. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3488. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
  3489. " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
  3490. }
  3491. TEST_F(FormatTest, FormatsFunctionTypes) {
  3492. verifyFormat("A<bool()> a;");
  3493. verifyFormat("A<SomeType()> a;");
  3494. verifyFormat("A<void (*)(int, std::string)> a;");
  3495. verifyFormat("A<void *(int)>;");
  3496. verifyFormat("void *(*a)(int *, SomeType *);");
  3497. verifyFormat("int (*func)(void *);");
  3498. verifyFormat("void f() { int (*func)(void *); }");
  3499. verifyGoogleFormat("A<void*(int*, SomeType*)>;");
  3500. verifyGoogleFormat("void* (*a)(int);");
  3501. // Other constructs can look somewhat like function types:
  3502. verifyFormat("A<sizeof(*x)> a;");
  3503. verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
  3504. verifyFormat("some_var = function(*some_pointer_var)[0];");
  3505. verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
  3506. }
  3507. TEST_F(FormatTest, BreaksLongDeclarations) {
  3508. verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
  3509. " AnotherNameForTheLongType;",
  3510. getGoogleStyle());
  3511. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  3512. " LoooooooooooooooooooooooooooooooooooooooongVariable;",
  3513. getGoogleStyle());
  3514. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
  3515. " LoooooooooooooooooooooooooooooooooooooooongVariable;",
  3516. getGoogleStyle());
  3517. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  3518. " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
  3519. getGoogleStyle());
  3520. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  3521. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  3522. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  3523. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  3524. // FIXME: Without the comment, this breaks after "(".
  3525. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
  3526. " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
  3527. getGoogleStyle());
  3528. verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
  3529. " int LoooooooooooooooooooongParam2) {}");
  3530. verifyFormat(
  3531. "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
  3532. " SourceLocation L, IdentifierIn *II,\n"
  3533. " Type *T) {}");
  3534. verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
  3535. "ReallyReallyLongFunctionName(\n"
  3536. " const std::string &SomeParameter,\n"
  3537. " const SomeType<string, SomeOtherTemplateParameter> &\n"
  3538. " ReallyReallyLongParameterName,\n"
  3539. " const SomeType<string, SomeOtherTemplateParameter> &\n"
  3540. " AnotherLongParameterName) {}");
  3541. verifyFormat("template <typename A>\n"
  3542. "SomeLoooooooooooooooooooooongType<\n"
  3543. " typename some_namespace::SomeOtherType<A>::Type>\n"
  3544. "Function() {}");
  3545. verifyFormat(
  3546. "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
  3547. " aaaaaaaaaaaaaaaaaaaaaaa;",
  3548. getGoogleStyle());
  3549. verifyGoogleFormat(
  3550. "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
  3551. " SourceLocation L) {}");
  3552. verifyGoogleFormat(
  3553. "some_namespace::LongReturnType\n"
  3554. "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
  3555. " int first_long_parameter, int second_parameter) {}");
  3556. verifyGoogleFormat("template <typename T>\n"
  3557. "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  3558. "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
  3559. verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3560. " int aaaaaaaaaaaaaaaaaaaaaaa);");
  3561. }
  3562. TEST_F(FormatTest, FormatsArrays) {
  3563. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  3564. " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
  3565. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3566. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  3567. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3568. " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
  3569. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3570. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  3571. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  3572. verifyFormat(
  3573. "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
  3574. " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  3575. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  3576. }
  3577. TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
  3578. verifyFormat("(a)->b();");
  3579. verifyFormat("--a;");
  3580. }
  3581. TEST_F(FormatTest, HandlesIncludeDirectives) {
  3582. verifyFormat("#include <string>\n"
  3583. "#include <a/b/c.h>\n"
  3584. "#include \"a/b/string\"\n"
  3585. "#include \"string.h\"\n"
  3586. "#include \"string.h\"\n"
  3587. "#include <a-a>\n"
  3588. "#include < path with space >\n"
  3589. "#include \"abc.h\" // this is included for ABC\n"
  3590. "#include \"some long include\" // with a comment\n"
  3591. "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
  3592. getLLVMStyleWithColumns(35));
  3593. verifyFormat("#import <string>");
  3594. verifyFormat("#import <a/b/c.h>");
  3595. verifyFormat("#import \"a/b/string\"");
  3596. verifyFormat("#import \"string.h\"");
  3597. verifyFormat("#import \"string.h\"");
  3598. }
  3599. //===----------------------------------------------------------------------===//
  3600. // Error recovery tests.
  3601. //===----------------------------------------------------------------------===//
  3602. TEST_F(FormatTest, IncompleteParameterLists) {
  3603. FormatStyle NoBinPacking = getLLVMStyle();
  3604. NoBinPacking.BinPackParameters = false;
  3605. verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
  3606. " double *min_x,\n"
  3607. " double *max_x,\n"
  3608. " double *min_y,\n"
  3609. " double *max_y,\n"
  3610. " double *min_z,\n"
  3611. " double *max_z, ) {}",
  3612. NoBinPacking);
  3613. }
  3614. TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
  3615. verifyFormat("void f() { return; }\n42");
  3616. verifyFormat("void f() {\n"
  3617. " if (0)\n"
  3618. " return;\n"
  3619. "}\n"
  3620. "42");
  3621. verifyFormat("void f() { return }\n42");
  3622. verifyFormat("void f() {\n"
  3623. " if (0)\n"
  3624. " return\n"
  3625. "}\n"
  3626. "42");
  3627. }
  3628. TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
  3629. EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
  3630. EXPECT_EQ("void f() {\n"
  3631. " if (a)\n"
  3632. " return\n"
  3633. "}",
  3634. format("void f ( ) { if ( a ) return }"));
  3635. EXPECT_EQ("namespace N {\n"
  3636. "void f()\n"
  3637. "}",
  3638. format("namespace N { void f() }"));
  3639. EXPECT_EQ("namespace N {\n"
  3640. "void f() {}\n"
  3641. "void g()\n"
  3642. "}",
  3643. format("namespace N { void f( ) { } void g( ) }"));
  3644. }
  3645. TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
  3646. verifyFormat("int aaaaaaaa =\n"
  3647. " // Overlylongcomment\n"
  3648. " b;",
  3649. getLLVMStyleWithColumns(20));
  3650. verifyFormat("function(\n"
  3651. " ShortArgument,\n"
  3652. " LoooooooooooongArgument);\n",
  3653. getLLVMStyleWithColumns(20));
  3654. }
  3655. TEST_F(FormatTest, IncorrectAccessSpecifier) {
  3656. verifyFormat("public:");
  3657. verifyFormat("class A {\n"
  3658. "public\n"
  3659. " void f() {}\n"
  3660. "};");
  3661. verifyFormat("public\n"
  3662. "int qwerty;");
  3663. verifyFormat("public\n"
  3664. "B {}");
  3665. verifyFormat("public\n"
  3666. "{}");
  3667. verifyFormat("public\n"
  3668. "B { int x; }");
  3669. }
  3670. TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
  3671. verifyFormat("{");
  3672. verifyFormat("#})");
  3673. }
  3674. TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
  3675. verifyFormat("do {\n}");
  3676. verifyFormat("do {\n}\n"
  3677. "f();");
  3678. verifyFormat("do {\n}\n"
  3679. "wheeee(fun);");
  3680. verifyFormat("do {\n"
  3681. " f();\n"
  3682. "}");
  3683. }
  3684. TEST_F(FormatTest, IncorrectCodeMissingParens) {
  3685. verifyFormat("if {\n foo;\n foo();\n}");
  3686. verifyFormat("switch {\n foo;\n foo();\n}");
  3687. verifyFormat("for {\n foo;\n foo();\n}");
  3688. verifyFormat("while {\n foo;\n foo();\n}");
  3689. verifyFormat("do {\n foo;\n foo();\n} while;");
  3690. }
  3691. TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
  3692. verifyFormat("namespace {\n"
  3693. "class Foo { Foo (\n"
  3694. "};\n"
  3695. "} // comment");
  3696. }
  3697. TEST_F(FormatTest, IncorrectCodeErrorDetection) {
  3698. EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
  3699. EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
  3700. EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
  3701. EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
  3702. EXPECT_EQ("{\n"
  3703. " {\n"
  3704. " breakme(\n"
  3705. " qwe);\n"
  3706. "}\n",
  3707. format("{\n"
  3708. " {\n"
  3709. " breakme(qwe);\n"
  3710. "}\n",
  3711. getLLVMStyleWithColumns(10)));
  3712. }
  3713. TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
  3714. verifyFormat("int x = {\n"
  3715. " avariable,\n"
  3716. " b(alongervariable)\n"
  3717. "};",
  3718. getLLVMStyleWithColumns(25));
  3719. }
  3720. TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
  3721. verifyFormat("return (a)(b) { 1, 2, 3 };");
  3722. }
  3723. TEST_F(FormatTest, LayoutCxx11ConstructorBraceInitializers) {
  3724. verifyFormat("vector<int> x{ 1, 2, 3, 4 };");
  3725. verifyFormat("vector<T> x{ {}, {}, {}, {} };");
  3726. verifyFormat("f({ 1, 2 });");
  3727. verifyFormat("auto v = Foo{ 1 };");
  3728. verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });");
  3729. verifyFormat("Class::Class : member{ 1, 2, 3 } {}");
  3730. verifyFormat("new vector<int>{ 1, 2, 3 };");
  3731. verifyFormat("new int[3]{ 1, 2, 3 };");
  3732. verifyFormat("return { arg1, arg2 };");
  3733. verifyFormat("return { arg1, SomeType{ parameter } };");
  3734. verifyFormat("new T{ arg1, arg2 };");
  3735. verifyFormat("class Class {\n"
  3736. " T member = { arg1, arg2 };\n"
  3737. "};");
  3738. verifyFormat(
  3739. "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3740. " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
  3741. " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  3742. " bbbbbbbbbbbbbbbbbbbb, bbbbb };");
  3743. verifyFormat("DoSomethingWithVector({} /* No data */);");
  3744. verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });");
  3745. verifyFormat(
  3746. "someFunction(OtherParam, BracedList{\n"
  3747. " // comment 1 (Forcing interesting break)\n"
  3748. " param1, param2,\n"
  3749. " // comment 2\n"
  3750. " param3, param4\n"
  3751. " });");
  3752. FormatStyle NoSpaces = getLLVMStyle();
  3753. NoSpaces.Cpp11BracedListStyle = true;
  3754. verifyFormat("vector<int> x{1, 2, 3, 4};", NoSpaces);
  3755. verifyFormat("vector<T> x{{}, {}, {}, {}};", NoSpaces);
  3756. verifyFormat("f({1, 2});", NoSpaces);
  3757. verifyFormat("auto v = Foo{-1};", NoSpaces);
  3758. verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});", NoSpaces);
  3759. verifyFormat("Class::Class : member{1, 2, 3} {}", NoSpaces);
  3760. verifyFormat("new vector<int>{1, 2, 3};", NoSpaces);
  3761. verifyFormat("new int[3]{1, 2, 3};", NoSpaces);
  3762. verifyFormat("return {arg1, arg2};", NoSpaces);
  3763. verifyFormat("return {arg1, SomeType{parameter}};", NoSpaces);
  3764. verifyFormat("new T{arg1, arg2};", NoSpaces);
  3765. verifyFormat("class Class {\n"
  3766. " T member = {arg1, arg2};\n"
  3767. "};",
  3768. NoSpaces);
  3769. }
  3770. TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
  3771. verifyFormat("void f() { return 42; }");
  3772. verifyFormat("void f() {\n"
  3773. " // Comment\n"
  3774. "}");
  3775. verifyFormat("{\n"
  3776. "#error {\n"
  3777. " int a;\n"
  3778. "}");
  3779. verifyFormat("{\n"
  3780. " int a;\n"
  3781. "#error {\n"
  3782. "}");
  3783. verifyFormat("void f() {} // comment");
  3784. verifyFormat("void f() { int a; } // comment");
  3785. verifyFormat("void f() {\n"
  3786. "} // comment",
  3787. getLLVMStyleWithColumns(15));
  3788. verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
  3789. verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
  3790. verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
  3791. verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
  3792. }
  3793. TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
  3794. // Elaborate type variable declarations.
  3795. verifyFormat("struct foo a = { bar };\nint n;");
  3796. verifyFormat("class foo a = { bar };\nint n;");
  3797. verifyFormat("union foo a = { bar };\nint n;");
  3798. // Elaborate types inside function definitions.
  3799. verifyFormat("struct foo f() {}\nint n;");
  3800. verifyFormat("class foo f() {}\nint n;");
  3801. verifyFormat("union foo f() {}\nint n;");
  3802. // Templates.
  3803. verifyFormat("template <class X> void f() {}\nint n;");
  3804. verifyFormat("template <struct X> void f() {}\nint n;");
  3805. verifyFormat("template <union X> void f() {}\nint n;");
  3806. // Actual definitions...
  3807. verifyFormat("struct {\n} n;");
  3808. verifyFormat(
  3809. "template <template <class T, class Y>, class Z> class X {\n} n;");
  3810. verifyFormat("union Z {\n int n;\n} x;");
  3811. verifyFormat("class MACRO Z {\n} n;");
  3812. verifyFormat("class MACRO(X) Z {\n} n;");
  3813. verifyFormat("class __attribute__(X) Z {\n} n;");
  3814. verifyFormat("class __declspec(X) Z {\n} n;");
  3815. verifyFormat("class A##B##C {\n} n;");
  3816. // Redefinition from nested context:
  3817. verifyFormat("class A::B::C {\n} n;");
  3818. // Template definitions.
  3819. verifyFormat(
  3820. "template <typename F>\n"
  3821. "Matcher(const Matcher<F> &Other,\n"
  3822. " typename enable_if_c<is_base_of<F, T>::value &&\n"
  3823. " !is_same<F, T>::value>::type * = 0)\n"
  3824. " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
  3825. // FIXME: This is still incorrectly handled at the formatter side.
  3826. verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
  3827. // FIXME:
  3828. // This now gets parsed incorrectly as class definition.
  3829. // verifyFormat("class A<int> f() {\n}\nint n;");
  3830. // Elaborate types where incorrectly parsing the structural element would
  3831. // break the indent.
  3832. verifyFormat("if (true)\n"
  3833. " class X x;\n"
  3834. "else\n"
  3835. " f();\n");
  3836. // This is simply incomplete. Formatting is not important, but must not crash.
  3837. verifyFormat("class A:");
  3838. }
  3839. TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
  3840. verifyFormat("#error Leave all white!!!!! space* alone!\n");
  3841. verifyFormat("#warning Leave all white!!!!! space* alone!\n");
  3842. EXPECT_EQ("#error 1", format(" # error 1"));
  3843. EXPECT_EQ("#warning 1", format(" # warning 1"));
  3844. }
  3845. TEST_F(FormatTest, FormatHashIfExpressions) {
  3846. // FIXME: Come up with a better indentation for #elif.
  3847. verifyFormat(
  3848. "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
  3849. " defined(BBBBBBBB)\n"
  3850. "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
  3851. " defined(BBBBBBBB)\n"
  3852. "#endif",
  3853. getLLVMStyleWithColumns(65));
  3854. }
  3855. TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
  3856. FormatStyle AllowsMergedIf = getGoogleStyle();
  3857. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  3858. verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
  3859. verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
  3860. verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
  3861. EXPECT_EQ("if (true) return 42;",
  3862. format("if (true)\nreturn 42;", AllowsMergedIf));
  3863. FormatStyle ShortMergedIf = AllowsMergedIf;
  3864. ShortMergedIf.ColumnLimit = 25;
  3865. verifyFormat("#define A \\\n"
  3866. " if (true) return 42;",
  3867. ShortMergedIf);
  3868. verifyFormat("#define A \\\n"
  3869. " f(); \\\n"
  3870. " if (true)\n"
  3871. "#define B",
  3872. ShortMergedIf);
  3873. verifyFormat("#define A \\\n"
  3874. " f(); \\\n"
  3875. " if (true)\n"
  3876. "g();",
  3877. ShortMergedIf);
  3878. verifyFormat("{\n"
  3879. "#ifdef A\n"
  3880. " // Comment\n"
  3881. " if (true) continue;\n"
  3882. "#endif\n"
  3883. " // Comment\n"
  3884. " if (true) continue;",
  3885. ShortMergedIf);
  3886. }
  3887. TEST_F(FormatTest, BlockCommentsInControlLoops) {
  3888. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  3889. " f();\n"
  3890. "}");
  3891. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  3892. " f();\n"
  3893. "} /* another comment */ else /* comment #3 */ {\n"
  3894. " g();\n"
  3895. "}");
  3896. verifyFormat("while (0) /* a comment in a strange place */ {\n"
  3897. " f();\n"
  3898. "}");
  3899. verifyFormat("for (;;) /* a comment in a strange place */ {\n"
  3900. " f();\n"
  3901. "}");
  3902. verifyFormat("do /* a comment in a strange place */ {\n"
  3903. " f();\n"
  3904. "} /* another comment */ while (0);");
  3905. }
  3906. TEST_F(FormatTest, BlockComments) {
  3907. EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
  3908. format("/* *//* */ /* */\n/* *//* */ /* */"));
  3909. EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
  3910. EXPECT_EQ("#define A /*123*/\\\n"
  3911. " b\n"
  3912. "/* */\n"
  3913. "someCall(\n"
  3914. " parameter);",
  3915. format("#define A /*123*/ b\n"
  3916. "/* */\n"
  3917. "someCall(parameter);",
  3918. getLLVMStyleWithColumns(15)));
  3919. EXPECT_EQ("#define A\n"
  3920. "/* */ someCall(\n"
  3921. " parameter);",
  3922. format("#define A\n"
  3923. "/* */someCall(parameter);",
  3924. getLLVMStyleWithColumns(15)));
  3925. EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
  3926. EXPECT_EQ("/*\n"
  3927. "*\n"
  3928. " * aaaaaa\n"
  3929. "*aaaaaa\n"
  3930. "*/",
  3931. format("/*\n"
  3932. "*\n"
  3933. " * aaaaaa aaaaaa\n"
  3934. "*/",
  3935. getLLVMStyleWithColumns(10)));
  3936. EXPECT_EQ("/*\n"
  3937. "**\n"
  3938. "* aaaaaa\n"
  3939. "*aaaaaa\n"
  3940. "*/",
  3941. format("/*\n"
  3942. "**\n"
  3943. "* aaaaaa aaaaaa\n"
  3944. "*/",
  3945. getLLVMStyleWithColumns(10)));
  3946. FormatStyle NoBinPacking = getLLVMStyle();
  3947. NoBinPacking.BinPackParameters = false;
  3948. EXPECT_EQ("someFunction(1, /* comment 1 */\n"
  3949. " 2, /* comment 2 */\n"
  3950. " 3, /* comment 3 */\n"
  3951. " aaaa,\n"
  3952. " bbbb);",
  3953. format("someFunction (1, /* comment 1 */\n"
  3954. " 2, /* comment 2 */ \n"
  3955. " 3, /* comment 3 */\n"
  3956. "aaaa, bbbb );",
  3957. NoBinPacking));
  3958. verifyFormat(
  3959. "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3960. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3961. EXPECT_EQ(
  3962. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  3963. " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3964. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
  3965. format(
  3966. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  3967. " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3968. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
  3969. EXPECT_EQ(
  3970. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  3971. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  3972. "int cccccccccccccccccccccccccccccc; /* comment */\n",
  3973. format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  3974. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  3975. "int cccccccccccccccccccccccccccccc; /* comment */\n"));
  3976. verifyFormat("void f(int * /* unused */) {}");
  3977. EXPECT_EQ("/*\n"
  3978. " **\n"
  3979. " */",
  3980. format("/*\n"
  3981. " **\n"
  3982. " */"));
  3983. EXPECT_EQ("/*\n"
  3984. " *q\n"
  3985. " */",
  3986. format("/*\n"
  3987. " *q\n"
  3988. " */"));
  3989. EXPECT_EQ("/*\n"
  3990. " * q\n"
  3991. " */",
  3992. format("/*\n"
  3993. " * q\n"
  3994. " */"));
  3995. EXPECT_EQ("/*\n"
  3996. " **/",
  3997. format("/*\n"
  3998. " **/"));
  3999. EXPECT_EQ("/*\n"
  4000. " ***/",
  4001. format("/*\n"
  4002. " ***/"));
  4003. }
  4004. TEST_F(FormatTest, BlockCommentsInMacros) {
  4005. EXPECT_EQ("#define A \\\n"
  4006. " { \\\n"
  4007. " /* one line */ \\\n"
  4008. " someCall();",
  4009. format("#define A { \\\n"
  4010. " /* one line */ \\\n"
  4011. " someCall();",
  4012. getLLVMStyleWithColumns(20)));
  4013. EXPECT_EQ("#define A \\\n"
  4014. " { \\\n"
  4015. " /* previous */ \\\n"
  4016. " /* one line */ \\\n"
  4017. " someCall();",
  4018. format("#define A { \\\n"
  4019. " /* previous */ \\\n"
  4020. " /* one line */ \\\n"
  4021. " someCall();",
  4022. getLLVMStyleWithColumns(20)));
  4023. }
  4024. TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
  4025. EXPECT_EQ("a = {\n"
  4026. " 1111 /* */\n"
  4027. "};",
  4028. format("a = {1111 /* */\n"
  4029. "};",
  4030. getLLVMStyleWithColumns(15)));
  4031. EXPECT_EQ("a = {\n"
  4032. " 1111 /* */\n"
  4033. "};",
  4034. format("a = {1111 /* */\n"
  4035. "};",
  4036. getLLVMStyleWithColumns(15)));
  4037. // FIXME: The formatting is still wrong here.
  4038. EXPECT_EQ("a = {\n"
  4039. " 1111 /* a\n"
  4040. " */\n"
  4041. "};",
  4042. format("a = {1111 /* a */\n"
  4043. "};",
  4044. getLLVMStyleWithColumns(15)));
  4045. }
  4046. TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
  4047. // FIXME: This is not what we want...
  4048. verifyFormat("{\n"
  4049. "// a"
  4050. "// b");
  4051. }
  4052. TEST_F(FormatTest, FormatStarDependingOnContext) {
  4053. verifyFormat("void f(int *a);");
  4054. verifyFormat("void f() { f(fint * b); }");
  4055. verifyFormat("class A {\n void f(int *a);\n};");
  4056. verifyFormat("class A {\n int *a;\n};");
  4057. verifyFormat("namespace a {\n"
  4058. "namespace b {\n"
  4059. "class A {\n"
  4060. " void f() {}\n"
  4061. " int *a;\n"
  4062. "};\n"
  4063. "}\n"
  4064. "}");
  4065. }
  4066. TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
  4067. verifyFormat("while");
  4068. verifyFormat("operator");
  4069. }
  4070. //===----------------------------------------------------------------------===//
  4071. // Objective-C tests.
  4072. //===----------------------------------------------------------------------===//
  4073. TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
  4074. verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
  4075. EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
  4076. format("-(NSUInteger)indexOfObject:(id)anObject;"));
  4077. EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
  4078. EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
  4079. EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
  4080. format("-(NSInteger)Method3:(id)anObject;"));
  4081. EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
  4082. format("-(NSInteger)Method4:(id)anObject;"));
  4083. EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
  4084. format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
  4085. EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
  4086. format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
  4087. EXPECT_EQ(
  4088. "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
  4089. format(
  4090. "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
  4091. // Very long objectiveC method declaration.
  4092. verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
  4093. " inRange:(NSRange)range\n"
  4094. " outRange:(NSRange)out_range\n"
  4095. " outRange1:(NSRange)out_range1\n"
  4096. " outRange2:(NSRange)out_range2\n"
  4097. " outRange3:(NSRange)out_range3\n"
  4098. " outRange4:(NSRange)out_range4\n"
  4099. " outRange5:(NSRange)out_range5\n"
  4100. " outRange6:(NSRange)out_range6\n"
  4101. " outRange7:(NSRange)out_range7\n"
  4102. " outRange8:(NSRange)out_range8\n"
  4103. " outRange9:(NSRange)out_range9;");
  4104. verifyFormat("- (int)sum:(vector<int>)numbers;");
  4105. verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
  4106. // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
  4107. // protocol lists (but not for template classes):
  4108. //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
  4109. verifyFormat("- (int (*)())foo:(int (*)())f;");
  4110. verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
  4111. // If there's no return type (very rare in practice!), LLVM and Google style
  4112. // agree.
  4113. verifyFormat("- foo;");
  4114. verifyFormat("- foo:(int)f;");
  4115. verifyGoogleFormat("- foo:(int)foo;");
  4116. }
  4117. TEST_F(FormatTest, FormatObjCBlocks) {
  4118. verifyFormat("int (^Block)(int, int);");
  4119. verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
  4120. }
  4121. TEST_F(FormatTest, FormatObjCInterface) {
  4122. verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
  4123. "@public\n"
  4124. " int field1;\n"
  4125. "@protected\n"
  4126. " int field2;\n"
  4127. "@private\n"
  4128. " int field3;\n"
  4129. "@package\n"
  4130. " int field4;\n"
  4131. "}\n"
  4132. "+ (id)init;\n"
  4133. "@end");
  4134. verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
  4135. " @public\n"
  4136. " int field1;\n"
  4137. " @protected\n"
  4138. " int field2;\n"
  4139. " @private\n"
  4140. " int field3;\n"
  4141. " @package\n"
  4142. " int field4;\n"
  4143. "}\n"
  4144. "+ (id)init;\n"
  4145. "@end");
  4146. verifyFormat("@interface /* wait for it */ Foo\n"
  4147. "+ (id)init;\n"
  4148. "// Look, a comment!\n"
  4149. "- (int)answerWith:(int)i;\n"
  4150. "@end");
  4151. verifyFormat("@interface Foo\n"
  4152. "@end\n"
  4153. "@interface Bar\n"
  4154. "@end");
  4155. verifyFormat("@interface Foo : Bar\n"
  4156. "+ (id)init;\n"
  4157. "@end");
  4158. verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
  4159. "+ (id)init;\n"
  4160. "@end");
  4161. verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
  4162. "+ (id)init;\n"
  4163. "@end");
  4164. verifyFormat("@interface Foo (HackStuff)\n"
  4165. "+ (id)init;\n"
  4166. "@end");
  4167. verifyFormat("@interface Foo ()\n"
  4168. "+ (id)init;\n"
  4169. "@end");
  4170. verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
  4171. "+ (id)init;\n"
  4172. "@end");
  4173. verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
  4174. "+ (id)init;\n"
  4175. "@end");
  4176. verifyFormat("@interface Foo {\n"
  4177. " int _i;\n"
  4178. "}\n"
  4179. "+ (id)init;\n"
  4180. "@end");
  4181. verifyFormat("@interface Foo : Bar {\n"
  4182. " int _i;\n"
  4183. "}\n"
  4184. "+ (id)init;\n"
  4185. "@end");
  4186. verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
  4187. " int _i;\n"
  4188. "}\n"
  4189. "+ (id)init;\n"
  4190. "@end");
  4191. verifyFormat("@interface Foo (HackStuff) {\n"
  4192. " int _i;\n"
  4193. "}\n"
  4194. "+ (id)init;\n"
  4195. "@end");
  4196. verifyFormat("@interface Foo () {\n"
  4197. " int _i;\n"
  4198. "}\n"
  4199. "+ (id)init;\n"
  4200. "@end");
  4201. verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
  4202. " int _i;\n"
  4203. "}\n"
  4204. "+ (id)init;\n"
  4205. "@end");
  4206. }
  4207. TEST_F(FormatTest, FormatObjCImplementation) {
  4208. verifyFormat("@implementation Foo : NSObject {\n"
  4209. "@public\n"
  4210. " int field1;\n"
  4211. "@protected\n"
  4212. " int field2;\n"
  4213. "@private\n"
  4214. " int field3;\n"
  4215. "@package\n"
  4216. " int field4;\n"
  4217. "}\n"
  4218. "+ (id)init {\n}\n"
  4219. "@end");
  4220. verifyGoogleFormat("@implementation Foo : NSObject {\n"
  4221. " @public\n"
  4222. " int field1;\n"
  4223. " @protected\n"
  4224. " int field2;\n"
  4225. " @private\n"
  4226. " int field3;\n"
  4227. " @package\n"
  4228. " int field4;\n"
  4229. "}\n"
  4230. "+ (id)init {\n}\n"
  4231. "@end");
  4232. verifyFormat("@implementation Foo\n"
  4233. "+ (id)init {\n"
  4234. " if (true)\n"
  4235. " return nil;\n"
  4236. "}\n"
  4237. "// Look, a comment!\n"
  4238. "- (int)answerWith:(int)i {\n"
  4239. " return i;\n"
  4240. "}\n"
  4241. "+ (int)answerWith:(int)i {\n"
  4242. " return i;\n"
  4243. "}\n"
  4244. "@end");
  4245. verifyFormat("@implementation Foo\n"
  4246. "@end\n"
  4247. "@implementation Bar\n"
  4248. "@end");
  4249. verifyFormat("@implementation Foo : Bar\n"
  4250. "+ (id)init {\n}\n"
  4251. "- (void)foo {\n}\n"
  4252. "@end");
  4253. verifyFormat("@implementation Foo {\n"
  4254. " int _i;\n"
  4255. "}\n"
  4256. "+ (id)init {\n}\n"
  4257. "@end");
  4258. verifyFormat("@implementation Foo : Bar {\n"
  4259. " int _i;\n"
  4260. "}\n"
  4261. "+ (id)init {\n}\n"
  4262. "@end");
  4263. verifyFormat("@implementation Foo (HackStuff)\n"
  4264. "+ (id)init {\n}\n"
  4265. "@end");
  4266. }
  4267. TEST_F(FormatTest, FormatObjCProtocol) {
  4268. verifyFormat("@protocol Foo\n"
  4269. "@property(weak) id delegate;\n"
  4270. "- (NSUInteger)numberOfThings;\n"
  4271. "@end");
  4272. verifyFormat("@protocol MyProtocol <NSObject>\n"
  4273. "- (NSUInteger)numberOfThings;\n"
  4274. "@end");
  4275. verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
  4276. "- (NSUInteger)numberOfThings;\n"
  4277. "@end");
  4278. verifyFormat("@protocol Foo;\n"
  4279. "@protocol Bar;\n");
  4280. verifyFormat("@protocol Foo\n"
  4281. "@end\n"
  4282. "@protocol Bar\n"
  4283. "@end");
  4284. verifyFormat("@protocol myProtocol\n"
  4285. "- (void)mandatoryWithInt:(int)i;\n"
  4286. "@optional\n"
  4287. "- (void)optional;\n"
  4288. "@required\n"
  4289. "- (void)required;\n"
  4290. "@optional\n"
  4291. "@property(assign) int madProp;\n"
  4292. "@end\n");
  4293. verifyFormat("@property(nonatomic, assign, readonly)\n"
  4294. " int *looooooooooooooooooooooooooooongNumber;\n"
  4295. "@property(nonatomic, assign, readonly)\n"
  4296. " NSString *looooooooooooooooooooooooooooongName;");
  4297. }
  4298. TEST_F(FormatTest, FormatObjCMethodDeclarations) {
  4299. verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
  4300. " rect:(NSRect)theRect\n"
  4301. " interval:(float)theInterval {\n"
  4302. "}");
  4303. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  4304. " longKeyword:(NSRect)theRect\n"
  4305. " evenLongerKeyword:(float)theInterval\n"
  4306. " error:(NSError **)theError {\n"
  4307. "}");
  4308. }
  4309. TEST_F(FormatTest, FormatObjCMethodExpr) {
  4310. verifyFormat("[foo bar:baz];");
  4311. verifyFormat("return [foo bar:baz];");
  4312. verifyFormat("f([foo bar:baz]);");
  4313. verifyFormat("f(2, [foo bar:baz]);");
  4314. verifyFormat("f(2, a ? b : c);");
  4315. verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
  4316. // Unary operators.
  4317. verifyFormat("int a = +[foo bar:baz];");
  4318. verifyFormat("int a = -[foo bar:baz];");
  4319. verifyFormat("int a = ![foo bar:baz];");
  4320. verifyFormat("int a = ~[foo bar:baz];");
  4321. verifyFormat("int a = ++[foo bar:baz];");
  4322. verifyFormat("int a = --[foo bar:baz];");
  4323. verifyFormat("int a = sizeof [foo bar:baz];");
  4324. verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
  4325. verifyFormat("int a = &[foo bar:baz];");
  4326. verifyFormat("int a = *[foo bar:baz];");
  4327. // FIXME: Make casts work, without breaking f()[4].
  4328. //verifyFormat("int a = (int)[foo bar:baz];");
  4329. //verifyFormat("return (int)[foo bar:baz];");
  4330. //verifyFormat("(void)[foo bar:baz];");
  4331. verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
  4332. // Binary operators.
  4333. verifyFormat("[foo bar:baz], [foo bar:baz];");
  4334. verifyFormat("[foo bar:baz] = [foo bar:baz];");
  4335. verifyFormat("[foo bar:baz] *= [foo bar:baz];");
  4336. verifyFormat("[foo bar:baz] /= [foo bar:baz];");
  4337. verifyFormat("[foo bar:baz] %= [foo bar:baz];");
  4338. verifyFormat("[foo bar:baz] += [foo bar:baz];");
  4339. verifyFormat("[foo bar:baz] -= [foo bar:baz];");
  4340. verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
  4341. verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
  4342. verifyFormat("[foo bar:baz] &= [foo bar:baz];");
  4343. verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
  4344. verifyFormat("[foo bar:baz] |= [foo bar:baz];");
  4345. verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
  4346. verifyFormat("[foo bar:baz] || [foo bar:baz];");
  4347. verifyFormat("[foo bar:baz] && [foo bar:baz];");
  4348. verifyFormat("[foo bar:baz] | [foo bar:baz];");
  4349. verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
  4350. verifyFormat("[foo bar:baz] & [foo bar:baz];");
  4351. verifyFormat("[foo bar:baz] == [foo bar:baz];");
  4352. verifyFormat("[foo bar:baz] != [foo bar:baz];");
  4353. verifyFormat("[foo bar:baz] >= [foo bar:baz];");
  4354. verifyFormat("[foo bar:baz] <= [foo bar:baz];");
  4355. verifyFormat("[foo bar:baz] > [foo bar:baz];");
  4356. verifyFormat("[foo bar:baz] < [foo bar:baz];");
  4357. verifyFormat("[foo bar:baz] >> [foo bar:baz];");
  4358. verifyFormat("[foo bar:baz] << [foo bar:baz];");
  4359. verifyFormat("[foo bar:baz] - [foo bar:baz];");
  4360. verifyFormat("[foo bar:baz] + [foo bar:baz];");
  4361. verifyFormat("[foo bar:baz] * [foo bar:baz];");
  4362. verifyFormat("[foo bar:baz] / [foo bar:baz];");
  4363. verifyFormat("[foo bar:baz] % [foo bar:baz];");
  4364. // Whew!
  4365. verifyFormat("return in[42];");
  4366. verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
  4367. "}");
  4368. verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
  4369. verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
  4370. verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
  4371. verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
  4372. verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
  4373. verifyFormat("[button setAction:@selector(zoomOut:)];");
  4374. verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
  4375. verifyFormat("arr[[self indexForFoo:a]];");
  4376. verifyFormat("throw [self errorFor:a];");
  4377. verifyFormat("@throw [self errorFor:a];");
  4378. verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
  4379. verifyFormat("[(id)foo bar:(id) ? baz : quux];");
  4380. verifyFormat("4 > 4 ? (id)a : (id)baz;");
  4381. // This tests that the formatter doesn't break after "backing" but before ":",
  4382. // which would be at 80 columns.
  4383. verifyFormat(
  4384. "void f() {\n"
  4385. " if ((self = [super initWithContentRect:contentRect\n"
  4386. " styleMask:styleMask ?: otherMask\n"
  4387. " backing:NSBackingStoreBuffered\n"
  4388. " defer:YES]))");
  4389. verifyFormat(
  4390. "[foo checkThatBreakingAfterColonWorksOk:\n"
  4391. " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
  4392. verifyFormat("[myObj short:arg1 // Force line break\n"
  4393. " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
  4394. " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
  4395. " error:arg4];");
  4396. verifyFormat(
  4397. "void f() {\n"
  4398. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  4399. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  4400. " pos.width(), pos.height())\n"
  4401. " styleMask:NSBorderlessWindowMask\n"
  4402. " backing:NSBackingStoreBuffered\n"
  4403. " defer:NO]);\n"
  4404. "}");
  4405. verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
  4406. " with:contentsNativeView];");
  4407. verifyFormat(
  4408. "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
  4409. " owner:nillllll];");
  4410. verifyFormat(
  4411. "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
  4412. " forType:kBookmarkButtonDragType];");
  4413. verifyFormat("[defaultCenter addObserver:self\n"
  4414. " selector:@selector(willEnterFullscreen)\n"
  4415. " name:kWillEnterFullscreenNotification\n"
  4416. " object:nil];");
  4417. verifyFormat("[image_rep drawInRect:drawRect\n"
  4418. " fromRect:NSZeroRect\n"
  4419. " operation:NSCompositeCopy\n"
  4420. " fraction:1.0\n"
  4421. " respectFlipped:NO\n"
  4422. " hints:nil];");
  4423. verifyFormat(
  4424. "scoped_nsobject<NSTextField> message(\n"
  4425. " // The frame will be fixed up when |-setMessageText:| is called.\n"
  4426. " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
  4427. verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
  4428. " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
  4429. " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
  4430. " aaaa:bbb];");
  4431. }
  4432. TEST_F(FormatTest, ObjCAt) {
  4433. verifyFormat("@autoreleasepool");
  4434. verifyFormat("@catch");
  4435. verifyFormat("@class");
  4436. verifyFormat("@compatibility_alias");
  4437. verifyFormat("@defs");
  4438. verifyFormat("@dynamic");
  4439. verifyFormat("@encode");
  4440. verifyFormat("@end");
  4441. verifyFormat("@finally");
  4442. verifyFormat("@implementation");
  4443. verifyFormat("@import");
  4444. verifyFormat("@interface");
  4445. verifyFormat("@optional");
  4446. verifyFormat("@package");
  4447. verifyFormat("@private");
  4448. verifyFormat("@property");
  4449. verifyFormat("@protected");
  4450. verifyFormat("@protocol");
  4451. verifyFormat("@public");
  4452. verifyFormat("@required");
  4453. verifyFormat("@selector");
  4454. verifyFormat("@synchronized");
  4455. verifyFormat("@synthesize");
  4456. verifyFormat("@throw");
  4457. verifyFormat("@try");
  4458. EXPECT_EQ("@interface", format("@ interface"));
  4459. // The precise formatting of this doesn't matter, nobody writes code like
  4460. // this.
  4461. verifyFormat("@ /*foo*/ interface");
  4462. }
  4463. TEST_F(FormatTest, ObjCSnippets) {
  4464. verifyFormat("@autoreleasepool {\n"
  4465. " foo();\n"
  4466. "}");
  4467. verifyFormat("@class Foo, Bar;");
  4468. verifyFormat("@compatibility_alias AliasName ExistingClass;");
  4469. verifyFormat("@dynamic textColor;");
  4470. verifyFormat("char *buf1 = @encode(int *);");
  4471. verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
  4472. verifyFormat("char *buf1 = @encode(int **);");
  4473. verifyFormat("Protocol *proto = @protocol(p1);");
  4474. verifyFormat("SEL s = @selector(foo:);");
  4475. verifyFormat("@synchronized(self) {\n"
  4476. " f();\n"
  4477. "}");
  4478. verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  4479. verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  4480. verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
  4481. verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
  4482. verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
  4483. verifyFormat("@import foo.bar;\n"
  4484. "@import baz;");
  4485. }
  4486. TEST_F(FormatTest, ObjCLiterals) {
  4487. verifyFormat("@\"String\"");
  4488. verifyFormat("@1");
  4489. verifyFormat("@+4.8");
  4490. verifyFormat("@-4");
  4491. verifyFormat("@1LL");
  4492. verifyFormat("@.5");
  4493. verifyFormat("@'c'");
  4494. verifyFormat("@true");
  4495. verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
  4496. verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
  4497. verifyFormat("NSNumber *favoriteColor = @(Green);");
  4498. verifyFormat("NSString *path = @(getenv(\"PATH\"));");
  4499. verifyFormat("@[");
  4500. verifyFormat("@[]");
  4501. verifyFormat(
  4502. "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
  4503. verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
  4504. verifyFormat("@{");
  4505. verifyFormat("@{}");
  4506. verifyFormat("@{ @\"one\" : @1 }");
  4507. verifyFormat("return @{ @\"one\" : @1 };");
  4508. verifyFormat("@{ @\"one\" : @1, }");
  4509. verifyFormat("@{ @\"one\" : @{ @2 : @1 } }");
  4510. verifyFormat("@{ @\"one\" : @{ @2 : @1 }, }");
  4511. verifyFormat("@{ 1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2 }");
  4512. verifyFormat("[self setDict:@{}");
  4513. verifyFormat("[self setDict:@{ @1 : @2 }");
  4514. verifyFormat("NSLog(@\"%@\", @{ @1 : @2, @2 : @3 }[@1]);");
  4515. verifyFormat(
  4516. "NSDictionary *masses = @{ @\"H\" : @1.0078, @\"He\" : @4.0026 };");
  4517. verifyFormat(
  4518. "NSDictionary *settings = @{ AVEncoderKey : @(AVAudioQualityMax) };");
  4519. // FIXME: Nested and multi-line array and dictionary literals need more work.
  4520. verifyFormat(
  4521. "NSDictionary *d = @{ @\"nam\" : NSUserNam(), @\"dte\" : [NSDate date],\n"
  4522. " @\"processInfo\" : [NSProcessInfo processInfo] };");
  4523. verifyFormat(
  4524. "@{ NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
  4525. " regularFont, };");
  4526. }
  4527. TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
  4528. EXPECT_EQ("{\n"
  4529. "{\n"
  4530. "a;\n"
  4531. "b;\n"
  4532. "}\n"
  4533. "}",
  4534. format("{\n"
  4535. "{\n"
  4536. "a;\n"
  4537. " b;\n"
  4538. "}\n"
  4539. "}",
  4540. 13, 2, getLLVMStyle()));
  4541. EXPECT_EQ("{\n"
  4542. "{\n"
  4543. " a;\n"
  4544. "b;\n"
  4545. "}\n"
  4546. "}",
  4547. format("{\n"
  4548. "{\n"
  4549. " a;\n"
  4550. "b;\n"
  4551. "}\n"
  4552. "}",
  4553. 9, 2, getLLVMStyle()));
  4554. EXPECT_EQ("{\n"
  4555. "{\n"
  4556. "public:\n"
  4557. " b;\n"
  4558. "}\n"
  4559. "}",
  4560. format("{\n"
  4561. "{\n"
  4562. "public:\n"
  4563. " b;\n"
  4564. "}\n"
  4565. "}",
  4566. 17, 2, getLLVMStyle()));
  4567. EXPECT_EQ("{\n"
  4568. "{\n"
  4569. "a;\n"
  4570. "}\n"
  4571. "{\n"
  4572. " b; //\n"
  4573. "}\n"
  4574. "}",
  4575. format("{\n"
  4576. "{\n"
  4577. "a;\n"
  4578. "}\n"
  4579. "{\n"
  4580. " b; //\n"
  4581. "}\n"
  4582. "}",
  4583. 22, 2, getLLVMStyle()));
  4584. EXPECT_EQ(" {\n"
  4585. " a; //\n"
  4586. " }",
  4587. format(" {\n"
  4588. "a; //\n"
  4589. " }",
  4590. 4, 2, getLLVMStyle()));
  4591. EXPECT_EQ("void f() {}\n"
  4592. "void g() {}",
  4593. format("void f() {}\n"
  4594. "void g() {}",
  4595. 13, 0, getLLVMStyle()));
  4596. EXPECT_EQ("int a; // comment\n"
  4597. " // line 2\n"
  4598. "int b;",
  4599. format("int a; // comment\n"
  4600. " // line 2\n"
  4601. " int b;",
  4602. 35, 0, getLLVMStyle()));
  4603. EXPECT_EQ(" int a;\n"
  4604. " void\n"
  4605. " ffffff() {\n"
  4606. " }",
  4607. format(" int a;\n"
  4608. "void ffffff() {}",
  4609. 11, 0, getLLVMStyleWithColumns(11)));
  4610. }
  4611. TEST_F(FormatTest, BreakStringLiterals) {
  4612. EXPECT_EQ("\"some text \"\n"
  4613. "\"other\";",
  4614. format("\"some text other\";", getLLVMStyleWithColumns(12)));
  4615. EXPECT_EQ("\"some text \"\n"
  4616. "\"other\";",
  4617. format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
  4618. EXPECT_EQ(
  4619. "#define A \\\n"
  4620. " \"some \" \\\n"
  4621. " \"text \" \\\n"
  4622. " \"other\";",
  4623. format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
  4624. EXPECT_EQ(
  4625. "#define A \\\n"
  4626. " \"so \" \\\n"
  4627. " \"text \" \\\n"
  4628. " \"other\";",
  4629. format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
  4630. EXPECT_EQ("\"some text\"",
  4631. format("\"some text\"", getLLVMStyleWithColumns(1)));
  4632. EXPECT_EQ("\"some text\"",
  4633. format("\"some text\"", getLLVMStyleWithColumns(11)));
  4634. EXPECT_EQ("\"some \"\n"
  4635. "\"text\"",
  4636. format("\"some text\"", getLLVMStyleWithColumns(10)));
  4637. EXPECT_EQ("\"some \"\n"
  4638. "\"text\"",
  4639. format("\"some text\"", getLLVMStyleWithColumns(7)));
  4640. EXPECT_EQ("\"some\"\n"
  4641. "\" tex\"\n"
  4642. "\"t\"",
  4643. format("\"some text\"", getLLVMStyleWithColumns(6)));
  4644. EXPECT_EQ("\"some\"\n"
  4645. "\" tex\"\n"
  4646. "\" and\"",
  4647. format("\"some tex and\"", getLLVMStyleWithColumns(6)));
  4648. EXPECT_EQ("\"some\"\n"
  4649. "\"/tex\"\n"
  4650. "\"/and\"",
  4651. format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
  4652. EXPECT_EQ("variable =\n"
  4653. " \"long string \"\n"
  4654. " \"literal\";",
  4655. format("variable = \"long string literal\";",
  4656. getLLVMStyleWithColumns(20)));
  4657. EXPECT_EQ("variable = f(\n"
  4658. " \"long string \"\n"
  4659. " \"literal\",\n"
  4660. " short,\n"
  4661. " loooooooooooooooooooong);",
  4662. format("variable = f(\"long string literal\", short, "
  4663. "loooooooooooooooooooong);",
  4664. getLLVMStyleWithColumns(20)));
  4665. EXPECT_EQ("f(g(\"long string \"\n"
  4666. " \"literal\"),\n"
  4667. " b);",
  4668. format("f(g(\"long string literal\"), b);",
  4669. getLLVMStyleWithColumns(20)));
  4670. EXPECT_EQ("f(g(\"long string \"\n"
  4671. " \"literal\",\n"
  4672. " a),\n"
  4673. " b);",
  4674. format("f(g(\"long string literal\", a), b);",
  4675. getLLVMStyleWithColumns(20)));
  4676. EXPECT_EQ(
  4677. "f(\"one two\".split(\n"
  4678. " variable));",
  4679. format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
  4680. EXPECT_EQ("f(\"one two three four five six \"\n"
  4681. " \"seven\".split(\n"
  4682. " really_looooong_variable));",
  4683. format("f(\"one two three four five six seven\"."
  4684. "split(really_looooong_variable));",
  4685. getLLVMStyleWithColumns(33)));
  4686. EXPECT_EQ("f(\"some \"\n"
  4687. " \"text\",\n"
  4688. " other);",
  4689. format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
  4690. // Only break as a last resort.
  4691. verifyFormat(
  4692. "aaaaaaaaaaaaaaaaaaaa(\n"
  4693. " aaaaaaaaaaaaaaaaaaaa,\n"
  4694. " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
  4695. EXPECT_EQ(
  4696. "\"splitmea\"\n"
  4697. "\"trandomp\"\n"
  4698. "\"oint\"",
  4699. format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
  4700. EXPECT_EQ(
  4701. "\"split/\"\n"
  4702. "\"pathat/\"\n"
  4703. "\"slashes\"",
  4704. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  4705. EXPECT_EQ(
  4706. "\"split/\"\n"
  4707. "\"pathat/\"\n"
  4708. "\"slashes\"",
  4709. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  4710. EXPECT_EQ("\"split at \"\n"
  4711. "\"spaces/at/\"\n"
  4712. "\"slashes.at.any$\"\n"
  4713. "\"non-alphanumeric%\"\n"
  4714. "\"1111111111characte\"\n"
  4715. "\"rs\"",
  4716. format("\"split at "
  4717. "spaces/at/"
  4718. "slashes.at."
  4719. "any$non-"
  4720. "alphanumeric%"
  4721. "1111111111characte"
  4722. "rs\"",
  4723. getLLVMStyleWithColumns(20)));
  4724. // Verify that splitting the strings understands
  4725. // Style::AlwaysBreakBeforeMultilineStrings.
  4726. EXPECT_EQ("aaaaaaaaaaaa(aaaaaaaaaaaaa,\n"
  4727. " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
  4728. " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
  4729. format("aaaaaaaaaaaa(aaaaaaaaaaaaa, \"aaaaaaaaaaaaaaaaaaaaaa "
  4730. "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa "
  4731. "aaaaaaaaaaaaaaaaaaaaaa\");",
  4732. getGoogleStyle()));
  4733. EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  4734. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  4735. format("llvm::outs() << "
  4736. "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
  4737. "aaaaaaaaaaaaaaaaaaa\";"));
  4738. FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
  4739. AlignLeft.AlignEscapedNewlinesLeft = true;
  4740. EXPECT_EQ(
  4741. "#define A \\\n"
  4742. " \"some \" \\\n"
  4743. " \"text \" \\\n"
  4744. " \"other\";",
  4745. format("#define A \"some text other\";", AlignLeft));
  4746. }
  4747. TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
  4748. EXPECT_EQ("aaaaaaaaaaa =\n"
  4749. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  4750. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  4751. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  4752. format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  4753. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  4754. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
  4755. }
  4756. TEST_F(FormatTest, SkipsUnknownStringLiterals) {
  4757. EXPECT_EQ(
  4758. "u8\"unsupported literal\";",
  4759. format("u8\"unsupported literal\";", getGoogleStyleWithColumns(15)));
  4760. EXPECT_EQ("u\"unsupported literal\";",
  4761. format("u\"unsupported literal\";", getGoogleStyleWithColumns(15)));
  4762. EXPECT_EQ("U\"unsupported literal\";",
  4763. format("U\"unsupported literal\";", getGoogleStyleWithColumns(15)));
  4764. EXPECT_EQ("L\"unsupported literal\";",
  4765. format("L\"unsupported literal\";", getGoogleStyleWithColumns(15)));
  4766. EXPECT_EQ("R\"x(raw literal)x\";",
  4767. format("R\"x(raw literal)x\";", getGoogleStyleWithColumns(15)));
  4768. verifyFormat("string a = \"unterminated;");
  4769. EXPECT_EQ("function(\"unterminated,\n"
  4770. " OtherParameter);",
  4771. format("function( \"unterminated,\n"
  4772. " OtherParameter);"));
  4773. }
  4774. TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
  4775. EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
  4776. format("#define x(_a) printf(\"foo\"_a);", getLLVMStyle()));
  4777. }
  4778. TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
  4779. EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
  4780. " \"ddeeefff\");",
  4781. format("someFunction(\"aaabbbcccdddeeefff\");",
  4782. getLLVMStyleWithColumns(25)));
  4783. EXPECT_EQ("someFunction1234567890(\n"
  4784. " \"aaabbbcccdddeeefff\");",
  4785. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  4786. getLLVMStyleWithColumns(26)));
  4787. EXPECT_EQ("someFunction1234567890(\n"
  4788. " \"aaabbbcccdddeeeff\"\n"
  4789. " \"f\");",
  4790. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  4791. getLLVMStyleWithColumns(25)));
  4792. EXPECT_EQ("someFunction1234567890(\n"
  4793. " \"aaabbbcccdddeeeff\"\n"
  4794. " \"f\");",
  4795. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  4796. getLLVMStyleWithColumns(24)));
  4797. EXPECT_EQ("someFunction(\n"
  4798. " \"aaabbbcc \"\n"
  4799. " \"dddeeefff\");",
  4800. format("someFunction(\"aaabbbcc dddeeefff\");",
  4801. getLLVMStyleWithColumns(25)));
  4802. EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
  4803. " \"ddeeefff\");",
  4804. format("someFunction(\"aaabbbccc ddeeefff\");",
  4805. getLLVMStyleWithColumns(25)));
  4806. EXPECT_EQ("someFunction1234567890(\n"
  4807. " \"aaabb \"\n"
  4808. " \"cccdddeeefff\");",
  4809. format("someFunction1234567890(\"aaabb cccdddeeefff\");",
  4810. getLLVMStyleWithColumns(25)));
  4811. EXPECT_EQ("#define A \\\n"
  4812. " string s = \\\n"
  4813. " \"123456789\" \\\n"
  4814. " \"0\"; \\\n"
  4815. " int i;",
  4816. format("#define A string s = \"1234567890\"; int i;",
  4817. getLLVMStyleWithColumns(20)));
  4818. }
  4819. TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
  4820. EXPECT_EQ("\"\\a\"",
  4821. format("\"\\a\"", getLLVMStyleWithColumns(3)));
  4822. EXPECT_EQ("\"\\\"",
  4823. format("\"\\\"", getLLVMStyleWithColumns(2)));
  4824. EXPECT_EQ("\"test\"\n"
  4825. "\"\\n\"",
  4826. format("\"test\\n\"", getLLVMStyleWithColumns(7)));
  4827. EXPECT_EQ("\"tes\\\\\"\n"
  4828. "\"n\"",
  4829. format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
  4830. EXPECT_EQ("\"\\\\\\\\\"\n"
  4831. "\"\\n\"",
  4832. format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
  4833. EXPECT_EQ("\"\\uff01\"",
  4834. format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
  4835. EXPECT_EQ("\"\\uff01\"\n"
  4836. "\"test\"",
  4837. format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
  4838. EXPECT_EQ("\"\\Uff01ff02\"",
  4839. format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
  4840. EXPECT_EQ("\"\\x000000000001\"\n"
  4841. "\"next\"",
  4842. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
  4843. EXPECT_EQ("\"\\x000000000001next\"",
  4844. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
  4845. EXPECT_EQ("\"\\x000000000001\"",
  4846. format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
  4847. EXPECT_EQ("\"test\"\n"
  4848. "\"\\000000\"\n"
  4849. "\"000001\"",
  4850. format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
  4851. EXPECT_EQ("\"test\\000\"\n"
  4852. "\"00000000\"\n"
  4853. "\"1\"",
  4854. format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
  4855. EXPECT_EQ("R\"(\\x\\x00)\"\n",
  4856. format("R\"(\\x\\x00)\"\n", getGoogleStyleWithColumns(7)));
  4857. }
  4858. TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
  4859. verifyFormat("void f() {\n"
  4860. " return g() {}\n"
  4861. " void h() {}");
  4862. verifyFormat("if (foo)\n"
  4863. " return { forgot_closing_brace();\n"
  4864. "test();");
  4865. verifyFormat("int a[] = { void forgot_closing_brace() { f();\n"
  4866. "g();\n"
  4867. "}");
  4868. }
  4869. TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
  4870. verifyFormat("class X {\n"
  4871. " void f() {\n"
  4872. " }\n"
  4873. "};",
  4874. getLLVMStyleWithColumns(12));
  4875. }
  4876. TEST_F(FormatTest, ConfigurableIndentWidth) {
  4877. FormatStyle EightIndent = getLLVMStyleWithColumns(18);
  4878. EightIndent.IndentWidth = 8;
  4879. verifyFormat("void f() {\n"
  4880. " someFunction();\n"
  4881. " if (true) {\n"
  4882. " f();\n"
  4883. " }\n"
  4884. "}",
  4885. EightIndent);
  4886. verifyFormat("class X {\n"
  4887. " void f() {\n"
  4888. " }\n"
  4889. "};",
  4890. EightIndent);
  4891. verifyFormat("int x[] = {\n"
  4892. " call(),\n"
  4893. " call(),\n"
  4894. "};",
  4895. EightIndent);
  4896. }
  4897. TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
  4898. verifyFormat("void\n"
  4899. "f();",
  4900. getLLVMStyleWithColumns(8));
  4901. }
  4902. TEST_F(FormatTest, ConfigurableUseOfTab) {
  4903. FormatStyle Tab = getLLVMStyleWithColumns(42);
  4904. Tab.IndentWidth = 8;
  4905. Tab.UseTab = true;
  4906. Tab.AlignEscapedNewlinesLeft = true;
  4907. verifyFormat("class X {\n"
  4908. "\tvoid f() {\n"
  4909. "\t\tsomeFunction(parameter1,\n"
  4910. "\t\t\t parameter2);\n"
  4911. "\t}\n"
  4912. "};",
  4913. Tab);
  4914. verifyFormat("#define A \\\n"
  4915. "\tvoid f() { \\\n"
  4916. "\t\tsomeFunction( \\\n"
  4917. "\t\t parameter1, \\\n"
  4918. "\t\t parameter2); \\\n"
  4919. "\t}",
  4920. Tab);
  4921. // FIXME: To correctly count mixed whitespace we need to
  4922. // also correctly count mixed whitespace in front of the comment.
  4923. //
  4924. // EXPECT_EQ("/*\n"
  4925. // "\t a\t\tcomment\n"
  4926. // "\t in multiple lines\n"
  4927. // " */",
  4928. // format(" /*\t \t \n"
  4929. // " \t \t a\t\tcomment\t \t\n"
  4930. // " \t \t in multiple lines\t\n"
  4931. // " \t */",
  4932. // Tab));
  4933. // Tab.UseTab = false;
  4934. // EXPECT_EQ("/*\n"
  4935. // " a\t\tcomment\n"
  4936. // " in multiple lines\n"
  4937. // " */",
  4938. // format(" /*\t \t \n"
  4939. // " \t \t a\t\tcomment\t \t\n"
  4940. // " \t \t in multiple lines\t\n"
  4941. // " \t */",
  4942. // Tab));
  4943. // EXPECT_EQ("/* some\n"
  4944. // " comment */",
  4945. // format(" \t \t /* some\n"
  4946. // " \t \t comment */",
  4947. // Tab));
  4948. EXPECT_EQ("{\n"
  4949. " /*\n"
  4950. " * Comment\n"
  4951. " */\n"
  4952. " int i;\n"
  4953. "}",
  4954. format("{\n"
  4955. "\t/*\n"
  4956. "\t * Comment\n"
  4957. "\t */\n"
  4958. "\t int i;\n"
  4959. "}"));
  4960. }
  4961. TEST_F(FormatTest, LinuxBraceBreaking) {
  4962. FormatStyle BreakBeforeBrace = getLLVMStyle();
  4963. BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Linux;
  4964. verifyFormat("namespace a\n"
  4965. "{\n"
  4966. "class A\n"
  4967. "{\n"
  4968. " void f()\n"
  4969. " {\n"
  4970. " if (true) {\n"
  4971. " a();\n"
  4972. " b();\n"
  4973. " }\n"
  4974. " }\n"
  4975. " void g()\n"
  4976. " {\n"
  4977. " return;\n"
  4978. " }\n"
  4979. "}\n"
  4980. "}",
  4981. BreakBeforeBrace);
  4982. }
  4983. TEST_F(FormatTest, StroustrupBraceBreaking) {
  4984. FormatStyle BreakBeforeBrace = getLLVMStyle();
  4985. BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  4986. verifyFormat("namespace a {\n"
  4987. "class A {\n"
  4988. " void f()\n"
  4989. " {\n"
  4990. " if (true) {\n"
  4991. " a();\n"
  4992. " b();\n"
  4993. " }\n"
  4994. " }\n"
  4995. " void g()\n"
  4996. " {\n"
  4997. " return;\n"
  4998. " }\n"
  4999. "}\n"
  5000. "}",
  5001. BreakBeforeBrace);
  5002. }
  5003. TEST_F(FormatTest, AllmanBraceBreaking) {
  5004. FormatStyle BreakBeforeBrace = getLLVMStyle();
  5005. BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Allman;
  5006. verifyFormat("namespace a\n"
  5007. "{\n"
  5008. "class A\n"
  5009. "{\n"
  5010. " void f()\n"
  5011. " {\n"
  5012. " if (true)\n"
  5013. " {\n"
  5014. " a();\n"
  5015. " b();\n"
  5016. " }\n"
  5017. " }\n"
  5018. " void g()\n"
  5019. " {\n"
  5020. " return;\n"
  5021. " }\n"
  5022. "}\n"
  5023. "}",
  5024. BreakBeforeBrace);
  5025. verifyFormat("void f()\n"
  5026. "{\n"
  5027. " if (true)\n"
  5028. " {\n"
  5029. " a();\n"
  5030. " }\n"
  5031. " else if (false)\n"
  5032. " {\n"
  5033. " b();\n"
  5034. " }\n"
  5035. " else\n"
  5036. " {\n"
  5037. " c();\n"
  5038. " }\n"
  5039. "}\n",
  5040. BreakBeforeBrace);
  5041. verifyFormat("void f()\n"
  5042. "{\n"
  5043. " for (int i = 0; i < 10; ++i)\n"
  5044. " {\n"
  5045. " a();\n"
  5046. " }\n"
  5047. " while (false)\n"
  5048. " {\n"
  5049. " b();\n"
  5050. " }\n"
  5051. " do\n"
  5052. " {\n"
  5053. " c();\n"
  5054. " } while (false)\n"
  5055. "}\n",
  5056. BreakBeforeBrace);
  5057. verifyFormat("void f(int a)\n"
  5058. "{\n"
  5059. " switch (a)\n"
  5060. " {\n"
  5061. " case 0:\n"
  5062. " break;\n"
  5063. " case 1:\n"
  5064. " {\n"
  5065. " break;\n"
  5066. " }\n"
  5067. " case 2:\n"
  5068. " {\n"
  5069. " }\n"
  5070. " break;\n"
  5071. " default:\n"
  5072. " break;\n"
  5073. " }\n"
  5074. "}\n",
  5075. BreakBeforeBrace);
  5076. verifyFormat("enum X\n"
  5077. "{\n"
  5078. " Y = 0,\n"
  5079. "}\n",
  5080. BreakBeforeBrace);
  5081. FormatStyle BreakBeforeBraceShortIfs = BreakBeforeBrace;
  5082. BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
  5083. BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
  5084. verifyFormat("void f(bool b)\n"
  5085. "{\n"
  5086. " if (b)\n"
  5087. " {\n"
  5088. " return;\n"
  5089. " }\n"
  5090. "}\n",
  5091. BreakBeforeBraceShortIfs);
  5092. verifyFormat("void f(bool b)\n"
  5093. "{\n"
  5094. " if (b) return;\n"
  5095. "}\n",
  5096. BreakBeforeBraceShortIfs);
  5097. verifyFormat("void f(bool b)\n"
  5098. "{\n"
  5099. " while (b)\n"
  5100. " {\n"
  5101. " return;\n"
  5102. " }\n"
  5103. "}\n",
  5104. BreakBeforeBraceShortIfs);
  5105. }
  5106. TEST_F(FormatTest, UnderstandsPragmas) {
  5107. verifyFormat("#pragma omp reduction(| : var)");
  5108. verifyFormat("#pragma omp reduction(+ : var)");
  5109. }
  5110. bool allStylesEqual(ArrayRef<FormatStyle> Styles) {
  5111. for (size_t i = 1; i < Styles.size(); ++i)
  5112. if (!(Styles[0] == Styles[i]))
  5113. return false;
  5114. return true;
  5115. }
  5116. TEST_F(FormatTest, GetsPredefinedStyleByName) {
  5117. FormatStyle Styles[3];
  5118. Styles[0] = getLLVMStyle();
  5119. EXPECT_TRUE(getPredefinedStyle("LLVM", &Styles[1]));
  5120. EXPECT_TRUE(getPredefinedStyle("lLvM", &Styles[2]));
  5121. EXPECT_TRUE(allStylesEqual(Styles));
  5122. Styles[0] = getGoogleStyle();
  5123. EXPECT_TRUE(getPredefinedStyle("Google", &Styles[1]));
  5124. EXPECT_TRUE(getPredefinedStyle("gOOgle", &Styles[2]));
  5125. EXPECT_TRUE(allStylesEqual(Styles));
  5126. Styles[0] = getChromiumStyle();
  5127. EXPECT_TRUE(getPredefinedStyle("Chromium", &Styles[1]));
  5128. EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", &Styles[2]));
  5129. EXPECT_TRUE(allStylesEqual(Styles));
  5130. Styles[0] = getMozillaStyle();
  5131. EXPECT_TRUE(getPredefinedStyle("Mozilla", &Styles[1]));
  5132. EXPECT_TRUE(getPredefinedStyle("moZILla", &Styles[2]));
  5133. EXPECT_TRUE(allStylesEqual(Styles));
  5134. Styles[0] = getWebKitStyle();
  5135. EXPECT_TRUE(getPredefinedStyle("WebKit", &Styles[1]));
  5136. EXPECT_TRUE(getPredefinedStyle("wEbKit", &Styles[2]));
  5137. EXPECT_TRUE(allStylesEqual(Styles));
  5138. EXPECT_FALSE(getPredefinedStyle("qwerty", &Styles[0]));
  5139. }
  5140. TEST_F(FormatTest, ParsesConfiguration) {
  5141. FormatStyle Style = {};
  5142. #define CHECK_PARSE(TEXT, FIELD, VALUE) \
  5143. EXPECT_NE(VALUE, Style.FIELD); \
  5144. EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \
  5145. EXPECT_EQ(VALUE, Style.FIELD)
  5146. #define CHECK_PARSE_BOOL(FIELD) \
  5147. Style.FIELD = false; \
  5148. EXPECT_EQ(0, parseConfiguration(#FIELD ": true", &Style).value()); \
  5149. EXPECT_TRUE(Style.FIELD); \
  5150. EXPECT_EQ(0, parseConfiguration(#FIELD ": false", &Style).value()); \
  5151. EXPECT_FALSE(Style.FIELD);
  5152. CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
  5153. CHECK_PARSE_BOOL(AlignTrailingComments);
  5154. CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
  5155. CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
  5156. CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
  5157. CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
  5158. CHECK_PARSE_BOOL(BinPackParameters);
  5159. CHECK_PARSE_BOOL(BreakBeforeBinaryOperators);
  5160. CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
  5161. CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
  5162. CHECK_PARSE_BOOL(DerivePointerBinding);
  5163. CHECK_PARSE_BOOL(IndentCaseLabels);
  5164. CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
  5165. CHECK_PARSE_BOOL(PointerBindsToType);
  5166. CHECK_PARSE_BOOL(Cpp11BracedListStyle);
  5167. CHECK_PARSE_BOOL(UseTab);
  5168. CHECK_PARSE_BOOL(IndentFunctionDeclarationAfterType);
  5169. CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
  5170. CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
  5171. CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
  5172. CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
  5173. CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
  5174. PenaltyReturnTypeOnItsOwnLine, 1234u);
  5175. CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
  5176. SpacesBeforeTrailingComments, 1234u);
  5177. CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
  5178. Style.Standard = FormatStyle::LS_Auto;
  5179. CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
  5180. CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
  5181. CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
  5182. Style.ColumnLimit = 123;
  5183. FormatStyle BaseStyle = getLLVMStyle();
  5184. CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
  5185. CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
  5186. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  5187. CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
  5188. FormatStyle::BS_Attach);
  5189. CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
  5190. FormatStyle::BS_Linux);
  5191. CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
  5192. FormatStyle::BS_Stroustrup);
  5193. Style.NamespaceIndentation = FormatStyle::NI_All;
  5194. CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
  5195. FormatStyle::NI_None);
  5196. CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
  5197. FormatStyle::NI_Inner);
  5198. CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
  5199. FormatStyle::NI_All);
  5200. #undef CHECK_PARSE
  5201. #undef CHECK_PARSE_BOOL
  5202. }
  5203. TEST_F(FormatTest, ConfigurationRoundTripTest) {
  5204. FormatStyle Style = getLLVMStyle();
  5205. std::string YAML = configurationAsText(Style);
  5206. FormatStyle ParsedStyle = {};
  5207. EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
  5208. EXPECT_EQ(Style, ParsedStyle);
  5209. }
  5210. TEST_F(FormatTest, WorksFor8bitEncodings) {
  5211. EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
  5212. "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
  5213. "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
  5214. "\"\xef\xee\xf0\xf3...\"",
  5215. format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
  5216. "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
  5217. "\xef\xee\xf0\xf3...\"",
  5218. getLLVMStyleWithColumns(12)));
  5219. }
  5220. // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
  5221. #if !defined(_MSC_VER)
  5222. TEST_F(FormatTest, CountsUTF8CharactersProperly) {
  5223. verifyFormat("\"Однажды в студёную зимнюю пору...\"",
  5224. getLLVMStyleWithColumns(35));
  5225. verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
  5226. getLLVMStyleWithColumns(21));
  5227. verifyFormat("// Однажды в студёную зимнюю пору...",
  5228. getLLVMStyleWithColumns(36));
  5229. verifyFormat("// 一 二 三 四 五 六 七 八 九 十",
  5230. getLLVMStyleWithColumns(22));
  5231. verifyFormat("/* Однажды в студёную зимнюю пору... */",
  5232. getLLVMStyleWithColumns(39));
  5233. verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
  5234. getLLVMStyleWithColumns(25));
  5235. }
  5236. TEST_F(FormatTest, SplitsUTF8Strings) {
  5237. EXPECT_EQ(
  5238. "\"Однажды, в \"\n"
  5239. "\"студёную \"\n"
  5240. "\"зимнюю \"\n"
  5241. "\"пору,\"",
  5242. format("\"Однажды, в студёную зимнюю пору,\"",
  5243. getLLVMStyleWithColumns(13)));
  5244. EXPECT_EQ("\"一 二 三 四 \"\n"
  5245. "\"五 六 七 八 \"\n"
  5246. "\"九 十\"",
  5247. format("\"一 二 三 四 五 六 七 八 九 十\"",
  5248. getLLVMStyleWithColumns(10)));
  5249. }
  5250. TEST_F(FormatTest, SplitsUTF8LineComments) {
  5251. EXPECT_EQ("// Я из лесу\n"
  5252. "// вышел; был\n"
  5253. "// сильный\n"
  5254. "// мороз.",
  5255. format("// Я из лесу вышел; был сильный мороз.",
  5256. getLLVMStyleWithColumns(13)));
  5257. EXPECT_EQ("// 一二三\n"
  5258. "// 四五六七\n"
  5259. "// 八\n"
  5260. "// 九 十",
  5261. format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(6)));
  5262. }
  5263. TEST_F(FormatTest, SplitsUTF8BlockComments) {
  5264. EXPECT_EQ("/* Гляжу,\n"
  5265. " * поднимается\n"
  5266. " * медленно в\n"
  5267. " * гору\n"
  5268. " * Лошадка,\n"
  5269. " * везущая\n"
  5270. " * хворосту\n"
  5271. " * воз. */",
  5272. format("/* Гляжу, поднимается медленно в гору\n"
  5273. " * Лошадка, везущая хворосту воз. */",
  5274. getLLVMStyleWithColumns(13)));
  5275. EXPECT_EQ("/* 一二三\n"
  5276. " * 四五六七\n"
  5277. " * 八\n"
  5278. " * 九 十\n"
  5279. " */",
  5280. format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(6)));
  5281. EXPECT_EQ("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
  5282. " * 𝕓𝕪𝕥𝕖\n"
  5283. " * 𝖀𝕿𝕱-𝟠 */",
  5284. format("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12)));
  5285. }
  5286. TEST_F(FormatTest, FormatsWithWebKitStyle) {
  5287. FormatStyle Style = getWebKitStyle();
  5288. // Don't indent in outer namespaces.
  5289. verifyFormat("namespace outer {\n"
  5290. "int i;\n"
  5291. "namespace inner {\n"
  5292. " int i;\n"
  5293. "} // namespace inner\n"
  5294. "} // namespace outer\n"
  5295. "namespace other_outer {\n"
  5296. "int i;\n"
  5297. "}",
  5298. Style);
  5299. // Don't indent case labels.
  5300. verifyFormat("switch (variable) {\n"
  5301. "case 1:\n"
  5302. "case 2:\n"
  5303. " doSomething();\n"
  5304. " break;\n"
  5305. "default:\n"
  5306. " ++variable;\n"
  5307. "}",
  5308. Style);
  5309. // Wrap before binary operators.
  5310. EXPECT_EQ(
  5311. "void f()\n"
  5312. "{\n"
  5313. " if (aaaaaaaaaaaaaaaa\n"
  5314. " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
  5315. " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  5316. " return;\n"
  5317. "}",
  5318. format(
  5319. "void f() {\n"
  5320. "if (aaaaaaaaaaaaaaaa\n"
  5321. "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
  5322. "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  5323. "return;\n"
  5324. "}",
  5325. Style));
  5326. // Constructor initializers are formatted one per line with the "," on the
  5327. // new line.
  5328. verifyFormat("Constructor()\n"
  5329. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  5330. " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
  5331. " aaaaaaaaaaaaaa)\n"
  5332. " , aaaaaaaaaaaaaaaaaaaaaaa()\n{\n}",
  5333. Style);
  5334. // Access specifiers should be aligned left.
  5335. verifyFormat("class C {\n"
  5336. "public:\n"
  5337. " int i;\n"
  5338. "};",
  5339. Style);
  5340. // Do not align comments.
  5341. verifyFormat("int a; // Do not\n"
  5342. "double b; // align comments.",
  5343. Style);
  5344. // Accept input's line breaks.
  5345. EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
  5346. " || bbbbbbbbbbbbbbb) {\n"
  5347. " i++;\n"
  5348. "}",
  5349. format("if (aaaaaaaaaaaaaaa\n"
  5350. "|| bbbbbbbbbbbbbbb) { i++; }",
  5351. Style));
  5352. EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
  5353. " i++;\n"
  5354. "}",
  5355. format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
  5356. }
  5357. #endif
  5358. } // end namespace tooling
  5359. } // end namespace clang