FormatTest.cpp 413 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041
  1. //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "FormatTestUtils.h"
  10. #include "clang/Format/Format.h"
  11. #include "llvm/Support/Debug.h"
  12. #include "gtest/gtest.h"
  13. #define DEBUG_TYPE "format-test"
  14. namespace clang {
  15. namespace format {
  16. namespace {
  17. FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
  18. class FormatTest : public ::testing::Test {
  19. protected:
  20. enum IncompleteCheck {
  21. IC_ExpectComplete,
  22. IC_ExpectIncomplete,
  23. IC_DoNotCheck
  24. };
  25. std::string format(llvm::StringRef Code,
  26. const FormatStyle &Style = getLLVMStyle(),
  27. IncompleteCheck CheckIncomplete = IC_ExpectComplete) {
  28. DEBUG(llvm::errs() << "---\n");
  29. DEBUG(llvm::errs() << Code << "\n\n");
  30. std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
  31. bool IncompleteFormat = false;
  32. tooling::Replacements Replaces =
  33. reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
  34. if (CheckIncomplete != IC_DoNotCheck) {
  35. bool ExpectedIncompleteFormat = CheckIncomplete == IC_ExpectIncomplete;
  36. EXPECT_EQ(ExpectedIncompleteFormat, IncompleteFormat) << Code << "\n\n";
  37. }
  38. ReplacementCount = Replaces.size();
  39. std::string Result = applyAllReplacements(Code, Replaces);
  40. EXPECT_NE("", Result);
  41. DEBUG(llvm::errs() << "\n" << Result << "\n\n");
  42. return Result;
  43. }
  44. FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
  45. FormatStyle Style = getLLVMStyle();
  46. Style.ColumnLimit = ColumnLimit;
  47. return Style;
  48. }
  49. FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
  50. FormatStyle Style = getGoogleStyle();
  51. Style.ColumnLimit = ColumnLimit;
  52. return Style;
  53. }
  54. void verifyFormat(llvm::StringRef Code,
  55. const FormatStyle &Style = getLLVMStyle()) {
  56. EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
  57. }
  58. void verifyIncompleteFormat(llvm::StringRef Code,
  59. const FormatStyle &Style = getLLVMStyle()) {
  60. EXPECT_EQ(Code.str(),
  61. format(test::messUp(Code), Style, IC_ExpectIncomplete));
  62. }
  63. void verifyGoogleFormat(llvm::StringRef Code) {
  64. verifyFormat(Code, getGoogleStyle());
  65. }
  66. void verifyIndependentOfContext(llvm::StringRef text) {
  67. verifyFormat(text);
  68. verifyFormat(llvm::Twine("void f() { " + text + " }").str());
  69. }
  70. /// \brief Verify that clang-format does not crash on the given input.
  71. void verifyNoCrash(llvm::StringRef Code,
  72. const FormatStyle &Style = getLLVMStyle()) {
  73. format(Code, Style, IC_DoNotCheck);
  74. }
  75. int ReplacementCount;
  76. };
  77. TEST_F(FormatTest, MessUp) {
  78. EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
  79. EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
  80. EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
  81. EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
  82. EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
  83. }
  84. //===----------------------------------------------------------------------===//
  85. // Basic function tests.
  86. //===----------------------------------------------------------------------===//
  87. TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
  88. EXPECT_EQ(";", format(";"));
  89. }
  90. TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
  91. EXPECT_EQ("int i;", format(" int i;"));
  92. EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;"));
  93. EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
  94. EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
  95. }
  96. TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
  97. EXPECT_EQ("int i;", format("int\ni;"));
  98. }
  99. TEST_F(FormatTest, FormatsNestedBlockStatements) {
  100. EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
  101. }
  102. TEST_F(FormatTest, FormatsNestedCall) {
  103. verifyFormat("Method(f1, f2(f3));");
  104. verifyFormat("Method(f1(f2, f3()));");
  105. verifyFormat("Method(f1(f2, (f3())));");
  106. }
  107. TEST_F(FormatTest, NestedNameSpecifiers) {
  108. verifyFormat("vector<::Type> v;");
  109. verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
  110. verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
  111. verifyFormat("bool a = 2 < ::SomeFunction();");
  112. }
  113. TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
  114. EXPECT_EQ("if (a) {\n"
  115. " f();\n"
  116. "}",
  117. format("if(a){f();}"));
  118. EXPECT_EQ(4, ReplacementCount);
  119. EXPECT_EQ("if (a) {\n"
  120. " f();\n"
  121. "}",
  122. format("if (a) {\n"
  123. " f();\n"
  124. "}"));
  125. EXPECT_EQ(0, ReplacementCount);
  126. EXPECT_EQ("/*\r\n"
  127. "\r\n"
  128. "*/\r\n",
  129. format("/*\r\n"
  130. "\r\n"
  131. "*/\r\n"));
  132. EXPECT_EQ(0, ReplacementCount);
  133. }
  134. TEST_F(FormatTest, RemovesEmptyLines) {
  135. EXPECT_EQ("class C {\n"
  136. " int i;\n"
  137. "};",
  138. format("class C {\n"
  139. " int i;\n"
  140. "\n"
  141. "};"));
  142. // Don't remove empty lines at the start of namespaces or extern "C" blocks.
  143. EXPECT_EQ("namespace N {\n"
  144. "\n"
  145. "int i;\n"
  146. "}",
  147. format("namespace N {\n"
  148. "\n"
  149. "int i;\n"
  150. "}",
  151. getGoogleStyle()));
  152. EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
  153. "\n"
  154. "int i;\n"
  155. "}",
  156. format("extern /**/ \"C\" /**/ {\n"
  157. "\n"
  158. "int i;\n"
  159. "}",
  160. getGoogleStyle()));
  161. // ...but do keep inlining and removing empty lines for non-block extern "C"
  162. // functions.
  163. verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
  164. EXPECT_EQ("extern \"C\" int f() {\n"
  165. " int i = 42;\n"
  166. " return i;\n"
  167. "}",
  168. format("extern \"C\" int f() {\n"
  169. "\n"
  170. " int i = 42;\n"
  171. " return i;\n"
  172. "}",
  173. getGoogleStyle()));
  174. // Remove empty lines at the beginning and end of blocks.
  175. EXPECT_EQ("void f() {\n"
  176. "\n"
  177. " if (a) {\n"
  178. "\n"
  179. " f();\n"
  180. " }\n"
  181. "}",
  182. format("void f() {\n"
  183. "\n"
  184. " if (a) {\n"
  185. "\n"
  186. " f();\n"
  187. "\n"
  188. " }\n"
  189. "\n"
  190. "}",
  191. getLLVMStyle()));
  192. EXPECT_EQ("void f() {\n"
  193. " if (a) {\n"
  194. " f();\n"
  195. " }\n"
  196. "}",
  197. format("void f() {\n"
  198. "\n"
  199. " if (a) {\n"
  200. "\n"
  201. " f();\n"
  202. "\n"
  203. " }\n"
  204. "\n"
  205. "}",
  206. getGoogleStyle()));
  207. // Don't remove empty lines in more complex control statements.
  208. EXPECT_EQ("void f() {\n"
  209. " if (a) {\n"
  210. " f();\n"
  211. "\n"
  212. " } else if (b) {\n"
  213. " f();\n"
  214. " }\n"
  215. "}",
  216. format("void f() {\n"
  217. " if (a) {\n"
  218. " f();\n"
  219. "\n"
  220. " } else if (b) {\n"
  221. " f();\n"
  222. "\n"
  223. " }\n"
  224. "\n"
  225. "}"));
  226. // FIXME: This is slightly inconsistent.
  227. EXPECT_EQ("namespace {\n"
  228. "int i;\n"
  229. "}",
  230. format("namespace {\n"
  231. "int i;\n"
  232. "\n"
  233. "}"));
  234. EXPECT_EQ("namespace {\n"
  235. "int i;\n"
  236. "\n"
  237. "} // namespace",
  238. format("namespace {\n"
  239. "int i;\n"
  240. "\n"
  241. "} // namespace"));
  242. }
  243. TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
  244. verifyFormat("x = (a) and (b);");
  245. verifyFormat("x = (a) or (b);");
  246. verifyFormat("x = (a) bitand (b);");
  247. verifyFormat("x = (a) bitor (b);");
  248. verifyFormat("x = (a) not_eq (b);");
  249. verifyFormat("x = (a) and_eq (b);");
  250. verifyFormat("x = (a) or_eq (b);");
  251. verifyFormat("x = (a) xor (b);");
  252. }
  253. //===----------------------------------------------------------------------===//
  254. // Tests for control statements.
  255. //===----------------------------------------------------------------------===//
  256. TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
  257. verifyFormat("if (true)\n f();\ng();");
  258. verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
  259. verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
  260. FormatStyle AllowsMergedIf = getLLVMStyle();
  261. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  262. verifyFormat("if (a)\n"
  263. " // comment\n"
  264. " f();",
  265. AllowsMergedIf);
  266. verifyFormat("if (a)\n"
  267. " ;",
  268. AllowsMergedIf);
  269. verifyFormat("if (a)\n"
  270. " if (b) return;",
  271. AllowsMergedIf);
  272. verifyFormat("if (a) // Can't merge this\n"
  273. " f();\n",
  274. AllowsMergedIf);
  275. verifyFormat("if (a) /* still don't merge */\n"
  276. " f();",
  277. AllowsMergedIf);
  278. verifyFormat("if (a) { // Never merge this\n"
  279. " f();\n"
  280. "}",
  281. AllowsMergedIf);
  282. verifyFormat("if (a) { /* Never merge this */\n"
  283. " f();\n"
  284. "}",
  285. AllowsMergedIf);
  286. AllowsMergedIf.ColumnLimit = 14;
  287. verifyFormat("if (a) return;", AllowsMergedIf);
  288. verifyFormat("if (aaaaaaaaa)\n"
  289. " return;",
  290. AllowsMergedIf);
  291. AllowsMergedIf.ColumnLimit = 13;
  292. verifyFormat("if (a)\n return;", AllowsMergedIf);
  293. }
  294. TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
  295. FormatStyle AllowsMergedLoops = getLLVMStyle();
  296. AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
  297. verifyFormat("while (true) continue;", AllowsMergedLoops);
  298. verifyFormat("for (;;) continue;", AllowsMergedLoops);
  299. verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
  300. verifyFormat("while (true)\n"
  301. " ;",
  302. AllowsMergedLoops);
  303. verifyFormat("for (;;)\n"
  304. " ;",
  305. AllowsMergedLoops);
  306. verifyFormat("for (;;)\n"
  307. " for (;;) continue;",
  308. AllowsMergedLoops);
  309. verifyFormat("for (;;) // Can't merge this\n"
  310. " continue;",
  311. AllowsMergedLoops);
  312. verifyFormat("for (;;) /* still don't merge */\n"
  313. " continue;",
  314. AllowsMergedLoops);
  315. }
  316. TEST_F(FormatTest, FormatShortBracedStatements) {
  317. FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
  318. AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
  319. AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
  320. AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
  321. verifyFormat("if (true) {}", AllowSimpleBracedStatements);
  322. verifyFormat("while (true) {}", AllowSimpleBracedStatements);
  323. verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
  324. verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
  325. verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
  326. verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
  327. verifyFormat("if (true) { //\n"
  328. " f();\n"
  329. "}",
  330. AllowSimpleBracedStatements);
  331. verifyFormat("if (true) {\n"
  332. " f();\n"
  333. " f();\n"
  334. "}",
  335. AllowSimpleBracedStatements);
  336. verifyFormat("if (true) {\n"
  337. " f();\n"
  338. "} else {\n"
  339. " f();\n"
  340. "}",
  341. AllowSimpleBracedStatements);
  342. verifyFormat("template <int> struct A2 {\n"
  343. " struct B {};\n"
  344. "};",
  345. AllowSimpleBracedStatements);
  346. AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
  347. verifyFormat("if (true) {\n"
  348. " f();\n"
  349. "}",
  350. AllowSimpleBracedStatements);
  351. verifyFormat("if (true) {\n"
  352. " f();\n"
  353. "} else {\n"
  354. " f();\n"
  355. "}",
  356. AllowSimpleBracedStatements);
  357. AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
  358. verifyFormat("while (true) {\n"
  359. " f();\n"
  360. "}",
  361. AllowSimpleBracedStatements);
  362. verifyFormat("for (;;) {\n"
  363. " f();\n"
  364. "}",
  365. AllowSimpleBracedStatements);
  366. }
  367. TEST_F(FormatTest, ParseIfElse) {
  368. verifyFormat("if (true)\n"
  369. " if (true)\n"
  370. " if (true)\n"
  371. " f();\n"
  372. " else\n"
  373. " g();\n"
  374. " else\n"
  375. " h();\n"
  376. "else\n"
  377. " i();");
  378. verifyFormat("if (true)\n"
  379. " if (true)\n"
  380. " if (true) {\n"
  381. " if (true)\n"
  382. " f();\n"
  383. " } else {\n"
  384. " g();\n"
  385. " }\n"
  386. " else\n"
  387. " h();\n"
  388. "else {\n"
  389. " i();\n"
  390. "}");
  391. verifyFormat("void f() {\n"
  392. " if (a) {\n"
  393. " } else {\n"
  394. " }\n"
  395. "}");
  396. }
  397. TEST_F(FormatTest, ElseIf) {
  398. verifyFormat("if (a) {\n} else if (b) {\n}");
  399. verifyFormat("if (a)\n"
  400. " f();\n"
  401. "else if (b)\n"
  402. " g();\n"
  403. "else\n"
  404. " h();");
  405. verifyFormat("if (a) {\n"
  406. " f();\n"
  407. "}\n"
  408. "// or else ..\n"
  409. "else {\n"
  410. " g()\n"
  411. "}");
  412. verifyFormat("if (a) {\n"
  413. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  414. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  415. "}");
  416. verifyFormat("if (a) {\n"
  417. "} else if (\n"
  418. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  419. "}",
  420. getLLVMStyleWithColumns(62));
  421. }
  422. TEST_F(FormatTest, FormatsForLoop) {
  423. verifyFormat(
  424. "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
  425. " ++VeryVeryLongLoopVariable)\n"
  426. " ;");
  427. verifyFormat("for (;;)\n"
  428. " f();");
  429. verifyFormat("for (;;) {\n}");
  430. verifyFormat("for (;;) {\n"
  431. " f();\n"
  432. "}");
  433. verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
  434. verifyFormat(
  435. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  436. " E = UnwrappedLines.end();\n"
  437. " I != E; ++I) {\n}");
  438. verifyFormat(
  439. "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
  440. " ++IIIII) {\n}");
  441. verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
  442. " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
  443. " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
  444. verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
  445. " I = FD->getDeclsInPrototypeScope().begin(),\n"
  446. " E = FD->getDeclsInPrototypeScope().end();\n"
  447. " I != E; ++I) {\n}");
  448. verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
  449. " I = Container.begin(),\n"
  450. " E = Container.end();\n"
  451. " I != E; ++I) {\n}",
  452. getLLVMStyleWithColumns(76));
  453. verifyFormat(
  454. "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  455. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
  456. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  457. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  458. " ++aaaaaaaaaaa) {\n}");
  459. verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  460. " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
  461. " ++i) {\n}");
  462. verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
  463. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  464. "}");
  465. verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
  466. " aaaaaaaaaa);\n"
  467. " iter; ++iter) {\n"
  468. "}");
  469. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  470. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  471. " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
  472. " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
  473. FormatStyle NoBinPacking = getLLVMStyle();
  474. NoBinPacking.BinPackParameters = false;
  475. verifyFormat("for (int aaaaaaaaaaa = 1;\n"
  476. " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
  477. " aaaaaaaaaaaaaaaa,\n"
  478. " aaaaaaaaaaaaaaaa,\n"
  479. " aaaaaaaaaaaaaaaa);\n"
  480. " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
  481. "}",
  482. NoBinPacking);
  483. verifyFormat(
  484. "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
  485. " E = UnwrappedLines.end();\n"
  486. " I != E;\n"
  487. " ++I) {\n}",
  488. NoBinPacking);
  489. }
  490. TEST_F(FormatTest, RangeBasedForLoops) {
  491. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  492. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  493. verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
  494. " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
  495. verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
  496. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  497. verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
  498. " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
  499. }
  500. TEST_F(FormatTest, ForEachLoops) {
  501. verifyFormat("void f() {\n"
  502. " foreach (Item *item, itemlist) {}\n"
  503. " Q_FOREACH (Item *item, itemlist) {}\n"
  504. " BOOST_FOREACH (Item *item, itemlist) {}\n"
  505. " UNKNOWN_FORACH(Item * item, itemlist) {}\n"
  506. "}");
  507. // As function-like macros.
  508. verifyFormat("#define foreach(x, y)\n"
  509. "#define Q_FOREACH(x, y)\n"
  510. "#define BOOST_FOREACH(x, y)\n"
  511. "#define UNKNOWN_FOREACH(x, y)\n");
  512. // Not as function-like macros.
  513. verifyFormat("#define foreach (x, y)\n"
  514. "#define Q_FOREACH (x, y)\n"
  515. "#define BOOST_FOREACH (x, y)\n"
  516. "#define UNKNOWN_FOREACH (x, y)\n");
  517. }
  518. TEST_F(FormatTest, FormatsWhileLoop) {
  519. verifyFormat("while (true) {\n}");
  520. verifyFormat("while (true)\n"
  521. " f();");
  522. verifyFormat("while () {\n}");
  523. verifyFormat("while () {\n"
  524. " f();\n"
  525. "}");
  526. }
  527. TEST_F(FormatTest, FormatsDoWhile) {
  528. verifyFormat("do {\n"
  529. " do_something();\n"
  530. "} while (something());");
  531. verifyFormat("do\n"
  532. " do_something();\n"
  533. "while (something());");
  534. }
  535. TEST_F(FormatTest, FormatsSwitchStatement) {
  536. verifyFormat("switch (x) {\n"
  537. "case 1:\n"
  538. " f();\n"
  539. " break;\n"
  540. "case kFoo:\n"
  541. "case ns::kBar:\n"
  542. "case kBaz:\n"
  543. " break;\n"
  544. "default:\n"
  545. " g();\n"
  546. " break;\n"
  547. "}");
  548. verifyFormat("switch (x) {\n"
  549. "case 1: {\n"
  550. " f();\n"
  551. " break;\n"
  552. "}\n"
  553. "case 2: {\n"
  554. " break;\n"
  555. "}\n"
  556. "}");
  557. verifyFormat("switch (x) {\n"
  558. "case 1: {\n"
  559. " f();\n"
  560. " {\n"
  561. " g();\n"
  562. " h();\n"
  563. " }\n"
  564. " break;\n"
  565. "}\n"
  566. "}");
  567. verifyFormat("switch (x) {\n"
  568. "case 1: {\n"
  569. " f();\n"
  570. " if (foo) {\n"
  571. " g();\n"
  572. " h();\n"
  573. " }\n"
  574. " break;\n"
  575. "}\n"
  576. "}");
  577. verifyFormat("switch (x) {\n"
  578. "case 1: {\n"
  579. " f();\n"
  580. " g();\n"
  581. "} break;\n"
  582. "}");
  583. verifyFormat("switch (test)\n"
  584. " ;");
  585. verifyFormat("switch (x) {\n"
  586. "default: {\n"
  587. " // Do nothing.\n"
  588. "}\n"
  589. "}");
  590. verifyFormat("switch (x) {\n"
  591. "// comment\n"
  592. "// if 1, do f()\n"
  593. "case 1:\n"
  594. " f();\n"
  595. "}");
  596. verifyFormat("switch (x) {\n"
  597. "case 1:\n"
  598. " // Do amazing stuff\n"
  599. " {\n"
  600. " f();\n"
  601. " g();\n"
  602. " }\n"
  603. " break;\n"
  604. "}");
  605. verifyFormat("#define A \\\n"
  606. " switch (x) { \\\n"
  607. " case a: \\\n"
  608. " foo = b; \\\n"
  609. " }",
  610. getLLVMStyleWithColumns(20));
  611. verifyFormat("#define OPERATION_CASE(name) \\\n"
  612. " case OP_name: \\\n"
  613. " return operations::Operation##name\n",
  614. getLLVMStyleWithColumns(40));
  615. verifyFormat("switch (x) {\n"
  616. "case 1:;\n"
  617. "default:;\n"
  618. " int i;\n"
  619. "}");
  620. verifyGoogleFormat("switch (x) {\n"
  621. " case 1:\n"
  622. " f();\n"
  623. " break;\n"
  624. " case kFoo:\n"
  625. " case ns::kBar:\n"
  626. " case kBaz:\n"
  627. " break;\n"
  628. " default:\n"
  629. " g();\n"
  630. " break;\n"
  631. "}");
  632. verifyGoogleFormat("switch (x) {\n"
  633. " case 1: {\n"
  634. " f();\n"
  635. " break;\n"
  636. " }\n"
  637. "}");
  638. verifyGoogleFormat("switch (test)\n"
  639. " ;");
  640. verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
  641. " case OP_name: \\\n"
  642. " return operations::Operation##name\n");
  643. verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
  644. " // Get the correction operation class.\n"
  645. " switch (OpCode) {\n"
  646. " CASE(Add);\n"
  647. " CASE(Subtract);\n"
  648. " default:\n"
  649. " return operations::Unknown;\n"
  650. " }\n"
  651. "#undef OPERATION_CASE\n"
  652. "}");
  653. verifyFormat("DEBUG({\n"
  654. " switch (x) {\n"
  655. " case A:\n"
  656. " f();\n"
  657. " break;\n"
  658. " // On B:\n"
  659. " case B:\n"
  660. " g();\n"
  661. " break;\n"
  662. " }\n"
  663. "});");
  664. verifyFormat("switch (a) {\n"
  665. "case (b):\n"
  666. " return;\n"
  667. "}");
  668. verifyFormat("switch (a) {\n"
  669. "case some_namespace::\n"
  670. " some_constant:\n"
  671. " return;\n"
  672. "}",
  673. getLLVMStyleWithColumns(34));
  674. }
  675. TEST_F(FormatTest, CaseRanges) {
  676. verifyFormat("switch (x) {\n"
  677. "case 'A' ... 'Z':\n"
  678. "case 1 ... 5:\n"
  679. " break;\n"
  680. "}");
  681. }
  682. TEST_F(FormatTest, ShortCaseLabels) {
  683. FormatStyle Style = getLLVMStyle();
  684. Style.AllowShortCaseLabelsOnASingleLine = true;
  685. verifyFormat("switch (a) {\n"
  686. "case 1: x = 1; break;\n"
  687. "case 2: return;\n"
  688. "case 3:\n"
  689. "case 4:\n"
  690. "case 5: return;\n"
  691. "case 6: // comment\n"
  692. " return;\n"
  693. "case 7:\n"
  694. " // comment\n"
  695. " return;\n"
  696. "case 8:\n"
  697. " x = 8; // comment\n"
  698. " break;\n"
  699. "default: y = 1; break;\n"
  700. "}",
  701. Style);
  702. verifyFormat("switch (a) {\n"
  703. "#if FOO\n"
  704. "case 0: return 0;\n"
  705. "#endif\n"
  706. "}",
  707. Style);
  708. verifyFormat("switch (a) {\n"
  709. "case 1: {\n"
  710. "}\n"
  711. "case 2: {\n"
  712. " return;\n"
  713. "}\n"
  714. "case 3: {\n"
  715. " x = 1;\n"
  716. " return;\n"
  717. "}\n"
  718. "case 4:\n"
  719. " if (x)\n"
  720. " return;\n"
  721. "}",
  722. Style);
  723. Style.ColumnLimit = 21;
  724. verifyFormat("switch (a) {\n"
  725. "case 1: x = 1; break;\n"
  726. "case 2: return;\n"
  727. "case 3:\n"
  728. "case 4:\n"
  729. "case 5: return;\n"
  730. "default:\n"
  731. " y = 1;\n"
  732. " break;\n"
  733. "}",
  734. Style);
  735. }
  736. TEST_F(FormatTest, FormatsLabels) {
  737. verifyFormat("void f() {\n"
  738. " some_code();\n"
  739. "test_label:\n"
  740. " some_other_code();\n"
  741. " {\n"
  742. " some_more_code();\n"
  743. " another_label:\n"
  744. " some_more_code();\n"
  745. " }\n"
  746. "}");
  747. verifyFormat("{\n"
  748. " some_code();\n"
  749. "test_label:\n"
  750. " some_other_code();\n"
  751. "}");
  752. verifyFormat("{\n"
  753. " some_code();\n"
  754. "test_label:;\n"
  755. " int i = 0;\n"
  756. "}");
  757. }
  758. //===----------------------------------------------------------------------===//
  759. // Tests for comments.
  760. //===----------------------------------------------------------------------===//
  761. TEST_F(FormatTest, UnderstandsSingleLineComments) {
  762. verifyFormat("//* */");
  763. verifyFormat("// line 1\n"
  764. "// line 2\n"
  765. "void f() {}\n");
  766. verifyFormat("void f() {\n"
  767. " // Doesn't do anything\n"
  768. "}");
  769. verifyFormat("SomeObject\n"
  770. " // Calling someFunction on SomeObject\n"
  771. " .someFunction();");
  772. verifyFormat("auto result = SomeObject\n"
  773. " // Calling someFunction on SomeObject\n"
  774. " .someFunction();");
  775. verifyFormat("void f(int i, // some comment (probably for i)\n"
  776. " int j, // some comment (probably for j)\n"
  777. " int k); // some comment (probably for k)");
  778. verifyFormat("void f(int i,\n"
  779. " // some comment (probably for j)\n"
  780. " int j,\n"
  781. " // some comment (probably for k)\n"
  782. " int k);");
  783. verifyFormat("int i // This is a fancy variable\n"
  784. " = 5; // with nicely aligned comment.");
  785. verifyFormat("// Leading comment.\n"
  786. "int a; // Trailing comment.");
  787. verifyFormat("int a; // Trailing comment\n"
  788. " // on 2\n"
  789. " // or 3 lines.\n"
  790. "int b;");
  791. verifyFormat("int a; // Trailing comment\n"
  792. "\n"
  793. "// Leading comment.\n"
  794. "int b;");
  795. verifyFormat("int a; // Comment.\n"
  796. " // More details.\n"
  797. "int bbbb; // Another comment.");
  798. verifyFormat(
  799. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  800. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n"
  801. "int cccccccccccccccccccccccccccccc; // comment\n"
  802. "int ddd; // looooooooooooooooooooooooong comment\n"
  803. "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
  804. "int bbbbbbbbbbbbbbbbbbbbb; // comment\n"
  805. "int ccccccccccccccccccc; // comment");
  806. verifyFormat("#include \"a\" // comment\n"
  807. "#include \"a/b/c\" // comment");
  808. verifyFormat("#include <a> // comment\n"
  809. "#include <a/b/c> // comment");
  810. EXPECT_EQ("#include \"a\" // comment\n"
  811. "#include \"a/b/c\" // comment",
  812. format("#include \\\n"
  813. " \"a\" // comment\n"
  814. "#include \"a/b/c\" // comment"));
  815. verifyFormat("enum E {\n"
  816. " // comment\n"
  817. " VAL_A, // comment\n"
  818. " VAL_B\n"
  819. "};");
  820. verifyFormat(
  821. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  822. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
  823. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  824. " // Comment inside a statement.\n"
  825. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  826. verifyFormat("SomeFunction(a,\n"
  827. " // comment\n"
  828. " b + x);");
  829. verifyFormat("SomeFunction(a, a,\n"
  830. " // comment\n"
  831. " b + x);");
  832. verifyFormat(
  833. "bool aaaaaaaaaaaaa = // comment\n"
  834. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  835. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  836. verifyFormat("int aaaa; // aaaaa\n"
  837. "int aa; // aaaaaaa",
  838. getLLVMStyleWithColumns(20));
  839. EXPECT_EQ("void f() { // This does something ..\n"
  840. "}\n"
  841. "int a; // This is unrelated",
  842. format("void f() { // This does something ..\n"
  843. " }\n"
  844. "int a; // This is unrelated"));
  845. EXPECT_EQ("class C {\n"
  846. " void f() { // This does something ..\n"
  847. " } // awesome..\n"
  848. "\n"
  849. " int a; // This is unrelated\n"
  850. "};",
  851. format("class C{void f() { // This does something ..\n"
  852. " } // awesome..\n"
  853. " \n"
  854. "int a; // This is unrelated\n"
  855. "};"));
  856. EXPECT_EQ("int i; // single line trailing comment",
  857. format("int i;\\\n// single line trailing comment"));
  858. verifyGoogleFormat("int a; // Trailing comment.");
  859. verifyFormat("someFunction(anotherFunction( // Force break.\n"
  860. " parameter));");
  861. verifyGoogleFormat("#endif // HEADER_GUARD");
  862. verifyFormat("const char *test[] = {\n"
  863. " // A\n"
  864. " \"aaaa\",\n"
  865. " // B\n"
  866. " \"aaaaa\"};");
  867. verifyGoogleFormat(
  868. "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  869. " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment");
  870. EXPECT_EQ("D(a, {\n"
  871. " // test\n"
  872. " int a;\n"
  873. "});",
  874. format("D(a, {\n"
  875. "// test\n"
  876. "int a;\n"
  877. "});"));
  878. EXPECT_EQ("lineWith(); // comment\n"
  879. "// at start\n"
  880. "otherLine();",
  881. format("lineWith(); // comment\n"
  882. "// at start\n"
  883. "otherLine();"));
  884. EXPECT_EQ("lineWith(); // comment\n"
  885. " // at start\n"
  886. "otherLine();",
  887. format("lineWith(); // comment\n"
  888. " // at start\n"
  889. "otherLine();"));
  890. EXPECT_EQ("lineWith(); // comment\n"
  891. "// at start\n"
  892. "otherLine(); // comment",
  893. format("lineWith(); // comment\n"
  894. "// at start\n"
  895. "otherLine(); // comment"));
  896. EXPECT_EQ("lineWith();\n"
  897. "// at start\n"
  898. "otherLine(); // comment",
  899. format("lineWith();\n"
  900. " // at start\n"
  901. "otherLine(); // comment"));
  902. EXPECT_EQ("// first\n"
  903. "// at start\n"
  904. "otherLine(); // comment",
  905. format("// first\n"
  906. " // at start\n"
  907. "otherLine(); // comment"));
  908. EXPECT_EQ("f();\n"
  909. "// first\n"
  910. "// at start\n"
  911. "otherLine(); // comment",
  912. format("f();\n"
  913. "// first\n"
  914. " // at start\n"
  915. "otherLine(); // comment"));
  916. verifyFormat("f(); // comment\n"
  917. "// first\n"
  918. "// at start\n"
  919. "otherLine();");
  920. EXPECT_EQ("f(); // comment\n"
  921. "// first\n"
  922. "// at start\n"
  923. "otherLine();",
  924. format("f(); // comment\n"
  925. "// first\n"
  926. " // at start\n"
  927. "otherLine();"));
  928. EXPECT_EQ("f(); // comment\n"
  929. " // first\n"
  930. "// at start\n"
  931. "otherLine();",
  932. format("f(); // comment\n"
  933. " // first\n"
  934. "// at start\n"
  935. "otherLine();"));
  936. EXPECT_EQ("void f() {\n"
  937. " lineWith(); // comment\n"
  938. " // at start\n"
  939. "}",
  940. format("void f() {\n"
  941. " lineWith(); // comment\n"
  942. " // at start\n"
  943. "}"));
  944. verifyFormat("#define A \\\n"
  945. " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
  946. " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
  947. getLLVMStyleWithColumns(60));
  948. verifyFormat(
  949. "#define A \\\n"
  950. " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
  951. " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
  952. getLLVMStyleWithColumns(61));
  953. verifyFormat("if ( // This is some comment\n"
  954. " x + 3) {\n"
  955. "}");
  956. EXPECT_EQ("if ( // This is some comment\n"
  957. " // spanning two lines\n"
  958. " x + 3) {\n"
  959. "}",
  960. format("if( // This is some comment\n"
  961. " // spanning two lines\n"
  962. " x + 3) {\n"
  963. "}"));
  964. verifyNoCrash("/\\\n/");
  965. verifyNoCrash("/\\\n* */");
  966. // The 0-character somehow makes the lexer return a proper comment.
  967. verifyNoCrash(StringRef("/*\\\0\n/", 6));
  968. }
  969. TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
  970. EXPECT_EQ("SomeFunction(a,\n"
  971. " b, // comment\n"
  972. " c);",
  973. format("SomeFunction(a,\n"
  974. " b, // comment\n"
  975. " c);"));
  976. EXPECT_EQ("SomeFunction(a, b,\n"
  977. " // comment\n"
  978. " c);",
  979. format("SomeFunction(a,\n"
  980. " b,\n"
  981. " // comment\n"
  982. " c);"));
  983. EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
  984. " c);",
  985. format("SomeFunction(a, b, // comment (unclear relation)\n"
  986. " c);"));
  987. EXPECT_EQ("SomeFunction(a, // comment\n"
  988. " b,\n"
  989. " c); // comment",
  990. format("SomeFunction(a, // comment\n"
  991. " b,\n"
  992. " c); // comment"));
  993. }
  994. TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
  995. EXPECT_EQ("// comment", format("// comment "));
  996. EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
  997. format("int aaaaaaa, bbbbbbb; // comment ",
  998. getLLVMStyleWithColumns(33)));
  999. EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f "));
  1000. EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f "));
  1001. }
  1002. TEST_F(FormatTest, UnderstandsBlockComments) {
  1003. verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
  1004. verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }");
  1005. EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
  1006. " bbbbbbbbbbbbbbbbbbbbbbbbb);",
  1007. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n"
  1008. "/* Trailing comment for aa... */\n"
  1009. " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  1010. EXPECT_EQ(
  1011. "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  1012. " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
  1013. format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
  1014. "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
  1015. EXPECT_EQ(
  1016. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1017. " aaaaaaaaaaaaaaaaaa,\n"
  1018. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  1019. "}",
  1020. format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1021. " aaaaaaaaaaaaaaaaaa ,\n"
  1022. " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
  1023. "}"));
  1024. FormatStyle NoBinPacking = getLLVMStyle();
  1025. NoBinPacking.BinPackParameters = false;
  1026. verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
  1027. " /* parameter 2 */ aaaaaa,\n"
  1028. " /* parameter 3 */ aaaaaa,\n"
  1029. " /* parameter 4 */ aaaaaa);",
  1030. NoBinPacking);
  1031. // Aligning block comments in macros.
  1032. verifyGoogleFormat("#define A \\\n"
  1033. " int i; /*a*/ \\\n"
  1034. " int jjj; /*b*/");
  1035. }
  1036. TEST_F(FormatTest, AlignsBlockComments) {
  1037. EXPECT_EQ("/*\n"
  1038. " * Really multi-line\n"
  1039. " * comment.\n"
  1040. " */\n"
  1041. "void f() {}",
  1042. format(" /*\n"
  1043. " * Really multi-line\n"
  1044. " * comment.\n"
  1045. " */\n"
  1046. " void f() {}"));
  1047. EXPECT_EQ("class C {\n"
  1048. " /*\n"
  1049. " * Another multi-line\n"
  1050. " * comment.\n"
  1051. " */\n"
  1052. " void f() {}\n"
  1053. "};",
  1054. format("class C {\n"
  1055. "/*\n"
  1056. " * Another multi-line\n"
  1057. " * comment.\n"
  1058. " */\n"
  1059. "void f() {}\n"
  1060. "};"));
  1061. EXPECT_EQ("/*\n"
  1062. " 1. This is a comment with non-trivial formatting.\n"
  1063. " 1.1. We have to indent/outdent all lines equally\n"
  1064. " 1.1.1. to keep the formatting.\n"
  1065. " */",
  1066. format(" /*\n"
  1067. " 1. This is a comment with non-trivial formatting.\n"
  1068. " 1.1. We have to indent/outdent all lines equally\n"
  1069. " 1.1.1. to keep the formatting.\n"
  1070. " */"));
  1071. EXPECT_EQ("/*\n"
  1072. "Don't try to outdent if there's not enough indentation.\n"
  1073. "*/",
  1074. format(" /*\n"
  1075. " Don't try to outdent if there's not enough indentation.\n"
  1076. " */"));
  1077. EXPECT_EQ("int i; /* Comment with empty...\n"
  1078. " *\n"
  1079. " * line. */",
  1080. format("int i; /* Comment with empty...\n"
  1081. " *\n"
  1082. " * line. */"));
  1083. EXPECT_EQ("int foobar = 0; /* comment */\n"
  1084. "int bar = 0; /* multiline\n"
  1085. " comment 1 */\n"
  1086. "int baz = 0; /* multiline\n"
  1087. " comment 2 */\n"
  1088. "int bzz = 0; /* multiline\n"
  1089. " comment 3 */",
  1090. format("int foobar = 0; /* comment */\n"
  1091. "int bar = 0; /* multiline\n"
  1092. " comment 1 */\n"
  1093. "int baz = 0; /* multiline\n"
  1094. " comment 2 */\n"
  1095. "int bzz = 0; /* multiline\n"
  1096. " comment 3 */"));
  1097. EXPECT_EQ("int foobar = 0; /* comment */\n"
  1098. "int bar = 0; /* multiline\n"
  1099. " comment */\n"
  1100. "int baz = 0; /* multiline\n"
  1101. "comment */",
  1102. format("int foobar = 0; /* comment */\n"
  1103. "int bar = 0; /* multiline\n"
  1104. "comment */\n"
  1105. "int baz = 0; /* multiline\n"
  1106. "comment */"));
  1107. }
  1108. TEST_F(FormatTest, CommentReflowingCanBeTurnedOff) {
  1109. FormatStyle Style = getLLVMStyleWithColumns(20);
  1110. Style.ReflowComments = false;
  1111. verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style);
  1112. verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style);
  1113. }
  1114. TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
  1115. EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1116. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
  1117. format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  1118. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
  1119. EXPECT_EQ(
  1120. "void ffffffffffff(\n"
  1121. " int aaaaaaaa, int bbbbbbbb,\n"
  1122. " int cccccccccccc) { /*\n"
  1123. " aaaaaaaaaa\n"
  1124. " aaaaaaaaaaaaa\n"
  1125. " bbbbbbbbbbbbbb\n"
  1126. " bbbbbbbbbb\n"
  1127. " */\n"
  1128. "}",
  1129. format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
  1130. "{ /*\n"
  1131. " aaaaaaaaaa aaaaaaaaaaaaa\n"
  1132. " bbbbbbbbbbbbbb bbbbbbbbbb\n"
  1133. " */\n"
  1134. "}",
  1135. getLLVMStyleWithColumns(40)));
  1136. }
  1137. TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
  1138. EXPECT_EQ("void ffffffffff(\n"
  1139. " int aaaaa /* test */);",
  1140. format("void ffffffffff(int aaaaa /* test */);",
  1141. getLLVMStyleWithColumns(35)));
  1142. }
  1143. TEST_F(FormatTest, SplitsLongCxxComments) {
  1144. EXPECT_EQ("// A comment that\n"
  1145. "// doesn't fit on\n"
  1146. "// one line",
  1147. format("// A comment that doesn't fit on one line",
  1148. getLLVMStyleWithColumns(20)));
  1149. EXPECT_EQ("/// A comment that\n"
  1150. "/// doesn't fit on\n"
  1151. "/// one line",
  1152. format("/// A comment that doesn't fit on one line",
  1153. getLLVMStyleWithColumns(20)));
  1154. EXPECT_EQ("//! A comment that\n"
  1155. "//! doesn't fit on\n"
  1156. "//! one line",
  1157. format("//! A comment that doesn't fit on one line",
  1158. getLLVMStyleWithColumns(20)));
  1159. EXPECT_EQ("// a b c d\n"
  1160. "// e f g\n"
  1161. "// h i j k",
  1162. format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10)));
  1163. EXPECT_EQ(
  1164. "// a b c d\n"
  1165. "// e f g\n"
  1166. "// h i j k",
  1167. format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10)));
  1168. EXPECT_EQ("if (true) // A comment that\n"
  1169. " // doesn't fit on\n"
  1170. " // one line",
  1171. format("if (true) // A comment that doesn't fit on one line ",
  1172. getLLVMStyleWithColumns(30)));
  1173. EXPECT_EQ("// Don't_touch_leading_whitespace",
  1174. format("// Don't_touch_leading_whitespace",
  1175. getLLVMStyleWithColumns(20)));
  1176. EXPECT_EQ("// Add leading\n"
  1177. "// whitespace",
  1178. format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
  1179. EXPECT_EQ("/// Add leading\n"
  1180. "/// whitespace",
  1181. format("///Add leading whitespace", getLLVMStyleWithColumns(20)));
  1182. EXPECT_EQ("//! Add leading\n"
  1183. "//! whitespace",
  1184. format("//!Add leading whitespace", getLLVMStyleWithColumns(20)));
  1185. EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
  1186. EXPECT_EQ("// Even if it makes the line exceed the column\n"
  1187. "// limit",
  1188. format("//Even if it makes the line exceed the column limit",
  1189. getLLVMStyleWithColumns(51)));
  1190. EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
  1191. EXPECT_EQ("// aa bb cc dd",
  1192. format("// aa bb cc dd ",
  1193. getLLVMStyleWithColumns(15)));
  1194. EXPECT_EQ("// A comment before\n"
  1195. "// a macro\n"
  1196. "// definition\n"
  1197. "#define a b",
  1198. format("// A comment before a macro definition\n"
  1199. "#define a b",
  1200. getLLVMStyleWithColumns(20)));
  1201. EXPECT_EQ("void ffffff(\n"
  1202. " int aaaaaaaaa, // wwww\n"
  1203. " int bbbbbbbbbb, // xxxxxxx\n"
  1204. " // yyyyyyyyyy\n"
  1205. " int c, int d, int e) {}",
  1206. format("void ffffff(\n"
  1207. " int aaaaaaaaa, // wwww\n"
  1208. " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
  1209. " int c, int d, int e) {}",
  1210. getLLVMStyleWithColumns(40)));
  1211. EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1212. format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1213. getLLVMStyleWithColumns(20)));
  1214. EXPECT_EQ(
  1215. "#define XXX // a b c d\n"
  1216. " // e f g h",
  1217. format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
  1218. EXPECT_EQ(
  1219. "#define XXX // q w e r\n"
  1220. " // t y u i",
  1221. format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
  1222. }
  1223. TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {
  1224. EXPECT_EQ("// A comment\n"
  1225. "// that doesn't\n"
  1226. "// fit on one\n"
  1227. "// line",
  1228. format("// A comment that doesn't fit on one line",
  1229. getLLVMStyleWithColumns(20)));
  1230. EXPECT_EQ("/// A comment\n"
  1231. "/// that doesn't\n"
  1232. "/// fit on one\n"
  1233. "/// line",
  1234. format("/// A comment that doesn't fit on one line",
  1235. getLLVMStyleWithColumns(20)));
  1236. }
  1237. TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
  1238. EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1239. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1240. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  1241. format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1242. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  1243. "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
  1244. EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1245. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1246. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1247. format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1248. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1249. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1250. getLLVMStyleWithColumns(50)));
  1251. // FIXME: One day we might want to implement adjustment of leading whitespace
  1252. // of the consecutive lines in this kind of comment:
  1253. EXPECT_EQ("double\n"
  1254. " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1255. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1256. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1257. format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1258. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
  1259. " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  1260. getLLVMStyleWithColumns(49)));
  1261. }
  1262. TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) {
  1263. FormatStyle Pragmas = getLLVMStyleWithColumns(30);
  1264. Pragmas.CommentPragmas = "^ IWYU pragma:";
  1265. EXPECT_EQ(
  1266. "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
  1267. format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
  1268. EXPECT_EQ(
  1269. "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
  1270. format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
  1271. }
  1272. TEST_F(FormatTest, PriorityOfCommentBreaking) {
  1273. EXPECT_EQ("if (xxx ==\n"
  1274. " yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  1275. " zzz)\n"
  1276. " q();",
  1277. format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
  1278. " zzz) q();",
  1279. getLLVMStyleWithColumns(40)));
  1280. EXPECT_EQ("if (xxxxxxxxxx ==\n"
  1281. " yyy && // aaaaaa bbbbbbbb cccc\n"
  1282. " zzz)\n"
  1283. " q();",
  1284. format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
  1285. " zzz) q();",
  1286. getLLVMStyleWithColumns(40)));
  1287. EXPECT_EQ("if (xxxxxxxxxx &&\n"
  1288. " yyy || // aaaaaa bbbbbbbb cccc\n"
  1289. " zzz)\n"
  1290. " q();",
  1291. format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
  1292. " zzz) q();",
  1293. getLLVMStyleWithColumns(40)));
  1294. EXPECT_EQ("fffffffff(\n"
  1295. " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  1296. " zzz);",
  1297. format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
  1298. " zzz);",
  1299. getLLVMStyleWithColumns(40)));
  1300. }
  1301. TEST_F(FormatTest, MultiLineCommentsInDefines) {
  1302. EXPECT_EQ("#define A(x) /* \\\n"
  1303. " a comment \\\n"
  1304. " inside */ \\\n"
  1305. " f();",
  1306. format("#define A(x) /* \\\n"
  1307. " a comment \\\n"
  1308. " inside */ \\\n"
  1309. " f();",
  1310. getLLVMStyleWithColumns(17)));
  1311. EXPECT_EQ("#define A( \\\n"
  1312. " x) /* \\\n"
  1313. " a comment \\\n"
  1314. " inside */ \\\n"
  1315. " f();",
  1316. format("#define A( \\\n"
  1317. " x) /* \\\n"
  1318. " a comment \\\n"
  1319. " inside */ \\\n"
  1320. " f();",
  1321. getLLVMStyleWithColumns(17)));
  1322. }
  1323. TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
  1324. EXPECT_EQ("namespace {}\n// Test\n#define A",
  1325. format("namespace {}\n // Test\n#define A"));
  1326. EXPECT_EQ("namespace {}\n/* Test */\n#define A",
  1327. format("namespace {}\n /* Test */\n#define A"));
  1328. EXPECT_EQ("namespace {}\n/* Test */ #define A",
  1329. format("namespace {}\n /* Test */ #define A"));
  1330. }
  1331. TEST_F(FormatTest, SplitsLongLinesInComments) {
  1332. EXPECT_EQ("/* This is a long\n"
  1333. " * comment that\n"
  1334. " * doesn't\n"
  1335. " * fit on one line.\n"
  1336. " */",
  1337. format("/* "
  1338. "This is a long "
  1339. "comment that "
  1340. "doesn't "
  1341. "fit on one line. */",
  1342. getLLVMStyleWithColumns(20)));
  1343. EXPECT_EQ(
  1344. "/* a b c d\n"
  1345. " * e f g\n"
  1346. " * h i j k\n"
  1347. " */",
  1348. format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
  1349. EXPECT_EQ(
  1350. "/* a b c d\n"
  1351. " * e f g\n"
  1352. " * h i j k\n"
  1353. " */",
  1354. format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
  1355. EXPECT_EQ("/*\n"
  1356. "This is a long\n"
  1357. "comment that doesn't\n"
  1358. "fit on one line.\n"
  1359. "*/",
  1360. format("/*\n"
  1361. "This is a long "
  1362. "comment that doesn't "
  1363. "fit on one line. \n"
  1364. "*/",
  1365. getLLVMStyleWithColumns(20)));
  1366. EXPECT_EQ("/*\n"
  1367. " * This is a long\n"
  1368. " * comment that\n"
  1369. " * doesn't fit on\n"
  1370. " * one line.\n"
  1371. " */",
  1372. format("/* \n"
  1373. " * This is a long "
  1374. " comment that "
  1375. " doesn't fit on "
  1376. " one line. \n"
  1377. " */",
  1378. getLLVMStyleWithColumns(20)));
  1379. EXPECT_EQ("/*\n"
  1380. " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
  1381. " * so_it_should_be_broken\n"
  1382. " * wherever_a_space_occurs\n"
  1383. " */",
  1384. format("/*\n"
  1385. " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
  1386. " so_it_should_be_broken "
  1387. " wherever_a_space_occurs \n"
  1388. " */",
  1389. getLLVMStyleWithColumns(20)));
  1390. EXPECT_EQ("/*\n"
  1391. " * This_comment_can_not_be_broken_into_lines\n"
  1392. " */",
  1393. format("/*\n"
  1394. " * This_comment_can_not_be_broken_into_lines\n"
  1395. " */",
  1396. getLLVMStyleWithColumns(20)));
  1397. EXPECT_EQ("{\n"
  1398. " /*\n"
  1399. " This is another\n"
  1400. " long comment that\n"
  1401. " doesn't fit on one\n"
  1402. " line 1234567890\n"
  1403. " */\n"
  1404. "}",
  1405. format("{\n"
  1406. "/*\n"
  1407. "This is another "
  1408. " long comment that "
  1409. " doesn't fit on one"
  1410. " line 1234567890\n"
  1411. "*/\n"
  1412. "}",
  1413. getLLVMStyleWithColumns(20)));
  1414. EXPECT_EQ("{\n"
  1415. " /*\n"
  1416. " * This i s\n"
  1417. " * another comment\n"
  1418. " * t hat doesn' t\n"
  1419. " * fit on one l i\n"
  1420. " * n e\n"
  1421. " */\n"
  1422. "}",
  1423. format("{\n"
  1424. "/*\n"
  1425. " * This i s"
  1426. " another comment"
  1427. " t hat doesn' t"
  1428. " fit on one l i"
  1429. " n e\n"
  1430. " */\n"
  1431. "}",
  1432. getLLVMStyleWithColumns(20)));
  1433. EXPECT_EQ("/*\n"
  1434. " * This is a long\n"
  1435. " * comment that\n"
  1436. " * doesn't fit on\n"
  1437. " * one line\n"
  1438. " */",
  1439. format(" /*\n"
  1440. " * This is a long comment that doesn't fit on one line\n"
  1441. " */",
  1442. getLLVMStyleWithColumns(20)));
  1443. EXPECT_EQ("{\n"
  1444. " if (something) /* This is a\n"
  1445. " long\n"
  1446. " comment */\n"
  1447. " ;\n"
  1448. "}",
  1449. format("{\n"
  1450. " if (something) /* This is a long comment */\n"
  1451. " ;\n"
  1452. "}",
  1453. getLLVMStyleWithColumns(30)));
  1454. EXPECT_EQ("/* A comment before\n"
  1455. " * a macro\n"
  1456. " * definition */\n"
  1457. "#define a b",
  1458. format("/* A comment before a macro definition */\n"
  1459. "#define a b",
  1460. getLLVMStyleWithColumns(20)));
  1461. EXPECT_EQ("/* some comment\n"
  1462. " * a comment\n"
  1463. "* that we break\n"
  1464. " * another comment\n"
  1465. "* we have to break\n"
  1466. "* a left comment\n"
  1467. " */",
  1468. format(" /* some comment\n"
  1469. " * a comment that we break\n"
  1470. " * another comment we have to break\n"
  1471. "* a left comment\n"
  1472. " */",
  1473. getLLVMStyleWithColumns(20)));
  1474. EXPECT_EQ("/**\n"
  1475. " * multiline block\n"
  1476. " * comment\n"
  1477. " *\n"
  1478. " */",
  1479. format("/**\n"
  1480. " * multiline block comment\n"
  1481. " *\n"
  1482. " */",
  1483. getLLVMStyleWithColumns(20)));
  1484. EXPECT_EQ("/*\n"
  1485. "\n"
  1486. "\n"
  1487. " */\n",
  1488. format(" /* \n"
  1489. " \n"
  1490. " \n"
  1491. " */\n"));
  1492. EXPECT_EQ("/* a a */",
  1493. format("/* a a */", getLLVMStyleWithColumns(15)));
  1494. EXPECT_EQ("/* a a bc */",
  1495. format("/* a a bc */", getLLVMStyleWithColumns(15)));
  1496. EXPECT_EQ("/* aaa aaa\n"
  1497. " * aaaaa */",
  1498. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1499. EXPECT_EQ("/* aaa aaa\n"
  1500. " * aaaaa */",
  1501. format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
  1502. }
  1503. TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
  1504. EXPECT_EQ("#define X \\\n"
  1505. " /* \\\n"
  1506. " Test \\\n"
  1507. " Macro comment \\\n"
  1508. " with a long \\\n"
  1509. " line \\\n"
  1510. " */ \\\n"
  1511. " A + B",
  1512. format("#define X \\\n"
  1513. " /*\n"
  1514. " Test\n"
  1515. " Macro comment with a long line\n"
  1516. " */ \\\n"
  1517. " A + B",
  1518. getLLVMStyleWithColumns(20)));
  1519. EXPECT_EQ("#define X \\\n"
  1520. " /* Macro comment \\\n"
  1521. " with a long \\\n"
  1522. " line */ \\\n"
  1523. " A + B",
  1524. format("#define X \\\n"
  1525. " /* Macro comment with a long\n"
  1526. " line */ \\\n"
  1527. " A + B",
  1528. getLLVMStyleWithColumns(20)));
  1529. EXPECT_EQ("#define X \\\n"
  1530. " /* Macro comment \\\n"
  1531. " * with a long \\\n"
  1532. " * line */ \\\n"
  1533. " A + B",
  1534. format("#define X \\\n"
  1535. " /* Macro comment with a long line */ \\\n"
  1536. " A + B",
  1537. getLLVMStyleWithColumns(20)));
  1538. }
  1539. TEST_F(FormatTest, CommentsInStaticInitializers) {
  1540. EXPECT_EQ(
  1541. "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
  1542. " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
  1543. " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
  1544. " aaaaaaaaaaaaaaaaaaaa, // comment\n"
  1545. " aaaaaaaaaaaaaaaaaaaa};",
  1546. format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
  1547. " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
  1548. " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
  1549. " aaaaaaaaaaaaaaaaaaaa , // comment\n"
  1550. " aaaaaaaaaaaaaaaaaaaa };"));
  1551. verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1552. " bbbbbbbbbbb, ccccccccccc};");
  1553. verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1554. " // comment for bb....\n"
  1555. " bbbbbbbbbbb, ccccccccccc};");
  1556. verifyGoogleFormat(
  1557. "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
  1558. " bbbbbbbbbbb, ccccccccccc};");
  1559. verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
  1560. " // comment for bb....\n"
  1561. " bbbbbbbbbbb, ccccccccccc};");
  1562. verifyFormat("S s = {{a, b, c}, // Group #1\n"
  1563. " {d, e, f}, // Group #2\n"
  1564. " {g, h, i}}; // Group #3");
  1565. verifyFormat("S s = {{// Group #1\n"
  1566. " a, b, c},\n"
  1567. " {// Group #2\n"
  1568. " d, e, f},\n"
  1569. " {// Group #3\n"
  1570. " g, h, i}};");
  1571. EXPECT_EQ("S s = {\n"
  1572. " // Some comment\n"
  1573. " a,\n"
  1574. "\n"
  1575. " // Comment after empty line\n"
  1576. " b}",
  1577. format("S s = {\n"
  1578. " // Some comment\n"
  1579. " a,\n"
  1580. " \n"
  1581. " // Comment after empty line\n"
  1582. " b\n"
  1583. "}"));
  1584. EXPECT_EQ("S s = {\n"
  1585. " /* Some comment */\n"
  1586. " a,\n"
  1587. "\n"
  1588. " /* Comment after empty line */\n"
  1589. " b}",
  1590. format("S s = {\n"
  1591. " /* Some comment */\n"
  1592. " a,\n"
  1593. " \n"
  1594. " /* Comment after empty line */\n"
  1595. " b\n"
  1596. "}"));
  1597. verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
  1598. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1599. " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
  1600. " 0x00, 0x00, 0x00, 0x00}; // comment\n");
  1601. }
  1602. TEST_F(FormatTest, IgnoresIf0Contents) {
  1603. EXPECT_EQ("#if 0\n"
  1604. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1605. "#endif\n"
  1606. "void f() {}",
  1607. format("#if 0\n"
  1608. "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
  1609. "#endif\n"
  1610. "void f( ) { }"));
  1611. EXPECT_EQ("#if false\n"
  1612. "void f( ) { }\n"
  1613. "#endif\n"
  1614. "void g() {}\n",
  1615. format("#if false\n"
  1616. "void f( ) { }\n"
  1617. "#endif\n"
  1618. "void g( ) { }\n"));
  1619. EXPECT_EQ("enum E {\n"
  1620. " One,\n"
  1621. " Two,\n"
  1622. "#if 0\n"
  1623. "Three,\n"
  1624. " Four,\n"
  1625. "#endif\n"
  1626. " Five\n"
  1627. "};",
  1628. format("enum E {\n"
  1629. " One,Two,\n"
  1630. "#if 0\n"
  1631. "Three,\n"
  1632. " Four,\n"
  1633. "#endif\n"
  1634. " Five};"));
  1635. EXPECT_EQ("enum F {\n"
  1636. " One,\n"
  1637. "#if 1\n"
  1638. " Two,\n"
  1639. "#if 0\n"
  1640. "Three,\n"
  1641. " Four,\n"
  1642. "#endif\n"
  1643. " Five\n"
  1644. "#endif\n"
  1645. "};",
  1646. format("enum F {\n"
  1647. "One,\n"
  1648. "#if 1\n"
  1649. "Two,\n"
  1650. "#if 0\n"
  1651. "Three,\n"
  1652. " Four,\n"
  1653. "#endif\n"
  1654. "Five\n"
  1655. "#endif\n"
  1656. "};"));
  1657. EXPECT_EQ("enum G {\n"
  1658. " One,\n"
  1659. "#if 0\n"
  1660. "Two,\n"
  1661. "#else\n"
  1662. " Three,\n"
  1663. "#endif\n"
  1664. " Four\n"
  1665. "};",
  1666. format("enum G {\n"
  1667. "One,\n"
  1668. "#if 0\n"
  1669. "Two,\n"
  1670. "#else\n"
  1671. "Three,\n"
  1672. "#endif\n"
  1673. "Four\n"
  1674. "};"));
  1675. EXPECT_EQ("enum H {\n"
  1676. " One,\n"
  1677. "#if 0\n"
  1678. "#ifdef Q\n"
  1679. "Two,\n"
  1680. "#else\n"
  1681. "Three,\n"
  1682. "#endif\n"
  1683. "#endif\n"
  1684. " Four\n"
  1685. "};",
  1686. format("enum H {\n"
  1687. "One,\n"
  1688. "#if 0\n"
  1689. "#ifdef Q\n"
  1690. "Two,\n"
  1691. "#else\n"
  1692. "Three,\n"
  1693. "#endif\n"
  1694. "#endif\n"
  1695. "Four\n"
  1696. "};"));
  1697. EXPECT_EQ("enum I {\n"
  1698. " One,\n"
  1699. "#if /* test */ 0 || 1\n"
  1700. "Two,\n"
  1701. "Three,\n"
  1702. "#endif\n"
  1703. " Four\n"
  1704. "};",
  1705. format("enum I {\n"
  1706. "One,\n"
  1707. "#if /* test */ 0 || 1\n"
  1708. "Two,\n"
  1709. "Three,\n"
  1710. "#endif\n"
  1711. "Four\n"
  1712. "};"));
  1713. EXPECT_EQ("enum J {\n"
  1714. " One,\n"
  1715. "#if 0\n"
  1716. "#if 0\n"
  1717. "Two,\n"
  1718. "#else\n"
  1719. "Three,\n"
  1720. "#endif\n"
  1721. "Four,\n"
  1722. "#endif\n"
  1723. " Five\n"
  1724. "};",
  1725. format("enum J {\n"
  1726. "One,\n"
  1727. "#if 0\n"
  1728. "#if 0\n"
  1729. "Two,\n"
  1730. "#else\n"
  1731. "Three,\n"
  1732. "#endif\n"
  1733. "Four,\n"
  1734. "#endif\n"
  1735. "Five\n"
  1736. "};"));
  1737. }
  1738. //===----------------------------------------------------------------------===//
  1739. // Tests for classes, namespaces, etc.
  1740. //===----------------------------------------------------------------------===//
  1741. TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
  1742. verifyFormat("class A {};");
  1743. }
  1744. TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
  1745. verifyFormat("class A {\n"
  1746. "public:\n"
  1747. "public: // comment\n"
  1748. "protected:\n"
  1749. "private:\n"
  1750. " void f() {}\n"
  1751. "};");
  1752. verifyGoogleFormat("class A {\n"
  1753. " public:\n"
  1754. " protected:\n"
  1755. " private:\n"
  1756. " void f() {}\n"
  1757. "};");
  1758. verifyFormat("class A {\n"
  1759. "public slots:\n"
  1760. " void f1() {}\n"
  1761. "public Q_SLOTS:\n"
  1762. " void f2() {}\n"
  1763. "protected slots:\n"
  1764. " void f3() {}\n"
  1765. "protected Q_SLOTS:\n"
  1766. " void f4() {}\n"
  1767. "private slots:\n"
  1768. " void f5() {}\n"
  1769. "private Q_SLOTS:\n"
  1770. " void f6() {}\n"
  1771. "signals:\n"
  1772. " void g1();\n"
  1773. "Q_SIGNALS:\n"
  1774. " void g2();\n"
  1775. "};");
  1776. // Don't interpret 'signals' the wrong way.
  1777. verifyFormat("signals.set();");
  1778. verifyFormat("for (Signals signals : f()) {\n}");
  1779. verifyFormat("{\n"
  1780. " signals.set(); // This needs indentation.\n"
  1781. "}");
  1782. }
  1783. TEST_F(FormatTest, SeparatesLogicalBlocks) {
  1784. EXPECT_EQ("class A {\n"
  1785. "public:\n"
  1786. " void f();\n"
  1787. "\n"
  1788. "private:\n"
  1789. " void g() {}\n"
  1790. " // test\n"
  1791. "protected:\n"
  1792. " int h;\n"
  1793. "};",
  1794. format("class A {\n"
  1795. "public:\n"
  1796. "void f();\n"
  1797. "private:\n"
  1798. "void g() {}\n"
  1799. "// test\n"
  1800. "protected:\n"
  1801. "int h;\n"
  1802. "};"));
  1803. EXPECT_EQ("class A {\n"
  1804. "protected:\n"
  1805. "public:\n"
  1806. " void f();\n"
  1807. "};",
  1808. format("class A {\n"
  1809. "protected:\n"
  1810. "\n"
  1811. "public:\n"
  1812. "\n"
  1813. " void f();\n"
  1814. "};"));
  1815. // Even ensure proper spacing inside macros.
  1816. EXPECT_EQ("#define B \\\n"
  1817. " class A { \\\n"
  1818. " protected: \\\n"
  1819. " public: \\\n"
  1820. " void f(); \\\n"
  1821. " };",
  1822. format("#define B \\\n"
  1823. " class A { \\\n"
  1824. " protected: \\\n"
  1825. " \\\n"
  1826. " public: \\\n"
  1827. " \\\n"
  1828. " void f(); \\\n"
  1829. " };",
  1830. getGoogleStyle()));
  1831. // But don't remove empty lines after macros ending in access specifiers.
  1832. EXPECT_EQ("#define A private:\n"
  1833. "\n"
  1834. "int i;",
  1835. format("#define A private:\n"
  1836. "\n"
  1837. "int i;"));
  1838. }
  1839. TEST_F(FormatTest, FormatsClasses) {
  1840. verifyFormat("class A : public B {};");
  1841. verifyFormat("class A : public ::B {};");
  1842. verifyFormat(
  1843. "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1844. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1845. verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
  1846. " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
  1847. " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
  1848. verifyFormat(
  1849. "class A : public B, public C, public D, public E, public F {};");
  1850. verifyFormat("class AAAAAAAAAAAA : public B,\n"
  1851. " public C,\n"
  1852. " public D,\n"
  1853. " public E,\n"
  1854. " public F,\n"
  1855. " public G {};");
  1856. verifyFormat("class\n"
  1857. " ReallyReallyLongClassName {\n"
  1858. " int i;\n"
  1859. "};",
  1860. getLLVMStyleWithColumns(32));
  1861. verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
  1862. " aaaaaaaaaaaaaaaa> {};");
  1863. verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
  1864. " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
  1865. " aaaaaaaaaaaaaaaaaaaaaa> {};");
  1866. verifyFormat("template <class R, class C>\n"
  1867. "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
  1868. " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
  1869. verifyFormat("class ::A::B {};");
  1870. }
  1871. TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
  1872. verifyFormat("class A {\n} a, b;");
  1873. verifyFormat("struct A {\n} a, b;");
  1874. verifyFormat("union A {\n} a;");
  1875. }
  1876. TEST_F(FormatTest, FormatsEnum) {
  1877. verifyFormat("enum {\n"
  1878. " Zero,\n"
  1879. " One = 1,\n"
  1880. " Two = One + 1,\n"
  1881. " Three = (One + Two),\n"
  1882. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1883. " Five = (One, Two, Three, Four, 5)\n"
  1884. "};");
  1885. verifyGoogleFormat("enum {\n"
  1886. " Zero,\n"
  1887. " One = 1,\n"
  1888. " Two = One + 1,\n"
  1889. " Three = (One + Two),\n"
  1890. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1891. " Five = (One, Two, Three, Four, 5)\n"
  1892. "};");
  1893. verifyFormat("enum Enum {};");
  1894. verifyFormat("enum {};");
  1895. verifyFormat("enum X E {} d;");
  1896. verifyFormat("enum __attribute__((...)) E {} d;");
  1897. verifyFormat("enum __declspec__((...)) E {} d;");
  1898. verifyFormat("enum {\n"
  1899. " Bar = Foo<int, int>::value\n"
  1900. "};",
  1901. getLLVMStyleWithColumns(30));
  1902. verifyFormat("enum ShortEnum { A, B, C };");
  1903. verifyGoogleFormat("enum ShortEnum { A, B, C };");
  1904. EXPECT_EQ("enum KeepEmptyLines {\n"
  1905. " ONE,\n"
  1906. "\n"
  1907. " TWO,\n"
  1908. "\n"
  1909. " THREE\n"
  1910. "}",
  1911. format("enum KeepEmptyLines {\n"
  1912. " ONE,\n"
  1913. "\n"
  1914. " TWO,\n"
  1915. "\n"
  1916. "\n"
  1917. " THREE\n"
  1918. "}"));
  1919. verifyFormat("enum E { // comment\n"
  1920. " ONE,\n"
  1921. " TWO\n"
  1922. "};\n"
  1923. "int i;");
  1924. // Not enums.
  1925. verifyFormat("enum X f() {\n"
  1926. " a();\n"
  1927. " return 42;\n"
  1928. "}");
  1929. verifyFormat("enum X Type::f() {\n"
  1930. " a();\n"
  1931. " return 42;\n"
  1932. "}");
  1933. verifyFormat("enum ::X f() {\n"
  1934. " a();\n"
  1935. " return 42;\n"
  1936. "}");
  1937. verifyFormat("enum ns::X f() {\n"
  1938. " a();\n"
  1939. " return 42;\n"
  1940. "}");
  1941. }
  1942. TEST_F(FormatTest, FormatsEnumsWithErrors) {
  1943. verifyFormat("enum Type {\n"
  1944. " One = 0; // These semicolons should be commas.\n"
  1945. " Two = 1;\n"
  1946. "};");
  1947. verifyFormat("namespace n {\n"
  1948. "enum Type {\n"
  1949. " One,\n"
  1950. " Two, // missing };\n"
  1951. " int i;\n"
  1952. "}\n"
  1953. "void g() {}");
  1954. }
  1955. TEST_F(FormatTest, FormatsEnumStruct) {
  1956. verifyFormat("enum struct {\n"
  1957. " Zero,\n"
  1958. " One = 1,\n"
  1959. " Two = One + 1,\n"
  1960. " Three = (One + Two),\n"
  1961. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1962. " Five = (One, Two, Three, Four, 5)\n"
  1963. "};");
  1964. verifyFormat("enum struct Enum {};");
  1965. verifyFormat("enum struct {};");
  1966. verifyFormat("enum struct X E {} d;");
  1967. verifyFormat("enum struct __attribute__((...)) E {} d;");
  1968. verifyFormat("enum struct __declspec__((...)) E {} d;");
  1969. verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
  1970. }
  1971. TEST_F(FormatTest, FormatsEnumClass) {
  1972. verifyFormat("enum class {\n"
  1973. " Zero,\n"
  1974. " One = 1,\n"
  1975. " Two = One + 1,\n"
  1976. " Three = (One + Two),\n"
  1977. " Four = (Zero && (One ^ Two)) | (One << Two),\n"
  1978. " Five = (One, Two, Three, Four, 5)\n"
  1979. "};");
  1980. verifyFormat("enum class Enum {};");
  1981. verifyFormat("enum class {};");
  1982. verifyFormat("enum class X E {} d;");
  1983. verifyFormat("enum class __attribute__((...)) E {} d;");
  1984. verifyFormat("enum class __declspec__((...)) E {} d;");
  1985. verifyFormat("enum class X f() {\n a();\n return 42;\n}");
  1986. }
  1987. TEST_F(FormatTest, FormatsEnumTypes) {
  1988. verifyFormat("enum X : int {\n"
  1989. " A, // Force multiple lines.\n"
  1990. " B\n"
  1991. "};");
  1992. verifyFormat("enum X : int { A, B };");
  1993. verifyFormat("enum X : std::uint32_t { A, B };");
  1994. }
  1995. TEST_F(FormatTest, FormatsNSEnums) {
  1996. verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
  1997. verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
  1998. " // Information about someDecentlyLongValue.\n"
  1999. " someDecentlyLongValue,\n"
  2000. " // Information about anotherDecentlyLongValue.\n"
  2001. " anotherDecentlyLongValue,\n"
  2002. " // Information about aThirdDecentlyLongValue.\n"
  2003. " aThirdDecentlyLongValue\n"
  2004. "};");
  2005. verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
  2006. " a = 1,\n"
  2007. " b = 2,\n"
  2008. " c = 3,\n"
  2009. "};");
  2010. verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
  2011. " a = 1,\n"
  2012. " b = 2,\n"
  2013. " c = 3,\n"
  2014. "};");
  2015. verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
  2016. " a = 1,\n"
  2017. " b = 2,\n"
  2018. " c = 3,\n"
  2019. "};");
  2020. }
  2021. TEST_F(FormatTest, FormatsBitfields) {
  2022. verifyFormat("struct Bitfields {\n"
  2023. " unsigned sClass : 8;\n"
  2024. " unsigned ValueKind : 2;\n"
  2025. "};");
  2026. verifyFormat("struct A {\n"
  2027. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
  2028. " bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
  2029. "};");
  2030. verifyFormat("struct MyStruct {\n"
  2031. " uchar data;\n"
  2032. " uchar : 8;\n"
  2033. " uchar : 8;\n"
  2034. " uchar other;\n"
  2035. "};");
  2036. }
  2037. TEST_F(FormatTest, FormatsNamespaces) {
  2038. verifyFormat("namespace some_namespace {\n"
  2039. "class A {};\n"
  2040. "void f() { f(); }\n"
  2041. "}");
  2042. verifyFormat("namespace {\n"
  2043. "class A {};\n"
  2044. "void f() { f(); }\n"
  2045. "}");
  2046. verifyFormat("inline namespace X {\n"
  2047. "class A {};\n"
  2048. "void f() { f(); }\n"
  2049. "}");
  2050. verifyFormat("using namespace some_namespace;\n"
  2051. "class A {};\n"
  2052. "void f() { f(); }");
  2053. // This code is more common than we thought; if we
  2054. // layout this correctly the semicolon will go into
  2055. // its own line, which is undesirable.
  2056. verifyFormat("namespace {};");
  2057. verifyFormat("namespace {\n"
  2058. "class A {};\n"
  2059. "};");
  2060. verifyFormat("namespace {\n"
  2061. "int SomeVariable = 0; // comment\n"
  2062. "} // namespace");
  2063. EXPECT_EQ("#ifndef HEADER_GUARD\n"
  2064. "#define HEADER_GUARD\n"
  2065. "namespace my_namespace {\n"
  2066. "int i;\n"
  2067. "} // my_namespace\n"
  2068. "#endif // HEADER_GUARD",
  2069. format("#ifndef HEADER_GUARD\n"
  2070. " #define HEADER_GUARD\n"
  2071. " namespace my_namespace {\n"
  2072. "int i;\n"
  2073. "} // my_namespace\n"
  2074. "#endif // HEADER_GUARD"));
  2075. EXPECT_EQ("namespace A::B {\n"
  2076. "class C {};\n"
  2077. "}",
  2078. format("namespace A::B {\n"
  2079. "class C {};\n"
  2080. "}"));
  2081. FormatStyle Style = getLLVMStyle();
  2082. Style.NamespaceIndentation = FormatStyle::NI_All;
  2083. EXPECT_EQ("namespace out {\n"
  2084. " int i;\n"
  2085. " namespace in {\n"
  2086. " int i;\n"
  2087. " } // namespace\n"
  2088. "} // namespace",
  2089. format("namespace out {\n"
  2090. "int i;\n"
  2091. "namespace in {\n"
  2092. "int i;\n"
  2093. "} // namespace\n"
  2094. "} // namespace",
  2095. Style));
  2096. Style.NamespaceIndentation = FormatStyle::NI_Inner;
  2097. EXPECT_EQ("namespace out {\n"
  2098. "int i;\n"
  2099. "namespace in {\n"
  2100. " int i;\n"
  2101. "} // namespace\n"
  2102. "} // namespace",
  2103. format("namespace out {\n"
  2104. "int i;\n"
  2105. "namespace in {\n"
  2106. "int i;\n"
  2107. "} // namespace\n"
  2108. "} // namespace",
  2109. Style));
  2110. }
  2111. TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
  2112. TEST_F(FormatTest, FormatsInlineASM) {
  2113. verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
  2114. verifyFormat("asm(\"nop\" ::: \"memory\");");
  2115. verifyFormat(
  2116. "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
  2117. " \"cpuid\\n\\t\"\n"
  2118. " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
  2119. " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
  2120. " : \"a\"(value));");
  2121. EXPECT_EQ(
  2122. "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
  2123. " __asm {\n"
  2124. " mov edx,[that] // vtable in edx\n"
  2125. " mov eax,methodIndex\n"
  2126. " call [edx][eax*4] // stdcall\n"
  2127. " }\n"
  2128. "}",
  2129. format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
  2130. " __asm {\n"
  2131. " mov edx,[that] // vtable in edx\n"
  2132. " mov eax,methodIndex\n"
  2133. " call [edx][eax*4] // stdcall\n"
  2134. " }\n"
  2135. "}"));
  2136. EXPECT_EQ("_asm {\n"
  2137. " xor eax, eax;\n"
  2138. " cpuid;\n"
  2139. "}",
  2140. format("_asm {\n"
  2141. " xor eax, eax;\n"
  2142. " cpuid;\n"
  2143. "}"));
  2144. verifyFormat("void function() {\n"
  2145. " // comment\n"
  2146. " asm(\"\");\n"
  2147. "}");
  2148. EXPECT_EQ("__asm {\n"
  2149. "}\n"
  2150. "int i;",
  2151. format("__asm {\n"
  2152. "}\n"
  2153. "int i;"));
  2154. }
  2155. TEST_F(FormatTest, FormatTryCatch) {
  2156. verifyFormat("try {\n"
  2157. " throw a * b;\n"
  2158. "} catch (int a) {\n"
  2159. " // Do nothing.\n"
  2160. "} catch (...) {\n"
  2161. " exit(42);\n"
  2162. "}");
  2163. // Function-level try statements.
  2164. verifyFormat("int f() try { return 4; } catch (...) {\n"
  2165. " return 5;\n"
  2166. "}");
  2167. verifyFormat("class A {\n"
  2168. " int a;\n"
  2169. " A() try : a(0) {\n"
  2170. " } catch (...) {\n"
  2171. " throw;\n"
  2172. " }\n"
  2173. "};\n");
  2174. // Incomplete try-catch blocks.
  2175. verifyIncompleteFormat("try {} catch (");
  2176. }
  2177. TEST_F(FormatTest, FormatSEHTryCatch) {
  2178. verifyFormat("__try {\n"
  2179. " int a = b * c;\n"
  2180. "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
  2181. " // Do nothing.\n"
  2182. "}");
  2183. verifyFormat("__try {\n"
  2184. " int a = b * c;\n"
  2185. "} __finally {\n"
  2186. " // Do nothing.\n"
  2187. "}");
  2188. verifyFormat("DEBUG({\n"
  2189. " __try {\n"
  2190. " } __finally {\n"
  2191. " }\n"
  2192. "});\n");
  2193. }
  2194. TEST_F(FormatTest, IncompleteTryCatchBlocks) {
  2195. verifyFormat("try {\n"
  2196. " f();\n"
  2197. "} catch {\n"
  2198. " g();\n"
  2199. "}");
  2200. verifyFormat("try {\n"
  2201. " f();\n"
  2202. "} catch (A a) MACRO(x) {\n"
  2203. " g();\n"
  2204. "} catch (B b) MACRO(x) {\n"
  2205. " g();\n"
  2206. "}");
  2207. }
  2208. TEST_F(FormatTest, FormatTryCatchBraceStyles) {
  2209. FormatStyle Style = getLLVMStyle();
  2210. for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
  2211. FormatStyle::BS_WebKit}) {
  2212. Style.BreakBeforeBraces = BraceStyle;
  2213. verifyFormat("try {\n"
  2214. " // something\n"
  2215. "} catch (...) {\n"
  2216. " // something\n"
  2217. "}",
  2218. Style);
  2219. }
  2220. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  2221. verifyFormat("try {\n"
  2222. " // something\n"
  2223. "}\n"
  2224. "catch (...) {\n"
  2225. " // something\n"
  2226. "}",
  2227. Style);
  2228. verifyFormat("__try {\n"
  2229. " // something\n"
  2230. "}\n"
  2231. "__finally {\n"
  2232. " // something\n"
  2233. "}",
  2234. Style);
  2235. verifyFormat("@try {\n"
  2236. " // something\n"
  2237. "}\n"
  2238. "@finally {\n"
  2239. " // something\n"
  2240. "}",
  2241. Style);
  2242. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  2243. verifyFormat("try\n"
  2244. "{\n"
  2245. " // something\n"
  2246. "}\n"
  2247. "catch (...)\n"
  2248. "{\n"
  2249. " // something\n"
  2250. "}",
  2251. Style);
  2252. Style.BreakBeforeBraces = FormatStyle::BS_GNU;
  2253. verifyFormat("try\n"
  2254. " {\n"
  2255. " // something\n"
  2256. " }\n"
  2257. "catch (...)\n"
  2258. " {\n"
  2259. " // something\n"
  2260. " }",
  2261. Style);
  2262. Style.BreakBeforeBraces = FormatStyle::BS_Custom;
  2263. Style.BraceWrapping.BeforeCatch = true;
  2264. verifyFormat("try {\n"
  2265. " // something\n"
  2266. "}\n"
  2267. "catch (...) {\n"
  2268. " // something\n"
  2269. "}",
  2270. Style);
  2271. }
  2272. TEST_F(FormatTest, FormatObjCTryCatch) {
  2273. verifyFormat("@try {\n"
  2274. " f();\n"
  2275. "} @catch (NSException e) {\n"
  2276. " @throw;\n"
  2277. "} @finally {\n"
  2278. " exit(42);\n"
  2279. "}");
  2280. verifyFormat("DEBUG({\n"
  2281. " @try {\n"
  2282. " } @finally {\n"
  2283. " }\n"
  2284. "});\n");
  2285. }
  2286. TEST_F(FormatTest, FormatObjCAutoreleasepool) {
  2287. FormatStyle Style = getLLVMStyle();
  2288. verifyFormat("@autoreleasepool {\n"
  2289. " f();\n"
  2290. "}\n"
  2291. "@autoreleasepool {\n"
  2292. " f();\n"
  2293. "}\n",
  2294. Style);
  2295. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  2296. verifyFormat("@autoreleasepool\n"
  2297. "{\n"
  2298. " f();\n"
  2299. "}\n"
  2300. "@autoreleasepool\n"
  2301. "{\n"
  2302. " f();\n"
  2303. "}\n",
  2304. Style);
  2305. }
  2306. TEST_F(FormatTest, StaticInitializers) {
  2307. verifyFormat("static SomeClass SC = {1, 'a'};");
  2308. verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
  2309. " 100000000, "
  2310. "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
  2311. // Here, everything other than the "}" would fit on a line.
  2312. verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
  2313. " 10000000000000000000000000};");
  2314. EXPECT_EQ("S s = {a,\n"
  2315. "\n"
  2316. " b};",
  2317. format("S s = {\n"
  2318. " a,\n"
  2319. "\n"
  2320. " b\n"
  2321. "};"));
  2322. // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
  2323. // line. However, the formatting looks a bit off and this probably doesn't
  2324. // happen often in practice.
  2325. verifyFormat("static int Variable[1] = {\n"
  2326. " {1000000000000000000000000000000000000}};",
  2327. getLLVMStyleWithColumns(40));
  2328. }
  2329. TEST_F(FormatTest, DesignatedInitializers) {
  2330. verifyFormat("const struct A a = {.a = 1, .b = 2};");
  2331. verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
  2332. " .bbbbbbbbbb = 2,\n"
  2333. " .cccccccccc = 3,\n"
  2334. " .dddddddddd = 4,\n"
  2335. " .eeeeeeeeee = 5};");
  2336. verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
  2337. " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
  2338. " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
  2339. " .ccccccccccccccccccccccccccc = 3,\n"
  2340. " .ddddddddddddddddddddddddddd = 4,\n"
  2341. " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
  2342. verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
  2343. }
  2344. TEST_F(FormatTest, NestedStaticInitializers) {
  2345. verifyFormat("static A x = {{{}}};\n");
  2346. verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
  2347. " {init1, init2, init3, init4}}};",
  2348. getLLVMStyleWithColumns(50));
  2349. verifyFormat("somes Status::global_reps[3] = {\n"
  2350. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  2351. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  2352. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
  2353. getLLVMStyleWithColumns(60));
  2354. verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
  2355. " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
  2356. " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
  2357. " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
  2358. verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
  2359. " {rect.fRight - rect.fLeft, rect.fBottom - "
  2360. "rect.fTop}};");
  2361. verifyFormat(
  2362. "SomeArrayOfSomeType a = {\n"
  2363. " {{1, 2, 3},\n"
  2364. " {1, 2, 3},\n"
  2365. " {111111111111111111111111111111, 222222222222222222222222222222,\n"
  2366. " 333333333333333333333333333333},\n"
  2367. " {1, 2, 3},\n"
  2368. " {1, 2, 3}}};");
  2369. verifyFormat(
  2370. "SomeArrayOfSomeType a = {\n"
  2371. " {{1, 2, 3}},\n"
  2372. " {{1, 2, 3}},\n"
  2373. " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
  2374. " 333333333333333333333333333333}},\n"
  2375. " {{1, 2, 3}},\n"
  2376. " {{1, 2, 3}}};");
  2377. verifyFormat("struct {\n"
  2378. " unsigned bit;\n"
  2379. " const char *const name;\n"
  2380. "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
  2381. " {kOsWin, \"Windows\"},\n"
  2382. " {kOsLinux, \"Linux\"},\n"
  2383. " {kOsCrOS, \"Chrome OS\"}};");
  2384. verifyFormat("struct {\n"
  2385. " unsigned bit;\n"
  2386. " const char *const name;\n"
  2387. "} kBitsToOs[] = {\n"
  2388. " {kOsMac, \"Mac\"},\n"
  2389. " {kOsWin, \"Windows\"},\n"
  2390. " {kOsLinux, \"Linux\"},\n"
  2391. " {kOsCrOS, \"Chrome OS\"},\n"
  2392. "};");
  2393. }
  2394. TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
  2395. verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2396. " \\\n"
  2397. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
  2398. }
  2399. TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
  2400. verifyFormat("virtual void write(ELFWriter *writerrr,\n"
  2401. " OwningPtr<FileOutputBuffer> &buffer) = 0;");
  2402. // Do break defaulted and deleted functions.
  2403. verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
  2404. " default;",
  2405. getLLVMStyleWithColumns(40));
  2406. verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
  2407. " delete;",
  2408. getLLVMStyleWithColumns(40));
  2409. }
  2410. TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
  2411. verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
  2412. getLLVMStyleWithColumns(40));
  2413. verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  2414. getLLVMStyleWithColumns(40));
  2415. EXPECT_EQ("#define Q \\\n"
  2416. " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n"
  2417. " \"aaaaaaaa.cpp\"",
  2418. format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
  2419. getLLVMStyleWithColumns(40)));
  2420. }
  2421. TEST_F(FormatTest, UnderstandsLinePPDirective) {
  2422. EXPECT_EQ("# 123 \"A string literal\"",
  2423. format(" # 123 \"A string literal\""));
  2424. }
  2425. TEST_F(FormatTest, LayoutUnknownPPDirective) {
  2426. EXPECT_EQ("#;", format("#;"));
  2427. verifyFormat("#\n;\n;\n;");
  2428. }
  2429. TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
  2430. EXPECT_EQ("#line 42 \"test\"\n",
  2431. format("# \\\n line \\\n 42 \\\n \"test\"\n"));
  2432. EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n",
  2433. getLLVMStyleWithColumns(12)));
  2434. }
  2435. TEST_F(FormatTest, EndOfFileEndsPPDirective) {
  2436. EXPECT_EQ("#line 42 \"test\"",
  2437. format("# \\\n line \\\n 42 \\\n \"test\""));
  2438. EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B"));
  2439. }
  2440. TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
  2441. verifyFormat("#define A \\x20");
  2442. verifyFormat("#define A \\ x20");
  2443. EXPECT_EQ("#define A \\ x20", format("#define A \\ x20"));
  2444. verifyFormat("#define A ''");
  2445. verifyFormat("#define A ''qqq");
  2446. verifyFormat("#define A `qqq");
  2447. verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
  2448. EXPECT_EQ("const char *c = STRINGIFY(\n"
  2449. "\\na : b);",
  2450. format("const char * c = STRINGIFY(\n"
  2451. "\\na : b);"));
  2452. verifyFormat("a\r\\");
  2453. verifyFormat("a\v\\");
  2454. verifyFormat("a\f\\");
  2455. }
  2456. TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
  2457. verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
  2458. verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
  2459. verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
  2460. // FIXME: We never break before the macro name.
  2461. verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
  2462. verifyFormat("#define A A\n#define A A");
  2463. verifyFormat("#define A(X) A\n#define A A");
  2464. verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
  2465. verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
  2466. }
  2467. TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
  2468. EXPECT_EQ("// somecomment\n"
  2469. "#include \"a.h\"\n"
  2470. "#define A( \\\n"
  2471. " A, B)\n"
  2472. "#include \"b.h\"\n"
  2473. "// somecomment\n",
  2474. format(" // somecomment\n"
  2475. " #include \"a.h\"\n"
  2476. "#define A(A,\\\n"
  2477. " B)\n"
  2478. " #include \"b.h\"\n"
  2479. " // somecomment\n",
  2480. getLLVMStyleWithColumns(13)));
  2481. }
  2482. TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
  2483. TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
  2484. EXPECT_EQ("#define A \\\n"
  2485. " c; \\\n"
  2486. " e;\n"
  2487. "f;",
  2488. format("#define A c; e;\n"
  2489. "f;",
  2490. getLLVMStyleWithColumns(14)));
  2491. }
  2492. TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
  2493. TEST_F(FormatTest, MacroDefinitionInsideStatement) {
  2494. EXPECT_EQ("int x,\n"
  2495. "#define A\n"
  2496. " y;",
  2497. format("int x,\n#define A\ny;"));
  2498. }
  2499. TEST_F(FormatTest, HashInMacroDefinition) {
  2500. EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
  2501. verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
  2502. verifyFormat("#define A \\\n"
  2503. " { \\\n"
  2504. " f(#c); \\\n"
  2505. " }",
  2506. getLLVMStyleWithColumns(11));
  2507. verifyFormat("#define A(X) \\\n"
  2508. " void function##X()",
  2509. getLLVMStyleWithColumns(22));
  2510. verifyFormat("#define A(a, b, c) \\\n"
  2511. " void a##b##c()",
  2512. getLLVMStyleWithColumns(22));
  2513. verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
  2514. }
  2515. TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
  2516. EXPECT_EQ("#define A (x)", format("#define A (x)"));
  2517. EXPECT_EQ("#define A(x)", format("#define A(x)"));
  2518. }
  2519. TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
  2520. EXPECT_EQ("#define A b;", format("#define A \\\n"
  2521. " \\\n"
  2522. " b;",
  2523. getLLVMStyleWithColumns(25)));
  2524. EXPECT_EQ("#define A \\\n"
  2525. " \\\n"
  2526. " a; \\\n"
  2527. " b;",
  2528. format("#define A \\\n"
  2529. " \\\n"
  2530. " a; \\\n"
  2531. " b;",
  2532. getLLVMStyleWithColumns(11)));
  2533. EXPECT_EQ("#define A \\\n"
  2534. " a; \\\n"
  2535. " \\\n"
  2536. " b;",
  2537. format("#define A \\\n"
  2538. " a; \\\n"
  2539. " \\\n"
  2540. " b;",
  2541. getLLVMStyleWithColumns(11)));
  2542. }
  2543. TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
  2544. verifyIncompleteFormat("#define A :");
  2545. verifyFormat("#define SOMECASES \\\n"
  2546. " case 1: \\\n"
  2547. " case 2\n",
  2548. getLLVMStyleWithColumns(20));
  2549. verifyFormat("#define A template <typename T>");
  2550. verifyIncompleteFormat("#define STR(x) #x\n"
  2551. "f(STR(this_is_a_string_literal{));");
  2552. verifyFormat("#pragma omp threadprivate( \\\n"
  2553. " y)), // expected-warning",
  2554. getLLVMStyleWithColumns(28));
  2555. verifyFormat("#d, = };");
  2556. verifyFormat("#if \"a");
  2557. verifyIncompleteFormat("({\n"
  2558. "#define b \\\n"
  2559. " } \\\n"
  2560. " a\n"
  2561. "a",
  2562. getLLVMStyleWithColumns(15));
  2563. verifyFormat("#define A \\\n"
  2564. " { \\\n"
  2565. " {\n"
  2566. "#define B \\\n"
  2567. " } \\\n"
  2568. " }",
  2569. getLLVMStyleWithColumns(15));
  2570. verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
  2571. verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
  2572. verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
  2573. verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}");
  2574. }
  2575. TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
  2576. verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
  2577. EXPECT_EQ("class A : public QObject {\n"
  2578. " Q_OBJECT\n"
  2579. "\n"
  2580. " A() {}\n"
  2581. "};",
  2582. format("class A : public QObject {\n"
  2583. " Q_OBJECT\n"
  2584. "\n"
  2585. " A() {\n}\n"
  2586. "} ;"));
  2587. EXPECT_EQ("MACRO\n"
  2588. "/*static*/ int i;",
  2589. format("MACRO\n"
  2590. " /*static*/ int i;"));
  2591. EXPECT_EQ("SOME_MACRO\n"
  2592. "namespace {\n"
  2593. "void f();\n"
  2594. "}",
  2595. format("SOME_MACRO\n"
  2596. " namespace {\n"
  2597. "void f( );\n"
  2598. "}"));
  2599. // Only if the identifier contains at least 5 characters.
  2600. EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
  2601. EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
  2602. // Only if everything is upper case.
  2603. EXPECT_EQ("class A : public QObject {\n"
  2604. " Q_Object A() {}\n"
  2605. "};",
  2606. format("class A : public QObject {\n"
  2607. " Q_Object\n"
  2608. " A() {\n}\n"
  2609. "} ;"));
  2610. // Only if the next line can actually start an unwrapped line.
  2611. EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
  2612. format("SOME_WEIRD_LOG_MACRO\n"
  2613. "<< SomeThing;"));
  2614. verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
  2615. "(n, buffers))\n",
  2616. getChromiumStyle(FormatStyle::LK_Cpp));
  2617. }
  2618. TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
  2619. EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  2620. "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  2621. "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  2622. "class X {};\n"
  2623. "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  2624. "int *createScopDetectionPass() { return 0; }",
  2625. format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
  2626. " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
  2627. " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
  2628. " class X {};\n"
  2629. " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
  2630. " int *createScopDetectionPass() { return 0; }"));
  2631. // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
  2632. // braces, so that inner block is indented one level more.
  2633. EXPECT_EQ("int q() {\n"
  2634. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  2635. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  2636. " IPC_END_MESSAGE_MAP()\n"
  2637. "}",
  2638. format("int q() {\n"
  2639. " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
  2640. " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
  2641. " IPC_END_MESSAGE_MAP()\n"
  2642. "}"));
  2643. // Same inside macros.
  2644. EXPECT_EQ("#define LIST(L) \\\n"
  2645. " L(A) \\\n"
  2646. " L(B) \\\n"
  2647. " L(C)",
  2648. format("#define LIST(L) \\\n"
  2649. " L(A) \\\n"
  2650. " L(B) \\\n"
  2651. " L(C)",
  2652. getGoogleStyle()));
  2653. // These must not be recognized as macros.
  2654. EXPECT_EQ("int q() {\n"
  2655. " f(x);\n"
  2656. " f(x) {}\n"
  2657. " f(x)->g();\n"
  2658. " f(x)->*g();\n"
  2659. " f(x).g();\n"
  2660. " f(x) = x;\n"
  2661. " f(x) += x;\n"
  2662. " f(x) -= x;\n"
  2663. " f(x) *= x;\n"
  2664. " f(x) /= x;\n"
  2665. " f(x) %= x;\n"
  2666. " f(x) &= x;\n"
  2667. " f(x) |= x;\n"
  2668. " f(x) ^= x;\n"
  2669. " f(x) >>= x;\n"
  2670. " f(x) <<= x;\n"
  2671. " f(x)[y].z();\n"
  2672. " LOG(INFO) << x;\n"
  2673. " ifstream(x) >> x;\n"
  2674. "}\n",
  2675. format("int q() {\n"
  2676. " f(x)\n;\n"
  2677. " f(x)\n {}\n"
  2678. " f(x)\n->g();\n"
  2679. " f(x)\n->*g();\n"
  2680. " f(x)\n.g();\n"
  2681. " f(x)\n = x;\n"
  2682. " f(x)\n += x;\n"
  2683. " f(x)\n -= x;\n"
  2684. " f(x)\n *= x;\n"
  2685. " f(x)\n /= x;\n"
  2686. " f(x)\n %= x;\n"
  2687. " f(x)\n &= x;\n"
  2688. " f(x)\n |= x;\n"
  2689. " f(x)\n ^= x;\n"
  2690. " f(x)\n >>= x;\n"
  2691. " f(x)\n <<= x;\n"
  2692. " f(x)\n[y].z();\n"
  2693. " LOG(INFO)\n << x;\n"
  2694. " ifstream(x)\n >> x;\n"
  2695. "}\n"));
  2696. EXPECT_EQ("int q() {\n"
  2697. " F(x)\n"
  2698. " if (1) {\n"
  2699. " }\n"
  2700. " F(x)\n"
  2701. " while (1) {\n"
  2702. " }\n"
  2703. " F(x)\n"
  2704. " G(x);\n"
  2705. " F(x)\n"
  2706. " try {\n"
  2707. " Q();\n"
  2708. " } catch (...) {\n"
  2709. " }\n"
  2710. "}\n",
  2711. format("int q() {\n"
  2712. "F(x)\n"
  2713. "if (1) {}\n"
  2714. "F(x)\n"
  2715. "while (1) {}\n"
  2716. "F(x)\n"
  2717. "G(x);\n"
  2718. "F(x)\n"
  2719. "try { Q(); } catch (...) {}\n"
  2720. "}\n"));
  2721. EXPECT_EQ("class A {\n"
  2722. " A() : t(0) {}\n"
  2723. " A(int i) noexcept() : {}\n"
  2724. " A(X x)\n" // FIXME: function-level try blocks are broken.
  2725. " try : t(0) {\n"
  2726. " } catch (...) {\n"
  2727. " }\n"
  2728. "};",
  2729. format("class A {\n"
  2730. " A()\n : t(0) {}\n"
  2731. " A(int i)\n noexcept() : {}\n"
  2732. " A(X x)\n"
  2733. " try : t(0) {} catch (...) {}\n"
  2734. "};"));
  2735. EXPECT_EQ("class SomeClass {\n"
  2736. "public:\n"
  2737. " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2738. "};",
  2739. format("class SomeClass {\n"
  2740. "public:\n"
  2741. " SomeClass()\n"
  2742. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2743. "};"));
  2744. EXPECT_EQ("class SomeClass {\n"
  2745. "public:\n"
  2746. " SomeClass()\n"
  2747. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2748. "};",
  2749. format("class SomeClass {\n"
  2750. "public:\n"
  2751. " SomeClass()\n"
  2752. " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
  2753. "};",
  2754. getLLVMStyleWithColumns(40)));
  2755. verifyFormat("MACRO(>)");
  2756. }
  2757. TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
  2758. verifyFormat("#define A \\\n"
  2759. " f({ \\\n"
  2760. " g(); \\\n"
  2761. " });",
  2762. getLLVMStyleWithColumns(11));
  2763. }
  2764. TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
  2765. EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
  2766. }
  2767. TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
  2768. verifyFormat("{\n { a #c; }\n}");
  2769. }
  2770. TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
  2771. EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
  2772. format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
  2773. EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
  2774. format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
  2775. }
  2776. TEST_F(FormatTest, EscapedNewlines) {
  2777. EXPECT_EQ(
  2778. "#define A \\\n int i; \\\n int j;",
  2779. format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
  2780. EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
  2781. EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
  2782. EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/"));
  2783. EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
  2784. }
  2785. TEST_F(FormatTest, DontCrashOnBlockComments) {
  2786. EXPECT_EQ(
  2787. "int xxxxxxxxx; /* "
  2788. "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
  2789. "zzzzzz\n"
  2790. "0*/",
  2791. format("int xxxxxxxxx; /* "
  2792. "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
  2793. "0*/"));
  2794. }
  2795. TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
  2796. verifyFormat("#define A \\\n"
  2797. " int v( \\\n"
  2798. " a); \\\n"
  2799. " int i;",
  2800. getLLVMStyleWithColumns(11));
  2801. }
  2802. TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
  2803. EXPECT_EQ(
  2804. "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2805. " \\\n"
  2806. " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2807. "\n"
  2808. "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2809. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
  2810. format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
  2811. "\\\n"
  2812. "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
  2813. " \n"
  2814. " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
  2815. " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
  2816. }
  2817. TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
  2818. EXPECT_EQ("int\n"
  2819. "#define A\n"
  2820. " a;",
  2821. format("int\n#define A\na;"));
  2822. verifyFormat("functionCallTo(\n"
  2823. " someOtherFunction(\n"
  2824. " withSomeParameters, whichInSequence,\n"
  2825. " areLongerThanALine(andAnotherCall,\n"
  2826. "#define A B\n"
  2827. " withMoreParamters,\n"
  2828. " whichStronglyInfluenceTheLayout),\n"
  2829. " andMoreParameters),\n"
  2830. " trailing);",
  2831. getLLVMStyleWithColumns(69));
  2832. verifyFormat("Foo::Foo()\n"
  2833. "#ifdef BAR\n"
  2834. " : baz(0)\n"
  2835. "#endif\n"
  2836. "{\n"
  2837. "}");
  2838. verifyFormat("void f() {\n"
  2839. " if (true)\n"
  2840. "#ifdef A\n"
  2841. " f(42);\n"
  2842. " x();\n"
  2843. "#else\n"
  2844. " g();\n"
  2845. " x();\n"
  2846. "#endif\n"
  2847. "}");
  2848. verifyFormat("void f(param1, param2,\n"
  2849. " param3,\n"
  2850. "#ifdef A\n"
  2851. " param4(param5,\n"
  2852. "#ifdef A1\n"
  2853. " param6,\n"
  2854. "#ifdef A2\n"
  2855. " param7),\n"
  2856. "#else\n"
  2857. " param8),\n"
  2858. " param9,\n"
  2859. "#endif\n"
  2860. " param10,\n"
  2861. "#endif\n"
  2862. " param11)\n"
  2863. "#else\n"
  2864. " param12)\n"
  2865. "#endif\n"
  2866. "{\n"
  2867. " x();\n"
  2868. "}",
  2869. getLLVMStyleWithColumns(28));
  2870. verifyFormat("#if 1\n"
  2871. "int i;");
  2872. verifyFormat("#if 1\n"
  2873. "#endif\n"
  2874. "#if 1\n"
  2875. "#else\n"
  2876. "#endif\n");
  2877. verifyFormat("DEBUG({\n"
  2878. " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  2879. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
  2880. "});\n"
  2881. "#if a\n"
  2882. "#else\n"
  2883. "#endif");
  2884. verifyIncompleteFormat("void f(\n"
  2885. "#if A\n"
  2886. " );\n"
  2887. "#else\n"
  2888. "#endif");
  2889. }
  2890. TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
  2891. verifyFormat("#endif\n"
  2892. "#if B");
  2893. }
  2894. TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
  2895. FormatStyle SingleLine = getLLVMStyle();
  2896. SingleLine.AllowShortIfStatementsOnASingleLine = true;
  2897. verifyFormat("#if 0\n"
  2898. "#elif 1\n"
  2899. "#endif\n"
  2900. "void foo() {\n"
  2901. " if (test) foo2();\n"
  2902. "}",
  2903. SingleLine);
  2904. }
  2905. TEST_F(FormatTest, LayoutBlockInsideParens) {
  2906. verifyFormat("functionCall({ int i; });");
  2907. verifyFormat("functionCall({\n"
  2908. " int i;\n"
  2909. " int j;\n"
  2910. "});");
  2911. verifyFormat("functionCall(\n"
  2912. " {\n"
  2913. " int i;\n"
  2914. " int j;\n"
  2915. " },\n"
  2916. " aaaa, bbbb, cccc);");
  2917. verifyFormat("functionA(functionB({\n"
  2918. " int i;\n"
  2919. " int j;\n"
  2920. " }),\n"
  2921. " aaaa, bbbb, cccc);");
  2922. verifyFormat("functionCall(\n"
  2923. " {\n"
  2924. " int i;\n"
  2925. " int j;\n"
  2926. " },\n"
  2927. " aaaa, bbbb, // comment\n"
  2928. " cccc);");
  2929. verifyFormat("functionA(functionB({\n"
  2930. " int i;\n"
  2931. " int j;\n"
  2932. " }),\n"
  2933. " aaaa, bbbb, // comment\n"
  2934. " cccc);");
  2935. verifyFormat("functionCall(aaaa, bbbb, { int i; });");
  2936. verifyFormat("functionCall(aaaa, bbbb, {\n"
  2937. " int i;\n"
  2938. " int j;\n"
  2939. "});");
  2940. verifyFormat(
  2941. "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
  2942. " {\n"
  2943. " int i; // break\n"
  2944. " },\n"
  2945. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  2946. " ccccccccccccccccc));");
  2947. verifyFormat("DEBUG({\n"
  2948. " if (a)\n"
  2949. " f();\n"
  2950. "});");
  2951. }
  2952. TEST_F(FormatTest, LayoutBlockInsideStatement) {
  2953. EXPECT_EQ("SOME_MACRO { int i; }\n"
  2954. "int i;",
  2955. format(" SOME_MACRO {int i;} int i;"));
  2956. }
  2957. TEST_F(FormatTest, LayoutNestedBlocks) {
  2958. verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
  2959. " struct s {\n"
  2960. " int i;\n"
  2961. " };\n"
  2962. " s kBitsToOs[] = {{10}};\n"
  2963. " for (int i = 0; i < 10; ++i)\n"
  2964. " return;\n"
  2965. "}");
  2966. verifyFormat("call(parameter, {\n"
  2967. " something();\n"
  2968. " // Comment using all columns.\n"
  2969. " somethingelse();\n"
  2970. "});",
  2971. getLLVMStyleWithColumns(40));
  2972. verifyFormat("DEBUG( //\n"
  2973. " { f(); }, a);");
  2974. verifyFormat("DEBUG( //\n"
  2975. " {\n"
  2976. " f(); //\n"
  2977. " },\n"
  2978. " a);");
  2979. EXPECT_EQ("call(parameter, {\n"
  2980. " something();\n"
  2981. " // Comment too\n"
  2982. " // looooooooooong.\n"
  2983. " somethingElse();\n"
  2984. "});",
  2985. format("call(parameter, {\n"
  2986. " something();\n"
  2987. " // Comment too looooooooooong.\n"
  2988. " somethingElse();\n"
  2989. "});",
  2990. getLLVMStyleWithColumns(29)));
  2991. EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });"));
  2992. EXPECT_EQ("DEBUG({ // comment\n"
  2993. " int i;\n"
  2994. "});",
  2995. format("DEBUG({ // comment\n"
  2996. "int i;\n"
  2997. "});"));
  2998. EXPECT_EQ("DEBUG({\n"
  2999. " int i;\n"
  3000. "\n"
  3001. " // comment\n"
  3002. " int j;\n"
  3003. "});",
  3004. format("DEBUG({\n"
  3005. " int i;\n"
  3006. "\n"
  3007. " // comment\n"
  3008. " int j;\n"
  3009. "});"));
  3010. verifyFormat("DEBUG({\n"
  3011. " if (a)\n"
  3012. " return;\n"
  3013. "});");
  3014. verifyGoogleFormat("DEBUG({\n"
  3015. " if (a) return;\n"
  3016. "});");
  3017. FormatStyle Style = getGoogleStyle();
  3018. Style.ColumnLimit = 45;
  3019. verifyFormat("Debug(aaaaa,\n"
  3020. " {\n"
  3021. " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
  3022. " },\n"
  3023. " a);",
  3024. Style);
  3025. verifyFormat("SomeFunction({MACRO({ return output; }), b});");
  3026. verifyNoCrash("^{v^{a}}");
  3027. }
  3028. TEST_F(FormatTest, FormatNestedBlocksInMacros) {
  3029. EXPECT_EQ("#define MACRO() \\\n"
  3030. " Debug(aaa, /* force line break */ \\\n"
  3031. " { \\\n"
  3032. " int i; \\\n"
  3033. " int j; \\\n"
  3034. " })",
  3035. format("#define MACRO() Debug(aaa, /* force line break */ \\\n"
  3036. " { int i; int j; })",
  3037. getGoogleStyle()));
  3038. EXPECT_EQ("#define A \\\n"
  3039. " [] { \\\n"
  3040. " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
  3041. " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
  3042. " }",
  3043. format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
  3044. "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
  3045. getGoogleStyle()));
  3046. }
  3047. TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
  3048. EXPECT_EQ("{}", format("{}"));
  3049. verifyFormat("enum E {};");
  3050. verifyFormat("enum E {}");
  3051. }
  3052. TEST_F(FormatTest, FormatBeginBlockEndMacros) {
  3053. FormatStyle Style = getLLVMStyle();
  3054. Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
  3055. Style.MacroBlockEnd = "^[A-Z_]+_END$";
  3056. verifyFormat("FOO_BEGIN\n"
  3057. " FOO_ENTRY\n"
  3058. "FOO_END", Style);
  3059. verifyFormat("FOO_BEGIN\n"
  3060. " NESTED_FOO_BEGIN\n"
  3061. " NESTED_FOO_ENTRY\n"
  3062. " NESTED_FOO_END\n"
  3063. "FOO_END", Style);
  3064. verifyFormat("FOO_BEGIN(Foo, Bar)\n"
  3065. " int x;\n"
  3066. " x = 1;\n"
  3067. "FOO_END(Baz)", Style);
  3068. }
  3069. //===----------------------------------------------------------------------===//
  3070. // Line break tests.
  3071. //===----------------------------------------------------------------------===//
  3072. TEST_F(FormatTest, PreventConfusingIndents) {
  3073. verifyFormat(
  3074. "void f() {\n"
  3075. " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
  3076. " parameter, parameter, parameter)),\n"
  3077. " SecondLongCall(parameter));\n"
  3078. "}");
  3079. verifyFormat(
  3080. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3081. " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3082. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3083. " aaaaaaaaaaaaaaaaaaaaaaaa);");
  3084. verifyFormat(
  3085. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3086. " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
  3087. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  3088. " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
  3089. verifyFormat(
  3090. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  3091. " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
  3092. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
  3093. " aaaaaaaaaaaaaaaaaaaaaaaa>;");
  3094. verifyFormat("int a = bbbb && ccc && fffff(\n"
  3095. "#define A Just forcing a new line\n"
  3096. " ddd);");
  3097. }
  3098. TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
  3099. verifyFormat(
  3100. "bool aaaaaaa =\n"
  3101. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
  3102. " bbbbbbbb();");
  3103. verifyFormat(
  3104. "bool aaaaaaa =\n"
  3105. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
  3106. " bbbbbbbb();");
  3107. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  3108. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
  3109. " ccccccccc == ddddddddddd;");
  3110. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
  3111. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
  3112. " ccccccccc == ddddddddddd;");
  3113. verifyFormat(
  3114. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  3115. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
  3116. " ccccccccc == ddddddddddd;");
  3117. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  3118. " aaaaaa) &&\n"
  3119. " bbbbbb && cccccc;");
  3120. verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
  3121. " aaaaaa) >>\n"
  3122. " bbbbbb;");
  3123. verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
  3124. " SourceMgr.getSpellingColumnNumber(\n"
  3125. " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
  3126. " 1);");
  3127. verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3128. " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
  3129. " cccccc) {\n}");
  3130. verifyFormat("b = a &&\n"
  3131. " // Comment\n"
  3132. " b.c && d;");
  3133. // If the LHS of a comparison is not a binary expression itself, the
  3134. // additional linebreak confuses many people.
  3135. verifyFormat(
  3136. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3137. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
  3138. "}");
  3139. verifyFormat(
  3140. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3141. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  3142. "}");
  3143. verifyFormat(
  3144. "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
  3145. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  3146. "}");
  3147. // Even explicit parentheses stress the precedence enough to make the
  3148. // additional break unnecessary.
  3149. verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3150. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
  3151. "}");
  3152. // This cases is borderline, but with the indentation it is still readable.
  3153. verifyFormat(
  3154. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3155. " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3156. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  3157. "}",
  3158. getLLVMStyleWithColumns(75));
  3159. // If the LHS is a binary expression, we should still use the additional break
  3160. // as otherwise the formatting hides the operator precedence.
  3161. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3162. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3163. " 5) {\n"
  3164. "}");
  3165. FormatStyle OnePerLine = getLLVMStyle();
  3166. OnePerLine.BinPackParameters = false;
  3167. verifyFormat(
  3168. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3169. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3170. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
  3171. OnePerLine);
  3172. }
  3173. TEST_F(FormatTest, ExpressionIndentation) {
  3174. verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3175. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3176. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3177. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3178. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
  3179. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
  3180. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3181. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
  3182. " ccccccccccccccccccccccccccccccccccccccccc;");
  3183. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3184. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3185. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3186. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3187. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3188. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3189. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3190. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3191. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
  3192. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
  3193. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3194. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  3195. verifyFormat("if () {\n"
  3196. "} else if (aaaaa &&\n"
  3197. " bbbbb > // break\n"
  3198. " ccccc) {\n"
  3199. "}");
  3200. // Presence of a trailing comment used to change indentation of b.
  3201. verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
  3202. " b;\n"
  3203. "return aaaaaaaaaaaaaaaaaaa +\n"
  3204. " b; //",
  3205. getLLVMStyleWithColumns(30));
  3206. }
  3207. TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
  3208. // Not sure what the best system is here. Like this, the LHS can be found
  3209. // immediately above an operator (everything with the same or a higher
  3210. // indent). The RHS is aligned right of the operator and so compasses
  3211. // everything until something with the same indent as the operator is found.
  3212. // FIXME: Is this a good system?
  3213. FormatStyle Style = getLLVMStyle();
  3214. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  3215. verifyFormat(
  3216. "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3217. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3218. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3219. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3220. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3221. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3222. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3223. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3224. " > ccccccccccccccccccccccccccccccccccccccccc;",
  3225. Style);
  3226. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3227. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3228. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3229. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3230. Style);
  3231. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3232. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3233. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3234. " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3235. Style);
  3236. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3237. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3238. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3239. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
  3240. Style);
  3241. verifyFormat("if () {\n"
  3242. "} else if (aaaaa\n"
  3243. " && bbbbb // break\n"
  3244. " > ccccc) {\n"
  3245. "}",
  3246. Style);
  3247. verifyFormat("return (a)\n"
  3248. " // comment\n"
  3249. " + b;",
  3250. Style);
  3251. verifyFormat(
  3252. "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3253. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3254. " + cc;",
  3255. Style);
  3256. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3257. " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  3258. Style);
  3259. // Forced by comments.
  3260. verifyFormat(
  3261. "unsigned ContentSize =\n"
  3262. " sizeof(int16_t) // DWARF ARange version number\n"
  3263. " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
  3264. " + sizeof(int8_t) // Pointer Size (in bytes)\n"
  3265. " + sizeof(int8_t); // Segment Size (in bytes)");
  3266. verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
  3267. " == boost::fusion::at_c<1>(iiii).second;",
  3268. Style);
  3269. Style.ColumnLimit = 60;
  3270. verifyFormat("zzzzzzzzzz\n"
  3271. " = bbbbbbbbbbbbbbbbb\n"
  3272. " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3273. Style);
  3274. }
  3275. TEST_F(FormatTest, NoOperandAlignment) {
  3276. FormatStyle Style = getLLVMStyle();
  3277. Style.AlignOperands = false;
  3278. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
  3279. verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3280. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3281. " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3282. " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3283. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3284. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3285. " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3286. " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3287. " > ccccccccccccccccccccccccccccccccccccccccc;",
  3288. Style);
  3289. verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3290. " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3291. " + cc;",
  3292. Style);
  3293. verifyFormat("int a = aa\n"
  3294. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  3295. " * cccccccccccccccccccccccccccccccccccc;",
  3296. Style);
  3297. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  3298. verifyFormat("return (a > b\n"
  3299. " // comment1\n"
  3300. " // comment2\n"
  3301. " || c);",
  3302. Style);
  3303. }
  3304. TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
  3305. FormatStyle Style = getLLVMStyle();
  3306. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
  3307. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  3308. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3309. " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
  3310. Style);
  3311. }
  3312. TEST_F(FormatTest, ConstructorInitializers) {
  3313. verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
  3314. verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
  3315. getLLVMStyleWithColumns(45));
  3316. verifyFormat("Constructor()\n"
  3317. " : Inttializer(FitsOnTheLine) {}",
  3318. getLLVMStyleWithColumns(44));
  3319. verifyFormat("Constructor()\n"
  3320. " : Inttializer(FitsOnTheLine) {}",
  3321. getLLVMStyleWithColumns(43));
  3322. verifyFormat("template <typename T>\n"
  3323. "Constructor() : Initializer(FitsOnTheLine) {}",
  3324. getLLVMStyleWithColumns(45));
  3325. verifyFormat(
  3326. "SomeClass::Constructor()\n"
  3327. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  3328. verifyFormat(
  3329. "SomeClass::Constructor()\n"
  3330. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3331. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
  3332. verifyFormat(
  3333. "SomeClass::Constructor()\n"
  3334. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3335. " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
  3336. verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3337. " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3338. " : aaaaaaaaaa(aaaaaa) {}");
  3339. verifyFormat("Constructor()\n"
  3340. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3341. " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3342. " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3343. " aaaaaaaaaaaaaaaaaaaaaaa() {}");
  3344. verifyFormat("Constructor()\n"
  3345. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3346. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3347. verifyFormat("Constructor(int Parameter = 0)\n"
  3348. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
  3349. " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
  3350. verifyFormat("Constructor()\n"
  3351. " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
  3352. "}",
  3353. getLLVMStyleWithColumns(60));
  3354. verifyFormat("Constructor()\n"
  3355. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3356. " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
  3357. // Here a line could be saved by splitting the second initializer onto two
  3358. // lines, but that is not desirable.
  3359. verifyFormat("Constructor()\n"
  3360. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
  3361. " aaaaaaaaaaa(aaaaaaaaaaa),\n"
  3362. " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3363. FormatStyle OnePerLine = getLLVMStyle();
  3364. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  3365. verifyFormat("SomeClass::Constructor()\n"
  3366. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3367. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3368. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  3369. OnePerLine);
  3370. verifyFormat("SomeClass::Constructor()\n"
  3371. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
  3372. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  3373. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  3374. OnePerLine);
  3375. verifyFormat("MyClass::MyClass(int var)\n"
  3376. " : some_var_(var), // 4 space indent\n"
  3377. " some_other_var_(var + 1) { // lined up\n"
  3378. "}",
  3379. OnePerLine);
  3380. verifyFormat("Constructor()\n"
  3381. " : aaaaa(aaaaaa),\n"
  3382. " aaaaa(aaaaaa),\n"
  3383. " aaaaa(aaaaaa),\n"
  3384. " aaaaa(aaaaaa),\n"
  3385. " aaaaa(aaaaaa) {}",
  3386. OnePerLine);
  3387. verifyFormat("Constructor()\n"
  3388. " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
  3389. " aaaaaaaaaaaaaaaaaaaaaa) {}",
  3390. OnePerLine);
  3391. OnePerLine.ColumnLimit = 60;
  3392. verifyFormat("Constructor()\n"
  3393. " : aaaaaaaaaaaaaaaaaaaa(a),\n"
  3394. " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
  3395. OnePerLine);
  3396. EXPECT_EQ("Constructor()\n"
  3397. " : // Comment forcing unwanted break.\n"
  3398. " aaaa(aaaa) {}",
  3399. format("Constructor() :\n"
  3400. " // Comment forcing unwanted break.\n"
  3401. " aaaa(aaaa) {}"));
  3402. }
  3403. TEST_F(FormatTest, MemoizationTests) {
  3404. // This breaks if the memoization lookup does not take \c Indent and
  3405. // \c LastSpace into account.
  3406. verifyFormat(
  3407. "extern CFRunLoopTimerRef\n"
  3408. "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
  3409. " CFTimeInterval interval, CFOptionFlags flags,\n"
  3410. " CFIndex order, CFRunLoopTimerCallBack callout,\n"
  3411. " CFRunLoopTimerContext *context) {}");
  3412. // Deep nesting somewhat works around our memoization.
  3413. verifyFormat(
  3414. "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3415. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3416. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3417. " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
  3418. " aaaaa())))))))))))))))))))))))))))))))))))))));",
  3419. getLLVMStyleWithColumns(65));
  3420. verifyFormat(
  3421. "aaaaa(\n"
  3422. " aaaaa,\n"
  3423. " aaaaa(\n"
  3424. " aaaaa,\n"
  3425. " aaaaa(\n"
  3426. " aaaaa,\n"
  3427. " aaaaa(\n"
  3428. " aaaaa,\n"
  3429. " aaaaa(\n"
  3430. " aaaaa,\n"
  3431. " aaaaa(\n"
  3432. " aaaaa,\n"
  3433. " aaaaa(\n"
  3434. " aaaaa,\n"
  3435. " aaaaa(\n"
  3436. " aaaaa,\n"
  3437. " aaaaa(\n"
  3438. " aaaaa,\n"
  3439. " aaaaa(\n"
  3440. " aaaaa,\n"
  3441. " aaaaa(\n"
  3442. " aaaaa,\n"
  3443. " aaaaa(\n"
  3444. " aaaaa,\n"
  3445. " aaaaa))))))))))));",
  3446. getLLVMStyleWithColumns(65));
  3447. verifyFormat(
  3448. "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"
  3449. " a),\n"
  3450. " a),\n"
  3451. " a),\n"
  3452. " a),\n"
  3453. " a),\n"
  3454. " a),\n"
  3455. " a),\n"
  3456. " a),\n"
  3457. " a),\n"
  3458. " a),\n"
  3459. " a),\n"
  3460. " a),\n"
  3461. " a),\n"
  3462. " a),\n"
  3463. " a),\n"
  3464. " a),\n"
  3465. " a)",
  3466. getLLVMStyleWithColumns(65));
  3467. // This test takes VERY long when memoization is broken.
  3468. FormatStyle OnePerLine = getLLVMStyle();
  3469. OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  3470. OnePerLine.BinPackParameters = false;
  3471. std::string input = "Constructor()\n"
  3472. " : aaaa(a,\n";
  3473. for (unsigned i = 0, e = 80; i != e; ++i) {
  3474. input += " a,\n";
  3475. }
  3476. input += " a) {}";
  3477. verifyFormat(input, OnePerLine);
  3478. }
  3479. TEST_F(FormatTest, BreaksAsHighAsPossible) {
  3480. verifyFormat(
  3481. "void f() {\n"
  3482. " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
  3483. " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
  3484. " f();\n"
  3485. "}");
  3486. verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
  3487. " Intervals[i - 1].getRange().getLast()) {\n}");
  3488. }
  3489. TEST_F(FormatTest, BreaksFunctionDeclarations) {
  3490. // Principially, we break function declarations in a certain order:
  3491. // 1) break amongst arguments.
  3492. verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
  3493. " Cccccccccccccc cccccccccccccc);");
  3494. verifyFormat("template <class TemplateIt>\n"
  3495. "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
  3496. " TemplateIt *stop) {}");
  3497. // 2) break after return type.
  3498. verifyFormat(
  3499. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3500. "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
  3501. getGoogleStyle());
  3502. // 3) break after (.
  3503. verifyFormat(
  3504. "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
  3505. " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
  3506. getGoogleStyle());
  3507. // 4) break before after nested name specifiers.
  3508. verifyFormat(
  3509. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3510. "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
  3511. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
  3512. getGoogleStyle());
  3513. // However, there are exceptions, if a sufficient amount of lines can be
  3514. // saved.
  3515. // FIXME: The precise cut-offs wrt. the number of saved lines might need some
  3516. // more adjusting.
  3517. verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  3518. " Cccccccccccccc cccccccccc,\n"
  3519. " Cccccccccccccc cccccccccc,\n"
  3520. " Cccccccccccccc cccccccccc,\n"
  3521. " Cccccccccccccc cccccccccc);");
  3522. verifyFormat(
  3523. "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3524. "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3525. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3526. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
  3527. getGoogleStyle());
  3528. verifyFormat(
  3529. "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
  3530. " Cccccccccccccc cccccccccc,\n"
  3531. " Cccccccccccccc cccccccccc,\n"
  3532. " Cccccccccccccc cccccccccc,\n"
  3533. " Cccccccccccccc cccccccccc,\n"
  3534. " Cccccccccccccc cccccccccc,\n"
  3535. " Cccccccccccccc cccccccccc);");
  3536. verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3537. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3538. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3539. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
  3540. " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
  3541. // Break after multi-line parameters.
  3542. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3543. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3544. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3545. " bbbb bbbb);");
  3546. verifyFormat("void SomeLoooooooooooongFunction(\n"
  3547. " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  3548. " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3549. " int bbbbbbbbbbbbb);");
  3550. // Treat overloaded operators like other functions.
  3551. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3552. "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
  3553. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3554. "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
  3555. verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
  3556. "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
  3557. verifyGoogleFormat(
  3558. "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
  3559. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  3560. verifyGoogleFormat(
  3561. "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
  3562. " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
  3563. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3564. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  3565. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
  3566. "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
  3567. verifyGoogleFormat(
  3568. "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
  3569. "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3570. " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
  3571. FormatStyle Style = getLLVMStyle();
  3572. Style.PointerAlignment = FormatStyle::PAS_Left;
  3573. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3574. " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
  3575. Style);
  3576. verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
  3577. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3578. Style);
  3579. }
  3580. TEST_F(FormatTest, TrailingReturnType) {
  3581. verifyFormat("auto foo() -> int;\n");
  3582. verifyFormat("struct S {\n"
  3583. " auto bar() const -> int;\n"
  3584. "};");
  3585. verifyFormat("template <size_t Order, typename T>\n"
  3586. "auto load_img(const std::string &filename)\n"
  3587. " -> alias::tensor<Order, T, mem::tag::cpu> {}");
  3588. verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
  3589. " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
  3590. verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
  3591. verifyFormat("template <typename T>\n"
  3592. "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
  3593. " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
  3594. // Not trailing return types.
  3595. verifyFormat("void f() { auto a = b->c(); }");
  3596. }
  3597. TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
  3598. // Avoid breaking before trailing 'const' or other trailing annotations, if
  3599. // they are not function-like.
  3600. FormatStyle Style = getGoogleStyle();
  3601. Style.ColumnLimit = 47;
  3602. verifyFormat("void someLongFunction(\n"
  3603. " int someLoooooooooooooongParameter) const {\n}",
  3604. getLLVMStyleWithColumns(47));
  3605. verifyFormat("LoooooongReturnType\n"
  3606. "someLoooooooongFunction() const {}",
  3607. getLLVMStyleWithColumns(47));
  3608. verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
  3609. " const {}",
  3610. Style);
  3611. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3612. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
  3613. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3614. " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
  3615. verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
  3616. " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
  3617. verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
  3618. " aaaaaaaaaaa aaaaa) const override;");
  3619. verifyGoogleFormat(
  3620. "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3621. " const override;");
  3622. // Even if the first parameter has to be wrapped.
  3623. verifyFormat("void someLongFunction(\n"
  3624. " int someLongParameter) const {}",
  3625. getLLVMStyleWithColumns(46));
  3626. verifyFormat("void someLongFunction(\n"
  3627. " int someLongParameter) const {}",
  3628. Style);
  3629. verifyFormat("void someLongFunction(\n"
  3630. " int someLongParameter) override {}",
  3631. Style);
  3632. verifyFormat("void someLongFunction(\n"
  3633. " int someLongParameter) OVERRIDE {}",
  3634. Style);
  3635. verifyFormat("void someLongFunction(\n"
  3636. " int someLongParameter) final {}",
  3637. Style);
  3638. verifyFormat("void someLongFunction(\n"
  3639. " int someLongParameter) FINAL {}",
  3640. Style);
  3641. verifyFormat("void someLongFunction(\n"
  3642. " int parameter) const override {}",
  3643. Style);
  3644. Style.BreakBeforeBraces = FormatStyle::BS_Allman;
  3645. verifyFormat("void someLongFunction(\n"
  3646. " int someLongParameter) const\n"
  3647. "{\n"
  3648. "}",
  3649. Style);
  3650. // Unless these are unknown annotations.
  3651. verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
  3652. " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3653. " LONG_AND_UGLY_ANNOTATION;");
  3654. // Breaking before function-like trailing annotations is fine to keep them
  3655. // close to their arguments.
  3656. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3657. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  3658. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  3659. " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
  3660. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
  3661. " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
  3662. verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
  3663. " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
  3664. verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
  3665. verifyFormat(
  3666. "void aaaaaaaaaaaaaaaaaa()\n"
  3667. " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3668. " aaaaaaaaaaaaaaaaaaaaaaaaa));");
  3669. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3670. " __attribute__((unused));");
  3671. verifyGoogleFormat(
  3672. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3673. " GUARDED_BY(aaaaaaaaaaaa);");
  3674. verifyGoogleFormat(
  3675. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3676. " GUARDED_BY(aaaaaaaaaaaa);");
  3677. verifyGoogleFormat(
  3678. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
  3679. " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  3680. verifyGoogleFormat(
  3681. "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
  3682. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  3683. }
  3684. TEST_F(FormatTest, FunctionAnnotations) {
  3685. verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
  3686. "int OldFunction(const string &parameter) {}");
  3687. verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
  3688. "string OldFunction(const string &parameter) {}");
  3689. verifyFormat("template <typename T>\n"
  3690. "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
  3691. "string OldFunction(const string &parameter) {}");
  3692. // Not function annotations.
  3693. verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  3694. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
  3695. verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
  3696. " ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
  3697. }
  3698. TEST_F(FormatTest, BreaksDesireably) {
  3699. verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  3700. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
  3701. " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
  3702. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3703. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
  3704. "}");
  3705. verifyFormat(
  3706. "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3707. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
  3708. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3709. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3710. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3711. verifyFormat(
  3712. "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3713. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  3714. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3715. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
  3716. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3717. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3718. verifyFormat(
  3719. "void f() {\n"
  3720. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
  3721. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3722. "}");
  3723. verifyFormat(
  3724. "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3725. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3726. verifyFormat(
  3727. "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3728. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  3729. verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  3730. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3731. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3732. // Indent consistently independent of call expression and unary operator.
  3733. verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3734. " dddddddddddddddddddddddddddddd));");
  3735. verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
  3736. " dddddddddddddddddddddddddddddd));");
  3737. verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
  3738. " dddddddddddddddddddddddddddddd));");
  3739. // This test case breaks on an incorrect memoization, i.e. an optimization not
  3740. // taking into account the StopAt value.
  3741. verifyFormat(
  3742. "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3743. " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3744. " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3745. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  3746. verifyFormat("{\n {\n {\n"
  3747. " Annotation.SpaceRequiredBefore =\n"
  3748. " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
  3749. " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
  3750. " }\n }\n}");
  3751. // Break on an outer level if there was a break on an inner level.
  3752. EXPECT_EQ("f(g(h(a, // comment\n"
  3753. " b, c),\n"
  3754. " d, e),\n"
  3755. " x, y);",
  3756. format("f(g(h(a, // comment\n"
  3757. " b, c), d, e), x, y);"));
  3758. // Prefer breaking similar line breaks.
  3759. verifyFormat(
  3760. "const int kTrackingOptions = NSTrackingMouseMoved |\n"
  3761. " NSTrackingMouseEnteredAndExited |\n"
  3762. " NSTrackingActiveAlways;");
  3763. }
  3764. TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
  3765. FormatStyle NoBinPacking = getGoogleStyle();
  3766. NoBinPacking.BinPackParameters = false;
  3767. NoBinPacking.BinPackArguments = true;
  3768. verifyFormat("void f() {\n"
  3769. " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
  3770. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  3771. "}",
  3772. NoBinPacking);
  3773. verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
  3774. " int aaaaaaaaaaaaaaaaaaaa,\n"
  3775. " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  3776. NoBinPacking);
  3777. }
  3778. TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
  3779. FormatStyle NoBinPacking = getGoogleStyle();
  3780. NoBinPacking.BinPackParameters = false;
  3781. NoBinPacking.BinPackArguments = false;
  3782. verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
  3783. " aaaaaaaaaaaaaaaaaaaa,\n"
  3784. " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
  3785. NoBinPacking);
  3786. verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
  3787. " aaaaaaaaaaaaa,\n"
  3788. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
  3789. NoBinPacking);
  3790. verifyFormat(
  3791. "aaaaaaaa(aaaaaaaaaaaaa,\n"
  3792. " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3793. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
  3794. " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3795. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
  3796. NoBinPacking);
  3797. verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  3798. " .aaaaaaaaaaaaaaaaaa();",
  3799. NoBinPacking);
  3800. verifyFormat("void f() {\n"
  3801. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3802. " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
  3803. "}",
  3804. NoBinPacking);
  3805. verifyFormat(
  3806. "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  3807. " aaaaaaaaaaaa,\n"
  3808. " aaaaaaaaaaaa);",
  3809. NoBinPacking);
  3810. verifyFormat(
  3811. "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
  3812. " ddddddddddddddddddddddddddddd),\n"
  3813. " test);",
  3814. NoBinPacking);
  3815. verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3816. " aaaaaaaaaaaaaaaaaaaaaaa,\n"
  3817. " aaaaaaaaaaaaaaaaaaaaaaa>\n"
  3818. " aaaaaaaaaaaaaaaaaa;",
  3819. NoBinPacking);
  3820. verifyFormat("a(\"a\"\n"
  3821. " \"a\",\n"
  3822. " a);");
  3823. NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
  3824. verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
  3825. " aaaaaaaaa,\n"
  3826. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  3827. NoBinPacking);
  3828. verifyFormat(
  3829. "void f() {\n"
  3830. " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
  3831. " .aaaaaaa();\n"
  3832. "}",
  3833. NoBinPacking);
  3834. verifyFormat(
  3835. "template <class SomeType, class SomeOtherType>\n"
  3836. "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
  3837. NoBinPacking);
  3838. }
  3839. TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
  3840. FormatStyle Style = getLLVMStyleWithColumns(15);
  3841. Style.ExperimentalAutoDetectBinPacking = true;
  3842. EXPECT_EQ("aaa(aaaa,\n"
  3843. " aaaa,\n"
  3844. " aaaa);\n"
  3845. "aaa(aaaa,\n"
  3846. " aaaa,\n"
  3847. " aaaa);",
  3848. format("aaa(aaaa,\n" // one-per-line
  3849. " aaaa,\n"
  3850. " aaaa );\n"
  3851. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  3852. Style));
  3853. EXPECT_EQ("aaa(aaaa, aaaa,\n"
  3854. " aaaa);\n"
  3855. "aaa(aaaa, aaaa,\n"
  3856. " aaaa);",
  3857. format("aaa(aaaa, aaaa,\n" // bin-packed
  3858. " aaaa );\n"
  3859. "aaa(aaaa, aaaa, aaaa);", // inconclusive
  3860. Style));
  3861. }
  3862. TEST_F(FormatTest, FormatsBuilderPattern) {
  3863. verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
  3864. " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
  3865. " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
  3866. " .StartsWith(\".init\", ORDER_INIT)\n"
  3867. " .StartsWith(\".fini\", ORDER_FINI)\n"
  3868. " .StartsWith(\".hash\", ORDER_HASH)\n"
  3869. " .Default(ORDER_TEXT);\n");
  3870. verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
  3871. " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
  3872. verifyFormat(
  3873. "aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  3874. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3875. " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
  3876. verifyFormat(
  3877. "aaaaaaa->aaaaaaa\n"
  3878. " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3879. " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
  3880. verifyFormat(
  3881. "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
  3882. " aaaaaaaaaaaaaa);");
  3883. verifyFormat(
  3884. "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
  3885. " aaaaaa->aaaaaaaaaaaa()\n"
  3886. " ->aaaaaaaaaaaaaaaa(\n"
  3887. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3888. " ->aaaaaaaaaaaaaaaaa();");
  3889. verifyGoogleFormat(
  3890. "void f() {\n"
  3891. " someo->Add((new util::filetools::Handler(dir))\n"
  3892. " ->OnEvent1(NewPermanentCallback(\n"
  3893. " this, &HandlerHolderClass::EventHandlerCBA))\n"
  3894. " ->OnEvent2(NewPermanentCallback(\n"
  3895. " this, &HandlerHolderClass::EventHandlerCBB))\n"
  3896. " ->OnEvent3(NewPermanentCallback(\n"
  3897. " this, &HandlerHolderClass::EventHandlerCBC))\n"
  3898. " ->OnEvent5(NewPermanentCallback(\n"
  3899. " this, &HandlerHolderClass::EventHandlerCBD))\n"
  3900. " ->OnEvent6(NewPermanentCallback(\n"
  3901. " this, &HandlerHolderClass::EventHandlerCBE)));\n"
  3902. "}");
  3903. verifyFormat(
  3904. "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
  3905. verifyFormat("aaaaaaaaaaaaaaa()\n"
  3906. " .aaaaaaaaaaaaaaa()\n"
  3907. " .aaaaaaaaaaaaaaa()\n"
  3908. " .aaaaaaaaaaaaaaa()\n"
  3909. " .aaaaaaaaaaaaaaa();");
  3910. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3911. " .aaaaaaaaaaaaaaa()\n"
  3912. " .aaaaaaaaaaaaaaa()\n"
  3913. " .aaaaaaaaaaaaaaa();");
  3914. verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3915. " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
  3916. " .aaaaaaaaaaaaaaa();");
  3917. verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3918. " ->aaaaaaaaaaaaaae(0)\n"
  3919. " ->aaaaaaaaaaaaaaa();");
  3920. // Don't linewrap after very short segments.
  3921. verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3922. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3923. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3924. verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3925. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3926. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3927. verifyFormat("aaa()\n"
  3928. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3929. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3930. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  3931. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  3932. " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  3933. " .has<bbbbbbbbbbbbbbbbbbbbb>();");
  3934. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
  3935. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  3936. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
  3937. // Prefer not to break after empty parentheses.
  3938. verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
  3939. " First->LastNewlineOffset);");
  3940. // Prefer not to create "hanging" indents.
  3941. verifyFormat(
  3942. "return !soooooooooooooome_map\n"
  3943. " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  3944. " .second;");
  3945. }
  3946. TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
  3947. verifyFormat(
  3948. "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  3949. " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
  3950. verifyFormat(
  3951. "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
  3952. " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
  3953. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  3954. " ccccccccccccccccccccccccc) {\n}");
  3955. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
  3956. " ccccccccccccccccccccccccc) {\n}");
  3957. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
  3958. " ccccccccccccccccccccccccc) {\n}");
  3959. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
  3960. " ccccccccccccccccccccccccc) {\n}");
  3961. verifyFormat(
  3962. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
  3963. " ccccccccccccccccccccccccc) {\n}");
  3964. verifyFormat(
  3965. "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
  3966. " ccccccccccccccccccccccccc) {\n}");
  3967. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
  3968. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
  3969. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
  3970. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  3971. verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
  3972. " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
  3973. " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
  3974. " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
  3975. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
  3976. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
  3977. " aaaaaaaaaaaaaaa != aa) {\n}");
  3978. verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
  3979. " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
  3980. " aaaaaaaaaaaaaaa != aa) {\n}");
  3981. }
  3982. TEST_F(FormatTest, BreaksAfterAssignments) {
  3983. verifyFormat(
  3984. "unsigned Cost =\n"
  3985. " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
  3986. " SI->getPointerAddressSpaceee());\n");
  3987. verifyFormat(
  3988. "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
  3989. " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
  3990. verifyFormat(
  3991. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
  3992. " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
  3993. verifyFormat("unsigned OriginalStartColumn =\n"
  3994. " SourceMgr.getSpellingColumnNumber(\n"
  3995. " Current.FormatTok.getStartOfNonWhitespace()) -\n"
  3996. " 1;");
  3997. }
  3998. TEST_F(FormatTest, AlignsAfterAssignments) {
  3999. verifyFormat(
  4000. "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4001. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  4002. verifyFormat(
  4003. "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4004. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  4005. verifyFormat(
  4006. "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4007. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  4008. verifyFormat(
  4009. "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4010. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  4011. verifyFormat(
  4012. "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4013. " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4014. " aaaaaaaaaaaaaaaaaaaaaaaa;");
  4015. }
  4016. TEST_F(FormatTest, AlignsAfterReturn) {
  4017. verifyFormat(
  4018. "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4019. " aaaaaaaaaaaaaaaaaaaaaaaaa;");
  4020. verifyFormat(
  4021. "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4022. " aaaaaaaaaaaaaaaaaaaaaaaaa);");
  4023. verifyFormat(
  4024. "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  4025. " aaaaaaaaaaaaaaaaaaaaaa();");
  4026. verifyFormat(
  4027. "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
  4028. " aaaaaaaaaaaaaaaaaaaaaa());");
  4029. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4030. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4031. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4032. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
  4033. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4034. verifyFormat("return\n"
  4035. " // true if code is one of a or b.\n"
  4036. " code == a || code == b;");
  4037. }
  4038. TEST_F(FormatTest, AlignsAfterOpenBracket) {
  4039. verifyFormat(
  4040. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
  4041. " aaaaaaaaa aaaaaaa) {}");
  4042. verifyFormat(
  4043. "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
  4044. " aaaaaaaaaaa aaaaaaaaa);");
  4045. verifyFormat(
  4046. "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
  4047. " aaaaaaaaaaaaaaaaaaaaa));");
  4048. FormatStyle Style = getLLVMStyle();
  4049. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  4050. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4051. " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
  4052. Style);
  4053. verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
  4054. " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
  4055. Style);
  4056. verifyFormat("SomeLongVariableName->someFunction(\n"
  4057. " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
  4058. Style);
  4059. verifyFormat(
  4060. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
  4061. " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  4062. Style);
  4063. verifyFormat(
  4064. "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
  4065. " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4066. Style);
  4067. verifyFormat(
  4068. "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
  4069. " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
  4070. Style);
  4071. Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
  4072. Style.BinPackArguments = false;
  4073. Style.BinPackParameters = false;
  4074. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4075. " aaaaaaaaaaa aaaaaaaa,\n"
  4076. " aaaaaaaaa aaaaaaa,\n"
  4077. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
  4078. Style);
  4079. verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
  4080. " aaaaaaaaaaa aaaaaaaaa,\n"
  4081. " aaaaaaaaaaa aaaaaaaaa,\n"
  4082. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4083. Style);
  4084. verifyFormat("SomeLongVariableName->someFunction(\n"
  4085. " foooooooo(\n"
  4086. " aaaaaaaaaaaaaaa,\n"
  4087. " aaaaaaaaaaaaaaaaaaaaa,\n"
  4088. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
  4089. Style);
  4090. }
  4091. TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
  4092. FormatStyle Style = getLLVMStyleWithColumns(40);
  4093. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4094. " bbbbbbbbbbbbbbbbbbbbbb);",
  4095. Style);
  4096. Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
  4097. Style.AlignOperands = false;
  4098. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4099. " bbbbbbbbbbbbbbbbbbbbbb);",
  4100. Style);
  4101. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  4102. Style.AlignOperands = true;
  4103. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4104. " bbbbbbbbbbbbbbbbbbbbbb);",
  4105. Style);
  4106. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  4107. Style.AlignOperands = false;
  4108. verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
  4109. " bbbbbbbbbbbbbbbbbbbbbb);",
  4110. Style);
  4111. }
  4112. TEST_F(FormatTest, BreaksConditionalExpressions) {
  4113. verifyFormat(
  4114. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4115. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4116. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4117. verifyFormat(
  4118. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4119. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4120. verifyFormat(
  4121. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
  4122. " : aaaaaaaaaaaaa);");
  4123. verifyFormat(
  4124. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4125. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4126. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4127. " aaaaaaaaaaaaa);");
  4128. verifyFormat(
  4129. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4130. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4131. " aaaaaaaaaaaaa);");
  4132. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4133. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4134. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4135. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4136. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4137. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4138. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4139. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4140. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4141. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4142. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4143. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4144. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4145. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4146. " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4147. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4148. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4149. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4150. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4151. " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4152. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  4153. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4154. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4155. " : aaaaaaaaaaaaaaaa;");
  4156. verifyFormat(
  4157. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4158. " ? aaaaaaaaaaaaaaa\n"
  4159. " : aaaaaaaaaaaaaaa;");
  4160. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  4161. " aaaaaaaaa\n"
  4162. " ? b\n"
  4163. " : c);");
  4164. verifyFormat("return aaaa == bbbb\n"
  4165. " // comment\n"
  4166. " ? aaaa\n"
  4167. " : bbbb;");
  4168. verifyFormat("unsigned Indent =\n"
  4169. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
  4170. " ? IndentForLevel[TheLine.Level]\n"
  4171. " : TheLine * 2,\n"
  4172. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  4173. getLLVMStyleWithColumns(70));
  4174. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  4175. " ? aaaaaaaaaaaaaaa\n"
  4176. " : bbbbbbbbbbbbbbb //\n"
  4177. " ? ccccccccccccccc\n"
  4178. " : ddddddddddddddd;");
  4179. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
  4180. " ? aaaaaaaaaaaaaaa\n"
  4181. " : (bbbbbbbbbbbbbbb //\n"
  4182. " ? ccccccccccccccc\n"
  4183. " : ddddddddddddddd);");
  4184. verifyFormat(
  4185. "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4186. " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
  4187. " aaaaaaaaaaaaaaaaaaaaa +\n"
  4188. " aaaaaaaaaaaaaaaaaaaaa\n"
  4189. " : aaaaaaaaaa;");
  4190. verifyFormat(
  4191. "aaaaaa = aaaaaaaaaaaa\n"
  4192. " ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4193. " : aaaaaaaaaaaaaaaaaaaaaa\n"
  4194. " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4195. FormatStyle NoBinPacking = getLLVMStyle();
  4196. NoBinPacking.BinPackArguments = false;
  4197. verifyFormat(
  4198. "void f() {\n"
  4199. " g(aaa,\n"
  4200. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  4201. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4202. " ? aaaaaaaaaaaaaaa\n"
  4203. " : aaaaaaaaaaaaaaa);\n"
  4204. "}",
  4205. NoBinPacking);
  4206. verifyFormat(
  4207. "void f() {\n"
  4208. " g(aaa,\n"
  4209. " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
  4210. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4211. " ?: aaaaaaaaaaaaaaa);\n"
  4212. "}",
  4213. NoBinPacking);
  4214. verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
  4215. " // comment.\n"
  4216. " ccccccccccccccccccccccccccccccccccccccc\n"
  4217. " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4218. " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
  4219. // Assignments in conditional expressions. Apparently not uncommon :-(.
  4220. verifyFormat("return a != b\n"
  4221. " // comment\n"
  4222. " ? a = b\n"
  4223. " : a = b;");
  4224. verifyFormat("return a != b\n"
  4225. " // comment\n"
  4226. " ? a = a != b\n"
  4227. " // comment\n"
  4228. " ? a = b\n"
  4229. " : a\n"
  4230. " : a;\n");
  4231. verifyFormat("return a != b\n"
  4232. " // comment\n"
  4233. " ? a\n"
  4234. " : a = a != b\n"
  4235. " // comment\n"
  4236. " ? a = b\n"
  4237. " : a;");
  4238. }
  4239. TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
  4240. FormatStyle Style = getLLVMStyle();
  4241. Style.BreakBeforeTernaryOperators = false;
  4242. Style.ColumnLimit = 70;
  4243. verifyFormat(
  4244. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4245. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4246. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4247. Style);
  4248. verifyFormat(
  4249. "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4250. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4251. Style);
  4252. verifyFormat(
  4253. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
  4254. " aaaaaaaaaaaaa);",
  4255. Style);
  4256. verifyFormat(
  4257. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4258. " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4259. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4260. " aaaaaaaaaaaaa);",
  4261. Style);
  4262. verifyFormat(
  4263. "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4264. " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4265. " aaaaaaaaaaaaa);",
  4266. Style);
  4267. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4268. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4269. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  4270. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4271. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4272. Style);
  4273. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4274. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4275. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4276. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
  4277. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4278. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4279. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4280. Style);
  4281. verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4282. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
  4283. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4284. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
  4285. " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4286. Style);
  4287. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4288. " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4289. " aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4290. Style);
  4291. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
  4292. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4293. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
  4294. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4295. Style);
  4296. verifyFormat(
  4297. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
  4298. " aaaaaaaaaaaaaaa :\n"
  4299. " aaaaaaaaaaaaaaa;",
  4300. Style);
  4301. verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
  4302. " aaaaaaaaa ?\n"
  4303. " b :\n"
  4304. " c);",
  4305. Style);
  4306. verifyFormat(
  4307. "unsigned Indent =\n"
  4308. " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n"
  4309. " IndentForLevel[TheLine.Level] :\n"
  4310. " TheLine * 2,\n"
  4311. " TheLine.InPPDirective, PreviousEndOfLineColumn);",
  4312. Style);
  4313. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  4314. " aaaaaaaaaaaaaaa :\n"
  4315. " bbbbbbbbbbbbbbb ? //\n"
  4316. " ccccccccccccccc :\n"
  4317. " ddddddddddddddd;",
  4318. Style);
  4319. verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
  4320. " aaaaaaaaaaaaaaa :\n"
  4321. " (bbbbbbbbbbbbbbb ? //\n"
  4322. " ccccccccccccccc :\n"
  4323. " ddddddddddddddd);",
  4324. Style);
  4325. }
  4326. TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
  4327. verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
  4328. " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
  4329. verifyFormat("bool a = true, b = false;");
  4330. verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4331. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
  4332. " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
  4333. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
  4334. verifyFormat(
  4335. "bool aaaaaaaaaaaaaaaaaaaaa =\n"
  4336. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
  4337. " d = e && f;");
  4338. verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
  4339. " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
  4340. verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  4341. " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
  4342. verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
  4343. " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
  4344. FormatStyle Style = getGoogleStyle();
  4345. Style.PointerAlignment = FormatStyle::PAS_Left;
  4346. Style.DerivePointerAlignment = false;
  4347. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4348. " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
  4349. " *b = bbbbbbbbbbbbbbbbbbb;",
  4350. Style);
  4351. verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
  4352. " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
  4353. Style);
  4354. }
  4355. TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
  4356. verifyFormat("arr[foo ? bar : baz];");
  4357. verifyFormat("f()[foo ? bar : baz];");
  4358. verifyFormat("(a + b)[foo ? bar : baz];");
  4359. verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
  4360. }
  4361. TEST_F(FormatTest, AlignsStringLiterals) {
  4362. verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
  4363. " \"short literal\");");
  4364. verifyFormat(
  4365. "looooooooooooooooooooooooongFunction(\n"
  4366. " \"short literal\"\n"
  4367. " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
  4368. verifyFormat("someFunction(\"Always break between multi-line\"\n"
  4369. " \" string literals\",\n"
  4370. " and, other, parameters);");
  4371. EXPECT_EQ("fun + \"1243\" /* comment */\n"
  4372. " \"5678\";",
  4373. format("fun + \"1243\" /* comment */\n"
  4374. " \"5678\";",
  4375. getLLVMStyleWithColumns(28)));
  4376. EXPECT_EQ(
  4377. "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  4378. " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
  4379. " \"aaaaaaaaaaaaaaaa\";",
  4380. format("aaaaaa ="
  4381. "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  4382. "aaaaaaaaaaaaaaaaaaaaa\" "
  4383. "\"aaaaaaaaaaaaaaaa\";"));
  4384. verifyFormat("a = a + \"a\"\n"
  4385. " \"a\"\n"
  4386. " \"a\";");
  4387. verifyFormat("f(\"a\", \"b\"\n"
  4388. " \"c\");");
  4389. verifyFormat(
  4390. "#define LL_FORMAT \"ll\"\n"
  4391. "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
  4392. " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
  4393. verifyFormat("#define A(X) \\\n"
  4394. " \"aaaaa\" #X \"bbbbbb\" \\\n"
  4395. " \"ccccc\"",
  4396. getLLVMStyleWithColumns(23));
  4397. verifyFormat("#define A \"def\"\n"
  4398. "f(\"abc\" A \"ghi\"\n"
  4399. " \"jkl\");");
  4400. verifyFormat("f(L\"a\"\n"
  4401. " L\"b\");");
  4402. verifyFormat("#define A(X) \\\n"
  4403. " L\"aaaaa\" #X L\"bbbbbb\" \\\n"
  4404. " L\"ccccc\"",
  4405. getLLVMStyleWithColumns(25));
  4406. verifyFormat("f(@\"a\"\n"
  4407. " @\"b\");");
  4408. verifyFormat("NSString s = @\"a\"\n"
  4409. " @\"b\"\n"
  4410. " @\"c\";");
  4411. verifyFormat("NSString s = @\"a\"\n"
  4412. " \"b\"\n"
  4413. " \"c\";");
  4414. }
  4415. TEST_F(FormatTest, ReturnTypeBreakingStyle) {
  4416. FormatStyle Style = getLLVMStyle();
  4417. // No declarations or definitions should be moved to own line.
  4418. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
  4419. verifyFormat("class A {\n"
  4420. " int f() { return 1; }\n"
  4421. " int g();\n"
  4422. "};\n"
  4423. "int f() { return 1; }\n"
  4424. "int g();\n",
  4425. Style);
  4426. // All declarations and definitions should have the return type moved to its
  4427. // own
  4428. // line.
  4429. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
  4430. verifyFormat("class E {\n"
  4431. " int\n"
  4432. " f() {\n"
  4433. " return 1;\n"
  4434. " }\n"
  4435. " int\n"
  4436. " g();\n"
  4437. "};\n"
  4438. "int\n"
  4439. "f() {\n"
  4440. " return 1;\n"
  4441. "}\n"
  4442. "int\n"
  4443. "g();\n",
  4444. Style);
  4445. // Top-level definitions, and no kinds of declarations should have the
  4446. // return type moved to its own line.
  4447. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
  4448. verifyFormat("class B {\n"
  4449. " int f() { return 1; }\n"
  4450. " int g();\n"
  4451. "};\n"
  4452. "int\n"
  4453. "f() {\n"
  4454. " return 1;\n"
  4455. "}\n"
  4456. "int g();\n",
  4457. Style);
  4458. // Top-level definitions and declarations should have the return type moved
  4459. // to its own line.
  4460. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
  4461. verifyFormat("class C {\n"
  4462. " int f() { return 1; }\n"
  4463. " int g();\n"
  4464. "};\n"
  4465. "int\n"
  4466. "f() {\n"
  4467. " return 1;\n"
  4468. "}\n"
  4469. "int\n"
  4470. "g();\n",
  4471. Style);
  4472. // All definitions should have the return type moved to its own line, but no
  4473. // kinds of declarations.
  4474. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
  4475. verifyFormat("class D {\n"
  4476. " int\n"
  4477. " f() {\n"
  4478. " return 1;\n"
  4479. " }\n"
  4480. " int g();\n"
  4481. "};\n"
  4482. "int\n"
  4483. "f() {\n"
  4484. " return 1;\n"
  4485. "}\n"
  4486. "int g();\n",
  4487. Style);
  4488. verifyFormat("const char *\n"
  4489. "f(void) {\n" // Break here.
  4490. " return \"\";\n"
  4491. "}\n"
  4492. "const char *bar(void);\n", // No break here.
  4493. Style);
  4494. verifyFormat("template <class T>\n"
  4495. "T *\n"
  4496. "f(T &c) {\n" // Break here.
  4497. " return NULL;\n"
  4498. "}\n"
  4499. "template <class T> T *f(T &c);\n", // No break here.
  4500. Style);
  4501. verifyFormat("class C {\n"
  4502. " int\n"
  4503. " operator+() {\n"
  4504. " return 1;\n"
  4505. " }\n"
  4506. " int\n"
  4507. " operator()() {\n"
  4508. " return 1;\n"
  4509. " }\n"
  4510. "};\n",
  4511. Style);
  4512. verifyFormat("void\n"
  4513. "A::operator()() {}\n"
  4514. "void\n"
  4515. "A::operator>>() {}\n"
  4516. "void\n"
  4517. "A::operator+() {}\n",
  4518. Style);
  4519. verifyFormat("void *operator new(std::size_t s);", // No break here.
  4520. Style);
  4521. verifyFormat("void *\n"
  4522. "operator new(std::size_t s) {}",
  4523. Style);
  4524. verifyFormat("void *\n"
  4525. "operator delete[](void *ptr) {}",
  4526. Style);
  4527. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  4528. verifyFormat("const char *\n"
  4529. "f(void)\n" // Break here.
  4530. "{\n"
  4531. " return \"\";\n"
  4532. "}\n"
  4533. "const char *bar(void);\n", // No break here.
  4534. Style);
  4535. verifyFormat("template <class T>\n"
  4536. "T *\n" // Problem here: no line break
  4537. "f(T &c)\n" // Break here.
  4538. "{\n"
  4539. " return NULL;\n"
  4540. "}\n"
  4541. "template <class T> T *f(T &c);\n", // No break here.
  4542. Style);
  4543. }
  4544. TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
  4545. FormatStyle NoBreak = getLLVMStyle();
  4546. NoBreak.AlwaysBreakBeforeMultilineStrings = false;
  4547. FormatStyle Break = getLLVMStyle();
  4548. Break.AlwaysBreakBeforeMultilineStrings = true;
  4549. verifyFormat("aaaa = \"bbbb\"\n"
  4550. " \"cccc\";",
  4551. NoBreak);
  4552. verifyFormat("aaaa =\n"
  4553. " \"bbbb\"\n"
  4554. " \"cccc\";",
  4555. Break);
  4556. verifyFormat("aaaa(\"bbbb\"\n"
  4557. " \"cccc\");",
  4558. NoBreak);
  4559. verifyFormat("aaaa(\n"
  4560. " \"bbbb\"\n"
  4561. " \"cccc\");",
  4562. Break);
  4563. verifyFormat("aaaa(qqq, \"bbbb\"\n"
  4564. " \"cccc\");",
  4565. NoBreak);
  4566. verifyFormat("aaaa(qqq,\n"
  4567. " \"bbbb\"\n"
  4568. " \"cccc\");",
  4569. Break);
  4570. verifyFormat("aaaa(qqq,\n"
  4571. " L\"bbbb\"\n"
  4572. " L\"cccc\");",
  4573. Break);
  4574. verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
  4575. " \"bbbb\"));",
  4576. Break);
  4577. verifyFormat("string s = someFunction(\n"
  4578. " \"abc\"\n"
  4579. " \"abc\");",
  4580. Break);
  4581. // As we break before unary operators, breaking right after them is bad.
  4582. verifyFormat("string foo = abc ? \"x\"\n"
  4583. " \"blah blah blah blah blah blah\"\n"
  4584. " : \"y\";",
  4585. Break);
  4586. // Don't break if there is no column gain.
  4587. verifyFormat("f(\"aaaa\"\n"
  4588. " \"bbbb\");",
  4589. Break);
  4590. // Treat literals with escaped newlines like multi-line string literals.
  4591. EXPECT_EQ("x = \"a\\\n"
  4592. "b\\\n"
  4593. "c\";",
  4594. format("x = \"a\\\n"
  4595. "b\\\n"
  4596. "c\";",
  4597. NoBreak));
  4598. EXPECT_EQ("xxxx =\n"
  4599. " \"a\\\n"
  4600. "b\\\n"
  4601. "c\";",
  4602. format("xxxx = \"a\\\n"
  4603. "b\\\n"
  4604. "c\";",
  4605. Break));
  4606. // Exempt ObjC strings for now.
  4607. EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
  4608. " @\"bbbb\";",
  4609. format("NSString *const kString = @\"aaaa\"\n"
  4610. "@\"bbbb\";",
  4611. Break));
  4612. Break.ColumnLimit = 0;
  4613. verifyFormat("const char *hello = \"hello llvm\";", Break);
  4614. }
  4615. TEST_F(FormatTest, AlignsPipes) {
  4616. verifyFormat(
  4617. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4618. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4619. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4620. verifyFormat(
  4621. "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
  4622. " << aaaaaaaaaaaaaaaaaaaa;");
  4623. verifyFormat(
  4624. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4625. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4626. verifyFormat(
  4627. "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
  4628. " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
  4629. " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
  4630. verifyFormat(
  4631. "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4632. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4633. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4634. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4635. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4636. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4637. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4638. verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
  4639. " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
  4640. verifyFormat(
  4641. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4642. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4643. verifyFormat("return out << \"somepacket = {\\n\"\n"
  4644. " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
  4645. " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
  4646. " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
  4647. " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
  4648. " << \"}\";");
  4649. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  4650. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
  4651. " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
  4652. verifyFormat(
  4653. "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
  4654. " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
  4655. " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
  4656. " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
  4657. " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
  4658. verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
  4659. " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  4660. verifyFormat(
  4661. "void f() {\n"
  4662. " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
  4663. " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
  4664. "}");
  4665. verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
  4666. " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
  4667. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4668. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4669. " aaaaaaaaaaaaaaaaaaaaa)\n"
  4670. " << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4671. verifyFormat("LOG_IF(aaa == //\n"
  4672. " bbb)\n"
  4673. " << a << b;");
  4674. // Breaking before the first "<<" is generally not desirable.
  4675. verifyFormat(
  4676. "llvm::errs()\n"
  4677. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4678. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4679. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4680. " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4681. getLLVMStyleWithColumns(70));
  4682. verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4683. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4684. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4685. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4686. " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
  4687. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  4688. getLLVMStyleWithColumns(70));
  4689. // But sometimes, breaking before the first "<<" is desirable.
  4690. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
  4691. " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
  4692. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
  4693. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4694. " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4695. verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
  4696. " << BEF << IsTemplate << Description << E->getType();");
  4697. verifyFormat(
  4698. "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4699. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4700. // Incomplete string literal.
  4701. EXPECT_EQ("llvm::errs() << \"\n"
  4702. " << a;",
  4703. format("llvm::errs() << \"\n<<a;"));
  4704. verifyFormat("void f() {\n"
  4705. " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
  4706. " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
  4707. "}");
  4708. // Handle 'endl'.
  4709. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
  4710. " << bbbbbbbbbbbbbbbbbbbbbb << endl;");
  4711. verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
  4712. }
  4713. TEST_F(FormatTest, UnderstandsEquals) {
  4714. verifyFormat(
  4715. "aaaaaaaaaaaaaaaaa =\n"
  4716. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  4717. verifyFormat(
  4718. "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4719. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  4720. verifyFormat(
  4721. "if (a) {\n"
  4722. " f();\n"
  4723. "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4724. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  4725. "}");
  4726. verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  4727. " 100000000 + 10000000) {\n}");
  4728. }
  4729. TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
  4730. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  4731. " .looooooooooooooooooooooooooooooooooooooongFunction();");
  4732. verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
  4733. " ->looooooooooooooooooooooooooooooooooooooongFunction();");
  4734. verifyFormat(
  4735. "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
  4736. " Parameter2);");
  4737. verifyFormat(
  4738. "ShortObject->shortFunction(\n"
  4739. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
  4740. " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
  4741. verifyFormat("loooooooooooooongFunction(\n"
  4742. " LoooooooooooooongObject->looooooooooooooooongFunction());");
  4743. verifyFormat(
  4744. "function(LoooooooooooooooooooooooooooooooooooongObject\n"
  4745. " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
  4746. verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  4747. " .WillRepeatedly(Return(SomeValue));");
  4748. verifyFormat("void f() {\n"
  4749. " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
  4750. " .Times(2)\n"
  4751. " .WillRepeatedly(Return(SomeValue));\n"
  4752. "}");
  4753. verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
  4754. " ccccccccccccccccccccccc);");
  4755. verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4756. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4757. " .aaaaa(aaaaa),\n"
  4758. " aaaaaaaaaaaaaaaaaaaaa);");
  4759. verifyFormat("void f() {\n"
  4760. " aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4761. " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
  4762. "}");
  4763. verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4764. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4765. " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4766. " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4767. " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  4768. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4769. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4770. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4771. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
  4772. "}");
  4773. // Here, it is not necessary to wrap at "." or "->".
  4774. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
  4775. " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
  4776. verifyFormat(
  4777. "aaaaaaaaaaa->aaaaaaaaa(\n"
  4778. " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4779. " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
  4780. verifyFormat(
  4781. "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4782. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
  4783. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
  4784. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  4785. verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
  4786. " aaaaaaaaa()->aaaaaa()->aaaaa());");
  4787. verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4788. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4789. " .a();");
  4790. FormatStyle NoBinPacking = getLLVMStyle();
  4791. NoBinPacking.BinPackParameters = false;
  4792. verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  4793. " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  4794. " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
  4795. " aaaaaaaaaaaaaaaaaaa,\n"
  4796. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
  4797. NoBinPacking);
  4798. // If there is a subsequent call, change to hanging indentation.
  4799. verifyFormat(
  4800. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4801. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
  4802. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4803. verifyFormat(
  4804. "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4805. " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
  4806. verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4807. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4808. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4809. verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4810. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  4811. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  4812. }
  4813. TEST_F(FormatTest, WrapsTemplateDeclarations) {
  4814. verifyFormat("template <typename T>\n"
  4815. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  4816. verifyFormat("template <typename T>\n"
  4817. "// T should be one of {A, B}.\n"
  4818. "virtual void loooooooooooongFunction(int Param1, int Param2);");
  4819. verifyFormat(
  4820. "template <typename T>\n"
  4821. "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
  4822. verifyFormat("template <typename T>\n"
  4823. "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
  4824. " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
  4825. verifyFormat(
  4826. "template <typename T>\n"
  4827. "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
  4828. " int Paaaaaaaaaaaaaaaaaaaaram2);");
  4829. verifyFormat(
  4830. "template <typename T>\n"
  4831. "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
  4832. " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
  4833. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4834. verifyFormat("template <typename T>\n"
  4835. "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4836. " int aaaaaaaaaaaaaaaaaaaaaa);");
  4837. verifyFormat(
  4838. "template <typename T1, typename T2 = char, typename T3 = char,\n"
  4839. " typename T4 = char>\n"
  4840. "void f();");
  4841. verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
  4842. " template <typename> class cccccccccccccccccccccc,\n"
  4843. " typename ddddddddddddd>\n"
  4844. "class C {};");
  4845. verifyFormat(
  4846. "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
  4847. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  4848. verifyFormat("void f() {\n"
  4849. " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
  4850. " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
  4851. "}");
  4852. verifyFormat("template <typename T> class C {};");
  4853. verifyFormat("template <typename T> void f();");
  4854. verifyFormat("template <typename T> void f() {}");
  4855. verifyFormat(
  4856. "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  4857. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4858. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
  4859. " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
  4860. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4861. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
  4862. " bbbbbbbbbbbbbbbbbbbbbbbb);",
  4863. getLLVMStyleWithColumns(72));
  4864. EXPECT_EQ("static_cast<A< //\n"
  4865. " B> *>(\n"
  4866. "\n"
  4867. " );",
  4868. format("static_cast<A<//\n"
  4869. " B>*>(\n"
  4870. "\n"
  4871. " );"));
  4872. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4873. " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
  4874. FormatStyle AlwaysBreak = getLLVMStyle();
  4875. AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
  4876. verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
  4877. verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
  4878. verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
  4879. verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4880. " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
  4881. " ccccccccccccccccccccccccccccccccccccccccccccccc);");
  4882. verifyFormat("template <template <typename> class Fooooooo,\n"
  4883. " template <typename> class Baaaaaaar>\n"
  4884. "struct C {};",
  4885. AlwaysBreak);
  4886. verifyFormat("template <typename T> // T can be A, B or C.\n"
  4887. "struct C {};",
  4888. AlwaysBreak);
  4889. }
  4890. TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
  4891. verifyFormat(
  4892. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4893. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4894. verifyFormat(
  4895. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4896. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4897. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
  4898. // FIXME: Should we have the extra indent after the second break?
  4899. verifyFormat(
  4900. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4901. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4902. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4903. verifyFormat(
  4904. "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
  4905. " cccccccccccccccccccccccccccccccccccccccccccccc());");
  4906. // Breaking at nested name specifiers is generally not desirable.
  4907. verifyFormat(
  4908. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4909. " aaaaaaaaaaaaaaaaaaaaaaa);");
  4910. verifyFormat(
  4911. "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4912. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  4913. " aaaaaaaaaaaaaaaaaaaaa);",
  4914. getLLVMStyleWithColumns(74));
  4915. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
  4916. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  4917. " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
  4918. }
  4919. TEST_F(FormatTest, UnderstandsTemplateParameters) {
  4920. verifyFormat("A<int> a;");
  4921. verifyFormat("A<A<A<int>>> a;");
  4922. verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
  4923. verifyFormat("bool x = a < 1 || 2 > a;");
  4924. verifyFormat("bool x = 5 < f<int>();");
  4925. verifyFormat("bool x = f<int>() > 5;");
  4926. verifyFormat("bool x = 5 < a<int>::x;");
  4927. verifyFormat("bool x = a < 4 ? a > 2 : false;");
  4928. verifyFormat("bool x = f() ? a < 2 : a > 2;");
  4929. verifyGoogleFormat("A<A<int>> a;");
  4930. verifyGoogleFormat("A<A<A<int>>> a;");
  4931. verifyGoogleFormat("A<A<A<A<int>>>> a;");
  4932. verifyGoogleFormat("A<A<int> > a;");
  4933. verifyGoogleFormat("A<A<A<int> > > a;");
  4934. verifyGoogleFormat("A<A<A<A<int> > > > a;");
  4935. verifyGoogleFormat("A<::A<int>> a;");
  4936. verifyGoogleFormat("A<::A> a;");
  4937. verifyGoogleFormat("A< ::A> a;");
  4938. verifyGoogleFormat("A< ::A<int> > a;");
  4939. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
  4940. EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
  4941. EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
  4942. EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
  4943. EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
  4944. format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
  4945. verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
  4946. verifyFormat("test >> a >> b;");
  4947. verifyFormat("test << a >> b;");
  4948. verifyFormat("f<int>();");
  4949. verifyFormat("template <typename T> void f() {}");
  4950. verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
  4951. verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
  4952. "sizeof(char)>::type>;");
  4953. verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
  4954. // Not template parameters.
  4955. verifyFormat("return a < b && c > d;");
  4956. verifyFormat("void f() {\n"
  4957. " while (a < b && c > d) {\n"
  4958. " }\n"
  4959. "}");
  4960. verifyFormat("template <typename... Types>\n"
  4961. "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
  4962. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  4963. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
  4964. getLLVMStyleWithColumns(60));
  4965. verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
  4966. verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
  4967. verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
  4968. }
  4969. TEST_F(FormatTest, UnderstandsBinaryOperators) {
  4970. verifyFormat("COMPARE(a, ==, b);");
  4971. }
  4972. TEST_F(FormatTest, UnderstandsPointersToMembers) {
  4973. verifyFormat("int A::*x;");
  4974. verifyFormat("int (S::*func)(void *);");
  4975. verifyFormat("void f() { int (S::*func)(void *); }");
  4976. verifyFormat("typedef bool *(Class::*Member)() const;");
  4977. verifyFormat("void f() {\n"
  4978. " (a->*f)();\n"
  4979. " a->*x;\n"
  4980. " (a.*f)();\n"
  4981. " ((*a).*f)();\n"
  4982. " a.*x;\n"
  4983. "}");
  4984. verifyFormat("void f() {\n"
  4985. " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  4986. " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
  4987. "}");
  4988. verifyFormat(
  4989. "(aaaaaaaaaa->*bbbbbbb)(\n"
  4990. " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
  4991. FormatStyle Style = getLLVMStyle();
  4992. Style.PointerAlignment = FormatStyle::PAS_Left;
  4993. verifyFormat("typedef bool* (Class::*Member)() const;", Style);
  4994. }
  4995. TEST_F(FormatTest, UnderstandsUnaryOperators) {
  4996. verifyFormat("int a = -2;");
  4997. verifyFormat("f(-1, -2, -3);");
  4998. verifyFormat("a[-1] = 5;");
  4999. verifyFormat("int a = 5 + -2;");
  5000. verifyFormat("if (i == -1) {\n}");
  5001. verifyFormat("if (i != -1) {\n}");
  5002. verifyFormat("if (i > -1) {\n}");
  5003. verifyFormat("if (i < -1) {\n}");
  5004. verifyFormat("++(a->f());");
  5005. verifyFormat("--(a->f());");
  5006. verifyFormat("(a->f())++;");
  5007. verifyFormat("a[42]++;");
  5008. verifyFormat("if (!(a->f())) {\n}");
  5009. verifyFormat("a-- > b;");
  5010. verifyFormat("b ? -a : c;");
  5011. verifyFormat("n * sizeof char16;");
  5012. verifyFormat("n * alignof char16;", getGoogleStyle());
  5013. verifyFormat("sizeof(char);");
  5014. verifyFormat("alignof(char);", getGoogleStyle());
  5015. verifyFormat("return -1;");
  5016. verifyFormat("switch (a) {\n"
  5017. "case -1:\n"
  5018. " break;\n"
  5019. "}");
  5020. verifyFormat("#define X -1");
  5021. verifyFormat("#define X -kConstant");
  5022. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
  5023. verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
  5024. verifyFormat("int a = /* confusing comment */ -1;");
  5025. // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
  5026. verifyFormat("int a = i /* confusing comment */++;");
  5027. }
  5028. TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
  5029. verifyFormat("if (!aaaaaaaaaa( // break\n"
  5030. " aaaaa)) {\n"
  5031. "}");
  5032. verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
  5033. " aaaaa));");
  5034. verifyFormat("*aaa = aaaaaaa( // break\n"
  5035. " bbbbbb);");
  5036. }
  5037. TEST_F(FormatTest, UnderstandsOverloadedOperators) {
  5038. verifyFormat("bool operator<();");
  5039. verifyFormat("bool operator>();");
  5040. verifyFormat("bool operator=();");
  5041. verifyFormat("bool operator==();");
  5042. verifyFormat("bool operator!=();");
  5043. verifyFormat("int operator+();");
  5044. verifyFormat("int operator++();");
  5045. verifyFormat("bool operator();");
  5046. verifyFormat("bool operator()();");
  5047. verifyFormat("bool operator[]();");
  5048. verifyFormat("operator bool();");
  5049. verifyFormat("operator int();");
  5050. verifyFormat("operator void *();");
  5051. verifyFormat("operator SomeType<int>();");
  5052. verifyFormat("operator SomeType<int, int>();");
  5053. verifyFormat("operator SomeType<SomeType<int>>();");
  5054. verifyFormat("void *operator new(std::size_t size);");
  5055. verifyFormat("void *operator new[](std::size_t size);");
  5056. verifyFormat("void operator delete(void *ptr);");
  5057. verifyFormat("void operator delete[](void *ptr);");
  5058. verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
  5059. "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
  5060. verifyFormat(
  5061. "ostream &operator<<(ostream &OutputStream,\n"
  5062. " SomeReallyLongType WithSomeReallyLongValue);");
  5063. verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
  5064. " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
  5065. " return left.group < right.group;\n"
  5066. "}");
  5067. verifyFormat("SomeType &operator=(const SomeType &S);");
  5068. verifyFormat("f.template operator()<int>();");
  5069. verifyGoogleFormat("operator void*();");
  5070. verifyGoogleFormat("operator SomeType<SomeType<int>>();");
  5071. verifyGoogleFormat("operator ::A();");
  5072. verifyFormat("using A::operator+;");
  5073. verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
  5074. "int i;");
  5075. }
  5076. TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
  5077. verifyFormat("Deleted &operator=(const Deleted &) & = default;");
  5078. verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
  5079. verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
  5080. verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
  5081. verifyFormat("Deleted &operator=(const Deleted &) &;");
  5082. verifyFormat("Deleted &operator=(const Deleted &) &&;");
  5083. verifyFormat("SomeType MemberFunction(const Deleted &) &;");
  5084. verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
  5085. verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
  5086. verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
  5087. verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
  5088. FormatStyle AlignLeft = getLLVMStyle();
  5089. AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
  5090. verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
  5091. verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
  5092. AlignLeft);
  5093. verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
  5094. verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
  5095. FormatStyle Spaces = getLLVMStyle();
  5096. Spaces.SpacesInCStyleCastParentheses = true;
  5097. verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
  5098. verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
  5099. verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
  5100. verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
  5101. Spaces.SpacesInCStyleCastParentheses = false;
  5102. Spaces.SpacesInParentheses = true;
  5103. verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
  5104. verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
  5105. verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
  5106. verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
  5107. }
  5108. TEST_F(FormatTest, UnderstandsNewAndDelete) {
  5109. verifyFormat("void f() {\n"
  5110. " A *a = new A;\n"
  5111. " A *a = new (placement) A;\n"
  5112. " delete a;\n"
  5113. " delete (A *)a;\n"
  5114. "}");
  5115. verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
  5116. " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
  5117. verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  5118. " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
  5119. " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
  5120. verifyFormat("delete[] h->p;");
  5121. }
  5122. TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
  5123. verifyFormat("int *f(int *a) {}");
  5124. verifyFormat("int main(int argc, char **argv) {}");
  5125. verifyFormat("Test::Test(int b) : a(b * b) {}");
  5126. verifyIndependentOfContext("f(a, *a);");
  5127. verifyFormat("void g() { f(*a); }");
  5128. verifyIndependentOfContext("int a = b * 10;");
  5129. verifyIndependentOfContext("int a = 10 * b;");
  5130. verifyIndependentOfContext("int a = b * c;");
  5131. verifyIndependentOfContext("int a += b * c;");
  5132. verifyIndependentOfContext("int a -= b * c;");
  5133. verifyIndependentOfContext("int a *= b * c;");
  5134. verifyIndependentOfContext("int a /= b * c;");
  5135. verifyIndependentOfContext("int a = *b;");
  5136. verifyIndependentOfContext("int a = *b * c;");
  5137. verifyIndependentOfContext("int a = b * *c;");
  5138. verifyIndependentOfContext("int a = b * (10);");
  5139. verifyIndependentOfContext("S << b * (10);");
  5140. verifyIndependentOfContext("return 10 * b;");
  5141. verifyIndependentOfContext("return *b * *c;");
  5142. verifyIndependentOfContext("return a & ~b;");
  5143. verifyIndependentOfContext("f(b ? *c : *d);");
  5144. verifyIndependentOfContext("int a = b ? *c : *d;");
  5145. verifyIndependentOfContext("*b = a;");
  5146. verifyIndependentOfContext("a * ~b;");
  5147. verifyIndependentOfContext("a * !b;");
  5148. verifyIndependentOfContext("a * +b;");
  5149. verifyIndependentOfContext("a * -b;");
  5150. verifyIndependentOfContext("a * ++b;");
  5151. verifyIndependentOfContext("a * --b;");
  5152. verifyIndependentOfContext("a[4] * b;");
  5153. verifyIndependentOfContext("a[a * a] = 1;");
  5154. verifyIndependentOfContext("f() * b;");
  5155. verifyIndependentOfContext("a * [self dostuff];");
  5156. verifyIndependentOfContext("int x = a * (a + b);");
  5157. verifyIndependentOfContext("(a *)(a + b);");
  5158. verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
  5159. verifyIndependentOfContext("int *pa = (int *)&a;");
  5160. verifyIndependentOfContext("return sizeof(int **);");
  5161. verifyIndependentOfContext("return sizeof(int ******);");
  5162. verifyIndependentOfContext("return (int **&)a;");
  5163. verifyIndependentOfContext("f((*PointerToArray)[10]);");
  5164. verifyFormat("void f(Type (*parameter)[10]) {}");
  5165. verifyFormat("void f(Type (&parameter)[10]) {}");
  5166. verifyGoogleFormat("return sizeof(int**);");
  5167. verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
  5168. verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
  5169. verifyFormat("auto a = [](int **&, int ***) {};");
  5170. verifyFormat("auto PointerBinding = [](const char *S) {};");
  5171. verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
  5172. verifyFormat("[](const decltype(*a) &value) {}");
  5173. verifyFormat("decltype(a * b) F();");
  5174. verifyFormat("#define MACRO() [](A *a) { return 1; }");
  5175. verifyIndependentOfContext("typedef void (*f)(int *a);");
  5176. verifyIndependentOfContext("int i{a * b};");
  5177. verifyIndependentOfContext("aaa && aaa->f();");
  5178. verifyIndependentOfContext("int x = ~*p;");
  5179. verifyFormat("Constructor() : a(a), area(width * height) {}");
  5180. verifyFormat("Constructor() : a(a), area(a, width * height) {}");
  5181. verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
  5182. verifyFormat("void f() { f(a, c * d); }");
  5183. verifyFormat("void f() { f(new a(), c * d); }");
  5184. verifyIndependentOfContext("InvalidRegions[*R] = 0;");
  5185. verifyIndependentOfContext("A<int *> a;");
  5186. verifyIndependentOfContext("A<int **> a;");
  5187. verifyIndependentOfContext("A<int *, int *> a;");
  5188. verifyIndependentOfContext("A<int *[]> a;");
  5189. verifyIndependentOfContext(
  5190. "const char *const p = reinterpret_cast<const char *const>(q);");
  5191. verifyIndependentOfContext("A<int **, int **> a;");
  5192. verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
  5193. verifyFormat("for (char **a = b; *a; ++a) {\n}");
  5194. verifyFormat("for (; a && b;) {\n}");
  5195. verifyFormat("bool foo = true && [] { return false; }();");
  5196. verifyFormat(
  5197. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5198. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5199. verifyGoogleFormat("**outparam = 1;");
  5200. verifyGoogleFormat("*outparam = a * b;");
  5201. verifyGoogleFormat("int main(int argc, char** argv) {}");
  5202. verifyGoogleFormat("A<int*> a;");
  5203. verifyGoogleFormat("A<int**> a;");
  5204. verifyGoogleFormat("A<int*, int*> a;");
  5205. verifyGoogleFormat("A<int**, int**> a;");
  5206. verifyGoogleFormat("f(b ? *c : *d);");
  5207. verifyGoogleFormat("int a = b ? *c : *d;");
  5208. verifyGoogleFormat("Type* t = **x;");
  5209. verifyGoogleFormat("Type* t = *++*x;");
  5210. verifyGoogleFormat("*++*x;");
  5211. verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
  5212. verifyGoogleFormat("Type* t = x++ * y;");
  5213. verifyGoogleFormat(
  5214. "const char* const p = reinterpret_cast<const char* const>(q);");
  5215. verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
  5216. verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
  5217. verifyGoogleFormat("template <typename T>\n"
  5218. "void f(int i = 0, SomeType** temps = NULL);");
  5219. FormatStyle Left = getLLVMStyle();
  5220. Left.PointerAlignment = FormatStyle::PAS_Left;
  5221. verifyFormat("x = *a(x) = *a(y);", Left);
  5222. verifyFormat("for (;; * = b) {\n}", Left);
  5223. verifyFormat("return *this += 1;", Left);
  5224. verifyIndependentOfContext("a = *(x + y);");
  5225. verifyIndependentOfContext("a = &(x + y);");
  5226. verifyIndependentOfContext("*(x + y).call();");
  5227. verifyIndependentOfContext("&(x + y)->call();");
  5228. verifyFormat("void f() { &(*I).first; }");
  5229. verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
  5230. verifyFormat(
  5231. "int *MyValues = {\n"
  5232. " *A, // Operator detection might be confused by the '{'\n"
  5233. " *BB // Operator detection might be confused by previous comment\n"
  5234. "};");
  5235. verifyIndependentOfContext("if (int *a = &b)");
  5236. verifyIndependentOfContext("if (int &a = *b)");
  5237. verifyIndependentOfContext("if (a & b[i])");
  5238. verifyIndependentOfContext("if (a::b::c::d & b[i])");
  5239. verifyIndependentOfContext("if (*b[i])");
  5240. verifyIndependentOfContext("if (int *a = (&b))");
  5241. verifyIndependentOfContext("while (int *a = &b)");
  5242. verifyIndependentOfContext("size = sizeof *a;");
  5243. verifyIndependentOfContext("if (a && (b = c))");
  5244. verifyFormat("void f() {\n"
  5245. " for (const int &v : Values) {\n"
  5246. " }\n"
  5247. "}");
  5248. verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
  5249. verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
  5250. verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
  5251. verifyFormat("#define A (!a * b)");
  5252. verifyFormat("#define MACRO \\\n"
  5253. " int *i = a * b; \\\n"
  5254. " void f(a *b);",
  5255. getLLVMStyleWithColumns(19));
  5256. verifyIndependentOfContext("A = new SomeType *[Length];");
  5257. verifyIndependentOfContext("A = new SomeType *[Length]();");
  5258. verifyIndependentOfContext("T **t = new T *;");
  5259. verifyIndependentOfContext("T **t = new T *();");
  5260. verifyGoogleFormat("A = new SomeType*[Length]();");
  5261. verifyGoogleFormat("A = new SomeType*[Length];");
  5262. verifyGoogleFormat("T** t = new T*;");
  5263. verifyGoogleFormat("T** t = new T*();");
  5264. FormatStyle PointerLeft = getLLVMStyle();
  5265. PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
  5266. verifyFormat("delete *x;", PointerLeft);
  5267. verifyFormat("STATIC_ASSERT((a & b) == 0);");
  5268. verifyFormat("STATIC_ASSERT(0 == (a & b));");
  5269. verifyFormat("template <bool a, bool b> "
  5270. "typename t::if<x && y>::type f() {}");
  5271. verifyFormat("template <int *y> f() {}");
  5272. verifyFormat("vector<int *> v;");
  5273. verifyFormat("vector<int *const> v;");
  5274. verifyFormat("vector<int *const **const *> v;");
  5275. verifyFormat("vector<int *volatile> v;");
  5276. verifyFormat("vector<a * b> v;");
  5277. verifyFormat("foo<b && false>();");
  5278. verifyFormat("foo<b & 1>();");
  5279. verifyFormat("decltype(*::std::declval<const T &>()) void F();");
  5280. verifyFormat(
  5281. "template <class T, class = typename std::enable_if<\n"
  5282. " std::is_integral<T>::value &&\n"
  5283. " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
  5284. "void F();",
  5285. getLLVMStyleWithColumns(76));
  5286. verifyFormat(
  5287. "template <class T,\n"
  5288. " class = typename ::std::enable_if<\n"
  5289. " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
  5290. "void F();",
  5291. getGoogleStyleWithColumns(68));
  5292. verifyIndependentOfContext("MACRO(int *i);");
  5293. verifyIndependentOfContext("MACRO(auto *a);");
  5294. verifyIndependentOfContext("MACRO(const A *a);");
  5295. verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
  5296. // FIXME: Is there a way to make this work?
  5297. // verifyIndependentOfContext("MACRO(A *a);");
  5298. verifyFormat("DatumHandle const *operator->() const { return input_; }");
  5299. verifyFormat("return options != nullptr && operator==(*options);");
  5300. EXPECT_EQ("#define OP(x) \\\n"
  5301. " ostream &operator<<(ostream &s, const A &a) { \\\n"
  5302. " return s << a.DebugString(); \\\n"
  5303. " }",
  5304. format("#define OP(x) \\\n"
  5305. " ostream &operator<<(ostream &s, const A &a) { \\\n"
  5306. " return s << a.DebugString(); \\\n"
  5307. " }",
  5308. getLLVMStyleWithColumns(50)));
  5309. // FIXME: We cannot handle this case yet; we might be able to figure out that
  5310. // foo<x> d > v; doesn't make sense.
  5311. verifyFormat("foo<a<b && c> d> v;");
  5312. FormatStyle PointerMiddle = getLLVMStyle();
  5313. PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
  5314. verifyFormat("delete *x;", PointerMiddle);
  5315. verifyFormat("int * x;", PointerMiddle);
  5316. verifyFormat("template <int * y> f() {}", PointerMiddle);
  5317. verifyFormat("int * f(int * a) {}", PointerMiddle);
  5318. verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
  5319. verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
  5320. verifyFormat("A<int *> a;", PointerMiddle);
  5321. verifyFormat("A<int **> a;", PointerMiddle);
  5322. verifyFormat("A<int *, int *> a;", PointerMiddle);
  5323. verifyFormat("A<int * []> a;", PointerMiddle);
  5324. verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
  5325. verifyFormat("A = new SomeType *[Length];", PointerMiddle);
  5326. verifyFormat("T ** t = new T *;", PointerMiddle);
  5327. // Member function reference qualifiers aren't binary operators.
  5328. verifyFormat("string // break\n"
  5329. "operator()() & {}");
  5330. verifyFormat("string // break\n"
  5331. "operator()() && {}");
  5332. verifyGoogleFormat("template <typename T>\n"
  5333. "auto x() & -> int {}");
  5334. }
  5335. TEST_F(FormatTest, UnderstandsAttributes) {
  5336. verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
  5337. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
  5338. "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
  5339. FormatStyle AfterType = getLLVMStyle();
  5340. AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
  5341. verifyFormat("__attribute__((nodebug)) void\n"
  5342. "foo() {}\n",
  5343. AfterType);
  5344. }
  5345. TEST_F(FormatTest, UnderstandsEllipsis) {
  5346. verifyFormat("int printf(const char *fmt, ...);");
  5347. verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
  5348. verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
  5349. FormatStyle PointersLeft = getLLVMStyle();
  5350. PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
  5351. verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
  5352. }
  5353. TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
  5354. EXPECT_EQ("int *a;\n"
  5355. "int *a;\n"
  5356. "int *a;",
  5357. format("int *a;\n"
  5358. "int* a;\n"
  5359. "int *a;",
  5360. getGoogleStyle()));
  5361. EXPECT_EQ("int* a;\n"
  5362. "int* a;\n"
  5363. "int* a;",
  5364. format("int* a;\n"
  5365. "int* a;\n"
  5366. "int *a;",
  5367. getGoogleStyle()));
  5368. EXPECT_EQ("int *a;\n"
  5369. "int *a;\n"
  5370. "int *a;",
  5371. format("int *a;\n"
  5372. "int * a;\n"
  5373. "int * a;",
  5374. getGoogleStyle()));
  5375. EXPECT_EQ("auto x = [] {\n"
  5376. " int *a;\n"
  5377. " int *a;\n"
  5378. " int *a;\n"
  5379. "};",
  5380. format("auto x=[]{int *a;\n"
  5381. "int * a;\n"
  5382. "int * a;};",
  5383. getGoogleStyle()));
  5384. }
  5385. TEST_F(FormatTest, UnderstandsRvalueReferences) {
  5386. verifyFormat("int f(int &&a) {}");
  5387. verifyFormat("int f(int a, char &&b) {}");
  5388. verifyFormat("void f() { int &&a = b; }");
  5389. verifyGoogleFormat("int f(int a, char&& b) {}");
  5390. verifyGoogleFormat("void f() { int&& a = b; }");
  5391. verifyIndependentOfContext("A<int &&> a;");
  5392. verifyIndependentOfContext("A<int &&, int &&> a;");
  5393. verifyGoogleFormat("A<int&&> a;");
  5394. verifyGoogleFormat("A<int&&, int&&> a;");
  5395. // Not rvalue references:
  5396. verifyFormat("template <bool B, bool C> class A {\n"
  5397. " static_assert(B && C, \"Something is wrong\");\n"
  5398. "};");
  5399. verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
  5400. verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
  5401. verifyFormat("#define A(a, b) (a && b)");
  5402. }
  5403. TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
  5404. verifyFormat("void f() {\n"
  5405. " x[aaaaaaaaa -\n"
  5406. " b] = 23;\n"
  5407. "}",
  5408. getLLVMStyleWithColumns(15));
  5409. }
  5410. TEST_F(FormatTest, FormatsCasts) {
  5411. verifyFormat("Type *A = static_cast<Type *>(P);");
  5412. verifyFormat("Type *A = (Type *)P;");
  5413. verifyFormat("Type *A = (vector<Type *, int *>)P;");
  5414. verifyFormat("int a = (int)(2.0f);");
  5415. verifyFormat("int a = (int)2.0f;");
  5416. verifyFormat("x[(int32)y];");
  5417. verifyFormat("x = (int32)y;");
  5418. verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
  5419. verifyFormat("int a = (int)*b;");
  5420. verifyFormat("int a = (int)2.0f;");
  5421. verifyFormat("int a = (int)~0;");
  5422. verifyFormat("int a = (int)++a;");
  5423. verifyFormat("int a = (int)sizeof(int);");
  5424. verifyFormat("int a = (int)+2;");
  5425. verifyFormat("my_int a = (my_int)2.0f;");
  5426. verifyFormat("my_int a = (my_int)sizeof(int);");
  5427. verifyFormat("return (my_int)aaa;");
  5428. verifyFormat("#define x ((int)-1)");
  5429. verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
  5430. verifyFormat("#define p(q) ((int *)&q)");
  5431. verifyFormat("fn(a)(b) + 1;");
  5432. verifyFormat("void f() { my_int a = (my_int)*b; }");
  5433. verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
  5434. verifyFormat("my_int a = (my_int)~0;");
  5435. verifyFormat("my_int a = (my_int)++a;");
  5436. verifyFormat("my_int a = (my_int)-2;");
  5437. verifyFormat("my_int a = (my_int)1;");
  5438. verifyFormat("my_int a = (my_int *)1;");
  5439. verifyFormat("my_int a = (const my_int)-1;");
  5440. verifyFormat("my_int a = (const my_int *)-1;");
  5441. verifyFormat("my_int a = (my_int)(my_int)-1;");
  5442. verifyFormat("my_int a = (ns::my_int)-2;");
  5443. verifyFormat("case (my_int)ONE:");
  5444. // FIXME: single value wrapped with paren will be treated as cast.
  5445. verifyFormat("void f(int i = (kValue)*kMask) {}");
  5446. verifyFormat("{ (void)F; }");
  5447. // Don't break after a cast's
  5448. verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
  5449. " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
  5450. " bbbbbbbbbbbbbbbbbbbbbb);");
  5451. // These are not casts.
  5452. verifyFormat("void f(int *) {}");
  5453. verifyFormat("f(foo)->b;");
  5454. verifyFormat("f(foo).b;");
  5455. verifyFormat("f(foo)(b);");
  5456. verifyFormat("f(foo)[b];");
  5457. verifyFormat("[](foo) { return 4; }(bar);");
  5458. verifyFormat("(*funptr)(foo)[4];");
  5459. verifyFormat("funptrs[4](foo)[4];");
  5460. verifyFormat("void f(int *);");
  5461. verifyFormat("void f(int *) = 0;");
  5462. verifyFormat("void f(SmallVector<int>) {}");
  5463. verifyFormat("void f(SmallVector<int>);");
  5464. verifyFormat("void f(SmallVector<int>) = 0;");
  5465. verifyFormat("void f(int i = (kA * kB) & kMask) {}");
  5466. verifyFormat("int a = sizeof(int) * b;");
  5467. verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
  5468. verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
  5469. verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
  5470. verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
  5471. // These are not casts, but at some point were confused with casts.
  5472. verifyFormat("virtual void foo(int *) override;");
  5473. verifyFormat("virtual void foo(char &) const;");
  5474. verifyFormat("virtual void foo(int *a, char *) const;");
  5475. verifyFormat("int a = sizeof(int *) + b;");
  5476. verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
  5477. verifyFormat("bool b = f(g<int>) && c;");
  5478. verifyFormat("typedef void (*f)(int i) func;");
  5479. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
  5480. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
  5481. // FIXME: The indentation here is not ideal.
  5482. verifyFormat(
  5483. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5484. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
  5485. " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
  5486. }
  5487. TEST_F(FormatTest, FormatsFunctionTypes) {
  5488. verifyFormat("A<bool()> a;");
  5489. verifyFormat("A<SomeType()> a;");
  5490. verifyFormat("A<void (*)(int, std::string)> a;");
  5491. verifyFormat("A<void *(int)>;");
  5492. verifyFormat("void *(*a)(int *, SomeType *);");
  5493. verifyFormat("int (*func)(void *);");
  5494. verifyFormat("void f() { int (*func)(void *); }");
  5495. verifyFormat("template <class CallbackClass>\n"
  5496. "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
  5497. verifyGoogleFormat("A<void*(int*, SomeType*)>;");
  5498. verifyGoogleFormat("void* (*a)(int);");
  5499. verifyGoogleFormat(
  5500. "template <class CallbackClass>\n"
  5501. "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
  5502. // Other constructs can look somewhat like function types:
  5503. verifyFormat("A<sizeof(*x)> a;");
  5504. verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
  5505. verifyFormat("some_var = function(*some_pointer_var)[0];");
  5506. verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
  5507. verifyFormat("int x = f(&h)();");
  5508. }
  5509. TEST_F(FormatTest, FormatsPointersToArrayTypes) {
  5510. verifyFormat("A (*foo_)[6];");
  5511. verifyFormat("vector<int> (*foo_)[6];");
  5512. }
  5513. TEST_F(FormatTest, BreaksLongVariableDeclarations) {
  5514. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5515. " LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5516. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
  5517. " LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5518. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5519. " *LoooooooooooooooooooooooooooooooooooooooongVariable;");
  5520. // Different ways of ()-initializiation.
  5521. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5522. " LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
  5523. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5524. " LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
  5525. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5526. " LoooooooooooooooooooooooooooooooooooooooongVariable({});");
  5527. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
  5528. " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
  5529. }
  5530. TEST_F(FormatTest, BreaksLongDeclarations) {
  5531. verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
  5532. " AnotherNameForTheLongType;");
  5533. verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
  5534. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  5535. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5536. "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
  5537. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
  5538. "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
  5539. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5540. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5541. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
  5542. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5543. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  5544. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5545. verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
  5546. "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
  5547. FormatStyle Indented = getLLVMStyle();
  5548. Indented.IndentWrappedFunctionNames = true;
  5549. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5550. " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
  5551. Indented);
  5552. verifyFormat(
  5553. "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
  5554. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5555. Indented);
  5556. verifyFormat(
  5557. "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
  5558. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5559. Indented);
  5560. verifyFormat(
  5561. "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
  5562. " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
  5563. Indented);
  5564. // FIXME: Without the comment, this breaks after "(".
  5565. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
  5566. " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
  5567. getGoogleStyle());
  5568. verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
  5569. " int LoooooooooooooooooooongParam2) {}");
  5570. verifyFormat(
  5571. "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
  5572. " SourceLocation L, IdentifierIn *II,\n"
  5573. " Type *T) {}");
  5574. verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
  5575. "ReallyReaaallyLongFunctionName(\n"
  5576. " const std::string &SomeParameter,\n"
  5577. " const SomeType<string, SomeOtherTemplateParameter>\n"
  5578. " &ReallyReallyLongParameterName,\n"
  5579. " const SomeType<string, SomeOtherTemplateParameter>\n"
  5580. " &AnotherLongParameterName) {}");
  5581. verifyFormat("template <typename A>\n"
  5582. "SomeLoooooooooooooooooooooongType<\n"
  5583. " typename some_namespace::SomeOtherType<A>::Type>\n"
  5584. "Function() {}");
  5585. verifyGoogleFormat(
  5586. "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
  5587. " aaaaaaaaaaaaaaaaaaaaaaa;");
  5588. verifyGoogleFormat(
  5589. "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
  5590. " SourceLocation L) {}");
  5591. verifyGoogleFormat(
  5592. "some_namespace::LongReturnType\n"
  5593. "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
  5594. " int first_long_parameter, int second_parameter) {}");
  5595. verifyGoogleFormat("template <typename T>\n"
  5596. "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  5597. "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
  5598. verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5599. " int aaaaaaaaaaaaaaaaaaaaaaa);");
  5600. verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
  5601. " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5602. " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5603. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5604. " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
  5605. " aaaaaaaaaaaaaaaaaaaaaaaa);");
  5606. verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  5607. " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
  5608. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
  5609. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5610. }
  5611. TEST_F(FormatTest, FormatsArrays) {
  5612. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5613. " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
  5614. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
  5615. " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
  5616. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
  5617. " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
  5618. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5619. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  5620. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5621. " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
  5622. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  5623. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5624. " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
  5625. verifyFormat(
  5626. "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
  5627. " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
  5628. " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  5629. verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
  5630. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
  5631. verifyFormat(
  5632. "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
  5633. " .aaaaaaa[0]\n"
  5634. " .aaaaaaaaaaaaaaaaaaaaaa();");
  5635. verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
  5636. }
  5637. TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
  5638. verifyFormat("(a)->b();");
  5639. verifyFormat("--a;");
  5640. }
  5641. TEST_F(FormatTest, HandlesIncludeDirectives) {
  5642. verifyFormat("#include <string>\n"
  5643. "#include <a/b/c.h>\n"
  5644. "#include \"a/b/string\"\n"
  5645. "#include \"string.h\"\n"
  5646. "#include \"string.h\"\n"
  5647. "#include <a-a>\n"
  5648. "#include < path with space >\n"
  5649. "#include_next <test.h>"
  5650. "#include \"abc.h\" // this is included for ABC\n"
  5651. "#include \"some long include\" // with a comment\n"
  5652. "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
  5653. getLLVMStyleWithColumns(35));
  5654. EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\""));
  5655. EXPECT_EQ("#include <a>", format("#include<a>"));
  5656. verifyFormat("#import <string>");
  5657. verifyFormat("#import <a/b/c.h>");
  5658. verifyFormat("#import \"a/b/string\"");
  5659. verifyFormat("#import \"string.h\"");
  5660. verifyFormat("#import \"string.h\"");
  5661. verifyFormat("#if __has_include(<strstream>)\n"
  5662. "#include <strstream>\n"
  5663. "#endif");
  5664. verifyFormat("#define MY_IMPORT <a/b>");
  5665. // Protocol buffer definition or missing "#".
  5666. verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
  5667. getLLVMStyleWithColumns(30));
  5668. FormatStyle Style = getLLVMStyle();
  5669. Style.AlwaysBreakBeforeMultilineStrings = true;
  5670. Style.ColumnLimit = 0;
  5671. verifyFormat("#import \"abc.h\"", Style);
  5672. // But 'import' might also be a regular C++ namespace.
  5673. verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5674. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  5675. }
  5676. //===----------------------------------------------------------------------===//
  5677. // Error recovery tests.
  5678. //===----------------------------------------------------------------------===//
  5679. TEST_F(FormatTest, IncompleteParameterLists) {
  5680. FormatStyle NoBinPacking = getLLVMStyle();
  5681. NoBinPacking.BinPackParameters = false;
  5682. verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
  5683. " double *min_x,\n"
  5684. " double *max_x,\n"
  5685. " double *min_y,\n"
  5686. " double *max_y,\n"
  5687. " double *min_z,\n"
  5688. " double *max_z, ) {}",
  5689. NoBinPacking);
  5690. }
  5691. TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
  5692. verifyFormat("void f() { return; }\n42");
  5693. verifyFormat("void f() {\n"
  5694. " if (0)\n"
  5695. " return;\n"
  5696. "}\n"
  5697. "42");
  5698. verifyFormat("void f() { return }\n42");
  5699. verifyFormat("void f() {\n"
  5700. " if (0)\n"
  5701. " return\n"
  5702. "}\n"
  5703. "42");
  5704. }
  5705. TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
  5706. EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
  5707. EXPECT_EQ("void f() {\n"
  5708. " if (a)\n"
  5709. " return\n"
  5710. "}",
  5711. format("void f ( ) { if ( a ) return }"));
  5712. EXPECT_EQ("namespace N {\n"
  5713. "void f()\n"
  5714. "}",
  5715. format("namespace N { void f() }"));
  5716. EXPECT_EQ("namespace N {\n"
  5717. "void f() {}\n"
  5718. "void g()\n"
  5719. "}",
  5720. format("namespace N { void f( ) { } void g( ) }"));
  5721. }
  5722. TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
  5723. verifyFormat("int aaaaaaaa =\n"
  5724. " // Overlylongcomment\n"
  5725. " b;",
  5726. getLLVMStyleWithColumns(20));
  5727. verifyFormat("function(\n"
  5728. " ShortArgument,\n"
  5729. " LoooooooooooongArgument);\n",
  5730. getLLVMStyleWithColumns(20));
  5731. }
  5732. TEST_F(FormatTest, IncorrectAccessSpecifier) {
  5733. verifyFormat("public:");
  5734. verifyFormat("class A {\n"
  5735. "public\n"
  5736. " void f() {}\n"
  5737. "};");
  5738. verifyFormat("public\n"
  5739. "int qwerty;");
  5740. verifyFormat("public\n"
  5741. "B {}");
  5742. verifyFormat("public\n"
  5743. "{}");
  5744. verifyFormat("public\n"
  5745. "B { int x; }");
  5746. }
  5747. TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
  5748. verifyFormat("{");
  5749. verifyFormat("#})");
  5750. verifyNoCrash("(/**/[:!] ?[).");
  5751. }
  5752. TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
  5753. verifyFormat("do {\n}");
  5754. verifyFormat("do {\n}\n"
  5755. "f();");
  5756. verifyFormat("do {\n}\n"
  5757. "wheeee(fun);");
  5758. verifyFormat("do {\n"
  5759. " f();\n"
  5760. "}");
  5761. }
  5762. TEST_F(FormatTest, IncorrectCodeMissingParens) {
  5763. verifyFormat("if {\n foo;\n foo();\n}");
  5764. verifyFormat("switch {\n foo;\n foo();\n}");
  5765. verifyIncompleteFormat("for {\n foo;\n foo();\n}");
  5766. verifyFormat("while {\n foo;\n foo();\n}");
  5767. verifyFormat("do {\n foo;\n foo();\n} while;");
  5768. }
  5769. TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
  5770. verifyIncompleteFormat("namespace {\n"
  5771. "class Foo { Foo (\n"
  5772. "};\n"
  5773. "} // comment");
  5774. }
  5775. TEST_F(FormatTest, IncorrectCodeErrorDetection) {
  5776. EXPECT_EQ("{\n {}\n", format("{\n{\n}\n"));
  5777. EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
  5778. EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
  5779. EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n"));
  5780. EXPECT_EQ("{\n"
  5781. " {\n"
  5782. " breakme(\n"
  5783. " qwe);\n"
  5784. " }\n",
  5785. format("{\n"
  5786. " {\n"
  5787. " breakme(qwe);\n"
  5788. "}\n",
  5789. getLLVMStyleWithColumns(10)));
  5790. }
  5791. TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
  5792. verifyFormat("int x = {\n"
  5793. " avariable,\n"
  5794. " b(alongervariable)};",
  5795. getLLVMStyleWithColumns(25));
  5796. }
  5797. TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
  5798. verifyFormat("return (a)(b){1, 2, 3};");
  5799. }
  5800. TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
  5801. verifyFormat("vector<int> x{1, 2, 3, 4};");
  5802. verifyFormat("vector<int> x{\n"
  5803. " 1, 2, 3, 4,\n"
  5804. "};");
  5805. verifyFormat("vector<T> x{{}, {}, {}, {}};");
  5806. verifyFormat("f({1, 2});");
  5807. verifyFormat("auto v = Foo{-1};");
  5808. verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
  5809. verifyFormat("Class::Class : member{1, 2, 3} {}");
  5810. verifyFormat("new vector<int>{1, 2, 3};");
  5811. verifyFormat("new int[3]{1, 2, 3};");
  5812. verifyFormat("new int{1};");
  5813. verifyFormat("return {arg1, arg2};");
  5814. verifyFormat("return {arg1, SomeType{parameter}};");
  5815. verifyFormat("int count = set<int>{f(), g(), h()}.size();");
  5816. verifyFormat("new T{arg1, arg2};");
  5817. verifyFormat("f(MyMap[{composite, key}]);");
  5818. verifyFormat("class Class {\n"
  5819. " T member = {arg1, arg2};\n"
  5820. "};");
  5821. verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
  5822. verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
  5823. verifyFormat("int a = std::is_integral<int>{} + 0;");
  5824. verifyFormat("int foo(int i) { return fo1{}(i); }");
  5825. verifyFormat("int foo(int i) { return fo1{}(i); }");
  5826. verifyFormat("auto i = decltype(x){};");
  5827. verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
  5828. verifyFormat("Node n{1, Node{1000}, //\n"
  5829. " 2};");
  5830. verifyFormat("Aaaa aaaaaaa{\n"
  5831. " {\n"
  5832. " aaaa,\n"
  5833. " },\n"
  5834. "};");
  5835. verifyFormat("class C : public D {\n"
  5836. " SomeClass SC{2};\n"
  5837. "};");
  5838. verifyFormat("class C : public A {\n"
  5839. " class D : public B {\n"
  5840. " void f() { int i{2}; }\n"
  5841. " };\n"
  5842. "};");
  5843. verifyFormat("#define A {a, a},");
  5844. // In combination with BinPackArguments = false.
  5845. FormatStyle NoBinPacking = getLLVMStyle();
  5846. NoBinPacking.BinPackArguments = false;
  5847. verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
  5848. " bbbbb,\n"
  5849. " ccccc,\n"
  5850. " ddddd,\n"
  5851. " eeeee,\n"
  5852. " ffffff,\n"
  5853. " ggggg,\n"
  5854. " hhhhhh,\n"
  5855. " iiiiii,\n"
  5856. " jjjjjj,\n"
  5857. " kkkkkk};",
  5858. NoBinPacking);
  5859. verifyFormat("const Aaaaaa aaaaa = {\n"
  5860. " aaaaa,\n"
  5861. " bbbbb,\n"
  5862. " ccccc,\n"
  5863. " ddddd,\n"
  5864. " eeeee,\n"
  5865. " ffffff,\n"
  5866. " ggggg,\n"
  5867. " hhhhhh,\n"
  5868. " iiiiii,\n"
  5869. " jjjjjj,\n"
  5870. " kkkkkk,\n"
  5871. "};",
  5872. NoBinPacking);
  5873. verifyFormat(
  5874. "const Aaaaaa aaaaa = {\n"
  5875. " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n"
  5876. " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n"
  5877. " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
  5878. "};",
  5879. NoBinPacking);
  5880. // FIXME: The alignment of these trailing comments might be bad. Then again,
  5881. // this might be utterly useless in real code.
  5882. verifyFormat("Constructor::Constructor()\n"
  5883. " : some_value{ //\n"
  5884. " aaaaaaa, //\n"
  5885. " bbbbbbb} {}");
  5886. // In braced lists, the first comment is always assumed to belong to the
  5887. // first element. Thus, it can be moved to the next or previous line as
  5888. // appropriate.
  5889. EXPECT_EQ("function({// First element:\n"
  5890. " 1,\n"
  5891. " // Second element:\n"
  5892. " 2});",
  5893. format("function({\n"
  5894. " // First element:\n"
  5895. " 1,\n"
  5896. " // Second element:\n"
  5897. " 2});"));
  5898. EXPECT_EQ("std::vector<int> MyNumbers{\n"
  5899. " // First element:\n"
  5900. " 1,\n"
  5901. " // Second element:\n"
  5902. " 2};",
  5903. format("std::vector<int> MyNumbers{// First element:\n"
  5904. " 1,\n"
  5905. " // Second element:\n"
  5906. " 2};",
  5907. getLLVMStyleWithColumns(30)));
  5908. // A trailing comma should still lead to an enforced line break.
  5909. EXPECT_EQ("vector<int> SomeVector = {\n"
  5910. " // aaa\n"
  5911. " 1, 2,\n"
  5912. "};",
  5913. format("vector<int> SomeVector = { // aaa\n"
  5914. " 1, 2, };"));
  5915. FormatStyle ExtraSpaces = getLLVMStyle();
  5916. ExtraSpaces.Cpp11BracedListStyle = false;
  5917. ExtraSpaces.ColumnLimit = 75;
  5918. verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
  5919. verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
  5920. verifyFormat("f({ 1, 2 });", ExtraSpaces);
  5921. verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
  5922. verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
  5923. verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
  5924. verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
  5925. verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
  5926. verifyFormat("return { arg1, arg2 };", ExtraSpaces);
  5927. verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
  5928. verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
  5929. verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
  5930. verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
  5931. verifyFormat("class Class {\n"
  5932. " T member = { arg1, arg2 };\n"
  5933. "};",
  5934. ExtraSpaces);
  5935. verifyFormat(
  5936. "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  5937. " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
  5938. " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  5939. " bbbbbbbbbbbbbbbbbbbb, bbbbb };",
  5940. ExtraSpaces);
  5941. verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
  5942. verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
  5943. ExtraSpaces);
  5944. verifyFormat(
  5945. "someFunction(OtherParam,\n"
  5946. " BracedList{ // comment 1 (Forcing interesting break)\n"
  5947. " param1, param2,\n"
  5948. " // comment 2\n"
  5949. " param3, param4 });",
  5950. ExtraSpaces);
  5951. verifyFormat(
  5952. "std::this_thread::sleep_for(\n"
  5953. " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
  5954. ExtraSpaces);
  5955. verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
  5956. " aaaaaaa,\n"
  5957. " aaaaaaaaaa,\n"
  5958. " aaaaa,\n"
  5959. " aaaaaaaaaaaaaaa,\n"
  5960. " aaa,\n"
  5961. " aaaaaaaaaa,\n"
  5962. " a,\n"
  5963. " aaaaaaaaaaaaaaaaaaaaa,\n"
  5964. " aaaaaaaaaaaa,\n"
  5965. " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
  5966. " aaaaaaa,\n"
  5967. " a};");
  5968. verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
  5969. }
  5970. TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
  5971. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5972. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5973. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5974. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5975. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5976. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  5977. verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
  5978. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5979. " 1, 22, 333, 4444, 55555, //\n"
  5980. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5981. " 1, 22, 333, 4444, 55555, 666666, 7777777};");
  5982. verifyFormat(
  5983. "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5984. " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
  5985. " 1, 22, 333, 4444, 55555, 666666, // comment\n"
  5986. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  5987. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  5988. " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
  5989. " 7777777};");
  5990. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  5991. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  5992. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  5993. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  5994. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  5995. " // Separating comment.\n"
  5996. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  5997. verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
  5998. " // Leading comment\n"
  5999. " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
  6000. " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
  6001. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  6002. " 1, 1, 1, 1};",
  6003. getLLVMStyleWithColumns(39));
  6004. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  6005. " 1, 1, 1, 1};",
  6006. getLLVMStyleWithColumns(38));
  6007. verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
  6008. " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
  6009. getLLVMStyleWithColumns(43));
  6010. verifyFormat(
  6011. "static unsigned SomeValues[10][3] = {\n"
  6012. " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n"
  6013. " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
  6014. verifyFormat("static auto fields = new vector<string>{\n"
  6015. " \"aaaaaaaaaaaaa\",\n"
  6016. " \"aaaaaaaaaaaaa\",\n"
  6017. " \"aaaaaaaaaaaa\",\n"
  6018. " \"aaaaaaaaaaaaaa\",\n"
  6019. " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
  6020. " \"aaaaaaaaaaaa\",\n"
  6021. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
  6022. "};");
  6023. verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
  6024. verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
  6025. " 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
  6026. " 3, cccccccccccccccccccccc};",
  6027. getLLVMStyleWithColumns(60));
  6028. // Trailing commas.
  6029. verifyFormat("vector<int> x = {\n"
  6030. " 1, 1, 1, 1, 1, 1, 1, 1,\n"
  6031. "};",
  6032. getLLVMStyleWithColumns(39));
  6033. verifyFormat("vector<int> x = {\n"
  6034. " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
  6035. "};",
  6036. getLLVMStyleWithColumns(39));
  6037. verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
  6038. " 1, 1, 1, 1,\n"
  6039. " /**/ /**/};",
  6040. getLLVMStyleWithColumns(39));
  6041. // Trailing comment in the first line.
  6042. verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n"
  6043. " 1111111111, 2222222222, 33333333333, 4444444444, //\n"
  6044. " 111111111, 222222222, 3333333333, 444444444, //\n"
  6045. " 11111111, 22222222, 333333333, 44444444};");
  6046. // Trailing comment in the last line.
  6047. verifyFormat("int aaaaa[] = {\n"
  6048. " 1, 2, 3, // comment\n"
  6049. " 4, 5, 6 // comment\n"
  6050. "};");
  6051. // With nested lists, we should either format one item per line or all nested
  6052. // lists one on line.
  6053. // FIXME: For some nested lists, we can do better.
  6054. verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
  6055. " {aaaaaaaaaaaaaaaaaaa},\n"
  6056. " {aaaaaaaaaaaaaaaaaaaaa},\n"
  6057. " {aaaaaaaaaaaaaaaaa}};",
  6058. getLLVMStyleWithColumns(60));
  6059. verifyFormat(
  6060. "SomeStruct my_struct_array = {\n"
  6061. " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
  6062. " aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
  6063. " {aaa, aaa},\n"
  6064. " {aaa, aaa},\n"
  6065. " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
  6066. " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6067. " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
  6068. // No column layout should be used here.
  6069. verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
  6070. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
  6071. verifyNoCrash("a<,");
  6072. // No braced initializer here.
  6073. verifyFormat("void f() {\n"
  6074. " struct Dummy {};\n"
  6075. " f(v);\n"
  6076. "}");
  6077. }
  6078. TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
  6079. FormatStyle DoNotMerge = getLLVMStyle();
  6080. DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  6081. verifyFormat("void f() { return 42; }");
  6082. verifyFormat("void f() {\n"
  6083. " return 42;\n"
  6084. "}",
  6085. DoNotMerge);
  6086. verifyFormat("void f() {\n"
  6087. " // Comment\n"
  6088. "}");
  6089. verifyFormat("{\n"
  6090. "#error {\n"
  6091. " int a;\n"
  6092. "}");
  6093. verifyFormat("{\n"
  6094. " int a;\n"
  6095. "#error {\n"
  6096. "}");
  6097. verifyFormat("void f() {} // comment");
  6098. verifyFormat("void f() { int a; } // comment");
  6099. verifyFormat("void f() {\n"
  6100. "} // comment",
  6101. DoNotMerge);
  6102. verifyFormat("void f() {\n"
  6103. " int a;\n"
  6104. "} // comment",
  6105. DoNotMerge);
  6106. verifyFormat("void f() {\n"
  6107. "} // comment",
  6108. getLLVMStyleWithColumns(15));
  6109. verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
  6110. verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
  6111. verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
  6112. verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
  6113. verifyFormat("class C {\n"
  6114. " C()\n"
  6115. " : iiiiiiii(nullptr),\n"
  6116. " kkkkkkk(nullptr),\n"
  6117. " mmmmmmm(nullptr),\n"
  6118. " nnnnnnn(nullptr) {}\n"
  6119. "};",
  6120. getGoogleStyle());
  6121. FormatStyle NoColumnLimit = getLLVMStyle();
  6122. NoColumnLimit.ColumnLimit = 0;
  6123. EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
  6124. EXPECT_EQ("class C {\n"
  6125. " A() : b(0) {}\n"
  6126. "};",
  6127. format("class C{A():b(0){}};", NoColumnLimit));
  6128. EXPECT_EQ("A()\n"
  6129. " : b(0) {\n"
  6130. "}",
  6131. format("A()\n:b(0)\n{\n}", NoColumnLimit));
  6132. FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
  6133. DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
  6134. FormatStyle::SFS_None;
  6135. EXPECT_EQ("A()\n"
  6136. " : b(0) {\n"
  6137. "}",
  6138. format("A():b(0){}", DoNotMergeNoColumnLimit));
  6139. EXPECT_EQ("A()\n"
  6140. " : b(0) {\n"
  6141. "}",
  6142. format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
  6143. verifyFormat("#define A \\\n"
  6144. " void f() { \\\n"
  6145. " int i; \\\n"
  6146. " }",
  6147. getLLVMStyleWithColumns(20));
  6148. verifyFormat("#define A \\\n"
  6149. " void f() { int i; }",
  6150. getLLVMStyleWithColumns(21));
  6151. verifyFormat("#define A \\\n"
  6152. " void f() { \\\n"
  6153. " int i; \\\n"
  6154. " } \\\n"
  6155. " int j;",
  6156. getLLVMStyleWithColumns(22));
  6157. verifyFormat("#define A \\\n"
  6158. " void f() { int i; } \\\n"
  6159. " int j;",
  6160. getLLVMStyleWithColumns(23));
  6161. }
  6162. TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
  6163. FormatStyle MergeInlineOnly = getLLVMStyle();
  6164. MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  6165. verifyFormat("class C {\n"
  6166. " int f() { return 42; }\n"
  6167. "};",
  6168. MergeInlineOnly);
  6169. verifyFormat("int f() {\n"
  6170. " return 42;\n"
  6171. "}",
  6172. MergeInlineOnly);
  6173. }
  6174. TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
  6175. // Elaborate type variable declarations.
  6176. verifyFormat("struct foo a = {bar};\nint n;");
  6177. verifyFormat("class foo a = {bar};\nint n;");
  6178. verifyFormat("union foo a = {bar};\nint n;");
  6179. // Elaborate types inside function definitions.
  6180. verifyFormat("struct foo f() {}\nint n;");
  6181. verifyFormat("class foo f() {}\nint n;");
  6182. verifyFormat("union foo f() {}\nint n;");
  6183. // Templates.
  6184. verifyFormat("template <class X> void f() {}\nint n;");
  6185. verifyFormat("template <struct X> void f() {}\nint n;");
  6186. verifyFormat("template <union X> void f() {}\nint n;");
  6187. // Actual definitions...
  6188. verifyFormat("struct {\n} n;");
  6189. verifyFormat(
  6190. "template <template <class T, class Y>, class Z> class X {\n} n;");
  6191. verifyFormat("union Z {\n int n;\n} x;");
  6192. verifyFormat("class MACRO Z {\n} n;");
  6193. verifyFormat("class MACRO(X) Z {\n} n;");
  6194. verifyFormat("class __attribute__(X) Z {\n} n;");
  6195. verifyFormat("class __declspec(X) Z {\n} n;");
  6196. verifyFormat("class A##B##C {\n} n;");
  6197. verifyFormat("class alignas(16) Z {\n} n;");
  6198. verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
  6199. verifyFormat("class MACROA MACRO(X) Z {\n} n;");
  6200. // Redefinition from nested context:
  6201. verifyFormat("class A::B::C {\n} n;");
  6202. // Template definitions.
  6203. verifyFormat(
  6204. "template <typename F>\n"
  6205. "Matcher(const Matcher<F> &Other,\n"
  6206. " typename enable_if_c<is_base_of<F, T>::value &&\n"
  6207. " !is_same<F, T>::value>::type * = 0)\n"
  6208. " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
  6209. // FIXME: This is still incorrectly handled at the formatter side.
  6210. verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
  6211. verifyFormat("int i = SomeFunction(a<b, a> b);");
  6212. // FIXME:
  6213. // This now gets parsed incorrectly as class definition.
  6214. // verifyFormat("class A<int> f() {\n}\nint n;");
  6215. // Elaborate types where incorrectly parsing the structural element would
  6216. // break the indent.
  6217. verifyFormat("if (true)\n"
  6218. " class X x;\n"
  6219. "else\n"
  6220. " f();\n");
  6221. // This is simply incomplete. Formatting is not important, but must not crash.
  6222. verifyFormat("class A:");
  6223. }
  6224. TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
  6225. EXPECT_EQ("#error Leave all white!!!!! space* alone!\n",
  6226. format("#error Leave all white!!!!! space* alone!\n"));
  6227. EXPECT_EQ(
  6228. "#warning Leave all white!!!!! space* alone!\n",
  6229. format("#warning Leave all white!!!!! space* alone!\n"));
  6230. EXPECT_EQ("#error 1", format(" # error 1"));
  6231. EXPECT_EQ("#warning 1", format(" # warning 1"));
  6232. }
  6233. TEST_F(FormatTest, FormatHashIfExpressions) {
  6234. verifyFormat("#if AAAA && BBBB");
  6235. verifyFormat("#if (AAAA && BBBB)");
  6236. verifyFormat("#elif (AAAA && BBBB)");
  6237. // FIXME: Come up with a better indentation for #elif.
  6238. verifyFormat(
  6239. "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
  6240. " defined(BBBBBBBB)\n"
  6241. "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
  6242. " defined(BBBBBBBB)\n"
  6243. "#endif",
  6244. getLLVMStyleWithColumns(65));
  6245. }
  6246. TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
  6247. FormatStyle AllowsMergedIf = getGoogleStyle();
  6248. AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
  6249. verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
  6250. verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
  6251. verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
  6252. EXPECT_EQ("if (true) return 42;",
  6253. format("if (true)\nreturn 42;", AllowsMergedIf));
  6254. FormatStyle ShortMergedIf = AllowsMergedIf;
  6255. ShortMergedIf.ColumnLimit = 25;
  6256. verifyFormat("#define A \\\n"
  6257. " if (true) return 42;",
  6258. ShortMergedIf);
  6259. verifyFormat("#define A \\\n"
  6260. " f(); \\\n"
  6261. " if (true)\n"
  6262. "#define B",
  6263. ShortMergedIf);
  6264. verifyFormat("#define A \\\n"
  6265. " f(); \\\n"
  6266. " if (true)\n"
  6267. "g();",
  6268. ShortMergedIf);
  6269. verifyFormat("{\n"
  6270. "#ifdef A\n"
  6271. " // Comment\n"
  6272. " if (true) continue;\n"
  6273. "#endif\n"
  6274. " // Comment\n"
  6275. " if (true) continue;\n"
  6276. "}",
  6277. ShortMergedIf);
  6278. ShortMergedIf.ColumnLimit = 29;
  6279. verifyFormat("#define A \\\n"
  6280. " if (aaaaaaaaaa) return 1; \\\n"
  6281. " return 2;",
  6282. ShortMergedIf);
  6283. ShortMergedIf.ColumnLimit = 28;
  6284. verifyFormat("#define A \\\n"
  6285. " if (aaaaaaaaaa) \\\n"
  6286. " return 1; \\\n"
  6287. " return 2;",
  6288. ShortMergedIf);
  6289. }
  6290. TEST_F(FormatTest, BlockCommentsInControlLoops) {
  6291. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  6292. " f();\n"
  6293. "}");
  6294. verifyFormat("if (0) /* a comment in a strange place */ {\n"
  6295. " f();\n"
  6296. "} /* another comment */ else /* comment #3 */ {\n"
  6297. " g();\n"
  6298. "}");
  6299. verifyFormat("while (0) /* a comment in a strange place */ {\n"
  6300. " f();\n"
  6301. "}");
  6302. verifyFormat("for (;;) /* a comment in a strange place */ {\n"
  6303. " f();\n"
  6304. "}");
  6305. verifyFormat("do /* a comment in a strange place */ {\n"
  6306. " f();\n"
  6307. "} /* another comment */ while (0);");
  6308. }
  6309. TEST_F(FormatTest, BlockComments) {
  6310. EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
  6311. format("/* *//* */ /* */\n/* *//* */ /* */"));
  6312. EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
  6313. EXPECT_EQ("#define A /*123*/ \\\n"
  6314. " b\n"
  6315. "/* */\n"
  6316. "someCall(\n"
  6317. " parameter);",
  6318. format("#define A /*123*/ b\n"
  6319. "/* */\n"
  6320. "someCall(parameter);",
  6321. getLLVMStyleWithColumns(15)));
  6322. EXPECT_EQ("#define A\n"
  6323. "/* */ someCall(\n"
  6324. " parameter);",
  6325. format("#define A\n"
  6326. "/* */someCall(parameter);",
  6327. getLLVMStyleWithColumns(15)));
  6328. EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
  6329. EXPECT_EQ("/*\n"
  6330. "*\n"
  6331. " * aaaaaa\n"
  6332. " * aaaaaa\n"
  6333. "*/",
  6334. format("/*\n"
  6335. "*\n"
  6336. " * aaaaaa aaaaaa\n"
  6337. "*/",
  6338. getLLVMStyleWithColumns(10)));
  6339. EXPECT_EQ("/*\n"
  6340. "**\n"
  6341. "* aaaaaa\n"
  6342. "*aaaaaa\n"
  6343. "*/",
  6344. format("/*\n"
  6345. "**\n"
  6346. "* aaaaaa aaaaaa\n"
  6347. "*/",
  6348. getLLVMStyleWithColumns(10)));
  6349. FormatStyle NoBinPacking = getLLVMStyle();
  6350. NoBinPacking.BinPackParameters = false;
  6351. EXPECT_EQ("someFunction(1, /* comment 1 */\n"
  6352. " 2, /* comment 2 */\n"
  6353. " 3, /* comment 3 */\n"
  6354. " aaaa,\n"
  6355. " bbbb);",
  6356. format("someFunction (1, /* comment 1 */\n"
  6357. " 2, /* comment 2 */ \n"
  6358. " 3, /* comment 3 */\n"
  6359. "aaaa, bbbb );",
  6360. NoBinPacking));
  6361. verifyFormat(
  6362. "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  6363. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  6364. EXPECT_EQ(
  6365. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  6366. " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  6367. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
  6368. format(
  6369. "bool aaaaaaaaaaaaa = /* trailing comment */\n"
  6370. " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  6371. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
  6372. EXPECT_EQ(
  6373. "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  6374. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  6375. "int cccccccccccccccccccccccccccccc; /* comment */\n",
  6376. format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
  6377. "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
  6378. "int cccccccccccccccccccccccccccccc; /* comment */\n"));
  6379. verifyFormat("void f(int * /* unused */) {}");
  6380. EXPECT_EQ("/*\n"
  6381. " **\n"
  6382. " */",
  6383. format("/*\n"
  6384. " **\n"
  6385. " */"));
  6386. EXPECT_EQ("/*\n"
  6387. " *q\n"
  6388. " */",
  6389. format("/*\n"
  6390. " *q\n"
  6391. " */"));
  6392. EXPECT_EQ("/*\n"
  6393. " * q\n"
  6394. " */",
  6395. format("/*\n"
  6396. " * q\n"
  6397. " */"));
  6398. EXPECT_EQ("/*\n"
  6399. " **/",
  6400. format("/*\n"
  6401. " **/"));
  6402. EXPECT_EQ("/*\n"
  6403. " ***/",
  6404. format("/*\n"
  6405. " ***/"));
  6406. }
  6407. TEST_F(FormatTest, BlockCommentsInMacros) {
  6408. EXPECT_EQ("#define A \\\n"
  6409. " { \\\n"
  6410. " /* one line */ \\\n"
  6411. " someCall();",
  6412. format("#define A { \\\n"
  6413. " /* one line */ \\\n"
  6414. " someCall();",
  6415. getLLVMStyleWithColumns(20)));
  6416. EXPECT_EQ("#define A \\\n"
  6417. " { \\\n"
  6418. " /* previous */ \\\n"
  6419. " /* one line */ \\\n"
  6420. " someCall();",
  6421. format("#define A { \\\n"
  6422. " /* previous */ \\\n"
  6423. " /* one line */ \\\n"
  6424. " someCall();",
  6425. getLLVMStyleWithColumns(20)));
  6426. }
  6427. TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
  6428. EXPECT_EQ("a = {\n"
  6429. " 1111 /* */\n"
  6430. "};",
  6431. format("a = {1111 /* */\n"
  6432. "};",
  6433. getLLVMStyleWithColumns(15)));
  6434. EXPECT_EQ("a = {\n"
  6435. " 1111 /* */\n"
  6436. "};",
  6437. format("a = {1111 /* */\n"
  6438. "};",
  6439. getLLVMStyleWithColumns(15)));
  6440. // FIXME: The formatting is still wrong here.
  6441. EXPECT_EQ("a = {\n"
  6442. " 1111 /* a\n"
  6443. " */\n"
  6444. "};",
  6445. format("a = {1111 /* a */\n"
  6446. "};",
  6447. getLLVMStyleWithColumns(15)));
  6448. }
  6449. TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
  6450. // FIXME: This is not what we want...
  6451. verifyFormat("{\n"
  6452. "// a"
  6453. "// b");
  6454. }
  6455. TEST_F(FormatTest, FormatStarDependingOnContext) {
  6456. verifyFormat("void f(int *a);");
  6457. verifyFormat("void f() { f(fint * b); }");
  6458. verifyFormat("class A {\n void f(int *a);\n};");
  6459. verifyFormat("class A {\n int *a;\n};");
  6460. verifyFormat("namespace a {\n"
  6461. "namespace b {\n"
  6462. "class A {\n"
  6463. " void f() {}\n"
  6464. " int *a;\n"
  6465. "};\n"
  6466. "}\n"
  6467. "}");
  6468. }
  6469. TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
  6470. verifyFormat("while");
  6471. verifyFormat("operator");
  6472. }
  6473. //===----------------------------------------------------------------------===//
  6474. // Objective-C tests.
  6475. //===----------------------------------------------------------------------===//
  6476. TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
  6477. verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
  6478. EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
  6479. format("-(NSUInteger)indexOfObject:(id)anObject;"));
  6480. EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
  6481. EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
  6482. EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
  6483. format("-(NSInteger)Method3:(id)anObject;"));
  6484. EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
  6485. format("-(NSInteger)Method4:(id)anObject;"));
  6486. EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
  6487. format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
  6488. EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
  6489. format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
  6490. EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
  6491. "forAllCells:(BOOL)flag;",
  6492. format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
  6493. "forAllCells:(BOOL)flag;"));
  6494. // Very long objectiveC method declaration.
  6495. verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
  6496. " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
  6497. verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
  6498. " inRange:(NSRange)range\n"
  6499. " outRange:(NSRange)out_range\n"
  6500. " outRange1:(NSRange)out_range1\n"
  6501. " outRange2:(NSRange)out_range2\n"
  6502. " outRange3:(NSRange)out_range3\n"
  6503. " outRange4:(NSRange)out_range4\n"
  6504. " outRange5:(NSRange)out_range5\n"
  6505. " outRange6:(NSRange)out_range6\n"
  6506. " outRange7:(NSRange)out_range7\n"
  6507. " outRange8:(NSRange)out_range8\n"
  6508. " outRange9:(NSRange)out_range9;");
  6509. // When the function name has to be wrapped.
  6510. FormatStyle Style = getLLVMStyle();
  6511. Style.IndentWrappedFunctionNames = false;
  6512. verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
  6513. "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
  6514. " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
  6515. "}",
  6516. Style);
  6517. Style.IndentWrappedFunctionNames = true;
  6518. verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
  6519. " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
  6520. " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
  6521. "}",
  6522. Style);
  6523. verifyFormat("- (int)sum:(vector<int>)numbers;");
  6524. verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
  6525. // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
  6526. // protocol lists (but not for template classes):
  6527. // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
  6528. verifyFormat("- (int (*)())foo:(int (*)())f;");
  6529. verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
  6530. // If there's no return type (very rare in practice!), LLVM and Google style
  6531. // agree.
  6532. verifyFormat("- foo;");
  6533. verifyFormat("- foo:(int)f;");
  6534. verifyGoogleFormat("- foo:(int)foo;");
  6535. }
  6536. TEST_F(FormatTest, FormatObjCInterface) {
  6537. verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
  6538. "@public\n"
  6539. " int field1;\n"
  6540. "@protected\n"
  6541. " int field2;\n"
  6542. "@private\n"
  6543. " int field3;\n"
  6544. "@package\n"
  6545. " int field4;\n"
  6546. "}\n"
  6547. "+ (id)init;\n"
  6548. "@end");
  6549. verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
  6550. " @public\n"
  6551. " int field1;\n"
  6552. " @protected\n"
  6553. " int field2;\n"
  6554. " @private\n"
  6555. " int field3;\n"
  6556. " @package\n"
  6557. " int field4;\n"
  6558. "}\n"
  6559. "+ (id)init;\n"
  6560. "@end");
  6561. verifyFormat("@interface /* wait for it */ Foo\n"
  6562. "+ (id)init;\n"
  6563. "// Look, a comment!\n"
  6564. "- (int)answerWith:(int)i;\n"
  6565. "@end");
  6566. verifyFormat("@interface Foo\n"
  6567. "@end\n"
  6568. "@interface Bar\n"
  6569. "@end");
  6570. verifyFormat("@interface Foo : Bar\n"
  6571. "+ (id)init;\n"
  6572. "@end");
  6573. verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
  6574. "+ (id)init;\n"
  6575. "@end");
  6576. verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
  6577. "+ (id)init;\n"
  6578. "@end");
  6579. verifyFormat("@interface Foo (HackStuff)\n"
  6580. "+ (id)init;\n"
  6581. "@end");
  6582. verifyFormat("@interface Foo ()\n"
  6583. "+ (id)init;\n"
  6584. "@end");
  6585. verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
  6586. "+ (id)init;\n"
  6587. "@end");
  6588. verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
  6589. "+ (id)init;\n"
  6590. "@end");
  6591. verifyFormat("@interface Foo {\n"
  6592. " int _i;\n"
  6593. "}\n"
  6594. "+ (id)init;\n"
  6595. "@end");
  6596. verifyFormat("@interface Foo : Bar {\n"
  6597. " int _i;\n"
  6598. "}\n"
  6599. "+ (id)init;\n"
  6600. "@end");
  6601. verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
  6602. " int _i;\n"
  6603. "}\n"
  6604. "+ (id)init;\n"
  6605. "@end");
  6606. verifyFormat("@interface Foo (HackStuff) {\n"
  6607. " int _i;\n"
  6608. "}\n"
  6609. "+ (id)init;\n"
  6610. "@end");
  6611. verifyFormat("@interface Foo () {\n"
  6612. " int _i;\n"
  6613. "}\n"
  6614. "+ (id)init;\n"
  6615. "@end");
  6616. verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
  6617. " int _i;\n"
  6618. "}\n"
  6619. "+ (id)init;\n"
  6620. "@end");
  6621. FormatStyle OnePerLine = getGoogleStyle();
  6622. OnePerLine.BinPackParameters = false;
  6623. verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n"
  6624. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6625. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6626. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  6627. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
  6628. "}",
  6629. OnePerLine);
  6630. }
  6631. TEST_F(FormatTest, FormatObjCImplementation) {
  6632. verifyFormat("@implementation Foo : NSObject {\n"
  6633. "@public\n"
  6634. " int field1;\n"
  6635. "@protected\n"
  6636. " int field2;\n"
  6637. "@private\n"
  6638. " int field3;\n"
  6639. "@package\n"
  6640. " int field4;\n"
  6641. "}\n"
  6642. "+ (id)init {\n}\n"
  6643. "@end");
  6644. verifyGoogleFormat("@implementation Foo : NSObject {\n"
  6645. " @public\n"
  6646. " int field1;\n"
  6647. " @protected\n"
  6648. " int field2;\n"
  6649. " @private\n"
  6650. " int field3;\n"
  6651. " @package\n"
  6652. " int field4;\n"
  6653. "}\n"
  6654. "+ (id)init {\n}\n"
  6655. "@end");
  6656. verifyFormat("@implementation Foo\n"
  6657. "+ (id)init {\n"
  6658. " if (true)\n"
  6659. " return nil;\n"
  6660. "}\n"
  6661. "// Look, a comment!\n"
  6662. "- (int)answerWith:(int)i {\n"
  6663. " return i;\n"
  6664. "}\n"
  6665. "+ (int)answerWith:(int)i {\n"
  6666. " return i;\n"
  6667. "}\n"
  6668. "@end");
  6669. verifyFormat("@implementation Foo\n"
  6670. "@end\n"
  6671. "@implementation Bar\n"
  6672. "@end");
  6673. EXPECT_EQ("@implementation Foo : Bar\n"
  6674. "+ (id)init {\n}\n"
  6675. "- (void)foo {\n}\n"
  6676. "@end",
  6677. format("@implementation Foo : Bar\n"
  6678. "+(id)init{}\n"
  6679. "-(void)foo{}\n"
  6680. "@end"));
  6681. verifyFormat("@implementation Foo {\n"
  6682. " int _i;\n"
  6683. "}\n"
  6684. "+ (id)init {\n}\n"
  6685. "@end");
  6686. verifyFormat("@implementation Foo : Bar {\n"
  6687. " int _i;\n"
  6688. "}\n"
  6689. "+ (id)init {\n}\n"
  6690. "@end");
  6691. verifyFormat("@implementation Foo (HackStuff)\n"
  6692. "+ (id)init {\n}\n"
  6693. "@end");
  6694. verifyFormat("@implementation ObjcClass\n"
  6695. "- (void)method;\n"
  6696. "{}\n"
  6697. "@end");
  6698. }
  6699. TEST_F(FormatTest, FormatObjCProtocol) {
  6700. verifyFormat("@protocol Foo\n"
  6701. "@property(weak) id delegate;\n"
  6702. "- (NSUInteger)numberOfThings;\n"
  6703. "@end");
  6704. verifyFormat("@protocol MyProtocol <NSObject>\n"
  6705. "- (NSUInteger)numberOfThings;\n"
  6706. "@end");
  6707. verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
  6708. "- (NSUInteger)numberOfThings;\n"
  6709. "@end");
  6710. verifyFormat("@protocol Foo;\n"
  6711. "@protocol Bar;\n");
  6712. verifyFormat("@protocol Foo\n"
  6713. "@end\n"
  6714. "@protocol Bar\n"
  6715. "@end");
  6716. verifyFormat("@protocol myProtocol\n"
  6717. "- (void)mandatoryWithInt:(int)i;\n"
  6718. "@optional\n"
  6719. "- (void)optional;\n"
  6720. "@required\n"
  6721. "- (void)required;\n"
  6722. "@optional\n"
  6723. "@property(assign) int madProp;\n"
  6724. "@end\n");
  6725. verifyFormat("@property(nonatomic, assign, readonly)\n"
  6726. " int *looooooooooooooooooooooooooooongNumber;\n"
  6727. "@property(nonatomic, assign, readonly)\n"
  6728. " NSString *looooooooooooooooooooooooooooongName;");
  6729. verifyFormat("@implementation PR18406\n"
  6730. "}\n"
  6731. "@end");
  6732. }
  6733. TEST_F(FormatTest, FormatObjCMethodDeclarations) {
  6734. verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
  6735. " rect:(NSRect)theRect\n"
  6736. " interval:(float)theInterval {\n"
  6737. "}");
  6738. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6739. " longKeyword:(NSRect)theRect\n"
  6740. " longerKeyword:(float)theInterval\n"
  6741. " error:(NSError **)theError {\n"
  6742. "}");
  6743. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6744. " longKeyword:(NSRect)theRect\n"
  6745. " evenLongerKeyword:(float)theInterval\n"
  6746. " error:(NSError **)theError {\n"
  6747. "}");
  6748. verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
  6749. " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
  6750. " NS_DESIGNATED_INITIALIZER;",
  6751. getLLVMStyleWithColumns(60));
  6752. // Continuation indent width should win over aligning colons if the function
  6753. // name is long.
  6754. FormatStyle continuationStyle = getGoogleStyle();
  6755. continuationStyle.ColumnLimit = 40;
  6756. continuationStyle.IndentWrappedFunctionNames = true;
  6757. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6758. " dontAlignNamef:(NSRect)theRect {\n"
  6759. "}",
  6760. continuationStyle);
  6761. // Make sure we don't break aligning for short parameter names.
  6762. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  6763. " aShortf:(NSRect)theRect {\n"
  6764. "}",
  6765. continuationStyle);
  6766. }
  6767. TEST_F(FormatTest, FormatObjCMethodExpr) {
  6768. verifyFormat("[foo bar:baz];");
  6769. verifyFormat("return [foo bar:baz];");
  6770. verifyFormat("return (a)[foo bar:baz];");
  6771. verifyFormat("f([foo bar:baz]);");
  6772. verifyFormat("f(2, [foo bar:baz]);");
  6773. verifyFormat("f(2, a ? b : c);");
  6774. verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
  6775. // Unary operators.
  6776. verifyFormat("int a = +[foo bar:baz];");
  6777. verifyFormat("int a = -[foo bar:baz];");
  6778. verifyFormat("int a = ![foo bar:baz];");
  6779. verifyFormat("int a = ~[foo bar:baz];");
  6780. verifyFormat("int a = ++[foo bar:baz];");
  6781. verifyFormat("int a = --[foo bar:baz];");
  6782. verifyFormat("int a = sizeof [foo bar:baz];");
  6783. verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
  6784. verifyFormat("int a = &[foo bar:baz];");
  6785. verifyFormat("int a = *[foo bar:baz];");
  6786. // FIXME: Make casts work, without breaking f()[4].
  6787. // verifyFormat("int a = (int)[foo bar:baz];");
  6788. // verifyFormat("return (int)[foo bar:baz];");
  6789. // verifyFormat("(void)[foo bar:baz];");
  6790. verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
  6791. // Binary operators.
  6792. verifyFormat("[foo bar:baz], [foo bar:baz];");
  6793. verifyFormat("[foo bar:baz] = [foo bar:baz];");
  6794. verifyFormat("[foo bar:baz] *= [foo bar:baz];");
  6795. verifyFormat("[foo bar:baz] /= [foo bar:baz];");
  6796. verifyFormat("[foo bar:baz] %= [foo bar:baz];");
  6797. verifyFormat("[foo bar:baz] += [foo bar:baz];");
  6798. verifyFormat("[foo bar:baz] -= [foo bar:baz];");
  6799. verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
  6800. verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
  6801. verifyFormat("[foo bar:baz] &= [foo bar:baz];");
  6802. verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
  6803. verifyFormat("[foo bar:baz] |= [foo bar:baz];");
  6804. verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
  6805. verifyFormat("[foo bar:baz] || [foo bar:baz];");
  6806. verifyFormat("[foo bar:baz] && [foo bar:baz];");
  6807. verifyFormat("[foo bar:baz] | [foo bar:baz];");
  6808. verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
  6809. verifyFormat("[foo bar:baz] & [foo bar:baz];");
  6810. verifyFormat("[foo bar:baz] == [foo bar:baz];");
  6811. verifyFormat("[foo bar:baz] != [foo bar:baz];");
  6812. verifyFormat("[foo bar:baz] >= [foo bar:baz];");
  6813. verifyFormat("[foo bar:baz] <= [foo bar:baz];");
  6814. verifyFormat("[foo bar:baz] > [foo bar:baz];");
  6815. verifyFormat("[foo bar:baz] < [foo bar:baz];");
  6816. verifyFormat("[foo bar:baz] >> [foo bar:baz];");
  6817. verifyFormat("[foo bar:baz] << [foo bar:baz];");
  6818. verifyFormat("[foo bar:baz] - [foo bar:baz];");
  6819. verifyFormat("[foo bar:baz] + [foo bar:baz];");
  6820. verifyFormat("[foo bar:baz] * [foo bar:baz];");
  6821. verifyFormat("[foo bar:baz] / [foo bar:baz];");
  6822. verifyFormat("[foo bar:baz] % [foo bar:baz];");
  6823. // Whew!
  6824. verifyFormat("return in[42];");
  6825. verifyFormat("for (auto v : in[1]) {\n}");
  6826. verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
  6827. verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
  6828. verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
  6829. verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
  6830. verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
  6831. verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
  6832. "}");
  6833. verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
  6834. verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
  6835. verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
  6836. verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
  6837. verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
  6838. verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
  6839. verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
  6840. verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
  6841. verifyFormat("[button setAction:@selector(zoomOut:)];");
  6842. verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
  6843. verifyFormat("arr[[self indexForFoo:a]];");
  6844. verifyFormat("throw [self errorFor:a];");
  6845. verifyFormat("@throw [self errorFor:a];");
  6846. verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
  6847. verifyFormat("[(id)foo bar:(id) ? baz : quux];");
  6848. verifyFormat("4 > 4 ? (id)a : (id)baz;");
  6849. // This tests that the formatter doesn't break after "backing" but before ":",
  6850. // which would be at 80 columns.
  6851. verifyFormat(
  6852. "void f() {\n"
  6853. " if ((self = [super initWithContentRect:contentRect\n"
  6854. " styleMask:styleMask ?: otherMask\n"
  6855. " backing:NSBackingStoreBuffered\n"
  6856. " defer:YES]))");
  6857. verifyFormat(
  6858. "[foo checkThatBreakingAfterColonWorksOk:\n"
  6859. " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
  6860. verifyFormat("[myObj short:arg1 // Force line break\n"
  6861. " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
  6862. " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
  6863. " error:arg4];");
  6864. verifyFormat(
  6865. "void f() {\n"
  6866. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  6867. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  6868. " pos.width(), pos.height())\n"
  6869. " styleMask:NSBorderlessWindowMask\n"
  6870. " backing:NSBackingStoreBuffered\n"
  6871. " defer:NO]);\n"
  6872. "}");
  6873. verifyFormat(
  6874. "void f() {\n"
  6875. " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
  6876. " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
  6877. " pos.width(), pos.height())\n"
  6878. " syeMask:NSBorderlessWindowMask\n"
  6879. " bking:NSBackingStoreBuffered\n"
  6880. " der:NO]);\n"
  6881. "}",
  6882. getLLVMStyleWithColumns(70));
  6883. verifyFormat(
  6884. "void f() {\n"
  6885. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  6886. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  6887. " pos.width(), pos.height())\n"
  6888. " styleMask:NSBorderlessWindowMask\n"
  6889. " backing:NSBackingStoreBuffered\n"
  6890. " defer:NO]);\n"
  6891. "}",
  6892. getChromiumStyle(FormatStyle::LK_Cpp));
  6893. verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
  6894. " with:contentsNativeView];");
  6895. verifyFormat(
  6896. "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
  6897. " owner:nillllll];");
  6898. verifyFormat(
  6899. "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
  6900. " forType:kBookmarkButtonDragType];");
  6901. verifyFormat("[defaultCenter addObserver:self\n"
  6902. " selector:@selector(willEnterFullscreen)\n"
  6903. " name:kWillEnterFullscreenNotification\n"
  6904. " object:nil];");
  6905. verifyFormat("[image_rep drawInRect:drawRect\n"
  6906. " fromRect:NSZeroRect\n"
  6907. " operation:NSCompositeCopy\n"
  6908. " fraction:1.0\n"
  6909. " respectFlipped:NO\n"
  6910. " hints:nil];");
  6911. verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  6912. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  6913. verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  6914. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  6915. verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
  6916. " aaaaaaaaaaaaaaaaaaaaaa];");
  6917. verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
  6918. " .aaaaaaaa.aaaaaaaa];", // FIXME: Indentation seems off.
  6919. getLLVMStyleWithColumns(60));
  6920. verifyFormat(
  6921. "scoped_nsobject<NSTextField> message(\n"
  6922. " // The frame will be fixed up when |-setMessageText:| is called.\n"
  6923. " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
  6924. verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
  6925. " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
  6926. " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
  6927. " aaaa:bbb];");
  6928. verifyFormat("[self param:function( //\n"
  6929. " parameter)]");
  6930. verifyFormat(
  6931. "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  6932. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  6933. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
  6934. // FIXME: This violates the column limit.
  6935. verifyFormat(
  6936. "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
  6937. " aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
  6938. " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];",
  6939. getLLVMStyleWithColumns(60));
  6940. // Variadic parameters.
  6941. verifyFormat(
  6942. "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
  6943. verifyFormat(
  6944. "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  6945. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  6946. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
  6947. verifyFormat("[self // break\n"
  6948. " a:a\n"
  6949. " aaa:aaa];");
  6950. verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
  6951. " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
  6952. }
  6953. TEST_F(FormatTest, ObjCAt) {
  6954. verifyFormat("@autoreleasepool");
  6955. verifyFormat("@catch");
  6956. verifyFormat("@class");
  6957. verifyFormat("@compatibility_alias");
  6958. verifyFormat("@defs");
  6959. verifyFormat("@dynamic");
  6960. verifyFormat("@encode");
  6961. verifyFormat("@end");
  6962. verifyFormat("@finally");
  6963. verifyFormat("@implementation");
  6964. verifyFormat("@import");
  6965. verifyFormat("@interface");
  6966. verifyFormat("@optional");
  6967. verifyFormat("@package");
  6968. verifyFormat("@private");
  6969. verifyFormat("@property");
  6970. verifyFormat("@protected");
  6971. verifyFormat("@protocol");
  6972. verifyFormat("@public");
  6973. verifyFormat("@required");
  6974. verifyFormat("@selector");
  6975. verifyFormat("@synchronized");
  6976. verifyFormat("@synthesize");
  6977. verifyFormat("@throw");
  6978. verifyFormat("@try");
  6979. EXPECT_EQ("@interface", format("@ interface"));
  6980. // The precise formatting of this doesn't matter, nobody writes code like
  6981. // this.
  6982. verifyFormat("@ /*foo*/ interface");
  6983. }
  6984. TEST_F(FormatTest, ObjCSnippets) {
  6985. verifyFormat("@autoreleasepool {\n"
  6986. " foo();\n"
  6987. "}");
  6988. verifyFormat("@class Foo, Bar;");
  6989. verifyFormat("@compatibility_alias AliasName ExistingClass;");
  6990. verifyFormat("@dynamic textColor;");
  6991. verifyFormat("char *buf1 = @encode(int *);");
  6992. verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
  6993. verifyFormat("char *buf1 = @encode(int **);");
  6994. verifyFormat("Protocol *proto = @protocol(p1);");
  6995. verifyFormat("SEL s = @selector(foo:);");
  6996. verifyFormat("@synchronized(self) {\n"
  6997. " f();\n"
  6998. "}");
  6999. verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  7000. verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  7001. verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
  7002. verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
  7003. verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
  7004. verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
  7005. getMozillaStyle());
  7006. verifyFormat("@property BOOL editable;", getMozillaStyle());
  7007. verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
  7008. getWebKitStyle());
  7009. verifyFormat("@property BOOL editable;", getWebKitStyle());
  7010. verifyFormat("@import foo.bar;\n"
  7011. "@import baz;");
  7012. }
  7013. TEST_F(FormatTest, ObjCForIn) {
  7014. verifyFormat("- (void)test {\n"
  7015. " for (NSString *n in arrayOfStrings) {\n"
  7016. " foo(n);\n"
  7017. " }\n"
  7018. "}");
  7019. verifyFormat("- (void)test {\n"
  7020. " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n"
  7021. " foo(n);\n"
  7022. " }\n"
  7023. "}");
  7024. }
  7025. TEST_F(FormatTest, ObjCLiterals) {
  7026. verifyFormat("@\"String\"");
  7027. verifyFormat("@1");
  7028. verifyFormat("@+4.8");
  7029. verifyFormat("@-4");
  7030. verifyFormat("@1LL");
  7031. verifyFormat("@.5");
  7032. verifyFormat("@'c'");
  7033. verifyFormat("@true");
  7034. verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
  7035. verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
  7036. verifyFormat("NSNumber *favoriteColor = @(Green);");
  7037. verifyFormat("NSString *path = @(getenv(\"PATH\"));");
  7038. verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
  7039. }
  7040. TEST_F(FormatTest, ObjCDictLiterals) {
  7041. verifyFormat("@{");
  7042. verifyFormat("@{}");
  7043. verifyFormat("@{@\"one\" : @1}");
  7044. verifyFormat("return @{@\"one\" : @1;");
  7045. verifyFormat("@{@\"one\" : @1}");
  7046. verifyFormat("@{@\"one\" : @{@2 : @1}}");
  7047. verifyFormat("@{\n"
  7048. " @\"one\" : @{@2 : @1},\n"
  7049. "}");
  7050. verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
  7051. verifyIncompleteFormat("[self setDict:@{}");
  7052. verifyIncompleteFormat("[self setDict:@{@1 : @2}");
  7053. verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
  7054. verifyFormat(
  7055. "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
  7056. verifyFormat(
  7057. "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
  7058. verifyFormat("NSDictionary *d = @{\n"
  7059. " @\"nam\" : NSUserNam(),\n"
  7060. " @\"dte\" : [NSDate date],\n"
  7061. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  7062. "};");
  7063. verifyFormat(
  7064. "@{\n"
  7065. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  7066. "regularFont,\n"
  7067. "};");
  7068. verifyGoogleFormat(
  7069. "@{\n"
  7070. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  7071. "regularFont,\n"
  7072. "};");
  7073. verifyFormat(
  7074. "@{\n"
  7075. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
  7076. " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
  7077. "};");
  7078. // We should try to be robust in case someone forgets the "@".
  7079. verifyFormat("NSDictionary *d = {\n"
  7080. " @\"nam\" : NSUserNam(),\n"
  7081. " @\"dte\" : [NSDate date],\n"
  7082. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  7083. "};");
  7084. verifyFormat("NSMutableDictionary *dictionary =\n"
  7085. " [NSMutableDictionary dictionaryWithDictionary:@{\n"
  7086. " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
  7087. " bbbbbbbbbbbbbbbbbb : bbbbb,\n"
  7088. " cccccccccccccccc : ccccccccccccccc\n"
  7089. " }];");
  7090. // Ensure that casts before the key are kept on the same line as the key.
  7091. verifyFormat(
  7092. "NSDictionary *d = @{\n"
  7093. " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n"
  7094. " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n"
  7095. "};");
  7096. }
  7097. TEST_F(FormatTest, ObjCArrayLiterals) {
  7098. verifyIncompleteFormat("@[");
  7099. verifyFormat("@[]");
  7100. verifyFormat(
  7101. "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
  7102. verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
  7103. verifyFormat("NSArray *array = @[ [foo description] ];");
  7104. verifyFormat(
  7105. "NSArray *some_variable = @[\n"
  7106. " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
  7107. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7108. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7109. " @\"aaaaaaaaaaaaaaaaa\"\n"
  7110. "];");
  7111. verifyFormat("NSArray *some_variable = @[\n"
  7112. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7113. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7114. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7115. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7116. "];");
  7117. verifyGoogleFormat("NSArray *some_variable = @[\n"
  7118. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7119. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7120. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7121. " @\"aaaaaaaaaaaaaaaaa\"\n"
  7122. "];");
  7123. verifyFormat("NSArray *array = @[\n"
  7124. " @\"a\",\n"
  7125. " @\"a\",\n" // Trailing comma -> one per line.
  7126. "];");
  7127. // We should try to be robust in case someone forgets the "@".
  7128. verifyFormat("NSArray *some_variable = [\n"
  7129. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7130. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7131. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7132. " @\"aaaaaaaaaaaaaaaaa\",\n"
  7133. "];");
  7134. verifyFormat(
  7135. "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
  7136. " index:(NSUInteger)index\n"
  7137. " nonDigitAttributes:\n"
  7138. " (NSDictionary *)noDigitAttributes;");
  7139. verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
  7140. " NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
  7141. "]];");
  7142. }
  7143. TEST_F(FormatTest, BreaksStringLiterals) {
  7144. EXPECT_EQ("\"some text \"\n"
  7145. "\"other\";",
  7146. format("\"some text other\";", getLLVMStyleWithColumns(12)));
  7147. EXPECT_EQ("\"some text \"\n"
  7148. "\"other\";",
  7149. format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
  7150. EXPECT_EQ(
  7151. "#define A \\\n"
  7152. " \"some \" \\\n"
  7153. " \"text \" \\\n"
  7154. " \"other\";",
  7155. format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
  7156. EXPECT_EQ(
  7157. "#define A \\\n"
  7158. " \"so \" \\\n"
  7159. " \"text \" \\\n"
  7160. " \"other\";",
  7161. format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
  7162. EXPECT_EQ("\"some text\"",
  7163. format("\"some text\"", getLLVMStyleWithColumns(1)));
  7164. EXPECT_EQ("\"some text\"",
  7165. format("\"some text\"", getLLVMStyleWithColumns(11)));
  7166. EXPECT_EQ("\"some \"\n"
  7167. "\"text\"",
  7168. format("\"some text\"", getLLVMStyleWithColumns(10)));
  7169. EXPECT_EQ("\"some \"\n"
  7170. "\"text\"",
  7171. format("\"some text\"", getLLVMStyleWithColumns(7)));
  7172. EXPECT_EQ("\"some\"\n"
  7173. "\" tex\"\n"
  7174. "\"t\"",
  7175. format("\"some text\"", getLLVMStyleWithColumns(6)));
  7176. EXPECT_EQ("\"some\"\n"
  7177. "\" tex\"\n"
  7178. "\" and\"",
  7179. format("\"some tex and\"", getLLVMStyleWithColumns(6)));
  7180. EXPECT_EQ("\"some\"\n"
  7181. "\"/tex\"\n"
  7182. "\"/and\"",
  7183. format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
  7184. EXPECT_EQ("variable =\n"
  7185. " \"long string \"\n"
  7186. " \"literal\";",
  7187. format("variable = \"long string literal\";",
  7188. getLLVMStyleWithColumns(20)));
  7189. EXPECT_EQ("variable = f(\n"
  7190. " \"long string \"\n"
  7191. " \"literal\",\n"
  7192. " short,\n"
  7193. " loooooooooooooooooooong);",
  7194. format("variable = f(\"long string literal\", short, "
  7195. "loooooooooooooooooooong);",
  7196. getLLVMStyleWithColumns(20)));
  7197. EXPECT_EQ(
  7198. "f(g(\"long string \"\n"
  7199. " \"literal\"),\n"
  7200. " b);",
  7201. format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
  7202. EXPECT_EQ("f(g(\"long string \"\n"
  7203. " \"literal\",\n"
  7204. " a),\n"
  7205. " b);",
  7206. format("f(g(\"long string literal\", a), b);",
  7207. getLLVMStyleWithColumns(20)));
  7208. EXPECT_EQ(
  7209. "f(\"one two\".split(\n"
  7210. " variable));",
  7211. format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
  7212. EXPECT_EQ("f(\"one two three four five six \"\n"
  7213. " \"seven\".split(\n"
  7214. " really_looooong_variable));",
  7215. format("f(\"one two three four five six seven\"."
  7216. "split(really_looooong_variable));",
  7217. getLLVMStyleWithColumns(33)));
  7218. EXPECT_EQ("f(\"some \"\n"
  7219. " \"text\",\n"
  7220. " other);",
  7221. format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
  7222. // Only break as a last resort.
  7223. verifyFormat(
  7224. "aaaaaaaaaaaaaaaaaaaa(\n"
  7225. " aaaaaaaaaaaaaaaaaaaa,\n"
  7226. " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
  7227. EXPECT_EQ("\"splitmea\"\n"
  7228. "\"trandomp\"\n"
  7229. "\"oint\"",
  7230. format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
  7231. EXPECT_EQ("\"split/\"\n"
  7232. "\"pathat/\"\n"
  7233. "\"slashes\"",
  7234. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  7235. EXPECT_EQ("\"split/\"\n"
  7236. "\"pathat/\"\n"
  7237. "\"slashes\"",
  7238. format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
  7239. EXPECT_EQ("\"split at \"\n"
  7240. "\"spaces/at/\"\n"
  7241. "\"slashes.at.any$\"\n"
  7242. "\"non-alphanumeric%\"\n"
  7243. "\"1111111111characte\"\n"
  7244. "\"rs\"",
  7245. format("\"split at "
  7246. "spaces/at/"
  7247. "slashes.at."
  7248. "any$non-"
  7249. "alphanumeric%"
  7250. "1111111111characte"
  7251. "rs\"",
  7252. getLLVMStyleWithColumns(20)));
  7253. // Verify that splitting the strings understands
  7254. // Style::AlwaysBreakBeforeMultilineStrings.
  7255. EXPECT_EQ(
  7256. "aaaaaaaaaaaa(\n"
  7257. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
  7258. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
  7259. format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
  7260. "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
  7261. "aaaaaaaaaaaaaaaaaaaaaa\");",
  7262. getGoogleStyle()));
  7263. EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  7264. " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
  7265. format("return \"aaaaaaaaaaaaaaaaaaaaaa "
  7266. "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
  7267. "aaaaaaaaaaaaaaaaaaaaaa\";",
  7268. getGoogleStyle()));
  7269. EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  7270. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  7271. format("llvm::outs() << "
  7272. "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
  7273. "aaaaaaaaaaaaaaaaaaa\";"));
  7274. EXPECT_EQ("ffff(\n"
  7275. " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
  7276. " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  7277. format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
  7278. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
  7279. getGoogleStyle()));
  7280. FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
  7281. AlignLeft.AlignEscapedNewlinesLeft = true;
  7282. EXPECT_EQ("#define A \\\n"
  7283. " \"some \" \\\n"
  7284. " \"text \" \\\n"
  7285. " \"other\";",
  7286. format("#define A \"some text other\";", AlignLeft));
  7287. }
  7288. TEST_F(FormatTest, FullyRemoveEmptyLines) {
  7289. FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
  7290. NoEmptyLines.MaxEmptyLinesToKeep = 0;
  7291. EXPECT_EQ("int i = a(b());",
  7292. format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
  7293. }
  7294. TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
  7295. EXPECT_EQ(
  7296. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  7297. "(\n"
  7298. " \"x\t\");",
  7299. format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  7300. "aaaaaaa("
  7301. "\"x\t\");"));
  7302. }
  7303. TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
  7304. EXPECT_EQ(
  7305. "u8\"utf8 string \"\n"
  7306. "u8\"literal\";",
  7307. format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
  7308. EXPECT_EQ(
  7309. "u\"utf16 string \"\n"
  7310. "u\"literal\";",
  7311. format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
  7312. EXPECT_EQ(
  7313. "U\"utf32 string \"\n"
  7314. "U\"literal\";",
  7315. format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
  7316. EXPECT_EQ("L\"wide string \"\n"
  7317. "L\"literal\";",
  7318. format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
  7319. EXPECT_EQ("@\"NSString \"\n"
  7320. "@\"literal\";",
  7321. format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
  7322. // This input makes clang-format try to split the incomplete unicode escape
  7323. // sequence, which used to lead to a crasher.
  7324. verifyNoCrash(
  7325. "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  7326. getLLVMStyleWithColumns(60));
  7327. }
  7328. TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
  7329. FormatStyle Style = getGoogleStyleWithColumns(15);
  7330. EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
  7331. EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
  7332. EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
  7333. EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
  7334. EXPECT_EQ("u8R\"x(raw literal)x\";",
  7335. format("u8R\"x(raw literal)x\";", Style));
  7336. }
  7337. TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
  7338. FormatStyle Style = getLLVMStyleWithColumns(20);
  7339. EXPECT_EQ(
  7340. "_T(\"aaaaaaaaaaaaaa\")\n"
  7341. "_T(\"aaaaaaaaaaaaaa\")\n"
  7342. "_T(\"aaaaaaaaaaaa\")",
  7343. format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
  7344. EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
  7345. " _T(\"aaaaaa\"),\n"
  7346. " z);",
  7347. format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
  7348. // FIXME: Handle embedded spaces in one iteration.
  7349. // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
  7350. // "_T(\"aaaaaaaaaaaaa\")\n"
  7351. // "_T(\"aaaaaaaaaaaaa\")\n"
  7352. // "_T(\"a\")",
  7353. // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  7354. // getLLVMStyleWithColumns(20)));
  7355. EXPECT_EQ(
  7356. "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
  7357. format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
  7358. EXPECT_EQ("f(\n"
  7359. "#if !TEST\n"
  7360. " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
  7361. "#endif\n"
  7362. " );",
  7363. format("f(\n"
  7364. "#if !TEST\n"
  7365. "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
  7366. "#endif\n"
  7367. ");"));
  7368. EXPECT_EQ("f(\n"
  7369. "\n"
  7370. " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
  7371. format("f(\n"
  7372. "\n"
  7373. "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
  7374. }
  7375. TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
  7376. EXPECT_EQ(
  7377. "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7378. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7379. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
  7380. format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7381. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
  7382. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
  7383. }
  7384. TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
  7385. EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
  7386. format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle()));
  7387. EXPECT_EQ("fffffffffff(g(R\"x(\n"
  7388. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7389. ")x\",\n"
  7390. " a),\n"
  7391. " b);",
  7392. format("fffffffffff(g(R\"x(\n"
  7393. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7394. ")x\", a), b);",
  7395. getGoogleStyleWithColumns(20)));
  7396. EXPECT_EQ("fffffffffff(\n"
  7397. " g(R\"x(qqq\n"
  7398. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7399. ")x\",\n"
  7400. " a),\n"
  7401. " b);",
  7402. format("fffffffffff(g(R\"x(qqq\n"
  7403. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7404. ")x\", a), b);",
  7405. getGoogleStyleWithColumns(20)));
  7406. EXPECT_EQ("fffffffffff(R\"x(\n"
  7407. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7408. ")x\");",
  7409. format("fffffffffff(R\"x(\n"
  7410. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7411. ")x\");",
  7412. getGoogleStyleWithColumns(20)));
  7413. EXPECT_EQ("fffffffffff(R\"x(\n"
  7414. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7415. ")x\" + bbbbbb);",
  7416. format("fffffffffff(R\"x(\n"
  7417. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7418. ")x\" + bbbbbb);",
  7419. getGoogleStyleWithColumns(20)));
  7420. EXPECT_EQ("fffffffffff(\n"
  7421. " R\"x(\n"
  7422. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7423. ")x\" +\n"
  7424. " bbbbbb);",
  7425. format("fffffffffff(\n"
  7426. " R\"x(\n"
  7427. "multiline raw string literal xxxxxxxxxxxxxx\n"
  7428. ")x\" + bbbbbb);",
  7429. getGoogleStyleWithColumns(20)));
  7430. }
  7431. TEST_F(FormatTest, SkipsUnknownStringLiterals) {
  7432. verifyFormat("string a = \"unterminated;");
  7433. EXPECT_EQ("function(\"unterminated,\n"
  7434. " OtherParameter);",
  7435. format("function( \"unterminated,\n"
  7436. " OtherParameter);"));
  7437. }
  7438. TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
  7439. FormatStyle Style = getLLVMStyle();
  7440. Style.Standard = FormatStyle::LS_Cpp03;
  7441. EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
  7442. format("#define x(_a) printf(\"foo\"_a);", Style));
  7443. }
  7444. TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
  7445. TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
  7446. EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
  7447. " \"ddeeefff\");",
  7448. format("someFunction(\"aaabbbcccdddeeefff\");",
  7449. getLLVMStyleWithColumns(25)));
  7450. EXPECT_EQ("someFunction1234567890(\n"
  7451. " \"aaabbbcccdddeeefff\");",
  7452. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  7453. getLLVMStyleWithColumns(26)));
  7454. EXPECT_EQ("someFunction1234567890(\n"
  7455. " \"aaabbbcccdddeeeff\"\n"
  7456. " \"f\");",
  7457. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  7458. getLLVMStyleWithColumns(25)));
  7459. EXPECT_EQ("someFunction1234567890(\n"
  7460. " \"aaabbbcccdddeeeff\"\n"
  7461. " \"f\");",
  7462. format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
  7463. getLLVMStyleWithColumns(24)));
  7464. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  7465. " \"ddde \"\n"
  7466. " \"efff\");",
  7467. format("someFunction(\"aaabbbcc ddde efff\");",
  7468. getLLVMStyleWithColumns(25)));
  7469. EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
  7470. " \"ddeeefff\");",
  7471. format("someFunction(\"aaabbbccc ddeeefff\");",
  7472. getLLVMStyleWithColumns(25)));
  7473. EXPECT_EQ("someFunction1234567890(\n"
  7474. " \"aaabb \"\n"
  7475. " \"cccdddeeefff\");",
  7476. format("someFunction1234567890(\"aaabb cccdddeeefff\");",
  7477. getLLVMStyleWithColumns(25)));
  7478. EXPECT_EQ("#define A \\\n"
  7479. " string s = \\\n"
  7480. " \"123456789\" \\\n"
  7481. " \"0\"; \\\n"
  7482. " int i;",
  7483. format("#define A string s = \"1234567890\"; int i;",
  7484. getLLVMStyleWithColumns(20)));
  7485. // FIXME: Put additional penalties on breaking at non-whitespace locations.
  7486. EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
  7487. " \"dddeeeff\"\n"
  7488. " \"f\");",
  7489. format("someFunction(\"aaabbbcc dddeeefff\");",
  7490. getLLVMStyleWithColumns(25)));
  7491. }
  7492. TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
  7493. EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
  7494. EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
  7495. EXPECT_EQ("\"test\"\n"
  7496. "\"\\n\"",
  7497. format("\"test\\n\"", getLLVMStyleWithColumns(7)));
  7498. EXPECT_EQ("\"tes\\\\\"\n"
  7499. "\"n\"",
  7500. format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
  7501. EXPECT_EQ("\"\\\\\\\\\"\n"
  7502. "\"\\n\"",
  7503. format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
  7504. EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
  7505. EXPECT_EQ("\"\\uff01\"\n"
  7506. "\"test\"",
  7507. format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
  7508. EXPECT_EQ("\"\\Uff01ff02\"",
  7509. format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
  7510. EXPECT_EQ("\"\\x000000000001\"\n"
  7511. "\"next\"",
  7512. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
  7513. EXPECT_EQ("\"\\x000000000001next\"",
  7514. format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
  7515. EXPECT_EQ("\"\\x000000000001\"",
  7516. format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
  7517. EXPECT_EQ("\"test\"\n"
  7518. "\"\\000000\"\n"
  7519. "\"000001\"",
  7520. format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
  7521. EXPECT_EQ("\"test\\000\"\n"
  7522. "\"00000000\"\n"
  7523. "\"1\"",
  7524. format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
  7525. }
  7526. TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
  7527. verifyFormat("void f() {\n"
  7528. " return g() {}\n"
  7529. " void h() {}");
  7530. verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
  7531. "g();\n"
  7532. "}");
  7533. }
  7534. TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
  7535. verifyFormat(
  7536. "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
  7537. }
  7538. TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
  7539. verifyFormat("class X {\n"
  7540. " void f() {\n"
  7541. " }\n"
  7542. "};",
  7543. getLLVMStyleWithColumns(12));
  7544. }
  7545. TEST_F(FormatTest, ConfigurableIndentWidth) {
  7546. FormatStyle EightIndent = getLLVMStyleWithColumns(18);
  7547. EightIndent.IndentWidth = 8;
  7548. EightIndent.ContinuationIndentWidth = 8;
  7549. verifyFormat("void f() {\n"
  7550. " someFunction();\n"
  7551. " if (true) {\n"
  7552. " f();\n"
  7553. " }\n"
  7554. "}",
  7555. EightIndent);
  7556. verifyFormat("class X {\n"
  7557. " void f() {\n"
  7558. " }\n"
  7559. "};",
  7560. EightIndent);
  7561. verifyFormat("int x[] = {\n"
  7562. " call(),\n"
  7563. " call()};",
  7564. EightIndent);
  7565. }
  7566. TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
  7567. verifyFormat("double\n"
  7568. "f();",
  7569. getLLVMStyleWithColumns(8));
  7570. }
  7571. TEST_F(FormatTest, ConfigurableUseOfTab) {
  7572. FormatStyle Tab = getLLVMStyleWithColumns(42);
  7573. Tab.IndentWidth = 8;
  7574. Tab.UseTab = FormatStyle::UT_Always;
  7575. Tab.AlignEscapedNewlinesLeft = true;
  7576. EXPECT_EQ("if (aaaaaaaa && // q\n"
  7577. " bb)\t\t// w\n"
  7578. "\t;",
  7579. format("if (aaaaaaaa &&// q\n"
  7580. "bb)// w\n"
  7581. ";",
  7582. Tab));
  7583. EXPECT_EQ("if (aaa && bbb) // w\n"
  7584. "\t;",
  7585. format("if(aaa&&bbb)// w\n"
  7586. ";",
  7587. Tab));
  7588. verifyFormat("class X {\n"
  7589. "\tvoid f() {\n"
  7590. "\t\tsomeFunction(parameter1,\n"
  7591. "\t\t\t parameter2);\n"
  7592. "\t}\n"
  7593. "};",
  7594. Tab);
  7595. verifyFormat("#define A \\\n"
  7596. "\tvoid f() { \\\n"
  7597. "\t\tsomeFunction( \\\n"
  7598. "\t\t parameter1, \\\n"
  7599. "\t\t parameter2); \\\n"
  7600. "\t}",
  7601. Tab);
  7602. Tab.TabWidth = 4;
  7603. Tab.IndentWidth = 8;
  7604. verifyFormat("class TabWidth4Indent8 {\n"
  7605. "\t\tvoid f() {\n"
  7606. "\t\t\t\tsomeFunction(parameter1,\n"
  7607. "\t\t\t\t\t\t\t parameter2);\n"
  7608. "\t\t}\n"
  7609. "};",
  7610. Tab);
  7611. Tab.TabWidth = 4;
  7612. Tab.IndentWidth = 4;
  7613. verifyFormat("class TabWidth4Indent4 {\n"
  7614. "\tvoid f() {\n"
  7615. "\t\tsomeFunction(parameter1,\n"
  7616. "\t\t\t\t\t parameter2);\n"
  7617. "\t}\n"
  7618. "};",
  7619. Tab);
  7620. Tab.TabWidth = 8;
  7621. Tab.IndentWidth = 4;
  7622. verifyFormat("class TabWidth8Indent4 {\n"
  7623. " void f() {\n"
  7624. "\tsomeFunction(parameter1,\n"
  7625. "\t\t parameter2);\n"
  7626. " }\n"
  7627. "};",
  7628. Tab);
  7629. Tab.TabWidth = 8;
  7630. Tab.IndentWidth = 8;
  7631. EXPECT_EQ("/*\n"
  7632. "\t a\t\tcomment\n"
  7633. "\t in multiple lines\n"
  7634. " */",
  7635. format(" /*\t \t \n"
  7636. " \t \t a\t\tcomment\t \t\n"
  7637. " \t \t in multiple lines\t\n"
  7638. " \t */",
  7639. Tab));
  7640. Tab.UseTab = FormatStyle::UT_ForIndentation;
  7641. verifyFormat("{\n"
  7642. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7643. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7644. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7645. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7646. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7647. "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
  7648. "};",
  7649. Tab);
  7650. verifyFormat("enum A {\n"
  7651. "\ta1, // Force multiple lines\n"
  7652. "\ta2,\n"
  7653. "\ta3\n"
  7654. "};",
  7655. Tab);
  7656. EXPECT_EQ("if (aaaaaaaa && // q\n"
  7657. " bb) // w\n"
  7658. "\t;",
  7659. format("if (aaaaaaaa &&// q\n"
  7660. "bb)// w\n"
  7661. ";",
  7662. Tab));
  7663. verifyFormat("class X {\n"
  7664. "\tvoid f() {\n"
  7665. "\t\tsomeFunction(parameter1,\n"
  7666. "\t\t parameter2);\n"
  7667. "\t}\n"
  7668. "};",
  7669. Tab);
  7670. verifyFormat("{\n"
  7671. "\tQ(\n"
  7672. "\t {\n"
  7673. "\t\t int a;\n"
  7674. "\t\t someFunction(aaaaaaaa,\n"
  7675. "\t\t bbbbbbb);\n"
  7676. "\t },\n"
  7677. "\t p);\n"
  7678. "}",
  7679. Tab);
  7680. EXPECT_EQ("{\n"
  7681. "\t/* aaaa\n"
  7682. "\t bbbb */\n"
  7683. "}",
  7684. format("{\n"
  7685. "/* aaaa\n"
  7686. " bbbb */\n"
  7687. "}",
  7688. Tab));
  7689. EXPECT_EQ("{\n"
  7690. "\t/*\n"
  7691. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7692. "\t bbbbbbbbbbbbb\n"
  7693. "\t*/\n"
  7694. "}",
  7695. format("{\n"
  7696. "/*\n"
  7697. " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7698. "*/\n"
  7699. "}",
  7700. Tab));
  7701. EXPECT_EQ("{\n"
  7702. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7703. "\t// bbbbbbbbbbbbb\n"
  7704. "}",
  7705. format("{\n"
  7706. "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7707. "}",
  7708. Tab));
  7709. EXPECT_EQ("{\n"
  7710. "\t/*\n"
  7711. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  7712. "\t bbbbbbbbbbbbb\n"
  7713. "\t*/\n"
  7714. "}",
  7715. format("{\n"
  7716. "\t/*\n"
  7717. "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
  7718. "\t*/\n"
  7719. "}",
  7720. Tab));
  7721. EXPECT_EQ("{\n"
  7722. "\t/*\n"
  7723. "\n"
  7724. "\t*/\n"
  7725. "}",
  7726. format("{\n"
  7727. "\t/*\n"
  7728. "\n"
  7729. "\t*/\n"
  7730. "}",
  7731. Tab));
  7732. EXPECT_EQ("{\n"
  7733. "\t/*\n"
  7734. " asdf\n"
  7735. "\t*/\n"
  7736. "}",
  7737. format("{\n"
  7738. "\t/*\n"
  7739. " asdf\n"
  7740. "\t*/\n"
  7741. "}",
  7742. Tab));
  7743. Tab.UseTab = FormatStyle::UT_Never;
  7744. EXPECT_EQ("/*\n"
  7745. " a\t\tcomment\n"
  7746. " in multiple lines\n"
  7747. " */",
  7748. format(" /*\t \t \n"
  7749. " \t \t a\t\tcomment\t \t\n"
  7750. " \t \t in multiple lines\t\n"
  7751. " \t */",
  7752. Tab));
  7753. EXPECT_EQ("/* some\n"
  7754. " comment */",
  7755. format(" \t \t /* some\n"
  7756. " \t \t comment */",
  7757. Tab));
  7758. EXPECT_EQ("int a; /* some\n"
  7759. " comment */",
  7760. format(" \t \t int a; /* some\n"
  7761. " \t \t comment */",
  7762. Tab));
  7763. EXPECT_EQ("int a; /* some\n"
  7764. "comment */",
  7765. format(" \t \t int\ta; /* some\n"
  7766. " \t \t comment */",
  7767. Tab));
  7768. EXPECT_EQ("f(\"\t\t\"); /* some\n"
  7769. " comment */",
  7770. format(" \t \t f(\"\t\t\"); /* some\n"
  7771. " \t \t comment */",
  7772. Tab));
  7773. EXPECT_EQ("{\n"
  7774. " /*\n"
  7775. " * Comment\n"
  7776. " */\n"
  7777. " int i;\n"
  7778. "}",
  7779. format("{\n"
  7780. "\t/*\n"
  7781. "\t * Comment\n"
  7782. "\t */\n"
  7783. "\t int i;\n"
  7784. "}"));
  7785. }
  7786. TEST_F(FormatTest, CalculatesOriginalColumn) {
  7787. EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7788. "q\"; /* some\n"
  7789. " comment */",
  7790. format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7791. "q\"; /* some\n"
  7792. " comment */",
  7793. getLLVMStyle()));
  7794. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  7795. "/* some\n"
  7796. " comment */",
  7797. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
  7798. " /* some\n"
  7799. " comment */",
  7800. getLLVMStyle()));
  7801. EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7802. "qqq\n"
  7803. "/* some\n"
  7804. " comment */",
  7805. format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7806. "qqq\n"
  7807. " /* some\n"
  7808. " comment */",
  7809. getLLVMStyle()));
  7810. EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7811. "wwww; /* some\n"
  7812. " comment */",
  7813. format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
  7814. "wwww; /* some\n"
  7815. " comment */",
  7816. getLLVMStyle()));
  7817. }
  7818. TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
  7819. FormatStyle NoSpace = getLLVMStyle();
  7820. NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
  7821. verifyFormat("while(true)\n"
  7822. " continue;",
  7823. NoSpace);
  7824. verifyFormat("for(;;)\n"
  7825. " continue;",
  7826. NoSpace);
  7827. verifyFormat("if(true)\n"
  7828. " f();\n"
  7829. "else if(true)\n"
  7830. " f();",
  7831. NoSpace);
  7832. verifyFormat("do {\n"
  7833. " do_something();\n"
  7834. "} while(something());",
  7835. NoSpace);
  7836. verifyFormat("switch(x) {\n"
  7837. "default:\n"
  7838. " break;\n"
  7839. "}",
  7840. NoSpace);
  7841. verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
  7842. verifyFormat("size_t x = sizeof(x);", NoSpace);
  7843. verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
  7844. verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
  7845. verifyFormat("alignas(128) char a[128];", NoSpace);
  7846. verifyFormat("size_t x = alignof(MyType);", NoSpace);
  7847. verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
  7848. verifyFormat("int f() throw(Deprecated);", NoSpace);
  7849. verifyFormat("typedef void (*cb)(int);", NoSpace);
  7850. verifyFormat("T A::operator()();", NoSpace);
  7851. verifyFormat("X A::operator++(T);", NoSpace);
  7852. FormatStyle Space = getLLVMStyle();
  7853. Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
  7854. verifyFormat("int f ();", Space);
  7855. verifyFormat("void f (int a, T b) {\n"
  7856. " while (true)\n"
  7857. " continue;\n"
  7858. "}",
  7859. Space);
  7860. verifyFormat("if (true)\n"
  7861. " f ();\n"
  7862. "else if (true)\n"
  7863. " f ();",
  7864. Space);
  7865. verifyFormat("do {\n"
  7866. " do_something ();\n"
  7867. "} while (something ());",
  7868. Space);
  7869. verifyFormat("switch (x) {\n"
  7870. "default:\n"
  7871. " break;\n"
  7872. "}",
  7873. Space);
  7874. verifyFormat("A::A () : a (1) {}", Space);
  7875. verifyFormat("void f () __attribute__ ((asdf));", Space);
  7876. verifyFormat("*(&a + 1);\n"
  7877. "&((&a)[1]);\n"
  7878. "a[(b + c) * d];\n"
  7879. "(((a + 1) * 2) + 3) * 4;",
  7880. Space);
  7881. verifyFormat("#define A(x) x", Space);
  7882. verifyFormat("#define A (x) x", Space);
  7883. verifyFormat("#if defined(x)\n"
  7884. "#endif",
  7885. Space);
  7886. verifyFormat("auto i = std::make_unique<int> (5);", Space);
  7887. verifyFormat("size_t x = sizeof (x);", Space);
  7888. verifyFormat("auto f (int x) -> decltype (x);", Space);
  7889. verifyFormat("int f (T x) noexcept (x.create ());", Space);
  7890. verifyFormat("alignas (128) char a[128];", Space);
  7891. verifyFormat("size_t x = alignof (MyType);", Space);
  7892. verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
  7893. verifyFormat("int f () throw (Deprecated);", Space);
  7894. verifyFormat("typedef void (*cb) (int);", Space);
  7895. verifyFormat("T A::operator() ();", Space);
  7896. verifyFormat("X A::operator++ (T);", Space);
  7897. }
  7898. TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
  7899. FormatStyle Spaces = getLLVMStyle();
  7900. Spaces.SpacesInParentheses = true;
  7901. verifyFormat("call( x, y, z );", Spaces);
  7902. verifyFormat("call();", Spaces);
  7903. verifyFormat("std::function<void( int, int )> callback;", Spaces);
  7904. verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
  7905. Spaces);
  7906. verifyFormat("while ( (bool)1 )\n"
  7907. " continue;",
  7908. Spaces);
  7909. verifyFormat("for ( ;; )\n"
  7910. " continue;",
  7911. Spaces);
  7912. verifyFormat("if ( true )\n"
  7913. " f();\n"
  7914. "else if ( true )\n"
  7915. " f();",
  7916. Spaces);
  7917. verifyFormat("do {\n"
  7918. " do_something( (int)i );\n"
  7919. "} while ( something() );",
  7920. Spaces);
  7921. verifyFormat("switch ( x ) {\n"
  7922. "default:\n"
  7923. " break;\n"
  7924. "}",
  7925. Spaces);
  7926. Spaces.SpacesInParentheses = false;
  7927. Spaces.SpacesInCStyleCastParentheses = true;
  7928. verifyFormat("Type *A = ( Type * )P;", Spaces);
  7929. verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
  7930. verifyFormat("x = ( int32 )y;", Spaces);
  7931. verifyFormat("int a = ( int )(2.0f);", Spaces);
  7932. verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
  7933. verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
  7934. verifyFormat("#define x (( int )-1)", Spaces);
  7935. // Run the first set of tests again with:
  7936. Spaces.SpacesInParentheses = false, Spaces.SpaceInEmptyParentheses = true;
  7937. Spaces.SpacesInCStyleCastParentheses = true;
  7938. verifyFormat("call(x, y, z);", Spaces);
  7939. verifyFormat("call( );", Spaces);
  7940. verifyFormat("std::function<void(int, int)> callback;", Spaces);
  7941. verifyFormat("while (( bool )1)\n"
  7942. " continue;",
  7943. Spaces);
  7944. verifyFormat("for (;;)\n"
  7945. " continue;",
  7946. Spaces);
  7947. verifyFormat("if (true)\n"
  7948. " f( );\n"
  7949. "else if (true)\n"
  7950. " f( );",
  7951. Spaces);
  7952. verifyFormat("do {\n"
  7953. " do_something(( int )i);\n"
  7954. "} while (something( ));",
  7955. Spaces);
  7956. verifyFormat("switch (x) {\n"
  7957. "default:\n"
  7958. " break;\n"
  7959. "}",
  7960. Spaces);
  7961. // Run the first set of tests again with:
  7962. Spaces.SpaceAfterCStyleCast = true;
  7963. verifyFormat("call(x, y, z);", Spaces);
  7964. verifyFormat("call( );", Spaces);
  7965. verifyFormat("std::function<void(int, int)> callback;", Spaces);
  7966. verifyFormat("while (( bool ) 1)\n"
  7967. " continue;",
  7968. Spaces);
  7969. verifyFormat("for (;;)\n"
  7970. " continue;",
  7971. Spaces);
  7972. verifyFormat("if (true)\n"
  7973. " f( );\n"
  7974. "else if (true)\n"
  7975. " f( );",
  7976. Spaces);
  7977. verifyFormat("do {\n"
  7978. " do_something(( int ) i);\n"
  7979. "} while (something( ));",
  7980. Spaces);
  7981. verifyFormat("switch (x) {\n"
  7982. "default:\n"
  7983. " break;\n"
  7984. "}",
  7985. Spaces);
  7986. // Run subset of tests again with:
  7987. Spaces.SpacesInCStyleCastParentheses = false;
  7988. Spaces.SpaceAfterCStyleCast = true;
  7989. verifyFormat("while ((bool) 1)\n"
  7990. " continue;",
  7991. Spaces);
  7992. verifyFormat("do {\n"
  7993. " do_something((int) i);\n"
  7994. "} while (something( ));",
  7995. Spaces);
  7996. }
  7997. TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
  7998. verifyFormat("int a[5];");
  7999. verifyFormat("a[3] += 42;");
  8000. FormatStyle Spaces = getLLVMStyle();
  8001. Spaces.SpacesInSquareBrackets = true;
  8002. // Lambdas unchanged.
  8003. verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
  8004. verifyFormat("return [i, args...] {};", Spaces);
  8005. // Not lambdas.
  8006. verifyFormat("int a[ 5 ];", Spaces);
  8007. verifyFormat("a[ 3 ] += 42;", Spaces);
  8008. verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
  8009. verifyFormat("double &operator[](int i) { return 0; }\n"
  8010. "int i;",
  8011. Spaces);
  8012. verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
  8013. verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
  8014. verifyFormat("int i = (*b)[ a ]->f();", Spaces);
  8015. }
  8016. TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
  8017. verifyFormat("int a = 5;");
  8018. verifyFormat("a += 42;");
  8019. verifyFormat("a or_eq 8;");
  8020. FormatStyle Spaces = getLLVMStyle();
  8021. Spaces.SpaceBeforeAssignmentOperators = false;
  8022. verifyFormat("int a= 5;", Spaces);
  8023. verifyFormat("a+= 42;", Spaces);
  8024. verifyFormat("a or_eq 8;", Spaces);
  8025. }
  8026. TEST_F(FormatTest, AlignConsecutiveAssignments) {
  8027. FormatStyle Alignment = getLLVMStyle();
  8028. Alignment.AlignConsecutiveAssignments = false;
  8029. verifyFormat("int a = 5;\n"
  8030. "int oneTwoThree = 123;",
  8031. Alignment);
  8032. verifyFormat("int a = 5;\n"
  8033. "int oneTwoThree = 123;",
  8034. Alignment);
  8035. Alignment.AlignConsecutiveAssignments = true;
  8036. verifyFormat("int a = 5;\n"
  8037. "int oneTwoThree = 123;",
  8038. Alignment);
  8039. verifyFormat("int a = method();\n"
  8040. "int oneTwoThree = 133;",
  8041. Alignment);
  8042. verifyFormat("a &= 5;\n"
  8043. "bcd *= 5;\n"
  8044. "ghtyf += 5;\n"
  8045. "dvfvdb -= 5;\n"
  8046. "a /= 5;\n"
  8047. "vdsvsv %= 5;\n"
  8048. "sfdbddfbdfbb ^= 5;\n"
  8049. "dvsdsv |= 5;\n"
  8050. "int dsvvdvsdvvv = 123;",
  8051. Alignment);
  8052. verifyFormat("int i = 1, j = 10;\n"
  8053. "something = 2000;",
  8054. Alignment);
  8055. verifyFormat("something = 2000;\n"
  8056. "int i = 1, j = 10;\n",
  8057. Alignment);
  8058. verifyFormat("something = 2000;\n"
  8059. "another = 911;\n"
  8060. "int i = 1, j = 10;\n"
  8061. "oneMore = 1;\n"
  8062. "i = 2;",
  8063. Alignment);
  8064. verifyFormat("int a = 5;\n"
  8065. "int one = 1;\n"
  8066. "method();\n"
  8067. "int oneTwoThree = 123;\n"
  8068. "int oneTwo = 12;",
  8069. Alignment);
  8070. verifyFormat("int oneTwoThree = 123;\n"
  8071. "int oneTwo = 12;\n"
  8072. "method();\n",
  8073. Alignment);
  8074. verifyFormat("int oneTwoThree = 123; // comment\n"
  8075. "int oneTwo = 12; // comment",
  8076. Alignment);
  8077. EXPECT_EQ("int a = 5;\n"
  8078. "\n"
  8079. "int oneTwoThree = 123;",
  8080. format("int a = 5;\n"
  8081. "\n"
  8082. "int oneTwoThree= 123;",
  8083. Alignment));
  8084. EXPECT_EQ("int a = 5;\n"
  8085. "int one = 1;\n"
  8086. "\n"
  8087. "int oneTwoThree = 123;",
  8088. format("int a = 5;\n"
  8089. "int one = 1;\n"
  8090. "\n"
  8091. "int oneTwoThree = 123;",
  8092. Alignment));
  8093. EXPECT_EQ("int a = 5;\n"
  8094. "int one = 1;\n"
  8095. "\n"
  8096. "int oneTwoThree = 123;\n"
  8097. "int oneTwo = 12;",
  8098. format("int a = 5;\n"
  8099. "int one = 1;\n"
  8100. "\n"
  8101. "int oneTwoThree = 123;\n"
  8102. "int oneTwo = 12;",
  8103. Alignment));
  8104. Alignment.AlignEscapedNewlinesLeft = true;
  8105. verifyFormat("#define A \\\n"
  8106. " int aaaa = 12; \\\n"
  8107. " int b = 23; \\\n"
  8108. " int ccc = 234; \\\n"
  8109. " int dddddddddd = 2345;",
  8110. Alignment);
  8111. Alignment.AlignEscapedNewlinesLeft = false;
  8112. verifyFormat("#define A "
  8113. " \\\n"
  8114. " int aaaa = 12; "
  8115. " \\\n"
  8116. " int b = 23; "
  8117. " \\\n"
  8118. " int ccc = 234; "
  8119. " \\\n"
  8120. " int dddddddddd = 2345;",
  8121. Alignment);
  8122. verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
  8123. "k = 4, int l = 5,\n"
  8124. " int m = 6) {\n"
  8125. " int j = 10;\n"
  8126. " otherThing = 1;\n"
  8127. "}",
  8128. Alignment);
  8129. verifyFormat("void SomeFunction(int parameter = 0) {\n"
  8130. " int i = 1;\n"
  8131. " int j = 2;\n"
  8132. " int big = 10000;\n"
  8133. "}",
  8134. Alignment);
  8135. verifyFormat("class C {\n"
  8136. "public:\n"
  8137. " int i = 1;\n"
  8138. " virtual void f() = 0;\n"
  8139. "};",
  8140. Alignment);
  8141. verifyFormat("int i = 1;\n"
  8142. "if (SomeType t = getSomething()) {\n"
  8143. "}\n"
  8144. "int j = 2;\n"
  8145. "int big = 10000;",
  8146. Alignment);
  8147. verifyFormat("int j = 7;\n"
  8148. "for (int k = 0; k < N; ++k) {\n"
  8149. "}\n"
  8150. "int j = 2;\n"
  8151. "int big = 10000;\n"
  8152. "}",
  8153. Alignment);
  8154. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  8155. verifyFormat("int i = 1;\n"
  8156. "LooooooooooongType loooooooooooooooooooooongVariable\n"
  8157. " = someLooooooooooooooooongFunction();\n"
  8158. "int j = 2;",
  8159. Alignment);
  8160. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
  8161. verifyFormat("int i = 1;\n"
  8162. "LooooooooooongType loooooooooooooooooooooongVariable =\n"
  8163. " someLooooooooooooooooongFunction();\n"
  8164. "int j = 2;",
  8165. Alignment);
  8166. verifyFormat("auto lambda = []() {\n"
  8167. " auto i = 0;\n"
  8168. " return 0;\n"
  8169. "};\n"
  8170. "int i = 0;\n"
  8171. "auto v = type{\n"
  8172. " i = 1, //\n"
  8173. " (i = 2), //\n"
  8174. " i = 3 //\n"
  8175. "};",
  8176. Alignment);
  8177. // FIXME: Should align all three assignments
  8178. verifyFormat(
  8179. "int i = 1;\n"
  8180. "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
  8181. " loooooooooooooooooooooongParameterB);\n"
  8182. "int j = 2;",
  8183. Alignment);
  8184. verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
  8185. " typename B = very_long_type_name_1,\n"
  8186. " typename T_2 = very_long_type_name_2>\n"
  8187. "auto foo() {}\n",
  8188. Alignment);
  8189. verifyFormat("int a, b = 1;\n"
  8190. "int c = 2;\n"
  8191. "int dd = 3;\n",
  8192. Alignment);
  8193. verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
  8194. "float b[1][] = {{3.f}};\n",
  8195. Alignment);
  8196. }
  8197. TEST_F(FormatTest, AlignConsecutiveDeclarations) {
  8198. FormatStyle Alignment = getLLVMStyle();
  8199. Alignment.AlignConsecutiveDeclarations = false;
  8200. verifyFormat("float const a = 5;\n"
  8201. "int oneTwoThree = 123;",
  8202. Alignment);
  8203. verifyFormat("int a = 5;\n"
  8204. "float const oneTwoThree = 123;",
  8205. Alignment);
  8206. Alignment.AlignConsecutiveDeclarations = true;
  8207. verifyFormat("float const a = 5;\n"
  8208. "int oneTwoThree = 123;",
  8209. Alignment);
  8210. verifyFormat("int a = method();\n"
  8211. "float const oneTwoThree = 133;",
  8212. Alignment);
  8213. verifyFormat("int i = 1, j = 10;\n"
  8214. "something = 2000;",
  8215. Alignment);
  8216. verifyFormat("something = 2000;\n"
  8217. "int i = 1, j = 10;\n",
  8218. Alignment);
  8219. verifyFormat("float something = 2000;\n"
  8220. "double another = 911;\n"
  8221. "int i = 1, j = 10;\n"
  8222. "const int *oneMore = 1;\n"
  8223. "unsigned i = 2;",
  8224. Alignment);
  8225. verifyFormat("float a = 5;\n"
  8226. "int one = 1;\n"
  8227. "method();\n"
  8228. "const double oneTwoThree = 123;\n"
  8229. "const unsigned int oneTwo = 12;",
  8230. Alignment);
  8231. verifyFormat("int oneTwoThree{0}; // comment\n"
  8232. "unsigned oneTwo; // comment",
  8233. Alignment);
  8234. EXPECT_EQ("float const a = 5;\n"
  8235. "\n"
  8236. "int oneTwoThree = 123;",
  8237. format("float const a = 5;\n"
  8238. "\n"
  8239. "int oneTwoThree= 123;",
  8240. Alignment));
  8241. EXPECT_EQ("float a = 5;\n"
  8242. "int one = 1;\n"
  8243. "\n"
  8244. "unsigned oneTwoThree = 123;",
  8245. format("float a = 5;\n"
  8246. "int one = 1;\n"
  8247. "\n"
  8248. "unsigned oneTwoThree = 123;",
  8249. Alignment));
  8250. EXPECT_EQ("float a = 5;\n"
  8251. "int one = 1;\n"
  8252. "\n"
  8253. "unsigned oneTwoThree = 123;\n"
  8254. "int oneTwo = 12;",
  8255. format("float a = 5;\n"
  8256. "int one = 1;\n"
  8257. "\n"
  8258. "unsigned oneTwoThree = 123;\n"
  8259. "int oneTwo = 12;",
  8260. Alignment));
  8261. Alignment.AlignConsecutiveAssignments = true;
  8262. verifyFormat("float something = 2000;\n"
  8263. "double another = 911;\n"
  8264. "int i = 1, j = 10;\n"
  8265. "const int *oneMore = 1;\n"
  8266. "unsigned i = 2;",
  8267. Alignment);
  8268. verifyFormat("int oneTwoThree = {0}; // comment\n"
  8269. "unsigned oneTwo = 0; // comment",
  8270. Alignment);
  8271. EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
  8272. " int const i = 1;\n"
  8273. " int * j = 2;\n"
  8274. " int big = 10000;\n"
  8275. "\n"
  8276. " unsigned oneTwoThree = 123;\n"
  8277. " int oneTwo = 12;\n"
  8278. " method();\n"
  8279. " float k = 2;\n"
  8280. " int ll = 10000;\n"
  8281. "}",
  8282. format("void SomeFunction(int parameter= 0) {\n"
  8283. " int const i= 1;\n"
  8284. " int *j=2;\n"
  8285. " int big = 10000;\n"
  8286. "\n"
  8287. "unsigned oneTwoThree =123;\n"
  8288. "int oneTwo = 12;\n"
  8289. " method();\n"
  8290. "float k= 2;\n"
  8291. "int ll=10000;\n"
  8292. "}",
  8293. Alignment));
  8294. Alignment.AlignConsecutiveAssignments = false;
  8295. Alignment.AlignEscapedNewlinesLeft = true;
  8296. verifyFormat("#define A \\\n"
  8297. " int aaaa = 12; \\\n"
  8298. " float b = 23; \\\n"
  8299. " const int ccc = 234; \\\n"
  8300. " unsigned dddddddddd = 2345;",
  8301. Alignment);
  8302. Alignment.AlignEscapedNewlinesLeft = false;
  8303. Alignment.ColumnLimit = 30;
  8304. verifyFormat("#define A \\\n"
  8305. " int aaaa = 12; \\\n"
  8306. " float b = 23; \\\n"
  8307. " const int ccc = 234; \\\n"
  8308. " int dddddddddd = 2345;",
  8309. Alignment);
  8310. Alignment.ColumnLimit = 80;
  8311. verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
  8312. "k = 4, int l = 5,\n"
  8313. " int m = 6) {\n"
  8314. " const int j = 10;\n"
  8315. " otherThing = 1;\n"
  8316. "}",
  8317. Alignment);
  8318. verifyFormat("void SomeFunction(int parameter = 0) {\n"
  8319. " int const i = 1;\n"
  8320. " int * j = 2;\n"
  8321. " int big = 10000;\n"
  8322. "}",
  8323. Alignment);
  8324. verifyFormat("class C {\n"
  8325. "public:\n"
  8326. " int i = 1;\n"
  8327. " virtual void f() = 0;\n"
  8328. "};",
  8329. Alignment);
  8330. verifyFormat("float i = 1;\n"
  8331. "if (SomeType t = getSomething()) {\n"
  8332. "}\n"
  8333. "const unsigned j = 2;\n"
  8334. "int big = 10000;",
  8335. Alignment);
  8336. verifyFormat("float j = 7;\n"
  8337. "for (int k = 0; k < N; ++k) {\n"
  8338. "}\n"
  8339. "unsigned j = 2;\n"
  8340. "int big = 10000;\n"
  8341. "}",
  8342. Alignment);
  8343. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  8344. verifyFormat("float i = 1;\n"
  8345. "LooooooooooongType loooooooooooooooooooooongVariable\n"
  8346. " = someLooooooooooooooooongFunction();\n"
  8347. "int j = 2;",
  8348. Alignment);
  8349. Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
  8350. verifyFormat("int i = 1;\n"
  8351. "LooooooooooongType loooooooooooooooooooooongVariable =\n"
  8352. " someLooooooooooooooooongFunction();\n"
  8353. "int j = 2;",
  8354. Alignment);
  8355. Alignment.AlignConsecutiveAssignments = true;
  8356. verifyFormat("auto lambda = []() {\n"
  8357. " auto ii = 0;\n"
  8358. " float j = 0;\n"
  8359. " return 0;\n"
  8360. "};\n"
  8361. "int i = 0;\n"
  8362. "float i2 = 0;\n"
  8363. "auto v = type{\n"
  8364. " i = 1, //\n"
  8365. " (i = 2), //\n"
  8366. " i = 3 //\n"
  8367. "};",
  8368. Alignment);
  8369. Alignment.AlignConsecutiveAssignments = false;
  8370. // FIXME: Should align all three declarations
  8371. verifyFormat(
  8372. "int i = 1;\n"
  8373. "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
  8374. " loooooooooooooooooooooongParameterB);\n"
  8375. "int j = 2;",
  8376. Alignment);
  8377. // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
  8378. // We expect declarations and assignments to align, as long as it doesn't
  8379. // exceed the column limit, starting a new alignemnt sequence whenever it
  8380. // happens.
  8381. Alignment.AlignConsecutiveAssignments = true;
  8382. Alignment.ColumnLimit = 30;
  8383. verifyFormat("float ii = 1;\n"
  8384. "unsigned j = 2;\n"
  8385. "int someVerylongVariable = 1;\n"
  8386. "AnotherLongType ll = 123456;\n"
  8387. "VeryVeryLongType k = 2;\n"
  8388. "int myvar = 1;",
  8389. Alignment);
  8390. Alignment.ColumnLimit = 80;
  8391. Alignment.AlignConsecutiveAssignments = false;
  8392. verifyFormat(
  8393. "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
  8394. " typename LongType, typename B>\n"
  8395. "auto foo() {}\n",
  8396. Alignment);
  8397. verifyFormat("float a, b = 1;\n"
  8398. "int c = 2;\n"
  8399. "int dd = 3;\n",
  8400. Alignment);
  8401. verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
  8402. "float b[1][] = {{3.f}};\n",
  8403. Alignment);
  8404. Alignment.AlignConsecutiveAssignments = true;
  8405. verifyFormat("float a, b = 1;\n"
  8406. "int c = 2;\n"
  8407. "int dd = 3;\n",
  8408. Alignment);
  8409. verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
  8410. "float b[1][] = {{3.f}};\n",
  8411. Alignment);
  8412. Alignment.AlignConsecutiveAssignments = false;
  8413. Alignment.ColumnLimit = 30;
  8414. Alignment.BinPackParameters = false;
  8415. verifyFormat("void foo(float a,\n"
  8416. " float b,\n"
  8417. " int c,\n"
  8418. " uint32_t *d) {\n"
  8419. " int * e = 0;\n"
  8420. " float f = 0;\n"
  8421. " double g = 0;\n"
  8422. "}\n"
  8423. "void bar(ino_t a,\n"
  8424. " int b,\n"
  8425. " uint32_t *c,\n"
  8426. " bool d) {}\n",
  8427. Alignment);
  8428. Alignment.BinPackParameters = true;
  8429. Alignment.ColumnLimit = 80;
  8430. }
  8431. TEST_F(FormatTest, LinuxBraceBreaking) {
  8432. FormatStyle LinuxBraceStyle = getLLVMStyle();
  8433. LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
  8434. verifyFormat("namespace a\n"
  8435. "{\n"
  8436. "class A\n"
  8437. "{\n"
  8438. " void f()\n"
  8439. " {\n"
  8440. " if (true) {\n"
  8441. " a();\n"
  8442. " b();\n"
  8443. " } else {\n"
  8444. " a();\n"
  8445. " }\n"
  8446. " }\n"
  8447. " void g() { return; }\n"
  8448. "};\n"
  8449. "struct B {\n"
  8450. " int x;\n"
  8451. "};\n"
  8452. "}\n",
  8453. LinuxBraceStyle);
  8454. verifyFormat("enum X {\n"
  8455. " Y = 0,\n"
  8456. "}\n",
  8457. LinuxBraceStyle);
  8458. verifyFormat("struct S {\n"
  8459. " int Type;\n"
  8460. " union {\n"
  8461. " int x;\n"
  8462. " double y;\n"
  8463. " } Value;\n"
  8464. " class C\n"
  8465. " {\n"
  8466. " MyFavoriteType Value;\n"
  8467. " } Class;\n"
  8468. "}\n",
  8469. LinuxBraceStyle);
  8470. }
  8471. TEST_F(FormatTest, MozillaBraceBreaking) {
  8472. FormatStyle MozillaBraceStyle = getLLVMStyle();
  8473. MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
  8474. verifyFormat("namespace a {\n"
  8475. "class A\n"
  8476. "{\n"
  8477. " void f()\n"
  8478. " {\n"
  8479. " if (true) {\n"
  8480. " a();\n"
  8481. " b();\n"
  8482. " }\n"
  8483. " }\n"
  8484. " void g() { return; }\n"
  8485. "};\n"
  8486. "enum E\n"
  8487. "{\n"
  8488. " A,\n"
  8489. " // foo\n"
  8490. " B,\n"
  8491. " C\n"
  8492. "};\n"
  8493. "struct B\n"
  8494. "{\n"
  8495. " int x;\n"
  8496. "};\n"
  8497. "}\n",
  8498. MozillaBraceStyle);
  8499. verifyFormat("struct S\n"
  8500. "{\n"
  8501. " int Type;\n"
  8502. " union\n"
  8503. " {\n"
  8504. " int x;\n"
  8505. " double y;\n"
  8506. " } Value;\n"
  8507. " class C\n"
  8508. " {\n"
  8509. " MyFavoriteType Value;\n"
  8510. " } Class;\n"
  8511. "}\n",
  8512. MozillaBraceStyle);
  8513. }
  8514. TEST_F(FormatTest, StroustrupBraceBreaking) {
  8515. FormatStyle StroustrupBraceStyle = getLLVMStyle();
  8516. StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  8517. verifyFormat("namespace a {\n"
  8518. "class A {\n"
  8519. " void f()\n"
  8520. " {\n"
  8521. " if (true) {\n"
  8522. " a();\n"
  8523. " b();\n"
  8524. " }\n"
  8525. " }\n"
  8526. " void g() { return; }\n"
  8527. "};\n"
  8528. "struct B {\n"
  8529. " int x;\n"
  8530. "};\n"
  8531. "}\n",
  8532. StroustrupBraceStyle);
  8533. verifyFormat("void foo()\n"
  8534. "{\n"
  8535. " if (a) {\n"
  8536. " a();\n"
  8537. " }\n"
  8538. " else {\n"
  8539. " b();\n"
  8540. " }\n"
  8541. "}\n",
  8542. StroustrupBraceStyle);
  8543. verifyFormat("#ifdef _DEBUG\n"
  8544. "int foo(int i = 0)\n"
  8545. "#else\n"
  8546. "int foo(int i = 5)\n"
  8547. "#endif\n"
  8548. "{\n"
  8549. " return i;\n"
  8550. "}",
  8551. StroustrupBraceStyle);
  8552. verifyFormat("void foo() {}\n"
  8553. "void bar()\n"
  8554. "#ifdef _DEBUG\n"
  8555. "{\n"
  8556. " foo();\n"
  8557. "}\n"
  8558. "#else\n"
  8559. "{\n"
  8560. "}\n"
  8561. "#endif",
  8562. StroustrupBraceStyle);
  8563. verifyFormat("void foobar() { int i = 5; }\n"
  8564. "#ifdef _DEBUG\n"
  8565. "void bar() {}\n"
  8566. "#else\n"
  8567. "void bar() { foobar(); }\n"
  8568. "#endif",
  8569. StroustrupBraceStyle);
  8570. }
  8571. TEST_F(FormatTest, AllmanBraceBreaking) {
  8572. FormatStyle AllmanBraceStyle = getLLVMStyle();
  8573. AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
  8574. verifyFormat("namespace a\n"
  8575. "{\n"
  8576. "class A\n"
  8577. "{\n"
  8578. " void f()\n"
  8579. " {\n"
  8580. " if (true)\n"
  8581. " {\n"
  8582. " a();\n"
  8583. " b();\n"
  8584. " }\n"
  8585. " }\n"
  8586. " void g() { return; }\n"
  8587. "};\n"
  8588. "struct B\n"
  8589. "{\n"
  8590. " int x;\n"
  8591. "};\n"
  8592. "}",
  8593. AllmanBraceStyle);
  8594. verifyFormat("void f()\n"
  8595. "{\n"
  8596. " if (true)\n"
  8597. " {\n"
  8598. " a();\n"
  8599. " }\n"
  8600. " else if (false)\n"
  8601. " {\n"
  8602. " b();\n"
  8603. " }\n"
  8604. " else\n"
  8605. " {\n"
  8606. " c();\n"
  8607. " }\n"
  8608. "}\n",
  8609. AllmanBraceStyle);
  8610. verifyFormat("void f()\n"
  8611. "{\n"
  8612. " for (int i = 0; i < 10; ++i)\n"
  8613. " {\n"
  8614. " a();\n"
  8615. " }\n"
  8616. " while (false)\n"
  8617. " {\n"
  8618. " b();\n"
  8619. " }\n"
  8620. " do\n"
  8621. " {\n"
  8622. " c();\n"
  8623. " } while (false)\n"
  8624. "}\n",
  8625. AllmanBraceStyle);
  8626. verifyFormat("void f(int a)\n"
  8627. "{\n"
  8628. " switch (a)\n"
  8629. " {\n"
  8630. " case 0:\n"
  8631. " break;\n"
  8632. " case 1:\n"
  8633. " {\n"
  8634. " break;\n"
  8635. " }\n"
  8636. " case 2:\n"
  8637. " {\n"
  8638. " }\n"
  8639. " break;\n"
  8640. " default:\n"
  8641. " break;\n"
  8642. " }\n"
  8643. "}\n",
  8644. AllmanBraceStyle);
  8645. verifyFormat("enum X\n"
  8646. "{\n"
  8647. " Y = 0,\n"
  8648. "}\n",
  8649. AllmanBraceStyle);
  8650. verifyFormat("enum X\n"
  8651. "{\n"
  8652. " Y = 0\n"
  8653. "}\n",
  8654. AllmanBraceStyle);
  8655. verifyFormat("@interface BSApplicationController ()\n"
  8656. "{\n"
  8657. "@private\n"
  8658. " id _extraIvar;\n"
  8659. "}\n"
  8660. "@end\n",
  8661. AllmanBraceStyle);
  8662. verifyFormat("#ifdef _DEBUG\n"
  8663. "int foo(int i = 0)\n"
  8664. "#else\n"
  8665. "int foo(int i = 5)\n"
  8666. "#endif\n"
  8667. "{\n"
  8668. " return i;\n"
  8669. "}",
  8670. AllmanBraceStyle);
  8671. verifyFormat("void foo() {}\n"
  8672. "void bar()\n"
  8673. "#ifdef _DEBUG\n"
  8674. "{\n"
  8675. " foo();\n"
  8676. "}\n"
  8677. "#else\n"
  8678. "{\n"
  8679. "}\n"
  8680. "#endif",
  8681. AllmanBraceStyle);
  8682. verifyFormat("void foobar() { int i = 5; }\n"
  8683. "#ifdef _DEBUG\n"
  8684. "void bar() {}\n"
  8685. "#else\n"
  8686. "void bar() { foobar(); }\n"
  8687. "#endif",
  8688. AllmanBraceStyle);
  8689. // This shouldn't affect ObjC blocks..
  8690. verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
  8691. " // ...\n"
  8692. " int i;\n"
  8693. "}];",
  8694. AllmanBraceStyle);
  8695. verifyFormat("void (^block)(void) = ^{\n"
  8696. " // ...\n"
  8697. " int i;\n"
  8698. "};",
  8699. AllmanBraceStyle);
  8700. // .. or dict literals.
  8701. verifyFormat("void f()\n"
  8702. "{\n"
  8703. " [object someMethod:@{ @\"a\" : @\"b\" }];\n"
  8704. "}",
  8705. AllmanBraceStyle);
  8706. verifyFormat("int f()\n"
  8707. "{ // comment\n"
  8708. " return 42;\n"
  8709. "}",
  8710. AllmanBraceStyle);
  8711. AllmanBraceStyle.ColumnLimit = 19;
  8712. verifyFormat("void f() { int i; }", AllmanBraceStyle);
  8713. AllmanBraceStyle.ColumnLimit = 18;
  8714. verifyFormat("void f()\n"
  8715. "{\n"
  8716. " int i;\n"
  8717. "}",
  8718. AllmanBraceStyle);
  8719. AllmanBraceStyle.ColumnLimit = 80;
  8720. FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
  8721. BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
  8722. BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
  8723. verifyFormat("void f(bool b)\n"
  8724. "{\n"
  8725. " if (b)\n"
  8726. " {\n"
  8727. " return;\n"
  8728. " }\n"
  8729. "}\n",
  8730. BreakBeforeBraceShortIfs);
  8731. verifyFormat("void f(bool b)\n"
  8732. "{\n"
  8733. " if (b) return;\n"
  8734. "}\n",
  8735. BreakBeforeBraceShortIfs);
  8736. verifyFormat("void f(bool b)\n"
  8737. "{\n"
  8738. " while (b)\n"
  8739. " {\n"
  8740. " return;\n"
  8741. " }\n"
  8742. "}\n",
  8743. BreakBeforeBraceShortIfs);
  8744. }
  8745. TEST_F(FormatTest, GNUBraceBreaking) {
  8746. FormatStyle GNUBraceStyle = getLLVMStyle();
  8747. GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
  8748. verifyFormat("namespace a\n"
  8749. "{\n"
  8750. "class A\n"
  8751. "{\n"
  8752. " void f()\n"
  8753. " {\n"
  8754. " int a;\n"
  8755. " {\n"
  8756. " int b;\n"
  8757. " }\n"
  8758. " if (true)\n"
  8759. " {\n"
  8760. " a();\n"
  8761. " b();\n"
  8762. " }\n"
  8763. " }\n"
  8764. " void g() { return; }\n"
  8765. "}\n"
  8766. "}",
  8767. GNUBraceStyle);
  8768. verifyFormat("void f()\n"
  8769. "{\n"
  8770. " if (true)\n"
  8771. " {\n"
  8772. " a();\n"
  8773. " }\n"
  8774. " else if (false)\n"
  8775. " {\n"
  8776. " b();\n"
  8777. " }\n"
  8778. " else\n"
  8779. " {\n"
  8780. " c();\n"
  8781. " }\n"
  8782. "}\n",
  8783. GNUBraceStyle);
  8784. verifyFormat("void f()\n"
  8785. "{\n"
  8786. " for (int i = 0; i < 10; ++i)\n"
  8787. " {\n"
  8788. " a();\n"
  8789. " }\n"
  8790. " while (false)\n"
  8791. " {\n"
  8792. " b();\n"
  8793. " }\n"
  8794. " do\n"
  8795. " {\n"
  8796. " c();\n"
  8797. " }\n"
  8798. " while (false);\n"
  8799. "}\n",
  8800. GNUBraceStyle);
  8801. verifyFormat("void f(int a)\n"
  8802. "{\n"
  8803. " switch (a)\n"
  8804. " {\n"
  8805. " case 0:\n"
  8806. " break;\n"
  8807. " case 1:\n"
  8808. " {\n"
  8809. " break;\n"
  8810. " }\n"
  8811. " case 2:\n"
  8812. " {\n"
  8813. " }\n"
  8814. " break;\n"
  8815. " default:\n"
  8816. " break;\n"
  8817. " }\n"
  8818. "}\n",
  8819. GNUBraceStyle);
  8820. verifyFormat("enum X\n"
  8821. "{\n"
  8822. " Y = 0,\n"
  8823. "}\n",
  8824. GNUBraceStyle);
  8825. verifyFormat("@interface BSApplicationController ()\n"
  8826. "{\n"
  8827. "@private\n"
  8828. " id _extraIvar;\n"
  8829. "}\n"
  8830. "@end\n",
  8831. GNUBraceStyle);
  8832. verifyFormat("#ifdef _DEBUG\n"
  8833. "int foo(int i = 0)\n"
  8834. "#else\n"
  8835. "int foo(int i = 5)\n"
  8836. "#endif\n"
  8837. "{\n"
  8838. " return i;\n"
  8839. "}",
  8840. GNUBraceStyle);
  8841. verifyFormat("void foo() {}\n"
  8842. "void bar()\n"
  8843. "#ifdef _DEBUG\n"
  8844. "{\n"
  8845. " foo();\n"
  8846. "}\n"
  8847. "#else\n"
  8848. "{\n"
  8849. "}\n"
  8850. "#endif",
  8851. GNUBraceStyle);
  8852. verifyFormat("void foobar() { int i = 5; }\n"
  8853. "#ifdef _DEBUG\n"
  8854. "void bar() {}\n"
  8855. "#else\n"
  8856. "void bar() { foobar(); }\n"
  8857. "#endif",
  8858. GNUBraceStyle);
  8859. }
  8860. TEST_F(FormatTest, WebKitBraceBreaking) {
  8861. FormatStyle WebKitBraceStyle = getLLVMStyle();
  8862. WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
  8863. verifyFormat("namespace a {\n"
  8864. "class A {\n"
  8865. " void f()\n"
  8866. " {\n"
  8867. " if (true) {\n"
  8868. " a();\n"
  8869. " b();\n"
  8870. " }\n"
  8871. " }\n"
  8872. " void g() { return; }\n"
  8873. "};\n"
  8874. "enum E {\n"
  8875. " A,\n"
  8876. " // foo\n"
  8877. " B,\n"
  8878. " C\n"
  8879. "};\n"
  8880. "struct B {\n"
  8881. " int x;\n"
  8882. "};\n"
  8883. "}\n",
  8884. WebKitBraceStyle);
  8885. verifyFormat("struct S {\n"
  8886. " int Type;\n"
  8887. " union {\n"
  8888. " int x;\n"
  8889. " double y;\n"
  8890. " } Value;\n"
  8891. " class C {\n"
  8892. " MyFavoriteType Value;\n"
  8893. " } Class;\n"
  8894. "};\n",
  8895. WebKitBraceStyle);
  8896. }
  8897. TEST_F(FormatTest, CatchExceptionReferenceBinding) {
  8898. verifyFormat("void f() {\n"
  8899. " try {\n"
  8900. " } catch (const Exception &e) {\n"
  8901. " }\n"
  8902. "}\n",
  8903. getLLVMStyle());
  8904. }
  8905. TEST_F(FormatTest, UnderstandsPragmas) {
  8906. verifyFormat("#pragma omp reduction(| : var)");
  8907. verifyFormat("#pragma omp reduction(+ : var)");
  8908. EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
  8909. "(including parentheses).",
  8910. format("#pragma mark Any non-hyphenated or hyphenated string "
  8911. "(including parentheses)."));
  8912. }
  8913. TEST_F(FormatTest, UnderstandPragmaOption) {
  8914. verifyFormat("#pragma option -C -A");
  8915. EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A"));
  8916. }
  8917. #define EXPECT_ALL_STYLES_EQUAL(Styles) \
  8918. for (size_t i = 1; i < Styles.size(); ++i) \
  8919. EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
  8920. << " differs from Style #0"
  8921. TEST_F(FormatTest, GetsPredefinedStyleByName) {
  8922. SmallVector<FormatStyle, 3> Styles;
  8923. Styles.resize(3);
  8924. Styles[0] = getLLVMStyle();
  8925. EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
  8926. EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
  8927. EXPECT_ALL_STYLES_EQUAL(Styles);
  8928. Styles[0] = getGoogleStyle();
  8929. EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
  8930. EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
  8931. EXPECT_ALL_STYLES_EQUAL(Styles);
  8932. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  8933. EXPECT_TRUE(
  8934. getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
  8935. EXPECT_TRUE(
  8936. getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
  8937. EXPECT_ALL_STYLES_EQUAL(Styles);
  8938. Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
  8939. EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
  8940. EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
  8941. EXPECT_ALL_STYLES_EQUAL(Styles);
  8942. Styles[0] = getMozillaStyle();
  8943. EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
  8944. EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
  8945. EXPECT_ALL_STYLES_EQUAL(Styles);
  8946. Styles[0] = getWebKitStyle();
  8947. EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
  8948. EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
  8949. EXPECT_ALL_STYLES_EQUAL(Styles);
  8950. Styles[0] = getGNUStyle();
  8951. EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
  8952. EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
  8953. EXPECT_ALL_STYLES_EQUAL(Styles);
  8954. EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
  8955. }
  8956. TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
  8957. SmallVector<FormatStyle, 8> Styles;
  8958. Styles.resize(2);
  8959. Styles[0] = getGoogleStyle();
  8960. Styles[1] = getLLVMStyle();
  8961. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  8962. EXPECT_ALL_STYLES_EQUAL(Styles);
  8963. Styles.resize(5);
  8964. Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
  8965. Styles[1] = getLLVMStyle();
  8966. Styles[1].Language = FormatStyle::LK_JavaScript;
  8967. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
  8968. Styles[2] = getLLVMStyle();
  8969. Styles[2].Language = FormatStyle::LK_JavaScript;
  8970. EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
  8971. "BasedOnStyle: Google",
  8972. &Styles[2])
  8973. .value());
  8974. Styles[3] = getLLVMStyle();
  8975. Styles[3].Language = FormatStyle::LK_JavaScript;
  8976. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
  8977. "Language: JavaScript",
  8978. &Styles[3])
  8979. .value());
  8980. Styles[4] = getLLVMStyle();
  8981. Styles[4].Language = FormatStyle::LK_JavaScript;
  8982. EXPECT_EQ(0, parseConfiguration("---\n"
  8983. "BasedOnStyle: LLVM\n"
  8984. "IndentWidth: 123\n"
  8985. "---\n"
  8986. "BasedOnStyle: Google\n"
  8987. "Language: JavaScript",
  8988. &Styles[4])
  8989. .value());
  8990. EXPECT_ALL_STYLES_EQUAL(Styles);
  8991. }
  8992. #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \
  8993. Style.FIELD = false; \
  8994. EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \
  8995. EXPECT_TRUE(Style.FIELD); \
  8996. EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \
  8997. EXPECT_FALSE(Style.FIELD);
  8998. #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
  8999. #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \
  9000. Style.STRUCT.FIELD = false; \
  9001. EXPECT_EQ(0, \
  9002. parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \
  9003. .value()); \
  9004. EXPECT_TRUE(Style.STRUCT.FIELD); \
  9005. EXPECT_EQ(0, \
  9006. parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \
  9007. .value()); \
  9008. EXPECT_FALSE(Style.STRUCT.FIELD);
  9009. #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \
  9010. CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
  9011. #define CHECK_PARSE(TEXT, FIELD, VALUE) \
  9012. EXPECT_NE(VALUE, Style.FIELD); \
  9013. EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \
  9014. EXPECT_EQ(VALUE, Style.FIELD)
  9015. TEST_F(FormatTest, ParsesConfigurationBools) {
  9016. FormatStyle Style = {};
  9017. Style.Language = FormatStyle::LK_Cpp;
  9018. CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
  9019. CHECK_PARSE_BOOL(AlignOperands);
  9020. CHECK_PARSE_BOOL(AlignTrailingComments);
  9021. CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
  9022. CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
  9023. CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
  9024. CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
  9025. CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
  9026. CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
  9027. CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
  9028. CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
  9029. CHECK_PARSE_BOOL(BinPackArguments);
  9030. CHECK_PARSE_BOOL(BinPackParameters);
  9031. CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
  9032. CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
  9033. CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
  9034. CHECK_PARSE_BOOL(DerivePointerAlignment);
  9035. CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
  9036. CHECK_PARSE_BOOL(DisableFormat);
  9037. CHECK_PARSE_BOOL(IndentCaseLabels);
  9038. CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
  9039. CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
  9040. CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
  9041. CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
  9042. CHECK_PARSE_BOOL(Cpp11BracedListStyle);
  9043. CHECK_PARSE_BOOL(ReflowComments);
  9044. CHECK_PARSE_BOOL(SortIncludes);
  9045. CHECK_PARSE_BOOL(SpacesInParentheses);
  9046. CHECK_PARSE_BOOL(SpacesInSquareBrackets);
  9047. CHECK_PARSE_BOOL(SpacesInAngles);
  9048. CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
  9049. CHECK_PARSE_BOOL(SpacesInContainerLiterals);
  9050. CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
  9051. CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
  9052. CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
  9053. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
  9054. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
  9055. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
  9056. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
  9057. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
  9058. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
  9059. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
  9060. CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
  9061. CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
  9062. CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
  9063. CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
  9064. }
  9065. #undef CHECK_PARSE_BOOL
  9066. TEST_F(FormatTest, ParsesConfiguration) {
  9067. FormatStyle Style = {};
  9068. Style.Language = FormatStyle::LK_Cpp;
  9069. CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
  9070. CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
  9071. ConstructorInitializerIndentWidth, 1234u);
  9072. CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
  9073. CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
  9074. CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
  9075. CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
  9076. PenaltyBreakBeforeFirstCallParameter, 1234u);
  9077. CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
  9078. CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
  9079. PenaltyReturnTypeOnItsOwnLine, 1234u);
  9080. CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
  9081. SpacesBeforeTrailingComments, 1234u);
  9082. CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
  9083. CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
  9084. Style.PointerAlignment = FormatStyle::PAS_Middle;
  9085. CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
  9086. FormatStyle::PAS_Left);
  9087. CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
  9088. FormatStyle::PAS_Right);
  9089. CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
  9090. FormatStyle::PAS_Middle);
  9091. // For backward compatibility:
  9092. CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
  9093. FormatStyle::PAS_Left);
  9094. CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
  9095. FormatStyle::PAS_Right);
  9096. CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
  9097. FormatStyle::PAS_Middle);
  9098. Style.Standard = FormatStyle::LS_Auto;
  9099. CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
  9100. CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
  9101. CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
  9102. CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
  9103. CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
  9104. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  9105. CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
  9106. BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
  9107. CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
  9108. FormatStyle::BOS_None);
  9109. CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
  9110. FormatStyle::BOS_All);
  9111. // For backward compatibility:
  9112. CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
  9113. FormatStyle::BOS_None);
  9114. CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
  9115. FormatStyle::BOS_All);
  9116. Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
  9117. CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
  9118. FormatStyle::BAS_Align);
  9119. CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
  9120. FormatStyle::BAS_DontAlign);
  9121. CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
  9122. FormatStyle::BAS_AlwaysBreak);
  9123. // For backward compatibility:
  9124. CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
  9125. FormatStyle::BAS_DontAlign);
  9126. CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
  9127. FormatStyle::BAS_Align);
  9128. Style.UseTab = FormatStyle::UT_ForIndentation;
  9129. CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
  9130. CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
  9131. CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
  9132. // For backward compatibility:
  9133. CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
  9134. CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
  9135. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  9136. CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
  9137. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
  9138. CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
  9139. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
  9140. CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
  9141. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
  9142. CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
  9143. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
  9144. // For backward compatibility:
  9145. CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
  9146. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
  9147. CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
  9148. AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
  9149. Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
  9150. CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
  9151. FormatStyle::SBPO_Never);
  9152. CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
  9153. FormatStyle::SBPO_Always);
  9154. CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
  9155. FormatStyle::SBPO_ControlStatements);
  9156. // For backward compatibility:
  9157. CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
  9158. FormatStyle::SBPO_Never);
  9159. CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
  9160. FormatStyle::SBPO_ControlStatements);
  9161. Style.ColumnLimit = 123;
  9162. FormatStyle BaseStyle = getLLVMStyle();
  9163. CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
  9164. CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
  9165. Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
  9166. CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
  9167. FormatStyle::BS_Attach);
  9168. CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
  9169. FormatStyle::BS_Linux);
  9170. CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
  9171. FormatStyle::BS_Mozilla);
  9172. CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
  9173. FormatStyle::BS_Stroustrup);
  9174. CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
  9175. FormatStyle::BS_Allman);
  9176. CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
  9177. CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
  9178. FormatStyle::BS_WebKit);
  9179. CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
  9180. FormatStyle::BS_Custom);
  9181. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
  9182. CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
  9183. FormatStyle::RTBS_None);
  9184. CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
  9185. FormatStyle::RTBS_All);
  9186. CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
  9187. AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
  9188. CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
  9189. AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
  9190. CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
  9191. AlwaysBreakAfterReturnType,
  9192. FormatStyle::RTBS_TopLevelDefinitions);
  9193. Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
  9194. CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
  9195. AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
  9196. CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
  9197. AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
  9198. CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
  9199. AlwaysBreakAfterDefinitionReturnType,
  9200. FormatStyle::DRTBS_TopLevel);
  9201. Style.NamespaceIndentation = FormatStyle::NI_All;
  9202. CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
  9203. FormatStyle::NI_None);
  9204. CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
  9205. FormatStyle::NI_Inner);
  9206. CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
  9207. FormatStyle::NI_All);
  9208. // FIXME: This is required because parsing a configuration simply overwrites
  9209. // the first N elements of the list instead of resetting it.
  9210. Style.ForEachMacros.clear();
  9211. std::vector<std::string> BoostForeach;
  9212. BoostForeach.push_back("BOOST_FOREACH");
  9213. CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
  9214. std::vector<std::string> BoostAndQForeach;
  9215. BoostAndQForeach.push_back("BOOST_FOREACH");
  9216. BoostAndQForeach.push_back("Q_FOREACH");
  9217. CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
  9218. BoostAndQForeach);
  9219. Style.IncludeCategories.clear();
  9220. std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2},
  9221. {".*", 1}};
  9222. CHECK_PARSE("IncludeCategories:\n"
  9223. " - Regex: abc/.*\n"
  9224. " Priority: 2\n"
  9225. " - Regex: .*\n"
  9226. " Priority: 1",
  9227. IncludeCategories, ExpectedCategories);
  9228. }
  9229. TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
  9230. FormatStyle Style = {};
  9231. Style.Language = FormatStyle::LK_Cpp;
  9232. CHECK_PARSE("Language: Cpp\n"
  9233. "IndentWidth: 12",
  9234. IndentWidth, 12u);
  9235. EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
  9236. "IndentWidth: 34",
  9237. &Style),
  9238. ParseError::Unsuitable);
  9239. EXPECT_EQ(12u, Style.IndentWidth);
  9240. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  9241. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  9242. Style.Language = FormatStyle::LK_JavaScript;
  9243. CHECK_PARSE("Language: JavaScript\n"
  9244. "IndentWidth: 12",
  9245. IndentWidth, 12u);
  9246. CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
  9247. EXPECT_EQ(parseConfiguration("Language: Cpp\n"
  9248. "IndentWidth: 34",
  9249. &Style),
  9250. ParseError::Unsuitable);
  9251. EXPECT_EQ(23u, Style.IndentWidth);
  9252. CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
  9253. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  9254. CHECK_PARSE("BasedOnStyle: LLVM\n"
  9255. "IndentWidth: 67",
  9256. IndentWidth, 67u);
  9257. CHECK_PARSE("---\n"
  9258. "Language: JavaScript\n"
  9259. "IndentWidth: 12\n"
  9260. "---\n"
  9261. "Language: Cpp\n"
  9262. "IndentWidth: 34\n"
  9263. "...\n",
  9264. IndentWidth, 12u);
  9265. Style.Language = FormatStyle::LK_Cpp;
  9266. CHECK_PARSE("---\n"
  9267. "Language: JavaScript\n"
  9268. "IndentWidth: 12\n"
  9269. "---\n"
  9270. "Language: Cpp\n"
  9271. "IndentWidth: 34\n"
  9272. "...\n",
  9273. IndentWidth, 34u);
  9274. CHECK_PARSE("---\n"
  9275. "IndentWidth: 78\n"
  9276. "---\n"
  9277. "Language: JavaScript\n"
  9278. "IndentWidth: 56\n"
  9279. "...\n",
  9280. IndentWidth, 78u);
  9281. Style.ColumnLimit = 123;
  9282. Style.IndentWidth = 234;
  9283. Style.BreakBeforeBraces = FormatStyle::BS_Linux;
  9284. Style.TabWidth = 345;
  9285. EXPECT_FALSE(parseConfiguration("---\n"
  9286. "IndentWidth: 456\n"
  9287. "BreakBeforeBraces: Allman\n"
  9288. "---\n"
  9289. "Language: JavaScript\n"
  9290. "IndentWidth: 111\n"
  9291. "TabWidth: 111\n"
  9292. "---\n"
  9293. "Language: Cpp\n"
  9294. "BreakBeforeBraces: Stroustrup\n"
  9295. "TabWidth: 789\n"
  9296. "...\n",
  9297. &Style));
  9298. EXPECT_EQ(123u, Style.ColumnLimit);
  9299. EXPECT_EQ(456u, Style.IndentWidth);
  9300. EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
  9301. EXPECT_EQ(789u, Style.TabWidth);
  9302. EXPECT_EQ(parseConfiguration("---\n"
  9303. "Language: JavaScript\n"
  9304. "IndentWidth: 56\n"
  9305. "---\n"
  9306. "IndentWidth: 78\n"
  9307. "...\n",
  9308. &Style),
  9309. ParseError::Error);
  9310. EXPECT_EQ(parseConfiguration("---\n"
  9311. "Language: JavaScript\n"
  9312. "IndentWidth: 56\n"
  9313. "---\n"
  9314. "Language: JavaScript\n"
  9315. "IndentWidth: 78\n"
  9316. "...\n",
  9317. &Style),
  9318. ParseError::Error);
  9319. EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
  9320. }
  9321. #undef CHECK_PARSE
  9322. TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
  9323. FormatStyle Style = {};
  9324. Style.Language = FormatStyle::LK_JavaScript;
  9325. Style.BreakBeforeTernaryOperators = true;
  9326. EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
  9327. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  9328. Style.BreakBeforeTernaryOperators = true;
  9329. EXPECT_EQ(0, parseConfiguration("---\n"
  9330. "BasedOnStyle: Google\n"
  9331. "---\n"
  9332. "Language: JavaScript\n"
  9333. "IndentWidth: 76\n"
  9334. "...\n",
  9335. &Style)
  9336. .value());
  9337. EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
  9338. EXPECT_EQ(76u, Style.IndentWidth);
  9339. EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
  9340. }
  9341. TEST_F(FormatTest, ConfigurationRoundTripTest) {
  9342. FormatStyle Style = getLLVMStyle();
  9343. std::string YAML = configurationAsText(Style);
  9344. FormatStyle ParsedStyle = {};
  9345. ParsedStyle.Language = FormatStyle::LK_Cpp;
  9346. EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
  9347. EXPECT_EQ(Style, ParsedStyle);
  9348. }
  9349. TEST_F(FormatTest, WorksFor8bitEncodings) {
  9350. EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
  9351. "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
  9352. "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
  9353. "\"\xef\xee\xf0\xf3...\"",
  9354. format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
  9355. "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
  9356. "\xef\xee\xf0\xf3...\"",
  9357. getLLVMStyleWithColumns(12)));
  9358. }
  9359. TEST_F(FormatTest, HandlesUTF8BOM) {
  9360. EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
  9361. EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
  9362. format("\xef\xbb\xbf#include <iostream>"));
  9363. EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
  9364. format("\xef\xbb\xbf\n#include <iostream>"));
  9365. }
  9366. // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
  9367. #if !defined(_MSC_VER)
  9368. TEST_F(FormatTest, CountsUTF8CharactersProperly) {
  9369. verifyFormat("\"Однажды в студёную зимнюю пору...\"",
  9370. getLLVMStyleWithColumns(35));
  9371. verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
  9372. getLLVMStyleWithColumns(31));
  9373. verifyFormat("// Однажды в студёную зимнюю пору...",
  9374. getLLVMStyleWithColumns(36));
  9375. verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
  9376. verifyFormat("/* Однажды в студёную зимнюю пору... */",
  9377. getLLVMStyleWithColumns(39));
  9378. verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
  9379. getLLVMStyleWithColumns(35));
  9380. }
  9381. TEST_F(FormatTest, SplitsUTF8Strings) {
  9382. // Non-printable characters' width is currently considered to be the length in
  9383. // bytes in UTF8. The characters can be displayed in very different manner
  9384. // (zero-width, single width with a substitution glyph, expanded to their code
  9385. // (e.g. "<8d>"), so there's no single correct way to handle them.
  9386. EXPECT_EQ("\"aaaaÄ\"\n"
  9387. "\"\xc2\x8d\";",
  9388. format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  9389. EXPECT_EQ("\"aaaaaaaÄ\"\n"
  9390. "\"\xc2\x8d\";",
  9391. format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
  9392. EXPECT_EQ("\"Однажды, в \"\n"
  9393. "\"студёную \"\n"
  9394. "\"зимнюю \"\n"
  9395. "\"пору,\"",
  9396. format("\"Однажды, в студёную зимнюю пору,\"",
  9397. getLLVMStyleWithColumns(13)));
  9398. EXPECT_EQ(
  9399. "\"一 二 三 \"\n"
  9400. "\"四 五六 \"\n"
  9401. "\"七 八 九 \"\n"
  9402. "\"十\"",
  9403. format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
  9404. EXPECT_EQ("\"一\t二 \"\n"
  9405. "\"\t三 \"\n"
  9406. "\"四 五\t六 \"\n"
  9407. "\"\t七 \"\n"
  9408. "\"八九十\tqq\"",
  9409. format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
  9410. getLLVMStyleWithColumns(11)));
  9411. // UTF8 character in an escape sequence.
  9412. EXPECT_EQ("\"aaaaaa\"\n"
  9413. "\"\\\xC2\x8D\"",
  9414. format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
  9415. }
  9416. TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
  9417. EXPECT_EQ("const char *sssss =\n"
  9418. " \"一二三四五六七八\\\n"
  9419. " 九 十\";",
  9420. format("const char *sssss = \"一二三四五六七八\\\n"
  9421. " 九 十\";",
  9422. getLLVMStyleWithColumns(30)));
  9423. }
  9424. TEST_F(FormatTest, SplitsUTF8LineComments) {
  9425. EXPECT_EQ("// aaaaÄ\xc2\x8d",
  9426. format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
  9427. EXPECT_EQ("// Я из лесу\n"
  9428. "// вышел; был\n"
  9429. "// сильный\n"
  9430. "// мороз.",
  9431. format("// Я из лесу вышел; был сильный мороз.",
  9432. getLLVMStyleWithColumns(13)));
  9433. EXPECT_EQ("// 一二三\n"
  9434. "// 四五六七\n"
  9435. "// 八 九\n"
  9436. "// 十",
  9437. format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9)));
  9438. }
  9439. TEST_F(FormatTest, SplitsUTF8BlockComments) {
  9440. EXPECT_EQ("/* Гляжу,\n"
  9441. " * поднимается\n"
  9442. " * медленно в\n"
  9443. " * гору\n"
  9444. " * Лошадка,\n"
  9445. " * везущая\n"
  9446. " * хворосту\n"
  9447. " * воз. */",
  9448. format("/* Гляжу, поднимается медленно в гору\n"
  9449. " * Лошадка, везущая хворосту воз. */",
  9450. getLLVMStyleWithColumns(13)));
  9451. EXPECT_EQ(
  9452. "/* 一二三\n"
  9453. " * 四五六七\n"
  9454. " * 八 九\n"
  9455. " * 十 */",
  9456. format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9)));
  9457. EXPECT_EQ("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
  9458. " * 𝕓𝕪𝕥𝕖\n"
  9459. " * 𝖀𝕿𝕱-𝟠 */",
  9460. format("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12)));
  9461. }
  9462. #endif // _MSC_VER
  9463. TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
  9464. FormatStyle Style = getLLVMStyle();
  9465. Style.ConstructorInitializerIndentWidth = 4;
  9466. verifyFormat(
  9467. "SomeClass::Constructor()\n"
  9468. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  9469. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  9470. Style);
  9471. Style.ConstructorInitializerIndentWidth = 2;
  9472. verifyFormat(
  9473. "SomeClass::Constructor()\n"
  9474. " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  9475. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  9476. Style);
  9477. Style.ConstructorInitializerIndentWidth = 0;
  9478. verifyFormat(
  9479. "SomeClass::Constructor()\n"
  9480. ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
  9481. " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
  9482. Style);
  9483. }
  9484. TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
  9485. FormatStyle Style = getLLVMStyle();
  9486. Style.BreakConstructorInitializersBeforeComma = true;
  9487. Style.ConstructorInitializerIndentWidth = 4;
  9488. verifyFormat("SomeClass::Constructor()\n"
  9489. " : a(a)\n"
  9490. " , b(b)\n"
  9491. " , c(c) {}",
  9492. Style);
  9493. verifyFormat("SomeClass::Constructor()\n"
  9494. " : a(a) {}",
  9495. Style);
  9496. Style.ColumnLimit = 0;
  9497. verifyFormat("SomeClass::Constructor()\n"
  9498. " : a(a) {}",
  9499. Style);
  9500. verifyFormat("SomeClass::Constructor()\n"
  9501. " : a(a)\n"
  9502. " , b(b)\n"
  9503. " , c(c) {}",
  9504. Style);
  9505. verifyFormat("SomeClass::Constructor()\n"
  9506. " : a(a) {\n"
  9507. " foo();\n"
  9508. " bar();\n"
  9509. "}",
  9510. Style);
  9511. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  9512. verifyFormat("SomeClass::Constructor()\n"
  9513. " : a(a)\n"
  9514. " , b(b)\n"
  9515. " , c(c) {\n}",
  9516. Style);
  9517. verifyFormat("SomeClass::Constructor()\n"
  9518. " : a(a) {\n}",
  9519. Style);
  9520. Style.ColumnLimit = 80;
  9521. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
  9522. Style.ConstructorInitializerIndentWidth = 2;
  9523. verifyFormat("SomeClass::Constructor()\n"
  9524. " : a(a)\n"
  9525. " , b(b)\n"
  9526. " , c(c) {}",
  9527. Style);
  9528. Style.ConstructorInitializerIndentWidth = 0;
  9529. verifyFormat("SomeClass::Constructor()\n"
  9530. ": a(a)\n"
  9531. ", b(b)\n"
  9532. ", c(c) {}",
  9533. Style);
  9534. Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  9535. Style.ConstructorInitializerIndentWidth = 4;
  9536. verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
  9537. verifyFormat(
  9538. "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
  9539. Style);
  9540. verifyFormat(
  9541. "SomeClass::Constructor()\n"
  9542. " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
  9543. Style);
  9544. Style.ConstructorInitializerIndentWidth = 4;
  9545. Style.ColumnLimit = 60;
  9546. verifyFormat("SomeClass::Constructor()\n"
  9547. " : aaaaaaaa(aaaaaaaa)\n"
  9548. " , aaaaaaaa(aaaaaaaa)\n"
  9549. " , aaaaaaaa(aaaaaaaa) {}",
  9550. Style);
  9551. }
  9552. TEST_F(FormatTest, Destructors) {
  9553. verifyFormat("void F(int &i) { i.~int(); }");
  9554. verifyFormat("void F(int &i) { i->~int(); }");
  9555. }
  9556. TEST_F(FormatTest, FormatsWithWebKitStyle) {
  9557. FormatStyle Style = getWebKitStyle();
  9558. // Don't indent in outer namespaces.
  9559. verifyFormat("namespace outer {\n"
  9560. "int i;\n"
  9561. "namespace inner {\n"
  9562. " int i;\n"
  9563. "} // namespace inner\n"
  9564. "} // namespace outer\n"
  9565. "namespace other_outer {\n"
  9566. "int i;\n"
  9567. "}",
  9568. Style);
  9569. // Don't indent case labels.
  9570. verifyFormat("switch (variable) {\n"
  9571. "case 1:\n"
  9572. "case 2:\n"
  9573. " doSomething();\n"
  9574. " break;\n"
  9575. "default:\n"
  9576. " ++variable;\n"
  9577. "}",
  9578. Style);
  9579. // Wrap before binary operators.
  9580. EXPECT_EQ("void f()\n"
  9581. "{\n"
  9582. " if (aaaaaaaaaaaaaaaa\n"
  9583. " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
  9584. " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  9585. " return;\n"
  9586. "}",
  9587. format("void f() {\n"
  9588. "if (aaaaaaaaaaaaaaaa\n"
  9589. "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
  9590. "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
  9591. "return;\n"
  9592. "}",
  9593. Style));
  9594. // Allow functions on a single line.
  9595. verifyFormat("void f() { return; }", Style);
  9596. // Constructor initializers are formatted one per line with the "," on the
  9597. // new line.
  9598. verifyFormat("Constructor()\n"
  9599. " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  9600. " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
  9601. " aaaaaaaaaaaaaa)\n"
  9602. " , aaaaaaaaaaaaaaaaaaaaaaa()\n"
  9603. "{\n"
  9604. "}",
  9605. Style);
  9606. verifyFormat("SomeClass::Constructor()\n"
  9607. " : a(a)\n"
  9608. "{\n"
  9609. "}",
  9610. Style);
  9611. EXPECT_EQ("SomeClass::Constructor()\n"
  9612. " : a(a)\n"
  9613. "{\n"
  9614. "}",
  9615. format("SomeClass::Constructor():a(a){}", Style));
  9616. verifyFormat("SomeClass::Constructor()\n"
  9617. " : a(a)\n"
  9618. " , b(b)\n"
  9619. " , c(c)\n"
  9620. "{\n"
  9621. "}",
  9622. Style);
  9623. verifyFormat("SomeClass::Constructor()\n"
  9624. " : a(a)\n"
  9625. "{\n"
  9626. " foo();\n"
  9627. " bar();\n"
  9628. "}",
  9629. Style);
  9630. // Access specifiers should be aligned left.
  9631. verifyFormat("class C {\n"
  9632. "public:\n"
  9633. " int i;\n"
  9634. "};",
  9635. Style);
  9636. // Do not align comments.
  9637. verifyFormat("int a; // Do not\n"
  9638. "double b; // align comments.",
  9639. Style);
  9640. // Do not align operands.
  9641. EXPECT_EQ("ASSERT(aaaa\n"
  9642. " || bbbb);",
  9643. format("ASSERT ( aaaa\n||bbbb);", Style));
  9644. // Accept input's line breaks.
  9645. EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
  9646. " || bbbbbbbbbbbbbbb) {\n"
  9647. " i++;\n"
  9648. "}",
  9649. format("if (aaaaaaaaaaaaaaa\n"
  9650. "|| bbbbbbbbbbbbbbb) { i++; }",
  9651. Style));
  9652. EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
  9653. " i++;\n"
  9654. "}",
  9655. format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
  9656. // Don't automatically break all macro definitions (llvm.org/PR17842).
  9657. verifyFormat("#define aNumber 10", Style);
  9658. // However, generally keep the line breaks that the user authored.
  9659. EXPECT_EQ("#define aNumber \\\n"
  9660. " 10",
  9661. format("#define aNumber \\\n"
  9662. " 10",
  9663. Style));
  9664. // Keep empty and one-element array literals on a single line.
  9665. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
  9666. " copyItems:YES];",
  9667. format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
  9668. "copyItems:YES];",
  9669. Style));
  9670. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
  9671. " copyItems:YES];",
  9672. format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
  9673. " copyItems:YES];",
  9674. Style));
  9675. // FIXME: This does not seem right, there should be more indentation before
  9676. // the array literal's entries. Nested blocks have the same problem.
  9677. EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
  9678. " @\"a\",\n"
  9679. " @\"a\"\n"
  9680. "]\n"
  9681. " copyItems:YES];",
  9682. format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
  9683. " @\"a\",\n"
  9684. " @\"a\"\n"
  9685. " ]\n"
  9686. " copyItems:YES];",
  9687. Style));
  9688. EXPECT_EQ(
  9689. "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
  9690. " copyItems:YES];",
  9691. format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
  9692. " copyItems:YES];",
  9693. Style));
  9694. verifyFormat("[self.a b:c c:d];", Style);
  9695. EXPECT_EQ("[self.a b:c\n"
  9696. " c:d];",
  9697. format("[self.a b:c\n"
  9698. "c:d];",
  9699. Style));
  9700. }
  9701. TEST_F(FormatTest, FormatsLambdas) {
  9702. verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
  9703. verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
  9704. verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
  9705. verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
  9706. verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
  9707. verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
  9708. verifyFormat("void f() {\n"
  9709. " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
  9710. "}\n");
  9711. verifyFormat("void f() {\n"
  9712. " other(x.begin(), //\n"
  9713. " x.end(), //\n"
  9714. " [&](int, int) { return 1; });\n"
  9715. "}\n");
  9716. verifyFormat("SomeFunction([]() { // A cool function...\n"
  9717. " return 43;\n"
  9718. "});");
  9719. EXPECT_EQ("SomeFunction([]() {\n"
  9720. "#define A a\n"
  9721. " return 43;\n"
  9722. "});",
  9723. format("SomeFunction([](){\n"
  9724. "#define A a\n"
  9725. "return 43;\n"
  9726. "});"));
  9727. verifyFormat("void f() {\n"
  9728. " SomeFunction([](decltype(x), A *a) {});\n"
  9729. "}");
  9730. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  9731. " [](const aaaaaaaaaa &a) { return a; });");
  9732. verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
  9733. " SomeOtherFunctioooooooooooooooooooooooooon();\n"
  9734. "});");
  9735. verifyFormat("Constructor()\n"
  9736. " : Field([] { // comment\n"
  9737. " int i;\n"
  9738. " }) {}");
  9739. verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
  9740. " return some_parameter.size();\n"
  9741. "};");
  9742. verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
  9743. " [](const string &s) { return s; };");
  9744. verifyFormat("int i = aaaaaa ? 1 //\n"
  9745. " : [] {\n"
  9746. " return 2; //\n"
  9747. " }();");
  9748. verifyFormat("llvm::errs() << \"number of twos is \"\n"
  9749. " << std::count_if(v.begin(), v.end(), [](int x) {\n"
  9750. " return x == 2; // force break\n"
  9751. " });");
  9752. verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n"
  9753. " int iiiiiiiiiiii) {\n"
  9754. " return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n"
  9755. "});",
  9756. getLLVMStyleWithColumns(60));
  9757. verifyFormat("SomeFunction({[&] {\n"
  9758. " // comment\n"
  9759. " },\n"
  9760. " [&] {\n"
  9761. " // comment\n"
  9762. " }});");
  9763. verifyFormat("SomeFunction({[&] {\n"
  9764. " // comment\n"
  9765. "}});");
  9766. verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n"
  9767. " [&]() { return true; },\n"
  9768. " aaaaa aaaaaaaaa);");
  9769. // Lambdas with return types.
  9770. verifyFormat("int c = []() -> int { return 2; }();\n");
  9771. verifyFormat("int c = []() -> int * { return 2; }();\n");
  9772. verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
  9773. verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
  9774. verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
  9775. verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
  9776. verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
  9777. verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
  9778. verifyFormat("[a, a]() -> a<1> {};");
  9779. verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
  9780. " int j) -> int {\n"
  9781. " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
  9782. "};");
  9783. verifyFormat(
  9784. "aaaaaaaaaaaaaaaaaaaaaa(\n"
  9785. " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
  9786. " return aaaaaaaaaaaaaaaaa;\n"
  9787. " });",
  9788. getLLVMStyleWithColumns(70));
  9789. // Multiple lambdas in the same parentheses change indentation rules.
  9790. verifyFormat("SomeFunction(\n"
  9791. " []() {\n"
  9792. " int i = 42;\n"
  9793. " return i;\n"
  9794. " },\n"
  9795. " []() {\n"
  9796. " int j = 43;\n"
  9797. " return j;\n"
  9798. " });");
  9799. // More complex introducers.
  9800. verifyFormat("return [i, args...] {};");
  9801. // Not lambdas.
  9802. verifyFormat("constexpr char hello[]{\"hello\"};");
  9803. verifyFormat("double &operator[](int i) { return 0; }\n"
  9804. "int i;");
  9805. verifyFormat("std::unique_ptr<int[]> foo() {}");
  9806. verifyFormat("int i = a[a][a]->f();");
  9807. verifyFormat("int i = (*b)[a]->f();");
  9808. // Other corner cases.
  9809. verifyFormat("void f() {\n"
  9810. " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
  9811. " );\n"
  9812. "}");
  9813. // Lambdas created through weird macros.
  9814. verifyFormat("void f() {\n"
  9815. " MACRO((const AA &a) { return 1; });\n"
  9816. "}");
  9817. verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
  9818. " doo_dah();\n"
  9819. " doo_dah();\n"
  9820. " })) {\n"
  9821. "}");
  9822. verifyFormat("auto lambda = []() {\n"
  9823. " int a = 2\n"
  9824. "#if A\n"
  9825. " + 2\n"
  9826. "#endif\n"
  9827. " ;\n"
  9828. "};");
  9829. }
  9830. TEST_F(FormatTest, FormatsBlocks) {
  9831. FormatStyle ShortBlocks = getLLVMStyle();
  9832. ShortBlocks.AllowShortBlocksOnASingleLine = true;
  9833. verifyFormat("int (^Block)(int, int);", ShortBlocks);
  9834. verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
  9835. verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
  9836. verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
  9837. verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
  9838. verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
  9839. verifyFormat("foo(^{ bar(); });", ShortBlocks);
  9840. verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
  9841. verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
  9842. verifyFormat("[operation setCompletionBlock:^{\n"
  9843. " [self onOperationDone];\n"
  9844. "}];");
  9845. verifyFormat("int i = {[operation setCompletionBlock:^{\n"
  9846. " [self onOperationDone];\n"
  9847. "}]};");
  9848. verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
  9849. " f();\n"
  9850. "}];");
  9851. verifyFormat("int a = [operation block:^int(int *i) {\n"
  9852. " return 1;\n"
  9853. "}];");
  9854. verifyFormat("[myObject doSomethingWith:arg1\n"
  9855. " aaa:^int(int *a) {\n"
  9856. " return 1;\n"
  9857. " }\n"
  9858. " bbb:f(a * bbbbbbbb)];");
  9859. verifyFormat("[operation setCompletionBlock:^{\n"
  9860. " [self.delegate newDataAvailable];\n"
  9861. "}];",
  9862. getLLVMStyleWithColumns(60));
  9863. verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
  9864. " NSString *path = [self sessionFilePath];\n"
  9865. " if (path) {\n"
  9866. " // ...\n"
  9867. " }\n"
  9868. "});");
  9869. verifyFormat("[[SessionService sharedService]\n"
  9870. " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  9871. " if (window) {\n"
  9872. " [self windowDidLoad:window];\n"
  9873. " } else {\n"
  9874. " [self errorLoadingWindow];\n"
  9875. " }\n"
  9876. " }];");
  9877. verifyFormat("void (^largeBlock)(void) = ^{\n"
  9878. " // ...\n"
  9879. "};\n",
  9880. getLLVMStyleWithColumns(40));
  9881. verifyFormat("[[SessionService sharedService]\n"
  9882. " loadWindowWithCompletionBlock: //\n"
  9883. " ^(SessionWindow *window) {\n"
  9884. " if (window) {\n"
  9885. " [self windowDidLoad:window];\n"
  9886. " } else {\n"
  9887. " [self errorLoadingWindow];\n"
  9888. " }\n"
  9889. " }];",
  9890. getLLVMStyleWithColumns(60));
  9891. verifyFormat("[myObject doSomethingWith:arg1\n"
  9892. " firstBlock:^(Foo *a) {\n"
  9893. " // ...\n"
  9894. " int i;\n"
  9895. " }\n"
  9896. " secondBlock:^(Bar *b) {\n"
  9897. " // ...\n"
  9898. " int i;\n"
  9899. " }\n"
  9900. " thirdBlock:^Foo(Bar *b) {\n"
  9901. " // ...\n"
  9902. " int i;\n"
  9903. " }];");
  9904. verifyFormat("[myObject doSomethingWith:arg1\n"
  9905. " firstBlock:-1\n"
  9906. " secondBlock:^(Bar *b) {\n"
  9907. " // ...\n"
  9908. " int i;\n"
  9909. " }];");
  9910. verifyFormat("f(^{\n"
  9911. " @autoreleasepool {\n"
  9912. " if (a) {\n"
  9913. " g();\n"
  9914. " }\n"
  9915. " }\n"
  9916. "});");
  9917. verifyFormat("Block b = ^int *(A *a, B *b) {}");
  9918. FormatStyle FourIndent = getLLVMStyle();
  9919. FourIndent.ObjCBlockIndentWidth = 4;
  9920. verifyFormat("[operation setCompletionBlock:^{\n"
  9921. " [self onOperationDone];\n"
  9922. "}];",
  9923. FourIndent);
  9924. }
  9925. TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
  9926. FormatStyle ZeroColumn = getLLVMStyle();
  9927. ZeroColumn.ColumnLimit = 0;
  9928. verifyFormat("[[SessionService sharedService] "
  9929. "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  9930. " if (window) {\n"
  9931. " [self windowDidLoad:window];\n"
  9932. " } else {\n"
  9933. " [self errorLoadingWindow];\n"
  9934. " }\n"
  9935. "}];",
  9936. ZeroColumn);
  9937. EXPECT_EQ("[[SessionService sharedService]\n"
  9938. " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  9939. " if (window) {\n"
  9940. " [self windowDidLoad:window];\n"
  9941. " } else {\n"
  9942. " [self errorLoadingWindow];\n"
  9943. " }\n"
  9944. " }];",
  9945. format("[[SessionService sharedService]\n"
  9946. "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
  9947. " if (window) {\n"
  9948. " [self windowDidLoad:window];\n"
  9949. " } else {\n"
  9950. " [self errorLoadingWindow];\n"
  9951. " }\n"
  9952. "}];",
  9953. ZeroColumn));
  9954. verifyFormat("[myObject doSomethingWith:arg1\n"
  9955. " firstBlock:^(Foo *a) {\n"
  9956. " // ...\n"
  9957. " int i;\n"
  9958. " }\n"
  9959. " secondBlock:^(Bar *b) {\n"
  9960. " // ...\n"
  9961. " int i;\n"
  9962. " }\n"
  9963. " thirdBlock:^Foo(Bar *b) {\n"
  9964. " // ...\n"
  9965. " int i;\n"
  9966. " }];",
  9967. ZeroColumn);
  9968. verifyFormat("f(^{\n"
  9969. " @autoreleasepool {\n"
  9970. " if (a) {\n"
  9971. " g();\n"
  9972. " }\n"
  9973. " }\n"
  9974. "});",
  9975. ZeroColumn);
  9976. verifyFormat("void (^largeBlock)(void) = ^{\n"
  9977. " // ...\n"
  9978. "};",
  9979. ZeroColumn);
  9980. ZeroColumn.AllowShortBlocksOnASingleLine = true;
  9981. EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
  9982. format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn));
  9983. ZeroColumn.AllowShortBlocksOnASingleLine = false;
  9984. EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
  9985. " int i;\n"
  9986. "};",
  9987. format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn));
  9988. }
  9989. TEST_F(FormatTest, SupportsCRLF) {
  9990. EXPECT_EQ("int a;\r\n"
  9991. "int b;\r\n"
  9992. "int c;\r\n",
  9993. format("int a;\r\n"
  9994. " int b;\r\n"
  9995. " int c;\r\n",
  9996. getLLVMStyle()));
  9997. EXPECT_EQ("int a;\r\n"
  9998. "int b;\r\n"
  9999. "int c;\r\n",
  10000. format("int a;\r\n"
  10001. " int b;\n"
  10002. " int c;\r\n",
  10003. getLLVMStyle()));
  10004. EXPECT_EQ("int a;\n"
  10005. "int b;\n"
  10006. "int c;\n",
  10007. format("int a;\r\n"
  10008. " int b;\n"
  10009. " int c;\n",
  10010. getLLVMStyle()));
  10011. EXPECT_EQ("\"aaaaaaa \"\r\n"
  10012. "\"bbbbbbb\";\r\n",
  10013. format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
  10014. EXPECT_EQ("#define A \\\r\n"
  10015. " b; \\\r\n"
  10016. " c; \\\r\n"
  10017. " d;\r\n",
  10018. format("#define A \\\r\n"
  10019. " b; \\\r\n"
  10020. " c; d; \r\n",
  10021. getGoogleStyle()));
  10022. EXPECT_EQ("/*\r\n"
  10023. "multi line block comments\r\n"
  10024. "should not introduce\r\n"
  10025. "an extra carriage return\r\n"
  10026. "*/\r\n",
  10027. format("/*\r\n"
  10028. "multi line block comments\r\n"
  10029. "should not introduce\r\n"
  10030. "an extra carriage return\r\n"
  10031. "*/\r\n"));
  10032. }
  10033. TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
  10034. verifyFormat("MY_CLASS(C) {\n"
  10035. " int i;\n"
  10036. " int j;\n"
  10037. "};");
  10038. }
  10039. TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
  10040. FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
  10041. TwoIndent.ContinuationIndentWidth = 2;
  10042. EXPECT_EQ("int i =\n"
  10043. " longFunction(\n"
  10044. " arg);",
  10045. format("int i = longFunction(arg);", TwoIndent));
  10046. FormatStyle SixIndent = getLLVMStyleWithColumns(20);
  10047. SixIndent.ContinuationIndentWidth = 6;
  10048. EXPECT_EQ("int i =\n"
  10049. " longFunction(\n"
  10050. " arg);",
  10051. format("int i = longFunction(arg);", SixIndent));
  10052. }
  10053. TEST_F(FormatTest, SpacesInAngles) {
  10054. FormatStyle Spaces = getLLVMStyle();
  10055. Spaces.SpacesInAngles = true;
  10056. verifyFormat("static_cast< int >(arg);", Spaces);
  10057. verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
  10058. verifyFormat("f< int, float >();", Spaces);
  10059. verifyFormat("template <> g() {}", Spaces);
  10060. verifyFormat("template < std::vector< int > > f() {}", Spaces);
  10061. verifyFormat("std::function< void(int, int) > fct;", Spaces);
  10062. verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
  10063. Spaces);
  10064. Spaces.Standard = FormatStyle::LS_Cpp03;
  10065. Spaces.SpacesInAngles = true;
  10066. verifyFormat("A< A< int > >();", Spaces);
  10067. Spaces.SpacesInAngles = false;
  10068. verifyFormat("A<A<int> >();", Spaces);
  10069. Spaces.Standard = FormatStyle::LS_Cpp11;
  10070. Spaces.SpacesInAngles = true;
  10071. verifyFormat("A< A< int > >();", Spaces);
  10072. Spaces.SpacesInAngles = false;
  10073. verifyFormat("A<A<int>>();", Spaces);
  10074. }
  10075. TEST_F(FormatTest, TripleAngleBrackets) {
  10076. verifyFormat("f<<<1, 1>>>();");
  10077. verifyFormat("f<<<1, 1, 1, s>>>();");
  10078. verifyFormat("f<<<a, b, c, d>>>();");
  10079. EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
  10080. verifyFormat("f<param><<<1, 1>>>();");
  10081. verifyFormat("f<1><<<1, 1>>>();");
  10082. EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
  10083. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  10084. "aaaaaaaaaaa<<<\n 1, 1>>>();");
  10085. }
  10086. TEST_F(FormatTest, MergeLessLessAtEnd) {
  10087. verifyFormat("<<");
  10088. EXPECT_EQ("< < <", format("\\\n<<<"));
  10089. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  10090. "aaallvm::outs() <<");
  10091. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  10092. "aaaallvm::outs()\n <<");
  10093. }
  10094. TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
  10095. std::string code = "#if A\n"
  10096. "#if B\n"
  10097. "a.\n"
  10098. "#endif\n"
  10099. " a = 1;\n"
  10100. "#else\n"
  10101. "#endif\n"
  10102. "#if C\n"
  10103. "#else\n"
  10104. "#endif\n";
  10105. EXPECT_EQ(code, format(code));
  10106. }
  10107. TEST_F(FormatTest, HandleConflictMarkers) {
  10108. // Git/SVN conflict markers.
  10109. EXPECT_EQ("int a;\n"
  10110. "void f() {\n"
  10111. " callme(some(parameter1,\n"
  10112. "<<<<<<< text by the vcs\n"
  10113. " parameter2),\n"
  10114. "||||||| text by the vcs\n"
  10115. " parameter2),\n"
  10116. " parameter3,\n"
  10117. "======= text by the vcs\n"
  10118. " parameter2, parameter3),\n"
  10119. ">>>>>>> text by the vcs\n"
  10120. " otherparameter);\n",
  10121. format("int a;\n"
  10122. "void f() {\n"
  10123. " callme(some(parameter1,\n"
  10124. "<<<<<<< text by the vcs\n"
  10125. " parameter2),\n"
  10126. "||||||| text by the vcs\n"
  10127. " parameter2),\n"
  10128. " parameter3,\n"
  10129. "======= text by the vcs\n"
  10130. " parameter2,\n"
  10131. " parameter3),\n"
  10132. ">>>>>>> text by the vcs\n"
  10133. " otherparameter);\n"));
  10134. // Perforce markers.
  10135. EXPECT_EQ("void f() {\n"
  10136. " function(\n"
  10137. ">>>> text by the vcs\n"
  10138. " parameter,\n"
  10139. "==== text by the vcs\n"
  10140. " parameter,\n"
  10141. "==== text by the vcs\n"
  10142. " parameter,\n"
  10143. "<<<< text by the vcs\n"
  10144. " parameter);\n",
  10145. format("void f() {\n"
  10146. " function(\n"
  10147. ">>>> text by the vcs\n"
  10148. " parameter,\n"
  10149. "==== text by the vcs\n"
  10150. " parameter,\n"
  10151. "==== text by the vcs\n"
  10152. " parameter,\n"
  10153. "<<<< text by the vcs\n"
  10154. " parameter);\n"));
  10155. EXPECT_EQ("<<<<<<<\n"
  10156. "|||||||\n"
  10157. "=======\n"
  10158. ">>>>>>>",
  10159. format("<<<<<<<\n"
  10160. "|||||||\n"
  10161. "=======\n"
  10162. ">>>>>>>"));
  10163. EXPECT_EQ("<<<<<<<\n"
  10164. "|||||||\n"
  10165. "int i;\n"
  10166. "=======\n"
  10167. ">>>>>>>",
  10168. format("<<<<<<<\n"
  10169. "|||||||\n"
  10170. "int i;\n"
  10171. "=======\n"
  10172. ">>>>>>>"));
  10173. // FIXME: Handle parsing of macros around conflict markers correctly:
  10174. EXPECT_EQ("#define Macro \\\n"
  10175. "<<<<<<<\n"
  10176. "Something \\\n"
  10177. "|||||||\n"
  10178. "Else \\\n"
  10179. "=======\n"
  10180. "Other \\\n"
  10181. ">>>>>>>\n"
  10182. " End int i;\n",
  10183. format("#define Macro \\\n"
  10184. "<<<<<<<\n"
  10185. " Something \\\n"
  10186. "|||||||\n"
  10187. " Else \\\n"
  10188. "=======\n"
  10189. " Other \\\n"
  10190. ">>>>>>>\n"
  10191. " End\n"
  10192. "int i;\n"));
  10193. }
  10194. TEST_F(FormatTest, DisableRegions) {
  10195. EXPECT_EQ("int i;\n"
  10196. "// clang-format off\n"
  10197. " int j;\n"
  10198. "// clang-format on\n"
  10199. "int k;",
  10200. format(" int i;\n"
  10201. " // clang-format off\n"
  10202. " int j;\n"
  10203. " // clang-format on\n"
  10204. " int k;"));
  10205. EXPECT_EQ("int i;\n"
  10206. "/* clang-format off */\n"
  10207. " int j;\n"
  10208. "/* clang-format on */\n"
  10209. "int k;",
  10210. format(" int i;\n"
  10211. " /* clang-format off */\n"
  10212. " int j;\n"
  10213. " /* clang-format on */\n"
  10214. " int k;"));
  10215. }
  10216. TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
  10217. format("? ) =");
  10218. verifyNoCrash("#define a\\\n /**/}");
  10219. }
  10220. TEST_F(FormatTest, FormatsTableGenCode) {
  10221. FormatStyle Style = getLLVMStyle();
  10222. Style.Language = FormatStyle::LK_TableGen;
  10223. verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
  10224. }
  10225. } // end namespace
  10226. } // end namespace format
  10227. } // end namespace clang