FormatTest.cpp 355 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598
  1. //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "FormatTestUtils.h"
  10. #include "clang/Format/Format.h"
  11. #include "llvm/Support/Debug.h"
  12. #include "gtest/gtest.h"
  13. #define DEBUG_TYPE "format-test"
  14. namespace clang {
  15. namespace format {
  16. FormatStyle getGoogleStyle() {
  17. return getGoogleStyle(FormatStyle::LK_Cpp);
  18. }
  19. class FormatTest : public ::testing::Test {
  20. protected:
  21. std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length,
  22. const FormatStyle &Style) {
  23. DEBUG(llvm::errs() << "---\n");
  24. DEBUG(llvm::errs() << Code << "\n\n");
  25. std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
  26. tooling::Replacements Replaces = reformat(Style, Code, Ranges);
  27. ReplacementCount = Replaces.size();
  28. std::string Result = applyAllReplacements(Code, Replaces);
  29. EXPECT_NE("", Result);
  30. DEBUG(llvm::errs() << "\n" << Result << "\n\n");
  31. return Result;
  32. }
  33. std::string
  34. format(llvm::StringRef Code, const FormatStyle &Style = getLLVMStyle()) {
  35. return format(Code, 0, Code.size(), Style);
  36. }
  37. FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
  38. FormatStyle Style = getLLVMStyle();
  39. Style.ColumnLimit = ColumnLimit;
  40. return Style;
  41. }
  42. FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
  43. FormatStyle Style = getGoogleStyle();
  44. Style.ColumnLimit = ColumnLimit;
  45. return Style;
  46. }
  47. void verifyFormat(llvm::StringRef Code,
  48. const FormatStyle &Style = getLLVMStyle()) {
  49. EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
  50. }
  51. void verifyGoogleFormat(llvm::StringRef Code) {
  52. verifyFormat(Code, getGoogleStyle());
  53. }
  54. void verifyIndependentOfContext(llvm::StringRef text) {
  55. verifyFormat(text);
  56. verifyFormat(llvm::Twine("void f() { " + text + " }").str());
  57. }
  58. int ReplacementCount;
  59. };
  60. TEST_F(FormatTest, MessUp) {
  61. EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
  62. EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
  63. EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
  64. EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
  65. EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
  66. }
  67. //===----------------------------------------------------------------------===//
  68. // Basic function tests.
  69. //===----------------------------------------------------------------------===//
  70. TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
  71. EXPECT_EQ(";", format(";"));
  72. }
  73. TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
  74. EXPECT_EQ("int i;", format(" int i;"));
  75. EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;"));
  76. EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
  77. EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
  78. }
  79. TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
  80. EXPECT_EQ("int i;", format("int\ni;"));
  81. }
  82. TEST_F(FormatTest, FormatsNestedBlockStatements) {
  83. EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
  84. }
  85. TEST_F(FormatTest, FormatsNestedCall) {
  86. verifyFormat("Method(f1, f2(f3));");
  87. verifyFormat("Method(f1(f2, f3()));");
  88. verifyFormat("Method(f1(f2, (f3())));");
  89. }
  90. TEST_F(FormatTest, NestedNameSpecifiers) {
  91. verifyFormat("vector<::Type> v;");
  92. verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
  93. verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
  94. verifyFormat("bool a = 2 < ::SomeFunction();");
  95. }
  96. TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
  97. EXPECT_EQ("if (a) {\n"
  98. " f();\n"
  99. "}",
  100. format("if(a){f();}"));
  101. EXPECT_EQ(4, ReplacementCount);
  102. EXPECT_EQ("if (a) {\n"
  103. " f();\n"
  104. "}",
  105. format("if (a) {\n"
  106. " f();\n"
  107. "}"));
  108. EXPECT_EQ(0, ReplacementCount);
  109. }
  110. TEST_F(FormatTest, RemovesTrailingWhitespaceOfFormattedLine) {
  111. EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0, getLLVMStyle()));
  112. EXPECT_EQ("int a;", format("int a; "));
  113. EXPECT_EQ("int a;\n", format("int a; \n \n \n "));
  114. EXPECT_EQ("int a;\nint b; ",
  115. format("int a; \nint b; ", 0, 0, getLLVMStyle()));
  116. }
  117. TEST_F(FormatTest, FormatsCorrectRegionForLeadingWhitespace) {
  118. EXPECT_EQ("int b;\nint a;",
  119. format("int b;\n int a;", 7, 0, getLLVMStyle()));
  120. EXPECT_EQ("int b;\n int a;",
  121. format("int b;\n int a;", 6, 0, getLLVMStyle()));
  122. EXPECT_EQ("#define A \\\n"
  123. " int a; \\\n"
  124. " int b;",
  125. format("#define A \\\n"
  126. " int a; \\\n"
  127. " int b;",
  128. 26, 0, getLLVMStyleWithColumns(12)));
  129. EXPECT_EQ("#define A \\\n"
  130. " int a; \\\n"
  131. " int b;",
  132. format("#define A \\\n"
  133. " int a; \\\n"
  134. " int b;",
  135. 25, 0, getLLVMStyleWithColumns(12)));
  136. }
  137. TEST_F(FormatTest, FormatLineWhenInvokedOnTrailingNewline) {
  138. EXPECT_EQ("int b;\n\nint a;",
  139. format("int b;\n\nint a;", 8, 0, getLLVMStyle()));
  140. EXPECT_EQ("int b;\n\nint a;",
  141. format("int b;\n\nint a;", 7, 0, getLLVMStyle()));
  142. // This might not strictly be correct, but is likely good in all practical
  143. // cases.
  144. EXPECT_EQ("int b;\nint a;",
  145. format("int b;int a;", 7, 0, getLLVMStyle()));
  146. }
  147. TEST_F(FormatTest, RemovesWhitespaceWhenTriggeredOnEmptyLine) {
  148. EXPECT_EQ("int a;\n\n int b;",
  149. format("int a;\n \n\n int b;", 8, 0, getLLVMStyle()));
  150. EXPECT_EQ("int a;\n\n int b;",
  151. format("int a;\n \n\n int b;", 9, 0, getLLVMStyle()));
  152. }
  153. TEST_F(FormatTest, RemovesEmptyLines) {
  154. EXPECT_EQ("class C {\n"
  155. " int i;\n"
  156. "};",
  157. format("class C {\n"
  158. " int i;\n"
  159. "\n"
  160. "};"));
  161. // Don't remove empty lines at the start of namespaces or extern "C" blocks.
  162. EXPECT_EQ("namespace N {\n"
  163. "\n"
  164. "int i;\n"
  165. "}",
  166. format("namespace N {\n"
  167. "\n"
  168. "int i;\n"
  169. "}",
  170. getGoogleStyle()));
  171. EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
  172. "\n"
  173. "int i;\n"
  174. "}",
  175. format("extern /**/ \"C\" /**/ {\n"
  176. "\n"
  177. "int i;\n"
  178. "}",
  179. getGoogleStyle()));
  180. // ...but do keep inlining and removing empty lines for non-block extern "C"
  181. // functions.
  182. verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
  183. EXPECT_EQ("extern \"C\" int f() {\n"
  184. " int i = 42;\n"
  185. " return i;\n"
  186. "}",
  187. format("extern \"C\" int f() {\n"
  188. "\n"
  189. " int i = 42;\n"
  190. " return i;\n"
  191. "}",
  192. getGoogleStyle()));
  193. // Remove empty lines at the beginning and end of blocks.
  194. EXPECT_EQ("void f() {\n"
  195. "\n"
  196. " if (a) {\n"
  197. "\n"
  198. " f();\n"
  199. " }\n"
  200. "}",
  201. format("void f() {\n"
  202. "\n"
  203. " if (a) {\n"
  204. "\n"
  205. " f();\n"
  206. "\n"
  207. " }\n"
  208. "\n"
  209. "}",
  210. getLLVMStyle()));
  211. EXPECT_EQ("void f() {\n"
  212. " if (a) {\n"
  213. " f();\n"
  214. " }\n"
  215. "}",
  216. format("void f() {\n"
  217. "\n"
  218. " if (a) {\n"
  219. "\n"
  220. " f();\n"
  221. "\n"
  222. " }\n"
  223. "\n"
  224. "}",
  225. getGoogleStyle()));
  226. // Don't remove empty lines in more complex control statements.
  227. EXPECT_EQ("void f() {\n"
  228. " if (a) {\n"
  229. " f();\n"
  230. "\n"
  231. " } else if (b) {\n"
  232. " f();\n"
  233. " }\n"
  234. "}",
  235. format("void f() {\n"
  236. " if (a) {\n"
  237. " f();\n"
  238. "\n"
  239. " } else if (b) {\n"
  240. " f();\n"
  241. "\n"
  242. " }\n"
  243. "\n"
  244. "}"));
  245. // FIXME: This is slightly inconsistent.
  246. EXPECT_EQ("namespace {\n"
  247. "int i;\n"
  248. "}",
  249. format("namespace {\n"
  250. "int i;\n"
  251. "\n"
  252. "}"));
  253. EXPECT_EQ("namespace {\n"
  254. "int i;\n"
  255. "\n"
  256. "} // namespace",
  257. format("namespace {\n"
  258. "int i;\n"
  259. "\n"
  260. "} // namespace"));
  261. }
  262. TEST_F(FormatTest, ReformatsMovedLines) {
  263. EXPECT_EQ(
  264. "template <typename T> T *getFETokenInfo() const {\n"
  265. " return static_cast<T *>(FETokenInfo);\n"
  266. "}\n"
  267. " int a; // <- Should not be formatted",
  268. format(
  269. "template<typename T>\n"
  270. "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
  271. " int a; // <- Should not be formatted",
  272. 9, 5, getLLVMStyle()));
  273. }
  274. TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
  275. verifyFormat("x = (a) and (b);");
  276. verifyFormat("x = (a) or (b);");
  277. verifyFormat("x = (a) bitand (b);");
  278. verifyFormat("x = (a) bitor (b);");
  279. verifyFormat("x = (a) not_eq (b);");
  280. verifyFormat("x = (a) and_eq (b);");
  281. verifyFormat("x = (a) or_eq (b);");
  282. verifyFormat("x = (a) xor (b);");
  283. }
  284. //===----------------------------------------------------------------------===//
  285. // Tests for control statements.
  286. //===----------------------------------------------------------------------===//
  287. TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
  288. verifyFormat("if (true)\n f();\ng();");
  289. verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
  290. verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
  291. FormatStyle AllowsMergedIf = getLLVMStyle();
  292. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  293. verifyFormat("if (a)\n"
  294. " // comment\n"
  295. " f();",
  296. AllowsMergedIf);
  297. verifyFormat("if (a)\n"
  298. " ;",
  299. AllowsMergedIf);
  300. verifyFormat("if (a)\n"
  301. " if (b) return;",
  302. AllowsMergedIf);
  303. verifyFormat("if (a) // Can't merge this\n"
  304. " f();\n",
  305. AllowsMergedIf);
  306. verifyFormat("if (a) /* still don't merge */\n"
  307. " f();",
  308. AllowsMergedIf);
  309. verifyFormat("if (a) { // Never merge this\n"
  310. " f();\n"
  311. "}",
  312. AllowsMergedIf);
  313. verifyFormat("if (a) {/* Never merge this */\n"
  314. " f();\n"
  315. "}",
  316. AllowsMergedIf);
  317. EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1, AllowsMergedIf));
  318. EXPECT_EQ("if (a) return; // comment",
  319. format("if(a)\nreturn; // comment", 20, 1, AllowsMergedIf));
  320. AllowsMergedIf.ColumnLimit = 14;
  321. verifyFormat("if (a) return;", AllowsMergedIf);
  322. verifyFormat("if (aaaaaaaaa)\n"
  323. " return;",
  324. AllowsMergedIf);
  325. AllowsMergedIf.ColumnLimit = 13;
  326. verifyFormat("if (a)\n return;", AllowsMergedIf);
  327. }
  328. TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
  329. FormatStyle AllowsMergedLoops = getLLVMStyle();
  330. AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
  331. verifyFormat("while (true) continue;", AllowsMergedLoops);
  332. verifyFormat("for (;;) continue;", AllowsMergedLoops);
  333. verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
  334. verifyFormat("while (true)\n"
  335. " ;",
  336. AllowsMergedLoops);
  337. verifyFormat("for (;;)\n"
  338. " ;",
  339. AllowsMergedLoops);
  340. verifyFormat("for (;;)\n"
  341. " for (;;) continue;",
  342. AllowsMergedLoops);
  343. verifyFormat("for (;;) // Can't merge this\n"
  344. " continue;",
  345. AllowsMergedLoops);
  346. verifyFormat("for (;;) /* still don't merge */\n"
  347. " continue;",
  348. AllowsMergedLoops);
  349. }
  350. TEST_F(FormatTest, FormatShortBracedStatements) {
  351. FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
  352. AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
  353. AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
  354. AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
  355. verifyFormat("if (true) {}", AllowSimpleBracedStatements);
  356. verifyFormat("while (true) {}", AllowSimpleBracedStatements);
  357. verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
  358. verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
  359. verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
  360. verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
  361. verifyFormat("if (true) { //\n"
  362. " f();\n"
  363. "}",
  364. AllowSimpleBracedStatements);
  365. verifyFormat("if (true) {\n"
  366. " f();\n"
  367. " f();\n"
  368. "}",
  369. AllowSimpleBracedStatements);
  370. verifyFormat("template <int> struct A2 {\n"
  371. " struct B {};\n"
  372. "};",
  373. AllowSimpleBracedStatements);
  374. AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
  375. verifyFormat("if (true) {\n"
  376. " f();\n"
  377. "}",
  378. AllowSimpleBracedStatements);
  379. AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
  380. verifyFormat("while (true) {\n"
  381. " f();\n"
  382. "}",
  383. AllowSimpleBracedStatements);
  384. verifyFormat("for (;;) {\n"
  385. " f();\n"
  386. "}",
  387. AllowSimpleBracedStatements);
  388. }
  389. TEST_F(FormatTest, ParseIfElse) {
  390. verifyFormat("if (true)\n"
  391. " if (true)\n"
  392. " if (true)\n"
  393. " f();\n"
  394. " else\n"
  395. " g();\n"
  396. " else\n"
  397. " h();\n"
  398. "else\n"
  399. " i();");
  400. verifyFormat("if (true)\n"
  401. " if (true)\n"
  402. " if (true) {\n"
  403. " if (true)\n"
  404. " f();\n"
  405. " } else {\n"
  406. " g();\n"
  407. " }\n"
  408. " else\n"
  409. " h();\n"
  410. "else {\n"
  411. " i();\n"
  412. "}");
  413. verifyFormat("void f() {\n"
  414. " if (a) {\n"
  415. " } else {\n"
  416. " }\n"
  417. "}");
  418. }
  419. TEST_F(FormatTest, ElseIf) {
  420. verifyFormat("if (a) {\n} else if (b) {\n}");
  421. verifyFormat("if (a)\n"
  422. " f();\n"
  423. "else if (b)\n"
  424. " g();\n"
  425. "else\n"
  426. " h();");
  427. verifyFormat("if (a) {\n"
  428. " f();\n"
  429. "}\n"
  430. "// or else ..\n"
  431. "else {\n"
  432. " g()\n"
  433. "}");
  434. verifyFormat("if (a) {\n"
  435. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  436. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  437. "}");
  438. }
  439. TEST_F(FormatTest, FormatsForLoop) {
  440. verifyFormat(
  441. "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
  442. " ++VeryVeryLongLoopVariable)\n"
  443. " ;");
  444. verifyFormat("for (;;)\n"
  445. " f();");
  446. verifyFormat("for (;;) {\n}");
  447. verifyFormat("for (;;) {\n"
  448. " f();\n"
  449. "}");
  450. verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
  451. verifyFormat(
  452. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  453. " E = UnwrappedLines.end();\n"
  454. " I != E; ++I) {\n}");
  455. verifyFormat(
  456. "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
  457. " ++IIIII) {\n}");
  458. verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
  459. " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
  460. " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
  461. verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
  462. " I = FD->getDeclsInPrototypeScope().begin(),\n"
  463. " E = FD->getDeclsInPrototypeScope().end();\n"
  464. " I != E; ++I) {\n}");
  465. // FIXME: Not sure whether we want extra identation in line 3 here:
  466. verifyFormat(
  467. "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  468. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
  469. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  470. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  471. " ++aaaaaaaaaaa) {\n}");
  472. verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
  473. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  474. "}");
  475. verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
  476. " aaaaaaaaaa);\n"
  477. " iter; ++iter) {\n"
  478. "}");
  479. FormatStyle NoBinPacking = getLLVMStyle();
  480. NoBinPacking.BinPackParameters = false;
  481. verifyFormat("for (int aaaaaaaaaaa = 1;\n"
  482. " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
  483. " aaaaaaaaaaaaaaaa,\n"
  484. " aaaaaaaaaaaaaaaa,\n"
  485. " aaaaaaaaaaaaaaaa);\n"
  486. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  487. "}",
  488. NoBinPacking);
  489. verifyFormat(
  490. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  491. " E = UnwrappedLines.end();\n"
  492. " I != E;\n"
  493. " ++I) {\n}",
  494. NoBinPacking);
  495. }
  496. TEST_F(FormatTest, RangeBasedForLoops) {
  497. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  498. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  499. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
  500. " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
  501. verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
  502. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  503. verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
  504. " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
  505. }
  506. TEST_F(FormatTest, ForEachLoops) {
  507. verifyFormat("void f() {\n"
  508. " foreach (Item *item, itemlist) {}\n"
  509. " Q_FOREACH (Item *item, itemlist) {}\n"
  510. " BOOST_FOREACH (Item *item, itemlist) {}\n"
  511. " UNKNOWN_FORACH(Item * item, itemlist) {}\n"
  512. "}");
  513. }
  514. TEST_F(FormatTest, FormatsWhileLoop) {
  515. verifyFormat("while (true) {\n}");
  516. verifyFormat("while (true)\n"
  517. " f();");
  518. verifyFormat("while () {\n}");
  519. verifyFormat("while () {\n"
  520. " f();\n"
  521. "}");
  522. }
  523. TEST_F(FormatTest, FormatsDoWhile) {
  524. verifyFormat("do {\n"
  525. " do_something();\n"
  526. "} while (something());");
  527. verifyFormat("do\n"
  528. " do_something();\n"
  529. "while (something());");
  530. }
  531. TEST_F(FormatTest, FormatsSwitchStatement) {
  532. verifyFormat("switch (x) {\n"
  533. "case 1:\n"
  534. " f();\n"
  535. " break;\n"
  536. "case kFoo:\n"
  537. "case ns::kBar:\n"
  538. "case kBaz:\n"
  539. " break;\n"
  540. "default:\n"
  541. " g();\n"
  542. " break;\n"
  543. "}");
  544. verifyFormat("switch (x) {\n"
  545. "case 1: {\n"
  546. " f();\n"
  547. " break;\n"
  548. "}\n"
  549. "case 2: {\n"
  550. " break;\n"
  551. "}\n"
  552. "}");
  553. verifyFormat("switch (x) {\n"
  554. "case 1: {\n"
  555. " f();\n"
  556. " {\n"
  557. " g();\n"
  558. " h();\n"
  559. " }\n"
  560. " break;\n"
  561. "}\n"
  562. "}");
  563. verifyFormat("switch (x) {\n"
  564. "case 1: {\n"
  565. " f();\n"
  566. " if (foo) {\n"
  567. " g();\n"
  568. " h();\n"
  569. " }\n"
  570. " break;\n"
  571. "}\n"
  572. "}");
  573. verifyFormat("switch (x) {\n"
  574. "case 1: {\n"
  575. " f();\n"
  576. " g();\n"
  577. "} break;\n"
  578. "}");
  579. verifyFormat("switch (test)\n"
  580. " ;");
  581. verifyFormat("switch (x) {\n"
  582. "default: {\n"
  583. " // Do nothing.\n"
  584. "}\n"
  585. "}");
  586. verifyFormat("switch (x) {\n"
  587. "// comment\n"
  588. "// if 1, do f()\n"
  589. "case 1:\n"
  590. " f();\n"
  591. "}");
  592. verifyFormat("switch (x) {\n"
  593. "case 1:\n"
  594. " // Do amazing stuff\n"
  595. " {\n"
  596. " f();\n"
  597. " g();\n"
  598. " }\n"
  599. " break;\n"
  600. "}");
  601. verifyFormat("#define A \\\n"
  602. " switch (x) { \\\n"
  603. " case a: \\\n"
  604. " foo = b; \\\n"
  605. " }", getLLVMStyleWithColumns(20));
  606. verifyFormat("#define OPERATION_CASE(name) \\\n"
  607. " case OP_name: \\\n"
  608. " return operations::Operation##name\n",
  609. getLLVMStyleWithColumns(40));
  610. verifyGoogleFormat("switch (x) {\n"
  611. " case 1:\n"
  612. " f();\n"
  613. " break;\n"
  614. " case kFoo:\n"
  615. " case ns::kBar:\n"
  616. " case kBaz:\n"
  617. " break;\n"
  618. " default:\n"
  619. " g();\n"
  620. " break;\n"
  621. "}");
  622. verifyGoogleFormat("switch (x) {\n"
  623. " case 1: {\n"
  624. " f();\n"
  625. " break;\n"
  626. " }\n"
  627. "}");
  628. verifyGoogleFormat("switch (test)\n"
  629. " ;");
  630. verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
  631. " case OP_name: \\\n"
  632. " return operations::Operation##name\n");
  633. verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
  634. " // Get the correction operation class.\n"
  635. " switch (OpCode) {\n"
  636. " CASE(Add);\n"
  637. " CASE(Subtract);\n"
  638. " default:\n"
  639. " return operations::Unknown;\n"
  640. " }\n"
  641. "#undef OPERATION_CASE\n"
  642. "}");
  643. verifyFormat("DEBUG({\n"
  644. " switch (x) {\n"
  645. " case A:\n"
  646. " f();\n"
  647. " break;\n"
  648. " // On B:\n"
  649. " case B:\n"
  650. " g();\n"
  651. " break;\n"
  652. " }\n"
  653. "});");
  654. verifyFormat("switch (a) {\n"
  655. "case (b):\n"
  656. " return;\n"
  657. "}");
  658. verifyFormat("switch (a) {\n"
  659. "case some_namespace::\n"
  660. " some_constant:\n"
  661. " return;\n"
  662. "}",
  663. getLLVMStyleWithColumns(34));
  664. }
  665. TEST_F(FormatTest, CaseRanges) {
  666. verifyFormat("switch (x) {\n"
  667. "case 'A' ... 'Z':\n"
  668. "case 1 ... 5:\n"
  669. " break;\n"
  670. "}");
  671. }
  672. TEST_F(FormatTest, ShortCaseLabels) {
  673. FormatStyle Style = getLLVMStyle();
  674. Style.AllowShortCaseLabelsOnASingleLine = true;
  675. verifyFormat("switch (a) {\n"
  676. "case 1: x = 1; break;\n"
  677. "case 2: return;\n"
  678. "case 3:\n"
  679. "case 4:\n"
  680. "case 5: return;\n"
  681. "default: y = 1; break;\n"
  682. "}",
  683. Style);
  684. verifyFormat("switch (a) {\n"
  685. "case 1: {\n"
  686. "}\n"
  687. "case 2: {\n"
  688. " return;\n"
  689. "}\n"
  690. "case 3: {\n"
  691. " x = 1;\n"
  692. " return;\n"
  693. "}\n"
  694. "case 4:\n"
  695. " if (x)\n"
  696. " return;\n"
  697. "}",
  698. Style);
  699. Style.ColumnLimit = 21;
  700. verifyFormat("switch (a) {\n"
  701. "case 1: x = 1; break;\n"
  702. "case 2: return;\n"
  703. "case 3:\n"
  704. "case 4:\n"
  705. "case 5: return;\n"
  706. "default:\n"
  707. " y = 1;\n"
  708. " break;\n"
  709. "}",
  710. Style);
  711. }
  712. TEST_F(FormatTest, FormatsLabels) {
  713. verifyFormat("void f() {\n"
  714. " some_code();\n"
  715. "test_label:\n"
  716. " some_other_code();\n"
  717. " {\n"
  718. " some_more_code();\n"
  719. " another_label:\n"
  720. " some_more_code();\n"
  721. " }\n"
  722. "}");
  723. verifyFormat("some_code();\n"
  724. "test_label:\n"
  725. "some_other_code();");
  726. }
  727. //===----------------------------------------------------------------------===//
  728. // Tests for comments.
  729. //===----------------------------------------------------------------------===//
  730. TEST_F(FormatTest, UnderstandsSingleLineComments) {
  731. verifyFormat("//* */");
  732. verifyFormat("// line 1\n"
  733. "// line 2\n"
  734. "void f() {}\n");
  735. verifyFormat("void f() {\n"
  736. " // Doesn't do anything\n"
  737. "}");
  738. verifyFormat("SomeObject\n"
  739. " // Calling someFunction on SomeObject\n"
  740. " .someFunction();");
  741. verifyFormat("auto result = SomeObject\n"
  742. " // Calling someFunction on SomeObject\n"
  743. " .someFunction();");
  744. verifyFormat("void f(int i, // some comment (probably for i)\n"
  745. " int j, // some comment (probably for j)\n"
  746. " int k); // some comment (probably for k)");
  747. verifyFormat("void f(int i,\n"
  748. " // some comment (probably for j)\n"
  749. " int j,\n"
  750. " // some comment (probably for k)\n"
  751. " int k);");
  752. verifyFormat("int i // This is a fancy variable\n"
  753. " = 5; // with nicely aligned comment.");
  754. verifyFormat("// Leading comment.\n"
  755. "int a; // Trailing comment.");
  756. verifyFormat("int a; // Trailing comment\n"
  757. " // on 2\n"
  758. " // or 3 lines.\n"
  759. "int b;");
  760. verifyFormat("int a; // Trailing comment\n"
  761. "\n"
  762. "// Leading comment.\n"
  763. "int b;");
  764. verifyFormat("int a; // Comment.\n"
  765. " // More details.\n"
  766. "int bbbb; // Another comment.");
  767. verifyFormat(
  768. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  769. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n"
  770. "int cccccccccccccccccccccccccccccc; // comment\n"
  771. "int ddd; // looooooooooooooooooooooooong comment\n"
  772. "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  773. "int bbbbbbbbbbbbbbbbbbbbb; // comment\n"
  774. "int ccccccccccccccccccc; // comment");
  775. verifyFormat("#include \"a\" // comment\n"
  776. "#include \"a/b/c\" // comment");
  777. verifyFormat("#include <a> // comment\n"
  778. "#include <a/b/c> // comment");
  779. EXPECT_EQ("#include \"a\" // comment\n"
  780. "#include \"a/b/c\" // comment",
  781. format("#include \\\n"
  782. " \"a\" // comment\n"
  783. "#include \"a/b/c\" // comment"));
  784. verifyFormat("enum E {\n"
  785. " // comment\n"
  786. " VAL_A, // comment\n"
  787. " VAL_B\n"
  788. "};");
  789. verifyFormat(
  790. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  791. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
  792. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  793. " // Comment inside a statement.\n"
  794. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  795. verifyFormat("SomeFunction(a,\n"
  796. " // comment\n"
  797. " b + x);");
  798. verifyFormat("SomeFunction(a, a,\n"
  799. " // comment\n"
  800. " b + x);");
  801. verifyFormat(
  802. "bool aaaaaaaaaaaaa = // comment\n"
  803. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  804. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  805. verifyFormat("int aaaa; // aaaaa\n"
  806. "int aa; // aaaaaaa",
  807. getLLVMStyleWithColumns(20));
  808. EXPECT_EQ("void f() { // This does something ..\n"
  809. "}\n"
  810. "int a; // This is unrelated",
  811. format("void f() { // This does something ..\n"
  812. " }\n"
  813. "int a; // This is unrelated"));
  814. EXPECT_EQ("class C {\n"
  815. " void f() { // This does something ..\n"
  816. " } // awesome..\n"
  817. "\n"
  818. " int a; // This is unrelated\n"
  819. "};",
  820. format("class C{void f() { // This does something ..\n"
  821. " } // awesome..\n"
  822. " \n"
  823. "int a; // This is unrelated\n"
  824. "};"));
  825. EXPECT_EQ("int i; // single line trailing comment",
  826. format("int i;\\\n// single line trailing comment"));
  827. verifyGoogleFormat("int a; // Trailing comment.");
  828. verifyFormat("someFunction(anotherFunction( // Force break.\n"
  829. " parameter));");
  830. verifyGoogleFormat("#endif // HEADER_GUARD");
  831. verifyFormat("const char *test[] = {\n"
  832. " // A\n"
  833. " \"aaaa\",\n"
  834. " // B\n"
  835. " \"aaaaa\"};");
  836. verifyGoogleFormat(
  837. "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  838. " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment");
  839. EXPECT_EQ("D(a, {\n"
  840. " // test\n"
  841. " int a;\n"
  842. "});",
  843. format("D(a, {\n"
  844. "// test\n"
  845. "int a;\n"
  846. "});"));
  847. EXPECT_EQ("lineWith(); // comment\n"
  848. "// at start\n"
  849. "otherLine();",
  850. format("lineWith(); // comment\n"
  851. "// at start\n"
  852. "otherLine();"));
  853. EXPECT_EQ("lineWith(); // comment\n"
  854. " // at start\n"
  855. "otherLine();",
  856. format("lineWith(); // comment\n"
  857. " // at start\n"
  858. "otherLine();"));
  859. EXPECT_EQ("lineWith(); // comment\n"
  860. "// at start\n"
  861. "otherLine(); // comment",
  862. format("lineWith(); // comment\n"
  863. "// at start\n"
  864. "otherLine(); // comment"));
  865. EXPECT_EQ("lineWith();\n"
  866. "// at start\n"
  867. "otherLine(); // comment",
  868. format("lineWith();\n"
  869. " // at start\n"
  870. "otherLine(); // comment"));
  871. EXPECT_EQ("// first\n"
  872. "// at start\n"
  873. "otherLine(); // comment",
  874. format("// first\n"
  875. " // at start\n"
  876. "otherLine(); // comment"));
  877. EXPECT_EQ("f();\n"
  878. "// first\n"
  879. "// at start\n"
  880. "otherLine(); // comment",
  881. format("f();\n"
  882. "// first\n"
  883. " // at start\n"
  884. "otherLine(); // comment"));
  885. verifyFormat("f(); // comment\n"
  886. "// first\n"
  887. "// at start\n"
  888. "otherLine();");
  889. EXPECT_EQ("f(); // comment\n"
  890. "// first\n"
  891. "// at start\n"
  892. "otherLine();",
  893. format("f(); // comment\n"
  894. "// first\n"
  895. " // at start\n"
  896. "otherLine();"));
  897. EXPECT_EQ("f(); // comment\n"
  898. " // first\n"
  899. "// at start\n"
  900. "otherLine();",
  901. format("f(); // comment\n"
  902. " // first\n"
  903. "// at start\n"
  904. "otherLine();"));
  905. verifyFormat(
  906. "#define A \\\n"
  907. " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
  908. " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
  909. getLLVMStyleWithColumns(60));
  910. verifyFormat(
  911. "#define A \\\n"
  912. " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
  913. " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
  914. getLLVMStyleWithColumns(61));
  915. verifyFormat("if ( // This is some comment\n"
  916. " x + 3) {\n"
  917. "}");
  918. EXPECT_EQ("if ( // This is some comment\n"
  919. " // spanning two lines\n"
  920. " x + 3) {\n"
  921. "}",
  922. format("if( // This is some comment\n"
  923. " // spanning two lines\n"
  924. " x + 3) {\n"
  925. "}"));
  926. }
  927. TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
  928. EXPECT_EQ("SomeFunction(a,\n"
  929. " b, // comment\n"
  930. " c);",
  931. format("SomeFunction(a,\n"
  932. " b, // comment\n"
  933. " c);"));
  934. EXPECT_EQ("SomeFunction(a, b,\n"
  935. " // comment\n"
  936. " c);",
  937. format("SomeFunction(a,\n"
  938. " b,\n"
  939. " // comment\n"
  940. " c);"));
  941. EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
  942. " c);",
  943. format("SomeFunction(a, b, // comment (unclear relation)\n"
  944. " c);"));
  945. EXPECT_EQ("SomeFunction(a, // comment\n"
  946. " b,\n"
  947. " c); // comment",
  948. format("SomeFunction(a, // comment\n"
  949. " b,\n"
  950. " c); // comment"));
  951. }
  952. TEST_F(FormatTest, CanFormatCommentsLocally) {
  953. EXPECT_EQ("int a; // comment\n"
  954. "int b; // comment",
  955. format("int a; // comment\n"
  956. "int b; // comment",
  957. 0, 0, getLLVMStyle()));
  958. EXPECT_EQ("int a; // comment\n"
  959. " // line 2\n"
  960. "int b;",
  961. format("int a; // comment\n"
  962. " // line 2\n"
  963. "int b;",
  964. 28, 0, getLLVMStyle()));
  965. EXPECT_EQ("int aaaaaa; // comment\n"
  966. "int b;\n"
  967. "int c; // unrelated comment",
  968. format("int aaaaaa; // comment\n"
  969. "int b;\n"
  970. "int c; // unrelated comment",
  971. 31, 0, getLLVMStyle()));
  972. EXPECT_EQ("int a; // This\n"
  973. " // is\n"
  974. " // a",
  975. format("int a; // This\n"
  976. " // is\n"
  977. " // a",
  978. 0, 0, getLLVMStyle()));
  979. EXPECT_EQ("int a; // This\n"
  980. " // is\n"
  981. " // a\n"
  982. "// This is b\n"
  983. "int b;",
  984. format("int a; // This\n"
  985. " // is\n"
  986. " // a\n"
  987. "// This is b\n"
  988. "int b;",
  989. 0, 0, getLLVMStyle()));
  990. EXPECT_EQ("int a; // This\n"
  991. " // is\n"
  992. " // a\n"
  993. "\n"
  994. " // This is unrelated",
  995. format("int a; // This\n"
  996. " // is\n"
  997. " // a\n"
  998. "\n"
  999. " // This is unrelated",
  1000. 0, 0, getLLVMStyle()));
  1001. EXPECT_EQ("int a;\n"
  1002. "// This is\n"
  1003. "// not formatted. ",
  1004. format("int a;\n"
  1005. "// This is\n"
  1006. "// not formatted. ",
  1007. 0, 0, getLLVMStyle()));
  1008. }
  1009. TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
  1010. EXPECT_EQ("// comment", format("// comment "));
  1011. EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
  1012. format("int aaaaaaa, bbbbbbb; // comment ",
  1013. getLLVMStyleWithColumns(33)));
  1014. EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f "));
  1015. EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f "));
  1016. }
  1017. TEST_F(FormatTest, UnderstandsBlockComments) {
  1018. verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
  1019. verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }");
  1020. EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
  1021. " bbbbbbbbbbbbbbbbbbbbbbbbb);",
  1022. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n"
  1023. "/* Trailing comment for aa... */\n"
  1024. " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  1025. EXPECT_EQ(
  1026. "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  1027. " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
  1028. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
  1029. "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  1030. EXPECT_EQ(
  1031. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1032. " aaaaaaaaaaaaaaaaaa,\n"
  1033. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  1034. "}",
  1035. format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1036. " aaaaaaaaaaaaaaaaaa ,\n"
  1037. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  1038. "}"));
  1039. FormatStyle NoBinPacking = getLLVMStyle();
  1040. NoBinPacking.BinPackParameters = false;
  1041. verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
  1042. " /* parameter 2 */ aaaaaa,\n"
  1043. " /* parameter 3 */ aaaaaa,\n"
  1044. " /* parameter 4 */ aaaaaa);",
  1045. NoBinPacking);
  1046. // Aligning block comments in macros.
  1047. verifyGoogleFormat("#define A \\\n"
  1048. " int i; /*a*/ \\\n"
  1049. " int jjj; /*b*/");
  1050. }
  1051. TEST_F(FormatTest, AlignsBlockComments) {
  1052. EXPECT_EQ("/*\n"
  1053. " * Really multi-line\n"
  1054. " * comment.\n"
  1055. " */\n"
  1056. "void f() {}",
  1057. format(" /*\n"
  1058. " * Really multi-line\n"
  1059. " * comment.\n"
  1060. " */\n"
  1061. " void f() {}"));
  1062. EXPECT_EQ("class C {\n"
  1063. " /*\n"
  1064. " * Another multi-line\n"
  1065. " * comment.\n"
  1066. " */\n"
  1067. " void f() {}\n"
  1068. "};",
  1069. format("class C {\n"
  1070. "/*\n"
  1071. " * Another multi-line\n"
  1072. " * comment.\n"
  1073. " */\n"
  1074. "void f() {}\n"
  1075. "};"));
  1076. EXPECT_EQ("/*\n"
  1077. " 1. This is a comment with non-trivial formatting.\n"
  1078. " 1.1. We have to indent/outdent all lines equally\n"
  1079. " 1.1.1. to keep the formatting.\n"
  1080. " */",
  1081. format(" /*\n"
  1082. " 1. This is a comment with non-trivial formatting.\n"
  1083. " 1.1. We have to indent/outdent all lines equally\n"
  1084. " 1.1.1. to keep the formatting.\n"
  1085. " */"));
  1086. EXPECT_EQ("/*\n"
  1087. "Don't try to outdent if there's not enough indentation.\n"
  1088. "*/",
  1089. format(" /*\n"
  1090. " Don't try to outdent if there's not enough indentation.\n"
  1091. " */"));
  1092. EXPECT_EQ("int i; /* Comment with empty...\n"
  1093. " *\n"
  1094. " * line. */",
  1095. format("int i; /* Comment with empty...\n"
  1096. " *\n"
  1097. " * line. */"));
  1098. EXPECT_EQ("int foobar = 0; /* comment */\n"
  1099. "int bar = 0; /* multiline\n"
  1100. " comment 1 */\n"
  1101. "int baz = 0; /* multiline\n"
  1102. " comment 2 */\n"
  1103. "int bzz = 0; /* multiline\n"
  1104. " comment 3 */",
  1105. format("int foobar = 0; /* comment */\n"
  1106. "int bar = 0; /* multiline\n"
  1107. " comment 1 */\n"
  1108. "int baz = 0; /* multiline\n"
  1109. " comment 2 */\n"
  1110. "int bzz = 0; /* multiline\n"
  1111. " comment 3 */"));
  1112. EXPECT_EQ("int foobar = 0; /* comment */\n"
  1113. "int bar = 0; /* multiline\n"
  1114. " comment */\n"
  1115. "int baz = 0; /* multiline\n"
  1116. "comment */",
  1117. format("int foobar = 0; /* comment */\n"
  1118. "int bar = 0; /* multiline\n"
  1119. "comment */\n"
  1120. "int baz = 0; /* multiline\n"
  1121. "comment */"));
  1122. }
  1123. TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
  1124. EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1125. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
  1126. format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1127. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
  1128. EXPECT_EQ(
  1129. "void ffffffffffff(\n"
  1130. " int aaaaaaaa, int bbbbbbbb,\n"
  1131. " int cccccccccccc) { /*\n"
  1132. " aaaaaaaaaa\n"
  1133. " aaaaaaaaaaaaa\n"
  1134. " bbbbbbbbbbbbbb\n"
  1135. " bbbbbbbbbb\n"
  1136. " */\n"
  1137. "}",
  1138. format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
  1139. "{ /*\n"
  1140. " aaaaaaaaaa aaaaaaaaaaaaa\n"
  1141. " bbbbbbbbbbbbbb bbbbbbbbbb\n"
  1142. " */\n"
  1143. "}",
  1144. getLLVMStyleWithColumns(40)));
  1145. }
  1146. TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
  1147. EXPECT_EQ("void ffffffffff(\n"
  1148. " int aaaaa /* test */);",
  1149. format("void ffffffffff(int aaaaa /* test */);",
  1150. getLLVMStyleWithColumns(35)));
  1151. }
  1152. TEST_F(FormatTest, SplitsLongCxxComments) {
  1153. EXPECT_EQ("// A comment that\n"
  1154. "// doesn't fit on\n"
  1155. "// one line",
  1156. format("// A comment that doesn't fit on one line",
  1157. getLLVMStyleWithColumns(20)));
  1158. EXPECT_EQ("// a b c d\n"
  1159. "// e f g\n"
  1160. "// h i j k",
  1161. format("// a b c d e f g h i j k",
  1162. getLLVMStyleWithColumns(10)));
  1163. EXPECT_EQ("// a b c d\n"
  1164. "// e f g\n"
  1165. "// h i j k",
  1166. format("\\\n// a b c d e f g h i j k",
  1167. getLLVMStyleWithColumns(10)));
  1168. EXPECT_EQ("if (true) // A comment that\n"
  1169. " // doesn't fit on\n"
  1170. " // one line",
  1171. format("if (true) // A comment that doesn't fit on one line ",
  1172. getLLVMStyleWithColumns(30)));
  1173. EXPECT_EQ("// Don't_touch_leading_whitespace",
  1174. format("// Don't_touch_leading_whitespace",
  1175. getLLVMStyleWithColumns(20)));
  1176. EXPECT_EQ("// Add leading\n"
  1177. "// whitespace",
  1178. format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
  1179. EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
  1180. EXPECT_EQ("// Even if it makes the line exceed the column\n"
  1181. "// limit",
  1182. format("//Even if it makes the line exceed the column limit",
  1183. getLLVMStyleWithColumns(51)));
  1184. EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
  1185. EXPECT_EQ("// aa bb cc dd",
  1186. format("// aa bb cc dd ",
  1187. getLLVMStyleWithColumns(15)));
  1188. EXPECT_EQ("// A comment before\n"
  1189. "// a macro\n"
  1190. "// definition\n"
  1191. "#define a b",
  1192. format("// A comment before a macro definition\n"
  1193. "#define a b",
  1194. getLLVMStyleWithColumns(20)));
  1195. EXPECT_EQ("void ffffff(\n"
  1196. " int aaaaaaaaa, // wwww\n"
  1197. " int bbbbbbbbbb, // xxxxxxx\n"
  1198. " // yyyyyyyyyy\n"
  1199. " int c, int d, int e) {}",
  1200. format("void ffffff(\n"
  1201. " int aaaaaaaaa, // wwww\n"
  1202. " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
  1203. " int c, int d, int e) {}",
  1204. getLLVMStyleWithColumns(40)));
  1205. EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1206. format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1207. getLLVMStyleWithColumns(20)));
  1208. EXPECT_EQ(
  1209. "#define XXX // a b c d\n"
  1210. " // e f g h",
  1211. format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
  1212. EXPECT_EQ(
  1213. "#define XXX // q w e r\n"
  1214. " // t y u i",
  1215. format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
  1216. }
  1217. TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {
  1218. EXPECT_EQ("// A comment\n"
  1219. "// that doesn't\n"
  1220. "// fit on one\n"
  1221. "// line",
  1222. format("// A comment that doesn't fit on one line",
  1223. getLLVMStyleWithColumns(20)));
  1224. EXPECT_EQ("/// A comment\n"
  1225. "/// that doesn't\n"
  1226. "/// fit on one\n"
  1227. "/// line",
  1228. format("/// A comment that doesn't fit on one line",
  1229. getLLVMStyleWithColumns(20)));
  1230. }
  1231. TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
  1232. EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1233. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1234. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1235. format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1236. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1237. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
  1238. EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1239. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1240. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1241. format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1242. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1243. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1244. getLLVMStyleWithColumns(50)));
  1245. // FIXME: One day we might want to implement adjustment of leading whitespace
  1246. // of the consecutive lines in this kind of comment:
  1247. EXPECT_EQ("double\n"
  1248. " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1249. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1250. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1251. format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1252. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1253. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1254. getLLVMStyleWithColumns(49)));
  1255. }
  1256. TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) {
  1257. FormatStyle Pragmas = getLLVMStyleWithColumns(30);
  1258. Pragmas.CommentPragmas = "^ IWYU pragma:";
  1259. EXPECT_EQ(
  1260. "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
  1261. format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
  1262. EXPECT_EQ(
  1263. "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
  1264. format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
  1265. }
  1266. TEST_F(FormatTest, PriorityOfCommentBreaking) {
  1267. EXPECT_EQ("if (xxx ==\n"
  1268. " yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  1269. " zzz)\n"
  1270. " q();",
  1271. format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  1272. " zzz) q();",
  1273. getLLVMStyleWithColumns(40)));
  1274. EXPECT_EQ("if (xxxxxxxxxx ==\n"
  1275. " yyy && // aaaaaa bbbbbbbb cccc\n"
  1276. " zzz)\n"
  1277. " q();",
  1278. format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
  1279. " zzz) q();",
  1280. getLLVMStyleWithColumns(40)));
  1281. EXPECT_EQ("if (xxxxxxxxxx &&\n"
  1282. " yyy || // aaaaaa bbbbbbbb cccc\n"
  1283. " zzz)\n"
  1284. " q();",
  1285. format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
  1286. " zzz) q();",
  1287. getLLVMStyleWithColumns(40)));
  1288. EXPECT_EQ("fffffffff(\n"
  1289. " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  1290. " zzz);",
  1291. format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  1292. " zzz);",
  1293. getLLVMStyleWithColumns(40)));
  1294. }
  1295. TEST_F(FormatTest, MultiLineCommentsInDefines) {
  1296. EXPECT_EQ("#define A(x) /* \\\n"
  1297. " a comment \\\n"
  1298. " inside */ \\\n"
  1299. " f();",
  1300. format("#define A(x) /* \\\n"
  1301. " a comment \\\n"
  1302. " inside */ \\\n"
  1303. " f();",
  1304. getLLVMStyleWithColumns(17)));
  1305. EXPECT_EQ("#define A( \\\n"
  1306. " x) /* \\\n"
  1307. " a comment \\\n"
  1308. " inside */ \\\n"
  1309. " f();",
  1310. format("#define A( \\\n"
  1311. " x) /* \\\n"
  1312. " a comment \\\n"
  1313. " inside */ \\\n"
  1314. " f();",
  1315. getLLVMStyleWithColumns(17)));
  1316. }
  1317. TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
  1318. EXPECT_EQ("namespace {}\n// Test\n#define A",
  1319. format("namespace {}\n // Test\n#define A"));
  1320. EXPECT_EQ("namespace {}\n/* Test */\n#define A",
  1321. format("namespace {}\n /* Test */\n#define A"));
  1322. EXPECT_EQ("namespace {}\n/* Test */ #define A",
  1323. format("namespace {}\n /* Test */ #define A"));
  1324. }
  1325. TEST_F(FormatTest, SplitsLongLinesInComments) {
  1326. EXPECT_EQ("/* This is a long\n"
  1327. " * comment that\n"
  1328. " * doesn't\n"
  1329. " * fit on one line.\n"
  1330. " */",
  1331. format("/* "
  1332. "This is a long "
  1333. "comment that "
  1334. "doesn't "
  1335. "fit on one line. */",
  1336. getLLVMStyleWithColumns(20)));
  1337. EXPECT_EQ("/* a b c d\n"
  1338. " * e f g\n"
  1339. " * h i j k\n"
  1340. " */",
  1341. format("/* a b c d e f g h i j k */",
  1342. getLLVMStyleWithColumns(10)));
  1343. EXPECT_EQ("/* a b c d\n"
  1344. " * e f g\n"
  1345. " * h i j k\n"
  1346. " */",
  1347. format("\\\n/* a b c d e f g h i j k */",
  1348. getLLVMStyleWithColumns(10)));
  1349. EXPECT_EQ("/*\n"
  1350. "This is a long\n"
  1351. "comment that doesn't\n"
  1352. "fit on one line.\n"
  1353. "*/",
  1354. format("/*\n"
  1355. "This is a long "
  1356. "comment that doesn't "
  1357. "fit on one line. \n"
  1358. "*/", getLLVMStyleWithColumns(20)));
  1359. EXPECT_EQ("/*\n"
  1360. " * This is a long\n"
  1361. " * comment that\n"
  1362. " * doesn't fit on\n"
  1363. " * one line.\n"
  1364. " */",
  1365. format("/* \n"
  1366. " * This is a long "
  1367. " comment that "
  1368. " doesn't fit on "
  1369. " one line. \n"
  1370. " */", getLLVMStyleWithColumns(20)));
  1371. EXPECT_EQ("/*\n"
  1372. " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
  1373. " * so_it_should_be_broken\n"
  1374. " * wherever_a_space_occurs\n"
  1375. " */",
  1376. format("/*\n"
  1377. " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
  1378. " so_it_should_be_broken "
  1379. " wherever_a_space_occurs \n"
  1380. " */",
  1381. getLLVMStyleWithColumns(20)));
  1382. EXPECT_EQ("/*\n"
  1383. " * This_comment_can_not_be_broken_into_lines\n"
  1384. " */",
  1385. format("/*\n"
  1386. " * This_comment_can_not_be_broken_into_lines\n"
  1387. " */",
  1388. getLLVMStyleWithColumns(20)));
  1389. EXPECT_EQ("{\n"
  1390. " /*\n"
  1391. " This is another\n"
  1392. " long comment that\n"
  1393. " doesn't fit on one\n"
  1394. " line 1234567890\n"
  1395. " */\n"
  1396. "}",
  1397. format("{\n"
  1398. "/*\n"
  1399. "This is another "
  1400. " long comment that "
  1401. " doesn't fit on one"
  1402. " line 1234567890\n"
  1403. "*/\n"
  1404. "}", getLLVMStyleWithColumns(20)));
  1405. EXPECT_EQ("{\n"
  1406. " /*\n"
  1407. " * This i s\n"
  1408. " * another comment\n"
  1409. " * t hat doesn' t\n"
  1410. " * fit on one l i\n"
  1411. " * n e\n"
  1412. " */\n"
  1413. "}",
  1414. format("{\n"
  1415. "/*\n"
  1416. " * This i s"
  1417. " another comment"
  1418. " t hat doesn' t"
  1419. " fit on one l i"
  1420. " n e\n"
  1421. " */\n"
  1422. "}", getLLVMStyleWithColumns(20)));
  1423. EXPECT_EQ("/*\n"
  1424. " * This is a long\n"
  1425. " * comment that\n"
  1426. " * doesn't fit on\n"
  1427. " * one line\n"
  1428. " */",
  1429. format(" /*\n"
  1430. " * This is a long comment that doesn't fit on one line\n"
  1431. " */", getLLVMStyleWithColumns(20)));
  1432. EXPECT_EQ("{\n"
  1433. " if (something) /* This is a\n"
  1434. " long\n"
  1435. " comment */\n"
  1436. " ;\n"
  1437. "}",
  1438. format("{\n"
  1439. " if (something) /* This is a long comment */\n"
  1440. " ;\n"
  1441. "}",
  1442. getLLVMStyleWithColumns(30)));
  1443. EXPECT_EQ("/* A comment before\n"
  1444. " * a macro\n"
  1445. " * definition */\n"
  1446. "#define a b",
  1447. format("/* A comment before a macro definition */\n"
  1448. "#define a b",
  1449. getLLVMStyleWithColumns(20)));
  1450. EXPECT_EQ("/* some comment\n"
  1451. " * a comment\n"
  1452. "* that we break\n"
  1453. " * another comment\n"
  1454. "* we have to break\n"
  1455. "* a left comment\n"
  1456. " */",
  1457. format(" /* some comment\n"
  1458. " * a comment that we break\n"
  1459. " * another comment we have to break\n"
  1460. "* a left comment\n"
  1461. " */",
  1462. getLLVMStyleWithColumns(20)));
  1463. EXPECT_EQ("/*\n"
  1464. "\n"
  1465. "\n"
  1466. " */\n",
  1467. format(" /* \n"
  1468. " \n"
  1469. " \n"
  1470. " */\n"));
  1471. EXPECT_EQ("/* a a */",
  1472. format("/* a a */", getLLVMStyleWithColumns(15)));
  1473. EXPECT_EQ("/* a a bc */",
  1474. format("/* a a bc */", getLLVMStyleWithColumns(15)));
  1475. EXPECT_EQ("/* aaa aaa\n"
  1476. " * aaaaa */",
  1477. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1478. EXPECT_EQ("/* aaa aaa\n"
  1479. " * aaaaa */",
  1480. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1481. }
  1482. TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
  1483. EXPECT_EQ("#define X \\\n"
  1484. " /* \\\n"
  1485. " Test \\\n"
  1486. " Macro comment \\\n"
  1487. " with a long \\\n"
  1488. " line \\\n"
  1489. " */ \\\n"
  1490. " A + B",
  1491. format("#define X \\\n"
  1492. " /*\n"
  1493. " Test\n"
  1494. " Macro comment with a long line\n"
  1495. " */ \\\n"
  1496. " A + B",
  1497. getLLVMStyleWithColumns(20)));
  1498. EXPECT_EQ("#define X \\\n"
  1499. " /* Macro comment \\\n"
  1500. " with a long \\\n"
  1501. " line */ \\\n"
  1502. " A + B",
  1503. format("#define X \\\n"
  1504. " /* Macro comment with a long\n"
  1505. " line */ \\\n"
  1506. " A + B",
  1507. getLLVMStyleWithColumns(20)));
  1508. EXPECT_EQ("#define X \\\n"
  1509. " /* Macro comment \\\n"
  1510. " * with a long \\\n"
  1511. " * line */ \\\n"
  1512. " A + B",
  1513. format("#define X \\\n"
  1514. " /* Macro comment with a long line */ \\\n"
  1515. " A + B",
  1516. getLLVMStyleWithColumns(20)));
  1517. }
  1518. TEST_F(FormatTest, CommentsInStaticInitializers) {
  1519. EXPECT_EQ(
  1520. "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
  1521. " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
  1522. " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
  1523. " aaaaaaaaaaaaaaaaaaaa, // comment\n"
  1524. " aaaaaaaaaaaaaaaaaaaa};",
  1525. format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
  1526. " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
  1527. " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
  1528. " aaaaaaaaaaaaaaaaaaaa , // comment\n"
  1529. " aaaaaaaaaaaaaaaaaaaa };"));
  1530. verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1531. " bbbbbbbbbbb, ccccccccccc};");
  1532. verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1533. " // comment for bb....\n"
  1534. " bbbbbbbbbbb, ccccccccccc};");
  1535. verifyGoogleFormat(
  1536. "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1537. " bbbbbbbbbbb, ccccccccccc};");
  1538. verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1539. " // comment for bb....\n"
  1540. " bbbbbbbbbbb, ccccccccccc};");
  1541. verifyFormat("S s = {{a, b, c}, // Group #1\n"
  1542. " {d, e, f}, // Group #2\n"
  1543. " {g, h, i}}; // Group #3");
  1544. verifyFormat("S s = {{// Group #1\n"
  1545. " a, b, c},\n"
  1546. " {// Group #2\n"
  1547. " d, e, f},\n"
  1548. " {// Group #3\n"
  1549. " g, h, i}};");
  1550. EXPECT_EQ("S s = {\n"
  1551. " // Some comment\n"
  1552. " a,\n"
  1553. "\n"
  1554. " // Comment after empty line\n"
  1555. " b}",
  1556. format("S s = {\n"
  1557. " // Some comment\n"
  1558. " a,\n"
  1559. " \n"
  1560. " // Comment after empty line\n"
  1561. " b\n"
  1562. "}"));
  1563. EXPECT_EQ("S s = {\n"
  1564. " /* Some comment */\n"
  1565. " a,\n"
  1566. "\n"
  1567. " /* Comment after empty line */\n"
  1568. " b}",
  1569. format("S s = {\n"
  1570. " /* Some comment */\n"
  1571. " a,\n"
  1572. " \n"
  1573. " /* Comment after empty line */\n"
  1574. " b\n"
  1575. "}"));
  1576. verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
  1577. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1578. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1579. " 0x00, 0x00, 0x00, 0x00}; // comment\n");
  1580. }
  1581. TEST_F(FormatTest, IgnoresIf0Contents) {
  1582. EXPECT_EQ("#if 0\n"
  1583. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1584. "#endif\n"
  1585. "void f() {}",
  1586. format("#if 0\n"
  1587. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1588. "#endif\n"
  1589. "void f( ) { }"));
  1590. EXPECT_EQ("#if false\n"
  1591. "void f( ) { }\n"
  1592. "#endif\n"
  1593. "void g() {}\n",
  1594. format("#if false\n"
  1595. "void f( ) { }\n"
  1596. "#endif\n"
  1597. "void g( ) { }\n"));
  1598. EXPECT_EQ("enum E {\n"
  1599. " One,\n"
  1600. " Two,\n"
  1601. "#if 0\n"
  1602. "Three,\n"
  1603. " Four,\n"
  1604. "#endif\n"
  1605. " Five\n"
  1606. "};",
  1607. format("enum E {\n"
  1608. " One,Two,\n"
  1609. "#if 0\n"
  1610. "Three,\n"
  1611. " Four,\n"
  1612. "#endif\n"
  1613. " Five};"));
  1614. EXPECT_EQ("enum F {\n"
  1615. " One,\n"
  1616. "#if 1\n"
  1617. " Two,\n"
  1618. "#if 0\n"
  1619. "Three,\n"
  1620. " Four,\n"
  1621. "#endif\n"
  1622. " Five\n"
  1623. "#endif\n"
  1624. "};",
  1625. format("enum F {\n"
  1626. "One,\n"
  1627. "#if 1\n"
  1628. "Two,\n"
  1629. "#if 0\n"
  1630. "Three,\n"
  1631. " Four,\n"
  1632. "#endif\n"
  1633. "Five\n"
  1634. "#endif\n"
  1635. "};"));
  1636. EXPECT_EQ("enum G {\n"
  1637. " One,\n"
  1638. "#if 0\n"
  1639. "Two,\n"
  1640. "#else\n"
  1641. " Three,\n"
  1642. "#endif\n"
  1643. " Four\n"
  1644. "};",
  1645. format("enum G {\n"
  1646. "One,\n"
  1647. "#if 0\n"
  1648. "Two,\n"
  1649. "#else\n"
  1650. "Three,\n"
  1651. "#endif\n"
  1652. "Four\n"
  1653. "};"));
  1654. EXPECT_EQ("enum H {\n"
  1655. " One,\n"
  1656. "#if 0\n"
  1657. "#ifdef Q\n"
  1658. "Two,\n"
  1659. "#else\n"
  1660. "Three,\n"
  1661. "#endif\n"
  1662. "#endif\n"
  1663. " Four\n"
  1664. "};",
  1665. format("enum H {\n"
  1666. "One,\n"
  1667. "#if 0\n"
  1668. "#ifdef Q\n"
  1669. "Two,\n"
  1670. "#else\n"
  1671. "Three,\n"
  1672. "#endif\n"
  1673. "#endif\n"
  1674. "Four\n"
  1675. "};"));
  1676. EXPECT_EQ("enum I {\n"
  1677. " One,\n"
  1678. "#if /* test */ 0 || 1\n"
  1679. "Two,\n"
  1680. "Three,\n"
  1681. "#endif\n"
  1682. " Four\n"
  1683. "};",
  1684. format("enum I {\n"
  1685. "One,\n"
  1686. "#if /* test */ 0 || 1\n"
  1687. "Two,\n"
  1688. "Three,\n"
  1689. "#endif\n"
  1690. "Four\n"
  1691. "};"));
  1692. EXPECT_EQ("enum J {\n"
  1693. " One,\n"
  1694. "#if 0\n"
  1695. "#if 0\n"
  1696. "Two,\n"
  1697. "#else\n"
  1698. "Three,\n"
  1699. "#endif\n"
  1700. "Four,\n"
  1701. "#endif\n"
  1702. " Five\n"
  1703. "};",
  1704. format("enum J {\n"
  1705. "One,\n"
  1706. "#if 0\n"
  1707. "#if 0\n"
  1708. "Two,\n"
  1709. "#else\n"
  1710. "Three,\n"
  1711. "#endif\n"
  1712. "Four,\n"
  1713. "#endif\n"
  1714. "Five\n"
  1715. "};"));
  1716. }
  1717. //===----------------------------------------------------------------------===//
  1718. // Tests for classes, namespaces, etc.
  1719. //===----------------------------------------------------------------------===//
  1720. TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
  1721. verifyFormat("class A {};");
  1722. }
  1723. TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
  1724. verifyFormat("class A {\n"
  1725. "public:\n"
  1726. "public: // comment\n"
  1727. "protected:\n"
  1728. "private:\n"
  1729. " void f() {}\n"
  1730. "};");
  1731. verifyGoogleFormat("class A {\n"
  1732. " public:\n"
  1733. " protected:\n"
  1734. " private:\n"
  1735. " void f() {}\n"
  1736. "};");
  1737. verifyFormat("class A {\n"
  1738. "public slots:\n"
  1739. " void f() {}\n"
  1740. "public Q_SLOTS:\n"
  1741. " void f() {}\n"
  1742. "};");
  1743. }
  1744. TEST_F(FormatTest, SeparatesLogicalBlocks) {
  1745. EXPECT_EQ("class A {\n"
  1746. "public:\n"
  1747. " void f();\n"
  1748. "\n"
  1749. "private:\n"
  1750. " void g() {}\n"
  1751. " // test\n"
  1752. "protected:\n"
  1753. " int h;\n"
  1754. "};",
  1755. format("class A {\n"
  1756. "public:\n"
  1757. "void f();\n"
  1758. "private:\n"
  1759. "void g() {}\n"
  1760. "// test\n"
  1761. "protected:\n"
  1762. "int h;\n"
  1763. "};"));
  1764. EXPECT_EQ("class A {\n"
  1765. "protected:\n"
  1766. "public:\n"
  1767. " void f();\n"
  1768. "};",
  1769. format("class A {\n"
  1770. "protected:\n"
  1771. "\n"
  1772. "public:\n"
  1773. "\n"
  1774. " void f();\n"
  1775. "};"));
  1776. }
  1777. TEST_F(FormatTest, FormatsClasses) {
  1778. verifyFormat("class A : public B {};");
  1779. verifyFormat("class A : public ::B {};");
  1780. verifyFormat(
  1781. "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1782. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1783. verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
  1784. " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1785. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1786. verifyFormat(
  1787. "class A : public B, public C, public D, public E, public F {};");
  1788. verifyFormat("class AAAAAAAAAAAA : public B,\n"
  1789. " public C,\n"
  1790. " public D,\n"
  1791. " public E,\n"
  1792. " public F,\n"
  1793. " public G {};");
  1794. verifyFormat("class\n"
  1795. " ReallyReallyLongClassName {\n"
  1796. " int i;\n"
  1797. "};",
  1798. getLLVMStyleWithColumns(32));
  1799. verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
  1800. " aaaaaaaaaaaaaaaa> {};");
  1801. verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
  1802. " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
  1803. " aaaaaaaaaaaaaaaaaaaaaa> {};");
  1804. verifyFormat("template <class R, class C>\n"
  1805. "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
  1806. " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
  1807. verifyFormat("class ::A::B {};");
  1808. }
  1809. TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
  1810. verifyFormat("class A {\n} a, b;");
  1811. verifyFormat("struct A {\n} a, b;");
  1812. verifyFormat("union A {\n} a;");
  1813. }
  1814. TEST_F(FormatTest, FormatsEnum) {
  1815. verifyFormat("enum {\n"
  1816. " Zero,\n"
  1817. " One = 1,\n"
  1818. " Two = One + 1,\n"
  1819. " Three = (One + Two),\n"
  1820. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1821. " Five = (One, Two, Three, Four, 5)\n"
  1822. "};");
  1823. verifyGoogleFormat("enum {\n"
  1824. " Zero,\n"
  1825. " One = 1,\n"
  1826. " Two = One + 1,\n"
  1827. " Three = (One + Two),\n"
  1828. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1829. " Five = (One, Two, Three, Four, 5)\n"
  1830. "};");
  1831. verifyFormat("enum Enum {};");
  1832. verifyFormat("enum {};");
  1833. verifyFormat("enum X E {} d;");
  1834. verifyFormat("enum __attribute__((...)) E {} d;");
  1835. verifyFormat("enum __declspec__((...)) E {} d;");
  1836. verifyFormat("enum X f() {\n a();\n return 42;\n}");
  1837. verifyFormat("enum {\n"
  1838. " Bar = Foo<int, int>::value\n"
  1839. "};",
  1840. getLLVMStyleWithColumns(30));
  1841. verifyFormat("enum ShortEnum { A, B, C };");
  1842. verifyGoogleFormat("enum ShortEnum { A, B, C };");
  1843. EXPECT_EQ("enum KeepEmptyLines {\n"
  1844. " ONE,\n"
  1845. "\n"
  1846. " TWO,\n"
  1847. "\n"
  1848. " THREE\n"
  1849. "}",
  1850. format("enum KeepEmptyLines {\n"
  1851. " ONE,\n"
  1852. "\n"
  1853. " TWO,\n"
  1854. "\n"
  1855. "\n"
  1856. " THREE\n"
  1857. "}"));
  1858. verifyFormat("enum E { // comment\n"
  1859. " ONE,\n"
  1860. " TWO\n"
  1861. "};\n"
  1862. "int i;");
  1863. }
  1864. TEST_F(FormatTest, FormatsEnumsWithErrors) {
  1865. verifyFormat("enum Type {\n"
  1866. " One = 0; // These semicolons should be commas.\n"
  1867. " Two = 1;\n"
  1868. "};");
  1869. verifyFormat("namespace n {\n"
  1870. "enum Type {\n"
  1871. " One,\n"
  1872. " Two, // missing };\n"
  1873. " int i;\n"
  1874. "}\n"
  1875. "void g() {}");
  1876. }
  1877. TEST_F(FormatTest, FormatsEnumStruct) {
  1878. verifyFormat("enum struct {\n"
  1879. " Zero,\n"
  1880. " One = 1,\n"
  1881. " Two = One + 1,\n"
  1882. " Three = (One + Two),\n"
  1883. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1884. " Five = (One, Two, Three, Four, 5)\n"
  1885. "};");
  1886. verifyFormat("enum struct Enum {};");
  1887. verifyFormat("enum struct {};");
  1888. verifyFormat("enum struct X E {} d;");
  1889. verifyFormat("enum struct __attribute__((...)) E {} d;");
  1890. verifyFormat("enum struct __declspec__((...)) E {} d;");
  1891. verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
  1892. }
  1893. TEST_F(FormatTest, FormatsEnumClass) {
  1894. verifyFormat("enum class {\n"
  1895. " Zero,\n"
  1896. " One = 1,\n"
  1897. " Two = One + 1,\n"
  1898. " Three = (One + Two),\n"
  1899. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1900. " Five = (One, Two, Three, Four, 5)\n"
  1901. "};");
  1902. verifyFormat("enum class Enum {};");
  1903. verifyFormat("enum class {};");
  1904. verifyFormat("enum class X E {} d;");
  1905. verifyFormat("enum class __attribute__((...)) E {} d;");
  1906. verifyFormat("enum class __declspec__((...)) E {} d;");
  1907. verifyFormat("enum class X f() {\n a();\n return 42;\n}");
  1908. }
  1909. TEST_F(FormatTest, FormatsEnumTypes) {
  1910. verifyFormat("enum X : int {\n"
  1911. " A, // Force multiple lines.\n"
  1912. " B\n"
  1913. "};");
  1914. verifyFormat("enum X : int { A, B };");
  1915. verifyFormat("enum X : std::uint32_t { A, B };");
  1916. }
  1917. TEST_F(FormatTest, FormatsNSEnums) {
  1918. verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
  1919. verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
  1920. " // Information about someDecentlyLongValue.\n"
  1921. " someDecentlyLongValue,\n"
  1922. " // Information about anotherDecentlyLongValue.\n"
  1923. " anotherDecentlyLongValue,\n"
  1924. " // Information about aThirdDecentlyLongValue.\n"
  1925. " aThirdDecentlyLongValue\n"
  1926. "};");
  1927. }
  1928. TEST_F(FormatTest, FormatsBitfields) {
  1929. verifyFormat("struct Bitfields {\n"
  1930. " unsigned sClass : 8;\n"
  1931. " unsigned ValueKind : 2;\n"
  1932. "};");
  1933. verifyFormat("struct A {\n"
  1934. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
  1935. " bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
  1936. "};");
  1937. }
  1938. TEST_F(FormatTest, FormatsNamespaces) {
  1939. verifyFormat("namespace some_namespace {\n"
  1940. "class A {};\n"
  1941. "void f() { f(); }\n"
  1942. "}");
  1943. verifyFormat("namespace {\n"
  1944. "class A {};\n"
  1945. "void f() { f(); }\n"
  1946. "}");
  1947. verifyFormat("inline namespace X {\n"
  1948. "class A {};\n"
  1949. "void f() { f(); }\n"
  1950. "}");
  1951. verifyFormat("using namespace some_namespace;\n"
  1952. "class A {};\n"
  1953. "void f() { f(); }");
  1954. // This code is more common than we thought; if we
  1955. // layout this correctly the semicolon will go into
  1956. // its own line, which is undesirable.
  1957. verifyFormat("namespace {};");
  1958. verifyFormat("namespace {\n"
  1959. "class A {};\n"
  1960. "};");
  1961. verifyFormat("namespace {\n"
  1962. "int SomeVariable = 0; // comment\n"
  1963. "} // namespace");
  1964. EXPECT_EQ("#ifndef HEADER_GUARD\n"
  1965. "#define HEADER_GUARD\n"
  1966. "namespace my_namespace {\n"
  1967. "int i;\n"
  1968. "} // my_namespace\n"
  1969. "#endif // HEADER_GUARD",
  1970. format("#ifndef HEADER_GUARD\n"
  1971. " #define HEADER_GUARD\n"
  1972. " namespace my_namespace {\n"
  1973. "int i;\n"
  1974. "} // my_namespace\n"
  1975. "#endif // HEADER_GUARD"));
  1976. FormatStyle Style = getLLVMStyle();
  1977. Style.NamespaceIndentation = FormatStyle::NI_All;
  1978. EXPECT_EQ("namespace out {\n"
  1979. " int i;\n"
  1980. " namespace in {\n"
  1981. " int i;\n"
  1982. " } // namespace\n"
  1983. "} // namespace",
  1984. format("namespace out {\n"
  1985. "int i;\n"
  1986. "namespace in {\n"
  1987. "int i;\n"
  1988. "} // namespace\n"
  1989. "} // namespace",
  1990. Style));
  1991. Style.NamespaceIndentation = FormatStyle::NI_Inner;
  1992. EXPECT_EQ("namespace out {\n"
  1993. "int i;\n"
  1994. "namespace in {\n"
  1995. " int i;\n"
  1996. "} // namespace\n"
  1997. "} // namespace",
  1998. format("namespace out {\n"
  1999. "int i;\n"
  2000. "namespace in {\n"
  2001. "int i;\n"
  2002. "} // namespace\n"
  2003. "} // namespace",
  2004. Style));
  2005. }
  2006. TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
  2007. TEST_F(FormatTest, FormatsInlineASM) {
  2008. verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
  2009. verifyFormat("asm(\"nop\" ::: \"memory\");");
  2010. verifyFormat(
  2011. "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
  2012. " \"cpuid\\n\\t\"\n"
  2013. " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
  2014. " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
  2015. " : \"a\"(value));");
  2016. EXPECT_EQ(
  2017. "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
  2018. " __asm {\n"
  2019. " mov edx,[that] // vtable in edx\n"
  2020. " mov eax,methodIndex\n"
  2021. " call [edx][eax*4] // stdcall\n"
  2022. " }\n"
  2023. "}",
  2024. format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
  2025. " __asm {\n"
  2026. " mov edx,[that] // vtable in edx\n"
  2027. " mov eax,methodIndex\n"
  2028. " call [edx][eax*4] // stdcall\n"
  2029. " }\n"
  2030. "}"));
  2031. }
  2032. TEST_F(FormatTest, FormatTryCatch) {
  2033. verifyFormat("try {\n"
  2034. " throw a * b;\n"
  2035. "} catch (int a) {\n"
  2036. " // Do nothing.\n"
  2037. "} catch (...) {\n"
  2038. " exit(42);\n"
  2039. "}");
  2040. // Function-level try statements.
  2041. verifyFormat("int f() try { return 4; } catch (...) {\n"
  2042. " return 5;\n"
  2043. "}");
  2044. verifyFormat("class A {\n"
  2045. " int a;\n"
  2046. " A() try : a(0) {\n"
  2047. " } catch (...) {\n"
  2048. " throw;\n"
  2049. " }\n"
  2050. "};\n");
  2051. }
  2052. TEST_F(FormatTest, IncompleteTryCatchBlocks) {
  2053. verifyFormat("try {\n"
  2054. " f();\n"
  2055. "} catch {\n"
  2056. " g();\n"
  2057. "}");
  2058. verifyFormat("try {\n"
  2059. " f();\n"
  2060. "} catch (A a) MACRO(x) {\n"
  2061. " g();\n"
  2062. "} catch (B b) MACRO(x) {\n"
  2063. " g();\n"
  2064. "}");
  2065. }
  2066. TEST_F(FormatTest, FormatTryCatchBraceStyles) {
  2067. FormatStyle Style = getLLVMStyle();
  2068. Style.BreakBeforeBraces = FormatStyle::BS_Attach;
  2069. verifyFormat("try {\n"
  2070. " // something\n"
  2071. "} catch (...) {\n"
  2072. " // something\n"
  2073. "}",
  2074. Style);
  2075. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  2076. verifyFormat("try {\n"
  2077. " // something\n"
  2078. "}\n"
  2079. "catch (...) {\n"
  2080. " // something\n"
  2081. "}",
  2082. Style);
  2083. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  2084. verifyFormat("try\n"
  2085. "{\n"
  2086. " // something\n"
  2087. "}\n"
  2088. "catch (...)\n"
  2089. "{\n"
  2090. " // something\n"
  2091. "}",
  2092. Style);
  2093. Style.BreakBeforeBraces = FormatStyle::BS_GNU;
  2094. verifyFormat("try\n"
  2095. " {\n"
  2096. " // something\n"
  2097. " }\n"
  2098. "catch (...)\n"
  2099. " {\n"
  2100. " // something\n"
  2101. " }",
  2102. Style);
  2103. }
  2104. TEST_F(FormatTest, FormatObjCTryCatch) {
  2105. verifyFormat("@try {\n"
  2106. " f();\n"
  2107. "}\n"
  2108. "@catch (NSException e) {\n"
  2109. " @throw;\n"
  2110. "}\n"
  2111. "@finally {\n"
  2112. " exit(42);\n"
  2113. "}");
  2114. }
  2115. TEST_F(FormatTest, StaticInitializers) {
  2116. verifyFormat("static SomeClass SC = {1, 'a'};");
  2117. verifyFormat(
  2118. "static SomeClass WithALoooooooooooooooooooongName = {\n"
  2119. " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
  2120. // Here, everything other than the "}" would fit on a line.
  2121. verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
  2122. " 10000000000000000000000000};");
  2123. EXPECT_EQ("S s = {a,\n"
  2124. "\n"
  2125. " b};",
  2126. format("S s = {\n"
  2127. " a,\n"
  2128. "\n"
  2129. " b\n"
  2130. "};"));
  2131. // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
  2132. // line. However, the formatting looks a bit off and this probably doesn't
  2133. // happen often in practice.
  2134. verifyFormat("static int Variable[1] = {\n"
  2135. " {1000000000000000000000000000000000000}};",
  2136. getLLVMStyleWithColumns(40));
  2137. }
  2138. TEST_F(FormatTest, DesignatedInitializers) {
  2139. verifyFormat("const struct A a = {.a = 1, .b = 2};");
  2140. verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
  2141. " .bbbbbbbbbb = 2,\n"
  2142. " .cccccccccc = 3,\n"
  2143. " .dddddddddd = 4,\n"
  2144. " .eeeeeeeeee = 5};");
  2145. verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
  2146. " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
  2147. " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
  2148. " .ccccccccccccccccccccccccccc = 3,\n"
  2149. " .ddddddddddddddddddddddddddd = 4,\n"
  2150. " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
  2151. verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
  2152. }
  2153. TEST_F(FormatTest, NestedStaticInitializers) {
  2154. verifyFormat("static A x = {{{}}};\n");
  2155. verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
  2156. " {init1, init2, init3, init4}}};",
  2157. getLLVMStyleWithColumns(50));
  2158. verifyFormat("somes Status::global_reps[3] = {\n"
  2159. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  2160. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  2161. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
  2162. getLLVMStyleWithColumns(60));
  2163. verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
  2164. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  2165. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  2166. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
  2167. verifyFormat(
  2168. "CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
  2169. " {rect.fRight - rect.fLeft, rect.fBottom - rect.fTop}};");
  2170. verifyFormat(
  2171. "SomeArrayOfSomeType a = {\n"
  2172. " {{1, 2, 3},\n"
  2173. " {1, 2, 3},\n"
  2174. " {111111111111111111111111111111, 222222222222222222222222222222,\n"
  2175. " 333333333333333333333333333333},\n"
  2176. " {1, 2, 3},\n"
  2177. " {1, 2, 3}}};");
  2178. verifyFormat(
  2179. "SomeArrayOfSomeType a = {\n"
  2180. " {{1, 2, 3}},\n"
  2181. " {{1, 2, 3}},\n"
  2182. " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
  2183. " 333333333333333333333333333333}},\n"
  2184. " {{1, 2, 3}},\n"
  2185. " {{1, 2, 3}}};");
  2186. verifyFormat(
  2187. "struct {\n"
  2188. " unsigned bit;\n"
  2189. " const char *const name;\n"
  2190. "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
  2191. " {kOsWin, \"Windows\"},\n"
  2192. " {kOsLinux, \"Linux\"},\n"
  2193. " {kOsCrOS, \"Chrome OS\"}};");
  2194. verifyFormat(
  2195. "struct {\n"
  2196. " unsigned bit;\n"
  2197. " const char *const name;\n"
  2198. "} kBitsToOs[] = {\n"
  2199. " {kOsMac, \"Mac\"},\n"
  2200. " {kOsWin, \"Windows\"},\n"
  2201. " {kOsLinux, \"Linux\"},\n"
  2202. " {kOsCrOS, \"Chrome OS\"},\n"
  2203. "};");
  2204. }
  2205. TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
  2206. verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2207. " \\\n"
  2208. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
  2209. }
  2210. TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
  2211. verifyFormat("virtual void write(ELFWriter *writerrr,\n"
  2212. " OwningPtr<FileOutputBuffer> &buffer) = 0;");
  2213. }
  2214. TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
  2215. verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
  2216. getLLVMStyleWithColumns(40));
  2217. verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  2218. getLLVMStyleWithColumns(40));
  2219. EXPECT_EQ("#define Q \\\n"
  2220. " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n"
  2221. " \"aaaaaaaa.cpp\"",
  2222. format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  2223. getLLVMStyleWithColumns(40)));
  2224. }
  2225. TEST_F(FormatTest, UnderstandsLinePPDirective) {
  2226. EXPECT_EQ("# 123 \"A string literal\"",
  2227. format(" # 123 \"A string literal\""));
  2228. }
  2229. TEST_F(FormatTest, LayoutUnknownPPDirective) {
  2230. EXPECT_EQ("#;", format("#;"));
  2231. verifyFormat("#\n;\n;\n;");
  2232. }
  2233. TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
  2234. EXPECT_EQ("#line 42 \"test\"\n",
  2235. format("# \\\n line \\\n 42 \\\n \"test\"\n"));
  2236. EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n",
  2237. getLLVMStyleWithColumns(12)));
  2238. }
  2239. TEST_F(FormatTest, EndOfFileEndsPPDirective) {
  2240. EXPECT_EQ("#line 42 \"test\"",
  2241. format("# \\\n line \\\n 42 \\\n \"test\""));
  2242. EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B"));
  2243. }
  2244. TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
  2245. verifyFormat("#define A \\x20");
  2246. verifyFormat("#define A \\ x20");
  2247. EXPECT_EQ("#define A \\ x20", format("#define A \\ x20"));
  2248. verifyFormat("#define A ''");
  2249. verifyFormat("#define A ''qqq");
  2250. verifyFormat("#define A `qqq");
  2251. verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
  2252. EXPECT_EQ("const char *c = STRINGIFY(\n"
  2253. "\\na : b);",
  2254. format("const char * c = STRINGIFY(\n"
  2255. "\\na : b);"));
  2256. }
  2257. TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
  2258. verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
  2259. verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
  2260. verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
  2261. // FIXME: We never break before the macro name.
  2262. verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
  2263. verifyFormat("#define A A\n#define A A");
  2264. verifyFormat("#define A(X) A\n#define A A");
  2265. verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
  2266. verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
  2267. }
  2268. TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
  2269. EXPECT_EQ("// somecomment\n"
  2270. "#include \"a.h\"\n"
  2271. "#define A( \\\n"
  2272. " A, B)\n"
  2273. "#include \"b.h\"\n"
  2274. "// somecomment\n",
  2275. format(" // somecomment\n"
  2276. " #include \"a.h\"\n"
  2277. "#define A(A,\\\n"
  2278. " B)\n"
  2279. " #include \"b.h\"\n"
  2280. " // somecomment\n",
  2281. getLLVMStyleWithColumns(13)));
  2282. }
  2283. TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
  2284. TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
  2285. EXPECT_EQ("#define A \\\n"
  2286. " c; \\\n"
  2287. " e;\n"
  2288. "f;",
  2289. format("#define A c; e;\n"
  2290. "f;",
  2291. getLLVMStyleWithColumns(14)));
  2292. }
  2293. TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
  2294. TEST_F(FormatTest, AlwaysFormatsEntireMacroDefinitions) {
  2295. EXPECT_EQ("int i;\n"
  2296. "#define A \\\n"
  2297. " int i; \\\n"
  2298. " int j\n"
  2299. "int k;",
  2300. format("int i;\n"
  2301. "#define A \\\n"
  2302. " int i ; \\\n"
  2303. " int j\n"
  2304. "int k;",
  2305. 8, 0, getGoogleStyle())); // 8: position of "#define".
  2306. EXPECT_EQ("int i;\n"
  2307. "#define A \\\n"
  2308. " int i; \\\n"
  2309. " int j\n"
  2310. "int k;",
  2311. format("int i;\n"
  2312. "#define A \\\n"
  2313. " int i ; \\\n"
  2314. " int j\n"
  2315. "int k;",
  2316. 45, 0, getGoogleStyle())); // 45: position of "j".
  2317. }
  2318. TEST_F(FormatTest, MacroDefinitionInsideStatement) {
  2319. EXPECT_EQ("int x,\n"
  2320. "#define A\n"
  2321. " y;",
  2322. format("int x,\n#define A\ny;"));
  2323. }
  2324. TEST_F(FormatTest, HashInMacroDefinition) {
  2325. EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
  2326. verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
  2327. verifyFormat("#define A \\\n"
  2328. " { \\\n"
  2329. " f(#c); \\\n"
  2330. " }",
  2331. getLLVMStyleWithColumns(11));
  2332. verifyFormat("#define A(X) \\\n"
  2333. " void function##X()",
  2334. getLLVMStyleWithColumns(22));
  2335. verifyFormat("#define A(a, b, c) \\\n"
  2336. " void a##b##c()",
  2337. getLLVMStyleWithColumns(22));
  2338. verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
  2339. }
  2340. TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
  2341. EXPECT_EQ("#define A (x)", format("#define A (x)"));
  2342. EXPECT_EQ("#define A(x)", format("#define A(x)"));
  2343. }
  2344. TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
  2345. EXPECT_EQ("#define A b;", format("#define A \\\n"
  2346. " \\\n"
  2347. " b;",
  2348. getLLVMStyleWithColumns(25)));
  2349. EXPECT_EQ("#define A \\\n"
  2350. " \\\n"
  2351. " a; \\\n"
  2352. " b;",
  2353. format("#define A \\\n"
  2354. " \\\n"
  2355. " a; \\\n"
  2356. " b;",
  2357. getLLVMStyleWithColumns(11)));
  2358. EXPECT_EQ("#define A \\\n"
  2359. " a; \\\n"
  2360. " \\\n"
  2361. " b;",
  2362. format("#define A \\\n"
  2363. " a; \\\n"
  2364. " \\\n"
  2365. " b;",
  2366. getLLVMStyleWithColumns(11)));
  2367. }
  2368. TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
  2369. verifyFormat("#define A :");
  2370. verifyFormat("#define SOMECASES \\\n"
  2371. " case 1: \\\n"
  2372. " case 2\n",
  2373. getLLVMStyleWithColumns(20));
  2374. verifyFormat("#define A template <typename T>");
  2375. verifyFormat("#define STR(x) #x\n"
  2376. "f(STR(this_is_a_string_literal{));");
  2377. verifyFormat("#pragma omp threadprivate( \\\n"
  2378. " y)), // expected-warning",
  2379. getLLVMStyleWithColumns(28));
  2380. }
  2381. TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
  2382. verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
  2383. EXPECT_EQ("class A : public QObject {\n"
  2384. " Q_OBJECT\n"
  2385. "\n"
  2386. " A() {}\n"
  2387. "};",
  2388. format("class A : public QObject {\n"
  2389. " Q_OBJECT\n"
  2390. "\n"
  2391. " A() {\n}\n"
  2392. "} ;"));
  2393. EXPECT_EQ("SOME_MACRO\n"
  2394. "namespace {\n"
  2395. "void f();\n"
  2396. "}",
  2397. format("SOME_MACRO\n"
  2398. " namespace {\n"
  2399. "void f( );\n"
  2400. "}"));
  2401. // Only if the identifier contains at least 5 characters.
  2402. EXPECT_EQ("HTTP f();",
  2403. format("HTTP\nf();"));
  2404. EXPECT_EQ("MACRO\nf();",
  2405. format("MACRO\nf();"));
  2406. // Only if everything is upper case.
  2407. EXPECT_EQ("class A : public QObject {\n"
  2408. " Q_Object A() {}\n"
  2409. "};",
  2410. format("class A : public QObject {\n"
  2411. " Q_Object\n"
  2412. " A() {\n}\n"
  2413. "} ;"));
  2414. // Only if the next line can actually start an unwrapped line.
  2415. EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
  2416. format("SOME_WEIRD_LOG_MACRO\n"
  2417. "<< SomeThing;"));
  2418. }
  2419. TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
  2420. EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  2421. "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  2422. "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  2423. "class X {};\n"
  2424. "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  2425. "int *createScopDetectionPass() { return 0; }",
  2426. format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  2427. " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  2428. " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  2429. " class X {};\n"
  2430. " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  2431. " int *createScopDetectionPass() { return 0; }"));
  2432. // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
  2433. // braces, so that inner block is indented one level more.
  2434. EXPECT_EQ("int q() {\n"
  2435. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  2436. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  2437. " IPC_END_MESSAGE_MAP()\n"
  2438. "}",
  2439. format("int q() {\n"
  2440. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  2441. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  2442. " IPC_END_MESSAGE_MAP()\n"
  2443. "}"));
  2444. // Same inside macros.
  2445. EXPECT_EQ("#define LIST(L) \\\n"
  2446. " L(A) \\\n"
  2447. " L(B) \\\n"
  2448. " L(C)",
  2449. format("#define LIST(L) \\\n"
  2450. " L(A) \\\n"
  2451. " L(B) \\\n"
  2452. " L(C)",
  2453. getGoogleStyle()));
  2454. // These must not be recognized as macros.
  2455. EXPECT_EQ("int q() {\n"
  2456. " f(x);\n"
  2457. " f(x) {}\n"
  2458. " f(x)->g();\n"
  2459. " f(x)->*g();\n"
  2460. " f(x).g();\n"
  2461. " f(x) = x;\n"
  2462. " f(x) += x;\n"
  2463. " f(x) -= x;\n"
  2464. " f(x) *= x;\n"
  2465. " f(x) /= x;\n"
  2466. " f(x) %= x;\n"
  2467. " f(x) &= x;\n"
  2468. " f(x) |= x;\n"
  2469. " f(x) ^= x;\n"
  2470. " f(x) >>= x;\n"
  2471. " f(x) <<= x;\n"
  2472. " f(x)[y].z();\n"
  2473. " LOG(INFO) << x;\n"
  2474. " ifstream(x) >> x;\n"
  2475. "}\n",
  2476. format("int q() {\n"
  2477. " f(x)\n;\n"
  2478. " f(x)\n {}\n"
  2479. " f(x)\n->g();\n"
  2480. " f(x)\n->*g();\n"
  2481. " f(x)\n.g();\n"
  2482. " f(x)\n = x;\n"
  2483. " f(x)\n += x;\n"
  2484. " f(x)\n -= x;\n"
  2485. " f(x)\n *= x;\n"
  2486. " f(x)\n /= x;\n"
  2487. " f(x)\n %= x;\n"
  2488. " f(x)\n &= x;\n"
  2489. " f(x)\n |= x;\n"
  2490. " f(x)\n ^= x;\n"
  2491. " f(x)\n >>= x;\n"
  2492. " f(x)\n <<= x;\n"
  2493. " f(x)\n[y].z();\n"
  2494. " LOG(INFO)\n << x;\n"
  2495. " ifstream(x)\n >> x;\n"
  2496. "}\n"));
  2497. EXPECT_EQ("int q() {\n"
  2498. " F(x)\n"
  2499. " if (1) {\n"
  2500. " }\n"
  2501. " F(x)\n"
  2502. " while (1) {\n"
  2503. " }\n"
  2504. " F(x)\n"
  2505. " G(x);\n"
  2506. " F(x)\n"
  2507. " try {\n"
  2508. " Q();\n"
  2509. " } catch (...) {\n"
  2510. " }\n"
  2511. "}\n",
  2512. format("int q() {\n"
  2513. "F(x)\n"
  2514. "if (1) {}\n"
  2515. "F(x)\n"
  2516. "while (1) {}\n"
  2517. "F(x)\n"
  2518. "G(x);\n"
  2519. "F(x)\n"
  2520. "try { Q(); } catch (...) {}\n"
  2521. "}\n"));
  2522. EXPECT_EQ("class A {\n"
  2523. " A() : t(0) {}\n"
  2524. " A(int i) noexcept() : {}\n"
  2525. " A(X x)\n" // FIXME: function-level try blocks are broken.
  2526. " try : t(0) {\n"
  2527. " } catch (...) {\n"
  2528. " }\n"
  2529. "};",
  2530. format("class A {\n"
  2531. " A()\n : t(0) {}\n"
  2532. " A(int i)\n noexcept() : {}\n"
  2533. " A(X x)\n"
  2534. " try : t(0) {} catch (...) {}\n"
  2535. "};"));
  2536. EXPECT_EQ(
  2537. "class SomeClass {\n"
  2538. "public:\n"
  2539. " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2540. "};",
  2541. format("class SomeClass {\n"
  2542. "public:\n"
  2543. " SomeClass()\n"
  2544. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2545. "};"));
  2546. EXPECT_EQ(
  2547. "class SomeClass {\n"
  2548. "public:\n"
  2549. " SomeClass()\n"
  2550. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2551. "};",
  2552. format("class SomeClass {\n"
  2553. "public:\n"
  2554. " SomeClass()\n"
  2555. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2556. "};", getLLVMStyleWithColumns(40)));
  2557. }
  2558. TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
  2559. verifyFormat("#define A \\\n"
  2560. " f({ \\\n"
  2561. " g(); \\\n"
  2562. " });", getLLVMStyleWithColumns(11));
  2563. }
  2564. TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
  2565. EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
  2566. }
  2567. TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
  2568. verifyFormat("{\n { a #c; }\n}");
  2569. }
  2570. TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
  2571. EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
  2572. format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
  2573. EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
  2574. format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
  2575. }
  2576. TEST_F(FormatTest, EscapedNewlineAtStartOfToken) {
  2577. EXPECT_EQ(
  2578. "#define A \\\n int i; \\\n int j;",
  2579. format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
  2580. EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
  2581. }
  2582. TEST_F(FormatTest, NoEscapedNewlineHandlingInBlockComments) {
  2583. EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/"));
  2584. }
  2585. TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
  2586. verifyFormat("#define A \\\n"
  2587. " int v( \\\n"
  2588. " a); \\\n"
  2589. " int i;",
  2590. getLLVMStyleWithColumns(11));
  2591. }
  2592. TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
  2593. EXPECT_EQ(
  2594. "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2595. " \\\n"
  2596. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2597. "\n"
  2598. "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2599. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
  2600. format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2601. "\\\n"
  2602. "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2603. " \n"
  2604. " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2605. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
  2606. }
  2607. TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
  2608. EXPECT_EQ("int\n"
  2609. "#define A\n"
  2610. " a;",
  2611. format("int\n#define A\na;"));
  2612. verifyFormat("functionCallTo(\n"
  2613. " someOtherFunction(\n"
  2614. " withSomeParameters, whichInSequence,\n"
  2615. " areLongerThanALine(andAnotherCall,\n"
  2616. "#define A B\n"
  2617. " withMoreParamters,\n"
  2618. " whichStronglyInfluenceTheLayout),\n"
  2619. " andMoreParameters),\n"
  2620. " trailing);",
  2621. getLLVMStyleWithColumns(69));
  2622. verifyFormat("Foo::Foo()\n"
  2623. "#ifdef BAR\n"
  2624. " : baz(0)\n"
  2625. "#endif\n"
  2626. "{\n"
  2627. "}");
  2628. verifyFormat("void f() {\n"
  2629. " if (true)\n"
  2630. "#ifdef A\n"
  2631. " f(42);\n"
  2632. " x();\n"
  2633. "#else\n"
  2634. " g();\n"
  2635. " x();\n"
  2636. "#endif\n"
  2637. "}");
  2638. verifyFormat("void f(param1, param2,\n"
  2639. " param3,\n"
  2640. "#ifdef A\n"
  2641. " param4(param5,\n"
  2642. "#ifdef A1\n"
  2643. " param6,\n"
  2644. "#ifdef A2\n"
  2645. " param7),\n"
  2646. "#else\n"
  2647. " param8),\n"
  2648. " param9,\n"
  2649. "#endif\n"
  2650. " param10,\n"
  2651. "#endif\n"
  2652. " param11)\n"
  2653. "#else\n"
  2654. " param12)\n"
  2655. "#endif\n"
  2656. "{\n"
  2657. " x();\n"
  2658. "}",
  2659. getLLVMStyleWithColumns(28));
  2660. verifyFormat("#if 1\n"
  2661. "int i;");
  2662. verifyFormat(
  2663. "#if 1\n"
  2664. "#endif\n"
  2665. "#if 1\n"
  2666. "#else\n"
  2667. "#endif\n");
  2668. verifyFormat("DEBUG({\n"
  2669. " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2670. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  2671. "});\n"
  2672. "#if a\n"
  2673. "#else\n"
  2674. "#endif");
  2675. }
  2676. TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
  2677. verifyFormat("#endif\n"
  2678. "#if B");
  2679. }
  2680. TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
  2681. FormatStyle SingleLine = getLLVMStyle();
  2682. SingleLine.AllowShortIfStatementsOnASingleLine = true;
  2683. verifyFormat(
  2684. "#if 0\n"
  2685. "#elif 1\n"
  2686. "#endif\n"
  2687. "void foo() {\n"
  2688. " if (test) foo2();\n"
  2689. "}",
  2690. SingleLine);
  2691. }
  2692. TEST_F(FormatTest, LayoutBlockInsideParens) {
  2693. EXPECT_EQ("functionCall({ int i; });", format(" functionCall ( {int i;} );"));
  2694. EXPECT_EQ("functionCall({\n"
  2695. " int i;\n"
  2696. " int j;\n"
  2697. "});",
  2698. format(" functionCall ( {int i;int j;} );"));
  2699. EXPECT_EQ("functionCall({\n"
  2700. " int i;\n"
  2701. " int j;\n"
  2702. " },\n"
  2703. " aaaa, bbbb, cccc);",
  2704. format(" functionCall ( {int i;int j;}, aaaa, bbbb, cccc);"));
  2705. EXPECT_EQ("functionCall(aaaa, bbbb, { int i; });",
  2706. format(" functionCall (aaaa, bbbb, {int i;});"));
  2707. EXPECT_EQ("functionCall(aaaa, bbbb, {\n"
  2708. " int i;\n"
  2709. " int j;\n"
  2710. "});",
  2711. format(" functionCall (aaaa, bbbb, {int i;int j;});"));
  2712. EXPECT_EQ("functionCall(aaaa, bbbb, { int i; });",
  2713. format(" functionCall (aaaa, bbbb, {int i;});"));
  2714. verifyFormat(
  2715. "Aaa({\n"
  2716. " int i; // break\n"
  2717. " },\n"
  2718. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  2719. " ccccccccccccccccc));");
  2720. verifyFormat("DEBUG({\n"
  2721. " if (a)\n"
  2722. " f();\n"
  2723. "});");
  2724. }
  2725. TEST_F(FormatTest, LayoutBlockInsideStatement) {
  2726. EXPECT_EQ("SOME_MACRO { int i; }\n"
  2727. "int i;",
  2728. format(" SOME_MACRO {int i;} int i;"));
  2729. }
  2730. TEST_F(FormatTest, LayoutNestedBlocks) {
  2731. verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
  2732. " struct s {\n"
  2733. " int i;\n"
  2734. " };\n"
  2735. " s kBitsToOs[] = {{10}};\n"
  2736. " for (int i = 0; i < 10; ++i)\n"
  2737. " return;\n"
  2738. "}");
  2739. verifyFormat("call(parameter, {\n"
  2740. " something();\n"
  2741. " // Comment using all columns.\n"
  2742. " somethingelse();\n"
  2743. "});",
  2744. getLLVMStyleWithColumns(40));
  2745. verifyFormat("DEBUG( //\n"
  2746. " { f(); }, a);");
  2747. verifyFormat("DEBUG( //\n"
  2748. " {\n"
  2749. " f(); //\n"
  2750. " },\n"
  2751. " a);");
  2752. EXPECT_EQ("call(parameter, {\n"
  2753. " something();\n"
  2754. " // Comment too\n"
  2755. " // looooooooooong.\n"
  2756. " somethingElse();\n"
  2757. "});",
  2758. format("call(parameter, {\n"
  2759. " something();\n"
  2760. " // Comment too looooooooooong.\n"
  2761. " somethingElse();\n"
  2762. "});",
  2763. getLLVMStyleWithColumns(29)));
  2764. EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });"));
  2765. EXPECT_EQ("DEBUG({ // comment\n"
  2766. " int i;\n"
  2767. "});",
  2768. format("DEBUG({ // comment\n"
  2769. "int i;\n"
  2770. "});"));
  2771. EXPECT_EQ("DEBUG({\n"
  2772. " int i;\n"
  2773. "\n"
  2774. " // comment\n"
  2775. " int j;\n"
  2776. "});",
  2777. format("DEBUG({\n"
  2778. " int i;\n"
  2779. "\n"
  2780. " // comment\n"
  2781. " int j;\n"
  2782. "});"));
  2783. verifyFormat("DEBUG({\n"
  2784. " if (a)\n"
  2785. " return;\n"
  2786. "});");
  2787. verifyGoogleFormat("DEBUG({\n"
  2788. " if (a) return;\n"
  2789. "});");
  2790. FormatStyle Style = getGoogleStyle();
  2791. Style.ColumnLimit = 45;
  2792. verifyFormat("Debug(aaaaa, {\n"
  2793. " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2794. " return;\n"
  2795. " },\n"
  2796. " a);", Style);
  2797. }
  2798. TEST_F(FormatTest, IndividualStatementsOfNestedBlocks) {
  2799. EXPECT_EQ("DEBUG({\n"
  2800. " int i;\n"
  2801. " int j;\n"
  2802. "});",
  2803. format("DEBUG( {\n"
  2804. " int i;\n"
  2805. " int j;\n"
  2806. "} ) ;",
  2807. 20, 1, getLLVMStyle()));
  2808. EXPECT_EQ("DEBUG( {\n"
  2809. " int i;\n"
  2810. " int j;\n"
  2811. "} ) ;",
  2812. format("DEBUG( {\n"
  2813. " int i;\n"
  2814. " int j;\n"
  2815. "} ) ;",
  2816. 41, 1, getLLVMStyle()));
  2817. EXPECT_EQ("DEBUG( {\n"
  2818. " int i;\n"
  2819. " int j;\n"
  2820. "} ) ;",
  2821. format("DEBUG( {\n"
  2822. " int i;\n"
  2823. " int j;\n"
  2824. "} ) ;",
  2825. 41, 1, getLLVMStyle()));
  2826. EXPECT_EQ("DEBUG({\n"
  2827. " int i;\n"
  2828. " int j;\n"
  2829. "});",
  2830. format("DEBUG( {\n"
  2831. " int i;\n"
  2832. " int j;\n"
  2833. "} ) ;",
  2834. 20, 1, getLLVMStyle()));
  2835. EXPECT_EQ("Debug({\n"
  2836. " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2837. " return;\n"
  2838. " },\n"
  2839. " a);",
  2840. format("Debug({\n"
  2841. " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2842. " return;\n"
  2843. " },\n"
  2844. " a);",
  2845. 50, 1, getLLVMStyle()));
  2846. EXPECT_EQ("DEBUG({\n"
  2847. " DEBUG({\n"
  2848. " int a;\n"
  2849. " int b;\n"
  2850. " }) ;\n"
  2851. "});",
  2852. format("DEBUG({\n"
  2853. " DEBUG({\n"
  2854. " int a;\n"
  2855. " int b;\n" // Format this line only.
  2856. " }) ;\n" // Don't touch this line.
  2857. "});",
  2858. 35, 0, getLLVMStyle()));
  2859. EXPECT_EQ("DEBUG({\n"
  2860. " int a; //\n"
  2861. "});",
  2862. format("DEBUG({\n"
  2863. " int a; //\n"
  2864. "});",
  2865. 0, 0, getLLVMStyle()));
  2866. }
  2867. TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
  2868. EXPECT_EQ("{}", format("{}"));
  2869. verifyFormat("enum E {};");
  2870. verifyFormat("enum E {}");
  2871. }
  2872. //===----------------------------------------------------------------------===//
  2873. // Line break tests.
  2874. //===----------------------------------------------------------------------===//
  2875. TEST_F(FormatTest, PreventConfusingIndents) {
  2876. verifyFormat(
  2877. "void f() {\n"
  2878. " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
  2879. " parameter, parameter, parameter)),\n"
  2880. " SecondLongCall(parameter));\n"
  2881. "}");
  2882. verifyFormat(
  2883. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2884. " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2885. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2886. " aaaaaaaaaaaaaaaaaaaaaaaa);");
  2887. verifyFormat(
  2888. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2889. " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
  2890. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  2891. " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
  2892. verifyFormat(
  2893. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  2894. " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
  2895. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
  2896. " aaaaaaaaaaaaaaaaaaaaaaaa>;");
  2897. verifyFormat("int a = bbbb && ccc && fffff(\n"
  2898. "#define A Just forcing a new line\n"
  2899. " ddd);");
  2900. }
  2901. TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
  2902. verifyFormat(
  2903. "bool aaaaaaa =\n"
  2904. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
  2905. " bbbbbbbb();");
  2906. verifyFormat(
  2907. "bool aaaaaaa =\n"
  2908. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
  2909. " bbbbbbbb();");
  2910. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  2911. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
  2912. " ccccccccc == ddddddddddd;");
  2913. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  2914. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
  2915. " ccccccccc == ddddddddddd;");
  2916. verifyFormat(
  2917. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  2918. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
  2919. " ccccccccc == ddddddddddd;");
  2920. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  2921. " aaaaaa) &&\n"
  2922. " bbbbbb && cccccc;");
  2923. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  2924. " aaaaaa) >>\n"
  2925. " bbbbbb;");
  2926. verifyFormat("Whitespaces.addUntouchableComment(\n"
  2927. " SourceMgr.getSpellingColumnNumber(\n"
  2928. " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
  2929. " 1);");
  2930. verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2931. " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
  2932. " cccccc) {\n}");
  2933. verifyFormat("b = a &&\n"
  2934. " // Comment\n"
  2935. " b.c && d;");
  2936. // If the LHS of a comparison is not a binary expression itself, the
  2937. // additional linebreak confuses many people.
  2938. verifyFormat(
  2939. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2940. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
  2941. "}");
  2942. verifyFormat(
  2943. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2944. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  2945. "}");
  2946. verifyFormat(
  2947. "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
  2948. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  2949. "}");
  2950. // Even explicit parentheses stress the precedence enough to make the
  2951. // additional break unnecessary.
  2952. verifyFormat(
  2953. "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2954. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  2955. "}");
  2956. // This cases is borderline, but with the indentation it is still readable.
  2957. verifyFormat(
  2958. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2959. " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2960. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  2961. "}",
  2962. getLLVMStyleWithColumns(75));
  2963. // If the LHS is a binary expression, we should still use the additional break
  2964. // as otherwise the formatting hides the operator precedence.
  2965. verifyFormat(
  2966. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2967. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2968. " 5) {\n"
  2969. "}");
  2970. FormatStyle OnePerLine = getLLVMStyle();
  2971. OnePerLine.BinPackParameters = false;
  2972. verifyFormat(
  2973. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2974. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2975. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
  2976. OnePerLine);
  2977. }
  2978. TEST_F(FormatTest, ExpressionIndentation) {
  2979. verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2980. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2981. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2982. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2983. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
  2984. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
  2985. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2986. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
  2987. " ccccccccccccccccccccccccccccccccccccccccc;");
  2988. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2989. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2990. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2991. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  2992. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2993. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2994. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2995. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  2996. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2997. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2998. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2999. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3000. verifyFormat("if () {\n"
  3001. "} else if (aaaaa &&\n"
  3002. " bbbbb > // break\n"
  3003. " ccccc) {\n"
  3004. "}");
  3005. // Presence of a trailing comment used to change indentation of b.
  3006. verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
  3007. " b;\n"
  3008. "return aaaaaaaaaaaaaaaaaaa +\n"
  3009. " b; //",
  3010. getLLVMStyleWithColumns(30));
  3011. }
  3012. TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
  3013. // Not sure what the best system is here. Like this, the LHS can be found
  3014. // immediately above an operator (everything with the same or a higher
  3015. // indent). The RHS is aligned right of the operator and so compasses
  3016. // everything until something with the same indent as the operator is found.
  3017. // FIXME: Is this a good system?
  3018. FormatStyle Style = getLLVMStyle();
  3019. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  3020. verifyFormat(
  3021. "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3022. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3023. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3024. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3025. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3026. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3027. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3028. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3029. " > ccccccccccccccccccccccccccccccccccccccccc;",
  3030. Style);
  3031. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3032. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3033. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3034. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3035. Style);
  3036. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3037. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3038. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3039. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3040. Style);
  3041. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3042. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3043. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3044. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3045. Style);
  3046. verifyFormat("if () {\n"
  3047. "} else if (aaaaa\n"
  3048. " && bbbbb // break\n"
  3049. " > ccccc) {\n"
  3050. "}",
  3051. Style);
  3052. verifyFormat("return (a)\n"
  3053. " // comment\n"
  3054. " + b;",
  3055. Style);
  3056. // Forced by comments.
  3057. verifyFormat(
  3058. "unsigned ContentSize =\n"
  3059. " sizeof(int16_t) // DWARF ARange version number\n"
  3060. " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
  3061. " + sizeof(int8_t) // Pointer Size (in bytes)\n"
  3062. " + sizeof(int8_t); // Segment Size (in bytes)");
  3063. verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
  3064. " == boost::fusion::at_c<1>(iiii).second;",
  3065. Style);
  3066. Style.ColumnLimit = 60;
  3067. verifyFormat("zzzzzzzzzz\n"
  3068. " = bbbbbbbbbbbbbbbbb\n"
  3069. " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3070. Style);
  3071. }
  3072. TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
  3073. FormatStyle Style = getLLVMStyle();
  3074. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
  3075. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3076. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3077. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", Style);
  3078. }
  3079. TEST_F(FormatTest, ConstructorInitializers) {
  3080. verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
  3081. verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
  3082. getLLVMStyleWithColumns(45));
  3083. verifyFormat("Constructor()\n"
  3084. " : Inttializer(FitsOnTheLine) {}",
  3085. getLLVMStyleWithColumns(44));
  3086. verifyFormat("Constructor()\n"
  3087. " : Inttializer(FitsOnTheLine) {}",
  3088. getLLVMStyleWithColumns(43));
  3089. verifyFormat(
  3090. "SomeClass::Constructor()\n"
  3091. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  3092. verifyFormat(
  3093. "SomeClass::Constructor()\n"
  3094. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3095. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
  3096. verifyFormat(
  3097. "SomeClass::Constructor()\n"
  3098. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3099. " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  3100. verifyFormat("Constructor()\n"
  3101. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3102. " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3103. " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3104. " aaaaaaaaaaaaaaaaaaaaaaa() {}");
  3105. verifyFormat("Constructor()\n"
  3106. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3107. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3108. verifyFormat("Constructor(int Parameter = 0)\n"
  3109. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
  3110. " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
  3111. verifyFormat("Constructor()\n"
  3112. " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
  3113. "}",
  3114. getLLVMStyleWithColumns(60));
  3115. verifyFormat("Constructor()\n"
  3116. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3117. " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
  3118. // Here a line could be saved by splitting the second initializer onto two
  3119. // lines, but that is not desirable.
  3120. verifyFormat("Constructor()\n"
  3121. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3122. " aaaaaaaaaaa(aaaaaaaaaaa),\n"
  3123. " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3124. FormatStyle OnePerLine = getLLVMStyle();
  3125. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  3126. verifyFormat("SomeClass::Constructor()\n"
  3127. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3128. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3129. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  3130. OnePerLine);
  3131. verifyFormat("SomeClass::Constructor()\n"
  3132. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
  3133. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3134. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  3135. OnePerLine);
  3136. verifyFormat("MyClass::MyClass(int var)\n"
  3137. " : some_var_(var), // 4 space indent\n"
  3138. " some_other_var_(var + 1) { // lined up\n"
  3139. "}",
  3140. OnePerLine);
  3141. verifyFormat("Constructor()\n"
  3142. " : aaaaa(aaaaaa),\n"
  3143. " aaaaa(aaaaaa),\n"
  3144. " aaaaa(aaaaaa),\n"
  3145. " aaaaa(aaaaaa),\n"
  3146. " aaaaa(aaaaaa) {}",
  3147. OnePerLine);
  3148. verifyFormat("Constructor()\n"
  3149. " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
  3150. " aaaaaaaaaaaaaaaaaaaaaa) {}",
  3151. OnePerLine);
  3152. EXPECT_EQ("Constructor()\n"
  3153. " : // Comment forcing unwanted break.\n"
  3154. " aaaa(aaaa) {}",
  3155. format("Constructor() :\n"
  3156. " // Comment forcing unwanted break.\n"
  3157. " aaaa(aaaa) {}"));
  3158. }
  3159. TEST_F(FormatTest, MemoizationTests) {
  3160. // This breaks if the memoization lookup does not take \c Indent and
  3161. // \c LastSpace into account.
  3162. verifyFormat(
  3163. "extern CFRunLoopTimerRef\n"
  3164. "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
  3165. " CFTimeInterval interval, CFOptionFlags flags,\n"
  3166. " CFIndex order, CFRunLoopTimerCallBack callout,\n"
  3167. " CFRunLoopTimerContext *context) {}");
  3168. // Deep nesting somewhat works around our memoization.
  3169. verifyFormat(
  3170. "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3171. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3172. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3173. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3174. " aaaaa())))))))))))))))))))))))))))))))))))))));",
  3175. getLLVMStyleWithColumns(65));
  3176. verifyFormat(
  3177. "aaaaa(\n"
  3178. " aaaaa,\n"
  3179. " aaaaa(\n"
  3180. " aaaaa,\n"
  3181. " aaaaa(\n"
  3182. " aaaaa,\n"
  3183. " aaaaa(\n"
  3184. " aaaaa,\n"
  3185. " aaaaa(\n"
  3186. " aaaaa,\n"
  3187. " aaaaa(\n"
  3188. " aaaaa,\n"
  3189. " aaaaa(\n"
  3190. " aaaaa,\n"
  3191. " aaaaa(\n"
  3192. " aaaaa,\n"
  3193. " aaaaa(\n"
  3194. " aaaaa,\n"
  3195. " aaaaa(\n"
  3196. " aaaaa,\n"
  3197. " aaaaa(\n"
  3198. " aaaaa,\n"
  3199. " aaaaa(\n"
  3200. " aaaaa,\n"
  3201. " aaaaa))))))))))));",
  3202. getLLVMStyleWithColumns(65));
  3203. verifyFormat(
  3204. "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"
  3205. " a),\n"
  3206. " a),\n"
  3207. " a),\n"
  3208. " a),\n"
  3209. " a),\n"
  3210. " a),\n"
  3211. " a),\n"
  3212. " a),\n"
  3213. " a),\n"
  3214. " a),\n"
  3215. " a),\n"
  3216. " a),\n"
  3217. " a),\n"
  3218. " a),\n"
  3219. " a),\n"
  3220. " a),\n"
  3221. " a)",
  3222. getLLVMStyleWithColumns(65));
  3223. // This test takes VERY long when memoization is broken.
  3224. FormatStyle OnePerLine = getLLVMStyle();
  3225. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  3226. OnePerLine.BinPackParameters = false;
  3227. std::string input = "Constructor()\n"
  3228. " : aaaa(a,\n";
  3229. for (unsigned i = 0, e = 80; i != e; ++i) {
  3230. input += " a,\n";
  3231. }
  3232. input += " a) {}";
  3233. verifyFormat(input, OnePerLine);
  3234. }
  3235. TEST_F(FormatTest, BreaksAsHighAsPossible) {
  3236. verifyFormat(
  3237. "void f() {\n"
  3238. " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
  3239. " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
  3240. " f();\n"
  3241. "}");
  3242. verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
  3243. " Intervals[i - 1].getRange().getLast()) {\n}");
  3244. }
  3245. TEST_F(FormatTest, BreaksFunctionDeclarations) {
  3246. // Principially, we break function declarations in a certain order:
  3247. // 1) break amongst arguments.
  3248. verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
  3249. " Cccccccccccccc cccccccccccccc);");
  3250. verifyFormat(
  3251. "template <class TemplateIt>\n"
  3252. "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
  3253. " TemplateIt *stop) {}");
  3254. // 2) break after return type.
  3255. verifyFormat(
  3256. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3257. "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
  3258. getGoogleStyle());
  3259. // 3) break after (.
  3260. verifyFormat(
  3261. "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
  3262. " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
  3263. getGoogleStyle());
  3264. // 4) break before after nested name specifiers.
  3265. verifyFormat(
  3266. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3267. "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
  3268. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
  3269. getGoogleStyle());
  3270. // However, there are exceptions, if a sufficient amount of lines can be
  3271. // saved.
  3272. // FIXME: The precise cut-offs wrt. the number of saved lines might need some
  3273. // more adjusting.
  3274. verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  3275. " Cccccccccccccc cccccccccc,\n"
  3276. " Cccccccccccccc cccccccccc,\n"
  3277. " Cccccccccccccc cccccccccc,\n"
  3278. " Cccccccccccccc cccccccccc);");
  3279. verifyFormat(
  3280. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3281. "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3282. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3283. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
  3284. getGoogleStyle());
  3285. verifyFormat(
  3286. "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  3287. " Cccccccccccccc cccccccccc,\n"
  3288. " Cccccccccccccc cccccccccc,\n"
  3289. " Cccccccccccccc cccccccccc,\n"
  3290. " Cccccccccccccc cccccccccc,\n"
  3291. " Cccccccccccccc cccccccccc,\n"
  3292. " Cccccccccccccc cccccccccc);");
  3293. verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3294. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3295. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3296. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3297. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
  3298. // Break after multi-line parameters.
  3299. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3300. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3301. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3302. " bbbb bbbb);");
  3303. verifyFormat("void SomeLoooooooooooongFunction(\n"
  3304. " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  3305. " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3306. " int bbbbbbbbbbbbb);");
  3307. // Treat overloaded operators like other functions.
  3308. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3309. "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
  3310. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3311. "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
  3312. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3313. "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
  3314. verifyGoogleFormat(
  3315. "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
  3316. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  3317. verifyGoogleFormat(
  3318. "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
  3319. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  3320. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3321. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  3322. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
  3323. "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  3324. verifyGoogleFormat(
  3325. "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
  3326. "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3327. " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
  3328. }
  3329. TEST_F(FormatTest, TrailingReturnType) {
  3330. verifyFormat("auto foo() -> int;\n");
  3331. verifyFormat("struct S {\n"
  3332. " auto bar() const -> int;\n"
  3333. "};");
  3334. verifyFormat("template <size_t Order, typename T>\n"
  3335. "auto load_img(const std::string &filename)\n"
  3336. " -> alias::tensor<Order, T, mem::tag::cpu> {}");
  3337. verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
  3338. " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
  3339. verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
  3340. // Not trailing return types.
  3341. verifyFormat("void f() { auto a = b->c(); }");
  3342. }
  3343. TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
  3344. // Avoid breaking before trailing 'const' or other trailing annotations, if
  3345. // they are not function-like.
  3346. FormatStyle Style = getGoogleStyle();
  3347. Style.ColumnLimit = 47;
  3348. verifyFormat("void someLongFunction(\n"
  3349. " int someLoooooooooooooongParameter) const {\n}",
  3350. getLLVMStyleWithColumns(47));
  3351. verifyFormat("LoooooongReturnType\n"
  3352. "someLoooooooongFunction() const {}",
  3353. getLLVMStyleWithColumns(47));
  3354. verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
  3355. " const {}",
  3356. Style);
  3357. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3358. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
  3359. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3360. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
  3361. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3362. " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
  3363. verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
  3364. " aaaaaaaaaaa aaaaa) const override;");
  3365. verifyGoogleFormat(
  3366. "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3367. " const override;");
  3368. // Even if the first parameter has to be wrapped.
  3369. verifyFormat("void someLongFunction(\n"
  3370. " int someLongParameter) const {}",
  3371. getLLVMStyleWithColumns(46));
  3372. verifyFormat("void someLongFunction(\n"
  3373. " int someLongParameter) const {}",
  3374. Style);
  3375. verifyFormat("void someLongFunction(\n"
  3376. " int someLongParameter) override {}",
  3377. Style);
  3378. verifyFormat("void someLongFunction(\n"
  3379. " int someLongParameter) OVERRIDE {}",
  3380. Style);
  3381. verifyFormat("void someLongFunction(\n"
  3382. " int someLongParameter) final {}",
  3383. Style);
  3384. verifyFormat("void someLongFunction(\n"
  3385. " int someLongParameter) FINAL {}",
  3386. Style);
  3387. verifyFormat("void someLongFunction(\n"
  3388. " int parameter) const override {}",
  3389. Style);
  3390. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  3391. verifyFormat("void someLongFunction(\n"
  3392. " int someLongParameter) const\n"
  3393. "{\n"
  3394. "}",
  3395. Style);
  3396. // Unless these are unknown annotations.
  3397. verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
  3398. " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3399. " LONG_AND_UGLY_ANNOTATION;");
  3400. // Breaking before function-like trailing annotations is fine to keep them
  3401. // close to their arguments.
  3402. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3403. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  3404. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  3405. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  3406. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  3407. " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
  3408. verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
  3409. " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
  3410. verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
  3411. verifyFormat(
  3412. "void aaaaaaaaaaaaaaaaaa()\n"
  3413. " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3414. " aaaaaaaaaaaaaaaaaaaaaaaaa));");
  3415. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3416. " __attribute__((unused));");
  3417. verifyGoogleFormat(
  3418. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3419. " GUARDED_BY(aaaaaaaaaaaa);");
  3420. verifyGoogleFormat(
  3421. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3422. " GUARDED_BY(aaaaaaaaaaaa);");
  3423. verifyGoogleFormat(
  3424. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
  3425. " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3426. }
  3427. TEST_F(FormatTest, BreaksDesireably) {
  3428. verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  3429. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  3430. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
  3431. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3432. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  3433. "}");
  3434. verifyFormat(
  3435. "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3436. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3437. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3438. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3439. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3440. verifyFormat(
  3441. "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3442. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  3443. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3444. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
  3445. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3446. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3447. verifyFormat(
  3448. "void f() {\n"
  3449. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
  3450. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3451. "}");
  3452. verifyFormat(
  3453. "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3454. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3455. verifyFormat(
  3456. "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3457. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3458. verifyFormat(
  3459. "aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3460. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3461. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3462. // Indent consistently independent of call expression.
  3463. verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
  3464. " dddddddddddddddddddddddddddddd));\n"
  3465. "aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3466. " dddddddddddddddddddddddddddddd));");
  3467. // This test case breaks on an incorrect memoization, i.e. an optimization not
  3468. // taking into account the StopAt value.
  3469. verifyFormat(
  3470. "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3471. " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3472. " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3473. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3474. verifyFormat("{\n {\n {\n"
  3475. " Annotation.SpaceRequiredBefore =\n"
  3476. " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
  3477. " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
  3478. " }\n }\n}");
  3479. // Break on an outer level if there was a break on an inner level.
  3480. EXPECT_EQ("f(g(h(a, // comment\n"
  3481. " b, c),\n"
  3482. " d, e),\n"
  3483. " x, y);",
  3484. format("f(g(h(a, // comment\n"
  3485. " b, c), d, e), x, y);"));
  3486. // Prefer breaking similar line breaks.
  3487. verifyFormat(
  3488. "const int kTrackingOptions = NSTrackingMouseMoved |\n"
  3489. " NSTrackingMouseEnteredAndExited |\n"
  3490. " NSTrackingActiveAlways;");
  3491. }
  3492. TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
  3493. FormatStyle NoBinPacking = getGoogleStyle();
  3494. NoBinPacking.BinPackParameters = false;
  3495. NoBinPacking.BinPackArguments = true;
  3496. verifyFormat("void f() {\n"
  3497. " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
  3498. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3499. "}",
  3500. NoBinPacking);
  3501. verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
  3502. " int aaaaaaaaaaaaaaaaaaaa,\n"
  3503. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3504. NoBinPacking);
  3505. }
  3506. TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
  3507. FormatStyle NoBinPacking = getGoogleStyle();
  3508. NoBinPacking.BinPackParameters = false;
  3509. NoBinPacking.BinPackArguments = false;
  3510. verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
  3511. " aaaaaaaaaaaaaaaaaaaa,\n"
  3512. " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
  3513. NoBinPacking);
  3514. verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
  3515. " aaaaaaaaaaaaa,\n"
  3516. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
  3517. NoBinPacking);
  3518. verifyFormat(
  3519. "aaaaaaaa(aaaaaaaaaaaaa,\n"
  3520. " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3521. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  3522. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3523. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
  3524. NoBinPacking);
  3525. verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  3526. " .aaaaaaaaaaaaaaaaaa();",
  3527. NoBinPacking);
  3528. verifyFormat("void f() {\n"
  3529. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3530. " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
  3531. "}",
  3532. NoBinPacking);
  3533. verifyFormat(
  3534. "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3535. " aaaaaaaaaaaa,\n"
  3536. " aaaaaaaaaaaa);",
  3537. NoBinPacking);
  3538. verifyFormat(
  3539. "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
  3540. " ddddddddddddddddddddddddddddd),\n"
  3541. " test);",
  3542. NoBinPacking);
  3543. verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3544. " aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3545. " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;",
  3546. NoBinPacking);
  3547. verifyFormat("a(\"a\"\n"
  3548. " \"a\",\n"
  3549. " a);");
  3550. NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
  3551. verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
  3552. " aaaaaaaaa,\n"
  3553. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3554. NoBinPacking);
  3555. verifyFormat(
  3556. "void f() {\n"
  3557. " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  3558. " .aaaaaaa();\n"
  3559. "}",
  3560. NoBinPacking);
  3561. verifyFormat(
  3562. "template <class SomeType, class SomeOtherType>\n"
  3563. "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
  3564. NoBinPacking);
  3565. }
  3566. TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
  3567. FormatStyle Style = getLLVMStyleWithColumns(15);
  3568. Style.ExperimentalAutoDetectBinPacking = true;
  3569. EXPECT_EQ("aaa(aaaa,\n"
  3570. " aaaa,\n"
  3571. " aaaa);\n"
  3572. "aaa(aaaa,\n"
  3573. " aaaa,\n"
  3574. " aaaa);",
  3575. format("aaa(aaaa,\n" // one-per-line
  3576. " aaaa,\n"
  3577. " aaaa );\n"
  3578. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  3579. Style));
  3580. EXPECT_EQ("aaa(aaaa, aaaa,\n"
  3581. " aaaa);\n"
  3582. "aaa(aaaa, aaaa,\n"
  3583. " aaaa);",
  3584. format("aaa(aaaa, aaaa,\n" // bin-packed
  3585. " aaaa );\n"
  3586. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  3587. Style));
  3588. }
  3589. TEST_F(FormatTest, FormatsBuilderPattern) {
  3590. verifyFormat(
  3591. "return llvm::StringSwitch<Reference::Kind>(name)\n"
  3592. " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
  3593. " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
  3594. " .StartsWith(\".init\", ORDER_INIT)\n"
  3595. " .StartsWith(\".fini\", ORDER_FINI)\n"
  3596. " .StartsWith(\".hash\", ORDER_HASH)\n"
  3597. " .Default(ORDER_TEXT);\n");
  3598. verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
  3599. " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
  3600. verifyFormat(
  3601. "aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaa(\n"
  3602. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3603. " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
  3604. verifyFormat(
  3605. "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
  3606. " aaaaaaaaaaaaaa);");
  3607. verifyFormat(
  3608. "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
  3609. " aaaaaa->aaaaaaaaaaaa()\n"
  3610. " ->aaaaaaaaaaaaaaaa(\n"
  3611. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3612. " ->aaaaaaaaaaaaaaaaa();");
  3613. verifyGoogleFormat(
  3614. "void f() {\n"
  3615. " someo->Add((new util::filetools::Handler(dir))\n"
  3616. " ->OnEvent1(NewPermanentCallback(\n"
  3617. " this, &HandlerHolderClass::EventHandlerCBA))\n"
  3618. " ->OnEvent2(NewPermanentCallback(\n"
  3619. " this, &HandlerHolderClass::EventHandlerCBB))\n"
  3620. " ->OnEvent3(NewPermanentCallback(\n"
  3621. " this, &HandlerHolderClass::EventHandlerCBC))\n"
  3622. " ->OnEvent5(NewPermanentCallback(\n"
  3623. " this, &HandlerHolderClass::EventHandlerCBD))\n"
  3624. " ->OnEvent6(NewPermanentCallback(\n"
  3625. " this, &HandlerHolderClass::EventHandlerCBE)));\n"
  3626. "}");
  3627. verifyFormat(
  3628. "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
  3629. verifyFormat("aaaaaaaaaaaaaaa()\n"
  3630. " .aaaaaaaaaaaaaaa()\n"
  3631. " .aaaaaaaaaaaaaaa()\n"
  3632. " .aaaaaaaaaaaaaaa()\n"
  3633. " .aaaaaaaaaaaaaaa();");
  3634. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3635. " .aaaaaaaaaaaaaaa()\n"
  3636. " .aaaaaaaaaaaaaaa()\n"
  3637. " .aaaaaaaaaaaaaaa();");
  3638. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3639. " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3640. " .aaaaaaaaaaaaaaa();");
  3641. verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3642. " ->aaaaaaaaaaaaaae(0)\n"
  3643. " ->aaaaaaaaaaaaaaa();");
  3644. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  3645. " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3646. " .has<bbbbbbbbbbbbbbbbbbbbb>();");
  3647. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  3648. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  3649. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
  3650. // Prefer not to break after empty parentheses.
  3651. verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
  3652. " First->LastNewlineOffset);");
  3653. }
  3654. TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
  3655. verifyFormat(
  3656. "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3657. " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
  3658. verifyFormat(
  3659. "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
  3660. " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
  3661. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  3662. " ccccccccccccccccccccccccc) {\n}");
  3663. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
  3664. " ccccccccccccccccccccccccc) {\n}");
  3665. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  3666. " ccccccccccccccccccccccccc) {\n}");
  3667. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
  3668. " ccccccccccccccccccccccccc) {\n}");
  3669. verifyFormat(
  3670. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
  3671. " ccccccccccccccccccccccccc) {\n}");
  3672. verifyFormat(
  3673. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
  3674. " ccccccccccccccccccccccccc) {\n}");
  3675. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
  3676. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
  3677. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
  3678. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  3679. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
  3680. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
  3681. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
  3682. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  3683. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
  3684. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
  3685. " aaaaaaaaaaaaaaa != aa) {\n}");
  3686. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
  3687. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
  3688. " aaaaaaaaaaaaaaa != aa) {\n}");
  3689. }
  3690. TEST_F(FormatTest, BreaksAfterAssignments) {
  3691. verifyFormat(
  3692. "unsigned Cost =\n"
  3693. " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
  3694. " SI->getPointerAddressSpaceee());\n");
  3695. verifyFormat(
  3696. "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
  3697. " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
  3698. verifyFormat(
  3699. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
  3700. " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
  3701. verifyFormat("unsigned OriginalStartColumn =\n"
  3702. " SourceMgr.getSpellingColumnNumber(\n"
  3703. " Current.FormatTok.getStartOfNonWhitespace()) -\n"
  3704. " 1;");
  3705. }
  3706. TEST_F(FormatTest, AlignsAfterAssignments) {
  3707. verifyFormat(
  3708. "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3709. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3710. verifyFormat(
  3711. "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3712. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3713. verifyFormat(
  3714. "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3715. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3716. verifyFormat(
  3717. "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3718. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  3719. verifyFormat(
  3720. "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3721. " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3722. " aaaaaaaaaaaaaaaaaaaaaaaa;");
  3723. }
  3724. TEST_F(FormatTest, AlignsAfterReturn) {
  3725. verifyFormat(
  3726. "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3727. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3728. verifyFormat(
  3729. "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3730. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  3731. verifyFormat(
  3732. "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  3733. " aaaaaaaaaaaaaaaaaaaaaa();");
  3734. verifyFormat(
  3735. "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  3736. " aaaaaaaaaaaaaaaaaaaaaa());");
  3737. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3738. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3739. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3740. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
  3741. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3742. verifyFormat("return\n"
  3743. " // true if code is one of a or b.\n"
  3744. " code == a || code == b;");
  3745. }
  3746. TEST_F(FormatTest, BreaksConditionalExpressions) {
  3747. verifyFormat(
  3748. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3749. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3750. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3751. verifyFormat(
  3752. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3753. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3754. verifyFormat(
  3755. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
  3756. " : aaaaaaaaaaaaa);");
  3757. verifyFormat(
  3758. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3759. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3760. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3761. " aaaaaaaaaaaaa);");
  3762. verifyFormat(
  3763. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3764. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3765. " aaaaaaaaaaaaa);");
  3766. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3767. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3768. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3769. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3770. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3771. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3772. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3773. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3774. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3775. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3776. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3777. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3778. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3779. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3780. " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3781. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3782. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3783. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3784. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3785. " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3786. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  3787. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3788. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3789. " : aaaaaaaaaaaaaaaa;");
  3790. verifyFormat(
  3791. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3792. " ? aaaaaaaaaaaaaaa\n"
  3793. " : aaaaaaaaaaaaaaa;");
  3794. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  3795. " aaaaaaaaa\n"
  3796. " ? b\n"
  3797. " : c);");
  3798. verifyFormat("return aaaa == bbbb\n"
  3799. " // comment\n"
  3800. " ? aaaa\n"
  3801. " : bbbb;");
  3802. verifyFormat(
  3803. "unsigned Indent =\n"
  3804. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
  3805. " ? IndentForLevel[TheLine.Level]\n"
  3806. " : TheLine * 2,\n"
  3807. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  3808. getLLVMStyleWithColumns(70));
  3809. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  3810. " ? aaaaaaaaaaaaaaa\n"
  3811. " : bbbbbbbbbbbbbbb //\n"
  3812. " ? ccccccccccccccc\n"
  3813. " : ddddddddddddddd;");
  3814. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  3815. " ? aaaaaaaaaaaaaaa\n"
  3816. " : (bbbbbbbbbbbbbbb //\n"
  3817. " ? ccccccccccccccc\n"
  3818. " : ddddddddddddddd);");
  3819. verifyFormat(
  3820. "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3821. " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3822. " aaaaaaaaaaaaaaaaaaaaa +\n"
  3823. " aaaaaaaaaaaaaaaaaaaaa\n"
  3824. " : aaaaaaaaaa;");
  3825. verifyFormat(
  3826. "aaaaaa = aaaaaaaaaaaa\n"
  3827. " ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3828. " : aaaaaaaaaaaaaaaaaaaaaa\n"
  3829. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3830. FormatStyle NoBinPacking = getLLVMStyle();
  3831. NoBinPacking.BinPackArguments = false;
  3832. verifyFormat(
  3833. "void f() {\n"
  3834. " g(aaa,\n"
  3835. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  3836. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3837. " ? aaaaaaaaaaaaaaa\n"
  3838. " : aaaaaaaaaaaaaaa);\n"
  3839. "}",
  3840. NoBinPacking);
  3841. verifyFormat(
  3842. "void f() {\n"
  3843. " g(aaa,\n"
  3844. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  3845. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3846. " ?: aaaaaaaaaaaaaaa);\n"
  3847. "}",
  3848. NoBinPacking);
  3849. verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
  3850. " // comment.\n"
  3851. " ccccccccccccccccccccccccccccccccccccccc\n"
  3852. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3853. " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
  3854. // Assignments in conditional expressions. Apparently not uncommon :-(.
  3855. verifyFormat("return a != b\n"
  3856. " // comment\n"
  3857. " ? a = b\n"
  3858. " : a = b;");
  3859. verifyFormat("return a != b\n"
  3860. " // comment\n"
  3861. " ? a = a != b\n"
  3862. " // comment\n"
  3863. " ? a = b\n"
  3864. " : a\n"
  3865. " : a;\n");
  3866. verifyFormat("return a != b\n"
  3867. " // comment\n"
  3868. " ? a\n"
  3869. " : a = a != b\n"
  3870. " // comment\n"
  3871. " ? a = b\n"
  3872. " : a;");
  3873. }
  3874. TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
  3875. FormatStyle Style = getLLVMStyle();
  3876. Style.BreakBeforeTernaryOperators = false;
  3877. Style.ColumnLimit = 70;
  3878. verifyFormat(
  3879. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  3880. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  3881. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3882. Style);
  3883. verifyFormat(
  3884. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  3885. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3886. Style);
  3887. verifyFormat(
  3888. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
  3889. " aaaaaaaaaaaaa);",
  3890. Style);
  3891. verifyFormat(
  3892. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3893. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  3894. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3895. " aaaaaaaaaaaaa);",
  3896. Style);
  3897. verifyFormat(
  3898. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3899. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3900. " aaaaaaaaaaaaa);",
  3901. Style);
  3902. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  3903. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3904. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  3905. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3906. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3907. Style);
  3908. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3909. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  3910. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3911. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  3912. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3913. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3914. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3915. Style);
  3916. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3917. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
  3918. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3919. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3920. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3921. Style);
  3922. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  3923. " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  3924. " aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  3925. Style);
  3926. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  3927. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  3928. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  3929. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  3930. Style);
  3931. verifyFormat(
  3932. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  3933. " aaaaaaaaaaaaaaa :\n"
  3934. " aaaaaaaaaaaaaaa;",
  3935. Style);
  3936. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  3937. " aaaaaaaaa ?\n"
  3938. " b :\n"
  3939. " c);",
  3940. Style);
  3941. verifyFormat(
  3942. "unsigned Indent =\n"
  3943. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n"
  3944. " IndentForLevel[TheLine.Level] :\n"
  3945. " TheLine * 2,\n"
  3946. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  3947. Style);
  3948. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  3949. " aaaaaaaaaaaaaaa :\n"
  3950. " bbbbbbbbbbbbbbb ? //\n"
  3951. " ccccccccccccccc :\n"
  3952. " ddddddddddddddd;",
  3953. Style);
  3954. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  3955. " aaaaaaaaaaaaaaa :\n"
  3956. " (bbbbbbbbbbbbbbb ? //\n"
  3957. " ccccccccccccccc :\n"
  3958. " ddddddddddddddd);",
  3959. Style);
  3960. }
  3961. TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
  3962. verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
  3963. " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
  3964. verifyFormat("bool a = true, b = false;");
  3965. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3966. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
  3967. " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
  3968. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
  3969. verifyFormat(
  3970. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  3971. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
  3972. " d = e && f;");
  3973. verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
  3974. " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
  3975. verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  3976. " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
  3977. verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
  3978. " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
  3979. // FIXME: If multiple variables are defined, the "*" needs to move to the new
  3980. // line. Also fix indent for breaking after the type, this looks bad.
  3981. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
  3982. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
  3983. " * b = bbbbbbbbbbbbbbbbbbb;",
  3984. getGoogleStyle());
  3985. // Not ideal, but pointer-with-type does not allow much here.
  3986. verifyGoogleFormat(
  3987. "aaaaaaaaa* a = aaaaaaaaaaaaaaaaaaa, * b = bbbbbbbbbbbbbbbbbbb,\n"
  3988. " * b = bbbbbbbbbbbbbbbbbbb, * d = ddddddddddddddddddd;");
  3989. }
  3990. TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
  3991. verifyFormat("arr[foo ? bar : baz];");
  3992. verifyFormat("f()[foo ? bar : baz];");
  3993. verifyFormat("(a + b)[foo ? bar : baz];");
  3994. verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
  3995. }
  3996. TEST_F(FormatTest, AlignsStringLiterals) {
  3997. verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
  3998. " \"short literal\");");
  3999. verifyFormat(
  4000. "looooooooooooooooooooooooongFunction(\n"
  4001. " \"short literal\"\n"
  4002. " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
  4003. verifyFormat("someFunction(\"Always break between multi-line\"\n"
  4004. " \" string literals\",\n"
  4005. " and, other, parameters);");
  4006. EXPECT_EQ("fun + \"1243\" /* comment */\n"
  4007. " \"5678\";",
  4008. format("fun + \"1243\" /* comment */\n"
  4009. " \"5678\";",
  4010. getLLVMStyleWithColumns(28)));
  4011. EXPECT_EQ(
  4012. "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  4013. " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
  4014. " \"aaaaaaaaaaaaaaaa\";",
  4015. format("aaaaaa ="
  4016. "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  4017. "aaaaaaaaaaaaaaaaaaaaa\" "
  4018. "\"aaaaaaaaaaaaaaaa\";"));
  4019. verifyFormat("a = a + \"a\"\n"
  4020. " \"a\"\n"
  4021. " \"a\";");
  4022. verifyFormat("f(\"a\", \"b\"\n"
  4023. " \"c\");");
  4024. verifyFormat(
  4025. "#define LL_FORMAT \"ll\"\n"
  4026. "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
  4027. " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
  4028. verifyFormat("#define A(X) \\\n"
  4029. " \"aaaaa\" #X \"bbbbbb\" \\\n"
  4030. " \"ccccc\"",
  4031. getLLVMStyleWithColumns(23));
  4032. verifyFormat("#define A \"def\"\n"
  4033. "f(\"abc\" A \"ghi\"\n"
  4034. " \"jkl\");");
  4035. verifyFormat("f(L\"a\"\n"
  4036. " L\"b\")");
  4037. verifyFormat("#define A(X) \\\n"
  4038. " L\"aaaaa\" #X L\"bbbbbb\" \\\n"
  4039. " L\"ccccc\"",
  4040. getLLVMStyleWithColumns(25));
  4041. }
  4042. TEST_F(FormatTest, AlwaysBreakAfterDefinitionReturnType) {
  4043. FormatStyle AfterType = getLLVMStyle();
  4044. AfterType.AlwaysBreakAfterDefinitionReturnType = true;
  4045. verifyFormat("const char *\n"
  4046. "f(void) {\n" // Break here.
  4047. " return \"\";\n"
  4048. "}\n"
  4049. "const char *bar(void);\n", // No break here.
  4050. AfterType);
  4051. verifyFormat("template <class T>\n"
  4052. "T *\n"
  4053. "f(T &c) {\n" // Break here.
  4054. " return NULL;\n"
  4055. "}\n"
  4056. "template <class T> T *f(T &c);\n", // No break here.
  4057. AfterType);
  4058. AfterType.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  4059. verifyFormat("const char *\n"
  4060. "f(void)\n" // Break here.
  4061. "{\n"
  4062. " return \"\";\n"
  4063. "}\n"
  4064. "const char *bar(void);\n", // No break here.
  4065. AfterType);
  4066. verifyFormat("template <class T>\n"
  4067. "T *\n" // Problem here: no line break
  4068. "f(T &c)\n" // Break here.
  4069. "{\n"
  4070. " return NULL;\n"
  4071. "}\n"
  4072. "template <class T> T *f(T &c);\n", // No break here.
  4073. AfterType);
  4074. }
  4075. TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
  4076. FormatStyle NoBreak = getLLVMStyle();
  4077. NoBreak.AlwaysBreakBeforeMultilineStrings = false;
  4078. FormatStyle Break = getLLVMStyle();
  4079. Break.AlwaysBreakBeforeMultilineStrings = true;
  4080. verifyFormat("aaaa = \"bbbb\"\n"
  4081. " \"cccc\";",
  4082. NoBreak);
  4083. verifyFormat("aaaa =\n"
  4084. " \"bbbb\"\n"
  4085. " \"cccc\";",
  4086. Break);
  4087. verifyFormat("aaaa(\"bbbb\"\n"
  4088. " \"cccc\");",
  4089. NoBreak);
  4090. verifyFormat("aaaa(\n"
  4091. " \"bbbb\"\n"
  4092. " \"cccc\");",
  4093. Break);
  4094. verifyFormat("aaaa(qqq, \"bbbb\"\n"
  4095. " \"cccc\");",
  4096. NoBreak);
  4097. verifyFormat("aaaa(qqq,\n"
  4098. " \"bbbb\"\n"
  4099. " \"cccc\");",
  4100. Break);
  4101. verifyFormat("aaaa(qqq,\n"
  4102. " L\"bbbb\"\n"
  4103. " L\"cccc\");",
  4104. Break);
  4105. // As we break before unary operators, breaking right after them is bad.
  4106. verifyFormat("string foo = abc ? \"x\"\n"
  4107. " \"blah blah blah blah blah blah\"\n"
  4108. " : \"y\";",
  4109. Break);
  4110. // Don't break if there is no column gain.
  4111. verifyFormat("f(\"aaaa\"\n"
  4112. " \"bbbb\");",
  4113. Break);
  4114. // Treat literals with escaped newlines like multi-line string literals.
  4115. EXPECT_EQ("x = \"a\\\n"
  4116. "b\\\n"
  4117. "c\";",
  4118. format("x = \"a\\\n"
  4119. "b\\\n"
  4120. "c\";",
  4121. NoBreak));
  4122. EXPECT_EQ("x =\n"
  4123. " \"a\\\n"
  4124. "b\\\n"
  4125. "c\";",
  4126. format("x = \"a\\\n"
  4127. "b\\\n"
  4128. "c\";",
  4129. Break));
  4130. // Exempt ObjC strings for now.
  4131. EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
  4132. " \"bbbb\";",
  4133. format("NSString *const kString = @\"aaaa\"\n"
  4134. "\"bbbb\";",
  4135. Break));
  4136. }
  4137. TEST_F(FormatTest, AlignsPipes) {
  4138. verifyFormat(
  4139. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4140. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4141. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4142. verifyFormat(
  4143. "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
  4144. " << aaaaaaaaaaaaaaaaaaaa;");
  4145. verifyFormat(
  4146. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4147. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4148. verifyFormat(
  4149. "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
  4150. " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
  4151. " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
  4152. verifyFormat(
  4153. "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4154. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4155. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4156. verifyFormat(
  4157. "llvm::errs() << \"a: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4158. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4159. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4160. verifyFormat(
  4161. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4162. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4163. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4164. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4165. verifyFormat(
  4166. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4167. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4168. verifyFormat("return out << \"somepacket = {\\n\"\n"
  4169. " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
  4170. " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
  4171. " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
  4172. " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
  4173. " << \"}\";");
  4174. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  4175. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  4176. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
  4177. verifyFormat(
  4178. "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
  4179. " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
  4180. " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
  4181. " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
  4182. " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
  4183. verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
  4184. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4185. verifyFormat(
  4186. "void f() {\n"
  4187. " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
  4188. " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  4189. "}");
  4190. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
  4191. " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
  4192. // Breaking before the first "<<" is generally not desirable.
  4193. verifyFormat(
  4194. "llvm::errs()\n"
  4195. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4196. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4197. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4198. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4199. getLLVMStyleWithColumns(70));
  4200. verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4201. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4202. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4203. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4204. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4205. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4206. getLLVMStyleWithColumns(70));
  4207. // But sometimes, breaking before the first "<<" is desirable.
  4208. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
  4209. " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
  4210. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
  4211. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4212. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4213. verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
  4214. " << BEF << IsTemplate << Description << E->getType();");
  4215. verifyFormat(
  4216. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4217. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4218. // Incomplete string literal.
  4219. EXPECT_EQ("llvm::errs() << \"\n"
  4220. " << a;",
  4221. format("llvm::errs() << \"\n<<a;"));
  4222. verifyFormat("void f() {\n"
  4223. " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
  4224. " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
  4225. "}");
  4226. }
  4227. TEST_F(FormatTest, UnderstandsEquals) {
  4228. verifyFormat(
  4229. "aaaaaaaaaaaaaaaaa =\n"
  4230. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4231. verifyFormat(
  4232. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4233. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  4234. verifyFormat(
  4235. "if (a) {\n"
  4236. " f();\n"
  4237. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4238. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  4239. "}");
  4240. verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4241. " 100000000 + 10000000) {\n}");
  4242. }
  4243. TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
  4244. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  4245. " .looooooooooooooooooooooooooooooooooooooongFunction();");
  4246. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  4247. " ->looooooooooooooooooooooooooooooooooooooongFunction();");
  4248. verifyFormat(
  4249. "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
  4250. " Parameter2);");
  4251. verifyFormat(
  4252. "ShortObject->shortFunction(\n"
  4253. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
  4254. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
  4255. verifyFormat("loooooooooooooongFunction(\n"
  4256. " LoooooooooooooongObject->looooooooooooooooongFunction());");
  4257. verifyFormat(
  4258. "function(LoooooooooooooooooooooooooooooooooooongObject\n"
  4259. " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
  4260. verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  4261. " .WillRepeatedly(Return(SomeValue));");
  4262. verifyFormat("void f() {\n"
  4263. " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  4264. " .Times(2)\n"
  4265. " .WillRepeatedly(Return(SomeValue));\n"
  4266. "}");
  4267. verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
  4268. " ccccccccccccccccccccccc);");
  4269. verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4270. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).aaaaa(aaaaa),\n"
  4271. " aaaaaaaaaaaaaaaaaaaaa);");
  4272. verifyFormat("void f() {\n"
  4273. " aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4274. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
  4275. "}");
  4276. verifyFormat(
  4277. "aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4278. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4279. " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4280. " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4281. " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  4282. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4283. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4284. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4285. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
  4286. "}");
  4287. // Here, it is not necessary to wrap at "." or "->".
  4288. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
  4289. " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  4290. verifyFormat(
  4291. "aaaaaaaaaaa->aaaaaaaaa(\n"
  4292. " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4293. " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
  4294. verifyFormat(
  4295. "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4296. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
  4297. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
  4298. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  4299. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
  4300. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  4301. // FIXME: Should we break before .a()?
  4302. verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4303. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).a();");
  4304. FormatStyle NoBinPacking = getLLVMStyle();
  4305. NoBinPacking.BinPackParameters = false;
  4306. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  4307. " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  4308. " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
  4309. " aaaaaaaaaaaaaaaaaaa,\n"
  4310. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4311. NoBinPacking);
  4312. // If there is a subsequent call, change to hanging indentation.
  4313. verifyFormat(
  4314. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4315. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
  4316. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4317. verifyFormat(
  4318. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4319. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
  4320. verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4321. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4322. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4323. verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4324. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4325. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  4326. }
  4327. TEST_F(FormatTest, WrapsTemplateDeclarations) {
  4328. verifyFormat("template <typename T>\n"
  4329. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  4330. verifyFormat("template <typename T>\n"
  4331. "// T should be one of {A, B}.\n"
  4332. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  4333. verifyFormat(
  4334. "template <typename T>\n"
  4335. "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
  4336. verifyFormat("template <typename T>\n"
  4337. "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
  4338. " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
  4339. verifyFormat(
  4340. "template <typename T>\n"
  4341. "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
  4342. " int Paaaaaaaaaaaaaaaaaaaaram2);");
  4343. verifyFormat(
  4344. "template <typename T>\n"
  4345. "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
  4346. " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
  4347. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4348. verifyFormat("template <typename T>\n"
  4349. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4350. " int aaaaaaaaaaaaaaaaaaaaaa);");
  4351. verifyFormat(
  4352. "template <typename T1, typename T2 = char, typename T3 = char,\n"
  4353. " typename T4 = char>\n"
  4354. "void f();");
  4355. verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
  4356. " template <typename> class cccccccccccccccccccccc,\n"
  4357. " typename ddddddddddddd>\n"
  4358. "class C {};");
  4359. verifyFormat(
  4360. "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
  4361. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4362. verifyFormat("void f() {\n"
  4363. " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
  4364. " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
  4365. "}");
  4366. verifyFormat("template <typename T> class C {};");
  4367. verifyFormat("template <typename T> void f();");
  4368. verifyFormat("template <typename T> void f() {}");
  4369. verifyFormat(
  4370. "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  4371. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4372. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
  4373. " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  4374. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4375. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
  4376. " bbbbbbbbbbbbbbbbbbbbbbbb);",
  4377. getLLVMStyleWithColumns(72));
  4378. EXPECT_EQ("static_cast<A< //\n"
  4379. " B> *>(\n"
  4380. "\n"
  4381. " );",
  4382. format("static_cast<A<//\n"
  4383. " B>*>(\n"
  4384. "\n"
  4385. " );"));
  4386. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4387. " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
  4388. FormatStyle AlwaysBreak = getLLVMStyle();
  4389. AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
  4390. verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
  4391. verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
  4392. verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
  4393. verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4394. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
  4395. " ccccccccccccccccccccccccccccccccccccccccccccccc);");
  4396. verifyFormat("template <template <typename> class Fooooooo,\n"
  4397. " template <typename> class Baaaaaaar>\n"
  4398. "struct C {};",
  4399. AlwaysBreak);
  4400. verifyFormat("template <typename T> // T can be A, B or C.\n"
  4401. "struct C {};",
  4402. AlwaysBreak);
  4403. }
  4404. TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
  4405. verifyFormat(
  4406. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4407. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4408. verifyFormat(
  4409. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4410. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4411. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  4412. // FIXME: Should we have the extra indent after the second break?
  4413. verifyFormat(
  4414. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4415. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4416. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4417. verifyFormat(
  4418. "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
  4419. " cccccccccccccccccccccccccccccccccccccccccccccc());");
  4420. // Breaking at nested name specifiers is generally not desirable.
  4421. verifyFormat(
  4422. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4423. " aaaaaaaaaaaaaaaaaaaaaaa);");
  4424. verifyFormat(
  4425. "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4426. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4427. " aaaaaaaaaaaaaaaaaaaaa);",
  4428. getLLVMStyleWithColumns(74));
  4429. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4430. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4431. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4432. }
  4433. TEST_F(FormatTest, UnderstandsTemplateParameters) {
  4434. verifyFormat("A<int> a;");
  4435. verifyFormat("A<A<A<int>>> a;");
  4436. verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
  4437. verifyFormat("bool x = a < 1 || 2 > a;");
  4438. verifyFormat("bool x = 5 < f<int>();");
  4439. verifyFormat("bool x = f<int>() > 5;");
  4440. verifyFormat("bool x = 5 < a<int>::x;");
  4441. verifyFormat("bool x = a < 4 ? a > 2 : false;");
  4442. verifyFormat("bool x = f() ? a < 2 : a > 2;");
  4443. verifyGoogleFormat("A<A<int>> a;");
  4444. verifyGoogleFormat("A<A<A<int>>> a;");
  4445. verifyGoogleFormat("A<A<A<A<int>>>> a;");
  4446. verifyGoogleFormat("A<A<int> > a;");
  4447. verifyGoogleFormat("A<A<A<int> > > a;");
  4448. verifyGoogleFormat("A<A<A<A<int> > > > a;");
  4449. verifyGoogleFormat("A<::A<int>> a;");
  4450. verifyGoogleFormat("A<::A> a;");
  4451. verifyGoogleFormat("A< ::A> a;");
  4452. verifyGoogleFormat("A< ::A<int> > a;");
  4453. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
  4454. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
  4455. EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
  4456. EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
  4457. verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
  4458. verifyFormat("test >> a >> b;");
  4459. verifyFormat("test << a >> b;");
  4460. verifyFormat("f<int>();");
  4461. verifyFormat("template <typename T> void f() {}");
  4462. // Not template parameters.
  4463. verifyFormat("return a < b && c > d;");
  4464. verifyFormat("void f() {\n"
  4465. " while (a < b && c > d) {\n"
  4466. " }\n"
  4467. "}");
  4468. verifyFormat("template <typename... Types>\n"
  4469. "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
  4470. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4471. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
  4472. getLLVMStyleWithColumns(60));
  4473. verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
  4474. verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
  4475. }
  4476. TEST_F(FormatTest, UnderstandsBinaryOperators) {
  4477. verifyFormat("COMPARE(a, ==, b);");
  4478. }
  4479. TEST_F(FormatTest, UnderstandsPointersToMembers) {
  4480. verifyFormat("int A::*x;");
  4481. verifyFormat("int (S::*func)(void *);");
  4482. verifyFormat("void f() { int (S::*func)(void *); }");
  4483. verifyFormat("typedef bool *(Class::*Member)() const;");
  4484. verifyFormat("void f() {\n"
  4485. " (a->*f)();\n"
  4486. " a->*x;\n"
  4487. " (a.*f)();\n"
  4488. " ((*a).*f)();\n"
  4489. " a.*x;\n"
  4490. "}");
  4491. verifyFormat("void f() {\n"
  4492. " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  4493. " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
  4494. "}");
  4495. verifyFormat(
  4496. "(aaaaaaaaaa->*bbbbbbb)(\n"
  4497. " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  4498. FormatStyle Style = getLLVMStyle();
  4499. Style.PointerAlignment = FormatStyle::PAS_Left;
  4500. verifyFormat("typedef bool* (Class::*Member)() const;", Style);
  4501. }
  4502. TEST_F(FormatTest, UnderstandsUnaryOperators) {
  4503. verifyFormat("int a = -2;");
  4504. verifyFormat("f(-1, -2, -3);");
  4505. verifyFormat("a[-1] = 5;");
  4506. verifyFormat("int a = 5 + -2;");
  4507. verifyFormat("if (i == -1) {\n}");
  4508. verifyFormat("if (i != -1) {\n}");
  4509. verifyFormat("if (i > -1) {\n}");
  4510. verifyFormat("if (i < -1) {\n}");
  4511. verifyFormat("++(a->f());");
  4512. verifyFormat("--(a->f());");
  4513. verifyFormat("(a->f())++;");
  4514. verifyFormat("a[42]++;");
  4515. verifyFormat("if (!(a->f())) {\n}");
  4516. verifyFormat("a-- > b;");
  4517. verifyFormat("b ? -a : c;");
  4518. verifyFormat("n * sizeof char16;");
  4519. verifyFormat("n * alignof char16;", getGoogleStyle());
  4520. verifyFormat("sizeof(char);");
  4521. verifyFormat("alignof(char);", getGoogleStyle());
  4522. verifyFormat("return -1;");
  4523. verifyFormat("switch (a) {\n"
  4524. "case -1:\n"
  4525. " break;\n"
  4526. "}");
  4527. verifyFormat("#define X -1");
  4528. verifyFormat("#define X -kConstant");
  4529. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
  4530. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
  4531. verifyFormat("int a = /* confusing comment */ -1;");
  4532. // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
  4533. verifyFormat("int a = i /* confusing comment */++;");
  4534. }
  4535. TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
  4536. verifyFormat("if (!aaaaaaaaaa( // break\n"
  4537. " aaaaa)) {\n"
  4538. "}");
  4539. verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
  4540. " aaaaa));");
  4541. verifyFormat("*aaa = aaaaaaa( // break\n"
  4542. " bbbbbb);");
  4543. }
  4544. TEST_F(FormatTest, UnderstandsOverloadedOperators) {
  4545. verifyFormat("bool operator<();");
  4546. verifyFormat("bool operator>();");
  4547. verifyFormat("bool operator=();");
  4548. verifyFormat("bool operator==();");
  4549. verifyFormat("bool operator!=();");
  4550. verifyFormat("int operator+();");
  4551. verifyFormat("int operator++();");
  4552. verifyFormat("bool operator();");
  4553. verifyFormat("bool operator()();");
  4554. verifyFormat("bool operator[]();");
  4555. verifyFormat("operator bool();");
  4556. verifyFormat("operator int();");
  4557. verifyFormat("operator void *();");
  4558. verifyFormat("operator SomeType<int>();");
  4559. verifyFormat("operator SomeType<int, int>();");
  4560. verifyFormat("operator SomeType<SomeType<int>>();");
  4561. verifyFormat("void *operator new(std::size_t size);");
  4562. verifyFormat("void *operator new[](std::size_t size);");
  4563. verifyFormat("void operator delete(void *ptr);");
  4564. verifyFormat("void operator delete[](void *ptr);");
  4565. verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
  4566. "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
  4567. verifyFormat(
  4568. "ostream &operator<<(ostream &OutputStream,\n"
  4569. " SomeReallyLongType WithSomeReallyLongValue);");
  4570. verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
  4571. " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
  4572. " return left.group < right.group;\n"
  4573. "}");
  4574. verifyFormat("SomeType &operator=(const SomeType &S);");
  4575. verifyFormat("f.template operator()<int>();");
  4576. verifyGoogleFormat("operator void*();");
  4577. verifyGoogleFormat("operator SomeType<SomeType<int>>();");
  4578. verifyGoogleFormat("operator ::A();");
  4579. verifyFormat("using A::operator+;");
  4580. verifyFormat("Deleted &operator=(const Deleted &)& = default;");
  4581. verifyFormat("Deleted &operator=(const Deleted &)&& = delete;");
  4582. verifyGoogleFormat("Deleted& operator=(const Deleted&)& = default;");
  4583. verifyGoogleFormat("Deleted& operator=(const Deleted&)&& = delete;");
  4584. verifyFormat("string // break\n"
  4585. "operator()() & {}");
  4586. verifyFormat("string // break\n"
  4587. "operator()() && {}");
  4588. }
  4589. TEST_F(FormatTest, UnderstandsNewAndDelete) {
  4590. verifyFormat("void f() {\n"
  4591. " A *a = new A;\n"
  4592. " A *a = new (placement) A;\n"
  4593. " delete a;\n"
  4594. " delete (A *)a;\n"
  4595. "}");
  4596. verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
  4597. " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
  4598. verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4599. " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
  4600. " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
  4601. verifyFormat("delete[] h->p;");
  4602. }
  4603. TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
  4604. verifyFormat("int *f(int *a) {}");
  4605. verifyFormat("int main(int argc, char **argv) {}");
  4606. verifyFormat("Test::Test(int b) : a(b * b) {}");
  4607. verifyIndependentOfContext("f(a, *a);");
  4608. verifyFormat("void g() { f(*a); }");
  4609. verifyIndependentOfContext("int a = b * 10;");
  4610. verifyIndependentOfContext("int a = 10 * b;");
  4611. verifyIndependentOfContext("int a = b * c;");
  4612. verifyIndependentOfContext("int a += b * c;");
  4613. verifyIndependentOfContext("int a -= b * c;");
  4614. verifyIndependentOfContext("int a *= b * c;");
  4615. verifyIndependentOfContext("int a /= b * c;");
  4616. verifyIndependentOfContext("int a = *b;");
  4617. verifyIndependentOfContext("int a = *b * c;");
  4618. verifyIndependentOfContext("int a = b * *c;");
  4619. verifyIndependentOfContext("return 10 * b;");
  4620. verifyIndependentOfContext("return *b * *c;");
  4621. verifyIndependentOfContext("return a & ~b;");
  4622. verifyIndependentOfContext("f(b ? *c : *d);");
  4623. verifyIndependentOfContext("int a = b ? *c : *d;");
  4624. verifyIndependentOfContext("*b = a;");
  4625. verifyIndependentOfContext("a * ~b;");
  4626. verifyIndependentOfContext("a * !b;");
  4627. verifyIndependentOfContext("a * +b;");
  4628. verifyIndependentOfContext("a * -b;");
  4629. verifyIndependentOfContext("a * ++b;");
  4630. verifyIndependentOfContext("a * --b;");
  4631. verifyIndependentOfContext("a[4] * b;");
  4632. verifyIndependentOfContext("a[a * a] = 1;");
  4633. verifyIndependentOfContext("f() * b;");
  4634. verifyIndependentOfContext("a * [self dostuff];");
  4635. verifyIndependentOfContext("int x = a * (a + b);");
  4636. verifyIndependentOfContext("(a *)(a + b);");
  4637. verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
  4638. verifyIndependentOfContext("int *pa = (int *)&a;");
  4639. verifyIndependentOfContext("return sizeof(int **);");
  4640. verifyIndependentOfContext("return sizeof(int ******);");
  4641. verifyIndependentOfContext("return (int **&)a;");
  4642. verifyIndependentOfContext("f((*PointerToArray)[10]);");
  4643. verifyFormat("void f(Type (*parameter)[10]) {}");
  4644. verifyGoogleFormat("return sizeof(int**);");
  4645. verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
  4646. verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
  4647. verifyFormat("auto a = [](int **&, int ***) {};");
  4648. verifyFormat("auto PointerBinding = [](const char *S) {};");
  4649. verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
  4650. verifyFormat("[](const decltype(*a) &value) {}");
  4651. verifyIndependentOfContext("typedef void (*f)(int *a);");
  4652. verifyIndependentOfContext("int i{a * b};");
  4653. verifyIndependentOfContext("aaa && aaa->f();");
  4654. verifyIndependentOfContext("int x = ~*p;");
  4655. verifyFormat("Constructor() : a(a), area(width * height) {}");
  4656. verifyIndependentOfContext("InvalidRegions[*R] = 0;");
  4657. verifyIndependentOfContext("A<int *> a;");
  4658. verifyIndependentOfContext("A<int **> a;");
  4659. verifyIndependentOfContext("A<int *, int *> a;");
  4660. verifyIndependentOfContext("A<int *[]> a;");
  4661. verifyIndependentOfContext(
  4662. "const char *const p = reinterpret_cast<const char *const>(q);");
  4663. verifyIndependentOfContext("A<int **, int **> a;");
  4664. verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
  4665. verifyFormat("for (char **a = b; *a; ++a) {\n}");
  4666. verifyFormat("for (; a && b;) {\n}");
  4667. verifyFormat("bool foo = true && [] { return false; }();");
  4668. verifyFormat(
  4669. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4670. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4671. verifyGoogleFormat("int main(int argc, char** argv) {}");
  4672. verifyGoogleFormat("A<int*> a;");
  4673. verifyGoogleFormat("A<int**> a;");
  4674. verifyGoogleFormat("A<int*, int*> a;");
  4675. verifyGoogleFormat("A<int**, int**> a;");
  4676. verifyGoogleFormat("f(b ? *c : *d);");
  4677. verifyGoogleFormat("int a = b ? *c : *d;");
  4678. verifyGoogleFormat("Type* t = **x;");
  4679. verifyGoogleFormat("Type* t = *++*x;");
  4680. verifyGoogleFormat("*++*x;");
  4681. verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
  4682. verifyGoogleFormat("Type* t = x++ * y;");
  4683. verifyGoogleFormat(
  4684. "const char* const p = reinterpret_cast<const char* const>(q);");
  4685. verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
  4686. verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
  4687. verifyGoogleFormat("template <typename T>\n"
  4688. "void f(int i = 0, SomeType** temps = NULL);");
  4689. FormatStyle Left = getLLVMStyle();
  4690. Left.PointerAlignment = FormatStyle::PAS_Left;
  4691. verifyFormat("x = *a(x) = *a(y);", Left);
  4692. verifyIndependentOfContext("a = *(x + y);");
  4693. verifyIndependentOfContext("a = &(x + y);");
  4694. verifyIndependentOfContext("*(x + y).call();");
  4695. verifyIndependentOfContext("&(x + y)->call();");
  4696. verifyFormat("void f() { &(*I).first; }");
  4697. verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
  4698. verifyFormat(
  4699. "int *MyValues = {\n"
  4700. " *A, // Operator detection might be confused by the '{'\n"
  4701. " *BB // Operator detection might be confused by previous comment\n"
  4702. "};");
  4703. verifyIndependentOfContext("if (int *a = &b)");
  4704. verifyIndependentOfContext("if (int &a = *b)");
  4705. verifyIndependentOfContext("if (a & b[i])");
  4706. verifyIndependentOfContext("if (a::b::c::d & b[i])");
  4707. verifyIndependentOfContext("if (*b[i])");
  4708. verifyIndependentOfContext("if (int *a = (&b))");
  4709. verifyIndependentOfContext("while (int *a = &b)");
  4710. verifyIndependentOfContext("size = sizeof *a;");
  4711. verifyFormat("void f() {\n"
  4712. " for (const int &v : Values) {\n"
  4713. " }\n"
  4714. "}");
  4715. verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
  4716. verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
  4717. verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
  4718. verifyFormat("#define A (!a * b)");
  4719. verifyFormat("#define MACRO \\\n"
  4720. " int *i = a * b; \\\n"
  4721. " void f(a *b);",
  4722. getLLVMStyleWithColumns(19));
  4723. verifyIndependentOfContext("A = new SomeType *[Length];");
  4724. verifyIndependentOfContext("A = new SomeType *[Length]();");
  4725. verifyIndependentOfContext("T **t = new T *;");
  4726. verifyIndependentOfContext("T **t = new T *();");
  4727. verifyGoogleFormat("A = new SomeType* [Length]();");
  4728. verifyGoogleFormat("A = new SomeType* [Length];");
  4729. verifyGoogleFormat("T** t = new T*;");
  4730. verifyGoogleFormat("T** t = new T*();");
  4731. FormatStyle PointerLeft = getLLVMStyle();
  4732. PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
  4733. verifyFormat("delete *x;", PointerLeft);
  4734. verifyFormat("STATIC_ASSERT((a & b) == 0);");
  4735. verifyFormat("STATIC_ASSERT(0 == (a & b));");
  4736. verifyFormat("template <bool a, bool b> "
  4737. "typename t::if<x && y>::type f() {}");
  4738. verifyFormat("template <int *y> f() {}");
  4739. verifyFormat("vector<int *> v;");
  4740. verifyFormat("vector<int *const> v;");
  4741. verifyFormat("vector<int *const **const *> v;");
  4742. verifyFormat("vector<int *volatile> v;");
  4743. verifyFormat("vector<a * b> v;");
  4744. verifyFormat("foo<b && false>();");
  4745. verifyFormat("foo<b & 1>();");
  4746. verifyFormat("decltype(*::std::declval<const T &>()) void F();");
  4747. verifyFormat(
  4748. "template <class T, class = typename std::enable_if<\n"
  4749. " std::is_integral<T>::value &&\n"
  4750. " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
  4751. "void F();",
  4752. getLLVMStyleWithColumns(76));
  4753. verifyFormat(
  4754. "template <class T,\n"
  4755. " class = typename ::std::enable_if<\n"
  4756. " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
  4757. "void F();",
  4758. getGoogleStyleWithColumns(68));
  4759. verifyIndependentOfContext("MACRO(int *i);");
  4760. verifyIndependentOfContext("MACRO(auto *a);");
  4761. verifyIndependentOfContext("MACRO(const A *a);");
  4762. verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
  4763. // FIXME: Is there a way to make this work?
  4764. // verifyIndependentOfContext("MACRO(A *a);");
  4765. verifyFormat("DatumHandle const *operator->() const { return input_; }");
  4766. EXPECT_EQ("#define OP(x) \\\n"
  4767. " ostream &operator<<(ostream &s, const A &a) { \\\n"
  4768. " return s << a.DebugString(); \\\n"
  4769. " }",
  4770. format("#define OP(x) \\\n"
  4771. " ostream &operator<<(ostream &s, const A &a) { \\\n"
  4772. " return s << a.DebugString(); \\\n"
  4773. " }",
  4774. getLLVMStyleWithColumns(50)));
  4775. // FIXME: We cannot handle this case yet; we might be able to figure out that
  4776. // foo<x> d > v; doesn't make sense.
  4777. verifyFormat("foo<a<b && c> d> v;");
  4778. FormatStyle PointerMiddle = getLLVMStyle();
  4779. PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
  4780. verifyFormat("delete *x;", PointerMiddle);
  4781. verifyFormat("int * x;", PointerMiddle);
  4782. verifyFormat("template <int * y> f() {}", PointerMiddle);
  4783. verifyFormat("int * f(int * a) {}", PointerMiddle);
  4784. verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
  4785. verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
  4786. verifyFormat("A<int *> a;", PointerMiddle);
  4787. verifyFormat("A<int **> a;", PointerMiddle);
  4788. verifyFormat("A<int *, int *> a;", PointerMiddle);
  4789. verifyFormat("A<int * []> a;", PointerMiddle);
  4790. verifyFormat("A = new SomeType * [Length]();", PointerMiddle);
  4791. verifyFormat("A = new SomeType * [Length];", PointerMiddle);
  4792. verifyFormat("T ** t = new T *;", PointerMiddle);
  4793. }
  4794. TEST_F(FormatTest, UnderstandsAttributes) {
  4795. verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
  4796. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
  4797. "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
  4798. }
  4799. TEST_F(FormatTest, UnderstandsEllipsis) {
  4800. verifyFormat("int printf(const char *fmt, ...);");
  4801. verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
  4802. verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
  4803. FormatStyle PointersLeft = getLLVMStyle();
  4804. PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
  4805. verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
  4806. }
  4807. TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
  4808. EXPECT_EQ("int *a;\n"
  4809. "int *a;\n"
  4810. "int *a;",
  4811. format("int *a;\n"
  4812. "int* a;\n"
  4813. "int *a;",
  4814. getGoogleStyle()));
  4815. EXPECT_EQ("int* a;\n"
  4816. "int* a;\n"
  4817. "int* a;",
  4818. format("int* a;\n"
  4819. "int* a;\n"
  4820. "int *a;",
  4821. getGoogleStyle()));
  4822. EXPECT_EQ("int *a;\n"
  4823. "int *a;\n"
  4824. "int *a;",
  4825. format("int *a;\n"
  4826. "int * a;\n"
  4827. "int * a;",
  4828. getGoogleStyle()));
  4829. }
  4830. TEST_F(FormatTest, UnderstandsRvalueReferences) {
  4831. verifyFormat("int f(int &&a) {}");
  4832. verifyFormat("int f(int a, char &&b) {}");
  4833. verifyFormat("void f() { int &&a = b; }");
  4834. verifyGoogleFormat("int f(int a, char&& b) {}");
  4835. verifyGoogleFormat("void f() { int&& a = b; }");
  4836. verifyIndependentOfContext("A<int &&> a;");
  4837. verifyIndependentOfContext("A<int &&, int &&> a;");
  4838. verifyGoogleFormat("A<int&&> a;");
  4839. verifyGoogleFormat("A<int&&, int&&> a;");
  4840. // Not rvalue references:
  4841. verifyFormat("template <bool B, bool C> class A {\n"
  4842. " static_assert(B && C, \"Something is wrong\");\n"
  4843. "};");
  4844. verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
  4845. verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
  4846. verifyFormat("#define A(a, b) (a && b)");
  4847. }
  4848. TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
  4849. verifyFormat("void f() {\n"
  4850. " x[aaaaaaaaa -\n"
  4851. " b] = 23;\n"
  4852. "}",
  4853. getLLVMStyleWithColumns(15));
  4854. }
  4855. TEST_F(FormatTest, FormatsCasts) {
  4856. verifyFormat("Type *A = static_cast<Type *>(P);");
  4857. verifyFormat("Type *A = (Type *)P;");
  4858. verifyFormat("Type *A = (vector<Type *, int *>)P;");
  4859. verifyFormat("int a = (int)(2.0f);");
  4860. verifyFormat("int a = (int)2.0f;");
  4861. verifyFormat("x[(int32)y];");
  4862. verifyFormat("x = (int32)y;");
  4863. verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
  4864. verifyFormat("int a = (int)*b;");
  4865. verifyFormat("int a = (int)2.0f;");
  4866. verifyFormat("int a = (int)~0;");
  4867. verifyFormat("int a = (int)++a;");
  4868. verifyFormat("int a = (int)sizeof(int);");
  4869. verifyFormat("int a = (int)+2;");
  4870. verifyFormat("my_int a = (my_int)2.0f;");
  4871. verifyFormat("my_int a = (my_int)sizeof(int);");
  4872. verifyFormat("return (my_int)aaa;");
  4873. verifyFormat("#define x ((int)-1)");
  4874. verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
  4875. verifyFormat("#define p(q) ((int *)&q)");
  4876. verifyFormat("fn(a)(b) + 1;");
  4877. verifyFormat("void f() { my_int a = (my_int)*b; }");
  4878. verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
  4879. verifyFormat("my_int a = (my_int)~0;");
  4880. verifyFormat("my_int a = (my_int)++a;");
  4881. verifyFormat("my_int a = (my_int)-2;");
  4882. verifyFormat("my_int a = (my_int)1;");
  4883. verifyFormat("my_int a = (my_int *)1;");
  4884. verifyFormat("my_int a = (const my_int)-1;");
  4885. verifyFormat("my_int a = (const my_int *)-1;");
  4886. verifyFormat("my_int a = (my_int)(my_int)-1;");
  4887. // FIXME: single value wrapped with paren will be treated as cast.
  4888. verifyFormat("void f(int i = (kValue)*kMask) {}");
  4889. verifyFormat("{ (void)F; }");
  4890. // Don't break after a cast's
  4891. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4892. " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
  4893. " bbbbbbbbbbbbbbbbbbbbbb);");
  4894. // These are not casts.
  4895. verifyFormat("void f(int *) {}");
  4896. verifyFormat("f(foo)->b;");
  4897. verifyFormat("f(foo).b;");
  4898. verifyFormat("f(foo)(b);");
  4899. verifyFormat("f(foo)[b];");
  4900. verifyFormat("[](foo) { return 4; }(bar);");
  4901. verifyFormat("(*funptr)(foo)[4];");
  4902. verifyFormat("funptrs[4](foo)[4];");
  4903. verifyFormat("void f(int *);");
  4904. verifyFormat("void f(int *) = 0;");
  4905. verifyFormat("void f(SmallVector<int>) {}");
  4906. verifyFormat("void f(SmallVector<int>);");
  4907. verifyFormat("void f(SmallVector<int>) = 0;");
  4908. verifyFormat("void f(int i = (kA * kB) & kMask) {}");
  4909. verifyFormat("int a = sizeof(int) * b;");
  4910. verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
  4911. verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
  4912. verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
  4913. verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
  4914. // These are not casts, but at some point were confused with casts.
  4915. verifyFormat("virtual void foo(int *) override;");
  4916. verifyFormat("virtual void foo(char &) const;");
  4917. verifyFormat("virtual void foo(int *a, char *) const;");
  4918. verifyFormat("int a = sizeof(int *) + b;");
  4919. verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
  4920. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
  4921. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4922. // FIXME: The indentation here is not ideal.
  4923. verifyFormat(
  4924. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4925. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
  4926. " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
  4927. }
  4928. TEST_F(FormatTest, FormatsFunctionTypes) {
  4929. verifyFormat("A<bool()> a;");
  4930. verifyFormat("A<SomeType()> a;");
  4931. verifyFormat("A<void (*)(int, std::string)> a;");
  4932. verifyFormat("A<void *(int)>;");
  4933. verifyFormat("void *(*a)(int *, SomeType *);");
  4934. verifyFormat("int (*func)(void *);");
  4935. verifyFormat("void f() { int (*func)(void *); }");
  4936. verifyFormat("template <class CallbackClass>\n"
  4937. "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
  4938. verifyGoogleFormat("A<void*(int*, SomeType*)>;");
  4939. verifyGoogleFormat("void* (*a)(int);");
  4940. verifyGoogleFormat(
  4941. "template <class CallbackClass>\n"
  4942. "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
  4943. // Other constructs can look somewhat like function types:
  4944. verifyFormat("A<sizeof(*x)> a;");
  4945. verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
  4946. verifyFormat("some_var = function(*some_pointer_var)[0];");
  4947. verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
  4948. }
  4949. TEST_F(FormatTest, BreaksLongVariableDeclarations) {
  4950. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  4951. " LoooooooooooooooooooooooooooooooooooooooongVariable;");
  4952. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
  4953. " LoooooooooooooooooooooooooooooooooooooooongVariable;");
  4954. // Different ways of ()-initializiation.
  4955. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  4956. " LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
  4957. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  4958. " LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
  4959. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  4960. " LoooooooooooooooooooooooooooooooooooooooongVariable({});");
  4961. }
  4962. TEST_F(FormatTest, BreaksLongDeclarations) {
  4963. verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
  4964. " AnotherNameForTheLongType;");
  4965. verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
  4966. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4967. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  4968. "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
  4969. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  4970. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  4971. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
  4972. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  4973. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  4974. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  4975. verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
  4976. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  4977. FormatStyle Indented = getLLVMStyle();
  4978. Indented.IndentWrappedFunctionNames = true;
  4979. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  4980. " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
  4981. Indented);
  4982. verifyFormat(
  4983. "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  4984. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  4985. Indented);
  4986. verifyFormat(
  4987. "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  4988. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  4989. Indented);
  4990. verifyFormat(
  4991. "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
  4992. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  4993. Indented);
  4994. // FIXME: Without the comment, this breaks after "(".
  4995. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
  4996. " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
  4997. getGoogleStyle());
  4998. verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
  4999. " int LoooooooooooooooooooongParam2) {}");
  5000. verifyFormat(
  5001. "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
  5002. " SourceLocation L, IdentifierIn *II,\n"
  5003. " Type *T) {}");
  5004. verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
  5005. "ReallyReallyLongFunctionName(\n"
  5006. " const std::string &SomeParameter,\n"
  5007. " const SomeType<string, SomeOtherTemplateParameter> &\n"
  5008. " ReallyReallyLongParameterName,\n"
  5009. " const SomeType<string, SomeOtherTemplateParameter> &\n"
  5010. " AnotherLongParameterName) {}");
  5011. verifyFormat("template <typename A>\n"
  5012. "SomeLoooooooooooooooooooooongType<\n"
  5013. " typename some_namespace::SomeOtherType<A>::Type>\n"
  5014. "Function() {}");
  5015. verifyGoogleFormat(
  5016. "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
  5017. " aaaaaaaaaaaaaaaaaaaaaaa;");
  5018. verifyGoogleFormat(
  5019. "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
  5020. " SourceLocation L) {}");
  5021. verifyGoogleFormat(
  5022. "some_namespace::LongReturnType\n"
  5023. "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
  5024. " int first_long_parameter, int second_parameter) {}");
  5025. verifyGoogleFormat("template <typename T>\n"
  5026. "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  5027. "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
  5028. verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5029. " int aaaaaaaaaaaaaaaaaaaaaaa);");
  5030. verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  5031. " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  5032. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5033. }
  5034. TEST_F(FormatTest, FormatsArrays) {
  5035. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5036. " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
  5037. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5038. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  5039. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5040. " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
  5041. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5042. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5043. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  5044. verifyFormat(
  5045. "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
  5046. " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5047. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  5048. verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
  5049. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
  5050. verifyFormat(
  5051. "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
  5052. " .aaaaaaa[0]\n"
  5053. " .aaaaaaaaaaaaaaaaaaaaaa();");
  5054. }
  5055. TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
  5056. verifyFormat("(a)->b();");
  5057. verifyFormat("--a;");
  5058. }
  5059. TEST_F(FormatTest, HandlesIncludeDirectives) {
  5060. verifyFormat("#include <string>\n"
  5061. "#include <a/b/c.h>\n"
  5062. "#include \"a/b/string\"\n"
  5063. "#include \"string.h\"\n"
  5064. "#include \"string.h\"\n"
  5065. "#include <a-a>\n"
  5066. "#include < path with space >\n"
  5067. "#include \"abc.h\" // this is included for ABC\n"
  5068. "#include \"some long include\" // with a comment\n"
  5069. "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
  5070. getLLVMStyleWithColumns(35));
  5071. EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\""));
  5072. EXPECT_EQ("#include <a>", format("#include<a>"));
  5073. verifyFormat("#import <string>");
  5074. verifyFormat("#import <a/b/c.h>");
  5075. verifyFormat("#import \"a/b/string\"");
  5076. verifyFormat("#import \"string.h\"");
  5077. verifyFormat("#import \"string.h\"");
  5078. verifyFormat("#if __has_include(<strstream>)\n"
  5079. "#include <strstream>\n"
  5080. "#endif");
  5081. verifyFormat("#define MY_IMPORT <a/b>");
  5082. // Protocol buffer definition or missing "#".
  5083. verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
  5084. getLLVMStyleWithColumns(30));
  5085. FormatStyle Style = getLLVMStyle();
  5086. Style.AlwaysBreakBeforeMultilineStrings = true;
  5087. Style.ColumnLimit = 0;
  5088. verifyFormat("#import \"abc.h\"", Style);
  5089. }
  5090. //===----------------------------------------------------------------------===//
  5091. // Error recovery tests.
  5092. //===----------------------------------------------------------------------===//
  5093. TEST_F(FormatTest, IncompleteParameterLists) {
  5094. FormatStyle NoBinPacking = getLLVMStyle();
  5095. NoBinPacking.BinPackParameters = false;
  5096. verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
  5097. " double *min_x,\n"
  5098. " double *max_x,\n"
  5099. " double *min_y,\n"
  5100. " double *max_y,\n"
  5101. " double *min_z,\n"
  5102. " double *max_z, ) {}",
  5103. NoBinPacking);
  5104. }
  5105. TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
  5106. verifyFormat("void f() { return; }\n42");
  5107. verifyFormat("void f() {\n"
  5108. " if (0)\n"
  5109. " return;\n"
  5110. "}\n"
  5111. "42");
  5112. verifyFormat("void f() { return }\n42");
  5113. verifyFormat("void f() {\n"
  5114. " if (0)\n"
  5115. " return\n"
  5116. "}\n"
  5117. "42");
  5118. }
  5119. TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
  5120. EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
  5121. EXPECT_EQ("void f() {\n"
  5122. " if (a)\n"
  5123. " return\n"
  5124. "}",
  5125. format("void f ( ) { if ( a ) return }"));
  5126. EXPECT_EQ("namespace N {\n"
  5127. "void f()\n"
  5128. "}",
  5129. format("namespace N { void f() }"));
  5130. EXPECT_EQ("namespace N {\n"
  5131. "void f() {}\n"
  5132. "void g()\n"
  5133. "}",
  5134. format("namespace N { void f( ) { } void g( ) }"));
  5135. }
  5136. TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
  5137. verifyFormat("int aaaaaaaa =\n"
  5138. " // Overlylongcomment\n"
  5139. " b;",
  5140. getLLVMStyleWithColumns(20));
  5141. verifyFormat("function(\n"
  5142. " ShortArgument,\n"
  5143. " LoooooooooooongArgument);\n",
  5144. getLLVMStyleWithColumns(20));
  5145. }
  5146. TEST_F(FormatTest, IncorrectAccessSpecifier) {
  5147. verifyFormat("public:");
  5148. verifyFormat("class A {\n"
  5149. "public\n"
  5150. " void f() {}\n"
  5151. "};");
  5152. verifyFormat("public\n"
  5153. "int qwerty;");
  5154. verifyFormat("public\n"
  5155. "B {}");
  5156. verifyFormat("public\n"
  5157. "{}");
  5158. verifyFormat("public\n"
  5159. "B { int x; }");
  5160. }
  5161. TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
  5162. verifyFormat("{");
  5163. verifyFormat("#})");
  5164. }
  5165. TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
  5166. verifyFormat("do {\n}");
  5167. verifyFormat("do {\n}\n"
  5168. "f();");
  5169. verifyFormat("do {\n}\n"
  5170. "wheeee(fun);");
  5171. verifyFormat("do {\n"
  5172. " f();\n"
  5173. "}");
  5174. }
  5175. TEST_F(FormatTest, IncorrectCodeMissingParens) {
  5176. verifyFormat("if {\n foo;\n foo();\n}");
  5177. verifyFormat("switch {\n foo;\n foo();\n}");
  5178. verifyFormat("for {\n foo;\n foo();\n}");
  5179. verifyFormat("while {\n foo;\n foo();\n}");
  5180. verifyFormat("do {\n foo;\n foo();\n} while;");
  5181. }
  5182. TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
  5183. verifyFormat("namespace {\n"
  5184. "class Foo { Foo (\n"
  5185. "};\n"
  5186. "} // comment");
  5187. }
  5188. TEST_F(FormatTest, IncorrectCodeErrorDetection) {
  5189. EXPECT_EQ("{\n {}\n", format("{\n{\n}\n"));
  5190. EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
  5191. EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
  5192. EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n"));
  5193. EXPECT_EQ("{\n"
  5194. " {\n"
  5195. " breakme(\n"
  5196. " qwe);\n"
  5197. " }\n",
  5198. format("{\n"
  5199. " {\n"
  5200. " breakme(qwe);\n"
  5201. "}\n",
  5202. getLLVMStyleWithColumns(10)));
  5203. }
  5204. TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
  5205. verifyFormat("int x = {\n"
  5206. " avariable,\n"
  5207. " b(alongervariable)};",
  5208. getLLVMStyleWithColumns(25));
  5209. }
  5210. TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
  5211. verifyFormat("return (a)(b){1, 2, 3};");
  5212. }
  5213. TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
  5214. verifyFormat("vector<int> x{1, 2, 3, 4};");
  5215. verifyFormat("vector<int> x{\n"
  5216. " 1, 2, 3, 4,\n"
  5217. "};");
  5218. verifyFormat("vector<T> x{{}, {}, {}, {}};");
  5219. verifyFormat("f({1, 2});");
  5220. verifyFormat("auto v = Foo{-1};");
  5221. verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
  5222. verifyFormat("Class::Class : member{1, 2, 3} {}");
  5223. verifyFormat("new vector<int>{1, 2, 3};");
  5224. verifyFormat("new int[3]{1, 2, 3};");
  5225. verifyFormat("new int{1};");
  5226. verifyFormat("return {arg1, arg2};");
  5227. verifyFormat("return {arg1, SomeType{parameter}};");
  5228. verifyFormat("int count = set<int>{f(), g(), h()}.size();");
  5229. verifyFormat("new T{arg1, arg2};");
  5230. verifyFormat("f(MyMap[{composite, key}]);");
  5231. verifyFormat("class Class {\n"
  5232. " T member = {arg1, arg2};\n"
  5233. "};");
  5234. verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
  5235. verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
  5236. verifyFormat("int a = std::is_integral<int>{} + 0;");
  5237. verifyFormat("int foo(int i) { return fo1{}(i); }");
  5238. verifyFormat("int foo(int i) { return fo1{}(i); }");
  5239. verifyFormat("auto i = decltype(x){};");
  5240. verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
  5241. verifyFormat("Node n{1, Node{1000}, //\n"
  5242. " 2};");
  5243. // In combination with BinPackParameters = false.
  5244. FormatStyle NoBinPacking = getLLVMStyle();
  5245. NoBinPacking.BinPackParameters = false;
  5246. verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
  5247. " bbbbb,\n"
  5248. " ccccc,\n"
  5249. " ddddd,\n"
  5250. " eeeee,\n"
  5251. " ffffff,\n"
  5252. " ggggg,\n"
  5253. " hhhhhh,\n"
  5254. " iiiiii,\n"
  5255. " jjjjjj,\n"
  5256. " kkkkkk};",
  5257. NoBinPacking);
  5258. verifyFormat("const Aaaaaa aaaaa = {\n"
  5259. " aaaaa,\n"
  5260. " bbbbb,\n"
  5261. " ccccc,\n"
  5262. " ddddd,\n"
  5263. " eeeee,\n"
  5264. " ffffff,\n"
  5265. " ggggg,\n"
  5266. " hhhhhh,\n"
  5267. " iiiiii,\n"
  5268. " jjjjjj,\n"
  5269. " kkkkkk,\n"
  5270. "};",
  5271. NoBinPacking);
  5272. verifyFormat(
  5273. "const Aaaaaa aaaaa = {\n"
  5274. " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n"
  5275. " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n"
  5276. " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
  5277. "};",
  5278. NoBinPacking);
  5279. // FIXME: The alignment of these trailing comments might be bad. Then again,
  5280. // this might be utterly useless in real code.
  5281. verifyFormat("Constructor::Constructor()\n"
  5282. " : some_value{ //\n"
  5283. " aaaaaaa, //\n"
  5284. " bbbbbbb} {}");
  5285. // In braced lists, the first comment is always assumed to belong to the
  5286. // first element. Thus, it can be moved to the next or previous line as
  5287. // appropriate.
  5288. EXPECT_EQ("function({// First element:\n"
  5289. " 1,\n"
  5290. " // Second element:\n"
  5291. " 2});",
  5292. format("function({\n"
  5293. " // First element:\n"
  5294. " 1,\n"
  5295. " // Second element:\n"
  5296. " 2});"));
  5297. EXPECT_EQ("std::vector<int> MyNumbers{\n"
  5298. " // First element:\n"
  5299. " 1,\n"
  5300. " // Second element:\n"
  5301. " 2};",
  5302. format("std::vector<int> MyNumbers{// First element:\n"
  5303. " 1,\n"
  5304. " // Second element:\n"
  5305. " 2};",
  5306. getLLVMStyleWithColumns(30)));
  5307. // A trailing comma should still lead to an enforced line break.
  5308. EXPECT_EQ("vector<int> SomeVector = {\n"
  5309. " // aaa\n"
  5310. " 1, 2,\n"
  5311. "};",
  5312. format("vector<int> SomeVector = { // aaa\n"
  5313. " 1, 2, };"));
  5314. FormatStyle ExtraSpaces = getLLVMStyle();
  5315. ExtraSpaces.Cpp11BracedListStyle = false;
  5316. ExtraSpaces.ColumnLimit = 75;
  5317. verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
  5318. verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
  5319. verifyFormat("f({ 1, 2 });", ExtraSpaces);
  5320. verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
  5321. verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
  5322. verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
  5323. verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
  5324. verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
  5325. verifyFormat("return { arg1, arg2 };", ExtraSpaces);
  5326. verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
  5327. verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
  5328. verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
  5329. verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
  5330. verifyFormat("class Class {\n"
  5331. " T member = { arg1, arg2 };\n"
  5332. "};",
  5333. ExtraSpaces);
  5334. verifyFormat(
  5335. "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5336. " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
  5337. " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  5338. " bbbbbbbbbbbbbbbbbbbb, bbbbb };",
  5339. ExtraSpaces);
  5340. verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
  5341. verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
  5342. ExtraSpaces);
  5343. verifyFormat(
  5344. "someFunction(OtherParam,\n"
  5345. " BracedList{ // comment 1 (Forcing interesting break)\n"
  5346. " param1, param2,\n"
  5347. " // comment 2\n"
  5348. " param3, param4\n"
  5349. " });",
  5350. ExtraSpaces);
  5351. verifyFormat(
  5352. "std::this_thread::sleep_for(\n"
  5353. " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
  5354. ExtraSpaces);
  5355. verifyFormat(
  5356. "std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
  5357. " aaaaaaa, aaaaaaaaaa, aaaaa, aaaaaaaaaaaaaaa, aaa, aaaaaaaaaa, a,\n"
  5358. " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaa,\n"
  5359. " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa, aaaaaaa, a};");
  5360. verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
  5361. }
  5362. TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
  5363. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5364. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5365. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5366. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5367. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5368. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  5369. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5370. " // line comment\n"
  5371. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5372. " 1, 22, 333, 4444, 55555,\n"
  5373. " // line comment\n"
  5374. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5375. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  5376. verifyFormat(
  5377. "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5378. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5379. " 1, 22, 333, 4444, 55555, 666666, // comment\n"
  5380. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  5381. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  5382. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  5383. " 7777777};");
  5384. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  5385. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  5386. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  5387. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  5388. " 1, 1, 1, 1};",
  5389. getLLVMStyleWithColumns(39));
  5390. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  5391. " 1, 1, 1, 1};",
  5392. getLLVMStyleWithColumns(38));
  5393. verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
  5394. " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
  5395. getLLVMStyleWithColumns(43));
  5396. // Trailing commas.
  5397. verifyFormat("vector<int> x = {\n"
  5398. " 1, 1, 1, 1, 1, 1, 1, 1,\n"
  5399. "};",
  5400. getLLVMStyleWithColumns(39));
  5401. verifyFormat("vector<int> x = {\n"
  5402. " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
  5403. "};",
  5404. getLLVMStyleWithColumns(39));
  5405. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  5406. " 1, 1, 1, 1,\n"
  5407. " /**/ /**/};",
  5408. getLLVMStyleWithColumns(39));
  5409. verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
  5410. " {aaaaaaaaaaaaaaaaaaa},\n"
  5411. " {aaaaaaaaaaaaaaaaaaaaa},\n"
  5412. " {aaaaaaaaaaaaaaaaa}};",
  5413. getLLVMStyleWithColumns(60));
  5414. // With nested lists, we should either format one item per line or all nested
  5415. // lists one one line.
  5416. // FIXME: For some nested lists, we can do better.
  5417. verifyFormat(
  5418. "SomeStruct my_struct_array = {\n"
  5419. " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
  5420. " aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
  5421. " {aaa, aaa},\n"
  5422. " {aaa, aaa},\n"
  5423. " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
  5424. " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5425. " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
  5426. // No column layout should be used here.
  5427. verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
  5428. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
  5429. }
  5430. TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
  5431. FormatStyle DoNotMerge = getLLVMStyle();
  5432. DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  5433. verifyFormat("void f() { return 42; }");
  5434. verifyFormat("void f() {\n"
  5435. " return 42;\n"
  5436. "}",
  5437. DoNotMerge);
  5438. verifyFormat("void f() {\n"
  5439. " // Comment\n"
  5440. "}");
  5441. verifyFormat("{\n"
  5442. "#error {\n"
  5443. " int a;\n"
  5444. "}");
  5445. verifyFormat("{\n"
  5446. " int a;\n"
  5447. "#error {\n"
  5448. "}");
  5449. verifyFormat("void f() {} // comment");
  5450. verifyFormat("void f() { int a; } // comment");
  5451. verifyFormat("void f() {\n"
  5452. "} // comment",
  5453. DoNotMerge);
  5454. verifyFormat("void f() {\n"
  5455. " int a;\n"
  5456. "} // comment",
  5457. DoNotMerge);
  5458. verifyFormat("void f() {\n"
  5459. "} // comment",
  5460. getLLVMStyleWithColumns(15));
  5461. verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
  5462. verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
  5463. verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
  5464. verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
  5465. verifyFormat("class C {\n"
  5466. " C()\n"
  5467. " : iiiiiiii(nullptr),\n"
  5468. " kkkkkkk(nullptr),\n"
  5469. " mmmmmmm(nullptr),\n"
  5470. " nnnnnnn(nullptr) {}\n"
  5471. "};",
  5472. getGoogleStyle());
  5473. FormatStyle NoColumnLimit = getLLVMStyle();
  5474. NoColumnLimit.ColumnLimit = 0;
  5475. EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
  5476. EXPECT_EQ("class C {\n"
  5477. " A() : b(0) {}\n"
  5478. "};", format("class C{A():b(0){}};", NoColumnLimit));
  5479. EXPECT_EQ("A()\n"
  5480. " : b(0) {\n"
  5481. "}",
  5482. format("A()\n:b(0)\n{\n}", NoColumnLimit));
  5483. FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
  5484. DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
  5485. FormatStyle::SFS_None;
  5486. EXPECT_EQ("A()\n"
  5487. " : b(0) {\n"
  5488. "}",
  5489. format("A():b(0){}", DoNotMergeNoColumnLimit));
  5490. EXPECT_EQ("A()\n"
  5491. " : b(0) {\n"
  5492. "}",
  5493. format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
  5494. verifyFormat("#define A \\\n"
  5495. " void f() { \\\n"
  5496. " int i; \\\n"
  5497. " }",
  5498. getLLVMStyleWithColumns(20));
  5499. verifyFormat("#define A \\\n"
  5500. " void f() { int i; }",
  5501. getLLVMStyleWithColumns(21));
  5502. verifyFormat("#define A \\\n"
  5503. " void f() { \\\n"
  5504. " int i; \\\n"
  5505. " } \\\n"
  5506. " int j;",
  5507. getLLVMStyleWithColumns(22));
  5508. verifyFormat("#define A \\\n"
  5509. " void f() { int i; } \\\n"
  5510. " int j;",
  5511. getLLVMStyleWithColumns(23));
  5512. }
  5513. TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
  5514. FormatStyle MergeInlineOnly = getLLVMStyle();
  5515. MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  5516. verifyFormat("class C {\n"
  5517. " int f() { return 42; }\n"
  5518. "};",
  5519. MergeInlineOnly);
  5520. verifyFormat("int f() {\n"
  5521. " return 42;\n"
  5522. "}",
  5523. MergeInlineOnly);
  5524. }
  5525. TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
  5526. // Elaborate type variable declarations.
  5527. verifyFormat("struct foo a = {bar};\nint n;");
  5528. verifyFormat("class foo a = {bar};\nint n;");
  5529. verifyFormat("union foo a = {bar};\nint n;");
  5530. // Elaborate types inside function definitions.
  5531. verifyFormat("struct foo f() {}\nint n;");
  5532. verifyFormat("class foo f() {}\nint n;");
  5533. verifyFormat("union foo f() {}\nint n;");
  5534. // Templates.
  5535. verifyFormat("template <class X> void f() {}\nint n;");
  5536. verifyFormat("template <struct X> void f() {}\nint n;");
  5537. verifyFormat("template <union X> void f() {}\nint n;");
  5538. // Actual definitions...
  5539. verifyFormat("struct {\n} n;");
  5540. verifyFormat(
  5541. "template <template <class T, class Y>, class Z> class X {\n} n;");
  5542. verifyFormat("union Z {\n int n;\n} x;");
  5543. verifyFormat("class MACRO Z {\n} n;");
  5544. verifyFormat("class MACRO(X) Z {\n} n;");
  5545. verifyFormat("class __attribute__(X) Z {\n} n;");
  5546. verifyFormat("class __declspec(X) Z {\n} n;");
  5547. verifyFormat("class A##B##C {\n} n;");
  5548. verifyFormat("class alignas(16) Z {\n} n;");
  5549. // Redefinition from nested context:
  5550. verifyFormat("class A::B::C {\n} n;");
  5551. // Template definitions.
  5552. verifyFormat(
  5553. "template <typename F>\n"
  5554. "Matcher(const Matcher<F> &Other,\n"
  5555. " typename enable_if_c<is_base_of<F, T>::value &&\n"
  5556. " !is_same<F, T>::value>::type * = 0)\n"
  5557. " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
  5558. // FIXME: This is still incorrectly handled at the formatter side.
  5559. verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
  5560. // FIXME:
  5561. // This now gets parsed incorrectly as class definition.
  5562. // verifyFormat("class A<int> f() {\n}\nint n;");
  5563. // Elaborate types where incorrectly parsing the structural element would
  5564. // break the indent.
  5565. verifyFormat("if (true)\n"
  5566. " class X x;\n"
  5567. "else\n"
  5568. " f();\n");
  5569. // This is simply incomplete. Formatting is not important, but must not crash.
  5570. verifyFormat("class A:");
  5571. }
  5572. TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
  5573. EXPECT_EQ("#error Leave all white!!!!! space* alone!\n",
  5574. format("#error Leave all white!!!!! space* alone!\n"));
  5575. EXPECT_EQ(
  5576. "#warning Leave all white!!!!! space* alone!\n",
  5577. format("#warning Leave all white!!!!! space* alone!\n"));
  5578. EXPECT_EQ("#error 1", format(" # error 1"));
  5579. EXPECT_EQ("#warning 1", format(" # warning 1"));
  5580. }
  5581. TEST_F(FormatTest, FormatHashIfExpressions) {
  5582. verifyFormat("#if AAAA && BBBB");
  5583. // FIXME: Come up with a better indentation for #elif.
  5584. verifyFormat(
  5585. "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
  5586. " defined(BBBBBBBB)\n"
  5587. "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
  5588. " defined(BBBBBBBB)\n"
  5589. "#endif",
  5590. getLLVMStyleWithColumns(65));
  5591. }
  5592. TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
  5593. FormatStyle AllowsMergedIf = getGoogleStyle();
  5594. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  5595. verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
  5596. verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
  5597. verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
  5598. EXPECT_EQ("if (true) return 42;",
  5599. format("if (true)\nreturn 42;", AllowsMergedIf));
  5600. FormatStyle ShortMergedIf = AllowsMergedIf;
  5601. ShortMergedIf.ColumnLimit = 25;
  5602. verifyFormat("#define A \\\n"
  5603. " if (true) return 42;",
  5604. ShortMergedIf);
  5605. verifyFormat("#define A \\\n"
  5606. " f(); \\\n"
  5607. " if (true)\n"
  5608. "#define B",
  5609. ShortMergedIf);
  5610. verifyFormat("#define A \\\n"
  5611. " f(); \\\n"
  5612. " if (true)\n"
  5613. "g();",
  5614. ShortMergedIf);
  5615. verifyFormat("{\n"
  5616. "#ifdef A\n"
  5617. " // Comment\n"
  5618. " if (true) continue;\n"
  5619. "#endif\n"
  5620. " // Comment\n"
  5621. " if (true) continue;\n"
  5622. "}",
  5623. ShortMergedIf);
  5624. ShortMergedIf.ColumnLimit = 29;
  5625. verifyFormat("#define A \\\n"
  5626. " if (aaaaaaaaaa) return 1; \\\n"
  5627. " return 2;",
  5628. ShortMergedIf);
  5629. ShortMergedIf.ColumnLimit = 28;
  5630. verifyFormat("#define A \\\n"
  5631. " if (aaaaaaaaaa) \\\n"
  5632. " return 1; \\\n"
  5633. " return 2;",
  5634. ShortMergedIf);
  5635. }
  5636. TEST_F(FormatTest, BlockCommentsInControlLoops) {
  5637. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  5638. " f();\n"
  5639. "}");
  5640. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  5641. " f();\n"
  5642. "} /* another comment */ else /* comment #3 */ {\n"
  5643. " g();\n"
  5644. "}");
  5645. verifyFormat("while (0) /* a comment in a strange place */ {\n"
  5646. " f();\n"
  5647. "}");
  5648. verifyFormat("for (;;) /* a comment in a strange place */ {\n"
  5649. " f();\n"
  5650. "}");
  5651. verifyFormat("do /* a comment in a strange place */ {\n"
  5652. " f();\n"
  5653. "} /* another comment */ while (0);");
  5654. }
  5655. TEST_F(FormatTest, BlockComments) {
  5656. EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
  5657. format("/* *//* */ /* */\n/* *//* */ /* */"));
  5658. EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
  5659. EXPECT_EQ("#define A /*123*/ \\\n"
  5660. " b\n"
  5661. "/* */\n"
  5662. "someCall(\n"
  5663. " parameter);",
  5664. format("#define A /*123*/ b\n"
  5665. "/* */\n"
  5666. "someCall(parameter);",
  5667. getLLVMStyleWithColumns(15)));
  5668. EXPECT_EQ("#define A\n"
  5669. "/* */ someCall(\n"
  5670. " parameter);",
  5671. format("#define A\n"
  5672. "/* */someCall(parameter);",
  5673. getLLVMStyleWithColumns(15)));
  5674. EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
  5675. EXPECT_EQ("/*\n"
  5676. "*\n"
  5677. " * aaaaaa\n"
  5678. "*aaaaaa\n"
  5679. "*/",
  5680. format("/*\n"
  5681. "*\n"
  5682. " * aaaaaa aaaaaa\n"
  5683. "*/",
  5684. getLLVMStyleWithColumns(10)));
  5685. EXPECT_EQ("/*\n"
  5686. "**\n"
  5687. "* aaaaaa\n"
  5688. "*aaaaaa\n"
  5689. "*/",
  5690. format("/*\n"
  5691. "**\n"
  5692. "* aaaaaa aaaaaa\n"
  5693. "*/",
  5694. getLLVMStyleWithColumns(10)));
  5695. FormatStyle NoBinPacking = getLLVMStyle();
  5696. NoBinPacking.BinPackParameters = false;
  5697. EXPECT_EQ("someFunction(1, /* comment 1 */\n"
  5698. " 2, /* comment 2 */\n"
  5699. " 3, /* comment 3 */\n"
  5700. " aaaa,\n"
  5701. " bbbb);",
  5702. format("someFunction (1, /* comment 1 */\n"
  5703. " 2, /* comment 2 */ \n"
  5704. " 3, /* comment 3 */\n"
  5705. "aaaa, bbbb );",
  5706. NoBinPacking));
  5707. verifyFormat(
  5708. "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  5709. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  5710. EXPECT_EQ(
  5711. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  5712. " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  5713. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
  5714. format(
  5715. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  5716. " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  5717. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
  5718. EXPECT_EQ(
  5719. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  5720. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  5721. "int cccccccccccccccccccccccccccccc; /* comment */\n",
  5722. format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  5723. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  5724. "int cccccccccccccccccccccccccccccc; /* comment */\n"));
  5725. verifyFormat("void f(int * /* unused */) {}");
  5726. EXPECT_EQ("/*\n"
  5727. " **\n"
  5728. " */",
  5729. format("/*\n"
  5730. " **\n"
  5731. " */"));
  5732. EXPECT_EQ("/*\n"
  5733. " *q\n"
  5734. " */",
  5735. format("/*\n"
  5736. " *q\n"
  5737. " */"));
  5738. EXPECT_EQ("/*\n"
  5739. " * q\n"
  5740. " */",
  5741. format("/*\n"
  5742. " * q\n"
  5743. " */"));
  5744. EXPECT_EQ("/*\n"
  5745. " **/",
  5746. format("/*\n"
  5747. " **/"));
  5748. EXPECT_EQ("/*\n"
  5749. " ***/",
  5750. format("/*\n"
  5751. " ***/"));
  5752. }
  5753. TEST_F(FormatTest, BlockCommentsInMacros) {
  5754. EXPECT_EQ("#define A \\\n"
  5755. " { \\\n"
  5756. " /* one line */ \\\n"
  5757. " someCall();",
  5758. format("#define A { \\\n"
  5759. " /* one line */ \\\n"
  5760. " someCall();",
  5761. getLLVMStyleWithColumns(20)));
  5762. EXPECT_EQ("#define A \\\n"
  5763. " { \\\n"
  5764. " /* previous */ \\\n"
  5765. " /* one line */ \\\n"
  5766. " someCall();",
  5767. format("#define A { \\\n"
  5768. " /* previous */ \\\n"
  5769. " /* one line */ \\\n"
  5770. " someCall();",
  5771. getLLVMStyleWithColumns(20)));
  5772. }
  5773. TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
  5774. EXPECT_EQ("a = {\n"
  5775. " 1111 /* */\n"
  5776. "};",
  5777. format("a = {1111 /* */\n"
  5778. "};",
  5779. getLLVMStyleWithColumns(15)));
  5780. EXPECT_EQ("a = {\n"
  5781. " 1111 /* */\n"
  5782. "};",
  5783. format("a = {1111 /* */\n"
  5784. "};",
  5785. getLLVMStyleWithColumns(15)));
  5786. // FIXME: The formatting is still wrong here.
  5787. EXPECT_EQ("a = {\n"
  5788. " 1111 /* a\n"
  5789. " */\n"
  5790. "};",
  5791. format("a = {1111 /* a */\n"
  5792. "};",
  5793. getLLVMStyleWithColumns(15)));
  5794. }
  5795. TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
  5796. // FIXME: This is not what we want...
  5797. verifyFormat("{\n"
  5798. "// a"
  5799. "// b");
  5800. }
  5801. TEST_F(FormatTest, FormatStarDependingOnContext) {
  5802. verifyFormat("void f(int *a);");
  5803. verifyFormat("void f() { f(fint * b); }");
  5804. verifyFormat("class A {\n void f(int *a);\n};");
  5805. verifyFormat("class A {\n int *a;\n};");
  5806. verifyFormat("namespace a {\n"
  5807. "namespace b {\n"
  5808. "class A {\n"
  5809. " void f() {}\n"
  5810. " int *a;\n"
  5811. "};\n"
  5812. "}\n"
  5813. "}");
  5814. }
  5815. TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
  5816. verifyFormat("while");
  5817. verifyFormat("operator");
  5818. }
  5819. //===----------------------------------------------------------------------===//
  5820. // Objective-C tests.
  5821. //===----------------------------------------------------------------------===//
  5822. TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
  5823. verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
  5824. EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
  5825. format("-(NSUInteger)indexOfObject:(id)anObject;"));
  5826. EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
  5827. EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
  5828. EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
  5829. format("-(NSInteger)Method3:(id)anObject;"));
  5830. EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
  5831. format("-(NSInteger)Method4:(id)anObject;"));
  5832. EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
  5833. format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
  5834. EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
  5835. format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
  5836. EXPECT_EQ(
  5837. "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
  5838. format(
  5839. "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
  5840. // Very long objectiveC method declaration.
  5841. verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
  5842. " inRange:(NSRange)range\n"
  5843. " outRange:(NSRange)out_range\n"
  5844. " outRange1:(NSRange)out_range1\n"
  5845. " outRange2:(NSRange)out_range2\n"
  5846. " outRange3:(NSRange)out_range3\n"
  5847. " outRange4:(NSRange)out_range4\n"
  5848. " outRange5:(NSRange)out_range5\n"
  5849. " outRange6:(NSRange)out_range6\n"
  5850. " outRange7:(NSRange)out_range7\n"
  5851. " outRange8:(NSRange)out_range8\n"
  5852. " outRange9:(NSRange)out_range9;");
  5853. verifyFormat("- (int)sum:(vector<int>)numbers;");
  5854. verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
  5855. // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
  5856. // protocol lists (but not for template classes):
  5857. //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
  5858. verifyFormat("- (int (*)())foo:(int (*)())f;");
  5859. verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
  5860. // If there's no return type (very rare in practice!), LLVM and Google style
  5861. // agree.
  5862. verifyFormat("- foo;");
  5863. verifyFormat("- foo:(int)f;");
  5864. verifyGoogleFormat("- foo:(int)foo;");
  5865. }
  5866. TEST_F(FormatTest, FormatObjCInterface) {
  5867. verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
  5868. "@public\n"
  5869. " int field1;\n"
  5870. "@protected\n"
  5871. " int field2;\n"
  5872. "@private\n"
  5873. " int field3;\n"
  5874. "@package\n"
  5875. " int field4;\n"
  5876. "}\n"
  5877. "+ (id)init;\n"
  5878. "@end");
  5879. verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
  5880. " @public\n"
  5881. " int field1;\n"
  5882. " @protected\n"
  5883. " int field2;\n"
  5884. " @private\n"
  5885. " int field3;\n"
  5886. " @package\n"
  5887. " int field4;\n"
  5888. "}\n"
  5889. "+ (id)init;\n"
  5890. "@end");
  5891. verifyFormat("@interface /* wait for it */ Foo\n"
  5892. "+ (id)init;\n"
  5893. "// Look, a comment!\n"
  5894. "- (int)answerWith:(int)i;\n"
  5895. "@end");
  5896. verifyFormat("@interface Foo\n"
  5897. "@end\n"
  5898. "@interface Bar\n"
  5899. "@end");
  5900. verifyFormat("@interface Foo : Bar\n"
  5901. "+ (id)init;\n"
  5902. "@end");
  5903. verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
  5904. "+ (id)init;\n"
  5905. "@end");
  5906. verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
  5907. "+ (id)init;\n"
  5908. "@end");
  5909. verifyFormat("@interface Foo (HackStuff)\n"
  5910. "+ (id)init;\n"
  5911. "@end");
  5912. verifyFormat("@interface Foo ()\n"
  5913. "+ (id)init;\n"
  5914. "@end");
  5915. verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
  5916. "+ (id)init;\n"
  5917. "@end");
  5918. verifyGoogleFormat("@interface Foo (HackStuff) <MyProtocol>\n"
  5919. "+ (id)init;\n"
  5920. "@end");
  5921. verifyFormat("@interface Foo {\n"
  5922. " int _i;\n"
  5923. "}\n"
  5924. "+ (id)init;\n"
  5925. "@end");
  5926. verifyFormat("@interface Foo : Bar {\n"
  5927. " int _i;\n"
  5928. "}\n"
  5929. "+ (id)init;\n"
  5930. "@end");
  5931. verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
  5932. " int _i;\n"
  5933. "}\n"
  5934. "+ (id)init;\n"
  5935. "@end");
  5936. verifyFormat("@interface Foo (HackStuff) {\n"
  5937. " int _i;\n"
  5938. "}\n"
  5939. "+ (id)init;\n"
  5940. "@end");
  5941. verifyFormat("@interface Foo () {\n"
  5942. " int _i;\n"
  5943. "}\n"
  5944. "+ (id)init;\n"
  5945. "@end");
  5946. verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
  5947. " int _i;\n"
  5948. "}\n"
  5949. "+ (id)init;\n"
  5950. "@end");
  5951. FormatStyle OnePerLine = getGoogleStyle();
  5952. OnePerLine.BinPackParameters = false;
  5953. verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa () <\n"
  5954. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5955. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5956. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5957. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
  5958. "}",
  5959. OnePerLine);
  5960. }
  5961. TEST_F(FormatTest, FormatObjCImplementation) {
  5962. verifyFormat("@implementation Foo : NSObject {\n"
  5963. "@public\n"
  5964. " int field1;\n"
  5965. "@protected\n"
  5966. " int field2;\n"
  5967. "@private\n"
  5968. " int field3;\n"
  5969. "@package\n"
  5970. " int field4;\n"
  5971. "}\n"
  5972. "+ (id)init {\n}\n"
  5973. "@end");
  5974. verifyGoogleFormat("@implementation Foo : NSObject {\n"
  5975. " @public\n"
  5976. " int field1;\n"
  5977. " @protected\n"
  5978. " int field2;\n"
  5979. " @private\n"
  5980. " int field3;\n"
  5981. " @package\n"
  5982. " int field4;\n"
  5983. "}\n"
  5984. "+ (id)init {\n}\n"
  5985. "@end");
  5986. verifyFormat("@implementation Foo\n"
  5987. "+ (id)init {\n"
  5988. " if (true)\n"
  5989. " return nil;\n"
  5990. "}\n"
  5991. "// Look, a comment!\n"
  5992. "- (int)answerWith:(int)i {\n"
  5993. " return i;\n"
  5994. "}\n"
  5995. "+ (int)answerWith:(int)i {\n"
  5996. " return i;\n"
  5997. "}\n"
  5998. "@end");
  5999. verifyFormat("@implementation Foo\n"
  6000. "@end\n"
  6001. "@implementation Bar\n"
  6002. "@end");
  6003. EXPECT_EQ("@implementation Foo : Bar\n"
  6004. "+ (id)init {\n}\n"
  6005. "- (void)foo {\n}\n"
  6006. "@end",
  6007. format("@implementation Foo : Bar\n"
  6008. "+(id)init{}\n"
  6009. "-(void)foo{}\n"
  6010. "@end"));
  6011. verifyFormat("@implementation Foo {\n"
  6012. " int _i;\n"
  6013. "}\n"
  6014. "+ (id)init {\n}\n"
  6015. "@end");
  6016. verifyFormat("@implementation Foo : Bar {\n"
  6017. " int _i;\n"
  6018. "}\n"
  6019. "+ (id)init {\n}\n"
  6020. "@end");
  6021. verifyFormat("@implementation Foo (HackStuff)\n"
  6022. "+ (id)init {\n}\n"
  6023. "@end");
  6024. verifyFormat("@implementation ObjcClass\n"
  6025. "- (void)method;\n"
  6026. "{}\n"
  6027. "@end");
  6028. }
  6029. TEST_F(FormatTest, FormatObjCProtocol) {
  6030. verifyFormat("@protocol Foo\n"
  6031. "@property(weak) id delegate;\n"
  6032. "- (NSUInteger)numberOfThings;\n"
  6033. "@end");
  6034. verifyFormat("@protocol MyProtocol <NSObject>\n"
  6035. "- (NSUInteger)numberOfThings;\n"
  6036. "@end");
  6037. verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
  6038. "- (NSUInteger)numberOfThings;\n"
  6039. "@end");
  6040. verifyFormat("@protocol Foo;\n"
  6041. "@protocol Bar;\n");
  6042. verifyFormat("@protocol Foo\n"
  6043. "@end\n"
  6044. "@protocol Bar\n"
  6045. "@end");
  6046. verifyFormat("@protocol myProtocol\n"
  6047. "- (void)mandatoryWithInt:(int)i;\n"
  6048. "@optional\n"
  6049. "- (void)optional;\n"
  6050. "@required\n"
  6051. "- (void)required;\n"
  6052. "@optional\n"
  6053. "@property(assign) int madProp;\n"
  6054. "@end\n");
  6055. verifyFormat("@property(nonatomic, assign, readonly)\n"
  6056. " int *looooooooooooooooooooooooooooongNumber;\n"
  6057. "@property(nonatomic, assign, readonly)\n"
  6058. " NSString *looooooooooooooooooooooooooooongName;");
  6059. verifyFormat("@implementation PR18406\n"
  6060. "}\n"
  6061. "@end");
  6062. }
  6063. TEST_F(FormatTest, FormatObjCMethodDeclarations) {
  6064. verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
  6065. " rect:(NSRect)theRect\n"
  6066. " interval:(float)theInterval {\n"
  6067. "}");
  6068. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6069. " longKeyword:(NSRect)theRect\n"
  6070. " evenLongerKeyword:(float)theInterval\n"
  6071. " error:(NSError **)theError {\n"
  6072. "}");
  6073. verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
  6074. " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
  6075. " NS_DESIGNATED_INITIALIZER;",
  6076. getLLVMStyleWithColumns(60));
  6077. }
  6078. TEST_F(FormatTest, FormatObjCMethodExpr) {
  6079. verifyFormat("[foo bar:baz];");
  6080. verifyFormat("return [foo bar:baz];");
  6081. verifyFormat("return (a)[foo bar:baz];");
  6082. verifyFormat("f([foo bar:baz]);");
  6083. verifyFormat("f(2, [foo bar:baz]);");
  6084. verifyFormat("f(2, a ? b : c);");
  6085. verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
  6086. // Unary operators.
  6087. verifyFormat("int a = +[foo bar:baz];");
  6088. verifyFormat("int a = -[foo bar:baz];");
  6089. verifyFormat("int a = ![foo bar:baz];");
  6090. verifyFormat("int a = ~[foo bar:baz];");
  6091. verifyFormat("int a = ++[foo bar:baz];");
  6092. verifyFormat("int a = --[foo bar:baz];");
  6093. verifyFormat("int a = sizeof [foo bar:baz];");
  6094. verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
  6095. verifyFormat("int a = &[foo bar:baz];");
  6096. verifyFormat("int a = *[foo bar:baz];");
  6097. // FIXME: Make casts work, without breaking f()[4].
  6098. //verifyFormat("int a = (int)[foo bar:baz];");
  6099. //verifyFormat("return (int)[foo bar:baz];");
  6100. //verifyFormat("(void)[foo bar:baz];");
  6101. verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
  6102. // Binary operators.
  6103. verifyFormat("[foo bar:baz], [foo bar:baz];");
  6104. verifyFormat("[foo bar:baz] = [foo bar:baz];");
  6105. verifyFormat("[foo bar:baz] *= [foo bar:baz];");
  6106. verifyFormat("[foo bar:baz] /= [foo bar:baz];");
  6107. verifyFormat("[foo bar:baz] %= [foo bar:baz];");
  6108. verifyFormat("[foo bar:baz] += [foo bar:baz];");
  6109. verifyFormat("[foo bar:baz] -= [foo bar:baz];");
  6110. verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
  6111. verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
  6112. verifyFormat("[foo bar:baz] &= [foo bar:baz];");
  6113. verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
  6114. verifyFormat("[foo bar:baz] |= [foo bar:baz];");
  6115. verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
  6116. verifyFormat("[foo bar:baz] || [foo bar:baz];");
  6117. verifyFormat("[foo bar:baz] && [foo bar:baz];");
  6118. verifyFormat("[foo bar:baz] | [foo bar:baz];");
  6119. verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
  6120. verifyFormat("[foo bar:baz] & [foo bar:baz];");
  6121. verifyFormat("[foo bar:baz] == [foo bar:baz];");
  6122. verifyFormat("[foo bar:baz] != [foo bar:baz];");
  6123. verifyFormat("[foo bar:baz] >= [foo bar:baz];");
  6124. verifyFormat("[foo bar:baz] <= [foo bar:baz];");
  6125. verifyFormat("[foo bar:baz] > [foo bar:baz];");
  6126. verifyFormat("[foo bar:baz] < [foo bar:baz];");
  6127. verifyFormat("[foo bar:baz] >> [foo bar:baz];");
  6128. verifyFormat("[foo bar:baz] << [foo bar:baz];");
  6129. verifyFormat("[foo bar:baz] - [foo bar:baz];");
  6130. verifyFormat("[foo bar:baz] + [foo bar:baz];");
  6131. verifyFormat("[foo bar:baz] * [foo bar:baz];");
  6132. verifyFormat("[foo bar:baz] / [foo bar:baz];");
  6133. verifyFormat("[foo bar:baz] % [foo bar:baz];");
  6134. // Whew!
  6135. verifyFormat("return in[42];");
  6136. verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
  6137. "}");
  6138. verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
  6139. verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
  6140. verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
  6141. verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
  6142. verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
  6143. verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
  6144. verifyFormat("[button setAction:@selector(zoomOut:)];");
  6145. verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
  6146. verifyFormat("arr[[self indexForFoo:a]];");
  6147. verifyFormat("throw [self errorFor:a];");
  6148. verifyFormat("@throw [self errorFor:a];");
  6149. verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
  6150. verifyFormat("[(id)foo bar:(id) ? baz : quux];");
  6151. verifyFormat("4 > 4 ? (id)a : (id)baz;");
  6152. // This tests that the formatter doesn't break after "backing" but before ":",
  6153. // which would be at 80 columns.
  6154. verifyFormat(
  6155. "void f() {\n"
  6156. " if ((self = [super initWithContentRect:contentRect\n"
  6157. " styleMask:styleMask ?: otherMask\n"
  6158. " backing:NSBackingStoreBuffered\n"
  6159. " defer:YES]))");
  6160. verifyFormat(
  6161. "[foo checkThatBreakingAfterColonWorksOk:\n"
  6162. " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
  6163. verifyFormat("[myObj short:arg1 // Force line break\n"
  6164. " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
  6165. " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
  6166. " error:arg4];");
  6167. verifyFormat(
  6168. "void f() {\n"
  6169. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  6170. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  6171. " pos.width(), pos.height())\n"
  6172. " styleMask:NSBorderlessWindowMask\n"
  6173. " backing:NSBackingStoreBuffered\n"
  6174. " defer:NO]);\n"
  6175. "}");
  6176. verifyFormat(
  6177. "void f() {\n"
  6178. " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
  6179. " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
  6180. " pos.width(), pos.height())\n"
  6181. " syeMask:NSBorderlessWindowMask\n"
  6182. " bking:NSBackingStoreBuffered\n"
  6183. " der:NO]);\n"
  6184. "}",
  6185. getLLVMStyleWithColumns(70));
  6186. verifyFormat(
  6187. "void f() {\n"
  6188. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  6189. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  6190. " pos.width(), pos.height())\n"
  6191. " styleMask:NSBorderlessWindowMask\n"
  6192. " backing:NSBackingStoreBuffered\n"
  6193. " defer:NO]);\n"
  6194. "}",
  6195. getChromiumStyle(FormatStyle::LK_Cpp));
  6196. verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
  6197. " with:contentsNativeView];");
  6198. verifyFormat(
  6199. "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
  6200. " owner:nillllll];");
  6201. verifyFormat(
  6202. "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
  6203. " forType:kBookmarkButtonDragType];");
  6204. verifyFormat("[defaultCenter addObserver:self\n"
  6205. " selector:@selector(willEnterFullscreen)\n"
  6206. " name:kWillEnterFullscreenNotification\n"
  6207. " object:nil];");
  6208. verifyFormat("[image_rep drawInRect:drawRect\n"
  6209. " fromRect:NSZeroRect\n"
  6210. " operation:NSCompositeCopy\n"
  6211. " fraction:1.0\n"
  6212. " respectFlipped:NO\n"
  6213. " hints:nil];");
  6214. verifyFormat(
  6215. "scoped_nsobject<NSTextField> message(\n"
  6216. " // The frame will be fixed up when |-setMessageText:| is called.\n"
  6217. " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
  6218. verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
  6219. " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
  6220. " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
  6221. " aaaa:bbb];");
  6222. verifyFormat("[self param:function( //\n"
  6223. " parameter)]");
  6224. verifyFormat(
  6225. "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  6226. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  6227. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
  6228. // Variadic parameters.
  6229. verifyFormat(
  6230. "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
  6231. verifyFormat(
  6232. "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  6233. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  6234. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
  6235. verifyFormat("[self // break\n"
  6236. " a:a\n"
  6237. " aaa:aaa];");
  6238. verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
  6239. " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
  6240. }
  6241. TEST_F(FormatTest, ObjCAt) {
  6242. verifyFormat("@autoreleasepool");
  6243. verifyFormat("@catch");
  6244. verifyFormat("@class");
  6245. verifyFormat("@compatibility_alias");
  6246. verifyFormat("@defs");
  6247. verifyFormat("@dynamic");
  6248. verifyFormat("@encode");
  6249. verifyFormat("@end");
  6250. verifyFormat("@finally");
  6251. verifyFormat("@implementation");
  6252. verifyFormat("@import");
  6253. verifyFormat("@interface");
  6254. verifyFormat("@optional");
  6255. verifyFormat("@package");
  6256. verifyFormat("@private");
  6257. verifyFormat("@property");
  6258. verifyFormat("@protected");
  6259. verifyFormat("@protocol");
  6260. verifyFormat("@public");
  6261. verifyFormat("@required");
  6262. verifyFormat("@selector");
  6263. verifyFormat("@synchronized");
  6264. verifyFormat("@synthesize");
  6265. verifyFormat("@throw");
  6266. verifyFormat("@try");
  6267. EXPECT_EQ("@interface", format("@ interface"));
  6268. // The precise formatting of this doesn't matter, nobody writes code like
  6269. // this.
  6270. verifyFormat("@ /*foo*/ interface");
  6271. }
  6272. TEST_F(FormatTest, ObjCSnippets) {
  6273. verifyFormat("@autoreleasepool {\n"
  6274. " foo();\n"
  6275. "}");
  6276. verifyFormat("@class Foo, Bar;");
  6277. verifyFormat("@compatibility_alias AliasName ExistingClass;");
  6278. verifyFormat("@dynamic textColor;");
  6279. verifyFormat("char *buf1 = @encode(int *);");
  6280. verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
  6281. verifyFormat("char *buf1 = @encode(int **);");
  6282. verifyFormat("Protocol *proto = @protocol(p1);");
  6283. verifyFormat("SEL s = @selector(foo:);");
  6284. verifyFormat("@synchronized(self) {\n"
  6285. " f();\n"
  6286. "}");
  6287. verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  6288. verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  6289. verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
  6290. verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
  6291. verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
  6292. verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
  6293. getMozillaStyle());
  6294. verifyFormat("@property BOOL editable;", getMozillaStyle());
  6295. verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
  6296. getWebKitStyle());
  6297. verifyFormat("@property BOOL editable;", getWebKitStyle());
  6298. verifyFormat("@import foo.bar;\n"
  6299. "@import baz;");
  6300. }
  6301. TEST_F(FormatTest, ObjCLiterals) {
  6302. verifyFormat("@\"String\"");
  6303. verifyFormat("@1");
  6304. verifyFormat("@+4.8");
  6305. verifyFormat("@-4");
  6306. verifyFormat("@1LL");
  6307. verifyFormat("@.5");
  6308. verifyFormat("@'c'");
  6309. verifyFormat("@true");
  6310. verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
  6311. verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
  6312. verifyFormat("NSNumber *favoriteColor = @(Green);");
  6313. verifyFormat("NSString *path = @(getenv(\"PATH\"));");
  6314. verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
  6315. }
  6316. TEST_F(FormatTest, ObjCDictLiterals) {
  6317. verifyFormat("@{");
  6318. verifyFormat("@{}");
  6319. verifyFormat("@{@\"one\" : @1}");
  6320. verifyFormat("return @{@\"one\" : @1;");
  6321. verifyFormat("@{@\"one\" : @1}");
  6322. verifyFormat("@{@\"one\" : @{@2 : @1}}");
  6323. verifyFormat("@{\n"
  6324. " @\"one\" : @{@2 : @1},\n"
  6325. "}");
  6326. verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
  6327. verifyFormat("[self setDict:@{}");
  6328. verifyFormat("[self setDict:@{@1 : @2}");
  6329. verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
  6330. verifyFormat(
  6331. "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
  6332. verifyFormat(
  6333. "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
  6334. verifyFormat(
  6335. "NSDictionary *d = @{\n"
  6336. " @\"nam\" : NSUserNam(),\n"
  6337. " @\"dte\" : [NSDate date],\n"
  6338. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  6339. "};");
  6340. verifyFormat(
  6341. "@{\n"
  6342. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  6343. "regularFont,\n"
  6344. "};");
  6345. verifyGoogleFormat(
  6346. "@{\n"
  6347. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  6348. "regularFont,\n"
  6349. "};");
  6350. verifyFormat(
  6351. "@{\n"
  6352. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
  6353. " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
  6354. "};");
  6355. // We should try to be robust in case someone forgets the "@".
  6356. verifyFormat(
  6357. "NSDictionary *d = {\n"
  6358. " @\"nam\" : NSUserNam(),\n"
  6359. " @\"dte\" : [NSDate date],\n"
  6360. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  6361. "};");
  6362. verifyFormat("NSMutableDictionary *dictionary =\n"
  6363. " [NSMutableDictionary dictionaryWithDictionary:@{\n"
  6364. " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
  6365. " bbbbbbbbbbbbbbbbbb : bbbbb,\n"
  6366. " cccccccccccccccc : ccccccccccccccc\n"
  6367. " }];");
  6368. }
  6369. TEST_F(FormatTest, ObjCArrayLiterals) {
  6370. verifyFormat("@[");
  6371. verifyFormat("@[]");
  6372. verifyFormat(
  6373. "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
  6374. verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
  6375. verifyFormat("NSArray *array = @[ [foo description] ];");
  6376. verifyFormat(
  6377. "NSArray *some_variable = @[\n"
  6378. " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
  6379. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6380. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6381. " @\"aaaaaaaaaaaaaaaaa\"\n"
  6382. "];");
  6383. verifyFormat("NSArray *some_variable = @[\n"
  6384. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6385. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6386. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6387. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6388. "];");
  6389. verifyGoogleFormat("NSArray *some_variable = @[\n"
  6390. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6391. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6392. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6393. " @\"aaaaaaaaaaaaaaaaa\"\n"
  6394. "];");
  6395. // We should try to be robust in case someone forgets the "@".
  6396. verifyFormat("NSArray *some_variable = [\n"
  6397. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6398. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6399. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6400. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6401. "];");
  6402. verifyFormat(
  6403. "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
  6404. " index:(NSUInteger)index\n"
  6405. " nonDigitAttributes:\n"
  6406. " (NSDictionary *)noDigitAttributes;");
  6407. verifyFormat(
  6408. "[someFunction someLooooooooooooongParameter:\n"
  6409. " @[ NSBundle.mainBundle.infoDictionary[@\"a\"] ]];");
  6410. }
  6411. TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
  6412. EXPECT_EQ("{\n"
  6413. "{\n"
  6414. "a;\n"
  6415. "b;\n"
  6416. "}\n"
  6417. "}",
  6418. format("{\n"
  6419. "{\n"
  6420. "a;\n"
  6421. " b;\n"
  6422. "}\n"
  6423. "}",
  6424. 13, 2, getLLVMStyle()));
  6425. EXPECT_EQ("{\n"
  6426. "{\n"
  6427. " a;\n"
  6428. "b;\n"
  6429. "}\n"
  6430. "}",
  6431. format("{\n"
  6432. "{\n"
  6433. " a;\n"
  6434. "b;\n"
  6435. "}\n"
  6436. "}",
  6437. 9, 2, getLLVMStyle()));
  6438. EXPECT_EQ("{\n"
  6439. "{\n"
  6440. "public:\n"
  6441. " b;\n"
  6442. "}\n"
  6443. "}",
  6444. format("{\n"
  6445. "{\n"
  6446. "public:\n"
  6447. " b;\n"
  6448. "}\n"
  6449. "}",
  6450. 17, 2, getLLVMStyle()));
  6451. EXPECT_EQ("{\n"
  6452. "{\n"
  6453. "a;\n"
  6454. "}\n"
  6455. "{\n"
  6456. " b; //\n"
  6457. "}\n"
  6458. "}",
  6459. format("{\n"
  6460. "{\n"
  6461. "a;\n"
  6462. "}\n"
  6463. "{\n"
  6464. " b; //\n"
  6465. "}\n"
  6466. "}",
  6467. 22, 2, getLLVMStyle()));
  6468. EXPECT_EQ(" {\n"
  6469. " a; //\n"
  6470. " }",
  6471. format(" {\n"
  6472. "a; //\n"
  6473. " }",
  6474. 4, 2, getLLVMStyle()));
  6475. EXPECT_EQ("void f() {}\n"
  6476. "void g() {}",
  6477. format("void f() {}\n"
  6478. "void g() {}",
  6479. 13, 0, getLLVMStyle()));
  6480. EXPECT_EQ("int a; // comment\n"
  6481. " // line 2\n"
  6482. "int b;",
  6483. format("int a; // comment\n"
  6484. " // line 2\n"
  6485. " int b;",
  6486. 35, 0, getLLVMStyle()));
  6487. EXPECT_EQ(" int a;\n"
  6488. " void\n"
  6489. " ffffff() {\n"
  6490. " }",
  6491. format(" int a;\n"
  6492. "void ffffff() {}",
  6493. 11, 0, getLLVMStyleWithColumns(11)));
  6494. EXPECT_EQ(" void f() {\n"
  6495. "#define A 1\n"
  6496. " }",
  6497. format(" void f() {\n"
  6498. " #define A 1\n" // Format this line.
  6499. " }",
  6500. 20, 0, getLLVMStyle()));
  6501. EXPECT_EQ(" void f() {\n"
  6502. " int i;\n"
  6503. "#define A \\\n"
  6504. " int i; \\\n"
  6505. " int j;\n"
  6506. " int k;\n"
  6507. " }",
  6508. format(" void f() {\n"
  6509. " int i;\n"
  6510. "#define A \\\n"
  6511. " int i; \\\n"
  6512. " int j;\n"
  6513. " int k;\n" // Format this line.
  6514. " }",
  6515. 67, 0, getLLVMStyle()));
  6516. }
  6517. TEST_F(FormatTest, BreaksStringLiterals) {
  6518. EXPECT_EQ("\"some text \"\n"
  6519. "\"other\";",
  6520. format("\"some text other\";", getLLVMStyleWithColumns(12)));
  6521. EXPECT_EQ("\"some text \"\n"
  6522. "\"other\";",
  6523. format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
  6524. EXPECT_EQ(
  6525. "#define A \\\n"
  6526. " \"some \" \\\n"
  6527. " \"text \" \\\n"
  6528. " \"other\";",
  6529. format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
  6530. EXPECT_EQ(
  6531. "#define A \\\n"
  6532. " \"so \" \\\n"
  6533. " \"text \" \\\n"
  6534. " \"other\";",
  6535. format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
  6536. EXPECT_EQ("\"some text\"",
  6537. format("\"some text\"", getLLVMStyleWithColumns(1)));
  6538. EXPECT_EQ("\"some text\"",
  6539. format("\"some text\"", getLLVMStyleWithColumns(11)));
  6540. EXPECT_EQ("\"some \"\n"
  6541. "\"text\"",
  6542. format("\"some text\"", getLLVMStyleWithColumns(10)));
  6543. EXPECT_EQ("\"some \"\n"
  6544. "\"text\"",
  6545. format("\"some text\"", getLLVMStyleWithColumns(7)));
  6546. EXPECT_EQ("\"some\"\n"
  6547. "\" tex\"\n"
  6548. "\"t\"",
  6549. format("\"some text\"", getLLVMStyleWithColumns(6)));
  6550. EXPECT_EQ("\"some\"\n"
  6551. "\" tex\"\n"
  6552. "\" and\"",
  6553. format("\"some tex and\"", getLLVMStyleWithColumns(6)));
  6554. EXPECT_EQ("\"some\"\n"
  6555. "\"/tex\"\n"
  6556. "\"/and\"",
  6557. format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
  6558. EXPECT_EQ("variable =\n"
  6559. " \"long string \"\n"
  6560. " \"literal\";",
  6561. format("variable = \"long string literal\";",
  6562. getLLVMStyleWithColumns(20)));
  6563. EXPECT_EQ("variable = f(\n"
  6564. " \"long string \"\n"
  6565. " \"literal\",\n"
  6566. " short,\n"
  6567. " loooooooooooooooooooong);",
  6568. format("variable = f(\"long string literal\", short, "
  6569. "loooooooooooooooooooong);",
  6570. getLLVMStyleWithColumns(20)));
  6571. EXPECT_EQ("f(g(\"long string \"\n"
  6572. " \"literal\"),\n"
  6573. " b);",
  6574. format("f(g(\"long string literal\"), b);",
  6575. getLLVMStyleWithColumns(20)));
  6576. EXPECT_EQ("f(g(\"long string \"\n"
  6577. " \"literal\",\n"
  6578. " a),\n"
  6579. " b);",
  6580. format("f(g(\"long string literal\", a), b);",
  6581. getLLVMStyleWithColumns(20)));
  6582. EXPECT_EQ(
  6583. "f(\"one two\".split(\n"
  6584. " variable));",
  6585. format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
  6586. EXPECT_EQ("f(\"one two three four five six \"\n"
  6587. " \"seven\".split(\n"
  6588. " really_looooong_variable));",
  6589. format("f(\"one two three four five six seven\"."
  6590. "split(really_looooong_variable));",
  6591. getLLVMStyleWithColumns(33)));
  6592. EXPECT_EQ("f(\"some \"\n"
  6593. " \"text\",\n"
  6594. " other);",
  6595. format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
  6596. // Only break as a last resort.
  6597. verifyFormat(
  6598. "aaaaaaaaaaaaaaaaaaaa(\n"
  6599. " aaaaaaaaaaaaaaaaaaaa,\n"
  6600. " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
  6601. EXPECT_EQ(
  6602. "\"splitmea\"\n"
  6603. "\"trandomp\"\n"
  6604. "\"oint\"",
  6605. format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
  6606. EXPECT_EQ(
  6607. "\"split/\"\n"
  6608. "\"pathat/\"\n"
  6609. "\"slashes\"",
  6610. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  6611. EXPECT_EQ(
  6612. "\"split/\"\n"
  6613. "\"pathat/\"\n"
  6614. "\"slashes\"",
  6615. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  6616. EXPECT_EQ("\"split at \"\n"
  6617. "\"spaces/at/\"\n"
  6618. "\"slashes.at.any$\"\n"
  6619. "\"non-alphanumeric%\"\n"
  6620. "\"1111111111characte\"\n"
  6621. "\"rs\"",
  6622. format("\"split at "
  6623. "spaces/at/"
  6624. "slashes.at."
  6625. "any$non-"
  6626. "alphanumeric%"
  6627. "1111111111characte"
  6628. "rs\"",
  6629. getLLVMStyleWithColumns(20)));
  6630. // Verify that splitting the strings understands
  6631. // Style::AlwaysBreakBeforeMultilineStrings.
  6632. EXPECT_EQ("aaaaaaaaaaaa(aaaaaaaaaaaaa,\n"
  6633. " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
  6634. " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
  6635. format("aaaaaaaaaaaa(aaaaaaaaaaaaa, \"aaaaaaaaaaaaaaaaaaaaaa "
  6636. "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa "
  6637. "aaaaaaaaaaaaaaaaaaaaaa\");",
  6638. getGoogleStyle()));
  6639. EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  6640. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
  6641. format("return \"aaaaaaaaaaaaaaaaaaaaaa "
  6642. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
  6643. "aaaaaaaaaaaaaaaaaaaaaa\";",
  6644. getGoogleStyle()));
  6645. EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  6646. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  6647. format("llvm::outs() << "
  6648. "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
  6649. "aaaaaaaaaaaaaaaaaaa\";"));
  6650. EXPECT_EQ("ffff(\n"
  6651. " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  6652. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  6653. format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  6654. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  6655. getGoogleStyle()));
  6656. FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
  6657. AlignLeft.AlignEscapedNewlinesLeft = true;
  6658. EXPECT_EQ(
  6659. "#define A \\\n"
  6660. " \"some \" \\\n"
  6661. " \"text \" \\\n"
  6662. " \"other\";",
  6663. format("#define A \"some text other\";", AlignLeft));
  6664. }
  6665. TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
  6666. EXPECT_EQ(
  6667. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  6668. "(\n"
  6669. " \"x\t\");",
  6670. format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  6671. "aaaaaaa("
  6672. "\"x\t\");"));
  6673. }
  6674. TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
  6675. EXPECT_EQ(
  6676. "u8\"utf8 string \"\n"
  6677. "u8\"literal\";",
  6678. format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
  6679. EXPECT_EQ(
  6680. "u\"utf16 string \"\n"
  6681. "u\"literal\";",
  6682. format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
  6683. EXPECT_EQ(
  6684. "U\"utf32 string \"\n"
  6685. "U\"literal\";",
  6686. format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
  6687. EXPECT_EQ("L\"wide string \"\n"
  6688. "L\"literal\";",
  6689. format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
  6690. EXPECT_EQ("@\"NSString \"\n"
  6691. "@\"literal\";",
  6692. format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
  6693. }
  6694. TEST_F(FormatTest, BreaksRawStringLiterals) {
  6695. EXPECT_EQ("R\"x(raw )x\"\n"
  6696. "R\"x(literal)x\";",
  6697. format("R\"x(raw literal)x\";", getGoogleStyleWithColumns(15)));
  6698. EXPECT_EQ("uR\"x(raw )x\"\n"
  6699. "uR\"x(literal)x\";",
  6700. format("uR\"x(raw literal)x\";", getGoogleStyleWithColumns(16)));
  6701. EXPECT_EQ("u8R\"x(raw )x\"\n"
  6702. "u8R\"x(literal)x\";",
  6703. format("u8R\"x(raw literal)x\";", getGoogleStyleWithColumns(17)));
  6704. EXPECT_EQ("LR\"x(raw )x\"\n"
  6705. "LR\"x(literal)x\";",
  6706. format("LR\"x(raw literal)x\";", getGoogleStyleWithColumns(16)));
  6707. EXPECT_EQ("UR\"x(raw )x\"\n"
  6708. "UR\"x(literal)x\";",
  6709. format("UR\"x(raw literal)x\";", getGoogleStyleWithColumns(16)));
  6710. }
  6711. TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
  6712. FormatStyle Style = getLLVMStyleWithColumns(20);
  6713. EXPECT_EQ(
  6714. "_T(\"aaaaaaaaaaaaaa\")\n"
  6715. "_T(\"aaaaaaaaaaaaaa\")\n"
  6716. "_T(\"aaaaaaaaaaaa\")",
  6717. format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
  6718. EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
  6719. " _T(\"aaaaaa\"),\n"
  6720. " z);",
  6721. format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
  6722. // FIXME: Handle embedded spaces in one iteration.
  6723. // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
  6724. // "_T(\"aaaaaaaaaaaaa\")\n"
  6725. // "_T(\"aaaaaaaaaaaaa\")\n"
  6726. // "_T(\"a\")",
  6727. // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  6728. // getLLVMStyleWithColumns(20)));
  6729. EXPECT_EQ(
  6730. "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  6731. format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
  6732. }
  6733. TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
  6734. EXPECT_EQ(
  6735. "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  6736. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  6737. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  6738. format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  6739. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  6740. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
  6741. }
  6742. TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
  6743. EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
  6744. format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle()));
  6745. EXPECT_EQ("fffffffffff(g(R\"x(\n"
  6746. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6747. ")x\",\n"
  6748. " a),\n"
  6749. " b);",
  6750. format("fffffffffff(g(R\"x(\n"
  6751. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6752. ")x\", a), b);",
  6753. getGoogleStyleWithColumns(20)));
  6754. EXPECT_EQ("fffffffffff(\n"
  6755. " g(R\"x(qqq\n"
  6756. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6757. ")x\",\n"
  6758. " a),\n"
  6759. " b);",
  6760. format("fffffffffff(g(R\"x(qqq\n"
  6761. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6762. ")x\", a), b);",
  6763. getGoogleStyleWithColumns(20)));
  6764. EXPECT_EQ("fffffffffff(R\"x(\n"
  6765. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6766. ")x\");",
  6767. format("fffffffffff(R\"x(\n"
  6768. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6769. ")x\");",
  6770. getGoogleStyleWithColumns(20)));
  6771. EXPECT_EQ("fffffffffff(R\"x(\n"
  6772. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6773. ")x\" + bbbbbb);",
  6774. format("fffffffffff(R\"x(\n"
  6775. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6776. ")x\" + bbbbbb);",
  6777. getGoogleStyleWithColumns(20)));
  6778. EXPECT_EQ("fffffffffff(\n"
  6779. " R\"x(\n"
  6780. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6781. ")x\" +\n"
  6782. " bbbbbb);",
  6783. format("fffffffffff(\n"
  6784. " R\"x(\n"
  6785. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6786. ")x\" + bbbbbb);",
  6787. getGoogleStyleWithColumns(20)));
  6788. }
  6789. TEST_F(FormatTest, SkipsUnknownStringLiterals) {
  6790. verifyFormat("string a = \"unterminated;");
  6791. EXPECT_EQ("function(\"unterminated,\n"
  6792. " OtherParameter);",
  6793. format("function( \"unterminated,\n"
  6794. " OtherParameter);"));
  6795. }
  6796. TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
  6797. FormatStyle Style = getLLVMStyle();
  6798. Style.Standard = FormatStyle::LS_Cpp03;
  6799. EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
  6800. format("#define x(_a) printf(\"foo\"_a);", Style));
  6801. }
  6802. TEST_F(FormatTest, UnderstandsCpp1y) {
  6803. verifyFormat("int bi{1'000'000};");
  6804. }
  6805. TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
  6806. EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
  6807. " \"ddeeefff\");",
  6808. format("someFunction(\"aaabbbcccdddeeefff\");",
  6809. getLLVMStyleWithColumns(25)));
  6810. EXPECT_EQ("someFunction1234567890(\n"
  6811. " \"aaabbbcccdddeeefff\");",
  6812. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  6813. getLLVMStyleWithColumns(26)));
  6814. EXPECT_EQ("someFunction1234567890(\n"
  6815. " \"aaabbbcccdddeeeff\"\n"
  6816. " \"f\");",
  6817. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  6818. getLLVMStyleWithColumns(25)));
  6819. EXPECT_EQ("someFunction1234567890(\n"
  6820. " \"aaabbbcccdddeeeff\"\n"
  6821. " \"f\");",
  6822. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  6823. getLLVMStyleWithColumns(24)));
  6824. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  6825. " \"ddde \"\n"
  6826. " \"efff\");",
  6827. format("someFunction(\"aaabbbcc ddde efff\");",
  6828. getLLVMStyleWithColumns(25)));
  6829. EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
  6830. " \"ddeeefff\");",
  6831. format("someFunction(\"aaabbbccc ddeeefff\");",
  6832. getLLVMStyleWithColumns(25)));
  6833. EXPECT_EQ("someFunction1234567890(\n"
  6834. " \"aaabb \"\n"
  6835. " \"cccdddeeefff\");",
  6836. format("someFunction1234567890(\"aaabb cccdddeeefff\");",
  6837. getLLVMStyleWithColumns(25)));
  6838. EXPECT_EQ("#define A \\\n"
  6839. " string s = \\\n"
  6840. " \"123456789\" \\\n"
  6841. " \"0\"; \\\n"
  6842. " int i;",
  6843. format("#define A string s = \"1234567890\"; int i;",
  6844. getLLVMStyleWithColumns(20)));
  6845. // FIXME: Put additional penalties on breaking at non-whitespace locations.
  6846. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  6847. " \"dddeeeff\"\n"
  6848. " \"f\");",
  6849. format("someFunction(\"aaabbbcc dddeeefff\");",
  6850. getLLVMStyleWithColumns(25)));
  6851. }
  6852. TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
  6853. EXPECT_EQ("\"\\a\"",
  6854. format("\"\\a\"", getLLVMStyleWithColumns(3)));
  6855. EXPECT_EQ("\"\\\"",
  6856. format("\"\\\"", getLLVMStyleWithColumns(2)));
  6857. EXPECT_EQ("\"test\"\n"
  6858. "\"\\n\"",
  6859. format("\"test\\n\"", getLLVMStyleWithColumns(7)));
  6860. EXPECT_EQ("\"tes\\\\\"\n"
  6861. "\"n\"",
  6862. format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
  6863. EXPECT_EQ("\"\\\\\\\\\"\n"
  6864. "\"\\n\"",
  6865. format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
  6866. EXPECT_EQ("\"\\uff01\"",
  6867. format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
  6868. EXPECT_EQ("\"\\uff01\"\n"
  6869. "\"test\"",
  6870. format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
  6871. EXPECT_EQ("\"\\Uff01ff02\"",
  6872. format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
  6873. EXPECT_EQ("\"\\x000000000001\"\n"
  6874. "\"next\"",
  6875. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
  6876. EXPECT_EQ("\"\\x000000000001next\"",
  6877. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
  6878. EXPECT_EQ("\"\\x000000000001\"",
  6879. format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
  6880. EXPECT_EQ("\"test\"\n"
  6881. "\"\\000000\"\n"
  6882. "\"000001\"",
  6883. format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
  6884. EXPECT_EQ("\"test\\000\"\n"
  6885. "\"00000000\"\n"
  6886. "\"1\"",
  6887. format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
  6888. // FIXME: We probably don't need to care about escape sequences in raw
  6889. // literals.
  6890. EXPECT_EQ("R\"(\\x)\"\n"
  6891. "R\"(\\x00)\"\n",
  6892. format("R\"(\\x\\x00)\"\n", getGoogleStyleWithColumns(7)));
  6893. }
  6894. TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
  6895. verifyFormat("void f() {\n"
  6896. " return g() {}\n"
  6897. " void h() {}");
  6898. verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
  6899. "g();\n"
  6900. "}");
  6901. }
  6902. TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
  6903. verifyFormat(
  6904. "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
  6905. }
  6906. TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
  6907. verifyFormat("class X {\n"
  6908. " void f() {\n"
  6909. " }\n"
  6910. "};",
  6911. getLLVMStyleWithColumns(12));
  6912. }
  6913. TEST_F(FormatTest, ConfigurableIndentWidth) {
  6914. FormatStyle EightIndent = getLLVMStyleWithColumns(18);
  6915. EightIndent.IndentWidth = 8;
  6916. EightIndent.ContinuationIndentWidth = 8;
  6917. verifyFormat("void f() {\n"
  6918. " someFunction();\n"
  6919. " if (true) {\n"
  6920. " f();\n"
  6921. " }\n"
  6922. "}",
  6923. EightIndent);
  6924. verifyFormat("class X {\n"
  6925. " void f() {\n"
  6926. " }\n"
  6927. "};",
  6928. EightIndent);
  6929. verifyFormat("int x[] = {\n"
  6930. " call(),\n"
  6931. " call()};",
  6932. EightIndent);
  6933. }
  6934. TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
  6935. verifyFormat("double\n"
  6936. "f();",
  6937. getLLVMStyleWithColumns(8));
  6938. }
  6939. TEST_F(FormatTest, ConfigurableUseOfTab) {
  6940. FormatStyle Tab = getLLVMStyleWithColumns(42);
  6941. Tab.IndentWidth = 8;
  6942. Tab.UseTab = FormatStyle::UT_Always;
  6943. Tab.AlignEscapedNewlinesLeft = true;
  6944. EXPECT_EQ("if (aaaaaaaa && // q\n"
  6945. " bb)\t\t// w\n"
  6946. "\t;",
  6947. format("if (aaaaaaaa &&// q\n"
  6948. "bb)// w\n"
  6949. ";",
  6950. Tab));
  6951. EXPECT_EQ("if (aaa && bbb) // w\n"
  6952. "\t;",
  6953. format("if(aaa&&bbb)// w\n"
  6954. ";",
  6955. Tab));
  6956. verifyFormat("class X {\n"
  6957. "\tvoid f() {\n"
  6958. "\t\tsomeFunction(parameter1,\n"
  6959. "\t\t\t parameter2);\n"
  6960. "\t}\n"
  6961. "};",
  6962. Tab);
  6963. verifyFormat("#define A \\\n"
  6964. "\tvoid f() { \\\n"
  6965. "\t\tsomeFunction( \\\n"
  6966. "\t\t parameter1, \\\n"
  6967. "\t\t parameter2); \\\n"
  6968. "\t}",
  6969. Tab);
  6970. EXPECT_EQ("void f() {\n"
  6971. "\tf();\n"
  6972. "\tg();\n"
  6973. "}",
  6974. format("void f() {\n"
  6975. "\tf();\n"
  6976. "\tg();\n"
  6977. "}",
  6978. 0, 0, Tab));
  6979. EXPECT_EQ("void f() {\n"
  6980. "\tf();\n"
  6981. "\tg();\n"
  6982. "}",
  6983. format("void f() {\n"
  6984. "\tf();\n"
  6985. "\tg();\n"
  6986. "}",
  6987. 16, 0, Tab));
  6988. EXPECT_EQ("void f() {\n"
  6989. " \tf();\n"
  6990. "\tg();\n"
  6991. "}",
  6992. format("void f() {\n"
  6993. " \tf();\n"
  6994. " \tg();\n"
  6995. "}",
  6996. 21, 0, Tab));
  6997. Tab.TabWidth = 4;
  6998. Tab.IndentWidth = 8;
  6999. verifyFormat("class TabWidth4Indent8 {\n"
  7000. "\t\tvoid f() {\n"
  7001. "\t\t\t\tsomeFunction(parameter1,\n"
  7002. "\t\t\t\t\t\t\t parameter2);\n"
  7003. "\t\t}\n"
  7004. "};",
  7005. Tab);
  7006. Tab.TabWidth = 4;
  7007. Tab.IndentWidth = 4;
  7008. verifyFormat("class TabWidth4Indent4 {\n"
  7009. "\tvoid f() {\n"
  7010. "\t\tsomeFunction(parameter1,\n"
  7011. "\t\t\t\t\t parameter2);\n"
  7012. "\t}\n"
  7013. "};",
  7014. Tab);
  7015. Tab.TabWidth = 8;
  7016. Tab.IndentWidth = 4;
  7017. verifyFormat("class TabWidth8Indent4 {\n"
  7018. " void f() {\n"
  7019. "\tsomeFunction(parameter1,\n"
  7020. "\t\t parameter2);\n"
  7021. " }\n"
  7022. "};",
  7023. Tab);
  7024. Tab.TabWidth = 8;
  7025. Tab.IndentWidth = 8;
  7026. EXPECT_EQ("/*\n"
  7027. "\t a\t\tcomment\n"
  7028. "\t in multiple lines\n"
  7029. " */",
  7030. format(" /*\t \t \n"
  7031. " \t \t a\t\tcomment\t \t\n"
  7032. " \t \t in multiple lines\t\n"
  7033. " \t */",
  7034. Tab));
  7035. Tab.UseTab = FormatStyle::UT_ForIndentation;
  7036. verifyFormat("{\n"
  7037. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7038. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7039. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7040. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7041. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7042. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7043. "};",
  7044. Tab);
  7045. verifyFormat("enum A {\n"
  7046. "\ta1, // Force multiple lines\n"
  7047. "\ta2,\n"
  7048. "\ta3\n"
  7049. "};",
  7050. Tab);
  7051. EXPECT_EQ("if (aaaaaaaa && // q\n"
  7052. " bb) // w\n"
  7053. "\t;",
  7054. format("if (aaaaaaaa &&// q\n"
  7055. "bb)// w\n"
  7056. ";",
  7057. Tab));
  7058. verifyFormat("class X {\n"
  7059. "\tvoid f() {\n"
  7060. "\t\tsomeFunction(parameter1,\n"
  7061. "\t\t parameter2);\n"
  7062. "\t}\n"
  7063. "};",
  7064. Tab);
  7065. verifyFormat("{\n"
  7066. "\tQ({\n"
  7067. "\t\t int a;\n"
  7068. "\t\t someFunction(aaaaaaaaaa,\n"
  7069. "\t\t bbbbbbbbb);\n"
  7070. "\t },\n"
  7071. "\t p);\n"
  7072. "}",
  7073. Tab);
  7074. EXPECT_EQ("{\n"
  7075. "\t/* aaaa\n"
  7076. "\t bbbb */\n"
  7077. "}",
  7078. format("{\n"
  7079. "/* aaaa\n"
  7080. " bbbb */\n"
  7081. "}",
  7082. Tab));
  7083. EXPECT_EQ("{\n"
  7084. "\t/*\n"
  7085. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7086. "\t bbbbbbbbbbbbb\n"
  7087. "\t*/\n"
  7088. "}",
  7089. format("{\n"
  7090. "/*\n"
  7091. " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7092. "*/\n"
  7093. "}",
  7094. Tab));
  7095. EXPECT_EQ("{\n"
  7096. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7097. "\t// bbbbbbbbbbbbb\n"
  7098. "}",
  7099. format("{\n"
  7100. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7101. "}",
  7102. Tab));
  7103. EXPECT_EQ("{\n"
  7104. "\t/*\n"
  7105. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7106. "\t bbbbbbbbbbbbb\n"
  7107. "\t*/\n"
  7108. "}",
  7109. format("{\n"
  7110. "\t/*\n"
  7111. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7112. "\t*/\n"
  7113. "}",
  7114. Tab));
  7115. EXPECT_EQ("{\n"
  7116. "\t/*\n"
  7117. "\n"
  7118. "\t*/\n"
  7119. "}",
  7120. format("{\n"
  7121. "\t/*\n"
  7122. "\n"
  7123. "\t*/\n"
  7124. "}",
  7125. Tab));
  7126. EXPECT_EQ("{\n"
  7127. "\t/*\n"
  7128. " asdf\n"
  7129. "\t*/\n"
  7130. "}",
  7131. format("{\n"
  7132. "\t/*\n"
  7133. " asdf\n"
  7134. "\t*/\n"
  7135. "}",
  7136. Tab));
  7137. Tab.UseTab = FormatStyle::UT_Never;
  7138. EXPECT_EQ("/*\n"
  7139. " a\t\tcomment\n"
  7140. " in multiple lines\n"
  7141. " */",
  7142. format(" /*\t \t \n"
  7143. " \t \t a\t\tcomment\t \t\n"
  7144. " \t \t in multiple lines\t\n"
  7145. " \t */",
  7146. Tab));
  7147. EXPECT_EQ("/* some\n"
  7148. " comment */",
  7149. format(" \t \t /* some\n"
  7150. " \t \t comment */",
  7151. Tab));
  7152. EXPECT_EQ("int a; /* some\n"
  7153. " comment */",
  7154. format(" \t \t int a; /* some\n"
  7155. " \t \t comment */",
  7156. Tab));
  7157. EXPECT_EQ("int a; /* some\n"
  7158. "comment */",
  7159. format(" \t \t int\ta; /* some\n"
  7160. " \t \t comment */",
  7161. Tab));
  7162. EXPECT_EQ("f(\"\t\t\"); /* some\n"
  7163. " comment */",
  7164. format(" \t \t f(\"\t\t\"); /* some\n"
  7165. " \t \t comment */",
  7166. Tab));
  7167. EXPECT_EQ("{\n"
  7168. " /*\n"
  7169. " * Comment\n"
  7170. " */\n"
  7171. " int i;\n"
  7172. "}",
  7173. format("{\n"
  7174. "\t/*\n"
  7175. "\t * Comment\n"
  7176. "\t */\n"
  7177. "\t int i;\n"
  7178. "}"));
  7179. }
  7180. TEST_F(FormatTest, CalculatesOriginalColumn) {
  7181. EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7182. "q\"; /* some\n"
  7183. " comment */",
  7184. format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7185. "q\"; /* some\n"
  7186. " comment */",
  7187. getLLVMStyle()));
  7188. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  7189. "/* some\n"
  7190. " comment */",
  7191. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  7192. " /* some\n"
  7193. " comment */",
  7194. getLLVMStyle()));
  7195. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7196. "qqq\n"
  7197. "/* some\n"
  7198. " comment */",
  7199. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7200. "qqq\n"
  7201. " /* some\n"
  7202. " comment */",
  7203. getLLVMStyle()));
  7204. EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7205. "wwww; /* some\n"
  7206. " comment */",
  7207. format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7208. "wwww; /* some\n"
  7209. " comment */",
  7210. getLLVMStyle()));
  7211. }
  7212. TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
  7213. FormatStyle NoSpace = getLLVMStyle();
  7214. NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
  7215. verifyFormat("while(true)\n"
  7216. " continue;", NoSpace);
  7217. verifyFormat("for(;;)\n"
  7218. " continue;", NoSpace);
  7219. verifyFormat("if(true)\n"
  7220. " f();\n"
  7221. "else if(true)\n"
  7222. " f();", NoSpace);
  7223. verifyFormat("do {\n"
  7224. " do_something();\n"
  7225. "} while(something());", NoSpace);
  7226. verifyFormat("switch(x) {\n"
  7227. "default:\n"
  7228. " break;\n"
  7229. "}", NoSpace);
  7230. verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
  7231. verifyFormat("size_t x = sizeof(x);", NoSpace);
  7232. verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
  7233. verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
  7234. verifyFormat("alignas(128) char a[128];", NoSpace);
  7235. verifyFormat("size_t x = alignof(MyType);", NoSpace);
  7236. verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
  7237. verifyFormat("int f() throw(Deprecated);", NoSpace);
  7238. FormatStyle Space = getLLVMStyle();
  7239. Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
  7240. verifyFormat("int f ();", Space);
  7241. verifyFormat("void f (int a, T b) {\n"
  7242. " while (true)\n"
  7243. " continue;\n"
  7244. "}",
  7245. Space);
  7246. verifyFormat("if (true)\n"
  7247. " f ();\n"
  7248. "else if (true)\n"
  7249. " f ();",
  7250. Space);
  7251. verifyFormat("do {\n"
  7252. " do_something ();\n"
  7253. "} while (something ());",
  7254. Space);
  7255. verifyFormat("switch (x) {\n"
  7256. "default:\n"
  7257. " break;\n"
  7258. "}",
  7259. Space);
  7260. verifyFormat("A::A () : a (1) {}", Space);
  7261. verifyFormat("void f () __attribute__ ((asdf));", Space);
  7262. verifyFormat("*(&a + 1);\n"
  7263. "&((&a)[1]);\n"
  7264. "a[(b + c) * d];\n"
  7265. "(((a + 1) * 2) + 3) * 4;",
  7266. Space);
  7267. verifyFormat("#define A(x) x", Space);
  7268. verifyFormat("#define A (x) x", Space);
  7269. verifyFormat("#if defined(x)\n"
  7270. "#endif",
  7271. Space);
  7272. verifyFormat("auto i = std::make_unique<int> (5);", Space);
  7273. verifyFormat("size_t x = sizeof (x);", Space);
  7274. verifyFormat("auto f (int x) -> decltype (x);", Space);
  7275. verifyFormat("int f (T x) noexcept (x.create ());", Space);
  7276. verifyFormat("alignas (128) char a[128];", Space);
  7277. verifyFormat("size_t x = alignof (MyType);", Space);
  7278. verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
  7279. verifyFormat("int f () throw (Deprecated);", Space);
  7280. }
  7281. TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
  7282. FormatStyle Spaces = getLLVMStyle();
  7283. Spaces.SpacesInParentheses = true;
  7284. verifyFormat("call( x, y, z );", Spaces);
  7285. verifyFormat("while ( (bool)1 )\n"
  7286. " continue;", Spaces);
  7287. verifyFormat("for ( ;; )\n"
  7288. " continue;", Spaces);
  7289. verifyFormat("if ( true )\n"
  7290. " f();\n"
  7291. "else if ( true )\n"
  7292. " f();", Spaces);
  7293. verifyFormat("do {\n"
  7294. " do_something( (int)i );\n"
  7295. "} while ( something() );", Spaces);
  7296. verifyFormat("switch ( x ) {\n"
  7297. "default:\n"
  7298. " break;\n"
  7299. "}", Spaces);
  7300. Spaces.SpacesInParentheses = false;
  7301. Spaces.SpacesInCStyleCastParentheses = true;
  7302. verifyFormat("Type *A = ( Type * )P;", Spaces);
  7303. verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
  7304. verifyFormat("x = ( int32 )y;", Spaces);
  7305. verifyFormat("int a = ( int )(2.0f);", Spaces);
  7306. verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
  7307. verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
  7308. verifyFormat("#define x (( int )-1)", Spaces);
  7309. Spaces.SpacesInParentheses = false;
  7310. Spaces.SpaceInEmptyParentheses = true;
  7311. verifyFormat("call(x, y, z);", Spaces);
  7312. verifyFormat("call( )", Spaces);
  7313. // Run the first set of tests again with
  7314. // Spaces.SpacesInParentheses = false,
  7315. // Spaces.SpaceInEmptyParentheses = true and
  7316. // Spaces.SpacesInCStyleCastParentheses = true
  7317. Spaces.SpacesInParentheses = false,
  7318. Spaces.SpaceInEmptyParentheses = true;
  7319. Spaces.SpacesInCStyleCastParentheses = true;
  7320. verifyFormat("call(x, y, z);", Spaces);
  7321. verifyFormat("while (( bool )1)\n"
  7322. " continue;", Spaces);
  7323. verifyFormat("for (;;)\n"
  7324. " continue;", Spaces);
  7325. verifyFormat("if (true)\n"
  7326. " f( );\n"
  7327. "else if (true)\n"
  7328. " f( );", Spaces);
  7329. verifyFormat("do {\n"
  7330. " do_something(( int )i);\n"
  7331. "} while (something( ));", Spaces);
  7332. verifyFormat("switch (x) {\n"
  7333. "default:\n"
  7334. " break;\n"
  7335. "}", Spaces);
  7336. Spaces.SpaceAfterCStyleCast = true;
  7337. verifyFormat("call(x, y, z);", Spaces);
  7338. verifyFormat("while (( bool ) 1)\n"
  7339. " continue;",
  7340. Spaces);
  7341. verifyFormat("for (;;)\n"
  7342. " continue;",
  7343. Spaces);
  7344. verifyFormat("if (true)\n"
  7345. " f( );\n"
  7346. "else if (true)\n"
  7347. " f( );",
  7348. Spaces);
  7349. verifyFormat("do {\n"
  7350. " do_something(( int ) i);\n"
  7351. "} while (something( ));",
  7352. Spaces);
  7353. verifyFormat("switch (x) {\n"
  7354. "default:\n"
  7355. " break;\n"
  7356. "}",
  7357. Spaces);
  7358. Spaces.SpacesInCStyleCastParentheses = false;
  7359. Spaces.SpaceAfterCStyleCast = true;
  7360. verifyFormat("while ((bool) 1)\n"
  7361. " continue;",
  7362. Spaces);
  7363. verifyFormat("do {\n"
  7364. " do_something((int) i);\n"
  7365. "} while (something( ));",
  7366. Spaces);
  7367. }
  7368. TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
  7369. verifyFormat("int a[5];");
  7370. verifyFormat("a[3] += 42;");
  7371. FormatStyle Spaces = getLLVMStyle();
  7372. Spaces.SpacesInSquareBrackets = true;
  7373. // Lambdas unchanged.
  7374. verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
  7375. verifyFormat("return [i, args...] {};", Spaces);
  7376. // Not lambdas.
  7377. verifyFormat("int a[ 5 ];", Spaces);
  7378. verifyFormat("a[ 3 ] += 42;", Spaces);
  7379. verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
  7380. verifyFormat("double &operator[](int i) { return 0; }\n"
  7381. "int i;",
  7382. Spaces);
  7383. verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
  7384. verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
  7385. verifyFormat("int i = (*b)[ a ]->f();", Spaces);
  7386. }
  7387. TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
  7388. verifyFormat("int a = 5;");
  7389. verifyFormat("a += 42;");
  7390. verifyFormat("a or_eq 8;");
  7391. FormatStyle Spaces = getLLVMStyle();
  7392. Spaces.SpaceBeforeAssignmentOperators = false;
  7393. verifyFormat("int a= 5;", Spaces);
  7394. verifyFormat("a+= 42;", Spaces);
  7395. verifyFormat("a or_eq 8;", Spaces);
  7396. }
  7397. TEST_F(FormatTest, LinuxBraceBreaking) {
  7398. FormatStyle LinuxBraceStyle = getLLVMStyle();
  7399. LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
  7400. verifyFormat("namespace a\n"
  7401. "{\n"
  7402. "class A\n"
  7403. "{\n"
  7404. " void f()\n"
  7405. " {\n"
  7406. " if (true) {\n"
  7407. " a();\n"
  7408. " b();\n"
  7409. " }\n"
  7410. " }\n"
  7411. " void g() { return; }\n"
  7412. "};\n"
  7413. "struct B {\n"
  7414. " int x;\n"
  7415. "};\n"
  7416. "}\n",
  7417. LinuxBraceStyle);
  7418. verifyFormat("enum X {\n"
  7419. " Y = 0,\n"
  7420. "}\n",
  7421. LinuxBraceStyle);
  7422. verifyFormat("struct S {\n"
  7423. " int Type;\n"
  7424. " union {\n"
  7425. " int x;\n"
  7426. " double y;\n"
  7427. " } Value;\n"
  7428. " class C\n"
  7429. " {\n"
  7430. " MyFavoriteType Value;\n"
  7431. " } Class;\n"
  7432. "}\n",
  7433. LinuxBraceStyle);
  7434. }
  7435. TEST_F(FormatTest, StroustrupBraceBreaking) {
  7436. FormatStyle StroustrupBraceStyle = getLLVMStyle();
  7437. StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  7438. verifyFormat("namespace a {\n"
  7439. "class A {\n"
  7440. " void f()\n"
  7441. " {\n"
  7442. " if (true) {\n"
  7443. " a();\n"
  7444. " b();\n"
  7445. " }\n"
  7446. " }\n"
  7447. " void g() { return; }\n"
  7448. "};\n"
  7449. "struct B {\n"
  7450. " int x;\n"
  7451. "};\n"
  7452. "}\n",
  7453. StroustrupBraceStyle);
  7454. verifyFormat("void foo()\n"
  7455. "{\n"
  7456. " if (a) {\n"
  7457. " a();\n"
  7458. " }\n"
  7459. " else {\n"
  7460. " b();\n"
  7461. " }\n"
  7462. "}\n",
  7463. StroustrupBraceStyle);
  7464. verifyFormat("#ifdef _DEBUG\n"
  7465. "int foo(int i = 0)\n"
  7466. "#else\n"
  7467. "int foo(int i = 5)\n"
  7468. "#endif\n"
  7469. "{\n"
  7470. " return i;\n"
  7471. "}",
  7472. StroustrupBraceStyle);
  7473. verifyFormat("void foo() {}\n"
  7474. "void bar()\n"
  7475. "#ifdef _DEBUG\n"
  7476. "{\n"
  7477. " foo();\n"
  7478. "}\n"
  7479. "#else\n"
  7480. "{\n"
  7481. "}\n"
  7482. "#endif",
  7483. StroustrupBraceStyle);
  7484. verifyFormat("void foobar() { int i = 5; }\n"
  7485. "#ifdef _DEBUG\n"
  7486. "void bar() {}\n"
  7487. "#else\n"
  7488. "void bar() { foobar(); }\n"
  7489. "#endif",
  7490. StroustrupBraceStyle);
  7491. }
  7492. TEST_F(FormatTest, AllmanBraceBreaking) {
  7493. FormatStyle AllmanBraceStyle = getLLVMStyle();
  7494. AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
  7495. verifyFormat("namespace a\n"
  7496. "{\n"
  7497. "class A\n"
  7498. "{\n"
  7499. " void f()\n"
  7500. " {\n"
  7501. " if (true)\n"
  7502. " {\n"
  7503. " a();\n"
  7504. " b();\n"
  7505. " }\n"
  7506. " }\n"
  7507. " void g() { return; }\n"
  7508. "};\n"
  7509. "struct B\n"
  7510. "{\n"
  7511. " int x;\n"
  7512. "};\n"
  7513. "}",
  7514. AllmanBraceStyle);
  7515. verifyFormat("void f()\n"
  7516. "{\n"
  7517. " if (true)\n"
  7518. " {\n"
  7519. " a();\n"
  7520. " }\n"
  7521. " else if (false)\n"
  7522. " {\n"
  7523. " b();\n"
  7524. " }\n"
  7525. " else\n"
  7526. " {\n"
  7527. " c();\n"
  7528. " }\n"
  7529. "}\n",
  7530. AllmanBraceStyle);
  7531. verifyFormat("void f()\n"
  7532. "{\n"
  7533. " for (int i = 0; i < 10; ++i)\n"
  7534. " {\n"
  7535. " a();\n"
  7536. " }\n"
  7537. " while (false)\n"
  7538. " {\n"
  7539. " b();\n"
  7540. " }\n"
  7541. " do\n"
  7542. " {\n"
  7543. " c();\n"
  7544. " } while (false)\n"
  7545. "}\n",
  7546. AllmanBraceStyle);
  7547. verifyFormat("void f(int a)\n"
  7548. "{\n"
  7549. " switch (a)\n"
  7550. " {\n"
  7551. " case 0:\n"
  7552. " break;\n"
  7553. " case 1:\n"
  7554. " {\n"
  7555. " break;\n"
  7556. " }\n"
  7557. " case 2:\n"
  7558. " {\n"
  7559. " }\n"
  7560. " break;\n"
  7561. " default:\n"
  7562. " break;\n"
  7563. " }\n"
  7564. "}\n",
  7565. AllmanBraceStyle);
  7566. verifyFormat("enum X\n"
  7567. "{\n"
  7568. " Y = 0,\n"
  7569. "}\n",
  7570. AllmanBraceStyle);
  7571. verifyFormat("enum X\n"
  7572. "{\n"
  7573. " Y = 0\n"
  7574. "}\n",
  7575. AllmanBraceStyle);
  7576. verifyFormat("@interface BSApplicationController ()\n"
  7577. "{\n"
  7578. "@private\n"
  7579. " id _extraIvar;\n"
  7580. "}\n"
  7581. "@end\n",
  7582. AllmanBraceStyle);
  7583. verifyFormat("#ifdef _DEBUG\n"
  7584. "int foo(int i = 0)\n"
  7585. "#else\n"
  7586. "int foo(int i = 5)\n"
  7587. "#endif\n"
  7588. "{\n"
  7589. " return i;\n"
  7590. "}",
  7591. AllmanBraceStyle);
  7592. verifyFormat("void foo() {}\n"
  7593. "void bar()\n"
  7594. "#ifdef _DEBUG\n"
  7595. "{\n"
  7596. " foo();\n"
  7597. "}\n"
  7598. "#else\n"
  7599. "{\n"
  7600. "}\n"
  7601. "#endif",
  7602. AllmanBraceStyle);
  7603. verifyFormat("void foobar() { int i = 5; }\n"
  7604. "#ifdef _DEBUG\n"
  7605. "void bar() {}\n"
  7606. "#else\n"
  7607. "void bar() { foobar(); }\n"
  7608. "#endif",
  7609. AllmanBraceStyle);
  7610. // This shouldn't affect ObjC blocks..
  7611. verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
  7612. " // ...\n"
  7613. " int i;\n"
  7614. "}];",
  7615. AllmanBraceStyle);
  7616. verifyFormat("void (^block)(void) = ^{\n"
  7617. " // ...\n"
  7618. " int i;\n"
  7619. "};",
  7620. AllmanBraceStyle);
  7621. // .. or dict literals.
  7622. verifyFormat("void f()\n"
  7623. "{\n"
  7624. " [object someMethod:@{ @\"a\" : @\"b\" }];\n"
  7625. "}",
  7626. AllmanBraceStyle);
  7627. AllmanBraceStyle.ColumnLimit = 19;
  7628. verifyFormat("void f() { int i; }", AllmanBraceStyle);
  7629. AllmanBraceStyle.ColumnLimit = 18;
  7630. verifyFormat("void f()\n"
  7631. "{\n"
  7632. " int i;\n"
  7633. "}",
  7634. AllmanBraceStyle);
  7635. AllmanBraceStyle.ColumnLimit = 80;
  7636. FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
  7637. BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
  7638. BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
  7639. verifyFormat("void f(bool b)\n"
  7640. "{\n"
  7641. " if (b)\n"
  7642. " {\n"
  7643. " return;\n"
  7644. " }\n"
  7645. "}\n",
  7646. BreakBeforeBraceShortIfs);
  7647. verifyFormat("void f(bool b)\n"
  7648. "{\n"
  7649. " if (b) return;\n"
  7650. "}\n",
  7651. BreakBeforeBraceShortIfs);
  7652. verifyFormat("void f(bool b)\n"
  7653. "{\n"
  7654. " while (b)\n"
  7655. " {\n"
  7656. " return;\n"
  7657. " }\n"
  7658. "}\n",
  7659. BreakBeforeBraceShortIfs);
  7660. }
  7661. TEST_F(FormatTest, GNUBraceBreaking) {
  7662. FormatStyle GNUBraceStyle = getLLVMStyle();
  7663. GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
  7664. verifyFormat("namespace a\n"
  7665. "{\n"
  7666. "class A\n"
  7667. "{\n"
  7668. " void f()\n"
  7669. " {\n"
  7670. " int a;\n"
  7671. " {\n"
  7672. " int b;\n"
  7673. " }\n"
  7674. " if (true)\n"
  7675. " {\n"
  7676. " a();\n"
  7677. " b();\n"
  7678. " }\n"
  7679. " }\n"
  7680. " void g() { return; }\n"
  7681. "}\n"
  7682. "}",
  7683. GNUBraceStyle);
  7684. verifyFormat("void f()\n"
  7685. "{\n"
  7686. " if (true)\n"
  7687. " {\n"
  7688. " a();\n"
  7689. " }\n"
  7690. " else if (false)\n"
  7691. " {\n"
  7692. " b();\n"
  7693. " }\n"
  7694. " else\n"
  7695. " {\n"
  7696. " c();\n"
  7697. " }\n"
  7698. "}\n",
  7699. GNUBraceStyle);
  7700. verifyFormat("void f()\n"
  7701. "{\n"
  7702. " for (int i = 0; i < 10; ++i)\n"
  7703. " {\n"
  7704. " a();\n"
  7705. " }\n"
  7706. " while (false)\n"
  7707. " {\n"
  7708. " b();\n"
  7709. " }\n"
  7710. " do\n"
  7711. " {\n"
  7712. " c();\n"
  7713. " }\n"
  7714. " while (false);\n"
  7715. "}\n",
  7716. GNUBraceStyle);
  7717. verifyFormat("void f(int a)\n"
  7718. "{\n"
  7719. " switch (a)\n"
  7720. " {\n"
  7721. " case 0:\n"
  7722. " break;\n"
  7723. " case 1:\n"
  7724. " {\n"
  7725. " break;\n"
  7726. " }\n"
  7727. " case 2:\n"
  7728. " {\n"
  7729. " }\n"
  7730. " break;\n"
  7731. " default:\n"
  7732. " break;\n"
  7733. " }\n"
  7734. "}\n",
  7735. GNUBraceStyle);
  7736. verifyFormat("enum X\n"
  7737. "{\n"
  7738. " Y = 0,\n"
  7739. "}\n",
  7740. GNUBraceStyle);
  7741. verifyFormat("@interface BSApplicationController ()\n"
  7742. "{\n"
  7743. "@private\n"
  7744. " id _extraIvar;\n"
  7745. "}\n"
  7746. "@end\n",
  7747. GNUBraceStyle);
  7748. verifyFormat("#ifdef _DEBUG\n"
  7749. "int foo(int i = 0)\n"
  7750. "#else\n"
  7751. "int foo(int i = 5)\n"
  7752. "#endif\n"
  7753. "{\n"
  7754. " return i;\n"
  7755. "}",
  7756. GNUBraceStyle);
  7757. verifyFormat("void foo() {}\n"
  7758. "void bar()\n"
  7759. "#ifdef _DEBUG\n"
  7760. "{\n"
  7761. " foo();\n"
  7762. "}\n"
  7763. "#else\n"
  7764. "{\n"
  7765. "}\n"
  7766. "#endif",
  7767. GNUBraceStyle);
  7768. verifyFormat("void foobar() { int i = 5; }\n"
  7769. "#ifdef _DEBUG\n"
  7770. "void bar() {}\n"
  7771. "#else\n"
  7772. "void bar() { foobar(); }\n"
  7773. "#endif",
  7774. GNUBraceStyle);
  7775. }
  7776. TEST_F(FormatTest, CatchExceptionReferenceBinding) {
  7777. verifyFormat("void f() {\n"
  7778. " try {\n"
  7779. " } catch (const Exception &e) {\n"
  7780. " }\n"
  7781. "}\n",
  7782. getLLVMStyle());
  7783. }
  7784. TEST_F(FormatTest, UnderstandsPragmas) {
  7785. verifyFormat("#pragma omp reduction(| : var)");
  7786. verifyFormat("#pragma omp reduction(+ : var)");
  7787. EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
  7788. "(including parentheses).",
  7789. format("#pragma mark Any non-hyphenated or hyphenated string "
  7790. "(including parentheses)."));
  7791. }
  7792. #define EXPECT_ALL_STYLES_EQUAL(Styles) \
  7793. for (size_t i = 1; i < Styles.size(); ++i) \
  7794. EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " \
  7795. << Styles.size() \
  7796. << " differs from Style #0"
  7797. TEST_F(FormatTest, GetsPredefinedStyleByName) {
  7798. SmallVector<FormatStyle, 3> Styles;
  7799. Styles.resize(3);
  7800. Styles[0] = getLLVMStyle();
  7801. EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
  7802. EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
  7803. EXPECT_ALL_STYLES_EQUAL(Styles);
  7804. Styles[0] = getGoogleStyle();
  7805. EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
  7806. EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
  7807. EXPECT_ALL_STYLES_EQUAL(Styles);
  7808. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  7809. EXPECT_TRUE(
  7810. getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
  7811. EXPECT_TRUE(
  7812. getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
  7813. EXPECT_ALL_STYLES_EQUAL(Styles);
  7814. Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
  7815. EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
  7816. EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
  7817. EXPECT_ALL_STYLES_EQUAL(Styles);
  7818. Styles[0] = getMozillaStyle();
  7819. EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
  7820. EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
  7821. EXPECT_ALL_STYLES_EQUAL(Styles);
  7822. Styles[0] = getWebKitStyle();
  7823. EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
  7824. EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
  7825. EXPECT_ALL_STYLES_EQUAL(Styles);
  7826. Styles[0] = getGNUStyle();
  7827. EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
  7828. EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
  7829. EXPECT_ALL_STYLES_EQUAL(Styles);
  7830. EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
  7831. }
  7832. TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
  7833. SmallVector<FormatStyle, 8> Styles;
  7834. Styles.resize(2);
  7835. Styles[0] = getGoogleStyle();
  7836. Styles[1] = getLLVMStyle();
  7837. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  7838. EXPECT_ALL_STYLES_EQUAL(Styles);
  7839. Styles.resize(5);
  7840. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  7841. Styles[1] = getLLVMStyle();
  7842. Styles[1].Language = FormatStyle::LK_JavaScript;
  7843. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  7844. Styles[2] = getLLVMStyle();
  7845. Styles[2].Language = FormatStyle::LK_JavaScript;
  7846. EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
  7847. "BasedOnStyle: Google",
  7848. &Styles[2]).value());
  7849. Styles[3] = getLLVMStyle();
  7850. Styles[3].Language = FormatStyle::LK_JavaScript;
  7851. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
  7852. "Language: JavaScript",
  7853. &Styles[3]).value());
  7854. Styles[4] = getLLVMStyle();
  7855. Styles[4].Language = FormatStyle::LK_JavaScript;
  7856. EXPECT_EQ(0, parseConfiguration("---\n"
  7857. "BasedOnStyle: LLVM\n"
  7858. "IndentWidth: 123\n"
  7859. "---\n"
  7860. "BasedOnStyle: Google\n"
  7861. "Language: JavaScript",
  7862. &Styles[4]).value());
  7863. EXPECT_ALL_STYLES_EQUAL(Styles);
  7864. }
  7865. #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \
  7866. Style.FIELD = false; \
  7867. EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \
  7868. EXPECT_TRUE(Style.FIELD); \
  7869. EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \
  7870. EXPECT_FALSE(Style.FIELD);
  7871. #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
  7872. #define CHECK_PARSE(TEXT, FIELD, VALUE) \
  7873. EXPECT_NE(VALUE, Style.FIELD); \
  7874. EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \
  7875. EXPECT_EQ(VALUE, Style.FIELD)
  7876. TEST_F(FormatTest, ParsesConfigurationBools) {
  7877. FormatStyle Style = {};
  7878. Style.Language = FormatStyle::LK_Cpp;
  7879. CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
  7880. CHECK_PARSE_BOOL(AlignTrailingComments);
  7881. CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
  7882. CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
  7883. CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
  7884. CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
  7885. CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
  7886. CHECK_PARSE_BOOL(AlwaysBreakAfterDefinitionReturnType);
  7887. CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
  7888. CHECK_PARSE_BOOL(BinPackParameters);
  7889. CHECK_PARSE_BOOL(BinPackArguments);
  7890. CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
  7891. CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
  7892. CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
  7893. CHECK_PARSE_BOOL(DerivePointerAlignment);
  7894. CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
  7895. CHECK_PARSE_BOOL(IndentCaseLabels);
  7896. CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
  7897. CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
  7898. CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
  7899. CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
  7900. CHECK_PARSE_BOOL(Cpp11BracedListStyle);
  7901. CHECK_PARSE_BOOL(SpacesInParentheses);
  7902. CHECK_PARSE_BOOL(SpacesInSquareBrackets);
  7903. CHECK_PARSE_BOOL(SpacesInAngles);
  7904. CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
  7905. CHECK_PARSE_BOOL(SpacesInContainerLiterals);
  7906. CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
  7907. CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
  7908. CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
  7909. }
  7910. #undef CHECK_PARSE_BOOL
  7911. TEST_F(FormatTest, ParsesConfiguration) {
  7912. FormatStyle Style = {};
  7913. Style.Language = FormatStyle::LK_Cpp;
  7914. CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
  7915. CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
  7916. ConstructorInitializerIndentWidth, 1234u);
  7917. CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
  7918. CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
  7919. CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
  7920. CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
  7921. PenaltyBreakBeforeFirstCallParameter, 1234u);
  7922. CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
  7923. CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
  7924. PenaltyReturnTypeOnItsOwnLine, 1234u);
  7925. CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
  7926. SpacesBeforeTrailingComments, 1234u);
  7927. CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
  7928. CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
  7929. Style.PointerAlignment = FormatStyle::PAS_Middle;
  7930. CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
  7931. FormatStyle::PAS_Left);
  7932. CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
  7933. FormatStyle::PAS_Right);
  7934. CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
  7935. FormatStyle::PAS_Middle);
  7936. // For backward compatibility:
  7937. CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
  7938. FormatStyle::PAS_Left);
  7939. CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
  7940. FormatStyle::PAS_Right);
  7941. CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
  7942. FormatStyle::PAS_Middle);
  7943. Style.Standard = FormatStyle::LS_Auto;
  7944. CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
  7945. CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
  7946. CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
  7947. CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
  7948. CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
  7949. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  7950. CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
  7951. BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
  7952. CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
  7953. FormatStyle::BOS_None);
  7954. CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
  7955. FormatStyle::BOS_All);
  7956. // For backward compatibility:
  7957. CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
  7958. FormatStyle::BOS_None);
  7959. CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
  7960. FormatStyle::BOS_All);
  7961. Style.UseTab = FormatStyle::UT_ForIndentation;
  7962. CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
  7963. CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
  7964. CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
  7965. // For backward compatibility:
  7966. CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
  7967. CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
  7968. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  7969. CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
  7970. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
  7971. CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
  7972. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
  7973. CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
  7974. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
  7975. // For backward compatibility:
  7976. CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
  7977. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
  7978. CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
  7979. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
  7980. Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
  7981. CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
  7982. FormatStyle::SBPO_Never);
  7983. CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
  7984. FormatStyle::SBPO_Always);
  7985. CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
  7986. FormatStyle::SBPO_ControlStatements);
  7987. // For backward compatibility:
  7988. CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
  7989. FormatStyle::SBPO_Never);
  7990. CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
  7991. FormatStyle::SBPO_ControlStatements);
  7992. Style.ColumnLimit = 123;
  7993. FormatStyle BaseStyle = getLLVMStyle();
  7994. CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
  7995. CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
  7996. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  7997. CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
  7998. FormatStyle::BS_Attach);
  7999. CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
  8000. FormatStyle::BS_Linux);
  8001. CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
  8002. FormatStyle::BS_Stroustrup);
  8003. CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
  8004. FormatStyle::BS_Allman);
  8005. CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
  8006. Style.NamespaceIndentation = FormatStyle::NI_All;
  8007. CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
  8008. FormatStyle::NI_None);
  8009. CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
  8010. FormatStyle::NI_Inner);
  8011. CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
  8012. FormatStyle::NI_All);
  8013. Style.ForEachMacros.clear();
  8014. std::vector<std::string> BoostForeach;
  8015. BoostForeach.push_back("BOOST_FOREACH");
  8016. CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
  8017. std::vector<std::string> BoostAndQForeach;
  8018. BoostAndQForeach.push_back("BOOST_FOREACH");
  8019. BoostAndQForeach.push_back("Q_FOREACH");
  8020. CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
  8021. BoostAndQForeach);
  8022. }
  8023. TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
  8024. FormatStyle Style = {};
  8025. Style.Language = FormatStyle::LK_Cpp;
  8026. CHECK_PARSE("Language: Cpp\n"
  8027. "IndentWidth: 12",
  8028. IndentWidth, 12u);
  8029. EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
  8030. "IndentWidth: 34",
  8031. &Style),
  8032. ParseError::Unsuitable);
  8033. EXPECT_EQ(12u, Style.IndentWidth);
  8034. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  8035. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  8036. Style.Language = FormatStyle::LK_JavaScript;
  8037. CHECK_PARSE("Language: JavaScript\n"
  8038. "IndentWidth: 12",
  8039. IndentWidth, 12u);
  8040. CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
  8041. EXPECT_EQ(parseConfiguration("Language: Cpp\n"
  8042. "IndentWidth: 34",
  8043. &Style),
  8044. ParseError::Unsuitable);
  8045. EXPECT_EQ(23u, Style.IndentWidth);
  8046. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  8047. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  8048. CHECK_PARSE("BasedOnStyle: LLVM\n"
  8049. "IndentWidth: 67",
  8050. IndentWidth, 67u);
  8051. CHECK_PARSE("---\n"
  8052. "Language: JavaScript\n"
  8053. "IndentWidth: 12\n"
  8054. "---\n"
  8055. "Language: Cpp\n"
  8056. "IndentWidth: 34\n"
  8057. "...\n",
  8058. IndentWidth, 12u);
  8059. Style.Language = FormatStyle::LK_Cpp;
  8060. CHECK_PARSE("---\n"
  8061. "Language: JavaScript\n"
  8062. "IndentWidth: 12\n"
  8063. "---\n"
  8064. "Language: Cpp\n"
  8065. "IndentWidth: 34\n"
  8066. "...\n",
  8067. IndentWidth, 34u);
  8068. CHECK_PARSE("---\n"
  8069. "IndentWidth: 78\n"
  8070. "---\n"
  8071. "Language: JavaScript\n"
  8072. "IndentWidth: 56\n"
  8073. "...\n",
  8074. IndentWidth, 78u);
  8075. Style.ColumnLimit = 123;
  8076. Style.IndentWidth = 234;
  8077. Style.BreakBeforeBraces = FormatStyle::BS_Linux;
  8078. Style.TabWidth = 345;
  8079. EXPECT_FALSE(parseConfiguration("---\n"
  8080. "IndentWidth: 456\n"
  8081. "BreakBeforeBraces: Allman\n"
  8082. "---\n"
  8083. "Language: JavaScript\n"
  8084. "IndentWidth: 111\n"
  8085. "TabWidth: 111\n"
  8086. "---\n"
  8087. "Language: Cpp\n"
  8088. "BreakBeforeBraces: Stroustrup\n"
  8089. "TabWidth: 789\n"
  8090. "...\n",
  8091. &Style));
  8092. EXPECT_EQ(123u, Style.ColumnLimit);
  8093. EXPECT_EQ(456u, Style.IndentWidth);
  8094. EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
  8095. EXPECT_EQ(789u, Style.TabWidth);
  8096. EXPECT_EQ(parseConfiguration("---\n"
  8097. "Language: JavaScript\n"
  8098. "IndentWidth: 56\n"
  8099. "---\n"
  8100. "IndentWidth: 78\n"
  8101. "...\n",
  8102. &Style),
  8103. ParseError::Error);
  8104. EXPECT_EQ(parseConfiguration("---\n"
  8105. "Language: JavaScript\n"
  8106. "IndentWidth: 56\n"
  8107. "---\n"
  8108. "Language: JavaScript\n"
  8109. "IndentWidth: 78\n"
  8110. "...\n",
  8111. &Style),
  8112. ParseError::Error);
  8113. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  8114. }
  8115. #undef CHECK_PARSE
  8116. TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
  8117. FormatStyle Style = {};
  8118. Style.Language = FormatStyle::LK_JavaScript;
  8119. Style.BreakBeforeTernaryOperators = true;
  8120. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
  8121. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  8122. Style.BreakBeforeTernaryOperators = true;
  8123. EXPECT_EQ(0, parseConfiguration("---\n"
  8124. "BasedOnStyle: Google\n"
  8125. "---\n"
  8126. "Language: JavaScript\n"
  8127. "IndentWidth: 76\n"
  8128. "...\n", &Style).value());
  8129. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  8130. EXPECT_EQ(76u, Style.IndentWidth);
  8131. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  8132. }
  8133. TEST_F(FormatTest, ConfigurationRoundTripTest) {
  8134. FormatStyle Style = getLLVMStyle();
  8135. std::string YAML = configurationAsText(Style);
  8136. FormatStyle ParsedStyle = {};
  8137. ParsedStyle.Language = FormatStyle::LK_Cpp;
  8138. EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
  8139. EXPECT_EQ(Style, ParsedStyle);
  8140. }
  8141. TEST_F(FormatTest, WorksFor8bitEncodings) {
  8142. EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
  8143. "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
  8144. "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
  8145. "\"\xef\xee\xf0\xf3...\"",
  8146. format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
  8147. "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
  8148. "\xef\xee\xf0\xf3...\"",
  8149. getLLVMStyleWithColumns(12)));
  8150. }
  8151. TEST_F(FormatTest, HandlesUTF8BOM) {
  8152. EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
  8153. EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
  8154. format("\xef\xbb\xbf#include <iostream>"));
  8155. EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
  8156. format("\xef\xbb\xbf\n#include <iostream>"));
  8157. }
  8158. // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
  8159. #if !defined(_MSC_VER)
  8160. TEST_F(FormatTest, CountsUTF8CharactersProperly) {
  8161. verifyFormat("\"Однажды в студёную зимнюю пору...\"",
  8162. getLLVMStyleWithColumns(35));
  8163. verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
  8164. getLLVMStyleWithColumns(31));
  8165. verifyFormat("// Однажды в студёную зимнюю пору...",
  8166. getLLVMStyleWithColumns(36));
  8167. verifyFormat("// 一 二 三 四 五 六 七 八 九 十",
  8168. getLLVMStyleWithColumns(32));
  8169. verifyFormat("/* Однажды в студёную зимнюю пору... */",
  8170. getLLVMStyleWithColumns(39));
  8171. verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
  8172. getLLVMStyleWithColumns(35));
  8173. }
  8174. TEST_F(FormatTest, SplitsUTF8Strings) {
  8175. // Non-printable characters' width is currently considered to be the length in
  8176. // bytes in UTF8. The characters can be displayed in very different manner
  8177. // (zero-width, single width with a substitution glyph, expanded to their code
  8178. // (e.g. "<8d>"), so there's no single correct way to handle them.
  8179. EXPECT_EQ("\"aaaaÄ\"\n"
  8180. "\"\xc2\x8d\";",
  8181. format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  8182. EXPECT_EQ("\"aaaaaaaÄ\"\n"
  8183. "\"\xc2\x8d\";",
  8184. format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  8185. EXPECT_EQ(
  8186. "\"Однажды, в \"\n"
  8187. "\"студёную \"\n"
  8188. "\"зимнюю \"\n"
  8189. "\"пору,\"",
  8190. format("\"Однажды, в студёную зимнюю пору,\"",
  8191. getLLVMStyleWithColumns(13)));
  8192. EXPECT_EQ("\"一 二 三 \"\n"
  8193. "\"四 五六 \"\n"
  8194. "\"七 八 九 \"\n"
  8195. "\"十\"",
  8196. format("\"一 二 三 四 五六 七 八 九 十\"",
  8197. getLLVMStyleWithColumns(11)));
  8198. EXPECT_EQ("\"一\t二 \"\n"
  8199. "\"\t三 \"\n"
  8200. "\"四 五\t六 \"\n"
  8201. "\"\t七 \"\n"
  8202. "\"八九十\tqq\"",
  8203. format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
  8204. getLLVMStyleWithColumns(11)));
  8205. }
  8206. TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
  8207. EXPECT_EQ("const char *sssss =\n"
  8208. " \"一二三四五六七八\\\n"
  8209. " 九 十\";",
  8210. format("const char *sssss = \"一二三四五六七八\\\n"
  8211. " 九 十\";",
  8212. getLLVMStyleWithColumns(30)));
  8213. }
  8214. TEST_F(FormatTest, SplitsUTF8LineComments) {
  8215. EXPECT_EQ("// aaaaÄ\xc2\x8d",
  8216. format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
  8217. EXPECT_EQ("// Я из лесу\n"
  8218. "// вышел; был\n"
  8219. "// сильный\n"
  8220. "// мороз.",
  8221. format("// Я из лесу вышел; был сильный мороз.",
  8222. getLLVMStyleWithColumns(13)));
  8223. EXPECT_EQ("// 一二三\n"
  8224. "// 四五六七\n"
  8225. "// 八 九\n"
  8226. "// 十",
  8227. format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9)));
  8228. }
  8229. TEST_F(FormatTest, SplitsUTF8BlockComments) {
  8230. EXPECT_EQ("/* Гляжу,\n"
  8231. " * поднимается\n"
  8232. " * медленно в\n"
  8233. " * гору\n"
  8234. " * Лошадка,\n"
  8235. " * везущая\n"
  8236. " * хворосту\n"
  8237. " * воз. */",
  8238. format("/* Гляжу, поднимается медленно в гору\n"
  8239. " * Лошадка, везущая хворосту воз. */",
  8240. getLLVMStyleWithColumns(13)));
  8241. EXPECT_EQ(
  8242. "/* 一二三\n"
  8243. " * 四五六七\n"
  8244. " * 八 九\n"
  8245. " * 十 */",
  8246. format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9)));
  8247. EXPECT_EQ("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
  8248. " * 𝕓𝕪𝕥𝕖\n"
  8249. " * 𝖀𝕿𝕱-𝟠 */",
  8250. format("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12)));
  8251. }
  8252. #endif // _MSC_VER
  8253. TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
  8254. FormatStyle Style = getLLVMStyle();
  8255. Style.ConstructorInitializerIndentWidth = 4;
  8256. verifyFormat(
  8257. "SomeClass::Constructor()\n"
  8258. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  8259. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  8260. Style);
  8261. Style.ConstructorInitializerIndentWidth = 2;
  8262. verifyFormat(
  8263. "SomeClass::Constructor()\n"
  8264. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  8265. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  8266. Style);
  8267. Style.ConstructorInitializerIndentWidth = 0;
  8268. verifyFormat(
  8269. "SomeClass::Constructor()\n"
  8270. ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  8271. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  8272. Style);
  8273. }
  8274. TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
  8275. FormatStyle Style = getLLVMStyle();
  8276. Style.BreakConstructorInitializersBeforeComma = true;
  8277. Style.ConstructorInitializerIndentWidth = 4;
  8278. verifyFormat("SomeClass::Constructor()\n"
  8279. " : a(a)\n"
  8280. " , b(b)\n"
  8281. " , c(c) {}",
  8282. Style);
  8283. verifyFormat("SomeClass::Constructor()\n"
  8284. " : a(a) {}",
  8285. Style);
  8286. Style.ColumnLimit = 0;
  8287. verifyFormat("SomeClass::Constructor()\n"
  8288. " : a(a) {}",
  8289. Style);
  8290. verifyFormat("SomeClass::Constructor()\n"
  8291. " : a(a)\n"
  8292. " , b(b)\n"
  8293. " , c(c) {}",
  8294. Style);
  8295. verifyFormat("SomeClass::Constructor()\n"
  8296. " : a(a) {\n"
  8297. " foo();\n"
  8298. " bar();\n"
  8299. "}",
  8300. Style);
  8301. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  8302. verifyFormat("SomeClass::Constructor()\n"
  8303. " : a(a)\n"
  8304. " , b(b)\n"
  8305. " , c(c) {\n}",
  8306. Style);
  8307. verifyFormat("SomeClass::Constructor()\n"
  8308. " : a(a) {\n}",
  8309. Style);
  8310. Style.ColumnLimit = 80;
  8311. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
  8312. Style.ConstructorInitializerIndentWidth = 2;
  8313. verifyFormat("SomeClass::Constructor()\n"
  8314. " : a(a)\n"
  8315. " , b(b)\n"
  8316. " , c(c) {}",
  8317. Style);
  8318. Style.ConstructorInitializerIndentWidth = 0;
  8319. verifyFormat("SomeClass::Constructor()\n"
  8320. ": a(a)\n"
  8321. ", b(b)\n"
  8322. ", c(c) {}",
  8323. Style);
  8324. Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  8325. Style.ConstructorInitializerIndentWidth = 4;
  8326. verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
  8327. verifyFormat(
  8328. "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
  8329. Style);
  8330. verifyFormat(
  8331. "SomeClass::Constructor()\n"
  8332. " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
  8333. Style);
  8334. Style.ConstructorInitializerIndentWidth = 4;
  8335. Style.ColumnLimit = 60;
  8336. verifyFormat("SomeClass::Constructor()\n"
  8337. " : aaaaaaaa(aaaaaaaa)\n"
  8338. " , aaaaaaaa(aaaaaaaa)\n"
  8339. " , aaaaaaaa(aaaaaaaa) {}",
  8340. Style);
  8341. }
  8342. TEST_F(FormatTest, Destructors) {
  8343. verifyFormat("void F(int &i) { i.~int(); }");
  8344. verifyFormat("void F(int &i) { i->~int(); }");
  8345. }
  8346. TEST_F(FormatTest, FormatsWithWebKitStyle) {
  8347. FormatStyle Style = getWebKitStyle();
  8348. // Don't indent in outer namespaces.
  8349. verifyFormat("namespace outer {\n"
  8350. "int i;\n"
  8351. "namespace inner {\n"
  8352. " int i;\n"
  8353. "} // namespace inner\n"
  8354. "} // namespace outer\n"
  8355. "namespace other_outer {\n"
  8356. "int i;\n"
  8357. "}",
  8358. Style);
  8359. // Don't indent case labels.
  8360. verifyFormat("switch (variable) {\n"
  8361. "case 1:\n"
  8362. "case 2:\n"
  8363. " doSomething();\n"
  8364. " break;\n"
  8365. "default:\n"
  8366. " ++variable;\n"
  8367. "}",
  8368. Style);
  8369. // Wrap before binary operators.
  8370. EXPECT_EQ(
  8371. "void f()\n"
  8372. "{\n"
  8373. " if (aaaaaaaaaaaaaaaa\n"
  8374. " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
  8375. " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  8376. " return;\n"
  8377. "}",
  8378. format(
  8379. "void f() {\n"
  8380. "if (aaaaaaaaaaaaaaaa\n"
  8381. "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
  8382. "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  8383. "return;\n"
  8384. "}",
  8385. Style));
  8386. // Allow functions on a single line.
  8387. verifyFormat("void f() { return; }", Style);
  8388. // Constructor initializers are formatted one per line with the "," on the
  8389. // new line.
  8390. verifyFormat("Constructor()\n"
  8391. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  8392. " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
  8393. " aaaaaaaaaaaaaa)\n"
  8394. " , aaaaaaaaaaaaaaaaaaaaaaa()\n"
  8395. "{\n"
  8396. "}",
  8397. Style);
  8398. verifyFormat("SomeClass::Constructor()\n"
  8399. " : a(a)\n"
  8400. "{\n"
  8401. "}",
  8402. Style);
  8403. EXPECT_EQ("SomeClass::Constructor()\n"
  8404. " : a(a)\n"
  8405. "{\n"
  8406. "}",
  8407. format("SomeClass::Constructor():a(a){}", Style));
  8408. verifyFormat("SomeClass::Constructor()\n"
  8409. " : a(a)\n"
  8410. " , b(b)\n"
  8411. " , c(c)\n"
  8412. "{\n"
  8413. "}", Style);
  8414. verifyFormat("SomeClass::Constructor()\n"
  8415. " : a(a)\n"
  8416. "{\n"
  8417. " foo();\n"
  8418. " bar();\n"
  8419. "}",
  8420. Style);
  8421. // Access specifiers should be aligned left.
  8422. verifyFormat("class C {\n"
  8423. "public:\n"
  8424. " int i;\n"
  8425. "};",
  8426. Style);
  8427. // Do not align comments.
  8428. verifyFormat("int a; // Do not\n"
  8429. "double b; // align comments.",
  8430. Style);
  8431. // Accept input's line breaks.
  8432. EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
  8433. " || bbbbbbbbbbbbbbb) {\n"
  8434. " i++;\n"
  8435. "}",
  8436. format("if (aaaaaaaaaaaaaaa\n"
  8437. "|| bbbbbbbbbbbbbbb) { i++; }",
  8438. Style));
  8439. EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
  8440. " i++;\n"
  8441. "}",
  8442. format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
  8443. // Don't automatically break all macro definitions (llvm.org/PR17842).
  8444. verifyFormat("#define aNumber 10", Style);
  8445. // However, generally keep the line breaks that the user authored.
  8446. EXPECT_EQ("#define aNumber \\\n"
  8447. " 10",
  8448. format("#define aNumber \\\n"
  8449. " 10",
  8450. Style));
  8451. // Keep empty and one-element array literals on a single line.
  8452. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
  8453. " copyItems:YES];",
  8454. format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
  8455. "copyItems:YES];",
  8456. Style));
  8457. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
  8458. " copyItems:YES];",
  8459. format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
  8460. " copyItems:YES];",
  8461. Style));
  8462. // FIXME: This does not seem right, there should be more indentation before
  8463. // the array literal's entries. Nested blocks have the same problem.
  8464. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
  8465. " @\"a\",\n"
  8466. " @\"a\"\n"
  8467. "]\n"
  8468. " copyItems:YES];",
  8469. format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
  8470. " @\"a\",\n"
  8471. " @\"a\"\n"
  8472. " ]\n"
  8473. " copyItems:YES];",
  8474. Style));
  8475. EXPECT_EQ(
  8476. "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
  8477. " copyItems:YES];",
  8478. format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
  8479. " copyItems:YES];",
  8480. Style));
  8481. verifyFormat("[self.a b:c c:d];", Style);
  8482. EXPECT_EQ("[self.a b:c\n"
  8483. " c:d];",
  8484. format("[self.a b:c\n"
  8485. "c:d];",
  8486. Style));
  8487. }
  8488. TEST_F(FormatTest, FormatsLambdas) {
  8489. verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
  8490. verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
  8491. verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
  8492. verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
  8493. verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
  8494. verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
  8495. verifyFormat("void f() {\n"
  8496. " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
  8497. "}\n");
  8498. verifyFormat("void f() {\n"
  8499. " other(x.begin(), //\n"
  8500. " x.end(), //\n"
  8501. " [&](int, int) { return 1; });\n"
  8502. "}\n");
  8503. verifyFormat("SomeFunction([]() { // A cool function...\n"
  8504. " return 43;\n"
  8505. "});");
  8506. EXPECT_EQ("SomeFunction([]() {\n"
  8507. "#define A a\n"
  8508. " return 43;\n"
  8509. "});",
  8510. format("SomeFunction([](){\n"
  8511. "#define A a\n"
  8512. "return 43;\n"
  8513. "});"));
  8514. verifyFormat("void f() {\n"
  8515. " SomeFunction([](decltype(x), A *a) {});\n"
  8516. "}");
  8517. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  8518. " [](const aaaaaaaaaa &a) { return a; });");
  8519. verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
  8520. " SomeOtherFunctioooooooooooooooooooooooooon();\n"
  8521. "});");
  8522. // Lambdas with return types.
  8523. verifyFormat("int c = []() -> int { return 2; }();\n");
  8524. verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
  8525. verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
  8526. verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
  8527. " int j) -> int {\n"
  8528. " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
  8529. "};");
  8530. // Multiple lambdas in the same parentheses change indentation rules.
  8531. verifyFormat("SomeFunction([]() {\n"
  8532. " int i = 42;\n"
  8533. " return i;\n"
  8534. " },\n"
  8535. " []() {\n"
  8536. " int j = 43;\n"
  8537. " return j;\n"
  8538. " });");
  8539. // More complex introducers.
  8540. verifyFormat("return [i, args...] {};");
  8541. // Not lambdas.
  8542. verifyFormat("constexpr char hello[]{\"hello\"};");
  8543. verifyFormat("double &operator[](int i) { return 0; }\n"
  8544. "int i;");
  8545. verifyFormat("std::unique_ptr<int[]> foo() {}");
  8546. verifyFormat("int i = a[a][a]->f();");
  8547. verifyFormat("int i = (*b)[a]->f();");
  8548. // Other corner cases.
  8549. verifyFormat("void f() {\n"
  8550. " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
  8551. " );\n"
  8552. "}");
  8553. // Lambdas created through weird macros.
  8554. verifyFormat("void f() {\n"
  8555. " MACRO((const AA &a) { return 1; });\n"
  8556. "}");
  8557. }
  8558. TEST_F(FormatTest, FormatsBlocks) {
  8559. FormatStyle ShortBlocks = getLLVMStyle();
  8560. ShortBlocks.AllowShortBlocksOnASingleLine = true;
  8561. verifyFormat("int (^Block)(int, int);", ShortBlocks);
  8562. verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
  8563. verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
  8564. verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
  8565. verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
  8566. verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
  8567. verifyFormat("foo(^{ bar(); });", ShortBlocks);
  8568. verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
  8569. verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
  8570. verifyFormat("[operation setCompletionBlock:^{\n"
  8571. " [self onOperationDone];\n"
  8572. "}];");
  8573. verifyFormat("int i = {[operation setCompletionBlock:^{\n"
  8574. " [self onOperationDone];\n"
  8575. "}]};");
  8576. verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
  8577. " f();\n"
  8578. "}];");
  8579. verifyFormat("int a = [operation block:^int(int *i) {\n"
  8580. " return 1;\n"
  8581. "}];");
  8582. verifyFormat("[myObject doSomethingWith:arg1\n"
  8583. " aaa:^int(int *a) {\n"
  8584. " return 1;\n"
  8585. " }\n"
  8586. " bbb:f(a * bbbbbbbb)];");
  8587. verifyFormat("[operation setCompletionBlock:^{\n"
  8588. " [self.delegate newDataAvailable];\n"
  8589. "}];",
  8590. getLLVMStyleWithColumns(60));
  8591. verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
  8592. " NSString *path = [self sessionFilePath];\n"
  8593. " if (path) {\n"
  8594. " // ...\n"
  8595. " }\n"
  8596. "});");
  8597. verifyFormat("[[SessionService sharedService]\n"
  8598. " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  8599. " if (window) {\n"
  8600. " [self windowDidLoad:window];\n"
  8601. " } else {\n"
  8602. " [self errorLoadingWindow];\n"
  8603. " }\n"
  8604. " }];");
  8605. verifyFormat("void (^largeBlock)(void) = ^{\n"
  8606. " // ...\n"
  8607. "};\n",
  8608. getLLVMStyleWithColumns(40));
  8609. verifyFormat("[[SessionService sharedService]\n"
  8610. " loadWindowWithCompletionBlock: //\n"
  8611. " ^(SessionWindow *window) {\n"
  8612. " if (window) {\n"
  8613. " [self windowDidLoad:window];\n"
  8614. " } else {\n"
  8615. " [self errorLoadingWindow];\n"
  8616. " }\n"
  8617. " }];",
  8618. getLLVMStyleWithColumns(60));
  8619. verifyFormat("[myObject doSomethingWith:arg1\n"
  8620. " firstBlock:^(Foo *a) {\n"
  8621. " // ...\n"
  8622. " int i;\n"
  8623. " }\n"
  8624. " secondBlock:^(Bar *b) {\n"
  8625. " // ...\n"
  8626. " int i;\n"
  8627. " }\n"
  8628. " thirdBlock:^Foo(Bar *b) {\n"
  8629. " // ...\n"
  8630. " int i;\n"
  8631. " }];");
  8632. verifyFormat("[myObject doSomethingWith:arg1\n"
  8633. " firstBlock:-1\n"
  8634. " secondBlock:^(Bar *b) {\n"
  8635. " // ...\n"
  8636. " int i;\n"
  8637. " }];");
  8638. verifyFormat("f(^{\n"
  8639. " @autoreleasepool {\n"
  8640. " if (a) {\n"
  8641. " g();\n"
  8642. " }\n"
  8643. " }\n"
  8644. "});");
  8645. FormatStyle FourIndent = getLLVMStyle();
  8646. FourIndent.ObjCBlockIndentWidth = 4;
  8647. verifyFormat("[operation setCompletionBlock:^{\n"
  8648. " [self onOperationDone];\n"
  8649. "}];",
  8650. FourIndent);
  8651. }
  8652. TEST_F(FormatTest, SupportsCRLF) {
  8653. EXPECT_EQ("int a;\r\n"
  8654. "int b;\r\n"
  8655. "int c;\r\n",
  8656. format("int a;\r\n"
  8657. " int b;\r\n"
  8658. " int c;\r\n",
  8659. getLLVMStyle()));
  8660. EXPECT_EQ("int a;\r\n"
  8661. "int b;\r\n"
  8662. "int c;\r\n",
  8663. format("int a;\r\n"
  8664. " int b;\n"
  8665. " int c;\r\n",
  8666. getLLVMStyle()));
  8667. EXPECT_EQ("int a;\n"
  8668. "int b;\n"
  8669. "int c;\n",
  8670. format("int a;\r\n"
  8671. " int b;\n"
  8672. " int c;\n",
  8673. getLLVMStyle()));
  8674. EXPECT_EQ("\"aaaaaaa \"\r\n"
  8675. "\"bbbbbbb\";\r\n",
  8676. format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
  8677. EXPECT_EQ("#define A \\\r\n"
  8678. " b; \\\r\n"
  8679. " c; \\\r\n"
  8680. " d;\r\n",
  8681. format("#define A \\\r\n"
  8682. " b; \\\r\n"
  8683. " c; d; \r\n",
  8684. getGoogleStyle()));
  8685. EXPECT_EQ("/*\r\n"
  8686. "multi line block comments\r\n"
  8687. "should not introduce\r\n"
  8688. "an extra carriage return\r\n"
  8689. "*/\r\n",
  8690. format("/*\r\n"
  8691. "multi line block comments\r\n"
  8692. "should not introduce\r\n"
  8693. "an extra carriage return\r\n"
  8694. "*/\r\n"));
  8695. }
  8696. TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
  8697. verifyFormat("MY_CLASS(C) {\n"
  8698. " int i;\n"
  8699. " int j;\n"
  8700. "};");
  8701. }
  8702. TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
  8703. FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
  8704. TwoIndent.ContinuationIndentWidth = 2;
  8705. EXPECT_EQ("int i =\n"
  8706. " longFunction(\n"
  8707. " arg);",
  8708. format("int i = longFunction(arg);", TwoIndent));
  8709. FormatStyle SixIndent = getLLVMStyleWithColumns(20);
  8710. SixIndent.ContinuationIndentWidth = 6;
  8711. EXPECT_EQ("int i =\n"
  8712. " longFunction(\n"
  8713. " arg);",
  8714. format("int i = longFunction(arg);", SixIndent));
  8715. }
  8716. TEST_F(FormatTest, SpacesInAngles) {
  8717. FormatStyle Spaces = getLLVMStyle();
  8718. Spaces.SpacesInAngles = true;
  8719. verifyFormat("static_cast< int >(arg);", Spaces);
  8720. verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
  8721. verifyFormat("f< int, float >();", Spaces);
  8722. verifyFormat("template <> g() {}", Spaces);
  8723. verifyFormat("template < std::vector< int > > f() {}", Spaces);
  8724. Spaces.Standard = FormatStyle::LS_Cpp03;
  8725. Spaces.SpacesInAngles = true;
  8726. verifyFormat("A< A< int > >();", Spaces);
  8727. Spaces.SpacesInAngles = false;
  8728. verifyFormat("A<A<int> >();", Spaces);
  8729. Spaces.Standard = FormatStyle::LS_Cpp11;
  8730. Spaces.SpacesInAngles = true;
  8731. verifyFormat("A< A< int > >();", Spaces);
  8732. Spaces.SpacesInAngles = false;
  8733. verifyFormat("A<A<int>>();", Spaces);
  8734. }
  8735. TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
  8736. std::string code = "#if A\n"
  8737. "#if B\n"
  8738. "a.\n"
  8739. "#endif\n"
  8740. " a = 1;\n"
  8741. "#else\n"
  8742. "#endif\n"
  8743. "#if C\n"
  8744. "#else\n"
  8745. "#endif\n";
  8746. EXPECT_EQ(code, format(code));
  8747. }
  8748. TEST_F(FormatTest, HandleConflictMarkers) {
  8749. // Git/SVN conflict markers.
  8750. EXPECT_EQ("int a;\n"
  8751. "void f() {\n"
  8752. " callme(some(parameter1,\n"
  8753. "<<<<<<< text by the vcs\n"
  8754. " parameter2),\n"
  8755. "||||||| text by the vcs\n"
  8756. " parameter2),\n"
  8757. " parameter3,\n"
  8758. "======= text by the vcs\n"
  8759. " parameter2, parameter3),\n"
  8760. ">>>>>>> text by the vcs\n"
  8761. " otherparameter);\n",
  8762. format("int a;\n"
  8763. "void f() {\n"
  8764. " callme(some(parameter1,\n"
  8765. "<<<<<<< text by the vcs\n"
  8766. " parameter2),\n"
  8767. "||||||| text by the vcs\n"
  8768. " parameter2),\n"
  8769. " parameter3,\n"
  8770. "======= text by the vcs\n"
  8771. " parameter2,\n"
  8772. " parameter3),\n"
  8773. ">>>>>>> text by the vcs\n"
  8774. " otherparameter);\n"));
  8775. // Perforce markers.
  8776. EXPECT_EQ("void f() {\n"
  8777. " function(\n"
  8778. ">>>> text by the vcs\n"
  8779. " parameter,\n"
  8780. "==== text by the vcs\n"
  8781. " parameter,\n"
  8782. "==== text by the vcs\n"
  8783. " parameter,\n"
  8784. "<<<< text by the vcs\n"
  8785. " parameter);\n",
  8786. format("void f() {\n"
  8787. " function(\n"
  8788. ">>>> text by the vcs\n"
  8789. " parameter,\n"
  8790. "==== text by the vcs\n"
  8791. " parameter,\n"
  8792. "==== text by the vcs\n"
  8793. " parameter,\n"
  8794. "<<<< text by the vcs\n"
  8795. " parameter);\n"));
  8796. EXPECT_EQ("<<<<<<<\n"
  8797. "|||||||\n"
  8798. "=======\n"
  8799. ">>>>>>>",
  8800. format("<<<<<<<\n"
  8801. "|||||||\n"
  8802. "=======\n"
  8803. ">>>>>>>"));
  8804. EXPECT_EQ("<<<<<<<\n"
  8805. "|||||||\n"
  8806. "int i;\n"
  8807. "=======\n"
  8808. ">>>>>>>",
  8809. format("<<<<<<<\n"
  8810. "|||||||\n"
  8811. "int i;\n"
  8812. "=======\n"
  8813. ">>>>>>>"));
  8814. // FIXME: Handle parsing of macros around conflict markers correctly:
  8815. EXPECT_EQ("#define Macro \\\n"
  8816. "<<<<<<<\n"
  8817. "Something \\\n"
  8818. "|||||||\n"
  8819. "Else \\\n"
  8820. "=======\n"
  8821. "Other \\\n"
  8822. ">>>>>>>\n"
  8823. " End int i;\n",
  8824. format("#define Macro \\\n"
  8825. "<<<<<<<\n"
  8826. " Something \\\n"
  8827. "|||||||\n"
  8828. " Else \\\n"
  8829. "=======\n"
  8830. " Other \\\n"
  8831. ">>>>>>>\n"
  8832. " End\n"
  8833. "int i;\n"));
  8834. }
  8835. TEST_F(FormatTest, DisableRegions) {
  8836. EXPECT_EQ("int i;\n"
  8837. "// clang-format off\n"
  8838. " int j;\n"
  8839. "// clang-format on\n"
  8840. "int k;",
  8841. format(" int i;\n"
  8842. " // clang-format off\n"
  8843. " int j;\n"
  8844. " // clang-format on\n"
  8845. " int k;"));
  8846. EXPECT_EQ("int i;\n"
  8847. "/* clang-format off */\n"
  8848. " int j;\n"
  8849. "/* clang-format on */\n"
  8850. "int k;",
  8851. format(" int i;\n"
  8852. " /* clang-format off */\n"
  8853. " int j;\n"
  8854. " /* clang-format on */\n"
  8855. " int k;"));
  8856. }
  8857. TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
  8858. format("? ) =");
  8859. }
  8860. } // end namespace tooling
  8861. } // end namespace clang