FormatTest.cpp 368 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952
  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. /// \brief Verify that clang-format does not crash on the given input.
  59. void verifyNoCrash(llvm::StringRef Code,
  60. const FormatStyle &Style = getLLVMStyle()) {
  61. format(Code, Style);
  62. }
  63. int ReplacementCount;
  64. };
  65. TEST_F(FormatTest, MessUp) {
  66. EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
  67. EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
  68. EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
  69. EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
  70. EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
  71. }
  72. //===----------------------------------------------------------------------===//
  73. // Basic function tests.
  74. //===----------------------------------------------------------------------===//
  75. TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
  76. EXPECT_EQ(";", format(";"));
  77. }
  78. TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
  79. EXPECT_EQ("int i;", format(" int i;"));
  80. EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;"));
  81. EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
  82. EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
  83. }
  84. TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
  85. EXPECT_EQ("int i;", format("int\ni;"));
  86. }
  87. TEST_F(FormatTest, FormatsNestedBlockStatements) {
  88. EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
  89. }
  90. TEST_F(FormatTest, FormatsNestedCall) {
  91. verifyFormat("Method(f1, f2(f3));");
  92. verifyFormat("Method(f1(f2, f3()));");
  93. verifyFormat("Method(f1(f2, (f3())));");
  94. }
  95. TEST_F(FormatTest, NestedNameSpecifiers) {
  96. verifyFormat("vector<::Type> v;");
  97. verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
  98. verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
  99. verifyFormat("bool a = 2 < ::SomeFunction();");
  100. }
  101. TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
  102. EXPECT_EQ("if (a) {\n"
  103. " f();\n"
  104. "}",
  105. format("if(a){f();}"));
  106. EXPECT_EQ(4, ReplacementCount);
  107. EXPECT_EQ("if (a) {\n"
  108. " f();\n"
  109. "}",
  110. format("if (a) {\n"
  111. " f();\n"
  112. "}"));
  113. EXPECT_EQ(0, ReplacementCount);
  114. }
  115. TEST_F(FormatTest, RemovesTrailingWhitespaceOfFormattedLine) {
  116. EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0, getLLVMStyle()));
  117. EXPECT_EQ("int a;", format("int a; "));
  118. EXPECT_EQ("int a;\n", format("int a; \n \n \n "));
  119. EXPECT_EQ("int a;\nint b; ",
  120. format("int a; \nint b; ", 0, 0, getLLVMStyle()));
  121. }
  122. TEST_F(FormatTest, FormatsCorrectRegionForLeadingWhitespace) {
  123. EXPECT_EQ("int b;\nint a;",
  124. format("int b;\n int a;", 7, 0, getLLVMStyle()));
  125. EXPECT_EQ("int b;\n int a;",
  126. format("int b;\n int a;", 6, 0, getLLVMStyle()));
  127. EXPECT_EQ("#define A \\\n"
  128. " int a; \\\n"
  129. " int b;",
  130. format("#define A \\\n"
  131. " int a; \\\n"
  132. " int b;",
  133. 26, 0, getLLVMStyleWithColumns(12)));
  134. EXPECT_EQ("#define A \\\n"
  135. " int a; \\\n"
  136. " int b;",
  137. format("#define A \\\n"
  138. " int a; \\\n"
  139. " int b;",
  140. 25, 0, getLLVMStyleWithColumns(12)));
  141. }
  142. TEST_F(FormatTest, FormatLineWhenInvokedOnTrailingNewline) {
  143. EXPECT_EQ("int b;\n\nint a;",
  144. format("int b;\n\nint a;", 8, 0, getLLVMStyle()));
  145. EXPECT_EQ("int b;\n\nint a;",
  146. format("int b;\n\nint a;", 7, 0, getLLVMStyle()));
  147. // This might not strictly be correct, but is likely good in all practical
  148. // cases.
  149. EXPECT_EQ("int b;\nint a;",
  150. format("int b;int a;", 7, 0, getLLVMStyle()));
  151. }
  152. TEST_F(FormatTest, RemovesWhitespaceWhenTriggeredOnEmptyLine) {
  153. EXPECT_EQ("int a;\n\n int b;",
  154. format("int a;\n \n\n int b;", 8, 0, getLLVMStyle()));
  155. EXPECT_EQ("int a;\n\n int b;",
  156. format("int a;\n \n\n int b;", 9, 0, getLLVMStyle()));
  157. }
  158. TEST_F(FormatTest, RemovesEmptyLines) {
  159. EXPECT_EQ("class C {\n"
  160. " int i;\n"
  161. "};",
  162. format("class C {\n"
  163. " int i;\n"
  164. "\n"
  165. "};"));
  166. // Don't remove empty lines at the start of namespaces or extern "C" blocks.
  167. EXPECT_EQ("namespace N {\n"
  168. "\n"
  169. "int i;\n"
  170. "}",
  171. format("namespace N {\n"
  172. "\n"
  173. "int i;\n"
  174. "}",
  175. getGoogleStyle()));
  176. EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
  177. "\n"
  178. "int i;\n"
  179. "}",
  180. format("extern /**/ \"C\" /**/ {\n"
  181. "\n"
  182. "int i;\n"
  183. "}",
  184. getGoogleStyle()));
  185. // ...but do keep inlining and removing empty lines for non-block extern "C"
  186. // functions.
  187. verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
  188. EXPECT_EQ("extern \"C\" int f() {\n"
  189. " int i = 42;\n"
  190. " return i;\n"
  191. "}",
  192. format("extern \"C\" int f() {\n"
  193. "\n"
  194. " int i = 42;\n"
  195. " return i;\n"
  196. "}",
  197. getGoogleStyle()));
  198. // Remove empty lines at the beginning and end of blocks.
  199. EXPECT_EQ("void f() {\n"
  200. "\n"
  201. " if (a) {\n"
  202. "\n"
  203. " f();\n"
  204. " }\n"
  205. "}",
  206. format("void f() {\n"
  207. "\n"
  208. " if (a) {\n"
  209. "\n"
  210. " f();\n"
  211. "\n"
  212. " }\n"
  213. "\n"
  214. "}",
  215. getLLVMStyle()));
  216. EXPECT_EQ("void f() {\n"
  217. " if (a) {\n"
  218. " f();\n"
  219. " }\n"
  220. "}",
  221. format("void f() {\n"
  222. "\n"
  223. " if (a) {\n"
  224. "\n"
  225. " f();\n"
  226. "\n"
  227. " }\n"
  228. "\n"
  229. "}",
  230. getGoogleStyle()));
  231. // Don't remove empty lines in more complex control statements.
  232. EXPECT_EQ("void f() {\n"
  233. " if (a) {\n"
  234. " f();\n"
  235. "\n"
  236. " } else if (b) {\n"
  237. " f();\n"
  238. " }\n"
  239. "}",
  240. format("void f() {\n"
  241. " if (a) {\n"
  242. " f();\n"
  243. "\n"
  244. " } else if (b) {\n"
  245. " f();\n"
  246. "\n"
  247. " }\n"
  248. "\n"
  249. "}"));
  250. // FIXME: This is slightly inconsistent.
  251. EXPECT_EQ("namespace {\n"
  252. "int i;\n"
  253. "}",
  254. format("namespace {\n"
  255. "int i;\n"
  256. "\n"
  257. "}"));
  258. EXPECT_EQ("namespace {\n"
  259. "int i;\n"
  260. "\n"
  261. "} // namespace",
  262. format("namespace {\n"
  263. "int i;\n"
  264. "\n"
  265. "} // namespace"));
  266. }
  267. TEST_F(FormatTest, ReformatsMovedLines) {
  268. EXPECT_EQ(
  269. "template <typename T> T *getFETokenInfo() const {\n"
  270. " return static_cast<T *>(FETokenInfo);\n"
  271. "}\n"
  272. " int a; // <- Should not be formatted",
  273. format(
  274. "template<typename T>\n"
  275. "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
  276. " int a; // <- Should not be formatted",
  277. 9, 5, getLLVMStyle()));
  278. }
  279. TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
  280. verifyFormat("x = (a) and (b);");
  281. verifyFormat("x = (a) or (b);");
  282. verifyFormat("x = (a) bitand (b);");
  283. verifyFormat("x = (a) bitor (b);");
  284. verifyFormat("x = (a) not_eq (b);");
  285. verifyFormat("x = (a) and_eq (b);");
  286. verifyFormat("x = (a) or_eq (b);");
  287. verifyFormat("x = (a) xor (b);");
  288. }
  289. //===----------------------------------------------------------------------===//
  290. // Tests for control statements.
  291. //===----------------------------------------------------------------------===//
  292. TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
  293. verifyFormat("if (true)\n f();\ng();");
  294. verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
  295. verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
  296. FormatStyle AllowsMergedIf = getLLVMStyle();
  297. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  298. verifyFormat("if (a)\n"
  299. " // comment\n"
  300. " f();",
  301. AllowsMergedIf);
  302. verifyFormat("if (a)\n"
  303. " ;",
  304. AllowsMergedIf);
  305. verifyFormat("if (a)\n"
  306. " if (b) return;",
  307. AllowsMergedIf);
  308. verifyFormat("if (a) // Can't merge this\n"
  309. " f();\n",
  310. AllowsMergedIf);
  311. verifyFormat("if (a) /* still don't merge */\n"
  312. " f();",
  313. AllowsMergedIf);
  314. verifyFormat("if (a) { // Never merge this\n"
  315. " f();\n"
  316. "}",
  317. AllowsMergedIf);
  318. verifyFormat("if (a) {/* Never merge this */\n"
  319. " f();\n"
  320. "}",
  321. AllowsMergedIf);
  322. EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1, AllowsMergedIf));
  323. EXPECT_EQ("if (a) return; // comment",
  324. format("if(a)\nreturn; // comment", 20, 1, AllowsMergedIf));
  325. AllowsMergedIf.ColumnLimit = 14;
  326. verifyFormat("if (a) return;", AllowsMergedIf);
  327. verifyFormat("if (aaaaaaaaa)\n"
  328. " return;",
  329. AllowsMergedIf);
  330. AllowsMergedIf.ColumnLimit = 13;
  331. verifyFormat("if (a)\n return;", AllowsMergedIf);
  332. }
  333. TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
  334. FormatStyle AllowsMergedLoops = getLLVMStyle();
  335. AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
  336. verifyFormat("while (true) continue;", AllowsMergedLoops);
  337. verifyFormat("for (;;) continue;", AllowsMergedLoops);
  338. verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
  339. verifyFormat("while (true)\n"
  340. " ;",
  341. AllowsMergedLoops);
  342. verifyFormat("for (;;)\n"
  343. " ;",
  344. AllowsMergedLoops);
  345. verifyFormat("for (;;)\n"
  346. " for (;;) continue;",
  347. AllowsMergedLoops);
  348. verifyFormat("for (;;) // Can't merge this\n"
  349. " continue;",
  350. AllowsMergedLoops);
  351. verifyFormat("for (;;) /* still don't merge */\n"
  352. " continue;",
  353. AllowsMergedLoops);
  354. }
  355. TEST_F(FormatTest, FormatShortBracedStatements) {
  356. FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
  357. AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
  358. AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
  359. AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
  360. verifyFormat("if (true) {}", AllowSimpleBracedStatements);
  361. verifyFormat("while (true) {}", AllowSimpleBracedStatements);
  362. verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
  363. verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
  364. verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
  365. verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
  366. verifyFormat("if (true) { //\n"
  367. " f();\n"
  368. "}",
  369. AllowSimpleBracedStatements);
  370. verifyFormat("if (true) {\n"
  371. " f();\n"
  372. " f();\n"
  373. "}",
  374. AllowSimpleBracedStatements);
  375. verifyFormat("template <int> struct A2 {\n"
  376. " struct B {};\n"
  377. "};",
  378. AllowSimpleBracedStatements);
  379. AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
  380. verifyFormat("if (true) {\n"
  381. " f();\n"
  382. "}",
  383. AllowSimpleBracedStatements);
  384. AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
  385. verifyFormat("while (true) {\n"
  386. " f();\n"
  387. "}",
  388. AllowSimpleBracedStatements);
  389. verifyFormat("for (;;) {\n"
  390. " f();\n"
  391. "}",
  392. AllowSimpleBracedStatements);
  393. }
  394. TEST_F(FormatTest, ParseIfElse) {
  395. verifyFormat("if (true)\n"
  396. " if (true)\n"
  397. " if (true)\n"
  398. " f();\n"
  399. " else\n"
  400. " g();\n"
  401. " else\n"
  402. " h();\n"
  403. "else\n"
  404. " i();");
  405. verifyFormat("if (true)\n"
  406. " if (true)\n"
  407. " if (true) {\n"
  408. " if (true)\n"
  409. " f();\n"
  410. " } else {\n"
  411. " g();\n"
  412. " }\n"
  413. " else\n"
  414. " h();\n"
  415. "else {\n"
  416. " i();\n"
  417. "}");
  418. verifyFormat("void f() {\n"
  419. " if (a) {\n"
  420. " } else {\n"
  421. " }\n"
  422. "}");
  423. }
  424. TEST_F(FormatTest, ElseIf) {
  425. verifyFormat("if (a) {\n} else if (b) {\n}");
  426. verifyFormat("if (a)\n"
  427. " f();\n"
  428. "else if (b)\n"
  429. " g();\n"
  430. "else\n"
  431. " h();");
  432. verifyFormat("if (a) {\n"
  433. " f();\n"
  434. "}\n"
  435. "// or else ..\n"
  436. "else {\n"
  437. " g()\n"
  438. "}");
  439. verifyFormat("if (a) {\n"
  440. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  441. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  442. "}");
  443. verifyFormat("if (a) {\n"
  444. "} else if (\n"
  445. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  446. "}",
  447. getLLVMStyleWithColumns(62));
  448. }
  449. TEST_F(FormatTest, FormatsForLoop) {
  450. verifyFormat(
  451. "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
  452. " ++VeryVeryLongLoopVariable)\n"
  453. " ;");
  454. verifyFormat("for (;;)\n"
  455. " f();");
  456. verifyFormat("for (;;) {\n}");
  457. verifyFormat("for (;;) {\n"
  458. " f();\n"
  459. "}");
  460. verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
  461. verifyFormat(
  462. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  463. " E = UnwrappedLines.end();\n"
  464. " I != E; ++I) {\n}");
  465. verifyFormat(
  466. "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
  467. " ++IIIII) {\n}");
  468. verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
  469. " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
  470. " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
  471. verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
  472. " I = FD->getDeclsInPrototypeScope().begin(),\n"
  473. " E = FD->getDeclsInPrototypeScope().end();\n"
  474. " I != E; ++I) {\n}");
  475. // FIXME: Not sure whether we want extra identation in line 3 here:
  476. verifyFormat(
  477. "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  478. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
  479. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  480. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  481. " ++aaaaaaaaaaa) {\n}");
  482. verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
  483. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  484. "}");
  485. verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
  486. " aaaaaaaaaa);\n"
  487. " iter; ++iter) {\n"
  488. "}");
  489. FormatStyle NoBinPacking = getLLVMStyle();
  490. NoBinPacking.BinPackParameters = false;
  491. verifyFormat("for (int aaaaaaaaaaa = 1;\n"
  492. " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
  493. " aaaaaaaaaaaaaaaa,\n"
  494. " aaaaaaaaaaaaaaaa,\n"
  495. " aaaaaaaaaaaaaaaa);\n"
  496. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  497. "}",
  498. NoBinPacking);
  499. verifyFormat(
  500. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  501. " E = UnwrappedLines.end();\n"
  502. " I != E;\n"
  503. " ++I) {\n}",
  504. NoBinPacking);
  505. }
  506. TEST_F(FormatTest, RangeBasedForLoops) {
  507. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  508. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  509. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
  510. " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
  511. verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
  512. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  513. verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
  514. " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
  515. }
  516. TEST_F(FormatTest, ForEachLoops) {
  517. verifyFormat("void f() {\n"
  518. " foreach (Item *item, itemlist) {}\n"
  519. " Q_FOREACH (Item *item, itemlist) {}\n"
  520. " BOOST_FOREACH (Item *item, itemlist) {}\n"
  521. " UNKNOWN_FORACH(Item * item, itemlist) {}\n"
  522. "}");
  523. }
  524. TEST_F(FormatTest, FormatsWhileLoop) {
  525. verifyFormat("while (true) {\n}");
  526. verifyFormat("while (true)\n"
  527. " f();");
  528. verifyFormat("while () {\n}");
  529. verifyFormat("while () {\n"
  530. " f();\n"
  531. "}");
  532. }
  533. TEST_F(FormatTest, FormatsDoWhile) {
  534. verifyFormat("do {\n"
  535. " do_something();\n"
  536. "} while (something());");
  537. verifyFormat("do\n"
  538. " do_something();\n"
  539. "while (something());");
  540. }
  541. TEST_F(FormatTest, FormatsSwitchStatement) {
  542. verifyFormat("switch (x) {\n"
  543. "case 1:\n"
  544. " f();\n"
  545. " break;\n"
  546. "case kFoo:\n"
  547. "case ns::kBar:\n"
  548. "case kBaz:\n"
  549. " break;\n"
  550. "default:\n"
  551. " g();\n"
  552. " break;\n"
  553. "}");
  554. verifyFormat("switch (x) {\n"
  555. "case 1: {\n"
  556. " f();\n"
  557. " break;\n"
  558. "}\n"
  559. "case 2: {\n"
  560. " break;\n"
  561. "}\n"
  562. "}");
  563. verifyFormat("switch (x) {\n"
  564. "case 1: {\n"
  565. " f();\n"
  566. " {\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. " if (foo) {\n"
  577. " g();\n"
  578. " h();\n"
  579. " }\n"
  580. " break;\n"
  581. "}\n"
  582. "}");
  583. verifyFormat("switch (x) {\n"
  584. "case 1: {\n"
  585. " f();\n"
  586. " g();\n"
  587. "} break;\n"
  588. "}");
  589. verifyFormat("switch (test)\n"
  590. " ;");
  591. verifyFormat("switch (x) {\n"
  592. "default: {\n"
  593. " // Do nothing.\n"
  594. "}\n"
  595. "}");
  596. verifyFormat("switch (x) {\n"
  597. "// comment\n"
  598. "// if 1, do f()\n"
  599. "case 1:\n"
  600. " f();\n"
  601. "}");
  602. verifyFormat("switch (x) {\n"
  603. "case 1:\n"
  604. " // Do amazing stuff\n"
  605. " {\n"
  606. " f();\n"
  607. " g();\n"
  608. " }\n"
  609. " break;\n"
  610. "}");
  611. verifyFormat("#define A \\\n"
  612. " switch (x) { \\\n"
  613. " case a: \\\n"
  614. " foo = b; \\\n"
  615. " }", getLLVMStyleWithColumns(20));
  616. verifyFormat("#define OPERATION_CASE(name) \\\n"
  617. " case OP_name: \\\n"
  618. " return operations::Operation##name\n",
  619. getLLVMStyleWithColumns(40));
  620. verifyGoogleFormat("switch (x) {\n"
  621. " case 1:\n"
  622. " f();\n"
  623. " break;\n"
  624. " case kFoo:\n"
  625. " case ns::kBar:\n"
  626. " case kBaz:\n"
  627. " break;\n"
  628. " default:\n"
  629. " g();\n"
  630. " break;\n"
  631. "}");
  632. verifyGoogleFormat("switch (x) {\n"
  633. " case 1: {\n"
  634. " f();\n"
  635. " break;\n"
  636. " }\n"
  637. "}");
  638. verifyGoogleFormat("switch (test)\n"
  639. " ;");
  640. verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
  641. " case OP_name: \\\n"
  642. " return operations::Operation##name\n");
  643. verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
  644. " // Get the correction operation class.\n"
  645. " switch (OpCode) {\n"
  646. " CASE(Add);\n"
  647. " CASE(Subtract);\n"
  648. " default:\n"
  649. " return operations::Unknown;\n"
  650. " }\n"
  651. "#undef OPERATION_CASE\n"
  652. "}");
  653. verifyFormat("DEBUG({\n"
  654. " switch (x) {\n"
  655. " case A:\n"
  656. " f();\n"
  657. " break;\n"
  658. " // On B:\n"
  659. " case B:\n"
  660. " g();\n"
  661. " break;\n"
  662. " }\n"
  663. "});");
  664. verifyFormat("switch (a) {\n"
  665. "case (b):\n"
  666. " return;\n"
  667. "}");
  668. verifyFormat("switch (a) {\n"
  669. "case some_namespace::\n"
  670. " some_constant:\n"
  671. " return;\n"
  672. "}",
  673. getLLVMStyleWithColumns(34));
  674. }
  675. TEST_F(FormatTest, CaseRanges) {
  676. verifyFormat("switch (x) {\n"
  677. "case 'A' ... 'Z':\n"
  678. "case 1 ... 5:\n"
  679. " break;\n"
  680. "}");
  681. }
  682. TEST_F(FormatTest, ShortCaseLabels) {
  683. FormatStyle Style = getLLVMStyle();
  684. Style.AllowShortCaseLabelsOnASingleLine = true;
  685. verifyFormat("switch (a) {\n"
  686. "case 1: x = 1; break;\n"
  687. "case 2: return;\n"
  688. "case 3:\n"
  689. "case 4:\n"
  690. "case 5: return;\n"
  691. "case 6: // comment\n"
  692. " return;\n"
  693. "case 7:\n"
  694. " // comment\n"
  695. " return;\n"
  696. "default: y = 1; break;\n"
  697. "}",
  698. Style);
  699. verifyFormat("switch (a) {\n"
  700. "#if FOO\n"
  701. "case 0: return 0;\n"
  702. "#endif\n"
  703. "}",
  704. Style);
  705. verifyFormat("switch (a) {\n"
  706. "case 1: {\n"
  707. "}\n"
  708. "case 2: {\n"
  709. " return;\n"
  710. "}\n"
  711. "case 3: {\n"
  712. " x = 1;\n"
  713. " return;\n"
  714. "}\n"
  715. "case 4:\n"
  716. " if (x)\n"
  717. " return;\n"
  718. "}",
  719. Style);
  720. Style.ColumnLimit = 21;
  721. verifyFormat("switch (a) {\n"
  722. "case 1: x = 1; break;\n"
  723. "case 2: return;\n"
  724. "case 3:\n"
  725. "case 4:\n"
  726. "case 5: return;\n"
  727. "default:\n"
  728. " y = 1;\n"
  729. " break;\n"
  730. "}",
  731. Style);
  732. }
  733. TEST_F(FormatTest, FormatsLabels) {
  734. verifyFormat("void f() {\n"
  735. " some_code();\n"
  736. "test_label:\n"
  737. " some_other_code();\n"
  738. " {\n"
  739. " some_more_code();\n"
  740. " another_label:\n"
  741. " some_more_code();\n"
  742. " }\n"
  743. "}");
  744. verifyFormat("some_code();\n"
  745. "test_label:\n"
  746. "some_other_code();");
  747. }
  748. //===----------------------------------------------------------------------===//
  749. // Tests for comments.
  750. //===----------------------------------------------------------------------===//
  751. TEST_F(FormatTest, UnderstandsSingleLineComments) {
  752. verifyFormat("//* */");
  753. verifyFormat("// line 1\n"
  754. "// line 2\n"
  755. "void f() {}\n");
  756. verifyFormat("void f() {\n"
  757. " // Doesn't do anything\n"
  758. "}");
  759. verifyFormat("SomeObject\n"
  760. " // Calling someFunction on SomeObject\n"
  761. " .someFunction();");
  762. verifyFormat("auto result = SomeObject\n"
  763. " // Calling someFunction on SomeObject\n"
  764. " .someFunction();");
  765. verifyFormat("void f(int i, // some comment (probably for i)\n"
  766. " int j, // some comment (probably for j)\n"
  767. " int k); // some comment (probably for k)");
  768. verifyFormat("void f(int i,\n"
  769. " // some comment (probably for j)\n"
  770. " int j,\n"
  771. " // some comment (probably for k)\n"
  772. " int k);");
  773. verifyFormat("int i // This is a fancy variable\n"
  774. " = 5; // with nicely aligned comment.");
  775. verifyFormat("// Leading comment.\n"
  776. "int a; // Trailing comment.");
  777. verifyFormat("int a; // Trailing comment\n"
  778. " // on 2\n"
  779. " // or 3 lines.\n"
  780. "int b;");
  781. verifyFormat("int a; // Trailing comment\n"
  782. "\n"
  783. "// Leading comment.\n"
  784. "int b;");
  785. verifyFormat("int a; // Comment.\n"
  786. " // More details.\n"
  787. "int bbbb; // Another comment.");
  788. verifyFormat(
  789. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  790. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n"
  791. "int cccccccccccccccccccccccccccccc; // comment\n"
  792. "int ddd; // looooooooooooooooooooooooong comment\n"
  793. "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  794. "int bbbbbbbbbbbbbbbbbbbbb; // comment\n"
  795. "int ccccccccccccccccccc; // comment");
  796. verifyFormat("#include \"a\" // comment\n"
  797. "#include \"a/b/c\" // comment");
  798. verifyFormat("#include <a> // comment\n"
  799. "#include <a/b/c> // comment");
  800. EXPECT_EQ("#include \"a\" // comment\n"
  801. "#include \"a/b/c\" // comment",
  802. format("#include \\\n"
  803. " \"a\" // comment\n"
  804. "#include \"a/b/c\" // comment"));
  805. verifyFormat("enum E {\n"
  806. " // comment\n"
  807. " VAL_A, // comment\n"
  808. " VAL_B\n"
  809. "};");
  810. verifyFormat(
  811. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  812. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
  813. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  814. " // Comment inside a statement.\n"
  815. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  816. verifyFormat("SomeFunction(a,\n"
  817. " // comment\n"
  818. " b + x);");
  819. verifyFormat("SomeFunction(a, a,\n"
  820. " // comment\n"
  821. " b + x);");
  822. verifyFormat(
  823. "bool aaaaaaaaaaaaa = // comment\n"
  824. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  825. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  826. verifyFormat("int aaaa; // aaaaa\n"
  827. "int aa; // aaaaaaa",
  828. getLLVMStyleWithColumns(20));
  829. EXPECT_EQ("void f() { // This does something ..\n"
  830. "}\n"
  831. "int a; // This is unrelated",
  832. format("void f() { // This does something ..\n"
  833. " }\n"
  834. "int a; // This is unrelated"));
  835. EXPECT_EQ("class C {\n"
  836. " void f() { // This does something ..\n"
  837. " } // awesome..\n"
  838. "\n"
  839. " int a; // This is unrelated\n"
  840. "};",
  841. format("class C{void f() { // This does something ..\n"
  842. " } // awesome..\n"
  843. " \n"
  844. "int a; // This is unrelated\n"
  845. "};"));
  846. EXPECT_EQ("int i; // single line trailing comment",
  847. format("int i;\\\n// single line trailing comment"));
  848. verifyGoogleFormat("int a; // Trailing comment.");
  849. verifyFormat("someFunction(anotherFunction( // Force break.\n"
  850. " parameter));");
  851. verifyGoogleFormat("#endif // HEADER_GUARD");
  852. verifyFormat("const char *test[] = {\n"
  853. " // A\n"
  854. " \"aaaa\",\n"
  855. " // B\n"
  856. " \"aaaaa\"};");
  857. verifyGoogleFormat(
  858. "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  859. " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment");
  860. EXPECT_EQ("D(a, {\n"
  861. " // test\n"
  862. " int a;\n"
  863. "});",
  864. format("D(a, {\n"
  865. "// test\n"
  866. "int a;\n"
  867. "});"));
  868. EXPECT_EQ("lineWith(); // comment\n"
  869. "// at start\n"
  870. "otherLine();",
  871. format("lineWith(); // comment\n"
  872. "// at start\n"
  873. "otherLine();"));
  874. EXPECT_EQ("lineWith(); // comment\n"
  875. " // at start\n"
  876. "otherLine();",
  877. format("lineWith(); // comment\n"
  878. " // at start\n"
  879. "otherLine();"));
  880. EXPECT_EQ("lineWith(); // comment\n"
  881. "// at start\n"
  882. "otherLine(); // comment",
  883. format("lineWith(); // comment\n"
  884. "// at start\n"
  885. "otherLine(); // comment"));
  886. EXPECT_EQ("lineWith();\n"
  887. "// at start\n"
  888. "otherLine(); // comment",
  889. format("lineWith();\n"
  890. " // at start\n"
  891. "otherLine(); // comment"));
  892. EXPECT_EQ("// first\n"
  893. "// at start\n"
  894. "otherLine(); // comment",
  895. format("// first\n"
  896. " // at start\n"
  897. "otherLine(); // comment"));
  898. EXPECT_EQ("f();\n"
  899. "// first\n"
  900. "// at start\n"
  901. "otherLine(); // comment",
  902. format("f();\n"
  903. "// first\n"
  904. " // at start\n"
  905. "otherLine(); // comment"));
  906. verifyFormat("f(); // comment\n"
  907. "// first\n"
  908. "// at start\n"
  909. "otherLine();");
  910. EXPECT_EQ("f(); // comment\n"
  911. "// first\n"
  912. "// at start\n"
  913. "otherLine();",
  914. format("f(); // comment\n"
  915. "// first\n"
  916. " // at start\n"
  917. "otherLine();"));
  918. EXPECT_EQ("f(); // comment\n"
  919. " // first\n"
  920. "// at start\n"
  921. "otherLine();",
  922. format("f(); // comment\n"
  923. " // first\n"
  924. "// at start\n"
  925. "otherLine();"));
  926. EXPECT_EQ("void f() {\n"
  927. " lineWith(); // comment\n"
  928. " // at start\n"
  929. "}",
  930. format("void f() {\n"
  931. " lineWith(); // comment\n"
  932. " // at start\n"
  933. "}"));
  934. verifyFormat(
  935. "#define A \\\n"
  936. " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
  937. " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
  938. getLLVMStyleWithColumns(60));
  939. verifyFormat(
  940. "#define A \\\n"
  941. " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
  942. " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
  943. getLLVMStyleWithColumns(61));
  944. verifyFormat("if ( // This is some comment\n"
  945. " x + 3) {\n"
  946. "}");
  947. EXPECT_EQ("if ( // This is some comment\n"
  948. " // spanning two lines\n"
  949. " x + 3) {\n"
  950. "}",
  951. format("if( // This is some comment\n"
  952. " // spanning two lines\n"
  953. " x + 3) {\n"
  954. "}"));
  955. verifyNoCrash("/\\\n/");
  956. verifyNoCrash("/\\\n* */");
  957. }
  958. TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
  959. EXPECT_EQ("SomeFunction(a,\n"
  960. " b, // comment\n"
  961. " c);",
  962. format("SomeFunction(a,\n"
  963. " b, // comment\n"
  964. " c);"));
  965. EXPECT_EQ("SomeFunction(a, b,\n"
  966. " // comment\n"
  967. " c);",
  968. format("SomeFunction(a,\n"
  969. " b,\n"
  970. " // comment\n"
  971. " c);"));
  972. EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
  973. " c);",
  974. format("SomeFunction(a, b, // comment (unclear relation)\n"
  975. " c);"));
  976. EXPECT_EQ("SomeFunction(a, // comment\n"
  977. " b,\n"
  978. " c); // comment",
  979. format("SomeFunction(a, // comment\n"
  980. " b,\n"
  981. " c); // comment"));
  982. }
  983. TEST_F(FormatTest, CanFormatCommentsLocally) {
  984. EXPECT_EQ("int a; // comment\n"
  985. "int b; // comment",
  986. format("int a; // comment\n"
  987. "int b; // comment",
  988. 0, 0, getLLVMStyle()));
  989. EXPECT_EQ("int a; // comment\n"
  990. " // line 2\n"
  991. "int b;",
  992. format("int a; // comment\n"
  993. " // line 2\n"
  994. "int b;",
  995. 28, 0, getLLVMStyle()));
  996. EXPECT_EQ("int aaaaaa; // comment\n"
  997. "int b;\n"
  998. "int c; // unrelated comment",
  999. format("int aaaaaa; // comment\n"
  1000. "int b;\n"
  1001. "int c; // unrelated comment",
  1002. 31, 0, getLLVMStyle()));
  1003. EXPECT_EQ("int a; // This\n"
  1004. " // is\n"
  1005. " // a",
  1006. format("int a; // This\n"
  1007. " // is\n"
  1008. " // a",
  1009. 0, 0, getLLVMStyle()));
  1010. EXPECT_EQ("int a; // This\n"
  1011. " // is\n"
  1012. " // a\n"
  1013. "// This is b\n"
  1014. "int b;",
  1015. format("int a; // This\n"
  1016. " // is\n"
  1017. " // a\n"
  1018. "// This is b\n"
  1019. "int b;",
  1020. 0, 0, getLLVMStyle()));
  1021. EXPECT_EQ("int a; // This\n"
  1022. " // is\n"
  1023. " // a\n"
  1024. "\n"
  1025. " // This is unrelated",
  1026. format("int a; // This\n"
  1027. " // is\n"
  1028. " // a\n"
  1029. "\n"
  1030. " // This is unrelated",
  1031. 0, 0, getLLVMStyle()));
  1032. EXPECT_EQ("int a;\n"
  1033. "// This is\n"
  1034. "// not formatted. ",
  1035. format("int a;\n"
  1036. "// This is\n"
  1037. "// not formatted. ",
  1038. 0, 0, getLLVMStyle()));
  1039. }
  1040. TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
  1041. EXPECT_EQ("// comment", format("// comment "));
  1042. EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
  1043. format("int aaaaaaa, bbbbbbb; // comment ",
  1044. getLLVMStyleWithColumns(33)));
  1045. EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f "));
  1046. EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f "));
  1047. }
  1048. TEST_F(FormatTest, UnderstandsBlockComments) {
  1049. verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
  1050. verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }");
  1051. EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
  1052. " bbbbbbbbbbbbbbbbbbbbbbbbb);",
  1053. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n"
  1054. "/* Trailing comment for aa... */\n"
  1055. " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  1056. EXPECT_EQ(
  1057. "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  1058. " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
  1059. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
  1060. "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  1061. EXPECT_EQ(
  1062. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1063. " aaaaaaaaaaaaaaaaaa,\n"
  1064. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  1065. "}",
  1066. format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1067. " aaaaaaaaaaaaaaaaaa ,\n"
  1068. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  1069. "}"));
  1070. FormatStyle NoBinPacking = getLLVMStyle();
  1071. NoBinPacking.BinPackParameters = false;
  1072. verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
  1073. " /* parameter 2 */ aaaaaa,\n"
  1074. " /* parameter 3 */ aaaaaa,\n"
  1075. " /* parameter 4 */ aaaaaa);",
  1076. NoBinPacking);
  1077. // Aligning block comments in macros.
  1078. verifyGoogleFormat("#define A \\\n"
  1079. " int i; /*a*/ \\\n"
  1080. " int jjj; /*b*/");
  1081. }
  1082. TEST_F(FormatTest, AlignsBlockComments) {
  1083. EXPECT_EQ("/*\n"
  1084. " * Really multi-line\n"
  1085. " * comment.\n"
  1086. " */\n"
  1087. "void f() {}",
  1088. format(" /*\n"
  1089. " * Really multi-line\n"
  1090. " * comment.\n"
  1091. " */\n"
  1092. " void f() {}"));
  1093. EXPECT_EQ("class C {\n"
  1094. " /*\n"
  1095. " * Another multi-line\n"
  1096. " * comment.\n"
  1097. " */\n"
  1098. " void f() {}\n"
  1099. "};",
  1100. format("class C {\n"
  1101. "/*\n"
  1102. " * Another multi-line\n"
  1103. " * comment.\n"
  1104. " */\n"
  1105. "void f() {}\n"
  1106. "};"));
  1107. EXPECT_EQ("/*\n"
  1108. " 1. This is a comment with non-trivial formatting.\n"
  1109. " 1.1. We have to indent/outdent all lines equally\n"
  1110. " 1.1.1. to keep the formatting.\n"
  1111. " */",
  1112. format(" /*\n"
  1113. " 1. This is a comment with non-trivial formatting.\n"
  1114. " 1.1. We have to indent/outdent all lines equally\n"
  1115. " 1.1.1. to keep the formatting.\n"
  1116. " */"));
  1117. EXPECT_EQ("/*\n"
  1118. "Don't try to outdent if there's not enough indentation.\n"
  1119. "*/",
  1120. format(" /*\n"
  1121. " Don't try to outdent if there's not enough indentation.\n"
  1122. " */"));
  1123. EXPECT_EQ("int i; /* Comment with empty...\n"
  1124. " *\n"
  1125. " * line. */",
  1126. format("int i; /* Comment with empty...\n"
  1127. " *\n"
  1128. " * line. */"));
  1129. EXPECT_EQ("int foobar = 0; /* comment */\n"
  1130. "int bar = 0; /* multiline\n"
  1131. " comment 1 */\n"
  1132. "int baz = 0; /* multiline\n"
  1133. " comment 2 */\n"
  1134. "int bzz = 0; /* multiline\n"
  1135. " comment 3 */",
  1136. format("int foobar = 0; /* comment */\n"
  1137. "int bar = 0; /* multiline\n"
  1138. " comment 1 */\n"
  1139. "int baz = 0; /* multiline\n"
  1140. " comment 2 */\n"
  1141. "int bzz = 0; /* multiline\n"
  1142. " comment 3 */"));
  1143. EXPECT_EQ("int foobar = 0; /* comment */\n"
  1144. "int bar = 0; /* multiline\n"
  1145. " comment */\n"
  1146. "int baz = 0; /* multiline\n"
  1147. "comment */",
  1148. format("int foobar = 0; /* comment */\n"
  1149. "int bar = 0; /* multiline\n"
  1150. "comment */\n"
  1151. "int baz = 0; /* multiline\n"
  1152. "comment */"));
  1153. }
  1154. TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
  1155. EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1156. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
  1157. format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1158. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
  1159. EXPECT_EQ(
  1160. "void ffffffffffff(\n"
  1161. " int aaaaaaaa, int bbbbbbbb,\n"
  1162. " int cccccccccccc) { /*\n"
  1163. " aaaaaaaaaa\n"
  1164. " aaaaaaaaaaaaa\n"
  1165. " bbbbbbbbbbbbbb\n"
  1166. " bbbbbbbbbb\n"
  1167. " */\n"
  1168. "}",
  1169. format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
  1170. "{ /*\n"
  1171. " aaaaaaaaaa aaaaaaaaaaaaa\n"
  1172. " bbbbbbbbbbbbbb bbbbbbbbbb\n"
  1173. " */\n"
  1174. "}",
  1175. getLLVMStyleWithColumns(40)));
  1176. }
  1177. TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
  1178. EXPECT_EQ("void ffffffffff(\n"
  1179. " int aaaaa /* test */);",
  1180. format("void ffffffffff(int aaaaa /* test */);",
  1181. getLLVMStyleWithColumns(35)));
  1182. }
  1183. TEST_F(FormatTest, SplitsLongCxxComments) {
  1184. EXPECT_EQ("// A comment that\n"
  1185. "// doesn't fit on\n"
  1186. "// one line",
  1187. format("// A comment that doesn't fit on one line",
  1188. getLLVMStyleWithColumns(20)));
  1189. EXPECT_EQ("// a b c d\n"
  1190. "// e f g\n"
  1191. "// h i j k",
  1192. format("// a b c d e f g h i j k",
  1193. getLLVMStyleWithColumns(10)));
  1194. EXPECT_EQ("// a b c d\n"
  1195. "// e f g\n"
  1196. "// h i j k",
  1197. format("\\\n// a b c d e f g h i j k",
  1198. getLLVMStyleWithColumns(10)));
  1199. EXPECT_EQ("if (true) // A comment that\n"
  1200. " // doesn't fit on\n"
  1201. " // one line",
  1202. format("if (true) // A comment that doesn't fit on one line ",
  1203. getLLVMStyleWithColumns(30)));
  1204. EXPECT_EQ("// Don't_touch_leading_whitespace",
  1205. format("// Don't_touch_leading_whitespace",
  1206. getLLVMStyleWithColumns(20)));
  1207. EXPECT_EQ("// Add leading\n"
  1208. "// whitespace",
  1209. format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
  1210. EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
  1211. EXPECT_EQ("// Even if it makes the line exceed the column\n"
  1212. "// limit",
  1213. format("//Even if it makes the line exceed the column limit",
  1214. getLLVMStyleWithColumns(51)));
  1215. EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
  1216. EXPECT_EQ("// aa bb cc dd",
  1217. format("// aa bb cc dd ",
  1218. getLLVMStyleWithColumns(15)));
  1219. EXPECT_EQ("// A comment before\n"
  1220. "// a macro\n"
  1221. "// definition\n"
  1222. "#define a b",
  1223. format("// A comment before a macro definition\n"
  1224. "#define a b",
  1225. getLLVMStyleWithColumns(20)));
  1226. EXPECT_EQ("void ffffff(\n"
  1227. " int aaaaaaaaa, // wwww\n"
  1228. " int bbbbbbbbbb, // xxxxxxx\n"
  1229. " // yyyyyyyyyy\n"
  1230. " int c, int d, int e) {}",
  1231. format("void ffffff(\n"
  1232. " int aaaaaaaaa, // wwww\n"
  1233. " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
  1234. " int c, int d, int e) {}",
  1235. getLLVMStyleWithColumns(40)));
  1236. EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1237. format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1238. getLLVMStyleWithColumns(20)));
  1239. EXPECT_EQ(
  1240. "#define XXX // a b c d\n"
  1241. " // e f g h",
  1242. format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
  1243. EXPECT_EQ(
  1244. "#define XXX // q w e r\n"
  1245. " // t y u i",
  1246. format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
  1247. }
  1248. TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {
  1249. EXPECT_EQ("// A comment\n"
  1250. "// that doesn't\n"
  1251. "// fit on one\n"
  1252. "// line",
  1253. format("// A comment that doesn't fit on one line",
  1254. getLLVMStyleWithColumns(20)));
  1255. EXPECT_EQ("/// A comment\n"
  1256. "/// that doesn't\n"
  1257. "/// fit on one\n"
  1258. "/// line",
  1259. format("/// A comment that doesn't fit on one line",
  1260. getLLVMStyleWithColumns(20)));
  1261. }
  1262. TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
  1263. EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1264. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1265. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1266. format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1267. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1268. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
  1269. EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1270. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1271. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1272. format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1273. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1274. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1275. getLLVMStyleWithColumns(50)));
  1276. // FIXME: One day we might want to implement adjustment of leading whitespace
  1277. // of the consecutive lines in this kind of comment:
  1278. EXPECT_EQ("double\n"
  1279. " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1280. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1281. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1282. format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1283. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1284. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1285. getLLVMStyleWithColumns(49)));
  1286. }
  1287. TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) {
  1288. FormatStyle Pragmas = getLLVMStyleWithColumns(30);
  1289. Pragmas.CommentPragmas = "^ IWYU pragma:";
  1290. EXPECT_EQ(
  1291. "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
  1292. format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
  1293. EXPECT_EQ(
  1294. "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
  1295. format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
  1296. }
  1297. TEST_F(FormatTest, PriorityOfCommentBreaking) {
  1298. EXPECT_EQ("if (xxx ==\n"
  1299. " yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  1300. " zzz)\n"
  1301. " q();",
  1302. format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  1303. " zzz) q();",
  1304. getLLVMStyleWithColumns(40)));
  1305. EXPECT_EQ("if (xxxxxxxxxx ==\n"
  1306. " yyy && // aaaaaa bbbbbbbb cccc\n"
  1307. " zzz)\n"
  1308. " q();",
  1309. format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
  1310. " zzz) q();",
  1311. getLLVMStyleWithColumns(40)));
  1312. EXPECT_EQ("if (xxxxxxxxxx &&\n"
  1313. " yyy || // aaaaaa bbbbbbbb cccc\n"
  1314. " zzz)\n"
  1315. " q();",
  1316. format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
  1317. " zzz) q();",
  1318. getLLVMStyleWithColumns(40)));
  1319. EXPECT_EQ("fffffffff(\n"
  1320. " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  1321. " zzz);",
  1322. format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  1323. " zzz);",
  1324. getLLVMStyleWithColumns(40)));
  1325. }
  1326. TEST_F(FormatTest, MultiLineCommentsInDefines) {
  1327. EXPECT_EQ("#define A(x) /* \\\n"
  1328. " a comment \\\n"
  1329. " inside */ \\\n"
  1330. " f();",
  1331. format("#define A(x) /* \\\n"
  1332. " a comment \\\n"
  1333. " inside */ \\\n"
  1334. " f();",
  1335. getLLVMStyleWithColumns(17)));
  1336. EXPECT_EQ("#define A( \\\n"
  1337. " x) /* \\\n"
  1338. " a comment \\\n"
  1339. " inside */ \\\n"
  1340. " f();",
  1341. format("#define A( \\\n"
  1342. " x) /* \\\n"
  1343. " a comment \\\n"
  1344. " inside */ \\\n"
  1345. " f();",
  1346. getLLVMStyleWithColumns(17)));
  1347. }
  1348. TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
  1349. EXPECT_EQ("namespace {}\n// Test\n#define A",
  1350. format("namespace {}\n // Test\n#define A"));
  1351. EXPECT_EQ("namespace {}\n/* Test */\n#define A",
  1352. format("namespace {}\n /* Test */\n#define A"));
  1353. EXPECT_EQ("namespace {}\n/* Test */ #define A",
  1354. format("namespace {}\n /* Test */ #define A"));
  1355. }
  1356. TEST_F(FormatTest, SplitsLongLinesInComments) {
  1357. EXPECT_EQ("/* This is a long\n"
  1358. " * comment that\n"
  1359. " * doesn't\n"
  1360. " * fit on one line.\n"
  1361. " */",
  1362. format("/* "
  1363. "This is a long "
  1364. "comment that "
  1365. "doesn't "
  1366. "fit on one line. */",
  1367. getLLVMStyleWithColumns(20)));
  1368. EXPECT_EQ("/* a b c d\n"
  1369. " * e f g\n"
  1370. " * h i j k\n"
  1371. " */",
  1372. format("/* a b c d e f g h i j k */",
  1373. getLLVMStyleWithColumns(10)));
  1374. EXPECT_EQ("/* a b c d\n"
  1375. " * e f g\n"
  1376. " * h i j k\n"
  1377. " */",
  1378. format("\\\n/* a b c d e f g h i j k */",
  1379. getLLVMStyleWithColumns(10)));
  1380. EXPECT_EQ("/*\n"
  1381. "This is a long\n"
  1382. "comment that doesn't\n"
  1383. "fit on one line.\n"
  1384. "*/",
  1385. format("/*\n"
  1386. "This is a long "
  1387. "comment that doesn't "
  1388. "fit on one line. \n"
  1389. "*/", getLLVMStyleWithColumns(20)));
  1390. EXPECT_EQ("/*\n"
  1391. " * This is a long\n"
  1392. " * comment that\n"
  1393. " * doesn't fit on\n"
  1394. " * one line.\n"
  1395. " */",
  1396. format("/* \n"
  1397. " * This is a long "
  1398. " comment that "
  1399. " doesn't fit on "
  1400. " one line. \n"
  1401. " */", getLLVMStyleWithColumns(20)));
  1402. EXPECT_EQ("/*\n"
  1403. " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
  1404. " * so_it_should_be_broken\n"
  1405. " * wherever_a_space_occurs\n"
  1406. " */",
  1407. format("/*\n"
  1408. " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
  1409. " so_it_should_be_broken "
  1410. " wherever_a_space_occurs \n"
  1411. " */",
  1412. getLLVMStyleWithColumns(20)));
  1413. EXPECT_EQ("/*\n"
  1414. " * This_comment_can_not_be_broken_into_lines\n"
  1415. " */",
  1416. format("/*\n"
  1417. " * This_comment_can_not_be_broken_into_lines\n"
  1418. " */",
  1419. getLLVMStyleWithColumns(20)));
  1420. EXPECT_EQ("{\n"
  1421. " /*\n"
  1422. " This is another\n"
  1423. " long comment that\n"
  1424. " doesn't fit on one\n"
  1425. " line 1234567890\n"
  1426. " */\n"
  1427. "}",
  1428. format("{\n"
  1429. "/*\n"
  1430. "This is another "
  1431. " long comment that "
  1432. " doesn't fit on one"
  1433. " line 1234567890\n"
  1434. "*/\n"
  1435. "}", getLLVMStyleWithColumns(20)));
  1436. EXPECT_EQ("{\n"
  1437. " /*\n"
  1438. " * This i s\n"
  1439. " * another comment\n"
  1440. " * t hat doesn' t\n"
  1441. " * fit on one l i\n"
  1442. " * n e\n"
  1443. " */\n"
  1444. "}",
  1445. format("{\n"
  1446. "/*\n"
  1447. " * This i s"
  1448. " another comment"
  1449. " t hat doesn' t"
  1450. " fit on one l i"
  1451. " n e\n"
  1452. " */\n"
  1453. "}", getLLVMStyleWithColumns(20)));
  1454. EXPECT_EQ("/*\n"
  1455. " * This is a long\n"
  1456. " * comment that\n"
  1457. " * doesn't fit on\n"
  1458. " * one line\n"
  1459. " */",
  1460. format(" /*\n"
  1461. " * This is a long comment that doesn't fit on one line\n"
  1462. " */", getLLVMStyleWithColumns(20)));
  1463. EXPECT_EQ("{\n"
  1464. " if (something) /* This is a\n"
  1465. " long\n"
  1466. " comment */\n"
  1467. " ;\n"
  1468. "}",
  1469. format("{\n"
  1470. " if (something) /* This is a long comment */\n"
  1471. " ;\n"
  1472. "}",
  1473. getLLVMStyleWithColumns(30)));
  1474. EXPECT_EQ("/* A comment before\n"
  1475. " * a macro\n"
  1476. " * definition */\n"
  1477. "#define a b",
  1478. format("/* A comment before a macro definition */\n"
  1479. "#define a b",
  1480. getLLVMStyleWithColumns(20)));
  1481. EXPECT_EQ("/* some comment\n"
  1482. " * a comment\n"
  1483. "* that we break\n"
  1484. " * another comment\n"
  1485. "* we have to break\n"
  1486. "* a left comment\n"
  1487. " */",
  1488. format(" /* some comment\n"
  1489. " * a comment that we break\n"
  1490. " * another comment we have to break\n"
  1491. "* a left comment\n"
  1492. " */",
  1493. getLLVMStyleWithColumns(20)));
  1494. EXPECT_EQ("/*\n"
  1495. "\n"
  1496. "\n"
  1497. " */\n",
  1498. format(" /* \n"
  1499. " \n"
  1500. " \n"
  1501. " */\n"));
  1502. EXPECT_EQ("/* a a */",
  1503. format("/* a a */", getLLVMStyleWithColumns(15)));
  1504. EXPECT_EQ("/* a a bc */",
  1505. format("/* a a bc */", getLLVMStyleWithColumns(15)));
  1506. EXPECT_EQ("/* aaa aaa\n"
  1507. " * aaaaa */",
  1508. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1509. EXPECT_EQ("/* aaa aaa\n"
  1510. " * aaaaa */",
  1511. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1512. }
  1513. TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
  1514. EXPECT_EQ("#define X \\\n"
  1515. " /* \\\n"
  1516. " Test \\\n"
  1517. " Macro comment \\\n"
  1518. " with a long \\\n"
  1519. " line \\\n"
  1520. " */ \\\n"
  1521. " A + B",
  1522. format("#define X \\\n"
  1523. " /*\n"
  1524. " Test\n"
  1525. " Macro comment with a long line\n"
  1526. " */ \\\n"
  1527. " A + B",
  1528. getLLVMStyleWithColumns(20)));
  1529. EXPECT_EQ("#define X \\\n"
  1530. " /* Macro comment \\\n"
  1531. " with a long \\\n"
  1532. " line */ \\\n"
  1533. " A + B",
  1534. format("#define X \\\n"
  1535. " /* Macro comment with a long\n"
  1536. " line */ \\\n"
  1537. " A + B",
  1538. getLLVMStyleWithColumns(20)));
  1539. EXPECT_EQ("#define X \\\n"
  1540. " /* Macro comment \\\n"
  1541. " * with a long \\\n"
  1542. " * line */ \\\n"
  1543. " A + B",
  1544. format("#define X \\\n"
  1545. " /* Macro comment with a long line */ \\\n"
  1546. " A + B",
  1547. getLLVMStyleWithColumns(20)));
  1548. }
  1549. TEST_F(FormatTest, CommentsInStaticInitializers) {
  1550. EXPECT_EQ(
  1551. "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
  1552. " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
  1553. " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
  1554. " aaaaaaaaaaaaaaaaaaaa, // comment\n"
  1555. " aaaaaaaaaaaaaaaaaaaa};",
  1556. format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
  1557. " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
  1558. " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
  1559. " aaaaaaaaaaaaaaaaaaaa , // comment\n"
  1560. " aaaaaaaaaaaaaaaaaaaa };"));
  1561. verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1562. " bbbbbbbbbbb, ccccccccccc};");
  1563. verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1564. " // comment for bb....\n"
  1565. " bbbbbbbbbbb, ccccccccccc};");
  1566. verifyGoogleFormat(
  1567. "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1568. " bbbbbbbbbbb, ccccccccccc};");
  1569. verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1570. " // comment for bb....\n"
  1571. " bbbbbbbbbbb, ccccccccccc};");
  1572. verifyFormat("S s = {{a, b, c}, // Group #1\n"
  1573. " {d, e, f}, // Group #2\n"
  1574. " {g, h, i}}; // Group #3");
  1575. verifyFormat("S s = {{// Group #1\n"
  1576. " a, b, c},\n"
  1577. " {// Group #2\n"
  1578. " d, e, f},\n"
  1579. " {// Group #3\n"
  1580. " g, h, i}};");
  1581. EXPECT_EQ("S s = {\n"
  1582. " // Some comment\n"
  1583. " a,\n"
  1584. "\n"
  1585. " // Comment after empty line\n"
  1586. " b}",
  1587. format("S s = {\n"
  1588. " // Some comment\n"
  1589. " a,\n"
  1590. " \n"
  1591. " // Comment after empty line\n"
  1592. " b\n"
  1593. "}"));
  1594. EXPECT_EQ("S s = {\n"
  1595. " /* Some comment */\n"
  1596. " a,\n"
  1597. "\n"
  1598. " /* Comment after empty line */\n"
  1599. " b}",
  1600. format("S s = {\n"
  1601. " /* Some comment */\n"
  1602. " a,\n"
  1603. " \n"
  1604. " /* Comment after empty line */\n"
  1605. " b\n"
  1606. "}"));
  1607. verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
  1608. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1609. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1610. " 0x00, 0x00, 0x00, 0x00}; // comment\n");
  1611. }
  1612. TEST_F(FormatTest, IgnoresIf0Contents) {
  1613. EXPECT_EQ("#if 0\n"
  1614. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1615. "#endif\n"
  1616. "void f() {}",
  1617. format("#if 0\n"
  1618. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1619. "#endif\n"
  1620. "void f( ) { }"));
  1621. EXPECT_EQ("#if false\n"
  1622. "void f( ) { }\n"
  1623. "#endif\n"
  1624. "void g() {}\n",
  1625. format("#if false\n"
  1626. "void f( ) { }\n"
  1627. "#endif\n"
  1628. "void g( ) { }\n"));
  1629. EXPECT_EQ("enum E {\n"
  1630. " One,\n"
  1631. " Two,\n"
  1632. "#if 0\n"
  1633. "Three,\n"
  1634. " Four,\n"
  1635. "#endif\n"
  1636. " Five\n"
  1637. "};",
  1638. format("enum E {\n"
  1639. " One,Two,\n"
  1640. "#if 0\n"
  1641. "Three,\n"
  1642. " Four,\n"
  1643. "#endif\n"
  1644. " Five};"));
  1645. EXPECT_EQ("enum F {\n"
  1646. " One,\n"
  1647. "#if 1\n"
  1648. " Two,\n"
  1649. "#if 0\n"
  1650. "Three,\n"
  1651. " Four,\n"
  1652. "#endif\n"
  1653. " Five\n"
  1654. "#endif\n"
  1655. "};",
  1656. format("enum F {\n"
  1657. "One,\n"
  1658. "#if 1\n"
  1659. "Two,\n"
  1660. "#if 0\n"
  1661. "Three,\n"
  1662. " Four,\n"
  1663. "#endif\n"
  1664. "Five\n"
  1665. "#endif\n"
  1666. "};"));
  1667. EXPECT_EQ("enum G {\n"
  1668. " One,\n"
  1669. "#if 0\n"
  1670. "Two,\n"
  1671. "#else\n"
  1672. " Three,\n"
  1673. "#endif\n"
  1674. " Four\n"
  1675. "};",
  1676. format("enum G {\n"
  1677. "One,\n"
  1678. "#if 0\n"
  1679. "Two,\n"
  1680. "#else\n"
  1681. "Three,\n"
  1682. "#endif\n"
  1683. "Four\n"
  1684. "};"));
  1685. EXPECT_EQ("enum H {\n"
  1686. " One,\n"
  1687. "#if 0\n"
  1688. "#ifdef Q\n"
  1689. "Two,\n"
  1690. "#else\n"
  1691. "Three,\n"
  1692. "#endif\n"
  1693. "#endif\n"
  1694. " Four\n"
  1695. "};",
  1696. format("enum H {\n"
  1697. "One,\n"
  1698. "#if 0\n"
  1699. "#ifdef Q\n"
  1700. "Two,\n"
  1701. "#else\n"
  1702. "Three,\n"
  1703. "#endif\n"
  1704. "#endif\n"
  1705. "Four\n"
  1706. "};"));
  1707. EXPECT_EQ("enum I {\n"
  1708. " One,\n"
  1709. "#if /* test */ 0 || 1\n"
  1710. "Two,\n"
  1711. "Three,\n"
  1712. "#endif\n"
  1713. " Four\n"
  1714. "};",
  1715. format("enum I {\n"
  1716. "One,\n"
  1717. "#if /* test */ 0 || 1\n"
  1718. "Two,\n"
  1719. "Three,\n"
  1720. "#endif\n"
  1721. "Four\n"
  1722. "};"));
  1723. EXPECT_EQ("enum J {\n"
  1724. " One,\n"
  1725. "#if 0\n"
  1726. "#if 0\n"
  1727. "Two,\n"
  1728. "#else\n"
  1729. "Three,\n"
  1730. "#endif\n"
  1731. "Four,\n"
  1732. "#endif\n"
  1733. " Five\n"
  1734. "};",
  1735. format("enum J {\n"
  1736. "One,\n"
  1737. "#if 0\n"
  1738. "#if 0\n"
  1739. "Two,\n"
  1740. "#else\n"
  1741. "Three,\n"
  1742. "#endif\n"
  1743. "Four,\n"
  1744. "#endif\n"
  1745. "Five\n"
  1746. "};"));
  1747. }
  1748. //===----------------------------------------------------------------------===//
  1749. // Tests for classes, namespaces, etc.
  1750. //===----------------------------------------------------------------------===//
  1751. TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
  1752. verifyFormat("class A {};");
  1753. }
  1754. TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
  1755. verifyFormat("class A {\n"
  1756. "public:\n"
  1757. "public: // comment\n"
  1758. "protected:\n"
  1759. "private:\n"
  1760. " void f() {}\n"
  1761. "};");
  1762. verifyGoogleFormat("class A {\n"
  1763. " public:\n"
  1764. " protected:\n"
  1765. " private:\n"
  1766. " void f() {}\n"
  1767. "};");
  1768. verifyFormat("class A {\n"
  1769. "public slots:\n"
  1770. " void f() {}\n"
  1771. "public Q_SLOTS:\n"
  1772. " void f() {}\n"
  1773. "};");
  1774. }
  1775. TEST_F(FormatTest, SeparatesLogicalBlocks) {
  1776. EXPECT_EQ("class A {\n"
  1777. "public:\n"
  1778. " void f();\n"
  1779. "\n"
  1780. "private:\n"
  1781. " void g() {}\n"
  1782. " // test\n"
  1783. "protected:\n"
  1784. " int h;\n"
  1785. "};",
  1786. format("class A {\n"
  1787. "public:\n"
  1788. "void f();\n"
  1789. "private:\n"
  1790. "void g() {}\n"
  1791. "// test\n"
  1792. "protected:\n"
  1793. "int h;\n"
  1794. "};"));
  1795. EXPECT_EQ("class A {\n"
  1796. "protected:\n"
  1797. "public:\n"
  1798. " void f();\n"
  1799. "};",
  1800. format("class A {\n"
  1801. "protected:\n"
  1802. "\n"
  1803. "public:\n"
  1804. "\n"
  1805. " void f();\n"
  1806. "};"));
  1807. }
  1808. TEST_F(FormatTest, FormatsClasses) {
  1809. verifyFormat("class A : public B {};");
  1810. verifyFormat("class A : public ::B {};");
  1811. verifyFormat(
  1812. "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1813. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1814. verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
  1815. " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1816. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1817. verifyFormat(
  1818. "class A : public B, public C, public D, public E, public F {};");
  1819. verifyFormat("class AAAAAAAAAAAA : public B,\n"
  1820. " public C,\n"
  1821. " public D,\n"
  1822. " public E,\n"
  1823. " public F,\n"
  1824. " public G {};");
  1825. verifyFormat("class\n"
  1826. " ReallyReallyLongClassName {\n"
  1827. " int i;\n"
  1828. "};",
  1829. getLLVMStyleWithColumns(32));
  1830. verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
  1831. " aaaaaaaaaaaaaaaa> {};");
  1832. verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
  1833. " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
  1834. " aaaaaaaaaaaaaaaaaaaaaa> {};");
  1835. verifyFormat("template <class R, class C>\n"
  1836. "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
  1837. " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
  1838. verifyFormat("class ::A::B {};");
  1839. }
  1840. TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
  1841. verifyFormat("class A {\n} a, b;");
  1842. verifyFormat("struct A {\n} a, b;");
  1843. verifyFormat("union A {\n} a;");
  1844. }
  1845. TEST_F(FormatTest, FormatsEnum) {
  1846. verifyFormat("enum {\n"
  1847. " Zero,\n"
  1848. " One = 1,\n"
  1849. " Two = One + 1,\n"
  1850. " Three = (One + Two),\n"
  1851. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1852. " Five = (One, Two, Three, Four, 5)\n"
  1853. "};");
  1854. verifyGoogleFormat("enum {\n"
  1855. " Zero,\n"
  1856. " One = 1,\n"
  1857. " Two = One + 1,\n"
  1858. " Three = (One + Two),\n"
  1859. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1860. " Five = (One, Two, Three, Four, 5)\n"
  1861. "};");
  1862. verifyFormat("enum Enum {};");
  1863. verifyFormat("enum {};");
  1864. verifyFormat("enum X E {} d;");
  1865. verifyFormat("enum __attribute__((...)) E {} d;");
  1866. verifyFormat("enum __declspec__((...)) E {} d;");
  1867. verifyFormat("enum X f() {\n a();\n return 42;\n}");
  1868. verifyFormat("enum {\n"
  1869. " Bar = Foo<int, int>::value\n"
  1870. "};",
  1871. getLLVMStyleWithColumns(30));
  1872. verifyFormat("enum ShortEnum { A, B, C };");
  1873. verifyGoogleFormat("enum ShortEnum { A, B, C };");
  1874. EXPECT_EQ("enum KeepEmptyLines {\n"
  1875. " ONE,\n"
  1876. "\n"
  1877. " TWO,\n"
  1878. "\n"
  1879. " THREE\n"
  1880. "}",
  1881. format("enum KeepEmptyLines {\n"
  1882. " ONE,\n"
  1883. "\n"
  1884. " TWO,\n"
  1885. "\n"
  1886. "\n"
  1887. " THREE\n"
  1888. "}"));
  1889. verifyFormat("enum E { // comment\n"
  1890. " ONE,\n"
  1891. " TWO\n"
  1892. "};\n"
  1893. "int i;");
  1894. }
  1895. TEST_F(FormatTest, FormatsEnumsWithErrors) {
  1896. verifyFormat("enum Type {\n"
  1897. " One = 0; // These semicolons should be commas.\n"
  1898. " Two = 1;\n"
  1899. "};");
  1900. verifyFormat("namespace n {\n"
  1901. "enum Type {\n"
  1902. " One,\n"
  1903. " Two, // missing };\n"
  1904. " int i;\n"
  1905. "}\n"
  1906. "void g() {}");
  1907. }
  1908. TEST_F(FormatTest, FormatsEnumStruct) {
  1909. verifyFormat("enum struct {\n"
  1910. " Zero,\n"
  1911. " One = 1,\n"
  1912. " Two = One + 1,\n"
  1913. " Three = (One + Two),\n"
  1914. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1915. " Five = (One, Two, Three, Four, 5)\n"
  1916. "};");
  1917. verifyFormat("enum struct Enum {};");
  1918. verifyFormat("enum struct {};");
  1919. verifyFormat("enum struct X E {} d;");
  1920. verifyFormat("enum struct __attribute__((...)) E {} d;");
  1921. verifyFormat("enum struct __declspec__((...)) E {} d;");
  1922. verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
  1923. }
  1924. TEST_F(FormatTest, FormatsEnumClass) {
  1925. verifyFormat("enum class {\n"
  1926. " Zero,\n"
  1927. " One = 1,\n"
  1928. " Two = One + 1,\n"
  1929. " Three = (One + Two),\n"
  1930. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1931. " Five = (One, Two, Three, Four, 5)\n"
  1932. "};");
  1933. verifyFormat("enum class Enum {};");
  1934. verifyFormat("enum class {};");
  1935. verifyFormat("enum class X E {} d;");
  1936. verifyFormat("enum class __attribute__((...)) E {} d;");
  1937. verifyFormat("enum class __declspec__((...)) E {} d;");
  1938. verifyFormat("enum class X f() {\n a();\n return 42;\n}");
  1939. }
  1940. TEST_F(FormatTest, FormatsEnumTypes) {
  1941. verifyFormat("enum X : int {\n"
  1942. " A, // Force multiple lines.\n"
  1943. " B\n"
  1944. "};");
  1945. verifyFormat("enum X : int { A, B };");
  1946. verifyFormat("enum X : std::uint32_t { A, B };");
  1947. }
  1948. TEST_F(FormatTest, FormatsNSEnums) {
  1949. verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
  1950. verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
  1951. " // Information about someDecentlyLongValue.\n"
  1952. " someDecentlyLongValue,\n"
  1953. " // Information about anotherDecentlyLongValue.\n"
  1954. " anotherDecentlyLongValue,\n"
  1955. " // Information about aThirdDecentlyLongValue.\n"
  1956. " aThirdDecentlyLongValue\n"
  1957. "};");
  1958. verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
  1959. " a = 1,\n"
  1960. " b = 2,\n"
  1961. " c = 3,\n"
  1962. "};");
  1963. verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
  1964. " a = 1,\n"
  1965. " b = 2,\n"
  1966. " c = 3,\n"
  1967. "};");
  1968. verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
  1969. " a = 1,\n"
  1970. " b = 2,\n"
  1971. " c = 3,\n"
  1972. "};");
  1973. }
  1974. TEST_F(FormatTest, FormatsBitfields) {
  1975. verifyFormat("struct Bitfields {\n"
  1976. " unsigned sClass : 8;\n"
  1977. " unsigned ValueKind : 2;\n"
  1978. "};");
  1979. verifyFormat("struct A {\n"
  1980. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
  1981. " bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
  1982. "};");
  1983. }
  1984. TEST_F(FormatTest, FormatsNamespaces) {
  1985. verifyFormat("namespace some_namespace {\n"
  1986. "class A {};\n"
  1987. "void f() { f(); }\n"
  1988. "}");
  1989. verifyFormat("namespace {\n"
  1990. "class A {};\n"
  1991. "void f() { f(); }\n"
  1992. "}");
  1993. verifyFormat("inline namespace X {\n"
  1994. "class A {};\n"
  1995. "void f() { f(); }\n"
  1996. "}");
  1997. verifyFormat("using namespace some_namespace;\n"
  1998. "class A {};\n"
  1999. "void f() { f(); }");
  2000. // This code is more common than we thought; if we
  2001. // layout this correctly the semicolon will go into
  2002. // its own line, which is undesirable.
  2003. verifyFormat("namespace {};");
  2004. verifyFormat("namespace {\n"
  2005. "class A {};\n"
  2006. "};");
  2007. verifyFormat("namespace {\n"
  2008. "int SomeVariable = 0; // comment\n"
  2009. "} // namespace");
  2010. EXPECT_EQ("#ifndef HEADER_GUARD\n"
  2011. "#define HEADER_GUARD\n"
  2012. "namespace my_namespace {\n"
  2013. "int i;\n"
  2014. "} // my_namespace\n"
  2015. "#endif // HEADER_GUARD",
  2016. format("#ifndef HEADER_GUARD\n"
  2017. " #define HEADER_GUARD\n"
  2018. " namespace my_namespace {\n"
  2019. "int i;\n"
  2020. "} // my_namespace\n"
  2021. "#endif // HEADER_GUARD"));
  2022. FormatStyle Style = getLLVMStyle();
  2023. Style.NamespaceIndentation = FormatStyle::NI_All;
  2024. EXPECT_EQ("namespace out {\n"
  2025. " int i;\n"
  2026. " namespace in {\n"
  2027. " int i;\n"
  2028. " } // namespace\n"
  2029. "} // namespace",
  2030. format("namespace out {\n"
  2031. "int i;\n"
  2032. "namespace in {\n"
  2033. "int i;\n"
  2034. "} // namespace\n"
  2035. "} // namespace",
  2036. Style));
  2037. Style.NamespaceIndentation = FormatStyle::NI_Inner;
  2038. EXPECT_EQ("namespace out {\n"
  2039. "int i;\n"
  2040. "namespace in {\n"
  2041. " int i;\n"
  2042. "} // namespace\n"
  2043. "} // namespace",
  2044. format("namespace out {\n"
  2045. "int i;\n"
  2046. "namespace in {\n"
  2047. "int i;\n"
  2048. "} // namespace\n"
  2049. "} // namespace",
  2050. Style));
  2051. }
  2052. TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
  2053. TEST_F(FormatTest, FormatsInlineASM) {
  2054. verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
  2055. verifyFormat("asm(\"nop\" ::: \"memory\");");
  2056. verifyFormat(
  2057. "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
  2058. " \"cpuid\\n\\t\"\n"
  2059. " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
  2060. " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
  2061. " : \"a\"(value));");
  2062. EXPECT_EQ(
  2063. "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
  2064. " __asm {\n"
  2065. " mov edx,[that] // vtable in edx\n"
  2066. " mov eax,methodIndex\n"
  2067. " call [edx][eax*4] // stdcall\n"
  2068. " }\n"
  2069. "}",
  2070. format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
  2071. " __asm {\n"
  2072. " mov edx,[that] // vtable in edx\n"
  2073. " mov eax,methodIndex\n"
  2074. " call [edx][eax*4] // stdcall\n"
  2075. " }\n"
  2076. "}"));
  2077. verifyFormat("void function() {\n"
  2078. " // comment\n"
  2079. " asm(\"\");\n"
  2080. "}");
  2081. }
  2082. TEST_F(FormatTest, FormatTryCatch) {
  2083. verifyFormat("try {\n"
  2084. " throw a * b;\n"
  2085. "} catch (int a) {\n"
  2086. " // Do nothing.\n"
  2087. "} catch (...) {\n"
  2088. " exit(42);\n"
  2089. "}");
  2090. // Function-level try statements.
  2091. verifyFormat("int f() try { return 4; } catch (...) {\n"
  2092. " return 5;\n"
  2093. "}");
  2094. verifyFormat("class A {\n"
  2095. " int a;\n"
  2096. " A() try : a(0) {\n"
  2097. " } catch (...) {\n"
  2098. " throw;\n"
  2099. " }\n"
  2100. "};\n");
  2101. // Incomplete try-catch blocks.
  2102. verifyFormat("try {} catch (");
  2103. }
  2104. TEST_F(FormatTest, FormatSEHTryCatch) {
  2105. verifyFormat("__try {\n"
  2106. " int a = b * c;\n"
  2107. "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
  2108. " // Do nothing.\n"
  2109. "}");
  2110. verifyFormat("__try {\n"
  2111. " int a = b * c;\n"
  2112. "} __finally {\n"
  2113. " // Do nothing.\n"
  2114. "}");
  2115. verifyFormat("DEBUG({\n"
  2116. " __try {\n"
  2117. " } __finally {\n"
  2118. " }\n"
  2119. "});\n");
  2120. }
  2121. TEST_F(FormatTest, IncompleteTryCatchBlocks) {
  2122. verifyFormat("try {\n"
  2123. " f();\n"
  2124. "} catch {\n"
  2125. " g();\n"
  2126. "}");
  2127. verifyFormat("try {\n"
  2128. " f();\n"
  2129. "} catch (A a) MACRO(x) {\n"
  2130. " g();\n"
  2131. "} catch (B b) MACRO(x) {\n"
  2132. " g();\n"
  2133. "}");
  2134. }
  2135. TEST_F(FormatTest, FormatTryCatchBraceStyles) {
  2136. FormatStyle Style = getLLVMStyle();
  2137. Style.BreakBeforeBraces = FormatStyle::BS_Attach;
  2138. verifyFormat("try {\n"
  2139. " // something\n"
  2140. "} catch (...) {\n"
  2141. " // something\n"
  2142. "}",
  2143. Style);
  2144. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  2145. verifyFormat("try {\n"
  2146. " // something\n"
  2147. "}\n"
  2148. "catch (...) {\n"
  2149. " // something\n"
  2150. "}",
  2151. Style);
  2152. verifyFormat("__try {\n"
  2153. " // something\n"
  2154. "}\n"
  2155. "__finally {\n"
  2156. " // something\n"
  2157. "}",
  2158. Style);
  2159. verifyFormat("@try {\n"
  2160. " // something\n"
  2161. "}\n"
  2162. "@finally {\n"
  2163. " // something\n"
  2164. "}",
  2165. Style);
  2166. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  2167. verifyFormat("try\n"
  2168. "{\n"
  2169. " // something\n"
  2170. "}\n"
  2171. "catch (...)\n"
  2172. "{\n"
  2173. " // something\n"
  2174. "}",
  2175. Style);
  2176. Style.BreakBeforeBraces = FormatStyle::BS_GNU;
  2177. verifyFormat("try\n"
  2178. " {\n"
  2179. " // something\n"
  2180. " }\n"
  2181. "catch (...)\n"
  2182. " {\n"
  2183. " // something\n"
  2184. " }",
  2185. Style);
  2186. }
  2187. TEST_F(FormatTest, FormatObjCTryCatch) {
  2188. verifyFormat("@try {\n"
  2189. " f();\n"
  2190. "} @catch (NSException e) {\n"
  2191. " @throw;\n"
  2192. "} @finally {\n"
  2193. " exit(42);\n"
  2194. "}");
  2195. verifyFormat("DEBUG({\n"
  2196. " @try {\n"
  2197. " } @finally {\n"
  2198. " }\n"
  2199. "});\n");
  2200. }
  2201. TEST_F(FormatTest, StaticInitializers) {
  2202. verifyFormat("static SomeClass SC = {1, 'a'};");
  2203. verifyFormat(
  2204. "static SomeClass WithALoooooooooooooooooooongName = {\n"
  2205. " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
  2206. // Here, everything other than the "}" would fit on a line.
  2207. verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
  2208. " 10000000000000000000000000};");
  2209. EXPECT_EQ("S s = {a,\n"
  2210. "\n"
  2211. " b};",
  2212. format("S s = {\n"
  2213. " a,\n"
  2214. "\n"
  2215. " b\n"
  2216. "};"));
  2217. // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
  2218. // line. However, the formatting looks a bit off and this probably doesn't
  2219. // happen often in practice.
  2220. verifyFormat("static int Variable[1] = {\n"
  2221. " {1000000000000000000000000000000000000}};",
  2222. getLLVMStyleWithColumns(40));
  2223. }
  2224. TEST_F(FormatTest, DesignatedInitializers) {
  2225. verifyFormat("const struct A a = {.a = 1, .b = 2};");
  2226. verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
  2227. " .bbbbbbbbbb = 2,\n"
  2228. " .cccccccccc = 3,\n"
  2229. " .dddddddddd = 4,\n"
  2230. " .eeeeeeeeee = 5};");
  2231. verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
  2232. " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
  2233. " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
  2234. " .ccccccccccccccccccccccccccc = 3,\n"
  2235. " .ddddddddddddddddddddddddddd = 4,\n"
  2236. " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
  2237. verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
  2238. }
  2239. TEST_F(FormatTest, NestedStaticInitializers) {
  2240. verifyFormat("static A x = {{{}}};\n");
  2241. verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
  2242. " {init1, init2, init3, init4}}};",
  2243. getLLVMStyleWithColumns(50));
  2244. verifyFormat("somes Status::global_reps[3] = {\n"
  2245. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  2246. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  2247. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
  2248. getLLVMStyleWithColumns(60));
  2249. verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
  2250. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  2251. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  2252. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
  2253. verifyFormat(
  2254. "CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
  2255. " {rect.fRight - rect.fLeft, rect.fBottom - rect.fTop}};");
  2256. verifyFormat(
  2257. "SomeArrayOfSomeType a = {\n"
  2258. " {{1, 2, 3},\n"
  2259. " {1, 2, 3},\n"
  2260. " {111111111111111111111111111111, 222222222222222222222222222222,\n"
  2261. " 333333333333333333333333333333},\n"
  2262. " {1, 2, 3},\n"
  2263. " {1, 2, 3}}};");
  2264. verifyFormat(
  2265. "SomeArrayOfSomeType a = {\n"
  2266. " {{1, 2, 3}},\n"
  2267. " {{1, 2, 3}},\n"
  2268. " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
  2269. " 333333333333333333333333333333}},\n"
  2270. " {{1, 2, 3}},\n"
  2271. " {{1, 2, 3}}};");
  2272. verifyFormat(
  2273. "struct {\n"
  2274. " unsigned bit;\n"
  2275. " const char *const name;\n"
  2276. "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
  2277. " {kOsWin, \"Windows\"},\n"
  2278. " {kOsLinux, \"Linux\"},\n"
  2279. " {kOsCrOS, \"Chrome OS\"}};");
  2280. verifyFormat(
  2281. "struct {\n"
  2282. " unsigned bit;\n"
  2283. " const char *const name;\n"
  2284. "} kBitsToOs[] = {\n"
  2285. " {kOsMac, \"Mac\"},\n"
  2286. " {kOsWin, \"Windows\"},\n"
  2287. " {kOsLinux, \"Linux\"},\n"
  2288. " {kOsCrOS, \"Chrome OS\"},\n"
  2289. "};");
  2290. }
  2291. TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
  2292. verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2293. " \\\n"
  2294. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
  2295. }
  2296. TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
  2297. verifyFormat("virtual void write(ELFWriter *writerrr,\n"
  2298. " OwningPtr<FileOutputBuffer> &buffer) = 0;");
  2299. }
  2300. TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
  2301. verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
  2302. getLLVMStyleWithColumns(40));
  2303. verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  2304. getLLVMStyleWithColumns(40));
  2305. EXPECT_EQ("#define Q \\\n"
  2306. " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n"
  2307. " \"aaaaaaaa.cpp\"",
  2308. format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  2309. getLLVMStyleWithColumns(40)));
  2310. }
  2311. TEST_F(FormatTest, UnderstandsLinePPDirective) {
  2312. EXPECT_EQ("# 123 \"A string literal\"",
  2313. format(" # 123 \"A string literal\""));
  2314. }
  2315. TEST_F(FormatTest, LayoutUnknownPPDirective) {
  2316. EXPECT_EQ("#;", format("#;"));
  2317. verifyFormat("#\n;\n;\n;");
  2318. }
  2319. TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
  2320. EXPECT_EQ("#line 42 \"test\"\n",
  2321. format("# \\\n line \\\n 42 \\\n \"test\"\n"));
  2322. EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n",
  2323. getLLVMStyleWithColumns(12)));
  2324. }
  2325. TEST_F(FormatTest, EndOfFileEndsPPDirective) {
  2326. EXPECT_EQ("#line 42 \"test\"",
  2327. format("# \\\n line \\\n 42 \\\n \"test\""));
  2328. EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B"));
  2329. }
  2330. TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
  2331. verifyFormat("#define A \\x20");
  2332. verifyFormat("#define A \\ x20");
  2333. EXPECT_EQ("#define A \\ x20", format("#define A \\ x20"));
  2334. verifyFormat("#define A ''");
  2335. verifyFormat("#define A ''qqq");
  2336. verifyFormat("#define A `qqq");
  2337. verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
  2338. EXPECT_EQ("const char *c = STRINGIFY(\n"
  2339. "\\na : b);",
  2340. format("const char * c = STRINGIFY(\n"
  2341. "\\na : b);"));
  2342. verifyFormat("a\r\\");
  2343. verifyFormat("a\v\\");
  2344. verifyFormat("a\f\\");
  2345. }
  2346. TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
  2347. verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
  2348. verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
  2349. verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
  2350. // FIXME: We never break before the macro name.
  2351. verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
  2352. verifyFormat("#define A A\n#define A A");
  2353. verifyFormat("#define A(X) A\n#define A A");
  2354. verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
  2355. verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
  2356. }
  2357. TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
  2358. EXPECT_EQ("// somecomment\n"
  2359. "#include \"a.h\"\n"
  2360. "#define A( \\\n"
  2361. " A, B)\n"
  2362. "#include \"b.h\"\n"
  2363. "// somecomment\n",
  2364. format(" // somecomment\n"
  2365. " #include \"a.h\"\n"
  2366. "#define A(A,\\\n"
  2367. " B)\n"
  2368. " #include \"b.h\"\n"
  2369. " // somecomment\n",
  2370. getLLVMStyleWithColumns(13)));
  2371. }
  2372. TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
  2373. TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
  2374. EXPECT_EQ("#define A \\\n"
  2375. " c; \\\n"
  2376. " e;\n"
  2377. "f;",
  2378. format("#define A c; e;\n"
  2379. "f;",
  2380. getLLVMStyleWithColumns(14)));
  2381. }
  2382. TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
  2383. TEST_F(FormatTest, AlwaysFormatsEntireMacroDefinitions) {
  2384. EXPECT_EQ("int i;\n"
  2385. "#define A \\\n"
  2386. " int i; \\\n"
  2387. " int j\n"
  2388. "int k;",
  2389. format("int i;\n"
  2390. "#define A \\\n"
  2391. " int i ; \\\n"
  2392. " int j\n"
  2393. "int k;",
  2394. 8, 0, getGoogleStyle())); // 8: position of "#define".
  2395. EXPECT_EQ("int i;\n"
  2396. "#define A \\\n"
  2397. " int i; \\\n"
  2398. " int j\n"
  2399. "int k;",
  2400. format("int i;\n"
  2401. "#define A \\\n"
  2402. " int i ; \\\n"
  2403. " int j\n"
  2404. "int k;",
  2405. 45, 0, getGoogleStyle())); // 45: position of "j".
  2406. }
  2407. TEST_F(FormatTest, MacroDefinitionInsideStatement) {
  2408. EXPECT_EQ("int x,\n"
  2409. "#define A\n"
  2410. " y;",
  2411. format("int x,\n#define A\ny;"));
  2412. }
  2413. TEST_F(FormatTest, HashInMacroDefinition) {
  2414. EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
  2415. verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
  2416. verifyFormat("#define A \\\n"
  2417. " { \\\n"
  2418. " f(#c); \\\n"
  2419. " }",
  2420. getLLVMStyleWithColumns(11));
  2421. verifyFormat("#define A(X) \\\n"
  2422. " void function##X()",
  2423. getLLVMStyleWithColumns(22));
  2424. verifyFormat("#define A(a, b, c) \\\n"
  2425. " void a##b##c()",
  2426. getLLVMStyleWithColumns(22));
  2427. verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
  2428. }
  2429. TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
  2430. EXPECT_EQ("#define A (x)", format("#define A (x)"));
  2431. EXPECT_EQ("#define A(x)", format("#define A(x)"));
  2432. }
  2433. TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
  2434. EXPECT_EQ("#define A b;", format("#define A \\\n"
  2435. " \\\n"
  2436. " b;",
  2437. getLLVMStyleWithColumns(25)));
  2438. EXPECT_EQ("#define A \\\n"
  2439. " \\\n"
  2440. " a; \\\n"
  2441. " b;",
  2442. format("#define A \\\n"
  2443. " \\\n"
  2444. " a; \\\n"
  2445. " b;",
  2446. getLLVMStyleWithColumns(11)));
  2447. EXPECT_EQ("#define A \\\n"
  2448. " a; \\\n"
  2449. " \\\n"
  2450. " b;",
  2451. format("#define A \\\n"
  2452. " a; \\\n"
  2453. " \\\n"
  2454. " b;",
  2455. getLLVMStyleWithColumns(11)));
  2456. }
  2457. TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
  2458. verifyFormat("#define A :");
  2459. verifyFormat("#define SOMECASES \\\n"
  2460. " case 1: \\\n"
  2461. " case 2\n",
  2462. getLLVMStyleWithColumns(20));
  2463. verifyFormat("#define A template <typename T>");
  2464. verifyFormat("#define STR(x) #x\n"
  2465. "f(STR(this_is_a_string_literal{));");
  2466. verifyFormat("#pragma omp threadprivate( \\\n"
  2467. " y)), // expected-warning",
  2468. getLLVMStyleWithColumns(28));
  2469. verifyFormat("#d, = };");
  2470. verifyFormat("#if \"a");
  2471. verifyFormat("({\n"
  2472. "#define b }\\\n"
  2473. " a\n"
  2474. "a");
  2475. verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
  2476. verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
  2477. verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
  2478. verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}");
  2479. }
  2480. TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
  2481. verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
  2482. EXPECT_EQ("class A : public QObject {\n"
  2483. " Q_OBJECT\n"
  2484. "\n"
  2485. " A() {}\n"
  2486. "};",
  2487. format("class A : public QObject {\n"
  2488. " Q_OBJECT\n"
  2489. "\n"
  2490. " A() {\n}\n"
  2491. "} ;"));
  2492. EXPECT_EQ("SOME_MACRO\n"
  2493. "namespace {\n"
  2494. "void f();\n"
  2495. "}",
  2496. format("SOME_MACRO\n"
  2497. " namespace {\n"
  2498. "void f( );\n"
  2499. "}"));
  2500. // Only if the identifier contains at least 5 characters.
  2501. EXPECT_EQ("HTTP f();",
  2502. format("HTTP\nf();"));
  2503. EXPECT_EQ("MACRO\nf();",
  2504. format("MACRO\nf();"));
  2505. // Only if everything is upper case.
  2506. EXPECT_EQ("class A : public QObject {\n"
  2507. " Q_Object A() {}\n"
  2508. "};",
  2509. format("class A : public QObject {\n"
  2510. " Q_Object\n"
  2511. " A() {\n}\n"
  2512. "} ;"));
  2513. // Only if the next line can actually start an unwrapped line.
  2514. EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
  2515. format("SOME_WEIRD_LOG_MACRO\n"
  2516. "<< SomeThing;"));
  2517. verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
  2518. "(n, buffers))\n", getChromiumStyle(FormatStyle::LK_Cpp));
  2519. }
  2520. TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
  2521. EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  2522. "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  2523. "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  2524. "class X {};\n"
  2525. "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  2526. "int *createScopDetectionPass() { return 0; }",
  2527. format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  2528. " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  2529. " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  2530. " class X {};\n"
  2531. " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  2532. " int *createScopDetectionPass() { return 0; }"));
  2533. // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
  2534. // braces, so that inner block is indented one level more.
  2535. EXPECT_EQ("int q() {\n"
  2536. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  2537. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  2538. " IPC_END_MESSAGE_MAP()\n"
  2539. "}",
  2540. format("int q() {\n"
  2541. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  2542. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  2543. " IPC_END_MESSAGE_MAP()\n"
  2544. "}"));
  2545. // Same inside macros.
  2546. EXPECT_EQ("#define LIST(L) \\\n"
  2547. " L(A) \\\n"
  2548. " L(B) \\\n"
  2549. " L(C)",
  2550. format("#define LIST(L) \\\n"
  2551. " L(A) \\\n"
  2552. " L(B) \\\n"
  2553. " L(C)",
  2554. getGoogleStyle()));
  2555. // These must not be recognized as macros.
  2556. EXPECT_EQ("int q() {\n"
  2557. " f(x);\n"
  2558. " f(x) {}\n"
  2559. " f(x)->g();\n"
  2560. " f(x)->*g();\n"
  2561. " f(x).g();\n"
  2562. " f(x) = x;\n"
  2563. " f(x) += x;\n"
  2564. " f(x) -= x;\n"
  2565. " f(x) *= x;\n"
  2566. " f(x) /= x;\n"
  2567. " f(x) %= x;\n"
  2568. " f(x) &= x;\n"
  2569. " f(x) |= x;\n"
  2570. " f(x) ^= x;\n"
  2571. " f(x) >>= x;\n"
  2572. " f(x) <<= x;\n"
  2573. " f(x)[y].z();\n"
  2574. " LOG(INFO) << x;\n"
  2575. " ifstream(x) >> x;\n"
  2576. "}\n",
  2577. format("int q() {\n"
  2578. " f(x)\n;\n"
  2579. " f(x)\n {}\n"
  2580. " f(x)\n->g();\n"
  2581. " f(x)\n->*g();\n"
  2582. " f(x)\n.g();\n"
  2583. " f(x)\n = x;\n"
  2584. " f(x)\n += x;\n"
  2585. " f(x)\n -= x;\n"
  2586. " f(x)\n *= x;\n"
  2587. " f(x)\n /= x;\n"
  2588. " f(x)\n %= x;\n"
  2589. " f(x)\n &= x;\n"
  2590. " f(x)\n |= x;\n"
  2591. " f(x)\n ^= x;\n"
  2592. " f(x)\n >>= x;\n"
  2593. " f(x)\n <<= x;\n"
  2594. " f(x)\n[y].z();\n"
  2595. " LOG(INFO)\n << x;\n"
  2596. " ifstream(x)\n >> x;\n"
  2597. "}\n"));
  2598. EXPECT_EQ("int q() {\n"
  2599. " F(x)\n"
  2600. " if (1) {\n"
  2601. " }\n"
  2602. " F(x)\n"
  2603. " while (1) {\n"
  2604. " }\n"
  2605. " F(x)\n"
  2606. " G(x);\n"
  2607. " F(x)\n"
  2608. " try {\n"
  2609. " Q();\n"
  2610. " } catch (...) {\n"
  2611. " }\n"
  2612. "}\n",
  2613. format("int q() {\n"
  2614. "F(x)\n"
  2615. "if (1) {}\n"
  2616. "F(x)\n"
  2617. "while (1) {}\n"
  2618. "F(x)\n"
  2619. "G(x);\n"
  2620. "F(x)\n"
  2621. "try { Q(); } catch (...) {}\n"
  2622. "}\n"));
  2623. EXPECT_EQ("class A {\n"
  2624. " A() : t(0) {}\n"
  2625. " A(int i) noexcept() : {}\n"
  2626. " A(X x)\n" // FIXME: function-level try blocks are broken.
  2627. " try : t(0) {\n"
  2628. " } catch (...) {\n"
  2629. " }\n"
  2630. "};",
  2631. format("class A {\n"
  2632. " A()\n : t(0) {}\n"
  2633. " A(int i)\n noexcept() : {}\n"
  2634. " A(X x)\n"
  2635. " try : t(0) {} catch (...) {}\n"
  2636. "};"));
  2637. EXPECT_EQ(
  2638. "class SomeClass {\n"
  2639. "public:\n"
  2640. " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2641. "};",
  2642. format("class SomeClass {\n"
  2643. "public:\n"
  2644. " SomeClass()\n"
  2645. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2646. "};"));
  2647. EXPECT_EQ(
  2648. "class SomeClass {\n"
  2649. "public:\n"
  2650. " SomeClass()\n"
  2651. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2652. "};",
  2653. format("class SomeClass {\n"
  2654. "public:\n"
  2655. " SomeClass()\n"
  2656. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2657. "};", getLLVMStyleWithColumns(40)));
  2658. }
  2659. TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
  2660. verifyFormat("#define A \\\n"
  2661. " f({ \\\n"
  2662. " g(); \\\n"
  2663. " });", getLLVMStyleWithColumns(11));
  2664. }
  2665. TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
  2666. EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
  2667. }
  2668. TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
  2669. verifyFormat("{\n { a #c; }\n}");
  2670. }
  2671. TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
  2672. EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
  2673. format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
  2674. EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
  2675. format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
  2676. }
  2677. TEST_F(FormatTest, EscapedNewlineAtStartOfToken) {
  2678. EXPECT_EQ(
  2679. "#define A \\\n int i; \\\n int j;",
  2680. format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
  2681. EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
  2682. }
  2683. TEST_F(FormatTest, NoEscapedNewlineHandlingInBlockComments) {
  2684. EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/"));
  2685. }
  2686. TEST_F(FormatTest, DontCrashOnBlockComments) {
  2687. EXPECT_EQ(
  2688. "int xxxxxxxxx; /* "
  2689. "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
  2690. "zzzzzz\n"
  2691. "0*/",
  2692. format("int xxxxxxxxx; /* "
  2693. "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
  2694. "0*/"));
  2695. }
  2696. TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
  2697. verifyFormat("#define A \\\n"
  2698. " int v( \\\n"
  2699. " a); \\\n"
  2700. " int i;",
  2701. getLLVMStyleWithColumns(11));
  2702. }
  2703. TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
  2704. EXPECT_EQ(
  2705. "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2706. " \\\n"
  2707. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2708. "\n"
  2709. "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2710. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
  2711. format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2712. "\\\n"
  2713. "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2714. " \n"
  2715. " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2716. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
  2717. }
  2718. TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
  2719. EXPECT_EQ("int\n"
  2720. "#define A\n"
  2721. " a;",
  2722. format("int\n#define A\na;"));
  2723. verifyFormat("functionCallTo(\n"
  2724. " someOtherFunction(\n"
  2725. " withSomeParameters, whichInSequence,\n"
  2726. " areLongerThanALine(andAnotherCall,\n"
  2727. "#define A B\n"
  2728. " withMoreParamters,\n"
  2729. " whichStronglyInfluenceTheLayout),\n"
  2730. " andMoreParameters),\n"
  2731. " trailing);",
  2732. getLLVMStyleWithColumns(69));
  2733. verifyFormat("Foo::Foo()\n"
  2734. "#ifdef BAR\n"
  2735. " : baz(0)\n"
  2736. "#endif\n"
  2737. "{\n"
  2738. "}");
  2739. verifyFormat("void f() {\n"
  2740. " if (true)\n"
  2741. "#ifdef A\n"
  2742. " f(42);\n"
  2743. " x();\n"
  2744. "#else\n"
  2745. " g();\n"
  2746. " x();\n"
  2747. "#endif\n"
  2748. "}");
  2749. verifyFormat("void f(param1, param2,\n"
  2750. " param3,\n"
  2751. "#ifdef A\n"
  2752. " param4(param5,\n"
  2753. "#ifdef A1\n"
  2754. " param6,\n"
  2755. "#ifdef A2\n"
  2756. " param7),\n"
  2757. "#else\n"
  2758. " param8),\n"
  2759. " param9,\n"
  2760. "#endif\n"
  2761. " param10,\n"
  2762. "#endif\n"
  2763. " param11)\n"
  2764. "#else\n"
  2765. " param12)\n"
  2766. "#endif\n"
  2767. "{\n"
  2768. " x();\n"
  2769. "}",
  2770. getLLVMStyleWithColumns(28));
  2771. verifyFormat("#if 1\n"
  2772. "int i;");
  2773. verifyFormat(
  2774. "#if 1\n"
  2775. "#endif\n"
  2776. "#if 1\n"
  2777. "#else\n"
  2778. "#endif\n");
  2779. verifyFormat("DEBUG({\n"
  2780. " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2781. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  2782. "});\n"
  2783. "#if a\n"
  2784. "#else\n"
  2785. "#endif");
  2786. verifyFormat("void f(\n"
  2787. "#if A\n"
  2788. " );\n"
  2789. "#else\n"
  2790. "#endif");
  2791. }
  2792. TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
  2793. verifyFormat("#endif\n"
  2794. "#if B");
  2795. }
  2796. TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
  2797. FormatStyle SingleLine = getLLVMStyle();
  2798. SingleLine.AllowShortIfStatementsOnASingleLine = true;
  2799. verifyFormat(
  2800. "#if 0\n"
  2801. "#elif 1\n"
  2802. "#endif\n"
  2803. "void foo() {\n"
  2804. " if (test) foo2();\n"
  2805. "}",
  2806. SingleLine);
  2807. }
  2808. TEST_F(FormatTest, LayoutBlockInsideParens) {
  2809. EXPECT_EQ("functionCall({ int i; });", format(" functionCall ( {int i;} );"));
  2810. EXPECT_EQ("functionCall({\n"
  2811. " int i;\n"
  2812. " int j;\n"
  2813. "});",
  2814. format(" functionCall ( {int i;int j;} );"));
  2815. EXPECT_EQ("functionCall({\n"
  2816. " int i;\n"
  2817. " int j;\n"
  2818. "}, aaaa, bbbb, cccc);",
  2819. format(" functionCall ( {int i;int j;}, aaaa, bbbb, cccc);"));
  2820. EXPECT_EQ("functionCall(\n"
  2821. " {\n"
  2822. " int i;\n"
  2823. " int j;\n"
  2824. " },\n"
  2825. " aaaa, bbbb, // comment\n"
  2826. " cccc);",
  2827. format(" functionCall ( {int i;int j;}, aaaa, bbbb, // comment\n"
  2828. "cccc);"));
  2829. EXPECT_EQ("functionCall(aaaa, bbbb, { int i; });",
  2830. format(" functionCall (aaaa, bbbb, {int i;});"));
  2831. EXPECT_EQ("functionCall(aaaa, bbbb, {\n"
  2832. " int i;\n"
  2833. " int j;\n"
  2834. "});",
  2835. format(" functionCall (aaaa, bbbb, {int i;int j;});"));
  2836. EXPECT_EQ("functionCall(aaaa, bbbb, { int i; });",
  2837. format(" functionCall (aaaa, bbbb, {int i;});"));
  2838. verifyFormat(
  2839. "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
  2840. " {\n"
  2841. " int i; // break\n"
  2842. " },\n"
  2843. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  2844. " ccccccccccccccccc));");
  2845. verifyFormat("DEBUG({\n"
  2846. " if (a)\n"
  2847. " f();\n"
  2848. "});");
  2849. }
  2850. TEST_F(FormatTest, LayoutBlockInsideStatement) {
  2851. EXPECT_EQ("SOME_MACRO { int i; }\n"
  2852. "int i;",
  2853. format(" SOME_MACRO {int i;} int i;"));
  2854. }
  2855. TEST_F(FormatTest, LayoutNestedBlocks) {
  2856. verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
  2857. " struct s {\n"
  2858. " int i;\n"
  2859. " };\n"
  2860. " s kBitsToOs[] = {{10}};\n"
  2861. " for (int i = 0; i < 10; ++i)\n"
  2862. " return;\n"
  2863. "}");
  2864. verifyFormat("call(parameter, {\n"
  2865. " something();\n"
  2866. " // Comment using all columns.\n"
  2867. " somethingelse();\n"
  2868. "});",
  2869. getLLVMStyleWithColumns(40));
  2870. verifyFormat("DEBUG( //\n"
  2871. " { f(); }, a);");
  2872. verifyFormat("DEBUG( //\n"
  2873. " {\n"
  2874. " f(); //\n"
  2875. " },\n"
  2876. " a);");
  2877. EXPECT_EQ("call(parameter, {\n"
  2878. " something();\n"
  2879. " // Comment too\n"
  2880. " // looooooooooong.\n"
  2881. " somethingElse();\n"
  2882. "});",
  2883. format("call(parameter, {\n"
  2884. " something();\n"
  2885. " // Comment too looooooooooong.\n"
  2886. " somethingElse();\n"
  2887. "});",
  2888. getLLVMStyleWithColumns(29)));
  2889. EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });"));
  2890. EXPECT_EQ("DEBUG({ // comment\n"
  2891. " int i;\n"
  2892. "});",
  2893. format("DEBUG({ // comment\n"
  2894. "int i;\n"
  2895. "});"));
  2896. EXPECT_EQ("DEBUG({\n"
  2897. " int i;\n"
  2898. "\n"
  2899. " // comment\n"
  2900. " int j;\n"
  2901. "});",
  2902. format("DEBUG({\n"
  2903. " int i;\n"
  2904. "\n"
  2905. " // comment\n"
  2906. " int j;\n"
  2907. "});"));
  2908. verifyFormat("DEBUG({\n"
  2909. " if (a)\n"
  2910. " return;\n"
  2911. "});");
  2912. verifyGoogleFormat("DEBUG({\n"
  2913. " if (a) return;\n"
  2914. "});");
  2915. FormatStyle Style = getGoogleStyle();
  2916. Style.ColumnLimit = 45;
  2917. verifyFormat("Debug(aaaaa, {\n"
  2918. " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
  2919. "}, a);",
  2920. Style);
  2921. verifyNoCrash("^{v^{a}}");
  2922. }
  2923. TEST_F(FormatTest, IndividualStatementsOfNestedBlocks) {
  2924. EXPECT_EQ("DEBUG({\n"
  2925. " int i;\n"
  2926. " int j;\n"
  2927. "});",
  2928. format("DEBUG( {\n"
  2929. " int i;\n"
  2930. " int j;\n"
  2931. "} ) ;",
  2932. 20, 1, getLLVMStyle()));
  2933. EXPECT_EQ("DEBUG( {\n"
  2934. " int i;\n"
  2935. " int j;\n"
  2936. "} ) ;",
  2937. format("DEBUG( {\n"
  2938. " int i;\n"
  2939. " int j;\n"
  2940. "} ) ;",
  2941. 41, 1, getLLVMStyle()));
  2942. EXPECT_EQ("DEBUG( {\n"
  2943. " int i;\n"
  2944. " int j;\n"
  2945. "} ) ;",
  2946. format("DEBUG( {\n"
  2947. " int i;\n"
  2948. " int j;\n"
  2949. "} ) ;",
  2950. 41, 1, getLLVMStyle()));
  2951. EXPECT_EQ("DEBUG({\n"
  2952. " int i;\n"
  2953. " int j;\n"
  2954. "});",
  2955. format("DEBUG( {\n"
  2956. " int i;\n"
  2957. " int j;\n"
  2958. "} ) ;",
  2959. 20, 1, getLLVMStyle()));
  2960. EXPECT_EQ("Debug({\n"
  2961. " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2962. " return;\n"
  2963. " },\n"
  2964. " a);",
  2965. format("Debug({\n"
  2966. " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2967. " return;\n"
  2968. " },\n"
  2969. " a);",
  2970. 50, 1, getLLVMStyle()));
  2971. EXPECT_EQ("DEBUG({\n"
  2972. " DEBUG({\n"
  2973. " int a;\n"
  2974. " int b;\n"
  2975. " }) ;\n"
  2976. "});",
  2977. format("DEBUG({\n"
  2978. " DEBUG({\n"
  2979. " int a;\n"
  2980. " int b;\n" // Format this line only.
  2981. " }) ;\n" // Don't touch this line.
  2982. "});",
  2983. 35, 0, getLLVMStyle()));
  2984. EXPECT_EQ("DEBUG({\n"
  2985. " int a; //\n"
  2986. "});",
  2987. format("DEBUG({\n"
  2988. " int a; //\n"
  2989. "});",
  2990. 0, 0, getLLVMStyle()));
  2991. }
  2992. TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
  2993. EXPECT_EQ("{}", format("{}"));
  2994. verifyFormat("enum E {};");
  2995. verifyFormat("enum E {}");
  2996. }
  2997. //===----------------------------------------------------------------------===//
  2998. // Line break tests.
  2999. //===----------------------------------------------------------------------===//
  3000. TEST_F(FormatTest, PreventConfusingIndents) {
  3001. verifyFormat(
  3002. "void f() {\n"
  3003. " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
  3004. " parameter, parameter, parameter)),\n"
  3005. " SecondLongCall(parameter));\n"
  3006. "}");
  3007. verifyFormat(
  3008. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3009. " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3010. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3011. " aaaaaaaaaaaaaaaaaaaaaaaa);");
  3012. verifyFormat(
  3013. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3014. " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
  3015. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  3016. " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
  3017. verifyFormat(
  3018. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  3019. " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
  3020. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
  3021. " aaaaaaaaaaaaaaaaaaaaaaaa>;");
  3022. verifyFormat("int a = bbbb && ccc && fffff(\n"
  3023. "#define A Just forcing a new line\n"
  3024. " ddd);");
  3025. }
  3026. TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
  3027. verifyFormat(
  3028. "bool aaaaaaa =\n"
  3029. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
  3030. " bbbbbbbb();");
  3031. verifyFormat(
  3032. "bool aaaaaaa =\n"
  3033. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
  3034. " bbbbbbbb();");
  3035. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  3036. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
  3037. " ccccccccc == ddddddddddd;");
  3038. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  3039. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
  3040. " ccccccccc == ddddddddddd;");
  3041. verifyFormat(
  3042. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  3043. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
  3044. " ccccccccc == ddddddddddd;");
  3045. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  3046. " aaaaaa) &&\n"
  3047. " bbbbbb && cccccc;");
  3048. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  3049. " aaaaaa) >>\n"
  3050. " bbbbbb;");
  3051. verifyFormat("Whitespaces.addUntouchableComment(\n"
  3052. " SourceMgr.getSpellingColumnNumber(\n"
  3053. " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
  3054. " 1);");
  3055. verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3056. " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
  3057. " cccccc) {\n}");
  3058. verifyFormat("b = a &&\n"
  3059. " // Comment\n"
  3060. " b.c && d;");
  3061. // If the LHS of a comparison is not a binary expression itself, the
  3062. // additional linebreak confuses many people.
  3063. verifyFormat(
  3064. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3065. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
  3066. "}");
  3067. verifyFormat(
  3068. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3069. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  3070. "}");
  3071. verifyFormat(
  3072. "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
  3073. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  3074. "}");
  3075. // Even explicit parentheses stress the precedence enough to make the
  3076. // additional break unnecessary.
  3077. verifyFormat(
  3078. "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3079. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  3080. "}");
  3081. // This cases is borderline, but with the indentation it is still readable.
  3082. verifyFormat(
  3083. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3084. " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3085. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  3086. "}",
  3087. getLLVMStyleWithColumns(75));
  3088. // If the LHS is a binary expression, we should still use the additional break
  3089. // as otherwise the formatting hides the operator precedence.
  3090. verifyFormat(
  3091. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3092. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3093. " 5) {\n"
  3094. "}");
  3095. FormatStyle OnePerLine = getLLVMStyle();
  3096. OnePerLine.BinPackParameters = false;
  3097. verifyFormat(
  3098. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3099. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3100. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
  3101. OnePerLine);
  3102. }
  3103. TEST_F(FormatTest, ExpressionIndentation) {
  3104. verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3105. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3106. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3107. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3108. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
  3109. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
  3110. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3111. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
  3112. " ccccccccccccccccccccccccccccccccccccccccc;");
  3113. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3114. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3115. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3116. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3117. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3118. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3119. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3120. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3121. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3122. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3123. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3124. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3125. verifyFormat("if () {\n"
  3126. "} else if (aaaaa &&\n"
  3127. " bbbbb > // break\n"
  3128. " ccccc) {\n"
  3129. "}");
  3130. // Presence of a trailing comment used to change indentation of b.
  3131. verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
  3132. " b;\n"
  3133. "return aaaaaaaaaaaaaaaaaaa +\n"
  3134. " b; //",
  3135. getLLVMStyleWithColumns(30));
  3136. }
  3137. TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
  3138. // Not sure what the best system is here. Like this, the LHS can be found
  3139. // immediately above an operator (everything with the same or a higher
  3140. // indent). The RHS is aligned right of the operator and so compasses
  3141. // everything until something with the same indent as the operator is found.
  3142. // FIXME: Is this a good system?
  3143. FormatStyle Style = getLLVMStyle();
  3144. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  3145. verifyFormat(
  3146. "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3147. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3148. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3149. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3150. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3151. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3152. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3153. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3154. " > ccccccccccccccccccccccccccccccccccccccccc;",
  3155. Style);
  3156. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3157. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3158. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3159. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3160. Style);
  3161. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3162. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3163. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3164. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3165. Style);
  3166. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3167. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3168. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3169. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3170. Style);
  3171. verifyFormat("if () {\n"
  3172. "} else if (aaaaa\n"
  3173. " && bbbbb // break\n"
  3174. " > ccccc) {\n"
  3175. "}",
  3176. Style);
  3177. verifyFormat("return (a)\n"
  3178. " // comment\n"
  3179. " + b;",
  3180. Style);
  3181. verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3182. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3183. " + cc;",
  3184. Style);
  3185. // Forced by comments.
  3186. verifyFormat(
  3187. "unsigned ContentSize =\n"
  3188. " sizeof(int16_t) // DWARF ARange version number\n"
  3189. " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
  3190. " + sizeof(int8_t) // Pointer Size (in bytes)\n"
  3191. " + sizeof(int8_t); // Segment Size (in bytes)");
  3192. verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
  3193. " == boost::fusion::at_c<1>(iiii).second;",
  3194. Style);
  3195. Style.ColumnLimit = 60;
  3196. verifyFormat("zzzzzzzzzz\n"
  3197. " = bbbbbbbbbbbbbbbbb\n"
  3198. " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3199. Style);
  3200. }
  3201. TEST_F(FormatTest, NoOperandAlignment) {
  3202. FormatStyle Style = getLLVMStyle();
  3203. Style.AlignOperands = false;
  3204. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
  3205. verifyFormat(
  3206. "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3207. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3208. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3209. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3210. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3211. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3212. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3213. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3214. " > ccccccccccccccccccccccccccccccccccccccccc;",
  3215. Style);
  3216. verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3217. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3218. " + cc;",
  3219. Style);
  3220. verifyFormat("int a = aa\n"
  3221. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3222. " * cccccccccccccccccccccccccccccccccccc;",
  3223. Style);
  3224. Style.AlignAfterOpenBracket = false;
  3225. verifyFormat("return (a > b\n"
  3226. " // comment1\n"
  3227. " // comment2\n"
  3228. " || c);",
  3229. Style);
  3230. }
  3231. TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
  3232. FormatStyle Style = getLLVMStyle();
  3233. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
  3234. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3235. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3236. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
  3237. Style);
  3238. }
  3239. TEST_F(FormatTest, ConstructorInitializers) {
  3240. verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
  3241. verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
  3242. getLLVMStyleWithColumns(45));
  3243. verifyFormat("Constructor()\n"
  3244. " : Inttializer(FitsOnTheLine) {}",
  3245. getLLVMStyleWithColumns(44));
  3246. verifyFormat("Constructor()\n"
  3247. " : Inttializer(FitsOnTheLine) {}",
  3248. getLLVMStyleWithColumns(43));
  3249. verifyFormat(
  3250. "SomeClass::Constructor()\n"
  3251. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  3252. verifyFormat(
  3253. "SomeClass::Constructor()\n"
  3254. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3255. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
  3256. verifyFormat(
  3257. "SomeClass::Constructor()\n"
  3258. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3259. " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  3260. verifyFormat("Constructor()\n"
  3261. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3262. " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3263. " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3264. " aaaaaaaaaaaaaaaaaaaaaaa() {}");
  3265. verifyFormat("Constructor()\n"
  3266. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3267. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3268. verifyFormat("Constructor(int Parameter = 0)\n"
  3269. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
  3270. " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
  3271. verifyFormat("Constructor()\n"
  3272. " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
  3273. "}",
  3274. getLLVMStyleWithColumns(60));
  3275. verifyFormat("Constructor()\n"
  3276. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3277. " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
  3278. // Here a line could be saved by splitting the second initializer onto two
  3279. // lines, but that is not desirable.
  3280. verifyFormat("Constructor()\n"
  3281. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3282. " aaaaaaaaaaa(aaaaaaaaaaa),\n"
  3283. " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3284. FormatStyle OnePerLine = getLLVMStyle();
  3285. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  3286. verifyFormat("SomeClass::Constructor()\n"
  3287. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3288. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3289. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  3290. OnePerLine);
  3291. verifyFormat("SomeClass::Constructor()\n"
  3292. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
  3293. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3294. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  3295. OnePerLine);
  3296. verifyFormat("MyClass::MyClass(int var)\n"
  3297. " : some_var_(var), // 4 space indent\n"
  3298. " some_other_var_(var + 1) { // lined up\n"
  3299. "}",
  3300. OnePerLine);
  3301. verifyFormat("Constructor()\n"
  3302. " : aaaaa(aaaaaa),\n"
  3303. " aaaaa(aaaaaa),\n"
  3304. " aaaaa(aaaaaa),\n"
  3305. " aaaaa(aaaaaa),\n"
  3306. " aaaaa(aaaaaa) {}",
  3307. OnePerLine);
  3308. verifyFormat("Constructor()\n"
  3309. " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
  3310. " aaaaaaaaaaaaaaaaaaaaaa) {}",
  3311. OnePerLine);
  3312. EXPECT_EQ("Constructor()\n"
  3313. " : // Comment forcing unwanted break.\n"
  3314. " aaaa(aaaa) {}",
  3315. format("Constructor() :\n"
  3316. " // Comment forcing unwanted break.\n"
  3317. " aaaa(aaaa) {}"));
  3318. }
  3319. TEST_F(FormatTest, MemoizationTests) {
  3320. // This breaks if the memoization lookup does not take \c Indent and
  3321. // \c LastSpace into account.
  3322. verifyFormat(
  3323. "extern CFRunLoopTimerRef\n"
  3324. "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
  3325. " CFTimeInterval interval, CFOptionFlags flags,\n"
  3326. " CFIndex order, CFRunLoopTimerCallBack callout,\n"
  3327. " CFRunLoopTimerContext *context) {}");
  3328. // Deep nesting somewhat works around our memoization.
  3329. verifyFormat(
  3330. "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3331. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3332. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3333. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3334. " aaaaa())))))))))))))))))))))))))))))))))))))));",
  3335. getLLVMStyleWithColumns(65));
  3336. verifyFormat(
  3337. "aaaaa(\n"
  3338. " aaaaa,\n"
  3339. " aaaaa(\n"
  3340. " aaaaa,\n"
  3341. " aaaaa(\n"
  3342. " aaaaa,\n"
  3343. " aaaaa(\n"
  3344. " aaaaa,\n"
  3345. " aaaaa(\n"
  3346. " aaaaa,\n"
  3347. " aaaaa(\n"
  3348. " aaaaa,\n"
  3349. " aaaaa(\n"
  3350. " aaaaa,\n"
  3351. " aaaaa(\n"
  3352. " aaaaa,\n"
  3353. " aaaaa(\n"
  3354. " aaaaa,\n"
  3355. " aaaaa(\n"
  3356. " aaaaa,\n"
  3357. " aaaaa(\n"
  3358. " aaaaa,\n"
  3359. " aaaaa(\n"
  3360. " aaaaa,\n"
  3361. " aaaaa))))))))))));",
  3362. getLLVMStyleWithColumns(65));
  3363. verifyFormat(
  3364. "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"
  3365. " a),\n"
  3366. " a),\n"
  3367. " a),\n"
  3368. " a),\n"
  3369. " a),\n"
  3370. " a),\n"
  3371. " a),\n"
  3372. " a),\n"
  3373. " a),\n"
  3374. " a),\n"
  3375. " a),\n"
  3376. " a),\n"
  3377. " a),\n"
  3378. " a),\n"
  3379. " a),\n"
  3380. " a),\n"
  3381. " a)",
  3382. getLLVMStyleWithColumns(65));
  3383. // This test takes VERY long when memoization is broken.
  3384. FormatStyle OnePerLine = getLLVMStyle();
  3385. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  3386. OnePerLine.BinPackParameters = false;
  3387. std::string input = "Constructor()\n"
  3388. " : aaaa(a,\n";
  3389. for (unsigned i = 0, e = 80; i != e; ++i) {
  3390. input += " a,\n";
  3391. }
  3392. input += " a) {}";
  3393. verifyFormat(input, OnePerLine);
  3394. }
  3395. TEST_F(FormatTest, BreaksAsHighAsPossible) {
  3396. verifyFormat(
  3397. "void f() {\n"
  3398. " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
  3399. " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
  3400. " f();\n"
  3401. "}");
  3402. verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
  3403. " Intervals[i - 1].getRange().getLast()) {\n}");
  3404. }
  3405. TEST_F(FormatTest, BreaksFunctionDeclarations) {
  3406. // Principially, we break function declarations in a certain order:
  3407. // 1) break amongst arguments.
  3408. verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
  3409. " Cccccccccccccc cccccccccccccc);");
  3410. verifyFormat(
  3411. "template <class TemplateIt>\n"
  3412. "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
  3413. " TemplateIt *stop) {}");
  3414. // 2) break after return type.
  3415. verifyFormat(
  3416. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3417. "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
  3418. getGoogleStyle());
  3419. // 3) break after (.
  3420. verifyFormat(
  3421. "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
  3422. " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
  3423. getGoogleStyle());
  3424. // 4) break before after nested name specifiers.
  3425. verifyFormat(
  3426. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3427. "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
  3428. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
  3429. getGoogleStyle());
  3430. // However, there are exceptions, if a sufficient amount of lines can be
  3431. // saved.
  3432. // FIXME: The precise cut-offs wrt. the number of saved lines might need some
  3433. // more adjusting.
  3434. verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  3435. " Cccccccccccccc cccccccccc,\n"
  3436. " Cccccccccccccc cccccccccc,\n"
  3437. " Cccccccccccccc cccccccccc,\n"
  3438. " Cccccccccccccc cccccccccc);");
  3439. verifyFormat(
  3440. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3441. "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3442. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3443. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
  3444. getGoogleStyle());
  3445. verifyFormat(
  3446. "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  3447. " Cccccccccccccc cccccccccc,\n"
  3448. " Cccccccccccccc cccccccccc,\n"
  3449. " Cccccccccccccc cccccccccc,\n"
  3450. " Cccccccccccccc cccccccccc,\n"
  3451. " Cccccccccccccc cccccccccc,\n"
  3452. " Cccccccccccccc cccccccccc);");
  3453. verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3454. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3455. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3456. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3457. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
  3458. // Break after multi-line parameters.
  3459. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3460. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3461. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3462. " bbbb bbbb);");
  3463. verifyFormat("void SomeLoooooooooooongFunction(\n"
  3464. " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  3465. " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3466. " int bbbbbbbbbbbbb);");
  3467. // Treat overloaded operators like other functions.
  3468. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3469. "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
  3470. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3471. "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
  3472. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3473. "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
  3474. verifyGoogleFormat(
  3475. "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
  3476. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  3477. verifyGoogleFormat(
  3478. "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
  3479. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  3480. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3481. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  3482. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
  3483. "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  3484. verifyGoogleFormat(
  3485. "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
  3486. "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3487. " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
  3488. }
  3489. TEST_F(FormatTest, TrailingReturnType) {
  3490. verifyFormat("auto foo() -> int;\n");
  3491. verifyFormat("struct S {\n"
  3492. " auto bar() const -> int;\n"
  3493. "};");
  3494. verifyFormat("template <size_t Order, typename T>\n"
  3495. "auto load_img(const std::string &filename)\n"
  3496. " -> alias::tensor<Order, T, mem::tag::cpu> {}");
  3497. verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
  3498. " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
  3499. verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
  3500. // Not trailing return types.
  3501. verifyFormat("void f() { auto a = b->c(); }");
  3502. }
  3503. TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
  3504. // Avoid breaking before trailing 'const' or other trailing annotations, if
  3505. // they are not function-like.
  3506. FormatStyle Style = getGoogleStyle();
  3507. Style.ColumnLimit = 47;
  3508. verifyFormat("void someLongFunction(\n"
  3509. " int someLoooooooooooooongParameter) const {\n}",
  3510. getLLVMStyleWithColumns(47));
  3511. verifyFormat("LoooooongReturnType\n"
  3512. "someLoooooooongFunction() const {}",
  3513. getLLVMStyleWithColumns(47));
  3514. verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
  3515. " const {}",
  3516. Style);
  3517. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3518. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
  3519. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3520. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
  3521. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3522. " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
  3523. verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
  3524. " aaaaaaaaaaa aaaaa) const override;");
  3525. verifyGoogleFormat(
  3526. "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3527. " const override;");
  3528. // Even if the first parameter has to be wrapped.
  3529. verifyFormat("void someLongFunction(\n"
  3530. " int someLongParameter) const {}",
  3531. getLLVMStyleWithColumns(46));
  3532. verifyFormat("void someLongFunction(\n"
  3533. " int someLongParameter) const {}",
  3534. Style);
  3535. verifyFormat("void someLongFunction(\n"
  3536. " int someLongParameter) override {}",
  3537. Style);
  3538. verifyFormat("void someLongFunction(\n"
  3539. " int someLongParameter) OVERRIDE {}",
  3540. Style);
  3541. verifyFormat("void someLongFunction(\n"
  3542. " int someLongParameter) final {}",
  3543. Style);
  3544. verifyFormat("void someLongFunction(\n"
  3545. " int someLongParameter) FINAL {}",
  3546. Style);
  3547. verifyFormat("void someLongFunction(\n"
  3548. " int parameter) const override {}",
  3549. Style);
  3550. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  3551. verifyFormat("void someLongFunction(\n"
  3552. " int someLongParameter) const\n"
  3553. "{\n"
  3554. "}",
  3555. Style);
  3556. // Unless these are unknown annotations.
  3557. verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
  3558. " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3559. " LONG_AND_UGLY_ANNOTATION;");
  3560. // Breaking before function-like trailing annotations is fine to keep them
  3561. // close to their arguments.
  3562. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3563. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  3564. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  3565. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  3566. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  3567. " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
  3568. verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
  3569. " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
  3570. verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
  3571. verifyFormat(
  3572. "void aaaaaaaaaaaaaaaaaa()\n"
  3573. " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3574. " aaaaaaaaaaaaaaaaaaaaaaaaa));");
  3575. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3576. " __attribute__((unused));");
  3577. verifyGoogleFormat(
  3578. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3579. " GUARDED_BY(aaaaaaaaaaaa);");
  3580. verifyGoogleFormat(
  3581. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3582. " GUARDED_BY(aaaaaaaaaaaa);");
  3583. verifyGoogleFormat(
  3584. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
  3585. " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3586. }
  3587. TEST_F(FormatTest, BreaksDesireably) {
  3588. verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  3589. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  3590. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
  3591. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3592. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  3593. "}");
  3594. verifyFormat(
  3595. "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3596. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3597. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3598. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3599. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3600. verifyFormat(
  3601. "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3602. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  3603. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3604. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
  3605. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3606. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3607. verifyFormat(
  3608. "void f() {\n"
  3609. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
  3610. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3611. "}");
  3612. verifyFormat(
  3613. "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3614. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3615. verifyFormat(
  3616. "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3617. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3618. verifyFormat(
  3619. "aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3620. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3621. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3622. // Indent consistently independent of call expression.
  3623. verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
  3624. " dddddddddddddddddddddddddddddd));\n"
  3625. "aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3626. " dddddddddddddddddddddddddddddd));");
  3627. // This test case breaks on an incorrect memoization, i.e. an optimization not
  3628. // taking into account the StopAt value.
  3629. verifyFormat(
  3630. "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3631. " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3632. " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3633. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3634. verifyFormat("{\n {\n {\n"
  3635. " Annotation.SpaceRequiredBefore =\n"
  3636. " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
  3637. " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
  3638. " }\n }\n}");
  3639. // Break on an outer level if there was a break on an inner level.
  3640. EXPECT_EQ("f(g(h(a, // comment\n"
  3641. " b, c),\n"
  3642. " d, e),\n"
  3643. " x, y);",
  3644. format("f(g(h(a, // comment\n"
  3645. " b, c), d, e), x, y);"));
  3646. // Prefer breaking similar line breaks.
  3647. verifyFormat(
  3648. "const int kTrackingOptions = NSTrackingMouseMoved |\n"
  3649. " NSTrackingMouseEnteredAndExited |\n"
  3650. " NSTrackingActiveAlways;");
  3651. }
  3652. TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
  3653. FormatStyle NoBinPacking = getGoogleStyle();
  3654. NoBinPacking.BinPackParameters = false;
  3655. NoBinPacking.BinPackArguments = true;
  3656. verifyFormat("void f() {\n"
  3657. " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
  3658. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3659. "}",
  3660. NoBinPacking);
  3661. verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
  3662. " int aaaaaaaaaaaaaaaaaaaa,\n"
  3663. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3664. NoBinPacking);
  3665. }
  3666. TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
  3667. FormatStyle NoBinPacking = getGoogleStyle();
  3668. NoBinPacking.BinPackParameters = false;
  3669. NoBinPacking.BinPackArguments = false;
  3670. verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
  3671. " aaaaaaaaaaaaaaaaaaaa,\n"
  3672. " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
  3673. NoBinPacking);
  3674. verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
  3675. " aaaaaaaaaaaaa,\n"
  3676. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
  3677. NoBinPacking);
  3678. verifyFormat(
  3679. "aaaaaaaa(aaaaaaaaaaaaa,\n"
  3680. " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3681. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  3682. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3683. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
  3684. NoBinPacking);
  3685. verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  3686. " .aaaaaaaaaaaaaaaaaa();",
  3687. NoBinPacking);
  3688. verifyFormat("void f() {\n"
  3689. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3690. " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
  3691. "}",
  3692. NoBinPacking);
  3693. verifyFormat(
  3694. "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3695. " aaaaaaaaaaaa,\n"
  3696. " aaaaaaaaaaaa);",
  3697. NoBinPacking);
  3698. verifyFormat(
  3699. "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
  3700. " ddddddddddddddddddddddddddddd),\n"
  3701. " test);",
  3702. NoBinPacking);
  3703. verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3704. " aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3705. " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;",
  3706. NoBinPacking);
  3707. verifyFormat("a(\"a\"\n"
  3708. " \"a\",\n"
  3709. " a);");
  3710. NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
  3711. verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
  3712. " aaaaaaaaa,\n"
  3713. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3714. NoBinPacking);
  3715. verifyFormat(
  3716. "void f() {\n"
  3717. " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  3718. " .aaaaaaa();\n"
  3719. "}",
  3720. NoBinPacking);
  3721. verifyFormat(
  3722. "template <class SomeType, class SomeOtherType>\n"
  3723. "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
  3724. NoBinPacking);
  3725. }
  3726. TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
  3727. FormatStyle Style = getLLVMStyleWithColumns(15);
  3728. Style.ExperimentalAutoDetectBinPacking = true;
  3729. EXPECT_EQ("aaa(aaaa,\n"
  3730. " aaaa,\n"
  3731. " aaaa);\n"
  3732. "aaa(aaaa,\n"
  3733. " aaaa,\n"
  3734. " aaaa);",
  3735. format("aaa(aaaa,\n" // one-per-line
  3736. " aaaa,\n"
  3737. " aaaa );\n"
  3738. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  3739. Style));
  3740. EXPECT_EQ("aaa(aaaa, aaaa,\n"
  3741. " aaaa);\n"
  3742. "aaa(aaaa, aaaa,\n"
  3743. " aaaa);",
  3744. format("aaa(aaaa, aaaa,\n" // bin-packed
  3745. " aaaa );\n"
  3746. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  3747. Style));
  3748. }
  3749. TEST_F(FormatTest, FormatsBuilderPattern) {
  3750. verifyFormat(
  3751. "return llvm::StringSwitch<Reference::Kind>(name)\n"
  3752. " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
  3753. " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
  3754. " .StartsWith(\".init\", ORDER_INIT)\n"
  3755. " .StartsWith(\".fini\", ORDER_FINI)\n"
  3756. " .StartsWith(\".hash\", ORDER_HASH)\n"
  3757. " .Default(ORDER_TEXT);\n");
  3758. verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
  3759. " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
  3760. verifyFormat(
  3761. "aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaa(\n"
  3762. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3763. " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
  3764. verifyFormat(
  3765. "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
  3766. " aaaaaaaaaaaaaa);");
  3767. verifyFormat(
  3768. "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
  3769. " aaaaaa->aaaaaaaaaaaa()\n"
  3770. " ->aaaaaaaaaaaaaaaa(\n"
  3771. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3772. " ->aaaaaaaaaaaaaaaaa();");
  3773. verifyGoogleFormat(
  3774. "void f() {\n"
  3775. " someo->Add((new util::filetools::Handler(dir))\n"
  3776. " ->OnEvent1(NewPermanentCallback(\n"
  3777. " this, &HandlerHolderClass::EventHandlerCBA))\n"
  3778. " ->OnEvent2(NewPermanentCallback(\n"
  3779. " this, &HandlerHolderClass::EventHandlerCBB))\n"
  3780. " ->OnEvent3(NewPermanentCallback(\n"
  3781. " this, &HandlerHolderClass::EventHandlerCBC))\n"
  3782. " ->OnEvent5(NewPermanentCallback(\n"
  3783. " this, &HandlerHolderClass::EventHandlerCBD))\n"
  3784. " ->OnEvent6(NewPermanentCallback(\n"
  3785. " this, &HandlerHolderClass::EventHandlerCBE)));\n"
  3786. "}");
  3787. verifyFormat(
  3788. "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
  3789. verifyFormat("aaaaaaaaaaaaaaa()\n"
  3790. " .aaaaaaaaaaaaaaa()\n"
  3791. " .aaaaaaaaaaaaaaa()\n"
  3792. " .aaaaaaaaaaaaaaa()\n"
  3793. " .aaaaaaaaaaaaaaa();");
  3794. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3795. " .aaaaaaaaaaaaaaa()\n"
  3796. " .aaaaaaaaaaaaaaa()\n"
  3797. " .aaaaaaaaaaaaaaa();");
  3798. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3799. " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3800. " .aaaaaaaaaaaaaaa();");
  3801. verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3802. " ->aaaaaaaaaaaaaae(0)\n"
  3803. " ->aaaaaaaaaaaaaaa();");
  3804. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  3805. " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3806. " .has<bbbbbbbbbbbbbbbbbbbbb>();");
  3807. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  3808. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  3809. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
  3810. // Prefer not to break after empty parentheses.
  3811. verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
  3812. " First->LastNewlineOffset);");
  3813. }
  3814. TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
  3815. verifyFormat(
  3816. "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3817. " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
  3818. verifyFormat(
  3819. "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
  3820. " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
  3821. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  3822. " ccccccccccccccccccccccccc) {\n}");
  3823. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
  3824. " ccccccccccccccccccccccccc) {\n}");
  3825. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  3826. " ccccccccccccccccccccccccc) {\n}");
  3827. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
  3828. " ccccccccccccccccccccccccc) {\n}");
  3829. verifyFormat(
  3830. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
  3831. " ccccccccccccccccccccccccc) {\n}");
  3832. verifyFormat(
  3833. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
  3834. " ccccccccccccccccccccccccc) {\n}");
  3835. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
  3836. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
  3837. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
  3838. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  3839. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
  3840. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
  3841. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
  3842. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  3843. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
  3844. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
  3845. " aaaaaaaaaaaaaaa != aa) {\n}");
  3846. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
  3847. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
  3848. " aaaaaaaaaaaaaaa != aa) {\n}");
  3849. }
  3850. TEST_F(FormatTest, BreaksAfterAssignments) {
  3851. verifyFormat(
  3852. "unsigned Cost =\n"
  3853. " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
  3854. " SI->getPointerAddressSpaceee());\n");
  3855. verifyFormat(
  3856. "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
  3857. " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
  3858. verifyFormat(
  3859. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
  3860. " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
  3861. verifyFormat("unsigned OriginalStartColumn =\n"
  3862. " SourceMgr.getSpellingColumnNumber(\n"
  3863. " Current.FormatTok.getStartOfNonWhitespace()) -\n"
  3864. " 1;");
  3865. }
  3866. TEST_F(FormatTest, AlignsAfterAssignments) {
  3867. verifyFormat(
  3868. "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3869. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3870. verifyFormat(
  3871. "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3872. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3873. verifyFormat(
  3874. "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3875. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3876. verifyFormat(
  3877. "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3878. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  3879. verifyFormat(
  3880. "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3881. " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3882. " aaaaaaaaaaaaaaaaaaaaaaaa;");
  3883. }
  3884. TEST_F(FormatTest, AlignsAfterReturn) {
  3885. verifyFormat(
  3886. "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3887. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3888. verifyFormat(
  3889. "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3890. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  3891. verifyFormat(
  3892. "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  3893. " aaaaaaaaaaaaaaaaaaaaaa();");
  3894. verifyFormat(
  3895. "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  3896. " aaaaaaaaaaaaaaaaaaaaaa());");
  3897. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3898. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3899. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3900. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
  3901. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3902. verifyFormat("return\n"
  3903. " // true if code is one of a or b.\n"
  3904. " code == a || code == b;");
  3905. }
  3906. TEST_F(FormatTest, AlignsAfterOpenBracket) {
  3907. verifyFormat(
  3908. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
  3909. " aaaaaaaaa aaaaaaa) {}");
  3910. verifyFormat(
  3911. "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
  3912. " aaaaaaaaaaa aaaaaaaaa);");
  3913. verifyFormat(
  3914. "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
  3915. " aaaaaaaaaaaaaaaaaaaaa));");
  3916. FormatStyle Style = getLLVMStyle();
  3917. Style.AlignAfterOpenBracket = false;
  3918. verifyFormat(
  3919. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3920. " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
  3921. Style);
  3922. verifyFormat(
  3923. "SomeLongVariableName->someVeryLongFunctionName(\n"
  3924. " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
  3925. Style);
  3926. verifyFormat(
  3927. "SomeLongVariableName->someFunction(\n"
  3928. " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
  3929. Style);
  3930. verifyFormat(
  3931. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
  3932. " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3933. Style);
  3934. verifyFormat(
  3935. "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
  3936. " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3937. Style);
  3938. verifyFormat(
  3939. "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
  3940. " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
  3941. Style);
  3942. }
  3943. TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
  3944. FormatStyle Style = getLLVMStyleWithColumns(40);
  3945. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  3946. " bbbbbbbbbbbbbbbbbbbbbb);",
  3947. Style);
  3948. Style.AlignAfterOpenBracket = true;
  3949. Style.AlignOperands = false;
  3950. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  3951. " bbbbbbbbbbbbbbbbbbbbbb);",
  3952. Style);
  3953. Style.AlignAfterOpenBracket = false;
  3954. Style.AlignOperands = true;
  3955. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  3956. " bbbbbbbbbbbbbbbbbbbbbb);",
  3957. Style);
  3958. Style.AlignAfterOpenBracket = false;
  3959. Style.AlignOperands = false;
  3960. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  3961. " bbbbbbbbbbbbbbbbbbbbbb);",
  3962. Style);
  3963. }
  3964. TEST_F(FormatTest, BreaksConditionalExpressions) {
  3965. verifyFormat(
  3966. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3967. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3968. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3969. verifyFormat(
  3970. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3971. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3972. verifyFormat(
  3973. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
  3974. " : aaaaaaaaaaaaa);");
  3975. verifyFormat(
  3976. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3977. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3978. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3979. " aaaaaaaaaaaaa);");
  3980. verifyFormat(
  3981. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3982. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3983. " aaaaaaaaaaaaa);");
  3984. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3985. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3986. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3987. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3988. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3989. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3990. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3991. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3992. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3993. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3994. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3995. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3996. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3997. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3998. " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3999. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4000. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4001. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4002. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4003. " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4004. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  4005. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4006. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4007. " : aaaaaaaaaaaaaaaa;");
  4008. verifyFormat(
  4009. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4010. " ? aaaaaaaaaaaaaaa\n"
  4011. " : aaaaaaaaaaaaaaa;");
  4012. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  4013. " aaaaaaaaa\n"
  4014. " ? b\n"
  4015. " : c);");
  4016. verifyFormat("return aaaa == bbbb\n"
  4017. " // comment\n"
  4018. " ? aaaa\n"
  4019. " : bbbb;");
  4020. verifyFormat(
  4021. "unsigned Indent =\n"
  4022. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
  4023. " ? IndentForLevel[TheLine.Level]\n"
  4024. " : TheLine * 2,\n"
  4025. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  4026. getLLVMStyleWithColumns(70));
  4027. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  4028. " ? aaaaaaaaaaaaaaa\n"
  4029. " : bbbbbbbbbbbbbbb //\n"
  4030. " ? ccccccccccccccc\n"
  4031. " : ddddddddddddddd;");
  4032. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  4033. " ? aaaaaaaaaaaaaaa\n"
  4034. " : (bbbbbbbbbbbbbbb //\n"
  4035. " ? ccccccccccccccc\n"
  4036. " : ddddddddddddddd);");
  4037. verifyFormat(
  4038. "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4039. " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4040. " aaaaaaaaaaaaaaaaaaaaa +\n"
  4041. " aaaaaaaaaaaaaaaaaaaaa\n"
  4042. " : aaaaaaaaaa;");
  4043. verifyFormat(
  4044. "aaaaaa = aaaaaaaaaaaa\n"
  4045. " ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4046. " : aaaaaaaaaaaaaaaaaaaaaa\n"
  4047. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4048. FormatStyle NoBinPacking = getLLVMStyle();
  4049. NoBinPacking.BinPackArguments = false;
  4050. verifyFormat(
  4051. "void f() {\n"
  4052. " g(aaa,\n"
  4053. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  4054. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4055. " ? aaaaaaaaaaaaaaa\n"
  4056. " : aaaaaaaaaaaaaaa);\n"
  4057. "}",
  4058. NoBinPacking);
  4059. verifyFormat(
  4060. "void f() {\n"
  4061. " g(aaa,\n"
  4062. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  4063. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4064. " ?: aaaaaaaaaaaaaaa);\n"
  4065. "}",
  4066. NoBinPacking);
  4067. verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
  4068. " // comment.\n"
  4069. " ccccccccccccccccccccccccccccccccccccccc\n"
  4070. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4071. " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
  4072. // Assignments in conditional expressions. Apparently not uncommon :-(.
  4073. verifyFormat("return a != b\n"
  4074. " // comment\n"
  4075. " ? a = b\n"
  4076. " : a = b;");
  4077. verifyFormat("return a != b\n"
  4078. " // comment\n"
  4079. " ? a = a != b\n"
  4080. " // comment\n"
  4081. " ? a = b\n"
  4082. " : a\n"
  4083. " : a;\n");
  4084. verifyFormat("return a != b\n"
  4085. " // comment\n"
  4086. " ? a\n"
  4087. " : a = a != b\n"
  4088. " // comment\n"
  4089. " ? a = b\n"
  4090. " : a;");
  4091. }
  4092. TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
  4093. FormatStyle Style = getLLVMStyle();
  4094. Style.BreakBeforeTernaryOperators = false;
  4095. Style.ColumnLimit = 70;
  4096. verifyFormat(
  4097. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4098. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4099. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4100. Style);
  4101. verifyFormat(
  4102. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4103. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4104. Style);
  4105. verifyFormat(
  4106. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
  4107. " aaaaaaaaaaaaa);",
  4108. Style);
  4109. verifyFormat(
  4110. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4111. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4112. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4113. " aaaaaaaaaaaaa);",
  4114. Style);
  4115. verifyFormat(
  4116. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4117. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4118. " aaaaaaaaaaaaa);",
  4119. Style);
  4120. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4121. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4122. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  4123. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4124. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4125. Style);
  4126. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4127. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4128. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4129. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  4130. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4131. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4132. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4133. Style);
  4134. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4135. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
  4136. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4137. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4138. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4139. Style);
  4140. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4141. " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4142. " aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4143. Style);
  4144. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  4145. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4146. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4147. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4148. Style);
  4149. verifyFormat(
  4150. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4151. " aaaaaaaaaaaaaaa :\n"
  4152. " aaaaaaaaaaaaaaa;",
  4153. Style);
  4154. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  4155. " aaaaaaaaa ?\n"
  4156. " b :\n"
  4157. " c);",
  4158. Style);
  4159. verifyFormat(
  4160. "unsigned Indent =\n"
  4161. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n"
  4162. " IndentForLevel[TheLine.Level] :\n"
  4163. " TheLine * 2,\n"
  4164. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  4165. Style);
  4166. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  4167. " aaaaaaaaaaaaaaa :\n"
  4168. " bbbbbbbbbbbbbbb ? //\n"
  4169. " ccccccccccccccc :\n"
  4170. " ddddddddddddddd;",
  4171. Style);
  4172. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  4173. " aaaaaaaaaaaaaaa :\n"
  4174. " (bbbbbbbbbbbbbbb ? //\n"
  4175. " ccccccccccccccc :\n"
  4176. " ddddddddddddddd);",
  4177. Style);
  4178. }
  4179. TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
  4180. verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
  4181. " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
  4182. verifyFormat("bool a = true, b = false;");
  4183. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4184. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
  4185. " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
  4186. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
  4187. verifyFormat(
  4188. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  4189. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
  4190. " d = e && f;");
  4191. verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
  4192. " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
  4193. verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  4194. " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
  4195. verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
  4196. " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
  4197. FormatStyle Style = getGoogleStyle();
  4198. Style.PointerAlignment = FormatStyle::PAS_Left;
  4199. Style.DerivePointerAlignment = false;
  4200. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4201. " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
  4202. " *b = bbbbbbbbbbbbbbbbbbb;",
  4203. Style);
  4204. verifyFormat(
  4205. "aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  4206. " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
  4207. Style);
  4208. }
  4209. TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
  4210. verifyFormat("arr[foo ? bar : baz];");
  4211. verifyFormat("f()[foo ? bar : baz];");
  4212. verifyFormat("(a + b)[foo ? bar : baz];");
  4213. verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
  4214. }
  4215. TEST_F(FormatTest, AlignsStringLiterals) {
  4216. verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
  4217. " \"short literal\");");
  4218. verifyFormat(
  4219. "looooooooooooooooooooooooongFunction(\n"
  4220. " \"short literal\"\n"
  4221. " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
  4222. verifyFormat("someFunction(\"Always break between multi-line\"\n"
  4223. " \" string literals\",\n"
  4224. " and, other, parameters);");
  4225. EXPECT_EQ("fun + \"1243\" /* comment */\n"
  4226. " \"5678\";",
  4227. format("fun + \"1243\" /* comment */\n"
  4228. " \"5678\";",
  4229. getLLVMStyleWithColumns(28)));
  4230. EXPECT_EQ(
  4231. "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  4232. " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
  4233. " \"aaaaaaaaaaaaaaaa\";",
  4234. format("aaaaaa ="
  4235. "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  4236. "aaaaaaaaaaaaaaaaaaaaa\" "
  4237. "\"aaaaaaaaaaaaaaaa\";"));
  4238. verifyFormat("a = a + \"a\"\n"
  4239. " \"a\"\n"
  4240. " \"a\";");
  4241. verifyFormat("f(\"a\", \"b\"\n"
  4242. " \"c\");");
  4243. verifyFormat(
  4244. "#define LL_FORMAT \"ll\"\n"
  4245. "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
  4246. " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
  4247. verifyFormat("#define A(X) \\\n"
  4248. " \"aaaaa\" #X \"bbbbbb\" \\\n"
  4249. " \"ccccc\"",
  4250. getLLVMStyleWithColumns(23));
  4251. verifyFormat("#define A \"def\"\n"
  4252. "f(\"abc\" A \"ghi\"\n"
  4253. " \"jkl\");");
  4254. verifyFormat("f(L\"a\"\n"
  4255. " L\"b\")");
  4256. verifyFormat("#define A(X) \\\n"
  4257. " L\"aaaaa\" #X L\"bbbbbb\" \\\n"
  4258. " L\"ccccc\"",
  4259. getLLVMStyleWithColumns(25));
  4260. }
  4261. TEST_F(FormatTest, AlwaysBreakAfterDefinitionReturnType) {
  4262. FormatStyle AfterType = getLLVMStyle();
  4263. AfterType.AlwaysBreakAfterDefinitionReturnType = true;
  4264. verifyFormat("const char *\n"
  4265. "f(void) {\n" // Break here.
  4266. " return \"\";\n"
  4267. "}\n"
  4268. "const char *bar(void);\n", // No break here.
  4269. AfterType);
  4270. verifyFormat("template <class T>\n"
  4271. "T *\n"
  4272. "f(T &c) {\n" // Break here.
  4273. " return NULL;\n"
  4274. "}\n"
  4275. "template <class T> T *f(T &c);\n", // No break here.
  4276. AfterType);
  4277. AfterType.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  4278. verifyFormat("const char *\n"
  4279. "f(void)\n" // Break here.
  4280. "{\n"
  4281. " return \"\";\n"
  4282. "}\n"
  4283. "const char *bar(void);\n", // No break here.
  4284. AfterType);
  4285. verifyFormat("template <class T>\n"
  4286. "T *\n" // Problem here: no line break
  4287. "f(T &c)\n" // Break here.
  4288. "{\n"
  4289. " return NULL;\n"
  4290. "}\n"
  4291. "template <class T> T *f(T &c);\n", // No break here.
  4292. AfterType);
  4293. }
  4294. TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
  4295. FormatStyle NoBreak = getLLVMStyle();
  4296. NoBreak.AlwaysBreakBeforeMultilineStrings = false;
  4297. FormatStyle Break = getLLVMStyle();
  4298. Break.AlwaysBreakBeforeMultilineStrings = true;
  4299. verifyFormat("aaaa = \"bbbb\"\n"
  4300. " \"cccc\";",
  4301. NoBreak);
  4302. verifyFormat("aaaa =\n"
  4303. " \"bbbb\"\n"
  4304. " \"cccc\";",
  4305. Break);
  4306. verifyFormat("aaaa(\"bbbb\"\n"
  4307. " \"cccc\");",
  4308. NoBreak);
  4309. verifyFormat("aaaa(\n"
  4310. " \"bbbb\"\n"
  4311. " \"cccc\");",
  4312. Break);
  4313. verifyFormat("aaaa(qqq, \"bbbb\"\n"
  4314. " \"cccc\");",
  4315. NoBreak);
  4316. verifyFormat("aaaa(qqq,\n"
  4317. " \"bbbb\"\n"
  4318. " \"cccc\");",
  4319. Break);
  4320. verifyFormat("aaaa(qqq,\n"
  4321. " L\"bbbb\"\n"
  4322. " L\"cccc\");",
  4323. Break);
  4324. // As we break before unary operators, breaking right after them is bad.
  4325. verifyFormat("string foo = abc ? \"x\"\n"
  4326. " \"blah blah blah blah blah blah\"\n"
  4327. " : \"y\";",
  4328. Break);
  4329. // Don't break if there is no column gain.
  4330. verifyFormat("f(\"aaaa\"\n"
  4331. " \"bbbb\");",
  4332. Break);
  4333. // Treat literals with escaped newlines like multi-line string literals.
  4334. EXPECT_EQ("x = \"a\\\n"
  4335. "b\\\n"
  4336. "c\";",
  4337. format("x = \"a\\\n"
  4338. "b\\\n"
  4339. "c\";",
  4340. NoBreak));
  4341. EXPECT_EQ("x =\n"
  4342. " \"a\\\n"
  4343. "b\\\n"
  4344. "c\";",
  4345. format("x = \"a\\\n"
  4346. "b\\\n"
  4347. "c\";",
  4348. Break));
  4349. // Exempt ObjC strings for now.
  4350. EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
  4351. " \"bbbb\";",
  4352. format("NSString *const kString = @\"aaaa\"\n"
  4353. "\"bbbb\";",
  4354. Break));
  4355. Break.ColumnLimit = 0;
  4356. verifyFormat("const char *hello = \"hello llvm\";", Break);
  4357. }
  4358. TEST_F(FormatTest, AlignsPipes) {
  4359. verifyFormat(
  4360. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4361. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4362. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4363. verifyFormat(
  4364. "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
  4365. " << aaaaaaaaaaaaaaaaaaaa;");
  4366. verifyFormat(
  4367. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4368. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4369. verifyFormat(
  4370. "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
  4371. " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
  4372. " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
  4373. verifyFormat(
  4374. "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4375. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4376. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4377. verifyFormat(
  4378. "llvm::errs() << \"a: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4379. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4380. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4381. verifyFormat(
  4382. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4383. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4384. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4385. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4386. verifyFormat(
  4387. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4388. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4389. verifyFormat("return out << \"somepacket = {\\n\"\n"
  4390. " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
  4391. " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
  4392. " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
  4393. " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
  4394. " << \"}\";");
  4395. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  4396. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  4397. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
  4398. verifyFormat(
  4399. "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
  4400. " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
  4401. " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
  4402. " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
  4403. " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
  4404. verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
  4405. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4406. verifyFormat(
  4407. "void f() {\n"
  4408. " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
  4409. " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  4410. "}");
  4411. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
  4412. " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
  4413. // Breaking before the first "<<" is generally not desirable.
  4414. verifyFormat(
  4415. "llvm::errs()\n"
  4416. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4417. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4418. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4419. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4420. getLLVMStyleWithColumns(70));
  4421. verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4422. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4423. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4424. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4425. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4426. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4427. getLLVMStyleWithColumns(70));
  4428. // But sometimes, breaking before the first "<<" is desirable.
  4429. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
  4430. " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
  4431. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
  4432. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4433. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4434. verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
  4435. " << BEF << IsTemplate << Description << E->getType();");
  4436. verifyFormat(
  4437. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4438. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4439. // Incomplete string literal.
  4440. EXPECT_EQ("llvm::errs() << \"\n"
  4441. " << a;",
  4442. format("llvm::errs() << \"\n<<a;"));
  4443. verifyFormat("void f() {\n"
  4444. " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
  4445. " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
  4446. "}");
  4447. // Handle 'endl'.
  4448. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
  4449. " << bbbbbbbbbbbbbbbbbbbbbb << endl;");
  4450. verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
  4451. }
  4452. TEST_F(FormatTest, UnderstandsEquals) {
  4453. verifyFormat(
  4454. "aaaaaaaaaaaaaaaaa =\n"
  4455. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4456. verifyFormat(
  4457. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4458. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  4459. verifyFormat(
  4460. "if (a) {\n"
  4461. " f();\n"
  4462. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4463. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  4464. "}");
  4465. verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4466. " 100000000 + 10000000) {\n}");
  4467. }
  4468. TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
  4469. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  4470. " .looooooooooooooooooooooooooooooooooooooongFunction();");
  4471. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  4472. " ->looooooooooooooooooooooooooooooooooooooongFunction();");
  4473. verifyFormat(
  4474. "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
  4475. " Parameter2);");
  4476. verifyFormat(
  4477. "ShortObject->shortFunction(\n"
  4478. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
  4479. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
  4480. verifyFormat("loooooooooooooongFunction(\n"
  4481. " LoooooooooooooongObject->looooooooooooooooongFunction());");
  4482. verifyFormat(
  4483. "function(LoooooooooooooooooooooooooooooooooooongObject\n"
  4484. " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
  4485. verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  4486. " .WillRepeatedly(Return(SomeValue));");
  4487. verifyFormat("void f() {\n"
  4488. " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  4489. " .Times(2)\n"
  4490. " .WillRepeatedly(Return(SomeValue));\n"
  4491. "}");
  4492. verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
  4493. " ccccccccccccccccccccccc);");
  4494. verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4495. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).aaaaa(aaaaa),\n"
  4496. " aaaaaaaaaaaaaaaaaaaaa);");
  4497. verifyFormat("void f() {\n"
  4498. " aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4499. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
  4500. "}");
  4501. verifyFormat(
  4502. "aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4503. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4504. " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4505. " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4506. " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  4507. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4508. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4509. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4510. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
  4511. "}");
  4512. // Here, it is not necessary to wrap at "." or "->".
  4513. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
  4514. " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  4515. verifyFormat(
  4516. "aaaaaaaaaaa->aaaaaaaaa(\n"
  4517. " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4518. " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
  4519. verifyFormat(
  4520. "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4521. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
  4522. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
  4523. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  4524. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
  4525. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  4526. // FIXME: Should we break before .a()?
  4527. verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4528. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).a();");
  4529. FormatStyle NoBinPacking = getLLVMStyle();
  4530. NoBinPacking.BinPackParameters = false;
  4531. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  4532. " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  4533. " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
  4534. " aaaaaaaaaaaaaaaaaaa,\n"
  4535. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4536. NoBinPacking);
  4537. // If there is a subsequent call, change to hanging indentation.
  4538. verifyFormat(
  4539. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4540. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
  4541. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4542. verifyFormat(
  4543. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4544. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
  4545. verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4546. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4547. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4548. verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4549. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4550. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  4551. }
  4552. TEST_F(FormatTest, WrapsTemplateDeclarations) {
  4553. verifyFormat("template <typename T>\n"
  4554. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  4555. verifyFormat("template <typename T>\n"
  4556. "// T should be one of {A, B}.\n"
  4557. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  4558. verifyFormat(
  4559. "template <typename T>\n"
  4560. "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
  4561. verifyFormat("template <typename T>\n"
  4562. "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
  4563. " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
  4564. verifyFormat(
  4565. "template <typename T>\n"
  4566. "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
  4567. " int Paaaaaaaaaaaaaaaaaaaaram2);");
  4568. verifyFormat(
  4569. "template <typename T>\n"
  4570. "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
  4571. " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
  4572. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4573. verifyFormat("template <typename T>\n"
  4574. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4575. " int aaaaaaaaaaaaaaaaaaaaaa);");
  4576. verifyFormat(
  4577. "template <typename T1, typename T2 = char, typename T3 = char,\n"
  4578. " typename T4 = char>\n"
  4579. "void f();");
  4580. verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
  4581. " template <typename> class cccccccccccccccccccccc,\n"
  4582. " typename ddddddddddddd>\n"
  4583. "class C {};");
  4584. verifyFormat(
  4585. "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
  4586. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4587. verifyFormat("void f() {\n"
  4588. " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
  4589. " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
  4590. "}");
  4591. verifyFormat("template <typename T> class C {};");
  4592. verifyFormat("template <typename T> void f();");
  4593. verifyFormat("template <typename T> void f() {}");
  4594. verifyFormat(
  4595. "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  4596. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4597. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
  4598. " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  4599. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4600. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
  4601. " bbbbbbbbbbbbbbbbbbbbbbbb);",
  4602. getLLVMStyleWithColumns(72));
  4603. EXPECT_EQ("static_cast<A< //\n"
  4604. " B> *>(\n"
  4605. "\n"
  4606. " );",
  4607. format("static_cast<A<//\n"
  4608. " B>*>(\n"
  4609. "\n"
  4610. " );"));
  4611. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4612. " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
  4613. FormatStyle AlwaysBreak = getLLVMStyle();
  4614. AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
  4615. verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
  4616. verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
  4617. verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
  4618. verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4619. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
  4620. " ccccccccccccccccccccccccccccccccccccccccccccccc);");
  4621. verifyFormat("template <template <typename> class Fooooooo,\n"
  4622. " template <typename> class Baaaaaaar>\n"
  4623. "struct C {};",
  4624. AlwaysBreak);
  4625. verifyFormat("template <typename T> // T can be A, B or C.\n"
  4626. "struct C {};",
  4627. AlwaysBreak);
  4628. }
  4629. TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
  4630. verifyFormat(
  4631. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4632. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4633. verifyFormat(
  4634. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4635. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4636. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  4637. // FIXME: Should we have the extra indent after the second break?
  4638. verifyFormat(
  4639. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4640. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4641. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4642. verifyFormat(
  4643. "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
  4644. " cccccccccccccccccccccccccccccccccccccccccccccc());");
  4645. // Breaking at nested name specifiers is generally not desirable.
  4646. verifyFormat(
  4647. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4648. " aaaaaaaaaaaaaaaaaaaaaaa);");
  4649. verifyFormat(
  4650. "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4651. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4652. " aaaaaaaaaaaaaaaaaaaaa);",
  4653. getLLVMStyleWithColumns(74));
  4654. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4655. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4656. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4657. }
  4658. TEST_F(FormatTest, UnderstandsTemplateParameters) {
  4659. verifyFormat("A<int> a;");
  4660. verifyFormat("A<A<A<int>>> a;");
  4661. verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
  4662. verifyFormat("bool x = a < 1 || 2 > a;");
  4663. verifyFormat("bool x = 5 < f<int>();");
  4664. verifyFormat("bool x = f<int>() > 5;");
  4665. verifyFormat("bool x = 5 < a<int>::x;");
  4666. verifyFormat("bool x = a < 4 ? a > 2 : false;");
  4667. verifyFormat("bool x = f() ? a < 2 : a > 2;");
  4668. verifyGoogleFormat("A<A<int>> a;");
  4669. verifyGoogleFormat("A<A<A<int>>> a;");
  4670. verifyGoogleFormat("A<A<A<A<int>>>> a;");
  4671. verifyGoogleFormat("A<A<int> > a;");
  4672. verifyGoogleFormat("A<A<A<int> > > a;");
  4673. verifyGoogleFormat("A<A<A<A<int> > > > a;");
  4674. verifyGoogleFormat("A<::A<int>> a;");
  4675. verifyGoogleFormat("A<::A> a;");
  4676. verifyGoogleFormat("A< ::A> a;");
  4677. verifyGoogleFormat("A< ::A<int> > a;");
  4678. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
  4679. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
  4680. EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
  4681. EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
  4682. verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
  4683. verifyFormat("test >> a >> b;");
  4684. verifyFormat("test << a >> b;");
  4685. verifyFormat("f<int>();");
  4686. verifyFormat("template <typename T> void f() {}");
  4687. verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
  4688. // Not template parameters.
  4689. verifyFormat("return a < b && c > d;");
  4690. verifyFormat("void f() {\n"
  4691. " while (a < b && c > d) {\n"
  4692. " }\n"
  4693. "}");
  4694. verifyFormat("template <typename... Types>\n"
  4695. "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
  4696. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4697. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
  4698. getLLVMStyleWithColumns(60));
  4699. verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
  4700. verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
  4701. }
  4702. TEST_F(FormatTest, UnderstandsBinaryOperators) {
  4703. verifyFormat("COMPARE(a, ==, b);");
  4704. }
  4705. TEST_F(FormatTest, UnderstandsPointersToMembers) {
  4706. verifyFormat("int A::*x;");
  4707. verifyFormat("int (S::*func)(void *);");
  4708. verifyFormat("void f() { int (S::*func)(void *); }");
  4709. verifyFormat("typedef bool *(Class::*Member)() const;");
  4710. verifyFormat("void f() {\n"
  4711. " (a->*f)();\n"
  4712. " a->*x;\n"
  4713. " (a.*f)();\n"
  4714. " ((*a).*f)();\n"
  4715. " a.*x;\n"
  4716. "}");
  4717. verifyFormat("void f() {\n"
  4718. " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  4719. " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
  4720. "}");
  4721. verifyFormat(
  4722. "(aaaaaaaaaa->*bbbbbbb)(\n"
  4723. " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  4724. FormatStyle Style = getLLVMStyle();
  4725. Style.PointerAlignment = FormatStyle::PAS_Left;
  4726. verifyFormat("typedef bool* (Class::*Member)() const;", Style);
  4727. }
  4728. TEST_F(FormatTest, UnderstandsUnaryOperators) {
  4729. verifyFormat("int a = -2;");
  4730. verifyFormat("f(-1, -2, -3);");
  4731. verifyFormat("a[-1] = 5;");
  4732. verifyFormat("int a = 5 + -2;");
  4733. verifyFormat("if (i == -1) {\n}");
  4734. verifyFormat("if (i != -1) {\n}");
  4735. verifyFormat("if (i > -1) {\n}");
  4736. verifyFormat("if (i < -1) {\n}");
  4737. verifyFormat("++(a->f());");
  4738. verifyFormat("--(a->f());");
  4739. verifyFormat("(a->f())++;");
  4740. verifyFormat("a[42]++;");
  4741. verifyFormat("if (!(a->f())) {\n}");
  4742. verifyFormat("a-- > b;");
  4743. verifyFormat("b ? -a : c;");
  4744. verifyFormat("n * sizeof char16;");
  4745. verifyFormat("n * alignof char16;", getGoogleStyle());
  4746. verifyFormat("sizeof(char);");
  4747. verifyFormat("alignof(char);", getGoogleStyle());
  4748. verifyFormat("return -1;");
  4749. verifyFormat("switch (a) {\n"
  4750. "case -1:\n"
  4751. " break;\n"
  4752. "}");
  4753. verifyFormat("#define X -1");
  4754. verifyFormat("#define X -kConstant");
  4755. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
  4756. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
  4757. verifyFormat("int a = /* confusing comment */ -1;");
  4758. // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
  4759. verifyFormat("int a = i /* confusing comment */++;");
  4760. }
  4761. TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
  4762. verifyFormat("if (!aaaaaaaaaa( // break\n"
  4763. " aaaaa)) {\n"
  4764. "}");
  4765. verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
  4766. " aaaaa));");
  4767. verifyFormat("*aaa = aaaaaaa( // break\n"
  4768. " bbbbbb);");
  4769. }
  4770. TEST_F(FormatTest, UnderstandsOverloadedOperators) {
  4771. verifyFormat("bool operator<();");
  4772. verifyFormat("bool operator>();");
  4773. verifyFormat("bool operator=();");
  4774. verifyFormat("bool operator==();");
  4775. verifyFormat("bool operator!=();");
  4776. verifyFormat("int operator+();");
  4777. verifyFormat("int operator++();");
  4778. verifyFormat("bool operator();");
  4779. verifyFormat("bool operator()();");
  4780. verifyFormat("bool operator[]();");
  4781. verifyFormat("operator bool();");
  4782. verifyFormat("operator int();");
  4783. verifyFormat("operator void *();");
  4784. verifyFormat("operator SomeType<int>();");
  4785. verifyFormat("operator SomeType<int, int>();");
  4786. verifyFormat("operator SomeType<SomeType<int>>();");
  4787. verifyFormat("void *operator new(std::size_t size);");
  4788. verifyFormat("void *operator new[](std::size_t size);");
  4789. verifyFormat("void operator delete(void *ptr);");
  4790. verifyFormat("void operator delete[](void *ptr);");
  4791. verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
  4792. "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
  4793. verifyFormat(
  4794. "ostream &operator<<(ostream &OutputStream,\n"
  4795. " SomeReallyLongType WithSomeReallyLongValue);");
  4796. verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
  4797. " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
  4798. " return left.group < right.group;\n"
  4799. "}");
  4800. verifyFormat("SomeType &operator=(const SomeType &S);");
  4801. verifyFormat("f.template operator()<int>();");
  4802. verifyGoogleFormat("operator void*();");
  4803. verifyGoogleFormat("operator SomeType<SomeType<int>>();");
  4804. verifyGoogleFormat("operator ::A();");
  4805. verifyFormat("using A::operator+;");
  4806. verifyFormat("string // break\n"
  4807. "operator()() & {}");
  4808. verifyFormat("string // break\n"
  4809. "operator()() && {}");
  4810. }
  4811. TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
  4812. verifyFormat("Deleted &operator=(const Deleted &)& = default;");
  4813. verifyFormat("Deleted &operator=(const Deleted &)&& = delete;");
  4814. verifyFormat("SomeType MemberFunction(const Deleted &)& = delete;");
  4815. verifyFormat("SomeType MemberFunction(const Deleted &)&& = delete;");
  4816. verifyFormat("Deleted &operator=(const Deleted &)&;");
  4817. verifyFormat("Deleted &operator=(const Deleted &)&&;");
  4818. verifyFormat("SomeType MemberFunction(const Deleted &)&;");
  4819. verifyFormat("SomeType MemberFunction(const Deleted &)&&;");
  4820. verifyGoogleFormat("Deleted& operator=(const Deleted&)& = default;");
  4821. verifyGoogleFormat("SomeType MemberFunction(const Deleted&)& = delete;");
  4822. verifyGoogleFormat("Deleted& operator=(const Deleted&)&;");
  4823. verifyGoogleFormat("SomeType MemberFunction(const Deleted&)&;");
  4824. FormatStyle Spaces = getLLVMStyle();
  4825. Spaces.SpacesInCStyleCastParentheses = true;
  4826. verifyFormat("Deleted &operator=(const Deleted &)& = default;", Spaces);
  4827. verifyFormat("SomeType MemberFunction(const Deleted &)& = delete;", Spaces);
  4828. verifyFormat("Deleted &operator=(const Deleted &)&;", Spaces);
  4829. verifyFormat("SomeType MemberFunction(const Deleted &)&;", Spaces);
  4830. Spaces.SpacesInCStyleCastParentheses = false;
  4831. Spaces.SpacesInParentheses = true;
  4832. verifyFormat("Deleted &operator=( const Deleted & )& = default;", Spaces);
  4833. verifyFormat("SomeType MemberFunction( const Deleted & )& = delete;", Spaces);
  4834. verifyFormat("Deleted &operator=( const Deleted & )&;", Spaces);
  4835. verifyFormat("SomeType MemberFunction( const Deleted & )&;", Spaces);
  4836. }
  4837. TEST_F(FormatTest, UnderstandsNewAndDelete) {
  4838. verifyFormat("void f() {\n"
  4839. " A *a = new A;\n"
  4840. " A *a = new (placement) A;\n"
  4841. " delete a;\n"
  4842. " delete (A *)a;\n"
  4843. "}");
  4844. verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
  4845. " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
  4846. verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4847. " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
  4848. " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
  4849. verifyFormat("delete[] h->p;");
  4850. }
  4851. TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
  4852. verifyFormat("int *f(int *a) {}");
  4853. verifyFormat("int main(int argc, char **argv) {}");
  4854. verifyFormat("Test::Test(int b) : a(b * b) {}");
  4855. verifyIndependentOfContext("f(a, *a);");
  4856. verifyFormat("void g() { f(*a); }");
  4857. verifyIndependentOfContext("int a = b * 10;");
  4858. verifyIndependentOfContext("int a = 10 * b;");
  4859. verifyIndependentOfContext("int a = b * c;");
  4860. verifyIndependentOfContext("int a += b * c;");
  4861. verifyIndependentOfContext("int a -= b * c;");
  4862. verifyIndependentOfContext("int a *= b * c;");
  4863. verifyIndependentOfContext("int a /= b * c;");
  4864. verifyIndependentOfContext("int a = *b;");
  4865. verifyIndependentOfContext("int a = *b * c;");
  4866. verifyIndependentOfContext("int a = b * *c;");
  4867. verifyIndependentOfContext("return 10 * b;");
  4868. verifyIndependentOfContext("return *b * *c;");
  4869. verifyIndependentOfContext("return a & ~b;");
  4870. verifyIndependentOfContext("f(b ? *c : *d);");
  4871. verifyIndependentOfContext("int a = b ? *c : *d;");
  4872. verifyIndependentOfContext("*b = a;");
  4873. verifyIndependentOfContext("a * ~b;");
  4874. verifyIndependentOfContext("a * !b;");
  4875. verifyIndependentOfContext("a * +b;");
  4876. verifyIndependentOfContext("a * -b;");
  4877. verifyIndependentOfContext("a * ++b;");
  4878. verifyIndependentOfContext("a * --b;");
  4879. verifyIndependentOfContext("a[4] * b;");
  4880. verifyIndependentOfContext("a[a * a] = 1;");
  4881. verifyIndependentOfContext("f() * b;");
  4882. verifyIndependentOfContext("a * [self dostuff];");
  4883. verifyIndependentOfContext("int x = a * (a + b);");
  4884. verifyIndependentOfContext("(a *)(a + b);");
  4885. verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
  4886. verifyIndependentOfContext("int *pa = (int *)&a;");
  4887. verifyIndependentOfContext("return sizeof(int **);");
  4888. verifyIndependentOfContext("return sizeof(int ******);");
  4889. verifyIndependentOfContext("return (int **&)a;");
  4890. verifyIndependentOfContext("f((*PointerToArray)[10]);");
  4891. verifyFormat("void f(Type (*parameter)[10]) {}");
  4892. verifyGoogleFormat("return sizeof(int**);");
  4893. verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
  4894. verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
  4895. verifyFormat("auto a = [](int **&, int ***) {};");
  4896. verifyFormat("auto PointerBinding = [](const char *S) {};");
  4897. verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
  4898. verifyFormat("[](const decltype(*a) &value) {}");
  4899. verifyIndependentOfContext("typedef void (*f)(int *a);");
  4900. verifyIndependentOfContext("int i{a * b};");
  4901. verifyIndependentOfContext("aaa && aaa->f();");
  4902. verifyIndependentOfContext("int x = ~*p;");
  4903. verifyFormat("Constructor() : a(a), area(width * height) {}");
  4904. verifyFormat("Constructor() : a(a), area(a, width * height) {}");
  4905. verifyFormat("void f() { f(a, c * d); }");
  4906. verifyIndependentOfContext("InvalidRegions[*R] = 0;");
  4907. verifyIndependentOfContext("A<int *> a;");
  4908. verifyIndependentOfContext("A<int **> a;");
  4909. verifyIndependentOfContext("A<int *, int *> a;");
  4910. verifyIndependentOfContext("A<int *[]> a;");
  4911. verifyIndependentOfContext(
  4912. "const char *const p = reinterpret_cast<const char *const>(q);");
  4913. verifyIndependentOfContext("A<int **, int **> a;");
  4914. verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
  4915. verifyFormat("for (char **a = b; *a; ++a) {\n}");
  4916. verifyFormat("for (; a && b;) {\n}");
  4917. verifyFormat("bool foo = true && [] { return false; }();");
  4918. verifyFormat(
  4919. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4920. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4921. verifyGoogleFormat("**outparam = 1;");
  4922. verifyGoogleFormat("*outparam = a * b;");
  4923. verifyGoogleFormat("int main(int argc, char** argv) {}");
  4924. verifyGoogleFormat("A<int*> a;");
  4925. verifyGoogleFormat("A<int**> a;");
  4926. verifyGoogleFormat("A<int*, int*> a;");
  4927. verifyGoogleFormat("A<int**, int**> a;");
  4928. verifyGoogleFormat("f(b ? *c : *d);");
  4929. verifyGoogleFormat("int a = b ? *c : *d;");
  4930. verifyGoogleFormat("Type* t = **x;");
  4931. verifyGoogleFormat("Type* t = *++*x;");
  4932. verifyGoogleFormat("*++*x;");
  4933. verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
  4934. verifyGoogleFormat("Type* t = x++ * y;");
  4935. verifyGoogleFormat(
  4936. "const char* const p = reinterpret_cast<const char* const>(q);");
  4937. verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
  4938. verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
  4939. verifyGoogleFormat("template <typename T>\n"
  4940. "void f(int i = 0, SomeType** temps = NULL);");
  4941. FormatStyle Left = getLLVMStyle();
  4942. Left.PointerAlignment = FormatStyle::PAS_Left;
  4943. verifyFormat("x = *a(x) = *a(y);", Left);
  4944. verifyIndependentOfContext("a = *(x + y);");
  4945. verifyIndependentOfContext("a = &(x + y);");
  4946. verifyIndependentOfContext("*(x + y).call();");
  4947. verifyIndependentOfContext("&(x + y)->call();");
  4948. verifyFormat("void f() { &(*I).first; }");
  4949. verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
  4950. verifyFormat(
  4951. "int *MyValues = {\n"
  4952. " *A, // Operator detection might be confused by the '{'\n"
  4953. " *BB // Operator detection might be confused by previous comment\n"
  4954. "};");
  4955. verifyIndependentOfContext("if (int *a = &b)");
  4956. verifyIndependentOfContext("if (int &a = *b)");
  4957. verifyIndependentOfContext("if (a & b[i])");
  4958. verifyIndependentOfContext("if (a::b::c::d & b[i])");
  4959. verifyIndependentOfContext("if (*b[i])");
  4960. verifyIndependentOfContext("if (int *a = (&b))");
  4961. verifyIndependentOfContext("while (int *a = &b)");
  4962. verifyIndependentOfContext("size = sizeof *a;");
  4963. verifyFormat("void f() {\n"
  4964. " for (const int &v : Values) {\n"
  4965. " }\n"
  4966. "}");
  4967. verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
  4968. verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
  4969. verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
  4970. verifyFormat("#define A (!a * b)");
  4971. verifyFormat("#define MACRO \\\n"
  4972. " int *i = a * b; \\\n"
  4973. " void f(a *b);",
  4974. getLLVMStyleWithColumns(19));
  4975. verifyIndependentOfContext("A = new SomeType *[Length];");
  4976. verifyIndependentOfContext("A = new SomeType *[Length]();");
  4977. verifyIndependentOfContext("T **t = new T *;");
  4978. verifyIndependentOfContext("T **t = new T *();");
  4979. verifyGoogleFormat("A = new SomeType* [Length]();");
  4980. verifyGoogleFormat("A = new SomeType* [Length];");
  4981. verifyGoogleFormat("T** t = new T*;");
  4982. verifyGoogleFormat("T** t = new T*();");
  4983. FormatStyle PointerLeft = getLLVMStyle();
  4984. PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
  4985. verifyFormat("delete *x;", PointerLeft);
  4986. verifyFormat("STATIC_ASSERT((a & b) == 0);");
  4987. verifyFormat("STATIC_ASSERT(0 == (a & b));");
  4988. verifyFormat("template <bool a, bool b> "
  4989. "typename t::if<x && y>::type f() {}");
  4990. verifyFormat("template <int *y> f() {}");
  4991. verifyFormat("vector<int *> v;");
  4992. verifyFormat("vector<int *const> v;");
  4993. verifyFormat("vector<int *const **const *> v;");
  4994. verifyFormat("vector<int *volatile> v;");
  4995. verifyFormat("vector<a * b> v;");
  4996. verifyFormat("foo<b && false>();");
  4997. verifyFormat("foo<b & 1>();");
  4998. verifyFormat("decltype(*::std::declval<const T &>()) void F();");
  4999. verifyFormat(
  5000. "template <class T, class = typename std::enable_if<\n"
  5001. " std::is_integral<T>::value &&\n"
  5002. " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
  5003. "void F();",
  5004. getLLVMStyleWithColumns(76));
  5005. verifyFormat(
  5006. "template <class T,\n"
  5007. " class = typename ::std::enable_if<\n"
  5008. " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
  5009. "void F();",
  5010. getGoogleStyleWithColumns(68));
  5011. verifyIndependentOfContext("MACRO(int *i);");
  5012. verifyIndependentOfContext("MACRO(auto *a);");
  5013. verifyIndependentOfContext("MACRO(const A *a);");
  5014. verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
  5015. // FIXME: Is there a way to make this work?
  5016. // verifyIndependentOfContext("MACRO(A *a);");
  5017. verifyFormat("DatumHandle const *operator->() const { return input_; }");
  5018. EXPECT_EQ("#define OP(x) \\\n"
  5019. " ostream &operator<<(ostream &s, const A &a) { \\\n"
  5020. " return s << a.DebugString(); \\\n"
  5021. " }",
  5022. format("#define OP(x) \\\n"
  5023. " ostream &operator<<(ostream &s, const A &a) { \\\n"
  5024. " return s << a.DebugString(); \\\n"
  5025. " }",
  5026. getLLVMStyleWithColumns(50)));
  5027. // FIXME: We cannot handle this case yet; we might be able to figure out that
  5028. // foo<x> d > v; doesn't make sense.
  5029. verifyFormat("foo<a<b && c> d> v;");
  5030. FormatStyle PointerMiddle = getLLVMStyle();
  5031. PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
  5032. verifyFormat("delete *x;", PointerMiddle);
  5033. verifyFormat("int * x;", PointerMiddle);
  5034. verifyFormat("template <int * y> f() {}", PointerMiddle);
  5035. verifyFormat("int * f(int * a) {}", PointerMiddle);
  5036. verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
  5037. verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
  5038. verifyFormat("A<int *> a;", PointerMiddle);
  5039. verifyFormat("A<int **> a;", PointerMiddle);
  5040. verifyFormat("A<int *, int *> a;", PointerMiddle);
  5041. verifyFormat("A<int * []> a;", PointerMiddle);
  5042. verifyFormat("A = new SomeType * [Length]();", PointerMiddle);
  5043. verifyFormat("A = new SomeType * [Length];", PointerMiddle);
  5044. verifyFormat("T ** t = new T *;", PointerMiddle);
  5045. }
  5046. TEST_F(FormatTest, UnderstandsAttributes) {
  5047. verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
  5048. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
  5049. "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
  5050. }
  5051. TEST_F(FormatTest, UnderstandsEllipsis) {
  5052. verifyFormat("int printf(const char *fmt, ...);");
  5053. verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
  5054. verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
  5055. FormatStyle PointersLeft = getLLVMStyle();
  5056. PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
  5057. verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
  5058. }
  5059. TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
  5060. EXPECT_EQ("int *a;\n"
  5061. "int *a;\n"
  5062. "int *a;",
  5063. format("int *a;\n"
  5064. "int* a;\n"
  5065. "int *a;",
  5066. getGoogleStyle()));
  5067. EXPECT_EQ("int* a;\n"
  5068. "int* a;\n"
  5069. "int* a;",
  5070. format("int* a;\n"
  5071. "int* a;\n"
  5072. "int *a;",
  5073. getGoogleStyle()));
  5074. EXPECT_EQ("int *a;\n"
  5075. "int *a;\n"
  5076. "int *a;",
  5077. format("int *a;\n"
  5078. "int * a;\n"
  5079. "int * a;",
  5080. getGoogleStyle()));
  5081. }
  5082. TEST_F(FormatTest, UnderstandsRvalueReferences) {
  5083. verifyFormat("int f(int &&a) {}");
  5084. verifyFormat("int f(int a, char &&b) {}");
  5085. verifyFormat("void f() { int &&a = b; }");
  5086. verifyGoogleFormat("int f(int a, char&& b) {}");
  5087. verifyGoogleFormat("void f() { int&& a = b; }");
  5088. verifyIndependentOfContext("A<int &&> a;");
  5089. verifyIndependentOfContext("A<int &&, int &&> a;");
  5090. verifyGoogleFormat("A<int&&> a;");
  5091. verifyGoogleFormat("A<int&&, int&&> a;");
  5092. // Not rvalue references:
  5093. verifyFormat("template <bool B, bool C> class A {\n"
  5094. " static_assert(B && C, \"Something is wrong\");\n"
  5095. "};");
  5096. verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
  5097. verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
  5098. verifyFormat("#define A(a, b) (a && b)");
  5099. }
  5100. TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
  5101. verifyFormat("void f() {\n"
  5102. " x[aaaaaaaaa -\n"
  5103. " b] = 23;\n"
  5104. "}",
  5105. getLLVMStyleWithColumns(15));
  5106. }
  5107. TEST_F(FormatTest, FormatsCasts) {
  5108. verifyFormat("Type *A = static_cast<Type *>(P);");
  5109. verifyFormat("Type *A = (Type *)P;");
  5110. verifyFormat("Type *A = (vector<Type *, int *>)P;");
  5111. verifyFormat("int a = (int)(2.0f);");
  5112. verifyFormat("int a = (int)2.0f;");
  5113. verifyFormat("x[(int32)y];");
  5114. verifyFormat("x = (int32)y;");
  5115. verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
  5116. verifyFormat("int a = (int)*b;");
  5117. verifyFormat("int a = (int)2.0f;");
  5118. verifyFormat("int a = (int)~0;");
  5119. verifyFormat("int a = (int)++a;");
  5120. verifyFormat("int a = (int)sizeof(int);");
  5121. verifyFormat("int a = (int)+2;");
  5122. verifyFormat("my_int a = (my_int)2.0f;");
  5123. verifyFormat("my_int a = (my_int)sizeof(int);");
  5124. verifyFormat("return (my_int)aaa;");
  5125. verifyFormat("#define x ((int)-1)");
  5126. verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
  5127. verifyFormat("#define p(q) ((int *)&q)");
  5128. verifyFormat("fn(a)(b) + 1;");
  5129. verifyFormat("void f() { my_int a = (my_int)*b; }");
  5130. verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
  5131. verifyFormat("my_int a = (my_int)~0;");
  5132. verifyFormat("my_int a = (my_int)++a;");
  5133. verifyFormat("my_int a = (my_int)-2;");
  5134. verifyFormat("my_int a = (my_int)1;");
  5135. verifyFormat("my_int a = (my_int *)1;");
  5136. verifyFormat("my_int a = (const my_int)-1;");
  5137. verifyFormat("my_int a = (const my_int *)-1;");
  5138. verifyFormat("my_int a = (my_int)(my_int)-1;");
  5139. // FIXME: single value wrapped with paren will be treated as cast.
  5140. verifyFormat("void f(int i = (kValue)*kMask) {}");
  5141. verifyFormat("{ (void)F; }");
  5142. // Don't break after a cast's
  5143. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  5144. " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
  5145. " bbbbbbbbbbbbbbbbbbbbbb);");
  5146. // These are not casts.
  5147. verifyFormat("void f(int *) {}");
  5148. verifyFormat("f(foo)->b;");
  5149. verifyFormat("f(foo).b;");
  5150. verifyFormat("f(foo)(b);");
  5151. verifyFormat("f(foo)[b];");
  5152. verifyFormat("[](foo) { return 4; }(bar);");
  5153. verifyFormat("(*funptr)(foo)[4];");
  5154. verifyFormat("funptrs[4](foo)[4];");
  5155. verifyFormat("void f(int *);");
  5156. verifyFormat("void f(int *) = 0;");
  5157. verifyFormat("void f(SmallVector<int>) {}");
  5158. verifyFormat("void f(SmallVector<int>);");
  5159. verifyFormat("void f(SmallVector<int>) = 0;");
  5160. verifyFormat("void f(int i = (kA * kB) & kMask) {}");
  5161. verifyFormat("int a = sizeof(int) * b;");
  5162. verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
  5163. verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
  5164. verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
  5165. verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
  5166. // These are not casts, but at some point were confused with casts.
  5167. verifyFormat("virtual void foo(int *) override;");
  5168. verifyFormat("virtual void foo(char &) const;");
  5169. verifyFormat("virtual void foo(int *a, char *) const;");
  5170. verifyFormat("int a = sizeof(int *) + b;");
  5171. verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
  5172. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
  5173. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  5174. // FIXME: The indentation here is not ideal.
  5175. verifyFormat(
  5176. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5177. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
  5178. " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
  5179. }
  5180. TEST_F(FormatTest, FormatsFunctionTypes) {
  5181. verifyFormat("A<bool()> a;");
  5182. verifyFormat("A<SomeType()> a;");
  5183. verifyFormat("A<void (*)(int, std::string)> a;");
  5184. verifyFormat("A<void *(int)>;");
  5185. verifyFormat("void *(*a)(int *, SomeType *);");
  5186. verifyFormat("int (*func)(void *);");
  5187. verifyFormat("void f() { int (*func)(void *); }");
  5188. verifyFormat("template <class CallbackClass>\n"
  5189. "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
  5190. verifyGoogleFormat("A<void*(int*, SomeType*)>;");
  5191. verifyGoogleFormat("void* (*a)(int);");
  5192. verifyGoogleFormat(
  5193. "template <class CallbackClass>\n"
  5194. "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
  5195. // Other constructs can look somewhat like function types:
  5196. verifyFormat("A<sizeof(*x)> a;");
  5197. verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
  5198. verifyFormat("some_var = function(*some_pointer_var)[0];");
  5199. verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
  5200. }
  5201. TEST_F(FormatTest, FormatsPointersToArrayTypes) {
  5202. verifyFormat("A (*foo_)[6];");
  5203. verifyFormat("vector<int> (*foo_)[6];");
  5204. }
  5205. TEST_F(FormatTest, BreaksLongVariableDeclarations) {
  5206. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5207. " LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5208. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
  5209. " LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5210. // Different ways of ()-initializiation.
  5211. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5212. " LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
  5213. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5214. " LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
  5215. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5216. " LoooooooooooooooooooooooooooooooooooooooongVariable({});");
  5217. }
  5218. TEST_F(FormatTest, BreaksLongDeclarations) {
  5219. verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
  5220. " AnotherNameForTheLongType;");
  5221. verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
  5222. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  5223. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5224. "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
  5225. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5226. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5227. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
  5228. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5229. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  5230. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5231. verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
  5232. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5233. FormatStyle Indented = getLLVMStyle();
  5234. Indented.IndentWrappedFunctionNames = true;
  5235. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5236. " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
  5237. Indented);
  5238. verifyFormat(
  5239. "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5240. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5241. Indented);
  5242. verifyFormat(
  5243. "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  5244. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5245. Indented);
  5246. verifyFormat(
  5247. "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
  5248. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5249. Indented);
  5250. // FIXME: Without the comment, this breaks after "(".
  5251. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
  5252. " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
  5253. getGoogleStyle());
  5254. verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
  5255. " int LoooooooooooooooooooongParam2) {}");
  5256. verifyFormat(
  5257. "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
  5258. " SourceLocation L, IdentifierIn *II,\n"
  5259. " Type *T) {}");
  5260. verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
  5261. "ReallyReallyLongFunctionName(\n"
  5262. " const std::string &SomeParameter,\n"
  5263. " const SomeType<string, SomeOtherTemplateParameter> &\n"
  5264. " ReallyReallyLongParameterName,\n"
  5265. " const SomeType<string, SomeOtherTemplateParameter> &\n"
  5266. " AnotherLongParameterName) {}");
  5267. verifyFormat("template <typename A>\n"
  5268. "SomeLoooooooooooooooooooooongType<\n"
  5269. " typename some_namespace::SomeOtherType<A>::Type>\n"
  5270. "Function() {}");
  5271. verifyGoogleFormat(
  5272. "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
  5273. " aaaaaaaaaaaaaaaaaaaaaaa;");
  5274. verifyGoogleFormat(
  5275. "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
  5276. " SourceLocation L) {}");
  5277. verifyGoogleFormat(
  5278. "some_namespace::LongReturnType\n"
  5279. "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
  5280. " int first_long_parameter, int second_parameter) {}");
  5281. verifyGoogleFormat("template <typename T>\n"
  5282. "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  5283. "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
  5284. verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5285. " int aaaaaaaaaaaaaaaaaaaaaaa);");
  5286. verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  5287. " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  5288. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5289. }
  5290. TEST_F(FormatTest, FormatsArrays) {
  5291. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5292. " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
  5293. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5294. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  5295. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5296. " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
  5297. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5298. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5299. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  5300. verifyFormat(
  5301. "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
  5302. " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5303. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  5304. verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
  5305. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
  5306. verifyFormat(
  5307. "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
  5308. " .aaaaaaa[0]\n"
  5309. " .aaaaaaaaaaaaaaaaaaaaaa();");
  5310. verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
  5311. }
  5312. TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
  5313. verifyFormat("(a)->b();");
  5314. verifyFormat("--a;");
  5315. }
  5316. TEST_F(FormatTest, HandlesIncludeDirectives) {
  5317. verifyFormat("#include <string>\n"
  5318. "#include <a/b/c.h>\n"
  5319. "#include \"a/b/string\"\n"
  5320. "#include \"string.h\"\n"
  5321. "#include \"string.h\"\n"
  5322. "#include <a-a>\n"
  5323. "#include < path with space >\n"
  5324. "#include \"abc.h\" // this is included for ABC\n"
  5325. "#include \"some long include\" // with a comment\n"
  5326. "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
  5327. getLLVMStyleWithColumns(35));
  5328. EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\""));
  5329. EXPECT_EQ("#include <a>", format("#include<a>"));
  5330. verifyFormat("#import <string>");
  5331. verifyFormat("#import <a/b/c.h>");
  5332. verifyFormat("#import \"a/b/string\"");
  5333. verifyFormat("#import \"string.h\"");
  5334. verifyFormat("#import \"string.h\"");
  5335. verifyFormat("#if __has_include(<strstream>)\n"
  5336. "#include <strstream>\n"
  5337. "#endif");
  5338. verifyFormat("#define MY_IMPORT <a/b>");
  5339. // Protocol buffer definition or missing "#".
  5340. verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
  5341. getLLVMStyleWithColumns(30));
  5342. FormatStyle Style = getLLVMStyle();
  5343. Style.AlwaysBreakBeforeMultilineStrings = true;
  5344. Style.ColumnLimit = 0;
  5345. verifyFormat("#import \"abc.h\"", Style);
  5346. // But 'import' might also be a regular C++ namespace.
  5347. verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5348. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5349. }
  5350. //===----------------------------------------------------------------------===//
  5351. // Error recovery tests.
  5352. //===----------------------------------------------------------------------===//
  5353. TEST_F(FormatTest, IncompleteParameterLists) {
  5354. FormatStyle NoBinPacking = getLLVMStyle();
  5355. NoBinPacking.BinPackParameters = false;
  5356. verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
  5357. " double *min_x,\n"
  5358. " double *max_x,\n"
  5359. " double *min_y,\n"
  5360. " double *max_y,\n"
  5361. " double *min_z,\n"
  5362. " double *max_z, ) {}",
  5363. NoBinPacking);
  5364. }
  5365. TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
  5366. verifyFormat("void f() { return; }\n42");
  5367. verifyFormat("void f() {\n"
  5368. " if (0)\n"
  5369. " return;\n"
  5370. "}\n"
  5371. "42");
  5372. verifyFormat("void f() { return }\n42");
  5373. verifyFormat("void f() {\n"
  5374. " if (0)\n"
  5375. " return\n"
  5376. "}\n"
  5377. "42");
  5378. }
  5379. TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
  5380. EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
  5381. EXPECT_EQ("void f() {\n"
  5382. " if (a)\n"
  5383. " return\n"
  5384. "}",
  5385. format("void f ( ) { if ( a ) return }"));
  5386. EXPECT_EQ("namespace N {\n"
  5387. "void f()\n"
  5388. "}",
  5389. format("namespace N { void f() }"));
  5390. EXPECT_EQ("namespace N {\n"
  5391. "void f() {}\n"
  5392. "void g()\n"
  5393. "}",
  5394. format("namespace N { void f( ) { } void g( ) }"));
  5395. }
  5396. TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
  5397. verifyFormat("int aaaaaaaa =\n"
  5398. " // Overlylongcomment\n"
  5399. " b;",
  5400. getLLVMStyleWithColumns(20));
  5401. verifyFormat("function(\n"
  5402. " ShortArgument,\n"
  5403. " LoooooooooooongArgument);\n",
  5404. getLLVMStyleWithColumns(20));
  5405. }
  5406. TEST_F(FormatTest, IncorrectAccessSpecifier) {
  5407. verifyFormat("public:");
  5408. verifyFormat("class A {\n"
  5409. "public\n"
  5410. " void f() {}\n"
  5411. "};");
  5412. verifyFormat("public\n"
  5413. "int qwerty;");
  5414. verifyFormat("public\n"
  5415. "B {}");
  5416. verifyFormat("public\n"
  5417. "{}");
  5418. verifyFormat("public\n"
  5419. "B { int x; }");
  5420. }
  5421. TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
  5422. verifyFormat("{");
  5423. verifyFormat("#})");
  5424. verifyNoCrash("(/**/[:!] ?[).");
  5425. }
  5426. TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
  5427. verifyFormat("do {\n}");
  5428. verifyFormat("do {\n}\n"
  5429. "f();");
  5430. verifyFormat("do {\n}\n"
  5431. "wheeee(fun);");
  5432. verifyFormat("do {\n"
  5433. " f();\n"
  5434. "}");
  5435. }
  5436. TEST_F(FormatTest, IncorrectCodeMissingParens) {
  5437. verifyFormat("if {\n foo;\n foo();\n}");
  5438. verifyFormat("switch {\n foo;\n foo();\n}");
  5439. verifyFormat("for {\n foo;\n foo();\n}");
  5440. verifyFormat("while {\n foo;\n foo();\n}");
  5441. verifyFormat("do {\n foo;\n foo();\n} while;");
  5442. }
  5443. TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
  5444. verifyFormat("namespace {\n"
  5445. "class Foo { Foo (\n"
  5446. "};\n"
  5447. "} // comment");
  5448. }
  5449. TEST_F(FormatTest, IncorrectCodeErrorDetection) {
  5450. EXPECT_EQ("{\n {}\n", format("{\n{\n}\n"));
  5451. EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
  5452. EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
  5453. EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n"));
  5454. EXPECT_EQ("{\n"
  5455. " {\n"
  5456. " breakme(\n"
  5457. " qwe);\n"
  5458. " }\n",
  5459. format("{\n"
  5460. " {\n"
  5461. " breakme(qwe);\n"
  5462. "}\n",
  5463. getLLVMStyleWithColumns(10)));
  5464. }
  5465. TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
  5466. verifyFormat("int x = {\n"
  5467. " avariable,\n"
  5468. " b(alongervariable)};",
  5469. getLLVMStyleWithColumns(25));
  5470. }
  5471. TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
  5472. verifyFormat("return (a)(b){1, 2, 3};");
  5473. }
  5474. TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
  5475. verifyFormat("vector<int> x{1, 2, 3, 4};");
  5476. verifyFormat("vector<int> x{\n"
  5477. " 1, 2, 3, 4,\n"
  5478. "};");
  5479. verifyFormat("vector<T> x{{}, {}, {}, {}};");
  5480. verifyFormat("f({1, 2});");
  5481. verifyFormat("auto v = Foo{-1};");
  5482. verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
  5483. verifyFormat("Class::Class : member{1, 2, 3} {}");
  5484. verifyFormat("new vector<int>{1, 2, 3};");
  5485. verifyFormat("new int[3]{1, 2, 3};");
  5486. verifyFormat("new int{1};");
  5487. verifyFormat("return {arg1, arg2};");
  5488. verifyFormat("return {arg1, SomeType{parameter}};");
  5489. verifyFormat("int count = set<int>{f(), g(), h()}.size();");
  5490. verifyFormat("new T{arg1, arg2};");
  5491. verifyFormat("f(MyMap[{composite, key}]);");
  5492. verifyFormat("class Class {\n"
  5493. " T member = {arg1, arg2};\n"
  5494. "};");
  5495. verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
  5496. verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
  5497. verifyFormat("int a = std::is_integral<int>{} + 0;");
  5498. verifyFormat("int foo(int i) { return fo1{}(i); }");
  5499. verifyFormat("int foo(int i) { return fo1{}(i); }");
  5500. verifyFormat("auto i = decltype(x){};");
  5501. verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
  5502. verifyFormat("Node n{1, Node{1000}, //\n"
  5503. " 2};");
  5504. verifyFormat("Aaaa aaaaaaa{\n"
  5505. " {\n"
  5506. " aaaa,\n"
  5507. " },\n"
  5508. "};");
  5509. // In combination with BinPackParameters = false.
  5510. FormatStyle NoBinPacking = getLLVMStyle();
  5511. NoBinPacking.BinPackParameters = false;
  5512. verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
  5513. " bbbbb,\n"
  5514. " ccccc,\n"
  5515. " ddddd,\n"
  5516. " eeeee,\n"
  5517. " ffffff,\n"
  5518. " ggggg,\n"
  5519. " hhhhhh,\n"
  5520. " iiiiii,\n"
  5521. " jjjjjj,\n"
  5522. " kkkkkk};",
  5523. NoBinPacking);
  5524. verifyFormat("const Aaaaaa aaaaa = {\n"
  5525. " aaaaa,\n"
  5526. " bbbbb,\n"
  5527. " ccccc,\n"
  5528. " ddddd,\n"
  5529. " eeeee,\n"
  5530. " ffffff,\n"
  5531. " ggggg,\n"
  5532. " hhhhhh,\n"
  5533. " iiiiii,\n"
  5534. " jjjjjj,\n"
  5535. " kkkkkk,\n"
  5536. "};",
  5537. NoBinPacking);
  5538. verifyFormat(
  5539. "const Aaaaaa aaaaa = {\n"
  5540. " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n"
  5541. " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n"
  5542. " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
  5543. "};",
  5544. NoBinPacking);
  5545. // FIXME: The alignment of these trailing comments might be bad. Then again,
  5546. // this might be utterly useless in real code.
  5547. verifyFormat("Constructor::Constructor()\n"
  5548. " : some_value{ //\n"
  5549. " aaaaaaa, //\n"
  5550. " bbbbbbb} {}");
  5551. // In braced lists, the first comment is always assumed to belong to the
  5552. // first element. Thus, it can be moved to the next or previous line as
  5553. // appropriate.
  5554. EXPECT_EQ("function({// First element:\n"
  5555. " 1,\n"
  5556. " // Second element:\n"
  5557. " 2});",
  5558. format("function({\n"
  5559. " // First element:\n"
  5560. " 1,\n"
  5561. " // Second element:\n"
  5562. " 2});"));
  5563. EXPECT_EQ("std::vector<int> MyNumbers{\n"
  5564. " // First element:\n"
  5565. " 1,\n"
  5566. " // Second element:\n"
  5567. " 2};",
  5568. format("std::vector<int> MyNumbers{// First element:\n"
  5569. " 1,\n"
  5570. " // Second element:\n"
  5571. " 2};",
  5572. getLLVMStyleWithColumns(30)));
  5573. // A trailing comma should still lead to an enforced line break.
  5574. EXPECT_EQ("vector<int> SomeVector = {\n"
  5575. " // aaa\n"
  5576. " 1, 2,\n"
  5577. "};",
  5578. format("vector<int> SomeVector = { // aaa\n"
  5579. " 1, 2, };"));
  5580. FormatStyle ExtraSpaces = getLLVMStyle();
  5581. ExtraSpaces.Cpp11BracedListStyle = false;
  5582. ExtraSpaces.ColumnLimit = 75;
  5583. verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
  5584. verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
  5585. verifyFormat("f({ 1, 2 });", ExtraSpaces);
  5586. verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
  5587. verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
  5588. verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
  5589. verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
  5590. verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
  5591. verifyFormat("return { arg1, arg2 };", ExtraSpaces);
  5592. verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
  5593. verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
  5594. verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
  5595. verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
  5596. verifyFormat("class Class {\n"
  5597. " T member = { arg1, arg2 };\n"
  5598. "};",
  5599. ExtraSpaces);
  5600. verifyFormat(
  5601. "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5602. " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
  5603. " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  5604. " bbbbbbbbbbbbbbbbbbbb, bbbbb };",
  5605. ExtraSpaces);
  5606. verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
  5607. verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
  5608. ExtraSpaces);
  5609. verifyFormat(
  5610. "someFunction(OtherParam,\n"
  5611. " BracedList{ // comment 1 (Forcing interesting break)\n"
  5612. " param1, param2,\n"
  5613. " // comment 2\n"
  5614. " param3, param4 });",
  5615. ExtraSpaces);
  5616. verifyFormat(
  5617. "std::this_thread::sleep_for(\n"
  5618. " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
  5619. ExtraSpaces);
  5620. verifyFormat(
  5621. "std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
  5622. " aaaaaaa, aaaaaaaaaa, aaaaa, aaaaaaaaaaaaaaa, aaa, aaaaaaaaaa, a,\n"
  5623. " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaa,\n"
  5624. " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa, aaaaaaa, a};");
  5625. verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
  5626. }
  5627. TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
  5628. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5629. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5630. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5631. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5632. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5633. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  5634. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5635. " // line comment\n"
  5636. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5637. " 1, 22, 333, 4444, 55555,\n"
  5638. " // line comment\n"
  5639. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5640. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  5641. verifyFormat(
  5642. "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5643. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5644. " 1, 22, 333, 4444, 55555, 666666, // comment\n"
  5645. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  5646. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  5647. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  5648. " 7777777};");
  5649. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  5650. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  5651. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  5652. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  5653. " 1, 1, 1, 1};",
  5654. getLLVMStyleWithColumns(39));
  5655. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  5656. " 1, 1, 1, 1};",
  5657. getLLVMStyleWithColumns(38));
  5658. verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
  5659. " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
  5660. getLLVMStyleWithColumns(43));
  5661. // Trailing commas.
  5662. verifyFormat("vector<int> x = {\n"
  5663. " 1, 1, 1, 1, 1, 1, 1, 1,\n"
  5664. "};",
  5665. getLLVMStyleWithColumns(39));
  5666. verifyFormat("vector<int> x = {\n"
  5667. " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
  5668. "};",
  5669. getLLVMStyleWithColumns(39));
  5670. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  5671. " 1, 1, 1, 1,\n"
  5672. " /**/ /**/};",
  5673. getLLVMStyleWithColumns(39));
  5674. verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
  5675. " {aaaaaaaaaaaaaaaaaaa},\n"
  5676. " {aaaaaaaaaaaaaaaaaaaaa},\n"
  5677. " {aaaaaaaaaaaaaaaaa}};",
  5678. getLLVMStyleWithColumns(60));
  5679. // With nested lists, we should either format one item per line or all nested
  5680. // lists one one line.
  5681. // FIXME: For some nested lists, we can do better.
  5682. verifyFormat(
  5683. "SomeStruct my_struct_array = {\n"
  5684. " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
  5685. " aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
  5686. " {aaa, aaa},\n"
  5687. " {aaa, aaa},\n"
  5688. " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
  5689. " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5690. " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
  5691. // No column layout should be used here.
  5692. verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
  5693. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
  5694. verifyNoCrash("a<,");
  5695. }
  5696. TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
  5697. FormatStyle DoNotMerge = getLLVMStyle();
  5698. DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  5699. verifyFormat("void f() { return 42; }");
  5700. verifyFormat("void f() {\n"
  5701. " return 42;\n"
  5702. "}",
  5703. DoNotMerge);
  5704. verifyFormat("void f() {\n"
  5705. " // Comment\n"
  5706. "}");
  5707. verifyFormat("{\n"
  5708. "#error {\n"
  5709. " int a;\n"
  5710. "}");
  5711. verifyFormat("{\n"
  5712. " int a;\n"
  5713. "#error {\n"
  5714. "}");
  5715. verifyFormat("void f() {} // comment");
  5716. verifyFormat("void f() { int a; } // comment");
  5717. verifyFormat("void f() {\n"
  5718. "} // comment",
  5719. DoNotMerge);
  5720. verifyFormat("void f() {\n"
  5721. " int a;\n"
  5722. "} // comment",
  5723. DoNotMerge);
  5724. verifyFormat("void f() {\n"
  5725. "} // comment",
  5726. getLLVMStyleWithColumns(15));
  5727. verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
  5728. verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
  5729. verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
  5730. verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
  5731. verifyFormat("class C {\n"
  5732. " C()\n"
  5733. " : iiiiiiii(nullptr),\n"
  5734. " kkkkkkk(nullptr),\n"
  5735. " mmmmmmm(nullptr),\n"
  5736. " nnnnnnn(nullptr) {}\n"
  5737. "};",
  5738. getGoogleStyle());
  5739. FormatStyle NoColumnLimit = getLLVMStyle();
  5740. NoColumnLimit.ColumnLimit = 0;
  5741. EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
  5742. EXPECT_EQ("class C {\n"
  5743. " A() : b(0) {}\n"
  5744. "};", format("class C{A():b(0){}};", NoColumnLimit));
  5745. EXPECT_EQ("A()\n"
  5746. " : b(0) {\n"
  5747. "}",
  5748. format("A()\n:b(0)\n{\n}", NoColumnLimit));
  5749. FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
  5750. DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
  5751. FormatStyle::SFS_None;
  5752. EXPECT_EQ("A()\n"
  5753. " : b(0) {\n"
  5754. "}",
  5755. format("A():b(0){}", DoNotMergeNoColumnLimit));
  5756. EXPECT_EQ("A()\n"
  5757. " : b(0) {\n"
  5758. "}",
  5759. format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
  5760. verifyFormat("#define A \\\n"
  5761. " void f() { \\\n"
  5762. " int i; \\\n"
  5763. " }",
  5764. getLLVMStyleWithColumns(20));
  5765. verifyFormat("#define A \\\n"
  5766. " void f() { int i; }",
  5767. getLLVMStyleWithColumns(21));
  5768. verifyFormat("#define A \\\n"
  5769. " void f() { \\\n"
  5770. " int i; \\\n"
  5771. " } \\\n"
  5772. " int j;",
  5773. getLLVMStyleWithColumns(22));
  5774. verifyFormat("#define A \\\n"
  5775. " void f() { int i; } \\\n"
  5776. " int j;",
  5777. getLLVMStyleWithColumns(23));
  5778. }
  5779. TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
  5780. FormatStyle MergeInlineOnly = getLLVMStyle();
  5781. MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  5782. verifyFormat("class C {\n"
  5783. " int f() { return 42; }\n"
  5784. "};",
  5785. MergeInlineOnly);
  5786. verifyFormat("int f() {\n"
  5787. " return 42;\n"
  5788. "}",
  5789. MergeInlineOnly);
  5790. }
  5791. TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
  5792. // Elaborate type variable declarations.
  5793. verifyFormat("struct foo a = {bar};\nint n;");
  5794. verifyFormat("class foo a = {bar};\nint n;");
  5795. verifyFormat("union foo a = {bar};\nint n;");
  5796. // Elaborate types inside function definitions.
  5797. verifyFormat("struct foo f() {}\nint n;");
  5798. verifyFormat("class foo f() {}\nint n;");
  5799. verifyFormat("union foo f() {}\nint n;");
  5800. // Templates.
  5801. verifyFormat("template <class X> void f() {}\nint n;");
  5802. verifyFormat("template <struct X> void f() {}\nint n;");
  5803. verifyFormat("template <union X> void f() {}\nint n;");
  5804. // Actual definitions...
  5805. verifyFormat("struct {\n} n;");
  5806. verifyFormat(
  5807. "template <template <class T, class Y>, class Z> class X {\n} n;");
  5808. verifyFormat("union Z {\n int n;\n} x;");
  5809. verifyFormat("class MACRO Z {\n} n;");
  5810. verifyFormat("class MACRO(X) Z {\n} n;");
  5811. verifyFormat("class __attribute__(X) Z {\n} n;");
  5812. verifyFormat("class __declspec(X) Z {\n} n;");
  5813. verifyFormat("class A##B##C {\n} n;");
  5814. verifyFormat("class alignas(16) Z {\n} n;");
  5815. // Redefinition from nested context:
  5816. verifyFormat("class A::B::C {\n} n;");
  5817. // Template definitions.
  5818. verifyFormat(
  5819. "template <typename F>\n"
  5820. "Matcher(const Matcher<F> &Other,\n"
  5821. " typename enable_if_c<is_base_of<F, T>::value &&\n"
  5822. " !is_same<F, T>::value>::type * = 0)\n"
  5823. " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
  5824. // FIXME: This is still incorrectly handled at the formatter side.
  5825. verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
  5826. // FIXME:
  5827. // This now gets parsed incorrectly as class definition.
  5828. // verifyFormat("class A<int> f() {\n}\nint n;");
  5829. // Elaborate types where incorrectly parsing the structural element would
  5830. // break the indent.
  5831. verifyFormat("if (true)\n"
  5832. " class X x;\n"
  5833. "else\n"
  5834. " f();\n");
  5835. // This is simply incomplete. Formatting is not important, but must not crash.
  5836. verifyFormat("class A:");
  5837. }
  5838. TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
  5839. EXPECT_EQ("#error Leave all white!!!!! space* alone!\n",
  5840. format("#error Leave all white!!!!! space* alone!\n"));
  5841. EXPECT_EQ(
  5842. "#warning Leave all white!!!!! space* alone!\n",
  5843. format("#warning Leave all white!!!!! space* alone!\n"));
  5844. EXPECT_EQ("#error 1", format(" # error 1"));
  5845. EXPECT_EQ("#warning 1", format(" # warning 1"));
  5846. }
  5847. TEST_F(FormatTest, FormatHashIfExpressions) {
  5848. verifyFormat("#if AAAA && BBBB");
  5849. // FIXME: Come up with a better indentation for #elif.
  5850. verifyFormat(
  5851. "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
  5852. " defined(BBBBBBBB)\n"
  5853. "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
  5854. " defined(BBBBBBBB)\n"
  5855. "#endif",
  5856. getLLVMStyleWithColumns(65));
  5857. }
  5858. TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
  5859. FormatStyle AllowsMergedIf = getGoogleStyle();
  5860. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  5861. verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
  5862. verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
  5863. verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
  5864. EXPECT_EQ("if (true) return 42;",
  5865. format("if (true)\nreturn 42;", AllowsMergedIf));
  5866. FormatStyle ShortMergedIf = AllowsMergedIf;
  5867. ShortMergedIf.ColumnLimit = 25;
  5868. verifyFormat("#define A \\\n"
  5869. " if (true) return 42;",
  5870. ShortMergedIf);
  5871. verifyFormat("#define A \\\n"
  5872. " f(); \\\n"
  5873. " if (true)\n"
  5874. "#define B",
  5875. ShortMergedIf);
  5876. verifyFormat("#define A \\\n"
  5877. " f(); \\\n"
  5878. " if (true)\n"
  5879. "g();",
  5880. ShortMergedIf);
  5881. verifyFormat("{\n"
  5882. "#ifdef A\n"
  5883. " // Comment\n"
  5884. " if (true) continue;\n"
  5885. "#endif\n"
  5886. " // Comment\n"
  5887. " if (true) continue;\n"
  5888. "}",
  5889. ShortMergedIf);
  5890. ShortMergedIf.ColumnLimit = 29;
  5891. verifyFormat("#define A \\\n"
  5892. " if (aaaaaaaaaa) return 1; \\\n"
  5893. " return 2;",
  5894. ShortMergedIf);
  5895. ShortMergedIf.ColumnLimit = 28;
  5896. verifyFormat("#define A \\\n"
  5897. " if (aaaaaaaaaa) \\\n"
  5898. " return 1; \\\n"
  5899. " return 2;",
  5900. ShortMergedIf);
  5901. }
  5902. TEST_F(FormatTest, BlockCommentsInControlLoops) {
  5903. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  5904. " f();\n"
  5905. "}");
  5906. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  5907. " f();\n"
  5908. "} /* another comment */ else /* comment #3 */ {\n"
  5909. " g();\n"
  5910. "}");
  5911. verifyFormat("while (0) /* a comment in a strange place */ {\n"
  5912. " f();\n"
  5913. "}");
  5914. verifyFormat("for (;;) /* a comment in a strange place */ {\n"
  5915. " f();\n"
  5916. "}");
  5917. verifyFormat("do /* a comment in a strange place */ {\n"
  5918. " f();\n"
  5919. "} /* another comment */ while (0);");
  5920. }
  5921. TEST_F(FormatTest, BlockComments) {
  5922. EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
  5923. format("/* *//* */ /* */\n/* *//* */ /* */"));
  5924. EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
  5925. EXPECT_EQ("#define A /*123*/ \\\n"
  5926. " b\n"
  5927. "/* */\n"
  5928. "someCall(\n"
  5929. " parameter);",
  5930. format("#define A /*123*/ b\n"
  5931. "/* */\n"
  5932. "someCall(parameter);",
  5933. getLLVMStyleWithColumns(15)));
  5934. EXPECT_EQ("#define A\n"
  5935. "/* */ someCall(\n"
  5936. " parameter);",
  5937. format("#define A\n"
  5938. "/* */someCall(parameter);",
  5939. getLLVMStyleWithColumns(15)));
  5940. EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
  5941. EXPECT_EQ("/*\n"
  5942. "*\n"
  5943. " * aaaaaa\n"
  5944. "*aaaaaa\n"
  5945. "*/",
  5946. format("/*\n"
  5947. "*\n"
  5948. " * aaaaaa aaaaaa\n"
  5949. "*/",
  5950. getLLVMStyleWithColumns(10)));
  5951. EXPECT_EQ("/*\n"
  5952. "**\n"
  5953. "* aaaaaa\n"
  5954. "*aaaaaa\n"
  5955. "*/",
  5956. format("/*\n"
  5957. "**\n"
  5958. "* aaaaaa aaaaaa\n"
  5959. "*/",
  5960. getLLVMStyleWithColumns(10)));
  5961. FormatStyle NoBinPacking = getLLVMStyle();
  5962. NoBinPacking.BinPackParameters = false;
  5963. EXPECT_EQ("someFunction(1, /* comment 1 */\n"
  5964. " 2, /* comment 2 */\n"
  5965. " 3, /* comment 3 */\n"
  5966. " aaaa,\n"
  5967. " bbbb);",
  5968. format("someFunction (1, /* comment 1 */\n"
  5969. " 2, /* comment 2 */ \n"
  5970. " 3, /* comment 3 */\n"
  5971. "aaaa, bbbb );",
  5972. NoBinPacking));
  5973. verifyFormat(
  5974. "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  5975. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  5976. EXPECT_EQ(
  5977. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  5978. " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  5979. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
  5980. format(
  5981. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  5982. " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  5983. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
  5984. EXPECT_EQ(
  5985. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  5986. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  5987. "int cccccccccccccccccccccccccccccc; /* comment */\n",
  5988. format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  5989. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  5990. "int cccccccccccccccccccccccccccccc; /* comment */\n"));
  5991. verifyFormat("void f(int * /* unused */) {}");
  5992. EXPECT_EQ("/*\n"
  5993. " **\n"
  5994. " */",
  5995. format("/*\n"
  5996. " **\n"
  5997. " */"));
  5998. EXPECT_EQ("/*\n"
  5999. " *q\n"
  6000. " */",
  6001. format("/*\n"
  6002. " *q\n"
  6003. " */"));
  6004. EXPECT_EQ("/*\n"
  6005. " * q\n"
  6006. " */",
  6007. format("/*\n"
  6008. " * q\n"
  6009. " */"));
  6010. EXPECT_EQ("/*\n"
  6011. " **/",
  6012. format("/*\n"
  6013. " **/"));
  6014. EXPECT_EQ("/*\n"
  6015. " ***/",
  6016. format("/*\n"
  6017. " ***/"));
  6018. }
  6019. TEST_F(FormatTest, BlockCommentsInMacros) {
  6020. EXPECT_EQ("#define A \\\n"
  6021. " { \\\n"
  6022. " /* one line */ \\\n"
  6023. " someCall();",
  6024. format("#define A { \\\n"
  6025. " /* one line */ \\\n"
  6026. " someCall();",
  6027. getLLVMStyleWithColumns(20)));
  6028. EXPECT_EQ("#define A \\\n"
  6029. " { \\\n"
  6030. " /* previous */ \\\n"
  6031. " /* one line */ \\\n"
  6032. " someCall();",
  6033. format("#define A { \\\n"
  6034. " /* previous */ \\\n"
  6035. " /* one line */ \\\n"
  6036. " someCall();",
  6037. getLLVMStyleWithColumns(20)));
  6038. }
  6039. TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
  6040. EXPECT_EQ("a = {\n"
  6041. " 1111 /* */\n"
  6042. "};",
  6043. format("a = {1111 /* */\n"
  6044. "};",
  6045. getLLVMStyleWithColumns(15)));
  6046. EXPECT_EQ("a = {\n"
  6047. " 1111 /* */\n"
  6048. "};",
  6049. format("a = {1111 /* */\n"
  6050. "};",
  6051. getLLVMStyleWithColumns(15)));
  6052. // FIXME: The formatting is still wrong here.
  6053. EXPECT_EQ("a = {\n"
  6054. " 1111 /* a\n"
  6055. " */\n"
  6056. "};",
  6057. format("a = {1111 /* a */\n"
  6058. "};",
  6059. getLLVMStyleWithColumns(15)));
  6060. }
  6061. TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
  6062. // FIXME: This is not what we want...
  6063. verifyFormat("{\n"
  6064. "// a"
  6065. "// b");
  6066. }
  6067. TEST_F(FormatTest, FormatStarDependingOnContext) {
  6068. verifyFormat("void f(int *a);");
  6069. verifyFormat("void f() { f(fint * b); }");
  6070. verifyFormat("class A {\n void f(int *a);\n};");
  6071. verifyFormat("class A {\n int *a;\n};");
  6072. verifyFormat("namespace a {\n"
  6073. "namespace b {\n"
  6074. "class A {\n"
  6075. " void f() {}\n"
  6076. " int *a;\n"
  6077. "};\n"
  6078. "}\n"
  6079. "}");
  6080. }
  6081. TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
  6082. verifyFormat("while");
  6083. verifyFormat("operator");
  6084. }
  6085. //===----------------------------------------------------------------------===//
  6086. // Objective-C tests.
  6087. //===----------------------------------------------------------------------===//
  6088. TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
  6089. verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
  6090. EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
  6091. format("-(NSUInteger)indexOfObject:(id)anObject;"));
  6092. EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
  6093. EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
  6094. EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
  6095. format("-(NSInteger)Method3:(id)anObject;"));
  6096. EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
  6097. format("-(NSInteger)Method4:(id)anObject;"));
  6098. EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
  6099. format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
  6100. EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
  6101. format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
  6102. EXPECT_EQ(
  6103. "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
  6104. format(
  6105. "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
  6106. // Very long objectiveC method declaration.
  6107. verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
  6108. " inRange:(NSRange)range\n"
  6109. " outRange:(NSRange)out_range\n"
  6110. " outRange1:(NSRange)out_range1\n"
  6111. " outRange2:(NSRange)out_range2\n"
  6112. " outRange3:(NSRange)out_range3\n"
  6113. " outRange4:(NSRange)out_range4\n"
  6114. " outRange5:(NSRange)out_range5\n"
  6115. " outRange6:(NSRange)out_range6\n"
  6116. " outRange7:(NSRange)out_range7\n"
  6117. " outRange8:(NSRange)out_range8\n"
  6118. " outRange9:(NSRange)out_range9;");
  6119. verifyFormat("- (int)sum:(vector<int>)numbers;");
  6120. verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
  6121. // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
  6122. // protocol lists (but not for template classes):
  6123. //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
  6124. verifyFormat("- (int (*)())foo:(int (*)())f;");
  6125. verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
  6126. // If there's no return type (very rare in practice!), LLVM and Google style
  6127. // agree.
  6128. verifyFormat("- foo;");
  6129. verifyFormat("- foo:(int)f;");
  6130. verifyGoogleFormat("- foo:(int)foo;");
  6131. }
  6132. TEST_F(FormatTest, FormatObjCInterface) {
  6133. verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
  6134. "@public\n"
  6135. " int field1;\n"
  6136. "@protected\n"
  6137. " int field2;\n"
  6138. "@private\n"
  6139. " int field3;\n"
  6140. "@package\n"
  6141. " int field4;\n"
  6142. "}\n"
  6143. "+ (id)init;\n"
  6144. "@end");
  6145. verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
  6146. " @public\n"
  6147. " int field1;\n"
  6148. " @protected\n"
  6149. " int field2;\n"
  6150. " @private\n"
  6151. " int field3;\n"
  6152. " @package\n"
  6153. " int field4;\n"
  6154. "}\n"
  6155. "+ (id)init;\n"
  6156. "@end");
  6157. verifyFormat("@interface /* wait for it */ Foo\n"
  6158. "+ (id)init;\n"
  6159. "// Look, a comment!\n"
  6160. "- (int)answerWith:(int)i;\n"
  6161. "@end");
  6162. verifyFormat("@interface Foo\n"
  6163. "@end\n"
  6164. "@interface Bar\n"
  6165. "@end");
  6166. verifyFormat("@interface Foo : Bar\n"
  6167. "+ (id)init;\n"
  6168. "@end");
  6169. verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
  6170. "+ (id)init;\n"
  6171. "@end");
  6172. verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
  6173. "+ (id)init;\n"
  6174. "@end");
  6175. verifyFormat("@interface Foo (HackStuff)\n"
  6176. "+ (id)init;\n"
  6177. "@end");
  6178. verifyFormat("@interface Foo ()\n"
  6179. "+ (id)init;\n"
  6180. "@end");
  6181. verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
  6182. "+ (id)init;\n"
  6183. "@end");
  6184. verifyGoogleFormat("@interface Foo (HackStuff) <MyProtocol>\n"
  6185. "+ (id)init;\n"
  6186. "@end");
  6187. verifyFormat("@interface Foo {\n"
  6188. " int _i;\n"
  6189. "}\n"
  6190. "+ (id)init;\n"
  6191. "@end");
  6192. verifyFormat("@interface Foo : Bar {\n"
  6193. " int _i;\n"
  6194. "}\n"
  6195. "+ (id)init;\n"
  6196. "@end");
  6197. verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
  6198. " int _i;\n"
  6199. "}\n"
  6200. "+ (id)init;\n"
  6201. "@end");
  6202. verifyFormat("@interface Foo (HackStuff) {\n"
  6203. " int _i;\n"
  6204. "}\n"
  6205. "+ (id)init;\n"
  6206. "@end");
  6207. verifyFormat("@interface Foo () {\n"
  6208. " int _i;\n"
  6209. "}\n"
  6210. "+ (id)init;\n"
  6211. "@end");
  6212. verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
  6213. " int _i;\n"
  6214. "}\n"
  6215. "+ (id)init;\n"
  6216. "@end");
  6217. FormatStyle OnePerLine = getGoogleStyle();
  6218. OnePerLine.BinPackParameters = false;
  6219. verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa () <\n"
  6220. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6221. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6222. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6223. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
  6224. "}",
  6225. OnePerLine);
  6226. }
  6227. TEST_F(FormatTest, FormatObjCImplementation) {
  6228. verifyFormat("@implementation Foo : NSObject {\n"
  6229. "@public\n"
  6230. " int field1;\n"
  6231. "@protected\n"
  6232. " int field2;\n"
  6233. "@private\n"
  6234. " int field3;\n"
  6235. "@package\n"
  6236. " int field4;\n"
  6237. "}\n"
  6238. "+ (id)init {\n}\n"
  6239. "@end");
  6240. verifyGoogleFormat("@implementation Foo : NSObject {\n"
  6241. " @public\n"
  6242. " int field1;\n"
  6243. " @protected\n"
  6244. " int field2;\n"
  6245. " @private\n"
  6246. " int field3;\n"
  6247. " @package\n"
  6248. " int field4;\n"
  6249. "}\n"
  6250. "+ (id)init {\n}\n"
  6251. "@end");
  6252. verifyFormat("@implementation Foo\n"
  6253. "+ (id)init {\n"
  6254. " if (true)\n"
  6255. " return nil;\n"
  6256. "}\n"
  6257. "// Look, a comment!\n"
  6258. "- (int)answerWith:(int)i {\n"
  6259. " return i;\n"
  6260. "}\n"
  6261. "+ (int)answerWith:(int)i {\n"
  6262. " return i;\n"
  6263. "}\n"
  6264. "@end");
  6265. verifyFormat("@implementation Foo\n"
  6266. "@end\n"
  6267. "@implementation Bar\n"
  6268. "@end");
  6269. EXPECT_EQ("@implementation Foo : Bar\n"
  6270. "+ (id)init {\n}\n"
  6271. "- (void)foo {\n}\n"
  6272. "@end",
  6273. format("@implementation Foo : Bar\n"
  6274. "+(id)init{}\n"
  6275. "-(void)foo{}\n"
  6276. "@end"));
  6277. verifyFormat("@implementation Foo {\n"
  6278. " int _i;\n"
  6279. "}\n"
  6280. "+ (id)init {\n}\n"
  6281. "@end");
  6282. verifyFormat("@implementation Foo : Bar {\n"
  6283. " int _i;\n"
  6284. "}\n"
  6285. "+ (id)init {\n}\n"
  6286. "@end");
  6287. verifyFormat("@implementation Foo (HackStuff)\n"
  6288. "+ (id)init {\n}\n"
  6289. "@end");
  6290. verifyFormat("@implementation ObjcClass\n"
  6291. "- (void)method;\n"
  6292. "{}\n"
  6293. "@end");
  6294. }
  6295. TEST_F(FormatTest, FormatObjCProtocol) {
  6296. verifyFormat("@protocol Foo\n"
  6297. "@property(weak) id delegate;\n"
  6298. "- (NSUInteger)numberOfThings;\n"
  6299. "@end");
  6300. verifyFormat("@protocol MyProtocol <NSObject>\n"
  6301. "- (NSUInteger)numberOfThings;\n"
  6302. "@end");
  6303. verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
  6304. "- (NSUInteger)numberOfThings;\n"
  6305. "@end");
  6306. verifyFormat("@protocol Foo;\n"
  6307. "@protocol Bar;\n");
  6308. verifyFormat("@protocol Foo\n"
  6309. "@end\n"
  6310. "@protocol Bar\n"
  6311. "@end");
  6312. verifyFormat("@protocol myProtocol\n"
  6313. "- (void)mandatoryWithInt:(int)i;\n"
  6314. "@optional\n"
  6315. "- (void)optional;\n"
  6316. "@required\n"
  6317. "- (void)required;\n"
  6318. "@optional\n"
  6319. "@property(assign) int madProp;\n"
  6320. "@end\n");
  6321. verifyFormat("@property(nonatomic, assign, readonly)\n"
  6322. " int *looooooooooooooooooooooooooooongNumber;\n"
  6323. "@property(nonatomic, assign, readonly)\n"
  6324. " NSString *looooooooooooooooooooooooooooongName;");
  6325. verifyFormat("@implementation PR18406\n"
  6326. "}\n"
  6327. "@end");
  6328. }
  6329. TEST_F(FormatTest, FormatObjCMethodDeclarations) {
  6330. verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
  6331. " rect:(NSRect)theRect\n"
  6332. " interval:(float)theInterval {\n"
  6333. "}");
  6334. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6335. " longKeyword:(NSRect)theRect\n"
  6336. " evenLongerKeyword:(float)theInterval\n"
  6337. " error:(NSError **)theError {\n"
  6338. "}");
  6339. verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
  6340. " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
  6341. " NS_DESIGNATED_INITIALIZER;",
  6342. getLLVMStyleWithColumns(60));
  6343. }
  6344. TEST_F(FormatTest, FormatObjCMethodExpr) {
  6345. verifyFormat("[foo bar:baz];");
  6346. verifyFormat("return [foo bar:baz];");
  6347. verifyFormat("return (a)[foo bar:baz];");
  6348. verifyFormat("f([foo bar:baz]);");
  6349. verifyFormat("f(2, [foo bar:baz]);");
  6350. verifyFormat("f(2, a ? b : c);");
  6351. verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
  6352. // Unary operators.
  6353. verifyFormat("int a = +[foo bar:baz];");
  6354. verifyFormat("int a = -[foo bar:baz];");
  6355. verifyFormat("int a = ![foo bar:baz];");
  6356. verifyFormat("int a = ~[foo bar:baz];");
  6357. verifyFormat("int a = ++[foo bar:baz];");
  6358. verifyFormat("int a = --[foo bar:baz];");
  6359. verifyFormat("int a = sizeof [foo bar:baz];");
  6360. verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
  6361. verifyFormat("int a = &[foo bar:baz];");
  6362. verifyFormat("int a = *[foo bar:baz];");
  6363. // FIXME: Make casts work, without breaking f()[4].
  6364. //verifyFormat("int a = (int)[foo bar:baz];");
  6365. //verifyFormat("return (int)[foo bar:baz];");
  6366. //verifyFormat("(void)[foo bar:baz];");
  6367. verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
  6368. // Binary operators.
  6369. verifyFormat("[foo bar:baz], [foo bar:baz];");
  6370. verifyFormat("[foo bar:baz] = [foo bar:baz];");
  6371. verifyFormat("[foo bar:baz] *= [foo bar:baz];");
  6372. verifyFormat("[foo bar:baz] /= [foo bar:baz];");
  6373. verifyFormat("[foo bar:baz] %= [foo bar:baz];");
  6374. verifyFormat("[foo bar:baz] += [foo bar:baz];");
  6375. verifyFormat("[foo bar:baz] -= [foo bar:baz];");
  6376. verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
  6377. verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
  6378. verifyFormat("[foo bar:baz] &= [foo bar:baz];");
  6379. verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
  6380. verifyFormat("[foo bar:baz] |= [foo bar:baz];");
  6381. verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
  6382. verifyFormat("[foo bar:baz] || [foo bar:baz];");
  6383. verifyFormat("[foo bar:baz] && [foo bar:baz];");
  6384. verifyFormat("[foo bar:baz] | [foo bar:baz];");
  6385. verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
  6386. verifyFormat("[foo bar:baz] & [foo bar:baz];");
  6387. verifyFormat("[foo bar:baz] == [foo bar:baz];");
  6388. verifyFormat("[foo bar:baz] != [foo bar:baz];");
  6389. verifyFormat("[foo bar:baz] >= [foo bar:baz];");
  6390. verifyFormat("[foo bar:baz] <= [foo bar:baz];");
  6391. verifyFormat("[foo bar:baz] > [foo bar:baz];");
  6392. verifyFormat("[foo bar:baz] < [foo bar:baz];");
  6393. verifyFormat("[foo bar:baz] >> [foo bar:baz];");
  6394. verifyFormat("[foo bar:baz] << [foo bar:baz];");
  6395. verifyFormat("[foo bar:baz] - [foo bar:baz];");
  6396. verifyFormat("[foo bar:baz] + [foo bar:baz];");
  6397. verifyFormat("[foo bar:baz] * [foo bar:baz];");
  6398. verifyFormat("[foo bar:baz] / [foo bar:baz];");
  6399. verifyFormat("[foo bar:baz] % [foo bar:baz];");
  6400. // Whew!
  6401. verifyFormat("return in[42];");
  6402. verifyFormat("for (auto v : in[1]) {\n}");
  6403. verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
  6404. "}");
  6405. verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
  6406. verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
  6407. verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
  6408. verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
  6409. verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
  6410. verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
  6411. verifyFormat("[button setAction:@selector(zoomOut:)];");
  6412. verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
  6413. verifyFormat("arr[[self indexForFoo:a]];");
  6414. verifyFormat("throw [self errorFor:a];");
  6415. verifyFormat("@throw [self errorFor:a];");
  6416. verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
  6417. verifyFormat("[(id)foo bar:(id) ? baz : quux];");
  6418. verifyFormat("4 > 4 ? (id)a : (id)baz;");
  6419. // This tests that the formatter doesn't break after "backing" but before ":",
  6420. // which would be at 80 columns.
  6421. verifyFormat(
  6422. "void f() {\n"
  6423. " if ((self = [super initWithContentRect:contentRect\n"
  6424. " styleMask:styleMask ?: otherMask\n"
  6425. " backing:NSBackingStoreBuffered\n"
  6426. " defer:YES]))");
  6427. verifyFormat(
  6428. "[foo checkThatBreakingAfterColonWorksOk:\n"
  6429. " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
  6430. verifyFormat("[myObj short:arg1 // Force line break\n"
  6431. " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
  6432. " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
  6433. " error:arg4];");
  6434. verifyFormat(
  6435. "void f() {\n"
  6436. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  6437. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  6438. " pos.width(), pos.height())\n"
  6439. " styleMask:NSBorderlessWindowMask\n"
  6440. " backing:NSBackingStoreBuffered\n"
  6441. " defer:NO]);\n"
  6442. "}");
  6443. verifyFormat(
  6444. "void f() {\n"
  6445. " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
  6446. " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
  6447. " pos.width(), pos.height())\n"
  6448. " syeMask:NSBorderlessWindowMask\n"
  6449. " bking:NSBackingStoreBuffered\n"
  6450. " der:NO]);\n"
  6451. "}",
  6452. getLLVMStyleWithColumns(70));
  6453. verifyFormat(
  6454. "void f() {\n"
  6455. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  6456. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  6457. " pos.width(), pos.height())\n"
  6458. " styleMask:NSBorderlessWindowMask\n"
  6459. " backing:NSBackingStoreBuffered\n"
  6460. " defer:NO]);\n"
  6461. "}",
  6462. getChromiumStyle(FormatStyle::LK_Cpp));
  6463. verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
  6464. " with:contentsNativeView];");
  6465. verifyFormat(
  6466. "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
  6467. " owner:nillllll];");
  6468. verifyFormat(
  6469. "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
  6470. " forType:kBookmarkButtonDragType];");
  6471. verifyFormat("[defaultCenter addObserver:self\n"
  6472. " selector:@selector(willEnterFullscreen)\n"
  6473. " name:kWillEnterFullscreenNotification\n"
  6474. " object:nil];");
  6475. verifyFormat("[image_rep drawInRect:drawRect\n"
  6476. " fromRect:NSZeroRect\n"
  6477. " operation:NSCompositeCopy\n"
  6478. " fraction:1.0\n"
  6479. " respectFlipped:NO\n"
  6480. " hints:nil];");
  6481. verifyFormat(
  6482. "scoped_nsobject<NSTextField> message(\n"
  6483. " // The frame will be fixed up when |-setMessageText:| is called.\n"
  6484. " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
  6485. verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
  6486. " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
  6487. " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
  6488. " aaaa:bbb];");
  6489. verifyFormat("[self param:function( //\n"
  6490. " parameter)]");
  6491. verifyFormat(
  6492. "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  6493. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  6494. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
  6495. // Variadic parameters.
  6496. verifyFormat(
  6497. "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
  6498. verifyFormat(
  6499. "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  6500. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  6501. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
  6502. verifyFormat("[self // break\n"
  6503. " a:a\n"
  6504. " aaa:aaa];");
  6505. verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
  6506. " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
  6507. }
  6508. TEST_F(FormatTest, ObjCAt) {
  6509. verifyFormat("@autoreleasepool");
  6510. verifyFormat("@catch");
  6511. verifyFormat("@class");
  6512. verifyFormat("@compatibility_alias");
  6513. verifyFormat("@defs");
  6514. verifyFormat("@dynamic");
  6515. verifyFormat("@encode");
  6516. verifyFormat("@end");
  6517. verifyFormat("@finally");
  6518. verifyFormat("@implementation");
  6519. verifyFormat("@import");
  6520. verifyFormat("@interface");
  6521. verifyFormat("@optional");
  6522. verifyFormat("@package");
  6523. verifyFormat("@private");
  6524. verifyFormat("@property");
  6525. verifyFormat("@protected");
  6526. verifyFormat("@protocol");
  6527. verifyFormat("@public");
  6528. verifyFormat("@required");
  6529. verifyFormat("@selector");
  6530. verifyFormat("@synchronized");
  6531. verifyFormat("@synthesize");
  6532. verifyFormat("@throw");
  6533. verifyFormat("@try");
  6534. EXPECT_EQ("@interface", format("@ interface"));
  6535. // The precise formatting of this doesn't matter, nobody writes code like
  6536. // this.
  6537. verifyFormat("@ /*foo*/ interface");
  6538. }
  6539. TEST_F(FormatTest, ObjCSnippets) {
  6540. verifyFormat("@autoreleasepool {\n"
  6541. " foo();\n"
  6542. "}");
  6543. verifyFormat("@class Foo, Bar;");
  6544. verifyFormat("@compatibility_alias AliasName ExistingClass;");
  6545. verifyFormat("@dynamic textColor;");
  6546. verifyFormat("char *buf1 = @encode(int *);");
  6547. verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
  6548. verifyFormat("char *buf1 = @encode(int **);");
  6549. verifyFormat("Protocol *proto = @protocol(p1);");
  6550. verifyFormat("SEL s = @selector(foo:);");
  6551. verifyFormat("@synchronized(self) {\n"
  6552. " f();\n"
  6553. "}");
  6554. verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  6555. verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  6556. verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
  6557. verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
  6558. verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
  6559. verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
  6560. getMozillaStyle());
  6561. verifyFormat("@property BOOL editable;", getMozillaStyle());
  6562. verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
  6563. getWebKitStyle());
  6564. verifyFormat("@property BOOL editable;", getWebKitStyle());
  6565. verifyFormat("@import foo.bar;\n"
  6566. "@import baz;");
  6567. }
  6568. TEST_F(FormatTest, ObjCLiterals) {
  6569. verifyFormat("@\"String\"");
  6570. verifyFormat("@1");
  6571. verifyFormat("@+4.8");
  6572. verifyFormat("@-4");
  6573. verifyFormat("@1LL");
  6574. verifyFormat("@.5");
  6575. verifyFormat("@'c'");
  6576. verifyFormat("@true");
  6577. verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
  6578. verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
  6579. verifyFormat("NSNumber *favoriteColor = @(Green);");
  6580. verifyFormat("NSString *path = @(getenv(\"PATH\"));");
  6581. verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
  6582. }
  6583. TEST_F(FormatTest, ObjCDictLiterals) {
  6584. verifyFormat("@{");
  6585. verifyFormat("@{}");
  6586. verifyFormat("@{@\"one\" : @1}");
  6587. verifyFormat("return @{@\"one\" : @1;");
  6588. verifyFormat("@{@\"one\" : @1}");
  6589. verifyFormat("@{@\"one\" : @{@2 : @1}}");
  6590. verifyFormat("@{\n"
  6591. " @\"one\" : @{@2 : @1},\n"
  6592. "}");
  6593. verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
  6594. verifyFormat("[self setDict:@{}");
  6595. verifyFormat("[self setDict:@{@1 : @2}");
  6596. verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
  6597. verifyFormat(
  6598. "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
  6599. verifyFormat(
  6600. "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
  6601. verifyFormat(
  6602. "NSDictionary *d = @{\n"
  6603. " @\"nam\" : NSUserNam(),\n"
  6604. " @\"dte\" : [NSDate date],\n"
  6605. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  6606. "};");
  6607. verifyFormat(
  6608. "@{\n"
  6609. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  6610. "regularFont,\n"
  6611. "};");
  6612. verifyGoogleFormat(
  6613. "@{\n"
  6614. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  6615. "regularFont,\n"
  6616. "};");
  6617. verifyFormat(
  6618. "@{\n"
  6619. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
  6620. " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
  6621. "};");
  6622. // We should try to be robust in case someone forgets the "@".
  6623. verifyFormat(
  6624. "NSDictionary *d = {\n"
  6625. " @\"nam\" : NSUserNam(),\n"
  6626. " @\"dte\" : [NSDate date],\n"
  6627. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  6628. "};");
  6629. verifyFormat("NSMutableDictionary *dictionary =\n"
  6630. " [NSMutableDictionary dictionaryWithDictionary:@{\n"
  6631. " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
  6632. " bbbbbbbbbbbbbbbbbb : bbbbb,\n"
  6633. " cccccccccccccccc : ccccccccccccccc\n"
  6634. " }];");
  6635. }
  6636. TEST_F(FormatTest, ObjCArrayLiterals) {
  6637. verifyFormat("@[");
  6638. verifyFormat("@[]");
  6639. verifyFormat(
  6640. "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
  6641. verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
  6642. verifyFormat("NSArray *array = @[ [foo description] ];");
  6643. verifyFormat(
  6644. "NSArray *some_variable = @[\n"
  6645. " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
  6646. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6647. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6648. " @\"aaaaaaaaaaaaaaaaa\"\n"
  6649. "];");
  6650. verifyFormat("NSArray *some_variable = @[\n"
  6651. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6652. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6653. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6654. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6655. "];");
  6656. verifyGoogleFormat("NSArray *some_variable = @[\n"
  6657. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6658. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6659. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6660. " @\"aaaaaaaaaaaaaaaaa\"\n"
  6661. "];");
  6662. verifyFormat("NSArray *array = @[\n"
  6663. " @\"a\",\n"
  6664. " @\"a\",\n" // Trailing comma -> one per line.
  6665. "];");
  6666. // We should try to be robust in case someone forgets the "@".
  6667. verifyFormat("NSArray *some_variable = [\n"
  6668. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6669. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6670. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6671. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6672. "];");
  6673. verifyFormat(
  6674. "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
  6675. " index:(NSUInteger)index\n"
  6676. " nonDigitAttributes:\n"
  6677. " (NSDictionary *)noDigitAttributes;");
  6678. verifyFormat(
  6679. "[someFunction someLooooooooooooongParameter:\n"
  6680. " @[ NSBundle.mainBundle.infoDictionary[@\"a\"] ]];");
  6681. }
  6682. TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
  6683. EXPECT_EQ("{\n"
  6684. "{\n"
  6685. "a;\n"
  6686. "b;\n"
  6687. "}\n"
  6688. "}",
  6689. format("{\n"
  6690. "{\n"
  6691. "a;\n"
  6692. " b;\n"
  6693. "}\n"
  6694. "}",
  6695. 13, 2, getLLVMStyle()));
  6696. EXPECT_EQ("{\n"
  6697. "{\n"
  6698. " a;\n"
  6699. "b;\n"
  6700. "}\n"
  6701. "}",
  6702. format("{\n"
  6703. "{\n"
  6704. " a;\n"
  6705. "b;\n"
  6706. "}\n"
  6707. "}",
  6708. 9, 2, getLLVMStyle()));
  6709. EXPECT_EQ("{\n"
  6710. "{\n"
  6711. "public:\n"
  6712. " b;\n"
  6713. "}\n"
  6714. "}",
  6715. format("{\n"
  6716. "{\n"
  6717. "public:\n"
  6718. " b;\n"
  6719. "}\n"
  6720. "}",
  6721. 17, 2, getLLVMStyle()));
  6722. EXPECT_EQ("{\n"
  6723. "{\n"
  6724. "a;\n"
  6725. "}\n"
  6726. "{\n"
  6727. " b; //\n"
  6728. "}\n"
  6729. "}",
  6730. format("{\n"
  6731. "{\n"
  6732. "a;\n"
  6733. "}\n"
  6734. "{\n"
  6735. " b; //\n"
  6736. "}\n"
  6737. "}",
  6738. 22, 2, getLLVMStyle()));
  6739. EXPECT_EQ(" {\n"
  6740. " a; //\n"
  6741. " }",
  6742. format(" {\n"
  6743. "a; //\n"
  6744. " }",
  6745. 4, 2, getLLVMStyle()));
  6746. EXPECT_EQ("void f() {}\n"
  6747. "void g() {}",
  6748. format("void f() {}\n"
  6749. "void g() {}",
  6750. 13, 0, getLLVMStyle()));
  6751. EXPECT_EQ("int a; // comment\n"
  6752. " // line 2\n"
  6753. "int b;",
  6754. format("int a; // comment\n"
  6755. " // line 2\n"
  6756. " int b;",
  6757. 35, 0, getLLVMStyle()));
  6758. EXPECT_EQ(" int a;\n"
  6759. " void\n"
  6760. " ffffff() {\n"
  6761. " }",
  6762. format(" int a;\n"
  6763. "void ffffff() {}",
  6764. 11, 0, getLLVMStyleWithColumns(11)));
  6765. EXPECT_EQ(" void f() {\n"
  6766. "#define A 1\n"
  6767. " }",
  6768. format(" void f() {\n"
  6769. " #define A 1\n" // Format this line.
  6770. " }",
  6771. 20, 0, getLLVMStyle()));
  6772. EXPECT_EQ(" void f() {\n"
  6773. " int i;\n"
  6774. "#define A \\\n"
  6775. " int i; \\\n"
  6776. " int j;\n"
  6777. " int k;\n"
  6778. " }",
  6779. format(" void f() {\n"
  6780. " int i;\n"
  6781. "#define A \\\n"
  6782. " int i; \\\n"
  6783. " int j;\n"
  6784. " int k;\n" // Format this line.
  6785. " }",
  6786. 67, 0, getLLVMStyle()));
  6787. }
  6788. TEST_F(FormatTest, BreaksStringLiterals) {
  6789. EXPECT_EQ("\"some text \"\n"
  6790. "\"other\";",
  6791. format("\"some text other\";", getLLVMStyleWithColumns(12)));
  6792. EXPECT_EQ("\"some text \"\n"
  6793. "\"other\";",
  6794. format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
  6795. EXPECT_EQ(
  6796. "#define A \\\n"
  6797. " \"some \" \\\n"
  6798. " \"text \" \\\n"
  6799. " \"other\";",
  6800. format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
  6801. EXPECT_EQ(
  6802. "#define A \\\n"
  6803. " \"so \" \\\n"
  6804. " \"text \" \\\n"
  6805. " \"other\";",
  6806. format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
  6807. EXPECT_EQ("\"some text\"",
  6808. format("\"some text\"", getLLVMStyleWithColumns(1)));
  6809. EXPECT_EQ("\"some text\"",
  6810. format("\"some text\"", getLLVMStyleWithColumns(11)));
  6811. EXPECT_EQ("\"some \"\n"
  6812. "\"text\"",
  6813. format("\"some text\"", getLLVMStyleWithColumns(10)));
  6814. EXPECT_EQ("\"some \"\n"
  6815. "\"text\"",
  6816. format("\"some text\"", getLLVMStyleWithColumns(7)));
  6817. EXPECT_EQ("\"some\"\n"
  6818. "\" tex\"\n"
  6819. "\"t\"",
  6820. format("\"some text\"", getLLVMStyleWithColumns(6)));
  6821. EXPECT_EQ("\"some\"\n"
  6822. "\" tex\"\n"
  6823. "\" and\"",
  6824. format("\"some tex and\"", getLLVMStyleWithColumns(6)));
  6825. EXPECT_EQ("\"some\"\n"
  6826. "\"/tex\"\n"
  6827. "\"/and\"",
  6828. format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
  6829. EXPECT_EQ("variable =\n"
  6830. " \"long string \"\n"
  6831. " \"literal\";",
  6832. format("variable = \"long string literal\";",
  6833. getLLVMStyleWithColumns(20)));
  6834. EXPECT_EQ("variable = f(\n"
  6835. " \"long string \"\n"
  6836. " \"literal\",\n"
  6837. " short,\n"
  6838. " loooooooooooooooooooong);",
  6839. format("variable = f(\"long string literal\", short, "
  6840. "loooooooooooooooooooong);",
  6841. getLLVMStyleWithColumns(20)));
  6842. EXPECT_EQ("f(g(\"long string \"\n"
  6843. " \"literal\"),\n"
  6844. " b);",
  6845. format("f(g(\"long string literal\"), b);",
  6846. getLLVMStyleWithColumns(20)));
  6847. EXPECT_EQ("f(g(\"long string \"\n"
  6848. " \"literal\",\n"
  6849. " a),\n"
  6850. " b);",
  6851. format("f(g(\"long string literal\", a), b);",
  6852. getLLVMStyleWithColumns(20)));
  6853. EXPECT_EQ(
  6854. "f(\"one two\".split(\n"
  6855. " variable));",
  6856. format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
  6857. EXPECT_EQ("f(\"one two three four five six \"\n"
  6858. " \"seven\".split(\n"
  6859. " really_looooong_variable));",
  6860. format("f(\"one two three four five six seven\"."
  6861. "split(really_looooong_variable));",
  6862. getLLVMStyleWithColumns(33)));
  6863. EXPECT_EQ("f(\"some \"\n"
  6864. " \"text\",\n"
  6865. " other);",
  6866. format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
  6867. // Only break as a last resort.
  6868. verifyFormat(
  6869. "aaaaaaaaaaaaaaaaaaaa(\n"
  6870. " aaaaaaaaaaaaaaaaaaaa,\n"
  6871. " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
  6872. EXPECT_EQ(
  6873. "\"splitmea\"\n"
  6874. "\"trandomp\"\n"
  6875. "\"oint\"",
  6876. format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
  6877. EXPECT_EQ(
  6878. "\"split/\"\n"
  6879. "\"pathat/\"\n"
  6880. "\"slashes\"",
  6881. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  6882. EXPECT_EQ(
  6883. "\"split/\"\n"
  6884. "\"pathat/\"\n"
  6885. "\"slashes\"",
  6886. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  6887. EXPECT_EQ("\"split at \"\n"
  6888. "\"spaces/at/\"\n"
  6889. "\"slashes.at.any$\"\n"
  6890. "\"non-alphanumeric%\"\n"
  6891. "\"1111111111characte\"\n"
  6892. "\"rs\"",
  6893. format("\"split at "
  6894. "spaces/at/"
  6895. "slashes.at."
  6896. "any$non-"
  6897. "alphanumeric%"
  6898. "1111111111characte"
  6899. "rs\"",
  6900. getLLVMStyleWithColumns(20)));
  6901. // Verify that splitting the strings understands
  6902. // Style::AlwaysBreakBeforeMultilineStrings.
  6903. EXPECT_EQ("aaaaaaaaaaaa(aaaaaaaaaaaaa,\n"
  6904. " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
  6905. " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
  6906. format("aaaaaaaaaaaa(aaaaaaaaaaaaa, \"aaaaaaaaaaaaaaaaaaaaaa "
  6907. "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa "
  6908. "aaaaaaaaaaaaaaaaaaaaaa\");",
  6909. getGoogleStyle()));
  6910. EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  6911. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
  6912. format("return \"aaaaaaaaaaaaaaaaaaaaaa "
  6913. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
  6914. "aaaaaaaaaaaaaaaaaaaaaa\";",
  6915. getGoogleStyle()));
  6916. EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  6917. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  6918. format("llvm::outs() << "
  6919. "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
  6920. "aaaaaaaaaaaaaaaaaaa\";"));
  6921. EXPECT_EQ("ffff(\n"
  6922. " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  6923. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  6924. format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  6925. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  6926. getGoogleStyle()));
  6927. FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
  6928. AlignLeft.AlignEscapedNewlinesLeft = true;
  6929. EXPECT_EQ(
  6930. "#define A \\\n"
  6931. " \"some \" \\\n"
  6932. " \"text \" \\\n"
  6933. " \"other\";",
  6934. format("#define A \"some text other\";", AlignLeft));
  6935. }
  6936. TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
  6937. EXPECT_EQ(
  6938. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  6939. "(\n"
  6940. " \"x\t\");",
  6941. format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  6942. "aaaaaaa("
  6943. "\"x\t\");"));
  6944. }
  6945. TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
  6946. EXPECT_EQ(
  6947. "u8\"utf8 string \"\n"
  6948. "u8\"literal\";",
  6949. format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
  6950. EXPECT_EQ(
  6951. "u\"utf16 string \"\n"
  6952. "u\"literal\";",
  6953. format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
  6954. EXPECT_EQ(
  6955. "U\"utf32 string \"\n"
  6956. "U\"literal\";",
  6957. format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
  6958. EXPECT_EQ("L\"wide string \"\n"
  6959. "L\"literal\";",
  6960. format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
  6961. EXPECT_EQ("@\"NSString \"\n"
  6962. "@\"literal\";",
  6963. format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
  6964. // This input makes clang-format try to split the incomplete unicode escape
  6965. // sequence, which used to lead to a crasher.
  6966. verifyNoCrash(
  6967. "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  6968. getLLVMStyleWithColumns(60));
  6969. }
  6970. TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
  6971. FormatStyle Style = getGoogleStyleWithColumns(15);
  6972. EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
  6973. EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
  6974. EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
  6975. EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
  6976. EXPECT_EQ("u8R\"x(raw literal)x\";",
  6977. format("u8R\"x(raw literal)x\";", Style));
  6978. }
  6979. TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
  6980. FormatStyle Style = getLLVMStyleWithColumns(20);
  6981. EXPECT_EQ(
  6982. "_T(\"aaaaaaaaaaaaaa\")\n"
  6983. "_T(\"aaaaaaaaaaaaaa\")\n"
  6984. "_T(\"aaaaaaaaaaaa\")",
  6985. format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
  6986. EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
  6987. " _T(\"aaaaaa\"),\n"
  6988. " z);",
  6989. format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
  6990. // FIXME: Handle embedded spaces in one iteration.
  6991. // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
  6992. // "_T(\"aaaaaaaaaaaaa\")\n"
  6993. // "_T(\"aaaaaaaaaaaaa\")\n"
  6994. // "_T(\"a\")",
  6995. // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  6996. // getLLVMStyleWithColumns(20)));
  6997. EXPECT_EQ(
  6998. "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  6999. format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
  7000. }
  7001. TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
  7002. EXPECT_EQ(
  7003. "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7004. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7005. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  7006. format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7007. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7008. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
  7009. }
  7010. TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
  7011. EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
  7012. format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle()));
  7013. EXPECT_EQ("fffffffffff(g(R\"x(\n"
  7014. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7015. ")x\",\n"
  7016. " a),\n"
  7017. " b);",
  7018. format("fffffffffff(g(R\"x(\n"
  7019. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7020. ")x\", a), b);",
  7021. getGoogleStyleWithColumns(20)));
  7022. EXPECT_EQ("fffffffffff(\n"
  7023. " g(R\"x(qqq\n"
  7024. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7025. ")x\",\n"
  7026. " a),\n"
  7027. " b);",
  7028. format("fffffffffff(g(R\"x(qqq\n"
  7029. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7030. ")x\", a), b);",
  7031. getGoogleStyleWithColumns(20)));
  7032. EXPECT_EQ("fffffffffff(R\"x(\n"
  7033. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7034. ")x\");",
  7035. format("fffffffffff(R\"x(\n"
  7036. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7037. ")x\");",
  7038. getGoogleStyleWithColumns(20)));
  7039. EXPECT_EQ("fffffffffff(R\"x(\n"
  7040. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7041. ")x\" + bbbbbb);",
  7042. format("fffffffffff(R\"x(\n"
  7043. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7044. ")x\" + bbbbbb);",
  7045. getGoogleStyleWithColumns(20)));
  7046. EXPECT_EQ("fffffffffff(\n"
  7047. " R\"x(\n"
  7048. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7049. ")x\" +\n"
  7050. " bbbbbb);",
  7051. format("fffffffffff(\n"
  7052. " R\"x(\n"
  7053. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7054. ")x\" + bbbbbb);",
  7055. getGoogleStyleWithColumns(20)));
  7056. }
  7057. TEST_F(FormatTest, SkipsUnknownStringLiterals) {
  7058. verifyFormat("string a = \"unterminated;");
  7059. EXPECT_EQ("function(\"unterminated,\n"
  7060. " OtherParameter);",
  7061. format("function( \"unterminated,\n"
  7062. " OtherParameter);"));
  7063. }
  7064. TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
  7065. FormatStyle Style = getLLVMStyle();
  7066. Style.Standard = FormatStyle::LS_Cpp03;
  7067. EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
  7068. format("#define x(_a) printf(\"foo\"_a);", Style));
  7069. }
  7070. TEST_F(FormatTest, UnderstandsCpp1y) {
  7071. verifyFormat("int bi{1'000'000};");
  7072. }
  7073. TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
  7074. EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
  7075. " \"ddeeefff\");",
  7076. format("someFunction(\"aaabbbcccdddeeefff\");",
  7077. getLLVMStyleWithColumns(25)));
  7078. EXPECT_EQ("someFunction1234567890(\n"
  7079. " \"aaabbbcccdddeeefff\");",
  7080. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  7081. getLLVMStyleWithColumns(26)));
  7082. EXPECT_EQ("someFunction1234567890(\n"
  7083. " \"aaabbbcccdddeeeff\"\n"
  7084. " \"f\");",
  7085. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  7086. getLLVMStyleWithColumns(25)));
  7087. EXPECT_EQ("someFunction1234567890(\n"
  7088. " \"aaabbbcccdddeeeff\"\n"
  7089. " \"f\");",
  7090. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  7091. getLLVMStyleWithColumns(24)));
  7092. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  7093. " \"ddde \"\n"
  7094. " \"efff\");",
  7095. format("someFunction(\"aaabbbcc ddde efff\");",
  7096. getLLVMStyleWithColumns(25)));
  7097. EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
  7098. " \"ddeeefff\");",
  7099. format("someFunction(\"aaabbbccc ddeeefff\");",
  7100. getLLVMStyleWithColumns(25)));
  7101. EXPECT_EQ("someFunction1234567890(\n"
  7102. " \"aaabb \"\n"
  7103. " \"cccdddeeefff\");",
  7104. format("someFunction1234567890(\"aaabb cccdddeeefff\");",
  7105. getLLVMStyleWithColumns(25)));
  7106. EXPECT_EQ("#define A \\\n"
  7107. " string s = \\\n"
  7108. " \"123456789\" \\\n"
  7109. " \"0\"; \\\n"
  7110. " int i;",
  7111. format("#define A string s = \"1234567890\"; int i;",
  7112. getLLVMStyleWithColumns(20)));
  7113. // FIXME: Put additional penalties on breaking at non-whitespace locations.
  7114. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  7115. " \"dddeeeff\"\n"
  7116. " \"f\");",
  7117. format("someFunction(\"aaabbbcc dddeeefff\");",
  7118. getLLVMStyleWithColumns(25)));
  7119. }
  7120. TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
  7121. EXPECT_EQ("\"\\a\"",
  7122. format("\"\\a\"", getLLVMStyleWithColumns(3)));
  7123. EXPECT_EQ("\"\\\"",
  7124. format("\"\\\"", getLLVMStyleWithColumns(2)));
  7125. EXPECT_EQ("\"test\"\n"
  7126. "\"\\n\"",
  7127. format("\"test\\n\"", getLLVMStyleWithColumns(7)));
  7128. EXPECT_EQ("\"tes\\\\\"\n"
  7129. "\"n\"",
  7130. format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
  7131. EXPECT_EQ("\"\\\\\\\\\"\n"
  7132. "\"\\n\"",
  7133. format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
  7134. EXPECT_EQ("\"\\uff01\"",
  7135. format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
  7136. EXPECT_EQ("\"\\uff01\"\n"
  7137. "\"test\"",
  7138. format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
  7139. EXPECT_EQ("\"\\Uff01ff02\"",
  7140. format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
  7141. EXPECT_EQ("\"\\x000000000001\"\n"
  7142. "\"next\"",
  7143. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
  7144. EXPECT_EQ("\"\\x000000000001next\"",
  7145. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
  7146. EXPECT_EQ("\"\\x000000000001\"",
  7147. format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
  7148. EXPECT_EQ("\"test\"\n"
  7149. "\"\\000000\"\n"
  7150. "\"000001\"",
  7151. format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
  7152. EXPECT_EQ("\"test\\000\"\n"
  7153. "\"00000000\"\n"
  7154. "\"1\"",
  7155. format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
  7156. }
  7157. TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
  7158. verifyFormat("void f() {\n"
  7159. " return g() {}\n"
  7160. " void h() {}");
  7161. verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
  7162. "g();\n"
  7163. "}");
  7164. }
  7165. TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
  7166. verifyFormat(
  7167. "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
  7168. }
  7169. TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
  7170. verifyFormat("class X {\n"
  7171. " void f() {\n"
  7172. " }\n"
  7173. "};",
  7174. getLLVMStyleWithColumns(12));
  7175. }
  7176. TEST_F(FormatTest, ConfigurableIndentWidth) {
  7177. FormatStyle EightIndent = getLLVMStyleWithColumns(18);
  7178. EightIndent.IndentWidth = 8;
  7179. EightIndent.ContinuationIndentWidth = 8;
  7180. verifyFormat("void f() {\n"
  7181. " someFunction();\n"
  7182. " if (true) {\n"
  7183. " f();\n"
  7184. " }\n"
  7185. "}",
  7186. EightIndent);
  7187. verifyFormat("class X {\n"
  7188. " void f() {\n"
  7189. " }\n"
  7190. "};",
  7191. EightIndent);
  7192. verifyFormat("int x[] = {\n"
  7193. " call(),\n"
  7194. " call()};",
  7195. EightIndent);
  7196. }
  7197. TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
  7198. verifyFormat("double\n"
  7199. "f();",
  7200. getLLVMStyleWithColumns(8));
  7201. }
  7202. TEST_F(FormatTest, ConfigurableUseOfTab) {
  7203. FormatStyle Tab = getLLVMStyleWithColumns(42);
  7204. Tab.IndentWidth = 8;
  7205. Tab.UseTab = FormatStyle::UT_Always;
  7206. Tab.AlignEscapedNewlinesLeft = true;
  7207. EXPECT_EQ("if (aaaaaaaa && // q\n"
  7208. " bb)\t\t// w\n"
  7209. "\t;",
  7210. format("if (aaaaaaaa &&// q\n"
  7211. "bb)// w\n"
  7212. ";",
  7213. Tab));
  7214. EXPECT_EQ("if (aaa && bbb) // w\n"
  7215. "\t;",
  7216. format("if(aaa&&bbb)// w\n"
  7217. ";",
  7218. Tab));
  7219. verifyFormat("class X {\n"
  7220. "\tvoid f() {\n"
  7221. "\t\tsomeFunction(parameter1,\n"
  7222. "\t\t\t parameter2);\n"
  7223. "\t}\n"
  7224. "};",
  7225. Tab);
  7226. verifyFormat("#define A \\\n"
  7227. "\tvoid f() { \\\n"
  7228. "\t\tsomeFunction( \\\n"
  7229. "\t\t parameter1, \\\n"
  7230. "\t\t parameter2); \\\n"
  7231. "\t}",
  7232. Tab);
  7233. EXPECT_EQ("void f() {\n"
  7234. "\tf();\n"
  7235. "\tg();\n"
  7236. "}",
  7237. format("void f() {\n"
  7238. "\tf();\n"
  7239. "\tg();\n"
  7240. "}",
  7241. 0, 0, Tab));
  7242. EXPECT_EQ("void f() {\n"
  7243. "\tf();\n"
  7244. "\tg();\n"
  7245. "}",
  7246. format("void f() {\n"
  7247. "\tf();\n"
  7248. "\tg();\n"
  7249. "}",
  7250. 16, 0, Tab));
  7251. EXPECT_EQ("void f() {\n"
  7252. " \tf();\n"
  7253. "\tg();\n"
  7254. "}",
  7255. format("void f() {\n"
  7256. " \tf();\n"
  7257. " \tg();\n"
  7258. "}",
  7259. 21, 0, Tab));
  7260. Tab.TabWidth = 4;
  7261. Tab.IndentWidth = 8;
  7262. verifyFormat("class TabWidth4Indent8 {\n"
  7263. "\t\tvoid f() {\n"
  7264. "\t\t\t\tsomeFunction(parameter1,\n"
  7265. "\t\t\t\t\t\t\t parameter2);\n"
  7266. "\t\t}\n"
  7267. "};",
  7268. Tab);
  7269. Tab.TabWidth = 4;
  7270. Tab.IndentWidth = 4;
  7271. verifyFormat("class TabWidth4Indent4 {\n"
  7272. "\tvoid f() {\n"
  7273. "\t\tsomeFunction(parameter1,\n"
  7274. "\t\t\t\t\t parameter2);\n"
  7275. "\t}\n"
  7276. "};",
  7277. Tab);
  7278. Tab.TabWidth = 8;
  7279. Tab.IndentWidth = 4;
  7280. verifyFormat("class TabWidth8Indent4 {\n"
  7281. " void f() {\n"
  7282. "\tsomeFunction(parameter1,\n"
  7283. "\t\t parameter2);\n"
  7284. " }\n"
  7285. "};",
  7286. Tab);
  7287. Tab.TabWidth = 8;
  7288. Tab.IndentWidth = 8;
  7289. EXPECT_EQ("/*\n"
  7290. "\t a\t\tcomment\n"
  7291. "\t in multiple lines\n"
  7292. " */",
  7293. format(" /*\t \t \n"
  7294. " \t \t a\t\tcomment\t \t\n"
  7295. " \t \t in multiple lines\t\n"
  7296. " \t */",
  7297. Tab));
  7298. Tab.UseTab = FormatStyle::UT_ForIndentation;
  7299. verifyFormat("{\n"
  7300. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7301. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7302. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7303. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7304. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7305. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7306. "};",
  7307. Tab);
  7308. verifyFormat("enum A {\n"
  7309. "\ta1, // Force multiple lines\n"
  7310. "\ta2,\n"
  7311. "\ta3\n"
  7312. "};",
  7313. Tab);
  7314. EXPECT_EQ("if (aaaaaaaa && // q\n"
  7315. " bb) // w\n"
  7316. "\t;",
  7317. format("if (aaaaaaaa &&// q\n"
  7318. "bb)// w\n"
  7319. ";",
  7320. Tab));
  7321. verifyFormat("class X {\n"
  7322. "\tvoid f() {\n"
  7323. "\t\tsomeFunction(parameter1,\n"
  7324. "\t\t parameter2);\n"
  7325. "\t}\n"
  7326. "};",
  7327. Tab);
  7328. verifyFormat("{\n"
  7329. "\tQ({\n"
  7330. "\t\tint a;\n"
  7331. "\t\tsomeFunction(aaaaaaaa,\n"
  7332. "\t\t bbbbbbb);\n"
  7333. "\t}, p);\n"
  7334. "}",
  7335. Tab);
  7336. EXPECT_EQ("{\n"
  7337. "\t/* aaaa\n"
  7338. "\t bbbb */\n"
  7339. "}",
  7340. format("{\n"
  7341. "/* aaaa\n"
  7342. " bbbb */\n"
  7343. "}",
  7344. Tab));
  7345. EXPECT_EQ("{\n"
  7346. "\t/*\n"
  7347. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7348. "\t bbbbbbbbbbbbb\n"
  7349. "\t*/\n"
  7350. "}",
  7351. format("{\n"
  7352. "/*\n"
  7353. " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7354. "*/\n"
  7355. "}",
  7356. Tab));
  7357. EXPECT_EQ("{\n"
  7358. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7359. "\t// bbbbbbbbbbbbb\n"
  7360. "}",
  7361. format("{\n"
  7362. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7363. "}",
  7364. Tab));
  7365. EXPECT_EQ("{\n"
  7366. "\t/*\n"
  7367. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7368. "\t bbbbbbbbbbbbb\n"
  7369. "\t*/\n"
  7370. "}",
  7371. format("{\n"
  7372. "\t/*\n"
  7373. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7374. "\t*/\n"
  7375. "}",
  7376. Tab));
  7377. EXPECT_EQ("{\n"
  7378. "\t/*\n"
  7379. "\n"
  7380. "\t*/\n"
  7381. "}",
  7382. format("{\n"
  7383. "\t/*\n"
  7384. "\n"
  7385. "\t*/\n"
  7386. "}",
  7387. Tab));
  7388. EXPECT_EQ("{\n"
  7389. "\t/*\n"
  7390. " asdf\n"
  7391. "\t*/\n"
  7392. "}",
  7393. format("{\n"
  7394. "\t/*\n"
  7395. " asdf\n"
  7396. "\t*/\n"
  7397. "}",
  7398. Tab));
  7399. Tab.UseTab = FormatStyle::UT_Never;
  7400. EXPECT_EQ("/*\n"
  7401. " a\t\tcomment\n"
  7402. " in multiple lines\n"
  7403. " */",
  7404. format(" /*\t \t \n"
  7405. " \t \t a\t\tcomment\t \t\n"
  7406. " \t \t in multiple lines\t\n"
  7407. " \t */",
  7408. Tab));
  7409. EXPECT_EQ("/* some\n"
  7410. " comment */",
  7411. format(" \t \t /* some\n"
  7412. " \t \t comment */",
  7413. Tab));
  7414. EXPECT_EQ("int a; /* some\n"
  7415. " comment */",
  7416. format(" \t \t int a; /* some\n"
  7417. " \t \t comment */",
  7418. Tab));
  7419. EXPECT_EQ("int a; /* some\n"
  7420. "comment */",
  7421. format(" \t \t int\ta; /* some\n"
  7422. " \t \t comment */",
  7423. Tab));
  7424. EXPECT_EQ("f(\"\t\t\"); /* some\n"
  7425. " comment */",
  7426. format(" \t \t f(\"\t\t\"); /* some\n"
  7427. " \t \t comment */",
  7428. Tab));
  7429. EXPECT_EQ("{\n"
  7430. " /*\n"
  7431. " * Comment\n"
  7432. " */\n"
  7433. " int i;\n"
  7434. "}",
  7435. format("{\n"
  7436. "\t/*\n"
  7437. "\t * Comment\n"
  7438. "\t */\n"
  7439. "\t int i;\n"
  7440. "}"));
  7441. }
  7442. TEST_F(FormatTest, CalculatesOriginalColumn) {
  7443. EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7444. "q\"; /* some\n"
  7445. " comment */",
  7446. format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7447. "q\"; /* some\n"
  7448. " comment */",
  7449. getLLVMStyle()));
  7450. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  7451. "/* some\n"
  7452. " comment */",
  7453. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  7454. " /* some\n"
  7455. " comment */",
  7456. getLLVMStyle()));
  7457. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7458. "qqq\n"
  7459. "/* some\n"
  7460. " comment */",
  7461. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7462. "qqq\n"
  7463. " /* some\n"
  7464. " comment */",
  7465. getLLVMStyle()));
  7466. EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7467. "wwww; /* some\n"
  7468. " comment */",
  7469. format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7470. "wwww; /* some\n"
  7471. " comment */",
  7472. getLLVMStyle()));
  7473. }
  7474. TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
  7475. FormatStyle NoSpace = getLLVMStyle();
  7476. NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
  7477. verifyFormat("while(true)\n"
  7478. " continue;", NoSpace);
  7479. verifyFormat("for(;;)\n"
  7480. " continue;", NoSpace);
  7481. verifyFormat("if(true)\n"
  7482. " f();\n"
  7483. "else if(true)\n"
  7484. " f();", NoSpace);
  7485. verifyFormat("do {\n"
  7486. " do_something();\n"
  7487. "} while(something());", NoSpace);
  7488. verifyFormat("switch(x) {\n"
  7489. "default:\n"
  7490. " break;\n"
  7491. "}", NoSpace);
  7492. verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
  7493. verifyFormat("size_t x = sizeof(x);", NoSpace);
  7494. verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
  7495. verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
  7496. verifyFormat("alignas(128) char a[128];", NoSpace);
  7497. verifyFormat("size_t x = alignof(MyType);", NoSpace);
  7498. verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
  7499. verifyFormat("int f() throw(Deprecated);", NoSpace);
  7500. FormatStyle Space = getLLVMStyle();
  7501. Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
  7502. verifyFormat("int f ();", Space);
  7503. verifyFormat("void f (int a, T b) {\n"
  7504. " while (true)\n"
  7505. " continue;\n"
  7506. "}",
  7507. Space);
  7508. verifyFormat("if (true)\n"
  7509. " f ();\n"
  7510. "else if (true)\n"
  7511. " f ();",
  7512. Space);
  7513. verifyFormat("do {\n"
  7514. " do_something ();\n"
  7515. "} while (something ());",
  7516. Space);
  7517. verifyFormat("switch (x) {\n"
  7518. "default:\n"
  7519. " break;\n"
  7520. "}",
  7521. Space);
  7522. verifyFormat("A::A () : a (1) {}", Space);
  7523. verifyFormat("void f () __attribute__ ((asdf));", Space);
  7524. verifyFormat("*(&a + 1);\n"
  7525. "&((&a)[1]);\n"
  7526. "a[(b + c) * d];\n"
  7527. "(((a + 1) * 2) + 3) * 4;",
  7528. Space);
  7529. verifyFormat("#define A(x) x", Space);
  7530. verifyFormat("#define A (x) x", Space);
  7531. verifyFormat("#if defined(x)\n"
  7532. "#endif",
  7533. Space);
  7534. verifyFormat("auto i = std::make_unique<int> (5);", Space);
  7535. verifyFormat("size_t x = sizeof (x);", Space);
  7536. verifyFormat("auto f (int x) -> decltype (x);", Space);
  7537. verifyFormat("int f (T x) noexcept (x.create ());", Space);
  7538. verifyFormat("alignas (128) char a[128];", Space);
  7539. verifyFormat("size_t x = alignof (MyType);", Space);
  7540. verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
  7541. verifyFormat("int f () throw (Deprecated);", Space);
  7542. }
  7543. TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
  7544. FormatStyle Spaces = getLLVMStyle();
  7545. Spaces.SpacesInParentheses = true;
  7546. verifyFormat("call( x, y, z );", Spaces);
  7547. verifyFormat("while ( (bool)1 )\n"
  7548. " continue;", Spaces);
  7549. verifyFormat("for ( ;; )\n"
  7550. " continue;", Spaces);
  7551. verifyFormat("if ( true )\n"
  7552. " f();\n"
  7553. "else if ( true )\n"
  7554. " f();", Spaces);
  7555. verifyFormat("do {\n"
  7556. " do_something( (int)i );\n"
  7557. "} while ( something() );", Spaces);
  7558. verifyFormat("switch ( x ) {\n"
  7559. "default:\n"
  7560. " break;\n"
  7561. "}", Spaces);
  7562. Spaces.SpacesInParentheses = false;
  7563. Spaces.SpacesInCStyleCastParentheses = true;
  7564. verifyFormat("Type *A = ( Type * )P;", Spaces);
  7565. verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
  7566. verifyFormat("x = ( int32 )y;", Spaces);
  7567. verifyFormat("int a = ( int )(2.0f);", Spaces);
  7568. verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
  7569. verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
  7570. verifyFormat("#define x (( int )-1)", Spaces);
  7571. Spaces.SpacesInParentheses = false;
  7572. Spaces.SpaceInEmptyParentheses = true;
  7573. verifyFormat("call(x, y, z);", Spaces);
  7574. verifyFormat("call( )", Spaces);
  7575. // Run the first set of tests again with
  7576. // Spaces.SpacesInParentheses = false,
  7577. // Spaces.SpaceInEmptyParentheses = true and
  7578. // Spaces.SpacesInCStyleCastParentheses = true
  7579. Spaces.SpacesInParentheses = false,
  7580. Spaces.SpaceInEmptyParentheses = true;
  7581. Spaces.SpacesInCStyleCastParentheses = true;
  7582. verifyFormat("call(x, y, z);", Spaces);
  7583. verifyFormat("while (( bool )1)\n"
  7584. " continue;", Spaces);
  7585. verifyFormat("for (;;)\n"
  7586. " continue;", Spaces);
  7587. verifyFormat("if (true)\n"
  7588. " f( );\n"
  7589. "else if (true)\n"
  7590. " f( );", Spaces);
  7591. verifyFormat("do {\n"
  7592. " do_something(( int )i);\n"
  7593. "} while (something( ));", Spaces);
  7594. verifyFormat("switch (x) {\n"
  7595. "default:\n"
  7596. " break;\n"
  7597. "}", Spaces);
  7598. Spaces.SpaceAfterCStyleCast = true;
  7599. verifyFormat("call(x, y, z);", Spaces);
  7600. verifyFormat("while (( bool ) 1)\n"
  7601. " continue;",
  7602. Spaces);
  7603. verifyFormat("for (;;)\n"
  7604. " continue;",
  7605. Spaces);
  7606. verifyFormat("if (true)\n"
  7607. " f( );\n"
  7608. "else if (true)\n"
  7609. " f( );",
  7610. Spaces);
  7611. verifyFormat("do {\n"
  7612. " do_something(( int ) i);\n"
  7613. "} while (something( ));",
  7614. Spaces);
  7615. verifyFormat("switch (x) {\n"
  7616. "default:\n"
  7617. " break;\n"
  7618. "}",
  7619. Spaces);
  7620. Spaces.SpacesInCStyleCastParentheses = false;
  7621. Spaces.SpaceAfterCStyleCast = true;
  7622. verifyFormat("while ((bool) 1)\n"
  7623. " continue;",
  7624. Spaces);
  7625. verifyFormat("do {\n"
  7626. " do_something((int) i);\n"
  7627. "} while (something( ));",
  7628. Spaces);
  7629. }
  7630. TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
  7631. verifyFormat("int a[5];");
  7632. verifyFormat("a[3] += 42;");
  7633. FormatStyle Spaces = getLLVMStyle();
  7634. Spaces.SpacesInSquareBrackets = true;
  7635. // Lambdas unchanged.
  7636. verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
  7637. verifyFormat("return [i, args...] {};", Spaces);
  7638. // Not lambdas.
  7639. verifyFormat("int a[ 5 ];", Spaces);
  7640. verifyFormat("a[ 3 ] += 42;", Spaces);
  7641. verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
  7642. verifyFormat("double &operator[](int i) { return 0; }\n"
  7643. "int i;",
  7644. Spaces);
  7645. verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
  7646. verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
  7647. verifyFormat("int i = (*b)[ a ]->f();", Spaces);
  7648. }
  7649. TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
  7650. verifyFormat("int a = 5;");
  7651. verifyFormat("a += 42;");
  7652. verifyFormat("a or_eq 8;");
  7653. FormatStyle Spaces = getLLVMStyle();
  7654. Spaces.SpaceBeforeAssignmentOperators = false;
  7655. verifyFormat("int a= 5;", Spaces);
  7656. verifyFormat("a+= 42;", Spaces);
  7657. verifyFormat("a or_eq 8;", Spaces);
  7658. }
  7659. TEST_F(FormatTest, LinuxBraceBreaking) {
  7660. FormatStyle LinuxBraceStyle = getLLVMStyle();
  7661. LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
  7662. verifyFormat("namespace a\n"
  7663. "{\n"
  7664. "class A\n"
  7665. "{\n"
  7666. " void f()\n"
  7667. " {\n"
  7668. " if (true) {\n"
  7669. " a();\n"
  7670. " b();\n"
  7671. " }\n"
  7672. " }\n"
  7673. " void g() { return; }\n"
  7674. "};\n"
  7675. "struct B {\n"
  7676. " int x;\n"
  7677. "};\n"
  7678. "}\n",
  7679. LinuxBraceStyle);
  7680. verifyFormat("enum X {\n"
  7681. " Y = 0,\n"
  7682. "}\n",
  7683. LinuxBraceStyle);
  7684. verifyFormat("struct S {\n"
  7685. " int Type;\n"
  7686. " union {\n"
  7687. " int x;\n"
  7688. " double y;\n"
  7689. " } Value;\n"
  7690. " class C\n"
  7691. " {\n"
  7692. " MyFavoriteType Value;\n"
  7693. " } Class;\n"
  7694. "}\n",
  7695. LinuxBraceStyle);
  7696. }
  7697. TEST_F(FormatTest, StroustrupBraceBreaking) {
  7698. FormatStyle StroustrupBraceStyle = getLLVMStyle();
  7699. StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  7700. verifyFormat("namespace a {\n"
  7701. "class A {\n"
  7702. " void f()\n"
  7703. " {\n"
  7704. " if (true) {\n"
  7705. " a();\n"
  7706. " b();\n"
  7707. " }\n"
  7708. " }\n"
  7709. " void g() { return; }\n"
  7710. "};\n"
  7711. "struct B {\n"
  7712. " int x;\n"
  7713. "};\n"
  7714. "}\n",
  7715. StroustrupBraceStyle);
  7716. verifyFormat("void foo()\n"
  7717. "{\n"
  7718. " if (a) {\n"
  7719. " a();\n"
  7720. " }\n"
  7721. " else {\n"
  7722. " b();\n"
  7723. " }\n"
  7724. "}\n",
  7725. StroustrupBraceStyle);
  7726. verifyFormat("#ifdef _DEBUG\n"
  7727. "int foo(int i = 0)\n"
  7728. "#else\n"
  7729. "int foo(int i = 5)\n"
  7730. "#endif\n"
  7731. "{\n"
  7732. " return i;\n"
  7733. "}",
  7734. StroustrupBraceStyle);
  7735. verifyFormat("void foo() {}\n"
  7736. "void bar()\n"
  7737. "#ifdef _DEBUG\n"
  7738. "{\n"
  7739. " foo();\n"
  7740. "}\n"
  7741. "#else\n"
  7742. "{\n"
  7743. "}\n"
  7744. "#endif",
  7745. StroustrupBraceStyle);
  7746. verifyFormat("void foobar() { int i = 5; }\n"
  7747. "#ifdef _DEBUG\n"
  7748. "void bar() {}\n"
  7749. "#else\n"
  7750. "void bar() { foobar(); }\n"
  7751. "#endif",
  7752. StroustrupBraceStyle);
  7753. }
  7754. TEST_F(FormatTest, AllmanBraceBreaking) {
  7755. FormatStyle AllmanBraceStyle = getLLVMStyle();
  7756. AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
  7757. verifyFormat("namespace a\n"
  7758. "{\n"
  7759. "class A\n"
  7760. "{\n"
  7761. " void f()\n"
  7762. " {\n"
  7763. " if (true)\n"
  7764. " {\n"
  7765. " a();\n"
  7766. " b();\n"
  7767. " }\n"
  7768. " }\n"
  7769. " void g() { return; }\n"
  7770. "};\n"
  7771. "struct B\n"
  7772. "{\n"
  7773. " int x;\n"
  7774. "};\n"
  7775. "}",
  7776. AllmanBraceStyle);
  7777. verifyFormat("void f()\n"
  7778. "{\n"
  7779. " if (true)\n"
  7780. " {\n"
  7781. " a();\n"
  7782. " }\n"
  7783. " else if (false)\n"
  7784. " {\n"
  7785. " b();\n"
  7786. " }\n"
  7787. " else\n"
  7788. " {\n"
  7789. " c();\n"
  7790. " }\n"
  7791. "}\n",
  7792. AllmanBraceStyle);
  7793. verifyFormat("void f()\n"
  7794. "{\n"
  7795. " for (int i = 0; i < 10; ++i)\n"
  7796. " {\n"
  7797. " a();\n"
  7798. " }\n"
  7799. " while (false)\n"
  7800. " {\n"
  7801. " b();\n"
  7802. " }\n"
  7803. " do\n"
  7804. " {\n"
  7805. " c();\n"
  7806. " } while (false)\n"
  7807. "}\n",
  7808. AllmanBraceStyle);
  7809. verifyFormat("void f(int a)\n"
  7810. "{\n"
  7811. " switch (a)\n"
  7812. " {\n"
  7813. " case 0:\n"
  7814. " break;\n"
  7815. " case 1:\n"
  7816. " {\n"
  7817. " break;\n"
  7818. " }\n"
  7819. " case 2:\n"
  7820. " {\n"
  7821. " }\n"
  7822. " break;\n"
  7823. " default:\n"
  7824. " break;\n"
  7825. " }\n"
  7826. "}\n",
  7827. AllmanBraceStyle);
  7828. verifyFormat("enum X\n"
  7829. "{\n"
  7830. " Y = 0,\n"
  7831. "}\n",
  7832. AllmanBraceStyle);
  7833. verifyFormat("enum X\n"
  7834. "{\n"
  7835. " Y = 0\n"
  7836. "}\n",
  7837. AllmanBraceStyle);
  7838. verifyFormat("@interface BSApplicationController ()\n"
  7839. "{\n"
  7840. "@private\n"
  7841. " id _extraIvar;\n"
  7842. "}\n"
  7843. "@end\n",
  7844. AllmanBraceStyle);
  7845. verifyFormat("#ifdef _DEBUG\n"
  7846. "int foo(int i = 0)\n"
  7847. "#else\n"
  7848. "int foo(int i = 5)\n"
  7849. "#endif\n"
  7850. "{\n"
  7851. " return i;\n"
  7852. "}",
  7853. AllmanBraceStyle);
  7854. verifyFormat("void foo() {}\n"
  7855. "void bar()\n"
  7856. "#ifdef _DEBUG\n"
  7857. "{\n"
  7858. " foo();\n"
  7859. "}\n"
  7860. "#else\n"
  7861. "{\n"
  7862. "}\n"
  7863. "#endif",
  7864. AllmanBraceStyle);
  7865. verifyFormat("void foobar() { int i = 5; }\n"
  7866. "#ifdef _DEBUG\n"
  7867. "void bar() {}\n"
  7868. "#else\n"
  7869. "void bar() { foobar(); }\n"
  7870. "#endif",
  7871. AllmanBraceStyle);
  7872. // This shouldn't affect ObjC blocks..
  7873. verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
  7874. " // ...\n"
  7875. " int i;\n"
  7876. "}];",
  7877. AllmanBraceStyle);
  7878. verifyFormat("void (^block)(void) = ^{\n"
  7879. " // ...\n"
  7880. " int i;\n"
  7881. "};",
  7882. AllmanBraceStyle);
  7883. // .. or dict literals.
  7884. verifyFormat("void f()\n"
  7885. "{\n"
  7886. " [object someMethod:@{ @\"a\" : @\"b\" }];\n"
  7887. "}",
  7888. AllmanBraceStyle);
  7889. verifyFormat("int f()\n"
  7890. "{ // comment\n"
  7891. " return 42;\n"
  7892. "}",
  7893. AllmanBraceStyle);
  7894. AllmanBraceStyle.ColumnLimit = 19;
  7895. verifyFormat("void f() { int i; }", AllmanBraceStyle);
  7896. AllmanBraceStyle.ColumnLimit = 18;
  7897. verifyFormat("void f()\n"
  7898. "{\n"
  7899. " int i;\n"
  7900. "}",
  7901. AllmanBraceStyle);
  7902. AllmanBraceStyle.ColumnLimit = 80;
  7903. FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
  7904. BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
  7905. BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
  7906. verifyFormat("void f(bool b)\n"
  7907. "{\n"
  7908. " if (b)\n"
  7909. " {\n"
  7910. " return;\n"
  7911. " }\n"
  7912. "}\n",
  7913. BreakBeforeBraceShortIfs);
  7914. verifyFormat("void f(bool b)\n"
  7915. "{\n"
  7916. " if (b) return;\n"
  7917. "}\n",
  7918. BreakBeforeBraceShortIfs);
  7919. verifyFormat("void f(bool b)\n"
  7920. "{\n"
  7921. " while (b)\n"
  7922. " {\n"
  7923. " return;\n"
  7924. " }\n"
  7925. "}\n",
  7926. BreakBeforeBraceShortIfs);
  7927. }
  7928. TEST_F(FormatTest, GNUBraceBreaking) {
  7929. FormatStyle GNUBraceStyle = getLLVMStyle();
  7930. GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
  7931. verifyFormat("namespace a\n"
  7932. "{\n"
  7933. "class A\n"
  7934. "{\n"
  7935. " void f()\n"
  7936. " {\n"
  7937. " int a;\n"
  7938. " {\n"
  7939. " int b;\n"
  7940. " }\n"
  7941. " if (true)\n"
  7942. " {\n"
  7943. " a();\n"
  7944. " b();\n"
  7945. " }\n"
  7946. " }\n"
  7947. " void g() { return; }\n"
  7948. "}\n"
  7949. "}",
  7950. GNUBraceStyle);
  7951. verifyFormat("void f()\n"
  7952. "{\n"
  7953. " if (true)\n"
  7954. " {\n"
  7955. " a();\n"
  7956. " }\n"
  7957. " else if (false)\n"
  7958. " {\n"
  7959. " b();\n"
  7960. " }\n"
  7961. " else\n"
  7962. " {\n"
  7963. " c();\n"
  7964. " }\n"
  7965. "}\n",
  7966. GNUBraceStyle);
  7967. verifyFormat("void f()\n"
  7968. "{\n"
  7969. " for (int i = 0; i < 10; ++i)\n"
  7970. " {\n"
  7971. " a();\n"
  7972. " }\n"
  7973. " while (false)\n"
  7974. " {\n"
  7975. " b();\n"
  7976. " }\n"
  7977. " do\n"
  7978. " {\n"
  7979. " c();\n"
  7980. " }\n"
  7981. " while (false);\n"
  7982. "}\n",
  7983. GNUBraceStyle);
  7984. verifyFormat("void f(int a)\n"
  7985. "{\n"
  7986. " switch (a)\n"
  7987. " {\n"
  7988. " case 0:\n"
  7989. " break;\n"
  7990. " case 1:\n"
  7991. " {\n"
  7992. " break;\n"
  7993. " }\n"
  7994. " case 2:\n"
  7995. " {\n"
  7996. " }\n"
  7997. " break;\n"
  7998. " default:\n"
  7999. " break;\n"
  8000. " }\n"
  8001. "}\n",
  8002. GNUBraceStyle);
  8003. verifyFormat("enum X\n"
  8004. "{\n"
  8005. " Y = 0,\n"
  8006. "}\n",
  8007. GNUBraceStyle);
  8008. verifyFormat("@interface BSApplicationController ()\n"
  8009. "{\n"
  8010. "@private\n"
  8011. " id _extraIvar;\n"
  8012. "}\n"
  8013. "@end\n",
  8014. GNUBraceStyle);
  8015. verifyFormat("#ifdef _DEBUG\n"
  8016. "int foo(int i = 0)\n"
  8017. "#else\n"
  8018. "int foo(int i = 5)\n"
  8019. "#endif\n"
  8020. "{\n"
  8021. " return i;\n"
  8022. "}",
  8023. GNUBraceStyle);
  8024. verifyFormat("void foo() {}\n"
  8025. "void bar()\n"
  8026. "#ifdef _DEBUG\n"
  8027. "{\n"
  8028. " foo();\n"
  8029. "}\n"
  8030. "#else\n"
  8031. "{\n"
  8032. "}\n"
  8033. "#endif",
  8034. GNUBraceStyle);
  8035. verifyFormat("void foobar() { int i = 5; }\n"
  8036. "#ifdef _DEBUG\n"
  8037. "void bar() {}\n"
  8038. "#else\n"
  8039. "void bar() { foobar(); }\n"
  8040. "#endif",
  8041. GNUBraceStyle);
  8042. }
  8043. TEST_F(FormatTest, CatchExceptionReferenceBinding) {
  8044. verifyFormat("void f() {\n"
  8045. " try {\n"
  8046. " } catch (const Exception &e) {\n"
  8047. " }\n"
  8048. "}\n",
  8049. getLLVMStyle());
  8050. }
  8051. TEST_F(FormatTest, UnderstandsPragmas) {
  8052. verifyFormat("#pragma omp reduction(| : var)");
  8053. verifyFormat("#pragma omp reduction(+ : var)");
  8054. EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
  8055. "(including parentheses).",
  8056. format("#pragma mark Any non-hyphenated or hyphenated string "
  8057. "(including parentheses)."));
  8058. }
  8059. #define EXPECT_ALL_STYLES_EQUAL(Styles) \
  8060. for (size_t i = 1; i < Styles.size(); ++i) \
  8061. EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " \
  8062. << Styles.size() \
  8063. << " differs from Style #0"
  8064. TEST_F(FormatTest, GetsPredefinedStyleByName) {
  8065. SmallVector<FormatStyle, 3> Styles;
  8066. Styles.resize(3);
  8067. Styles[0] = getLLVMStyle();
  8068. EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
  8069. EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
  8070. EXPECT_ALL_STYLES_EQUAL(Styles);
  8071. Styles[0] = getGoogleStyle();
  8072. EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
  8073. EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
  8074. EXPECT_ALL_STYLES_EQUAL(Styles);
  8075. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  8076. EXPECT_TRUE(
  8077. getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
  8078. EXPECT_TRUE(
  8079. getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
  8080. EXPECT_ALL_STYLES_EQUAL(Styles);
  8081. Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
  8082. EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
  8083. EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
  8084. EXPECT_ALL_STYLES_EQUAL(Styles);
  8085. Styles[0] = getMozillaStyle();
  8086. EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
  8087. EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
  8088. EXPECT_ALL_STYLES_EQUAL(Styles);
  8089. Styles[0] = getWebKitStyle();
  8090. EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
  8091. EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
  8092. EXPECT_ALL_STYLES_EQUAL(Styles);
  8093. Styles[0] = getGNUStyle();
  8094. EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
  8095. EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
  8096. EXPECT_ALL_STYLES_EQUAL(Styles);
  8097. EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
  8098. }
  8099. TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
  8100. SmallVector<FormatStyle, 8> Styles;
  8101. Styles.resize(2);
  8102. Styles[0] = getGoogleStyle();
  8103. Styles[1] = getLLVMStyle();
  8104. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  8105. EXPECT_ALL_STYLES_EQUAL(Styles);
  8106. Styles.resize(5);
  8107. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  8108. Styles[1] = getLLVMStyle();
  8109. Styles[1].Language = FormatStyle::LK_JavaScript;
  8110. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  8111. Styles[2] = getLLVMStyle();
  8112. Styles[2].Language = FormatStyle::LK_JavaScript;
  8113. EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
  8114. "BasedOnStyle: Google",
  8115. &Styles[2]).value());
  8116. Styles[3] = getLLVMStyle();
  8117. Styles[3].Language = FormatStyle::LK_JavaScript;
  8118. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
  8119. "Language: JavaScript",
  8120. &Styles[3]).value());
  8121. Styles[4] = getLLVMStyle();
  8122. Styles[4].Language = FormatStyle::LK_JavaScript;
  8123. EXPECT_EQ(0, parseConfiguration("---\n"
  8124. "BasedOnStyle: LLVM\n"
  8125. "IndentWidth: 123\n"
  8126. "---\n"
  8127. "BasedOnStyle: Google\n"
  8128. "Language: JavaScript",
  8129. &Styles[4]).value());
  8130. EXPECT_ALL_STYLES_EQUAL(Styles);
  8131. }
  8132. #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \
  8133. Style.FIELD = false; \
  8134. EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \
  8135. EXPECT_TRUE(Style.FIELD); \
  8136. EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \
  8137. EXPECT_FALSE(Style.FIELD);
  8138. #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
  8139. #define CHECK_PARSE(TEXT, FIELD, VALUE) \
  8140. EXPECT_NE(VALUE, Style.FIELD); \
  8141. EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \
  8142. EXPECT_EQ(VALUE, Style.FIELD)
  8143. TEST_F(FormatTest, ParsesConfigurationBools) {
  8144. FormatStyle Style = {};
  8145. Style.Language = FormatStyle::LK_Cpp;
  8146. CHECK_PARSE_BOOL(AlignAfterOpenBracket);
  8147. CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
  8148. CHECK_PARSE_BOOL(AlignOperands);
  8149. CHECK_PARSE_BOOL(AlignTrailingComments);
  8150. CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
  8151. CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
  8152. CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
  8153. CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
  8154. CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
  8155. CHECK_PARSE_BOOL(AlwaysBreakAfterDefinitionReturnType);
  8156. CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
  8157. CHECK_PARSE_BOOL(BinPackParameters);
  8158. CHECK_PARSE_BOOL(BinPackArguments);
  8159. CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
  8160. CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
  8161. CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
  8162. CHECK_PARSE_BOOL(DerivePointerAlignment);
  8163. CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
  8164. CHECK_PARSE_BOOL(IndentCaseLabels);
  8165. CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
  8166. CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
  8167. CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
  8168. CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
  8169. CHECK_PARSE_BOOL(Cpp11BracedListStyle);
  8170. CHECK_PARSE_BOOL(SpacesInParentheses);
  8171. CHECK_PARSE_BOOL(SpacesInSquareBrackets);
  8172. CHECK_PARSE_BOOL(SpacesInAngles);
  8173. CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
  8174. CHECK_PARSE_BOOL(SpacesInContainerLiterals);
  8175. CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
  8176. CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
  8177. CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
  8178. }
  8179. #undef CHECK_PARSE_BOOL
  8180. TEST_F(FormatTest, ParsesConfiguration) {
  8181. FormatStyle Style = {};
  8182. Style.Language = FormatStyle::LK_Cpp;
  8183. CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
  8184. CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
  8185. ConstructorInitializerIndentWidth, 1234u);
  8186. CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
  8187. CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
  8188. CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
  8189. CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
  8190. PenaltyBreakBeforeFirstCallParameter, 1234u);
  8191. CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
  8192. CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
  8193. PenaltyReturnTypeOnItsOwnLine, 1234u);
  8194. CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
  8195. SpacesBeforeTrailingComments, 1234u);
  8196. CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
  8197. CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
  8198. Style.PointerAlignment = FormatStyle::PAS_Middle;
  8199. CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
  8200. FormatStyle::PAS_Left);
  8201. CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
  8202. FormatStyle::PAS_Right);
  8203. CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
  8204. FormatStyle::PAS_Middle);
  8205. // For backward compatibility:
  8206. CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
  8207. FormatStyle::PAS_Left);
  8208. CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
  8209. FormatStyle::PAS_Right);
  8210. CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
  8211. FormatStyle::PAS_Middle);
  8212. Style.Standard = FormatStyle::LS_Auto;
  8213. CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
  8214. CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
  8215. CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
  8216. CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
  8217. CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
  8218. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  8219. CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
  8220. BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
  8221. CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
  8222. FormatStyle::BOS_None);
  8223. CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
  8224. FormatStyle::BOS_All);
  8225. // For backward compatibility:
  8226. CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
  8227. FormatStyle::BOS_None);
  8228. CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
  8229. FormatStyle::BOS_All);
  8230. Style.UseTab = FormatStyle::UT_ForIndentation;
  8231. CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
  8232. CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
  8233. CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
  8234. // For backward compatibility:
  8235. CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
  8236. CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
  8237. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  8238. CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
  8239. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
  8240. CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
  8241. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
  8242. CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
  8243. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
  8244. CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
  8245. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
  8246. // For backward compatibility:
  8247. CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
  8248. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
  8249. CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
  8250. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
  8251. Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
  8252. CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
  8253. FormatStyle::SBPO_Never);
  8254. CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
  8255. FormatStyle::SBPO_Always);
  8256. CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
  8257. FormatStyle::SBPO_ControlStatements);
  8258. // For backward compatibility:
  8259. CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
  8260. FormatStyle::SBPO_Never);
  8261. CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
  8262. FormatStyle::SBPO_ControlStatements);
  8263. Style.ColumnLimit = 123;
  8264. FormatStyle BaseStyle = getLLVMStyle();
  8265. CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
  8266. CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
  8267. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  8268. CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
  8269. FormatStyle::BS_Attach);
  8270. CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
  8271. FormatStyle::BS_Linux);
  8272. CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
  8273. FormatStyle::BS_Stroustrup);
  8274. CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
  8275. FormatStyle::BS_Allman);
  8276. CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
  8277. Style.NamespaceIndentation = FormatStyle::NI_All;
  8278. CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
  8279. FormatStyle::NI_None);
  8280. CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
  8281. FormatStyle::NI_Inner);
  8282. CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
  8283. FormatStyle::NI_All);
  8284. Style.ForEachMacros.clear();
  8285. std::vector<std::string> BoostForeach;
  8286. BoostForeach.push_back("BOOST_FOREACH");
  8287. CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
  8288. std::vector<std::string> BoostAndQForeach;
  8289. BoostAndQForeach.push_back("BOOST_FOREACH");
  8290. BoostAndQForeach.push_back("Q_FOREACH");
  8291. CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
  8292. BoostAndQForeach);
  8293. }
  8294. TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
  8295. FormatStyle Style = {};
  8296. Style.Language = FormatStyle::LK_Cpp;
  8297. CHECK_PARSE("Language: Cpp\n"
  8298. "IndentWidth: 12",
  8299. IndentWidth, 12u);
  8300. EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
  8301. "IndentWidth: 34",
  8302. &Style),
  8303. ParseError::Unsuitable);
  8304. EXPECT_EQ(12u, Style.IndentWidth);
  8305. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  8306. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  8307. Style.Language = FormatStyle::LK_JavaScript;
  8308. CHECK_PARSE("Language: JavaScript\n"
  8309. "IndentWidth: 12",
  8310. IndentWidth, 12u);
  8311. CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
  8312. EXPECT_EQ(parseConfiguration("Language: Cpp\n"
  8313. "IndentWidth: 34",
  8314. &Style),
  8315. ParseError::Unsuitable);
  8316. EXPECT_EQ(23u, Style.IndentWidth);
  8317. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  8318. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  8319. CHECK_PARSE("BasedOnStyle: LLVM\n"
  8320. "IndentWidth: 67",
  8321. IndentWidth, 67u);
  8322. CHECK_PARSE("---\n"
  8323. "Language: JavaScript\n"
  8324. "IndentWidth: 12\n"
  8325. "---\n"
  8326. "Language: Cpp\n"
  8327. "IndentWidth: 34\n"
  8328. "...\n",
  8329. IndentWidth, 12u);
  8330. Style.Language = FormatStyle::LK_Cpp;
  8331. CHECK_PARSE("---\n"
  8332. "Language: JavaScript\n"
  8333. "IndentWidth: 12\n"
  8334. "---\n"
  8335. "Language: Cpp\n"
  8336. "IndentWidth: 34\n"
  8337. "...\n",
  8338. IndentWidth, 34u);
  8339. CHECK_PARSE("---\n"
  8340. "IndentWidth: 78\n"
  8341. "---\n"
  8342. "Language: JavaScript\n"
  8343. "IndentWidth: 56\n"
  8344. "...\n",
  8345. IndentWidth, 78u);
  8346. Style.ColumnLimit = 123;
  8347. Style.IndentWidth = 234;
  8348. Style.BreakBeforeBraces = FormatStyle::BS_Linux;
  8349. Style.TabWidth = 345;
  8350. EXPECT_FALSE(parseConfiguration("---\n"
  8351. "IndentWidth: 456\n"
  8352. "BreakBeforeBraces: Allman\n"
  8353. "---\n"
  8354. "Language: JavaScript\n"
  8355. "IndentWidth: 111\n"
  8356. "TabWidth: 111\n"
  8357. "---\n"
  8358. "Language: Cpp\n"
  8359. "BreakBeforeBraces: Stroustrup\n"
  8360. "TabWidth: 789\n"
  8361. "...\n",
  8362. &Style));
  8363. EXPECT_EQ(123u, Style.ColumnLimit);
  8364. EXPECT_EQ(456u, Style.IndentWidth);
  8365. EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
  8366. EXPECT_EQ(789u, Style.TabWidth);
  8367. EXPECT_EQ(parseConfiguration("---\n"
  8368. "Language: JavaScript\n"
  8369. "IndentWidth: 56\n"
  8370. "---\n"
  8371. "IndentWidth: 78\n"
  8372. "...\n",
  8373. &Style),
  8374. ParseError::Error);
  8375. EXPECT_EQ(parseConfiguration("---\n"
  8376. "Language: JavaScript\n"
  8377. "IndentWidth: 56\n"
  8378. "---\n"
  8379. "Language: JavaScript\n"
  8380. "IndentWidth: 78\n"
  8381. "...\n",
  8382. &Style),
  8383. ParseError::Error);
  8384. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  8385. }
  8386. #undef CHECK_PARSE
  8387. TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
  8388. FormatStyle Style = {};
  8389. Style.Language = FormatStyle::LK_JavaScript;
  8390. Style.BreakBeforeTernaryOperators = true;
  8391. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
  8392. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  8393. Style.BreakBeforeTernaryOperators = true;
  8394. EXPECT_EQ(0, parseConfiguration("---\n"
  8395. "BasedOnStyle: Google\n"
  8396. "---\n"
  8397. "Language: JavaScript\n"
  8398. "IndentWidth: 76\n"
  8399. "...\n", &Style).value());
  8400. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  8401. EXPECT_EQ(76u, Style.IndentWidth);
  8402. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  8403. }
  8404. TEST_F(FormatTest, ConfigurationRoundTripTest) {
  8405. FormatStyle Style = getLLVMStyle();
  8406. std::string YAML = configurationAsText(Style);
  8407. FormatStyle ParsedStyle = {};
  8408. ParsedStyle.Language = FormatStyle::LK_Cpp;
  8409. EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
  8410. EXPECT_EQ(Style, ParsedStyle);
  8411. }
  8412. TEST_F(FormatTest, WorksFor8bitEncodings) {
  8413. EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
  8414. "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
  8415. "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
  8416. "\"\xef\xee\xf0\xf3...\"",
  8417. format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
  8418. "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
  8419. "\xef\xee\xf0\xf3...\"",
  8420. getLLVMStyleWithColumns(12)));
  8421. }
  8422. TEST_F(FormatTest, HandlesUTF8BOM) {
  8423. EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
  8424. EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
  8425. format("\xef\xbb\xbf#include <iostream>"));
  8426. EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
  8427. format("\xef\xbb\xbf\n#include <iostream>"));
  8428. }
  8429. // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
  8430. #if !defined(_MSC_VER)
  8431. TEST_F(FormatTest, CountsUTF8CharactersProperly) {
  8432. verifyFormat("\"Однажды в студёную зимнюю пору...\"",
  8433. getLLVMStyleWithColumns(35));
  8434. verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
  8435. getLLVMStyleWithColumns(31));
  8436. verifyFormat("// Однажды в студёную зимнюю пору...",
  8437. getLLVMStyleWithColumns(36));
  8438. verifyFormat("// 一 二 三 四 五 六 七 八 九 十",
  8439. getLLVMStyleWithColumns(32));
  8440. verifyFormat("/* Однажды в студёную зимнюю пору... */",
  8441. getLLVMStyleWithColumns(39));
  8442. verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
  8443. getLLVMStyleWithColumns(35));
  8444. }
  8445. TEST_F(FormatTest, SplitsUTF8Strings) {
  8446. // Non-printable characters' width is currently considered to be the length in
  8447. // bytes in UTF8. The characters can be displayed in very different manner
  8448. // (zero-width, single width with a substitution glyph, expanded to their code
  8449. // (e.g. "<8d>"), so there's no single correct way to handle them.
  8450. EXPECT_EQ("\"aaaaÄ\"\n"
  8451. "\"\xc2\x8d\";",
  8452. format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  8453. EXPECT_EQ("\"aaaaaaaÄ\"\n"
  8454. "\"\xc2\x8d\";",
  8455. format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  8456. EXPECT_EQ(
  8457. "\"Однажды, в \"\n"
  8458. "\"студёную \"\n"
  8459. "\"зимнюю \"\n"
  8460. "\"пору,\"",
  8461. format("\"Однажды, в студёную зимнюю пору,\"",
  8462. getLLVMStyleWithColumns(13)));
  8463. EXPECT_EQ("\"一 二 三 \"\n"
  8464. "\"四 五六 \"\n"
  8465. "\"七 八 九 \"\n"
  8466. "\"十\"",
  8467. format("\"一 二 三 四 五六 七 八 九 十\"",
  8468. getLLVMStyleWithColumns(11)));
  8469. EXPECT_EQ("\"一\t二 \"\n"
  8470. "\"\t三 \"\n"
  8471. "\"四 五\t六 \"\n"
  8472. "\"\t七 \"\n"
  8473. "\"八九十\tqq\"",
  8474. format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
  8475. getLLVMStyleWithColumns(11)));
  8476. }
  8477. TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
  8478. EXPECT_EQ("const char *sssss =\n"
  8479. " \"一二三四五六七八\\\n"
  8480. " 九 十\";",
  8481. format("const char *sssss = \"一二三四五六七八\\\n"
  8482. " 九 十\";",
  8483. getLLVMStyleWithColumns(30)));
  8484. }
  8485. TEST_F(FormatTest, SplitsUTF8LineComments) {
  8486. EXPECT_EQ("// aaaaÄ\xc2\x8d",
  8487. format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
  8488. EXPECT_EQ("// Я из лесу\n"
  8489. "// вышел; был\n"
  8490. "// сильный\n"
  8491. "// мороз.",
  8492. format("// Я из лесу вышел; был сильный мороз.",
  8493. getLLVMStyleWithColumns(13)));
  8494. EXPECT_EQ("// 一二三\n"
  8495. "// 四五六七\n"
  8496. "// 八 九\n"
  8497. "// 十",
  8498. format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9)));
  8499. }
  8500. TEST_F(FormatTest, SplitsUTF8BlockComments) {
  8501. EXPECT_EQ("/* Гляжу,\n"
  8502. " * поднимается\n"
  8503. " * медленно в\n"
  8504. " * гору\n"
  8505. " * Лошадка,\n"
  8506. " * везущая\n"
  8507. " * хворосту\n"
  8508. " * воз. */",
  8509. format("/* Гляжу, поднимается медленно в гору\n"
  8510. " * Лошадка, везущая хворосту воз. */",
  8511. getLLVMStyleWithColumns(13)));
  8512. EXPECT_EQ(
  8513. "/* 一二三\n"
  8514. " * 四五六七\n"
  8515. " * 八 九\n"
  8516. " * 十 */",
  8517. format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9)));
  8518. EXPECT_EQ("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
  8519. " * 𝕓𝕪𝕥𝕖\n"
  8520. " * 𝖀𝕿𝕱-𝟠 */",
  8521. format("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12)));
  8522. }
  8523. #endif // _MSC_VER
  8524. TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
  8525. FormatStyle Style = getLLVMStyle();
  8526. Style.ConstructorInitializerIndentWidth = 4;
  8527. verifyFormat(
  8528. "SomeClass::Constructor()\n"
  8529. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  8530. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  8531. Style);
  8532. Style.ConstructorInitializerIndentWidth = 2;
  8533. verifyFormat(
  8534. "SomeClass::Constructor()\n"
  8535. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  8536. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  8537. Style);
  8538. Style.ConstructorInitializerIndentWidth = 0;
  8539. verifyFormat(
  8540. "SomeClass::Constructor()\n"
  8541. ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  8542. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  8543. Style);
  8544. }
  8545. TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
  8546. FormatStyle Style = getLLVMStyle();
  8547. Style.BreakConstructorInitializersBeforeComma = true;
  8548. Style.ConstructorInitializerIndentWidth = 4;
  8549. verifyFormat("SomeClass::Constructor()\n"
  8550. " : a(a)\n"
  8551. " , b(b)\n"
  8552. " , c(c) {}",
  8553. Style);
  8554. verifyFormat("SomeClass::Constructor()\n"
  8555. " : a(a) {}",
  8556. Style);
  8557. Style.ColumnLimit = 0;
  8558. verifyFormat("SomeClass::Constructor()\n"
  8559. " : a(a) {}",
  8560. Style);
  8561. verifyFormat("SomeClass::Constructor()\n"
  8562. " : a(a)\n"
  8563. " , b(b)\n"
  8564. " , c(c) {}",
  8565. Style);
  8566. verifyFormat("SomeClass::Constructor()\n"
  8567. " : a(a) {\n"
  8568. " foo();\n"
  8569. " bar();\n"
  8570. "}",
  8571. Style);
  8572. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  8573. verifyFormat("SomeClass::Constructor()\n"
  8574. " : a(a)\n"
  8575. " , b(b)\n"
  8576. " , c(c) {\n}",
  8577. Style);
  8578. verifyFormat("SomeClass::Constructor()\n"
  8579. " : a(a) {\n}",
  8580. Style);
  8581. Style.ColumnLimit = 80;
  8582. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
  8583. Style.ConstructorInitializerIndentWidth = 2;
  8584. verifyFormat("SomeClass::Constructor()\n"
  8585. " : a(a)\n"
  8586. " , b(b)\n"
  8587. " , c(c) {}",
  8588. Style);
  8589. Style.ConstructorInitializerIndentWidth = 0;
  8590. verifyFormat("SomeClass::Constructor()\n"
  8591. ": a(a)\n"
  8592. ", b(b)\n"
  8593. ", c(c) {}",
  8594. Style);
  8595. Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  8596. Style.ConstructorInitializerIndentWidth = 4;
  8597. verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
  8598. verifyFormat(
  8599. "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
  8600. Style);
  8601. verifyFormat(
  8602. "SomeClass::Constructor()\n"
  8603. " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
  8604. Style);
  8605. Style.ConstructorInitializerIndentWidth = 4;
  8606. Style.ColumnLimit = 60;
  8607. verifyFormat("SomeClass::Constructor()\n"
  8608. " : aaaaaaaa(aaaaaaaa)\n"
  8609. " , aaaaaaaa(aaaaaaaa)\n"
  8610. " , aaaaaaaa(aaaaaaaa) {}",
  8611. Style);
  8612. }
  8613. TEST_F(FormatTest, Destructors) {
  8614. verifyFormat("void F(int &i) { i.~int(); }");
  8615. verifyFormat("void F(int &i) { i->~int(); }");
  8616. }
  8617. TEST_F(FormatTest, FormatsWithWebKitStyle) {
  8618. FormatStyle Style = getWebKitStyle();
  8619. // Don't indent in outer namespaces.
  8620. verifyFormat("namespace outer {\n"
  8621. "int i;\n"
  8622. "namespace inner {\n"
  8623. " int i;\n"
  8624. "} // namespace inner\n"
  8625. "} // namespace outer\n"
  8626. "namespace other_outer {\n"
  8627. "int i;\n"
  8628. "}",
  8629. Style);
  8630. // Don't indent case labels.
  8631. verifyFormat("switch (variable) {\n"
  8632. "case 1:\n"
  8633. "case 2:\n"
  8634. " doSomething();\n"
  8635. " break;\n"
  8636. "default:\n"
  8637. " ++variable;\n"
  8638. "}",
  8639. Style);
  8640. // Wrap before binary operators.
  8641. EXPECT_EQ(
  8642. "void f()\n"
  8643. "{\n"
  8644. " if (aaaaaaaaaaaaaaaa\n"
  8645. " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
  8646. " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  8647. " return;\n"
  8648. "}",
  8649. format(
  8650. "void f() {\n"
  8651. "if (aaaaaaaaaaaaaaaa\n"
  8652. "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
  8653. "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  8654. "return;\n"
  8655. "}",
  8656. Style));
  8657. // Allow functions on a single line.
  8658. verifyFormat("void f() { return; }", Style);
  8659. // Constructor initializers are formatted one per line with the "," on the
  8660. // new line.
  8661. verifyFormat("Constructor()\n"
  8662. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  8663. " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
  8664. " aaaaaaaaaaaaaa)\n"
  8665. " , aaaaaaaaaaaaaaaaaaaaaaa()\n"
  8666. "{\n"
  8667. "}",
  8668. Style);
  8669. verifyFormat("SomeClass::Constructor()\n"
  8670. " : a(a)\n"
  8671. "{\n"
  8672. "}",
  8673. Style);
  8674. EXPECT_EQ("SomeClass::Constructor()\n"
  8675. " : a(a)\n"
  8676. "{\n"
  8677. "}",
  8678. format("SomeClass::Constructor():a(a){}", Style));
  8679. verifyFormat("SomeClass::Constructor()\n"
  8680. " : a(a)\n"
  8681. " , b(b)\n"
  8682. " , c(c)\n"
  8683. "{\n"
  8684. "}", Style);
  8685. verifyFormat("SomeClass::Constructor()\n"
  8686. " : a(a)\n"
  8687. "{\n"
  8688. " foo();\n"
  8689. " bar();\n"
  8690. "}",
  8691. Style);
  8692. // Access specifiers should be aligned left.
  8693. verifyFormat("class C {\n"
  8694. "public:\n"
  8695. " int i;\n"
  8696. "};",
  8697. Style);
  8698. // Do not align comments.
  8699. verifyFormat("int a; // Do not\n"
  8700. "double b; // align comments.",
  8701. Style);
  8702. // Do not align operands.
  8703. EXPECT_EQ("ASSERT(aaaa\n"
  8704. " || bbbb);",
  8705. format("ASSERT ( aaaa\n||bbbb);", Style));
  8706. // Accept input's line breaks.
  8707. EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
  8708. " || bbbbbbbbbbbbbbb) {\n"
  8709. " i++;\n"
  8710. "}",
  8711. format("if (aaaaaaaaaaaaaaa\n"
  8712. "|| bbbbbbbbbbbbbbb) { i++; }",
  8713. Style));
  8714. EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
  8715. " i++;\n"
  8716. "}",
  8717. format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
  8718. // Don't automatically break all macro definitions (llvm.org/PR17842).
  8719. verifyFormat("#define aNumber 10", Style);
  8720. // However, generally keep the line breaks that the user authored.
  8721. EXPECT_EQ("#define aNumber \\\n"
  8722. " 10",
  8723. format("#define aNumber \\\n"
  8724. " 10",
  8725. Style));
  8726. // Keep empty and one-element array literals on a single line.
  8727. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
  8728. " copyItems:YES];",
  8729. format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
  8730. "copyItems:YES];",
  8731. Style));
  8732. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
  8733. " copyItems:YES];",
  8734. format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
  8735. " copyItems:YES];",
  8736. Style));
  8737. // FIXME: This does not seem right, there should be more indentation before
  8738. // the array literal's entries. Nested blocks have the same problem.
  8739. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
  8740. " @\"a\",\n"
  8741. " @\"a\"\n"
  8742. "]\n"
  8743. " copyItems:YES];",
  8744. format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
  8745. " @\"a\",\n"
  8746. " @\"a\"\n"
  8747. " ]\n"
  8748. " copyItems:YES];",
  8749. Style));
  8750. EXPECT_EQ(
  8751. "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
  8752. " copyItems:YES];",
  8753. format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
  8754. " copyItems:YES];",
  8755. Style));
  8756. verifyFormat("[self.a b:c c:d];", Style);
  8757. EXPECT_EQ("[self.a b:c\n"
  8758. " c:d];",
  8759. format("[self.a b:c\n"
  8760. "c:d];",
  8761. Style));
  8762. }
  8763. TEST_F(FormatTest, FormatsLambdas) {
  8764. verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
  8765. verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
  8766. verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
  8767. verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
  8768. verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
  8769. verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
  8770. verifyFormat("void f() {\n"
  8771. " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
  8772. "}\n");
  8773. verifyFormat("void f() {\n"
  8774. " other(x.begin(), //\n"
  8775. " x.end(), //\n"
  8776. " [&](int, int) { return 1; });\n"
  8777. "}\n");
  8778. verifyFormat("SomeFunction([]() { // A cool function...\n"
  8779. " return 43;\n"
  8780. "});");
  8781. EXPECT_EQ("SomeFunction([]() {\n"
  8782. "#define A a\n"
  8783. " return 43;\n"
  8784. "});",
  8785. format("SomeFunction([](){\n"
  8786. "#define A a\n"
  8787. "return 43;\n"
  8788. "});"));
  8789. verifyFormat("void f() {\n"
  8790. " SomeFunction([](decltype(x), A *a) {});\n"
  8791. "}");
  8792. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  8793. " [](const aaaaaaaaaa &a) { return a; });");
  8794. verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
  8795. " SomeOtherFunctioooooooooooooooooooooooooon();\n"
  8796. "});");
  8797. verifyFormat("Constructor()\n"
  8798. " : Field([] { // comment\n"
  8799. " int i;\n"
  8800. " }) {}");
  8801. // Lambdas with return types.
  8802. verifyFormat("int c = []() -> int { return 2; }();\n");
  8803. verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
  8804. verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
  8805. verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
  8806. verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
  8807. verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
  8808. verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
  8809. verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
  8810. " int j) -> int {\n"
  8811. " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
  8812. "};");
  8813. verifyFormat(
  8814. "aaaaaaaaaaaaaaaaaaaaaa(\n"
  8815. " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
  8816. " return aaaaaaaaaaaaaaaaa;\n"
  8817. " });",
  8818. getLLVMStyleWithColumns(70));
  8819. // Multiple lambdas in the same parentheses change indentation rules.
  8820. verifyFormat("SomeFunction(\n"
  8821. " []() {\n"
  8822. " int i = 42;\n"
  8823. " return i;\n"
  8824. " },\n"
  8825. " []() {\n"
  8826. " int j = 43;\n"
  8827. " return j;\n"
  8828. " });");
  8829. // More complex introducers.
  8830. verifyFormat("return [i, args...] {};");
  8831. // Not lambdas.
  8832. verifyFormat("constexpr char hello[]{\"hello\"};");
  8833. verifyFormat("double &operator[](int i) { return 0; }\n"
  8834. "int i;");
  8835. verifyFormat("std::unique_ptr<int[]> foo() {}");
  8836. verifyFormat("int i = a[a][a]->f();");
  8837. verifyFormat("int i = (*b)[a]->f();");
  8838. // Other corner cases.
  8839. verifyFormat("void f() {\n"
  8840. " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
  8841. " );\n"
  8842. "}");
  8843. // Lambdas created through weird macros.
  8844. verifyFormat("void f() {\n"
  8845. " MACRO((const AA &a) { return 1; });\n"
  8846. "}");
  8847. verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
  8848. " doo_dah();\n"
  8849. " doo_dah();\n"
  8850. " })) {\n"
  8851. "}");
  8852. verifyFormat("auto lambda = []() {\n"
  8853. " int a = 2\n"
  8854. "#if A\n"
  8855. " + 2\n"
  8856. "#endif\n"
  8857. " ;\n"
  8858. "};");
  8859. }
  8860. TEST_F(FormatTest, FormatsBlocks) {
  8861. FormatStyle ShortBlocks = getLLVMStyle();
  8862. ShortBlocks.AllowShortBlocksOnASingleLine = true;
  8863. verifyFormat("int (^Block)(int, int);", ShortBlocks);
  8864. verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
  8865. verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
  8866. verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
  8867. verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
  8868. verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
  8869. verifyFormat("foo(^{ bar(); });", ShortBlocks);
  8870. verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
  8871. verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
  8872. verifyFormat("[operation setCompletionBlock:^{\n"
  8873. " [self onOperationDone];\n"
  8874. "}];");
  8875. verifyFormat("int i = {[operation setCompletionBlock:^{\n"
  8876. " [self onOperationDone];\n"
  8877. "}]};");
  8878. verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
  8879. " f();\n"
  8880. "}];");
  8881. verifyFormat("int a = [operation block:^int(int *i) {\n"
  8882. " return 1;\n"
  8883. "}];");
  8884. verifyFormat("[myObject doSomethingWith:arg1\n"
  8885. " aaa:^int(int *a) {\n"
  8886. " return 1;\n"
  8887. " }\n"
  8888. " bbb:f(a * bbbbbbbb)];");
  8889. verifyFormat("[operation setCompletionBlock:^{\n"
  8890. " [self.delegate newDataAvailable];\n"
  8891. "}];",
  8892. getLLVMStyleWithColumns(60));
  8893. verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
  8894. " NSString *path = [self sessionFilePath];\n"
  8895. " if (path) {\n"
  8896. " // ...\n"
  8897. " }\n"
  8898. "});");
  8899. verifyFormat("[[SessionService sharedService]\n"
  8900. " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  8901. " if (window) {\n"
  8902. " [self windowDidLoad:window];\n"
  8903. " } else {\n"
  8904. " [self errorLoadingWindow];\n"
  8905. " }\n"
  8906. " }];");
  8907. verifyFormat("void (^largeBlock)(void) = ^{\n"
  8908. " // ...\n"
  8909. "};\n",
  8910. getLLVMStyleWithColumns(40));
  8911. verifyFormat("[[SessionService sharedService]\n"
  8912. " loadWindowWithCompletionBlock: //\n"
  8913. " ^(SessionWindow *window) {\n"
  8914. " if (window) {\n"
  8915. " [self windowDidLoad:window];\n"
  8916. " } else {\n"
  8917. " [self errorLoadingWindow];\n"
  8918. " }\n"
  8919. " }];",
  8920. getLLVMStyleWithColumns(60));
  8921. verifyFormat("[myObject doSomethingWith:arg1\n"
  8922. " firstBlock:^(Foo *a) {\n"
  8923. " // ...\n"
  8924. " int i;\n"
  8925. " }\n"
  8926. " secondBlock:^(Bar *b) {\n"
  8927. " // ...\n"
  8928. " int i;\n"
  8929. " }\n"
  8930. " thirdBlock:^Foo(Bar *b) {\n"
  8931. " // ...\n"
  8932. " int i;\n"
  8933. " }];");
  8934. verifyFormat("[myObject doSomethingWith:arg1\n"
  8935. " firstBlock:-1\n"
  8936. " secondBlock:^(Bar *b) {\n"
  8937. " // ...\n"
  8938. " int i;\n"
  8939. " }];");
  8940. verifyFormat("f(^{\n"
  8941. " @autoreleasepool {\n"
  8942. " if (a) {\n"
  8943. " g();\n"
  8944. " }\n"
  8945. " }\n"
  8946. "});");
  8947. verifyFormat("Block b = ^int *(A *a, B *b) {}");
  8948. FormatStyle FourIndent = getLLVMStyle();
  8949. FourIndent.ObjCBlockIndentWidth = 4;
  8950. verifyFormat("[operation setCompletionBlock:^{\n"
  8951. " [self onOperationDone];\n"
  8952. "}];",
  8953. FourIndent);
  8954. }
  8955. TEST_F(FormatTest, SupportsCRLF) {
  8956. EXPECT_EQ("int a;\r\n"
  8957. "int b;\r\n"
  8958. "int c;\r\n",
  8959. format("int a;\r\n"
  8960. " int b;\r\n"
  8961. " int c;\r\n",
  8962. getLLVMStyle()));
  8963. EXPECT_EQ("int a;\r\n"
  8964. "int b;\r\n"
  8965. "int c;\r\n",
  8966. format("int a;\r\n"
  8967. " int b;\n"
  8968. " int c;\r\n",
  8969. getLLVMStyle()));
  8970. EXPECT_EQ("int a;\n"
  8971. "int b;\n"
  8972. "int c;\n",
  8973. format("int a;\r\n"
  8974. " int b;\n"
  8975. " int c;\n",
  8976. getLLVMStyle()));
  8977. EXPECT_EQ("\"aaaaaaa \"\r\n"
  8978. "\"bbbbbbb\";\r\n",
  8979. format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
  8980. EXPECT_EQ("#define A \\\r\n"
  8981. " b; \\\r\n"
  8982. " c; \\\r\n"
  8983. " d;\r\n",
  8984. format("#define A \\\r\n"
  8985. " b; \\\r\n"
  8986. " c; d; \r\n",
  8987. getGoogleStyle()));
  8988. EXPECT_EQ("/*\r\n"
  8989. "multi line block comments\r\n"
  8990. "should not introduce\r\n"
  8991. "an extra carriage return\r\n"
  8992. "*/\r\n",
  8993. format("/*\r\n"
  8994. "multi line block comments\r\n"
  8995. "should not introduce\r\n"
  8996. "an extra carriage return\r\n"
  8997. "*/\r\n"));
  8998. }
  8999. TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
  9000. verifyFormat("MY_CLASS(C) {\n"
  9001. " int i;\n"
  9002. " int j;\n"
  9003. "};");
  9004. }
  9005. TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
  9006. FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
  9007. TwoIndent.ContinuationIndentWidth = 2;
  9008. EXPECT_EQ("int i =\n"
  9009. " longFunction(\n"
  9010. " arg);",
  9011. format("int i = longFunction(arg);", TwoIndent));
  9012. FormatStyle SixIndent = getLLVMStyleWithColumns(20);
  9013. SixIndent.ContinuationIndentWidth = 6;
  9014. EXPECT_EQ("int i =\n"
  9015. " longFunction(\n"
  9016. " arg);",
  9017. format("int i = longFunction(arg);", SixIndent));
  9018. }
  9019. TEST_F(FormatTest, SpacesInAngles) {
  9020. FormatStyle Spaces = getLLVMStyle();
  9021. Spaces.SpacesInAngles = true;
  9022. verifyFormat("static_cast< int >(arg);", Spaces);
  9023. verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
  9024. verifyFormat("f< int, float >();", Spaces);
  9025. verifyFormat("template <> g() {}", Spaces);
  9026. verifyFormat("template < std::vector< int > > f() {}", Spaces);
  9027. Spaces.Standard = FormatStyle::LS_Cpp03;
  9028. Spaces.SpacesInAngles = true;
  9029. verifyFormat("A< A< int > >();", Spaces);
  9030. Spaces.SpacesInAngles = false;
  9031. verifyFormat("A<A<int> >();", Spaces);
  9032. Spaces.Standard = FormatStyle::LS_Cpp11;
  9033. Spaces.SpacesInAngles = true;
  9034. verifyFormat("A< A< int > >();", Spaces);
  9035. Spaces.SpacesInAngles = false;
  9036. verifyFormat("A<A<int>>();", Spaces);
  9037. }
  9038. TEST_F(FormatTest, TripleAngleBrackets) {
  9039. verifyFormat("f<<<1, 1>>>();");
  9040. verifyFormat("f<<<1, 1, 1, s>>>();");
  9041. verifyFormat("f<<<a, b, c, d>>>();");
  9042. EXPECT_EQ("f<<<1, 1>>>();",
  9043. format("f <<< 1, 1 >>> ();"));
  9044. verifyFormat("f<param><<<1, 1>>>();");
  9045. verifyFormat("f<1><<<1, 1>>>();");
  9046. EXPECT_EQ("f<param><<<1, 1>>>();",
  9047. format("f< param > <<< 1, 1 >>> ();"));
  9048. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  9049. "aaaaaaaaaaa<<<\n 1, 1>>>();");
  9050. }
  9051. TEST_F(FormatTest, MergeLessLessAtEnd) {
  9052. verifyFormat("<<");
  9053. EXPECT_EQ("< < <", format("\\\n<<<"));
  9054. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  9055. "aaallvm::outs() <<");
  9056. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  9057. "aaaallvm::outs()\n <<");
  9058. }
  9059. TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
  9060. std::string code = "#if A\n"
  9061. "#if B\n"
  9062. "a.\n"
  9063. "#endif\n"
  9064. " a = 1;\n"
  9065. "#else\n"
  9066. "#endif\n"
  9067. "#if C\n"
  9068. "#else\n"
  9069. "#endif\n";
  9070. EXPECT_EQ(code, format(code));
  9071. }
  9072. TEST_F(FormatTest, HandleConflictMarkers) {
  9073. // Git/SVN conflict markers.
  9074. EXPECT_EQ("int a;\n"
  9075. "void f() {\n"
  9076. " callme(some(parameter1,\n"
  9077. "<<<<<<< text by the vcs\n"
  9078. " parameter2),\n"
  9079. "||||||| text by the vcs\n"
  9080. " parameter2),\n"
  9081. " parameter3,\n"
  9082. "======= text by the vcs\n"
  9083. " parameter2, parameter3),\n"
  9084. ">>>>>>> text by the vcs\n"
  9085. " otherparameter);\n",
  9086. format("int a;\n"
  9087. "void f() {\n"
  9088. " callme(some(parameter1,\n"
  9089. "<<<<<<< text by the vcs\n"
  9090. " parameter2),\n"
  9091. "||||||| text by the vcs\n"
  9092. " parameter2),\n"
  9093. " parameter3,\n"
  9094. "======= text by the vcs\n"
  9095. " parameter2,\n"
  9096. " parameter3),\n"
  9097. ">>>>>>> text by the vcs\n"
  9098. " otherparameter);\n"));
  9099. // Perforce markers.
  9100. EXPECT_EQ("void f() {\n"
  9101. " function(\n"
  9102. ">>>> text by the vcs\n"
  9103. " parameter,\n"
  9104. "==== text by the vcs\n"
  9105. " parameter,\n"
  9106. "==== text by the vcs\n"
  9107. " parameter,\n"
  9108. "<<<< text by the vcs\n"
  9109. " parameter);\n",
  9110. format("void f() {\n"
  9111. " function(\n"
  9112. ">>>> text by the vcs\n"
  9113. " parameter,\n"
  9114. "==== text by the vcs\n"
  9115. " parameter,\n"
  9116. "==== text by the vcs\n"
  9117. " parameter,\n"
  9118. "<<<< text by the vcs\n"
  9119. " parameter);\n"));
  9120. EXPECT_EQ("<<<<<<<\n"
  9121. "|||||||\n"
  9122. "=======\n"
  9123. ">>>>>>>",
  9124. format("<<<<<<<\n"
  9125. "|||||||\n"
  9126. "=======\n"
  9127. ">>>>>>>"));
  9128. EXPECT_EQ("<<<<<<<\n"
  9129. "|||||||\n"
  9130. "int i;\n"
  9131. "=======\n"
  9132. ">>>>>>>",
  9133. format("<<<<<<<\n"
  9134. "|||||||\n"
  9135. "int i;\n"
  9136. "=======\n"
  9137. ">>>>>>>"));
  9138. // FIXME: Handle parsing of macros around conflict markers correctly:
  9139. EXPECT_EQ("#define Macro \\\n"
  9140. "<<<<<<<\n"
  9141. "Something \\\n"
  9142. "|||||||\n"
  9143. "Else \\\n"
  9144. "=======\n"
  9145. "Other \\\n"
  9146. ">>>>>>>\n"
  9147. " End int i;\n",
  9148. format("#define Macro \\\n"
  9149. "<<<<<<<\n"
  9150. " Something \\\n"
  9151. "|||||||\n"
  9152. " Else \\\n"
  9153. "=======\n"
  9154. " Other \\\n"
  9155. ">>>>>>>\n"
  9156. " End\n"
  9157. "int i;\n"));
  9158. }
  9159. TEST_F(FormatTest, DisableRegions) {
  9160. EXPECT_EQ("int i;\n"
  9161. "// clang-format off\n"
  9162. " int j;\n"
  9163. "// clang-format on\n"
  9164. "int k;",
  9165. format(" int i;\n"
  9166. " // clang-format off\n"
  9167. " int j;\n"
  9168. " // clang-format on\n"
  9169. " int k;"));
  9170. EXPECT_EQ("int i;\n"
  9171. "/* clang-format off */\n"
  9172. " int j;\n"
  9173. "/* clang-format on */\n"
  9174. "int k;",
  9175. format(" int i;\n"
  9176. " /* clang-format off */\n"
  9177. " int j;\n"
  9178. " /* clang-format on */\n"
  9179. " int k;"));
  9180. }
  9181. TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
  9182. format("? ) =");
  9183. }
  9184. } // end namespace tooling
  9185. } // end namespace clang