FormatTest.cpp 418 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144
  1. //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "FormatTestUtils.h"
  10. #include "clang/Format/Format.h"
  11. #include "llvm/Support/Debug.h"
  12. #include "gtest/gtest.h"
  13. #define DEBUG_TYPE "format-test"
  14. namespace clang {
  15. namespace format {
  16. namespace {
  17. FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
  18. class FormatTest : public ::testing::Test {
  19. protected:
  20. enum IncompleteCheck {
  21. IC_ExpectComplete,
  22. IC_ExpectIncomplete,
  23. IC_DoNotCheck
  24. };
  25. std::string format(llvm::StringRef Code,
  26. const FormatStyle &Style = getLLVMStyle(),
  27. IncompleteCheck CheckIncomplete = IC_ExpectComplete) {
  28. DEBUG(llvm::errs() << "---\n");
  29. DEBUG(llvm::errs() << Code << "\n\n");
  30. std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
  31. bool IncompleteFormat = false;
  32. tooling::Replacements Replaces =
  33. reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
  34. if (CheckIncomplete != IC_DoNotCheck) {
  35. bool ExpectedIncompleteFormat = CheckIncomplete == IC_ExpectIncomplete;
  36. EXPECT_EQ(ExpectedIncompleteFormat, IncompleteFormat) << Code << "\n\n";
  37. }
  38. ReplacementCount = Replaces.size();
  39. std::string Result = applyAllReplacements(Code, Replaces);
  40. EXPECT_NE("", Result);
  41. DEBUG(llvm::errs() << "\n" << Result << "\n\n");
  42. return Result;
  43. }
  44. FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
  45. FormatStyle Style = getLLVMStyle();
  46. Style.ColumnLimit = ColumnLimit;
  47. return Style;
  48. }
  49. FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
  50. FormatStyle Style = getGoogleStyle();
  51. Style.ColumnLimit = ColumnLimit;
  52. return Style;
  53. }
  54. void verifyFormat(llvm::StringRef Code,
  55. const FormatStyle &Style = getLLVMStyle()) {
  56. EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
  57. }
  58. void verifyIncompleteFormat(llvm::StringRef Code,
  59. const FormatStyle &Style = getLLVMStyle()) {
  60. EXPECT_EQ(Code.str(),
  61. format(test::messUp(Code), Style, IC_ExpectIncomplete));
  62. }
  63. void verifyGoogleFormat(llvm::StringRef Code) {
  64. verifyFormat(Code, getGoogleStyle());
  65. }
  66. void verifyIndependentOfContext(llvm::StringRef text) {
  67. verifyFormat(text);
  68. verifyFormat(llvm::Twine("void f() { " + text + " }").str());
  69. }
  70. /// \brief Verify that clang-format does not crash on the given input.
  71. void verifyNoCrash(llvm::StringRef Code,
  72. const FormatStyle &Style = getLLVMStyle()) {
  73. format(Code, Style, IC_DoNotCheck);
  74. }
  75. int ReplacementCount;
  76. };
  77. TEST_F(FormatTest, MessUp) {
  78. EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
  79. EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
  80. EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
  81. EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
  82. EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
  83. }
  84. //===----------------------------------------------------------------------===//
  85. // Basic function tests.
  86. //===----------------------------------------------------------------------===//
  87. TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
  88. EXPECT_EQ(";", format(";"));
  89. }
  90. TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
  91. EXPECT_EQ("int i;", format(" int i;"));
  92. EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;"));
  93. EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
  94. EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
  95. }
  96. TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
  97. EXPECT_EQ("int i;", format("int\ni;"));
  98. }
  99. TEST_F(FormatTest, FormatsNestedBlockStatements) {
  100. EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
  101. }
  102. TEST_F(FormatTest, FormatsNestedCall) {
  103. verifyFormat("Method(f1, f2(f3));");
  104. verifyFormat("Method(f1(f2, f3()));");
  105. verifyFormat("Method(f1(f2, (f3())));");
  106. }
  107. TEST_F(FormatTest, NestedNameSpecifiers) {
  108. verifyFormat("vector<::Type> v;");
  109. verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
  110. verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
  111. verifyFormat("bool a = 2 < ::SomeFunction();");
  112. }
  113. TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
  114. EXPECT_EQ("if (a) {\n"
  115. " f();\n"
  116. "}",
  117. format("if(a){f();}"));
  118. EXPECT_EQ(4, ReplacementCount);
  119. EXPECT_EQ("if (a) {\n"
  120. " f();\n"
  121. "}",
  122. format("if (a) {\n"
  123. " f();\n"
  124. "}"));
  125. EXPECT_EQ(0, ReplacementCount);
  126. EXPECT_EQ("/*\r\n"
  127. "\r\n"
  128. "*/\r\n",
  129. format("/*\r\n"
  130. "\r\n"
  131. "*/\r\n"));
  132. EXPECT_EQ(0, ReplacementCount);
  133. }
  134. TEST_F(FormatTest, RemovesEmptyLines) {
  135. EXPECT_EQ("class C {\n"
  136. " int i;\n"
  137. "};",
  138. format("class C {\n"
  139. " int i;\n"
  140. "\n"
  141. "};"));
  142. // Don't remove empty lines at the start of namespaces or extern "C" blocks.
  143. EXPECT_EQ("namespace N {\n"
  144. "\n"
  145. "int i;\n"
  146. "}",
  147. format("namespace N {\n"
  148. "\n"
  149. "int i;\n"
  150. "}",
  151. getGoogleStyle()));
  152. EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
  153. "\n"
  154. "int i;\n"
  155. "}",
  156. format("extern /**/ \"C\" /**/ {\n"
  157. "\n"
  158. "int i;\n"
  159. "}",
  160. getGoogleStyle()));
  161. // ...but do keep inlining and removing empty lines for non-block extern "C"
  162. // functions.
  163. verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
  164. EXPECT_EQ("extern \"C\" int f() {\n"
  165. " int i = 42;\n"
  166. " return i;\n"
  167. "}",
  168. format("extern \"C\" int f() {\n"
  169. "\n"
  170. " int i = 42;\n"
  171. " return i;\n"
  172. "}",
  173. getGoogleStyle()));
  174. // Remove empty lines at the beginning and end of blocks.
  175. EXPECT_EQ("void f() {\n"
  176. "\n"
  177. " if (a) {\n"
  178. "\n"
  179. " f();\n"
  180. " }\n"
  181. "}",
  182. format("void f() {\n"
  183. "\n"
  184. " if (a) {\n"
  185. "\n"
  186. " f();\n"
  187. "\n"
  188. " }\n"
  189. "\n"
  190. "}",
  191. getLLVMStyle()));
  192. EXPECT_EQ("void f() {\n"
  193. " if (a) {\n"
  194. " f();\n"
  195. " }\n"
  196. "}",
  197. format("void f() {\n"
  198. "\n"
  199. " if (a) {\n"
  200. "\n"
  201. " f();\n"
  202. "\n"
  203. " }\n"
  204. "\n"
  205. "}",
  206. getGoogleStyle()));
  207. // Don't remove empty lines in more complex control statements.
  208. EXPECT_EQ("void f() {\n"
  209. " if (a) {\n"
  210. " f();\n"
  211. "\n"
  212. " } else if (b) {\n"
  213. " f();\n"
  214. " }\n"
  215. "}",
  216. format("void f() {\n"
  217. " if (a) {\n"
  218. " f();\n"
  219. "\n"
  220. " } else if (b) {\n"
  221. " f();\n"
  222. "\n"
  223. " }\n"
  224. "\n"
  225. "}"));
  226. // FIXME: This is slightly inconsistent.
  227. EXPECT_EQ("namespace {\n"
  228. "int i;\n"
  229. "}",
  230. format("namespace {\n"
  231. "int i;\n"
  232. "\n"
  233. "}"));
  234. EXPECT_EQ("namespace {\n"
  235. "int i;\n"
  236. "\n"
  237. "} // namespace",
  238. format("namespace {\n"
  239. "int i;\n"
  240. "\n"
  241. "} // namespace"));
  242. }
  243. TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
  244. verifyFormat("x = (a) and (b);");
  245. verifyFormat("x = (a) or (b);");
  246. verifyFormat("x = (a) bitand (b);");
  247. verifyFormat("x = (a) bitor (b);");
  248. verifyFormat("x = (a) not_eq (b);");
  249. verifyFormat("x = (a) and_eq (b);");
  250. verifyFormat("x = (a) or_eq (b);");
  251. verifyFormat("x = (a) xor (b);");
  252. }
  253. //===----------------------------------------------------------------------===//
  254. // Tests for control statements.
  255. //===----------------------------------------------------------------------===//
  256. TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
  257. verifyFormat("if (true)\n f();\ng();");
  258. verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
  259. verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
  260. FormatStyle AllowsMergedIf = getLLVMStyle();
  261. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  262. verifyFormat("if (a)\n"
  263. " // comment\n"
  264. " f();",
  265. AllowsMergedIf);
  266. verifyFormat("if (a)\n"
  267. " ;",
  268. AllowsMergedIf);
  269. verifyFormat("if (a)\n"
  270. " if (b) return;",
  271. AllowsMergedIf);
  272. verifyFormat("if (a) // Can't merge this\n"
  273. " f();\n",
  274. AllowsMergedIf);
  275. verifyFormat("if (a) /* still don't merge */\n"
  276. " f();",
  277. AllowsMergedIf);
  278. verifyFormat("if (a) { // Never merge this\n"
  279. " f();\n"
  280. "}",
  281. AllowsMergedIf);
  282. verifyFormat("if (a) { /* Never merge this */\n"
  283. " f();\n"
  284. "}",
  285. AllowsMergedIf);
  286. AllowsMergedIf.ColumnLimit = 14;
  287. verifyFormat("if (a) return;", AllowsMergedIf);
  288. verifyFormat("if (aaaaaaaaa)\n"
  289. " return;",
  290. AllowsMergedIf);
  291. AllowsMergedIf.ColumnLimit = 13;
  292. verifyFormat("if (a)\n return;", AllowsMergedIf);
  293. }
  294. TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
  295. FormatStyle AllowsMergedLoops = getLLVMStyle();
  296. AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
  297. verifyFormat("while (true) continue;", AllowsMergedLoops);
  298. verifyFormat("for (;;) continue;", AllowsMergedLoops);
  299. verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
  300. verifyFormat("while (true)\n"
  301. " ;",
  302. AllowsMergedLoops);
  303. verifyFormat("for (;;)\n"
  304. " ;",
  305. AllowsMergedLoops);
  306. verifyFormat("for (;;)\n"
  307. " for (;;) continue;",
  308. AllowsMergedLoops);
  309. verifyFormat("for (;;) // Can't merge this\n"
  310. " continue;",
  311. AllowsMergedLoops);
  312. verifyFormat("for (;;) /* still don't merge */\n"
  313. " continue;",
  314. AllowsMergedLoops);
  315. }
  316. TEST_F(FormatTest, FormatShortBracedStatements) {
  317. FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
  318. AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
  319. AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
  320. AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
  321. verifyFormat("if (true) {}", AllowSimpleBracedStatements);
  322. verifyFormat("while (true) {}", AllowSimpleBracedStatements);
  323. verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
  324. verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
  325. verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
  326. verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
  327. verifyFormat("if (true) { //\n"
  328. " f();\n"
  329. "}",
  330. AllowSimpleBracedStatements);
  331. verifyFormat("if (true) {\n"
  332. " f();\n"
  333. " f();\n"
  334. "}",
  335. AllowSimpleBracedStatements);
  336. verifyFormat("if (true) {\n"
  337. " f();\n"
  338. "} else {\n"
  339. " f();\n"
  340. "}",
  341. AllowSimpleBracedStatements);
  342. verifyFormat("template <int> struct A2 {\n"
  343. " struct B {};\n"
  344. "};",
  345. AllowSimpleBracedStatements);
  346. AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
  347. verifyFormat("if (true) {\n"
  348. " f();\n"
  349. "}",
  350. AllowSimpleBracedStatements);
  351. verifyFormat("if (true) {\n"
  352. " f();\n"
  353. "} else {\n"
  354. " f();\n"
  355. "}",
  356. AllowSimpleBracedStatements);
  357. AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
  358. verifyFormat("while (true) {\n"
  359. " f();\n"
  360. "}",
  361. AllowSimpleBracedStatements);
  362. verifyFormat("for (;;) {\n"
  363. " f();\n"
  364. "}",
  365. AllowSimpleBracedStatements);
  366. }
  367. TEST_F(FormatTest, ParseIfElse) {
  368. verifyFormat("if (true)\n"
  369. " if (true)\n"
  370. " if (true)\n"
  371. " f();\n"
  372. " else\n"
  373. " g();\n"
  374. " else\n"
  375. " h();\n"
  376. "else\n"
  377. " i();");
  378. verifyFormat("if (true)\n"
  379. " if (true)\n"
  380. " if (true) {\n"
  381. " if (true)\n"
  382. " f();\n"
  383. " } else {\n"
  384. " g();\n"
  385. " }\n"
  386. " else\n"
  387. " h();\n"
  388. "else {\n"
  389. " i();\n"
  390. "}");
  391. verifyFormat("void f() {\n"
  392. " if (a) {\n"
  393. " } else {\n"
  394. " }\n"
  395. "}");
  396. }
  397. TEST_F(FormatTest, ElseIf) {
  398. verifyFormat("if (a) {\n} else if (b) {\n}");
  399. verifyFormat("if (a)\n"
  400. " f();\n"
  401. "else if (b)\n"
  402. " g();\n"
  403. "else\n"
  404. " h();");
  405. verifyFormat("if (a) {\n"
  406. " f();\n"
  407. "}\n"
  408. "// or else ..\n"
  409. "else {\n"
  410. " g()\n"
  411. "}");
  412. verifyFormat("if (a) {\n"
  413. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  414. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  415. "}");
  416. verifyFormat("if (a) {\n"
  417. "} else if (\n"
  418. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  419. "}",
  420. getLLVMStyleWithColumns(62));
  421. }
  422. TEST_F(FormatTest, FormatsForLoop) {
  423. verifyFormat(
  424. "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
  425. " ++VeryVeryLongLoopVariable)\n"
  426. " ;");
  427. verifyFormat("for (;;)\n"
  428. " f();");
  429. verifyFormat("for (;;) {\n}");
  430. verifyFormat("for (;;) {\n"
  431. " f();\n"
  432. "}");
  433. verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
  434. verifyFormat(
  435. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  436. " E = UnwrappedLines.end();\n"
  437. " I != E; ++I) {\n}");
  438. verifyFormat(
  439. "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
  440. " ++IIIII) {\n}");
  441. verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
  442. " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
  443. " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
  444. verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
  445. " I = FD->getDeclsInPrototypeScope().begin(),\n"
  446. " E = FD->getDeclsInPrototypeScope().end();\n"
  447. " I != E; ++I) {\n}");
  448. verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
  449. " I = Container.begin(),\n"
  450. " E = Container.end();\n"
  451. " I != E; ++I) {\n}",
  452. getLLVMStyleWithColumns(76));
  453. verifyFormat(
  454. "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  455. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
  456. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  457. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  458. " ++aaaaaaaaaaa) {\n}");
  459. verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  460. " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
  461. " ++i) {\n}");
  462. verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
  463. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  464. "}");
  465. verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
  466. " aaaaaaaaaa);\n"
  467. " iter; ++iter) {\n"
  468. "}");
  469. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  470. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  471. " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
  472. " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
  473. FormatStyle NoBinPacking = getLLVMStyle();
  474. NoBinPacking.BinPackParameters = false;
  475. verifyFormat("for (int aaaaaaaaaaa = 1;\n"
  476. " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
  477. " aaaaaaaaaaaaaaaa,\n"
  478. " aaaaaaaaaaaaaaaa,\n"
  479. " aaaaaaaaaaaaaaaa);\n"
  480. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  481. "}",
  482. NoBinPacking);
  483. verifyFormat(
  484. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  485. " E = UnwrappedLines.end();\n"
  486. " I != E;\n"
  487. " ++I) {\n}",
  488. NoBinPacking);
  489. }
  490. TEST_F(FormatTest, RangeBasedForLoops) {
  491. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  492. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  493. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
  494. " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
  495. verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
  496. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  497. verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
  498. " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
  499. }
  500. TEST_F(FormatTest, ForEachLoops) {
  501. verifyFormat("void f() {\n"
  502. " foreach (Item *item, itemlist) {}\n"
  503. " Q_FOREACH (Item *item, itemlist) {}\n"
  504. " BOOST_FOREACH (Item *item, itemlist) {}\n"
  505. " UNKNOWN_FORACH(Item * item, itemlist) {}\n"
  506. "}");
  507. // As function-like macros.
  508. verifyFormat("#define foreach(x, y)\n"
  509. "#define Q_FOREACH(x, y)\n"
  510. "#define BOOST_FOREACH(x, y)\n"
  511. "#define UNKNOWN_FOREACH(x, y)\n");
  512. // Not as function-like macros.
  513. verifyFormat("#define foreach (x, y)\n"
  514. "#define Q_FOREACH (x, y)\n"
  515. "#define BOOST_FOREACH (x, y)\n"
  516. "#define UNKNOWN_FOREACH (x, y)\n");
  517. }
  518. TEST_F(FormatTest, FormatsWhileLoop) {
  519. verifyFormat("while (true) {\n}");
  520. verifyFormat("while (true)\n"
  521. " f();");
  522. verifyFormat("while () {\n}");
  523. verifyFormat("while () {\n"
  524. " f();\n"
  525. "}");
  526. }
  527. TEST_F(FormatTest, FormatsDoWhile) {
  528. verifyFormat("do {\n"
  529. " do_something();\n"
  530. "} while (something());");
  531. verifyFormat("do\n"
  532. " do_something();\n"
  533. "while (something());");
  534. }
  535. TEST_F(FormatTest, FormatsSwitchStatement) {
  536. verifyFormat("switch (x) {\n"
  537. "case 1:\n"
  538. " f();\n"
  539. " break;\n"
  540. "case kFoo:\n"
  541. "case ns::kBar:\n"
  542. "case kBaz:\n"
  543. " break;\n"
  544. "default:\n"
  545. " g();\n"
  546. " break;\n"
  547. "}");
  548. verifyFormat("switch (x) {\n"
  549. "case 1: {\n"
  550. " f();\n"
  551. " break;\n"
  552. "}\n"
  553. "case 2: {\n"
  554. " break;\n"
  555. "}\n"
  556. "}");
  557. verifyFormat("switch (x) {\n"
  558. "case 1: {\n"
  559. " f();\n"
  560. " {\n"
  561. " g();\n"
  562. " h();\n"
  563. " }\n"
  564. " break;\n"
  565. "}\n"
  566. "}");
  567. verifyFormat("switch (x) {\n"
  568. "case 1: {\n"
  569. " f();\n"
  570. " if (foo) {\n"
  571. " g();\n"
  572. " h();\n"
  573. " }\n"
  574. " break;\n"
  575. "}\n"
  576. "}");
  577. verifyFormat("switch (x) {\n"
  578. "case 1: {\n"
  579. " f();\n"
  580. " g();\n"
  581. "} break;\n"
  582. "}");
  583. verifyFormat("switch (test)\n"
  584. " ;");
  585. verifyFormat("switch (x) {\n"
  586. "default: {\n"
  587. " // Do nothing.\n"
  588. "}\n"
  589. "}");
  590. verifyFormat("switch (x) {\n"
  591. "// comment\n"
  592. "// if 1, do f()\n"
  593. "case 1:\n"
  594. " f();\n"
  595. "}");
  596. verifyFormat("switch (x) {\n"
  597. "case 1:\n"
  598. " // Do amazing stuff\n"
  599. " {\n"
  600. " f();\n"
  601. " g();\n"
  602. " }\n"
  603. " break;\n"
  604. "}");
  605. verifyFormat("#define A \\\n"
  606. " switch (x) { \\\n"
  607. " case a: \\\n"
  608. " foo = b; \\\n"
  609. " }",
  610. getLLVMStyleWithColumns(20));
  611. verifyFormat("#define OPERATION_CASE(name) \\\n"
  612. " case OP_name: \\\n"
  613. " return operations::Operation##name\n",
  614. getLLVMStyleWithColumns(40));
  615. verifyFormat("switch (x) {\n"
  616. "case 1:;\n"
  617. "default:;\n"
  618. " int i;\n"
  619. "}");
  620. verifyGoogleFormat("switch (x) {\n"
  621. " case 1:\n"
  622. " f();\n"
  623. " break;\n"
  624. " case kFoo:\n"
  625. " case ns::kBar:\n"
  626. " case kBaz:\n"
  627. " break;\n"
  628. " default:\n"
  629. " g();\n"
  630. " break;\n"
  631. "}");
  632. verifyGoogleFormat("switch (x) {\n"
  633. " case 1: {\n"
  634. " f();\n"
  635. " break;\n"
  636. " }\n"
  637. "}");
  638. verifyGoogleFormat("switch (test)\n"
  639. " ;");
  640. verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
  641. " case OP_name: \\\n"
  642. " return operations::Operation##name\n");
  643. verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
  644. " // Get the correction operation class.\n"
  645. " switch (OpCode) {\n"
  646. " CASE(Add);\n"
  647. " CASE(Subtract);\n"
  648. " default:\n"
  649. " return operations::Unknown;\n"
  650. " }\n"
  651. "#undef OPERATION_CASE\n"
  652. "}");
  653. verifyFormat("DEBUG({\n"
  654. " switch (x) {\n"
  655. " case A:\n"
  656. " f();\n"
  657. " break;\n"
  658. " // On B:\n"
  659. " case B:\n"
  660. " g();\n"
  661. " break;\n"
  662. " }\n"
  663. "});");
  664. verifyFormat("switch (a) {\n"
  665. "case (b):\n"
  666. " return;\n"
  667. "}");
  668. verifyFormat("switch (a) {\n"
  669. "case some_namespace::\n"
  670. " some_constant:\n"
  671. " return;\n"
  672. "}",
  673. getLLVMStyleWithColumns(34));
  674. }
  675. TEST_F(FormatTest, CaseRanges) {
  676. verifyFormat("switch (x) {\n"
  677. "case 'A' ... 'Z':\n"
  678. "case 1 ... 5:\n"
  679. " break;\n"
  680. "}");
  681. }
  682. TEST_F(FormatTest, ShortCaseLabels) {
  683. FormatStyle Style = getLLVMStyle();
  684. Style.AllowShortCaseLabelsOnASingleLine = true;
  685. verifyFormat("switch (a) {\n"
  686. "case 1: x = 1; break;\n"
  687. "case 2: return;\n"
  688. "case 3:\n"
  689. "case 4:\n"
  690. "case 5: return;\n"
  691. "case 6: // comment\n"
  692. " return;\n"
  693. "case 7:\n"
  694. " // comment\n"
  695. " return;\n"
  696. "case 8:\n"
  697. " x = 8; // comment\n"
  698. " break;\n"
  699. "default: y = 1; break;\n"
  700. "}",
  701. Style);
  702. verifyFormat("switch (a) {\n"
  703. "#if FOO\n"
  704. "case 0: return 0;\n"
  705. "#endif\n"
  706. "}",
  707. Style);
  708. verifyFormat("switch (a) {\n"
  709. "case 1: {\n"
  710. "}\n"
  711. "case 2: {\n"
  712. " return;\n"
  713. "}\n"
  714. "case 3: {\n"
  715. " x = 1;\n"
  716. " return;\n"
  717. "}\n"
  718. "case 4:\n"
  719. " if (x)\n"
  720. " return;\n"
  721. "}",
  722. Style);
  723. Style.ColumnLimit = 21;
  724. verifyFormat("switch (a) {\n"
  725. "case 1: x = 1; break;\n"
  726. "case 2: return;\n"
  727. "case 3:\n"
  728. "case 4:\n"
  729. "case 5: return;\n"
  730. "default:\n"
  731. " y = 1;\n"
  732. " break;\n"
  733. "}",
  734. Style);
  735. }
  736. TEST_F(FormatTest, FormatsLabels) {
  737. verifyFormat("void f() {\n"
  738. " some_code();\n"
  739. "test_label:\n"
  740. " some_other_code();\n"
  741. " {\n"
  742. " some_more_code();\n"
  743. " another_label:\n"
  744. " some_more_code();\n"
  745. " }\n"
  746. "}");
  747. verifyFormat("{\n"
  748. " some_code();\n"
  749. "test_label:\n"
  750. " some_other_code();\n"
  751. "}");
  752. verifyFormat("{\n"
  753. " some_code();\n"
  754. "test_label:;\n"
  755. " int i = 0;\n"
  756. "}");
  757. }
  758. //===----------------------------------------------------------------------===//
  759. // Tests for comments.
  760. //===----------------------------------------------------------------------===//
  761. TEST_F(FormatTest, UnderstandsSingleLineComments) {
  762. verifyFormat("//* */");
  763. verifyFormat("// line 1\n"
  764. "// line 2\n"
  765. "void f() {}\n");
  766. verifyFormat("void f() {\n"
  767. " // Doesn't do anything\n"
  768. "}");
  769. verifyFormat("SomeObject\n"
  770. " // Calling someFunction on SomeObject\n"
  771. " .someFunction();");
  772. verifyFormat("auto result = SomeObject\n"
  773. " // Calling someFunction on SomeObject\n"
  774. " .someFunction();");
  775. verifyFormat("void f(int i, // some comment (probably for i)\n"
  776. " int j, // some comment (probably for j)\n"
  777. " int k); // some comment (probably for k)");
  778. verifyFormat("void f(int i,\n"
  779. " // some comment (probably for j)\n"
  780. " int j,\n"
  781. " // some comment (probably for k)\n"
  782. " int k);");
  783. verifyFormat("int i // This is a fancy variable\n"
  784. " = 5; // with nicely aligned comment.");
  785. verifyFormat("// Leading comment.\n"
  786. "int a; // Trailing comment.");
  787. verifyFormat("int a; // Trailing comment\n"
  788. " // on 2\n"
  789. " // or 3 lines.\n"
  790. "int b;");
  791. verifyFormat("int a; // Trailing comment\n"
  792. "\n"
  793. "// Leading comment.\n"
  794. "int b;");
  795. verifyFormat("int a; // Comment.\n"
  796. " // More details.\n"
  797. "int bbbb; // Another comment.");
  798. verifyFormat(
  799. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  800. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n"
  801. "int cccccccccccccccccccccccccccccc; // comment\n"
  802. "int ddd; // looooooooooooooooooooooooong comment\n"
  803. "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  804. "int bbbbbbbbbbbbbbbbbbbbb; // comment\n"
  805. "int ccccccccccccccccccc; // comment");
  806. verifyFormat("#include \"a\" // comment\n"
  807. "#include \"a/b/c\" // comment");
  808. verifyFormat("#include <a> // comment\n"
  809. "#include <a/b/c> // comment");
  810. EXPECT_EQ("#include \"a\" // comment\n"
  811. "#include \"a/b/c\" // comment",
  812. format("#include \\\n"
  813. " \"a\" // comment\n"
  814. "#include \"a/b/c\" // comment"));
  815. verifyFormat("enum E {\n"
  816. " // comment\n"
  817. " VAL_A, // comment\n"
  818. " VAL_B\n"
  819. "};");
  820. verifyFormat(
  821. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  822. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
  823. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  824. " // Comment inside a statement.\n"
  825. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  826. verifyFormat("SomeFunction(a,\n"
  827. " // comment\n"
  828. " b + x);");
  829. verifyFormat("SomeFunction(a, a,\n"
  830. " // comment\n"
  831. " b + x);");
  832. verifyFormat(
  833. "bool aaaaaaaaaaaaa = // comment\n"
  834. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  835. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  836. verifyFormat("int aaaa; // aaaaa\n"
  837. "int aa; // aaaaaaa",
  838. getLLVMStyleWithColumns(20));
  839. EXPECT_EQ("void f() { // This does something ..\n"
  840. "}\n"
  841. "int a; // This is unrelated",
  842. format("void f() { // This does something ..\n"
  843. " }\n"
  844. "int a; // This is unrelated"));
  845. EXPECT_EQ("class C {\n"
  846. " void f() { // This does something ..\n"
  847. " } // awesome..\n"
  848. "\n"
  849. " int a; // This is unrelated\n"
  850. "};",
  851. format("class C{void f() { // This does something ..\n"
  852. " } // awesome..\n"
  853. " \n"
  854. "int a; // This is unrelated\n"
  855. "};"));
  856. EXPECT_EQ("int i; // single line trailing comment",
  857. format("int i;\\\n// single line trailing comment"));
  858. verifyGoogleFormat("int a; // Trailing comment.");
  859. verifyFormat("someFunction(anotherFunction( // Force break.\n"
  860. " parameter));");
  861. verifyGoogleFormat("#endif // HEADER_GUARD");
  862. verifyFormat("const char *test[] = {\n"
  863. " // A\n"
  864. " \"aaaa\",\n"
  865. " // B\n"
  866. " \"aaaaa\"};");
  867. verifyGoogleFormat(
  868. "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  869. " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment");
  870. EXPECT_EQ("D(a, {\n"
  871. " // test\n"
  872. " int a;\n"
  873. "});",
  874. format("D(a, {\n"
  875. "// test\n"
  876. "int a;\n"
  877. "});"));
  878. EXPECT_EQ("lineWith(); // comment\n"
  879. "// at start\n"
  880. "otherLine();",
  881. format("lineWith(); // comment\n"
  882. "// at start\n"
  883. "otherLine();"));
  884. EXPECT_EQ("lineWith(); // comment\n"
  885. "/*\n"
  886. " * at start */\n"
  887. "otherLine();",
  888. format("lineWith(); // comment\n"
  889. "/*\n"
  890. " * at start */\n"
  891. "otherLine();"));
  892. EXPECT_EQ("lineWith(); // comment\n"
  893. " // at start\n"
  894. "otherLine();",
  895. format("lineWith(); // comment\n"
  896. " // at start\n"
  897. "otherLine();"));
  898. EXPECT_EQ("lineWith(); // comment\n"
  899. "// at start\n"
  900. "otherLine(); // comment",
  901. format("lineWith(); // comment\n"
  902. "// at start\n"
  903. "otherLine(); // comment"));
  904. EXPECT_EQ("lineWith();\n"
  905. "// at start\n"
  906. "otherLine(); // comment",
  907. format("lineWith();\n"
  908. " // at start\n"
  909. "otherLine(); // comment"));
  910. EXPECT_EQ("// first\n"
  911. "// at start\n"
  912. "otherLine(); // comment",
  913. format("// first\n"
  914. " // at start\n"
  915. "otherLine(); // comment"));
  916. EXPECT_EQ("f();\n"
  917. "// first\n"
  918. "// at start\n"
  919. "otherLine(); // comment",
  920. format("f();\n"
  921. "// first\n"
  922. " // at start\n"
  923. "otherLine(); // comment"));
  924. verifyFormat("f(); // comment\n"
  925. "// first\n"
  926. "// at start\n"
  927. "otherLine();");
  928. EXPECT_EQ("f(); // comment\n"
  929. "// first\n"
  930. "// at start\n"
  931. "otherLine();",
  932. format("f(); // comment\n"
  933. "// first\n"
  934. " // at start\n"
  935. "otherLine();"));
  936. EXPECT_EQ("f(); // comment\n"
  937. " // first\n"
  938. "// at start\n"
  939. "otherLine();",
  940. format("f(); // comment\n"
  941. " // first\n"
  942. "// at start\n"
  943. "otherLine();"));
  944. EXPECT_EQ("void f() {\n"
  945. " lineWith(); // comment\n"
  946. " // at start\n"
  947. "}",
  948. format("void f() {\n"
  949. " lineWith(); // comment\n"
  950. " // at start\n"
  951. "}"));
  952. EXPECT_EQ("int xy; // a\n"
  953. "int z; // b",
  954. format("int xy; // a\n"
  955. "int z; //b"));
  956. EXPECT_EQ("int xy; // a\n"
  957. "int z; // bb",
  958. format("int xy; // a\n"
  959. "int z; //bb",
  960. getLLVMStyleWithColumns(12)));
  961. verifyFormat("#define A \\\n"
  962. " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
  963. " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
  964. getLLVMStyleWithColumns(60));
  965. verifyFormat(
  966. "#define A \\\n"
  967. " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
  968. " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
  969. getLLVMStyleWithColumns(61));
  970. verifyFormat("if ( // This is some comment\n"
  971. " x + 3) {\n"
  972. "}");
  973. EXPECT_EQ("if ( // This is some comment\n"
  974. " // spanning two lines\n"
  975. " x + 3) {\n"
  976. "}",
  977. format("if( // This is some comment\n"
  978. " // spanning two lines\n"
  979. " x + 3) {\n"
  980. "}"));
  981. verifyNoCrash("/\\\n/");
  982. verifyNoCrash("/\\\n* */");
  983. // The 0-character somehow makes the lexer return a proper comment.
  984. verifyNoCrash(StringRef("/*\\\0\n/", 6));
  985. }
  986. TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
  987. EXPECT_EQ("SomeFunction(a,\n"
  988. " b, // comment\n"
  989. " c);",
  990. format("SomeFunction(a,\n"
  991. " b, // comment\n"
  992. " c);"));
  993. EXPECT_EQ("SomeFunction(a, b,\n"
  994. " // comment\n"
  995. " c);",
  996. format("SomeFunction(a,\n"
  997. " b,\n"
  998. " // comment\n"
  999. " c);"));
  1000. EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
  1001. " c);",
  1002. format("SomeFunction(a, b, // comment (unclear relation)\n"
  1003. " c);"));
  1004. EXPECT_EQ("SomeFunction(a, // comment\n"
  1005. " b,\n"
  1006. " c); // comment",
  1007. format("SomeFunction(a, // comment\n"
  1008. " b,\n"
  1009. " c); // comment"));
  1010. }
  1011. TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
  1012. EXPECT_EQ("// comment", format("// comment "));
  1013. EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
  1014. format("int aaaaaaa, bbbbbbb; // comment ",
  1015. getLLVMStyleWithColumns(33)));
  1016. EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f "));
  1017. EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f "));
  1018. }
  1019. TEST_F(FormatTest, UnderstandsBlockComments) {
  1020. verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
  1021. verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }");
  1022. EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
  1023. " bbbbbbbbbbbbbbbbbbbbbbbbb);",
  1024. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n"
  1025. "/* Trailing comment for aa... */\n"
  1026. " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  1027. EXPECT_EQ(
  1028. "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  1029. " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
  1030. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
  1031. "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  1032. EXPECT_EQ(
  1033. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1034. " aaaaaaaaaaaaaaaaaa,\n"
  1035. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  1036. "}",
  1037. format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1038. " aaaaaaaaaaaaaaaaaa ,\n"
  1039. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  1040. "}"));
  1041. FormatStyle NoBinPacking = getLLVMStyle();
  1042. NoBinPacking.BinPackParameters = false;
  1043. verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
  1044. " /* parameter 2 */ aaaaaa,\n"
  1045. " /* parameter 3 */ aaaaaa,\n"
  1046. " /* parameter 4 */ aaaaaa);",
  1047. NoBinPacking);
  1048. // Aligning block comments in macros.
  1049. verifyGoogleFormat("#define A \\\n"
  1050. " int i; /*a*/ \\\n"
  1051. " int jjj; /*b*/");
  1052. }
  1053. TEST_F(FormatTest, AlignsBlockComments) {
  1054. EXPECT_EQ("/*\n"
  1055. " * Really multi-line\n"
  1056. " * comment.\n"
  1057. " */\n"
  1058. "void f() {}",
  1059. format(" /*\n"
  1060. " * Really multi-line\n"
  1061. " * comment.\n"
  1062. " */\n"
  1063. " void f() {}"));
  1064. EXPECT_EQ("class C {\n"
  1065. " /*\n"
  1066. " * Another multi-line\n"
  1067. " * comment.\n"
  1068. " */\n"
  1069. " void f() {}\n"
  1070. "};",
  1071. format("class C {\n"
  1072. "/*\n"
  1073. " * Another multi-line\n"
  1074. " * comment.\n"
  1075. " */\n"
  1076. "void f() {}\n"
  1077. "};"));
  1078. EXPECT_EQ("/*\n"
  1079. " 1. This is a comment with non-trivial formatting.\n"
  1080. " 1.1. We have to indent/outdent all lines equally\n"
  1081. " 1.1.1. to keep the formatting.\n"
  1082. " */",
  1083. format(" /*\n"
  1084. " 1. This is a comment with non-trivial formatting.\n"
  1085. " 1.1. We have to indent/outdent all lines equally\n"
  1086. " 1.1.1. to keep the formatting.\n"
  1087. " */"));
  1088. EXPECT_EQ("/*\n"
  1089. "Don't try to outdent if there's not enough indentation.\n"
  1090. "*/",
  1091. format(" /*\n"
  1092. " Don't try to outdent if there's not enough indentation.\n"
  1093. " */"));
  1094. EXPECT_EQ("int i; /* Comment with empty...\n"
  1095. " *\n"
  1096. " * line. */",
  1097. format("int i; /* Comment with empty...\n"
  1098. " *\n"
  1099. " * line. */"));
  1100. EXPECT_EQ("int foobar = 0; /* comment */\n"
  1101. "int bar = 0; /* multiline\n"
  1102. " comment 1 */\n"
  1103. "int baz = 0; /* multiline\n"
  1104. " comment 2 */\n"
  1105. "int bzz = 0; /* multiline\n"
  1106. " comment 3 */",
  1107. format("int foobar = 0; /* comment */\n"
  1108. "int bar = 0; /* multiline\n"
  1109. " comment 1 */\n"
  1110. "int baz = 0; /* multiline\n"
  1111. " comment 2 */\n"
  1112. "int bzz = 0; /* multiline\n"
  1113. " comment 3 */"));
  1114. EXPECT_EQ("int foobar = 0; /* comment */\n"
  1115. "int bar = 0; /* multiline\n"
  1116. " comment */\n"
  1117. "int baz = 0; /* multiline\n"
  1118. "comment */",
  1119. format("int foobar = 0; /* comment */\n"
  1120. "int bar = 0; /* multiline\n"
  1121. "comment */\n"
  1122. "int baz = 0; /* multiline\n"
  1123. "comment */"));
  1124. }
  1125. TEST_F(FormatTest, CommentReflowingCanBeTurnedOff) {
  1126. FormatStyle Style = getLLVMStyleWithColumns(20);
  1127. Style.ReflowComments = false;
  1128. verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style);
  1129. verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style);
  1130. }
  1131. TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
  1132. EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1133. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
  1134. format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1135. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
  1136. EXPECT_EQ(
  1137. "void ffffffffffff(\n"
  1138. " int aaaaaaaa, int bbbbbbbb,\n"
  1139. " int cccccccccccc) { /*\n"
  1140. " aaaaaaaaaa\n"
  1141. " aaaaaaaaaaaaa\n"
  1142. " bbbbbbbbbbbbbb\n"
  1143. " bbbbbbbbbb\n"
  1144. " */\n"
  1145. "}",
  1146. format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
  1147. "{ /*\n"
  1148. " aaaaaaaaaa aaaaaaaaaaaaa\n"
  1149. " bbbbbbbbbbbbbb bbbbbbbbbb\n"
  1150. " */\n"
  1151. "}",
  1152. getLLVMStyleWithColumns(40)));
  1153. }
  1154. TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
  1155. EXPECT_EQ("void ffffffffff(\n"
  1156. " int aaaaa /* test */);",
  1157. format("void ffffffffff(int aaaaa /* test */);",
  1158. getLLVMStyleWithColumns(35)));
  1159. }
  1160. TEST_F(FormatTest, SplitsLongCxxComments) {
  1161. EXPECT_EQ("// A comment that\n"
  1162. "// doesn't fit on\n"
  1163. "// one line",
  1164. format("// A comment that doesn't fit on one line",
  1165. getLLVMStyleWithColumns(20)));
  1166. EXPECT_EQ("/// A comment that\n"
  1167. "/// doesn't fit on\n"
  1168. "/// one line",
  1169. format("/// A comment that doesn't fit on one line",
  1170. getLLVMStyleWithColumns(20)));
  1171. EXPECT_EQ("//! A comment that\n"
  1172. "//! doesn't fit on\n"
  1173. "//! one line",
  1174. format("//! A comment that doesn't fit on one line",
  1175. getLLVMStyleWithColumns(20)));
  1176. EXPECT_EQ("// a b c d\n"
  1177. "// e f g\n"
  1178. "// h i j k",
  1179. format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10)));
  1180. EXPECT_EQ(
  1181. "// a b c d\n"
  1182. "// e f g\n"
  1183. "// h i j k",
  1184. format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10)));
  1185. EXPECT_EQ("if (true) // A comment that\n"
  1186. " // doesn't fit on\n"
  1187. " // one line",
  1188. format("if (true) // A comment that doesn't fit on one line ",
  1189. getLLVMStyleWithColumns(30)));
  1190. EXPECT_EQ("// Don't_touch_leading_whitespace",
  1191. format("// Don't_touch_leading_whitespace",
  1192. getLLVMStyleWithColumns(20)));
  1193. EXPECT_EQ("// Add leading\n"
  1194. "// whitespace",
  1195. format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
  1196. EXPECT_EQ("/// Add leading\n"
  1197. "/// whitespace",
  1198. format("///Add leading whitespace", getLLVMStyleWithColumns(20)));
  1199. EXPECT_EQ("//! Add leading\n"
  1200. "//! whitespace",
  1201. format("//!Add leading whitespace", getLLVMStyleWithColumns(20)));
  1202. EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
  1203. EXPECT_EQ("// Even if it makes the line exceed the column\n"
  1204. "// limit",
  1205. format("//Even if it makes the line exceed the column limit",
  1206. getLLVMStyleWithColumns(51)));
  1207. EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
  1208. EXPECT_EQ("// aa bb cc dd",
  1209. format("// aa bb cc dd ",
  1210. getLLVMStyleWithColumns(15)));
  1211. EXPECT_EQ("// A comment before\n"
  1212. "// a macro\n"
  1213. "// definition\n"
  1214. "#define a b",
  1215. format("// A comment before a macro definition\n"
  1216. "#define a b",
  1217. getLLVMStyleWithColumns(20)));
  1218. EXPECT_EQ("void ffffff(\n"
  1219. " int aaaaaaaaa, // wwww\n"
  1220. " int bbbbbbbbbb, // xxxxxxx\n"
  1221. " // yyyyyyyyyy\n"
  1222. " int c, int d, int e) {}",
  1223. format("void ffffff(\n"
  1224. " int aaaaaaaaa, // wwww\n"
  1225. " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
  1226. " int c, int d, int e) {}",
  1227. getLLVMStyleWithColumns(40)));
  1228. EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1229. format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1230. getLLVMStyleWithColumns(20)));
  1231. EXPECT_EQ(
  1232. "#define XXX // a b c d\n"
  1233. " // e f g h",
  1234. format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
  1235. EXPECT_EQ(
  1236. "#define XXX // q w e r\n"
  1237. " // t y u i",
  1238. format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
  1239. }
  1240. TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {
  1241. EXPECT_EQ("// A comment\n"
  1242. "// that doesn't\n"
  1243. "// fit on one\n"
  1244. "// line",
  1245. format("// A comment that doesn't fit on one line",
  1246. getLLVMStyleWithColumns(20)));
  1247. EXPECT_EQ("/// A comment\n"
  1248. "/// that doesn't\n"
  1249. "/// fit on one\n"
  1250. "/// line",
  1251. format("/// A comment that doesn't fit on one line",
  1252. getLLVMStyleWithColumns(20)));
  1253. }
  1254. TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
  1255. EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1256. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1257. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1258. format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1259. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1260. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
  1261. EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1262. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1263. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1264. format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1265. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1266. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1267. getLLVMStyleWithColumns(50)));
  1268. // FIXME: One day we might want to implement adjustment of leading whitespace
  1269. // of the consecutive lines in this kind of comment:
  1270. EXPECT_EQ("double\n"
  1271. " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1272. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1273. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1274. format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1275. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1276. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1277. getLLVMStyleWithColumns(49)));
  1278. }
  1279. TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) {
  1280. FormatStyle Pragmas = getLLVMStyleWithColumns(30);
  1281. Pragmas.CommentPragmas = "^ IWYU pragma:";
  1282. EXPECT_EQ(
  1283. "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
  1284. format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
  1285. EXPECT_EQ(
  1286. "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
  1287. format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
  1288. }
  1289. TEST_F(FormatTest, PriorityOfCommentBreaking) {
  1290. EXPECT_EQ("if (xxx ==\n"
  1291. " yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  1292. " zzz)\n"
  1293. " q();",
  1294. format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  1295. " zzz) q();",
  1296. getLLVMStyleWithColumns(40)));
  1297. EXPECT_EQ("if (xxxxxxxxxx ==\n"
  1298. " yyy && // aaaaaa bbbbbbbb cccc\n"
  1299. " zzz)\n"
  1300. " q();",
  1301. format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
  1302. " zzz) q();",
  1303. getLLVMStyleWithColumns(40)));
  1304. EXPECT_EQ("if (xxxxxxxxxx &&\n"
  1305. " yyy || // aaaaaa bbbbbbbb cccc\n"
  1306. " zzz)\n"
  1307. " q();",
  1308. format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
  1309. " zzz) q();",
  1310. getLLVMStyleWithColumns(40)));
  1311. EXPECT_EQ("fffffffff(\n"
  1312. " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  1313. " zzz);",
  1314. format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  1315. " zzz);",
  1316. getLLVMStyleWithColumns(40)));
  1317. }
  1318. TEST_F(FormatTest, MultiLineCommentsInDefines) {
  1319. EXPECT_EQ("#define A(x) /* \\\n"
  1320. " a comment \\\n"
  1321. " inside */ \\\n"
  1322. " f();",
  1323. format("#define A(x) /* \\\n"
  1324. " a comment \\\n"
  1325. " inside */ \\\n"
  1326. " f();",
  1327. getLLVMStyleWithColumns(17)));
  1328. EXPECT_EQ("#define A( \\\n"
  1329. " x) /* \\\n"
  1330. " a comment \\\n"
  1331. " inside */ \\\n"
  1332. " f();",
  1333. format("#define A( \\\n"
  1334. " x) /* \\\n"
  1335. " a comment \\\n"
  1336. " inside */ \\\n"
  1337. " f();",
  1338. getLLVMStyleWithColumns(17)));
  1339. }
  1340. TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
  1341. EXPECT_EQ("namespace {}\n// Test\n#define A",
  1342. format("namespace {}\n // Test\n#define A"));
  1343. EXPECT_EQ("namespace {}\n/* Test */\n#define A",
  1344. format("namespace {}\n /* Test */\n#define A"));
  1345. EXPECT_EQ("namespace {}\n/* Test */ #define A",
  1346. format("namespace {}\n /* Test */ #define A"));
  1347. }
  1348. TEST_F(FormatTest, SplitsLongLinesInComments) {
  1349. EXPECT_EQ("/* This is a long\n"
  1350. " * comment that\n"
  1351. " * doesn't\n"
  1352. " * fit on one line.\n"
  1353. " */",
  1354. format("/* "
  1355. "This is a long "
  1356. "comment that "
  1357. "doesn't "
  1358. "fit on one line. */",
  1359. getLLVMStyleWithColumns(20)));
  1360. EXPECT_EQ(
  1361. "/* a b c d\n"
  1362. " * e f g\n"
  1363. " * h i j k\n"
  1364. " */",
  1365. format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
  1366. EXPECT_EQ(
  1367. "/* a b c d\n"
  1368. " * e f g\n"
  1369. " * h i j k\n"
  1370. " */",
  1371. format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
  1372. EXPECT_EQ("/*\n"
  1373. "This is a long\n"
  1374. "comment that doesn't\n"
  1375. "fit on one line.\n"
  1376. "*/",
  1377. format("/*\n"
  1378. "This is a long "
  1379. "comment that doesn't "
  1380. "fit on one line. \n"
  1381. "*/",
  1382. getLLVMStyleWithColumns(20)));
  1383. EXPECT_EQ("/*\n"
  1384. " * This is a long\n"
  1385. " * comment that\n"
  1386. " * doesn't fit on\n"
  1387. " * one line.\n"
  1388. " */",
  1389. format("/* \n"
  1390. " * This is a long "
  1391. " comment that "
  1392. " doesn't fit on "
  1393. " one line. \n"
  1394. " */",
  1395. getLLVMStyleWithColumns(20)));
  1396. EXPECT_EQ("/*\n"
  1397. " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
  1398. " * so_it_should_be_broken\n"
  1399. " * wherever_a_space_occurs\n"
  1400. " */",
  1401. format("/*\n"
  1402. " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
  1403. " so_it_should_be_broken "
  1404. " wherever_a_space_occurs \n"
  1405. " */",
  1406. getLLVMStyleWithColumns(20)));
  1407. EXPECT_EQ("/*\n"
  1408. " * This_comment_can_not_be_broken_into_lines\n"
  1409. " */",
  1410. format("/*\n"
  1411. " * This_comment_can_not_be_broken_into_lines\n"
  1412. " */",
  1413. getLLVMStyleWithColumns(20)));
  1414. EXPECT_EQ("{\n"
  1415. " /*\n"
  1416. " This is another\n"
  1417. " long comment that\n"
  1418. " doesn't fit on one\n"
  1419. " line 1234567890\n"
  1420. " */\n"
  1421. "}",
  1422. format("{\n"
  1423. "/*\n"
  1424. "This is another "
  1425. " long comment that "
  1426. " doesn't fit on one"
  1427. " line 1234567890\n"
  1428. "*/\n"
  1429. "}",
  1430. getLLVMStyleWithColumns(20)));
  1431. EXPECT_EQ("{\n"
  1432. " /*\n"
  1433. " * This i s\n"
  1434. " * another comment\n"
  1435. " * t hat doesn' t\n"
  1436. " * fit on one l i\n"
  1437. " * n e\n"
  1438. " */\n"
  1439. "}",
  1440. format("{\n"
  1441. "/*\n"
  1442. " * This i s"
  1443. " another comment"
  1444. " t hat doesn' t"
  1445. " fit on one l i"
  1446. " n e\n"
  1447. " */\n"
  1448. "}",
  1449. getLLVMStyleWithColumns(20)));
  1450. EXPECT_EQ("/*\n"
  1451. " * This is a long\n"
  1452. " * comment that\n"
  1453. " * doesn't fit on\n"
  1454. " * one line\n"
  1455. " */",
  1456. format(" /*\n"
  1457. " * This is a long comment that doesn't fit on one line\n"
  1458. " */",
  1459. getLLVMStyleWithColumns(20)));
  1460. EXPECT_EQ("{\n"
  1461. " if (something) /* This is a\n"
  1462. " long\n"
  1463. " comment */\n"
  1464. " ;\n"
  1465. "}",
  1466. format("{\n"
  1467. " if (something) /* This is a long comment */\n"
  1468. " ;\n"
  1469. "}",
  1470. getLLVMStyleWithColumns(30)));
  1471. EXPECT_EQ("/* A comment before\n"
  1472. " * a macro\n"
  1473. " * definition */\n"
  1474. "#define a b",
  1475. format("/* A comment before a macro definition */\n"
  1476. "#define a b",
  1477. getLLVMStyleWithColumns(20)));
  1478. EXPECT_EQ("/* some comment\n"
  1479. " * a comment\n"
  1480. "* that we break\n"
  1481. " * another comment\n"
  1482. "* we have to break\n"
  1483. "* a left comment\n"
  1484. " */",
  1485. format(" /* some comment\n"
  1486. " * a comment that we break\n"
  1487. " * another comment we have to break\n"
  1488. "* a left comment\n"
  1489. " */",
  1490. getLLVMStyleWithColumns(20)));
  1491. EXPECT_EQ("/**\n"
  1492. " * multiline block\n"
  1493. " * comment\n"
  1494. " *\n"
  1495. " */",
  1496. format("/**\n"
  1497. " * multiline block comment\n"
  1498. " *\n"
  1499. " */",
  1500. getLLVMStyleWithColumns(20)));
  1501. EXPECT_EQ("/*\n"
  1502. "\n"
  1503. "\n"
  1504. " */\n",
  1505. format(" /* \n"
  1506. " \n"
  1507. " \n"
  1508. " */\n"));
  1509. EXPECT_EQ("/* a a */",
  1510. format("/* a a */", getLLVMStyleWithColumns(15)));
  1511. EXPECT_EQ("/* a a bc */",
  1512. format("/* a a bc */", getLLVMStyleWithColumns(15)));
  1513. EXPECT_EQ("/* aaa aaa\n"
  1514. " * aaaaa */",
  1515. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1516. EXPECT_EQ("/* aaa aaa\n"
  1517. " * aaaaa */",
  1518. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1519. }
  1520. TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
  1521. EXPECT_EQ("#define X \\\n"
  1522. " /* \\\n"
  1523. " Test \\\n"
  1524. " Macro comment \\\n"
  1525. " with a long \\\n"
  1526. " line \\\n"
  1527. " */ \\\n"
  1528. " A + B",
  1529. format("#define X \\\n"
  1530. " /*\n"
  1531. " Test\n"
  1532. " Macro comment with a long line\n"
  1533. " */ \\\n"
  1534. " A + B",
  1535. getLLVMStyleWithColumns(20)));
  1536. EXPECT_EQ("#define X \\\n"
  1537. " /* Macro comment \\\n"
  1538. " with a long \\\n"
  1539. " line */ \\\n"
  1540. " A + B",
  1541. format("#define X \\\n"
  1542. " /* Macro comment with a long\n"
  1543. " line */ \\\n"
  1544. " A + B",
  1545. getLLVMStyleWithColumns(20)));
  1546. EXPECT_EQ("#define X \\\n"
  1547. " /* Macro comment \\\n"
  1548. " * with a long \\\n"
  1549. " * line */ \\\n"
  1550. " A + B",
  1551. format("#define X \\\n"
  1552. " /* Macro comment with a long line */ \\\n"
  1553. " A + B",
  1554. getLLVMStyleWithColumns(20)));
  1555. }
  1556. TEST_F(FormatTest, CommentsInStaticInitializers) {
  1557. EXPECT_EQ(
  1558. "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
  1559. " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
  1560. " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
  1561. " aaaaaaaaaaaaaaaaaaaa, // comment\n"
  1562. " aaaaaaaaaaaaaaaaaaaa};",
  1563. format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
  1564. " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
  1565. " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
  1566. " aaaaaaaaaaaaaaaaaaaa , // comment\n"
  1567. " aaaaaaaaaaaaaaaaaaaa };"));
  1568. verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1569. " bbbbbbbbbbb, ccccccccccc};");
  1570. verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1571. " // comment for bb....\n"
  1572. " bbbbbbbbbbb, ccccccccccc};");
  1573. verifyGoogleFormat(
  1574. "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1575. " bbbbbbbbbbb, ccccccccccc};");
  1576. verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1577. " // comment for bb....\n"
  1578. " bbbbbbbbbbb, ccccccccccc};");
  1579. verifyFormat("S s = {{a, b, c}, // Group #1\n"
  1580. " {d, e, f}, // Group #2\n"
  1581. " {g, h, i}}; // Group #3");
  1582. verifyFormat("S s = {{// Group #1\n"
  1583. " a, b, c},\n"
  1584. " {// Group #2\n"
  1585. " d, e, f},\n"
  1586. " {// Group #3\n"
  1587. " g, h, i}};");
  1588. EXPECT_EQ("S s = {\n"
  1589. " // Some comment\n"
  1590. " a,\n"
  1591. "\n"
  1592. " // Comment after empty line\n"
  1593. " b}",
  1594. format("S s = {\n"
  1595. " // Some comment\n"
  1596. " a,\n"
  1597. " \n"
  1598. " // Comment after empty line\n"
  1599. " b\n"
  1600. "}"));
  1601. EXPECT_EQ("S s = {\n"
  1602. " /* Some comment */\n"
  1603. " a,\n"
  1604. "\n"
  1605. " /* Comment after empty line */\n"
  1606. " b}",
  1607. format("S s = {\n"
  1608. " /* Some comment */\n"
  1609. " a,\n"
  1610. " \n"
  1611. " /* Comment after empty line */\n"
  1612. " b\n"
  1613. "}"));
  1614. verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
  1615. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1616. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1617. " 0x00, 0x00, 0x00, 0x00}; // comment\n");
  1618. }
  1619. TEST_F(FormatTest, IgnoresIf0Contents) {
  1620. EXPECT_EQ("#if 0\n"
  1621. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1622. "#endif\n"
  1623. "void f() {}",
  1624. format("#if 0\n"
  1625. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1626. "#endif\n"
  1627. "void f( ) { }"));
  1628. EXPECT_EQ("#if false\n"
  1629. "void f( ) { }\n"
  1630. "#endif\n"
  1631. "void g() {}\n",
  1632. format("#if false\n"
  1633. "void f( ) { }\n"
  1634. "#endif\n"
  1635. "void g( ) { }\n"));
  1636. EXPECT_EQ("enum E {\n"
  1637. " One,\n"
  1638. " Two,\n"
  1639. "#if 0\n"
  1640. "Three,\n"
  1641. " Four,\n"
  1642. "#endif\n"
  1643. " Five\n"
  1644. "};",
  1645. format("enum E {\n"
  1646. " One,Two,\n"
  1647. "#if 0\n"
  1648. "Three,\n"
  1649. " Four,\n"
  1650. "#endif\n"
  1651. " Five};"));
  1652. EXPECT_EQ("enum F {\n"
  1653. " One,\n"
  1654. "#if 1\n"
  1655. " Two,\n"
  1656. "#if 0\n"
  1657. "Three,\n"
  1658. " Four,\n"
  1659. "#endif\n"
  1660. " Five\n"
  1661. "#endif\n"
  1662. "};",
  1663. format("enum F {\n"
  1664. "One,\n"
  1665. "#if 1\n"
  1666. "Two,\n"
  1667. "#if 0\n"
  1668. "Three,\n"
  1669. " Four,\n"
  1670. "#endif\n"
  1671. "Five\n"
  1672. "#endif\n"
  1673. "};"));
  1674. EXPECT_EQ("enum G {\n"
  1675. " One,\n"
  1676. "#if 0\n"
  1677. "Two,\n"
  1678. "#else\n"
  1679. " Three,\n"
  1680. "#endif\n"
  1681. " Four\n"
  1682. "};",
  1683. format("enum G {\n"
  1684. "One,\n"
  1685. "#if 0\n"
  1686. "Two,\n"
  1687. "#else\n"
  1688. "Three,\n"
  1689. "#endif\n"
  1690. "Four\n"
  1691. "};"));
  1692. EXPECT_EQ("enum H {\n"
  1693. " One,\n"
  1694. "#if 0\n"
  1695. "#ifdef Q\n"
  1696. "Two,\n"
  1697. "#else\n"
  1698. "Three,\n"
  1699. "#endif\n"
  1700. "#endif\n"
  1701. " Four\n"
  1702. "};",
  1703. format("enum H {\n"
  1704. "One,\n"
  1705. "#if 0\n"
  1706. "#ifdef Q\n"
  1707. "Two,\n"
  1708. "#else\n"
  1709. "Three,\n"
  1710. "#endif\n"
  1711. "#endif\n"
  1712. "Four\n"
  1713. "};"));
  1714. EXPECT_EQ("enum I {\n"
  1715. " One,\n"
  1716. "#if /* test */ 0 || 1\n"
  1717. "Two,\n"
  1718. "Three,\n"
  1719. "#endif\n"
  1720. " Four\n"
  1721. "};",
  1722. format("enum I {\n"
  1723. "One,\n"
  1724. "#if /* test */ 0 || 1\n"
  1725. "Two,\n"
  1726. "Three,\n"
  1727. "#endif\n"
  1728. "Four\n"
  1729. "};"));
  1730. EXPECT_EQ("enum J {\n"
  1731. " One,\n"
  1732. "#if 0\n"
  1733. "#if 0\n"
  1734. "Two,\n"
  1735. "#else\n"
  1736. "Three,\n"
  1737. "#endif\n"
  1738. "Four,\n"
  1739. "#endif\n"
  1740. " Five\n"
  1741. "};",
  1742. format("enum J {\n"
  1743. "One,\n"
  1744. "#if 0\n"
  1745. "#if 0\n"
  1746. "Two,\n"
  1747. "#else\n"
  1748. "Three,\n"
  1749. "#endif\n"
  1750. "Four,\n"
  1751. "#endif\n"
  1752. "Five\n"
  1753. "};"));
  1754. }
  1755. //===----------------------------------------------------------------------===//
  1756. // Tests for classes, namespaces, etc.
  1757. //===----------------------------------------------------------------------===//
  1758. TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
  1759. verifyFormat("class A {};");
  1760. }
  1761. TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
  1762. verifyFormat("class A {\n"
  1763. "public:\n"
  1764. "public: // comment\n"
  1765. "protected:\n"
  1766. "private:\n"
  1767. " void f() {}\n"
  1768. "};");
  1769. verifyGoogleFormat("class A {\n"
  1770. " public:\n"
  1771. " protected:\n"
  1772. " private:\n"
  1773. " void f() {}\n"
  1774. "};");
  1775. verifyFormat("class A {\n"
  1776. "public slots:\n"
  1777. " void f1() {}\n"
  1778. "public Q_SLOTS:\n"
  1779. " void f2() {}\n"
  1780. "protected slots:\n"
  1781. " void f3() {}\n"
  1782. "protected Q_SLOTS:\n"
  1783. " void f4() {}\n"
  1784. "private slots:\n"
  1785. " void f5() {}\n"
  1786. "private Q_SLOTS:\n"
  1787. " void f6() {}\n"
  1788. "signals:\n"
  1789. " void g1();\n"
  1790. "Q_SIGNALS:\n"
  1791. " void g2();\n"
  1792. "};");
  1793. // Don't interpret 'signals' the wrong way.
  1794. verifyFormat("signals.set();");
  1795. verifyFormat("for (Signals signals : f()) {\n}");
  1796. verifyFormat("{\n"
  1797. " signals.set(); // This needs indentation.\n"
  1798. "}");
  1799. }
  1800. TEST_F(FormatTest, SeparatesLogicalBlocks) {
  1801. EXPECT_EQ("class A {\n"
  1802. "public:\n"
  1803. " void f();\n"
  1804. "\n"
  1805. "private:\n"
  1806. " void g() {}\n"
  1807. " // test\n"
  1808. "protected:\n"
  1809. " int h;\n"
  1810. "};",
  1811. format("class A {\n"
  1812. "public:\n"
  1813. "void f();\n"
  1814. "private:\n"
  1815. "void g() {}\n"
  1816. "// test\n"
  1817. "protected:\n"
  1818. "int h;\n"
  1819. "};"));
  1820. EXPECT_EQ("class A {\n"
  1821. "protected:\n"
  1822. "public:\n"
  1823. " void f();\n"
  1824. "};",
  1825. format("class A {\n"
  1826. "protected:\n"
  1827. "\n"
  1828. "public:\n"
  1829. "\n"
  1830. " void f();\n"
  1831. "};"));
  1832. // Even ensure proper spacing inside macros.
  1833. EXPECT_EQ("#define B \\\n"
  1834. " class A { \\\n"
  1835. " protected: \\\n"
  1836. " public: \\\n"
  1837. " void f(); \\\n"
  1838. " };",
  1839. format("#define B \\\n"
  1840. " class A { \\\n"
  1841. " protected: \\\n"
  1842. " \\\n"
  1843. " public: \\\n"
  1844. " \\\n"
  1845. " void f(); \\\n"
  1846. " };",
  1847. getGoogleStyle()));
  1848. // But don't remove empty lines after macros ending in access specifiers.
  1849. EXPECT_EQ("#define A private:\n"
  1850. "\n"
  1851. "int i;",
  1852. format("#define A private:\n"
  1853. "\n"
  1854. "int i;"));
  1855. }
  1856. TEST_F(FormatTest, FormatsClasses) {
  1857. verifyFormat("class A : public B {};");
  1858. verifyFormat("class A : public ::B {};");
  1859. verifyFormat(
  1860. "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1861. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1862. verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
  1863. " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1864. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1865. verifyFormat(
  1866. "class A : public B, public C, public D, public E, public F {};");
  1867. verifyFormat("class AAAAAAAAAAAA : public B,\n"
  1868. " public C,\n"
  1869. " public D,\n"
  1870. " public E,\n"
  1871. " public F,\n"
  1872. " public G {};");
  1873. verifyFormat("class\n"
  1874. " ReallyReallyLongClassName {\n"
  1875. " int i;\n"
  1876. "};",
  1877. getLLVMStyleWithColumns(32));
  1878. verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
  1879. " aaaaaaaaaaaaaaaa> {};");
  1880. verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
  1881. " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
  1882. " aaaaaaaaaaaaaaaaaaaaaa> {};");
  1883. verifyFormat("template <class R, class C>\n"
  1884. "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
  1885. " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
  1886. verifyFormat("class ::A::B {};");
  1887. }
  1888. TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
  1889. verifyFormat("class A {\n} a, b;");
  1890. verifyFormat("struct A {\n} a, b;");
  1891. verifyFormat("union A {\n} a;");
  1892. }
  1893. TEST_F(FormatTest, FormatsEnum) {
  1894. verifyFormat("enum {\n"
  1895. " Zero,\n"
  1896. " One = 1,\n"
  1897. " Two = One + 1,\n"
  1898. " Three = (One + Two),\n"
  1899. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1900. " Five = (One, Two, Three, Four, 5)\n"
  1901. "};");
  1902. verifyGoogleFormat("enum {\n"
  1903. " Zero,\n"
  1904. " One = 1,\n"
  1905. " Two = One + 1,\n"
  1906. " Three = (One + Two),\n"
  1907. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1908. " Five = (One, Two, Three, Four, 5)\n"
  1909. "};");
  1910. verifyFormat("enum Enum {};");
  1911. verifyFormat("enum {};");
  1912. verifyFormat("enum X E {} d;");
  1913. verifyFormat("enum __attribute__((...)) E {} d;");
  1914. verifyFormat("enum __declspec__((...)) E {} d;");
  1915. verifyFormat("enum {\n"
  1916. " Bar = Foo<int, int>::value\n"
  1917. "};",
  1918. getLLVMStyleWithColumns(30));
  1919. verifyFormat("enum ShortEnum { A, B, C };");
  1920. verifyGoogleFormat("enum ShortEnum { A, B, C };");
  1921. EXPECT_EQ("enum KeepEmptyLines {\n"
  1922. " ONE,\n"
  1923. "\n"
  1924. " TWO,\n"
  1925. "\n"
  1926. " THREE\n"
  1927. "}",
  1928. format("enum KeepEmptyLines {\n"
  1929. " ONE,\n"
  1930. "\n"
  1931. " TWO,\n"
  1932. "\n"
  1933. "\n"
  1934. " THREE\n"
  1935. "}"));
  1936. verifyFormat("enum E { // comment\n"
  1937. " ONE,\n"
  1938. " TWO\n"
  1939. "};\n"
  1940. "int i;");
  1941. // Not enums.
  1942. verifyFormat("enum X f() {\n"
  1943. " a();\n"
  1944. " return 42;\n"
  1945. "}");
  1946. verifyFormat("enum X Type::f() {\n"
  1947. " a();\n"
  1948. " return 42;\n"
  1949. "}");
  1950. verifyFormat("enum ::X f() {\n"
  1951. " a();\n"
  1952. " return 42;\n"
  1953. "}");
  1954. verifyFormat("enum ns::X f() {\n"
  1955. " a();\n"
  1956. " return 42;\n"
  1957. "}");
  1958. }
  1959. TEST_F(FormatTest, FormatsEnumsWithErrors) {
  1960. verifyFormat("enum Type {\n"
  1961. " One = 0; // These semicolons should be commas.\n"
  1962. " Two = 1;\n"
  1963. "};");
  1964. verifyFormat("namespace n {\n"
  1965. "enum Type {\n"
  1966. " One,\n"
  1967. " Two, // missing };\n"
  1968. " int i;\n"
  1969. "}\n"
  1970. "void g() {}");
  1971. }
  1972. TEST_F(FormatTest, FormatsEnumStruct) {
  1973. verifyFormat("enum struct {\n"
  1974. " Zero,\n"
  1975. " One = 1,\n"
  1976. " Two = One + 1,\n"
  1977. " Three = (One + Two),\n"
  1978. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1979. " Five = (One, Two, Three, Four, 5)\n"
  1980. "};");
  1981. verifyFormat("enum struct Enum {};");
  1982. verifyFormat("enum struct {};");
  1983. verifyFormat("enum struct X E {} d;");
  1984. verifyFormat("enum struct __attribute__((...)) E {} d;");
  1985. verifyFormat("enum struct __declspec__((...)) E {} d;");
  1986. verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
  1987. }
  1988. TEST_F(FormatTest, FormatsEnumClass) {
  1989. verifyFormat("enum class {\n"
  1990. " Zero,\n"
  1991. " One = 1,\n"
  1992. " Two = One + 1,\n"
  1993. " Three = (One + Two),\n"
  1994. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1995. " Five = (One, Two, Three, Four, 5)\n"
  1996. "};");
  1997. verifyFormat("enum class Enum {};");
  1998. verifyFormat("enum class {};");
  1999. verifyFormat("enum class X E {} d;");
  2000. verifyFormat("enum class __attribute__((...)) E {} d;");
  2001. verifyFormat("enum class __declspec__((...)) E {} d;");
  2002. verifyFormat("enum class X f() {\n a();\n return 42;\n}");
  2003. }
  2004. TEST_F(FormatTest, FormatsEnumTypes) {
  2005. verifyFormat("enum X : int {\n"
  2006. " A, // Force multiple lines.\n"
  2007. " B\n"
  2008. "};");
  2009. verifyFormat("enum X : int { A, B };");
  2010. verifyFormat("enum X : std::uint32_t { A, B };");
  2011. }
  2012. TEST_F(FormatTest, FormatsNSEnums) {
  2013. verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
  2014. verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
  2015. " // Information about someDecentlyLongValue.\n"
  2016. " someDecentlyLongValue,\n"
  2017. " // Information about anotherDecentlyLongValue.\n"
  2018. " anotherDecentlyLongValue,\n"
  2019. " // Information about aThirdDecentlyLongValue.\n"
  2020. " aThirdDecentlyLongValue\n"
  2021. "};");
  2022. verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
  2023. " a = 1,\n"
  2024. " b = 2,\n"
  2025. " c = 3,\n"
  2026. "};");
  2027. verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
  2028. " a = 1,\n"
  2029. " b = 2,\n"
  2030. " c = 3,\n"
  2031. "};");
  2032. verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
  2033. " a = 1,\n"
  2034. " b = 2,\n"
  2035. " c = 3,\n"
  2036. "};");
  2037. }
  2038. TEST_F(FormatTest, FormatsBitfields) {
  2039. verifyFormat("struct Bitfields {\n"
  2040. " unsigned sClass : 8;\n"
  2041. " unsigned ValueKind : 2;\n"
  2042. "};");
  2043. verifyFormat("struct A {\n"
  2044. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
  2045. " bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
  2046. "};");
  2047. verifyFormat("struct MyStruct {\n"
  2048. " uchar data;\n"
  2049. " uchar : 8;\n"
  2050. " uchar : 8;\n"
  2051. " uchar other;\n"
  2052. "};");
  2053. }
  2054. TEST_F(FormatTest, FormatsNamespaces) {
  2055. verifyFormat("namespace some_namespace {\n"
  2056. "class A {};\n"
  2057. "void f() { f(); }\n"
  2058. "}");
  2059. verifyFormat("namespace {\n"
  2060. "class A {};\n"
  2061. "void f() { f(); }\n"
  2062. "}");
  2063. verifyFormat("inline namespace X {\n"
  2064. "class A {};\n"
  2065. "void f() { f(); }\n"
  2066. "}");
  2067. verifyFormat("using namespace some_namespace;\n"
  2068. "class A {};\n"
  2069. "void f() { f(); }");
  2070. // This code is more common than we thought; if we
  2071. // layout this correctly the semicolon will go into
  2072. // its own line, which is undesirable.
  2073. verifyFormat("namespace {};");
  2074. verifyFormat("namespace {\n"
  2075. "class A {};\n"
  2076. "};");
  2077. verifyFormat("namespace {\n"
  2078. "int SomeVariable = 0; // comment\n"
  2079. "} // namespace");
  2080. EXPECT_EQ("#ifndef HEADER_GUARD\n"
  2081. "#define HEADER_GUARD\n"
  2082. "namespace my_namespace {\n"
  2083. "int i;\n"
  2084. "} // my_namespace\n"
  2085. "#endif // HEADER_GUARD",
  2086. format("#ifndef HEADER_GUARD\n"
  2087. " #define HEADER_GUARD\n"
  2088. " namespace my_namespace {\n"
  2089. "int i;\n"
  2090. "} // my_namespace\n"
  2091. "#endif // HEADER_GUARD"));
  2092. EXPECT_EQ("namespace A::B {\n"
  2093. "class C {};\n"
  2094. "}",
  2095. format("namespace A::B {\n"
  2096. "class C {};\n"
  2097. "}"));
  2098. FormatStyle Style = getLLVMStyle();
  2099. Style.NamespaceIndentation = FormatStyle::NI_All;
  2100. EXPECT_EQ("namespace out {\n"
  2101. " int i;\n"
  2102. " namespace in {\n"
  2103. " int i;\n"
  2104. " } // namespace\n"
  2105. "} // namespace",
  2106. format("namespace out {\n"
  2107. "int i;\n"
  2108. "namespace in {\n"
  2109. "int i;\n"
  2110. "} // namespace\n"
  2111. "} // namespace",
  2112. Style));
  2113. Style.NamespaceIndentation = FormatStyle::NI_Inner;
  2114. EXPECT_EQ("namespace out {\n"
  2115. "int i;\n"
  2116. "namespace in {\n"
  2117. " int i;\n"
  2118. "} // namespace\n"
  2119. "} // namespace",
  2120. format("namespace out {\n"
  2121. "int i;\n"
  2122. "namespace in {\n"
  2123. "int i;\n"
  2124. "} // namespace\n"
  2125. "} // namespace",
  2126. Style));
  2127. }
  2128. TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
  2129. TEST_F(FormatTest, FormatsInlineASM) {
  2130. verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
  2131. verifyFormat("asm(\"nop\" ::: \"memory\");");
  2132. verifyFormat(
  2133. "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
  2134. " \"cpuid\\n\\t\"\n"
  2135. " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
  2136. " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
  2137. " : \"a\"(value));");
  2138. EXPECT_EQ(
  2139. "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
  2140. " __asm {\n"
  2141. " mov edx,[that] // vtable in edx\n"
  2142. " mov eax,methodIndex\n"
  2143. " call [edx][eax*4] // stdcall\n"
  2144. " }\n"
  2145. "}",
  2146. format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
  2147. " __asm {\n"
  2148. " mov edx,[that] // vtable in edx\n"
  2149. " mov eax,methodIndex\n"
  2150. " call [edx][eax*4] // stdcall\n"
  2151. " }\n"
  2152. "}"));
  2153. EXPECT_EQ("_asm {\n"
  2154. " xor eax, eax;\n"
  2155. " cpuid;\n"
  2156. "}",
  2157. format("_asm {\n"
  2158. " xor eax, eax;\n"
  2159. " cpuid;\n"
  2160. "}"));
  2161. verifyFormat("void function() {\n"
  2162. " // comment\n"
  2163. " asm(\"\");\n"
  2164. "}");
  2165. EXPECT_EQ("__asm {\n"
  2166. "}\n"
  2167. "int i;",
  2168. format("__asm {\n"
  2169. "}\n"
  2170. "int i;"));
  2171. }
  2172. TEST_F(FormatTest, FormatTryCatch) {
  2173. verifyFormat("try {\n"
  2174. " throw a * b;\n"
  2175. "} catch (int a) {\n"
  2176. " // Do nothing.\n"
  2177. "} catch (...) {\n"
  2178. " exit(42);\n"
  2179. "}");
  2180. // Function-level try statements.
  2181. verifyFormat("int f() try { return 4; } catch (...) {\n"
  2182. " return 5;\n"
  2183. "}");
  2184. verifyFormat("class A {\n"
  2185. " int a;\n"
  2186. " A() try : a(0) {\n"
  2187. " } catch (...) {\n"
  2188. " throw;\n"
  2189. " }\n"
  2190. "};\n");
  2191. // Incomplete try-catch blocks.
  2192. verifyIncompleteFormat("try {} catch (");
  2193. }
  2194. TEST_F(FormatTest, FormatSEHTryCatch) {
  2195. verifyFormat("__try {\n"
  2196. " int a = b * c;\n"
  2197. "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
  2198. " // Do nothing.\n"
  2199. "}");
  2200. verifyFormat("__try {\n"
  2201. " int a = b * c;\n"
  2202. "} __finally {\n"
  2203. " // Do nothing.\n"
  2204. "}");
  2205. verifyFormat("DEBUG({\n"
  2206. " __try {\n"
  2207. " } __finally {\n"
  2208. " }\n"
  2209. "});\n");
  2210. }
  2211. TEST_F(FormatTest, IncompleteTryCatchBlocks) {
  2212. verifyFormat("try {\n"
  2213. " f();\n"
  2214. "} catch {\n"
  2215. " g();\n"
  2216. "}");
  2217. verifyFormat("try {\n"
  2218. " f();\n"
  2219. "} catch (A a) MACRO(x) {\n"
  2220. " g();\n"
  2221. "} catch (B b) MACRO(x) {\n"
  2222. " g();\n"
  2223. "}");
  2224. }
  2225. TEST_F(FormatTest, FormatTryCatchBraceStyles) {
  2226. FormatStyle Style = getLLVMStyle();
  2227. for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
  2228. FormatStyle::BS_WebKit}) {
  2229. Style.BreakBeforeBraces = BraceStyle;
  2230. verifyFormat("try {\n"
  2231. " // something\n"
  2232. "} catch (...) {\n"
  2233. " // something\n"
  2234. "}",
  2235. Style);
  2236. }
  2237. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  2238. verifyFormat("try {\n"
  2239. " // something\n"
  2240. "}\n"
  2241. "catch (...) {\n"
  2242. " // something\n"
  2243. "}",
  2244. Style);
  2245. verifyFormat("__try {\n"
  2246. " // something\n"
  2247. "}\n"
  2248. "__finally {\n"
  2249. " // something\n"
  2250. "}",
  2251. Style);
  2252. verifyFormat("@try {\n"
  2253. " // something\n"
  2254. "}\n"
  2255. "@finally {\n"
  2256. " // something\n"
  2257. "}",
  2258. Style);
  2259. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  2260. verifyFormat("try\n"
  2261. "{\n"
  2262. " // something\n"
  2263. "}\n"
  2264. "catch (...)\n"
  2265. "{\n"
  2266. " // something\n"
  2267. "}",
  2268. Style);
  2269. Style.BreakBeforeBraces = FormatStyle::BS_GNU;
  2270. verifyFormat("try\n"
  2271. " {\n"
  2272. " // something\n"
  2273. " }\n"
  2274. "catch (...)\n"
  2275. " {\n"
  2276. " // something\n"
  2277. " }",
  2278. Style);
  2279. Style.BreakBeforeBraces = FormatStyle::BS_Custom;
  2280. Style.BraceWrapping.BeforeCatch = true;
  2281. verifyFormat("try {\n"
  2282. " // something\n"
  2283. "}\n"
  2284. "catch (...) {\n"
  2285. " // something\n"
  2286. "}",
  2287. Style);
  2288. }
  2289. TEST_F(FormatTest, FormatObjCTryCatch) {
  2290. verifyFormat("@try {\n"
  2291. " f();\n"
  2292. "} @catch (NSException e) {\n"
  2293. " @throw;\n"
  2294. "} @finally {\n"
  2295. " exit(42);\n"
  2296. "}");
  2297. verifyFormat("DEBUG({\n"
  2298. " @try {\n"
  2299. " } @finally {\n"
  2300. " }\n"
  2301. "});\n");
  2302. }
  2303. TEST_F(FormatTest, FormatObjCAutoreleasepool) {
  2304. FormatStyle Style = getLLVMStyle();
  2305. verifyFormat("@autoreleasepool {\n"
  2306. " f();\n"
  2307. "}\n"
  2308. "@autoreleasepool {\n"
  2309. " f();\n"
  2310. "}\n",
  2311. Style);
  2312. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  2313. verifyFormat("@autoreleasepool\n"
  2314. "{\n"
  2315. " f();\n"
  2316. "}\n"
  2317. "@autoreleasepool\n"
  2318. "{\n"
  2319. " f();\n"
  2320. "}\n",
  2321. Style);
  2322. }
  2323. TEST_F(FormatTest, StaticInitializers) {
  2324. verifyFormat("static SomeClass SC = {1, 'a'};");
  2325. verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
  2326. " 100000000, "
  2327. "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
  2328. // Here, everything other than the "}" would fit on a line.
  2329. verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
  2330. " 10000000000000000000000000};");
  2331. EXPECT_EQ("S s = {a,\n"
  2332. "\n"
  2333. " b};",
  2334. format("S s = {\n"
  2335. " a,\n"
  2336. "\n"
  2337. " b\n"
  2338. "};"));
  2339. // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
  2340. // line. However, the formatting looks a bit off and this probably doesn't
  2341. // happen often in practice.
  2342. verifyFormat("static int Variable[1] = {\n"
  2343. " {1000000000000000000000000000000000000}};",
  2344. getLLVMStyleWithColumns(40));
  2345. }
  2346. TEST_F(FormatTest, DesignatedInitializers) {
  2347. verifyFormat("const struct A a = {.a = 1, .b = 2};");
  2348. verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
  2349. " .bbbbbbbbbb = 2,\n"
  2350. " .cccccccccc = 3,\n"
  2351. " .dddddddddd = 4,\n"
  2352. " .eeeeeeeeee = 5};");
  2353. verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
  2354. " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
  2355. " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
  2356. " .ccccccccccccccccccccccccccc = 3,\n"
  2357. " .ddddddddddddddddddddddddddd = 4,\n"
  2358. " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
  2359. verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
  2360. }
  2361. TEST_F(FormatTest, NestedStaticInitializers) {
  2362. verifyFormat("static A x = {{{}}};\n");
  2363. verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
  2364. " {init1, init2, init3, init4}}};",
  2365. getLLVMStyleWithColumns(50));
  2366. verifyFormat("somes Status::global_reps[3] = {\n"
  2367. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  2368. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  2369. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
  2370. getLLVMStyleWithColumns(60));
  2371. verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
  2372. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  2373. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  2374. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
  2375. verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
  2376. " {rect.fRight - rect.fLeft, rect.fBottom - "
  2377. "rect.fTop}};");
  2378. verifyFormat(
  2379. "SomeArrayOfSomeType a = {\n"
  2380. " {{1, 2, 3},\n"
  2381. " {1, 2, 3},\n"
  2382. " {111111111111111111111111111111, 222222222222222222222222222222,\n"
  2383. " 333333333333333333333333333333},\n"
  2384. " {1, 2, 3},\n"
  2385. " {1, 2, 3}}};");
  2386. verifyFormat(
  2387. "SomeArrayOfSomeType a = {\n"
  2388. " {{1, 2, 3}},\n"
  2389. " {{1, 2, 3}},\n"
  2390. " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
  2391. " 333333333333333333333333333333}},\n"
  2392. " {{1, 2, 3}},\n"
  2393. " {{1, 2, 3}}};");
  2394. verifyFormat("struct {\n"
  2395. " unsigned bit;\n"
  2396. " const char *const name;\n"
  2397. "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
  2398. " {kOsWin, \"Windows\"},\n"
  2399. " {kOsLinux, \"Linux\"},\n"
  2400. " {kOsCrOS, \"Chrome OS\"}};");
  2401. verifyFormat("struct {\n"
  2402. " unsigned bit;\n"
  2403. " const char *const name;\n"
  2404. "} kBitsToOs[] = {\n"
  2405. " {kOsMac, \"Mac\"},\n"
  2406. " {kOsWin, \"Windows\"},\n"
  2407. " {kOsLinux, \"Linux\"},\n"
  2408. " {kOsCrOS, \"Chrome OS\"},\n"
  2409. "};");
  2410. }
  2411. TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
  2412. verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2413. " \\\n"
  2414. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
  2415. }
  2416. TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
  2417. verifyFormat("virtual void write(ELFWriter *writerrr,\n"
  2418. " OwningPtr<FileOutputBuffer> &buffer) = 0;");
  2419. // Do break defaulted and deleted functions.
  2420. verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
  2421. " default;",
  2422. getLLVMStyleWithColumns(40));
  2423. verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
  2424. " delete;",
  2425. getLLVMStyleWithColumns(40));
  2426. }
  2427. TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
  2428. verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
  2429. getLLVMStyleWithColumns(40));
  2430. verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  2431. getLLVMStyleWithColumns(40));
  2432. EXPECT_EQ("#define Q \\\n"
  2433. " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n"
  2434. " \"aaaaaaaa.cpp\"",
  2435. format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  2436. getLLVMStyleWithColumns(40)));
  2437. }
  2438. TEST_F(FormatTest, UnderstandsLinePPDirective) {
  2439. EXPECT_EQ("# 123 \"A string literal\"",
  2440. format(" # 123 \"A string literal\""));
  2441. }
  2442. TEST_F(FormatTest, LayoutUnknownPPDirective) {
  2443. EXPECT_EQ("#;", format("#;"));
  2444. verifyFormat("#\n;\n;\n;");
  2445. }
  2446. TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
  2447. EXPECT_EQ("#line 42 \"test\"\n",
  2448. format("# \\\n line \\\n 42 \\\n \"test\"\n"));
  2449. EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n",
  2450. getLLVMStyleWithColumns(12)));
  2451. }
  2452. TEST_F(FormatTest, EndOfFileEndsPPDirective) {
  2453. EXPECT_EQ("#line 42 \"test\"",
  2454. format("# \\\n line \\\n 42 \\\n \"test\""));
  2455. EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B"));
  2456. }
  2457. TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
  2458. verifyFormat("#define A \\x20");
  2459. verifyFormat("#define A \\ x20");
  2460. EXPECT_EQ("#define A \\ x20", format("#define A \\ x20"));
  2461. verifyFormat("#define A ''");
  2462. verifyFormat("#define A ''qqq");
  2463. verifyFormat("#define A `qqq");
  2464. verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
  2465. EXPECT_EQ("const char *c = STRINGIFY(\n"
  2466. "\\na : b);",
  2467. format("const char * c = STRINGIFY(\n"
  2468. "\\na : b);"));
  2469. verifyFormat("a\r\\");
  2470. verifyFormat("a\v\\");
  2471. verifyFormat("a\f\\");
  2472. }
  2473. TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
  2474. verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
  2475. verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
  2476. verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
  2477. // FIXME: We never break before the macro name.
  2478. verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
  2479. verifyFormat("#define A A\n#define A A");
  2480. verifyFormat("#define A(X) A\n#define A A");
  2481. verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
  2482. verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
  2483. }
  2484. TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
  2485. EXPECT_EQ("// somecomment\n"
  2486. "#include \"a.h\"\n"
  2487. "#define A( \\\n"
  2488. " A, B)\n"
  2489. "#include \"b.h\"\n"
  2490. "// somecomment\n",
  2491. format(" // somecomment\n"
  2492. " #include \"a.h\"\n"
  2493. "#define A(A,\\\n"
  2494. " B)\n"
  2495. " #include \"b.h\"\n"
  2496. " // somecomment\n",
  2497. getLLVMStyleWithColumns(13)));
  2498. }
  2499. TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
  2500. TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
  2501. EXPECT_EQ("#define A \\\n"
  2502. " c; \\\n"
  2503. " e;\n"
  2504. "f;",
  2505. format("#define A c; e;\n"
  2506. "f;",
  2507. getLLVMStyleWithColumns(14)));
  2508. }
  2509. TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
  2510. TEST_F(FormatTest, MacroDefinitionInsideStatement) {
  2511. EXPECT_EQ("int x,\n"
  2512. "#define A\n"
  2513. " y;",
  2514. format("int x,\n#define A\ny;"));
  2515. }
  2516. TEST_F(FormatTest, HashInMacroDefinition) {
  2517. EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
  2518. verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
  2519. verifyFormat("#define A \\\n"
  2520. " { \\\n"
  2521. " f(#c); \\\n"
  2522. " }",
  2523. getLLVMStyleWithColumns(11));
  2524. verifyFormat("#define A(X) \\\n"
  2525. " void function##X()",
  2526. getLLVMStyleWithColumns(22));
  2527. verifyFormat("#define A(a, b, c) \\\n"
  2528. " void a##b##c()",
  2529. getLLVMStyleWithColumns(22));
  2530. verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
  2531. }
  2532. TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
  2533. EXPECT_EQ("#define A (x)", format("#define A (x)"));
  2534. EXPECT_EQ("#define A(x)", format("#define A(x)"));
  2535. }
  2536. TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
  2537. EXPECT_EQ("#define A b;", format("#define A \\\n"
  2538. " \\\n"
  2539. " b;",
  2540. getLLVMStyleWithColumns(25)));
  2541. EXPECT_EQ("#define A \\\n"
  2542. " \\\n"
  2543. " a; \\\n"
  2544. " b;",
  2545. format("#define A \\\n"
  2546. " \\\n"
  2547. " a; \\\n"
  2548. " b;",
  2549. getLLVMStyleWithColumns(11)));
  2550. EXPECT_EQ("#define A \\\n"
  2551. " a; \\\n"
  2552. " \\\n"
  2553. " b;",
  2554. format("#define A \\\n"
  2555. " a; \\\n"
  2556. " \\\n"
  2557. " b;",
  2558. getLLVMStyleWithColumns(11)));
  2559. }
  2560. TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
  2561. verifyIncompleteFormat("#define A :");
  2562. verifyFormat("#define SOMECASES \\\n"
  2563. " case 1: \\\n"
  2564. " case 2\n",
  2565. getLLVMStyleWithColumns(20));
  2566. verifyFormat("#define A template <typename T>");
  2567. verifyIncompleteFormat("#define STR(x) #x\n"
  2568. "f(STR(this_is_a_string_literal{));");
  2569. verifyFormat("#pragma omp threadprivate( \\\n"
  2570. " y)), // expected-warning",
  2571. getLLVMStyleWithColumns(28));
  2572. verifyFormat("#d, = };");
  2573. verifyFormat("#if \"a");
  2574. verifyIncompleteFormat("({\n"
  2575. "#define b \\\n"
  2576. " } \\\n"
  2577. " a\n"
  2578. "a",
  2579. getLLVMStyleWithColumns(15));
  2580. verifyFormat("#define A \\\n"
  2581. " { \\\n"
  2582. " {\n"
  2583. "#define B \\\n"
  2584. " } \\\n"
  2585. " }",
  2586. getLLVMStyleWithColumns(15));
  2587. verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
  2588. verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
  2589. verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
  2590. verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}");
  2591. }
  2592. TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
  2593. verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
  2594. EXPECT_EQ("class A : public QObject {\n"
  2595. " Q_OBJECT\n"
  2596. "\n"
  2597. " A() {}\n"
  2598. "};",
  2599. format("class A : public QObject {\n"
  2600. " Q_OBJECT\n"
  2601. "\n"
  2602. " A() {\n}\n"
  2603. "} ;"));
  2604. EXPECT_EQ("MACRO\n"
  2605. "/*static*/ int i;",
  2606. format("MACRO\n"
  2607. " /*static*/ int i;"));
  2608. EXPECT_EQ("SOME_MACRO\n"
  2609. "namespace {\n"
  2610. "void f();\n"
  2611. "}",
  2612. format("SOME_MACRO\n"
  2613. " namespace {\n"
  2614. "void f( );\n"
  2615. "}"));
  2616. // Only if the identifier contains at least 5 characters.
  2617. EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
  2618. EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
  2619. // Only if everything is upper case.
  2620. EXPECT_EQ("class A : public QObject {\n"
  2621. " Q_Object A() {}\n"
  2622. "};",
  2623. format("class A : public QObject {\n"
  2624. " Q_Object\n"
  2625. " A() {\n}\n"
  2626. "} ;"));
  2627. // Only if the next line can actually start an unwrapped line.
  2628. EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
  2629. format("SOME_WEIRD_LOG_MACRO\n"
  2630. "<< SomeThing;"));
  2631. verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
  2632. "(n, buffers))\n",
  2633. getChromiumStyle(FormatStyle::LK_Cpp));
  2634. }
  2635. TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
  2636. EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  2637. "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  2638. "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  2639. "class X {};\n"
  2640. "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  2641. "int *createScopDetectionPass() { return 0; }",
  2642. format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  2643. " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  2644. " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  2645. " class X {};\n"
  2646. " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  2647. " int *createScopDetectionPass() { return 0; }"));
  2648. // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
  2649. // braces, so that inner block is indented one level more.
  2650. EXPECT_EQ("int q() {\n"
  2651. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  2652. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  2653. " IPC_END_MESSAGE_MAP()\n"
  2654. "}",
  2655. format("int q() {\n"
  2656. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  2657. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  2658. " IPC_END_MESSAGE_MAP()\n"
  2659. "}"));
  2660. // Same inside macros.
  2661. EXPECT_EQ("#define LIST(L) \\\n"
  2662. " L(A) \\\n"
  2663. " L(B) \\\n"
  2664. " L(C)",
  2665. format("#define LIST(L) \\\n"
  2666. " L(A) \\\n"
  2667. " L(B) \\\n"
  2668. " L(C)",
  2669. getGoogleStyle()));
  2670. // These must not be recognized as macros.
  2671. EXPECT_EQ("int q() {\n"
  2672. " f(x);\n"
  2673. " f(x) {}\n"
  2674. " f(x)->g();\n"
  2675. " f(x)->*g();\n"
  2676. " f(x).g();\n"
  2677. " f(x) = x;\n"
  2678. " f(x) += x;\n"
  2679. " f(x) -= x;\n"
  2680. " f(x) *= x;\n"
  2681. " f(x) /= x;\n"
  2682. " f(x) %= x;\n"
  2683. " f(x) &= x;\n"
  2684. " f(x) |= x;\n"
  2685. " f(x) ^= x;\n"
  2686. " f(x) >>= x;\n"
  2687. " f(x) <<= x;\n"
  2688. " f(x)[y].z();\n"
  2689. " LOG(INFO) << x;\n"
  2690. " ifstream(x) >> x;\n"
  2691. "}\n",
  2692. format("int q() {\n"
  2693. " f(x)\n;\n"
  2694. " f(x)\n {}\n"
  2695. " f(x)\n->g();\n"
  2696. " f(x)\n->*g();\n"
  2697. " f(x)\n.g();\n"
  2698. " f(x)\n = x;\n"
  2699. " f(x)\n += x;\n"
  2700. " f(x)\n -= x;\n"
  2701. " f(x)\n *= x;\n"
  2702. " f(x)\n /= x;\n"
  2703. " f(x)\n %= x;\n"
  2704. " f(x)\n &= x;\n"
  2705. " f(x)\n |= x;\n"
  2706. " f(x)\n ^= x;\n"
  2707. " f(x)\n >>= x;\n"
  2708. " f(x)\n <<= x;\n"
  2709. " f(x)\n[y].z();\n"
  2710. " LOG(INFO)\n << x;\n"
  2711. " ifstream(x)\n >> x;\n"
  2712. "}\n"));
  2713. EXPECT_EQ("int q() {\n"
  2714. " F(x)\n"
  2715. " if (1) {\n"
  2716. " }\n"
  2717. " F(x)\n"
  2718. " while (1) {\n"
  2719. " }\n"
  2720. " F(x)\n"
  2721. " G(x);\n"
  2722. " F(x)\n"
  2723. " try {\n"
  2724. " Q();\n"
  2725. " } catch (...) {\n"
  2726. " }\n"
  2727. "}\n",
  2728. format("int q() {\n"
  2729. "F(x)\n"
  2730. "if (1) {}\n"
  2731. "F(x)\n"
  2732. "while (1) {}\n"
  2733. "F(x)\n"
  2734. "G(x);\n"
  2735. "F(x)\n"
  2736. "try { Q(); } catch (...) {}\n"
  2737. "}\n"));
  2738. EXPECT_EQ("class A {\n"
  2739. " A() : t(0) {}\n"
  2740. " A(int i) noexcept() : {}\n"
  2741. " A(X x)\n" // FIXME: function-level try blocks are broken.
  2742. " try : t(0) {\n"
  2743. " } catch (...) {\n"
  2744. " }\n"
  2745. "};",
  2746. format("class A {\n"
  2747. " A()\n : t(0) {}\n"
  2748. " A(int i)\n noexcept() : {}\n"
  2749. " A(X x)\n"
  2750. " try : t(0) {} catch (...) {}\n"
  2751. "};"));
  2752. EXPECT_EQ("class SomeClass {\n"
  2753. "public:\n"
  2754. " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2755. "};",
  2756. format("class SomeClass {\n"
  2757. "public:\n"
  2758. " SomeClass()\n"
  2759. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2760. "};"));
  2761. EXPECT_EQ("class SomeClass {\n"
  2762. "public:\n"
  2763. " SomeClass()\n"
  2764. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2765. "};",
  2766. format("class SomeClass {\n"
  2767. "public:\n"
  2768. " SomeClass()\n"
  2769. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2770. "};",
  2771. getLLVMStyleWithColumns(40)));
  2772. verifyFormat("MACRO(>)");
  2773. }
  2774. TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
  2775. verifyFormat("#define A \\\n"
  2776. " f({ \\\n"
  2777. " g(); \\\n"
  2778. " });",
  2779. getLLVMStyleWithColumns(11));
  2780. }
  2781. TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
  2782. EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
  2783. }
  2784. TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
  2785. verifyFormat("{\n { a #c; }\n}");
  2786. }
  2787. TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
  2788. EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
  2789. format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
  2790. EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
  2791. format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
  2792. }
  2793. TEST_F(FormatTest, EscapedNewlines) {
  2794. EXPECT_EQ(
  2795. "#define A \\\n int i; \\\n int j;",
  2796. format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
  2797. EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
  2798. EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
  2799. EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/"));
  2800. EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
  2801. }
  2802. TEST_F(FormatTest, DontCrashOnBlockComments) {
  2803. EXPECT_EQ(
  2804. "int xxxxxxxxx; /* "
  2805. "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
  2806. "zzzzzz\n"
  2807. "0*/",
  2808. format("int xxxxxxxxx; /* "
  2809. "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
  2810. "0*/"));
  2811. }
  2812. TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
  2813. verifyFormat("#define A \\\n"
  2814. " int v( \\\n"
  2815. " a); \\\n"
  2816. " int i;",
  2817. getLLVMStyleWithColumns(11));
  2818. }
  2819. TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
  2820. EXPECT_EQ(
  2821. "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2822. " \\\n"
  2823. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2824. "\n"
  2825. "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2826. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
  2827. format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2828. "\\\n"
  2829. "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2830. " \n"
  2831. " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2832. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
  2833. }
  2834. TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
  2835. EXPECT_EQ("int\n"
  2836. "#define A\n"
  2837. " a;",
  2838. format("int\n#define A\na;"));
  2839. verifyFormat("functionCallTo(\n"
  2840. " someOtherFunction(\n"
  2841. " withSomeParameters, whichInSequence,\n"
  2842. " areLongerThanALine(andAnotherCall,\n"
  2843. "#define A B\n"
  2844. " withMoreParamters,\n"
  2845. " whichStronglyInfluenceTheLayout),\n"
  2846. " andMoreParameters),\n"
  2847. " trailing);",
  2848. getLLVMStyleWithColumns(69));
  2849. verifyFormat("Foo::Foo()\n"
  2850. "#ifdef BAR\n"
  2851. " : baz(0)\n"
  2852. "#endif\n"
  2853. "{\n"
  2854. "}");
  2855. verifyFormat("void f() {\n"
  2856. " if (true)\n"
  2857. "#ifdef A\n"
  2858. " f(42);\n"
  2859. " x();\n"
  2860. "#else\n"
  2861. " g();\n"
  2862. " x();\n"
  2863. "#endif\n"
  2864. "}");
  2865. verifyFormat("void f(param1, param2,\n"
  2866. " param3,\n"
  2867. "#ifdef A\n"
  2868. " param4(param5,\n"
  2869. "#ifdef A1\n"
  2870. " param6,\n"
  2871. "#ifdef A2\n"
  2872. " param7),\n"
  2873. "#else\n"
  2874. " param8),\n"
  2875. " param9,\n"
  2876. "#endif\n"
  2877. " param10,\n"
  2878. "#endif\n"
  2879. " param11)\n"
  2880. "#else\n"
  2881. " param12)\n"
  2882. "#endif\n"
  2883. "{\n"
  2884. " x();\n"
  2885. "}",
  2886. getLLVMStyleWithColumns(28));
  2887. verifyFormat("#if 1\n"
  2888. "int i;");
  2889. verifyFormat("#if 1\n"
  2890. "#endif\n"
  2891. "#if 1\n"
  2892. "#else\n"
  2893. "#endif\n");
  2894. verifyFormat("DEBUG({\n"
  2895. " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2896. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  2897. "});\n"
  2898. "#if a\n"
  2899. "#else\n"
  2900. "#endif");
  2901. verifyIncompleteFormat("void f(\n"
  2902. "#if A\n"
  2903. " );\n"
  2904. "#else\n"
  2905. "#endif");
  2906. }
  2907. TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
  2908. verifyFormat("#endif\n"
  2909. "#if B");
  2910. }
  2911. TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
  2912. FormatStyle SingleLine = getLLVMStyle();
  2913. SingleLine.AllowShortIfStatementsOnASingleLine = true;
  2914. verifyFormat("#if 0\n"
  2915. "#elif 1\n"
  2916. "#endif\n"
  2917. "void foo() {\n"
  2918. " if (test) foo2();\n"
  2919. "}",
  2920. SingleLine);
  2921. }
  2922. TEST_F(FormatTest, LayoutBlockInsideParens) {
  2923. verifyFormat("functionCall({ int i; });");
  2924. verifyFormat("functionCall({\n"
  2925. " int i;\n"
  2926. " int j;\n"
  2927. "});");
  2928. verifyFormat("functionCall(\n"
  2929. " {\n"
  2930. " int i;\n"
  2931. " int j;\n"
  2932. " },\n"
  2933. " aaaa, bbbb, cccc);");
  2934. verifyFormat("functionA(functionB({\n"
  2935. " int i;\n"
  2936. " int j;\n"
  2937. " }),\n"
  2938. " aaaa, bbbb, cccc);");
  2939. verifyFormat("functionCall(\n"
  2940. " {\n"
  2941. " int i;\n"
  2942. " int j;\n"
  2943. " },\n"
  2944. " aaaa, bbbb, // comment\n"
  2945. " cccc);");
  2946. verifyFormat("functionA(functionB({\n"
  2947. " int i;\n"
  2948. " int j;\n"
  2949. " }),\n"
  2950. " aaaa, bbbb, // comment\n"
  2951. " cccc);");
  2952. verifyFormat("functionCall(aaaa, bbbb, { int i; });");
  2953. verifyFormat("functionCall(aaaa, bbbb, {\n"
  2954. " int i;\n"
  2955. " int j;\n"
  2956. "});");
  2957. verifyFormat(
  2958. "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
  2959. " {\n"
  2960. " int i; // break\n"
  2961. " },\n"
  2962. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  2963. " ccccccccccccccccc));");
  2964. verifyFormat("DEBUG({\n"
  2965. " if (a)\n"
  2966. " f();\n"
  2967. "});");
  2968. }
  2969. TEST_F(FormatTest, LayoutBlockInsideStatement) {
  2970. EXPECT_EQ("SOME_MACRO { int i; }\n"
  2971. "int i;",
  2972. format(" SOME_MACRO {int i;} int i;"));
  2973. }
  2974. TEST_F(FormatTest, LayoutNestedBlocks) {
  2975. verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
  2976. " struct s {\n"
  2977. " int i;\n"
  2978. " };\n"
  2979. " s kBitsToOs[] = {{10}};\n"
  2980. " for (int i = 0; i < 10; ++i)\n"
  2981. " return;\n"
  2982. "}");
  2983. verifyFormat("call(parameter, {\n"
  2984. " something();\n"
  2985. " // Comment using all columns.\n"
  2986. " somethingelse();\n"
  2987. "});",
  2988. getLLVMStyleWithColumns(40));
  2989. verifyFormat("DEBUG( //\n"
  2990. " { f(); }, a);");
  2991. verifyFormat("DEBUG( //\n"
  2992. " {\n"
  2993. " f(); //\n"
  2994. " },\n"
  2995. " a);");
  2996. EXPECT_EQ("call(parameter, {\n"
  2997. " something();\n"
  2998. " // Comment too\n"
  2999. " // looooooooooong.\n"
  3000. " somethingElse();\n"
  3001. "});",
  3002. format("call(parameter, {\n"
  3003. " something();\n"
  3004. " // Comment too looooooooooong.\n"
  3005. " somethingElse();\n"
  3006. "});",
  3007. getLLVMStyleWithColumns(29)));
  3008. EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });"));
  3009. EXPECT_EQ("DEBUG({ // comment\n"
  3010. " int i;\n"
  3011. "});",
  3012. format("DEBUG({ // comment\n"
  3013. "int i;\n"
  3014. "});"));
  3015. EXPECT_EQ("DEBUG({\n"
  3016. " int i;\n"
  3017. "\n"
  3018. " // comment\n"
  3019. " int j;\n"
  3020. "});",
  3021. format("DEBUG({\n"
  3022. " int i;\n"
  3023. "\n"
  3024. " // comment\n"
  3025. " int j;\n"
  3026. "});"));
  3027. verifyFormat("DEBUG({\n"
  3028. " if (a)\n"
  3029. " return;\n"
  3030. "});");
  3031. verifyGoogleFormat("DEBUG({\n"
  3032. " if (a) return;\n"
  3033. "});");
  3034. FormatStyle Style = getGoogleStyle();
  3035. Style.ColumnLimit = 45;
  3036. verifyFormat("Debug(aaaaa,\n"
  3037. " {\n"
  3038. " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
  3039. " },\n"
  3040. " a);",
  3041. Style);
  3042. verifyFormat("SomeFunction({MACRO({ return output; }), b});");
  3043. verifyNoCrash("^{v^{a}}");
  3044. }
  3045. TEST_F(FormatTest, FormatNestedBlocksInMacros) {
  3046. EXPECT_EQ("#define MACRO() \\\n"
  3047. " Debug(aaa, /* force line break */ \\\n"
  3048. " { \\\n"
  3049. " int i; \\\n"
  3050. " int j; \\\n"
  3051. " })",
  3052. format("#define MACRO() Debug(aaa, /* force line break */ \\\n"
  3053. " { int i; int j; })",
  3054. getGoogleStyle()));
  3055. EXPECT_EQ("#define A \\\n"
  3056. " [] { \\\n"
  3057. " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
  3058. " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
  3059. " }",
  3060. format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
  3061. "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
  3062. getGoogleStyle()));
  3063. }
  3064. TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
  3065. EXPECT_EQ("{}", format("{}"));
  3066. verifyFormat("enum E {};");
  3067. verifyFormat("enum E {}");
  3068. }
  3069. TEST_F(FormatTest, FormatBeginBlockEndMacros) {
  3070. FormatStyle Style = getLLVMStyle();
  3071. Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
  3072. Style.MacroBlockEnd = "^[A-Z_]+_END$";
  3073. verifyFormat("FOO_BEGIN\n"
  3074. " FOO_ENTRY\n"
  3075. "FOO_END", Style);
  3076. verifyFormat("FOO_BEGIN\n"
  3077. " NESTED_FOO_BEGIN\n"
  3078. " NESTED_FOO_ENTRY\n"
  3079. " NESTED_FOO_END\n"
  3080. "FOO_END", Style);
  3081. verifyFormat("FOO_BEGIN(Foo, Bar)\n"
  3082. " int x;\n"
  3083. " x = 1;\n"
  3084. "FOO_END(Baz)", Style);
  3085. }
  3086. //===----------------------------------------------------------------------===//
  3087. // Line break tests.
  3088. //===----------------------------------------------------------------------===//
  3089. TEST_F(FormatTest, PreventConfusingIndents) {
  3090. verifyFormat(
  3091. "void f() {\n"
  3092. " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
  3093. " parameter, parameter, parameter)),\n"
  3094. " SecondLongCall(parameter));\n"
  3095. "}");
  3096. verifyFormat(
  3097. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3098. " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3099. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3100. " aaaaaaaaaaaaaaaaaaaaaaaa);");
  3101. verifyFormat(
  3102. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3103. " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
  3104. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  3105. " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
  3106. verifyFormat(
  3107. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  3108. " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
  3109. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
  3110. " aaaaaaaaaaaaaaaaaaaaaaaa>;");
  3111. verifyFormat("int a = bbbb && ccc && fffff(\n"
  3112. "#define A Just forcing a new line\n"
  3113. " ddd);");
  3114. }
  3115. TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
  3116. verifyFormat(
  3117. "bool aaaaaaa =\n"
  3118. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
  3119. " bbbbbbbb();");
  3120. verifyFormat(
  3121. "bool aaaaaaa =\n"
  3122. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
  3123. " bbbbbbbb();");
  3124. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  3125. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
  3126. " ccccccccc == ddddddddddd;");
  3127. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  3128. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
  3129. " ccccccccc == ddddddddddd;");
  3130. verifyFormat(
  3131. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  3132. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
  3133. " ccccccccc == ddddddddddd;");
  3134. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  3135. " aaaaaa) &&\n"
  3136. " bbbbbb && cccccc;");
  3137. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  3138. " aaaaaa) >>\n"
  3139. " bbbbbb;");
  3140. verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
  3141. " SourceMgr.getSpellingColumnNumber(\n"
  3142. " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
  3143. " 1);");
  3144. verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3145. " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
  3146. " cccccc) {\n}");
  3147. verifyFormat("b = a &&\n"
  3148. " // Comment\n"
  3149. " b.c && d;");
  3150. // If the LHS of a comparison is not a binary expression itself, the
  3151. // additional linebreak confuses many people.
  3152. verifyFormat(
  3153. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3154. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
  3155. "}");
  3156. verifyFormat(
  3157. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3158. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  3159. "}");
  3160. verifyFormat(
  3161. "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
  3162. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  3163. "}");
  3164. // Even explicit parentheses stress the precedence enough to make the
  3165. // additional break unnecessary.
  3166. verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3167. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  3168. "}");
  3169. // This cases is borderline, but with the indentation it is still readable.
  3170. verifyFormat(
  3171. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3172. " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3173. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  3174. "}",
  3175. getLLVMStyleWithColumns(75));
  3176. // If the LHS is a binary expression, we should still use the additional break
  3177. // as otherwise the formatting hides the operator precedence.
  3178. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3179. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3180. " 5) {\n"
  3181. "}");
  3182. FormatStyle OnePerLine = getLLVMStyle();
  3183. OnePerLine.BinPackParameters = false;
  3184. verifyFormat(
  3185. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3186. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3187. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
  3188. OnePerLine);
  3189. }
  3190. TEST_F(FormatTest, ExpressionIndentation) {
  3191. verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3192. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3193. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3194. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3195. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
  3196. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
  3197. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3198. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
  3199. " ccccccccccccccccccccccccccccccccccccccccc;");
  3200. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3201. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3202. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3203. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3204. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3205. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3206. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3207. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3208. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3209. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3210. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3211. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3212. verifyFormat("if () {\n"
  3213. "} else if (aaaaa &&\n"
  3214. " bbbbb > // break\n"
  3215. " ccccc) {\n"
  3216. "}");
  3217. // Presence of a trailing comment used to change indentation of b.
  3218. verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
  3219. " b;\n"
  3220. "return aaaaaaaaaaaaaaaaaaa +\n"
  3221. " b; //",
  3222. getLLVMStyleWithColumns(30));
  3223. }
  3224. TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
  3225. // Not sure what the best system is here. Like this, the LHS can be found
  3226. // immediately above an operator (everything with the same or a higher
  3227. // indent). The RHS is aligned right of the operator and so compasses
  3228. // everything until something with the same indent as the operator is found.
  3229. // FIXME: Is this a good system?
  3230. FormatStyle Style = getLLVMStyle();
  3231. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  3232. verifyFormat(
  3233. "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3234. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3235. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3236. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3237. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3238. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3239. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3240. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3241. " > ccccccccccccccccccccccccccccccccccccccccc;",
  3242. Style);
  3243. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3244. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3245. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3246. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3247. Style);
  3248. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3249. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3250. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3251. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3252. Style);
  3253. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3254. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3255. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3256. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3257. Style);
  3258. verifyFormat("if () {\n"
  3259. "} else if (aaaaa\n"
  3260. " && bbbbb // break\n"
  3261. " > ccccc) {\n"
  3262. "}",
  3263. Style);
  3264. verifyFormat("return (a)\n"
  3265. " // comment\n"
  3266. " + b;",
  3267. Style);
  3268. verifyFormat(
  3269. "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3270. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3271. " + cc;",
  3272. Style);
  3273. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3274. " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  3275. Style);
  3276. // Forced by comments.
  3277. verifyFormat(
  3278. "unsigned ContentSize =\n"
  3279. " sizeof(int16_t) // DWARF ARange version number\n"
  3280. " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
  3281. " + sizeof(int8_t) // Pointer Size (in bytes)\n"
  3282. " + sizeof(int8_t); // Segment Size (in bytes)");
  3283. verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
  3284. " == boost::fusion::at_c<1>(iiii).second;",
  3285. Style);
  3286. Style.ColumnLimit = 60;
  3287. verifyFormat("zzzzzzzzzz\n"
  3288. " = bbbbbbbbbbbbbbbbb\n"
  3289. " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3290. Style);
  3291. }
  3292. TEST_F(FormatTest, NoOperandAlignment) {
  3293. FormatStyle Style = getLLVMStyle();
  3294. Style.AlignOperands = false;
  3295. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
  3296. verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3297. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3298. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3299. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3300. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3301. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3302. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3303. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3304. " > ccccccccccccccccccccccccccccccccccccccccc;",
  3305. Style);
  3306. verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3307. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3308. " + cc;",
  3309. Style);
  3310. verifyFormat("int a = aa\n"
  3311. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3312. " * cccccccccccccccccccccccccccccccccccc;",
  3313. Style);
  3314. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  3315. verifyFormat("return (a > b\n"
  3316. " // comment1\n"
  3317. " // comment2\n"
  3318. " || c);",
  3319. Style);
  3320. }
  3321. TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
  3322. FormatStyle Style = getLLVMStyle();
  3323. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
  3324. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3325. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3326. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
  3327. Style);
  3328. }
  3329. TEST_F(FormatTest, ConstructorInitializers) {
  3330. verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
  3331. verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
  3332. getLLVMStyleWithColumns(45));
  3333. verifyFormat("Constructor()\n"
  3334. " : Inttializer(FitsOnTheLine) {}",
  3335. getLLVMStyleWithColumns(44));
  3336. verifyFormat("Constructor()\n"
  3337. " : Inttializer(FitsOnTheLine) {}",
  3338. getLLVMStyleWithColumns(43));
  3339. verifyFormat("template <typename T>\n"
  3340. "Constructor() : Initializer(FitsOnTheLine) {}",
  3341. getLLVMStyleWithColumns(45));
  3342. verifyFormat(
  3343. "SomeClass::Constructor()\n"
  3344. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  3345. verifyFormat(
  3346. "SomeClass::Constructor()\n"
  3347. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3348. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
  3349. verifyFormat(
  3350. "SomeClass::Constructor()\n"
  3351. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3352. " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  3353. verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3354. " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3355. " : aaaaaaaaaa(aaaaaa) {}");
  3356. verifyFormat("Constructor()\n"
  3357. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3358. " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3359. " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3360. " aaaaaaaaaaaaaaaaaaaaaaa() {}");
  3361. verifyFormat("Constructor()\n"
  3362. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3363. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3364. verifyFormat("Constructor(int Parameter = 0)\n"
  3365. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
  3366. " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
  3367. verifyFormat("Constructor()\n"
  3368. " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
  3369. "}",
  3370. getLLVMStyleWithColumns(60));
  3371. verifyFormat("Constructor()\n"
  3372. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3373. " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
  3374. // Here a line could be saved by splitting the second initializer onto two
  3375. // lines, but that is not desirable.
  3376. verifyFormat("Constructor()\n"
  3377. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3378. " aaaaaaaaaaa(aaaaaaaaaaa),\n"
  3379. " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3380. FormatStyle OnePerLine = getLLVMStyle();
  3381. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  3382. OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
  3383. verifyFormat("SomeClass::Constructor()\n"
  3384. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3385. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3386. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  3387. OnePerLine);
  3388. verifyFormat("SomeClass::Constructor()\n"
  3389. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
  3390. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3391. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  3392. OnePerLine);
  3393. verifyFormat("MyClass::MyClass(int var)\n"
  3394. " : some_var_(var), // 4 space indent\n"
  3395. " some_other_var_(var + 1) { // lined up\n"
  3396. "}",
  3397. OnePerLine);
  3398. verifyFormat("Constructor()\n"
  3399. " : aaaaa(aaaaaa),\n"
  3400. " aaaaa(aaaaaa),\n"
  3401. " aaaaa(aaaaaa),\n"
  3402. " aaaaa(aaaaaa),\n"
  3403. " aaaaa(aaaaaa) {}",
  3404. OnePerLine);
  3405. verifyFormat("Constructor()\n"
  3406. " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
  3407. " aaaaaaaaaaaaaaaaaaaaaa) {}",
  3408. OnePerLine);
  3409. OnePerLine.BinPackParameters = false;
  3410. verifyFormat(
  3411. "Constructor()\n"
  3412. " : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3413. " aaaaaaaaaaa().aaa(),\n"
  3414. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3415. OnePerLine);
  3416. OnePerLine.ColumnLimit = 60;
  3417. verifyFormat("Constructor()\n"
  3418. " : aaaaaaaaaaaaaaaaaaaa(a),\n"
  3419. " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
  3420. OnePerLine);
  3421. EXPECT_EQ("Constructor()\n"
  3422. " : // Comment forcing unwanted break.\n"
  3423. " aaaa(aaaa) {}",
  3424. format("Constructor() :\n"
  3425. " // Comment forcing unwanted break.\n"
  3426. " aaaa(aaaa) {}"));
  3427. }
  3428. TEST_F(FormatTest, MemoizationTests) {
  3429. // This breaks if the memoization lookup does not take \c Indent and
  3430. // \c LastSpace into account.
  3431. verifyFormat(
  3432. "extern CFRunLoopTimerRef\n"
  3433. "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
  3434. " CFTimeInterval interval, CFOptionFlags flags,\n"
  3435. " CFIndex order, CFRunLoopTimerCallBack callout,\n"
  3436. " CFRunLoopTimerContext *context) {}");
  3437. // Deep nesting somewhat works around our memoization.
  3438. verifyFormat(
  3439. "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3440. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3441. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3442. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3443. " aaaaa())))))))))))))))))))))))))))))))))))))));",
  3444. getLLVMStyleWithColumns(65));
  3445. verifyFormat(
  3446. "aaaaa(\n"
  3447. " aaaaa,\n"
  3448. " aaaaa(\n"
  3449. " aaaaa,\n"
  3450. " aaaaa(\n"
  3451. " aaaaa,\n"
  3452. " aaaaa(\n"
  3453. " aaaaa,\n"
  3454. " aaaaa(\n"
  3455. " aaaaa,\n"
  3456. " aaaaa(\n"
  3457. " aaaaa,\n"
  3458. " aaaaa(\n"
  3459. " aaaaa,\n"
  3460. " aaaaa(\n"
  3461. " aaaaa,\n"
  3462. " aaaaa(\n"
  3463. " aaaaa,\n"
  3464. " aaaaa(\n"
  3465. " aaaaa,\n"
  3466. " aaaaa(\n"
  3467. " aaaaa,\n"
  3468. " aaaaa(\n"
  3469. " aaaaa,\n"
  3470. " aaaaa))))))))))));",
  3471. getLLVMStyleWithColumns(65));
  3472. verifyFormat(
  3473. "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"
  3474. " a),\n"
  3475. " a),\n"
  3476. " a),\n"
  3477. " a),\n"
  3478. " a),\n"
  3479. " a),\n"
  3480. " a),\n"
  3481. " a),\n"
  3482. " a),\n"
  3483. " a),\n"
  3484. " a),\n"
  3485. " a),\n"
  3486. " a),\n"
  3487. " a),\n"
  3488. " a),\n"
  3489. " a),\n"
  3490. " a)",
  3491. getLLVMStyleWithColumns(65));
  3492. // This test takes VERY long when memoization is broken.
  3493. FormatStyle OnePerLine = getLLVMStyle();
  3494. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  3495. OnePerLine.BinPackParameters = false;
  3496. std::string input = "Constructor()\n"
  3497. " : aaaa(a,\n";
  3498. for (unsigned i = 0, e = 80; i != e; ++i) {
  3499. input += " a,\n";
  3500. }
  3501. input += " a) {}";
  3502. verifyFormat(input, OnePerLine);
  3503. }
  3504. TEST_F(FormatTest, BreaksAsHighAsPossible) {
  3505. verifyFormat(
  3506. "void f() {\n"
  3507. " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
  3508. " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
  3509. " f();\n"
  3510. "}");
  3511. verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
  3512. " Intervals[i - 1].getRange().getLast()) {\n}");
  3513. }
  3514. TEST_F(FormatTest, BreaksFunctionDeclarations) {
  3515. // Principially, we break function declarations in a certain order:
  3516. // 1) break amongst arguments.
  3517. verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
  3518. " Cccccccccccccc cccccccccccccc);");
  3519. verifyFormat("template <class TemplateIt>\n"
  3520. "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
  3521. " TemplateIt *stop) {}");
  3522. // 2) break after return type.
  3523. verifyFormat(
  3524. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3525. "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
  3526. getGoogleStyle());
  3527. // 3) break after (.
  3528. verifyFormat(
  3529. "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
  3530. " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
  3531. getGoogleStyle());
  3532. // 4) break before after nested name specifiers.
  3533. verifyFormat(
  3534. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3535. "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
  3536. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
  3537. getGoogleStyle());
  3538. // However, there are exceptions, if a sufficient amount of lines can be
  3539. // saved.
  3540. // FIXME: The precise cut-offs wrt. the number of saved lines might need some
  3541. // more adjusting.
  3542. verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  3543. " Cccccccccccccc cccccccccc,\n"
  3544. " Cccccccccccccc cccccccccc,\n"
  3545. " Cccccccccccccc cccccccccc,\n"
  3546. " Cccccccccccccc cccccccccc);");
  3547. verifyFormat(
  3548. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3549. "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3550. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3551. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
  3552. getGoogleStyle());
  3553. verifyFormat(
  3554. "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  3555. " Cccccccccccccc cccccccccc,\n"
  3556. " Cccccccccccccc cccccccccc,\n"
  3557. " Cccccccccccccc cccccccccc,\n"
  3558. " Cccccccccccccc cccccccccc,\n"
  3559. " Cccccccccccccc cccccccccc,\n"
  3560. " Cccccccccccccc cccccccccc);");
  3561. verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3562. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3563. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3564. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3565. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
  3566. // Break after multi-line parameters.
  3567. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3568. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3569. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3570. " bbbb bbbb);");
  3571. verifyFormat("void SomeLoooooooooooongFunction(\n"
  3572. " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  3573. " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3574. " int bbbbbbbbbbbbb);");
  3575. // Treat overloaded operators like other functions.
  3576. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3577. "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
  3578. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3579. "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
  3580. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3581. "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
  3582. verifyGoogleFormat(
  3583. "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
  3584. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  3585. verifyGoogleFormat(
  3586. "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
  3587. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  3588. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3589. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  3590. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
  3591. "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  3592. verifyGoogleFormat(
  3593. "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
  3594. "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3595. " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
  3596. FormatStyle Style = getLLVMStyle();
  3597. Style.PointerAlignment = FormatStyle::PAS_Left;
  3598. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3599. " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
  3600. Style);
  3601. verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
  3602. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3603. Style);
  3604. }
  3605. TEST_F(FormatTest, TrailingReturnType) {
  3606. verifyFormat("auto foo() -> int;\n");
  3607. verifyFormat("struct S {\n"
  3608. " auto bar() const -> int;\n"
  3609. "};");
  3610. verifyFormat("template <size_t Order, typename T>\n"
  3611. "auto load_img(const std::string &filename)\n"
  3612. " -> alias::tensor<Order, T, mem::tag::cpu> {}");
  3613. verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
  3614. " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
  3615. verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
  3616. verifyFormat("template <typename T>\n"
  3617. "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
  3618. " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
  3619. // Not trailing return types.
  3620. verifyFormat("void f() { auto a = b->c(); }");
  3621. }
  3622. TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
  3623. // Avoid breaking before trailing 'const' or other trailing annotations, if
  3624. // they are not function-like.
  3625. FormatStyle Style = getGoogleStyle();
  3626. Style.ColumnLimit = 47;
  3627. verifyFormat("void someLongFunction(\n"
  3628. " int someLoooooooooooooongParameter) const {\n}",
  3629. getLLVMStyleWithColumns(47));
  3630. verifyFormat("LoooooongReturnType\n"
  3631. "someLoooooooongFunction() const {}",
  3632. getLLVMStyleWithColumns(47));
  3633. verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
  3634. " const {}",
  3635. Style);
  3636. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3637. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
  3638. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3639. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
  3640. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3641. " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
  3642. verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
  3643. " aaaaaaaaaaa aaaaa) const override;");
  3644. verifyGoogleFormat(
  3645. "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3646. " const override;");
  3647. // Even if the first parameter has to be wrapped.
  3648. verifyFormat("void someLongFunction(\n"
  3649. " int someLongParameter) const {}",
  3650. getLLVMStyleWithColumns(46));
  3651. verifyFormat("void someLongFunction(\n"
  3652. " int someLongParameter) const {}",
  3653. Style);
  3654. verifyFormat("void someLongFunction(\n"
  3655. " int someLongParameter) override {}",
  3656. Style);
  3657. verifyFormat("void someLongFunction(\n"
  3658. " int someLongParameter) OVERRIDE {}",
  3659. Style);
  3660. verifyFormat("void someLongFunction(\n"
  3661. " int someLongParameter) final {}",
  3662. Style);
  3663. verifyFormat("void someLongFunction(\n"
  3664. " int someLongParameter) FINAL {}",
  3665. Style);
  3666. verifyFormat("void someLongFunction(\n"
  3667. " int parameter) const override {}",
  3668. Style);
  3669. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  3670. verifyFormat("void someLongFunction(\n"
  3671. " int someLongParameter) const\n"
  3672. "{\n"
  3673. "}",
  3674. Style);
  3675. // Unless these are unknown annotations.
  3676. verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
  3677. " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3678. " LONG_AND_UGLY_ANNOTATION;");
  3679. // Breaking before function-like trailing annotations is fine to keep them
  3680. // close to their arguments.
  3681. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3682. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  3683. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  3684. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  3685. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  3686. " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
  3687. verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
  3688. " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
  3689. verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
  3690. verifyFormat(
  3691. "void aaaaaaaaaaaaaaaaaa()\n"
  3692. " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3693. " aaaaaaaaaaaaaaaaaaaaaaaaa));");
  3694. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3695. " __attribute__((unused));");
  3696. verifyGoogleFormat(
  3697. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3698. " GUARDED_BY(aaaaaaaaaaaa);");
  3699. verifyGoogleFormat(
  3700. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3701. " GUARDED_BY(aaaaaaaaaaaa);");
  3702. verifyGoogleFormat(
  3703. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
  3704. " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3705. verifyGoogleFormat(
  3706. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
  3707. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3708. }
  3709. TEST_F(FormatTest, FunctionAnnotations) {
  3710. verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
  3711. "int OldFunction(const string &parameter) {}");
  3712. verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
  3713. "string OldFunction(const string &parameter) {}");
  3714. verifyFormat("template <typename T>\n"
  3715. "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
  3716. "string OldFunction(const string &parameter) {}");
  3717. // Not function annotations.
  3718. verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3719. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
  3720. verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
  3721. " ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
  3722. }
  3723. TEST_F(FormatTest, BreaksDesireably) {
  3724. verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  3725. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  3726. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
  3727. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3728. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  3729. "}");
  3730. verifyFormat(
  3731. "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3732. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3733. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3734. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3735. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3736. verifyFormat(
  3737. "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3738. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  3739. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3740. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
  3741. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3742. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3743. verifyFormat(
  3744. "void f() {\n"
  3745. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
  3746. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3747. "}");
  3748. verifyFormat(
  3749. "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3750. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3751. verifyFormat(
  3752. "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3753. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3754. verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3755. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3756. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3757. // Indent consistently independent of call expression and unary operator.
  3758. verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3759. " dddddddddddddddddddddddddddddd));");
  3760. verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3761. " dddddddddddddddddddddddddddddd));");
  3762. verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
  3763. " dddddddddddddddddddddddddddddd));");
  3764. // This test case breaks on an incorrect memoization, i.e. an optimization not
  3765. // taking into account the StopAt value.
  3766. verifyFormat(
  3767. "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3768. " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3769. " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3770. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3771. verifyFormat("{\n {\n {\n"
  3772. " Annotation.SpaceRequiredBefore =\n"
  3773. " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
  3774. " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
  3775. " }\n }\n}");
  3776. // Break on an outer level if there was a break on an inner level.
  3777. EXPECT_EQ("f(g(h(a, // comment\n"
  3778. " b, c),\n"
  3779. " d, e),\n"
  3780. " x, y);",
  3781. format("f(g(h(a, // comment\n"
  3782. " b, c), d, e), x, y);"));
  3783. // Prefer breaking similar line breaks.
  3784. verifyFormat(
  3785. "const int kTrackingOptions = NSTrackingMouseMoved |\n"
  3786. " NSTrackingMouseEnteredAndExited |\n"
  3787. " NSTrackingActiveAlways;");
  3788. }
  3789. TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
  3790. FormatStyle NoBinPacking = getGoogleStyle();
  3791. NoBinPacking.BinPackParameters = false;
  3792. NoBinPacking.BinPackArguments = true;
  3793. verifyFormat("void f() {\n"
  3794. " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
  3795. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3796. "}",
  3797. NoBinPacking);
  3798. verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
  3799. " int aaaaaaaaaaaaaaaaaaaa,\n"
  3800. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3801. NoBinPacking);
  3802. NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
  3803. verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3804. " vector<int> bbbbbbbbbbbbbbb);",
  3805. NoBinPacking);
  3806. // FIXME: This behavior difference is probably not wanted. However, currently
  3807. // we cannot distinguish BreakBeforeParameter being set because of the wrapped
  3808. // template arguments from BreakBeforeParameter being set because of the
  3809. // one-per-line formatting.
  3810. verifyFormat(
  3811. "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3812. " aaaaaaaaaa> aaaaaaaaaa);",
  3813. NoBinPacking);
  3814. verifyFormat(
  3815. "void fffffffffff(\n"
  3816. " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
  3817. " aaaaaaaaaa);");
  3818. }
  3819. TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
  3820. FormatStyle NoBinPacking = getGoogleStyle();
  3821. NoBinPacking.BinPackParameters = false;
  3822. NoBinPacking.BinPackArguments = false;
  3823. verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
  3824. " aaaaaaaaaaaaaaaaaaaa,\n"
  3825. " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
  3826. NoBinPacking);
  3827. verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
  3828. " aaaaaaaaaaaaa,\n"
  3829. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
  3830. NoBinPacking);
  3831. verifyFormat(
  3832. "aaaaaaaa(aaaaaaaaaaaaa,\n"
  3833. " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3834. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  3835. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3836. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
  3837. NoBinPacking);
  3838. verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  3839. " .aaaaaaaaaaaaaaaaaa();",
  3840. NoBinPacking);
  3841. verifyFormat("void f() {\n"
  3842. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3843. " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
  3844. "}",
  3845. NoBinPacking);
  3846. verifyFormat(
  3847. "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3848. " aaaaaaaaaaaa,\n"
  3849. " aaaaaaaaaaaa);",
  3850. NoBinPacking);
  3851. verifyFormat(
  3852. "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
  3853. " ddddddddddddddddddddddddddddd),\n"
  3854. " test);",
  3855. NoBinPacking);
  3856. verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3857. " aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3858. " aaaaaaaaaaaaaaaaaaaaaaa>\n"
  3859. " aaaaaaaaaaaaaaaaaa;",
  3860. NoBinPacking);
  3861. verifyFormat("a(\"a\"\n"
  3862. " \"a\",\n"
  3863. " a);");
  3864. NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
  3865. verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
  3866. " aaaaaaaaa,\n"
  3867. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3868. NoBinPacking);
  3869. verifyFormat(
  3870. "void f() {\n"
  3871. " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  3872. " .aaaaaaa();\n"
  3873. "}",
  3874. NoBinPacking);
  3875. verifyFormat(
  3876. "template <class SomeType, class SomeOtherType>\n"
  3877. "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
  3878. NoBinPacking);
  3879. }
  3880. TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
  3881. FormatStyle Style = getLLVMStyleWithColumns(15);
  3882. Style.ExperimentalAutoDetectBinPacking = true;
  3883. EXPECT_EQ("aaa(aaaa,\n"
  3884. " aaaa,\n"
  3885. " aaaa);\n"
  3886. "aaa(aaaa,\n"
  3887. " aaaa,\n"
  3888. " aaaa);",
  3889. format("aaa(aaaa,\n" // one-per-line
  3890. " aaaa,\n"
  3891. " aaaa );\n"
  3892. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  3893. Style));
  3894. EXPECT_EQ("aaa(aaaa, aaaa,\n"
  3895. " aaaa);\n"
  3896. "aaa(aaaa, aaaa,\n"
  3897. " aaaa);",
  3898. format("aaa(aaaa, aaaa,\n" // bin-packed
  3899. " aaaa );\n"
  3900. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  3901. Style));
  3902. }
  3903. TEST_F(FormatTest, FormatsBuilderPattern) {
  3904. verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
  3905. " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
  3906. " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
  3907. " .StartsWith(\".init\", ORDER_INIT)\n"
  3908. " .StartsWith(\".fini\", ORDER_FINI)\n"
  3909. " .StartsWith(\".hash\", ORDER_HASH)\n"
  3910. " .Default(ORDER_TEXT);\n");
  3911. verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
  3912. " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
  3913. verifyFormat(
  3914. "aaaaaaa->aaaaaaa\n"
  3915. " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3916. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3917. " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
  3918. verifyFormat(
  3919. "aaaaaaa->aaaaaaa\n"
  3920. " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3921. " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
  3922. verifyFormat(
  3923. "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
  3924. " aaaaaaaaaaaaaa);");
  3925. verifyFormat(
  3926. "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
  3927. " aaaaaa->aaaaaaaaaaaa()\n"
  3928. " ->aaaaaaaaaaaaaaaa(\n"
  3929. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3930. " ->aaaaaaaaaaaaaaaaa();");
  3931. verifyGoogleFormat(
  3932. "void f() {\n"
  3933. " someo->Add((new util::filetools::Handler(dir))\n"
  3934. " ->OnEvent1(NewPermanentCallback(\n"
  3935. " this, &HandlerHolderClass::EventHandlerCBA))\n"
  3936. " ->OnEvent2(NewPermanentCallback(\n"
  3937. " this, &HandlerHolderClass::EventHandlerCBB))\n"
  3938. " ->OnEvent3(NewPermanentCallback(\n"
  3939. " this, &HandlerHolderClass::EventHandlerCBC))\n"
  3940. " ->OnEvent5(NewPermanentCallback(\n"
  3941. " this, &HandlerHolderClass::EventHandlerCBD))\n"
  3942. " ->OnEvent6(NewPermanentCallback(\n"
  3943. " this, &HandlerHolderClass::EventHandlerCBE)));\n"
  3944. "}");
  3945. verifyFormat(
  3946. "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
  3947. verifyFormat("aaaaaaaaaaaaaaa()\n"
  3948. " .aaaaaaaaaaaaaaa()\n"
  3949. " .aaaaaaaaaaaaaaa()\n"
  3950. " .aaaaaaaaaaaaaaa()\n"
  3951. " .aaaaaaaaaaaaaaa();");
  3952. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3953. " .aaaaaaaaaaaaaaa()\n"
  3954. " .aaaaaaaaaaaaaaa()\n"
  3955. " .aaaaaaaaaaaaaaa();");
  3956. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3957. " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3958. " .aaaaaaaaaaaaaaa();");
  3959. verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3960. " ->aaaaaaaaaaaaaae(0)\n"
  3961. " ->aaaaaaaaaaaaaaa();");
  3962. // Don't linewrap after very short segments.
  3963. verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3964. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3965. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3966. verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3967. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3968. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3969. verifyFormat("aaa()\n"
  3970. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3971. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3972. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3973. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  3974. " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3975. " .has<bbbbbbbbbbbbbbbbbbbbb>();");
  3976. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  3977. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  3978. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
  3979. // Prefer not to break after empty parentheses.
  3980. verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
  3981. " First->LastNewlineOffset);");
  3982. // Prefer not to create "hanging" indents.
  3983. verifyFormat(
  3984. "return !soooooooooooooome_map\n"
  3985. " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3986. " .second;");
  3987. verifyFormat(
  3988. "return aaaaaaaaaaaaaaaa\n"
  3989. " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
  3990. " .aaaa(aaaaaaaaaaaaaa);");
  3991. // No hanging indent here.
  3992. verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
  3993. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3994. verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
  3995. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3996. verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
  3997. " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3998. getLLVMStyleWithColumns(60));
  3999. verifyFormat("aaaaaaaaaaaaaaaaaa\n"
  4000. " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
  4001. " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4002. getLLVMStyleWithColumns(59));
  4003. verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4004. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4005. " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4006. }
  4007. TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
  4008. verifyFormat(
  4009. "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  4010. " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
  4011. verifyFormat(
  4012. "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
  4013. " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
  4014. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  4015. " ccccccccccccccccccccccccc) {\n}");
  4016. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
  4017. " ccccccccccccccccccccccccc) {\n}");
  4018. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  4019. " ccccccccccccccccccccccccc) {\n}");
  4020. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
  4021. " ccccccccccccccccccccccccc) {\n}");
  4022. verifyFormat(
  4023. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
  4024. " ccccccccccccccccccccccccc) {\n}");
  4025. verifyFormat(
  4026. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
  4027. " ccccccccccccccccccccccccc) {\n}");
  4028. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
  4029. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
  4030. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
  4031. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  4032. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
  4033. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
  4034. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
  4035. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  4036. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
  4037. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
  4038. " aaaaaaaaaaaaaaa != aa) {\n}");
  4039. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
  4040. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
  4041. " aaaaaaaaaaaaaaa != aa) {\n}");
  4042. }
  4043. TEST_F(FormatTest, BreaksAfterAssignments) {
  4044. verifyFormat(
  4045. "unsigned Cost =\n"
  4046. " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
  4047. " SI->getPointerAddressSpaceee());\n");
  4048. verifyFormat(
  4049. "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
  4050. " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
  4051. verifyFormat(
  4052. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
  4053. " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
  4054. verifyFormat("unsigned OriginalStartColumn =\n"
  4055. " SourceMgr.getSpellingColumnNumber(\n"
  4056. " Current.FormatTok.getStartOfNonWhitespace()) -\n"
  4057. " 1;");
  4058. }
  4059. TEST_F(FormatTest, AlignsAfterAssignments) {
  4060. verifyFormat(
  4061. "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4062. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  4063. verifyFormat(
  4064. "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4065. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  4066. verifyFormat(
  4067. "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4068. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  4069. verifyFormat(
  4070. "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4071. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  4072. verifyFormat(
  4073. "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4074. " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4075. " aaaaaaaaaaaaaaaaaaaaaaaa;");
  4076. }
  4077. TEST_F(FormatTest, AlignsAfterReturn) {
  4078. verifyFormat(
  4079. "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4080. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  4081. verifyFormat(
  4082. "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4083. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  4084. verifyFormat(
  4085. "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  4086. " aaaaaaaaaaaaaaaaaaaaaa();");
  4087. verifyFormat(
  4088. "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  4089. " aaaaaaaaaaaaaaaaaaaaaa());");
  4090. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4091. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4092. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4093. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
  4094. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4095. verifyFormat("return\n"
  4096. " // true if code is one of a or b.\n"
  4097. " code == a || code == b;");
  4098. }
  4099. TEST_F(FormatTest, AlignsAfterOpenBracket) {
  4100. verifyFormat(
  4101. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
  4102. " aaaaaaaaa aaaaaaa) {}");
  4103. verifyFormat(
  4104. "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
  4105. " aaaaaaaaaaa aaaaaaaaa);");
  4106. verifyFormat(
  4107. "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
  4108. " aaaaaaaaaaaaaaaaaaaaa));");
  4109. FormatStyle Style = getLLVMStyle();
  4110. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  4111. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4112. " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
  4113. Style);
  4114. verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
  4115. " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
  4116. Style);
  4117. verifyFormat("SomeLongVariableName->someFunction(\n"
  4118. " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
  4119. Style);
  4120. verifyFormat(
  4121. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
  4122. " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  4123. Style);
  4124. verifyFormat(
  4125. "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
  4126. " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4127. Style);
  4128. verifyFormat(
  4129. "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
  4130. " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
  4131. Style);
  4132. Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
  4133. Style.BinPackArguments = false;
  4134. Style.BinPackParameters = false;
  4135. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4136. " aaaaaaaaaaa aaaaaaaa,\n"
  4137. " aaaaaaaaa aaaaaaa,\n"
  4138. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  4139. Style);
  4140. verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
  4141. " aaaaaaaaaaa aaaaaaaaa,\n"
  4142. " aaaaaaaaaaa aaaaaaaaa,\n"
  4143. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4144. Style);
  4145. verifyFormat("SomeLongVariableName->someFunction(\n"
  4146. " foooooooo(\n"
  4147. " aaaaaaaaaaaaaaa,\n"
  4148. " aaaaaaaaaaaaaaaaaaaaa,\n"
  4149. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
  4150. Style);
  4151. }
  4152. TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
  4153. FormatStyle Style = getLLVMStyleWithColumns(40);
  4154. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4155. " bbbbbbbbbbbbbbbbbbbbbb);",
  4156. Style);
  4157. Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
  4158. Style.AlignOperands = false;
  4159. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4160. " bbbbbbbbbbbbbbbbbbbbbb);",
  4161. Style);
  4162. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  4163. Style.AlignOperands = true;
  4164. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4165. " bbbbbbbbbbbbbbbbbbbbbb);",
  4166. Style);
  4167. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  4168. Style.AlignOperands = false;
  4169. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4170. " bbbbbbbbbbbbbbbbbbbbbb);",
  4171. Style);
  4172. }
  4173. TEST_F(FormatTest, BreaksConditionalExpressions) {
  4174. verifyFormat(
  4175. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4176. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4177. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4178. verifyFormat(
  4179. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4180. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4181. verifyFormat(
  4182. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
  4183. " : aaaaaaaaaaaaa);");
  4184. verifyFormat(
  4185. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4186. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4187. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4188. " aaaaaaaaaaaaa);");
  4189. verifyFormat(
  4190. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4191. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4192. " aaaaaaaaaaaaa);");
  4193. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4194. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4195. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4196. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4197. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4198. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4199. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4200. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4201. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4202. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4203. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4204. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4205. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4206. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4207. " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4208. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4209. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4210. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4211. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4212. " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4213. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  4214. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4215. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4216. " : aaaaaaaaaaaaaaaa;");
  4217. verifyFormat(
  4218. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4219. " ? aaaaaaaaaaaaaaa\n"
  4220. " : aaaaaaaaaaaaaaa;");
  4221. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  4222. " aaaaaaaaa\n"
  4223. " ? b\n"
  4224. " : c);");
  4225. verifyFormat("return aaaa == bbbb\n"
  4226. " // comment\n"
  4227. " ? aaaa\n"
  4228. " : bbbb;");
  4229. verifyFormat("unsigned Indent =\n"
  4230. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
  4231. " ? IndentForLevel[TheLine.Level]\n"
  4232. " : TheLine * 2,\n"
  4233. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  4234. getLLVMStyleWithColumns(70));
  4235. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  4236. " ? aaaaaaaaaaaaaaa\n"
  4237. " : bbbbbbbbbbbbbbb //\n"
  4238. " ? ccccccccccccccc\n"
  4239. " : ddddddddddddddd;");
  4240. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  4241. " ? aaaaaaaaaaaaaaa\n"
  4242. " : (bbbbbbbbbbbbbbb //\n"
  4243. " ? ccccccccccccccc\n"
  4244. " : ddddddddddddddd);");
  4245. verifyFormat(
  4246. "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4247. " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4248. " aaaaaaaaaaaaaaaaaaaaa +\n"
  4249. " aaaaaaaaaaaaaaaaaaaaa\n"
  4250. " : aaaaaaaaaa;");
  4251. verifyFormat(
  4252. "aaaaaa = aaaaaaaaaaaa\n"
  4253. " ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4254. " : aaaaaaaaaaaaaaaaaaaaaa\n"
  4255. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4256. FormatStyle NoBinPacking = getLLVMStyle();
  4257. NoBinPacking.BinPackArguments = false;
  4258. verifyFormat(
  4259. "void f() {\n"
  4260. " g(aaa,\n"
  4261. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  4262. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4263. " ? aaaaaaaaaaaaaaa\n"
  4264. " : aaaaaaaaaaaaaaa);\n"
  4265. "}",
  4266. NoBinPacking);
  4267. verifyFormat(
  4268. "void f() {\n"
  4269. " g(aaa,\n"
  4270. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  4271. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4272. " ?: aaaaaaaaaaaaaaa);\n"
  4273. "}",
  4274. NoBinPacking);
  4275. verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
  4276. " // comment.\n"
  4277. " ccccccccccccccccccccccccccccccccccccccc\n"
  4278. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4279. " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
  4280. // Assignments in conditional expressions. Apparently not uncommon :-(.
  4281. verifyFormat("return a != b\n"
  4282. " // comment\n"
  4283. " ? a = b\n"
  4284. " : a = b;");
  4285. verifyFormat("return a != b\n"
  4286. " // comment\n"
  4287. " ? a = a != b\n"
  4288. " // comment\n"
  4289. " ? a = b\n"
  4290. " : a\n"
  4291. " : a;\n");
  4292. verifyFormat("return a != b\n"
  4293. " // comment\n"
  4294. " ? a\n"
  4295. " : a = a != b\n"
  4296. " // comment\n"
  4297. " ? a = b\n"
  4298. " : a;");
  4299. }
  4300. TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
  4301. FormatStyle Style = getLLVMStyle();
  4302. Style.BreakBeforeTernaryOperators = false;
  4303. Style.ColumnLimit = 70;
  4304. verifyFormat(
  4305. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4306. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4307. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4308. Style);
  4309. verifyFormat(
  4310. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4311. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4312. Style);
  4313. verifyFormat(
  4314. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
  4315. " aaaaaaaaaaaaa);",
  4316. Style);
  4317. verifyFormat(
  4318. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4319. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4320. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4321. " aaaaaaaaaaaaa);",
  4322. Style);
  4323. verifyFormat(
  4324. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4325. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4326. " aaaaaaaaaaaaa);",
  4327. Style);
  4328. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4329. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4330. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  4331. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4332. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4333. Style);
  4334. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4335. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4336. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4337. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  4338. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4339. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4340. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4341. Style);
  4342. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4343. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
  4344. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4345. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4346. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4347. Style);
  4348. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4349. " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4350. " aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4351. Style);
  4352. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  4353. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4354. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4355. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4356. Style);
  4357. verifyFormat(
  4358. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4359. " aaaaaaaaaaaaaaa :\n"
  4360. " aaaaaaaaaaaaaaa;",
  4361. Style);
  4362. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  4363. " aaaaaaaaa ?\n"
  4364. " b :\n"
  4365. " c);",
  4366. Style);
  4367. verifyFormat(
  4368. "unsigned Indent =\n"
  4369. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n"
  4370. " IndentForLevel[TheLine.Level] :\n"
  4371. " TheLine * 2,\n"
  4372. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  4373. Style);
  4374. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  4375. " aaaaaaaaaaaaaaa :\n"
  4376. " bbbbbbbbbbbbbbb ? //\n"
  4377. " ccccccccccccccc :\n"
  4378. " ddddddddddddddd;",
  4379. Style);
  4380. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  4381. " aaaaaaaaaaaaaaa :\n"
  4382. " (bbbbbbbbbbbbbbb ? //\n"
  4383. " ccccccccccccccc :\n"
  4384. " ddddddddddddddd);",
  4385. Style);
  4386. }
  4387. TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
  4388. verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
  4389. " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
  4390. verifyFormat("bool a = true, b = false;");
  4391. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4392. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
  4393. " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
  4394. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
  4395. verifyFormat(
  4396. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  4397. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
  4398. " d = e && f;");
  4399. verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
  4400. " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
  4401. verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  4402. " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
  4403. verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
  4404. " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
  4405. FormatStyle Style = getGoogleStyle();
  4406. Style.PointerAlignment = FormatStyle::PAS_Left;
  4407. Style.DerivePointerAlignment = false;
  4408. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4409. " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
  4410. " *b = bbbbbbbbbbbbbbbbbbb;",
  4411. Style);
  4412. verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  4413. " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
  4414. Style);
  4415. }
  4416. TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
  4417. verifyFormat("arr[foo ? bar : baz];");
  4418. verifyFormat("f()[foo ? bar : baz];");
  4419. verifyFormat("(a + b)[foo ? bar : baz];");
  4420. verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
  4421. }
  4422. TEST_F(FormatTest, AlignsStringLiterals) {
  4423. verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
  4424. " \"short literal\");");
  4425. verifyFormat(
  4426. "looooooooooooooooooooooooongFunction(\n"
  4427. " \"short literal\"\n"
  4428. " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
  4429. verifyFormat("someFunction(\"Always break between multi-line\"\n"
  4430. " \" string literals\",\n"
  4431. " and, other, parameters);");
  4432. EXPECT_EQ("fun + \"1243\" /* comment */\n"
  4433. " \"5678\";",
  4434. format("fun + \"1243\" /* comment */\n"
  4435. " \"5678\";",
  4436. getLLVMStyleWithColumns(28)));
  4437. EXPECT_EQ(
  4438. "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  4439. " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
  4440. " \"aaaaaaaaaaaaaaaa\";",
  4441. format("aaaaaa ="
  4442. "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  4443. "aaaaaaaaaaaaaaaaaaaaa\" "
  4444. "\"aaaaaaaaaaaaaaaa\";"));
  4445. verifyFormat("a = a + \"a\"\n"
  4446. " \"a\"\n"
  4447. " \"a\";");
  4448. verifyFormat("f(\"a\", \"b\"\n"
  4449. " \"c\");");
  4450. verifyFormat(
  4451. "#define LL_FORMAT \"ll\"\n"
  4452. "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
  4453. " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
  4454. verifyFormat("#define A(X) \\\n"
  4455. " \"aaaaa\" #X \"bbbbbb\" \\\n"
  4456. " \"ccccc\"",
  4457. getLLVMStyleWithColumns(23));
  4458. verifyFormat("#define A \"def\"\n"
  4459. "f(\"abc\" A \"ghi\"\n"
  4460. " \"jkl\");");
  4461. verifyFormat("f(L\"a\"\n"
  4462. " L\"b\");");
  4463. verifyFormat("#define A(X) \\\n"
  4464. " L\"aaaaa\" #X L\"bbbbbb\" \\\n"
  4465. " L\"ccccc\"",
  4466. getLLVMStyleWithColumns(25));
  4467. verifyFormat("f(@\"a\"\n"
  4468. " @\"b\");");
  4469. verifyFormat("NSString s = @\"a\"\n"
  4470. " @\"b\"\n"
  4471. " @\"c\";");
  4472. verifyFormat("NSString s = @\"a\"\n"
  4473. " \"b\"\n"
  4474. " \"c\";");
  4475. }
  4476. TEST_F(FormatTest, ReturnTypeBreakingStyle) {
  4477. FormatStyle Style = getLLVMStyle();
  4478. // No declarations or definitions should be moved to own line.
  4479. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
  4480. verifyFormat("class A {\n"
  4481. " int f() { return 1; }\n"
  4482. " int g();\n"
  4483. "};\n"
  4484. "int f() { return 1; }\n"
  4485. "int g();\n",
  4486. Style);
  4487. // All declarations and definitions should have the return type moved to its
  4488. // own
  4489. // line.
  4490. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
  4491. verifyFormat("class E {\n"
  4492. " int\n"
  4493. " f() {\n"
  4494. " return 1;\n"
  4495. " }\n"
  4496. " int\n"
  4497. " g();\n"
  4498. "};\n"
  4499. "int\n"
  4500. "f() {\n"
  4501. " return 1;\n"
  4502. "}\n"
  4503. "int\n"
  4504. "g();\n",
  4505. Style);
  4506. // Top-level definitions, and no kinds of declarations should have the
  4507. // return type moved to its own line.
  4508. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
  4509. verifyFormat("class B {\n"
  4510. " int f() { return 1; }\n"
  4511. " int g();\n"
  4512. "};\n"
  4513. "int\n"
  4514. "f() {\n"
  4515. " return 1;\n"
  4516. "}\n"
  4517. "int g();\n",
  4518. Style);
  4519. // Top-level definitions and declarations should have the return type moved
  4520. // to its own line.
  4521. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
  4522. verifyFormat("class C {\n"
  4523. " int f() { return 1; }\n"
  4524. " int g();\n"
  4525. "};\n"
  4526. "int\n"
  4527. "f() {\n"
  4528. " return 1;\n"
  4529. "}\n"
  4530. "int\n"
  4531. "g();\n",
  4532. Style);
  4533. // All definitions should have the return type moved to its own line, but no
  4534. // kinds of declarations.
  4535. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
  4536. verifyFormat("class D {\n"
  4537. " int\n"
  4538. " f() {\n"
  4539. " return 1;\n"
  4540. " }\n"
  4541. " int g();\n"
  4542. "};\n"
  4543. "int\n"
  4544. "f() {\n"
  4545. " return 1;\n"
  4546. "}\n"
  4547. "int g();\n",
  4548. Style);
  4549. verifyFormat("const char *\n"
  4550. "f(void) {\n" // Break here.
  4551. " return \"\";\n"
  4552. "}\n"
  4553. "const char *bar(void);\n", // No break here.
  4554. Style);
  4555. verifyFormat("template <class T>\n"
  4556. "T *\n"
  4557. "f(T &c) {\n" // Break here.
  4558. " return NULL;\n"
  4559. "}\n"
  4560. "template <class T> T *f(T &c);\n", // No break here.
  4561. Style);
  4562. verifyFormat("class C {\n"
  4563. " int\n"
  4564. " operator+() {\n"
  4565. " return 1;\n"
  4566. " }\n"
  4567. " int\n"
  4568. " operator()() {\n"
  4569. " return 1;\n"
  4570. " }\n"
  4571. "};\n",
  4572. Style);
  4573. verifyFormat("void\n"
  4574. "A::operator()() {}\n"
  4575. "void\n"
  4576. "A::operator>>() {}\n"
  4577. "void\n"
  4578. "A::operator+() {}\n",
  4579. Style);
  4580. verifyFormat("void *operator new(std::size_t s);", // No break here.
  4581. Style);
  4582. verifyFormat("void *\n"
  4583. "operator new(std::size_t s) {}",
  4584. Style);
  4585. verifyFormat("void *\n"
  4586. "operator delete[](void *ptr) {}",
  4587. Style);
  4588. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  4589. verifyFormat("const char *\n"
  4590. "f(void)\n" // Break here.
  4591. "{\n"
  4592. " return \"\";\n"
  4593. "}\n"
  4594. "const char *bar(void);\n", // No break here.
  4595. Style);
  4596. verifyFormat("template <class T>\n"
  4597. "T *\n" // Problem here: no line break
  4598. "f(T &c)\n" // Break here.
  4599. "{\n"
  4600. " return NULL;\n"
  4601. "}\n"
  4602. "template <class T> T *f(T &c);\n", // No break here.
  4603. Style);
  4604. }
  4605. TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
  4606. FormatStyle NoBreak = getLLVMStyle();
  4607. NoBreak.AlwaysBreakBeforeMultilineStrings = false;
  4608. FormatStyle Break = getLLVMStyle();
  4609. Break.AlwaysBreakBeforeMultilineStrings = true;
  4610. verifyFormat("aaaa = \"bbbb\"\n"
  4611. " \"cccc\";",
  4612. NoBreak);
  4613. verifyFormat("aaaa =\n"
  4614. " \"bbbb\"\n"
  4615. " \"cccc\";",
  4616. Break);
  4617. verifyFormat("aaaa(\"bbbb\"\n"
  4618. " \"cccc\");",
  4619. NoBreak);
  4620. verifyFormat("aaaa(\n"
  4621. " \"bbbb\"\n"
  4622. " \"cccc\");",
  4623. Break);
  4624. verifyFormat("aaaa(qqq, \"bbbb\"\n"
  4625. " \"cccc\");",
  4626. NoBreak);
  4627. verifyFormat("aaaa(qqq,\n"
  4628. " \"bbbb\"\n"
  4629. " \"cccc\");",
  4630. Break);
  4631. verifyFormat("aaaa(qqq,\n"
  4632. " L\"bbbb\"\n"
  4633. " L\"cccc\");",
  4634. Break);
  4635. verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
  4636. " \"bbbb\"));",
  4637. Break);
  4638. verifyFormat("string s = someFunction(\n"
  4639. " \"abc\"\n"
  4640. " \"abc\");",
  4641. Break);
  4642. // As we break before unary operators, breaking right after them is bad.
  4643. verifyFormat("string foo = abc ? \"x\"\n"
  4644. " \"blah blah blah blah blah blah\"\n"
  4645. " : \"y\";",
  4646. Break);
  4647. // Don't break if there is no column gain.
  4648. verifyFormat("f(\"aaaa\"\n"
  4649. " \"bbbb\");",
  4650. Break);
  4651. // Treat literals with escaped newlines like multi-line string literals.
  4652. EXPECT_EQ("x = \"a\\\n"
  4653. "b\\\n"
  4654. "c\";",
  4655. format("x = \"a\\\n"
  4656. "b\\\n"
  4657. "c\";",
  4658. NoBreak));
  4659. EXPECT_EQ("xxxx =\n"
  4660. " \"a\\\n"
  4661. "b\\\n"
  4662. "c\";",
  4663. format("xxxx = \"a\\\n"
  4664. "b\\\n"
  4665. "c\";",
  4666. Break));
  4667. // Exempt ObjC strings for now.
  4668. EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
  4669. " @\"bbbb\";",
  4670. format("NSString *const kString = @\"aaaa\"\n"
  4671. "@\"bbbb\";",
  4672. Break));
  4673. Break.ColumnLimit = 0;
  4674. verifyFormat("const char *hello = \"hello llvm\";", Break);
  4675. }
  4676. TEST_F(FormatTest, AlignsPipes) {
  4677. verifyFormat(
  4678. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4679. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4680. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4681. verifyFormat(
  4682. "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
  4683. " << aaaaaaaaaaaaaaaaaaaa;");
  4684. verifyFormat(
  4685. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4686. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4687. verifyFormat(
  4688. "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
  4689. " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
  4690. " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
  4691. verifyFormat(
  4692. "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4693. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4694. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4695. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4696. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4697. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4698. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4699. verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
  4700. " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
  4701. verifyFormat(
  4702. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4703. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4704. verifyFormat("return out << \"somepacket = {\\n\"\n"
  4705. " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
  4706. " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
  4707. " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
  4708. " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
  4709. " << \"}\";");
  4710. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  4711. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  4712. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
  4713. verifyFormat(
  4714. "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
  4715. " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
  4716. " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
  4717. " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
  4718. " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
  4719. verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
  4720. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4721. verifyFormat(
  4722. "void f() {\n"
  4723. " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
  4724. " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  4725. "}");
  4726. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
  4727. " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
  4728. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4729. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4730. " aaaaaaaaaaaaaaaaaaaaa)\n"
  4731. " << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4732. verifyFormat("LOG_IF(aaa == //\n"
  4733. " bbb)\n"
  4734. " << a << b;");
  4735. // Breaking before the first "<<" is generally not desirable.
  4736. verifyFormat(
  4737. "llvm::errs()\n"
  4738. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4739. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4740. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4741. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4742. getLLVMStyleWithColumns(70));
  4743. verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4744. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4745. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4746. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4747. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4748. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4749. getLLVMStyleWithColumns(70));
  4750. // But sometimes, breaking before the first "<<" is desirable.
  4751. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
  4752. " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
  4753. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
  4754. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4755. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4756. verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
  4757. " << BEF << IsTemplate << Description << E->getType();");
  4758. verifyFormat(
  4759. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4760. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4761. // Incomplete string literal.
  4762. EXPECT_EQ("llvm::errs() << \"\n"
  4763. " << a;",
  4764. format("llvm::errs() << \"\n<<a;"));
  4765. verifyFormat("void f() {\n"
  4766. " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
  4767. " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
  4768. "}");
  4769. // Handle 'endl'.
  4770. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
  4771. " << bbbbbbbbbbbbbbbbbbbbbb << endl;");
  4772. verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
  4773. // Handle '\n'.
  4774. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
  4775. " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
  4776. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
  4777. " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
  4778. verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
  4779. " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
  4780. verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
  4781. }
  4782. TEST_F(FormatTest, UnderstandsEquals) {
  4783. verifyFormat(
  4784. "aaaaaaaaaaaaaaaaa =\n"
  4785. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4786. verifyFormat(
  4787. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4788. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  4789. verifyFormat(
  4790. "if (a) {\n"
  4791. " f();\n"
  4792. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4793. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  4794. "}");
  4795. verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4796. " 100000000 + 10000000) {\n}");
  4797. }
  4798. TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
  4799. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  4800. " .looooooooooooooooooooooooooooooooooooooongFunction();");
  4801. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  4802. " ->looooooooooooooooooooooooooooooooooooooongFunction();");
  4803. verifyFormat(
  4804. "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
  4805. " Parameter2);");
  4806. verifyFormat(
  4807. "ShortObject->shortFunction(\n"
  4808. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
  4809. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
  4810. verifyFormat("loooooooooooooongFunction(\n"
  4811. " LoooooooooooooongObject->looooooooooooooooongFunction());");
  4812. verifyFormat(
  4813. "function(LoooooooooooooooooooooooooooooooooooongObject\n"
  4814. " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
  4815. verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  4816. " .WillRepeatedly(Return(SomeValue));");
  4817. verifyFormat("void f() {\n"
  4818. " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  4819. " .Times(2)\n"
  4820. " .WillRepeatedly(Return(SomeValue));\n"
  4821. "}");
  4822. verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
  4823. " ccccccccccccccccccccccc);");
  4824. verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4825. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4826. " .aaaaa(aaaaa),\n"
  4827. " aaaaaaaaaaaaaaaaaaaaa);");
  4828. verifyFormat("void f() {\n"
  4829. " aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4830. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
  4831. "}");
  4832. verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4833. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4834. " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4835. " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4836. " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  4837. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4838. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4839. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4840. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
  4841. "}");
  4842. // Here, it is not necessary to wrap at "." or "->".
  4843. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
  4844. " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  4845. verifyFormat(
  4846. "aaaaaaaaaaa->aaaaaaaaa(\n"
  4847. " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4848. " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
  4849. verifyFormat(
  4850. "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4851. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
  4852. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
  4853. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  4854. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
  4855. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  4856. verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4857. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4858. " .a();");
  4859. FormatStyle NoBinPacking = getLLVMStyle();
  4860. NoBinPacking.BinPackParameters = false;
  4861. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  4862. " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  4863. " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
  4864. " aaaaaaaaaaaaaaaaaaa,\n"
  4865. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4866. NoBinPacking);
  4867. // If there is a subsequent call, change to hanging indentation.
  4868. verifyFormat(
  4869. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4870. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
  4871. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4872. verifyFormat(
  4873. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4874. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
  4875. verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4876. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4877. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4878. verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4879. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4880. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  4881. }
  4882. TEST_F(FormatTest, WrapsTemplateDeclarations) {
  4883. verifyFormat("template <typename T>\n"
  4884. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  4885. verifyFormat("template <typename T>\n"
  4886. "// T should be one of {A, B}.\n"
  4887. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  4888. verifyFormat(
  4889. "template <typename T>\n"
  4890. "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
  4891. verifyFormat("template <typename T>\n"
  4892. "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
  4893. " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
  4894. verifyFormat(
  4895. "template <typename T>\n"
  4896. "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
  4897. " int Paaaaaaaaaaaaaaaaaaaaram2);");
  4898. verifyFormat(
  4899. "template <typename T>\n"
  4900. "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
  4901. " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
  4902. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4903. verifyFormat("template <typename T>\n"
  4904. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4905. " int aaaaaaaaaaaaaaaaaaaaaa);");
  4906. verifyFormat(
  4907. "template <typename T1, typename T2 = char, typename T3 = char,\n"
  4908. " typename T4 = char>\n"
  4909. "void f();");
  4910. verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
  4911. " template <typename> class cccccccccccccccccccccc,\n"
  4912. " typename ddddddddddddd>\n"
  4913. "class C {};");
  4914. verifyFormat(
  4915. "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
  4916. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4917. verifyFormat("void f() {\n"
  4918. " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
  4919. " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
  4920. "}");
  4921. verifyFormat("template <typename T> class C {};");
  4922. verifyFormat("template <typename T> void f();");
  4923. verifyFormat("template <typename T> void f() {}");
  4924. verifyFormat(
  4925. "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  4926. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4927. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
  4928. " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  4929. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4930. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
  4931. " bbbbbbbbbbbbbbbbbbbbbbbb);",
  4932. getLLVMStyleWithColumns(72));
  4933. EXPECT_EQ("static_cast<A< //\n"
  4934. " B> *>(\n"
  4935. "\n"
  4936. " );",
  4937. format("static_cast<A<//\n"
  4938. " B>*>(\n"
  4939. "\n"
  4940. " );"));
  4941. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4942. " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
  4943. FormatStyle AlwaysBreak = getLLVMStyle();
  4944. AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
  4945. verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
  4946. verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
  4947. verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
  4948. verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4949. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
  4950. " ccccccccccccccccccccccccccccccccccccccccccccccc);");
  4951. verifyFormat("template <template <typename> class Fooooooo,\n"
  4952. " template <typename> class Baaaaaaar>\n"
  4953. "struct C {};",
  4954. AlwaysBreak);
  4955. verifyFormat("template <typename T> // T can be A, B or C.\n"
  4956. "struct C {};",
  4957. AlwaysBreak);
  4958. }
  4959. TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
  4960. verifyFormat(
  4961. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4962. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4963. verifyFormat(
  4964. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4965. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4966. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  4967. // FIXME: Should we have the extra indent after the second break?
  4968. verifyFormat(
  4969. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4970. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4971. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4972. verifyFormat(
  4973. "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
  4974. " cccccccccccccccccccccccccccccccccccccccccccccc());");
  4975. // Breaking at nested name specifiers is generally not desirable.
  4976. verifyFormat(
  4977. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4978. " aaaaaaaaaaaaaaaaaaaaaaa);");
  4979. verifyFormat(
  4980. "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4981. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4982. " aaaaaaaaaaaaaaaaaaaaa);",
  4983. getLLVMStyleWithColumns(74));
  4984. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4985. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4986. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4987. }
  4988. TEST_F(FormatTest, UnderstandsTemplateParameters) {
  4989. verifyFormat("A<int> a;");
  4990. verifyFormat("A<A<A<int>>> a;");
  4991. verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
  4992. verifyFormat("bool x = a < 1 || 2 > a;");
  4993. verifyFormat("bool x = 5 < f<int>();");
  4994. verifyFormat("bool x = f<int>() > 5;");
  4995. verifyFormat("bool x = 5 < a<int>::x;");
  4996. verifyFormat("bool x = a < 4 ? a > 2 : false;");
  4997. verifyFormat("bool x = f() ? a < 2 : a > 2;");
  4998. verifyGoogleFormat("A<A<int>> a;");
  4999. verifyGoogleFormat("A<A<A<int>>> a;");
  5000. verifyGoogleFormat("A<A<A<A<int>>>> a;");
  5001. verifyGoogleFormat("A<A<int> > a;");
  5002. verifyGoogleFormat("A<A<A<int> > > a;");
  5003. verifyGoogleFormat("A<A<A<A<int> > > > a;");
  5004. verifyGoogleFormat("A<::A<int>> a;");
  5005. verifyGoogleFormat("A<::A> a;");
  5006. verifyGoogleFormat("A< ::A> a;");
  5007. verifyGoogleFormat("A< ::A<int> > a;");
  5008. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
  5009. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
  5010. EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
  5011. EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
  5012. EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
  5013. format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
  5014. verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
  5015. verifyFormat("test >> a >> b;");
  5016. verifyFormat("test << a >> b;");
  5017. verifyFormat("f<int>();");
  5018. verifyFormat("template <typename T> void f() {}");
  5019. verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
  5020. verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
  5021. "sizeof(char)>::type>;");
  5022. verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
  5023. // Not template parameters.
  5024. verifyFormat("return a < b && c > d;");
  5025. verifyFormat("void f() {\n"
  5026. " while (a < b && c > d) {\n"
  5027. " }\n"
  5028. "}");
  5029. verifyFormat("template <typename... Types>\n"
  5030. "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
  5031. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5032. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
  5033. getLLVMStyleWithColumns(60));
  5034. verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
  5035. verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
  5036. verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
  5037. }
  5038. TEST_F(FormatTest, UnderstandsBinaryOperators) {
  5039. verifyFormat("COMPARE(a, ==, b);");
  5040. }
  5041. TEST_F(FormatTest, UnderstandsPointersToMembers) {
  5042. verifyFormat("int A::*x;");
  5043. verifyFormat("int (S::*func)(void *);");
  5044. verifyFormat("void f() { int (S::*func)(void *); }");
  5045. verifyFormat("typedef bool *(Class::*Member)() const;");
  5046. verifyFormat("void f() {\n"
  5047. " (a->*f)();\n"
  5048. " a->*x;\n"
  5049. " (a.*f)();\n"
  5050. " ((*a).*f)();\n"
  5051. " a.*x;\n"
  5052. "}");
  5053. verifyFormat("void f() {\n"
  5054. " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  5055. " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
  5056. "}");
  5057. verifyFormat(
  5058. "(aaaaaaaaaa->*bbbbbbb)(\n"
  5059. " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  5060. FormatStyle Style = getLLVMStyle();
  5061. Style.PointerAlignment = FormatStyle::PAS_Left;
  5062. verifyFormat("typedef bool* (Class::*Member)() const;", Style);
  5063. }
  5064. TEST_F(FormatTest, UnderstandsUnaryOperators) {
  5065. verifyFormat("int a = -2;");
  5066. verifyFormat("f(-1, -2, -3);");
  5067. verifyFormat("a[-1] = 5;");
  5068. verifyFormat("int a = 5 + -2;");
  5069. verifyFormat("if (i == -1) {\n}");
  5070. verifyFormat("if (i != -1) {\n}");
  5071. verifyFormat("if (i > -1) {\n}");
  5072. verifyFormat("if (i < -1) {\n}");
  5073. verifyFormat("++(a->f());");
  5074. verifyFormat("--(a->f());");
  5075. verifyFormat("(a->f())++;");
  5076. verifyFormat("a[42]++;");
  5077. verifyFormat("if (!(a->f())) {\n}");
  5078. verifyFormat("a-- > b;");
  5079. verifyFormat("b ? -a : c;");
  5080. verifyFormat("n * sizeof char16;");
  5081. verifyFormat("n * alignof char16;", getGoogleStyle());
  5082. verifyFormat("sizeof(char);");
  5083. verifyFormat("alignof(char);", getGoogleStyle());
  5084. verifyFormat("return -1;");
  5085. verifyFormat("switch (a) {\n"
  5086. "case -1:\n"
  5087. " break;\n"
  5088. "}");
  5089. verifyFormat("#define X -1");
  5090. verifyFormat("#define X -kConstant");
  5091. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
  5092. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
  5093. verifyFormat("int a = /* confusing comment */ -1;");
  5094. // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
  5095. verifyFormat("int a = i /* confusing comment */++;");
  5096. }
  5097. TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
  5098. verifyFormat("if (!aaaaaaaaaa( // break\n"
  5099. " aaaaa)) {\n"
  5100. "}");
  5101. verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
  5102. " aaaaa));");
  5103. verifyFormat("*aaa = aaaaaaa( // break\n"
  5104. " bbbbbb);");
  5105. }
  5106. TEST_F(FormatTest, UnderstandsOverloadedOperators) {
  5107. verifyFormat("bool operator<();");
  5108. verifyFormat("bool operator>();");
  5109. verifyFormat("bool operator=();");
  5110. verifyFormat("bool operator==();");
  5111. verifyFormat("bool operator!=();");
  5112. verifyFormat("int operator+();");
  5113. verifyFormat("int operator++();");
  5114. verifyFormat("bool operator,();");
  5115. verifyFormat("bool operator();");
  5116. verifyFormat("bool operator()();");
  5117. verifyFormat("bool operator[]();");
  5118. verifyFormat("operator bool();");
  5119. verifyFormat("operator int();");
  5120. verifyFormat("operator void *();");
  5121. verifyFormat("operator SomeType<int>();");
  5122. verifyFormat("operator SomeType<int, int>();");
  5123. verifyFormat("operator SomeType<SomeType<int>>();");
  5124. verifyFormat("void *operator new(std::size_t size);");
  5125. verifyFormat("void *operator new[](std::size_t size);");
  5126. verifyFormat("void operator delete(void *ptr);");
  5127. verifyFormat("void operator delete[](void *ptr);");
  5128. verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
  5129. "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
  5130. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
  5131. " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
  5132. verifyFormat(
  5133. "ostream &operator<<(ostream &OutputStream,\n"
  5134. " SomeReallyLongType WithSomeReallyLongValue);");
  5135. verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
  5136. " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
  5137. " return left.group < right.group;\n"
  5138. "}");
  5139. verifyFormat("SomeType &operator=(const SomeType &S);");
  5140. verifyFormat("f.template operator()<int>();");
  5141. verifyGoogleFormat("operator void*();");
  5142. verifyGoogleFormat("operator SomeType<SomeType<int>>();");
  5143. verifyGoogleFormat("operator ::A();");
  5144. verifyFormat("using A::operator+;");
  5145. verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
  5146. "int i;");
  5147. }
  5148. TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
  5149. verifyFormat("Deleted &operator=(const Deleted &) & = default;");
  5150. verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
  5151. verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
  5152. verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
  5153. verifyFormat("Deleted &operator=(const Deleted &) &;");
  5154. verifyFormat("Deleted &operator=(const Deleted &) &&;");
  5155. verifyFormat("SomeType MemberFunction(const Deleted &) &;");
  5156. verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
  5157. verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
  5158. verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
  5159. verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
  5160. FormatStyle AlignLeft = getLLVMStyle();
  5161. AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
  5162. verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
  5163. verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
  5164. AlignLeft);
  5165. verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
  5166. verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
  5167. FormatStyle Spaces = getLLVMStyle();
  5168. Spaces.SpacesInCStyleCastParentheses = true;
  5169. verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
  5170. verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
  5171. verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
  5172. verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
  5173. Spaces.SpacesInCStyleCastParentheses = false;
  5174. Spaces.SpacesInParentheses = true;
  5175. verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
  5176. verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
  5177. verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
  5178. verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
  5179. }
  5180. TEST_F(FormatTest, UnderstandsNewAndDelete) {
  5181. verifyFormat("void f() {\n"
  5182. " A *a = new A;\n"
  5183. " A *a = new (placement) A;\n"
  5184. " delete a;\n"
  5185. " delete (A *)a;\n"
  5186. "}");
  5187. verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
  5188. " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
  5189. verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  5190. " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
  5191. " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
  5192. verifyFormat("delete[] h->p;");
  5193. }
  5194. TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
  5195. verifyFormat("int *f(int *a) {}");
  5196. verifyFormat("int main(int argc, char **argv) {}");
  5197. verifyFormat("Test::Test(int b) : a(b * b) {}");
  5198. verifyIndependentOfContext("f(a, *a);");
  5199. verifyFormat("void g() { f(*a); }");
  5200. verifyIndependentOfContext("int a = b * 10;");
  5201. verifyIndependentOfContext("int a = 10 * b;");
  5202. verifyIndependentOfContext("int a = b * c;");
  5203. verifyIndependentOfContext("int a += b * c;");
  5204. verifyIndependentOfContext("int a -= b * c;");
  5205. verifyIndependentOfContext("int a *= b * c;");
  5206. verifyIndependentOfContext("int a /= b * c;");
  5207. verifyIndependentOfContext("int a = *b;");
  5208. verifyIndependentOfContext("int a = *b * c;");
  5209. verifyIndependentOfContext("int a = b * *c;");
  5210. verifyIndependentOfContext("int a = b * (10);");
  5211. verifyIndependentOfContext("S << b * (10);");
  5212. verifyIndependentOfContext("return 10 * b;");
  5213. verifyIndependentOfContext("return *b * *c;");
  5214. verifyIndependentOfContext("return a & ~b;");
  5215. verifyIndependentOfContext("f(b ? *c : *d);");
  5216. verifyIndependentOfContext("int a = b ? *c : *d;");
  5217. verifyIndependentOfContext("*b = a;");
  5218. verifyIndependentOfContext("a * ~b;");
  5219. verifyIndependentOfContext("a * !b;");
  5220. verifyIndependentOfContext("a * +b;");
  5221. verifyIndependentOfContext("a * -b;");
  5222. verifyIndependentOfContext("a * ++b;");
  5223. verifyIndependentOfContext("a * --b;");
  5224. verifyIndependentOfContext("a[4] * b;");
  5225. verifyIndependentOfContext("a[a * a] = 1;");
  5226. verifyIndependentOfContext("f() * b;");
  5227. verifyIndependentOfContext("a * [self dostuff];");
  5228. verifyIndependentOfContext("int x = a * (a + b);");
  5229. verifyIndependentOfContext("(a *)(a + b);");
  5230. verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
  5231. verifyIndependentOfContext("int *pa = (int *)&a;");
  5232. verifyIndependentOfContext("return sizeof(int **);");
  5233. verifyIndependentOfContext("return sizeof(int ******);");
  5234. verifyIndependentOfContext("return (int **&)a;");
  5235. verifyIndependentOfContext("f((*PointerToArray)[10]);");
  5236. verifyFormat("void f(Type (*parameter)[10]) {}");
  5237. verifyFormat("void f(Type (&parameter)[10]) {}");
  5238. verifyGoogleFormat("return sizeof(int**);");
  5239. verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
  5240. verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
  5241. verifyFormat("auto a = [](int **&, int ***) {};");
  5242. verifyFormat("auto PointerBinding = [](const char *S) {};");
  5243. verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
  5244. verifyFormat("[](const decltype(*a) &value) {}");
  5245. verifyFormat("decltype(a * b) F();");
  5246. verifyFormat("#define MACRO() [](A *a) { return 1; }");
  5247. verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
  5248. verifyIndependentOfContext("typedef void (*f)(int *a);");
  5249. verifyIndependentOfContext("int i{a * b};");
  5250. verifyIndependentOfContext("aaa && aaa->f();");
  5251. verifyIndependentOfContext("int x = ~*p;");
  5252. verifyFormat("Constructor() : a(a), area(width * height) {}");
  5253. verifyFormat("Constructor() : a(a), area(a, width * height) {}");
  5254. verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
  5255. verifyFormat("void f() { f(a, c * d); }");
  5256. verifyFormat("void f() { f(new a(), c * d); }");
  5257. verifyIndependentOfContext("InvalidRegions[*R] = 0;");
  5258. verifyIndependentOfContext("A<int *> a;");
  5259. verifyIndependentOfContext("A<int **> a;");
  5260. verifyIndependentOfContext("A<int *, int *> a;");
  5261. verifyIndependentOfContext("A<int *[]> a;");
  5262. verifyIndependentOfContext(
  5263. "const char *const p = reinterpret_cast<const char *const>(q);");
  5264. verifyIndependentOfContext("A<int **, int **> a;");
  5265. verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
  5266. verifyFormat("for (char **a = b; *a; ++a) {\n}");
  5267. verifyFormat("for (; a && b;) {\n}");
  5268. verifyFormat("bool foo = true && [] { return false; }();");
  5269. verifyFormat(
  5270. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5271. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5272. verifyGoogleFormat("**outparam = 1;");
  5273. verifyGoogleFormat("*outparam = a * b;");
  5274. verifyGoogleFormat("int main(int argc, char** argv) {}");
  5275. verifyGoogleFormat("A<int*> a;");
  5276. verifyGoogleFormat("A<int**> a;");
  5277. verifyGoogleFormat("A<int*, int*> a;");
  5278. verifyGoogleFormat("A<int**, int**> a;");
  5279. verifyGoogleFormat("f(b ? *c : *d);");
  5280. verifyGoogleFormat("int a = b ? *c : *d;");
  5281. verifyGoogleFormat("Type* t = **x;");
  5282. verifyGoogleFormat("Type* t = *++*x;");
  5283. verifyGoogleFormat("*++*x;");
  5284. verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
  5285. verifyGoogleFormat("Type* t = x++ * y;");
  5286. verifyGoogleFormat(
  5287. "const char* const p = reinterpret_cast<const char* const>(q);");
  5288. verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
  5289. verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
  5290. verifyGoogleFormat("template <typename T>\n"
  5291. "void f(int i = 0, SomeType** temps = NULL);");
  5292. FormatStyle Left = getLLVMStyle();
  5293. Left.PointerAlignment = FormatStyle::PAS_Left;
  5294. verifyFormat("x = *a(x) = *a(y);", Left);
  5295. verifyFormat("for (;; * = b) {\n}", Left);
  5296. verifyFormat("return *this += 1;", Left);
  5297. verifyIndependentOfContext("a = *(x + y);");
  5298. verifyIndependentOfContext("a = &(x + y);");
  5299. verifyIndependentOfContext("*(x + y).call();");
  5300. verifyIndependentOfContext("&(x + y)->call();");
  5301. verifyFormat("void f() { &(*I).first; }");
  5302. verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
  5303. verifyFormat(
  5304. "int *MyValues = {\n"
  5305. " *A, // Operator detection might be confused by the '{'\n"
  5306. " *BB // Operator detection might be confused by previous comment\n"
  5307. "};");
  5308. verifyIndependentOfContext("if (int *a = &b)");
  5309. verifyIndependentOfContext("if (int &a = *b)");
  5310. verifyIndependentOfContext("if (a & b[i])");
  5311. verifyIndependentOfContext("if (a::b::c::d & b[i])");
  5312. verifyIndependentOfContext("if (*b[i])");
  5313. verifyIndependentOfContext("if (int *a = (&b))");
  5314. verifyIndependentOfContext("while (int *a = &b)");
  5315. verifyIndependentOfContext("size = sizeof *a;");
  5316. verifyIndependentOfContext("if (a && (b = c))");
  5317. verifyFormat("void f() {\n"
  5318. " for (const int &v : Values) {\n"
  5319. " }\n"
  5320. "}");
  5321. verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
  5322. verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
  5323. verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
  5324. verifyFormat("#define A (!a * b)");
  5325. verifyFormat("#define MACRO \\\n"
  5326. " int *i = a * b; \\\n"
  5327. " void f(a *b);",
  5328. getLLVMStyleWithColumns(19));
  5329. verifyIndependentOfContext("A = new SomeType *[Length];");
  5330. verifyIndependentOfContext("A = new SomeType *[Length]();");
  5331. verifyIndependentOfContext("T **t = new T *;");
  5332. verifyIndependentOfContext("T **t = new T *();");
  5333. verifyGoogleFormat("A = new SomeType*[Length]();");
  5334. verifyGoogleFormat("A = new SomeType*[Length];");
  5335. verifyGoogleFormat("T** t = new T*;");
  5336. verifyGoogleFormat("T** t = new T*();");
  5337. FormatStyle PointerLeft = getLLVMStyle();
  5338. PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
  5339. verifyFormat("delete *x;", PointerLeft);
  5340. verifyFormat("STATIC_ASSERT((a & b) == 0);");
  5341. verifyFormat("STATIC_ASSERT(0 == (a & b));");
  5342. verifyFormat("template <bool a, bool b> "
  5343. "typename t::if<x && y>::type f() {}");
  5344. verifyFormat("template <int *y> f() {}");
  5345. verifyFormat("vector<int *> v;");
  5346. verifyFormat("vector<int *const> v;");
  5347. verifyFormat("vector<int *const **const *> v;");
  5348. verifyFormat("vector<int *volatile> v;");
  5349. verifyFormat("vector<a * b> v;");
  5350. verifyFormat("foo<b && false>();");
  5351. verifyFormat("foo<b & 1>();");
  5352. verifyFormat("decltype(*::std::declval<const T &>()) void F();");
  5353. verifyFormat(
  5354. "template <class T, class = typename std::enable_if<\n"
  5355. " std::is_integral<T>::value &&\n"
  5356. " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
  5357. "void F();",
  5358. getLLVMStyleWithColumns(76));
  5359. verifyFormat(
  5360. "template <class T,\n"
  5361. " class = typename ::std::enable_if<\n"
  5362. " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
  5363. "void F();",
  5364. getGoogleStyleWithColumns(68));
  5365. verifyIndependentOfContext("MACRO(int *i);");
  5366. verifyIndependentOfContext("MACRO(auto *a);");
  5367. verifyIndependentOfContext("MACRO(const A *a);");
  5368. verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
  5369. // FIXME: Is there a way to make this work?
  5370. // verifyIndependentOfContext("MACRO(A *a);");
  5371. verifyFormat("DatumHandle const *operator->() const { return input_; }");
  5372. verifyFormat("return options != nullptr && operator==(*options);");
  5373. EXPECT_EQ("#define OP(x) \\\n"
  5374. " ostream &operator<<(ostream &s, const A &a) { \\\n"
  5375. " return s << a.DebugString(); \\\n"
  5376. " }",
  5377. format("#define OP(x) \\\n"
  5378. " ostream &operator<<(ostream &s, const A &a) { \\\n"
  5379. " return s << a.DebugString(); \\\n"
  5380. " }",
  5381. getLLVMStyleWithColumns(50)));
  5382. // FIXME: We cannot handle this case yet; we might be able to figure out that
  5383. // foo<x> d > v; doesn't make sense.
  5384. verifyFormat("foo<a<b && c> d> v;");
  5385. FormatStyle PointerMiddle = getLLVMStyle();
  5386. PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
  5387. verifyFormat("delete *x;", PointerMiddle);
  5388. verifyFormat("int * x;", PointerMiddle);
  5389. verifyFormat("template <int * y> f() {}", PointerMiddle);
  5390. verifyFormat("int * f(int * a) {}", PointerMiddle);
  5391. verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
  5392. verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
  5393. verifyFormat("A<int *> a;", PointerMiddle);
  5394. verifyFormat("A<int **> a;", PointerMiddle);
  5395. verifyFormat("A<int *, int *> a;", PointerMiddle);
  5396. verifyFormat("A<int * []> a;", PointerMiddle);
  5397. verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
  5398. verifyFormat("A = new SomeType *[Length];", PointerMiddle);
  5399. verifyFormat("T ** t = new T *;", PointerMiddle);
  5400. // Member function reference qualifiers aren't binary operators.
  5401. verifyFormat("string // break\n"
  5402. "operator()() & {}");
  5403. verifyFormat("string // break\n"
  5404. "operator()() && {}");
  5405. verifyGoogleFormat("template <typename T>\n"
  5406. "auto x() & -> int {}");
  5407. }
  5408. TEST_F(FormatTest, UnderstandsAttributes) {
  5409. verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
  5410. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
  5411. "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
  5412. FormatStyle AfterType = getLLVMStyle();
  5413. AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
  5414. verifyFormat("__attribute__((nodebug)) void\n"
  5415. "foo() {}\n",
  5416. AfterType);
  5417. }
  5418. TEST_F(FormatTest, UnderstandsEllipsis) {
  5419. verifyFormat("int printf(const char *fmt, ...);");
  5420. verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
  5421. verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
  5422. FormatStyle PointersLeft = getLLVMStyle();
  5423. PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
  5424. verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
  5425. }
  5426. TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
  5427. EXPECT_EQ("int *a;\n"
  5428. "int *a;\n"
  5429. "int *a;",
  5430. format("int *a;\n"
  5431. "int* a;\n"
  5432. "int *a;",
  5433. getGoogleStyle()));
  5434. EXPECT_EQ("int* a;\n"
  5435. "int* a;\n"
  5436. "int* a;",
  5437. format("int* a;\n"
  5438. "int* a;\n"
  5439. "int *a;",
  5440. getGoogleStyle()));
  5441. EXPECT_EQ("int *a;\n"
  5442. "int *a;\n"
  5443. "int *a;",
  5444. format("int *a;\n"
  5445. "int * a;\n"
  5446. "int * a;",
  5447. getGoogleStyle()));
  5448. EXPECT_EQ("auto x = [] {\n"
  5449. " int *a;\n"
  5450. " int *a;\n"
  5451. " int *a;\n"
  5452. "};",
  5453. format("auto x=[]{int *a;\n"
  5454. "int * a;\n"
  5455. "int * a;};",
  5456. getGoogleStyle()));
  5457. }
  5458. TEST_F(FormatTest, UnderstandsRvalueReferences) {
  5459. verifyFormat("int f(int &&a) {}");
  5460. verifyFormat("int f(int a, char &&b) {}");
  5461. verifyFormat("void f() { int &&a = b; }");
  5462. verifyGoogleFormat("int f(int a, char&& b) {}");
  5463. verifyGoogleFormat("void f() { int&& a = b; }");
  5464. verifyIndependentOfContext("A<int &&> a;");
  5465. verifyIndependentOfContext("A<int &&, int &&> a;");
  5466. verifyGoogleFormat("A<int&&> a;");
  5467. verifyGoogleFormat("A<int&&, int&&> a;");
  5468. // Not rvalue references:
  5469. verifyFormat("template <bool B, bool C> class A {\n"
  5470. " static_assert(B && C, \"Something is wrong\");\n"
  5471. "};");
  5472. verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
  5473. verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
  5474. verifyFormat("#define A(a, b) (a && b)");
  5475. }
  5476. TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
  5477. verifyFormat("void f() {\n"
  5478. " x[aaaaaaaaa -\n"
  5479. " b] = 23;\n"
  5480. "}",
  5481. getLLVMStyleWithColumns(15));
  5482. }
  5483. TEST_F(FormatTest, FormatsCasts) {
  5484. verifyFormat("Type *A = static_cast<Type *>(P);");
  5485. verifyFormat("Type *A = (Type *)P;");
  5486. verifyFormat("Type *A = (vector<Type *, int *>)P;");
  5487. verifyFormat("int a = (int)(2.0f);");
  5488. verifyFormat("int a = (int)2.0f;");
  5489. verifyFormat("x[(int32)y];");
  5490. verifyFormat("x = (int32)y;");
  5491. verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
  5492. verifyFormat("int a = (int)*b;");
  5493. verifyFormat("int a = (int)2.0f;");
  5494. verifyFormat("int a = (int)~0;");
  5495. verifyFormat("int a = (int)++a;");
  5496. verifyFormat("int a = (int)sizeof(int);");
  5497. verifyFormat("int a = (int)+2;");
  5498. verifyFormat("my_int a = (my_int)2.0f;");
  5499. verifyFormat("my_int a = (my_int)sizeof(int);");
  5500. verifyFormat("return (my_int)aaa;");
  5501. verifyFormat("#define x ((int)-1)");
  5502. verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
  5503. verifyFormat("#define p(q) ((int *)&q)");
  5504. verifyFormat("fn(a)(b) + 1;");
  5505. verifyFormat("void f() { my_int a = (my_int)*b; }");
  5506. verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
  5507. verifyFormat("my_int a = (my_int)~0;");
  5508. verifyFormat("my_int a = (my_int)++a;");
  5509. verifyFormat("my_int a = (my_int)-2;");
  5510. verifyFormat("my_int a = (my_int)1;");
  5511. verifyFormat("my_int a = (my_int *)1;");
  5512. verifyFormat("my_int a = (const my_int)-1;");
  5513. verifyFormat("my_int a = (const my_int *)-1;");
  5514. verifyFormat("my_int a = (my_int)(my_int)-1;");
  5515. verifyFormat("my_int a = (ns::my_int)-2;");
  5516. verifyFormat("case (my_int)ONE:");
  5517. // FIXME: single value wrapped with paren will be treated as cast.
  5518. verifyFormat("void f(int i = (kValue)*kMask) {}");
  5519. verifyFormat("{ (void)F; }");
  5520. // Don't break after a cast's
  5521. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  5522. " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
  5523. " bbbbbbbbbbbbbbbbbbbbbb);");
  5524. // These are not casts.
  5525. verifyFormat("void f(int *) {}");
  5526. verifyFormat("f(foo)->b;");
  5527. verifyFormat("f(foo).b;");
  5528. verifyFormat("f(foo)(b);");
  5529. verifyFormat("f(foo)[b];");
  5530. verifyFormat("[](foo) { return 4; }(bar);");
  5531. verifyFormat("(*funptr)(foo)[4];");
  5532. verifyFormat("funptrs[4](foo)[4];");
  5533. verifyFormat("void f(int *);");
  5534. verifyFormat("void f(int *) = 0;");
  5535. verifyFormat("void f(SmallVector<int>) {}");
  5536. verifyFormat("void f(SmallVector<int>);");
  5537. verifyFormat("void f(SmallVector<int>) = 0;");
  5538. verifyFormat("void f(int i = (kA * kB) & kMask) {}");
  5539. verifyFormat("int a = sizeof(int) * b;");
  5540. verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
  5541. verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
  5542. verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
  5543. verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
  5544. // These are not casts, but at some point were confused with casts.
  5545. verifyFormat("virtual void foo(int *) override;");
  5546. verifyFormat("virtual void foo(char &) const;");
  5547. verifyFormat("virtual void foo(int *a, char *) const;");
  5548. verifyFormat("int a = sizeof(int *) + b;");
  5549. verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
  5550. verifyFormat("bool b = f(g<int>) && c;");
  5551. verifyFormat("typedef void (*f)(int i) func;");
  5552. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
  5553. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  5554. // FIXME: The indentation here is not ideal.
  5555. verifyFormat(
  5556. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5557. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
  5558. " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
  5559. }
  5560. TEST_F(FormatTest, FormatsFunctionTypes) {
  5561. verifyFormat("A<bool()> a;");
  5562. verifyFormat("A<SomeType()> a;");
  5563. verifyFormat("A<void (*)(int, std::string)> a;");
  5564. verifyFormat("A<void *(int)>;");
  5565. verifyFormat("void *(*a)(int *, SomeType *);");
  5566. verifyFormat("int (*func)(void *);");
  5567. verifyFormat("void f() { int (*func)(void *); }");
  5568. verifyFormat("template <class CallbackClass>\n"
  5569. "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
  5570. verifyGoogleFormat("A<void*(int*, SomeType*)>;");
  5571. verifyGoogleFormat("void* (*a)(int);");
  5572. verifyGoogleFormat(
  5573. "template <class CallbackClass>\n"
  5574. "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
  5575. // Other constructs can look somewhat like function types:
  5576. verifyFormat("A<sizeof(*x)> a;");
  5577. verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
  5578. verifyFormat("some_var = function(*some_pointer_var)[0];");
  5579. verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
  5580. verifyFormat("int x = f(&h)();");
  5581. }
  5582. TEST_F(FormatTest, FormatsPointersToArrayTypes) {
  5583. verifyFormat("A (*foo_)[6];");
  5584. verifyFormat("vector<int> (*foo_)[6];");
  5585. }
  5586. TEST_F(FormatTest, BreaksLongVariableDeclarations) {
  5587. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5588. " LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5589. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
  5590. " LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5591. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5592. " *LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5593. // Different ways of ()-initializiation.
  5594. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5595. " LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
  5596. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5597. " LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
  5598. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5599. " LoooooooooooooooooooooooooooooooooooooooongVariable({});");
  5600. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5601. " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
  5602. }
  5603. TEST_F(FormatTest, BreaksLongDeclarations) {
  5604. verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
  5605. " AnotherNameForTheLongType;");
  5606. verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
  5607. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  5608. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5609. "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
  5610. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
  5611. "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
  5612. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5613. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5614. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
  5615. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5616. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  5617. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5618. verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
  5619. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5620. FormatStyle Indented = getLLVMStyle();
  5621. Indented.IndentWrappedFunctionNames = true;
  5622. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5623. " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
  5624. Indented);
  5625. verifyFormat(
  5626. "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5627. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5628. Indented);
  5629. verifyFormat(
  5630. "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  5631. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5632. Indented);
  5633. verifyFormat(
  5634. "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
  5635. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5636. Indented);
  5637. // FIXME: Without the comment, this breaks after "(".
  5638. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
  5639. " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
  5640. getGoogleStyle());
  5641. verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
  5642. " int LoooooooooooooooooooongParam2) {}");
  5643. verifyFormat(
  5644. "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
  5645. " SourceLocation L, IdentifierIn *II,\n"
  5646. " Type *T) {}");
  5647. verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
  5648. "ReallyReaaallyLongFunctionName(\n"
  5649. " const std::string &SomeParameter,\n"
  5650. " const SomeType<string, SomeOtherTemplateParameter>\n"
  5651. " &ReallyReallyLongParameterName,\n"
  5652. " const SomeType<string, SomeOtherTemplateParameter>\n"
  5653. " &AnotherLongParameterName) {}");
  5654. verifyFormat("template <typename A>\n"
  5655. "SomeLoooooooooooooooooooooongType<\n"
  5656. " typename some_namespace::SomeOtherType<A>::Type>\n"
  5657. "Function() {}");
  5658. verifyGoogleFormat(
  5659. "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
  5660. " aaaaaaaaaaaaaaaaaaaaaaa;");
  5661. verifyGoogleFormat(
  5662. "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
  5663. " SourceLocation L) {}");
  5664. verifyGoogleFormat(
  5665. "some_namespace::LongReturnType\n"
  5666. "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
  5667. " int first_long_parameter, int second_parameter) {}");
  5668. verifyGoogleFormat("template <typename T>\n"
  5669. "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  5670. "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
  5671. verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5672. " int aaaaaaaaaaaaaaaaaaaaaaa);");
  5673. verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  5674. " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5675. " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5676. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5677. " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  5678. " aaaaaaaaaaaaaaaaaaaaaaaa);");
  5679. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5680. " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  5681. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
  5682. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5683. }
  5684. TEST_F(FormatTest, FormatsArrays) {
  5685. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5686. " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
  5687. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
  5688. " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
  5689. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
  5690. " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
  5691. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5692. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  5693. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5694. " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
  5695. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5696. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5697. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  5698. verifyFormat(
  5699. "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
  5700. " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5701. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  5702. verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
  5703. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
  5704. verifyFormat(
  5705. "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
  5706. " .aaaaaaa[0]\n"
  5707. " .aaaaaaaaaaaaaaaaaaaaaa();");
  5708. verifyFormat("a[::b::c];");
  5709. verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
  5710. FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
  5711. verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
  5712. }
  5713. TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
  5714. verifyFormat("(a)->b();");
  5715. verifyFormat("--a;");
  5716. }
  5717. TEST_F(FormatTest, HandlesIncludeDirectives) {
  5718. verifyFormat("#include <string>\n"
  5719. "#include <a/b/c.h>\n"
  5720. "#include \"a/b/string\"\n"
  5721. "#include \"string.h\"\n"
  5722. "#include \"string.h\"\n"
  5723. "#include <a-a>\n"
  5724. "#include < path with space >\n"
  5725. "#include_next <test.h>"
  5726. "#include \"abc.h\" // this is included for ABC\n"
  5727. "#include \"some long include\" // with a comment\n"
  5728. "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
  5729. getLLVMStyleWithColumns(35));
  5730. EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\""));
  5731. EXPECT_EQ("#include <a>", format("#include<a>"));
  5732. verifyFormat("#import <string>");
  5733. verifyFormat("#import <a/b/c.h>");
  5734. verifyFormat("#import \"a/b/string\"");
  5735. verifyFormat("#import \"string.h\"");
  5736. verifyFormat("#import \"string.h\"");
  5737. verifyFormat("#if __has_include(<strstream>)\n"
  5738. "#include <strstream>\n"
  5739. "#endif");
  5740. verifyFormat("#define MY_IMPORT <a/b>");
  5741. // Protocol buffer definition or missing "#".
  5742. verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
  5743. getLLVMStyleWithColumns(30));
  5744. FormatStyle Style = getLLVMStyle();
  5745. Style.AlwaysBreakBeforeMultilineStrings = true;
  5746. Style.ColumnLimit = 0;
  5747. verifyFormat("#import \"abc.h\"", Style);
  5748. // But 'import' might also be a regular C++ namespace.
  5749. verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5750. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5751. }
  5752. //===----------------------------------------------------------------------===//
  5753. // Error recovery tests.
  5754. //===----------------------------------------------------------------------===//
  5755. TEST_F(FormatTest, IncompleteParameterLists) {
  5756. FormatStyle NoBinPacking = getLLVMStyle();
  5757. NoBinPacking.BinPackParameters = false;
  5758. verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
  5759. " double *min_x,\n"
  5760. " double *max_x,\n"
  5761. " double *min_y,\n"
  5762. " double *max_y,\n"
  5763. " double *min_z,\n"
  5764. " double *max_z, ) {}",
  5765. NoBinPacking);
  5766. }
  5767. TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
  5768. verifyFormat("void f() { return; }\n42");
  5769. verifyFormat("void f() {\n"
  5770. " if (0)\n"
  5771. " return;\n"
  5772. "}\n"
  5773. "42");
  5774. verifyFormat("void f() { return }\n42");
  5775. verifyFormat("void f() {\n"
  5776. " if (0)\n"
  5777. " return\n"
  5778. "}\n"
  5779. "42");
  5780. }
  5781. TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
  5782. EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
  5783. EXPECT_EQ("void f() {\n"
  5784. " if (a)\n"
  5785. " return\n"
  5786. "}",
  5787. format("void f ( ) { if ( a ) return }"));
  5788. EXPECT_EQ("namespace N {\n"
  5789. "void f()\n"
  5790. "}",
  5791. format("namespace N { void f() }"));
  5792. EXPECT_EQ("namespace N {\n"
  5793. "void f() {}\n"
  5794. "void g()\n"
  5795. "}",
  5796. format("namespace N { void f( ) { } void g( ) }"));
  5797. }
  5798. TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
  5799. verifyFormat("int aaaaaaaa =\n"
  5800. " // Overlylongcomment\n"
  5801. " b;",
  5802. getLLVMStyleWithColumns(20));
  5803. verifyFormat("function(\n"
  5804. " ShortArgument,\n"
  5805. " LoooooooooooongArgument);\n",
  5806. getLLVMStyleWithColumns(20));
  5807. }
  5808. TEST_F(FormatTest, IncorrectAccessSpecifier) {
  5809. verifyFormat("public:");
  5810. verifyFormat("class A {\n"
  5811. "public\n"
  5812. " void f() {}\n"
  5813. "};");
  5814. verifyFormat("public\n"
  5815. "int qwerty;");
  5816. verifyFormat("public\n"
  5817. "B {}");
  5818. verifyFormat("public\n"
  5819. "{}");
  5820. verifyFormat("public\n"
  5821. "B { int x; }");
  5822. }
  5823. TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
  5824. verifyFormat("{");
  5825. verifyFormat("#})");
  5826. verifyNoCrash("(/**/[:!] ?[).");
  5827. }
  5828. TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
  5829. verifyFormat("do {\n}");
  5830. verifyFormat("do {\n}\n"
  5831. "f();");
  5832. verifyFormat("do {\n}\n"
  5833. "wheeee(fun);");
  5834. verifyFormat("do {\n"
  5835. " f();\n"
  5836. "}");
  5837. }
  5838. TEST_F(FormatTest, IncorrectCodeMissingParens) {
  5839. verifyFormat("if {\n foo;\n foo();\n}");
  5840. verifyFormat("switch {\n foo;\n foo();\n}");
  5841. verifyIncompleteFormat("for {\n foo;\n foo();\n}");
  5842. verifyFormat("while {\n foo;\n foo();\n}");
  5843. verifyFormat("do {\n foo;\n foo();\n} while;");
  5844. }
  5845. TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
  5846. verifyIncompleteFormat("namespace {\n"
  5847. "class Foo { Foo (\n"
  5848. "};\n"
  5849. "} // comment");
  5850. }
  5851. TEST_F(FormatTest, IncorrectCodeErrorDetection) {
  5852. EXPECT_EQ("{\n {}\n", format("{\n{\n}\n"));
  5853. EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
  5854. EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
  5855. EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n"));
  5856. EXPECT_EQ("{\n"
  5857. " {\n"
  5858. " breakme(\n"
  5859. " qwe);\n"
  5860. " }\n",
  5861. format("{\n"
  5862. " {\n"
  5863. " breakme(qwe);\n"
  5864. "}\n",
  5865. getLLVMStyleWithColumns(10)));
  5866. }
  5867. TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
  5868. verifyFormat("int x = {\n"
  5869. " avariable,\n"
  5870. " b(alongervariable)};",
  5871. getLLVMStyleWithColumns(25));
  5872. }
  5873. TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
  5874. verifyFormat("return (a)(b){1, 2, 3};");
  5875. }
  5876. TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
  5877. verifyFormat("vector<int> x{1, 2, 3, 4};");
  5878. verifyFormat("vector<int> x{\n"
  5879. " 1, 2, 3, 4,\n"
  5880. "};");
  5881. verifyFormat("vector<T> x{{}, {}, {}, {}};");
  5882. verifyFormat("f({1, 2});");
  5883. verifyFormat("auto v = Foo{-1};");
  5884. verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
  5885. verifyFormat("Class::Class : member{1, 2, 3} {}");
  5886. verifyFormat("new vector<int>{1, 2, 3};");
  5887. verifyFormat("new int[3]{1, 2, 3};");
  5888. verifyFormat("new int{1};");
  5889. verifyFormat("return {arg1, arg2};");
  5890. verifyFormat("return {arg1, SomeType{parameter}};");
  5891. verifyFormat("int count = set<int>{f(), g(), h()}.size();");
  5892. verifyFormat("new T{arg1, arg2};");
  5893. verifyFormat("f(MyMap[{composite, key}]);");
  5894. verifyFormat("class Class {\n"
  5895. " T member = {arg1, arg2};\n"
  5896. "};");
  5897. verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
  5898. verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
  5899. verifyFormat("int a = std::is_integral<int>{} + 0;");
  5900. verifyFormat("int foo(int i) { return fo1{}(i); }");
  5901. verifyFormat("int foo(int i) { return fo1{}(i); }");
  5902. verifyFormat("auto i = decltype(x){};");
  5903. verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
  5904. verifyFormat("Node n{1, Node{1000}, //\n"
  5905. " 2};");
  5906. verifyFormat("Aaaa aaaaaaa{\n"
  5907. " {\n"
  5908. " aaaa,\n"
  5909. " },\n"
  5910. "};");
  5911. verifyFormat("class C : public D {\n"
  5912. " SomeClass SC{2};\n"
  5913. "};");
  5914. verifyFormat("class C : public A {\n"
  5915. " class D : public B {\n"
  5916. " void f() { int i{2}; }\n"
  5917. " };\n"
  5918. "};");
  5919. verifyFormat("#define A {a, a},");
  5920. // In combination with BinPackArguments = false.
  5921. FormatStyle NoBinPacking = getLLVMStyle();
  5922. NoBinPacking.BinPackArguments = false;
  5923. verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
  5924. " bbbbb,\n"
  5925. " ccccc,\n"
  5926. " ddddd,\n"
  5927. " eeeee,\n"
  5928. " ffffff,\n"
  5929. " ggggg,\n"
  5930. " hhhhhh,\n"
  5931. " iiiiii,\n"
  5932. " jjjjjj,\n"
  5933. " kkkkkk};",
  5934. NoBinPacking);
  5935. verifyFormat("const Aaaaaa aaaaa = {\n"
  5936. " aaaaa,\n"
  5937. " bbbbb,\n"
  5938. " ccccc,\n"
  5939. " ddddd,\n"
  5940. " eeeee,\n"
  5941. " ffffff,\n"
  5942. " ggggg,\n"
  5943. " hhhhhh,\n"
  5944. " iiiiii,\n"
  5945. " jjjjjj,\n"
  5946. " kkkkkk,\n"
  5947. "};",
  5948. NoBinPacking);
  5949. verifyFormat(
  5950. "const Aaaaaa aaaaa = {\n"
  5951. " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n"
  5952. " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n"
  5953. " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
  5954. "};",
  5955. NoBinPacking);
  5956. // FIXME: The alignment of these trailing comments might be bad. Then again,
  5957. // this might be utterly useless in real code.
  5958. verifyFormat("Constructor::Constructor()\n"
  5959. " : some_value{ //\n"
  5960. " aaaaaaa, //\n"
  5961. " bbbbbbb} {}");
  5962. // In braced lists, the first comment is always assumed to belong to the
  5963. // first element. Thus, it can be moved to the next or previous line as
  5964. // appropriate.
  5965. EXPECT_EQ("function({// First element:\n"
  5966. " 1,\n"
  5967. " // Second element:\n"
  5968. " 2});",
  5969. format("function({\n"
  5970. " // First element:\n"
  5971. " 1,\n"
  5972. " // Second element:\n"
  5973. " 2});"));
  5974. EXPECT_EQ("std::vector<int> MyNumbers{\n"
  5975. " // First element:\n"
  5976. " 1,\n"
  5977. " // Second element:\n"
  5978. " 2};",
  5979. format("std::vector<int> MyNumbers{// First element:\n"
  5980. " 1,\n"
  5981. " // Second element:\n"
  5982. " 2};",
  5983. getLLVMStyleWithColumns(30)));
  5984. // A trailing comma should still lead to an enforced line break.
  5985. EXPECT_EQ("vector<int> SomeVector = {\n"
  5986. " // aaa\n"
  5987. " 1, 2,\n"
  5988. "};",
  5989. format("vector<int> SomeVector = { // aaa\n"
  5990. " 1, 2, };"));
  5991. FormatStyle ExtraSpaces = getLLVMStyle();
  5992. ExtraSpaces.Cpp11BracedListStyle = false;
  5993. ExtraSpaces.ColumnLimit = 75;
  5994. verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
  5995. verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
  5996. verifyFormat("f({ 1, 2 });", ExtraSpaces);
  5997. verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
  5998. verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
  5999. verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
  6000. verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
  6001. verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
  6002. verifyFormat("return { arg1, arg2 };", ExtraSpaces);
  6003. verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
  6004. verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
  6005. verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
  6006. verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
  6007. verifyFormat("class Class {\n"
  6008. " T member = { arg1, arg2 };\n"
  6009. "};",
  6010. ExtraSpaces);
  6011. verifyFormat(
  6012. "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6013. " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
  6014. " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  6015. " bbbbbbbbbbbbbbbbbbbb, bbbbb };",
  6016. ExtraSpaces);
  6017. verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
  6018. verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
  6019. ExtraSpaces);
  6020. verifyFormat(
  6021. "someFunction(OtherParam,\n"
  6022. " BracedList{ // comment 1 (Forcing interesting break)\n"
  6023. " param1, param2,\n"
  6024. " // comment 2\n"
  6025. " param3, param4 });",
  6026. ExtraSpaces);
  6027. verifyFormat(
  6028. "std::this_thread::sleep_for(\n"
  6029. " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
  6030. ExtraSpaces);
  6031. verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
  6032. " aaaaaaa,\n"
  6033. " aaaaaaaaaa,\n"
  6034. " aaaaa,\n"
  6035. " aaaaaaaaaaaaaaa,\n"
  6036. " aaa,\n"
  6037. " aaaaaaaaaa,\n"
  6038. " a,\n"
  6039. " aaaaaaaaaaaaaaaaaaaaa,\n"
  6040. " aaaaaaaaaaaa,\n"
  6041. " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
  6042. " aaaaaaa,\n"
  6043. " a};");
  6044. verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
  6045. }
  6046. TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
  6047. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6048. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6049. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6050. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6051. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6052. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  6053. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
  6054. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6055. " 1, 22, 333, 4444, 55555, //\n"
  6056. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6057. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  6058. verifyFormat(
  6059. "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6060. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6061. " 1, 22, 333, 4444, 55555, 666666, // comment\n"
  6062. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  6063. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  6064. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  6065. " 7777777};");
  6066. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  6067. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  6068. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  6069. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  6070. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  6071. " // Separating comment.\n"
  6072. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  6073. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  6074. " // Leading comment\n"
  6075. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  6076. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  6077. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  6078. " 1, 1, 1, 1};",
  6079. getLLVMStyleWithColumns(39));
  6080. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  6081. " 1, 1, 1, 1};",
  6082. getLLVMStyleWithColumns(38));
  6083. verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
  6084. " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
  6085. getLLVMStyleWithColumns(43));
  6086. verifyFormat(
  6087. "static unsigned SomeValues[10][3] = {\n"
  6088. " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n"
  6089. " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
  6090. verifyFormat("static auto fields = new vector<string>{\n"
  6091. " \"aaaaaaaaaaaaa\",\n"
  6092. " \"aaaaaaaaaaaaa\",\n"
  6093. " \"aaaaaaaaaaaa\",\n"
  6094. " \"aaaaaaaaaaaaaa\",\n"
  6095. " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
  6096. " \"aaaaaaaaaaaa\",\n"
  6097. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
  6098. "};");
  6099. verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
  6100. verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
  6101. " 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
  6102. " 3, cccccccccccccccccccccc};",
  6103. getLLVMStyleWithColumns(60));
  6104. // Trailing commas.
  6105. verifyFormat("vector<int> x = {\n"
  6106. " 1, 1, 1, 1, 1, 1, 1, 1,\n"
  6107. "};",
  6108. getLLVMStyleWithColumns(39));
  6109. verifyFormat("vector<int> x = {\n"
  6110. " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
  6111. "};",
  6112. getLLVMStyleWithColumns(39));
  6113. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  6114. " 1, 1, 1, 1,\n"
  6115. " /**/ /**/};",
  6116. getLLVMStyleWithColumns(39));
  6117. // Trailing comment in the first line.
  6118. verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n"
  6119. " 1111111111, 2222222222, 33333333333, 4444444444, //\n"
  6120. " 111111111, 222222222, 3333333333, 444444444, //\n"
  6121. " 11111111, 22222222, 333333333, 44444444};");
  6122. // Trailing comment in the last line.
  6123. verifyFormat("int aaaaa[] = {\n"
  6124. " 1, 2, 3, // comment\n"
  6125. " 4, 5, 6 // comment\n"
  6126. "};");
  6127. // With nested lists, we should either format one item per line or all nested
  6128. // lists one on line.
  6129. // FIXME: For some nested lists, we can do better.
  6130. verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
  6131. " {aaaaaaaaaaaaaaaaaaa},\n"
  6132. " {aaaaaaaaaaaaaaaaaaaaa},\n"
  6133. " {aaaaaaaaaaaaaaaaa}};",
  6134. getLLVMStyleWithColumns(60));
  6135. verifyFormat(
  6136. "SomeStruct my_struct_array = {\n"
  6137. " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
  6138. " aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
  6139. " {aaa, aaa},\n"
  6140. " {aaa, aaa},\n"
  6141. " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
  6142. " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6143. " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
  6144. // No column layout should be used here.
  6145. verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
  6146. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
  6147. verifyNoCrash("a<,");
  6148. // No braced initializer here.
  6149. verifyFormat("void f() {\n"
  6150. " struct Dummy {};\n"
  6151. " f(v);\n"
  6152. "}");
  6153. // Long lists should be formatted in columns even if they are nested.
  6154. verifyFormat(
  6155. "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6156. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6157. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6158. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6159. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6160. " 1, 22, 333, 4444, 55555, 666666, 7777777});");
  6161. }
  6162. TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
  6163. FormatStyle DoNotMerge = getLLVMStyle();
  6164. DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  6165. verifyFormat("void f() { return 42; }");
  6166. verifyFormat("void f() {\n"
  6167. " return 42;\n"
  6168. "}",
  6169. DoNotMerge);
  6170. verifyFormat("void f() {\n"
  6171. " // Comment\n"
  6172. "}");
  6173. verifyFormat("{\n"
  6174. "#error {\n"
  6175. " int a;\n"
  6176. "}");
  6177. verifyFormat("{\n"
  6178. " int a;\n"
  6179. "#error {\n"
  6180. "}");
  6181. verifyFormat("void f() {} // comment");
  6182. verifyFormat("void f() { int a; } // comment");
  6183. verifyFormat("void f() {\n"
  6184. "} // comment",
  6185. DoNotMerge);
  6186. verifyFormat("void f() {\n"
  6187. " int a;\n"
  6188. "} // comment",
  6189. DoNotMerge);
  6190. verifyFormat("void f() {\n"
  6191. "} // comment",
  6192. getLLVMStyleWithColumns(15));
  6193. verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
  6194. verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
  6195. verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
  6196. verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
  6197. verifyFormat("class C {\n"
  6198. " C()\n"
  6199. " : iiiiiiii(nullptr),\n"
  6200. " kkkkkkk(nullptr),\n"
  6201. " mmmmmmm(nullptr),\n"
  6202. " nnnnnnn(nullptr) {}\n"
  6203. "};",
  6204. getGoogleStyle());
  6205. FormatStyle NoColumnLimit = getLLVMStyle();
  6206. NoColumnLimit.ColumnLimit = 0;
  6207. EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
  6208. EXPECT_EQ("class C {\n"
  6209. " A() : b(0) {}\n"
  6210. "};",
  6211. format("class C{A():b(0){}};", NoColumnLimit));
  6212. EXPECT_EQ("A()\n"
  6213. " : b(0) {\n"
  6214. "}",
  6215. format("A()\n:b(0)\n{\n}", NoColumnLimit));
  6216. FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
  6217. DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
  6218. FormatStyle::SFS_None;
  6219. EXPECT_EQ("A()\n"
  6220. " : b(0) {\n"
  6221. "}",
  6222. format("A():b(0){}", DoNotMergeNoColumnLimit));
  6223. EXPECT_EQ("A()\n"
  6224. " : b(0) {\n"
  6225. "}",
  6226. format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
  6227. verifyFormat("#define A \\\n"
  6228. " void f() { \\\n"
  6229. " int i; \\\n"
  6230. " }",
  6231. getLLVMStyleWithColumns(20));
  6232. verifyFormat("#define A \\\n"
  6233. " void f() { int i; }",
  6234. getLLVMStyleWithColumns(21));
  6235. verifyFormat("#define A \\\n"
  6236. " void f() { \\\n"
  6237. " int i; \\\n"
  6238. " } \\\n"
  6239. " int j;",
  6240. getLLVMStyleWithColumns(22));
  6241. verifyFormat("#define A \\\n"
  6242. " void f() { int i; } \\\n"
  6243. " int j;",
  6244. getLLVMStyleWithColumns(23));
  6245. }
  6246. TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
  6247. FormatStyle MergeInlineOnly = getLLVMStyle();
  6248. MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  6249. verifyFormat("class C {\n"
  6250. " int f() { return 42; }\n"
  6251. "};",
  6252. MergeInlineOnly);
  6253. verifyFormat("int f() {\n"
  6254. " return 42;\n"
  6255. "}",
  6256. MergeInlineOnly);
  6257. }
  6258. TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
  6259. // Elaborate type variable declarations.
  6260. verifyFormat("struct foo a = {bar};\nint n;");
  6261. verifyFormat("class foo a = {bar};\nint n;");
  6262. verifyFormat("union foo a = {bar};\nint n;");
  6263. // Elaborate types inside function definitions.
  6264. verifyFormat("struct foo f() {}\nint n;");
  6265. verifyFormat("class foo f() {}\nint n;");
  6266. verifyFormat("union foo f() {}\nint n;");
  6267. // Templates.
  6268. verifyFormat("template <class X> void f() {}\nint n;");
  6269. verifyFormat("template <struct X> void f() {}\nint n;");
  6270. verifyFormat("template <union X> void f() {}\nint n;");
  6271. // Actual definitions...
  6272. verifyFormat("struct {\n} n;");
  6273. verifyFormat(
  6274. "template <template <class T, class Y>, class Z> class X {\n} n;");
  6275. verifyFormat("union Z {\n int n;\n} x;");
  6276. verifyFormat("class MACRO Z {\n} n;");
  6277. verifyFormat("class MACRO(X) Z {\n} n;");
  6278. verifyFormat("class __attribute__(X) Z {\n} n;");
  6279. verifyFormat("class __declspec(X) Z {\n} n;");
  6280. verifyFormat("class A##B##C {\n} n;");
  6281. verifyFormat("class alignas(16) Z {\n} n;");
  6282. verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
  6283. verifyFormat("class MACROA MACRO(X) Z {\n} n;");
  6284. // Redefinition from nested context:
  6285. verifyFormat("class A::B::C {\n} n;");
  6286. // Template definitions.
  6287. verifyFormat(
  6288. "template <typename F>\n"
  6289. "Matcher(const Matcher<F> &Other,\n"
  6290. " typename enable_if_c<is_base_of<F, T>::value &&\n"
  6291. " !is_same<F, T>::value>::type * = 0)\n"
  6292. " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
  6293. // FIXME: This is still incorrectly handled at the formatter side.
  6294. verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
  6295. verifyFormat("int i = SomeFunction(a<b, a> b);");
  6296. // FIXME:
  6297. // This now gets parsed incorrectly as class definition.
  6298. // verifyFormat("class A<int> f() {\n}\nint n;");
  6299. // Elaborate types where incorrectly parsing the structural element would
  6300. // break the indent.
  6301. verifyFormat("if (true)\n"
  6302. " class X x;\n"
  6303. "else\n"
  6304. " f();\n");
  6305. // This is simply incomplete. Formatting is not important, but must not crash.
  6306. verifyFormat("class A:");
  6307. }
  6308. TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
  6309. EXPECT_EQ("#error Leave all white!!!!! space* alone!\n",
  6310. format("#error Leave all white!!!!! space* alone!\n"));
  6311. EXPECT_EQ(
  6312. "#warning Leave all white!!!!! space* alone!\n",
  6313. format("#warning Leave all white!!!!! space* alone!\n"));
  6314. EXPECT_EQ("#error 1", format(" # error 1"));
  6315. EXPECT_EQ("#warning 1", format(" # warning 1"));
  6316. }
  6317. TEST_F(FormatTest, FormatHashIfExpressions) {
  6318. verifyFormat("#if AAAA && BBBB");
  6319. verifyFormat("#if (AAAA && BBBB)");
  6320. verifyFormat("#elif (AAAA && BBBB)");
  6321. // FIXME: Come up with a better indentation for #elif.
  6322. verifyFormat(
  6323. "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
  6324. " defined(BBBBBBBB)\n"
  6325. "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
  6326. " defined(BBBBBBBB)\n"
  6327. "#endif",
  6328. getLLVMStyleWithColumns(65));
  6329. }
  6330. TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
  6331. FormatStyle AllowsMergedIf = getGoogleStyle();
  6332. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  6333. verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
  6334. verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
  6335. verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
  6336. EXPECT_EQ("if (true) return 42;",
  6337. format("if (true)\nreturn 42;", AllowsMergedIf));
  6338. FormatStyle ShortMergedIf = AllowsMergedIf;
  6339. ShortMergedIf.ColumnLimit = 25;
  6340. verifyFormat("#define A \\\n"
  6341. " if (true) return 42;",
  6342. ShortMergedIf);
  6343. verifyFormat("#define A \\\n"
  6344. " f(); \\\n"
  6345. " if (true)\n"
  6346. "#define B",
  6347. ShortMergedIf);
  6348. verifyFormat("#define A \\\n"
  6349. " f(); \\\n"
  6350. " if (true)\n"
  6351. "g();",
  6352. ShortMergedIf);
  6353. verifyFormat("{\n"
  6354. "#ifdef A\n"
  6355. " // Comment\n"
  6356. " if (true) continue;\n"
  6357. "#endif\n"
  6358. " // Comment\n"
  6359. " if (true) continue;\n"
  6360. "}",
  6361. ShortMergedIf);
  6362. ShortMergedIf.ColumnLimit = 29;
  6363. verifyFormat("#define A \\\n"
  6364. " if (aaaaaaaaaa) return 1; \\\n"
  6365. " return 2;",
  6366. ShortMergedIf);
  6367. ShortMergedIf.ColumnLimit = 28;
  6368. verifyFormat("#define A \\\n"
  6369. " if (aaaaaaaaaa) \\\n"
  6370. " return 1; \\\n"
  6371. " return 2;",
  6372. ShortMergedIf);
  6373. }
  6374. TEST_F(FormatTest, BlockCommentsInControlLoops) {
  6375. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  6376. " f();\n"
  6377. "}");
  6378. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  6379. " f();\n"
  6380. "} /* another comment */ else /* comment #3 */ {\n"
  6381. " g();\n"
  6382. "}");
  6383. verifyFormat("while (0) /* a comment in a strange place */ {\n"
  6384. " f();\n"
  6385. "}");
  6386. verifyFormat("for (;;) /* a comment in a strange place */ {\n"
  6387. " f();\n"
  6388. "}");
  6389. verifyFormat("do /* a comment in a strange place */ {\n"
  6390. " f();\n"
  6391. "} /* another comment */ while (0);");
  6392. }
  6393. TEST_F(FormatTest, BlockComments) {
  6394. EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
  6395. format("/* *//* */ /* */\n/* *//* */ /* */"));
  6396. EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
  6397. EXPECT_EQ("#define A /*123*/ \\\n"
  6398. " b\n"
  6399. "/* */\n"
  6400. "someCall(\n"
  6401. " parameter);",
  6402. format("#define A /*123*/ b\n"
  6403. "/* */\n"
  6404. "someCall(parameter);",
  6405. getLLVMStyleWithColumns(15)));
  6406. EXPECT_EQ("#define A\n"
  6407. "/* */ someCall(\n"
  6408. " parameter);",
  6409. format("#define A\n"
  6410. "/* */someCall(parameter);",
  6411. getLLVMStyleWithColumns(15)));
  6412. EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
  6413. EXPECT_EQ("/*\n"
  6414. "*\n"
  6415. " * aaaaaa\n"
  6416. " * aaaaaa\n"
  6417. "*/",
  6418. format("/*\n"
  6419. "*\n"
  6420. " * aaaaaa aaaaaa\n"
  6421. "*/",
  6422. getLLVMStyleWithColumns(10)));
  6423. EXPECT_EQ("/*\n"
  6424. "**\n"
  6425. "* aaaaaa\n"
  6426. "*aaaaaa\n"
  6427. "*/",
  6428. format("/*\n"
  6429. "**\n"
  6430. "* aaaaaa aaaaaa\n"
  6431. "*/",
  6432. getLLVMStyleWithColumns(10)));
  6433. FormatStyle NoBinPacking = getLLVMStyle();
  6434. NoBinPacking.BinPackParameters = false;
  6435. EXPECT_EQ("someFunction(1, /* comment 1 */\n"
  6436. " 2, /* comment 2 */\n"
  6437. " 3, /* comment 3 */\n"
  6438. " aaaa,\n"
  6439. " bbbb);",
  6440. format("someFunction (1, /* comment 1 */\n"
  6441. " 2, /* comment 2 */ \n"
  6442. " 3, /* comment 3 */\n"
  6443. "aaaa, bbbb );",
  6444. NoBinPacking));
  6445. verifyFormat(
  6446. "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  6447. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  6448. EXPECT_EQ(
  6449. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  6450. " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  6451. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
  6452. format(
  6453. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  6454. " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  6455. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
  6456. EXPECT_EQ(
  6457. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  6458. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  6459. "int cccccccccccccccccccccccccccccc; /* comment */\n",
  6460. format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  6461. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  6462. "int cccccccccccccccccccccccccccccc; /* comment */\n"));
  6463. verifyFormat("void f(int * /* unused */) {}");
  6464. EXPECT_EQ("/*\n"
  6465. " **\n"
  6466. " */",
  6467. format("/*\n"
  6468. " **\n"
  6469. " */"));
  6470. EXPECT_EQ("/*\n"
  6471. " *q\n"
  6472. " */",
  6473. format("/*\n"
  6474. " *q\n"
  6475. " */"));
  6476. EXPECT_EQ("/*\n"
  6477. " * q\n"
  6478. " */",
  6479. format("/*\n"
  6480. " * q\n"
  6481. " */"));
  6482. EXPECT_EQ("/*\n"
  6483. " **/",
  6484. format("/*\n"
  6485. " **/"));
  6486. EXPECT_EQ("/*\n"
  6487. " ***/",
  6488. format("/*\n"
  6489. " ***/"));
  6490. }
  6491. TEST_F(FormatTest, BlockCommentsInMacros) {
  6492. EXPECT_EQ("#define A \\\n"
  6493. " { \\\n"
  6494. " /* one line */ \\\n"
  6495. " someCall();",
  6496. format("#define A { \\\n"
  6497. " /* one line */ \\\n"
  6498. " someCall();",
  6499. getLLVMStyleWithColumns(20)));
  6500. EXPECT_EQ("#define A \\\n"
  6501. " { \\\n"
  6502. " /* previous */ \\\n"
  6503. " /* one line */ \\\n"
  6504. " someCall();",
  6505. format("#define A { \\\n"
  6506. " /* previous */ \\\n"
  6507. " /* one line */ \\\n"
  6508. " someCall();",
  6509. getLLVMStyleWithColumns(20)));
  6510. }
  6511. TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
  6512. EXPECT_EQ("a = {\n"
  6513. " 1111 /* */\n"
  6514. "};",
  6515. format("a = {1111 /* */\n"
  6516. "};",
  6517. getLLVMStyleWithColumns(15)));
  6518. EXPECT_EQ("a = {\n"
  6519. " 1111 /* */\n"
  6520. "};",
  6521. format("a = {1111 /* */\n"
  6522. "};",
  6523. getLLVMStyleWithColumns(15)));
  6524. // FIXME: The formatting is still wrong here.
  6525. EXPECT_EQ("a = {\n"
  6526. " 1111 /* a\n"
  6527. " */\n"
  6528. "};",
  6529. format("a = {1111 /* a */\n"
  6530. "};",
  6531. getLLVMStyleWithColumns(15)));
  6532. }
  6533. TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
  6534. // FIXME: This is not what we want...
  6535. verifyFormat("{\n"
  6536. "// a"
  6537. "// b");
  6538. }
  6539. TEST_F(FormatTest, FormatStarDependingOnContext) {
  6540. verifyFormat("void f(int *a);");
  6541. verifyFormat("void f() { f(fint * b); }");
  6542. verifyFormat("class A {\n void f(int *a);\n};");
  6543. verifyFormat("class A {\n int *a;\n};");
  6544. verifyFormat("namespace a {\n"
  6545. "namespace b {\n"
  6546. "class A {\n"
  6547. " void f() {}\n"
  6548. " int *a;\n"
  6549. "};\n"
  6550. "}\n"
  6551. "}");
  6552. }
  6553. TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
  6554. verifyFormat("while");
  6555. verifyFormat("operator");
  6556. }
  6557. //===----------------------------------------------------------------------===//
  6558. // Objective-C tests.
  6559. //===----------------------------------------------------------------------===//
  6560. TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
  6561. verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
  6562. EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
  6563. format("-(NSUInteger)indexOfObject:(id)anObject;"));
  6564. EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
  6565. EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
  6566. EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
  6567. format("-(NSInteger)Method3:(id)anObject;"));
  6568. EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
  6569. format("-(NSInteger)Method4:(id)anObject;"));
  6570. EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
  6571. format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
  6572. EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
  6573. format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
  6574. EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
  6575. "forAllCells:(BOOL)flag;",
  6576. format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
  6577. "forAllCells:(BOOL)flag;"));
  6578. // Very long objectiveC method declaration.
  6579. verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
  6580. " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
  6581. verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
  6582. " inRange:(NSRange)range\n"
  6583. " outRange:(NSRange)out_range\n"
  6584. " outRange1:(NSRange)out_range1\n"
  6585. " outRange2:(NSRange)out_range2\n"
  6586. " outRange3:(NSRange)out_range3\n"
  6587. " outRange4:(NSRange)out_range4\n"
  6588. " outRange5:(NSRange)out_range5\n"
  6589. " outRange6:(NSRange)out_range6\n"
  6590. " outRange7:(NSRange)out_range7\n"
  6591. " outRange8:(NSRange)out_range8\n"
  6592. " outRange9:(NSRange)out_range9;");
  6593. // When the function name has to be wrapped.
  6594. FormatStyle Style = getLLVMStyle();
  6595. Style.IndentWrappedFunctionNames = false;
  6596. verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
  6597. "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
  6598. " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
  6599. "}",
  6600. Style);
  6601. Style.IndentWrappedFunctionNames = true;
  6602. verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
  6603. " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
  6604. " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
  6605. "}",
  6606. Style);
  6607. verifyFormat("- (int)sum:(vector<int>)numbers;");
  6608. verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
  6609. // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
  6610. // protocol lists (but not for template classes):
  6611. // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
  6612. verifyFormat("- (int (*)())foo:(int (*)())f;");
  6613. verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
  6614. // If there's no return type (very rare in practice!), LLVM and Google style
  6615. // agree.
  6616. verifyFormat("- foo;");
  6617. verifyFormat("- foo:(int)f;");
  6618. verifyGoogleFormat("- foo:(int)foo;");
  6619. }
  6620. TEST_F(FormatTest, FormatObjCInterface) {
  6621. verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
  6622. "@public\n"
  6623. " int field1;\n"
  6624. "@protected\n"
  6625. " int field2;\n"
  6626. "@private\n"
  6627. " int field3;\n"
  6628. "@package\n"
  6629. " int field4;\n"
  6630. "}\n"
  6631. "+ (id)init;\n"
  6632. "@end");
  6633. verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
  6634. " @public\n"
  6635. " int field1;\n"
  6636. " @protected\n"
  6637. " int field2;\n"
  6638. " @private\n"
  6639. " int field3;\n"
  6640. " @package\n"
  6641. " int field4;\n"
  6642. "}\n"
  6643. "+ (id)init;\n"
  6644. "@end");
  6645. verifyFormat("@interface /* wait for it */ Foo\n"
  6646. "+ (id)init;\n"
  6647. "// Look, a comment!\n"
  6648. "- (int)answerWith:(int)i;\n"
  6649. "@end");
  6650. verifyFormat("@interface Foo\n"
  6651. "@end\n"
  6652. "@interface Bar\n"
  6653. "@end");
  6654. verifyFormat("@interface Foo : Bar\n"
  6655. "+ (id)init;\n"
  6656. "@end");
  6657. verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
  6658. "+ (id)init;\n"
  6659. "@end");
  6660. verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
  6661. "+ (id)init;\n"
  6662. "@end");
  6663. verifyFormat("@interface Foo (HackStuff)\n"
  6664. "+ (id)init;\n"
  6665. "@end");
  6666. verifyFormat("@interface Foo ()\n"
  6667. "+ (id)init;\n"
  6668. "@end");
  6669. verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
  6670. "+ (id)init;\n"
  6671. "@end");
  6672. verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
  6673. "+ (id)init;\n"
  6674. "@end");
  6675. verifyFormat("@interface Foo {\n"
  6676. " int _i;\n"
  6677. "}\n"
  6678. "+ (id)init;\n"
  6679. "@end");
  6680. verifyFormat("@interface Foo : Bar {\n"
  6681. " int _i;\n"
  6682. "}\n"
  6683. "+ (id)init;\n"
  6684. "@end");
  6685. verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
  6686. " int _i;\n"
  6687. "}\n"
  6688. "+ (id)init;\n"
  6689. "@end");
  6690. verifyFormat("@interface Foo (HackStuff) {\n"
  6691. " int _i;\n"
  6692. "}\n"
  6693. "+ (id)init;\n"
  6694. "@end");
  6695. verifyFormat("@interface Foo () {\n"
  6696. " int _i;\n"
  6697. "}\n"
  6698. "+ (id)init;\n"
  6699. "@end");
  6700. verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
  6701. " int _i;\n"
  6702. "}\n"
  6703. "+ (id)init;\n"
  6704. "@end");
  6705. FormatStyle OnePerLine = getGoogleStyle();
  6706. OnePerLine.BinPackParameters = false;
  6707. verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n"
  6708. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6709. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6710. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6711. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
  6712. "}",
  6713. OnePerLine);
  6714. }
  6715. TEST_F(FormatTest, FormatObjCImplementation) {
  6716. verifyFormat("@implementation Foo : NSObject {\n"
  6717. "@public\n"
  6718. " int field1;\n"
  6719. "@protected\n"
  6720. " int field2;\n"
  6721. "@private\n"
  6722. " int field3;\n"
  6723. "@package\n"
  6724. " int field4;\n"
  6725. "}\n"
  6726. "+ (id)init {\n}\n"
  6727. "@end");
  6728. verifyGoogleFormat("@implementation Foo : NSObject {\n"
  6729. " @public\n"
  6730. " int field1;\n"
  6731. " @protected\n"
  6732. " int field2;\n"
  6733. " @private\n"
  6734. " int field3;\n"
  6735. " @package\n"
  6736. " int field4;\n"
  6737. "}\n"
  6738. "+ (id)init {\n}\n"
  6739. "@end");
  6740. verifyFormat("@implementation Foo\n"
  6741. "+ (id)init {\n"
  6742. " if (true)\n"
  6743. " return nil;\n"
  6744. "}\n"
  6745. "// Look, a comment!\n"
  6746. "- (int)answerWith:(int)i {\n"
  6747. " return i;\n"
  6748. "}\n"
  6749. "+ (int)answerWith:(int)i {\n"
  6750. " return i;\n"
  6751. "}\n"
  6752. "@end");
  6753. verifyFormat("@implementation Foo\n"
  6754. "@end\n"
  6755. "@implementation Bar\n"
  6756. "@end");
  6757. EXPECT_EQ("@implementation Foo : Bar\n"
  6758. "+ (id)init {\n}\n"
  6759. "- (void)foo {\n}\n"
  6760. "@end",
  6761. format("@implementation Foo : Bar\n"
  6762. "+(id)init{}\n"
  6763. "-(void)foo{}\n"
  6764. "@end"));
  6765. verifyFormat("@implementation Foo {\n"
  6766. " int _i;\n"
  6767. "}\n"
  6768. "+ (id)init {\n}\n"
  6769. "@end");
  6770. verifyFormat("@implementation Foo : Bar {\n"
  6771. " int _i;\n"
  6772. "}\n"
  6773. "+ (id)init {\n}\n"
  6774. "@end");
  6775. verifyFormat("@implementation Foo (HackStuff)\n"
  6776. "+ (id)init {\n}\n"
  6777. "@end");
  6778. verifyFormat("@implementation ObjcClass\n"
  6779. "- (void)method;\n"
  6780. "{}\n"
  6781. "@end");
  6782. }
  6783. TEST_F(FormatTest, FormatObjCProtocol) {
  6784. verifyFormat("@protocol Foo\n"
  6785. "@property(weak) id delegate;\n"
  6786. "- (NSUInteger)numberOfThings;\n"
  6787. "@end");
  6788. verifyFormat("@protocol MyProtocol <NSObject>\n"
  6789. "- (NSUInteger)numberOfThings;\n"
  6790. "@end");
  6791. verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
  6792. "- (NSUInteger)numberOfThings;\n"
  6793. "@end");
  6794. verifyFormat("@protocol Foo;\n"
  6795. "@protocol Bar;\n");
  6796. verifyFormat("@protocol Foo\n"
  6797. "@end\n"
  6798. "@protocol Bar\n"
  6799. "@end");
  6800. verifyFormat("@protocol myProtocol\n"
  6801. "- (void)mandatoryWithInt:(int)i;\n"
  6802. "@optional\n"
  6803. "- (void)optional;\n"
  6804. "@required\n"
  6805. "- (void)required;\n"
  6806. "@optional\n"
  6807. "@property(assign) int madProp;\n"
  6808. "@end\n");
  6809. verifyFormat("@property(nonatomic, assign, readonly)\n"
  6810. " int *looooooooooooooooooooooooooooongNumber;\n"
  6811. "@property(nonatomic, assign, readonly)\n"
  6812. " NSString *looooooooooooooooooooooooooooongName;");
  6813. verifyFormat("@implementation PR18406\n"
  6814. "}\n"
  6815. "@end");
  6816. }
  6817. TEST_F(FormatTest, FormatObjCMethodDeclarations) {
  6818. verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
  6819. " rect:(NSRect)theRect\n"
  6820. " interval:(float)theInterval {\n"
  6821. "}");
  6822. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6823. " longKeyword:(NSRect)theRect\n"
  6824. " longerKeyword:(float)theInterval\n"
  6825. " error:(NSError **)theError {\n"
  6826. "}");
  6827. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6828. " longKeyword:(NSRect)theRect\n"
  6829. " evenLongerKeyword:(float)theInterval\n"
  6830. " error:(NSError **)theError {\n"
  6831. "}");
  6832. verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
  6833. " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
  6834. " NS_DESIGNATED_INITIALIZER;",
  6835. getLLVMStyleWithColumns(60));
  6836. // Continuation indent width should win over aligning colons if the function
  6837. // name is long.
  6838. FormatStyle continuationStyle = getGoogleStyle();
  6839. continuationStyle.ColumnLimit = 40;
  6840. continuationStyle.IndentWrappedFunctionNames = true;
  6841. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6842. " dontAlignNamef:(NSRect)theRect {\n"
  6843. "}",
  6844. continuationStyle);
  6845. // Make sure we don't break aligning for short parameter names.
  6846. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6847. " aShortf:(NSRect)theRect {\n"
  6848. "}",
  6849. continuationStyle);
  6850. }
  6851. TEST_F(FormatTest, FormatObjCMethodExpr) {
  6852. verifyFormat("[foo bar:baz];");
  6853. verifyFormat("return [foo bar:baz];");
  6854. verifyFormat("return (a)[foo bar:baz];");
  6855. verifyFormat("f([foo bar:baz]);");
  6856. verifyFormat("f(2, [foo bar:baz]);");
  6857. verifyFormat("f(2, a ? b : c);");
  6858. verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
  6859. // Unary operators.
  6860. verifyFormat("int a = +[foo bar:baz];");
  6861. verifyFormat("int a = -[foo bar:baz];");
  6862. verifyFormat("int a = ![foo bar:baz];");
  6863. verifyFormat("int a = ~[foo bar:baz];");
  6864. verifyFormat("int a = ++[foo bar:baz];");
  6865. verifyFormat("int a = --[foo bar:baz];");
  6866. verifyFormat("int a = sizeof [foo bar:baz];");
  6867. verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
  6868. verifyFormat("int a = &[foo bar:baz];");
  6869. verifyFormat("int a = *[foo bar:baz];");
  6870. // FIXME: Make casts work, without breaking f()[4].
  6871. // verifyFormat("int a = (int)[foo bar:baz];");
  6872. // verifyFormat("return (int)[foo bar:baz];");
  6873. // verifyFormat("(void)[foo bar:baz];");
  6874. verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
  6875. // Binary operators.
  6876. verifyFormat("[foo bar:baz], [foo bar:baz];");
  6877. verifyFormat("[foo bar:baz] = [foo bar:baz];");
  6878. verifyFormat("[foo bar:baz] *= [foo bar:baz];");
  6879. verifyFormat("[foo bar:baz] /= [foo bar:baz];");
  6880. verifyFormat("[foo bar:baz] %= [foo bar:baz];");
  6881. verifyFormat("[foo bar:baz] += [foo bar:baz];");
  6882. verifyFormat("[foo bar:baz] -= [foo bar:baz];");
  6883. verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
  6884. verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
  6885. verifyFormat("[foo bar:baz] &= [foo bar:baz];");
  6886. verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
  6887. verifyFormat("[foo bar:baz] |= [foo bar:baz];");
  6888. verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
  6889. verifyFormat("[foo bar:baz] || [foo bar:baz];");
  6890. verifyFormat("[foo bar:baz] && [foo bar:baz];");
  6891. verifyFormat("[foo bar:baz] | [foo bar:baz];");
  6892. verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
  6893. verifyFormat("[foo bar:baz] & [foo bar:baz];");
  6894. verifyFormat("[foo bar:baz] == [foo bar:baz];");
  6895. verifyFormat("[foo bar:baz] != [foo bar:baz];");
  6896. verifyFormat("[foo bar:baz] >= [foo bar:baz];");
  6897. verifyFormat("[foo bar:baz] <= [foo bar:baz];");
  6898. verifyFormat("[foo bar:baz] > [foo bar:baz];");
  6899. verifyFormat("[foo bar:baz] < [foo bar:baz];");
  6900. verifyFormat("[foo bar:baz] >> [foo bar:baz];");
  6901. verifyFormat("[foo bar:baz] << [foo bar:baz];");
  6902. verifyFormat("[foo bar:baz] - [foo bar:baz];");
  6903. verifyFormat("[foo bar:baz] + [foo bar:baz];");
  6904. verifyFormat("[foo bar:baz] * [foo bar:baz];");
  6905. verifyFormat("[foo bar:baz] / [foo bar:baz];");
  6906. verifyFormat("[foo bar:baz] % [foo bar:baz];");
  6907. // Whew!
  6908. verifyFormat("return in[42];");
  6909. verifyFormat("for (auto v : in[1]) {\n}");
  6910. verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
  6911. verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
  6912. verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
  6913. verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
  6914. verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
  6915. verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
  6916. "}");
  6917. verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
  6918. verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
  6919. verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
  6920. verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
  6921. verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
  6922. verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
  6923. verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
  6924. verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
  6925. verifyFormat("[button setAction:@selector(zoomOut:)];");
  6926. verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
  6927. verifyFormat("arr[[self indexForFoo:a]];");
  6928. verifyFormat("throw [self errorFor:a];");
  6929. verifyFormat("@throw [self errorFor:a];");
  6930. verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
  6931. verifyFormat("[(id)foo bar:(id) ? baz : quux];");
  6932. verifyFormat("4 > 4 ? (id)a : (id)baz;");
  6933. // This tests that the formatter doesn't break after "backing" but before ":",
  6934. // which would be at 80 columns.
  6935. verifyFormat(
  6936. "void f() {\n"
  6937. " if ((self = [super initWithContentRect:contentRect\n"
  6938. " styleMask:styleMask ?: otherMask\n"
  6939. " backing:NSBackingStoreBuffered\n"
  6940. " defer:YES]))");
  6941. verifyFormat(
  6942. "[foo checkThatBreakingAfterColonWorksOk:\n"
  6943. " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
  6944. verifyFormat("[myObj short:arg1 // Force line break\n"
  6945. " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
  6946. " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
  6947. " error:arg4];");
  6948. verifyFormat(
  6949. "void f() {\n"
  6950. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  6951. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  6952. " pos.width(), pos.height())\n"
  6953. " styleMask:NSBorderlessWindowMask\n"
  6954. " backing:NSBackingStoreBuffered\n"
  6955. " defer:NO]);\n"
  6956. "}");
  6957. verifyFormat(
  6958. "void f() {\n"
  6959. " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
  6960. " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
  6961. " pos.width(), pos.height())\n"
  6962. " syeMask:NSBorderlessWindowMask\n"
  6963. " bking:NSBackingStoreBuffered\n"
  6964. " der:NO]);\n"
  6965. "}",
  6966. getLLVMStyleWithColumns(70));
  6967. verifyFormat(
  6968. "void f() {\n"
  6969. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  6970. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  6971. " pos.width(), pos.height())\n"
  6972. " styleMask:NSBorderlessWindowMask\n"
  6973. " backing:NSBackingStoreBuffered\n"
  6974. " defer:NO]);\n"
  6975. "}",
  6976. getChromiumStyle(FormatStyle::LK_Cpp));
  6977. verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
  6978. " with:contentsNativeView];");
  6979. verifyFormat(
  6980. "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
  6981. " owner:nillllll];");
  6982. verifyFormat(
  6983. "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
  6984. " forType:kBookmarkButtonDragType];");
  6985. verifyFormat("[defaultCenter addObserver:self\n"
  6986. " selector:@selector(willEnterFullscreen)\n"
  6987. " name:kWillEnterFullscreenNotification\n"
  6988. " object:nil];");
  6989. verifyFormat("[image_rep drawInRect:drawRect\n"
  6990. " fromRect:NSZeroRect\n"
  6991. " operation:NSCompositeCopy\n"
  6992. " fraction:1.0\n"
  6993. " respectFlipped:NO\n"
  6994. " hints:nil];");
  6995. verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  6996. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  6997. verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  6998. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  6999. verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
  7000. " aaaaaaaaaaaaaaaaaaaaaa];");
  7001. verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
  7002. " .aaaaaaaa];", // FIXME: Indentation seems off.
  7003. getLLVMStyleWithColumns(60));
  7004. verifyFormat(
  7005. "scoped_nsobject<NSTextField> message(\n"
  7006. " // The frame will be fixed up when |-setMessageText:| is called.\n"
  7007. " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
  7008. verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
  7009. " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
  7010. " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
  7011. " aaaa:bbb];");
  7012. verifyFormat("[self param:function( //\n"
  7013. " parameter)]");
  7014. verifyFormat(
  7015. "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  7016. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  7017. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
  7018. // FIXME: This violates the column limit.
  7019. verifyFormat(
  7020. "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7021. " aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
  7022. " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];",
  7023. getLLVMStyleWithColumns(60));
  7024. // Variadic parameters.
  7025. verifyFormat(
  7026. "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
  7027. verifyFormat(
  7028. "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  7029. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  7030. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
  7031. verifyFormat("[self // break\n"
  7032. " a:a\n"
  7033. " aaa:aaa];");
  7034. verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
  7035. " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
  7036. }
  7037. TEST_F(FormatTest, ObjCAt) {
  7038. verifyFormat("@autoreleasepool");
  7039. verifyFormat("@catch");
  7040. verifyFormat("@class");
  7041. verifyFormat("@compatibility_alias");
  7042. verifyFormat("@defs");
  7043. verifyFormat("@dynamic");
  7044. verifyFormat("@encode");
  7045. verifyFormat("@end");
  7046. verifyFormat("@finally");
  7047. verifyFormat("@implementation");
  7048. verifyFormat("@import");
  7049. verifyFormat("@interface");
  7050. verifyFormat("@optional");
  7051. verifyFormat("@package");
  7052. verifyFormat("@private");
  7053. verifyFormat("@property");
  7054. verifyFormat("@protected");
  7055. verifyFormat("@protocol");
  7056. verifyFormat("@public");
  7057. verifyFormat("@required");
  7058. verifyFormat("@selector");
  7059. verifyFormat("@synchronized");
  7060. verifyFormat("@synthesize");
  7061. verifyFormat("@throw");
  7062. verifyFormat("@try");
  7063. EXPECT_EQ("@interface", format("@ interface"));
  7064. // The precise formatting of this doesn't matter, nobody writes code like
  7065. // this.
  7066. verifyFormat("@ /*foo*/ interface");
  7067. }
  7068. TEST_F(FormatTest, ObjCSnippets) {
  7069. verifyFormat("@autoreleasepool {\n"
  7070. " foo();\n"
  7071. "}");
  7072. verifyFormat("@class Foo, Bar;");
  7073. verifyFormat("@compatibility_alias AliasName ExistingClass;");
  7074. verifyFormat("@dynamic textColor;");
  7075. verifyFormat("char *buf1 = @encode(int *);");
  7076. verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
  7077. verifyFormat("char *buf1 = @encode(int **);");
  7078. verifyFormat("Protocol *proto = @protocol(p1);");
  7079. verifyFormat("SEL s = @selector(foo:);");
  7080. verifyFormat("@synchronized(self) {\n"
  7081. " f();\n"
  7082. "}");
  7083. verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  7084. verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  7085. verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
  7086. verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
  7087. verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
  7088. verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
  7089. getMozillaStyle());
  7090. verifyFormat("@property BOOL editable;", getMozillaStyle());
  7091. verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
  7092. getWebKitStyle());
  7093. verifyFormat("@property BOOL editable;", getWebKitStyle());
  7094. verifyFormat("@import foo.bar;\n"
  7095. "@import baz;");
  7096. }
  7097. TEST_F(FormatTest, ObjCForIn) {
  7098. verifyFormat("- (void)test {\n"
  7099. " for (NSString *n in arrayOfStrings) {\n"
  7100. " foo(n);\n"
  7101. " }\n"
  7102. "}");
  7103. verifyFormat("- (void)test {\n"
  7104. " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n"
  7105. " foo(n);\n"
  7106. " }\n"
  7107. "}");
  7108. }
  7109. TEST_F(FormatTest, ObjCLiterals) {
  7110. verifyFormat("@\"String\"");
  7111. verifyFormat("@1");
  7112. verifyFormat("@+4.8");
  7113. verifyFormat("@-4");
  7114. verifyFormat("@1LL");
  7115. verifyFormat("@.5");
  7116. verifyFormat("@'c'");
  7117. verifyFormat("@true");
  7118. verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
  7119. verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
  7120. verifyFormat("NSNumber *favoriteColor = @(Green);");
  7121. verifyFormat("NSString *path = @(getenv(\"PATH\"));");
  7122. verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
  7123. }
  7124. TEST_F(FormatTest, ObjCDictLiterals) {
  7125. verifyFormat("@{");
  7126. verifyFormat("@{}");
  7127. verifyFormat("@{@\"one\" : @1}");
  7128. verifyFormat("return @{@\"one\" : @1;");
  7129. verifyFormat("@{@\"one\" : @1}");
  7130. verifyFormat("@{@\"one\" : @{@2 : @1}}");
  7131. verifyFormat("@{\n"
  7132. " @\"one\" : @{@2 : @1},\n"
  7133. "}");
  7134. verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
  7135. verifyIncompleteFormat("[self setDict:@{}");
  7136. verifyIncompleteFormat("[self setDict:@{@1 : @2}");
  7137. verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
  7138. verifyFormat(
  7139. "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
  7140. verifyFormat(
  7141. "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
  7142. verifyFormat("NSDictionary *d = @{\n"
  7143. " @\"nam\" : NSUserNam(),\n"
  7144. " @\"dte\" : [NSDate date],\n"
  7145. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  7146. "};");
  7147. verifyFormat(
  7148. "@{\n"
  7149. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  7150. "regularFont,\n"
  7151. "};");
  7152. verifyGoogleFormat(
  7153. "@{\n"
  7154. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  7155. "regularFont,\n"
  7156. "};");
  7157. verifyFormat(
  7158. "@{\n"
  7159. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
  7160. " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
  7161. "};");
  7162. // We should try to be robust in case someone forgets the "@".
  7163. verifyFormat("NSDictionary *d = {\n"
  7164. " @\"nam\" : NSUserNam(),\n"
  7165. " @\"dte\" : [NSDate date],\n"
  7166. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  7167. "};");
  7168. verifyFormat("NSMutableDictionary *dictionary =\n"
  7169. " [NSMutableDictionary dictionaryWithDictionary:@{\n"
  7170. " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
  7171. " bbbbbbbbbbbbbbbbbb : bbbbb,\n"
  7172. " cccccccccccccccc : ccccccccccccccc\n"
  7173. " }];");
  7174. // Ensure that casts before the key are kept on the same line as the key.
  7175. verifyFormat(
  7176. "NSDictionary *d = @{\n"
  7177. " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n"
  7178. " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n"
  7179. "};");
  7180. }
  7181. TEST_F(FormatTest, ObjCArrayLiterals) {
  7182. verifyIncompleteFormat("@[");
  7183. verifyFormat("@[]");
  7184. verifyFormat(
  7185. "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
  7186. verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
  7187. verifyFormat("NSArray *array = @[ [foo description] ];");
  7188. verifyFormat(
  7189. "NSArray *some_variable = @[\n"
  7190. " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
  7191. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7192. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7193. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7194. "];");
  7195. verifyFormat(
  7196. "NSArray *some_variable = @[\n"
  7197. " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
  7198. " @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n"
  7199. "];");
  7200. verifyFormat("NSArray *some_variable = @[\n"
  7201. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7202. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7203. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7204. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7205. "];");
  7206. verifyFormat("NSArray *array = @[\n"
  7207. " @\"a\",\n"
  7208. " @\"a\",\n" // Trailing comma -> one per line.
  7209. "];");
  7210. // We should try to be robust in case someone forgets the "@".
  7211. verifyFormat("NSArray *some_variable = [\n"
  7212. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7213. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7214. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7215. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7216. "];");
  7217. verifyFormat(
  7218. "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
  7219. " index:(NSUInteger)index\n"
  7220. " nonDigitAttributes:\n"
  7221. " (NSDictionary *)noDigitAttributes;");
  7222. verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
  7223. " NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
  7224. "]];");
  7225. }
  7226. TEST_F(FormatTest, BreaksStringLiterals) {
  7227. EXPECT_EQ("\"some text \"\n"
  7228. "\"other\";",
  7229. format("\"some text other\";", getLLVMStyleWithColumns(12)));
  7230. EXPECT_EQ("\"some text \"\n"
  7231. "\"other\";",
  7232. format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
  7233. EXPECT_EQ(
  7234. "#define A \\\n"
  7235. " \"some \" \\\n"
  7236. " \"text \" \\\n"
  7237. " \"other\";",
  7238. format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
  7239. EXPECT_EQ(
  7240. "#define A \\\n"
  7241. " \"so \" \\\n"
  7242. " \"text \" \\\n"
  7243. " \"other\";",
  7244. format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
  7245. EXPECT_EQ("\"some text\"",
  7246. format("\"some text\"", getLLVMStyleWithColumns(1)));
  7247. EXPECT_EQ("\"some text\"",
  7248. format("\"some text\"", getLLVMStyleWithColumns(11)));
  7249. EXPECT_EQ("\"some \"\n"
  7250. "\"text\"",
  7251. format("\"some text\"", getLLVMStyleWithColumns(10)));
  7252. EXPECT_EQ("\"some \"\n"
  7253. "\"text\"",
  7254. format("\"some text\"", getLLVMStyleWithColumns(7)));
  7255. EXPECT_EQ("\"some\"\n"
  7256. "\" tex\"\n"
  7257. "\"t\"",
  7258. format("\"some text\"", getLLVMStyleWithColumns(6)));
  7259. EXPECT_EQ("\"some\"\n"
  7260. "\" tex\"\n"
  7261. "\" and\"",
  7262. format("\"some tex and\"", getLLVMStyleWithColumns(6)));
  7263. EXPECT_EQ("\"some\"\n"
  7264. "\"/tex\"\n"
  7265. "\"/and\"",
  7266. format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
  7267. EXPECT_EQ("variable =\n"
  7268. " \"long string \"\n"
  7269. " \"literal\";",
  7270. format("variable = \"long string literal\";",
  7271. getLLVMStyleWithColumns(20)));
  7272. EXPECT_EQ("variable = f(\n"
  7273. " \"long string \"\n"
  7274. " \"literal\",\n"
  7275. " short,\n"
  7276. " loooooooooooooooooooong);",
  7277. format("variable = f(\"long string literal\", short, "
  7278. "loooooooooooooooooooong);",
  7279. getLLVMStyleWithColumns(20)));
  7280. EXPECT_EQ(
  7281. "f(g(\"long string \"\n"
  7282. " \"literal\"),\n"
  7283. " b);",
  7284. format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
  7285. EXPECT_EQ("f(g(\"long string \"\n"
  7286. " \"literal\",\n"
  7287. " a),\n"
  7288. " b);",
  7289. format("f(g(\"long string literal\", a), b);",
  7290. getLLVMStyleWithColumns(20)));
  7291. EXPECT_EQ(
  7292. "f(\"one two\".split(\n"
  7293. " variable));",
  7294. format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
  7295. EXPECT_EQ("f(\"one two three four five six \"\n"
  7296. " \"seven\".split(\n"
  7297. " really_looooong_variable));",
  7298. format("f(\"one two three four five six seven\"."
  7299. "split(really_looooong_variable));",
  7300. getLLVMStyleWithColumns(33)));
  7301. EXPECT_EQ("f(\"some \"\n"
  7302. " \"text\",\n"
  7303. " other);",
  7304. format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
  7305. // Only break as a last resort.
  7306. verifyFormat(
  7307. "aaaaaaaaaaaaaaaaaaaa(\n"
  7308. " aaaaaaaaaaaaaaaaaaaa,\n"
  7309. " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
  7310. EXPECT_EQ("\"splitmea\"\n"
  7311. "\"trandomp\"\n"
  7312. "\"oint\"",
  7313. format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
  7314. EXPECT_EQ("\"split/\"\n"
  7315. "\"pathat/\"\n"
  7316. "\"slashes\"",
  7317. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  7318. EXPECT_EQ("\"split/\"\n"
  7319. "\"pathat/\"\n"
  7320. "\"slashes\"",
  7321. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  7322. EXPECT_EQ("\"split at \"\n"
  7323. "\"spaces/at/\"\n"
  7324. "\"slashes.at.any$\"\n"
  7325. "\"non-alphanumeric%\"\n"
  7326. "\"1111111111characte\"\n"
  7327. "\"rs\"",
  7328. format("\"split at "
  7329. "spaces/at/"
  7330. "slashes.at."
  7331. "any$non-"
  7332. "alphanumeric%"
  7333. "1111111111characte"
  7334. "rs\"",
  7335. getLLVMStyleWithColumns(20)));
  7336. // Verify that splitting the strings understands
  7337. // Style::AlwaysBreakBeforeMultilineStrings.
  7338. EXPECT_EQ(
  7339. "aaaaaaaaaaaa(\n"
  7340. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
  7341. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
  7342. format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
  7343. "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
  7344. "aaaaaaaaaaaaaaaaaaaaaa\");",
  7345. getGoogleStyle()));
  7346. EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  7347. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
  7348. format("return \"aaaaaaaaaaaaaaaaaaaaaa "
  7349. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
  7350. "aaaaaaaaaaaaaaaaaaaaaa\";",
  7351. getGoogleStyle()));
  7352. EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  7353. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  7354. format("llvm::outs() << "
  7355. "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
  7356. "aaaaaaaaaaaaaaaaaaa\";"));
  7357. EXPECT_EQ("ffff(\n"
  7358. " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  7359. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  7360. format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  7361. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  7362. getGoogleStyle()));
  7363. FormatStyle Style = getLLVMStyleWithColumns(12);
  7364. Style.BreakStringLiterals = false;
  7365. EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
  7366. FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
  7367. AlignLeft.AlignEscapedNewlinesLeft = true;
  7368. EXPECT_EQ("#define A \\\n"
  7369. " \"some \" \\\n"
  7370. " \"text \" \\\n"
  7371. " \"other\";",
  7372. format("#define A \"some text other\";", AlignLeft));
  7373. }
  7374. TEST_F(FormatTest, FullyRemoveEmptyLines) {
  7375. FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
  7376. NoEmptyLines.MaxEmptyLinesToKeep = 0;
  7377. EXPECT_EQ("int i = a(b());",
  7378. format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
  7379. }
  7380. TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
  7381. EXPECT_EQ(
  7382. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  7383. "(\n"
  7384. " \"x\t\");",
  7385. format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  7386. "aaaaaaa("
  7387. "\"x\t\");"));
  7388. }
  7389. TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
  7390. EXPECT_EQ(
  7391. "u8\"utf8 string \"\n"
  7392. "u8\"literal\";",
  7393. format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
  7394. EXPECT_EQ(
  7395. "u\"utf16 string \"\n"
  7396. "u\"literal\";",
  7397. format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
  7398. EXPECT_EQ(
  7399. "U\"utf32 string \"\n"
  7400. "U\"literal\";",
  7401. format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
  7402. EXPECT_EQ("L\"wide string \"\n"
  7403. "L\"literal\";",
  7404. format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
  7405. EXPECT_EQ("@\"NSString \"\n"
  7406. "@\"literal\";",
  7407. format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
  7408. // This input makes clang-format try to split the incomplete unicode escape
  7409. // sequence, which used to lead to a crasher.
  7410. verifyNoCrash(
  7411. "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  7412. getLLVMStyleWithColumns(60));
  7413. }
  7414. TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
  7415. FormatStyle Style = getGoogleStyleWithColumns(15);
  7416. EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
  7417. EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
  7418. EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
  7419. EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
  7420. EXPECT_EQ("u8R\"x(raw literal)x\";",
  7421. format("u8R\"x(raw literal)x\";", Style));
  7422. }
  7423. TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
  7424. FormatStyle Style = getLLVMStyleWithColumns(20);
  7425. EXPECT_EQ(
  7426. "_T(\"aaaaaaaaaaaaaa\")\n"
  7427. "_T(\"aaaaaaaaaaaaaa\")\n"
  7428. "_T(\"aaaaaaaaaaaa\")",
  7429. format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
  7430. EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
  7431. " _T(\"aaaaaa\"),\n"
  7432. " z);",
  7433. format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
  7434. // FIXME: Handle embedded spaces in one iteration.
  7435. // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
  7436. // "_T(\"aaaaaaaaaaaaa\")\n"
  7437. // "_T(\"aaaaaaaaaaaaa\")\n"
  7438. // "_T(\"a\")",
  7439. // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  7440. // getLLVMStyleWithColumns(20)));
  7441. EXPECT_EQ(
  7442. "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  7443. format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
  7444. EXPECT_EQ("f(\n"
  7445. "#if !TEST\n"
  7446. " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
  7447. "#endif\n"
  7448. " );",
  7449. format("f(\n"
  7450. "#if !TEST\n"
  7451. "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
  7452. "#endif\n"
  7453. ");"));
  7454. EXPECT_EQ("f(\n"
  7455. "\n"
  7456. " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
  7457. format("f(\n"
  7458. "\n"
  7459. "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
  7460. }
  7461. TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
  7462. EXPECT_EQ(
  7463. "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7464. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7465. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  7466. format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7467. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7468. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
  7469. }
  7470. TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
  7471. EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
  7472. format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle()));
  7473. EXPECT_EQ("fffffffffff(g(R\"x(\n"
  7474. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7475. ")x\",\n"
  7476. " a),\n"
  7477. " b);",
  7478. format("fffffffffff(g(R\"x(\n"
  7479. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7480. ")x\", a), b);",
  7481. getGoogleStyleWithColumns(20)));
  7482. EXPECT_EQ("fffffffffff(\n"
  7483. " g(R\"x(qqq\n"
  7484. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7485. ")x\",\n"
  7486. " a),\n"
  7487. " b);",
  7488. format("fffffffffff(g(R\"x(qqq\n"
  7489. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7490. ")x\", a), b);",
  7491. getGoogleStyleWithColumns(20)));
  7492. EXPECT_EQ("fffffffffff(R\"x(\n"
  7493. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7494. ")x\");",
  7495. format("fffffffffff(R\"x(\n"
  7496. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7497. ")x\");",
  7498. getGoogleStyleWithColumns(20)));
  7499. EXPECT_EQ("fffffffffff(R\"x(\n"
  7500. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7501. ")x\" + bbbbbb);",
  7502. format("fffffffffff(R\"x(\n"
  7503. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7504. ")x\" + bbbbbb);",
  7505. getGoogleStyleWithColumns(20)));
  7506. EXPECT_EQ("fffffffffff(\n"
  7507. " R\"x(\n"
  7508. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7509. ")x\" +\n"
  7510. " bbbbbb);",
  7511. format("fffffffffff(\n"
  7512. " R\"x(\n"
  7513. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7514. ")x\" + bbbbbb);",
  7515. getGoogleStyleWithColumns(20)));
  7516. }
  7517. TEST_F(FormatTest, SkipsUnknownStringLiterals) {
  7518. verifyFormat("string a = \"unterminated;");
  7519. EXPECT_EQ("function(\"unterminated,\n"
  7520. " OtherParameter);",
  7521. format("function( \"unterminated,\n"
  7522. " OtherParameter);"));
  7523. }
  7524. TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
  7525. FormatStyle Style = getLLVMStyle();
  7526. Style.Standard = FormatStyle::LS_Cpp03;
  7527. EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
  7528. format("#define x(_a) printf(\"foo\"_a);", Style));
  7529. }
  7530. TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
  7531. TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
  7532. EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
  7533. " \"ddeeefff\");",
  7534. format("someFunction(\"aaabbbcccdddeeefff\");",
  7535. getLLVMStyleWithColumns(25)));
  7536. EXPECT_EQ("someFunction1234567890(\n"
  7537. " \"aaabbbcccdddeeefff\");",
  7538. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  7539. getLLVMStyleWithColumns(26)));
  7540. EXPECT_EQ("someFunction1234567890(\n"
  7541. " \"aaabbbcccdddeeeff\"\n"
  7542. " \"f\");",
  7543. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  7544. getLLVMStyleWithColumns(25)));
  7545. EXPECT_EQ("someFunction1234567890(\n"
  7546. " \"aaabbbcccdddeeeff\"\n"
  7547. " \"f\");",
  7548. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  7549. getLLVMStyleWithColumns(24)));
  7550. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  7551. " \"ddde \"\n"
  7552. " \"efff\");",
  7553. format("someFunction(\"aaabbbcc ddde efff\");",
  7554. getLLVMStyleWithColumns(25)));
  7555. EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
  7556. " \"ddeeefff\");",
  7557. format("someFunction(\"aaabbbccc ddeeefff\");",
  7558. getLLVMStyleWithColumns(25)));
  7559. EXPECT_EQ("someFunction1234567890(\n"
  7560. " \"aaabb \"\n"
  7561. " \"cccdddeeefff\");",
  7562. format("someFunction1234567890(\"aaabb cccdddeeefff\");",
  7563. getLLVMStyleWithColumns(25)));
  7564. EXPECT_EQ("#define A \\\n"
  7565. " string s = \\\n"
  7566. " \"123456789\" \\\n"
  7567. " \"0\"; \\\n"
  7568. " int i;",
  7569. format("#define A string s = \"1234567890\"; int i;",
  7570. getLLVMStyleWithColumns(20)));
  7571. // FIXME: Put additional penalties on breaking at non-whitespace locations.
  7572. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  7573. " \"dddeeeff\"\n"
  7574. " \"f\");",
  7575. format("someFunction(\"aaabbbcc dddeeefff\");",
  7576. getLLVMStyleWithColumns(25)));
  7577. }
  7578. TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
  7579. EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
  7580. EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
  7581. EXPECT_EQ("\"test\"\n"
  7582. "\"\\n\"",
  7583. format("\"test\\n\"", getLLVMStyleWithColumns(7)));
  7584. EXPECT_EQ("\"tes\\\\\"\n"
  7585. "\"n\"",
  7586. format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
  7587. EXPECT_EQ("\"\\\\\\\\\"\n"
  7588. "\"\\n\"",
  7589. format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
  7590. EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
  7591. EXPECT_EQ("\"\\uff01\"\n"
  7592. "\"test\"",
  7593. format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
  7594. EXPECT_EQ("\"\\Uff01ff02\"",
  7595. format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
  7596. EXPECT_EQ("\"\\x000000000001\"\n"
  7597. "\"next\"",
  7598. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
  7599. EXPECT_EQ("\"\\x000000000001next\"",
  7600. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
  7601. EXPECT_EQ("\"\\x000000000001\"",
  7602. format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
  7603. EXPECT_EQ("\"test\"\n"
  7604. "\"\\000000\"\n"
  7605. "\"000001\"",
  7606. format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
  7607. EXPECT_EQ("\"test\\000\"\n"
  7608. "\"00000000\"\n"
  7609. "\"1\"",
  7610. format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
  7611. }
  7612. TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
  7613. verifyFormat("void f() {\n"
  7614. " return g() {}\n"
  7615. " void h() {}");
  7616. verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
  7617. "g();\n"
  7618. "}");
  7619. }
  7620. TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
  7621. verifyFormat(
  7622. "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
  7623. }
  7624. TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
  7625. verifyFormat("class X {\n"
  7626. " void f() {\n"
  7627. " }\n"
  7628. "};",
  7629. getLLVMStyleWithColumns(12));
  7630. }
  7631. TEST_F(FormatTest, ConfigurableIndentWidth) {
  7632. FormatStyle EightIndent = getLLVMStyleWithColumns(18);
  7633. EightIndent.IndentWidth = 8;
  7634. EightIndent.ContinuationIndentWidth = 8;
  7635. verifyFormat("void f() {\n"
  7636. " someFunction();\n"
  7637. " if (true) {\n"
  7638. " f();\n"
  7639. " }\n"
  7640. "}",
  7641. EightIndent);
  7642. verifyFormat("class X {\n"
  7643. " void f() {\n"
  7644. " }\n"
  7645. "};",
  7646. EightIndent);
  7647. verifyFormat("int x[] = {\n"
  7648. " call(),\n"
  7649. " call()};",
  7650. EightIndent);
  7651. }
  7652. TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
  7653. verifyFormat("double\n"
  7654. "f();",
  7655. getLLVMStyleWithColumns(8));
  7656. }
  7657. TEST_F(FormatTest, ConfigurableUseOfTab) {
  7658. FormatStyle Tab = getLLVMStyleWithColumns(42);
  7659. Tab.IndentWidth = 8;
  7660. Tab.UseTab = FormatStyle::UT_Always;
  7661. Tab.AlignEscapedNewlinesLeft = true;
  7662. EXPECT_EQ("if (aaaaaaaa && // q\n"
  7663. " bb)\t\t// w\n"
  7664. "\t;",
  7665. format("if (aaaaaaaa &&// q\n"
  7666. "bb)// w\n"
  7667. ";",
  7668. Tab));
  7669. EXPECT_EQ("if (aaa && bbb) // w\n"
  7670. "\t;",
  7671. format("if(aaa&&bbb)// w\n"
  7672. ";",
  7673. Tab));
  7674. verifyFormat("class X {\n"
  7675. "\tvoid f() {\n"
  7676. "\t\tsomeFunction(parameter1,\n"
  7677. "\t\t\t parameter2);\n"
  7678. "\t}\n"
  7679. "};",
  7680. Tab);
  7681. verifyFormat("#define A \\\n"
  7682. "\tvoid f() { \\\n"
  7683. "\t\tsomeFunction( \\\n"
  7684. "\t\t parameter1, \\\n"
  7685. "\t\t parameter2); \\\n"
  7686. "\t}",
  7687. Tab);
  7688. Tab.TabWidth = 4;
  7689. Tab.IndentWidth = 8;
  7690. verifyFormat("class TabWidth4Indent8 {\n"
  7691. "\t\tvoid f() {\n"
  7692. "\t\t\t\tsomeFunction(parameter1,\n"
  7693. "\t\t\t\t\t\t\t parameter2);\n"
  7694. "\t\t}\n"
  7695. "};",
  7696. Tab);
  7697. Tab.TabWidth = 4;
  7698. Tab.IndentWidth = 4;
  7699. verifyFormat("class TabWidth4Indent4 {\n"
  7700. "\tvoid f() {\n"
  7701. "\t\tsomeFunction(parameter1,\n"
  7702. "\t\t\t\t\t parameter2);\n"
  7703. "\t}\n"
  7704. "};",
  7705. Tab);
  7706. Tab.TabWidth = 8;
  7707. Tab.IndentWidth = 4;
  7708. verifyFormat("class TabWidth8Indent4 {\n"
  7709. " void f() {\n"
  7710. "\tsomeFunction(parameter1,\n"
  7711. "\t\t parameter2);\n"
  7712. " }\n"
  7713. "};",
  7714. Tab);
  7715. Tab.TabWidth = 8;
  7716. Tab.IndentWidth = 8;
  7717. EXPECT_EQ("/*\n"
  7718. "\t a\t\tcomment\n"
  7719. "\t in multiple lines\n"
  7720. " */",
  7721. format(" /*\t \t \n"
  7722. " \t \t a\t\tcomment\t \t\n"
  7723. " \t \t in multiple lines\t\n"
  7724. " \t */",
  7725. Tab));
  7726. Tab.UseTab = FormatStyle::UT_ForIndentation;
  7727. verifyFormat("{\n"
  7728. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7729. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7730. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7731. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7732. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7733. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7734. "};",
  7735. Tab);
  7736. verifyFormat("enum AA {\n"
  7737. "\ta1, // Force multiple lines\n"
  7738. "\ta2,\n"
  7739. "\ta3\n"
  7740. "};",
  7741. Tab);
  7742. EXPECT_EQ("if (aaaaaaaa && // q\n"
  7743. " bb) // w\n"
  7744. "\t;",
  7745. format("if (aaaaaaaa &&// q\n"
  7746. "bb)// w\n"
  7747. ";",
  7748. Tab));
  7749. verifyFormat("class X {\n"
  7750. "\tvoid f() {\n"
  7751. "\t\tsomeFunction(parameter1,\n"
  7752. "\t\t parameter2);\n"
  7753. "\t}\n"
  7754. "};",
  7755. Tab);
  7756. verifyFormat("{\n"
  7757. "\tQ(\n"
  7758. "\t {\n"
  7759. "\t\t int a;\n"
  7760. "\t\t someFunction(aaaaaaaa,\n"
  7761. "\t\t bbbbbbb);\n"
  7762. "\t },\n"
  7763. "\t p);\n"
  7764. "}",
  7765. Tab);
  7766. EXPECT_EQ("{\n"
  7767. "\t/* aaaa\n"
  7768. "\t bbbb */\n"
  7769. "}",
  7770. format("{\n"
  7771. "/* aaaa\n"
  7772. " bbbb */\n"
  7773. "}",
  7774. Tab));
  7775. EXPECT_EQ("{\n"
  7776. "\t/*\n"
  7777. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7778. "\t bbbbbbbbbbbbb\n"
  7779. "\t*/\n"
  7780. "}",
  7781. format("{\n"
  7782. "/*\n"
  7783. " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7784. "*/\n"
  7785. "}",
  7786. Tab));
  7787. EXPECT_EQ("{\n"
  7788. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7789. "\t// bbbbbbbbbbbbb\n"
  7790. "}",
  7791. format("{\n"
  7792. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7793. "}",
  7794. Tab));
  7795. EXPECT_EQ("{\n"
  7796. "\t/*\n"
  7797. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7798. "\t bbbbbbbbbbbbb\n"
  7799. "\t*/\n"
  7800. "}",
  7801. format("{\n"
  7802. "\t/*\n"
  7803. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7804. "\t*/\n"
  7805. "}",
  7806. Tab));
  7807. EXPECT_EQ("{\n"
  7808. "\t/*\n"
  7809. "\n"
  7810. "\t*/\n"
  7811. "}",
  7812. format("{\n"
  7813. "\t/*\n"
  7814. "\n"
  7815. "\t*/\n"
  7816. "}",
  7817. Tab));
  7818. EXPECT_EQ("{\n"
  7819. "\t/*\n"
  7820. " asdf\n"
  7821. "\t*/\n"
  7822. "}",
  7823. format("{\n"
  7824. "\t/*\n"
  7825. " asdf\n"
  7826. "\t*/\n"
  7827. "}",
  7828. Tab));
  7829. Tab.UseTab = FormatStyle::UT_Never;
  7830. EXPECT_EQ("/*\n"
  7831. " a\t\tcomment\n"
  7832. " in multiple lines\n"
  7833. " */",
  7834. format(" /*\t \t \n"
  7835. " \t \t a\t\tcomment\t \t\n"
  7836. " \t \t in multiple lines\t\n"
  7837. " \t */",
  7838. Tab));
  7839. EXPECT_EQ("/* some\n"
  7840. " comment */",
  7841. format(" \t \t /* some\n"
  7842. " \t \t comment */",
  7843. Tab));
  7844. EXPECT_EQ("int a; /* some\n"
  7845. " comment */",
  7846. format(" \t \t int a; /* some\n"
  7847. " \t \t comment */",
  7848. Tab));
  7849. EXPECT_EQ("int a; /* some\n"
  7850. "comment */",
  7851. format(" \t \t int\ta; /* some\n"
  7852. " \t \t comment */",
  7853. Tab));
  7854. EXPECT_EQ("f(\"\t\t\"); /* some\n"
  7855. " comment */",
  7856. format(" \t \t f(\"\t\t\"); /* some\n"
  7857. " \t \t comment */",
  7858. Tab));
  7859. EXPECT_EQ("{\n"
  7860. " /*\n"
  7861. " * Comment\n"
  7862. " */\n"
  7863. " int i;\n"
  7864. "}",
  7865. format("{\n"
  7866. "\t/*\n"
  7867. "\t * Comment\n"
  7868. "\t */\n"
  7869. "\t int i;\n"
  7870. "}"));
  7871. }
  7872. TEST_F(FormatTest, CalculatesOriginalColumn) {
  7873. EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7874. "q\"; /* some\n"
  7875. " comment */",
  7876. format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7877. "q\"; /* some\n"
  7878. " comment */",
  7879. getLLVMStyle()));
  7880. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  7881. "/* some\n"
  7882. " comment */",
  7883. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  7884. " /* some\n"
  7885. " comment */",
  7886. getLLVMStyle()));
  7887. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7888. "qqq\n"
  7889. "/* some\n"
  7890. " comment */",
  7891. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7892. "qqq\n"
  7893. " /* some\n"
  7894. " comment */",
  7895. getLLVMStyle()));
  7896. EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7897. "wwww; /* some\n"
  7898. " comment */",
  7899. format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7900. "wwww; /* some\n"
  7901. " comment */",
  7902. getLLVMStyle()));
  7903. }
  7904. TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
  7905. FormatStyle NoSpace = getLLVMStyle();
  7906. NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
  7907. verifyFormat("while(true)\n"
  7908. " continue;",
  7909. NoSpace);
  7910. verifyFormat("for(;;)\n"
  7911. " continue;",
  7912. NoSpace);
  7913. verifyFormat("if(true)\n"
  7914. " f();\n"
  7915. "else if(true)\n"
  7916. " f();",
  7917. NoSpace);
  7918. verifyFormat("do {\n"
  7919. " do_something();\n"
  7920. "} while(something());",
  7921. NoSpace);
  7922. verifyFormat("switch(x) {\n"
  7923. "default:\n"
  7924. " break;\n"
  7925. "}",
  7926. NoSpace);
  7927. verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
  7928. verifyFormat("size_t x = sizeof(x);", NoSpace);
  7929. verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
  7930. verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
  7931. verifyFormat("alignas(128) char a[128];", NoSpace);
  7932. verifyFormat("size_t x = alignof(MyType);", NoSpace);
  7933. verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
  7934. verifyFormat("int f() throw(Deprecated);", NoSpace);
  7935. verifyFormat("typedef void (*cb)(int);", NoSpace);
  7936. verifyFormat("T A::operator()();", NoSpace);
  7937. verifyFormat("X A::operator++(T);", NoSpace);
  7938. FormatStyle Space = getLLVMStyle();
  7939. Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
  7940. verifyFormat("int f ();", Space);
  7941. verifyFormat("void f (int a, T b) {\n"
  7942. " while (true)\n"
  7943. " continue;\n"
  7944. "}",
  7945. Space);
  7946. verifyFormat("if (true)\n"
  7947. " f ();\n"
  7948. "else if (true)\n"
  7949. " f ();",
  7950. Space);
  7951. verifyFormat("do {\n"
  7952. " do_something ();\n"
  7953. "} while (something ());",
  7954. Space);
  7955. verifyFormat("switch (x) {\n"
  7956. "default:\n"
  7957. " break;\n"
  7958. "}",
  7959. Space);
  7960. verifyFormat("A::A () : a (1) {}", Space);
  7961. verifyFormat("void f () __attribute__ ((asdf));", Space);
  7962. verifyFormat("*(&a + 1);\n"
  7963. "&((&a)[1]);\n"
  7964. "a[(b + c) * d];\n"
  7965. "(((a + 1) * 2) + 3) * 4;",
  7966. Space);
  7967. verifyFormat("#define A(x) x", Space);
  7968. verifyFormat("#define A (x) x", Space);
  7969. verifyFormat("#if defined(x)\n"
  7970. "#endif",
  7971. Space);
  7972. verifyFormat("auto i = std::make_unique<int> (5);", Space);
  7973. verifyFormat("size_t x = sizeof (x);", Space);
  7974. verifyFormat("auto f (int x) -> decltype (x);", Space);
  7975. verifyFormat("int f (T x) noexcept (x.create ());", Space);
  7976. verifyFormat("alignas (128) char a[128];", Space);
  7977. verifyFormat("size_t x = alignof (MyType);", Space);
  7978. verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
  7979. verifyFormat("int f () throw (Deprecated);", Space);
  7980. verifyFormat("typedef void (*cb) (int);", Space);
  7981. verifyFormat("T A::operator() ();", Space);
  7982. verifyFormat("X A::operator++ (T);", Space);
  7983. }
  7984. TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
  7985. FormatStyle Spaces = getLLVMStyle();
  7986. Spaces.SpacesInParentheses = true;
  7987. verifyFormat("call( x, y, z );", Spaces);
  7988. verifyFormat("call();", Spaces);
  7989. verifyFormat("std::function<void( int, int )> callback;", Spaces);
  7990. verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
  7991. Spaces);
  7992. verifyFormat("while ( (bool)1 )\n"
  7993. " continue;",
  7994. Spaces);
  7995. verifyFormat("for ( ;; )\n"
  7996. " continue;",
  7997. Spaces);
  7998. verifyFormat("if ( true )\n"
  7999. " f();\n"
  8000. "else if ( true )\n"
  8001. " f();",
  8002. Spaces);
  8003. verifyFormat("do {\n"
  8004. " do_something( (int)i );\n"
  8005. "} while ( something() );",
  8006. Spaces);
  8007. verifyFormat("switch ( x ) {\n"
  8008. "default:\n"
  8009. " break;\n"
  8010. "}",
  8011. Spaces);
  8012. Spaces.SpacesInParentheses = false;
  8013. Spaces.SpacesInCStyleCastParentheses = true;
  8014. verifyFormat("Type *A = ( Type * )P;", Spaces);
  8015. verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
  8016. verifyFormat("x = ( int32 )y;", Spaces);
  8017. verifyFormat("int a = ( int )(2.0f);", Spaces);
  8018. verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
  8019. verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
  8020. verifyFormat("#define x (( int )-1)", Spaces);
  8021. // Run the first set of tests again with:
  8022. Spaces.SpacesInParentheses = false, Spaces.SpaceInEmptyParentheses = true;
  8023. Spaces.SpacesInCStyleCastParentheses = true;
  8024. verifyFormat("call(x, y, z);", Spaces);
  8025. verifyFormat("call( );", Spaces);
  8026. verifyFormat("std::function<void(int, int)> callback;", Spaces);
  8027. verifyFormat("while (( bool )1)\n"
  8028. " continue;",
  8029. Spaces);
  8030. verifyFormat("for (;;)\n"
  8031. " continue;",
  8032. Spaces);
  8033. verifyFormat("if (true)\n"
  8034. " f( );\n"
  8035. "else if (true)\n"
  8036. " f( );",
  8037. Spaces);
  8038. verifyFormat("do {\n"
  8039. " do_something(( int )i);\n"
  8040. "} while (something( ));",
  8041. Spaces);
  8042. verifyFormat("switch (x) {\n"
  8043. "default:\n"
  8044. " break;\n"
  8045. "}",
  8046. Spaces);
  8047. // Run the first set of tests again with:
  8048. Spaces.SpaceAfterCStyleCast = true;
  8049. verifyFormat("call(x, y, z);", Spaces);
  8050. verifyFormat("call( );", Spaces);
  8051. verifyFormat("std::function<void(int, int)> callback;", Spaces);
  8052. verifyFormat("while (( bool ) 1)\n"
  8053. " continue;",
  8054. Spaces);
  8055. verifyFormat("for (;;)\n"
  8056. " continue;",
  8057. Spaces);
  8058. verifyFormat("if (true)\n"
  8059. " f( );\n"
  8060. "else if (true)\n"
  8061. " f( );",
  8062. Spaces);
  8063. verifyFormat("do {\n"
  8064. " do_something(( int ) i);\n"
  8065. "} while (something( ));",
  8066. Spaces);
  8067. verifyFormat("switch (x) {\n"
  8068. "default:\n"
  8069. " break;\n"
  8070. "}",
  8071. Spaces);
  8072. // Run subset of tests again with:
  8073. Spaces.SpacesInCStyleCastParentheses = false;
  8074. Spaces.SpaceAfterCStyleCast = true;
  8075. verifyFormat("while ((bool) 1)\n"
  8076. " continue;",
  8077. Spaces);
  8078. verifyFormat("do {\n"
  8079. " do_something((int) i);\n"
  8080. "} while (something( ));",
  8081. Spaces);
  8082. }
  8083. TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
  8084. verifyFormat("int a[5];");
  8085. verifyFormat("a[3] += 42;");
  8086. FormatStyle Spaces = getLLVMStyle();
  8087. Spaces.SpacesInSquareBrackets = true;
  8088. // Lambdas unchanged.
  8089. verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
  8090. verifyFormat("return [i, args...] {};", Spaces);
  8091. // Not lambdas.
  8092. verifyFormat("int a[ 5 ];", Spaces);
  8093. verifyFormat("a[ 3 ] += 42;", Spaces);
  8094. verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
  8095. verifyFormat("double &operator[](int i) { return 0; }\n"
  8096. "int i;",
  8097. Spaces);
  8098. verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
  8099. verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
  8100. verifyFormat("int i = (*b)[ a ]->f();", Spaces);
  8101. }
  8102. TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
  8103. verifyFormat("int a = 5;");
  8104. verifyFormat("a += 42;");
  8105. verifyFormat("a or_eq 8;");
  8106. FormatStyle Spaces = getLLVMStyle();
  8107. Spaces.SpaceBeforeAssignmentOperators = false;
  8108. verifyFormat("int a= 5;", Spaces);
  8109. verifyFormat("a+= 42;", Spaces);
  8110. verifyFormat("a or_eq 8;", Spaces);
  8111. }
  8112. TEST_F(FormatTest, AlignConsecutiveAssignments) {
  8113. FormatStyle Alignment = getLLVMStyle();
  8114. Alignment.AlignConsecutiveAssignments = false;
  8115. verifyFormat("int a = 5;\n"
  8116. "int oneTwoThree = 123;",
  8117. Alignment);
  8118. verifyFormat("int a = 5;\n"
  8119. "int oneTwoThree = 123;",
  8120. Alignment);
  8121. Alignment.AlignConsecutiveAssignments = true;
  8122. verifyFormat("int a = 5;\n"
  8123. "int oneTwoThree = 123;",
  8124. Alignment);
  8125. verifyFormat("int a = method();\n"
  8126. "int oneTwoThree = 133;",
  8127. Alignment);
  8128. verifyFormat("a &= 5;\n"
  8129. "bcd *= 5;\n"
  8130. "ghtyf += 5;\n"
  8131. "dvfvdb -= 5;\n"
  8132. "a /= 5;\n"
  8133. "vdsvsv %= 5;\n"
  8134. "sfdbddfbdfbb ^= 5;\n"
  8135. "dvsdsv |= 5;\n"
  8136. "int dsvvdvsdvvv = 123;",
  8137. Alignment);
  8138. verifyFormat("int i = 1, j = 10;\n"
  8139. "something = 2000;",
  8140. Alignment);
  8141. verifyFormat("something = 2000;\n"
  8142. "int i = 1, j = 10;\n",
  8143. Alignment);
  8144. verifyFormat("something = 2000;\n"
  8145. "another = 911;\n"
  8146. "int i = 1, j = 10;\n"
  8147. "oneMore = 1;\n"
  8148. "i = 2;",
  8149. Alignment);
  8150. verifyFormat("int a = 5;\n"
  8151. "int one = 1;\n"
  8152. "method();\n"
  8153. "int oneTwoThree = 123;\n"
  8154. "int oneTwo = 12;",
  8155. Alignment);
  8156. verifyFormat("int oneTwoThree = 123;\n"
  8157. "int oneTwo = 12;\n"
  8158. "method();\n",
  8159. Alignment);
  8160. verifyFormat("int oneTwoThree = 123; // comment\n"
  8161. "int oneTwo = 12; // comment",
  8162. Alignment);
  8163. EXPECT_EQ("int a = 5;\n"
  8164. "\n"
  8165. "int oneTwoThree = 123;",
  8166. format("int a = 5;\n"
  8167. "\n"
  8168. "int oneTwoThree= 123;",
  8169. Alignment));
  8170. EXPECT_EQ("int a = 5;\n"
  8171. "int one = 1;\n"
  8172. "\n"
  8173. "int oneTwoThree = 123;",
  8174. format("int a = 5;\n"
  8175. "int one = 1;\n"
  8176. "\n"
  8177. "int oneTwoThree = 123;",
  8178. Alignment));
  8179. EXPECT_EQ("int a = 5;\n"
  8180. "int one = 1;\n"
  8181. "\n"
  8182. "int oneTwoThree = 123;\n"
  8183. "int oneTwo = 12;",
  8184. format("int a = 5;\n"
  8185. "int one = 1;\n"
  8186. "\n"
  8187. "int oneTwoThree = 123;\n"
  8188. "int oneTwo = 12;",
  8189. Alignment));
  8190. Alignment.AlignEscapedNewlinesLeft = true;
  8191. verifyFormat("#define A \\\n"
  8192. " int aaaa = 12; \\\n"
  8193. " int b = 23; \\\n"
  8194. " int ccc = 234; \\\n"
  8195. " int dddddddddd = 2345;",
  8196. Alignment);
  8197. Alignment.AlignEscapedNewlinesLeft = false;
  8198. verifyFormat("#define A "
  8199. " \\\n"
  8200. " int aaaa = 12; "
  8201. " \\\n"
  8202. " int b = 23; "
  8203. " \\\n"
  8204. " int ccc = 234; "
  8205. " \\\n"
  8206. " int dddddddddd = 2345;",
  8207. Alignment);
  8208. verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
  8209. "k = 4, int l = 5,\n"
  8210. " int m = 6) {\n"
  8211. " int j = 10;\n"
  8212. " otherThing = 1;\n"
  8213. "}",
  8214. Alignment);
  8215. verifyFormat("void SomeFunction(int parameter = 0) {\n"
  8216. " int i = 1;\n"
  8217. " int j = 2;\n"
  8218. " int big = 10000;\n"
  8219. "}",
  8220. Alignment);
  8221. verifyFormat("class C {\n"
  8222. "public:\n"
  8223. " int i = 1;\n"
  8224. " virtual void f() = 0;\n"
  8225. "};",
  8226. Alignment);
  8227. verifyFormat("int i = 1;\n"
  8228. "if (SomeType t = getSomething()) {\n"
  8229. "}\n"
  8230. "int j = 2;\n"
  8231. "int big = 10000;",
  8232. Alignment);
  8233. verifyFormat("int j = 7;\n"
  8234. "for (int k = 0; k < N; ++k) {\n"
  8235. "}\n"
  8236. "int j = 2;\n"
  8237. "int big = 10000;\n"
  8238. "}",
  8239. Alignment);
  8240. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  8241. verifyFormat("int i = 1;\n"
  8242. "LooooooooooongType loooooooooooooooooooooongVariable\n"
  8243. " = someLooooooooooooooooongFunction();\n"
  8244. "int j = 2;",
  8245. Alignment);
  8246. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
  8247. verifyFormat("int i = 1;\n"
  8248. "LooooooooooongType loooooooooooooooooooooongVariable =\n"
  8249. " someLooooooooooooooooongFunction();\n"
  8250. "int j = 2;",
  8251. Alignment);
  8252. verifyFormat("auto lambda = []() {\n"
  8253. " auto i = 0;\n"
  8254. " return 0;\n"
  8255. "};\n"
  8256. "int i = 0;\n"
  8257. "auto v = type{\n"
  8258. " i = 1, //\n"
  8259. " (i = 2), //\n"
  8260. " i = 3 //\n"
  8261. "};",
  8262. Alignment);
  8263. // FIXME: Should align all three assignments
  8264. verifyFormat(
  8265. "int i = 1;\n"
  8266. "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
  8267. " loooooooooooooooooooooongParameterB);\n"
  8268. "int j = 2;",
  8269. Alignment);
  8270. verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
  8271. " typename B = very_long_type_name_1,\n"
  8272. " typename T_2 = very_long_type_name_2>\n"
  8273. "auto foo() {}\n",
  8274. Alignment);
  8275. verifyFormat("int a, b = 1;\n"
  8276. "int c = 2;\n"
  8277. "int dd = 3;\n",
  8278. Alignment);
  8279. verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
  8280. "float b[1][] = {{3.f}};\n",
  8281. Alignment);
  8282. }
  8283. TEST_F(FormatTest, AlignConsecutiveDeclarations) {
  8284. FormatStyle Alignment = getLLVMStyle();
  8285. Alignment.AlignConsecutiveDeclarations = false;
  8286. verifyFormat("float const a = 5;\n"
  8287. "int oneTwoThree = 123;",
  8288. Alignment);
  8289. verifyFormat("int a = 5;\n"
  8290. "float const oneTwoThree = 123;",
  8291. Alignment);
  8292. Alignment.AlignConsecutiveDeclarations = true;
  8293. verifyFormat("float const a = 5;\n"
  8294. "int oneTwoThree = 123;",
  8295. Alignment);
  8296. verifyFormat("int a = method();\n"
  8297. "float const oneTwoThree = 133;",
  8298. Alignment);
  8299. verifyFormat("int i = 1, j = 10;\n"
  8300. "something = 2000;",
  8301. Alignment);
  8302. verifyFormat("something = 2000;\n"
  8303. "int i = 1, j = 10;\n",
  8304. Alignment);
  8305. verifyFormat("float something = 2000;\n"
  8306. "double another = 911;\n"
  8307. "int i = 1, j = 10;\n"
  8308. "const int *oneMore = 1;\n"
  8309. "unsigned i = 2;",
  8310. Alignment);
  8311. verifyFormat("float a = 5;\n"
  8312. "int one = 1;\n"
  8313. "method();\n"
  8314. "const double oneTwoThree = 123;\n"
  8315. "const unsigned int oneTwo = 12;",
  8316. Alignment);
  8317. verifyFormat("int oneTwoThree{0}; // comment\n"
  8318. "unsigned oneTwo; // comment",
  8319. Alignment);
  8320. EXPECT_EQ("float const a = 5;\n"
  8321. "\n"
  8322. "int oneTwoThree = 123;",
  8323. format("float const a = 5;\n"
  8324. "\n"
  8325. "int oneTwoThree= 123;",
  8326. Alignment));
  8327. EXPECT_EQ("float a = 5;\n"
  8328. "int one = 1;\n"
  8329. "\n"
  8330. "unsigned oneTwoThree = 123;",
  8331. format("float a = 5;\n"
  8332. "int one = 1;\n"
  8333. "\n"
  8334. "unsigned oneTwoThree = 123;",
  8335. Alignment));
  8336. EXPECT_EQ("float a = 5;\n"
  8337. "int one = 1;\n"
  8338. "\n"
  8339. "unsigned oneTwoThree = 123;\n"
  8340. "int oneTwo = 12;",
  8341. format("float a = 5;\n"
  8342. "int one = 1;\n"
  8343. "\n"
  8344. "unsigned oneTwoThree = 123;\n"
  8345. "int oneTwo = 12;",
  8346. Alignment));
  8347. Alignment.AlignConsecutiveAssignments = true;
  8348. verifyFormat("float something = 2000;\n"
  8349. "double another = 911;\n"
  8350. "int i = 1, j = 10;\n"
  8351. "const int *oneMore = 1;\n"
  8352. "unsigned i = 2;",
  8353. Alignment);
  8354. verifyFormat("int oneTwoThree = {0}; // comment\n"
  8355. "unsigned oneTwo = 0; // comment",
  8356. Alignment);
  8357. EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
  8358. " int const i = 1;\n"
  8359. " int * j = 2;\n"
  8360. " int big = 10000;\n"
  8361. "\n"
  8362. " unsigned oneTwoThree = 123;\n"
  8363. " int oneTwo = 12;\n"
  8364. " method();\n"
  8365. " float k = 2;\n"
  8366. " int ll = 10000;\n"
  8367. "}",
  8368. format("void SomeFunction(int parameter= 0) {\n"
  8369. " int const i= 1;\n"
  8370. " int *j=2;\n"
  8371. " int big = 10000;\n"
  8372. "\n"
  8373. "unsigned oneTwoThree =123;\n"
  8374. "int oneTwo = 12;\n"
  8375. " method();\n"
  8376. "float k= 2;\n"
  8377. "int ll=10000;\n"
  8378. "}",
  8379. Alignment));
  8380. Alignment.AlignConsecutiveAssignments = false;
  8381. Alignment.AlignEscapedNewlinesLeft = true;
  8382. verifyFormat("#define A \\\n"
  8383. " int aaaa = 12; \\\n"
  8384. " float b = 23; \\\n"
  8385. " const int ccc = 234; \\\n"
  8386. " unsigned dddddddddd = 2345;",
  8387. Alignment);
  8388. Alignment.AlignEscapedNewlinesLeft = false;
  8389. Alignment.ColumnLimit = 30;
  8390. verifyFormat("#define A \\\n"
  8391. " int aaaa = 12; \\\n"
  8392. " float b = 23; \\\n"
  8393. " const int ccc = 234; \\\n"
  8394. " int dddddddddd = 2345;",
  8395. Alignment);
  8396. Alignment.ColumnLimit = 80;
  8397. verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
  8398. "k = 4, int l = 5,\n"
  8399. " int m = 6) {\n"
  8400. " const int j = 10;\n"
  8401. " otherThing = 1;\n"
  8402. "}",
  8403. Alignment);
  8404. verifyFormat("void SomeFunction(int parameter = 0) {\n"
  8405. " int const i = 1;\n"
  8406. " int * j = 2;\n"
  8407. " int big = 10000;\n"
  8408. "}",
  8409. Alignment);
  8410. verifyFormat("class C {\n"
  8411. "public:\n"
  8412. " int i = 1;\n"
  8413. " virtual void f() = 0;\n"
  8414. "};",
  8415. Alignment);
  8416. verifyFormat("float i = 1;\n"
  8417. "if (SomeType t = getSomething()) {\n"
  8418. "}\n"
  8419. "const unsigned j = 2;\n"
  8420. "int big = 10000;",
  8421. Alignment);
  8422. verifyFormat("float j = 7;\n"
  8423. "for (int k = 0; k < N; ++k) {\n"
  8424. "}\n"
  8425. "unsigned j = 2;\n"
  8426. "int big = 10000;\n"
  8427. "}",
  8428. Alignment);
  8429. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  8430. verifyFormat("float i = 1;\n"
  8431. "LooooooooooongType loooooooooooooooooooooongVariable\n"
  8432. " = someLooooooooooooooooongFunction();\n"
  8433. "int j = 2;",
  8434. Alignment);
  8435. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
  8436. verifyFormat("int i = 1;\n"
  8437. "LooooooooooongType loooooooooooooooooooooongVariable =\n"
  8438. " someLooooooooooooooooongFunction();\n"
  8439. "int j = 2;",
  8440. Alignment);
  8441. Alignment.AlignConsecutiveAssignments = true;
  8442. verifyFormat("auto lambda = []() {\n"
  8443. " auto ii = 0;\n"
  8444. " float j = 0;\n"
  8445. " return 0;\n"
  8446. "};\n"
  8447. "int i = 0;\n"
  8448. "float i2 = 0;\n"
  8449. "auto v = type{\n"
  8450. " i = 1, //\n"
  8451. " (i = 2), //\n"
  8452. " i = 3 //\n"
  8453. "};",
  8454. Alignment);
  8455. Alignment.AlignConsecutiveAssignments = false;
  8456. // FIXME: Should align all three declarations
  8457. verifyFormat(
  8458. "int i = 1;\n"
  8459. "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
  8460. " loooooooooooooooooooooongParameterB);\n"
  8461. "int j = 2;",
  8462. Alignment);
  8463. // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
  8464. // We expect declarations and assignments to align, as long as it doesn't
  8465. // exceed the column limit, starting a new alignemnt sequence whenever it
  8466. // happens.
  8467. Alignment.AlignConsecutiveAssignments = true;
  8468. Alignment.ColumnLimit = 30;
  8469. verifyFormat("float ii = 1;\n"
  8470. "unsigned j = 2;\n"
  8471. "int someVerylongVariable = 1;\n"
  8472. "AnotherLongType ll = 123456;\n"
  8473. "VeryVeryLongType k = 2;\n"
  8474. "int myvar = 1;",
  8475. Alignment);
  8476. Alignment.ColumnLimit = 80;
  8477. Alignment.AlignConsecutiveAssignments = false;
  8478. verifyFormat(
  8479. "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
  8480. " typename LongType, typename B>\n"
  8481. "auto foo() {}\n",
  8482. Alignment);
  8483. verifyFormat("float a, b = 1;\n"
  8484. "int c = 2;\n"
  8485. "int dd = 3;\n",
  8486. Alignment);
  8487. verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
  8488. "float b[1][] = {{3.f}};\n",
  8489. Alignment);
  8490. Alignment.AlignConsecutiveAssignments = true;
  8491. verifyFormat("float a, b = 1;\n"
  8492. "int c = 2;\n"
  8493. "int dd = 3;\n",
  8494. Alignment);
  8495. verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
  8496. "float b[1][] = {{3.f}};\n",
  8497. Alignment);
  8498. Alignment.AlignConsecutiveAssignments = false;
  8499. Alignment.ColumnLimit = 30;
  8500. Alignment.BinPackParameters = false;
  8501. verifyFormat("void foo(float a,\n"
  8502. " float b,\n"
  8503. " int c,\n"
  8504. " uint32_t *d) {\n"
  8505. " int * e = 0;\n"
  8506. " float f = 0;\n"
  8507. " double g = 0;\n"
  8508. "}\n"
  8509. "void bar(ino_t a,\n"
  8510. " int b,\n"
  8511. " uint32_t *c,\n"
  8512. " bool d) {}\n",
  8513. Alignment);
  8514. Alignment.BinPackParameters = true;
  8515. Alignment.ColumnLimit = 80;
  8516. }
  8517. TEST_F(FormatTest, LinuxBraceBreaking) {
  8518. FormatStyle LinuxBraceStyle = getLLVMStyle();
  8519. LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
  8520. verifyFormat("namespace a\n"
  8521. "{\n"
  8522. "class A\n"
  8523. "{\n"
  8524. " void f()\n"
  8525. " {\n"
  8526. " if (true) {\n"
  8527. " a();\n"
  8528. " b();\n"
  8529. " } else {\n"
  8530. " a();\n"
  8531. " }\n"
  8532. " }\n"
  8533. " void g() { return; }\n"
  8534. "};\n"
  8535. "struct B {\n"
  8536. " int x;\n"
  8537. "};\n"
  8538. "}\n",
  8539. LinuxBraceStyle);
  8540. verifyFormat("enum X {\n"
  8541. " Y = 0,\n"
  8542. "}\n",
  8543. LinuxBraceStyle);
  8544. verifyFormat("struct S {\n"
  8545. " int Type;\n"
  8546. " union {\n"
  8547. " int x;\n"
  8548. " double y;\n"
  8549. " } Value;\n"
  8550. " class C\n"
  8551. " {\n"
  8552. " MyFavoriteType Value;\n"
  8553. " } Class;\n"
  8554. "}\n",
  8555. LinuxBraceStyle);
  8556. }
  8557. TEST_F(FormatTest, MozillaBraceBreaking) {
  8558. FormatStyle MozillaBraceStyle = getLLVMStyle();
  8559. MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
  8560. verifyFormat("namespace a {\n"
  8561. "class A\n"
  8562. "{\n"
  8563. " void f()\n"
  8564. " {\n"
  8565. " if (true) {\n"
  8566. " a();\n"
  8567. " b();\n"
  8568. " }\n"
  8569. " }\n"
  8570. " void g() { return; }\n"
  8571. "};\n"
  8572. "enum E\n"
  8573. "{\n"
  8574. " A,\n"
  8575. " // foo\n"
  8576. " B,\n"
  8577. " C\n"
  8578. "};\n"
  8579. "struct B\n"
  8580. "{\n"
  8581. " int x;\n"
  8582. "};\n"
  8583. "}\n",
  8584. MozillaBraceStyle);
  8585. verifyFormat("struct S\n"
  8586. "{\n"
  8587. " int Type;\n"
  8588. " union\n"
  8589. " {\n"
  8590. " int x;\n"
  8591. " double y;\n"
  8592. " } Value;\n"
  8593. " class C\n"
  8594. " {\n"
  8595. " MyFavoriteType Value;\n"
  8596. " } Class;\n"
  8597. "}\n",
  8598. MozillaBraceStyle);
  8599. }
  8600. TEST_F(FormatTest, StroustrupBraceBreaking) {
  8601. FormatStyle StroustrupBraceStyle = getLLVMStyle();
  8602. StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  8603. verifyFormat("namespace a {\n"
  8604. "class A {\n"
  8605. " void f()\n"
  8606. " {\n"
  8607. " if (true) {\n"
  8608. " a();\n"
  8609. " b();\n"
  8610. " }\n"
  8611. " }\n"
  8612. " void g() { return; }\n"
  8613. "};\n"
  8614. "struct B {\n"
  8615. " int x;\n"
  8616. "};\n"
  8617. "}\n",
  8618. StroustrupBraceStyle);
  8619. verifyFormat("void foo()\n"
  8620. "{\n"
  8621. " if (a) {\n"
  8622. " a();\n"
  8623. " }\n"
  8624. " else {\n"
  8625. " b();\n"
  8626. " }\n"
  8627. "}\n",
  8628. StroustrupBraceStyle);
  8629. verifyFormat("#ifdef _DEBUG\n"
  8630. "int foo(int i = 0)\n"
  8631. "#else\n"
  8632. "int foo(int i = 5)\n"
  8633. "#endif\n"
  8634. "{\n"
  8635. " return i;\n"
  8636. "}",
  8637. StroustrupBraceStyle);
  8638. verifyFormat("void foo() {}\n"
  8639. "void bar()\n"
  8640. "#ifdef _DEBUG\n"
  8641. "{\n"
  8642. " foo();\n"
  8643. "}\n"
  8644. "#else\n"
  8645. "{\n"
  8646. "}\n"
  8647. "#endif",
  8648. StroustrupBraceStyle);
  8649. verifyFormat("void foobar() { int i = 5; }\n"
  8650. "#ifdef _DEBUG\n"
  8651. "void bar() {}\n"
  8652. "#else\n"
  8653. "void bar() { foobar(); }\n"
  8654. "#endif",
  8655. StroustrupBraceStyle);
  8656. }
  8657. TEST_F(FormatTest, AllmanBraceBreaking) {
  8658. FormatStyle AllmanBraceStyle = getLLVMStyle();
  8659. AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
  8660. verifyFormat("namespace a\n"
  8661. "{\n"
  8662. "class A\n"
  8663. "{\n"
  8664. " void f()\n"
  8665. " {\n"
  8666. " if (true)\n"
  8667. " {\n"
  8668. " a();\n"
  8669. " b();\n"
  8670. " }\n"
  8671. " }\n"
  8672. " void g() { return; }\n"
  8673. "};\n"
  8674. "struct B\n"
  8675. "{\n"
  8676. " int x;\n"
  8677. "};\n"
  8678. "}",
  8679. AllmanBraceStyle);
  8680. verifyFormat("void f()\n"
  8681. "{\n"
  8682. " if (true)\n"
  8683. " {\n"
  8684. " a();\n"
  8685. " }\n"
  8686. " else if (false)\n"
  8687. " {\n"
  8688. " b();\n"
  8689. " }\n"
  8690. " else\n"
  8691. " {\n"
  8692. " c();\n"
  8693. " }\n"
  8694. "}\n",
  8695. AllmanBraceStyle);
  8696. verifyFormat("void f()\n"
  8697. "{\n"
  8698. " for (int i = 0; i < 10; ++i)\n"
  8699. " {\n"
  8700. " a();\n"
  8701. " }\n"
  8702. " while (false)\n"
  8703. " {\n"
  8704. " b();\n"
  8705. " }\n"
  8706. " do\n"
  8707. " {\n"
  8708. " c();\n"
  8709. " } while (false)\n"
  8710. "}\n",
  8711. AllmanBraceStyle);
  8712. verifyFormat("void f(int a)\n"
  8713. "{\n"
  8714. " switch (a)\n"
  8715. " {\n"
  8716. " case 0:\n"
  8717. " break;\n"
  8718. " case 1:\n"
  8719. " {\n"
  8720. " break;\n"
  8721. " }\n"
  8722. " case 2:\n"
  8723. " {\n"
  8724. " }\n"
  8725. " break;\n"
  8726. " default:\n"
  8727. " break;\n"
  8728. " }\n"
  8729. "}\n",
  8730. AllmanBraceStyle);
  8731. verifyFormat("enum X\n"
  8732. "{\n"
  8733. " Y = 0,\n"
  8734. "}\n",
  8735. AllmanBraceStyle);
  8736. verifyFormat("enum X\n"
  8737. "{\n"
  8738. " Y = 0\n"
  8739. "}\n",
  8740. AllmanBraceStyle);
  8741. verifyFormat("@interface BSApplicationController ()\n"
  8742. "{\n"
  8743. "@private\n"
  8744. " id _extraIvar;\n"
  8745. "}\n"
  8746. "@end\n",
  8747. AllmanBraceStyle);
  8748. verifyFormat("#ifdef _DEBUG\n"
  8749. "int foo(int i = 0)\n"
  8750. "#else\n"
  8751. "int foo(int i = 5)\n"
  8752. "#endif\n"
  8753. "{\n"
  8754. " return i;\n"
  8755. "}",
  8756. AllmanBraceStyle);
  8757. verifyFormat("void foo() {}\n"
  8758. "void bar()\n"
  8759. "#ifdef _DEBUG\n"
  8760. "{\n"
  8761. " foo();\n"
  8762. "}\n"
  8763. "#else\n"
  8764. "{\n"
  8765. "}\n"
  8766. "#endif",
  8767. AllmanBraceStyle);
  8768. verifyFormat("void foobar() { int i = 5; }\n"
  8769. "#ifdef _DEBUG\n"
  8770. "void bar() {}\n"
  8771. "#else\n"
  8772. "void bar() { foobar(); }\n"
  8773. "#endif",
  8774. AllmanBraceStyle);
  8775. // This shouldn't affect ObjC blocks..
  8776. verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
  8777. " // ...\n"
  8778. " int i;\n"
  8779. "}];",
  8780. AllmanBraceStyle);
  8781. verifyFormat("void (^block)(void) = ^{\n"
  8782. " // ...\n"
  8783. " int i;\n"
  8784. "};",
  8785. AllmanBraceStyle);
  8786. // .. or dict literals.
  8787. verifyFormat("void f()\n"
  8788. "{\n"
  8789. " [object someMethod:@{ @\"a\" : @\"b\" }];\n"
  8790. "}",
  8791. AllmanBraceStyle);
  8792. verifyFormat("int f()\n"
  8793. "{ // comment\n"
  8794. " return 42;\n"
  8795. "}",
  8796. AllmanBraceStyle);
  8797. AllmanBraceStyle.ColumnLimit = 19;
  8798. verifyFormat("void f() { int i; }", AllmanBraceStyle);
  8799. AllmanBraceStyle.ColumnLimit = 18;
  8800. verifyFormat("void f()\n"
  8801. "{\n"
  8802. " int i;\n"
  8803. "}",
  8804. AllmanBraceStyle);
  8805. AllmanBraceStyle.ColumnLimit = 80;
  8806. FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
  8807. BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
  8808. BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
  8809. verifyFormat("void f(bool b)\n"
  8810. "{\n"
  8811. " if (b)\n"
  8812. " {\n"
  8813. " return;\n"
  8814. " }\n"
  8815. "}\n",
  8816. BreakBeforeBraceShortIfs);
  8817. verifyFormat("void f(bool b)\n"
  8818. "{\n"
  8819. " if (b) return;\n"
  8820. "}\n",
  8821. BreakBeforeBraceShortIfs);
  8822. verifyFormat("void f(bool b)\n"
  8823. "{\n"
  8824. " while (b)\n"
  8825. " {\n"
  8826. " return;\n"
  8827. " }\n"
  8828. "}\n",
  8829. BreakBeforeBraceShortIfs);
  8830. }
  8831. TEST_F(FormatTest, GNUBraceBreaking) {
  8832. FormatStyle GNUBraceStyle = getLLVMStyle();
  8833. GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
  8834. verifyFormat("namespace a\n"
  8835. "{\n"
  8836. "class A\n"
  8837. "{\n"
  8838. " void f()\n"
  8839. " {\n"
  8840. " int a;\n"
  8841. " {\n"
  8842. " int b;\n"
  8843. " }\n"
  8844. " if (true)\n"
  8845. " {\n"
  8846. " a();\n"
  8847. " b();\n"
  8848. " }\n"
  8849. " }\n"
  8850. " void g() { return; }\n"
  8851. "}\n"
  8852. "}",
  8853. GNUBraceStyle);
  8854. verifyFormat("void f()\n"
  8855. "{\n"
  8856. " if (true)\n"
  8857. " {\n"
  8858. " a();\n"
  8859. " }\n"
  8860. " else if (false)\n"
  8861. " {\n"
  8862. " b();\n"
  8863. " }\n"
  8864. " else\n"
  8865. " {\n"
  8866. " c();\n"
  8867. " }\n"
  8868. "}\n",
  8869. GNUBraceStyle);
  8870. verifyFormat("void f()\n"
  8871. "{\n"
  8872. " for (int i = 0; i < 10; ++i)\n"
  8873. " {\n"
  8874. " a();\n"
  8875. " }\n"
  8876. " while (false)\n"
  8877. " {\n"
  8878. " b();\n"
  8879. " }\n"
  8880. " do\n"
  8881. " {\n"
  8882. " c();\n"
  8883. " }\n"
  8884. " while (false);\n"
  8885. "}\n",
  8886. GNUBraceStyle);
  8887. verifyFormat("void f(int a)\n"
  8888. "{\n"
  8889. " switch (a)\n"
  8890. " {\n"
  8891. " case 0:\n"
  8892. " break;\n"
  8893. " case 1:\n"
  8894. " {\n"
  8895. " break;\n"
  8896. " }\n"
  8897. " case 2:\n"
  8898. " {\n"
  8899. " }\n"
  8900. " break;\n"
  8901. " default:\n"
  8902. " break;\n"
  8903. " }\n"
  8904. "}\n",
  8905. GNUBraceStyle);
  8906. verifyFormat("enum X\n"
  8907. "{\n"
  8908. " Y = 0,\n"
  8909. "}\n",
  8910. GNUBraceStyle);
  8911. verifyFormat("@interface BSApplicationController ()\n"
  8912. "{\n"
  8913. "@private\n"
  8914. " id _extraIvar;\n"
  8915. "}\n"
  8916. "@end\n",
  8917. GNUBraceStyle);
  8918. verifyFormat("#ifdef _DEBUG\n"
  8919. "int foo(int i = 0)\n"
  8920. "#else\n"
  8921. "int foo(int i = 5)\n"
  8922. "#endif\n"
  8923. "{\n"
  8924. " return i;\n"
  8925. "}",
  8926. GNUBraceStyle);
  8927. verifyFormat("void foo() {}\n"
  8928. "void bar()\n"
  8929. "#ifdef _DEBUG\n"
  8930. "{\n"
  8931. " foo();\n"
  8932. "}\n"
  8933. "#else\n"
  8934. "{\n"
  8935. "}\n"
  8936. "#endif",
  8937. GNUBraceStyle);
  8938. verifyFormat("void foobar() { int i = 5; }\n"
  8939. "#ifdef _DEBUG\n"
  8940. "void bar() {}\n"
  8941. "#else\n"
  8942. "void bar() { foobar(); }\n"
  8943. "#endif",
  8944. GNUBraceStyle);
  8945. }
  8946. TEST_F(FormatTest, WebKitBraceBreaking) {
  8947. FormatStyle WebKitBraceStyle = getLLVMStyle();
  8948. WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
  8949. verifyFormat("namespace a {\n"
  8950. "class A {\n"
  8951. " void f()\n"
  8952. " {\n"
  8953. " if (true) {\n"
  8954. " a();\n"
  8955. " b();\n"
  8956. " }\n"
  8957. " }\n"
  8958. " void g() { return; }\n"
  8959. "};\n"
  8960. "enum E {\n"
  8961. " A,\n"
  8962. " // foo\n"
  8963. " B,\n"
  8964. " C\n"
  8965. "};\n"
  8966. "struct B {\n"
  8967. " int x;\n"
  8968. "};\n"
  8969. "}\n",
  8970. WebKitBraceStyle);
  8971. verifyFormat("struct S {\n"
  8972. " int Type;\n"
  8973. " union {\n"
  8974. " int x;\n"
  8975. " double y;\n"
  8976. " } Value;\n"
  8977. " class C {\n"
  8978. " MyFavoriteType Value;\n"
  8979. " } Class;\n"
  8980. "};\n",
  8981. WebKitBraceStyle);
  8982. }
  8983. TEST_F(FormatTest, CatchExceptionReferenceBinding) {
  8984. verifyFormat("void f() {\n"
  8985. " try {\n"
  8986. " } catch (const Exception &e) {\n"
  8987. " }\n"
  8988. "}\n",
  8989. getLLVMStyle());
  8990. }
  8991. TEST_F(FormatTest, UnderstandsPragmas) {
  8992. verifyFormat("#pragma omp reduction(| : var)");
  8993. verifyFormat("#pragma omp reduction(+ : var)");
  8994. EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
  8995. "(including parentheses).",
  8996. format("#pragma mark Any non-hyphenated or hyphenated string "
  8997. "(including parentheses)."));
  8998. }
  8999. TEST_F(FormatTest, UnderstandPragmaOption) {
  9000. verifyFormat("#pragma option -C -A");
  9001. EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A"));
  9002. }
  9003. #define EXPECT_ALL_STYLES_EQUAL(Styles) \
  9004. for (size_t i = 1; i < Styles.size(); ++i) \
  9005. EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
  9006. << " differs from Style #0"
  9007. TEST_F(FormatTest, GetsPredefinedStyleByName) {
  9008. SmallVector<FormatStyle, 3> Styles;
  9009. Styles.resize(3);
  9010. Styles[0] = getLLVMStyle();
  9011. EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
  9012. EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
  9013. EXPECT_ALL_STYLES_EQUAL(Styles);
  9014. Styles[0] = getGoogleStyle();
  9015. EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
  9016. EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
  9017. EXPECT_ALL_STYLES_EQUAL(Styles);
  9018. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  9019. EXPECT_TRUE(
  9020. getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
  9021. EXPECT_TRUE(
  9022. getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
  9023. EXPECT_ALL_STYLES_EQUAL(Styles);
  9024. Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
  9025. EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
  9026. EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
  9027. EXPECT_ALL_STYLES_EQUAL(Styles);
  9028. Styles[0] = getMozillaStyle();
  9029. EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
  9030. EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
  9031. EXPECT_ALL_STYLES_EQUAL(Styles);
  9032. Styles[0] = getWebKitStyle();
  9033. EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
  9034. EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
  9035. EXPECT_ALL_STYLES_EQUAL(Styles);
  9036. Styles[0] = getGNUStyle();
  9037. EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
  9038. EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
  9039. EXPECT_ALL_STYLES_EQUAL(Styles);
  9040. EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
  9041. }
  9042. TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
  9043. SmallVector<FormatStyle, 8> Styles;
  9044. Styles.resize(2);
  9045. Styles[0] = getGoogleStyle();
  9046. Styles[1] = getLLVMStyle();
  9047. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  9048. EXPECT_ALL_STYLES_EQUAL(Styles);
  9049. Styles.resize(5);
  9050. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  9051. Styles[1] = getLLVMStyle();
  9052. Styles[1].Language = FormatStyle::LK_JavaScript;
  9053. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  9054. Styles[2] = getLLVMStyle();
  9055. Styles[2].Language = FormatStyle::LK_JavaScript;
  9056. EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
  9057. "BasedOnStyle: Google",
  9058. &Styles[2])
  9059. .value());
  9060. Styles[3] = getLLVMStyle();
  9061. Styles[3].Language = FormatStyle::LK_JavaScript;
  9062. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
  9063. "Language: JavaScript",
  9064. &Styles[3])
  9065. .value());
  9066. Styles[4] = getLLVMStyle();
  9067. Styles[4].Language = FormatStyle::LK_JavaScript;
  9068. EXPECT_EQ(0, parseConfiguration("---\n"
  9069. "BasedOnStyle: LLVM\n"
  9070. "IndentWidth: 123\n"
  9071. "---\n"
  9072. "BasedOnStyle: Google\n"
  9073. "Language: JavaScript",
  9074. &Styles[4])
  9075. .value());
  9076. EXPECT_ALL_STYLES_EQUAL(Styles);
  9077. }
  9078. #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \
  9079. Style.FIELD = false; \
  9080. EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \
  9081. EXPECT_TRUE(Style.FIELD); \
  9082. EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \
  9083. EXPECT_FALSE(Style.FIELD);
  9084. #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
  9085. #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \
  9086. Style.STRUCT.FIELD = false; \
  9087. EXPECT_EQ(0, \
  9088. parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \
  9089. .value()); \
  9090. EXPECT_TRUE(Style.STRUCT.FIELD); \
  9091. EXPECT_EQ(0, \
  9092. parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \
  9093. .value()); \
  9094. EXPECT_FALSE(Style.STRUCT.FIELD);
  9095. #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \
  9096. CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
  9097. #define CHECK_PARSE(TEXT, FIELD, VALUE) \
  9098. EXPECT_NE(VALUE, Style.FIELD); \
  9099. EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \
  9100. EXPECT_EQ(VALUE, Style.FIELD)
  9101. TEST_F(FormatTest, ParsesConfigurationBools) {
  9102. FormatStyle Style = {};
  9103. Style.Language = FormatStyle::LK_Cpp;
  9104. CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
  9105. CHECK_PARSE_BOOL(AlignOperands);
  9106. CHECK_PARSE_BOOL(AlignTrailingComments);
  9107. CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
  9108. CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
  9109. CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
  9110. CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
  9111. CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
  9112. CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
  9113. CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
  9114. CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
  9115. CHECK_PARSE_BOOL(BinPackArguments);
  9116. CHECK_PARSE_BOOL(BinPackParameters);
  9117. CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
  9118. CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
  9119. CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
  9120. CHECK_PARSE_BOOL(BreakStringLiterals);
  9121. CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
  9122. CHECK_PARSE_BOOL(DerivePointerAlignment);
  9123. CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
  9124. CHECK_PARSE_BOOL(DisableFormat);
  9125. CHECK_PARSE_BOOL(IndentCaseLabels);
  9126. CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
  9127. CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
  9128. CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
  9129. CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
  9130. CHECK_PARSE_BOOL(Cpp11BracedListStyle);
  9131. CHECK_PARSE_BOOL(ReflowComments);
  9132. CHECK_PARSE_BOOL(SortIncludes);
  9133. CHECK_PARSE_BOOL(SpacesInParentheses);
  9134. CHECK_PARSE_BOOL(SpacesInSquareBrackets);
  9135. CHECK_PARSE_BOOL(SpacesInAngles);
  9136. CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
  9137. CHECK_PARSE_BOOL(SpacesInContainerLiterals);
  9138. CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
  9139. CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
  9140. CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
  9141. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
  9142. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
  9143. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
  9144. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
  9145. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
  9146. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
  9147. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
  9148. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
  9149. CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
  9150. CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
  9151. CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
  9152. }
  9153. #undef CHECK_PARSE_BOOL
  9154. TEST_F(FormatTest, ParsesConfiguration) {
  9155. FormatStyle Style = {};
  9156. Style.Language = FormatStyle::LK_Cpp;
  9157. CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
  9158. CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
  9159. ConstructorInitializerIndentWidth, 1234u);
  9160. CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
  9161. CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
  9162. CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
  9163. CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
  9164. PenaltyBreakBeforeFirstCallParameter, 1234u);
  9165. CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
  9166. CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
  9167. PenaltyReturnTypeOnItsOwnLine, 1234u);
  9168. CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
  9169. SpacesBeforeTrailingComments, 1234u);
  9170. CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
  9171. CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
  9172. Style.PointerAlignment = FormatStyle::PAS_Middle;
  9173. CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
  9174. FormatStyle::PAS_Left);
  9175. CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
  9176. FormatStyle::PAS_Right);
  9177. CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
  9178. FormatStyle::PAS_Middle);
  9179. // For backward compatibility:
  9180. CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
  9181. FormatStyle::PAS_Left);
  9182. CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
  9183. FormatStyle::PAS_Right);
  9184. CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
  9185. FormatStyle::PAS_Middle);
  9186. Style.Standard = FormatStyle::LS_Auto;
  9187. CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
  9188. CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
  9189. CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
  9190. CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
  9191. CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
  9192. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  9193. CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
  9194. BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
  9195. CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
  9196. FormatStyle::BOS_None);
  9197. CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
  9198. FormatStyle::BOS_All);
  9199. // For backward compatibility:
  9200. CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
  9201. FormatStyle::BOS_None);
  9202. CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
  9203. FormatStyle::BOS_All);
  9204. Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
  9205. CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
  9206. FormatStyle::BAS_Align);
  9207. CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
  9208. FormatStyle::BAS_DontAlign);
  9209. CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
  9210. FormatStyle::BAS_AlwaysBreak);
  9211. // For backward compatibility:
  9212. CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
  9213. FormatStyle::BAS_DontAlign);
  9214. CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
  9215. FormatStyle::BAS_Align);
  9216. Style.UseTab = FormatStyle::UT_ForIndentation;
  9217. CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
  9218. CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
  9219. CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
  9220. // For backward compatibility:
  9221. CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
  9222. CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
  9223. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  9224. CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
  9225. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
  9226. CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
  9227. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
  9228. CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
  9229. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
  9230. CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
  9231. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
  9232. // For backward compatibility:
  9233. CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
  9234. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
  9235. CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
  9236. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
  9237. Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
  9238. CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
  9239. FormatStyle::SBPO_Never);
  9240. CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
  9241. FormatStyle::SBPO_Always);
  9242. CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
  9243. FormatStyle::SBPO_ControlStatements);
  9244. // For backward compatibility:
  9245. CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
  9246. FormatStyle::SBPO_Never);
  9247. CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
  9248. FormatStyle::SBPO_ControlStatements);
  9249. Style.ColumnLimit = 123;
  9250. FormatStyle BaseStyle = getLLVMStyle();
  9251. CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
  9252. CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
  9253. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  9254. CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
  9255. FormatStyle::BS_Attach);
  9256. CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
  9257. FormatStyle::BS_Linux);
  9258. CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
  9259. FormatStyle::BS_Mozilla);
  9260. CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
  9261. FormatStyle::BS_Stroustrup);
  9262. CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
  9263. FormatStyle::BS_Allman);
  9264. CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
  9265. CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
  9266. FormatStyle::BS_WebKit);
  9267. CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
  9268. FormatStyle::BS_Custom);
  9269. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
  9270. CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
  9271. FormatStyle::RTBS_None);
  9272. CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
  9273. FormatStyle::RTBS_All);
  9274. CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
  9275. AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
  9276. CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
  9277. AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
  9278. CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
  9279. AlwaysBreakAfterReturnType,
  9280. FormatStyle::RTBS_TopLevelDefinitions);
  9281. Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
  9282. CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
  9283. AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
  9284. CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
  9285. AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
  9286. CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
  9287. AlwaysBreakAfterDefinitionReturnType,
  9288. FormatStyle::DRTBS_TopLevel);
  9289. Style.NamespaceIndentation = FormatStyle::NI_All;
  9290. CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
  9291. FormatStyle::NI_None);
  9292. CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
  9293. FormatStyle::NI_Inner);
  9294. CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
  9295. FormatStyle::NI_All);
  9296. // FIXME: This is required because parsing a configuration simply overwrites
  9297. // the first N elements of the list instead of resetting it.
  9298. Style.ForEachMacros.clear();
  9299. std::vector<std::string> BoostForeach;
  9300. BoostForeach.push_back("BOOST_FOREACH");
  9301. CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
  9302. std::vector<std::string> BoostAndQForeach;
  9303. BoostAndQForeach.push_back("BOOST_FOREACH");
  9304. BoostAndQForeach.push_back("Q_FOREACH");
  9305. CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
  9306. BoostAndQForeach);
  9307. Style.IncludeCategories.clear();
  9308. std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2},
  9309. {".*", 1}};
  9310. CHECK_PARSE("IncludeCategories:\n"
  9311. " - Regex: abc/.*\n"
  9312. " Priority: 2\n"
  9313. " - Regex: .*\n"
  9314. " Priority: 1",
  9315. IncludeCategories, ExpectedCategories);
  9316. }
  9317. TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
  9318. FormatStyle Style = {};
  9319. Style.Language = FormatStyle::LK_Cpp;
  9320. CHECK_PARSE("Language: Cpp\n"
  9321. "IndentWidth: 12",
  9322. IndentWidth, 12u);
  9323. EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
  9324. "IndentWidth: 34",
  9325. &Style),
  9326. ParseError::Unsuitable);
  9327. EXPECT_EQ(12u, Style.IndentWidth);
  9328. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  9329. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  9330. Style.Language = FormatStyle::LK_JavaScript;
  9331. CHECK_PARSE("Language: JavaScript\n"
  9332. "IndentWidth: 12",
  9333. IndentWidth, 12u);
  9334. CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
  9335. EXPECT_EQ(parseConfiguration("Language: Cpp\n"
  9336. "IndentWidth: 34",
  9337. &Style),
  9338. ParseError::Unsuitable);
  9339. EXPECT_EQ(23u, Style.IndentWidth);
  9340. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  9341. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  9342. CHECK_PARSE("BasedOnStyle: LLVM\n"
  9343. "IndentWidth: 67",
  9344. IndentWidth, 67u);
  9345. CHECK_PARSE("---\n"
  9346. "Language: JavaScript\n"
  9347. "IndentWidth: 12\n"
  9348. "---\n"
  9349. "Language: Cpp\n"
  9350. "IndentWidth: 34\n"
  9351. "...\n",
  9352. IndentWidth, 12u);
  9353. Style.Language = FormatStyle::LK_Cpp;
  9354. CHECK_PARSE("---\n"
  9355. "Language: JavaScript\n"
  9356. "IndentWidth: 12\n"
  9357. "---\n"
  9358. "Language: Cpp\n"
  9359. "IndentWidth: 34\n"
  9360. "...\n",
  9361. IndentWidth, 34u);
  9362. CHECK_PARSE("---\n"
  9363. "IndentWidth: 78\n"
  9364. "---\n"
  9365. "Language: JavaScript\n"
  9366. "IndentWidth: 56\n"
  9367. "...\n",
  9368. IndentWidth, 78u);
  9369. Style.ColumnLimit = 123;
  9370. Style.IndentWidth = 234;
  9371. Style.BreakBeforeBraces = FormatStyle::BS_Linux;
  9372. Style.TabWidth = 345;
  9373. EXPECT_FALSE(parseConfiguration("---\n"
  9374. "IndentWidth: 456\n"
  9375. "BreakBeforeBraces: Allman\n"
  9376. "---\n"
  9377. "Language: JavaScript\n"
  9378. "IndentWidth: 111\n"
  9379. "TabWidth: 111\n"
  9380. "---\n"
  9381. "Language: Cpp\n"
  9382. "BreakBeforeBraces: Stroustrup\n"
  9383. "TabWidth: 789\n"
  9384. "...\n",
  9385. &Style));
  9386. EXPECT_EQ(123u, Style.ColumnLimit);
  9387. EXPECT_EQ(456u, Style.IndentWidth);
  9388. EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
  9389. EXPECT_EQ(789u, Style.TabWidth);
  9390. EXPECT_EQ(parseConfiguration("---\n"
  9391. "Language: JavaScript\n"
  9392. "IndentWidth: 56\n"
  9393. "---\n"
  9394. "IndentWidth: 78\n"
  9395. "...\n",
  9396. &Style),
  9397. ParseError::Error);
  9398. EXPECT_EQ(parseConfiguration("---\n"
  9399. "Language: JavaScript\n"
  9400. "IndentWidth: 56\n"
  9401. "---\n"
  9402. "Language: JavaScript\n"
  9403. "IndentWidth: 78\n"
  9404. "...\n",
  9405. &Style),
  9406. ParseError::Error);
  9407. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  9408. }
  9409. #undef CHECK_PARSE
  9410. TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
  9411. FormatStyle Style = {};
  9412. Style.Language = FormatStyle::LK_JavaScript;
  9413. Style.BreakBeforeTernaryOperators = true;
  9414. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
  9415. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  9416. Style.BreakBeforeTernaryOperators = true;
  9417. EXPECT_EQ(0, parseConfiguration("---\n"
  9418. "BasedOnStyle: Google\n"
  9419. "---\n"
  9420. "Language: JavaScript\n"
  9421. "IndentWidth: 76\n"
  9422. "...\n",
  9423. &Style)
  9424. .value());
  9425. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  9426. EXPECT_EQ(76u, Style.IndentWidth);
  9427. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  9428. }
  9429. TEST_F(FormatTest, ConfigurationRoundTripTest) {
  9430. FormatStyle Style = getLLVMStyle();
  9431. std::string YAML = configurationAsText(Style);
  9432. FormatStyle ParsedStyle = {};
  9433. ParsedStyle.Language = FormatStyle::LK_Cpp;
  9434. EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
  9435. EXPECT_EQ(Style, ParsedStyle);
  9436. }
  9437. TEST_F(FormatTest, WorksFor8bitEncodings) {
  9438. EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
  9439. "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
  9440. "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
  9441. "\"\xef\xee\xf0\xf3...\"",
  9442. format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
  9443. "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
  9444. "\xef\xee\xf0\xf3...\"",
  9445. getLLVMStyleWithColumns(12)));
  9446. }
  9447. TEST_F(FormatTest, HandlesUTF8BOM) {
  9448. EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
  9449. EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
  9450. format("\xef\xbb\xbf#include <iostream>"));
  9451. EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
  9452. format("\xef\xbb\xbf\n#include <iostream>"));
  9453. }
  9454. // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
  9455. #if !defined(_MSC_VER)
  9456. TEST_F(FormatTest, CountsUTF8CharactersProperly) {
  9457. verifyFormat("\"Однажды в студёную зимнюю пору...\"",
  9458. getLLVMStyleWithColumns(35));
  9459. verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
  9460. getLLVMStyleWithColumns(31));
  9461. verifyFormat("// Однажды в студёную зимнюю пору...",
  9462. getLLVMStyleWithColumns(36));
  9463. verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
  9464. verifyFormat("/* Однажды в студёную зимнюю пору... */",
  9465. getLLVMStyleWithColumns(39));
  9466. verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
  9467. getLLVMStyleWithColumns(35));
  9468. }
  9469. TEST_F(FormatTest, SplitsUTF8Strings) {
  9470. // Non-printable characters' width is currently considered to be the length in
  9471. // bytes in UTF8. The characters can be displayed in very different manner
  9472. // (zero-width, single width with a substitution glyph, expanded to their code
  9473. // (e.g. "<8d>"), so there's no single correct way to handle them.
  9474. EXPECT_EQ("\"aaaaÄ\"\n"
  9475. "\"\xc2\x8d\";",
  9476. format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  9477. EXPECT_EQ("\"aaaaaaaÄ\"\n"
  9478. "\"\xc2\x8d\";",
  9479. format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  9480. EXPECT_EQ("\"Однажды, в \"\n"
  9481. "\"студёную \"\n"
  9482. "\"зимнюю \"\n"
  9483. "\"пору,\"",
  9484. format("\"Однажды, в студёную зимнюю пору,\"",
  9485. getLLVMStyleWithColumns(13)));
  9486. EXPECT_EQ(
  9487. "\"一 二 三 \"\n"
  9488. "\"四 五六 \"\n"
  9489. "\"七 八 九 \"\n"
  9490. "\"十\"",
  9491. format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
  9492. EXPECT_EQ("\"一\t二 \"\n"
  9493. "\"\t三 \"\n"
  9494. "\"四 五\t六 \"\n"
  9495. "\"\t七 \"\n"
  9496. "\"八九十\tqq\"",
  9497. format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
  9498. getLLVMStyleWithColumns(11)));
  9499. // UTF8 character in an escape sequence.
  9500. EXPECT_EQ("\"aaaaaa\"\n"
  9501. "\"\\\xC2\x8D\"",
  9502. format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
  9503. }
  9504. TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
  9505. EXPECT_EQ("const char *sssss =\n"
  9506. " \"一二三四五六七八\\\n"
  9507. " 九 十\";",
  9508. format("const char *sssss = \"一二三四五六七八\\\n"
  9509. " 九 十\";",
  9510. getLLVMStyleWithColumns(30)));
  9511. }
  9512. TEST_F(FormatTest, SplitsUTF8LineComments) {
  9513. EXPECT_EQ("// aaaaÄ\xc2\x8d",
  9514. format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
  9515. EXPECT_EQ("// Я из лесу\n"
  9516. "// вышел; был\n"
  9517. "// сильный\n"
  9518. "// мороз.",
  9519. format("// Я из лесу вышел; был сильный мороз.",
  9520. getLLVMStyleWithColumns(13)));
  9521. EXPECT_EQ("// 一二三\n"
  9522. "// 四五六七\n"
  9523. "// 八 九\n"
  9524. "// 十",
  9525. format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9)));
  9526. }
  9527. TEST_F(FormatTest, SplitsUTF8BlockComments) {
  9528. EXPECT_EQ("/* Гляжу,\n"
  9529. " * поднимается\n"
  9530. " * медленно в\n"
  9531. " * гору\n"
  9532. " * Лошадка,\n"
  9533. " * везущая\n"
  9534. " * хворосту\n"
  9535. " * воз. */",
  9536. format("/* Гляжу, поднимается медленно в гору\n"
  9537. " * Лошадка, везущая хворосту воз. */",
  9538. getLLVMStyleWithColumns(13)));
  9539. EXPECT_EQ(
  9540. "/* 一二三\n"
  9541. " * 四五六七\n"
  9542. " * 八 九\n"
  9543. " * 十 */",
  9544. format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9)));
  9545. EXPECT_EQ("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
  9546. " * 𝕓𝕪𝕥𝕖\n"
  9547. " * 𝖀𝕿𝕱-𝟠 */",
  9548. format("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12)));
  9549. }
  9550. #endif // _MSC_VER
  9551. TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
  9552. FormatStyle Style = getLLVMStyle();
  9553. Style.ConstructorInitializerIndentWidth = 4;
  9554. verifyFormat(
  9555. "SomeClass::Constructor()\n"
  9556. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  9557. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  9558. Style);
  9559. Style.ConstructorInitializerIndentWidth = 2;
  9560. verifyFormat(
  9561. "SomeClass::Constructor()\n"
  9562. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  9563. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  9564. Style);
  9565. Style.ConstructorInitializerIndentWidth = 0;
  9566. verifyFormat(
  9567. "SomeClass::Constructor()\n"
  9568. ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  9569. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  9570. Style);
  9571. Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
  9572. verifyFormat(
  9573. "SomeLongTemplateVariableName<\n"
  9574. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
  9575. Style);
  9576. verifyFormat(
  9577. "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  9578. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  9579. Style);
  9580. }
  9581. TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
  9582. FormatStyle Style = getLLVMStyle();
  9583. Style.BreakConstructorInitializersBeforeComma = true;
  9584. Style.ConstructorInitializerIndentWidth = 4;
  9585. verifyFormat("SomeClass::Constructor()\n"
  9586. " : a(a)\n"
  9587. " , b(b)\n"
  9588. " , c(c) {}",
  9589. Style);
  9590. verifyFormat("SomeClass::Constructor()\n"
  9591. " : a(a) {}",
  9592. Style);
  9593. Style.ColumnLimit = 0;
  9594. verifyFormat("SomeClass::Constructor()\n"
  9595. " : a(a) {}",
  9596. Style);
  9597. verifyFormat("SomeClass::Constructor()\n"
  9598. " : a(a)\n"
  9599. " , b(b)\n"
  9600. " , c(c) {}",
  9601. Style);
  9602. verifyFormat("SomeClass::Constructor()\n"
  9603. " : a(a) {\n"
  9604. " foo();\n"
  9605. " bar();\n"
  9606. "}",
  9607. Style);
  9608. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  9609. verifyFormat("SomeClass::Constructor()\n"
  9610. " : a(a)\n"
  9611. " , b(b)\n"
  9612. " , c(c) {\n}",
  9613. Style);
  9614. verifyFormat("SomeClass::Constructor()\n"
  9615. " : a(a) {\n}",
  9616. Style);
  9617. Style.ColumnLimit = 80;
  9618. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
  9619. Style.ConstructorInitializerIndentWidth = 2;
  9620. verifyFormat("SomeClass::Constructor()\n"
  9621. " : a(a)\n"
  9622. " , b(b)\n"
  9623. " , c(c) {}",
  9624. Style);
  9625. Style.ConstructorInitializerIndentWidth = 0;
  9626. verifyFormat("SomeClass::Constructor()\n"
  9627. ": a(a)\n"
  9628. ", b(b)\n"
  9629. ", c(c) {}",
  9630. Style);
  9631. Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  9632. Style.ConstructorInitializerIndentWidth = 4;
  9633. verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
  9634. verifyFormat(
  9635. "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
  9636. Style);
  9637. verifyFormat(
  9638. "SomeClass::Constructor()\n"
  9639. " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
  9640. Style);
  9641. Style.ConstructorInitializerIndentWidth = 4;
  9642. Style.ColumnLimit = 60;
  9643. verifyFormat("SomeClass::Constructor()\n"
  9644. " : aaaaaaaa(aaaaaaaa)\n"
  9645. " , aaaaaaaa(aaaaaaaa)\n"
  9646. " , aaaaaaaa(aaaaaaaa) {}",
  9647. Style);
  9648. }
  9649. TEST_F(FormatTest, Destructors) {
  9650. verifyFormat("void F(int &i) { i.~int(); }");
  9651. verifyFormat("void F(int &i) { i->~int(); }");
  9652. }
  9653. TEST_F(FormatTest, FormatsWithWebKitStyle) {
  9654. FormatStyle Style = getWebKitStyle();
  9655. // Don't indent in outer namespaces.
  9656. verifyFormat("namespace outer {\n"
  9657. "int i;\n"
  9658. "namespace inner {\n"
  9659. " int i;\n"
  9660. "} // namespace inner\n"
  9661. "} // namespace outer\n"
  9662. "namespace other_outer {\n"
  9663. "int i;\n"
  9664. "}",
  9665. Style);
  9666. // Don't indent case labels.
  9667. verifyFormat("switch (variable) {\n"
  9668. "case 1:\n"
  9669. "case 2:\n"
  9670. " doSomething();\n"
  9671. " break;\n"
  9672. "default:\n"
  9673. " ++variable;\n"
  9674. "}",
  9675. Style);
  9676. // Wrap before binary operators.
  9677. EXPECT_EQ("void f()\n"
  9678. "{\n"
  9679. " if (aaaaaaaaaaaaaaaa\n"
  9680. " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
  9681. " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  9682. " return;\n"
  9683. "}",
  9684. format("void f() {\n"
  9685. "if (aaaaaaaaaaaaaaaa\n"
  9686. "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
  9687. "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  9688. "return;\n"
  9689. "}",
  9690. Style));
  9691. // Allow functions on a single line.
  9692. verifyFormat("void f() { return; }", Style);
  9693. // Constructor initializers are formatted one per line with the "," on the
  9694. // new line.
  9695. verifyFormat("Constructor()\n"
  9696. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  9697. " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
  9698. " aaaaaaaaaaaaaa)\n"
  9699. " , aaaaaaaaaaaaaaaaaaaaaaa()\n"
  9700. "{\n"
  9701. "}",
  9702. Style);
  9703. verifyFormat("SomeClass::Constructor()\n"
  9704. " : a(a)\n"
  9705. "{\n"
  9706. "}",
  9707. Style);
  9708. EXPECT_EQ("SomeClass::Constructor()\n"
  9709. " : a(a)\n"
  9710. "{\n"
  9711. "}",
  9712. format("SomeClass::Constructor():a(a){}", Style));
  9713. verifyFormat("SomeClass::Constructor()\n"
  9714. " : a(a)\n"
  9715. " , b(b)\n"
  9716. " , c(c)\n"
  9717. "{\n"
  9718. "}",
  9719. Style);
  9720. verifyFormat("SomeClass::Constructor()\n"
  9721. " : a(a)\n"
  9722. "{\n"
  9723. " foo();\n"
  9724. " bar();\n"
  9725. "}",
  9726. Style);
  9727. // Access specifiers should be aligned left.
  9728. verifyFormat("class C {\n"
  9729. "public:\n"
  9730. " int i;\n"
  9731. "};",
  9732. Style);
  9733. // Do not align comments.
  9734. verifyFormat("int a; // Do not\n"
  9735. "double b; // align comments.",
  9736. Style);
  9737. // Do not align operands.
  9738. EXPECT_EQ("ASSERT(aaaa\n"
  9739. " || bbbb);",
  9740. format("ASSERT ( aaaa\n||bbbb);", Style));
  9741. // Accept input's line breaks.
  9742. EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
  9743. " || bbbbbbbbbbbbbbb) {\n"
  9744. " i++;\n"
  9745. "}",
  9746. format("if (aaaaaaaaaaaaaaa\n"
  9747. "|| bbbbbbbbbbbbbbb) { i++; }",
  9748. Style));
  9749. EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
  9750. " i++;\n"
  9751. "}",
  9752. format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
  9753. // Don't automatically break all macro definitions (llvm.org/PR17842).
  9754. verifyFormat("#define aNumber 10", Style);
  9755. // However, generally keep the line breaks that the user authored.
  9756. EXPECT_EQ("#define aNumber \\\n"
  9757. " 10",
  9758. format("#define aNumber \\\n"
  9759. " 10",
  9760. Style));
  9761. // Keep empty and one-element array literals on a single line.
  9762. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
  9763. " copyItems:YES];",
  9764. format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
  9765. "copyItems:YES];",
  9766. Style));
  9767. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
  9768. " copyItems:YES];",
  9769. format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
  9770. " copyItems:YES];",
  9771. Style));
  9772. // FIXME: This does not seem right, there should be more indentation before
  9773. // the array literal's entries. Nested blocks have the same problem.
  9774. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
  9775. " @\"a\",\n"
  9776. " @\"a\"\n"
  9777. "]\n"
  9778. " copyItems:YES];",
  9779. format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
  9780. " @\"a\",\n"
  9781. " @\"a\"\n"
  9782. " ]\n"
  9783. " copyItems:YES];",
  9784. Style));
  9785. EXPECT_EQ(
  9786. "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
  9787. " copyItems:YES];",
  9788. format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
  9789. " copyItems:YES];",
  9790. Style));
  9791. verifyFormat("[self.a b:c c:d];", Style);
  9792. EXPECT_EQ("[self.a b:c\n"
  9793. " c:d];",
  9794. format("[self.a b:c\n"
  9795. "c:d];",
  9796. Style));
  9797. }
  9798. TEST_F(FormatTest, FormatsLambdas) {
  9799. verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
  9800. verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
  9801. verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
  9802. verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
  9803. verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
  9804. verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
  9805. verifyFormat("void f() {\n"
  9806. " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
  9807. "}\n");
  9808. verifyFormat("void f() {\n"
  9809. " other(x.begin(), //\n"
  9810. " x.end(), //\n"
  9811. " [&](int, int) { return 1; });\n"
  9812. "}\n");
  9813. verifyFormat("SomeFunction([]() { // A cool function...\n"
  9814. " return 43;\n"
  9815. "});");
  9816. EXPECT_EQ("SomeFunction([]() {\n"
  9817. "#define A a\n"
  9818. " return 43;\n"
  9819. "});",
  9820. format("SomeFunction([](){\n"
  9821. "#define A a\n"
  9822. "return 43;\n"
  9823. "});"));
  9824. verifyFormat("void f() {\n"
  9825. " SomeFunction([](decltype(x), A *a) {});\n"
  9826. "}");
  9827. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  9828. " [](const aaaaaaaaaa &a) { return a; });");
  9829. verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
  9830. " SomeOtherFunctioooooooooooooooooooooooooon();\n"
  9831. "});");
  9832. verifyFormat("Constructor()\n"
  9833. " : Field([] { // comment\n"
  9834. " int i;\n"
  9835. " }) {}");
  9836. verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
  9837. " return some_parameter.size();\n"
  9838. "};");
  9839. verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
  9840. " [](const string &s) { return s; };");
  9841. verifyFormat("int i = aaaaaa ? 1 //\n"
  9842. " : [] {\n"
  9843. " return 2; //\n"
  9844. " }();");
  9845. verifyFormat("llvm::errs() << \"number of twos is \"\n"
  9846. " << std::count_if(v.begin(), v.end(), [](int x) {\n"
  9847. " return x == 2; // force break\n"
  9848. " });");
  9849. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n"
  9850. " int iiiiiiiiiiii) {\n"
  9851. " return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n"
  9852. "});",
  9853. getLLVMStyleWithColumns(60));
  9854. verifyFormat("SomeFunction({[&] {\n"
  9855. " // comment\n"
  9856. " },\n"
  9857. " [&] {\n"
  9858. " // comment\n"
  9859. " }});");
  9860. verifyFormat("SomeFunction({[&] {\n"
  9861. " // comment\n"
  9862. "}});");
  9863. verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n"
  9864. " [&]() { return true; },\n"
  9865. " aaaaa aaaaaaaaa);");
  9866. // Lambdas with return types.
  9867. verifyFormat("int c = []() -> int { return 2; }();\n");
  9868. verifyFormat("int c = []() -> int * { return 2; }();\n");
  9869. verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
  9870. verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
  9871. verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
  9872. verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
  9873. verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
  9874. verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
  9875. verifyFormat("[a, a]() -> a<1> {};");
  9876. verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
  9877. " int j) -> int {\n"
  9878. " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
  9879. "};");
  9880. verifyFormat(
  9881. "aaaaaaaaaaaaaaaaaaaaaa(\n"
  9882. " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
  9883. " return aaaaaaaaaaaaaaaaa;\n"
  9884. " });",
  9885. getLLVMStyleWithColumns(70));
  9886. // Multiple lambdas in the same parentheses change indentation rules.
  9887. verifyFormat("SomeFunction(\n"
  9888. " []() {\n"
  9889. " int i = 42;\n"
  9890. " return i;\n"
  9891. " },\n"
  9892. " []() {\n"
  9893. " int j = 43;\n"
  9894. " return j;\n"
  9895. " });");
  9896. // More complex introducers.
  9897. verifyFormat("return [i, args...] {};");
  9898. // Not lambdas.
  9899. verifyFormat("constexpr char hello[]{\"hello\"};");
  9900. verifyFormat("double &operator[](int i) { return 0; }\n"
  9901. "int i;");
  9902. verifyFormat("std::unique_ptr<int[]> foo() {}");
  9903. verifyFormat("int i = a[a][a]->f();");
  9904. verifyFormat("int i = (*b)[a]->f();");
  9905. // Other corner cases.
  9906. verifyFormat("void f() {\n"
  9907. " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
  9908. " );\n"
  9909. "}");
  9910. // Lambdas created through weird macros.
  9911. verifyFormat("void f() {\n"
  9912. " MACRO((const AA &a) { return 1; });\n"
  9913. " MACRO((AA &a) { return 1; });\n"
  9914. "}");
  9915. verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
  9916. " doo_dah();\n"
  9917. " doo_dah();\n"
  9918. " })) {\n"
  9919. "}");
  9920. verifyFormat("auto lambda = []() {\n"
  9921. " int a = 2\n"
  9922. "#if A\n"
  9923. " + 2\n"
  9924. "#endif\n"
  9925. " ;\n"
  9926. "};");
  9927. }
  9928. TEST_F(FormatTest, FormatsBlocks) {
  9929. FormatStyle ShortBlocks = getLLVMStyle();
  9930. ShortBlocks.AllowShortBlocksOnASingleLine = true;
  9931. verifyFormat("int (^Block)(int, int);", ShortBlocks);
  9932. verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
  9933. verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
  9934. verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
  9935. verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
  9936. verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
  9937. verifyFormat("foo(^{ bar(); });", ShortBlocks);
  9938. verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
  9939. verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
  9940. verifyFormat("[operation setCompletionBlock:^{\n"
  9941. " [self onOperationDone];\n"
  9942. "}];");
  9943. verifyFormat("int i = {[operation setCompletionBlock:^{\n"
  9944. " [self onOperationDone];\n"
  9945. "}]};");
  9946. verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
  9947. " f();\n"
  9948. "}];");
  9949. verifyFormat("int a = [operation block:^int(int *i) {\n"
  9950. " return 1;\n"
  9951. "}];");
  9952. verifyFormat("[myObject doSomethingWith:arg1\n"
  9953. " aaa:^int(int *a) {\n"
  9954. " return 1;\n"
  9955. " }\n"
  9956. " bbb:f(a * bbbbbbbb)];");
  9957. verifyFormat("[operation setCompletionBlock:^{\n"
  9958. " [self.delegate newDataAvailable];\n"
  9959. "}];",
  9960. getLLVMStyleWithColumns(60));
  9961. verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
  9962. " NSString *path = [self sessionFilePath];\n"
  9963. " if (path) {\n"
  9964. " // ...\n"
  9965. " }\n"
  9966. "});");
  9967. verifyFormat("[[SessionService sharedService]\n"
  9968. " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  9969. " if (window) {\n"
  9970. " [self windowDidLoad:window];\n"
  9971. " } else {\n"
  9972. " [self errorLoadingWindow];\n"
  9973. " }\n"
  9974. " }];");
  9975. verifyFormat("void (^largeBlock)(void) = ^{\n"
  9976. " // ...\n"
  9977. "};\n",
  9978. getLLVMStyleWithColumns(40));
  9979. verifyFormat("[[SessionService sharedService]\n"
  9980. " loadWindowWithCompletionBlock: //\n"
  9981. " ^(SessionWindow *window) {\n"
  9982. " if (window) {\n"
  9983. " [self windowDidLoad:window];\n"
  9984. " } else {\n"
  9985. " [self errorLoadingWindow];\n"
  9986. " }\n"
  9987. " }];",
  9988. getLLVMStyleWithColumns(60));
  9989. verifyFormat("[myObject doSomethingWith:arg1\n"
  9990. " firstBlock:^(Foo *a) {\n"
  9991. " // ...\n"
  9992. " int i;\n"
  9993. " }\n"
  9994. " secondBlock:^(Bar *b) {\n"
  9995. " // ...\n"
  9996. " int i;\n"
  9997. " }\n"
  9998. " thirdBlock:^Foo(Bar *b) {\n"
  9999. " // ...\n"
  10000. " int i;\n"
  10001. " }];");
  10002. verifyFormat("[myObject doSomethingWith:arg1\n"
  10003. " firstBlock:-1\n"
  10004. " secondBlock:^(Bar *b) {\n"
  10005. " // ...\n"
  10006. " int i;\n"
  10007. " }];");
  10008. verifyFormat("f(^{\n"
  10009. " @autoreleasepool {\n"
  10010. " if (a) {\n"
  10011. " g();\n"
  10012. " }\n"
  10013. " }\n"
  10014. "});");
  10015. verifyFormat("Block b = ^int *(A *a, B *b) {}");
  10016. FormatStyle FourIndent = getLLVMStyle();
  10017. FourIndent.ObjCBlockIndentWidth = 4;
  10018. verifyFormat("[operation setCompletionBlock:^{\n"
  10019. " [self onOperationDone];\n"
  10020. "}];",
  10021. FourIndent);
  10022. }
  10023. TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
  10024. FormatStyle ZeroColumn = getLLVMStyle();
  10025. ZeroColumn.ColumnLimit = 0;
  10026. verifyFormat("[[SessionService sharedService] "
  10027. "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  10028. " if (window) {\n"
  10029. " [self windowDidLoad:window];\n"
  10030. " } else {\n"
  10031. " [self errorLoadingWindow];\n"
  10032. " }\n"
  10033. "}];",
  10034. ZeroColumn);
  10035. EXPECT_EQ("[[SessionService sharedService]\n"
  10036. " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  10037. " if (window) {\n"
  10038. " [self windowDidLoad:window];\n"
  10039. " } else {\n"
  10040. " [self errorLoadingWindow];\n"
  10041. " }\n"
  10042. " }];",
  10043. format("[[SessionService sharedService]\n"
  10044. "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  10045. " if (window) {\n"
  10046. " [self windowDidLoad:window];\n"
  10047. " } else {\n"
  10048. " [self errorLoadingWindow];\n"
  10049. " }\n"
  10050. "}];",
  10051. ZeroColumn));
  10052. verifyFormat("[myObject doSomethingWith:arg1\n"
  10053. " firstBlock:^(Foo *a) {\n"
  10054. " // ...\n"
  10055. " int i;\n"
  10056. " }\n"
  10057. " secondBlock:^(Bar *b) {\n"
  10058. " // ...\n"
  10059. " int i;\n"
  10060. " }\n"
  10061. " thirdBlock:^Foo(Bar *b) {\n"
  10062. " // ...\n"
  10063. " int i;\n"
  10064. " }];",
  10065. ZeroColumn);
  10066. verifyFormat("f(^{\n"
  10067. " @autoreleasepool {\n"
  10068. " if (a) {\n"
  10069. " g();\n"
  10070. " }\n"
  10071. " }\n"
  10072. "});",
  10073. ZeroColumn);
  10074. verifyFormat("void (^largeBlock)(void) = ^{\n"
  10075. " // ...\n"
  10076. "};",
  10077. ZeroColumn);
  10078. ZeroColumn.AllowShortBlocksOnASingleLine = true;
  10079. EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
  10080. format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn));
  10081. ZeroColumn.AllowShortBlocksOnASingleLine = false;
  10082. EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
  10083. " int i;\n"
  10084. "};",
  10085. format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn));
  10086. }
  10087. TEST_F(FormatTest, SupportsCRLF) {
  10088. EXPECT_EQ("int a;\r\n"
  10089. "int b;\r\n"
  10090. "int c;\r\n",
  10091. format("int a;\r\n"
  10092. " int b;\r\n"
  10093. " int c;\r\n",
  10094. getLLVMStyle()));
  10095. EXPECT_EQ("int a;\r\n"
  10096. "int b;\r\n"
  10097. "int c;\r\n",
  10098. format("int a;\r\n"
  10099. " int b;\n"
  10100. " int c;\r\n",
  10101. getLLVMStyle()));
  10102. EXPECT_EQ("int a;\n"
  10103. "int b;\n"
  10104. "int c;\n",
  10105. format("int a;\r\n"
  10106. " int b;\n"
  10107. " int c;\n",
  10108. getLLVMStyle()));
  10109. EXPECT_EQ("\"aaaaaaa \"\r\n"
  10110. "\"bbbbbbb\";\r\n",
  10111. format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
  10112. EXPECT_EQ("#define A \\\r\n"
  10113. " b; \\\r\n"
  10114. " c; \\\r\n"
  10115. " d;\r\n",
  10116. format("#define A \\\r\n"
  10117. " b; \\\r\n"
  10118. " c; d; \r\n",
  10119. getGoogleStyle()));
  10120. EXPECT_EQ("/*\r\n"
  10121. "multi line block comments\r\n"
  10122. "should not introduce\r\n"
  10123. "an extra carriage return\r\n"
  10124. "*/\r\n",
  10125. format("/*\r\n"
  10126. "multi line block comments\r\n"
  10127. "should not introduce\r\n"
  10128. "an extra carriage return\r\n"
  10129. "*/\r\n"));
  10130. }
  10131. TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
  10132. verifyFormat("MY_CLASS(C) {\n"
  10133. " int i;\n"
  10134. " int j;\n"
  10135. "};");
  10136. }
  10137. TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
  10138. FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
  10139. TwoIndent.ContinuationIndentWidth = 2;
  10140. EXPECT_EQ("int i =\n"
  10141. " longFunction(\n"
  10142. " arg);",
  10143. format("int i = longFunction(arg);", TwoIndent));
  10144. FormatStyle SixIndent = getLLVMStyleWithColumns(20);
  10145. SixIndent.ContinuationIndentWidth = 6;
  10146. EXPECT_EQ("int i =\n"
  10147. " longFunction(\n"
  10148. " arg);",
  10149. format("int i = longFunction(arg);", SixIndent));
  10150. }
  10151. TEST_F(FormatTest, SpacesInAngles) {
  10152. FormatStyle Spaces = getLLVMStyle();
  10153. Spaces.SpacesInAngles = true;
  10154. verifyFormat("static_cast< int >(arg);", Spaces);
  10155. verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
  10156. verifyFormat("f< int, float >();", Spaces);
  10157. verifyFormat("template <> g() {}", Spaces);
  10158. verifyFormat("template < std::vector< int > > f() {}", Spaces);
  10159. verifyFormat("std::function< void(int, int) > fct;", Spaces);
  10160. verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
  10161. Spaces);
  10162. Spaces.Standard = FormatStyle::LS_Cpp03;
  10163. Spaces.SpacesInAngles = true;
  10164. verifyFormat("A< A< int > >();", Spaces);
  10165. Spaces.SpacesInAngles = false;
  10166. verifyFormat("A<A<int> >();", Spaces);
  10167. Spaces.Standard = FormatStyle::LS_Cpp11;
  10168. Spaces.SpacesInAngles = true;
  10169. verifyFormat("A< A< int > >();", Spaces);
  10170. Spaces.SpacesInAngles = false;
  10171. verifyFormat("A<A<int>>();", Spaces);
  10172. }
  10173. TEST_F(FormatTest, TripleAngleBrackets) {
  10174. verifyFormat("f<<<1, 1>>>();");
  10175. verifyFormat("f<<<1, 1, 1, s>>>();");
  10176. verifyFormat("f<<<a, b, c, d>>>();");
  10177. EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
  10178. verifyFormat("f<param><<<1, 1>>>();");
  10179. verifyFormat("f<1><<<1, 1>>>();");
  10180. EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
  10181. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  10182. "aaaaaaaaaaa<<<\n 1, 1>>>();");
  10183. }
  10184. TEST_F(FormatTest, MergeLessLessAtEnd) {
  10185. verifyFormat("<<");
  10186. EXPECT_EQ("< < <", format("\\\n<<<"));
  10187. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  10188. "aaallvm::outs() <<");
  10189. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  10190. "aaaallvm::outs()\n <<");
  10191. }
  10192. TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
  10193. std::string code = "#if A\n"
  10194. "#if B\n"
  10195. "a.\n"
  10196. "#endif\n"
  10197. " a = 1;\n"
  10198. "#else\n"
  10199. "#endif\n"
  10200. "#if C\n"
  10201. "#else\n"
  10202. "#endif\n";
  10203. EXPECT_EQ(code, format(code));
  10204. }
  10205. TEST_F(FormatTest, HandleConflictMarkers) {
  10206. // Git/SVN conflict markers.
  10207. EXPECT_EQ("int a;\n"
  10208. "void f() {\n"
  10209. " callme(some(parameter1,\n"
  10210. "<<<<<<< text by the vcs\n"
  10211. " parameter2),\n"
  10212. "||||||| text by the vcs\n"
  10213. " parameter2),\n"
  10214. " parameter3,\n"
  10215. "======= text by the vcs\n"
  10216. " parameter2, parameter3),\n"
  10217. ">>>>>>> text by the vcs\n"
  10218. " otherparameter);\n",
  10219. format("int a;\n"
  10220. "void f() {\n"
  10221. " callme(some(parameter1,\n"
  10222. "<<<<<<< text by the vcs\n"
  10223. " parameter2),\n"
  10224. "||||||| text by the vcs\n"
  10225. " parameter2),\n"
  10226. " parameter3,\n"
  10227. "======= text by the vcs\n"
  10228. " parameter2,\n"
  10229. " parameter3),\n"
  10230. ">>>>>>> text by the vcs\n"
  10231. " otherparameter);\n"));
  10232. // Perforce markers.
  10233. EXPECT_EQ("void f() {\n"
  10234. " function(\n"
  10235. ">>>> text by the vcs\n"
  10236. " parameter,\n"
  10237. "==== text by the vcs\n"
  10238. " parameter,\n"
  10239. "==== text by the vcs\n"
  10240. " parameter,\n"
  10241. "<<<< text by the vcs\n"
  10242. " parameter);\n",
  10243. format("void f() {\n"
  10244. " function(\n"
  10245. ">>>> text by the vcs\n"
  10246. " parameter,\n"
  10247. "==== text by the vcs\n"
  10248. " parameter,\n"
  10249. "==== text by the vcs\n"
  10250. " parameter,\n"
  10251. "<<<< text by the vcs\n"
  10252. " parameter);\n"));
  10253. EXPECT_EQ("<<<<<<<\n"
  10254. "|||||||\n"
  10255. "=======\n"
  10256. ">>>>>>>",
  10257. format("<<<<<<<\n"
  10258. "|||||||\n"
  10259. "=======\n"
  10260. ">>>>>>>"));
  10261. EXPECT_EQ("<<<<<<<\n"
  10262. "|||||||\n"
  10263. "int i;\n"
  10264. "=======\n"
  10265. ">>>>>>>",
  10266. format("<<<<<<<\n"
  10267. "|||||||\n"
  10268. "int i;\n"
  10269. "=======\n"
  10270. ">>>>>>>"));
  10271. // FIXME: Handle parsing of macros around conflict markers correctly:
  10272. EXPECT_EQ("#define Macro \\\n"
  10273. "<<<<<<<\n"
  10274. "Something \\\n"
  10275. "|||||||\n"
  10276. "Else \\\n"
  10277. "=======\n"
  10278. "Other \\\n"
  10279. ">>>>>>>\n"
  10280. " End int i;\n",
  10281. format("#define Macro \\\n"
  10282. "<<<<<<<\n"
  10283. " Something \\\n"
  10284. "|||||||\n"
  10285. " Else \\\n"
  10286. "=======\n"
  10287. " Other \\\n"
  10288. ">>>>>>>\n"
  10289. " End\n"
  10290. "int i;\n"));
  10291. }
  10292. TEST_F(FormatTest, DisableRegions) {
  10293. EXPECT_EQ("int i;\n"
  10294. "// clang-format off\n"
  10295. " int j;\n"
  10296. "// clang-format on\n"
  10297. "int k;",
  10298. format(" int i;\n"
  10299. " // clang-format off\n"
  10300. " int j;\n"
  10301. " // clang-format on\n"
  10302. " int k;"));
  10303. EXPECT_EQ("int i;\n"
  10304. "/* clang-format off */\n"
  10305. " int j;\n"
  10306. "/* clang-format on */\n"
  10307. "int k;",
  10308. format(" int i;\n"
  10309. " /* clang-format off */\n"
  10310. " int j;\n"
  10311. " /* clang-format on */\n"
  10312. " int k;"));
  10313. }
  10314. TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
  10315. format("? ) =");
  10316. verifyNoCrash("#define a\\\n /**/}");
  10317. }
  10318. TEST_F(FormatTest, FormatsTableGenCode) {
  10319. FormatStyle Style = getLLVMStyle();
  10320. Style.Language = FormatStyle::LK_TableGen;
  10321. verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
  10322. }
  10323. } // end namespace
  10324. } // end namespace format
  10325. } // end namespace clang