FormatTest.cpp 362 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772
  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. EXPECT_EQ("void f() {\n"
  917. " lineWith(); // comment\n"
  918. " // at start\n"
  919. "}",
  920. format("void f() {\n"
  921. " lineWith(); // comment\n"
  922. " // at start\n"
  923. "}"));
  924. verifyFormat(
  925. "#define A \\\n"
  926. " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
  927. " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
  928. getLLVMStyleWithColumns(60));
  929. verifyFormat(
  930. "#define A \\\n"
  931. " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
  932. " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
  933. getLLVMStyleWithColumns(61));
  934. verifyFormat("if ( // This is some comment\n"
  935. " x + 3) {\n"
  936. "}");
  937. EXPECT_EQ("if ( // This is some comment\n"
  938. " // spanning two lines\n"
  939. " x + 3) {\n"
  940. "}",
  941. format("if( // This is some comment\n"
  942. " // spanning two lines\n"
  943. " x + 3) {\n"
  944. "}"));
  945. }
  946. TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
  947. EXPECT_EQ("SomeFunction(a,\n"
  948. " b, // comment\n"
  949. " c);",
  950. format("SomeFunction(a,\n"
  951. " b, // comment\n"
  952. " c);"));
  953. EXPECT_EQ("SomeFunction(a, b,\n"
  954. " // comment\n"
  955. " c);",
  956. format("SomeFunction(a,\n"
  957. " b,\n"
  958. " // comment\n"
  959. " c);"));
  960. EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
  961. " c);",
  962. format("SomeFunction(a, b, // comment (unclear relation)\n"
  963. " c);"));
  964. EXPECT_EQ("SomeFunction(a, // comment\n"
  965. " b,\n"
  966. " c); // comment",
  967. format("SomeFunction(a, // comment\n"
  968. " b,\n"
  969. " c); // comment"));
  970. }
  971. TEST_F(FormatTest, CanFormatCommentsLocally) {
  972. EXPECT_EQ("int a; // comment\n"
  973. "int b; // comment",
  974. format("int a; // comment\n"
  975. "int b; // comment",
  976. 0, 0, getLLVMStyle()));
  977. EXPECT_EQ("int a; // comment\n"
  978. " // line 2\n"
  979. "int b;",
  980. format("int a; // comment\n"
  981. " // line 2\n"
  982. "int b;",
  983. 28, 0, getLLVMStyle()));
  984. EXPECT_EQ("int aaaaaa; // comment\n"
  985. "int b;\n"
  986. "int c; // unrelated comment",
  987. format("int aaaaaa; // comment\n"
  988. "int b;\n"
  989. "int c; // unrelated comment",
  990. 31, 0, getLLVMStyle()));
  991. EXPECT_EQ("int a; // This\n"
  992. " // is\n"
  993. " // a",
  994. format("int a; // This\n"
  995. " // is\n"
  996. " // a",
  997. 0, 0, getLLVMStyle()));
  998. EXPECT_EQ("int a; // This\n"
  999. " // is\n"
  1000. " // a\n"
  1001. "// This is b\n"
  1002. "int b;",
  1003. format("int a; // This\n"
  1004. " // is\n"
  1005. " // a\n"
  1006. "// This is b\n"
  1007. "int b;",
  1008. 0, 0, getLLVMStyle()));
  1009. EXPECT_EQ("int a; // This\n"
  1010. " // is\n"
  1011. " // a\n"
  1012. "\n"
  1013. " // This is unrelated",
  1014. format("int a; // This\n"
  1015. " // is\n"
  1016. " // a\n"
  1017. "\n"
  1018. " // This is unrelated",
  1019. 0, 0, getLLVMStyle()));
  1020. EXPECT_EQ("int a;\n"
  1021. "// This is\n"
  1022. "// not formatted. ",
  1023. format("int a;\n"
  1024. "// This is\n"
  1025. "// not formatted. ",
  1026. 0, 0, getLLVMStyle()));
  1027. }
  1028. TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
  1029. EXPECT_EQ("// comment", format("// comment "));
  1030. EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
  1031. format("int aaaaaaa, bbbbbbb; // comment ",
  1032. getLLVMStyleWithColumns(33)));
  1033. EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f "));
  1034. EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f "));
  1035. }
  1036. TEST_F(FormatTest, UnderstandsBlockComments) {
  1037. verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
  1038. verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }");
  1039. EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
  1040. " bbbbbbbbbbbbbbbbbbbbbbbbb);",
  1041. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n"
  1042. "/* Trailing comment for aa... */\n"
  1043. " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  1044. EXPECT_EQ(
  1045. "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  1046. " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
  1047. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
  1048. "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  1049. EXPECT_EQ(
  1050. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1051. " aaaaaaaaaaaaaaaaaa,\n"
  1052. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  1053. "}",
  1054. format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1055. " aaaaaaaaaaaaaaaaaa ,\n"
  1056. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  1057. "}"));
  1058. FormatStyle NoBinPacking = getLLVMStyle();
  1059. NoBinPacking.BinPackParameters = false;
  1060. verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
  1061. " /* parameter 2 */ aaaaaa,\n"
  1062. " /* parameter 3 */ aaaaaa,\n"
  1063. " /* parameter 4 */ aaaaaa);",
  1064. NoBinPacking);
  1065. // Aligning block comments in macros.
  1066. verifyGoogleFormat("#define A \\\n"
  1067. " int i; /*a*/ \\\n"
  1068. " int jjj; /*b*/");
  1069. }
  1070. TEST_F(FormatTest, AlignsBlockComments) {
  1071. EXPECT_EQ("/*\n"
  1072. " * Really multi-line\n"
  1073. " * comment.\n"
  1074. " */\n"
  1075. "void f() {}",
  1076. format(" /*\n"
  1077. " * Really multi-line\n"
  1078. " * comment.\n"
  1079. " */\n"
  1080. " void f() {}"));
  1081. EXPECT_EQ("class C {\n"
  1082. " /*\n"
  1083. " * Another multi-line\n"
  1084. " * comment.\n"
  1085. " */\n"
  1086. " void f() {}\n"
  1087. "};",
  1088. format("class C {\n"
  1089. "/*\n"
  1090. " * Another multi-line\n"
  1091. " * comment.\n"
  1092. " */\n"
  1093. "void f() {}\n"
  1094. "};"));
  1095. EXPECT_EQ("/*\n"
  1096. " 1. This is a comment with non-trivial formatting.\n"
  1097. " 1.1. We have to indent/outdent all lines equally\n"
  1098. " 1.1.1. to keep the formatting.\n"
  1099. " */",
  1100. format(" /*\n"
  1101. " 1. This is a comment with non-trivial formatting.\n"
  1102. " 1.1. We have to indent/outdent all lines equally\n"
  1103. " 1.1.1. to keep the formatting.\n"
  1104. " */"));
  1105. EXPECT_EQ("/*\n"
  1106. "Don't try to outdent if there's not enough indentation.\n"
  1107. "*/",
  1108. format(" /*\n"
  1109. " Don't try to outdent if there's not enough indentation.\n"
  1110. " */"));
  1111. EXPECT_EQ("int i; /* Comment with empty...\n"
  1112. " *\n"
  1113. " * line. */",
  1114. format("int i; /* Comment with empty...\n"
  1115. " *\n"
  1116. " * line. */"));
  1117. EXPECT_EQ("int foobar = 0; /* comment */\n"
  1118. "int bar = 0; /* multiline\n"
  1119. " comment 1 */\n"
  1120. "int baz = 0; /* multiline\n"
  1121. " comment 2 */\n"
  1122. "int bzz = 0; /* multiline\n"
  1123. " comment 3 */",
  1124. format("int foobar = 0; /* comment */\n"
  1125. "int bar = 0; /* multiline\n"
  1126. " comment 1 */\n"
  1127. "int baz = 0; /* multiline\n"
  1128. " comment 2 */\n"
  1129. "int bzz = 0; /* multiline\n"
  1130. " comment 3 */"));
  1131. EXPECT_EQ("int foobar = 0; /* comment */\n"
  1132. "int bar = 0; /* multiline\n"
  1133. " comment */\n"
  1134. "int baz = 0; /* multiline\n"
  1135. "comment */",
  1136. format("int foobar = 0; /* comment */\n"
  1137. "int bar = 0; /* multiline\n"
  1138. "comment */\n"
  1139. "int baz = 0; /* multiline\n"
  1140. "comment */"));
  1141. }
  1142. TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
  1143. EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1144. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
  1145. format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1146. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
  1147. EXPECT_EQ(
  1148. "void ffffffffffff(\n"
  1149. " int aaaaaaaa, int bbbbbbbb,\n"
  1150. " int cccccccccccc) { /*\n"
  1151. " aaaaaaaaaa\n"
  1152. " aaaaaaaaaaaaa\n"
  1153. " bbbbbbbbbbbbbb\n"
  1154. " bbbbbbbbbb\n"
  1155. " */\n"
  1156. "}",
  1157. format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
  1158. "{ /*\n"
  1159. " aaaaaaaaaa aaaaaaaaaaaaa\n"
  1160. " bbbbbbbbbbbbbb bbbbbbbbbb\n"
  1161. " */\n"
  1162. "}",
  1163. getLLVMStyleWithColumns(40)));
  1164. }
  1165. TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
  1166. EXPECT_EQ("void ffffffffff(\n"
  1167. " int aaaaa /* test */);",
  1168. format("void ffffffffff(int aaaaa /* test */);",
  1169. getLLVMStyleWithColumns(35)));
  1170. }
  1171. TEST_F(FormatTest, SplitsLongCxxComments) {
  1172. EXPECT_EQ("// A comment that\n"
  1173. "// doesn't fit on\n"
  1174. "// one line",
  1175. format("// A comment that doesn't fit on one line",
  1176. getLLVMStyleWithColumns(20)));
  1177. EXPECT_EQ("// a b c d\n"
  1178. "// e f g\n"
  1179. "// h i j k",
  1180. format("// a b c d e f g h i j k",
  1181. getLLVMStyleWithColumns(10)));
  1182. EXPECT_EQ("// a b c d\n"
  1183. "// e f g\n"
  1184. "// h i j k",
  1185. format("\\\n// a b c d e f g h i j k",
  1186. getLLVMStyleWithColumns(10)));
  1187. EXPECT_EQ("if (true) // A comment that\n"
  1188. " // doesn't fit on\n"
  1189. " // one line",
  1190. format("if (true) // A comment that doesn't fit on one line ",
  1191. getLLVMStyleWithColumns(30)));
  1192. EXPECT_EQ("// Don't_touch_leading_whitespace",
  1193. format("// Don't_touch_leading_whitespace",
  1194. getLLVMStyleWithColumns(20)));
  1195. EXPECT_EQ("// Add leading\n"
  1196. "// whitespace",
  1197. format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
  1198. EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
  1199. EXPECT_EQ("// Even if it makes the line exceed the column\n"
  1200. "// limit",
  1201. format("//Even if it makes the line exceed the column limit",
  1202. getLLVMStyleWithColumns(51)));
  1203. EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
  1204. EXPECT_EQ("// aa bb cc dd",
  1205. format("// aa bb cc dd ",
  1206. getLLVMStyleWithColumns(15)));
  1207. EXPECT_EQ("// A comment before\n"
  1208. "// a macro\n"
  1209. "// definition\n"
  1210. "#define a b",
  1211. format("// A comment before a macro definition\n"
  1212. "#define a b",
  1213. getLLVMStyleWithColumns(20)));
  1214. EXPECT_EQ("void ffffff(\n"
  1215. " int aaaaaaaaa, // wwww\n"
  1216. " int bbbbbbbbbb, // xxxxxxx\n"
  1217. " // yyyyyyyyyy\n"
  1218. " int c, int d, int e) {}",
  1219. format("void ffffff(\n"
  1220. " int aaaaaaaaa, // wwww\n"
  1221. " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
  1222. " int c, int d, int e) {}",
  1223. getLLVMStyleWithColumns(40)));
  1224. EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1225. format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1226. getLLVMStyleWithColumns(20)));
  1227. EXPECT_EQ(
  1228. "#define XXX // a b c d\n"
  1229. " // e f g h",
  1230. format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
  1231. EXPECT_EQ(
  1232. "#define XXX // q w e r\n"
  1233. " // t y u i",
  1234. format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
  1235. }
  1236. TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {
  1237. EXPECT_EQ("// A comment\n"
  1238. "// that doesn't\n"
  1239. "// fit on one\n"
  1240. "// line",
  1241. format("// A comment that doesn't fit on one line",
  1242. getLLVMStyleWithColumns(20)));
  1243. EXPECT_EQ("/// A comment\n"
  1244. "/// that doesn't\n"
  1245. "/// fit on one\n"
  1246. "/// line",
  1247. format("/// A comment that doesn't fit on one line",
  1248. getLLVMStyleWithColumns(20)));
  1249. }
  1250. TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
  1251. EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1252. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1253. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1254. format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1255. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1256. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
  1257. EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1258. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1259. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1260. format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1261. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1262. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1263. getLLVMStyleWithColumns(50)));
  1264. // FIXME: One day we might want to implement adjustment of leading whitespace
  1265. // of the consecutive lines in this kind of comment:
  1266. EXPECT_EQ("double\n"
  1267. " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1268. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1269. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1270. format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1271. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1272. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1273. getLLVMStyleWithColumns(49)));
  1274. }
  1275. TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) {
  1276. FormatStyle Pragmas = getLLVMStyleWithColumns(30);
  1277. Pragmas.CommentPragmas = "^ IWYU pragma:";
  1278. EXPECT_EQ(
  1279. "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
  1280. format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
  1281. EXPECT_EQ(
  1282. "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
  1283. format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
  1284. }
  1285. TEST_F(FormatTest, PriorityOfCommentBreaking) {
  1286. EXPECT_EQ("if (xxx ==\n"
  1287. " yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  1288. " zzz)\n"
  1289. " q();",
  1290. format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  1291. " zzz) q();",
  1292. getLLVMStyleWithColumns(40)));
  1293. EXPECT_EQ("if (xxxxxxxxxx ==\n"
  1294. " yyy && // aaaaaa bbbbbbbb cccc\n"
  1295. " zzz)\n"
  1296. " q();",
  1297. format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
  1298. " zzz) q();",
  1299. getLLVMStyleWithColumns(40)));
  1300. EXPECT_EQ("if (xxxxxxxxxx &&\n"
  1301. " yyy || // aaaaaa bbbbbbbb cccc\n"
  1302. " zzz)\n"
  1303. " q();",
  1304. format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
  1305. " zzz) q();",
  1306. getLLVMStyleWithColumns(40)));
  1307. EXPECT_EQ("fffffffff(\n"
  1308. " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  1309. " zzz);",
  1310. format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  1311. " zzz);",
  1312. getLLVMStyleWithColumns(40)));
  1313. }
  1314. TEST_F(FormatTest, MultiLineCommentsInDefines) {
  1315. EXPECT_EQ("#define A(x) /* \\\n"
  1316. " a comment \\\n"
  1317. " inside */ \\\n"
  1318. " f();",
  1319. format("#define A(x) /* \\\n"
  1320. " a comment \\\n"
  1321. " inside */ \\\n"
  1322. " f();",
  1323. getLLVMStyleWithColumns(17)));
  1324. EXPECT_EQ("#define A( \\\n"
  1325. " x) /* \\\n"
  1326. " a comment \\\n"
  1327. " inside */ \\\n"
  1328. " f();",
  1329. format("#define A( \\\n"
  1330. " x) /* \\\n"
  1331. " a comment \\\n"
  1332. " inside */ \\\n"
  1333. " f();",
  1334. getLLVMStyleWithColumns(17)));
  1335. }
  1336. TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
  1337. EXPECT_EQ("namespace {}\n// Test\n#define A",
  1338. format("namespace {}\n // Test\n#define A"));
  1339. EXPECT_EQ("namespace {}\n/* Test */\n#define A",
  1340. format("namespace {}\n /* Test */\n#define A"));
  1341. EXPECT_EQ("namespace {}\n/* Test */ #define A",
  1342. format("namespace {}\n /* Test */ #define A"));
  1343. }
  1344. TEST_F(FormatTest, SplitsLongLinesInComments) {
  1345. EXPECT_EQ("/* This is a long\n"
  1346. " * comment that\n"
  1347. " * doesn't\n"
  1348. " * fit on one line.\n"
  1349. " */",
  1350. format("/* "
  1351. "This is a long "
  1352. "comment that "
  1353. "doesn't "
  1354. "fit on one line. */",
  1355. getLLVMStyleWithColumns(20)));
  1356. EXPECT_EQ("/* a b c d\n"
  1357. " * e f g\n"
  1358. " * h i j k\n"
  1359. " */",
  1360. format("/* a b c d e f g h i j k */",
  1361. getLLVMStyleWithColumns(10)));
  1362. EXPECT_EQ("/* a b c d\n"
  1363. " * e f g\n"
  1364. " * h i j k\n"
  1365. " */",
  1366. format("\\\n/* a b c d e f g h i j k */",
  1367. getLLVMStyleWithColumns(10)));
  1368. EXPECT_EQ("/*\n"
  1369. "This is a long\n"
  1370. "comment that doesn't\n"
  1371. "fit on one line.\n"
  1372. "*/",
  1373. format("/*\n"
  1374. "This is a long "
  1375. "comment that doesn't "
  1376. "fit on one line. \n"
  1377. "*/", getLLVMStyleWithColumns(20)));
  1378. EXPECT_EQ("/*\n"
  1379. " * This is a long\n"
  1380. " * comment that\n"
  1381. " * doesn't fit on\n"
  1382. " * one line.\n"
  1383. " */",
  1384. format("/* \n"
  1385. " * This is a long "
  1386. " comment that "
  1387. " doesn't fit on "
  1388. " one line. \n"
  1389. " */", getLLVMStyleWithColumns(20)));
  1390. EXPECT_EQ("/*\n"
  1391. " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
  1392. " * so_it_should_be_broken\n"
  1393. " * wherever_a_space_occurs\n"
  1394. " */",
  1395. format("/*\n"
  1396. " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
  1397. " so_it_should_be_broken "
  1398. " wherever_a_space_occurs \n"
  1399. " */",
  1400. getLLVMStyleWithColumns(20)));
  1401. EXPECT_EQ("/*\n"
  1402. " * This_comment_can_not_be_broken_into_lines\n"
  1403. " */",
  1404. format("/*\n"
  1405. " * This_comment_can_not_be_broken_into_lines\n"
  1406. " */",
  1407. getLLVMStyleWithColumns(20)));
  1408. EXPECT_EQ("{\n"
  1409. " /*\n"
  1410. " This is another\n"
  1411. " long comment that\n"
  1412. " doesn't fit on one\n"
  1413. " line 1234567890\n"
  1414. " */\n"
  1415. "}",
  1416. format("{\n"
  1417. "/*\n"
  1418. "This is another "
  1419. " long comment that "
  1420. " doesn't fit on one"
  1421. " line 1234567890\n"
  1422. "*/\n"
  1423. "}", getLLVMStyleWithColumns(20)));
  1424. EXPECT_EQ("{\n"
  1425. " /*\n"
  1426. " * This i s\n"
  1427. " * another comment\n"
  1428. " * t hat doesn' t\n"
  1429. " * fit on one l i\n"
  1430. " * n e\n"
  1431. " */\n"
  1432. "}",
  1433. format("{\n"
  1434. "/*\n"
  1435. " * This i s"
  1436. " another comment"
  1437. " t hat doesn' t"
  1438. " fit on one l i"
  1439. " n e\n"
  1440. " */\n"
  1441. "}", getLLVMStyleWithColumns(20)));
  1442. EXPECT_EQ("/*\n"
  1443. " * This is a long\n"
  1444. " * comment that\n"
  1445. " * doesn't fit on\n"
  1446. " * one line\n"
  1447. " */",
  1448. format(" /*\n"
  1449. " * This is a long comment that doesn't fit on one line\n"
  1450. " */", getLLVMStyleWithColumns(20)));
  1451. EXPECT_EQ("{\n"
  1452. " if (something) /* This is a\n"
  1453. " long\n"
  1454. " comment */\n"
  1455. " ;\n"
  1456. "}",
  1457. format("{\n"
  1458. " if (something) /* This is a long comment */\n"
  1459. " ;\n"
  1460. "}",
  1461. getLLVMStyleWithColumns(30)));
  1462. EXPECT_EQ("/* A comment before\n"
  1463. " * a macro\n"
  1464. " * definition */\n"
  1465. "#define a b",
  1466. format("/* A comment before a macro definition */\n"
  1467. "#define a b",
  1468. getLLVMStyleWithColumns(20)));
  1469. EXPECT_EQ("/* some comment\n"
  1470. " * a comment\n"
  1471. "* that we break\n"
  1472. " * another comment\n"
  1473. "* we have to break\n"
  1474. "* a left comment\n"
  1475. " */",
  1476. format(" /* some comment\n"
  1477. " * a comment that we break\n"
  1478. " * another comment we have to break\n"
  1479. "* a left comment\n"
  1480. " */",
  1481. getLLVMStyleWithColumns(20)));
  1482. EXPECT_EQ("/*\n"
  1483. "\n"
  1484. "\n"
  1485. " */\n",
  1486. format(" /* \n"
  1487. " \n"
  1488. " \n"
  1489. " */\n"));
  1490. EXPECT_EQ("/* a a */",
  1491. format("/* a a */", getLLVMStyleWithColumns(15)));
  1492. EXPECT_EQ("/* a a bc */",
  1493. format("/* a a bc */", getLLVMStyleWithColumns(15)));
  1494. EXPECT_EQ("/* aaa aaa\n"
  1495. " * aaaaa */",
  1496. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1497. EXPECT_EQ("/* aaa aaa\n"
  1498. " * aaaaa */",
  1499. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1500. }
  1501. TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
  1502. EXPECT_EQ("#define X \\\n"
  1503. " /* \\\n"
  1504. " Test \\\n"
  1505. " Macro comment \\\n"
  1506. " with a long \\\n"
  1507. " line \\\n"
  1508. " */ \\\n"
  1509. " A + B",
  1510. format("#define X \\\n"
  1511. " /*\n"
  1512. " Test\n"
  1513. " Macro comment with a long line\n"
  1514. " */ \\\n"
  1515. " A + B",
  1516. getLLVMStyleWithColumns(20)));
  1517. EXPECT_EQ("#define X \\\n"
  1518. " /* Macro comment \\\n"
  1519. " with a long \\\n"
  1520. " line */ \\\n"
  1521. " A + B",
  1522. format("#define X \\\n"
  1523. " /* Macro comment with a long\n"
  1524. " line */ \\\n"
  1525. " A + B",
  1526. getLLVMStyleWithColumns(20)));
  1527. EXPECT_EQ("#define X \\\n"
  1528. " /* Macro comment \\\n"
  1529. " * with a long \\\n"
  1530. " * line */ \\\n"
  1531. " A + B",
  1532. format("#define X \\\n"
  1533. " /* Macro comment with a long line */ \\\n"
  1534. " A + B",
  1535. getLLVMStyleWithColumns(20)));
  1536. }
  1537. TEST_F(FormatTest, CommentsInStaticInitializers) {
  1538. EXPECT_EQ(
  1539. "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
  1540. " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
  1541. " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
  1542. " aaaaaaaaaaaaaaaaaaaa, // comment\n"
  1543. " aaaaaaaaaaaaaaaaaaaa};",
  1544. format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
  1545. " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
  1546. " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
  1547. " aaaaaaaaaaaaaaaaaaaa , // comment\n"
  1548. " aaaaaaaaaaaaaaaaaaaa };"));
  1549. verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1550. " bbbbbbbbbbb, ccccccccccc};");
  1551. verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1552. " // comment for bb....\n"
  1553. " bbbbbbbbbbb, ccccccccccc};");
  1554. verifyGoogleFormat(
  1555. "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1556. " bbbbbbbbbbb, ccccccccccc};");
  1557. verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1558. " // comment for bb....\n"
  1559. " bbbbbbbbbbb, ccccccccccc};");
  1560. verifyFormat("S s = {{a, b, c}, // Group #1\n"
  1561. " {d, e, f}, // Group #2\n"
  1562. " {g, h, i}}; // Group #3");
  1563. verifyFormat("S s = {{// Group #1\n"
  1564. " a, b, c},\n"
  1565. " {// Group #2\n"
  1566. " d, e, f},\n"
  1567. " {// Group #3\n"
  1568. " g, h, i}};");
  1569. EXPECT_EQ("S s = {\n"
  1570. " // Some comment\n"
  1571. " a,\n"
  1572. "\n"
  1573. " // Comment after empty line\n"
  1574. " b}",
  1575. format("S s = {\n"
  1576. " // Some comment\n"
  1577. " a,\n"
  1578. " \n"
  1579. " // Comment after empty line\n"
  1580. " b\n"
  1581. "}"));
  1582. EXPECT_EQ("S s = {\n"
  1583. " /* Some comment */\n"
  1584. " a,\n"
  1585. "\n"
  1586. " /* Comment after empty line */\n"
  1587. " b}",
  1588. format("S s = {\n"
  1589. " /* Some comment */\n"
  1590. " a,\n"
  1591. " \n"
  1592. " /* Comment after empty line */\n"
  1593. " b\n"
  1594. "}"));
  1595. verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
  1596. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1597. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1598. " 0x00, 0x00, 0x00, 0x00}; // comment\n");
  1599. }
  1600. TEST_F(FormatTest, IgnoresIf0Contents) {
  1601. EXPECT_EQ("#if 0\n"
  1602. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1603. "#endif\n"
  1604. "void f() {}",
  1605. format("#if 0\n"
  1606. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1607. "#endif\n"
  1608. "void f( ) { }"));
  1609. EXPECT_EQ("#if false\n"
  1610. "void f( ) { }\n"
  1611. "#endif\n"
  1612. "void g() {}\n",
  1613. format("#if false\n"
  1614. "void f( ) { }\n"
  1615. "#endif\n"
  1616. "void g( ) { }\n"));
  1617. EXPECT_EQ("enum E {\n"
  1618. " One,\n"
  1619. " Two,\n"
  1620. "#if 0\n"
  1621. "Three,\n"
  1622. " Four,\n"
  1623. "#endif\n"
  1624. " Five\n"
  1625. "};",
  1626. format("enum E {\n"
  1627. " One,Two,\n"
  1628. "#if 0\n"
  1629. "Three,\n"
  1630. " Four,\n"
  1631. "#endif\n"
  1632. " Five};"));
  1633. EXPECT_EQ("enum F {\n"
  1634. " One,\n"
  1635. "#if 1\n"
  1636. " Two,\n"
  1637. "#if 0\n"
  1638. "Three,\n"
  1639. " Four,\n"
  1640. "#endif\n"
  1641. " Five\n"
  1642. "#endif\n"
  1643. "};",
  1644. format("enum F {\n"
  1645. "One,\n"
  1646. "#if 1\n"
  1647. "Two,\n"
  1648. "#if 0\n"
  1649. "Three,\n"
  1650. " Four,\n"
  1651. "#endif\n"
  1652. "Five\n"
  1653. "#endif\n"
  1654. "};"));
  1655. EXPECT_EQ("enum G {\n"
  1656. " One,\n"
  1657. "#if 0\n"
  1658. "Two,\n"
  1659. "#else\n"
  1660. " Three,\n"
  1661. "#endif\n"
  1662. " Four\n"
  1663. "};",
  1664. format("enum G {\n"
  1665. "One,\n"
  1666. "#if 0\n"
  1667. "Two,\n"
  1668. "#else\n"
  1669. "Three,\n"
  1670. "#endif\n"
  1671. "Four\n"
  1672. "};"));
  1673. EXPECT_EQ("enum H {\n"
  1674. " One,\n"
  1675. "#if 0\n"
  1676. "#ifdef Q\n"
  1677. "Two,\n"
  1678. "#else\n"
  1679. "Three,\n"
  1680. "#endif\n"
  1681. "#endif\n"
  1682. " Four\n"
  1683. "};",
  1684. format("enum H {\n"
  1685. "One,\n"
  1686. "#if 0\n"
  1687. "#ifdef Q\n"
  1688. "Two,\n"
  1689. "#else\n"
  1690. "Three,\n"
  1691. "#endif\n"
  1692. "#endif\n"
  1693. "Four\n"
  1694. "};"));
  1695. EXPECT_EQ("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. format("enum I {\n"
  1704. "One,\n"
  1705. "#if /* test */ 0 || 1\n"
  1706. "Two,\n"
  1707. "Three,\n"
  1708. "#endif\n"
  1709. "Four\n"
  1710. "};"));
  1711. EXPECT_EQ("enum J {\n"
  1712. " One,\n"
  1713. "#if 0\n"
  1714. "#if 0\n"
  1715. "Two,\n"
  1716. "#else\n"
  1717. "Three,\n"
  1718. "#endif\n"
  1719. "Four,\n"
  1720. "#endif\n"
  1721. " Five\n"
  1722. "};",
  1723. format("enum J {\n"
  1724. "One,\n"
  1725. "#if 0\n"
  1726. "#if 0\n"
  1727. "Two,\n"
  1728. "#else\n"
  1729. "Three,\n"
  1730. "#endif\n"
  1731. "Four,\n"
  1732. "#endif\n"
  1733. "Five\n"
  1734. "};"));
  1735. }
  1736. //===----------------------------------------------------------------------===//
  1737. // Tests for classes, namespaces, etc.
  1738. //===----------------------------------------------------------------------===//
  1739. TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
  1740. verifyFormat("class A {};");
  1741. }
  1742. TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
  1743. verifyFormat("class A {\n"
  1744. "public:\n"
  1745. "public: // comment\n"
  1746. "protected:\n"
  1747. "private:\n"
  1748. " void f() {}\n"
  1749. "};");
  1750. verifyGoogleFormat("class A {\n"
  1751. " public:\n"
  1752. " protected:\n"
  1753. " private:\n"
  1754. " void f() {}\n"
  1755. "};");
  1756. verifyFormat("class A {\n"
  1757. "public slots:\n"
  1758. " void f() {}\n"
  1759. "public Q_SLOTS:\n"
  1760. " void f() {}\n"
  1761. "};");
  1762. }
  1763. TEST_F(FormatTest, SeparatesLogicalBlocks) {
  1764. EXPECT_EQ("class A {\n"
  1765. "public:\n"
  1766. " void f();\n"
  1767. "\n"
  1768. "private:\n"
  1769. " void g() {}\n"
  1770. " // test\n"
  1771. "protected:\n"
  1772. " int h;\n"
  1773. "};",
  1774. format("class A {\n"
  1775. "public:\n"
  1776. "void f();\n"
  1777. "private:\n"
  1778. "void g() {}\n"
  1779. "// test\n"
  1780. "protected:\n"
  1781. "int h;\n"
  1782. "};"));
  1783. EXPECT_EQ("class A {\n"
  1784. "protected:\n"
  1785. "public:\n"
  1786. " void f();\n"
  1787. "};",
  1788. format("class A {\n"
  1789. "protected:\n"
  1790. "\n"
  1791. "public:\n"
  1792. "\n"
  1793. " void f();\n"
  1794. "};"));
  1795. }
  1796. TEST_F(FormatTest, FormatsClasses) {
  1797. verifyFormat("class A : public B {};");
  1798. verifyFormat("class A : public ::B {};");
  1799. verifyFormat(
  1800. "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1801. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1802. verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
  1803. " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1804. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1805. verifyFormat(
  1806. "class A : public B, public C, public D, public E, public F {};");
  1807. verifyFormat("class AAAAAAAAAAAA : public B,\n"
  1808. " public C,\n"
  1809. " public D,\n"
  1810. " public E,\n"
  1811. " public F,\n"
  1812. " public G {};");
  1813. verifyFormat("class\n"
  1814. " ReallyReallyLongClassName {\n"
  1815. " int i;\n"
  1816. "};",
  1817. getLLVMStyleWithColumns(32));
  1818. verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
  1819. " aaaaaaaaaaaaaaaa> {};");
  1820. verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
  1821. " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
  1822. " aaaaaaaaaaaaaaaaaaaaaa> {};");
  1823. verifyFormat("template <class R, class C>\n"
  1824. "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
  1825. " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
  1826. verifyFormat("class ::A::B {};");
  1827. }
  1828. TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
  1829. verifyFormat("class A {\n} a, b;");
  1830. verifyFormat("struct A {\n} a, b;");
  1831. verifyFormat("union A {\n} a;");
  1832. }
  1833. TEST_F(FormatTest, FormatsEnum) {
  1834. verifyFormat("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. verifyGoogleFormat("enum {\n"
  1843. " Zero,\n"
  1844. " One = 1,\n"
  1845. " Two = One + 1,\n"
  1846. " Three = (One + Two),\n"
  1847. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1848. " Five = (One, Two, Three, Four, 5)\n"
  1849. "};");
  1850. verifyFormat("enum Enum {};");
  1851. verifyFormat("enum {};");
  1852. verifyFormat("enum X E {} d;");
  1853. verifyFormat("enum __attribute__((...)) E {} d;");
  1854. verifyFormat("enum __declspec__((...)) E {} d;");
  1855. verifyFormat("enum X f() {\n a();\n return 42;\n}");
  1856. verifyFormat("enum {\n"
  1857. " Bar = Foo<int, int>::value\n"
  1858. "};",
  1859. getLLVMStyleWithColumns(30));
  1860. verifyFormat("enum ShortEnum { A, B, C };");
  1861. verifyGoogleFormat("enum ShortEnum { A, B, C };");
  1862. EXPECT_EQ("enum KeepEmptyLines {\n"
  1863. " ONE,\n"
  1864. "\n"
  1865. " TWO,\n"
  1866. "\n"
  1867. " THREE\n"
  1868. "}",
  1869. format("enum KeepEmptyLines {\n"
  1870. " ONE,\n"
  1871. "\n"
  1872. " TWO,\n"
  1873. "\n"
  1874. "\n"
  1875. " THREE\n"
  1876. "}"));
  1877. verifyFormat("enum E { // comment\n"
  1878. " ONE,\n"
  1879. " TWO\n"
  1880. "};\n"
  1881. "int i;");
  1882. }
  1883. TEST_F(FormatTest, FormatsEnumsWithErrors) {
  1884. verifyFormat("enum Type {\n"
  1885. " One = 0; // These semicolons should be commas.\n"
  1886. " Two = 1;\n"
  1887. "};");
  1888. verifyFormat("namespace n {\n"
  1889. "enum Type {\n"
  1890. " One,\n"
  1891. " Two, // missing };\n"
  1892. " int i;\n"
  1893. "}\n"
  1894. "void g() {}");
  1895. }
  1896. TEST_F(FormatTest, FormatsEnumStruct) {
  1897. verifyFormat("enum struct {\n"
  1898. " Zero,\n"
  1899. " One = 1,\n"
  1900. " Two = One + 1,\n"
  1901. " Three = (One + Two),\n"
  1902. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1903. " Five = (One, Two, Three, Four, 5)\n"
  1904. "};");
  1905. verifyFormat("enum struct Enum {};");
  1906. verifyFormat("enum struct {};");
  1907. verifyFormat("enum struct X E {} d;");
  1908. verifyFormat("enum struct __attribute__((...)) E {} d;");
  1909. verifyFormat("enum struct __declspec__((...)) E {} d;");
  1910. verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
  1911. }
  1912. TEST_F(FormatTest, FormatsEnumClass) {
  1913. verifyFormat("enum class {\n"
  1914. " Zero,\n"
  1915. " One = 1,\n"
  1916. " Two = One + 1,\n"
  1917. " Three = (One + Two),\n"
  1918. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1919. " Five = (One, Two, Three, Four, 5)\n"
  1920. "};");
  1921. verifyFormat("enum class Enum {};");
  1922. verifyFormat("enum class {};");
  1923. verifyFormat("enum class X E {} d;");
  1924. verifyFormat("enum class __attribute__((...)) E {} d;");
  1925. verifyFormat("enum class __declspec__((...)) E {} d;");
  1926. verifyFormat("enum class X f() {\n a();\n return 42;\n}");
  1927. }
  1928. TEST_F(FormatTest, FormatsEnumTypes) {
  1929. verifyFormat("enum X : int {\n"
  1930. " A, // Force multiple lines.\n"
  1931. " B\n"
  1932. "};");
  1933. verifyFormat("enum X : int { A, B };");
  1934. verifyFormat("enum X : std::uint32_t { A, B };");
  1935. }
  1936. TEST_F(FormatTest, FormatsNSEnums) {
  1937. verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
  1938. verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
  1939. " // Information about someDecentlyLongValue.\n"
  1940. " someDecentlyLongValue,\n"
  1941. " // Information about anotherDecentlyLongValue.\n"
  1942. " anotherDecentlyLongValue,\n"
  1943. " // Information about aThirdDecentlyLongValue.\n"
  1944. " aThirdDecentlyLongValue\n"
  1945. "};");
  1946. verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
  1947. " a = 1,\n"
  1948. " b = 2,\n"
  1949. " c = 3,\n"
  1950. "};");
  1951. verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
  1952. " a = 1,\n"
  1953. " b = 2,\n"
  1954. " c = 3,\n"
  1955. "};");
  1956. verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
  1957. " a = 1,\n"
  1958. " b = 2,\n"
  1959. " c = 3,\n"
  1960. "};");
  1961. }
  1962. TEST_F(FormatTest, FormatsBitfields) {
  1963. verifyFormat("struct Bitfields {\n"
  1964. " unsigned sClass : 8;\n"
  1965. " unsigned ValueKind : 2;\n"
  1966. "};");
  1967. verifyFormat("struct A {\n"
  1968. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
  1969. " bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
  1970. "};");
  1971. }
  1972. TEST_F(FormatTest, FormatsNamespaces) {
  1973. verifyFormat("namespace some_namespace {\n"
  1974. "class A {};\n"
  1975. "void f() { f(); }\n"
  1976. "}");
  1977. verifyFormat("namespace {\n"
  1978. "class A {};\n"
  1979. "void f() { f(); }\n"
  1980. "}");
  1981. verifyFormat("inline namespace X {\n"
  1982. "class A {};\n"
  1983. "void f() { f(); }\n"
  1984. "}");
  1985. verifyFormat("using namespace some_namespace;\n"
  1986. "class A {};\n"
  1987. "void f() { f(); }");
  1988. // This code is more common than we thought; if we
  1989. // layout this correctly the semicolon will go into
  1990. // its own line, which is undesirable.
  1991. verifyFormat("namespace {};");
  1992. verifyFormat("namespace {\n"
  1993. "class A {};\n"
  1994. "};");
  1995. verifyFormat("namespace {\n"
  1996. "int SomeVariable = 0; // comment\n"
  1997. "} // namespace");
  1998. EXPECT_EQ("#ifndef HEADER_GUARD\n"
  1999. "#define HEADER_GUARD\n"
  2000. "namespace my_namespace {\n"
  2001. "int i;\n"
  2002. "} // my_namespace\n"
  2003. "#endif // HEADER_GUARD",
  2004. format("#ifndef HEADER_GUARD\n"
  2005. " #define HEADER_GUARD\n"
  2006. " namespace my_namespace {\n"
  2007. "int i;\n"
  2008. "} // my_namespace\n"
  2009. "#endif // HEADER_GUARD"));
  2010. FormatStyle Style = getLLVMStyle();
  2011. Style.NamespaceIndentation = FormatStyle::NI_All;
  2012. EXPECT_EQ("namespace out {\n"
  2013. " int i;\n"
  2014. " namespace in {\n"
  2015. " int i;\n"
  2016. " } // namespace\n"
  2017. "} // namespace",
  2018. format("namespace out {\n"
  2019. "int i;\n"
  2020. "namespace in {\n"
  2021. "int i;\n"
  2022. "} // namespace\n"
  2023. "} // namespace",
  2024. Style));
  2025. Style.NamespaceIndentation = FormatStyle::NI_Inner;
  2026. EXPECT_EQ("namespace out {\n"
  2027. "int i;\n"
  2028. "namespace in {\n"
  2029. " int i;\n"
  2030. "} // namespace\n"
  2031. "} // namespace",
  2032. format("namespace out {\n"
  2033. "int i;\n"
  2034. "namespace in {\n"
  2035. "int i;\n"
  2036. "} // namespace\n"
  2037. "} // namespace",
  2038. Style));
  2039. }
  2040. TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
  2041. TEST_F(FormatTest, FormatsInlineASM) {
  2042. verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
  2043. verifyFormat("asm(\"nop\" ::: \"memory\");");
  2044. verifyFormat(
  2045. "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
  2046. " \"cpuid\\n\\t\"\n"
  2047. " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
  2048. " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
  2049. " : \"a\"(value));");
  2050. EXPECT_EQ(
  2051. "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
  2052. " __asm {\n"
  2053. " mov edx,[that] // vtable in edx\n"
  2054. " mov eax,methodIndex\n"
  2055. " call [edx][eax*4] // stdcall\n"
  2056. " }\n"
  2057. "}",
  2058. format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
  2059. " __asm {\n"
  2060. " mov edx,[that] // vtable in edx\n"
  2061. " mov eax,methodIndex\n"
  2062. " call [edx][eax*4] // stdcall\n"
  2063. " }\n"
  2064. "}"));
  2065. verifyFormat("void function() {\n"
  2066. " // comment\n"
  2067. " asm(\"\");\n"
  2068. "}");
  2069. }
  2070. TEST_F(FormatTest, FormatTryCatch) {
  2071. verifyFormat("try {\n"
  2072. " throw a * b;\n"
  2073. "} catch (int a) {\n"
  2074. " // Do nothing.\n"
  2075. "} catch (...) {\n"
  2076. " exit(42);\n"
  2077. "}");
  2078. // Function-level try statements.
  2079. verifyFormat("int f() try { return 4; } catch (...) {\n"
  2080. " return 5;\n"
  2081. "}");
  2082. verifyFormat("class A {\n"
  2083. " int a;\n"
  2084. " A() try : a(0) {\n"
  2085. " } catch (...) {\n"
  2086. " throw;\n"
  2087. " }\n"
  2088. "};\n");
  2089. }
  2090. TEST_F(FormatTest, IncompleteTryCatchBlocks) {
  2091. verifyFormat("try {\n"
  2092. " f();\n"
  2093. "} catch {\n"
  2094. " g();\n"
  2095. "}");
  2096. verifyFormat("try {\n"
  2097. " f();\n"
  2098. "} catch (A a) MACRO(x) {\n"
  2099. " g();\n"
  2100. "} catch (B b) MACRO(x) {\n"
  2101. " g();\n"
  2102. "}");
  2103. }
  2104. TEST_F(FormatTest, FormatTryCatchBraceStyles) {
  2105. FormatStyle Style = getLLVMStyle();
  2106. Style.BreakBeforeBraces = FormatStyle::BS_Attach;
  2107. verifyFormat("try {\n"
  2108. " // something\n"
  2109. "} catch (...) {\n"
  2110. " // something\n"
  2111. "}",
  2112. Style);
  2113. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  2114. verifyFormat("try {\n"
  2115. " // something\n"
  2116. "}\n"
  2117. "catch (...) {\n"
  2118. " // something\n"
  2119. "}",
  2120. Style);
  2121. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  2122. verifyFormat("try\n"
  2123. "{\n"
  2124. " // something\n"
  2125. "}\n"
  2126. "catch (...)\n"
  2127. "{\n"
  2128. " // something\n"
  2129. "}",
  2130. Style);
  2131. Style.BreakBeforeBraces = FormatStyle::BS_GNU;
  2132. verifyFormat("try\n"
  2133. " {\n"
  2134. " // something\n"
  2135. " }\n"
  2136. "catch (...)\n"
  2137. " {\n"
  2138. " // something\n"
  2139. " }",
  2140. Style);
  2141. }
  2142. TEST_F(FormatTest, FormatObjCTryCatch) {
  2143. verifyFormat("@try {\n"
  2144. " f();\n"
  2145. "}\n"
  2146. "@catch (NSException e) {\n"
  2147. " @throw;\n"
  2148. "}\n"
  2149. "@finally {\n"
  2150. " exit(42);\n"
  2151. "}");
  2152. }
  2153. TEST_F(FormatTest, StaticInitializers) {
  2154. verifyFormat("static SomeClass SC = {1, 'a'};");
  2155. verifyFormat(
  2156. "static SomeClass WithALoooooooooooooooooooongName = {\n"
  2157. " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
  2158. // Here, everything other than the "}" would fit on a line.
  2159. verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
  2160. " 10000000000000000000000000};");
  2161. EXPECT_EQ("S s = {a,\n"
  2162. "\n"
  2163. " b};",
  2164. format("S s = {\n"
  2165. " a,\n"
  2166. "\n"
  2167. " b\n"
  2168. "};"));
  2169. // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
  2170. // line. However, the formatting looks a bit off and this probably doesn't
  2171. // happen often in practice.
  2172. verifyFormat("static int Variable[1] = {\n"
  2173. " {1000000000000000000000000000000000000}};",
  2174. getLLVMStyleWithColumns(40));
  2175. }
  2176. TEST_F(FormatTest, DesignatedInitializers) {
  2177. verifyFormat("const struct A a = {.a = 1, .b = 2};");
  2178. verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
  2179. " .bbbbbbbbbb = 2,\n"
  2180. " .cccccccccc = 3,\n"
  2181. " .dddddddddd = 4,\n"
  2182. " .eeeeeeeeee = 5};");
  2183. verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
  2184. " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
  2185. " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
  2186. " .ccccccccccccccccccccccccccc = 3,\n"
  2187. " .ddddddddddddddddddddddddddd = 4,\n"
  2188. " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
  2189. verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
  2190. }
  2191. TEST_F(FormatTest, NestedStaticInitializers) {
  2192. verifyFormat("static A x = {{{}}};\n");
  2193. verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
  2194. " {init1, init2, init3, init4}}};",
  2195. getLLVMStyleWithColumns(50));
  2196. verifyFormat("somes Status::global_reps[3] = {\n"
  2197. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  2198. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  2199. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
  2200. getLLVMStyleWithColumns(60));
  2201. verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
  2202. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  2203. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  2204. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
  2205. verifyFormat(
  2206. "CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
  2207. " {rect.fRight - rect.fLeft, rect.fBottom - rect.fTop}};");
  2208. verifyFormat(
  2209. "SomeArrayOfSomeType a = {\n"
  2210. " {{1, 2, 3},\n"
  2211. " {1, 2, 3},\n"
  2212. " {111111111111111111111111111111, 222222222222222222222222222222,\n"
  2213. " 333333333333333333333333333333},\n"
  2214. " {1, 2, 3},\n"
  2215. " {1, 2, 3}}};");
  2216. verifyFormat(
  2217. "SomeArrayOfSomeType a = {\n"
  2218. " {{1, 2, 3}},\n"
  2219. " {{1, 2, 3}},\n"
  2220. " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
  2221. " 333333333333333333333333333333}},\n"
  2222. " {{1, 2, 3}},\n"
  2223. " {{1, 2, 3}}};");
  2224. verifyFormat(
  2225. "struct {\n"
  2226. " unsigned bit;\n"
  2227. " const char *const name;\n"
  2228. "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
  2229. " {kOsWin, \"Windows\"},\n"
  2230. " {kOsLinux, \"Linux\"},\n"
  2231. " {kOsCrOS, \"Chrome OS\"}};");
  2232. verifyFormat(
  2233. "struct {\n"
  2234. " unsigned bit;\n"
  2235. " const char *const name;\n"
  2236. "} kBitsToOs[] = {\n"
  2237. " {kOsMac, \"Mac\"},\n"
  2238. " {kOsWin, \"Windows\"},\n"
  2239. " {kOsLinux, \"Linux\"},\n"
  2240. " {kOsCrOS, \"Chrome OS\"},\n"
  2241. "};");
  2242. }
  2243. TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
  2244. verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2245. " \\\n"
  2246. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
  2247. }
  2248. TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
  2249. verifyFormat("virtual void write(ELFWriter *writerrr,\n"
  2250. " OwningPtr<FileOutputBuffer> &buffer) = 0;");
  2251. }
  2252. TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
  2253. verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
  2254. getLLVMStyleWithColumns(40));
  2255. verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  2256. getLLVMStyleWithColumns(40));
  2257. EXPECT_EQ("#define Q \\\n"
  2258. " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n"
  2259. " \"aaaaaaaa.cpp\"",
  2260. format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  2261. getLLVMStyleWithColumns(40)));
  2262. }
  2263. TEST_F(FormatTest, UnderstandsLinePPDirective) {
  2264. EXPECT_EQ("# 123 \"A string literal\"",
  2265. format(" # 123 \"A string literal\""));
  2266. }
  2267. TEST_F(FormatTest, LayoutUnknownPPDirective) {
  2268. EXPECT_EQ("#;", format("#;"));
  2269. verifyFormat("#\n;\n;\n;");
  2270. }
  2271. TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
  2272. EXPECT_EQ("#line 42 \"test\"\n",
  2273. format("# \\\n line \\\n 42 \\\n \"test\"\n"));
  2274. EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n",
  2275. getLLVMStyleWithColumns(12)));
  2276. }
  2277. TEST_F(FormatTest, EndOfFileEndsPPDirective) {
  2278. EXPECT_EQ("#line 42 \"test\"",
  2279. format("# \\\n line \\\n 42 \\\n \"test\""));
  2280. EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B"));
  2281. }
  2282. TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
  2283. verifyFormat("#define A \\x20");
  2284. verifyFormat("#define A \\ x20");
  2285. EXPECT_EQ("#define A \\ x20", format("#define A \\ x20"));
  2286. verifyFormat("#define A ''");
  2287. verifyFormat("#define A ''qqq");
  2288. verifyFormat("#define A `qqq");
  2289. verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
  2290. EXPECT_EQ("const char *c = STRINGIFY(\n"
  2291. "\\na : b);",
  2292. format("const char * c = STRINGIFY(\n"
  2293. "\\na : b);"));
  2294. }
  2295. TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
  2296. verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
  2297. verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
  2298. verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
  2299. // FIXME: We never break before the macro name.
  2300. verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
  2301. verifyFormat("#define A A\n#define A A");
  2302. verifyFormat("#define A(X) A\n#define A A");
  2303. verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
  2304. verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
  2305. }
  2306. TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
  2307. EXPECT_EQ("// somecomment\n"
  2308. "#include \"a.h\"\n"
  2309. "#define A( \\\n"
  2310. " A, B)\n"
  2311. "#include \"b.h\"\n"
  2312. "// somecomment\n",
  2313. format(" // somecomment\n"
  2314. " #include \"a.h\"\n"
  2315. "#define A(A,\\\n"
  2316. " B)\n"
  2317. " #include \"b.h\"\n"
  2318. " // somecomment\n",
  2319. getLLVMStyleWithColumns(13)));
  2320. }
  2321. TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
  2322. TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
  2323. EXPECT_EQ("#define A \\\n"
  2324. " c; \\\n"
  2325. " e;\n"
  2326. "f;",
  2327. format("#define A c; e;\n"
  2328. "f;",
  2329. getLLVMStyleWithColumns(14)));
  2330. }
  2331. TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
  2332. TEST_F(FormatTest, AlwaysFormatsEntireMacroDefinitions) {
  2333. EXPECT_EQ("int i;\n"
  2334. "#define A \\\n"
  2335. " int i; \\\n"
  2336. " int j\n"
  2337. "int k;",
  2338. format("int i;\n"
  2339. "#define A \\\n"
  2340. " int i ; \\\n"
  2341. " int j\n"
  2342. "int k;",
  2343. 8, 0, getGoogleStyle())); // 8: position of "#define".
  2344. EXPECT_EQ("int i;\n"
  2345. "#define A \\\n"
  2346. " int i; \\\n"
  2347. " int j\n"
  2348. "int k;",
  2349. format("int i;\n"
  2350. "#define A \\\n"
  2351. " int i ; \\\n"
  2352. " int j\n"
  2353. "int k;",
  2354. 45, 0, getGoogleStyle())); // 45: position of "j".
  2355. }
  2356. TEST_F(FormatTest, MacroDefinitionInsideStatement) {
  2357. EXPECT_EQ("int x,\n"
  2358. "#define A\n"
  2359. " y;",
  2360. format("int x,\n#define A\ny;"));
  2361. }
  2362. TEST_F(FormatTest, HashInMacroDefinition) {
  2363. EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
  2364. verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
  2365. verifyFormat("#define A \\\n"
  2366. " { \\\n"
  2367. " f(#c); \\\n"
  2368. " }",
  2369. getLLVMStyleWithColumns(11));
  2370. verifyFormat("#define A(X) \\\n"
  2371. " void function##X()",
  2372. getLLVMStyleWithColumns(22));
  2373. verifyFormat("#define A(a, b, c) \\\n"
  2374. " void a##b##c()",
  2375. getLLVMStyleWithColumns(22));
  2376. verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
  2377. }
  2378. TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
  2379. EXPECT_EQ("#define A (x)", format("#define A (x)"));
  2380. EXPECT_EQ("#define A(x)", format("#define A(x)"));
  2381. }
  2382. TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
  2383. EXPECT_EQ("#define A b;", format("#define A \\\n"
  2384. " \\\n"
  2385. " b;",
  2386. getLLVMStyleWithColumns(25)));
  2387. EXPECT_EQ("#define A \\\n"
  2388. " \\\n"
  2389. " a; \\\n"
  2390. " b;",
  2391. format("#define A \\\n"
  2392. " \\\n"
  2393. " a; \\\n"
  2394. " b;",
  2395. getLLVMStyleWithColumns(11)));
  2396. EXPECT_EQ("#define A \\\n"
  2397. " a; \\\n"
  2398. " \\\n"
  2399. " b;",
  2400. format("#define A \\\n"
  2401. " a; \\\n"
  2402. " \\\n"
  2403. " b;",
  2404. getLLVMStyleWithColumns(11)));
  2405. }
  2406. TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
  2407. verifyFormat("#define A :");
  2408. verifyFormat("#define SOMECASES \\\n"
  2409. " case 1: \\\n"
  2410. " case 2\n",
  2411. getLLVMStyleWithColumns(20));
  2412. verifyFormat("#define A template <typename T>");
  2413. verifyFormat("#define STR(x) #x\n"
  2414. "f(STR(this_is_a_string_literal{));");
  2415. verifyFormat("#pragma omp threadprivate( \\\n"
  2416. " y)), // expected-warning",
  2417. getLLVMStyleWithColumns(28));
  2418. }
  2419. TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
  2420. verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
  2421. EXPECT_EQ("class A : public QObject {\n"
  2422. " Q_OBJECT\n"
  2423. "\n"
  2424. " A() {}\n"
  2425. "};",
  2426. format("class A : public QObject {\n"
  2427. " Q_OBJECT\n"
  2428. "\n"
  2429. " A() {\n}\n"
  2430. "} ;"));
  2431. EXPECT_EQ("SOME_MACRO\n"
  2432. "namespace {\n"
  2433. "void f();\n"
  2434. "}",
  2435. format("SOME_MACRO\n"
  2436. " namespace {\n"
  2437. "void f( );\n"
  2438. "}"));
  2439. // Only if the identifier contains at least 5 characters.
  2440. EXPECT_EQ("HTTP f();",
  2441. format("HTTP\nf();"));
  2442. EXPECT_EQ("MACRO\nf();",
  2443. format("MACRO\nf();"));
  2444. // Only if everything is upper case.
  2445. EXPECT_EQ("class A : public QObject {\n"
  2446. " Q_Object A() {}\n"
  2447. "};",
  2448. format("class A : public QObject {\n"
  2449. " Q_Object\n"
  2450. " A() {\n}\n"
  2451. "} ;"));
  2452. // Only if the next line can actually start an unwrapped line.
  2453. EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
  2454. format("SOME_WEIRD_LOG_MACRO\n"
  2455. "<< SomeThing;"));
  2456. verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
  2457. "(n, buffers))\n", getChromiumStyle(FormatStyle::LK_Cpp));
  2458. }
  2459. TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
  2460. EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  2461. "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  2462. "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  2463. "class X {};\n"
  2464. "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  2465. "int *createScopDetectionPass() { return 0; }",
  2466. format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  2467. " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  2468. " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  2469. " class X {};\n"
  2470. " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  2471. " int *createScopDetectionPass() { return 0; }"));
  2472. // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
  2473. // braces, so that inner block is indented one level more.
  2474. EXPECT_EQ("int q() {\n"
  2475. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  2476. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  2477. " IPC_END_MESSAGE_MAP()\n"
  2478. "}",
  2479. format("int q() {\n"
  2480. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  2481. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  2482. " IPC_END_MESSAGE_MAP()\n"
  2483. "}"));
  2484. // Same inside macros.
  2485. EXPECT_EQ("#define LIST(L) \\\n"
  2486. " L(A) \\\n"
  2487. " L(B) \\\n"
  2488. " L(C)",
  2489. format("#define LIST(L) \\\n"
  2490. " L(A) \\\n"
  2491. " L(B) \\\n"
  2492. " L(C)",
  2493. getGoogleStyle()));
  2494. // These must not be recognized as macros.
  2495. EXPECT_EQ("int q() {\n"
  2496. " f(x);\n"
  2497. " f(x) {}\n"
  2498. " f(x)->g();\n"
  2499. " f(x)->*g();\n"
  2500. " f(x).g();\n"
  2501. " f(x) = x;\n"
  2502. " f(x) += x;\n"
  2503. " f(x) -= x;\n"
  2504. " f(x) *= x;\n"
  2505. " f(x) /= x;\n"
  2506. " f(x) %= x;\n"
  2507. " f(x) &= x;\n"
  2508. " f(x) |= x;\n"
  2509. " f(x) ^= x;\n"
  2510. " f(x) >>= x;\n"
  2511. " f(x) <<= x;\n"
  2512. " f(x)[y].z();\n"
  2513. " LOG(INFO) << x;\n"
  2514. " ifstream(x) >> x;\n"
  2515. "}\n",
  2516. format("int q() {\n"
  2517. " f(x)\n;\n"
  2518. " f(x)\n {}\n"
  2519. " f(x)\n->g();\n"
  2520. " f(x)\n->*g();\n"
  2521. " f(x)\n.g();\n"
  2522. " f(x)\n = x;\n"
  2523. " f(x)\n += x;\n"
  2524. " f(x)\n -= x;\n"
  2525. " f(x)\n *= x;\n"
  2526. " f(x)\n /= x;\n"
  2527. " f(x)\n %= x;\n"
  2528. " f(x)\n &= x;\n"
  2529. " f(x)\n |= x;\n"
  2530. " f(x)\n ^= x;\n"
  2531. " f(x)\n >>= x;\n"
  2532. " f(x)\n <<= x;\n"
  2533. " f(x)\n[y].z();\n"
  2534. " LOG(INFO)\n << x;\n"
  2535. " ifstream(x)\n >> x;\n"
  2536. "}\n"));
  2537. EXPECT_EQ("int q() {\n"
  2538. " F(x)\n"
  2539. " if (1) {\n"
  2540. " }\n"
  2541. " F(x)\n"
  2542. " while (1) {\n"
  2543. " }\n"
  2544. " F(x)\n"
  2545. " G(x);\n"
  2546. " F(x)\n"
  2547. " try {\n"
  2548. " Q();\n"
  2549. " } catch (...) {\n"
  2550. " }\n"
  2551. "}\n",
  2552. format("int q() {\n"
  2553. "F(x)\n"
  2554. "if (1) {}\n"
  2555. "F(x)\n"
  2556. "while (1) {}\n"
  2557. "F(x)\n"
  2558. "G(x);\n"
  2559. "F(x)\n"
  2560. "try { Q(); } catch (...) {}\n"
  2561. "}\n"));
  2562. EXPECT_EQ("class A {\n"
  2563. " A() : t(0) {}\n"
  2564. " A(int i) noexcept() : {}\n"
  2565. " A(X x)\n" // FIXME: function-level try blocks are broken.
  2566. " try : t(0) {\n"
  2567. " } catch (...) {\n"
  2568. " }\n"
  2569. "};",
  2570. format("class A {\n"
  2571. " A()\n : t(0) {}\n"
  2572. " A(int i)\n noexcept() : {}\n"
  2573. " A(X x)\n"
  2574. " try : t(0) {} catch (...) {}\n"
  2575. "};"));
  2576. EXPECT_EQ(
  2577. "class SomeClass {\n"
  2578. "public:\n"
  2579. " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2580. "};",
  2581. format("class SomeClass {\n"
  2582. "public:\n"
  2583. " SomeClass()\n"
  2584. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2585. "};"));
  2586. EXPECT_EQ(
  2587. "class SomeClass {\n"
  2588. "public:\n"
  2589. " SomeClass()\n"
  2590. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2591. "};",
  2592. format("class SomeClass {\n"
  2593. "public:\n"
  2594. " SomeClass()\n"
  2595. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2596. "};", getLLVMStyleWithColumns(40)));
  2597. }
  2598. TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
  2599. verifyFormat("#define A \\\n"
  2600. " f({ \\\n"
  2601. " g(); \\\n"
  2602. " });", getLLVMStyleWithColumns(11));
  2603. }
  2604. TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
  2605. EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
  2606. }
  2607. TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
  2608. verifyFormat("{\n { a #c; }\n}");
  2609. }
  2610. TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
  2611. EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
  2612. format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
  2613. EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
  2614. format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
  2615. }
  2616. TEST_F(FormatTest, EscapedNewlineAtStartOfToken) {
  2617. EXPECT_EQ(
  2618. "#define A \\\n int i; \\\n int j;",
  2619. format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
  2620. EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
  2621. }
  2622. TEST_F(FormatTest, NoEscapedNewlineHandlingInBlockComments) {
  2623. EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/"));
  2624. }
  2625. TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
  2626. verifyFormat("#define A \\\n"
  2627. " int v( \\\n"
  2628. " a); \\\n"
  2629. " int i;",
  2630. getLLVMStyleWithColumns(11));
  2631. }
  2632. TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
  2633. EXPECT_EQ(
  2634. "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2635. " \\\n"
  2636. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2637. "\n"
  2638. "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2639. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
  2640. format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2641. "\\\n"
  2642. "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2643. " \n"
  2644. " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2645. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
  2646. }
  2647. TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
  2648. EXPECT_EQ("int\n"
  2649. "#define A\n"
  2650. " a;",
  2651. format("int\n#define A\na;"));
  2652. verifyFormat("functionCallTo(\n"
  2653. " someOtherFunction(\n"
  2654. " withSomeParameters, whichInSequence,\n"
  2655. " areLongerThanALine(andAnotherCall,\n"
  2656. "#define A B\n"
  2657. " withMoreParamters,\n"
  2658. " whichStronglyInfluenceTheLayout),\n"
  2659. " andMoreParameters),\n"
  2660. " trailing);",
  2661. getLLVMStyleWithColumns(69));
  2662. verifyFormat("Foo::Foo()\n"
  2663. "#ifdef BAR\n"
  2664. " : baz(0)\n"
  2665. "#endif\n"
  2666. "{\n"
  2667. "}");
  2668. verifyFormat("void f() {\n"
  2669. " if (true)\n"
  2670. "#ifdef A\n"
  2671. " f(42);\n"
  2672. " x();\n"
  2673. "#else\n"
  2674. " g();\n"
  2675. " x();\n"
  2676. "#endif\n"
  2677. "}");
  2678. verifyFormat("void f(param1, param2,\n"
  2679. " param3,\n"
  2680. "#ifdef A\n"
  2681. " param4(param5,\n"
  2682. "#ifdef A1\n"
  2683. " param6,\n"
  2684. "#ifdef A2\n"
  2685. " param7),\n"
  2686. "#else\n"
  2687. " param8),\n"
  2688. " param9,\n"
  2689. "#endif\n"
  2690. " param10,\n"
  2691. "#endif\n"
  2692. " param11)\n"
  2693. "#else\n"
  2694. " param12)\n"
  2695. "#endif\n"
  2696. "{\n"
  2697. " x();\n"
  2698. "}",
  2699. getLLVMStyleWithColumns(28));
  2700. verifyFormat("#if 1\n"
  2701. "int i;");
  2702. verifyFormat(
  2703. "#if 1\n"
  2704. "#endif\n"
  2705. "#if 1\n"
  2706. "#else\n"
  2707. "#endif\n");
  2708. verifyFormat("DEBUG({\n"
  2709. " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2710. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  2711. "});\n"
  2712. "#if a\n"
  2713. "#else\n"
  2714. "#endif");
  2715. }
  2716. TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
  2717. verifyFormat("#endif\n"
  2718. "#if B");
  2719. }
  2720. TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
  2721. FormatStyle SingleLine = getLLVMStyle();
  2722. SingleLine.AllowShortIfStatementsOnASingleLine = true;
  2723. verifyFormat(
  2724. "#if 0\n"
  2725. "#elif 1\n"
  2726. "#endif\n"
  2727. "void foo() {\n"
  2728. " if (test) foo2();\n"
  2729. "}",
  2730. SingleLine);
  2731. }
  2732. TEST_F(FormatTest, LayoutBlockInsideParens) {
  2733. EXPECT_EQ("functionCall({ int i; });", format(" functionCall ( {int i;} );"));
  2734. EXPECT_EQ("functionCall({\n"
  2735. " int i;\n"
  2736. " int j;\n"
  2737. "});",
  2738. format(" functionCall ( {int i;int j;} );"));
  2739. EXPECT_EQ("functionCall({\n"
  2740. " int i;\n"
  2741. " int j;\n"
  2742. "}, aaaa, bbbb, cccc);",
  2743. format(" functionCall ( {int i;int j;}, aaaa, bbbb, cccc);"));
  2744. EXPECT_EQ("functionCall(\n"
  2745. " {\n"
  2746. " int i;\n"
  2747. " int j;\n"
  2748. " },\n"
  2749. " aaaa, bbbb, // comment\n"
  2750. " cccc);",
  2751. format(" functionCall ( {int i;int j;}, aaaa, bbbb, // comment\n"
  2752. "cccc);"));
  2753. EXPECT_EQ("functionCall(aaaa, bbbb, { int i; });",
  2754. format(" functionCall (aaaa, bbbb, {int i;});"));
  2755. EXPECT_EQ("functionCall(aaaa, bbbb, {\n"
  2756. " int i;\n"
  2757. " int j;\n"
  2758. "});",
  2759. format(" functionCall (aaaa, bbbb, {int i;int j;});"));
  2760. EXPECT_EQ("functionCall(aaaa, bbbb, { int i; });",
  2761. format(" functionCall (aaaa, bbbb, {int i;});"));
  2762. verifyFormat(
  2763. "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
  2764. " {\n"
  2765. " int i; // break\n"
  2766. " },\n"
  2767. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  2768. " ccccccccccccccccc));");
  2769. verifyFormat("DEBUG({\n"
  2770. " if (a)\n"
  2771. " f();\n"
  2772. "});");
  2773. }
  2774. TEST_F(FormatTest, LayoutBlockInsideStatement) {
  2775. EXPECT_EQ("SOME_MACRO { int i; }\n"
  2776. "int i;",
  2777. format(" SOME_MACRO {int i;} int i;"));
  2778. }
  2779. TEST_F(FormatTest, LayoutNestedBlocks) {
  2780. verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
  2781. " struct s {\n"
  2782. " int i;\n"
  2783. " };\n"
  2784. " s kBitsToOs[] = {{10}};\n"
  2785. " for (int i = 0; i < 10; ++i)\n"
  2786. " return;\n"
  2787. "}");
  2788. verifyFormat("call(parameter, {\n"
  2789. " something();\n"
  2790. " // Comment using all columns.\n"
  2791. " somethingelse();\n"
  2792. "});",
  2793. getLLVMStyleWithColumns(40));
  2794. verifyFormat("DEBUG( //\n"
  2795. " { f(); }, a);");
  2796. verifyFormat("DEBUG( //\n"
  2797. " {\n"
  2798. " f(); //\n"
  2799. " },\n"
  2800. " a);");
  2801. EXPECT_EQ("call(parameter, {\n"
  2802. " something();\n"
  2803. " // Comment too\n"
  2804. " // looooooooooong.\n"
  2805. " somethingElse();\n"
  2806. "});",
  2807. format("call(parameter, {\n"
  2808. " something();\n"
  2809. " // Comment too looooooooooong.\n"
  2810. " somethingElse();\n"
  2811. "});",
  2812. getLLVMStyleWithColumns(29)));
  2813. EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });"));
  2814. EXPECT_EQ("DEBUG({ // comment\n"
  2815. " int i;\n"
  2816. "});",
  2817. format("DEBUG({ // comment\n"
  2818. "int i;\n"
  2819. "});"));
  2820. EXPECT_EQ("DEBUG({\n"
  2821. " int i;\n"
  2822. "\n"
  2823. " // comment\n"
  2824. " int j;\n"
  2825. "});",
  2826. format("DEBUG({\n"
  2827. " int i;\n"
  2828. "\n"
  2829. " // comment\n"
  2830. " int j;\n"
  2831. "});"));
  2832. verifyFormat("DEBUG({\n"
  2833. " if (a)\n"
  2834. " return;\n"
  2835. "});");
  2836. verifyGoogleFormat("DEBUG({\n"
  2837. " if (a) return;\n"
  2838. "});");
  2839. FormatStyle Style = getGoogleStyle();
  2840. Style.ColumnLimit = 45;
  2841. verifyFormat("Debug(aaaaa, {\n"
  2842. " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
  2843. "}, a);",
  2844. Style);
  2845. }
  2846. TEST_F(FormatTest, IndividualStatementsOfNestedBlocks) {
  2847. EXPECT_EQ("DEBUG({\n"
  2848. " int i;\n"
  2849. " int j;\n"
  2850. "});",
  2851. format("DEBUG( {\n"
  2852. " int i;\n"
  2853. " int j;\n"
  2854. "} ) ;",
  2855. 20, 1, getLLVMStyle()));
  2856. EXPECT_EQ("DEBUG( {\n"
  2857. " int i;\n"
  2858. " int j;\n"
  2859. "} ) ;",
  2860. format("DEBUG( {\n"
  2861. " int i;\n"
  2862. " int j;\n"
  2863. "} ) ;",
  2864. 41, 1, getLLVMStyle()));
  2865. EXPECT_EQ("DEBUG( {\n"
  2866. " int i;\n"
  2867. " int j;\n"
  2868. "} ) ;",
  2869. format("DEBUG( {\n"
  2870. " int i;\n"
  2871. " int j;\n"
  2872. "} ) ;",
  2873. 41, 1, getLLVMStyle()));
  2874. EXPECT_EQ("DEBUG({\n"
  2875. " int i;\n"
  2876. " int j;\n"
  2877. "});",
  2878. format("DEBUG( {\n"
  2879. " int i;\n"
  2880. " int j;\n"
  2881. "} ) ;",
  2882. 20, 1, getLLVMStyle()));
  2883. EXPECT_EQ("Debug({\n"
  2884. " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2885. " return;\n"
  2886. " },\n"
  2887. " a);",
  2888. format("Debug({\n"
  2889. " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2890. " return;\n"
  2891. " },\n"
  2892. " a);",
  2893. 50, 1, getLLVMStyle()));
  2894. EXPECT_EQ("DEBUG({\n"
  2895. " DEBUG({\n"
  2896. " int a;\n"
  2897. " int b;\n"
  2898. " }) ;\n"
  2899. "});",
  2900. format("DEBUG({\n"
  2901. " DEBUG({\n"
  2902. " int a;\n"
  2903. " int b;\n" // Format this line only.
  2904. " }) ;\n" // Don't touch this line.
  2905. "});",
  2906. 35, 0, getLLVMStyle()));
  2907. EXPECT_EQ("DEBUG({\n"
  2908. " int a; //\n"
  2909. "});",
  2910. format("DEBUG({\n"
  2911. " int a; //\n"
  2912. "});",
  2913. 0, 0, getLLVMStyle()));
  2914. }
  2915. TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
  2916. EXPECT_EQ("{}", format("{}"));
  2917. verifyFormat("enum E {};");
  2918. verifyFormat("enum E {}");
  2919. }
  2920. //===----------------------------------------------------------------------===//
  2921. // Line break tests.
  2922. //===----------------------------------------------------------------------===//
  2923. TEST_F(FormatTest, PreventConfusingIndents) {
  2924. verifyFormat(
  2925. "void f() {\n"
  2926. " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
  2927. " parameter, parameter, parameter)),\n"
  2928. " SecondLongCall(parameter));\n"
  2929. "}");
  2930. verifyFormat(
  2931. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2932. " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2933. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2934. " aaaaaaaaaaaaaaaaaaaaaaaa);");
  2935. verifyFormat(
  2936. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2937. " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
  2938. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  2939. " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
  2940. verifyFormat(
  2941. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  2942. " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
  2943. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
  2944. " aaaaaaaaaaaaaaaaaaaaaaaa>;");
  2945. verifyFormat("int a = bbbb && ccc && fffff(\n"
  2946. "#define A Just forcing a new line\n"
  2947. " ddd);");
  2948. }
  2949. TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
  2950. verifyFormat(
  2951. "bool aaaaaaa =\n"
  2952. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
  2953. " bbbbbbbb();");
  2954. verifyFormat(
  2955. "bool aaaaaaa =\n"
  2956. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
  2957. " bbbbbbbb();");
  2958. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  2959. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
  2960. " ccccccccc == ddddddddddd;");
  2961. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  2962. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
  2963. " ccccccccc == ddddddddddd;");
  2964. verifyFormat(
  2965. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  2966. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
  2967. " ccccccccc == ddddddddddd;");
  2968. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  2969. " aaaaaa) &&\n"
  2970. " bbbbbb && cccccc;");
  2971. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  2972. " aaaaaa) >>\n"
  2973. " bbbbbb;");
  2974. verifyFormat("Whitespaces.addUntouchableComment(\n"
  2975. " SourceMgr.getSpellingColumnNumber(\n"
  2976. " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
  2977. " 1);");
  2978. verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2979. " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
  2980. " cccccc) {\n}");
  2981. verifyFormat("b = a &&\n"
  2982. " // Comment\n"
  2983. " b.c && d;");
  2984. // If the LHS of a comparison is not a binary expression itself, the
  2985. // additional linebreak confuses many people.
  2986. verifyFormat(
  2987. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2988. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
  2989. "}");
  2990. verifyFormat(
  2991. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2992. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  2993. "}");
  2994. verifyFormat(
  2995. "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
  2996. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  2997. "}");
  2998. // Even explicit parentheses stress the precedence enough to make the
  2999. // additional break unnecessary.
  3000. verifyFormat(
  3001. "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3002. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  3003. "}");
  3004. // This cases is borderline, but with the indentation it is still readable.
  3005. verifyFormat(
  3006. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3007. " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3008. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  3009. "}",
  3010. getLLVMStyleWithColumns(75));
  3011. // If the LHS is a binary expression, we should still use the additional break
  3012. // as otherwise the formatting hides the operator precedence.
  3013. verifyFormat(
  3014. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3015. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3016. " 5) {\n"
  3017. "}");
  3018. FormatStyle OnePerLine = getLLVMStyle();
  3019. OnePerLine.BinPackParameters = false;
  3020. verifyFormat(
  3021. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3022. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3023. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
  3024. OnePerLine);
  3025. }
  3026. TEST_F(FormatTest, ExpressionIndentation) {
  3027. verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3028. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3029. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3030. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3031. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
  3032. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
  3033. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3034. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
  3035. " ccccccccccccccccccccccccccccccccccccccccc;");
  3036. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3037. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3038. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3039. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3040. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3041. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3042. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3043. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3044. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3045. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3046. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3047. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3048. verifyFormat("if () {\n"
  3049. "} else if (aaaaa &&\n"
  3050. " bbbbb > // break\n"
  3051. " ccccc) {\n"
  3052. "}");
  3053. // Presence of a trailing comment used to change indentation of b.
  3054. verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
  3055. " b;\n"
  3056. "return aaaaaaaaaaaaaaaaaaa +\n"
  3057. " b; //",
  3058. getLLVMStyleWithColumns(30));
  3059. }
  3060. TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
  3061. // Not sure what the best system is here. Like this, the LHS can be found
  3062. // immediately above an operator (everything with the same or a higher
  3063. // indent). The RHS is aligned right of the operator and so compasses
  3064. // everything until something with the same indent as the operator is found.
  3065. // FIXME: Is this a good system?
  3066. FormatStyle Style = getLLVMStyle();
  3067. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  3068. verifyFormat(
  3069. "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3070. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3071. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3072. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3073. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3074. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3075. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3076. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3077. " > ccccccccccccccccccccccccccccccccccccccccc;",
  3078. Style);
  3079. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3080. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3081. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3082. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3083. Style);
  3084. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3085. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3086. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3087. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3088. Style);
  3089. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3090. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3091. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3092. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3093. Style);
  3094. verifyFormat("if () {\n"
  3095. "} else if (aaaaa\n"
  3096. " && bbbbb // break\n"
  3097. " > ccccc) {\n"
  3098. "}",
  3099. Style);
  3100. verifyFormat("return (a)\n"
  3101. " // comment\n"
  3102. " + b;",
  3103. Style);
  3104. verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3105. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3106. " + cc;",
  3107. Style);
  3108. // Forced by comments.
  3109. verifyFormat(
  3110. "unsigned ContentSize =\n"
  3111. " sizeof(int16_t) // DWARF ARange version number\n"
  3112. " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
  3113. " + sizeof(int8_t) // Pointer Size (in bytes)\n"
  3114. " + sizeof(int8_t); // Segment Size (in bytes)");
  3115. verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
  3116. " == boost::fusion::at_c<1>(iiii).second;",
  3117. Style);
  3118. Style.ColumnLimit = 60;
  3119. verifyFormat("zzzzzzzzzz\n"
  3120. " = bbbbbbbbbbbbbbbbb\n"
  3121. " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3122. Style);
  3123. }
  3124. TEST_F(FormatTest, NoOperandAlignment) {
  3125. FormatStyle Style = getLLVMStyle();
  3126. Style.AlignOperands = false;
  3127. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
  3128. verifyFormat(
  3129. "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3130. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3131. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3132. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3133. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3134. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3135. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3136. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3137. " > ccccccccccccccccccccccccccccccccccccccccc;",
  3138. Style);
  3139. verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3140. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3141. " + cc;",
  3142. Style);
  3143. verifyFormat("int a = aa\n"
  3144. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3145. " * cccccccccccccccccccccccccccccccccccc;",
  3146. Style);
  3147. Style.AlignAfterOpenBracket = false;
  3148. verifyFormat("return (a > b\n"
  3149. " // comment1\n"
  3150. " // comment2\n"
  3151. " || c);",
  3152. Style);
  3153. }
  3154. TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
  3155. FormatStyle Style = getLLVMStyle();
  3156. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
  3157. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3158. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3159. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
  3160. Style);
  3161. }
  3162. TEST_F(FormatTest, ConstructorInitializers) {
  3163. verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
  3164. verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
  3165. getLLVMStyleWithColumns(45));
  3166. verifyFormat("Constructor()\n"
  3167. " : Inttializer(FitsOnTheLine) {}",
  3168. getLLVMStyleWithColumns(44));
  3169. verifyFormat("Constructor()\n"
  3170. " : Inttializer(FitsOnTheLine) {}",
  3171. getLLVMStyleWithColumns(43));
  3172. verifyFormat(
  3173. "SomeClass::Constructor()\n"
  3174. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  3175. verifyFormat(
  3176. "SomeClass::Constructor()\n"
  3177. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3178. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
  3179. verifyFormat(
  3180. "SomeClass::Constructor()\n"
  3181. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3182. " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  3183. verifyFormat("Constructor()\n"
  3184. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3185. " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3186. " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3187. " aaaaaaaaaaaaaaaaaaaaaaa() {}");
  3188. verifyFormat("Constructor()\n"
  3189. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3190. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3191. verifyFormat("Constructor(int Parameter = 0)\n"
  3192. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
  3193. " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
  3194. verifyFormat("Constructor()\n"
  3195. " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
  3196. "}",
  3197. getLLVMStyleWithColumns(60));
  3198. verifyFormat("Constructor()\n"
  3199. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3200. " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
  3201. // Here a line could be saved by splitting the second initializer onto two
  3202. // lines, but that is not desirable.
  3203. verifyFormat("Constructor()\n"
  3204. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3205. " aaaaaaaaaaa(aaaaaaaaaaa),\n"
  3206. " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3207. FormatStyle OnePerLine = getLLVMStyle();
  3208. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  3209. verifyFormat("SomeClass::Constructor()\n"
  3210. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3211. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3212. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  3213. OnePerLine);
  3214. verifyFormat("SomeClass::Constructor()\n"
  3215. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
  3216. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3217. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  3218. OnePerLine);
  3219. verifyFormat("MyClass::MyClass(int var)\n"
  3220. " : some_var_(var), // 4 space indent\n"
  3221. " some_other_var_(var + 1) { // lined up\n"
  3222. "}",
  3223. OnePerLine);
  3224. verifyFormat("Constructor()\n"
  3225. " : aaaaa(aaaaaa),\n"
  3226. " aaaaa(aaaaaa),\n"
  3227. " aaaaa(aaaaaa),\n"
  3228. " aaaaa(aaaaaa),\n"
  3229. " aaaaa(aaaaaa) {}",
  3230. OnePerLine);
  3231. verifyFormat("Constructor()\n"
  3232. " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
  3233. " aaaaaaaaaaaaaaaaaaaaaa) {}",
  3234. OnePerLine);
  3235. EXPECT_EQ("Constructor()\n"
  3236. " : // Comment forcing unwanted break.\n"
  3237. " aaaa(aaaa) {}",
  3238. format("Constructor() :\n"
  3239. " // Comment forcing unwanted break.\n"
  3240. " aaaa(aaaa) {}"));
  3241. }
  3242. TEST_F(FormatTest, MemoizationTests) {
  3243. // This breaks if the memoization lookup does not take \c Indent and
  3244. // \c LastSpace into account.
  3245. verifyFormat(
  3246. "extern CFRunLoopTimerRef\n"
  3247. "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
  3248. " CFTimeInterval interval, CFOptionFlags flags,\n"
  3249. " CFIndex order, CFRunLoopTimerCallBack callout,\n"
  3250. " CFRunLoopTimerContext *context) {}");
  3251. // Deep nesting somewhat works around our memoization.
  3252. verifyFormat(
  3253. "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3254. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3255. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3256. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3257. " aaaaa())))))))))))))))))))))))))))))))))))))));",
  3258. getLLVMStyleWithColumns(65));
  3259. verifyFormat(
  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(\n"
  3273. " aaaaa,\n"
  3274. " aaaaa(\n"
  3275. " aaaaa,\n"
  3276. " aaaaa(\n"
  3277. " aaaaa,\n"
  3278. " aaaaa(\n"
  3279. " aaaaa,\n"
  3280. " aaaaa(\n"
  3281. " aaaaa,\n"
  3282. " aaaaa(\n"
  3283. " aaaaa,\n"
  3284. " aaaaa))))))))))));",
  3285. getLLVMStyleWithColumns(65));
  3286. verifyFormat(
  3287. "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"
  3288. " a),\n"
  3289. " a),\n"
  3290. " a),\n"
  3291. " a),\n"
  3292. " a),\n"
  3293. " a),\n"
  3294. " a),\n"
  3295. " a),\n"
  3296. " a),\n"
  3297. " a),\n"
  3298. " a),\n"
  3299. " a),\n"
  3300. " a),\n"
  3301. " a),\n"
  3302. " a),\n"
  3303. " a),\n"
  3304. " a)",
  3305. getLLVMStyleWithColumns(65));
  3306. // This test takes VERY long when memoization is broken.
  3307. FormatStyle OnePerLine = getLLVMStyle();
  3308. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  3309. OnePerLine.BinPackParameters = false;
  3310. std::string input = "Constructor()\n"
  3311. " : aaaa(a,\n";
  3312. for (unsigned i = 0, e = 80; i != e; ++i) {
  3313. input += " a,\n";
  3314. }
  3315. input += " a) {}";
  3316. verifyFormat(input, OnePerLine);
  3317. }
  3318. TEST_F(FormatTest, BreaksAsHighAsPossible) {
  3319. verifyFormat(
  3320. "void f() {\n"
  3321. " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
  3322. " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
  3323. " f();\n"
  3324. "}");
  3325. verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
  3326. " Intervals[i - 1].getRange().getLast()) {\n}");
  3327. }
  3328. TEST_F(FormatTest, BreaksFunctionDeclarations) {
  3329. // Principially, we break function declarations in a certain order:
  3330. // 1) break amongst arguments.
  3331. verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
  3332. " Cccccccccccccc cccccccccccccc);");
  3333. verifyFormat(
  3334. "template <class TemplateIt>\n"
  3335. "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
  3336. " TemplateIt *stop) {}");
  3337. // 2) break after return type.
  3338. verifyFormat(
  3339. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3340. "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
  3341. getGoogleStyle());
  3342. // 3) break after (.
  3343. verifyFormat(
  3344. "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
  3345. " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
  3346. getGoogleStyle());
  3347. // 4) break before after nested name specifiers.
  3348. verifyFormat(
  3349. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3350. "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
  3351. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
  3352. getGoogleStyle());
  3353. // However, there are exceptions, if a sufficient amount of lines can be
  3354. // saved.
  3355. // FIXME: The precise cut-offs wrt. the number of saved lines might need some
  3356. // more adjusting.
  3357. verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  3358. " Cccccccccccccc cccccccccc,\n"
  3359. " Cccccccccccccc cccccccccc,\n"
  3360. " Cccccccccccccc cccccccccc,\n"
  3361. " Cccccccccccccc cccccccccc);");
  3362. verifyFormat(
  3363. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3364. "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3365. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3366. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
  3367. getGoogleStyle());
  3368. verifyFormat(
  3369. "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  3370. " Cccccccccccccc cccccccccc,\n"
  3371. " Cccccccccccccc cccccccccc,\n"
  3372. " Cccccccccccccc cccccccccc,\n"
  3373. " Cccccccccccccc cccccccccc,\n"
  3374. " Cccccccccccccc cccccccccc,\n"
  3375. " Cccccccccccccc cccccccccc);");
  3376. verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3377. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3378. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3379. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3380. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
  3381. // Break after multi-line parameters.
  3382. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3383. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3384. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3385. " bbbb bbbb);");
  3386. verifyFormat("void SomeLoooooooooooongFunction(\n"
  3387. " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  3388. " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3389. " int bbbbbbbbbbbbb);");
  3390. // Treat overloaded operators like other functions.
  3391. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3392. "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
  3393. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3394. "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
  3395. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3396. "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
  3397. verifyGoogleFormat(
  3398. "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
  3399. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  3400. verifyGoogleFormat(
  3401. "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
  3402. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  3403. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3404. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  3405. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
  3406. "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  3407. verifyGoogleFormat(
  3408. "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
  3409. "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3410. " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
  3411. }
  3412. TEST_F(FormatTest, TrailingReturnType) {
  3413. verifyFormat("auto foo() -> int;\n");
  3414. verifyFormat("struct S {\n"
  3415. " auto bar() const -> int;\n"
  3416. "};");
  3417. verifyFormat("template <size_t Order, typename T>\n"
  3418. "auto load_img(const std::string &filename)\n"
  3419. " -> alias::tensor<Order, T, mem::tag::cpu> {}");
  3420. verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
  3421. " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
  3422. verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
  3423. // Not trailing return types.
  3424. verifyFormat("void f() { auto a = b->c(); }");
  3425. }
  3426. TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
  3427. // Avoid breaking before trailing 'const' or other trailing annotations, if
  3428. // they are not function-like.
  3429. FormatStyle Style = getGoogleStyle();
  3430. Style.ColumnLimit = 47;
  3431. verifyFormat("void someLongFunction(\n"
  3432. " int someLoooooooooooooongParameter) const {\n}",
  3433. getLLVMStyleWithColumns(47));
  3434. verifyFormat("LoooooongReturnType\n"
  3435. "someLoooooooongFunction() const {}",
  3436. getLLVMStyleWithColumns(47));
  3437. verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
  3438. " const {}",
  3439. Style);
  3440. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3441. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
  3442. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3443. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
  3444. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3445. " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
  3446. verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
  3447. " aaaaaaaaaaa aaaaa) const override;");
  3448. verifyGoogleFormat(
  3449. "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3450. " const override;");
  3451. // Even if the first parameter has to be wrapped.
  3452. verifyFormat("void someLongFunction(\n"
  3453. " int someLongParameter) const {}",
  3454. getLLVMStyleWithColumns(46));
  3455. verifyFormat("void someLongFunction(\n"
  3456. " int someLongParameter) const {}",
  3457. Style);
  3458. verifyFormat("void someLongFunction(\n"
  3459. " int someLongParameter) override {}",
  3460. Style);
  3461. verifyFormat("void someLongFunction(\n"
  3462. " int someLongParameter) OVERRIDE {}",
  3463. Style);
  3464. verifyFormat("void someLongFunction(\n"
  3465. " int someLongParameter) final {}",
  3466. Style);
  3467. verifyFormat("void someLongFunction(\n"
  3468. " int someLongParameter) FINAL {}",
  3469. Style);
  3470. verifyFormat("void someLongFunction(\n"
  3471. " int parameter) const override {}",
  3472. Style);
  3473. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  3474. verifyFormat("void someLongFunction(\n"
  3475. " int someLongParameter) const\n"
  3476. "{\n"
  3477. "}",
  3478. Style);
  3479. // Unless these are unknown annotations.
  3480. verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
  3481. " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3482. " LONG_AND_UGLY_ANNOTATION;");
  3483. // Breaking before function-like trailing annotations is fine to keep them
  3484. // close to their arguments.
  3485. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3486. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  3487. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  3488. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  3489. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  3490. " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
  3491. verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
  3492. " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
  3493. verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
  3494. verifyFormat(
  3495. "void aaaaaaaaaaaaaaaaaa()\n"
  3496. " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3497. " aaaaaaaaaaaaaaaaaaaaaaaaa));");
  3498. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3499. " __attribute__((unused));");
  3500. verifyGoogleFormat(
  3501. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3502. " GUARDED_BY(aaaaaaaaaaaa);");
  3503. verifyGoogleFormat(
  3504. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3505. " GUARDED_BY(aaaaaaaaaaaa);");
  3506. verifyGoogleFormat(
  3507. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
  3508. " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3509. }
  3510. TEST_F(FormatTest, BreaksDesireably) {
  3511. verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  3512. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  3513. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
  3514. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3515. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  3516. "}");
  3517. verifyFormat(
  3518. "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3519. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3520. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3521. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3522. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3523. verifyFormat(
  3524. "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3525. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  3526. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3527. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
  3528. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3529. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3530. verifyFormat(
  3531. "void f() {\n"
  3532. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
  3533. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3534. "}");
  3535. verifyFormat(
  3536. "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3537. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3538. verifyFormat(
  3539. "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3540. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3541. verifyFormat(
  3542. "aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3543. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3544. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3545. // Indent consistently independent of call expression.
  3546. verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
  3547. " dddddddddddddddddddddddddddddd));\n"
  3548. "aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3549. " dddddddddddddddddddddddddddddd));");
  3550. // This test case breaks on an incorrect memoization, i.e. an optimization not
  3551. // taking into account the StopAt value.
  3552. verifyFormat(
  3553. "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3554. " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3555. " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3556. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3557. verifyFormat("{\n {\n {\n"
  3558. " Annotation.SpaceRequiredBefore =\n"
  3559. " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
  3560. " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
  3561. " }\n }\n}");
  3562. // Break on an outer level if there was a break on an inner level.
  3563. EXPECT_EQ("f(g(h(a, // comment\n"
  3564. " b, c),\n"
  3565. " d, e),\n"
  3566. " x, y);",
  3567. format("f(g(h(a, // comment\n"
  3568. " b, c), d, e), x, y);"));
  3569. // Prefer breaking similar line breaks.
  3570. verifyFormat(
  3571. "const int kTrackingOptions = NSTrackingMouseMoved |\n"
  3572. " NSTrackingMouseEnteredAndExited |\n"
  3573. " NSTrackingActiveAlways;");
  3574. }
  3575. TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
  3576. FormatStyle NoBinPacking = getGoogleStyle();
  3577. NoBinPacking.BinPackParameters = false;
  3578. NoBinPacking.BinPackArguments = true;
  3579. verifyFormat("void f() {\n"
  3580. " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
  3581. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3582. "}",
  3583. NoBinPacking);
  3584. verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
  3585. " int aaaaaaaaaaaaaaaaaaaa,\n"
  3586. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3587. NoBinPacking);
  3588. }
  3589. TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
  3590. FormatStyle NoBinPacking = getGoogleStyle();
  3591. NoBinPacking.BinPackParameters = false;
  3592. NoBinPacking.BinPackArguments = false;
  3593. verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
  3594. " aaaaaaaaaaaaaaaaaaaa,\n"
  3595. " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
  3596. NoBinPacking);
  3597. verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
  3598. " aaaaaaaaaaaaa,\n"
  3599. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
  3600. NoBinPacking);
  3601. verifyFormat(
  3602. "aaaaaaaa(aaaaaaaaaaaaa,\n"
  3603. " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3604. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  3605. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3606. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
  3607. NoBinPacking);
  3608. verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  3609. " .aaaaaaaaaaaaaaaaaa();",
  3610. NoBinPacking);
  3611. verifyFormat("void f() {\n"
  3612. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3613. " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
  3614. "}",
  3615. NoBinPacking);
  3616. verifyFormat(
  3617. "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3618. " aaaaaaaaaaaa,\n"
  3619. " aaaaaaaaaaaa);",
  3620. NoBinPacking);
  3621. verifyFormat(
  3622. "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
  3623. " ddddddddddddddddddddddddddddd),\n"
  3624. " test);",
  3625. NoBinPacking);
  3626. verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3627. " aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3628. " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;",
  3629. NoBinPacking);
  3630. verifyFormat("a(\"a\"\n"
  3631. " \"a\",\n"
  3632. " a);");
  3633. NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
  3634. verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
  3635. " aaaaaaaaa,\n"
  3636. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3637. NoBinPacking);
  3638. verifyFormat(
  3639. "void f() {\n"
  3640. " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  3641. " .aaaaaaa();\n"
  3642. "}",
  3643. NoBinPacking);
  3644. verifyFormat(
  3645. "template <class SomeType, class SomeOtherType>\n"
  3646. "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
  3647. NoBinPacking);
  3648. }
  3649. TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
  3650. FormatStyle Style = getLLVMStyleWithColumns(15);
  3651. Style.ExperimentalAutoDetectBinPacking = true;
  3652. EXPECT_EQ("aaa(aaaa,\n"
  3653. " aaaa,\n"
  3654. " aaaa);\n"
  3655. "aaa(aaaa,\n"
  3656. " aaaa,\n"
  3657. " aaaa);",
  3658. format("aaa(aaaa,\n" // one-per-line
  3659. " aaaa,\n"
  3660. " aaaa );\n"
  3661. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  3662. Style));
  3663. EXPECT_EQ("aaa(aaaa, aaaa,\n"
  3664. " aaaa);\n"
  3665. "aaa(aaaa, aaaa,\n"
  3666. " aaaa);",
  3667. format("aaa(aaaa, aaaa,\n" // bin-packed
  3668. " aaaa );\n"
  3669. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  3670. Style));
  3671. }
  3672. TEST_F(FormatTest, FormatsBuilderPattern) {
  3673. verifyFormat(
  3674. "return llvm::StringSwitch<Reference::Kind>(name)\n"
  3675. " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
  3676. " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
  3677. " .StartsWith(\".init\", ORDER_INIT)\n"
  3678. " .StartsWith(\".fini\", ORDER_FINI)\n"
  3679. " .StartsWith(\".hash\", ORDER_HASH)\n"
  3680. " .Default(ORDER_TEXT);\n");
  3681. verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
  3682. " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
  3683. verifyFormat(
  3684. "aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaa(\n"
  3685. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3686. " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
  3687. verifyFormat(
  3688. "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
  3689. " aaaaaaaaaaaaaa);");
  3690. verifyFormat(
  3691. "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
  3692. " aaaaaa->aaaaaaaaaaaa()\n"
  3693. " ->aaaaaaaaaaaaaaaa(\n"
  3694. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3695. " ->aaaaaaaaaaaaaaaaa();");
  3696. verifyGoogleFormat(
  3697. "void f() {\n"
  3698. " someo->Add((new util::filetools::Handler(dir))\n"
  3699. " ->OnEvent1(NewPermanentCallback(\n"
  3700. " this, &HandlerHolderClass::EventHandlerCBA))\n"
  3701. " ->OnEvent2(NewPermanentCallback(\n"
  3702. " this, &HandlerHolderClass::EventHandlerCBB))\n"
  3703. " ->OnEvent3(NewPermanentCallback(\n"
  3704. " this, &HandlerHolderClass::EventHandlerCBC))\n"
  3705. " ->OnEvent5(NewPermanentCallback(\n"
  3706. " this, &HandlerHolderClass::EventHandlerCBD))\n"
  3707. " ->OnEvent6(NewPermanentCallback(\n"
  3708. " this, &HandlerHolderClass::EventHandlerCBE)));\n"
  3709. "}");
  3710. verifyFormat(
  3711. "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
  3712. verifyFormat("aaaaaaaaaaaaaaa()\n"
  3713. " .aaaaaaaaaaaaaaa()\n"
  3714. " .aaaaaaaaaaaaaaa()\n"
  3715. " .aaaaaaaaaaaaaaa()\n"
  3716. " .aaaaaaaaaaaaaaa();");
  3717. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3718. " .aaaaaaaaaaaaaaa()\n"
  3719. " .aaaaaaaaaaaaaaa()\n"
  3720. " .aaaaaaaaaaaaaaa();");
  3721. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3722. " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3723. " .aaaaaaaaaaaaaaa();");
  3724. verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3725. " ->aaaaaaaaaaaaaae(0)\n"
  3726. " ->aaaaaaaaaaaaaaa();");
  3727. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  3728. " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3729. " .has<bbbbbbbbbbbbbbbbbbbbb>();");
  3730. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  3731. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  3732. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
  3733. // Prefer not to break after empty parentheses.
  3734. verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
  3735. " First->LastNewlineOffset);");
  3736. }
  3737. TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
  3738. verifyFormat(
  3739. "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3740. " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
  3741. verifyFormat(
  3742. "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
  3743. " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
  3744. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  3745. " ccccccccccccccccccccccccc) {\n}");
  3746. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
  3747. " ccccccccccccccccccccccccc) {\n}");
  3748. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  3749. " ccccccccccccccccccccccccc) {\n}");
  3750. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
  3751. " ccccccccccccccccccccccccc) {\n}");
  3752. verifyFormat(
  3753. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
  3754. " ccccccccccccccccccccccccc) {\n}");
  3755. verifyFormat(
  3756. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
  3757. " ccccccccccccccccccccccccc) {\n}");
  3758. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
  3759. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
  3760. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
  3761. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  3762. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
  3763. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
  3764. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
  3765. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  3766. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
  3767. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
  3768. " aaaaaaaaaaaaaaa != aa) {\n}");
  3769. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
  3770. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
  3771. " aaaaaaaaaaaaaaa != aa) {\n}");
  3772. }
  3773. TEST_F(FormatTest, BreaksAfterAssignments) {
  3774. verifyFormat(
  3775. "unsigned Cost =\n"
  3776. " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
  3777. " SI->getPointerAddressSpaceee());\n");
  3778. verifyFormat(
  3779. "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
  3780. " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
  3781. verifyFormat(
  3782. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
  3783. " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
  3784. verifyFormat("unsigned OriginalStartColumn =\n"
  3785. " SourceMgr.getSpellingColumnNumber(\n"
  3786. " Current.FormatTok.getStartOfNonWhitespace()) -\n"
  3787. " 1;");
  3788. }
  3789. TEST_F(FormatTest, AlignsAfterAssignments) {
  3790. verifyFormat(
  3791. "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3792. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3793. verifyFormat(
  3794. "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3795. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3796. verifyFormat(
  3797. "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3798. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3799. verifyFormat(
  3800. "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3801. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  3802. verifyFormat(
  3803. "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3804. " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3805. " aaaaaaaaaaaaaaaaaaaaaaaa;");
  3806. }
  3807. TEST_F(FormatTest, AlignsAfterReturn) {
  3808. verifyFormat(
  3809. "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3810. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3811. verifyFormat(
  3812. "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3813. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  3814. verifyFormat(
  3815. "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  3816. " aaaaaaaaaaaaaaaaaaaaaa();");
  3817. verifyFormat(
  3818. "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  3819. " aaaaaaaaaaaaaaaaaaaaaa());");
  3820. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3821. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3822. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3823. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
  3824. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3825. verifyFormat("return\n"
  3826. " // true if code is one of a or b.\n"
  3827. " code == a || code == b;");
  3828. }
  3829. TEST_F(FormatTest, AlignsAfterOpenBracket) {
  3830. verifyFormat(
  3831. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
  3832. " aaaaaaaaa aaaaaaa) {}");
  3833. verifyFormat(
  3834. "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
  3835. " aaaaaaaaaaa aaaaaaaaa);");
  3836. verifyFormat(
  3837. "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
  3838. " aaaaaaaaaaaaaaaaaaaaa));");
  3839. FormatStyle Style = getLLVMStyle();
  3840. Style.AlignAfterOpenBracket = false;
  3841. verifyFormat(
  3842. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3843. " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
  3844. Style);
  3845. verifyFormat(
  3846. "SomeLongVariableName->someVeryLongFunctionName(\n"
  3847. " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
  3848. Style);
  3849. verifyFormat(
  3850. "SomeLongVariableName->someFunction(\n"
  3851. " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
  3852. Style);
  3853. verifyFormat(
  3854. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
  3855. " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3856. Style);
  3857. verifyFormat(
  3858. "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
  3859. " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3860. Style);
  3861. verifyFormat(
  3862. "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
  3863. " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
  3864. Style);
  3865. }
  3866. TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
  3867. FormatStyle Style = getLLVMStyleWithColumns(40);
  3868. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  3869. " bbbbbbbbbbbbbbbbbbbbbb);",
  3870. Style);
  3871. Style.AlignAfterOpenBracket = true;
  3872. Style.AlignOperands = false;
  3873. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  3874. " bbbbbbbbbbbbbbbbbbbbbb);",
  3875. Style);
  3876. Style.AlignAfterOpenBracket = false;
  3877. Style.AlignOperands = true;
  3878. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  3879. " bbbbbbbbbbbbbbbbbbbbbb);",
  3880. Style);
  3881. Style.AlignAfterOpenBracket = false;
  3882. Style.AlignOperands = false;
  3883. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  3884. " bbbbbbbbbbbbbbbbbbbbbb);",
  3885. Style);
  3886. }
  3887. TEST_F(FormatTest, BreaksConditionalExpressions) {
  3888. verifyFormat(
  3889. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3890. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3891. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3892. verifyFormat(
  3893. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3894. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3895. verifyFormat(
  3896. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
  3897. " : aaaaaaaaaaaaa);");
  3898. verifyFormat(
  3899. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3900. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3901. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3902. " aaaaaaaaaaaaa);");
  3903. verifyFormat(
  3904. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3905. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3906. " aaaaaaaaaaaaa);");
  3907. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3908. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3909. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3910. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3911. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3912. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3913. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3914. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3915. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3916. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3917. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3918. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3919. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3920. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3921. " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3922. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3923. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3924. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3925. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3926. " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3927. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  3928. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3929. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3930. " : aaaaaaaaaaaaaaaa;");
  3931. verifyFormat(
  3932. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3933. " ? aaaaaaaaaaaaaaa\n"
  3934. " : aaaaaaaaaaaaaaa;");
  3935. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  3936. " aaaaaaaaa\n"
  3937. " ? b\n"
  3938. " : c);");
  3939. verifyFormat("return aaaa == bbbb\n"
  3940. " // comment\n"
  3941. " ? aaaa\n"
  3942. " : bbbb;");
  3943. verifyFormat(
  3944. "unsigned Indent =\n"
  3945. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
  3946. " ? IndentForLevel[TheLine.Level]\n"
  3947. " : TheLine * 2,\n"
  3948. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  3949. getLLVMStyleWithColumns(70));
  3950. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  3951. " ? aaaaaaaaaaaaaaa\n"
  3952. " : bbbbbbbbbbbbbbb //\n"
  3953. " ? ccccccccccccccc\n"
  3954. " : ddddddddddddddd;");
  3955. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  3956. " ? aaaaaaaaaaaaaaa\n"
  3957. " : (bbbbbbbbbbbbbbb //\n"
  3958. " ? ccccccccccccccc\n"
  3959. " : ddddddddddddddd);");
  3960. verifyFormat(
  3961. "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3962. " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3963. " aaaaaaaaaaaaaaaaaaaaa +\n"
  3964. " aaaaaaaaaaaaaaaaaaaaa\n"
  3965. " : aaaaaaaaaa;");
  3966. verifyFormat(
  3967. "aaaaaa = aaaaaaaaaaaa\n"
  3968. " ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3969. " : aaaaaaaaaaaaaaaaaaaaaa\n"
  3970. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3971. FormatStyle NoBinPacking = getLLVMStyle();
  3972. NoBinPacking.BinPackArguments = false;
  3973. verifyFormat(
  3974. "void f() {\n"
  3975. " g(aaa,\n"
  3976. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  3977. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3978. " ? aaaaaaaaaaaaaaa\n"
  3979. " : aaaaaaaaaaaaaaa);\n"
  3980. "}",
  3981. NoBinPacking);
  3982. verifyFormat(
  3983. "void f() {\n"
  3984. " g(aaa,\n"
  3985. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  3986. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3987. " ?: aaaaaaaaaaaaaaa);\n"
  3988. "}",
  3989. NoBinPacking);
  3990. verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
  3991. " // comment.\n"
  3992. " ccccccccccccccccccccccccccccccccccccccc\n"
  3993. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3994. " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
  3995. // Assignments in conditional expressions. Apparently not uncommon :-(.
  3996. verifyFormat("return a != b\n"
  3997. " // comment\n"
  3998. " ? a = b\n"
  3999. " : a = b;");
  4000. verifyFormat("return a != b\n"
  4001. " // comment\n"
  4002. " ? a = a != b\n"
  4003. " // comment\n"
  4004. " ? a = b\n"
  4005. " : a\n"
  4006. " : a;\n");
  4007. verifyFormat("return a != b\n"
  4008. " // comment\n"
  4009. " ? a\n"
  4010. " : a = a != b\n"
  4011. " // comment\n"
  4012. " ? a = b\n"
  4013. " : a;");
  4014. }
  4015. TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
  4016. FormatStyle Style = getLLVMStyle();
  4017. Style.BreakBeforeTernaryOperators = false;
  4018. Style.ColumnLimit = 70;
  4019. verifyFormat(
  4020. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4021. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4022. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4023. Style);
  4024. verifyFormat(
  4025. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4026. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4027. Style);
  4028. verifyFormat(
  4029. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
  4030. " aaaaaaaaaaaaa);",
  4031. Style);
  4032. verifyFormat(
  4033. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4034. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4035. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4036. " aaaaaaaaaaaaa);",
  4037. Style);
  4038. verifyFormat(
  4039. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4040. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4041. " aaaaaaaaaaaaa);",
  4042. Style);
  4043. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4044. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4045. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  4046. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4047. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4048. Style);
  4049. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4050. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4051. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4052. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  4053. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4054. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4055. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4056. Style);
  4057. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4058. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
  4059. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4060. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4061. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4062. Style);
  4063. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4064. " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4065. " aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4066. Style);
  4067. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  4068. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4069. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4070. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4071. Style);
  4072. verifyFormat(
  4073. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4074. " aaaaaaaaaaaaaaa :\n"
  4075. " aaaaaaaaaaaaaaa;",
  4076. Style);
  4077. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  4078. " aaaaaaaaa ?\n"
  4079. " b :\n"
  4080. " c);",
  4081. Style);
  4082. verifyFormat(
  4083. "unsigned Indent =\n"
  4084. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n"
  4085. " IndentForLevel[TheLine.Level] :\n"
  4086. " TheLine * 2,\n"
  4087. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  4088. Style);
  4089. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  4090. " aaaaaaaaaaaaaaa :\n"
  4091. " bbbbbbbbbbbbbbb ? //\n"
  4092. " ccccccccccccccc :\n"
  4093. " ddddddddddddddd;",
  4094. Style);
  4095. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  4096. " aaaaaaaaaaaaaaa :\n"
  4097. " (bbbbbbbbbbbbbbb ? //\n"
  4098. " ccccccccccccccc :\n"
  4099. " ddddddddddddddd);",
  4100. Style);
  4101. }
  4102. TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
  4103. verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
  4104. " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
  4105. verifyFormat("bool a = true, b = false;");
  4106. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4107. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
  4108. " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
  4109. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
  4110. verifyFormat(
  4111. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  4112. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
  4113. " d = e && f;");
  4114. verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
  4115. " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
  4116. verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  4117. " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
  4118. verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
  4119. " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
  4120. // FIXME: If multiple variables are defined, the "*" needs to move to the new
  4121. // line. Also fix indent for breaking after the type, this looks bad.
  4122. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
  4123. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
  4124. " * b = bbbbbbbbbbbbbbbbbbb;",
  4125. getGoogleStyle());
  4126. // Not ideal, but pointer-with-type does not allow much here.
  4127. verifyGoogleFormat(
  4128. "aaaaaaaaa* a = aaaaaaaaaaaaaaaaaaa, * b = bbbbbbbbbbbbbbbbbbb,\n"
  4129. " * b = bbbbbbbbbbbbbbbbbbb, * d = ddddddddddddddddddd;");
  4130. }
  4131. TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
  4132. verifyFormat("arr[foo ? bar : baz];");
  4133. verifyFormat("f()[foo ? bar : baz];");
  4134. verifyFormat("(a + b)[foo ? bar : baz];");
  4135. verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
  4136. }
  4137. TEST_F(FormatTest, AlignsStringLiterals) {
  4138. verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
  4139. " \"short literal\");");
  4140. verifyFormat(
  4141. "looooooooooooooooooooooooongFunction(\n"
  4142. " \"short literal\"\n"
  4143. " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
  4144. verifyFormat("someFunction(\"Always break between multi-line\"\n"
  4145. " \" string literals\",\n"
  4146. " and, other, parameters);");
  4147. EXPECT_EQ("fun + \"1243\" /* comment */\n"
  4148. " \"5678\";",
  4149. format("fun + \"1243\" /* comment */\n"
  4150. " \"5678\";",
  4151. getLLVMStyleWithColumns(28)));
  4152. EXPECT_EQ(
  4153. "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  4154. " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
  4155. " \"aaaaaaaaaaaaaaaa\";",
  4156. format("aaaaaa ="
  4157. "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  4158. "aaaaaaaaaaaaaaaaaaaaa\" "
  4159. "\"aaaaaaaaaaaaaaaa\";"));
  4160. verifyFormat("a = a + \"a\"\n"
  4161. " \"a\"\n"
  4162. " \"a\";");
  4163. verifyFormat("f(\"a\", \"b\"\n"
  4164. " \"c\");");
  4165. verifyFormat(
  4166. "#define LL_FORMAT \"ll\"\n"
  4167. "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
  4168. " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
  4169. verifyFormat("#define A(X) \\\n"
  4170. " \"aaaaa\" #X \"bbbbbb\" \\\n"
  4171. " \"ccccc\"",
  4172. getLLVMStyleWithColumns(23));
  4173. verifyFormat("#define A \"def\"\n"
  4174. "f(\"abc\" A \"ghi\"\n"
  4175. " \"jkl\");");
  4176. verifyFormat("f(L\"a\"\n"
  4177. " L\"b\")");
  4178. verifyFormat("#define A(X) \\\n"
  4179. " L\"aaaaa\" #X L\"bbbbbb\" \\\n"
  4180. " L\"ccccc\"",
  4181. getLLVMStyleWithColumns(25));
  4182. }
  4183. TEST_F(FormatTest, AlwaysBreakAfterDefinitionReturnType) {
  4184. FormatStyle AfterType = getLLVMStyle();
  4185. AfterType.AlwaysBreakAfterDefinitionReturnType = true;
  4186. verifyFormat("const char *\n"
  4187. "f(void) {\n" // Break here.
  4188. " return \"\";\n"
  4189. "}\n"
  4190. "const char *bar(void);\n", // No break here.
  4191. AfterType);
  4192. verifyFormat("template <class T>\n"
  4193. "T *\n"
  4194. "f(T &c) {\n" // Break here.
  4195. " return NULL;\n"
  4196. "}\n"
  4197. "template <class T> T *f(T &c);\n", // No break here.
  4198. AfterType);
  4199. AfterType.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  4200. verifyFormat("const char *\n"
  4201. "f(void)\n" // Break here.
  4202. "{\n"
  4203. " return \"\";\n"
  4204. "}\n"
  4205. "const char *bar(void);\n", // No break here.
  4206. AfterType);
  4207. verifyFormat("template <class T>\n"
  4208. "T *\n" // Problem here: no line break
  4209. "f(T &c)\n" // Break here.
  4210. "{\n"
  4211. " return NULL;\n"
  4212. "}\n"
  4213. "template <class T> T *f(T &c);\n", // No break here.
  4214. AfterType);
  4215. }
  4216. TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
  4217. FormatStyle NoBreak = getLLVMStyle();
  4218. NoBreak.AlwaysBreakBeforeMultilineStrings = false;
  4219. FormatStyle Break = getLLVMStyle();
  4220. Break.AlwaysBreakBeforeMultilineStrings = true;
  4221. verifyFormat("aaaa = \"bbbb\"\n"
  4222. " \"cccc\";",
  4223. NoBreak);
  4224. verifyFormat("aaaa =\n"
  4225. " \"bbbb\"\n"
  4226. " \"cccc\";",
  4227. Break);
  4228. verifyFormat("aaaa(\"bbbb\"\n"
  4229. " \"cccc\");",
  4230. NoBreak);
  4231. verifyFormat("aaaa(\n"
  4232. " \"bbbb\"\n"
  4233. " \"cccc\");",
  4234. Break);
  4235. verifyFormat("aaaa(qqq, \"bbbb\"\n"
  4236. " \"cccc\");",
  4237. NoBreak);
  4238. verifyFormat("aaaa(qqq,\n"
  4239. " \"bbbb\"\n"
  4240. " \"cccc\");",
  4241. Break);
  4242. verifyFormat("aaaa(qqq,\n"
  4243. " L\"bbbb\"\n"
  4244. " L\"cccc\");",
  4245. Break);
  4246. // As we break before unary operators, breaking right after them is bad.
  4247. verifyFormat("string foo = abc ? \"x\"\n"
  4248. " \"blah blah blah blah blah blah\"\n"
  4249. " : \"y\";",
  4250. Break);
  4251. // Don't break if there is no column gain.
  4252. verifyFormat("f(\"aaaa\"\n"
  4253. " \"bbbb\");",
  4254. Break);
  4255. // Treat literals with escaped newlines like multi-line string literals.
  4256. EXPECT_EQ("x = \"a\\\n"
  4257. "b\\\n"
  4258. "c\";",
  4259. format("x = \"a\\\n"
  4260. "b\\\n"
  4261. "c\";",
  4262. NoBreak));
  4263. EXPECT_EQ("x =\n"
  4264. " \"a\\\n"
  4265. "b\\\n"
  4266. "c\";",
  4267. format("x = \"a\\\n"
  4268. "b\\\n"
  4269. "c\";",
  4270. Break));
  4271. // Exempt ObjC strings for now.
  4272. EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
  4273. " \"bbbb\";",
  4274. format("NSString *const kString = @\"aaaa\"\n"
  4275. "\"bbbb\";",
  4276. Break));
  4277. }
  4278. TEST_F(FormatTest, AlignsPipes) {
  4279. verifyFormat(
  4280. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4281. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4282. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4283. verifyFormat(
  4284. "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
  4285. " << aaaaaaaaaaaaaaaaaaaa;");
  4286. verifyFormat(
  4287. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4288. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4289. verifyFormat(
  4290. "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
  4291. " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
  4292. " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
  4293. verifyFormat(
  4294. "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4295. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4296. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4297. verifyFormat(
  4298. "llvm::errs() << \"a: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4299. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4300. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4301. verifyFormat(
  4302. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4303. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4304. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4305. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4306. verifyFormat(
  4307. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4308. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4309. verifyFormat("return out << \"somepacket = {\\n\"\n"
  4310. " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
  4311. " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
  4312. " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
  4313. " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
  4314. " << \"}\";");
  4315. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  4316. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  4317. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
  4318. verifyFormat(
  4319. "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
  4320. " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
  4321. " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
  4322. " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
  4323. " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
  4324. verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
  4325. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4326. verifyFormat(
  4327. "void f() {\n"
  4328. " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
  4329. " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  4330. "}");
  4331. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
  4332. " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
  4333. // Breaking before the first "<<" is generally not desirable.
  4334. verifyFormat(
  4335. "llvm::errs()\n"
  4336. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4337. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4338. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4339. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4340. getLLVMStyleWithColumns(70));
  4341. verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4342. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4343. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4344. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4345. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4346. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4347. getLLVMStyleWithColumns(70));
  4348. // But sometimes, breaking before the first "<<" is desirable.
  4349. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
  4350. " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
  4351. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
  4352. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4353. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4354. verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
  4355. " << BEF << IsTemplate << Description << E->getType();");
  4356. verifyFormat(
  4357. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4358. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4359. // Incomplete string literal.
  4360. EXPECT_EQ("llvm::errs() << \"\n"
  4361. " << a;",
  4362. format("llvm::errs() << \"\n<<a;"));
  4363. verifyFormat("void f() {\n"
  4364. " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
  4365. " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
  4366. "}");
  4367. // Handle 'endl'.
  4368. verifyFormat("llvm::errs() << aaaa << endl\n"
  4369. " << bbbb << endl;");
  4370. }
  4371. TEST_F(FormatTest, UnderstandsEquals) {
  4372. verifyFormat(
  4373. "aaaaaaaaaaaaaaaaa =\n"
  4374. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4375. verifyFormat(
  4376. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4377. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  4378. verifyFormat(
  4379. "if (a) {\n"
  4380. " f();\n"
  4381. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4382. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  4383. "}");
  4384. verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4385. " 100000000 + 10000000) {\n}");
  4386. }
  4387. TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
  4388. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  4389. " .looooooooooooooooooooooooooooooooooooooongFunction();");
  4390. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  4391. " ->looooooooooooooooooooooooooooooooooooooongFunction();");
  4392. verifyFormat(
  4393. "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
  4394. " Parameter2);");
  4395. verifyFormat(
  4396. "ShortObject->shortFunction(\n"
  4397. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
  4398. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
  4399. verifyFormat("loooooooooooooongFunction(\n"
  4400. " LoooooooooooooongObject->looooooooooooooooongFunction());");
  4401. verifyFormat(
  4402. "function(LoooooooooooooooooooooooooooooooooooongObject\n"
  4403. " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
  4404. verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  4405. " .WillRepeatedly(Return(SomeValue));");
  4406. verifyFormat("void f() {\n"
  4407. " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  4408. " .Times(2)\n"
  4409. " .WillRepeatedly(Return(SomeValue));\n"
  4410. "}");
  4411. verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
  4412. " ccccccccccccccccccccccc);");
  4413. verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4414. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).aaaaa(aaaaa),\n"
  4415. " aaaaaaaaaaaaaaaaaaaaa);");
  4416. verifyFormat("void f() {\n"
  4417. " aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4418. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
  4419. "}");
  4420. verifyFormat(
  4421. "aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4422. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4423. " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4424. " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4425. " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  4426. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4427. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4428. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4429. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
  4430. "}");
  4431. // Here, it is not necessary to wrap at "." or "->".
  4432. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
  4433. " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  4434. verifyFormat(
  4435. "aaaaaaaaaaa->aaaaaaaaa(\n"
  4436. " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4437. " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
  4438. verifyFormat(
  4439. "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4440. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
  4441. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
  4442. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  4443. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
  4444. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  4445. // FIXME: Should we break before .a()?
  4446. verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4447. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).a();");
  4448. FormatStyle NoBinPacking = getLLVMStyle();
  4449. NoBinPacking.BinPackParameters = false;
  4450. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  4451. " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  4452. " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
  4453. " aaaaaaaaaaaaaaaaaaa,\n"
  4454. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4455. NoBinPacking);
  4456. // If there is a subsequent call, change to hanging indentation.
  4457. verifyFormat(
  4458. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4459. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
  4460. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4461. verifyFormat(
  4462. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4463. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
  4464. verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4465. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4466. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4467. verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4468. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4469. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  4470. }
  4471. TEST_F(FormatTest, WrapsTemplateDeclarations) {
  4472. verifyFormat("template <typename T>\n"
  4473. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  4474. verifyFormat("template <typename T>\n"
  4475. "// T should be one of {A, B}.\n"
  4476. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  4477. verifyFormat(
  4478. "template <typename T>\n"
  4479. "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
  4480. verifyFormat("template <typename T>\n"
  4481. "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
  4482. " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
  4483. verifyFormat(
  4484. "template <typename T>\n"
  4485. "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
  4486. " int Paaaaaaaaaaaaaaaaaaaaram2);");
  4487. verifyFormat(
  4488. "template <typename T>\n"
  4489. "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
  4490. " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
  4491. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4492. verifyFormat("template <typename T>\n"
  4493. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4494. " int aaaaaaaaaaaaaaaaaaaaaa);");
  4495. verifyFormat(
  4496. "template <typename T1, typename T2 = char, typename T3 = char,\n"
  4497. " typename T4 = char>\n"
  4498. "void f();");
  4499. verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
  4500. " template <typename> class cccccccccccccccccccccc,\n"
  4501. " typename ddddddddddddd>\n"
  4502. "class C {};");
  4503. verifyFormat(
  4504. "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
  4505. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4506. verifyFormat("void f() {\n"
  4507. " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
  4508. " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
  4509. "}");
  4510. verifyFormat("template <typename T> class C {};");
  4511. verifyFormat("template <typename T> void f();");
  4512. verifyFormat("template <typename T> void f() {}");
  4513. verifyFormat(
  4514. "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  4515. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4516. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
  4517. " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  4518. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4519. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
  4520. " bbbbbbbbbbbbbbbbbbbbbbbb);",
  4521. getLLVMStyleWithColumns(72));
  4522. EXPECT_EQ("static_cast<A< //\n"
  4523. " B> *>(\n"
  4524. "\n"
  4525. " );",
  4526. format("static_cast<A<//\n"
  4527. " B>*>(\n"
  4528. "\n"
  4529. " );"));
  4530. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4531. " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
  4532. FormatStyle AlwaysBreak = getLLVMStyle();
  4533. AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
  4534. verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
  4535. verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
  4536. verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
  4537. verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4538. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
  4539. " ccccccccccccccccccccccccccccccccccccccccccccccc);");
  4540. verifyFormat("template <template <typename> class Fooooooo,\n"
  4541. " template <typename> class Baaaaaaar>\n"
  4542. "struct C {};",
  4543. AlwaysBreak);
  4544. verifyFormat("template <typename T> // T can be A, B or C.\n"
  4545. "struct C {};",
  4546. AlwaysBreak);
  4547. }
  4548. TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
  4549. verifyFormat(
  4550. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4551. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4552. verifyFormat(
  4553. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4554. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4555. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  4556. // FIXME: Should we have the extra indent after the second break?
  4557. verifyFormat(
  4558. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4559. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4560. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4561. verifyFormat(
  4562. "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
  4563. " cccccccccccccccccccccccccccccccccccccccccccccc());");
  4564. // Breaking at nested name specifiers is generally not desirable.
  4565. verifyFormat(
  4566. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4567. " aaaaaaaaaaaaaaaaaaaaaaa);");
  4568. verifyFormat(
  4569. "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4570. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4571. " aaaaaaaaaaaaaaaaaaaaa);",
  4572. getLLVMStyleWithColumns(74));
  4573. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4574. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4575. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4576. }
  4577. TEST_F(FormatTest, UnderstandsTemplateParameters) {
  4578. verifyFormat("A<int> a;");
  4579. verifyFormat("A<A<A<int>>> a;");
  4580. verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
  4581. verifyFormat("bool x = a < 1 || 2 > a;");
  4582. verifyFormat("bool x = 5 < f<int>();");
  4583. verifyFormat("bool x = f<int>() > 5;");
  4584. verifyFormat("bool x = 5 < a<int>::x;");
  4585. verifyFormat("bool x = a < 4 ? a > 2 : false;");
  4586. verifyFormat("bool x = f() ? a < 2 : a > 2;");
  4587. verifyGoogleFormat("A<A<int>> a;");
  4588. verifyGoogleFormat("A<A<A<int>>> a;");
  4589. verifyGoogleFormat("A<A<A<A<int>>>> a;");
  4590. verifyGoogleFormat("A<A<int> > a;");
  4591. verifyGoogleFormat("A<A<A<int> > > a;");
  4592. verifyGoogleFormat("A<A<A<A<int> > > > a;");
  4593. verifyGoogleFormat("A<::A<int>> a;");
  4594. verifyGoogleFormat("A<::A> a;");
  4595. verifyGoogleFormat("A< ::A> a;");
  4596. verifyGoogleFormat("A< ::A<int> > a;");
  4597. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
  4598. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
  4599. EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
  4600. EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
  4601. verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
  4602. verifyFormat("test >> a >> b;");
  4603. verifyFormat("test << a >> b;");
  4604. verifyFormat("f<int>();");
  4605. verifyFormat("template <typename T> void f() {}");
  4606. verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
  4607. // Not template parameters.
  4608. verifyFormat("return a < b && c > d;");
  4609. verifyFormat("void f() {\n"
  4610. " while (a < b && c > d) {\n"
  4611. " }\n"
  4612. "}");
  4613. verifyFormat("template <typename... Types>\n"
  4614. "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
  4615. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4616. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
  4617. getLLVMStyleWithColumns(60));
  4618. verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
  4619. verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
  4620. }
  4621. TEST_F(FormatTest, UnderstandsBinaryOperators) {
  4622. verifyFormat("COMPARE(a, ==, b);");
  4623. }
  4624. TEST_F(FormatTest, UnderstandsPointersToMembers) {
  4625. verifyFormat("int A::*x;");
  4626. verifyFormat("int (S::*func)(void *);");
  4627. verifyFormat("void f() { int (S::*func)(void *); }");
  4628. verifyFormat("typedef bool *(Class::*Member)() const;");
  4629. verifyFormat("void f() {\n"
  4630. " (a->*f)();\n"
  4631. " a->*x;\n"
  4632. " (a.*f)();\n"
  4633. " ((*a).*f)();\n"
  4634. " a.*x;\n"
  4635. "}");
  4636. verifyFormat("void f() {\n"
  4637. " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  4638. " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
  4639. "}");
  4640. verifyFormat(
  4641. "(aaaaaaaaaa->*bbbbbbb)(\n"
  4642. " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  4643. FormatStyle Style = getLLVMStyle();
  4644. Style.PointerAlignment = FormatStyle::PAS_Left;
  4645. verifyFormat("typedef bool* (Class::*Member)() const;", Style);
  4646. }
  4647. TEST_F(FormatTest, UnderstandsUnaryOperators) {
  4648. verifyFormat("int a = -2;");
  4649. verifyFormat("f(-1, -2, -3);");
  4650. verifyFormat("a[-1] = 5;");
  4651. verifyFormat("int a = 5 + -2;");
  4652. verifyFormat("if (i == -1) {\n}");
  4653. verifyFormat("if (i != -1) {\n}");
  4654. verifyFormat("if (i > -1) {\n}");
  4655. verifyFormat("if (i < -1) {\n}");
  4656. verifyFormat("++(a->f());");
  4657. verifyFormat("--(a->f());");
  4658. verifyFormat("(a->f())++;");
  4659. verifyFormat("a[42]++;");
  4660. verifyFormat("if (!(a->f())) {\n}");
  4661. verifyFormat("a-- > b;");
  4662. verifyFormat("b ? -a : c;");
  4663. verifyFormat("n * sizeof char16;");
  4664. verifyFormat("n * alignof char16;", getGoogleStyle());
  4665. verifyFormat("sizeof(char);");
  4666. verifyFormat("alignof(char);", getGoogleStyle());
  4667. verifyFormat("return -1;");
  4668. verifyFormat("switch (a) {\n"
  4669. "case -1:\n"
  4670. " break;\n"
  4671. "}");
  4672. verifyFormat("#define X -1");
  4673. verifyFormat("#define X -kConstant");
  4674. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
  4675. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
  4676. verifyFormat("int a = /* confusing comment */ -1;");
  4677. // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
  4678. verifyFormat("int a = i /* confusing comment */++;");
  4679. }
  4680. TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
  4681. verifyFormat("if (!aaaaaaaaaa( // break\n"
  4682. " aaaaa)) {\n"
  4683. "}");
  4684. verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
  4685. " aaaaa));");
  4686. verifyFormat("*aaa = aaaaaaa( // break\n"
  4687. " bbbbbb);");
  4688. }
  4689. TEST_F(FormatTest, UnderstandsOverloadedOperators) {
  4690. verifyFormat("bool operator<();");
  4691. verifyFormat("bool operator>();");
  4692. verifyFormat("bool operator=();");
  4693. verifyFormat("bool operator==();");
  4694. verifyFormat("bool operator!=();");
  4695. verifyFormat("int operator+();");
  4696. verifyFormat("int operator++();");
  4697. verifyFormat("bool operator();");
  4698. verifyFormat("bool operator()();");
  4699. verifyFormat("bool operator[]();");
  4700. verifyFormat("operator bool();");
  4701. verifyFormat("operator int();");
  4702. verifyFormat("operator void *();");
  4703. verifyFormat("operator SomeType<int>();");
  4704. verifyFormat("operator SomeType<int, int>();");
  4705. verifyFormat("operator SomeType<SomeType<int>>();");
  4706. verifyFormat("void *operator new(std::size_t size);");
  4707. verifyFormat("void *operator new[](std::size_t size);");
  4708. verifyFormat("void operator delete(void *ptr);");
  4709. verifyFormat("void operator delete[](void *ptr);");
  4710. verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
  4711. "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
  4712. verifyFormat(
  4713. "ostream &operator<<(ostream &OutputStream,\n"
  4714. " SomeReallyLongType WithSomeReallyLongValue);");
  4715. verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
  4716. " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
  4717. " return left.group < right.group;\n"
  4718. "}");
  4719. verifyFormat("SomeType &operator=(const SomeType &S);");
  4720. verifyFormat("f.template operator()<int>();");
  4721. verifyGoogleFormat("operator void*();");
  4722. verifyGoogleFormat("operator SomeType<SomeType<int>>();");
  4723. verifyGoogleFormat("operator ::A();");
  4724. verifyFormat("using A::operator+;");
  4725. verifyFormat("Deleted &operator=(const Deleted &)& = default;");
  4726. verifyFormat("Deleted &operator=(const Deleted &)&& = delete;");
  4727. verifyGoogleFormat("Deleted& operator=(const Deleted&)& = default;");
  4728. verifyGoogleFormat("Deleted& operator=(const Deleted&)&& = delete;");
  4729. verifyFormat("string // break\n"
  4730. "operator()() & {}");
  4731. verifyFormat("string // break\n"
  4732. "operator()() && {}");
  4733. }
  4734. TEST_F(FormatTest, UnderstandsNewAndDelete) {
  4735. verifyFormat("void f() {\n"
  4736. " A *a = new A;\n"
  4737. " A *a = new (placement) A;\n"
  4738. " delete a;\n"
  4739. " delete (A *)a;\n"
  4740. "}");
  4741. verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
  4742. " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
  4743. verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4744. " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
  4745. " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
  4746. verifyFormat("delete[] h->p;");
  4747. }
  4748. TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
  4749. verifyFormat("int *f(int *a) {}");
  4750. verifyFormat("int main(int argc, char **argv) {}");
  4751. verifyFormat("Test::Test(int b) : a(b * b) {}");
  4752. verifyIndependentOfContext("f(a, *a);");
  4753. verifyFormat("void g() { f(*a); }");
  4754. verifyIndependentOfContext("int a = b * 10;");
  4755. verifyIndependentOfContext("int a = 10 * b;");
  4756. verifyIndependentOfContext("int a = b * c;");
  4757. verifyIndependentOfContext("int a += b * c;");
  4758. verifyIndependentOfContext("int a -= b * c;");
  4759. verifyIndependentOfContext("int a *= b * c;");
  4760. verifyIndependentOfContext("int a /= b * c;");
  4761. verifyIndependentOfContext("int a = *b;");
  4762. verifyIndependentOfContext("int a = *b * c;");
  4763. verifyIndependentOfContext("int a = b * *c;");
  4764. verifyIndependentOfContext("return 10 * b;");
  4765. verifyIndependentOfContext("return *b * *c;");
  4766. verifyIndependentOfContext("return a & ~b;");
  4767. verifyIndependentOfContext("f(b ? *c : *d);");
  4768. verifyIndependentOfContext("int a = b ? *c : *d;");
  4769. verifyIndependentOfContext("*b = a;");
  4770. verifyIndependentOfContext("a * ~b;");
  4771. verifyIndependentOfContext("a * !b;");
  4772. verifyIndependentOfContext("a * +b;");
  4773. verifyIndependentOfContext("a * -b;");
  4774. verifyIndependentOfContext("a * ++b;");
  4775. verifyIndependentOfContext("a * --b;");
  4776. verifyIndependentOfContext("a[4] * b;");
  4777. verifyIndependentOfContext("a[a * a] = 1;");
  4778. verifyIndependentOfContext("f() * b;");
  4779. verifyIndependentOfContext("a * [self dostuff];");
  4780. verifyIndependentOfContext("int x = a * (a + b);");
  4781. verifyIndependentOfContext("(a *)(a + b);");
  4782. verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
  4783. verifyIndependentOfContext("int *pa = (int *)&a;");
  4784. verifyIndependentOfContext("return sizeof(int **);");
  4785. verifyIndependentOfContext("return sizeof(int ******);");
  4786. verifyIndependentOfContext("return (int **&)a;");
  4787. verifyIndependentOfContext("f((*PointerToArray)[10]);");
  4788. verifyFormat("void f(Type (*parameter)[10]) {}");
  4789. verifyGoogleFormat("return sizeof(int**);");
  4790. verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
  4791. verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
  4792. verifyFormat("auto a = [](int **&, int ***) {};");
  4793. verifyFormat("auto PointerBinding = [](const char *S) {};");
  4794. verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
  4795. verifyFormat("[](const decltype(*a) &value) {}");
  4796. verifyIndependentOfContext("typedef void (*f)(int *a);");
  4797. verifyIndependentOfContext("int i{a * b};");
  4798. verifyIndependentOfContext("aaa && aaa->f();");
  4799. verifyIndependentOfContext("int x = ~*p;");
  4800. verifyFormat("Constructor() : a(a), area(width * height) {}");
  4801. verifyFormat("Constructor() : a(a), area(a, width * height) {}");
  4802. verifyFormat("void f() { f(a, c * d); }");
  4803. verifyIndependentOfContext("InvalidRegions[*R] = 0;");
  4804. verifyIndependentOfContext("A<int *> a;");
  4805. verifyIndependentOfContext("A<int **> a;");
  4806. verifyIndependentOfContext("A<int *, int *> a;");
  4807. verifyIndependentOfContext("A<int *[]> a;");
  4808. verifyIndependentOfContext(
  4809. "const char *const p = reinterpret_cast<const char *const>(q);");
  4810. verifyIndependentOfContext("A<int **, int **> a;");
  4811. verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
  4812. verifyFormat("for (char **a = b; *a; ++a) {\n}");
  4813. verifyFormat("for (; a && b;) {\n}");
  4814. verifyFormat("bool foo = true && [] { return false; }();");
  4815. verifyFormat(
  4816. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4817. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4818. verifyGoogleFormat("**outparam = 1;");
  4819. verifyGoogleFormat("int main(int argc, char** argv) {}");
  4820. verifyGoogleFormat("A<int*> a;");
  4821. verifyGoogleFormat("A<int**> a;");
  4822. verifyGoogleFormat("A<int*, int*> a;");
  4823. verifyGoogleFormat("A<int**, int**> a;");
  4824. verifyGoogleFormat("f(b ? *c : *d);");
  4825. verifyGoogleFormat("int a = b ? *c : *d;");
  4826. verifyGoogleFormat("Type* t = **x;");
  4827. verifyGoogleFormat("Type* t = *++*x;");
  4828. verifyGoogleFormat("*++*x;");
  4829. verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
  4830. verifyGoogleFormat("Type* t = x++ * y;");
  4831. verifyGoogleFormat(
  4832. "const char* const p = reinterpret_cast<const char* const>(q);");
  4833. verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
  4834. verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
  4835. verifyGoogleFormat("template <typename T>\n"
  4836. "void f(int i = 0, SomeType** temps = NULL);");
  4837. FormatStyle Left = getLLVMStyle();
  4838. Left.PointerAlignment = FormatStyle::PAS_Left;
  4839. verifyFormat("x = *a(x) = *a(y);", Left);
  4840. verifyIndependentOfContext("a = *(x + y);");
  4841. verifyIndependentOfContext("a = &(x + y);");
  4842. verifyIndependentOfContext("*(x + y).call();");
  4843. verifyIndependentOfContext("&(x + y)->call();");
  4844. verifyFormat("void f() { &(*I).first; }");
  4845. verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
  4846. verifyFormat(
  4847. "int *MyValues = {\n"
  4848. " *A, // Operator detection might be confused by the '{'\n"
  4849. " *BB // Operator detection might be confused by previous comment\n"
  4850. "};");
  4851. verifyIndependentOfContext("if (int *a = &b)");
  4852. verifyIndependentOfContext("if (int &a = *b)");
  4853. verifyIndependentOfContext("if (a & b[i])");
  4854. verifyIndependentOfContext("if (a::b::c::d & b[i])");
  4855. verifyIndependentOfContext("if (*b[i])");
  4856. verifyIndependentOfContext("if (int *a = (&b))");
  4857. verifyIndependentOfContext("while (int *a = &b)");
  4858. verifyIndependentOfContext("size = sizeof *a;");
  4859. verifyFormat("void f() {\n"
  4860. " for (const int &v : Values) {\n"
  4861. " }\n"
  4862. "}");
  4863. verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
  4864. verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
  4865. verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
  4866. verifyFormat("#define A (!a * b)");
  4867. verifyFormat("#define MACRO \\\n"
  4868. " int *i = a * b; \\\n"
  4869. " void f(a *b);",
  4870. getLLVMStyleWithColumns(19));
  4871. verifyIndependentOfContext("A = new SomeType *[Length];");
  4872. verifyIndependentOfContext("A = new SomeType *[Length]();");
  4873. verifyIndependentOfContext("T **t = new T *;");
  4874. verifyIndependentOfContext("T **t = new T *();");
  4875. verifyGoogleFormat("A = new SomeType* [Length]();");
  4876. verifyGoogleFormat("A = new SomeType* [Length];");
  4877. verifyGoogleFormat("T** t = new T*;");
  4878. verifyGoogleFormat("T** t = new T*();");
  4879. FormatStyle PointerLeft = getLLVMStyle();
  4880. PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
  4881. verifyFormat("delete *x;", PointerLeft);
  4882. verifyFormat("STATIC_ASSERT((a & b) == 0);");
  4883. verifyFormat("STATIC_ASSERT(0 == (a & b));");
  4884. verifyFormat("template <bool a, bool b> "
  4885. "typename t::if<x && y>::type f() {}");
  4886. verifyFormat("template <int *y> f() {}");
  4887. verifyFormat("vector<int *> v;");
  4888. verifyFormat("vector<int *const> v;");
  4889. verifyFormat("vector<int *const **const *> v;");
  4890. verifyFormat("vector<int *volatile> v;");
  4891. verifyFormat("vector<a * b> v;");
  4892. verifyFormat("foo<b && false>();");
  4893. verifyFormat("foo<b & 1>();");
  4894. verifyFormat("decltype(*::std::declval<const T &>()) void F();");
  4895. verifyFormat(
  4896. "template <class T, class = typename std::enable_if<\n"
  4897. " std::is_integral<T>::value &&\n"
  4898. " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
  4899. "void F();",
  4900. getLLVMStyleWithColumns(76));
  4901. verifyFormat(
  4902. "template <class T,\n"
  4903. " class = typename ::std::enable_if<\n"
  4904. " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
  4905. "void F();",
  4906. getGoogleStyleWithColumns(68));
  4907. verifyIndependentOfContext("MACRO(int *i);");
  4908. verifyIndependentOfContext("MACRO(auto *a);");
  4909. verifyIndependentOfContext("MACRO(const A *a);");
  4910. verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
  4911. // FIXME: Is there a way to make this work?
  4912. // verifyIndependentOfContext("MACRO(A *a);");
  4913. verifyFormat("DatumHandle const *operator->() const { return input_; }");
  4914. EXPECT_EQ("#define OP(x) \\\n"
  4915. " ostream &operator<<(ostream &s, const A &a) { \\\n"
  4916. " return s << a.DebugString(); \\\n"
  4917. " }",
  4918. format("#define OP(x) \\\n"
  4919. " ostream &operator<<(ostream &s, const A &a) { \\\n"
  4920. " return s << a.DebugString(); \\\n"
  4921. " }",
  4922. getLLVMStyleWithColumns(50)));
  4923. // FIXME: We cannot handle this case yet; we might be able to figure out that
  4924. // foo<x> d > v; doesn't make sense.
  4925. verifyFormat("foo<a<b && c> d> v;");
  4926. FormatStyle PointerMiddle = getLLVMStyle();
  4927. PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
  4928. verifyFormat("delete *x;", PointerMiddle);
  4929. verifyFormat("int * x;", PointerMiddle);
  4930. verifyFormat("template <int * y> f() {}", PointerMiddle);
  4931. verifyFormat("int * f(int * a) {}", PointerMiddle);
  4932. verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
  4933. verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
  4934. verifyFormat("A<int *> a;", PointerMiddle);
  4935. verifyFormat("A<int **> a;", PointerMiddle);
  4936. verifyFormat("A<int *, int *> a;", PointerMiddle);
  4937. verifyFormat("A<int * []> a;", PointerMiddle);
  4938. verifyFormat("A = new SomeType * [Length]();", PointerMiddle);
  4939. verifyFormat("A = new SomeType * [Length];", PointerMiddle);
  4940. verifyFormat("T ** t = new T *;", PointerMiddle);
  4941. }
  4942. TEST_F(FormatTest, UnderstandsAttributes) {
  4943. verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
  4944. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
  4945. "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
  4946. }
  4947. TEST_F(FormatTest, UnderstandsEllipsis) {
  4948. verifyFormat("int printf(const char *fmt, ...);");
  4949. verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
  4950. verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
  4951. FormatStyle PointersLeft = getLLVMStyle();
  4952. PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
  4953. verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
  4954. }
  4955. TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
  4956. EXPECT_EQ("int *a;\n"
  4957. "int *a;\n"
  4958. "int *a;",
  4959. format("int *a;\n"
  4960. "int* a;\n"
  4961. "int *a;",
  4962. getGoogleStyle()));
  4963. EXPECT_EQ("int* a;\n"
  4964. "int* a;\n"
  4965. "int* a;",
  4966. format("int* a;\n"
  4967. "int* a;\n"
  4968. "int *a;",
  4969. getGoogleStyle()));
  4970. EXPECT_EQ("int *a;\n"
  4971. "int *a;\n"
  4972. "int *a;",
  4973. format("int *a;\n"
  4974. "int * a;\n"
  4975. "int * a;",
  4976. getGoogleStyle()));
  4977. }
  4978. TEST_F(FormatTest, UnderstandsRvalueReferences) {
  4979. verifyFormat("int f(int &&a) {}");
  4980. verifyFormat("int f(int a, char &&b) {}");
  4981. verifyFormat("void f() { int &&a = b; }");
  4982. verifyGoogleFormat("int f(int a, char&& b) {}");
  4983. verifyGoogleFormat("void f() { int&& a = b; }");
  4984. verifyIndependentOfContext("A<int &&> a;");
  4985. verifyIndependentOfContext("A<int &&, int &&> a;");
  4986. verifyGoogleFormat("A<int&&> a;");
  4987. verifyGoogleFormat("A<int&&, int&&> a;");
  4988. // Not rvalue references:
  4989. verifyFormat("template <bool B, bool C> class A {\n"
  4990. " static_assert(B && C, \"Something is wrong\");\n"
  4991. "};");
  4992. verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
  4993. verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
  4994. verifyFormat("#define A(a, b) (a && b)");
  4995. }
  4996. TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
  4997. verifyFormat("void f() {\n"
  4998. " x[aaaaaaaaa -\n"
  4999. " b] = 23;\n"
  5000. "}",
  5001. getLLVMStyleWithColumns(15));
  5002. }
  5003. TEST_F(FormatTest, FormatsCasts) {
  5004. verifyFormat("Type *A = static_cast<Type *>(P);");
  5005. verifyFormat("Type *A = (Type *)P;");
  5006. verifyFormat("Type *A = (vector<Type *, int *>)P;");
  5007. verifyFormat("int a = (int)(2.0f);");
  5008. verifyFormat("int a = (int)2.0f;");
  5009. verifyFormat("x[(int32)y];");
  5010. verifyFormat("x = (int32)y;");
  5011. verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
  5012. verifyFormat("int a = (int)*b;");
  5013. verifyFormat("int a = (int)2.0f;");
  5014. verifyFormat("int a = (int)~0;");
  5015. verifyFormat("int a = (int)++a;");
  5016. verifyFormat("int a = (int)sizeof(int);");
  5017. verifyFormat("int a = (int)+2;");
  5018. verifyFormat("my_int a = (my_int)2.0f;");
  5019. verifyFormat("my_int a = (my_int)sizeof(int);");
  5020. verifyFormat("return (my_int)aaa;");
  5021. verifyFormat("#define x ((int)-1)");
  5022. verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
  5023. verifyFormat("#define p(q) ((int *)&q)");
  5024. verifyFormat("fn(a)(b) + 1;");
  5025. verifyFormat("void f() { my_int a = (my_int)*b; }");
  5026. verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
  5027. verifyFormat("my_int a = (my_int)~0;");
  5028. verifyFormat("my_int a = (my_int)++a;");
  5029. verifyFormat("my_int a = (my_int)-2;");
  5030. verifyFormat("my_int a = (my_int)1;");
  5031. verifyFormat("my_int a = (my_int *)1;");
  5032. verifyFormat("my_int a = (const my_int)-1;");
  5033. verifyFormat("my_int a = (const my_int *)-1;");
  5034. verifyFormat("my_int a = (my_int)(my_int)-1;");
  5035. // FIXME: single value wrapped with paren will be treated as cast.
  5036. verifyFormat("void f(int i = (kValue)*kMask) {}");
  5037. verifyFormat("{ (void)F; }");
  5038. // Don't break after a cast's
  5039. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  5040. " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
  5041. " bbbbbbbbbbbbbbbbbbbbbb);");
  5042. // These are not casts.
  5043. verifyFormat("void f(int *) {}");
  5044. verifyFormat("f(foo)->b;");
  5045. verifyFormat("f(foo).b;");
  5046. verifyFormat("f(foo)(b);");
  5047. verifyFormat("f(foo)[b];");
  5048. verifyFormat("[](foo) { return 4; }(bar);");
  5049. verifyFormat("(*funptr)(foo)[4];");
  5050. verifyFormat("funptrs[4](foo)[4];");
  5051. verifyFormat("void f(int *);");
  5052. verifyFormat("void f(int *) = 0;");
  5053. verifyFormat("void f(SmallVector<int>) {}");
  5054. verifyFormat("void f(SmallVector<int>);");
  5055. verifyFormat("void f(SmallVector<int>) = 0;");
  5056. verifyFormat("void f(int i = (kA * kB) & kMask) {}");
  5057. verifyFormat("int a = sizeof(int) * b;");
  5058. verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
  5059. verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
  5060. verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
  5061. verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
  5062. // These are not casts, but at some point were confused with casts.
  5063. verifyFormat("virtual void foo(int *) override;");
  5064. verifyFormat("virtual void foo(char &) const;");
  5065. verifyFormat("virtual void foo(int *a, char *) const;");
  5066. verifyFormat("int a = sizeof(int *) + b;");
  5067. verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
  5068. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
  5069. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  5070. // FIXME: The indentation here is not ideal.
  5071. verifyFormat(
  5072. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5073. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
  5074. " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
  5075. }
  5076. TEST_F(FormatTest, FormatsFunctionTypes) {
  5077. verifyFormat("A<bool()> a;");
  5078. verifyFormat("A<SomeType()> a;");
  5079. verifyFormat("A<void (*)(int, std::string)> a;");
  5080. verifyFormat("A<void *(int)>;");
  5081. verifyFormat("void *(*a)(int *, SomeType *);");
  5082. verifyFormat("int (*func)(void *);");
  5083. verifyFormat("void f() { int (*func)(void *); }");
  5084. verifyFormat("template <class CallbackClass>\n"
  5085. "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
  5086. verifyGoogleFormat("A<void*(int*, SomeType*)>;");
  5087. verifyGoogleFormat("void* (*a)(int);");
  5088. verifyGoogleFormat(
  5089. "template <class CallbackClass>\n"
  5090. "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
  5091. // Other constructs can look somewhat like function types:
  5092. verifyFormat("A<sizeof(*x)> a;");
  5093. verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
  5094. verifyFormat("some_var = function(*some_pointer_var)[0];");
  5095. verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
  5096. }
  5097. TEST_F(FormatTest, BreaksLongVariableDeclarations) {
  5098. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5099. " LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5100. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
  5101. " LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5102. // Different ways of ()-initializiation.
  5103. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5104. " LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
  5105. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5106. " LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
  5107. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5108. " LoooooooooooooooooooooooooooooooooooooooongVariable({});");
  5109. }
  5110. TEST_F(FormatTest, BreaksLongDeclarations) {
  5111. verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
  5112. " AnotherNameForTheLongType;");
  5113. verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
  5114. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  5115. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5116. "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
  5117. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5118. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5119. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
  5120. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5121. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  5122. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5123. verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
  5124. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5125. FormatStyle Indented = getLLVMStyle();
  5126. Indented.IndentWrappedFunctionNames = true;
  5127. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5128. " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
  5129. Indented);
  5130. verifyFormat(
  5131. "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5132. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5133. Indented);
  5134. verifyFormat(
  5135. "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  5136. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5137. Indented);
  5138. verifyFormat(
  5139. "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
  5140. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5141. Indented);
  5142. // FIXME: Without the comment, this breaks after "(".
  5143. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
  5144. " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
  5145. getGoogleStyle());
  5146. verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
  5147. " int LoooooooooooooooooooongParam2) {}");
  5148. verifyFormat(
  5149. "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
  5150. " SourceLocation L, IdentifierIn *II,\n"
  5151. " Type *T) {}");
  5152. verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
  5153. "ReallyReallyLongFunctionName(\n"
  5154. " const std::string &SomeParameter,\n"
  5155. " const SomeType<string, SomeOtherTemplateParameter> &\n"
  5156. " ReallyReallyLongParameterName,\n"
  5157. " const SomeType<string, SomeOtherTemplateParameter> &\n"
  5158. " AnotherLongParameterName) {}");
  5159. verifyFormat("template <typename A>\n"
  5160. "SomeLoooooooooooooooooooooongType<\n"
  5161. " typename some_namespace::SomeOtherType<A>::Type>\n"
  5162. "Function() {}");
  5163. verifyGoogleFormat(
  5164. "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
  5165. " aaaaaaaaaaaaaaaaaaaaaaa;");
  5166. verifyGoogleFormat(
  5167. "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
  5168. " SourceLocation L) {}");
  5169. verifyGoogleFormat(
  5170. "some_namespace::LongReturnType\n"
  5171. "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
  5172. " int first_long_parameter, int second_parameter) {}");
  5173. verifyGoogleFormat("template <typename T>\n"
  5174. "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  5175. "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
  5176. verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5177. " int aaaaaaaaaaaaaaaaaaaaaaa);");
  5178. verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  5179. " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  5180. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5181. }
  5182. TEST_F(FormatTest, FormatsArrays) {
  5183. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5184. " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
  5185. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5186. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  5187. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5188. " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
  5189. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5190. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5191. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  5192. verifyFormat(
  5193. "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
  5194. " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5195. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  5196. verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
  5197. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
  5198. verifyFormat(
  5199. "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
  5200. " .aaaaaaa[0]\n"
  5201. " .aaaaaaaaaaaaaaaaaaaaaa();");
  5202. }
  5203. TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
  5204. verifyFormat("(a)->b();");
  5205. verifyFormat("--a;");
  5206. }
  5207. TEST_F(FormatTest, HandlesIncludeDirectives) {
  5208. verifyFormat("#include <string>\n"
  5209. "#include <a/b/c.h>\n"
  5210. "#include \"a/b/string\"\n"
  5211. "#include \"string.h\"\n"
  5212. "#include \"string.h\"\n"
  5213. "#include <a-a>\n"
  5214. "#include < path with space >\n"
  5215. "#include \"abc.h\" // this is included for ABC\n"
  5216. "#include \"some long include\" // with a comment\n"
  5217. "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
  5218. getLLVMStyleWithColumns(35));
  5219. EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\""));
  5220. EXPECT_EQ("#include <a>", format("#include<a>"));
  5221. verifyFormat("#import <string>");
  5222. verifyFormat("#import <a/b/c.h>");
  5223. verifyFormat("#import \"a/b/string\"");
  5224. verifyFormat("#import \"string.h\"");
  5225. verifyFormat("#import \"string.h\"");
  5226. verifyFormat("#if __has_include(<strstream>)\n"
  5227. "#include <strstream>\n"
  5228. "#endif");
  5229. verifyFormat("#define MY_IMPORT <a/b>");
  5230. // Protocol buffer definition or missing "#".
  5231. verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
  5232. getLLVMStyleWithColumns(30));
  5233. FormatStyle Style = getLLVMStyle();
  5234. Style.AlwaysBreakBeforeMultilineStrings = true;
  5235. Style.ColumnLimit = 0;
  5236. verifyFormat("#import \"abc.h\"", Style);
  5237. // But 'import' might also be a regular C++ namespace.
  5238. verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5239. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5240. }
  5241. //===----------------------------------------------------------------------===//
  5242. // Error recovery tests.
  5243. //===----------------------------------------------------------------------===//
  5244. TEST_F(FormatTest, IncompleteParameterLists) {
  5245. FormatStyle NoBinPacking = getLLVMStyle();
  5246. NoBinPacking.BinPackParameters = false;
  5247. verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
  5248. " double *min_x,\n"
  5249. " double *max_x,\n"
  5250. " double *min_y,\n"
  5251. " double *max_y,\n"
  5252. " double *min_z,\n"
  5253. " double *max_z, ) {}",
  5254. NoBinPacking);
  5255. }
  5256. TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
  5257. verifyFormat("void f() { return; }\n42");
  5258. verifyFormat("void f() {\n"
  5259. " if (0)\n"
  5260. " return;\n"
  5261. "}\n"
  5262. "42");
  5263. verifyFormat("void f() { return }\n42");
  5264. verifyFormat("void f() {\n"
  5265. " if (0)\n"
  5266. " return\n"
  5267. "}\n"
  5268. "42");
  5269. }
  5270. TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
  5271. EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
  5272. EXPECT_EQ("void f() {\n"
  5273. " if (a)\n"
  5274. " return\n"
  5275. "}",
  5276. format("void f ( ) { if ( a ) return }"));
  5277. EXPECT_EQ("namespace N {\n"
  5278. "void f()\n"
  5279. "}",
  5280. format("namespace N { void f() }"));
  5281. EXPECT_EQ("namespace N {\n"
  5282. "void f() {}\n"
  5283. "void g()\n"
  5284. "}",
  5285. format("namespace N { void f( ) { } void g( ) }"));
  5286. }
  5287. TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
  5288. verifyFormat("int aaaaaaaa =\n"
  5289. " // Overlylongcomment\n"
  5290. " b;",
  5291. getLLVMStyleWithColumns(20));
  5292. verifyFormat("function(\n"
  5293. " ShortArgument,\n"
  5294. " LoooooooooooongArgument);\n",
  5295. getLLVMStyleWithColumns(20));
  5296. }
  5297. TEST_F(FormatTest, IncorrectAccessSpecifier) {
  5298. verifyFormat("public:");
  5299. verifyFormat("class A {\n"
  5300. "public\n"
  5301. " void f() {}\n"
  5302. "};");
  5303. verifyFormat("public\n"
  5304. "int qwerty;");
  5305. verifyFormat("public\n"
  5306. "B {}");
  5307. verifyFormat("public\n"
  5308. "{}");
  5309. verifyFormat("public\n"
  5310. "B { int x; }");
  5311. }
  5312. TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
  5313. verifyFormat("{");
  5314. verifyFormat("#})");
  5315. }
  5316. TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
  5317. verifyFormat("do {\n}");
  5318. verifyFormat("do {\n}\n"
  5319. "f();");
  5320. verifyFormat("do {\n}\n"
  5321. "wheeee(fun);");
  5322. verifyFormat("do {\n"
  5323. " f();\n"
  5324. "}");
  5325. }
  5326. TEST_F(FormatTest, IncorrectCodeMissingParens) {
  5327. verifyFormat("if {\n foo;\n foo();\n}");
  5328. verifyFormat("switch {\n foo;\n foo();\n}");
  5329. verifyFormat("for {\n foo;\n foo();\n}");
  5330. verifyFormat("while {\n foo;\n foo();\n}");
  5331. verifyFormat("do {\n foo;\n foo();\n} while;");
  5332. }
  5333. TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
  5334. verifyFormat("namespace {\n"
  5335. "class Foo { Foo (\n"
  5336. "};\n"
  5337. "} // comment");
  5338. }
  5339. TEST_F(FormatTest, IncorrectCodeErrorDetection) {
  5340. EXPECT_EQ("{\n {}\n", format("{\n{\n}\n"));
  5341. EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
  5342. EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
  5343. EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n"));
  5344. EXPECT_EQ("{\n"
  5345. " {\n"
  5346. " breakme(\n"
  5347. " qwe);\n"
  5348. " }\n",
  5349. format("{\n"
  5350. " {\n"
  5351. " breakme(qwe);\n"
  5352. "}\n",
  5353. getLLVMStyleWithColumns(10)));
  5354. }
  5355. TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
  5356. verifyFormat("int x = {\n"
  5357. " avariable,\n"
  5358. " b(alongervariable)};",
  5359. getLLVMStyleWithColumns(25));
  5360. }
  5361. TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
  5362. verifyFormat("return (a)(b){1, 2, 3};");
  5363. }
  5364. TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
  5365. verifyFormat("vector<int> x{1, 2, 3, 4};");
  5366. verifyFormat("vector<int> x{\n"
  5367. " 1, 2, 3, 4,\n"
  5368. "};");
  5369. verifyFormat("vector<T> x{{}, {}, {}, {}};");
  5370. verifyFormat("f({1, 2});");
  5371. verifyFormat("auto v = Foo{-1};");
  5372. verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
  5373. verifyFormat("Class::Class : member{1, 2, 3} {}");
  5374. verifyFormat("new vector<int>{1, 2, 3};");
  5375. verifyFormat("new int[3]{1, 2, 3};");
  5376. verifyFormat("new int{1};");
  5377. verifyFormat("return {arg1, arg2};");
  5378. verifyFormat("return {arg1, SomeType{parameter}};");
  5379. verifyFormat("int count = set<int>{f(), g(), h()}.size();");
  5380. verifyFormat("new T{arg1, arg2};");
  5381. verifyFormat("f(MyMap[{composite, key}]);");
  5382. verifyFormat("class Class {\n"
  5383. " T member = {arg1, arg2};\n"
  5384. "};");
  5385. verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
  5386. verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
  5387. verifyFormat("int a = std::is_integral<int>{} + 0;");
  5388. verifyFormat("int foo(int i) { return fo1{}(i); }");
  5389. verifyFormat("int foo(int i) { return fo1{}(i); }");
  5390. verifyFormat("auto i = decltype(x){};");
  5391. verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
  5392. verifyFormat("Node n{1, Node{1000}, //\n"
  5393. " 2};");
  5394. // In combination with BinPackParameters = false.
  5395. FormatStyle NoBinPacking = getLLVMStyle();
  5396. NoBinPacking.BinPackParameters = false;
  5397. verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
  5398. " bbbbb,\n"
  5399. " ccccc,\n"
  5400. " ddddd,\n"
  5401. " eeeee,\n"
  5402. " ffffff,\n"
  5403. " ggggg,\n"
  5404. " hhhhhh,\n"
  5405. " iiiiii,\n"
  5406. " jjjjjj,\n"
  5407. " kkkkkk};",
  5408. NoBinPacking);
  5409. verifyFormat("const Aaaaaa aaaaa = {\n"
  5410. " aaaaa,\n"
  5411. " bbbbb,\n"
  5412. " ccccc,\n"
  5413. " ddddd,\n"
  5414. " eeeee,\n"
  5415. " ffffff,\n"
  5416. " ggggg,\n"
  5417. " hhhhhh,\n"
  5418. " iiiiii,\n"
  5419. " jjjjjj,\n"
  5420. " kkkkkk,\n"
  5421. "};",
  5422. NoBinPacking);
  5423. verifyFormat(
  5424. "const Aaaaaa aaaaa = {\n"
  5425. " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n"
  5426. " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n"
  5427. " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
  5428. "};",
  5429. NoBinPacking);
  5430. // FIXME: The alignment of these trailing comments might be bad. Then again,
  5431. // this might be utterly useless in real code.
  5432. verifyFormat("Constructor::Constructor()\n"
  5433. " : some_value{ //\n"
  5434. " aaaaaaa, //\n"
  5435. " bbbbbbb} {}");
  5436. // In braced lists, the first comment is always assumed to belong to the
  5437. // first element. Thus, it can be moved to the next or previous line as
  5438. // appropriate.
  5439. EXPECT_EQ("function({// First element:\n"
  5440. " 1,\n"
  5441. " // Second element:\n"
  5442. " 2});",
  5443. format("function({\n"
  5444. " // First element:\n"
  5445. " 1,\n"
  5446. " // Second element:\n"
  5447. " 2});"));
  5448. EXPECT_EQ("std::vector<int> MyNumbers{\n"
  5449. " // First element:\n"
  5450. " 1,\n"
  5451. " // Second element:\n"
  5452. " 2};",
  5453. format("std::vector<int> MyNumbers{// First element:\n"
  5454. " 1,\n"
  5455. " // Second element:\n"
  5456. " 2};",
  5457. getLLVMStyleWithColumns(30)));
  5458. // A trailing comma should still lead to an enforced line break.
  5459. EXPECT_EQ("vector<int> SomeVector = {\n"
  5460. " // aaa\n"
  5461. " 1, 2,\n"
  5462. "};",
  5463. format("vector<int> SomeVector = { // aaa\n"
  5464. " 1, 2, };"));
  5465. FormatStyle ExtraSpaces = getLLVMStyle();
  5466. ExtraSpaces.Cpp11BracedListStyle = false;
  5467. ExtraSpaces.ColumnLimit = 75;
  5468. verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
  5469. verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
  5470. verifyFormat("f({ 1, 2 });", ExtraSpaces);
  5471. verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
  5472. verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
  5473. verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
  5474. verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
  5475. verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
  5476. verifyFormat("return { arg1, arg2 };", ExtraSpaces);
  5477. verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
  5478. verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
  5479. verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
  5480. verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
  5481. verifyFormat("class Class {\n"
  5482. " T member = { arg1, arg2 };\n"
  5483. "};",
  5484. ExtraSpaces);
  5485. verifyFormat(
  5486. "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5487. " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
  5488. " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  5489. " bbbbbbbbbbbbbbbbbbbb, bbbbb };",
  5490. ExtraSpaces);
  5491. verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
  5492. verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
  5493. ExtraSpaces);
  5494. verifyFormat(
  5495. "someFunction(OtherParam,\n"
  5496. " BracedList{ // comment 1 (Forcing interesting break)\n"
  5497. " param1, param2,\n"
  5498. " // comment 2\n"
  5499. " param3, param4 });",
  5500. ExtraSpaces);
  5501. verifyFormat(
  5502. "std::this_thread::sleep_for(\n"
  5503. " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
  5504. ExtraSpaces);
  5505. verifyFormat(
  5506. "std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
  5507. " aaaaaaa, aaaaaaaaaa, aaaaa, aaaaaaaaaaaaaaa, aaa, aaaaaaaaaa, a,\n"
  5508. " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaa,\n"
  5509. " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa, aaaaaaa, a};");
  5510. verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
  5511. }
  5512. TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
  5513. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5514. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5515. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5516. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5517. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5518. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  5519. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5520. " // line comment\n"
  5521. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5522. " 1, 22, 333, 4444, 55555,\n"
  5523. " // line comment\n"
  5524. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5525. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  5526. verifyFormat(
  5527. "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5528. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5529. " 1, 22, 333, 4444, 55555, 666666, // comment\n"
  5530. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  5531. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  5532. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  5533. " 7777777};");
  5534. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  5535. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  5536. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  5537. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  5538. " 1, 1, 1, 1};",
  5539. getLLVMStyleWithColumns(39));
  5540. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  5541. " 1, 1, 1, 1};",
  5542. getLLVMStyleWithColumns(38));
  5543. verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
  5544. " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
  5545. getLLVMStyleWithColumns(43));
  5546. // Trailing commas.
  5547. verifyFormat("vector<int> x = {\n"
  5548. " 1, 1, 1, 1, 1, 1, 1, 1,\n"
  5549. "};",
  5550. getLLVMStyleWithColumns(39));
  5551. verifyFormat("vector<int> x = {\n"
  5552. " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
  5553. "};",
  5554. getLLVMStyleWithColumns(39));
  5555. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  5556. " 1, 1, 1, 1,\n"
  5557. " /**/ /**/};",
  5558. getLLVMStyleWithColumns(39));
  5559. verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
  5560. " {aaaaaaaaaaaaaaaaaaa},\n"
  5561. " {aaaaaaaaaaaaaaaaaaaaa},\n"
  5562. " {aaaaaaaaaaaaaaaaa}};",
  5563. getLLVMStyleWithColumns(60));
  5564. // With nested lists, we should either format one item per line or all nested
  5565. // lists one one line.
  5566. // FIXME: For some nested lists, we can do better.
  5567. verifyFormat(
  5568. "SomeStruct my_struct_array = {\n"
  5569. " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
  5570. " aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
  5571. " {aaa, aaa},\n"
  5572. " {aaa, aaa},\n"
  5573. " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
  5574. " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5575. " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
  5576. // No column layout should be used here.
  5577. verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
  5578. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
  5579. }
  5580. TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
  5581. FormatStyle DoNotMerge = getLLVMStyle();
  5582. DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  5583. verifyFormat("void f() { return 42; }");
  5584. verifyFormat("void f() {\n"
  5585. " return 42;\n"
  5586. "}",
  5587. DoNotMerge);
  5588. verifyFormat("void f() {\n"
  5589. " // Comment\n"
  5590. "}");
  5591. verifyFormat("{\n"
  5592. "#error {\n"
  5593. " int a;\n"
  5594. "}");
  5595. verifyFormat("{\n"
  5596. " int a;\n"
  5597. "#error {\n"
  5598. "}");
  5599. verifyFormat("void f() {} // comment");
  5600. verifyFormat("void f() { int a; } // comment");
  5601. verifyFormat("void f() {\n"
  5602. "} // comment",
  5603. DoNotMerge);
  5604. verifyFormat("void f() {\n"
  5605. " int a;\n"
  5606. "} // comment",
  5607. DoNotMerge);
  5608. verifyFormat("void f() {\n"
  5609. "} // comment",
  5610. getLLVMStyleWithColumns(15));
  5611. verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
  5612. verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
  5613. verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
  5614. verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
  5615. verifyFormat("class C {\n"
  5616. " C()\n"
  5617. " : iiiiiiii(nullptr),\n"
  5618. " kkkkkkk(nullptr),\n"
  5619. " mmmmmmm(nullptr),\n"
  5620. " nnnnnnn(nullptr) {}\n"
  5621. "};",
  5622. getGoogleStyle());
  5623. FormatStyle NoColumnLimit = getLLVMStyle();
  5624. NoColumnLimit.ColumnLimit = 0;
  5625. EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
  5626. EXPECT_EQ("class C {\n"
  5627. " A() : b(0) {}\n"
  5628. "};", format("class C{A():b(0){}};", NoColumnLimit));
  5629. EXPECT_EQ("A()\n"
  5630. " : b(0) {\n"
  5631. "}",
  5632. format("A()\n:b(0)\n{\n}", NoColumnLimit));
  5633. FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
  5634. DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
  5635. FormatStyle::SFS_None;
  5636. EXPECT_EQ("A()\n"
  5637. " : b(0) {\n"
  5638. "}",
  5639. format("A():b(0){}", DoNotMergeNoColumnLimit));
  5640. EXPECT_EQ("A()\n"
  5641. " : b(0) {\n"
  5642. "}",
  5643. format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
  5644. verifyFormat("#define A \\\n"
  5645. " void f() { \\\n"
  5646. " int i; \\\n"
  5647. " }",
  5648. getLLVMStyleWithColumns(20));
  5649. verifyFormat("#define A \\\n"
  5650. " void f() { int i; }",
  5651. getLLVMStyleWithColumns(21));
  5652. verifyFormat("#define A \\\n"
  5653. " void f() { \\\n"
  5654. " int i; \\\n"
  5655. " } \\\n"
  5656. " int j;",
  5657. getLLVMStyleWithColumns(22));
  5658. verifyFormat("#define A \\\n"
  5659. " void f() { int i; } \\\n"
  5660. " int j;",
  5661. getLLVMStyleWithColumns(23));
  5662. }
  5663. TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
  5664. FormatStyle MergeInlineOnly = getLLVMStyle();
  5665. MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  5666. verifyFormat("class C {\n"
  5667. " int f() { return 42; }\n"
  5668. "};",
  5669. MergeInlineOnly);
  5670. verifyFormat("int f() {\n"
  5671. " return 42;\n"
  5672. "}",
  5673. MergeInlineOnly);
  5674. }
  5675. TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
  5676. // Elaborate type variable declarations.
  5677. verifyFormat("struct foo a = {bar};\nint n;");
  5678. verifyFormat("class foo a = {bar};\nint n;");
  5679. verifyFormat("union foo a = {bar};\nint n;");
  5680. // Elaborate types inside function definitions.
  5681. verifyFormat("struct foo f() {}\nint n;");
  5682. verifyFormat("class foo f() {}\nint n;");
  5683. verifyFormat("union foo f() {}\nint n;");
  5684. // Templates.
  5685. verifyFormat("template <class X> void f() {}\nint n;");
  5686. verifyFormat("template <struct X> void f() {}\nint n;");
  5687. verifyFormat("template <union X> void f() {}\nint n;");
  5688. // Actual definitions...
  5689. verifyFormat("struct {\n} n;");
  5690. verifyFormat(
  5691. "template <template <class T, class Y>, class Z> class X {\n} n;");
  5692. verifyFormat("union Z {\n int n;\n} x;");
  5693. verifyFormat("class MACRO Z {\n} n;");
  5694. verifyFormat("class MACRO(X) Z {\n} n;");
  5695. verifyFormat("class __attribute__(X) Z {\n} n;");
  5696. verifyFormat("class __declspec(X) Z {\n} n;");
  5697. verifyFormat("class A##B##C {\n} n;");
  5698. verifyFormat("class alignas(16) Z {\n} n;");
  5699. // Redefinition from nested context:
  5700. verifyFormat("class A::B::C {\n} n;");
  5701. // Template definitions.
  5702. verifyFormat(
  5703. "template <typename F>\n"
  5704. "Matcher(const Matcher<F> &Other,\n"
  5705. " typename enable_if_c<is_base_of<F, T>::value &&\n"
  5706. " !is_same<F, T>::value>::type * = 0)\n"
  5707. " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
  5708. // FIXME: This is still incorrectly handled at the formatter side.
  5709. verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
  5710. // FIXME:
  5711. // This now gets parsed incorrectly as class definition.
  5712. // verifyFormat("class A<int> f() {\n}\nint n;");
  5713. // Elaborate types where incorrectly parsing the structural element would
  5714. // break the indent.
  5715. verifyFormat("if (true)\n"
  5716. " class X x;\n"
  5717. "else\n"
  5718. " f();\n");
  5719. // This is simply incomplete. Formatting is not important, but must not crash.
  5720. verifyFormat("class A:");
  5721. }
  5722. TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
  5723. EXPECT_EQ("#error Leave all white!!!!! space* alone!\n",
  5724. format("#error Leave all white!!!!! space* alone!\n"));
  5725. EXPECT_EQ(
  5726. "#warning Leave all white!!!!! space* alone!\n",
  5727. format("#warning Leave all white!!!!! space* alone!\n"));
  5728. EXPECT_EQ("#error 1", format(" # error 1"));
  5729. EXPECT_EQ("#warning 1", format(" # warning 1"));
  5730. }
  5731. TEST_F(FormatTest, FormatHashIfExpressions) {
  5732. verifyFormat("#if AAAA && BBBB");
  5733. // FIXME: Come up with a better indentation for #elif.
  5734. verifyFormat(
  5735. "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
  5736. " defined(BBBBBBBB)\n"
  5737. "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
  5738. " defined(BBBBBBBB)\n"
  5739. "#endif",
  5740. getLLVMStyleWithColumns(65));
  5741. }
  5742. TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
  5743. FormatStyle AllowsMergedIf = getGoogleStyle();
  5744. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  5745. verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
  5746. verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
  5747. verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
  5748. EXPECT_EQ("if (true) return 42;",
  5749. format("if (true)\nreturn 42;", AllowsMergedIf));
  5750. FormatStyle ShortMergedIf = AllowsMergedIf;
  5751. ShortMergedIf.ColumnLimit = 25;
  5752. verifyFormat("#define A \\\n"
  5753. " if (true) return 42;",
  5754. ShortMergedIf);
  5755. verifyFormat("#define A \\\n"
  5756. " f(); \\\n"
  5757. " if (true)\n"
  5758. "#define B",
  5759. ShortMergedIf);
  5760. verifyFormat("#define A \\\n"
  5761. " f(); \\\n"
  5762. " if (true)\n"
  5763. "g();",
  5764. ShortMergedIf);
  5765. verifyFormat("{\n"
  5766. "#ifdef A\n"
  5767. " // Comment\n"
  5768. " if (true) continue;\n"
  5769. "#endif\n"
  5770. " // Comment\n"
  5771. " if (true) continue;\n"
  5772. "}",
  5773. ShortMergedIf);
  5774. ShortMergedIf.ColumnLimit = 29;
  5775. verifyFormat("#define A \\\n"
  5776. " if (aaaaaaaaaa) return 1; \\\n"
  5777. " return 2;",
  5778. ShortMergedIf);
  5779. ShortMergedIf.ColumnLimit = 28;
  5780. verifyFormat("#define A \\\n"
  5781. " if (aaaaaaaaaa) \\\n"
  5782. " return 1; \\\n"
  5783. " return 2;",
  5784. ShortMergedIf);
  5785. }
  5786. TEST_F(FormatTest, BlockCommentsInControlLoops) {
  5787. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  5788. " f();\n"
  5789. "}");
  5790. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  5791. " f();\n"
  5792. "} /* another comment */ else /* comment #3 */ {\n"
  5793. " g();\n"
  5794. "}");
  5795. verifyFormat("while (0) /* a comment in a strange place */ {\n"
  5796. " f();\n"
  5797. "}");
  5798. verifyFormat("for (;;) /* a comment in a strange place */ {\n"
  5799. " f();\n"
  5800. "}");
  5801. verifyFormat("do /* a comment in a strange place */ {\n"
  5802. " f();\n"
  5803. "} /* another comment */ while (0);");
  5804. }
  5805. TEST_F(FormatTest, BlockComments) {
  5806. EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
  5807. format("/* *//* */ /* */\n/* *//* */ /* */"));
  5808. EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
  5809. EXPECT_EQ("#define A /*123*/ \\\n"
  5810. " b\n"
  5811. "/* */\n"
  5812. "someCall(\n"
  5813. " parameter);",
  5814. format("#define A /*123*/ b\n"
  5815. "/* */\n"
  5816. "someCall(parameter);",
  5817. getLLVMStyleWithColumns(15)));
  5818. EXPECT_EQ("#define A\n"
  5819. "/* */ someCall(\n"
  5820. " parameter);",
  5821. format("#define A\n"
  5822. "/* */someCall(parameter);",
  5823. getLLVMStyleWithColumns(15)));
  5824. EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
  5825. EXPECT_EQ("/*\n"
  5826. "*\n"
  5827. " * aaaaaa\n"
  5828. "*aaaaaa\n"
  5829. "*/",
  5830. format("/*\n"
  5831. "*\n"
  5832. " * aaaaaa aaaaaa\n"
  5833. "*/",
  5834. getLLVMStyleWithColumns(10)));
  5835. EXPECT_EQ("/*\n"
  5836. "**\n"
  5837. "* aaaaaa\n"
  5838. "*aaaaaa\n"
  5839. "*/",
  5840. format("/*\n"
  5841. "**\n"
  5842. "* aaaaaa aaaaaa\n"
  5843. "*/",
  5844. getLLVMStyleWithColumns(10)));
  5845. FormatStyle NoBinPacking = getLLVMStyle();
  5846. NoBinPacking.BinPackParameters = false;
  5847. EXPECT_EQ("someFunction(1, /* comment 1 */\n"
  5848. " 2, /* comment 2 */\n"
  5849. " 3, /* comment 3 */\n"
  5850. " aaaa,\n"
  5851. " bbbb);",
  5852. format("someFunction (1, /* comment 1 */\n"
  5853. " 2, /* comment 2 */ \n"
  5854. " 3, /* comment 3 */\n"
  5855. "aaaa, bbbb );",
  5856. NoBinPacking));
  5857. verifyFormat(
  5858. "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  5859. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  5860. EXPECT_EQ(
  5861. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  5862. " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  5863. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
  5864. format(
  5865. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  5866. " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  5867. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
  5868. EXPECT_EQ(
  5869. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  5870. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  5871. "int cccccccccccccccccccccccccccccc; /* comment */\n",
  5872. format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  5873. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  5874. "int cccccccccccccccccccccccccccccc; /* comment */\n"));
  5875. verifyFormat("void f(int * /* unused */) {}");
  5876. EXPECT_EQ("/*\n"
  5877. " **\n"
  5878. " */",
  5879. format("/*\n"
  5880. " **\n"
  5881. " */"));
  5882. EXPECT_EQ("/*\n"
  5883. " *q\n"
  5884. " */",
  5885. format("/*\n"
  5886. " *q\n"
  5887. " */"));
  5888. EXPECT_EQ("/*\n"
  5889. " * q\n"
  5890. " */",
  5891. format("/*\n"
  5892. " * q\n"
  5893. " */"));
  5894. EXPECT_EQ("/*\n"
  5895. " **/",
  5896. format("/*\n"
  5897. " **/"));
  5898. EXPECT_EQ("/*\n"
  5899. " ***/",
  5900. format("/*\n"
  5901. " ***/"));
  5902. }
  5903. TEST_F(FormatTest, BlockCommentsInMacros) {
  5904. EXPECT_EQ("#define A \\\n"
  5905. " { \\\n"
  5906. " /* one line */ \\\n"
  5907. " someCall();",
  5908. format("#define A { \\\n"
  5909. " /* one line */ \\\n"
  5910. " someCall();",
  5911. getLLVMStyleWithColumns(20)));
  5912. EXPECT_EQ("#define A \\\n"
  5913. " { \\\n"
  5914. " /* previous */ \\\n"
  5915. " /* one line */ \\\n"
  5916. " someCall();",
  5917. format("#define A { \\\n"
  5918. " /* previous */ \\\n"
  5919. " /* one line */ \\\n"
  5920. " someCall();",
  5921. getLLVMStyleWithColumns(20)));
  5922. }
  5923. TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
  5924. EXPECT_EQ("a = {\n"
  5925. " 1111 /* */\n"
  5926. "};",
  5927. format("a = {1111 /* */\n"
  5928. "};",
  5929. getLLVMStyleWithColumns(15)));
  5930. EXPECT_EQ("a = {\n"
  5931. " 1111 /* */\n"
  5932. "};",
  5933. format("a = {1111 /* */\n"
  5934. "};",
  5935. getLLVMStyleWithColumns(15)));
  5936. // FIXME: The formatting is still wrong here.
  5937. EXPECT_EQ("a = {\n"
  5938. " 1111 /* a\n"
  5939. " */\n"
  5940. "};",
  5941. format("a = {1111 /* a */\n"
  5942. "};",
  5943. getLLVMStyleWithColumns(15)));
  5944. }
  5945. TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
  5946. // FIXME: This is not what we want...
  5947. verifyFormat("{\n"
  5948. "// a"
  5949. "// b");
  5950. }
  5951. TEST_F(FormatTest, FormatStarDependingOnContext) {
  5952. verifyFormat("void f(int *a);");
  5953. verifyFormat("void f() { f(fint * b); }");
  5954. verifyFormat("class A {\n void f(int *a);\n};");
  5955. verifyFormat("class A {\n int *a;\n};");
  5956. verifyFormat("namespace a {\n"
  5957. "namespace b {\n"
  5958. "class A {\n"
  5959. " void f() {}\n"
  5960. " int *a;\n"
  5961. "};\n"
  5962. "}\n"
  5963. "}");
  5964. }
  5965. TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
  5966. verifyFormat("while");
  5967. verifyFormat("operator");
  5968. }
  5969. //===----------------------------------------------------------------------===//
  5970. // Objective-C tests.
  5971. //===----------------------------------------------------------------------===//
  5972. TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
  5973. verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
  5974. EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
  5975. format("-(NSUInteger)indexOfObject:(id)anObject;"));
  5976. EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
  5977. EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
  5978. EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
  5979. format("-(NSInteger)Method3:(id)anObject;"));
  5980. EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
  5981. format("-(NSInteger)Method4:(id)anObject;"));
  5982. EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
  5983. format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
  5984. EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
  5985. format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
  5986. EXPECT_EQ(
  5987. "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
  5988. format(
  5989. "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
  5990. // Very long objectiveC method declaration.
  5991. verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
  5992. " inRange:(NSRange)range\n"
  5993. " outRange:(NSRange)out_range\n"
  5994. " outRange1:(NSRange)out_range1\n"
  5995. " outRange2:(NSRange)out_range2\n"
  5996. " outRange3:(NSRange)out_range3\n"
  5997. " outRange4:(NSRange)out_range4\n"
  5998. " outRange5:(NSRange)out_range5\n"
  5999. " outRange6:(NSRange)out_range6\n"
  6000. " outRange7:(NSRange)out_range7\n"
  6001. " outRange8:(NSRange)out_range8\n"
  6002. " outRange9:(NSRange)out_range9;");
  6003. verifyFormat("- (int)sum:(vector<int>)numbers;");
  6004. verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
  6005. // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
  6006. // protocol lists (but not for template classes):
  6007. //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
  6008. verifyFormat("- (int (*)())foo:(int (*)())f;");
  6009. verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
  6010. // If there's no return type (very rare in practice!), LLVM and Google style
  6011. // agree.
  6012. verifyFormat("- foo;");
  6013. verifyFormat("- foo:(int)f;");
  6014. verifyGoogleFormat("- foo:(int)foo;");
  6015. }
  6016. TEST_F(FormatTest, FormatObjCInterface) {
  6017. verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
  6018. "@public\n"
  6019. " int field1;\n"
  6020. "@protected\n"
  6021. " int field2;\n"
  6022. "@private\n"
  6023. " int field3;\n"
  6024. "@package\n"
  6025. " int field4;\n"
  6026. "}\n"
  6027. "+ (id)init;\n"
  6028. "@end");
  6029. verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
  6030. " @public\n"
  6031. " int field1;\n"
  6032. " @protected\n"
  6033. " int field2;\n"
  6034. " @private\n"
  6035. " int field3;\n"
  6036. " @package\n"
  6037. " int field4;\n"
  6038. "}\n"
  6039. "+ (id)init;\n"
  6040. "@end");
  6041. verifyFormat("@interface /* wait for it */ Foo\n"
  6042. "+ (id)init;\n"
  6043. "// Look, a comment!\n"
  6044. "- (int)answerWith:(int)i;\n"
  6045. "@end");
  6046. verifyFormat("@interface Foo\n"
  6047. "@end\n"
  6048. "@interface Bar\n"
  6049. "@end");
  6050. verifyFormat("@interface Foo : Bar\n"
  6051. "+ (id)init;\n"
  6052. "@end");
  6053. verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
  6054. "+ (id)init;\n"
  6055. "@end");
  6056. verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
  6057. "+ (id)init;\n"
  6058. "@end");
  6059. verifyFormat("@interface Foo (HackStuff)\n"
  6060. "+ (id)init;\n"
  6061. "@end");
  6062. verifyFormat("@interface Foo ()\n"
  6063. "+ (id)init;\n"
  6064. "@end");
  6065. verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
  6066. "+ (id)init;\n"
  6067. "@end");
  6068. verifyGoogleFormat("@interface Foo (HackStuff) <MyProtocol>\n"
  6069. "+ (id)init;\n"
  6070. "@end");
  6071. verifyFormat("@interface Foo {\n"
  6072. " int _i;\n"
  6073. "}\n"
  6074. "+ (id)init;\n"
  6075. "@end");
  6076. verifyFormat("@interface Foo : Bar {\n"
  6077. " int _i;\n"
  6078. "}\n"
  6079. "+ (id)init;\n"
  6080. "@end");
  6081. verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
  6082. " int _i;\n"
  6083. "}\n"
  6084. "+ (id)init;\n"
  6085. "@end");
  6086. verifyFormat("@interface Foo (HackStuff) {\n"
  6087. " int _i;\n"
  6088. "}\n"
  6089. "+ (id)init;\n"
  6090. "@end");
  6091. verifyFormat("@interface Foo () {\n"
  6092. " int _i;\n"
  6093. "}\n"
  6094. "+ (id)init;\n"
  6095. "@end");
  6096. verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
  6097. " int _i;\n"
  6098. "}\n"
  6099. "+ (id)init;\n"
  6100. "@end");
  6101. FormatStyle OnePerLine = getGoogleStyle();
  6102. OnePerLine.BinPackParameters = false;
  6103. verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa () <\n"
  6104. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6105. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6106. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6107. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
  6108. "}",
  6109. OnePerLine);
  6110. }
  6111. TEST_F(FormatTest, FormatObjCImplementation) {
  6112. verifyFormat("@implementation Foo : NSObject {\n"
  6113. "@public\n"
  6114. " int field1;\n"
  6115. "@protected\n"
  6116. " int field2;\n"
  6117. "@private\n"
  6118. " int field3;\n"
  6119. "@package\n"
  6120. " int field4;\n"
  6121. "}\n"
  6122. "+ (id)init {\n}\n"
  6123. "@end");
  6124. verifyGoogleFormat("@implementation Foo : NSObject {\n"
  6125. " @public\n"
  6126. " int field1;\n"
  6127. " @protected\n"
  6128. " int field2;\n"
  6129. " @private\n"
  6130. " int field3;\n"
  6131. " @package\n"
  6132. " int field4;\n"
  6133. "}\n"
  6134. "+ (id)init {\n}\n"
  6135. "@end");
  6136. verifyFormat("@implementation Foo\n"
  6137. "+ (id)init {\n"
  6138. " if (true)\n"
  6139. " return nil;\n"
  6140. "}\n"
  6141. "// Look, a comment!\n"
  6142. "- (int)answerWith:(int)i {\n"
  6143. " return i;\n"
  6144. "}\n"
  6145. "+ (int)answerWith:(int)i {\n"
  6146. " return i;\n"
  6147. "}\n"
  6148. "@end");
  6149. verifyFormat("@implementation Foo\n"
  6150. "@end\n"
  6151. "@implementation Bar\n"
  6152. "@end");
  6153. EXPECT_EQ("@implementation Foo : Bar\n"
  6154. "+ (id)init {\n}\n"
  6155. "- (void)foo {\n}\n"
  6156. "@end",
  6157. format("@implementation Foo : Bar\n"
  6158. "+(id)init{}\n"
  6159. "-(void)foo{}\n"
  6160. "@end"));
  6161. verifyFormat("@implementation Foo {\n"
  6162. " int _i;\n"
  6163. "}\n"
  6164. "+ (id)init {\n}\n"
  6165. "@end");
  6166. verifyFormat("@implementation Foo : Bar {\n"
  6167. " int _i;\n"
  6168. "}\n"
  6169. "+ (id)init {\n}\n"
  6170. "@end");
  6171. verifyFormat("@implementation Foo (HackStuff)\n"
  6172. "+ (id)init {\n}\n"
  6173. "@end");
  6174. verifyFormat("@implementation ObjcClass\n"
  6175. "- (void)method;\n"
  6176. "{}\n"
  6177. "@end");
  6178. }
  6179. TEST_F(FormatTest, FormatObjCProtocol) {
  6180. verifyFormat("@protocol Foo\n"
  6181. "@property(weak) id delegate;\n"
  6182. "- (NSUInteger)numberOfThings;\n"
  6183. "@end");
  6184. verifyFormat("@protocol MyProtocol <NSObject>\n"
  6185. "- (NSUInteger)numberOfThings;\n"
  6186. "@end");
  6187. verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
  6188. "- (NSUInteger)numberOfThings;\n"
  6189. "@end");
  6190. verifyFormat("@protocol Foo;\n"
  6191. "@protocol Bar;\n");
  6192. verifyFormat("@protocol Foo\n"
  6193. "@end\n"
  6194. "@protocol Bar\n"
  6195. "@end");
  6196. verifyFormat("@protocol myProtocol\n"
  6197. "- (void)mandatoryWithInt:(int)i;\n"
  6198. "@optional\n"
  6199. "- (void)optional;\n"
  6200. "@required\n"
  6201. "- (void)required;\n"
  6202. "@optional\n"
  6203. "@property(assign) int madProp;\n"
  6204. "@end\n");
  6205. verifyFormat("@property(nonatomic, assign, readonly)\n"
  6206. " int *looooooooooooooooooooooooooooongNumber;\n"
  6207. "@property(nonatomic, assign, readonly)\n"
  6208. " NSString *looooooooooooooooooooooooooooongName;");
  6209. verifyFormat("@implementation PR18406\n"
  6210. "}\n"
  6211. "@end");
  6212. }
  6213. TEST_F(FormatTest, FormatObjCMethodDeclarations) {
  6214. verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
  6215. " rect:(NSRect)theRect\n"
  6216. " interval:(float)theInterval {\n"
  6217. "}");
  6218. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6219. " longKeyword:(NSRect)theRect\n"
  6220. " evenLongerKeyword:(float)theInterval\n"
  6221. " error:(NSError **)theError {\n"
  6222. "}");
  6223. verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
  6224. " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
  6225. " NS_DESIGNATED_INITIALIZER;",
  6226. getLLVMStyleWithColumns(60));
  6227. }
  6228. TEST_F(FormatTest, FormatObjCMethodExpr) {
  6229. verifyFormat("[foo bar:baz];");
  6230. verifyFormat("return [foo bar:baz];");
  6231. verifyFormat("return (a)[foo bar:baz];");
  6232. verifyFormat("f([foo bar:baz]);");
  6233. verifyFormat("f(2, [foo bar:baz]);");
  6234. verifyFormat("f(2, a ? b : c);");
  6235. verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
  6236. // Unary operators.
  6237. verifyFormat("int a = +[foo bar:baz];");
  6238. verifyFormat("int a = -[foo bar:baz];");
  6239. verifyFormat("int a = ![foo bar:baz];");
  6240. verifyFormat("int a = ~[foo bar:baz];");
  6241. verifyFormat("int a = ++[foo bar:baz];");
  6242. verifyFormat("int a = --[foo bar:baz];");
  6243. verifyFormat("int a = sizeof [foo bar:baz];");
  6244. verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
  6245. verifyFormat("int a = &[foo bar:baz];");
  6246. verifyFormat("int a = *[foo bar:baz];");
  6247. // FIXME: Make casts work, without breaking f()[4].
  6248. //verifyFormat("int a = (int)[foo bar:baz];");
  6249. //verifyFormat("return (int)[foo bar:baz];");
  6250. //verifyFormat("(void)[foo bar:baz];");
  6251. verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
  6252. // Binary operators.
  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] : [foo bar:baz];");
  6266. verifyFormat("[foo bar:baz] || [foo bar:baz];");
  6267. verifyFormat("[foo bar:baz] && [foo bar:baz];");
  6268. verifyFormat("[foo bar:baz] | [foo bar:baz];");
  6269. verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
  6270. verifyFormat("[foo bar:baz] & [foo bar:baz];");
  6271. verifyFormat("[foo bar:baz] == [foo bar:baz];");
  6272. verifyFormat("[foo bar:baz] != [foo bar:baz];");
  6273. verifyFormat("[foo bar:baz] >= [foo bar:baz];");
  6274. verifyFormat("[foo bar:baz] <= [foo bar:baz];");
  6275. verifyFormat("[foo bar:baz] > [foo bar:baz];");
  6276. verifyFormat("[foo bar:baz] < [foo bar:baz];");
  6277. verifyFormat("[foo bar:baz] >> [foo bar:baz];");
  6278. verifyFormat("[foo bar:baz] << [foo bar:baz];");
  6279. verifyFormat("[foo bar:baz] - [foo bar:baz];");
  6280. verifyFormat("[foo bar:baz] + [foo bar:baz];");
  6281. verifyFormat("[foo bar:baz] * [foo bar:baz];");
  6282. verifyFormat("[foo bar:baz] / [foo bar:baz];");
  6283. verifyFormat("[foo bar:baz] % [foo bar:baz];");
  6284. // Whew!
  6285. verifyFormat("return in[42];");
  6286. verifyFormat("for (auto v : in[1]) {\n}");
  6287. verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
  6288. "}");
  6289. verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
  6290. verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
  6291. verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
  6292. verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
  6293. verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
  6294. verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
  6295. verifyFormat("[button setAction:@selector(zoomOut:)];");
  6296. verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
  6297. verifyFormat("arr[[self indexForFoo:a]];");
  6298. verifyFormat("throw [self errorFor:a];");
  6299. verifyFormat("@throw [self errorFor:a];");
  6300. verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
  6301. verifyFormat("[(id)foo bar:(id) ? baz : quux];");
  6302. verifyFormat("4 > 4 ? (id)a : (id)baz;");
  6303. // This tests that the formatter doesn't break after "backing" but before ":",
  6304. // which would be at 80 columns.
  6305. verifyFormat(
  6306. "void f() {\n"
  6307. " if ((self = [super initWithContentRect:contentRect\n"
  6308. " styleMask:styleMask ?: otherMask\n"
  6309. " backing:NSBackingStoreBuffered\n"
  6310. " defer:YES]))");
  6311. verifyFormat(
  6312. "[foo checkThatBreakingAfterColonWorksOk:\n"
  6313. " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
  6314. verifyFormat("[myObj short:arg1 // Force line break\n"
  6315. " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
  6316. " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
  6317. " error:arg4];");
  6318. verifyFormat(
  6319. "void f() {\n"
  6320. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  6321. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  6322. " pos.width(), pos.height())\n"
  6323. " styleMask:NSBorderlessWindowMask\n"
  6324. " backing:NSBackingStoreBuffered\n"
  6325. " defer:NO]);\n"
  6326. "}");
  6327. verifyFormat(
  6328. "void f() {\n"
  6329. " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
  6330. " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
  6331. " pos.width(), pos.height())\n"
  6332. " syeMask:NSBorderlessWindowMask\n"
  6333. " bking:NSBackingStoreBuffered\n"
  6334. " der:NO]);\n"
  6335. "}",
  6336. getLLVMStyleWithColumns(70));
  6337. verifyFormat(
  6338. "void f() {\n"
  6339. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  6340. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  6341. " pos.width(), pos.height())\n"
  6342. " styleMask:NSBorderlessWindowMask\n"
  6343. " backing:NSBackingStoreBuffered\n"
  6344. " defer:NO]);\n"
  6345. "}",
  6346. getChromiumStyle(FormatStyle::LK_Cpp));
  6347. verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
  6348. " with:contentsNativeView];");
  6349. verifyFormat(
  6350. "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
  6351. " owner:nillllll];");
  6352. verifyFormat(
  6353. "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
  6354. " forType:kBookmarkButtonDragType];");
  6355. verifyFormat("[defaultCenter addObserver:self\n"
  6356. " selector:@selector(willEnterFullscreen)\n"
  6357. " name:kWillEnterFullscreenNotification\n"
  6358. " object:nil];");
  6359. verifyFormat("[image_rep drawInRect:drawRect\n"
  6360. " fromRect:NSZeroRect\n"
  6361. " operation:NSCompositeCopy\n"
  6362. " fraction:1.0\n"
  6363. " respectFlipped:NO\n"
  6364. " hints:nil];");
  6365. verifyFormat(
  6366. "scoped_nsobject<NSTextField> message(\n"
  6367. " // The frame will be fixed up when |-setMessageText:| is called.\n"
  6368. " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
  6369. verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
  6370. " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
  6371. " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
  6372. " aaaa:bbb];");
  6373. verifyFormat("[self param:function( //\n"
  6374. " parameter)]");
  6375. verifyFormat(
  6376. "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  6377. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  6378. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
  6379. // Variadic parameters.
  6380. verifyFormat(
  6381. "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
  6382. verifyFormat(
  6383. "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  6384. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  6385. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
  6386. verifyFormat("[self // break\n"
  6387. " a:a\n"
  6388. " aaa:aaa];");
  6389. verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
  6390. " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
  6391. }
  6392. TEST_F(FormatTest, ObjCAt) {
  6393. verifyFormat("@autoreleasepool");
  6394. verifyFormat("@catch");
  6395. verifyFormat("@class");
  6396. verifyFormat("@compatibility_alias");
  6397. verifyFormat("@defs");
  6398. verifyFormat("@dynamic");
  6399. verifyFormat("@encode");
  6400. verifyFormat("@end");
  6401. verifyFormat("@finally");
  6402. verifyFormat("@implementation");
  6403. verifyFormat("@import");
  6404. verifyFormat("@interface");
  6405. verifyFormat("@optional");
  6406. verifyFormat("@package");
  6407. verifyFormat("@private");
  6408. verifyFormat("@property");
  6409. verifyFormat("@protected");
  6410. verifyFormat("@protocol");
  6411. verifyFormat("@public");
  6412. verifyFormat("@required");
  6413. verifyFormat("@selector");
  6414. verifyFormat("@synchronized");
  6415. verifyFormat("@synthesize");
  6416. verifyFormat("@throw");
  6417. verifyFormat("@try");
  6418. EXPECT_EQ("@interface", format("@ interface"));
  6419. // The precise formatting of this doesn't matter, nobody writes code like
  6420. // this.
  6421. verifyFormat("@ /*foo*/ interface");
  6422. }
  6423. TEST_F(FormatTest, ObjCSnippets) {
  6424. verifyFormat("@autoreleasepool {\n"
  6425. " foo();\n"
  6426. "}");
  6427. verifyFormat("@class Foo, Bar;");
  6428. verifyFormat("@compatibility_alias AliasName ExistingClass;");
  6429. verifyFormat("@dynamic textColor;");
  6430. verifyFormat("char *buf1 = @encode(int *);");
  6431. verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
  6432. verifyFormat("char *buf1 = @encode(int **);");
  6433. verifyFormat("Protocol *proto = @protocol(p1);");
  6434. verifyFormat("SEL s = @selector(foo:);");
  6435. verifyFormat("@synchronized(self) {\n"
  6436. " f();\n"
  6437. "}");
  6438. verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  6439. verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  6440. verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
  6441. verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
  6442. verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
  6443. verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
  6444. getMozillaStyle());
  6445. verifyFormat("@property BOOL editable;", getMozillaStyle());
  6446. verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
  6447. getWebKitStyle());
  6448. verifyFormat("@property BOOL editable;", getWebKitStyle());
  6449. verifyFormat("@import foo.bar;\n"
  6450. "@import baz;");
  6451. }
  6452. TEST_F(FormatTest, ObjCLiterals) {
  6453. verifyFormat("@\"String\"");
  6454. verifyFormat("@1");
  6455. verifyFormat("@+4.8");
  6456. verifyFormat("@-4");
  6457. verifyFormat("@1LL");
  6458. verifyFormat("@.5");
  6459. verifyFormat("@'c'");
  6460. verifyFormat("@true");
  6461. verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
  6462. verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
  6463. verifyFormat("NSNumber *favoriteColor = @(Green);");
  6464. verifyFormat("NSString *path = @(getenv(\"PATH\"));");
  6465. verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
  6466. }
  6467. TEST_F(FormatTest, ObjCDictLiterals) {
  6468. verifyFormat("@{");
  6469. verifyFormat("@{}");
  6470. verifyFormat("@{@\"one\" : @1}");
  6471. verifyFormat("return @{@\"one\" : @1;");
  6472. verifyFormat("@{@\"one\" : @1}");
  6473. verifyFormat("@{@\"one\" : @{@2 : @1}}");
  6474. verifyFormat("@{\n"
  6475. " @\"one\" : @{@2 : @1},\n"
  6476. "}");
  6477. verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
  6478. verifyFormat("[self setDict:@{}");
  6479. verifyFormat("[self setDict:@{@1 : @2}");
  6480. verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
  6481. verifyFormat(
  6482. "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
  6483. verifyFormat(
  6484. "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
  6485. verifyFormat(
  6486. "NSDictionary *d = @{\n"
  6487. " @\"nam\" : NSUserNam(),\n"
  6488. " @\"dte\" : [NSDate date],\n"
  6489. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  6490. "};");
  6491. verifyFormat(
  6492. "@{\n"
  6493. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  6494. "regularFont,\n"
  6495. "};");
  6496. verifyGoogleFormat(
  6497. "@{\n"
  6498. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  6499. "regularFont,\n"
  6500. "};");
  6501. verifyFormat(
  6502. "@{\n"
  6503. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
  6504. " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
  6505. "};");
  6506. // We should try to be robust in case someone forgets the "@".
  6507. verifyFormat(
  6508. "NSDictionary *d = {\n"
  6509. " @\"nam\" : NSUserNam(),\n"
  6510. " @\"dte\" : [NSDate date],\n"
  6511. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  6512. "};");
  6513. verifyFormat("NSMutableDictionary *dictionary =\n"
  6514. " [NSMutableDictionary dictionaryWithDictionary:@{\n"
  6515. " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
  6516. " bbbbbbbbbbbbbbbbbb : bbbbb,\n"
  6517. " cccccccccccccccc : ccccccccccccccc\n"
  6518. " }];");
  6519. }
  6520. TEST_F(FormatTest, ObjCArrayLiterals) {
  6521. verifyFormat("@[");
  6522. verifyFormat("@[]");
  6523. verifyFormat(
  6524. "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
  6525. verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
  6526. verifyFormat("NSArray *array = @[ [foo description] ];");
  6527. verifyFormat(
  6528. "NSArray *some_variable = @[\n"
  6529. " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
  6530. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6531. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6532. " @\"aaaaaaaaaaaaaaaaa\"\n"
  6533. "];");
  6534. verifyFormat("NSArray *some_variable = @[\n"
  6535. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6536. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6537. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6538. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6539. "];");
  6540. verifyGoogleFormat("NSArray *some_variable = @[\n"
  6541. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6542. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6543. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6544. " @\"aaaaaaaaaaaaaaaaa\"\n"
  6545. "];");
  6546. // We should try to be robust in case someone forgets the "@".
  6547. verifyFormat("NSArray *some_variable = [\n"
  6548. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6549. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6550. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6551. " @\"aaaaaaaaaaaaaaaaa\",\n"
  6552. "];");
  6553. verifyFormat(
  6554. "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
  6555. " index:(NSUInteger)index\n"
  6556. " nonDigitAttributes:\n"
  6557. " (NSDictionary *)noDigitAttributes;");
  6558. verifyFormat(
  6559. "[someFunction someLooooooooooooongParameter:\n"
  6560. " @[ NSBundle.mainBundle.infoDictionary[@\"a\"] ]];");
  6561. }
  6562. TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
  6563. EXPECT_EQ("{\n"
  6564. "{\n"
  6565. "a;\n"
  6566. "b;\n"
  6567. "}\n"
  6568. "}",
  6569. format("{\n"
  6570. "{\n"
  6571. "a;\n"
  6572. " b;\n"
  6573. "}\n"
  6574. "}",
  6575. 13, 2, getLLVMStyle()));
  6576. EXPECT_EQ("{\n"
  6577. "{\n"
  6578. " a;\n"
  6579. "b;\n"
  6580. "}\n"
  6581. "}",
  6582. format("{\n"
  6583. "{\n"
  6584. " a;\n"
  6585. "b;\n"
  6586. "}\n"
  6587. "}",
  6588. 9, 2, getLLVMStyle()));
  6589. EXPECT_EQ("{\n"
  6590. "{\n"
  6591. "public:\n"
  6592. " b;\n"
  6593. "}\n"
  6594. "}",
  6595. format("{\n"
  6596. "{\n"
  6597. "public:\n"
  6598. " b;\n"
  6599. "}\n"
  6600. "}",
  6601. 17, 2, getLLVMStyle()));
  6602. EXPECT_EQ("{\n"
  6603. "{\n"
  6604. "a;\n"
  6605. "}\n"
  6606. "{\n"
  6607. " b; //\n"
  6608. "}\n"
  6609. "}",
  6610. format("{\n"
  6611. "{\n"
  6612. "a;\n"
  6613. "}\n"
  6614. "{\n"
  6615. " b; //\n"
  6616. "}\n"
  6617. "}",
  6618. 22, 2, getLLVMStyle()));
  6619. EXPECT_EQ(" {\n"
  6620. " a; //\n"
  6621. " }",
  6622. format(" {\n"
  6623. "a; //\n"
  6624. " }",
  6625. 4, 2, getLLVMStyle()));
  6626. EXPECT_EQ("void f() {}\n"
  6627. "void g() {}",
  6628. format("void f() {}\n"
  6629. "void g() {}",
  6630. 13, 0, getLLVMStyle()));
  6631. EXPECT_EQ("int a; // comment\n"
  6632. " // line 2\n"
  6633. "int b;",
  6634. format("int a; // comment\n"
  6635. " // line 2\n"
  6636. " int b;",
  6637. 35, 0, getLLVMStyle()));
  6638. EXPECT_EQ(" int a;\n"
  6639. " void\n"
  6640. " ffffff() {\n"
  6641. " }",
  6642. format(" int a;\n"
  6643. "void ffffff() {}",
  6644. 11, 0, getLLVMStyleWithColumns(11)));
  6645. EXPECT_EQ(" void f() {\n"
  6646. "#define A 1\n"
  6647. " }",
  6648. format(" void f() {\n"
  6649. " #define A 1\n" // Format this line.
  6650. " }",
  6651. 20, 0, getLLVMStyle()));
  6652. EXPECT_EQ(" void f() {\n"
  6653. " int i;\n"
  6654. "#define A \\\n"
  6655. " int i; \\\n"
  6656. " int j;\n"
  6657. " int k;\n"
  6658. " }",
  6659. format(" void f() {\n"
  6660. " int i;\n"
  6661. "#define A \\\n"
  6662. " int i; \\\n"
  6663. " int j;\n"
  6664. " int k;\n" // Format this line.
  6665. " }",
  6666. 67, 0, getLLVMStyle()));
  6667. }
  6668. TEST_F(FormatTest, BreaksStringLiterals) {
  6669. EXPECT_EQ("\"some text \"\n"
  6670. "\"other\";",
  6671. format("\"some text other\";", getLLVMStyleWithColumns(12)));
  6672. EXPECT_EQ("\"some text \"\n"
  6673. "\"other\";",
  6674. format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
  6675. EXPECT_EQ(
  6676. "#define A \\\n"
  6677. " \"some \" \\\n"
  6678. " \"text \" \\\n"
  6679. " \"other\";",
  6680. format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
  6681. EXPECT_EQ(
  6682. "#define A \\\n"
  6683. " \"so \" \\\n"
  6684. " \"text \" \\\n"
  6685. " \"other\";",
  6686. format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
  6687. EXPECT_EQ("\"some text\"",
  6688. format("\"some text\"", getLLVMStyleWithColumns(1)));
  6689. EXPECT_EQ("\"some text\"",
  6690. format("\"some text\"", getLLVMStyleWithColumns(11)));
  6691. EXPECT_EQ("\"some \"\n"
  6692. "\"text\"",
  6693. format("\"some text\"", getLLVMStyleWithColumns(10)));
  6694. EXPECT_EQ("\"some \"\n"
  6695. "\"text\"",
  6696. format("\"some text\"", getLLVMStyleWithColumns(7)));
  6697. EXPECT_EQ("\"some\"\n"
  6698. "\" tex\"\n"
  6699. "\"t\"",
  6700. format("\"some text\"", getLLVMStyleWithColumns(6)));
  6701. EXPECT_EQ("\"some\"\n"
  6702. "\" tex\"\n"
  6703. "\" and\"",
  6704. format("\"some tex and\"", getLLVMStyleWithColumns(6)));
  6705. EXPECT_EQ("\"some\"\n"
  6706. "\"/tex\"\n"
  6707. "\"/and\"",
  6708. format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
  6709. EXPECT_EQ("variable =\n"
  6710. " \"long string \"\n"
  6711. " \"literal\";",
  6712. format("variable = \"long string literal\";",
  6713. getLLVMStyleWithColumns(20)));
  6714. EXPECT_EQ("variable = f(\n"
  6715. " \"long string \"\n"
  6716. " \"literal\",\n"
  6717. " short,\n"
  6718. " loooooooooooooooooooong);",
  6719. format("variable = f(\"long string literal\", short, "
  6720. "loooooooooooooooooooong);",
  6721. getLLVMStyleWithColumns(20)));
  6722. EXPECT_EQ("f(g(\"long string \"\n"
  6723. " \"literal\"),\n"
  6724. " b);",
  6725. format("f(g(\"long string literal\"), b);",
  6726. getLLVMStyleWithColumns(20)));
  6727. EXPECT_EQ("f(g(\"long string \"\n"
  6728. " \"literal\",\n"
  6729. " a),\n"
  6730. " b);",
  6731. format("f(g(\"long string literal\", a), b);",
  6732. getLLVMStyleWithColumns(20)));
  6733. EXPECT_EQ(
  6734. "f(\"one two\".split(\n"
  6735. " variable));",
  6736. format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
  6737. EXPECT_EQ("f(\"one two three four five six \"\n"
  6738. " \"seven\".split(\n"
  6739. " really_looooong_variable));",
  6740. format("f(\"one two three four five six seven\"."
  6741. "split(really_looooong_variable));",
  6742. getLLVMStyleWithColumns(33)));
  6743. EXPECT_EQ("f(\"some \"\n"
  6744. " \"text\",\n"
  6745. " other);",
  6746. format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
  6747. // Only break as a last resort.
  6748. verifyFormat(
  6749. "aaaaaaaaaaaaaaaaaaaa(\n"
  6750. " aaaaaaaaaaaaaaaaaaaa,\n"
  6751. " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
  6752. EXPECT_EQ(
  6753. "\"splitmea\"\n"
  6754. "\"trandomp\"\n"
  6755. "\"oint\"",
  6756. format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
  6757. EXPECT_EQ(
  6758. "\"split/\"\n"
  6759. "\"pathat/\"\n"
  6760. "\"slashes\"",
  6761. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  6762. EXPECT_EQ(
  6763. "\"split/\"\n"
  6764. "\"pathat/\"\n"
  6765. "\"slashes\"",
  6766. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  6767. EXPECT_EQ("\"split at \"\n"
  6768. "\"spaces/at/\"\n"
  6769. "\"slashes.at.any$\"\n"
  6770. "\"non-alphanumeric%\"\n"
  6771. "\"1111111111characte\"\n"
  6772. "\"rs\"",
  6773. format("\"split at "
  6774. "spaces/at/"
  6775. "slashes.at."
  6776. "any$non-"
  6777. "alphanumeric%"
  6778. "1111111111characte"
  6779. "rs\"",
  6780. getLLVMStyleWithColumns(20)));
  6781. // Verify that splitting the strings understands
  6782. // Style::AlwaysBreakBeforeMultilineStrings.
  6783. EXPECT_EQ("aaaaaaaaaaaa(aaaaaaaaaaaaa,\n"
  6784. " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
  6785. " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
  6786. format("aaaaaaaaaaaa(aaaaaaaaaaaaa, \"aaaaaaaaaaaaaaaaaaaaaa "
  6787. "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa "
  6788. "aaaaaaaaaaaaaaaaaaaaaa\");",
  6789. getGoogleStyle()));
  6790. EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  6791. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
  6792. format("return \"aaaaaaaaaaaaaaaaaaaaaa "
  6793. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
  6794. "aaaaaaaaaaaaaaaaaaaaaa\";",
  6795. getGoogleStyle()));
  6796. EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  6797. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  6798. format("llvm::outs() << "
  6799. "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
  6800. "aaaaaaaaaaaaaaaaaaa\";"));
  6801. EXPECT_EQ("ffff(\n"
  6802. " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  6803. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  6804. format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  6805. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  6806. getGoogleStyle()));
  6807. FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
  6808. AlignLeft.AlignEscapedNewlinesLeft = true;
  6809. EXPECT_EQ(
  6810. "#define A \\\n"
  6811. " \"some \" \\\n"
  6812. " \"text \" \\\n"
  6813. " \"other\";",
  6814. format("#define A \"some text other\";", AlignLeft));
  6815. }
  6816. TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
  6817. EXPECT_EQ(
  6818. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  6819. "(\n"
  6820. " \"x\t\");",
  6821. format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  6822. "aaaaaaa("
  6823. "\"x\t\");"));
  6824. }
  6825. TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
  6826. EXPECT_EQ(
  6827. "u8\"utf8 string \"\n"
  6828. "u8\"literal\";",
  6829. format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
  6830. EXPECT_EQ(
  6831. "u\"utf16 string \"\n"
  6832. "u\"literal\";",
  6833. format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
  6834. EXPECT_EQ(
  6835. "U\"utf32 string \"\n"
  6836. "U\"literal\";",
  6837. format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
  6838. EXPECT_EQ("L\"wide string \"\n"
  6839. "L\"literal\";",
  6840. format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
  6841. EXPECT_EQ("@\"NSString \"\n"
  6842. "@\"literal\";",
  6843. format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
  6844. }
  6845. TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
  6846. FormatStyle Style = getGoogleStyleWithColumns(15);
  6847. EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
  6848. EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
  6849. EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
  6850. EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
  6851. EXPECT_EQ("u8R\"x(raw literal)x\";",
  6852. format("u8R\"x(raw literal)x\";", Style));
  6853. }
  6854. TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
  6855. FormatStyle Style = getLLVMStyleWithColumns(20);
  6856. EXPECT_EQ(
  6857. "_T(\"aaaaaaaaaaaaaa\")\n"
  6858. "_T(\"aaaaaaaaaaaaaa\")\n"
  6859. "_T(\"aaaaaaaaaaaa\")",
  6860. format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
  6861. EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
  6862. " _T(\"aaaaaa\"),\n"
  6863. " z);",
  6864. format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
  6865. // FIXME: Handle embedded spaces in one iteration.
  6866. // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
  6867. // "_T(\"aaaaaaaaaaaaa\")\n"
  6868. // "_T(\"aaaaaaaaaaaaa\")\n"
  6869. // "_T(\"a\")",
  6870. // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  6871. // getLLVMStyleWithColumns(20)));
  6872. EXPECT_EQ(
  6873. "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  6874. format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
  6875. }
  6876. TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
  6877. EXPECT_EQ(
  6878. "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  6879. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  6880. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  6881. format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  6882. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  6883. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
  6884. }
  6885. TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
  6886. EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
  6887. format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle()));
  6888. EXPECT_EQ("fffffffffff(g(R\"x(\n"
  6889. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6890. ")x\",\n"
  6891. " a),\n"
  6892. " b);",
  6893. format("fffffffffff(g(R\"x(\n"
  6894. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6895. ")x\", a), b);",
  6896. getGoogleStyleWithColumns(20)));
  6897. EXPECT_EQ("fffffffffff(\n"
  6898. " g(R\"x(qqq\n"
  6899. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6900. ")x\",\n"
  6901. " a),\n"
  6902. " b);",
  6903. format("fffffffffff(g(R\"x(qqq\n"
  6904. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6905. ")x\", a), b);",
  6906. getGoogleStyleWithColumns(20)));
  6907. EXPECT_EQ("fffffffffff(R\"x(\n"
  6908. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6909. ")x\");",
  6910. format("fffffffffff(R\"x(\n"
  6911. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6912. ")x\");",
  6913. getGoogleStyleWithColumns(20)));
  6914. EXPECT_EQ("fffffffffff(R\"x(\n"
  6915. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6916. ")x\" + bbbbbb);",
  6917. format("fffffffffff(R\"x(\n"
  6918. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6919. ")x\" + bbbbbb);",
  6920. getGoogleStyleWithColumns(20)));
  6921. EXPECT_EQ("fffffffffff(\n"
  6922. " R\"x(\n"
  6923. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6924. ")x\" +\n"
  6925. " bbbbbb);",
  6926. format("fffffffffff(\n"
  6927. " R\"x(\n"
  6928. "multiline raw string literal xxxxxxxxxxxxxx\n"
  6929. ")x\" + bbbbbb);",
  6930. getGoogleStyleWithColumns(20)));
  6931. }
  6932. TEST_F(FormatTest, SkipsUnknownStringLiterals) {
  6933. verifyFormat("string a = \"unterminated;");
  6934. EXPECT_EQ("function(\"unterminated,\n"
  6935. " OtherParameter);",
  6936. format("function( \"unterminated,\n"
  6937. " OtherParameter);"));
  6938. }
  6939. TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
  6940. FormatStyle Style = getLLVMStyle();
  6941. Style.Standard = FormatStyle::LS_Cpp03;
  6942. EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
  6943. format("#define x(_a) printf(\"foo\"_a);", Style));
  6944. }
  6945. TEST_F(FormatTest, UnderstandsCpp1y) {
  6946. verifyFormat("int bi{1'000'000};");
  6947. }
  6948. TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
  6949. EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
  6950. " \"ddeeefff\");",
  6951. format("someFunction(\"aaabbbcccdddeeefff\");",
  6952. getLLVMStyleWithColumns(25)));
  6953. EXPECT_EQ("someFunction1234567890(\n"
  6954. " \"aaabbbcccdddeeefff\");",
  6955. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  6956. getLLVMStyleWithColumns(26)));
  6957. EXPECT_EQ("someFunction1234567890(\n"
  6958. " \"aaabbbcccdddeeeff\"\n"
  6959. " \"f\");",
  6960. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  6961. getLLVMStyleWithColumns(25)));
  6962. EXPECT_EQ("someFunction1234567890(\n"
  6963. " \"aaabbbcccdddeeeff\"\n"
  6964. " \"f\");",
  6965. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  6966. getLLVMStyleWithColumns(24)));
  6967. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  6968. " \"ddde \"\n"
  6969. " \"efff\");",
  6970. format("someFunction(\"aaabbbcc ddde efff\");",
  6971. getLLVMStyleWithColumns(25)));
  6972. EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
  6973. " \"ddeeefff\");",
  6974. format("someFunction(\"aaabbbccc ddeeefff\");",
  6975. getLLVMStyleWithColumns(25)));
  6976. EXPECT_EQ("someFunction1234567890(\n"
  6977. " \"aaabb \"\n"
  6978. " \"cccdddeeefff\");",
  6979. format("someFunction1234567890(\"aaabb cccdddeeefff\");",
  6980. getLLVMStyleWithColumns(25)));
  6981. EXPECT_EQ("#define A \\\n"
  6982. " string s = \\\n"
  6983. " \"123456789\" \\\n"
  6984. " \"0\"; \\\n"
  6985. " int i;",
  6986. format("#define A string s = \"1234567890\"; int i;",
  6987. getLLVMStyleWithColumns(20)));
  6988. // FIXME: Put additional penalties on breaking at non-whitespace locations.
  6989. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  6990. " \"dddeeeff\"\n"
  6991. " \"f\");",
  6992. format("someFunction(\"aaabbbcc dddeeefff\");",
  6993. getLLVMStyleWithColumns(25)));
  6994. }
  6995. TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
  6996. EXPECT_EQ("\"\\a\"",
  6997. format("\"\\a\"", getLLVMStyleWithColumns(3)));
  6998. EXPECT_EQ("\"\\\"",
  6999. format("\"\\\"", getLLVMStyleWithColumns(2)));
  7000. EXPECT_EQ("\"test\"\n"
  7001. "\"\\n\"",
  7002. format("\"test\\n\"", getLLVMStyleWithColumns(7)));
  7003. EXPECT_EQ("\"tes\\\\\"\n"
  7004. "\"n\"",
  7005. format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
  7006. EXPECT_EQ("\"\\\\\\\\\"\n"
  7007. "\"\\n\"",
  7008. format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
  7009. EXPECT_EQ("\"\\uff01\"",
  7010. format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
  7011. EXPECT_EQ("\"\\uff01\"\n"
  7012. "\"test\"",
  7013. format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
  7014. EXPECT_EQ("\"\\Uff01ff02\"",
  7015. format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
  7016. EXPECT_EQ("\"\\x000000000001\"\n"
  7017. "\"next\"",
  7018. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
  7019. EXPECT_EQ("\"\\x000000000001next\"",
  7020. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
  7021. EXPECT_EQ("\"\\x000000000001\"",
  7022. format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
  7023. EXPECT_EQ("\"test\"\n"
  7024. "\"\\000000\"\n"
  7025. "\"000001\"",
  7026. format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
  7027. EXPECT_EQ("\"test\\000\"\n"
  7028. "\"00000000\"\n"
  7029. "\"1\"",
  7030. format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
  7031. }
  7032. TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
  7033. verifyFormat("void f() {\n"
  7034. " return g() {}\n"
  7035. " void h() {}");
  7036. verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
  7037. "g();\n"
  7038. "}");
  7039. }
  7040. TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
  7041. verifyFormat(
  7042. "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
  7043. }
  7044. TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
  7045. verifyFormat("class X {\n"
  7046. " void f() {\n"
  7047. " }\n"
  7048. "};",
  7049. getLLVMStyleWithColumns(12));
  7050. }
  7051. TEST_F(FormatTest, ConfigurableIndentWidth) {
  7052. FormatStyle EightIndent = getLLVMStyleWithColumns(18);
  7053. EightIndent.IndentWidth = 8;
  7054. EightIndent.ContinuationIndentWidth = 8;
  7055. verifyFormat("void f() {\n"
  7056. " someFunction();\n"
  7057. " if (true) {\n"
  7058. " f();\n"
  7059. " }\n"
  7060. "}",
  7061. EightIndent);
  7062. verifyFormat("class X {\n"
  7063. " void f() {\n"
  7064. " }\n"
  7065. "};",
  7066. EightIndent);
  7067. verifyFormat("int x[] = {\n"
  7068. " call(),\n"
  7069. " call()};",
  7070. EightIndent);
  7071. }
  7072. TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
  7073. verifyFormat("double\n"
  7074. "f();",
  7075. getLLVMStyleWithColumns(8));
  7076. }
  7077. TEST_F(FormatTest, ConfigurableUseOfTab) {
  7078. FormatStyle Tab = getLLVMStyleWithColumns(42);
  7079. Tab.IndentWidth = 8;
  7080. Tab.UseTab = FormatStyle::UT_Always;
  7081. Tab.AlignEscapedNewlinesLeft = true;
  7082. EXPECT_EQ("if (aaaaaaaa && // q\n"
  7083. " bb)\t\t// w\n"
  7084. "\t;",
  7085. format("if (aaaaaaaa &&// q\n"
  7086. "bb)// w\n"
  7087. ";",
  7088. Tab));
  7089. EXPECT_EQ("if (aaa && bbb) // w\n"
  7090. "\t;",
  7091. format("if(aaa&&bbb)// w\n"
  7092. ";",
  7093. Tab));
  7094. verifyFormat("class X {\n"
  7095. "\tvoid f() {\n"
  7096. "\t\tsomeFunction(parameter1,\n"
  7097. "\t\t\t parameter2);\n"
  7098. "\t}\n"
  7099. "};",
  7100. Tab);
  7101. verifyFormat("#define A \\\n"
  7102. "\tvoid f() { \\\n"
  7103. "\t\tsomeFunction( \\\n"
  7104. "\t\t parameter1, \\\n"
  7105. "\t\t parameter2); \\\n"
  7106. "\t}",
  7107. Tab);
  7108. EXPECT_EQ("void f() {\n"
  7109. "\tf();\n"
  7110. "\tg();\n"
  7111. "}",
  7112. format("void f() {\n"
  7113. "\tf();\n"
  7114. "\tg();\n"
  7115. "}",
  7116. 0, 0, Tab));
  7117. EXPECT_EQ("void f() {\n"
  7118. "\tf();\n"
  7119. "\tg();\n"
  7120. "}",
  7121. format("void f() {\n"
  7122. "\tf();\n"
  7123. "\tg();\n"
  7124. "}",
  7125. 16, 0, Tab));
  7126. EXPECT_EQ("void f() {\n"
  7127. " \tf();\n"
  7128. "\tg();\n"
  7129. "}",
  7130. format("void f() {\n"
  7131. " \tf();\n"
  7132. " \tg();\n"
  7133. "}",
  7134. 21, 0, Tab));
  7135. Tab.TabWidth = 4;
  7136. Tab.IndentWidth = 8;
  7137. verifyFormat("class TabWidth4Indent8 {\n"
  7138. "\t\tvoid f() {\n"
  7139. "\t\t\t\tsomeFunction(parameter1,\n"
  7140. "\t\t\t\t\t\t\t parameter2);\n"
  7141. "\t\t}\n"
  7142. "};",
  7143. Tab);
  7144. Tab.TabWidth = 4;
  7145. Tab.IndentWidth = 4;
  7146. verifyFormat("class TabWidth4Indent4 {\n"
  7147. "\tvoid f() {\n"
  7148. "\t\tsomeFunction(parameter1,\n"
  7149. "\t\t\t\t\t parameter2);\n"
  7150. "\t}\n"
  7151. "};",
  7152. Tab);
  7153. Tab.TabWidth = 8;
  7154. Tab.IndentWidth = 4;
  7155. verifyFormat("class TabWidth8Indent4 {\n"
  7156. " void f() {\n"
  7157. "\tsomeFunction(parameter1,\n"
  7158. "\t\t parameter2);\n"
  7159. " }\n"
  7160. "};",
  7161. Tab);
  7162. Tab.TabWidth = 8;
  7163. Tab.IndentWidth = 8;
  7164. EXPECT_EQ("/*\n"
  7165. "\t a\t\tcomment\n"
  7166. "\t in multiple lines\n"
  7167. " */",
  7168. format(" /*\t \t \n"
  7169. " \t \t a\t\tcomment\t \t\n"
  7170. " \t \t in multiple lines\t\n"
  7171. " \t */",
  7172. Tab));
  7173. Tab.UseTab = FormatStyle::UT_ForIndentation;
  7174. verifyFormat("{\n"
  7175. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7176. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7177. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7178. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7179. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7180. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7181. "};",
  7182. Tab);
  7183. verifyFormat("enum A {\n"
  7184. "\ta1, // Force multiple lines\n"
  7185. "\ta2,\n"
  7186. "\ta3\n"
  7187. "};",
  7188. Tab);
  7189. EXPECT_EQ("if (aaaaaaaa && // q\n"
  7190. " bb) // w\n"
  7191. "\t;",
  7192. format("if (aaaaaaaa &&// q\n"
  7193. "bb)// w\n"
  7194. ";",
  7195. Tab));
  7196. verifyFormat("class X {\n"
  7197. "\tvoid f() {\n"
  7198. "\t\tsomeFunction(parameter1,\n"
  7199. "\t\t parameter2);\n"
  7200. "\t}\n"
  7201. "};",
  7202. Tab);
  7203. verifyFormat("{\n"
  7204. "\tQ({\n"
  7205. "\t\tint a;\n"
  7206. "\t\tsomeFunction(aaaaaaaa,\n"
  7207. "\t\t bbbbbbb);\n"
  7208. "\t}, p);\n"
  7209. "}",
  7210. Tab);
  7211. EXPECT_EQ("{\n"
  7212. "\t/* aaaa\n"
  7213. "\t bbbb */\n"
  7214. "}",
  7215. format("{\n"
  7216. "/* aaaa\n"
  7217. " bbbb */\n"
  7218. "}",
  7219. Tab));
  7220. EXPECT_EQ("{\n"
  7221. "\t/*\n"
  7222. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7223. "\t bbbbbbbbbbbbb\n"
  7224. "\t*/\n"
  7225. "}",
  7226. format("{\n"
  7227. "/*\n"
  7228. " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7229. "*/\n"
  7230. "}",
  7231. Tab));
  7232. EXPECT_EQ("{\n"
  7233. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7234. "\t// bbbbbbbbbbbbb\n"
  7235. "}",
  7236. format("{\n"
  7237. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7238. "}",
  7239. Tab));
  7240. EXPECT_EQ("{\n"
  7241. "\t/*\n"
  7242. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7243. "\t bbbbbbbbbbbbb\n"
  7244. "\t*/\n"
  7245. "}",
  7246. format("{\n"
  7247. "\t/*\n"
  7248. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7249. "\t*/\n"
  7250. "}",
  7251. Tab));
  7252. EXPECT_EQ("{\n"
  7253. "\t/*\n"
  7254. "\n"
  7255. "\t*/\n"
  7256. "}",
  7257. format("{\n"
  7258. "\t/*\n"
  7259. "\n"
  7260. "\t*/\n"
  7261. "}",
  7262. Tab));
  7263. EXPECT_EQ("{\n"
  7264. "\t/*\n"
  7265. " asdf\n"
  7266. "\t*/\n"
  7267. "}",
  7268. format("{\n"
  7269. "\t/*\n"
  7270. " asdf\n"
  7271. "\t*/\n"
  7272. "}",
  7273. Tab));
  7274. Tab.UseTab = FormatStyle::UT_Never;
  7275. EXPECT_EQ("/*\n"
  7276. " a\t\tcomment\n"
  7277. " in multiple lines\n"
  7278. " */",
  7279. format(" /*\t \t \n"
  7280. " \t \t a\t\tcomment\t \t\n"
  7281. " \t \t in multiple lines\t\n"
  7282. " \t */",
  7283. Tab));
  7284. EXPECT_EQ("/* some\n"
  7285. " comment */",
  7286. format(" \t \t /* some\n"
  7287. " \t \t comment */",
  7288. Tab));
  7289. EXPECT_EQ("int a; /* some\n"
  7290. " comment */",
  7291. format(" \t \t int a; /* some\n"
  7292. " \t \t comment */",
  7293. Tab));
  7294. EXPECT_EQ("int a; /* some\n"
  7295. "comment */",
  7296. format(" \t \t int\ta; /* some\n"
  7297. " \t \t comment */",
  7298. Tab));
  7299. EXPECT_EQ("f(\"\t\t\"); /* some\n"
  7300. " comment */",
  7301. format(" \t \t f(\"\t\t\"); /* some\n"
  7302. " \t \t comment */",
  7303. Tab));
  7304. EXPECT_EQ("{\n"
  7305. " /*\n"
  7306. " * Comment\n"
  7307. " */\n"
  7308. " int i;\n"
  7309. "}",
  7310. format("{\n"
  7311. "\t/*\n"
  7312. "\t * Comment\n"
  7313. "\t */\n"
  7314. "\t int i;\n"
  7315. "}"));
  7316. }
  7317. TEST_F(FormatTest, CalculatesOriginalColumn) {
  7318. EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7319. "q\"; /* some\n"
  7320. " comment */",
  7321. format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7322. "q\"; /* some\n"
  7323. " comment */",
  7324. getLLVMStyle()));
  7325. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  7326. "/* some\n"
  7327. " comment */",
  7328. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  7329. " /* some\n"
  7330. " comment */",
  7331. getLLVMStyle()));
  7332. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7333. "qqq\n"
  7334. "/* some\n"
  7335. " comment */",
  7336. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7337. "qqq\n"
  7338. " /* some\n"
  7339. " comment */",
  7340. getLLVMStyle()));
  7341. EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7342. "wwww; /* some\n"
  7343. " comment */",
  7344. format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7345. "wwww; /* some\n"
  7346. " comment */",
  7347. getLLVMStyle()));
  7348. }
  7349. TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
  7350. FormatStyle NoSpace = getLLVMStyle();
  7351. NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
  7352. verifyFormat("while(true)\n"
  7353. " continue;", NoSpace);
  7354. verifyFormat("for(;;)\n"
  7355. " continue;", NoSpace);
  7356. verifyFormat("if(true)\n"
  7357. " f();\n"
  7358. "else if(true)\n"
  7359. " f();", NoSpace);
  7360. verifyFormat("do {\n"
  7361. " do_something();\n"
  7362. "} while(something());", NoSpace);
  7363. verifyFormat("switch(x) {\n"
  7364. "default:\n"
  7365. " break;\n"
  7366. "}", NoSpace);
  7367. verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
  7368. verifyFormat("size_t x = sizeof(x);", NoSpace);
  7369. verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
  7370. verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
  7371. verifyFormat("alignas(128) char a[128];", NoSpace);
  7372. verifyFormat("size_t x = alignof(MyType);", NoSpace);
  7373. verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
  7374. verifyFormat("int f() throw(Deprecated);", NoSpace);
  7375. FormatStyle Space = getLLVMStyle();
  7376. Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
  7377. verifyFormat("int f ();", Space);
  7378. verifyFormat("void f (int a, T b) {\n"
  7379. " while (true)\n"
  7380. " continue;\n"
  7381. "}",
  7382. Space);
  7383. verifyFormat("if (true)\n"
  7384. " f ();\n"
  7385. "else if (true)\n"
  7386. " f ();",
  7387. Space);
  7388. verifyFormat("do {\n"
  7389. " do_something ();\n"
  7390. "} while (something ());",
  7391. Space);
  7392. verifyFormat("switch (x) {\n"
  7393. "default:\n"
  7394. " break;\n"
  7395. "}",
  7396. Space);
  7397. verifyFormat("A::A () : a (1) {}", Space);
  7398. verifyFormat("void f () __attribute__ ((asdf));", Space);
  7399. verifyFormat("*(&a + 1);\n"
  7400. "&((&a)[1]);\n"
  7401. "a[(b + c) * d];\n"
  7402. "(((a + 1) * 2) + 3) * 4;",
  7403. Space);
  7404. verifyFormat("#define A(x) x", Space);
  7405. verifyFormat("#define A (x) x", Space);
  7406. verifyFormat("#if defined(x)\n"
  7407. "#endif",
  7408. Space);
  7409. verifyFormat("auto i = std::make_unique<int> (5);", Space);
  7410. verifyFormat("size_t x = sizeof (x);", Space);
  7411. verifyFormat("auto f (int x) -> decltype (x);", Space);
  7412. verifyFormat("int f (T x) noexcept (x.create ());", Space);
  7413. verifyFormat("alignas (128) char a[128];", Space);
  7414. verifyFormat("size_t x = alignof (MyType);", Space);
  7415. verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
  7416. verifyFormat("int f () throw (Deprecated);", Space);
  7417. }
  7418. TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
  7419. FormatStyle Spaces = getLLVMStyle();
  7420. Spaces.SpacesInParentheses = true;
  7421. verifyFormat("call( x, y, z );", Spaces);
  7422. verifyFormat("while ( (bool)1 )\n"
  7423. " continue;", Spaces);
  7424. verifyFormat("for ( ;; )\n"
  7425. " continue;", Spaces);
  7426. verifyFormat("if ( true )\n"
  7427. " f();\n"
  7428. "else if ( true )\n"
  7429. " f();", Spaces);
  7430. verifyFormat("do {\n"
  7431. " do_something( (int)i );\n"
  7432. "} while ( something() );", Spaces);
  7433. verifyFormat("switch ( x ) {\n"
  7434. "default:\n"
  7435. " break;\n"
  7436. "}", Spaces);
  7437. Spaces.SpacesInParentheses = false;
  7438. Spaces.SpacesInCStyleCastParentheses = true;
  7439. verifyFormat("Type *A = ( Type * )P;", Spaces);
  7440. verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
  7441. verifyFormat("x = ( int32 )y;", Spaces);
  7442. verifyFormat("int a = ( int )(2.0f);", Spaces);
  7443. verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
  7444. verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
  7445. verifyFormat("#define x (( int )-1)", Spaces);
  7446. Spaces.SpacesInParentheses = false;
  7447. Spaces.SpaceInEmptyParentheses = true;
  7448. verifyFormat("call(x, y, z);", Spaces);
  7449. verifyFormat("call( )", Spaces);
  7450. // Run the first set of tests again with
  7451. // Spaces.SpacesInParentheses = false,
  7452. // Spaces.SpaceInEmptyParentheses = true and
  7453. // Spaces.SpacesInCStyleCastParentheses = true
  7454. Spaces.SpacesInParentheses = false,
  7455. Spaces.SpaceInEmptyParentheses = true;
  7456. Spaces.SpacesInCStyleCastParentheses = true;
  7457. verifyFormat("call(x, y, z);", Spaces);
  7458. verifyFormat("while (( bool )1)\n"
  7459. " continue;", Spaces);
  7460. verifyFormat("for (;;)\n"
  7461. " continue;", Spaces);
  7462. verifyFormat("if (true)\n"
  7463. " f( );\n"
  7464. "else if (true)\n"
  7465. " f( );", Spaces);
  7466. verifyFormat("do {\n"
  7467. " do_something(( int )i);\n"
  7468. "} while (something( ));", Spaces);
  7469. verifyFormat("switch (x) {\n"
  7470. "default:\n"
  7471. " break;\n"
  7472. "}", Spaces);
  7473. Spaces.SpaceAfterCStyleCast = true;
  7474. verifyFormat("call(x, y, z);", Spaces);
  7475. verifyFormat("while (( bool ) 1)\n"
  7476. " continue;",
  7477. Spaces);
  7478. verifyFormat("for (;;)\n"
  7479. " continue;",
  7480. Spaces);
  7481. verifyFormat("if (true)\n"
  7482. " f( );\n"
  7483. "else if (true)\n"
  7484. " f( );",
  7485. Spaces);
  7486. verifyFormat("do {\n"
  7487. " do_something(( int ) i);\n"
  7488. "} while (something( ));",
  7489. Spaces);
  7490. verifyFormat("switch (x) {\n"
  7491. "default:\n"
  7492. " break;\n"
  7493. "}",
  7494. Spaces);
  7495. Spaces.SpacesInCStyleCastParentheses = false;
  7496. Spaces.SpaceAfterCStyleCast = true;
  7497. verifyFormat("while ((bool) 1)\n"
  7498. " continue;",
  7499. Spaces);
  7500. verifyFormat("do {\n"
  7501. " do_something((int) i);\n"
  7502. "} while (something( ));",
  7503. Spaces);
  7504. }
  7505. TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
  7506. verifyFormat("int a[5];");
  7507. verifyFormat("a[3] += 42;");
  7508. FormatStyle Spaces = getLLVMStyle();
  7509. Spaces.SpacesInSquareBrackets = true;
  7510. // Lambdas unchanged.
  7511. verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
  7512. verifyFormat("return [i, args...] {};", Spaces);
  7513. // Not lambdas.
  7514. verifyFormat("int a[ 5 ];", Spaces);
  7515. verifyFormat("a[ 3 ] += 42;", Spaces);
  7516. verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
  7517. verifyFormat("double &operator[](int i) { return 0; }\n"
  7518. "int i;",
  7519. Spaces);
  7520. verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
  7521. verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
  7522. verifyFormat("int i = (*b)[ a ]->f();", Spaces);
  7523. }
  7524. TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
  7525. verifyFormat("int a = 5;");
  7526. verifyFormat("a += 42;");
  7527. verifyFormat("a or_eq 8;");
  7528. FormatStyle Spaces = getLLVMStyle();
  7529. Spaces.SpaceBeforeAssignmentOperators = false;
  7530. verifyFormat("int a= 5;", Spaces);
  7531. verifyFormat("a+= 42;", Spaces);
  7532. verifyFormat("a or_eq 8;", Spaces);
  7533. }
  7534. TEST_F(FormatTest, LinuxBraceBreaking) {
  7535. FormatStyle LinuxBraceStyle = getLLVMStyle();
  7536. LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
  7537. verifyFormat("namespace a\n"
  7538. "{\n"
  7539. "class A\n"
  7540. "{\n"
  7541. " void f()\n"
  7542. " {\n"
  7543. " if (true) {\n"
  7544. " a();\n"
  7545. " b();\n"
  7546. " }\n"
  7547. " }\n"
  7548. " void g() { return; }\n"
  7549. "};\n"
  7550. "struct B {\n"
  7551. " int x;\n"
  7552. "};\n"
  7553. "}\n",
  7554. LinuxBraceStyle);
  7555. verifyFormat("enum X {\n"
  7556. " Y = 0,\n"
  7557. "}\n",
  7558. LinuxBraceStyle);
  7559. verifyFormat("struct S {\n"
  7560. " int Type;\n"
  7561. " union {\n"
  7562. " int x;\n"
  7563. " double y;\n"
  7564. " } Value;\n"
  7565. " class C\n"
  7566. " {\n"
  7567. " MyFavoriteType Value;\n"
  7568. " } Class;\n"
  7569. "}\n",
  7570. LinuxBraceStyle);
  7571. }
  7572. TEST_F(FormatTest, StroustrupBraceBreaking) {
  7573. FormatStyle StroustrupBraceStyle = getLLVMStyle();
  7574. StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  7575. verifyFormat("namespace a {\n"
  7576. "class A {\n"
  7577. " void f()\n"
  7578. " {\n"
  7579. " if (true) {\n"
  7580. " a();\n"
  7581. " b();\n"
  7582. " }\n"
  7583. " }\n"
  7584. " void g() { return; }\n"
  7585. "};\n"
  7586. "struct B {\n"
  7587. " int x;\n"
  7588. "};\n"
  7589. "}\n",
  7590. StroustrupBraceStyle);
  7591. verifyFormat("void foo()\n"
  7592. "{\n"
  7593. " if (a) {\n"
  7594. " a();\n"
  7595. " }\n"
  7596. " else {\n"
  7597. " b();\n"
  7598. " }\n"
  7599. "}\n",
  7600. StroustrupBraceStyle);
  7601. verifyFormat("#ifdef _DEBUG\n"
  7602. "int foo(int i = 0)\n"
  7603. "#else\n"
  7604. "int foo(int i = 5)\n"
  7605. "#endif\n"
  7606. "{\n"
  7607. " return i;\n"
  7608. "}",
  7609. StroustrupBraceStyle);
  7610. verifyFormat("void foo() {}\n"
  7611. "void bar()\n"
  7612. "#ifdef _DEBUG\n"
  7613. "{\n"
  7614. " foo();\n"
  7615. "}\n"
  7616. "#else\n"
  7617. "{\n"
  7618. "}\n"
  7619. "#endif",
  7620. StroustrupBraceStyle);
  7621. verifyFormat("void foobar() { int i = 5; }\n"
  7622. "#ifdef _DEBUG\n"
  7623. "void bar() {}\n"
  7624. "#else\n"
  7625. "void bar() { foobar(); }\n"
  7626. "#endif",
  7627. StroustrupBraceStyle);
  7628. }
  7629. TEST_F(FormatTest, AllmanBraceBreaking) {
  7630. FormatStyle AllmanBraceStyle = getLLVMStyle();
  7631. AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
  7632. verifyFormat("namespace a\n"
  7633. "{\n"
  7634. "class A\n"
  7635. "{\n"
  7636. " void f()\n"
  7637. " {\n"
  7638. " if (true)\n"
  7639. " {\n"
  7640. " a();\n"
  7641. " b();\n"
  7642. " }\n"
  7643. " }\n"
  7644. " void g() { return; }\n"
  7645. "};\n"
  7646. "struct B\n"
  7647. "{\n"
  7648. " int x;\n"
  7649. "};\n"
  7650. "}",
  7651. AllmanBraceStyle);
  7652. verifyFormat("void f()\n"
  7653. "{\n"
  7654. " if (true)\n"
  7655. " {\n"
  7656. " a();\n"
  7657. " }\n"
  7658. " else if (false)\n"
  7659. " {\n"
  7660. " b();\n"
  7661. " }\n"
  7662. " else\n"
  7663. " {\n"
  7664. " c();\n"
  7665. " }\n"
  7666. "}\n",
  7667. AllmanBraceStyle);
  7668. verifyFormat("void f()\n"
  7669. "{\n"
  7670. " for (int i = 0; i < 10; ++i)\n"
  7671. " {\n"
  7672. " a();\n"
  7673. " }\n"
  7674. " while (false)\n"
  7675. " {\n"
  7676. " b();\n"
  7677. " }\n"
  7678. " do\n"
  7679. " {\n"
  7680. " c();\n"
  7681. " } while (false)\n"
  7682. "}\n",
  7683. AllmanBraceStyle);
  7684. verifyFormat("void f(int a)\n"
  7685. "{\n"
  7686. " switch (a)\n"
  7687. " {\n"
  7688. " case 0:\n"
  7689. " break;\n"
  7690. " case 1:\n"
  7691. " {\n"
  7692. " break;\n"
  7693. " }\n"
  7694. " case 2:\n"
  7695. " {\n"
  7696. " }\n"
  7697. " break;\n"
  7698. " default:\n"
  7699. " break;\n"
  7700. " }\n"
  7701. "}\n",
  7702. AllmanBraceStyle);
  7703. verifyFormat("enum X\n"
  7704. "{\n"
  7705. " Y = 0,\n"
  7706. "}\n",
  7707. AllmanBraceStyle);
  7708. verifyFormat("enum X\n"
  7709. "{\n"
  7710. " Y = 0\n"
  7711. "}\n",
  7712. AllmanBraceStyle);
  7713. verifyFormat("@interface BSApplicationController ()\n"
  7714. "{\n"
  7715. "@private\n"
  7716. " id _extraIvar;\n"
  7717. "}\n"
  7718. "@end\n",
  7719. AllmanBraceStyle);
  7720. verifyFormat("#ifdef _DEBUG\n"
  7721. "int foo(int i = 0)\n"
  7722. "#else\n"
  7723. "int foo(int i = 5)\n"
  7724. "#endif\n"
  7725. "{\n"
  7726. " return i;\n"
  7727. "}",
  7728. AllmanBraceStyle);
  7729. verifyFormat("void foo() {}\n"
  7730. "void bar()\n"
  7731. "#ifdef _DEBUG\n"
  7732. "{\n"
  7733. " foo();\n"
  7734. "}\n"
  7735. "#else\n"
  7736. "{\n"
  7737. "}\n"
  7738. "#endif",
  7739. AllmanBraceStyle);
  7740. verifyFormat("void foobar() { int i = 5; }\n"
  7741. "#ifdef _DEBUG\n"
  7742. "void bar() {}\n"
  7743. "#else\n"
  7744. "void bar() { foobar(); }\n"
  7745. "#endif",
  7746. AllmanBraceStyle);
  7747. // This shouldn't affect ObjC blocks..
  7748. verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
  7749. " // ...\n"
  7750. " int i;\n"
  7751. "}];",
  7752. AllmanBraceStyle);
  7753. verifyFormat("void (^block)(void) = ^{\n"
  7754. " // ...\n"
  7755. " int i;\n"
  7756. "};",
  7757. AllmanBraceStyle);
  7758. // .. or dict literals.
  7759. verifyFormat("void f()\n"
  7760. "{\n"
  7761. " [object someMethod:@{ @\"a\" : @\"b\" }];\n"
  7762. "}",
  7763. AllmanBraceStyle);
  7764. verifyFormat("int f()\n"
  7765. "{ // comment\n"
  7766. " return 42;\n"
  7767. "}",
  7768. AllmanBraceStyle);
  7769. AllmanBraceStyle.ColumnLimit = 19;
  7770. verifyFormat("void f() { int i; }", AllmanBraceStyle);
  7771. AllmanBraceStyle.ColumnLimit = 18;
  7772. verifyFormat("void f()\n"
  7773. "{\n"
  7774. " int i;\n"
  7775. "}",
  7776. AllmanBraceStyle);
  7777. AllmanBraceStyle.ColumnLimit = 80;
  7778. FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
  7779. BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
  7780. BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
  7781. verifyFormat("void f(bool b)\n"
  7782. "{\n"
  7783. " if (b)\n"
  7784. " {\n"
  7785. " return;\n"
  7786. " }\n"
  7787. "}\n",
  7788. BreakBeforeBraceShortIfs);
  7789. verifyFormat("void f(bool b)\n"
  7790. "{\n"
  7791. " if (b) return;\n"
  7792. "}\n",
  7793. BreakBeforeBraceShortIfs);
  7794. verifyFormat("void f(bool b)\n"
  7795. "{\n"
  7796. " while (b)\n"
  7797. " {\n"
  7798. " return;\n"
  7799. " }\n"
  7800. "}\n",
  7801. BreakBeforeBraceShortIfs);
  7802. }
  7803. TEST_F(FormatTest, GNUBraceBreaking) {
  7804. FormatStyle GNUBraceStyle = getLLVMStyle();
  7805. GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
  7806. verifyFormat("namespace a\n"
  7807. "{\n"
  7808. "class A\n"
  7809. "{\n"
  7810. " void f()\n"
  7811. " {\n"
  7812. " int a;\n"
  7813. " {\n"
  7814. " int b;\n"
  7815. " }\n"
  7816. " if (true)\n"
  7817. " {\n"
  7818. " a();\n"
  7819. " b();\n"
  7820. " }\n"
  7821. " }\n"
  7822. " void g() { return; }\n"
  7823. "}\n"
  7824. "}",
  7825. GNUBraceStyle);
  7826. verifyFormat("void f()\n"
  7827. "{\n"
  7828. " if (true)\n"
  7829. " {\n"
  7830. " a();\n"
  7831. " }\n"
  7832. " else if (false)\n"
  7833. " {\n"
  7834. " b();\n"
  7835. " }\n"
  7836. " else\n"
  7837. " {\n"
  7838. " c();\n"
  7839. " }\n"
  7840. "}\n",
  7841. GNUBraceStyle);
  7842. verifyFormat("void f()\n"
  7843. "{\n"
  7844. " for (int i = 0; i < 10; ++i)\n"
  7845. " {\n"
  7846. " a();\n"
  7847. " }\n"
  7848. " while (false)\n"
  7849. " {\n"
  7850. " b();\n"
  7851. " }\n"
  7852. " do\n"
  7853. " {\n"
  7854. " c();\n"
  7855. " }\n"
  7856. " while (false);\n"
  7857. "}\n",
  7858. GNUBraceStyle);
  7859. verifyFormat("void f(int a)\n"
  7860. "{\n"
  7861. " switch (a)\n"
  7862. " {\n"
  7863. " case 0:\n"
  7864. " break;\n"
  7865. " case 1:\n"
  7866. " {\n"
  7867. " break;\n"
  7868. " }\n"
  7869. " case 2:\n"
  7870. " {\n"
  7871. " }\n"
  7872. " break;\n"
  7873. " default:\n"
  7874. " break;\n"
  7875. " }\n"
  7876. "}\n",
  7877. GNUBraceStyle);
  7878. verifyFormat("enum X\n"
  7879. "{\n"
  7880. " Y = 0,\n"
  7881. "}\n",
  7882. GNUBraceStyle);
  7883. verifyFormat("@interface BSApplicationController ()\n"
  7884. "{\n"
  7885. "@private\n"
  7886. " id _extraIvar;\n"
  7887. "}\n"
  7888. "@end\n",
  7889. GNUBraceStyle);
  7890. verifyFormat("#ifdef _DEBUG\n"
  7891. "int foo(int i = 0)\n"
  7892. "#else\n"
  7893. "int foo(int i = 5)\n"
  7894. "#endif\n"
  7895. "{\n"
  7896. " return i;\n"
  7897. "}",
  7898. GNUBraceStyle);
  7899. verifyFormat("void foo() {}\n"
  7900. "void bar()\n"
  7901. "#ifdef _DEBUG\n"
  7902. "{\n"
  7903. " foo();\n"
  7904. "}\n"
  7905. "#else\n"
  7906. "{\n"
  7907. "}\n"
  7908. "#endif",
  7909. GNUBraceStyle);
  7910. verifyFormat("void foobar() { int i = 5; }\n"
  7911. "#ifdef _DEBUG\n"
  7912. "void bar() {}\n"
  7913. "#else\n"
  7914. "void bar() { foobar(); }\n"
  7915. "#endif",
  7916. GNUBraceStyle);
  7917. }
  7918. TEST_F(FormatTest, CatchExceptionReferenceBinding) {
  7919. verifyFormat("void f() {\n"
  7920. " try {\n"
  7921. " } catch (const Exception &e) {\n"
  7922. " }\n"
  7923. "}\n",
  7924. getLLVMStyle());
  7925. }
  7926. TEST_F(FormatTest, UnderstandsPragmas) {
  7927. verifyFormat("#pragma omp reduction(| : var)");
  7928. verifyFormat("#pragma omp reduction(+ : var)");
  7929. EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
  7930. "(including parentheses).",
  7931. format("#pragma mark Any non-hyphenated or hyphenated string "
  7932. "(including parentheses)."));
  7933. }
  7934. #define EXPECT_ALL_STYLES_EQUAL(Styles) \
  7935. for (size_t i = 1; i < Styles.size(); ++i) \
  7936. EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " \
  7937. << Styles.size() \
  7938. << " differs from Style #0"
  7939. TEST_F(FormatTest, GetsPredefinedStyleByName) {
  7940. SmallVector<FormatStyle, 3> Styles;
  7941. Styles.resize(3);
  7942. Styles[0] = getLLVMStyle();
  7943. EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
  7944. EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
  7945. EXPECT_ALL_STYLES_EQUAL(Styles);
  7946. Styles[0] = getGoogleStyle();
  7947. EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
  7948. EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
  7949. EXPECT_ALL_STYLES_EQUAL(Styles);
  7950. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  7951. EXPECT_TRUE(
  7952. getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
  7953. EXPECT_TRUE(
  7954. getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
  7955. EXPECT_ALL_STYLES_EQUAL(Styles);
  7956. Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
  7957. EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
  7958. EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
  7959. EXPECT_ALL_STYLES_EQUAL(Styles);
  7960. Styles[0] = getMozillaStyle();
  7961. EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
  7962. EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
  7963. EXPECT_ALL_STYLES_EQUAL(Styles);
  7964. Styles[0] = getWebKitStyle();
  7965. EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
  7966. EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
  7967. EXPECT_ALL_STYLES_EQUAL(Styles);
  7968. Styles[0] = getGNUStyle();
  7969. EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
  7970. EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
  7971. EXPECT_ALL_STYLES_EQUAL(Styles);
  7972. EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
  7973. }
  7974. TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
  7975. SmallVector<FormatStyle, 8> Styles;
  7976. Styles.resize(2);
  7977. Styles[0] = getGoogleStyle();
  7978. Styles[1] = getLLVMStyle();
  7979. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  7980. EXPECT_ALL_STYLES_EQUAL(Styles);
  7981. Styles.resize(5);
  7982. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  7983. Styles[1] = getLLVMStyle();
  7984. Styles[1].Language = FormatStyle::LK_JavaScript;
  7985. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  7986. Styles[2] = getLLVMStyle();
  7987. Styles[2].Language = FormatStyle::LK_JavaScript;
  7988. EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
  7989. "BasedOnStyle: Google",
  7990. &Styles[2]).value());
  7991. Styles[3] = getLLVMStyle();
  7992. Styles[3].Language = FormatStyle::LK_JavaScript;
  7993. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
  7994. "Language: JavaScript",
  7995. &Styles[3]).value());
  7996. Styles[4] = getLLVMStyle();
  7997. Styles[4].Language = FormatStyle::LK_JavaScript;
  7998. EXPECT_EQ(0, parseConfiguration("---\n"
  7999. "BasedOnStyle: LLVM\n"
  8000. "IndentWidth: 123\n"
  8001. "---\n"
  8002. "BasedOnStyle: Google\n"
  8003. "Language: JavaScript",
  8004. &Styles[4]).value());
  8005. EXPECT_ALL_STYLES_EQUAL(Styles);
  8006. }
  8007. #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \
  8008. Style.FIELD = false; \
  8009. EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \
  8010. EXPECT_TRUE(Style.FIELD); \
  8011. EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \
  8012. EXPECT_FALSE(Style.FIELD);
  8013. #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
  8014. #define CHECK_PARSE(TEXT, FIELD, VALUE) \
  8015. EXPECT_NE(VALUE, Style.FIELD); \
  8016. EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \
  8017. EXPECT_EQ(VALUE, Style.FIELD)
  8018. TEST_F(FormatTest, ParsesConfigurationBools) {
  8019. FormatStyle Style = {};
  8020. Style.Language = FormatStyle::LK_Cpp;
  8021. CHECK_PARSE_BOOL(AlignAfterOpenBracket);
  8022. CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
  8023. CHECK_PARSE_BOOL(AlignOperands);
  8024. CHECK_PARSE_BOOL(AlignTrailingComments);
  8025. CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
  8026. CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
  8027. CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
  8028. CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
  8029. CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
  8030. CHECK_PARSE_BOOL(AlwaysBreakAfterDefinitionReturnType);
  8031. CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
  8032. CHECK_PARSE_BOOL(BinPackParameters);
  8033. CHECK_PARSE_BOOL(BinPackArguments);
  8034. CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
  8035. CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
  8036. CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
  8037. CHECK_PARSE_BOOL(DerivePointerAlignment);
  8038. CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
  8039. CHECK_PARSE_BOOL(IndentCaseLabels);
  8040. CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
  8041. CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
  8042. CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
  8043. CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
  8044. CHECK_PARSE_BOOL(Cpp11BracedListStyle);
  8045. CHECK_PARSE_BOOL(SpacesInParentheses);
  8046. CHECK_PARSE_BOOL(SpacesInSquareBrackets);
  8047. CHECK_PARSE_BOOL(SpacesInAngles);
  8048. CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
  8049. CHECK_PARSE_BOOL(SpacesInContainerLiterals);
  8050. CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
  8051. CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
  8052. CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
  8053. }
  8054. #undef CHECK_PARSE_BOOL
  8055. TEST_F(FormatTest, ParsesConfiguration) {
  8056. FormatStyle Style = {};
  8057. Style.Language = FormatStyle::LK_Cpp;
  8058. CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
  8059. CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
  8060. ConstructorInitializerIndentWidth, 1234u);
  8061. CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
  8062. CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
  8063. CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
  8064. CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
  8065. PenaltyBreakBeforeFirstCallParameter, 1234u);
  8066. CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
  8067. CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
  8068. PenaltyReturnTypeOnItsOwnLine, 1234u);
  8069. CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
  8070. SpacesBeforeTrailingComments, 1234u);
  8071. CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
  8072. CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
  8073. Style.PointerAlignment = FormatStyle::PAS_Middle;
  8074. CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
  8075. FormatStyle::PAS_Left);
  8076. CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
  8077. FormatStyle::PAS_Right);
  8078. CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
  8079. FormatStyle::PAS_Middle);
  8080. // For backward compatibility:
  8081. CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
  8082. FormatStyle::PAS_Left);
  8083. CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
  8084. FormatStyle::PAS_Right);
  8085. CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
  8086. FormatStyle::PAS_Middle);
  8087. Style.Standard = FormatStyle::LS_Auto;
  8088. CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
  8089. CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
  8090. CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
  8091. CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
  8092. CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
  8093. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  8094. CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
  8095. BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
  8096. CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
  8097. FormatStyle::BOS_None);
  8098. CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
  8099. FormatStyle::BOS_All);
  8100. // For backward compatibility:
  8101. CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
  8102. FormatStyle::BOS_None);
  8103. CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
  8104. FormatStyle::BOS_All);
  8105. Style.UseTab = FormatStyle::UT_ForIndentation;
  8106. CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
  8107. CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
  8108. CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
  8109. // For backward compatibility:
  8110. CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
  8111. CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
  8112. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  8113. CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
  8114. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
  8115. CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
  8116. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
  8117. CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
  8118. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
  8119. CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
  8120. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
  8121. // For backward compatibility:
  8122. CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
  8123. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
  8124. CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
  8125. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
  8126. Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
  8127. CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
  8128. FormatStyle::SBPO_Never);
  8129. CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
  8130. FormatStyle::SBPO_Always);
  8131. CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
  8132. FormatStyle::SBPO_ControlStatements);
  8133. // For backward compatibility:
  8134. CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
  8135. FormatStyle::SBPO_Never);
  8136. CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
  8137. FormatStyle::SBPO_ControlStatements);
  8138. Style.ColumnLimit = 123;
  8139. FormatStyle BaseStyle = getLLVMStyle();
  8140. CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
  8141. CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
  8142. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  8143. CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
  8144. FormatStyle::BS_Attach);
  8145. CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
  8146. FormatStyle::BS_Linux);
  8147. CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
  8148. FormatStyle::BS_Stroustrup);
  8149. CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
  8150. FormatStyle::BS_Allman);
  8151. CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
  8152. Style.NamespaceIndentation = FormatStyle::NI_All;
  8153. CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
  8154. FormatStyle::NI_None);
  8155. CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
  8156. FormatStyle::NI_Inner);
  8157. CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
  8158. FormatStyle::NI_All);
  8159. Style.ForEachMacros.clear();
  8160. std::vector<std::string> BoostForeach;
  8161. BoostForeach.push_back("BOOST_FOREACH");
  8162. CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
  8163. std::vector<std::string> BoostAndQForeach;
  8164. BoostAndQForeach.push_back("BOOST_FOREACH");
  8165. BoostAndQForeach.push_back("Q_FOREACH");
  8166. CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
  8167. BoostAndQForeach);
  8168. }
  8169. TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
  8170. FormatStyle Style = {};
  8171. Style.Language = FormatStyle::LK_Cpp;
  8172. CHECK_PARSE("Language: Cpp\n"
  8173. "IndentWidth: 12",
  8174. IndentWidth, 12u);
  8175. EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
  8176. "IndentWidth: 34",
  8177. &Style),
  8178. ParseError::Unsuitable);
  8179. EXPECT_EQ(12u, Style.IndentWidth);
  8180. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  8181. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  8182. Style.Language = FormatStyle::LK_JavaScript;
  8183. CHECK_PARSE("Language: JavaScript\n"
  8184. "IndentWidth: 12",
  8185. IndentWidth, 12u);
  8186. CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
  8187. EXPECT_EQ(parseConfiguration("Language: Cpp\n"
  8188. "IndentWidth: 34",
  8189. &Style),
  8190. ParseError::Unsuitable);
  8191. EXPECT_EQ(23u, Style.IndentWidth);
  8192. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  8193. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  8194. CHECK_PARSE("BasedOnStyle: LLVM\n"
  8195. "IndentWidth: 67",
  8196. IndentWidth, 67u);
  8197. CHECK_PARSE("---\n"
  8198. "Language: JavaScript\n"
  8199. "IndentWidth: 12\n"
  8200. "---\n"
  8201. "Language: Cpp\n"
  8202. "IndentWidth: 34\n"
  8203. "...\n",
  8204. IndentWidth, 12u);
  8205. Style.Language = FormatStyle::LK_Cpp;
  8206. CHECK_PARSE("---\n"
  8207. "Language: JavaScript\n"
  8208. "IndentWidth: 12\n"
  8209. "---\n"
  8210. "Language: Cpp\n"
  8211. "IndentWidth: 34\n"
  8212. "...\n",
  8213. IndentWidth, 34u);
  8214. CHECK_PARSE("---\n"
  8215. "IndentWidth: 78\n"
  8216. "---\n"
  8217. "Language: JavaScript\n"
  8218. "IndentWidth: 56\n"
  8219. "...\n",
  8220. IndentWidth, 78u);
  8221. Style.ColumnLimit = 123;
  8222. Style.IndentWidth = 234;
  8223. Style.BreakBeforeBraces = FormatStyle::BS_Linux;
  8224. Style.TabWidth = 345;
  8225. EXPECT_FALSE(parseConfiguration("---\n"
  8226. "IndentWidth: 456\n"
  8227. "BreakBeforeBraces: Allman\n"
  8228. "---\n"
  8229. "Language: JavaScript\n"
  8230. "IndentWidth: 111\n"
  8231. "TabWidth: 111\n"
  8232. "---\n"
  8233. "Language: Cpp\n"
  8234. "BreakBeforeBraces: Stroustrup\n"
  8235. "TabWidth: 789\n"
  8236. "...\n",
  8237. &Style));
  8238. EXPECT_EQ(123u, Style.ColumnLimit);
  8239. EXPECT_EQ(456u, Style.IndentWidth);
  8240. EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
  8241. EXPECT_EQ(789u, Style.TabWidth);
  8242. EXPECT_EQ(parseConfiguration("---\n"
  8243. "Language: JavaScript\n"
  8244. "IndentWidth: 56\n"
  8245. "---\n"
  8246. "IndentWidth: 78\n"
  8247. "...\n",
  8248. &Style),
  8249. ParseError::Error);
  8250. EXPECT_EQ(parseConfiguration("---\n"
  8251. "Language: JavaScript\n"
  8252. "IndentWidth: 56\n"
  8253. "---\n"
  8254. "Language: JavaScript\n"
  8255. "IndentWidth: 78\n"
  8256. "...\n",
  8257. &Style),
  8258. ParseError::Error);
  8259. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  8260. }
  8261. #undef CHECK_PARSE
  8262. TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
  8263. FormatStyle Style = {};
  8264. Style.Language = FormatStyle::LK_JavaScript;
  8265. Style.BreakBeforeTernaryOperators = true;
  8266. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
  8267. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  8268. Style.BreakBeforeTernaryOperators = true;
  8269. EXPECT_EQ(0, parseConfiguration("---\n"
  8270. "BasedOnStyle: Google\n"
  8271. "---\n"
  8272. "Language: JavaScript\n"
  8273. "IndentWidth: 76\n"
  8274. "...\n", &Style).value());
  8275. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  8276. EXPECT_EQ(76u, Style.IndentWidth);
  8277. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  8278. }
  8279. TEST_F(FormatTest, ConfigurationRoundTripTest) {
  8280. FormatStyle Style = getLLVMStyle();
  8281. std::string YAML = configurationAsText(Style);
  8282. FormatStyle ParsedStyle = {};
  8283. ParsedStyle.Language = FormatStyle::LK_Cpp;
  8284. EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
  8285. EXPECT_EQ(Style, ParsedStyle);
  8286. }
  8287. TEST_F(FormatTest, WorksFor8bitEncodings) {
  8288. EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
  8289. "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
  8290. "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
  8291. "\"\xef\xee\xf0\xf3...\"",
  8292. format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
  8293. "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
  8294. "\xef\xee\xf0\xf3...\"",
  8295. getLLVMStyleWithColumns(12)));
  8296. }
  8297. TEST_F(FormatTest, HandlesUTF8BOM) {
  8298. EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
  8299. EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
  8300. format("\xef\xbb\xbf#include <iostream>"));
  8301. EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
  8302. format("\xef\xbb\xbf\n#include <iostream>"));
  8303. }
  8304. // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
  8305. #if !defined(_MSC_VER)
  8306. TEST_F(FormatTest, CountsUTF8CharactersProperly) {
  8307. verifyFormat("\"Однажды в студёную зимнюю пору...\"",
  8308. getLLVMStyleWithColumns(35));
  8309. verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
  8310. getLLVMStyleWithColumns(31));
  8311. verifyFormat("// Однажды в студёную зимнюю пору...",
  8312. getLLVMStyleWithColumns(36));
  8313. verifyFormat("// 一 二 三 四 五 六 七 八 九 十",
  8314. getLLVMStyleWithColumns(32));
  8315. verifyFormat("/* Однажды в студёную зимнюю пору... */",
  8316. getLLVMStyleWithColumns(39));
  8317. verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
  8318. getLLVMStyleWithColumns(35));
  8319. }
  8320. TEST_F(FormatTest, SplitsUTF8Strings) {
  8321. // Non-printable characters' width is currently considered to be the length in
  8322. // bytes in UTF8. The characters can be displayed in very different manner
  8323. // (zero-width, single width with a substitution glyph, expanded to their code
  8324. // (e.g. "<8d>"), so there's no single correct way to handle them.
  8325. EXPECT_EQ("\"aaaaÄ\"\n"
  8326. "\"\xc2\x8d\";",
  8327. format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  8328. EXPECT_EQ("\"aaaaaaaÄ\"\n"
  8329. "\"\xc2\x8d\";",
  8330. format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  8331. EXPECT_EQ(
  8332. "\"Однажды, в \"\n"
  8333. "\"студёную \"\n"
  8334. "\"зимнюю \"\n"
  8335. "\"пору,\"",
  8336. format("\"Однажды, в студёную зимнюю пору,\"",
  8337. getLLVMStyleWithColumns(13)));
  8338. EXPECT_EQ("\"一 二 三 \"\n"
  8339. "\"四 五六 \"\n"
  8340. "\"七 八 九 \"\n"
  8341. "\"十\"",
  8342. format("\"一 二 三 四 五六 七 八 九 十\"",
  8343. getLLVMStyleWithColumns(11)));
  8344. EXPECT_EQ("\"一\t二 \"\n"
  8345. "\"\t三 \"\n"
  8346. "\"四 五\t六 \"\n"
  8347. "\"\t七 \"\n"
  8348. "\"八九十\tqq\"",
  8349. format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
  8350. getLLVMStyleWithColumns(11)));
  8351. }
  8352. TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
  8353. EXPECT_EQ("const char *sssss =\n"
  8354. " \"一二三四五六七八\\\n"
  8355. " 九 十\";",
  8356. format("const char *sssss = \"一二三四五六七八\\\n"
  8357. " 九 十\";",
  8358. getLLVMStyleWithColumns(30)));
  8359. }
  8360. TEST_F(FormatTest, SplitsUTF8LineComments) {
  8361. EXPECT_EQ("// aaaaÄ\xc2\x8d",
  8362. format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
  8363. EXPECT_EQ("// Я из лесу\n"
  8364. "// вышел; был\n"
  8365. "// сильный\n"
  8366. "// мороз.",
  8367. format("// Я из лесу вышел; был сильный мороз.",
  8368. getLLVMStyleWithColumns(13)));
  8369. EXPECT_EQ("// 一二三\n"
  8370. "// 四五六七\n"
  8371. "// 八 九\n"
  8372. "// 十",
  8373. format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9)));
  8374. }
  8375. TEST_F(FormatTest, SplitsUTF8BlockComments) {
  8376. EXPECT_EQ("/* Гляжу,\n"
  8377. " * поднимается\n"
  8378. " * медленно в\n"
  8379. " * гору\n"
  8380. " * Лошадка,\n"
  8381. " * везущая\n"
  8382. " * хворосту\n"
  8383. " * воз. */",
  8384. format("/* Гляжу, поднимается медленно в гору\n"
  8385. " * Лошадка, везущая хворосту воз. */",
  8386. getLLVMStyleWithColumns(13)));
  8387. EXPECT_EQ(
  8388. "/* 一二三\n"
  8389. " * 四五六七\n"
  8390. " * 八 九\n"
  8391. " * 十 */",
  8392. format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9)));
  8393. EXPECT_EQ("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
  8394. " * 𝕓𝕪𝕥𝕖\n"
  8395. " * 𝖀𝕿𝕱-𝟠 */",
  8396. format("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12)));
  8397. }
  8398. #endif // _MSC_VER
  8399. TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
  8400. FormatStyle Style = getLLVMStyle();
  8401. Style.ConstructorInitializerIndentWidth = 4;
  8402. verifyFormat(
  8403. "SomeClass::Constructor()\n"
  8404. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  8405. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  8406. Style);
  8407. Style.ConstructorInitializerIndentWidth = 2;
  8408. verifyFormat(
  8409. "SomeClass::Constructor()\n"
  8410. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  8411. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  8412. Style);
  8413. Style.ConstructorInitializerIndentWidth = 0;
  8414. verifyFormat(
  8415. "SomeClass::Constructor()\n"
  8416. ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  8417. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  8418. Style);
  8419. }
  8420. TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
  8421. FormatStyle Style = getLLVMStyle();
  8422. Style.BreakConstructorInitializersBeforeComma = true;
  8423. Style.ConstructorInitializerIndentWidth = 4;
  8424. verifyFormat("SomeClass::Constructor()\n"
  8425. " : a(a)\n"
  8426. " , b(b)\n"
  8427. " , c(c) {}",
  8428. Style);
  8429. verifyFormat("SomeClass::Constructor()\n"
  8430. " : a(a) {}",
  8431. Style);
  8432. Style.ColumnLimit = 0;
  8433. verifyFormat("SomeClass::Constructor()\n"
  8434. " : a(a) {}",
  8435. Style);
  8436. verifyFormat("SomeClass::Constructor()\n"
  8437. " : a(a)\n"
  8438. " , b(b)\n"
  8439. " , c(c) {}",
  8440. Style);
  8441. verifyFormat("SomeClass::Constructor()\n"
  8442. " : a(a) {\n"
  8443. " foo();\n"
  8444. " bar();\n"
  8445. "}",
  8446. Style);
  8447. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  8448. verifyFormat("SomeClass::Constructor()\n"
  8449. " : a(a)\n"
  8450. " , b(b)\n"
  8451. " , c(c) {\n}",
  8452. Style);
  8453. verifyFormat("SomeClass::Constructor()\n"
  8454. " : a(a) {\n}",
  8455. Style);
  8456. Style.ColumnLimit = 80;
  8457. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
  8458. Style.ConstructorInitializerIndentWidth = 2;
  8459. verifyFormat("SomeClass::Constructor()\n"
  8460. " : a(a)\n"
  8461. " , b(b)\n"
  8462. " , c(c) {}",
  8463. Style);
  8464. Style.ConstructorInitializerIndentWidth = 0;
  8465. verifyFormat("SomeClass::Constructor()\n"
  8466. ": a(a)\n"
  8467. ", b(b)\n"
  8468. ", c(c) {}",
  8469. Style);
  8470. Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  8471. Style.ConstructorInitializerIndentWidth = 4;
  8472. verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
  8473. verifyFormat(
  8474. "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
  8475. Style);
  8476. verifyFormat(
  8477. "SomeClass::Constructor()\n"
  8478. " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
  8479. Style);
  8480. Style.ConstructorInitializerIndentWidth = 4;
  8481. Style.ColumnLimit = 60;
  8482. verifyFormat("SomeClass::Constructor()\n"
  8483. " : aaaaaaaa(aaaaaaaa)\n"
  8484. " , aaaaaaaa(aaaaaaaa)\n"
  8485. " , aaaaaaaa(aaaaaaaa) {}",
  8486. Style);
  8487. }
  8488. TEST_F(FormatTest, Destructors) {
  8489. verifyFormat("void F(int &i) { i.~int(); }");
  8490. verifyFormat("void F(int &i) { i->~int(); }");
  8491. }
  8492. TEST_F(FormatTest, FormatsWithWebKitStyle) {
  8493. FormatStyle Style = getWebKitStyle();
  8494. // Don't indent in outer namespaces.
  8495. verifyFormat("namespace outer {\n"
  8496. "int i;\n"
  8497. "namespace inner {\n"
  8498. " int i;\n"
  8499. "} // namespace inner\n"
  8500. "} // namespace outer\n"
  8501. "namespace other_outer {\n"
  8502. "int i;\n"
  8503. "}",
  8504. Style);
  8505. // Don't indent case labels.
  8506. verifyFormat("switch (variable) {\n"
  8507. "case 1:\n"
  8508. "case 2:\n"
  8509. " doSomething();\n"
  8510. " break;\n"
  8511. "default:\n"
  8512. " ++variable;\n"
  8513. "}",
  8514. Style);
  8515. // Wrap before binary operators.
  8516. EXPECT_EQ(
  8517. "void f()\n"
  8518. "{\n"
  8519. " if (aaaaaaaaaaaaaaaa\n"
  8520. " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
  8521. " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  8522. " return;\n"
  8523. "}",
  8524. format(
  8525. "void f() {\n"
  8526. "if (aaaaaaaaaaaaaaaa\n"
  8527. "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
  8528. "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  8529. "return;\n"
  8530. "}",
  8531. Style));
  8532. // Allow functions on a single line.
  8533. verifyFormat("void f() { return; }", Style);
  8534. // Constructor initializers are formatted one per line with the "," on the
  8535. // new line.
  8536. verifyFormat("Constructor()\n"
  8537. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  8538. " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
  8539. " aaaaaaaaaaaaaa)\n"
  8540. " , aaaaaaaaaaaaaaaaaaaaaaa()\n"
  8541. "{\n"
  8542. "}",
  8543. Style);
  8544. verifyFormat("SomeClass::Constructor()\n"
  8545. " : a(a)\n"
  8546. "{\n"
  8547. "}",
  8548. Style);
  8549. EXPECT_EQ("SomeClass::Constructor()\n"
  8550. " : a(a)\n"
  8551. "{\n"
  8552. "}",
  8553. format("SomeClass::Constructor():a(a){}", Style));
  8554. verifyFormat("SomeClass::Constructor()\n"
  8555. " : a(a)\n"
  8556. " , b(b)\n"
  8557. " , c(c)\n"
  8558. "{\n"
  8559. "}", Style);
  8560. verifyFormat("SomeClass::Constructor()\n"
  8561. " : a(a)\n"
  8562. "{\n"
  8563. " foo();\n"
  8564. " bar();\n"
  8565. "}",
  8566. Style);
  8567. // Access specifiers should be aligned left.
  8568. verifyFormat("class C {\n"
  8569. "public:\n"
  8570. " int i;\n"
  8571. "};",
  8572. Style);
  8573. // Do not align comments.
  8574. verifyFormat("int a; // Do not\n"
  8575. "double b; // align comments.",
  8576. Style);
  8577. // Do not align operands.
  8578. EXPECT_EQ("ASSERT(aaaa\n"
  8579. " || bbbb);",
  8580. format("ASSERT ( aaaa\n||bbbb);", Style));
  8581. // Accept input's line breaks.
  8582. EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
  8583. " || bbbbbbbbbbbbbbb) {\n"
  8584. " i++;\n"
  8585. "}",
  8586. format("if (aaaaaaaaaaaaaaa\n"
  8587. "|| bbbbbbbbbbbbbbb) { i++; }",
  8588. Style));
  8589. EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
  8590. " i++;\n"
  8591. "}",
  8592. format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
  8593. // Don't automatically break all macro definitions (llvm.org/PR17842).
  8594. verifyFormat("#define aNumber 10", Style);
  8595. // However, generally keep the line breaks that the user authored.
  8596. EXPECT_EQ("#define aNumber \\\n"
  8597. " 10",
  8598. format("#define aNumber \\\n"
  8599. " 10",
  8600. Style));
  8601. // Keep empty and one-element array literals on a single line.
  8602. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
  8603. " copyItems:YES];",
  8604. format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
  8605. "copyItems:YES];",
  8606. Style));
  8607. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
  8608. " copyItems:YES];",
  8609. format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
  8610. " copyItems:YES];",
  8611. Style));
  8612. // FIXME: This does not seem right, there should be more indentation before
  8613. // the array literal's entries. Nested blocks have the same problem.
  8614. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
  8615. " @\"a\",\n"
  8616. " @\"a\"\n"
  8617. "]\n"
  8618. " copyItems:YES];",
  8619. format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
  8620. " @\"a\",\n"
  8621. " @\"a\"\n"
  8622. " ]\n"
  8623. " copyItems:YES];",
  8624. Style));
  8625. EXPECT_EQ(
  8626. "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
  8627. " copyItems:YES];",
  8628. format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
  8629. " copyItems:YES];",
  8630. Style));
  8631. verifyFormat("[self.a b:c c:d];", Style);
  8632. EXPECT_EQ("[self.a b:c\n"
  8633. " c:d];",
  8634. format("[self.a b:c\n"
  8635. "c:d];",
  8636. Style));
  8637. }
  8638. TEST_F(FormatTest, FormatsLambdas) {
  8639. verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
  8640. verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
  8641. verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
  8642. verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
  8643. verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
  8644. verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
  8645. verifyFormat("void f() {\n"
  8646. " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
  8647. "}\n");
  8648. verifyFormat("void f() {\n"
  8649. " other(x.begin(), //\n"
  8650. " x.end(), //\n"
  8651. " [&](int, int) { return 1; });\n"
  8652. "}\n");
  8653. verifyFormat("SomeFunction([]() { // A cool function...\n"
  8654. " return 43;\n"
  8655. "});");
  8656. EXPECT_EQ("SomeFunction([]() {\n"
  8657. "#define A a\n"
  8658. " return 43;\n"
  8659. "});",
  8660. format("SomeFunction([](){\n"
  8661. "#define A a\n"
  8662. "return 43;\n"
  8663. "});"));
  8664. verifyFormat("void f() {\n"
  8665. " SomeFunction([](decltype(x), A *a) {});\n"
  8666. "}");
  8667. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  8668. " [](const aaaaaaaaaa &a) { return a; });");
  8669. verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
  8670. " SomeOtherFunctioooooooooooooooooooooooooon();\n"
  8671. "});");
  8672. verifyFormat("Constructor()\n"
  8673. " : Field([] { // comment\n"
  8674. " int i;\n"
  8675. " }) {}");
  8676. // Lambdas with return types.
  8677. verifyFormat("int c = []() -> int { return 2; }();\n");
  8678. verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
  8679. verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
  8680. verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
  8681. verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
  8682. verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
  8683. verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
  8684. verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
  8685. " int j) -> int {\n"
  8686. " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
  8687. "};");
  8688. // Multiple lambdas in the same parentheses change indentation rules.
  8689. verifyFormat("SomeFunction(\n"
  8690. " []() {\n"
  8691. " int i = 42;\n"
  8692. " return i;\n"
  8693. " },\n"
  8694. " []() {\n"
  8695. " int j = 43;\n"
  8696. " return j;\n"
  8697. " });");
  8698. // More complex introducers.
  8699. verifyFormat("return [i, args...] {};");
  8700. // Not lambdas.
  8701. verifyFormat("constexpr char hello[]{\"hello\"};");
  8702. verifyFormat("double &operator[](int i) { return 0; }\n"
  8703. "int i;");
  8704. verifyFormat("std::unique_ptr<int[]> foo() {}");
  8705. verifyFormat("int i = a[a][a]->f();");
  8706. verifyFormat("int i = (*b)[a]->f();");
  8707. // Other corner cases.
  8708. verifyFormat("void f() {\n"
  8709. " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
  8710. " );\n"
  8711. "}");
  8712. // Lambdas created through weird macros.
  8713. verifyFormat("void f() {\n"
  8714. " MACRO((const AA &a) { return 1; });\n"
  8715. "}");
  8716. verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
  8717. " doo_dah();\n"
  8718. " doo_dah();\n"
  8719. " })) {\n"
  8720. "}");
  8721. }
  8722. TEST_F(FormatTest, FormatsBlocks) {
  8723. FormatStyle ShortBlocks = getLLVMStyle();
  8724. ShortBlocks.AllowShortBlocksOnASingleLine = true;
  8725. verifyFormat("int (^Block)(int, int);", ShortBlocks);
  8726. verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
  8727. verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
  8728. verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
  8729. verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
  8730. verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
  8731. verifyFormat("foo(^{ bar(); });", ShortBlocks);
  8732. verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
  8733. verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
  8734. verifyFormat("[operation setCompletionBlock:^{\n"
  8735. " [self onOperationDone];\n"
  8736. "}];");
  8737. verifyFormat("int i = {[operation setCompletionBlock:^{\n"
  8738. " [self onOperationDone];\n"
  8739. "}]};");
  8740. verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
  8741. " f();\n"
  8742. "}];");
  8743. verifyFormat("int a = [operation block:^int(int *i) {\n"
  8744. " return 1;\n"
  8745. "}];");
  8746. verifyFormat("[myObject doSomethingWith:arg1\n"
  8747. " aaa:^int(int *a) {\n"
  8748. " return 1;\n"
  8749. " }\n"
  8750. " bbb:f(a * bbbbbbbb)];");
  8751. verifyFormat("[operation setCompletionBlock:^{\n"
  8752. " [self.delegate newDataAvailable];\n"
  8753. "}];",
  8754. getLLVMStyleWithColumns(60));
  8755. verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
  8756. " NSString *path = [self sessionFilePath];\n"
  8757. " if (path) {\n"
  8758. " // ...\n"
  8759. " }\n"
  8760. "});");
  8761. verifyFormat("[[SessionService sharedService]\n"
  8762. " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  8763. " if (window) {\n"
  8764. " [self windowDidLoad:window];\n"
  8765. " } else {\n"
  8766. " [self errorLoadingWindow];\n"
  8767. " }\n"
  8768. " }];");
  8769. verifyFormat("void (^largeBlock)(void) = ^{\n"
  8770. " // ...\n"
  8771. "};\n",
  8772. getLLVMStyleWithColumns(40));
  8773. verifyFormat("[[SessionService sharedService]\n"
  8774. " loadWindowWithCompletionBlock: //\n"
  8775. " ^(SessionWindow *window) {\n"
  8776. " if (window) {\n"
  8777. " [self windowDidLoad:window];\n"
  8778. " } else {\n"
  8779. " [self errorLoadingWindow];\n"
  8780. " }\n"
  8781. " }];",
  8782. getLLVMStyleWithColumns(60));
  8783. verifyFormat("[myObject doSomethingWith:arg1\n"
  8784. " firstBlock:^(Foo *a) {\n"
  8785. " // ...\n"
  8786. " int i;\n"
  8787. " }\n"
  8788. " secondBlock:^(Bar *b) {\n"
  8789. " // ...\n"
  8790. " int i;\n"
  8791. " }\n"
  8792. " thirdBlock:^Foo(Bar *b) {\n"
  8793. " // ...\n"
  8794. " int i;\n"
  8795. " }];");
  8796. verifyFormat("[myObject doSomethingWith:arg1\n"
  8797. " firstBlock:-1\n"
  8798. " secondBlock:^(Bar *b) {\n"
  8799. " // ...\n"
  8800. " int i;\n"
  8801. " }];");
  8802. verifyFormat("f(^{\n"
  8803. " @autoreleasepool {\n"
  8804. " if (a) {\n"
  8805. " g();\n"
  8806. " }\n"
  8807. " }\n"
  8808. "});");
  8809. verifyFormat("Block b = ^int *(A *a, B *b) {}");
  8810. FormatStyle FourIndent = getLLVMStyle();
  8811. FourIndent.ObjCBlockIndentWidth = 4;
  8812. verifyFormat("[operation setCompletionBlock:^{\n"
  8813. " [self onOperationDone];\n"
  8814. "}];",
  8815. FourIndent);
  8816. }
  8817. TEST_F(FormatTest, SupportsCRLF) {
  8818. EXPECT_EQ("int a;\r\n"
  8819. "int b;\r\n"
  8820. "int c;\r\n",
  8821. format("int a;\r\n"
  8822. " int b;\r\n"
  8823. " int c;\r\n",
  8824. getLLVMStyle()));
  8825. EXPECT_EQ("int a;\r\n"
  8826. "int b;\r\n"
  8827. "int c;\r\n",
  8828. format("int a;\r\n"
  8829. " int b;\n"
  8830. " int c;\r\n",
  8831. getLLVMStyle()));
  8832. EXPECT_EQ("int a;\n"
  8833. "int b;\n"
  8834. "int c;\n",
  8835. format("int a;\r\n"
  8836. " int b;\n"
  8837. " int c;\n",
  8838. getLLVMStyle()));
  8839. EXPECT_EQ("\"aaaaaaa \"\r\n"
  8840. "\"bbbbbbb\";\r\n",
  8841. format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
  8842. EXPECT_EQ("#define A \\\r\n"
  8843. " b; \\\r\n"
  8844. " c; \\\r\n"
  8845. " d;\r\n",
  8846. format("#define A \\\r\n"
  8847. " b; \\\r\n"
  8848. " c; d; \r\n",
  8849. getGoogleStyle()));
  8850. EXPECT_EQ("/*\r\n"
  8851. "multi line block comments\r\n"
  8852. "should not introduce\r\n"
  8853. "an extra carriage return\r\n"
  8854. "*/\r\n",
  8855. format("/*\r\n"
  8856. "multi line block comments\r\n"
  8857. "should not introduce\r\n"
  8858. "an extra carriage return\r\n"
  8859. "*/\r\n"));
  8860. }
  8861. TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
  8862. verifyFormat("MY_CLASS(C) {\n"
  8863. " int i;\n"
  8864. " int j;\n"
  8865. "};");
  8866. }
  8867. TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
  8868. FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
  8869. TwoIndent.ContinuationIndentWidth = 2;
  8870. EXPECT_EQ("int i =\n"
  8871. " longFunction(\n"
  8872. " arg);",
  8873. format("int i = longFunction(arg);", TwoIndent));
  8874. FormatStyle SixIndent = getLLVMStyleWithColumns(20);
  8875. SixIndent.ContinuationIndentWidth = 6;
  8876. EXPECT_EQ("int i =\n"
  8877. " longFunction(\n"
  8878. " arg);",
  8879. format("int i = longFunction(arg);", SixIndent));
  8880. }
  8881. TEST_F(FormatTest, SpacesInAngles) {
  8882. FormatStyle Spaces = getLLVMStyle();
  8883. Spaces.SpacesInAngles = true;
  8884. verifyFormat("static_cast< int >(arg);", Spaces);
  8885. verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
  8886. verifyFormat("f< int, float >();", Spaces);
  8887. verifyFormat("template <> g() {}", Spaces);
  8888. verifyFormat("template < std::vector< int > > f() {}", Spaces);
  8889. Spaces.Standard = FormatStyle::LS_Cpp03;
  8890. Spaces.SpacesInAngles = true;
  8891. verifyFormat("A< A< int > >();", Spaces);
  8892. Spaces.SpacesInAngles = false;
  8893. verifyFormat("A<A<int> >();", Spaces);
  8894. Spaces.Standard = FormatStyle::LS_Cpp11;
  8895. Spaces.SpacesInAngles = true;
  8896. verifyFormat("A< A< int > >();", Spaces);
  8897. Spaces.SpacesInAngles = false;
  8898. verifyFormat("A<A<int>>();", Spaces);
  8899. }
  8900. TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
  8901. std::string code = "#if A\n"
  8902. "#if B\n"
  8903. "a.\n"
  8904. "#endif\n"
  8905. " a = 1;\n"
  8906. "#else\n"
  8907. "#endif\n"
  8908. "#if C\n"
  8909. "#else\n"
  8910. "#endif\n";
  8911. EXPECT_EQ(code, format(code));
  8912. }
  8913. TEST_F(FormatTest, HandleConflictMarkers) {
  8914. // Git/SVN conflict markers.
  8915. EXPECT_EQ("int a;\n"
  8916. "void f() {\n"
  8917. " callme(some(parameter1,\n"
  8918. "<<<<<<< text by the vcs\n"
  8919. " parameter2),\n"
  8920. "||||||| text by the vcs\n"
  8921. " parameter2),\n"
  8922. " parameter3,\n"
  8923. "======= text by the vcs\n"
  8924. " parameter2, parameter3),\n"
  8925. ">>>>>>> text by the vcs\n"
  8926. " otherparameter);\n",
  8927. format("int a;\n"
  8928. "void f() {\n"
  8929. " callme(some(parameter1,\n"
  8930. "<<<<<<< text by the vcs\n"
  8931. " parameter2),\n"
  8932. "||||||| text by the vcs\n"
  8933. " parameter2),\n"
  8934. " parameter3,\n"
  8935. "======= text by the vcs\n"
  8936. " parameter2,\n"
  8937. " parameter3),\n"
  8938. ">>>>>>> text by the vcs\n"
  8939. " otherparameter);\n"));
  8940. // Perforce markers.
  8941. EXPECT_EQ("void f() {\n"
  8942. " function(\n"
  8943. ">>>> text by the vcs\n"
  8944. " parameter,\n"
  8945. "==== text by the vcs\n"
  8946. " parameter,\n"
  8947. "==== text by the vcs\n"
  8948. " parameter,\n"
  8949. "<<<< text by the vcs\n"
  8950. " parameter);\n",
  8951. format("void f() {\n"
  8952. " function(\n"
  8953. ">>>> text by the vcs\n"
  8954. " parameter,\n"
  8955. "==== text by the vcs\n"
  8956. " parameter,\n"
  8957. "==== text by the vcs\n"
  8958. " parameter,\n"
  8959. "<<<< text by the vcs\n"
  8960. " parameter);\n"));
  8961. EXPECT_EQ("<<<<<<<\n"
  8962. "|||||||\n"
  8963. "=======\n"
  8964. ">>>>>>>",
  8965. format("<<<<<<<\n"
  8966. "|||||||\n"
  8967. "=======\n"
  8968. ">>>>>>>"));
  8969. EXPECT_EQ("<<<<<<<\n"
  8970. "|||||||\n"
  8971. "int i;\n"
  8972. "=======\n"
  8973. ">>>>>>>",
  8974. format("<<<<<<<\n"
  8975. "|||||||\n"
  8976. "int i;\n"
  8977. "=======\n"
  8978. ">>>>>>>"));
  8979. // FIXME: Handle parsing of macros around conflict markers correctly:
  8980. EXPECT_EQ("#define Macro \\\n"
  8981. "<<<<<<<\n"
  8982. "Something \\\n"
  8983. "|||||||\n"
  8984. "Else \\\n"
  8985. "=======\n"
  8986. "Other \\\n"
  8987. ">>>>>>>\n"
  8988. " End int i;\n",
  8989. format("#define Macro \\\n"
  8990. "<<<<<<<\n"
  8991. " Something \\\n"
  8992. "|||||||\n"
  8993. " Else \\\n"
  8994. "=======\n"
  8995. " Other \\\n"
  8996. ">>>>>>>\n"
  8997. " End\n"
  8998. "int i;\n"));
  8999. }
  9000. TEST_F(FormatTest, DisableRegions) {
  9001. EXPECT_EQ("int i;\n"
  9002. "// clang-format off\n"
  9003. " int j;\n"
  9004. "// clang-format on\n"
  9005. "int k;",
  9006. format(" int i;\n"
  9007. " // clang-format off\n"
  9008. " int j;\n"
  9009. " // clang-format on\n"
  9010. " int k;"));
  9011. EXPECT_EQ("int i;\n"
  9012. "/* clang-format off */\n"
  9013. " int j;\n"
  9014. "/* clang-format on */\n"
  9015. "int k;",
  9016. format(" int i;\n"
  9017. " /* clang-format off */\n"
  9018. " int j;\n"
  9019. " /* clang-format on */\n"
  9020. " int k;"));
  9021. }
  9022. TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
  9023. format("? ) =");
  9024. }
  9025. } // end namespace tooling
  9026. } // end namespace clang