FormatTest.cpp 361 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751
  1. //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "FormatTestUtils.h"
  10. #include "clang/Format/Format.h"
  11. #include "llvm/Support/Debug.h"
  12. #include "gtest/gtest.h"
  13. #define DEBUG_TYPE "format-test"
  14. namespace clang {
  15. namespace format {
  16. FormatStyle getGoogleStyle() {
  17. return getGoogleStyle(FormatStyle::LK_Cpp);
  18. }
  19. class FormatTest : public ::testing::Test {
  20. protected:
  21. std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length,
  22. const FormatStyle &Style) {
  23. DEBUG(llvm::errs() << "---\n");
  24. DEBUG(llvm::errs() << Code << "\n\n");
  25. std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
  26. tooling::Replacements Replaces = reformat(Style, Code, Ranges);
  27. ReplacementCount = Replaces.size();
  28. std::string Result = applyAllReplacements(Code, Replaces);
  29. EXPECT_NE("", Result);
  30. DEBUG(llvm::errs() << "\n" << Result << "\n\n");
  31. return Result;
  32. }
  33. std::string
  34. format(llvm::StringRef Code, const FormatStyle &Style = getLLVMStyle()) {
  35. return format(Code, 0, Code.size(), Style);
  36. }
  37. FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
  38. FormatStyle Style = getLLVMStyle();
  39. Style.ColumnLimit = ColumnLimit;
  40. return Style;
  41. }
  42. FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
  43. FormatStyle Style = getGoogleStyle();
  44. Style.ColumnLimit = ColumnLimit;
  45. return Style;
  46. }
  47. void verifyFormat(llvm::StringRef Code,
  48. const FormatStyle &Style = getLLVMStyle()) {
  49. EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
  50. }
  51. void verifyGoogleFormat(llvm::StringRef Code) {
  52. verifyFormat(Code, getGoogleStyle());
  53. }
  54. void verifyIndependentOfContext(llvm::StringRef text) {
  55. verifyFormat(text);
  56. verifyFormat(llvm::Twine("void f() { " + text + " }").str());
  57. }
  58. int ReplacementCount;
  59. };
  60. TEST_F(FormatTest, MessUp) {
  61. EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
  62. EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
  63. EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
  64. EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
  65. EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
  66. }
  67. //===----------------------------------------------------------------------===//
  68. // Basic function tests.
  69. //===----------------------------------------------------------------------===//
  70. TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
  71. EXPECT_EQ(";", format(";"));
  72. }
  73. TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
  74. EXPECT_EQ("int i;", format(" int i;"));
  75. EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;"));
  76. EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
  77. EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
  78. }
  79. TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
  80. EXPECT_EQ("int i;", format("int\ni;"));
  81. }
  82. TEST_F(FormatTest, FormatsNestedBlockStatements) {
  83. EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
  84. }
  85. TEST_F(FormatTest, FormatsNestedCall) {
  86. verifyFormat("Method(f1, f2(f3));");
  87. verifyFormat("Method(f1(f2, f3()));");
  88. verifyFormat("Method(f1(f2, (f3())));");
  89. }
  90. TEST_F(FormatTest, NestedNameSpecifiers) {
  91. verifyFormat("vector<::Type> v;");
  92. verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
  93. verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
  94. verifyFormat("bool a = 2 < ::SomeFunction();");
  95. }
  96. TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
  97. EXPECT_EQ("if (a) {\n"
  98. " f();\n"
  99. "}",
  100. format("if(a){f();}"));
  101. EXPECT_EQ(4, ReplacementCount);
  102. EXPECT_EQ("if (a) {\n"
  103. " f();\n"
  104. "}",
  105. format("if (a) {\n"
  106. " f();\n"
  107. "}"));
  108. EXPECT_EQ(0, ReplacementCount);
  109. }
  110. TEST_F(FormatTest, RemovesTrailingWhitespaceOfFormattedLine) {
  111. EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0, getLLVMStyle()));
  112. EXPECT_EQ("int a;", format("int a; "));
  113. EXPECT_EQ("int a;\n", format("int a; \n \n \n "));
  114. EXPECT_EQ("int a;\nint b; ",
  115. format("int a; \nint b; ", 0, 0, getLLVMStyle()));
  116. }
  117. TEST_F(FormatTest, FormatsCorrectRegionForLeadingWhitespace) {
  118. EXPECT_EQ("int b;\nint a;",
  119. format("int b;\n int a;", 7, 0, getLLVMStyle()));
  120. EXPECT_EQ("int b;\n int a;",
  121. format("int b;\n int a;", 6, 0, getLLVMStyle()));
  122. EXPECT_EQ("#define A \\\n"
  123. " int a; \\\n"
  124. " int b;",
  125. format("#define A \\\n"
  126. " int a; \\\n"
  127. " int b;",
  128. 26, 0, getLLVMStyleWithColumns(12)));
  129. EXPECT_EQ("#define A \\\n"
  130. " int a; \\\n"
  131. " int b;",
  132. format("#define A \\\n"
  133. " int a; \\\n"
  134. " int b;",
  135. 25, 0, getLLVMStyleWithColumns(12)));
  136. }
  137. TEST_F(FormatTest, FormatLineWhenInvokedOnTrailingNewline) {
  138. EXPECT_EQ("int b;\n\nint a;",
  139. format("int b;\n\nint a;", 8, 0, getLLVMStyle()));
  140. EXPECT_EQ("int b;\n\nint a;",
  141. format("int b;\n\nint a;", 7, 0, getLLVMStyle()));
  142. // This might not strictly be correct, but is likely good in all practical
  143. // cases.
  144. EXPECT_EQ("int b;\nint a;",
  145. format("int b;int a;", 7, 0, getLLVMStyle()));
  146. }
  147. TEST_F(FormatTest, RemovesWhitespaceWhenTriggeredOnEmptyLine) {
  148. EXPECT_EQ("int a;\n\n int b;",
  149. format("int a;\n \n\n int b;", 8, 0, getLLVMStyle()));
  150. EXPECT_EQ("int a;\n\n int b;",
  151. format("int a;\n \n\n int b;", 9, 0, getLLVMStyle()));
  152. }
  153. TEST_F(FormatTest, RemovesEmptyLines) {
  154. EXPECT_EQ("class C {\n"
  155. " int i;\n"
  156. "};",
  157. format("class C {\n"
  158. " int i;\n"
  159. "\n"
  160. "};"));
  161. // Don't remove empty lines at the start of namespaces or extern "C" blocks.
  162. EXPECT_EQ("namespace N {\n"
  163. "\n"
  164. "int i;\n"
  165. "}",
  166. format("namespace N {\n"
  167. "\n"
  168. "int i;\n"
  169. "}",
  170. getGoogleStyle()));
  171. EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
  172. "\n"
  173. "int i;\n"
  174. "}",
  175. format("extern /**/ \"C\" /**/ {\n"
  176. "\n"
  177. "int i;\n"
  178. "}",
  179. getGoogleStyle()));
  180. // ...but do keep inlining and removing empty lines for non-block extern "C"
  181. // functions.
  182. verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
  183. EXPECT_EQ("extern \"C\" int f() {\n"
  184. " int i = 42;\n"
  185. " return i;\n"
  186. "}",
  187. format("extern \"C\" int f() {\n"
  188. "\n"
  189. " int i = 42;\n"
  190. " return i;\n"
  191. "}",
  192. getGoogleStyle()));
  193. // Remove empty lines at the beginning and end of blocks.
  194. EXPECT_EQ("void f() {\n"
  195. "\n"
  196. " if (a) {\n"
  197. "\n"
  198. " f();\n"
  199. " }\n"
  200. "}",
  201. format("void f() {\n"
  202. "\n"
  203. " if (a) {\n"
  204. "\n"
  205. " f();\n"
  206. "\n"
  207. " }\n"
  208. "\n"
  209. "}",
  210. getLLVMStyle()));
  211. EXPECT_EQ("void f() {\n"
  212. " if (a) {\n"
  213. " f();\n"
  214. " }\n"
  215. "}",
  216. format("void f() {\n"
  217. "\n"
  218. " if (a) {\n"
  219. "\n"
  220. " f();\n"
  221. "\n"
  222. " }\n"
  223. "\n"
  224. "}",
  225. getGoogleStyle()));
  226. // Don't remove empty lines in more complex control statements.
  227. EXPECT_EQ("void f() {\n"
  228. " if (a) {\n"
  229. " f();\n"
  230. "\n"
  231. " } else if (b) {\n"
  232. " f();\n"
  233. " }\n"
  234. "}",
  235. format("void f() {\n"
  236. " if (a) {\n"
  237. " f();\n"
  238. "\n"
  239. " } else if (b) {\n"
  240. " f();\n"
  241. "\n"
  242. " }\n"
  243. "\n"
  244. "}"));
  245. // FIXME: This is slightly inconsistent.
  246. EXPECT_EQ("namespace {\n"
  247. "int i;\n"
  248. "}",
  249. format("namespace {\n"
  250. "int i;\n"
  251. "\n"
  252. "}"));
  253. EXPECT_EQ("namespace {\n"
  254. "int i;\n"
  255. "\n"
  256. "} // namespace",
  257. format("namespace {\n"
  258. "int i;\n"
  259. "\n"
  260. "} // namespace"));
  261. }
  262. TEST_F(FormatTest, ReformatsMovedLines) {
  263. EXPECT_EQ(
  264. "template <typename T> T *getFETokenInfo() const {\n"
  265. " return static_cast<T *>(FETokenInfo);\n"
  266. "}\n"
  267. " int a; // <- Should not be formatted",
  268. format(
  269. "template<typename T>\n"
  270. "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
  271. " int a; // <- Should not be formatted",
  272. 9, 5, getLLVMStyle()));
  273. }
  274. TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
  275. verifyFormat("x = (a) and (b);");
  276. verifyFormat("x = (a) or (b);");
  277. verifyFormat("x = (a) bitand (b);");
  278. verifyFormat("x = (a) bitor (b);");
  279. verifyFormat("x = (a) not_eq (b);");
  280. verifyFormat("x = (a) and_eq (b);");
  281. verifyFormat("x = (a) or_eq (b);");
  282. verifyFormat("x = (a) xor (b);");
  283. }
  284. //===----------------------------------------------------------------------===//
  285. // Tests for control statements.
  286. //===----------------------------------------------------------------------===//
  287. TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
  288. verifyFormat("if (true)\n f();\ng();");
  289. verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
  290. verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
  291. FormatStyle AllowsMergedIf = getLLVMStyle();
  292. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  293. verifyFormat("if (a)\n"
  294. " // comment\n"
  295. " f();",
  296. AllowsMergedIf);
  297. verifyFormat("if (a)\n"
  298. " ;",
  299. AllowsMergedIf);
  300. verifyFormat("if (a)\n"
  301. " if (b) return;",
  302. AllowsMergedIf);
  303. verifyFormat("if (a) // Can't merge this\n"
  304. " f();\n",
  305. AllowsMergedIf);
  306. verifyFormat("if (a) /* still don't merge */\n"
  307. " f();",
  308. AllowsMergedIf);
  309. verifyFormat("if (a) { // Never merge this\n"
  310. " f();\n"
  311. "}",
  312. AllowsMergedIf);
  313. verifyFormat("if (a) {/* Never merge this */\n"
  314. " f();\n"
  315. "}",
  316. AllowsMergedIf);
  317. EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1, AllowsMergedIf));
  318. EXPECT_EQ("if (a) return; // comment",
  319. format("if(a)\nreturn; // comment", 20, 1, AllowsMergedIf));
  320. AllowsMergedIf.ColumnLimit = 14;
  321. verifyFormat("if (a) return;", AllowsMergedIf);
  322. verifyFormat("if (aaaaaaaaa)\n"
  323. " return;",
  324. AllowsMergedIf);
  325. AllowsMergedIf.ColumnLimit = 13;
  326. verifyFormat("if (a)\n return;", AllowsMergedIf);
  327. }
  328. TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
  329. FormatStyle AllowsMergedLoops = getLLVMStyle();
  330. AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
  331. verifyFormat("while (true) continue;", AllowsMergedLoops);
  332. verifyFormat("for (;;) continue;", AllowsMergedLoops);
  333. verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
  334. verifyFormat("while (true)\n"
  335. " ;",
  336. AllowsMergedLoops);
  337. verifyFormat("for (;;)\n"
  338. " ;",
  339. AllowsMergedLoops);
  340. verifyFormat("for (;;)\n"
  341. " for (;;) continue;",
  342. AllowsMergedLoops);
  343. verifyFormat("for (;;) // Can't merge this\n"
  344. " continue;",
  345. AllowsMergedLoops);
  346. verifyFormat("for (;;) /* still don't merge */\n"
  347. " continue;",
  348. AllowsMergedLoops);
  349. }
  350. TEST_F(FormatTest, FormatShortBracedStatements) {
  351. FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
  352. AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
  353. AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
  354. AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
  355. verifyFormat("if (true) {}", AllowSimpleBracedStatements);
  356. verifyFormat("while (true) {}", AllowSimpleBracedStatements);
  357. verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
  358. verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
  359. verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
  360. verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
  361. verifyFormat("if (true) { //\n"
  362. " f();\n"
  363. "}",
  364. AllowSimpleBracedStatements);
  365. verifyFormat("if (true) {\n"
  366. " f();\n"
  367. " f();\n"
  368. "}",
  369. AllowSimpleBracedStatements);
  370. verifyFormat("template <int> struct A2 {\n"
  371. " struct B {};\n"
  372. "};",
  373. AllowSimpleBracedStatements);
  374. AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
  375. verifyFormat("if (true) {\n"
  376. " f();\n"
  377. "}",
  378. AllowSimpleBracedStatements);
  379. AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
  380. verifyFormat("while (true) {\n"
  381. " f();\n"
  382. "}",
  383. AllowSimpleBracedStatements);
  384. verifyFormat("for (;;) {\n"
  385. " f();\n"
  386. "}",
  387. AllowSimpleBracedStatements);
  388. }
  389. TEST_F(FormatTest, ParseIfElse) {
  390. verifyFormat("if (true)\n"
  391. " if (true)\n"
  392. " if (true)\n"
  393. " f();\n"
  394. " else\n"
  395. " g();\n"
  396. " else\n"
  397. " h();\n"
  398. "else\n"
  399. " i();");
  400. verifyFormat("if (true)\n"
  401. " if (true)\n"
  402. " if (true) {\n"
  403. " if (true)\n"
  404. " f();\n"
  405. " } else {\n"
  406. " g();\n"
  407. " }\n"
  408. " else\n"
  409. " h();\n"
  410. "else {\n"
  411. " i();\n"
  412. "}");
  413. verifyFormat("void f() {\n"
  414. " if (a) {\n"
  415. " } else {\n"
  416. " }\n"
  417. "}");
  418. }
  419. TEST_F(FormatTest, ElseIf) {
  420. verifyFormat("if (a) {\n} else if (b) {\n}");
  421. verifyFormat("if (a)\n"
  422. " f();\n"
  423. "else if (b)\n"
  424. " g();\n"
  425. "else\n"
  426. " h();");
  427. verifyFormat("if (a) {\n"
  428. " f();\n"
  429. "}\n"
  430. "// or else ..\n"
  431. "else {\n"
  432. " g()\n"
  433. "}");
  434. verifyFormat("if (a) {\n"
  435. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  436. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  437. "}");
  438. }
  439. TEST_F(FormatTest, FormatsForLoop) {
  440. verifyFormat(
  441. "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
  442. " ++VeryVeryLongLoopVariable)\n"
  443. " ;");
  444. verifyFormat("for (;;)\n"
  445. " f();");
  446. verifyFormat("for (;;) {\n}");
  447. verifyFormat("for (;;) {\n"
  448. " f();\n"
  449. "}");
  450. verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
  451. verifyFormat(
  452. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  453. " E = UnwrappedLines.end();\n"
  454. " I != E; ++I) {\n}");
  455. verifyFormat(
  456. "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
  457. " ++IIIII) {\n}");
  458. verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
  459. " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
  460. " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
  461. verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
  462. " I = FD->getDeclsInPrototypeScope().begin(),\n"
  463. " E = FD->getDeclsInPrototypeScope().end();\n"
  464. " I != E; ++I) {\n}");
  465. // FIXME: Not sure whether we want extra identation in line 3 here:
  466. verifyFormat(
  467. "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  468. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
  469. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  470. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  471. " ++aaaaaaaaaaa) {\n}");
  472. verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
  473. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  474. "}");
  475. verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
  476. " aaaaaaaaaa);\n"
  477. " iter; ++iter) {\n"
  478. "}");
  479. FormatStyle NoBinPacking = getLLVMStyle();
  480. NoBinPacking.BinPackParameters = false;
  481. verifyFormat("for (int aaaaaaaaaaa = 1;\n"
  482. " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
  483. " aaaaaaaaaaaaaaaa,\n"
  484. " aaaaaaaaaaaaaaaa,\n"
  485. " aaaaaaaaaaaaaaaa);\n"
  486. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  487. "}",
  488. NoBinPacking);
  489. verifyFormat(
  490. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  491. " E = UnwrappedLines.end();\n"
  492. " I != E;\n"
  493. " ++I) {\n}",
  494. NoBinPacking);
  495. }
  496. TEST_F(FormatTest, RangeBasedForLoops) {
  497. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  498. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  499. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
  500. " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
  501. verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
  502. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  503. verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
  504. " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
  505. }
  506. TEST_F(FormatTest, ForEachLoops) {
  507. verifyFormat("void f() {\n"
  508. " foreach (Item *item, itemlist) {}\n"
  509. " Q_FOREACH (Item *item, itemlist) {}\n"
  510. " BOOST_FOREACH (Item *item, itemlist) {}\n"
  511. " UNKNOWN_FORACH(Item * item, itemlist) {}\n"
  512. "}");
  513. }
  514. TEST_F(FormatTest, FormatsWhileLoop) {
  515. verifyFormat("while (true) {\n}");
  516. verifyFormat("while (true)\n"
  517. " f();");
  518. verifyFormat("while () {\n}");
  519. verifyFormat("while () {\n"
  520. " f();\n"
  521. "}");
  522. }
  523. TEST_F(FormatTest, FormatsDoWhile) {
  524. verifyFormat("do {\n"
  525. " do_something();\n"
  526. "} while (something());");
  527. verifyFormat("do\n"
  528. " do_something();\n"
  529. "while (something());");
  530. }
  531. TEST_F(FormatTest, FormatsSwitchStatement) {
  532. verifyFormat("switch (x) {\n"
  533. "case 1:\n"
  534. " f();\n"
  535. " break;\n"
  536. "case kFoo:\n"
  537. "case ns::kBar:\n"
  538. "case kBaz:\n"
  539. " break;\n"
  540. "default:\n"
  541. " g();\n"
  542. " break;\n"
  543. "}");
  544. verifyFormat("switch (x) {\n"
  545. "case 1: {\n"
  546. " f();\n"
  547. " break;\n"
  548. "}\n"
  549. "case 2: {\n"
  550. " break;\n"
  551. "}\n"
  552. "}");
  553. verifyFormat("switch (x) {\n"
  554. "case 1: {\n"
  555. " f();\n"
  556. " {\n"
  557. " g();\n"
  558. " h();\n"
  559. " }\n"
  560. " break;\n"
  561. "}\n"
  562. "}");
  563. verifyFormat("switch (x) {\n"
  564. "case 1: {\n"
  565. " f();\n"
  566. " if (foo) {\n"
  567. " g();\n"
  568. " h();\n"
  569. " }\n"
  570. " break;\n"
  571. "}\n"
  572. "}");
  573. verifyFormat("switch (x) {\n"
  574. "case 1: {\n"
  575. " f();\n"
  576. " g();\n"
  577. "} break;\n"
  578. "}");
  579. verifyFormat("switch (test)\n"
  580. " ;");
  581. verifyFormat("switch (x) {\n"
  582. "default: {\n"
  583. " // Do nothing.\n"
  584. "}\n"
  585. "}");
  586. verifyFormat("switch (x) {\n"
  587. "// comment\n"
  588. "// if 1, do f()\n"
  589. "case 1:\n"
  590. " f();\n"
  591. "}");
  592. verifyFormat("switch (x) {\n"
  593. "case 1:\n"
  594. " // Do amazing stuff\n"
  595. " {\n"
  596. " f();\n"
  597. " g();\n"
  598. " }\n"
  599. " break;\n"
  600. "}");
  601. verifyFormat("#define A \\\n"
  602. " switch (x) { \\\n"
  603. " case a: \\\n"
  604. " foo = b; \\\n"
  605. " }", getLLVMStyleWithColumns(20));
  606. verifyFormat("#define OPERATION_CASE(name) \\\n"
  607. " case OP_name: \\\n"
  608. " return operations::Operation##name\n",
  609. getLLVMStyleWithColumns(40));
  610. verifyGoogleFormat("switch (x) {\n"
  611. " case 1:\n"
  612. " f();\n"
  613. " break;\n"
  614. " case kFoo:\n"
  615. " case ns::kBar:\n"
  616. " case kBaz:\n"
  617. " break;\n"
  618. " default:\n"
  619. " g();\n"
  620. " break;\n"
  621. "}");
  622. verifyGoogleFormat("switch (x) {\n"
  623. " case 1: {\n"
  624. " f();\n"
  625. " break;\n"
  626. " }\n"
  627. "}");
  628. verifyGoogleFormat("switch (test)\n"
  629. " ;");
  630. verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
  631. " case OP_name: \\\n"
  632. " return operations::Operation##name\n");
  633. verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
  634. " // Get the correction operation class.\n"
  635. " switch (OpCode) {\n"
  636. " CASE(Add);\n"
  637. " CASE(Subtract);\n"
  638. " default:\n"
  639. " return operations::Unknown;\n"
  640. " }\n"
  641. "#undef OPERATION_CASE\n"
  642. "}");
  643. verifyFormat("DEBUG({\n"
  644. " switch (x) {\n"
  645. " case A:\n"
  646. " f();\n"
  647. " break;\n"
  648. " // On B:\n"
  649. " case B:\n"
  650. " g();\n"
  651. " break;\n"
  652. " }\n"
  653. "});");
  654. verifyFormat("switch (a) {\n"
  655. "case (b):\n"
  656. " return;\n"
  657. "}");
  658. verifyFormat("switch (a) {\n"
  659. "case some_namespace::\n"
  660. " some_constant:\n"
  661. " return;\n"
  662. "}",
  663. getLLVMStyleWithColumns(34));
  664. }
  665. TEST_F(FormatTest, CaseRanges) {
  666. verifyFormat("switch (x) {\n"
  667. "case 'A' ... 'Z':\n"
  668. "case 1 ... 5:\n"
  669. " break;\n"
  670. "}");
  671. }
  672. TEST_F(FormatTest, ShortCaseLabels) {
  673. FormatStyle Style = getLLVMStyle();
  674. Style.AllowShortCaseLabelsOnASingleLine = true;
  675. verifyFormat("switch (a) {\n"
  676. "case 1: x = 1; break;\n"
  677. "case 2: return;\n"
  678. "case 3:\n"
  679. "case 4:\n"
  680. "case 5: return;\n"
  681. "case 6: // comment\n"
  682. " return;\n"
  683. "case 7:\n"
  684. " // comment\n"
  685. " return;\n"
  686. "default: y = 1; break;\n"
  687. "}",
  688. Style);
  689. verifyFormat("switch (a) {\n"
  690. "#if FOO\n"
  691. "case 0: return 0;\n"
  692. "#endif\n"
  693. "}",
  694. Style);
  695. verifyFormat("switch (a) {\n"
  696. "case 1: {\n"
  697. "}\n"
  698. "case 2: {\n"
  699. " return;\n"
  700. "}\n"
  701. "case 3: {\n"
  702. " x = 1;\n"
  703. " return;\n"
  704. "}\n"
  705. "case 4:\n"
  706. " if (x)\n"
  707. " return;\n"
  708. "}",
  709. Style);
  710. Style.ColumnLimit = 21;
  711. verifyFormat("switch (a) {\n"
  712. "case 1: x = 1; break;\n"
  713. "case 2: return;\n"
  714. "case 3:\n"
  715. "case 4:\n"
  716. "case 5: return;\n"
  717. "default:\n"
  718. " y = 1;\n"
  719. " break;\n"
  720. "}",
  721. Style);
  722. }
  723. TEST_F(FormatTest, FormatsLabels) {
  724. verifyFormat("void f() {\n"
  725. " some_code();\n"
  726. "test_label:\n"
  727. " some_other_code();\n"
  728. " {\n"
  729. " some_more_code();\n"
  730. " another_label:\n"
  731. " some_more_code();\n"
  732. " }\n"
  733. "}");
  734. verifyFormat("some_code();\n"
  735. "test_label:\n"
  736. "some_other_code();");
  737. }
  738. //===----------------------------------------------------------------------===//
  739. // Tests for comments.
  740. //===----------------------------------------------------------------------===//
  741. TEST_F(FormatTest, UnderstandsSingleLineComments) {
  742. verifyFormat("//* */");
  743. verifyFormat("// line 1\n"
  744. "// line 2\n"
  745. "void f() {}\n");
  746. verifyFormat("void f() {\n"
  747. " // Doesn't do anything\n"
  748. "}");
  749. verifyFormat("SomeObject\n"
  750. " // Calling someFunction on SomeObject\n"
  751. " .someFunction();");
  752. verifyFormat("auto result = SomeObject\n"
  753. " // Calling someFunction on SomeObject\n"
  754. " .someFunction();");
  755. verifyFormat("void f(int i, // some comment (probably for i)\n"
  756. " int j, // some comment (probably for j)\n"
  757. " int k); // some comment (probably for k)");
  758. verifyFormat("void f(int i,\n"
  759. " // some comment (probably for j)\n"
  760. " int j,\n"
  761. " // some comment (probably for k)\n"
  762. " int k);");
  763. verifyFormat("int i // This is a fancy variable\n"
  764. " = 5; // with nicely aligned comment.");
  765. verifyFormat("// Leading comment.\n"
  766. "int a; // Trailing comment.");
  767. verifyFormat("int a; // Trailing comment\n"
  768. " // on 2\n"
  769. " // or 3 lines.\n"
  770. "int b;");
  771. verifyFormat("int a; // Trailing comment\n"
  772. "\n"
  773. "// Leading comment.\n"
  774. "int b;");
  775. verifyFormat("int a; // Comment.\n"
  776. " // More details.\n"
  777. "int bbbb; // Another comment.");
  778. verifyFormat(
  779. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  780. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n"
  781. "int cccccccccccccccccccccccccccccc; // comment\n"
  782. "int ddd; // looooooooooooooooooooooooong comment\n"
  783. "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  784. "int bbbbbbbbbbbbbbbbbbbbb; // comment\n"
  785. "int ccccccccccccccccccc; // comment");
  786. verifyFormat("#include \"a\" // comment\n"
  787. "#include \"a/b/c\" // comment");
  788. verifyFormat("#include <a> // comment\n"
  789. "#include <a/b/c> // comment");
  790. EXPECT_EQ("#include \"a\" // comment\n"
  791. "#include \"a/b/c\" // comment",
  792. format("#include \\\n"
  793. " \"a\" // comment\n"
  794. "#include \"a/b/c\" // comment"));
  795. verifyFormat("enum E {\n"
  796. " // comment\n"
  797. " VAL_A, // comment\n"
  798. " VAL_B\n"
  799. "};");
  800. verifyFormat(
  801. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  802. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
  803. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  804. " // Comment inside a statement.\n"
  805. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  806. verifyFormat("SomeFunction(a,\n"
  807. " // comment\n"
  808. " b + x);");
  809. verifyFormat("SomeFunction(a, a,\n"
  810. " // comment\n"
  811. " b + x);");
  812. verifyFormat(
  813. "bool aaaaaaaaaaaaa = // comment\n"
  814. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  815. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  816. verifyFormat("int aaaa; // aaaaa\n"
  817. "int aa; // aaaaaaa",
  818. getLLVMStyleWithColumns(20));
  819. EXPECT_EQ("void f() { // This does something ..\n"
  820. "}\n"
  821. "int a; // This is unrelated",
  822. format("void f() { // This does something ..\n"
  823. " }\n"
  824. "int a; // This is unrelated"));
  825. EXPECT_EQ("class C {\n"
  826. " void f() { // This does something ..\n"
  827. " } // awesome..\n"
  828. "\n"
  829. " int a; // This is unrelated\n"
  830. "};",
  831. format("class C{void f() { // This does something ..\n"
  832. " } // awesome..\n"
  833. " \n"
  834. "int a; // This is unrelated\n"
  835. "};"));
  836. EXPECT_EQ("int i; // single line trailing comment",
  837. format("int i;\\\n// single line trailing comment"));
  838. verifyGoogleFormat("int a; // Trailing comment.");
  839. verifyFormat("someFunction(anotherFunction( // Force break.\n"
  840. " parameter));");
  841. verifyGoogleFormat("#endif // HEADER_GUARD");
  842. verifyFormat("const char *test[] = {\n"
  843. " // A\n"
  844. " \"aaaa\",\n"
  845. " // B\n"
  846. " \"aaaaa\"};");
  847. verifyGoogleFormat(
  848. "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  849. " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment");
  850. EXPECT_EQ("D(a, {\n"
  851. " // test\n"
  852. " int a;\n"
  853. "});",
  854. format("D(a, {\n"
  855. "// test\n"
  856. "int a;\n"
  857. "});"));
  858. EXPECT_EQ("lineWith(); // comment\n"
  859. "// at start\n"
  860. "otherLine();",
  861. format("lineWith(); // comment\n"
  862. "// at start\n"
  863. "otherLine();"));
  864. EXPECT_EQ("lineWith(); // comment\n"
  865. " // at start\n"
  866. "otherLine();",
  867. format("lineWith(); // comment\n"
  868. " // at start\n"
  869. "otherLine();"));
  870. EXPECT_EQ("lineWith(); // comment\n"
  871. "// at start\n"
  872. "otherLine(); // comment",
  873. format("lineWith(); // comment\n"
  874. "// at start\n"
  875. "otherLine(); // comment"));
  876. EXPECT_EQ("lineWith();\n"
  877. "// at start\n"
  878. "otherLine(); // comment",
  879. format("lineWith();\n"
  880. " // at start\n"
  881. "otherLine(); // comment"));
  882. EXPECT_EQ("// first\n"
  883. "// at start\n"
  884. "otherLine(); // comment",
  885. format("// first\n"
  886. " // at start\n"
  887. "otherLine(); // comment"));
  888. EXPECT_EQ("f();\n"
  889. "// first\n"
  890. "// at start\n"
  891. "otherLine(); // comment",
  892. format("f();\n"
  893. "// first\n"
  894. " // at start\n"
  895. "otherLine(); // comment"));
  896. verifyFormat("f(); // comment\n"
  897. "// first\n"
  898. "// at start\n"
  899. "otherLine();");
  900. EXPECT_EQ("f(); // comment\n"
  901. "// first\n"
  902. "// at start\n"
  903. "otherLine();",
  904. format("f(); // comment\n"
  905. "// first\n"
  906. " // at start\n"
  907. "otherLine();"));
  908. EXPECT_EQ("f(); // comment\n"
  909. " // first\n"
  910. "// at start\n"
  911. "otherLine();",
  912. format("f(); // comment\n"
  913. " // first\n"
  914. "// at start\n"
  915. "otherLine();"));
  916. verifyFormat(
  917. "#define A \\\n"
  918. " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
  919. " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
  920. getLLVMStyleWithColumns(60));
  921. verifyFormat(
  922. "#define A \\\n"
  923. " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
  924. " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
  925. getLLVMStyleWithColumns(61));
  926. verifyFormat("if ( // This is some comment\n"
  927. " x + 3) {\n"
  928. "}");
  929. EXPECT_EQ("if ( // This is some comment\n"
  930. " // spanning two lines\n"
  931. " x + 3) {\n"
  932. "}",
  933. format("if( // This is some comment\n"
  934. " // spanning two lines\n"
  935. " x + 3) {\n"
  936. "}"));
  937. }
  938. TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
  939. EXPECT_EQ("SomeFunction(a,\n"
  940. " b, // comment\n"
  941. " c);",
  942. format("SomeFunction(a,\n"
  943. " b, // comment\n"
  944. " c);"));
  945. EXPECT_EQ("SomeFunction(a, b,\n"
  946. " // comment\n"
  947. " c);",
  948. format("SomeFunction(a,\n"
  949. " b,\n"
  950. " // comment\n"
  951. " c);"));
  952. EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
  953. " c);",
  954. format("SomeFunction(a, b, // comment (unclear relation)\n"
  955. " c);"));
  956. EXPECT_EQ("SomeFunction(a, // comment\n"
  957. " b,\n"
  958. " c); // comment",
  959. format("SomeFunction(a, // comment\n"
  960. " b,\n"
  961. " c); // comment"));
  962. }
  963. TEST_F(FormatTest, CanFormatCommentsLocally) {
  964. EXPECT_EQ("int a; // comment\n"
  965. "int b; // comment",
  966. format("int a; // comment\n"
  967. "int b; // comment",
  968. 0, 0, getLLVMStyle()));
  969. EXPECT_EQ("int a; // comment\n"
  970. " // line 2\n"
  971. "int b;",
  972. format("int a; // comment\n"
  973. " // line 2\n"
  974. "int b;",
  975. 28, 0, getLLVMStyle()));
  976. EXPECT_EQ("int aaaaaa; // comment\n"
  977. "int b;\n"
  978. "int c; // unrelated comment",
  979. format("int aaaaaa; // comment\n"
  980. "int b;\n"
  981. "int c; // unrelated comment",
  982. 31, 0, getLLVMStyle()));
  983. EXPECT_EQ("int a; // This\n"
  984. " // is\n"
  985. " // a",
  986. format("int a; // This\n"
  987. " // is\n"
  988. " // a",
  989. 0, 0, getLLVMStyle()));
  990. EXPECT_EQ("int a; // This\n"
  991. " // is\n"
  992. " // a\n"
  993. "// This is b\n"
  994. "int b;",
  995. format("int a; // This\n"
  996. " // is\n"
  997. " // a\n"
  998. "// This is b\n"
  999. "int b;",
  1000. 0, 0, getLLVMStyle()));
  1001. EXPECT_EQ("int a; // This\n"
  1002. " // is\n"
  1003. " // a\n"
  1004. "\n"
  1005. " // This is unrelated",
  1006. format("int a; // This\n"
  1007. " // is\n"
  1008. " // a\n"
  1009. "\n"
  1010. " // This is unrelated",
  1011. 0, 0, getLLVMStyle()));
  1012. EXPECT_EQ("int a;\n"
  1013. "// This is\n"
  1014. "// not formatted. ",
  1015. format("int a;\n"
  1016. "// This is\n"
  1017. "// not formatted. ",
  1018. 0, 0, getLLVMStyle()));
  1019. }
  1020. TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
  1021. EXPECT_EQ("// comment", format("// comment "));
  1022. EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
  1023. format("int aaaaaaa, bbbbbbb; // comment ",
  1024. getLLVMStyleWithColumns(33)));
  1025. EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f "));
  1026. EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f "));
  1027. }
  1028. TEST_F(FormatTest, UnderstandsBlockComments) {
  1029. verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
  1030. verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }");
  1031. EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
  1032. " bbbbbbbbbbbbbbbbbbbbbbbbb);",
  1033. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n"
  1034. "/* Trailing comment for aa... */\n"
  1035. " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  1036. EXPECT_EQ(
  1037. "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  1038. " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
  1039. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
  1040. "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  1041. EXPECT_EQ(
  1042. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1043. " aaaaaaaaaaaaaaaaaa,\n"
  1044. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  1045. "}",
  1046. format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1047. " aaaaaaaaaaaaaaaaaa ,\n"
  1048. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  1049. "}"));
  1050. FormatStyle NoBinPacking = getLLVMStyle();
  1051. NoBinPacking.BinPackParameters = false;
  1052. verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
  1053. " /* parameter 2 */ aaaaaa,\n"
  1054. " /* parameter 3 */ aaaaaa,\n"
  1055. " /* parameter 4 */ aaaaaa);",
  1056. NoBinPacking);
  1057. // Aligning block comments in macros.
  1058. verifyGoogleFormat("#define A \\\n"
  1059. " int i; /*a*/ \\\n"
  1060. " int jjj; /*b*/");
  1061. }
  1062. TEST_F(FormatTest, AlignsBlockComments) {
  1063. EXPECT_EQ("/*\n"
  1064. " * Really multi-line\n"
  1065. " * comment.\n"
  1066. " */\n"
  1067. "void f() {}",
  1068. format(" /*\n"
  1069. " * Really multi-line\n"
  1070. " * comment.\n"
  1071. " */\n"
  1072. " void f() {}"));
  1073. EXPECT_EQ("class C {\n"
  1074. " /*\n"
  1075. " * Another multi-line\n"
  1076. " * comment.\n"
  1077. " */\n"
  1078. " void f() {}\n"
  1079. "};",
  1080. format("class C {\n"
  1081. "/*\n"
  1082. " * Another multi-line\n"
  1083. " * comment.\n"
  1084. " */\n"
  1085. "void f() {}\n"
  1086. "};"));
  1087. EXPECT_EQ("/*\n"
  1088. " 1. This is a comment with non-trivial formatting.\n"
  1089. " 1.1. We have to indent/outdent all lines equally\n"
  1090. " 1.1.1. to keep the formatting.\n"
  1091. " */",
  1092. format(" /*\n"
  1093. " 1. This is a comment with non-trivial formatting.\n"
  1094. " 1.1. We have to indent/outdent all lines equally\n"
  1095. " 1.1.1. to keep the formatting.\n"
  1096. " */"));
  1097. EXPECT_EQ("/*\n"
  1098. "Don't try to outdent if there's not enough indentation.\n"
  1099. "*/",
  1100. format(" /*\n"
  1101. " Don't try to outdent if there's not enough indentation.\n"
  1102. " */"));
  1103. EXPECT_EQ("int i; /* Comment with empty...\n"
  1104. " *\n"
  1105. " * line. */",
  1106. format("int i; /* Comment with empty...\n"
  1107. " *\n"
  1108. " * line. */"));
  1109. EXPECT_EQ("int foobar = 0; /* comment */\n"
  1110. "int bar = 0; /* multiline\n"
  1111. " comment 1 */\n"
  1112. "int baz = 0; /* multiline\n"
  1113. " comment 2 */\n"
  1114. "int bzz = 0; /* multiline\n"
  1115. " comment 3 */",
  1116. format("int foobar = 0; /* comment */\n"
  1117. "int bar = 0; /* multiline\n"
  1118. " comment 1 */\n"
  1119. "int baz = 0; /* multiline\n"
  1120. " comment 2 */\n"
  1121. "int bzz = 0; /* multiline\n"
  1122. " comment 3 */"));
  1123. EXPECT_EQ("int foobar = 0; /* comment */\n"
  1124. "int bar = 0; /* multiline\n"
  1125. " comment */\n"
  1126. "int baz = 0; /* multiline\n"
  1127. "comment */",
  1128. format("int foobar = 0; /* comment */\n"
  1129. "int bar = 0; /* multiline\n"
  1130. "comment */\n"
  1131. "int baz = 0; /* multiline\n"
  1132. "comment */"));
  1133. }
  1134. TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
  1135. EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1136. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
  1137. format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1138. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
  1139. EXPECT_EQ(
  1140. "void ffffffffffff(\n"
  1141. " int aaaaaaaa, int bbbbbbbb,\n"
  1142. " int cccccccccccc) { /*\n"
  1143. " aaaaaaaaaa\n"
  1144. " aaaaaaaaaaaaa\n"
  1145. " bbbbbbbbbbbbbb\n"
  1146. " bbbbbbbbbb\n"
  1147. " */\n"
  1148. "}",
  1149. format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
  1150. "{ /*\n"
  1151. " aaaaaaaaaa aaaaaaaaaaaaa\n"
  1152. " bbbbbbbbbbbbbb bbbbbbbbbb\n"
  1153. " */\n"
  1154. "}",
  1155. getLLVMStyleWithColumns(40)));
  1156. }
  1157. TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
  1158. EXPECT_EQ("void ffffffffff(\n"
  1159. " int aaaaa /* test */);",
  1160. format("void ffffffffff(int aaaaa /* test */);",
  1161. getLLVMStyleWithColumns(35)));
  1162. }
  1163. TEST_F(FormatTest, SplitsLongCxxComments) {
  1164. EXPECT_EQ("// A comment that\n"
  1165. "// doesn't fit on\n"
  1166. "// one line",
  1167. format("// A comment that doesn't fit on one line",
  1168. getLLVMStyleWithColumns(20)));
  1169. EXPECT_EQ("// a b c d\n"
  1170. "// e f g\n"
  1171. "// h i j k",
  1172. format("// a b c d e f g h i j k",
  1173. getLLVMStyleWithColumns(10)));
  1174. EXPECT_EQ("// a b c d\n"
  1175. "// e f g\n"
  1176. "// h i j k",
  1177. format("\\\n// a b c d e f g h i j k",
  1178. getLLVMStyleWithColumns(10)));
  1179. EXPECT_EQ("if (true) // A comment that\n"
  1180. " // doesn't fit on\n"
  1181. " // one line",
  1182. format("if (true) // A comment that doesn't fit on one line ",
  1183. getLLVMStyleWithColumns(30)));
  1184. EXPECT_EQ("// Don't_touch_leading_whitespace",
  1185. format("// Don't_touch_leading_whitespace",
  1186. getLLVMStyleWithColumns(20)));
  1187. EXPECT_EQ("// Add leading\n"
  1188. "// whitespace",
  1189. format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
  1190. EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
  1191. EXPECT_EQ("// Even if it makes the line exceed the column\n"
  1192. "// limit",
  1193. format("//Even if it makes the line exceed the column limit",
  1194. getLLVMStyleWithColumns(51)));
  1195. EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
  1196. EXPECT_EQ("// aa bb cc dd",
  1197. format("// aa bb cc dd ",
  1198. getLLVMStyleWithColumns(15)));
  1199. EXPECT_EQ("// A comment before\n"
  1200. "// a macro\n"
  1201. "// definition\n"
  1202. "#define a b",
  1203. format("// A comment before a macro definition\n"
  1204. "#define a b",
  1205. getLLVMStyleWithColumns(20)));
  1206. EXPECT_EQ("void ffffff(\n"
  1207. " int aaaaaaaaa, // wwww\n"
  1208. " int bbbbbbbbbb, // xxxxxxx\n"
  1209. " // yyyyyyyyyy\n"
  1210. " int c, int d, int e) {}",
  1211. format("void ffffff(\n"
  1212. " int aaaaaaaaa, // wwww\n"
  1213. " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
  1214. " int c, int d, int e) {}",
  1215. getLLVMStyleWithColumns(40)));
  1216. EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1217. format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1218. getLLVMStyleWithColumns(20)));
  1219. EXPECT_EQ(
  1220. "#define XXX // a b c d\n"
  1221. " // e f g h",
  1222. format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
  1223. EXPECT_EQ(
  1224. "#define XXX // q w e r\n"
  1225. " // t y u i",
  1226. format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
  1227. }
  1228. TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {
  1229. EXPECT_EQ("// A comment\n"
  1230. "// that doesn't\n"
  1231. "// fit on one\n"
  1232. "// line",
  1233. format("// A comment that doesn't fit on one line",
  1234. getLLVMStyleWithColumns(20)));
  1235. EXPECT_EQ("/// A comment\n"
  1236. "/// that doesn't\n"
  1237. "/// fit on one\n"
  1238. "/// line",
  1239. format("/// A comment that doesn't fit on one line",
  1240. getLLVMStyleWithColumns(20)));
  1241. }
  1242. TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
  1243. EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1244. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1245. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1246. format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1247. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1248. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
  1249. EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1250. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1251. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1252. format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1253. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1254. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1255. getLLVMStyleWithColumns(50)));
  1256. // FIXME: One day we might want to implement adjustment of leading whitespace
  1257. // of the consecutive lines in this kind of comment:
  1258. EXPECT_EQ("double\n"
  1259. " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1260. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1261. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1262. format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1263. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1264. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1265. getLLVMStyleWithColumns(49)));
  1266. }
  1267. TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) {
  1268. FormatStyle Pragmas = getLLVMStyleWithColumns(30);
  1269. Pragmas.CommentPragmas = "^ IWYU pragma:";
  1270. EXPECT_EQ(
  1271. "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
  1272. format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
  1273. EXPECT_EQ(
  1274. "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
  1275. format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
  1276. }
  1277. TEST_F(FormatTest, PriorityOfCommentBreaking) {
  1278. EXPECT_EQ("if (xxx ==\n"
  1279. " yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  1280. " zzz)\n"
  1281. " q();",
  1282. format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  1283. " zzz) q();",
  1284. getLLVMStyleWithColumns(40)));
  1285. EXPECT_EQ("if (xxxxxxxxxx ==\n"
  1286. " yyy && // aaaaaa bbbbbbbb cccc\n"
  1287. " zzz)\n"
  1288. " q();",
  1289. format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
  1290. " zzz) q();",
  1291. getLLVMStyleWithColumns(40)));
  1292. EXPECT_EQ("if (xxxxxxxxxx &&\n"
  1293. " yyy || // aaaaaa bbbbbbbb cccc\n"
  1294. " zzz)\n"
  1295. " q();",
  1296. format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
  1297. " zzz) q();",
  1298. getLLVMStyleWithColumns(40)));
  1299. EXPECT_EQ("fffffffff(\n"
  1300. " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  1301. " zzz);",
  1302. format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  1303. " zzz);",
  1304. getLLVMStyleWithColumns(40)));
  1305. }
  1306. TEST_F(FormatTest, MultiLineCommentsInDefines) {
  1307. EXPECT_EQ("#define A(x) /* \\\n"
  1308. " a comment \\\n"
  1309. " inside */ \\\n"
  1310. " f();",
  1311. format("#define A(x) /* \\\n"
  1312. " a comment \\\n"
  1313. " inside */ \\\n"
  1314. " f();",
  1315. getLLVMStyleWithColumns(17)));
  1316. EXPECT_EQ("#define A( \\\n"
  1317. " x) /* \\\n"
  1318. " a comment \\\n"
  1319. " inside */ \\\n"
  1320. " f();",
  1321. format("#define A( \\\n"
  1322. " x) /* \\\n"
  1323. " a comment \\\n"
  1324. " inside */ \\\n"
  1325. " f();",
  1326. getLLVMStyleWithColumns(17)));
  1327. }
  1328. TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
  1329. EXPECT_EQ("namespace {}\n// Test\n#define A",
  1330. format("namespace {}\n // Test\n#define A"));
  1331. EXPECT_EQ("namespace {}\n/* Test */\n#define A",
  1332. format("namespace {}\n /* Test */\n#define A"));
  1333. EXPECT_EQ("namespace {}\n/* Test */ #define A",
  1334. format("namespace {}\n /* Test */ #define A"));
  1335. }
  1336. TEST_F(FormatTest, SplitsLongLinesInComments) {
  1337. EXPECT_EQ("/* This is a long\n"
  1338. " * comment that\n"
  1339. " * doesn't\n"
  1340. " * fit on one line.\n"
  1341. " */",
  1342. format("/* "
  1343. "This is a long "
  1344. "comment that "
  1345. "doesn't "
  1346. "fit on one line. */",
  1347. getLLVMStyleWithColumns(20)));
  1348. EXPECT_EQ("/* a b c d\n"
  1349. " * e f g\n"
  1350. " * h i j k\n"
  1351. " */",
  1352. format("/* a b c d e f g h i j k */",
  1353. getLLVMStyleWithColumns(10)));
  1354. EXPECT_EQ("/* a b c d\n"
  1355. " * e f g\n"
  1356. " * h i j k\n"
  1357. " */",
  1358. format("\\\n/* a b c d e f g h i j k */",
  1359. getLLVMStyleWithColumns(10)));
  1360. EXPECT_EQ("/*\n"
  1361. "This is a long\n"
  1362. "comment that doesn't\n"
  1363. "fit on one line.\n"
  1364. "*/",
  1365. format("/*\n"
  1366. "This is a long "
  1367. "comment that doesn't "
  1368. "fit on one line. \n"
  1369. "*/", getLLVMStyleWithColumns(20)));
  1370. EXPECT_EQ("/*\n"
  1371. " * This is a long\n"
  1372. " * comment that\n"
  1373. " * doesn't fit on\n"
  1374. " * one line.\n"
  1375. " */",
  1376. format("/* \n"
  1377. " * This is a long "
  1378. " comment that "
  1379. " doesn't fit on "
  1380. " one line. \n"
  1381. " */", getLLVMStyleWithColumns(20)));
  1382. EXPECT_EQ("/*\n"
  1383. " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
  1384. " * so_it_should_be_broken\n"
  1385. " * wherever_a_space_occurs\n"
  1386. " */",
  1387. format("/*\n"
  1388. " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
  1389. " so_it_should_be_broken "
  1390. " wherever_a_space_occurs \n"
  1391. " */",
  1392. getLLVMStyleWithColumns(20)));
  1393. EXPECT_EQ("/*\n"
  1394. " * This_comment_can_not_be_broken_into_lines\n"
  1395. " */",
  1396. format("/*\n"
  1397. " * This_comment_can_not_be_broken_into_lines\n"
  1398. " */",
  1399. getLLVMStyleWithColumns(20)));
  1400. EXPECT_EQ("{\n"
  1401. " /*\n"
  1402. " This is another\n"
  1403. " long comment that\n"
  1404. " doesn't fit on one\n"
  1405. " line 1234567890\n"
  1406. " */\n"
  1407. "}",
  1408. format("{\n"
  1409. "/*\n"
  1410. "This is another "
  1411. " long comment that "
  1412. " doesn't fit on one"
  1413. " line 1234567890\n"
  1414. "*/\n"
  1415. "}", getLLVMStyleWithColumns(20)));
  1416. EXPECT_EQ("{\n"
  1417. " /*\n"
  1418. " * This i s\n"
  1419. " * another comment\n"
  1420. " * t hat doesn' t\n"
  1421. " * fit on one l i\n"
  1422. " * n e\n"
  1423. " */\n"
  1424. "}",
  1425. format("{\n"
  1426. "/*\n"
  1427. " * This i s"
  1428. " another comment"
  1429. " t hat doesn' t"
  1430. " fit on one l i"
  1431. " n e\n"
  1432. " */\n"
  1433. "}", getLLVMStyleWithColumns(20)));
  1434. EXPECT_EQ("/*\n"
  1435. " * This is a long\n"
  1436. " * comment that\n"
  1437. " * doesn't fit on\n"
  1438. " * one line\n"
  1439. " */",
  1440. format(" /*\n"
  1441. " * This is a long comment that doesn't fit on one line\n"
  1442. " */", getLLVMStyleWithColumns(20)));
  1443. EXPECT_EQ("{\n"
  1444. " if (something) /* This is a\n"
  1445. " long\n"
  1446. " comment */\n"
  1447. " ;\n"
  1448. "}",
  1449. format("{\n"
  1450. " if (something) /* This is a long comment */\n"
  1451. " ;\n"
  1452. "}",
  1453. getLLVMStyleWithColumns(30)));
  1454. EXPECT_EQ("/* A comment before\n"
  1455. " * a macro\n"
  1456. " * definition */\n"
  1457. "#define a b",
  1458. format("/* A comment before a macro definition */\n"
  1459. "#define a b",
  1460. getLLVMStyleWithColumns(20)));
  1461. EXPECT_EQ("/* some comment\n"
  1462. " * a comment\n"
  1463. "* that we break\n"
  1464. " * another comment\n"
  1465. "* we have to break\n"
  1466. "* a left comment\n"
  1467. " */",
  1468. format(" /* some comment\n"
  1469. " * a comment that we break\n"
  1470. " * another comment we have to break\n"
  1471. "* a left comment\n"
  1472. " */",
  1473. getLLVMStyleWithColumns(20)));
  1474. EXPECT_EQ("/*\n"
  1475. "\n"
  1476. "\n"
  1477. " */\n",
  1478. format(" /* \n"
  1479. " \n"
  1480. " \n"
  1481. " */\n"));
  1482. EXPECT_EQ("/* a a */",
  1483. format("/* a a */", getLLVMStyleWithColumns(15)));
  1484. EXPECT_EQ("/* a a bc */",
  1485. format("/* a a bc */", getLLVMStyleWithColumns(15)));
  1486. EXPECT_EQ("/* aaa aaa\n"
  1487. " * aaaaa */",
  1488. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1489. EXPECT_EQ("/* aaa aaa\n"
  1490. " * aaaaa */",
  1491. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1492. }
  1493. TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
  1494. EXPECT_EQ("#define X \\\n"
  1495. " /* \\\n"
  1496. " Test \\\n"
  1497. " Macro comment \\\n"
  1498. " with a long \\\n"
  1499. " line \\\n"
  1500. " */ \\\n"
  1501. " A + B",
  1502. format("#define X \\\n"
  1503. " /*\n"
  1504. " Test\n"
  1505. " Macro comment with a long line\n"
  1506. " */ \\\n"
  1507. " A + B",
  1508. getLLVMStyleWithColumns(20)));
  1509. EXPECT_EQ("#define X \\\n"
  1510. " /* Macro comment \\\n"
  1511. " with a long \\\n"
  1512. " line */ \\\n"
  1513. " A + B",
  1514. format("#define X \\\n"
  1515. " /* Macro comment with a long\n"
  1516. " line */ \\\n"
  1517. " A + B",
  1518. getLLVMStyleWithColumns(20)));
  1519. EXPECT_EQ("#define X \\\n"
  1520. " /* Macro comment \\\n"
  1521. " * with a long \\\n"
  1522. " * line */ \\\n"
  1523. " A + B",
  1524. format("#define X \\\n"
  1525. " /* Macro comment with a long line */ \\\n"
  1526. " A + B",
  1527. getLLVMStyleWithColumns(20)));
  1528. }
  1529. TEST_F(FormatTest, CommentsInStaticInitializers) {
  1530. EXPECT_EQ(
  1531. "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
  1532. " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
  1533. " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
  1534. " aaaaaaaaaaaaaaaaaaaa, // comment\n"
  1535. " aaaaaaaaaaaaaaaaaaaa};",
  1536. format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
  1537. " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
  1538. " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
  1539. " aaaaaaaaaaaaaaaaaaaa , // comment\n"
  1540. " aaaaaaaaaaaaaaaaaaaa };"));
  1541. verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1542. " bbbbbbbbbbb, ccccccccccc};");
  1543. verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1544. " // comment for bb....\n"
  1545. " bbbbbbbbbbb, ccccccccccc};");
  1546. verifyGoogleFormat(
  1547. "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1548. " bbbbbbbbbbb, ccccccccccc};");
  1549. verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1550. " // comment for bb....\n"
  1551. " bbbbbbbbbbb, ccccccccccc};");
  1552. verifyFormat("S s = {{a, b, c}, // Group #1\n"
  1553. " {d, e, f}, // Group #2\n"
  1554. " {g, h, i}}; // Group #3");
  1555. verifyFormat("S s = {{// Group #1\n"
  1556. " a, b, c},\n"
  1557. " {// Group #2\n"
  1558. " d, e, f},\n"
  1559. " {// Group #3\n"
  1560. " g, h, i}};");
  1561. EXPECT_EQ("S s = {\n"
  1562. " // Some comment\n"
  1563. " a,\n"
  1564. "\n"
  1565. " // Comment after empty line\n"
  1566. " b}",
  1567. format("S s = {\n"
  1568. " // Some comment\n"
  1569. " a,\n"
  1570. " \n"
  1571. " // Comment after empty line\n"
  1572. " b\n"
  1573. "}"));
  1574. EXPECT_EQ("S s = {\n"
  1575. " /* Some comment */\n"
  1576. " a,\n"
  1577. "\n"
  1578. " /* Comment after empty line */\n"
  1579. " b}",
  1580. format("S s = {\n"
  1581. " /* Some comment */\n"
  1582. " a,\n"
  1583. " \n"
  1584. " /* Comment after empty line */\n"
  1585. " b\n"
  1586. "}"));
  1587. verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
  1588. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1589. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1590. " 0x00, 0x00, 0x00, 0x00}; // comment\n");
  1591. }
  1592. TEST_F(FormatTest, IgnoresIf0Contents) {
  1593. EXPECT_EQ("#if 0\n"
  1594. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1595. "#endif\n"
  1596. "void f() {}",
  1597. format("#if 0\n"
  1598. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1599. "#endif\n"
  1600. "void f( ) { }"));
  1601. EXPECT_EQ("#if false\n"
  1602. "void f( ) { }\n"
  1603. "#endif\n"
  1604. "void g() {}\n",
  1605. format("#if false\n"
  1606. "void f( ) { }\n"
  1607. "#endif\n"
  1608. "void g( ) { }\n"));
  1609. EXPECT_EQ("enum E {\n"
  1610. " One,\n"
  1611. " Two,\n"
  1612. "#if 0\n"
  1613. "Three,\n"
  1614. " Four,\n"
  1615. "#endif\n"
  1616. " Five\n"
  1617. "};",
  1618. format("enum E {\n"
  1619. " One,Two,\n"
  1620. "#if 0\n"
  1621. "Three,\n"
  1622. " Four,\n"
  1623. "#endif\n"
  1624. " Five};"));
  1625. EXPECT_EQ("enum F {\n"
  1626. " One,\n"
  1627. "#if 1\n"
  1628. " Two,\n"
  1629. "#if 0\n"
  1630. "Three,\n"
  1631. " Four,\n"
  1632. "#endif\n"
  1633. " Five\n"
  1634. "#endif\n"
  1635. "};",
  1636. format("enum F {\n"
  1637. "One,\n"
  1638. "#if 1\n"
  1639. "Two,\n"
  1640. "#if 0\n"
  1641. "Three,\n"
  1642. " Four,\n"
  1643. "#endif\n"
  1644. "Five\n"
  1645. "#endif\n"
  1646. "};"));
  1647. EXPECT_EQ("enum G {\n"
  1648. " One,\n"
  1649. "#if 0\n"
  1650. "Two,\n"
  1651. "#else\n"
  1652. " Three,\n"
  1653. "#endif\n"
  1654. " Four\n"
  1655. "};",
  1656. format("enum G {\n"
  1657. "One,\n"
  1658. "#if 0\n"
  1659. "Two,\n"
  1660. "#else\n"
  1661. "Three,\n"
  1662. "#endif\n"
  1663. "Four\n"
  1664. "};"));
  1665. EXPECT_EQ("enum H {\n"
  1666. " One,\n"
  1667. "#if 0\n"
  1668. "#ifdef Q\n"
  1669. "Two,\n"
  1670. "#else\n"
  1671. "Three,\n"
  1672. "#endif\n"
  1673. "#endif\n"
  1674. " Four\n"
  1675. "};",
  1676. format("enum H {\n"
  1677. "One,\n"
  1678. "#if 0\n"
  1679. "#ifdef Q\n"
  1680. "Two,\n"
  1681. "#else\n"
  1682. "Three,\n"
  1683. "#endif\n"
  1684. "#endif\n"
  1685. "Four\n"
  1686. "};"));
  1687. EXPECT_EQ("enum I {\n"
  1688. " One,\n"
  1689. "#if /* test */ 0 || 1\n"
  1690. "Two,\n"
  1691. "Three,\n"
  1692. "#endif\n"
  1693. " Four\n"
  1694. "};",
  1695. format("enum I {\n"
  1696. "One,\n"
  1697. "#if /* test */ 0 || 1\n"
  1698. "Two,\n"
  1699. "Three,\n"
  1700. "#endif\n"
  1701. "Four\n"
  1702. "};"));
  1703. EXPECT_EQ("enum J {\n"
  1704. " One,\n"
  1705. "#if 0\n"
  1706. "#if 0\n"
  1707. "Two,\n"
  1708. "#else\n"
  1709. "Three,\n"
  1710. "#endif\n"
  1711. "Four,\n"
  1712. "#endif\n"
  1713. " Five\n"
  1714. "};",
  1715. format("enum J {\n"
  1716. "One,\n"
  1717. "#if 0\n"
  1718. "#if 0\n"
  1719. "Two,\n"
  1720. "#else\n"
  1721. "Three,\n"
  1722. "#endif\n"
  1723. "Four,\n"
  1724. "#endif\n"
  1725. "Five\n"
  1726. "};"));
  1727. }
  1728. //===----------------------------------------------------------------------===//
  1729. // Tests for classes, namespaces, etc.
  1730. //===----------------------------------------------------------------------===//
  1731. TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
  1732. verifyFormat("class A {};");
  1733. }
  1734. TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
  1735. verifyFormat("class A {\n"
  1736. "public:\n"
  1737. "public: // comment\n"
  1738. "protected:\n"
  1739. "private:\n"
  1740. " void f() {}\n"
  1741. "};");
  1742. verifyGoogleFormat("class A {\n"
  1743. " public:\n"
  1744. " protected:\n"
  1745. " private:\n"
  1746. " void f() {}\n"
  1747. "};");
  1748. verifyFormat("class A {\n"
  1749. "public slots:\n"
  1750. " void f() {}\n"
  1751. "public Q_SLOTS:\n"
  1752. " void f() {}\n"
  1753. "};");
  1754. }
  1755. TEST_F(FormatTest, SeparatesLogicalBlocks) {
  1756. EXPECT_EQ("class A {\n"
  1757. "public:\n"
  1758. " void f();\n"
  1759. "\n"
  1760. "private:\n"
  1761. " void g() {}\n"
  1762. " // test\n"
  1763. "protected:\n"
  1764. " int h;\n"
  1765. "};",
  1766. format("class A {\n"
  1767. "public:\n"
  1768. "void f();\n"
  1769. "private:\n"
  1770. "void g() {}\n"
  1771. "// test\n"
  1772. "protected:\n"
  1773. "int h;\n"
  1774. "};"));
  1775. EXPECT_EQ("class A {\n"
  1776. "protected:\n"
  1777. "public:\n"
  1778. " void f();\n"
  1779. "};",
  1780. format("class A {\n"
  1781. "protected:\n"
  1782. "\n"
  1783. "public:\n"
  1784. "\n"
  1785. " void f();\n"
  1786. "};"));
  1787. }
  1788. TEST_F(FormatTest, FormatsClasses) {
  1789. verifyFormat("class A : public B {};");
  1790. verifyFormat("class A : public ::B {};");
  1791. verifyFormat(
  1792. "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1793. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1794. verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
  1795. " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1796. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1797. verifyFormat(
  1798. "class A : public B, public C, public D, public E, public F {};");
  1799. verifyFormat("class AAAAAAAAAAAA : public B,\n"
  1800. " public C,\n"
  1801. " public D,\n"
  1802. " public E,\n"
  1803. " public F,\n"
  1804. " public G {};");
  1805. verifyFormat("class\n"
  1806. " ReallyReallyLongClassName {\n"
  1807. " int i;\n"
  1808. "};",
  1809. getLLVMStyleWithColumns(32));
  1810. verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
  1811. " aaaaaaaaaaaaaaaa> {};");
  1812. verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
  1813. " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
  1814. " aaaaaaaaaaaaaaaaaaaaaa> {};");
  1815. verifyFormat("template <class R, class C>\n"
  1816. "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
  1817. " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
  1818. verifyFormat("class ::A::B {};");
  1819. }
  1820. TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
  1821. verifyFormat("class A {\n} a, b;");
  1822. verifyFormat("struct A {\n} a, b;");
  1823. verifyFormat("union A {\n} a;");
  1824. }
  1825. TEST_F(FormatTest, FormatsEnum) {
  1826. verifyFormat("enum {\n"
  1827. " Zero,\n"
  1828. " One = 1,\n"
  1829. " Two = One + 1,\n"
  1830. " Three = (One + Two),\n"
  1831. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1832. " Five = (One, Two, Three, Four, 5)\n"
  1833. "};");
  1834. verifyGoogleFormat("enum {\n"
  1835. " Zero,\n"
  1836. " One = 1,\n"
  1837. " Two = One + 1,\n"
  1838. " Three = (One + Two),\n"
  1839. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1840. " Five = (One, Two, Three, Four, 5)\n"
  1841. "};");
  1842. verifyFormat("enum Enum {};");
  1843. verifyFormat("enum {};");
  1844. verifyFormat("enum X E {} d;");
  1845. verifyFormat("enum __attribute__((...)) E {} d;");
  1846. verifyFormat("enum __declspec__((...)) E {} d;");
  1847. verifyFormat("enum X f() {\n a();\n return 42;\n}");
  1848. verifyFormat("enum {\n"
  1849. " Bar = Foo<int, int>::value\n"
  1850. "};",
  1851. getLLVMStyleWithColumns(30));
  1852. verifyFormat("enum ShortEnum { A, B, C };");
  1853. verifyGoogleFormat("enum ShortEnum { A, B, C };");
  1854. EXPECT_EQ("enum KeepEmptyLines {\n"
  1855. " ONE,\n"
  1856. "\n"
  1857. " TWO,\n"
  1858. "\n"
  1859. " THREE\n"
  1860. "}",
  1861. format("enum KeepEmptyLines {\n"
  1862. " ONE,\n"
  1863. "\n"
  1864. " TWO,\n"
  1865. "\n"
  1866. "\n"
  1867. " THREE\n"
  1868. "}"));
  1869. verifyFormat("enum E { // comment\n"
  1870. " ONE,\n"
  1871. " TWO\n"
  1872. "};\n"
  1873. "int i;");
  1874. }
  1875. TEST_F(FormatTest, FormatsEnumsWithErrors) {
  1876. verifyFormat("enum Type {\n"
  1877. " One = 0; // These semicolons should be commas.\n"
  1878. " Two = 1;\n"
  1879. "};");
  1880. verifyFormat("namespace n {\n"
  1881. "enum Type {\n"
  1882. " One,\n"
  1883. " Two, // missing };\n"
  1884. " int i;\n"
  1885. "}\n"
  1886. "void g() {}");
  1887. }
  1888. TEST_F(FormatTest, FormatsEnumStruct) {
  1889. verifyFormat("enum struct {\n"
  1890. " Zero,\n"
  1891. " One = 1,\n"
  1892. " Two = One + 1,\n"
  1893. " Three = (One + Two),\n"
  1894. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1895. " Five = (One, Two, Three, Four, 5)\n"
  1896. "};");
  1897. verifyFormat("enum struct Enum {};");
  1898. verifyFormat("enum struct {};");
  1899. verifyFormat("enum struct X E {} d;");
  1900. verifyFormat("enum struct __attribute__((...)) E {} d;");
  1901. verifyFormat("enum struct __declspec__((...)) E {} d;");
  1902. verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
  1903. }
  1904. TEST_F(FormatTest, FormatsEnumClass) {
  1905. verifyFormat("enum class {\n"
  1906. " Zero,\n"
  1907. " One = 1,\n"
  1908. " Two = One + 1,\n"
  1909. " Three = (One + Two),\n"
  1910. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1911. " Five = (One, Two, Three, Four, 5)\n"
  1912. "};");
  1913. verifyFormat("enum class Enum {};");
  1914. verifyFormat("enum class {};");
  1915. verifyFormat("enum class X E {} d;");
  1916. verifyFormat("enum class __attribute__((...)) E {} d;");
  1917. verifyFormat("enum class __declspec__((...)) E {} d;");
  1918. verifyFormat("enum class X f() {\n a();\n return 42;\n}");
  1919. }
  1920. TEST_F(FormatTest, FormatsEnumTypes) {
  1921. verifyFormat("enum X : int {\n"
  1922. " A, // Force multiple lines.\n"
  1923. " B\n"
  1924. "};");
  1925. verifyFormat("enum X : int { A, B };");
  1926. verifyFormat("enum X : std::uint32_t { A, B };");
  1927. }
  1928. TEST_F(FormatTest, FormatsNSEnums) {
  1929. verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
  1930. verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
  1931. " // Information about someDecentlyLongValue.\n"
  1932. " someDecentlyLongValue,\n"
  1933. " // Information about anotherDecentlyLongValue.\n"
  1934. " anotherDecentlyLongValue,\n"
  1935. " // Information about aThirdDecentlyLongValue.\n"
  1936. " aThirdDecentlyLongValue\n"
  1937. "};");
  1938. verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
  1939. " a = 1,\n"
  1940. " b = 2,\n"
  1941. " c = 3,\n"
  1942. "};");
  1943. verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
  1944. " a = 1,\n"
  1945. " b = 2,\n"
  1946. " c = 3,\n"
  1947. "};");
  1948. verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
  1949. " a = 1,\n"
  1950. " b = 2,\n"
  1951. " c = 3,\n"
  1952. "};");
  1953. }
  1954. TEST_F(FormatTest, FormatsBitfields) {
  1955. verifyFormat("struct Bitfields {\n"
  1956. " unsigned sClass : 8;\n"
  1957. " unsigned ValueKind : 2;\n"
  1958. "};");
  1959. verifyFormat("struct A {\n"
  1960. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
  1961. " bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
  1962. "};");
  1963. }
  1964. TEST_F(FormatTest, FormatsNamespaces) {
  1965. verifyFormat("namespace some_namespace {\n"
  1966. "class A {};\n"
  1967. "void f() { f(); }\n"
  1968. "}");
  1969. verifyFormat("namespace {\n"
  1970. "class A {};\n"
  1971. "void f() { f(); }\n"
  1972. "}");
  1973. verifyFormat("inline namespace X {\n"
  1974. "class A {};\n"
  1975. "void f() { f(); }\n"
  1976. "}");
  1977. verifyFormat("using namespace some_namespace;\n"
  1978. "class A {};\n"
  1979. "void f() { f(); }");
  1980. // This code is more common than we thought; if we
  1981. // layout this correctly the semicolon will go into
  1982. // its own line, which is undesirable.
  1983. verifyFormat("namespace {};");
  1984. verifyFormat("namespace {\n"
  1985. "class A {};\n"
  1986. "};");
  1987. verifyFormat("namespace {\n"
  1988. "int SomeVariable = 0; // comment\n"
  1989. "} // namespace");
  1990. EXPECT_EQ("#ifndef HEADER_GUARD\n"
  1991. "#define HEADER_GUARD\n"
  1992. "namespace my_namespace {\n"
  1993. "int i;\n"
  1994. "} // my_namespace\n"
  1995. "#endif // HEADER_GUARD",
  1996. format("#ifndef HEADER_GUARD\n"
  1997. " #define HEADER_GUARD\n"
  1998. " namespace my_namespace {\n"
  1999. "int i;\n"
  2000. "} // my_namespace\n"
  2001. "#endif // HEADER_GUARD"));
  2002. FormatStyle Style = getLLVMStyle();
  2003. Style.NamespaceIndentation = FormatStyle::NI_All;
  2004. EXPECT_EQ("namespace out {\n"
  2005. " int i;\n"
  2006. " namespace in {\n"
  2007. " int i;\n"
  2008. " } // namespace\n"
  2009. "} // namespace",
  2010. format("namespace out {\n"
  2011. "int i;\n"
  2012. "namespace in {\n"
  2013. "int i;\n"
  2014. "} // namespace\n"
  2015. "} // namespace",
  2016. Style));
  2017. Style.NamespaceIndentation = FormatStyle::NI_Inner;
  2018. EXPECT_EQ("namespace out {\n"
  2019. "int i;\n"
  2020. "namespace in {\n"
  2021. " int i;\n"
  2022. "} // namespace\n"
  2023. "} // namespace",
  2024. format("namespace out {\n"
  2025. "int i;\n"
  2026. "namespace in {\n"
  2027. "int i;\n"
  2028. "} // namespace\n"
  2029. "} // namespace",
  2030. Style));
  2031. }
  2032. TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
  2033. TEST_F(FormatTest, FormatsInlineASM) {
  2034. verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
  2035. verifyFormat("asm(\"nop\" ::: \"memory\");");
  2036. verifyFormat(
  2037. "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
  2038. " \"cpuid\\n\\t\"\n"
  2039. " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
  2040. " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
  2041. " : \"a\"(value));");
  2042. EXPECT_EQ(
  2043. "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
  2044. " __asm {\n"
  2045. " mov edx,[that] // vtable in edx\n"
  2046. " mov eax,methodIndex\n"
  2047. " call [edx][eax*4] // stdcall\n"
  2048. " }\n"
  2049. "}",
  2050. format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
  2051. " __asm {\n"
  2052. " mov edx,[that] // vtable in edx\n"
  2053. " mov eax,methodIndex\n"
  2054. " call [edx][eax*4] // stdcall\n"
  2055. " }\n"
  2056. "}"));
  2057. }
  2058. TEST_F(FormatTest, FormatTryCatch) {
  2059. verifyFormat("try {\n"
  2060. " throw a * b;\n"
  2061. "} catch (int a) {\n"
  2062. " // Do nothing.\n"
  2063. "} catch (...) {\n"
  2064. " exit(42);\n"
  2065. "}");
  2066. // Function-level try statements.
  2067. verifyFormat("int f() try { return 4; } catch (...) {\n"
  2068. " return 5;\n"
  2069. "}");
  2070. verifyFormat("class A {\n"
  2071. " int a;\n"
  2072. " A() try : a(0) {\n"
  2073. " } catch (...) {\n"
  2074. " throw;\n"
  2075. " }\n"
  2076. "};\n");
  2077. }
  2078. TEST_F(FormatTest, IncompleteTryCatchBlocks) {
  2079. verifyFormat("try {\n"
  2080. " f();\n"
  2081. "} catch {\n"
  2082. " g();\n"
  2083. "}");
  2084. verifyFormat("try {\n"
  2085. " f();\n"
  2086. "} catch (A a) MACRO(x) {\n"
  2087. " g();\n"
  2088. "} catch (B b) MACRO(x) {\n"
  2089. " g();\n"
  2090. "}");
  2091. }
  2092. TEST_F(FormatTest, FormatTryCatchBraceStyles) {
  2093. FormatStyle Style = getLLVMStyle();
  2094. Style.BreakBeforeBraces = FormatStyle::BS_Attach;
  2095. verifyFormat("try {\n"
  2096. " // something\n"
  2097. "} catch (...) {\n"
  2098. " // something\n"
  2099. "}",
  2100. Style);
  2101. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  2102. verifyFormat("try {\n"
  2103. " // something\n"
  2104. "}\n"
  2105. "catch (...) {\n"
  2106. " // something\n"
  2107. "}",
  2108. Style);
  2109. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  2110. verifyFormat("try\n"
  2111. "{\n"
  2112. " // something\n"
  2113. "}\n"
  2114. "catch (...)\n"
  2115. "{\n"
  2116. " // something\n"
  2117. "}",
  2118. Style);
  2119. Style.BreakBeforeBraces = FormatStyle::BS_GNU;
  2120. verifyFormat("try\n"
  2121. " {\n"
  2122. " // something\n"
  2123. " }\n"
  2124. "catch (...)\n"
  2125. " {\n"
  2126. " // something\n"
  2127. " }",
  2128. Style);
  2129. }
  2130. TEST_F(FormatTest, FormatObjCTryCatch) {
  2131. verifyFormat("@try {\n"
  2132. " f();\n"
  2133. "}\n"
  2134. "@catch (NSException e) {\n"
  2135. " @throw;\n"
  2136. "}\n"
  2137. "@finally {\n"
  2138. " exit(42);\n"
  2139. "}");
  2140. }
  2141. TEST_F(FormatTest, StaticInitializers) {
  2142. verifyFormat("static SomeClass SC = {1, 'a'};");
  2143. verifyFormat(
  2144. "static SomeClass WithALoooooooooooooooooooongName = {\n"
  2145. " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
  2146. // Here, everything other than the "}" would fit on a line.
  2147. verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
  2148. " 10000000000000000000000000};");
  2149. EXPECT_EQ("S s = {a,\n"
  2150. "\n"
  2151. " b};",
  2152. format("S s = {\n"
  2153. " a,\n"
  2154. "\n"
  2155. " b\n"
  2156. "};"));
  2157. // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
  2158. // line. However, the formatting looks a bit off and this probably doesn't
  2159. // happen often in practice.
  2160. verifyFormat("static int Variable[1] = {\n"
  2161. " {1000000000000000000000000000000000000}};",
  2162. getLLVMStyleWithColumns(40));
  2163. }
  2164. TEST_F(FormatTest, DesignatedInitializers) {
  2165. verifyFormat("const struct A a = {.a = 1, .b = 2};");
  2166. verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
  2167. " .bbbbbbbbbb = 2,\n"
  2168. " .cccccccccc = 3,\n"
  2169. " .dddddddddd = 4,\n"
  2170. " .eeeeeeeeee = 5};");
  2171. verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
  2172. " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
  2173. " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
  2174. " .ccccccccccccccccccccccccccc = 3,\n"
  2175. " .ddddddddddddddddddddddddddd = 4,\n"
  2176. " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
  2177. verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
  2178. }
  2179. TEST_F(FormatTest, NestedStaticInitializers) {
  2180. verifyFormat("static A x = {{{}}};\n");
  2181. verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
  2182. " {init1, init2, init3, init4}}};",
  2183. getLLVMStyleWithColumns(50));
  2184. verifyFormat("somes Status::global_reps[3] = {\n"
  2185. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  2186. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  2187. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
  2188. getLLVMStyleWithColumns(60));
  2189. verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
  2190. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  2191. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  2192. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
  2193. verifyFormat(
  2194. "CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
  2195. " {rect.fRight - rect.fLeft, rect.fBottom - rect.fTop}};");
  2196. verifyFormat(
  2197. "SomeArrayOfSomeType a = {\n"
  2198. " {{1, 2, 3},\n"
  2199. " {1, 2, 3},\n"
  2200. " {111111111111111111111111111111, 222222222222222222222222222222,\n"
  2201. " 333333333333333333333333333333},\n"
  2202. " {1, 2, 3},\n"
  2203. " {1, 2, 3}}};");
  2204. verifyFormat(
  2205. "SomeArrayOfSomeType a = {\n"
  2206. " {{1, 2, 3}},\n"
  2207. " {{1, 2, 3}},\n"
  2208. " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
  2209. " 333333333333333333333333333333}},\n"
  2210. " {{1, 2, 3}},\n"
  2211. " {{1, 2, 3}}};");
  2212. verifyFormat(
  2213. "struct {\n"
  2214. " unsigned bit;\n"
  2215. " const char *const name;\n"
  2216. "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
  2217. " {kOsWin, \"Windows\"},\n"
  2218. " {kOsLinux, \"Linux\"},\n"
  2219. " {kOsCrOS, \"Chrome OS\"}};");
  2220. verifyFormat(
  2221. "struct {\n"
  2222. " unsigned bit;\n"
  2223. " const char *const name;\n"
  2224. "} kBitsToOs[] = {\n"
  2225. " {kOsMac, \"Mac\"},\n"
  2226. " {kOsWin, \"Windows\"},\n"
  2227. " {kOsLinux, \"Linux\"},\n"
  2228. " {kOsCrOS, \"Chrome OS\"},\n"
  2229. "};");
  2230. }
  2231. TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
  2232. verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2233. " \\\n"
  2234. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
  2235. }
  2236. TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
  2237. verifyFormat("virtual void write(ELFWriter *writerrr,\n"
  2238. " OwningPtr<FileOutputBuffer> &buffer) = 0;");
  2239. }
  2240. TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
  2241. verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
  2242. getLLVMStyleWithColumns(40));
  2243. verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  2244. getLLVMStyleWithColumns(40));
  2245. EXPECT_EQ("#define Q \\\n"
  2246. " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n"
  2247. " \"aaaaaaaa.cpp\"",
  2248. format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  2249. getLLVMStyleWithColumns(40)));
  2250. }
  2251. TEST_F(FormatTest, UnderstandsLinePPDirective) {
  2252. EXPECT_EQ("# 123 \"A string literal\"",
  2253. format(" # 123 \"A string literal\""));
  2254. }
  2255. TEST_F(FormatTest, LayoutUnknownPPDirective) {
  2256. EXPECT_EQ("#;", format("#;"));
  2257. verifyFormat("#\n;\n;\n;");
  2258. }
  2259. TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
  2260. EXPECT_EQ("#line 42 \"test\"\n",
  2261. format("# \\\n line \\\n 42 \\\n \"test\"\n"));
  2262. EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n",
  2263. getLLVMStyleWithColumns(12)));
  2264. }
  2265. TEST_F(FormatTest, EndOfFileEndsPPDirective) {
  2266. EXPECT_EQ("#line 42 \"test\"",
  2267. format("# \\\n line \\\n 42 \\\n \"test\""));
  2268. EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B"));
  2269. }
  2270. TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
  2271. verifyFormat("#define A \\x20");
  2272. verifyFormat("#define A \\ x20");
  2273. EXPECT_EQ("#define A \\ x20", format("#define A \\ x20"));
  2274. verifyFormat("#define A ''");
  2275. verifyFormat("#define A ''qqq");
  2276. verifyFormat("#define A `qqq");
  2277. verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
  2278. EXPECT_EQ("const char *c = STRINGIFY(\n"
  2279. "\\na : b);",
  2280. format("const char * c = STRINGIFY(\n"
  2281. "\\na : b);"));
  2282. }
  2283. TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
  2284. verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
  2285. verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
  2286. verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
  2287. // FIXME: We never break before the macro name.
  2288. verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
  2289. verifyFormat("#define A A\n#define A A");
  2290. verifyFormat("#define A(X) A\n#define A A");
  2291. verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
  2292. verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
  2293. }
  2294. TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
  2295. EXPECT_EQ("// somecomment\n"
  2296. "#include \"a.h\"\n"
  2297. "#define A( \\\n"
  2298. " A, B)\n"
  2299. "#include \"b.h\"\n"
  2300. "// somecomment\n",
  2301. format(" // somecomment\n"
  2302. " #include \"a.h\"\n"
  2303. "#define A(A,\\\n"
  2304. " B)\n"
  2305. " #include \"b.h\"\n"
  2306. " // somecomment\n",
  2307. getLLVMStyleWithColumns(13)));
  2308. }
  2309. TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
  2310. TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
  2311. EXPECT_EQ("#define A \\\n"
  2312. " c; \\\n"
  2313. " e;\n"
  2314. "f;",
  2315. format("#define A c; e;\n"
  2316. "f;",
  2317. getLLVMStyleWithColumns(14)));
  2318. }
  2319. TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
  2320. TEST_F(FormatTest, AlwaysFormatsEntireMacroDefinitions) {
  2321. EXPECT_EQ("int i;\n"
  2322. "#define A \\\n"
  2323. " int i; \\\n"
  2324. " int j\n"
  2325. "int k;",
  2326. format("int i;\n"
  2327. "#define A \\\n"
  2328. " int i ; \\\n"
  2329. " int j\n"
  2330. "int k;",
  2331. 8, 0, getGoogleStyle())); // 8: position of "#define".
  2332. EXPECT_EQ("int i;\n"
  2333. "#define A \\\n"
  2334. " int i; \\\n"
  2335. " int j\n"
  2336. "int k;",
  2337. format("int i;\n"
  2338. "#define A \\\n"
  2339. " int i ; \\\n"
  2340. " int j\n"
  2341. "int k;",
  2342. 45, 0, getGoogleStyle())); // 45: position of "j".
  2343. }
  2344. TEST_F(FormatTest, MacroDefinitionInsideStatement) {
  2345. EXPECT_EQ("int x,\n"
  2346. "#define A\n"
  2347. " y;",
  2348. format("int x,\n#define A\ny;"));
  2349. }
  2350. TEST_F(FormatTest, HashInMacroDefinition) {
  2351. EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
  2352. verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
  2353. verifyFormat("#define A \\\n"
  2354. " { \\\n"
  2355. " f(#c); \\\n"
  2356. " }",
  2357. getLLVMStyleWithColumns(11));
  2358. verifyFormat("#define A(X) \\\n"
  2359. " void function##X()",
  2360. getLLVMStyleWithColumns(22));
  2361. verifyFormat("#define A(a, b, c) \\\n"
  2362. " void a##b##c()",
  2363. getLLVMStyleWithColumns(22));
  2364. verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
  2365. }
  2366. TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
  2367. EXPECT_EQ("#define A (x)", format("#define A (x)"));
  2368. EXPECT_EQ("#define A(x)", format("#define A(x)"));
  2369. }
  2370. TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
  2371. EXPECT_EQ("#define A b;", format("#define A \\\n"
  2372. " \\\n"
  2373. " b;",
  2374. getLLVMStyleWithColumns(25)));
  2375. EXPECT_EQ("#define A \\\n"
  2376. " \\\n"
  2377. " a; \\\n"
  2378. " b;",
  2379. format("#define A \\\n"
  2380. " \\\n"
  2381. " a; \\\n"
  2382. " b;",
  2383. getLLVMStyleWithColumns(11)));
  2384. EXPECT_EQ("#define A \\\n"
  2385. " a; \\\n"
  2386. " \\\n"
  2387. " b;",
  2388. format("#define A \\\n"
  2389. " a; \\\n"
  2390. " \\\n"
  2391. " b;",
  2392. getLLVMStyleWithColumns(11)));
  2393. }
  2394. TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
  2395. verifyFormat("#define A :");
  2396. verifyFormat("#define SOMECASES \\\n"
  2397. " case 1: \\\n"
  2398. " case 2\n",
  2399. getLLVMStyleWithColumns(20));
  2400. verifyFormat("#define A template <typename T>");
  2401. verifyFormat("#define STR(x) #x\n"
  2402. "f(STR(this_is_a_string_literal{));");
  2403. verifyFormat("#pragma omp threadprivate( \\\n"
  2404. " y)), // expected-warning",
  2405. getLLVMStyleWithColumns(28));
  2406. }
  2407. TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
  2408. verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
  2409. EXPECT_EQ("class A : public QObject {\n"
  2410. " Q_OBJECT\n"
  2411. "\n"
  2412. " A() {}\n"
  2413. "};",
  2414. format("class A : public QObject {\n"
  2415. " Q_OBJECT\n"
  2416. "\n"
  2417. " A() {\n}\n"
  2418. "} ;"));
  2419. EXPECT_EQ("SOME_MACRO\n"
  2420. "namespace {\n"
  2421. "void f();\n"
  2422. "}",
  2423. format("SOME_MACRO\n"
  2424. " namespace {\n"
  2425. "void f( );\n"
  2426. "}"));
  2427. // Only if the identifier contains at least 5 characters.
  2428. EXPECT_EQ("HTTP f();",
  2429. format("HTTP\nf();"));
  2430. EXPECT_EQ("MACRO\nf();",
  2431. format("MACRO\nf();"));
  2432. // Only if everything is upper case.
  2433. EXPECT_EQ("class A : public QObject {\n"
  2434. " Q_Object A() {}\n"
  2435. "};",
  2436. format("class A : public QObject {\n"
  2437. " Q_Object\n"
  2438. " A() {\n}\n"
  2439. "} ;"));
  2440. // Only if the next line can actually start an unwrapped line.
  2441. EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
  2442. format("SOME_WEIRD_LOG_MACRO\n"
  2443. "<< SomeThing;"));
  2444. verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
  2445. "(n, buffers))\n", getChromiumStyle(FormatStyle::LK_Cpp));
  2446. }
  2447. TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
  2448. EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  2449. "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  2450. "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  2451. "class X {};\n"
  2452. "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  2453. "int *createScopDetectionPass() { return 0; }",
  2454. format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  2455. " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  2456. " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  2457. " class X {};\n"
  2458. " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  2459. " int *createScopDetectionPass() { return 0; }"));
  2460. // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
  2461. // braces, so that inner block is indented one level more.
  2462. EXPECT_EQ("int q() {\n"
  2463. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  2464. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  2465. " IPC_END_MESSAGE_MAP()\n"
  2466. "}",
  2467. format("int q() {\n"
  2468. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  2469. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  2470. " IPC_END_MESSAGE_MAP()\n"
  2471. "}"));
  2472. // Same inside macros.
  2473. EXPECT_EQ("#define LIST(L) \\\n"
  2474. " L(A) \\\n"
  2475. " L(B) \\\n"
  2476. " L(C)",
  2477. format("#define LIST(L) \\\n"
  2478. " L(A) \\\n"
  2479. " L(B) \\\n"
  2480. " L(C)",
  2481. getGoogleStyle()));
  2482. // These must not be recognized as macros.
  2483. EXPECT_EQ("int q() {\n"
  2484. " f(x);\n"
  2485. " f(x) {}\n"
  2486. " f(x)->g();\n"
  2487. " f(x)->*g();\n"
  2488. " f(x).g();\n"
  2489. " f(x) = x;\n"
  2490. " f(x) += x;\n"
  2491. " f(x) -= x;\n"
  2492. " f(x) *= x;\n"
  2493. " f(x) /= x;\n"
  2494. " f(x) %= x;\n"
  2495. " f(x) &= x;\n"
  2496. " f(x) |= x;\n"
  2497. " f(x) ^= x;\n"
  2498. " f(x) >>= x;\n"
  2499. " f(x) <<= x;\n"
  2500. " f(x)[y].z();\n"
  2501. " LOG(INFO) << x;\n"
  2502. " ifstream(x) >> x;\n"
  2503. "}\n",
  2504. format("int q() {\n"
  2505. " f(x)\n;\n"
  2506. " f(x)\n {}\n"
  2507. " f(x)\n->g();\n"
  2508. " f(x)\n->*g();\n"
  2509. " f(x)\n.g();\n"
  2510. " f(x)\n = x;\n"
  2511. " f(x)\n += x;\n"
  2512. " f(x)\n -= x;\n"
  2513. " f(x)\n *= x;\n"
  2514. " f(x)\n /= x;\n"
  2515. " f(x)\n %= x;\n"
  2516. " f(x)\n &= x;\n"
  2517. " f(x)\n |= x;\n"
  2518. " f(x)\n ^= x;\n"
  2519. " f(x)\n >>= x;\n"
  2520. " f(x)\n <<= x;\n"
  2521. " f(x)\n[y].z();\n"
  2522. " LOG(INFO)\n << x;\n"
  2523. " ifstream(x)\n >> x;\n"
  2524. "}\n"));
  2525. EXPECT_EQ("int q() {\n"
  2526. " F(x)\n"
  2527. " if (1) {\n"
  2528. " }\n"
  2529. " F(x)\n"
  2530. " while (1) {\n"
  2531. " }\n"
  2532. " F(x)\n"
  2533. " G(x);\n"
  2534. " F(x)\n"
  2535. " try {\n"
  2536. " Q();\n"
  2537. " } catch (...) {\n"
  2538. " }\n"
  2539. "}\n",
  2540. format("int q() {\n"
  2541. "F(x)\n"
  2542. "if (1) {}\n"
  2543. "F(x)\n"
  2544. "while (1) {}\n"
  2545. "F(x)\n"
  2546. "G(x);\n"
  2547. "F(x)\n"
  2548. "try { Q(); } catch (...) {}\n"
  2549. "}\n"));
  2550. EXPECT_EQ("class A {\n"
  2551. " A() : t(0) {}\n"
  2552. " A(int i) noexcept() : {}\n"
  2553. " A(X x)\n" // FIXME: function-level try blocks are broken.
  2554. " try : t(0) {\n"
  2555. " } catch (...) {\n"
  2556. " }\n"
  2557. "};",
  2558. format("class A {\n"
  2559. " A()\n : t(0) {}\n"
  2560. " A(int i)\n noexcept() : {}\n"
  2561. " A(X x)\n"
  2562. " try : t(0) {} catch (...) {}\n"
  2563. "};"));
  2564. EXPECT_EQ(
  2565. "class SomeClass {\n"
  2566. "public:\n"
  2567. " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2568. "};",
  2569. format("class SomeClass {\n"
  2570. "public:\n"
  2571. " SomeClass()\n"
  2572. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2573. "};"));
  2574. EXPECT_EQ(
  2575. "class SomeClass {\n"
  2576. "public:\n"
  2577. " SomeClass()\n"
  2578. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2579. "};",
  2580. format("class SomeClass {\n"
  2581. "public:\n"
  2582. " SomeClass()\n"
  2583. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2584. "};", getLLVMStyleWithColumns(40)));
  2585. }
  2586. TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
  2587. verifyFormat("#define A \\\n"
  2588. " f({ \\\n"
  2589. " g(); \\\n"
  2590. " });", getLLVMStyleWithColumns(11));
  2591. }
  2592. TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
  2593. EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
  2594. }
  2595. TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
  2596. verifyFormat("{\n { a #c; }\n}");
  2597. }
  2598. TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
  2599. EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
  2600. format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
  2601. EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
  2602. format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
  2603. }
  2604. TEST_F(FormatTest, EscapedNewlineAtStartOfToken) {
  2605. EXPECT_EQ(
  2606. "#define A \\\n int i; \\\n int j;",
  2607. format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
  2608. EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
  2609. }
  2610. TEST_F(FormatTest, NoEscapedNewlineHandlingInBlockComments) {
  2611. EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/"));
  2612. }
  2613. TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
  2614. verifyFormat("#define A \\\n"
  2615. " int v( \\\n"
  2616. " a); \\\n"
  2617. " int i;",
  2618. getLLVMStyleWithColumns(11));
  2619. }
  2620. TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
  2621. EXPECT_EQ(
  2622. "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2623. " \\\n"
  2624. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2625. "\n"
  2626. "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2627. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
  2628. format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2629. "\\\n"
  2630. "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2631. " \n"
  2632. " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2633. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
  2634. }
  2635. TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
  2636. EXPECT_EQ("int\n"
  2637. "#define A\n"
  2638. " a;",
  2639. format("int\n#define A\na;"));
  2640. verifyFormat("functionCallTo(\n"
  2641. " someOtherFunction(\n"
  2642. " withSomeParameters, whichInSequence,\n"
  2643. " areLongerThanALine(andAnotherCall,\n"
  2644. "#define A B\n"
  2645. " withMoreParamters,\n"
  2646. " whichStronglyInfluenceTheLayout),\n"
  2647. " andMoreParameters),\n"
  2648. " trailing);",
  2649. getLLVMStyleWithColumns(69));
  2650. verifyFormat("Foo::Foo()\n"
  2651. "#ifdef BAR\n"
  2652. " : baz(0)\n"
  2653. "#endif\n"
  2654. "{\n"
  2655. "}");
  2656. verifyFormat("void f() {\n"
  2657. " if (true)\n"
  2658. "#ifdef A\n"
  2659. " f(42);\n"
  2660. " x();\n"
  2661. "#else\n"
  2662. " g();\n"
  2663. " x();\n"
  2664. "#endif\n"
  2665. "}");
  2666. verifyFormat("void f(param1, param2,\n"
  2667. " param3,\n"
  2668. "#ifdef A\n"
  2669. " param4(param5,\n"
  2670. "#ifdef A1\n"
  2671. " param6,\n"
  2672. "#ifdef A2\n"
  2673. " param7),\n"
  2674. "#else\n"
  2675. " param8),\n"
  2676. " param9,\n"
  2677. "#endif\n"
  2678. " param10,\n"
  2679. "#endif\n"
  2680. " param11)\n"
  2681. "#else\n"
  2682. " param12)\n"
  2683. "#endif\n"
  2684. "{\n"
  2685. " x();\n"
  2686. "}",
  2687. getLLVMStyleWithColumns(28));
  2688. verifyFormat("#if 1\n"
  2689. "int i;");
  2690. verifyFormat(
  2691. "#if 1\n"
  2692. "#endif\n"
  2693. "#if 1\n"
  2694. "#else\n"
  2695. "#endif\n");
  2696. verifyFormat("DEBUG({\n"
  2697. " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2698. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  2699. "});\n"
  2700. "#if a\n"
  2701. "#else\n"
  2702. "#endif");
  2703. }
  2704. TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
  2705. verifyFormat("#endif\n"
  2706. "#if B");
  2707. }
  2708. TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
  2709. FormatStyle SingleLine = getLLVMStyle();
  2710. SingleLine.AllowShortIfStatementsOnASingleLine = true;
  2711. verifyFormat(
  2712. "#if 0\n"
  2713. "#elif 1\n"
  2714. "#endif\n"
  2715. "void foo() {\n"
  2716. " if (test) foo2();\n"
  2717. "}",
  2718. SingleLine);
  2719. }
  2720. TEST_F(FormatTest, LayoutBlockInsideParens) {
  2721. EXPECT_EQ("functionCall({ int i; });", format(" functionCall ( {int i;} );"));
  2722. EXPECT_EQ("functionCall({\n"
  2723. " int i;\n"
  2724. " int j;\n"
  2725. "});",
  2726. format(" functionCall ( {int i;int j;} );"));
  2727. EXPECT_EQ("functionCall({\n"
  2728. " int i;\n"
  2729. " int j;\n"
  2730. "}, aaaa, bbbb, cccc);",
  2731. format(" functionCall ( {int i;int j;}, aaaa, bbbb, cccc);"));
  2732. EXPECT_EQ("functionCall(\n"
  2733. " {\n"
  2734. " int i;\n"
  2735. " int j;\n"
  2736. " },\n"
  2737. " aaaa, bbbb, // comment\n"
  2738. " cccc);",
  2739. format(" functionCall ( {int i;int j;}, aaaa, bbbb, // comment\n"
  2740. "cccc);"));
  2741. EXPECT_EQ("functionCall(aaaa, bbbb, { int i; });",
  2742. format(" functionCall (aaaa, bbbb, {int i;});"));
  2743. EXPECT_EQ("functionCall(aaaa, bbbb, {\n"
  2744. " int i;\n"
  2745. " int j;\n"
  2746. "});",
  2747. format(" functionCall (aaaa, bbbb, {int i;int j;});"));
  2748. EXPECT_EQ("functionCall(aaaa, bbbb, { int i; });",
  2749. format(" functionCall (aaaa, bbbb, {int i;});"));
  2750. verifyFormat(
  2751. "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
  2752. " {\n"
  2753. " int i; // break\n"
  2754. " },\n"
  2755. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  2756. " ccccccccccccccccc));");
  2757. verifyFormat("DEBUG({\n"
  2758. " if (a)\n"
  2759. " f();\n"
  2760. "});");
  2761. }
  2762. TEST_F(FormatTest, LayoutBlockInsideStatement) {
  2763. EXPECT_EQ("SOME_MACRO { int i; }\n"
  2764. "int i;",
  2765. format(" SOME_MACRO {int i;} int i;"));
  2766. }
  2767. TEST_F(FormatTest, LayoutNestedBlocks) {
  2768. verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
  2769. " struct s {\n"
  2770. " int i;\n"
  2771. " };\n"
  2772. " s kBitsToOs[] = {{10}};\n"
  2773. " for (int i = 0; i < 10; ++i)\n"
  2774. " return;\n"
  2775. "}");
  2776. verifyFormat("call(parameter, {\n"
  2777. " something();\n"
  2778. " // Comment using all columns.\n"
  2779. " somethingelse();\n"
  2780. "});",
  2781. getLLVMStyleWithColumns(40));
  2782. verifyFormat("DEBUG( //\n"
  2783. " { f(); }, a);");
  2784. verifyFormat("DEBUG( //\n"
  2785. " {\n"
  2786. " f(); //\n"
  2787. " },\n"
  2788. " a);");
  2789. EXPECT_EQ("call(parameter, {\n"
  2790. " something();\n"
  2791. " // Comment too\n"
  2792. " // looooooooooong.\n"
  2793. " somethingElse();\n"
  2794. "});",
  2795. format("call(parameter, {\n"
  2796. " something();\n"
  2797. " // Comment too looooooooooong.\n"
  2798. " somethingElse();\n"
  2799. "});",
  2800. getLLVMStyleWithColumns(29)));
  2801. EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });"));
  2802. EXPECT_EQ("DEBUG({ // comment\n"
  2803. " int i;\n"
  2804. "});",
  2805. format("DEBUG({ // comment\n"
  2806. "int i;\n"
  2807. "});"));
  2808. EXPECT_EQ("DEBUG({\n"
  2809. " int i;\n"
  2810. "\n"
  2811. " // comment\n"
  2812. " int j;\n"
  2813. "});",
  2814. format("DEBUG({\n"
  2815. " int i;\n"
  2816. "\n"
  2817. " // comment\n"
  2818. " int j;\n"
  2819. "});"));
  2820. verifyFormat("DEBUG({\n"
  2821. " if (a)\n"
  2822. " return;\n"
  2823. "});");
  2824. verifyGoogleFormat("DEBUG({\n"
  2825. " if (a) return;\n"
  2826. "});");
  2827. FormatStyle Style = getGoogleStyle();
  2828. Style.ColumnLimit = 45;
  2829. verifyFormat("Debug(aaaaa, {\n"
  2830. " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
  2831. "}, a);",
  2832. Style);
  2833. }
  2834. TEST_F(FormatTest, IndividualStatementsOfNestedBlocks) {
  2835. EXPECT_EQ("DEBUG({\n"
  2836. " int i;\n"
  2837. " int j;\n"
  2838. "});",
  2839. format("DEBUG( {\n"
  2840. " int i;\n"
  2841. " int j;\n"
  2842. "} ) ;",
  2843. 20, 1, getLLVMStyle()));
  2844. EXPECT_EQ("DEBUG( {\n"
  2845. " int i;\n"
  2846. " int j;\n"
  2847. "} ) ;",
  2848. format("DEBUG( {\n"
  2849. " int i;\n"
  2850. " int j;\n"
  2851. "} ) ;",
  2852. 41, 1, getLLVMStyle()));
  2853. EXPECT_EQ("DEBUG( {\n"
  2854. " int i;\n"
  2855. " int j;\n"
  2856. "} ) ;",
  2857. format("DEBUG( {\n"
  2858. " int i;\n"
  2859. " int j;\n"
  2860. "} ) ;",
  2861. 41, 1, getLLVMStyle()));
  2862. EXPECT_EQ("DEBUG({\n"
  2863. " int i;\n"
  2864. " int j;\n"
  2865. "});",
  2866. format("DEBUG( {\n"
  2867. " int i;\n"
  2868. " int j;\n"
  2869. "} ) ;",
  2870. 20, 1, getLLVMStyle()));
  2871. EXPECT_EQ("Debug({\n"
  2872. " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2873. " return;\n"
  2874. " },\n"
  2875. " a);",
  2876. format("Debug({\n"
  2877. " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2878. " return;\n"
  2879. " },\n"
  2880. " a);",
  2881. 50, 1, getLLVMStyle()));
  2882. EXPECT_EQ("DEBUG({\n"
  2883. " DEBUG({\n"
  2884. " int a;\n"
  2885. " int b;\n"
  2886. " }) ;\n"
  2887. "});",
  2888. format("DEBUG({\n"
  2889. " DEBUG({\n"
  2890. " int a;\n"
  2891. " int b;\n" // Format this line only.
  2892. " }) ;\n" // Don't touch this line.
  2893. "});",
  2894. 35, 0, getLLVMStyle()));
  2895. EXPECT_EQ("DEBUG({\n"
  2896. " int a; //\n"
  2897. "});",
  2898. format("DEBUG({\n"
  2899. " int a; //\n"
  2900. "});",
  2901. 0, 0, getLLVMStyle()));
  2902. }
  2903. TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
  2904. EXPECT_EQ("{}", format("{}"));
  2905. verifyFormat("enum E {};");
  2906. verifyFormat("enum E {}");
  2907. }
  2908. //===----------------------------------------------------------------------===//
  2909. // Line break tests.
  2910. //===----------------------------------------------------------------------===//
  2911. TEST_F(FormatTest, PreventConfusingIndents) {
  2912. verifyFormat(
  2913. "void f() {\n"
  2914. " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
  2915. " parameter, parameter, parameter)),\n"
  2916. " SecondLongCall(parameter));\n"
  2917. "}");
  2918. verifyFormat(
  2919. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2920. " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2921. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2922. " aaaaaaaaaaaaaaaaaaaaaaaa);");
  2923. verifyFormat(
  2924. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2925. " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
  2926. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  2927. " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
  2928. verifyFormat(
  2929. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  2930. " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
  2931. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
  2932. " aaaaaaaaaaaaaaaaaaaaaaaa>;");
  2933. verifyFormat("int a = bbbb && ccc && fffff(\n"
  2934. "#define A Just forcing a new line\n"
  2935. " ddd);");
  2936. }
  2937. TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
  2938. verifyFormat(
  2939. "bool aaaaaaa =\n"
  2940. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
  2941. " bbbbbbbb();");
  2942. verifyFormat(
  2943. "bool aaaaaaa =\n"
  2944. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
  2945. " bbbbbbbb();");
  2946. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  2947. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
  2948. " ccccccccc == ddddddddddd;");
  2949. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  2950. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
  2951. " ccccccccc == ddddddddddd;");
  2952. verifyFormat(
  2953. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  2954. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
  2955. " ccccccccc == ddddddddddd;");
  2956. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  2957. " aaaaaa) &&\n"
  2958. " bbbbbb && cccccc;");
  2959. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  2960. " aaaaaa) >>\n"
  2961. " bbbbbb;");
  2962. verifyFormat("Whitespaces.addUntouchableComment(\n"
  2963. " SourceMgr.getSpellingColumnNumber(\n"
  2964. " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
  2965. " 1);");
  2966. verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2967. " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
  2968. " cccccc) {\n}");
  2969. verifyFormat("b = a &&\n"
  2970. " // Comment\n"
  2971. " b.c && d;");
  2972. // If the LHS of a comparison is not a binary expression itself, the
  2973. // additional linebreak confuses many people.
  2974. verifyFormat(
  2975. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2976. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
  2977. "}");
  2978. verifyFormat(
  2979. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2980. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  2981. "}");
  2982. verifyFormat(
  2983. "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
  2984. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  2985. "}");
  2986. // Even explicit parentheses stress the precedence enough to make the
  2987. // additional break unnecessary.
  2988. verifyFormat(
  2989. "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2990. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  2991. "}");
  2992. // This cases is borderline, but with the indentation it is still readable.
  2993. verifyFormat(
  2994. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2995. " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2996. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  2997. "}",
  2998. getLLVMStyleWithColumns(75));
  2999. // If the LHS is a binary expression, we should still use the additional break
  3000. // as otherwise the formatting hides the operator precedence.
  3001. verifyFormat(
  3002. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3003. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3004. " 5) {\n"
  3005. "}");
  3006. FormatStyle OnePerLine = getLLVMStyle();
  3007. OnePerLine.BinPackParameters = false;
  3008. verifyFormat(
  3009. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3010. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3011. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
  3012. OnePerLine);
  3013. }
  3014. TEST_F(FormatTest, ExpressionIndentation) {
  3015. verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3016. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3017. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3018. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3019. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
  3020. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
  3021. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3022. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
  3023. " ccccccccccccccccccccccccccccccccccccccccc;");
  3024. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3025. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3026. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3027. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3028. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3029. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3030. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3031. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3032. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3033. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3034. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3035. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3036. verifyFormat("if () {\n"
  3037. "} else if (aaaaa &&\n"
  3038. " bbbbb > // break\n"
  3039. " ccccc) {\n"
  3040. "}");
  3041. // Presence of a trailing comment used to change indentation of b.
  3042. verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
  3043. " b;\n"
  3044. "return aaaaaaaaaaaaaaaaaaa +\n"
  3045. " b; //",
  3046. getLLVMStyleWithColumns(30));
  3047. }
  3048. TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
  3049. // Not sure what the best system is here. Like this, the LHS can be found
  3050. // immediately above an operator (everything with the same or a higher
  3051. // indent). The RHS is aligned right of the operator and so compasses
  3052. // everything until something with the same indent as the operator is found.
  3053. // FIXME: Is this a good system?
  3054. FormatStyle Style = getLLVMStyle();
  3055. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  3056. verifyFormat(
  3057. "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3058. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3059. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3060. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3061. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3062. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3063. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3064. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3065. " > ccccccccccccccccccccccccccccccccccccccccc;",
  3066. Style);
  3067. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3068. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3069. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3070. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3071. Style);
  3072. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3073. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3074. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3075. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3076. Style);
  3077. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3078. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3079. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3080. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3081. Style);
  3082. verifyFormat("if () {\n"
  3083. "} else if (aaaaa\n"
  3084. " && bbbbb // break\n"
  3085. " > ccccc) {\n"
  3086. "}",
  3087. Style);
  3088. verifyFormat("return (a)\n"
  3089. " // comment\n"
  3090. " + b;",
  3091. Style);
  3092. verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3093. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3094. " + cc;",
  3095. Style);
  3096. // Forced by comments.
  3097. verifyFormat(
  3098. "unsigned ContentSize =\n"
  3099. " sizeof(int16_t) // DWARF ARange version number\n"
  3100. " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
  3101. " + sizeof(int8_t) // Pointer Size (in bytes)\n"
  3102. " + sizeof(int8_t); // Segment Size (in bytes)");
  3103. verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
  3104. " == boost::fusion::at_c<1>(iiii).second;",
  3105. Style);
  3106. Style.ColumnLimit = 60;
  3107. verifyFormat("zzzzzzzzzz\n"
  3108. " = bbbbbbbbbbbbbbbbb\n"
  3109. " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3110. Style);
  3111. }
  3112. TEST_F(FormatTest, NoOperandAlignment) {
  3113. FormatStyle Style = getLLVMStyle();
  3114. Style.AlignOperands = false;
  3115. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
  3116. verifyFormat(
  3117. "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3118. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3119. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3120. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3121. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3122. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3123. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3124. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3125. " > ccccccccccccccccccccccccccccccccccccccccc;",
  3126. Style);
  3127. verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3128. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3129. " + cc;",
  3130. Style);
  3131. verifyFormat("int a = aa\n"
  3132. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3133. " * cccccccccccccccccccccccccccccccccccc;",
  3134. Style);
  3135. Style.AlignAfterOpenBracket = false;
  3136. verifyFormat("return (a > b\n"
  3137. " // comment1\n"
  3138. " // comment2\n"
  3139. " || c);",
  3140. Style);
  3141. }
  3142. TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
  3143. FormatStyle Style = getLLVMStyle();
  3144. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
  3145. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3146. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3147. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
  3148. Style);
  3149. }
  3150. TEST_F(FormatTest, ConstructorInitializers) {
  3151. verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
  3152. verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
  3153. getLLVMStyleWithColumns(45));
  3154. verifyFormat("Constructor()\n"
  3155. " : Inttializer(FitsOnTheLine) {}",
  3156. getLLVMStyleWithColumns(44));
  3157. verifyFormat("Constructor()\n"
  3158. " : Inttializer(FitsOnTheLine) {}",
  3159. getLLVMStyleWithColumns(43));
  3160. verifyFormat(
  3161. "SomeClass::Constructor()\n"
  3162. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  3163. verifyFormat(
  3164. "SomeClass::Constructor()\n"
  3165. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3166. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
  3167. verifyFormat(
  3168. "SomeClass::Constructor()\n"
  3169. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3170. " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  3171. verifyFormat("Constructor()\n"
  3172. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3173. " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3174. " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3175. " aaaaaaaaaaaaaaaaaaaaaaa() {}");
  3176. verifyFormat("Constructor()\n"
  3177. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3178. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3179. verifyFormat("Constructor(int Parameter = 0)\n"
  3180. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
  3181. " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
  3182. verifyFormat("Constructor()\n"
  3183. " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
  3184. "}",
  3185. getLLVMStyleWithColumns(60));
  3186. verifyFormat("Constructor()\n"
  3187. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3188. " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
  3189. // Here a line could be saved by splitting the second initializer onto two
  3190. // lines, but that is not desirable.
  3191. verifyFormat("Constructor()\n"
  3192. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3193. " aaaaaaaaaaa(aaaaaaaaaaa),\n"
  3194. " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3195. FormatStyle OnePerLine = getLLVMStyle();
  3196. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  3197. verifyFormat("SomeClass::Constructor()\n"
  3198. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3199. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3200. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  3201. OnePerLine);
  3202. verifyFormat("SomeClass::Constructor()\n"
  3203. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
  3204. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3205. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  3206. OnePerLine);
  3207. verifyFormat("MyClass::MyClass(int var)\n"
  3208. " : some_var_(var), // 4 space indent\n"
  3209. " some_other_var_(var + 1) { // lined up\n"
  3210. "}",
  3211. OnePerLine);
  3212. verifyFormat("Constructor()\n"
  3213. " : aaaaa(aaaaaa),\n"
  3214. " aaaaa(aaaaaa),\n"
  3215. " aaaaa(aaaaaa),\n"
  3216. " aaaaa(aaaaaa),\n"
  3217. " aaaaa(aaaaaa) {}",
  3218. OnePerLine);
  3219. verifyFormat("Constructor()\n"
  3220. " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
  3221. " aaaaaaaaaaaaaaaaaaaaaa) {}",
  3222. OnePerLine);
  3223. EXPECT_EQ("Constructor()\n"
  3224. " : // Comment forcing unwanted break.\n"
  3225. " aaaa(aaaa) {}",
  3226. format("Constructor() :\n"
  3227. " // Comment forcing unwanted break.\n"
  3228. " aaaa(aaaa) {}"));
  3229. }
  3230. TEST_F(FormatTest, MemoizationTests) {
  3231. // This breaks if the memoization lookup does not take \c Indent and
  3232. // \c LastSpace into account.
  3233. verifyFormat(
  3234. "extern CFRunLoopTimerRef\n"
  3235. "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
  3236. " CFTimeInterval interval, CFOptionFlags flags,\n"
  3237. " CFIndex order, CFRunLoopTimerCallBack callout,\n"
  3238. " CFRunLoopTimerContext *context) {}");
  3239. // Deep nesting somewhat works around our memoization.
  3240. verifyFormat(
  3241. "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3242. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3243. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3244. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3245. " aaaaa())))))))))))))))))))))))))))))))))))))));",
  3246. getLLVMStyleWithColumns(65));
  3247. verifyFormat(
  3248. "aaaaa(\n"
  3249. " aaaaa,\n"
  3250. " aaaaa(\n"
  3251. " aaaaa,\n"
  3252. " aaaaa(\n"
  3253. " aaaaa,\n"
  3254. " aaaaa(\n"
  3255. " aaaaa,\n"
  3256. " aaaaa(\n"
  3257. " aaaaa,\n"
  3258. " aaaaa(\n"
  3259. " aaaaa,\n"
  3260. " aaaaa(\n"
  3261. " aaaaa,\n"
  3262. " aaaaa(\n"
  3263. " aaaaa,\n"
  3264. " aaaaa(\n"
  3265. " aaaaa,\n"
  3266. " aaaaa(\n"
  3267. " aaaaa,\n"
  3268. " aaaaa(\n"
  3269. " aaaaa,\n"
  3270. " aaaaa(\n"
  3271. " aaaaa,\n"
  3272. " aaaaa))))))))))));",
  3273. getLLVMStyleWithColumns(65));
  3274. verifyFormat(
  3275. "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"
  3276. " a),\n"
  3277. " a),\n"
  3278. " a),\n"
  3279. " a),\n"
  3280. " a),\n"
  3281. " a),\n"
  3282. " a),\n"
  3283. " a),\n"
  3284. " a),\n"
  3285. " a),\n"
  3286. " a),\n"
  3287. " a),\n"
  3288. " a),\n"
  3289. " a),\n"
  3290. " a),\n"
  3291. " a),\n"
  3292. " a)",
  3293. getLLVMStyleWithColumns(65));
  3294. // This test takes VERY long when memoization is broken.
  3295. FormatStyle OnePerLine = getLLVMStyle();
  3296. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  3297. OnePerLine.BinPackParameters = false;
  3298. std::string input = "Constructor()\n"
  3299. " : aaaa(a,\n";
  3300. for (unsigned i = 0, e = 80; i != e; ++i) {
  3301. input += " a,\n";
  3302. }
  3303. input += " a) {}";
  3304. verifyFormat(input, OnePerLine);
  3305. }
  3306. TEST_F(FormatTest, BreaksAsHighAsPossible) {
  3307. verifyFormat(
  3308. "void f() {\n"
  3309. " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
  3310. " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
  3311. " f();\n"
  3312. "}");
  3313. verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
  3314. " Intervals[i - 1].getRange().getLast()) {\n}");
  3315. }
  3316. TEST_F(FormatTest, BreaksFunctionDeclarations) {
  3317. // Principially, we break function declarations in a certain order:
  3318. // 1) break amongst arguments.
  3319. verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
  3320. " Cccccccccccccc cccccccccccccc);");
  3321. verifyFormat(
  3322. "template <class TemplateIt>\n"
  3323. "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
  3324. " TemplateIt *stop) {}");
  3325. // 2) break after return type.
  3326. verifyFormat(
  3327. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3328. "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
  3329. getGoogleStyle());
  3330. // 3) break after (.
  3331. verifyFormat(
  3332. "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
  3333. " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
  3334. getGoogleStyle());
  3335. // 4) break before after nested name specifiers.
  3336. verifyFormat(
  3337. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3338. "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
  3339. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
  3340. getGoogleStyle());
  3341. // However, there are exceptions, if a sufficient amount of lines can be
  3342. // saved.
  3343. // FIXME: The precise cut-offs wrt. the number of saved lines might need some
  3344. // more adjusting.
  3345. verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  3346. " Cccccccccccccc cccccccccc,\n"
  3347. " Cccccccccccccc cccccccccc,\n"
  3348. " Cccccccccccccc cccccccccc,\n"
  3349. " Cccccccccccccc cccccccccc);");
  3350. verifyFormat(
  3351. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3352. "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3353. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3354. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
  3355. getGoogleStyle());
  3356. verifyFormat(
  3357. "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  3358. " Cccccccccccccc cccccccccc,\n"
  3359. " Cccccccccccccc cccccccccc,\n"
  3360. " Cccccccccccccc cccccccccc,\n"
  3361. " Cccccccccccccc cccccccccc,\n"
  3362. " Cccccccccccccc cccccccccc,\n"
  3363. " Cccccccccccccc cccccccccc);");
  3364. verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3365. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3366. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3367. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3368. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
  3369. // Break after multi-line parameters.
  3370. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3371. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3372. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3373. " bbbb bbbb);");
  3374. verifyFormat("void SomeLoooooooooooongFunction(\n"
  3375. " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  3376. " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3377. " int bbbbbbbbbbbbb);");
  3378. // Treat overloaded operators like other functions.
  3379. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3380. "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
  3381. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3382. "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
  3383. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3384. "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
  3385. verifyGoogleFormat(
  3386. "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
  3387. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  3388. verifyGoogleFormat(
  3389. "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
  3390. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  3391. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3392. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  3393. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
  3394. "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  3395. verifyGoogleFormat(
  3396. "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
  3397. "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3398. " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
  3399. }
  3400. TEST_F(FormatTest, TrailingReturnType) {
  3401. verifyFormat("auto foo() -> int;\n");
  3402. verifyFormat("struct S {\n"
  3403. " auto bar() const -> int;\n"
  3404. "};");
  3405. verifyFormat("template <size_t Order, typename T>\n"
  3406. "auto load_img(const std::string &filename)\n"
  3407. " -> alias::tensor<Order, T, mem::tag::cpu> {}");
  3408. verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
  3409. " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
  3410. verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
  3411. // Not trailing return types.
  3412. verifyFormat("void f() { auto a = b->c(); }");
  3413. }
  3414. TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
  3415. // Avoid breaking before trailing 'const' or other trailing annotations, if
  3416. // they are not function-like.
  3417. FormatStyle Style = getGoogleStyle();
  3418. Style.ColumnLimit = 47;
  3419. verifyFormat("void someLongFunction(\n"
  3420. " int someLoooooooooooooongParameter) const {\n}",
  3421. getLLVMStyleWithColumns(47));
  3422. verifyFormat("LoooooongReturnType\n"
  3423. "someLoooooooongFunction() const {}",
  3424. getLLVMStyleWithColumns(47));
  3425. verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
  3426. " const {}",
  3427. Style);
  3428. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3429. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
  3430. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3431. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
  3432. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3433. " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
  3434. verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
  3435. " aaaaaaaaaaa aaaaa) const override;");
  3436. verifyGoogleFormat(
  3437. "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3438. " const override;");
  3439. // Even if the first parameter has to be wrapped.
  3440. verifyFormat("void someLongFunction(\n"
  3441. " int someLongParameter) const {}",
  3442. getLLVMStyleWithColumns(46));
  3443. verifyFormat("void someLongFunction(\n"
  3444. " int someLongParameter) const {}",
  3445. Style);
  3446. verifyFormat("void someLongFunction(\n"
  3447. " int someLongParameter) override {}",
  3448. Style);
  3449. verifyFormat("void someLongFunction(\n"
  3450. " int someLongParameter) OVERRIDE {}",
  3451. Style);
  3452. verifyFormat("void someLongFunction(\n"
  3453. " int someLongParameter) final {}",
  3454. Style);
  3455. verifyFormat("void someLongFunction(\n"
  3456. " int someLongParameter) FINAL {}",
  3457. Style);
  3458. verifyFormat("void someLongFunction(\n"
  3459. " int parameter) const override {}",
  3460. Style);
  3461. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  3462. verifyFormat("void someLongFunction(\n"
  3463. " int someLongParameter) const\n"
  3464. "{\n"
  3465. "}",
  3466. Style);
  3467. // Unless these are unknown annotations.
  3468. verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
  3469. " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3470. " LONG_AND_UGLY_ANNOTATION;");
  3471. // Breaking before function-like trailing annotations is fine to keep them
  3472. // close to their arguments.
  3473. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3474. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  3475. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  3476. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  3477. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  3478. " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
  3479. verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
  3480. " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
  3481. verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
  3482. verifyFormat(
  3483. "void aaaaaaaaaaaaaaaaaa()\n"
  3484. " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3485. " aaaaaaaaaaaaaaaaaaaaaaaaa));");
  3486. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3487. " __attribute__((unused));");
  3488. verifyGoogleFormat(
  3489. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3490. " GUARDED_BY(aaaaaaaaaaaa);");
  3491. verifyGoogleFormat(
  3492. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3493. " GUARDED_BY(aaaaaaaaaaaa);");
  3494. verifyGoogleFormat(
  3495. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
  3496. " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3497. }
  3498. TEST_F(FormatTest, BreaksDesireably) {
  3499. verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  3500. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  3501. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
  3502. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3503. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  3504. "}");
  3505. verifyFormat(
  3506. "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3507. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3508. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3509. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3510. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3511. verifyFormat(
  3512. "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3513. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  3514. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3515. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
  3516. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3517. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3518. verifyFormat(
  3519. "void f() {\n"
  3520. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
  3521. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3522. "}");
  3523. verifyFormat(
  3524. "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3525. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3526. verifyFormat(
  3527. "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3528. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3529. verifyFormat(
  3530. "aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3531. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3532. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3533. // Indent consistently independent of call expression.
  3534. verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
  3535. " dddddddddddddddddddddddddddddd));\n"
  3536. "aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3537. " dddddddddddddddddddddddddddddd));");
  3538. // This test case breaks on an incorrect memoization, i.e. an optimization not
  3539. // taking into account the StopAt value.
  3540. verifyFormat(
  3541. "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3542. " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3543. " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3544. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3545. verifyFormat("{\n {\n {\n"
  3546. " Annotation.SpaceRequiredBefore =\n"
  3547. " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
  3548. " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
  3549. " }\n }\n}");
  3550. // Break on an outer level if there was a break on an inner level.
  3551. EXPECT_EQ("f(g(h(a, // comment\n"
  3552. " b, c),\n"
  3553. " d, e),\n"
  3554. " x, y);",
  3555. format("f(g(h(a, // comment\n"
  3556. " b, c), d, e), x, y);"));
  3557. // Prefer breaking similar line breaks.
  3558. verifyFormat(
  3559. "const int kTrackingOptions = NSTrackingMouseMoved |\n"
  3560. " NSTrackingMouseEnteredAndExited |\n"
  3561. " NSTrackingActiveAlways;");
  3562. }
  3563. TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
  3564. FormatStyle NoBinPacking = getGoogleStyle();
  3565. NoBinPacking.BinPackParameters = false;
  3566. NoBinPacking.BinPackArguments = true;
  3567. verifyFormat("void f() {\n"
  3568. " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
  3569. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3570. "}",
  3571. NoBinPacking);
  3572. verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
  3573. " int aaaaaaaaaaaaaaaaaaaa,\n"
  3574. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3575. NoBinPacking);
  3576. }
  3577. TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
  3578. FormatStyle NoBinPacking = getGoogleStyle();
  3579. NoBinPacking.BinPackParameters = false;
  3580. NoBinPacking.BinPackArguments = false;
  3581. verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
  3582. " aaaaaaaaaaaaaaaaaaaa,\n"
  3583. " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
  3584. NoBinPacking);
  3585. verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
  3586. " aaaaaaaaaaaaa,\n"
  3587. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
  3588. NoBinPacking);
  3589. verifyFormat(
  3590. "aaaaaaaa(aaaaaaaaaaaaa,\n"
  3591. " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3592. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  3593. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3594. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
  3595. NoBinPacking);
  3596. verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  3597. " .aaaaaaaaaaaaaaaaaa();",
  3598. NoBinPacking);
  3599. verifyFormat("void f() {\n"
  3600. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3601. " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
  3602. "}",
  3603. NoBinPacking);
  3604. verifyFormat(
  3605. "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3606. " aaaaaaaaaaaa,\n"
  3607. " aaaaaaaaaaaa);",
  3608. NoBinPacking);
  3609. verifyFormat(
  3610. "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
  3611. " ddddddddddddddddddddddddddddd),\n"
  3612. " test);",
  3613. NoBinPacking);
  3614. verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3615. " aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3616. " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;",
  3617. NoBinPacking);
  3618. verifyFormat("a(\"a\"\n"
  3619. " \"a\",\n"
  3620. " a);");
  3621. NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
  3622. verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
  3623. " aaaaaaaaa,\n"
  3624. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3625. NoBinPacking);
  3626. verifyFormat(
  3627. "void f() {\n"
  3628. " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  3629. " .aaaaaaa();\n"
  3630. "}",
  3631. NoBinPacking);
  3632. verifyFormat(
  3633. "template <class SomeType, class SomeOtherType>\n"
  3634. "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
  3635. NoBinPacking);
  3636. }
  3637. TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
  3638. FormatStyle Style = getLLVMStyleWithColumns(15);
  3639. Style.ExperimentalAutoDetectBinPacking = true;
  3640. EXPECT_EQ("aaa(aaaa,\n"
  3641. " aaaa,\n"
  3642. " aaaa);\n"
  3643. "aaa(aaaa,\n"
  3644. " aaaa,\n"
  3645. " aaaa);",
  3646. format("aaa(aaaa,\n" // one-per-line
  3647. " aaaa,\n"
  3648. " aaaa );\n"
  3649. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  3650. Style));
  3651. EXPECT_EQ("aaa(aaaa, aaaa,\n"
  3652. " aaaa);\n"
  3653. "aaa(aaaa, aaaa,\n"
  3654. " aaaa);",
  3655. format("aaa(aaaa, aaaa,\n" // bin-packed
  3656. " aaaa );\n"
  3657. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  3658. Style));
  3659. }
  3660. TEST_F(FormatTest, FormatsBuilderPattern) {
  3661. verifyFormat(
  3662. "return llvm::StringSwitch<Reference::Kind>(name)\n"
  3663. " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
  3664. " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
  3665. " .StartsWith(\".init\", ORDER_INIT)\n"
  3666. " .StartsWith(\".fini\", ORDER_FINI)\n"
  3667. " .StartsWith(\".hash\", ORDER_HASH)\n"
  3668. " .Default(ORDER_TEXT);\n");
  3669. verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
  3670. " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
  3671. verifyFormat(
  3672. "aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaa(\n"
  3673. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3674. " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
  3675. verifyFormat(
  3676. "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
  3677. " aaaaaaaaaaaaaa);");
  3678. verifyFormat(
  3679. "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
  3680. " aaaaaa->aaaaaaaaaaaa()\n"
  3681. " ->aaaaaaaaaaaaaaaa(\n"
  3682. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3683. " ->aaaaaaaaaaaaaaaaa();");
  3684. verifyGoogleFormat(
  3685. "void f() {\n"
  3686. " someo->Add((new util::filetools::Handler(dir))\n"
  3687. " ->OnEvent1(NewPermanentCallback(\n"
  3688. " this, &HandlerHolderClass::EventHandlerCBA))\n"
  3689. " ->OnEvent2(NewPermanentCallback(\n"
  3690. " this, &HandlerHolderClass::EventHandlerCBB))\n"
  3691. " ->OnEvent3(NewPermanentCallback(\n"
  3692. " this, &HandlerHolderClass::EventHandlerCBC))\n"
  3693. " ->OnEvent5(NewPermanentCallback(\n"
  3694. " this, &HandlerHolderClass::EventHandlerCBD))\n"
  3695. " ->OnEvent6(NewPermanentCallback(\n"
  3696. " this, &HandlerHolderClass::EventHandlerCBE)));\n"
  3697. "}");
  3698. verifyFormat(
  3699. "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
  3700. verifyFormat("aaaaaaaaaaaaaaa()\n"
  3701. " .aaaaaaaaaaaaaaa()\n"
  3702. " .aaaaaaaaaaaaaaa()\n"
  3703. " .aaaaaaaaaaaaaaa()\n"
  3704. " .aaaaaaaaaaaaaaa();");
  3705. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3706. " .aaaaaaaaaaaaaaa()\n"
  3707. " .aaaaaaaaaaaaaaa()\n"
  3708. " .aaaaaaaaaaaaaaa();");
  3709. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3710. " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3711. " .aaaaaaaaaaaaaaa();");
  3712. verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3713. " ->aaaaaaaaaaaaaae(0)\n"
  3714. " ->aaaaaaaaaaaaaaa();");
  3715. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  3716. " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3717. " .has<bbbbbbbbbbbbbbbbbbbbb>();");
  3718. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  3719. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  3720. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
  3721. // Prefer not to break after empty parentheses.
  3722. verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
  3723. " First->LastNewlineOffset);");
  3724. }
  3725. TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
  3726. verifyFormat(
  3727. "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3728. " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
  3729. verifyFormat(
  3730. "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
  3731. " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
  3732. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  3733. " ccccccccccccccccccccccccc) {\n}");
  3734. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
  3735. " ccccccccccccccccccccccccc) {\n}");
  3736. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  3737. " ccccccccccccccccccccccccc) {\n}");
  3738. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
  3739. " ccccccccccccccccccccccccc) {\n}");
  3740. verifyFormat(
  3741. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
  3742. " ccccccccccccccccccccccccc) {\n}");
  3743. verifyFormat(
  3744. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
  3745. " ccccccccccccccccccccccccc) {\n}");
  3746. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
  3747. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
  3748. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
  3749. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  3750. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
  3751. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
  3752. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
  3753. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  3754. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
  3755. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
  3756. " aaaaaaaaaaaaaaa != aa) {\n}");
  3757. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
  3758. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
  3759. " aaaaaaaaaaaaaaa != aa) {\n}");
  3760. }
  3761. TEST_F(FormatTest, BreaksAfterAssignments) {
  3762. verifyFormat(
  3763. "unsigned Cost =\n"
  3764. " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
  3765. " SI->getPointerAddressSpaceee());\n");
  3766. verifyFormat(
  3767. "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
  3768. " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
  3769. verifyFormat(
  3770. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
  3771. " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
  3772. verifyFormat("unsigned OriginalStartColumn =\n"
  3773. " SourceMgr.getSpellingColumnNumber(\n"
  3774. " Current.FormatTok.getStartOfNonWhitespace()) -\n"
  3775. " 1;");
  3776. }
  3777. TEST_F(FormatTest, AlignsAfterAssignments) {
  3778. verifyFormat(
  3779. "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3780. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3781. verifyFormat(
  3782. "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3783. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3784. verifyFormat(
  3785. "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3786. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3787. verifyFormat(
  3788. "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3789. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  3790. verifyFormat(
  3791. "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3792. " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3793. " aaaaaaaaaaaaaaaaaaaaaaaa;");
  3794. }
  3795. TEST_F(FormatTest, AlignsAfterReturn) {
  3796. verifyFormat(
  3797. "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3798. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3799. verifyFormat(
  3800. "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3801. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  3802. verifyFormat(
  3803. "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  3804. " aaaaaaaaaaaaaaaaaaaaaa();");
  3805. verifyFormat(
  3806. "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  3807. " aaaaaaaaaaaaaaaaaaaaaa());");
  3808. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3809. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3810. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3811. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
  3812. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3813. verifyFormat("return\n"
  3814. " // true if code is one of a or b.\n"
  3815. " code == a || code == b;");
  3816. }
  3817. TEST_F(FormatTest, AlignsAfterOpenBracket) {
  3818. verifyFormat(
  3819. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
  3820. " aaaaaaaaa aaaaaaa) {}");
  3821. verifyFormat(
  3822. "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
  3823. " aaaaaaaaaaa aaaaaaaaa);");
  3824. verifyFormat(
  3825. "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
  3826. " aaaaaaaaaaaaaaaaaaaaa));");
  3827. FormatStyle Style = getLLVMStyle();
  3828. Style.AlignAfterOpenBracket = false;
  3829. verifyFormat(
  3830. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3831. " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
  3832. Style);
  3833. verifyFormat(
  3834. "SomeLongVariableName->someVeryLongFunctionName(\n"
  3835. " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
  3836. Style);
  3837. verifyFormat(
  3838. "SomeLongVariableName->someFunction(\n"
  3839. " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
  3840. Style);
  3841. verifyFormat(
  3842. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
  3843. " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3844. Style);
  3845. verifyFormat(
  3846. "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
  3847. " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3848. Style);
  3849. verifyFormat(
  3850. "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
  3851. " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
  3852. Style);
  3853. }
  3854. TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
  3855. FormatStyle Style = getLLVMStyleWithColumns(40);
  3856. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  3857. " bbbbbbbbbbbbbbbbbbbbbb);",
  3858. Style);
  3859. Style.AlignAfterOpenBracket = true;
  3860. Style.AlignOperands = false;
  3861. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  3862. " bbbbbbbbbbbbbbbbbbbbbb);",
  3863. Style);
  3864. Style.AlignAfterOpenBracket = false;
  3865. Style.AlignOperands = true;
  3866. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  3867. " bbbbbbbbbbbbbbbbbbbbbb);",
  3868. Style);
  3869. Style.AlignAfterOpenBracket = false;
  3870. Style.AlignOperands = false;
  3871. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  3872. " bbbbbbbbbbbbbbbbbbbbbb);",
  3873. Style);
  3874. }
  3875. TEST_F(FormatTest, BreaksConditionalExpressions) {
  3876. verifyFormat(
  3877. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3878. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3879. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3880. verifyFormat(
  3881. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3882. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3883. verifyFormat(
  3884. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
  3885. " : aaaaaaaaaaaaa);");
  3886. verifyFormat(
  3887. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3888. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3889. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3890. " aaaaaaaaaaaaa);");
  3891. verifyFormat(
  3892. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3893. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3894. " aaaaaaaaaaaaa);");
  3895. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3896. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3897. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3898. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3899. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3900. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3901. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3902. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3903. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3904. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3905. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3906. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3907. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3908. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3909. " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3910. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3911. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3912. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3913. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3914. " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3915. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  3916. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3917. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3918. " : aaaaaaaaaaaaaaaa;");
  3919. verifyFormat(
  3920. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3921. " ? aaaaaaaaaaaaaaa\n"
  3922. " : aaaaaaaaaaaaaaa;");
  3923. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  3924. " aaaaaaaaa\n"
  3925. " ? b\n"
  3926. " : c);");
  3927. verifyFormat("return aaaa == bbbb\n"
  3928. " // comment\n"
  3929. " ? aaaa\n"
  3930. " : bbbb;");
  3931. verifyFormat(
  3932. "unsigned Indent =\n"
  3933. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
  3934. " ? IndentForLevel[TheLine.Level]\n"
  3935. " : TheLine * 2,\n"
  3936. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  3937. getLLVMStyleWithColumns(70));
  3938. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  3939. " ? aaaaaaaaaaaaaaa\n"
  3940. " : bbbbbbbbbbbbbbb //\n"
  3941. " ? ccccccccccccccc\n"
  3942. " : ddddddddddddddd;");
  3943. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  3944. " ? aaaaaaaaaaaaaaa\n"
  3945. " : (bbbbbbbbbbbbbbb //\n"
  3946. " ? ccccccccccccccc\n"
  3947. " : ddddddddddddddd);");
  3948. verifyFormat(
  3949. "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3950. " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3951. " aaaaaaaaaaaaaaaaaaaaa +\n"
  3952. " aaaaaaaaaaaaaaaaaaaaa\n"
  3953. " : aaaaaaaaaa;");
  3954. verifyFormat(
  3955. "aaaaaa = aaaaaaaaaaaa\n"
  3956. " ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3957. " : aaaaaaaaaaaaaaaaaaaaaa\n"
  3958. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3959. FormatStyle NoBinPacking = getLLVMStyle();
  3960. NoBinPacking.BinPackArguments = false;
  3961. verifyFormat(
  3962. "void f() {\n"
  3963. " g(aaa,\n"
  3964. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  3965. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3966. " ? aaaaaaaaaaaaaaa\n"
  3967. " : aaaaaaaaaaaaaaa);\n"
  3968. "}",
  3969. NoBinPacking);
  3970. verifyFormat(
  3971. "void f() {\n"
  3972. " g(aaa,\n"
  3973. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  3974. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3975. " ?: aaaaaaaaaaaaaaa);\n"
  3976. "}",
  3977. NoBinPacking);
  3978. verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
  3979. " // comment.\n"
  3980. " ccccccccccccccccccccccccccccccccccccccc\n"
  3981. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3982. " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
  3983. // Assignments in conditional expressions. Apparently not uncommon :-(.
  3984. verifyFormat("return a != b\n"
  3985. " // comment\n"
  3986. " ? a = b\n"
  3987. " : a = b;");
  3988. verifyFormat("return a != b\n"
  3989. " // comment\n"
  3990. " ? a = a != b\n"
  3991. " // comment\n"
  3992. " ? a = b\n"
  3993. " : a\n"
  3994. " : a;\n");
  3995. verifyFormat("return a != b\n"
  3996. " // comment\n"
  3997. " ? a\n"
  3998. " : a = a != b\n"
  3999. " // comment\n"
  4000. " ? a = b\n"
  4001. " : a;");
  4002. }
  4003. TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
  4004. FormatStyle Style = getLLVMStyle();
  4005. Style.BreakBeforeTernaryOperators = false;
  4006. Style.ColumnLimit = 70;
  4007. verifyFormat(
  4008. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4009. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4010. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4011. Style);
  4012. verifyFormat(
  4013. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4014. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4015. Style);
  4016. verifyFormat(
  4017. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
  4018. " aaaaaaaaaaaaa);",
  4019. Style);
  4020. verifyFormat(
  4021. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4022. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4023. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4024. " aaaaaaaaaaaaa);",
  4025. Style);
  4026. verifyFormat(
  4027. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4028. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4029. " aaaaaaaaaaaaa);",
  4030. Style);
  4031. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4032. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4033. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  4034. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4035. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4036. Style);
  4037. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4038. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4039. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4040. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  4041. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4042. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4043. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4044. Style);
  4045. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4046. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
  4047. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4048. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4049. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4050. Style);
  4051. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4052. " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4053. " aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4054. Style);
  4055. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  4056. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4057. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4058. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4059. Style);
  4060. verifyFormat(
  4061. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4062. " aaaaaaaaaaaaaaa :\n"
  4063. " aaaaaaaaaaaaaaa;",
  4064. Style);
  4065. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  4066. " aaaaaaaaa ?\n"
  4067. " b :\n"
  4068. " c);",
  4069. Style);
  4070. verifyFormat(
  4071. "unsigned Indent =\n"
  4072. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n"
  4073. " IndentForLevel[TheLine.Level] :\n"
  4074. " TheLine * 2,\n"
  4075. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  4076. Style);
  4077. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  4078. " aaaaaaaaaaaaaaa :\n"
  4079. " bbbbbbbbbbbbbbb ? //\n"
  4080. " ccccccccccccccc :\n"
  4081. " ddddddddddddddd;",
  4082. Style);
  4083. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  4084. " aaaaaaaaaaaaaaa :\n"
  4085. " (bbbbbbbbbbbbbbb ? //\n"
  4086. " ccccccccccccccc :\n"
  4087. " ddddddddddddddd);",
  4088. Style);
  4089. }
  4090. TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
  4091. verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
  4092. " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
  4093. verifyFormat("bool a = true, b = false;");
  4094. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4095. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
  4096. " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
  4097. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
  4098. verifyFormat(
  4099. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  4100. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
  4101. " d = e && f;");
  4102. verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
  4103. " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
  4104. verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  4105. " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
  4106. verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
  4107. " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
  4108. // FIXME: If multiple variables are defined, the "*" needs to move to the new
  4109. // line. Also fix indent for breaking after the type, this looks bad.
  4110. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
  4111. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
  4112. " * b = bbbbbbbbbbbbbbbbbbb;",
  4113. getGoogleStyle());
  4114. // Not ideal, but pointer-with-type does not allow much here.
  4115. verifyGoogleFormat(
  4116. "aaaaaaaaa* a = aaaaaaaaaaaaaaaaaaa, * b = bbbbbbbbbbbbbbbbbbb,\n"
  4117. " * b = bbbbbbbbbbbbbbbbbbb, * d = ddddddddddddddddddd;");
  4118. }
  4119. TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
  4120. verifyFormat("arr[foo ? bar : baz];");
  4121. verifyFormat("f()[foo ? bar : baz];");
  4122. verifyFormat("(a + b)[foo ? bar : baz];");
  4123. verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
  4124. }
  4125. TEST_F(FormatTest, AlignsStringLiterals) {
  4126. verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
  4127. " \"short literal\");");
  4128. verifyFormat(
  4129. "looooooooooooooooooooooooongFunction(\n"
  4130. " \"short literal\"\n"
  4131. " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
  4132. verifyFormat("someFunction(\"Always break between multi-line\"\n"
  4133. " \" string literals\",\n"
  4134. " and, other, parameters);");
  4135. EXPECT_EQ("fun + \"1243\" /* comment */\n"
  4136. " \"5678\";",
  4137. format("fun + \"1243\" /* comment */\n"
  4138. " \"5678\";",
  4139. getLLVMStyleWithColumns(28)));
  4140. EXPECT_EQ(
  4141. "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  4142. " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
  4143. " \"aaaaaaaaaaaaaaaa\";",
  4144. format("aaaaaa ="
  4145. "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  4146. "aaaaaaaaaaaaaaaaaaaaa\" "
  4147. "\"aaaaaaaaaaaaaaaa\";"));
  4148. verifyFormat("a = a + \"a\"\n"
  4149. " \"a\"\n"
  4150. " \"a\";");
  4151. verifyFormat("f(\"a\", \"b\"\n"
  4152. " \"c\");");
  4153. verifyFormat(
  4154. "#define LL_FORMAT \"ll\"\n"
  4155. "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
  4156. " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
  4157. verifyFormat("#define A(X) \\\n"
  4158. " \"aaaaa\" #X \"bbbbbb\" \\\n"
  4159. " \"ccccc\"",
  4160. getLLVMStyleWithColumns(23));
  4161. verifyFormat("#define A \"def\"\n"
  4162. "f(\"abc\" A \"ghi\"\n"
  4163. " \"jkl\");");
  4164. verifyFormat("f(L\"a\"\n"
  4165. " L\"b\")");
  4166. verifyFormat("#define A(X) \\\n"
  4167. " L\"aaaaa\" #X L\"bbbbbb\" \\\n"
  4168. " L\"ccccc\"",
  4169. getLLVMStyleWithColumns(25));
  4170. }
  4171. TEST_F(FormatTest, AlwaysBreakAfterDefinitionReturnType) {
  4172. FormatStyle AfterType = getLLVMStyle();
  4173. AfterType.AlwaysBreakAfterDefinitionReturnType = true;
  4174. verifyFormat("const char *\n"
  4175. "f(void) {\n" // Break here.
  4176. " return \"\";\n"
  4177. "}\n"
  4178. "const char *bar(void);\n", // No break here.
  4179. AfterType);
  4180. verifyFormat("template <class T>\n"
  4181. "T *\n"
  4182. "f(T &c) {\n" // Break here.
  4183. " return NULL;\n"
  4184. "}\n"
  4185. "template <class T> T *f(T &c);\n", // No break here.
  4186. AfterType);
  4187. AfterType.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  4188. verifyFormat("const char *\n"
  4189. "f(void)\n" // Break here.
  4190. "{\n"
  4191. " return \"\";\n"
  4192. "}\n"
  4193. "const char *bar(void);\n", // No break here.
  4194. AfterType);
  4195. verifyFormat("template <class T>\n"
  4196. "T *\n" // Problem here: no line break
  4197. "f(T &c)\n" // Break here.
  4198. "{\n"
  4199. " return NULL;\n"
  4200. "}\n"
  4201. "template <class T> T *f(T &c);\n", // No break here.
  4202. AfterType);
  4203. }
  4204. TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
  4205. FormatStyle NoBreak = getLLVMStyle();
  4206. NoBreak.AlwaysBreakBeforeMultilineStrings = false;
  4207. FormatStyle Break = getLLVMStyle();
  4208. Break.AlwaysBreakBeforeMultilineStrings = true;
  4209. verifyFormat("aaaa = \"bbbb\"\n"
  4210. " \"cccc\";",
  4211. NoBreak);
  4212. verifyFormat("aaaa =\n"
  4213. " \"bbbb\"\n"
  4214. " \"cccc\";",
  4215. Break);
  4216. verifyFormat("aaaa(\"bbbb\"\n"
  4217. " \"cccc\");",
  4218. NoBreak);
  4219. verifyFormat("aaaa(\n"
  4220. " \"bbbb\"\n"
  4221. " \"cccc\");",
  4222. Break);
  4223. verifyFormat("aaaa(qqq, \"bbbb\"\n"
  4224. " \"cccc\");",
  4225. NoBreak);
  4226. verifyFormat("aaaa(qqq,\n"
  4227. " \"bbbb\"\n"
  4228. " \"cccc\");",
  4229. Break);
  4230. verifyFormat("aaaa(qqq,\n"
  4231. " L\"bbbb\"\n"
  4232. " L\"cccc\");",
  4233. Break);
  4234. // As we break before unary operators, breaking right after them is bad.
  4235. verifyFormat("string foo = abc ? \"x\"\n"
  4236. " \"blah blah blah blah blah blah\"\n"
  4237. " : \"y\";",
  4238. Break);
  4239. // Don't break if there is no column gain.
  4240. verifyFormat("f(\"aaaa\"\n"
  4241. " \"bbbb\");",
  4242. Break);
  4243. // Treat literals with escaped newlines like multi-line string literals.
  4244. EXPECT_EQ("x = \"a\\\n"
  4245. "b\\\n"
  4246. "c\";",
  4247. format("x = \"a\\\n"
  4248. "b\\\n"
  4249. "c\";",
  4250. NoBreak));
  4251. EXPECT_EQ("x =\n"
  4252. " \"a\\\n"
  4253. "b\\\n"
  4254. "c\";",
  4255. format("x = \"a\\\n"
  4256. "b\\\n"
  4257. "c\";",
  4258. Break));
  4259. // Exempt ObjC strings for now.
  4260. EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
  4261. " \"bbbb\";",
  4262. format("NSString *const kString = @\"aaaa\"\n"
  4263. "\"bbbb\";",
  4264. Break));
  4265. }
  4266. TEST_F(FormatTest, AlignsPipes) {
  4267. verifyFormat(
  4268. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4269. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4270. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4271. verifyFormat(
  4272. "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
  4273. " << aaaaaaaaaaaaaaaaaaaa;");
  4274. verifyFormat(
  4275. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4276. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4277. verifyFormat(
  4278. "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
  4279. " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
  4280. " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
  4281. verifyFormat(
  4282. "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4283. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4284. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4285. verifyFormat(
  4286. "llvm::errs() << \"a: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4287. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4288. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4289. verifyFormat(
  4290. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4291. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4292. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4293. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4294. verifyFormat(
  4295. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4296. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4297. verifyFormat("return out << \"somepacket = {\\n\"\n"
  4298. " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
  4299. " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
  4300. " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
  4301. " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
  4302. " << \"}\";");
  4303. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  4304. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  4305. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
  4306. verifyFormat(
  4307. "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
  4308. " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
  4309. " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
  4310. " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
  4311. " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
  4312. verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
  4313. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4314. verifyFormat(
  4315. "void f() {\n"
  4316. " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
  4317. " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  4318. "}");
  4319. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
  4320. " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
  4321. // Breaking before the first "<<" is generally not desirable.
  4322. verifyFormat(
  4323. "llvm::errs()\n"
  4324. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4325. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4326. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4327. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4328. getLLVMStyleWithColumns(70));
  4329. verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4330. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4331. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4332. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4333. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4334. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4335. getLLVMStyleWithColumns(70));
  4336. // But sometimes, breaking before the first "<<" is desirable.
  4337. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
  4338. " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
  4339. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
  4340. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4341. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4342. verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
  4343. " << BEF << IsTemplate << Description << E->getType();");
  4344. verifyFormat(
  4345. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4346. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4347. // Incomplete string literal.
  4348. EXPECT_EQ("llvm::errs() << \"\n"
  4349. " << a;",
  4350. format("llvm::errs() << \"\n<<a;"));
  4351. verifyFormat("void f() {\n"
  4352. " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
  4353. " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
  4354. "}");
  4355. }
  4356. TEST_F(FormatTest, UnderstandsEquals) {
  4357. verifyFormat(
  4358. "aaaaaaaaaaaaaaaaa =\n"
  4359. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4360. verifyFormat(
  4361. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4362. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  4363. verifyFormat(
  4364. "if (a) {\n"
  4365. " f();\n"
  4366. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4367. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  4368. "}");
  4369. verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4370. " 100000000 + 10000000) {\n}");
  4371. }
  4372. TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
  4373. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  4374. " .looooooooooooooooooooooooooooooooooooooongFunction();");
  4375. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  4376. " ->looooooooooooooooooooooooooooooooooooooongFunction();");
  4377. verifyFormat(
  4378. "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
  4379. " Parameter2);");
  4380. verifyFormat(
  4381. "ShortObject->shortFunction(\n"
  4382. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
  4383. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
  4384. verifyFormat("loooooooooooooongFunction(\n"
  4385. " LoooooooooooooongObject->looooooooooooooooongFunction());");
  4386. verifyFormat(
  4387. "function(LoooooooooooooooooooooooooooooooooooongObject\n"
  4388. " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
  4389. verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  4390. " .WillRepeatedly(Return(SomeValue));");
  4391. verifyFormat("void f() {\n"
  4392. " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  4393. " .Times(2)\n"
  4394. " .WillRepeatedly(Return(SomeValue));\n"
  4395. "}");
  4396. verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
  4397. " ccccccccccccccccccccccc);");
  4398. verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4399. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).aaaaa(aaaaa),\n"
  4400. " aaaaaaaaaaaaaaaaaaaaa);");
  4401. verifyFormat("void f() {\n"
  4402. " aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4403. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
  4404. "}");
  4405. verifyFormat(
  4406. "aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4407. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4408. " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4409. " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4410. " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  4411. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4412. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4413. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4414. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
  4415. "}");
  4416. // Here, it is not necessary to wrap at "." or "->".
  4417. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
  4418. " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  4419. verifyFormat(
  4420. "aaaaaaaaaaa->aaaaaaaaa(\n"
  4421. " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4422. " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
  4423. verifyFormat(
  4424. "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4425. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
  4426. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
  4427. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  4428. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
  4429. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  4430. // FIXME: Should we break before .a()?
  4431. verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4432. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).a();");
  4433. FormatStyle NoBinPacking = getLLVMStyle();
  4434. NoBinPacking.BinPackParameters = false;
  4435. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  4436. " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  4437. " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
  4438. " aaaaaaaaaaaaaaaaaaa,\n"
  4439. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4440. NoBinPacking);
  4441. // If there is a subsequent call, change to hanging indentation.
  4442. verifyFormat(
  4443. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4444. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
  4445. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4446. verifyFormat(
  4447. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4448. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
  4449. verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4450. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4451. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4452. verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4453. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4454. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  4455. }
  4456. TEST_F(FormatTest, WrapsTemplateDeclarations) {
  4457. verifyFormat("template <typename T>\n"
  4458. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  4459. verifyFormat("template <typename T>\n"
  4460. "// T should be one of {A, B}.\n"
  4461. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  4462. verifyFormat(
  4463. "template <typename T>\n"
  4464. "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
  4465. verifyFormat("template <typename T>\n"
  4466. "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
  4467. " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
  4468. verifyFormat(
  4469. "template <typename T>\n"
  4470. "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
  4471. " int Paaaaaaaaaaaaaaaaaaaaram2);");
  4472. verifyFormat(
  4473. "template <typename T>\n"
  4474. "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
  4475. " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
  4476. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4477. verifyFormat("template <typename T>\n"
  4478. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4479. " int aaaaaaaaaaaaaaaaaaaaaa);");
  4480. verifyFormat(
  4481. "template <typename T1, typename T2 = char, typename T3 = char,\n"
  4482. " typename T4 = char>\n"
  4483. "void f();");
  4484. verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
  4485. " template <typename> class cccccccccccccccccccccc,\n"
  4486. " typename ddddddddddddd>\n"
  4487. "class C {};");
  4488. verifyFormat(
  4489. "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
  4490. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4491. verifyFormat("void f() {\n"
  4492. " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
  4493. " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
  4494. "}");
  4495. verifyFormat("template <typename T> class C {};");
  4496. verifyFormat("template <typename T> void f();");
  4497. verifyFormat("template <typename T> void f() {}");
  4498. verifyFormat(
  4499. "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  4500. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4501. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
  4502. " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  4503. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4504. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
  4505. " bbbbbbbbbbbbbbbbbbbbbbbb);",
  4506. getLLVMStyleWithColumns(72));
  4507. EXPECT_EQ("static_cast<A< //\n"
  4508. " B> *>(\n"
  4509. "\n"
  4510. " );",
  4511. format("static_cast<A<//\n"
  4512. " B>*>(\n"
  4513. "\n"
  4514. " );"));
  4515. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4516. " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
  4517. FormatStyle AlwaysBreak = getLLVMStyle();
  4518. AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
  4519. verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
  4520. verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
  4521. verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
  4522. verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4523. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
  4524. " ccccccccccccccccccccccccccccccccccccccccccccccc);");
  4525. verifyFormat("template <template <typename> class Fooooooo,\n"
  4526. " template <typename> class Baaaaaaar>\n"
  4527. "struct C {};",
  4528. AlwaysBreak);
  4529. verifyFormat("template <typename T> // T can be A, B or C.\n"
  4530. "struct C {};",
  4531. AlwaysBreak);
  4532. }
  4533. TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
  4534. verifyFormat(
  4535. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4536. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4537. verifyFormat(
  4538. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4539. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4540. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  4541. // FIXME: Should we have the extra indent after the second break?
  4542. verifyFormat(
  4543. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4544. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4545. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4546. verifyFormat(
  4547. "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
  4548. " cccccccccccccccccccccccccccccccccccccccccccccc());");
  4549. // Breaking at nested name specifiers is generally not desirable.
  4550. verifyFormat(
  4551. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4552. " aaaaaaaaaaaaaaaaaaaaaaa);");
  4553. verifyFormat(
  4554. "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4555. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4556. " aaaaaaaaaaaaaaaaaaaaa);",
  4557. getLLVMStyleWithColumns(74));
  4558. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4559. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4560. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4561. }
  4562. TEST_F(FormatTest, UnderstandsTemplateParameters) {
  4563. verifyFormat("A<int> a;");
  4564. verifyFormat("A<A<A<int>>> a;");
  4565. verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
  4566. verifyFormat("bool x = a < 1 || 2 > a;");
  4567. verifyFormat("bool x = 5 < f<int>();");
  4568. verifyFormat("bool x = f<int>() > 5;");
  4569. verifyFormat("bool x = 5 < a<int>::x;");
  4570. verifyFormat("bool x = a < 4 ? a > 2 : false;");
  4571. verifyFormat("bool x = f() ? a < 2 : a > 2;");
  4572. verifyGoogleFormat("A<A<int>> a;");
  4573. verifyGoogleFormat("A<A<A<int>>> a;");
  4574. verifyGoogleFormat("A<A<A<A<int>>>> a;");
  4575. verifyGoogleFormat("A<A<int> > a;");
  4576. verifyGoogleFormat("A<A<A<int> > > a;");
  4577. verifyGoogleFormat("A<A<A<A<int> > > > a;");
  4578. verifyGoogleFormat("A<::A<int>> a;");
  4579. verifyGoogleFormat("A<::A> a;");
  4580. verifyGoogleFormat("A< ::A> a;");
  4581. verifyGoogleFormat("A< ::A<int> > a;");
  4582. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
  4583. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
  4584. EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
  4585. EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
  4586. verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
  4587. verifyFormat("test >> a >> b;");
  4588. verifyFormat("test << a >> b;");
  4589. verifyFormat("f<int>();");
  4590. verifyFormat("template <typename T> void f() {}");
  4591. // Not template parameters.
  4592. verifyFormat("return a < b && c > d;");
  4593. verifyFormat("void f() {\n"
  4594. " while (a < b && c > d) {\n"
  4595. " }\n"
  4596. "}");
  4597. verifyFormat("template <typename... Types>\n"
  4598. "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
  4599. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4600. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
  4601. getLLVMStyleWithColumns(60));
  4602. verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
  4603. verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
  4604. }
  4605. TEST_F(FormatTest, UnderstandsBinaryOperators) {
  4606. verifyFormat("COMPARE(a, ==, b);");
  4607. }
  4608. TEST_F(FormatTest, UnderstandsPointersToMembers) {
  4609. verifyFormat("int A::*x;");
  4610. verifyFormat("int (S::*func)(void *);");
  4611. verifyFormat("void f() { int (S::*func)(void *); }");
  4612. verifyFormat("typedef bool *(Class::*Member)() const;");
  4613. verifyFormat("void f() {\n"
  4614. " (a->*f)();\n"
  4615. " a->*x;\n"
  4616. " (a.*f)();\n"
  4617. " ((*a).*f)();\n"
  4618. " a.*x;\n"
  4619. "}");
  4620. verifyFormat("void f() {\n"
  4621. " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  4622. " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
  4623. "}");
  4624. verifyFormat(
  4625. "(aaaaaaaaaa->*bbbbbbb)(\n"
  4626. " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  4627. FormatStyle Style = getLLVMStyle();
  4628. Style.PointerAlignment = FormatStyle::PAS_Left;
  4629. verifyFormat("typedef bool* (Class::*Member)() const;", Style);
  4630. }
  4631. TEST_F(FormatTest, UnderstandsUnaryOperators) {
  4632. verifyFormat("int a = -2;");
  4633. verifyFormat("f(-1, -2, -3);");
  4634. verifyFormat("a[-1] = 5;");
  4635. verifyFormat("int a = 5 + -2;");
  4636. verifyFormat("if (i == -1) {\n}");
  4637. verifyFormat("if (i != -1) {\n}");
  4638. verifyFormat("if (i > -1) {\n}");
  4639. verifyFormat("if (i < -1) {\n}");
  4640. verifyFormat("++(a->f());");
  4641. verifyFormat("--(a->f());");
  4642. verifyFormat("(a->f())++;");
  4643. verifyFormat("a[42]++;");
  4644. verifyFormat("if (!(a->f())) {\n}");
  4645. verifyFormat("a-- > b;");
  4646. verifyFormat("b ? -a : c;");
  4647. verifyFormat("n * sizeof char16;");
  4648. verifyFormat("n * alignof char16;", getGoogleStyle());
  4649. verifyFormat("sizeof(char);");
  4650. verifyFormat("alignof(char);", getGoogleStyle());
  4651. verifyFormat("return -1;");
  4652. verifyFormat("switch (a) {\n"
  4653. "case -1:\n"
  4654. " break;\n"
  4655. "}");
  4656. verifyFormat("#define X -1");
  4657. verifyFormat("#define X -kConstant");
  4658. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
  4659. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
  4660. verifyFormat("int a = /* confusing comment */ -1;");
  4661. // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
  4662. verifyFormat("int a = i /* confusing comment */++;");
  4663. }
  4664. TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
  4665. verifyFormat("if (!aaaaaaaaaa( // break\n"
  4666. " aaaaa)) {\n"
  4667. "}");
  4668. verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
  4669. " aaaaa));");
  4670. verifyFormat("*aaa = aaaaaaa( // break\n"
  4671. " bbbbbb);");
  4672. }
  4673. TEST_F(FormatTest, UnderstandsOverloadedOperators) {
  4674. verifyFormat("bool operator<();");
  4675. verifyFormat("bool operator>();");
  4676. verifyFormat("bool operator=();");
  4677. verifyFormat("bool operator==();");
  4678. verifyFormat("bool operator!=();");
  4679. verifyFormat("int operator+();");
  4680. verifyFormat("int operator++();");
  4681. verifyFormat("bool operator();");
  4682. verifyFormat("bool operator()();");
  4683. verifyFormat("bool operator[]();");
  4684. verifyFormat("operator bool();");
  4685. verifyFormat("operator int();");
  4686. verifyFormat("operator void *();");
  4687. verifyFormat("operator SomeType<int>();");
  4688. verifyFormat("operator SomeType<int, int>();");
  4689. verifyFormat("operator SomeType<SomeType<int>>();");
  4690. verifyFormat("void *operator new(std::size_t size);");
  4691. verifyFormat("void *operator new[](std::size_t size);");
  4692. verifyFormat("void operator delete(void *ptr);");
  4693. verifyFormat("void operator delete[](void *ptr);");
  4694. verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
  4695. "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
  4696. verifyFormat(
  4697. "ostream &operator<<(ostream &OutputStream,\n"
  4698. " SomeReallyLongType WithSomeReallyLongValue);");
  4699. verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
  4700. " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
  4701. " return left.group < right.group;\n"
  4702. "}");
  4703. verifyFormat("SomeType &operator=(const SomeType &S);");
  4704. verifyFormat("f.template operator()<int>();");
  4705. verifyGoogleFormat("operator void*();");
  4706. verifyGoogleFormat("operator SomeType<SomeType<int>>();");
  4707. verifyGoogleFormat("operator ::A();");
  4708. verifyFormat("using A::operator+;");
  4709. verifyFormat("Deleted &operator=(const Deleted &)& = default;");
  4710. verifyFormat("Deleted &operator=(const Deleted &)&& = delete;");
  4711. verifyGoogleFormat("Deleted& operator=(const Deleted&)& = default;");
  4712. verifyGoogleFormat("Deleted& operator=(const Deleted&)&& = delete;");
  4713. verifyFormat("string // break\n"
  4714. "operator()() & {}");
  4715. verifyFormat("string // break\n"
  4716. "operator()() && {}");
  4717. }
  4718. TEST_F(FormatTest, UnderstandsNewAndDelete) {
  4719. verifyFormat("void f() {\n"
  4720. " A *a = new A;\n"
  4721. " A *a = new (placement) A;\n"
  4722. " delete a;\n"
  4723. " delete (A *)a;\n"
  4724. "}");
  4725. verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
  4726. " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
  4727. verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4728. " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
  4729. " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
  4730. verifyFormat("delete[] h->p;");
  4731. }
  4732. TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
  4733. verifyFormat("int *f(int *a) {}");
  4734. verifyFormat("int main(int argc, char **argv) {}");
  4735. verifyFormat("Test::Test(int b) : a(b * b) {}");
  4736. verifyIndependentOfContext("f(a, *a);");
  4737. verifyFormat("void g() { f(*a); }");
  4738. verifyIndependentOfContext("int a = b * 10;");
  4739. verifyIndependentOfContext("int a = 10 * b;");
  4740. verifyIndependentOfContext("int a = b * c;");
  4741. verifyIndependentOfContext("int a += b * c;");
  4742. verifyIndependentOfContext("int a -= b * c;");
  4743. verifyIndependentOfContext("int a *= b * c;");
  4744. verifyIndependentOfContext("int a /= b * c;");
  4745. verifyIndependentOfContext("int a = *b;");
  4746. verifyIndependentOfContext("int a = *b * c;");
  4747. verifyIndependentOfContext("int a = b * *c;");
  4748. verifyIndependentOfContext("return 10 * b;");
  4749. verifyIndependentOfContext("return *b * *c;");
  4750. verifyIndependentOfContext("return a & ~b;");
  4751. verifyIndependentOfContext("f(b ? *c : *d);");
  4752. verifyIndependentOfContext("int a = b ? *c : *d;");
  4753. verifyIndependentOfContext("*b = a;");
  4754. verifyIndependentOfContext("a * ~b;");
  4755. verifyIndependentOfContext("a * !b;");
  4756. verifyIndependentOfContext("a * +b;");
  4757. verifyIndependentOfContext("a * -b;");
  4758. verifyIndependentOfContext("a * ++b;");
  4759. verifyIndependentOfContext("a * --b;");
  4760. verifyIndependentOfContext("a[4] * b;");
  4761. verifyIndependentOfContext("a[a * a] = 1;");
  4762. verifyIndependentOfContext("f() * b;");
  4763. verifyIndependentOfContext("a * [self dostuff];");
  4764. verifyIndependentOfContext("int x = a * (a + b);");
  4765. verifyIndependentOfContext("(a *)(a + b);");
  4766. verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
  4767. verifyIndependentOfContext("int *pa = (int *)&a;");
  4768. verifyIndependentOfContext("return sizeof(int **);");
  4769. verifyIndependentOfContext("return sizeof(int ******);");
  4770. verifyIndependentOfContext("return (int **&)a;");
  4771. verifyIndependentOfContext("f((*PointerToArray)[10]);");
  4772. verifyFormat("void f(Type (*parameter)[10]) {}");
  4773. verifyGoogleFormat("return sizeof(int**);");
  4774. verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
  4775. verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
  4776. verifyFormat("auto a = [](int **&, int ***) {};");
  4777. verifyFormat("auto PointerBinding = [](const char *S) {};");
  4778. verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
  4779. verifyFormat("[](const decltype(*a) &value) {}");
  4780. verifyIndependentOfContext("typedef void (*f)(int *a);");
  4781. verifyIndependentOfContext("int i{a * b};");
  4782. verifyIndependentOfContext("aaa && aaa->f();");
  4783. verifyIndependentOfContext("int x = ~*p;");
  4784. verifyFormat("Constructor() : a(a), area(width * height) {}");
  4785. verifyFormat("Constructor() : a(a), area(a, width * height) {}");
  4786. verifyFormat("void f() { f(a, c * d); }");
  4787. verifyIndependentOfContext("InvalidRegions[*R] = 0;");
  4788. verifyIndependentOfContext("A<int *> a;");
  4789. verifyIndependentOfContext("A<int **> a;");
  4790. verifyIndependentOfContext("A<int *, int *> a;");
  4791. verifyIndependentOfContext("A<int *[]> a;");
  4792. verifyIndependentOfContext(
  4793. "const char *const p = reinterpret_cast<const char *const>(q);");
  4794. verifyIndependentOfContext("A<int **, int **> a;");
  4795. verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
  4796. verifyFormat("for (char **a = b; *a; ++a) {\n}");
  4797. verifyFormat("for (; a && b;) {\n}");
  4798. verifyFormat("bool foo = true && [] { return false; }();");
  4799. verifyFormat(
  4800. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4801. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4802. verifyGoogleFormat("**outparam = 1;");
  4803. verifyGoogleFormat("int main(int argc, char** argv) {}");
  4804. verifyGoogleFormat("A<int*> a;");
  4805. verifyGoogleFormat("A<int**> a;");
  4806. verifyGoogleFormat("A<int*, int*> a;");
  4807. verifyGoogleFormat("A<int**, int**> a;");
  4808. verifyGoogleFormat("f(b ? *c : *d);");
  4809. verifyGoogleFormat("int a = b ? *c : *d;");
  4810. verifyGoogleFormat("Type* t = **x;");
  4811. verifyGoogleFormat("Type* t = *++*x;");
  4812. verifyGoogleFormat("*++*x;");
  4813. verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
  4814. verifyGoogleFormat("Type* t = x++ * y;");
  4815. verifyGoogleFormat(
  4816. "const char* const p = reinterpret_cast<const char* const>(q);");
  4817. verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
  4818. verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
  4819. verifyGoogleFormat("template <typename T>\n"
  4820. "void f(int i = 0, SomeType** temps = NULL);");
  4821. FormatStyle Left = getLLVMStyle();
  4822. Left.PointerAlignment = FormatStyle::PAS_Left;
  4823. verifyFormat("x = *a(x) = *a(y);", Left);
  4824. verifyIndependentOfContext("a = *(x + y);");
  4825. verifyIndependentOfContext("a = &(x + y);");
  4826. verifyIndependentOfContext("*(x + y).call();");
  4827. verifyIndependentOfContext("&(x + y)->call();");
  4828. verifyFormat("void f() { &(*I).first; }");
  4829. verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
  4830. verifyFormat(
  4831. "int *MyValues = {\n"
  4832. " *A, // Operator detection might be confused by the '{'\n"
  4833. " *BB // Operator detection might be confused by previous comment\n"
  4834. "};");
  4835. verifyIndependentOfContext("if (int *a = &b)");
  4836. verifyIndependentOfContext("if (int &a = *b)");
  4837. verifyIndependentOfContext("if (a & b[i])");
  4838. verifyIndependentOfContext("if (a::b::c::d & b[i])");
  4839. verifyIndependentOfContext("if (*b[i])");
  4840. verifyIndependentOfContext("if (int *a = (&b))");
  4841. verifyIndependentOfContext("while (int *a = &b)");
  4842. verifyIndependentOfContext("size = sizeof *a;");
  4843. verifyFormat("void f() {\n"
  4844. " for (const int &v : Values) {\n"
  4845. " }\n"
  4846. "}");
  4847. verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
  4848. verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
  4849. verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
  4850. verifyFormat("#define A (!a * b)");
  4851. verifyFormat("#define MACRO \\\n"
  4852. " int *i = a * b; \\\n"
  4853. " void f(a *b);",
  4854. getLLVMStyleWithColumns(19));
  4855. verifyIndependentOfContext("A = new SomeType *[Length];");
  4856. verifyIndependentOfContext("A = new SomeType *[Length]();");
  4857. verifyIndependentOfContext("T **t = new T *;");
  4858. verifyIndependentOfContext("T **t = new T *();");
  4859. verifyGoogleFormat("A = new SomeType* [Length]();");
  4860. verifyGoogleFormat("A = new SomeType* [Length];");
  4861. verifyGoogleFormat("T** t = new T*;");
  4862. verifyGoogleFormat("T** t = new T*();");
  4863. FormatStyle PointerLeft = getLLVMStyle();
  4864. PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
  4865. verifyFormat("delete *x;", PointerLeft);
  4866. verifyFormat("STATIC_ASSERT((a & b) == 0);");
  4867. verifyFormat("STATIC_ASSERT(0 == (a & b));");
  4868. verifyFormat("template <bool a, bool b> "
  4869. "typename t::if<x && y>::type f() {}");
  4870. verifyFormat("template <int *y> f() {}");
  4871. verifyFormat("vector<int *> v;");
  4872. verifyFormat("vector<int *const> v;");
  4873. verifyFormat("vector<int *const **const *> v;");
  4874. verifyFormat("vector<int *volatile> v;");
  4875. verifyFormat("vector<a * b> v;");
  4876. verifyFormat("foo<b && false>();");
  4877. verifyFormat("foo<b & 1>();");
  4878. verifyFormat("decltype(*::std::declval<const T &>()) void F();");
  4879. verifyFormat(
  4880. "template <class T, class = typename std::enable_if<\n"
  4881. " std::is_integral<T>::value &&\n"
  4882. " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
  4883. "void F();",
  4884. getLLVMStyleWithColumns(76));
  4885. verifyFormat(
  4886. "template <class T,\n"
  4887. " class = typename ::std::enable_if<\n"
  4888. " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
  4889. "void F();",
  4890. getGoogleStyleWithColumns(68));
  4891. verifyIndependentOfContext("MACRO(int *i);");
  4892. verifyIndependentOfContext("MACRO(auto *a);");
  4893. verifyIndependentOfContext("MACRO(const A *a);");
  4894. verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
  4895. // FIXME: Is there a way to make this work?
  4896. // verifyIndependentOfContext("MACRO(A *a);");
  4897. verifyFormat("DatumHandle const *operator->() const { return input_; }");
  4898. EXPECT_EQ("#define OP(x) \\\n"
  4899. " ostream &operator<<(ostream &s, const A &a) { \\\n"
  4900. " return s << a.DebugString(); \\\n"
  4901. " }",
  4902. format("#define OP(x) \\\n"
  4903. " ostream &operator<<(ostream &s, const A &a) { \\\n"
  4904. " return s << a.DebugString(); \\\n"
  4905. " }",
  4906. getLLVMStyleWithColumns(50)));
  4907. // FIXME: We cannot handle this case yet; we might be able to figure out that
  4908. // foo<x> d > v; doesn't make sense.
  4909. verifyFormat("foo<a<b && c> d> v;");
  4910. FormatStyle PointerMiddle = getLLVMStyle();
  4911. PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
  4912. verifyFormat("delete *x;", PointerMiddle);
  4913. verifyFormat("int * x;", PointerMiddle);
  4914. verifyFormat("template <int * y> f() {}", PointerMiddle);
  4915. verifyFormat("int * f(int * a) {}", PointerMiddle);
  4916. verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
  4917. verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
  4918. verifyFormat("A<int *> a;", PointerMiddle);
  4919. verifyFormat("A<int **> a;", PointerMiddle);
  4920. verifyFormat("A<int *, int *> a;", PointerMiddle);
  4921. verifyFormat("A<int * []> a;", PointerMiddle);
  4922. verifyFormat("A = new SomeType * [Length]();", PointerMiddle);
  4923. verifyFormat("A = new SomeType * [Length];", PointerMiddle);
  4924. verifyFormat("T ** t = new T *;", PointerMiddle);
  4925. }
  4926. TEST_F(FormatTest, UnderstandsAttributes) {
  4927. verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
  4928. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
  4929. "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
  4930. }
  4931. TEST_F(FormatTest, UnderstandsEllipsis) {
  4932. verifyFormat("int printf(const char *fmt, ...);");
  4933. verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
  4934. verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
  4935. FormatStyle PointersLeft = getLLVMStyle();
  4936. PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
  4937. verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
  4938. }
  4939. TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
  4940. EXPECT_EQ("int *a;\n"
  4941. "int *a;\n"
  4942. "int *a;",
  4943. format("int *a;\n"
  4944. "int* a;\n"
  4945. "int *a;",
  4946. getGoogleStyle()));
  4947. EXPECT_EQ("int* a;\n"
  4948. "int* a;\n"
  4949. "int* a;",
  4950. format("int* a;\n"
  4951. "int* a;\n"
  4952. "int *a;",
  4953. getGoogleStyle()));
  4954. EXPECT_EQ("int *a;\n"
  4955. "int *a;\n"
  4956. "int *a;",
  4957. format("int *a;\n"
  4958. "int * a;\n"
  4959. "int * a;",
  4960. getGoogleStyle()));
  4961. }
  4962. TEST_F(FormatTest, UnderstandsRvalueReferences) {
  4963. verifyFormat("int f(int &&a) {}");
  4964. verifyFormat("int f(int a, char &&b) {}");
  4965. verifyFormat("void f() { int &&a = b; }");
  4966. verifyGoogleFormat("int f(int a, char&& b) {}");
  4967. verifyGoogleFormat("void f() { int&& a = b; }");
  4968. verifyIndependentOfContext("A<int &&> a;");
  4969. verifyIndependentOfContext("A<int &&, int &&> a;");
  4970. verifyGoogleFormat("A<int&&> a;");
  4971. verifyGoogleFormat("A<int&&, int&&> a;");
  4972. // Not rvalue references:
  4973. verifyFormat("template <bool B, bool C> class A {\n"
  4974. " static_assert(B && C, \"Something is wrong\");\n"
  4975. "};");
  4976. verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
  4977. verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
  4978. verifyFormat("#define A(a, b) (a && b)");
  4979. }
  4980. TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
  4981. verifyFormat("void f() {\n"
  4982. " x[aaaaaaaaa -\n"
  4983. " b] = 23;\n"
  4984. "}",
  4985. getLLVMStyleWithColumns(15));
  4986. }
  4987. TEST_F(FormatTest, FormatsCasts) {
  4988. verifyFormat("Type *A = static_cast<Type *>(P);");
  4989. verifyFormat("Type *A = (Type *)P;");
  4990. verifyFormat("Type *A = (vector<Type *, int *>)P;");
  4991. verifyFormat("int a = (int)(2.0f);");
  4992. verifyFormat("int a = (int)2.0f;");
  4993. verifyFormat("x[(int32)y];");
  4994. verifyFormat("x = (int32)y;");
  4995. verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
  4996. verifyFormat("int a = (int)*b;");
  4997. verifyFormat("int a = (int)2.0f;");
  4998. verifyFormat("int a = (int)~0;");
  4999. verifyFormat("int a = (int)++a;");
  5000. verifyFormat("int a = (int)sizeof(int);");
  5001. verifyFormat("int a = (int)+2;");
  5002. verifyFormat("my_int a = (my_int)2.0f;");
  5003. verifyFormat("my_int a = (my_int)sizeof(int);");
  5004. verifyFormat("return (my_int)aaa;");
  5005. verifyFormat("#define x ((int)-1)");
  5006. verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
  5007. verifyFormat("#define p(q) ((int *)&q)");
  5008. verifyFormat("fn(a)(b) + 1;");
  5009. verifyFormat("void f() { my_int a = (my_int)*b; }");
  5010. verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
  5011. verifyFormat("my_int a = (my_int)~0;");
  5012. verifyFormat("my_int a = (my_int)++a;");
  5013. verifyFormat("my_int a = (my_int)-2;");
  5014. verifyFormat("my_int a = (my_int)1;");
  5015. verifyFormat("my_int a = (my_int *)1;");
  5016. verifyFormat("my_int a = (const my_int)-1;");
  5017. verifyFormat("my_int a = (const my_int *)-1;");
  5018. verifyFormat("my_int a = (my_int)(my_int)-1;");
  5019. // FIXME: single value wrapped with paren will be treated as cast.
  5020. verifyFormat("void f(int i = (kValue)*kMask) {}");
  5021. verifyFormat("{ (void)F; }");
  5022. // Don't break after a cast's
  5023. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  5024. " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
  5025. " bbbbbbbbbbbbbbbbbbbbbb);");
  5026. // These are not casts.
  5027. verifyFormat("void f(int *) {}");
  5028. verifyFormat("f(foo)->b;");
  5029. verifyFormat("f(foo).b;");
  5030. verifyFormat("f(foo)(b);");
  5031. verifyFormat("f(foo)[b];");
  5032. verifyFormat("[](foo) { return 4; }(bar);");
  5033. verifyFormat("(*funptr)(foo)[4];");
  5034. verifyFormat("funptrs[4](foo)[4];");
  5035. verifyFormat("void f(int *);");
  5036. verifyFormat("void f(int *) = 0;");
  5037. verifyFormat("void f(SmallVector<int>) {}");
  5038. verifyFormat("void f(SmallVector<int>);");
  5039. verifyFormat("void f(SmallVector<int>) = 0;");
  5040. verifyFormat("void f(int i = (kA * kB) & kMask) {}");
  5041. verifyFormat("int a = sizeof(int) * b;");
  5042. verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
  5043. verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
  5044. verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
  5045. verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
  5046. // These are not casts, but at some point were confused with casts.
  5047. verifyFormat("virtual void foo(int *) override;");
  5048. verifyFormat("virtual void foo(char &) const;");
  5049. verifyFormat("virtual void foo(int *a, char *) const;");
  5050. verifyFormat("int a = sizeof(int *) + b;");
  5051. verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
  5052. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
  5053. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  5054. // FIXME: The indentation here is not ideal.
  5055. verifyFormat(
  5056. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5057. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
  5058. " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
  5059. }
  5060. TEST_F(FormatTest, FormatsFunctionTypes) {
  5061. verifyFormat("A<bool()> a;");
  5062. verifyFormat("A<SomeType()> a;");
  5063. verifyFormat("A<void (*)(int, std::string)> a;");
  5064. verifyFormat("A<void *(int)>;");
  5065. verifyFormat("void *(*a)(int *, SomeType *);");
  5066. verifyFormat("int (*func)(void *);");
  5067. verifyFormat("void f() { int (*func)(void *); }");
  5068. verifyFormat("template <class CallbackClass>\n"
  5069. "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
  5070. verifyGoogleFormat("A<void*(int*, SomeType*)>;");
  5071. verifyGoogleFormat("void* (*a)(int);");
  5072. verifyGoogleFormat(
  5073. "template <class CallbackClass>\n"
  5074. "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
  5075. // Other constructs can look somewhat like function types:
  5076. verifyFormat("A<sizeof(*x)> a;");
  5077. verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
  5078. verifyFormat("some_var = function(*some_pointer_var)[0];");
  5079. verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
  5080. }
  5081. TEST_F(FormatTest, BreaksLongVariableDeclarations) {
  5082. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5083. " LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5084. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
  5085. " LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5086. // Different ways of ()-initializiation.
  5087. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5088. " LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
  5089. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5090. " LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
  5091. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5092. " LoooooooooooooooooooooooooooooooooooooooongVariable({});");
  5093. }
  5094. TEST_F(FormatTest, BreaksLongDeclarations) {
  5095. verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
  5096. " AnotherNameForTheLongType;");
  5097. verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
  5098. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  5099. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5100. "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
  5101. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5102. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5103. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
  5104. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5105. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  5106. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5107. verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
  5108. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5109. FormatStyle Indented = getLLVMStyle();
  5110. Indented.IndentWrappedFunctionNames = true;
  5111. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5112. " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
  5113. Indented);
  5114. verifyFormat(
  5115. "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5116. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5117. Indented);
  5118. verifyFormat(
  5119. "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  5120. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5121. Indented);
  5122. verifyFormat(
  5123. "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
  5124. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5125. Indented);
  5126. // FIXME: Without the comment, this breaks after "(".
  5127. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
  5128. " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
  5129. getGoogleStyle());
  5130. verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
  5131. " int LoooooooooooooooooooongParam2) {}");
  5132. verifyFormat(
  5133. "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
  5134. " SourceLocation L, IdentifierIn *II,\n"
  5135. " Type *T) {}");
  5136. verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
  5137. "ReallyReallyLongFunctionName(\n"
  5138. " const std::string &SomeParameter,\n"
  5139. " const SomeType<string, SomeOtherTemplateParameter> &\n"
  5140. " ReallyReallyLongParameterName,\n"
  5141. " const SomeType<string, SomeOtherTemplateParameter> &\n"
  5142. " AnotherLongParameterName) {}");
  5143. verifyFormat("template <typename A>\n"
  5144. "SomeLoooooooooooooooooooooongType<\n"
  5145. " typename some_namespace::SomeOtherType<A>::Type>\n"
  5146. "Function() {}");
  5147. verifyGoogleFormat(
  5148. "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
  5149. " aaaaaaaaaaaaaaaaaaaaaaa;");
  5150. verifyGoogleFormat(
  5151. "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
  5152. " SourceLocation L) {}");
  5153. verifyGoogleFormat(
  5154. "some_namespace::LongReturnType\n"
  5155. "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
  5156. " int first_long_parameter, int second_parameter) {}");
  5157. verifyGoogleFormat("template <typename T>\n"
  5158. "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  5159. "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
  5160. verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5161. " int aaaaaaaaaaaaaaaaaaaaaaa);");
  5162. verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  5163. " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  5164. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5165. }
  5166. TEST_F(FormatTest, FormatsArrays) {
  5167. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5168. " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
  5169. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5170. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  5171. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5172. " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
  5173. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5174. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5175. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  5176. verifyFormat(
  5177. "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
  5178. " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5179. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  5180. verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
  5181. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
  5182. verifyFormat(
  5183. "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
  5184. " .aaaaaaa[0]\n"
  5185. " .aaaaaaaaaaaaaaaaaaaaaa();");
  5186. }
  5187. TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
  5188. verifyFormat("(a)->b();");
  5189. verifyFormat("--a;");
  5190. }
  5191. TEST_F(FormatTest, HandlesIncludeDirectives) {
  5192. verifyFormat("#include <string>\n"
  5193. "#include <a/b/c.h>\n"
  5194. "#include \"a/b/string\"\n"
  5195. "#include \"string.h\"\n"
  5196. "#include \"string.h\"\n"
  5197. "#include <a-a>\n"
  5198. "#include < path with space >\n"
  5199. "#include \"abc.h\" // this is included for ABC\n"
  5200. "#include \"some long include\" // with a comment\n"
  5201. "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
  5202. getLLVMStyleWithColumns(35));
  5203. EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\""));
  5204. EXPECT_EQ("#include <a>", format("#include<a>"));
  5205. verifyFormat("#import <string>");
  5206. verifyFormat("#import <a/b/c.h>");
  5207. verifyFormat("#import \"a/b/string\"");
  5208. verifyFormat("#import \"string.h\"");
  5209. verifyFormat("#import \"string.h\"");
  5210. verifyFormat("#if __has_include(<strstream>)\n"
  5211. "#include <strstream>\n"
  5212. "#endif");
  5213. verifyFormat("#define MY_IMPORT <a/b>");
  5214. // Protocol buffer definition or missing "#".
  5215. verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
  5216. getLLVMStyleWithColumns(30));
  5217. FormatStyle Style = getLLVMStyle();
  5218. Style.AlwaysBreakBeforeMultilineStrings = true;
  5219. Style.ColumnLimit = 0;
  5220. verifyFormat("#import \"abc.h\"", Style);
  5221. // But 'import' might also be a regular C++ namespace.
  5222. verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5223. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5224. }
  5225. //===----------------------------------------------------------------------===//
  5226. // Error recovery tests.
  5227. //===----------------------------------------------------------------------===//
  5228. TEST_F(FormatTest, IncompleteParameterLists) {
  5229. FormatStyle NoBinPacking = getLLVMStyle();
  5230. NoBinPacking.BinPackParameters = false;
  5231. verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
  5232. " double *min_x,\n"
  5233. " double *max_x,\n"
  5234. " double *min_y,\n"
  5235. " double *max_y,\n"
  5236. " double *min_z,\n"
  5237. " double *max_z, ) {}",
  5238. NoBinPacking);
  5239. }
  5240. TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
  5241. verifyFormat("void f() { return; }\n42");
  5242. verifyFormat("void f() {\n"
  5243. " if (0)\n"
  5244. " return;\n"
  5245. "}\n"
  5246. "42");
  5247. verifyFormat("void f() { return }\n42");
  5248. verifyFormat("void f() {\n"
  5249. " if (0)\n"
  5250. " return\n"
  5251. "}\n"
  5252. "42");
  5253. }
  5254. TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
  5255. EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
  5256. EXPECT_EQ("void f() {\n"
  5257. " if (a)\n"
  5258. " return\n"
  5259. "}",
  5260. format("void f ( ) { if ( a ) return }"));
  5261. EXPECT_EQ("namespace N {\n"
  5262. "void f()\n"
  5263. "}",
  5264. format("namespace N { void f() }"));
  5265. EXPECT_EQ("namespace N {\n"
  5266. "void f() {}\n"
  5267. "void g()\n"
  5268. "}",
  5269. format("namespace N { void f( ) { } void g( ) }"));
  5270. }
  5271. TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
  5272. verifyFormat("int aaaaaaaa =\n"
  5273. " // Overlylongcomment\n"
  5274. " b;",
  5275. getLLVMStyleWithColumns(20));
  5276. verifyFormat("function(\n"
  5277. " ShortArgument,\n"
  5278. " LoooooooooooongArgument);\n",
  5279. getLLVMStyleWithColumns(20));
  5280. }
  5281. TEST_F(FormatTest, IncorrectAccessSpecifier) {
  5282. verifyFormat("public:");
  5283. verifyFormat("class A {\n"
  5284. "public\n"
  5285. " void f() {}\n"
  5286. "};");
  5287. verifyFormat("public\n"
  5288. "int qwerty;");
  5289. verifyFormat("public\n"
  5290. "B {}");
  5291. verifyFormat("public\n"
  5292. "{}");
  5293. verifyFormat("public\n"
  5294. "B { int x; }");
  5295. }
  5296. TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
  5297. verifyFormat("{");
  5298. verifyFormat("#})");
  5299. }
  5300. TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
  5301. verifyFormat("do {\n}");
  5302. verifyFormat("do {\n}\n"
  5303. "f();");
  5304. verifyFormat("do {\n}\n"
  5305. "wheeee(fun);");
  5306. verifyFormat("do {\n"
  5307. " f();\n"
  5308. "}");
  5309. }
  5310. TEST_F(FormatTest, IncorrectCodeMissingParens) {
  5311. verifyFormat("if {\n foo;\n foo();\n}");
  5312. verifyFormat("switch {\n foo;\n foo();\n}");
  5313. verifyFormat("for {\n foo;\n foo();\n}");
  5314. verifyFormat("while {\n foo;\n foo();\n}");
  5315. verifyFormat("do {\n foo;\n foo();\n} while;");
  5316. }
  5317. TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
  5318. verifyFormat("namespace {\n"
  5319. "class Foo { Foo (\n"
  5320. "};\n"
  5321. "} // comment");
  5322. }
  5323. TEST_F(FormatTest, IncorrectCodeErrorDetection) {
  5324. EXPECT_EQ("{\n {}\n", format("{\n{\n}\n"));
  5325. EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
  5326. EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
  5327. EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n"));
  5328. EXPECT_EQ("{\n"
  5329. " {\n"
  5330. " breakme(\n"
  5331. " qwe);\n"
  5332. " }\n",
  5333. format("{\n"
  5334. " {\n"
  5335. " breakme(qwe);\n"
  5336. "}\n",
  5337. getLLVMStyleWithColumns(10)));
  5338. }
  5339. TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
  5340. verifyFormat("int x = {\n"
  5341. " avariable,\n"
  5342. " b(alongervariable)};",
  5343. getLLVMStyleWithColumns(25));
  5344. }
  5345. TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
  5346. verifyFormat("return (a)(b){1, 2, 3};");
  5347. }
  5348. TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
  5349. verifyFormat("vector<int> x{1, 2, 3, 4};");
  5350. verifyFormat("vector<int> x{\n"
  5351. " 1, 2, 3, 4,\n"
  5352. "};");
  5353. verifyFormat("vector<T> x{{}, {}, {}, {}};");
  5354. verifyFormat("f({1, 2});");
  5355. verifyFormat("auto v = Foo{-1};");
  5356. verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
  5357. verifyFormat("Class::Class : member{1, 2, 3} {}");
  5358. verifyFormat("new vector<int>{1, 2, 3};");
  5359. verifyFormat("new int[3]{1, 2, 3};");
  5360. verifyFormat("new int{1};");
  5361. verifyFormat("return {arg1, arg2};");
  5362. verifyFormat("return {arg1, SomeType{parameter}};");
  5363. verifyFormat("int count = set<int>{f(), g(), h()}.size();");
  5364. verifyFormat("new T{arg1, arg2};");
  5365. verifyFormat("f(MyMap[{composite, key}]);");
  5366. verifyFormat("class Class {\n"
  5367. " T member = {arg1, arg2};\n"
  5368. "};");
  5369. verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
  5370. verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
  5371. verifyFormat("int a = std::is_integral<int>{} + 0;");
  5372. verifyFormat("int foo(int i) { return fo1{}(i); }");
  5373. verifyFormat("int foo(int i) { return fo1{}(i); }");
  5374. verifyFormat("auto i = decltype(x){};");
  5375. verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
  5376. verifyFormat("Node n{1, Node{1000}, //\n"
  5377. " 2};");
  5378. // In combination with BinPackParameters = false.
  5379. FormatStyle NoBinPacking = getLLVMStyle();
  5380. NoBinPacking.BinPackParameters = false;
  5381. verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
  5382. " bbbbb,\n"
  5383. " ccccc,\n"
  5384. " ddddd,\n"
  5385. " eeeee,\n"
  5386. " ffffff,\n"
  5387. " ggggg,\n"
  5388. " hhhhhh,\n"
  5389. " iiiiii,\n"
  5390. " jjjjjj,\n"
  5391. " kkkkkk};",
  5392. NoBinPacking);
  5393. verifyFormat("const Aaaaaa aaaaa = {\n"
  5394. " aaaaa,\n"
  5395. " bbbbb,\n"
  5396. " ccccc,\n"
  5397. " ddddd,\n"
  5398. " eeeee,\n"
  5399. " ffffff,\n"
  5400. " ggggg,\n"
  5401. " hhhhhh,\n"
  5402. " iiiiii,\n"
  5403. " jjjjjj,\n"
  5404. " kkkkkk,\n"
  5405. "};",
  5406. NoBinPacking);
  5407. verifyFormat(
  5408. "const Aaaaaa aaaaa = {\n"
  5409. " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n"
  5410. " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n"
  5411. " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
  5412. "};",
  5413. NoBinPacking);
  5414. // FIXME: The alignment of these trailing comments might be bad. Then again,
  5415. // this might be utterly useless in real code.
  5416. verifyFormat("Constructor::Constructor()\n"
  5417. " : some_value{ //\n"
  5418. " aaaaaaa, //\n"
  5419. " bbbbbbb} {}");
  5420. // In braced lists, the first comment is always assumed to belong to the
  5421. // first element. Thus, it can be moved to the next or previous line as
  5422. // appropriate.
  5423. EXPECT_EQ("function({// First element:\n"
  5424. " 1,\n"
  5425. " // Second element:\n"
  5426. " 2});",
  5427. format("function({\n"
  5428. " // First element:\n"
  5429. " 1,\n"
  5430. " // Second element:\n"
  5431. " 2});"));
  5432. EXPECT_EQ("std::vector<int> MyNumbers{\n"
  5433. " // First element:\n"
  5434. " 1,\n"
  5435. " // Second element:\n"
  5436. " 2};",
  5437. format("std::vector<int> MyNumbers{// First element:\n"
  5438. " 1,\n"
  5439. " // Second element:\n"
  5440. " 2};",
  5441. getLLVMStyleWithColumns(30)));
  5442. // A trailing comma should still lead to an enforced line break.
  5443. EXPECT_EQ("vector<int> SomeVector = {\n"
  5444. " // aaa\n"
  5445. " 1, 2,\n"
  5446. "};",
  5447. format("vector<int> SomeVector = { // aaa\n"
  5448. " 1, 2, };"));
  5449. FormatStyle ExtraSpaces = getLLVMStyle();
  5450. ExtraSpaces.Cpp11BracedListStyle = false;
  5451. ExtraSpaces.ColumnLimit = 75;
  5452. verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
  5453. verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
  5454. verifyFormat("f({ 1, 2 });", ExtraSpaces);
  5455. verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
  5456. verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
  5457. verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
  5458. verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
  5459. verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
  5460. verifyFormat("return { arg1, arg2 };", ExtraSpaces);
  5461. verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
  5462. verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
  5463. verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
  5464. verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
  5465. verifyFormat("class Class {\n"
  5466. " T member = { arg1, arg2 };\n"
  5467. "};",
  5468. ExtraSpaces);
  5469. verifyFormat(
  5470. "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5471. " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
  5472. " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  5473. " bbbbbbbbbbbbbbbbbbbb, bbbbb };",
  5474. ExtraSpaces);
  5475. verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
  5476. verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
  5477. ExtraSpaces);
  5478. verifyFormat(
  5479. "someFunction(OtherParam,\n"
  5480. " BracedList{ // comment 1 (Forcing interesting break)\n"
  5481. " param1, param2,\n"
  5482. " // comment 2\n"
  5483. " param3, param4 });",
  5484. ExtraSpaces);
  5485. verifyFormat(
  5486. "std::this_thread::sleep_for(\n"
  5487. " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
  5488. ExtraSpaces);
  5489. verifyFormat(
  5490. "std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
  5491. " aaaaaaa, aaaaaaaaaa, aaaaa, aaaaaaaaaaaaaaa, aaa, aaaaaaaaaa, a,\n"
  5492. " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaa,\n"
  5493. " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa, aaaaaaa, a};");
  5494. verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
  5495. }
  5496. TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
  5497. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5498. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5499. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5500. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5501. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5502. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  5503. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5504. " // line comment\n"
  5505. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5506. " 1, 22, 333, 4444, 55555,\n"
  5507. " // line comment\n"
  5508. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5509. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  5510. verifyFormat(
  5511. "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5512. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5513. " 1, 22, 333, 4444, 55555, 666666, // comment\n"
  5514. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  5515. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  5516. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  5517. " 7777777};");
  5518. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  5519. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  5520. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  5521. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  5522. " 1, 1, 1, 1};",
  5523. getLLVMStyleWithColumns(39));
  5524. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  5525. " 1, 1, 1, 1};",
  5526. getLLVMStyleWithColumns(38));
  5527. verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
  5528. " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
  5529. getLLVMStyleWithColumns(43));
  5530. // Trailing commas.
  5531. verifyFormat("vector<int> x = {\n"
  5532. " 1, 1, 1, 1, 1, 1, 1, 1,\n"
  5533. "};",
  5534. getLLVMStyleWithColumns(39));
  5535. verifyFormat("vector<int> x = {\n"
  5536. " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
  5537. "};",
  5538. getLLVMStyleWithColumns(39));
  5539. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  5540. " 1, 1, 1, 1,\n"
  5541. " /**/ /**/};",
  5542. getLLVMStyleWithColumns(39));
  5543. verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
  5544. " {aaaaaaaaaaaaaaaaaaa},\n"
  5545. " {aaaaaaaaaaaaaaaaaaaaa},\n"
  5546. " {aaaaaaaaaaaaaaaaa}};",
  5547. getLLVMStyleWithColumns(60));
  5548. // With nested lists, we should either format one item per line or all nested
  5549. // lists one one line.
  5550. // FIXME: For some nested lists, we can do better.
  5551. verifyFormat(
  5552. "SomeStruct my_struct_array = {\n"
  5553. " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
  5554. " aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
  5555. " {aaa, aaa},\n"
  5556. " {aaa, aaa},\n"
  5557. " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
  5558. " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5559. " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
  5560. // No column layout should be used here.
  5561. verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
  5562. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
  5563. }
  5564. TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
  5565. FormatStyle DoNotMerge = getLLVMStyle();
  5566. DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  5567. verifyFormat("void f() { return 42; }");
  5568. verifyFormat("void f() {\n"
  5569. " return 42;\n"
  5570. "}",
  5571. DoNotMerge);
  5572. verifyFormat("void f() {\n"
  5573. " // Comment\n"
  5574. "}");
  5575. verifyFormat("{\n"
  5576. "#error {\n"
  5577. " int a;\n"
  5578. "}");
  5579. verifyFormat("{\n"
  5580. " int a;\n"
  5581. "#error {\n"
  5582. "}");
  5583. verifyFormat("void f() {} // comment");
  5584. verifyFormat("void f() { int a; } // comment");
  5585. verifyFormat("void f() {\n"
  5586. "} // comment",
  5587. DoNotMerge);
  5588. verifyFormat("void f() {\n"
  5589. " int a;\n"
  5590. "} // comment",
  5591. DoNotMerge);
  5592. verifyFormat("void f() {\n"
  5593. "} // comment",
  5594. getLLVMStyleWithColumns(15));
  5595. verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
  5596. verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
  5597. verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
  5598. verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
  5599. verifyFormat("class C {\n"
  5600. " C()\n"
  5601. " : iiiiiiii(nullptr),\n"
  5602. " kkkkkkk(nullptr),\n"
  5603. " mmmmmmm(nullptr),\n"
  5604. " nnnnnnn(nullptr) {}\n"
  5605. "};",
  5606. getGoogleStyle());
  5607. FormatStyle NoColumnLimit = getLLVMStyle();
  5608. NoColumnLimit.ColumnLimit = 0;
  5609. EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
  5610. EXPECT_EQ("class C {\n"
  5611. " A() : b(0) {}\n"
  5612. "};", format("class C{A():b(0){}};", NoColumnLimit));
  5613. EXPECT_EQ("A()\n"
  5614. " : b(0) {\n"
  5615. "}",
  5616. format("A()\n:b(0)\n{\n}", NoColumnLimit));
  5617. FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
  5618. DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
  5619. FormatStyle::SFS_None;
  5620. EXPECT_EQ("A()\n"
  5621. " : b(0) {\n"
  5622. "}",
  5623. format("A():b(0){}", DoNotMergeNoColumnLimit));
  5624. EXPECT_EQ("A()\n"
  5625. " : b(0) {\n"
  5626. "}",
  5627. format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
  5628. verifyFormat("#define A \\\n"
  5629. " void f() { \\\n"
  5630. " int i; \\\n"
  5631. " }",
  5632. getLLVMStyleWithColumns(20));
  5633. verifyFormat("#define A \\\n"
  5634. " void f() { int i; }",
  5635. getLLVMStyleWithColumns(21));
  5636. verifyFormat("#define A \\\n"
  5637. " void f() { \\\n"
  5638. " int i; \\\n"
  5639. " } \\\n"
  5640. " int j;",
  5641. getLLVMStyleWithColumns(22));
  5642. verifyFormat("#define A \\\n"
  5643. " void f() { int i; } \\\n"
  5644. " int j;",
  5645. getLLVMStyleWithColumns(23));
  5646. }
  5647. TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
  5648. FormatStyle MergeInlineOnly = getLLVMStyle();
  5649. MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  5650. verifyFormat("class C {\n"
  5651. " int f() { return 42; }\n"
  5652. "};",
  5653. MergeInlineOnly);
  5654. verifyFormat("int f() {\n"
  5655. " return 42;\n"
  5656. "}",
  5657. MergeInlineOnly);
  5658. }
  5659. TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
  5660. // Elaborate type variable declarations.
  5661. verifyFormat("struct foo a = {bar};\nint n;");
  5662. verifyFormat("class foo a = {bar};\nint n;");
  5663. verifyFormat("union foo a = {bar};\nint n;");
  5664. // Elaborate types inside function definitions.
  5665. verifyFormat("struct foo f() {}\nint n;");
  5666. verifyFormat("class foo f() {}\nint n;");
  5667. verifyFormat("union foo f() {}\nint n;");
  5668. // Templates.
  5669. verifyFormat("template <class X> void f() {}\nint n;");
  5670. verifyFormat("template <struct X> void f() {}\nint n;");
  5671. verifyFormat("template <union X> void f() {}\nint n;");
  5672. // Actual definitions...
  5673. verifyFormat("struct {\n} n;");
  5674. verifyFormat(
  5675. "template <template <class T, class Y>, class Z> class X {\n} n;");
  5676. verifyFormat("union Z {\n int n;\n} x;");
  5677. verifyFormat("class MACRO Z {\n} n;");
  5678. verifyFormat("class MACRO(X) Z {\n} n;");
  5679. verifyFormat("class __attribute__(X) Z {\n} n;");
  5680. verifyFormat("class __declspec(X) Z {\n} n;");
  5681. verifyFormat("class A##B##C {\n} n;");
  5682. verifyFormat("class alignas(16) Z {\n} n;");
  5683. // Redefinition from nested context:
  5684. verifyFormat("class A::B::C {\n} n;");
  5685. // Template definitions.
  5686. verifyFormat(
  5687. "template <typename F>\n"
  5688. "Matcher(const Matcher<F> &Other,\n"
  5689. " typename enable_if_c<is_base_of<F, T>::value &&\n"
  5690. " !is_same<F, T>::value>::type * = 0)\n"
  5691. " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
  5692. // FIXME: This is still incorrectly handled at the formatter side.
  5693. verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
  5694. // FIXME:
  5695. // This now gets parsed incorrectly as class definition.
  5696. // verifyFormat("class A<int> f() {\n}\nint n;");
  5697. // Elaborate types where incorrectly parsing the structural element would
  5698. // break the indent.
  5699. verifyFormat("if (true)\n"
  5700. " class X x;\n"
  5701. "else\n"
  5702. " f();\n");
  5703. // This is simply incomplete. Formatting is not important, but must not crash.
  5704. verifyFormat("class A:");
  5705. }
  5706. TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
  5707. EXPECT_EQ("#error Leave all white!!!!! space* alone!\n",
  5708. format("#error Leave all white!!!!! space* alone!\n"));
  5709. EXPECT_EQ(
  5710. "#warning Leave all white!!!!! space* alone!\n",
  5711. format("#warning Leave all white!!!!! space* alone!\n"));
  5712. EXPECT_EQ("#error 1", format(" # error 1"));
  5713. EXPECT_EQ("#warning 1", format(" # warning 1"));
  5714. }
  5715. TEST_F(FormatTest, FormatHashIfExpressions) {
  5716. verifyFormat("#if AAAA && BBBB");
  5717. // FIXME: Come up with a better indentation for #elif.
  5718. verifyFormat(
  5719. "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
  5720. " defined(BBBBBBBB)\n"
  5721. "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
  5722. " defined(BBBBBBBB)\n"
  5723. "#endif",
  5724. getLLVMStyleWithColumns(65));
  5725. }
  5726. TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
  5727. FormatStyle AllowsMergedIf = getGoogleStyle();
  5728. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  5729. verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
  5730. verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
  5731. verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
  5732. EXPECT_EQ("if (true) return 42;",
  5733. format("if (true)\nreturn 42;", AllowsMergedIf));
  5734. FormatStyle ShortMergedIf = AllowsMergedIf;
  5735. ShortMergedIf.ColumnLimit = 25;
  5736. verifyFormat("#define A \\\n"
  5737. " if (true) return 42;",
  5738. ShortMergedIf);
  5739. verifyFormat("#define A \\\n"
  5740. " f(); \\\n"
  5741. " if (true)\n"
  5742. "#define B",
  5743. ShortMergedIf);
  5744. verifyFormat("#define A \\\n"
  5745. " f(); \\\n"
  5746. " if (true)\n"
  5747. "g();",
  5748. ShortMergedIf);
  5749. verifyFormat("{\n"
  5750. "#ifdef A\n"
  5751. " // Comment\n"
  5752. " if (true) continue;\n"
  5753. "#endif\n"
  5754. " // Comment\n"
  5755. " if (true) continue;\n"
  5756. "}",
  5757. ShortMergedIf);
  5758. ShortMergedIf.ColumnLimit = 29;
  5759. verifyFormat("#define A \\\n"
  5760. " if (aaaaaaaaaa) return 1; \\\n"
  5761. " return 2;",
  5762. ShortMergedIf);
  5763. ShortMergedIf.ColumnLimit = 28;
  5764. verifyFormat("#define A \\\n"
  5765. " if (aaaaaaaaaa) \\\n"
  5766. " return 1; \\\n"
  5767. " return 2;",
  5768. ShortMergedIf);
  5769. }
  5770. TEST_F(FormatTest, BlockCommentsInControlLoops) {
  5771. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  5772. " f();\n"
  5773. "}");
  5774. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  5775. " f();\n"
  5776. "} /* another comment */ else /* comment #3 */ {\n"
  5777. " g();\n"
  5778. "}");
  5779. verifyFormat("while (0) /* a comment in a strange place */ {\n"
  5780. " f();\n"
  5781. "}");
  5782. verifyFormat("for (;;) /* a comment in a strange place */ {\n"
  5783. " f();\n"
  5784. "}");
  5785. verifyFormat("do /* a comment in a strange place */ {\n"
  5786. " f();\n"
  5787. "} /* another comment */ while (0);");
  5788. }
  5789. TEST_F(FormatTest, BlockComments) {
  5790. EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
  5791. format("/* *//* */ /* */\n/* *//* */ /* */"));
  5792. EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
  5793. EXPECT_EQ("#define A /*123*/ \\\n"
  5794. " b\n"
  5795. "/* */\n"
  5796. "someCall(\n"
  5797. " parameter);",
  5798. format("#define A /*123*/ b\n"
  5799. "/* */\n"
  5800. "someCall(parameter);",
  5801. getLLVMStyleWithColumns(15)));
  5802. EXPECT_EQ("#define A\n"
  5803. "/* */ someCall(\n"
  5804. " parameter);",
  5805. format("#define A\n"
  5806. "/* */someCall(parameter);",
  5807. getLLVMStyleWithColumns(15)));
  5808. EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
  5809. EXPECT_EQ("/*\n"
  5810. "*\n"
  5811. " * aaaaaa\n"
  5812. "*aaaaaa\n"
  5813. "*/",
  5814. format("/*\n"
  5815. "*\n"
  5816. " * aaaaaa aaaaaa\n"
  5817. "*/",
  5818. getLLVMStyleWithColumns(10)));
  5819. EXPECT_EQ("/*\n"
  5820. "**\n"
  5821. "* aaaaaa\n"
  5822. "*aaaaaa\n"
  5823. "*/",
  5824. format("/*\n"
  5825. "**\n"
  5826. "* aaaaaa aaaaaa\n"
  5827. "*/",
  5828. getLLVMStyleWithColumns(10)));
  5829. FormatStyle NoBinPacking = getLLVMStyle();
  5830. NoBinPacking.BinPackParameters = false;
  5831. EXPECT_EQ("someFunction(1, /* comment 1 */\n"
  5832. " 2, /* comment 2 */\n"
  5833. " 3, /* comment 3 */\n"
  5834. " aaaa,\n"
  5835. " bbbb);",
  5836. format("someFunction (1, /* comment 1 */\n"
  5837. " 2, /* comment 2 */ \n"
  5838. " 3, /* comment 3 */\n"
  5839. "aaaa, bbbb );",
  5840. NoBinPacking));
  5841. verifyFormat(
  5842. "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  5843. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  5844. EXPECT_EQ(
  5845. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  5846. " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  5847. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
  5848. format(
  5849. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  5850. " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  5851. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
  5852. EXPECT_EQ(
  5853. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  5854. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  5855. "int cccccccccccccccccccccccccccccc; /* comment */\n",
  5856. format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  5857. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  5858. "int cccccccccccccccccccccccccccccc; /* comment */\n"));
  5859. verifyFormat("void f(int * /* unused */) {}");
  5860. EXPECT_EQ("/*\n"
  5861. " **\n"
  5862. " */",
  5863. format("/*\n"
  5864. " **\n"
  5865. " */"));
  5866. EXPECT_EQ("/*\n"
  5867. " *q\n"
  5868. " */",
  5869. format("/*\n"
  5870. " *q\n"
  5871. " */"));
  5872. EXPECT_EQ("/*\n"
  5873. " * q\n"
  5874. " */",
  5875. format("/*\n"
  5876. " * q\n"
  5877. " */"));
  5878. EXPECT_EQ("/*\n"
  5879. " **/",
  5880. format("/*\n"
  5881. " **/"));
  5882. EXPECT_EQ("/*\n"
  5883. " ***/",
  5884. format("/*\n"
  5885. " ***/"));
  5886. }
  5887. TEST_F(FormatTest, BlockCommentsInMacros) {
  5888. EXPECT_EQ("#define A \\\n"
  5889. " { \\\n"
  5890. " /* one line */ \\\n"
  5891. " someCall();",
  5892. format("#define A { \\\n"
  5893. " /* one line */ \\\n"
  5894. " someCall();",
  5895. getLLVMStyleWithColumns(20)));
  5896. EXPECT_EQ("#define A \\\n"
  5897. " { \\\n"
  5898. " /* previous */ \\\n"
  5899. " /* one line */ \\\n"
  5900. " someCall();",
  5901. format("#define A { \\\n"
  5902. " /* previous */ \\\n"
  5903. " /* one line */ \\\n"
  5904. " someCall();",
  5905. getLLVMStyleWithColumns(20)));
  5906. }
  5907. TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
  5908. EXPECT_EQ("a = {\n"
  5909. " 1111 /* */\n"
  5910. "};",
  5911. format("a = {1111 /* */\n"
  5912. "};",
  5913. getLLVMStyleWithColumns(15)));
  5914. EXPECT_EQ("a = {\n"
  5915. " 1111 /* */\n"
  5916. "};",
  5917. format("a = {1111 /* */\n"
  5918. "};",
  5919. getLLVMStyleWithColumns(15)));
  5920. // FIXME: The formatting is still wrong here.
  5921. EXPECT_EQ("a = {\n"
  5922. " 1111 /* a\n"
  5923. " */\n"
  5924. "};",
  5925. format("a = {1111 /* a */\n"
  5926. "};",
  5927. getLLVMStyleWithColumns(15)));
  5928. }
  5929. TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
  5930. // FIXME: This is not what we want...
  5931. verifyFormat("{\n"
  5932. "// a"
  5933. "// b");
  5934. }
  5935. TEST_F(FormatTest, FormatStarDependingOnContext) {
  5936. verifyFormat("void f(int *a);");
  5937. verifyFormat("void f() { f(fint * b); }");
  5938. verifyFormat("class A {\n void f(int *a);\n};");
  5939. verifyFormat("class A {\n int *a;\n};");
  5940. verifyFormat("namespace a {\n"
  5941. "namespace b {\n"
  5942. "class A {\n"
  5943. " void f() {}\n"
  5944. " int *a;\n"
  5945. "};\n"
  5946. "}\n"
  5947. "}");
  5948. }
  5949. TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
  5950. verifyFormat("while");
  5951. verifyFormat("operator");
  5952. }
  5953. //===----------------------------------------------------------------------===//
  5954. // Objective-C tests.
  5955. //===----------------------------------------------------------------------===//
  5956. TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
  5957. verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
  5958. EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
  5959. format("-(NSUInteger)indexOfObject:(id)anObject;"));
  5960. EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
  5961. EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
  5962. EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
  5963. format("-(NSInteger)Method3:(id)anObject;"));
  5964. EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
  5965. format("-(NSInteger)Method4:(id)anObject;"));
  5966. EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
  5967. format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
  5968. EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
  5969. format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
  5970. EXPECT_EQ(
  5971. "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
  5972. format(
  5973. "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
  5974. // Very long objectiveC method declaration.
  5975. verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
  5976. " inRange:(NSRange)range\n"
  5977. " outRange:(NSRange)out_range\n"
  5978. " outRange1:(NSRange)out_range1\n"
  5979. " outRange2:(NSRange)out_range2\n"
  5980. " outRange3:(NSRange)out_range3\n"
  5981. " outRange4:(NSRange)out_range4\n"
  5982. " outRange5:(NSRange)out_range5\n"
  5983. " outRange6:(NSRange)out_range6\n"
  5984. " outRange7:(NSRange)out_range7\n"
  5985. " outRange8:(NSRange)out_range8\n"
  5986. " outRange9:(NSRange)out_range9;");
  5987. verifyFormat("- (int)sum:(vector<int>)numbers;");
  5988. verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
  5989. // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
  5990. // protocol lists (but not for template classes):
  5991. //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
  5992. verifyFormat("- (int (*)())foo:(int (*)())f;");
  5993. verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
  5994. // If there's no return type (very rare in practice!), LLVM and Google style
  5995. // agree.
  5996. verifyFormat("- foo;");
  5997. verifyFormat("- foo:(int)f;");
  5998. verifyGoogleFormat("- foo:(int)foo;");
  5999. }
  6000. TEST_F(FormatTest, FormatObjCInterface) {
  6001. verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
  6002. "@public\n"
  6003. " int field1;\n"
  6004. "@protected\n"
  6005. " int field2;\n"
  6006. "@private\n"
  6007. " int field3;\n"
  6008. "@package\n"
  6009. " int field4;\n"
  6010. "}\n"
  6011. "+ (id)init;\n"
  6012. "@end");
  6013. verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
  6014. " @public\n"
  6015. " int field1;\n"
  6016. " @protected\n"
  6017. " int field2;\n"
  6018. " @private\n"
  6019. " int field3;\n"
  6020. " @package\n"
  6021. " int field4;\n"
  6022. "}\n"
  6023. "+ (id)init;\n"
  6024. "@end");
  6025. verifyFormat("@interface /* wait for it */ Foo\n"
  6026. "+ (id)init;\n"
  6027. "// Look, a comment!\n"
  6028. "- (int)answerWith:(int)i;\n"
  6029. "@end");
  6030. verifyFormat("@interface Foo\n"
  6031. "@end\n"
  6032. "@interface Bar\n"
  6033. "@end");
  6034. verifyFormat("@interface Foo : Bar\n"
  6035. "+ (id)init;\n"
  6036. "@end");
  6037. verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
  6038. "+ (id)init;\n"
  6039. "@end");
  6040. verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
  6041. "+ (id)init;\n"
  6042. "@end");
  6043. verifyFormat("@interface Foo (HackStuff)\n"
  6044. "+ (id)init;\n"
  6045. "@end");
  6046. verifyFormat("@interface Foo ()\n"
  6047. "+ (id)init;\n"
  6048. "@end");
  6049. verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
  6050. "+ (id)init;\n"
  6051. "@end");
  6052. verifyGoogleFormat("@interface Foo (HackStuff) <MyProtocol>\n"
  6053. "+ (id)init;\n"
  6054. "@end");
  6055. verifyFormat("@interface Foo {\n"
  6056. " int _i;\n"
  6057. "}\n"
  6058. "+ (id)init;\n"
  6059. "@end");
  6060. verifyFormat("@interface Foo : Bar {\n"
  6061. " int _i;\n"
  6062. "}\n"
  6063. "+ (id)init;\n"
  6064. "@end");
  6065. verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
  6066. " int _i;\n"
  6067. "}\n"
  6068. "+ (id)init;\n"
  6069. "@end");
  6070. verifyFormat("@interface Foo (HackStuff) {\n"
  6071. " int _i;\n"
  6072. "}\n"
  6073. "+ (id)init;\n"
  6074. "@end");
  6075. verifyFormat("@interface Foo () {\n"
  6076. " int _i;\n"
  6077. "}\n"
  6078. "+ (id)init;\n"
  6079. "@end");
  6080. verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
  6081. " int _i;\n"
  6082. "}\n"
  6083. "+ (id)init;\n"
  6084. "@end");
  6085. FormatStyle OnePerLine = getGoogleStyle();
  6086. OnePerLine.BinPackParameters = false;
  6087. verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa () <\n"
  6088. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6089. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6090. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6091. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
  6092. "}",
  6093. OnePerLine);
  6094. }
  6095. TEST_F(FormatTest, FormatObjCImplementation) {
  6096. verifyFormat("@implementation Foo : NSObject {\n"
  6097. "@public\n"
  6098. " int field1;\n"
  6099. "@protected\n"
  6100. " int field2;\n"
  6101. "@private\n"
  6102. " int field3;\n"
  6103. "@package\n"
  6104. " int field4;\n"
  6105. "}\n"
  6106. "+ (id)init {\n}\n"
  6107. "@end");
  6108. verifyGoogleFormat("@implementation Foo : NSObject {\n"
  6109. " @public\n"
  6110. " int field1;\n"
  6111. " @protected\n"
  6112. " int field2;\n"
  6113. " @private\n"
  6114. " int field3;\n"
  6115. " @package\n"
  6116. " int field4;\n"
  6117. "}\n"
  6118. "+ (id)init {\n}\n"
  6119. "@end");
  6120. verifyFormat("@implementation Foo\n"
  6121. "+ (id)init {\n"
  6122. " if (true)\n"
  6123. " return nil;\n"
  6124. "}\n"
  6125. "// Look, a comment!\n"
  6126. "- (int)answerWith:(int)i {\n"
  6127. " return i;\n"
  6128. "}\n"
  6129. "+ (int)answerWith:(int)i {\n"
  6130. " return i;\n"
  6131. "}\n"
  6132. "@end");
  6133. verifyFormat("@implementation Foo\n"
  6134. "@end\n"
  6135. "@implementation Bar\n"
  6136. "@end");
  6137. EXPECT_EQ("@implementation Foo : Bar\n"
  6138. "+ (id)init {\n}\n"
  6139. "- (void)foo {\n}\n"
  6140. "@end",
  6141. format("@implementation Foo : Bar\n"
  6142. "+(id)init{}\n"
  6143. "-(void)foo{}\n"
  6144. "@end"));
  6145. verifyFormat("@implementation Foo {\n"
  6146. " int _i;\n"
  6147. "}\n"
  6148. "+ (id)init {\n}\n"
  6149. "@end");
  6150. verifyFormat("@implementation Foo : Bar {\n"
  6151. " int _i;\n"
  6152. "}\n"
  6153. "+ (id)init {\n}\n"
  6154. "@end");
  6155. verifyFormat("@implementation Foo (HackStuff)\n"
  6156. "+ (id)init {\n}\n"
  6157. "@end");
  6158. verifyFormat("@implementation ObjcClass\n"
  6159. "- (void)method;\n"
  6160. "{}\n"
  6161. "@end");
  6162. }
  6163. TEST_F(FormatTest, FormatObjCProtocol) {
  6164. verifyFormat("@protocol Foo\n"
  6165. "@property(weak) id delegate;\n"
  6166. "- (NSUInteger)numberOfThings;\n"
  6167. "@end");
  6168. verifyFormat("@protocol MyProtocol <NSObject>\n"
  6169. "- (NSUInteger)numberOfThings;\n"
  6170. "@end");
  6171. verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
  6172. "- (NSUInteger)numberOfThings;\n"
  6173. "@end");
  6174. verifyFormat("@protocol Foo;\n"
  6175. "@protocol Bar;\n");
  6176. verifyFormat("@protocol Foo\n"
  6177. "@end\n"
  6178. "@protocol Bar\n"
  6179. "@end");
  6180. verifyFormat("@protocol myProtocol\n"
  6181. "- (void)mandatoryWithInt:(int)i;\n"
  6182. "@optional\n"
  6183. "- (void)optional;\n"
  6184. "@required\n"
  6185. "- (void)required;\n"
  6186. "@optional\n"
  6187. "@property(assign) int madProp;\n"
  6188. "@end\n");
  6189. verifyFormat("@property(nonatomic, assign, readonly)\n"
  6190. " int *looooooooooooooooooooooooooooongNumber;\n"
  6191. "@property(nonatomic, assign, readonly)\n"
  6192. " NSString *looooooooooooooooooooooooooooongName;");
  6193. verifyFormat("@implementation PR18406\n"
  6194. "}\n"
  6195. "@end");
  6196. }
  6197. TEST_F(FormatTest, FormatObjCMethodDeclarations) {
  6198. verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
  6199. " rect:(NSRect)theRect\n"
  6200. " interval:(float)theInterval {\n"
  6201. "}");
  6202. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6203. " longKeyword:(NSRect)theRect\n"
  6204. " evenLongerKeyword:(float)theInterval\n"
  6205. " error:(NSError **)theError {\n"
  6206. "}");
  6207. verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
  6208. " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
  6209. " NS_DESIGNATED_INITIALIZER;",
  6210. getLLVMStyleWithColumns(60));
  6211. }
  6212. TEST_F(FormatTest, FormatObjCMethodExpr) {
  6213. verifyFormat("[foo bar:baz];");
  6214. verifyFormat("return [foo bar:baz];");
  6215. verifyFormat("return (a)[foo bar:baz];");
  6216. verifyFormat("f([foo bar:baz]);");
  6217. verifyFormat("f(2, [foo bar:baz]);");
  6218. verifyFormat("f(2, a ? b : c);");
  6219. verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
  6220. // Unary operators.
  6221. verifyFormat("int a = +[foo bar:baz];");
  6222. verifyFormat("int a = -[foo bar:baz];");
  6223. verifyFormat("int a = ![foo bar:baz];");
  6224. verifyFormat("int a = ~[foo bar:baz];");
  6225. verifyFormat("int a = ++[foo bar:baz];");
  6226. verifyFormat("int a = --[foo bar:baz];");
  6227. verifyFormat("int a = sizeof [foo bar:baz];");
  6228. verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
  6229. verifyFormat("int a = &[foo bar:baz];");
  6230. verifyFormat("int a = *[foo bar:baz];");
  6231. // FIXME: Make casts work, without breaking f()[4].
  6232. //verifyFormat("int a = (int)[foo bar:baz];");
  6233. //verifyFormat("return (int)[foo bar:baz];");
  6234. //verifyFormat("(void)[foo bar:baz];");
  6235. verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
  6236. // Binary operators.
  6237. verifyFormat("[foo bar:baz], [foo bar:baz];");
  6238. verifyFormat("[foo bar:baz] = [foo bar:baz];");
  6239. verifyFormat("[foo bar:baz] *= [foo bar:baz];");
  6240. verifyFormat("[foo bar:baz] /= [foo bar:baz];");
  6241. verifyFormat("[foo bar:baz] %= [foo bar:baz];");
  6242. verifyFormat("[foo bar:baz] += [foo bar:baz];");
  6243. verifyFormat("[foo bar:baz] -= [foo bar:baz];");
  6244. verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
  6245. verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
  6246. verifyFormat("[foo bar:baz] &= [foo bar:baz];");
  6247. verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
  6248. verifyFormat("[foo bar:baz] |= [foo bar:baz];");
  6249. verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
  6250. verifyFormat("[foo bar:baz] || [foo bar:baz];");
  6251. verifyFormat("[foo bar:baz] && [foo bar:baz];");
  6252. verifyFormat("[foo bar:baz] | [foo bar:baz];");
  6253. verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
  6254. verifyFormat("[foo bar:baz] & [foo bar:baz];");
  6255. verifyFormat("[foo bar:baz] == [foo bar:baz];");
  6256. verifyFormat("[foo bar:baz] != [foo bar:baz];");
  6257. verifyFormat("[foo bar:baz] >= [foo bar:baz];");
  6258. verifyFormat("[foo bar:baz] <= [foo bar:baz];");
  6259. verifyFormat("[foo bar:baz] > [foo bar:baz];");
  6260. verifyFormat("[foo bar:baz] < [foo bar:baz];");
  6261. verifyFormat("[foo bar:baz] >> [foo bar:baz];");
  6262. verifyFormat("[foo bar:baz] << [foo bar:baz];");
  6263. verifyFormat("[foo bar:baz] - [foo bar:baz];");
  6264. verifyFormat("[foo bar:baz] + [foo bar:baz];");
  6265. verifyFormat("[foo bar:baz] * [foo bar:baz];");
  6266. verifyFormat("[foo bar:baz] / [foo bar:baz];");
  6267. verifyFormat("[foo bar:baz] % [foo bar:baz];");
  6268. // Whew!
  6269. verifyFormat("return in[42];");
  6270. verifyFormat("for (auto v : in[1]) {\n}");
  6271. verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
  6272. "}");
  6273. verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
  6274. verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
  6275. verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
  6276. verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
  6277. verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
  6278. verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
  6279. verifyFormat("[button setAction:@selector(zoomOut:)];");
  6280. verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
  6281. verifyFormat("arr[[self indexForFoo:a]];");
  6282. verifyFormat("throw [self errorFor:a];");
  6283. verifyFormat("@throw [self errorFor:a];");
  6284. verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
  6285. verifyFormat("[(id)foo bar:(id) ? baz : quux];");
  6286. verifyFormat("4 > 4 ? (id)a : (id)baz;");
  6287. // This tests that the formatter doesn't break after "backing" but before ":",
  6288. // which would be at 80 columns.
  6289. verifyFormat(
  6290. "void f() {\n"
  6291. " if ((self = [super initWithContentRect:contentRect\n"
  6292. " styleMask:styleMask ?: otherMask\n"
  6293. " backing:NSBackingStoreBuffered\n"
  6294. " defer:YES]))");
  6295. verifyFormat(
  6296. "[foo checkThatBreakingAfterColonWorksOk:\n"
  6297. " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
  6298. verifyFormat("[myObj short:arg1 // Force line break\n"
  6299. " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
  6300. " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
  6301. " error:arg4];");
  6302. verifyFormat(
  6303. "void f() {\n"
  6304. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  6305. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  6306. " pos.width(), pos.height())\n"
  6307. " styleMask:NSBorderlessWindowMask\n"
  6308. " backing:NSBackingStoreBuffered\n"
  6309. " defer:NO]);\n"
  6310. "}");
  6311. verifyFormat(
  6312. "void f() {\n"
  6313. " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
  6314. " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
  6315. " pos.width(), pos.height())\n"
  6316. " syeMask:NSBorderlessWindowMask\n"
  6317. " bking:NSBackingStoreBuffered\n"
  6318. " der:NO]);\n"
  6319. "}",
  6320. getLLVMStyleWithColumns(70));
  6321. verifyFormat(
  6322. "void f() {\n"
  6323. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  6324. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  6325. " pos.width(), pos.height())\n"
  6326. " styleMask:NSBorderlessWindowMask\n"
  6327. " backing:NSBackingStoreBuffered\n"
  6328. " defer:NO]);\n"
  6329. "}",
  6330. getChromiumStyle(FormatStyle::LK_Cpp));
  6331. verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
  6332. " with:contentsNativeView];");
  6333. verifyFormat(
  6334. "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
  6335. " owner:nillllll];");
  6336. verifyFormat(
  6337. "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
  6338. " forType:kBookmarkButtonDragType];");
  6339. verifyFormat("[defaultCenter addObserver:self\n"
  6340. " selector:@selector(willEnterFullscreen)\n"
  6341. " name:kWillEnterFullscreenNotification\n"
  6342. " object:nil];");
  6343. verifyFormat("[image_rep drawInRect:drawRect\n"
  6344. " fromRect:NSZeroRect\n"
  6345. " operation:NSCompositeCopy\n"
  6346. " fraction:1.0\n"
  6347. " respectFlipped:NO\n"
  6348. " hints:nil];");
  6349. verifyFormat(
  6350. "scoped_nsobject<NSTextField> message(\n"
  6351. " // The frame will be fixed up when |-setMessageText:| is called.\n"
  6352. " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
  6353. verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
  6354. " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
  6355. " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
  6356. " aaaa:bbb];");
  6357. verifyFormat("[self param:function( //\n"
  6358. " parameter)]");
  6359. verifyFormat(
  6360. "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  6361. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  6362. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
  6363. // Variadic parameters.
  6364. verifyFormat(
  6365. "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
  6366. verifyFormat(
  6367. "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  6368. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  6369. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
  6370. verifyFormat("[self // break\n"
  6371. " a:a\n"
  6372. " aaa:aaa];");
  6373. verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
  6374. " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
  6375. }
  6376. TEST_F(FormatTest, ObjCAt) {
  6377. verifyFormat("@autoreleasepool");
  6378. verifyFormat("@catch");
  6379. verifyFormat("@class");
  6380. verifyFormat("@compatibility_alias");
  6381. verifyFormat("@defs");
  6382. verifyFormat("@dynamic");
  6383. verifyFormat("@encode");
  6384. verifyFormat("@end");
  6385. verifyFormat("@finally");
  6386. verifyFormat("@implementation");
  6387. verifyFormat("@import");
  6388. verifyFormat("@interface");
  6389. verifyFormat("@optional");
  6390. verifyFormat("@package");
  6391. verifyFormat("@private");
  6392. verifyFormat("@property");
  6393. verifyFormat("@protected");
  6394. verifyFormat("@protocol");
  6395. verifyFormat("@public");
  6396. verifyFormat("@required");
  6397. verifyFormat("@selector");
  6398. verifyFormat("@synchronized");
  6399. verifyFormat("@synthesize");
  6400. verifyFormat("@throw");
  6401. verifyFormat("@try");
  6402. EXPECT_EQ("@interface", format("@ interface"));
  6403. // The precise formatting of this doesn't matter, nobody writes code like
  6404. // this.
  6405. verifyFormat("@ /*foo*/ interface");
  6406. }
  6407. TEST_F(FormatTest, ObjCSnippets) {
  6408. verifyFormat("@autoreleasepool {\n"
  6409. " foo();\n"
  6410. "}");
  6411. verifyFormat("@class Foo, Bar;");
  6412. verifyFormat("@compatibility_alias AliasName ExistingClass;");
  6413. verifyFormat("@dynamic textColor;");
  6414. verifyFormat("char *buf1 = @encode(int *);");
  6415. verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
  6416. verifyFormat("char *buf1 = @encode(int **);");
  6417. verifyFormat("Protocol *proto = @protocol(p1);");
  6418. verifyFormat("SEL s = @selector(foo:);");
  6419. verifyFormat("@synchronized(self) {\n"
  6420. " f();\n"
  6421. "}");
  6422. verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  6423. verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  6424. verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
  6425. verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
  6426. verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
  6427. verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
  6428. getMozillaStyle());
  6429. verifyFormat("@property BOOL editable;", getMozillaStyle());
  6430. verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
  6431. getWebKitStyle());
  6432. verifyFormat("@property BOOL editable;", getWebKitStyle());
  6433. verifyFormat("@import foo.bar;\n"
  6434. "@import baz;");
  6435. }
  6436. TEST_F(FormatTest, ObjCLiterals) {
  6437. verifyFormat("@\"String\"");
  6438. verifyFormat("@1");
  6439. verifyFormat("@+4.8");
  6440. verifyFormat("@-4");
  6441. verifyFormat("@1LL");
  6442. verifyFormat("@.5");
  6443. verifyFormat("@'c'");
  6444. verifyFormat("@true");
  6445. verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
  6446. verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
  6447. verifyFormat("NSNumber *favoriteColor = @(Green);");
  6448. verifyFormat("NSString *path = @(getenv(\"PATH\"));");
  6449. verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
  6450. }
  6451. TEST_F(FormatTest, ObjCDictLiterals) {
  6452. verifyFormat("@{");
  6453. verifyFormat("@{}");
  6454. verifyFormat("@{@\"one\" : @1}");
  6455. verifyFormat("return @{@\"one\" : @1;");
  6456. verifyFormat("@{@\"one\" : @1}");
  6457. verifyFormat("@{@\"one\" : @{@2 : @1}}");
  6458. verifyFormat("@{\n"
  6459. " @\"one\" : @{@2 : @1},\n"
  6460. "}");
  6461. verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
  6462. verifyFormat("[self setDict:@{}");
  6463. verifyFormat("[self setDict:@{@1 : @2}");
  6464. verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
  6465. verifyFormat(
  6466. "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
  6467. verifyFormat(
  6468. "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
  6469. verifyFormat(
  6470. "NSDictionary *d = @{\n"
  6471. " @\"nam\" : NSUserNam(),\n"
  6472. " @\"dte\" : [NSDate date],\n"
  6473. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  6474. "};");
  6475. verifyFormat(
  6476. "@{\n"
  6477. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  6478. "regularFont,\n"
  6479. "};");
  6480. verifyGoogleFormat(
  6481. "@{\n"
  6482. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  6483. "regularFont,\n"
  6484. "};");
  6485. verifyFormat(
  6486. "@{\n"
  6487. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
  6488. " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
  6489. "};");
  6490. // We should try to be robust in case someone forgets the "@".
  6491. verifyFormat(
  6492. "NSDictionary *d = {\n"
  6493. " @\"nam\" : NSUserNam(),\n"
  6494. " @\"dte\" : [NSDate date],\n"
  6495. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  6496. "};");
  6497. verifyFormat("NSMutableDictionary *dictionary =\n"
  6498. " [NSMutableDictionary dictionaryWithDictionary:@{\n"
  6499. " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
  6500. " bbbbbbbbbbbbbbbbbb : bbbbb,\n"
  6501. " cccccccccccccccc : ccccccccccccccc\n"
  6502. " }];");
  6503. }
  6504. TEST_F(FormatTest, ObjCArrayLiterals) {
  6505. verifyFormat("@[");
  6506. verifyFormat("@[]");
  6507. verifyFormat(
  6508. "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
  6509. verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
  6510. verifyFormat("NSArray *array = @[ [foo description] ];");
  6511. verifyFormat(
  6512. "NSArray *some_variable = @[\n"
  6513. " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
  6514. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6515. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6516. " @\"aaaaaaaaaaaaaaaaa\"\n"
  6517. "];");
  6518. verifyFormat("NSArray *some_variable = @[\n"
  6519. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6520. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6521. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6522. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6523. "];");
  6524. verifyGoogleFormat("NSArray *some_variable = @[\n"
  6525. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6526. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6527. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6528. " @\"aaaaaaaaaaaaaaaaa\"\n"
  6529. "];");
  6530. // We should try to be robust in case someone forgets the "@".
  6531. verifyFormat("NSArray *some_variable = [\n"
  6532. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6533. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6534. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6535. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6536. "];");
  6537. verifyFormat(
  6538. "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
  6539. " index:(NSUInteger)index\n"
  6540. " nonDigitAttributes:\n"
  6541. " (NSDictionary *)noDigitAttributes;");
  6542. verifyFormat(
  6543. "[someFunction someLooooooooooooongParameter:\n"
  6544. " @[ NSBundle.mainBundle.infoDictionary[@\"a\"] ]];");
  6545. }
  6546. TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
  6547. EXPECT_EQ("{\n"
  6548. "{\n"
  6549. "a;\n"
  6550. "b;\n"
  6551. "}\n"
  6552. "}",
  6553. format("{\n"
  6554. "{\n"
  6555. "a;\n"
  6556. " b;\n"
  6557. "}\n"
  6558. "}",
  6559. 13, 2, getLLVMStyle()));
  6560. EXPECT_EQ("{\n"
  6561. "{\n"
  6562. " a;\n"
  6563. "b;\n"
  6564. "}\n"
  6565. "}",
  6566. format("{\n"
  6567. "{\n"
  6568. " a;\n"
  6569. "b;\n"
  6570. "}\n"
  6571. "}",
  6572. 9, 2, getLLVMStyle()));
  6573. EXPECT_EQ("{\n"
  6574. "{\n"
  6575. "public:\n"
  6576. " b;\n"
  6577. "}\n"
  6578. "}",
  6579. format("{\n"
  6580. "{\n"
  6581. "public:\n"
  6582. " b;\n"
  6583. "}\n"
  6584. "}",
  6585. 17, 2, getLLVMStyle()));
  6586. EXPECT_EQ("{\n"
  6587. "{\n"
  6588. "a;\n"
  6589. "}\n"
  6590. "{\n"
  6591. " b; //\n"
  6592. "}\n"
  6593. "}",
  6594. format("{\n"
  6595. "{\n"
  6596. "a;\n"
  6597. "}\n"
  6598. "{\n"
  6599. " b; //\n"
  6600. "}\n"
  6601. "}",
  6602. 22, 2, getLLVMStyle()));
  6603. EXPECT_EQ(" {\n"
  6604. " a; //\n"
  6605. " }",
  6606. format(" {\n"
  6607. "a; //\n"
  6608. " }",
  6609. 4, 2, getLLVMStyle()));
  6610. EXPECT_EQ("void f() {}\n"
  6611. "void g() {}",
  6612. format("void f() {}\n"
  6613. "void g() {}",
  6614. 13, 0, getLLVMStyle()));
  6615. EXPECT_EQ("int a; // comment\n"
  6616. " // line 2\n"
  6617. "int b;",
  6618. format("int a; // comment\n"
  6619. " // line 2\n"
  6620. " int b;",
  6621. 35, 0, getLLVMStyle()));
  6622. EXPECT_EQ(" int a;\n"
  6623. " void\n"
  6624. " ffffff() {\n"
  6625. " }",
  6626. format(" int a;\n"
  6627. "void ffffff() {}",
  6628. 11, 0, getLLVMStyleWithColumns(11)));
  6629. EXPECT_EQ(" void f() {\n"
  6630. "#define A 1\n"
  6631. " }",
  6632. format(" void f() {\n"
  6633. " #define A 1\n" // Format this line.
  6634. " }",
  6635. 20, 0, getLLVMStyle()));
  6636. EXPECT_EQ(" void f() {\n"
  6637. " int i;\n"
  6638. "#define A \\\n"
  6639. " int i; \\\n"
  6640. " int j;\n"
  6641. " int k;\n"
  6642. " }",
  6643. format(" void f() {\n"
  6644. " int i;\n"
  6645. "#define A \\\n"
  6646. " int i; \\\n"
  6647. " int j;\n"
  6648. " int k;\n" // Format this line.
  6649. " }",
  6650. 67, 0, getLLVMStyle()));
  6651. }
  6652. TEST_F(FormatTest, BreaksStringLiterals) {
  6653. EXPECT_EQ("\"some text \"\n"
  6654. "\"other\";",
  6655. format("\"some text other\";", getLLVMStyleWithColumns(12)));
  6656. EXPECT_EQ("\"some text \"\n"
  6657. "\"other\";",
  6658. format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
  6659. EXPECT_EQ(
  6660. "#define A \\\n"
  6661. " \"some \" \\\n"
  6662. " \"text \" \\\n"
  6663. " \"other\";",
  6664. format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
  6665. EXPECT_EQ(
  6666. "#define A \\\n"
  6667. " \"so \" \\\n"
  6668. " \"text \" \\\n"
  6669. " \"other\";",
  6670. format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
  6671. EXPECT_EQ("\"some text\"",
  6672. format("\"some text\"", getLLVMStyleWithColumns(1)));
  6673. EXPECT_EQ("\"some text\"",
  6674. format("\"some text\"", getLLVMStyleWithColumns(11)));
  6675. EXPECT_EQ("\"some \"\n"
  6676. "\"text\"",
  6677. format("\"some text\"", getLLVMStyleWithColumns(10)));
  6678. EXPECT_EQ("\"some \"\n"
  6679. "\"text\"",
  6680. format("\"some text\"", getLLVMStyleWithColumns(7)));
  6681. EXPECT_EQ("\"some\"\n"
  6682. "\" tex\"\n"
  6683. "\"t\"",
  6684. format("\"some text\"", getLLVMStyleWithColumns(6)));
  6685. EXPECT_EQ("\"some\"\n"
  6686. "\" tex\"\n"
  6687. "\" and\"",
  6688. format("\"some tex and\"", getLLVMStyleWithColumns(6)));
  6689. EXPECT_EQ("\"some\"\n"
  6690. "\"/tex\"\n"
  6691. "\"/and\"",
  6692. format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
  6693. EXPECT_EQ("variable =\n"
  6694. " \"long string \"\n"
  6695. " \"literal\";",
  6696. format("variable = \"long string literal\";",
  6697. getLLVMStyleWithColumns(20)));
  6698. EXPECT_EQ("variable = f(\n"
  6699. " \"long string \"\n"
  6700. " \"literal\",\n"
  6701. " short,\n"
  6702. " loooooooooooooooooooong);",
  6703. format("variable = f(\"long string literal\", short, "
  6704. "loooooooooooooooooooong);",
  6705. getLLVMStyleWithColumns(20)));
  6706. EXPECT_EQ("f(g(\"long string \"\n"
  6707. " \"literal\"),\n"
  6708. " b);",
  6709. format("f(g(\"long string literal\"), b);",
  6710. getLLVMStyleWithColumns(20)));
  6711. EXPECT_EQ("f(g(\"long string \"\n"
  6712. " \"literal\",\n"
  6713. " a),\n"
  6714. " b);",
  6715. format("f(g(\"long string literal\", a), b);",
  6716. getLLVMStyleWithColumns(20)));
  6717. EXPECT_EQ(
  6718. "f(\"one two\".split(\n"
  6719. " variable));",
  6720. format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
  6721. EXPECT_EQ("f(\"one two three four five six \"\n"
  6722. " \"seven\".split(\n"
  6723. " really_looooong_variable));",
  6724. format("f(\"one two three four five six seven\"."
  6725. "split(really_looooong_variable));",
  6726. getLLVMStyleWithColumns(33)));
  6727. EXPECT_EQ("f(\"some \"\n"
  6728. " \"text\",\n"
  6729. " other);",
  6730. format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
  6731. // Only break as a last resort.
  6732. verifyFormat(
  6733. "aaaaaaaaaaaaaaaaaaaa(\n"
  6734. " aaaaaaaaaaaaaaaaaaaa,\n"
  6735. " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
  6736. EXPECT_EQ(
  6737. "\"splitmea\"\n"
  6738. "\"trandomp\"\n"
  6739. "\"oint\"",
  6740. format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
  6741. EXPECT_EQ(
  6742. "\"split/\"\n"
  6743. "\"pathat/\"\n"
  6744. "\"slashes\"",
  6745. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  6746. EXPECT_EQ(
  6747. "\"split/\"\n"
  6748. "\"pathat/\"\n"
  6749. "\"slashes\"",
  6750. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  6751. EXPECT_EQ("\"split at \"\n"
  6752. "\"spaces/at/\"\n"
  6753. "\"slashes.at.any$\"\n"
  6754. "\"non-alphanumeric%\"\n"
  6755. "\"1111111111characte\"\n"
  6756. "\"rs\"",
  6757. format("\"split at "
  6758. "spaces/at/"
  6759. "slashes.at."
  6760. "any$non-"
  6761. "alphanumeric%"
  6762. "1111111111characte"
  6763. "rs\"",
  6764. getLLVMStyleWithColumns(20)));
  6765. // Verify that splitting the strings understands
  6766. // Style::AlwaysBreakBeforeMultilineStrings.
  6767. EXPECT_EQ("aaaaaaaaaaaa(aaaaaaaaaaaaa,\n"
  6768. " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
  6769. " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
  6770. format("aaaaaaaaaaaa(aaaaaaaaaaaaa, \"aaaaaaaaaaaaaaaaaaaaaa "
  6771. "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa "
  6772. "aaaaaaaaaaaaaaaaaaaaaa\");",
  6773. getGoogleStyle()));
  6774. EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  6775. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
  6776. format("return \"aaaaaaaaaaaaaaaaaaaaaa "
  6777. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
  6778. "aaaaaaaaaaaaaaaaaaaaaa\";",
  6779. getGoogleStyle()));
  6780. EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  6781. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  6782. format("llvm::outs() << "
  6783. "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
  6784. "aaaaaaaaaaaaaaaaaaa\";"));
  6785. EXPECT_EQ("ffff(\n"
  6786. " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  6787. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  6788. format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  6789. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  6790. getGoogleStyle()));
  6791. FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
  6792. AlignLeft.AlignEscapedNewlinesLeft = true;
  6793. EXPECT_EQ(
  6794. "#define A \\\n"
  6795. " \"some \" \\\n"
  6796. " \"text \" \\\n"
  6797. " \"other\";",
  6798. format("#define A \"some text other\";", AlignLeft));
  6799. }
  6800. TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
  6801. EXPECT_EQ(
  6802. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  6803. "(\n"
  6804. " \"x\t\");",
  6805. format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  6806. "aaaaaaa("
  6807. "\"x\t\");"));
  6808. }
  6809. TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
  6810. EXPECT_EQ(
  6811. "u8\"utf8 string \"\n"
  6812. "u8\"literal\";",
  6813. format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
  6814. EXPECT_EQ(
  6815. "u\"utf16 string \"\n"
  6816. "u\"literal\";",
  6817. format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
  6818. EXPECT_EQ(
  6819. "U\"utf32 string \"\n"
  6820. "U\"literal\";",
  6821. format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
  6822. EXPECT_EQ("L\"wide string \"\n"
  6823. "L\"literal\";",
  6824. format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
  6825. EXPECT_EQ("@\"NSString \"\n"
  6826. "@\"literal\";",
  6827. format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
  6828. }
  6829. TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
  6830. FormatStyle Style = getGoogleStyleWithColumns(15);
  6831. EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
  6832. EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
  6833. EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
  6834. EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
  6835. EXPECT_EQ("u8R\"x(raw literal)x\";",
  6836. format("u8R\"x(raw literal)x\";", Style));
  6837. }
  6838. TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
  6839. FormatStyle Style = getLLVMStyleWithColumns(20);
  6840. EXPECT_EQ(
  6841. "_T(\"aaaaaaaaaaaaaa\")\n"
  6842. "_T(\"aaaaaaaaaaaaaa\")\n"
  6843. "_T(\"aaaaaaaaaaaa\")",
  6844. format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
  6845. EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
  6846. " _T(\"aaaaaa\"),\n"
  6847. " z);",
  6848. format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
  6849. // FIXME: Handle embedded spaces in one iteration.
  6850. // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
  6851. // "_T(\"aaaaaaaaaaaaa\")\n"
  6852. // "_T(\"aaaaaaaaaaaaa\")\n"
  6853. // "_T(\"a\")",
  6854. // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  6855. // getLLVMStyleWithColumns(20)));
  6856. EXPECT_EQ(
  6857. "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  6858. format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
  6859. }
  6860. TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
  6861. EXPECT_EQ(
  6862. "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  6863. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  6864. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  6865. format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  6866. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  6867. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
  6868. }
  6869. TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
  6870. EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
  6871. format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle()));
  6872. EXPECT_EQ("fffffffffff(g(R\"x(\n"
  6873. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6874. ")x\",\n"
  6875. " a),\n"
  6876. " b);",
  6877. format("fffffffffff(g(R\"x(\n"
  6878. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6879. ")x\", a), b);",
  6880. getGoogleStyleWithColumns(20)));
  6881. EXPECT_EQ("fffffffffff(\n"
  6882. " g(R\"x(qqq\n"
  6883. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6884. ")x\",\n"
  6885. " a),\n"
  6886. " b);",
  6887. format("fffffffffff(g(R\"x(qqq\n"
  6888. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6889. ")x\", a), b);",
  6890. getGoogleStyleWithColumns(20)));
  6891. EXPECT_EQ("fffffffffff(R\"x(\n"
  6892. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6893. ")x\");",
  6894. format("fffffffffff(R\"x(\n"
  6895. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6896. ")x\");",
  6897. getGoogleStyleWithColumns(20)));
  6898. EXPECT_EQ("fffffffffff(R\"x(\n"
  6899. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6900. ")x\" + bbbbbb);",
  6901. format("fffffffffff(R\"x(\n"
  6902. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6903. ")x\" + bbbbbb);",
  6904. getGoogleStyleWithColumns(20)));
  6905. EXPECT_EQ("fffffffffff(\n"
  6906. " R\"x(\n"
  6907. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6908. ")x\" +\n"
  6909. " bbbbbb);",
  6910. format("fffffffffff(\n"
  6911. " R\"x(\n"
  6912. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6913. ")x\" + bbbbbb);",
  6914. getGoogleStyleWithColumns(20)));
  6915. }
  6916. TEST_F(FormatTest, SkipsUnknownStringLiterals) {
  6917. verifyFormat("string a = \"unterminated;");
  6918. EXPECT_EQ("function(\"unterminated,\n"
  6919. " OtherParameter);",
  6920. format("function( \"unterminated,\n"
  6921. " OtherParameter);"));
  6922. }
  6923. TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
  6924. FormatStyle Style = getLLVMStyle();
  6925. Style.Standard = FormatStyle::LS_Cpp03;
  6926. EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
  6927. format("#define x(_a) printf(\"foo\"_a);", Style));
  6928. }
  6929. TEST_F(FormatTest, UnderstandsCpp1y) {
  6930. verifyFormat("int bi{1'000'000};");
  6931. }
  6932. TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
  6933. EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
  6934. " \"ddeeefff\");",
  6935. format("someFunction(\"aaabbbcccdddeeefff\");",
  6936. getLLVMStyleWithColumns(25)));
  6937. EXPECT_EQ("someFunction1234567890(\n"
  6938. " \"aaabbbcccdddeeefff\");",
  6939. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  6940. getLLVMStyleWithColumns(26)));
  6941. EXPECT_EQ("someFunction1234567890(\n"
  6942. " \"aaabbbcccdddeeeff\"\n"
  6943. " \"f\");",
  6944. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  6945. getLLVMStyleWithColumns(25)));
  6946. EXPECT_EQ("someFunction1234567890(\n"
  6947. " \"aaabbbcccdddeeeff\"\n"
  6948. " \"f\");",
  6949. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  6950. getLLVMStyleWithColumns(24)));
  6951. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  6952. " \"ddde \"\n"
  6953. " \"efff\");",
  6954. format("someFunction(\"aaabbbcc ddde efff\");",
  6955. getLLVMStyleWithColumns(25)));
  6956. EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
  6957. " \"ddeeefff\");",
  6958. format("someFunction(\"aaabbbccc ddeeefff\");",
  6959. getLLVMStyleWithColumns(25)));
  6960. EXPECT_EQ("someFunction1234567890(\n"
  6961. " \"aaabb \"\n"
  6962. " \"cccdddeeefff\");",
  6963. format("someFunction1234567890(\"aaabb cccdddeeefff\");",
  6964. getLLVMStyleWithColumns(25)));
  6965. EXPECT_EQ("#define A \\\n"
  6966. " string s = \\\n"
  6967. " \"123456789\" \\\n"
  6968. " \"0\"; \\\n"
  6969. " int i;",
  6970. format("#define A string s = \"1234567890\"; int i;",
  6971. getLLVMStyleWithColumns(20)));
  6972. // FIXME: Put additional penalties on breaking at non-whitespace locations.
  6973. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  6974. " \"dddeeeff\"\n"
  6975. " \"f\");",
  6976. format("someFunction(\"aaabbbcc dddeeefff\");",
  6977. getLLVMStyleWithColumns(25)));
  6978. }
  6979. TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
  6980. EXPECT_EQ("\"\\a\"",
  6981. format("\"\\a\"", getLLVMStyleWithColumns(3)));
  6982. EXPECT_EQ("\"\\\"",
  6983. format("\"\\\"", getLLVMStyleWithColumns(2)));
  6984. EXPECT_EQ("\"test\"\n"
  6985. "\"\\n\"",
  6986. format("\"test\\n\"", getLLVMStyleWithColumns(7)));
  6987. EXPECT_EQ("\"tes\\\\\"\n"
  6988. "\"n\"",
  6989. format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
  6990. EXPECT_EQ("\"\\\\\\\\\"\n"
  6991. "\"\\n\"",
  6992. format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
  6993. EXPECT_EQ("\"\\uff01\"",
  6994. format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
  6995. EXPECT_EQ("\"\\uff01\"\n"
  6996. "\"test\"",
  6997. format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
  6998. EXPECT_EQ("\"\\Uff01ff02\"",
  6999. format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
  7000. EXPECT_EQ("\"\\x000000000001\"\n"
  7001. "\"next\"",
  7002. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
  7003. EXPECT_EQ("\"\\x000000000001next\"",
  7004. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
  7005. EXPECT_EQ("\"\\x000000000001\"",
  7006. format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
  7007. EXPECT_EQ("\"test\"\n"
  7008. "\"\\000000\"\n"
  7009. "\"000001\"",
  7010. format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
  7011. EXPECT_EQ("\"test\\000\"\n"
  7012. "\"00000000\"\n"
  7013. "\"1\"",
  7014. format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
  7015. }
  7016. TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
  7017. verifyFormat("void f() {\n"
  7018. " return g() {}\n"
  7019. " void h() {}");
  7020. verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
  7021. "g();\n"
  7022. "}");
  7023. }
  7024. TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
  7025. verifyFormat(
  7026. "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
  7027. }
  7028. TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
  7029. verifyFormat("class X {\n"
  7030. " void f() {\n"
  7031. " }\n"
  7032. "};",
  7033. getLLVMStyleWithColumns(12));
  7034. }
  7035. TEST_F(FormatTest, ConfigurableIndentWidth) {
  7036. FormatStyle EightIndent = getLLVMStyleWithColumns(18);
  7037. EightIndent.IndentWidth = 8;
  7038. EightIndent.ContinuationIndentWidth = 8;
  7039. verifyFormat("void f() {\n"
  7040. " someFunction();\n"
  7041. " if (true) {\n"
  7042. " f();\n"
  7043. " }\n"
  7044. "}",
  7045. EightIndent);
  7046. verifyFormat("class X {\n"
  7047. " void f() {\n"
  7048. " }\n"
  7049. "};",
  7050. EightIndent);
  7051. verifyFormat("int x[] = {\n"
  7052. " call(),\n"
  7053. " call()};",
  7054. EightIndent);
  7055. }
  7056. TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
  7057. verifyFormat("double\n"
  7058. "f();",
  7059. getLLVMStyleWithColumns(8));
  7060. }
  7061. TEST_F(FormatTest, ConfigurableUseOfTab) {
  7062. FormatStyle Tab = getLLVMStyleWithColumns(42);
  7063. Tab.IndentWidth = 8;
  7064. Tab.UseTab = FormatStyle::UT_Always;
  7065. Tab.AlignEscapedNewlinesLeft = true;
  7066. EXPECT_EQ("if (aaaaaaaa && // q\n"
  7067. " bb)\t\t// w\n"
  7068. "\t;",
  7069. format("if (aaaaaaaa &&// q\n"
  7070. "bb)// w\n"
  7071. ";",
  7072. Tab));
  7073. EXPECT_EQ("if (aaa && bbb) // w\n"
  7074. "\t;",
  7075. format("if(aaa&&bbb)// w\n"
  7076. ";",
  7077. Tab));
  7078. verifyFormat("class X {\n"
  7079. "\tvoid f() {\n"
  7080. "\t\tsomeFunction(parameter1,\n"
  7081. "\t\t\t parameter2);\n"
  7082. "\t}\n"
  7083. "};",
  7084. Tab);
  7085. verifyFormat("#define A \\\n"
  7086. "\tvoid f() { \\\n"
  7087. "\t\tsomeFunction( \\\n"
  7088. "\t\t parameter1, \\\n"
  7089. "\t\t parameter2); \\\n"
  7090. "\t}",
  7091. Tab);
  7092. EXPECT_EQ("void f() {\n"
  7093. "\tf();\n"
  7094. "\tg();\n"
  7095. "}",
  7096. format("void f() {\n"
  7097. "\tf();\n"
  7098. "\tg();\n"
  7099. "}",
  7100. 0, 0, Tab));
  7101. EXPECT_EQ("void f() {\n"
  7102. "\tf();\n"
  7103. "\tg();\n"
  7104. "}",
  7105. format("void f() {\n"
  7106. "\tf();\n"
  7107. "\tg();\n"
  7108. "}",
  7109. 16, 0, Tab));
  7110. EXPECT_EQ("void f() {\n"
  7111. " \tf();\n"
  7112. "\tg();\n"
  7113. "}",
  7114. format("void f() {\n"
  7115. " \tf();\n"
  7116. " \tg();\n"
  7117. "}",
  7118. 21, 0, Tab));
  7119. Tab.TabWidth = 4;
  7120. Tab.IndentWidth = 8;
  7121. verifyFormat("class TabWidth4Indent8 {\n"
  7122. "\t\tvoid f() {\n"
  7123. "\t\t\t\tsomeFunction(parameter1,\n"
  7124. "\t\t\t\t\t\t\t parameter2);\n"
  7125. "\t\t}\n"
  7126. "};",
  7127. Tab);
  7128. Tab.TabWidth = 4;
  7129. Tab.IndentWidth = 4;
  7130. verifyFormat("class TabWidth4Indent4 {\n"
  7131. "\tvoid f() {\n"
  7132. "\t\tsomeFunction(parameter1,\n"
  7133. "\t\t\t\t\t parameter2);\n"
  7134. "\t}\n"
  7135. "};",
  7136. Tab);
  7137. Tab.TabWidth = 8;
  7138. Tab.IndentWidth = 4;
  7139. verifyFormat("class TabWidth8Indent4 {\n"
  7140. " void f() {\n"
  7141. "\tsomeFunction(parameter1,\n"
  7142. "\t\t parameter2);\n"
  7143. " }\n"
  7144. "};",
  7145. Tab);
  7146. Tab.TabWidth = 8;
  7147. Tab.IndentWidth = 8;
  7148. EXPECT_EQ("/*\n"
  7149. "\t a\t\tcomment\n"
  7150. "\t in multiple lines\n"
  7151. " */",
  7152. format(" /*\t \t \n"
  7153. " \t \t a\t\tcomment\t \t\n"
  7154. " \t \t in multiple lines\t\n"
  7155. " \t */",
  7156. Tab));
  7157. Tab.UseTab = FormatStyle::UT_ForIndentation;
  7158. verifyFormat("{\n"
  7159. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7160. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7161. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7162. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7163. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7164. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7165. "};",
  7166. Tab);
  7167. verifyFormat("enum A {\n"
  7168. "\ta1, // Force multiple lines\n"
  7169. "\ta2,\n"
  7170. "\ta3\n"
  7171. "};",
  7172. Tab);
  7173. EXPECT_EQ("if (aaaaaaaa && // q\n"
  7174. " bb) // w\n"
  7175. "\t;",
  7176. format("if (aaaaaaaa &&// q\n"
  7177. "bb)// w\n"
  7178. ";",
  7179. Tab));
  7180. verifyFormat("class X {\n"
  7181. "\tvoid f() {\n"
  7182. "\t\tsomeFunction(parameter1,\n"
  7183. "\t\t parameter2);\n"
  7184. "\t}\n"
  7185. "};",
  7186. Tab);
  7187. verifyFormat("{\n"
  7188. "\tQ({\n"
  7189. "\t\tint a;\n"
  7190. "\t\tsomeFunction(aaaaaaaa,\n"
  7191. "\t\t bbbbbbb);\n"
  7192. "\t}, p);\n"
  7193. "}",
  7194. Tab);
  7195. EXPECT_EQ("{\n"
  7196. "\t/* aaaa\n"
  7197. "\t bbbb */\n"
  7198. "}",
  7199. format("{\n"
  7200. "/* aaaa\n"
  7201. " bbbb */\n"
  7202. "}",
  7203. Tab));
  7204. EXPECT_EQ("{\n"
  7205. "\t/*\n"
  7206. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7207. "\t bbbbbbbbbbbbb\n"
  7208. "\t*/\n"
  7209. "}",
  7210. format("{\n"
  7211. "/*\n"
  7212. " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7213. "*/\n"
  7214. "}",
  7215. Tab));
  7216. EXPECT_EQ("{\n"
  7217. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7218. "\t// bbbbbbbbbbbbb\n"
  7219. "}",
  7220. format("{\n"
  7221. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7222. "}",
  7223. Tab));
  7224. EXPECT_EQ("{\n"
  7225. "\t/*\n"
  7226. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7227. "\t bbbbbbbbbbbbb\n"
  7228. "\t*/\n"
  7229. "}",
  7230. format("{\n"
  7231. "\t/*\n"
  7232. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7233. "\t*/\n"
  7234. "}",
  7235. Tab));
  7236. EXPECT_EQ("{\n"
  7237. "\t/*\n"
  7238. "\n"
  7239. "\t*/\n"
  7240. "}",
  7241. format("{\n"
  7242. "\t/*\n"
  7243. "\n"
  7244. "\t*/\n"
  7245. "}",
  7246. Tab));
  7247. EXPECT_EQ("{\n"
  7248. "\t/*\n"
  7249. " asdf\n"
  7250. "\t*/\n"
  7251. "}",
  7252. format("{\n"
  7253. "\t/*\n"
  7254. " asdf\n"
  7255. "\t*/\n"
  7256. "}",
  7257. Tab));
  7258. Tab.UseTab = FormatStyle::UT_Never;
  7259. EXPECT_EQ("/*\n"
  7260. " a\t\tcomment\n"
  7261. " in multiple lines\n"
  7262. " */",
  7263. format(" /*\t \t \n"
  7264. " \t \t a\t\tcomment\t \t\n"
  7265. " \t \t in multiple lines\t\n"
  7266. " \t */",
  7267. Tab));
  7268. EXPECT_EQ("/* some\n"
  7269. " comment */",
  7270. format(" \t \t /* some\n"
  7271. " \t \t comment */",
  7272. Tab));
  7273. EXPECT_EQ("int a; /* some\n"
  7274. " comment */",
  7275. format(" \t \t int a; /* some\n"
  7276. " \t \t comment */",
  7277. Tab));
  7278. EXPECT_EQ("int a; /* some\n"
  7279. "comment */",
  7280. format(" \t \t int\ta; /* some\n"
  7281. " \t \t comment */",
  7282. Tab));
  7283. EXPECT_EQ("f(\"\t\t\"); /* some\n"
  7284. " comment */",
  7285. format(" \t \t f(\"\t\t\"); /* some\n"
  7286. " \t \t comment */",
  7287. Tab));
  7288. EXPECT_EQ("{\n"
  7289. " /*\n"
  7290. " * Comment\n"
  7291. " */\n"
  7292. " int i;\n"
  7293. "}",
  7294. format("{\n"
  7295. "\t/*\n"
  7296. "\t * Comment\n"
  7297. "\t */\n"
  7298. "\t int i;\n"
  7299. "}"));
  7300. }
  7301. TEST_F(FormatTest, CalculatesOriginalColumn) {
  7302. EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7303. "q\"; /* some\n"
  7304. " comment */",
  7305. format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7306. "q\"; /* some\n"
  7307. " comment */",
  7308. getLLVMStyle()));
  7309. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  7310. "/* some\n"
  7311. " comment */",
  7312. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  7313. " /* some\n"
  7314. " comment */",
  7315. getLLVMStyle()));
  7316. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7317. "qqq\n"
  7318. "/* some\n"
  7319. " comment */",
  7320. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7321. "qqq\n"
  7322. " /* some\n"
  7323. " comment */",
  7324. getLLVMStyle()));
  7325. EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7326. "wwww; /* some\n"
  7327. " comment */",
  7328. format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7329. "wwww; /* some\n"
  7330. " comment */",
  7331. getLLVMStyle()));
  7332. }
  7333. TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
  7334. FormatStyle NoSpace = getLLVMStyle();
  7335. NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
  7336. verifyFormat("while(true)\n"
  7337. " continue;", NoSpace);
  7338. verifyFormat("for(;;)\n"
  7339. " continue;", NoSpace);
  7340. verifyFormat("if(true)\n"
  7341. " f();\n"
  7342. "else if(true)\n"
  7343. " f();", NoSpace);
  7344. verifyFormat("do {\n"
  7345. " do_something();\n"
  7346. "} while(something());", NoSpace);
  7347. verifyFormat("switch(x) {\n"
  7348. "default:\n"
  7349. " break;\n"
  7350. "}", NoSpace);
  7351. verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
  7352. verifyFormat("size_t x = sizeof(x);", NoSpace);
  7353. verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
  7354. verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
  7355. verifyFormat("alignas(128) char a[128];", NoSpace);
  7356. verifyFormat("size_t x = alignof(MyType);", NoSpace);
  7357. verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
  7358. verifyFormat("int f() throw(Deprecated);", NoSpace);
  7359. FormatStyle Space = getLLVMStyle();
  7360. Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
  7361. verifyFormat("int f ();", Space);
  7362. verifyFormat("void f (int a, T b) {\n"
  7363. " while (true)\n"
  7364. " continue;\n"
  7365. "}",
  7366. Space);
  7367. verifyFormat("if (true)\n"
  7368. " f ();\n"
  7369. "else if (true)\n"
  7370. " f ();",
  7371. Space);
  7372. verifyFormat("do {\n"
  7373. " do_something ();\n"
  7374. "} while (something ());",
  7375. Space);
  7376. verifyFormat("switch (x) {\n"
  7377. "default:\n"
  7378. " break;\n"
  7379. "}",
  7380. Space);
  7381. verifyFormat("A::A () : a (1) {}", Space);
  7382. verifyFormat("void f () __attribute__ ((asdf));", Space);
  7383. verifyFormat("*(&a + 1);\n"
  7384. "&((&a)[1]);\n"
  7385. "a[(b + c) * d];\n"
  7386. "(((a + 1) * 2) + 3) * 4;",
  7387. Space);
  7388. verifyFormat("#define A(x) x", Space);
  7389. verifyFormat("#define A (x) x", Space);
  7390. verifyFormat("#if defined(x)\n"
  7391. "#endif",
  7392. Space);
  7393. verifyFormat("auto i = std::make_unique<int> (5);", Space);
  7394. verifyFormat("size_t x = sizeof (x);", Space);
  7395. verifyFormat("auto f (int x) -> decltype (x);", Space);
  7396. verifyFormat("int f (T x) noexcept (x.create ());", Space);
  7397. verifyFormat("alignas (128) char a[128];", Space);
  7398. verifyFormat("size_t x = alignof (MyType);", Space);
  7399. verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
  7400. verifyFormat("int f () throw (Deprecated);", Space);
  7401. }
  7402. TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
  7403. FormatStyle Spaces = getLLVMStyle();
  7404. Spaces.SpacesInParentheses = true;
  7405. verifyFormat("call( x, y, z );", Spaces);
  7406. verifyFormat("while ( (bool)1 )\n"
  7407. " continue;", Spaces);
  7408. verifyFormat("for ( ;; )\n"
  7409. " continue;", Spaces);
  7410. verifyFormat("if ( true )\n"
  7411. " f();\n"
  7412. "else if ( true )\n"
  7413. " f();", Spaces);
  7414. verifyFormat("do {\n"
  7415. " do_something( (int)i );\n"
  7416. "} while ( something() );", Spaces);
  7417. verifyFormat("switch ( x ) {\n"
  7418. "default:\n"
  7419. " break;\n"
  7420. "}", Spaces);
  7421. Spaces.SpacesInParentheses = false;
  7422. Spaces.SpacesInCStyleCastParentheses = true;
  7423. verifyFormat("Type *A = ( Type * )P;", Spaces);
  7424. verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
  7425. verifyFormat("x = ( int32 )y;", Spaces);
  7426. verifyFormat("int a = ( int )(2.0f);", Spaces);
  7427. verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
  7428. verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
  7429. verifyFormat("#define x (( int )-1)", Spaces);
  7430. Spaces.SpacesInParentheses = false;
  7431. Spaces.SpaceInEmptyParentheses = true;
  7432. verifyFormat("call(x, y, z);", Spaces);
  7433. verifyFormat("call( )", Spaces);
  7434. // Run the first set of tests again with
  7435. // Spaces.SpacesInParentheses = false,
  7436. // Spaces.SpaceInEmptyParentheses = true and
  7437. // Spaces.SpacesInCStyleCastParentheses = true
  7438. Spaces.SpacesInParentheses = false,
  7439. Spaces.SpaceInEmptyParentheses = true;
  7440. Spaces.SpacesInCStyleCastParentheses = true;
  7441. verifyFormat("call(x, y, z);", Spaces);
  7442. verifyFormat("while (( bool )1)\n"
  7443. " continue;", Spaces);
  7444. verifyFormat("for (;;)\n"
  7445. " continue;", Spaces);
  7446. verifyFormat("if (true)\n"
  7447. " f( );\n"
  7448. "else if (true)\n"
  7449. " f( );", Spaces);
  7450. verifyFormat("do {\n"
  7451. " do_something(( int )i);\n"
  7452. "} while (something( ));", Spaces);
  7453. verifyFormat("switch (x) {\n"
  7454. "default:\n"
  7455. " break;\n"
  7456. "}", Spaces);
  7457. Spaces.SpaceAfterCStyleCast = true;
  7458. verifyFormat("call(x, y, z);", Spaces);
  7459. verifyFormat("while (( bool ) 1)\n"
  7460. " continue;",
  7461. Spaces);
  7462. verifyFormat("for (;;)\n"
  7463. " continue;",
  7464. Spaces);
  7465. verifyFormat("if (true)\n"
  7466. " f( );\n"
  7467. "else if (true)\n"
  7468. " f( );",
  7469. Spaces);
  7470. verifyFormat("do {\n"
  7471. " do_something(( int ) i);\n"
  7472. "} while (something( ));",
  7473. Spaces);
  7474. verifyFormat("switch (x) {\n"
  7475. "default:\n"
  7476. " break;\n"
  7477. "}",
  7478. Spaces);
  7479. Spaces.SpacesInCStyleCastParentheses = false;
  7480. Spaces.SpaceAfterCStyleCast = true;
  7481. verifyFormat("while ((bool) 1)\n"
  7482. " continue;",
  7483. Spaces);
  7484. verifyFormat("do {\n"
  7485. " do_something((int) i);\n"
  7486. "} while (something( ));",
  7487. Spaces);
  7488. }
  7489. TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
  7490. verifyFormat("int a[5];");
  7491. verifyFormat("a[3] += 42;");
  7492. FormatStyle Spaces = getLLVMStyle();
  7493. Spaces.SpacesInSquareBrackets = true;
  7494. // Lambdas unchanged.
  7495. verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
  7496. verifyFormat("return [i, args...] {};", Spaces);
  7497. // Not lambdas.
  7498. verifyFormat("int a[ 5 ];", Spaces);
  7499. verifyFormat("a[ 3 ] += 42;", Spaces);
  7500. verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
  7501. verifyFormat("double &operator[](int i) { return 0; }\n"
  7502. "int i;",
  7503. Spaces);
  7504. verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
  7505. verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
  7506. verifyFormat("int i = (*b)[ a ]->f();", Spaces);
  7507. }
  7508. TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
  7509. verifyFormat("int a = 5;");
  7510. verifyFormat("a += 42;");
  7511. verifyFormat("a or_eq 8;");
  7512. FormatStyle Spaces = getLLVMStyle();
  7513. Spaces.SpaceBeforeAssignmentOperators = false;
  7514. verifyFormat("int a= 5;", Spaces);
  7515. verifyFormat("a+= 42;", Spaces);
  7516. verifyFormat("a or_eq 8;", Spaces);
  7517. }
  7518. TEST_F(FormatTest, LinuxBraceBreaking) {
  7519. FormatStyle LinuxBraceStyle = getLLVMStyle();
  7520. LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
  7521. verifyFormat("namespace a\n"
  7522. "{\n"
  7523. "class A\n"
  7524. "{\n"
  7525. " void f()\n"
  7526. " {\n"
  7527. " if (true) {\n"
  7528. " a();\n"
  7529. " b();\n"
  7530. " }\n"
  7531. " }\n"
  7532. " void g() { return; }\n"
  7533. "};\n"
  7534. "struct B {\n"
  7535. " int x;\n"
  7536. "};\n"
  7537. "}\n",
  7538. LinuxBraceStyle);
  7539. verifyFormat("enum X {\n"
  7540. " Y = 0,\n"
  7541. "}\n",
  7542. LinuxBraceStyle);
  7543. verifyFormat("struct S {\n"
  7544. " int Type;\n"
  7545. " union {\n"
  7546. " int x;\n"
  7547. " double y;\n"
  7548. " } Value;\n"
  7549. " class C\n"
  7550. " {\n"
  7551. " MyFavoriteType Value;\n"
  7552. " } Class;\n"
  7553. "}\n",
  7554. LinuxBraceStyle);
  7555. }
  7556. TEST_F(FormatTest, StroustrupBraceBreaking) {
  7557. FormatStyle StroustrupBraceStyle = getLLVMStyle();
  7558. StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  7559. verifyFormat("namespace a {\n"
  7560. "class A {\n"
  7561. " void f()\n"
  7562. " {\n"
  7563. " if (true) {\n"
  7564. " a();\n"
  7565. " b();\n"
  7566. " }\n"
  7567. " }\n"
  7568. " void g() { return; }\n"
  7569. "};\n"
  7570. "struct B {\n"
  7571. " int x;\n"
  7572. "};\n"
  7573. "}\n",
  7574. StroustrupBraceStyle);
  7575. verifyFormat("void foo()\n"
  7576. "{\n"
  7577. " if (a) {\n"
  7578. " a();\n"
  7579. " }\n"
  7580. " else {\n"
  7581. " b();\n"
  7582. " }\n"
  7583. "}\n",
  7584. StroustrupBraceStyle);
  7585. verifyFormat("#ifdef _DEBUG\n"
  7586. "int foo(int i = 0)\n"
  7587. "#else\n"
  7588. "int foo(int i = 5)\n"
  7589. "#endif\n"
  7590. "{\n"
  7591. " return i;\n"
  7592. "}",
  7593. StroustrupBraceStyle);
  7594. verifyFormat("void foo() {}\n"
  7595. "void bar()\n"
  7596. "#ifdef _DEBUG\n"
  7597. "{\n"
  7598. " foo();\n"
  7599. "}\n"
  7600. "#else\n"
  7601. "{\n"
  7602. "}\n"
  7603. "#endif",
  7604. StroustrupBraceStyle);
  7605. verifyFormat("void foobar() { int i = 5; }\n"
  7606. "#ifdef _DEBUG\n"
  7607. "void bar() {}\n"
  7608. "#else\n"
  7609. "void bar() { foobar(); }\n"
  7610. "#endif",
  7611. StroustrupBraceStyle);
  7612. }
  7613. TEST_F(FormatTest, AllmanBraceBreaking) {
  7614. FormatStyle AllmanBraceStyle = getLLVMStyle();
  7615. AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
  7616. verifyFormat("namespace a\n"
  7617. "{\n"
  7618. "class A\n"
  7619. "{\n"
  7620. " void f()\n"
  7621. " {\n"
  7622. " if (true)\n"
  7623. " {\n"
  7624. " a();\n"
  7625. " b();\n"
  7626. " }\n"
  7627. " }\n"
  7628. " void g() { return; }\n"
  7629. "};\n"
  7630. "struct B\n"
  7631. "{\n"
  7632. " int x;\n"
  7633. "};\n"
  7634. "}",
  7635. AllmanBraceStyle);
  7636. verifyFormat("void f()\n"
  7637. "{\n"
  7638. " if (true)\n"
  7639. " {\n"
  7640. " a();\n"
  7641. " }\n"
  7642. " else if (false)\n"
  7643. " {\n"
  7644. " b();\n"
  7645. " }\n"
  7646. " else\n"
  7647. " {\n"
  7648. " c();\n"
  7649. " }\n"
  7650. "}\n",
  7651. AllmanBraceStyle);
  7652. verifyFormat("void f()\n"
  7653. "{\n"
  7654. " for (int i = 0; i < 10; ++i)\n"
  7655. " {\n"
  7656. " a();\n"
  7657. " }\n"
  7658. " while (false)\n"
  7659. " {\n"
  7660. " b();\n"
  7661. " }\n"
  7662. " do\n"
  7663. " {\n"
  7664. " c();\n"
  7665. " } while (false)\n"
  7666. "}\n",
  7667. AllmanBraceStyle);
  7668. verifyFormat("void f(int a)\n"
  7669. "{\n"
  7670. " switch (a)\n"
  7671. " {\n"
  7672. " case 0:\n"
  7673. " break;\n"
  7674. " case 1:\n"
  7675. " {\n"
  7676. " break;\n"
  7677. " }\n"
  7678. " case 2:\n"
  7679. " {\n"
  7680. " }\n"
  7681. " break;\n"
  7682. " default:\n"
  7683. " break;\n"
  7684. " }\n"
  7685. "}\n",
  7686. AllmanBraceStyle);
  7687. verifyFormat("enum X\n"
  7688. "{\n"
  7689. " Y = 0,\n"
  7690. "}\n",
  7691. AllmanBraceStyle);
  7692. verifyFormat("enum X\n"
  7693. "{\n"
  7694. " Y = 0\n"
  7695. "}\n",
  7696. AllmanBraceStyle);
  7697. verifyFormat("@interface BSApplicationController ()\n"
  7698. "{\n"
  7699. "@private\n"
  7700. " id _extraIvar;\n"
  7701. "}\n"
  7702. "@end\n",
  7703. AllmanBraceStyle);
  7704. verifyFormat("#ifdef _DEBUG\n"
  7705. "int foo(int i = 0)\n"
  7706. "#else\n"
  7707. "int foo(int i = 5)\n"
  7708. "#endif\n"
  7709. "{\n"
  7710. " return i;\n"
  7711. "}",
  7712. AllmanBraceStyle);
  7713. verifyFormat("void foo() {}\n"
  7714. "void bar()\n"
  7715. "#ifdef _DEBUG\n"
  7716. "{\n"
  7717. " foo();\n"
  7718. "}\n"
  7719. "#else\n"
  7720. "{\n"
  7721. "}\n"
  7722. "#endif",
  7723. AllmanBraceStyle);
  7724. verifyFormat("void foobar() { int i = 5; }\n"
  7725. "#ifdef _DEBUG\n"
  7726. "void bar() {}\n"
  7727. "#else\n"
  7728. "void bar() { foobar(); }\n"
  7729. "#endif",
  7730. AllmanBraceStyle);
  7731. // This shouldn't affect ObjC blocks..
  7732. verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
  7733. " // ...\n"
  7734. " int i;\n"
  7735. "}];",
  7736. AllmanBraceStyle);
  7737. verifyFormat("void (^block)(void) = ^{\n"
  7738. " // ...\n"
  7739. " int i;\n"
  7740. "};",
  7741. AllmanBraceStyle);
  7742. // .. or dict literals.
  7743. verifyFormat("void f()\n"
  7744. "{\n"
  7745. " [object someMethod:@{ @\"a\" : @\"b\" }];\n"
  7746. "}",
  7747. AllmanBraceStyle);
  7748. verifyFormat("int f()\n"
  7749. "{ // comment\n"
  7750. " return 42;\n"
  7751. "}",
  7752. AllmanBraceStyle);
  7753. AllmanBraceStyle.ColumnLimit = 19;
  7754. verifyFormat("void f() { int i; }", AllmanBraceStyle);
  7755. AllmanBraceStyle.ColumnLimit = 18;
  7756. verifyFormat("void f()\n"
  7757. "{\n"
  7758. " int i;\n"
  7759. "}",
  7760. AllmanBraceStyle);
  7761. AllmanBraceStyle.ColumnLimit = 80;
  7762. FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
  7763. BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
  7764. BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
  7765. verifyFormat("void f(bool b)\n"
  7766. "{\n"
  7767. " if (b)\n"
  7768. " {\n"
  7769. " return;\n"
  7770. " }\n"
  7771. "}\n",
  7772. BreakBeforeBraceShortIfs);
  7773. verifyFormat("void f(bool b)\n"
  7774. "{\n"
  7775. " if (b) return;\n"
  7776. "}\n",
  7777. BreakBeforeBraceShortIfs);
  7778. verifyFormat("void f(bool b)\n"
  7779. "{\n"
  7780. " while (b)\n"
  7781. " {\n"
  7782. " return;\n"
  7783. " }\n"
  7784. "}\n",
  7785. BreakBeforeBraceShortIfs);
  7786. }
  7787. TEST_F(FormatTest, GNUBraceBreaking) {
  7788. FormatStyle GNUBraceStyle = getLLVMStyle();
  7789. GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
  7790. verifyFormat("namespace a\n"
  7791. "{\n"
  7792. "class A\n"
  7793. "{\n"
  7794. " void f()\n"
  7795. " {\n"
  7796. " int a;\n"
  7797. " {\n"
  7798. " int b;\n"
  7799. " }\n"
  7800. " if (true)\n"
  7801. " {\n"
  7802. " a();\n"
  7803. " b();\n"
  7804. " }\n"
  7805. " }\n"
  7806. " void g() { return; }\n"
  7807. "}\n"
  7808. "}",
  7809. GNUBraceStyle);
  7810. verifyFormat("void f()\n"
  7811. "{\n"
  7812. " if (true)\n"
  7813. " {\n"
  7814. " a();\n"
  7815. " }\n"
  7816. " else if (false)\n"
  7817. " {\n"
  7818. " b();\n"
  7819. " }\n"
  7820. " else\n"
  7821. " {\n"
  7822. " c();\n"
  7823. " }\n"
  7824. "}\n",
  7825. GNUBraceStyle);
  7826. verifyFormat("void f()\n"
  7827. "{\n"
  7828. " for (int i = 0; i < 10; ++i)\n"
  7829. " {\n"
  7830. " a();\n"
  7831. " }\n"
  7832. " while (false)\n"
  7833. " {\n"
  7834. " b();\n"
  7835. " }\n"
  7836. " do\n"
  7837. " {\n"
  7838. " c();\n"
  7839. " }\n"
  7840. " while (false);\n"
  7841. "}\n",
  7842. GNUBraceStyle);
  7843. verifyFormat("void f(int a)\n"
  7844. "{\n"
  7845. " switch (a)\n"
  7846. " {\n"
  7847. " case 0:\n"
  7848. " break;\n"
  7849. " case 1:\n"
  7850. " {\n"
  7851. " break;\n"
  7852. " }\n"
  7853. " case 2:\n"
  7854. " {\n"
  7855. " }\n"
  7856. " break;\n"
  7857. " default:\n"
  7858. " break;\n"
  7859. " }\n"
  7860. "}\n",
  7861. GNUBraceStyle);
  7862. verifyFormat("enum X\n"
  7863. "{\n"
  7864. " Y = 0,\n"
  7865. "}\n",
  7866. GNUBraceStyle);
  7867. verifyFormat("@interface BSApplicationController ()\n"
  7868. "{\n"
  7869. "@private\n"
  7870. " id _extraIvar;\n"
  7871. "}\n"
  7872. "@end\n",
  7873. GNUBraceStyle);
  7874. verifyFormat("#ifdef _DEBUG\n"
  7875. "int foo(int i = 0)\n"
  7876. "#else\n"
  7877. "int foo(int i = 5)\n"
  7878. "#endif\n"
  7879. "{\n"
  7880. " return i;\n"
  7881. "}",
  7882. GNUBraceStyle);
  7883. verifyFormat("void foo() {}\n"
  7884. "void bar()\n"
  7885. "#ifdef _DEBUG\n"
  7886. "{\n"
  7887. " foo();\n"
  7888. "}\n"
  7889. "#else\n"
  7890. "{\n"
  7891. "}\n"
  7892. "#endif",
  7893. GNUBraceStyle);
  7894. verifyFormat("void foobar() { int i = 5; }\n"
  7895. "#ifdef _DEBUG\n"
  7896. "void bar() {}\n"
  7897. "#else\n"
  7898. "void bar() { foobar(); }\n"
  7899. "#endif",
  7900. GNUBraceStyle);
  7901. }
  7902. TEST_F(FormatTest, CatchExceptionReferenceBinding) {
  7903. verifyFormat("void f() {\n"
  7904. " try {\n"
  7905. " } catch (const Exception &e) {\n"
  7906. " }\n"
  7907. "}\n",
  7908. getLLVMStyle());
  7909. }
  7910. TEST_F(FormatTest, UnderstandsPragmas) {
  7911. verifyFormat("#pragma omp reduction(| : var)");
  7912. verifyFormat("#pragma omp reduction(+ : var)");
  7913. EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
  7914. "(including parentheses).",
  7915. format("#pragma mark Any non-hyphenated or hyphenated string "
  7916. "(including parentheses)."));
  7917. }
  7918. #define EXPECT_ALL_STYLES_EQUAL(Styles) \
  7919. for (size_t i = 1; i < Styles.size(); ++i) \
  7920. EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " \
  7921. << Styles.size() \
  7922. << " differs from Style #0"
  7923. TEST_F(FormatTest, GetsPredefinedStyleByName) {
  7924. SmallVector<FormatStyle, 3> Styles;
  7925. Styles.resize(3);
  7926. Styles[0] = getLLVMStyle();
  7927. EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
  7928. EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
  7929. EXPECT_ALL_STYLES_EQUAL(Styles);
  7930. Styles[0] = getGoogleStyle();
  7931. EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
  7932. EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
  7933. EXPECT_ALL_STYLES_EQUAL(Styles);
  7934. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  7935. EXPECT_TRUE(
  7936. getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
  7937. EXPECT_TRUE(
  7938. getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
  7939. EXPECT_ALL_STYLES_EQUAL(Styles);
  7940. Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
  7941. EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
  7942. EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
  7943. EXPECT_ALL_STYLES_EQUAL(Styles);
  7944. Styles[0] = getMozillaStyle();
  7945. EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
  7946. EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
  7947. EXPECT_ALL_STYLES_EQUAL(Styles);
  7948. Styles[0] = getWebKitStyle();
  7949. EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
  7950. EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
  7951. EXPECT_ALL_STYLES_EQUAL(Styles);
  7952. Styles[0] = getGNUStyle();
  7953. EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
  7954. EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
  7955. EXPECT_ALL_STYLES_EQUAL(Styles);
  7956. EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
  7957. }
  7958. TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
  7959. SmallVector<FormatStyle, 8> Styles;
  7960. Styles.resize(2);
  7961. Styles[0] = getGoogleStyle();
  7962. Styles[1] = getLLVMStyle();
  7963. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  7964. EXPECT_ALL_STYLES_EQUAL(Styles);
  7965. Styles.resize(5);
  7966. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  7967. Styles[1] = getLLVMStyle();
  7968. Styles[1].Language = FormatStyle::LK_JavaScript;
  7969. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  7970. Styles[2] = getLLVMStyle();
  7971. Styles[2].Language = FormatStyle::LK_JavaScript;
  7972. EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
  7973. "BasedOnStyle: Google",
  7974. &Styles[2]).value());
  7975. Styles[3] = getLLVMStyle();
  7976. Styles[3].Language = FormatStyle::LK_JavaScript;
  7977. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
  7978. "Language: JavaScript",
  7979. &Styles[3]).value());
  7980. Styles[4] = getLLVMStyle();
  7981. Styles[4].Language = FormatStyle::LK_JavaScript;
  7982. EXPECT_EQ(0, parseConfiguration("---\n"
  7983. "BasedOnStyle: LLVM\n"
  7984. "IndentWidth: 123\n"
  7985. "---\n"
  7986. "BasedOnStyle: Google\n"
  7987. "Language: JavaScript",
  7988. &Styles[4]).value());
  7989. EXPECT_ALL_STYLES_EQUAL(Styles);
  7990. }
  7991. #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \
  7992. Style.FIELD = false; \
  7993. EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \
  7994. EXPECT_TRUE(Style.FIELD); \
  7995. EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \
  7996. EXPECT_FALSE(Style.FIELD);
  7997. #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
  7998. #define CHECK_PARSE(TEXT, FIELD, VALUE) \
  7999. EXPECT_NE(VALUE, Style.FIELD); \
  8000. EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \
  8001. EXPECT_EQ(VALUE, Style.FIELD)
  8002. TEST_F(FormatTest, ParsesConfigurationBools) {
  8003. FormatStyle Style = {};
  8004. Style.Language = FormatStyle::LK_Cpp;
  8005. CHECK_PARSE_BOOL(AlignAfterOpenBracket);
  8006. CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
  8007. CHECK_PARSE_BOOL(AlignOperands);
  8008. CHECK_PARSE_BOOL(AlignTrailingComments);
  8009. CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
  8010. CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
  8011. CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
  8012. CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
  8013. CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
  8014. CHECK_PARSE_BOOL(AlwaysBreakAfterDefinitionReturnType);
  8015. CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
  8016. CHECK_PARSE_BOOL(BinPackParameters);
  8017. CHECK_PARSE_BOOL(BinPackArguments);
  8018. CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
  8019. CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
  8020. CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
  8021. CHECK_PARSE_BOOL(DerivePointerAlignment);
  8022. CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
  8023. CHECK_PARSE_BOOL(IndentCaseLabels);
  8024. CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
  8025. CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
  8026. CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
  8027. CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
  8028. CHECK_PARSE_BOOL(Cpp11BracedListStyle);
  8029. CHECK_PARSE_BOOL(SpacesInParentheses);
  8030. CHECK_PARSE_BOOL(SpacesInSquareBrackets);
  8031. CHECK_PARSE_BOOL(SpacesInAngles);
  8032. CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
  8033. CHECK_PARSE_BOOL(SpacesInContainerLiterals);
  8034. CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
  8035. CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
  8036. CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
  8037. }
  8038. #undef CHECK_PARSE_BOOL
  8039. TEST_F(FormatTest, ParsesConfiguration) {
  8040. FormatStyle Style = {};
  8041. Style.Language = FormatStyle::LK_Cpp;
  8042. CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
  8043. CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
  8044. ConstructorInitializerIndentWidth, 1234u);
  8045. CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
  8046. CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
  8047. CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
  8048. CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
  8049. PenaltyBreakBeforeFirstCallParameter, 1234u);
  8050. CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
  8051. CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
  8052. PenaltyReturnTypeOnItsOwnLine, 1234u);
  8053. CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
  8054. SpacesBeforeTrailingComments, 1234u);
  8055. CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
  8056. CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
  8057. Style.PointerAlignment = FormatStyle::PAS_Middle;
  8058. CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
  8059. FormatStyle::PAS_Left);
  8060. CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
  8061. FormatStyle::PAS_Right);
  8062. CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
  8063. FormatStyle::PAS_Middle);
  8064. // For backward compatibility:
  8065. CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
  8066. FormatStyle::PAS_Left);
  8067. CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
  8068. FormatStyle::PAS_Right);
  8069. CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
  8070. FormatStyle::PAS_Middle);
  8071. Style.Standard = FormatStyle::LS_Auto;
  8072. CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
  8073. CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
  8074. CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
  8075. CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
  8076. CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
  8077. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  8078. CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
  8079. BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
  8080. CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
  8081. FormatStyle::BOS_None);
  8082. CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
  8083. FormatStyle::BOS_All);
  8084. // For backward compatibility:
  8085. CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
  8086. FormatStyle::BOS_None);
  8087. CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
  8088. FormatStyle::BOS_All);
  8089. Style.UseTab = FormatStyle::UT_ForIndentation;
  8090. CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
  8091. CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
  8092. CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
  8093. // For backward compatibility:
  8094. CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
  8095. CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
  8096. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  8097. CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
  8098. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
  8099. CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
  8100. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
  8101. CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
  8102. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
  8103. CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
  8104. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
  8105. // For backward compatibility:
  8106. CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
  8107. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
  8108. CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
  8109. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
  8110. Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
  8111. CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
  8112. FormatStyle::SBPO_Never);
  8113. CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
  8114. FormatStyle::SBPO_Always);
  8115. CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
  8116. FormatStyle::SBPO_ControlStatements);
  8117. // For backward compatibility:
  8118. CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
  8119. FormatStyle::SBPO_Never);
  8120. CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
  8121. FormatStyle::SBPO_ControlStatements);
  8122. Style.ColumnLimit = 123;
  8123. FormatStyle BaseStyle = getLLVMStyle();
  8124. CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
  8125. CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
  8126. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  8127. CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
  8128. FormatStyle::BS_Attach);
  8129. CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
  8130. FormatStyle::BS_Linux);
  8131. CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
  8132. FormatStyle::BS_Stroustrup);
  8133. CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
  8134. FormatStyle::BS_Allman);
  8135. CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
  8136. Style.NamespaceIndentation = FormatStyle::NI_All;
  8137. CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
  8138. FormatStyle::NI_None);
  8139. CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
  8140. FormatStyle::NI_Inner);
  8141. CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
  8142. FormatStyle::NI_All);
  8143. Style.ForEachMacros.clear();
  8144. std::vector<std::string> BoostForeach;
  8145. BoostForeach.push_back("BOOST_FOREACH");
  8146. CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
  8147. std::vector<std::string> BoostAndQForeach;
  8148. BoostAndQForeach.push_back("BOOST_FOREACH");
  8149. BoostAndQForeach.push_back("Q_FOREACH");
  8150. CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
  8151. BoostAndQForeach);
  8152. }
  8153. TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
  8154. FormatStyle Style = {};
  8155. Style.Language = FormatStyle::LK_Cpp;
  8156. CHECK_PARSE("Language: Cpp\n"
  8157. "IndentWidth: 12",
  8158. IndentWidth, 12u);
  8159. EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
  8160. "IndentWidth: 34",
  8161. &Style),
  8162. ParseError::Unsuitable);
  8163. EXPECT_EQ(12u, Style.IndentWidth);
  8164. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  8165. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  8166. Style.Language = FormatStyle::LK_JavaScript;
  8167. CHECK_PARSE("Language: JavaScript\n"
  8168. "IndentWidth: 12",
  8169. IndentWidth, 12u);
  8170. CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
  8171. EXPECT_EQ(parseConfiguration("Language: Cpp\n"
  8172. "IndentWidth: 34",
  8173. &Style),
  8174. ParseError::Unsuitable);
  8175. EXPECT_EQ(23u, Style.IndentWidth);
  8176. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  8177. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  8178. CHECK_PARSE("BasedOnStyle: LLVM\n"
  8179. "IndentWidth: 67",
  8180. IndentWidth, 67u);
  8181. CHECK_PARSE("---\n"
  8182. "Language: JavaScript\n"
  8183. "IndentWidth: 12\n"
  8184. "---\n"
  8185. "Language: Cpp\n"
  8186. "IndentWidth: 34\n"
  8187. "...\n",
  8188. IndentWidth, 12u);
  8189. Style.Language = FormatStyle::LK_Cpp;
  8190. CHECK_PARSE("---\n"
  8191. "Language: JavaScript\n"
  8192. "IndentWidth: 12\n"
  8193. "---\n"
  8194. "Language: Cpp\n"
  8195. "IndentWidth: 34\n"
  8196. "...\n",
  8197. IndentWidth, 34u);
  8198. CHECK_PARSE("---\n"
  8199. "IndentWidth: 78\n"
  8200. "---\n"
  8201. "Language: JavaScript\n"
  8202. "IndentWidth: 56\n"
  8203. "...\n",
  8204. IndentWidth, 78u);
  8205. Style.ColumnLimit = 123;
  8206. Style.IndentWidth = 234;
  8207. Style.BreakBeforeBraces = FormatStyle::BS_Linux;
  8208. Style.TabWidth = 345;
  8209. EXPECT_FALSE(parseConfiguration("---\n"
  8210. "IndentWidth: 456\n"
  8211. "BreakBeforeBraces: Allman\n"
  8212. "---\n"
  8213. "Language: JavaScript\n"
  8214. "IndentWidth: 111\n"
  8215. "TabWidth: 111\n"
  8216. "---\n"
  8217. "Language: Cpp\n"
  8218. "BreakBeforeBraces: Stroustrup\n"
  8219. "TabWidth: 789\n"
  8220. "...\n",
  8221. &Style));
  8222. EXPECT_EQ(123u, Style.ColumnLimit);
  8223. EXPECT_EQ(456u, Style.IndentWidth);
  8224. EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
  8225. EXPECT_EQ(789u, Style.TabWidth);
  8226. EXPECT_EQ(parseConfiguration("---\n"
  8227. "Language: JavaScript\n"
  8228. "IndentWidth: 56\n"
  8229. "---\n"
  8230. "IndentWidth: 78\n"
  8231. "...\n",
  8232. &Style),
  8233. ParseError::Error);
  8234. EXPECT_EQ(parseConfiguration("---\n"
  8235. "Language: JavaScript\n"
  8236. "IndentWidth: 56\n"
  8237. "---\n"
  8238. "Language: JavaScript\n"
  8239. "IndentWidth: 78\n"
  8240. "...\n",
  8241. &Style),
  8242. ParseError::Error);
  8243. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  8244. }
  8245. #undef CHECK_PARSE
  8246. TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
  8247. FormatStyle Style = {};
  8248. Style.Language = FormatStyle::LK_JavaScript;
  8249. Style.BreakBeforeTernaryOperators = true;
  8250. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
  8251. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  8252. Style.BreakBeforeTernaryOperators = true;
  8253. EXPECT_EQ(0, parseConfiguration("---\n"
  8254. "BasedOnStyle: Google\n"
  8255. "---\n"
  8256. "Language: JavaScript\n"
  8257. "IndentWidth: 76\n"
  8258. "...\n", &Style).value());
  8259. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  8260. EXPECT_EQ(76u, Style.IndentWidth);
  8261. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  8262. }
  8263. TEST_F(FormatTest, ConfigurationRoundTripTest) {
  8264. FormatStyle Style = getLLVMStyle();
  8265. std::string YAML = configurationAsText(Style);
  8266. FormatStyle ParsedStyle = {};
  8267. ParsedStyle.Language = FormatStyle::LK_Cpp;
  8268. EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
  8269. EXPECT_EQ(Style, ParsedStyle);
  8270. }
  8271. TEST_F(FormatTest, WorksFor8bitEncodings) {
  8272. EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
  8273. "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
  8274. "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
  8275. "\"\xef\xee\xf0\xf3...\"",
  8276. format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
  8277. "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
  8278. "\xef\xee\xf0\xf3...\"",
  8279. getLLVMStyleWithColumns(12)));
  8280. }
  8281. TEST_F(FormatTest, HandlesUTF8BOM) {
  8282. EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
  8283. EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
  8284. format("\xef\xbb\xbf#include <iostream>"));
  8285. EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
  8286. format("\xef\xbb\xbf\n#include <iostream>"));
  8287. }
  8288. // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
  8289. #if !defined(_MSC_VER)
  8290. TEST_F(FormatTest, CountsUTF8CharactersProperly) {
  8291. verifyFormat("\"Однажды в студёную зимнюю пору...\"",
  8292. getLLVMStyleWithColumns(35));
  8293. verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
  8294. getLLVMStyleWithColumns(31));
  8295. verifyFormat("// Однажды в студёную зимнюю пору...",
  8296. getLLVMStyleWithColumns(36));
  8297. verifyFormat("// 一 二 三 四 五 六 七 八 九 十",
  8298. getLLVMStyleWithColumns(32));
  8299. verifyFormat("/* Однажды в студёную зимнюю пору... */",
  8300. getLLVMStyleWithColumns(39));
  8301. verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
  8302. getLLVMStyleWithColumns(35));
  8303. }
  8304. TEST_F(FormatTest, SplitsUTF8Strings) {
  8305. // Non-printable characters' width is currently considered to be the length in
  8306. // bytes in UTF8. The characters can be displayed in very different manner
  8307. // (zero-width, single width with a substitution glyph, expanded to their code
  8308. // (e.g. "<8d>"), so there's no single correct way to handle them.
  8309. EXPECT_EQ("\"aaaaÄ\"\n"
  8310. "\"\xc2\x8d\";",
  8311. format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  8312. EXPECT_EQ("\"aaaaaaaÄ\"\n"
  8313. "\"\xc2\x8d\";",
  8314. format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  8315. EXPECT_EQ(
  8316. "\"Однажды, в \"\n"
  8317. "\"студёную \"\n"
  8318. "\"зимнюю \"\n"
  8319. "\"пору,\"",
  8320. format("\"Однажды, в студёную зимнюю пору,\"",
  8321. getLLVMStyleWithColumns(13)));
  8322. EXPECT_EQ("\"一 二 三 \"\n"
  8323. "\"四 五六 \"\n"
  8324. "\"七 八 九 \"\n"
  8325. "\"十\"",
  8326. format("\"一 二 三 四 五六 七 八 九 十\"",
  8327. getLLVMStyleWithColumns(11)));
  8328. EXPECT_EQ("\"一\t二 \"\n"
  8329. "\"\t三 \"\n"
  8330. "\"四 五\t六 \"\n"
  8331. "\"\t七 \"\n"
  8332. "\"八九十\tqq\"",
  8333. format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
  8334. getLLVMStyleWithColumns(11)));
  8335. }
  8336. TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
  8337. EXPECT_EQ("const char *sssss =\n"
  8338. " \"一二三四五六七八\\\n"
  8339. " 九 十\";",
  8340. format("const char *sssss = \"一二三四五六七八\\\n"
  8341. " 九 十\";",
  8342. getLLVMStyleWithColumns(30)));
  8343. }
  8344. TEST_F(FormatTest, SplitsUTF8LineComments) {
  8345. EXPECT_EQ("// aaaaÄ\xc2\x8d",
  8346. format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
  8347. EXPECT_EQ("// Я из лесу\n"
  8348. "// вышел; был\n"
  8349. "// сильный\n"
  8350. "// мороз.",
  8351. format("// Я из лесу вышел; был сильный мороз.",
  8352. getLLVMStyleWithColumns(13)));
  8353. EXPECT_EQ("// 一二三\n"
  8354. "// 四五六七\n"
  8355. "// 八 九\n"
  8356. "// 十",
  8357. format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9)));
  8358. }
  8359. TEST_F(FormatTest, SplitsUTF8BlockComments) {
  8360. EXPECT_EQ("/* Гляжу,\n"
  8361. " * поднимается\n"
  8362. " * медленно в\n"
  8363. " * гору\n"
  8364. " * Лошадка,\n"
  8365. " * везущая\n"
  8366. " * хворосту\n"
  8367. " * воз. */",
  8368. format("/* Гляжу, поднимается медленно в гору\n"
  8369. " * Лошадка, везущая хворосту воз. */",
  8370. getLLVMStyleWithColumns(13)));
  8371. EXPECT_EQ(
  8372. "/* 一二三\n"
  8373. " * 四五六七\n"
  8374. " * 八 九\n"
  8375. " * 十 */",
  8376. format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9)));
  8377. EXPECT_EQ("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
  8378. " * 𝕓𝕪𝕥𝕖\n"
  8379. " * 𝖀𝕿𝕱-𝟠 */",
  8380. format("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12)));
  8381. }
  8382. #endif // _MSC_VER
  8383. TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
  8384. FormatStyle Style = getLLVMStyle();
  8385. Style.ConstructorInitializerIndentWidth = 4;
  8386. verifyFormat(
  8387. "SomeClass::Constructor()\n"
  8388. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  8389. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  8390. Style);
  8391. Style.ConstructorInitializerIndentWidth = 2;
  8392. verifyFormat(
  8393. "SomeClass::Constructor()\n"
  8394. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  8395. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  8396. Style);
  8397. Style.ConstructorInitializerIndentWidth = 0;
  8398. verifyFormat(
  8399. "SomeClass::Constructor()\n"
  8400. ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  8401. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  8402. Style);
  8403. }
  8404. TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
  8405. FormatStyle Style = getLLVMStyle();
  8406. Style.BreakConstructorInitializersBeforeComma = true;
  8407. Style.ConstructorInitializerIndentWidth = 4;
  8408. verifyFormat("SomeClass::Constructor()\n"
  8409. " : a(a)\n"
  8410. " , b(b)\n"
  8411. " , c(c) {}",
  8412. Style);
  8413. verifyFormat("SomeClass::Constructor()\n"
  8414. " : a(a) {}",
  8415. Style);
  8416. Style.ColumnLimit = 0;
  8417. verifyFormat("SomeClass::Constructor()\n"
  8418. " : a(a) {}",
  8419. Style);
  8420. verifyFormat("SomeClass::Constructor()\n"
  8421. " : a(a)\n"
  8422. " , b(b)\n"
  8423. " , c(c) {}",
  8424. Style);
  8425. verifyFormat("SomeClass::Constructor()\n"
  8426. " : a(a) {\n"
  8427. " foo();\n"
  8428. " bar();\n"
  8429. "}",
  8430. Style);
  8431. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  8432. verifyFormat("SomeClass::Constructor()\n"
  8433. " : a(a)\n"
  8434. " , b(b)\n"
  8435. " , c(c) {\n}",
  8436. Style);
  8437. verifyFormat("SomeClass::Constructor()\n"
  8438. " : a(a) {\n}",
  8439. Style);
  8440. Style.ColumnLimit = 80;
  8441. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
  8442. Style.ConstructorInitializerIndentWidth = 2;
  8443. verifyFormat("SomeClass::Constructor()\n"
  8444. " : a(a)\n"
  8445. " , b(b)\n"
  8446. " , c(c) {}",
  8447. Style);
  8448. Style.ConstructorInitializerIndentWidth = 0;
  8449. verifyFormat("SomeClass::Constructor()\n"
  8450. ": a(a)\n"
  8451. ", b(b)\n"
  8452. ", c(c) {}",
  8453. Style);
  8454. Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  8455. Style.ConstructorInitializerIndentWidth = 4;
  8456. verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
  8457. verifyFormat(
  8458. "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
  8459. Style);
  8460. verifyFormat(
  8461. "SomeClass::Constructor()\n"
  8462. " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
  8463. Style);
  8464. Style.ConstructorInitializerIndentWidth = 4;
  8465. Style.ColumnLimit = 60;
  8466. verifyFormat("SomeClass::Constructor()\n"
  8467. " : aaaaaaaa(aaaaaaaa)\n"
  8468. " , aaaaaaaa(aaaaaaaa)\n"
  8469. " , aaaaaaaa(aaaaaaaa) {}",
  8470. Style);
  8471. }
  8472. TEST_F(FormatTest, Destructors) {
  8473. verifyFormat("void F(int &i) { i.~int(); }");
  8474. verifyFormat("void F(int &i) { i->~int(); }");
  8475. }
  8476. TEST_F(FormatTest, FormatsWithWebKitStyle) {
  8477. FormatStyle Style = getWebKitStyle();
  8478. // Don't indent in outer namespaces.
  8479. verifyFormat("namespace outer {\n"
  8480. "int i;\n"
  8481. "namespace inner {\n"
  8482. " int i;\n"
  8483. "} // namespace inner\n"
  8484. "} // namespace outer\n"
  8485. "namespace other_outer {\n"
  8486. "int i;\n"
  8487. "}",
  8488. Style);
  8489. // Don't indent case labels.
  8490. verifyFormat("switch (variable) {\n"
  8491. "case 1:\n"
  8492. "case 2:\n"
  8493. " doSomething();\n"
  8494. " break;\n"
  8495. "default:\n"
  8496. " ++variable;\n"
  8497. "}",
  8498. Style);
  8499. // Wrap before binary operators.
  8500. EXPECT_EQ(
  8501. "void f()\n"
  8502. "{\n"
  8503. " if (aaaaaaaaaaaaaaaa\n"
  8504. " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
  8505. " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  8506. " return;\n"
  8507. "}",
  8508. format(
  8509. "void f() {\n"
  8510. "if (aaaaaaaaaaaaaaaa\n"
  8511. "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
  8512. "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  8513. "return;\n"
  8514. "}",
  8515. Style));
  8516. // Allow functions on a single line.
  8517. verifyFormat("void f() { return; }", Style);
  8518. // Constructor initializers are formatted one per line with the "," on the
  8519. // new line.
  8520. verifyFormat("Constructor()\n"
  8521. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  8522. " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
  8523. " aaaaaaaaaaaaaa)\n"
  8524. " , aaaaaaaaaaaaaaaaaaaaaaa()\n"
  8525. "{\n"
  8526. "}",
  8527. Style);
  8528. verifyFormat("SomeClass::Constructor()\n"
  8529. " : a(a)\n"
  8530. "{\n"
  8531. "}",
  8532. Style);
  8533. EXPECT_EQ("SomeClass::Constructor()\n"
  8534. " : a(a)\n"
  8535. "{\n"
  8536. "}",
  8537. format("SomeClass::Constructor():a(a){}", Style));
  8538. verifyFormat("SomeClass::Constructor()\n"
  8539. " : a(a)\n"
  8540. " , b(b)\n"
  8541. " , c(c)\n"
  8542. "{\n"
  8543. "}", Style);
  8544. verifyFormat("SomeClass::Constructor()\n"
  8545. " : a(a)\n"
  8546. "{\n"
  8547. " foo();\n"
  8548. " bar();\n"
  8549. "}",
  8550. Style);
  8551. // Access specifiers should be aligned left.
  8552. verifyFormat("class C {\n"
  8553. "public:\n"
  8554. " int i;\n"
  8555. "};",
  8556. Style);
  8557. // Do not align comments.
  8558. verifyFormat("int a; // Do not\n"
  8559. "double b; // align comments.",
  8560. Style);
  8561. // Do not align operands.
  8562. EXPECT_EQ("ASSERT(aaaa\n"
  8563. " || bbbb);",
  8564. format("ASSERT ( aaaa\n||bbbb);", Style));
  8565. // Accept input's line breaks.
  8566. EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
  8567. " || bbbbbbbbbbbbbbb) {\n"
  8568. " i++;\n"
  8569. "}",
  8570. format("if (aaaaaaaaaaaaaaa\n"
  8571. "|| bbbbbbbbbbbbbbb) { i++; }",
  8572. Style));
  8573. EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
  8574. " i++;\n"
  8575. "}",
  8576. format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
  8577. // Don't automatically break all macro definitions (llvm.org/PR17842).
  8578. verifyFormat("#define aNumber 10", Style);
  8579. // However, generally keep the line breaks that the user authored.
  8580. EXPECT_EQ("#define aNumber \\\n"
  8581. " 10",
  8582. format("#define aNumber \\\n"
  8583. " 10",
  8584. Style));
  8585. // Keep empty and one-element array literals on a single line.
  8586. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
  8587. " copyItems:YES];",
  8588. format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
  8589. "copyItems:YES];",
  8590. Style));
  8591. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
  8592. " copyItems:YES];",
  8593. format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
  8594. " copyItems:YES];",
  8595. Style));
  8596. // FIXME: This does not seem right, there should be more indentation before
  8597. // the array literal's entries. Nested blocks have the same problem.
  8598. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
  8599. " @\"a\",\n"
  8600. " @\"a\"\n"
  8601. "]\n"
  8602. " copyItems:YES];",
  8603. format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
  8604. " @\"a\",\n"
  8605. " @\"a\"\n"
  8606. " ]\n"
  8607. " copyItems:YES];",
  8608. Style));
  8609. EXPECT_EQ(
  8610. "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
  8611. " copyItems:YES];",
  8612. format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
  8613. " copyItems:YES];",
  8614. Style));
  8615. verifyFormat("[self.a b:c c:d];", Style);
  8616. EXPECT_EQ("[self.a b:c\n"
  8617. " c:d];",
  8618. format("[self.a b:c\n"
  8619. "c:d];",
  8620. Style));
  8621. }
  8622. TEST_F(FormatTest, FormatsLambdas) {
  8623. verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
  8624. verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
  8625. verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
  8626. verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
  8627. verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
  8628. verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
  8629. verifyFormat("void f() {\n"
  8630. " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
  8631. "}\n");
  8632. verifyFormat("void f() {\n"
  8633. " other(x.begin(), //\n"
  8634. " x.end(), //\n"
  8635. " [&](int, int) { return 1; });\n"
  8636. "}\n");
  8637. verifyFormat("SomeFunction([]() { // A cool function...\n"
  8638. " return 43;\n"
  8639. "});");
  8640. EXPECT_EQ("SomeFunction([]() {\n"
  8641. "#define A a\n"
  8642. " return 43;\n"
  8643. "});",
  8644. format("SomeFunction([](){\n"
  8645. "#define A a\n"
  8646. "return 43;\n"
  8647. "});"));
  8648. verifyFormat("void f() {\n"
  8649. " SomeFunction([](decltype(x), A *a) {});\n"
  8650. "}");
  8651. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  8652. " [](const aaaaaaaaaa &a) { return a; });");
  8653. verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
  8654. " SomeOtherFunctioooooooooooooooooooooooooon();\n"
  8655. "});");
  8656. // Lambdas with return types.
  8657. verifyFormat("int c = []() -> int { return 2; }();\n");
  8658. verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
  8659. verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
  8660. verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
  8661. verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
  8662. verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
  8663. verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
  8664. verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
  8665. " int j) -> int {\n"
  8666. " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
  8667. "};");
  8668. // Multiple lambdas in the same parentheses change indentation rules.
  8669. verifyFormat("SomeFunction(\n"
  8670. " []() {\n"
  8671. " int i = 42;\n"
  8672. " return i;\n"
  8673. " },\n"
  8674. " []() {\n"
  8675. " int j = 43;\n"
  8676. " return j;\n"
  8677. " });");
  8678. // More complex introducers.
  8679. verifyFormat("return [i, args...] {};");
  8680. // Not lambdas.
  8681. verifyFormat("constexpr char hello[]{\"hello\"};");
  8682. verifyFormat("double &operator[](int i) { return 0; }\n"
  8683. "int i;");
  8684. verifyFormat("std::unique_ptr<int[]> foo() {}");
  8685. verifyFormat("int i = a[a][a]->f();");
  8686. verifyFormat("int i = (*b)[a]->f();");
  8687. // Other corner cases.
  8688. verifyFormat("void f() {\n"
  8689. " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
  8690. " );\n"
  8691. "}");
  8692. // Lambdas created through weird macros.
  8693. verifyFormat("void f() {\n"
  8694. " MACRO((const AA &a) { return 1; });\n"
  8695. "}");
  8696. verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
  8697. " doo_dah();\n"
  8698. " doo_dah();\n"
  8699. " })) {\n"
  8700. "}");
  8701. }
  8702. TEST_F(FormatTest, FormatsBlocks) {
  8703. FormatStyle ShortBlocks = getLLVMStyle();
  8704. ShortBlocks.AllowShortBlocksOnASingleLine = true;
  8705. verifyFormat("int (^Block)(int, int);", ShortBlocks);
  8706. verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
  8707. verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
  8708. verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
  8709. verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
  8710. verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
  8711. verifyFormat("foo(^{ bar(); });", ShortBlocks);
  8712. verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
  8713. verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
  8714. verifyFormat("[operation setCompletionBlock:^{\n"
  8715. " [self onOperationDone];\n"
  8716. "}];");
  8717. verifyFormat("int i = {[operation setCompletionBlock:^{\n"
  8718. " [self onOperationDone];\n"
  8719. "}]};");
  8720. verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
  8721. " f();\n"
  8722. "}];");
  8723. verifyFormat("int a = [operation block:^int(int *i) {\n"
  8724. " return 1;\n"
  8725. "}];");
  8726. verifyFormat("[myObject doSomethingWith:arg1\n"
  8727. " aaa:^int(int *a) {\n"
  8728. " return 1;\n"
  8729. " }\n"
  8730. " bbb:f(a * bbbbbbbb)];");
  8731. verifyFormat("[operation setCompletionBlock:^{\n"
  8732. " [self.delegate newDataAvailable];\n"
  8733. "}];",
  8734. getLLVMStyleWithColumns(60));
  8735. verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
  8736. " NSString *path = [self sessionFilePath];\n"
  8737. " if (path) {\n"
  8738. " // ...\n"
  8739. " }\n"
  8740. "});");
  8741. verifyFormat("[[SessionService sharedService]\n"
  8742. " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  8743. " if (window) {\n"
  8744. " [self windowDidLoad:window];\n"
  8745. " } else {\n"
  8746. " [self errorLoadingWindow];\n"
  8747. " }\n"
  8748. " }];");
  8749. verifyFormat("void (^largeBlock)(void) = ^{\n"
  8750. " // ...\n"
  8751. "};\n",
  8752. getLLVMStyleWithColumns(40));
  8753. verifyFormat("[[SessionService sharedService]\n"
  8754. " loadWindowWithCompletionBlock: //\n"
  8755. " ^(SessionWindow *window) {\n"
  8756. " if (window) {\n"
  8757. " [self windowDidLoad:window];\n"
  8758. " } else {\n"
  8759. " [self errorLoadingWindow];\n"
  8760. " }\n"
  8761. " }];",
  8762. getLLVMStyleWithColumns(60));
  8763. verifyFormat("[myObject doSomethingWith:arg1\n"
  8764. " firstBlock:^(Foo *a) {\n"
  8765. " // ...\n"
  8766. " int i;\n"
  8767. " }\n"
  8768. " secondBlock:^(Bar *b) {\n"
  8769. " // ...\n"
  8770. " int i;\n"
  8771. " }\n"
  8772. " thirdBlock:^Foo(Bar *b) {\n"
  8773. " // ...\n"
  8774. " int i;\n"
  8775. " }];");
  8776. verifyFormat("[myObject doSomethingWith:arg1\n"
  8777. " firstBlock:-1\n"
  8778. " secondBlock:^(Bar *b) {\n"
  8779. " // ...\n"
  8780. " int i;\n"
  8781. " }];");
  8782. verifyFormat("f(^{\n"
  8783. " @autoreleasepool {\n"
  8784. " if (a) {\n"
  8785. " g();\n"
  8786. " }\n"
  8787. " }\n"
  8788. "});");
  8789. verifyFormat("Block b = ^int *(A *a, B *b) {}");
  8790. FormatStyle FourIndent = getLLVMStyle();
  8791. FourIndent.ObjCBlockIndentWidth = 4;
  8792. verifyFormat("[operation setCompletionBlock:^{\n"
  8793. " [self onOperationDone];\n"
  8794. "}];",
  8795. FourIndent);
  8796. }
  8797. TEST_F(FormatTest, SupportsCRLF) {
  8798. EXPECT_EQ("int a;\r\n"
  8799. "int b;\r\n"
  8800. "int c;\r\n",
  8801. format("int a;\r\n"
  8802. " int b;\r\n"
  8803. " int c;\r\n",
  8804. getLLVMStyle()));
  8805. EXPECT_EQ("int a;\r\n"
  8806. "int b;\r\n"
  8807. "int c;\r\n",
  8808. format("int a;\r\n"
  8809. " int b;\n"
  8810. " int c;\r\n",
  8811. getLLVMStyle()));
  8812. EXPECT_EQ("int a;\n"
  8813. "int b;\n"
  8814. "int c;\n",
  8815. format("int a;\r\n"
  8816. " int b;\n"
  8817. " int c;\n",
  8818. getLLVMStyle()));
  8819. EXPECT_EQ("\"aaaaaaa \"\r\n"
  8820. "\"bbbbbbb\";\r\n",
  8821. format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
  8822. EXPECT_EQ("#define A \\\r\n"
  8823. " b; \\\r\n"
  8824. " c; \\\r\n"
  8825. " d;\r\n",
  8826. format("#define A \\\r\n"
  8827. " b; \\\r\n"
  8828. " c; d; \r\n",
  8829. getGoogleStyle()));
  8830. EXPECT_EQ("/*\r\n"
  8831. "multi line block comments\r\n"
  8832. "should not introduce\r\n"
  8833. "an extra carriage return\r\n"
  8834. "*/\r\n",
  8835. format("/*\r\n"
  8836. "multi line block comments\r\n"
  8837. "should not introduce\r\n"
  8838. "an extra carriage return\r\n"
  8839. "*/\r\n"));
  8840. }
  8841. TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
  8842. verifyFormat("MY_CLASS(C) {\n"
  8843. " int i;\n"
  8844. " int j;\n"
  8845. "};");
  8846. }
  8847. TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
  8848. FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
  8849. TwoIndent.ContinuationIndentWidth = 2;
  8850. EXPECT_EQ("int i =\n"
  8851. " longFunction(\n"
  8852. " arg);",
  8853. format("int i = longFunction(arg);", TwoIndent));
  8854. FormatStyle SixIndent = getLLVMStyleWithColumns(20);
  8855. SixIndent.ContinuationIndentWidth = 6;
  8856. EXPECT_EQ("int i =\n"
  8857. " longFunction(\n"
  8858. " arg);",
  8859. format("int i = longFunction(arg);", SixIndent));
  8860. }
  8861. TEST_F(FormatTest, SpacesInAngles) {
  8862. FormatStyle Spaces = getLLVMStyle();
  8863. Spaces.SpacesInAngles = true;
  8864. verifyFormat("static_cast< int >(arg);", Spaces);
  8865. verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
  8866. verifyFormat("f< int, float >();", Spaces);
  8867. verifyFormat("template <> g() {}", Spaces);
  8868. verifyFormat("template < std::vector< int > > f() {}", Spaces);
  8869. Spaces.Standard = FormatStyle::LS_Cpp03;
  8870. Spaces.SpacesInAngles = true;
  8871. verifyFormat("A< A< int > >();", Spaces);
  8872. Spaces.SpacesInAngles = false;
  8873. verifyFormat("A<A<int> >();", Spaces);
  8874. Spaces.Standard = FormatStyle::LS_Cpp11;
  8875. Spaces.SpacesInAngles = true;
  8876. verifyFormat("A< A< int > >();", Spaces);
  8877. Spaces.SpacesInAngles = false;
  8878. verifyFormat("A<A<int>>();", Spaces);
  8879. }
  8880. TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
  8881. std::string code = "#if A\n"
  8882. "#if B\n"
  8883. "a.\n"
  8884. "#endif\n"
  8885. " a = 1;\n"
  8886. "#else\n"
  8887. "#endif\n"
  8888. "#if C\n"
  8889. "#else\n"
  8890. "#endif\n";
  8891. EXPECT_EQ(code, format(code));
  8892. }
  8893. TEST_F(FormatTest, HandleConflictMarkers) {
  8894. // Git/SVN conflict markers.
  8895. EXPECT_EQ("int a;\n"
  8896. "void f() {\n"
  8897. " callme(some(parameter1,\n"
  8898. "<<<<<<< text by the vcs\n"
  8899. " parameter2),\n"
  8900. "||||||| text by the vcs\n"
  8901. " parameter2),\n"
  8902. " parameter3,\n"
  8903. "======= text by the vcs\n"
  8904. " parameter2, parameter3),\n"
  8905. ">>>>>>> text by the vcs\n"
  8906. " otherparameter);\n",
  8907. format("int a;\n"
  8908. "void f() {\n"
  8909. " callme(some(parameter1,\n"
  8910. "<<<<<<< text by the vcs\n"
  8911. " parameter2),\n"
  8912. "||||||| text by the vcs\n"
  8913. " parameter2),\n"
  8914. " parameter3,\n"
  8915. "======= text by the vcs\n"
  8916. " parameter2,\n"
  8917. " parameter3),\n"
  8918. ">>>>>>> text by the vcs\n"
  8919. " otherparameter);\n"));
  8920. // Perforce markers.
  8921. EXPECT_EQ("void f() {\n"
  8922. " function(\n"
  8923. ">>>> text by the vcs\n"
  8924. " parameter,\n"
  8925. "==== text by the vcs\n"
  8926. " parameter,\n"
  8927. "==== text by the vcs\n"
  8928. " parameter,\n"
  8929. "<<<< text by the vcs\n"
  8930. " parameter);\n",
  8931. format("void f() {\n"
  8932. " function(\n"
  8933. ">>>> text by the vcs\n"
  8934. " parameter,\n"
  8935. "==== text by the vcs\n"
  8936. " parameter,\n"
  8937. "==== text by the vcs\n"
  8938. " parameter,\n"
  8939. "<<<< text by the vcs\n"
  8940. " parameter);\n"));
  8941. EXPECT_EQ("<<<<<<<\n"
  8942. "|||||||\n"
  8943. "=======\n"
  8944. ">>>>>>>",
  8945. format("<<<<<<<\n"
  8946. "|||||||\n"
  8947. "=======\n"
  8948. ">>>>>>>"));
  8949. EXPECT_EQ("<<<<<<<\n"
  8950. "|||||||\n"
  8951. "int i;\n"
  8952. "=======\n"
  8953. ">>>>>>>",
  8954. format("<<<<<<<\n"
  8955. "|||||||\n"
  8956. "int i;\n"
  8957. "=======\n"
  8958. ">>>>>>>"));
  8959. // FIXME: Handle parsing of macros around conflict markers correctly:
  8960. EXPECT_EQ("#define Macro \\\n"
  8961. "<<<<<<<\n"
  8962. "Something \\\n"
  8963. "|||||||\n"
  8964. "Else \\\n"
  8965. "=======\n"
  8966. "Other \\\n"
  8967. ">>>>>>>\n"
  8968. " End int i;\n",
  8969. format("#define Macro \\\n"
  8970. "<<<<<<<\n"
  8971. " Something \\\n"
  8972. "|||||||\n"
  8973. " Else \\\n"
  8974. "=======\n"
  8975. " Other \\\n"
  8976. ">>>>>>>\n"
  8977. " End\n"
  8978. "int i;\n"));
  8979. }
  8980. TEST_F(FormatTest, DisableRegions) {
  8981. EXPECT_EQ("int i;\n"
  8982. "// clang-format off\n"
  8983. " int j;\n"
  8984. "// clang-format on\n"
  8985. "int k;",
  8986. format(" int i;\n"
  8987. " // clang-format off\n"
  8988. " int j;\n"
  8989. " // clang-format on\n"
  8990. " int k;"));
  8991. EXPECT_EQ("int i;\n"
  8992. "/* clang-format off */\n"
  8993. " int j;\n"
  8994. "/* clang-format on */\n"
  8995. "int k;",
  8996. format(" int i;\n"
  8997. " /* clang-format off */\n"
  8998. " int j;\n"
  8999. " /* clang-format on */\n"
  9000. " int k;"));
  9001. }
  9002. TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
  9003. format("? ) =");
  9004. }
  9005. } // end namespace tooling
  9006. } // end namespace clang