FormatTest.cpp 388 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367
  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. namespace {
  17. FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
  18. class FormatTest : public ::testing::Test {
  19. protected:
  20. enum IncompleteCheck {
  21. IC_ExpectComplete,
  22. IC_ExpectIncomplete,
  23. IC_DoNotCheck
  24. };
  25. std::string format(llvm::StringRef Code,
  26. const FormatStyle &Style = getLLVMStyle(),
  27. IncompleteCheck CheckIncomplete = IC_ExpectComplete) {
  28. DEBUG(llvm::errs() << "---\n");
  29. DEBUG(llvm::errs() << Code << "\n\n");
  30. std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
  31. bool IncompleteFormat = false;
  32. tooling::Replacements Replaces =
  33. reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
  34. if (CheckIncomplete != IC_DoNotCheck) {
  35. bool ExpectedIncompleteFormat = CheckIncomplete == IC_ExpectIncomplete;
  36. EXPECT_EQ(ExpectedIncompleteFormat, IncompleteFormat) << Code << "\n\n";
  37. }
  38. ReplacementCount = Replaces.size();
  39. std::string Result = applyAllReplacements(Code, Replaces);
  40. EXPECT_NE("", Result);
  41. DEBUG(llvm::errs() << "\n" << Result << "\n\n");
  42. return Result;
  43. }
  44. FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
  45. FormatStyle Style = getLLVMStyle();
  46. Style.ColumnLimit = ColumnLimit;
  47. return Style;
  48. }
  49. FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
  50. FormatStyle Style = getGoogleStyle();
  51. Style.ColumnLimit = ColumnLimit;
  52. return Style;
  53. }
  54. void verifyFormat(llvm::StringRef Code,
  55. const FormatStyle &Style = getLLVMStyle()) {
  56. EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
  57. }
  58. void verifyIncompleteFormat(llvm::StringRef Code,
  59. const FormatStyle &Style = getLLVMStyle()) {
  60. EXPECT_EQ(Code.str(),
  61. format(test::messUp(Code), Style, IC_ExpectIncomplete));
  62. }
  63. void verifyGoogleFormat(llvm::StringRef Code) {
  64. verifyFormat(Code, getGoogleStyle());
  65. }
  66. void verifyIndependentOfContext(llvm::StringRef text) {
  67. verifyFormat(text);
  68. verifyFormat(llvm::Twine("void f() { " + text + " }").str());
  69. }
  70. /// \brief Verify that clang-format does not crash on the given input.
  71. void verifyNoCrash(llvm::StringRef Code,
  72. const FormatStyle &Style = getLLVMStyle()) {
  73. format(Code, Style, IC_DoNotCheck);
  74. }
  75. int ReplacementCount;
  76. };
  77. TEST_F(FormatTest, MessUp) {
  78. EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
  79. EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
  80. EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
  81. EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
  82. EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
  83. }
  84. //===----------------------------------------------------------------------===//
  85. // Basic function tests.
  86. //===----------------------------------------------------------------------===//
  87. TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
  88. EXPECT_EQ(";", format(";"));
  89. }
  90. TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
  91. EXPECT_EQ("int i;", format(" int i;"));
  92. EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;"));
  93. EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
  94. EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
  95. }
  96. TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
  97. EXPECT_EQ("int i;", format("int\ni;"));
  98. }
  99. TEST_F(FormatTest, FormatsNestedBlockStatements) {
  100. EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
  101. }
  102. TEST_F(FormatTest, FormatsNestedCall) {
  103. verifyFormat("Method(f1, f2(f3));");
  104. verifyFormat("Method(f1(f2, f3()));");
  105. verifyFormat("Method(f1(f2, (f3())));");
  106. }
  107. TEST_F(FormatTest, NestedNameSpecifiers) {
  108. verifyFormat("vector<::Type> v;");
  109. verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
  110. verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
  111. verifyFormat("bool a = 2 < ::SomeFunction();");
  112. }
  113. TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
  114. EXPECT_EQ("if (a) {\n"
  115. " f();\n"
  116. "}",
  117. format("if(a){f();}"));
  118. EXPECT_EQ(4, ReplacementCount);
  119. EXPECT_EQ("if (a) {\n"
  120. " f();\n"
  121. "}",
  122. format("if (a) {\n"
  123. " f();\n"
  124. "}"));
  125. EXPECT_EQ(0, ReplacementCount);
  126. EXPECT_EQ("/*\r\n"
  127. "\r\n"
  128. "*/\r\n",
  129. format("/*\r\n"
  130. "\r\n"
  131. "*/\r\n"));
  132. EXPECT_EQ(0, ReplacementCount);
  133. }
  134. TEST_F(FormatTest, RemovesEmptyLines) {
  135. EXPECT_EQ("class C {\n"
  136. " int i;\n"
  137. "};",
  138. format("class C {\n"
  139. " int i;\n"
  140. "\n"
  141. "};"));
  142. // Don't remove empty lines at the start of namespaces or extern "C" blocks.
  143. EXPECT_EQ("namespace N {\n"
  144. "\n"
  145. "int i;\n"
  146. "}",
  147. format("namespace N {\n"
  148. "\n"
  149. "int i;\n"
  150. "}",
  151. getGoogleStyle()));
  152. EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
  153. "\n"
  154. "int i;\n"
  155. "}",
  156. format("extern /**/ \"C\" /**/ {\n"
  157. "\n"
  158. "int i;\n"
  159. "}",
  160. getGoogleStyle()));
  161. // ...but do keep inlining and removing empty lines for non-block extern "C"
  162. // functions.
  163. verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
  164. EXPECT_EQ("extern \"C\" int f() {\n"
  165. " int i = 42;\n"
  166. " return i;\n"
  167. "}",
  168. format("extern \"C\" int f() {\n"
  169. "\n"
  170. " int i = 42;\n"
  171. " return i;\n"
  172. "}",
  173. getGoogleStyle()));
  174. // Remove empty lines at the beginning and end of blocks.
  175. EXPECT_EQ("void f() {\n"
  176. "\n"
  177. " if (a) {\n"
  178. "\n"
  179. " f();\n"
  180. " }\n"
  181. "}",
  182. format("void f() {\n"
  183. "\n"
  184. " if (a) {\n"
  185. "\n"
  186. " f();\n"
  187. "\n"
  188. " }\n"
  189. "\n"
  190. "}",
  191. getLLVMStyle()));
  192. EXPECT_EQ("void f() {\n"
  193. " if (a) {\n"
  194. " f();\n"
  195. " }\n"
  196. "}",
  197. format("void f() {\n"
  198. "\n"
  199. " if (a) {\n"
  200. "\n"
  201. " f();\n"
  202. "\n"
  203. " }\n"
  204. "\n"
  205. "}",
  206. getGoogleStyle()));
  207. // Don't remove empty lines in more complex control statements.
  208. EXPECT_EQ("void f() {\n"
  209. " if (a) {\n"
  210. " f();\n"
  211. "\n"
  212. " } else if (b) {\n"
  213. " f();\n"
  214. " }\n"
  215. "}",
  216. format("void f() {\n"
  217. " if (a) {\n"
  218. " f();\n"
  219. "\n"
  220. " } else if (b) {\n"
  221. " f();\n"
  222. "\n"
  223. " }\n"
  224. "\n"
  225. "}"));
  226. // FIXME: This is slightly inconsistent.
  227. EXPECT_EQ("namespace {\n"
  228. "int i;\n"
  229. "}",
  230. format("namespace {\n"
  231. "int i;\n"
  232. "\n"
  233. "}"));
  234. EXPECT_EQ("namespace {\n"
  235. "int i;\n"
  236. "\n"
  237. "} // namespace",
  238. format("namespace {\n"
  239. "int i;\n"
  240. "\n"
  241. "} // namespace"));
  242. }
  243. TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
  244. verifyFormat("x = (a) and (b);");
  245. verifyFormat("x = (a) or (b);");
  246. verifyFormat("x = (a) bitand (b);");
  247. verifyFormat("x = (a) bitor (b);");
  248. verifyFormat("x = (a) not_eq (b);");
  249. verifyFormat("x = (a) and_eq (b);");
  250. verifyFormat("x = (a) or_eq (b);");
  251. verifyFormat("x = (a) xor (b);");
  252. }
  253. //===----------------------------------------------------------------------===//
  254. // Tests for control statements.
  255. //===----------------------------------------------------------------------===//
  256. TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
  257. verifyFormat("if (true)\n f();\ng();");
  258. verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
  259. verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
  260. FormatStyle AllowsMergedIf = getLLVMStyle();
  261. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  262. verifyFormat("if (a)\n"
  263. " // comment\n"
  264. " f();",
  265. AllowsMergedIf);
  266. verifyFormat("if (a)\n"
  267. " ;",
  268. AllowsMergedIf);
  269. verifyFormat("if (a)\n"
  270. " if (b) return;",
  271. AllowsMergedIf);
  272. verifyFormat("if (a) // Can't merge this\n"
  273. " f();\n",
  274. AllowsMergedIf);
  275. verifyFormat("if (a) /* still don't merge */\n"
  276. " f();",
  277. AllowsMergedIf);
  278. verifyFormat("if (a) { // Never merge this\n"
  279. " f();\n"
  280. "}",
  281. AllowsMergedIf);
  282. verifyFormat("if (a) {/* Never merge this */\n"
  283. " f();\n"
  284. "}",
  285. AllowsMergedIf);
  286. AllowsMergedIf.ColumnLimit = 14;
  287. verifyFormat("if (a) return;", AllowsMergedIf);
  288. verifyFormat("if (aaaaaaaaa)\n"
  289. " return;",
  290. AllowsMergedIf);
  291. AllowsMergedIf.ColumnLimit = 13;
  292. verifyFormat("if (a)\n return;", AllowsMergedIf);
  293. }
  294. TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
  295. FormatStyle AllowsMergedLoops = getLLVMStyle();
  296. AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
  297. verifyFormat("while (true) continue;", AllowsMergedLoops);
  298. verifyFormat("for (;;) continue;", AllowsMergedLoops);
  299. verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
  300. verifyFormat("while (true)\n"
  301. " ;",
  302. AllowsMergedLoops);
  303. verifyFormat("for (;;)\n"
  304. " ;",
  305. AllowsMergedLoops);
  306. verifyFormat("for (;;)\n"
  307. " for (;;) continue;",
  308. AllowsMergedLoops);
  309. verifyFormat("for (;;) // Can't merge this\n"
  310. " continue;",
  311. AllowsMergedLoops);
  312. verifyFormat("for (;;) /* still don't merge */\n"
  313. " continue;",
  314. AllowsMergedLoops);
  315. }
  316. TEST_F(FormatTest, FormatShortBracedStatements) {
  317. FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
  318. AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
  319. AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
  320. AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
  321. verifyFormat("if (true) {}", AllowSimpleBracedStatements);
  322. verifyFormat("while (true) {}", AllowSimpleBracedStatements);
  323. verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
  324. verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
  325. verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
  326. verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
  327. verifyFormat("if (true) { //\n"
  328. " f();\n"
  329. "}",
  330. AllowSimpleBracedStatements);
  331. verifyFormat("if (true) {\n"
  332. " f();\n"
  333. " f();\n"
  334. "}",
  335. AllowSimpleBracedStatements);
  336. verifyFormat("if (true) {\n"
  337. " f();\n"
  338. "} else {\n"
  339. " f();\n"
  340. "}",
  341. AllowSimpleBracedStatements);
  342. verifyFormat("template <int> struct A2 {\n"
  343. " struct B {};\n"
  344. "};",
  345. AllowSimpleBracedStatements);
  346. AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
  347. verifyFormat("if (true) {\n"
  348. " f();\n"
  349. "}",
  350. AllowSimpleBracedStatements);
  351. verifyFormat("if (true) {\n"
  352. " f();\n"
  353. "} else {\n"
  354. " f();\n"
  355. "}",
  356. AllowSimpleBracedStatements);
  357. AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
  358. verifyFormat("while (true) {\n"
  359. " f();\n"
  360. "}",
  361. AllowSimpleBracedStatements);
  362. verifyFormat("for (;;) {\n"
  363. " f();\n"
  364. "}",
  365. AllowSimpleBracedStatements);
  366. }
  367. TEST_F(FormatTest, ParseIfElse) {
  368. verifyFormat("if (true)\n"
  369. " if (true)\n"
  370. " if (true)\n"
  371. " f();\n"
  372. " else\n"
  373. " g();\n"
  374. " else\n"
  375. " h();\n"
  376. "else\n"
  377. " i();");
  378. verifyFormat("if (true)\n"
  379. " if (true)\n"
  380. " if (true) {\n"
  381. " if (true)\n"
  382. " f();\n"
  383. " } else {\n"
  384. " g();\n"
  385. " }\n"
  386. " else\n"
  387. " h();\n"
  388. "else {\n"
  389. " i();\n"
  390. "}");
  391. verifyFormat("void f() {\n"
  392. " if (a) {\n"
  393. " } else {\n"
  394. " }\n"
  395. "}");
  396. }
  397. TEST_F(FormatTest, ElseIf) {
  398. verifyFormat("if (a) {\n} else if (b) {\n}");
  399. verifyFormat("if (a)\n"
  400. " f();\n"
  401. "else if (b)\n"
  402. " g();\n"
  403. "else\n"
  404. " h();");
  405. verifyFormat("if (a) {\n"
  406. " f();\n"
  407. "}\n"
  408. "// or else ..\n"
  409. "else {\n"
  410. " g()\n"
  411. "}");
  412. verifyFormat("if (a) {\n"
  413. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  414. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  415. "}");
  416. verifyFormat("if (a) {\n"
  417. "} else if (\n"
  418. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  419. "}",
  420. getLLVMStyleWithColumns(62));
  421. }
  422. TEST_F(FormatTest, FormatsForLoop) {
  423. verifyFormat(
  424. "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
  425. " ++VeryVeryLongLoopVariable)\n"
  426. " ;");
  427. verifyFormat("for (;;)\n"
  428. " f();");
  429. verifyFormat("for (;;) {\n}");
  430. verifyFormat("for (;;) {\n"
  431. " f();\n"
  432. "}");
  433. verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
  434. verifyFormat(
  435. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  436. " E = UnwrappedLines.end();\n"
  437. " I != E; ++I) {\n}");
  438. verifyFormat(
  439. "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
  440. " ++IIIII) {\n}");
  441. verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
  442. " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
  443. " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
  444. verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
  445. " I = FD->getDeclsInPrototypeScope().begin(),\n"
  446. " E = FD->getDeclsInPrototypeScope().end();\n"
  447. " I != E; ++I) {\n}");
  448. verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
  449. " I = Container.begin(),\n"
  450. " E = Container.end();\n"
  451. " I != E; ++I) {\n}",
  452. getLLVMStyleWithColumns(76));
  453. verifyFormat(
  454. "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  455. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
  456. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  457. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  458. " ++aaaaaaaaaaa) {\n}");
  459. verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  460. " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
  461. " ++i) {\n}");
  462. verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
  463. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  464. "}");
  465. verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
  466. " aaaaaaaaaa);\n"
  467. " iter; ++iter) {\n"
  468. "}");
  469. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  470. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  471. " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
  472. " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
  473. FormatStyle NoBinPacking = getLLVMStyle();
  474. NoBinPacking.BinPackParameters = false;
  475. verifyFormat("for (int aaaaaaaaaaa = 1;\n"
  476. " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
  477. " aaaaaaaaaaaaaaaa,\n"
  478. " aaaaaaaaaaaaaaaa,\n"
  479. " aaaaaaaaaaaaaaaa);\n"
  480. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  481. "}",
  482. NoBinPacking);
  483. verifyFormat(
  484. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  485. " E = UnwrappedLines.end();\n"
  486. " I != E;\n"
  487. " ++I) {\n}",
  488. NoBinPacking);
  489. }
  490. TEST_F(FormatTest, RangeBasedForLoops) {
  491. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  492. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  493. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
  494. " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
  495. verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
  496. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  497. verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
  498. " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
  499. }
  500. TEST_F(FormatTest, ForEachLoops) {
  501. verifyFormat("void f() {\n"
  502. " foreach (Item *item, itemlist) {}\n"
  503. " Q_FOREACH (Item *item, itemlist) {}\n"
  504. " BOOST_FOREACH (Item *item, itemlist) {}\n"
  505. " UNKNOWN_FORACH(Item * item, itemlist) {}\n"
  506. "}");
  507. // As function-like macros.
  508. verifyFormat("#define foreach(x, y)\n"
  509. "#define Q_FOREACH(x, y)\n"
  510. "#define BOOST_FOREACH(x, y)\n"
  511. "#define UNKNOWN_FOREACH(x, y)\n");
  512. // Not as function-like macros.
  513. verifyFormat("#define foreach (x, y)\n"
  514. "#define Q_FOREACH (x, y)\n"
  515. "#define BOOST_FOREACH (x, y)\n"
  516. "#define UNKNOWN_FOREACH (x, y)\n");
  517. }
  518. TEST_F(FormatTest, FormatsWhileLoop) {
  519. verifyFormat("while (true) {\n}");
  520. verifyFormat("while (true)\n"
  521. " f();");
  522. verifyFormat("while () {\n}");
  523. verifyFormat("while () {\n"
  524. " f();\n"
  525. "}");
  526. }
  527. TEST_F(FormatTest, FormatsDoWhile) {
  528. verifyFormat("do {\n"
  529. " do_something();\n"
  530. "} while (something());");
  531. verifyFormat("do\n"
  532. " do_something();\n"
  533. "while (something());");
  534. }
  535. TEST_F(FormatTest, FormatsSwitchStatement) {
  536. verifyFormat("switch (x) {\n"
  537. "case 1:\n"
  538. " f();\n"
  539. " break;\n"
  540. "case kFoo:\n"
  541. "case ns::kBar:\n"
  542. "case kBaz:\n"
  543. " break;\n"
  544. "default:\n"
  545. " g();\n"
  546. " break;\n"
  547. "}");
  548. verifyFormat("switch (x) {\n"
  549. "case 1: {\n"
  550. " f();\n"
  551. " break;\n"
  552. "}\n"
  553. "case 2: {\n"
  554. " break;\n"
  555. "}\n"
  556. "}");
  557. verifyFormat("switch (x) {\n"
  558. "case 1: {\n"
  559. " f();\n"
  560. " {\n"
  561. " g();\n"
  562. " h();\n"
  563. " }\n"
  564. " break;\n"
  565. "}\n"
  566. "}");
  567. verifyFormat("switch (x) {\n"
  568. "case 1: {\n"
  569. " f();\n"
  570. " if (foo) {\n"
  571. " g();\n"
  572. " h();\n"
  573. " }\n"
  574. " break;\n"
  575. "}\n"
  576. "}");
  577. verifyFormat("switch (x) {\n"
  578. "case 1: {\n"
  579. " f();\n"
  580. " g();\n"
  581. "} break;\n"
  582. "}");
  583. verifyFormat("switch (test)\n"
  584. " ;");
  585. verifyFormat("switch (x) {\n"
  586. "default: {\n"
  587. " // Do nothing.\n"
  588. "}\n"
  589. "}");
  590. verifyFormat("switch (x) {\n"
  591. "// comment\n"
  592. "// if 1, do f()\n"
  593. "case 1:\n"
  594. " f();\n"
  595. "}");
  596. verifyFormat("switch (x) {\n"
  597. "case 1:\n"
  598. " // Do amazing stuff\n"
  599. " {\n"
  600. " f();\n"
  601. " g();\n"
  602. " }\n"
  603. " break;\n"
  604. "}");
  605. verifyFormat("#define A \\\n"
  606. " switch (x) { \\\n"
  607. " case a: \\\n"
  608. " foo = b; \\\n"
  609. " }",
  610. getLLVMStyleWithColumns(20));
  611. verifyFormat("#define OPERATION_CASE(name) \\\n"
  612. " case OP_name: \\\n"
  613. " return operations::Operation##name\n",
  614. getLLVMStyleWithColumns(40));
  615. verifyFormat("switch (x) {\n"
  616. "case 1:;\n"
  617. "default:;\n"
  618. " int i;\n"
  619. "}");
  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("{\n"
  745. " some_code();\n"
  746. "test_label:\n"
  747. " some_other_code();\n"
  748. "}");
  749. verifyFormat("{\n"
  750. " some_code();\n"
  751. "test_label:;\n"
  752. " int i = 0;\n"
  753. "}");
  754. }
  755. //===----------------------------------------------------------------------===//
  756. // Tests for comments.
  757. //===----------------------------------------------------------------------===//
  758. TEST_F(FormatTest, UnderstandsSingleLineComments) {
  759. verifyFormat("//* */");
  760. verifyFormat("// line 1\n"
  761. "// line 2\n"
  762. "void f() {}\n");
  763. verifyFormat("void f() {\n"
  764. " // Doesn't do anything\n"
  765. "}");
  766. verifyFormat("SomeObject\n"
  767. " // Calling someFunction on SomeObject\n"
  768. " .someFunction();");
  769. verifyFormat("auto result = SomeObject\n"
  770. " // Calling someFunction on SomeObject\n"
  771. " .someFunction();");
  772. verifyFormat("void f(int i, // some comment (probably for i)\n"
  773. " int j, // some comment (probably for j)\n"
  774. " int k); // some comment (probably for k)");
  775. verifyFormat("void f(int i,\n"
  776. " // some comment (probably for j)\n"
  777. " int j,\n"
  778. " // some comment (probably for k)\n"
  779. " int k);");
  780. verifyFormat("int i // This is a fancy variable\n"
  781. " = 5; // with nicely aligned comment.");
  782. verifyFormat("// Leading comment.\n"
  783. "int a; // Trailing comment.");
  784. verifyFormat("int a; // Trailing comment\n"
  785. " // on 2\n"
  786. " // or 3 lines.\n"
  787. "int b;");
  788. verifyFormat("int a; // Trailing comment\n"
  789. "\n"
  790. "// Leading comment.\n"
  791. "int b;");
  792. verifyFormat("int a; // Comment.\n"
  793. " // More details.\n"
  794. "int bbbb; // Another comment.");
  795. verifyFormat(
  796. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  797. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n"
  798. "int cccccccccccccccccccccccccccccc; // comment\n"
  799. "int ddd; // looooooooooooooooooooooooong comment\n"
  800. "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  801. "int bbbbbbbbbbbbbbbbbbbbb; // comment\n"
  802. "int ccccccccccccccccccc; // comment");
  803. verifyFormat("#include \"a\" // comment\n"
  804. "#include \"a/b/c\" // comment");
  805. verifyFormat("#include <a> // comment\n"
  806. "#include <a/b/c> // comment");
  807. EXPECT_EQ("#include \"a\" // comment\n"
  808. "#include \"a/b/c\" // comment",
  809. format("#include \\\n"
  810. " \"a\" // comment\n"
  811. "#include \"a/b/c\" // comment"));
  812. verifyFormat("enum E {\n"
  813. " // comment\n"
  814. " VAL_A, // comment\n"
  815. " VAL_B\n"
  816. "};");
  817. verifyFormat(
  818. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  819. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
  820. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  821. " // Comment inside a statement.\n"
  822. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  823. verifyFormat("SomeFunction(a,\n"
  824. " // comment\n"
  825. " b + x);");
  826. verifyFormat("SomeFunction(a, a,\n"
  827. " // comment\n"
  828. " b + x);");
  829. verifyFormat(
  830. "bool aaaaaaaaaaaaa = // comment\n"
  831. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  832. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  833. verifyFormat("int aaaa; // aaaaa\n"
  834. "int aa; // aaaaaaa",
  835. getLLVMStyleWithColumns(20));
  836. EXPECT_EQ("void f() { // This does something ..\n"
  837. "}\n"
  838. "int a; // This is unrelated",
  839. format("void f() { // This does something ..\n"
  840. " }\n"
  841. "int a; // This is unrelated"));
  842. EXPECT_EQ("class C {\n"
  843. " void f() { // This does something ..\n"
  844. " } // awesome..\n"
  845. "\n"
  846. " int a; // This is unrelated\n"
  847. "};",
  848. format("class C{void f() { // This does something ..\n"
  849. " } // awesome..\n"
  850. " \n"
  851. "int a; // This is unrelated\n"
  852. "};"));
  853. EXPECT_EQ("int i; // single line trailing comment",
  854. format("int i;\\\n// single line trailing comment"));
  855. verifyGoogleFormat("int a; // Trailing comment.");
  856. verifyFormat("someFunction(anotherFunction( // Force break.\n"
  857. " parameter));");
  858. verifyGoogleFormat("#endif // HEADER_GUARD");
  859. verifyFormat("const char *test[] = {\n"
  860. " // A\n"
  861. " \"aaaa\",\n"
  862. " // B\n"
  863. " \"aaaaa\"};");
  864. verifyGoogleFormat(
  865. "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  866. " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment");
  867. EXPECT_EQ("D(a, {\n"
  868. " // test\n"
  869. " int a;\n"
  870. "});",
  871. format("D(a, {\n"
  872. "// test\n"
  873. "int a;\n"
  874. "});"));
  875. EXPECT_EQ("lineWith(); // comment\n"
  876. "// at start\n"
  877. "otherLine();",
  878. format("lineWith(); // comment\n"
  879. "// at start\n"
  880. "otherLine();"));
  881. EXPECT_EQ("lineWith(); // comment\n"
  882. " // at start\n"
  883. "otherLine();",
  884. format("lineWith(); // comment\n"
  885. " // at start\n"
  886. "otherLine();"));
  887. EXPECT_EQ("lineWith(); // comment\n"
  888. "// at start\n"
  889. "otherLine(); // comment",
  890. format("lineWith(); // comment\n"
  891. "// at start\n"
  892. "otherLine(); // comment"));
  893. EXPECT_EQ("lineWith();\n"
  894. "// at start\n"
  895. "otherLine(); // comment",
  896. format("lineWith();\n"
  897. " // at start\n"
  898. "otherLine(); // comment"));
  899. EXPECT_EQ("// first\n"
  900. "// at start\n"
  901. "otherLine(); // comment",
  902. format("// first\n"
  903. " // at start\n"
  904. "otherLine(); // comment"));
  905. EXPECT_EQ("f();\n"
  906. "// first\n"
  907. "// at start\n"
  908. "otherLine(); // comment",
  909. format("f();\n"
  910. "// first\n"
  911. " // at start\n"
  912. "otherLine(); // comment"));
  913. verifyFormat("f(); // comment\n"
  914. "// first\n"
  915. "// at start\n"
  916. "otherLine();");
  917. EXPECT_EQ("f(); // comment\n"
  918. "// first\n"
  919. "// at start\n"
  920. "otherLine();",
  921. format("f(); // comment\n"
  922. "// first\n"
  923. " // at start\n"
  924. "otherLine();"));
  925. EXPECT_EQ("f(); // comment\n"
  926. " // first\n"
  927. "// at start\n"
  928. "otherLine();",
  929. format("f(); // comment\n"
  930. " // first\n"
  931. "// at start\n"
  932. "otherLine();"));
  933. EXPECT_EQ("void f() {\n"
  934. " lineWith(); // comment\n"
  935. " // at start\n"
  936. "}",
  937. format("void f() {\n"
  938. " lineWith(); // comment\n"
  939. " // at start\n"
  940. "}"));
  941. verifyFormat("#define A \\\n"
  942. " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
  943. " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
  944. getLLVMStyleWithColumns(60));
  945. verifyFormat(
  946. "#define A \\\n"
  947. " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
  948. " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
  949. getLLVMStyleWithColumns(61));
  950. verifyFormat("if ( // This is some comment\n"
  951. " x + 3) {\n"
  952. "}");
  953. EXPECT_EQ("if ( // This is some comment\n"
  954. " // spanning two lines\n"
  955. " x + 3) {\n"
  956. "}",
  957. format("if( // This is some comment\n"
  958. " // spanning two lines\n"
  959. " x + 3) {\n"
  960. "}"));
  961. verifyNoCrash("/\\\n/");
  962. verifyNoCrash("/\\\n* */");
  963. // The 0-character somehow makes the lexer return a proper comment.
  964. verifyNoCrash(StringRef("/*\\\0\n/", 6));
  965. }
  966. TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
  967. EXPECT_EQ("SomeFunction(a,\n"
  968. " b, // comment\n"
  969. " c);",
  970. format("SomeFunction(a,\n"
  971. " b, // comment\n"
  972. " c);"));
  973. EXPECT_EQ("SomeFunction(a, b,\n"
  974. " // comment\n"
  975. " c);",
  976. format("SomeFunction(a,\n"
  977. " b,\n"
  978. " // comment\n"
  979. " c);"));
  980. EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
  981. " c);",
  982. format("SomeFunction(a, b, // comment (unclear relation)\n"
  983. " c);"));
  984. EXPECT_EQ("SomeFunction(a, // comment\n"
  985. " b,\n"
  986. " c); // comment",
  987. format("SomeFunction(a, // comment\n"
  988. " b,\n"
  989. " c); // comment"));
  990. }
  991. TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
  992. EXPECT_EQ("// comment", format("// comment "));
  993. EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
  994. format("int aaaaaaa, bbbbbbb; // comment ",
  995. getLLVMStyleWithColumns(33)));
  996. EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f "));
  997. EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f "));
  998. }
  999. TEST_F(FormatTest, UnderstandsBlockComments) {
  1000. verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
  1001. verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }");
  1002. EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
  1003. " bbbbbbbbbbbbbbbbbbbbbbbbb);",
  1004. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n"
  1005. "/* Trailing comment for aa... */\n"
  1006. " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  1007. EXPECT_EQ(
  1008. "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  1009. " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
  1010. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
  1011. "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  1012. EXPECT_EQ(
  1013. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1014. " aaaaaaaaaaaaaaaaaa,\n"
  1015. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  1016. "}",
  1017. format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1018. " aaaaaaaaaaaaaaaaaa ,\n"
  1019. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  1020. "}"));
  1021. FormatStyle NoBinPacking = getLLVMStyle();
  1022. NoBinPacking.BinPackParameters = false;
  1023. verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
  1024. " /* parameter 2 */ aaaaaa,\n"
  1025. " /* parameter 3 */ aaaaaa,\n"
  1026. " /* parameter 4 */ aaaaaa);",
  1027. NoBinPacking);
  1028. // Aligning block comments in macros.
  1029. verifyGoogleFormat("#define A \\\n"
  1030. " int i; /*a*/ \\\n"
  1031. " int jjj; /*b*/");
  1032. }
  1033. TEST_F(FormatTest, AlignsBlockComments) {
  1034. EXPECT_EQ("/*\n"
  1035. " * Really multi-line\n"
  1036. " * comment.\n"
  1037. " */\n"
  1038. "void f() {}",
  1039. format(" /*\n"
  1040. " * Really multi-line\n"
  1041. " * comment.\n"
  1042. " */\n"
  1043. " void f() {}"));
  1044. EXPECT_EQ("class C {\n"
  1045. " /*\n"
  1046. " * Another multi-line\n"
  1047. " * comment.\n"
  1048. " */\n"
  1049. " void f() {}\n"
  1050. "};",
  1051. format("class C {\n"
  1052. "/*\n"
  1053. " * Another multi-line\n"
  1054. " * comment.\n"
  1055. " */\n"
  1056. "void f() {}\n"
  1057. "};"));
  1058. EXPECT_EQ("/*\n"
  1059. " 1. This is a comment with non-trivial formatting.\n"
  1060. " 1.1. We have to indent/outdent all lines equally\n"
  1061. " 1.1.1. to keep the formatting.\n"
  1062. " */",
  1063. format(" /*\n"
  1064. " 1. This is a comment with non-trivial formatting.\n"
  1065. " 1.1. We have to indent/outdent all lines equally\n"
  1066. " 1.1.1. to keep the formatting.\n"
  1067. " */"));
  1068. EXPECT_EQ("/*\n"
  1069. "Don't try to outdent if there's not enough indentation.\n"
  1070. "*/",
  1071. format(" /*\n"
  1072. " Don't try to outdent if there's not enough indentation.\n"
  1073. " */"));
  1074. EXPECT_EQ("int i; /* Comment with empty...\n"
  1075. " *\n"
  1076. " * line. */",
  1077. format("int i; /* Comment with empty...\n"
  1078. " *\n"
  1079. " * line. */"));
  1080. EXPECT_EQ("int foobar = 0; /* comment */\n"
  1081. "int bar = 0; /* multiline\n"
  1082. " comment 1 */\n"
  1083. "int baz = 0; /* multiline\n"
  1084. " comment 2 */\n"
  1085. "int bzz = 0; /* multiline\n"
  1086. " comment 3 */",
  1087. format("int foobar = 0; /* comment */\n"
  1088. "int bar = 0; /* multiline\n"
  1089. " comment 1 */\n"
  1090. "int baz = 0; /* multiline\n"
  1091. " comment 2 */\n"
  1092. "int bzz = 0; /* multiline\n"
  1093. " comment 3 */"));
  1094. EXPECT_EQ("int foobar = 0; /* comment */\n"
  1095. "int bar = 0; /* multiline\n"
  1096. " comment */\n"
  1097. "int baz = 0; /* multiline\n"
  1098. "comment */",
  1099. format("int foobar = 0; /* comment */\n"
  1100. "int bar = 0; /* multiline\n"
  1101. "comment */\n"
  1102. "int baz = 0; /* multiline\n"
  1103. "comment */"));
  1104. }
  1105. TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
  1106. EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1107. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
  1108. format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1109. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
  1110. EXPECT_EQ(
  1111. "void ffffffffffff(\n"
  1112. " int aaaaaaaa, int bbbbbbbb,\n"
  1113. " int cccccccccccc) { /*\n"
  1114. " aaaaaaaaaa\n"
  1115. " aaaaaaaaaaaaa\n"
  1116. " bbbbbbbbbbbbbb\n"
  1117. " bbbbbbbbbb\n"
  1118. " */\n"
  1119. "}",
  1120. format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
  1121. "{ /*\n"
  1122. " aaaaaaaaaa aaaaaaaaaaaaa\n"
  1123. " bbbbbbbbbbbbbb bbbbbbbbbb\n"
  1124. " */\n"
  1125. "}",
  1126. getLLVMStyleWithColumns(40)));
  1127. }
  1128. TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
  1129. EXPECT_EQ("void ffffffffff(\n"
  1130. " int aaaaa /* test */);",
  1131. format("void ffffffffff(int aaaaa /* test */);",
  1132. getLLVMStyleWithColumns(35)));
  1133. }
  1134. TEST_F(FormatTest, SplitsLongCxxComments) {
  1135. EXPECT_EQ("// A comment that\n"
  1136. "// doesn't fit on\n"
  1137. "// one line",
  1138. format("// A comment that doesn't fit on one line",
  1139. getLLVMStyleWithColumns(20)));
  1140. EXPECT_EQ("/// A comment that\n"
  1141. "/// doesn't fit on\n"
  1142. "/// one line",
  1143. format("/// A comment that doesn't fit on one line",
  1144. getLLVMStyleWithColumns(20)));
  1145. EXPECT_EQ("//! A comment that\n"
  1146. "//! doesn't fit on\n"
  1147. "//! one line",
  1148. format("//! A comment that doesn't fit on one line",
  1149. getLLVMStyleWithColumns(20)));
  1150. EXPECT_EQ("// a b c d\n"
  1151. "// e f g\n"
  1152. "// h i j k",
  1153. format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10)));
  1154. EXPECT_EQ(
  1155. "// a b c d\n"
  1156. "// e f g\n"
  1157. "// h i j k",
  1158. format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10)));
  1159. EXPECT_EQ("if (true) // A comment that\n"
  1160. " // doesn't fit on\n"
  1161. " // one line",
  1162. format("if (true) // A comment that doesn't fit on one line ",
  1163. getLLVMStyleWithColumns(30)));
  1164. EXPECT_EQ("// Don't_touch_leading_whitespace",
  1165. format("// Don't_touch_leading_whitespace",
  1166. getLLVMStyleWithColumns(20)));
  1167. EXPECT_EQ("// Add leading\n"
  1168. "// whitespace",
  1169. format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
  1170. EXPECT_EQ("/// Add leading\n"
  1171. "/// whitespace",
  1172. format("///Add leading whitespace", getLLVMStyleWithColumns(20)));
  1173. EXPECT_EQ("//! Add leading\n"
  1174. "//! whitespace",
  1175. format("//!Add leading whitespace", getLLVMStyleWithColumns(20)));
  1176. EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
  1177. EXPECT_EQ("// Even if it makes the line exceed the column\n"
  1178. "// limit",
  1179. format("//Even if it makes the line exceed the column limit",
  1180. getLLVMStyleWithColumns(51)));
  1181. EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
  1182. EXPECT_EQ("// aa bb cc dd",
  1183. format("// aa bb cc dd ",
  1184. getLLVMStyleWithColumns(15)));
  1185. EXPECT_EQ("// A comment before\n"
  1186. "// a macro\n"
  1187. "// definition\n"
  1188. "#define a b",
  1189. format("// A comment before a macro definition\n"
  1190. "#define a b",
  1191. getLLVMStyleWithColumns(20)));
  1192. EXPECT_EQ("void ffffff(\n"
  1193. " int aaaaaaaaa, // wwww\n"
  1194. " int bbbbbbbbbb, // xxxxxxx\n"
  1195. " // yyyyyyyyyy\n"
  1196. " int c, int d, int e) {}",
  1197. format("void ffffff(\n"
  1198. " int aaaaaaaaa, // wwww\n"
  1199. " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
  1200. " int c, int d, int e) {}",
  1201. getLLVMStyleWithColumns(40)));
  1202. EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1203. format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1204. getLLVMStyleWithColumns(20)));
  1205. EXPECT_EQ(
  1206. "#define XXX // a b c d\n"
  1207. " // e f g h",
  1208. format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
  1209. EXPECT_EQ(
  1210. "#define XXX // q w e r\n"
  1211. " // t y u i",
  1212. format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
  1213. }
  1214. TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {
  1215. EXPECT_EQ("// A comment\n"
  1216. "// that doesn't\n"
  1217. "// fit on one\n"
  1218. "// line",
  1219. format("// A comment that doesn't fit on one line",
  1220. getLLVMStyleWithColumns(20)));
  1221. EXPECT_EQ("/// A comment\n"
  1222. "/// that doesn't\n"
  1223. "/// fit on one\n"
  1224. "/// line",
  1225. format("/// A comment that doesn't fit on one line",
  1226. getLLVMStyleWithColumns(20)));
  1227. }
  1228. TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
  1229. EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1230. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1231. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1232. format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1233. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1234. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
  1235. EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1236. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1237. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1238. format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1239. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1240. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1241. getLLVMStyleWithColumns(50)));
  1242. // FIXME: One day we might want to implement adjustment of leading whitespace
  1243. // of the consecutive lines in this kind of comment:
  1244. EXPECT_EQ("double\n"
  1245. " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1246. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1247. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1248. format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1249. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1250. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1251. getLLVMStyleWithColumns(49)));
  1252. }
  1253. TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) {
  1254. FormatStyle Pragmas = getLLVMStyleWithColumns(30);
  1255. Pragmas.CommentPragmas = "^ IWYU pragma:";
  1256. EXPECT_EQ(
  1257. "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
  1258. format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
  1259. EXPECT_EQ(
  1260. "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
  1261. format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
  1262. }
  1263. TEST_F(FormatTest, PriorityOfCommentBreaking) {
  1264. EXPECT_EQ("if (xxx ==\n"
  1265. " yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  1266. " zzz)\n"
  1267. " q();",
  1268. format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  1269. " zzz) q();",
  1270. getLLVMStyleWithColumns(40)));
  1271. EXPECT_EQ("if (xxxxxxxxxx ==\n"
  1272. " yyy && // aaaaaa bbbbbbbb cccc\n"
  1273. " zzz)\n"
  1274. " q();",
  1275. format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
  1276. " zzz) q();",
  1277. getLLVMStyleWithColumns(40)));
  1278. EXPECT_EQ("if (xxxxxxxxxx &&\n"
  1279. " yyy || // aaaaaa bbbbbbbb cccc\n"
  1280. " zzz)\n"
  1281. " q();",
  1282. format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
  1283. " zzz) q();",
  1284. getLLVMStyleWithColumns(40)));
  1285. EXPECT_EQ("fffffffff(\n"
  1286. " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  1287. " zzz);",
  1288. format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  1289. " zzz);",
  1290. getLLVMStyleWithColumns(40)));
  1291. }
  1292. TEST_F(FormatTest, MultiLineCommentsInDefines) {
  1293. EXPECT_EQ("#define A(x) /* \\\n"
  1294. " a comment \\\n"
  1295. " inside */ \\\n"
  1296. " f();",
  1297. format("#define A(x) /* \\\n"
  1298. " a comment \\\n"
  1299. " inside */ \\\n"
  1300. " f();",
  1301. getLLVMStyleWithColumns(17)));
  1302. EXPECT_EQ("#define A( \\\n"
  1303. " x) /* \\\n"
  1304. " a comment \\\n"
  1305. " inside */ \\\n"
  1306. " f();",
  1307. format("#define A( \\\n"
  1308. " x) /* \\\n"
  1309. " a comment \\\n"
  1310. " inside */ \\\n"
  1311. " f();",
  1312. getLLVMStyleWithColumns(17)));
  1313. }
  1314. TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
  1315. EXPECT_EQ("namespace {}\n// Test\n#define A",
  1316. format("namespace {}\n // Test\n#define A"));
  1317. EXPECT_EQ("namespace {}\n/* Test */\n#define A",
  1318. format("namespace {}\n /* Test */\n#define A"));
  1319. EXPECT_EQ("namespace {}\n/* Test */ #define A",
  1320. format("namespace {}\n /* Test */ #define A"));
  1321. }
  1322. TEST_F(FormatTest, SplitsLongLinesInComments) {
  1323. EXPECT_EQ("/* This is a long\n"
  1324. " * comment that\n"
  1325. " * doesn't\n"
  1326. " * fit on one line.\n"
  1327. " */",
  1328. format("/* "
  1329. "This is a long "
  1330. "comment that "
  1331. "doesn't "
  1332. "fit on one line. */",
  1333. getLLVMStyleWithColumns(20)));
  1334. EXPECT_EQ(
  1335. "/* a b c d\n"
  1336. " * e f g\n"
  1337. " * h i j k\n"
  1338. " */",
  1339. format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
  1340. EXPECT_EQ(
  1341. "/* a b c d\n"
  1342. " * e f g\n"
  1343. " * h i j k\n"
  1344. " */",
  1345. format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
  1346. EXPECT_EQ("/*\n"
  1347. "This is a long\n"
  1348. "comment that doesn't\n"
  1349. "fit on one line.\n"
  1350. "*/",
  1351. format("/*\n"
  1352. "This is a long "
  1353. "comment that doesn't "
  1354. "fit on one line. \n"
  1355. "*/",
  1356. getLLVMStyleWithColumns(20)));
  1357. EXPECT_EQ("/*\n"
  1358. " * This is a long\n"
  1359. " * comment that\n"
  1360. " * doesn't fit on\n"
  1361. " * one line.\n"
  1362. " */",
  1363. format("/* \n"
  1364. " * This is a long "
  1365. " comment that "
  1366. " doesn't fit on "
  1367. " one line. \n"
  1368. " */",
  1369. getLLVMStyleWithColumns(20)));
  1370. EXPECT_EQ("/*\n"
  1371. " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
  1372. " * so_it_should_be_broken\n"
  1373. " * wherever_a_space_occurs\n"
  1374. " */",
  1375. format("/*\n"
  1376. " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
  1377. " so_it_should_be_broken "
  1378. " wherever_a_space_occurs \n"
  1379. " */",
  1380. getLLVMStyleWithColumns(20)));
  1381. EXPECT_EQ("/*\n"
  1382. " * This_comment_can_not_be_broken_into_lines\n"
  1383. " */",
  1384. format("/*\n"
  1385. " * This_comment_can_not_be_broken_into_lines\n"
  1386. " */",
  1387. getLLVMStyleWithColumns(20)));
  1388. EXPECT_EQ("{\n"
  1389. " /*\n"
  1390. " This is another\n"
  1391. " long comment that\n"
  1392. " doesn't fit on one\n"
  1393. " line 1234567890\n"
  1394. " */\n"
  1395. "}",
  1396. format("{\n"
  1397. "/*\n"
  1398. "This is another "
  1399. " long comment that "
  1400. " doesn't fit on one"
  1401. " line 1234567890\n"
  1402. "*/\n"
  1403. "}",
  1404. getLLVMStyleWithColumns(20)));
  1405. EXPECT_EQ("{\n"
  1406. " /*\n"
  1407. " * This i s\n"
  1408. " * another comment\n"
  1409. " * t hat doesn' t\n"
  1410. " * fit on one l i\n"
  1411. " * n e\n"
  1412. " */\n"
  1413. "}",
  1414. format("{\n"
  1415. "/*\n"
  1416. " * This i s"
  1417. " another comment"
  1418. " t hat doesn' t"
  1419. " fit on one l i"
  1420. " n e\n"
  1421. " */\n"
  1422. "}",
  1423. getLLVMStyleWithColumns(20)));
  1424. EXPECT_EQ("/*\n"
  1425. " * This is a long\n"
  1426. " * comment that\n"
  1427. " * doesn't fit on\n"
  1428. " * one line\n"
  1429. " */",
  1430. format(" /*\n"
  1431. " * This is a long comment that doesn't fit on one line\n"
  1432. " */",
  1433. getLLVMStyleWithColumns(20)));
  1434. EXPECT_EQ("{\n"
  1435. " if (something) /* This is a\n"
  1436. " long\n"
  1437. " comment */\n"
  1438. " ;\n"
  1439. "}",
  1440. format("{\n"
  1441. " if (something) /* This is a long comment */\n"
  1442. " ;\n"
  1443. "}",
  1444. getLLVMStyleWithColumns(30)));
  1445. EXPECT_EQ("/* A comment before\n"
  1446. " * a macro\n"
  1447. " * definition */\n"
  1448. "#define a b",
  1449. format("/* A comment before a macro definition */\n"
  1450. "#define a b",
  1451. getLLVMStyleWithColumns(20)));
  1452. EXPECT_EQ("/* some comment\n"
  1453. " * a comment\n"
  1454. "* that we break\n"
  1455. " * another comment\n"
  1456. "* we have to break\n"
  1457. "* a left comment\n"
  1458. " */",
  1459. format(" /* some comment\n"
  1460. " * a comment that we break\n"
  1461. " * another comment we have to break\n"
  1462. "* a left comment\n"
  1463. " */",
  1464. getLLVMStyleWithColumns(20)));
  1465. EXPECT_EQ("/**\n"
  1466. " * multiline block\n"
  1467. " * comment\n"
  1468. " *\n"
  1469. " */",
  1470. format("/**\n"
  1471. " * multiline block comment\n"
  1472. " *\n"
  1473. " */",
  1474. getLLVMStyleWithColumns(20)));
  1475. EXPECT_EQ("/*\n"
  1476. "\n"
  1477. "\n"
  1478. " */\n",
  1479. format(" /* \n"
  1480. " \n"
  1481. " \n"
  1482. " */\n"));
  1483. EXPECT_EQ("/* a a */",
  1484. format("/* a a */", getLLVMStyleWithColumns(15)));
  1485. EXPECT_EQ("/* a a bc */",
  1486. format("/* a a bc */", getLLVMStyleWithColumns(15)));
  1487. EXPECT_EQ("/* aaa aaa\n"
  1488. " * aaaaa */",
  1489. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1490. EXPECT_EQ("/* aaa aaa\n"
  1491. " * aaaaa */",
  1492. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1493. }
  1494. TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
  1495. EXPECT_EQ("#define X \\\n"
  1496. " /* \\\n"
  1497. " Test \\\n"
  1498. " Macro comment \\\n"
  1499. " with a long \\\n"
  1500. " line \\\n"
  1501. " */ \\\n"
  1502. " A + B",
  1503. format("#define X \\\n"
  1504. " /*\n"
  1505. " Test\n"
  1506. " Macro comment with a long line\n"
  1507. " */ \\\n"
  1508. " A + B",
  1509. getLLVMStyleWithColumns(20)));
  1510. EXPECT_EQ("#define X \\\n"
  1511. " /* Macro comment \\\n"
  1512. " with a long \\\n"
  1513. " line */ \\\n"
  1514. " A + B",
  1515. format("#define X \\\n"
  1516. " /* Macro comment with a long\n"
  1517. " line */ \\\n"
  1518. " A + B",
  1519. getLLVMStyleWithColumns(20)));
  1520. EXPECT_EQ("#define X \\\n"
  1521. " /* Macro comment \\\n"
  1522. " * with a long \\\n"
  1523. " * line */ \\\n"
  1524. " A + B",
  1525. format("#define X \\\n"
  1526. " /* Macro comment with a long line */ \\\n"
  1527. " A + B",
  1528. getLLVMStyleWithColumns(20)));
  1529. }
  1530. TEST_F(FormatTest, CommentsInStaticInitializers) {
  1531. EXPECT_EQ(
  1532. "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
  1533. " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
  1534. " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
  1535. " aaaaaaaaaaaaaaaaaaaa, // comment\n"
  1536. " aaaaaaaaaaaaaaaaaaaa};",
  1537. format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
  1538. " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
  1539. " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
  1540. " aaaaaaaaaaaaaaaaaaaa , // comment\n"
  1541. " aaaaaaaaaaaaaaaaaaaa };"));
  1542. verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1543. " bbbbbbbbbbb, ccccccccccc};");
  1544. verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1545. " // comment for bb....\n"
  1546. " bbbbbbbbbbb, ccccccccccc};");
  1547. verifyGoogleFormat(
  1548. "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1549. " bbbbbbbbbbb, ccccccccccc};");
  1550. verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1551. " // comment for bb....\n"
  1552. " bbbbbbbbbbb, ccccccccccc};");
  1553. verifyFormat("S s = {{a, b, c}, // Group #1\n"
  1554. " {d, e, f}, // Group #2\n"
  1555. " {g, h, i}}; // Group #3");
  1556. verifyFormat("S s = {{// Group #1\n"
  1557. " a, b, c},\n"
  1558. " {// Group #2\n"
  1559. " d, e, f},\n"
  1560. " {// Group #3\n"
  1561. " g, h, i}};");
  1562. EXPECT_EQ("S s = {\n"
  1563. " // Some comment\n"
  1564. " a,\n"
  1565. "\n"
  1566. " // Comment after empty line\n"
  1567. " b}",
  1568. format("S s = {\n"
  1569. " // Some comment\n"
  1570. " a,\n"
  1571. " \n"
  1572. " // Comment after empty line\n"
  1573. " b\n"
  1574. "}"));
  1575. EXPECT_EQ("S s = {\n"
  1576. " /* Some comment */\n"
  1577. " a,\n"
  1578. "\n"
  1579. " /* Comment after empty line */\n"
  1580. " b}",
  1581. format("S s = {\n"
  1582. " /* Some comment */\n"
  1583. " a,\n"
  1584. " \n"
  1585. " /* Comment after empty line */\n"
  1586. " b\n"
  1587. "}"));
  1588. verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
  1589. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1590. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1591. " 0x00, 0x00, 0x00, 0x00}; // comment\n");
  1592. }
  1593. TEST_F(FormatTest, IgnoresIf0Contents) {
  1594. EXPECT_EQ("#if 0\n"
  1595. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1596. "#endif\n"
  1597. "void f() {}",
  1598. format("#if 0\n"
  1599. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1600. "#endif\n"
  1601. "void f( ) { }"));
  1602. EXPECT_EQ("#if false\n"
  1603. "void f( ) { }\n"
  1604. "#endif\n"
  1605. "void g() {}\n",
  1606. format("#if false\n"
  1607. "void f( ) { }\n"
  1608. "#endif\n"
  1609. "void g( ) { }\n"));
  1610. EXPECT_EQ("enum E {\n"
  1611. " One,\n"
  1612. " Two,\n"
  1613. "#if 0\n"
  1614. "Three,\n"
  1615. " Four,\n"
  1616. "#endif\n"
  1617. " Five\n"
  1618. "};",
  1619. format("enum E {\n"
  1620. " One,Two,\n"
  1621. "#if 0\n"
  1622. "Three,\n"
  1623. " Four,\n"
  1624. "#endif\n"
  1625. " Five};"));
  1626. EXPECT_EQ("enum F {\n"
  1627. " One,\n"
  1628. "#if 1\n"
  1629. " Two,\n"
  1630. "#if 0\n"
  1631. "Three,\n"
  1632. " Four,\n"
  1633. "#endif\n"
  1634. " Five\n"
  1635. "#endif\n"
  1636. "};",
  1637. format("enum F {\n"
  1638. "One,\n"
  1639. "#if 1\n"
  1640. "Two,\n"
  1641. "#if 0\n"
  1642. "Three,\n"
  1643. " Four,\n"
  1644. "#endif\n"
  1645. "Five\n"
  1646. "#endif\n"
  1647. "};"));
  1648. EXPECT_EQ("enum G {\n"
  1649. " One,\n"
  1650. "#if 0\n"
  1651. "Two,\n"
  1652. "#else\n"
  1653. " Three,\n"
  1654. "#endif\n"
  1655. " Four\n"
  1656. "};",
  1657. format("enum G {\n"
  1658. "One,\n"
  1659. "#if 0\n"
  1660. "Two,\n"
  1661. "#else\n"
  1662. "Three,\n"
  1663. "#endif\n"
  1664. "Four\n"
  1665. "};"));
  1666. EXPECT_EQ("enum H {\n"
  1667. " One,\n"
  1668. "#if 0\n"
  1669. "#ifdef Q\n"
  1670. "Two,\n"
  1671. "#else\n"
  1672. "Three,\n"
  1673. "#endif\n"
  1674. "#endif\n"
  1675. " Four\n"
  1676. "};",
  1677. format("enum H {\n"
  1678. "One,\n"
  1679. "#if 0\n"
  1680. "#ifdef Q\n"
  1681. "Two,\n"
  1682. "#else\n"
  1683. "Three,\n"
  1684. "#endif\n"
  1685. "#endif\n"
  1686. "Four\n"
  1687. "};"));
  1688. EXPECT_EQ("enum I {\n"
  1689. " One,\n"
  1690. "#if /* test */ 0 || 1\n"
  1691. "Two,\n"
  1692. "Three,\n"
  1693. "#endif\n"
  1694. " Four\n"
  1695. "};",
  1696. format("enum I {\n"
  1697. "One,\n"
  1698. "#if /* test */ 0 || 1\n"
  1699. "Two,\n"
  1700. "Three,\n"
  1701. "#endif\n"
  1702. "Four\n"
  1703. "};"));
  1704. EXPECT_EQ("enum J {\n"
  1705. " One,\n"
  1706. "#if 0\n"
  1707. "#if 0\n"
  1708. "Two,\n"
  1709. "#else\n"
  1710. "Three,\n"
  1711. "#endif\n"
  1712. "Four,\n"
  1713. "#endif\n"
  1714. " Five\n"
  1715. "};",
  1716. format("enum J {\n"
  1717. "One,\n"
  1718. "#if 0\n"
  1719. "#if 0\n"
  1720. "Two,\n"
  1721. "#else\n"
  1722. "Three,\n"
  1723. "#endif\n"
  1724. "Four,\n"
  1725. "#endif\n"
  1726. "Five\n"
  1727. "};"));
  1728. }
  1729. //===----------------------------------------------------------------------===//
  1730. // Tests for classes, namespaces, etc.
  1731. //===----------------------------------------------------------------------===//
  1732. TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
  1733. verifyFormat("class A {};");
  1734. }
  1735. TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
  1736. verifyFormat("class A {\n"
  1737. "public:\n"
  1738. "public: // comment\n"
  1739. "protected:\n"
  1740. "private:\n"
  1741. " void f() {}\n"
  1742. "};");
  1743. verifyGoogleFormat("class A {\n"
  1744. " public:\n"
  1745. " protected:\n"
  1746. " private:\n"
  1747. " void f() {}\n"
  1748. "};");
  1749. verifyFormat("class A {\n"
  1750. "public slots:\n"
  1751. " void f() {}\n"
  1752. "public Q_SLOTS:\n"
  1753. " void f() {}\n"
  1754. "signals:\n"
  1755. " void g();\n"
  1756. "};");
  1757. // Don't interpret 'signals' the wrong way.
  1758. verifyFormat("signals.set();");
  1759. verifyFormat("for (Signals signals : f()) {\n}");
  1760. verifyFormat("{\n"
  1761. " signals.set(); // This needs indentation.\n"
  1762. "}");
  1763. }
  1764. TEST_F(FormatTest, SeparatesLogicalBlocks) {
  1765. EXPECT_EQ("class A {\n"
  1766. "public:\n"
  1767. " void f();\n"
  1768. "\n"
  1769. "private:\n"
  1770. " void g() {}\n"
  1771. " // test\n"
  1772. "protected:\n"
  1773. " int h;\n"
  1774. "};",
  1775. format("class A {\n"
  1776. "public:\n"
  1777. "void f();\n"
  1778. "private:\n"
  1779. "void g() {}\n"
  1780. "// test\n"
  1781. "protected:\n"
  1782. "int h;\n"
  1783. "};"));
  1784. EXPECT_EQ("class A {\n"
  1785. "protected:\n"
  1786. "public:\n"
  1787. " void f();\n"
  1788. "};",
  1789. format("class A {\n"
  1790. "protected:\n"
  1791. "\n"
  1792. "public:\n"
  1793. "\n"
  1794. " void f();\n"
  1795. "};"));
  1796. // Even ensure proper spacing inside macros.
  1797. EXPECT_EQ("#define B \\\n"
  1798. " class A { \\\n"
  1799. " protected: \\\n"
  1800. " public: \\\n"
  1801. " void f(); \\\n"
  1802. " };",
  1803. format("#define B \\\n"
  1804. " class A { \\\n"
  1805. " protected: \\\n"
  1806. " \\\n"
  1807. " public: \\\n"
  1808. " \\\n"
  1809. " void f(); \\\n"
  1810. " };",
  1811. getGoogleStyle()));
  1812. // But don't remove empty lines after macros ending in access specifiers.
  1813. EXPECT_EQ("#define A private:\n"
  1814. "\n"
  1815. "int i;",
  1816. format("#define A private:\n"
  1817. "\n"
  1818. "int i;"));
  1819. }
  1820. TEST_F(FormatTest, FormatsClasses) {
  1821. verifyFormat("class A : public B {};");
  1822. verifyFormat("class A : public ::B {};");
  1823. verifyFormat(
  1824. "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1825. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1826. verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
  1827. " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1828. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1829. verifyFormat(
  1830. "class A : public B, public C, public D, public E, public F {};");
  1831. verifyFormat("class AAAAAAAAAAAA : public B,\n"
  1832. " public C,\n"
  1833. " public D,\n"
  1834. " public E,\n"
  1835. " public F,\n"
  1836. " public G {};");
  1837. verifyFormat("class\n"
  1838. " ReallyReallyLongClassName {\n"
  1839. " int i;\n"
  1840. "};",
  1841. getLLVMStyleWithColumns(32));
  1842. verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
  1843. " aaaaaaaaaaaaaaaa> {};");
  1844. verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
  1845. " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
  1846. " aaaaaaaaaaaaaaaaaaaaaa> {};");
  1847. verifyFormat("template <class R, class C>\n"
  1848. "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
  1849. " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
  1850. verifyFormat("class ::A::B {};");
  1851. }
  1852. TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
  1853. verifyFormat("class A {\n} a, b;");
  1854. verifyFormat("struct A {\n} a, b;");
  1855. verifyFormat("union A {\n} a;");
  1856. }
  1857. TEST_F(FormatTest, FormatsEnum) {
  1858. verifyFormat("enum {\n"
  1859. " Zero,\n"
  1860. " One = 1,\n"
  1861. " Two = One + 1,\n"
  1862. " Three = (One + Two),\n"
  1863. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1864. " Five = (One, Two, Three, Four, 5)\n"
  1865. "};");
  1866. verifyGoogleFormat("enum {\n"
  1867. " Zero,\n"
  1868. " One = 1,\n"
  1869. " Two = One + 1,\n"
  1870. " Three = (One + Two),\n"
  1871. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1872. " Five = (One, Two, Three, Four, 5)\n"
  1873. "};");
  1874. verifyFormat("enum Enum {};");
  1875. verifyFormat("enum {};");
  1876. verifyFormat("enum X E {} d;");
  1877. verifyFormat("enum __attribute__((...)) E {} d;");
  1878. verifyFormat("enum __declspec__((...)) E {} d;");
  1879. verifyFormat("enum {\n"
  1880. " Bar = Foo<int, int>::value\n"
  1881. "};",
  1882. getLLVMStyleWithColumns(30));
  1883. verifyFormat("enum ShortEnum { A, B, C };");
  1884. verifyGoogleFormat("enum ShortEnum { A, B, C };");
  1885. EXPECT_EQ("enum KeepEmptyLines {\n"
  1886. " ONE,\n"
  1887. "\n"
  1888. " TWO,\n"
  1889. "\n"
  1890. " THREE\n"
  1891. "}",
  1892. format("enum KeepEmptyLines {\n"
  1893. " ONE,\n"
  1894. "\n"
  1895. " TWO,\n"
  1896. "\n"
  1897. "\n"
  1898. " THREE\n"
  1899. "}"));
  1900. verifyFormat("enum E { // comment\n"
  1901. " ONE,\n"
  1902. " TWO\n"
  1903. "};\n"
  1904. "int i;");
  1905. // Not enums.
  1906. verifyFormat("enum X f() {\n"
  1907. " a();\n"
  1908. " return 42;\n"
  1909. "}");
  1910. verifyFormat("enum X Type::f() {\n"
  1911. " a();\n"
  1912. " return 42;\n"
  1913. "}");
  1914. verifyFormat("enum ::X f() {\n"
  1915. " a();\n"
  1916. " return 42;\n"
  1917. "}");
  1918. verifyFormat("enum ns::X f() {\n"
  1919. " a();\n"
  1920. " return 42;\n"
  1921. "}");
  1922. }
  1923. TEST_F(FormatTest, FormatsEnumsWithErrors) {
  1924. verifyFormat("enum Type {\n"
  1925. " One = 0; // These semicolons should be commas.\n"
  1926. " Two = 1;\n"
  1927. "};");
  1928. verifyFormat("namespace n {\n"
  1929. "enum Type {\n"
  1930. " One,\n"
  1931. " Two, // missing };\n"
  1932. " int i;\n"
  1933. "}\n"
  1934. "void g() {}");
  1935. }
  1936. TEST_F(FormatTest, FormatsEnumStruct) {
  1937. verifyFormat("enum struct {\n"
  1938. " Zero,\n"
  1939. " One = 1,\n"
  1940. " Two = One + 1,\n"
  1941. " Three = (One + Two),\n"
  1942. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1943. " Five = (One, Two, Three, Four, 5)\n"
  1944. "};");
  1945. verifyFormat("enum struct Enum {};");
  1946. verifyFormat("enum struct {};");
  1947. verifyFormat("enum struct X E {} d;");
  1948. verifyFormat("enum struct __attribute__((...)) E {} d;");
  1949. verifyFormat("enum struct __declspec__((...)) E {} d;");
  1950. verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
  1951. }
  1952. TEST_F(FormatTest, FormatsEnumClass) {
  1953. verifyFormat("enum class {\n"
  1954. " Zero,\n"
  1955. " One = 1,\n"
  1956. " Two = One + 1,\n"
  1957. " Three = (One + Two),\n"
  1958. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1959. " Five = (One, Two, Three, Four, 5)\n"
  1960. "};");
  1961. verifyFormat("enum class Enum {};");
  1962. verifyFormat("enum class {};");
  1963. verifyFormat("enum class X E {} d;");
  1964. verifyFormat("enum class __attribute__((...)) E {} d;");
  1965. verifyFormat("enum class __declspec__((...)) E {} d;");
  1966. verifyFormat("enum class X f() {\n a();\n return 42;\n}");
  1967. }
  1968. TEST_F(FormatTest, FormatsEnumTypes) {
  1969. verifyFormat("enum X : int {\n"
  1970. " A, // Force multiple lines.\n"
  1971. " B\n"
  1972. "};");
  1973. verifyFormat("enum X : int { A, B };");
  1974. verifyFormat("enum X : std::uint32_t { A, B };");
  1975. }
  1976. TEST_F(FormatTest, FormatsNSEnums) {
  1977. verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
  1978. verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
  1979. " // Information about someDecentlyLongValue.\n"
  1980. " someDecentlyLongValue,\n"
  1981. " // Information about anotherDecentlyLongValue.\n"
  1982. " anotherDecentlyLongValue,\n"
  1983. " // Information about aThirdDecentlyLongValue.\n"
  1984. " aThirdDecentlyLongValue\n"
  1985. "};");
  1986. verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
  1987. " a = 1,\n"
  1988. " b = 2,\n"
  1989. " c = 3,\n"
  1990. "};");
  1991. verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
  1992. " a = 1,\n"
  1993. " b = 2,\n"
  1994. " c = 3,\n"
  1995. "};");
  1996. verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
  1997. " a = 1,\n"
  1998. " b = 2,\n"
  1999. " c = 3,\n"
  2000. "};");
  2001. }
  2002. TEST_F(FormatTest, FormatsBitfields) {
  2003. verifyFormat("struct Bitfields {\n"
  2004. " unsigned sClass : 8;\n"
  2005. " unsigned ValueKind : 2;\n"
  2006. "};");
  2007. verifyFormat("struct A {\n"
  2008. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
  2009. " bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
  2010. "};");
  2011. verifyFormat("struct MyStruct {\n"
  2012. " uchar data;\n"
  2013. " uchar : 8;\n"
  2014. " uchar : 8;\n"
  2015. " uchar other;\n"
  2016. "};");
  2017. }
  2018. TEST_F(FormatTest, FormatsNamespaces) {
  2019. verifyFormat("namespace some_namespace {\n"
  2020. "class A {};\n"
  2021. "void f() { f(); }\n"
  2022. "}");
  2023. verifyFormat("namespace {\n"
  2024. "class A {};\n"
  2025. "void f() { f(); }\n"
  2026. "}");
  2027. verifyFormat("inline namespace X {\n"
  2028. "class A {};\n"
  2029. "void f() { f(); }\n"
  2030. "}");
  2031. verifyFormat("using namespace some_namespace;\n"
  2032. "class A {};\n"
  2033. "void f() { f(); }");
  2034. // This code is more common than we thought; if we
  2035. // layout this correctly the semicolon will go into
  2036. // its own line, which is undesirable.
  2037. verifyFormat("namespace {};");
  2038. verifyFormat("namespace {\n"
  2039. "class A {};\n"
  2040. "};");
  2041. verifyFormat("namespace {\n"
  2042. "int SomeVariable = 0; // comment\n"
  2043. "} // namespace");
  2044. EXPECT_EQ("#ifndef HEADER_GUARD\n"
  2045. "#define HEADER_GUARD\n"
  2046. "namespace my_namespace {\n"
  2047. "int i;\n"
  2048. "} // my_namespace\n"
  2049. "#endif // HEADER_GUARD",
  2050. format("#ifndef HEADER_GUARD\n"
  2051. " #define HEADER_GUARD\n"
  2052. " namespace my_namespace {\n"
  2053. "int i;\n"
  2054. "} // my_namespace\n"
  2055. "#endif // HEADER_GUARD"));
  2056. FormatStyle Style = getLLVMStyle();
  2057. Style.NamespaceIndentation = FormatStyle::NI_All;
  2058. EXPECT_EQ("namespace out {\n"
  2059. " int i;\n"
  2060. " namespace in {\n"
  2061. " int i;\n"
  2062. " } // namespace\n"
  2063. "} // namespace",
  2064. format("namespace out {\n"
  2065. "int i;\n"
  2066. "namespace in {\n"
  2067. "int i;\n"
  2068. "} // namespace\n"
  2069. "} // namespace",
  2070. Style));
  2071. Style.NamespaceIndentation = FormatStyle::NI_Inner;
  2072. EXPECT_EQ("namespace out {\n"
  2073. "int i;\n"
  2074. "namespace in {\n"
  2075. " int i;\n"
  2076. "} // namespace\n"
  2077. "} // namespace",
  2078. format("namespace out {\n"
  2079. "int i;\n"
  2080. "namespace in {\n"
  2081. "int i;\n"
  2082. "} // namespace\n"
  2083. "} // namespace",
  2084. Style));
  2085. }
  2086. TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
  2087. TEST_F(FormatTest, FormatsInlineASM) {
  2088. verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
  2089. verifyFormat("asm(\"nop\" ::: \"memory\");");
  2090. verifyFormat(
  2091. "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
  2092. " \"cpuid\\n\\t\"\n"
  2093. " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
  2094. " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
  2095. " : \"a\"(value));");
  2096. EXPECT_EQ(
  2097. "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
  2098. " __asm {\n"
  2099. " mov edx,[that] // vtable in edx\n"
  2100. " mov eax,methodIndex\n"
  2101. " call [edx][eax*4] // stdcall\n"
  2102. " }\n"
  2103. "}",
  2104. format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
  2105. " __asm {\n"
  2106. " mov edx,[that] // vtable in edx\n"
  2107. " mov eax,methodIndex\n"
  2108. " call [edx][eax*4] // stdcall\n"
  2109. " }\n"
  2110. "}"));
  2111. EXPECT_EQ("_asm {\n"
  2112. " xor eax, eax;\n"
  2113. " cpuid;\n"
  2114. "}",
  2115. format("_asm {\n"
  2116. " xor eax, eax;\n"
  2117. " cpuid;\n"
  2118. "}"));
  2119. verifyFormat("void function() {\n"
  2120. " // comment\n"
  2121. " asm(\"\");\n"
  2122. "}");
  2123. EXPECT_EQ("__asm {\n"
  2124. "}\n"
  2125. "int i;",
  2126. format("__asm {\n"
  2127. "}\n"
  2128. "int i;"));
  2129. }
  2130. TEST_F(FormatTest, FormatTryCatch) {
  2131. verifyFormat("try {\n"
  2132. " throw a * b;\n"
  2133. "} catch (int a) {\n"
  2134. " // Do nothing.\n"
  2135. "} catch (...) {\n"
  2136. " exit(42);\n"
  2137. "}");
  2138. // Function-level try statements.
  2139. verifyFormat("int f() try { return 4; } catch (...) {\n"
  2140. " return 5;\n"
  2141. "}");
  2142. verifyFormat("class A {\n"
  2143. " int a;\n"
  2144. " A() try : a(0) {\n"
  2145. " } catch (...) {\n"
  2146. " throw;\n"
  2147. " }\n"
  2148. "};\n");
  2149. // Incomplete try-catch blocks.
  2150. verifyIncompleteFormat("try {} catch (");
  2151. }
  2152. TEST_F(FormatTest, FormatSEHTryCatch) {
  2153. verifyFormat("__try {\n"
  2154. " int a = b * c;\n"
  2155. "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
  2156. " // Do nothing.\n"
  2157. "}");
  2158. verifyFormat("__try {\n"
  2159. " int a = b * c;\n"
  2160. "} __finally {\n"
  2161. " // Do nothing.\n"
  2162. "}");
  2163. verifyFormat("DEBUG({\n"
  2164. " __try {\n"
  2165. " } __finally {\n"
  2166. " }\n"
  2167. "});\n");
  2168. }
  2169. TEST_F(FormatTest, IncompleteTryCatchBlocks) {
  2170. verifyFormat("try {\n"
  2171. " f();\n"
  2172. "} catch {\n"
  2173. " g();\n"
  2174. "}");
  2175. verifyFormat("try {\n"
  2176. " f();\n"
  2177. "} catch (A a) MACRO(x) {\n"
  2178. " g();\n"
  2179. "} catch (B b) MACRO(x) {\n"
  2180. " g();\n"
  2181. "}");
  2182. }
  2183. TEST_F(FormatTest, FormatTryCatchBraceStyles) {
  2184. FormatStyle Style = getLLVMStyle();
  2185. Style.BreakBeforeBraces = FormatStyle::BS_Attach;
  2186. verifyFormat("try {\n"
  2187. " // something\n"
  2188. "} catch (...) {\n"
  2189. " // something\n"
  2190. "}",
  2191. Style);
  2192. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  2193. verifyFormat("try {\n"
  2194. " // something\n"
  2195. "}\n"
  2196. "catch (...) {\n"
  2197. " // something\n"
  2198. "}",
  2199. Style);
  2200. verifyFormat("__try {\n"
  2201. " // something\n"
  2202. "}\n"
  2203. "__finally {\n"
  2204. " // something\n"
  2205. "}",
  2206. Style);
  2207. verifyFormat("@try {\n"
  2208. " // something\n"
  2209. "}\n"
  2210. "@finally {\n"
  2211. " // something\n"
  2212. "}",
  2213. Style);
  2214. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  2215. verifyFormat("try\n"
  2216. "{\n"
  2217. " // something\n"
  2218. "}\n"
  2219. "catch (...)\n"
  2220. "{\n"
  2221. " // something\n"
  2222. "}",
  2223. Style);
  2224. Style.BreakBeforeBraces = FormatStyle::BS_GNU;
  2225. verifyFormat("try\n"
  2226. " {\n"
  2227. " // something\n"
  2228. " }\n"
  2229. "catch (...)\n"
  2230. " {\n"
  2231. " // something\n"
  2232. " }",
  2233. Style);
  2234. }
  2235. TEST_F(FormatTest, FormatObjCTryCatch) {
  2236. verifyFormat("@try {\n"
  2237. " f();\n"
  2238. "} @catch (NSException e) {\n"
  2239. " @throw;\n"
  2240. "} @finally {\n"
  2241. " exit(42);\n"
  2242. "}");
  2243. verifyFormat("DEBUG({\n"
  2244. " @try {\n"
  2245. " } @finally {\n"
  2246. " }\n"
  2247. "});\n");
  2248. }
  2249. TEST_F(FormatTest, FormatObjCAutoreleasepool) {
  2250. FormatStyle Style = getLLVMStyle();
  2251. verifyFormat("@autoreleasepool {\n"
  2252. " f();\n"
  2253. "}\n"
  2254. "@autoreleasepool {\n"
  2255. " f();\n"
  2256. "}\n",
  2257. Style);
  2258. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  2259. verifyFormat("@autoreleasepool\n"
  2260. "{\n"
  2261. " f();\n"
  2262. "}\n"
  2263. "@autoreleasepool\n"
  2264. "{\n"
  2265. " f();\n"
  2266. "}\n",
  2267. Style);
  2268. }
  2269. TEST_F(FormatTest, StaticInitializers) {
  2270. verifyFormat("static SomeClass SC = {1, 'a'};");
  2271. verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
  2272. " 100000000, "
  2273. "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
  2274. // Here, everything other than the "}" would fit on a line.
  2275. verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
  2276. " 10000000000000000000000000};");
  2277. EXPECT_EQ("S s = {a,\n"
  2278. "\n"
  2279. " b};",
  2280. format("S s = {\n"
  2281. " a,\n"
  2282. "\n"
  2283. " b\n"
  2284. "};"));
  2285. // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
  2286. // line. However, the formatting looks a bit off and this probably doesn't
  2287. // happen often in practice.
  2288. verifyFormat("static int Variable[1] = {\n"
  2289. " {1000000000000000000000000000000000000}};",
  2290. getLLVMStyleWithColumns(40));
  2291. }
  2292. TEST_F(FormatTest, DesignatedInitializers) {
  2293. verifyFormat("const struct A a = {.a = 1, .b = 2};");
  2294. verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
  2295. " .bbbbbbbbbb = 2,\n"
  2296. " .cccccccccc = 3,\n"
  2297. " .dddddddddd = 4,\n"
  2298. " .eeeeeeeeee = 5};");
  2299. verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
  2300. " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
  2301. " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
  2302. " .ccccccccccccccccccccccccccc = 3,\n"
  2303. " .ddddddddddddddddddddddddddd = 4,\n"
  2304. " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
  2305. verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
  2306. }
  2307. TEST_F(FormatTest, NestedStaticInitializers) {
  2308. verifyFormat("static A x = {{{}}};\n");
  2309. verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
  2310. " {init1, init2, init3, init4}}};",
  2311. getLLVMStyleWithColumns(50));
  2312. verifyFormat("somes Status::global_reps[3] = {\n"
  2313. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  2314. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  2315. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
  2316. getLLVMStyleWithColumns(60));
  2317. verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
  2318. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  2319. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  2320. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
  2321. verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
  2322. " {rect.fRight - rect.fLeft, rect.fBottom - "
  2323. "rect.fTop}};");
  2324. verifyFormat(
  2325. "SomeArrayOfSomeType a = {\n"
  2326. " {{1, 2, 3},\n"
  2327. " {1, 2, 3},\n"
  2328. " {111111111111111111111111111111, 222222222222222222222222222222,\n"
  2329. " 333333333333333333333333333333},\n"
  2330. " {1, 2, 3},\n"
  2331. " {1, 2, 3}}};");
  2332. verifyFormat(
  2333. "SomeArrayOfSomeType a = {\n"
  2334. " {{1, 2, 3}},\n"
  2335. " {{1, 2, 3}},\n"
  2336. " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
  2337. " 333333333333333333333333333333}},\n"
  2338. " {{1, 2, 3}},\n"
  2339. " {{1, 2, 3}}};");
  2340. verifyFormat("struct {\n"
  2341. " unsigned bit;\n"
  2342. " const char *const name;\n"
  2343. "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
  2344. " {kOsWin, \"Windows\"},\n"
  2345. " {kOsLinux, \"Linux\"},\n"
  2346. " {kOsCrOS, \"Chrome OS\"}};");
  2347. verifyFormat("struct {\n"
  2348. " unsigned bit;\n"
  2349. " const char *const name;\n"
  2350. "} kBitsToOs[] = {\n"
  2351. " {kOsMac, \"Mac\"},\n"
  2352. " {kOsWin, \"Windows\"},\n"
  2353. " {kOsLinux, \"Linux\"},\n"
  2354. " {kOsCrOS, \"Chrome OS\"},\n"
  2355. "};");
  2356. }
  2357. TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
  2358. verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2359. " \\\n"
  2360. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
  2361. }
  2362. TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
  2363. verifyFormat("virtual void write(ELFWriter *writerrr,\n"
  2364. " OwningPtr<FileOutputBuffer> &buffer) = 0;");
  2365. // Do break defaulted and deleted functions.
  2366. verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
  2367. " default;",
  2368. getLLVMStyleWithColumns(40));
  2369. verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
  2370. " delete;",
  2371. getLLVMStyleWithColumns(40));
  2372. }
  2373. TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
  2374. verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
  2375. getLLVMStyleWithColumns(40));
  2376. verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  2377. getLLVMStyleWithColumns(40));
  2378. EXPECT_EQ("#define Q \\\n"
  2379. " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n"
  2380. " \"aaaaaaaa.cpp\"",
  2381. format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  2382. getLLVMStyleWithColumns(40)));
  2383. }
  2384. TEST_F(FormatTest, UnderstandsLinePPDirective) {
  2385. EXPECT_EQ("# 123 \"A string literal\"",
  2386. format(" # 123 \"A string literal\""));
  2387. }
  2388. TEST_F(FormatTest, LayoutUnknownPPDirective) {
  2389. EXPECT_EQ("#;", format("#;"));
  2390. verifyFormat("#\n;\n;\n;");
  2391. }
  2392. TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
  2393. EXPECT_EQ("#line 42 \"test\"\n",
  2394. format("# \\\n line \\\n 42 \\\n \"test\"\n"));
  2395. EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n",
  2396. getLLVMStyleWithColumns(12)));
  2397. }
  2398. TEST_F(FormatTest, EndOfFileEndsPPDirective) {
  2399. EXPECT_EQ("#line 42 \"test\"",
  2400. format("# \\\n line \\\n 42 \\\n \"test\""));
  2401. EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B"));
  2402. }
  2403. TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
  2404. verifyFormat("#define A \\x20");
  2405. verifyFormat("#define A \\ x20");
  2406. EXPECT_EQ("#define A \\ x20", format("#define A \\ x20"));
  2407. verifyFormat("#define A ''");
  2408. verifyFormat("#define A ''qqq");
  2409. verifyFormat("#define A `qqq");
  2410. verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
  2411. EXPECT_EQ("const char *c = STRINGIFY(\n"
  2412. "\\na : b);",
  2413. format("const char * c = STRINGIFY(\n"
  2414. "\\na : b);"));
  2415. verifyFormat("a\r\\");
  2416. verifyFormat("a\v\\");
  2417. verifyFormat("a\f\\");
  2418. }
  2419. TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
  2420. verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
  2421. verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
  2422. verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
  2423. // FIXME: We never break before the macro name.
  2424. verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
  2425. verifyFormat("#define A A\n#define A A");
  2426. verifyFormat("#define A(X) A\n#define A A");
  2427. verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
  2428. verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
  2429. }
  2430. TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
  2431. EXPECT_EQ("// somecomment\n"
  2432. "#include \"a.h\"\n"
  2433. "#define A( \\\n"
  2434. " A, B)\n"
  2435. "#include \"b.h\"\n"
  2436. "// somecomment\n",
  2437. format(" // somecomment\n"
  2438. " #include \"a.h\"\n"
  2439. "#define A(A,\\\n"
  2440. " B)\n"
  2441. " #include \"b.h\"\n"
  2442. " // somecomment\n",
  2443. getLLVMStyleWithColumns(13)));
  2444. }
  2445. TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
  2446. TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
  2447. EXPECT_EQ("#define A \\\n"
  2448. " c; \\\n"
  2449. " e;\n"
  2450. "f;",
  2451. format("#define A c; e;\n"
  2452. "f;",
  2453. getLLVMStyleWithColumns(14)));
  2454. }
  2455. TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
  2456. TEST_F(FormatTest, MacroDefinitionInsideStatement) {
  2457. EXPECT_EQ("int x,\n"
  2458. "#define A\n"
  2459. " y;",
  2460. format("int x,\n#define A\ny;"));
  2461. }
  2462. TEST_F(FormatTest, HashInMacroDefinition) {
  2463. EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
  2464. verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
  2465. verifyFormat("#define A \\\n"
  2466. " { \\\n"
  2467. " f(#c); \\\n"
  2468. " }",
  2469. getLLVMStyleWithColumns(11));
  2470. verifyFormat("#define A(X) \\\n"
  2471. " void function##X()",
  2472. getLLVMStyleWithColumns(22));
  2473. verifyFormat("#define A(a, b, c) \\\n"
  2474. " void a##b##c()",
  2475. getLLVMStyleWithColumns(22));
  2476. verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
  2477. }
  2478. TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
  2479. EXPECT_EQ("#define A (x)", format("#define A (x)"));
  2480. EXPECT_EQ("#define A(x)", format("#define A(x)"));
  2481. }
  2482. TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
  2483. EXPECT_EQ("#define A b;", format("#define A \\\n"
  2484. " \\\n"
  2485. " b;",
  2486. getLLVMStyleWithColumns(25)));
  2487. EXPECT_EQ("#define A \\\n"
  2488. " \\\n"
  2489. " a; \\\n"
  2490. " b;",
  2491. format("#define A \\\n"
  2492. " \\\n"
  2493. " a; \\\n"
  2494. " b;",
  2495. getLLVMStyleWithColumns(11)));
  2496. EXPECT_EQ("#define A \\\n"
  2497. " a; \\\n"
  2498. " \\\n"
  2499. " b;",
  2500. format("#define A \\\n"
  2501. " a; \\\n"
  2502. " \\\n"
  2503. " b;",
  2504. getLLVMStyleWithColumns(11)));
  2505. }
  2506. TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
  2507. verifyIncompleteFormat("#define A :");
  2508. verifyFormat("#define SOMECASES \\\n"
  2509. " case 1: \\\n"
  2510. " case 2\n",
  2511. getLLVMStyleWithColumns(20));
  2512. verifyFormat("#define A template <typename T>");
  2513. verifyIncompleteFormat("#define STR(x) #x\n"
  2514. "f(STR(this_is_a_string_literal{));");
  2515. verifyFormat("#pragma omp threadprivate( \\\n"
  2516. " y)), // expected-warning",
  2517. getLLVMStyleWithColumns(28));
  2518. verifyFormat("#d, = };");
  2519. verifyFormat("#if \"a");
  2520. verifyIncompleteFormat("({\n"
  2521. "#define b \\\n"
  2522. " } \\\n"
  2523. " a\n"
  2524. "a",
  2525. getLLVMStyleWithColumns(15));
  2526. verifyFormat("#define A \\\n"
  2527. " { \\\n"
  2528. " {\n"
  2529. "#define B \\\n"
  2530. " } \\\n"
  2531. " }",
  2532. getLLVMStyleWithColumns(15));
  2533. verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
  2534. verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
  2535. verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
  2536. verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}");
  2537. }
  2538. TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
  2539. verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
  2540. EXPECT_EQ("class A : public QObject {\n"
  2541. " Q_OBJECT\n"
  2542. "\n"
  2543. " A() {}\n"
  2544. "};",
  2545. format("class A : public QObject {\n"
  2546. " Q_OBJECT\n"
  2547. "\n"
  2548. " A() {\n}\n"
  2549. "} ;"));
  2550. EXPECT_EQ("MACRO\n"
  2551. "/*static*/ int i;",
  2552. format("MACRO\n"
  2553. " /*static*/ int i;"));
  2554. EXPECT_EQ("SOME_MACRO\n"
  2555. "namespace {\n"
  2556. "void f();\n"
  2557. "}",
  2558. format("SOME_MACRO\n"
  2559. " namespace {\n"
  2560. "void f( );\n"
  2561. "}"));
  2562. // Only if the identifier contains at least 5 characters.
  2563. EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
  2564. EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
  2565. // Only if everything is upper case.
  2566. EXPECT_EQ("class A : public QObject {\n"
  2567. " Q_Object A() {}\n"
  2568. "};",
  2569. format("class A : public QObject {\n"
  2570. " Q_Object\n"
  2571. " A() {\n}\n"
  2572. "} ;"));
  2573. // Only if the next line can actually start an unwrapped line.
  2574. EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
  2575. format("SOME_WEIRD_LOG_MACRO\n"
  2576. "<< SomeThing;"));
  2577. verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
  2578. "(n, buffers))\n",
  2579. getChromiumStyle(FormatStyle::LK_Cpp));
  2580. }
  2581. TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
  2582. EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  2583. "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  2584. "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  2585. "class X {};\n"
  2586. "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  2587. "int *createScopDetectionPass() { return 0; }",
  2588. format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  2589. " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  2590. " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  2591. " class X {};\n"
  2592. " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  2593. " int *createScopDetectionPass() { return 0; }"));
  2594. // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
  2595. // braces, so that inner block is indented one level more.
  2596. EXPECT_EQ("int q() {\n"
  2597. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  2598. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  2599. " IPC_END_MESSAGE_MAP()\n"
  2600. "}",
  2601. format("int q() {\n"
  2602. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  2603. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  2604. " IPC_END_MESSAGE_MAP()\n"
  2605. "}"));
  2606. // Same inside macros.
  2607. EXPECT_EQ("#define LIST(L) \\\n"
  2608. " L(A) \\\n"
  2609. " L(B) \\\n"
  2610. " L(C)",
  2611. format("#define LIST(L) \\\n"
  2612. " L(A) \\\n"
  2613. " L(B) \\\n"
  2614. " L(C)",
  2615. getGoogleStyle()));
  2616. // These must not be recognized as macros.
  2617. EXPECT_EQ("int q() {\n"
  2618. " f(x);\n"
  2619. " f(x) {}\n"
  2620. " f(x)->g();\n"
  2621. " f(x)->*g();\n"
  2622. " f(x).g();\n"
  2623. " f(x) = x;\n"
  2624. " f(x) += x;\n"
  2625. " f(x) -= x;\n"
  2626. " f(x) *= x;\n"
  2627. " f(x) /= x;\n"
  2628. " f(x) %= x;\n"
  2629. " f(x) &= x;\n"
  2630. " f(x) |= x;\n"
  2631. " f(x) ^= x;\n"
  2632. " f(x) >>= x;\n"
  2633. " f(x) <<= x;\n"
  2634. " f(x)[y].z();\n"
  2635. " LOG(INFO) << x;\n"
  2636. " ifstream(x) >> x;\n"
  2637. "}\n",
  2638. format("int q() {\n"
  2639. " f(x)\n;\n"
  2640. " f(x)\n {}\n"
  2641. " f(x)\n->g();\n"
  2642. " f(x)\n->*g();\n"
  2643. " f(x)\n.g();\n"
  2644. " f(x)\n = x;\n"
  2645. " f(x)\n += x;\n"
  2646. " f(x)\n -= x;\n"
  2647. " f(x)\n *= x;\n"
  2648. " f(x)\n /= x;\n"
  2649. " f(x)\n %= x;\n"
  2650. " f(x)\n &= x;\n"
  2651. " f(x)\n |= x;\n"
  2652. " f(x)\n ^= x;\n"
  2653. " f(x)\n >>= x;\n"
  2654. " f(x)\n <<= x;\n"
  2655. " f(x)\n[y].z();\n"
  2656. " LOG(INFO)\n << x;\n"
  2657. " ifstream(x)\n >> x;\n"
  2658. "}\n"));
  2659. EXPECT_EQ("int q() {\n"
  2660. " F(x)\n"
  2661. " if (1) {\n"
  2662. " }\n"
  2663. " F(x)\n"
  2664. " while (1) {\n"
  2665. " }\n"
  2666. " F(x)\n"
  2667. " G(x);\n"
  2668. " F(x)\n"
  2669. " try {\n"
  2670. " Q();\n"
  2671. " } catch (...) {\n"
  2672. " }\n"
  2673. "}\n",
  2674. format("int q() {\n"
  2675. "F(x)\n"
  2676. "if (1) {}\n"
  2677. "F(x)\n"
  2678. "while (1) {}\n"
  2679. "F(x)\n"
  2680. "G(x);\n"
  2681. "F(x)\n"
  2682. "try { Q(); } catch (...) {}\n"
  2683. "}\n"));
  2684. EXPECT_EQ("class A {\n"
  2685. " A() : t(0) {}\n"
  2686. " A(int i) noexcept() : {}\n"
  2687. " A(X x)\n" // FIXME: function-level try blocks are broken.
  2688. " try : t(0) {\n"
  2689. " } catch (...) {\n"
  2690. " }\n"
  2691. "};",
  2692. format("class A {\n"
  2693. " A()\n : t(0) {}\n"
  2694. " A(int i)\n noexcept() : {}\n"
  2695. " A(X x)\n"
  2696. " try : t(0) {} catch (...) {}\n"
  2697. "};"));
  2698. EXPECT_EQ("class SomeClass {\n"
  2699. "public:\n"
  2700. " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2701. "};",
  2702. format("class SomeClass {\n"
  2703. "public:\n"
  2704. " SomeClass()\n"
  2705. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2706. "};"));
  2707. EXPECT_EQ("class SomeClass {\n"
  2708. "public:\n"
  2709. " SomeClass()\n"
  2710. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2711. "};",
  2712. format("class SomeClass {\n"
  2713. "public:\n"
  2714. " SomeClass()\n"
  2715. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2716. "};",
  2717. getLLVMStyleWithColumns(40)));
  2718. }
  2719. TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
  2720. verifyFormat("#define A \\\n"
  2721. " f({ \\\n"
  2722. " g(); \\\n"
  2723. " });",
  2724. getLLVMStyleWithColumns(11));
  2725. }
  2726. TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
  2727. EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
  2728. }
  2729. TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
  2730. verifyFormat("{\n { a #c; }\n}");
  2731. }
  2732. TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
  2733. EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
  2734. format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
  2735. EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
  2736. format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
  2737. }
  2738. TEST_F(FormatTest, EscapedNewlines) {
  2739. EXPECT_EQ(
  2740. "#define A \\\n int i; \\\n int j;",
  2741. format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
  2742. EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
  2743. EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
  2744. EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/"));
  2745. EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
  2746. }
  2747. TEST_F(FormatTest, DontCrashOnBlockComments) {
  2748. EXPECT_EQ(
  2749. "int xxxxxxxxx; /* "
  2750. "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
  2751. "zzzzzz\n"
  2752. "0*/",
  2753. format("int xxxxxxxxx; /* "
  2754. "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
  2755. "0*/"));
  2756. }
  2757. TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
  2758. verifyFormat("#define A \\\n"
  2759. " int v( \\\n"
  2760. " a); \\\n"
  2761. " int i;",
  2762. getLLVMStyleWithColumns(11));
  2763. }
  2764. TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
  2765. EXPECT_EQ(
  2766. "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2767. " \\\n"
  2768. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2769. "\n"
  2770. "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2771. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
  2772. format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2773. "\\\n"
  2774. "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2775. " \n"
  2776. " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2777. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
  2778. }
  2779. TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
  2780. EXPECT_EQ("int\n"
  2781. "#define A\n"
  2782. " a;",
  2783. format("int\n#define A\na;"));
  2784. verifyFormat("functionCallTo(\n"
  2785. " someOtherFunction(\n"
  2786. " withSomeParameters, whichInSequence,\n"
  2787. " areLongerThanALine(andAnotherCall,\n"
  2788. "#define A B\n"
  2789. " withMoreParamters,\n"
  2790. " whichStronglyInfluenceTheLayout),\n"
  2791. " andMoreParameters),\n"
  2792. " trailing);",
  2793. getLLVMStyleWithColumns(69));
  2794. verifyFormat("Foo::Foo()\n"
  2795. "#ifdef BAR\n"
  2796. " : baz(0)\n"
  2797. "#endif\n"
  2798. "{\n"
  2799. "}");
  2800. verifyFormat("void f() {\n"
  2801. " if (true)\n"
  2802. "#ifdef A\n"
  2803. " f(42);\n"
  2804. " x();\n"
  2805. "#else\n"
  2806. " g();\n"
  2807. " x();\n"
  2808. "#endif\n"
  2809. "}");
  2810. verifyFormat("void f(param1, param2,\n"
  2811. " param3,\n"
  2812. "#ifdef A\n"
  2813. " param4(param5,\n"
  2814. "#ifdef A1\n"
  2815. " param6,\n"
  2816. "#ifdef A2\n"
  2817. " param7),\n"
  2818. "#else\n"
  2819. " param8),\n"
  2820. " param9,\n"
  2821. "#endif\n"
  2822. " param10,\n"
  2823. "#endif\n"
  2824. " param11)\n"
  2825. "#else\n"
  2826. " param12)\n"
  2827. "#endif\n"
  2828. "{\n"
  2829. " x();\n"
  2830. "}",
  2831. getLLVMStyleWithColumns(28));
  2832. verifyFormat("#if 1\n"
  2833. "int i;");
  2834. verifyFormat("#if 1\n"
  2835. "#endif\n"
  2836. "#if 1\n"
  2837. "#else\n"
  2838. "#endif\n");
  2839. verifyFormat("DEBUG({\n"
  2840. " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2841. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  2842. "});\n"
  2843. "#if a\n"
  2844. "#else\n"
  2845. "#endif");
  2846. verifyIncompleteFormat("void f(\n"
  2847. "#if A\n"
  2848. " );\n"
  2849. "#else\n"
  2850. "#endif");
  2851. }
  2852. TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
  2853. verifyFormat("#endif\n"
  2854. "#if B");
  2855. }
  2856. TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
  2857. FormatStyle SingleLine = getLLVMStyle();
  2858. SingleLine.AllowShortIfStatementsOnASingleLine = true;
  2859. verifyFormat("#if 0\n"
  2860. "#elif 1\n"
  2861. "#endif\n"
  2862. "void foo() {\n"
  2863. " if (test) foo2();\n"
  2864. "}",
  2865. SingleLine);
  2866. }
  2867. TEST_F(FormatTest, LayoutBlockInsideParens) {
  2868. verifyFormat("functionCall({ int i; });");
  2869. verifyFormat("functionCall({\n"
  2870. " int i;\n"
  2871. " int j;\n"
  2872. "});");
  2873. verifyFormat("functionCall({\n"
  2874. " int i;\n"
  2875. " int j;\n"
  2876. "}, aaaa, bbbb, cccc);");
  2877. verifyFormat("functionA(functionB({\n"
  2878. " int i;\n"
  2879. " int j;\n"
  2880. " }),\n"
  2881. " aaaa, bbbb, cccc);");
  2882. verifyFormat("functionCall(\n"
  2883. " {\n"
  2884. " int i;\n"
  2885. " int j;\n"
  2886. " },\n"
  2887. " aaaa, bbbb, // comment\n"
  2888. " cccc);");
  2889. verifyFormat("functionA(functionB({\n"
  2890. " int i;\n"
  2891. " int j;\n"
  2892. " }),\n"
  2893. " aaaa, bbbb, // comment\n"
  2894. " cccc);");
  2895. verifyFormat("functionCall(aaaa, bbbb, { int i; });");
  2896. verifyFormat("functionCall(aaaa, bbbb, {\n"
  2897. " int i;\n"
  2898. " int j;\n"
  2899. "});");
  2900. verifyFormat(
  2901. "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
  2902. " {\n"
  2903. " int i; // break\n"
  2904. " },\n"
  2905. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  2906. " ccccccccccccccccc));");
  2907. verifyFormat("DEBUG({\n"
  2908. " if (a)\n"
  2909. " f();\n"
  2910. "});");
  2911. }
  2912. TEST_F(FormatTest, LayoutBlockInsideStatement) {
  2913. EXPECT_EQ("SOME_MACRO { int i; }\n"
  2914. "int i;",
  2915. format(" SOME_MACRO {int i;} int i;"));
  2916. }
  2917. TEST_F(FormatTest, LayoutNestedBlocks) {
  2918. verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
  2919. " struct s {\n"
  2920. " int i;\n"
  2921. " };\n"
  2922. " s kBitsToOs[] = {{10}};\n"
  2923. " for (int i = 0; i < 10; ++i)\n"
  2924. " return;\n"
  2925. "}");
  2926. verifyFormat("call(parameter, {\n"
  2927. " something();\n"
  2928. " // Comment using all columns.\n"
  2929. " somethingelse();\n"
  2930. "});",
  2931. getLLVMStyleWithColumns(40));
  2932. verifyFormat("DEBUG( //\n"
  2933. " { f(); }, a);");
  2934. verifyFormat("DEBUG( //\n"
  2935. " {\n"
  2936. " f(); //\n"
  2937. " },\n"
  2938. " a);");
  2939. EXPECT_EQ("call(parameter, {\n"
  2940. " something();\n"
  2941. " // Comment too\n"
  2942. " // looooooooooong.\n"
  2943. " somethingElse();\n"
  2944. "});",
  2945. format("call(parameter, {\n"
  2946. " something();\n"
  2947. " // Comment too looooooooooong.\n"
  2948. " somethingElse();\n"
  2949. "});",
  2950. getLLVMStyleWithColumns(29)));
  2951. EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });"));
  2952. EXPECT_EQ("DEBUG({ // comment\n"
  2953. " int i;\n"
  2954. "});",
  2955. format("DEBUG({ // comment\n"
  2956. "int i;\n"
  2957. "});"));
  2958. EXPECT_EQ("DEBUG({\n"
  2959. " int i;\n"
  2960. "\n"
  2961. " // comment\n"
  2962. " int j;\n"
  2963. "});",
  2964. format("DEBUG({\n"
  2965. " int i;\n"
  2966. "\n"
  2967. " // comment\n"
  2968. " int j;\n"
  2969. "});"));
  2970. verifyFormat("DEBUG({\n"
  2971. " if (a)\n"
  2972. " return;\n"
  2973. "});");
  2974. verifyGoogleFormat("DEBUG({\n"
  2975. " if (a) return;\n"
  2976. "});");
  2977. FormatStyle Style = getGoogleStyle();
  2978. Style.ColumnLimit = 45;
  2979. verifyFormat("Debug(aaaaa, {\n"
  2980. " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
  2981. "}, a);",
  2982. Style);
  2983. verifyFormat("SomeFunction({MACRO({ return output; }), b});");
  2984. verifyNoCrash("^{v^{a}}");
  2985. }
  2986. TEST_F(FormatTest, FormatNestedBlocksInMacros) {
  2987. EXPECT_EQ("#define MACRO() \\\n"
  2988. " Debug(aaa, /* force line break */ \\\n"
  2989. " { \\\n"
  2990. " int i; \\\n"
  2991. " int j; \\\n"
  2992. " })",
  2993. format("#define MACRO() Debug(aaa, /* force line break */ \\\n"
  2994. " { int i; int j; })",
  2995. getGoogleStyle()));
  2996. EXPECT_EQ("#define A \\\n"
  2997. " [] { \\\n"
  2998. " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
  2999. " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
  3000. " }",
  3001. format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
  3002. "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
  3003. getGoogleStyle()));
  3004. }
  3005. TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
  3006. EXPECT_EQ("{}", format("{}"));
  3007. verifyFormat("enum E {};");
  3008. verifyFormat("enum E {}");
  3009. }
  3010. TEST_F(FormatTest, FormatBeginBlockEndMacros) {
  3011. FormatStyle Style = getLLVMStyle();
  3012. Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
  3013. Style.MacroBlockEnd = "^[A-Z_]+_END$";
  3014. verifyFormat("FOO_BEGIN\n"
  3015. " FOO_ENTRY\n"
  3016. "FOO_END", Style);
  3017. verifyFormat("FOO_BEGIN\n"
  3018. " NESTED_FOO_BEGIN\n"
  3019. " NESTED_FOO_ENTRY\n"
  3020. " NESTED_FOO_END\n"
  3021. "FOO_END", Style);
  3022. verifyFormat("FOO_BEGIN(Foo, Bar)\n"
  3023. " int x;\n"
  3024. " x = 1;\n"
  3025. "FOO_END(Baz)", Style);
  3026. }
  3027. //===----------------------------------------------------------------------===//
  3028. // Line break tests.
  3029. //===----------------------------------------------------------------------===//
  3030. TEST_F(FormatTest, PreventConfusingIndents) {
  3031. verifyFormat(
  3032. "void f() {\n"
  3033. " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
  3034. " parameter, parameter, parameter)),\n"
  3035. " SecondLongCall(parameter));\n"
  3036. "}");
  3037. verifyFormat(
  3038. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3039. " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3040. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3041. " aaaaaaaaaaaaaaaaaaaaaaaa);");
  3042. verifyFormat(
  3043. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3044. " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
  3045. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  3046. " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
  3047. verifyFormat(
  3048. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  3049. " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
  3050. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
  3051. " aaaaaaaaaaaaaaaaaaaaaaaa>;");
  3052. verifyFormat("int a = bbbb && ccc && fffff(\n"
  3053. "#define A Just forcing a new line\n"
  3054. " ddd);");
  3055. }
  3056. TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
  3057. verifyFormat(
  3058. "bool aaaaaaa =\n"
  3059. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
  3060. " bbbbbbbb();");
  3061. verifyFormat(
  3062. "bool aaaaaaa =\n"
  3063. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
  3064. " bbbbbbbb();");
  3065. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  3066. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
  3067. " ccccccccc == ddddddddddd;");
  3068. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  3069. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
  3070. " ccccccccc == ddddddddddd;");
  3071. verifyFormat(
  3072. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  3073. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
  3074. " ccccccccc == ddddddddddd;");
  3075. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  3076. " aaaaaa) &&\n"
  3077. " bbbbbb && cccccc;");
  3078. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  3079. " aaaaaa) >>\n"
  3080. " bbbbbb;");
  3081. verifyFormat("Whitespaces.addUntouchableComment(\n"
  3082. " SourceMgr.getSpellingColumnNumber(\n"
  3083. " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
  3084. " 1);");
  3085. verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3086. " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
  3087. " cccccc) {\n}");
  3088. verifyFormat("b = a &&\n"
  3089. " // Comment\n"
  3090. " b.c && d;");
  3091. // If the LHS of a comparison is not a binary expression itself, the
  3092. // additional linebreak confuses many people.
  3093. verifyFormat(
  3094. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3095. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
  3096. "}");
  3097. verifyFormat(
  3098. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3099. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  3100. "}");
  3101. verifyFormat(
  3102. "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
  3103. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  3104. "}");
  3105. // Even explicit parentheses stress the precedence enough to make the
  3106. // additional break unnecessary.
  3107. verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3108. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  3109. "}");
  3110. // This cases is borderline, but with the indentation it is still readable.
  3111. verifyFormat(
  3112. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3113. " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3114. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  3115. "}",
  3116. getLLVMStyleWithColumns(75));
  3117. // If the LHS is a binary expression, we should still use the additional break
  3118. // as otherwise the formatting hides the operator precedence.
  3119. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3120. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3121. " 5) {\n"
  3122. "}");
  3123. FormatStyle OnePerLine = getLLVMStyle();
  3124. OnePerLine.BinPackParameters = false;
  3125. verifyFormat(
  3126. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3127. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3128. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
  3129. OnePerLine);
  3130. }
  3131. TEST_F(FormatTest, ExpressionIndentation) {
  3132. verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3133. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3134. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3135. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3136. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
  3137. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
  3138. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3139. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
  3140. " ccccccccccccccccccccccccccccccccccccccccc;");
  3141. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3142. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3143. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3144. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3145. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3146. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3147. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3148. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3149. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3150. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3151. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3152. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3153. verifyFormat("if () {\n"
  3154. "} else if (aaaaa &&\n"
  3155. " bbbbb > // break\n"
  3156. " ccccc) {\n"
  3157. "}");
  3158. // Presence of a trailing comment used to change indentation of b.
  3159. verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
  3160. " b;\n"
  3161. "return aaaaaaaaaaaaaaaaaaa +\n"
  3162. " b; //",
  3163. getLLVMStyleWithColumns(30));
  3164. }
  3165. TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
  3166. // Not sure what the best system is here. Like this, the LHS can be found
  3167. // immediately above an operator (everything with the same or a higher
  3168. // indent). The RHS is aligned right of the operator and so compasses
  3169. // everything until something with the same indent as the operator is found.
  3170. // FIXME: Is this a good system?
  3171. FormatStyle Style = getLLVMStyle();
  3172. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  3173. verifyFormat(
  3174. "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3175. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3176. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3177. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3178. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3179. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3180. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3181. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3182. " > ccccccccccccccccccccccccccccccccccccccccc;",
  3183. Style);
  3184. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3185. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3186. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3187. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3188. Style);
  3189. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3190. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3191. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3192. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3193. Style);
  3194. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3195. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3196. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3197. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3198. Style);
  3199. verifyFormat("if () {\n"
  3200. "} else if (aaaaa\n"
  3201. " && bbbbb // break\n"
  3202. " > ccccc) {\n"
  3203. "}",
  3204. Style);
  3205. verifyFormat("return (a)\n"
  3206. " // comment\n"
  3207. " + b;",
  3208. Style);
  3209. verifyFormat(
  3210. "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3211. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3212. " + cc;",
  3213. Style);
  3214. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3215. " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  3216. Style);
  3217. // Forced by comments.
  3218. verifyFormat(
  3219. "unsigned ContentSize =\n"
  3220. " sizeof(int16_t) // DWARF ARange version number\n"
  3221. " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
  3222. " + sizeof(int8_t) // Pointer Size (in bytes)\n"
  3223. " + sizeof(int8_t); // Segment Size (in bytes)");
  3224. verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
  3225. " == boost::fusion::at_c<1>(iiii).second;",
  3226. Style);
  3227. Style.ColumnLimit = 60;
  3228. verifyFormat("zzzzzzzzzz\n"
  3229. " = bbbbbbbbbbbbbbbbb\n"
  3230. " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3231. Style);
  3232. }
  3233. TEST_F(FormatTest, NoOperandAlignment) {
  3234. FormatStyle Style = getLLVMStyle();
  3235. Style.AlignOperands = false;
  3236. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
  3237. verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3238. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3239. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3240. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3241. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3242. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3243. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3244. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3245. " > ccccccccccccccccccccccccccccccccccccccccc;",
  3246. Style);
  3247. verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3248. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3249. " + cc;",
  3250. Style);
  3251. verifyFormat("int a = aa\n"
  3252. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3253. " * cccccccccccccccccccccccccccccccccccc;",
  3254. Style);
  3255. Style.AlignAfterOpenBracket = false;
  3256. verifyFormat("return (a > b\n"
  3257. " // comment1\n"
  3258. " // comment2\n"
  3259. " || c);",
  3260. Style);
  3261. }
  3262. TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
  3263. FormatStyle Style = getLLVMStyle();
  3264. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
  3265. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3266. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3267. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
  3268. Style);
  3269. }
  3270. TEST_F(FormatTest, ConstructorInitializers) {
  3271. verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
  3272. verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
  3273. getLLVMStyleWithColumns(45));
  3274. verifyFormat("Constructor()\n"
  3275. " : Inttializer(FitsOnTheLine) {}",
  3276. getLLVMStyleWithColumns(44));
  3277. verifyFormat("Constructor()\n"
  3278. " : Inttializer(FitsOnTheLine) {}",
  3279. getLLVMStyleWithColumns(43));
  3280. verifyFormat(
  3281. "SomeClass::Constructor()\n"
  3282. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  3283. verifyFormat(
  3284. "SomeClass::Constructor()\n"
  3285. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3286. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
  3287. verifyFormat(
  3288. "SomeClass::Constructor()\n"
  3289. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3290. " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  3291. verifyFormat("Constructor()\n"
  3292. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3293. " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3294. " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3295. " aaaaaaaaaaaaaaaaaaaaaaa() {}");
  3296. verifyFormat("Constructor()\n"
  3297. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3298. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3299. verifyFormat("Constructor(int Parameter = 0)\n"
  3300. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
  3301. " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
  3302. verifyFormat("Constructor()\n"
  3303. " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
  3304. "}",
  3305. getLLVMStyleWithColumns(60));
  3306. verifyFormat("Constructor()\n"
  3307. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3308. " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
  3309. // Here a line could be saved by splitting the second initializer onto two
  3310. // lines, but that is not desirable.
  3311. verifyFormat("Constructor()\n"
  3312. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3313. " aaaaaaaaaaa(aaaaaaaaaaa),\n"
  3314. " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3315. FormatStyle OnePerLine = getLLVMStyle();
  3316. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  3317. verifyFormat("SomeClass::Constructor()\n"
  3318. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3319. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3320. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  3321. OnePerLine);
  3322. verifyFormat("SomeClass::Constructor()\n"
  3323. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
  3324. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3325. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  3326. OnePerLine);
  3327. verifyFormat("MyClass::MyClass(int var)\n"
  3328. " : some_var_(var), // 4 space indent\n"
  3329. " some_other_var_(var + 1) { // lined up\n"
  3330. "}",
  3331. OnePerLine);
  3332. verifyFormat("Constructor()\n"
  3333. " : aaaaa(aaaaaa),\n"
  3334. " aaaaa(aaaaaa),\n"
  3335. " aaaaa(aaaaaa),\n"
  3336. " aaaaa(aaaaaa),\n"
  3337. " aaaaa(aaaaaa) {}",
  3338. OnePerLine);
  3339. verifyFormat("Constructor()\n"
  3340. " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
  3341. " aaaaaaaaaaaaaaaaaaaaaa) {}",
  3342. OnePerLine);
  3343. EXPECT_EQ("Constructor()\n"
  3344. " : // Comment forcing unwanted break.\n"
  3345. " aaaa(aaaa) {}",
  3346. format("Constructor() :\n"
  3347. " // Comment forcing unwanted break.\n"
  3348. " aaaa(aaaa) {}"));
  3349. }
  3350. TEST_F(FormatTest, MemoizationTests) {
  3351. // This breaks if the memoization lookup does not take \c Indent and
  3352. // \c LastSpace into account.
  3353. verifyFormat(
  3354. "extern CFRunLoopTimerRef\n"
  3355. "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
  3356. " CFTimeInterval interval, CFOptionFlags flags,\n"
  3357. " CFIndex order, CFRunLoopTimerCallBack callout,\n"
  3358. " CFRunLoopTimerContext *context) {}");
  3359. // Deep nesting somewhat works around our memoization.
  3360. verifyFormat(
  3361. "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3362. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3363. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3364. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3365. " aaaaa())))))))))))))))))))))))))))))))))))))));",
  3366. getLLVMStyleWithColumns(65));
  3367. verifyFormat(
  3368. "aaaaa(\n"
  3369. " aaaaa,\n"
  3370. " aaaaa(\n"
  3371. " aaaaa,\n"
  3372. " aaaaa(\n"
  3373. " aaaaa,\n"
  3374. " aaaaa(\n"
  3375. " aaaaa,\n"
  3376. " aaaaa(\n"
  3377. " aaaaa,\n"
  3378. " aaaaa(\n"
  3379. " aaaaa,\n"
  3380. " aaaaa(\n"
  3381. " aaaaa,\n"
  3382. " aaaaa(\n"
  3383. " aaaaa,\n"
  3384. " aaaaa(\n"
  3385. " aaaaa,\n"
  3386. " aaaaa(\n"
  3387. " aaaaa,\n"
  3388. " aaaaa(\n"
  3389. " aaaaa,\n"
  3390. " aaaaa(\n"
  3391. " aaaaa,\n"
  3392. " aaaaa))))))))))));",
  3393. getLLVMStyleWithColumns(65));
  3394. verifyFormat(
  3395. "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"
  3396. " a),\n"
  3397. " a),\n"
  3398. " a),\n"
  3399. " a),\n"
  3400. " a),\n"
  3401. " a),\n"
  3402. " a),\n"
  3403. " a),\n"
  3404. " a),\n"
  3405. " a),\n"
  3406. " a),\n"
  3407. " a),\n"
  3408. " a),\n"
  3409. " a),\n"
  3410. " a),\n"
  3411. " a),\n"
  3412. " a)",
  3413. getLLVMStyleWithColumns(65));
  3414. // This test takes VERY long when memoization is broken.
  3415. FormatStyle OnePerLine = getLLVMStyle();
  3416. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  3417. OnePerLine.BinPackParameters = false;
  3418. std::string input = "Constructor()\n"
  3419. " : aaaa(a,\n";
  3420. for (unsigned i = 0, e = 80; i != e; ++i) {
  3421. input += " a,\n";
  3422. }
  3423. input += " a) {}";
  3424. verifyFormat(input, OnePerLine);
  3425. }
  3426. TEST_F(FormatTest, BreaksAsHighAsPossible) {
  3427. verifyFormat(
  3428. "void f() {\n"
  3429. " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
  3430. " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
  3431. " f();\n"
  3432. "}");
  3433. verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
  3434. " Intervals[i - 1].getRange().getLast()) {\n}");
  3435. }
  3436. TEST_F(FormatTest, BreaksFunctionDeclarations) {
  3437. // Principially, we break function declarations in a certain order:
  3438. // 1) break amongst arguments.
  3439. verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
  3440. " Cccccccccccccc cccccccccccccc);");
  3441. verifyFormat("template <class TemplateIt>\n"
  3442. "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
  3443. " TemplateIt *stop) {}");
  3444. // 2) break after return type.
  3445. verifyFormat(
  3446. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3447. "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
  3448. getGoogleStyle());
  3449. // 3) break after (.
  3450. verifyFormat(
  3451. "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
  3452. " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
  3453. getGoogleStyle());
  3454. // 4) break before after nested name specifiers.
  3455. verifyFormat(
  3456. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3457. "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
  3458. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
  3459. getGoogleStyle());
  3460. // However, there are exceptions, if a sufficient amount of lines can be
  3461. // saved.
  3462. // FIXME: The precise cut-offs wrt. the number of saved lines might need some
  3463. // more adjusting.
  3464. verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  3465. " Cccccccccccccc cccccccccc,\n"
  3466. " Cccccccccccccc cccccccccc,\n"
  3467. " Cccccccccccccc cccccccccc,\n"
  3468. " Cccccccccccccc cccccccccc);");
  3469. verifyFormat(
  3470. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3471. "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3472. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3473. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
  3474. getGoogleStyle());
  3475. verifyFormat(
  3476. "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  3477. " Cccccccccccccc cccccccccc,\n"
  3478. " Cccccccccccccc cccccccccc,\n"
  3479. " Cccccccccccccc cccccccccc,\n"
  3480. " Cccccccccccccc cccccccccc,\n"
  3481. " Cccccccccccccc cccccccccc,\n"
  3482. " Cccccccccccccc cccccccccc);");
  3483. verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3484. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3485. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3486. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3487. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
  3488. // Break after multi-line parameters.
  3489. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3490. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3491. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3492. " bbbb bbbb);");
  3493. verifyFormat("void SomeLoooooooooooongFunction(\n"
  3494. " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  3495. " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3496. " int bbbbbbbbbbbbb);");
  3497. // Treat overloaded operators like other functions.
  3498. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3499. "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
  3500. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3501. "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
  3502. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3503. "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
  3504. verifyGoogleFormat(
  3505. "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
  3506. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  3507. verifyGoogleFormat(
  3508. "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
  3509. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  3510. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3511. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  3512. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
  3513. "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  3514. verifyGoogleFormat(
  3515. "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
  3516. "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3517. " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
  3518. FormatStyle Style = getLLVMStyle();
  3519. Style.PointerAlignment = FormatStyle::PAS_Left;
  3520. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3521. " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
  3522. Style);
  3523. verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
  3524. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3525. Style);
  3526. }
  3527. TEST_F(FormatTest, TrailingReturnType) {
  3528. verifyFormat("auto foo() -> int;\n");
  3529. verifyFormat("struct S {\n"
  3530. " auto bar() const -> int;\n"
  3531. "};");
  3532. verifyFormat("template <size_t Order, typename T>\n"
  3533. "auto load_img(const std::string &filename)\n"
  3534. " -> alias::tensor<Order, T, mem::tag::cpu> {}");
  3535. verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
  3536. " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
  3537. verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
  3538. verifyFormat("template <typename T>\n"
  3539. "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
  3540. " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
  3541. // Not trailing return types.
  3542. verifyFormat("void f() { auto a = b->c(); }");
  3543. }
  3544. TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
  3545. // Avoid breaking before trailing 'const' or other trailing annotations, if
  3546. // they are not function-like.
  3547. FormatStyle Style = getGoogleStyle();
  3548. Style.ColumnLimit = 47;
  3549. verifyFormat("void someLongFunction(\n"
  3550. " int someLoooooooooooooongParameter) const {\n}",
  3551. getLLVMStyleWithColumns(47));
  3552. verifyFormat("LoooooongReturnType\n"
  3553. "someLoooooooongFunction() const {}",
  3554. getLLVMStyleWithColumns(47));
  3555. verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
  3556. " const {}",
  3557. Style);
  3558. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3559. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
  3560. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3561. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
  3562. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3563. " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
  3564. verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
  3565. " aaaaaaaaaaa aaaaa) const override;");
  3566. verifyGoogleFormat(
  3567. "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3568. " const override;");
  3569. // Even if the first parameter has to be wrapped.
  3570. verifyFormat("void someLongFunction(\n"
  3571. " int someLongParameter) const {}",
  3572. getLLVMStyleWithColumns(46));
  3573. verifyFormat("void someLongFunction(\n"
  3574. " int someLongParameter) const {}",
  3575. Style);
  3576. verifyFormat("void someLongFunction(\n"
  3577. " int someLongParameter) override {}",
  3578. Style);
  3579. verifyFormat("void someLongFunction(\n"
  3580. " int someLongParameter) OVERRIDE {}",
  3581. Style);
  3582. verifyFormat("void someLongFunction(\n"
  3583. " int someLongParameter) final {}",
  3584. Style);
  3585. verifyFormat("void someLongFunction(\n"
  3586. " int someLongParameter) FINAL {}",
  3587. Style);
  3588. verifyFormat("void someLongFunction(\n"
  3589. " int parameter) const override {}",
  3590. Style);
  3591. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  3592. verifyFormat("void someLongFunction(\n"
  3593. " int someLongParameter) const\n"
  3594. "{\n"
  3595. "}",
  3596. Style);
  3597. // Unless these are unknown annotations.
  3598. verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
  3599. " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3600. " LONG_AND_UGLY_ANNOTATION;");
  3601. // Breaking before function-like trailing annotations is fine to keep them
  3602. // close to their arguments.
  3603. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3604. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  3605. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  3606. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  3607. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  3608. " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
  3609. verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
  3610. " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
  3611. verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
  3612. verifyFormat(
  3613. "void aaaaaaaaaaaaaaaaaa()\n"
  3614. " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3615. " aaaaaaaaaaaaaaaaaaaaaaaaa));");
  3616. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3617. " __attribute__((unused));");
  3618. verifyGoogleFormat(
  3619. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3620. " GUARDED_BY(aaaaaaaaaaaa);");
  3621. verifyGoogleFormat(
  3622. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3623. " GUARDED_BY(aaaaaaaaaaaa);");
  3624. verifyGoogleFormat(
  3625. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
  3626. " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3627. verifyGoogleFormat(
  3628. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
  3629. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3630. }
  3631. TEST_F(FormatTest, FunctionAnnotations) {
  3632. verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
  3633. "string OldFunction(const string &parameter) {}");
  3634. verifyFormat("template <typename T>\n"
  3635. "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
  3636. "string OldFunction(const string &parameter) {}");
  3637. // Not function annotations.
  3638. verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3639. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
  3640. verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
  3641. " ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
  3642. }
  3643. TEST_F(FormatTest, BreaksDesireably) {
  3644. verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  3645. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  3646. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
  3647. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3648. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  3649. "}");
  3650. verifyFormat(
  3651. "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3652. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3653. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3654. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3655. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3656. verifyFormat(
  3657. "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3658. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  3659. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3660. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
  3661. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3662. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3663. verifyFormat(
  3664. "void f() {\n"
  3665. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
  3666. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3667. "}");
  3668. verifyFormat(
  3669. "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3670. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3671. verifyFormat(
  3672. "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3673. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3674. verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3675. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3676. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3677. // Indent consistently independent of call expression and unary operator.
  3678. verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3679. " dddddddddddddddddddddddddddddd));");
  3680. verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3681. " dddddddddddddddddddddddddddddd));");
  3682. verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
  3683. " dddddddddddddddddddddddddddddd));");
  3684. // This test case breaks on an incorrect memoization, i.e. an optimization not
  3685. // taking into account the StopAt value.
  3686. verifyFormat(
  3687. "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3688. " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3689. " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3690. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3691. verifyFormat("{\n {\n {\n"
  3692. " Annotation.SpaceRequiredBefore =\n"
  3693. " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
  3694. " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
  3695. " }\n }\n}");
  3696. // Break on an outer level if there was a break on an inner level.
  3697. EXPECT_EQ("f(g(h(a, // comment\n"
  3698. " b, c),\n"
  3699. " d, e),\n"
  3700. " x, y);",
  3701. format("f(g(h(a, // comment\n"
  3702. " b, c), d, e), x, y);"));
  3703. // Prefer breaking similar line breaks.
  3704. verifyFormat(
  3705. "const int kTrackingOptions = NSTrackingMouseMoved |\n"
  3706. " NSTrackingMouseEnteredAndExited |\n"
  3707. " NSTrackingActiveAlways;");
  3708. }
  3709. TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
  3710. FormatStyle NoBinPacking = getGoogleStyle();
  3711. NoBinPacking.BinPackParameters = false;
  3712. NoBinPacking.BinPackArguments = true;
  3713. verifyFormat("void f() {\n"
  3714. " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
  3715. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3716. "}",
  3717. NoBinPacking);
  3718. verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
  3719. " int aaaaaaaaaaaaaaaaaaaa,\n"
  3720. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3721. NoBinPacking);
  3722. }
  3723. TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
  3724. FormatStyle NoBinPacking = getGoogleStyle();
  3725. NoBinPacking.BinPackParameters = false;
  3726. NoBinPacking.BinPackArguments = false;
  3727. verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
  3728. " aaaaaaaaaaaaaaaaaaaa,\n"
  3729. " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
  3730. NoBinPacking);
  3731. verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
  3732. " aaaaaaaaaaaaa,\n"
  3733. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
  3734. NoBinPacking);
  3735. verifyFormat(
  3736. "aaaaaaaa(aaaaaaaaaaaaa,\n"
  3737. " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3738. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  3739. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3740. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
  3741. NoBinPacking);
  3742. verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  3743. " .aaaaaaaaaaaaaaaaaa();",
  3744. NoBinPacking);
  3745. verifyFormat("void f() {\n"
  3746. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3747. " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
  3748. "}",
  3749. NoBinPacking);
  3750. verifyFormat(
  3751. "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3752. " aaaaaaaaaaaa,\n"
  3753. " aaaaaaaaaaaa);",
  3754. NoBinPacking);
  3755. verifyFormat(
  3756. "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
  3757. " ddddddddddddddddddddddddddddd),\n"
  3758. " test);",
  3759. NoBinPacking);
  3760. verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3761. " aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3762. " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;",
  3763. NoBinPacking);
  3764. verifyFormat("a(\"a\"\n"
  3765. " \"a\",\n"
  3766. " a);");
  3767. NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
  3768. verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
  3769. " aaaaaaaaa,\n"
  3770. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3771. NoBinPacking);
  3772. verifyFormat(
  3773. "void f() {\n"
  3774. " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  3775. " .aaaaaaa();\n"
  3776. "}",
  3777. NoBinPacking);
  3778. verifyFormat(
  3779. "template <class SomeType, class SomeOtherType>\n"
  3780. "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
  3781. NoBinPacking);
  3782. }
  3783. TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
  3784. FormatStyle Style = getLLVMStyleWithColumns(15);
  3785. Style.ExperimentalAutoDetectBinPacking = true;
  3786. EXPECT_EQ("aaa(aaaa,\n"
  3787. " aaaa,\n"
  3788. " aaaa);\n"
  3789. "aaa(aaaa,\n"
  3790. " aaaa,\n"
  3791. " aaaa);",
  3792. format("aaa(aaaa,\n" // one-per-line
  3793. " aaaa,\n"
  3794. " aaaa );\n"
  3795. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  3796. Style));
  3797. EXPECT_EQ("aaa(aaaa, aaaa,\n"
  3798. " aaaa);\n"
  3799. "aaa(aaaa, aaaa,\n"
  3800. " aaaa);",
  3801. format("aaa(aaaa, aaaa,\n" // bin-packed
  3802. " aaaa );\n"
  3803. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  3804. Style));
  3805. }
  3806. TEST_F(FormatTest, FormatsBuilderPattern) {
  3807. verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
  3808. " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
  3809. " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
  3810. " .StartsWith(\".init\", ORDER_INIT)\n"
  3811. " .StartsWith(\".fini\", ORDER_FINI)\n"
  3812. " .StartsWith(\".hash\", ORDER_HASH)\n"
  3813. " .Default(ORDER_TEXT);\n");
  3814. verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
  3815. " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
  3816. verifyFormat(
  3817. "aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaa(\n"
  3818. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3819. " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
  3820. verifyFormat(
  3821. "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
  3822. " aaaaaaaaaaaaaa);");
  3823. verifyFormat(
  3824. "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
  3825. " aaaaaa->aaaaaaaaaaaa()\n"
  3826. " ->aaaaaaaaaaaaaaaa(\n"
  3827. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3828. " ->aaaaaaaaaaaaaaaaa();");
  3829. verifyGoogleFormat(
  3830. "void f() {\n"
  3831. " someo->Add((new util::filetools::Handler(dir))\n"
  3832. " ->OnEvent1(NewPermanentCallback(\n"
  3833. " this, &HandlerHolderClass::EventHandlerCBA))\n"
  3834. " ->OnEvent2(NewPermanentCallback(\n"
  3835. " this, &HandlerHolderClass::EventHandlerCBB))\n"
  3836. " ->OnEvent3(NewPermanentCallback(\n"
  3837. " this, &HandlerHolderClass::EventHandlerCBC))\n"
  3838. " ->OnEvent5(NewPermanentCallback(\n"
  3839. " this, &HandlerHolderClass::EventHandlerCBD))\n"
  3840. " ->OnEvent6(NewPermanentCallback(\n"
  3841. " this, &HandlerHolderClass::EventHandlerCBE)));\n"
  3842. "}");
  3843. verifyFormat(
  3844. "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
  3845. verifyFormat("aaaaaaaaaaaaaaa()\n"
  3846. " .aaaaaaaaaaaaaaa()\n"
  3847. " .aaaaaaaaaaaaaaa()\n"
  3848. " .aaaaaaaaaaaaaaa()\n"
  3849. " .aaaaaaaaaaaaaaa();");
  3850. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3851. " .aaaaaaaaaaaaaaa()\n"
  3852. " .aaaaaaaaaaaaaaa()\n"
  3853. " .aaaaaaaaaaaaaaa();");
  3854. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3855. " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3856. " .aaaaaaaaaaaaaaa();");
  3857. verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3858. " ->aaaaaaaaaaaaaae(0)\n"
  3859. " ->aaaaaaaaaaaaaaa();");
  3860. // Don't linewrap after very short segments.
  3861. verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3862. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3863. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3864. verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3865. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3866. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3867. verifyFormat("aaa()\n"
  3868. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3869. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3870. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3871. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  3872. " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3873. " .has<bbbbbbbbbbbbbbbbbbbbb>();");
  3874. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  3875. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  3876. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
  3877. // Prefer not to break after empty parentheses.
  3878. verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
  3879. " First->LastNewlineOffset);");
  3880. }
  3881. TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
  3882. verifyFormat(
  3883. "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3884. " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
  3885. verifyFormat(
  3886. "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
  3887. " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
  3888. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  3889. " ccccccccccccccccccccccccc) {\n}");
  3890. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
  3891. " ccccccccccccccccccccccccc) {\n}");
  3892. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  3893. " ccccccccccccccccccccccccc) {\n}");
  3894. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
  3895. " ccccccccccccccccccccccccc) {\n}");
  3896. verifyFormat(
  3897. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
  3898. " ccccccccccccccccccccccccc) {\n}");
  3899. verifyFormat(
  3900. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
  3901. " ccccccccccccccccccccccccc) {\n}");
  3902. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
  3903. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
  3904. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
  3905. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  3906. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
  3907. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
  3908. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
  3909. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  3910. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
  3911. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
  3912. " aaaaaaaaaaaaaaa != aa) {\n}");
  3913. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
  3914. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
  3915. " aaaaaaaaaaaaaaa != aa) {\n}");
  3916. }
  3917. TEST_F(FormatTest, BreaksAfterAssignments) {
  3918. verifyFormat(
  3919. "unsigned Cost =\n"
  3920. " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
  3921. " SI->getPointerAddressSpaceee());\n");
  3922. verifyFormat(
  3923. "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
  3924. " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
  3925. verifyFormat(
  3926. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
  3927. " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
  3928. verifyFormat("unsigned OriginalStartColumn =\n"
  3929. " SourceMgr.getSpellingColumnNumber(\n"
  3930. " Current.FormatTok.getStartOfNonWhitespace()) -\n"
  3931. " 1;");
  3932. }
  3933. TEST_F(FormatTest, AlignsAfterAssignments) {
  3934. verifyFormat(
  3935. "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3936. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3937. verifyFormat(
  3938. "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3939. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3940. verifyFormat(
  3941. "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3942. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3943. verifyFormat(
  3944. "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3945. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  3946. verifyFormat(
  3947. "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3948. " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3949. " aaaaaaaaaaaaaaaaaaaaaaaa;");
  3950. }
  3951. TEST_F(FormatTest, AlignsAfterReturn) {
  3952. verifyFormat(
  3953. "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3954. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3955. verifyFormat(
  3956. "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3957. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  3958. verifyFormat(
  3959. "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  3960. " aaaaaaaaaaaaaaaaaaaaaa();");
  3961. verifyFormat(
  3962. "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  3963. " aaaaaaaaaaaaaaaaaaaaaa());");
  3964. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3965. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3966. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3967. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
  3968. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3969. verifyFormat("return\n"
  3970. " // true if code is one of a or b.\n"
  3971. " code == a || code == b;");
  3972. }
  3973. TEST_F(FormatTest, AlignsAfterOpenBracket) {
  3974. verifyFormat(
  3975. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
  3976. " aaaaaaaaa aaaaaaa) {}");
  3977. verifyFormat(
  3978. "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
  3979. " aaaaaaaaaaa aaaaaaaaa);");
  3980. verifyFormat(
  3981. "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
  3982. " aaaaaaaaaaaaaaaaaaaaa));");
  3983. FormatStyle Style = getLLVMStyle();
  3984. Style.AlignAfterOpenBracket = false;
  3985. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3986. " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
  3987. Style);
  3988. verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
  3989. " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
  3990. Style);
  3991. verifyFormat("SomeLongVariableName->someFunction(\n"
  3992. " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
  3993. Style);
  3994. verifyFormat(
  3995. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
  3996. " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3997. Style);
  3998. verifyFormat(
  3999. "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
  4000. " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4001. Style);
  4002. verifyFormat(
  4003. "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
  4004. " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
  4005. Style);
  4006. }
  4007. TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
  4008. FormatStyle Style = getLLVMStyleWithColumns(40);
  4009. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4010. " bbbbbbbbbbbbbbbbbbbbbb);",
  4011. Style);
  4012. Style.AlignAfterOpenBracket = true;
  4013. Style.AlignOperands = false;
  4014. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4015. " bbbbbbbbbbbbbbbbbbbbbb);",
  4016. Style);
  4017. Style.AlignAfterOpenBracket = false;
  4018. Style.AlignOperands = true;
  4019. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4020. " bbbbbbbbbbbbbbbbbbbbbb);",
  4021. Style);
  4022. Style.AlignAfterOpenBracket = false;
  4023. Style.AlignOperands = false;
  4024. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4025. " bbbbbbbbbbbbbbbbbbbbbb);",
  4026. Style);
  4027. }
  4028. TEST_F(FormatTest, BreaksConditionalExpressions) {
  4029. verifyFormat(
  4030. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4031. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4032. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4033. verifyFormat(
  4034. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4035. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4036. verifyFormat(
  4037. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
  4038. " : aaaaaaaaaaaaa);");
  4039. verifyFormat(
  4040. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4041. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4042. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4043. " aaaaaaaaaaaaa);");
  4044. verifyFormat(
  4045. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4046. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4047. " aaaaaaaaaaaaa);");
  4048. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4049. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4050. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4051. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4052. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4053. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4054. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4055. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4056. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4057. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4058. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4059. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4060. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4061. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4062. " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4063. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4064. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4065. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4066. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4067. " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4068. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  4069. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4070. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4071. " : aaaaaaaaaaaaaaaa;");
  4072. verifyFormat(
  4073. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4074. " ? aaaaaaaaaaaaaaa\n"
  4075. " : aaaaaaaaaaaaaaa;");
  4076. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  4077. " aaaaaaaaa\n"
  4078. " ? b\n"
  4079. " : c);");
  4080. verifyFormat("return aaaa == bbbb\n"
  4081. " // comment\n"
  4082. " ? aaaa\n"
  4083. " : bbbb;");
  4084. verifyFormat("unsigned Indent =\n"
  4085. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
  4086. " ? IndentForLevel[TheLine.Level]\n"
  4087. " : TheLine * 2,\n"
  4088. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  4089. getLLVMStyleWithColumns(70));
  4090. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  4091. " ? aaaaaaaaaaaaaaa\n"
  4092. " : bbbbbbbbbbbbbbb //\n"
  4093. " ? ccccccccccccccc\n"
  4094. " : ddddddddddddddd;");
  4095. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  4096. " ? aaaaaaaaaaaaaaa\n"
  4097. " : (bbbbbbbbbbbbbbb //\n"
  4098. " ? ccccccccccccccc\n"
  4099. " : ddddddddddddddd);");
  4100. verifyFormat(
  4101. "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4102. " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4103. " aaaaaaaaaaaaaaaaaaaaa +\n"
  4104. " aaaaaaaaaaaaaaaaaaaaa\n"
  4105. " : aaaaaaaaaa;");
  4106. verifyFormat(
  4107. "aaaaaa = aaaaaaaaaaaa\n"
  4108. " ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4109. " : aaaaaaaaaaaaaaaaaaaaaa\n"
  4110. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4111. FormatStyle NoBinPacking = getLLVMStyle();
  4112. NoBinPacking.BinPackArguments = false;
  4113. verifyFormat(
  4114. "void f() {\n"
  4115. " g(aaa,\n"
  4116. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  4117. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4118. " ? aaaaaaaaaaaaaaa\n"
  4119. " : aaaaaaaaaaaaaaa);\n"
  4120. "}",
  4121. NoBinPacking);
  4122. verifyFormat(
  4123. "void f() {\n"
  4124. " g(aaa,\n"
  4125. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  4126. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4127. " ?: aaaaaaaaaaaaaaa);\n"
  4128. "}",
  4129. NoBinPacking);
  4130. verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
  4131. " // comment.\n"
  4132. " ccccccccccccccccccccccccccccccccccccccc\n"
  4133. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4134. " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
  4135. // Assignments in conditional expressions. Apparently not uncommon :-(.
  4136. verifyFormat("return a != b\n"
  4137. " // comment\n"
  4138. " ? a = b\n"
  4139. " : a = b;");
  4140. verifyFormat("return a != b\n"
  4141. " // comment\n"
  4142. " ? a = a != b\n"
  4143. " // comment\n"
  4144. " ? a = b\n"
  4145. " : a\n"
  4146. " : a;\n");
  4147. verifyFormat("return a != b\n"
  4148. " // comment\n"
  4149. " ? a\n"
  4150. " : a = a != b\n"
  4151. " // comment\n"
  4152. " ? a = b\n"
  4153. " : a;");
  4154. }
  4155. TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
  4156. FormatStyle Style = getLLVMStyle();
  4157. Style.BreakBeforeTernaryOperators = false;
  4158. Style.ColumnLimit = 70;
  4159. verifyFormat(
  4160. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4161. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4162. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4163. Style);
  4164. verifyFormat(
  4165. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4166. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4167. Style);
  4168. verifyFormat(
  4169. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
  4170. " aaaaaaaaaaaaa);",
  4171. Style);
  4172. verifyFormat(
  4173. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4174. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4175. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4176. " aaaaaaaaaaaaa);",
  4177. Style);
  4178. verifyFormat(
  4179. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4180. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4181. " aaaaaaaaaaaaa);",
  4182. Style);
  4183. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4184. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4185. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  4186. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4187. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4188. Style);
  4189. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4190. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4191. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4192. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  4193. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4194. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4195. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4196. Style);
  4197. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4198. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
  4199. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4200. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4201. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4202. Style);
  4203. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4204. " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4205. " aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4206. Style);
  4207. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  4208. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4209. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4210. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4211. Style);
  4212. verifyFormat(
  4213. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4214. " aaaaaaaaaaaaaaa :\n"
  4215. " aaaaaaaaaaaaaaa;",
  4216. Style);
  4217. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  4218. " aaaaaaaaa ?\n"
  4219. " b :\n"
  4220. " c);",
  4221. Style);
  4222. verifyFormat(
  4223. "unsigned Indent =\n"
  4224. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n"
  4225. " IndentForLevel[TheLine.Level] :\n"
  4226. " TheLine * 2,\n"
  4227. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  4228. Style);
  4229. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  4230. " aaaaaaaaaaaaaaa :\n"
  4231. " bbbbbbbbbbbbbbb ? //\n"
  4232. " ccccccccccccccc :\n"
  4233. " ddddddddddddddd;",
  4234. Style);
  4235. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  4236. " aaaaaaaaaaaaaaa :\n"
  4237. " (bbbbbbbbbbbbbbb ? //\n"
  4238. " ccccccccccccccc :\n"
  4239. " ddddddddddddddd);",
  4240. Style);
  4241. }
  4242. TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
  4243. verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
  4244. " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
  4245. verifyFormat("bool a = true, b = false;");
  4246. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4247. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
  4248. " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
  4249. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
  4250. verifyFormat(
  4251. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  4252. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
  4253. " d = e && f;");
  4254. verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
  4255. " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
  4256. verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  4257. " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
  4258. verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
  4259. " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
  4260. FormatStyle Style = getGoogleStyle();
  4261. Style.PointerAlignment = FormatStyle::PAS_Left;
  4262. Style.DerivePointerAlignment = false;
  4263. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4264. " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
  4265. " *b = bbbbbbbbbbbbbbbbbbb;",
  4266. Style);
  4267. verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  4268. " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
  4269. Style);
  4270. }
  4271. TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
  4272. verifyFormat("arr[foo ? bar : baz];");
  4273. verifyFormat("f()[foo ? bar : baz];");
  4274. verifyFormat("(a + b)[foo ? bar : baz];");
  4275. verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
  4276. }
  4277. TEST_F(FormatTest, AlignsStringLiterals) {
  4278. verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
  4279. " \"short literal\");");
  4280. verifyFormat(
  4281. "looooooooooooooooooooooooongFunction(\n"
  4282. " \"short literal\"\n"
  4283. " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
  4284. verifyFormat("someFunction(\"Always break between multi-line\"\n"
  4285. " \" string literals\",\n"
  4286. " and, other, parameters);");
  4287. EXPECT_EQ("fun + \"1243\" /* comment */\n"
  4288. " \"5678\";",
  4289. format("fun + \"1243\" /* comment */\n"
  4290. " \"5678\";",
  4291. getLLVMStyleWithColumns(28)));
  4292. EXPECT_EQ(
  4293. "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  4294. " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
  4295. " \"aaaaaaaaaaaaaaaa\";",
  4296. format("aaaaaa ="
  4297. "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  4298. "aaaaaaaaaaaaaaaaaaaaa\" "
  4299. "\"aaaaaaaaaaaaaaaa\";"));
  4300. verifyFormat("a = a + \"a\"\n"
  4301. " \"a\"\n"
  4302. " \"a\";");
  4303. verifyFormat("f(\"a\", \"b\"\n"
  4304. " \"c\");");
  4305. verifyFormat(
  4306. "#define LL_FORMAT \"ll\"\n"
  4307. "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
  4308. " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
  4309. verifyFormat("#define A(X) \\\n"
  4310. " \"aaaaa\" #X \"bbbbbb\" \\\n"
  4311. " \"ccccc\"",
  4312. getLLVMStyleWithColumns(23));
  4313. verifyFormat("#define A \"def\"\n"
  4314. "f(\"abc\" A \"ghi\"\n"
  4315. " \"jkl\");");
  4316. verifyFormat("f(L\"a\"\n"
  4317. " L\"b\");");
  4318. verifyFormat("#define A(X) \\\n"
  4319. " L\"aaaaa\" #X L\"bbbbbb\" \\\n"
  4320. " L\"ccccc\"",
  4321. getLLVMStyleWithColumns(25));
  4322. verifyFormat("f(@\"a\"\n"
  4323. " @\"b\");");
  4324. verifyFormat("NSString s = @\"a\"\n"
  4325. " @\"b\"\n"
  4326. " @\"c\";");
  4327. verifyFormat("NSString s = @\"a\"\n"
  4328. " \"b\"\n"
  4329. " \"c\";");
  4330. }
  4331. TEST_F(FormatTest, DefinitionReturnTypeBreakingStyle) {
  4332. FormatStyle Style = getLLVMStyle();
  4333. Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_TopLevel;
  4334. verifyFormat("class C {\n"
  4335. " int f() { return 1; }\n"
  4336. "};\n"
  4337. "int\n"
  4338. "f() {\n"
  4339. " return 1;\n"
  4340. "}",
  4341. Style);
  4342. Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
  4343. verifyFormat("class C {\n"
  4344. " int\n"
  4345. " f() {\n"
  4346. " return 1;\n"
  4347. " }\n"
  4348. "};\n"
  4349. "int\n"
  4350. "f() {\n"
  4351. " return 1;\n"
  4352. "}",
  4353. Style);
  4354. verifyFormat("const char *\n"
  4355. "f(void) {\n" // Break here.
  4356. " return \"\";\n"
  4357. "}\n"
  4358. "const char *bar(void);\n", // No break here.
  4359. Style);
  4360. verifyFormat("template <class T>\n"
  4361. "T *\n"
  4362. "f(T &c) {\n" // Break here.
  4363. " return NULL;\n"
  4364. "}\n"
  4365. "template <class T> T *f(T &c);\n", // No break here.
  4366. Style);
  4367. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  4368. verifyFormat("const char *\n"
  4369. "f(void)\n" // Break here.
  4370. "{\n"
  4371. " return \"\";\n"
  4372. "}\n"
  4373. "const char *bar(void);\n", // No break here.
  4374. Style);
  4375. verifyFormat("template <class T>\n"
  4376. "T *\n" // Problem here: no line break
  4377. "f(T &c)\n" // Break here.
  4378. "{\n"
  4379. " return NULL;\n"
  4380. "}\n"
  4381. "template <class T> T *f(T &c);\n", // No break here.
  4382. Style);
  4383. }
  4384. TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
  4385. FormatStyle NoBreak = getLLVMStyle();
  4386. NoBreak.AlwaysBreakBeforeMultilineStrings = false;
  4387. FormatStyle Break = getLLVMStyle();
  4388. Break.AlwaysBreakBeforeMultilineStrings = true;
  4389. verifyFormat("aaaa = \"bbbb\"\n"
  4390. " \"cccc\";",
  4391. NoBreak);
  4392. verifyFormat("aaaa =\n"
  4393. " \"bbbb\"\n"
  4394. " \"cccc\";",
  4395. Break);
  4396. verifyFormat("aaaa(\"bbbb\"\n"
  4397. " \"cccc\");",
  4398. NoBreak);
  4399. verifyFormat("aaaa(\n"
  4400. " \"bbbb\"\n"
  4401. " \"cccc\");",
  4402. Break);
  4403. verifyFormat("aaaa(qqq, \"bbbb\"\n"
  4404. " \"cccc\");",
  4405. NoBreak);
  4406. verifyFormat("aaaa(qqq,\n"
  4407. " \"bbbb\"\n"
  4408. " \"cccc\");",
  4409. Break);
  4410. verifyFormat("aaaa(qqq,\n"
  4411. " L\"bbbb\"\n"
  4412. " L\"cccc\");",
  4413. Break);
  4414. verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
  4415. " \"bbbb\"));",
  4416. Break);
  4417. verifyFormat("string s = someFunction(\n"
  4418. " \"abc\"\n"
  4419. " \"abc\");",
  4420. Break);
  4421. // As we break before unary operators, breaking right after them is bad.
  4422. verifyFormat("string foo = abc ? \"x\"\n"
  4423. " \"blah blah blah blah blah blah\"\n"
  4424. " : \"y\";",
  4425. Break);
  4426. // Don't break if there is no column gain.
  4427. verifyFormat("f(\"aaaa\"\n"
  4428. " \"bbbb\");",
  4429. Break);
  4430. // Treat literals with escaped newlines like multi-line string literals.
  4431. EXPECT_EQ("x = \"a\\\n"
  4432. "b\\\n"
  4433. "c\";",
  4434. format("x = \"a\\\n"
  4435. "b\\\n"
  4436. "c\";",
  4437. NoBreak));
  4438. EXPECT_EQ("xxxx =\n"
  4439. " \"a\\\n"
  4440. "b\\\n"
  4441. "c\";",
  4442. format("xxxx = \"a\\\n"
  4443. "b\\\n"
  4444. "c\";",
  4445. Break));
  4446. // Exempt ObjC strings for now.
  4447. EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
  4448. " @\"bbbb\";",
  4449. format("NSString *const kString = @\"aaaa\"\n"
  4450. "@\"bbbb\";",
  4451. Break));
  4452. Break.ColumnLimit = 0;
  4453. verifyFormat("const char *hello = \"hello llvm\";", Break);
  4454. }
  4455. TEST_F(FormatTest, AlignsPipes) {
  4456. verifyFormat(
  4457. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4458. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4459. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4460. verifyFormat(
  4461. "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
  4462. " << aaaaaaaaaaaaaaaaaaaa;");
  4463. verifyFormat(
  4464. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4465. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4466. verifyFormat(
  4467. "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
  4468. " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
  4469. " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
  4470. verifyFormat(
  4471. "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4472. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4473. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4474. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4475. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4476. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4477. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4478. verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
  4479. " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
  4480. verifyFormat(
  4481. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4482. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4483. verifyFormat("return out << \"somepacket = {\\n\"\n"
  4484. " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
  4485. " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
  4486. " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
  4487. " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
  4488. " << \"}\";");
  4489. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  4490. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  4491. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
  4492. verifyFormat(
  4493. "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
  4494. " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
  4495. " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
  4496. " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
  4497. " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
  4498. verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
  4499. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4500. verifyFormat(
  4501. "void f() {\n"
  4502. " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
  4503. " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  4504. "}");
  4505. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
  4506. " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
  4507. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4508. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4509. " aaaaaaaaaaaaaaaaaaaaa)\n"
  4510. " << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4511. verifyFormat("LOG_IF(aaa == //\n"
  4512. " bbb)\n"
  4513. " << a << b;");
  4514. // Breaking before the first "<<" is generally not desirable.
  4515. verifyFormat(
  4516. "llvm::errs()\n"
  4517. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4518. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4519. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4520. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4521. getLLVMStyleWithColumns(70));
  4522. verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4523. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4524. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4525. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4526. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4527. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4528. getLLVMStyleWithColumns(70));
  4529. // But sometimes, breaking before the first "<<" is desirable.
  4530. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
  4531. " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
  4532. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
  4533. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4534. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4535. verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
  4536. " << BEF << IsTemplate << Description << E->getType();");
  4537. verifyFormat(
  4538. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4539. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4540. // Incomplete string literal.
  4541. EXPECT_EQ("llvm::errs() << \"\n"
  4542. " << a;",
  4543. format("llvm::errs() << \"\n<<a;"));
  4544. verifyFormat("void f() {\n"
  4545. " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
  4546. " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
  4547. "}");
  4548. // Handle 'endl'.
  4549. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
  4550. " << bbbbbbbbbbbbbbbbbbbbbb << endl;");
  4551. verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
  4552. }
  4553. TEST_F(FormatTest, UnderstandsEquals) {
  4554. verifyFormat(
  4555. "aaaaaaaaaaaaaaaaa =\n"
  4556. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4557. verifyFormat(
  4558. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4559. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  4560. verifyFormat(
  4561. "if (a) {\n"
  4562. " f();\n"
  4563. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4564. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  4565. "}");
  4566. verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4567. " 100000000 + 10000000) {\n}");
  4568. }
  4569. TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
  4570. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  4571. " .looooooooooooooooooooooooooooooooooooooongFunction();");
  4572. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  4573. " ->looooooooooooooooooooooooooooooooooooooongFunction();");
  4574. verifyFormat(
  4575. "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
  4576. " Parameter2);");
  4577. verifyFormat(
  4578. "ShortObject->shortFunction(\n"
  4579. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
  4580. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
  4581. verifyFormat("loooooooooooooongFunction(\n"
  4582. " LoooooooooooooongObject->looooooooooooooooongFunction());");
  4583. verifyFormat(
  4584. "function(LoooooooooooooooooooooooooooooooooooongObject\n"
  4585. " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
  4586. verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  4587. " .WillRepeatedly(Return(SomeValue));");
  4588. verifyFormat("void f() {\n"
  4589. " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  4590. " .Times(2)\n"
  4591. " .WillRepeatedly(Return(SomeValue));\n"
  4592. "}");
  4593. verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
  4594. " ccccccccccccccccccccccc);");
  4595. verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4596. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4597. " .aaaaa(aaaaa),\n"
  4598. " aaaaaaaaaaaaaaaaaaaaa);");
  4599. verifyFormat("void f() {\n"
  4600. " aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4601. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
  4602. "}");
  4603. verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4604. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4605. " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4606. " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4607. " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  4608. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4609. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4610. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4611. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
  4612. "}");
  4613. // Here, it is not necessary to wrap at "." or "->".
  4614. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
  4615. " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  4616. verifyFormat(
  4617. "aaaaaaaaaaa->aaaaaaaaa(\n"
  4618. " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4619. " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
  4620. verifyFormat(
  4621. "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4622. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
  4623. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
  4624. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  4625. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
  4626. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  4627. verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4628. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4629. " .a();");
  4630. FormatStyle NoBinPacking = getLLVMStyle();
  4631. NoBinPacking.BinPackParameters = false;
  4632. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  4633. " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  4634. " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
  4635. " aaaaaaaaaaaaaaaaaaa,\n"
  4636. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4637. NoBinPacking);
  4638. // If there is a subsequent call, change to hanging indentation.
  4639. verifyFormat(
  4640. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4641. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
  4642. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4643. verifyFormat(
  4644. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4645. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
  4646. verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4647. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4648. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4649. verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4650. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4651. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  4652. }
  4653. TEST_F(FormatTest, WrapsTemplateDeclarations) {
  4654. verifyFormat("template <typename T>\n"
  4655. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  4656. verifyFormat("template <typename T>\n"
  4657. "// T should be one of {A, B}.\n"
  4658. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  4659. verifyFormat(
  4660. "template <typename T>\n"
  4661. "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
  4662. verifyFormat("template <typename T>\n"
  4663. "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
  4664. " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
  4665. verifyFormat(
  4666. "template <typename T>\n"
  4667. "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
  4668. " int Paaaaaaaaaaaaaaaaaaaaram2);");
  4669. verifyFormat(
  4670. "template <typename T>\n"
  4671. "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
  4672. " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
  4673. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4674. verifyFormat("template <typename T>\n"
  4675. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4676. " int aaaaaaaaaaaaaaaaaaaaaa);");
  4677. verifyFormat(
  4678. "template <typename T1, typename T2 = char, typename T3 = char,\n"
  4679. " typename T4 = char>\n"
  4680. "void f();");
  4681. verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
  4682. " template <typename> class cccccccccccccccccccccc,\n"
  4683. " typename ddddddddddddd>\n"
  4684. "class C {};");
  4685. verifyFormat(
  4686. "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
  4687. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4688. verifyFormat("void f() {\n"
  4689. " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
  4690. " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
  4691. "}");
  4692. verifyFormat("template <typename T> class C {};");
  4693. verifyFormat("template <typename T> void f();");
  4694. verifyFormat("template <typename T> void f() {}");
  4695. verifyFormat(
  4696. "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  4697. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4698. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
  4699. " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  4700. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4701. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
  4702. " bbbbbbbbbbbbbbbbbbbbbbbb);",
  4703. getLLVMStyleWithColumns(72));
  4704. EXPECT_EQ("static_cast<A< //\n"
  4705. " B> *>(\n"
  4706. "\n"
  4707. " );",
  4708. format("static_cast<A<//\n"
  4709. " B>*>(\n"
  4710. "\n"
  4711. " );"));
  4712. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4713. " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
  4714. FormatStyle AlwaysBreak = getLLVMStyle();
  4715. AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
  4716. verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
  4717. verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
  4718. verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
  4719. verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4720. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
  4721. " ccccccccccccccccccccccccccccccccccccccccccccccc);");
  4722. verifyFormat("template <template <typename> class Fooooooo,\n"
  4723. " template <typename> class Baaaaaaar>\n"
  4724. "struct C {};",
  4725. AlwaysBreak);
  4726. verifyFormat("template <typename T> // T can be A, B or C.\n"
  4727. "struct C {};",
  4728. AlwaysBreak);
  4729. }
  4730. TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
  4731. verifyFormat(
  4732. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4733. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4734. verifyFormat(
  4735. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4736. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4737. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  4738. // FIXME: Should we have the extra indent after the second break?
  4739. verifyFormat(
  4740. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4741. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4742. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4743. verifyFormat(
  4744. "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
  4745. " cccccccccccccccccccccccccccccccccccccccccccccc());");
  4746. // Breaking at nested name specifiers is generally not desirable.
  4747. verifyFormat(
  4748. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4749. " aaaaaaaaaaaaaaaaaaaaaaa);");
  4750. verifyFormat(
  4751. "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4752. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4753. " aaaaaaaaaaaaaaaaaaaaa);",
  4754. getLLVMStyleWithColumns(74));
  4755. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4756. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4757. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4758. }
  4759. TEST_F(FormatTest, UnderstandsTemplateParameters) {
  4760. verifyFormat("A<int> a;");
  4761. verifyFormat("A<A<A<int>>> a;");
  4762. verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
  4763. verifyFormat("bool x = a < 1 || 2 > a;");
  4764. verifyFormat("bool x = 5 < f<int>();");
  4765. verifyFormat("bool x = f<int>() > 5;");
  4766. verifyFormat("bool x = 5 < a<int>::x;");
  4767. verifyFormat("bool x = a < 4 ? a > 2 : false;");
  4768. verifyFormat("bool x = f() ? a < 2 : a > 2;");
  4769. verifyGoogleFormat("A<A<int>> a;");
  4770. verifyGoogleFormat("A<A<A<int>>> a;");
  4771. verifyGoogleFormat("A<A<A<A<int>>>> a;");
  4772. verifyGoogleFormat("A<A<int> > a;");
  4773. verifyGoogleFormat("A<A<A<int> > > a;");
  4774. verifyGoogleFormat("A<A<A<A<int> > > > a;");
  4775. verifyGoogleFormat("A<::A<int>> a;");
  4776. verifyGoogleFormat("A<::A> a;");
  4777. verifyGoogleFormat("A< ::A> a;");
  4778. verifyGoogleFormat("A< ::A<int> > a;");
  4779. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
  4780. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
  4781. EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
  4782. EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
  4783. verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
  4784. verifyFormat("test >> a >> b;");
  4785. verifyFormat("test << a >> b;");
  4786. verifyFormat("f<int>();");
  4787. verifyFormat("template <typename T> void f() {}");
  4788. verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
  4789. verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
  4790. "sizeof(char)>::type>;");
  4791. verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
  4792. // Not template parameters.
  4793. verifyFormat("return a < b && c > d;");
  4794. verifyFormat("void f() {\n"
  4795. " while (a < b && c > d) {\n"
  4796. " }\n"
  4797. "}");
  4798. verifyFormat("template <typename... Types>\n"
  4799. "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
  4800. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4801. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
  4802. getLLVMStyleWithColumns(60));
  4803. verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
  4804. verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
  4805. verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
  4806. }
  4807. TEST_F(FormatTest, UnderstandsBinaryOperators) {
  4808. verifyFormat("COMPARE(a, ==, b);");
  4809. }
  4810. TEST_F(FormatTest, UnderstandsPointersToMembers) {
  4811. verifyFormat("int A::*x;");
  4812. verifyFormat("int (S::*func)(void *);");
  4813. verifyFormat("void f() { int (S::*func)(void *); }");
  4814. verifyFormat("typedef bool *(Class::*Member)() const;");
  4815. verifyFormat("void f() {\n"
  4816. " (a->*f)();\n"
  4817. " a->*x;\n"
  4818. " (a.*f)();\n"
  4819. " ((*a).*f)();\n"
  4820. " a.*x;\n"
  4821. "}");
  4822. verifyFormat("void f() {\n"
  4823. " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  4824. " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
  4825. "}");
  4826. verifyFormat(
  4827. "(aaaaaaaaaa->*bbbbbbb)(\n"
  4828. " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  4829. FormatStyle Style = getLLVMStyle();
  4830. Style.PointerAlignment = FormatStyle::PAS_Left;
  4831. verifyFormat("typedef bool* (Class::*Member)() const;", Style);
  4832. }
  4833. TEST_F(FormatTest, UnderstandsUnaryOperators) {
  4834. verifyFormat("int a = -2;");
  4835. verifyFormat("f(-1, -2, -3);");
  4836. verifyFormat("a[-1] = 5;");
  4837. verifyFormat("int a = 5 + -2;");
  4838. verifyFormat("if (i == -1) {\n}");
  4839. verifyFormat("if (i != -1) {\n}");
  4840. verifyFormat("if (i > -1) {\n}");
  4841. verifyFormat("if (i < -1) {\n}");
  4842. verifyFormat("++(a->f());");
  4843. verifyFormat("--(a->f());");
  4844. verifyFormat("(a->f())++;");
  4845. verifyFormat("a[42]++;");
  4846. verifyFormat("if (!(a->f())) {\n}");
  4847. verifyFormat("a-- > b;");
  4848. verifyFormat("b ? -a : c;");
  4849. verifyFormat("n * sizeof char16;");
  4850. verifyFormat("n * alignof char16;", getGoogleStyle());
  4851. verifyFormat("sizeof(char);");
  4852. verifyFormat("alignof(char);", getGoogleStyle());
  4853. verifyFormat("return -1;");
  4854. verifyFormat("switch (a) {\n"
  4855. "case -1:\n"
  4856. " break;\n"
  4857. "}");
  4858. verifyFormat("#define X -1");
  4859. verifyFormat("#define X -kConstant");
  4860. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
  4861. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
  4862. verifyFormat("int a = /* confusing comment */ -1;");
  4863. // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
  4864. verifyFormat("int a = i /* confusing comment */++;");
  4865. }
  4866. TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
  4867. verifyFormat("if (!aaaaaaaaaa( // break\n"
  4868. " aaaaa)) {\n"
  4869. "}");
  4870. verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
  4871. " aaaaa));");
  4872. verifyFormat("*aaa = aaaaaaa( // break\n"
  4873. " bbbbbb);");
  4874. }
  4875. TEST_F(FormatTest, UnderstandsOverloadedOperators) {
  4876. verifyFormat("bool operator<();");
  4877. verifyFormat("bool operator>();");
  4878. verifyFormat("bool operator=();");
  4879. verifyFormat("bool operator==();");
  4880. verifyFormat("bool operator!=();");
  4881. verifyFormat("int operator+();");
  4882. verifyFormat("int operator++();");
  4883. verifyFormat("bool operator();");
  4884. verifyFormat("bool operator()();");
  4885. verifyFormat("bool operator[]();");
  4886. verifyFormat("operator bool();");
  4887. verifyFormat("operator int();");
  4888. verifyFormat("operator void *();");
  4889. verifyFormat("operator SomeType<int>();");
  4890. verifyFormat("operator SomeType<int, int>();");
  4891. verifyFormat("operator SomeType<SomeType<int>>();");
  4892. verifyFormat("void *operator new(std::size_t size);");
  4893. verifyFormat("void *operator new[](std::size_t size);");
  4894. verifyFormat("void operator delete(void *ptr);");
  4895. verifyFormat("void operator delete[](void *ptr);");
  4896. verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
  4897. "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
  4898. verifyFormat(
  4899. "ostream &operator<<(ostream &OutputStream,\n"
  4900. " SomeReallyLongType WithSomeReallyLongValue);");
  4901. verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
  4902. " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
  4903. " return left.group < right.group;\n"
  4904. "}");
  4905. verifyFormat("SomeType &operator=(const SomeType &S);");
  4906. verifyFormat("f.template operator()<int>();");
  4907. verifyGoogleFormat("operator void*();");
  4908. verifyGoogleFormat("operator SomeType<SomeType<int>>();");
  4909. verifyGoogleFormat("operator ::A();");
  4910. verifyFormat("using A::operator+;");
  4911. }
  4912. TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
  4913. verifyFormat("Deleted &operator=(const Deleted &)& = default;");
  4914. verifyFormat("Deleted &operator=(const Deleted &)&& = delete;");
  4915. verifyFormat("SomeType MemberFunction(const Deleted &)& = delete;");
  4916. verifyFormat("SomeType MemberFunction(const Deleted &)&& = delete;");
  4917. verifyFormat("Deleted &operator=(const Deleted &)&;");
  4918. verifyFormat("Deleted &operator=(const Deleted &)&&;");
  4919. verifyFormat("SomeType MemberFunction(const Deleted &)&;");
  4920. verifyFormat("SomeType MemberFunction(const Deleted &)&&;");
  4921. verifyGoogleFormat("Deleted& operator=(const Deleted&)& = default;");
  4922. verifyGoogleFormat("SomeType MemberFunction(const Deleted&)& = delete;");
  4923. verifyGoogleFormat("Deleted& operator=(const Deleted&)&;");
  4924. verifyGoogleFormat("SomeType MemberFunction(const Deleted&)&;");
  4925. FormatStyle Spaces = getLLVMStyle();
  4926. Spaces.SpacesInCStyleCastParentheses = true;
  4927. verifyFormat("Deleted &operator=(const Deleted &)& = default;", Spaces);
  4928. verifyFormat("SomeType MemberFunction(const Deleted &)& = delete;", Spaces);
  4929. verifyFormat("Deleted &operator=(const Deleted &)&;", Spaces);
  4930. verifyFormat("SomeType MemberFunction(const Deleted &)&;", Spaces);
  4931. Spaces.SpacesInCStyleCastParentheses = false;
  4932. Spaces.SpacesInParentheses = true;
  4933. verifyFormat("Deleted &operator=( const Deleted & )& = default;", Spaces);
  4934. verifyFormat("SomeType MemberFunction( const Deleted & )& = delete;", Spaces);
  4935. verifyFormat("Deleted &operator=( const Deleted & )&;", Spaces);
  4936. verifyFormat("SomeType MemberFunction( const Deleted & )&;", Spaces);
  4937. }
  4938. TEST_F(FormatTest, UnderstandsNewAndDelete) {
  4939. verifyFormat("void f() {\n"
  4940. " A *a = new A;\n"
  4941. " A *a = new (placement) A;\n"
  4942. " delete a;\n"
  4943. " delete (A *)a;\n"
  4944. "}");
  4945. verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
  4946. " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
  4947. verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4948. " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
  4949. " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
  4950. verifyFormat("delete[] h->p;");
  4951. }
  4952. TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
  4953. verifyFormat("int *f(int *a) {}");
  4954. verifyFormat("int main(int argc, char **argv) {}");
  4955. verifyFormat("Test::Test(int b) : a(b * b) {}");
  4956. verifyIndependentOfContext("f(a, *a);");
  4957. verifyFormat("void g() { f(*a); }");
  4958. verifyIndependentOfContext("int a = b * 10;");
  4959. verifyIndependentOfContext("int a = 10 * b;");
  4960. verifyIndependentOfContext("int a = b * c;");
  4961. verifyIndependentOfContext("int a += b * c;");
  4962. verifyIndependentOfContext("int a -= b * c;");
  4963. verifyIndependentOfContext("int a *= b * c;");
  4964. verifyIndependentOfContext("int a /= b * c;");
  4965. verifyIndependentOfContext("int a = *b;");
  4966. verifyIndependentOfContext("int a = *b * c;");
  4967. verifyIndependentOfContext("int a = b * *c;");
  4968. verifyIndependentOfContext("int a = b * (10);");
  4969. verifyIndependentOfContext("S << b * (10);");
  4970. verifyIndependentOfContext("return 10 * b;");
  4971. verifyIndependentOfContext("return *b * *c;");
  4972. verifyIndependentOfContext("return a & ~b;");
  4973. verifyIndependentOfContext("f(b ? *c : *d);");
  4974. verifyIndependentOfContext("int a = b ? *c : *d;");
  4975. verifyIndependentOfContext("*b = a;");
  4976. verifyIndependentOfContext("a * ~b;");
  4977. verifyIndependentOfContext("a * !b;");
  4978. verifyIndependentOfContext("a * +b;");
  4979. verifyIndependentOfContext("a * -b;");
  4980. verifyIndependentOfContext("a * ++b;");
  4981. verifyIndependentOfContext("a * --b;");
  4982. verifyIndependentOfContext("a[4] * b;");
  4983. verifyIndependentOfContext("a[a * a] = 1;");
  4984. verifyIndependentOfContext("f() * b;");
  4985. verifyIndependentOfContext("a * [self dostuff];");
  4986. verifyIndependentOfContext("int x = a * (a + b);");
  4987. verifyIndependentOfContext("(a *)(a + b);");
  4988. verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
  4989. verifyIndependentOfContext("int *pa = (int *)&a;");
  4990. verifyIndependentOfContext("return sizeof(int **);");
  4991. verifyIndependentOfContext("return sizeof(int ******);");
  4992. verifyIndependentOfContext("return (int **&)a;");
  4993. verifyIndependentOfContext("f((*PointerToArray)[10]);");
  4994. verifyFormat("void f(Type (*parameter)[10]) {}");
  4995. verifyGoogleFormat("return sizeof(int**);");
  4996. verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
  4997. verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
  4998. verifyFormat("auto a = [](int **&, int ***) {};");
  4999. verifyFormat("auto PointerBinding = [](const char *S) {};");
  5000. verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
  5001. verifyFormat("[](const decltype(*a) &value) {}");
  5002. verifyFormat("#define MACRO() [](A *a) { return 1; }");
  5003. verifyIndependentOfContext("typedef void (*f)(int *a);");
  5004. verifyIndependentOfContext("int i{a * b};");
  5005. verifyIndependentOfContext("aaa && aaa->f();");
  5006. verifyIndependentOfContext("int x = ~*p;");
  5007. verifyFormat("Constructor() : a(a), area(width * height) {}");
  5008. verifyFormat("Constructor() : a(a), area(a, width * height) {}");
  5009. verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
  5010. verifyFormat("void f() { f(a, c * d); }");
  5011. verifyFormat("void f() { f(new a(), c * d); }");
  5012. verifyIndependentOfContext("InvalidRegions[*R] = 0;");
  5013. verifyIndependentOfContext("A<int *> a;");
  5014. verifyIndependentOfContext("A<int **> a;");
  5015. verifyIndependentOfContext("A<int *, int *> a;");
  5016. verifyIndependentOfContext("A<int *[]> a;");
  5017. verifyIndependentOfContext(
  5018. "const char *const p = reinterpret_cast<const char *const>(q);");
  5019. verifyIndependentOfContext("A<int **, int **> a;");
  5020. verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
  5021. verifyFormat("for (char **a = b; *a; ++a) {\n}");
  5022. verifyFormat("for (; a && b;) {\n}");
  5023. verifyFormat("bool foo = true && [] { return false; }();");
  5024. verifyFormat(
  5025. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5026. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5027. verifyGoogleFormat("**outparam = 1;");
  5028. verifyGoogleFormat("*outparam = a * b;");
  5029. verifyGoogleFormat("int main(int argc, char** argv) {}");
  5030. verifyGoogleFormat("A<int*> a;");
  5031. verifyGoogleFormat("A<int**> a;");
  5032. verifyGoogleFormat("A<int*, int*> a;");
  5033. verifyGoogleFormat("A<int**, int**> a;");
  5034. verifyGoogleFormat("f(b ? *c : *d);");
  5035. verifyGoogleFormat("int a = b ? *c : *d;");
  5036. verifyGoogleFormat("Type* t = **x;");
  5037. verifyGoogleFormat("Type* t = *++*x;");
  5038. verifyGoogleFormat("*++*x;");
  5039. verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
  5040. verifyGoogleFormat("Type* t = x++ * y;");
  5041. verifyGoogleFormat(
  5042. "const char* const p = reinterpret_cast<const char* const>(q);");
  5043. verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
  5044. verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
  5045. verifyGoogleFormat("template <typename T>\n"
  5046. "void f(int i = 0, SomeType** temps = NULL);");
  5047. FormatStyle Left = getLLVMStyle();
  5048. Left.PointerAlignment = FormatStyle::PAS_Left;
  5049. verifyFormat("x = *a(x) = *a(y);", Left);
  5050. verifyIndependentOfContext("a = *(x + y);");
  5051. verifyIndependentOfContext("a = &(x + y);");
  5052. verifyIndependentOfContext("*(x + y).call();");
  5053. verifyIndependentOfContext("&(x + y)->call();");
  5054. verifyFormat("void f() { &(*I).first; }");
  5055. verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
  5056. verifyFormat(
  5057. "int *MyValues = {\n"
  5058. " *A, // Operator detection might be confused by the '{'\n"
  5059. " *BB // Operator detection might be confused by previous comment\n"
  5060. "};");
  5061. verifyIndependentOfContext("if (int *a = &b)");
  5062. verifyIndependentOfContext("if (int &a = *b)");
  5063. verifyIndependentOfContext("if (a & b[i])");
  5064. verifyIndependentOfContext("if (a::b::c::d & b[i])");
  5065. verifyIndependentOfContext("if (*b[i])");
  5066. verifyIndependentOfContext("if (int *a = (&b))");
  5067. verifyIndependentOfContext("while (int *a = &b)");
  5068. verifyIndependentOfContext("size = sizeof *a;");
  5069. verifyIndependentOfContext("if (a && (b = c))");
  5070. verifyFormat("void f() {\n"
  5071. " for (const int &v : Values) {\n"
  5072. " }\n"
  5073. "}");
  5074. verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
  5075. verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
  5076. verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
  5077. verifyFormat("#define A (!a * b)");
  5078. verifyFormat("#define MACRO \\\n"
  5079. " int *i = a * b; \\\n"
  5080. " void f(a *b);",
  5081. getLLVMStyleWithColumns(19));
  5082. verifyIndependentOfContext("A = new SomeType *[Length];");
  5083. verifyIndependentOfContext("A = new SomeType *[Length]();");
  5084. verifyIndependentOfContext("T **t = new T *;");
  5085. verifyIndependentOfContext("T **t = new T *();");
  5086. verifyGoogleFormat("A = new SomeType*[Length]();");
  5087. verifyGoogleFormat("A = new SomeType*[Length];");
  5088. verifyGoogleFormat("T** t = new T*;");
  5089. verifyGoogleFormat("T** t = new T*();");
  5090. FormatStyle PointerLeft = getLLVMStyle();
  5091. PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
  5092. verifyFormat("delete *x;", PointerLeft);
  5093. verifyFormat("STATIC_ASSERT((a & b) == 0);");
  5094. verifyFormat("STATIC_ASSERT(0 == (a & b));");
  5095. verifyFormat("template <bool a, bool b> "
  5096. "typename t::if<x && y>::type f() {}");
  5097. verifyFormat("template <int *y> f() {}");
  5098. verifyFormat("vector<int *> v;");
  5099. verifyFormat("vector<int *const> v;");
  5100. verifyFormat("vector<int *const **const *> v;");
  5101. verifyFormat("vector<int *volatile> v;");
  5102. verifyFormat("vector<a * b> v;");
  5103. verifyFormat("foo<b && false>();");
  5104. verifyFormat("foo<b & 1>();");
  5105. verifyFormat("decltype(*::std::declval<const T &>()) void F();");
  5106. verifyFormat(
  5107. "template <class T, class = typename std::enable_if<\n"
  5108. " std::is_integral<T>::value &&\n"
  5109. " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
  5110. "void F();",
  5111. getLLVMStyleWithColumns(76));
  5112. verifyFormat(
  5113. "template <class T,\n"
  5114. " class = typename ::std::enable_if<\n"
  5115. " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
  5116. "void F();",
  5117. getGoogleStyleWithColumns(68));
  5118. verifyIndependentOfContext("MACRO(int *i);");
  5119. verifyIndependentOfContext("MACRO(auto *a);");
  5120. verifyIndependentOfContext("MACRO(const A *a);");
  5121. verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
  5122. // FIXME: Is there a way to make this work?
  5123. // verifyIndependentOfContext("MACRO(A *a);");
  5124. verifyFormat("DatumHandle const *operator->() const { return input_; }");
  5125. EXPECT_EQ("#define OP(x) \\\n"
  5126. " ostream &operator<<(ostream &s, const A &a) { \\\n"
  5127. " return s << a.DebugString(); \\\n"
  5128. " }",
  5129. format("#define OP(x) \\\n"
  5130. " ostream &operator<<(ostream &s, const A &a) { \\\n"
  5131. " return s << a.DebugString(); \\\n"
  5132. " }",
  5133. getLLVMStyleWithColumns(50)));
  5134. // FIXME: We cannot handle this case yet; we might be able to figure out that
  5135. // foo<x> d > v; doesn't make sense.
  5136. verifyFormat("foo<a<b && c> d> v;");
  5137. FormatStyle PointerMiddle = getLLVMStyle();
  5138. PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
  5139. verifyFormat("delete *x;", PointerMiddle);
  5140. verifyFormat("int * x;", PointerMiddle);
  5141. verifyFormat("template <int * y> f() {}", PointerMiddle);
  5142. verifyFormat("int * f(int * a) {}", PointerMiddle);
  5143. verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
  5144. verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
  5145. verifyFormat("A<int *> a;", PointerMiddle);
  5146. verifyFormat("A<int **> a;", PointerMiddle);
  5147. verifyFormat("A<int *, int *> a;", PointerMiddle);
  5148. verifyFormat("A<int * []> a;", PointerMiddle);
  5149. verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
  5150. verifyFormat("A = new SomeType *[Length];", PointerMiddle);
  5151. verifyFormat("T ** t = new T *;", PointerMiddle);
  5152. // Member function reference qualifiers aren't binary operators.
  5153. verifyFormat("string // break\n"
  5154. "operator()() & {}");
  5155. verifyFormat("string // break\n"
  5156. "operator()() && {}");
  5157. verifyGoogleFormat("template <typename T>\n"
  5158. "auto x() & -> int {}");
  5159. }
  5160. TEST_F(FormatTest, UnderstandsAttributes) {
  5161. verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
  5162. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
  5163. "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
  5164. FormatStyle AfterType = getLLVMStyle();
  5165. AfterType.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
  5166. verifyFormat("__attribute__((nodebug)) void\n"
  5167. "foo() {}\n",
  5168. AfterType);
  5169. }
  5170. TEST_F(FormatTest, UnderstandsEllipsis) {
  5171. verifyFormat("int printf(const char *fmt, ...);");
  5172. verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
  5173. verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
  5174. FormatStyle PointersLeft = getLLVMStyle();
  5175. PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
  5176. verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
  5177. }
  5178. TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
  5179. EXPECT_EQ("int *a;\n"
  5180. "int *a;\n"
  5181. "int *a;",
  5182. format("int *a;\n"
  5183. "int* a;\n"
  5184. "int *a;",
  5185. getGoogleStyle()));
  5186. EXPECT_EQ("int* a;\n"
  5187. "int* a;\n"
  5188. "int* a;",
  5189. format("int* a;\n"
  5190. "int* a;\n"
  5191. "int *a;",
  5192. getGoogleStyle()));
  5193. EXPECT_EQ("int *a;\n"
  5194. "int *a;\n"
  5195. "int *a;",
  5196. format("int *a;\n"
  5197. "int * a;\n"
  5198. "int * a;",
  5199. getGoogleStyle()));
  5200. }
  5201. TEST_F(FormatTest, UnderstandsRvalueReferences) {
  5202. verifyFormat("int f(int &&a) {}");
  5203. verifyFormat("int f(int a, char &&b) {}");
  5204. verifyFormat("void f() { int &&a = b; }");
  5205. verifyGoogleFormat("int f(int a, char&& b) {}");
  5206. verifyGoogleFormat("void f() { int&& a = b; }");
  5207. verifyIndependentOfContext("A<int &&> a;");
  5208. verifyIndependentOfContext("A<int &&, int &&> a;");
  5209. verifyGoogleFormat("A<int&&> a;");
  5210. verifyGoogleFormat("A<int&&, int&&> a;");
  5211. // Not rvalue references:
  5212. verifyFormat("template <bool B, bool C> class A {\n"
  5213. " static_assert(B && C, \"Something is wrong\");\n"
  5214. "};");
  5215. verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
  5216. verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
  5217. verifyFormat("#define A(a, b) (a && b)");
  5218. }
  5219. TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
  5220. verifyFormat("void f() {\n"
  5221. " x[aaaaaaaaa -\n"
  5222. " b] = 23;\n"
  5223. "}",
  5224. getLLVMStyleWithColumns(15));
  5225. }
  5226. TEST_F(FormatTest, FormatsCasts) {
  5227. verifyFormat("Type *A = static_cast<Type *>(P);");
  5228. verifyFormat("Type *A = (Type *)P;");
  5229. verifyFormat("Type *A = (vector<Type *, int *>)P;");
  5230. verifyFormat("int a = (int)(2.0f);");
  5231. verifyFormat("int a = (int)2.0f;");
  5232. verifyFormat("x[(int32)y];");
  5233. verifyFormat("x = (int32)y;");
  5234. verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
  5235. verifyFormat("int a = (int)*b;");
  5236. verifyFormat("int a = (int)2.0f;");
  5237. verifyFormat("int a = (int)~0;");
  5238. verifyFormat("int a = (int)++a;");
  5239. verifyFormat("int a = (int)sizeof(int);");
  5240. verifyFormat("int a = (int)+2;");
  5241. verifyFormat("my_int a = (my_int)2.0f;");
  5242. verifyFormat("my_int a = (my_int)sizeof(int);");
  5243. verifyFormat("return (my_int)aaa;");
  5244. verifyFormat("#define x ((int)-1)");
  5245. verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
  5246. verifyFormat("#define p(q) ((int *)&q)");
  5247. verifyFormat("fn(a)(b) + 1;");
  5248. verifyFormat("void f() { my_int a = (my_int)*b; }");
  5249. verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
  5250. verifyFormat("my_int a = (my_int)~0;");
  5251. verifyFormat("my_int a = (my_int)++a;");
  5252. verifyFormat("my_int a = (my_int)-2;");
  5253. verifyFormat("my_int a = (my_int)1;");
  5254. verifyFormat("my_int a = (my_int *)1;");
  5255. verifyFormat("my_int a = (const my_int)-1;");
  5256. verifyFormat("my_int a = (const my_int *)-1;");
  5257. verifyFormat("my_int a = (my_int)(my_int)-1;");
  5258. verifyFormat("my_int a = (ns::my_int)-2;");
  5259. verifyFormat("case (my_int)ONE:");
  5260. // FIXME: single value wrapped with paren will be treated as cast.
  5261. verifyFormat("void f(int i = (kValue)*kMask) {}");
  5262. verifyFormat("{ (void)F; }");
  5263. // Don't break after a cast's
  5264. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  5265. " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
  5266. " bbbbbbbbbbbbbbbbbbbbbb);");
  5267. // These are not casts.
  5268. verifyFormat("void f(int *) {}");
  5269. verifyFormat("f(foo)->b;");
  5270. verifyFormat("f(foo).b;");
  5271. verifyFormat("f(foo)(b);");
  5272. verifyFormat("f(foo)[b];");
  5273. verifyFormat("[](foo) { return 4; }(bar);");
  5274. verifyFormat("(*funptr)(foo)[4];");
  5275. verifyFormat("funptrs[4](foo)[4];");
  5276. verifyFormat("void f(int *);");
  5277. verifyFormat("void f(int *) = 0;");
  5278. verifyFormat("void f(SmallVector<int>) {}");
  5279. verifyFormat("void f(SmallVector<int>);");
  5280. verifyFormat("void f(SmallVector<int>) = 0;");
  5281. verifyFormat("void f(int i = (kA * kB) & kMask) {}");
  5282. verifyFormat("int a = sizeof(int) * b;");
  5283. verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
  5284. verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
  5285. verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
  5286. verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
  5287. // These are not casts, but at some point were confused with casts.
  5288. verifyFormat("virtual void foo(int *) override;");
  5289. verifyFormat("virtual void foo(char &) const;");
  5290. verifyFormat("virtual void foo(int *a, char *) const;");
  5291. verifyFormat("int a = sizeof(int *) + b;");
  5292. verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
  5293. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
  5294. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  5295. // FIXME: The indentation here is not ideal.
  5296. verifyFormat(
  5297. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5298. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
  5299. " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
  5300. }
  5301. TEST_F(FormatTest, FormatsFunctionTypes) {
  5302. verifyFormat("A<bool()> a;");
  5303. verifyFormat("A<SomeType()> a;");
  5304. verifyFormat("A<void (*)(int, std::string)> a;");
  5305. verifyFormat("A<void *(int)>;");
  5306. verifyFormat("void *(*a)(int *, SomeType *);");
  5307. verifyFormat("int (*func)(void *);");
  5308. verifyFormat("void f() { int (*func)(void *); }");
  5309. verifyFormat("template <class CallbackClass>\n"
  5310. "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
  5311. verifyGoogleFormat("A<void*(int*, SomeType*)>;");
  5312. verifyGoogleFormat("void* (*a)(int);");
  5313. verifyGoogleFormat(
  5314. "template <class CallbackClass>\n"
  5315. "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
  5316. // Other constructs can look somewhat like function types:
  5317. verifyFormat("A<sizeof(*x)> a;");
  5318. verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
  5319. verifyFormat("some_var = function(*some_pointer_var)[0];");
  5320. verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
  5321. }
  5322. TEST_F(FormatTest, FormatsPointersToArrayTypes) {
  5323. verifyFormat("A (*foo_)[6];");
  5324. verifyFormat("vector<int> (*foo_)[6];");
  5325. }
  5326. TEST_F(FormatTest, BreaksLongVariableDeclarations) {
  5327. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5328. " LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5329. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
  5330. " LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5331. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5332. " *LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5333. // Different ways of ()-initializiation.
  5334. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5335. " LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
  5336. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5337. " LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
  5338. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5339. " LoooooooooooooooooooooooooooooooooooooooongVariable({});");
  5340. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5341. " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
  5342. }
  5343. TEST_F(FormatTest, BreaksLongDeclarations) {
  5344. verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
  5345. " AnotherNameForTheLongType;");
  5346. verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
  5347. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  5348. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5349. "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
  5350. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
  5351. "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
  5352. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5353. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5354. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
  5355. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5356. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  5357. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5358. verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
  5359. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5360. FormatStyle Indented = getLLVMStyle();
  5361. Indented.IndentWrappedFunctionNames = true;
  5362. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5363. " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
  5364. Indented);
  5365. verifyFormat(
  5366. "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5367. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5368. Indented);
  5369. verifyFormat(
  5370. "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  5371. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5372. Indented);
  5373. verifyFormat(
  5374. "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
  5375. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5376. Indented);
  5377. // FIXME: Without the comment, this breaks after "(".
  5378. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
  5379. " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
  5380. getGoogleStyle());
  5381. verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
  5382. " int LoooooooooooooooooooongParam2) {}");
  5383. verifyFormat(
  5384. "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
  5385. " SourceLocation L, IdentifierIn *II,\n"
  5386. " Type *T) {}");
  5387. verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
  5388. "ReallyReaaallyLongFunctionName(\n"
  5389. " const std::string &SomeParameter,\n"
  5390. " const SomeType<string, SomeOtherTemplateParameter>\n"
  5391. " &ReallyReallyLongParameterName,\n"
  5392. " const SomeType<string, SomeOtherTemplateParameter>\n"
  5393. " &AnotherLongParameterName) {}");
  5394. verifyFormat("template <typename A>\n"
  5395. "SomeLoooooooooooooooooooooongType<\n"
  5396. " typename some_namespace::SomeOtherType<A>::Type>\n"
  5397. "Function() {}");
  5398. verifyGoogleFormat(
  5399. "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
  5400. " aaaaaaaaaaaaaaaaaaaaaaa;");
  5401. verifyGoogleFormat(
  5402. "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
  5403. " SourceLocation L) {}");
  5404. verifyGoogleFormat(
  5405. "some_namespace::LongReturnType\n"
  5406. "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
  5407. " int first_long_parameter, int second_parameter) {}");
  5408. verifyGoogleFormat("template <typename T>\n"
  5409. "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  5410. "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
  5411. verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5412. " int aaaaaaaaaaaaaaaaaaaaaaa);");
  5413. verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  5414. " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5415. " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5416. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5417. " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  5418. " aaaaaaaaaaaaaaaaaaaaaaaa);");
  5419. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5420. " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  5421. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
  5422. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5423. }
  5424. TEST_F(FormatTest, FormatsArrays) {
  5425. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5426. " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
  5427. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5428. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  5429. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5430. " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
  5431. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5432. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5433. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  5434. verifyFormat(
  5435. "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
  5436. " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5437. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  5438. verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
  5439. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
  5440. verifyFormat(
  5441. "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
  5442. " .aaaaaaa[0]\n"
  5443. " .aaaaaaaaaaaaaaaaaaaaaa();");
  5444. verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
  5445. }
  5446. TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
  5447. verifyFormat("(a)->b();");
  5448. verifyFormat("--a;");
  5449. }
  5450. TEST_F(FormatTest, HandlesIncludeDirectives) {
  5451. verifyFormat("#include <string>\n"
  5452. "#include <a/b/c.h>\n"
  5453. "#include \"a/b/string\"\n"
  5454. "#include \"string.h\"\n"
  5455. "#include \"string.h\"\n"
  5456. "#include <a-a>\n"
  5457. "#include < path with space >\n"
  5458. "#include_next <test.h>"
  5459. "#include \"abc.h\" // this is included for ABC\n"
  5460. "#include \"some long include\" // with a comment\n"
  5461. "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
  5462. getLLVMStyleWithColumns(35));
  5463. EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\""));
  5464. EXPECT_EQ("#include <a>", format("#include<a>"));
  5465. verifyFormat("#import <string>");
  5466. verifyFormat("#import <a/b/c.h>");
  5467. verifyFormat("#import \"a/b/string\"");
  5468. verifyFormat("#import \"string.h\"");
  5469. verifyFormat("#import \"string.h\"");
  5470. verifyFormat("#if __has_include(<strstream>)\n"
  5471. "#include <strstream>\n"
  5472. "#endif");
  5473. verifyFormat("#define MY_IMPORT <a/b>");
  5474. // Protocol buffer definition or missing "#".
  5475. verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
  5476. getLLVMStyleWithColumns(30));
  5477. FormatStyle Style = getLLVMStyle();
  5478. Style.AlwaysBreakBeforeMultilineStrings = true;
  5479. Style.ColumnLimit = 0;
  5480. verifyFormat("#import \"abc.h\"", Style);
  5481. // But 'import' might also be a regular C++ namespace.
  5482. verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5483. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5484. }
  5485. //===----------------------------------------------------------------------===//
  5486. // Error recovery tests.
  5487. //===----------------------------------------------------------------------===//
  5488. TEST_F(FormatTest, IncompleteParameterLists) {
  5489. FormatStyle NoBinPacking = getLLVMStyle();
  5490. NoBinPacking.BinPackParameters = false;
  5491. verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
  5492. " double *min_x,\n"
  5493. " double *max_x,\n"
  5494. " double *min_y,\n"
  5495. " double *max_y,\n"
  5496. " double *min_z,\n"
  5497. " double *max_z, ) {}",
  5498. NoBinPacking);
  5499. }
  5500. TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
  5501. verifyFormat("void f() { return; }\n42");
  5502. verifyFormat("void f() {\n"
  5503. " if (0)\n"
  5504. " return;\n"
  5505. "}\n"
  5506. "42");
  5507. verifyFormat("void f() { return }\n42");
  5508. verifyFormat("void f() {\n"
  5509. " if (0)\n"
  5510. " return\n"
  5511. "}\n"
  5512. "42");
  5513. }
  5514. TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
  5515. EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
  5516. EXPECT_EQ("void f() {\n"
  5517. " if (a)\n"
  5518. " return\n"
  5519. "}",
  5520. format("void f ( ) { if ( a ) return }"));
  5521. EXPECT_EQ("namespace N {\n"
  5522. "void f()\n"
  5523. "}",
  5524. format("namespace N { void f() }"));
  5525. EXPECT_EQ("namespace N {\n"
  5526. "void f() {}\n"
  5527. "void g()\n"
  5528. "}",
  5529. format("namespace N { void f( ) { } void g( ) }"));
  5530. }
  5531. TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
  5532. verifyFormat("int aaaaaaaa =\n"
  5533. " // Overlylongcomment\n"
  5534. " b;",
  5535. getLLVMStyleWithColumns(20));
  5536. verifyFormat("function(\n"
  5537. " ShortArgument,\n"
  5538. " LoooooooooooongArgument);\n",
  5539. getLLVMStyleWithColumns(20));
  5540. }
  5541. TEST_F(FormatTest, IncorrectAccessSpecifier) {
  5542. verifyFormat("public:");
  5543. verifyFormat("class A {\n"
  5544. "public\n"
  5545. " void f() {}\n"
  5546. "};");
  5547. verifyFormat("public\n"
  5548. "int qwerty;");
  5549. verifyFormat("public\n"
  5550. "B {}");
  5551. verifyFormat("public\n"
  5552. "{}");
  5553. verifyFormat("public\n"
  5554. "B { int x; }");
  5555. }
  5556. TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
  5557. verifyFormat("{");
  5558. verifyFormat("#})");
  5559. verifyNoCrash("(/**/[:!] ?[).");
  5560. }
  5561. TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
  5562. verifyFormat("do {\n}");
  5563. verifyFormat("do {\n}\n"
  5564. "f();");
  5565. verifyFormat("do {\n}\n"
  5566. "wheeee(fun);");
  5567. verifyFormat("do {\n"
  5568. " f();\n"
  5569. "}");
  5570. }
  5571. TEST_F(FormatTest, IncorrectCodeMissingParens) {
  5572. verifyFormat("if {\n foo;\n foo();\n}");
  5573. verifyFormat("switch {\n foo;\n foo();\n}");
  5574. verifyIncompleteFormat("for {\n foo;\n foo();\n}");
  5575. verifyFormat("while {\n foo;\n foo();\n}");
  5576. verifyFormat("do {\n foo;\n foo();\n} while;");
  5577. }
  5578. TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
  5579. verifyIncompleteFormat("namespace {\n"
  5580. "class Foo { Foo (\n"
  5581. "};\n"
  5582. "} // comment");
  5583. }
  5584. TEST_F(FormatTest, IncorrectCodeErrorDetection) {
  5585. EXPECT_EQ("{\n {}\n", format("{\n{\n}\n"));
  5586. EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
  5587. EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
  5588. EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n"));
  5589. EXPECT_EQ("{\n"
  5590. " {\n"
  5591. " breakme(\n"
  5592. " qwe);\n"
  5593. " }\n",
  5594. format("{\n"
  5595. " {\n"
  5596. " breakme(qwe);\n"
  5597. "}\n",
  5598. getLLVMStyleWithColumns(10)));
  5599. }
  5600. TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
  5601. verifyFormat("int x = {\n"
  5602. " avariable,\n"
  5603. " b(alongervariable)};",
  5604. getLLVMStyleWithColumns(25));
  5605. }
  5606. TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
  5607. verifyFormat("return (a)(b){1, 2, 3};");
  5608. }
  5609. TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
  5610. verifyFormat("vector<int> x{1, 2, 3, 4};");
  5611. verifyFormat("vector<int> x{\n"
  5612. " 1, 2, 3, 4,\n"
  5613. "};");
  5614. verifyFormat("vector<T> x{{}, {}, {}, {}};");
  5615. verifyFormat("f({1, 2});");
  5616. verifyFormat("auto v = Foo{-1};");
  5617. verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
  5618. verifyFormat("Class::Class : member{1, 2, 3} {}");
  5619. verifyFormat("new vector<int>{1, 2, 3};");
  5620. verifyFormat("new int[3]{1, 2, 3};");
  5621. verifyFormat("new int{1};");
  5622. verifyFormat("return {arg1, arg2};");
  5623. verifyFormat("return {arg1, SomeType{parameter}};");
  5624. verifyFormat("int count = set<int>{f(), g(), h()}.size();");
  5625. verifyFormat("new T{arg1, arg2};");
  5626. verifyFormat("f(MyMap[{composite, key}]);");
  5627. verifyFormat("class Class {\n"
  5628. " T member = {arg1, arg2};\n"
  5629. "};");
  5630. verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
  5631. verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
  5632. verifyFormat("int a = std::is_integral<int>{} + 0;");
  5633. verifyFormat("int foo(int i) { return fo1{}(i); }");
  5634. verifyFormat("int foo(int i) { return fo1{}(i); }");
  5635. verifyFormat("auto i = decltype(x){};");
  5636. verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
  5637. verifyFormat("Node n{1, Node{1000}, //\n"
  5638. " 2};");
  5639. verifyFormat("Aaaa aaaaaaa{\n"
  5640. " {\n"
  5641. " aaaa,\n"
  5642. " },\n"
  5643. "};");
  5644. verifyFormat("class C : public D {\n"
  5645. " SomeClass SC{2};\n"
  5646. "};");
  5647. verifyFormat("class C : public A {\n"
  5648. " class D : public B {\n"
  5649. " void f() { int i{2}; }\n"
  5650. " };\n"
  5651. "};");
  5652. // In combination with BinPackArguments = false.
  5653. FormatStyle NoBinPacking = getLLVMStyle();
  5654. NoBinPacking.BinPackArguments = false;
  5655. verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
  5656. " bbbbb,\n"
  5657. " ccccc,\n"
  5658. " ddddd,\n"
  5659. " eeeee,\n"
  5660. " ffffff,\n"
  5661. " ggggg,\n"
  5662. " hhhhhh,\n"
  5663. " iiiiii,\n"
  5664. " jjjjjj,\n"
  5665. " kkkkkk};",
  5666. NoBinPacking);
  5667. verifyFormat("const Aaaaaa aaaaa = {\n"
  5668. " aaaaa,\n"
  5669. " bbbbb,\n"
  5670. " ccccc,\n"
  5671. " ddddd,\n"
  5672. " eeeee,\n"
  5673. " ffffff,\n"
  5674. " ggggg,\n"
  5675. " hhhhhh,\n"
  5676. " iiiiii,\n"
  5677. " jjjjjj,\n"
  5678. " kkkkkk,\n"
  5679. "};",
  5680. NoBinPacking);
  5681. verifyFormat(
  5682. "const Aaaaaa aaaaa = {\n"
  5683. " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n"
  5684. " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n"
  5685. " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
  5686. "};",
  5687. NoBinPacking);
  5688. // FIXME: The alignment of these trailing comments might be bad. Then again,
  5689. // this might be utterly useless in real code.
  5690. verifyFormat("Constructor::Constructor()\n"
  5691. " : some_value{ //\n"
  5692. " aaaaaaa, //\n"
  5693. " bbbbbbb} {}");
  5694. // In braced lists, the first comment is always assumed to belong to the
  5695. // first element. Thus, it can be moved to the next or previous line as
  5696. // appropriate.
  5697. EXPECT_EQ("function({// First element:\n"
  5698. " 1,\n"
  5699. " // Second element:\n"
  5700. " 2});",
  5701. format("function({\n"
  5702. " // First element:\n"
  5703. " 1,\n"
  5704. " // Second element:\n"
  5705. " 2});"));
  5706. EXPECT_EQ("std::vector<int> MyNumbers{\n"
  5707. " // First element:\n"
  5708. " 1,\n"
  5709. " // Second element:\n"
  5710. " 2};",
  5711. format("std::vector<int> MyNumbers{// First element:\n"
  5712. " 1,\n"
  5713. " // Second element:\n"
  5714. " 2};",
  5715. getLLVMStyleWithColumns(30)));
  5716. // A trailing comma should still lead to an enforced line break.
  5717. EXPECT_EQ("vector<int> SomeVector = {\n"
  5718. " // aaa\n"
  5719. " 1, 2,\n"
  5720. "};",
  5721. format("vector<int> SomeVector = { // aaa\n"
  5722. " 1, 2, };"));
  5723. FormatStyle ExtraSpaces = getLLVMStyle();
  5724. ExtraSpaces.Cpp11BracedListStyle = false;
  5725. ExtraSpaces.ColumnLimit = 75;
  5726. verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
  5727. verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
  5728. verifyFormat("f({ 1, 2 });", ExtraSpaces);
  5729. verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
  5730. verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
  5731. verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
  5732. verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
  5733. verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
  5734. verifyFormat("return { arg1, arg2 };", ExtraSpaces);
  5735. verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
  5736. verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
  5737. verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
  5738. verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
  5739. verifyFormat("class Class {\n"
  5740. " T member = { arg1, arg2 };\n"
  5741. "};",
  5742. ExtraSpaces);
  5743. verifyFormat(
  5744. "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5745. " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
  5746. " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  5747. " bbbbbbbbbbbbbbbbbbbb, bbbbb };",
  5748. ExtraSpaces);
  5749. verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
  5750. verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
  5751. ExtraSpaces);
  5752. verifyFormat(
  5753. "someFunction(OtherParam,\n"
  5754. " BracedList{ // comment 1 (Forcing interesting break)\n"
  5755. " param1, param2,\n"
  5756. " // comment 2\n"
  5757. " param3, param4 });",
  5758. ExtraSpaces);
  5759. verifyFormat(
  5760. "std::this_thread::sleep_for(\n"
  5761. " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
  5762. ExtraSpaces);
  5763. verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
  5764. " aaaaaaa,\n"
  5765. " aaaaaaaaaa,\n"
  5766. " aaaaa,\n"
  5767. " aaaaaaaaaaaaaaa,\n"
  5768. " aaa,\n"
  5769. " aaaaaaaaaa,\n"
  5770. " a,\n"
  5771. " aaaaaaaaaaaaaaaaaaaaa,\n"
  5772. " aaaaaaaaaaaa,\n"
  5773. " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
  5774. " aaaaaaa,\n"
  5775. " a};");
  5776. verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
  5777. }
  5778. TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
  5779. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5780. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5781. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5782. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5783. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5784. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  5785. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
  5786. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5787. " 1, 22, 333, 4444, 55555, //\n"
  5788. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5789. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  5790. verifyFormat(
  5791. "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5792. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5793. " 1, 22, 333, 4444, 55555, 666666, // comment\n"
  5794. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  5795. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  5796. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  5797. " 7777777};");
  5798. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  5799. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  5800. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  5801. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  5802. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  5803. " // Separating comment.\n"
  5804. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  5805. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  5806. " // Leading comment\n"
  5807. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  5808. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  5809. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  5810. " 1, 1, 1, 1};",
  5811. getLLVMStyleWithColumns(39));
  5812. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  5813. " 1, 1, 1, 1};",
  5814. getLLVMStyleWithColumns(38));
  5815. verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
  5816. " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
  5817. getLLVMStyleWithColumns(43));
  5818. verifyFormat(
  5819. "static unsigned SomeValues[10][3] = {\n"
  5820. " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n"
  5821. " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
  5822. verifyFormat("static auto fields = new vector<string>{\n"
  5823. " \"aaaaaaaaaaaaa\",\n"
  5824. " \"aaaaaaaaaaaaa\",\n"
  5825. " \"aaaaaaaaaaaa\",\n"
  5826. " \"aaaaaaaaaaaaaa\",\n"
  5827. " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
  5828. " \"aaaaaaaaaaaa\",\n"
  5829. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
  5830. "};");
  5831. verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
  5832. verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
  5833. " 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
  5834. " 3, cccccccccccccccccccccc};",
  5835. getLLVMStyleWithColumns(60));
  5836. // Trailing commas.
  5837. verifyFormat("vector<int> x = {\n"
  5838. " 1, 1, 1, 1, 1, 1, 1, 1,\n"
  5839. "};",
  5840. getLLVMStyleWithColumns(39));
  5841. verifyFormat("vector<int> x = {\n"
  5842. " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
  5843. "};",
  5844. getLLVMStyleWithColumns(39));
  5845. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  5846. " 1, 1, 1, 1,\n"
  5847. " /**/ /**/};",
  5848. getLLVMStyleWithColumns(39));
  5849. // Trailing comment in the first line.
  5850. verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n"
  5851. " 1111111111, 2222222222, 33333333333, 4444444444, //\n"
  5852. " 111111111, 222222222, 3333333333, 444444444, //\n"
  5853. " 11111111, 22222222, 333333333, 44444444};");
  5854. // With nested lists, we should either format one item per line or all nested
  5855. // lists one on line.
  5856. // FIXME: For some nested lists, we can do better.
  5857. verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
  5858. " {aaaaaaaaaaaaaaaaaaa},\n"
  5859. " {aaaaaaaaaaaaaaaaaaaaa},\n"
  5860. " {aaaaaaaaaaaaaaaaa}};",
  5861. getLLVMStyleWithColumns(60));
  5862. verifyFormat(
  5863. "SomeStruct my_struct_array = {\n"
  5864. " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
  5865. " aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
  5866. " {aaa, aaa},\n"
  5867. " {aaa, aaa},\n"
  5868. " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
  5869. " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5870. " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
  5871. // No column layout should be used here.
  5872. verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
  5873. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
  5874. verifyNoCrash("a<,");
  5875. }
  5876. TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
  5877. FormatStyle DoNotMerge = getLLVMStyle();
  5878. DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  5879. verifyFormat("void f() { return 42; }");
  5880. verifyFormat("void f() {\n"
  5881. " return 42;\n"
  5882. "}",
  5883. DoNotMerge);
  5884. verifyFormat("void f() {\n"
  5885. " // Comment\n"
  5886. "}");
  5887. verifyFormat("{\n"
  5888. "#error {\n"
  5889. " int a;\n"
  5890. "}");
  5891. verifyFormat("{\n"
  5892. " int a;\n"
  5893. "#error {\n"
  5894. "}");
  5895. verifyFormat("void f() {} // comment");
  5896. verifyFormat("void f() { int a; } // comment");
  5897. verifyFormat("void f() {\n"
  5898. "} // comment",
  5899. DoNotMerge);
  5900. verifyFormat("void f() {\n"
  5901. " int a;\n"
  5902. "} // comment",
  5903. DoNotMerge);
  5904. verifyFormat("void f() {\n"
  5905. "} // comment",
  5906. getLLVMStyleWithColumns(15));
  5907. verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
  5908. verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
  5909. verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
  5910. verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
  5911. verifyFormat("class C {\n"
  5912. " C()\n"
  5913. " : iiiiiiii(nullptr),\n"
  5914. " kkkkkkk(nullptr),\n"
  5915. " mmmmmmm(nullptr),\n"
  5916. " nnnnnnn(nullptr) {}\n"
  5917. "};",
  5918. getGoogleStyle());
  5919. FormatStyle NoColumnLimit = getLLVMStyle();
  5920. NoColumnLimit.ColumnLimit = 0;
  5921. EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
  5922. EXPECT_EQ("class C {\n"
  5923. " A() : b(0) {}\n"
  5924. "};",
  5925. format("class C{A():b(0){}};", NoColumnLimit));
  5926. EXPECT_EQ("A()\n"
  5927. " : b(0) {\n"
  5928. "}",
  5929. format("A()\n:b(0)\n{\n}", NoColumnLimit));
  5930. FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
  5931. DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
  5932. FormatStyle::SFS_None;
  5933. EXPECT_EQ("A()\n"
  5934. " : b(0) {\n"
  5935. "}",
  5936. format("A():b(0){}", DoNotMergeNoColumnLimit));
  5937. EXPECT_EQ("A()\n"
  5938. " : b(0) {\n"
  5939. "}",
  5940. format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
  5941. verifyFormat("#define A \\\n"
  5942. " void f() { \\\n"
  5943. " int i; \\\n"
  5944. " }",
  5945. getLLVMStyleWithColumns(20));
  5946. verifyFormat("#define A \\\n"
  5947. " void f() { int i; }",
  5948. getLLVMStyleWithColumns(21));
  5949. verifyFormat("#define A \\\n"
  5950. " void f() { \\\n"
  5951. " int i; \\\n"
  5952. " } \\\n"
  5953. " int j;",
  5954. getLLVMStyleWithColumns(22));
  5955. verifyFormat("#define A \\\n"
  5956. " void f() { int i; } \\\n"
  5957. " int j;",
  5958. getLLVMStyleWithColumns(23));
  5959. }
  5960. TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
  5961. FormatStyle MergeInlineOnly = getLLVMStyle();
  5962. MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  5963. verifyFormat("class C {\n"
  5964. " int f() { return 42; }\n"
  5965. "};",
  5966. MergeInlineOnly);
  5967. verifyFormat("int f() {\n"
  5968. " return 42;\n"
  5969. "}",
  5970. MergeInlineOnly);
  5971. }
  5972. TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
  5973. // Elaborate type variable declarations.
  5974. verifyFormat("struct foo a = {bar};\nint n;");
  5975. verifyFormat("class foo a = {bar};\nint n;");
  5976. verifyFormat("union foo a = {bar};\nint n;");
  5977. // Elaborate types inside function definitions.
  5978. verifyFormat("struct foo f() {}\nint n;");
  5979. verifyFormat("class foo f() {}\nint n;");
  5980. verifyFormat("union foo f() {}\nint n;");
  5981. // Templates.
  5982. verifyFormat("template <class X> void f() {}\nint n;");
  5983. verifyFormat("template <struct X> void f() {}\nint n;");
  5984. verifyFormat("template <union X> void f() {}\nint n;");
  5985. // Actual definitions...
  5986. verifyFormat("struct {\n} n;");
  5987. verifyFormat(
  5988. "template <template <class T, class Y>, class Z> class X {\n} n;");
  5989. verifyFormat("union Z {\n int n;\n} x;");
  5990. verifyFormat("class MACRO Z {\n} n;");
  5991. verifyFormat("class MACRO(X) Z {\n} n;");
  5992. verifyFormat("class __attribute__(X) Z {\n} n;");
  5993. verifyFormat("class __declspec(X) Z {\n} n;");
  5994. verifyFormat("class A##B##C {\n} n;");
  5995. verifyFormat("class alignas(16) Z {\n} n;");
  5996. verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
  5997. verifyFormat("class MACROA MACRO(X) Z {\n} n;");
  5998. // Redefinition from nested context:
  5999. verifyFormat("class A::B::C {\n} n;");
  6000. // Template definitions.
  6001. verifyFormat(
  6002. "template <typename F>\n"
  6003. "Matcher(const Matcher<F> &Other,\n"
  6004. " typename enable_if_c<is_base_of<F, T>::value &&\n"
  6005. " !is_same<F, T>::value>::type * = 0)\n"
  6006. " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
  6007. // FIXME: This is still incorrectly handled at the formatter side.
  6008. verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
  6009. verifyFormat("int i = SomeFunction(a<b, a> b);");
  6010. // FIXME:
  6011. // This now gets parsed incorrectly as class definition.
  6012. // verifyFormat("class A<int> f() {\n}\nint n;");
  6013. // Elaborate types where incorrectly parsing the structural element would
  6014. // break the indent.
  6015. verifyFormat("if (true)\n"
  6016. " class X x;\n"
  6017. "else\n"
  6018. " f();\n");
  6019. // This is simply incomplete. Formatting is not important, but must not crash.
  6020. verifyFormat("class A:");
  6021. }
  6022. TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
  6023. EXPECT_EQ("#error Leave all white!!!!! space* alone!\n",
  6024. format("#error Leave all white!!!!! space* alone!\n"));
  6025. EXPECT_EQ(
  6026. "#warning Leave all white!!!!! space* alone!\n",
  6027. format("#warning Leave all white!!!!! space* alone!\n"));
  6028. EXPECT_EQ("#error 1", format(" # error 1"));
  6029. EXPECT_EQ("#warning 1", format(" # warning 1"));
  6030. }
  6031. TEST_F(FormatTest, FormatHashIfExpressions) {
  6032. verifyFormat("#if AAAA && BBBB");
  6033. // FIXME: Come up with a better indentation for #elif.
  6034. verifyFormat(
  6035. "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
  6036. " defined(BBBBBBBB)\n"
  6037. "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
  6038. " defined(BBBBBBBB)\n"
  6039. "#endif",
  6040. getLLVMStyleWithColumns(65));
  6041. }
  6042. TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
  6043. FormatStyle AllowsMergedIf = getGoogleStyle();
  6044. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  6045. verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
  6046. verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
  6047. verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
  6048. EXPECT_EQ("if (true) return 42;",
  6049. format("if (true)\nreturn 42;", AllowsMergedIf));
  6050. FormatStyle ShortMergedIf = AllowsMergedIf;
  6051. ShortMergedIf.ColumnLimit = 25;
  6052. verifyFormat("#define A \\\n"
  6053. " if (true) return 42;",
  6054. ShortMergedIf);
  6055. verifyFormat("#define A \\\n"
  6056. " f(); \\\n"
  6057. " if (true)\n"
  6058. "#define B",
  6059. ShortMergedIf);
  6060. verifyFormat("#define A \\\n"
  6061. " f(); \\\n"
  6062. " if (true)\n"
  6063. "g();",
  6064. ShortMergedIf);
  6065. verifyFormat("{\n"
  6066. "#ifdef A\n"
  6067. " // Comment\n"
  6068. " if (true) continue;\n"
  6069. "#endif\n"
  6070. " // Comment\n"
  6071. " if (true) continue;\n"
  6072. "}",
  6073. ShortMergedIf);
  6074. ShortMergedIf.ColumnLimit = 29;
  6075. verifyFormat("#define A \\\n"
  6076. " if (aaaaaaaaaa) return 1; \\\n"
  6077. " return 2;",
  6078. ShortMergedIf);
  6079. ShortMergedIf.ColumnLimit = 28;
  6080. verifyFormat("#define A \\\n"
  6081. " if (aaaaaaaaaa) \\\n"
  6082. " return 1; \\\n"
  6083. " return 2;",
  6084. ShortMergedIf);
  6085. }
  6086. TEST_F(FormatTest, BlockCommentsInControlLoops) {
  6087. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  6088. " f();\n"
  6089. "}");
  6090. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  6091. " f();\n"
  6092. "} /* another comment */ else /* comment #3 */ {\n"
  6093. " g();\n"
  6094. "}");
  6095. verifyFormat("while (0) /* a comment in a strange place */ {\n"
  6096. " f();\n"
  6097. "}");
  6098. verifyFormat("for (;;) /* a comment in a strange place */ {\n"
  6099. " f();\n"
  6100. "}");
  6101. verifyFormat("do /* a comment in a strange place */ {\n"
  6102. " f();\n"
  6103. "} /* another comment */ while (0);");
  6104. }
  6105. TEST_F(FormatTest, BlockComments) {
  6106. EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
  6107. format("/* *//* */ /* */\n/* *//* */ /* */"));
  6108. EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
  6109. EXPECT_EQ("#define A /*123*/ \\\n"
  6110. " b\n"
  6111. "/* */\n"
  6112. "someCall(\n"
  6113. " parameter);",
  6114. format("#define A /*123*/ b\n"
  6115. "/* */\n"
  6116. "someCall(parameter);",
  6117. getLLVMStyleWithColumns(15)));
  6118. EXPECT_EQ("#define A\n"
  6119. "/* */ someCall(\n"
  6120. " parameter);",
  6121. format("#define A\n"
  6122. "/* */someCall(parameter);",
  6123. getLLVMStyleWithColumns(15)));
  6124. EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
  6125. EXPECT_EQ("/*\n"
  6126. "*\n"
  6127. " * aaaaaa\n"
  6128. " * aaaaaa\n"
  6129. "*/",
  6130. format("/*\n"
  6131. "*\n"
  6132. " * aaaaaa aaaaaa\n"
  6133. "*/",
  6134. getLLVMStyleWithColumns(10)));
  6135. EXPECT_EQ("/*\n"
  6136. "**\n"
  6137. "* aaaaaa\n"
  6138. "*aaaaaa\n"
  6139. "*/",
  6140. format("/*\n"
  6141. "**\n"
  6142. "* aaaaaa aaaaaa\n"
  6143. "*/",
  6144. getLLVMStyleWithColumns(10)));
  6145. FormatStyle NoBinPacking = getLLVMStyle();
  6146. NoBinPacking.BinPackParameters = false;
  6147. EXPECT_EQ("someFunction(1, /* comment 1 */\n"
  6148. " 2, /* comment 2 */\n"
  6149. " 3, /* comment 3 */\n"
  6150. " aaaa,\n"
  6151. " bbbb);",
  6152. format("someFunction (1, /* comment 1 */\n"
  6153. " 2, /* comment 2 */ \n"
  6154. " 3, /* comment 3 */\n"
  6155. "aaaa, bbbb );",
  6156. NoBinPacking));
  6157. verifyFormat(
  6158. "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  6159. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  6160. EXPECT_EQ(
  6161. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  6162. " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  6163. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
  6164. format(
  6165. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  6166. " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  6167. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
  6168. EXPECT_EQ(
  6169. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  6170. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  6171. "int cccccccccccccccccccccccccccccc; /* comment */\n",
  6172. format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  6173. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  6174. "int cccccccccccccccccccccccccccccc; /* comment */\n"));
  6175. verifyFormat("void f(int * /* unused */) {}");
  6176. EXPECT_EQ("/*\n"
  6177. " **\n"
  6178. " */",
  6179. format("/*\n"
  6180. " **\n"
  6181. " */"));
  6182. EXPECT_EQ("/*\n"
  6183. " *q\n"
  6184. " */",
  6185. format("/*\n"
  6186. " *q\n"
  6187. " */"));
  6188. EXPECT_EQ("/*\n"
  6189. " * q\n"
  6190. " */",
  6191. format("/*\n"
  6192. " * q\n"
  6193. " */"));
  6194. EXPECT_EQ("/*\n"
  6195. " **/",
  6196. format("/*\n"
  6197. " **/"));
  6198. EXPECT_EQ("/*\n"
  6199. " ***/",
  6200. format("/*\n"
  6201. " ***/"));
  6202. }
  6203. TEST_F(FormatTest, BlockCommentsInMacros) {
  6204. EXPECT_EQ("#define A \\\n"
  6205. " { \\\n"
  6206. " /* one line */ \\\n"
  6207. " someCall();",
  6208. format("#define A { \\\n"
  6209. " /* one line */ \\\n"
  6210. " someCall();",
  6211. getLLVMStyleWithColumns(20)));
  6212. EXPECT_EQ("#define A \\\n"
  6213. " { \\\n"
  6214. " /* previous */ \\\n"
  6215. " /* one line */ \\\n"
  6216. " someCall();",
  6217. format("#define A { \\\n"
  6218. " /* previous */ \\\n"
  6219. " /* one line */ \\\n"
  6220. " someCall();",
  6221. getLLVMStyleWithColumns(20)));
  6222. }
  6223. TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
  6224. EXPECT_EQ("a = {\n"
  6225. " 1111 /* */\n"
  6226. "};",
  6227. format("a = {1111 /* */\n"
  6228. "};",
  6229. getLLVMStyleWithColumns(15)));
  6230. EXPECT_EQ("a = {\n"
  6231. " 1111 /* */\n"
  6232. "};",
  6233. format("a = {1111 /* */\n"
  6234. "};",
  6235. getLLVMStyleWithColumns(15)));
  6236. // FIXME: The formatting is still wrong here.
  6237. EXPECT_EQ("a = {\n"
  6238. " 1111 /* a\n"
  6239. " */\n"
  6240. "};",
  6241. format("a = {1111 /* a */\n"
  6242. "};",
  6243. getLLVMStyleWithColumns(15)));
  6244. }
  6245. TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
  6246. // FIXME: This is not what we want...
  6247. verifyFormat("{\n"
  6248. "// a"
  6249. "// b");
  6250. }
  6251. TEST_F(FormatTest, FormatStarDependingOnContext) {
  6252. verifyFormat("void f(int *a);");
  6253. verifyFormat("void f() { f(fint * b); }");
  6254. verifyFormat("class A {\n void f(int *a);\n};");
  6255. verifyFormat("class A {\n int *a;\n};");
  6256. verifyFormat("namespace a {\n"
  6257. "namespace b {\n"
  6258. "class A {\n"
  6259. " void f() {}\n"
  6260. " int *a;\n"
  6261. "};\n"
  6262. "}\n"
  6263. "}");
  6264. }
  6265. TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
  6266. verifyFormat("while");
  6267. verifyFormat("operator");
  6268. }
  6269. //===----------------------------------------------------------------------===//
  6270. // Objective-C tests.
  6271. //===----------------------------------------------------------------------===//
  6272. TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
  6273. verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
  6274. EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
  6275. format("-(NSUInteger)indexOfObject:(id)anObject;"));
  6276. EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
  6277. EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
  6278. EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
  6279. format("-(NSInteger)Method3:(id)anObject;"));
  6280. EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
  6281. format("-(NSInteger)Method4:(id)anObject;"));
  6282. EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
  6283. format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
  6284. EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
  6285. format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
  6286. EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
  6287. "forAllCells:(BOOL)flag;",
  6288. format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
  6289. "forAllCells:(BOOL)flag;"));
  6290. // Very long objectiveC method declaration.
  6291. verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
  6292. " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
  6293. verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
  6294. " inRange:(NSRange)range\n"
  6295. " outRange:(NSRange)out_range\n"
  6296. " outRange1:(NSRange)out_range1\n"
  6297. " outRange2:(NSRange)out_range2\n"
  6298. " outRange3:(NSRange)out_range3\n"
  6299. " outRange4:(NSRange)out_range4\n"
  6300. " outRange5:(NSRange)out_range5\n"
  6301. " outRange6:(NSRange)out_range6\n"
  6302. " outRange7:(NSRange)out_range7\n"
  6303. " outRange8:(NSRange)out_range8\n"
  6304. " outRange9:(NSRange)out_range9;");
  6305. // When the function name has to be wrapped.
  6306. FormatStyle Style = getLLVMStyle();
  6307. Style.IndentWrappedFunctionNames = false;
  6308. verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
  6309. "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
  6310. " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
  6311. "}",
  6312. Style);
  6313. Style.IndentWrappedFunctionNames = true;
  6314. verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
  6315. " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
  6316. " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
  6317. "}",
  6318. Style);
  6319. verifyFormat("- (int)sum:(vector<int>)numbers;");
  6320. verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
  6321. // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
  6322. // protocol lists (but not for template classes):
  6323. // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
  6324. verifyFormat("- (int (*)())foo:(int (*)())f;");
  6325. verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
  6326. // If there's no return type (very rare in practice!), LLVM and Google style
  6327. // agree.
  6328. verifyFormat("- foo;");
  6329. verifyFormat("- foo:(int)f;");
  6330. verifyGoogleFormat("- foo:(int)foo;");
  6331. }
  6332. TEST_F(FormatTest, FormatObjCInterface) {
  6333. verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
  6334. "@public\n"
  6335. " int field1;\n"
  6336. "@protected\n"
  6337. " int field2;\n"
  6338. "@private\n"
  6339. " int field3;\n"
  6340. "@package\n"
  6341. " int field4;\n"
  6342. "}\n"
  6343. "+ (id)init;\n"
  6344. "@end");
  6345. verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
  6346. " @public\n"
  6347. " int field1;\n"
  6348. " @protected\n"
  6349. " int field2;\n"
  6350. " @private\n"
  6351. " int field3;\n"
  6352. " @package\n"
  6353. " int field4;\n"
  6354. "}\n"
  6355. "+ (id)init;\n"
  6356. "@end");
  6357. verifyFormat("@interface /* wait for it */ Foo\n"
  6358. "+ (id)init;\n"
  6359. "// Look, a comment!\n"
  6360. "- (int)answerWith:(int)i;\n"
  6361. "@end");
  6362. verifyFormat("@interface Foo\n"
  6363. "@end\n"
  6364. "@interface Bar\n"
  6365. "@end");
  6366. verifyFormat("@interface Foo : Bar\n"
  6367. "+ (id)init;\n"
  6368. "@end");
  6369. verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
  6370. "+ (id)init;\n"
  6371. "@end");
  6372. verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
  6373. "+ (id)init;\n"
  6374. "@end");
  6375. verifyFormat("@interface Foo (HackStuff)\n"
  6376. "+ (id)init;\n"
  6377. "@end");
  6378. verifyFormat("@interface Foo ()\n"
  6379. "+ (id)init;\n"
  6380. "@end");
  6381. verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
  6382. "+ (id)init;\n"
  6383. "@end");
  6384. verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
  6385. "+ (id)init;\n"
  6386. "@end");
  6387. verifyFormat("@interface Foo {\n"
  6388. " int _i;\n"
  6389. "}\n"
  6390. "+ (id)init;\n"
  6391. "@end");
  6392. verifyFormat("@interface Foo : Bar {\n"
  6393. " int _i;\n"
  6394. "}\n"
  6395. "+ (id)init;\n"
  6396. "@end");
  6397. verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
  6398. " int _i;\n"
  6399. "}\n"
  6400. "+ (id)init;\n"
  6401. "@end");
  6402. verifyFormat("@interface Foo (HackStuff) {\n"
  6403. " int _i;\n"
  6404. "}\n"
  6405. "+ (id)init;\n"
  6406. "@end");
  6407. verifyFormat("@interface Foo () {\n"
  6408. " int _i;\n"
  6409. "}\n"
  6410. "+ (id)init;\n"
  6411. "@end");
  6412. verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
  6413. " int _i;\n"
  6414. "}\n"
  6415. "+ (id)init;\n"
  6416. "@end");
  6417. FormatStyle OnePerLine = getGoogleStyle();
  6418. OnePerLine.BinPackParameters = false;
  6419. verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n"
  6420. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6421. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6422. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6423. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
  6424. "}",
  6425. OnePerLine);
  6426. }
  6427. TEST_F(FormatTest, FormatObjCImplementation) {
  6428. verifyFormat("@implementation Foo : NSObject {\n"
  6429. "@public\n"
  6430. " int field1;\n"
  6431. "@protected\n"
  6432. " int field2;\n"
  6433. "@private\n"
  6434. " int field3;\n"
  6435. "@package\n"
  6436. " int field4;\n"
  6437. "}\n"
  6438. "+ (id)init {\n}\n"
  6439. "@end");
  6440. verifyGoogleFormat("@implementation Foo : NSObject {\n"
  6441. " @public\n"
  6442. " int field1;\n"
  6443. " @protected\n"
  6444. " int field2;\n"
  6445. " @private\n"
  6446. " int field3;\n"
  6447. " @package\n"
  6448. " int field4;\n"
  6449. "}\n"
  6450. "+ (id)init {\n}\n"
  6451. "@end");
  6452. verifyFormat("@implementation Foo\n"
  6453. "+ (id)init {\n"
  6454. " if (true)\n"
  6455. " return nil;\n"
  6456. "}\n"
  6457. "// Look, a comment!\n"
  6458. "- (int)answerWith:(int)i {\n"
  6459. " return i;\n"
  6460. "}\n"
  6461. "+ (int)answerWith:(int)i {\n"
  6462. " return i;\n"
  6463. "}\n"
  6464. "@end");
  6465. verifyFormat("@implementation Foo\n"
  6466. "@end\n"
  6467. "@implementation Bar\n"
  6468. "@end");
  6469. EXPECT_EQ("@implementation Foo : Bar\n"
  6470. "+ (id)init {\n}\n"
  6471. "- (void)foo {\n}\n"
  6472. "@end",
  6473. format("@implementation Foo : Bar\n"
  6474. "+(id)init{}\n"
  6475. "-(void)foo{}\n"
  6476. "@end"));
  6477. verifyFormat("@implementation Foo {\n"
  6478. " int _i;\n"
  6479. "}\n"
  6480. "+ (id)init {\n}\n"
  6481. "@end");
  6482. verifyFormat("@implementation Foo : Bar {\n"
  6483. " int _i;\n"
  6484. "}\n"
  6485. "+ (id)init {\n}\n"
  6486. "@end");
  6487. verifyFormat("@implementation Foo (HackStuff)\n"
  6488. "+ (id)init {\n}\n"
  6489. "@end");
  6490. verifyFormat("@implementation ObjcClass\n"
  6491. "- (void)method;\n"
  6492. "{}\n"
  6493. "@end");
  6494. }
  6495. TEST_F(FormatTest, FormatObjCProtocol) {
  6496. verifyFormat("@protocol Foo\n"
  6497. "@property(weak) id delegate;\n"
  6498. "- (NSUInteger)numberOfThings;\n"
  6499. "@end");
  6500. verifyFormat("@protocol MyProtocol <NSObject>\n"
  6501. "- (NSUInteger)numberOfThings;\n"
  6502. "@end");
  6503. verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
  6504. "- (NSUInteger)numberOfThings;\n"
  6505. "@end");
  6506. verifyFormat("@protocol Foo;\n"
  6507. "@protocol Bar;\n");
  6508. verifyFormat("@protocol Foo\n"
  6509. "@end\n"
  6510. "@protocol Bar\n"
  6511. "@end");
  6512. verifyFormat("@protocol myProtocol\n"
  6513. "- (void)mandatoryWithInt:(int)i;\n"
  6514. "@optional\n"
  6515. "- (void)optional;\n"
  6516. "@required\n"
  6517. "- (void)required;\n"
  6518. "@optional\n"
  6519. "@property(assign) int madProp;\n"
  6520. "@end\n");
  6521. verifyFormat("@property(nonatomic, assign, readonly)\n"
  6522. " int *looooooooooooooooooooooooooooongNumber;\n"
  6523. "@property(nonatomic, assign, readonly)\n"
  6524. " NSString *looooooooooooooooooooooooooooongName;");
  6525. verifyFormat("@implementation PR18406\n"
  6526. "}\n"
  6527. "@end");
  6528. }
  6529. TEST_F(FormatTest, FormatObjCMethodDeclarations) {
  6530. verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
  6531. " rect:(NSRect)theRect\n"
  6532. " interval:(float)theInterval {\n"
  6533. "}");
  6534. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6535. " longKeyword:(NSRect)theRect\n"
  6536. " evenLongerKeyword:(float)theInterval\n"
  6537. " error:(NSError **)theError {\n"
  6538. "}");
  6539. verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
  6540. " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
  6541. " NS_DESIGNATED_INITIALIZER;",
  6542. getLLVMStyleWithColumns(60));
  6543. }
  6544. TEST_F(FormatTest, FormatObjCMethodExpr) {
  6545. verifyFormat("[foo bar:baz];");
  6546. verifyFormat("return [foo bar:baz];");
  6547. verifyFormat("return (a)[foo bar:baz];");
  6548. verifyFormat("f([foo bar:baz]);");
  6549. verifyFormat("f(2, [foo bar:baz]);");
  6550. verifyFormat("f(2, a ? b : c);");
  6551. verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
  6552. // Unary operators.
  6553. verifyFormat("int a = +[foo bar:baz];");
  6554. verifyFormat("int a = -[foo bar:baz];");
  6555. verifyFormat("int a = ![foo bar:baz];");
  6556. verifyFormat("int a = ~[foo bar:baz];");
  6557. verifyFormat("int a = ++[foo bar:baz];");
  6558. verifyFormat("int a = --[foo bar:baz];");
  6559. verifyFormat("int a = sizeof [foo bar:baz];");
  6560. verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
  6561. verifyFormat("int a = &[foo bar:baz];");
  6562. verifyFormat("int a = *[foo bar:baz];");
  6563. // FIXME: Make casts work, without breaking f()[4].
  6564. // verifyFormat("int a = (int)[foo bar:baz];");
  6565. // verifyFormat("return (int)[foo bar:baz];");
  6566. // verifyFormat("(void)[foo bar:baz];");
  6567. verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
  6568. // Binary operators.
  6569. verifyFormat("[foo bar:baz], [foo bar:baz];");
  6570. verifyFormat("[foo bar:baz] = [foo bar:baz];");
  6571. verifyFormat("[foo bar:baz] *= [foo bar:baz];");
  6572. verifyFormat("[foo bar:baz] /= [foo bar:baz];");
  6573. verifyFormat("[foo bar:baz] %= [foo bar:baz];");
  6574. verifyFormat("[foo bar:baz] += [foo bar:baz];");
  6575. verifyFormat("[foo bar:baz] -= [foo bar:baz];");
  6576. verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
  6577. verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
  6578. verifyFormat("[foo bar:baz] &= [foo bar:baz];");
  6579. verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
  6580. verifyFormat("[foo bar:baz] |= [foo bar:baz];");
  6581. verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
  6582. verifyFormat("[foo bar:baz] || [foo bar:baz];");
  6583. verifyFormat("[foo bar:baz] && [foo bar:baz];");
  6584. verifyFormat("[foo bar:baz] | [foo bar:baz];");
  6585. verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
  6586. verifyFormat("[foo bar:baz] & [foo bar:baz];");
  6587. verifyFormat("[foo bar:baz] == [foo bar:baz];");
  6588. verifyFormat("[foo bar:baz] != [foo bar:baz];");
  6589. verifyFormat("[foo bar:baz] >= [foo bar:baz];");
  6590. verifyFormat("[foo bar:baz] <= [foo bar:baz];");
  6591. verifyFormat("[foo bar:baz] > [foo bar:baz];");
  6592. verifyFormat("[foo bar:baz] < [foo bar:baz];");
  6593. verifyFormat("[foo bar:baz] >> [foo bar:baz];");
  6594. verifyFormat("[foo bar:baz] << [foo bar:baz];");
  6595. verifyFormat("[foo bar:baz] - [foo bar:baz];");
  6596. verifyFormat("[foo bar:baz] + [foo bar:baz];");
  6597. verifyFormat("[foo bar:baz] * [foo bar:baz];");
  6598. verifyFormat("[foo bar:baz] / [foo bar:baz];");
  6599. verifyFormat("[foo bar:baz] % [foo bar:baz];");
  6600. // Whew!
  6601. verifyFormat("return in[42];");
  6602. verifyFormat("for (auto v : in[1]) {\n}");
  6603. verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
  6604. verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
  6605. verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
  6606. verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
  6607. verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
  6608. verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
  6609. "}");
  6610. verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
  6611. verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
  6612. verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
  6613. verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
  6614. verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
  6615. verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
  6616. verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
  6617. verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
  6618. verifyFormat("[button setAction:@selector(zoomOut:)];");
  6619. verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
  6620. verifyFormat("arr[[self indexForFoo:a]];");
  6621. verifyFormat("throw [self errorFor:a];");
  6622. verifyFormat("@throw [self errorFor:a];");
  6623. verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
  6624. verifyFormat("[(id)foo bar:(id) ? baz : quux];");
  6625. verifyFormat("4 > 4 ? (id)a : (id)baz;");
  6626. // This tests that the formatter doesn't break after "backing" but before ":",
  6627. // which would be at 80 columns.
  6628. verifyFormat(
  6629. "void f() {\n"
  6630. " if ((self = [super initWithContentRect:contentRect\n"
  6631. " styleMask:styleMask ?: otherMask\n"
  6632. " backing:NSBackingStoreBuffered\n"
  6633. " defer:YES]))");
  6634. verifyFormat(
  6635. "[foo checkThatBreakingAfterColonWorksOk:\n"
  6636. " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
  6637. verifyFormat("[myObj short:arg1 // Force line break\n"
  6638. " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
  6639. " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
  6640. " error:arg4];");
  6641. verifyFormat(
  6642. "void f() {\n"
  6643. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  6644. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  6645. " pos.width(), pos.height())\n"
  6646. " styleMask:NSBorderlessWindowMask\n"
  6647. " backing:NSBackingStoreBuffered\n"
  6648. " defer:NO]);\n"
  6649. "}");
  6650. verifyFormat(
  6651. "void f() {\n"
  6652. " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
  6653. " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
  6654. " pos.width(), pos.height())\n"
  6655. " syeMask:NSBorderlessWindowMask\n"
  6656. " bking:NSBackingStoreBuffered\n"
  6657. " der:NO]);\n"
  6658. "}",
  6659. getLLVMStyleWithColumns(70));
  6660. verifyFormat(
  6661. "void f() {\n"
  6662. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  6663. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  6664. " pos.width(), pos.height())\n"
  6665. " styleMask:NSBorderlessWindowMask\n"
  6666. " backing:NSBackingStoreBuffered\n"
  6667. " defer:NO]);\n"
  6668. "}",
  6669. getChromiumStyle(FormatStyle::LK_Cpp));
  6670. verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
  6671. " with:contentsNativeView];");
  6672. verifyFormat(
  6673. "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
  6674. " owner:nillllll];");
  6675. verifyFormat(
  6676. "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
  6677. " forType:kBookmarkButtonDragType];");
  6678. verifyFormat("[defaultCenter addObserver:self\n"
  6679. " selector:@selector(willEnterFullscreen)\n"
  6680. " name:kWillEnterFullscreenNotification\n"
  6681. " object:nil];");
  6682. verifyFormat("[image_rep drawInRect:drawRect\n"
  6683. " fromRect:NSZeroRect\n"
  6684. " operation:NSCompositeCopy\n"
  6685. " fraction:1.0\n"
  6686. " respectFlipped:NO\n"
  6687. " hints:nil];");
  6688. verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  6689. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  6690. verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  6691. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  6692. verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
  6693. " aaaaaaaaaaaaaaaaaaaaaa];");
  6694. verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
  6695. " .aaaaaaaa];", // FIXME: Indentation seems off.
  6696. getLLVMStyleWithColumns(60));
  6697. verifyFormat(
  6698. "scoped_nsobject<NSTextField> message(\n"
  6699. " // The frame will be fixed up when |-setMessageText:| is called.\n"
  6700. " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
  6701. verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
  6702. " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
  6703. " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
  6704. " aaaa:bbb];");
  6705. verifyFormat("[self param:function( //\n"
  6706. " parameter)]");
  6707. verifyFormat(
  6708. "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  6709. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  6710. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
  6711. // FIXME: This violates the column limit.
  6712. verifyFormat(
  6713. "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
  6714. " aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
  6715. " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];",
  6716. getLLVMStyleWithColumns(60));
  6717. // Variadic parameters.
  6718. verifyFormat(
  6719. "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
  6720. verifyFormat(
  6721. "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  6722. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  6723. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
  6724. verifyFormat("[self // break\n"
  6725. " a:a\n"
  6726. " aaa:aaa];");
  6727. verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
  6728. " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
  6729. }
  6730. TEST_F(FormatTest, ObjCAt) {
  6731. verifyFormat("@autoreleasepool");
  6732. verifyFormat("@catch");
  6733. verifyFormat("@class");
  6734. verifyFormat("@compatibility_alias");
  6735. verifyFormat("@defs");
  6736. verifyFormat("@dynamic");
  6737. verifyFormat("@encode");
  6738. verifyFormat("@end");
  6739. verifyFormat("@finally");
  6740. verifyFormat("@implementation");
  6741. verifyFormat("@import");
  6742. verifyFormat("@interface");
  6743. verifyFormat("@optional");
  6744. verifyFormat("@package");
  6745. verifyFormat("@private");
  6746. verifyFormat("@property");
  6747. verifyFormat("@protected");
  6748. verifyFormat("@protocol");
  6749. verifyFormat("@public");
  6750. verifyFormat("@required");
  6751. verifyFormat("@selector");
  6752. verifyFormat("@synchronized");
  6753. verifyFormat("@synthesize");
  6754. verifyFormat("@throw");
  6755. verifyFormat("@try");
  6756. EXPECT_EQ("@interface", format("@ interface"));
  6757. // The precise formatting of this doesn't matter, nobody writes code like
  6758. // this.
  6759. verifyFormat("@ /*foo*/ interface");
  6760. }
  6761. TEST_F(FormatTest, ObjCSnippets) {
  6762. verifyFormat("@autoreleasepool {\n"
  6763. " foo();\n"
  6764. "}");
  6765. verifyFormat("@class Foo, Bar;");
  6766. verifyFormat("@compatibility_alias AliasName ExistingClass;");
  6767. verifyFormat("@dynamic textColor;");
  6768. verifyFormat("char *buf1 = @encode(int *);");
  6769. verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
  6770. verifyFormat("char *buf1 = @encode(int **);");
  6771. verifyFormat("Protocol *proto = @protocol(p1);");
  6772. verifyFormat("SEL s = @selector(foo:);");
  6773. verifyFormat("@synchronized(self) {\n"
  6774. " f();\n"
  6775. "}");
  6776. verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  6777. verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  6778. verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
  6779. verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
  6780. verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
  6781. verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
  6782. getMozillaStyle());
  6783. verifyFormat("@property BOOL editable;", getMozillaStyle());
  6784. verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
  6785. getWebKitStyle());
  6786. verifyFormat("@property BOOL editable;", getWebKitStyle());
  6787. verifyFormat("@import foo.bar;\n"
  6788. "@import baz;");
  6789. }
  6790. TEST_F(FormatTest, ObjCLiterals) {
  6791. verifyFormat("@\"String\"");
  6792. verifyFormat("@1");
  6793. verifyFormat("@+4.8");
  6794. verifyFormat("@-4");
  6795. verifyFormat("@1LL");
  6796. verifyFormat("@.5");
  6797. verifyFormat("@'c'");
  6798. verifyFormat("@true");
  6799. verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
  6800. verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
  6801. verifyFormat("NSNumber *favoriteColor = @(Green);");
  6802. verifyFormat("NSString *path = @(getenv(\"PATH\"));");
  6803. verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
  6804. }
  6805. TEST_F(FormatTest, ObjCDictLiterals) {
  6806. verifyFormat("@{");
  6807. verifyFormat("@{}");
  6808. verifyFormat("@{@\"one\" : @1}");
  6809. verifyFormat("return @{@\"one\" : @1;");
  6810. verifyFormat("@{@\"one\" : @1}");
  6811. verifyFormat("@{@\"one\" : @{@2 : @1}}");
  6812. verifyFormat("@{\n"
  6813. " @\"one\" : @{@2 : @1},\n"
  6814. "}");
  6815. verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
  6816. verifyIncompleteFormat("[self setDict:@{}");
  6817. verifyIncompleteFormat("[self setDict:@{@1 : @2}");
  6818. verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
  6819. verifyFormat(
  6820. "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
  6821. verifyFormat(
  6822. "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
  6823. verifyFormat("NSDictionary *d = @{\n"
  6824. " @\"nam\" : NSUserNam(),\n"
  6825. " @\"dte\" : [NSDate date],\n"
  6826. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  6827. "};");
  6828. verifyFormat(
  6829. "@{\n"
  6830. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  6831. "regularFont,\n"
  6832. "};");
  6833. verifyGoogleFormat(
  6834. "@{\n"
  6835. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  6836. "regularFont,\n"
  6837. "};");
  6838. verifyFormat(
  6839. "@{\n"
  6840. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
  6841. " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
  6842. "};");
  6843. // We should try to be robust in case someone forgets the "@".
  6844. verifyFormat("NSDictionary *d = {\n"
  6845. " @\"nam\" : NSUserNam(),\n"
  6846. " @\"dte\" : [NSDate date],\n"
  6847. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  6848. "};");
  6849. verifyFormat("NSMutableDictionary *dictionary =\n"
  6850. " [NSMutableDictionary dictionaryWithDictionary:@{\n"
  6851. " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
  6852. " bbbbbbbbbbbbbbbbbb : bbbbb,\n"
  6853. " cccccccccccccccc : ccccccccccccccc\n"
  6854. " }];");
  6855. }
  6856. TEST_F(FormatTest, ObjCArrayLiterals) {
  6857. verifyIncompleteFormat("@[");
  6858. verifyFormat("@[]");
  6859. verifyFormat(
  6860. "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
  6861. verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
  6862. verifyFormat("NSArray *array = @[ [foo description] ];");
  6863. verifyFormat(
  6864. "NSArray *some_variable = @[\n"
  6865. " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
  6866. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6867. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6868. " @\"aaaaaaaaaaaaaaaaa\"\n"
  6869. "];");
  6870. verifyFormat("NSArray *some_variable = @[\n"
  6871. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6872. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6873. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6874. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6875. "];");
  6876. verifyGoogleFormat("NSArray *some_variable = @[\n"
  6877. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6878. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6879. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6880. " @\"aaaaaaaaaaaaaaaaa\"\n"
  6881. "];");
  6882. verifyFormat("NSArray *array = @[\n"
  6883. " @\"a\",\n"
  6884. " @\"a\",\n" // Trailing comma -> one per line.
  6885. "];");
  6886. // We should try to be robust in case someone forgets the "@".
  6887. verifyFormat("NSArray *some_variable = [\n"
  6888. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6889. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6890. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6891. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6892. "];");
  6893. verifyFormat(
  6894. "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
  6895. " index:(NSUInteger)index\n"
  6896. " nonDigitAttributes:\n"
  6897. " (NSDictionary *)noDigitAttributes;");
  6898. verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
  6899. " NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
  6900. "]];");
  6901. }
  6902. TEST_F(FormatTest, BreaksStringLiterals) {
  6903. EXPECT_EQ("\"some text \"\n"
  6904. "\"other\";",
  6905. format("\"some text other\";", getLLVMStyleWithColumns(12)));
  6906. EXPECT_EQ("\"some text \"\n"
  6907. "\"other\";",
  6908. format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
  6909. EXPECT_EQ(
  6910. "#define A \\\n"
  6911. " \"some \" \\\n"
  6912. " \"text \" \\\n"
  6913. " \"other\";",
  6914. format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
  6915. EXPECT_EQ(
  6916. "#define A \\\n"
  6917. " \"so \" \\\n"
  6918. " \"text \" \\\n"
  6919. " \"other\";",
  6920. format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
  6921. EXPECT_EQ("\"some text\"",
  6922. format("\"some text\"", getLLVMStyleWithColumns(1)));
  6923. EXPECT_EQ("\"some text\"",
  6924. format("\"some text\"", getLLVMStyleWithColumns(11)));
  6925. EXPECT_EQ("\"some \"\n"
  6926. "\"text\"",
  6927. format("\"some text\"", getLLVMStyleWithColumns(10)));
  6928. EXPECT_EQ("\"some \"\n"
  6929. "\"text\"",
  6930. format("\"some text\"", getLLVMStyleWithColumns(7)));
  6931. EXPECT_EQ("\"some\"\n"
  6932. "\" tex\"\n"
  6933. "\"t\"",
  6934. format("\"some text\"", getLLVMStyleWithColumns(6)));
  6935. EXPECT_EQ("\"some\"\n"
  6936. "\" tex\"\n"
  6937. "\" and\"",
  6938. format("\"some tex and\"", getLLVMStyleWithColumns(6)));
  6939. EXPECT_EQ("\"some\"\n"
  6940. "\"/tex\"\n"
  6941. "\"/and\"",
  6942. format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
  6943. EXPECT_EQ("variable =\n"
  6944. " \"long string \"\n"
  6945. " \"literal\";",
  6946. format("variable = \"long string literal\";",
  6947. getLLVMStyleWithColumns(20)));
  6948. EXPECT_EQ("variable = f(\n"
  6949. " \"long string \"\n"
  6950. " \"literal\",\n"
  6951. " short,\n"
  6952. " loooooooooooooooooooong);",
  6953. format("variable = f(\"long string literal\", short, "
  6954. "loooooooooooooooooooong);",
  6955. getLLVMStyleWithColumns(20)));
  6956. EXPECT_EQ(
  6957. "f(g(\"long string \"\n"
  6958. " \"literal\"),\n"
  6959. " b);",
  6960. format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
  6961. EXPECT_EQ("f(g(\"long string \"\n"
  6962. " \"literal\",\n"
  6963. " a),\n"
  6964. " b);",
  6965. format("f(g(\"long string literal\", a), b);",
  6966. getLLVMStyleWithColumns(20)));
  6967. EXPECT_EQ(
  6968. "f(\"one two\".split(\n"
  6969. " variable));",
  6970. format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
  6971. EXPECT_EQ("f(\"one two three four five six \"\n"
  6972. " \"seven\".split(\n"
  6973. " really_looooong_variable));",
  6974. format("f(\"one two three four five six seven\"."
  6975. "split(really_looooong_variable));",
  6976. getLLVMStyleWithColumns(33)));
  6977. EXPECT_EQ("f(\"some \"\n"
  6978. " \"text\",\n"
  6979. " other);",
  6980. format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
  6981. // Only break as a last resort.
  6982. verifyFormat(
  6983. "aaaaaaaaaaaaaaaaaaaa(\n"
  6984. " aaaaaaaaaaaaaaaaaaaa,\n"
  6985. " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
  6986. EXPECT_EQ("\"splitmea\"\n"
  6987. "\"trandomp\"\n"
  6988. "\"oint\"",
  6989. format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
  6990. EXPECT_EQ("\"split/\"\n"
  6991. "\"pathat/\"\n"
  6992. "\"slashes\"",
  6993. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  6994. EXPECT_EQ("\"split/\"\n"
  6995. "\"pathat/\"\n"
  6996. "\"slashes\"",
  6997. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  6998. EXPECT_EQ("\"split at \"\n"
  6999. "\"spaces/at/\"\n"
  7000. "\"slashes.at.any$\"\n"
  7001. "\"non-alphanumeric%\"\n"
  7002. "\"1111111111characte\"\n"
  7003. "\"rs\"",
  7004. format("\"split at "
  7005. "spaces/at/"
  7006. "slashes.at."
  7007. "any$non-"
  7008. "alphanumeric%"
  7009. "1111111111characte"
  7010. "rs\"",
  7011. getLLVMStyleWithColumns(20)));
  7012. // Verify that splitting the strings understands
  7013. // Style::AlwaysBreakBeforeMultilineStrings.
  7014. EXPECT_EQ(
  7015. "aaaaaaaaaaaa(\n"
  7016. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
  7017. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
  7018. format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
  7019. "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
  7020. "aaaaaaaaaaaaaaaaaaaaaa\");",
  7021. getGoogleStyle()));
  7022. EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  7023. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
  7024. format("return \"aaaaaaaaaaaaaaaaaaaaaa "
  7025. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
  7026. "aaaaaaaaaaaaaaaaaaaaaa\";",
  7027. getGoogleStyle()));
  7028. EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  7029. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  7030. format("llvm::outs() << "
  7031. "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
  7032. "aaaaaaaaaaaaaaaaaaa\";"));
  7033. EXPECT_EQ("ffff(\n"
  7034. " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  7035. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  7036. format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  7037. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  7038. getGoogleStyle()));
  7039. FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
  7040. AlignLeft.AlignEscapedNewlinesLeft = true;
  7041. EXPECT_EQ("#define A \\\n"
  7042. " \"some \" \\\n"
  7043. " \"text \" \\\n"
  7044. " \"other\";",
  7045. format("#define A \"some text other\";", AlignLeft));
  7046. }
  7047. TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
  7048. EXPECT_EQ(
  7049. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  7050. "(\n"
  7051. " \"x\t\");",
  7052. format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  7053. "aaaaaaa("
  7054. "\"x\t\");"));
  7055. }
  7056. TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
  7057. EXPECT_EQ(
  7058. "u8\"utf8 string \"\n"
  7059. "u8\"literal\";",
  7060. format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
  7061. EXPECT_EQ(
  7062. "u\"utf16 string \"\n"
  7063. "u\"literal\";",
  7064. format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
  7065. EXPECT_EQ(
  7066. "U\"utf32 string \"\n"
  7067. "U\"literal\";",
  7068. format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
  7069. EXPECT_EQ("L\"wide string \"\n"
  7070. "L\"literal\";",
  7071. format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
  7072. EXPECT_EQ("@\"NSString \"\n"
  7073. "@\"literal\";",
  7074. format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
  7075. // This input makes clang-format try to split the incomplete unicode escape
  7076. // sequence, which used to lead to a crasher.
  7077. verifyNoCrash(
  7078. "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  7079. getLLVMStyleWithColumns(60));
  7080. }
  7081. TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
  7082. FormatStyle Style = getGoogleStyleWithColumns(15);
  7083. EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
  7084. EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
  7085. EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
  7086. EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
  7087. EXPECT_EQ("u8R\"x(raw literal)x\";",
  7088. format("u8R\"x(raw literal)x\";", Style));
  7089. }
  7090. TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
  7091. FormatStyle Style = getLLVMStyleWithColumns(20);
  7092. EXPECT_EQ(
  7093. "_T(\"aaaaaaaaaaaaaa\")\n"
  7094. "_T(\"aaaaaaaaaaaaaa\")\n"
  7095. "_T(\"aaaaaaaaaaaa\")",
  7096. format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
  7097. EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
  7098. " _T(\"aaaaaa\"),\n"
  7099. " z);",
  7100. format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
  7101. // FIXME: Handle embedded spaces in one iteration.
  7102. // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
  7103. // "_T(\"aaaaaaaaaaaaa\")\n"
  7104. // "_T(\"aaaaaaaaaaaaa\")\n"
  7105. // "_T(\"a\")",
  7106. // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  7107. // getLLVMStyleWithColumns(20)));
  7108. EXPECT_EQ(
  7109. "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  7110. format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
  7111. EXPECT_EQ("f(\n"
  7112. "#if !TEST\n"
  7113. " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
  7114. "#endif\n"
  7115. " );",
  7116. format("f(\n"
  7117. "#if !TEST\n"
  7118. "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
  7119. "#endif\n"
  7120. ");"));
  7121. EXPECT_EQ("f(\n"
  7122. "\n"
  7123. " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
  7124. format("f(\n"
  7125. "\n"
  7126. "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
  7127. }
  7128. TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
  7129. EXPECT_EQ(
  7130. "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7131. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7132. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  7133. format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7134. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7135. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
  7136. }
  7137. TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
  7138. EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
  7139. format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle()));
  7140. EXPECT_EQ("fffffffffff(g(R\"x(\n"
  7141. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7142. ")x\",\n"
  7143. " a),\n"
  7144. " b);",
  7145. format("fffffffffff(g(R\"x(\n"
  7146. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7147. ")x\", a), b);",
  7148. getGoogleStyleWithColumns(20)));
  7149. EXPECT_EQ("fffffffffff(\n"
  7150. " g(R\"x(qqq\n"
  7151. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7152. ")x\",\n"
  7153. " a),\n"
  7154. " b);",
  7155. format("fffffffffff(g(R\"x(qqq\n"
  7156. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7157. ")x\", a), b);",
  7158. getGoogleStyleWithColumns(20)));
  7159. EXPECT_EQ("fffffffffff(R\"x(\n"
  7160. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7161. ")x\");",
  7162. format("fffffffffff(R\"x(\n"
  7163. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7164. ")x\");",
  7165. getGoogleStyleWithColumns(20)));
  7166. EXPECT_EQ("fffffffffff(R\"x(\n"
  7167. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7168. ")x\" + bbbbbb);",
  7169. format("fffffffffff(R\"x(\n"
  7170. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7171. ")x\" + bbbbbb);",
  7172. getGoogleStyleWithColumns(20)));
  7173. EXPECT_EQ("fffffffffff(\n"
  7174. " R\"x(\n"
  7175. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7176. ")x\" +\n"
  7177. " bbbbbb);",
  7178. format("fffffffffff(\n"
  7179. " R\"x(\n"
  7180. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7181. ")x\" + bbbbbb);",
  7182. getGoogleStyleWithColumns(20)));
  7183. }
  7184. TEST_F(FormatTest, SkipsUnknownStringLiterals) {
  7185. verifyFormat("string a = \"unterminated;");
  7186. EXPECT_EQ("function(\"unterminated,\n"
  7187. " OtherParameter);",
  7188. format("function( \"unterminated,\n"
  7189. " OtherParameter);"));
  7190. }
  7191. TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
  7192. FormatStyle Style = getLLVMStyle();
  7193. Style.Standard = FormatStyle::LS_Cpp03;
  7194. EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
  7195. format("#define x(_a) printf(\"foo\"_a);", Style));
  7196. }
  7197. TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
  7198. TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
  7199. EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
  7200. " \"ddeeefff\");",
  7201. format("someFunction(\"aaabbbcccdddeeefff\");",
  7202. getLLVMStyleWithColumns(25)));
  7203. EXPECT_EQ("someFunction1234567890(\n"
  7204. " \"aaabbbcccdddeeefff\");",
  7205. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  7206. getLLVMStyleWithColumns(26)));
  7207. EXPECT_EQ("someFunction1234567890(\n"
  7208. " \"aaabbbcccdddeeeff\"\n"
  7209. " \"f\");",
  7210. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  7211. getLLVMStyleWithColumns(25)));
  7212. EXPECT_EQ("someFunction1234567890(\n"
  7213. " \"aaabbbcccdddeeeff\"\n"
  7214. " \"f\");",
  7215. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  7216. getLLVMStyleWithColumns(24)));
  7217. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  7218. " \"ddde \"\n"
  7219. " \"efff\");",
  7220. format("someFunction(\"aaabbbcc ddde efff\");",
  7221. getLLVMStyleWithColumns(25)));
  7222. EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
  7223. " \"ddeeefff\");",
  7224. format("someFunction(\"aaabbbccc ddeeefff\");",
  7225. getLLVMStyleWithColumns(25)));
  7226. EXPECT_EQ("someFunction1234567890(\n"
  7227. " \"aaabb \"\n"
  7228. " \"cccdddeeefff\");",
  7229. format("someFunction1234567890(\"aaabb cccdddeeefff\");",
  7230. getLLVMStyleWithColumns(25)));
  7231. EXPECT_EQ("#define A \\\n"
  7232. " string s = \\\n"
  7233. " \"123456789\" \\\n"
  7234. " \"0\"; \\\n"
  7235. " int i;",
  7236. format("#define A string s = \"1234567890\"; int i;",
  7237. getLLVMStyleWithColumns(20)));
  7238. // FIXME: Put additional penalties on breaking at non-whitespace locations.
  7239. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  7240. " \"dddeeeff\"\n"
  7241. " \"f\");",
  7242. format("someFunction(\"aaabbbcc dddeeefff\");",
  7243. getLLVMStyleWithColumns(25)));
  7244. }
  7245. TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
  7246. EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
  7247. EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
  7248. EXPECT_EQ("\"test\"\n"
  7249. "\"\\n\"",
  7250. format("\"test\\n\"", getLLVMStyleWithColumns(7)));
  7251. EXPECT_EQ("\"tes\\\\\"\n"
  7252. "\"n\"",
  7253. format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
  7254. EXPECT_EQ("\"\\\\\\\\\"\n"
  7255. "\"\\n\"",
  7256. format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
  7257. EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
  7258. EXPECT_EQ("\"\\uff01\"\n"
  7259. "\"test\"",
  7260. format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
  7261. EXPECT_EQ("\"\\Uff01ff02\"",
  7262. format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
  7263. EXPECT_EQ("\"\\x000000000001\"\n"
  7264. "\"next\"",
  7265. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
  7266. EXPECT_EQ("\"\\x000000000001next\"",
  7267. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
  7268. EXPECT_EQ("\"\\x000000000001\"",
  7269. format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
  7270. EXPECT_EQ("\"test\"\n"
  7271. "\"\\000000\"\n"
  7272. "\"000001\"",
  7273. format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
  7274. EXPECT_EQ("\"test\\000\"\n"
  7275. "\"00000000\"\n"
  7276. "\"1\"",
  7277. format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
  7278. }
  7279. TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
  7280. verifyFormat("void f() {\n"
  7281. " return g() {}\n"
  7282. " void h() {}");
  7283. verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
  7284. "g();\n"
  7285. "}");
  7286. }
  7287. TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
  7288. verifyFormat(
  7289. "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
  7290. }
  7291. TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
  7292. verifyFormat("class X {\n"
  7293. " void f() {\n"
  7294. " }\n"
  7295. "};",
  7296. getLLVMStyleWithColumns(12));
  7297. }
  7298. TEST_F(FormatTest, ConfigurableIndentWidth) {
  7299. FormatStyle EightIndent = getLLVMStyleWithColumns(18);
  7300. EightIndent.IndentWidth = 8;
  7301. EightIndent.ContinuationIndentWidth = 8;
  7302. verifyFormat("void f() {\n"
  7303. " someFunction();\n"
  7304. " if (true) {\n"
  7305. " f();\n"
  7306. " }\n"
  7307. "}",
  7308. EightIndent);
  7309. verifyFormat("class X {\n"
  7310. " void f() {\n"
  7311. " }\n"
  7312. "};",
  7313. EightIndent);
  7314. verifyFormat("int x[] = {\n"
  7315. " call(),\n"
  7316. " call()};",
  7317. EightIndent);
  7318. }
  7319. TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
  7320. verifyFormat("double\n"
  7321. "f();",
  7322. getLLVMStyleWithColumns(8));
  7323. }
  7324. TEST_F(FormatTest, ConfigurableUseOfTab) {
  7325. FormatStyle Tab = getLLVMStyleWithColumns(42);
  7326. Tab.IndentWidth = 8;
  7327. Tab.UseTab = FormatStyle::UT_Always;
  7328. Tab.AlignEscapedNewlinesLeft = true;
  7329. EXPECT_EQ("if (aaaaaaaa && // q\n"
  7330. " bb)\t\t// w\n"
  7331. "\t;",
  7332. format("if (aaaaaaaa &&// q\n"
  7333. "bb)// w\n"
  7334. ";",
  7335. Tab));
  7336. EXPECT_EQ("if (aaa && bbb) // w\n"
  7337. "\t;",
  7338. format("if(aaa&&bbb)// w\n"
  7339. ";",
  7340. Tab));
  7341. verifyFormat("class X {\n"
  7342. "\tvoid f() {\n"
  7343. "\t\tsomeFunction(parameter1,\n"
  7344. "\t\t\t parameter2);\n"
  7345. "\t}\n"
  7346. "};",
  7347. Tab);
  7348. verifyFormat("#define A \\\n"
  7349. "\tvoid f() { \\\n"
  7350. "\t\tsomeFunction( \\\n"
  7351. "\t\t parameter1, \\\n"
  7352. "\t\t parameter2); \\\n"
  7353. "\t}",
  7354. Tab);
  7355. Tab.TabWidth = 4;
  7356. Tab.IndentWidth = 8;
  7357. verifyFormat("class TabWidth4Indent8 {\n"
  7358. "\t\tvoid f() {\n"
  7359. "\t\t\t\tsomeFunction(parameter1,\n"
  7360. "\t\t\t\t\t\t\t parameter2);\n"
  7361. "\t\t}\n"
  7362. "};",
  7363. Tab);
  7364. Tab.TabWidth = 4;
  7365. Tab.IndentWidth = 4;
  7366. verifyFormat("class TabWidth4Indent4 {\n"
  7367. "\tvoid f() {\n"
  7368. "\t\tsomeFunction(parameter1,\n"
  7369. "\t\t\t\t\t parameter2);\n"
  7370. "\t}\n"
  7371. "};",
  7372. Tab);
  7373. Tab.TabWidth = 8;
  7374. Tab.IndentWidth = 4;
  7375. verifyFormat("class TabWidth8Indent4 {\n"
  7376. " void f() {\n"
  7377. "\tsomeFunction(parameter1,\n"
  7378. "\t\t parameter2);\n"
  7379. " }\n"
  7380. "};",
  7381. Tab);
  7382. Tab.TabWidth = 8;
  7383. Tab.IndentWidth = 8;
  7384. EXPECT_EQ("/*\n"
  7385. "\t a\t\tcomment\n"
  7386. "\t in multiple lines\n"
  7387. " */",
  7388. format(" /*\t \t \n"
  7389. " \t \t a\t\tcomment\t \t\n"
  7390. " \t \t in multiple lines\t\n"
  7391. " \t */",
  7392. Tab));
  7393. Tab.UseTab = FormatStyle::UT_ForIndentation;
  7394. verifyFormat("{\n"
  7395. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7396. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7397. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7398. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7399. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7400. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7401. "};",
  7402. Tab);
  7403. verifyFormat("enum A {\n"
  7404. "\ta1, // Force multiple lines\n"
  7405. "\ta2,\n"
  7406. "\ta3\n"
  7407. "};",
  7408. Tab);
  7409. EXPECT_EQ("if (aaaaaaaa && // q\n"
  7410. " bb) // w\n"
  7411. "\t;",
  7412. format("if (aaaaaaaa &&// q\n"
  7413. "bb)// w\n"
  7414. ";",
  7415. Tab));
  7416. verifyFormat("class X {\n"
  7417. "\tvoid f() {\n"
  7418. "\t\tsomeFunction(parameter1,\n"
  7419. "\t\t parameter2);\n"
  7420. "\t}\n"
  7421. "};",
  7422. Tab);
  7423. verifyFormat("{\n"
  7424. "\tQ({\n"
  7425. "\t\tint a;\n"
  7426. "\t\tsomeFunction(aaaaaaaa,\n"
  7427. "\t\t bbbbbbb);\n"
  7428. "\t}, p);\n"
  7429. "}",
  7430. Tab);
  7431. EXPECT_EQ("{\n"
  7432. "\t/* aaaa\n"
  7433. "\t bbbb */\n"
  7434. "}",
  7435. format("{\n"
  7436. "/* aaaa\n"
  7437. " bbbb */\n"
  7438. "}",
  7439. Tab));
  7440. EXPECT_EQ("{\n"
  7441. "\t/*\n"
  7442. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7443. "\t bbbbbbbbbbbbb\n"
  7444. "\t*/\n"
  7445. "}",
  7446. format("{\n"
  7447. "/*\n"
  7448. " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7449. "*/\n"
  7450. "}",
  7451. Tab));
  7452. EXPECT_EQ("{\n"
  7453. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7454. "\t// bbbbbbbbbbbbb\n"
  7455. "}",
  7456. format("{\n"
  7457. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7458. "}",
  7459. Tab));
  7460. EXPECT_EQ("{\n"
  7461. "\t/*\n"
  7462. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7463. "\t bbbbbbbbbbbbb\n"
  7464. "\t*/\n"
  7465. "}",
  7466. format("{\n"
  7467. "\t/*\n"
  7468. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7469. "\t*/\n"
  7470. "}",
  7471. Tab));
  7472. EXPECT_EQ("{\n"
  7473. "\t/*\n"
  7474. "\n"
  7475. "\t*/\n"
  7476. "}",
  7477. format("{\n"
  7478. "\t/*\n"
  7479. "\n"
  7480. "\t*/\n"
  7481. "}",
  7482. Tab));
  7483. EXPECT_EQ("{\n"
  7484. "\t/*\n"
  7485. " asdf\n"
  7486. "\t*/\n"
  7487. "}",
  7488. format("{\n"
  7489. "\t/*\n"
  7490. " asdf\n"
  7491. "\t*/\n"
  7492. "}",
  7493. Tab));
  7494. Tab.UseTab = FormatStyle::UT_Never;
  7495. EXPECT_EQ("/*\n"
  7496. " a\t\tcomment\n"
  7497. " in multiple lines\n"
  7498. " */",
  7499. format(" /*\t \t \n"
  7500. " \t \t a\t\tcomment\t \t\n"
  7501. " \t \t in multiple lines\t\n"
  7502. " \t */",
  7503. Tab));
  7504. EXPECT_EQ("/* some\n"
  7505. " comment */",
  7506. format(" \t \t /* some\n"
  7507. " \t \t comment */",
  7508. Tab));
  7509. EXPECT_EQ("int a; /* some\n"
  7510. " comment */",
  7511. format(" \t \t int a; /* some\n"
  7512. " \t \t comment */",
  7513. Tab));
  7514. EXPECT_EQ("int a; /* some\n"
  7515. "comment */",
  7516. format(" \t \t int\ta; /* some\n"
  7517. " \t \t comment */",
  7518. Tab));
  7519. EXPECT_EQ("f(\"\t\t\"); /* some\n"
  7520. " comment */",
  7521. format(" \t \t f(\"\t\t\"); /* some\n"
  7522. " \t \t comment */",
  7523. Tab));
  7524. EXPECT_EQ("{\n"
  7525. " /*\n"
  7526. " * Comment\n"
  7527. " */\n"
  7528. " int i;\n"
  7529. "}",
  7530. format("{\n"
  7531. "\t/*\n"
  7532. "\t * Comment\n"
  7533. "\t */\n"
  7534. "\t int i;\n"
  7535. "}"));
  7536. }
  7537. TEST_F(FormatTest, CalculatesOriginalColumn) {
  7538. EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7539. "q\"; /* some\n"
  7540. " comment */",
  7541. format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7542. "q\"; /* some\n"
  7543. " comment */",
  7544. getLLVMStyle()));
  7545. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  7546. "/* some\n"
  7547. " comment */",
  7548. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  7549. " /* some\n"
  7550. " comment */",
  7551. getLLVMStyle()));
  7552. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7553. "qqq\n"
  7554. "/* some\n"
  7555. " comment */",
  7556. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7557. "qqq\n"
  7558. " /* some\n"
  7559. " comment */",
  7560. getLLVMStyle()));
  7561. EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7562. "wwww; /* some\n"
  7563. " comment */",
  7564. format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7565. "wwww; /* some\n"
  7566. " comment */",
  7567. getLLVMStyle()));
  7568. }
  7569. TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
  7570. FormatStyle NoSpace = getLLVMStyle();
  7571. NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
  7572. verifyFormat("while(true)\n"
  7573. " continue;",
  7574. NoSpace);
  7575. verifyFormat("for(;;)\n"
  7576. " continue;",
  7577. NoSpace);
  7578. verifyFormat("if(true)\n"
  7579. " f();\n"
  7580. "else if(true)\n"
  7581. " f();",
  7582. NoSpace);
  7583. verifyFormat("do {\n"
  7584. " do_something();\n"
  7585. "} while(something());",
  7586. NoSpace);
  7587. verifyFormat("switch(x) {\n"
  7588. "default:\n"
  7589. " break;\n"
  7590. "}",
  7591. NoSpace);
  7592. verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
  7593. verifyFormat("size_t x = sizeof(x);", NoSpace);
  7594. verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
  7595. verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
  7596. verifyFormat("alignas(128) char a[128];", NoSpace);
  7597. verifyFormat("size_t x = alignof(MyType);", NoSpace);
  7598. verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
  7599. verifyFormat("int f() throw(Deprecated);", NoSpace);
  7600. verifyFormat("typedef void (*cb)(int);", NoSpace);
  7601. FormatStyle Space = getLLVMStyle();
  7602. Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
  7603. verifyFormat("int f ();", Space);
  7604. verifyFormat("void f (int a, T b) {\n"
  7605. " while (true)\n"
  7606. " continue;\n"
  7607. "}",
  7608. Space);
  7609. verifyFormat("if (true)\n"
  7610. " f ();\n"
  7611. "else if (true)\n"
  7612. " f ();",
  7613. Space);
  7614. verifyFormat("do {\n"
  7615. " do_something ();\n"
  7616. "} while (something ());",
  7617. Space);
  7618. verifyFormat("switch (x) {\n"
  7619. "default:\n"
  7620. " break;\n"
  7621. "}",
  7622. Space);
  7623. verifyFormat("A::A () : a (1) {}", Space);
  7624. verifyFormat("void f () __attribute__ ((asdf));", Space);
  7625. verifyFormat("*(&a + 1);\n"
  7626. "&((&a)[1]);\n"
  7627. "a[(b + c) * d];\n"
  7628. "(((a + 1) * 2) + 3) * 4;",
  7629. Space);
  7630. verifyFormat("#define A(x) x", Space);
  7631. verifyFormat("#define A (x) x", Space);
  7632. verifyFormat("#if defined(x)\n"
  7633. "#endif",
  7634. Space);
  7635. verifyFormat("auto i = std::make_unique<int> (5);", Space);
  7636. verifyFormat("size_t x = sizeof (x);", Space);
  7637. verifyFormat("auto f (int x) -> decltype (x);", Space);
  7638. verifyFormat("int f (T x) noexcept (x.create ());", Space);
  7639. verifyFormat("alignas (128) char a[128];", Space);
  7640. verifyFormat("size_t x = alignof (MyType);", Space);
  7641. verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
  7642. verifyFormat("int f () throw (Deprecated);", Space);
  7643. verifyFormat("typedef void (*cb) (int);", Space);
  7644. }
  7645. TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
  7646. FormatStyle Spaces = getLLVMStyle();
  7647. Spaces.SpacesInParentheses = true;
  7648. verifyFormat("call( x, y, z );", Spaces);
  7649. verifyFormat("call();", Spaces);
  7650. verifyFormat("std::function<void( int, int )> callback;", Spaces);
  7651. verifyFormat("while ( (bool)1 )\n"
  7652. " continue;",
  7653. Spaces);
  7654. verifyFormat("for ( ;; )\n"
  7655. " continue;",
  7656. Spaces);
  7657. verifyFormat("if ( true )\n"
  7658. " f();\n"
  7659. "else if ( true )\n"
  7660. " f();",
  7661. Spaces);
  7662. verifyFormat("do {\n"
  7663. " do_something( (int)i );\n"
  7664. "} while ( something() );",
  7665. Spaces);
  7666. verifyFormat("switch ( x ) {\n"
  7667. "default:\n"
  7668. " break;\n"
  7669. "}",
  7670. Spaces);
  7671. Spaces.SpacesInParentheses = false;
  7672. Spaces.SpacesInCStyleCastParentheses = true;
  7673. verifyFormat("Type *A = ( Type * )P;", Spaces);
  7674. verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
  7675. verifyFormat("x = ( int32 )y;", Spaces);
  7676. verifyFormat("int a = ( int )(2.0f);", Spaces);
  7677. verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
  7678. verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
  7679. verifyFormat("#define x (( int )-1)", Spaces);
  7680. // Run the first set of tests again with:
  7681. Spaces.SpacesInParentheses = false, Spaces.SpaceInEmptyParentheses = true;
  7682. Spaces.SpacesInCStyleCastParentheses = true;
  7683. verifyFormat("call(x, y, z);", Spaces);
  7684. verifyFormat("call( );", Spaces);
  7685. verifyFormat("std::function<void(int, int)> callback;", Spaces);
  7686. verifyFormat("while (( bool )1)\n"
  7687. " continue;",
  7688. Spaces);
  7689. verifyFormat("for (;;)\n"
  7690. " continue;",
  7691. Spaces);
  7692. verifyFormat("if (true)\n"
  7693. " f( );\n"
  7694. "else if (true)\n"
  7695. " f( );",
  7696. Spaces);
  7697. verifyFormat("do {\n"
  7698. " do_something(( int )i);\n"
  7699. "} while (something( ));",
  7700. Spaces);
  7701. verifyFormat("switch (x) {\n"
  7702. "default:\n"
  7703. " break;\n"
  7704. "}",
  7705. Spaces);
  7706. // Run the first set of tests again with:
  7707. Spaces.SpaceAfterCStyleCast = true;
  7708. verifyFormat("call(x, y, z);", Spaces);
  7709. verifyFormat("call( );", Spaces);
  7710. verifyFormat("std::function<void(int, int)> callback;", Spaces);
  7711. verifyFormat("while (( bool ) 1)\n"
  7712. " continue;",
  7713. Spaces);
  7714. verifyFormat("for (;;)\n"
  7715. " continue;",
  7716. Spaces);
  7717. verifyFormat("if (true)\n"
  7718. " f( );\n"
  7719. "else if (true)\n"
  7720. " f( );",
  7721. Spaces);
  7722. verifyFormat("do {\n"
  7723. " do_something(( int ) i);\n"
  7724. "} while (something( ));",
  7725. Spaces);
  7726. verifyFormat("switch (x) {\n"
  7727. "default:\n"
  7728. " break;\n"
  7729. "}",
  7730. Spaces);
  7731. // Run subset of tests again with:
  7732. Spaces.SpacesInCStyleCastParentheses = false;
  7733. Spaces.SpaceAfterCStyleCast = true;
  7734. verifyFormat("while ((bool) 1)\n"
  7735. " continue;",
  7736. Spaces);
  7737. verifyFormat("do {\n"
  7738. " do_something((int) i);\n"
  7739. "} while (something( ));",
  7740. Spaces);
  7741. }
  7742. TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
  7743. verifyFormat("int a[5];");
  7744. verifyFormat("a[3] += 42;");
  7745. FormatStyle Spaces = getLLVMStyle();
  7746. Spaces.SpacesInSquareBrackets = true;
  7747. // Lambdas unchanged.
  7748. verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
  7749. verifyFormat("return [i, args...] {};", Spaces);
  7750. // Not lambdas.
  7751. verifyFormat("int a[ 5 ];", Spaces);
  7752. verifyFormat("a[ 3 ] += 42;", Spaces);
  7753. verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
  7754. verifyFormat("double &operator[](int i) { return 0; }\n"
  7755. "int i;",
  7756. Spaces);
  7757. verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
  7758. verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
  7759. verifyFormat("int i = (*b)[ a ]->f();", Spaces);
  7760. }
  7761. TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
  7762. verifyFormat("int a = 5;");
  7763. verifyFormat("a += 42;");
  7764. verifyFormat("a or_eq 8;");
  7765. FormatStyle Spaces = getLLVMStyle();
  7766. Spaces.SpaceBeforeAssignmentOperators = false;
  7767. verifyFormat("int a= 5;", Spaces);
  7768. verifyFormat("a+= 42;", Spaces);
  7769. verifyFormat("a or_eq 8;", Spaces);
  7770. }
  7771. TEST_F(FormatTest, AlignConsecutiveAssignments) {
  7772. FormatStyle Alignment = getLLVMStyle();
  7773. Alignment.AlignConsecutiveAssignments = false;
  7774. verifyFormat("int a = 5;\n"
  7775. "int oneTwoThree = 123;",
  7776. Alignment);
  7777. verifyFormat("int a = 5;\n"
  7778. "int oneTwoThree = 123;",
  7779. Alignment);
  7780. Alignment.AlignConsecutiveAssignments = true;
  7781. verifyFormat("int a = 5;\n"
  7782. "int oneTwoThree = 123;",
  7783. Alignment);
  7784. verifyFormat("int a = method();\n"
  7785. "int oneTwoThree = 133;",
  7786. Alignment);
  7787. verifyFormat("a &= 5;\n"
  7788. "bcd *= 5;\n"
  7789. "ghtyf += 5;\n"
  7790. "dvfvdb -= 5;\n"
  7791. "a /= 5;\n"
  7792. "vdsvsv %= 5;\n"
  7793. "sfdbddfbdfbb ^= 5;\n"
  7794. "dvsdsv |= 5;\n"
  7795. "int dsvvdvsdvvv = 123;",
  7796. Alignment);
  7797. verifyFormat("int i = 1, j = 10;\n"
  7798. "something = 2000;",
  7799. Alignment);
  7800. verifyFormat("something = 2000;\n"
  7801. "int i = 1, j = 10;\n",
  7802. Alignment);
  7803. verifyFormat("something = 2000;\n"
  7804. "another = 911;\n"
  7805. "int i = 1, j = 10;\n"
  7806. "oneMore = 1;\n"
  7807. "i = 2;",
  7808. Alignment);
  7809. verifyFormat("int a = 5;\n"
  7810. "int one = 1;\n"
  7811. "method();\n"
  7812. "int oneTwoThree = 123;\n"
  7813. "int oneTwo = 12;",
  7814. Alignment);
  7815. verifyFormat("int oneTwoThree = 123; // comment\n"
  7816. "int oneTwo = 12; // comment",
  7817. Alignment);
  7818. EXPECT_EQ("int a = 5;\n"
  7819. "\n"
  7820. "int oneTwoThree = 123;",
  7821. format("int a = 5;\n"
  7822. "\n"
  7823. "int oneTwoThree= 123;",
  7824. Alignment));
  7825. EXPECT_EQ("int a = 5;\n"
  7826. "int one = 1;\n"
  7827. "\n"
  7828. "int oneTwoThree = 123;",
  7829. format("int a = 5;\n"
  7830. "int one = 1;\n"
  7831. "\n"
  7832. "int oneTwoThree = 123;",
  7833. Alignment));
  7834. EXPECT_EQ("int a = 5;\n"
  7835. "int one = 1;\n"
  7836. "\n"
  7837. "int oneTwoThree = 123;\n"
  7838. "int oneTwo = 12;",
  7839. format("int a = 5;\n"
  7840. "int one = 1;\n"
  7841. "\n"
  7842. "int oneTwoThree = 123;\n"
  7843. "int oneTwo = 12;",
  7844. Alignment));
  7845. Alignment.AlignEscapedNewlinesLeft = true;
  7846. verifyFormat("#define A \\\n"
  7847. " int aaaa = 12; \\\n"
  7848. " int b = 23; \\\n"
  7849. " int ccc = 234; \\\n"
  7850. " int dddddddddd = 2345;",
  7851. Alignment);
  7852. Alignment.AlignEscapedNewlinesLeft = false;
  7853. verifyFormat("#define A "
  7854. " \\\n"
  7855. " int aaaa = 12; "
  7856. " \\\n"
  7857. " int b = 23; "
  7858. " \\\n"
  7859. " int ccc = 234; "
  7860. " \\\n"
  7861. " int dddddddddd = 2345;",
  7862. Alignment);
  7863. verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
  7864. "k = 4, int l = 5,\n"
  7865. " int m = 6) {\n"
  7866. " int j = 10;\n"
  7867. " otherThing = 1;\n"
  7868. "}",
  7869. Alignment);
  7870. verifyFormat("void SomeFunction(int parameter = 0) {\n"
  7871. " int i = 1;\n"
  7872. " int j = 2;\n"
  7873. " int big = 10000;\n"
  7874. "}",
  7875. Alignment);
  7876. verifyFormat("class C {\n"
  7877. "public:\n"
  7878. " int i = 1;\n"
  7879. " virtual void f() = 0;\n"
  7880. "};",
  7881. Alignment);
  7882. verifyFormat("int i = 1;\n"
  7883. "if (SomeType t = getSomething()) {\n"
  7884. "}\n"
  7885. "int j = 2;\n"
  7886. "int big = 10000;",
  7887. Alignment);
  7888. verifyFormat("int j = 7;\n"
  7889. "for (int k = 0; k < N; ++k) {\n"
  7890. "}\n"
  7891. "int j = 2;\n"
  7892. "int big = 10000;\n"
  7893. "}",
  7894. Alignment);
  7895. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  7896. verifyFormat("int i = 1;\n"
  7897. "LooooooooooongType loooooooooooooooooooooongVariable\n"
  7898. " = someLooooooooooooooooongFunction();\n"
  7899. "int j = 2;",
  7900. Alignment);
  7901. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
  7902. verifyFormat("int i = 1;\n"
  7903. "LooooooooooongType loooooooooooooooooooooongVariable =\n"
  7904. " someLooooooooooooooooongFunction();\n"
  7905. "int j = 2;",
  7906. Alignment);
  7907. // FIXME: Should align all three assignments
  7908. verifyFormat(
  7909. "int i = 1;\n"
  7910. "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
  7911. " loooooooooooooooooooooongParameterB);\n"
  7912. "int j = 2;",
  7913. Alignment);
  7914. }
  7915. TEST_F(FormatTest, LinuxBraceBreaking) {
  7916. FormatStyle LinuxBraceStyle = getLLVMStyle();
  7917. LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
  7918. verifyFormat("namespace a\n"
  7919. "{\n"
  7920. "class A\n"
  7921. "{\n"
  7922. " void f()\n"
  7923. " {\n"
  7924. " if (true) {\n"
  7925. " a();\n"
  7926. " b();\n"
  7927. " }\n"
  7928. " }\n"
  7929. " void g() { return; }\n"
  7930. "};\n"
  7931. "struct B {\n"
  7932. " int x;\n"
  7933. "};\n"
  7934. "}\n",
  7935. LinuxBraceStyle);
  7936. verifyFormat("enum X {\n"
  7937. " Y = 0,\n"
  7938. "}\n",
  7939. LinuxBraceStyle);
  7940. verifyFormat("struct S {\n"
  7941. " int Type;\n"
  7942. " union {\n"
  7943. " int x;\n"
  7944. " double y;\n"
  7945. " } Value;\n"
  7946. " class C\n"
  7947. " {\n"
  7948. " MyFavoriteType Value;\n"
  7949. " } Class;\n"
  7950. "}\n",
  7951. LinuxBraceStyle);
  7952. }
  7953. TEST_F(FormatTest, MozillaBraceBreaking) {
  7954. FormatStyle MozillaBraceStyle = getLLVMStyle();
  7955. MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
  7956. verifyFormat("namespace a {\n"
  7957. "class A\n"
  7958. "{\n"
  7959. " void f()\n"
  7960. " {\n"
  7961. " if (true) {\n"
  7962. " a();\n"
  7963. " b();\n"
  7964. " }\n"
  7965. " }\n"
  7966. " void g() { return; }\n"
  7967. "};\n"
  7968. "enum E\n"
  7969. "{\n"
  7970. " A,\n"
  7971. " // foo\n"
  7972. " B,\n"
  7973. " C\n"
  7974. "};\n"
  7975. "struct B\n"
  7976. "{\n"
  7977. " int x;\n"
  7978. "};\n"
  7979. "}\n",
  7980. MozillaBraceStyle);
  7981. verifyFormat("struct S\n"
  7982. "{\n"
  7983. " int Type;\n"
  7984. " union\n"
  7985. " {\n"
  7986. " int x;\n"
  7987. " double y;\n"
  7988. " } Value;\n"
  7989. " class C\n"
  7990. " {\n"
  7991. " MyFavoriteType Value;\n"
  7992. " } Class;\n"
  7993. "}\n",
  7994. MozillaBraceStyle);
  7995. }
  7996. TEST_F(FormatTest, StroustrupBraceBreaking) {
  7997. FormatStyle StroustrupBraceStyle = getLLVMStyle();
  7998. StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  7999. verifyFormat("namespace a {\n"
  8000. "class A {\n"
  8001. " void f()\n"
  8002. " {\n"
  8003. " if (true) {\n"
  8004. " a();\n"
  8005. " b();\n"
  8006. " }\n"
  8007. " }\n"
  8008. " void g() { return; }\n"
  8009. "};\n"
  8010. "struct B {\n"
  8011. " int x;\n"
  8012. "};\n"
  8013. "}\n",
  8014. StroustrupBraceStyle);
  8015. verifyFormat("void foo()\n"
  8016. "{\n"
  8017. " if (a) {\n"
  8018. " a();\n"
  8019. " }\n"
  8020. " else {\n"
  8021. " b();\n"
  8022. " }\n"
  8023. "}\n",
  8024. StroustrupBraceStyle);
  8025. verifyFormat("#ifdef _DEBUG\n"
  8026. "int foo(int i = 0)\n"
  8027. "#else\n"
  8028. "int foo(int i = 5)\n"
  8029. "#endif\n"
  8030. "{\n"
  8031. " return i;\n"
  8032. "}",
  8033. StroustrupBraceStyle);
  8034. verifyFormat("void foo() {}\n"
  8035. "void bar()\n"
  8036. "#ifdef _DEBUG\n"
  8037. "{\n"
  8038. " foo();\n"
  8039. "}\n"
  8040. "#else\n"
  8041. "{\n"
  8042. "}\n"
  8043. "#endif",
  8044. StroustrupBraceStyle);
  8045. verifyFormat("void foobar() { int i = 5; }\n"
  8046. "#ifdef _DEBUG\n"
  8047. "void bar() {}\n"
  8048. "#else\n"
  8049. "void bar() { foobar(); }\n"
  8050. "#endif",
  8051. StroustrupBraceStyle);
  8052. }
  8053. TEST_F(FormatTest, AllmanBraceBreaking) {
  8054. FormatStyle AllmanBraceStyle = getLLVMStyle();
  8055. AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
  8056. verifyFormat("namespace a\n"
  8057. "{\n"
  8058. "class A\n"
  8059. "{\n"
  8060. " void f()\n"
  8061. " {\n"
  8062. " if (true)\n"
  8063. " {\n"
  8064. " a();\n"
  8065. " b();\n"
  8066. " }\n"
  8067. " }\n"
  8068. " void g() { return; }\n"
  8069. "};\n"
  8070. "struct B\n"
  8071. "{\n"
  8072. " int x;\n"
  8073. "};\n"
  8074. "}",
  8075. AllmanBraceStyle);
  8076. verifyFormat("void f()\n"
  8077. "{\n"
  8078. " if (true)\n"
  8079. " {\n"
  8080. " a();\n"
  8081. " }\n"
  8082. " else if (false)\n"
  8083. " {\n"
  8084. " b();\n"
  8085. " }\n"
  8086. " else\n"
  8087. " {\n"
  8088. " c();\n"
  8089. " }\n"
  8090. "}\n",
  8091. AllmanBraceStyle);
  8092. verifyFormat("void f()\n"
  8093. "{\n"
  8094. " for (int i = 0; i < 10; ++i)\n"
  8095. " {\n"
  8096. " a();\n"
  8097. " }\n"
  8098. " while (false)\n"
  8099. " {\n"
  8100. " b();\n"
  8101. " }\n"
  8102. " do\n"
  8103. " {\n"
  8104. " c();\n"
  8105. " } while (false)\n"
  8106. "}\n",
  8107. AllmanBraceStyle);
  8108. verifyFormat("void f(int a)\n"
  8109. "{\n"
  8110. " switch (a)\n"
  8111. " {\n"
  8112. " case 0:\n"
  8113. " break;\n"
  8114. " case 1:\n"
  8115. " {\n"
  8116. " break;\n"
  8117. " }\n"
  8118. " case 2:\n"
  8119. " {\n"
  8120. " }\n"
  8121. " break;\n"
  8122. " default:\n"
  8123. " break;\n"
  8124. " }\n"
  8125. "}\n",
  8126. AllmanBraceStyle);
  8127. verifyFormat("enum X\n"
  8128. "{\n"
  8129. " Y = 0,\n"
  8130. "}\n",
  8131. AllmanBraceStyle);
  8132. verifyFormat("enum X\n"
  8133. "{\n"
  8134. " Y = 0\n"
  8135. "}\n",
  8136. AllmanBraceStyle);
  8137. verifyFormat("@interface BSApplicationController ()\n"
  8138. "{\n"
  8139. "@private\n"
  8140. " id _extraIvar;\n"
  8141. "}\n"
  8142. "@end\n",
  8143. AllmanBraceStyle);
  8144. verifyFormat("#ifdef _DEBUG\n"
  8145. "int foo(int i = 0)\n"
  8146. "#else\n"
  8147. "int foo(int i = 5)\n"
  8148. "#endif\n"
  8149. "{\n"
  8150. " return i;\n"
  8151. "}",
  8152. AllmanBraceStyle);
  8153. verifyFormat("void foo() {}\n"
  8154. "void bar()\n"
  8155. "#ifdef _DEBUG\n"
  8156. "{\n"
  8157. " foo();\n"
  8158. "}\n"
  8159. "#else\n"
  8160. "{\n"
  8161. "}\n"
  8162. "#endif",
  8163. AllmanBraceStyle);
  8164. verifyFormat("void foobar() { int i = 5; }\n"
  8165. "#ifdef _DEBUG\n"
  8166. "void bar() {}\n"
  8167. "#else\n"
  8168. "void bar() { foobar(); }\n"
  8169. "#endif",
  8170. AllmanBraceStyle);
  8171. // This shouldn't affect ObjC blocks..
  8172. verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
  8173. " // ...\n"
  8174. " int i;\n"
  8175. "}];",
  8176. AllmanBraceStyle);
  8177. verifyFormat("void (^block)(void) = ^{\n"
  8178. " // ...\n"
  8179. " int i;\n"
  8180. "};",
  8181. AllmanBraceStyle);
  8182. // .. or dict literals.
  8183. verifyFormat("void f()\n"
  8184. "{\n"
  8185. " [object someMethod:@{ @\"a\" : @\"b\" }];\n"
  8186. "}",
  8187. AllmanBraceStyle);
  8188. verifyFormat("int f()\n"
  8189. "{ // comment\n"
  8190. " return 42;\n"
  8191. "}",
  8192. AllmanBraceStyle);
  8193. AllmanBraceStyle.ColumnLimit = 19;
  8194. verifyFormat("void f() { int i; }", AllmanBraceStyle);
  8195. AllmanBraceStyle.ColumnLimit = 18;
  8196. verifyFormat("void f()\n"
  8197. "{\n"
  8198. " int i;\n"
  8199. "}",
  8200. AllmanBraceStyle);
  8201. AllmanBraceStyle.ColumnLimit = 80;
  8202. FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
  8203. BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
  8204. BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
  8205. verifyFormat("void f(bool b)\n"
  8206. "{\n"
  8207. " if (b)\n"
  8208. " {\n"
  8209. " return;\n"
  8210. " }\n"
  8211. "}\n",
  8212. BreakBeforeBraceShortIfs);
  8213. verifyFormat("void f(bool b)\n"
  8214. "{\n"
  8215. " if (b) return;\n"
  8216. "}\n",
  8217. BreakBeforeBraceShortIfs);
  8218. verifyFormat("void f(bool b)\n"
  8219. "{\n"
  8220. " while (b)\n"
  8221. " {\n"
  8222. " return;\n"
  8223. " }\n"
  8224. "}\n",
  8225. BreakBeforeBraceShortIfs);
  8226. }
  8227. TEST_F(FormatTest, GNUBraceBreaking) {
  8228. FormatStyle GNUBraceStyle = getLLVMStyle();
  8229. GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
  8230. verifyFormat("namespace a\n"
  8231. "{\n"
  8232. "class A\n"
  8233. "{\n"
  8234. " void f()\n"
  8235. " {\n"
  8236. " int a;\n"
  8237. " {\n"
  8238. " int b;\n"
  8239. " }\n"
  8240. " if (true)\n"
  8241. " {\n"
  8242. " a();\n"
  8243. " b();\n"
  8244. " }\n"
  8245. " }\n"
  8246. " void g() { return; }\n"
  8247. "}\n"
  8248. "}",
  8249. GNUBraceStyle);
  8250. verifyFormat("void f()\n"
  8251. "{\n"
  8252. " if (true)\n"
  8253. " {\n"
  8254. " a();\n"
  8255. " }\n"
  8256. " else if (false)\n"
  8257. " {\n"
  8258. " b();\n"
  8259. " }\n"
  8260. " else\n"
  8261. " {\n"
  8262. " c();\n"
  8263. " }\n"
  8264. "}\n",
  8265. GNUBraceStyle);
  8266. verifyFormat("void f()\n"
  8267. "{\n"
  8268. " for (int i = 0; i < 10; ++i)\n"
  8269. " {\n"
  8270. " a();\n"
  8271. " }\n"
  8272. " while (false)\n"
  8273. " {\n"
  8274. " b();\n"
  8275. " }\n"
  8276. " do\n"
  8277. " {\n"
  8278. " c();\n"
  8279. " }\n"
  8280. " while (false);\n"
  8281. "}\n",
  8282. GNUBraceStyle);
  8283. verifyFormat("void f(int a)\n"
  8284. "{\n"
  8285. " switch (a)\n"
  8286. " {\n"
  8287. " case 0:\n"
  8288. " break;\n"
  8289. " case 1:\n"
  8290. " {\n"
  8291. " break;\n"
  8292. " }\n"
  8293. " case 2:\n"
  8294. " {\n"
  8295. " }\n"
  8296. " break;\n"
  8297. " default:\n"
  8298. " break;\n"
  8299. " }\n"
  8300. "}\n",
  8301. GNUBraceStyle);
  8302. verifyFormat("enum X\n"
  8303. "{\n"
  8304. " Y = 0,\n"
  8305. "}\n",
  8306. GNUBraceStyle);
  8307. verifyFormat("@interface BSApplicationController ()\n"
  8308. "{\n"
  8309. "@private\n"
  8310. " id _extraIvar;\n"
  8311. "}\n"
  8312. "@end\n",
  8313. GNUBraceStyle);
  8314. verifyFormat("#ifdef _DEBUG\n"
  8315. "int foo(int i = 0)\n"
  8316. "#else\n"
  8317. "int foo(int i = 5)\n"
  8318. "#endif\n"
  8319. "{\n"
  8320. " return i;\n"
  8321. "}",
  8322. GNUBraceStyle);
  8323. verifyFormat("void foo() {}\n"
  8324. "void bar()\n"
  8325. "#ifdef _DEBUG\n"
  8326. "{\n"
  8327. " foo();\n"
  8328. "}\n"
  8329. "#else\n"
  8330. "{\n"
  8331. "}\n"
  8332. "#endif",
  8333. GNUBraceStyle);
  8334. verifyFormat("void foobar() { int i = 5; }\n"
  8335. "#ifdef _DEBUG\n"
  8336. "void bar() {}\n"
  8337. "#else\n"
  8338. "void bar() { foobar(); }\n"
  8339. "#endif",
  8340. GNUBraceStyle);
  8341. }
  8342. TEST_F(FormatTest, CatchExceptionReferenceBinding) {
  8343. verifyFormat("void f() {\n"
  8344. " try {\n"
  8345. " } catch (const Exception &e) {\n"
  8346. " }\n"
  8347. "}\n",
  8348. getLLVMStyle());
  8349. }
  8350. TEST_F(FormatTest, UnderstandsPragmas) {
  8351. verifyFormat("#pragma omp reduction(| : var)");
  8352. verifyFormat("#pragma omp reduction(+ : var)");
  8353. EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
  8354. "(including parentheses).",
  8355. format("#pragma mark Any non-hyphenated or hyphenated string "
  8356. "(including parentheses)."));
  8357. }
  8358. TEST_F(FormatTest, UnderstandPragmaOption) {
  8359. verifyFormat("#pragma option -C -A");
  8360. EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A"));
  8361. }
  8362. #define EXPECT_ALL_STYLES_EQUAL(Styles) \
  8363. for (size_t i = 1; i < Styles.size(); ++i) \
  8364. EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
  8365. << " differs from Style #0"
  8366. TEST_F(FormatTest, GetsPredefinedStyleByName) {
  8367. SmallVector<FormatStyle, 3> Styles;
  8368. Styles.resize(3);
  8369. Styles[0] = getLLVMStyle();
  8370. EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
  8371. EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
  8372. EXPECT_ALL_STYLES_EQUAL(Styles);
  8373. Styles[0] = getGoogleStyle();
  8374. EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
  8375. EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
  8376. EXPECT_ALL_STYLES_EQUAL(Styles);
  8377. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  8378. EXPECT_TRUE(
  8379. getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
  8380. EXPECT_TRUE(
  8381. getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
  8382. EXPECT_ALL_STYLES_EQUAL(Styles);
  8383. Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
  8384. EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
  8385. EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
  8386. EXPECT_ALL_STYLES_EQUAL(Styles);
  8387. Styles[0] = getMozillaStyle();
  8388. EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
  8389. EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
  8390. EXPECT_ALL_STYLES_EQUAL(Styles);
  8391. Styles[0] = getWebKitStyle();
  8392. EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
  8393. EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
  8394. EXPECT_ALL_STYLES_EQUAL(Styles);
  8395. Styles[0] = getGNUStyle();
  8396. EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
  8397. EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
  8398. EXPECT_ALL_STYLES_EQUAL(Styles);
  8399. EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
  8400. }
  8401. TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
  8402. SmallVector<FormatStyle, 8> Styles;
  8403. Styles.resize(2);
  8404. Styles[0] = getGoogleStyle();
  8405. Styles[1] = getLLVMStyle();
  8406. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  8407. EXPECT_ALL_STYLES_EQUAL(Styles);
  8408. Styles.resize(5);
  8409. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  8410. Styles[1] = getLLVMStyle();
  8411. Styles[1].Language = FormatStyle::LK_JavaScript;
  8412. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  8413. Styles[2] = getLLVMStyle();
  8414. Styles[2].Language = FormatStyle::LK_JavaScript;
  8415. EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
  8416. "BasedOnStyle: Google",
  8417. &Styles[2])
  8418. .value());
  8419. Styles[3] = getLLVMStyle();
  8420. Styles[3].Language = FormatStyle::LK_JavaScript;
  8421. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
  8422. "Language: JavaScript",
  8423. &Styles[3])
  8424. .value());
  8425. Styles[4] = getLLVMStyle();
  8426. Styles[4].Language = FormatStyle::LK_JavaScript;
  8427. EXPECT_EQ(0, parseConfiguration("---\n"
  8428. "BasedOnStyle: LLVM\n"
  8429. "IndentWidth: 123\n"
  8430. "---\n"
  8431. "BasedOnStyle: Google\n"
  8432. "Language: JavaScript",
  8433. &Styles[4])
  8434. .value());
  8435. EXPECT_ALL_STYLES_EQUAL(Styles);
  8436. }
  8437. #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \
  8438. Style.FIELD = false; \
  8439. EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \
  8440. EXPECT_TRUE(Style.FIELD); \
  8441. EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \
  8442. EXPECT_FALSE(Style.FIELD);
  8443. #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
  8444. #define CHECK_PARSE(TEXT, FIELD, VALUE) \
  8445. EXPECT_NE(VALUE, Style.FIELD); \
  8446. EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \
  8447. EXPECT_EQ(VALUE, Style.FIELD)
  8448. TEST_F(FormatTest, ParsesConfigurationBools) {
  8449. FormatStyle Style = {};
  8450. Style.Language = FormatStyle::LK_Cpp;
  8451. CHECK_PARSE_BOOL(AlignAfterOpenBracket);
  8452. CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
  8453. CHECK_PARSE_BOOL(AlignOperands);
  8454. CHECK_PARSE_BOOL(AlignTrailingComments);
  8455. CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
  8456. CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
  8457. CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
  8458. CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
  8459. CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
  8460. CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
  8461. CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
  8462. CHECK_PARSE_BOOL(BinPackParameters);
  8463. CHECK_PARSE_BOOL(BinPackArguments);
  8464. CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
  8465. CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
  8466. CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
  8467. CHECK_PARSE_BOOL(DerivePointerAlignment);
  8468. CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
  8469. CHECK_PARSE_BOOL(IndentCaseLabels);
  8470. CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
  8471. CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
  8472. CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
  8473. CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
  8474. CHECK_PARSE_BOOL(Cpp11BracedListStyle);
  8475. CHECK_PARSE_BOOL(SpacesInParentheses);
  8476. CHECK_PARSE_BOOL(SpacesInSquareBrackets);
  8477. CHECK_PARSE_BOOL(SpacesInAngles);
  8478. CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
  8479. CHECK_PARSE_BOOL(SpacesInContainerLiterals);
  8480. CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
  8481. CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
  8482. CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
  8483. }
  8484. #undef CHECK_PARSE_BOOL
  8485. TEST_F(FormatTest, ParsesConfiguration) {
  8486. FormatStyle Style = {};
  8487. Style.Language = FormatStyle::LK_Cpp;
  8488. CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
  8489. CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
  8490. ConstructorInitializerIndentWidth, 1234u);
  8491. CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
  8492. CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
  8493. CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
  8494. CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
  8495. PenaltyBreakBeforeFirstCallParameter, 1234u);
  8496. CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
  8497. CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
  8498. PenaltyReturnTypeOnItsOwnLine, 1234u);
  8499. CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
  8500. SpacesBeforeTrailingComments, 1234u);
  8501. CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
  8502. CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
  8503. Style.PointerAlignment = FormatStyle::PAS_Middle;
  8504. CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
  8505. FormatStyle::PAS_Left);
  8506. CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
  8507. FormatStyle::PAS_Right);
  8508. CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
  8509. FormatStyle::PAS_Middle);
  8510. // For backward compatibility:
  8511. CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
  8512. FormatStyle::PAS_Left);
  8513. CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
  8514. FormatStyle::PAS_Right);
  8515. CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
  8516. FormatStyle::PAS_Middle);
  8517. Style.Standard = FormatStyle::LS_Auto;
  8518. CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
  8519. CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
  8520. CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
  8521. CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
  8522. CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
  8523. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  8524. CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
  8525. BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
  8526. CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
  8527. FormatStyle::BOS_None);
  8528. CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
  8529. FormatStyle::BOS_All);
  8530. // For backward compatibility:
  8531. CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
  8532. FormatStyle::BOS_None);
  8533. CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
  8534. FormatStyle::BOS_All);
  8535. Style.UseTab = FormatStyle::UT_ForIndentation;
  8536. CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
  8537. CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
  8538. CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
  8539. // For backward compatibility:
  8540. CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
  8541. CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
  8542. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  8543. CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
  8544. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
  8545. CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
  8546. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
  8547. CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
  8548. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
  8549. CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
  8550. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
  8551. // For backward compatibility:
  8552. CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
  8553. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
  8554. CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
  8555. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
  8556. Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
  8557. CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
  8558. FormatStyle::SBPO_Never);
  8559. CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
  8560. FormatStyle::SBPO_Always);
  8561. CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
  8562. FormatStyle::SBPO_ControlStatements);
  8563. // For backward compatibility:
  8564. CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
  8565. FormatStyle::SBPO_Never);
  8566. CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
  8567. FormatStyle::SBPO_ControlStatements);
  8568. Style.ColumnLimit = 123;
  8569. FormatStyle BaseStyle = getLLVMStyle();
  8570. CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
  8571. CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
  8572. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  8573. CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
  8574. FormatStyle::BS_Attach);
  8575. CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
  8576. FormatStyle::BS_Linux);
  8577. CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
  8578. FormatStyle::BS_Mozilla);
  8579. CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
  8580. FormatStyle::BS_Stroustrup);
  8581. CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
  8582. FormatStyle::BS_Allman);
  8583. CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
  8584. Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
  8585. CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
  8586. AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
  8587. CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
  8588. AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
  8589. CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
  8590. AlwaysBreakAfterDefinitionReturnType,
  8591. FormatStyle::DRTBS_TopLevel);
  8592. Style.NamespaceIndentation = FormatStyle::NI_All;
  8593. CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
  8594. FormatStyle::NI_None);
  8595. CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
  8596. FormatStyle::NI_Inner);
  8597. CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
  8598. FormatStyle::NI_All);
  8599. Style.ForEachMacros.clear();
  8600. std::vector<std::string> BoostForeach;
  8601. BoostForeach.push_back("BOOST_FOREACH");
  8602. CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
  8603. std::vector<std::string> BoostAndQForeach;
  8604. BoostAndQForeach.push_back("BOOST_FOREACH");
  8605. BoostAndQForeach.push_back("Q_FOREACH");
  8606. CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
  8607. BoostAndQForeach);
  8608. }
  8609. TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
  8610. FormatStyle Style = {};
  8611. Style.Language = FormatStyle::LK_Cpp;
  8612. CHECK_PARSE("Language: Cpp\n"
  8613. "IndentWidth: 12",
  8614. IndentWidth, 12u);
  8615. EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
  8616. "IndentWidth: 34",
  8617. &Style),
  8618. ParseError::Unsuitable);
  8619. EXPECT_EQ(12u, Style.IndentWidth);
  8620. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  8621. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  8622. Style.Language = FormatStyle::LK_JavaScript;
  8623. CHECK_PARSE("Language: JavaScript\n"
  8624. "IndentWidth: 12",
  8625. IndentWidth, 12u);
  8626. CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
  8627. EXPECT_EQ(parseConfiguration("Language: Cpp\n"
  8628. "IndentWidth: 34",
  8629. &Style),
  8630. ParseError::Unsuitable);
  8631. EXPECT_EQ(23u, Style.IndentWidth);
  8632. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  8633. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  8634. CHECK_PARSE("BasedOnStyle: LLVM\n"
  8635. "IndentWidth: 67",
  8636. IndentWidth, 67u);
  8637. CHECK_PARSE("---\n"
  8638. "Language: JavaScript\n"
  8639. "IndentWidth: 12\n"
  8640. "---\n"
  8641. "Language: Cpp\n"
  8642. "IndentWidth: 34\n"
  8643. "...\n",
  8644. IndentWidth, 12u);
  8645. Style.Language = FormatStyle::LK_Cpp;
  8646. CHECK_PARSE("---\n"
  8647. "Language: JavaScript\n"
  8648. "IndentWidth: 12\n"
  8649. "---\n"
  8650. "Language: Cpp\n"
  8651. "IndentWidth: 34\n"
  8652. "...\n",
  8653. IndentWidth, 34u);
  8654. CHECK_PARSE("---\n"
  8655. "IndentWidth: 78\n"
  8656. "---\n"
  8657. "Language: JavaScript\n"
  8658. "IndentWidth: 56\n"
  8659. "...\n",
  8660. IndentWidth, 78u);
  8661. Style.ColumnLimit = 123;
  8662. Style.IndentWidth = 234;
  8663. Style.BreakBeforeBraces = FormatStyle::BS_Linux;
  8664. Style.TabWidth = 345;
  8665. EXPECT_FALSE(parseConfiguration("---\n"
  8666. "IndentWidth: 456\n"
  8667. "BreakBeforeBraces: Allman\n"
  8668. "---\n"
  8669. "Language: JavaScript\n"
  8670. "IndentWidth: 111\n"
  8671. "TabWidth: 111\n"
  8672. "---\n"
  8673. "Language: Cpp\n"
  8674. "BreakBeforeBraces: Stroustrup\n"
  8675. "TabWidth: 789\n"
  8676. "...\n",
  8677. &Style));
  8678. EXPECT_EQ(123u, Style.ColumnLimit);
  8679. EXPECT_EQ(456u, Style.IndentWidth);
  8680. EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
  8681. EXPECT_EQ(789u, Style.TabWidth);
  8682. EXPECT_EQ(parseConfiguration("---\n"
  8683. "Language: JavaScript\n"
  8684. "IndentWidth: 56\n"
  8685. "---\n"
  8686. "IndentWidth: 78\n"
  8687. "...\n",
  8688. &Style),
  8689. ParseError::Error);
  8690. EXPECT_EQ(parseConfiguration("---\n"
  8691. "Language: JavaScript\n"
  8692. "IndentWidth: 56\n"
  8693. "---\n"
  8694. "Language: JavaScript\n"
  8695. "IndentWidth: 78\n"
  8696. "...\n",
  8697. &Style),
  8698. ParseError::Error);
  8699. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  8700. }
  8701. #undef CHECK_PARSE
  8702. TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
  8703. FormatStyle Style = {};
  8704. Style.Language = FormatStyle::LK_JavaScript;
  8705. Style.BreakBeforeTernaryOperators = true;
  8706. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
  8707. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  8708. Style.BreakBeforeTernaryOperators = true;
  8709. EXPECT_EQ(0, parseConfiguration("---\n"
  8710. "BasedOnStyle: Google\n"
  8711. "---\n"
  8712. "Language: JavaScript\n"
  8713. "IndentWidth: 76\n"
  8714. "...\n",
  8715. &Style)
  8716. .value());
  8717. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  8718. EXPECT_EQ(76u, Style.IndentWidth);
  8719. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  8720. }
  8721. TEST_F(FormatTest, ConfigurationRoundTripTest) {
  8722. FormatStyle Style = getLLVMStyle();
  8723. std::string YAML = configurationAsText(Style);
  8724. FormatStyle ParsedStyle = {};
  8725. ParsedStyle.Language = FormatStyle::LK_Cpp;
  8726. EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
  8727. EXPECT_EQ(Style, ParsedStyle);
  8728. }
  8729. TEST_F(FormatTest, WorksFor8bitEncodings) {
  8730. EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
  8731. "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
  8732. "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
  8733. "\"\xef\xee\xf0\xf3...\"",
  8734. format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
  8735. "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
  8736. "\xef\xee\xf0\xf3...\"",
  8737. getLLVMStyleWithColumns(12)));
  8738. }
  8739. TEST_F(FormatTest, HandlesUTF8BOM) {
  8740. EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
  8741. EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
  8742. format("\xef\xbb\xbf#include <iostream>"));
  8743. EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
  8744. format("\xef\xbb\xbf\n#include <iostream>"));
  8745. }
  8746. // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
  8747. #if !defined(_MSC_VER)
  8748. TEST_F(FormatTest, CountsUTF8CharactersProperly) {
  8749. verifyFormat("\"Однажды в студёную зимнюю пору...\"",
  8750. getLLVMStyleWithColumns(35));
  8751. verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
  8752. getLLVMStyleWithColumns(31));
  8753. verifyFormat("// Однажды в студёную зимнюю пору...",
  8754. getLLVMStyleWithColumns(36));
  8755. verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
  8756. verifyFormat("/* Однажды в студёную зимнюю пору... */",
  8757. getLLVMStyleWithColumns(39));
  8758. verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
  8759. getLLVMStyleWithColumns(35));
  8760. }
  8761. TEST_F(FormatTest, SplitsUTF8Strings) {
  8762. // Non-printable characters' width is currently considered to be the length in
  8763. // bytes in UTF8. The characters can be displayed in very different manner
  8764. // (zero-width, single width with a substitution glyph, expanded to their code
  8765. // (e.g. "<8d>"), so there's no single correct way to handle them.
  8766. EXPECT_EQ("\"aaaaÄ\"\n"
  8767. "\"\xc2\x8d\";",
  8768. format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  8769. EXPECT_EQ("\"aaaaaaaÄ\"\n"
  8770. "\"\xc2\x8d\";",
  8771. format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  8772. EXPECT_EQ("\"Однажды, в \"\n"
  8773. "\"студёную \"\n"
  8774. "\"зимнюю \"\n"
  8775. "\"пору,\"",
  8776. format("\"Однажды, в студёную зимнюю пору,\"",
  8777. getLLVMStyleWithColumns(13)));
  8778. EXPECT_EQ(
  8779. "\"一 二 三 \"\n"
  8780. "\"四 五六 \"\n"
  8781. "\"七 八 九 \"\n"
  8782. "\"十\"",
  8783. format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
  8784. EXPECT_EQ("\"一\t二 \"\n"
  8785. "\"\t三 \"\n"
  8786. "\"四 五\t六 \"\n"
  8787. "\"\t七 \"\n"
  8788. "\"八九十\tqq\"",
  8789. format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
  8790. getLLVMStyleWithColumns(11)));
  8791. }
  8792. TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
  8793. EXPECT_EQ("const char *sssss =\n"
  8794. " \"一二三四五六七八\\\n"
  8795. " 九 十\";",
  8796. format("const char *sssss = \"一二三四五六七八\\\n"
  8797. " 九 十\";",
  8798. getLLVMStyleWithColumns(30)));
  8799. }
  8800. TEST_F(FormatTest, SplitsUTF8LineComments) {
  8801. EXPECT_EQ("// aaaaÄ\xc2\x8d",
  8802. format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
  8803. EXPECT_EQ("// Я из лесу\n"
  8804. "// вышел; был\n"
  8805. "// сильный\n"
  8806. "// мороз.",
  8807. format("// Я из лесу вышел; был сильный мороз.",
  8808. getLLVMStyleWithColumns(13)));
  8809. EXPECT_EQ("// 一二三\n"
  8810. "// 四五六七\n"
  8811. "// 八 九\n"
  8812. "// 十",
  8813. format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9)));
  8814. }
  8815. TEST_F(FormatTest, SplitsUTF8BlockComments) {
  8816. EXPECT_EQ("/* Гляжу,\n"
  8817. " * поднимается\n"
  8818. " * медленно в\n"
  8819. " * гору\n"
  8820. " * Лошадка,\n"
  8821. " * везущая\n"
  8822. " * хворосту\n"
  8823. " * воз. */",
  8824. format("/* Гляжу, поднимается медленно в гору\n"
  8825. " * Лошадка, везущая хворосту воз. */",
  8826. getLLVMStyleWithColumns(13)));
  8827. EXPECT_EQ(
  8828. "/* 一二三\n"
  8829. " * 四五六七\n"
  8830. " * 八 九\n"
  8831. " * 十 */",
  8832. format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9)));
  8833. EXPECT_EQ("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
  8834. " * 𝕓𝕪𝕥𝕖\n"
  8835. " * 𝖀𝕿𝕱-𝟠 */",
  8836. format("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12)));
  8837. }
  8838. #endif // _MSC_VER
  8839. TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
  8840. FormatStyle Style = getLLVMStyle();
  8841. Style.ConstructorInitializerIndentWidth = 4;
  8842. verifyFormat(
  8843. "SomeClass::Constructor()\n"
  8844. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  8845. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  8846. Style);
  8847. Style.ConstructorInitializerIndentWidth = 2;
  8848. verifyFormat(
  8849. "SomeClass::Constructor()\n"
  8850. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  8851. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  8852. Style);
  8853. Style.ConstructorInitializerIndentWidth = 0;
  8854. verifyFormat(
  8855. "SomeClass::Constructor()\n"
  8856. ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  8857. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  8858. Style);
  8859. }
  8860. TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
  8861. FormatStyle Style = getLLVMStyle();
  8862. Style.BreakConstructorInitializersBeforeComma = true;
  8863. Style.ConstructorInitializerIndentWidth = 4;
  8864. verifyFormat("SomeClass::Constructor()\n"
  8865. " : a(a)\n"
  8866. " , b(b)\n"
  8867. " , c(c) {}",
  8868. Style);
  8869. verifyFormat("SomeClass::Constructor()\n"
  8870. " : a(a) {}",
  8871. Style);
  8872. Style.ColumnLimit = 0;
  8873. verifyFormat("SomeClass::Constructor()\n"
  8874. " : a(a) {}",
  8875. Style);
  8876. verifyFormat("SomeClass::Constructor()\n"
  8877. " : a(a)\n"
  8878. " , b(b)\n"
  8879. " , c(c) {}",
  8880. Style);
  8881. verifyFormat("SomeClass::Constructor()\n"
  8882. " : a(a) {\n"
  8883. " foo();\n"
  8884. " bar();\n"
  8885. "}",
  8886. Style);
  8887. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  8888. verifyFormat("SomeClass::Constructor()\n"
  8889. " : a(a)\n"
  8890. " , b(b)\n"
  8891. " , c(c) {\n}",
  8892. Style);
  8893. verifyFormat("SomeClass::Constructor()\n"
  8894. " : a(a) {\n}",
  8895. Style);
  8896. Style.ColumnLimit = 80;
  8897. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
  8898. Style.ConstructorInitializerIndentWidth = 2;
  8899. verifyFormat("SomeClass::Constructor()\n"
  8900. " : a(a)\n"
  8901. " , b(b)\n"
  8902. " , c(c) {}",
  8903. Style);
  8904. Style.ConstructorInitializerIndentWidth = 0;
  8905. verifyFormat("SomeClass::Constructor()\n"
  8906. ": a(a)\n"
  8907. ", b(b)\n"
  8908. ", c(c) {}",
  8909. Style);
  8910. Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  8911. Style.ConstructorInitializerIndentWidth = 4;
  8912. verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
  8913. verifyFormat(
  8914. "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
  8915. Style);
  8916. verifyFormat(
  8917. "SomeClass::Constructor()\n"
  8918. " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
  8919. Style);
  8920. Style.ConstructorInitializerIndentWidth = 4;
  8921. Style.ColumnLimit = 60;
  8922. verifyFormat("SomeClass::Constructor()\n"
  8923. " : aaaaaaaa(aaaaaaaa)\n"
  8924. " , aaaaaaaa(aaaaaaaa)\n"
  8925. " , aaaaaaaa(aaaaaaaa) {}",
  8926. Style);
  8927. }
  8928. TEST_F(FormatTest, Destructors) {
  8929. verifyFormat("void F(int &i) { i.~int(); }");
  8930. verifyFormat("void F(int &i) { i->~int(); }");
  8931. }
  8932. TEST_F(FormatTest, FormatsWithWebKitStyle) {
  8933. FormatStyle Style = getWebKitStyle();
  8934. // Don't indent in outer namespaces.
  8935. verifyFormat("namespace outer {\n"
  8936. "int i;\n"
  8937. "namespace inner {\n"
  8938. " int i;\n"
  8939. "} // namespace inner\n"
  8940. "} // namespace outer\n"
  8941. "namespace other_outer {\n"
  8942. "int i;\n"
  8943. "}",
  8944. Style);
  8945. // Don't indent case labels.
  8946. verifyFormat("switch (variable) {\n"
  8947. "case 1:\n"
  8948. "case 2:\n"
  8949. " doSomething();\n"
  8950. " break;\n"
  8951. "default:\n"
  8952. " ++variable;\n"
  8953. "}",
  8954. Style);
  8955. // Wrap before binary operators.
  8956. EXPECT_EQ("void f()\n"
  8957. "{\n"
  8958. " if (aaaaaaaaaaaaaaaa\n"
  8959. " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
  8960. " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  8961. " return;\n"
  8962. "}",
  8963. format("void f() {\n"
  8964. "if (aaaaaaaaaaaaaaaa\n"
  8965. "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
  8966. "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  8967. "return;\n"
  8968. "}",
  8969. Style));
  8970. // Allow functions on a single line.
  8971. verifyFormat("void f() { return; }", Style);
  8972. // Constructor initializers are formatted one per line with the "," on the
  8973. // new line.
  8974. verifyFormat("Constructor()\n"
  8975. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  8976. " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
  8977. " aaaaaaaaaaaaaa)\n"
  8978. " , aaaaaaaaaaaaaaaaaaaaaaa()\n"
  8979. "{\n"
  8980. "}",
  8981. Style);
  8982. verifyFormat("SomeClass::Constructor()\n"
  8983. " : a(a)\n"
  8984. "{\n"
  8985. "}",
  8986. Style);
  8987. EXPECT_EQ("SomeClass::Constructor()\n"
  8988. " : a(a)\n"
  8989. "{\n"
  8990. "}",
  8991. format("SomeClass::Constructor():a(a){}", Style));
  8992. verifyFormat("SomeClass::Constructor()\n"
  8993. " : a(a)\n"
  8994. " , b(b)\n"
  8995. " , c(c)\n"
  8996. "{\n"
  8997. "}",
  8998. Style);
  8999. verifyFormat("SomeClass::Constructor()\n"
  9000. " : a(a)\n"
  9001. "{\n"
  9002. " foo();\n"
  9003. " bar();\n"
  9004. "}",
  9005. Style);
  9006. // Access specifiers should be aligned left.
  9007. verifyFormat("class C {\n"
  9008. "public:\n"
  9009. " int i;\n"
  9010. "};",
  9011. Style);
  9012. // Do not align comments.
  9013. verifyFormat("int a; // Do not\n"
  9014. "double b; // align comments.",
  9015. Style);
  9016. // Do not align operands.
  9017. EXPECT_EQ("ASSERT(aaaa\n"
  9018. " || bbbb);",
  9019. format("ASSERT ( aaaa\n||bbbb);", Style));
  9020. // Accept input's line breaks.
  9021. EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
  9022. " || bbbbbbbbbbbbbbb) {\n"
  9023. " i++;\n"
  9024. "}",
  9025. format("if (aaaaaaaaaaaaaaa\n"
  9026. "|| bbbbbbbbbbbbbbb) { i++; }",
  9027. Style));
  9028. EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
  9029. " i++;\n"
  9030. "}",
  9031. format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
  9032. // Don't automatically break all macro definitions (llvm.org/PR17842).
  9033. verifyFormat("#define aNumber 10", Style);
  9034. // However, generally keep the line breaks that the user authored.
  9035. EXPECT_EQ("#define aNumber \\\n"
  9036. " 10",
  9037. format("#define aNumber \\\n"
  9038. " 10",
  9039. Style));
  9040. // Keep empty and one-element array literals on a single line.
  9041. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
  9042. " copyItems:YES];",
  9043. format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
  9044. "copyItems:YES];",
  9045. Style));
  9046. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
  9047. " copyItems:YES];",
  9048. format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
  9049. " copyItems:YES];",
  9050. Style));
  9051. // FIXME: This does not seem right, there should be more indentation before
  9052. // the array literal's entries. Nested blocks have the same problem.
  9053. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
  9054. " @\"a\",\n"
  9055. " @\"a\"\n"
  9056. "]\n"
  9057. " copyItems:YES];",
  9058. format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
  9059. " @\"a\",\n"
  9060. " @\"a\"\n"
  9061. " ]\n"
  9062. " copyItems:YES];",
  9063. Style));
  9064. EXPECT_EQ(
  9065. "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
  9066. " copyItems:YES];",
  9067. format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
  9068. " copyItems:YES];",
  9069. Style));
  9070. verifyFormat("[self.a b:c c:d];", Style);
  9071. EXPECT_EQ("[self.a b:c\n"
  9072. " c:d];",
  9073. format("[self.a b:c\n"
  9074. "c:d];",
  9075. Style));
  9076. }
  9077. TEST_F(FormatTest, FormatsLambdas) {
  9078. verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
  9079. verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
  9080. verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
  9081. verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
  9082. verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
  9083. verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
  9084. verifyFormat("void f() {\n"
  9085. " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
  9086. "}\n");
  9087. verifyFormat("void f() {\n"
  9088. " other(x.begin(), //\n"
  9089. " x.end(), //\n"
  9090. " [&](int, int) { return 1; });\n"
  9091. "}\n");
  9092. verifyFormat("SomeFunction([]() { // A cool function...\n"
  9093. " return 43;\n"
  9094. "});");
  9095. EXPECT_EQ("SomeFunction([]() {\n"
  9096. "#define A a\n"
  9097. " return 43;\n"
  9098. "});",
  9099. format("SomeFunction([](){\n"
  9100. "#define A a\n"
  9101. "return 43;\n"
  9102. "});"));
  9103. verifyFormat("void f() {\n"
  9104. " SomeFunction([](decltype(x), A *a) {});\n"
  9105. "}");
  9106. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  9107. " [](const aaaaaaaaaa &a) { return a; });");
  9108. verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
  9109. " SomeOtherFunctioooooooooooooooooooooooooon();\n"
  9110. "});");
  9111. verifyFormat("Constructor()\n"
  9112. " : Field([] { // comment\n"
  9113. " int i;\n"
  9114. " }) {}");
  9115. verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
  9116. " return some_parameter.size();\n"
  9117. "};");
  9118. verifyFormat("int i = aaaaaa ? 1 //\n"
  9119. " : [] {\n"
  9120. " return 2; //\n"
  9121. " }();");
  9122. verifyFormat("llvm::errs() << \"number of twos is \"\n"
  9123. " << std::count_if(v.begin(), v.end(), [](int x) {\n"
  9124. " return x == 2; // force break\n"
  9125. " });");
  9126. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n"
  9127. " int iiiiiiiiiiii) {\n"
  9128. " return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n"
  9129. "});",
  9130. getLLVMStyleWithColumns(60));
  9131. verifyFormat("SomeFunction({[&] {\n"
  9132. " // comment\n"
  9133. " },\n"
  9134. " [&] {\n"
  9135. " // comment\n"
  9136. " }});");
  9137. verifyFormat("SomeFunction({[&] {\n"
  9138. " // comment\n"
  9139. "}});");
  9140. // Lambdas with return types.
  9141. verifyFormat("int c = []() -> int { return 2; }();\n");
  9142. verifyFormat("int c = []() -> int * { return 2; }();\n");
  9143. verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
  9144. verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
  9145. verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
  9146. verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
  9147. verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
  9148. verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
  9149. verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
  9150. " int j) -> int {\n"
  9151. " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
  9152. "};");
  9153. verifyFormat(
  9154. "aaaaaaaaaaaaaaaaaaaaaa(\n"
  9155. " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
  9156. " return aaaaaaaaaaaaaaaaa;\n"
  9157. " });",
  9158. getLLVMStyleWithColumns(70));
  9159. // Multiple lambdas in the same parentheses change indentation rules.
  9160. verifyFormat("SomeFunction(\n"
  9161. " []() {\n"
  9162. " int i = 42;\n"
  9163. " return i;\n"
  9164. " },\n"
  9165. " []() {\n"
  9166. " int j = 43;\n"
  9167. " return j;\n"
  9168. " });");
  9169. // More complex introducers.
  9170. verifyFormat("return [i, args...] {};");
  9171. // Not lambdas.
  9172. verifyFormat("constexpr char hello[]{\"hello\"};");
  9173. verifyFormat("double &operator[](int i) { return 0; }\n"
  9174. "int i;");
  9175. verifyFormat("std::unique_ptr<int[]> foo() {}");
  9176. verifyFormat("int i = a[a][a]->f();");
  9177. verifyFormat("int i = (*b)[a]->f();");
  9178. // Other corner cases.
  9179. verifyFormat("void f() {\n"
  9180. " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
  9181. " );\n"
  9182. "}");
  9183. // Lambdas created through weird macros.
  9184. verifyFormat("void f() {\n"
  9185. " MACRO((const AA &a) { return 1; });\n"
  9186. "}");
  9187. verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
  9188. " doo_dah();\n"
  9189. " doo_dah();\n"
  9190. " })) {\n"
  9191. "}");
  9192. verifyFormat("auto lambda = []() {\n"
  9193. " int a = 2\n"
  9194. "#if A\n"
  9195. " + 2\n"
  9196. "#endif\n"
  9197. " ;\n"
  9198. "};");
  9199. }
  9200. TEST_F(FormatTest, FormatsBlocks) {
  9201. FormatStyle ShortBlocks = getLLVMStyle();
  9202. ShortBlocks.AllowShortBlocksOnASingleLine = true;
  9203. verifyFormat("int (^Block)(int, int);", ShortBlocks);
  9204. verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
  9205. verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
  9206. verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
  9207. verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
  9208. verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
  9209. verifyFormat("foo(^{ bar(); });", ShortBlocks);
  9210. verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
  9211. verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
  9212. verifyFormat("[operation setCompletionBlock:^{\n"
  9213. " [self onOperationDone];\n"
  9214. "}];");
  9215. verifyFormat("int i = {[operation setCompletionBlock:^{\n"
  9216. " [self onOperationDone];\n"
  9217. "}]};");
  9218. verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
  9219. " f();\n"
  9220. "}];");
  9221. verifyFormat("int a = [operation block:^int(int *i) {\n"
  9222. " return 1;\n"
  9223. "}];");
  9224. verifyFormat("[myObject doSomethingWith:arg1\n"
  9225. " aaa:^int(int *a) {\n"
  9226. " return 1;\n"
  9227. " }\n"
  9228. " bbb:f(a * bbbbbbbb)];");
  9229. verifyFormat("[operation setCompletionBlock:^{\n"
  9230. " [self.delegate newDataAvailable];\n"
  9231. "}];",
  9232. getLLVMStyleWithColumns(60));
  9233. verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
  9234. " NSString *path = [self sessionFilePath];\n"
  9235. " if (path) {\n"
  9236. " // ...\n"
  9237. " }\n"
  9238. "});");
  9239. verifyFormat("[[SessionService sharedService]\n"
  9240. " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  9241. " if (window) {\n"
  9242. " [self windowDidLoad:window];\n"
  9243. " } else {\n"
  9244. " [self errorLoadingWindow];\n"
  9245. " }\n"
  9246. " }];");
  9247. verifyFormat("void (^largeBlock)(void) = ^{\n"
  9248. " // ...\n"
  9249. "};\n",
  9250. getLLVMStyleWithColumns(40));
  9251. verifyFormat("[[SessionService sharedService]\n"
  9252. " loadWindowWithCompletionBlock: //\n"
  9253. " ^(SessionWindow *window) {\n"
  9254. " if (window) {\n"
  9255. " [self windowDidLoad:window];\n"
  9256. " } else {\n"
  9257. " [self errorLoadingWindow];\n"
  9258. " }\n"
  9259. " }];",
  9260. getLLVMStyleWithColumns(60));
  9261. verifyFormat("[myObject doSomethingWith:arg1\n"
  9262. " firstBlock:^(Foo *a) {\n"
  9263. " // ...\n"
  9264. " int i;\n"
  9265. " }\n"
  9266. " secondBlock:^(Bar *b) {\n"
  9267. " // ...\n"
  9268. " int i;\n"
  9269. " }\n"
  9270. " thirdBlock:^Foo(Bar *b) {\n"
  9271. " // ...\n"
  9272. " int i;\n"
  9273. " }];");
  9274. verifyFormat("[myObject doSomethingWith:arg1\n"
  9275. " firstBlock:-1\n"
  9276. " secondBlock:^(Bar *b) {\n"
  9277. " // ...\n"
  9278. " int i;\n"
  9279. " }];");
  9280. verifyFormat("f(^{\n"
  9281. " @autoreleasepool {\n"
  9282. " if (a) {\n"
  9283. " g();\n"
  9284. " }\n"
  9285. " }\n"
  9286. "});");
  9287. verifyFormat("Block b = ^int *(A *a, B *b) {}");
  9288. FormatStyle FourIndent = getLLVMStyle();
  9289. FourIndent.ObjCBlockIndentWidth = 4;
  9290. verifyFormat("[operation setCompletionBlock:^{\n"
  9291. " [self onOperationDone];\n"
  9292. "}];",
  9293. FourIndent);
  9294. }
  9295. TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
  9296. FormatStyle ZeroColumn = getLLVMStyle();
  9297. ZeroColumn.ColumnLimit = 0;
  9298. verifyFormat("[[SessionService sharedService] "
  9299. "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  9300. " if (window) {\n"
  9301. " [self windowDidLoad:window];\n"
  9302. " } else {\n"
  9303. " [self errorLoadingWindow];\n"
  9304. " }\n"
  9305. "}];",
  9306. ZeroColumn);
  9307. EXPECT_EQ("[[SessionService sharedService]\n"
  9308. " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  9309. " if (window) {\n"
  9310. " [self windowDidLoad:window];\n"
  9311. " } else {\n"
  9312. " [self errorLoadingWindow];\n"
  9313. " }\n"
  9314. " }];",
  9315. format("[[SessionService sharedService]\n"
  9316. "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  9317. " if (window) {\n"
  9318. " [self windowDidLoad:window];\n"
  9319. " } else {\n"
  9320. " [self errorLoadingWindow];\n"
  9321. " }\n"
  9322. "}];",
  9323. ZeroColumn));
  9324. verifyFormat("[myObject doSomethingWith:arg1\n"
  9325. " firstBlock:^(Foo *a) {\n"
  9326. " // ...\n"
  9327. " int i;\n"
  9328. " }\n"
  9329. " secondBlock:^(Bar *b) {\n"
  9330. " // ...\n"
  9331. " int i;\n"
  9332. " }\n"
  9333. " thirdBlock:^Foo(Bar *b) {\n"
  9334. " // ...\n"
  9335. " int i;\n"
  9336. " }];",
  9337. ZeroColumn);
  9338. verifyFormat("f(^{\n"
  9339. " @autoreleasepool {\n"
  9340. " if (a) {\n"
  9341. " g();\n"
  9342. " }\n"
  9343. " }\n"
  9344. "});",
  9345. ZeroColumn);
  9346. verifyFormat("void (^largeBlock)(void) = ^{\n"
  9347. " // ...\n"
  9348. "};",
  9349. ZeroColumn);
  9350. ZeroColumn.AllowShortBlocksOnASingleLine = true;
  9351. EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
  9352. format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn));
  9353. ZeroColumn.AllowShortBlocksOnASingleLine = false;
  9354. EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
  9355. " int i;\n"
  9356. "};",
  9357. format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn));
  9358. }
  9359. TEST_F(FormatTest, SupportsCRLF) {
  9360. EXPECT_EQ("int a;\r\n"
  9361. "int b;\r\n"
  9362. "int c;\r\n",
  9363. format("int a;\r\n"
  9364. " int b;\r\n"
  9365. " int c;\r\n",
  9366. getLLVMStyle()));
  9367. EXPECT_EQ("int a;\r\n"
  9368. "int b;\r\n"
  9369. "int c;\r\n",
  9370. format("int a;\r\n"
  9371. " int b;\n"
  9372. " int c;\r\n",
  9373. getLLVMStyle()));
  9374. EXPECT_EQ("int a;\n"
  9375. "int b;\n"
  9376. "int c;\n",
  9377. format("int a;\r\n"
  9378. " int b;\n"
  9379. " int c;\n",
  9380. getLLVMStyle()));
  9381. EXPECT_EQ("\"aaaaaaa \"\r\n"
  9382. "\"bbbbbbb\";\r\n",
  9383. format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
  9384. EXPECT_EQ("#define A \\\r\n"
  9385. " b; \\\r\n"
  9386. " c; \\\r\n"
  9387. " d;\r\n",
  9388. format("#define A \\\r\n"
  9389. " b; \\\r\n"
  9390. " c; d; \r\n",
  9391. getGoogleStyle()));
  9392. EXPECT_EQ("/*\r\n"
  9393. "multi line block comments\r\n"
  9394. "should not introduce\r\n"
  9395. "an extra carriage return\r\n"
  9396. "*/\r\n",
  9397. format("/*\r\n"
  9398. "multi line block comments\r\n"
  9399. "should not introduce\r\n"
  9400. "an extra carriage return\r\n"
  9401. "*/\r\n"));
  9402. }
  9403. TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
  9404. verifyFormat("MY_CLASS(C) {\n"
  9405. " int i;\n"
  9406. " int j;\n"
  9407. "};");
  9408. }
  9409. TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
  9410. FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
  9411. TwoIndent.ContinuationIndentWidth = 2;
  9412. EXPECT_EQ("int i =\n"
  9413. " longFunction(\n"
  9414. " arg);",
  9415. format("int i = longFunction(arg);", TwoIndent));
  9416. FormatStyle SixIndent = getLLVMStyleWithColumns(20);
  9417. SixIndent.ContinuationIndentWidth = 6;
  9418. EXPECT_EQ("int i =\n"
  9419. " longFunction(\n"
  9420. " arg);",
  9421. format("int i = longFunction(arg);", SixIndent));
  9422. }
  9423. TEST_F(FormatTest, SpacesInAngles) {
  9424. FormatStyle Spaces = getLLVMStyle();
  9425. Spaces.SpacesInAngles = true;
  9426. verifyFormat("static_cast< int >(arg);", Spaces);
  9427. verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
  9428. verifyFormat("f< int, float >();", Spaces);
  9429. verifyFormat("template <> g() {}", Spaces);
  9430. verifyFormat("template < std::vector< int > > f() {}", Spaces);
  9431. Spaces.Standard = FormatStyle::LS_Cpp03;
  9432. Spaces.SpacesInAngles = true;
  9433. verifyFormat("A< A< int > >();", Spaces);
  9434. Spaces.SpacesInAngles = false;
  9435. verifyFormat("A<A<int> >();", Spaces);
  9436. Spaces.Standard = FormatStyle::LS_Cpp11;
  9437. Spaces.SpacesInAngles = true;
  9438. verifyFormat("A< A< int > >();", Spaces);
  9439. Spaces.SpacesInAngles = false;
  9440. verifyFormat("A<A<int>>();", Spaces);
  9441. }
  9442. TEST_F(FormatTest, TripleAngleBrackets) {
  9443. verifyFormat("f<<<1, 1>>>();");
  9444. verifyFormat("f<<<1, 1, 1, s>>>();");
  9445. verifyFormat("f<<<a, b, c, d>>>();");
  9446. EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
  9447. verifyFormat("f<param><<<1, 1>>>();");
  9448. verifyFormat("f<1><<<1, 1>>>();");
  9449. EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
  9450. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  9451. "aaaaaaaaaaa<<<\n 1, 1>>>();");
  9452. }
  9453. TEST_F(FormatTest, MergeLessLessAtEnd) {
  9454. verifyFormat("<<");
  9455. EXPECT_EQ("< < <", format("\\\n<<<"));
  9456. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  9457. "aaallvm::outs() <<");
  9458. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  9459. "aaaallvm::outs()\n <<");
  9460. }
  9461. TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
  9462. std::string code = "#if A\n"
  9463. "#if B\n"
  9464. "a.\n"
  9465. "#endif\n"
  9466. " a = 1;\n"
  9467. "#else\n"
  9468. "#endif\n"
  9469. "#if C\n"
  9470. "#else\n"
  9471. "#endif\n";
  9472. EXPECT_EQ(code, format(code));
  9473. }
  9474. TEST_F(FormatTest, HandleConflictMarkers) {
  9475. // Git/SVN conflict markers.
  9476. EXPECT_EQ("int a;\n"
  9477. "void f() {\n"
  9478. " callme(some(parameter1,\n"
  9479. "<<<<<<< text by the vcs\n"
  9480. " parameter2),\n"
  9481. "||||||| text by the vcs\n"
  9482. " parameter2),\n"
  9483. " parameter3,\n"
  9484. "======= text by the vcs\n"
  9485. " parameter2, parameter3),\n"
  9486. ">>>>>>> text by the vcs\n"
  9487. " otherparameter);\n",
  9488. format("int a;\n"
  9489. "void f() {\n"
  9490. " callme(some(parameter1,\n"
  9491. "<<<<<<< text by the vcs\n"
  9492. " parameter2),\n"
  9493. "||||||| text by the vcs\n"
  9494. " parameter2),\n"
  9495. " parameter3,\n"
  9496. "======= text by the vcs\n"
  9497. " parameter2,\n"
  9498. " parameter3),\n"
  9499. ">>>>>>> text by the vcs\n"
  9500. " otherparameter);\n"));
  9501. // Perforce markers.
  9502. EXPECT_EQ("void f() {\n"
  9503. " function(\n"
  9504. ">>>> text by the vcs\n"
  9505. " parameter,\n"
  9506. "==== text by the vcs\n"
  9507. " parameter,\n"
  9508. "==== text by the vcs\n"
  9509. " parameter,\n"
  9510. "<<<< text by the vcs\n"
  9511. " parameter);\n",
  9512. format("void f() {\n"
  9513. " function(\n"
  9514. ">>>> text by the vcs\n"
  9515. " parameter,\n"
  9516. "==== text by the vcs\n"
  9517. " parameter,\n"
  9518. "==== text by the vcs\n"
  9519. " parameter,\n"
  9520. "<<<< text by the vcs\n"
  9521. " parameter);\n"));
  9522. EXPECT_EQ("<<<<<<<\n"
  9523. "|||||||\n"
  9524. "=======\n"
  9525. ">>>>>>>",
  9526. format("<<<<<<<\n"
  9527. "|||||||\n"
  9528. "=======\n"
  9529. ">>>>>>>"));
  9530. EXPECT_EQ("<<<<<<<\n"
  9531. "|||||||\n"
  9532. "int i;\n"
  9533. "=======\n"
  9534. ">>>>>>>",
  9535. format("<<<<<<<\n"
  9536. "|||||||\n"
  9537. "int i;\n"
  9538. "=======\n"
  9539. ">>>>>>>"));
  9540. // FIXME: Handle parsing of macros around conflict markers correctly:
  9541. EXPECT_EQ("#define Macro \\\n"
  9542. "<<<<<<<\n"
  9543. "Something \\\n"
  9544. "|||||||\n"
  9545. "Else \\\n"
  9546. "=======\n"
  9547. "Other \\\n"
  9548. ">>>>>>>\n"
  9549. " End int i;\n",
  9550. format("#define Macro \\\n"
  9551. "<<<<<<<\n"
  9552. " Something \\\n"
  9553. "|||||||\n"
  9554. " Else \\\n"
  9555. "=======\n"
  9556. " Other \\\n"
  9557. ">>>>>>>\n"
  9558. " End\n"
  9559. "int i;\n"));
  9560. }
  9561. TEST_F(FormatTest, DisableRegions) {
  9562. EXPECT_EQ("int i;\n"
  9563. "// clang-format off\n"
  9564. " int j;\n"
  9565. "// clang-format on\n"
  9566. "int k;",
  9567. format(" int i;\n"
  9568. " // clang-format off\n"
  9569. " int j;\n"
  9570. " // clang-format on\n"
  9571. " int k;"));
  9572. EXPECT_EQ("int i;\n"
  9573. "/* clang-format off */\n"
  9574. " int j;\n"
  9575. "/* clang-format on */\n"
  9576. "int k;",
  9577. format(" int i;\n"
  9578. " /* clang-format off */\n"
  9579. " int j;\n"
  9580. " /* clang-format on */\n"
  9581. " int k;"));
  9582. }
  9583. TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
  9584. format("? ) =");
  9585. verifyNoCrash("#define a\\\n /**/}");
  9586. }
  9587. } // end namespace
  9588. } // end namespace format
  9589. } // end namespace clang