FormatTest.cpp 433 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567
  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 "clang/Format/Format.h"
  10. #include "../Tooling/RewriterTestContext.h"
  11. #include "FormatTestUtils.h"
  12. #include "clang/Frontend/TextDiagnosticPrinter.h"
  13. #include "llvm/Support/Debug.h"
  14. #include "llvm/Support/MemoryBuffer.h"
  15. #include "gtest/gtest.h"
  16. #define DEBUG_TYPE "format-test"
  17. namespace clang {
  18. namespace format {
  19. namespace {
  20. FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
  21. class FormatTest : public ::testing::Test {
  22. protected:
  23. enum IncompleteCheck {
  24. IC_ExpectComplete,
  25. IC_ExpectIncomplete,
  26. IC_DoNotCheck
  27. };
  28. std::string format(llvm::StringRef Code,
  29. const FormatStyle &Style = getLLVMStyle(),
  30. IncompleteCheck CheckIncomplete = IC_ExpectComplete) {
  31. DEBUG(llvm::errs() << "---\n");
  32. DEBUG(llvm::errs() << Code << "\n\n");
  33. std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
  34. bool IncompleteFormat = false;
  35. tooling::Replacements Replaces =
  36. reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
  37. if (CheckIncomplete != IC_DoNotCheck) {
  38. bool ExpectedIncompleteFormat = CheckIncomplete == IC_ExpectIncomplete;
  39. EXPECT_EQ(ExpectedIncompleteFormat, IncompleteFormat) << Code << "\n\n";
  40. }
  41. ReplacementCount = Replaces.size();
  42. std::string Result = applyAllReplacements(Code, Replaces);
  43. EXPECT_NE("", Result);
  44. DEBUG(llvm::errs() << "\n" << Result << "\n\n");
  45. return Result;
  46. }
  47. FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
  48. FormatStyle Style = getLLVMStyle();
  49. Style.ColumnLimit = ColumnLimit;
  50. return Style;
  51. }
  52. FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
  53. FormatStyle Style = getGoogleStyle();
  54. Style.ColumnLimit = ColumnLimit;
  55. return Style;
  56. }
  57. void verifyFormat(llvm::StringRef Code,
  58. const FormatStyle &Style = getLLVMStyle()) {
  59. EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
  60. }
  61. void verifyIncompleteFormat(llvm::StringRef Code,
  62. const FormatStyle &Style = getLLVMStyle()) {
  63. EXPECT_EQ(Code.str(),
  64. format(test::messUp(Code), Style, IC_ExpectIncomplete));
  65. }
  66. void verifyGoogleFormat(llvm::StringRef Code) {
  67. verifyFormat(Code, getGoogleStyle());
  68. }
  69. void verifyIndependentOfContext(llvm::StringRef text) {
  70. verifyFormat(text);
  71. verifyFormat(llvm::Twine("void f() { " + text + " }").str());
  72. }
  73. /// \brief Verify that clang-format does not crash on the given input.
  74. void verifyNoCrash(llvm::StringRef Code,
  75. const FormatStyle &Style = getLLVMStyle()) {
  76. format(Code, Style, IC_DoNotCheck);
  77. }
  78. int ReplacementCount;
  79. };
  80. TEST_F(FormatTest, MessUp) {
  81. EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
  82. EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
  83. EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
  84. EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
  85. EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
  86. }
  87. //===----------------------------------------------------------------------===//
  88. // Basic function tests.
  89. //===----------------------------------------------------------------------===//
  90. TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
  91. EXPECT_EQ(";", format(";"));
  92. }
  93. TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
  94. EXPECT_EQ("int i;", format(" int i;"));
  95. EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;"));
  96. EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
  97. EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
  98. }
  99. TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
  100. EXPECT_EQ("int i;", format("int\ni;"));
  101. }
  102. TEST_F(FormatTest, FormatsNestedBlockStatements) {
  103. EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
  104. }
  105. TEST_F(FormatTest, FormatsNestedCall) {
  106. verifyFormat("Method(f1, f2(f3));");
  107. verifyFormat("Method(f1(f2, f3()));");
  108. verifyFormat("Method(f1(f2, (f3())));");
  109. }
  110. TEST_F(FormatTest, NestedNameSpecifiers) {
  111. verifyFormat("vector<::Type> v;");
  112. verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
  113. verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
  114. verifyFormat("bool a = 2 < ::SomeFunction();");
  115. }
  116. TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
  117. EXPECT_EQ("if (a) {\n"
  118. " f();\n"
  119. "}",
  120. format("if(a){f();}"));
  121. EXPECT_EQ(4, ReplacementCount);
  122. EXPECT_EQ("if (a) {\n"
  123. " f();\n"
  124. "}",
  125. format("if (a) {\n"
  126. " f();\n"
  127. "}"));
  128. EXPECT_EQ(0, ReplacementCount);
  129. EXPECT_EQ("/*\r\n"
  130. "\r\n"
  131. "*/\r\n",
  132. format("/*\r\n"
  133. "\r\n"
  134. "*/\r\n"));
  135. EXPECT_EQ(0, ReplacementCount);
  136. }
  137. TEST_F(FormatTest, RemovesEmptyLines) {
  138. EXPECT_EQ("class C {\n"
  139. " int i;\n"
  140. "};",
  141. format("class C {\n"
  142. " int i;\n"
  143. "\n"
  144. "};"));
  145. // Don't remove empty lines at the start of namespaces or extern "C" blocks.
  146. EXPECT_EQ("namespace N {\n"
  147. "\n"
  148. "int i;\n"
  149. "}",
  150. format("namespace N {\n"
  151. "\n"
  152. "int i;\n"
  153. "}",
  154. getGoogleStyle()));
  155. EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
  156. "\n"
  157. "int i;\n"
  158. "}",
  159. format("extern /**/ \"C\" /**/ {\n"
  160. "\n"
  161. "int i;\n"
  162. "}",
  163. getGoogleStyle()));
  164. // ...but do keep inlining and removing empty lines for non-block extern "C"
  165. // functions.
  166. verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
  167. EXPECT_EQ("extern \"C\" int f() {\n"
  168. " int i = 42;\n"
  169. " return i;\n"
  170. "}",
  171. format("extern \"C\" int f() {\n"
  172. "\n"
  173. " int i = 42;\n"
  174. " return i;\n"
  175. "}",
  176. getGoogleStyle()));
  177. // Remove empty lines at the beginning and end of blocks.
  178. EXPECT_EQ("void f() {\n"
  179. "\n"
  180. " if (a) {\n"
  181. "\n"
  182. " f();\n"
  183. " }\n"
  184. "}",
  185. format("void f() {\n"
  186. "\n"
  187. " if (a) {\n"
  188. "\n"
  189. " f();\n"
  190. "\n"
  191. " }\n"
  192. "\n"
  193. "}",
  194. getLLVMStyle()));
  195. EXPECT_EQ("void f() {\n"
  196. " if (a) {\n"
  197. " f();\n"
  198. " }\n"
  199. "}",
  200. format("void f() {\n"
  201. "\n"
  202. " if (a) {\n"
  203. "\n"
  204. " f();\n"
  205. "\n"
  206. " }\n"
  207. "\n"
  208. "}",
  209. getGoogleStyle()));
  210. // Don't remove empty lines in more complex control statements.
  211. EXPECT_EQ("void f() {\n"
  212. " if (a) {\n"
  213. " f();\n"
  214. "\n"
  215. " } else if (b) {\n"
  216. " f();\n"
  217. " }\n"
  218. "}",
  219. format("void f() {\n"
  220. " if (a) {\n"
  221. " f();\n"
  222. "\n"
  223. " } else if (b) {\n"
  224. " f();\n"
  225. "\n"
  226. " }\n"
  227. "\n"
  228. "}"));
  229. // FIXME: This is slightly inconsistent.
  230. EXPECT_EQ("namespace {\n"
  231. "int i;\n"
  232. "}",
  233. format("namespace {\n"
  234. "int i;\n"
  235. "\n"
  236. "}"));
  237. EXPECT_EQ("namespace {\n"
  238. "int i;\n"
  239. "\n"
  240. "} // namespace",
  241. format("namespace {\n"
  242. "int i;\n"
  243. "\n"
  244. "} // namespace"));
  245. }
  246. TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
  247. verifyFormat("x = (a) and (b);");
  248. verifyFormat("x = (a) or (b);");
  249. verifyFormat("x = (a) bitand (b);");
  250. verifyFormat("x = (a) bitor (b);");
  251. verifyFormat("x = (a) not_eq (b);");
  252. verifyFormat("x = (a) and_eq (b);");
  253. verifyFormat("x = (a) or_eq (b);");
  254. verifyFormat("x = (a) xor (b);");
  255. }
  256. //===----------------------------------------------------------------------===//
  257. // Tests for control statements.
  258. //===----------------------------------------------------------------------===//
  259. TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
  260. verifyFormat("if (true)\n f();\ng();");
  261. verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
  262. verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
  263. FormatStyle AllowsMergedIf = getLLVMStyle();
  264. AllowsMergedIf.AlignEscapedNewlinesLeft = true;
  265. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  266. verifyFormat("if (a)\n"
  267. " // comment\n"
  268. " f();",
  269. AllowsMergedIf);
  270. verifyFormat("{\n"
  271. " if (a)\n"
  272. " label:\n"
  273. " f();\n"
  274. "}",
  275. AllowsMergedIf);
  276. verifyFormat("#define A \\\n"
  277. " if (a) \\\n"
  278. " label: \\\n"
  279. " f()",
  280. AllowsMergedIf);
  281. verifyFormat("if (a)\n"
  282. " ;",
  283. AllowsMergedIf);
  284. verifyFormat("if (a)\n"
  285. " if (b) return;",
  286. AllowsMergedIf);
  287. verifyFormat("if (a) // Can't merge this\n"
  288. " f();\n",
  289. AllowsMergedIf);
  290. verifyFormat("if (a) /* still don't merge */\n"
  291. " f();",
  292. AllowsMergedIf);
  293. verifyFormat("if (a) { // Never merge this\n"
  294. " f();\n"
  295. "}",
  296. AllowsMergedIf);
  297. verifyFormat("if (a) { /* Never merge this */\n"
  298. " f();\n"
  299. "}",
  300. AllowsMergedIf);
  301. AllowsMergedIf.ColumnLimit = 14;
  302. verifyFormat("if (a) return;", AllowsMergedIf);
  303. verifyFormat("if (aaaaaaaaa)\n"
  304. " return;",
  305. AllowsMergedIf);
  306. AllowsMergedIf.ColumnLimit = 13;
  307. verifyFormat("if (a)\n return;", AllowsMergedIf);
  308. }
  309. TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
  310. FormatStyle AllowsMergedLoops = getLLVMStyle();
  311. AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
  312. verifyFormat("while (true) continue;", AllowsMergedLoops);
  313. verifyFormat("for (;;) continue;", AllowsMergedLoops);
  314. verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
  315. verifyFormat("while (true)\n"
  316. " ;",
  317. AllowsMergedLoops);
  318. verifyFormat("for (;;)\n"
  319. " ;",
  320. AllowsMergedLoops);
  321. verifyFormat("for (;;)\n"
  322. " for (;;) continue;",
  323. AllowsMergedLoops);
  324. verifyFormat("for (;;) // Can't merge this\n"
  325. " continue;",
  326. AllowsMergedLoops);
  327. verifyFormat("for (;;) /* still don't merge */\n"
  328. " continue;",
  329. AllowsMergedLoops);
  330. }
  331. TEST_F(FormatTest, FormatShortBracedStatements) {
  332. FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
  333. AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
  334. AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
  335. AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
  336. verifyFormat("if (true) {}", AllowSimpleBracedStatements);
  337. verifyFormat("while (true) {}", AllowSimpleBracedStatements);
  338. verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
  339. verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
  340. verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
  341. verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
  342. verifyFormat("if (true) { //\n"
  343. " f();\n"
  344. "}",
  345. AllowSimpleBracedStatements);
  346. verifyFormat("if (true) {\n"
  347. " f();\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. verifyFormat("template <int> struct A2 {\n"
  358. " struct B {};\n"
  359. "};",
  360. AllowSimpleBracedStatements);
  361. AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
  362. verifyFormat("if (true) {\n"
  363. " f();\n"
  364. "}",
  365. AllowSimpleBracedStatements);
  366. verifyFormat("if (true) {\n"
  367. " f();\n"
  368. "} else {\n"
  369. " f();\n"
  370. "}",
  371. AllowSimpleBracedStatements);
  372. AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
  373. verifyFormat("while (true) {\n"
  374. " f();\n"
  375. "}",
  376. AllowSimpleBracedStatements);
  377. verifyFormat("for (;;) {\n"
  378. " f();\n"
  379. "}",
  380. AllowSimpleBracedStatements);
  381. }
  382. TEST_F(FormatTest, ParseIfElse) {
  383. verifyFormat("if (true)\n"
  384. " if (true)\n"
  385. " if (true)\n"
  386. " f();\n"
  387. " else\n"
  388. " g();\n"
  389. " else\n"
  390. " h();\n"
  391. "else\n"
  392. " i();");
  393. verifyFormat("if (true)\n"
  394. " if (true)\n"
  395. " if (true) {\n"
  396. " if (true)\n"
  397. " f();\n"
  398. " } else {\n"
  399. " g();\n"
  400. " }\n"
  401. " else\n"
  402. " h();\n"
  403. "else {\n"
  404. " i();\n"
  405. "}");
  406. verifyFormat("void f() {\n"
  407. " if (a) {\n"
  408. " } else {\n"
  409. " }\n"
  410. "}");
  411. }
  412. TEST_F(FormatTest, ElseIf) {
  413. verifyFormat("if (a) {\n} else if (b) {\n}");
  414. verifyFormat("if (a)\n"
  415. " f();\n"
  416. "else if (b)\n"
  417. " g();\n"
  418. "else\n"
  419. " h();");
  420. verifyFormat("if (a) {\n"
  421. " f();\n"
  422. "}\n"
  423. "// or else ..\n"
  424. "else {\n"
  425. " g()\n"
  426. "}");
  427. verifyFormat("if (a) {\n"
  428. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  429. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  430. "}");
  431. verifyFormat("if (a) {\n"
  432. "} else if (\n"
  433. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  434. "}",
  435. getLLVMStyleWithColumns(62));
  436. }
  437. TEST_F(FormatTest, FormatsForLoop) {
  438. verifyFormat(
  439. "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
  440. " ++VeryVeryLongLoopVariable)\n"
  441. " ;");
  442. verifyFormat("for (;;)\n"
  443. " f();");
  444. verifyFormat("for (;;) {\n}");
  445. verifyFormat("for (;;) {\n"
  446. " f();\n"
  447. "}");
  448. verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
  449. verifyFormat(
  450. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  451. " E = UnwrappedLines.end();\n"
  452. " I != E; ++I) {\n}");
  453. verifyFormat(
  454. "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
  455. " ++IIIII) {\n}");
  456. verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
  457. " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
  458. " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
  459. verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
  460. " I = FD->getDeclsInPrototypeScope().begin(),\n"
  461. " E = FD->getDeclsInPrototypeScope().end();\n"
  462. " I != E; ++I) {\n}");
  463. verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
  464. " I = Container.begin(),\n"
  465. " E = Container.end();\n"
  466. " I != E; ++I) {\n}",
  467. getLLVMStyleWithColumns(76));
  468. verifyFormat(
  469. "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  470. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
  471. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  472. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  473. " ++aaaaaaaaaaa) {\n}");
  474. verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  475. " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
  476. " ++i) {\n}");
  477. verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
  478. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  479. "}");
  480. verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
  481. " aaaaaaaaaa);\n"
  482. " iter; ++iter) {\n"
  483. "}");
  484. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  485. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  486. " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
  487. " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
  488. FormatStyle NoBinPacking = getLLVMStyle();
  489. NoBinPacking.BinPackParameters = false;
  490. verifyFormat("for (int aaaaaaaaaaa = 1;\n"
  491. " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
  492. " aaaaaaaaaaaaaaaa,\n"
  493. " aaaaaaaaaaaaaaaa,\n"
  494. " aaaaaaaaaaaaaaaa);\n"
  495. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  496. "}",
  497. NoBinPacking);
  498. verifyFormat(
  499. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  500. " E = UnwrappedLines.end();\n"
  501. " I != E;\n"
  502. " ++I) {\n}",
  503. NoBinPacking);
  504. }
  505. TEST_F(FormatTest, RangeBasedForLoops) {
  506. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  507. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  508. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
  509. " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
  510. verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
  511. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  512. verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
  513. " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
  514. }
  515. TEST_F(FormatTest, ForEachLoops) {
  516. verifyFormat("void f() {\n"
  517. " foreach (Item *item, itemlist) {}\n"
  518. " Q_FOREACH (Item *item, itemlist) {}\n"
  519. " BOOST_FOREACH (Item *item, itemlist) {}\n"
  520. " UNKNOWN_FORACH(Item * item, itemlist) {}\n"
  521. "}");
  522. // As function-like macros.
  523. verifyFormat("#define foreach(x, y)\n"
  524. "#define Q_FOREACH(x, y)\n"
  525. "#define BOOST_FOREACH(x, y)\n"
  526. "#define UNKNOWN_FOREACH(x, y)\n");
  527. // Not as function-like macros.
  528. verifyFormat("#define foreach (x, y)\n"
  529. "#define Q_FOREACH (x, y)\n"
  530. "#define BOOST_FOREACH (x, y)\n"
  531. "#define UNKNOWN_FOREACH (x, y)\n");
  532. }
  533. TEST_F(FormatTest, FormatsWhileLoop) {
  534. verifyFormat("while (true) {\n}");
  535. verifyFormat("while (true)\n"
  536. " f();");
  537. verifyFormat("while () {\n}");
  538. verifyFormat("while () {\n"
  539. " f();\n"
  540. "}");
  541. }
  542. TEST_F(FormatTest, FormatsDoWhile) {
  543. verifyFormat("do {\n"
  544. " do_something();\n"
  545. "} while (something());");
  546. verifyFormat("do\n"
  547. " do_something();\n"
  548. "while (something());");
  549. }
  550. TEST_F(FormatTest, FormatsSwitchStatement) {
  551. verifyFormat("switch (x) {\n"
  552. "case 1:\n"
  553. " f();\n"
  554. " break;\n"
  555. "case kFoo:\n"
  556. "case ns::kBar:\n"
  557. "case kBaz:\n"
  558. " break;\n"
  559. "default:\n"
  560. " g();\n"
  561. " break;\n"
  562. "}");
  563. verifyFormat("switch (x) {\n"
  564. "case 1: {\n"
  565. " f();\n"
  566. " break;\n"
  567. "}\n"
  568. "case 2: {\n"
  569. " break;\n"
  570. "}\n"
  571. "}");
  572. verifyFormat("switch (x) {\n"
  573. "case 1: {\n"
  574. " f();\n"
  575. " {\n"
  576. " g();\n"
  577. " h();\n"
  578. " }\n"
  579. " break;\n"
  580. "}\n"
  581. "}");
  582. verifyFormat("switch (x) {\n"
  583. "case 1: {\n"
  584. " f();\n"
  585. " if (foo) {\n"
  586. " g();\n"
  587. " h();\n"
  588. " }\n"
  589. " break;\n"
  590. "}\n"
  591. "}");
  592. verifyFormat("switch (x) {\n"
  593. "case 1: {\n"
  594. " f();\n"
  595. " g();\n"
  596. "} break;\n"
  597. "}");
  598. verifyFormat("switch (test)\n"
  599. " ;");
  600. verifyFormat("switch (x) {\n"
  601. "default: {\n"
  602. " // Do nothing.\n"
  603. "}\n"
  604. "}");
  605. verifyFormat("switch (x) {\n"
  606. "// comment\n"
  607. "// if 1, do f()\n"
  608. "case 1:\n"
  609. " f();\n"
  610. "}");
  611. verifyFormat("switch (x) {\n"
  612. "case 1:\n"
  613. " // Do amazing stuff\n"
  614. " {\n"
  615. " f();\n"
  616. " g();\n"
  617. " }\n"
  618. " break;\n"
  619. "}");
  620. verifyFormat("#define A \\\n"
  621. " switch (x) { \\\n"
  622. " case a: \\\n"
  623. " foo = b; \\\n"
  624. " }",
  625. getLLVMStyleWithColumns(20));
  626. verifyFormat("#define OPERATION_CASE(name) \\\n"
  627. " case OP_name: \\\n"
  628. " return operations::Operation##name\n",
  629. getLLVMStyleWithColumns(40));
  630. verifyFormat("switch (x) {\n"
  631. "case 1:;\n"
  632. "default:;\n"
  633. " int i;\n"
  634. "}");
  635. verifyGoogleFormat("switch (x) {\n"
  636. " case 1:\n"
  637. " f();\n"
  638. " break;\n"
  639. " case kFoo:\n"
  640. " case ns::kBar:\n"
  641. " case kBaz:\n"
  642. " break;\n"
  643. " default:\n"
  644. " g();\n"
  645. " break;\n"
  646. "}");
  647. verifyGoogleFormat("switch (x) {\n"
  648. " case 1: {\n"
  649. " f();\n"
  650. " break;\n"
  651. " }\n"
  652. "}");
  653. verifyGoogleFormat("switch (test)\n"
  654. " ;");
  655. verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
  656. " case OP_name: \\\n"
  657. " return operations::Operation##name\n");
  658. verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
  659. " // Get the correction operation class.\n"
  660. " switch (OpCode) {\n"
  661. " CASE(Add);\n"
  662. " CASE(Subtract);\n"
  663. " default:\n"
  664. " return operations::Unknown;\n"
  665. " }\n"
  666. "#undef OPERATION_CASE\n"
  667. "}");
  668. verifyFormat("DEBUG({\n"
  669. " switch (x) {\n"
  670. " case A:\n"
  671. " f();\n"
  672. " break;\n"
  673. " // On B:\n"
  674. " case B:\n"
  675. " g();\n"
  676. " break;\n"
  677. " }\n"
  678. "});");
  679. verifyFormat("switch (a) {\n"
  680. "case (b):\n"
  681. " return;\n"
  682. "}");
  683. verifyFormat("switch (a) {\n"
  684. "case some_namespace::\n"
  685. " some_constant:\n"
  686. " return;\n"
  687. "}",
  688. getLLVMStyleWithColumns(34));
  689. }
  690. TEST_F(FormatTest, CaseRanges) {
  691. verifyFormat("switch (x) {\n"
  692. "case 'A' ... 'Z':\n"
  693. "case 1 ... 5:\n"
  694. "case a ... b:\n"
  695. " break;\n"
  696. "}");
  697. }
  698. TEST_F(FormatTest, ShortCaseLabels) {
  699. FormatStyle Style = getLLVMStyle();
  700. Style.AllowShortCaseLabelsOnASingleLine = true;
  701. verifyFormat("switch (a) {\n"
  702. "case 1: x = 1; break;\n"
  703. "case 2: return;\n"
  704. "case 3:\n"
  705. "case 4:\n"
  706. "case 5: return;\n"
  707. "case 6: // comment\n"
  708. " return;\n"
  709. "case 7:\n"
  710. " // comment\n"
  711. " return;\n"
  712. "case 8:\n"
  713. " x = 8; // comment\n"
  714. " break;\n"
  715. "default: y = 1; break;\n"
  716. "}",
  717. Style);
  718. verifyFormat("switch (a) {\n"
  719. "#if FOO\n"
  720. "case 0: return 0;\n"
  721. "#endif\n"
  722. "}",
  723. Style);
  724. verifyFormat("switch (a) {\n"
  725. "case 1: {\n"
  726. "}\n"
  727. "case 2: {\n"
  728. " return;\n"
  729. "}\n"
  730. "case 3: {\n"
  731. " x = 1;\n"
  732. " return;\n"
  733. "}\n"
  734. "case 4:\n"
  735. " if (x)\n"
  736. " return;\n"
  737. "}",
  738. Style);
  739. Style.ColumnLimit = 21;
  740. verifyFormat("switch (a) {\n"
  741. "case 1: x = 1; break;\n"
  742. "case 2: return;\n"
  743. "case 3:\n"
  744. "case 4:\n"
  745. "case 5: return;\n"
  746. "default:\n"
  747. " y = 1;\n"
  748. " break;\n"
  749. "}",
  750. Style);
  751. }
  752. TEST_F(FormatTest, FormatsLabels) {
  753. verifyFormat("void f() {\n"
  754. " some_code();\n"
  755. "test_label:\n"
  756. " some_other_code();\n"
  757. " {\n"
  758. " some_more_code();\n"
  759. " another_label:\n"
  760. " some_more_code();\n"
  761. " }\n"
  762. "}");
  763. verifyFormat("{\n"
  764. " some_code();\n"
  765. "test_label:\n"
  766. " some_other_code();\n"
  767. "}");
  768. verifyFormat("{\n"
  769. " some_code();\n"
  770. "test_label:;\n"
  771. " int i = 0;\n"
  772. "}");
  773. }
  774. //===----------------------------------------------------------------------===//
  775. // Tests for comments.
  776. //===----------------------------------------------------------------------===//
  777. TEST_F(FormatTest, UnderstandsSingleLineComments) {
  778. verifyFormat("//* */");
  779. verifyFormat("// line 1\n"
  780. "// line 2\n"
  781. "void f() {}\n");
  782. verifyFormat("void f() {\n"
  783. " // Doesn't do anything\n"
  784. "}");
  785. verifyFormat("SomeObject\n"
  786. " // Calling someFunction on SomeObject\n"
  787. " .someFunction();");
  788. verifyFormat("auto result = SomeObject\n"
  789. " // Calling someFunction on SomeObject\n"
  790. " .someFunction();");
  791. verifyFormat("void f(int i, // some comment (probably for i)\n"
  792. " int j, // some comment (probably for j)\n"
  793. " int k); // some comment (probably for k)");
  794. verifyFormat("void f(int i,\n"
  795. " // some comment (probably for j)\n"
  796. " int j,\n"
  797. " // some comment (probably for k)\n"
  798. " int k);");
  799. verifyFormat("int i // This is a fancy variable\n"
  800. " = 5; // with nicely aligned comment.");
  801. verifyFormat("// Leading comment.\n"
  802. "int a; // Trailing comment.");
  803. verifyFormat("int a; // Trailing comment\n"
  804. " // on 2\n"
  805. " // or 3 lines.\n"
  806. "int b;");
  807. verifyFormat("int a; // Trailing comment\n"
  808. "\n"
  809. "// Leading comment.\n"
  810. "int b;");
  811. verifyFormat("int a; // Comment.\n"
  812. " // More details.\n"
  813. "int bbbb; // Another comment.");
  814. verifyFormat(
  815. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  816. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n"
  817. "int cccccccccccccccccccccccccccccc; // comment\n"
  818. "int ddd; // looooooooooooooooooooooooong comment\n"
  819. "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  820. "int bbbbbbbbbbbbbbbbbbbbb; // comment\n"
  821. "int ccccccccccccccccccc; // comment");
  822. verifyFormat("#include \"a\" // comment\n"
  823. "#include \"a/b/c\" // comment");
  824. verifyFormat("#include <a> // comment\n"
  825. "#include <a/b/c> // comment");
  826. EXPECT_EQ("#include \"a\" // comment\n"
  827. "#include \"a/b/c\" // comment",
  828. format("#include \\\n"
  829. " \"a\" // comment\n"
  830. "#include \"a/b/c\" // comment"));
  831. verifyFormat("enum E {\n"
  832. " // comment\n"
  833. " VAL_A, // comment\n"
  834. " VAL_B\n"
  835. "};");
  836. verifyFormat(
  837. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  838. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
  839. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  840. " // Comment inside a statement.\n"
  841. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  842. verifyFormat("SomeFunction(a,\n"
  843. " // comment\n"
  844. " b + x);");
  845. verifyFormat("SomeFunction(a, a,\n"
  846. " // comment\n"
  847. " b + x);");
  848. verifyFormat(
  849. "bool aaaaaaaaaaaaa = // comment\n"
  850. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  851. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  852. verifyFormat("int aaaa; // aaaaa\n"
  853. "int aa; // aaaaaaa",
  854. getLLVMStyleWithColumns(20));
  855. EXPECT_EQ("void f() { // This does something ..\n"
  856. "}\n"
  857. "int a; // This is unrelated",
  858. format("void f() { // This does something ..\n"
  859. " }\n"
  860. "int a; // This is unrelated"));
  861. EXPECT_EQ("class C {\n"
  862. " void f() { // This does something ..\n"
  863. " } // awesome..\n"
  864. "\n"
  865. " int a; // This is unrelated\n"
  866. "};",
  867. format("class C{void f() { // This does something ..\n"
  868. " } // awesome..\n"
  869. " \n"
  870. "int a; // This is unrelated\n"
  871. "};"));
  872. EXPECT_EQ("int i; // single line trailing comment",
  873. format("int i;\\\n// single line trailing comment"));
  874. verifyGoogleFormat("int a; // Trailing comment.");
  875. verifyFormat("someFunction(anotherFunction( // Force break.\n"
  876. " parameter));");
  877. verifyGoogleFormat("#endif // HEADER_GUARD");
  878. verifyFormat("const char *test[] = {\n"
  879. " // A\n"
  880. " \"aaaa\",\n"
  881. " // B\n"
  882. " \"aaaaa\"};");
  883. verifyGoogleFormat(
  884. "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  885. " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment");
  886. EXPECT_EQ("D(a, {\n"
  887. " // test\n"
  888. " int a;\n"
  889. "});",
  890. format("D(a, {\n"
  891. "// test\n"
  892. "int a;\n"
  893. "});"));
  894. EXPECT_EQ("lineWith(); // comment\n"
  895. "// at start\n"
  896. "otherLine();",
  897. format("lineWith(); // comment\n"
  898. "// at start\n"
  899. "otherLine();"));
  900. EXPECT_EQ("lineWith(); // comment\n"
  901. "/*\n"
  902. " * at start */\n"
  903. "otherLine();",
  904. format("lineWith(); // comment\n"
  905. "/*\n"
  906. " * at start */\n"
  907. "otherLine();"));
  908. EXPECT_EQ("lineWith(); // comment\n"
  909. " // at start\n"
  910. "otherLine();",
  911. format("lineWith(); // comment\n"
  912. " // at start\n"
  913. "otherLine();"));
  914. EXPECT_EQ("lineWith(); // comment\n"
  915. "// at start\n"
  916. "otherLine(); // comment",
  917. format("lineWith(); // comment\n"
  918. "// at start\n"
  919. "otherLine(); // comment"));
  920. EXPECT_EQ("lineWith();\n"
  921. "// at start\n"
  922. "otherLine(); // comment",
  923. format("lineWith();\n"
  924. " // at start\n"
  925. "otherLine(); // comment"));
  926. EXPECT_EQ("// first\n"
  927. "// at start\n"
  928. "otherLine(); // comment",
  929. format("// first\n"
  930. " // at start\n"
  931. "otherLine(); // comment"));
  932. EXPECT_EQ("f();\n"
  933. "// first\n"
  934. "// at start\n"
  935. "otherLine(); // comment",
  936. format("f();\n"
  937. "// first\n"
  938. " // at start\n"
  939. "otherLine(); // comment"));
  940. verifyFormat("f(); // comment\n"
  941. "// first\n"
  942. "// at start\n"
  943. "otherLine();");
  944. EXPECT_EQ("f(); // comment\n"
  945. "// first\n"
  946. "// at start\n"
  947. "otherLine();",
  948. format("f(); // comment\n"
  949. "// first\n"
  950. " // at start\n"
  951. "otherLine();"));
  952. EXPECT_EQ("f(); // comment\n"
  953. " // first\n"
  954. "// at start\n"
  955. "otherLine();",
  956. format("f(); // comment\n"
  957. " // first\n"
  958. "// at start\n"
  959. "otherLine();"));
  960. EXPECT_EQ("void f() {\n"
  961. " lineWith(); // comment\n"
  962. " // at start\n"
  963. "}",
  964. format("void f() {\n"
  965. " lineWith(); // comment\n"
  966. " // at start\n"
  967. "}"));
  968. EXPECT_EQ("int xy; // a\n"
  969. "int z; // b",
  970. format("int xy; // a\n"
  971. "int z; //b"));
  972. EXPECT_EQ("int xy; // a\n"
  973. "int z; // bb",
  974. format("int xy; // a\n"
  975. "int z; //bb",
  976. getLLVMStyleWithColumns(12)));
  977. verifyFormat("#define A \\\n"
  978. " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
  979. " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
  980. getLLVMStyleWithColumns(60));
  981. verifyFormat(
  982. "#define A \\\n"
  983. " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
  984. " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
  985. getLLVMStyleWithColumns(61));
  986. verifyFormat("if ( // This is some comment\n"
  987. " x + 3) {\n"
  988. "}");
  989. EXPECT_EQ("if ( // This is some comment\n"
  990. " // spanning two lines\n"
  991. " x + 3) {\n"
  992. "}",
  993. format("if( // This is some comment\n"
  994. " // spanning two lines\n"
  995. " x + 3) {\n"
  996. "}"));
  997. verifyNoCrash("/\\\n/");
  998. verifyNoCrash("/\\\n* */");
  999. // The 0-character somehow makes the lexer return a proper comment.
  1000. verifyNoCrash(StringRef("/*\\\0\n/", 6));
  1001. }
  1002. TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
  1003. EXPECT_EQ("SomeFunction(a,\n"
  1004. " b, // comment\n"
  1005. " c);",
  1006. format("SomeFunction(a,\n"
  1007. " b, // comment\n"
  1008. " c);"));
  1009. EXPECT_EQ("SomeFunction(a, b,\n"
  1010. " // comment\n"
  1011. " c);",
  1012. format("SomeFunction(a,\n"
  1013. " b,\n"
  1014. " // comment\n"
  1015. " c);"));
  1016. EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
  1017. " c);",
  1018. format("SomeFunction(a, b, // comment (unclear relation)\n"
  1019. " c);"));
  1020. EXPECT_EQ("SomeFunction(a, // comment\n"
  1021. " b,\n"
  1022. " c); // comment",
  1023. format("SomeFunction(a, // comment\n"
  1024. " b,\n"
  1025. " c); // comment"));
  1026. }
  1027. TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
  1028. EXPECT_EQ("// comment", format("// comment "));
  1029. EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
  1030. format("int aaaaaaa, bbbbbbb; // comment ",
  1031. getLLVMStyleWithColumns(33)));
  1032. EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f "));
  1033. EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f "));
  1034. }
  1035. TEST_F(FormatTest, UnderstandsBlockComments) {
  1036. verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
  1037. verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y, /*c=*/::c); }");
  1038. EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
  1039. " bbbbbbbbbbbbbbbbbbbbbbbbb);",
  1040. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n"
  1041. "/* Trailing comment for aa... */\n"
  1042. " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  1043. EXPECT_EQ(
  1044. "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  1045. " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
  1046. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
  1047. "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  1048. EXPECT_EQ(
  1049. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1050. " aaaaaaaaaaaaaaaaaa,\n"
  1051. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  1052. "}",
  1053. format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1054. " aaaaaaaaaaaaaaaaaa ,\n"
  1055. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  1056. "}"));
  1057. FormatStyle NoBinPacking = getLLVMStyle();
  1058. NoBinPacking.BinPackParameters = false;
  1059. verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
  1060. " /* parameter 2 */ aaaaaa,\n"
  1061. " /* parameter 3 */ aaaaaa,\n"
  1062. " /* parameter 4 */ aaaaaa);",
  1063. NoBinPacking);
  1064. // Aligning block comments in macros.
  1065. verifyGoogleFormat("#define A \\\n"
  1066. " int i; /*a*/ \\\n"
  1067. " int jjj; /*b*/");
  1068. }
  1069. TEST_F(FormatTest, AlignsBlockComments) {
  1070. EXPECT_EQ("/*\n"
  1071. " * Really multi-line\n"
  1072. " * comment.\n"
  1073. " */\n"
  1074. "void f() {}",
  1075. format(" /*\n"
  1076. " * Really multi-line\n"
  1077. " * comment.\n"
  1078. " */\n"
  1079. " void f() {}"));
  1080. EXPECT_EQ("class C {\n"
  1081. " /*\n"
  1082. " * Another multi-line\n"
  1083. " * comment.\n"
  1084. " */\n"
  1085. " void f() {}\n"
  1086. "};",
  1087. format("class C {\n"
  1088. "/*\n"
  1089. " * Another multi-line\n"
  1090. " * comment.\n"
  1091. " */\n"
  1092. "void f() {}\n"
  1093. "};"));
  1094. EXPECT_EQ("/*\n"
  1095. " 1. This is a comment with non-trivial formatting.\n"
  1096. " 1.1. We have to indent/outdent all lines equally\n"
  1097. " 1.1.1. to keep the formatting.\n"
  1098. " */",
  1099. format(" /*\n"
  1100. " 1. This is a comment with non-trivial formatting.\n"
  1101. " 1.1. We have to indent/outdent all lines equally\n"
  1102. " 1.1.1. to keep the formatting.\n"
  1103. " */"));
  1104. EXPECT_EQ("/*\n"
  1105. "Don't try to outdent if there's not enough indentation.\n"
  1106. "*/",
  1107. format(" /*\n"
  1108. " Don't try to outdent if there's not enough indentation.\n"
  1109. " */"));
  1110. EXPECT_EQ("int i; /* Comment with empty...\n"
  1111. " *\n"
  1112. " * line. */",
  1113. format("int i; /* Comment with empty...\n"
  1114. " *\n"
  1115. " * line. */"));
  1116. EXPECT_EQ("int foobar = 0; /* comment */\n"
  1117. "int bar = 0; /* multiline\n"
  1118. " comment 1 */\n"
  1119. "int baz = 0; /* multiline\n"
  1120. " comment 2 */\n"
  1121. "int bzz = 0; /* multiline\n"
  1122. " comment 3 */",
  1123. format("int foobar = 0; /* comment */\n"
  1124. "int bar = 0; /* multiline\n"
  1125. " comment 1 */\n"
  1126. "int baz = 0; /* multiline\n"
  1127. " comment 2 */\n"
  1128. "int bzz = 0; /* multiline\n"
  1129. " comment 3 */"));
  1130. EXPECT_EQ("int foobar = 0; /* comment */\n"
  1131. "int bar = 0; /* multiline\n"
  1132. " comment */\n"
  1133. "int baz = 0; /* multiline\n"
  1134. "comment */",
  1135. format("int foobar = 0; /* comment */\n"
  1136. "int bar = 0; /* multiline\n"
  1137. "comment */\n"
  1138. "int baz = 0; /* multiline\n"
  1139. "comment */"));
  1140. }
  1141. TEST_F(FormatTest, CommentReflowingCanBeTurnedOff) {
  1142. FormatStyle Style = getLLVMStyleWithColumns(20);
  1143. Style.ReflowComments = false;
  1144. verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style);
  1145. verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style);
  1146. }
  1147. TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
  1148. EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1149. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
  1150. format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1151. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
  1152. EXPECT_EQ(
  1153. "void ffffffffffff(\n"
  1154. " int aaaaaaaa, int bbbbbbbb,\n"
  1155. " int cccccccccccc) { /*\n"
  1156. " aaaaaaaaaa\n"
  1157. " aaaaaaaaaaaaa\n"
  1158. " bbbbbbbbbbbbbb\n"
  1159. " bbbbbbbbbb\n"
  1160. " */\n"
  1161. "}",
  1162. format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
  1163. "{ /*\n"
  1164. " aaaaaaaaaa aaaaaaaaaaaaa\n"
  1165. " bbbbbbbbbbbbbb bbbbbbbbbb\n"
  1166. " */\n"
  1167. "}",
  1168. getLLVMStyleWithColumns(40)));
  1169. }
  1170. TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
  1171. EXPECT_EQ("void ffffffffff(\n"
  1172. " int aaaaa /* test */);",
  1173. format("void ffffffffff(int aaaaa /* test */);",
  1174. getLLVMStyleWithColumns(35)));
  1175. }
  1176. TEST_F(FormatTest, SplitsLongCxxComments) {
  1177. EXPECT_EQ("// A comment that\n"
  1178. "// doesn't fit on\n"
  1179. "// one line",
  1180. format("// A comment that doesn't fit on one line",
  1181. getLLVMStyleWithColumns(20)));
  1182. EXPECT_EQ("/// A comment that\n"
  1183. "/// doesn't fit on\n"
  1184. "/// one line",
  1185. format("/// A comment that doesn't fit on one line",
  1186. getLLVMStyleWithColumns(20)));
  1187. EXPECT_EQ("//! A comment that\n"
  1188. "//! doesn't fit on\n"
  1189. "//! one line",
  1190. format("//! A comment that doesn't fit on one line",
  1191. getLLVMStyleWithColumns(20)));
  1192. EXPECT_EQ("// a b c d\n"
  1193. "// e f g\n"
  1194. "// h i j k",
  1195. format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10)));
  1196. EXPECT_EQ(
  1197. "// a b c d\n"
  1198. "// e f g\n"
  1199. "// h i j k",
  1200. format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10)));
  1201. EXPECT_EQ("if (true) // A comment that\n"
  1202. " // doesn't fit on\n"
  1203. " // one line",
  1204. format("if (true) // A comment that doesn't fit on one line ",
  1205. getLLVMStyleWithColumns(30)));
  1206. EXPECT_EQ("// Don't_touch_leading_whitespace",
  1207. format("// Don't_touch_leading_whitespace",
  1208. getLLVMStyleWithColumns(20)));
  1209. EXPECT_EQ("// Add leading\n"
  1210. "// whitespace",
  1211. format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
  1212. EXPECT_EQ("/// Add leading\n"
  1213. "/// whitespace",
  1214. format("///Add leading whitespace", getLLVMStyleWithColumns(20)));
  1215. EXPECT_EQ("//! Add leading\n"
  1216. "//! whitespace",
  1217. format("//!Add leading whitespace", getLLVMStyleWithColumns(20)));
  1218. EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
  1219. EXPECT_EQ("// Even if it makes the line exceed the column\n"
  1220. "// limit",
  1221. format("//Even if it makes the line exceed the column limit",
  1222. getLLVMStyleWithColumns(51)));
  1223. EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
  1224. EXPECT_EQ("// aa bb cc dd",
  1225. format("// aa bb cc dd ",
  1226. getLLVMStyleWithColumns(15)));
  1227. EXPECT_EQ("// A comment before\n"
  1228. "// a macro\n"
  1229. "// definition\n"
  1230. "#define a b",
  1231. format("// A comment before a macro definition\n"
  1232. "#define a b",
  1233. getLLVMStyleWithColumns(20)));
  1234. EXPECT_EQ("void ffffff(\n"
  1235. " int aaaaaaaaa, // wwww\n"
  1236. " int bbbbbbbbbb, // xxxxxxx\n"
  1237. " // yyyyyyyyyy\n"
  1238. " int c, int d, int e) {}",
  1239. format("void ffffff(\n"
  1240. " int aaaaaaaaa, // wwww\n"
  1241. " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
  1242. " int c, int d, int e) {}",
  1243. getLLVMStyleWithColumns(40)));
  1244. EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1245. format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1246. getLLVMStyleWithColumns(20)));
  1247. EXPECT_EQ(
  1248. "#define XXX // a b c d\n"
  1249. " // e f g h",
  1250. format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
  1251. EXPECT_EQ(
  1252. "#define XXX // q w e r\n"
  1253. " // t y u i",
  1254. format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
  1255. }
  1256. TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {
  1257. EXPECT_EQ("// A comment\n"
  1258. "// that doesn't\n"
  1259. "// fit on one\n"
  1260. "// line",
  1261. format("// A comment that doesn't fit on one line",
  1262. getLLVMStyleWithColumns(20)));
  1263. EXPECT_EQ("/// A comment\n"
  1264. "/// that doesn't\n"
  1265. "/// fit on one\n"
  1266. "/// line",
  1267. format("/// A comment that doesn't fit on one line",
  1268. getLLVMStyleWithColumns(20)));
  1269. }
  1270. TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
  1271. EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1272. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1273. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1274. format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1275. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1276. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
  1277. EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1278. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1279. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1280. format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1281. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1282. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1283. getLLVMStyleWithColumns(50)));
  1284. // FIXME: One day we might want to implement adjustment of leading whitespace
  1285. // of the consecutive lines in this kind of comment:
  1286. EXPECT_EQ("double\n"
  1287. " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1288. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1289. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1290. format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1291. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1292. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1293. getLLVMStyleWithColumns(49)));
  1294. }
  1295. TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) {
  1296. FormatStyle Pragmas = getLLVMStyleWithColumns(30);
  1297. Pragmas.CommentPragmas = "^ IWYU pragma:";
  1298. EXPECT_EQ(
  1299. "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
  1300. format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
  1301. EXPECT_EQ(
  1302. "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
  1303. format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
  1304. }
  1305. TEST_F(FormatTest, PriorityOfCommentBreaking) {
  1306. EXPECT_EQ("if (xxx ==\n"
  1307. " yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  1308. " zzz)\n"
  1309. " q();",
  1310. format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  1311. " zzz) q();",
  1312. getLLVMStyleWithColumns(40)));
  1313. EXPECT_EQ("if (xxxxxxxxxx ==\n"
  1314. " yyy && // aaaaaa bbbbbbbb cccc\n"
  1315. " zzz)\n"
  1316. " q();",
  1317. format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
  1318. " zzz) q();",
  1319. getLLVMStyleWithColumns(40)));
  1320. EXPECT_EQ("if (xxxxxxxxxx &&\n"
  1321. " yyy || // aaaaaa bbbbbbbb cccc\n"
  1322. " zzz)\n"
  1323. " q();",
  1324. format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
  1325. " zzz) q();",
  1326. getLLVMStyleWithColumns(40)));
  1327. EXPECT_EQ("fffffffff(\n"
  1328. " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  1329. " zzz);",
  1330. format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  1331. " zzz);",
  1332. getLLVMStyleWithColumns(40)));
  1333. }
  1334. TEST_F(FormatTest, MultiLineCommentsInDefines) {
  1335. EXPECT_EQ("#define A(x) /* \\\n"
  1336. " a comment \\\n"
  1337. " inside */ \\\n"
  1338. " f();",
  1339. format("#define A(x) /* \\\n"
  1340. " a comment \\\n"
  1341. " inside */ \\\n"
  1342. " f();",
  1343. getLLVMStyleWithColumns(17)));
  1344. EXPECT_EQ("#define A( \\\n"
  1345. " x) /* \\\n"
  1346. " a comment \\\n"
  1347. " inside */ \\\n"
  1348. " f();",
  1349. format("#define A( \\\n"
  1350. " x) /* \\\n"
  1351. " a comment \\\n"
  1352. " inside */ \\\n"
  1353. " f();",
  1354. getLLVMStyleWithColumns(17)));
  1355. }
  1356. TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
  1357. EXPECT_EQ("namespace {}\n// Test\n#define A",
  1358. format("namespace {}\n // Test\n#define A"));
  1359. EXPECT_EQ("namespace {}\n/* Test */\n#define A",
  1360. format("namespace {}\n /* Test */\n#define A"));
  1361. EXPECT_EQ("namespace {}\n/* Test */ #define A",
  1362. format("namespace {}\n /* Test */ #define A"));
  1363. }
  1364. TEST_F(FormatTest, SplitsLongLinesInComments) {
  1365. EXPECT_EQ("/* This is a long\n"
  1366. " * comment that\n"
  1367. " * doesn't\n"
  1368. " * fit on one line.\n"
  1369. " */",
  1370. format("/* "
  1371. "This is a long "
  1372. "comment that "
  1373. "doesn't "
  1374. "fit on one line. */",
  1375. getLLVMStyleWithColumns(20)));
  1376. EXPECT_EQ(
  1377. "/* a b c d\n"
  1378. " * e f g\n"
  1379. " * h i j k\n"
  1380. " */",
  1381. format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
  1382. EXPECT_EQ(
  1383. "/* a b c d\n"
  1384. " * e f g\n"
  1385. " * h i j k\n"
  1386. " */",
  1387. format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
  1388. EXPECT_EQ("/*\n"
  1389. "This is a long\n"
  1390. "comment that doesn't\n"
  1391. "fit on one line.\n"
  1392. "*/",
  1393. format("/*\n"
  1394. "This is a long "
  1395. "comment that doesn't "
  1396. "fit on one line. \n"
  1397. "*/",
  1398. getLLVMStyleWithColumns(20)));
  1399. EXPECT_EQ("/*\n"
  1400. " * This is a long\n"
  1401. " * comment that\n"
  1402. " * doesn't fit on\n"
  1403. " * one line.\n"
  1404. " */",
  1405. format("/* \n"
  1406. " * This is a long "
  1407. " comment that "
  1408. " doesn't fit on "
  1409. " one line. \n"
  1410. " */",
  1411. getLLVMStyleWithColumns(20)));
  1412. EXPECT_EQ("/*\n"
  1413. " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
  1414. " * so_it_should_be_broken\n"
  1415. " * wherever_a_space_occurs\n"
  1416. " */",
  1417. format("/*\n"
  1418. " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
  1419. " so_it_should_be_broken "
  1420. " wherever_a_space_occurs \n"
  1421. " */",
  1422. getLLVMStyleWithColumns(20)));
  1423. EXPECT_EQ("/*\n"
  1424. " * This_comment_can_not_be_broken_into_lines\n"
  1425. " */",
  1426. format("/*\n"
  1427. " * This_comment_can_not_be_broken_into_lines\n"
  1428. " */",
  1429. getLLVMStyleWithColumns(20)));
  1430. EXPECT_EQ("{\n"
  1431. " /*\n"
  1432. " This is another\n"
  1433. " long comment that\n"
  1434. " doesn't fit on one\n"
  1435. " line 1234567890\n"
  1436. " */\n"
  1437. "}",
  1438. format("{\n"
  1439. "/*\n"
  1440. "This is another "
  1441. " long comment that "
  1442. " doesn't fit on one"
  1443. " line 1234567890\n"
  1444. "*/\n"
  1445. "}",
  1446. getLLVMStyleWithColumns(20)));
  1447. EXPECT_EQ("{\n"
  1448. " /*\n"
  1449. " * This i s\n"
  1450. " * another comment\n"
  1451. " * t hat doesn' t\n"
  1452. " * fit on one l i\n"
  1453. " * n e\n"
  1454. " */\n"
  1455. "}",
  1456. format("{\n"
  1457. "/*\n"
  1458. " * This i s"
  1459. " another comment"
  1460. " t hat doesn' t"
  1461. " fit on one l i"
  1462. " n e\n"
  1463. " */\n"
  1464. "}",
  1465. getLLVMStyleWithColumns(20)));
  1466. EXPECT_EQ("/*\n"
  1467. " * This is a long\n"
  1468. " * comment that\n"
  1469. " * doesn't fit on\n"
  1470. " * one line\n"
  1471. " */",
  1472. format(" /*\n"
  1473. " * This is a long comment that doesn't fit on one line\n"
  1474. " */",
  1475. getLLVMStyleWithColumns(20)));
  1476. EXPECT_EQ("{\n"
  1477. " if (something) /* This is a\n"
  1478. " long\n"
  1479. " comment */\n"
  1480. " ;\n"
  1481. "}",
  1482. format("{\n"
  1483. " if (something) /* This is a long comment */\n"
  1484. " ;\n"
  1485. "}",
  1486. getLLVMStyleWithColumns(30)));
  1487. EXPECT_EQ("/* A comment before\n"
  1488. " * a macro\n"
  1489. " * definition */\n"
  1490. "#define a b",
  1491. format("/* A comment before a macro definition */\n"
  1492. "#define a b",
  1493. getLLVMStyleWithColumns(20)));
  1494. EXPECT_EQ("/* some comment\n"
  1495. " * a comment\n"
  1496. "* that we break\n"
  1497. " * another comment\n"
  1498. "* we have to break\n"
  1499. "* a left comment\n"
  1500. " */",
  1501. format(" /* some comment\n"
  1502. " * a comment that we break\n"
  1503. " * another comment we have to break\n"
  1504. "* a left comment\n"
  1505. " */",
  1506. getLLVMStyleWithColumns(20)));
  1507. EXPECT_EQ("/**\n"
  1508. " * multiline block\n"
  1509. " * comment\n"
  1510. " *\n"
  1511. " */",
  1512. format("/**\n"
  1513. " * multiline block comment\n"
  1514. " *\n"
  1515. " */",
  1516. getLLVMStyleWithColumns(20)));
  1517. EXPECT_EQ("/*\n"
  1518. "\n"
  1519. "\n"
  1520. " */\n",
  1521. format(" /* \n"
  1522. " \n"
  1523. " \n"
  1524. " */\n"));
  1525. EXPECT_EQ("/* a a */",
  1526. format("/* a a */", getLLVMStyleWithColumns(15)));
  1527. EXPECT_EQ("/* a a bc */",
  1528. format("/* a a bc */", getLLVMStyleWithColumns(15)));
  1529. EXPECT_EQ("/* aaa aaa\n"
  1530. " * aaaaa */",
  1531. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1532. EXPECT_EQ("/* aaa aaa\n"
  1533. " * aaaaa */",
  1534. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1535. }
  1536. TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
  1537. EXPECT_EQ("#define X \\\n"
  1538. " /* \\\n"
  1539. " Test \\\n"
  1540. " Macro comment \\\n"
  1541. " with a long \\\n"
  1542. " line \\\n"
  1543. " */ \\\n"
  1544. " A + B",
  1545. format("#define X \\\n"
  1546. " /*\n"
  1547. " Test\n"
  1548. " Macro comment with a long line\n"
  1549. " */ \\\n"
  1550. " A + B",
  1551. getLLVMStyleWithColumns(20)));
  1552. EXPECT_EQ("#define X \\\n"
  1553. " /* Macro comment \\\n"
  1554. " with a long \\\n"
  1555. " line */ \\\n"
  1556. " A + B",
  1557. format("#define X \\\n"
  1558. " /* Macro comment with a long\n"
  1559. " line */ \\\n"
  1560. " A + B",
  1561. getLLVMStyleWithColumns(20)));
  1562. EXPECT_EQ("#define X \\\n"
  1563. " /* Macro comment \\\n"
  1564. " * with a long \\\n"
  1565. " * line */ \\\n"
  1566. " A + B",
  1567. format("#define X \\\n"
  1568. " /* Macro comment with a long line */ \\\n"
  1569. " A + B",
  1570. getLLVMStyleWithColumns(20)));
  1571. }
  1572. TEST_F(FormatTest, CommentsInStaticInitializers) {
  1573. EXPECT_EQ(
  1574. "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
  1575. " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
  1576. " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
  1577. " aaaaaaaaaaaaaaaaaaaa, // comment\n"
  1578. " aaaaaaaaaaaaaaaaaaaa};",
  1579. format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
  1580. " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
  1581. " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
  1582. " aaaaaaaaaaaaaaaaaaaa , // comment\n"
  1583. " aaaaaaaaaaaaaaaaaaaa };"));
  1584. verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1585. " bbbbbbbbbbb, ccccccccccc};");
  1586. verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1587. " // comment for bb....\n"
  1588. " bbbbbbbbbbb, ccccccccccc};");
  1589. verifyGoogleFormat(
  1590. "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1591. " bbbbbbbbbbb, ccccccccccc};");
  1592. verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1593. " // comment for bb....\n"
  1594. " bbbbbbbbbbb, ccccccccccc};");
  1595. verifyFormat("S s = {{a, b, c}, // Group #1\n"
  1596. " {d, e, f}, // Group #2\n"
  1597. " {g, h, i}}; // Group #3");
  1598. verifyFormat("S s = {{// Group #1\n"
  1599. " a, b, c},\n"
  1600. " {// Group #2\n"
  1601. " d, e, f},\n"
  1602. " {// Group #3\n"
  1603. " g, h, i}};");
  1604. EXPECT_EQ("S s = {\n"
  1605. " // Some comment\n"
  1606. " a,\n"
  1607. "\n"
  1608. " // Comment after empty line\n"
  1609. " b}",
  1610. format("S s = {\n"
  1611. " // Some comment\n"
  1612. " a,\n"
  1613. " \n"
  1614. " // Comment after empty line\n"
  1615. " b\n"
  1616. "}"));
  1617. EXPECT_EQ("S s = {\n"
  1618. " /* Some comment */\n"
  1619. " a,\n"
  1620. "\n"
  1621. " /* Comment after empty line */\n"
  1622. " b}",
  1623. format("S s = {\n"
  1624. " /* Some comment */\n"
  1625. " a,\n"
  1626. " \n"
  1627. " /* Comment after empty line */\n"
  1628. " b\n"
  1629. "}"));
  1630. verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
  1631. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1632. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1633. " 0x00, 0x00, 0x00, 0x00}; // comment\n");
  1634. }
  1635. TEST_F(FormatTest, IgnoresIf0Contents) {
  1636. EXPECT_EQ("#if 0\n"
  1637. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1638. "#endif\n"
  1639. "void f() {}",
  1640. format("#if 0\n"
  1641. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1642. "#endif\n"
  1643. "void f( ) { }"));
  1644. EXPECT_EQ("#if false\n"
  1645. "void f( ) { }\n"
  1646. "#endif\n"
  1647. "void g() {}\n",
  1648. format("#if false\n"
  1649. "void f( ) { }\n"
  1650. "#endif\n"
  1651. "void g( ) { }\n"));
  1652. EXPECT_EQ("enum E {\n"
  1653. " One,\n"
  1654. " Two,\n"
  1655. "#if 0\n"
  1656. "Three,\n"
  1657. " Four,\n"
  1658. "#endif\n"
  1659. " Five\n"
  1660. "};",
  1661. format("enum E {\n"
  1662. " One,Two,\n"
  1663. "#if 0\n"
  1664. "Three,\n"
  1665. " Four,\n"
  1666. "#endif\n"
  1667. " Five};"));
  1668. EXPECT_EQ("enum F {\n"
  1669. " One,\n"
  1670. "#if 1\n"
  1671. " Two,\n"
  1672. "#if 0\n"
  1673. "Three,\n"
  1674. " Four,\n"
  1675. "#endif\n"
  1676. " Five\n"
  1677. "#endif\n"
  1678. "};",
  1679. format("enum F {\n"
  1680. "One,\n"
  1681. "#if 1\n"
  1682. "Two,\n"
  1683. "#if 0\n"
  1684. "Three,\n"
  1685. " Four,\n"
  1686. "#endif\n"
  1687. "Five\n"
  1688. "#endif\n"
  1689. "};"));
  1690. EXPECT_EQ("enum G {\n"
  1691. " One,\n"
  1692. "#if 0\n"
  1693. "Two,\n"
  1694. "#else\n"
  1695. " Three,\n"
  1696. "#endif\n"
  1697. " Four\n"
  1698. "};",
  1699. format("enum G {\n"
  1700. "One,\n"
  1701. "#if 0\n"
  1702. "Two,\n"
  1703. "#else\n"
  1704. "Three,\n"
  1705. "#endif\n"
  1706. "Four\n"
  1707. "};"));
  1708. EXPECT_EQ("enum H {\n"
  1709. " One,\n"
  1710. "#if 0\n"
  1711. "#ifdef Q\n"
  1712. "Two,\n"
  1713. "#else\n"
  1714. "Three,\n"
  1715. "#endif\n"
  1716. "#endif\n"
  1717. " Four\n"
  1718. "};",
  1719. format("enum H {\n"
  1720. "One,\n"
  1721. "#if 0\n"
  1722. "#ifdef Q\n"
  1723. "Two,\n"
  1724. "#else\n"
  1725. "Three,\n"
  1726. "#endif\n"
  1727. "#endif\n"
  1728. "Four\n"
  1729. "};"));
  1730. EXPECT_EQ("enum I {\n"
  1731. " One,\n"
  1732. "#if /* test */ 0 || 1\n"
  1733. "Two,\n"
  1734. "Three,\n"
  1735. "#endif\n"
  1736. " Four\n"
  1737. "};",
  1738. format("enum I {\n"
  1739. "One,\n"
  1740. "#if /* test */ 0 || 1\n"
  1741. "Two,\n"
  1742. "Three,\n"
  1743. "#endif\n"
  1744. "Four\n"
  1745. "};"));
  1746. EXPECT_EQ("enum J {\n"
  1747. " One,\n"
  1748. "#if 0\n"
  1749. "#if 0\n"
  1750. "Two,\n"
  1751. "#else\n"
  1752. "Three,\n"
  1753. "#endif\n"
  1754. "Four,\n"
  1755. "#endif\n"
  1756. " Five\n"
  1757. "};",
  1758. format("enum J {\n"
  1759. "One,\n"
  1760. "#if 0\n"
  1761. "#if 0\n"
  1762. "Two,\n"
  1763. "#else\n"
  1764. "Three,\n"
  1765. "#endif\n"
  1766. "Four,\n"
  1767. "#endif\n"
  1768. "Five\n"
  1769. "};"));
  1770. }
  1771. //===----------------------------------------------------------------------===//
  1772. // Tests for classes, namespaces, etc.
  1773. //===----------------------------------------------------------------------===//
  1774. TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
  1775. verifyFormat("class A {};");
  1776. }
  1777. TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
  1778. verifyFormat("class A {\n"
  1779. "public:\n"
  1780. "public: // comment\n"
  1781. "protected:\n"
  1782. "private:\n"
  1783. " void f() {}\n"
  1784. "};");
  1785. verifyGoogleFormat("class A {\n"
  1786. " public:\n"
  1787. " protected:\n"
  1788. " private:\n"
  1789. " void f() {}\n"
  1790. "};");
  1791. verifyFormat("class A {\n"
  1792. "public slots:\n"
  1793. " void f1() {}\n"
  1794. "public Q_SLOTS:\n"
  1795. " void f2() {}\n"
  1796. "protected slots:\n"
  1797. " void f3() {}\n"
  1798. "protected Q_SLOTS:\n"
  1799. " void f4() {}\n"
  1800. "private slots:\n"
  1801. " void f5() {}\n"
  1802. "private Q_SLOTS:\n"
  1803. " void f6() {}\n"
  1804. "signals:\n"
  1805. " void g1();\n"
  1806. "Q_SIGNALS:\n"
  1807. " void g2();\n"
  1808. "};");
  1809. // Don't interpret 'signals' the wrong way.
  1810. verifyFormat("signals.set();");
  1811. verifyFormat("for (Signals signals : f()) {\n}");
  1812. verifyFormat("{\n"
  1813. " signals.set(); // This needs indentation.\n"
  1814. "}");
  1815. }
  1816. TEST_F(FormatTest, SeparatesLogicalBlocks) {
  1817. EXPECT_EQ("class A {\n"
  1818. "public:\n"
  1819. " void f();\n"
  1820. "\n"
  1821. "private:\n"
  1822. " void g() {}\n"
  1823. " // test\n"
  1824. "protected:\n"
  1825. " int h;\n"
  1826. "};",
  1827. format("class A {\n"
  1828. "public:\n"
  1829. "void f();\n"
  1830. "private:\n"
  1831. "void g() {}\n"
  1832. "// test\n"
  1833. "protected:\n"
  1834. "int h;\n"
  1835. "};"));
  1836. EXPECT_EQ("class A {\n"
  1837. "protected:\n"
  1838. "public:\n"
  1839. " void f();\n"
  1840. "};",
  1841. format("class A {\n"
  1842. "protected:\n"
  1843. "\n"
  1844. "public:\n"
  1845. "\n"
  1846. " void f();\n"
  1847. "};"));
  1848. // Even ensure proper spacing inside macros.
  1849. EXPECT_EQ("#define B \\\n"
  1850. " class A { \\\n"
  1851. " protected: \\\n"
  1852. " public: \\\n"
  1853. " void f(); \\\n"
  1854. " };",
  1855. format("#define B \\\n"
  1856. " class A { \\\n"
  1857. " protected: \\\n"
  1858. " \\\n"
  1859. " public: \\\n"
  1860. " \\\n"
  1861. " void f(); \\\n"
  1862. " };",
  1863. getGoogleStyle()));
  1864. // But don't remove empty lines after macros ending in access specifiers.
  1865. EXPECT_EQ("#define A private:\n"
  1866. "\n"
  1867. "int i;",
  1868. format("#define A private:\n"
  1869. "\n"
  1870. "int i;"));
  1871. }
  1872. TEST_F(FormatTest, FormatsClasses) {
  1873. verifyFormat("class A : public B {};");
  1874. verifyFormat("class A : public ::B {};");
  1875. verifyFormat(
  1876. "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1877. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1878. verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
  1879. " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1880. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1881. verifyFormat(
  1882. "class A : public B, public C, public D, public E, public F {};");
  1883. verifyFormat("class AAAAAAAAAAAA : public B,\n"
  1884. " public C,\n"
  1885. " public D,\n"
  1886. " public E,\n"
  1887. " public F,\n"
  1888. " public G {};");
  1889. verifyFormat("class\n"
  1890. " ReallyReallyLongClassName {\n"
  1891. " int i;\n"
  1892. "};",
  1893. getLLVMStyleWithColumns(32));
  1894. verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
  1895. " aaaaaaaaaaaaaaaa> {};");
  1896. verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
  1897. " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
  1898. " aaaaaaaaaaaaaaaaaaaaaa> {};");
  1899. verifyFormat("template <class R, class C>\n"
  1900. "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
  1901. " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
  1902. verifyFormat("class ::A::B {};");
  1903. }
  1904. TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
  1905. verifyFormat("class A {\n} a, b;");
  1906. verifyFormat("struct A {\n} a, b;");
  1907. verifyFormat("union A {\n} a;");
  1908. }
  1909. TEST_F(FormatTest, FormatsEnum) {
  1910. verifyFormat("enum {\n"
  1911. " Zero,\n"
  1912. " One = 1,\n"
  1913. " Two = One + 1,\n"
  1914. " Three = (One + Two),\n"
  1915. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1916. " Five = (One, Two, Three, Four, 5)\n"
  1917. "};");
  1918. verifyGoogleFormat("enum {\n"
  1919. " Zero,\n"
  1920. " One = 1,\n"
  1921. " Two = One + 1,\n"
  1922. " Three = (One + Two),\n"
  1923. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1924. " Five = (One, Two, Three, Four, 5)\n"
  1925. "};");
  1926. verifyFormat("enum Enum {};");
  1927. verifyFormat("enum {};");
  1928. verifyFormat("enum X E {} d;");
  1929. verifyFormat("enum __attribute__((...)) E {} d;");
  1930. verifyFormat("enum __declspec__((...)) E {} d;");
  1931. verifyFormat("enum {\n"
  1932. " Bar = Foo<int, int>::value\n"
  1933. "};",
  1934. getLLVMStyleWithColumns(30));
  1935. verifyFormat("enum ShortEnum { A, B, C };");
  1936. verifyGoogleFormat("enum ShortEnum { A, B, C };");
  1937. EXPECT_EQ("enum KeepEmptyLines {\n"
  1938. " ONE,\n"
  1939. "\n"
  1940. " TWO,\n"
  1941. "\n"
  1942. " THREE\n"
  1943. "}",
  1944. format("enum KeepEmptyLines {\n"
  1945. " ONE,\n"
  1946. "\n"
  1947. " TWO,\n"
  1948. "\n"
  1949. "\n"
  1950. " THREE\n"
  1951. "}"));
  1952. verifyFormat("enum E { // comment\n"
  1953. " ONE,\n"
  1954. " TWO\n"
  1955. "};\n"
  1956. "int i;");
  1957. // Not enums.
  1958. verifyFormat("enum X f() {\n"
  1959. " a();\n"
  1960. " return 42;\n"
  1961. "}");
  1962. verifyFormat("enum X Type::f() {\n"
  1963. " a();\n"
  1964. " return 42;\n"
  1965. "}");
  1966. verifyFormat("enum ::X f() {\n"
  1967. " a();\n"
  1968. " return 42;\n"
  1969. "}");
  1970. verifyFormat("enum ns::X f() {\n"
  1971. " a();\n"
  1972. " return 42;\n"
  1973. "}");
  1974. }
  1975. TEST_F(FormatTest, FormatsEnumsWithErrors) {
  1976. verifyFormat("enum Type {\n"
  1977. " One = 0; // These semicolons should be commas.\n"
  1978. " Two = 1;\n"
  1979. "};");
  1980. verifyFormat("namespace n {\n"
  1981. "enum Type {\n"
  1982. " One,\n"
  1983. " Two, // missing };\n"
  1984. " int i;\n"
  1985. "}\n"
  1986. "void g() {}");
  1987. }
  1988. TEST_F(FormatTest, FormatsEnumStruct) {
  1989. verifyFormat("enum struct {\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 struct Enum {};");
  1998. verifyFormat("enum struct {};");
  1999. verifyFormat("enum struct X E {} d;");
  2000. verifyFormat("enum struct __attribute__((...)) E {} d;");
  2001. verifyFormat("enum struct __declspec__((...)) E {} d;");
  2002. verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
  2003. }
  2004. TEST_F(FormatTest, FormatsEnumClass) {
  2005. verifyFormat("enum class {\n"
  2006. " Zero,\n"
  2007. " One = 1,\n"
  2008. " Two = One + 1,\n"
  2009. " Three = (One + Two),\n"
  2010. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  2011. " Five = (One, Two, Three, Four, 5)\n"
  2012. "};");
  2013. verifyFormat("enum class Enum {};");
  2014. verifyFormat("enum class {};");
  2015. verifyFormat("enum class X E {} d;");
  2016. verifyFormat("enum class __attribute__((...)) E {} d;");
  2017. verifyFormat("enum class __declspec__((...)) E {} d;");
  2018. verifyFormat("enum class X f() {\n a();\n return 42;\n}");
  2019. }
  2020. TEST_F(FormatTest, FormatsEnumTypes) {
  2021. verifyFormat("enum X : int {\n"
  2022. " A, // Force multiple lines.\n"
  2023. " B\n"
  2024. "};");
  2025. verifyFormat("enum X : int { A, B };");
  2026. verifyFormat("enum X : std::uint32_t { A, B };");
  2027. }
  2028. TEST_F(FormatTest, FormatsNSEnums) {
  2029. verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
  2030. verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
  2031. " // Information about someDecentlyLongValue.\n"
  2032. " someDecentlyLongValue,\n"
  2033. " // Information about anotherDecentlyLongValue.\n"
  2034. " anotherDecentlyLongValue,\n"
  2035. " // Information about aThirdDecentlyLongValue.\n"
  2036. " aThirdDecentlyLongValue\n"
  2037. "};");
  2038. verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
  2039. " a = 1,\n"
  2040. " b = 2,\n"
  2041. " c = 3,\n"
  2042. "};");
  2043. verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
  2044. " a = 1,\n"
  2045. " b = 2,\n"
  2046. " c = 3,\n"
  2047. "};");
  2048. verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
  2049. " a = 1,\n"
  2050. " b = 2,\n"
  2051. " c = 3,\n"
  2052. "};");
  2053. }
  2054. TEST_F(FormatTest, FormatsBitfields) {
  2055. verifyFormat("struct Bitfields {\n"
  2056. " unsigned sClass : 8;\n"
  2057. " unsigned ValueKind : 2;\n"
  2058. "};");
  2059. verifyFormat("struct A {\n"
  2060. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
  2061. " bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
  2062. "};");
  2063. verifyFormat("struct MyStruct {\n"
  2064. " uchar data;\n"
  2065. " uchar : 8;\n"
  2066. " uchar : 8;\n"
  2067. " uchar other;\n"
  2068. "};");
  2069. }
  2070. TEST_F(FormatTest, FormatsNamespaces) {
  2071. verifyFormat("namespace some_namespace {\n"
  2072. "class A {};\n"
  2073. "void f() { f(); }\n"
  2074. "}");
  2075. verifyFormat("namespace {\n"
  2076. "class A {};\n"
  2077. "void f() { f(); }\n"
  2078. "}");
  2079. verifyFormat("inline namespace X {\n"
  2080. "class A {};\n"
  2081. "void f() { f(); }\n"
  2082. "}");
  2083. verifyFormat("using namespace some_namespace;\n"
  2084. "class A {};\n"
  2085. "void f() { f(); }");
  2086. // This code is more common than we thought; if we
  2087. // layout this correctly the semicolon will go into
  2088. // its own line, which is undesirable.
  2089. verifyFormat("namespace {};");
  2090. verifyFormat("namespace {\n"
  2091. "class A {};\n"
  2092. "};");
  2093. verifyFormat("namespace {\n"
  2094. "int SomeVariable = 0; // comment\n"
  2095. "} // namespace");
  2096. EXPECT_EQ("#ifndef HEADER_GUARD\n"
  2097. "#define HEADER_GUARD\n"
  2098. "namespace my_namespace {\n"
  2099. "int i;\n"
  2100. "} // my_namespace\n"
  2101. "#endif // HEADER_GUARD",
  2102. format("#ifndef HEADER_GUARD\n"
  2103. " #define HEADER_GUARD\n"
  2104. " namespace my_namespace {\n"
  2105. "int i;\n"
  2106. "} // my_namespace\n"
  2107. "#endif // HEADER_GUARD"));
  2108. EXPECT_EQ("namespace A::B {\n"
  2109. "class C {};\n"
  2110. "}",
  2111. format("namespace A::B {\n"
  2112. "class C {};\n"
  2113. "}"));
  2114. FormatStyle Style = getLLVMStyle();
  2115. Style.NamespaceIndentation = FormatStyle::NI_All;
  2116. EXPECT_EQ("namespace out {\n"
  2117. " int i;\n"
  2118. " namespace in {\n"
  2119. " int i;\n"
  2120. " } // namespace\n"
  2121. "} // namespace",
  2122. format("namespace out {\n"
  2123. "int i;\n"
  2124. "namespace in {\n"
  2125. "int i;\n"
  2126. "} // namespace\n"
  2127. "} // namespace",
  2128. Style));
  2129. Style.NamespaceIndentation = FormatStyle::NI_Inner;
  2130. EXPECT_EQ("namespace out {\n"
  2131. "int i;\n"
  2132. "namespace in {\n"
  2133. " int i;\n"
  2134. "} // namespace\n"
  2135. "} // namespace",
  2136. format("namespace out {\n"
  2137. "int i;\n"
  2138. "namespace in {\n"
  2139. "int i;\n"
  2140. "} // namespace\n"
  2141. "} // namespace",
  2142. Style));
  2143. }
  2144. TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
  2145. TEST_F(FormatTest, FormatsInlineASM) {
  2146. verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
  2147. verifyFormat("asm(\"nop\" ::: \"memory\");");
  2148. verifyFormat(
  2149. "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
  2150. " \"cpuid\\n\\t\"\n"
  2151. " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
  2152. " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
  2153. " : \"a\"(value));");
  2154. EXPECT_EQ(
  2155. "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
  2156. " __asm {\n"
  2157. " mov edx,[that] // vtable in edx\n"
  2158. " mov eax,methodIndex\n"
  2159. " call [edx][eax*4] // stdcall\n"
  2160. " }\n"
  2161. "}",
  2162. format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
  2163. " __asm {\n"
  2164. " mov edx,[that] // vtable in edx\n"
  2165. " mov eax,methodIndex\n"
  2166. " call [edx][eax*4] // stdcall\n"
  2167. " }\n"
  2168. "}"));
  2169. EXPECT_EQ("_asm {\n"
  2170. " xor eax, eax;\n"
  2171. " cpuid;\n"
  2172. "}",
  2173. format("_asm {\n"
  2174. " xor eax, eax;\n"
  2175. " cpuid;\n"
  2176. "}"));
  2177. verifyFormat("void function() {\n"
  2178. " // comment\n"
  2179. " asm(\"\");\n"
  2180. "}");
  2181. EXPECT_EQ("__asm {\n"
  2182. "}\n"
  2183. "int i;",
  2184. format("__asm {\n"
  2185. "}\n"
  2186. "int i;"));
  2187. }
  2188. TEST_F(FormatTest, FormatTryCatch) {
  2189. verifyFormat("try {\n"
  2190. " throw a * b;\n"
  2191. "} catch (int a) {\n"
  2192. " // Do nothing.\n"
  2193. "} catch (...) {\n"
  2194. " exit(42);\n"
  2195. "}");
  2196. // Function-level try statements.
  2197. verifyFormat("int f() try { return 4; } catch (...) {\n"
  2198. " return 5;\n"
  2199. "}");
  2200. verifyFormat("class A {\n"
  2201. " int a;\n"
  2202. " A() try : a(0) {\n"
  2203. " } catch (...) {\n"
  2204. " throw;\n"
  2205. " }\n"
  2206. "};\n");
  2207. // Incomplete try-catch blocks.
  2208. verifyIncompleteFormat("try {} catch (");
  2209. }
  2210. TEST_F(FormatTest, FormatSEHTryCatch) {
  2211. verifyFormat("__try {\n"
  2212. " int a = b * c;\n"
  2213. "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
  2214. " // Do nothing.\n"
  2215. "}");
  2216. verifyFormat("__try {\n"
  2217. " int a = b * c;\n"
  2218. "} __finally {\n"
  2219. " // Do nothing.\n"
  2220. "}");
  2221. verifyFormat("DEBUG({\n"
  2222. " __try {\n"
  2223. " } __finally {\n"
  2224. " }\n"
  2225. "});\n");
  2226. }
  2227. TEST_F(FormatTest, IncompleteTryCatchBlocks) {
  2228. verifyFormat("try {\n"
  2229. " f();\n"
  2230. "} catch {\n"
  2231. " g();\n"
  2232. "}");
  2233. verifyFormat("try {\n"
  2234. " f();\n"
  2235. "} catch (A a) MACRO(x) {\n"
  2236. " g();\n"
  2237. "} catch (B b) MACRO(x) {\n"
  2238. " g();\n"
  2239. "}");
  2240. }
  2241. TEST_F(FormatTest, FormatTryCatchBraceStyles) {
  2242. FormatStyle Style = getLLVMStyle();
  2243. for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
  2244. FormatStyle::BS_WebKit}) {
  2245. Style.BreakBeforeBraces = BraceStyle;
  2246. verifyFormat("try {\n"
  2247. " // something\n"
  2248. "} catch (...) {\n"
  2249. " // something\n"
  2250. "}",
  2251. Style);
  2252. }
  2253. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  2254. verifyFormat("try {\n"
  2255. " // something\n"
  2256. "}\n"
  2257. "catch (...) {\n"
  2258. " // something\n"
  2259. "}",
  2260. Style);
  2261. verifyFormat("__try {\n"
  2262. " // something\n"
  2263. "}\n"
  2264. "__finally {\n"
  2265. " // something\n"
  2266. "}",
  2267. Style);
  2268. verifyFormat("@try {\n"
  2269. " // something\n"
  2270. "}\n"
  2271. "@finally {\n"
  2272. " // something\n"
  2273. "}",
  2274. Style);
  2275. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  2276. verifyFormat("try\n"
  2277. "{\n"
  2278. " // something\n"
  2279. "}\n"
  2280. "catch (...)\n"
  2281. "{\n"
  2282. " // something\n"
  2283. "}",
  2284. Style);
  2285. Style.BreakBeforeBraces = FormatStyle::BS_GNU;
  2286. verifyFormat("try\n"
  2287. " {\n"
  2288. " // something\n"
  2289. " }\n"
  2290. "catch (...)\n"
  2291. " {\n"
  2292. " // something\n"
  2293. " }",
  2294. Style);
  2295. Style.BreakBeforeBraces = FormatStyle::BS_Custom;
  2296. Style.BraceWrapping.BeforeCatch = true;
  2297. verifyFormat("try {\n"
  2298. " // something\n"
  2299. "}\n"
  2300. "catch (...) {\n"
  2301. " // something\n"
  2302. "}",
  2303. Style);
  2304. }
  2305. TEST_F(FormatTest, FormatObjCTryCatch) {
  2306. verifyFormat("@try {\n"
  2307. " f();\n"
  2308. "} @catch (NSException e) {\n"
  2309. " @throw;\n"
  2310. "} @finally {\n"
  2311. " exit(42);\n"
  2312. "}");
  2313. verifyFormat("DEBUG({\n"
  2314. " @try {\n"
  2315. " } @finally {\n"
  2316. " }\n"
  2317. "});\n");
  2318. }
  2319. TEST_F(FormatTest, FormatObjCAutoreleasepool) {
  2320. FormatStyle Style = getLLVMStyle();
  2321. verifyFormat("@autoreleasepool {\n"
  2322. " f();\n"
  2323. "}\n"
  2324. "@autoreleasepool {\n"
  2325. " f();\n"
  2326. "}\n",
  2327. Style);
  2328. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  2329. verifyFormat("@autoreleasepool\n"
  2330. "{\n"
  2331. " f();\n"
  2332. "}\n"
  2333. "@autoreleasepool\n"
  2334. "{\n"
  2335. " f();\n"
  2336. "}\n",
  2337. Style);
  2338. }
  2339. TEST_F(FormatTest, StaticInitializers) {
  2340. verifyFormat("static SomeClass SC = {1, 'a'};");
  2341. verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
  2342. " 100000000, "
  2343. "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
  2344. // Here, everything other than the "}" would fit on a line.
  2345. verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
  2346. " 10000000000000000000000000};");
  2347. EXPECT_EQ("S s = {a,\n"
  2348. "\n"
  2349. " b};",
  2350. format("S s = {\n"
  2351. " a,\n"
  2352. "\n"
  2353. " b\n"
  2354. "};"));
  2355. // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
  2356. // line. However, the formatting looks a bit off and this probably doesn't
  2357. // happen often in practice.
  2358. verifyFormat("static int Variable[1] = {\n"
  2359. " {1000000000000000000000000000000000000}};",
  2360. getLLVMStyleWithColumns(40));
  2361. }
  2362. TEST_F(FormatTest, DesignatedInitializers) {
  2363. verifyFormat("const struct A a = {.a = 1, .b = 2};");
  2364. verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
  2365. " .bbbbbbbbbb = 2,\n"
  2366. " .cccccccccc = 3,\n"
  2367. " .dddddddddd = 4,\n"
  2368. " .eeeeeeeeee = 5};");
  2369. verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
  2370. " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
  2371. " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
  2372. " .ccccccccccccccccccccccccccc = 3,\n"
  2373. " .ddddddddddddddddddddddddddd = 4,\n"
  2374. " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
  2375. verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
  2376. }
  2377. TEST_F(FormatTest, NestedStaticInitializers) {
  2378. verifyFormat("static A x = {{{}}};\n");
  2379. verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
  2380. " {init1, init2, init3, init4}}};",
  2381. getLLVMStyleWithColumns(50));
  2382. verifyFormat("somes Status::global_reps[3] = {\n"
  2383. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  2384. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  2385. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
  2386. getLLVMStyleWithColumns(60));
  2387. verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
  2388. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  2389. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  2390. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
  2391. verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
  2392. " {rect.fRight - rect.fLeft, rect.fBottom - "
  2393. "rect.fTop}};");
  2394. verifyFormat(
  2395. "SomeArrayOfSomeType a = {\n"
  2396. " {{1, 2, 3},\n"
  2397. " {1, 2, 3},\n"
  2398. " {111111111111111111111111111111, 222222222222222222222222222222,\n"
  2399. " 333333333333333333333333333333},\n"
  2400. " {1, 2, 3},\n"
  2401. " {1, 2, 3}}};");
  2402. verifyFormat(
  2403. "SomeArrayOfSomeType a = {\n"
  2404. " {{1, 2, 3}},\n"
  2405. " {{1, 2, 3}},\n"
  2406. " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
  2407. " 333333333333333333333333333333}},\n"
  2408. " {{1, 2, 3}},\n"
  2409. " {{1, 2, 3}}};");
  2410. verifyFormat("struct {\n"
  2411. " unsigned bit;\n"
  2412. " const char *const name;\n"
  2413. "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
  2414. " {kOsWin, \"Windows\"},\n"
  2415. " {kOsLinux, \"Linux\"},\n"
  2416. " {kOsCrOS, \"Chrome OS\"}};");
  2417. verifyFormat("struct {\n"
  2418. " unsigned bit;\n"
  2419. " const char *const name;\n"
  2420. "} kBitsToOs[] = {\n"
  2421. " {kOsMac, \"Mac\"},\n"
  2422. " {kOsWin, \"Windows\"},\n"
  2423. " {kOsLinux, \"Linux\"},\n"
  2424. " {kOsCrOS, \"Chrome OS\"},\n"
  2425. "};");
  2426. }
  2427. TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
  2428. verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2429. " \\\n"
  2430. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
  2431. }
  2432. TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
  2433. verifyFormat("virtual void write(ELFWriter *writerrr,\n"
  2434. " OwningPtr<FileOutputBuffer> &buffer) = 0;");
  2435. // Do break defaulted and deleted functions.
  2436. verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
  2437. " default;",
  2438. getLLVMStyleWithColumns(40));
  2439. verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
  2440. " delete;",
  2441. getLLVMStyleWithColumns(40));
  2442. }
  2443. TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
  2444. verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
  2445. getLLVMStyleWithColumns(40));
  2446. verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  2447. getLLVMStyleWithColumns(40));
  2448. EXPECT_EQ("#define Q \\\n"
  2449. " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n"
  2450. " \"aaaaaaaa.cpp\"",
  2451. format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  2452. getLLVMStyleWithColumns(40)));
  2453. }
  2454. TEST_F(FormatTest, UnderstandsLinePPDirective) {
  2455. EXPECT_EQ("# 123 \"A string literal\"",
  2456. format(" # 123 \"A string literal\""));
  2457. }
  2458. TEST_F(FormatTest, LayoutUnknownPPDirective) {
  2459. EXPECT_EQ("#;", format("#;"));
  2460. verifyFormat("#\n;\n;\n;");
  2461. }
  2462. TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
  2463. EXPECT_EQ("#line 42 \"test\"\n",
  2464. format("# \\\n line \\\n 42 \\\n \"test\"\n"));
  2465. EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n",
  2466. getLLVMStyleWithColumns(12)));
  2467. }
  2468. TEST_F(FormatTest, EndOfFileEndsPPDirective) {
  2469. EXPECT_EQ("#line 42 \"test\"",
  2470. format("# \\\n line \\\n 42 \\\n \"test\""));
  2471. EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B"));
  2472. }
  2473. TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
  2474. verifyFormat("#define A \\x20");
  2475. verifyFormat("#define A \\ x20");
  2476. EXPECT_EQ("#define A \\ x20", format("#define A \\ x20"));
  2477. verifyFormat("#define A ''");
  2478. verifyFormat("#define A ''qqq");
  2479. verifyFormat("#define A `qqq");
  2480. verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
  2481. EXPECT_EQ("const char *c = STRINGIFY(\n"
  2482. "\\na : b);",
  2483. format("const char * c = STRINGIFY(\n"
  2484. "\\na : b);"));
  2485. verifyFormat("a\r\\");
  2486. verifyFormat("a\v\\");
  2487. verifyFormat("a\f\\");
  2488. }
  2489. TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
  2490. verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
  2491. verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
  2492. verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
  2493. // FIXME: We never break before the macro name.
  2494. verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
  2495. verifyFormat("#define A A\n#define A A");
  2496. verifyFormat("#define A(X) A\n#define A A");
  2497. verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
  2498. verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
  2499. }
  2500. TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
  2501. EXPECT_EQ("// somecomment\n"
  2502. "#include \"a.h\"\n"
  2503. "#define A( \\\n"
  2504. " A, B)\n"
  2505. "#include \"b.h\"\n"
  2506. "// somecomment\n",
  2507. format(" // somecomment\n"
  2508. " #include \"a.h\"\n"
  2509. "#define A(A,\\\n"
  2510. " B)\n"
  2511. " #include \"b.h\"\n"
  2512. " // somecomment\n",
  2513. getLLVMStyleWithColumns(13)));
  2514. }
  2515. TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
  2516. TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
  2517. EXPECT_EQ("#define A \\\n"
  2518. " c; \\\n"
  2519. " e;\n"
  2520. "f;",
  2521. format("#define A c; e;\n"
  2522. "f;",
  2523. getLLVMStyleWithColumns(14)));
  2524. }
  2525. TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
  2526. TEST_F(FormatTest, MacroDefinitionInsideStatement) {
  2527. EXPECT_EQ("int x,\n"
  2528. "#define A\n"
  2529. " y;",
  2530. format("int x,\n#define A\ny;"));
  2531. }
  2532. TEST_F(FormatTest, HashInMacroDefinition) {
  2533. EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
  2534. verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
  2535. verifyFormat("#define A \\\n"
  2536. " { \\\n"
  2537. " f(#c); \\\n"
  2538. " }",
  2539. getLLVMStyleWithColumns(11));
  2540. verifyFormat("#define A(X) \\\n"
  2541. " void function##X()",
  2542. getLLVMStyleWithColumns(22));
  2543. verifyFormat("#define A(a, b, c) \\\n"
  2544. " void a##b##c()",
  2545. getLLVMStyleWithColumns(22));
  2546. verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
  2547. }
  2548. TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
  2549. EXPECT_EQ("#define A (x)", format("#define A (x)"));
  2550. EXPECT_EQ("#define A(x)", format("#define A(x)"));
  2551. }
  2552. TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
  2553. EXPECT_EQ("#define A b;", format("#define A \\\n"
  2554. " \\\n"
  2555. " b;",
  2556. getLLVMStyleWithColumns(25)));
  2557. EXPECT_EQ("#define A \\\n"
  2558. " \\\n"
  2559. " a; \\\n"
  2560. " b;",
  2561. format("#define A \\\n"
  2562. " \\\n"
  2563. " a; \\\n"
  2564. " b;",
  2565. getLLVMStyleWithColumns(11)));
  2566. EXPECT_EQ("#define A \\\n"
  2567. " a; \\\n"
  2568. " \\\n"
  2569. " b;",
  2570. format("#define A \\\n"
  2571. " a; \\\n"
  2572. " \\\n"
  2573. " b;",
  2574. getLLVMStyleWithColumns(11)));
  2575. }
  2576. TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
  2577. verifyIncompleteFormat("#define A :");
  2578. verifyFormat("#define SOMECASES \\\n"
  2579. " case 1: \\\n"
  2580. " case 2\n",
  2581. getLLVMStyleWithColumns(20));
  2582. verifyFormat("#define MACRO(a) \\\n"
  2583. " if (a) \\\n"
  2584. " f(); \\\n"
  2585. " else \\\n"
  2586. " g()",
  2587. getLLVMStyleWithColumns(18));
  2588. verifyFormat("#define A template <typename T>");
  2589. verifyIncompleteFormat("#define STR(x) #x\n"
  2590. "f(STR(this_is_a_string_literal{));");
  2591. verifyFormat("#pragma omp threadprivate( \\\n"
  2592. " y)), // expected-warning",
  2593. getLLVMStyleWithColumns(28));
  2594. verifyFormat("#d, = };");
  2595. verifyFormat("#if \"a");
  2596. verifyIncompleteFormat("({\n"
  2597. "#define b \\\n"
  2598. " } \\\n"
  2599. " a\n"
  2600. "a",
  2601. getLLVMStyleWithColumns(15));
  2602. verifyFormat("#define A \\\n"
  2603. " { \\\n"
  2604. " {\n"
  2605. "#define B \\\n"
  2606. " } \\\n"
  2607. " }",
  2608. getLLVMStyleWithColumns(15));
  2609. verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
  2610. verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
  2611. verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
  2612. verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}");
  2613. }
  2614. TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
  2615. verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
  2616. EXPECT_EQ("class A : public QObject {\n"
  2617. " Q_OBJECT\n"
  2618. "\n"
  2619. " A() {}\n"
  2620. "};",
  2621. format("class A : public QObject {\n"
  2622. " Q_OBJECT\n"
  2623. "\n"
  2624. " A() {\n}\n"
  2625. "} ;"));
  2626. EXPECT_EQ("MACRO\n"
  2627. "/*static*/ int i;",
  2628. format("MACRO\n"
  2629. " /*static*/ int i;"));
  2630. EXPECT_EQ("SOME_MACRO\n"
  2631. "namespace {\n"
  2632. "void f();\n"
  2633. "}",
  2634. format("SOME_MACRO\n"
  2635. " namespace {\n"
  2636. "void f( );\n"
  2637. "}"));
  2638. // Only if the identifier contains at least 5 characters.
  2639. EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
  2640. EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
  2641. // Only if everything is upper case.
  2642. EXPECT_EQ("class A : public QObject {\n"
  2643. " Q_Object A() {}\n"
  2644. "};",
  2645. format("class A : public QObject {\n"
  2646. " Q_Object\n"
  2647. " A() {\n}\n"
  2648. "} ;"));
  2649. // Only if the next line can actually start an unwrapped line.
  2650. EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
  2651. format("SOME_WEIRD_LOG_MACRO\n"
  2652. "<< SomeThing;"));
  2653. verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
  2654. "(n, buffers))\n",
  2655. getChromiumStyle(FormatStyle::LK_Cpp));
  2656. }
  2657. TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
  2658. EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  2659. "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  2660. "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  2661. "class X {};\n"
  2662. "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  2663. "int *createScopDetectionPass() { return 0; }",
  2664. format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  2665. " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  2666. " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  2667. " class X {};\n"
  2668. " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  2669. " int *createScopDetectionPass() { return 0; }"));
  2670. // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
  2671. // braces, so that inner block is indented one level more.
  2672. EXPECT_EQ("int q() {\n"
  2673. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  2674. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  2675. " IPC_END_MESSAGE_MAP()\n"
  2676. "}",
  2677. format("int q() {\n"
  2678. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  2679. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  2680. " IPC_END_MESSAGE_MAP()\n"
  2681. "}"));
  2682. // Same inside macros.
  2683. EXPECT_EQ("#define LIST(L) \\\n"
  2684. " L(A) \\\n"
  2685. " L(B) \\\n"
  2686. " L(C)",
  2687. format("#define LIST(L) \\\n"
  2688. " L(A) \\\n"
  2689. " L(B) \\\n"
  2690. " L(C)",
  2691. getGoogleStyle()));
  2692. // These must not be recognized as macros.
  2693. EXPECT_EQ("int q() {\n"
  2694. " f(x);\n"
  2695. " f(x) {}\n"
  2696. " f(x)->g();\n"
  2697. " f(x)->*g();\n"
  2698. " f(x).g();\n"
  2699. " f(x) = x;\n"
  2700. " f(x) += x;\n"
  2701. " f(x) -= x;\n"
  2702. " f(x) *= x;\n"
  2703. " f(x) /= x;\n"
  2704. " f(x) %= x;\n"
  2705. " f(x) &= x;\n"
  2706. " f(x) |= x;\n"
  2707. " f(x) ^= x;\n"
  2708. " f(x) >>= x;\n"
  2709. " f(x) <<= x;\n"
  2710. " f(x)[y].z();\n"
  2711. " LOG(INFO) << x;\n"
  2712. " ifstream(x) >> x;\n"
  2713. "}\n",
  2714. format("int q() {\n"
  2715. " f(x)\n;\n"
  2716. " f(x)\n {}\n"
  2717. " f(x)\n->g();\n"
  2718. " f(x)\n->*g();\n"
  2719. " f(x)\n.g();\n"
  2720. " f(x)\n = x;\n"
  2721. " f(x)\n += x;\n"
  2722. " f(x)\n -= x;\n"
  2723. " f(x)\n *= x;\n"
  2724. " f(x)\n /= x;\n"
  2725. " f(x)\n %= x;\n"
  2726. " f(x)\n &= x;\n"
  2727. " f(x)\n |= x;\n"
  2728. " f(x)\n ^= x;\n"
  2729. " f(x)\n >>= x;\n"
  2730. " f(x)\n <<= x;\n"
  2731. " f(x)\n[y].z();\n"
  2732. " LOG(INFO)\n << x;\n"
  2733. " ifstream(x)\n >> x;\n"
  2734. "}\n"));
  2735. EXPECT_EQ("int q() {\n"
  2736. " F(x)\n"
  2737. " if (1) {\n"
  2738. " }\n"
  2739. " F(x)\n"
  2740. " while (1) {\n"
  2741. " }\n"
  2742. " F(x)\n"
  2743. " G(x);\n"
  2744. " F(x)\n"
  2745. " try {\n"
  2746. " Q();\n"
  2747. " } catch (...) {\n"
  2748. " }\n"
  2749. "}\n",
  2750. format("int q() {\n"
  2751. "F(x)\n"
  2752. "if (1) {}\n"
  2753. "F(x)\n"
  2754. "while (1) {}\n"
  2755. "F(x)\n"
  2756. "G(x);\n"
  2757. "F(x)\n"
  2758. "try { Q(); } catch (...) {}\n"
  2759. "}\n"));
  2760. EXPECT_EQ("class A {\n"
  2761. " A() : t(0) {}\n"
  2762. " A(int i) noexcept() : {}\n"
  2763. " A(X x)\n" // FIXME: function-level try blocks are broken.
  2764. " try : t(0) {\n"
  2765. " } catch (...) {\n"
  2766. " }\n"
  2767. "};",
  2768. format("class A {\n"
  2769. " A()\n : t(0) {}\n"
  2770. " A(int i)\n noexcept() : {}\n"
  2771. " A(X x)\n"
  2772. " try : t(0) {} catch (...) {}\n"
  2773. "};"));
  2774. EXPECT_EQ("class SomeClass {\n"
  2775. "public:\n"
  2776. " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2777. "};",
  2778. format("class SomeClass {\n"
  2779. "public:\n"
  2780. " SomeClass()\n"
  2781. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2782. "};"));
  2783. EXPECT_EQ("class SomeClass {\n"
  2784. "public:\n"
  2785. " SomeClass()\n"
  2786. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2787. "};",
  2788. format("class SomeClass {\n"
  2789. "public:\n"
  2790. " SomeClass()\n"
  2791. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2792. "};",
  2793. getLLVMStyleWithColumns(40)));
  2794. verifyFormat("MACRO(>)");
  2795. }
  2796. TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
  2797. verifyFormat("#define A \\\n"
  2798. " f({ \\\n"
  2799. " g(); \\\n"
  2800. " });",
  2801. getLLVMStyleWithColumns(11));
  2802. }
  2803. TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
  2804. EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
  2805. }
  2806. TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
  2807. verifyFormat("{\n { a #c; }\n}");
  2808. }
  2809. TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
  2810. EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
  2811. format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
  2812. EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
  2813. format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
  2814. }
  2815. TEST_F(FormatTest, EscapedNewlines) {
  2816. EXPECT_EQ(
  2817. "#define A \\\n int i; \\\n int j;",
  2818. format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
  2819. EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
  2820. EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
  2821. EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/"));
  2822. EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
  2823. }
  2824. TEST_F(FormatTest, DontCrashOnBlockComments) {
  2825. EXPECT_EQ(
  2826. "int xxxxxxxxx; /* "
  2827. "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
  2828. "zzzzzz\n"
  2829. "0*/",
  2830. format("int xxxxxxxxx; /* "
  2831. "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
  2832. "0*/"));
  2833. }
  2834. TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
  2835. verifyFormat("#define A \\\n"
  2836. " int v( \\\n"
  2837. " a); \\\n"
  2838. " int i;",
  2839. getLLVMStyleWithColumns(11));
  2840. }
  2841. TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
  2842. EXPECT_EQ(
  2843. "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2844. " \\\n"
  2845. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2846. "\n"
  2847. "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2848. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
  2849. format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2850. "\\\n"
  2851. "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2852. " \n"
  2853. " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2854. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
  2855. }
  2856. TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
  2857. EXPECT_EQ("int\n"
  2858. "#define A\n"
  2859. " a;",
  2860. format("int\n#define A\na;"));
  2861. verifyFormat("functionCallTo(\n"
  2862. " someOtherFunction(\n"
  2863. " withSomeParameters, whichInSequence,\n"
  2864. " areLongerThanALine(andAnotherCall,\n"
  2865. "#define A B\n"
  2866. " withMoreParamters,\n"
  2867. " whichStronglyInfluenceTheLayout),\n"
  2868. " andMoreParameters),\n"
  2869. " trailing);",
  2870. getLLVMStyleWithColumns(69));
  2871. verifyFormat("Foo::Foo()\n"
  2872. "#ifdef BAR\n"
  2873. " : baz(0)\n"
  2874. "#endif\n"
  2875. "{\n"
  2876. "}");
  2877. verifyFormat("void f() {\n"
  2878. " if (true)\n"
  2879. "#ifdef A\n"
  2880. " f(42);\n"
  2881. " x();\n"
  2882. "#else\n"
  2883. " g();\n"
  2884. " x();\n"
  2885. "#endif\n"
  2886. "}");
  2887. verifyFormat("void f(param1, param2,\n"
  2888. " param3,\n"
  2889. "#ifdef A\n"
  2890. " param4(param5,\n"
  2891. "#ifdef A1\n"
  2892. " param6,\n"
  2893. "#ifdef A2\n"
  2894. " param7),\n"
  2895. "#else\n"
  2896. " param8),\n"
  2897. " param9,\n"
  2898. "#endif\n"
  2899. " param10,\n"
  2900. "#endif\n"
  2901. " param11)\n"
  2902. "#else\n"
  2903. " param12)\n"
  2904. "#endif\n"
  2905. "{\n"
  2906. " x();\n"
  2907. "}",
  2908. getLLVMStyleWithColumns(28));
  2909. verifyFormat("#if 1\n"
  2910. "int i;");
  2911. verifyFormat("#if 1\n"
  2912. "#endif\n"
  2913. "#if 1\n"
  2914. "#else\n"
  2915. "#endif\n");
  2916. verifyFormat("DEBUG({\n"
  2917. " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2918. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  2919. "});\n"
  2920. "#if a\n"
  2921. "#else\n"
  2922. "#endif");
  2923. verifyIncompleteFormat("void f(\n"
  2924. "#if A\n"
  2925. " );\n"
  2926. "#else\n"
  2927. "#endif");
  2928. }
  2929. TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
  2930. verifyFormat("#endif\n"
  2931. "#if B");
  2932. }
  2933. TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
  2934. FormatStyle SingleLine = getLLVMStyle();
  2935. SingleLine.AllowShortIfStatementsOnASingleLine = true;
  2936. verifyFormat("#if 0\n"
  2937. "#elif 1\n"
  2938. "#endif\n"
  2939. "void foo() {\n"
  2940. " if (test) foo2();\n"
  2941. "}",
  2942. SingleLine);
  2943. }
  2944. TEST_F(FormatTest, LayoutBlockInsideParens) {
  2945. verifyFormat("functionCall({ int i; });");
  2946. verifyFormat("functionCall({\n"
  2947. " int i;\n"
  2948. " int j;\n"
  2949. "});");
  2950. verifyFormat("functionCall(\n"
  2951. " {\n"
  2952. " int i;\n"
  2953. " int j;\n"
  2954. " },\n"
  2955. " aaaa, bbbb, cccc);");
  2956. verifyFormat("functionA(functionB({\n"
  2957. " int i;\n"
  2958. " int j;\n"
  2959. " }),\n"
  2960. " aaaa, bbbb, cccc);");
  2961. verifyFormat("functionCall(\n"
  2962. " {\n"
  2963. " int i;\n"
  2964. " int j;\n"
  2965. " },\n"
  2966. " aaaa, bbbb, // comment\n"
  2967. " cccc);");
  2968. verifyFormat("functionA(functionB({\n"
  2969. " int i;\n"
  2970. " int j;\n"
  2971. " }),\n"
  2972. " aaaa, bbbb, // comment\n"
  2973. " cccc);");
  2974. verifyFormat("functionCall(aaaa, bbbb, { int i; });");
  2975. verifyFormat("functionCall(aaaa, bbbb, {\n"
  2976. " int i;\n"
  2977. " int j;\n"
  2978. "});");
  2979. verifyFormat(
  2980. "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
  2981. " {\n"
  2982. " int i; // break\n"
  2983. " },\n"
  2984. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  2985. " ccccccccccccccccc));");
  2986. verifyFormat("DEBUG({\n"
  2987. " if (a)\n"
  2988. " f();\n"
  2989. "});");
  2990. }
  2991. TEST_F(FormatTest, LayoutBlockInsideStatement) {
  2992. EXPECT_EQ("SOME_MACRO { int i; }\n"
  2993. "int i;",
  2994. format(" SOME_MACRO {int i;} int i;"));
  2995. }
  2996. TEST_F(FormatTest, LayoutNestedBlocks) {
  2997. verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
  2998. " struct s {\n"
  2999. " int i;\n"
  3000. " };\n"
  3001. " s kBitsToOs[] = {{10}};\n"
  3002. " for (int i = 0; i < 10; ++i)\n"
  3003. " return;\n"
  3004. "}");
  3005. verifyFormat("call(parameter, {\n"
  3006. " something();\n"
  3007. " // Comment using all columns.\n"
  3008. " somethingelse();\n"
  3009. "});",
  3010. getLLVMStyleWithColumns(40));
  3011. verifyFormat("DEBUG( //\n"
  3012. " { f(); }, a);");
  3013. verifyFormat("DEBUG( //\n"
  3014. " {\n"
  3015. " f(); //\n"
  3016. " },\n"
  3017. " a);");
  3018. EXPECT_EQ("call(parameter, {\n"
  3019. " something();\n"
  3020. " // Comment too\n"
  3021. " // looooooooooong.\n"
  3022. " somethingElse();\n"
  3023. "});",
  3024. format("call(parameter, {\n"
  3025. " something();\n"
  3026. " // Comment too looooooooooong.\n"
  3027. " somethingElse();\n"
  3028. "});",
  3029. getLLVMStyleWithColumns(29)));
  3030. EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });"));
  3031. EXPECT_EQ("DEBUG({ // comment\n"
  3032. " int i;\n"
  3033. "});",
  3034. format("DEBUG({ // comment\n"
  3035. "int i;\n"
  3036. "});"));
  3037. EXPECT_EQ("DEBUG({\n"
  3038. " int i;\n"
  3039. "\n"
  3040. " // comment\n"
  3041. " int j;\n"
  3042. "});",
  3043. format("DEBUG({\n"
  3044. " int i;\n"
  3045. "\n"
  3046. " // comment\n"
  3047. " int j;\n"
  3048. "});"));
  3049. verifyFormat("DEBUG({\n"
  3050. " if (a)\n"
  3051. " return;\n"
  3052. "});");
  3053. verifyGoogleFormat("DEBUG({\n"
  3054. " if (a) return;\n"
  3055. "});");
  3056. FormatStyle Style = getGoogleStyle();
  3057. Style.ColumnLimit = 45;
  3058. verifyFormat("Debug(aaaaa,\n"
  3059. " {\n"
  3060. " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
  3061. " },\n"
  3062. " a);",
  3063. Style);
  3064. verifyFormat("SomeFunction({MACRO({ return output; }), b});");
  3065. verifyNoCrash("^{v^{a}}");
  3066. }
  3067. TEST_F(FormatTest, FormatNestedBlocksInMacros) {
  3068. EXPECT_EQ("#define MACRO() \\\n"
  3069. " Debug(aaa, /* force line break */ \\\n"
  3070. " { \\\n"
  3071. " int i; \\\n"
  3072. " int j; \\\n"
  3073. " })",
  3074. format("#define MACRO() Debug(aaa, /* force line break */ \\\n"
  3075. " { int i; int j; })",
  3076. getGoogleStyle()));
  3077. EXPECT_EQ("#define A \\\n"
  3078. " [] { \\\n"
  3079. " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
  3080. " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
  3081. " }",
  3082. format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
  3083. "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
  3084. getGoogleStyle()));
  3085. }
  3086. TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
  3087. EXPECT_EQ("{}", format("{}"));
  3088. verifyFormat("enum E {};");
  3089. verifyFormat("enum E {}");
  3090. }
  3091. TEST_F(FormatTest, FormatBeginBlockEndMacros) {
  3092. FormatStyle Style = getLLVMStyle();
  3093. Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
  3094. Style.MacroBlockEnd = "^[A-Z_]+_END$";
  3095. verifyFormat("FOO_BEGIN\n"
  3096. " FOO_ENTRY\n"
  3097. "FOO_END", Style);
  3098. verifyFormat("FOO_BEGIN\n"
  3099. " NESTED_FOO_BEGIN\n"
  3100. " NESTED_FOO_ENTRY\n"
  3101. " NESTED_FOO_END\n"
  3102. "FOO_END", Style);
  3103. verifyFormat("FOO_BEGIN(Foo, Bar)\n"
  3104. " int x;\n"
  3105. " x = 1;\n"
  3106. "FOO_END(Baz)", Style);
  3107. }
  3108. //===----------------------------------------------------------------------===//
  3109. // Line break tests.
  3110. //===----------------------------------------------------------------------===//
  3111. TEST_F(FormatTest, PreventConfusingIndents) {
  3112. verifyFormat(
  3113. "void f() {\n"
  3114. " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
  3115. " parameter, parameter, parameter)),\n"
  3116. " SecondLongCall(parameter));\n"
  3117. "}");
  3118. verifyFormat(
  3119. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3120. " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3121. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3122. " aaaaaaaaaaaaaaaaaaaaaaaa);");
  3123. verifyFormat(
  3124. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3125. " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
  3126. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  3127. " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
  3128. verifyFormat(
  3129. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  3130. " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
  3131. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
  3132. " aaaaaaaaaaaaaaaaaaaaaaaa>;");
  3133. verifyFormat("int a = bbbb && ccc && fffff(\n"
  3134. "#define A Just forcing a new line\n"
  3135. " ddd);");
  3136. }
  3137. TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
  3138. verifyFormat(
  3139. "bool aaaaaaa =\n"
  3140. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
  3141. " bbbbbbbb();");
  3142. verifyFormat(
  3143. "bool aaaaaaa =\n"
  3144. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
  3145. " bbbbbbbb();");
  3146. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  3147. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
  3148. " ccccccccc == ddddddddddd;");
  3149. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  3150. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
  3151. " ccccccccc == ddddddddddd;");
  3152. verifyFormat(
  3153. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  3154. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
  3155. " ccccccccc == ddddddddddd;");
  3156. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  3157. " aaaaaa) &&\n"
  3158. " bbbbbb && cccccc;");
  3159. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  3160. " aaaaaa) >>\n"
  3161. " bbbbbb;");
  3162. verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
  3163. " SourceMgr.getSpellingColumnNumber(\n"
  3164. " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
  3165. " 1);");
  3166. verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3167. " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
  3168. " cccccc) {\n}");
  3169. verifyFormat("b = a &&\n"
  3170. " // Comment\n"
  3171. " b.c && d;");
  3172. // If the LHS of a comparison is not a binary expression itself, the
  3173. // additional linebreak confuses many people.
  3174. verifyFormat(
  3175. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3176. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
  3177. "}");
  3178. verifyFormat(
  3179. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3180. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  3181. "}");
  3182. verifyFormat(
  3183. "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
  3184. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  3185. "}");
  3186. // Even explicit parentheses stress the precedence enough to make the
  3187. // additional break unnecessary.
  3188. verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3189. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  3190. "}");
  3191. // This cases is borderline, but with the indentation it is still readable.
  3192. verifyFormat(
  3193. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3194. " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3195. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  3196. "}",
  3197. getLLVMStyleWithColumns(75));
  3198. // If the LHS is a binary expression, we should still use the additional break
  3199. // as otherwise the formatting hides the operator precedence.
  3200. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3201. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3202. " 5) {\n"
  3203. "}");
  3204. FormatStyle OnePerLine = getLLVMStyle();
  3205. OnePerLine.BinPackParameters = false;
  3206. verifyFormat(
  3207. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3208. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3209. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
  3210. OnePerLine);
  3211. }
  3212. TEST_F(FormatTest, ExpressionIndentation) {
  3213. verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3214. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3215. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3216. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3217. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
  3218. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
  3219. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3220. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
  3221. " ccccccccccccccccccccccccccccccccccccccccc;");
  3222. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3223. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3224. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3225. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3226. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3227. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3228. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3229. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3230. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3231. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3232. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3233. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3234. verifyFormat("if () {\n"
  3235. "} else if (aaaaa &&\n"
  3236. " bbbbb > // break\n"
  3237. " ccccc) {\n"
  3238. "}");
  3239. // Presence of a trailing comment used to change indentation of b.
  3240. verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
  3241. " b;\n"
  3242. "return aaaaaaaaaaaaaaaaaaa +\n"
  3243. " b; //",
  3244. getLLVMStyleWithColumns(30));
  3245. }
  3246. TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
  3247. // Not sure what the best system is here. Like this, the LHS can be found
  3248. // immediately above an operator (everything with the same or a higher
  3249. // indent). The RHS is aligned right of the operator and so compasses
  3250. // everything until something with the same indent as the operator is found.
  3251. // FIXME: Is this a good system?
  3252. FormatStyle Style = getLLVMStyle();
  3253. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  3254. verifyFormat(
  3255. "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3256. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3257. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3258. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3259. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3260. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3261. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3262. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3263. " > ccccccccccccccccccccccccccccccccccccccccc;",
  3264. Style);
  3265. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3266. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3267. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3268. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3269. Style);
  3270. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3271. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3272. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3273. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3274. Style);
  3275. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3276. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3277. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3278. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3279. Style);
  3280. verifyFormat("if () {\n"
  3281. "} else if (aaaaa\n"
  3282. " && bbbbb // break\n"
  3283. " > ccccc) {\n"
  3284. "}",
  3285. Style);
  3286. verifyFormat("return (a)\n"
  3287. " // comment\n"
  3288. " + b;",
  3289. Style);
  3290. verifyFormat(
  3291. "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3292. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3293. " + cc;",
  3294. Style);
  3295. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3296. " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  3297. Style);
  3298. // Forced by comments.
  3299. verifyFormat(
  3300. "unsigned ContentSize =\n"
  3301. " sizeof(int16_t) // DWARF ARange version number\n"
  3302. " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
  3303. " + sizeof(int8_t) // Pointer Size (in bytes)\n"
  3304. " + sizeof(int8_t); // Segment Size (in bytes)");
  3305. verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
  3306. " == boost::fusion::at_c<1>(iiii).second;",
  3307. Style);
  3308. Style.ColumnLimit = 60;
  3309. verifyFormat("zzzzzzzzzz\n"
  3310. " = bbbbbbbbbbbbbbbbb\n"
  3311. " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3312. Style);
  3313. }
  3314. TEST_F(FormatTest, NoOperandAlignment) {
  3315. FormatStyle Style = getLLVMStyle();
  3316. Style.AlignOperands = false;
  3317. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
  3318. verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3319. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3320. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3321. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3322. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3323. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3324. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3325. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3326. " > ccccccccccccccccccccccccccccccccccccccccc;",
  3327. Style);
  3328. verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3329. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3330. " + cc;",
  3331. Style);
  3332. verifyFormat("int a = aa\n"
  3333. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3334. " * cccccccccccccccccccccccccccccccccccc;",
  3335. Style);
  3336. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  3337. verifyFormat("return (a > b\n"
  3338. " // comment1\n"
  3339. " // comment2\n"
  3340. " || c);",
  3341. Style);
  3342. }
  3343. TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
  3344. FormatStyle Style = getLLVMStyle();
  3345. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
  3346. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3347. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3348. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
  3349. Style);
  3350. }
  3351. TEST_F(FormatTest, ConstructorInitializers) {
  3352. verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
  3353. verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
  3354. getLLVMStyleWithColumns(45));
  3355. verifyFormat("Constructor()\n"
  3356. " : Inttializer(FitsOnTheLine) {}",
  3357. getLLVMStyleWithColumns(44));
  3358. verifyFormat("Constructor()\n"
  3359. " : Inttializer(FitsOnTheLine) {}",
  3360. getLLVMStyleWithColumns(43));
  3361. verifyFormat("template <typename T>\n"
  3362. "Constructor() : Initializer(FitsOnTheLine) {}",
  3363. getLLVMStyleWithColumns(45));
  3364. verifyFormat(
  3365. "SomeClass::Constructor()\n"
  3366. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  3367. verifyFormat(
  3368. "SomeClass::Constructor()\n"
  3369. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3370. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
  3371. verifyFormat(
  3372. "SomeClass::Constructor()\n"
  3373. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3374. " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  3375. verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3376. " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3377. " : aaaaaaaaaa(aaaaaa) {}");
  3378. verifyFormat("Constructor()\n"
  3379. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3380. " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3381. " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3382. " aaaaaaaaaaaaaaaaaaaaaaa() {}");
  3383. verifyFormat("Constructor()\n"
  3384. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3385. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3386. verifyFormat("Constructor(int Parameter = 0)\n"
  3387. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
  3388. " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
  3389. verifyFormat("Constructor()\n"
  3390. " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
  3391. "}",
  3392. getLLVMStyleWithColumns(60));
  3393. verifyFormat("Constructor()\n"
  3394. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3395. " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
  3396. // Here a line could be saved by splitting the second initializer onto two
  3397. // lines, but that is not desirable.
  3398. verifyFormat("Constructor()\n"
  3399. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3400. " aaaaaaaaaaa(aaaaaaaaaaa),\n"
  3401. " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3402. FormatStyle OnePerLine = getLLVMStyle();
  3403. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  3404. OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
  3405. verifyFormat("SomeClass::Constructor()\n"
  3406. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3407. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3408. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  3409. OnePerLine);
  3410. verifyFormat("SomeClass::Constructor()\n"
  3411. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
  3412. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3413. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  3414. OnePerLine);
  3415. verifyFormat("MyClass::MyClass(int var)\n"
  3416. " : some_var_(var), // 4 space indent\n"
  3417. " some_other_var_(var + 1) { // lined up\n"
  3418. "}",
  3419. OnePerLine);
  3420. verifyFormat("Constructor()\n"
  3421. " : aaaaa(aaaaaa),\n"
  3422. " aaaaa(aaaaaa),\n"
  3423. " aaaaa(aaaaaa),\n"
  3424. " aaaaa(aaaaaa),\n"
  3425. " aaaaa(aaaaaa) {}",
  3426. OnePerLine);
  3427. verifyFormat("Constructor()\n"
  3428. " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
  3429. " aaaaaaaaaaaaaaaaaaaaaa) {}",
  3430. OnePerLine);
  3431. OnePerLine.BinPackParameters = false;
  3432. verifyFormat(
  3433. "Constructor()\n"
  3434. " : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3435. " aaaaaaaaaaa().aaa(),\n"
  3436. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3437. OnePerLine);
  3438. OnePerLine.ColumnLimit = 60;
  3439. verifyFormat("Constructor()\n"
  3440. " : aaaaaaaaaaaaaaaaaaaa(a),\n"
  3441. " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
  3442. OnePerLine);
  3443. EXPECT_EQ("Constructor()\n"
  3444. " : // Comment forcing unwanted break.\n"
  3445. " aaaa(aaaa) {}",
  3446. format("Constructor() :\n"
  3447. " // Comment forcing unwanted break.\n"
  3448. " aaaa(aaaa) {}"));
  3449. }
  3450. TEST_F(FormatTest, MemoizationTests) {
  3451. // This breaks if the memoization lookup does not take \c Indent and
  3452. // \c LastSpace into account.
  3453. verifyFormat(
  3454. "extern CFRunLoopTimerRef\n"
  3455. "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
  3456. " CFTimeInterval interval, CFOptionFlags flags,\n"
  3457. " CFIndex order, CFRunLoopTimerCallBack callout,\n"
  3458. " CFRunLoopTimerContext *context) {}");
  3459. // Deep nesting somewhat works around our memoization.
  3460. verifyFormat(
  3461. "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3462. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3463. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3464. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3465. " aaaaa())))))))))))))))))))))))))))))))))))))));",
  3466. getLLVMStyleWithColumns(65));
  3467. verifyFormat(
  3468. "aaaaa(\n"
  3469. " aaaaa,\n"
  3470. " aaaaa(\n"
  3471. " aaaaa,\n"
  3472. " aaaaa(\n"
  3473. " aaaaa,\n"
  3474. " aaaaa(\n"
  3475. " aaaaa,\n"
  3476. " aaaaa(\n"
  3477. " aaaaa,\n"
  3478. " aaaaa(\n"
  3479. " aaaaa,\n"
  3480. " aaaaa(\n"
  3481. " aaaaa,\n"
  3482. " aaaaa(\n"
  3483. " aaaaa,\n"
  3484. " aaaaa(\n"
  3485. " aaaaa,\n"
  3486. " aaaaa(\n"
  3487. " aaaaa,\n"
  3488. " aaaaa(\n"
  3489. " aaaaa,\n"
  3490. " aaaaa(\n"
  3491. " aaaaa,\n"
  3492. " aaaaa))))))))))));",
  3493. getLLVMStyleWithColumns(65));
  3494. verifyFormat(
  3495. "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"
  3496. " a),\n"
  3497. " a),\n"
  3498. " a),\n"
  3499. " a),\n"
  3500. " a),\n"
  3501. " a),\n"
  3502. " a),\n"
  3503. " a),\n"
  3504. " a),\n"
  3505. " a),\n"
  3506. " a),\n"
  3507. " a),\n"
  3508. " a),\n"
  3509. " a),\n"
  3510. " a),\n"
  3511. " a),\n"
  3512. " a)",
  3513. getLLVMStyleWithColumns(65));
  3514. // This test takes VERY long when memoization is broken.
  3515. FormatStyle OnePerLine = getLLVMStyle();
  3516. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  3517. OnePerLine.BinPackParameters = false;
  3518. std::string input = "Constructor()\n"
  3519. " : aaaa(a,\n";
  3520. for (unsigned i = 0, e = 80; i != e; ++i) {
  3521. input += " a,\n";
  3522. }
  3523. input += " a) {}";
  3524. verifyFormat(input, OnePerLine);
  3525. }
  3526. TEST_F(FormatTest, BreaksAsHighAsPossible) {
  3527. verifyFormat(
  3528. "void f() {\n"
  3529. " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
  3530. " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
  3531. " f();\n"
  3532. "}");
  3533. verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
  3534. " Intervals[i - 1].getRange().getLast()) {\n}");
  3535. }
  3536. TEST_F(FormatTest, BreaksFunctionDeclarations) {
  3537. // Principially, we break function declarations in a certain order:
  3538. // 1) break amongst arguments.
  3539. verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
  3540. " Cccccccccccccc cccccccccccccc);");
  3541. verifyFormat("template <class TemplateIt>\n"
  3542. "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
  3543. " TemplateIt *stop) {}");
  3544. // 2) break after return type.
  3545. verifyFormat(
  3546. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3547. "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
  3548. getGoogleStyle());
  3549. // 3) break after (.
  3550. verifyFormat(
  3551. "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
  3552. " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
  3553. getGoogleStyle());
  3554. // 4) break before after nested name specifiers.
  3555. verifyFormat(
  3556. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3557. "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
  3558. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
  3559. getGoogleStyle());
  3560. // However, there are exceptions, if a sufficient amount of lines can be
  3561. // saved.
  3562. // FIXME: The precise cut-offs wrt. the number of saved lines might need some
  3563. // more adjusting.
  3564. verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  3565. " Cccccccccccccc cccccccccc,\n"
  3566. " Cccccccccccccc cccccccccc,\n"
  3567. " Cccccccccccccc cccccccccc,\n"
  3568. " Cccccccccccccc cccccccccc);");
  3569. verifyFormat(
  3570. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3571. "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3572. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3573. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
  3574. getGoogleStyle());
  3575. verifyFormat(
  3576. "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  3577. " Cccccccccccccc cccccccccc,\n"
  3578. " Cccccccccccccc cccccccccc,\n"
  3579. " Cccccccccccccc cccccccccc,\n"
  3580. " Cccccccccccccc cccccccccc,\n"
  3581. " Cccccccccccccc cccccccccc,\n"
  3582. " Cccccccccccccc cccccccccc);");
  3583. verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3584. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3585. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3586. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3587. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
  3588. // Break after multi-line parameters.
  3589. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3590. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3591. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3592. " bbbb bbbb);");
  3593. verifyFormat("void SomeLoooooooooooongFunction(\n"
  3594. " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  3595. " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3596. " int bbbbbbbbbbbbb);");
  3597. // Treat overloaded operators like other functions.
  3598. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3599. "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
  3600. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3601. "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
  3602. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3603. "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
  3604. verifyGoogleFormat(
  3605. "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
  3606. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  3607. verifyGoogleFormat(
  3608. "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
  3609. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  3610. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3611. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  3612. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
  3613. "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  3614. verifyGoogleFormat(
  3615. "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
  3616. "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3617. " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
  3618. verifyGoogleFormat(
  3619. "template <typename T>\n"
  3620. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3621. "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
  3622. " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
  3623. FormatStyle Style = getLLVMStyle();
  3624. Style.PointerAlignment = FormatStyle::PAS_Left;
  3625. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3626. " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
  3627. Style);
  3628. verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
  3629. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3630. Style);
  3631. }
  3632. TEST_F(FormatTest, TrailingReturnType) {
  3633. verifyFormat("auto foo() -> int;\n");
  3634. verifyFormat("struct S {\n"
  3635. " auto bar() const -> int;\n"
  3636. "};");
  3637. verifyFormat("template <size_t Order, typename T>\n"
  3638. "auto load_img(const std::string &filename)\n"
  3639. " -> alias::tensor<Order, T, mem::tag::cpu> {}");
  3640. verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
  3641. " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
  3642. verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
  3643. verifyFormat("template <typename T>\n"
  3644. "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
  3645. " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
  3646. // Not trailing return types.
  3647. verifyFormat("void f() { auto a = b->c(); }");
  3648. }
  3649. TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
  3650. // Avoid breaking before trailing 'const' or other trailing annotations, if
  3651. // they are not function-like.
  3652. FormatStyle Style = getGoogleStyle();
  3653. Style.ColumnLimit = 47;
  3654. verifyFormat("void someLongFunction(\n"
  3655. " int someLoooooooooooooongParameter) const {\n}",
  3656. getLLVMStyleWithColumns(47));
  3657. verifyFormat("LoooooongReturnType\n"
  3658. "someLoooooooongFunction() const {}",
  3659. getLLVMStyleWithColumns(47));
  3660. verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
  3661. " const {}",
  3662. Style);
  3663. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3664. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
  3665. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3666. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
  3667. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3668. " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
  3669. verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
  3670. " aaaaaaaaaaa aaaaa) const override;");
  3671. verifyGoogleFormat(
  3672. "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3673. " const override;");
  3674. // Even if the first parameter has to be wrapped.
  3675. verifyFormat("void someLongFunction(\n"
  3676. " int someLongParameter) const {}",
  3677. getLLVMStyleWithColumns(46));
  3678. verifyFormat("void someLongFunction(\n"
  3679. " int someLongParameter) const {}",
  3680. Style);
  3681. verifyFormat("void someLongFunction(\n"
  3682. " int someLongParameter) override {}",
  3683. Style);
  3684. verifyFormat("void someLongFunction(\n"
  3685. " int someLongParameter) OVERRIDE {}",
  3686. Style);
  3687. verifyFormat("void someLongFunction(\n"
  3688. " int someLongParameter) final {}",
  3689. Style);
  3690. verifyFormat("void someLongFunction(\n"
  3691. " int someLongParameter) FINAL {}",
  3692. Style);
  3693. verifyFormat("void someLongFunction(\n"
  3694. " int parameter) const override {}",
  3695. Style);
  3696. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  3697. verifyFormat("void someLongFunction(\n"
  3698. " int someLongParameter) const\n"
  3699. "{\n"
  3700. "}",
  3701. Style);
  3702. // Unless these are unknown annotations.
  3703. verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
  3704. " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3705. " LONG_AND_UGLY_ANNOTATION;");
  3706. // Breaking before function-like trailing annotations is fine to keep them
  3707. // close to their arguments.
  3708. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3709. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  3710. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  3711. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  3712. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  3713. " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
  3714. verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
  3715. " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
  3716. verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
  3717. verifyFormat(
  3718. "void aaaaaaaaaaaaaaaaaa()\n"
  3719. " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3720. " aaaaaaaaaaaaaaaaaaaaaaaaa));");
  3721. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3722. " __attribute__((unused));");
  3723. verifyGoogleFormat(
  3724. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3725. " GUARDED_BY(aaaaaaaaaaaa);");
  3726. verifyGoogleFormat(
  3727. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3728. " GUARDED_BY(aaaaaaaaaaaa);");
  3729. verifyGoogleFormat(
  3730. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
  3731. " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3732. verifyGoogleFormat(
  3733. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
  3734. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3735. }
  3736. TEST_F(FormatTest, FunctionAnnotations) {
  3737. verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
  3738. "int OldFunction(const string &parameter) {}");
  3739. verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
  3740. "string OldFunction(const string &parameter) {}");
  3741. verifyFormat("template <typename T>\n"
  3742. "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
  3743. "string OldFunction(const string &parameter) {}");
  3744. // Not function annotations.
  3745. verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3746. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
  3747. verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
  3748. " ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
  3749. verifyFormat("MACRO(abc).function() // wrap\n"
  3750. " << abc;");
  3751. verifyFormat("MACRO(abc)->function() // wrap\n"
  3752. " << abc;");
  3753. verifyFormat("MACRO(abc)::function() // wrap\n"
  3754. " << abc;");
  3755. }
  3756. TEST_F(FormatTest, BreaksDesireably) {
  3757. verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  3758. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  3759. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
  3760. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3761. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  3762. "}");
  3763. verifyFormat(
  3764. "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3765. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3766. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3767. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3768. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3769. verifyFormat(
  3770. "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3771. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  3772. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3773. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
  3774. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3775. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3776. verifyFormat(
  3777. "void f() {\n"
  3778. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
  3779. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3780. "}");
  3781. verifyFormat(
  3782. "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3783. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3784. verifyFormat(
  3785. "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3786. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3787. verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3788. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3789. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3790. // Indent consistently independent of call expression and unary operator.
  3791. verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3792. " dddddddddddddddddddddddddddddd));");
  3793. verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3794. " dddddddddddddddddddddddddddddd));");
  3795. verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
  3796. " dddddddddddddddddddddddddddddd));");
  3797. // This test case breaks on an incorrect memoization, i.e. an optimization not
  3798. // taking into account the StopAt value.
  3799. verifyFormat(
  3800. "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3801. " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3802. " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3803. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3804. verifyFormat("{\n {\n {\n"
  3805. " Annotation.SpaceRequiredBefore =\n"
  3806. " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
  3807. " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
  3808. " }\n }\n}");
  3809. // Break on an outer level if there was a break on an inner level.
  3810. EXPECT_EQ("f(g(h(a, // comment\n"
  3811. " b, c),\n"
  3812. " d, e),\n"
  3813. " x, y);",
  3814. format("f(g(h(a, // comment\n"
  3815. " b, c), d, e), x, y);"));
  3816. // Prefer breaking similar line breaks.
  3817. verifyFormat(
  3818. "const int kTrackingOptions = NSTrackingMouseMoved |\n"
  3819. " NSTrackingMouseEnteredAndExited |\n"
  3820. " NSTrackingActiveAlways;");
  3821. }
  3822. TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
  3823. FormatStyle NoBinPacking = getGoogleStyle();
  3824. NoBinPacking.BinPackParameters = false;
  3825. NoBinPacking.BinPackArguments = true;
  3826. verifyFormat("void f() {\n"
  3827. " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
  3828. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3829. "}",
  3830. NoBinPacking);
  3831. verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
  3832. " int aaaaaaaaaaaaaaaaaaaa,\n"
  3833. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3834. NoBinPacking);
  3835. NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
  3836. verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3837. " vector<int> bbbbbbbbbbbbbbb);",
  3838. NoBinPacking);
  3839. // FIXME: This behavior difference is probably not wanted. However, currently
  3840. // we cannot distinguish BreakBeforeParameter being set because of the wrapped
  3841. // template arguments from BreakBeforeParameter being set because of the
  3842. // one-per-line formatting.
  3843. verifyFormat(
  3844. "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3845. " aaaaaaaaaa> aaaaaaaaaa);",
  3846. NoBinPacking);
  3847. verifyFormat(
  3848. "void fffffffffff(\n"
  3849. " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
  3850. " aaaaaaaaaa);");
  3851. }
  3852. TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
  3853. FormatStyle NoBinPacking = getGoogleStyle();
  3854. NoBinPacking.BinPackParameters = false;
  3855. NoBinPacking.BinPackArguments = false;
  3856. verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
  3857. " aaaaaaaaaaaaaaaaaaaa,\n"
  3858. " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
  3859. NoBinPacking);
  3860. verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
  3861. " aaaaaaaaaaaaa,\n"
  3862. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
  3863. NoBinPacking);
  3864. verifyFormat(
  3865. "aaaaaaaa(aaaaaaaaaaaaa,\n"
  3866. " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3867. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  3868. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3869. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
  3870. NoBinPacking);
  3871. verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  3872. " .aaaaaaaaaaaaaaaaaa();",
  3873. NoBinPacking);
  3874. verifyFormat("void f() {\n"
  3875. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3876. " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
  3877. "}",
  3878. NoBinPacking);
  3879. verifyFormat(
  3880. "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3881. " aaaaaaaaaaaa,\n"
  3882. " aaaaaaaaaaaa);",
  3883. NoBinPacking);
  3884. verifyFormat(
  3885. "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
  3886. " ddddddddddddddddddddddddddddd),\n"
  3887. " test);",
  3888. NoBinPacking);
  3889. verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3890. " aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3891. " aaaaaaaaaaaaaaaaaaaaaaa>\n"
  3892. " aaaaaaaaaaaaaaaaaa;",
  3893. NoBinPacking);
  3894. verifyFormat("a(\"a\"\n"
  3895. " \"a\",\n"
  3896. " a);");
  3897. NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
  3898. verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
  3899. " aaaaaaaaa,\n"
  3900. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3901. NoBinPacking);
  3902. verifyFormat(
  3903. "void f() {\n"
  3904. " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  3905. " .aaaaaaa();\n"
  3906. "}",
  3907. NoBinPacking);
  3908. verifyFormat(
  3909. "template <class SomeType, class SomeOtherType>\n"
  3910. "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
  3911. NoBinPacking);
  3912. }
  3913. TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
  3914. FormatStyle Style = getLLVMStyleWithColumns(15);
  3915. Style.ExperimentalAutoDetectBinPacking = true;
  3916. EXPECT_EQ("aaa(aaaa,\n"
  3917. " aaaa,\n"
  3918. " aaaa);\n"
  3919. "aaa(aaaa,\n"
  3920. " aaaa,\n"
  3921. " aaaa);",
  3922. format("aaa(aaaa,\n" // one-per-line
  3923. " aaaa,\n"
  3924. " aaaa );\n"
  3925. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  3926. Style));
  3927. EXPECT_EQ("aaa(aaaa, aaaa,\n"
  3928. " aaaa);\n"
  3929. "aaa(aaaa, aaaa,\n"
  3930. " aaaa);",
  3931. format("aaa(aaaa, aaaa,\n" // bin-packed
  3932. " aaaa );\n"
  3933. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  3934. Style));
  3935. }
  3936. TEST_F(FormatTest, FormatsBuilderPattern) {
  3937. verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
  3938. " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
  3939. " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
  3940. " .StartsWith(\".init\", ORDER_INIT)\n"
  3941. " .StartsWith(\".fini\", ORDER_FINI)\n"
  3942. " .StartsWith(\".hash\", ORDER_HASH)\n"
  3943. " .Default(ORDER_TEXT);\n");
  3944. verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
  3945. " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
  3946. verifyFormat(
  3947. "aaaaaaa->aaaaaaa\n"
  3948. " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3949. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3950. " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
  3951. verifyFormat(
  3952. "aaaaaaa->aaaaaaa\n"
  3953. " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3954. " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
  3955. verifyFormat(
  3956. "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
  3957. " aaaaaaaaaaaaaa);");
  3958. verifyFormat(
  3959. "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
  3960. " aaaaaa->aaaaaaaaaaaa()\n"
  3961. " ->aaaaaaaaaaaaaaaa(\n"
  3962. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3963. " ->aaaaaaaaaaaaaaaaa();");
  3964. verifyGoogleFormat(
  3965. "void f() {\n"
  3966. " someo->Add((new util::filetools::Handler(dir))\n"
  3967. " ->OnEvent1(NewPermanentCallback(\n"
  3968. " this, &HandlerHolderClass::EventHandlerCBA))\n"
  3969. " ->OnEvent2(NewPermanentCallback(\n"
  3970. " this, &HandlerHolderClass::EventHandlerCBB))\n"
  3971. " ->OnEvent3(NewPermanentCallback(\n"
  3972. " this, &HandlerHolderClass::EventHandlerCBC))\n"
  3973. " ->OnEvent5(NewPermanentCallback(\n"
  3974. " this, &HandlerHolderClass::EventHandlerCBD))\n"
  3975. " ->OnEvent6(NewPermanentCallback(\n"
  3976. " this, &HandlerHolderClass::EventHandlerCBE)));\n"
  3977. "}");
  3978. verifyFormat(
  3979. "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
  3980. verifyFormat("aaaaaaaaaaaaaaa()\n"
  3981. " .aaaaaaaaaaaaaaa()\n"
  3982. " .aaaaaaaaaaaaaaa()\n"
  3983. " .aaaaaaaaaaaaaaa()\n"
  3984. " .aaaaaaaaaaaaaaa();");
  3985. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3986. " .aaaaaaaaaaaaaaa()\n"
  3987. " .aaaaaaaaaaaaaaa()\n"
  3988. " .aaaaaaaaaaaaaaa();");
  3989. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3990. " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3991. " .aaaaaaaaaaaaaaa();");
  3992. verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3993. " ->aaaaaaaaaaaaaae(0)\n"
  3994. " ->aaaaaaaaaaaaaaa();");
  3995. // Don't linewrap after very short segments.
  3996. verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3997. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3998. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3999. verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  4000. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  4001. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4002. verifyFormat("aaa()\n"
  4003. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  4004. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  4005. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4006. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  4007. " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  4008. " .has<bbbbbbbbbbbbbbbbbbbbb>();");
  4009. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  4010. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  4011. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
  4012. // Prefer not to break after empty parentheses.
  4013. verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
  4014. " First->LastNewlineOffset);");
  4015. // Prefer not to create "hanging" indents.
  4016. verifyFormat(
  4017. "return !soooooooooooooome_map\n"
  4018. " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4019. " .second;");
  4020. verifyFormat(
  4021. "return aaaaaaaaaaaaaaaa\n"
  4022. " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
  4023. " .aaaa(aaaaaaaaaaaaaa);");
  4024. // No hanging indent here.
  4025. verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
  4026. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4027. verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
  4028. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4029. verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
  4030. " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4031. getLLVMStyleWithColumns(60));
  4032. verifyFormat("aaaaaaaaaaaaaaaaaa\n"
  4033. " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
  4034. " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4035. getLLVMStyleWithColumns(59));
  4036. verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4037. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4038. " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4039. }
  4040. TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
  4041. verifyFormat(
  4042. "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  4043. " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
  4044. verifyFormat(
  4045. "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
  4046. " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
  4047. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  4048. " ccccccccccccccccccccccccc) {\n}");
  4049. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
  4050. " ccccccccccccccccccccccccc) {\n}");
  4051. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  4052. " ccccccccccccccccccccccccc) {\n}");
  4053. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
  4054. " ccccccccccccccccccccccccc) {\n}");
  4055. verifyFormat(
  4056. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
  4057. " ccccccccccccccccccccccccc) {\n}");
  4058. verifyFormat(
  4059. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
  4060. " ccccccccccccccccccccccccc) {\n}");
  4061. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
  4062. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
  4063. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
  4064. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  4065. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
  4066. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
  4067. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
  4068. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  4069. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
  4070. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
  4071. " aaaaaaaaaaaaaaa != aa) {\n}");
  4072. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
  4073. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
  4074. " aaaaaaaaaaaaaaa != aa) {\n}");
  4075. }
  4076. TEST_F(FormatTest, BreaksAfterAssignments) {
  4077. verifyFormat(
  4078. "unsigned Cost =\n"
  4079. " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
  4080. " SI->getPointerAddressSpaceee());\n");
  4081. verifyFormat(
  4082. "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
  4083. " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
  4084. verifyFormat(
  4085. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
  4086. " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
  4087. verifyFormat("unsigned OriginalStartColumn =\n"
  4088. " SourceMgr.getSpellingColumnNumber(\n"
  4089. " Current.FormatTok.getStartOfNonWhitespace()) -\n"
  4090. " 1;");
  4091. }
  4092. TEST_F(FormatTest, AlignsAfterAssignments) {
  4093. verifyFormat(
  4094. "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4095. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  4096. verifyFormat(
  4097. "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4098. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  4099. verifyFormat(
  4100. "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4101. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  4102. verifyFormat(
  4103. "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4104. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  4105. verifyFormat(
  4106. "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4107. " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4108. " aaaaaaaaaaaaaaaaaaaaaaaa;");
  4109. }
  4110. TEST_F(FormatTest, AlignsAfterReturn) {
  4111. verifyFormat(
  4112. "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4113. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  4114. verifyFormat(
  4115. "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4116. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  4117. verifyFormat(
  4118. "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  4119. " aaaaaaaaaaaaaaaaaaaaaa();");
  4120. verifyFormat(
  4121. "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  4122. " aaaaaaaaaaaaaaaaaaaaaa());");
  4123. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4124. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4125. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4126. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
  4127. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4128. verifyFormat("return\n"
  4129. " // true if code is one of a or b.\n"
  4130. " code == a || code == b;");
  4131. }
  4132. TEST_F(FormatTest, AlignsAfterOpenBracket) {
  4133. verifyFormat(
  4134. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
  4135. " aaaaaaaaa aaaaaaa) {}");
  4136. verifyFormat(
  4137. "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
  4138. " aaaaaaaaaaa aaaaaaaaa);");
  4139. verifyFormat(
  4140. "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
  4141. " aaaaaaaaaaaaaaaaaaaaa));");
  4142. FormatStyle Style = getLLVMStyle();
  4143. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  4144. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4145. " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
  4146. Style);
  4147. verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
  4148. " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
  4149. Style);
  4150. verifyFormat("SomeLongVariableName->someFunction(\n"
  4151. " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
  4152. Style);
  4153. verifyFormat(
  4154. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
  4155. " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  4156. Style);
  4157. verifyFormat(
  4158. "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
  4159. " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4160. Style);
  4161. verifyFormat(
  4162. "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
  4163. " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
  4164. Style);
  4165. verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
  4166. " ccccccc(aaaaaaaaaaaaaaaaa, //\n"
  4167. " b));",
  4168. Style);
  4169. Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
  4170. Style.BinPackArguments = false;
  4171. Style.BinPackParameters = false;
  4172. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4173. " aaaaaaaaaaa aaaaaaaa,\n"
  4174. " aaaaaaaaa aaaaaaa,\n"
  4175. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  4176. Style);
  4177. verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
  4178. " aaaaaaaaaaa aaaaaaaaa,\n"
  4179. " aaaaaaaaaaa aaaaaaaaa,\n"
  4180. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4181. Style);
  4182. verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
  4183. " aaaaaaaaaaaaaaa,\n"
  4184. " aaaaaaaaaaaaaaaaaaaaa,\n"
  4185. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
  4186. Style);
  4187. verifyFormat(
  4188. "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
  4189. " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
  4190. Style);
  4191. verifyFormat(
  4192. "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
  4193. " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
  4194. Style);
  4195. verifyFormat(
  4196. "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4197. " aaaaaaaaaaaaaaaaaaaaa(\n"
  4198. " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
  4199. " aaaaaaaaaaaaaaaa);",
  4200. Style);
  4201. verifyFormat(
  4202. "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4203. " aaaaaaaaaaaaaaaaaaaaa(\n"
  4204. " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
  4205. " aaaaaaaaaaaaaaaa);",
  4206. Style);
  4207. }
  4208. TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
  4209. FormatStyle Style = getLLVMStyleWithColumns(40);
  4210. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4211. " bbbbbbbbbbbbbbbbbbbbbb);",
  4212. Style);
  4213. Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
  4214. Style.AlignOperands = false;
  4215. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4216. " bbbbbbbbbbbbbbbbbbbbbb);",
  4217. Style);
  4218. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  4219. Style.AlignOperands = true;
  4220. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4221. " bbbbbbbbbbbbbbbbbbbbbb);",
  4222. Style);
  4223. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  4224. Style.AlignOperands = false;
  4225. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4226. " bbbbbbbbbbbbbbbbbbbbbb);",
  4227. Style);
  4228. }
  4229. TEST_F(FormatTest, BreaksConditionalExpressions) {
  4230. verifyFormat(
  4231. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4232. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4233. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4234. verifyFormat(
  4235. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4236. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4237. verifyFormat(
  4238. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
  4239. " : aaaaaaaaaaaaa);");
  4240. verifyFormat(
  4241. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4242. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4243. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4244. " aaaaaaaaaaaaa);");
  4245. verifyFormat(
  4246. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4247. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4248. " aaaaaaaaaaaaa);");
  4249. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4250. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4251. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4252. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4253. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4254. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4255. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4256. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4257. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4258. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4259. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4260. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4261. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4262. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4263. " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4264. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4265. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4266. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4267. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4268. " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4269. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  4270. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4271. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4272. " : aaaaaaaaaaaaaaaa;");
  4273. verifyFormat(
  4274. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4275. " ? aaaaaaaaaaaaaaa\n"
  4276. " : aaaaaaaaaaaaaaa;");
  4277. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  4278. " aaaaaaaaa\n"
  4279. " ? b\n"
  4280. " : c);");
  4281. verifyFormat("return aaaa == bbbb\n"
  4282. " // comment\n"
  4283. " ? aaaa\n"
  4284. " : bbbb;");
  4285. verifyFormat("unsigned Indent =\n"
  4286. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
  4287. " ? IndentForLevel[TheLine.Level]\n"
  4288. " : TheLine * 2,\n"
  4289. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  4290. getLLVMStyleWithColumns(70));
  4291. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  4292. " ? aaaaaaaaaaaaaaa\n"
  4293. " : bbbbbbbbbbbbbbb //\n"
  4294. " ? ccccccccccccccc\n"
  4295. " : ddddddddddddddd;");
  4296. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  4297. " ? aaaaaaaaaaaaaaa\n"
  4298. " : (bbbbbbbbbbbbbbb //\n"
  4299. " ? ccccccccccccccc\n"
  4300. " : ddddddddddddddd);");
  4301. verifyFormat(
  4302. "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4303. " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4304. " aaaaaaaaaaaaaaaaaaaaa +\n"
  4305. " aaaaaaaaaaaaaaaaaaaaa\n"
  4306. " : aaaaaaaaaa;");
  4307. verifyFormat(
  4308. "aaaaaa = aaaaaaaaaaaa\n"
  4309. " ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4310. " : aaaaaaaaaaaaaaaaaaaaaa\n"
  4311. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4312. FormatStyle NoBinPacking = getLLVMStyle();
  4313. NoBinPacking.BinPackArguments = false;
  4314. verifyFormat(
  4315. "void f() {\n"
  4316. " g(aaa,\n"
  4317. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  4318. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4319. " ? aaaaaaaaaaaaaaa\n"
  4320. " : aaaaaaaaaaaaaaa);\n"
  4321. "}",
  4322. NoBinPacking);
  4323. verifyFormat(
  4324. "void f() {\n"
  4325. " g(aaa,\n"
  4326. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  4327. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4328. " ?: aaaaaaaaaaaaaaa);\n"
  4329. "}",
  4330. NoBinPacking);
  4331. verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
  4332. " // comment.\n"
  4333. " ccccccccccccccccccccccccccccccccccccccc\n"
  4334. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4335. " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
  4336. // Assignments in conditional expressions. Apparently not uncommon :-(.
  4337. verifyFormat("return a != b\n"
  4338. " // comment\n"
  4339. " ? a = b\n"
  4340. " : a = b;");
  4341. verifyFormat("return a != b\n"
  4342. " // comment\n"
  4343. " ? a = a != b\n"
  4344. " // comment\n"
  4345. " ? a = b\n"
  4346. " : a\n"
  4347. " : a;\n");
  4348. verifyFormat("return a != b\n"
  4349. " // comment\n"
  4350. " ? a\n"
  4351. " : a = a != b\n"
  4352. " // comment\n"
  4353. " ? a = b\n"
  4354. " : a;");
  4355. }
  4356. TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
  4357. FormatStyle Style = getLLVMStyle();
  4358. Style.BreakBeforeTernaryOperators = false;
  4359. Style.ColumnLimit = 70;
  4360. verifyFormat(
  4361. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4362. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4363. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4364. Style);
  4365. verifyFormat(
  4366. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4367. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4368. Style);
  4369. verifyFormat(
  4370. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
  4371. " aaaaaaaaaaaaa);",
  4372. Style);
  4373. verifyFormat(
  4374. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4375. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4376. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4377. " aaaaaaaaaaaaa);",
  4378. Style);
  4379. verifyFormat(
  4380. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4381. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4382. " aaaaaaaaaaaaa);",
  4383. Style);
  4384. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4385. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4386. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  4387. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4388. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4389. Style);
  4390. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4391. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4392. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4393. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  4394. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4395. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4396. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4397. Style);
  4398. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4399. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
  4400. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4401. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4402. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4403. Style);
  4404. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4405. " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4406. " aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4407. Style);
  4408. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  4409. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4410. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4411. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4412. Style);
  4413. verifyFormat(
  4414. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4415. " aaaaaaaaaaaaaaa :\n"
  4416. " aaaaaaaaaaaaaaa;",
  4417. Style);
  4418. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  4419. " aaaaaaaaa ?\n"
  4420. " b :\n"
  4421. " c);",
  4422. Style);
  4423. verifyFormat(
  4424. "unsigned Indent =\n"
  4425. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n"
  4426. " IndentForLevel[TheLine.Level] :\n"
  4427. " TheLine * 2,\n"
  4428. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  4429. Style);
  4430. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  4431. " aaaaaaaaaaaaaaa :\n"
  4432. " bbbbbbbbbbbbbbb ? //\n"
  4433. " ccccccccccccccc :\n"
  4434. " ddddddddddddddd;",
  4435. Style);
  4436. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  4437. " aaaaaaaaaaaaaaa :\n"
  4438. " (bbbbbbbbbbbbbbb ? //\n"
  4439. " ccccccccccccccc :\n"
  4440. " ddddddddddddddd);",
  4441. Style);
  4442. verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4443. " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
  4444. " ccccccccccccccccccccccccccc;",
  4445. Style);
  4446. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4447. " aaaaa :\n"
  4448. " bbbbbbbbbbbbbbb + cccccccccccccccc;",
  4449. Style);
  4450. }
  4451. TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
  4452. verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
  4453. " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
  4454. verifyFormat("bool a = true, b = false;");
  4455. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4456. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
  4457. " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
  4458. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
  4459. verifyFormat(
  4460. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  4461. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
  4462. " d = e && f;");
  4463. verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
  4464. " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
  4465. verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  4466. " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
  4467. verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
  4468. " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
  4469. FormatStyle Style = getGoogleStyle();
  4470. Style.PointerAlignment = FormatStyle::PAS_Left;
  4471. Style.DerivePointerAlignment = false;
  4472. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4473. " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
  4474. " *b = bbbbbbbbbbbbbbbbbbb;",
  4475. Style);
  4476. verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  4477. " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
  4478. Style);
  4479. }
  4480. TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
  4481. verifyFormat("arr[foo ? bar : baz];");
  4482. verifyFormat("f()[foo ? bar : baz];");
  4483. verifyFormat("(a + b)[foo ? bar : baz];");
  4484. verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
  4485. }
  4486. TEST_F(FormatTest, AlignsStringLiterals) {
  4487. verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
  4488. " \"short literal\");");
  4489. verifyFormat(
  4490. "looooooooooooooooooooooooongFunction(\n"
  4491. " \"short literal\"\n"
  4492. " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
  4493. verifyFormat("someFunction(\"Always break between multi-line\"\n"
  4494. " \" string literals\",\n"
  4495. " and, other, parameters);");
  4496. EXPECT_EQ("fun + \"1243\" /* comment */\n"
  4497. " \"5678\";",
  4498. format("fun + \"1243\" /* comment */\n"
  4499. " \"5678\";",
  4500. getLLVMStyleWithColumns(28)));
  4501. EXPECT_EQ(
  4502. "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  4503. " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
  4504. " \"aaaaaaaaaaaaaaaa\";",
  4505. format("aaaaaa ="
  4506. "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  4507. "aaaaaaaaaaaaaaaaaaaaa\" "
  4508. "\"aaaaaaaaaaaaaaaa\";"));
  4509. verifyFormat("a = a + \"a\"\n"
  4510. " \"a\"\n"
  4511. " \"a\";");
  4512. verifyFormat("f(\"a\", \"b\"\n"
  4513. " \"c\");");
  4514. verifyFormat(
  4515. "#define LL_FORMAT \"ll\"\n"
  4516. "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
  4517. " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
  4518. verifyFormat("#define A(X) \\\n"
  4519. " \"aaaaa\" #X \"bbbbbb\" \\\n"
  4520. " \"ccccc\"",
  4521. getLLVMStyleWithColumns(23));
  4522. verifyFormat("#define A \"def\"\n"
  4523. "f(\"abc\" A \"ghi\"\n"
  4524. " \"jkl\");");
  4525. verifyFormat("f(L\"a\"\n"
  4526. " L\"b\");");
  4527. verifyFormat("#define A(X) \\\n"
  4528. " L\"aaaaa\" #X L\"bbbbbb\" \\\n"
  4529. " L\"ccccc\"",
  4530. getLLVMStyleWithColumns(25));
  4531. verifyFormat("f(@\"a\"\n"
  4532. " @\"b\");");
  4533. verifyFormat("NSString s = @\"a\"\n"
  4534. " @\"b\"\n"
  4535. " @\"c\";");
  4536. verifyFormat("NSString s = @\"a\"\n"
  4537. " \"b\"\n"
  4538. " \"c\";");
  4539. }
  4540. TEST_F(FormatTest, ReturnTypeBreakingStyle) {
  4541. FormatStyle Style = getLLVMStyle();
  4542. // No declarations or definitions should be moved to own line.
  4543. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
  4544. verifyFormat("class A {\n"
  4545. " int f() { return 1; }\n"
  4546. " int g();\n"
  4547. "};\n"
  4548. "int f() { return 1; }\n"
  4549. "int g();\n",
  4550. Style);
  4551. // All declarations and definitions should have the return type moved to its
  4552. // own
  4553. // line.
  4554. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
  4555. verifyFormat("class E {\n"
  4556. " int\n"
  4557. " f() {\n"
  4558. " return 1;\n"
  4559. " }\n"
  4560. " int\n"
  4561. " g();\n"
  4562. "};\n"
  4563. "int\n"
  4564. "f() {\n"
  4565. " return 1;\n"
  4566. "}\n"
  4567. "int\n"
  4568. "g();\n",
  4569. Style);
  4570. // Top-level definitions, and no kinds of declarations should have the
  4571. // return type moved to its own line.
  4572. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
  4573. verifyFormat("class B {\n"
  4574. " int f() { return 1; }\n"
  4575. " int g();\n"
  4576. "};\n"
  4577. "int\n"
  4578. "f() {\n"
  4579. " return 1;\n"
  4580. "}\n"
  4581. "int g();\n",
  4582. Style);
  4583. // Top-level definitions and declarations should have the return type moved
  4584. // to its own line.
  4585. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
  4586. verifyFormat("class C {\n"
  4587. " int f() { return 1; }\n"
  4588. " int g();\n"
  4589. "};\n"
  4590. "int\n"
  4591. "f() {\n"
  4592. " return 1;\n"
  4593. "}\n"
  4594. "int\n"
  4595. "g();\n",
  4596. Style);
  4597. // All definitions should have the return type moved to its own line, but no
  4598. // kinds of declarations.
  4599. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
  4600. verifyFormat("class D {\n"
  4601. " int\n"
  4602. " f() {\n"
  4603. " return 1;\n"
  4604. " }\n"
  4605. " int g();\n"
  4606. "};\n"
  4607. "int\n"
  4608. "f() {\n"
  4609. " return 1;\n"
  4610. "}\n"
  4611. "int g();\n",
  4612. Style);
  4613. verifyFormat("const char *\n"
  4614. "f(void) {\n" // Break here.
  4615. " return \"\";\n"
  4616. "}\n"
  4617. "const char *bar(void);\n", // No break here.
  4618. Style);
  4619. verifyFormat("template <class T>\n"
  4620. "T *\n"
  4621. "f(T &c) {\n" // Break here.
  4622. " return NULL;\n"
  4623. "}\n"
  4624. "template <class T> T *f(T &c);\n", // No break here.
  4625. Style);
  4626. verifyFormat("class C {\n"
  4627. " int\n"
  4628. " operator+() {\n"
  4629. " return 1;\n"
  4630. " }\n"
  4631. " int\n"
  4632. " operator()() {\n"
  4633. " return 1;\n"
  4634. " }\n"
  4635. "};\n",
  4636. Style);
  4637. verifyFormat("void\n"
  4638. "A::operator()() {}\n"
  4639. "void\n"
  4640. "A::operator>>() {}\n"
  4641. "void\n"
  4642. "A::operator+() {}\n",
  4643. Style);
  4644. verifyFormat("void *operator new(std::size_t s);", // No break here.
  4645. Style);
  4646. verifyFormat("void *\n"
  4647. "operator new(std::size_t s) {}",
  4648. Style);
  4649. verifyFormat("void *\n"
  4650. "operator delete[](void *ptr) {}",
  4651. Style);
  4652. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  4653. verifyFormat("const char *\n"
  4654. "f(void)\n" // Break here.
  4655. "{\n"
  4656. " return \"\";\n"
  4657. "}\n"
  4658. "const char *bar(void);\n", // No break here.
  4659. Style);
  4660. verifyFormat("template <class T>\n"
  4661. "T *\n" // Problem here: no line break
  4662. "f(T &c)\n" // Break here.
  4663. "{\n"
  4664. " return NULL;\n"
  4665. "}\n"
  4666. "template <class T> T *f(T &c);\n", // No break here.
  4667. Style);
  4668. }
  4669. TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
  4670. FormatStyle NoBreak = getLLVMStyle();
  4671. NoBreak.AlwaysBreakBeforeMultilineStrings = false;
  4672. FormatStyle Break = getLLVMStyle();
  4673. Break.AlwaysBreakBeforeMultilineStrings = true;
  4674. verifyFormat("aaaa = \"bbbb\"\n"
  4675. " \"cccc\";",
  4676. NoBreak);
  4677. verifyFormat("aaaa =\n"
  4678. " \"bbbb\"\n"
  4679. " \"cccc\";",
  4680. Break);
  4681. verifyFormat("aaaa(\"bbbb\"\n"
  4682. " \"cccc\");",
  4683. NoBreak);
  4684. verifyFormat("aaaa(\n"
  4685. " \"bbbb\"\n"
  4686. " \"cccc\");",
  4687. Break);
  4688. verifyFormat("aaaa(qqq, \"bbbb\"\n"
  4689. " \"cccc\");",
  4690. NoBreak);
  4691. verifyFormat("aaaa(qqq,\n"
  4692. " \"bbbb\"\n"
  4693. " \"cccc\");",
  4694. Break);
  4695. verifyFormat("aaaa(qqq,\n"
  4696. " L\"bbbb\"\n"
  4697. " L\"cccc\");",
  4698. Break);
  4699. verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
  4700. " \"bbbb\"));",
  4701. Break);
  4702. verifyFormat("string s = someFunction(\n"
  4703. " \"abc\"\n"
  4704. " \"abc\");",
  4705. Break);
  4706. // As we break before unary operators, breaking right after them is bad.
  4707. verifyFormat("string foo = abc ? \"x\"\n"
  4708. " \"blah blah blah blah blah blah\"\n"
  4709. " : \"y\";",
  4710. Break);
  4711. // Don't break if there is no column gain.
  4712. verifyFormat("f(\"aaaa\"\n"
  4713. " \"bbbb\");",
  4714. Break);
  4715. // Treat literals with escaped newlines like multi-line string literals.
  4716. EXPECT_EQ("x = \"a\\\n"
  4717. "b\\\n"
  4718. "c\";",
  4719. format("x = \"a\\\n"
  4720. "b\\\n"
  4721. "c\";",
  4722. NoBreak));
  4723. EXPECT_EQ("xxxx =\n"
  4724. " \"a\\\n"
  4725. "b\\\n"
  4726. "c\";",
  4727. format("xxxx = \"a\\\n"
  4728. "b\\\n"
  4729. "c\";",
  4730. Break));
  4731. // Exempt ObjC strings for now.
  4732. EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
  4733. " @\"bbbb\";",
  4734. format("NSString *const kString = @\"aaaa\"\n"
  4735. "@\"bbbb\";",
  4736. Break));
  4737. Break.ColumnLimit = 0;
  4738. verifyFormat("const char *hello = \"hello llvm\";", Break);
  4739. }
  4740. TEST_F(FormatTest, AlignsPipes) {
  4741. verifyFormat(
  4742. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4743. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4744. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4745. verifyFormat(
  4746. "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
  4747. " << aaaaaaaaaaaaaaaaaaaa;");
  4748. verifyFormat(
  4749. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4750. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4751. verifyFormat(
  4752. "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
  4753. " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
  4754. " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
  4755. verifyFormat(
  4756. "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4757. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4758. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4759. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4760. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4761. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4762. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4763. verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
  4764. " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
  4765. verifyFormat(
  4766. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4767. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4768. verifyFormat("return out << \"somepacket = {\\n\"\n"
  4769. " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
  4770. " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
  4771. " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
  4772. " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
  4773. " << \"}\";");
  4774. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  4775. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  4776. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
  4777. verifyFormat(
  4778. "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
  4779. " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
  4780. " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
  4781. " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
  4782. " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
  4783. verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
  4784. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4785. verifyFormat(
  4786. "void f() {\n"
  4787. " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
  4788. " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  4789. "}");
  4790. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
  4791. " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
  4792. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4793. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4794. " aaaaaaaaaaaaaaaaaaaaa)\n"
  4795. " << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4796. verifyFormat("LOG_IF(aaa == //\n"
  4797. " bbb)\n"
  4798. " << a << b;");
  4799. // Breaking before the first "<<" is generally not desirable.
  4800. verifyFormat(
  4801. "llvm::errs()\n"
  4802. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4803. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4804. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4805. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4806. getLLVMStyleWithColumns(70));
  4807. verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4808. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4809. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4810. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4811. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4812. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4813. getLLVMStyleWithColumns(70));
  4814. // But sometimes, breaking before the first "<<" is desirable.
  4815. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
  4816. " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
  4817. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
  4818. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4819. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4820. verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
  4821. " << BEF << IsTemplate << Description << E->getType();");
  4822. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
  4823. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4824. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4825. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
  4826. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4827. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4828. " << aaa;");
  4829. verifyFormat(
  4830. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4831. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4832. // Incomplete string literal.
  4833. EXPECT_EQ("llvm::errs() << \"\n"
  4834. " << a;",
  4835. format("llvm::errs() << \"\n<<a;"));
  4836. verifyFormat("void f() {\n"
  4837. " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
  4838. " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
  4839. "}");
  4840. // Handle 'endl'.
  4841. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
  4842. " << bbbbbbbbbbbbbbbbbbbbbb << endl;");
  4843. verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
  4844. // Handle '\n'.
  4845. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
  4846. " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
  4847. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
  4848. " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
  4849. verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
  4850. " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
  4851. verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
  4852. }
  4853. TEST_F(FormatTest, UnderstandsEquals) {
  4854. verifyFormat(
  4855. "aaaaaaaaaaaaaaaaa =\n"
  4856. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4857. verifyFormat(
  4858. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4859. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  4860. verifyFormat(
  4861. "if (a) {\n"
  4862. " f();\n"
  4863. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4864. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  4865. "}");
  4866. verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4867. " 100000000 + 10000000) {\n}");
  4868. }
  4869. TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
  4870. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  4871. " .looooooooooooooooooooooooooooooooooooooongFunction();");
  4872. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  4873. " ->looooooooooooooooooooooooooooooooooooooongFunction();");
  4874. verifyFormat(
  4875. "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
  4876. " Parameter2);");
  4877. verifyFormat(
  4878. "ShortObject->shortFunction(\n"
  4879. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
  4880. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
  4881. verifyFormat("loooooooooooooongFunction(\n"
  4882. " LoooooooooooooongObject->looooooooooooooooongFunction());");
  4883. verifyFormat(
  4884. "function(LoooooooooooooooooooooooooooooooooooongObject\n"
  4885. " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
  4886. verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  4887. " .WillRepeatedly(Return(SomeValue));");
  4888. verifyFormat("void f() {\n"
  4889. " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  4890. " .Times(2)\n"
  4891. " .WillRepeatedly(Return(SomeValue));\n"
  4892. "}");
  4893. verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
  4894. " ccccccccccccccccccccccc);");
  4895. verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4896. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4897. " .aaaaa(aaaaa),\n"
  4898. " aaaaaaaaaaaaaaaaaaaaa);");
  4899. verifyFormat("void f() {\n"
  4900. " aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4901. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
  4902. "}");
  4903. verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4904. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4905. " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4906. " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4907. " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  4908. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4909. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4910. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4911. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
  4912. "}");
  4913. // Here, it is not necessary to wrap at "." or "->".
  4914. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
  4915. " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  4916. verifyFormat(
  4917. "aaaaaaaaaaa->aaaaaaaaa(\n"
  4918. " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4919. " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
  4920. verifyFormat(
  4921. "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4922. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
  4923. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
  4924. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  4925. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
  4926. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  4927. verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4928. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4929. " .a();");
  4930. FormatStyle NoBinPacking = getLLVMStyle();
  4931. NoBinPacking.BinPackParameters = false;
  4932. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  4933. " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  4934. " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
  4935. " aaaaaaaaaaaaaaaaaaa,\n"
  4936. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4937. NoBinPacking);
  4938. // If there is a subsequent call, change to hanging indentation.
  4939. verifyFormat(
  4940. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4941. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
  4942. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4943. verifyFormat(
  4944. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4945. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
  4946. verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4947. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4948. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4949. verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4950. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4951. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  4952. }
  4953. TEST_F(FormatTest, WrapsTemplateDeclarations) {
  4954. verifyFormat("template <typename T>\n"
  4955. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  4956. verifyFormat("template <typename T>\n"
  4957. "// T should be one of {A, B}.\n"
  4958. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  4959. verifyFormat(
  4960. "template <typename T>\n"
  4961. "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
  4962. verifyFormat("template <typename T>\n"
  4963. "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
  4964. " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
  4965. verifyFormat(
  4966. "template <typename T>\n"
  4967. "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
  4968. " int Paaaaaaaaaaaaaaaaaaaaram2);");
  4969. verifyFormat(
  4970. "template <typename T>\n"
  4971. "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
  4972. " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
  4973. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4974. verifyFormat("template <typename T>\n"
  4975. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4976. " int aaaaaaaaaaaaaaaaaaaaaa);");
  4977. verifyFormat(
  4978. "template <typename T1, typename T2 = char, typename T3 = char,\n"
  4979. " typename T4 = char>\n"
  4980. "void f();");
  4981. verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
  4982. " template <typename> class cccccccccccccccccccccc,\n"
  4983. " typename ddddddddddddd>\n"
  4984. "class C {};");
  4985. verifyFormat(
  4986. "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
  4987. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4988. verifyFormat("void f() {\n"
  4989. " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
  4990. " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
  4991. "}");
  4992. verifyFormat("template <typename T> class C {};");
  4993. verifyFormat("template <typename T> void f();");
  4994. verifyFormat("template <typename T> void f() {}");
  4995. verifyFormat(
  4996. "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  4997. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4998. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
  4999. " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  5000. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5001. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
  5002. " bbbbbbbbbbbbbbbbbbbbbbbb);",
  5003. getLLVMStyleWithColumns(72));
  5004. EXPECT_EQ("static_cast<A< //\n"
  5005. " B> *>(\n"
  5006. "\n"
  5007. " );",
  5008. format("static_cast<A<//\n"
  5009. " B>*>(\n"
  5010. "\n"
  5011. " );"));
  5012. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5013. " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
  5014. FormatStyle AlwaysBreak = getLLVMStyle();
  5015. AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
  5016. verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
  5017. verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
  5018. verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
  5019. verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5020. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
  5021. " ccccccccccccccccccccccccccccccccccccccccccccccc);");
  5022. verifyFormat("template <template <typename> class Fooooooo,\n"
  5023. " template <typename> class Baaaaaaar>\n"
  5024. "struct C {};",
  5025. AlwaysBreak);
  5026. verifyFormat("template <typename T> // T can be A, B or C.\n"
  5027. "struct C {};",
  5028. AlwaysBreak);
  5029. verifyFormat("template <enum E> class A {\n"
  5030. "public:\n"
  5031. " E *f();\n"
  5032. "};");
  5033. }
  5034. TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
  5035. verifyFormat(
  5036. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  5037. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  5038. verifyFormat(
  5039. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  5040. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5041. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  5042. // FIXME: Should we have the extra indent after the second break?
  5043. verifyFormat(
  5044. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  5045. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  5046. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  5047. verifyFormat(
  5048. "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
  5049. " cccccccccccccccccccccccccccccccccccccccccccccc());");
  5050. // Breaking at nested name specifiers is generally not desirable.
  5051. verifyFormat(
  5052. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5053. " aaaaaaaaaaaaaaaaaaaaaaa);");
  5054. verifyFormat(
  5055. "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  5056. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5057. " aaaaaaaaaaaaaaaaaaaaa);",
  5058. getLLVMStyleWithColumns(74));
  5059. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  5060. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5061. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  5062. }
  5063. TEST_F(FormatTest, UnderstandsTemplateParameters) {
  5064. verifyFormat("A<int> a;");
  5065. verifyFormat("A<A<A<int>>> a;");
  5066. verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
  5067. verifyFormat("bool x = a < 1 || 2 > a;");
  5068. verifyFormat("bool x = 5 < f<int>();");
  5069. verifyFormat("bool x = f<int>() > 5;");
  5070. verifyFormat("bool x = 5 < a<int>::x;");
  5071. verifyFormat("bool x = a < 4 ? a > 2 : false;");
  5072. verifyFormat("bool x = f() ? a < 2 : a > 2;");
  5073. verifyGoogleFormat("A<A<int>> a;");
  5074. verifyGoogleFormat("A<A<A<int>>> a;");
  5075. verifyGoogleFormat("A<A<A<A<int>>>> a;");
  5076. verifyGoogleFormat("A<A<int> > a;");
  5077. verifyGoogleFormat("A<A<A<int> > > a;");
  5078. verifyGoogleFormat("A<A<A<A<int> > > > a;");
  5079. verifyGoogleFormat("A<::A<int>> a;");
  5080. verifyGoogleFormat("A<::A> a;");
  5081. verifyGoogleFormat("A< ::A> a;");
  5082. verifyGoogleFormat("A< ::A<int> > a;");
  5083. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
  5084. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
  5085. EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
  5086. EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
  5087. EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
  5088. format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
  5089. verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
  5090. verifyFormat("test >> a >> b;");
  5091. verifyFormat("test << a >> b;");
  5092. verifyFormat("f<int>();");
  5093. verifyFormat("template <typename T> void f() {}");
  5094. verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
  5095. verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
  5096. "sizeof(char)>::type>;");
  5097. verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
  5098. verifyFormat("f(a.operator()<A>());");
  5099. verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5100. " .template operator()<A>());",
  5101. getLLVMStyleWithColumns(35));
  5102. // Not template parameters.
  5103. verifyFormat("return a < b && c > d;");
  5104. verifyFormat("void f() {\n"
  5105. " while (a < b && c > d) {\n"
  5106. " }\n"
  5107. "}");
  5108. verifyFormat("template <typename... Types>\n"
  5109. "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
  5110. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5111. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
  5112. getLLVMStyleWithColumns(60));
  5113. verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
  5114. verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
  5115. verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
  5116. }
  5117. TEST_F(FormatTest, UnderstandsBinaryOperators) {
  5118. verifyFormat("COMPARE(a, ==, b);");
  5119. }
  5120. TEST_F(FormatTest, UnderstandsPointersToMembers) {
  5121. verifyFormat("int A::*x;");
  5122. verifyFormat("int (S::*func)(void *);");
  5123. verifyFormat("void f() { int (S::*func)(void *); }");
  5124. verifyFormat("typedef bool *(Class::*Member)() const;");
  5125. verifyFormat("void f() {\n"
  5126. " (a->*f)();\n"
  5127. " a->*x;\n"
  5128. " (a.*f)();\n"
  5129. " ((*a).*f)();\n"
  5130. " a.*x;\n"
  5131. "}");
  5132. verifyFormat("void f() {\n"
  5133. " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  5134. " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
  5135. "}");
  5136. verifyFormat(
  5137. "(aaaaaaaaaa->*bbbbbbb)(\n"
  5138. " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  5139. FormatStyle Style = getLLVMStyle();
  5140. Style.PointerAlignment = FormatStyle::PAS_Left;
  5141. verifyFormat("typedef bool* (Class::*Member)() const;", Style);
  5142. }
  5143. TEST_F(FormatTest, UnderstandsUnaryOperators) {
  5144. verifyFormat("int a = -2;");
  5145. verifyFormat("f(-1, -2, -3);");
  5146. verifyFormat("a[-1] = 5;");
  5147. verifyFormat("int a = 5 + -2;");
  5148. verifyFormat("if (i == -1) {\n}");
  5149. verifyFormat("if (i != -1) {\n}");
  5150. verifyFormat("if (i > -1) {\n}");
  5151. verifyFormat("if (i < -1) {\n}");
  5152. verifyFormat("++(a->f());");
  5153. verifyFormat("--(a->f());");
  5154. verifyFormat("(a->f())++;");
  5155. verifyFormat("a[42]++;");
  5156. verifyFormat("if (!(a->f())) {\n}");
  5157. verifyFormat("a-- > b;");
  5158. verifyFormat("b ? -a : c;");
  5159. verifyFormat("n * sizeof char16;");
  5160. verifyFormat("n * alignof char16;", getGoogleStyle());
  5161. verifyFormat("sizeof(char);");
  5162. verifyFormat("alignof(char);", getGoogleStyle());
  5163. verifyFormat("return -1;");
  5164. verifyFormat("switch (a) {\n"
  5165. "case -1:\n"
  5166. " break;\n"
  5167. "}");
  5168. verifyFormat("#define X -1");
  5169. verifyFormat("#define X -kConstant");
  5170. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
  5171. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
  5172. verifyFormat("int a = /* confusing comment */ -1;");
  5173. // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
  5174. verifyFormat("int a = i /* confusing comment */++;");
  5175. }
  5176. TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
  5177. verifyFormat("if (!aaaaaaaaaa( // break\n"
  5178. " aaaaa)) {\n"
  5179. "}");
  5180. verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
  5181. " aaaaa));");
  5182. verifyFormat("*aaa = aaaaaaa( // break\n"
  5183. " bbbbbb);");
  5184. }
  5185. TEST_F(FormatTest, UnderstandsOverloadedOperators) {
  5186. verifyFormat("bool operator<();");
  5187. verifyFormat("bool operator>();");
  5188. verifyFormat("bool operator=();");
  5189. verifyFormat("bool operator==();");
  5190. verifyFormat("bool operator!=();");
  5191. verifyFormat("int operator+();");
  5192. verifyFormat("int operator++();");
  5193. verifyFormat("bool operator,();");
  5194. verifyFormat("bool operator();");
  5195. verifyFormat("bool operator()();");
  5196. verifyFormat("bool operator[]();");
  5197. verifyFormat("operator bool();");
  5198. verifyFormat("operator int();");
  5199. verifyFormat("operator void *();");
  5200. verifyFormat("operator SomeType<int>();");
  5201. verifyFormat("operator SomeType<int, int>();");
  5202. verifyFormat("operator SomeType<SomeType<int>>();");
  5203. verifyFormat("void *operator new(std::size_t size);");
  5204. verifyFormat("void *operator new[](std::size_t size);");
  5205. verifyFormat("void operator delete(void *ptr);");
  5206. verifyFormat("void operator delete[](void *ptr);");
  5207. verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
  5208. "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
  5209. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
  5210. " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
  5211. verifyFormat(
  5212. "ostream &operator<<(ostream &OutputStream,\n"
  5213. " SomeReallyLongType WithSomeReallyLongValue);");
  5214. verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
  5215. " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
  5216. " return left.group < right.group;\n"
  5217. "}");
  5218. verifyFormat("SomeType &operator=(const SomeType &S);");
  5219. verifyFormat("f.template operator()<int>();");
  5220. verifyGoogleFormat("operator void*();");
  5221. verifyGoogleFormat("operator SomeType<SomeType<int>>();");
  5222. verifyGoogleFormat("operator ::A();");
  5223. verifyFormat("using A::operator+;");
  5224. verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
  5225. "int i;");
  5226. }
  5227. TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
  5228. verifyFormat("Deleted &operator=(const Deleted &) & = default;");
  5229. verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
  5230. verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
  5231. verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
  5232. verifyFormat("Deleted &operator=(const Deleted &) &;");
  5233. verifyFormat("Deleted &operator=(const Deleted &) &&;");
  5234. verifyFormat("SomeType MemberFunction(const Deleted &) &;");
  5235. verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
  5236. verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
  5237. verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
  5238. verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
  5239. FormatStyle AlignLeft = getLLVMStyle();
  5240. AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
  5241. verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
  5242. verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
  5243. AlignLeft);
  5244. verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
  5245. verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
  5246. verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
  5247. verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
  5248. verifyFormat("auto Function(T) & -> void {}", AlignLeft);
  5249. verifyFormat("auto Function(T) & -> void;", AlignLeft);
  5250. FormatStyle Spaces = getLLVMStyle();
  5251. Spaces.SpacesInCStyleCastParentheses = true;
  5252. verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
  5253. verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
  5254. verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
  5255. verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
  5256. Spaces.SpacesInCStyleCastParentheses = false;
  5257. Spaces.SpacesInParentheses = true;
  5258. verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
  5259. verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
  5260. verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
  5261. verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
  5262. }
  5263. TEST_F(FormatTest, UnderstandsNewAndDelete) {
  5264. verifyFormat("void f() {\n"
  5265. " A *a = new A;\n"
  5266. " A *a = new (placement) A;\n"
  5267. " delete a;\n"
  5268. " delete (A *)a;\n"
  5269. "}");
  5270. verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
  5271. " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
  5272. verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  5273. " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
  5274. " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
  5275. verifyFormat("delete[] h->p;");
  5276. }
  5277. TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
  5278. verifyFormat("int *f(int *a) {}");
  5279. verifyFormat("int main(int argc, char **argv) {}");
  5280. verifyFormat("Test::Test(int b) : a(b * b) {}");
  5281. verifyIndependentOfContext("f(a, *a);");
  5282. verifyFormat("void g() { f(*a); }");
  5283. verifyIndependentOfContext("int a = b * 10;");
  5284. verifyIndependentOfContext("int a = 10 * b;");
  5285. verifyIndependentOfContext("int a = b * c;");
  5286. verifyIndependentOfContext("int a += b * c;");
  5287. verifyIndependentOfContext("int a -= b * c;");
  5288. verifyIndependentOfContext("int a *= b * c;");
  5289. verifyIndependentOfContext("int a /= b * c;");
  5290. verifyIndependentOfContext("int a = *b;");
  5291. verifyIndependentOfContext("int a = *b * c;");
  5292. verifyIndependentOfContext("int a = b * *c;");
  5293. verifyIndependentOfContext("int a = b * (10);");
  5294. verifyIndependentOfContext("S << b * (10);");
  5295. verifyIndependentOfContext("return 10 * b;");
  5296. verifyIndependentOfContext("return *b * *c;");
  5297. verifyIndependentOfContext("return a & ~b;");
  5298. verifyIndependentOfContext("f(b ? *c : *d);");
  5299. verifyIndependentOfContext("int a = b ? *c : *d;");
  5300. verifyIndependentOfContext("*b = a;");
  5301. verifyIndependentOfContext("a * ~b;");
  5302. verifyIndependentOfContext("a * !b;");
  5303. verifyIndependentOfContext("a * +b;");
  5304. verifyIndependentOfContext("a * -b;");
  5305. verifyIndependentOfContext("a * ++b;");
  5306. verifyIndependentOfContext("a * --b;");
  5307. verifyIndependentOfContext("a[4] * b;");
  5308. verifyIndependentOfContext("a[a * a] = 1;");
  5309. verifyIndependentOfContext("f() * b;");
  5310. verifyIndependentOfContext("a * [self dostuff];");
  5311. verifyIndependentOfContext("int x = a * (a + b);");
  5312. verifyIndependentOfContext("(a *)(a + b);");
  5313. verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
  5314. verifyIndependentOfContext("int *pa = (int *)&a;");
  5315. verifyIndependentOfContext("return sizeof(int **);");
  5316. verifyIndependentOfContext("return sizeof(int ******);");
  5317. verifyIndependentOfContext("return (int **&)a;");
  5318. verifyIndependentOfContext("f((*PointerToArray)[10]);");
  5319. verifyFormat("void f(Type (*parameter)[10]) {}");
  5320. verifyFormat("void f(Type (&parameter)[10]) {}");
  5321. verifyGoogleFormat("return sizeof(int**);");
  5322. verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
  5323. verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
  5324. verifyFormat("auto a = [](int **&, int ***) {};");
  5325. verifyFormat("auto PointerBinding = [](const char *S) {};");
  5326. verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
  5327. verifyFormat("[](const decltype(*a) &value) {}");
  5328. verifyFormat("decltype(a * b) F();");
  5329. verifyFormat("#define MACRO() [](A *a) { return 1; }");
  5330. verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
  5331. verifyIndependentOfContext("typedef void (*f)(int *a);");
  5332. verifyIndependentOfContext("int i{a * b};");
  5333. verifyIndependentOfContext("aaa && aaa->f();");
  5334. verifyIndependentOfContext("int x = ~*p;");
  5335. verifyFormat("Constructor() : a(a), area(width * height) {}");
  5336. verifyFormat("Constructor() : a(a), area(a, width * height) {}");
  5337. verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
  5338. verifyFormat("void f() { f(a, c * d); }");
  5339. verifyFormat("void f() { f(new a(), c * d); }");
  5340. verifyIndependentOfContext("InvalidRegions[*R] = 0;");
  5341. verifyIndependentOfContext("A<int *> a;");
  5342. verifyIndependentOfContext("A<int **> a;");
  5343. verifyIndependentOfContext("A<int *, int *> a;");
  5344. verifyIndependentOfContext("A<int *[]> a;");
  5345. verifyIndependentOfContext(
  5346. "const char *const p = reinterpret_cast<const char *const>(q);");
  5347. verifyIndependentOfContext("A<int **, int **> a;");
  5348. verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
  5349. verifyFormat("for (char **a = b; *a; ++a) {\n}");
  5350. verifyFormat("for (; a && b;) {\n}");
  5351. verifyFormat("bool foo = true && [] { return false; }();");
  5352. verifyFormat(
  5353. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5354. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5355. verifyGoogleFormat("**outparam = 1;");
  5356. verifyGoogleFormat("*outparam = a * b;");
  5357. verifyGoogleFormat("int main(int argc, char** argv) {}");
  5358. verifyGoogleFormat("A<int*> a;");
  5359. verifyGoogleFormat("A<int**> a;");
  5360. verifyGoogleFormat("A<int*, int*> a;");
  5361. verifyGoogleFormat("A<int**, int**> a;");
  5362. verifyGoogleFormat("f(b ? *c : *d);");
  5363. verifyGoogleFormat("int a = b ? *c : *d;");
  5364. verifyGoogleFormat("Type* t = **x;");
  5365. verifyGoogleFormat("Type* t = *++*x;");
  5366. verifyGoogleFormat("*++*x;");
  5367. verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
  5368. verifyGoogleFormat("Type* t = x++ * y;");
  5369. verifyGoogleFormat(
  5370. "const char* const p = reinterpret_cast<const char* const>(q);");
  5371. verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
  5372. verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
  5373. verifyGoogleFormat("template <typename T>\n"
  5374. "void f(int i = 0, SomeType** temps = NULL);");
  5375. FormatStyle Left = getLLVMStyle();
  5376. Left.PointerAlignment = FormatStyle::PAS_Left;
  5377. verifyFormat("x = *a(x) = *a(y);", Left);
  5378. verifyFormat("for (;; * = b) {\n}", Left);
  5379. verifyFormat("return *this += 1;", Left);
  5380. verifyIndependentOfContext("a = *(x + y);");
  5381. verifyIndependentOfContext("a = &(x + y);");
  5382. verifyIndependentOfContext("*(x + y).call();");
  5383. verifyIndependentOfContext("&(x + y)->call();");
  5384. verifyFormat("void f() { &(*I).first; }");
  5385. verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
  5386. verifyFormat(
  5387. "int *MyValues = {\n"
  5388. " *A, // Operator detection might be confused by the '{'\n"
  5389. " *BB // Operator detection might be confused by previous comment\n"
  5390. "};");
  5391. verifyIndependentOfContext("if (int *a = &b)");
  5392. verifyIndependentOfContext("if (int &a = *b)");
  5393. verifyIndependentOfContext("if (a & b[i])");
  5394. verifyIndependentOfContext("if (a::b::c::d & b[i])");
  5395. verifyIndependentOfContext("if (*b[i])");
  5396. verifyIndependentOfContext("if (int *a = (&b))");
  5397. verifyIndependentOfContext("while (int *a = &b)");
  5398. verifyIndependentOfContext("size = sizeof *a;");
  5399. verifyIndependentOfContext("if (a && (b = c))");
  5400. verifyFormat("void f() {\n"
  5401. " for (const int &v : Values) {\n"
  5402. " }\n"
  5403. "}");
  5404. verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
  5405. verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
  5406. verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
  5407. verifyFormat("#define A (!a * b)");
  5408. verifyFormat("#define MACRO \\\n"
  5409. " int *i = a * b; \\\n"
  5410. " void f(a *b);",
  5411. getLLVMStyleWithColumns(19));
  5412. verifyIndependentOfContext("A = new SomeType *[Length];");
  5413. verifyIndependentOfContext("A = new SomeType *[Length]();");
  5414. verifyIndependentOfContext("T **t = new T *;");
  5415. verifyIndependentOfContext("T **t = new T *();");
  5416. verifyGoogleFormat("A = new SomeType*[Length]();");
  5417. verifyGoogleFormat("A = new SomeType*[Length];");
  5418. verifyGoogleFormat("T** t = new T*;");
  5419. verifyGoogleFormat("T** t = new T*();");
  5420. FormatStyle PointerLeft = getLLVMStyle();
  5421. PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
  5422. verifyFormat("delete *x;", PointerLeft);
  5423. verifyFormat("STATIC_ASSERT((a & b) == 0);");
  5424. verifyFormat("STATIC_ASSERT(0 == (a & b));");
  5425. verifyFormat("template <bool a, bool b> "
  5426. "typename t::if<x && y>::type f() {}");
  5427. verifyFormat("template <int *y> f() {}");
  5428. verifyFormat("vector<int *> v;");
  5429. verifyFormat("vector<int *const> v;");
  5430. verifyFormat("vector<int *const **const *> v;");
  5431. verifyFormat("vector<int *volatile> v;");
  5432. verifyFormat("vector<a * b> v;");
  5433. verifyFormat("foo<b && false>();");
  5434. verifyFormat("foo<b & 1>();");
  5435. verifyFormat("decltype(*::std::declval<const T &>()) void F();");
  5436. verifyFormat(
  5437. "template <class T, class = typename std::enable_if<\n"
  5438. " std::is_integral<T>::value &&\n"
  5439. " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
  5440. "void F();",
  5441. getLLVMStyleWithColumns(76));
  5442. verifyFormat(
  5443. "template <class T,\n"
  5444. " class = typename ::std::enable_if<\n"
  5445. " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
  5446. "void F();",
  5447. getGoogleStyleWithColumns(68));
  5448. verifyIndependentOfContext("MACRO(int *i);");
  5449. verifyIndependentOfContext("MACRO(auto *a);");
  5450. verifyIndependentOfContext("MACRO(const A *a);");
  5451. verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
  5452. // FIXME: Is there a way to make this work?
  5453. // verifyIndependentOfContext("MACRO(A *a);");
  5454. verifyFormat("DatumHandle const *operator->() const { return input_; }");
  5455. verifyFormat("return options != nullptr && operator==(*options);");
  5456. EXPECT_EQ("#define OP(x) \\\n"
  5457. " ostream &operator<<(ostream &s, const A &a) { \\\n"
  5458. " return s << a.DebugString(); \\\n"
  5459. " }",
  5460. format("#define OP(x) \\\n"
  5461. " ostream &operator<<(ostream &s, const A &a) { \\\n"
  5462. " return s << a.DebugString(); \\\n"
  5463. " }",
  5464. getLLVMStyleWithColumns(50)));
  5465. // FIXME: We cannot handle this case yet; we might be able to figure out that
  5466. // foo<x> d > v; doesn't make sense.
  5467. verifyFormat("foo<a<b && c> d> v;");
  5468. FormatStyle PointerMiddle = getLLVMStyle();
  5469. PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
  5470. verifyFormat("delete *x;", PointerMiddle);
  5471. verifyFormat("int * x;", PointerMiddle);
  5472. verifyFormat("template <int * y> f() {}", PointerMiddle);
  5473. verifyFormat("int * f(int * a) {}", PointerMiddle);
  5474. verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
  5475. verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
  5476. verifyFormat("A<int *> a;", PointerMiddle);
  5477. verifyFormat("A<int **> a;", PointerMiddle);
  5478. verifyFormat("A<int *, int *> a;", PointerMiddle);
  5479. verifyFormat("A<int * []> a;", PointerMiddle);
  5480. verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
  5481. verifyFormat("A = new SomeType *[Length];", PointerMiddle);
  5482. verifyFormat("T ** t = new T *;", PointerMiddle);
  5483. // Member function reference qualifiers aren't binary operators.
  5484. verifyFormat("string // break\n"
  5485. "operator()() & {}");
  5486. verifyFormat("string // break\n"
  5487. "operator()() && {}");
  5488. verifyGoogleFormat("template <typename T>\n"
  5489. "auto x() & -> int {}");
  5490. }
  5491. TEST_F(FormatTest, UnderstandsAttributes) {
  5492. verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
  5493. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
  5494. "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
  5495. FormatStyle AfterType = getLLVMStyle();
  5496. AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
  5497. verifyFormat("__attribute__((nodebug)) void\n"
  5498. "foo() {}\n",
  5499. AfterType);
  5500. }
  5501. TEST_F(FormatTest, UnderstandsEllipsis) {
  5502. verifyFormat("int printf(const char *fmt, ...);");
  5503. verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
  5504. verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
  5505. FormatStyle PointersLeft = getLLVMStyle();
  5506. PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
  5507. verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
  5508. }
  5509. TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
  5510. EXPECT_EQ("int *a;\n"
  5511. "int *a;\n"
  5512. "int *a;",
  5513. format("int *a;\n"
  5514. "int* a;\n"
  5515. "int *a;",
  5516. getGoogleStyle()));
  5517. EXPECT_EQ("int* a;\n"
  5518. "int* a;\n"
  5519. "int* a;",
  5520. format("int* a;\n"
  5521. "int* a;\n"
  5522. "int *a;",
  5523. getGoogleStyle()));
  5524. EXPECT_EQ("int *a;\n"
  5525. "int *a;\n"
  5526. "int *a;",
  5527. format("int *a;\n"
  5528. "int * a;\n"
  5529. "int * a;",
  5530. getGoogleStyle()));
  5531. EXPECT_EQ("auto x = [] {\n"
  5532. " int *a;\n"
  5533. " int *a;\n"
  5534. " int *a;\n"
  5535. "};",
  5536. format("auto x=[]{int *a;\n"
  5537. "int * a;\n"
  5538. "int * a;};",
  5539. getGoogleStyle()));
  5540. }
  5541. TEST_F(FormatTest, UnderstandsRvalueReferences) {
  5542. verifyFormat("int f(int &&a) {}");
  5543. verifyFormat("int f(int a, char &&b) {}");
  5544. verifyFormat("void f() { int &&a = b; }");
  5545. verifyGoogleFormat("int f(int a, char&& b) {}");
  5546. verifyGoogleFormat("void f() { int&& a = b; }");
  5547. verifyIndependentOfContext("A<int &&> a;");
  5548. verifyIndependentOfContext("A<int &&, int &&> a;");
  5549. verifyGoogleFormat("A<int&&> a;");
  5550. verifyGoogleFormat("A<int&&, int&&> a;");
  5551. // Not rvalue references:
  5552. verifyFormat("template <bool B, bool C> class A {\n"
  5553. " static_assert(B && C, \"Something is wrong\");\n"
  5554. "};");
  5555. verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
  5556. verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
  5557. verifyFormat("#define A(a, b) (a && b)");
  5558. }
  5559. TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
  5560. verifyFormat("void f() {\n"
  5561. " x[aaaaaaaaa -\n"
  5562. " b] = 23;\n"
  5563. "}",
  5564. getLLVMStyleWithColumns(15));
  5565. }
  5566. TEST_F(FormatTest, FormatsCasts) {
  5567. verifyFormat("Type *A = static_cast<Type *>(P);");
  5568. verifyFormat("Type *A = (Type *)P;");
  5569. verifyFormat("Type *A = (vector<Type *, int *>)P;");
  5570. verifyFormat("int a = (int)(2.0f);");
  5571. verifyFormat("int a = (int)2.0f;");
  5572. verifyFormat("x[(int32)y];");
  5573. verifyFormat("x = (int32)y;");
  5574. verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
  5575. verifyFormat("int a = (int)*b;");
  5576. verifyFormat("int a = (int)2.0f;");
  5577. verifyFormat("int a = (int)~0;");
  5578. verifyFormat("int a = (int)++a;");
  5579. verifyFormat("int a = (int)sizeof(int);");
  5580. verifyFormat("int a = (int)+2;");
  5581. verifyFormat("my_int a = (my_int)2.0f;");
  5582. verifyFormat("my_int a = (my_int)sizeof(int);");
  5583. verifyFormat("return (my_int)aaa;");
  5584. verifyFormat("#define x ((int)-1)");
  5585. verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
  5586. verifyFormat("#define p(q) ((int *)&q)");
  5587. verifyFormat("fn(a)(b) + 1;");
  5588. verifyFormat("void f() { my_int a = (my_int)*b; }");
  5589. verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
  5590. verifyFormat("my_int a = (my_int)~0;");
  5591. verifyFormat("my_int a = (my_int)++a;");
  5592. verifyFormat("my_int a = (my_int)-2;");
  5593. verifyFormat("my_int a = (my_int)1;");
  5594. verifyFormat("my_int a = (my_int *)1;");
  5595. verifyFormat("my_int a = (const my_int)-1;");
  5596. verifyFormat("my_int a = (const my_int *)-1;");
  5597. verifyFormat("my_int a = (my_int)(my_int)-1;");
  5598. verifyFormat("my_int a = (ns::my_int)-2;");
  5599. verifyFormat("case (my_int)ONE:");
  5600. verifyFormat("auto x = (X)this;");
  5601. // FIXME: single value wrapped with paren will be treated as cast.
  5602. verifyFormat("void f(int i = (kValue)*kMask) {}");
  5603. verifyFormat("{ (void)F; }");
  5604. // Don't break after a cast's
  5605. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  5606. " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
  5607. " bbbbbbbbbbbbbbbbbbbbbb);");
  5608. // These are not casts.
  5609. verifyFormat("void f(int *) {}");
  5610. verifyFormat("f(foo)->b;");
  5611. verifyFormat("f(foo).b;");
  5612. verifyFormat("f(foo)(b);");
  5613. verifyFormat("f(foo)[b];");
  5614. verifyFormat("[](foo) { return 4; }(bar);");
  5615. verifyFormat("(*funptr)(foo)[4];");
  5616. verifyFormat("funptrs[4](foo)[4];");
  5617. verifyFormat("void f(int *);");
  5618. verifyFormat("void f(int *) = 0;");
  5619. verifyFormat("void f(SmallVector<int>) {}");
  5620. verifyFormat("void f(SmallVector<int>);");
  5621. verifyFormat("void f(SmallVector<int>) = 0;");
  5622. verifyFormat("void f(int i = (kA * kB) & kMask) {}");
  5623. verifyFormat("int a = sizeof(int) * b;");
  5624. verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
  5625. verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
  5626. verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
  5627. verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
  5628. // These are not casts, but at some point were confused with casts.
  5629. verifyFormat("virtual void foo(int *) override;");
  5630. verifyFormat("virtual void foo(char &) const;");
  5631. verifyFormat("virtual void foo(int *a, char *) const;");
  5632. verifyFormat("int a = sizeof(int *) + b;");
  5633. verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
  5634. verifyFormat("bool b = f(g<int>) && c;");
  5635. verifyFormat("typedef void (*f)(int i) func;");
  5636. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
  5637. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  5638. // FIXME: The indentation here is not ideal.
  5639. verifyFormat(
  5640. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5641. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
  5642. " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
  5643. }
  5644. TEST_F(FormatTest, FormatsFunctionTypes) {
  5645. verifyFormat("A<bool()> a;");
  5646. verifyFormat("A<SomeType()> a;");
  5647. verifyFormat("A<void (*)(int, std::string)> a;");
  5648. verifyFormat("A<void *(int)>;");
  5649. verifyFormat("void *(*a)(int *, SomeType *);");
  5650. verifyFormat("int (*func)(void *);");
  5651. verifyFormat("void f() { int (*func)(void *); }");
  5652. verifyFormat("template <class CallbackClass>\n"
  5653. "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
  5654. verifyGoogleFormat("A<void*(int*, SomeType*)>;");
  5655. verifyGoogleFormat("void* (*a)(int);");
  5656. verifyGoogleFormat(
  5657. "template <class CallbackClass>\n"
  5658. "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
  5659. // Other constructs can look somewhat like function types:
  5660. verifyFormat("A<sizeof(*x)> a;");
  5661. verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
  5662. verifyFormat("some_var = function(*some_pointer_var)[0];");
  5663. verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
  5664. verifyFormat("int x = f(&h)();");
  5665. }
  5666. TEST_F(FormatTest, FormatsPointersToArrayTypes) {
  5667. verifyFormat("A (*foo_)[6];");
  5668. verifyFormat("vector<int> (*foo_)[6];");
  5669. }
  5670. TEST_F(FormatTest, BreaksLongVariableDeclarations) {
  5671. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5672. " LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5673. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
  5674. " LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5675. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5676. " *LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5677. // Different ways of ()-initializiation.
  5678. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5679. " LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
  5680. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5681. " LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
  5682. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5683. " LoooooooooooooooooooooooooooooooooooooooongVariable({});");
  5684. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5685. " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
  5686. }
  5687. TEST_F(FormatTest, BreaksLongDeclarations) {
  5688. verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
  5689. " AnotherNameForTheLongType;");
  5690. verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
  5691. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  5692. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5693. "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
  5694. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
  5695. "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
  5696. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5697. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5698. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
  5699. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5700. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  5701. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5702. verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
  5703. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5704. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5705. "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
  5706. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5707. "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
  5708. FormatStyle Indented = getLLVMStyle();
  5709. Indented.IndentWrappedFunctionNames = true;
  5710. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5711. " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
  5712. Indented);
  5713. verifyFormat(
  5714. "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5715. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5716. Indented);
  5717. verifyFormat(
  5718. "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  5719. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5720. Indented);
  5721. verifyFormat(
  5722. "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
  5723. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5724. Indented);
  5725. // FIXME: Without the comment, this breaks after "(".
  5726. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
  5727. " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
  5728. getGoogleStyle());
  5729. verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
  5730. " int LoooooooooooooooooooongParam2) {}");
  5731. verifyFormat(
  5732. "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
  5733. " SourceLocation L, IdentifierIn *II,\n"
  5734. " Type *T) {}");
  5735. verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
  5736. "ReallyReaaallyLongFunctionName(\n"
  5737. " const std::string &SomeParameter,\n"
  5738. " const SomeType<string, SomeOtherTemplateParameter>\n"
  5739. " &ReallyReallyLongParameterName,\n"
  5740. " const SomeType<string, SomeOtherTemplateParameter>\n"
  5741. " &AnotherLongParameterName) {}");
  5742. verifyFormat("template <typename A>\n"
  5743. "SomeLoooooooooooooooooooooongType<\n"
  5744. " typename some_namespace::SomeOtherType<A>::Type>\n"
  5745. "Function() {}");
  5746. verifyGoogleFormat(
  5747. "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
  5748. " aaaaaaaaaaaaaaaaaaaaaaa;");
  5749. verifyGoogleFormat(
  5750. "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
  5751. " SourceLocation L) {}");
  5752. verifyGoogleFormat(
  5753. "some_namespace::LongReturnType\n"
  5754. "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
  5755. " int first_long_parameter, int second_parameter) {}");
  5756. verifyGoogleFormat("template <typename T>\n"
  5757. "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  5758. "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
  5759. verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5760. " int aaaaaaaaaaaaaaaaaaaaaaa);");
  5761. verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  5762. " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5763. " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5764. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5765. " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  5766. " aaaaaaaaaaaaaaaaaaaaaaaa);");
  5767. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5768. " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  5769. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
  5770. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5771. }
  5772. TEST_F(FormatTest, FormatsArrays) {
  5773. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5774. " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
  5775. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
  5776. " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
  5777. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
  5778. " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
  5779. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5780. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  5781. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5782. " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
  5783. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5784. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5785. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  5786. verifyFormat(
  5787. "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
  5788. " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5789. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  5790. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
  5791. " .aaaaaaaaaaaaaaaaaaaaaa();");
  5792. verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
  5793. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
  5794. verifyFormat(
  5795. "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
  5796. " .aaaaaaa[0]\n"
  5797. " .aaaaaaaaaaaaaaaaaaaaaa();");
  5798. verifyFormat("a[::b::c];");
  5799. verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
  5800. FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
  5801. verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
  5802. }
  5803. TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
  5804. verifyFormat("(a)->b();");
  5805. verifyFormat("--a;");
  5806. }
  5807. TEST_F(FormatTest, HandlesIncludeDirectives) {
  5808. verifyFormat("#include <string>\n"
  5809. "#include <a/b/c.h>\n"
  5810. "#include \"a/b/string\"\n"
  5811. "#include \"string.h\"\n"
  5812. "#include \"string.h\"\n"
  5813. "#include <a-a>\n"
  5814. "#include < path with space >\n"
  5815. "#include_next <test.h>"
  5816. "#include \"abc.h\" // this is included for ABC\n"
  5817. "#include \"some long include\" // with a comment\n"
  5818. "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
  5819. getLLVMStyleWithColumns(35));
  5820. EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\""));
  5821. EXPECT_EQ("#include <a>", format("#include<a>"));
  5822. verifyFormat("#import <string>");
  5823. verifyFormat("#import <a/b/c.h>");
  5824. verifyFormat("#import \"a/b/string\"");
  5825. verifyFormat("#import \"string.h\"");
  5826. verifyFormat("#import \"string.h\"");
  5827. verifyFormat("#if __has_include(<strstream>)\n"
  5828. "#include <strstream>\n"
  5829. "#endif");
  5830. verifyFormat("#define MY_IMPORT <a/b>");
  5831. // Protocol buffer definition or missing "#".
  5832. verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
  5833. getLLVMStyleWithColumns(30));
  5834. FormatStyle Style = getLLVMStyle();
  5835. Style.AlwaysBreakBeforeMultilineStrings = true;
  5836. Style.ColumnLimit = 0;
  5837. verifyFormat("#import \"abc.h\"", Style);
  5838. // But 'import' might also be a regular C++ namespace.
  5839. verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5840. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5841. }
  5842. //===----------------------------------------------------------------------===//
  5843. // Error recovery tests.
  5844. //===----------------------------------------------------------------------===//
  5845. TEST_F(FormatTest, IncompleteParameterLists) {
  5846. FormatStyle NoBinPacking = getLLVMStyle();
  5847. NoBinPacking.BinPackParameters = false;
  5848. verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
  5849. " double *min_x,\n"
  5850. " double *max_x,\n"
  5851. " double *min_y,\n"
  5852. " double *max_y,\n"
  5853. " double *min_z,\n"
  5854. " double *max_z, ) {}",
  5855. NoBinPacking);
  5856. }
  5857. TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
  5858. verifyFormat("void f() { return; }\n42");
  5859. verifyFormat("void f() {\n"
  5860. " if (0)\n"
  5861. " return;\n"
  5862. "}\n"
  5863. "42");
  5864. verifyFormat("void f() { return }\n42");
  5865. verifyFormat("void f() {\n"
  5866. " if (0)\n"
  5867. " return\n"
  5868. "}\n"
  5869. "42");
  5870. }
  5871. TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
  5872. EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
  5873. EXPECT_EQ("void f() {\n"
  5874. " if (a)\n"
  5875. " return\n"
  5876. "}",
  5877. format("void f ( ) { if ( a ) return }"));
  5878. EXPECT_EQ("namespace N {\n"
  5879. "void f()\n"
  5880. "}",
  5881. format("namespace N { void f() }"));
  5882. EXPECT_EQ("namespace N {\n"
  5883. "void f() {}\n"
  5884. "void g()\n"
  5885. "}",
  5886. format("namespace N { void f( ) { } void g( ) }"));
  5887. }
  5888. TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
  5889. verifyFormat("int aaaaaaaa =\n"
  5890. " // Overlylongcomment\n"
  5891. " b;",
  5892. getLLVMStyleWithColumns(20));
  5893. verifyFormat("function(\n"
  5894. " ShortArgument,\n"
  5895. " LoooooooooooongArgument);\n",
  5896. getLLVMStyleWithColumns(20));
  5897. }
  5898. TEST_F(FormatTest, IncorrectAccessSpecifier) {
  5899. verifyFormat("public:");
  5900. verifyFormat("class A {\n"
  5901. "public\n"
  5902. " void f() {}\n"
  5903. "};");
  5904. verifyFormat("public\n"
  5905. "int qwerty;");
  5906. verifyFormat("public\n"
  5907. "B {}");
  5908. verifyFormat("public\n"
  5909. "{}");
  5910. verifyFormat("public\n"
  5911. "B { int x; }");
  5912. }
  5913. TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
  5914. verifyFormat("{");
  5915. verifyFormat("#})");
  5916. verifyNoCrash("(/**/[:!] ?[).");
  5917. }
  5918. TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
  5919. verifyFormat("do {\n}");
  5920. verifyFormat("do {\n}\n"
  5921. "f();");
  5922. verifyFormat("do {\n}\n"
  5923. "wheeee(fun);");
  5924. verifyFormat("do {\n"
  5925. " f();\n"
  5926. "}");
  5927. }
  5928. TEST_F(FormatTest, IncorrectCodeMissingParens) {
  5929. verifyFormat("if {\n foo;\n foo();\n}");
  5930. verifyFormat("switch {\n foo;\n foo();\n}");
  5931. verifyIncompleteFormat("for {\n foo;\n foo();\n}");
  5932. verifyFormat("while {\n foo;\n foo();\n}");
  5933. verifyFormat("do {\n foo;\n foo();\n} while;");
  5934. }
  5935. TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
  5936. verifyIncompleteFormat("namespace {\n"
  5937. "class Foo { Foo (\n"
  5938. "};\n"
  5939. "} // comment");
  5940. }
  5941. TEST_F(FormatTest, IncorrectCodeErrorDetection) {
  5942. EXPECT_EQ("{\n {}\n", format("{\n{\n}\n"));
  5943. EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
  5944. EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
  5945. EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n"));
  5946. EXPECT_EQ("{\n"
  5947. " {\n"
  5948. " breakme(\n"
  5949. " qwe);\n"
  5950. " }\n",
  5951. format("{\n"
  5952. " {\n"
  5953. " breakme(qwe);\n"
  5954. "}\n",
  5955. getLLVMStyleWithColumns(10)));
  5956. }
  5957. TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
  5958. verifyFormat("int x = {\n"
  5959. " avariable,\n"
  5960. " b(alongervariable)};",
  5961. getLLVMStyleWithColumns(25));
  5962. }
  5963. TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
  5964. verifyFormat("return (a)(b){1, 2, 3};");
  5965. }
  5966. TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
  5967. verifyFormat("vector<int> x{1, 2, 3, 4};");
  5968. verifyFormat("vector<int> x{\n"
  5969. " 1, 2, 3, 4,\n"
  5970. "};");
  5971. verifyFormat("vector<T> x{{}, {}, {}, {}};");
  5972. verifyFormat("f({1, 2});");
  5973. verifyFormat("auto v = Foo{-1};");
  5974. verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
  5975. verifyFormat("Class::Class : member{1, 2, 3} {}");
  5976. verifyFormat("new vector<int>{1, 2, 3};");
  5977. verifyFormat("new int[3]{1, 2, 3};");
  5978. verifyFormat("new int{1};");
  5979. verifyFormat("return {arg1, arg2};");
  5980. verifyFormat("return {arg1, SomeType{parameter}};");
  5981. verifyFormat("int count = set<int>{f(), g(), h()}.size();");
  5982. verifyFormat("new T{arg1, arg2};");
  5983. verifyFormat("f(MyMap[{composite, key}]);");
  5984. verifyFormat("class Class {\n"
  5985. " T member = {arg1, arg2};\n"
  5986. "};");
  5987. verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
  5988. verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
  5989. verifyFormat("int a = std::is_integral<int>{} + 0;");
  5990. verifyFormat("int foo(int i) { return fo1{}(i); }");
  5991. verifyFormat("int foo(int i) { return fo1{}(i); }");
  5992. verifyFormat("auto i = decltype(x){};");
  5993. verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
  5994. verifyFormat("Node n{1, Node{1000}, //\n"
  5995. " 2};");
  5996. verifyFormat("Aaaa aaaaaaa{\n"
  5997. " {\n"
  5998. " aaaa,\n"
  5999. " },\n"
  6000. "};");
  6001. verifyFormat("class C : public D {\n"
  6002. " SomeClass SC{2};\n"
  6003. "};");
  6004. verifyFormat("class C : public A {\n"
  6005. " class D : public B {\n"
  6006. " void f() { int i{2}; }\n"
  6007. " };\n"
  6008. "};");
  6009. verifyFormat("#define A {a, a},");
  6010. // In combination with BinPackArguments = false.
  6011. FormatStyle NoBinPacking = getLLVMStyle();
  6012. NoBinPacking.BinPackArguments = false;
  6013. verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
  6014. " bbbbb,\n"
  6015. " ccccc,\n"
  6016. " ddddd,\n"
  6017. " eeeee,\n"
  6018. " ffffff,\n"
  6019. " ggggg,\n"
  6020. " hhhhhh,\n"
  6021. " iiiiii,\n"
  6022. " jjjjjj,\n"
  6023. " kkkkkk};",
  6024. NoBinPacking);
  6025. verifyFormat("const Aaaaaa aaaaa = {\n"
  6026. " aaaaa,\n"
  6027. " bbbbb,\n"
  6028. " ccccc,\n"
  6029. " ddddd,\n"
  6030. " eeeee,\n"
  6031. " ffffff,\n"
  6032. " ggggg,\n"
  6033. " hhhhhh,\n"
  6034. " iiiiii,\n"
  6035. " jjjjjj,\n"
  6036. " kkkkkk,\n"
  6037. "};",
  6038. NoBinPacking);
  6039. verifyFormat(
  6040. "const Aaaaaa aaaaa = {\n"
  6041. " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n"
  6042. " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n"
  6043. " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
  6044. "};",
  6045. NoBinPacking);
  6046. // FIXME: The alignment of these trailing comments might be bad. Then again,
  6047. // this might be utterly useless in real code.
  6048. verifyFormat("Constructor::Constructor()\n"
  6049. " : some_value{ //\n"
  6050. " aaaaaaa, //\n"
  6051. " bbbbbbb} {}");
  6052. // In braced lists, the first comment is always assumed to belong to the
  6053. // first element. Thus, it can be moved to the next or previous line as
  6054. // appropriate.
  6055. EXPECT_EQ("function({// First element:\n"
  6056. " 1,\n"
  6057. " // Second element:\n"
  6058. " 2});",
  6059. format("function({\n"
  6060. " // First element:\n"
  6061. " 1,\n"
  6062. " // Second element:\n"
  6063. " 2});"));
  6064. EXPECT_EQ("std::vector<int> MyNumbers{\n"
  6065. " // First element:\n"
  6066. " 1,\n"
  6067. " // Second element:\n"
  6068. " 2};",
  6069. format("std::vector<int> MyNumbers{// First element:\n"
  6070. " 1,\n"
  6071. " // Second element:\n"
  6072. " 2};",
  6073. getLLVMStyleWithColumns(30)));
  6074. // A trailing comma should still lead to an enforced line break.
  6075. EXPECT_EQ("vector<int> SomeVector = {\n"
  6076. " // aaa\n"
  6077. " 1, 2,\n"
  6078. "};",
  6079. format("vector<int> SomeVector = { // aaa\n"
  6080. " 1, 2, };"));
  6081. FormatStyle ExtraSpaces = getLLVMStyle();
  6082. ExtraSpaces.Cpp11BracedListStyle = false;
  6083. ExtraSpaces.ColumnLimit = 75;
  6084. verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
  6085. verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
  6086. verifyFormat("f({ 1, 2 });", ExtraSpaces);
  6087. verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
  6088. verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
  6089. verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
  6090. verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
  6091. verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
  6092. verifyFormat("return { arg1, arg2 };", ExtraSpaces);
  6093. verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
  6094. verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
  6095. verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
  6096. verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
  6097. verifyFormat("class Class {\n"
  6098. " T member = { arg1, arg2 };\n"
  6099. "};",
  6100. ExtraSpaces);
  6101. verifyFormat(
  6102. "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6103. " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
  6104. " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  6105. " bbbbbbbbbbbbbbbbbbbb, bbbbb };",
  6106. ExtraSpaces);
  6107. verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
  6108. verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
  6109. ExtraSpaces);
  6110. verifyFormat(
  6111. "someFunction(OtherParam,\n"
  6112. " BracedList{ // comment 1 (Forcing interesting break)\n"
  6113. " param1, param2,\n"
  6114. " // comment 2\n"
  6115. " param3, param4 });",
  6116. ExtraSpaces);
  6117. verifyFormat(
  6118. "std::this_thread::sleep_for(\n"
  6119. " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
  6120. ExtraSpaces);
  6121. verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
  6122. " aaaaaaa,\n"
  6123. " aaaaaaaaaa,\n"
  6124. " aaaaa,\n"
  6125. " aaaaaaaaaaaaaaa,\n"
  6126. " aaa,\n"
  6127. " aaaaaaaaaa,\n"
  6128. " a,\n"
  6129. " aaaaaaaaaaaaaaaaaaaaa,\n"
  6130. " aaaaaaaaaaaa,\n"
  6131. " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
  6132. " aaaaaaa,\n"
  6133. " a};");
  6134. verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
  6135. }
  6136. TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
  6137. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6138. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6139. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6140. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6141. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6142. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  6143. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
  6144. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6145. " 1, 22, 333, 4444, 55555, //\n"
  6146. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6147. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  6148. verifyFormat(
  6149. "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6150. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6151. " 1, 22, 333, 4444, 55555, 666666, // comment\n"
  6152. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  6153. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  6154. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  6155. " 7777777};");
  6156. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  6157. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  6158. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  6159. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  6160. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  6161. " // Separating comment.\n"
  6162. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  6163. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  6164. " // Leading comment\n"
  6165. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  6166. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  6167. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  6168. " 1, 1, 1, 1};",
  6169. getLLVMStyleWithColumns(39));
  6170. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  6171. " 1, 1, 1, 1};",
  6172. getLLVMStyleWithColumns(38));
  6173. verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
  6174. " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
  6175. getLLVMStyleWithColumns(43));
  6176. verifyFormat(
  6177. "static unsigned SomeValues[10][3] = {\n"
  6178. " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n"
  6179. " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
  6180. verifyFormat("static auto fields = new vector<string>{\n"
  6181. " \"aaaaaaaaaaaaa\",\n"
  6182. " \"aaaaaaaaaaaaa\",\n"
  6183. " \"aaaaaaaaaaaa\",\n"
  6184. " \"aaaaaaaaaaaaaa\",\n"
  6185. " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
  6186. " \"aaaaaaaaaaaa\",\n"
  6187. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
  6188. "};");
  6189. verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
  6190. verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
  6191. " 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
  6192. " 3, cccccccccccccccccccccc};",
  6193. getLLVMStyleWithColumns(60));
  6194. // Trailing commas.
  6195. verifyFormat("vector<int> x = {\n"
  6196. " 1, 1, 1, 1, 1, 1, 1, 1,\n"
  6197. "};",
  6198. getLLVMStyleWithColumns(39));
  6199. verifyFormat("vector<int> x = {\n"
  6200. " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
  6201. "};",
  6202. getLLVMStyleWithColumns(39));
  6203. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  6204. " 1, 1, 1, 1,\n"
  6205. " /**/ /**/};",
  6206. getLLVMStyleWithColumns(39));
  6207. // Trailing comment in the first line.
  6208. verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n"
  6209. " 1111111111, 2222222222, 33333333333, 4444444444, //\n"
  6210. " 111111111, 222222222, 3333333333, 444444444, //\n"
  6211. " 11111111, 22222222, 333333333, 44444444};");
  6212. // Trailing comment in the last line.
  6213. verifyFormat("int aaaaa[] = {\n"
  6214. " 1, 2, 3, // comment\n"
  6215. " 4, 5, 6 // comment\n"
  6216. "};");
  6217. // With nested lists, we should either format one item per line or all nested
  6218. // lists one on line.
  6219. // FIXME: For some nested lists, we can do better.
  6220. verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
  6221. " {aaaaaaaaaaaaaaaaaaa},\n"
  6222. " {aaaaaaaaaaaaaaaaaaaaa},\n"
  6223. " {aaaaaaaaaaaaaaaaa}};",
  6224. getLLVMStyleWithColumns(60));
  6225. verifyFormat(
  6226. "SomeStruct my_struct_array = {\n"
  6227. " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
  6228. " aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
  6229. " {aaa, aaa},\n"
  6230. " {aaa, aaa},\n"
  6231. " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
  6232. " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6233. " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
  6234. // No column layout should be used here.
  6235. verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
  6236. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
  6237. verifyNoCrash("a<,");
  6238. // No braced initializer here.
  6239. verifyFormat("void f() {\n"
  6240. " struct Dummy {};\n"
  6241. " f(v);\n"
  6242. "}");
  6243. // Long lists should be formatted in columns even if they are nested.
  6244. verifyFormat(
  6245. "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6246. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6247. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6248. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6249. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  6250. " 1, 22, 333, 4444, 55555, 666666, 7777777});");
  6251. }
  6252. TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
  6253. FormatStyle DoNotMerge = getLLVMStyle();
  6254. DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  6255. verifyFormat("void f() { return 42; }");
  6256. verifyFormat("void f() {\n"
  6257. " return 42;\n"
  6258. "}",
  6259. DoNotMerge);
  6260. verifyFormat("void f() {\n"
  6261. " // Comment\n"
  6262. "}");
  6263. verifyFormat("{\n"
  6264. "#error {\n"
  6265. " int a;\n"
  6266. "}");
  6267. verifyFormat("{\n"
  6268. " int a;\n"
  6269. "#error {\n"
  6270. "}");
  6271. verifyFormat("void f() {} // comment");
  6272. verifyFormat("void f() { int a; } // comment");
  6273. verifyFormat("void f() {\n"
  6274. "} // comment",
  6275. DoNotMerge);
  6276. verifyFormat("void f() {\n"
  6277. " int a;\n"
  6278. "} // comment",
  6279. DoNotMerge);
  6280. verifyFormat("void f() {\n"
  6281. "} // comment",
  6282. getLLVMStyleWithColumns(15));
  6283. verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
  6284. verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
  6285. verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
  6286. verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
  6287. verifyFormat("class C {\n"
  6288. " C()\n"
  6289. " : iiiiiiii(nullptr),\n"
  6290. " kkkkkkk(nullptr),\n"
  6291. " mmmmmmm(nullptr),\n"
  6292. " nnnnnnn(nullptr) {}\n"
  6293. "};",
  6294. getGoogleStyle());
  6295. FormatStyle NoColumnLimit = getLLVMStyle();
  6296. NoColumnLimit.ColumnLimit = 0;
  6297. EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
  6298. EXPECT_EQ("class C {\n"
  6299. " A() : b(0) {}\n"
  6300. "};",
  6301. format("class C{A():b(0){}};", NoColumnLimit));
  6302. EXPECT_EQ("A()\n"
  6303. " : b(0) {\n"
  6304. "}",
  6305. format("A()\n:b(0)\n{\n}", NoColumnLimit));
  6306. FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
  6307. DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
  6308. FormatStyle::SFS_None;
  6309. EXPECT_EQ("A()\n"
  6310. " : b(0) {\n"
  6311. "}",
  6312. format("A():b(0){}", DoNotMergeNoColumnLimit));
  6313. EXPECT_EQ("A()\n"
  6314. " : b(0) {\n"
  6315. "}",
  6316. format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
  6317. verifyFormat("#define A \\\n"
  6318. " void f() { \\\n"
  6319. " int i; \\\n"
  6320. " }",
  6321. getLLVMStyleWithColumns(20));
  6322. verifyFormat("#define A \\\n"
  6323. " void f() { int i; }",
  6324. getLLVMStyleWithColumns(21));
  6325. verifyFormat("#define A \\\n"
  6326. " void f() { \\\n"
  6327. " int i; \\\n"
  6328. " } \\\n"
  6329. " int j;",
  6330. getLLVMStyleWithColumns(22));
  6331. verifyFormat("#define A \\\n"
  6332. " void f() { int i; } \\\n"
  6333. " int j;",
  6334. getLLVMStyleWithColumns(23));
  6335. }
  6336. TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
  6337. FormatStyle MergeInlineOnly = getLLVMStyle();
  6338. MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  6339. verifyFormat("class C {\n"
  6340. " int f() { return 42; }\n"
  6341. "};",
  6342. MergeInlineOnly);
  6343. verifyFormat("int f() {\n"
  6344. " return 42;\n"
  6345. "}",
  6346. MergeInlineOnly);
  6347. }
  6348. TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
  6349. // Elaborate type variable declarations.
  6350. verifyFormat("struct foo a = {bar};\nint n;");
  6351. verifyFormat("class foo a = {bar};\nint n;");
  6352. verifyFormat("union foo a = {bar};\nint n;");
  6353. // Elaborate types inside function definitions.
  6354. verifyFormat("struct foo f() {}\nint n;");
  6355. verifyFormat("class foo f() {}\nint n;");
  6356. verifyFormat("union foo f() {}\nint n;");
  6357. // Templates.
  6358. verifyFormat("template <class X> void f() {}\nint n;");
  6359. verifyFormat("template <struct X> void f() {}\nint n;");
  6360. verifyFormat("template <union X> void f() {}\nint n;");
  6361. // Actual definitions...
  6362. verifyFormat("struct {\n} n;");
  6363. verifyFormat(
  6364. "template <template <class T, class Y>, class Z> class X {\n} n;");
  6365. verifyFormat("union Z {\n int n;\n} x;");
  6366. verifyFormat("class MACRO Z {\n} n;");
  6367. verifyFormat("class MACRO(X) Z {\n} n;");
  6368. verifyFormat("class __attribute__(X) Z {\n} n;");
  6369. verifyFormat("class __declspec(X) Z {\n} n;");
  6370. verifyFormat("class A##B##C {\n} n;");
  6371. verifyFormat("class alignas(16) Z {\n} n;");
  6372. verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
  6373. verifyFormat("class MACROA MACRO(X) Z {\n} n;");
  6374. // Redefinition from nested context:
  6375. verifyFormat("class A::B::C {\n} n;");
  6376. // Template definitions.
  6377. verifyFormat(
  6378. "template <typename F>\n"
  6379. "Matcher(const Matcher<F> &Other,\n"
  6380. " typename enable_if_c<is_base_of<F, T>::value &&\n"
  6381. " !is_same<F, T>::value>::type * = 0)\n"
  6382. " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
  6383. // FIXME: This is still incorrectly handled at the formatter side.
  6384. verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
  6385. verifyFormat("int i = SomeFunction(a<b, a> b);");
  6386. // FIXME:
  6387. // This now gets parsed incorrectly as class definition.
  6388. // verifyFormat("class A<int> f() {\n}\nint n;");
  6389. // Elaborate types where incorrectly parsing the structural element would
  6390. // break the indent.
  6391. verifyFormat("if (true)\n"
  6392. " class X x;\n"
  6393. "else\n"
  6394. " f();\n");
  6395. // This is simply incomplete. Formatting is not important, but must not crash.
  6396. verifyFormat("class A:");
  6397. }
  6398. TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
  6399. EXPECT_EQ("#error Leave all white!!!!! space* alone!\n",
  6400. format("#error Leave all white!!!!! space* alone!\n"));
  6401. EXPECT_EQ(
  6402. "#warning Leave all white!!!!! space* alone!\n",
  6403. format("#warning Leave all white!!!!! space* alone!\n"));
  6404. EXPECT_EQ("#error 1", format(" # error 1"));
  6405. EXPECT_EQ("#warning 1", format(" # warning 1"));
  6406. }
  6407. TEST_F(FormatTest, FormatHashIfExpressions) {
  6408. verifyFormat("#if AAAA && BBBB");
  6409. verifyFormat("#if (AAAA && BBBB)");
  6410. verifyFormat("#elif (AAAA && BBBB)");
  6411. // FIXME: Come up with a better indentation for #elif.
  6412. verifyFormat(
  6413. "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
  6414. " defined(BBBBBBBB)\n"
  6415. "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
  6416. " defined(BBBBBBBB)\n"
  6417. "#endif",
  6418. getLLVMStyleWithColumns(65));
  6419. }
  6420. TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
  6421. FormatStyle AllowsMergedIf = getGoogleStyle();
  6422. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  6423. verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
  6424. verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
  6425. verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
  6426. EXPECT_EQ("if (true) return 42;",
  6427. format("if (true)\nreturn 42;", AllowsMergedIf));
  6428. FormatStyle ShortMergedIf = AllowsMergedIf;
  6429. ShortMergedIf.ColumnLimit = 25;
  6430. verifyFormat("#define A \\\n"
  6431. " if (true) return 42;",
  6432. ShortMergedIf);
  6433. verifyFormat("#define A \\\n"
  6434. " f(); \\\n"
  6435. " if (true)\n"
  6436. "#define B",
  6437. ShortMergedIf);
  6438. verifyFormat("#define A \\\n"
  6439. " f(); \\\n"
  6440. " if (true)\n"
  6441. "g();",
  6442. ShortMergedIf);
  6443. verifyFormat("{\n"
  6444. "#ifdef A\n"
  6445. " // Comment\n"
  6446. " if (true) continue;\n"
  6447. "#endif\n"
  6448. " // Comment\n"
  6449. " if (true) continue;\n"
  6450. "}",
  6451. ShortMergedIf);
  6452. ShortMergedIf.ColumnLimit = 29;
  6453. verifyFormat("#define A \\\n"
  6454. " if (aaaaaaaaaa) return 1; \\\n"
  6455. " return 2;",
  6456. ShortMergedIf);
  6457. ShortMergedIf.ColumnLimit = 28;
  6458. verifyFormat("#define A \\\n"
  6459. " if (aaaaaaaaaa) \\\n"
  6460. " return 1; \\\n"
  6461. " return 2;",
  6462. ShortMergedIf);
  6463. }
  6464. TEST_F(FormatTest, BlockCommentsInControlLoops) {
  6465. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  6466. " f();\n"
  6467. "}");
  6468. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  6469. " f();\n"
  6470. "} /* another comment */ else /* comment #3 */ {\n"
  6471. " g();\n"
  6472. "}");
  6473. verifyFormat("while (0) /* a comment in a strange place */ {\n"
  6474. " f();\n"
  6475. "}");
  6476. verifyFormat("for (;;) /* a comment in a strange place */ {\n"
  6477. " f();\n"
  6478. "}");
  6479. verifyFormat("do /* a comment in a strange place */ {\n"
  6480. " f();\n"
  6481. "} /* another comment */ while (0);");
  6482. }
  6483. TEST_F(FormatTest, BlockComments) {
  6484. EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
  6485. format("/* *//* */ /* */\n/* *//* */ /* */"));
  6486. EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
  6487. EXPECT_EQ("#define A /*123*/ \\\n"
  6488. " b\n"
  6489. "/* */\n"
  6490. "someCall(\n"
  6491. " parameter);",
  6492. format("#define A /*123*/ b\n"
  6493. "/* */\n"
  6494. "someCall(parameter);",
  6495. getLLVMStyleWithColumns(15)));
  6496. EXPECT_EQ("#define A\n"
  6497. "/* */ someCall(\n"
  6498. " parameter);",
  6499. format("#define A\n"
  6500. "/* */someCall(parameter);",
  6501. getLLVMStyleWithColumns(15)));
  6502. EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
  6503. EXPECT_EQ("/*\n"
  6504. "*\n"
  6505. " * aaaaaa\n"
  6506. " * aaaaaa\n"
  6507. "*/",
  6508. format("/*\n"
  6509. "*\n"
  6510. " * aaaaaa aaaaaa\n"
  6511. "*/",
  6512. getLLVMStyleWithColumns(10)));
  6513. EXPECT_EQ("/*\n"
  6514. "**\n"
  6515. "* aaaaaa\n"
  6516. "*aaaaaa\n"
  6517. "*/",
  6518. format("/*\n"
  6519. "**\n"
  6520. "* aaaaaa aaaaaa\n"
  6521. "*/",
  6522. getLLVMStyleWithColumns(10)));
  6523. FormatStyle NoBinPacking = getLLVMStyle();
  6524. NoBinPacking.BinPackParameters = false;
  6525. EXPECT_EQ("someFunction(1, /* comment 1 */\n"
  6526. " 2, /* comment 2 */\n"
  6527. " 3, /* comment 3 */\n"
  6528. " aaaa,\n"
  6529. " bbbb);",
  6530. format("someFunction (1, /* comment 1 */\n"
  6531. " 2, /* comment 2 */ \n"
  6532. " 3, /* comment 3 */\n"
  6533. "aaaa, bbbb );",
  6534. NoBinPacking));
  6535. verifyFormat(
  6536. "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  6537. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  6538. EXPECT_EQ(
  6539. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  6540. " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  6541. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
  6542. format(
  6543. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  6544. " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  6545. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
  6546. EXPECT_EQ(
  6547. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  6548. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  6549. "int cccccccccccccccccccccccccccccc; /* comment */\n",
  6550. format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  6551. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  6552. "int cccccccccccccccccccccccccccccc; /* comment */\n"));
  6553. verifyFormat("void f(int * /* unused */) {}");
  6554. EXPECT_EQ("/*\n"
  6555. " **\n"
  6556. " */",
  6557. format("/*\n"
  6558. " **\n"
  6559. " */"));
  6560. EXPECT_EQ("/*\n"
  6561. " *q\n"
  6562. " */",
  6563. format("/*\n"
  6564. " *q\n"
  6565. " */"));
  6566. EXPECT_EQ("/*\n"
  6567. " * q\n"
  6568. " */",
  6569. format("/*\n"
  6570. " * q\n"
  6571. " */"));
  6572. EXPECT_EQ("/*\n"
  6573. " **/",
  6574. format("/*\n"
  6575. " **/"));
  6576. EXPECT_EQ("/*\n"
  6577. " ***/",
  6578. format("/*\n"
  6579. " ***/"));
  6580. }
  6581. TEST_F(FormatTest, BlockCommentsInMacros) {
  6582. EXPECT_EQ("#define A \\\n"
  6583. " { \\\n"
  6584. " /* one line */ \\\n"
  6585. " someCall();",
  6586. format("#define A { \\\n"
  6587. " /* one line */ \\\n"
  6588. " someCall();",
  6589. getLLVMStyleWithColumns(20)));
  6590. EXPECT_EQ("#define A \\\n"
  6591. " { \\\n"
  6592. " /* previous */ \\\n"
  6593. " /* one line */ \\\n"
  6594. " someCall();",
  6595. format("#define A { \\\n"
  6596. " /* previous */ \\\n"
  6597. " /* one line */ \\\n"
  6598. " someCall();",
  6599. getLLVMStyleWithColumns(20)));
  6600. }
  6601. TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
  6602. EXPECT_EQ("a = {\n"
  6603. " 1111 /* */\n"
  6604. "};",
  6605. format("a = {1111 /* */\n"
  6606. "};",
  6607. getLLVMStyleWithColumns(15)));
  6608. EXPECT_EQ("a = {\n"
  6609. " 1111 /* */\n"
  6610. "};",
  6611. format("a = {1111 /* */\n"
  6612. "};",
  6613. getLLVMStyleWithColumns(15)));
  6614. // FIXME: The formatting is still wrong here.
  6615. EXPECT_EQ("a = {\n"
  6616. " 1111 /* a\n"
  6617. " */\n"
  6618. "};",
  6619. format("a = {1111 /* a */\n"
  6620. "};",
  6621. getLLVMStyleWithColumns(15)));
  6622. }
  6623. TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
  6624. verifyFormat("{\n"
  6625. " // a\n"
  6626. " // b");
  6627. }
  6628. TEST_F(FormatTest, FormatStarDependingOnContext) {
  6629. verifyFormat("void f(int *a);");
  6630. verifyFormat("void f() { f(fint * b); }");
  6631. verifyFormat("class A {\n void f(int *a);\n};");
  6632. verifyFormat("class A {\n int *a;\n};");
  6633. verifyFormat("namespace a {\n"
  6634. "namespace b {\n"
  6635. "class A {\n"
  6636. " void f() {}\n"
  6637. " int *a;\n"
  6638. "};\n"
  6639. "}\n"
  6640. "}");
  6641. }
  6642. TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
  6643. verifyFormat("while");
  6644. verifyFormat("operator");
  6645. }
  6646. //===----------------------------------------------------------------------===//
  6647. // Objective-C tests.
  6648. //===----------------------------------------------------------------------===//
  6649. TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
  6650. verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
  6651. EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
  6652. format("-(NSUInteger)indexOfObject:(id)anObject;"));
  6653. EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
  6654. EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
  6655. EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
  6656. format("-(NSInteger)Method3:(id)anObject;"));
  6657. EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
  6658. format("-(NSInteger)Method4:(id)anObject;"));
  6659. EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
  6660. format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
  6661. EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
  6662. format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
  6663. EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
  6664. "forAllCells:(BOOL)flag;",
  6665. format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
  6666. "forAllCells:(BOOL)flag;"));
  6667. // Very long objectiveC method declaration.
  6668. verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
  6669. " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
  6670. verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
  6671. " inRange:(NSRange)range\n"
  6672. " outRange:(NSRange)out_range\n"
  6673. " outRange1:(NSRange)out_range1\n"
  6674. " outRange2:(NSRange)out_range2\n"
  6675. " outRange3:(NSRange)out_range3\n"
  6676. " outRange4:(NSRange)out_range4\n"
  6677. " outRange5:(NSRange)out_range5\n"
  6678. " outRange6:(NSRange)out_range6\n"
  6679. " outRange7:(NSRange)out_range7\n"
  6680. " outRange8:(NSRange)out_range8\n"
  6681. " outRange9:(NSRange)out_range9;");
  6682. // When the function name has to be wrapped.
  6683. FormatStyle Style = getLLVMStyle();
  6684. Style.IndentWrappedFunctionNames = false;
  6685. verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
  6686. "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
  6687. " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
  6688. "}",
  6689. Style);
  6690. Style.IndentWrappedFunctionNames = true;
  6691. verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
  6692. " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
  6693. " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
  6694. "}",
  6695. Style);
  6696. verifyFormat("- (int)sum:(vector<int>)numbers;");
  6697. verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
  6698. // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
  6699. // protocol lists (but not for template classes):
  6700. // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
  6701. verifyFormat("- (int (*)())foo:(int (*)())f;");
  6702. verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
  6703. // If there's no return type (very rare in practice!), LLVM and Google style
  6704. // agree.
  6705. verifyFormat("- foo;");
  6706. verifyFormat("- foo:(int)f;");
  6707. verifyGoogleFormat("- foo:(int)foo;");
  6708. }
  6709. TEST_F(FormatTest, FormatObjCInterface) {
  6710. verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
  6711. "@public\n"
  6712. " int field1;\n"
  6713. "@protected\n"
  6714. " int field2;\n"
  6715. "@private\n"
  6716. " int field3;\n"
  6717. "@package\n"
  6718. " int field4;\n"
  6719. "}\n"
  6720. "+ (id)init;\n"
  6721. "@end");
  6722. verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
  6723. " @public\n"
  6724. " int field1;\n"
  6725. " @protected\n"
  6726. " int field2;\n"
  6727. " @private\n"
  6728. " int field3;\n"
  6729. " @package\n"
  6730. " int field4;\n"
  6731. "}\n"
  6732. "+ (id)init;\n"
  6733. "@end");
  6734. verifyFormat("@interface /* wait for it */ Foo\n"
  6735. "+ (id)init;\n"
  6736. "// Look, a comment!\n"
  6737. "- (int)answerWith:(int)i;\n"
  6738. "@end");
  6739. verifyFormat("@interface Foo\n"
  6740. "@end\n"
  6741. "@interface Bar\n"
  6742. "@end");
  6743. verifyFormat("@interface Foo : Bar\n"
  6744. "+ (id)init;\n"
  6745. "@end");
  6746. verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
  6747. "+ (id)init;\n"
  6748. "@end");
  6749. verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
  6750. "+ (id)init;\n"
  6751. "@end");
  6752. verifyFormat("@interface Foo (HackStuff)\n"
  6753. "+ (id)init;\n"
  6754. "@end");
  6755. verifyFormat("@interface Foo ()\n"
  6756. "+ (id)init;\n"
  6757. "@end");
  6758. verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
  6759. "+ (id)init;\n"
  6760. "@end");
  6761. verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
  6762. "+ (id)init;\n"
  6763. "@end");
  6764. verifyFormat("@interface Foo {\n"
  6765. " int _i;\n"
  6766. "}\n"
  6767. "+ (id)init;\n"
  6768. "@end");
  6769. verifyFormat("@interface Foo : Bar {\n"
  6770. " int _i;\n"
  6771. "}\n"
  6772. "+ (id)init;\n"
  6773. "@end");
  6774. verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
  6775. " int _i;\n"
  6776. "}\n"
  6777. "+ (id)init;\n"
  6778. "@end");
  6779. verifyFormat("@interface Foo (HackStuff) {\n"
  6780. " int _i;\n"
  6781. "}\n"
  6782. "+ (id)init;\n"
  6783. "@end");
  6784. verifyFormat("@interface Foo () {\n"
  6785. " int _i;\n"
  6786. "}\n"
  6787. "+ (id)init;\n"
  6788. "@end");
  6789. verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
  6790. " int _i;\n"
  6791. "}\n"
  6792. "+ (id)init;\n"
  6793. "@end");
  6794. FormatStyle OnePerLine = getGoogleStyle();
  6795. OnePerLine.BinPackParameters = false;
  6796. verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n"
  6797. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6798. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6799. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6800. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
  6801. "}",
  6802. OnePerLine);
  6803. }
  6804. TEST_F(FormatTest, FormatObjCImplementation) {
  6805. verifyFormat("@implementation Foo : NSObject {\n"
  6806. "@public\n"
  6807. " int field1;\n"
  6808. "@protected\n"
  6809. " int field2;\n"
  6810. "@private\n"
  6811. " int field3;\n"
  6812. "@package\n"
  6813. " int field4;\n"
  6814. "}\n"
  6815. "+ (id)init {\n}\n"
  6816. "@end");
  6817. verifyGoogleFormat("@implementation Foo : NSObject {\n"
  6818. " @public\n"
  6819. " int field1;\n"
  6820. " @protected\n"
  6821. " int field2;\n"
  6822. " @private\n"
  6823. " int field3;\n"
  6824. " @package\n"
  6825. " int field4;\n"
  6826. "}\n"
  6827. "+ (id)init {\n}\n"
  6828. "@end");
  6829. verifyFormat("@implementation Foo\n"
  6830. "+ (id)init {\n"
  6831. " if (true)\n"
  6832. " return nil;\n"
  6833. "}\n"
  6834. "// Look, a comment!\n"
  6835. "- (int)answerWith:(int)i {\n"
  6836. " return i;\n"
  6837. "}\n"
  6838. "+ (int)answerWith:(int)i {\n"
  6839. " return i;\n"
  6840. "}\n"
  6841. "@end");
  6842. verifyFormat("@implementation Foo\n"
  6843. "@end\n"
  6844. "@implementation Bar\n"
  6845. "@end");
  6846. EXPECT_EQ("@implementation Foo : Bar\n"
  6847. "+ (id)init {\n}\n"
  6848. "- (void)foo {\n}\n"
  6849. "@end",
  6850. format("@implementation Foo : Bar\n"
  6851. "+(id)init{}\n"
  6852. "-(void)foo{}\n"
  6853. "@end"));
  6854. verifyFormat("@implementation Foo {\n"
  6855. " int _i;\n"
  6856. "}\n"
  6857. "+ (id)init {\n}\n"
  6858. "@end");
  6859. verifyFormat("@implementation Foo : Bar {\n"
  6860. " int _i;\n"
  6861. "}\n"
  6862. "+ (id)init {\n}\n"
  6863. "@end");
  6864. verifyFormat("@implementation Foo (HackStuff)\n"
  6865. "+ (id)init {\n}\n"
  6866. "@end");
  6867. verifyFormat("@implementation ObjcClass\n"
  6868. "- (void)method;\n"
  6869. "{}\n"
  6870. "@end");
  6871. }
  6872. TEST_F(FormatTest, FormatObjCProtocol) {
  6873. verifyFormat("@protocol Foo\n"
  6874. "@property(weak) id delegate;\n"
  6875. "- (NSUInteger)numberOfThings;\n"
  6876. "@end");
  6877. verifyFormat("@protocol MyProtocol <NSObject>\n"
  6878. "- (NSUInteger)numberOfThings;\n"
  6879. "@end");
  6880. verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
  6881. "- (NSUInteger)numberOfThings;\n"
  6882. "@end");
  6883. verifyFormat("@protocol Foo;\n"
  6884. "@protocol Bar;\n");
  6885. verifyFormat("@protocol Foo\n"
  6886. "@end\n"
  6887. "@protocol Bar\n"
  6888. "@end");
  6889. verifyFormat("@protocol myProtocol\n"
  6890. "- (void)mandatoryWithInt:(int)i;\n"
  6891. "@optional\n"
  6892. "- (void)optional;\n"
  6893. "@required\n"
  6894. "- (void)required;\n"
  6895. "@optional\n"
  6896. "@property(assign) int madProp;\n"
  6897. "@end\n");
  6898. verifyFormat("@property(nonatomic, assign, readonly)\n"
  6899. " int *looooooooooooooooooooooooooooongNumber;\n"
  6900. "@property(nonatomic, assign, readonly)\n"
  6901. " NSString *looooooooooooooooooooooooooooongName;");
  6902. verifyFormat("@implementation PR18406\n"
  6903. "}\n"
  6904. "@end");
  6905. }
  6906. TEST_F(FormatTest, FormatObjCMethodDeclarations) {
  6907. verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
  6908. " rect:(NSRect)theRect\n"
  6909. " interval:(float)theInterval {\n"
  6910. "}");
  6911. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6912. " longKeyword:(NSRect)theRect\n"
  6913. " longerKeyword:(float)theInterval\n"
  6914. " error:(NSError **)theError {\n"
  6915. "}");
  6916. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6917. " longKeyword:(NSRect)theRect\n"
  6918. " evenLongerKeyword:(float)theInterval\n"
  6919. " error:(NSError **)theError {\n"
  6920. "}");
  6921. verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
  6922. " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
  6923. " NS_DESIGNATED_INITIALIZER;",
  6924. getLLVMStyleWithColumns(60));
  6925. // Continuation indent width should win over aligning colons if the function
  6926. // name is long.
  6927. FormatStyle continuationStyle = getGoogleStyle();
  6928. continuationStyle.ColumnLimit = 40;
  6929. continuationStyle.IndentWrappedFunctionNames = true;
  6930. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6931. " dontAlignNamef:(NSRect)theRect {\n"
  6932. "}",
  6933. continuationStyle);
  6934. // Make sure we don't break aligning for short parameter names.
  6935. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6936. " aShortf:(NSRect)theRect {\n"
  6937. "}",
  6938. continuationStyle);
  6939. }
  6940. TEST_F(FormatTest, FormatObjCMethodExpr) {
  6941. verifyFormat("[foo bar:baz];");
  6942. verifyFormat("return [foo bar:baz];");
  6943. verifyFormat("return (a)[foo bar:baz];");
  6944. verifyFormat("f([foo bar:baz]);");
  6945. verifyFormat("f(2, [foo bar:baz]);");
  6946. verifyFormat("f(2, a ? b : c);");
  6947. verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
  6948. // Unary operators.
  6949. verifyFormat("int a = +[foo bar:baz];");
  6950. verifyFormat("int a = -[foo bar:baz];");
  6951. verifyFormat("int a = ![foo bar:baz];");
  6952. verifyFormat("int a = ~[foo bar:baz];");
  6953. verifyFormat("int a = ++[foo bar:baz];");
  6954. verifyFormat("int a = --[foo bar:baz];");
  6955. verifyFormat("int a = sizeof [foo bar:baz];");
  6956. verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
  6957. verifyFormat("int a = &[foo bar:baz];");
  6958. verifyFormat("int a = *[foo bar:baz];");
  6959. // FIXME: Make casts work, without breaking f()[4].
  6960. // verifyFormat("int a = (int)[foo bar:baz];");
  6961. // verifyFormat("return (int)[foo bar:baz];");
  6962. // verifyFormat("(void)[foo bar:baz];");
  6963. verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
  6964. // Binary operators.
  6965. verifyFormat("[foo bar:baz], [foo bar:baz];");
  6966. verifyFormat("[foo bar:baz] = [foo bar:baz];");
  6967. verifyFormat("[foo bar:baz] *= [foo bar:baz];");
  6968. verifyFormat("[foo bar:baz] /= [foo bar:baz];");
  6969. verifyFormat("[foo bar:baz] %= [foo bar:baz];");
  6970. verifyFormat("[foo bar:baz] += [foo bar:baz];");
  6971. verifyFormat("[foo bar:baz] -= [foo bar:baz];");
  6972. verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
  6973. verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
  6974. verifyFormat("[foo bar:baz] &= [foo bar:baz];");
  6975. verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
  6976. verifyFormat("[foo bar:baz] |= [foo bar:baz];");
  6977. verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
  6978. verifyFormat("[foo bar:baz] || [foo bar:baz];");
  6979. verifyFormat("[foo bar:baz] && [foo bar:baz];");
  6980. verifyFormat("[foo bar:baz] | [foo bar:baz];");
  6981. verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
  6982. verifyFormat("[foo bar:baz] & [foo bar:baz];");
  6983. verifyFormat("[foo bar:baz] == [foo bar:baz];");
  6984. verifyFormat("[foo bar:baz] != [foo bar:baz];");
  6985. verifyFormat("[foo bar:baz] >= [foo bar:baz];");
  6986. verifyFormat("[foo bar:baz] <= [foo bar:baz];");
  6987. verifyFormat("[foo bar:baz] > [foo bar:baz];");
  6988. verifyFormat("[foo bar:baz] < [foo bar:baz];");
  6989. verifyFormat("[foo bar:baz] >> [foo bar:baz];");
  6990. verifyFormat("[foo bar:baz] << [foo bar:baz];");
  6991. verifyFormat("[foo bar:baz] - [foo bar:baz];");
  6992. verifyFormat("[foo bar:baz] + [foo bar:baz];");
  6993. verifyFormat("[foo bar:baz] * [foo bar:baz];");
  6994. verifyFormat("[foo bar:baz] / [foo bar:baz];");
  6995. verifyFormat("[foo bar:baz] % [foo bar:baz];");
  6996. // Whew!
  6997. verifyFormat("return in[42];");
  6998. verifyFormat("for (auto v : in[1]) {\n}");
  6999. verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
  7000. verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
  7001. verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
  7002. verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
  7003. verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
  7004. verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
  7005. "}");
  7006. verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
  7007. verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
  7008. verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
  7009. verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
  7010. verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
  7011. verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
  7012. verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
  7013. verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
  7014. verifyFormat("[button setAction:@selector(zoomOut:)];");
  7015. verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
  7016. verifyFormat("arr[[self indexForFoo:a]];");
  7017. verifyFormat("throw [self errorFor:a];");
  7018. verifyFormat("@throw [self errorFor:a];");
  7019. verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
  7020. verifyFormat("[(id)foo bar:(id) ? baz : quux];");
  7021. verifyFormat("4 > 4 ? (id)a : (id)baz;");
  7022. // This tests that the formatter doesn't break after "backing" but before ":",
  7023. // which would be at 80 columns.
  7024. verifyFormat(
  7025. "void f() {\n"
  7026. " if ((self = [super initWithContentRect:contentRect\n"
  7027. " styleMask:styleMask ?: otherMask\n"
  7028. " backing:NSBackingStoreBuffered\n"
  7029. " defer:YES]))");
  7030. verifyFormat(
  7031. "[foo checkThatBreakingAfterColonWorksOk:\n"
  7032. " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
  7033. verifyFormat("[myObj short:arg1 // Force line break\n"
  7034. " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
  7035. " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
  7036. " error:arg4];");
  7037. verifyFormat(
  7038. "void f() {\n"
  7039. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  7040. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  7041. " pos.width(), pos.height())\n"
  7042. " styleMask:NSBorderlessWindowMask\n"
  7043. " backing:NSBackingStoreBuffered\n"
  7044. " defer:NO]);\n"
  7045. "}");
  7046. verifyFormat(
  7047. "void f() {\n"
  7048. " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
  7049. " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
  7050. " pos.width(), pos.height())\n"
  7051. " syeMask:NSBorderlessWindowMask\n"
  7052. " bking:NSBackingStoreBuffered\n"
  7053. " der:NO]);\n"
  7054. "}",
  7055. getLLVMStyleWithColumns(70));
  7056. verifyFormat(
  7057. "void f() {\n"
  7058. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  7059. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  7060. " pos.width(), pos.height())\n"
  7061. " styleMask:NSBorderlessWindowMask\n"
  7062. " backing:NSBackingStoreBuffered\n"
  7063. " defer:NO]);\n"
  7064. "}",
  7065. getChromiumStyle(FormatStyle::LK_Cpp));
  7066. verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
  7067. " with:contentsNativeView];");
  7068. verifyFormat(
  7069. "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
  7070. " owner:nillllll];");
  7071. verifyFormat(
  7072. "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
  7073. " forType:kBookmarkButtonDragType];");
  7074. verifyFormat("[defaultCenter addObserver:self\n"
  7075. " selector:@selector(willEnterFullscreen)\n"
  7076. " name:kWillEnterFullscreenNotification\n"
  7077. " object:nil];");
  7078. verifyFormat("[image_rep drawInRect:drawRect\n"
  7079. " fromRect:NSZeroRect\n"
  7080. " operation:NSCompositeCopy\n"
  7081. " fraction:1.0\n"
  7082. " respectFlipped:NO\n"
  7083. " hints:nil];");
  7084. verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7085. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  7086. verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  7087. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  7088. verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
  7089. " aaaaaaaaaaaaaaaaaaaaaa];");
  7090. verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
  7091. " .aaaaaaaa];", // FIXME: Indentation seems off.
  7092. getLLVMStyleWithColumns(60));
  7093. verifyFormat(
  7094. "scoped_nsobject<NSTextField> message(\n"
  7095. " // The frame will be fixed up when |-setMessageText:| is called.\n"
  7096. " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
  7097. verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
  7098. " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
  7099. " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
  7100. " aaaa:bbb];");
  7101. verifyFormat("[self param:function( //\n"
  7102. " parameter)]");
  7103. verifyFormat(
  7104. "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  7105. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  7106. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
  7107. // FIXME: This violates the column limit.
  7108. verifyFormat(
  7109. "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7110. " aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
  7111. " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];",
  7112. getLLVMStyleWithColumns(60));
  7113. // Variadic parameters.
  7114. verifyFormat(
  7115. "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
  7116. verifyFormat(
  7117. "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  7118. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  7119. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
  7120. verifyFormat("[self // break\n"
  7121. " a:a\n"
  7122. " aaa:aaa];");
  7123. verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
  7124. " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
  7125. }
  7126. TEST_F(FormatTest, ObjCAt) {
  7127. verifyFormat("@autoreleasepool");
  7128. verifyFormat("@catch");
  7129. verifyFormat("@class");
  7130. verifyFormat("@compatibility_alias");
  7131. verifyFormat("@defs");
  7132. verifyFormat("@dynamic");
  7133. verifyFormat("@encode");
  7134. verifyFormat("@end");
  7135. verifyFormat("@finally");
  7136. verifyFormat("@implementation");
  7137. verifyFormat("@import");
  7138. verifyFormat("@interface");
  7139. verifyFormat("@optional");
  7140. verifyFormat("@package");
  7141. verifyFormat("@private");
  7142. verifyFormat("@property");
  7143. verifyFormat("@protected");
  7144. verifyFormat("@protocol");
  7145. verifyFormat("@public");
  7146. verifyFormat("@required");
  7147. verifyFormat("@selector");
  7148. verifyFormat("@synchronized");
  7149. verifyFormat("@synthesize");
  7150. verifyFormat("@throw");
  7151. verifyFormat("@try");
  7152. EXPECT_EQ("@interface", format("@ interface"));
  7153. // The precise formatting of this doesn't matter, nobody writes code like
  7154. // this.
  7155. verifyFormat("@ /*foo*/ interface");
  7156. }
  7157. TEST_F(FormatTest, ObjCSnippets) {
  7158. verifyFormat("@autoreleasepool {\n"
  7159. " foo();\n"
  7160. "}");
  7161. verifyFormat("@class Foo, Bar;");
  7162. verifyFormat("@compatibility_alias AliasName ExistingClass;");
  7163. verifyFormat("@dynamic textColor;");
  7164. verifyFormat("char *buf1 = @encode(int *);");
  7165. verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
  7166. verifyFormat("char *buf1 = @encode(int **);");
  7167. verifyFormat("Protocol *proto = @protocol(p1);");
  7168. verifyFormat("SEL s = @selector(foo:);");
  7169. verifyFormat("@synchronized(self) {\n"
  7170. " f();\n"
  7171. "}");
  7172. verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  7173. verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  7174. verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
  7175. verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
  7176. verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
  7177. verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
  7178. getMozillaStyle());
  7179. verifyFormat("@property BOOL editable;", getMozillaStyle());
  7180. verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
  7181. getWebKitStyle());
  7182. verifyFormat("@property BOOL editable;", getWebKitStyle());
  7183. verifyFormat("@import foo.bar;\n"
  7184. "@import baz;");
  7185. }
  7186. TEST_F(FormatTest, ObjCForIn) {
  7187. verifyFormat("- (void)test {\n"
  7188. " for (NSString *n in arrayOfStrings) {\n"
  7189. " foo(n);\n"
  7190. " }\n"
  7191. "}");
  7192. verifyFormat("- (void)test {\n"
  7193. " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n"
  7194. " foo(n);\n"
  7195. " }\n"
  7196. "}");
  7197. }
  7198. TEST_F(FormatTest, ObjCLiterals) {
  7199. verifyFormat("@\"String\"");
  7200. verifyFormat("@1");
  7201. verifyFormat("@+4.8");
  7202. verifyFormat("@-4");
  7203. verifyFormat("@1LL");
  7204. verifyFormat("@.5");
  7205. verifyFormat("@'c'");
  7206. verifyFormat("@true");
  7207. verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
  7208. verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
  7209. verifyFormat("NSNumber *favoriteColor = @(Green);");
  7210. verifyFormat("NSString *path = @(getenv(\"PATH\"));");
  7211. verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
  7212. }
  7213. TEST_F(FormatTest, ObjCDictLiterals) {
  7214. verifyFormat("@{");
  7215. verifyFormat("@{}");
  7216. verifyFormat("@{@\"one\" : @1}");
  7217. verifyFormat("return @{@\"one\" : @1;");
  7218. verifyFormat("@{@\"one\" : @1}");
  7219. verifyFormat("@{@\"one\" : @{@2 : @1}}");
  7220. verifyFormat("@{\n"
  7221. " @\"one\" : @{@2 : @1},\n"
  7222. "}");
  7223. verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
  7224. verifyIncompleteFormat("[self setDict:@{}");
  7225. verifyIncompleteFormat("[self setDict:@{@1 : @2}");
  7226. verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
  7227. verifyFormat(
  7228. "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
  7229. verifyFormat(
  7230. "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
  7231. verifyFormat("NSDictionary *d = @{\n"
  7232. " @\"nam\" : NSUserNam(),\n"
  7233. " @\"dte\" : [NSDate date],\n"
  7234. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  7235. "};");
  7236. verifyFormat(
  7237. "@{\n"
  7238. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  7239. "regularFont,\n"
  7240. "};");
  7241. verifyGoogleFormat(
  7242. "@{\n"
  7243. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  7244. "regularFont,\n"
  7245. "};");
  7246. verifyFormat(
  7247. "@{\n"
  7248. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
  7249. " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
  7250. "};");
  7251. // We should try to be robust in case someone forgets the "@".
  7252. verifyFormat("NSDictionary *d = {\n"
  7253. " @\"nam\" : NSUserNam(),\n"
  7254. " @\"dte\" : [NSDate date],\n"
  7255. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  7256. "};");
  7257. verifyFormat("NSMutableDictionary *dictionary =\n"
  7258. " [NSMutableDictionary dictionaryWithDictionary:@{\n"
  7259. " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
  7260. " bbbbbbbbbbbbbbbbbb : bbbbb,\n"
  7261. " cccccccccccccccc : ccccccccccccccc\n"
  7262. " }];");
  7263. // Ensure that casts before the key are kept on the same line as the key.
  7264. verifyFormat(
  7265. "NSDictionary *d = @{\n"
  7266. " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n"
  7267. " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n"
  7268. "};");
  7269. }
  7270. TEST_F(FormatTest, ObjCArrayLiterals) {
  7271. verifyIncompleteFormat("@[");
  7272. verifyFormat("@[]");
  7273. verifyFormat(
  7274. "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
  7275. verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
  7276. verifyFormat("NSArray *array = @[ [foo description] ];");
  7277. verifyFormat(
  7278. "NSArray *some_variable = @[\n"
  7279. " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
  7280. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7281. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7282. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7283. "];");
  7284. verifyFormat(
  7285. "NSArray *some_variable = @[\n"
  7286. " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
  7287. " @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n"
  7288. "];");
  7289. verifyFormat("NSArray *some_variable = @[\n"
  7290. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7291. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7292. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7293. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7294. "];");
  7295. verifyFormat("NSArray *array = @[\n"
  7296. " @\"a\",\n"
  7297. " @\"a\",\n" // Trailing comma -> one per line.
  7298. "];");
  7299. // We should try to be robust in case someone forgets the "@".
  7300. verifyFormat("NSArray *some_variable = [\n"
  7301. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7302. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7303. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7304. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7305. "];");
  7306. verifyFormat(
  7307. "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
  7308. " index:(NSUInteger)index\n"
  7309. " nonDigitAttributes:\n"
  7310. " (NSDictionary *)noDigitAttributes;");
  7311. verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
  7312. " NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
  7313. "]];");
  7314. }
  7315. TEST_F(FormatTest, BreaksStringLiterals) {
  7316. EXPECT_EQ("\"some text \"\n"
  7317. "\"other\";",
  7318. format("\"some text other\";", getLLVMStyleWithColumns(12)));
  7319. EXPECT_EQ("\"some text \"\n"
  7320. "\"other\";",
  7321. format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
  7322. EXPECT_EQ(
  7323. "#define A \\\n"
  7324. " \"some \" \\\n"
  7325. " \"text \" \\\n"
  7326. " \"other\";",
  7327. format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
  7328. EXPECT_EQ(
  7329. "#define A \\\n"
  7330. " \"so \" \\\n"
  7331. " \"text \" \\\n"
  7332. " \"other\";",
  7333. format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
  7334. EXPECT_EQ("\"some text\"",
  7335. format("\"some text\"", getLLVMStyleWithColumns(1)));
  7336. EXPECT_EQ("\"some text\"",
  7337. format("\"some text\"", getLLVMStyleWithColumns(11)));
  7338. EXPECT_EQ("\"some \"\n"
  7339. "\"text\"",
  7340. format("\"some text\"", getLLVMStyleWithColumns(10)));
  7341. EXPECT_EQ("\"some \"\n"
  7342. "\"text\"",
  7343. format("\"some text\"", getLLVMStyleWithColumns(7)));
  7344. EXPECT_EQ("\"some\"\n"
  7345. "\" tex\"\n"
  7346. "\"t\"",
  7347. format("\"some text\"", getLLVMStyleWithColumns(6)));
  7348. EXPECT_EQ("\"some\"\n"
  7349. "\" tex\"\n"
  7350. "\" and\"",
  7351. format("\"some tex and\"", getLLVMStyleWithColumns(6)));
  7352. EXPECT_EQ("\"some\"\n"
  7353. "\"/tex\"\n"
  7354. "\"/and\"",
  7355. format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
  7356. EXPECT_EQ("variable =\n"
  7357. " \"long string \"\n"
  7358. " \"literal\";",
  7359. format("variable = \"long string literal\";",
  7360. getLLVMStyleWithColumns(20)));
  7361. EXPECT_EQ("variable = f(\n"
  7362. " \"long string \"\n"
  7363. " \"literal\",\n"
  7364. " short,\n"
  7365. " loooooooooooooooooooong);",
  7366. format("variable = f(\"long string literal\", short, "
  7367. "loooooooooooooooooooong);",
  7368. getLLVMStyleWithColumns(20)));
  7369. EXPECT_EQ(
  7370. "f(g(\"long string \"\n"
  7371. " \"literal\"),\n"
  7372. " b);",
  7373. format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
  7374. EXPECT_EQ("f(g(\"long string \"\n"
  7375. " \"literal\",\n"
  7376. " a),\n"
  7377. " b);",
  7378. format("f(g(\"long string literal\", a), b);",
  7379. getLLVMStyleWithColumns(20)));
  7380. EXPECT_EQ(
  7381. "f(\"one two\".split(\n"
  7382. " variable));",
  7383. format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
  7384. EXPECT_EQ("f(\"one two three four five six \"\n"
  7385. " \"seven\".split(\n"
  7386. " really_looooong_variable));",
  7387. format("f(\"one two three four five six seven\"."
  7388. "split(really_looooong_variable));",
  7389. getLLVMStyleWithColumns(33)));
  7390. EXPECT_EQ("f(\"some \"\n"
  7391. " \"text\",\n"
  7392. " other);",
  7393. format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
  7394. // Only break as a last resort.
  7395. verifyFormat(
  7396. "aaaaaaaaaaaaaaaaaaaa(\n"
  7397. " aaaaaaaaaaaaaaaaaaaa,\n"
  7398. " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
  7399. EXPECT_EQ("\"splitmea\"\n"
  7400. "\"trandomp\"\n"
  7401. "\"oint\"",
  7402. format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
  7403. EXPECT_EQ("\"split/\"\n"
  7404. "\"pathat/\"\n"
  7405. "\"slashes\"",
  7406. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  7407. EXPECT_EQ("\"split/\"\n"
  7408. "\"pathat/\"\n"
  7409. "\"slashes\"",
  7410. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  7411. EXPECT_EQ("\"split at \"\n"
  7412. "\"spaces/at/\"\n"
  7413. "\"slashes.at.any$\"\n"
  7414. "\"non-alphanumeric%\"\n"
  7415. "\"1111111111characte\"\n"
  7416. "\"rs\"",
  7417. format("\"split at "
  7418. "spaces/at/"
  7419. "slashes.at."
  7420. "any$non-"
  7421. "alphanumeric%"
  7422. "1111111111characte"
  7423. "rs\"",
  7424. getLLVMStyleWithColumns(20)));
  7425. // Verify that splitting the strings understands
  7426. // Style::AlwaysBreakBeforeMultilineStrings.
  7427. EXPECT_EQ(
  7428. "aaaaaaaaaaaa(\n"
  7429. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
  7430. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
  7431. format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
  7432. "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
  7433. "aaaaaaaaaaaaaaaaaaaaaa\");",
  7434. getGoogleStyle()));
  7435. EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  7436. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
  7437. format("return \"aaaaaaaaaaaaaaaaaaaaaa "
  7438. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
  7439. "aaaaaaaaaaaaaaaaaaaaaa\";",
  7440. getGoogleStyle()));
  7441. EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  7442. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  7443. format("llvm::outs() << "
  7444. "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
  7445. "aaaaaaaaaaaaaaaaaaa\";"));
  7446. EXPECT_EQ("ffff(\n"
  7447. " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  7448. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  7449. format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  7450. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  7451. getGoogleStyle()));
  7452. FormatStyle Style = getLLVMStyleWithColumns(12);
  7453. Style.BreakStringLiterals = false;
  7454. EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
  7455. FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
  7456. AlignLeft.AlignEscapedNewlinesLeft = true;
  7457. EXPECT_EQ("#define A \\\n"
  7458. " \"some \" \\\n"
  7459. " \"text \" \\\n"
  7460. " \"other\";",
  7461. format("#define A \"some text other\";", AlignLeft));
  7462. }
  7463. TEST_F(FormatTest, FullyRemoveEmptyLines) {
  7464. FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
  7465. NoEmptyLines.MaxEmptyLinesToKeep = 0;
  7466. EXPECT_EQ("int i = a(b());",
  7467. format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
  7468. }
  7469. TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
  7470. EXPECT_EQ(
  7471. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  7472. "(\n"
  7473. " \"x\t\");",
  7474. format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  7475. "aaaaaaa("
  7476. "\"x\t\");"));
  7477. }
  7478. TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
  7479. EXPECT_EQ(
  7480. "u8\"utf8 string \"\n"
  7481. "u8\"literal\";",
  7482. format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
  7483. EXPECT_EQ(
  7484. "u\"utf16 string \"\n"
  7485. "u\"literal\";",
  7486. format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
  7487. EXPECT_EQ(
  7488. "U\"utf32 string \"\n"
  7489. "U\"literal\";",
  7490. format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
  7491. EXPECT_EQ("L\"wide string \"\n"
  7492. "L\"literal\";",
  7493. format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
  7494. EXPECT_EQ("@\"NSString \"\n"
  7495. "@\"literal\";",
  7496. format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
  7497. // This input makes clang-format try to split the incomplete unicode escape
  7498. // sequence, which used to lead to a crasher.
  7499. verifyNoCrash(
  7500. "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  7501. getLLVMStyleWithColumns(60));
  7502. }
  7503. TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
  7504. FormatStyle Style = getGoogleStyleWithColumns(15);
  7505. EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
  7506. EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
  7507. EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
  7508. EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
  7509. EXPECT_EQ("u8R\"x(raw literal)x\";",
  7510. format("u8R\"x(raw literal)x\";", Style));
  7511. }
  7512. TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
  7513. FormatStyle Style = getLLVMStyleWithColumns(20);
  7514. EXPECT_EQ(
  7515. "_T(\"aaaaaaaaaaaaaa\")\n"
  7516. "_T(\"aaaaaaaaaaaaaa\")\n"
  7517. "_T(\"aaaaaaaaaaaa\")",
  7518. format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
  7519. EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
  7520. " _T(\"aaaaaa\"),\n"
  7521. " z);",
  7522. format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
  7523. // FIXME: Handle embedded spaces in one iteration.
  7524. // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
  7525. // "_T(\"aaaaaaaaaaaaa\")\n"
  7526. // "_T(\"aaaaaaaaaaaaa\")\n"
  7527. // "_T(\"a\")",
  7528. // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  7529. // getLLVMStyleWithColumns(20)));
  7530. EXPECT_EQ(
  7531. "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  7532. format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
  7533. EXPECT_EQ("f(\n"
  7534. "#if !TEST\n"
  7535. " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
  7536. "#endif\n"
  7537. " );",
  7538. format("f(\n"
  7539. "#if !TEST\n"
  7540. "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
  7541. "#endif\n"
  7542. ");"));
  7543. EXPECT_EQ("f(\n"
  7544. "\n"
  7545. " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
  7546. format("f(\n"
  7547. "\n"
  7548. "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
  7549. }
  7550. TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
  7551. EXPECT_EQ(
  7552. "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7553. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7554. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  7555. format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7556. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7557. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
  7558. }
  7559. TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
  7560. EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
  7561. format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle()));
  7562. EXPECT_EQ("fffffffffff(g(R\"x(\n"
  7563. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7564. ")x\",\n"
  7565. " a),\n"
  7566. " b);",
  7567. format("fffffffffff(g(R\"x(\n"
  7568. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7569. ")x\", a), b);",
  7570. getGoogleStyleWithColumns(20)));
  7571. EXPECT_EQ("fffffffffff(\n"
  7572. " g(R\"x(qqq\n"
  7573. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7574. ")x\",\n"
  7575. " a),\n"
  7576. " b);",
  7577. format("fffffffffff(g(R\"x(qqq\n"
  7578. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7579. ")x\", a), b);",
  7580. getGoogleStyleWithColumns(20)));
  7581. EXPECT_EQ("fffffffffff(R\"x(\n"
  7582. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7583. ")x\");",
  7584. format("fffffffffff(R\"x(\n"
  7585. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7586. ")x\");",
  7587. getGoogleStyleWithColumns(20)));
  7588. EXPECT_EQ("fffffffffff(R\"x(\n"
  7589. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7590. ")x\" + bbbbbb);",
  7591. format("fffffffffff(R\"x(\n"
  7592. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7593. ")x\" + bbbbbb);",
  7594. getGoogleStyleWithColumns(20)));
  7595. EXPECT_EQ("fffffffffff(\n"
  7596. " R\"x(\n"
  7597. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7598. ")x\" +\n"
  7599. " bbbbbb);",
  7600. format("fffffffffff(\n"
  7601. " R\"x(\n"
  7602. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7603. ")x\" + bbbbbb);",
  7604. getGoogleStyleWithColumns(20)));
  7605. }
  7606. TEST_F(FormatTest, SkipsUnknownStringLiterals) {
  7607. verifyFormat("string a = \"unterminated;");
  7608. EXPECT_EQ("function(\"unterminated,\n"
  7609. " OtherParameter);",
  7610. format("function( \"unterminated,\n"
  7611. " OtherParameter);"));
  7612. }
  7613. TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
  7614. FormatStyle Style = getLLVMStyle();
  7615. Style.Standard = FormatStyle::LS_Cpp03;
  7616. EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
  7617. format("#define x(_a) printf(\"foo\"_a);", Style));
  7618. }
  7619. TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
  7620. TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
  7621. EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
  7622. " \"ddeeefff\");",
  7623. format("someFunction(\"aaabbbcccdddeeefff\");",
  7624. getLLVMStyleWithColumns(25)));
  7625. EXPECT_EQ("someFunction1234567890(\n"
  7626. " \"aaabbbcccdddeeefff\");",
  7627. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  7628. getLLVMStyleWithColumns(26)));
  7629. EXPECT_EQ("someFunction1234567890(\n"
  7630. " \"aaabbbcccdddeeeff\"\n"
  7631. " \"f\");",
  7632. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  7633. getLLVMStyleWithColumns(25)));
  7634. EXPECT_EQ("someFunction1234567890(\n"
  7635. " \"aaabbbcccdddeeeff\"\n"
  7636. " \"f\");",
  7637. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  7638. getLLVMStyleWithColumns(24)));
  7639. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  7640. " \"ddde \"\n"
  7641. " \"efff\");",
  7642. format("someFunction(\"aaabbbcc ddde efff\");",
  7643. getLLVMStyleWithColumns(25)));
  7644. EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
  7645. " \"ddeeefff\");",
  7646. format("someFunction(\"aaabbbccc ddeeefff\");",
  7647. getLLVMStyleWithColumns(25)));
  7648. EXPECT_EQ("someFunction1234567890(\n"
  7649. " \"aaabb \"\n"
  7650. " \"cccdddeeefff\");",
  7651. format("someFunction1234567890(\"aaabb cccdddeeefff\");",
  7652. getLLVMStyleWithColumns(25)));
  7653. EXPECT_EQ("#define A \\\n"
  7654. " string s = \\\n"
  7655. " \"123456789\" \\\n"
  7656. " \"0\"; \\\n"
  7657. " int i;",
  7658. format("#define A string s = \"1234567890\"; int i;",
  7659. getLLVMStyleWithColumns(20)));
  7660. // FIXME: Put additional penalties on breaking at non-whitespace locations.
  7661. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  7662. " \"dddeeeff\"\n"
  7663. " \"f\");",
  7664. format("someFunction(\"aaabbbcc dddeeefff\");",
  7665. getLLVMStyleWithColumns(25)));
  7666. }
  7667. TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
  7668. EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
  7669. EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
  7670. EXPECT_EQ("\"test\"\n"
  7671. "\"\\n\"",
  7672. format("\"test\\n\"", getLLVMStyleWithColumns(7)));
  7673. EXPECT_EQ("\"tes\\\\\"\n"
  7674. "\"n\"",
  7675. format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
  7676. EXPECT_EQ("\"\\\\\\\\\"\n"
  7677. "\"\\n\"",
  7678. format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
  7679. EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
  7680. EXPECT_EQ("\"\\uff01\"\n"
  7681. "\"test\"",
  7682. format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
  7683. EXPECT_EQ("\"\\Uff01ff02\"",
  7684. format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
  7685. EXPECT_EQ("\"\\x000000000001\"\n"
  7686. "\"next\"",
  7687. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
  7688. EXPECT_EQ("\"\\x000000000001next\"",
  7689. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
  7690. EXPECT_EQ("\"\\x000000000001\"",
  7691. format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
  7692. EXPECT_EQ("\"test\"\n"
  7693. "\"\\000000\"\n"
  7694. "\"000001\"",
  7695. format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
  7696. EXPECT_EQ("\"test\\000\"\n"
  7697. "\"00000000\"\n"
  7698. "\"1\"",
  7699. format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
  7700. }
  7701. TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
  7702. verifyFormat("void f() {\n"
  7703. " return g() {}\n"
  7704. " void h() {}");
  7705. verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
  7706. "g();\n"
  7707. "}");
  7708. }
  7709. TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
  7710. verifyFormat(
  7711. "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
  7712. }
  7713. TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
  7714. verifyFormat("class X {\n"
  7715. " void f() {\n"
  7716. " }\n"
  7717. "};",
  7718. getLLVMStyleWithColumns(12));
  7719. }
  7720. TEST_F(FormatTest, ConfigurableIndentWidth) {
  7721. FormatStyle EightIndent = getLLVMStyleWithColumns(18);
  7722. EightIndent.IndentWidth = 8;
  7723. EightIndent.ContinuationIndentWidth = 8;
  7724. verifyFormat("void f() {\n"
  7725. " someFunction();\n"
  7726. " if (true) {\n"
  7727. " f();\n"
  7728. " }\n"
  7729. "}",
  7730. EightIndent);
  7731. verifyFormat("class X {\n"
  7732. " void f() {\n"
  7733. " }\n"
  7734. "};",
  7735. EightIndent);
  7736. verifyFormat("int x[] = {\n"
  7737. " call(),\n"
  7738. " call()};",
  7739. EightIndent);
  7740. }
  7741. TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
  7742. verifyFormat("double\n"
  7743. "f();",
  7744. getLLVMStyleWithColumns(8));
  7745. }
  7746. TEST_F(FormatTest, ConfigurableUseOfTab) {
  7747. FormatStyle Tab = getLLVMStyleWithColumns(42);
  7748. Tab.IndentWidth = 8;
  7749. Tab.UseTab = FormatStyle::UT_Always;
  7750. Tab.AlignEscapedNewlinesLeft = true;
  7751. EXPECT_EQ("if (aaaaaaaa && // q\n"
  7752. " bb)\t\t// w\n"
  7753. "\t;",
  7754. format("if (aaaaaaaa &&// q\n"
  7755. "bb)// w\n"
  7756. ";",
  7757. Tab));
  7758. EXPECT_EQ("if (aaa && bbb) // w\n"
  7759. "\t;",
  7760. format("if(aaa&&bbb)// w\n"
  7761. ";",
  7762. Tab));
  7763. verifyFormat("class X {\n"
  7764. "\tvoid f() {\n"
  7765. "\t\tsomeFunction(parameter1,\n"
  7766. "\t\t\t parameter2);\n"
  7767. "\t}\n"
  7768. "};",
  7769. Tab);
  7770. verifyFormat("#define A \\\n"
  7771. "\tvoid f() { \\\n"
  7772. "\t\tsomeFunction( \\\n"
  7773. "\t\t parameter1, \\\n"
  7774. "\t\t parameter2); \\\n"
  7775. "\t}",
  7776. Tab);
  7777. Tab.TabWidth = 4;
  7778. Tab.IndentWidth = 8;
  7779. verifyFormat("class TabWidth4Indent8 {\n"
  7780. "\t\tvoid f() {\n"
  7781. "\t\t\t\tsomeFunction(parameter1,\n"
  7782. "\t\t\t\t\t\t\t parameter2);\n"
  7783. "\t\t}\n"
  7784. "};",
  7785. Tab);
  7786. Tab.TabWidth = 4;
  7787. Tab.IndentWidth = 4;
  7788. verifyFormat("class TabWidth4Indent4 {\n"
  7789. "\tvoid f() {\n"
  7790. "\t\tsomeFunction(parameter1,\n"
  7791. "\t\t\t\t\t parameter2);\n"
  7792. "\t}\n"
  7793. "};",
  7794. Tab);
  7795. Tab.TabWidth = 8;
  7796. Tab.IndentWidth = 4;
  7797. verifyFormat("class TabWidth8Indent4 {\n"
  7798. " void f() {\n"
  7799. "\tsomeFunction(parameter1,\n"
  7800. "\t\t parameter2);\n"
  7801. " }\n"
  7802. "};",
  7803. Tab);
  7804. Tab.TabWidth = 8;
  7805. Tab.IndentWidth = 8;
  7806. EXPECT_EQ("/*\n"
  7807. "\t a\t\tcomment\n"
  7808. "\t in multiple lines\n"
  7809. " */",
  7810. format(" /*\t \t \n"
  7811. " \t \t a\t\tcomment\t \t\n"
  7812. " \t \t in multiple lines\t\n"
  7813. " \t */",
  7814. Tab));
  7815. Tab.UseTab = FormatStyle::UT_ForIndentation;
  7816. verifyFormat("{\n"
  7817. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7818. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7819. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7820. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7821. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7822. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7823. "};",
  7824. Tab);
  7825. verifyFormat("enum AA {\n"
  7826. "\ta1, // Force multiple lines\n"
  7827. "\ta2,\n"
  7828. "\ta3\n"
  7829. "};",
  7830. Tab);
  7831. EXPECT_EQ("if (aaaaaaaa && // q\n"
  7832. " bb) // w\n"
  7833. "\t;",
  7834. format("if (aaaaaaaa &&// q\n"
  7835. "bb)// w\n"
  7836. ";",
  7837. Tab));
  7838. verifyFormat("class X {\n"
  7839. "\tvoid f() {\n"
  7840. "\t\tsomeFunction(parameter1,\n"
  7841. "\t\t parameter2);\n"
  7842. "\t}\n"
  7843. "};",
  7844. Tab);
  7845. verifyFormat("{\n"
  7846. "\tQ(\n"
  7847. "\t {\n"
  7848. "\t\t int a;\n"
  7849. "\t\t someFunction(aaaaaaaa,\n"
  7850. "\t\t bbbbbbb);\n"
  7851. "\t },\n"
  7852. "\t p);\n"
  7853. "}",
  7854. Tab);
  7855. EXPECT_EQ("{\n"
  7856. "\t/* aaaa\n"
  7857. "\t bbbb */\n"
  7858. "}",
  7859. format("{\n"
  7860. "/* aaaa\n"
  7861. " bbbb */\n"
  7862. "}",
  7863. Tab));
  7864. EXPECT_EQ("{\n"
  7865. "\t/*\n"
  7866. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7867. "\t bbbbbbbbbbbbb\n"
  7868. "\t*/\n"
  7869. "}",
  7870. format("{\n"
  7871. "/*\n"
  7872. " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7873. "*/\n"
  7874. "}",
  7875. Tab));
  7876. EXPECT_EQ("{\n"
  7877. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7878. "\t// bbbbbbbbbbbbb\n"
  7879. "}",
  7880. format("{\n"
  7881. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7882. "}",
  7883. Tab));
  7884. EXPECT_EQ("{\n"
  7885. "\t/*\n"
  7886. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7887. "\t bbbbbbbbbbbbb\n"
  7888. "\t*/\n"
  7889. "}",
  7890. format("{\n"
  7891. "\t/*\n"
  7892. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7893. "\t*/\n"
  7894. "}",
  7895. Tab));
  7896. EXPECT_EQ("{\n"
  7897. "\t/*\n"
  7898. "\n"
  7899. "\t*/\n"
  7900. "}",
  7901. format("{\n"
  7902. "\t/*\n"
  7903. "\n"
  7904. "\t*/\n"
  7905. "}",
  7906. Tab));
  7907. EXPECT_EQ("{\n"
  7908. "\t/*\n"
  7909. " asdf\n"
  7910. "\t*/\n"
  7911. "}",
  7912. format("{\n"
  7913. "\t/*\n"
  7914. " asdf\n"
  7915. "\t*/\n"
  7916. "}",
  7917. Tab));
  7918. Tab.UseTab = FormatStyle::UT_Never;
  7919. EXPECT_EQ("/*\n"
  7920. " a\t\tcomment\n"
  7921. " in multiple lines\n"
  7922. " */",
  7923. format(" /*\t \t \n"
  7924. " \t \t a\t\tcomment\t \t\n"
  7925. " \t \t in multiple lines\t\n"
  7926. " \t */",
  7927. Tab));
  7928. EXPECT_EQ("/* some\n"
  7929. " comment */",
  7930. format(" \t \t /* some\n"
  7931. " \t \t comment */",
  7932. Tab));
  7933. EXPECT_EQ("int a; /* some\n"
  7934. " comment */",
  7935. format(" \t \t int a; /* some\n"
  7936. " \t \t comment */",
  7937. Tab));
  7938. EXPECT_EQ("int a; /* some\n"
  7939. "comment */",
  7940. format(" \t \t int\ta; /* some\n"
  7941. " \t \t comment */",
  7942. Tab));
  7943. EXPECT_EQ("f(\"\t\t\"); /* some\n"
  7944. " comment */",
  7945. format(" \t \t f(\"\t\t\"); /* some\n"
  7946. " \t \t comment */",
  7947. Tab));
  7948. EXPECT_EQ("{\n"
  7949. " /*\n"
  7950. " * Comment\n"
  7951. " */\n"
  7952. " int i;\n"
  7953. "}",
  7954. format("{\n"
  7955. "\t/*\n"
  7956. "\t * Comment\n"
  7957. "\t */\n"
  7958. "\t int i;\n"
  7959. "}"));
  7960. Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
  7961. Tab.TabWidth = 8;
  7962. Tab.IndentWidth = 8;
  7963. EXPECT_EQ("if (aaaaaaaa && // q\n"
  7964. " bb) // w\n"
  7965. "\t;",
  7966. format("if (aaaaaaaa &&// q\n"
  7967. "bb)// w\n"
  7968. ";",
  7969. Tab));
  7970. EXPECT_EQ("if (aaa && bbb) // w\n"
  7971. "\t;",
  7972. format("if(aaa&&bbb)// w\n"
  7973. ";",
  7974. Tab));
  7975. verifyFormat("class X {\n"
  7976. "\tvoid f() {\n"
  7977. "\t\tsomeFunction(parameter1,\n"
  7978. "\t\t\t parameter2);\n"
  7979. "\t}\n"
  7980. "};",
  7981. Tab);
  7982. verifyFormat("#define A \\\n"
  7983. "\tvoid f() { \\\n"
  7984. "\t\tsomeFunction( \\\n"
  7985. "\t\t parameter1, \\\n"
  7986. "\t\t parameter2); \\\n"
  7987. "\t}",
  7988. Tab);
  7989. Tab.TabWidth = 4;
  7990. Tab.IndentWidth = 8;
  7991. verifyFormat("class TabWidth4Indent8 {\n"
  7992. "\t\tvoid f() {\n"
  7993. "\t\t\t\tsomeFunction(parameter1,\n"
  7994. "\t\t\t\t\t\t\t parameter2);\n"
  7995. "\t\t}\n"
  7996. "};",
  7997. Tab);
  7998. Tab.TabWidth = 4;
  7999. Tab.IndentWidth = 4;
  8000. verifyFormat("class TabWidth4Indent4 {\n"
  8001. "\tvoid f() {\n"
  8002. "\t\tsomeFunction(parameter1,\n"
  8003. "\t\t\t\t\t parameter2);\n"
  8004. "\t}\n"
  8005. "};",
  8006. Tab);
  8007. Tab.TabWidth = 8;
  8008. Tab.IndentWidth = 4;
  8009. verifyFormat("class TabWidth8Indent4 {\n"
  8010. " void f() {\n"
  8011. "\tsomeFunction(parameter1,\n"
  8012. "\t\t parameter2);\n"
  8013. " }\n"
  8014. "};",
  8015. Tab);
  8016. Tab.TabWidth = 8;
  8017. Tab.IndentWidth = 8;
  8018. EXPECT_EQ("/*\n"
  8019. "\t a\t\tcomment\n"
  8020. "\t in multiple lines\n"
  8021. " */",
  8022. format(" /*\t \t \n"
  8023. " \t \t a\t\tcomment\t \t\n"
  8024. " \t \t in multiple lines\t\n"
  8025. " \t */",
  8026. Tab));
  8027. verifyFormat("{\n"
  8028. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  8029. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  8030. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  8031. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  8032. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  8033. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  8034. "};",
  8035. Tab);
  8036. verifyFormat("enum AA {\n"
  8037. "\ta1, // Force multiple lines\n"
  8038. "\ta2,\n"
  8039. "\ta3\n"
  8040. "};",
  8041. Tab);
  8042. EXPECT_EQ("if (aaaaaaaa && // q\n"
  8043. " bb) // w\n"
  8044. "\t;",
  8045. format("if (aaaaaaaa &&// q\n"
  8046. "bb)// w\n"
  8047. ";",
  8048. Tab));
  8049. verifyFormat("class X {\n"
  8050. "\tvoid f() {\n"
  8051. "\t\tsomeFunction(parameter1,\n"
  8052. "\t\t\t parameter2);\n"
  8053. "\t}\n"
  8054. "};",
  8055. Tab);
  8056. verifyFormat("{\n"
  8057. "\tQ(\n"
  8058. "\t {\n"
  8059. "\t\t int a;\n"
  8060. "\t\t someFunction(aaaaaaaa,\n"
  8061. "\t\t\t\t bbbbbbb);\n"
  8062. "\t },\n"
  8063. "\t p);\n"
  8064. "}",
  8065. Tab);
  8066. EXPECT_EQ("{\n"
  8067. "\t/* aaaa\n"
  8068. "\t bbbb */\n"
  8069. "}",
  8070. format("{\n"
  8071. "/* aaaa\n"
  8072. " bbbb */\n"
  8073. "}",
  8074. Tab));
  8075. EXPECT_EQ("{\n"
  8076. "\t/*\n"
  8077. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  8078. "\t bbbbbbbbbbbbb\n"
  8079. "\t*/\n"
  8080. "}",
  8081. format("{\n"
  8082. "/*\n"
  8083. " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  8084. "*/\n"
  8085. "}",
  8086. Tab));
  8087. EXPECT_EQ("{\n"
  8088. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  8089. "\t// bbbbbbbbbbbbb\n"
  8090. "}",
  8091. format("{\n"
  8092. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  8093. "}",
  8094. Tab));
  8095. EXPECT_EQ("{\n"
  8096. "\t/*\n"
  8097. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  8098. "\t bbbbbbbbbbbbb\n"
  8099. "\t*/\n"
  8100. "}",
  8101. format("{\n"
  8102. "\t/*\n"
  8103. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  8104. "\t*/\n"
  8105. "}",
  8106. Tab));
  8107. EXPECT_EQ("{\n"
  8108. "\t/*\n"
  8109. "\n"
  8110. "\t*/\n"
  8111. "}",
  8112. format("{\n"
  8113. "\t/*\n"
  8114. "\n"
  8115. "\t*/\n"
  8116. "}",
  8117. Tab));
  8118. EXPECT_EQ("{\n"
  8119. "\t/*\n"
  8120. " asdf\n"
  8121. "\t*/\n"
  8122. "}",
  8123. format("{\n"
  8124. "\t/*\n"
  8125. " asdf\n"
  8126. "\t*/\n"
  8127. "}",
  8128. Tab));
  8129. EXPECT_EQ("/*\n"
  8130. "\t a\t\tcomment\n"
  8131. "\t in multiple lines\n"
  8132. " */",
  8133. format(" /*\t \t \n"
  8134. " \t \t a\t\tcomment\t \t\n"
  8135. " \t \t in multiple lines\t\n"
  8136. " \t */",
  8137. Tab));
  8138. EXPECT_EQ("/* some\n"
  8139. " comment */",
  8140. format(" \t \t /* some\n"
  8141. " \t \t comment */",
  8142. Tab));
  8143. EXPECT_EQ("int a; /* some\n"
  8144. " comment */",
  8145. format(" \t \t int a; /* some\n"
  8146. " \t \t comment */",
  8147. Tab));
  8148. EXPECT_EQ("int a; /* some\n"
  8149. "comment */",
  8150. format(" \t \t int\ta; /* some\n"
  8151. " \t \t comment */",
  8152. Tab));
  8153. EXPECT_EQ("f(\"\t\t\"); /* some\n"
  8154. " comment */",
  8155. format(" \t \t f(\"\t\t\"); /* some\n"
  8156. " \t \t comment */",
  8157. Tab));
  8158. EXPECT_EQ("{\n"
  8159. " /*\n"
  8160. " * Comment\n"
  8161. " */\n"
  8162. " int i;\n"
  8163. "}",
  8164. format("{\n"
  8165. "\t/*\n"
  8166. "\t * Comment\n"
  8167. "\t */\n"
  8168. "\t int i;\n"
  8169. "}"));
  8170. Tab.AlignConsecutiveAssignments = true;
  8171. Tab.AlignConsecutiveDeclarations = true;
  8172. Tab.TabWidth = 4;
  8173. Tab.IndentWidth = 4;
  8174. verifyFormat("class Assign {\n"
  8175. "\tvoid f() {\n"
  8176. "\t\tint x = 123;\n"
  8177. "\t\tint random = 4;\n"
  8178. "\t\tstd::string alphabet =\n"
  8179. "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
  8180. "\t}\n"
  8181. "};",
  8182. Tab);
  8183. }
  8184. TEST_F(FormatTest, CalculatesOriginalColumn) {
  8185. EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  8186. "q\"; /* some\n"
  8187. " comment */",
  8188. format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  8189. "q\"; /* some\n"
  8190. " comment */",
  8191. getLLVMStyle()));
  8192. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  8193. "/* some\n"
  8194. " comment */",
  8195. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  8196. " /* some\n"
  8197. " comment */",
  8198. getLLVMStyle()));
  8199. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  8200. "qqq\n"
  8201. "/* some\n"
  8202. " comment */",
  8203. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  8204. "qqq\n"
  8205. " /* some\n"
  8206. " comment */",
  8207. getLLVMStyle()));
  8208. EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  8209. "wwww; /* some\n"
  8210. " comment */",
  8211. format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  8212. "wwww; /* some\n"
  8213. " comment */",
  8214. getLLVMStyle()));
  8215. }
  8216. TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
  8217. FormatStyle NoSpace = getLLVMStyle();
  8218. NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
  8219. verifyFormat("while(true)\n"
  8220. " continue;",
  8221. NoSpace);
  8222. verifyFormat("for(;;)\n"
  8223. " continue;",
  8224. NoSpace);
  8225. verifyFormat("if(true)\n"
  8226. " f();\n"
  8227. "else if(true)\n"
  8228. " f();",
  8229. NoSpace);
  8230. verifyFormat("do {\n"
  8231. " do_something();\n"
  8232. "} while(something());",
  8233. NoSpace);
  8234. verifyFormat("switch(x) {\n"
  8235. "default:\n"
  8236. " break;\n"
  8237. "}",
  8238. NoSpace);
  8239. verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
  8240. verifyFormat("size_t x = sizeof(x);", NoSpace);
  8241. verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
  8242. verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
  8243. verifyFormat("alignas(128) char a[128];", NoSpace);
  8244. verifyFormat("size_t x = alignof(MyType);", NoSpace);
  8245. verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
  8246. verifyFormat("int f() throw(Deprecated);", NoSpace);
  8247. verifyFormat("typedef void (*cb)(int);", NoSpace);
  8248. verifyFormat("T A::operator()();", NoSpace);
  8249. verifyFormat("X A::operator++(T);", NoSpace);
  8250. FormatStyle Space = getLLVMStyle();
  8251. Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
  8252. verifyFormat("int f ();", Space);
  8253. verifyFormat("void f (int a, T b) {\n"
  8254. " while (true)\n"
  8255. " continue;\n"
  8256. "}",
  8257. Space);
  8258. verifyFormat("if (true)\n"
  8259. " f ();\n"
  8260. "else if (true)\n"
  8261. " f ();",
  8262. Space);
  8263. verifyFormat("do {\n"
  8264. " do_something ();\n"
  8265. "} while (something ());",
  8266. Space);
  8267. verifyFormat("switch (x) {\n"
  8268. "default:\n"
  8269. " break;\n"
  8270. "}",
  8271. Space);
  8272. verifyFormat("A::A () : a (1) {}", Space);
  8273. verifyFormat("void f () __attribute__ ((asdf));", Space);
  8274. verifyFormat("*(&a + 1);\n"
  8275. "&((&a)[1]);\n"
  8276. "a[(b + c) * d];\n"
  8277. "(((a + 1) * 2) + 3) * 4;",
  8278. Space);
  8279. verifyFormat("#define A(x) x", Space);
  8280. verifyFormat("#define A (x) x", Space);
  8281. verifyFormat("#if defined(x)\n"
  8282. "#endif",
  8283. Space);
  8284. verifyFormat("auto i = std::make_unique<int> (5);", Space);
  8285. verifyFormat("size_t x = sizeof (x);", Space);
  8286. verifyFormat("auto f (int x) -> decltype (x);", Space);
  8287. verifyFormat("int f (T x) noexcept (x.create ());", Space);
  8288. verifyFormat("alignas (128) char a[128];", Space);
  8289. verifyFormat("size_t x = alignof (MyType);", Space);
  8290. verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
  8291. verifyFormat("int f () throw (Deprecated);", Space);
  8292. verifyFormat("typedef void (*cb) (int);", Space);
  8293. verifyFormat("T A::operator() ();", Space);
  8294. verifyFormat("X A::operator++ (T);", Space);
  8295. }
  8296. TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
  8297. FormatStyle Spaces = getLLVMStyle();
  8298. Spaces.SpacesInParentheses = true;
  8299. verifyFormat("call( x, y, z );", Spaces);
  8300. verifyFormat("call();", Spaces);
  8301. verifyFormat("std::function<void( int, int )> callback;", Spaces);
  8302. verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
  8303. Spaces);
  8304. verifyFormat("while ( (bool)1 )\n"
  8305. " continue;",
  8306. Spaces);
  8307. verifyFormat("for ( ;; )\n"
  8308. " continue;",
  8309. Spaces);
  8310. verifyFormat("if ( true )\n"
  8311. " f();\n"
  8312. "else if ( true )\n"
  8313. " f();",
  8314. Spaces);
  8315. verifyFormat("do {\n"
  8316. " do_something( (int)i );\n"
  8317. "} while ( something() );",
  8318. Spaces);
  8319. verifyFormat("switch ( x ) {\n"
  8320. "default:\n"
  8321. " break;\n"
  8322. "}",
  8323. Spaces);
  8324. Spaces.SpacesInParentheses = false;
  8325. Spaces.SpacesInCStyleCastParentheses = true;
  8326. verifyFormat("Type *A = ( Type * )P;", Spaces);
  8327. verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
  8328. verifyFormat("x = ( int32 )y;", Spaces);
  8329. verifyFormat("int a = ( int )(2.0f);", Spaces);
  8330. verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
  8331. verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
  8332. verifyFormat("#define x (( int )-1)", Spaces);
  8333. // Run the first set of tests again with:
  8334. Spaces.SpacesInParentheses = false;
  8335. Spaces.SpaceInEmptyParentheses = true;
  8336. Spaces.SpacesInCStyleCastParentheses = true;
  8337. verifyFormat("call(x, y, z);", Spaces);
  8338. verifyFormat("call( );", Spaces);
  8339. verifyFormat("std::function<void(int, int)> callback;", Spaces);
  8340. verifyFormat("while (( bool )1)\n"
  8341. " continue;",
  8342. Spaces);
  8343. verifyFormat("for (;;)\n"
  8344. " continue;",
  8345. Spaces);
  8346. verifyFormat("if (true)\n"
  8347. " f( );\n"
  8348. "else if (true)\n"
  8349. " f( );",
  8350. Spaces);
  8351. verifyFormat("do {\n"
  8352. " do_something(( int )i);\n"
  8353. "} while (something( ));",
  8354. Spaces);
  8355. verifyFormat("switch (x) {\n"
  8356. "default:\n"
  8357. " break;\n"
  8358. "}",
  8359. Spaces);
  8360. // Run the first set of tests again with:
  8361. Spaces.SpaceAfterCStyleCast = true;
  8362. verifyFormat("call(x, y, z);", Spaces);
  8363. verifyFormat("call( );", Spaces);
  8364. verifyFormat("std::function<void(int, int)> callback;", Spaces);
  8365. verifyFormat("while (( bool ) 1)\n"
  8366. " continue;",
  8367. Spaces);
  8368. verifyFormat("for (;;)\n"
  8369. " continue;",
  8370. Spaces);
  8371. verifyFormat("if (true)\n"
  8372. " f( );\n"
  8373. "else if (true)\n"
  8374. " f( );",
  8375. Spaces);
  8376. verifyFormat("do {\n"
  8377. " do_something(( int ) i);\n"
  8378. "} while (something( ));",
  8379. Spaces);
  8380. verifyFormat("switch (x) {\n"
  8381. "default:\n"
  8382. " break;\n"
  8383. "}",
  8384. Spaces);
  8385. // Run subset of tests again with:
  8386. Spaces.SpacesInCStyleCastParentheses = false;
  8387. Spaces.SpaceAfterCStyleCast = true;
  8388. verifyFormat("while ((bool) 1)\n"
  8389. " continue;",
  8390. Spaces);
  8391. verifyFormat("do {\n"
  8392. " do_something((int) i);\n"
  8393. "} while (something( ));",
  8394. Spaces);
  8395. }
  8396. TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
  8397. verifyFormat("int a[5];");
  8398. verifyFormat("a[3] += 42;");
  8399. FormatStyle Spaces = getLLVMStyle();
  8400. Spaces.SpacesInSquareBrackets = true;
  8401. // Lambdas unchanged.
  8402. verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
  8403. verifyFormat("return [i, args...] {};", Spaces);
  8404. // Not lambdas.
  8405. verifyFormat("int a[ 5 ];", Spaces);
  8406. verifyFormat("a[ 3 ] += 42;", Spaces);
  8407. verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
  8408. verifyFormat("double &operator[](int i) { return 0; }\n"
  8409. "int i;",
  8410. Spaces);
  8411. verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
  8412. verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
  8413. verifyFormat("int i = (*b)[ a ]->f();", Spaces);
  8414. }
  8415. TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
  8416. verifyFormat("int a = 5;");
  8417. verifyFormat("a += 42;");
  8418. verifyFormat("a or_eq 8;");
  8419. FormatStyle Spaces = getLLVMStyle();
  8420. Spaces.SpaceBeforeAssignmentOperators = false;
  8421. verifyFormat("int a= 5;", Spaces);
  8422. verifyFormat("a+= 42;", Spaces);
  8423. verifyFormat("a or_eq 8;", Spaces);
  8424. }
  8425. TEST_F(FormatTest, AlignConsecutiveAssignments) {
  8426. FormatStyle Alignment = getLLVMStyle();
  8427. Alignment.AlignConsecutiveAssignments = false;
  8428. verifyFormat("int a = 5;\n"
  8429. "int oneTwoThree = 123;",
  8430. Alignment);
  8431. verifyFormat("int a = 5;\n"
  8432. "int oneTwoThree = 123;",
  8433. Alignment);
  8434. Alignment.AlignConsecutiveAssignments = true;
  8435. verifyFormat("int a = 5;\n"
  8436. "int oneTwoThree = 123;",
  8437. Alignment);
  8438. verifyFormat("int a = method();\n"
  8439. "int oneTwoThree = 133;",
  8440. Alignment);
  8441. verifyFormat("a &= 5;\n"
  8442. "bcd *= 5;\n"
  8443. "ghtyf += 5;\n"
  8444. "dvfvdb -= 5;\n"
  8445. "a /= 5;\n"
  8446. "vdsvsv %= 5;\n"
  8447. "sfdbddfbdfbb ^= 5;\n"
  8448. "dvsdsv |= 5;\n"
  8449. "int dsvvdvsdvvv = 123;",
  8450. Alignment);
  8451. verifyFormat("int i = 1, j = 10;\n"
  8452. "something = 2000;",
  8453. Alignment);
  8454. verifyFormat("something = 2000;\n"
  8455. "int i = 1, j = 10;\n",
  8456. Alignment);
  8457. verifyFormat("something = 2000;\n"
  8458. "another = 911;\n"
  8459. "int i = 1, j = 10;\n"
  8460. "oneMore = 1;\n"
  8461. "i = 2;",
  8462. Alignment);
  8463. verifyFormat("int a = 5;\n"
  8464. "int one = 1;\n"
  8465. "method();\n"
  8466. "int oneTwoThree = 123;\n"
  8467. "int oneTwo = 12;",
  8468. Alignment);
  8469. verifyFormat("int oneTwoThree = 123;\n"
  8470. "int oneTwo = 12;\n"
  8471. "method();\n",
  8472. Alignment);
  8473. verifyFormat("int oneTwoThree = 123; // comment\n"
  8474. "int oneTwo = 12; // comment",
  8475. Alignment);
  8476. EXPECT_EQ("int a = 5;\n"
  8477. "\n"
  8478. "int oneTwoThree = 123;",
  8479. format("int a = 5;\n"
  8480. "\n"
  8481. "int oneTwoThree= 123;",
  8482. Alignment));
  8483. EXPECT_EQ("int a = 5;\n"
  8484. "int one = 1;\n"
  8485. "\n"
  8486. "int oneTwoThree = 123;",
  8487. format("int a = 5;\n"
  8488. "int one = 1;\n"
  8489. "\n"
  8490. "int oneTwoThree = 123;",
  8491. Alignment));
  8492. EXPECT_EQ("int a = 5;\n"
  8493. "int one = 1;\n"
  8494. "\n"
  8495. "int oneTwoThree = 123;\n"
  8496. "int oneTwo = 12;",
  8497. format("int a = 5;\n"
  8498. "int one = 1;\n"
  8499. "\n"
  8500. "int oneTwoThree = 123;\n"
  8501. "int oneTwo = 12;",
  8502. Alignment));
  8503. Alignment.AlignEscapedNewlinesLeft = true;
  8504. verifyFormat("#define A \\\n"
  8505. " int aaaa = 12; \\\n"
  8506. " int b = 23; \\\n"
  8507. " int ccc = 234; \\\n"
  8508. " int dddddddddd = 2345;",
  8509. Alignment);
  8510. Alignment.AlignEscapedNewlinesLeft = false;
  8511. verifyFormat("#define A "
  8512. " \\\n"
  8513. " int aaaa = 12; "
  8514. " \\\n"
  8515. " int b = 23; "
  8516. " \\\n"
  8517. " int ccc = 234; "
  8518. " \\\n"
  8519. " int dddddddddd = 2345;",
  8520. Alignment);
  8521. verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
  8522. "k = 4, int l = 5,\n"
  8523. " int m = 6) {\n"
  8524. " int j = 10;\n"
  8525. " otherThing = 1;\n"
  8526. "}",
  8527. Alignment);
  8528. verifyFormat("void SomeFunction(int parameter = 0) {\n"
  8529. " int i = 1;\n"
  8530. " int j = 2;\n"
  8531. " int big = 10000;\n"
  8532. "}",
  8533. Alignment);
  8534. verifyFormat("class C {\n"
  8535. "public:\n"
  8536. " int i = 1;\n"
  8537. " virtual void f() = 0;\n"
  8538. "};",
  8539. Alignment);
  8540. verifyFormat("int i = 1;\n"
  8541. "if (SomeType t = getSomething()) {\n"
  8542. "}\n"
  8543. "int j = 2;\n"
  8544. "int big = 10000;",
  8545. Alignment);
  8546. verifyFormat("int j = 7;\n"
  8547. "for (int k = 0; k < N; ++k) {\n"
  8548. "}\n"
  8549. "int j = 2;\n"
  8550. "int big = 10000;\n"
  8551. "}",
  8552. Alignment);
  8553. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  8554. verifyFormat("int i = 1;\n"
  8555. "LooooooooooongType loooooooooooooooooooooongVariable\n"
  8556. " = someLooooooooooooooooongFunction();\n"
  8557. "int j = 2;",
  8558. Alignment);
  8559. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
  8560. verifyFormat("int i = 1;\n"
  8561. "LooooooooooongType loooooooooooooooooooooongVariable =\n"
  8562. " someLooooooooooooooooongFunction();\n"
  8563. "int j = 2;",
  8564. Alignment);
  8565. verifyFormat("auto lambda = []() {\n"
  8566. " auto i = 0;\n"
  8567. " return 0;\n"
  8568. "};\n"
  8569. "int i = 0;\n"
  8570. "auto v = type{\n"
  8571. " i = 1, //\n"
  8572. " (i = 2), //\n"
  8573. " i = 3 //\n"
  8574. "};",
  8575. Alignment);
  8576. // FIXME: Should align all three assignments
  8577. verifyFormat(
  8578. "int i = 1;\n"
  8579. "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
  8580. " loooooooooooooooooooooongParameterB);\n"
  8581. "int j = 2;",
  8582. Alignment);
  8583. verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
  8584. " typename B = very_long_type_name_1,\n"
  8585. " typename T_2 = very_long_type_name_2>\n"
  8586. "auto foo() {}\n",
  8587. Alignment);
  8588. verifyFormat("int a, b = 1;\n"
  8589. "int c = 2;\n"
  8590. "int dd = 3;\n",
  8591. Alignment);
  8592. verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
  8593. "float b[1][] = {{3.f}};\n",
  8594. Alignment);
  8595. }
  8596. TEST_F(FormatTest, AlignConsecutiveDeclarations) {
  8597. FormatStyle Alignment = getLLVMStyle();
  8598. Alignment.AlignConsecutiveDeclarations = false;
  8599. verifyFormat("float const a = 5;\n"
  8600. "int oneTwoThree = 123;",
  8601. Alignment);
  8602. verifyFormat("int a = 5;\n"
  8603. "float const oneTwoThree = 123;",
  8604. Alignment);
  8605. Alignment.AlignConsecutiveDeclarations = true;
  8606. verifyFormat("float const a = 5;\n"
  8607. "int oneTwoThree = 123;",
  8608. Alignment);
  8609. verifyFormat("int a = method();\n"
  8610. "float const oneTwoThree = 133;",
  8611. Alignment);
  8612. verifyFormat("int i = 1, j = 10;\n"
  8613. "something = 2000;",
  8614. Alignment);
  8615. verifyFormat("something = 2000;\n"
  8616. "int i = 1, j = 10;\n",
  8617. Alignment);
  8618. verifyFormat("float something = 2000;\n"
  8619. "double another = 911;\n"
  8620. "int i = 1, j = 10;\n"
  8621. "const int *oneMore = 1;\n"
  8622. "unsigned i = 2;",
  8623. Alignment);
  8624. verifyFormat("float a = 5;\n"
  8625. "int one = 1;\n"
  8626. "method();\n"
  8627. "const double oneTwoThree = 123;\n"
  8628. "const unsigned int oneTwo = 12;",
  8629. Alignment);
  8630. verifyFormat("int oneTwoThree{0}; // comment\n"
  8631. "unsigned oneTwo; // comment",
  8632. Alignment);
  8633. EXPECT_EQ("float const a = 5;\n"
  8634. "\n"
  8635. "int oneTwoThree = 123;",
  8636. format("float const a = 5;\n"
  8637. "\n"
  8638. "int oneTwoThree= 123;",
  8639. Alignment));
  8640. EXPECT_EQ("float a = 5;\n"
  8641. "int one = 1;\n"
  8642. "\n"
  8643. "unsigned oneTwoThree = 123;",
  8644. format("float a = 5;\n"
  8645. "int one = 1;\n"
  8646. "\n"
  8647. "unsigned oneTwoThree = 123;",
  8648. Alignment));
  8649. EXPECT_EQ("float a = 5;\n"
  8650. "int one = 1;\n"
  8651. "\n"
  8652. "unsigned oneTwoThree = 123;\n"
  8653. "int oneTwo = 12;",
  8654. format("float a = 5;\n"
  8655. "int one = 1;\n"
  8656. "\n"
  8657. "unsigned oneTwoThree = 123;\n"
  8658. "int oneTwo = 12;",
  8659. Alignment));
  8660. Alignment.AlignConsecutiveAssignments = true;
  8661. verifyFormat("float something = 2000;\n"
  8662. "double another = 911;\n"
  8663. "int i = 1, j = 10;\n"
  8664. "const int *oneMore = 1;\n"
  8665. "unsigned i = 2;",
  8666. Alignment);
  8667. verifyFormat("int oneTwoThree = {0}; // comment\n"
  8668. "unsigned oneTwo = 0; // comment",
  8669. Alignment);
  8670. EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
  8671. " int const i = 1;\n"
  8672. " int * j = 2;\n"
  8673. " int big = 10000;\n"
  8674. "\n"
  8675. " unsigned oneTwoThree = 123;\n"
  8676. " int oneTwo = 12;\n"
  8677. " method();\n"
  8678. " float k = 2;\n"
  8679. " int ll = 10000;\n"
  8680. "}",
  8681. format("void SomeFunction(int parameter= 0) {\n"
  8682. " int const i= 1;\n"
  8683. " int *j=2;\n"
  8684. " int big = 10000;\n"
  8685. "\n"
  8686. "unsigned oneTwoThree =123;\n"
  8687. "int oneTwo = 12;\n"
  8688. " method();\n"
  8689. "float k= 2;\n"
  8690. "int ll=10000;\n"
  8691. "}",
  8692. Alignment));
  8693. Alignment.AlignConsecutiveAssignments = false;
  8694. Alignment.AlignEscapedNewlinesLeft = true;
  8695. verifyFormat("#define A \\\n"
  8696. " int aaaa = 12; \\\n"
  8697. " float b = 23; \\\n"
  8698. " const int ccc = 234; \\\n"
  8699. " unsigned dddddddddd = 2345;",
  8700. Alignment);
  8701. Alignment.AlignEscapedNewlinesLeft = false;
  8702. Alignment.ColumnLimit = 30;
  8703. verifyFormat("#define A \\\n"
  8704. " int aaaa = 12; \\\n"
  8705. " float b = 23; \\\n"
  8706. " const int ccc = 234; \\\n"
  8707. " int dddddddddd = 2345;",
  8708. Alignment);
  8709. Alignment.ColumnLimit = 80;
  8710. verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
  8711. "k = 4, int l = 5,\n"
  8712. " int m = 6) {\n"
  8713. " const int j = 10;\n"
  8714. " otherThing = 1;\n"
  8715. "}",
  8716. Alignment);
  8717. verifyFormat("void SomeFunction(int parameter = 0) {\n"
  8718. " int const i = 1;\n"
  8719. " int * j = 2;\n"
  8720. " int big = 10000;\n"
  8721. "}",
  8722. Alignment);
  8723. verifyFormat("class C {\n"
  8724. "public:\n"
  8725. " int i = 1;\n"
  8726. " virtual void f() = 0;\n"
  8727. "};",
  8728. Alignment);
  8729. verifyFormat("float i = 1;\n"
  8730. "if (SomeType t = getSomething()) {\n"
  8731. "}\n"
  8732. "const unsigned j = 2;\n"
  8733. "int big = 10000;",
  8734. Alignment);
  8735. verifyFormat("float j = 7;\n"
  8736. "for (int k = 0; k < N; ++k) {\n"
  8737. "}\n"
  8738. "unsigned j = 2;\n"
  8739. "int big = 10000;\n"
  8740. "}",
  8741. Alignment);
  8742. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  8743. verifyFormat("float i = 1;\n"
  8744. "LooooooooooongType loooooooooooooooooooooongVariable\n"
  8745. " = someLooooooooooooooooongFunction();\n"
  8746. "int j = 2;",
  8747. Alignment);
  8748. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
  8749. verifyFormat("int i = 1;\n"
  8750. "LooooooooooongType loooooooooooooooooooooongVariable =\n"
  8751. " someLooooooooooooooooongFunction();\n"
  8752. "int j = 2;",
  8753. Alignment);
  8754. Alignment.AlignConsecutiveAssignments = true;
  8755. verifyFormat("auto lambda = []() {\n"
  8756. " auto ii = 0;\n"
  8757. " float j = 0;\n"
  8758. " return 0;\n"
  8759. "};\n"
  8760. "int i = 0;\n"
  8761. "float i2 = 0;\n"
  8762. "auto v = type{\n"
  8763. " i = 1, //\n"
  8764. " (i = 2), //\n"
  8765. " i = 3 //\n"
  8766. "};",
  8767. Alignment);
  8768. Alignment.AlignConsecutiveAssignments = false;
  8769. // FIXME: Should align all three declarations
  8770. verifyFormat(
  8771. "int i = 1;\n"
  8772. "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
  8773. " loooooooooooooooooooooongParameterB);\n"
  8774. "int j = 2;",
  8775. Alignment);
  8776. // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
  8777. // We expect declarations and assignments to align, as long as it doesn't
  8778. // exceed the column limit, starting a new alignemnt sequence whenever it
  8779. // happens.
  8780. Alignment.AlignConsecutiveAssignments = true;
  8781. Alignment.ColumnLimit = 30;
  8782. verifyFormat("float ii = 1;\n"
  8783. "unsigned j = 2;\n"
  8784. "int someVerylongVariable = 1;\n"
  8785. "AnotherLongType ll = 123456;\n"
  8786. "VeryVeryLongType k = 2;\n"
  8787. "int myvar = 1;",
  8788. Alignment);
  8789. Alignment.ColumnLimit = 80;
  8790. Alignment.AlignConsecutiveAssignments = false;
  8791. verifyFormat(
  8792. "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
  8793. " typename LongType, typename B>\n"
  8794. "auto foo() {}\n",
  8795. Alignment);
  8796. verifyFormat("float a, b = 1;\n"
  8797. "int c = 2;\n"
  8798. "int dd = 3;\n",
  8799. Alignment);
  8800. verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
  8801. "float b[1][] = {{3.f}};\n",
  8802. Alignment);
  8803. Alignment.AlignConsecutiveAssignments = true;
  8804. verifyFormat("float a, b = 1;\n"
  8805. "int c = 2;\n"
  8806. "int dd = 3;\n",
  8807. Alignment);
  8808. verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
  8809. "float b[1][] = {{3.f}};\n",
  8810. Alignment);
  8811. Alignment.AlignConsecutiveAssignments = false;
  8812. Alignment.ColumnLimit = 30;
  8813. Alignment.BinPackParameters = false;
  8814. verifyFormat("void foo(float a,\n"
  8815. " float b,\n"
  8816. " int c,\n"
  8817. " uint32_t *d) {\n"
  8818. " int * e = 0;\n"
  8819. " float f = 0;\n"
  8820. " double g = 0;\n"
  8821. "}\n"
  8822. "void bar(ino_t a,\n"
  8823. " int b,\n"
  8824. " uint32_t *c,\n"
  8825. " bool d) {}\n",
  8826. Alignment);
  8827. Alignment.BinPackParameters = true;
  8828. Alignment.ColumnLimit = 80;
  8829. }
  8830. TEST_F(FormatTest, LinuxBraceBreaking) {
  8831. FormatStyle LinuxBraceStyle = getLLVMStyle();
  8832. LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
  8833. verifyFormat("namespace a\n"
  8834. "{\n"
  8835. "class A\n"
  8836. "{\n"
  8837. " void f()\n"
  8838. " {\n"
  8839. " if (true) {\n"
  8840. " a();\n"
  8841. " b();\n"
  8842. " } else {\n"
  8843. " a();\n"
  8844. " }\n"
  8845. " }\n"
  8846. " void g() { return; }\n"
  8847. "};\n"
  8848. "struct B {\n"
  8849. " int x;\n"
  8850. "};\n"
  8851. "}\n",
  8852. LinuxBraceStyle);
  8853. verifyFormat("enum X {\n"
  8854. " Y = 0,\n"
  8855. "}\n",
  8856. LinuxBraceStyle);
  8857. verifyFormat("struct S {\n"
  8858. " int Type;\n"
  8859. " union {\n"
  8860. " int x;\n"
  8861. " double y;\n"
  8862. " } Value;\n"
  8863. " class C\n"
  8864. " {\n"
  8865. " MyFavoriteType Value;\n"
  8866. " } Class;\n"
  8867. "}\n",
  8868. LinuxBraceStyle);
  8869. }
  8870. TEST_F(FormatTest, MozillaBraceBreaking) {
  8871. FormatStyle MozillaBraceStyle = getLLVMStyle();
  8872. MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
  8873. verifyFormat("namespace a {\n"
  8874. "class A\n"
  8875. "{\n"
  8876. " void f()\n"
  8877. " {\n"
  8878. " if (true) {\n"
  8879. " a();\n"
  8880. " b();\n"
  8881. " }\n"
  8882. " }\n"
  8883. " void g() { return; }\n"
  8884. "};\n"
  8885. "enum E\n"
  8886. "{\n"
  8887. " A,\n"
  8888. " // foo\n"
  8889. " B,\n"
  8890. " C\n"
  8891. "};\n"
  8892. "struct B\n"
  8893. "{\n"
  8894. " int x;\n"
  8895. "};\n"
  8896. "}\n",
  8897. MozillaBraceStyle);
  8898. verifyFormat("struct S\n"
  8899. "{\n"
  8900. " int Type;\n"
  8901. " union\n"
  8902. " {\n"
  8903. " int x;\n"
  8904. " double y;\n"
  8905. " } Value;\n"
  8906. " class C\n"
  8907. " {\n"
  8908. " MyFavoriteType Value;\n"
  8909. " } Class;\n"
  8910. "}\n",
  8911. MozillaBraceStyle);
  8912. }
  8913. TEST_F(FormatTest, StroustrupBraceBreaking) {
  8914. FormatStyle StroustrupBraceStyle = getLLVMStyle();
  8915. StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  8916. verifyFormat("namespace a {\n"
  8917. "class A {\n"
  8918. " void f()\n"
  8919. " {\n"
  8920. " if (true) {\n"
  8921. " a();\n"
  8922. " b();\n"
  8923. " }\n"
  8924. " }\n"
  8925. " void g() { return; }\n"
  8926. "};\n"
  8927. "struct B {\n"
  8928. " int x;\n"
  8929. "};\n"
  8930. "}\n",
  8931. StroustrupBraceStyle);
  8932. verifyFormat("void foo()\n"
  8933. "{\n"
  8934. " if (a) {\n"
  8935. " a();\n"
  8936. " }\n"
  8937. " else {\n"
  8938. " b();\n"
  8939. " }\n"
  8940. "}\n",
  8941. StroustrupBraceStyle);
  8942. verifyFormat("#ifdef _DEBUG\n"
  8943. "int foo(int i = 0)\n"
  8944. "#else\n"
  8945. "int foo(int i = 5)\n"
  8946. "#endif\n"
  8947. "{\n"
  8948. " return i;\n"
  8949. "}",
  8950. StroustrupBraceStyle);
  8951. verifyFormat("void foo() {}\n"
  8952. "void bar()\n"
  8953. "#ifdef _DEBUG\n"
  8954. "{\n"
  8955. " foo();\n"
  8956. "}\n"
  8957. "#else\n"
  8958. "{\n"
  8959. "}\n"
  8960. "#endif",
  8961. StroustrupBraceStyle);
  8962. verifyFormat("void foobar() { int i = 5; }\n"
  8963. "#ifdef _DEBUG\n"
  8964. "void bar() {}\n"
  8965. "#else\n"
  8966. "void bar() { foobar(); }\n"
  8967. "#endif",
  8968. StroustrupBraceStyle);
  8969. }
  8970. TEST_F(FormatTest, AllmanBraceBreaking) {
  8971. FormatStyle AllmanBraceStyle = getLLVMStyle();
  8972. AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
  8973. verifyFormat("namespace a\n"
  8974. "{\n"
  8975. "class A\n"
  8976. "{\n"
  8977. " void f()\n"
  8978. " {\n"
  8979. " if (true)\n"
  8980. " {\n"
  8981. " a();\n"
  8982. " b();\n"
  8983. " }\n"
  8984. " }\n"
  8985. " void g() { return; }\n"
  8986. "};\n"
  8987. "struct B\n"
  8988. "{\n"
  8989. " int x;\n"
  8990. "};\n"
  8991. "}",
  8992. AllmanBraceStyle);
  8993. verifyFormat("void f()\n"
  8994. "{\n"
  8995. " if (true)\n"
  8996. " {\n"
  8997. " a();\n"
  8998. " }\n"
  8999. " else if (false)\n"
  9000. " {\n"
  9001. " b();\n"
  9002. " }\n"
  9003. " else\n"
  9004. " {\n"
  9005. " c();\n"
  9006. " }\n"
  9007. "}\n",
  9008. AllmanBraceStyle);
  9009. verifyFormat("void f()\n"
  9010. "{\n"
  9011. " for (int i = 0; i < 10; ++i)\n"
  9012. " {\n"
  9013. " a();\n"
  9014. " }\n"
  9015. " while (false)\n"
  9016. " {\n"
  9017. " b();\n"
  9018. " }\n"
  9019. " do\n"
  9020. " {\n"
  9021. " c();\n"
  9022. " } while (false)\n"
  9023. "}\n",
  9024. AllmanBraceStyle);
  9025. verifyFormat("void f(int a)\n"
  9026. "{\n"
  9027. " switch (a)\n"
  9028. " {\n"
  9029. " case 0:\n"
  9030. " break;\n"
  9031. " case 1:\n"
  9032. " {\n"
  9033. " break;\n"
  9034. " }\n"
  9035. " case 2:\n"
  9036. " {\n"
  9037. " }\n"
  9038. " break;\n"
  9039. " default:\n"
  9040. " break;\n"
  9041. " }\n"
  9042. "}\n",
  9043. AllmanBraceStyle);
  9044. verifyFormat("enum X\n"
  9045. "{\n"
  9046. " Y = 0,\n"
  9047. "}\n",
  9048. AllmanBraceStyle);
  9049. verifyFormat("enum X\n"
  9050. "{\n"
  9051. " Y = 0\n"
  9052. "}\n",
  9053. AllmanBraceStyle);
  9054. verifyFormat("@interface BSApplicationController ()\n"
  9055. "{\n"
  9056. "@private\n"
  9057. " id _extraIvar;\n"
  9058. "}\n"
  9059. "@end\n",
  9060. AllmanBraceStyle);
  9061. verifyFormat("#ifdef _DEBUG\n"
  9062. "int foo(int i = 0)\n"
  9063. "#else\n"
  9064. "int foo(int i = 5)\n"
  9065. "#endif\n"
  9066. "{\n"
  9067. " return i;\n"
  9068. "}",
  9069. AllmanBraceStyle);
  9070. verifyFormat("void foo() {}\n"
  9071. "void bar()\n"
  9072. "#ifdef _DEBUG\n"
  9073. "{\n"
  9074. " foo();\n"
  9075. "}\n"
  9076. "#else\n"
  9077. "{\n"
  9078. "}\n"
  9079. "#endif",
  9080. AllmanBraceStyle);
  9081. verifyFormat("void foobar() { int i = 5; }\n"
  9082. "#ifdef _DEBUG\n"
  9083. "void bar() {}\n"
  9084. "#else\n"
  9085. "void bar() { foobar(); }\n"
  9086. "#endif",
  9087. AllmanBraceStyle);
  9088. // This shouldn't affect ObjC blocks..
  9089. verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
  9090. " // ...\n"
  9091. " int i;\n"
  9092. "}];",
  9093. AllmanBraceStyle);
  9094. verifyFormat("void (^block)(void) = ^{\n"
  9095. " // ...\n"
  9096. " int i;\n"
  9097. "};",
  9098. AllmanBraceStyle);
  9099. // .. or dict literals.
  9100. verifyFormat("void f()\n"
  9101. "{\n"
  9102. " [object someMethod:@{ @\"a\" : @\"b\" }];\n"
  9103. "}",
  9104. AllmanBraceStyle);
  9105. verifyFormat("int f()\n"
  9106. "{ // comment\n"
  9107. " return 42;\n"
  9108. "}",
  9109. AllmanBraceStyle);
  9110. AllmanBraceStyle.ColumnLimit = 19;
  9111. verifyFormat("void f() { int i; }", AllmanBraceStyle);
  9112. AllmanBraceStyle.ColumnLimit = 18;
  9113. verifyFormat("void f()\n"
  9114. "{\n"
  9115. " int i;\n"
  9116. "}",
  9117. AllmanBraceStyle);
  9118. AllmanBraceStyle.ColumnLimit = 80;
  9119. FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
  9120. BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
  9121. BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
  9122. verifyFormat("void f(bool b)\n"
  9123. "{\n"
  9124. " if (b)\n"
  9125. " {\n"
  9126. " return;\n"
  9127. " }\n"
  9128. "}\n",
  9129. BreakBeforeBraceShortIfs);
  9130. verifyFormat("void f(bool b)\n"
  9131. "{\n"
  9132. " if (b) return;\n"
  9133. "}\n",
  9134. BreakBeforeBraceShortIfs);
  9135. verifyFormat("void f(bool b)\n"
  9136. "{\n"
  9137. " while (b)\n"
  9138. " {\n"
  9139. " return;\n"
  9140. " }\n"
  9141. "}\n",
  9142. BreakBeforeBraceShortIfs);
  9143. }
  9144. TEST_F(FormatTest, GNUBraceBreaking) {
  9145. FormatStyle GNUBraceStyle = getLLVMStyle();
  9146. GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
  9147. verifyFormat("namespace a\n"
  9148. "{\n"
  9149. "class A\n"
  9150. "{\n"
  9151. " void f()\n"
  9152. " {\n"
  9153. " int a;\n"
  9154. " {\n"
  9155. " int b;\n"
  9156. " }\n"
  9157. " if (true)\n"
  9158. " {\n"
  9159. " a();\n"
  9160. " b();\n"
  9161. " }\n"
  9162. " }\n"
  9163. " void g() { return; }\n"
  9164. "}\n"
  9165. "}",
  9166. GNUBraceStyle);
  9167. verifyFormat("void f()\n"
  9168. "{\n"
  9169. " if (true)\n"
  9170. " {\n"
  9171. " a();\n"
  9172. " }\n"
  9173. " else if (false)\n"
  9174. " {\n"
  9175. " b();\n"
  9176. " }\n"
  9177. " else\n"
  9178. " {\n"
  9179. " c();\n"
  9180. " }\n"
  9181. "}\n",
  9182. GNUBraceStyle);
  9183. verifyFormat("void f()\n"
  9184. "{\n"
  9185. " for (int i = 0; i < 10; ++i)\n"
  9186. " {\n"
  9187. " a();\n"
  9188. " }\n"
  9189. " while (false)\n"
  9190. " {\n"
  9191. " b();\n"
  9192. " }\n"
  9193. " do\n"
  9194. " {\n"
  9195. " c();\n"
  9196. " }\n"
  9197. " while (false);\n"
  9198. "}\n",
  9199. GNUBraceStyle);
  9200. verifyFormat("void f(int a)\n"
  9201. "{\n"
  9202. " switch (a)\n"
  9203. " {\n"
  9204. " case 0:\n"
  9205. " break;\n"
  9206. " case 1:\n"
  9207. " {\n"
  9208. " break;\n"
  9209. " }\n"
  9210. " case 2:\n"
  9211. " {\n"
  9212. " }\n"
  9213. " break;\n"
  9214. " default:\n"
  9215. " break;\n"
  9216. " }\n"
  9217. "}\n",
  9218. GNUBraceStyle);
  9219. verifyFormat("enum X\n"
  9220. "{\n"
  9221. " Y = 0,\n"
  9222. "}\n",
  9223. GNUBraceStyle);
  9224. verifyFormat("@interface BSApplicationController ()\n"
  9225. "{\n"
  9226. "@private\n"
  9227. " id _extraIvar;\n"
  9228. "}\n"
  9229. "@end\n",
  9230. GNUBraceStyle);
  9231. verifyFormat("#ifdef _DEBUG\n"
  9232. "int foo(int i = 0)\n"
  9233. "#else\n"
  9234. "int foo(int i = 5)\n"
  9235. "#endif\n"
  9236. "{\n"
  9237. " return i;\n"
  9238. "}",
  9239. GNUBraceStyle);
  9240. verifyFormat("void foo() {}\n"
  9241. "void bar()\n"
  9242. "#ifdef _DEBUG\n"
  9243. "{\n"
  9244. " foo();\n"
  9245. "}\n"
  9246. "#else\n"
  9247. "{\n"
  9248. "}\n"
  9249. "#endif",
  9250. GNUBraceStyle);
  9251. verifyFormat("void foobar() { int i = 5; }\n"
  9252. "#ifdef _DEBUG\n"
  9253. "void bar() {}\n"
  9254. "#else\n"
  9255. "void bar() { foobar(); }\n"
  9256. "#endif",
  9257. GNUBraceStyle);
  9258. }
  9259. TEST_F(FormatTest, WebKitBraceBreaking) {
  9260. FormatStyle WebKitBraceStyle = getLLVMStyle();
  9261. WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
  9262. verifyFormat("namespace a {\n"
  9263. "class A {\n"
  9264. " void f()\n"
  9265. " {\n"
  9266. " if (true) {\n"
  9267. " a();\n"
  9268. " b();\n"
  9269. " }\n"
  9270. " }\n"
  9271. " void g() { return; }\n"
  9272. "};\n"
  9273. "enum E {\n"
  9274. " A,\n"
  9275. " // foo\n"
  9276. " B,\n"
  9277. " C\n"
  9278. "};\n"
  9279. "struct B {\n"
  9280. " int x;\n"
  9281. "};\n"
  9282. "}\n",
  9283. WebKitBraceStyle);
  9284. verifyFormat("struct S {\n"
  9285. " int Type;\n"
  9286. " union {\n"
  9287. " int x;\n"
  9288. " double y;\n"
  9289. " } Value;\n"
  9290. " class C {\n"
  9291. " MyFavoriteType Value;\n"
  9292. " } Class;\n"
  9293. "};\n",
  9294. WebKitBraceStyle);
  9295. }
  9296. TEST_F(FormatTest, CatchExceptionReferenceBinding) {
  9297. verifyFormat("void f() {\n"
  9298. " try {\n"
  9299. " } catch (const Exception &e) {\n"
  9300. " }\n"
  9301. "}\n",
  9302. getLLVMStyle());
  9303. }
  9304. TEST_F(FormatTest, UnderstandsPragmas) {
  9305. verifyFormat("#pragma omp reduction(| : var)");
  9306. verifyFormat("#pragma omp reduction(+ : var)");
  9307. EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
  9308. "(including parentheses).",
  9309. format("#pragma mark Any non-hyphenated or hyphenated string "
  9310. "(including parentheses)."));
  9311. }
  9312. TEST_F(FormatTest, UnderstandPragmaOption) {
  9313. verifyFormat("#pragma option -C -A");
  9314. EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A"));
  9315. }
  9316. #define EXPECT_ALL_STYLES_EQUAL(Styles) \
  9317. for (size_t i = 1; i < Styles.size(); ++i) \
  9318. EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
  9319. << " differs from Style #0"
  9320. TEST_F(FormatTest, GetsPredefinedStyleByName) {
  9321. SmallVector<FormatStyle, 3> Styles;
  9322. Styles.resize(3);
  9323. Styles[0] = getLLVMStyle();
  9324. EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
  9325. EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
  9326. EXPECT_ALL_STYLES_EQUAL(Styles);
  9327. Styles[0] = getGoogleStyle();
  9328. EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
  9329. EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
  9330. EXPECT_ALL_STYLES_EQUAL(Styles);
  9331. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  9332. EXPECT_TRUE(
  9333. getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
  9334. EXPECT_TRUE(
  9335. getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
  9336. EXPECT_ALL_STYLES_EQUAL(Styles);
  9337. Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
  9338. EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
  9339. EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
  9340. EXPECT_ALL_STYLES_EQUAL(Styles);
  9341. Styles[0] = getMozillaStyle();
  9342. EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
  9343. EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
  9344. EXPECT_ALL_STYLES_EQUAL(Styles);
  9345. Styles[0] = getWebKitStyle();
  9346. EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
  9347. EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
  9348. EXPECT_ALL_STYLES_EQUAL(Styles);
  9349. Styles[0] = getGNUStyle();
  9350. EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
  9351. EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
  9352. EXPECT_ALL_STYLES_EQUAL(Styles);
  9353. EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
  9354. }
  9355. TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
  9356. SmallVector<FormatStyle, 8> Styles;
  9357. Styles.resize(2);
  9358. Styles[0] = getGoogleStyle();
  9359. Styles[1] = getLLVMStyle();
  9360. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  9361. EXPECT_ALL_STYLES_EQUAL(Styles);
  9362. Styles.resize(5);
  9363. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  9364. Styles[1] = getLLVMStyle();
  9365. Styles[1].Language = FormatStyle::LK_JavaScript;
  9366. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  9367. Styles[2] = getLLVMStyle();
  9368. Styles[2].Language = FormatStyle::LK_JavaScript;
  9369. EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
  9370. "BasedOnStyle: Google",
  9371. &Styles[2])
  9372. .value());
  9373. Styles[3] = getLLVMStyle();
  9374. Styles[3].Language = FormatStyle::LK_JavaScript;
  9375. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
  9376. "Language: JavaScript",
  9377. &Styles[3])
  9378. .value());
  9379. Styles[4] = getLLVMStyle();
  9380. Styles[4].Language = FormatStyle::LK_JavaScript;
  9381. EXPECT_EQ(0, parseConfiguration("---\n"
  9382. "BasedOnStyle: LLVM\n"
  9383. "IndentWidth: 123\n"
  9384. "---\n"
  9385. "BasedOnStyle: Google\n"
  9386. "Language: JavaScript",
  9387. &Styles[4])
  9388. .value());
  9389. EXPECT_ALL_STYLES_EQUAL(Styles);
  9390. }
  9391. #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \
  9392. Style.FIELD = false; \
  9393. EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \
  9394. EXPECT_TRUE(Style.FIELD); \
  9395. EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \
  9396. EXPECT_FALSE(Style.FIELD);
  9397. #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
  9398. #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \
  9399. Style.STRUCT.FIELD = false; \
  9400. EXPECT_EQ(0, \
  9401. parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \
  9402. .value()); \
  9403. EXPECT_TRUE(Style.STRUCT.FIELD); \
  9404. EXPECT_EQ(0, \
  9405. parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \
  9406. .value()); \
  9407. EXPECT_FALSE(Style.STRUCT.FIELD);
  9408. #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \
  9409. CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
  9410. #define CHECK_PARSE(TEXT, FIELD, VALUE) \
  9411. EXPECT_NE(VALUE, Style.FIELD); \
  9412. EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \
  9413. EXPECT_EQ(VALUE, Style.FIELD)
  9414. TEST_F(FormatTest, ParsesConfigurationBools) {
  9415. FormatStyle Style = {};
  9416. Style.Language = FormatStyle::LK_Cpp;
  9417. CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
  9418. CHECK_PARSE_BOOL(AlignOperands);
  9419. CHECK_PARSE_BOOL(AlignTrailingComments);
  9420. CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
  9421. CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
  9422. CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
  9423. CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
  9424. CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
  9425. CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
  9426. CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
  9427. CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
  9428. CHECK_PARSE_BOOL(BinPackArguments);
  9429. CHECK_PARSE_BOOL(BinPackParameters);
  9430. CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
  9431. CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
  9432. CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
  9433. CHECK_PARSE_BOOL(BreakStringLiterals);
  9434. CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
  9435. CHECK_PARSE_BOOL(DerivePointerAlignment);
  9436. CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
  9437. CHECK_PARSE_BOOL(DisableFormat);
  9438. CHECK_PARSE_BOOL(IndentCaseLabels);
  9439. CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
  9440. CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
  9441. CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
  9442. CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
  9443. CHECK_PARSE_BOOL(Cpp11BracedListStyle);
  9444. CHECK_PARSE_BOOL(ReflowComments);
  9445. CHECK_PARSE_BOOL(SortIncludes);
  9446. CHECK_PARSE_BOOL(SpacesInParentheses);
  9447. CHECK_PARSE_BOOL(SpacesInSquareBrackets);
  9448. CHECK_PARSE_BOOL(SpacesInAngles);
  9449. CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
  9450. CHECK_PARSE_BOOL(SpacesInContainerLiterals);
  9451. CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
  9452. CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
  9453. CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
  9454. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
  9455. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
  9456. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
  9457. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
  9458. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
  9459. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
  9460. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
  9461. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
  9462. CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
  9463. CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
  9464. CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
  9465. }
  9466. #undef CHECK_PARSE_BOOL
  9467. TEST_F(FormatTest, ParsesConfiguration) {
  9468. FormatStyle Style = {};
  9469. Style.Language = FormatStyle::LK_Cpp;
  9470. CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
  9471. CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
  9472. ConstructorInitializerIndentWidth, 1234u);
  9473. CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
  9474. CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
  9475. CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
  9476. CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
  9477. PenaltyBreakBeforeFirstCallParameter, 1234u);
  9478. CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
  9479. CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
  9480. PenaltyReturnTypeOnItsOwnLine, 1234u);
  9481. CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
  9482. SpacesBeforeTrailingComments, 1234u);
  9483. CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
  9484. CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
  9485. CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
  9486. Style.PointerAlignment = FormatStyle::PAS_Middle;
  9487. CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
  9488. FormatStyle::PAS_Left);
  9489. CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
  9490. FormatStyle::PAS_Right);
  9491. CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
  9492. FormatStyle::PAS_Middle);
  9493. // For backward compatibility:
  9494. CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
  9495. FormatStyle::PAS_Left);
  9496. CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
  9497. FormatStyle::PAS_Right);
  9498. CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
  9499. FormatStyle::PAS_Middle);
  9500. Style.Standard = FormatStyle::LS_Auto;
  9501. CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
  9502. CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
  9503. CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
  9504. CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
  9505. CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
  9506. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  9507. CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
  9508. BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
  9509. CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
  9510. FormatStyle::BOS_None);
  9511. CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
  9512. FormatStyle::BOS_All);
  9513. // For backward compatibility:
  9514. CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
  9515. FormatStyle::BOS_None);
  9516. CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
  9517. FormatStyle::BOS_All);
  9518. Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
  9519. CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
  9520. FormatStyle::BAS_Align);
  9521. CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
  9522. FormatStyle::BAS_DontAlign);
  9523. CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
  9524. FormatStyle::BAS_AlwaysBreak);
  9525. // For backward compatibility:
  9526. CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
  9527. FormatStyle::BAS_DontAlign);
  9528. CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
  9529. FormatStyle::BAS_Align);
  9530. Style.UseTab = FormatStyle::UT_ForIndentation;
  9531. CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
  9532. CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
  9533. CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
  9534. CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
  9535. FormatStyle::UT_ForContinuationAndIndentation);
  9536. // For backward compatibility:
  9537. CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
  9538. CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
  9539. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  9540. CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
  9541. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
  9542. CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
  9543. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
  9544. CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
  9545. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
  9546. CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
  9547. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
  9548. // For backward compatibility:
  9549. CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
  9550. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
  9551. CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
  9552. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
  9553. Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
  9554. CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
  9555. FormatStyle::SBPO_Never);
  9556. CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
  9557. FormatStyle::SBPO_Always);
  9558. CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
  9559. FormatStyle::SBPO_ControlStatements);
  9560. // For backward compatibility:
  9561. CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
  9562. FormatStyle::SBPO_Never);
  9563. CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
  9564. FormatStyle::SBPO_ControlStatements);
  9565. Style.ColumnLimit = 123;
  9566. FormatStyle BaseStyle = getLLVMStyle();
  9567. CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
  9568. CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
  9569. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  9570. CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
  9571. FormatStyle::BS_Attach);
  9572. CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
  9573. FormatStyle::BS_Linux);
  9574. CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
  9575. FormatStyle::BS_Mozilla);
  9576. CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
  9577. FormatStyle::BS_Stroustrup);
  9578. CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
  9579. FormatStyle::BS_Allman);
  9580. CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
  9581. CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
  9582. FormatStyle::BS_WebKit);
  9583. CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
  9584. FormatStyle::BS_Custom);
  9585. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
  9586. CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
  9587. FormatStyle::RTBS_None);
  9588. CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
  9589. FormatStyle::RTBS_All);
  9590. CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
  9591. AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
  9592. CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
  9593. AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
  9594. CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
  9595. AlwaysBreakAfterReturnType,
  9596. FormatStyle::RTBS_TopLevelDefinitions);
  9597. Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
  9598. CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
  9599. AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
  9600. CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
  9601. AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
  9602. CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
  9603. AlwaysBreakAfterDefinitionReturnType,
  9604. FormatStyle::DRTBS_TopLevel);
  9605. Style.NamespaceIndentation = FormatStyle::NI_All;
  9606. CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
  9607. FormatStyle::NI_None);
  9608. CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
  9609. FormatStyle::NI_Inner);
  9610. CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
  9611. FormatStyle::NI_All);
  9612. // FIXME: This is required because parsing a configuration simply overwrites
  9613. // the first N elements of the list instead of resetting it.
  9614. Style.ForEachMacros.clear();
  9615. std::vector<std::string> BoostForeach;
  9616. BoostForeach.push_back("BOOST_FOREACH");
  9617. CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
  9618. std::vector<std::string> BoostAndQForeach;
  9619. BoostAndQForeach.push_back("BOOST_FOREACH");
  9620. BoostAndQForeach.push_back("Q_FOREACH");
  9621. CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
  9622. BoostAndQForeach);
  9623. Style.IncludeCategories.clear();
  9624. std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2},
  9625. {".*", 1}};
  9626. CHECK_PARSE("IncludeCategories:\n"
  9627. " - Regex: abc/.*\n"
  9628. " Priority: 2\n"
  9629. " - Regex: .*\n"
  9630. " Priority: 1",
  9631. IncludeCategories, ExpectedCategories);
  9632. CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$");
  9633. }
  9634. TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
  9635. FormatStyle Style = {};
  9636. Style.Language = FormatStyle::LK_Cpp;
  9637. CHECK_PARSE("Language: Cpp\n"
  9638. "IndentWidth: 12",
  9639. IndentWidth, 12u);
  9640. EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
  9641. "IndentWidth: 34",
  9642. &Style),
  9643. ParseError::Unsuitable);
  9644. EXPECT_EQ(12u, Style.IndentWidth);
  9645. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  9646. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  9647. Style.Language = FormatStyle::LK_JavaScript;
  9648. CHECK_PARSE("Language: JavaScript\n"
  9649. "IndentWidth: 12",
  9650. IndentWidth, 12u);
  9651. CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
  9652. EXPECT_EQ(parseConfiguration("Language: Cpp\n"
  9653. "IndentWidth: 34",
  9654. &Style),
  9655. ParseError::Unsuitable);
  9656. EXPECT_EQ(23u, Style.IndentWidth);
  9657. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  9658. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  9659. CHECK_PARSE("BasedOnStyle: LLVM\n"
  9660. "IndentWidth: 67",
  9661. IndentWidth, 67u);
  9662. CHECK_PARSE("---\n"
  9663. "Language: JavaScript\n"
  9664. "IndentWidth: 12\n"
  9665. "---\n"
  9666. "Language: Cpp\n"
  9667. "IndentWidth: 34\n"
  9668. "...\n",
  9669. IndentWidth, 12u);
  9670. Style.Language = FormatStyle::LK_Cpp;
  9671. CHECK_PARSE("---\n"
  9672. "Language: JavaScript\n"
  9673. "IndentWidth: 12\n"
  9674. "---\n"
  9675. "Language: Cpp\n"
  9676. "IndentWidth: 34\n"
  9677. "...\n",
  9678. IndentWidth, 34u);
  9679. CHECK_PARSE("---\n"
  9680. "IndentWidth: 78\n"
  9681. "---\n"
  9682. "Language: JavaScript\n"
  9683. "IndentWidth: 56\n"
  9684. "...\n",
  9685. IndentWidth, 78u);
  9686. Style.ColumnLimit = 123;
  9687. Style.IndentWidth = 234;
  9688. Style.BreakBeforeBraces = FormatStyle::BS_Linux;
  9689. Style.TabWidth = 345;
  9690. EXPECT_FALSE(parseConfiguration("---\n"
  9691. "IndentWidth: 456\n"
  9692. "BreakBeforeBraces: Allman\n"
  9693. "---\n"
  9694. "Language: JavaScript\n"
  9695. "IndentWidth: 111\n"
  9696. "TabWidth: 111\n"
  9697. "---\n"
  9698. "Language: Cpp\n"
  9699. "BreakBeforeBraces: Stroustrup\n"
  9700. "TabWidth: 789\n"
  9701. "...\n",
  9702. &Style));
  9703. EXPECT_EQ(123u, Style.ColumnLimit);
  9704. EXPECT_EQ(456u, Style.IndentWidth);
  9705. EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
  9706. EXPECT_EQ(789u, Style.TabWidth);
  9707. EXPECT_EQ(parseConfiguration("---\n"
  9708. "Language: JavaScript\n"
  9709. "IndentWidth: 56\n"
  9710. "---\n"
  9711. "IndentWidth: 78\n"
  9712. "...\n",
  9713. &Style),
  9714. ParseError::Error);
  9715. EXPECT_EQ(parseConfiguration("---\n"
  9716. "Language: JavaScript\n"
  9717. "IndentWidth: 56\n"
  9718. "---\n"
  9719. "Language: JavaScript\n"
  9720. "IndentWidth: 78\n"
  9721. "...\n",
  9722. &Style),
  9723. ParseError::Error);
  9724. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  9725. }
  9726. #undef CHECK_PARSE
  9727. TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
  9728. FormatStyle Style = {};
  9729. Style.Language = FormatStyle::LK_JavaScript;
  9730. Style.BreakBeforeTernaryOperators = true;
  9731. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
  9732. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  9733. Style.BreakBeforeTernaryOperators = true;
  9734. EXPECT_EQ(0, parseConfiguration("---\n"
  9735. "BasedOnStyle: Google\n"
  9736. "---\n"
  9737. "Language: JavaScript\n"
  9738. "IndentWidth: 76\n"
  9739. "...\n",
  9740. &Style)
  9741. .value());
  9742. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  9743. EXPECT_EQ(76u, Style.IndentWidth);
  9744. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  9745. }
  9746. TEST_F(FormatTest, ConfigurationRoundTripTest) {
  9747. FormatStyle Style = getLLVMStyle();
  9748. std::string YAML = configurationAsText(Style);
  9749. FormatStyle ParsedStyle = {};
  9750. ParsedStyle.Language = FormatStyle::LK_Cpp;
  9751. EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
  9752. EXPECT_EQ(Style, ParsedStyle);
  9753. }
  9754. TEST_F(FormatTest, WorksFor8bitEncodings) {
  9755. EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
  9756. "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
  9757. "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
  9758. "\"\xef\xee\xf0\xf3...\"",
  9759. format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
  9760. "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
  9761. "\xef\xee\xf0\xf3...\"",
  9762. getLLVMStyleWithColumns(12)));
  9763. }
  9764. TEST_F(FormatTest, HandlesUTF8BOM) {
  9765. EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
  9766. EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
  9767. format("\xef\xbb\xbf#include <iostream>"));
  9768. EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
  9769. format("\xef\xbb\xbf\n#include <iostream>"));
  9770. }
  9771. // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
  9772. #if !defined(_MSC_VER)
  9773. TEST_F(FormatTest, CountsUTF8CharactersProperly) {
  9774. verifyFormat("\"Однажды в студёную зимнюю пору...\"",
  9775. getLLVMStyleWithColumns(35));
  9776. verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
  9777. getLLVMStyleWithColumns(31));
  9778. verifyFormat("// Однажды в студёную зимнюю пору...",
  9779. getLLVMStyleWithColumns(36));
  9780. verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
  9781. verifyFormat("/* Однажды в студёную зимнюю пору... */",
  9782. getLLVMStyleWithColumns(39));
  9783. verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
  9784. getLLVMStyleWithColumns(35));
  9785. }
  9786. TEST_F(FormatTest, SplitsUTF8Strings) {
  9787. // Non-printable characters' width is currently considered to be the length in
  9788. // bytes in UTF8. The characters can be displayed in very different manner
  9789. // (zero-width, single width with a substitution glyph, expanded to their code
  9790. // (e.g. "<8d>"), so there's no single correct way to handle them.
  9791. EXPECT_EQ("\"aaaaÄ\"\n"
  9792. "\"\xc2\x8d\";",
  9793. format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  9794. EXPECT_EQ("\"aaaaaaaÄ\"\n"
  9795. "\"\xc2\x8d\";",
  9796. format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  9797. EXPECT_EQ("\"Однажды, в \"\n"
  9798. "\"студёную \"\n"
  9799. "\"зимнюю \"\n"
  9800. "\"пору,\"",
  9801. format("\"Однажды, в студёную зимнюю пору,\"",
  9802. getLLVMStyleWithColumns(13)));
  9803. EXPECT_EQ(
  9804. "\"一 二 三 \"\n"
  9805. "\"四 五六 \"\n"
  9806. "\"七 八 九 \"\n"
  9807. "\"十\"",
  9808. format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
  9809. EXPECT_EQ("\"一\t二 \"\n"
  9810. "\"\t三 \"\n"
  9811. "\"四 五\t六 \"\n"
  9812. "\"\t七 \"\n"
  9813. "\"八九十\tqq\"",
  9814. format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
  9815. getLLVMStyleWithColumns(11)));
  9816. // UTF8 character in an escape sequence.
  9817. EXPECT_EQ("\"aaaaaa\"\n"
  9818. "\"\\\xC2\x8D\"",
  9819. format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
  9820. }
  9821. TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
  9822. EXPECT_EQ("const char *sssss =\n"
  9823. " \"一二三四五六七八\\\n"
  9824. " 九 十\";",
  9825. format("const char *sssss = \"一二三四五六七八\\\n"
  9826. " 九 十\";",
  9827. getLLVMStyleWithColumns(30)));
  9828. }
  9829. TEST_F(FormatTest, SplitsUTF8LineComments) {
  9830. EXPECT_EQ("// aaaaÄ\xc2\x8d",
  9831. format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
  9832. EXPECT_EQ("// Я из лесу\n"
  9833. "// вышел; был\n"
  9834. "// сильный\n"
  9835. "// мороз.",
  9836. format("// Я из лесу вышел; был сильный мороз.",
  9837. getLLVMStyleWithColumns(13)));
  9838. EXPECT_EQ("// 一二三\n"
  9839. "// 四五六七\n"
  9840. "// 八 九\n"
  9841. "// 十",
  9842. format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9)));
  9843. }
  9844. TEST_F(FormatTest, SplitsUTF8BlockComments) {
  9845. EXPECT_EQ("/* Гляжу,\n"
  9846. " * поднимается\n"
  9847. " * медленно в\n"
  9848. " * гору\n"
  9849. " * Лошадка,\n"
  9850. " * везущая\n"
  9851. " * хворосту\n"
  9852. " * воз. */",
  9853. format("/* Гляжу, поднимается медленно в гору\n"
  9854. " * Лошадка, везущая хворосту воз. */",
  9855. getLLVMStyleWithColumns(13)));
  9856. EXPECT_EQ(
  9857. "/* 一二三\n"
  9858. " * 四五六七\n"
  9859. " * 八 九\n"
  9860. " * 十 */",
  9861. format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9)));
  9862. EXPECT_EQ("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
  9863. " * 𝕓𝕪𝕥𝕖\n"
  9864. " * 𝖀𝕿𝕱-𝟠 */",
  9865. format("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12)));
  9866. }
  9867. #endif // _MSC_VER
  9868. TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
  9869. FormatStyle Style = getLLVMStyle();
  9870. Style.ConstructorInitializerIndentWidth = 4;
  9871. verifyFormat(
  9872. "SomeClass::Constructor()\n"
  9873. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  9874. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  9875. Style);
  9876. Style.ConstructorInitializerIndentWidth = 2;
  9877. verifyFormat(
  9878. "SomeClass::Constructor()\n"
  9879. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  9880. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  9881. Style);
  9882. Style.ConstructorInitializerIndentWidth = 0;
  9883. verifyFormat(
  9884. "SomeClass::Constructor()\n"
  9885. ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  9886. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  9887. Style);
  9888. Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
  9889. verifyFormat(
  9890. "SomeLongTemplateVariableName<\n"
  9891. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
  9892. Style);
  9893. verifyFormat(
  9894. "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  9895. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  9896. Style);
  9897. }
  9898. TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
  9899. FormatStyle Style = getLLVMStyle();
  9900. Style.BreakConstructorInitializersBeforeComma = true;
  9901. Style.ConstructorInitializerIndentWidth = 4;
  9902. verifyFormat("SomeClass::Constructor()\n"
  9903. " : a(a)\n"
  9904. " , b(b)\n"
  9905. " , c(c) {}",
  9906. Style);
  9907. verifyFormat("SomeClass::Constructor()\n"
  9908. " : a(a) {}",
  9909. Style);
  9910. Style.ColumnLimit = 0;
  9911. verifyFormat("SomeClass::Constructor()\n"
  9912. " : a(a) {}",
  9913. Style);
  9914. verifyFormat("SomeClass::Constructor() noexcept\n"
  9915. " : a(a) {}",
  9916. Style);
  9917. verifyFormat("SomeClass::Constructor()\n"
  9918. " : a(a)\n"
  9919. " , b(b)\n"
  9920. " , c(c) {}",
  9921. Style);
  9922. verifyFormat("SomeClass::Constructor()\n"
  9923. " : a(a) {\n"
  9924. " foo();\n"
  9925. " bar();\n"
  9926. "}",
  9927. Style);
  9928. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  9929. verifyFormat("SomeClass::Constructor()\n"
  9930. " : a(a)\n"
  9931. " , b(b)\n"
  9932. " , c(c) {\n}",
  9933. Style);
  9934. verifyFormat("SomeClass::Constructor()\n"
  9935. " : a(a) {\n}",
  9936. Style);
  9937. Style.ColumnLimit = 80;
  9938. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
  9939. Style.ConstructorInitializerIndentWidth = 2;
  9940. verifyFormat("SomeClass::Constructor()\n"
  9941. " : a(a)\n"
  9942. " , b(b)\n"
  9943. " , c(c) {}",
  9944. Style);
  9945. Style.ConstructorInitializerIndentWidth = 0;
  9946. verifyFormat("SomeClass::Constructor()\n"
  9947. ": a(a)\n"
  9948. ", b(b)\n"
  9949. ", c(c) {}",
  9950. Style);
  9951. Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  9952. Style.ConstructorInitializerIndentWidth = 4;
  9953. verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
  9954. verifyFormat(
  9955. "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
  9956. Style);
  9957. verifyFormat(
  9958. "SomeClass::Constructor()\n"
  9959. " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
  9960. Style);
  9961. Style.ConstructorInitializerIndentWidth = 4;
  9962. Style.ColumnLimit = 60;
  9963. verifyFormat("SomeClass::Constructor()\n"
  9964. " : aaaaaaaa(aaaaaaaa)\n"
  9965. " , aaaaaaaa(aaaaaaaa)\n"
  9966. " , aaaaaaaa(aaaaaaaa) {}",
  9967. Style);
  9968. }
  9969. TEST_F(FormatTest, Destructors) {
  9970. verifyFormat("void F(int &i) { i.~int(); }");
  9971. verifyFormat("void F(int &i) { i->~int(); }");
  9972. }
  9973. TEST_F(FormatTest, FormatsWithWebKitStyle) {
  9974. FormatStyle Style = getWebKitStyle();
  9975. // Don't indent in outer namespaces.
  9976. verifyFormat("namespace outer {\n"
  9977. "int i;\n"
  9978. "namespace inner {\n"
  9979. " int i;\n"
  9980. "} // namespace inner\n"
  9981. "} // namespace outer\n"
  9982. "namespace other_outer {\n"
  9983. "int i;\n"
  9984. "}",
  9985. Style);
  9986. // Don't indent case labels.
  9987. verifyFormat("switch (variable) {\n"
  9988. "case 1:\n"
  9989. "case 2:\n"
  9990. " doSomething();\n"
  9991. " break;\n"
  9992. "default:\n"
  9993. " ++variable;\n"
  9994. "}",
  9995. Style);
  9996. // Wrap before binary operators.
  9997. EXPECT_EQ("void f()\n"
  9998. "{\n"
  9999. " if (aaaaaaaaaaaaaaaa\n"
  10000. " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
  10001. " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  10002. " return;\n"
  10003. "}",
  10004. format("void f() {\n"
  10005. "if (aaaaaaaaaaaaaaaa\n"
  10006. "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
  10007. "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  10008. "return;\n"
  10009. "}",
  10010. Style));
  10011. // Allow functions on a single line.
  10012. verifyFormat("void f() { return; }", Style);
  10013. // Constructor initializers are formatted one per line with the "," on the
  10014. // new line.
  10015. verifyFormat("Constructor()\n"
  10016. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  10017. " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
  10018. " aaaaaaaaaaaaaa)\n"
  10019. " , aaaaaaaaaaaaaaaaaaaaaaa()\n"
  10020. "{\n"
  10021. "}",
  10022. Style);
  10023. verifyFormat("SomeClass::Constructor()\n"
  10024. " : a(a)\n"
  10025. "{\n"
  10026. "}",
  10027. Style);
  10028. EXPECT_EQ("SomeClass::Constructor()\n"
  10029. " : a(a)\n"
  10030. "{\n"
  10031. "}",
  10032. format("SomeClass::Constructor():a(a){}", Style));
  10033. verifyFormat("SomeClass::Constructor()\n"
  10034. " : a(a)\n"
  10035. " , b(b)\n"
  10036. " , c(c)\n"
  10037. "{\n"
  10038. "}",
  10039. Style);
  10040. verifyFormat("SomeClass::Constructor()\n"
  10041. " : a(a)\n"
  10042. "{\n"
  10043. " foo();\n"
  10044. " bar();\n"
  10045. "}",
  10046. Style);
  10047. // Access specifiers should be aligned left.
  10048. verifyFormat("class C {\n"
  10049. "public:\n"
  10050. " int i;\n"
  10051. "};",
  10052. Style);
  10053. // Do not align comments.
  10054. verifyFormat("int a; // Do not\n"
  10055. "double b; // align comments.",
  10056. Style);
  10057. // Do not align operands.
  10058. EXPECT_EQ("ASSERT(aaaa\n"
  10059. " || bbbb);",
  10060. format("ASSERT ( aaaa\n||bbbb);", Style));
  10061. // Accept input's line breaks.
  10062. EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
  10063. " || bbbbbbbbbbbbbbb) {\n"
  10064. " i++;\n"
  10065. "}",
  10066. format("if (aaaaaaaaaaaaaaa\n"
  10067. "|| bbbbbbbbbbbbbbb) { i++; }",
  10068. Style));
  10069. EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
  10070. " i++;\n"
  10071. "}",
  10072. format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
  10073. // Don't automatically break all macro definitions (llvm.org/PR17842).
  10074. verifyFormat("#define aNumber 10", Style);
  10075. // However, generally keep the line breaks that the user authored.
  10076. EXPECT_EQ("#define aNumber \\\n"
  10077. " 10",
  10078. format("#define aNumber \\\n"
  10079. " 10",
  10080. Style));
  10081. // Keep empty and one-element array literals on a single line.
  10082. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
  10083. " copyItems:YES];",
  10084. format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
  10085. "copyItems:YES];",
  10086. Style));
  10087. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
  10088. " copyItems:YES];",
  10089. format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
  10090. " copyItems:YES];",
  10091. Style));
  10092. // FIXME: This does not seem right, there should be more indentation before
  10093. // the array literal's entries. Nested blocks have the same problem.
  10094. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
  10095. " @\"a\",\n"
  10096. " @\"a\"\n"
  10097. "]\n"
  10098. " copyItems:YES];",
  10099. format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
  10100. " @\"a\",\n"
  10101. " @\"a\"\n"
  10102. " ]\n"
  10103. " copyItems:YES];",
  10104. Style));
  10105. EXPECT_EQ(
  10106. "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
  10107. " copyItems:YES];",
  10108. format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
  10109. " copyItems:YES];",
  10110. Style));
  10111. verifyFormat("[self.a b:c c:d];", Style);
  10112. EXPECT_EQ("[self.a b:c\n"
  10113. " c:d];",
  10114. format("[self.a b:c\n"
  10115. "c:d];",
  10116. Style));
  10117. }
  10118. TEST_F(FormatTest, FormatsLambdas) {
  10119. verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
  10120. verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
  10121. verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
  10122. verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
  10123. verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
  10124. verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
  10125. verifyFormat("void f() {\n"
  10126. " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
  10127. "}\n");
  10128. verifyFormat("void f() {\n"
  10129. " other(x.begin(), //\n"
  10130. " x.end(), //\n"
  10131. " [&](int, int) { return 1; });\n"
  10132. "}\n");
  10133. verifyFormat("SomeFunction([]() { // A cool function...\n"
  10134. " return 43;\n"
  10135. "});");
  10136. EXPECT_EQ("SomeFunction([]() {\n"
  10137. "#define A a\n"
  10138. " return 43;\n"
  10139. "});",
  10140. format("SomeFunction([](){\n"
  10141. "#define A a\n"
  10142. "return 43;\n"
  10143. "});"));
  10144. verifyFormat("void f() {\n"
  10145. " SomeFunction([](decltype(x), A *a) {});\n"
  10146. "}");
  10147. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  10148. " [](const aaaaaaaaaa &a) { return a; });");
  10149. verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
  10150. " SomeOtherFunctioooooooooooooooooooooooooon();\n"
  10151. "});");
  10152. verifyFormat("Constructor()\n"
  10153. " : Field([] { // comment\n"
  10154. " int i;\n"
  10155. " }) {}");
  10156. verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
  10157. " return some_parameter.size();\n"
  10158. "};");
  10159. verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
  10160. " [](const string &s) { return s; };");
  10161. verifyFormat("int i = aaaaaa ? 1 //\n"
  10162. " : [] {\n"
  10163. " return 2; //\n"
  10164. " }();");
  10165. verifyFormat("llvm::errs() << \"number of twos is \"\n"
  10166. " << std::count_if(v.begin(), v.end(), [](int x) {\n"
  10167. " return x == 2; // force break\n"
  10168. " });");
  10169. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n"
  10170. " int iiiiiiiiiiii) {\n"
  10171. " return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n"
  10172. "});",
  10173. getLLVMStyleWithColumns(60));
  10174. verifyFormat("SomeFunction({[&] {\n"
  10175. " // comment\n"
  10176. " },\n"
  10177. " [&] {\n"
  10178. " // comment\n"
  10179. " }});");
  10180. verifyFormat("SomeFunction({[&] {\n"
  10181. " // comment\n"
  10182. "}});");
  10183. verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n"
  10184. " [&]() { return true; },\n"
  10185. " aaaaa aaaaaaaaa);");
  10186. // Lambdas with return types.
  10187. verifyFormat("int c = []() -> int { return 2; }();\n");
  10188. verifyFormat("int c = []() -> int * { return 2; }();\n");
  10189. verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
  10190. verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
  10191. verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
  10192. verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
  10193. verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
  10194. verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
  10195. verifyFormat("[a, a]() -> a<1> {};");
  10196. verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
  10197. " int j) -> int {\n"
  10198. " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
  10199. "};");
  10200. verifyFormat(
  10201. "aaaaaaaaaaaaaaaaaaaaaa(\n"
  10202. " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
  10203. " return aaaaaaaaaaaaaaaaa;\n"
  10204. " });",
  10205. getLLVMStyleWithColumns(70));
  10206. // Multiple lambdas in the same parentheses change indentation rules.
  10207. verifyFormat("SomeFunction(\n"
  10208. " []() {\n"
  10209. " int i = 42;\n"
  10210. " return i;\n"
  10211. " },\n"
  10212. " []() {\n"
  10213. " int j = 43;\n"
  10214. " return j;\n"
  10215. " });");
  10216. // More complex introducers.
  10217. verifyFormat("return [i, args...] {};");
  10218. // Not lambdas.
  10219. verifyFormat("constexpr char hello[]{\"hello\"};");
  10220. verifyFormat("double &operator[](int i) { return 0; }\n"
  10221. "int i;");
  10222. verifyFormat("std::unique_ptr<int[]> foo() {}");
  10223. verifyFormat("int i = a[a][a]->f();");
  10224. verifyFormat("int i = (*b)[a]->f();");
  10225. // Other corner cases.
  10226. verifyFormat("void f() {\n"
  10227. " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
  10228. " );\n"
  10229. "}");
  10230. // Lambdas created through weird macros.
  10231. verifyFormat("void f() {\n"
  10232. " MACRO((const AA &a) { return 1; });\n"
  10233. " MACRO((AA &a) { return 1; });\n"
  10234. "}");
  10235. verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
  10236. " doo_dah();\n"
  10237. " doo_dah();\n"
  10238. " })) {\n"
  10239. "}");
  10240. verifyFormat("auto lambda = []() {\n"
  10241. " int a = 2\n"
  10242. "#if A\n"
  10243. " + 2\n"
  10244. "#endif\n"
  10245. " ;\n"
  10246. "};");
  10247. }
  10248. TEST_F(FormatTest, FormatsBlocks) {
  10249. FormatStyle ShortBlocks = getLLVMStyle();
  10250. ShortBlocks.AllowShortBlocksOnASingleLine = true;
  10251. verifyFormat("int (^Block)(int, int);", ShortBlocks);
  10252. verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
  10253. verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
  10254. verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
  10255. verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
  10256. verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
  10257. verifyFormat("foo(^{ bar(); });", ShortBlocks);
  10258. verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
  10259. verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
  10260. verifyFormat("[operation setCompletionBlock:^{\n"
  10261. " [self onOperationDone];\n"
  10262. "}];");
  10263. verifyFormat("int i = {[operation setCompletionBlock:^{\n"
  10264. " [self onOperationDone];\n"
  10265. "}]};");
  10266. verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
  10267. " f();\n"
  10268. "}];");
  10269. verifyFormat("int a = [operation block:^int(int *i) {\n"
  10270. " return 1;\n"
  10271. "}];");
  10272. verifyFormat("[myObject doSomethingWith:arg1\n"
  10273. " aaa:^int(int *a) {\n"
  10274. " return 1;\n"
  10275. " }\n"
  10276. " bbb:f(a * bbbbbbbb)];");
  10277. verifyFormat("[operation setCompletionBlock:^{\n"
  10278. " [self.delegate newDataAvailable];\n"
  10279. "}];",
  10280. getLLVMStyleWithColumns(60));
  10281. verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
  10282. " NSString *path = [self sessionFilePath];\n"
  10283. " if (path) {\n"
  10284. " // ...\n"
  10285. " }\n"
  10286. "});");
  10287. verifyFormat("[[SessionService sharedService]\n"
  10288. " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  10289. " if (window) {\n"
  10290. " [self windowDidLoad:window];\n"
  10291. " } else {\n"
  10292. " [self errorLoadingWindow];\n"
  10293. " }\n"
  10294. " }];");
  10295. verifyFormat("void (^largeBlock)(void) = ^{\n"
  10296. " // ...\n"
  10297. "};\n",
  10298. getLLVMStyleWithColumns(40));
  10299. verifyFormat("[[SessionService sharedService]\n"
  10300. " loadWindowWithCompletionBlock: //\n"
  10301. " ^(SessionWindow *window) {\n"
  10302. " if (window) {\n"
  10303. " [self windowDidLoad:window];\n"
  10304. " } else {\n"
  10305. " [self errorLoadingWindow];\n"
  10306. " }\n"
  10307. " }];",
  10308. getLLVMStyleWithColumns(60));
  10309. verifyFormat("[myObject doSomethingWith:arg1\n"
  10310. " firstBlock:^(Foo *a) {\n"
  10311. " // ...\n"
  10312. " int i;\n"
  10313. " }\n"
  10314. " secondBlock:^(Bar *b) {\n"
  10315. " // ...\n"
  10316. " int i;\n"
  10317. " }\n"
  10318. " thirdBlock:^Foo(Bar *b) {\n"
  10319. " // ...\n"
  10320. " int i;\n"
  10321. " }];");
  10322. verifyFormat("[myObject doSomethingWith:arg1\n"
  10323. " firstBlock:-1\n"
  10324. " secondBlock:^(Bar *b) {\n"
  10325. " // ...\n"
  10326. " int i;\n"
  10327. " }];");
  10328. verifyFormat("f(^{\n"
  10329. " @autoreleasepool {\n"
  10330. " if (a) {\n"
  10331. " g();\n"
  10332. " }\n"
  10333. " }\n"
  10334. "});");
  10335. verifyFormat("Block b = ^int *(A *a, B *b) {}");
  10336. FormatStyle FourIndent = getLLVMStyle();
  10337. FourIndent.ObjCBlockIndentWidth = 4;
  10338. verifyFormat("[operation setCompletionBlock:^{\n"
  10339. " [self onOperationDone];\n"
  10340. "}];",
  10341. FourIndent);
  10342. }
  10343. TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
  10344. FormatStyle ZeroColumn = getLLVMStyle();
  10345. ZeroColumn.ColumnLimit = 0;
  10346. verifyFormat("[[SessionService sharedService] "
  10347. "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  10348. " if (window) {\n"
  10349. " [self windowDidLoad:window];\n"
  10350. " } else {\n"
  10351. " [self errorLoadingWindow];\n"
  10352. " }\n"
  10353. "}];",
  10354. ZeroColumn);
  10355. EXPECT_EQ("[[SessionService sharedService]\n"
  10356. " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  10357. " if (window) {\n"
  10358. " [self windowDidLoad:window];\n"
  10359. " } else {\n"
  10360. " [self errorLoadingWindow];\n"
  10361. " }\n"
  10362. " }];",
  10363. format("[[SessionService sharedService]\n"
  10364. "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  10365. " if (window) {\n"
  10366. " [self windowDidLoad:window];\n"
  10367. " } else {\n"
  10368. " [self errorLoadingWindow];\n"
  10369. " }\n"
  10370. "}];",
  10371. ZeroColumn));
  10372. verifyFormat("[myObject doSomethingWith:arg1\n"
  10373. " firstBlock:^(Foo *a) {\n"
  10374. " // ...\n"
  10375. " int i;\n"
  10376. " }\n"
  10377. " secondBlock:^(Bar *b) {\n"
  10378. " // ...\n"
  10379. " int i;\n"
  10380. " }\n"
  10381. " thirdBlock:^Foo(Bar *b) {\n"
  10382. " // ...\n"
  10383. " int i;\n"
  10384. " }];",
  10385. ZeroColumn);
  10386. verifyFormat("f(^{\n"
  10387. " @autoreleasepool {\n"
  10388. " if (a) {\n"
  10389. " g();\n"
  10390. " }\n"
  10391. " }\n"
  10392. "});",
  10393. ZeroColumn);
  10394. verifyFormat("void (^largeBlock)(void) = ^{\n"
  10395. " // ...\n"
  10396. "};",
  10397. ZeroColumn);
  10398. ZeroColumn.AllowShortBlocksOnASingleLine = true;
  10399. EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
  10400. format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn));
  10401. ZeroColumn.AllowShortBlocksOnASingleLine = false;
  10402. EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
  10403. " int i;\n"
  10404. "};",
  10405. format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn));
  10406. }
  10407. TEST_F(FormatTest, SupportsCRLF) {
  10408. EXPECT_EQ("int a;\r\n"
  10409. "int b;\r\n"
  10410. "int c;\r\n",
  10411. format("int a;\r\n"
  10412. " int b;\r\n"
  10413. " int c;\r\n",
  10414. getLLVMStyle()));
  10415. EXPECT_EQ("int a;\r\n"
  10416. "int b;\r\n"
  10417. "int c;\r\n",
  10418. format("int a;\r\n"
  10419. " int b;\n"
  10420. " int c;\r\n",
  10421. getLLVMStyle()));
  10422. EXPECT_EQ("int a;\n"
  10423. "int b;\n"
  10424. "int c;\n",
  10425. format("int a;\r\n"
  10426. " int b;\n"
  10427. " int c;\n",
  10428. getLLVMStyle()));
  10429. EXPECT_EQ("\"aaaaaaa \"\r\n"
  10430. "\"bbbbbbb\";\r\n",
  10431. format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
  10432. EXPECT_EQ("#define A \\\r\n"
  10433. " b; \\\r\n"
  10434. " c; \\\r\n"
  10435. " d;\r\n",
  10436. format("#define A \\\r\n"
  10437. " b; \\\r\n"
  10438. " c; d; \r\n",
  10439. getGoogleStyle()));
  10440. EXPECT_EQ("/*\r\n"
  10441. "multi line block comments\r\n"
  10442. "should not introduce\r\n"
  10443. "an extra carriage return\r\n"
  10444. "*/\r\n",
  10445. format("/*\r\n"
  10446. "multi line block comments\r\n"
  10447. "should not introduce\r\n"
  10448. "an extra carriage return\r\n"
  10449. "*/\r\n"));
  10450. }
  10451. TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
  10452. verifyFormat("MY_CLASS(C) {\n"
  10453. " int i;\n"
  10454. " int j;\n"
  10455. "};");
  10456. }
  10457. TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
  10458. FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
  10459. TwoIndent.ContinuationIndentWidth = 2;
  10460. EXPECT_EQ("int i =\n"
  10461. " longFunction(\n"
  10462. " arg);",
  10463. format("int i = longFunction(arg);", TwoIndent));
  10464. FormatStyle SixIndent = getLLVMStyleWithColumns(20);
  10465. SixIndent.ContinuationIndentWidth = 6;
  10466. EXPECT_EQ("int i =\n"
  10467. " longFunction(\n"
  10468. " arg);",
  10469. format("int i = longFunction(arg);", SixIndent));
  10470. }
  10471. TEST_F(FormatTest, SpacesInAngles) {
  10472. FormatStyle Spaces = getLLVMStyle();
  10473. Spaces.SpacesInAngles = true;
  10474. verifyFormat("static_cast< int >(arg);", Spaces);
  10475. verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
  10476. verifyFormat("f< int, float >();", Spaces);
  10477. verifyFormat("template <> g() {}", Spaces);
  10478. verifyFormat("template < std::vector< int > > f() {}", Spaces);
  10479. verifyFormat("std::function< void(int, int) > fct;", Spaces);
  10480. verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
  10481. Spaces);
  10482. Spaces.Standard = FormatStyle::LS_Cpp03;
  10483. Spaces.SpacesInAngles = true;
  10484. verifyFormat("A< A< int > >();", Spaces);
  10485. Spaces.SpacesInAngles = false;
  10486. verifyFormat("A<A<int> >();", Spaces);
  10487. Spaces.Standard = FormatStyle::LS_Cpp11;
  10488. Spaces.SpacesInAngles = true;
  10489. verifyFormat("A< A< int > >();", Spaces);
  10490. Spaces.SpacesInAngles = false;
  10491. verifyFormat("A<A<int>>();", Spaces);
  10492. }
  10493. TEST_F(FormatTest, TripleAngleBrackets) {
  10494. verifyFormat("f<<<1, 1>>>();");
  10495. verifyFormat("f<<<1, 1, 1, s>>>();");
  10496. verifyFormat("f<<<a, b, c, d>>>();");
  10497. EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
  10498. verifyFormat("f<param><<<1, 1>>>();");
  10499. verifyFormat("f<1><<<1, 1>>>();");
  10500. EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
  10501. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  10502. "aaaaaaaaaaa<<<\n 1, 1>>>();");
  10503. }
  10504. TEST_F(FormatTest, MergeLessLessAtEnd) {
  10505. verifyFormat("<<");
  10506. EXPECT_EQ("< < <", format("\\\n<<<"));
  10507. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  10508. "aaallvm::outs() <<");
  10509. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  10510. "aaaallvm::outs()\n <<");
  10511. }
  10512. TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
  10513. std::string code = "#if A\n"
  10514. "#if B\n"
  10515. "a.\n"
  10516. "#endif\n"
  10517. " a = 1;\n"
  10518. "#else\n"
  10519. "#endif\n"
  10520. "#if C\n"
  10521. "#else\n"
  10522. "#endif\n";
  10523. EXPECT_EQ(code, format(code));
  10524. }
  10525. TEST_F(FormatTest, HandleConflictMarkers) {
  10526. // Git/SVN conflict markers.
  10527. EXPECT_EQ("int a;\n"
  10528. "void f() {\n"
  10529. " callme(some(parameter1,\n"
  10530. "<<<<<<< text by the vcs\n"
  10531. " parameter2),\n"
  10532. "||||||| text by the vcs\n"
  10533. " parameter2),\n"
  10534. " parameter3,\n"
  10535. "======= text by the vcs\n"
  10536. " parameter2, parameter3),\n"
  10537. ">>>>>>> text by the vcs\n"
  10538. " otherparameter);\n",
  10539. format("int a;\n"
  10540. "void f() {\n"
  10541. " callme(some(parameter1,\n"
  10542. "<<<<<<< text by the vcs\n"
  10543. " parameter2),\n"
  10544. "||||||| text by the vcs\n"
  10545. " parameter2),\n"
  10546. " parameter3,\n"
  10547. "======= text by the vcs\n"
  10548. " parameter2,\n"
  10549. " parameter3),\n"
  10550. ">>>>>>> text by the vcs\n"
  10551. " otherparameter);\n"));
  10552. // Perforce markers.
  10553. EXPECT_EQ("void f() {\n"
  10554. " function(\n"
  10555. ">>>> text by the vcs\n"
  10556. " parameter,\n"
  10557. "==== text by the vcs\n"
  10558. " parameter,\n"
  10559. "==== text by the vcs\n"
  10560. " parameter,\n"
  10561. "<<<< text by the vcs\n"
  10562. " parameter);\n",
  10563. format("void f() {\n"
  10564. " function(\n"
  10565. ">>>> text by the vcs\n"
  10566. " parameter,\n"
  10567. "==== text by the vcs\n"
  10568. " parameter,\n"
  10569. "==== text by the vcs\n"
  10570. " parameter,\n"
  10571. "<<<< text by the vcs\n"
  10572. " parameter);\n"));
  10573. EXPECT_EQ("<<<<<<<\n"
  10574. "|||||||\n"
  10575. "=======\n"
  10576. ">>>>>>>",
  10577. format("<<<<<<<\n"
  10578. "|||||||\n"
  10579. "=======\n"
  10580. ">>>>>>>"));
  10581. EXPECT_EQ("<<<<<<<\n"
  10582. "|||||||\n"
  10583. "int i;\n"
  10584. "=======\n"
  10585. ">>>>>>>",
  10586. format("<<<<<<<\n"
  10587. "|||||||\n"
  10588. "int i;\n"
  10589. "=======\n"
  10590. ">>>>>>>"));
  10591. // FIXME: Handle parsing of macros around conflict markers correctly:
  10592. EXPECT_EQ("#define Macro \\\n"
  10593. "<<<<<<<\n"
  10594. "Something \\\n"
  10595. "|||||||\n"
  10596. "Else \\\n"
  10597. "=======\n"
  10598. "Other \\\n"
  10599. ">>>>>>>\n"
  10600. " End int i;\n",
  10601. format("#define Macro \\\n"
  10602. "<<<<<<<\n"
  10603. " Something \\\n"
  10604. "|||||||\n"
  10605. " Else \\\n"
  10606. "=======\n"
  10607. " Other \\\n"
  10608. ">>>>>>>\n"
  10609. " End\n"
  10610. "int i;\n"));
  10611. }
  10612. TEST_F(FormatTest, DisableRegions) {
  10613. EXPECT_EQ("int i;\n"
  10614. "// clang-format off\n"
  10615. " int j;\n"
  10616. "// clang-format on\n"
  10617. "int k;",
  10618. format(" int i;\n"
  10619. " // clang-format off\n"
  10620. " int j;\n"
  10621. " // clang-format on\n"
  10622. " int k;"));
  10623. EXPECT_EQ("int i;\n"
  10624. "/* clang-format off */\n"
  10625. " int j;\n"
  10626. "/* clang-format on */\n"
  10627. "int k;",
  10628. format(" int i;\n"
  10629. " /* clang-format off */\n"
  10630. " int j;\n"
  10631. " /* clang-format on */\n"
  10632. " int k;"));
  10633. }
  10634. TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
  10635. format("? ) =");
  10636. verifyNoCrash("#define a\\\n /**/}");
  10637. }
  10638. TEST_F(FormatTest, FormatsTableGenCode) {
  10639. FormatStyle Style = getLLVMStyle();
  10640. Style.Language = FormatStyle::LK_TableGen;
  10641. verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
  10642. }
  10643. // Since this test case uses UNIX-style file path. We disable it for MS
  10644. // compiler.
  10645. #if !defined(_MSC_VER) && !defined(__MINGW32__)
  10646. TEST(FormatStyle, GetStyleOfFile) {
  10647. vfs::InMemoryFileSystem FS;
  10648. // Test 1: format file in the same directory.
  10649. ASSERT_TRUE(
  10650. FS.addFile("/a/.clang-format", 0,
  10651. llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
  10652. ASSERT_TRUE(
  10653. FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
  10654. auto Style1 = getStyle("file", "/a/.clang-format", "Google", &FS);
  10655. ASSERT_EQ(Style1, getLLVMStyle());
  10656. // Test 2: fallback to default.
  10657. ASSERT_TRUE(
  10658. FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
  10659. auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", &FS);
  10660. ASSERT_EQ(Style2, getMozillaStyle());
  10661. // Test 3: format file in parent directory.
  10662. ASSERT_TRUE(
  10663. FS.addFile("/c/.clang-format", 0,
  10664. llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
  10665. ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
  10666. llvm::MemoryBuffer::getMemBuffer("int i;")));
  10667. auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", &FS);
  10668. ASSERT_EQ(Style3, getGoogleStyle());
  10669. }
  10670. #endif // _MSC_VER
  10671. class ReplacementTest : public ::testing::Test {
  10672. protected:
  10673. tooling::Replacement createReplacement(SourceLocation Start, unsigned Length,
  10674. llvm::StringRef ReplacementText) {
  10675. return tooling::Replacement(Context.Sources, Start, Length,
  10676. ReplacementText);
  10677. }
  10678. RewriterTestContext Context;
  10679. };
  10680. TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
  10681. // Column limit is 20.
  10682. std::string Code = "Type *a =\n"
  10683. " new Type();\n"
  10684. "g(iiiii, 0, jjjjj,\n"
  10685. " 0, kkkkk, 0, mm);\n"
  10686. "int bad = format ;";
  10687. std::string Expected = "auto a = new Type();\n"
  10688. "g(iiiii, nullptr,\n"
  10689. " jjjjj, nullptr,\n"
  10690. " kkkkk, nullptr,\n"
  10691. " mm);\n"
  10692. "int bad = format ;";
  10693. FileID ID = Context.createInMemoryFile("format.cpp", Code);
  10694. tooling::Replacements Replaces;
  10695. Replaces.insert(tooling::Replacement(
  10696. Context.Sources, Context.getLocation(ID, 1, 1), 6, "auto "));
  10697. Replaces.insert(tooling::Replacement(
  10698. Context.Sources, Context.getLocation(ID, 3, 10), 1, "nullptr"));
  10699. Replaces.insert(tooling::Replacement(
  10700. Context.Sources, Context.getLocation(ID, 4, 3), 1, "nullptr"));
  10701. Replaces.insert(tooling::Replacement(
  10702. Context.Sources, Context.getLocation(ID, 4, 13), 1, "nullptr"));
  10703. format::FormatStyle Style = format::getLLVMStyle();
  10704. Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
  10705. EXPECT_EQ(Expected, applyAllReplacements(
  10706. Code, formatReplacements(Code, Replaces, Style)));
  10707. }
  10708. TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
  10709. std::string Code = "#include \"a.h\"\n"
  10710. "#include \"c.h\"\n"
  10711. "\n"
  10712. "int main() {\n"
  10713. " return 0;\n"
  10714. "}";
  10715. std::string Expected = "#include \"a.h\"\n"
  10716. "#include \"b.h\"\n"
  10717. "#include \"c.h\"\n"
  10718. "\n"
  10719. "int main() {\n"
  10720. " return 0;\n"
  10721. "}";
  10722. FileID ID = Context.createInMemoryFile("fix.cpp", Code);
  10723. tooling::Replacements Replaces;
  10724. Replaces.insert(tooling::Replacement(
  10725. Context.Sources, Context.getLocation(ID, 1, 1), 0, "#include \"b.h\"\n"));
  10726. format::FormatStyle Style = format::getLLVMStyle();
  10727. Style.SortIncludes = true;
  10728. auto FinalReplaces = formatReplacements(Code, Replaces, Style);
  10729. EXPECT_EQ(Expected, applyAllReplacements(Code, FinalReplaces));
  10730. }
  10731. } // end namespace
  10732. } // end namespace format
  10733. } // end namespace clang