FormatTest.cpp 233 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232
  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. #define DEBUG_TYPE "format-test"
  10. #include "clang/Format/Format.h"
  11. #include "clang/Lex/Lexer.h"
  12. #include "llvm/Support/Debug.h"
  13. #include "gtest/gtest.h"
  14. namespace clang {
  15. namespace format {
  16. class FormatTest : public ::testing::Test {
  17. protected:
  18. std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length,
  19. const FormatStyle &Style) {
  20. DEBUG(llvm::errs() << "---\n");
  21. std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
  22. tooling::Replacements Replaces = reformat(Style, Code, Ranges);
  23. ReplacementCount = Replaces.size();
  24. std::string Result = applyAllReplacements(Code, Replaces);
  25. EXPECT_NE("", Result);
  26. DEBUG(llvm::errs() << "\n" << Result << "\n\n");
  27. return Result;
  28. }
  29. std::string
  30. format(llvm::StringRef Code, const FormatStyle &Style = getLLVMStyle()) {
  31. return format(Code, 0, Code.size(), Style);
  32. }
  33. std::string messUp(llvm::StringRef Code) {
  34. std::string MessedUp(Code.str());
  35. bool InComment = false;
  36. bool InPreprocessorDirective = false;
  37. bool JustReplacedNewline = false;
  38. for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) {
  39. if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') {
  40. if (JustReplacedNewline)
  41. MessedUp[i - 1] = '\n';
  42. InComment = true;
  43. } else if (MessedUp[i] == '#' && (JustReplacedNewline || i == 0)) {
  44. if (i != 0)
  45. MessedUp[i - 1] = '\n';
  46. InPreprocessorDirective = true;
  47. } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') {
  48. MessedUp[i] = ' ';
  49. MessedUp[i + 1] = ' ';
  50. } else if (MessedUp[i] == '\n') {
  51. if (InComment) {
  52. InComment = false;
  53. } else if (InPreprocessorDirective) {
  54. InPreprocessorDirective = false;
  55. } else {
  56. JustReplacedNewline = true;
  57. MessedUp[i] = ' ';
  58. }
  59. } else if (MessedUp[i] != ' ') {
  60. JustReplacedNewline = false;
  61. }
  62. }
  63. return MessedUp;
  64. }
  65. FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
  66. FormatStyle Style = getLLVMStyle();
  67. Style.ColumnLimit = ColumnLimit;
  68. return Style;
  69. }
  70. FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
  71. FormatStyle Style = getGoogleStyle();
  72. Style.ColumnLimit = ColumnLimit;
  73. return Style;
  74. }
  75. void verifyFormat(llvm::StringRef Code,
  76. const FormatStyle &Style = getLLVMStyle()) {
  77. EXPECT_EQ(Code.str(), format(messUp(Code), Style));
  78. }
  79. void verifyGoogleFormat(llvm::StringRef Code) {
  80. verifyFormat(Code, getGoogleStyle());
  81. }
  82. void verifyIndependentOfContext(llvm::StringRef text) {
  83. verifyFormat(text);
  84. verifyFormat(llvm::Twine("void f() { " + text + " }").str());
  85. }
  86. int ReplacementCount;
  87. };
  88. TEST_F(FormatTest, MessUp) {
  89. EXPECT_EQ("1 2 3", messUp("1 2 3"));
  90. EXPECT_EQ("1 2 3\n", messUp("1\n2\n3\n"));
  91. EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc"));
  92. EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc"));
  93. EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne"));
  94. }
  95. //===----------------------------------------------------------------------===//
  96. // Basic function tests.
  97. //===----------------------------------------------------------------------===//
  98. TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) {
  99. EXPECT_EQ(";", format(";"));
  100. }
  101. TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
  102. EXPECT_EQ("int i;", format(" int i;"));
  103. EXPECT_EQ("\nint i;", format(" \n\t \r int i;"));
  104. EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
  105. EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
  106. }
  107. TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
  108. EXPECT_EQ("int i;", format("int\ni;"));
  109. }
  110. TEST_F(FormatTest, FormatsNestedBlockStatements) {
  111. EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
  112. }
  113. TEST_F(FormatTest, FormatsNestedCall) {
  114. verifyFormat("Method(f1, f2(f3));");
  115. verifyFormat("Method(f1(f2, f3()));");
  116. verifyFormat("Method(f1(f2, (f3())));");
  117. }
  118. TEST_F(FormatTest, NestedNameSpecifiers) {
  119. verifyFormat("vector< ::Type> v;");
  120. verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
  121. verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
  122. }
  123. TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
  124. EXPECT_EQ("if (a) {\n"
  125. " f();\n"
  126. "}",
  127. format("if(a){f();}"));
  128. EXPECT_EQ(4, ReplacementCount);
  129. EXPECT_EQ("if (a) {\n"
  130. " f();\n"
  131. "}",
  132. format("if (a) {\n"
  133. " f();\n"
  134. "}"));
  135. EXPECT_EQ(0, ReplacementCount);
  136. }
  137. TEST_F(FormatTest, RemovesTrailingWhitespaceOfFormattedLine) {
  138. EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0, getLLVMStyle()));
  139. EXPECT_EQ("int a;", format("int a; "));
  140. EXPECT_EQ("int a;\n", format("int a; \n \n \n "));
  141. EXPECT_EQ("int a;\nint b; ",
  142. format("int a; \nint b; ", 0, 0, getLLVMStyle()));
  143. }
  144. TEST_F(FormatTest, FormatsCorrectRegionForLeadingWhitespace) {
  145. EXPECT_EQ("int b;\nint a;",
  146. format("int b;\n int a;", 7, 0, getLLVMStyle()));
  147. EXPECT_EQ("int b;\n int a;",
  148. format("int b;\n int a;", 6, 0, getLLVMStyle()));
  149. EXPECT_EQ("#define A \\\n"
  150. " int a; \\\n"
  151. " int b;",
  152. format("#define A \\\n"
  153. " int a; \\\n"
  154. " int b;",
  155. 26, 0, getLLVMStyleWithColumns(12)));
  156. EXPECT_EQ("#define A \\\n"
  157. " int a; \\\n"
  158. " int b;",
  159. format("#define A \\\n"
  160. " int a; \\\n"
  161. " int b;",
  162. 25, 0, getLLVMStyleWithColumns(12)));
  163. }
  164. TEST_F(FormatTest, RemovesWhitespaceWhenTriggeredOnEmptyLine) {
  165. EXPECT_EQ("int a;\n\n int b;",
  166. format("int a;\n \n\n int b;", 7, 0, getLLVMStyle()));
  167. EXPECT_EQ("int a;\n\n int b;",
  168. format("int a;\n \n\n int b;", 9, 0, getLLVMStyle()));
  169. }
  170. TEST_F(FormatTest, RemovesEmptyLines) {
  171. EXPECT_EQ("class C {\n"
  172. " int i;\n"
  173. "};",
  174. format("class C {\n"
  175. " int i;\n"
  176. "\n"
  177. "};"));
  178. // Don't remove empty lines in more complex control statements.
  179. EXPECT_EQ("void f() {\n"
  180. " if (a) {\n"
  181. " f();\n"
  182. "\n"
  183. " } else if (b) {\n"
  184. " f();\n"
  185. " }\n"
  186. "}",
  187. format("void f() {\n"
  188. " if (a) {\n"
  189. " f();\n"
  190. "\n"
  191. " } else if (b) {\n"
  192. " f();\n"
  193. "\n"
  194. " }\n"
  195. "\n"
  196. "}"));
  197. // FIXME: This is slightly inconsistent.
  198. EXPECT_EQ("namespace {\n"
  199. "int i;\n"
  200. "}",
  201. format("namespace {\n"
  202. "int i;\n"
  203. "\n"
  204. "}"));
  205. EXPECT_EQ("namespace {\n"
  206. "int i;\n"
  207. "\n"
  208. "} // namespace",
  209. format("namespace {\n"
  210. "int i;\n"
  211. "\n"
  212. "} // namespace"));
  213. }
  214. TEST_F(FormatTest, ReformatsMovedLines) {
  215. EXPECT_EQ(
  216. "template <typename T> T *getFETokenInfo() const {\n"
  217. " return static_cast<T *>(FETokenInfo);\n"
  218. "}\n"
  219. " int a; // <- Should not be formatted",
  220. format(
  221. "template<typename T>\n"
  222. "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
  223. " int a; // <- Should not be formatted",
  224. 9, 5, getLLVMStyle()));
  225. }
  226. //===----------------------------------------------------------------------===//
  227. // Tests for control statements.
  228. //===----------------------------------------------------------------------===//
  229. TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
  230. verifyFormat("if (true)\n f();\ng();");
  231. verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
  232. verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
  233. FormatStyle AllowsMergedIf = getLLVMStyle();
  234. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  235. verifyFormat("if (a)\n"
  236. " // comment\n"
  237. " f();",
  238. AllowsMergedIf);
  239. verifyFormat("if (a)\n"
  240. " ;",
  241. AllowsMergedIf);
  242. verifyFormat("if (a)\n"
  243. " if (b) return;",
  244. AllowsMergedIf);
  245. verifyFormat("if (a) // Can't merge this\n"
  246. " f();\n",
  247. AllowsMergedIf);
  248. verifyFormat("if (a) /* still don't merge */\n"
  249. " f();",
  250. AllowsMergedIf);
  251. verifyFormat("if (a) { // Never merge this\n"
  252. " f();\n"
  253. "}",
  254. AllowsMergedIf);
  255. verifyFormat("if (a) { /* Never merge this */\n"
  256. " f();\n"
  257. "}",
  258. AllowsMergedIf);
  259. EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1, AllowsMergedIf));
  260. EXPECT_EQ("if (a) return; // comment",
  261. format("if(a)\nreturn; // comment", 20, 1, AllowsMergedIf));
  262. AllowsMergedIf.ColumnLimit = 14;
  263. verifyFormat("if (a) return;", AllowsMergedIf);
  264. verifyFormat("if (aaaaaaaaa)\n"
  265. " return;",
  266. AllowsMergedIf);
  267. AllowsMergedIf.ColumnLimit = 13;
  268. verifyFormat("if (a)\n return;", AllowsMergedIf);
  269. }
  270. TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
  271. FormatStyle AllowsMergedLoops = getLLVMStyle();
  272. AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
  273. verifyFormat("while (true) continue;", AllowsMergedLoops);
  274. verifyFormat("for (;;) continue;", AllowsMergedLoops);
  275. verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
  276. verifyFormat("while (true)\n"
  277. " ;",
  278. AllowsMergedLoops);
  279. verifyFormat("for (;;)\n"
  280. " ;",
  281. AllowsMergedLoops);
  282. verifyFormat("for (;;)\n"
  283. " for (;;) continue;",
  284. AllowsMergedLoops);
  285. verifyFormat("for (;;) // Can't merge this\n"
  286. " continue;",
  287. AllowsMergedLoops);
  288. verifyFormat("for (;;) /* still don't merge */\n"
  289. " continue;",
  290. AllowsMergedLoops);
  291. }
  292. TEST_F(FormatTest, ParseIfElse) {
  293. verifyFormat("if (true)\n"
  294. " if (true)\n"
  295. " if (true)\n"
  296. " f();\n"
  297. " else\n"
  298. " g();\n"
  299. " else\n"
  300. " h();\n"
  301. "else\n"
  302. " i();");
  303. verifyFormat("if (true)\n"
  304. " if (true)\n"
  305. " if (true) {\n"
  306. " if (true)\n"
  307. " f();\n"
  308. " } else {\n"
  309. " g();\n"
  310. " }\n"
  311. " else\n"
  312. " h();\n"
  313. "else {\n"
  314. " i();\n"
  315. "}");
  316. }
  317. TEST_F(FormatTest, ElseIf) {
  318. verifyFormat("if (a) {\n} else if (b) {\n}");
  319. verifyFormat("if (a)\n"
  320. " f();\n"
  321. "else if (b)\n"
  322. " g();\n"
  323. "else\n"
  324. " h();");
  325. }
  326. TEST_F(FormatTest, FormatsForLoop) {
  327. verifyFormat(
  328. "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
  329. " ++VeryVeryLongLoopVariable)\n"
  330. " ;");
  331. verifyFormat("for (;;)\n"
  332. " f();");
  333. verifyFormat("for (;;) {\n}");
  334. verifyFormat("for (;;) {\n"
  335. " f();\n"
  336. "}");
  337. verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
  338. verifyFormat(
  339. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  340. " E = UnwrappedLines.end();\n"
  341. " I != E; ++I) {\n}");
  342. verifyFormat(
  343. "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
  344. " ++IIIII) {\n}");
  345. verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
  346. " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
  347. " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
  348. verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
  349. " I = FD->getDeclsInPrototypeScope().begin(),\n"
  350. " E = FD->getDeclsInPrototypeScope().end();\n"
  351. " I != E; ++I) {\n}");
  352. // FIXME: Not sure whether we want extra identation in line 3 here:
  353. verifyFormat(
  354. "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  355. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
  356. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  357. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  358. " ++aaaaaaaaaaa) {\n}");
  359. verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
  360. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  361. "}");
  362. verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
  363. " aaaaaaaaaa);\n"
  364. " iter; ++iter) {\n"
  365. "}");
  366. FormatStyle NoBinPacking = getLLVMStyle();
  367. NoBinPacking.BinPackParameters = false;
  368. verifyFormat("for (int aaaaaaaaaaa = 1;\n"
  369. " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
  370. " aaaaaaaaaaaaaaaa,\n"
  371. " aaaaaaaaaaaaaaaa,\n"
  372. " aaaaaaaaaaaaaaaa);\n"
  373. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  374. "}",
  375. NoBinPacking);
  376. verifyFormat(
  377. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  378. " E = UnwrappedLines.end();\n"
  379. " I != E;\n"
  380. " ++I) {\n}",
  381. NoBinPacking);
  382. }
  383. TEST_F(FormatTest, RangeBasedForLoops) {
  384. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  385. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  386. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
  387. " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
  388. verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
  389. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  390. }
  391. TEST_F(FormatTest, FormatsWhileLoop) {
  392. verifyFormat("while (true) {\n}");
  393. verifyFormat("while (true)\n"
  394. " f();");
  395. verifyFormat("while () {\n}");
  396. verifyFormat("while () {\n"
  397. " f();\n"
  398. "}");
  399. }
  400. TEST_F(FormatTest, FormatsDoWhile) {
  401. verifyFormat("do {\n"
  402. " do_something();\n"
  403. "} while (something());");
  404. verifyFormat("do\n"
  405. " do_something();\n"
  406. "while (something());");
  407. }
  408. TEST_F(FormatTest, FormatsSwitchStatement) {
  409. verifyFormat("switch (x) {\n"
  410. "case 1:\n"
  411. " f();\n"
  412. " break;\n"
  413. "case kFoo:\n"
  414. "case ns::kBar:\n"
  415. "case kBaz:\n"
  416. " break;\n"
  417. "default:\n"
  418. " g();\n"
  419. " break;\n"
  420. "}");
  421. verifyFormat("switch (x) {\n"
  422. "case 1: {\n"
  423. " f();\n"
  424. " break;\n"
  425. "}\n"
  426. "}");
  427. verifyFormat("switch (x) {\n"
  428. "case 1: {\n"
  429. " f();\n"
  430. " {\n"
  431. " g();\n"
  432. " h();\n"
  433. " }\n"
  434. " break;\n"
  435. "}\n"
  436. "}");
  437. verifyFormat("switch (x) {\n"
  438. "case 1: {\n"
  439. " f();\n"
  440. " if (foo) {\n"
  441. " g();\n"
  442. " h();\n"
  443. " }\n"
  444. " break;\n"
  445. "}\n"
  446. "}");
  447. verifyFormat("switch (x) {\n"
  448. "case 1: {\n"
  449. " f();\n"
  450. " g();\n"
  451. "} break;\n"
  452. "}");
  453. verifyFormat("switch (test)\n"
  454. " ;");
  455. verifyFormat("switch (x) {\n"
  456. "default: {\n"
  457. " // Do nothing.\n"
  458. "}\n"
  459. "}");
  460. verifyFormat("switch (x) {\n"
  461. "// comment\n"
  462. "// if 1, do f()\n"
  463. "case 1:\n"
  464. " f();\n"
  465. "}");
  466. verifyFormat("switch (x) {\n"
  467. "case 1:\n"
  468. " // Do amazing stuff\n"
  469. " {\n"
  470. " f();\n"
  471. " g();\n"
  472. " }\n"
  473. " break;\n"
  474. "}");
  475. verifyFormat("#define A \\\n"
  476. " switch (x) { \\\n"
  477. " case a: \\\n"
  478. " foo = b; \\\n"
  479. " }", getLLVMStyleWithColumns(20));
  480. verifyGoogleFormat("switch (x) {\n"
  481. " case 1:\n"
  482. " f();\n"
  483. " break;\n"
  484. " case kFoo:\n"
  485. " case ns::kBar:\n"
  486. " case kBaz:\n"
  487. " break;\n"
  488. " default:\n"
  489. " g();\n"
  490. " break;\n"
  491. "}");
  492. verifyGoogleFormat("switch (x) {\n"
  493. " case 1: {\n"
  494. " f();\n"
  495. " break;\n"
  496. " }\n"
  497. "}");
  498. verifyGoogleFormat("switch (test)\n"
  499. " ;");
  500. verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
  501. " case OP_name: \\\n"
  502. " return operations::Operation##name\n");
  503. verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
  504. " // Get the correction operation class.\n"
  505. " switch (OpCode) {\n"
  506. " CASE(Add);\n"
  507. " CASE(Subtract);\n"
  508. " default:\n"
  509. " return operations::Unknown;\n"
  510. " }\n"
  511. "#undef OPERATION_CASE\n"
  512. "}");
  513. }
  514. TEST_F(FormatTest, FormatsLabels) {
  515. verifyFormat("void f() {\n"
  516. " some_code();\n"
  517. "test_label:\n"
  518. " some_other_code();\n"
  519. " {\n"
  520. " some_more_code();\n"
  521. " another_label:\n"
  522. " some_more_code();\n"
  523. " }\n"
  524. "}");
  525. verifyFormat("some_code();\n"
  526. "test_label:\n"
  527. "some_other_code();");
  528. }
  529. //===----------------------------------------------------------------------===//
  530. // Tests for comments.
  531. //===----------------------------------------------------------------------===//
  532. TEST_F(FormatTest, UnderstandsSingleLineComments) {
  533. verifyFormat("//* */");
  534. verifyFormat("// line 1\n"
  535. "// line 2\n"
  536. "void f() {}\n");
  537. verifyFormat("void f() {\n"
  538. " // Doesn't do anything\n"
  539. "}");
  540. verifyFormat("SomeObject\n"
  541. " // Calling someFunction on SomeObject\n"
  542. " .someFunction();");
  543. verifyFormat("void f(int i, // some comment (probably for i)\n"
  544. " int j, // some comment (probably for j)\n"
  545. " int k); // some comment (probably for k)");
  546. verifyFormat("void f(int i,\n"
  547. " // some comment (probably for j)\n"
  548. " int j,\n"
  549. " // some comment (probably for k)\n"
  550. " int k);");
  551. verifyFormat("int i // This is a fancy variable\n"
  552. " = 5; // with nicely aligned comment.");
  553. verifyFormat("// Leading comment.\n"
  554. "int a; // Trailing comment.");
  555. verifyFormat("int a; // Trailing comment\n"
  556. " // on 2\n"
  557. " // or 3 lines.\n"
  558. "int b;");
  559. verifyFormat("int a; // Trailing comment\n"
  560. "\n"
  561. "// Leading comment.\n"
  562. "int b;");
  563. verifyFormat("int a; // Comment.\n"
  564. " // More details.\n"
  565. "int bbbb; // Another comment.");
  566. verifyFormat(
  567. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  568. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n"
  569. "int cccccccccccccccccccccccccccccc; // comment\n"
  570. "int ddd; // looooooooooooooooooooooooong comment\n"
  571. "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  572. "int bbbbbbbbbbbbbbbbbbbbb; // comment\n"
  573. "int ccccccccccccccccccc; // comment");
  574. verifyFormat("#include \"a\" // comment\n"
  575. "#include \"a/b/c\" // comment");
  576. verifyFormat("#include <a> // comment\n"
  577. "#include <a/b/c> // comment");
  578. EXPECT_EQ("#include \\\n"
  579. " \"a\" // comment\n"
  580. "#include \"a/b/c\" // comment",
  581. format("#include \\\n"
  582. " \"a\" // comment\n"
  583. "#include \"a/b/c\" // comment"));
  584. verifyFormat("enum E {\n"
  585. " // comment\n"
  586. " VAL_A, // comment\n"
  587. " VAL_B\n"
  588. "};");
  589. verifyFormat(
  590. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  591. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
  592. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  593. " // Comment inside a statement.\n"
  594. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  595. verifyFormat(
  596. "bool aaaaaaaaaaaaa = // comment\n"
  597. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  598. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  599. verifyFormat("int aaaa; // aaaaa\n"
  600. "int aa; // aaaaaaa",
  601. getLLVMStyleWithColumns(20));
  602. EXPECT_EQ("void f() { // This does something ..\n"
  603. "}\n"
  604. "int a; // This is unrelated",
  605. format("void f() { // This does something ..\n"
  606. " }\n"
  607. "int a; // This is unrelated"));
  608. EXPECT_EQ("class C {\n"
  609. " void f() { // This does something ..\n"
  610. " } // awesome..\n"
  611. "\n"
  612. " int a; // This is unrelated\n"
  613. "};",
  614. format("class C{void f() { // This does something ..\n"
  615. " } // awesome..\n"
  616. " \n"
  617. "int a; // This is unrelated\n"
  618. "};"));
  619. EXPECT_EQ("int i; // single line trailing comment",
  620. format("int i;\\\n// single line trailing comment"));
  621. verifyGoogleFormat("int a; // Trailing comment.");
  622. verifyFormat("someFunction(anotherFunction( // Force break.\n"
  623. " parameter));");
  624. verifyGoogleFormat("#endif // HEADER_GUARD");
  625. verifyFormat("const char *test[] = {\n"
  626. " // A\n"
  627. " \"aaaa\",\n"
  628. " // B\n"
  629. " \"aaaaa\",\n"
  630. "};");
  631. verifyGoogleFormat(
  632. "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  633. " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment");
  634. EXPECT_EQ("D(a, {\n"
  635. " // test\n"
  636. " int a;\n"
  637. "});",
  638. format("D(a, {\n"
  639. "// test\n"
  640. "int a;\n"
  641. "});"));
  642. EXPECT_EQ("lineWith(); // comment\n"
  643. "// at start\n"
  644. "otherLine();",
  645. format("lineWith(); // comment\n"
  646. "// at start\n"
  647. "otherLine();"));
  648. EXPECT_EQ("lineWith(); // comment\n"
  649. " // at start\n"
  650. "otherLine();",
  651. format("lineWith(); // comment\n"
  652. " // at start\n"
  653. "otherLine();"));
  654. EXPECT_EQ("lineWith(); // comment\n"
  655. "// at start\n"
  656. "otherLine(); // comment",
  657. format("lineWith(); // comment\n"
  658. "// at start\n"
  659. "otherLine(); // comment"));
  660. EXPECT_EQ("lineWith();\n"
  661. "// at start\n"
  662. "otherLine(); // comment",
  663. format("lineWith();\n"
  664. " // at start\n"
  665. "otherLine(); // comment"));
  666. EXPECT_EQ("// first\n"
  667. "// at start\n"
  668. "otherLine(); // comment",
  669. format("// first\n"
  670. " // at start\n"
  671. "otherLine(); // comment"));
  672. EXPECT_EQ("f();\n"
  673. "// first\n"
  674. "// at start\n"
  675. "otherLine(); // comment",
  676. format("f();\n"
  677. "// first\n"
  678. " // at start\n"
  679. "otherLine(); // comment"));
  680. }
  681. TEST_F(FormatTest, CanFormatCommentsLocally) {
  682. EXPECT_EQ("int a; // comment\n"
  683. "int b; // comment",
  684. format("int a; // comment\n"
  685. "int b; // comment",
  686. 0, 0, getLLVMStyle()));
  687. EXPECT_EQ("int a; // comment\n"
  688. " // line 2\n"
  689. "int b;",
  690. format("int a; // comment\n"
  691. " // line 2\n"
  692. "int b;",
  693. 28, 0, getLLVMStyle()));
  694. EXPECT_EQ("int aaaaaa; // comment\n"
  695. "int b;\n"
  696. "int c; // unrelated comment",
  697. format("int aaaaaa; // comment\n"
  698. "int b;\n"
  699. "int c; // unrelated comment",
  700. 31, 0, getLLVMStyle()));
  701. }
  702. TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
  703. EXPECT_EQ("// comment", format("// comment "));
  704. EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
  705. format("int aaaaaaa, bbbbbbb; // comment ",
  706. getLLVMStyleWithColumns(33)));
  707. }
  708. TEST_F(FormatTest, UnderstandsBlockComments) {
  709. verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
  710. EXPECT_EQ(
  711. "f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
  712. " bbbbbbbbbbbbbbbbbbbbbbbbb);",
  713. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n/* Trailing comment for aa... */\n"
  714. " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  715. EXPECT_EQ(
  716. "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  717. " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
  718. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
  719. "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  720. EXPECT_EQ(
  721. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  722. " aaaaaaaaaaaaaaaaaa,\n"
  723. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  724. "}",
  725. format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  726. " aaaaaaaaaaaaaaaaaa ,\n"
  727. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  728. "}"));
  729. FormatStyle NoBinPacking = getLLVMStyle();
  730. NoBinPacking.BinPackParameters = false;
  731. verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
  732. " /* parameter 2 */ aaaaaa,\n"
  733. " /* parameter 3 */ aaaaaa,\n"
  734. " /* parameter 4 */ aaaaaa);",
  735. NoBinPacking);
  736. }
  737. TEST_F(FormatTest, AlignsBlockComments) {
  738. EXPECT_EQ("/*\n"
  739. " * Really multi-line\n"
  740. " * comment.\n"
  741. " */\n"
  742. "void f() {}",
  743. format(" /*\n"
  744. " * Really multi-line\n"
  745. " * comment.\n"
  746. " */\n"
  747. " void f() {}"));
  748. EXPECT_EQ("class C {\n"
  749. " /*\n"
  750. " * Another multi-line\n"
  751. " * comment.\n"
  752. " */\n"
  753. " void f() {}\n"
  754. "};",
  755. format("class C {\n"
  756. "/*\n"
  757. " * Another multi-line\n"
  758. " * comment.\n"
  759. " */\n"
  760. "void f() {}\n"
  761. "};"));
  762. EXPECT_EQ("/*\n"
  763. " 1. This is a comment with non-trivial formatting.\n"
  764. " 1.1. We have to indent/outdent all lines equally\n"
  765. " 1.1.1. to keep the formatting.\n"
  766. " */",
  767. format(" /*\n"
  768. " 1. This is a comment with non-trivial formatting.\n"
  769. " 1.1. We have to indent/outdent all lines equally\n"
  770. " 1.1.1. to keep the formatting.\n"
  771. " */"));
  772. EXPECT_EQ("/*\n"
  773. "Don't try to outdent if there's not enough indentation.\n"
  774. "*/",
  775. format(" /*\n"
  776. " Don't try to outdent if there's not enough indentation.\n"
  777. " */"));
  778. EXPECT_EQ("int i; /* Comment with empty...\n"
  779. " *\n"
  780. " * line. */",
  781. format("int i; /* Comment with empty...\n"
  782. " *\n"
  783. " * line. */"));
  784. }
  785. TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
  786. EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  787. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
  788. format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  789. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
  790. EXPECT_EQ(
  791. "void ffffffffffff(\n"
  792. " int aaaaaaaa, int bbbbbbbb,\n"
  793. " int cccccccccccc) { /*\n"
  794. " aaaaaaaaaa\n"
  795. " aaaaaaaaaaaaa\n"
  796. " bbbbbbbbbbbbbb\n"
  797. " bbbbbbbbbb\n"
  798. " */\n"
  799. "}",
  800. format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
  801. "{ /*\n"
  802. " aaaaaaaaaa aaaaaaaaaaaaa\n"
  803. " bbbbbbbbbbbbbb bbbbbbbbbb\n"
  804. " */\n"
  805. "}",
  806. getLLVMStyleWithColumns(40)));
  807. }
  808. TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
  809. EXPECT_EQ("void\n"
  810. "ffffffffff(int aaaaa /* test */);",
  811. format("void ffffffffff(int aaaaa /* test */);",
  812. getLLVMStyleWithColumns(35)));
  813. }
  814. TEST_F(FormatTest, SplitsLongCxxComments) {
  815. EXPECT_EQ("// A comment that\n"
  816. "// doesn't fit on\n"
  817. "// one line",
  818. format("// A comment that doesn't fit on one line",
  819. getLLVMStyleWithColumns(20)));
  820. EXPECT_EQ("// a b c d\n"
  821. "// e f g\n"
  822. "// h i j k",
  823. format("// a b c d e f g h i j k",
  824. getLLVMStyleWithColumns(10)));
  825. EXPECT_EQ("// a b c d\n"
  826. "// e f g\n"
  827. "// h i j k",
  828. format("\\\n// a b c d e f g h i j k",
  829. getLLVMStyleWithColumns(10)));
  830. EXPECT_EQ("if (true) // A comment that\n"
  831. " // doesn't fit on\n"
  832. " // one line",
  833. format("if (true) // A comment that doesn't fit on one line ",
  834. getLLVMStyleWithColumns(30)));
  835. EXPECT_EQ("// Don't_touch_leading_whitespace",
  836. format("// Don't_touch_leading_whitespace",
  837. getLLVMStyleWithColumns(20)));
  838. EXPECT_EQ("// Add leading\n"
  839. "// whitespace",
  840. format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
  841. EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
  842. EXPECT_EQ("// Even if it makes the line exceed the column\n"
  843. "// limit",
  844. format("//Even if it makes the line exceed the column limit",
  845. getLLVMStyleWithColumns(51)));
  846. EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
  847. EXPECT_EQ("// A comment before\n"
  848. "// a macro\n"
  849. "// definition\n"
  850. "#define a b",
  851. format("// A comment before a macro definition\n"
  852. "#define a b",
  853. getLLVMStyleWithColumns(20)));
  854. EXPECT_EQ("void ffffff(int aaaaaaaaa, // wwww\n"
  855. " int a, int bbb, // xxxxxxx\n"
  856. " // yyyyyyyyy\n"
  857. " int c, int d, int e) {}",
  858. format("void ffffff(\n"
  859. " int aaaaaaaaa, // wwww\n"
  860. " int a,\n"
  861. " int bbb, // xxxxxxx yyyyyyyyy\n"
  862. " int c, int d, int e) {}",
  863. getLLVMStyleWithColumns(40)));
  864. EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  865. format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  866. getLLVMStyleWithColumns(20)));
  867. }
  868. TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
  869. EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  870. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  871. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  872. format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  873. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  874. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
  875. EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  876. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  877. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  878. format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  879. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  880. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  881. getLLVMStyleWithColumns(50)));
  882. // FIXME: One day we might want to implement adjustment of leading whitespace
  883. // of the consecutive lines in this kind of comment:
  884. EXPECT_EQ("int\n"
  885. "a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  886. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  887. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  888. format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  889. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  890. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  891. getLLVMStyleWithColumns(49)));
  892. }
  893. TEST_F(FormatTest, PriorityOfCommentBreaking) {
  894. EXPECT_EQ("if (xxx ==\n"
  895. " yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  896. " zzz)\n"
  897. " q();",
  898. format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  899. " zzz) q();",
  900. getLLVMStyleWithColumns(40)));
  901. EXPECT_EQ("if (xxxxxxxxxx ==\n"
  902. " yyy && // aaaaaa bbbbbbbb cccc\n"
  903. " zzz)\n"
  904. " q();",
  905. format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
  906. " zzz) q();",
  907. getLLVMStyleWithColumns(40)));
  908. EXPECT_EQ("if (xxxxxxxxxx &&\n"
  909. " yyy || // aaaaaa bbbbbbbb cccc\n"
  910. " zzz)\n"
  911. " q();",
  912. format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
  913. " zzz) q();",
  914. getLLVMStyleWithColumns(40)));
  915. EXPECT_EQ("fffffffff(&xxx, // aaaaaaaaaaaa\n"
  916. " // bbbbbbbbbbb\n"
  917. " zzz);",
  918. format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  919. " zzz);",
  920. getLLVMStyleWithColumns(40)));
  921. }
  922. TEST_F(FormatTest, MultiLineCommentsInDefines) {
  923. EXPECT_EQ("#define A(x) /* \\\n"
  924. " a comment \\\n"
  925. " inside */ \\\n"
  926. " f();",
  927. format("#define A(x) /* \\\n"
  928. " a comment \\\n"
  929. " inside */ \\\n"
  930. " f();",
  931. getLLVMStyleWithColumns(17)));
  932. EXPECT_EQ("#define A( \\\n"
  933. " x) /* \\\n"
  934. " a comment \\\n"
  935. " inside */ \\\n"
  936. " f();",
  937. format("#define A( \\\n"
  938. " x) /* \\\n"
  939. " a comment \\\n"
  940. " inside */ \\\n"
  941. " f();",
  942. getLLVMStyleWithColumns(17)));
  943. }
  944. TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
  945. EXPECT_EQ("namespace {}\n// Test\n#define A",
  946. format("namespace {}\n // Test\n#define A"));
  947. EXPECT_EQ("namespace {}\n/* Test */\n#define A",
  948. format("namespace {}\n /* Test */\n#define A"));
  949. EXPECT_EQ("namespace {}\n/* Test */ #define A",
  950. format("namespace {}\n /* Test */ #define A"));
  951. }
  952. TEST_F(FormatTest, SplitsLongLinesInComments) {
  953. EXPECT_EQ("/* This is a long\n"
  954. " * comment that\n"
  955. " * doesn't\n"
  956. " * fit on one line.\n"
  957. " */",
  958. format("/* "
  959. "This is a long "
  960. "comment that "
  961. "doesn't "
  962. "fit on one line. */",
  963. getLLVMStyleWithColumns(20)));
  964. EXPECT_EQ("/* a b c d\n"
  965. " * e f g\n"
  966. " * h i j k\n"
  967. " */",
  968. format("/* a b c d e f g h i j k */",
  969. getLLVMStyleWithColumns(10)));
  970. EXPECT_EQ("/* a b c d\n"
  971. " * e f g\n"
  972. " * h i j k\n"
  973. " */",
  974. format("\\\n/* a b c d e f g h i j k */",
  975. getLLVMStyleWithColumns(10)));
  976. EXPECT_EQ("/*\n"
  977. "This is a long\n"
  978. "comment that doesn't\n"
  979. "fit on one line.\n"
  980. "*/",
  981. format("/*\n"
  982. "This is a long "
  983. "comment that doesn't "
  984. "fit on one line. \n"
  985. "*/", getLLVMStyleWithColumns(20)));
  986. EXPECT_EQ("/*\n"
  987. " * This is a long\n"
  988. " * comment that\n"
  989. " * doesn't fit on\n"
  990. " * one line.\n"
  991. " */",
  992. format("/* \n"
  993. " * This is a long "
  994. " comment that "
  995. " doesn't fit on "
  996. " one line. \n"
  997. " */", getLLVMStyleWithColumns(20)));
  998. EXPECT_EQ("/*\n"
  999. " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
  1000. " * so_it_should_be_broken\n"
  1001. " * wherever_a_space_occurs\n"
  1002. " */",
  1003. format("/*\n"
  1004. " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
  1005. " so_it_should_be_broken "
  1006. " wherever_a_space_occurs \n"
  1007. " */",
  1008. getLLVMStyleWithColumns(20)));
  1009. EXPECT_EQ("/*\n"
  1010. " * This_comment_can_not_be_broken_into_lines\n"
  1011. " */",
  1012. format("/*\n"
  1013. " * This_comment_can_not_be_broken_into_lines\n"
  1014. " */",
  1015. getLLVMStyleWithColumns(20)));
  1016. EXPECT_EQ("{\n"
  1017. " /*\n"
  1018. " This is another\n"
  1019. " long comment that\n"
  1020. " doesn't fit on one\n"
  1021. " line 1234567890\n"
  1022. " */\n"
  1023. "}",
  1024. format("{\n"
  1025. "/*\n"
  1026. "This is another "
  1027. " long comment that "
  1028. " doesn't fit on one"
  1029. " line 1234567890\n"
  1030. "*/\n"
  1031. "}", getLLVMStyleWithColumns(20)));
  1032. EXPECT_EQ("{\n"
  1033. " /*\n"
  1034. " * This i s\n"
  1035. " * another comment\n"
  1036. " * t hat doesn' t\n"
  1037. " * fit on one l i\n"
  1038. " * n e\n"
  1039. " */\n"
  1040. "}",
  1041. format("{\n"
  1042. "/*\n"
  1043. " * This i s"
  1044. " another comment"
  1045. " t hat doesn' t"
  1046. " fit on one l i"
  1047. " n e\n"
  1048. " */\n"
  1049. "}", getLLVMStyleWithColumns(20)));
  1050. EXPECT_EQ("/*\n"
  1051. " * This is a long\n"
  1052. " * comment that\n"
  1053. " * doesn't fit on\n"
  1054. " * one line\n"
  1055. " */",
  1056. format(" /*\n"
  1057. " * This is a long comment that doesn't fit on one line\n"
  1058. " */", getLLVMStyleWithColumns(20)));
  1059. EXPECT_EQ("{\n"
  1060. " if (something) /* This is a\n"
  1061. " long\n"
  1062. " comment */\n"
  1063. " ;\n"
  1064. "}",
  1065. format("{\n"
  1066. " if (something) /* This is a long comment */\n"
  1067. " ;\n"
  1068. "}",
  1069. getLLVMStyleWithColumns(30)));
  1070. EXPECT_EQ("/* A comment before\n"
  1071. " * a macro\n"
  1072. " * definition */\n"
  1073. "#define a b",
  1074. format("/* A comment before a macro definition */\n"
  1075. "#define a b",
  1076. getLLVMStyleWithColumns(20)));
  1077. EXPECT_EQ("/* some comment\n"
  1078. " * a comment\n"
  1079. "* that we break\n"
  1080. " * another comment\n"
  1081. "* we have to break\n"
  1082. "* a left comment\n"
  1083. " */",
  1084. format(" /* some comment\n"
  1085. " * a comment that we break\n"
  1086. " * another comment we have to break\n"
  1087. "* a left comment\n"
  1088. " */",
  1089. getLLVMStyleWithColumns(20)));
  1090. EXPECT_EQ("/*\n"
  1091. "\n"
  1092. "\n"
  1093. " */\n",
  1094. format(" /* \n"
  1095. " \n"
  1096. " \n"
  1097. " */\n"));
  1098. }
  1099. TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
  1100. EXPECT_EQ("#define X \\\n"
  1101. " /* \\\n"
  1102. " Test \\\n"
  1103. " Macro comment \\\n"
  1104. " with a long \\\n"
  1105. " line \\\n"
  1106. " */ \\\n"
  1107. " A + B",
  1108. format("#define X \\\n"
  1109. " /*\n"
  1110. " Test\n"
  1111. " Macro comment with a long line\n"
  1112. " */ \\\n"
  1113. " A + B",
  1114. getLLVMStyleWithColumns(20)));
  1115. EXPECT_EQ("#define X \\\n"
  1116. " /* Macro comment \\\n"
  1117. " with a long \\\n"
  1118. " line */ \\\n"
  1119. " A + B",
  1120. format("#define X \\\n"
  1121. " /* Macro comment with a long\n"
  1122. " line */ \\\n"
  1123. " A + B",
  1124. getLLVMStyleWithColumns(20)));
  1125. EXPECT_EQ("#define X \\\n"
  1126. " /* Macro comment \\\n"
  1127. " * with a long \\\n"
  1128. " * line */ \\\n"
  1129. " A + B",
  1130. format("#define X \\\n"
  1131. " /* Macro comment with a long line */ \\\n"
  1132. " A + B",
  1133. getLLVMStyleWithColumns(20)));
  1134. }
  1135. TEST_F(FormatTest, CommentsInStaticInitializers) {
  1136. EXPECT_EQ(
  1137. "static SomeType type = { aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
  1138. " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
  1139. " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
  1140. " aaaaaaaaaaaaaaaaaaaa, // comment\n"
  1141. " aaaaaaaaaaaaaaaaaaaa };",
  1142. format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
  1143. " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
  1144. " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
  1145. " aaaaaaaaaaaaaaaaaaaa , // comment\n"
  1146. " aaaaaaaaaaaaaaaaaaaa };"));
  1147. verifyFormat("static SomeType type = { aaaaaaaaaaa, // comment for aa...\n"
  1148. " bbbbbbbbbbb, ccccccccccc };");
  1149. verifyFormat("static SomeType type = { aaaaaaaaaaa,\n"
  1150. " // comment for bb....\n"
  1151. " bbbbbbbbbbb, ccccccccccc };");
  1152. verifyGoogleFormat(
  1153. "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1154. " bbbbbbbbbbb, ccccccccccc};");
  1155. verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1156. " // comment for bb....\n"
  1157. " bbbbbbbbbbb, ccccccccccc};");
  1158. verifyFormat("S s = { { a, b, c }, // Group #1\n"
  1159. " { d, e, f }, // Group #2\n"
  1160. " { g, h, i } }; // Group #3");
  1161. verifyFormat("S s = { { // Group #1\n"
  1162. " a, b, c },\n"
  1163. " { // Group #2\n"
  1164. " d, e, f },\n"
  1165. " { // Group #3\n"
  1166. " g, h, i } };");
  1167. EXPECT_EQ("S s = {\n"
  1168. " // Some comment\n"
  1169. " a,\n"
  1170. "\n"
  1171. " // Comment after empty line\n"
  1172. " b\n"
  1173. "}",
  1174. format("S s = {\n"
  1175. " // Some comment\n"
  1176. " a,\n"
  1177. " \n"
  1178. " // Comment after empty line\n"
  1179. " b\n"
  1180. "}"));
  1181. EXPECT_EQ("S s = {\n"
  1182. " /* Some comment */\n"
  1183. " a,\n"
  1184. "\n"
  1185. " /* Comment after empty line */\n"
  1186. " b\n"
  1187. "}",
  1188. format("S s = {\n"
  1189. " /* Some comment */\n"
  1190. " a,\n"
  1191. " \n"
  1192. " /* Comment after empty line */\n"
  1193. " b\n"
  1194. "}"));
  1195. verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
  1196. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1197. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1198. " 0x00, 0x00, 0x00, 0x00 // comment\n"
  1199. "};");
  1200. }
  1201. TEST_F(FormatTest, IgnoresIf0Contents) {
  1202. EXPECT_EQ("#if 0\n"
  1203. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1204. "#endif\n"
  1205. "void f() {}",
  1206. format("#if 0\n"
  1207. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1208. "#endif\n"
  1209. "void f( ) { }"));
  1210. EXPECT_EQ("#if false\n"
  1211. "void f( ) { }\n"
  1212. "#endif\n"
  1213. "void g() {}\n",
  1214. format("#if false\n"
  1215. "void f( ) { }\n"
  1216. "#endif\n"
  1217. "void g( ) { }\n"));
  1218. EXPECT_EQ("enum E {\n"
  1219. " One,\n"
  1220. " Two,\n"
  1221. "#if 0\n"
  1222. "Three,\n"
  1223. " Four,\n"
  1224. "#endif\n"
  1225. " Five\n"
  1226. "};",
  1227. format("enum E {\n"
  1228. " One,Two,\n"
  1229. "#if 0\n"
  1230. "Three,\n"
  1231. " Four,\n"
  1232. "#endif\n"
  1233. " Five};"));
  1234. EXPECT_EQ("enum F {\n"
  1235. " One,\n"
  1236. "#if 1\n"
  1237. " Two,\n"
  1238. "#if 0\n"
  1239. "Three,\n"
  1240. " Four,\n"
  1241. "#endif\n"
  1242. " Five\n"
  1243. "#endif\n"
  1244. "};",
  1245. format("enum F {\n"
  1246. "One,\n"
  1247. "#if 1\n"
  1248. "Two,\n"
  1249. "#if 0\n"
  1250. "Three,\n"
  1251. " Four,\n"
  1252. "#endif\n"
  1253. "Five\n"
  1254. "#endif\n"
  1255. "};"));
  1256. EXPECT_EQ("enum G {\n"
  1257. " One,\n"
  1258. "#if 0\n"
  1259. "Two,\n"
  1260. "#else\n"
  1261. " Three,\n"
  1262. "#endif\n"
  1263. " Four\n"
  1264. "};",
  1265. format("enum G {\n"
  1266. "One,\n"
  1267. "#if 0\n"
  1268. "Two,\n"
  1269. "#else\n"
  1270. "Three,\n"
  1271. "#endif\n"
  1272. "Four\n"
  1273. "};"));
  1274. EXPECT_EQ("enum H {\n"
  1275. " One,\n"
  1276. "#if 0\n"
  1277. "#ifdef Q\n"
  1278. "Two,\n"
  1279. "#else\n"
  1280. "Three,\n"
  1281. "#endif\n"
  1282. "#endif\n"
  1283. " Four\n"
  1284. "};",
  1285. format("enum H {\n"
  1286. "One,\n"
  1287. "#if 0\n"
  1288. "#ifdef Q\n"
  1289. "Two,\n"
  1290. "#else\n"
  1291. "Three,\n"
  1292. "#endif\n"
  1293. "#endif\n"
  1294. "Four\n"
  1295. "};"));
  1296. EXPECT_EQ("enum I {\n"
  1297. " One,\n"
  1298. "#if /* test */ 0 || 1\n"
  1299. "Two,\n"
  1300. "Three,\n"
  1301. "#endif\n"
  1302. " Four\n"
  1303. "};",
  1304. format("enum I {\n"
  1305. "One,\n"
  1306. "#if /* test */ 0 || 1\n"
  1307. "Two,\n"
  1308. "Three,\n"
  1309. "#endif\n"
  1310. "Four\n"
  1311. "};"));
  1312. EXPECT_EQ("enum J {\n"
  1313. " One,\n"
  1314. "#if 0\n"
  1315. "#if 0\n"
  1316. "Two,\n"
  1317. "#else\n"
  1318. "Three,\n"
  1319. "#endif\n"
  1320. "Four,\n"
  1321. "#endif\n"
  1322. " Five\n"
  1323. "};",
  1324. format("enum J {\n"
  1325. "One,\n"
  1326. "#if 0\n"
  1327. "#if 0\n"
  1328. "Two,\n"
  1329. "#else\n"
  1330. "Three,\n"
  1331. "#endif\n"
  1332. "Four,\n"
  1333. "#endif\n"
  1334. "Five\n"
  1335. "};"));
  1336. }
  1337. //===----------------------------------------------------------------------===//
  1338. // Tests for classes, namespaces, etc.
  1339. //===----------------------------------------------------------------------===//
  1340. TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
  1341. verifyFormat("class A {};");
  1342. }
  1343. TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
  1344. verifyFormat("class A {\n"
  1345. "public:\n"
  1346. "public: // comment\n"
  1347. "protected:\n"
  1348. "private:\n"
  1349. " void f() {}\n"
  1350. "};");
  1351. verifyGoogleFormat("class A {\n"
  1352. " public:\n"
  1353. " protected:\n"
  1354. " private:\n"
  1355. " void f() {}\n"
  1356. "};");
  1357. }
  1358. TEST_F(FormatTest, SeparatesLogicalBlocks) {
  1359. EXPECT_EQ("class A {\n"
  1360. "public:\n"
  1361. " void f();\n"
  1362. "\n"
  1363. "private:\n"
  1364. " void g() {}\n"
  1365. " // test\n"
  1366. "protected:\n"
  1367. " int h;\n"
  1368. "};",
  1369. format("class A {\n"
  1370. "public:\n"
  1371. "void f();\n"
  1372. "private:\n"
  1373. "void g() {}\n"
  1374. "// test\n"
  1375. "protected:\n"
  1376. "int h;\n"
  1377. "};"));
  1378. }
  1379. TEST_F(FormatTest, FormatsClasses) {
  1380. verifyFormat("class A : public B {};");
  1381. verifyFormat("class A : public ::B {};");
  1382. verifyFormat(
  1383. "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1384. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1385. verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
  1386. " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1387. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1388. verifyFormat(
  1389. "class A : public B, public C, public D, public E, public F {};");
  1390. verifyFormat("class AAAAAAAAAAAA : public B,\n"
  1391. " public C,\n"
  1392. " public D,\n"
  1393. " public E,\n"
  1394. " public F,\n"
  1395. " public G {};");
  1396. verifyFormat("class\n"
  1397. " ReallyReallyLongClassName {\n};",
  1398. getLLVMStyleWithColumns(32));
  1399. }
  1400. TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
  1401. verifyFormat("class A {\n} a, b;");
  1402. verifyFormat("struct A {\n} a, b;");
  1403. verifyFormat("union A {\n} a;");
  1404. }
  1405. TEST_F(FormatTest, FormatsEnum) {
  1406. verifyFormat("enum {\n"
  1407. " Zero,\n"
  1408. " One = 1,\n"
  1409. " Two = One + 1,\n"
  1410. " Three = (One + Two),\n"
  1411. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1412. " Five = (One, Two, Three, Four, 5)\n"
  1413. "};");
  1414. verifyFormat("enum Enum {};");
  1415. verifyFormat("enum {};");
  1416. verifyFormat("enum X E {\n} d;");
  1417. verifyFormat("enum __attribute__((...)) E {\n} d;");
  1418. verifyFormat("enum __declspec__((...)) E {\n} d;");
  1419. verifyFormat("enum X f() {\n a();\n return 42;\n}");
  1420. }
  1421. TEST_F(FormatTest, FormatsEnumStruct) {
  1422. verifyFormat("enum struct {\n"
  1423. " Zero,\n"
  1424. " One = 1,\n"
  1425. " Two = One + 1,\n"
  1426. " Three = (One + Two),\n"
  1427. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1428. " Five = (One, Two, Three, Four, 5)\n"
  1429. "};");
  1430. verifyFormat("enum struct Enum {};");
  1431. verifyFormat("enum struct {};");
  1432. verifyFormat("enum struct X E {\n} d;");
  1433. verifyFormat("enum struct __attribute__((...)) E {\n} d;");
  1434. verifyFormat("enum struct __declspec__((...)) E {\n} d;");
  1435. verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
  1436. }
  1437. TEST_F(FormatTest, FormatsEnumClass) {
  1438. verifyFormat("enum class {\n"
  1439. " Zero,\n"
  1440. " One = 1,\n"
  1441. " Two = One + 1,\n"
  1442. " Three = (One + Two),\n"
  1443. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1444. " Five = (One, Two, Three, Four, 5)\n"
  1445. "};");
  1446. verifyFormat("enum class Enum {};");
  1447. verifyFormat("enum class {};");
  1448. verifyFormat("enum class X E {\n} d;");
  1449. verifyFormat("enum class __attribute__((...)) E {\n} d;");
  1450. verifyFormat("enum class __declspec__((...)) E {\n} d;");
  1451. verifyFormat("enum class X f() {\n a();\n return 42;\n}");
  1452. }
  1453. TEST_F(FormatTest, FormatsBitfields) {
  1454. verifyFormat("struct Bitfields {\n"
  1455. " unsigned sClass : 8;\n"
  1456. " unsigned ValueKind : 2;\n"
  1457. "};");
  1458. }
  1459. TEST_F(FormatTest, FormatsNamespaces) {
  1460. verifyFormat("namespace some_namespace {\n"
  1461. "class A {};\n"
  1462. "void f() { f(); }\n"
  1463. "}");
  1464. verifyFormat("namespace {\n"
  1465. "class A {};\n"
  1466. "void f() { f(); }\n"
  1467. "}");
  1468. verifyFormat("inline namespace X {\n"
  1469. "class A {};\n"
  1470. "void f() { f(); }\n"
  1471. "}");
  1472. verifyFormat("using namespace some_namespace;\n"
  1473. "class A {};\n"
  1474. "void f() { f(); }");
  1475. // This code is more common than we thought; if we
  1476. // layout this correctly the semicolon will go into
  1477. // its own line, which is undesireable.
  1478. verifyFormat("namespace {};");
  1479. verifyFormat("namespace {\n"
  1480. "class A {};\n"
  1481. "};");
  1482. verifyFormat("namespace {\n"
  1483. "int SomeVariable = 0; // comment\n"
  1484. "} // namespace");
  1485. EXPECT_EQ("#ifndef HEADER_GUARD\n"
  1486. "#define HEADER_GUARD\n"
  1487. "namespace my_namespace {\n"
  1488. "int i;\n"
  1489. "} // my_namespace\n"
  1490. "#endif // HEADER_GUARD",
  1491. format("#ifndef HEADER_GUARD\n"
  1492. " #define HEADER_GUARD\n"
  1493. " namespace my_namespace {\n"
  1494. "int i;\n"
  1495. "} // my_namespace\n"
  1496. "#endif // HEADER_GUARD"));
  1497. FormatStyle Style = getLLVMStyle();
  1498. Style.NamespaceIndentation = FormatStyle::NI_All;
  1499. EXPECT_EQ("namespace out {\n"
  1500. " int i;\n"
  1501. " namespace in {\n"
  1502. " int i;\n"
  1503. " } // namespace\n"
  1504. "} // namespace",
  1505. format("namespace out {\n"
  1506. "int i;\n"
  1507. "namespace in {\n"
  1508. "int i;\n"
  1509. "} // namespace\n"
  1510. "} // namespace",
  1511. Style));
  1512. Style.NamespaceIndentation = FormatStyle::NI_Inner;
  1513. EXPECT_EQ("namespace out {\n"
  1514. "int i;\n"
  1515. "namespace in {\n"
  1516. " int i;\n"
  1517. "} // namespace\n"
  1518. "} // namespace",
  1519. format("namespace out {\n"
  1520. "int i;\n"
  1521. "namespace in {\n"
  1522. "int i;\n"
  1523. "} // namespace\n"
  1524. "} // namespace",
  1525. Style));
  1526. }
  1527. TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
  1528. TEST_F(FormatTest, FormatsInlineASM) {
  1529. verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
  1530. verifyFormat(
  1531. "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
  1532. " \"cpuid\\n\\t\"\n"
  1533. " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
  1534. " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
  1535. " : \"a\"(value));");
  1536. }
  1537. TEST_F(FormatTest, FormatTryCatch) {
  1538. // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll
  1539. // also not create single-line-blocks.
  1540. verifyFormat("try {\n"
  1541. " throw a * b;\n"
  1542. "}\n"
  1543. "catch (int a) {\n"
  1544. " // Do nothing.\n"
  1545. "}\n"
  1546. "catch (...) {\n"
  1547. " exit(42);\n"
  1548. "}");
  1549. // Function-level try statements.
  1550. verifyFormat("int f() try { return 4; }\n"
  1551. "catch (...) {\n"
  1552. " return 5;\n"
  1553. "}");
  1554. verifyFormat("class A {\n"
  1555. " int a;\n"
  1556. " A() try : a(0) {}\n"
  1557. " catch (...) {\n"
  1558. " throw;\n"
  1559. " }\n"
  1560. "};\n");
  1561. }
  1562. TEST_F(FormatTest, FormatObjCTryCatch) {
  1563. verifyFormat("@try {\n"
  1564. " f();\n"
  1565. "}\n"
  1566. "@catch (NSException e) {\n"
  1567. " @throw;\n"
  1568. "}\n"
  1569. "@finally {\n"
  1570. " exit(42);\n"
  1571. "}");
  1572. }
  1573. TEST_F(FormatTest, StaticInitializers) {
  1574. verifyFormat("static SomeClass SC = { 1, 'a' };");
  1575. verifyFormat(
  1576. "static SomeClass WithALoooooooooooooooooooongName = {\n"
  1577. " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
  1578. "};");
  1579. // Here, everything other than the "}" would fit on a line.
  1580. verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
  1581. " 100000000000000000000000\n"
  1582. "};");
  1583. EXPECT_EQ("S s = { a, b };", format("S s = {\n"
  1584. " a,\n"
  1585. "\n"
  1586. " b\n"
  1587. "};"));
  1588. // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
  1589. // line. However, the formatting looks a bit off and this probably doesn't
  1590. // happen often in practice.
  1591. verifyFormat("static int Variable[1] = {\n"
  1592. " { 1000000000000000000000000000000000000 }\n"
  1593. "};",
  1594. getLLVMStyleWithColumns(40));
  1595. }
  1596. TEST_F(FormatTest, DesignatedInitializers) {
  1597. verifyFormat("const struct A a = { .a = 1, .b = 2 };");
  1598. verifyFormat("const struct A a = { .aaaaaaaaaa = 1,\n"
  1599. " .bbbbbbbbbb = 2,\n"
  1600. " .cccccccccc = 3,\n"
  1601. " .dddddddddd = 4,\n"
  1602. " .eeeeeeeeee = 5 };");
  1603. verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
  1604. " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
  1605. " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
  1606. " .ccccccccccccccccccccccccccc = 3,\n"
  1607. " .ddddddddddddddddddddddddddd = 4,\n"
  1608. " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5\n"
  1609. "};");
  1610. verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
  1611. }
  1612. TEST_F(FormatTest, NestedStaticInitializers) {
  1613. verifyFormat("static A x = { { {} } };\n");
  1614. verifyFormat("static A x = { { { init1, init2, init3, init4 },\n"
  1615. " { init1, init2, init3, init4 } } };");
  1616. verifyFormat("somes Status::global_reps[3] = {\n"
  1617. " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n"
  1618. " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n"
  1619. " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n"
  1620. "};");
  1621. verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
  1622. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  1623. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  1624. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
  1625. verifyFormat(
  1626. "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n"
  1627. " { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop"
  1628. " } };");
  1629. verifyFormat(
  1630. "SomeArrayOfSomeType a = { { { 1, 2, 3 }, { 1, 2, 3 },\n"
  1631. " { 111111111111111111111111111111,\n"
  1632. " 222222222222222222222222222222,\n"
  1633. " 333333333333333333333333333333 },\n"
  1634. " { 1, 2, 3 }, { 1, 2, 3 } } };");
  1635. verifyFormat(
  1636. "SomeArrayOfSomeType a = { { { 1, 2, 3 } }, { { 1, 2, 3 } },\n"
  1637. " { { 111111111111111111111111111111,\n"
  1638. " 222222222222222222222222222222,\n"
  1639. " 333333333333333333333333333333 } },\n"
  1640. " { { 1, 2, 3 } }, { { 1, 2, 3 } } };");
  1641. verifyGoogleFormat(
  1642. "SomeArrayOfSomeType a = {\n"
  1643. " {{1, 2, 3}}, {{1, 2, 3}},\n"
  1644. " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
  1645. " 333333333333333333333333333333}},\n"
  1646. " {{1, 2, 3}}, {{1, 2, 3}}};");
  1647. // FIXME: We might at some point want to handle this similar to parameter
  1648. // lists, where we have an option to put each on a single line.
  1649. verifyFormat(
  1650. "struct {\n"
  1651. " unsigned bit;\n"
  1652. " const char *const name;\n"
  1653. "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n"
  1654. " { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };");
  1655. }
  1656. TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
  1657. verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  1658. " \\\n"
  1659. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
  1660. }
  1661. TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
  1662. verifyFormat(
  1663. "virtual void write(ELFWriter *writerrr,\n"
  1664. " OwningPtr<FileOutputBuffer> &buffer) = 0;");
  1665. }
  1666. TEST_F(FormatTest, LayoutUnknownPPDirective) {
  1667. EXPECT_EQ("#123 \"A string literal\"",
  1668. format(" # 123 \"A string literal\""));
  1669. EXPECT_EQ("#;", format("#;"));
  1670. verifyFormat("#\n;\n;\n;");
  1671. }
  1672. TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
  1673. EXPECT_EQ("#line 42 \"test\"\n",
  1674. format("# \\\n line \\\n 42 \\\n \"test\"\n"));
  1675. EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n",
  1676. getLLVMStyleWithColumns(12)));
  1677. }
  1678. TEST_F(FormatTest, EndOfFileEndsPPDirective) {
  1679. EXPECT_EQ("#line 42 \"test\"",
  1680. format("# \\\n line \\\n 42 \\\n \"test\""));
  1681. EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B"));
  1682. }
  1683. TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
  1684. verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
  1685. verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
  1686. verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
  1687. // FIXME: We never break before the macro name.
  1688. verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
  1689. verifyFormat("#define A A\n#define A A");
  1690. verifyFormat("#define A(X) A\n#define A A");
  1691. verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
  1692. verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
  1693. }
  1694. TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
  1695. EXPECT_EQ("// somecomment\n"
  1696. "#include \"a.h\"\n"
  1697. "#define A( \\\n"
  1698. " A, B)\n"
  1699. "#include \"b.h\"\n"
  1700. "// somecomment\n",
  1701. format(" // somecomment\n"
  1702. " #include \"a.h\"\n"
  1703. "#define A(A,\\\n"
  1704. " B)\n"
  1705. " #include \"b.h\"\n"
  1706. " // somecomment\n",
  1707. getLLVMStyleWithColumns(13)));
  1708. }
  1709. TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
  1710. TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
  1711. EXPECT_EQ("#define A \\\n"
  1712. " c; \\\n"
  1713. " e;\n"
  1714. "f;",
  1715. format("#define A c; e;\n"
  1716. "f;",
  1717. getLLVMStyleWithColumns(14)));
  1718. }
  1719. TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
  1720. TEST_F(FormatTest, AlwaysFormatsEntireMacroDefinitions) {
  1721. EXPECT_EQ("int i;\n"
  1722. "#define A \\\n"
  1723. " int i; \\\n"
  1724. " int j\n"
  1725. "int k;",
  1726. format("int i;\n"
  1727. "#define A \\\n"
  1728. " int i ; \\\n"
  1729. " int j\n"
  1730. "int k;",
  1731. 8, 0, getGoogleStyle())); // 8: position of "#define".
  1732. EXPECT_EQ("int i;\n"
  1733. "#define A \\\n"
  1734. " int i; \\\n"
  1735. " int j\n"
  1736. "int k;",
  1737. format("int i;\n"
  1738. "#define A \\\n"
  1739. " int i ; \\\n"
  1740. " int j\n"
  1741. "int k;",
  1742. 45, 0, getGoogleStyle())); // 45: position of "j".
  1743. }
  1744. TEST_F(FormatTest, MacroDefinitionInsideStatement) {
  1745. EXPECT_EQ("int x,\n"
  1746. "#define A\n"
  1747. " y;",
  1748. format("int x,\n#define A\ny;"));
  1749. }
  1750. TEST_F(FormatTest, HashInMacroDefinition) {
  1751. verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
  1752. verifyFormat("#define A \\\n"
  1753. " { \\\n"
  1754. " f(#c); \\\n"
  1755. " }",
  1756. getLLVMStyleWithColumns(11));
  1757. verifyFormat("#define A(X) \\\n"
  1758. " void function##X()",
  1759. getLLVMStyleWithColumns(22));
  1760. verifyFormat("#define A(a, b, c) \\\n"
  1761. " void a##b##c()",
  1762. getLLVMStyleWithColumns(22));
  1763. verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
  1764. }
  1765. TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
  1766. verifyFormat("#define A (1)");
  1767. }
  1768. TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
  1769. EXPECT_EQ("#define A b;", format("#define A \\\n"
  1770. " \\\n"
  1771. " b;",
  1772. getLLVMStyleWithColumns(25)));
  1773. EXPECT_EQ("#define A \\\n"
  1774. " \\\n"
  1775. " a; \\\n"
  1776. " b;",
  1777. format("#define A \\\n"
  1778. " \\\n"
  1779. " a; \\\n"
  1780. " b;",
  1781. getLLVMStyleWithColumns(11)));
  1782. EXPECT_EQ("#define A \\\n"
  1783. " a; \\\n"
  1784. " \\\n"
  1785. " b;",
  1786. format("#define A \\\n"
  1787. " a; \\\n"
  1788. " \\\n"
  1789. " b;",
  1790. getLLVMStyleWithColumns(11)));
  1791. }
  1792. TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
  1793. verifyFormat("#define A :");
  1794. verifyFormat("#define SOMECASES \\\n"
  1795. " case 1: \\\n"
  1796. " case 2\n",
  1797. getLLVMStyleWithColumns(20));
  1798. verifyFormat("#define A template <typename T>");
  1799. verifyFormat("#define STR(x) #x\n"
  1800. "f(STR(this_is_a_string_literal{));");
  1801. verifyFormat("#pragma omp threadprivate( \\\n"
  1802. " y)), // expected-warning",
  1803. getLLVMStyleWithColumns(28));
  1804. }
  1805. TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
  1806. verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
  1807. EXPECT_EQ("class A : public QObject {\n"
  1808. " Q_OBJECT\n"
  1809. "\n"
  1810. " A() {}\n"
  1811. "};",
  1812. format("class A : public QObject {\n"
  1813. " Q_OBJECT\n"
  1814. "\n"
  1815. " A() {\n}\n"
  1816. "} ;"));
  1817. EXPECT_EQ("SOME_MACRO\n"
  1818. "namespace {\n"
  1819. "void f();\n"
  1820. "}",
  1821. format("SOME_MACRO\n"
  1822. " namespace {\n"
  1823. "void f( );\n"
  1824. "}"));
  1825. // Only if the identifier contains at least 5 characters.
  1826. EXPECT_EQ("HTTP f();",
  1827. format("HTTP\nf();"));
  1828. EXPECT_EQ("MACRO\nf();",
  1829. format("MACRO\nf();"));
  1830. // Only if everything is upper case.
  1831. EXPECT_EQ("class A : public QObject {\n"
  1832. " Q_Object A() {}\n"
  1833. "};",
  1834. format("class A : public QObject {\n"
  1835. " Q_Object\n"
  1836. "\n"
  1837. " A() {\n}\n"
  1838. "} ;"));
  1839. }
  1840. TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
  1841. EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  1842. "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  1843. "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  1844. "class X {};\n"
  1845. "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  1846. "int *createScopDetectionPass() { return 0; }",
  1847. format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  1848. " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  1849. " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  1850. " class X {};\n"
  1851. " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  1852. " int *createScopDetectionPass() { return 0; }"));
  1853. // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
  1854. // braces, so that inner block is indented one level more.
  1855. EXPECT_EQ("int q() {\n"
  1856. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  1857. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  1858. " IPC_END_MESSAGE_MAP()\n"
  1859. "}",
  1860. format("int q() {\n"
  1861. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  1862. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  1863. " IPC_END_MESSAGE_MAP()\n"
  1864. "}"));
  1865. EXPECT_EQ("int q() {\n"
  1866. " f(x);\n"
  1867. " f(x) {}\n"
  1868. " f(x)->g();\n"
  1869. " f(x)->*g();\n"
  1870. " f(x).g();\n"
  1871. " f(x) = x;\n"
  1872. " f(x) += x;\n"
  1873. " f(x) -= x;\n"
  1874. " f(x) *= x;\n"
  1875. " f(x) /= x;\n"
  1876. " f(x) %= x;\n"
  1877. " f(x) &= x;\n"
  1878. " f(x) |= x;\n"
  1879. " f(x) ^= x;\n"
  1880. " f(x) >>= x;\n"
  1881. " f(x) <<= x;\n"
  1882. " f(x)[y].z();\n"
  1883. " LOG(INFO) << x;\n"
  1884. " ifstream(x) >> x;\n"
  1885. "}\n",
  1886. format("int q() {\n"
  1887. " f(x)\n;\n"
  1888. " f(x)\n {}\n"
  1889. " f(x)\n->g();\n"
  1890. " f(x)\n->*g();\n"
  1891. " f(x)\n.g();\n"
  1892. " f(x)\n = x;\n"
  1893. " f(x)\n += x;\n"
  1894. " f(x)\n -= x;\n"
  1895. " f(x)\n *= x;\n"
  1896. " f(x)\n /= x;\n"
  1897. " f(x)\n %= x;\n"
  1898. " f(x)\n &= x;\n"
  1899. " f(x)\n |= x;\n"
  1900. " f(x)\n ^= x;\n"
  1901. " f(x)\n >>= x;\n"
  1902. " f(x)\n <<= x;\n"
  1903. " f(x)\n[y].z();\n"
  1904. " LOG(INFO)\n << x;\n"
  1905. " ifstream(x)\n >> x;\n"
  1906. "}\n"));
  1907. EXPECT_EQ("int q() {\n"
  1908. " f(x)\n"
  1909. " if (1) {\n"
  1910. " }\n"
  1911. " f(x)\n"
  1912. " while (1) {\n"
  1913. " }\n"
  1914. " f(x)\n"
  1915. " g(x);\n"
  1916. " f(x)\n"
  1917. " try {\n"
  1918. " q();\n"
  1919. " }\n"
  1920. " catch (...) {\n"
  1921. " }\n"
  1922. "}\n",
  1923. format("int q() {\n"
  1924. "f(x)\n"
  1925. "if (1) {}\n"
  1926. "f(x)\n"
  1927. "while (1) {}\n"
  1928. "f(x)\n"
  1929. "g(x);\n"
  1930. "f(x)\n"
  1931. "try { q(); } catch (...) {}\n"
  1932. "}\n"));
  1933. EXPECT_EQ("class A {\n"
  1934. " A() : t(0) {}\n"
  1935. " A(X x)\n" // FIXME: function-level try blocks are broken.
  1936. " try : t(0) {\n"
  1937. " }\n"
  1938. " catch (...) {\n"
  1939. " }\n"
  1940. "};",
  1941. format("class A {\n"
  1942. " A()\n : t(0) {}\n"
  1943. " A(X x)\n"
  1944. " try : t(0) {} catch (...) {}\n"
  1945. "};"));
  1946. }
  1947. TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
  1948. verifyFormat("#define A \\\n"
  1949. " f({ \\\n"
  1950. " g(); \\\n"
  1951. " });", getLLVMStyleWithColumns(11));
  1952. }
  1953. TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
  1954. EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
  1955. }
  1956. TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
  1957. verifyFormat("{\n { a #c; }\n}");
  1958. }
  1959. TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
  1960. EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
  1961. format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
  1962. EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
  1963. format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
  1964. }
  1965. TEST_F(FormatTest, EscapedNewlineAtStartOfToken) {
  1966. EXPECT_EQ(
  1967. "#define A \\\n int i; \\\n int j;",
  1968. format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
  1969. EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
  1970. }
  1971. TEST_F(FormatTest, NoEscapedNewlineHandlingInBlockComments) {
  1972. EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/"));
  1973. }
  1974. TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
  1975. verifyFormat("#define A \\\n"
  1976. " int v( \\\n"
  1977. " a); \\\n"
  1978. " int i;",
  1979. getLLVMStyleWithColumns(11));
  1980. }
  1981. TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
  1982. EXPECT_EQ(
  1983. "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  1984. " \\\n"
  1985. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  1986. "\n"
  1987. "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  1988. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
  1989. format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
  1990. "\\\n"
  1991. "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  1992. " \n"
  1993. " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  1994. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
  1995. }
  1996. TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
  1997. EXPECT_EQ("int\n"
  1998. "#define A\n"
  1999. " a;",
  2000. format("int\n#define A\na;", getGoogleStyle()));
  2001. verifyFormat("functionCallTo(\n"
  2002. " someOtherFunction(\n"
  2003. " withSomeParameters, whichInSequence,\n"
  2004. " areLongerThanALine(andAnotherCall,\n"
  2005. "#define A B\n"
  2006. " withMoreParamters,\n"
  2007. " whichStronglyInfluenceTheLayout),\n"
  2008. " andMoreParameters),\n"
  2009. " trailing);",
  2010. getLLVMStyleWithColumns(69));
  2011. }
  2012. TEST_F(FormatTest, LayoutBlockInsideParens) {
  2013. EXPECT_EQ("functionCall({\n"
  2014. " int i;\n"
  2015. "});",
  2016. format(" functionCall ( {int i;} );"));
  2017. // FIXME: This is bad, find a better and more generic solution.
  2018. EXPECT_EQ("functionCall({\n"
  2019. " int i;\n"
  2020. "},\n"
  2021. " aaaa, bbbb, cccc);",
  2022. format(" functionCall ( {int i;}, aaaa, bbbb, cccc);"));
  2023. verifyFormat(
  2024. "Aaa({\n"
  2025. " int i;\n"
  2026. "},\n"
  2027. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  2028. " ccccccccccccccccc));");
  2029. }
  2030. TEST_F(FormatTest, LayoutBlockInsideStatement) {
  2031. EXPECT_EQ("SOME_MACRO { int i; }\n"
  2032. "int i;",
  2033. format(" SOME_MACRO {int i;} int i;"));
  2034. }
  2035. TEST_F(FormatTest, LayoutNestedBlocks) {
  2036. verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
  2037. " struct s {\n"
  2038. " int i;\n"
  2039. " };\n"
  2040. " s kBitsToOs[] = { { 10 } };\n"
  2041. " for (int i = 0; i < 10; ++i)\n"
  2042. " return;\n"
  2043. "}");
  2044. }
  2045. TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
  2046. EXPECT_EQ("{}", format("{}"));
  2047. verifyFormat("enum E {};");
  2048. verifyFormat("enum E {}");
  2049. }
  2050. //===----------------------------------------------------------------------===//
  2051. // Line break tests.
  2052. //===----------------------------------------------------------------------===//
  2053. TEST_F(FormatTest, PreventConfusingIndents) {
  2054. verifyFormat(
  2055. "SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
  2056. " parameter, parameter, parameter)),\n"
  2057. " SecondLongCall(parameter));");
  2058. verifyFormat(
  2059. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2060. " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2061. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2062. " aaaaaaaaaaaaaaaaaaaaaaaa);");
  2063. verifyFormat(
  2064. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2065. " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
  2066. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  2067. " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
  2068. verifyFormat(
  2069. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  2070. " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
  2071. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
  2072. " aaaaaaaaaaaaaaaaaaaaaaaa>;");
  2073. verifyFormat("int a = bbbb && ccc && fffff(\n"
  2074. "#define A Just forcing a new line\n"
  2075. " ddd);");
  2076. }
  2077. TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
  2078. verifyFormat(
  2079. "bool aaaaaaa =\n"
  2080. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
  2081. " bbbbbbbb();");
  2082. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  2083. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
  2084. " ccccccccc == ddddddddddd;");
  2085. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  2086. " aaaaaa) &&\n"
  2087. " bbbbbb && cccccc;");
  2088. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  2089. " aaaaaa) >>\n"
  2090. " bbbbbb;");
  2091. verifyFormat("Whitespaces.addUntouchableComment(\n"
  2092. " SourceMgr.getSpellingColumnNumber(\n"
  2093. " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
  2094. " 1);");
  2095. verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2096. " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
  2097. " cccccc) {\n}");
  2098. // If the LHS of a comparison is not a binary expression itself, the
  2099. // additional linebreak confuses many people.
  2100. verifyFormat(
  2101. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2102. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
  2103. "}");
  2104. verifyFormat(
  2105. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2106. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  2107. "}");
  2108. // Even explicit parentheses stress the precedence enough to make the
  2109. // additional break unnecessary.
  2110. verifyFormat(
  2111. "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2112. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  2113. "}");
  2114. // This cases is borderline, but with the indentation it is still readable.
  2115. verifyFormat(
  2116. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2117. " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2118. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  2119. "}",
  2120. getLLVMStyleWithColumns(75));
  2121. // If the LHS is a binary expression, we should still use the additional break
  2122. // as otherwise the formatting hides the operator precedence.
  2123. verifyFormat(
  2124. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2125. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2126. " 5) {\n"
  2127. "}");
  2128. FormatStyle OnePerLine = getLLVMStyle();
  2129. OnePerLine.BinPackParameters = false;
  2130. verifyFormat(
  2131. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2132. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2133. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
  2134. OnePerLine);
  2135. }
  2136. TEST_F(FormatTest, ExpressionIndentation) {
  2137. verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2138. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2139. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2140. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2141. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
  2142. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
  2143. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2144. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
  2145. " ccccccccccccccccccccccccccccccccccccccccc;");
  2146. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2147. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2148. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2149. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  2150. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2151. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2152. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2153. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  2154. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  2155. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  2156. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2157. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  2158. verifyFormat("if () {\n"
  2159. "} else if (aaaaa && bbbbb > // break\n"
  2160. " ccccc) {\n"
  2161. "}");
  2162. // Presence of a trailing comment used to change indentation of b.
  2163. verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
  2164. " b;\n"
  2165. "return aaaaaaaaaaaaaaaaaaa +\n"
  2166. " b; //",
  2167. getLLVMStyleWithColumns(30));
  2168. }
  2169. TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
  2170. // Not sure what the best system is here. Like this, the LHS can be found
  2171. // immediately above an operator (everything with the same or a higher
  2172. // indent). The RHS is aligned right of the operator and so compasses
  2173. // everything until something with the same indent as the operator is found.
  2174. // FIXME: Is this a good system?
  2175. FormatStyle Style = getLLVMStyle();
  2176. Style.BreakBeforeBinaryOperators = true;
  2177. verifyFormat(
  2178. "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2179. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2180. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2181. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2182. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  2183. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  2184. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2185. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2186. " > ccccccccccccccccccccccccccccccccccccccccc;",
  2187. Style);
  2188. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2189. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2190. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2191. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  2192. Style);
  2193. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2194. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2195. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2196. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  2197. Style);
  2198. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2199. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2200. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2201. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  2202. Style);
  2203. verifyFormat("if () {\n"
  2204. "} else if (aaaaa && bbbbb // break\n"
  2205. " > ccccc) {\n"
  2206. "}",
  2207. Style);
  2208. }
  2209. TEST_F(FormatTest, ConstructorInitializers) {
  2210. verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
  2211. verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
  2212. getLLVMStyleWithColumns(45));
  2213. verifyFormat("Constructor()\n"
  2214. " : Inttializer(FitsOnTheLine) {}",
  2215. getLLVMStyleWithColumns(44));
  2216. verifyFormat("Constructor()\n"
  2217. " : Inttializer(FitsOnTheLine) {}",
  2218. getLLVMStyleWithColumns(43));
  2219. verifyFormat(
  2220. "SomeClass::Constructor()\n"
  2221. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  2222. verifyFormat(
  2223. "SomeClass::Constructor()\n"
  2224. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  2225. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
  2226. verifyFormat(
  2227. "SomeClass::Constructor()\n"
  2228. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2229. " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  2230. verifyFormat("Constructor()\n"
  2231. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2232. " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2233. " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2234. " aaaaaaaaaaaaaaaaaaaaaaa() {}");
  2235. verifyFormat("Constructor()\n"
  2236. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2237. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  2238. verifyFormat("Constructor(int Parameter = 0)\n"
  2239. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
  2240. " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
  2241. // Here a line could be saved by splitting the second initializer onto two
  2242. // lines, but that is not desireable.
  2243. verifyFormat("Constructor()\n"
  2244. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2245. " aaaaaaaaaaa(aaaaaaaaaaa),\n"
  2246. " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  2247. FormatStyle OnePerLine = getLLVMStyle();
  2248. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  2249. verifyFormat("SomeClass::Constructor()\n"
  2250. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  2251. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  2252. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  2253. OnePerLine);
  2254. verifyFormat("SomeClass::Constructor()\n"
  2255. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
  2256. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  2257. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  2258. OnePerLine);
  2259. verifyFormat("MyClass::MyClass(int var)\n"
  2260. " : some_var_(var), // 4 space indent\n"
  2261. " some_other_var_(var + 1) { // lined up\n"
  2262. "}",
  2263. OnePerLine);
  2264. verifyFormat("Constructor()\n"
  2265. " : aaaaa(aaaaaa),\n"
  2266. " aaaaa(aaaaaa),\n"
  2267. " aaaaa(aaaaaa),\n"
  2268. " aaaaa(aaaaaa),\n"
  2269. " aaaaa(aaaaaa) {}",
  2270. OnePerLine);
  2271. verifyFormat("Constructor()\n"
  2272. " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
  2273. " aaaaaaaaaaaaaaaaaaaaaa) {}",
  2274. OnePerLine);
  2275. }
  2276. TEST_F(FormatTest, MemoizationTests) {
  2277. // This breaks if the memoization lookup does not take \c Indent and
  2278. // \c LastSpace into account.
  2279. verifyFormat(
  2280. "extern CFRunLoopTimerRef\n"
  2281. "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
  2282. " CFTimeInterval interval, CFOptionFlags flags,\n"
  2283. " CFIndex order, CFRunLoopTimerCallBack callout,\n"
  2284. " CFRunLoopTimerContext *context) {}");
  2285. // Deep nesting somewhat works around our memoization.
  2286. verifyFormat(
  2287. "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  2288. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  2289. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  2290. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  2291. " aaaaa())))))))))))))))))))))))))))))))))))))));",
  2292. getLLVMStyleWithColumns(65));
  2293. verifyFormat(
  2294. "aaaaa(\n"
  2295. " aaaaa,\n"
  2296. " aaaaa(\n"
  2297. " aaaaa,\n"
  2298. " aaaaa(\n"
  2299. " aaaaa,\n"
  2300. " aaaaa(\n"
  2301. " aaaaa,\n"
  2302. " aaaaa(\n"
  2303. " aaaaa,\n"
  2304. " aaaaa(\n"
  2305. " aaaaa,\n"
  2306. " aaaaa(\n"
  2307. " aaaaa,\n"
  2308. " aaaaa(\n"
  2309. " aaaaa,\n"
  2310. " aaaaa(\n"
  2311. " aaaaa,\n"
  2312. " aaaaa(\n"
  2313. " aaaaa,\n"
  2314. " aaaaa(\n"
  2315. " aaaaa,\n"
  2316. " aaaaa(\n"
  2317. " aaaaa,\n"
  2318. " aaaaa))))))))))));",
  2319. getLLVMStyleWithColumns(65));
  2320. verifyFormat(
  2321. "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"
  2322. " a),\n"
  2323. " a),\n"
  2324. " a),\n"
  2325. " a),\n"
  2326. " a),\n"
  2327. " a),\n"
  2328. " a),\n"
  2329. " a),\n"
  2330. " a),\n"
  2331. " a),\n"
  2332. " a),\n"
  2333. " a),\n"
  2334. " a),\n"
  2335. " a),\n"
  2336. " a),\n"
  2337. " a),\n"
  2338. " a)",
  2339. getLLVMStyleWithColumns(65));
  2340. // This test takes VERY long when memoization is broken.
  2341. FormatStyle OnePerLine = getLLVMStyle();
  2342. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  2343. OnePerLine.BinPackParameters = false;
  2344. std::string input = "Constructor()\n"
  2345. " : aaaa(a,\n";
  2346. for (unsigned i = 0, e = 80; i != e; ++i) {
  2347. input += " a,\n";
  2348. }
  2349. input += " a) {}";
  2350. verifyFormat(input, OnePerLine);
  2351. }
  2352. TEST_F(FormatTest, BreaksAsHighAsPossible) {
  2353. verifyFormat(
  2354. "void f() {\n"
  2355. " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
  2356. " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
  2357. " f();\n"
  2358. "}");
  2359. verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
  2360. " Intervals[i - 1].getRange().getLast()) {\n}");
  2361. }
  2362. TEST_F(FormatTest, BreaksFunctionDeclarations) {
  2363. // Principially, we break function declarations in a certain order:
  2364. // 1) break amongst arguments.
  2365. verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
  2366. " Cccccccccccccc cccccccccccccc);");
  2367. verifyFormat(
  2368. "template <class TemplateIt>\n"
  2369. "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
  2370. " TemplateIt *stop) {}");
  2371. // 2) break after return type.
  2372. verifyFormat(
  2373. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2374. " bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
  2375. getGoogleStyle());
  2376. // 3) break after (.
  2377. verifyFormat(
  2378. "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
  2379. " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
  2380. getGoogleStyle());
  2381. // 4) break before after nested name specifiers.
  2382. verifyFormat(
  2383. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2384. " SomeClasssssssssssssssssssssssssssssssssssssss::\n"
  2385. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
  2386. getGoogleStyle());
  2387. // However, there are exceptions, if a sufficient amount of lines can be
  2388. // saved.
  2389. // FIXME: The precise cut-offs wrt. the number of saved lines might need some
  2390. // more adjusting.
  2391. verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  2392. " Cccccccccccccc cccccccccc,\n"
  2393. " Cccccccccccccc cccccccccc,\n"
  2394. " Cccccccccccccc cccccccccc,\n"
  2395. " Cccccccccccccc cccccccccc);");
  2396. verifyFormat(
  2397. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2398. " bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  2399. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  2400. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
  2401. getGoogleStyle());
  2402. verifyFormat(
  2403. "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  2404. " Cccccccccccccc cccccccccc,\n"
  2405. " Cccccccccccccc cccccccccc,\n"
  2406. " Cccccccccccccc cccccccccc,\n"
  2407. " Cccccccccccccc cccccccccc,\n"
  2408. " Cccccccccccccc cccccccccc,\n"
  2409. " Cccccccccccccc cccccccccc);");
  2410. verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  2411. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  2412. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  2413. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  2414. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
  2415. // Break after multi-line parameters.
  2416. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2417. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2418. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2419. " bbbb bbbb);");
  2420. // Treat overloaded operators like other functions.
  2421. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  2422. "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
  2423. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  2424. "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
  2425. verifyGoogleFormat(
  2426. "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
  2427. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  2428. }
  2429. TEST_F(FormatTest, TrailingReturnType) {
  2430. verifyFormat("auto foo() -> int;\n");
  2431. verifyFormat("struct S {\n"
  2432. " auto bar() const -> int;\n"
  2433. "};");
  2434. verifyFormat("template <size_t Order, typename T>\n"
  2435. "auto load_img(const std::string &filename)\n"
  2436. " -> alias::tensor<Order, T, mem::tag::cpu> {}");
  2437. // Not trailing return types.
  2438. verifyFormat("void f() { auto a = b->c(); }");
  2439. }
  2440. TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
  2441. // Avoid breaking before trailing 'const'.
  2442. verifyFormat("void someLongFunction(\n"
  2443. " int someLongParameter) const {}",
  2444. getLLVMStyleWithColumns(46));
  2445. FormatStyle Style = getGoogleStyle();
  2446. Style.ColumnLimit = 47;
  2447. verifyFormat("void\n"
  2448. "someLongFunction(int someLongParameter) const {\n}",
  2449. getLLVMStyleWithColumns(47));
  2450. verifyFormat("void someLongFunction(\n"
  2451. " int someLongParameter) const {}",
  2452. Style);
  2453. verifyFormat("LoooooongReturnType\n"
  2454. "someLoooooooongFunction() const {}",
  2455. getLLVMStyleWithColumns(47));
  2456. verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
  2457. " const {}",
  2458. Style);
  2459. // Avoid breaking before other trailing annotations, if they are not
  2460. // function-like.
  2461. verifyFormat("void SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2462. " aaaaaaaaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
  2463. // Breaking before function-like trailing annotations is fine to keep them
  2464. // close to their arguments.
  2465. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2466. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  2467. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  2468. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  2469. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  2470. " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
  2471. verifyFormat(
  2472. "void aaaaaaaaaaaaaaaaaa()\n"
  2473. " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
  2474. " aaaaaaaaaaaaaaaaaaaaaaaaa));");
  2475. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2476. " __attribute__((unused));");
  2477. verifyFormat(
  2478. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2479. " GUARDED_BY(aaaaaaaaaaaa);",
  2480. getGoogleStyle());
  2481. verifyFormat(
  2482. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2483. " GUARDED_BY(aaaaaaaaaaaa);",
  2484. getGoogleStyle());
  2485. }
  2486. TEST_F(FormatTest, BreaksDesireably) {
  2487. verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  2488. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  2489. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
  2490. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2491. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  2492. "}");
  2493. verifyFormat(
  2494. "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2495. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  2496. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2497. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2498. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  2499. verifyFormat(
  2500. "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2501. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  2502. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2503. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
  2504. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2505. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2506. verifyFormat(
  2507. "void f() {\n"
  2508. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
  2509. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  2510. "}");
  2511. verifyFormat(
  2512. "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2513. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  2514. verifyFormat(
  2515. "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2516. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  2517. verifyFormat(
  2518. "aaaaaaaaaaaaaaaaa(\n"
  2519. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2520. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2521. // Indent consistently indenpendent of call expression.
  2522. verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
  2523. " dddddddddddddddddddddddddddddd));\n"
  2524. "aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  2525. " dddddddddddddddddddddddddddddd));");
  2526. // This test case breaks on an incorrect memoization, i.e. an optimization not
  2527. // taking into account the StopAt value.
  2528. verifyFormat(
  2529. "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2530. " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2531. " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2532. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2533. verifyFormat("{\n {\n {\n"
  2534. " Annotation.SpaceRequiredBefore =\n"
  2535. " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
  2536. " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
  2537. " }\n }\n}");
  2538. // Break on an outer level if there was a break on an inner level.
  2539. EXPECT_EQ("f(g(h(a, // comment\n"
  2540. " b, c),\n"
  2541. " d, e),\n"
  2542. " x, y);",
  2543. format("f(g(h(a, // comment\n"
  2544. " b, c), d, e), x, y);"));
  2545. // Prefer breaking similar line breaks.
  2546. verifyFormat(
  2547. "const int kTrackingOptions = NSTrackingMouseMoved |\n"
  2548. " NSTrackingMouseEnteredAndExited |\n"
  2549. " NSTrackingActiveAlways;");
  2550. }
  2551. TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
  2552. FormatStyle NoBinPacking = getGoogleStyle();
  2553. NoBinPacking.BinPackParameters = false;
  2554. verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
  2555. " aaaaaaaaaaaaaaaaaaaa,\n"
  2556. " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
  2557. NoBinPacking);
  2558. verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
  2559. " aaaaaaaaaaaaa,\n"
  2560. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
  2561. NoBinPacking);
  2562. verifyFormat(
  2563. "aaaaaaaa(aaaaaaaaaaaaa,\n"
  2564. " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2565. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  2566. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2567. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
  2568. NoBinPacking);
  2569. verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  2570. " .aaaaaaaaaaaaaaaaaa();",
  2571. NoBinPacking);
  2572. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2573. " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);",
  2574. NoBinPacking);
  2575. verifyFormat(
  2576. "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2577. " aaaaaaaaaaaa,\n"
  2578. " aaaaaaaaaaaa);",
  2579. NoBinPacking);
  2580. verifyFormat(
  2581. "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
  2582. " ddddddddddddddddddddddddddddd),\n"
  2583. " test);",
  2584. NoBinPacking);
  2585. verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
  2586. " aaaaaaaaaaaaaaaaaaaaaaa,\n"
  2587. " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;",
  2588. NoBinPacking);
  2589. verifyFormat("a(\"a\"\n"
  2590. " \"a\",\n"
  2591. " a);");
  2592. NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
  2593. verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
  2594. " aaaaaaaaa,\n"
  2595. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  2596. NoBinPacking);
  2597. verifyFormat(
  2598. "void f() {\n"
  2599. " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  2600. " .aaaaaaa();\n"
  2601. "}",
  2602. NoBinPacking);
  2603. verifyFormat(
  2604. "template <class SomeType, class SomeOtherType>\n"
  2605. "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
  2606. NoBinPacking);
  2607. }
  2608. TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
  2609. FormatStyle Style = getLLVMStyleWithColumns(15);
  2610. Style.ExperimentalAutoDetectBinPacking = true;
  2611. EXPECT_EQ("aaa(aaaa,\n"
  2612. " aaaa,\n"
  2613. " aaaa);\n"
  2614. "aaa(aaaa,\n"
  2615. " aaaa,\n"
  2616. " aaaa);",
  2617. format("aaa(aaaa,\n" // one-per-line
  2618. " aaaa,\n"
  2619. " aaaa );\n"
  2620. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  2621. Style));
  2622. EXPECT_EQ("aaa(aaaa, aaaa,\n"
  2623. " aaaa);\n"
  2624. "aaa(aaaa, aaaa,\n"
  2625. " aaaa);",
  2626. format("aaa(aaaa, aaaa,\n" // bin-packed
  2627. " aaaa );\n"
  2628. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  2629. Style));
  2630. }
  2631. TEST_F(FormatTest, FormatsBuilderPattern) {
  2632. verifyFormat(
  2633. "return llvm::StringSwitch<Reference::Kind>(name)\n"
  2634. " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
  2635. " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
  2636. " .StartsWith(\".init\", ORDER_INIT)\n"
  2637. " .StartsWith(\".fini\", ORDER_FINI)\n"
  2638. " .StartsWith(\".hash\", ORDER_HASH)\n"
  2639. " .Default(ORDER_TEXT);\n");
  2640. verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
  2641. " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
  2642. verifyFormat(
  2643. "aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaa(\n"
  2644. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2645. " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
  2646. verifyFormat(
  2647. "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
  2648. " aaaaaaaaaaaaaa);");
  2649. verifyFormat(
  2650. "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
  2651. " aaaaaa->aaaaaaaaaaaa()\n"
  2652. " ->aaaaaaaaaaaaaaaa(\n"
  2653. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2654. " ->aaaaaaaaaaaaaaaaa();");
  2655. verifyFormat(
  2656. "someobj->Add((new util::filetools::Handler(dir))\n"
  2657. " ->OnEvent1(NewPermanentCallback(\n"
  2658. " this, &HandlerHolderClass::EventHandlerCBA))\n"
  2659. " ->OnEvent2(NewPermanentCallback(\n"
  2660. " this, &HandlerHolderClass::EventHandlerCBB))\n"
  2661. " ->OnEvent3(NewPermanentCallback(\n"
  2662. " this, &HandlerHolderClass::EventHandlerCBC))\n"
  2663. " ->OnEvent5(NewPermanentCallback(\n"
  2664. " this, &HandlerHolderClass::EventHandlerCBD))\n"
  2665. " ->OnEvent6(NewPermanentCallback(\n"
  2666. " this, &HandlerHolderClass::EventHandlerCBE)));");
  2667. verifyFormat(
  2668. "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
  2669. verifyFormat("aaaaaaaaaaaaaaa()\n"
  2670. " .aaaaaaaaaaaaaaa()\n"
  2671. " .aaaaaaaaaaaaaaa()\n"
  2672. " .aaaaaaaaaaaaaaa()\n"
  2673. " .aaaaaaaaaaaaaaa();");
  2674. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  2675. " .aaaaaaaaaaaaaaa()\n"
  2676. " .aaaaaaaaaaaaaaa()\n"
  2677. " .aaaaaaaaaaaaaaa();");
  2678. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  2679. " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  2680. " .aaaaaaaaaaaaaaa();");
  2681. verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
  2682. " ->aaaaaaaaaaaaaae(0)\n"
  2683. " ->aaaaaaaaaaaaaaa();");
  2684. }
  2685. TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
  2686. verifyFormat(
  2687. "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  2688. " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
  2689. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  2690. " ccccccccccccccccccccccccc) {\n}");
  2691. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  2692. " ccccccccccccccccccccccccc) {\n}");
  2693. verifyFormat(
  2694. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
  2695. " ccccccccccccccccccccccccc) {\n}");
  2696. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
  2697. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
  2698. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
  2699. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  2700. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
  2701. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
  2702. " aaaaaaaaaaaaaaa != aa) {\n}");
  2703. }
  2704. TEST_F(FormatTest, BreaksAfterAssignments) {
  2705. verifyFormat(
  2706. "unsigned Cost =\n"
  2707. " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
  2708. " SI->getPointerAddressSpaceee());\n");
  2709. verifyFormat(
  2710. "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
  2711. " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
  2712. verifyFormat(
  2713. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
  2714. " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
  2715. verifyFormat("unsigned OriginalStartColumn =\n"
  2716. " SourceMgr.getSpellingColumnNumber(\n"
  2717. " Current.FormatTok.getStartOfNonWhitespace()) -\n"
  2718. " 1;");
  2719. }
  2720. TEST_F(FormatTest, AlignsAfterAssignments) {
  2721. verifyFormat(
  2722. "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2723. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  2724. verifyFormat(
  2725. "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2726. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  2727. verifyFormat(
  2728. "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2729. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  2730. verifyFormat(
  2731. "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2732. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  2733. verifyFormat(
  2734. "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2735. " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2736. " aaaaaaaaaaaaaaaaaaaaaaaa;");
  2737. }
  2738. TEST_F(FormatTest, AlignsAfterReturn) {
  2739. verifyFormat(
  2740. "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2741. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  2742. verifyFormat(
  2743. "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2744. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  2745. verifyFormat(
  2746. "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  2747. " aaaaaaaaaaaaaaaaaaaaaa();");
  2748. verifyFormat(
  2749. "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  2750. " aaaaaaaaaaaaaaaaaaaaaa());");
  2751. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2752. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2753. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2754. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
  2755. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  2756. }
  2757. TEST_F(FormatTest, BreaksConditionalExpressions) {
  2758. verifyFormat(
  2759. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2760. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2761. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2762. verifyFormat(
  2763. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2764. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2765. verifyFormat(
  2766. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
  2767. " : aaaaaaaaaaaaa);");
  2768. verifyFormat(
  2769. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2770. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2771. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2772. " aaaaaaaaaaaaa);");
  2773. verifyFormat(
  2774. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2775. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2776. " aaaaaaaaaaaaa);");
  2777. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2778. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2779. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2780. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2781. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2782. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2783. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2784. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2785. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2786. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2787. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2788. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2789. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  2790. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2791. " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  2792. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  2793. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  2794. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2795. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2796. " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  2797. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  2798. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2799. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2800. " : aaaaaaaaaaaaaaaa;");
  2801. verifyFormat(
  2802. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2803. " ? aaaaaaaaaaaaaaa\n"
  2804. " : aaaaaaaaaaaaaaa;");
  2805. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  2806. " aaaaaaaaa\n"
  2807. " ? b\n"
  2808. " : c);");
  2809. verifyFormat(
  2810. "unsigned Indent =\n"
  2811. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
  2812. " ? IndentForLevel[TheLine.Level]\n"
  2813. " : TheLine * 2,\n"
  2814. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  2815. getLLVMStyleWithColumns(70));
  2816. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  2817. " ? aaaaaaaaaaaaaaa\n"
  2818. " : bbbbbbbbbbbbbbb //\n"
  2819. " ? ccccccccccccccc\n"
  2820. " : ddddddddddddddd;");
  2821. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  2822. " ? aaaaaaaaaaaaaaa\n"
  2823. " : (bbbbbbbbbbbbbbb //\n"
  2824. " ? ccccccccccccccc\n"
  2825. " : ddddddddddddddd);");
  2826. FormatStyle NoBinPacking = getLLVMStyle();
  2827. NoBinPacking.BinPackParameters = false;
  2828. verifyFormat(
  2829. "void f() {\n"
  2830. " g(aaa,\n"
  2831. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  2832. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2833. " ? aaaaaaaaaaaaaaa\n"
  2834. " : aaaaaaaaaaaaaaa);\n"
  2835. "}",
  2836. NoBinPacking);
  2837. verifyFormat(
  2838. "void f() {\n"
  2839. " g(aaa,\n"
  2840. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  2841. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2842. " ?: aaaaaaaaaaaaaaa);\n"
  2843. "}",
  2844. NoBinPacking);
  2845. }
  2846. TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
  2847. verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
  2848. " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
  2849. verifyFormat("bool a = true, b = false;");
  2850. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  2851. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
  2852. " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
  2853. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
  2854. verifyFormat(
  2855. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  2856. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
  2857. " d = e && f;");
  2858. verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
  2859. " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
  2860. verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  2861. " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
  2862. verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
  2863. " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
  2864. // FIXME: If multiple variables are defined, the "*" needs to move to the new
  2865. // line. Also fix indent for breaking after the type, this looks bad.
  2866. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
  2867. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
  2868. " *b = bbbbbbbbbbbbbbbbbbb;",
  2869. getGoogleStyle());
  2870. // Not ideal, but pointer-with-type does not allow much here.
  2871. verifyGoogleFormat(
  2872. "aaaaaaaaa* a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  2873. " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;");
  2874. }
  2875. TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
  2876. verifyFormat("arr[foo ? bar : baz];");
  2877. verifyFormat("f()[foo ? bar : baz];");
  2878. verifyFormat("(a + b)[foo ? bar : baz];");
  2879. verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
  2880. }
  2881. TEST_F(FormatTest, AlignsStringLiterals) {
  2882. verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
  2883. " \"short literal\");");
  2884. verifyFormat(
  2885. "looooooooooooooooooooooooongFunction(\n"
  2886. " \"short literal\"\n"
  2887. " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
  2888. verifyFormat("someFunction(\"Always break between multi-line\"\n"
  2889. " \" string literals\",\n"
  2890. " and, other, parameters);");
  2891. EXPECT_EQ("fun + \"1243\" /* comment */\n"
  2892. " \"5678\";",
  2893. format("fun + \"1243\" /* comment */\n"
  2894. " \"5678\";",
  2895. getLLVMStyleWithColumns(28)));
  2896. EXPECT_EQ(
  2897. "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  2898. " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
  2899. " \"aaaaaaaaaaaaaaaa\";",
  2900. format("aaaaaa ="
  2901. "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  2902. "aaaaaaaaaaaaaaaaaaaaa\" "
  2903. "\"aaaaaaaaaaaaaaaa\";"));
  2904. verifyFormat("a = a + \"a\"\n"
  2905. " \"a\"\n"
  2906. " \"a\";");
  2907. verifyFormat("f(\"a\", \"b\"\n"
  2908. " \"c\");");
  2909. verifyFormat(
  2910. "#define LL_FORMAT \"ll\"\n"
  2911. "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
  2912. " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
  2913. verifyFormat("#define A(X) \\\n"
  2914. " \"aaaaa\" #X \"bbbbbb\" \\\n"
  2915. " \"ccccc\"",
  2916. getLLVMStyleWithColumns(23));
  2917. verifyFormat("#define A \"def\"\n"
  2918. "f(\"abc\" A \"ghi\"\n"
  2919. " \"jkl\");");
  2920. }
  2921. TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
  2922. FormatStyle NoBreak = getLLVMStyle();
  2923. NoBreak.AlwaysBreakBeforeMultilineStrings = false;
  2924. FormatStyle Break = getLLVMStyle();
  2925. Break.AlwaysBreakBeforeMultilineStrings = true;
  2926. verifyFormat("aaaa = \"bbbb\"\n"
  2927. " \"cccc\";",
  2928. NoBreak);
  2929. verifyFormat("aaaa =\n"
  2930. " \"bbbb\"\n"
  2931. " \"cccc\";",
  2932. Break);
  2933. verifyFormat("aaaa(\"bbbb\"\n"
  2934. " \"cccc\");",
  2935. NoBreak);
  2936. verifyFormat("aaaa(\n"
  2937. " \"bbbb\"\n"
  2938. " \"cccc\");",
  2939. Break);
  2940. verifyFormat("aaaa(qqq, \"bbbb\"\n"
  2941. " \"cccc\");",
  2942. NoBreak);
  2943. verifyFormat("aaaa(qqq,\n"
  2944. " \"bbbb\"\n"
  2945. " \"cccc\");",
  2946. Break);
  2947. // Don't break if there is no column gain.
  2948. verifyFormat("f(\"aaaa\"\n"
  2949. " \"bbbb\");",
  2950. Break);
  2951. // Treat literals with escaped newlines like multi-line string literals.
  2952. EXPECT_EQ("x = \"a\\\n"
  2953. "b\\\n"
  2954. "c\";",
  2955. format("x = \"a\\\n"
  2956. "b\\\n"
  2957. "c\";",
  2958. NoBreak));
  2959. EXPECT_EQ("x =\n"
  2960. " \"a\\\n"
  2961. "b\\\n"
  2962. "c\";",
  2963. format("x = \"a\\\n"
  2964. "b\\\n"
  2965. "c\";",
  2966. Break));
  2967. }
  2968. TEST_F(FormatTest, AlignsPipes) {
  2969. verifyFormat(
  2970. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2971. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2972. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  2973. verifyFormat(
  2974. "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
  2975. " << aaaaaaaaaaaaaaaaaaaa;");
  2976. verifyFormat(
  2977. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2978. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  2979. verifyFormat(
  2980. "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
  2981. " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
  2982. " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
  2983. verifyFormat(
  2984. "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  2985. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  2986. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  2987. verifyFormat("return out << \"somepacket = {\\n\"\n"
  2988. " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
  2989. " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
  2990. " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
  2991. " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
  2992. " << \"}\";");
  2993. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  2994. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  2995. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
  2996. verifyFormat(
  2997. "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
  2998. " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
  2999. " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
  3000. " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
  3001. " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
  3002. verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
  3003. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  3004. verifyFormat(
  3005. "void f() {\n"
  3006. " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
  3007. " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3008. "}");
  3009. // Breaking before the first "<<" is generally not desirable.
  3010. verifyFormat(
  3011. "llvm::errs()\n"
  3012. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3013. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3014. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3015. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  3016. getLLVMStyleWithColumns(70));
  3017. verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  3018. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3019. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  3020. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3021. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  3022. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  3023. getLLVMStyleWithColumns(70));
  3024. // But sometimes, breaking before the first "<<" is desirable.
  3025. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
  3026. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3027. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3028. verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
  3029. " << BEF << IsTemplate << Description << E->getType();");
  3030. verifyFormat(
  3031. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3032. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3033. }
  3034. TEST_F(FormatTest, UnderstandsEquals) {
  3035. verifyFormat(
  3036. "aaaaaaaaaaaaaaaaa =\n"
  3037. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3038. verifyFormat(
  3039. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3040. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  3041. verifyFormat(
  3042. "if (a) {\n"
  3043. " f();\n"
  3044. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3045. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  3046. "}");
  3047. verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3048. " 100000000 + 10000000) {\n}");
  3049. }
  3050. TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
  3051. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  3052. " .looooooooooooooooooooooooooooooooooooooongFunction();");
  3053. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  3054. " ->looooooooooooooooooooooooooooooooooooooongFunction();");
  3055. verifyFormat(
  3056. "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
  3057. " Parameter2);");
  3058. verifyFormat(
  3059. "ShortObject->shortFunction(\n"
  3060. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
  3061. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
  3062. verifyFormat("loooooooooooooongFunction(\n"
  3063. " LoooooooooooooongObject->looooooooooooooooongFunction());");
  3064. verifyFormat(
  3065. "function(LoooooooooooooooooooooooooooooooooooongObject\n"
  3066. " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
  3067. verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  3068. " .WillRepeatedly(Return(SomeValue));");
  3069. verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)]\n"
  3070. " .insert(ccccccccccccccccccccccc);");
  3071. verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3072. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).aaaaa(aaaaa),\n"
  3073. " aaaaaaaaaaaaaaaaaaaaa);");
  3074. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3075. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());");
  3076. verifyFormat(
  3077. "aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3078. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3079. " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3080. " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3081. " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3082. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3083. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3084. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3085. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
  3086. "}");
  3087. // Here, it is not necessary to wrap at "." or "->".
  3088. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
  3089. " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  3090. verifyFormat(
  3091. "aaaaaaaaaaa->aaaaaaaaa(\n"
  3092. " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3093. " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
  3094. verifyFormat(
  3095. "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3096. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
  3097. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
  3098. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  3099. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
  3100. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  3101. // FIXME: Should we break before .a()?
  3102. verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3103. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).a();");
  3104. FormatStyle NoBinPacking = getLLVMStyle();
  3105. NoBinPacking.BinPackParameters = false;
  3106. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  3107. " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  3108. " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
  3109. " aaaaaaaaaaaaaaaaaaa,\n"
  3110. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3111. NoBinPacking);
  3112. // If there is a subsequent call, change to hanging indentation.
  3113. verifyFormat(
  3114. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3115. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
  3116. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3117. verifyFormat(
  3118. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3119. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
  3120. }
  3121. TEST_F(FormatTest, WrapsTemplateDeclarations) {
  3122. verifyFormat("template <typename T>\n"
  3123. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  3124. verifyFormat(
  3125. "template <typename T>\n"
  3126. "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
  3127. verifyFormat("template <typename T>\n"
  3128. "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
  3129. " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
  3130. verifyFormat(
  3131. "template <typename T>\n"
  3132. "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
  3133. " int Paaaaaaaaaaaaaaaaaaaaram2);");
  3134. verifyFormat(
  3135. "template <typename T>\n"
  3136. "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
  3137. " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
  3138. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3139. verifyFormat("template <typename T>\n"
  3140. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3141. " int aaaaaaaaaaaaaaaaaaaaaa);");
  3142. verifyFormat(
  3143. "template <typename T1, typename T2 = char, typename T3 = char,\n"
  3144. " typename T4 = char>\n"
  3145. "void f();");
  3146. verifyFormat(
  3147. "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
  3148. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3149. verifyFormat("a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
  3150. " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));");
  3151. verifyFormat("template <typename T> class C {};");
  3152. verifyFormat("template <typename T> void f();");
  3153. verifyFormat("template <typename T> void f() {}");
  3154. FormatStyle AlwaysBreak = getLLVMStyle();
  3155. AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
  3156. verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
  3157. verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
  3158. verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
  3159. verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3160. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
  3161. " ccccccccccccccccccccccccccccccccccccccccccccccc);");
  3162. verifyFormat(
  3163. "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  3164. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3165. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
  3166. " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  3167. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3168. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
  3169. " bbbbbbbbbbbbbbbbbbbbbbbb);",
  3170. getLLVMStyleWithColumns(72));
  3171. }
  3172. TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
  3173. verifyFormat(
  3174. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  3175. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3176. verifyFormat(
  3177. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  3178. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3179. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  3180. // FIXME: Should we have the extra indent after the second break?
  3181. verifyFormat(
  3182. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  3183. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  3184. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3185. verifyFormat(
  3186. "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
  3187. " cccccccccccccccccccccccccccccccccccccccccccccc());");
  3188. // Breaking at nested name specifiers is generally not desirable.
  3189. verifyFormat(
  3190. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3191. " aaaaaaaaaaaaaaaaaaaaaaa);");
  3192. verifyFormat(
  3193. "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  3194. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3195. " aaaaaaaaaaaaaaaaaaaaa);",
  3196. getLLVMStyleWithColumns(74));
  3197. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  3198. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3199. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3200. }
  3201. TEST_F(FormatTest, UnderstandsTemplateParameters) {
  3202. verifyFormat("A<int> a;");
  3203. verifyFormat("A<A<A<int> > > a;");
  3204. verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
  3205. verifyFormat("bool x = a < 1 || 2 > a;");
  3206. verifyFormat("bool x = 5 < f<int>();");
  3207. verifyFormat("bool x = f<int>() > 5;");
  3208. verifyFormat("bool x = 5 < a<int>::x;");
  3209. verifyFormat("bool x = a < 4 ? a > 2 : false;");
  3210. verifyFormat("bool x = f() ? a < 2 : a > 2;");
  3211. verifyGoogleFormat("A<A<int>> a;");
  3212. verifyGoogleFormat("A<A<A<int>>> a;");
  3213. verifyGoogleFormat("A<A<A<A<int>>>> a;");
  3214. verifyGoogleFormat("A<A<int> > a;");
  3215. verifyGoogleFormat("A<A<A<int> > > a;");
  3216. verifyGoogleFormat("A<A<A<A<int> > > > a;");
  3217. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
  3218. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
  3219. verifyFormat("test >> a >> b;");
  3220. verifyFormat("test << a >> b;");
  3221. verifyFormat("f<int>();");
  3222. verifyFormat("template <typename T> void f() {}");
  3223. // Not template parameters.
  3224. verifyFormat("return a < b && c > d;");
  3225. verifyFormat("void f() {\n"
  3226. " while (a < b && c > d) {\n"
  3227. " }\n"
  3228. "}");
  3229. verifyFormat("template <typename... Types>\n"
  3230. "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
  3231. }
  3232. TEST_F(FormatTest, UnderstandsBinaryOperators) {
  3233. verifyFormat("COMPARE(a, ==, b);");
  3234. }
  3235. TEST_F(FormatTest, UnderstandsPointersToMembers) {
  3236. verifyFormat("int A::*x;");
  3237. verifyFormat("int (S::*func)(void *);");
  3238. verifyFormat("void f() { int (S::*func)(void *); }");
  3239. verifyFormat("typedef bool *(Class::*Member)() const;");
  3240. verifyFormat("void f() {\n"
  3241. " (a->*f)();\n"
  3242. " a->*x;\n"
  3243. " (a.*f)();\n"
  3244. " ((*a).*f)();\n"
  3245. " a.*x;\n"
  3246. "}");
  3247. verifyFormat("void f() {\n"
  3248. " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  3249. " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
  3250. "}");
  3251. FormatStyle Style = getLLVMStyle();
  3252. Style.PointerBindsToType = true;
  3253. verifyFormat("typedef bool* (Class::*Member)() const;", Style);
  3254. }
  3255. TEST_F(FormatTest, UnderstandsUnaryOperators) {
  3256. verifyFormat("int a = -2;");
  3257. verifyFormat("f(-1, -2, -3);");
  3258. verifyFormat("a[-1] = 5;");
  3259. verifyFormat("int a = 5 + -2;");
  3260. verifyFormat("if (i == -1) {\n}");
  3261. verifyFormat("if (i != -1) {\n}");
  3262. verifyFormat("if (i > -1) {\n}");
  3263. verifyFormat("if (i < -1) {\n}");
  3264. verifyFormat("++(a->f());");
  3265. verifyFormat("--(a->f());");
  3266. verifyFormat("(a->f())++;");
  3267. verifyFormat("a[42]++;");
  3268. verifyFormat("if (!(a->f())) {\n}");
  3269. verifyFormat("a-- > b;");
  3270. verifyFormat("b ? -a : c;");
  3271. verifyFormat("n * sizeof char16;");
  3272. verifyFormat("n * alignof char16;", getGoogleStyle());
  3273. verifyFormat("sizeof(char);");
  3274. verifyFormat("alignof(char);", getGoogleStyle());
  3275. verifyFormat("return -1;");
  3276. verifyFormat("switch (a) {\n"
  3277. "case -1:\n"
  3278. " break;\n"
  3279. "}");
  3280. verifyFormat("#define X -1");
  3281. verifyFormat("#define X -kConstant");
  3282. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
  3283. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
  3284. verifyFormat("int a = /* confusing comment */ -1;");
  3285. // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
  3286. verifyFormat("int a = i /* confusing comment */++;");
  3287. }
  3288. TEST_F(FormatTest, IndentsRelativeToUnaryOperators) {
  3289. verifyFormat("if (!aaaaaaaaaa( // break\n"
  3290. " aaaaa)) {\n"
  3291. "}");
  3292. verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
  3293. " aaaaa));");
  3294. // Only indent relative to unary operators if the expression is nested.
  3295. verifyFormat("*aaa = aaaaaaa( // break\n"
  3296. " bbbbbb);");
  3297. }
  3298. TEST_F(FormatTest, UnderstandsOverloadedOperators) {
  3299. verifyFormat("bool operator<();");
  3300. verifyFormat("bool operator>();");
  3301. verifyFormat("bool operator=();");
  3302. verifyFormat("bool operator==();");
  3303. verifyFormat("bool operator!=();");
  3304. verifyFormat("int operator+();");
  3305. verifyFormat("int operator++();");
  3306. verifyFormat("bool operator();");
  3307. verifyFormat("bool operator()();");
  3308. verifyFormat("bool operator[]();");
  3309. verifyFormat("operator bool();");
  3310. verifyFormat("operator int();");
  3311. verifyFormat("operator void *();");
  3312. verifyFormat("operator SomeType<int>();");
  3313. verifyFormat("operator SomeType<int, int>();");
  3314. verifyFormat("operator SomeType<SomeType<int> >();");
  3315. verifyFormat("void *operator new(std::size_t size);");
  3316. verifyFormat("void *operator new[](std::size_t size);");
  3317. verifyFormat("void operator delete(void *ptr);");
  3318. verifyFormat("void operator delete[](void *ptr);");
  3319. verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
  3320. "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
  3321. verifyFormat(
  3322. "ostream &operator<<(ostream &OutputStream,\n"
  3323. " SomeReallyLongType WithSomeReallyLongValue);");
  3324. verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
  3325. " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
  3326. " return left.group < right.group;\n"
  3327. "}");
  3328. verifyFormat("SomeType &operator=(const SomeType &S);");
  3329. verifyGoogleFormat("operator void*();");
  3330. verifyGoogleFormat("operator SomeType<SomeType<int>>();");
  3331. }
  3332. TEST_F(FormatTest, UnderstandsNewAndDelete) {
  3333. verifyFormat("void f() {\n"
  3334. " A *a = new A;\n"
  3335. " A *a = new (placement) A;\n"
  3336. " delete a;\n"
  3337. " delete (A *)a;\n"
  3338. "}");
  3339. }
  3340. TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
  3341. verifyFormat("int *f(int *a) {}");
  3342. verifyFormat("int main(int argc, char **argv) {}");
  3343. verifyFormat("Test::Test(int b) : a(b * b) {}");
  3344. verifyIndependentOfContext("f(a, *a);");
  3345. verifyFormat("void g() { f(*a); }");
  3346. verifyIndependentOfContext("int a = b * 10;");
  3347. verifyIndependentOfContext("int a = 10 * b;");
  3348. verifyIndependentOfContext("int a = b * c;");
  3349. verifyIndependentOfContext("int a += b * c;");
  3350. verifyIndependentOfContext("int a -= b * c;");
  3351. verifyIndependentOfContext("int a *= b * c;");
  3352. verifyIndependentOfContext("int a /= b * c;");
  3353. verifyIndependentOfContext("int a = *b;");
  3354. verifyIndependentOfContext("int a = *b * c;");
  3355. verifyIndependentOfContext("int a = b * *c;");
  3356. verifyIndependentOfContext("return 10 * b;");
  3357. verifyIndependentOfContext("return *b * *c;");
  3358. verifyIndependentOfContext("return a & ~b;");
  3359. verifyIndependentOfContext("f(b ? *c : *d);");
  3360. verifyIndependentOfContext("int a = b ? *c : *d;");
  3361. verifyIndependentOfContext("*b = a;");
  3362. verifyIndependentOfContext("a * ~b;");
  3363. verifyIndependentOfContext("a * !b;");
  3364. verifyIndependentOfContext("a * +b;");
  3365. verifyIndependentOfContext("a * -b;");
  3366. verifyIndependentOfContext("a * ++b;");
  3367. verifyIndependentOfContext("a * --b;");
  3368. verifyIndependentOfContext("a[4] * b;");
  3369. verifyIndependentOfContext("a[a * a] = 1;");
  3370. verifyIndependentOfContext("f() * b;");
  3371. verifyIndependentOfContext("a * [self dostuff];");
  3372. verifyIndependentOfContext("int x = a * (a + b);");
  3373. verifyIndependentOfContext("(a *)(a + b);");
  3374. verifyIndependentOfContext("int *pa = (int *)&a;");
  3375. verifyIndependentOfContext("return sizeof(int **);");
  3376. verifyIndependentOfContext("return sizeof(int ******);");
  3377. verifyIndependentOfContext("return (int **&)a;");
  3378. verifyIndependentOfContext("f((*PointerToArray)[10]);");
  3379. verifyFormat("void f(Type (*parameter)[10]) {}");
  3380. verifyGoogleFormat("return sizeof(int**);");
  3381. verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
  3382. verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
  3383. verifyFormat("auto a = [](int **&, int ***) {};");
  3384. verifyIndependentOfContext("InvalidRegions[*R] = 0;");
  3385. verifyIndependentOfContext("A<int *> a;");
  3386. verifyIndependentOfContext("A<int **> a;");
  3387. verifyIndependentOfContext("A<int *, int *> a;");
  3388. verifyIndependentOfContext(
  3389. "const char *const p = reinterpret_cast<const char *const>(q);");
  3390. verifyIndependentOfContext("A<int **, int **> a;");
  3391. verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
  3392. verifyFormat("for (char **a = b; *a; ++a) {\n}");
  3393. verifyFormat("for (; a && b;) {\n}");
  3394. verifyFormat(
  3395. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3396. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3397. verifyGoogleFormat("int main(int argc, char** argv) {}");
  3398. verifyGoogleFormat("A<int*> a;");
  3399. verifyGoogleFormat("A<int**> a;");
  3400. verifyGoogleFormat("A<int*, int*> a;");
  3401. verifyGoogleFormat("A<int**, int**> a;");
  3402. verifyGoogleFormat("f(b ? *c : *d);");
  3403. verifyGoogleFormat("int a = b ? *c : *d;");
  3404. verifyGoogleFormat("Type* t = **x;");
  3405. verifyGoogleFormat("Type* t = *++*x;");
  3406. verifyGoogleFormat("*++*x;");
  3407. verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
  3408. verifyGoogleFormat("Type* t = x++ * y;");
  3409. verifyGoogleFormat(
  3410. "const char* const p = reinterpret_cast<const char* const>(q);");
  3411. verifyIndependentOfContext("a = *(x + y);");
  3412. verifyIndependentOfContext("a = &(x + y);");
  3413. verifyIndependentOfContext("*(x + y).call();");
  3414. verifyIndependentOfContext("&(x + y)->call();");
  3415. verifyFormat("void f() { &(*I).first; }");
  3416. verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
  3417. verifyFormat(
  3418. "int *MyValues = {\n"
  3419. " *A, // Operator detection might be confused by the '{'\n"
  3420. " *BB // Operator detection might be confused by previous comment\n"
  3421. "};");
  3422. verifyIndependentOfContext("if (int *a = &b)");
  3423. verifyIndependentOfContext("if (int &a = *b)");
  3424. verifyIndependentOfContext("if (a & b[i])");
  3425. verifyIndependentOfContext("if (a::b::c::d & b[i])");
  3426. verifyIndependentOfContext("if (*b[i])");
  3427. verifyIndependentOfContext("if (int *a = (&b))");
  3428. verifyIndependentOfContext("while (int *a = &b)");
  3429. verifyFormat("void f() {\n"
  3430. " for (const int &v : Values) {\n"
  3431. " }\n"
  3432. "}");
  3433. verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
  3434. verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
  3435. verifyFormat("#define MACRO \\\n"
  3436. " int *i = a * b; \\\n"
  3437. " void f(a *b);",
  3438. getLLVMStyleWithColumns(19));
  3439. verifyIndependentOfContext("A = new SomeType *[Length];");
  3440. verifyIndependentOfContext("A = new SomeType *[Length]();");
  3441. verifyIndependentOfContext("T **t = new T *;");
  3442. verifyIndependentOfContext("T **t = new T *();");
  3443. verifyGoogleFormat("A = new SomeType* [Length]();");
  3444. verifyGoogleFormat("A = new SomeType* [Length];");
  3445. verifyGoogleFormat("T** t = new T*;");
  3446. verifyGoogleFormat("T** t = new T*();");
  3447. FormatStyle PointerLeft = getLLVMStyle();
  3448. PointerLeft.PointerBindsToType = true;
  3449. verifyFormat("delete *x;", PointerLeft);
  3450. }
  3451. TEST_F(FormatTest, UnderstandsAttributes) {
  3452. verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
  3453. }
  3454. TEST_F(FormatTest, UnderstandsEllipsis) {
  3455. verifyFormat("int printf(const char *fmt, ...);");
  3456. verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
  3457. verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
  3458. FormatStyle PointersLeft = getLLVMStyle();
  3459. PointersLeft.PointerBindsToType = true;
  3460. verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
  3461. }
  3462. TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
  3463. EXPECT_EQ("int *a;\n"
  3464. "int *a;\n"
  3465. "int *a;",
  3466. format("int *a;\n"
  3467. "int* a;\n"
  3468. "int *a;",
  3469. getGoogleStyle()));
  3470. EXPECT_EQ("int* a;\n"
  3471. "int* a;\n"
  3472. "int* a;",
  3473. format("int* a;\n"
  3474. "int* a;\n"
  3475. "int *a;",
  3476. getGoogleStyle()));
  3477. EXPECT_EQ("int *a;\n"
  3478. "int *a;\n"
  3479. "int *a;",
  3480. format("int *a;\n"
  3481. "int * a;\n"
  3482. "int * a;",
  3483. getGoogleStyle()));
  3484. }
  3485. TEST_F(FormatTest, UnderstandsRvalueReferences) {
  3486. verifyFormat("int f(int &&a) {}");
  3487. verifyFormat("int f(int a, char &&b) {}");
  3488. verifyFormat("void f() { int &&a = b; }");
  3489. verifyGoogleFormat("int f(int a, char&& b) {}");
  3490. verifyGoogleFormat("void f() { int&& a = b; }");
  3491. verifyIndependentOfContext("A<int &&> a;");
  3492. verifyIndependentOfContext("A<int &&, int &&> a;");
  3493. verifyGoogleFormat("A<int&&> a;");
  3494. verifyGoogleFormat("A<int&&, int&&> a;");
  3495. // Not rvalue references:
  3496. verifyFormat("template <bool B, bool C> class A {\n"
  3497. " static_assert(B && C, \"Something is wrong\");\n"
  3498. "};");
  3499. verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
  3500. verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
  3501. }
  3502. TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
  3503. verifyFormat("void f() {\n"
  3504. " x[aaaaaaaaa -\n"
  3505. " b] = 23;\n"
  3506. "}",
  3507. getLLVMStyleWithColumns(15));
  3508. }
  3509. TEST_F(FormatTest, FormatsCasts) {
  3510. verifyFormat("Type *A = static_cast<Type *>(P);");
  3511. verifyFormat("Type *A = (Type *)P;");
  3512. verifyFormat("Type *A = (vector<Type *, int *>)P;");
  3513. verifyFormat("int a = (int)(2.0f);");
  3514. verifyFormat("int a = (int)2.0f;");
  3515. verifyFormat("x[(int32)y];");
  3516. verifyFormat("x = (int32)y;");
  3517. verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
  3518. verifyFormat("int a = (int)*b;");
  3519. verifyFormat("int a = (int)2.0f;");
  3520. verifyFormat("int a = (int)~0;");
  3521. verifyFormat("int a = (int)++a;");
  3522. verifyFormat("int a = (int)sizeof(int);");
  3523. verifyFormat("int a = (int)+2;");
  3524. verifyFormat("my_int a = (my_int)2.0f;");
  3525. verifyFormat("my_int a = (my_int)sizeof(int);");
  3526. verifyFormat("return (my_int)aaa;");
  3527. verifyFormat("#define x ((int)-1)");
  3528. verifyFormat("#define p(q) ((int *)&q)");
  3529. // FIXME: Without type knowledge, this can still fall apart miserably.
  3530. verifyFormat("void f() { my_int a = (my_int) * b; }");
  3531. verifyFormat("void f() { return P ? (my_int) * P : (my_int)0; }");
  3532. verifyFormat("my_int a = (my_int) ~0;");
  3533. verifyFormat("my_int a = (my_int)++ a;");
  3534. verifyFormat("my_int a = (my_int) + 2;");
  3535. // Don't break after a cast's
  3536. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3537. " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
  3538. " bbbbbbbbbbbbbbbbbbbbbb);");
  3539. // These are not casts.
  3540. verifyFormat("void f(int *) {}");
  3541. verifyFormat("f(foo)->b;");
  3542. verifyFormat("f(foo).b;");
  3543. verifyFormat("f(foo)(b);");
  3544. verifyFormat("f(foo)[b];");
  3545. verifyFormat("[](foo) { return 4; }(bar)];");
  3546. verifyFormat("(*funptr)(foo)[4];");
  3547. verifyFormat("funptrs[4](foo)[4];");
  3548. verifyFormat("void f(int *);");
  3549. verifyFormat("void f(int *) = 0;");
  3550. verifyFormat("void f(SmallVector<int>) {}");
  3551. verifyFormat("void f(SmallVector<int>);");
  3552. verifyFormat("void f(SmallVector<int>) = 0;");
  3553. verifyFormat("void f(int i = (kValue) * kMask) {}");
  3554. verifyFormat("void f(int i = (kA * kB) & kMask) {}");
  3555. verifyFormat("int a = sizeof(int) * b;");
  3556. verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
  3557. verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
  3558. verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
  3559. verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
  3560. // These are not casts, but at some point were confused with casts.
  3561. verifyFormat("virtual void foo(int *) override;");
  3562. verifyFormat("virtual void foo(char &) const;");
  3563. verifyFormat("virtual void foo(int *a, char *) const;");
  3564. verifyFormat("int a = sizeof(int *) + b;");
  3565. verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
  3566. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
  3567. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  3568. // FIXME: The indentation here is not ideal.
  3569. verifyFormat(
  3570. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3571. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
  3572. " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
  3573. }
  3574. TEST_F(FormatTest, FormatsFunctionTypes) {
  3575. verifyFormat("A<bool()> a;");
  3576. verifyFormat("A<SomeType()> a;");
  3577. verifyFormat("A<void (*)(int, std::string)> a;");
  3578. verifyFormat("A<void *(int)>;");
  3579. verifyFormat("void *(*a)(int *, SomeType *);");
  3580. verifyFormat("int (*func)(void *);");
  3581. verifyFormat("void f() { int (*func)(void *); }");
  3582. verifyFormat("template <class CallbackClass>\n"
  3583. "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
  3584. verifyGoogleFormat("A<void*(int*, SomeType*)>;");
  3585. verifyGoogleFormat("void* (*a)(int);");
  3586. verifyGoogleFormat(
  3587. "template <class CallbackClass>\n"
  3588. "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
  3589. // Other constructs can look somewhat like function types:
  3590. verifyFormat("A<sizeof(*x)> a;");
  3591. verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
  3592. verifyFormat("some_var = function(*some_pointer_var)[0];");
  3593. verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
  3594. }
  3595. TEST_F(FormatTest, BreaksLongDeclarations) {
  3596. verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
  3597. " AnotherNameForTheLongType;",
  3598. getGoogleStyle());
  3599. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  3600. " LoooooooooooooooooooooooooooooooooooooooongVariable;",
  3601. getGoogleStyle());
  3602. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
  3603. " LoooooooooooooooooooooooooooooooooooooooongVariable;",
  3604. getGoogleStyle());
  3605. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  3606. " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
  3607. getGoogleStyle());
  3608. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  3609. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  3610. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  3611. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  3612. // FIXME: Without the comment, this breaks after "(".
  3613. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
  3614. " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
  3615. getGoogleStyle());
  3616. verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
  3617. " int LoooooooooooooooooooongParam2) {}");
  3618. verifyFormat(
  3619. "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
  3620. " SourceLocation L, IdentifierIn *II,\n"
  3621. " Type *T) {}");
  3622. verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
  3623. "ReallyReallyLongFunctionName(\n"
  3624. " const std::string &SomeParameter,\n"
  3625. " const SomeType<string, SomeOtherTemplateParameter> &\n"
  3626. " ReallyReallyLongParameterName,\n"
  3627. " const SomeType<string, SomeOtherTemplateParameter> &\n"
  3628. " AnotherLongParameterName) {}");
  3629. verifyFormat("template <typename A>\n"
  3630. "SomeLoooooooooooooooooooooongType<\n"
  3631. " typename some_namespace::SomeOtherType<A>::Type>\n"
  3632. "Function() {}");
  3633. verifyGoogleFormat(
  3634. "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
  3635. " aaaaaaaaaaaaaaaaaaaaaaa;");
  3636. verifyGoogleFormat(
  3637. "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
  3638. " SourceLocation L) {}");
  3639. verifyGoogleFormat(
  3640. "some_namespace::LongReturnType\n"
  3641. "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
  3642. " int first_long_parameter, int second_parameter) {}");
  3643. verifyGoogleFormat("template <typename T>\n"
  3644. "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  3645. "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
  3646. verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3647. " int aaaaaaaaaaaaaaaaaaaaaaa);");
  3648. }
  3649. TEST_F(FormatTest, FormatsArrays) {
  3650. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  3651. " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
  3652. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3653. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  3654. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3655. " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
  3656. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3657. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  3658. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  3659. verifyFormat(
  3660. "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
  3661. " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  3662. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  3663. }
  3664. TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
  3665. verifyFormat("(a)->b();");
  3666. verifyFormat("--a;");
  3667. }
  3668. TEST_F(FormatTest, HandlesIncludeDirectives) {
  3669. verifyFormat("#include <string>\n"
  3670. "#include <a/b/c.h>\n"
  3671. "#include \"a/b/string\"\n"
  3672. "#include \"string.h\"\n"
  3673. "#include \"string.h\"\n"
  3674. "#include <a-a>\n"
  3675. "#include < path with space >\n"
  3676. "#include \"abc.h\" // this is included for ABC\n"
  3677. "#include \"some long include\" // with a comment\n"
  3678. "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
  3679. getLLVMStyleWithColumns(35));
  3680. verifyFormat("#import <string>");
  3681. verifyFormat("#import <a/b/c.h>");
  3682. verifyFormat("#import \"a/b/string\"");
  3683. verifyFormat("#import \"string.h\"");
  3684. verifyFormat("#import \"string.h\"");
  3685. verifyFormat("#if __has_include(<strstream>)\n"
  3686. "#include <strstream>\n"
  3687. "#endif");
  3688. }
  3689. //===----------------------------------------------------------------------===//
  3690. // Error recovery tests.
  3691. //===----------------------------------------------------------------------===//
  3692. TEST_F(FormatTest, IncompleteParameterLists) {
  3693. FormatStyle NoBinPacking = getLLVMStyle();
  3694. NoBinPacking.BinPackParameters = false;
  3695. verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
  3696. " double *min_x,\n"
  3697. " double *max_x,\n"
  3698. " double *min_y,\n"
  3699. " double *max_y,\n"
  3700. " double *min_z,\n"
  3701. " double *max_z, ) {}",
  3702. NoBinPacking);
  3703. }
  3704. TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
  3705. verifyFormat("void f() { return; }\n42");
  3706. verifyFormat("void f() {\n"
  3707. " if (0)\n"
  3708. " return;\n"
  3709. "}\n"
  3710. "42");
  3711. verifyFormat("void f() { return }\n42");
  3712. verifyFormat("void f() {\n"
  3713. " if (0)\n"
  3714. " return\n"
  3715. "}\n"
  3716. "42");
  3717. }
  3718. TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
  3719. EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
  3720. EXPECT_EQ("void f() {\n"
  3721. " if (a)\n"
  3722. " return\n"
  3723. "}",
  3724. format("void f ( ) { if ( a ) return }"));
  3725. EXPECT_EQ("namespace N {\n"
  3726. "void f()\n"
  3727. "}",
  3728. format("namespace N { void f() }"));
  3729. EXPECT_EQ("namespace N {\n"
  3730. "void f() {}\n"
  3731. "void g()\n"
  3732. "}",
  3733. format("namespace N { void f( ) { } void g( ) }"));
  3734. }
  3735. TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
  3736. verifyFormat("int aaaaaaaa =\n"
  3737. " // Overlylongcomment\n"
  3738. " b;",
  3739. getLLVMStyleWithColumns(20));
  3740. verifyFormat("function(\n"
  3741. " ShortArgument,\n"
  3742. " LoooooooooooongArgument);\n",
  3743. getLLVMStyleWithColumns(20));
  3744. }
  3745. TEST_F(FormatTest, IncorrectAccessSpecifier) {
  3746. verifyFormat("public:");
  3747. verifyFormat("class A {\n"
  3748. "public\n"
  3749. " void f() {}\n"
  3750. "};");
  3751. verifyFormat("public\n"
  3752. "int qwerty;");
  3753. verifyFormat("public\n"
  3754. "B {}");
  3755. verifyFormat("public\n"
  3756. "{}");
  3757. verifyFormat("public\n"
  3758. "B { int x; }");
  3759. }
  3760. TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
  3761. verifyFormat("{");
  3762. verifyFormat("#})");
  3763. }
  3764. TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
  3765. verifyFormat("do {\n}");
  3766. verifyFormat("do {\n}\n"
  3767. "f();");
  3768. verifyFormat("do {\n}\n"
  3769. "wheeee(fun);");
  3770. verifyFormat("do {\n"
  3771. " f();\n"
  3772. "}");
  3773. }
  3774. TEST_F(FormatTest, IncorrectCodeMissingParens) {
  3775. verifyFormat("if {\n foo;\n foo();\n}");
  3776. verifyFormat("switch {\n foo;\n foo();\n}");
  3777. verifyFormat("for {\n foo;\n foo();\n}");
  3778. verifyFormat("while {\n foo;\n foo();\n}");
  3779. verifyFormat("do {\n foo;\n foo();\n} while;");
  3780. }
  3781. TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
  3782. verifyFormat("namespace {\n"
  3783. "class Foo { Foo (\n"
  3784. "};\n"
  3785. "} // comment");
  3786. }
  3787. TEST_F(FormatTest, IncorrectCodeErrorDetection) {
  3788. EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
  3789. EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
  3790. EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
  3791. EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
  3792. EXPECT_EQ("{\n"
  3793. " {\n"
  3794. " breakme(\n"
  3795. " qwe);\n"
  3796. "}\n",
  3797. format("{\n"
  3798. " {\n"
  3799. " breakme(qwe);\n"
  3800. "}\n",
  3801. getLLVMStyleWithColumns(10)));
  3802. }
  3803. TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
  3804. verifyFormat("int x = {\n"
  3805. " avariable,\n"
  3806. " b(alongervariable)\n"
  3807. "};",
  3808. getLLVMStyleWithColumns(25));
  3809. }
  3810. TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
  3811. verifyFormat("return (a)(b) { 1, 2, 3 };");
  3812. }
  3813. TEST_F(FormatTest, LayoutCxx11ConstructorBraceInitializers) {
  3814. verifyFormat("vector<int> x{ 1, 2, 3, 4 };");
  3815. verifyFormat("vector<T> x{ {}, {}, {}, {} };");
  3816. verifyFormat("f({ 1, 2 });");
  3817. verifyFormat("auto v = Foo{ 1 };");
  3818. verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });");
  3819. verifyFormat("Class::Class : member{ 1, 2, 3 } {}");
  3820. verifyFormat("new vector<int>{ 1, 2, 3 };");
  3821. verifyFormat("new int[3]{ 1, 2, 3 };");
  3822. verifyFormat("return { arg1, arg2 };");
  3823. verifyFormat("return { arg1, SomeType{ parameter } };");
  3824. verifyFormat("new T{ arg1, arg2 };");
  3825. verifyFormat("class Class {\n"
  3826. " T member = { arg1, arg2 };\n"
  3827. "};");
  3828. verifyFormat(
  3829. "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3830. " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
  3831. " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  3832. " bbbbbbbbbbbbbbbbbbbb, bbbbb };");
  3833. verifyFormat("DoSomethingWithVector({} /* No data */);");
  3834. verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });");
  3835. verifyFormat(
  3836. "someFunction(OtherParam, BracedList{\n"
  3837. " // comment 1 (Forcing interesting break)\n"
  3838. " param1, param2,\n"
  3839. " // comment 2\n"
  3840. " param3, param4\n"
  3841. " });");
  3842. verifyFormat(
  3843. "std::this_thread::sleep_for(\n"
  3844. " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);");
  3845. FormatStyle NoSpaces = getLLVMStyle();
  3846. NoSpaces.Cpp11BracedListStyle = true;
  3847. verifyFormat("vector<int> x{1, 2, 3, 4};", NoSpaces);
  3848. verifyFormat("vector<T> x{{}, {}, {}, {}};", NoSpaces);
  3849. verifyFormat("f({1, 2});", NoSpaces);
  3850. verifyFormat("auto v = Foo{-1};", NoSpaces);
  3851. verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});", NoSpaces);
  3852. verifyFormat("Class::Class : member{1, 2, 3} {}", NoSpaces);
  3853. verifyFormat("new vector<int>{1, 2, 3};", NoSpaces);
  3854. verifyFormat("new int[3]{1, 2, 3};", NoSpaces);
  3855. verifyFormat("return {arg1, arg2};", NoSpaces);
  3856. verifyFormat("return {arg1, SomeType{parameter}};", NoSpaces);
  3857. verifyFormat("new T{arg1, arg2};", NoSpaces);
  3858. verifyFormat("class Class {\n"
  3859. " T member = {arg1, arg2};\n"
  3860. "};",
  3861. NoSpaces);
  3862. }
  3863. TEST_F(FormatTest, FormatsBracedListsinColumnLayout) {
  3864. verifyFormat("vector<int> x = { 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  3865. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  3866. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  3867. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  3868. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  3869. " 1, 22, 333, 4444, 55555, 666666, 7777777 };");
  3870. verifyFormat("vector<int> x = { 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  3871. " // line comment\n"
  3872. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  3873. " 1, 22, 333, 4444, 55555,\n"
  3874. " // line comment\n"
  3875. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  3876. " 1, 22, 333, 4444, 55555, 666666, 7777777 };");
  3877. verifyFormat(
  3878. "vector<int> x = { 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  3879. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  3880. " 1, 22, 333, 4444, 55555, 666666, // comment\n"
  3881. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  3882. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  3883. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  3884. " 7777777 };");
  3885. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  3886. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  3887. " X86::R8, X86::R9, X86::R10, X86::R11, 0\n"
  3888. "};");
  3889. verifyFormat("vector<int> x = { 1, 1, 1, 1,\n"
  3890. " 1, 1, 1, 1 };",
  3891. getLLVMStyleWithColumns(39));
  3892. verifyFormat("vector<int> x = { 1, 1, 1, 1,\n"
  3893. " 1, 1, 1, 1 };",
  3894. getLLVMStyleWithColumns(38));
  3895. verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
  3896. " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1\n"
  3897. "};",
  3898. getLLVMStyleWithColumns(40));
  3899. // Trailing commas.
  3900. verifyFormat("vector<int> x = { 1, 1, 1, 1,\n"
  3901. " 1, 1, 1, 1, };",
  3902. getLLVMStyleWithColumns(39));
  3903. verifyFormat("vector<int> x = {\n"
  3904. " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
  3905. "};",
  3906. getLLVMStyleWithColumns(39));
  3907. verifyFormat("vector<int> x = { 1, 1, 1, 1,\n"
  3908. " 1, 1, 1, 1,\n"
  3909. " /**/ /**/ };",
  3910. getLLVMStyleWithColumns(39));
  3911. verifyFormat("return { { aaaaaaaaaaaaaaaaaaaaa }, { aaaaaaaaaaaaaaaaaaa },\n"
  3912. " { aaaaaaaaaaaaaaaaaaaaa }, { aaaaaaaaaaaaaaaaa } };",
  3913. getLLVMStyleWithColumns(60));
  3914. }
  3915. TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
  3916. verifyFormat("void f() { return 42; }");
  3917. verifyFormat("void f() {\n"
  3918. " // Comment\n"
  3919. "}");
  3920. verifyFormat("{\n"
  3921. "#error {\n"
  3922. " int a;\n"
  3923. "}");
  3924. verifyFormat("{\n"
  3925. " int a;\n"
  3926. "#error {\n"
  3927. "}");
  3928. verifyFormat("void f() {} // comment");
  3929. verifyFormat("void f() { int a; } // comment");
  3930. verifyFormat("void f() {\n"
  3931. "} // comment",
  3932. getLLVMStyleWithColumns(15));
  3933. verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
  3934. verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
  3935. verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
  3936. verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
  3937. }
  3938. TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
  3939. // Elaborate type variable declarations.
  3940. verifyFormat("struct foo a = { bar };\nint n;");
  3941. verifyFormat("class foo a = { bar };\nint n;");
  3942. verifyFormat("union foo a = { bar };\nint n;");
  3943. // Elaborate types inside function definitions.
  3944. verifyFormat("struct foo f() {}\nint n;");
  3945. verifyFormat("class foo f() {}\nint n;");
  3946. verifyFormat("union foo f() {}\nint n;");
  3947. // Templates.
  3948. verifyFormat("template <class X> void f() {}\nint n;");
  3949. verifyFormat("template <struct X> void f() {}\nint n;");
  3950. verifyFormat("template <union X> void f() {}\nint n;");
  3951. // Actual definitions...
  3952. verifyFormat("struct {\n} n;");
  3953. verifyFormat(
  3954. "template <template <class T, class Y>, class Z> class X {\n} n;");
  3955. verifyFormat("union Z {\n int n;\n} x;");
  3956. verifyFormat("class MACRO Z {\n} n;");
  3957. verifyFormat("class MACRO(X) Z {\n} n;");
  3958. verifyFormat("class __attribute__(X) Z {\n} n;");
  3959. verifyFormat("class __declspec(X) Z {\n} n;");
  3960. verifyFormat("class A##B##C {\n} n;");
  3961. // Redefinition from nested context:
  3962. verifyFormat("class A::B::C {\n} n;");
  3963. // Template definitions.
  3964. verifyFormat(
  3965. "template <typename F>\n"
  3966. "Matcher(const Matcher<F> &Other,\n"
  3967. " typename enable_if_c<is_base_of<F, T>::value &&\n"
  3968. " !is_same<F, T>::value>::type * = 0)\n"
  3969. " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
  3970. // FIXME: This is still incorrectly handled at the formatter side.
  3971. verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
  3972. // FIXME:
  3973. // This now gets parsed incorrectly as class definition.
  3974. // verifyFormat("class A<int> f() {\n}\nint n;");
  3975. // Elaborate types where incorrectly parsing the structural element would
  3976. // break the indent.
  3977. verifyFormat("if (true)\n"
  3978. " class X x;\n"
  3979. "else\n"
  3980. " f();\n");
  3981. // This is simply incomplete. Formatting is not important, but must not crash.
  3982. verifyFormat("class A:");
  3983. }
  3984. TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
  3985. verifyFormat("#error Leave all white!!!!! space* alone!\n");
  3986. verifyFormat("#warning Leave all white!!!!! space* alone!\n");
  3987. EXPECT_EQ("#error 1", format(" # error 1"));
  3988. EXPECT_EQ("#warning 1", format(" # warning 1"));
  3989. }
  3990. TEST_F(FormatTest, FormatHashIfExpressions) {
  3991. // FIXME: Come up with a better indentation for #elif.
  3992. verifyFormat(
  3993. "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
  3994. " defined(BBBBBBBB)\n"
  3995. "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
  3996. " defined(BBBBBBBB)\n"
  3997. "#endif",
  3998. getLLVMStyleWithColumns(65));
  3999. }
  4000. TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
  4001. FormatStyle AllowsMergedIf = getGoogleStyle();
  4002. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  4003. verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
  4004. verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
  4005. verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
  4006. EXPECT_EQ("if (true) return 42;",
  4007. format("if (true)\nreturn 42;", AllowsMergedIf));
  4008. FormatStyle ShortMergedIf = AllowsMergedIf;
  4009. ShortMergedIf.ColumnLimit = 25;
  4010. verifyFormat("#define A \\\n"
  4011. " if (true) return 42;",
  4012. ShortMergedIf);
  4013. verifyFormat("#define A \\\n"
  4014. " f(); \\\n"
  4015. " if (true)\n"
  4016. "#define B",
  4017. ShortMergedIf);
  4018. verifyFormat("#define A \\\n"
  4019. " f(); \\\n"
  4020. " if (true)\n"
  4021. "g();",
  4022. ShortMergedIf);
  4023. verifyFormat("{\n"
  4024. "#ifdef A\n"
  4025. " // Comment\n"
  4026. " if (true) continue;\n"
  4027. "#endif\n"
  4028. " // Comment\n"
  4029. " if (true) continue;",
  4030. ShortMergedIf);
  4031. }
  4032. TEST_F(FormatTest, BlockCommentsInControlLoops) {
  4033. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  4034. " f();\n"
  4035. "}");
  4036. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  4037. " f();\n"
  4038. "} /* another comment */ else /* comment #3 */ {\n"
  4039. " g();\n"
  4040. "}");
  4041. verifyFormat("while (0) /* a comment in a strange place */ {\n"
  4042. " f();\n"
  4043. "}");
  4044. verifyFormat("for (;;) /* a comment in a strange place */ {\n"
  4045. " f();\n"
  4046. "}");
  4047. verifyFormat("do /* a comment in a strange place */ {\n"
  4048. " f();\n"
  4049. "} /* another comment */ while (0);");
  4050. }
  4051. TEST_F(FormatTest, BlockComments) {
  4052. EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
  4053. format("/* *//* */ /* */\n/* *//* */ /* */"));
  4054. EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
  4055. EXPECT_EQ("#define A /*123*/ \\\n"
  4056. " b\n"
  4057. "/* */\n"
  4058. "someCall(\n"
  4059. " parameter);",
  4060. format("#define A /*123*/ b\n"
  4061. "/* */\n"
  4062. "someCall(parameter);",
  4063. getLLVMStyleWithColumns(15)));
  4064. EXPECT_EQ("#define A\n"
  4065. "/* */ someCall(\n"
  4066. " parameter);",
  4067. format("#define A\n"
  4068. "/* */someCall(parameter);",
  4069. getLLVMStyleWithColumns(15)));
  4070. EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
  4071. EXPECT_EQ("/*\n"
  4072. "*\n"
  4073. " * aaaaaa\n"
  4074. "*aaaaaa\n"
  4075. "*/",
  4076. format("/*\n"
  4077. "*\n"
  4078. " * aaaaaa aaaaaa\n"
  4079. "*/",
  4080. getLLVMStyleWithColumns(10)));
  4081. EXPECT_EQ("/*\n"
  4082. "**\n"
  4083. "* aaaaaa\n"
  4084. "*aaaaaa\n"
  4085. "*/",
  4086. format("/*\n"
  4087. "**\n"
  4088. "* aaaaaa aaaaaa\n"
  4089. "*/",
  4090. getLLVMStyleWithColumns(10)));
  4091. FormatStyle NoBinPacking = getLLVMStyle();
  4092. NoBinPacking.BinPackParameters = false;
  4093. EXPECT_EQ("someFunction(1, /* comment 1 */\n"
  4094. " 2, /* comment 2 */\n"
  4095. " 3, /* comment 3 */\n"
  4096. " aaaa,\n"
  4097. " bbbb);",
  4098. format("someFunction (1, /* comment 1 */\n"
  4099. " 2, /* comment 2 */ \n"
  4100. " 3, /* comment 3 */\n"
  4101. "aaaa, bbbb );",
  4102. NoBinPacking));
  4103. verifyFormat(
  4104. "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  4105. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4106. EXPECT_EQ(
  4107. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  4108. " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  4109. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4110. format(
  4111. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  4112. " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  4113. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
  4114. EXPECT_EQ(
  4115. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  4116. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  4117. "int cccccccccccccccccccccccccccccc; /* comment */\n",
  4118. format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  4119. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  4120. "int cccccccccccccccccccccccccccccc; /* comment */\n"));
  4121. verifyFormat("void f(int * /* unused */) {}");
  4122. EXPECT_EQ("/*\n"
  4123. " **\n"
  4124. " */",
  4125. format("/*\n"
  4126. " **\n"
  4127. " */"));
  4128. EXPECT_EQ("/*\n"
  4129. " *q\n"
  4130. " */",
  4131. format("/*\n"
  4132. " *q\n"
  4133. " */"));
  4134. EXPECT_EQ("/*\n"
  4135. " * q\n"
  4136. " */",
  4137. format("/*\n"
  4138. " * q\n"
  4139. " */"));
  4140. EXPECT_EQ("/*\n"
  4141. " **/",
  4142. format("/*\n"
  4143. " **/"));
  4144. EXPECT_EQ("/*\n"
  4145. " ***/",
  4146. format("/*\n"
  4147. " ***/"));
  4148. }
  4149. TEST_F(FormatTest, BlockCommentsInMacros) {
  4150. EXPECT_EQ("#define A \\\n"
  4151. " { \\\n"
  4152. " /* one line */ \\\n"
  4153. " someCall();",
  4154. format("#define A { \\\n"
  4155. " /* one line */ \\\n"
  4156. " someCall();",
  4157. getLLVMStyleWithColumns(20)));
  4158. EXPECT_EQ("#define A \\\n"
  4159. " { \\\n"
  4160. " /* previous */ \\\n"
  4161. " /* one line */ \\\n"
  4162. " someCall();",
  4163. format("#define A { \\\n"
  4164. " /* previous */ \\\n"
  4165. " /* one line */ \\\n"
  4166. " someCall();",
  4167. getLLVMStyleWithColumns(20)));
  4168. }
  4169. TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
  4170. EXPECT_EQ("a = {\n"
  4171. " 1111 /* */\n"
  4172. "};",
  4173. format("a = {1111 /* */\n"
  4174. "};",
  4175. getLLVMStyleWithColumns(15)));
  4176. EXPECT_EQ("a = {\n"
  4177. " 1111 /* */\n"
  4178. "};",
  4179. format("a = {1111 /* */\n"
  4180. "};",
  4181. getLLVMStyleWithColumns(15)));
  4182. // FIXME: The formatting is still wrong here.
  4183. EXPECT_EQ("a = {\n"
  4184. " 1111 /* a\n"
  4185. " */\n"
  4186. "};",
  4187. format("a = {1111 /* a */\n"
  4188. "};",
  4189. getLLVMStyleWithColumns(15)));
  4190. }
  4191. TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
  4192. // FIXME: This is not what we want...
  4193. verifyFormat("{\n"
  4194. "// a"
  4195. "// b");
  4196. }
  4197. TEST_F(FormatTest, FormatStarDependingOnContext) {
  4198. verifyFormat("void f(int *a);");
  4199. verifyFormat("void f() { f(fint * b); }");
  4200. verifyFormat("class A {\n void f(int *a);\n};");
  4201. verifyFormat("class A {\n int *a;\n};");
  4202. verifyFormat("namespace a {\n"
  4203. "namespace b {\n"
  4204. "class A {\n"
  4205. " void f() {}\n"
  4206. " int *a;\n"
  4207. "};\n"
  4208. "}\n"
  4209. "}");
  4210. }
  4211. TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
  4212. verifyFormat("while");
  4213. verifyFormat("operator");
  4214. }
  4215. //===----------------------------------------------------------------------===//
  4216. // Objective-C tests.
  4217. //===----------------------------------------------------------------------===//
  4218. TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
  4219. verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
  4220. EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
  4221. format("-(NSUInteger)indexOfObject:(id)anObject;"));
  4222. EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
  4223. EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
  4224. EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
  4225. format("-(NSInteger)Method3:(id)anObject;"));
  4226. EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
  4227. format("-(NSInteger)Method4:(id)anObject;"));
  4228. EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
  4229. format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
  4230. EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
  4231. format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
  4232. EXPECT_EQ(
  4233. "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
  4234. format(
  4235. "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
  4236. // Very long objectiveC method declaration.
  4237. verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
  4238. " inRange:(NSRange)range\n"
  4239. " outRange:(NSRange)out_range\n"
  4240. " outRange1:(NSRange)out_range1\n"
  4241. " outRange2:(NSRange)out_range2\n"
  4242. " outRange3:(NSRange)out_range3\n"
  4243. " outRange4:(NSRange)out_range4\n"
  4244. " outRange5:(NSRange)out_range5\n"
  4245. " outRange6:(NSRange)out_range6\n"
  4246. " outRange7:(NSRange)out_range7\n"
  4247. " outRange8:(NSRange)out_range8\n"
  4248. " outRange9:(NSRange)out_range9;");
  4249. verifyFormat("- (int)sum:(vector<int>)numbers;");
  4250. verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
  4251. // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
  4252. // protocol lists (but not for template classes):
  4253. //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
  4254. verifyFormat("- (int (*)())foo:(int (*)())f;");
  4255. verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
  4256. // If there's no return type (very rare in practice!), LLVM and Google style
  4257. // agree.
  4258. verifyFormat("- foo;");
  4259. verifyFormat("- foo:(int)f;");
  4260. verifyGoogleFormat("- foo:(int)foo;");
  4261. }
  4262. TEST_F(FormatTest, FormatObjCBlocks) {
  4263. verifyFormat("int (^Block)(int, int);");
  4264. verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
  4265. }
  4266. TEST_F(FormatTest, FormatObjCInterface) {
  4267. verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
  4268. "@public\n"
  4269. " int field1;\n"
  4270. "@protected\n"
  4271. " int field2;\n"
  4272. "@private\n"
  4273. " int field3;\n"
  4274. "@package\n"
  4275. " int field4;\n"
  4276. "}\n"
  4277. "+ (id)init;\n"
  4278. "@end");
  4279. verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
  4280. " @public\n"
  4281. " int field1;\n"
  4282. " @protected\n"
  4283. " int field2;\n"
  4284. " @private\n"
  4285. " int field3;\n"
  4286. " @package\n"
  4287. " int field4;\n"
  4288. "}\n"
  4289. "+ (id)init;\n"
  4290. "@end");
  4291. verifyFormat("@interface /* wait for it */ Foo\n"
  4292. "+ (id)init;\n"
  4293. "// Look, a comment!\n"
  4294. "- (int)answerWith:(int)i;\n"
  4295. "@end");
  4296. verifyFormat("@interface Foo\n"
  4297. "@end\n"
  4298. "@interface Bar\n"
  4299. "@end");
  4300. verifyFormat("@interface Foo : Bar\n"
  4301. "+ (id)init;\n"
  4302. "@end");
  4303. verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
  4304. "+ (id)init;\n"
  4305. "@end");
  4306. verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
  4307. "+ (id)init;\n"
  4308. "@end");
  4309. verifyFormat("@interface Foo (HackStuff)\n"
  4310. "+ (id)init;\n"
  4311. "@end");
  4312. verifyFormat("@interface Foo ()\n"
  4313. "+ (id)init;\n"
  4314. "@end");
  4315. verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
  4316. "+ (id)init;\n"
  4317. "@end");
  4318. verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
  4319. "+ (id)init;\n"
  4320. "@end");
  4321. verifyFormat("@interface Foo {\n"
  4322. " int _i;\n"
  4323. "}\n"
  4324. "+ (id)init;\n"
  4325. "@end");
  4326. verifyFormat("@interface Foo : Bar {\n"
  4327. " int _i;\n"
  4328. "}\n"
  4329. "+ (id)init;\n"
  4330. "@end");
  4331. verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
  4332. " int _i;\n"
  4333. "}\n"
  4334. "+ (id)init;\n"
  4335. "@end");
  4336. verifyFormat("@interface Foo (HackStuff) {\n"
  4337. " int _i;\n"
  4338. "}\n"
  4339. "+ (id)init;\n"
  4340. "@end");
  4341. verifyFormat("@interface Foo () {\n"
  4342. " int _i;\n"
  4343. "}\n"
  4344. "+ (id)init;\n"
  4345. "@end");
  4346. verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
  4347. " int _i;\n"
  4348. "}\n"
  4349. "+ (id)init;\n"
  4350. "@end");
  4351. }
  4352. TEST_F(FormatTest, FormatObjCImplementation) {
  4353. verifyFormat("@implementation Foo : NSObject {\n"
  4354. "@public\n"
  4355. " int field1;\n"
  4356. "@protected\n"
  4357. " int field2;\n"
  4358. "@private\n"
  4359. " int field3;\n"
  4360. "@package\n"
  4361. " int field4;\n"
  4362. "}\n"
  4363. "+ (id)init {\n}\n"
  4364. "@end");
  4365. verifyGoogleFormat("@implementation Foo : NSObject {\n"
  4366. " @public\n"
  4367. " int field1;\n"
  4368. " @protected\n"
  4369. " int field2;\n"
  4370. " @private\n"
  4371. " int field3;\n"
  4372. " @package\n"
  4373. " int field4;\n"
  4374. "}\n"
  4375. "+ (id)init {\n}\n"
  4376. "@end");
  4377. verifyFormat("@implementation Foo\n"
  4378. "+ (id)init {\n"
  4379. " if (true)\n"
  4380. " return nil;\n"
  4381. "}\n"
  4382. "// Look, a comment!\n"
  4383. "- (int)answerWith:(int)i {\n"
  4384. " return i;\n"
  4385. "}\n"
  4386. "+ (int)answerWith:(int)i {\n"
  4387. " return i;\n"
  4388. "}\n"
  4389. "@end");
  4390. verifyFormat("@implementation Foo\n"
  4391. "@end\n"
  4392. "@implementation Bar\n"
  4393. "@end");
  4394. verifyFormat("@implementation Foo : Bar\n"
  4395. "+ (id)init {\n}\n"
  4396. "- (void)foo {\n}\n"
  4397. "@end");
  4398. verifyFormat("@implementation Foo {\n"
  4399. " int _i;\n"
  4400. "}\n"
  4401. "+ (id)init {\n}\n"
  4402. "@end");
  4403. verifyFormat("@implementation Foo : Bar {\n"
  4404. " int _i;\n"
  4405. "}\n"
  4406. "+ (id)init {\n}\n"
  4407. "@end");
  4408. verifyFormat("@implementation Foo (HackStuff)\n"
  4409. "+ (id)init {\n}\n"
  4410. "@end");
  4411. verifyFormat("@implementation ObjcClass\n"
  4412. "- (void)method;\n"
  4413. "{}\n"
  4414. "@end");
  4415. }
  4416. TEST_F(FormatTest, FormatObjCProtocol) {
  4417. verifyFormat("@protocol Foo\n"
  4418. "@property(weak) id delegate;\n"
  4419. "- (NSUInteger)numberOfThings;\n"
  4420. "@end");
  4421. verifyFormat("@protocol MyProtocol <NSObject>\n"
  4422. "- (NSUInteger)numberOfThings;\n"
  4423. "@end");
  4424. verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
  4425. "- (NSUInteger)numberOfThings;\n"
  4426. "@end");
  4427. verifyFormat("@protocol Foo;\n"
  4428. "@protocol Bar;\n");
  4429. verifyFormat("@protocol Foo\n"
  4430. "@end\n"
  4431. "@protocol Bar\n"
  4432. "@end");
  4433. verifyFormat("@protocol myProtocol\n"
  4434. "- (void)mandatoryWithInt:(int)i;\n"
  4435. "@optional\n"
  4436. "- (void)optional;\n"
  4437. "@required\n"
  4438. "- (void)required;\n"
  4439. "@optional\n"
  4440. "@property(assign) int madProp;\n"
  4441. "@end\n");
  4442. verifyFormat("@property(nonatomic, assign, readonly)\n"
  4443. " int *looooooooooooooooooooooooooooongNumber;\n"
  4444. "@property(nonatomic, assign, readonly)\n"
  4445. " NSString *looooooooooooooooooooooooooooongName;");
  4446. }
  4447. TEST_F(FormatTest, FormatObjCMethodDeclarations) {
  4448. verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
  4449. " rect:(NSRect)theRect\n"
  4450. " interval:(float)theInterval {\n"
  4451. "}");
  4452. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  4453. " longKeyword:(NSRect)theRect\n"
  4454. " evenLongerKeyword:(float)theInterval\n"
  4455. " error:(NSError **)theError {\n"
  4456. "}");
  4457. }
  4458. TEST_F(FormatTest, FormatObjCMethodExpr) {
  4459. verifyFormat("[foo bar:baz];");
  4460. verifyFormat("return [foo bar:baz];");
  4461. verifyFormat("f([foo bar:baz]);");
  4462. verifyFormat("f(2, [foo bar:baz]);");
  4463. verifyFormat("f(2, a ? b : c);");
  4464. verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
  4465. // Unary operators.
  4466. verifyFormat("int a = +[foo bar:baz];");
  4467. verifyFormat("int a = -[foo bar:baz];");
  4468. verifyFormat("int a = ![foo bar:baz];");
  4469. verifyFormat("int a = ~[foo bar:baz];");
  4470. verifyFormat("int a = ++[foo bar:baz];");
  4471. verifyFormat("int a = --[foo bar:baz];");
  4472. verifyFormat("int a = sizeof [foo bar:baz];");
  4473. verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
  4474. verifyFormat("int a = &[foo bar:baz];");
  4475. verifyFormat("int a = *[foo bar:baz];");
  4476. // FIXME: Make casts work, without breaking f()[4].
  4477. //verifyFormat("int a = (int)[foo bar:baz];");
  4478. //verifyFormat("return (int)[foo bar:baz];");
  4479. //verifyFormat("(void)[foo bar:baz];");
  4480. verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
  4481. // Binary operators.
  4482. verifyFormat("[foo bar:baz], [foo bar:baz];");
  4483. verifyFormat("[foo bar:baz] = [foo bar:baz];");
  4484. verifyFormat("[foo bar:baz] *= [foo bar:baz];");
  4485. verifyFormat("[foo bar:baz] /= [foo bar:baz];");
  4486. verifyFormat("[foo bar:baz] %= [foo bar:baz];");
  4487. verifyFormat("[foo bar:baz] += [foo bar:baz];");
  4488. verifyFormat("[foo bar:baz] -= [foo bar:baz];");
  4489. verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
  4490. verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
  4491. verifyFormat("[foo bar:baz] &= [foo bar:baz];");
  4492. verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
  4493. verifyFormat("[foo bar:baz] |= [foo bar:baz];");
  4494. verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
  4495. verifyFormat("[foo bar:baz] || [foo bar:baz];");
  4496. verifyFormat("[foo bar:baz] && [foo bar:baz];");
  4497. verifyFormat("[foo bar:baz] | [foo bar:baz];");
  4498. verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
  4499. verifyFormat("[foo bar:baz] & [foo bar:baz];");
  4500. verifyFormat("[foo bar:baz] == [foo bar:baz];");
  4501. verifyFormat("[foo bar:baz] != [foo bar:baz];");
  4502. verifyFormat("[foo bar:baz] >= [foo bar:baz];");
  4503. verifyFormat("[foo bar:baz] <= [foo bar:baz];");
  4504. verifyFormat("[foo bar:baz] > [foo bar:baz];");
  4505. verifyFormat("[foo bar:baz] < [foo bar:baz];");
  4506. verifyFormat("[foo bar:baz] >> [foo bar:baz];");
  4507. verifyFormat("[foo bar:baz] << [foo bar:baz];");
  4508. verifyFormat("[foo bar:baz] - [foo bar:baz];");
  4509. verifyFormat("[foo bar:baz] + [foo bar:baz];");
  4510. verifyFormat("[foo bar:baz] * [foo bar:baz];");
  4511. verifyFormat("[foo bar:baz] / [foo bar:baz];");
  4512. verifyFormat("[foo bar:baz] % [foo bar:baz];");
  4513. // Whew!
  4514. verifyFormat("return in[42];");
  4515. verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
  4516. "}");
  4517. verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
  4518. verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
  4519. verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
  4520. verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
  4521. verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
  4522. verifyFormat("[button setAction:@selector(zoomOut:)];");
  4523. verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
  4524. verifyFormat("arr[[self indexForFoo:a]];");
  4525. verifyFormat("throw [self errorFor:a];");
  4526. verifyFormat("@throw [self errorFor:a];");
  4527. verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
  4528. verifyFormat("[(id)foo bar:(id) ? baz : quux];");
  4529. verifyFormat("4 > 4 ? (id)a : (id)baz;");
  4530. // This tests that the formatter doesn't break after "backing" but before ":",
  4531. // which would be at 80 columns.
  4532. verifyFormat(
  4533. "void f() {\n"
  4534. " if ((self = [super initWithContentRect:contentRect\n"
  4535. " styleMask:styleMask ?: otherMask\n"
  4536. " backing:NSBackingStoreBuffered\n"
  4537. " defer:YES]))");
  4538. verifyFormat(
  4539. "[foo checkThatBreakingAfterColonWorksOk:\n"
  4540. " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
  4541. verifyFormat("[myObj short:arg1 // Force line break\n"
  4542. " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
  4543. " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
  4544. " error:arg4];");
  4545. verifyFormat(
  4546. "void f() {\n"
  4547. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  4548. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  4549. " pos.width(), pos.height())\n"
  4550. " styleMask:NSBorderlessWindowMask\n"
  4551. " backing:NSBackingStoreBuffered\n"
  4552. " defer:NO]);\n"
  4553. "}");
  4554. verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
  4555. " with:contentsNativeView];");
  4556. verifyFormat(
  4557. "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
  4558. " owner:nillllll];");
  4559. verifyFormat(
  4560. "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
  4561. " forType:kBookmarkButtonDragType];");
  4562. verifyFormat("[defaultCenter addObserver:self\n"
  4563. " selector:@selector(willEnterFullscreen)\n"
  4564. " name:kWillEnterFullscreenNotification\n"
  4565. " object:nil];");
  4566. verifyFormat("[image_rep drawInRect:drawRect\n"
  4567. " fromRect:NSZeroRect\n"
  4568. " operation:NSCompositeCopy\n"
  4569. " fraction:1.0\n"
  4570. " respectFlipped:NO\n"
  4571. " hints:nil];");
  4572. verifyFormat(
  4573. "scoped_nsobject<NSTextField> message(\n"
  4574. " // The frame will be fixed up when |-setMessageText:| is called.\n"
  4575. " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
  4576. verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
  4577. " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
  4578. " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
  4579. " aaaa:bbb];");
  4580. }
  4581. TEST_F(FormatTest, ObjCAt) {
  4582. verifyFormat("@autoreleasepool");
  4583. verifyFormat("@catch");
  4584. verifyFormat("@class");
  4585. verifyFormat("@compatibility_alias");
  4586. verifyFormat("@defs");
  4587. verifyFormat("@dynamic");
  4588. verifyFormat("@encode");
  4589. verifyFormat("@end");
  4590. verifyFormat("@finally");
  4591. verifyFormat("@implementation");
  4592. verifyFormat("@import");
  4593. verifyFormat("@interface");
  4594. verifyFormat("@optional");
  4595. verifyFormat("@package");
  4596. verifyFormat("@private");
  4597. verifyFormat("@property");
  4598. verifyFormat("@protected");
  4599. verifyFormat("@protocol");
  4600. verifyFormat("@public");
  4601. verifyFormat("@required");
  4602. verifyFormat("@selector");
  4603. verifyFormat("@synchronized");
  4604. verifyFormat("@synthesize");
  4605. verifyFormat("@throw");
  4606. verifyFormat("@try");
  4607. EXPECT_EQ("@interface", format("@ interface"));
  4608. // The precise formatting of this doesn't matter, nobody writes code like
  4609. // this.
  4610. verifyFormat("@ /*foo*/ interface");
  4611. }
  4612. TEST_F(FormatTest, ObjCSnippets) {
  4613. verifyFormat("@autoreleasepool {\n"
  4614. " foo();\n"
  4615. "}");
  4616. verifyFormat("@class Foo, Bar;");
  4617. verifyFormat("@compatibility_alias AliasName ExistingClass;");
  4618. verifyFormat("@dynamic textColor;");
  4619. verifyFormat("char *buf1 = @encode(int *);");
  4620. verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
  4621. verifyFormat("char *buf1 = @encode(int **);");
  4622. verifyFormat("Protocol *proto = @protocol(p1);");
  4623. verifyFormat("SEL s = @selector(foo:);");
  4624. verifyFormat("@synchronized(self) {\n"
  4625. " f();\n"
  4626. "}");
  4627. verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  4628. verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  4629. verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
  4630. verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
  4631. verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
  4632. verifyFormat("@import foo.bar;\n"
  4633. "@import baz;");
  4634. }
  4635. TEST_F(FormatTest, ObjCLiterals) {
  4636. verifyFormat("@\"String\"");
  4637. verifyFormat("@1");
  4638. verifyFormat("@+4.8");
  4639. verifyFormat("@-4");
  4640. verifyFormat("@1LL");
  4641. verifyFormat("@.5");
  4642. verifyFormat("@'c'");
  4643. verifyFormat("@true");
  4644. verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
  4645. verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
  4646. verifyFormat("NSNumber *favoriteColor = @(Green);");
  4647. verifyFormat("NSString *path = @(getenv(\"PATH\"));");
  4648. verifyFormat("@[");
  4649. verifyFormat("@[]");
  4650. verifyFormat(
  4651. "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
  4652. verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
  4653. verifyFormat("@{");
  4654. verifyFormat("@{}");
  4655. verifyFormat("@{ @\"one\" : @1 }");
  4656. verifyFormat("return @{ @\"one\" : @1 };");
  4657. verifyFormat("@{ @\"one\" : @1, }");
  4658. verifyFormat("@{ @\"one\" : @{ @2 : @1 } }");
  4659. verifyFormat("@{ @\"one\" : @{ @2 : @1 }, }");
  4660. verifyFormat("@{ 1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2 }");
  4661. verifyFormat("[self setDict:@{}");
  4662. verifyFormat("[self setDict:@{ @1 : @2 }");
  4663. verifyFormat("NSLog(@\"%@\", @{ @1 : @2, @2 : @3 }[@1]);");
  4664. verifyFormat(
  4665. "NSDictionary *masses = @{ @\"H\" : @1.0078, @\"He\" : @4.0026 };");
  4666. verifyFormat(
  4667. "NSDictionary *settings = @{ AVEncoderKey : @(AVAudioQualityMax) };");
  4668. // FIXME: Nested and multi-line array and dictionary literals need more work.
  4669. verifyFormat(
  4670. "NSDictionary *d = @{ @\"nam\" : NSUserNam(), @\"dte\" : [NSDate date],\n"
  4671. " @\"processInfo\" : [NSProcessInfo processInfo] };");
  4672. verifyFormat(
  4673. "@{ NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
  4674. " regularFont, };");
  4675. }
  4676. TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
  4677. EXPECT_EQ("{\n"
  4678. "{\n"
  4679. "a;\n"
  4680. "b;\n"
  4681. "}\n"
  4682. "}",
  4683. format("{\n"
  4684. "{\n"
  4685. "a;\n"
  4686. " b;\n"
  4687. "}\n"
  4688. "}",
  4689. 13, 2, getLLVMStyle()));
  4690. EXPECT_EQ("{\n"
  4691. "{\n"
  4692. " a;\n"
  4693. "b;\n"
  4694. "}\n"
  4695. "}",
  4696. format("{\n"
  4697. "{\n"
  4698. " a;\n"
  4699. "b;\n"
  4700. "}\n"
  4701. "}",
  4702. 9, 2, getLLVMStyle()));
  4703. EXPECT_EQ("{\n"
  4704. "{\n"
  4705. "public:\n"
  4706. " b;\n"
  4707. "}\n"
  4708. "}",
  4709. format("{\n"
  4710. "{\n"
  4711. "public:\n"
  4712. " b;\n"
  4713. "}\n"
  4714. "}",
  4715. 17, 2, getLLVMStyle()));
  4716. EXPECT_EQ("{\n"
  4717. "{\n"
  4718. "a;\n"
  4719. "}\n"
  4720. "{\n"
  4721. " b; //\n"
  4722. "}\n"
  4723. "}",
  4724. format("{\n"
  4725. "{\n"
  4726. "a;\n"
  4727. "}\n"
  4728. "{\n"
  4729. " b; //\n"
  4730. "}\n"
  4731. "}",
  4732. 22, 2, getLLVMStyle()));
  4733. EXPECT_EQ(" {\n"
  4734. " a; //\n"
  4735. " }",
  4736. format(" {\n"
  4737. "a; //\n"
  4738. " }",
  4739. 4, 2, getLLVMStyle()));
  4740. EXPECT_EQ("void f() {}\n"
  4741. "void g() {}",
  4742. format("void f() {}\n"
  4743. "void g() {}",
  4744. 13, 0, getLLVMStyle()));
  4745. EXPECT_EQ("int a; // comment\n"
  4746. " // line 2\n"
  4747. "int b;",
  4748. format("int a; // comment\n"
  4749. " // line 2\n"
  4750. " int b;",
  4751. 35, 0, getLLVMStyle()));
  4752. EXPECT_EQ(" int a;\n"
  4753. " void\n"
  4754. " ffffff() {\n"
  4755. " }",
  4756. format(" int a;\n"
  4757. "void ffffff() {}",
  4758. 11, 0, getLLVMStyleWithColumns(11)));
  4759. }
  4760. TEST_F(FormatTest, BreakStringLiterals) {
  4761. EXPECT_EQ("\"some text \"\n"
  4762. "\"other\";",
  4763. format("\"some text other\";", getLLVMStyleWithColumns(12)));
  4764. EXPECT_EQ("\"some text \"\n"
  4765. "\"other\";",
  4766. format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
  4767. EXPECT_EQ(
  4768. "#define A \\\n"
  4769. " \"some \" \\\n"
  4770. " \"text \" \\\n"
  4771. " \"other\";",
  4772. format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
  4773. EXPECT_EQ(
  4774. "#define A \\\n"
  4775. " \"so \" \\\n"
  4776. " \"text \" \\\n"
  4777. " \"other\";",
  4778. format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
  4779. EXPECT_EQ("\"some text\"",
  4780. format("\"some text\"", getLLVMStyleWithColumns(1)));
  4781. EXPECT_EQ("\"some text\"",
  4782. format("\"some text\"", getLLVMStyleWithColumns(11)));
  4783. EXPECT_EQ("\"some \"\n"
  4784. "\"text\"",
  4785. format("\"some text\"", getLLVMStyleWithColumns(10)));
  4786. EXPECT_EQ("\"some \"\n"
  4787. "\"text\"",
  4788. format("\"some text\"", getLLVMStyleWithColumns(7)));
  4789. EXPECT_EQ("\"some\"\n"
  4790. "\" tex\"\n"
  4791. "\"t\"",
  4792. format("\"some text\"", getLLVMStyleWithColumns(6)));
  4793. EXPECT_EQ("\"some\"\n"
  4794. "\" tex\"\n"
  4795. "\" and\"",
  4796. format("\"some tex and\"", getLLVMStyleWithColumns(6)));
  4797. EXPECT_EQ("\"some\"\n"
  4798. "\"/tex\"\n"
  4799. "\"/and\"",
  4800. format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
  4801. EXPECT_EQ("variable =\n"
  4802. " \"long string \"\n"
  4803. " \"literal\";",
  4804. format("variable = \"long string literal\";",
  4805. getLLVMStyleWithColumns(20)));
  4806. EXPECT_EQ("variable = f(\n"
  4807. " \"long string \"\n"
  4808. " \"literal\",\n"
  4809. " short,\n"
  4810. " loooooooooooooooooooong);",
  4811. format("variable = f(\"long string literal\", short, "
  4812. "loooooooooooooooooooong);",
  4813. getLLVMStyleWithColumns(20)));
  4814. EXPECT_EQ("f(g(\"long string \"\n"
  4815. " \"literal\"),\n"
  4816. " b);",
  4817. format("f(g(\"long string literal\"), b);",
  4818. getLLVMStyleWithColumns(20)));
  4819. EXPECT_EQ("f(g(\"long string \"\n"
  4820. " \"literal\",\n"
  4821. " a),\n"
  4822. " b);",
  4823. format("f(g(\"long string literal\", a), b);",
  4824. getLLVMStyleWithColumns(20)));
  4825. EXPECT_EQ(
  4826. "f(\"one two\".split(\n"
  4827. " variable));",
  4828. format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
  4829. EXPECT_EQ("f(\"one two three four five six \"\n"
  4830. " \"seven\".split(\n"
  4831. " really_looooong_variable));",
  4832. format("f(\"one two three four five six seven\"."
  4833. "split(really_looooong_variable));",
  4834. getLLVMStyleWithColumns(33)));
  4835. EXPECT_EQ("f(\"some \"\n"
  4836. " \"text\",\n"
  4837. " other);",
  4838. format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
  4839. // Only break as a last resort.
  4840. verifyFormat(
  4841. "aaaaaaaaaaaaaaaaaaaa(\n"
  4842. " aaaaaaaaaaaaaaaaaaaa,\n"
  4843. " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
  4844. EXPECT_EQ(
  4845. "\"splitmea\"\n"
  4846. "\"trandomp\"\n"
  4847. "\"oint\"",
  4848. format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
  4849. EXPECT_EQ(
  4850. "\"split/\"\n"
  4851. "\"pathat/\"\n"
  4852. "\"slashes\"",
  4853. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  4854. EXPECT_EQ(
  4855. "\"split/\"\n"
  4856. "\"pathat/\"\n"
  4857. "\"slashes\"",
  4858. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  4859. EXPECT_EQ("\"split at \"\n"
  4860. "\"spaces/at/\"\n"
  4861. "\"slashes.at.any$\"\n"
  4862. "\"non-alphanumeric%\"\n"
  4863. "\"1111111111characte\"\n"
  4864. "\"rs\"",
  4865. format("\"split at "
  4866. "spaces/at/"
  4867. "slashes.at."
  4868. "any$non-"
  4869. "alphanumeric%"
  4870. "1111111111characte"
  4871. "rs\"",
  4872. getLLVMStyleWithColumns(20)));
  4873. // Verify that splitting the strings understands
  4874. // Style::AlwaysBreakBeforeMultilineStrings.
  4875. EXPECT_EQ("aaaaaaaaaaaa(aaaaaaaaaaaaa,\n"
  4876. " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
  4877. " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
  4878. format("aaaaaaaaaaaa(aaaaaaaaaaaaa, \"aaaaaaaaaaaaaaaaaaaaaa "
  4879. "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa "
  4880. "aaaaaaaaaaaaaaaaaaaaaa\");",
  4881. getGoogleStyle()));
  4882. EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  4883. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  4884. format("llvm::outs() << "
  4885. "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
  4886. "aaaaaaaaaaaaaaaaaaa\";"));
  4887. EXPECT_EQ("ffff(\n"
  4888. " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  4889. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  4890. format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  4891. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  4892. getGoogleStyle()));
  4893. FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
  4894. AlignLeft.AlignEscapedNewlinesLeft = true;
  4895. EXPECT_EQ(
  4896. "#define A \\\n"
  4897. " \"some \" \\\n"
  4898. " \"text \" \\\n"
  4899. " \"other\";",
  4900. format("#define A \"some text other\";", AlignLeft));
  4901. }
  4902. TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
  4903. EXPECT_EQ(
  4904. "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  4905. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  4906. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  4907. format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  4908. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  4909. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
  4910. }
  4911. TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
  4912. EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
  4913. format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle()));
  4914. EXPECT_EQ("fffffffffff(g(R\"x(\n"
  4915. "multiline raw string literal xxxxxxxxxxxxxx\n"
  4916. ")x\",\n"
  4917. " a),\n"
  4918. " b);",
  4919. format("fffffffffff(g(R\"x(\n"
  4920. "multiline raw string literal xxxxxxxxxxxxxx\n"
  4921. ")x\", a), b);",
  4922. getGoogleStyleWithColumns(20)));
  4923. EXPECT_EQ("fffffffffff(\n"
  4924. " g(R\"x(qqq\n"
  4925. "multiline raw string literal xxxxxxxxxxxxxx\n"
  4926. ")x\",\n"
  4927. " a),\n"
  4928. " b);",
  4929. format("fffffffffff(g(R\"x(qqq\n"
  4930. "multiline raw string literal xxxxxxxxxxxxxx\n"
  4931. ")x\", a), b);",
  4932. getGoogleStyleWithColumns(20)));
  4933. EXPECT_EQ("fffffffffff(R\"x(\n"
  4934. "multiline raw string literal xxxxxxxxxxxxxx\n"
  4935. ")x\");",
  4936. format("fffffffffff(R\"x(\n"
  4937. "multiline raw string literal xxxxxxxxxxxxxx\n"
  4938. ")x\");",
  4939. getGoogleStyleWithColumns(20)));
  4940. EXPECT_EQ("fffffffffff(R\"x(\n"
  4941. "multiline raw string literal xxxxxxxxxxxxxx\n"
  4942. ")x\" +\n"
  4943. " bbbbbb);",
  4944. format("fffffffffff(R\"x(\n"
  4945. "multiline raw string literal xxxxxxxxxxxxxx\n"
  4946. ")x\" + bbbbbb);",
  4947. getGoogleStyleWithColumns(20)));
  4948. }
  4949. TEST_F(FormatTest, SkipsUnknownStringLiterals) {
  4950. EXPECT_EQ(
  4951. "u8\"unsupported literal\";",
  4952. format("u8\"unsupported literal\";", getGoogleStyleWithColumns(15)));
  4953. EXPECT_EQ("u\"unsupported literal\";",
  4954. format("u\"unsupported literal\";", getGoogleStyleWithColumns(15)));
  4955. EXPECT_EQ("U\"unsupported literal\";",
  4956. format("U\"unsupported literal\";", getGoogleStyleWithColumns(15)));
  4957. EXPECT_EQ("L\"unsupported literal\";",
  4958. format("L\"unsupported literal\";", getGoogleStyleWithColumns(15)));
  4959. EXPECT_EQ("R\"x(raw literal)x\";",
  4960. format("R\"x(raw literal)x\";", getGoogleStyleWithColumns(15)));
  4961. verifyFormat("string a = \"unterminated;");
  4962. EXPECT_EQ("function(\"unterminated,\n"
  4963. " OtherParameter);",
  4964. format("function( \"unterminated,\n"
  4965. " OtherParameter);"));
  4966. }
  4967. TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
  4968. EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
  4969. format("#define x(_a) printf(\"foo\"_a);", getLLVMStyle()));
  4970. }
  4971. TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
  4972. EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
  4973. " \"ddeeefff\");",
  4974. format("someFunction(\"aaabbbcccdddeeefff\");",
  4975. getLLVMStyleWithColumns(25)));
  4976. EXPECT_EQ("someFunction1234567890(\n"
  4977. " \"aaabbbcccdddeeefff\");",
  4978. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  4979. getLLVMStyleWithColumns(26)));
  4980. EXPECT_EQ("someFunction1234567890(\n"
  4981. " \"aaabbbcccdddeeeff\"\n"
  4982. " \"f\");",
  4983. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  4984. getLLVMStyleWithColumns(25)));
  4985. EXPECT_EQ("someFunction1234567890(\n"
  4986. " \"aaabbbcccdddeeeff\"\n"
  4987. " \"f\");",
  4988. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  4989. getLLVMStyleWithColumns(24)));
  4990. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  4991. " \"ddde \"\n"
  4992. " \"efff\");",
  4993. format("someFunction(\"aaabbbcc ddde efff\");",
  4994. getLLVMStyleWithColumns(25)));
  4995. EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
  4996. " \"ddeeefff\");",
  4997. format("someFunction(\"aaabbbccc ddeeefff\");",
  4998. getLLVMStyleWithColumns(25)));
  4999. EXPECT_EQ("someFunction1234567890(\n"
  5000. " \"aaabb \"\n"
  5001. " \"cccdddeeefff\");",
  5002. format("someFunction1234567890(\"aaabb cccdddeeefff\");",
  5003. getLLVMStyleWithColumns(25)));
  5004. EXPECT_EQ("#define A \\\n"
  5005. " string s = \\\n"
  5006. " \"123456789\" \\\n"
  5007. " \"0\"; \\\n"
  5008. " int i;",
  5009. format("#define A string s = \"1234567890\"; int i;",
  5010. getLLVMStyleWithColumns(20)));
  5011. // FIXME: Put additional penalties on breaking at non-whitespace locations.
  5012. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  5013. " \"dddeeeff\"\n"
  5014. " \"f\");",
  5015. format("someFunction(\"aaabbbcc dddeeefff\");",
  5016. getLLVMStyleWithColumns(25)));
  5017. }
  5018. TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
  5019. EXPECT_EQ("\"\\a\"",
  5020. format("\"\\a\"", getLLVMStyleWithColumns(3)));
  5021. EXPECT_EQ("\"\\\"",
  5022. format("\"\\\"", getLLVMStyleWithColumns(2)));
  5023. EXPECT_EQ("\"test\"\n"
  5024. "\"\\n\"",
  5025. format("\"test\\n\"", getLLVMStyleWithColumns(7)));
  5026. EXPECT_EQ("\"tes\\\\\"\n"
  5027. "\"n\"",
  5028. format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
  5029. EXPECT_EQ("\"\\\\\\\\\"\n"
  5030. "\"\\n\"",
  5031. format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
  5032. EXPECT_EQ("\"\\uff01\"",
  5033. format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
  5034. EXPECT_EQ("\"\\uff01\"\n"
  5035. "\"test\"",
  5036. format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
  5037. EXPECT_EQ("\"\\Uff01ff02\"",
  5038. format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
  5039. EXPECT_EQ("\"\\x000000000001\"\n"
  5040. "\"next\"",
  5041. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
  5042. EXPECT_EQ("\"\\x000000000001next\"",
  5043. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
  5044. EXPECT_EQ("\"\\x000000000001\"",
  5045. format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
  5046. EXPECT_EQ("\"test\"\n"
  5047. "\"\\000000\"\n"
  5048. "\"000001\"",
  5049. format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
  5050. EXPECT_EQ("\"test\\000\"\n"
  5051. "\"00000000\"\n"
  5052. "\"1\"",
  5053. format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
  5054. EXPECT_EQ("R\"(\\x\\x00)\"\n",
  5055. format("R\"(\\x\\x00)\"\n", getGoogleStyleWithColumns(7)));
  5056. }
  5057. TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
  5058. verifyFormat("void f() {\n"
  5059. " return g() {}\n"
  5060. " void h() {}");
  5061. verifyFormat("if (foo)\n"
  5062. " return { forgot_closing_brace();\n"
  5063. "test();");
  5064. verifyFormat("int a[] = { void forgot_closing_brace() { f();\n"
  5065. "g();\n"
  5066. "}");
  5067. }
  5068. TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
  5069. verifyFormat("class X {\n"
  5070. " void f() {\n"
  5071. " }\n"
  5072. "};",
  5073. getLLVMStyleWithColumns(12));
  5074. }
  5075. TEST_F(FormatTest, ConfigurableIndentWidth) {
  5076. FormatStyle EightIndent = getLLVMStyleWithColumns(18);
  5077. EightIndent.IndentWidth = 8;
  5078. verifyFormat("void f() {\n"
  5079. " someFunction();\n"
  5080. " if (true) {\n"
  5081. " f();\n"
  5082. " }\n"
  5083. "}",
  5084. EightIndent);
  5085. verifyFormat("class X {\n"
  5086. " void f() {\n"
  5087. " }\n"
  5088. "};",
  5089. EightIndent);
  5090. verifyFormat("int x[] = {\n"
  5091. " call(),\n"
  5092. " call(),\n"
  5093. "};",
  5094. EightIndent);
  5095. }
  5096. TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
  5097. verifyFormat("void\n"
  5098. "f();",
  5099. getLLVMStyleWithColumns(8));
  5100. }
  5101. TEST_F(FormatTest, ConfigurableUseOfTab) {
  5102. FormatStyle Tab = getLLVMStyleWithColumns(42);
  5103. Tab.IndentWidth = 8;
  5104. Tab.UseTab = true;
  5105. Tab.AlignEscapedNewlinesLeft = true;
  5106. verifyFormat("class X {\n"
  5107. "\tvoid f() {\n"
  5108. "\t\tsomeFunction(parameter1,\n"
  5109. "\t\t\t parameter2);\n"
  5110. "\t}\n"
  5111. "};",
  5112. Tab);
  5113. verifyFormat("#define A \\\n"
  5114. "\tvoid f() { \\\n"
  5115. "\t\tsomeFunction( \\\n"
  5116. "\t\t parameter1, \\\n"
  5117. "\t\t parameter2); \\\n"
  5118. "\t}",
  5119. Tab);
  5120. EXPECT_EQ("void f() {\n"
  5121. "\tf();\n"
  5122. "\tg();\n"
  5123. "}",
  5124. format("void f() {\n"
  5125. "\tf();\n"
  5126. "\tg();\n"
  5127. "}",
  5128. 0, 0, Tab));
  5129. EXPECT_EQ("void f() {\n"
  5130. "\tf();\n"
  5131. "\tg();\n"
  5132. "}",
  5133. format("void f() {\n"
  5134. "\tf();\n"
  5135. "\tg();\n"
  5136. "}",
  5137. 16, 0, Tab));
  5138. EXPECT_EQ("void f() {\n"
  5139. " \tf();\n"
  5140. "\tg();\n"
  5141. "}",
  5142. format("void f() {\n"
  5143. " \tf();\n"
  5144. " \tg();\n"
  5145. "}",
  5146. 21, 0, Tab));
  5147. // FIXME: To correctly count mixed whitespace we need to
  5148. // also correctly count mixed whitespace in front of the comment.
  5149. //
  5150. // EXPECT_EQ("/*\n"
  5151. // "\t a\t\tcomment\n"
  5152. // "\t in multiple lines\n"
  5153. // " */",
  5154. // format(" /*\t \t \n"
  5155. // " \t \t a\t\tcomment\t \t\n"
  5156. // " \t \t in multiple lines\t\n"
  5157. // " \t */",
  5158. // Tab));
  5159. // Tab.UseTab = false;
  5160. // EXPECT_EQ("/*\n"
  5161. // " a\t\tcomment\n"
  5162. // " in multiple lines\n"
  5163. // " */",
  5164. // format(" /*\t \t \n"
  5165. // " \t \t a\t\tcomment\t \t\n"
  5166. // " \t \t in multiple lines\t\n"
  5167. // " \t */",
  5168. // Tab));
  5169. // EXPECT_EQ("/* some\n"
  5170. // " comment */",
  5171. // format(" \t \t /* some\n"
  5172. // " \t \t comment */",
  5173. // Tab));
  5174. EXPECT_EQ("{\n"
  5175. " /*\n"
  5176. " * Comment\n"
  5177. " */\n"
  5178. " int i;\n"
  5179. "}",
  5180. format("{\n"
  5181. "\t/*\n"
  5182. "\t * Comment\n"
  5183. "\t */\n"
  5184. "\t int i;\n"
  5185. "}"));
  5186. }
  5187. TEST_F(FormatTest, ConfigurableSpaceAfterControlStatementKeyword) {
  5188. FormatStyle NoSpace = getLLVMStyle();
  5189. NoSpace.SpaceAfterControlStatementKeyword = false;
  5190. verifyFormat("while(true)\n"
  5191. " continue;", NoSpace);
  5192. verifyFormat("for(;;)\n"
  5193. " continue;", NoSpace);
  5194. verifyFormat("if(true)\n"
  5195. " f();\n"
  5196. "else if(true)\n"
  5197. " f();", NoSpace);
  5198. verifyFormat("do {\n"
  5199. " do_something();\n"
  5200. "} while(something());", NoSpace);
  5201. verifyFormat("switch(x) {\n"
  5202. "default:\n"
  5203. " break;\n"
  5204. "}", NoSpace);
  5205. }
  5206. TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
  5207. FormatStyle Spaces = getLLVMStyle();
  5208. Spaces.SpacesInParentheses = true;
  5209. verifyFormat("call( x, y, z );", Spaces);
  5210. verifyFormat("while ( (bool)1 )\n"
  5211. " continue;", Spaces);
  5212. verifyFormat("for ( ;; )\n"
  5213. " continue;", Spaces);
  5214. verifyFormat("if ( true )\n"
  5215. " f();\n"
  5216. "else if ( true )\n"
  5217. " f();", Spaces);
  5218. verifyFormat("do {\n"
  5219. " do_something( (int)i );\n"
  5220. "} while ( something() );", Spaces);
  5221. verifyFormat("switch ( x ) {\n"
  5222. "default:\n"
  5223. " break;\n"
  5224. "}", Spaces);
  5225. Spaces.SpacesInParentheses = false;
  5226. Spaces.SpacesInCStyleCastParentheses = true;
  5227. verifyFormat("Type *A = ( Type * )P;", Spaces);
  5228. verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
  5229. verifyFormat("x = ( int32 )y;", Spaces);
  5230. verifyFormat("int a = ( int )(2.0f);", Spaces);
  5231. verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
  5232. verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
  5233. verifyFormat("#define x (( int )-1)", Spaces);
  5234. Spaces.SpacesInParentheses = false;
  5235. Spaces.SpaceInEmptyParentheses = true;
  5236. verifyFormat("call(x, y, z);", Spaces);
  5237. verifyFormat("call( )", Spaces);
  5238. // Run the first set of tests again with
  5239. // Spaces.SpacesInParentheses = false,
  5240. // Spaces.SpaceInEmptyParentheses = true and
  5241. // Spaces.SpacesInCStyleCastParentheses = true
  5242. Spaces.SpacesInParentheses = false,
  5243. Spaces.SpaceInEmptyParentheses = true;
  5244. Spaces.SpacesInCStyleCastParentheses = true;
  5245. verifyFormat("call(x, y, z);", Spaces);
  5246. verifyFormat("while (( bool )1)\n"
  5247. " continue;", Spaces);
  5248. verifyFormat("for (;;)\n"
  5249. " continue;", Spaces);
  5250. verifyFormat("if (true)\n"
  5251. " f( );\n"
  5252. "else if (true)\n"
  5253. " f( );", Spaces);
  5254. verifyFormat("do {\n"
  5255. " do_something(( int )i);\n"
  5256. "} while (something( ));", Spaces);
  5257. verifyFormat("switch (x) {\n"
  5258. "default:\n"
  5259. " break;\n"
  5260. "}", Spaces);
  5261. }
  5262. TEST_F(FormatTest, LinuxBraceBreaking) {
  5263. FormatStyle BreakBeforeBrace = getLLVMStyle();
  5264. BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Linux;
  5265. verifyFormat("namespace a\n"
  5266. "{\n"
  5267. "class A\n"
  5268. "{\n"
  5269. " void f()\n"
  5270. " {\n"
  5271. " if (true) {\n"
  5272. " a();\n"
  5273. " b();\n"
  5274. " }\n"
  5275. " }\n"
  5276. " void g()\n"
  5277. " {\n"
  5278. " return;\n"
  5279. " }\n"
  5280. "}\n"
  5281. "}",
  5282. BreakBeforeBrace);
  5283. }
  5284. TEST_F(FormatTest, StroustrupBraceBreaking) {
  5285. FormatStyle BreakBeforeBrace = getLLVMStyle();
  5286. BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  5287. verifyFormat("namespace a {\n"
  5288. "class A {\n"
  5289. " void f()\n"
  5290. " {\n"
  5291. " if (true) {\n"
  5292. " a();\n"
  5293. " b();\n"
  5294. " }\n"
  5295. " }\n"
  5296. " void g()\n"
  5297. " {\n"
  5298. " return;\n"
  5299. " }\n"
  5300. "}\n"
  5301. "}",
  5302. BreakBeforeBrace);
  5303. }
  5304. TEST_F(FormatTest, AllmanBraceBreaking) {
  5305. FormatStyle BreakBeforeBrace = getLLVMStyle();
  5306. BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Allman;
  5307. verifyFormat("namespace a\n"
  5308. "{\n"
  5309. "class A\n"
  5310. "{\n"
  5311. " void f()\n"
  5312. " {\n"
  5313. " if (true)\n"
  5314. " {\n"
  5315. " a();\n"
  5316. " b();\n"
  5317. " }\n"
  5318. " }\n"
  5319. " void g()\n"
  5320. " {\n"
  5321. " return;\n"
  5322. " }\n"
  5323. "}\n"
  5324. "}",
  5325. BreakBeforeBrace);
  5326. verifyFormat("void f()\n"
  5327. "{\n"
  5328. " if (true)\n"
  5329. " {\n"
  5330. " a();\n"
  5331. " }\n"
  5332. " else if (false)\n"
  5333. " {\n"
  5334. " b();\n"
  5335. " }\n"
  5336. " else\n"
  5337. " {\n"
  5338. " c();\n"
  5339. " }\n"
  5340. "}\n",
  5341. BreakBeforeBrace);
  5342. verifyFormat("void f()\n"
  5343. "{\n"
  5344. " for (int i = 0; i < 10; ++i)\n"
  5345. " {\n"
  5346. " a();\n"
  5347. " }\n"
  5348. " while (false)\n"
  5349. " {\n"
  5350. " b();\n"
  5351. " }\n"
  5352. " do\n"
  5353. " {\n"
  5354. " c();\n"
  5355. " } while (false)\n"
  5356. "}\n",
  5357. BreakBeforeBrace);
  5358. verifyFormat("void f(int a)\n"
  5359. "{\n"
  5360. " switch (a)\n"
  5361. " {\n"
  5362. " case 0:\n"
  5363. " break;\n"
  5364. " case 1:\n"
  5365. " {\n"
  5366. " break;\n"
  5367. " }\n"
  5368. " case 2:\n"
  5369. " {\n"
  5370. " }\n"
  5371. " break;\n"
  5372. " default:\n"
  5373. " break;\n"
  5374. " }\n"
  5375. "}\n",
  5376. BreakBeforeBrace);
  5377. verifyFormat("enum X\n"
  5378. "{\n"
  5379. " Y = 0,\n"
  5380. "}\n",
  5381. BreakBeforeBrace);
  5382. FormatStyle BreakBeforeBraceShortIfs = BreakBeforeBrace;
  5383. BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
  5384. BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
  5385. verifyFormat("void f(bool b)\n"
  5386. "{\n"
  5387. " if (b)\n"
  5388. " {\n"
  5389. " return;\n"
  5390. " }\n"
  5391. "}\n",
  5392. BreakBeforeBraceShortIfs);
  5393. verifyFormat("void f(bool b)\n"
  5394. "{\n"
  5395. " if (b) return;\n"
  5396. "}\n",
  5397. BreakBeforeBraceShortIfs);
  5398. verifyFormat("void f(bool b)\n"
  5399. "{\n"
  5400. " while (b)\n"
  5401. " {\n"
  5402. " return;\n"
  5403. " }\n"
  5404. "}\n",
  5405. BreakBeforeBraceShortIfs);
  5406. }
  5407. TEST_F(FormatTest, CatchExceptionReferenceBinding) {
  5408. verifyFormat("void f() {\n"
  5409. " try {\n"
  5410. " }\n"
  5411. " catch (const Exception &e) {\n"
  5412. " }\n"
  5413. "}\n",
  5414. getLLVMStyle());
  5415. }
  5416. TEST_F(FormatTest, UnderstandsPragmas) {
  5417. verifyFormat("#pragma omp reduction(| : var)");
  5418. verifyFormat("#pragma omp reduction(+ : var)");
  5419. }
  5420. bool allStylesEqual(ArrayRef<FormatStyle> Styles) {
  5421. for (size_t i = 1; i < Styles.size(); ++i)
  5422. if (!(Styles[0] == Styles[i]))
  5423. return false;
  5424. return true;
  5425. }
  5426. TEST_F(FormatTest, GetsPredefinedStyleByName) {
  5427. FormatStyle Styles[3];
  5428. Styles[0] = getLLVMStyle();
  5429. EXPECT_TRUE(getPredefinedStyle("LLVM", &Styles[1]));
  5430. EXPECT_TRUE(getPredefinedStyle("lLvM", &Styles[2]));
  5431. EXPECT_TRUE(allStylesEqual(Styles));
  5432. Styles[0] = getGoogleStyle();
  5433. EXPECT_TRUE(getPredefinedStyle("Google", &Styles[1]));
  5434. EXPECT_TRUE(getPredefinedStyle("gOOgle", &Styles[2]));
  5435. EXPECT_TRUE(allStylesEqual(Styles));
  5436. Styles[0] = getChromiumStyle();
  5437. EXPECT_TRUE(getPredefinedStyle("Chromium", &Styles[1]));
  5438. EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", &Styles[2]));
  5439. EXPECT_TRUE(allStylesEqual(Styles));
  5440. Styles[0] = getMozillaStyle();
  5441. EXPECT_TRUE(getPredefinedStyle("Mozilla", &Styles[1]));
  5442. EXPECT_TRUE(getPredefinedStyle("moZILla", &Styles[2]));
  5443. EXPECT_TRUE(allStylesEqual(Styles));
  5444. Styles[0] = getWebKitStyle();
  5445. EXPECT_TRUE(getPredefinedStyle("WebKit", &Styles[1]));
  5446. EXPECT_TRUE(getPredefinedStyle("wEbKit", &Styles[2]));
  5447. EXPECT_TRUE(allStylesEqual(Styles));
  5448. EXPECT_FALSE(getPredefinedStyle("qwerty", &Styles[0]));
  5449. }
  5450. TEST_F(FormatTest, ParsesConfiguration) {
  5451. FormatStyle Style = {};
  5452. #define CHECK_PARSE(TEXT, FIELD, VALUE) \
  5453. EXPECT_NE(VALUE, Style.FIELD); \
  5454. EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \
  5455. EXPECT_EQ(VALUE, Style.FIELD)
  5456. #define CHECK_PARSE_BOOL(FIELD) \
  5457. Style.FIELD = false; \
  5458. EXPECT_EQ(0, parseConfiguration(#FIELD ": true", &Style).value()); \
  5459. EXPECT_TRUE(Style.FIELD); \
  5460. EXPECT_EQ(0, parseConfiguration(#FIELD ": false", &Style).value()); \
  5461. EXPECT_FALSE(Style.FIELD);
  5462. CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
  5463. CHECK_PARSE_BOOL(AlignTrailingComments);
  5464. CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
  5465. CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
  5466. CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
  5467. CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
  5468. CHECK_PARSE_BOOL(BinPackParameters);
  5469. CHECK_PARSE_BOOL(BreakBeforeBinaryOperators);
  5470. CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
  5471. CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
  5472. CHECK_PARSE_BOOL(DerivePointerBinding);
  5473. CHECK_PARSE_BOOL(IndentCaseLabels);
  5474. CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
  5475. CHECK_PARSE_BOOL(PointerBindsToType);
  5476. CHECK_PARSE_BOOL(Cpp11BracedListStyle);
  5477. CHECK_PARSE_BOOL(UseTab);
  5478. CHECK_PARSE_BOOL(IndentFunctionDeclarationAfterType);
  5479. CHECK_PARSE_BOOL(SpacesInParentheses);
  5480. CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
  5481. CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
  5482. CHECK_PARSE_BOOL(SpaceAfterControlStatementKeyword);
  5483. CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
  5484. CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
  5485. ConstructorInitializerIndentWidth, 1234u);
  5486. CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
  5487. CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
  5488. CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
  5489. CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
  5490. PenaltyReturnTypeOnItsOwnLine, 1234u);
  5491. CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
  5492. SpacesBeforeTrailingComments, 1234u);
  5493. CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
  5494. Style.Standard = FormatStyle::LS_Auto;
  5495. CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
  5496. CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
  5497. CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
  5498. Style.ColumnLimit = 123;
  5499. FormatStyle BaseStyle = getLLVMStyle();
  5500. CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
  5501. CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
  5502. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  5503. CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
  5504. FormatStyle::BS_Attach);
  5505. CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
  5506. FormatStyle::BS_Linux);
  5507. CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
  5508. FormatStyle::BS_Stroustrup);
  5509. Style.NamespaceIndentation = FormatStyle::NI_All;
  5510. CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
  5511. FormatStyle::NI_None);
  5512. CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
  5513. FormatStyle::NI_Inner);
  5514. CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
  5515. FormatStyle::NI_All);
  5516. #undef CHECK_PARSE
  5517. #undef CHECK_PARSE_BOOL
  5518. }
  5519. TEST_F(FormatTest, ConfigurationRoundTripTest) {
  5520. FormatStyle Style = getLLVMStyle();
  5521. std::string YAML = configurationAsText(Style);
  5522. FormatStyle ParsedStyle = {};
  5523. EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
  5524. EXPECT_EQ(Style, ParsedStyle);
  5525. }
  5526. TEST_F(FormatTest, WorksFor8bitEncodings) {
  5527. EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
  5528. "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
  5529. "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
  5530. "\"\xef\xee\xf0\xf3...\"",
  5531. format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
  5532. "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
  5533. "\xef\xee\xf0\xf3...\"",
  5534. getLLVMStyleWithColumns(12)));
  5535. }
  5536. // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
  5537. #if !defined(_MSC_VER)
  5538. TEST_F(FormatTest, CountsUTF8CharactersProperly) {
  5539. verifyFormat("\"Однажды в студёную зимнюю пору...\"",
  5540. getLLVMStyleWithColumns(35));
  5541. verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
  5542. getLLVMStyleWithColumns(21));
  5543. verifyFormat("// Однажды в студёную зимнюю пору...",
  5544. getLLVMStyleWithColumns(36));
  5545. verifyFormat("// 一 二 三 四 五 六 七 八 九 十",
  5546. getLLVMStyleWithColumns(22));
  5547. verifyFormat("/* Однажды в студёную зимнюю пору... */",
  5548. getLLVMStyleWithColumns(39));
  5549. verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
  5550. getLLVMStyleWithColumns(25));
  5551. }
  5552. TEST_F(FormatTest, SplitsUTF8Strings) {
  5553. EXPECT_EQ(
  5554. "\"Однажды, в \"\n"
  5555. "\"студёную \"\n"
  5556. "\"зимнюю \"\n"
  5557. "\"пору,\"",
  5558. format("\"Однажды, в студёную зимнюю пору,\"",
  5559. getLLVMStyleWithColumns(13)));
  5560. EXPECT_EQ("\"一 二 三 四 \"\n"
  5561. "\"五 六 七 八 \"\n"
  5562. "\"九 十\"",
  5563. format("\"一 二 三 四 五 六 七 八 九 十\"",
  5564. getLLVMStyleWithColumns(10)));
  5565. }
  5566. TEST_F(FormatTest, SplitsUTF8LineComments) {
  5567. EXPECT_EQ("// Я из лесу\n"
  5568. "// вышел; был\n"
  5569. "// сильный\n"
  5570. "// мороз.",
  5571. format("// Я из лесу вышел; был сильный мороз.",
  5572. getLLVMStyleWithColumns(13)));
  5573. EXPECT_EQ("// 一二三\n"
  5574. "// 四五六七\n"
  5575. "// 八\n"
  5576. "// 九 十",
  5577. format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(6)));
  5578. }
  5579. TEST_F(FormatTest, SplitsUTF8BlockComments) {
  5580. EXPECT_EQ("/* Гляжу,\n"
  5581. " * поднимается\n"
  5582. " * медленно в\n"
  5583. " * гору\n"
  5584. " * Лошадка,\n"
  5585. " * везущая\n"
  5586. " * хворосту\n"
  5587. " * воз. */",
  5588. format("/* Гляжу, поднимается медленно в гору\n"
  5589. " * Лошадка, везущая хворосту воз. */",
  5590. getLLVMStyleWithColumns(13)));
  5591. EXPECT_EQ("/* 一二三\n"
  5592. " * 四五六七\n"
  5593. " * 八\n"
  5594. " * 九 十\n"
  5595. " */",
  5596. format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(6)));
  5597. EXPECT_EQ("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
  5598. " * 𝕓𝕪𝕥𝕖\n"
  5599. " * 𝖀𝕿𝕱-𝟠 */",
  5600. format("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12)));
  5601. }
  5602. TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
  5603. FormatStyle Style = getLLVMStyle();
  5604. Style.ConstructorInitializerIndentWidth = 4;
  5605. verifyFormat(
  5606. "SomeClass::Constructor()\n"
  5607. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  5608. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  5609. Style);
  5610. Style.ConstructorInitializerIndentWidth = 2;
  5611. verifyFormat(
  5612. "SomeClass::Constructor()\n"
  5613. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  5614. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  5615. Style);
  5616. Style.ConstructorInitializerIndentWidth = 0;
  5617. verifyFormat(
  5618. "SomeClass::Constructor()\n"
  5619. ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  5620. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  5621. Style);
  5622. Style.BreakConstructorInitializersBeforeComma = true;
  5623. Style.ConstructorInitializerIndentWidth = 4;
  5624. verifyFormat("SomeClass::Constructor()\n"
  5625. " : a(a)\n"
  5626. " , b(b)\n"
  5627. " , c(c) {}",
  5628. Style);
  5629. Style.ConstructorInitializerIndentWidth = 2;
  5630. verifyFormat("SomeClass::Constructor()\n"
  5631. " : a(a)\n"
  5632. " , b(b)\n"
  5633. " , c(c) {}",
  5634. Style);
  5635. Style.ConstructorInitializerIndentWidth = 0;
  5636. verifyFormat("SomeClass::Constructor()\n"
  5637. ": a(a)\n"
  5638. ", b(b)\n"
  5639. ", c(c) {}",
  5640. Style);
  5641. }
  5642. #endif
  5643. TEST_F(FormatTest, FormatsWithWebKitStyle) {
  5644. FormatStyle Style = getWebKitStyle();
  5645. // Don't indent in outer namespaces.
  5646. verifyFormat("namespace outer {\n"
  5647. "int i;\n"
  5648. "namespace inner {\n"
  5649. " int i;\n"
  5650. "} // namespace inner\n"
  5651. "} // namespace outer\n"
  5652. "namespace other_outer {\n"
  5653. "int i;\n"
  5654. "}",
  5655. Style);
  5656. // Don't indent case labels.
  5657. verifyFormat("switch (variable) {\n"
  5658. "case 1:\n"
  5659. "case 2:\n"
  5660. " doSomething();\n"
  5661. " break;\n"
  5662. "default:\n"
  5663. " ++variable;\n"
  5664. "}",
  5665. Style);
  5666. // Wrap before binary operators.
  5667. EXPECT_EQ(
  5668. "void f()\n"
  5669. "{\n"
  5670. " if (aaaaaaaaaaaaaaaa\n"
  5671. " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
  5672. " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  5673. " return;\n"
  5674. "}",
  5675. format(
  5676. "void f() {\n"
  5677. "if (aaaaaaaaaaaaaaaa\n"
  5678. "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
  5679. "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  5680. "return;\n"
  5681. "}",
  5682. Style));
  5683. // Constructor initializers are formatted one per line with the "," on the
  5684. // new line.
  5685. verifyFormat("Constructor()\n"
  5686. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  5687. " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
  5688. " aaaaaaaaaaaaaa)\n"
  5689. " , aaaaaaaaaaaaaaaaaaaaaaa()\n{\n}",
  5690. Style);
  5691. // Access specifiers should be aligned left.
  5692. verifyFormat("class C {\n"
  5693. "public:\n"
  5694. " int i;\n"
  5695. "};",
  5696. Style);
  5697. // Do not align comments.
  5698. verifyFormat("int a; // Do not\n"
  5699. "double b; // align comments.",
  5700. Style);
  5701. // Accept input's line breaks.
  5702. EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
  5703. " || bbbbbbbbbbbbbbb) {\n"
  5704. " i++;\n"
  5705. "}",
  5706. format("if (aaaaaaaaaaaaaaa\n"
  5707. "|| bbbbbbbbbbbbbbb) { i++; }",
  5708. Style));
  5709. EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
  5710. " i++;\n"
  5711. "}",
  5712. format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
  5713. }
  5714. } // end namespace tooling
  5715. } // end namespace clang